2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2007 Nate Lawson (SDG)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
41 #include <sys/queue.h>
43 #include <sys/sched.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
48 #include <sys/timetc.h>
49 #include <sys/taskqueue.h>
51 #include "cpufreq_if.h"
54 * Common CPU frequency glue code. Drivers for specific hardware can
55 * attach this interface to allow users to get/set the CPU frequency.
59 * Number of levels we can handle. Levels are synthesized from settings
60 * so for M settings and N drivers, there may be M*N levels.
62 #define CF_MAX_LEVELS 256
64 struct cf_saved_freq {
65 struct cf_level level;
67 SLIST_ENTRY(cf_saved_freq) link;
70 struct cpufreq_softc {
72 struct cf_level curr_level;
74 SLIST_HEAD(, cf_saved_freq) saved_freq;
75 struct cf_level_lst all_levels;
79 struct sysctl_ctx_list sysctl_ctx;
80 struct task startup_task;
81 struct cf_level *levels_buf;
84 struct cf_setting_array {
85 struct cf_setting sets[MAX_SETTINGS];
87 TAILQ_ENTRY(cf_setting_array) link;
90 TAILQ_HEAD(cf_setting_lst, cf_setting_array);
92 #define CF_MTX_INIT(x) sx_init((x), "cpufreq lock")
93 #define CF_MTX_LOCK(x) sx_xlock((x))
94 #define CF_MTX_UNLOCK(x) sx_xunlock((x))
95 #define CF_MTX_ASSERT(x) sx_assert((x), SX_XLOCKED)
97 #define CF_DEBUG(msg...) do { \
99 printf("cpufreq: " msg); \
102 static int cpufreq_attach(device_t dev);
103 static void cpufreq_startup_task(void *ctx, int pending);
104 static int cpufreq_detach(device_t dev);
105 static int cf_set_method(device_t dev, const struct cf_level *level,
107 static int cf_get_method(device_t dev, struct cf_level *level);
108 static int cf_levels_method(device_t dev, struct cf_level *levels,
110 static int cpufreq_insert_abs(struct cpufreq_softc *sc,
111 struct cf_setting *sets, int count);
112 static int cpufreq_expand_set(struct cpufreq_softc *sc,
113 struct cf_setting_array *set_arr);
114 static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
115 struct cf_level *dup, struct cf_setting *set);
116 static int cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
117 static int cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
118 static int cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
120 static device_method_t cpufreq_methods[] = {
121 DEVMETHOD(device_probe, bus_generic_probe),
122 DEVMETHOD(device_attach, cpufreq_attach),
123 DEVMETHOD(device_detach, cpufreq_detach),
125 DEVMETHOD(cpufreq_set, cf_set_method),
126 DEVMETHOD(cpufreq_get, cf_get_method),
127 DEVMETHOD(cpufreq_levels, cf_levels_method),
130 static driver_t cpufreq_driver = {
131 "cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
133 static devclass_t cpufreq_dc;
134 DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
136 static int cf_lowest_freq;
137 static int cf_verbose;
138 static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL,
139 "cpufreq debugging");
140 SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RWTUN, &cf_lowest_freq, 1,
141 "Don't provide levels below this frequency.");
142 SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RWTUN, &cf_verbose, 1,
143 "Print verbose debugging messages");
146 cpufreq_attach(device_t dev)
148 struct cpufreq_softc *sc;
154 CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
155 sc = device_get_softc(dev);
156 parent = device_get_parent(dev);
158 sysctl_ctx_init(&sc->sysctl_ctx);
159 TAILQ_INIT(&sc->all_levels);
160 CF_MTX_INIT(&sc->lock);
161 sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
162 SLIST_INIT(&sc->saved_freq);
163 /* Try to get nominal CPU freq to use it as maximum later if needed */
164 sc->max_mhz = cpu_get_nominal_mhz(dev);
165 /* If that fails, try to measure the current rate */
166 if (sc->max_mhz <= 0) {
167 pc = cpu_get_pcpu(dev);
168 if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
169 sc->max_mhz = rate / 1000000;
171 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
175 * Only initialize one set of sysctls for all CPUs. In the future,
176 * if multiple CPUs can have different settings, we can move these
177 * sysctls to be under every CPU instead of just the first one.
179 numdevs = devclass_get_count(cpufreq_dc);
183 CF_DEBUG("initializing one-time data for %s\n",
184 device_get_nameunit(dev));
185 sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf),
187 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
188 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
189 OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
190 cpufreq_curr_sysctl, "I", "Current CPU frequency");
191 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
192 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
193 OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
194 cpufreq_levels_sysctl, "A", "CPU frequency levels");
197 * Queue a one-shot broadcast that levels have changed.
198 * It will run once the system has completed booting.
200 TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
201 taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
206 /* Handle any work to be done for all drivers that attached during boot. */
208 cpufreq_startup_task(void *ctx, int pending)
211 cpufreq_settings_changed((device_t)ctx);
215 cpufreq_detach(device_t dev)
217 struct cpufreq_softc *sc;
218 struct cf_saved_freq *saved_freq;
221 CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
222 sc = device_get_softc(dev);
223 sysctl_ctx_free(&sc->sysctl_ctx);
225 while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
226 SLIST_REMOVE_HEAD(&sc->saved_freq, link);
227 free(saved_freq, M_TEMP);
230 /* Only clean up these resources when the last device is detaching. */
231 numdevs = devclass_get_count(cpufreq_dc);
233 CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
234 free(sc->levels_buf, M_DEVBUF);
241 cf_set_method(device_t dev, const struct cf_level *level, int priority)
243 struct cpufreq_softc *sc;
244 const struct cf_setting *set;
245 struct cf_saved_freq *saved_freq, *curr_freq;
250 sc = device_get_softc(dev);
255 /* We are going to change levels so notify the pre-change handler. */
256 EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
258 EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
262 CF_MTX_LOCK(&sc->lock);
265 #ifdef EARLY_AP_STARTUP
266 MPASS(mp_ncpus == 1 || smp_started);
269 * If still booting and secondary CPUs not started yet, don't allow
270 * changing the frequency until they're online. This is because we
271 * can't switch to them using sched_bind() and thus we'd only be
272 * switching the main CPU. XXXTODO: Need to think more about how to
273 * handle having different CPUs at different frequencies.
275 if (mp_ncpus > 1 && !smp_started) {
276 device_printf(dev, "rejecting change, SMP not started yet\n");
284 * If the requested level has a lower priority, don't allow
285 * the new level right now.
287 if (priority < sc->curr_priority) {
288 CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
295 * If the caller didn't specify a level and one is saved, prepare to
296 * restore the saved level. If none has been saved, return an error.
299 saved_freq = SLIST_FIRST(&sc->saved_freq);
300 if (saved_freq == NULL) {
301 CF_DEBUG("NULL level, no saved level\n");
305 level = &saved_freq->level;
306 priority = saved_freq->priority;
307 CF_DEBUG("restoring saved level, freq %d prio %d\n",
308 level->total_set.freq, priority);
311 /* Reject levels that are below our specified threshold. */
312 if (level->total_set.freq < cf_lowest_freq) {
313 CF_DEBUG("rejecting freq %d, less than %d limit\n",
314 level->total_set.freq, cf_lowest_freq);
319 /* If already at this level, just return. */
320 if (sc->curr_level.total_set.freq == level->total_set.freq) {
321 CF_DEBUG("skipping freq %d, same as current level %d\n",
322 level->total_set.freq, sc->curr_level.total_set.freq);
326 /* First, set the absolute frequency via its driver. */
327 set = &level->abs_set;
329 if (!device_is_attached(set->dev)) {
334 /* Bind to the target CPU before switching. */
335 pc = cpu_get_pcpu(set->dev);
337 /* Skip settings if CPU is not started. */
342 thread_lock(curthread);
343 pri = curthread->td_priority;
344 sched_prio(curthread, PRI_MIN);
345 sched_bind(curthread, pc->pc_cpuid);
346 thread_unlock(curthread);
347 CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
348 device_get_nameunit(set->dev), PCPU_GET(cpuid));
349 error = CPUFREQ_DRV_SET(set->dev, set);
350 thread_lock(curthread);
351 sched_unbind(curthread);
352 sched_prio(curthread, pri);
353 thread_unlock(curthread);
359 /* Next, set any/all relative frequencies via their drivers. */
360 for (i = 0; i < level->rel_count; i++) {
361 set = &level->rel_set[i];
362 if (!device_is_attached(set->dev)) {
367 /* Bind to the target CPU before switching. */
368 pc = cpu_get_pcpu(set->dev);
369 thread_lock(curthread);
370 pri = curthread->td_priority;
371 sched_prio(curthread, PRI_MIN);
372 sched_bind(curthread, pc->pc_cpuid);
373 thread_unlock(curthread);
374 CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
375 device_get_nameunit(set->dev), PCPU_GET(cpuid));
376 error = CPUFREQ_DRV_SET(set->dev, set);
377 thread_lock(curthread);
378 sched_unbind(curthread);
379 sched_prio(curthread, pri);
380 thread_unlock(curthread);
382 /* XXX Back out any successful setting? */
389 * Before recording the current level, check if we're going to a
390 * higher priority. If so, save the previous level and priority.
392 if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
393 priority > sc->curr_priority) {
394 CF_DEBUG("saving level, freq %d prio %d\n",
395 sc->curr_level.total_set.freq, sc->curr_priority);
396 curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
397 if (curr_freq == NULL) {
401 curr_freq->level = sc->curr_level;
402 curr_freq->priority = sc->curr_priority;
403 SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
405 sc->curr_level = *level;
406 sc->curr_priority = priority;
408 /* If we were restoring a saved state, reset it to "unused". */
409 if (saved_freq != NULL) {
410 CF_DEBUG("resetting saved level\n");
411 sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
412 SLIST_REMOVE_HEAD(&sc->saved_freq, link);
413 free(saved_freq, M_TEMP);
417 CF_MTX_UNLOCK(&sc->lock);
420 * We changed levels (or attempted to) so notify the post-change
421 * handler of new frequency or error.
423 EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
425 device_printf(set->dev, "set freq failed, err %d\n", error);
431 cf_get_method(device_t dev, struct cf_level *level)
433 struct cpufreq_softc *sc;
434 struct cf_level *levels;
435 struct cf_setting *curr_set, set;
438 int bdiff, count, diff, error, i, n, numdevs;
441 sc = device_get_softc(dev);
445 /* If we already know the current frequency, we're done. */
446 CF_MTX_LOCK(&sc->lock);
447 curr_set = &sc->curr_level.total_set;
448 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
449 CF_DEBUG("get returning known freq %d\n", curr_set->freq);
452 CF_MTX_UNLOCK(&sc->lock);
455 * We need to figure out the current level. Loop through every
456 * driver, getting the current setting. Then, attempt to get a best
457 * match of settings against each level.
459 count = CF_MAX_LEVELS;
460 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
463 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
466 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
467 free(levels, M_TEMP);
470 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
472 free(levels, M_TEMP);
477 * Reacquire the lock and search for the given level.
479 * XXX Note: this is not quite right since we really need to go
480 * through each level and compare both absolute and relative
481 * settings for each driver in the system before making a match.
482 * The estimation code below catches this case though.
484 CF_MTX_LOCK(&sc->lock);
485 for (n = 0; n < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; n++) {
486 if (!device_is_attached(devs[n]))
488 if (CPUFREQ_DRV_GET(devs[n], &set) != 0)
490 for (i = 0; i < count; i++) {
491 if (set.freq == levels[i].total_set.freq) {
492 sc->curr_level = levels[i];
498 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
499 CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
504 * We couldn't find an exact match, so attempt to estimate and then
505 * match against a level.
507 pc = cpu_get_pcpu(dev);
512 cpu_est_clockrate(pc->pc_cpuid, &rate);
515 for (i = 0; i < count; i++) {
516 diff = abs(levels[i].total_set.freq - rate);
519 sc->curr_level = levels[i];
522 CF_DEBUG("get estimated freq %d\n", curr_set->freq);
526 *level = sc->curr_level;
528 CF_MTX_UNLOCK(&sc->lock);
530 free(levels, M_TEMP);
535 cf_levels_method(device_t dev, struct cf_level *levels, int *count)
537 struct cf_setting_array *set_arr;
538 struct cf_setting_lst rel_sets;
539 struct cpufreq_softc *sc;
540 struct cf_level *lev;
541 struct cf_setting *sets;
544 int error, i, numdevs, set_count, type;
547 if (levels == NULL || count == NULL)
550 TAILQ_INIT(&rel_sets);
551 sc = device_get_softc(dev);
552 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
555 sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
561 /* Get settings from all cpufreq drivers. */
562 CF_MTX_LOCK(&sc->lock);
563 for (i = 0; i < numdevs; i++) {
564 /* Skip devices that aren't ready. */
565 if (!device_is_attached(devs[i]))
569 * Get settings, skipping drivers that offer no settings or
570 * provide settings for informational purposes only.
572 error = CPUFREQ_DRV_TYPE(devs[i], &type);
573 if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
575 CF_DEBUG("skipping info-only driver %s\n",
576 device_get_nameunit(devs[i]));
580 set_count = MAX_SETTINGS;
581 error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
582 if (error || set_count == 0)
585 /* Add the settings to our absolute/relative lists. */
586 switch (type & CPUFREQ_TYPE_MASK) {
587 case CPUFREQ_TYPE_ABSOLUTE:
588 error = cpufreq_insert_abs(sc, sets, set_count);
590 case CPUFREQ_TYPE_RELATIVE:
591 CF_DEBUG("adding %d relative settings\n", set_count);
592 set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
593 if (set_arr == NULL) {
597 bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
598 set_arr->count = set_count;
599 TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
609 * If there are no absolute levels, create a fake one at 100%. We
610 * then cache the clockrate for later use as our base frequency.
612 if (TAILQ_EMPTY(&sc->all_levels)) {
613 if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
614 sc->max_mhz = cpu_get_nominal_mhz(dev);
616 * If the CPU can't report a rate for 100%, hope
617 * the CPU is running at its nominal rate right now,
618 * and use that instead.
620 if (sc->max_mhz <= 0) {
621 pc = cpu_get_pcpu(dev);
622 cpu_est_clockrate(pc->pc_cpuid, &rate);
623 sc->max_mhz = rate / 1000000;
626 memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
627 sets[0].freq = sc->max_mhz;
629 error = cpufreq_insert_abs(sc, sets, 1);
634 /* Create a combined list of absolute + relative levels. */
635 TAILQ_FOREACH(set_arr, &rel_sets, link)
636 cpufreq_expand_set(sc, set_arr);
638 /* If the caller doesn't have enough space, return the actual count. */
639 if (sc->all_count > *count) {
640 *count = sc->all_count;
645 /* Finally, output the list of levels. */
647 TAILQ_FOREACH(lev, &sc->all_levels, link) {
649 /* Skip levels that have a frequency that is too low. */
650 if (lev->total_set.freq < cf_lowest_freq) {
658 *count = sc->all_count;
662 /* Clear all levels since we regenerate them each time. */
663 while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
664 TAILQ_REMOVE(&sc->all_levels, lev, link);
669 CF_MTX_UNLOCK(&sc->lock);
670 while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
671 TAILQ_REMOVE(&rel_sets, set_arr, link);
672 free(set_arr, M_TEMP);
680 * Create levels for an array of absolute settings and insert them in
681 * sorted order in the specified list.
684 cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
687 struct cf_level_lst *list;
688 struct cf_level *level, *search;
691 CF_MTX_ASSERT(&sc->lock);
693 list = &sc->all_levels;
694 for (i = 0; i < count; i++) {
695 level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
698 level->abs_set = sets[i];
699 level->total_set = sets[i];
700 level->total_set.dev = NULL;
704 if (TAILQ_EMPTY(list)) {
705 CF_DEBUG("adding abs setting %d at head\n",
707 TAILQ_INSERT_HEAD(list, level, link);
711 TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link)
712 if (sets[i].freq <= search->total_set.freq) {
713 CF_DEBUG("adding abs setting %d after %d\n",
714 sets[i].freq, search->total_set.freq);
715 TAILQ_INSERT_AFTER(list, search, level, link);
721 TAILQ_FOREACH(search, list, link)
722 if (sets[i].freq >= search->total_set.freq) {
723 CF_DEBUG("adding abs setting %d before %d\n",
724 sets[i].freq, search->total_set.freq);
725 TAILQ_INSERT_BEFORE(search, level, link);
735 * Expand a group of relative settings, creating derived levels from them.
738 cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
740 struct cf_level *fill, *search;
741 struct cf_setting *set;
744 CF_MTX_ASSERT(&sc->lock);
747 * Walk the set of all existing levels in reverse. This is so we
748 * create derived states from the lowest absolute settings first
749 * and discard duplicates created from higher absolute settings.
750 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
751 * preferable to 200 Mhz + 25% because absolute settings are more
752 * efficient since they often change the voltage as well.
754 TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
755 /* Add each setting to the level, duplicating if necessary. */
756 for (i = 0; i < set_arr->count; i++) {
757 set = &set_arr->sets[i];
760 * If this setting is less than 100%, split the level
761 * into two and add this setting to the new level.
764 if (set->freq < 10000) {
765 fill = cpufreq_dup_set(sc, search, set);
768 * The new level was a duplicate of an existing
769 * level or its absolute setting is too high
770 * so we freed it. For example, we discard a
771 * derived level of 1000 MHz/25% if a level
772 * of 500 MHz/100% already exists.
778 /* Add this setting to the existing or new level. */
779 KASSERT(fill->rel_count < MAX_SETTINGS,
780 ("cpufreq: too many relative drivers (%d)",
782 fill->rel_set[fill->rel_count] = *set;
785 "expand set added rel setting %d%% to %d level\n",
786 set->freq / 100, fill->total_set.freq);
793 static struct cf_level *
794 cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
795 struct cf_setting *set)
797 struct cf_level_lst *list;
798 struct cf_level *fill, *itr;
799 struct cf_setting *fill_set, *itr_set;
802 CF_MTX_ASSERT(&sc->lock);
805 * Create a new level, copy it from the old one, and update the
806 * total frequency and power by the percentage specified in the
809 fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
813 fill_set = &fill->total_set;
815 ((uint64_t)fill_set->freq * set->freq) / 10000;
816 if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
817 fill_set->power = ((uint64_t)fill_set->power * set->freq)
820 if (set->lat != CPUFREQ_VAL_UNKNOWN) {
821 if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
822 fill_set->lat += set->lat;
824 fill_set->lat = set->lat;
826 CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
829 * If we copied an old level that we already modified (say, at 100%),
830 * we need to remove that setting before adding this one. Since we
831 * process each setting array in order, we know any settings for this
832 * driver will be found at the end.
834 for (i = fill->rel_count; i != 0; i--) {
835 if (fill->rel_set[i - 1].dev != set->dev)
837 CF_DEBUG("removed last relative driver: %s\n",
838 device_get_nameunit(set->dev));
843 * Insert the new level in sorted order. If it is a duplicate of an
844 * existing level (1) or has an absolute setting higher than the
845 * existing level (2), do not add it. We can do this since any such
846 * level is guaranteed use less power. For example (1), a level with
847 * one absolute setting of 800 Mhz uses less power than one composed
848 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
849 * Also for example (2), a level of 800 Mhz/75% is preferable to
850 * 1600 Mhz/25% even though the latter has a lower total frequency.
852 list = &sc->all_levels;
853 KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
854 TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
855 itr_set = &itr->total_set;
856 if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
857 CF_DEBUG("dup set rejecting %d (dupe)\n",
861 } else if (fill_set->freq < itr_set->freq) {
862 if (fill->abs_set.freq <= itr->abs_set.freq) {
864 "dup done, inserting new level %d after %d\n",
865 fill_set->freq, itr_set->freq);
866 TAILQ_INSERT_AFTER(list, itr, fill, link);
869 CF_DEBUG("dup set rejecting %d (abs too big)\n",
877 /* We didn't find a good place for this new level so free it. */
879 CF_DEBUG("dup set freeing new level %d (not optimal)\n",
889 cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
891 struct cpufreq_softc *sc;
892 struct cf_level *levels;
893 int best, count, diff, bdiff, devcount, error, freq, i, n;
898 levels = sc->levels_buf;
900 error = CPUFREQ_GET(sc->dev, &levels[0]);
903 freq = levels[0].total_set.freq;
904 error = sysctl_handle_int(oidp, &freq, 0, req);
905 if (error != 0 || req->newptr == NULL)
909 * While we only call cpufreq_get() on one device (assuming all
910 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
911 * This is needed for some MP systems.
913 error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
916 for (n = 0; n < devcount; n++) {
917 count = CF_MAX_LEVELS;
918 error = CPUFREQ_LEVELS(devs[n], levels, &count);
922 "cpufreq: need to increase CF_MAX_LEVELS\n");
927 for (i = 0; i < count; i++) {
928 diff = abs(levels[i].total_set.freq - freq);
934 error = CPUFREQ_SET(devs[n], &levels[best], CPUFREQ_PRIO_USER);
944 cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
946 struct cpufreq_softc *sc;
947 struct cf_level *levels;
948 struct cf_setting *set;
953 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
955 /* Get settings from the device and generate the output string. */
956 count = CF_MAX_LEVELS;
957 levels = sc->levels_buf;
958 if (levels == NULL) {
962 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
965 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
969 for (i = 0; i < count; i++) {
970 set = &levels[i].total_set;
971 sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
977 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
985 cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
988 struct cf_setting *sets;
990 int error, i, set_count;
992 dev = oidp->oid_arg1;
993 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
995 /* Get settings from the device and generate the output string. */
996 set_count = MAX_SETTINGS;
997 sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
1002 error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
1006 for (i = 0; i < set_count; i++)
1007 sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
1012 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1021 cpufreq_register(device_t dev)
1023 struct cpufreq_softc *sc;
1024 device_t cf_dev, cpu_dev;
1026 /* Add a sysctl to get each driver's settings separately. */
1027 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1028 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1029 OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
1030 cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
1033 * Add only one cpufreq device to each CPU. Currently, all CPUs
1034 * must offer the same levels and be switched at the same time.
1036 cpu_dev = device_get_parent(dev);
1037 if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
1038 sc = device_get_softc(cf_dev);
1039 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
1043 /* Add the child device and possibly sysctls. */
1044 cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
1047 device_quiet(cf_dev);
1049 return (device_probe_and_attach(cf_dev));
1053 cpufreq_unregister(device_t dev)
1055 device_t cf_dev, *devs;
1056 int cfcount, devcount, error, i, type;
1059 * If this is the last cpufreq child device, remove the control
1060 * device as well. We identify cpufreq children by calling a method
1063 error = device_get_children(device_get_parent(dev), &devs, &devcount);
1066 cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
1067 if (cf_dev == NULL) {
1069 "warning: cpufreq_unregister called with no cpufreq device active\n");
1074 for (i = 0; i < devcount; i++) {
1075 if (!device_is_attached(devs[i]))
1077 if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
1081 device_delete_child(device_get_parent(cf_dev), cf_dev);
1088 cpufreq_settings_changed(device_t dev)
1091 EVENTHANDLER_INVOKE(cpufreq_levels_changed,
1092 device_get_unit(device_get_parent(dev)));