2 * Copyright (c) 2004-2007 Nate Lawson (SDG)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/queue.h>
41 #include <sys/sched.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
46 #include <sys/timetc.h>
47 #include <sys/taskqueue.h>
49 #include "cpufreq_if.h"
52 * Common CPU frequency glue code. Drivers for specific hardware can
53 * attach this interface to allow users to get/set the CPU frequency.
57 * Number of levels we can handle. Levels are synthesized from settings
58 * so for M settings and N drivers, there may be M*N levels.
60 #define CF_MAX_LEVELS 64
62 struct cf_saved_freq {
63 struct cf_level level;
65 SLIST_ENTRY(cf_saved_freq) link;
68 struct cpufreq_softc {
70 struct cf_level curr_level;
72 SLIST_HEAD(, cf_saved_freq) saved_freq;
73 struct cf_level_lst all_levels;
77 struct sysctl_ctx_list sysctl_ctx;
78 struct task startup_task;
79 struct cf_level *levels_buf;
82 struct cf_setting_array {
83 struct cf_setting sets[MAX_SETTINGS];
85 TAILQ_ENTRY(cf_setting_array) link;
88 TAILQ_HEAD(cf_setting_lst, cf_setting_array);
90 #define CF_MTX_INIT(x) sx_init((x), "cpufreq lock")
91 #define CF_MTX_LOCK(x) sx_xlock((x))
92 #define CF_MTX_UNLOCK(x) sx_xunlock((x))
93 #define CF_MTX_ASSERT(x) sx_assert((x), SX_XLOCKED)
95 #define CF_DEBUG(msg...) do { \
97 printf("cpufreq: " msg); \
100 static int cpufreq_attach(device_t dev);
101 static void cpufreq_startup_task(void *ctx, int pending);
102 static int cpufreq_detach(device_t dev);
103 static int cf_set_method(device_t dev, const struct cf_level *level,
105 static int cf_get_method(device_t dev, struct cf_level *level);
106 static int cf_levels_method(device_t dev, struct cf_level *levels,
108 static int cpufreq_insert_abs(struct cpufreq_softc *sc,
109 struct cf_setting *sets, int count);
110 static int cpufreq_expand_set(struct cpufreq_softc *sc,
111 struct cf_setting_array *set_arr);
112 static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
113 struct cf_level *dup, struct cf_setting *set);
114 static int cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
115 static int cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
116 static int cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
118 static device_method_t cpufreq_methods[] = {
119 DEVMETHOD(device_probe, bus_generic_probe),
120 DEVMETHOD(device_attach, cpufreq_attach),
121 DEVMETHOD(device_detach, cpufreq_detach),
123 DEVMETHOD(cpufreq_set, cf_set_method),
124 DEVMETHOD(cpufreq_get, cf_get_method),
125 DEVMETHOD(cpufreq_levels, cf_levels_method),
128 static driver_t cpufreq_driver = {
129 "cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
131 static devclass_t cpufreq_dc;
132 DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
134 static int cf_lowest_freq;
135 static int cf_verbose;
136 static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL,
137 "cpufreq debugging");
138 SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RWTUN, &cf_lowest_freq, 1,
139 "Don't provide levels below this frequency.");
140 SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RWTUN, &cf_verbose, 1,
141 "Print verbose debugging messages");
144 cpufreq_attach(device_t dev)
146 struct cpufreq_softc *sc;
152 CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
153 sc = device_get_softc(dev);
154 parent = device_get_parent(dev);
156 sysctl_ctx_init(&sc->sysctl_ctx);
157 TAILQ_INIT(&sc->all_levels);
158 CF_MTX_INIT(&sc->lock);
159 sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
160 SLIST_INIT(&sc->saved_freq);
161 /* Try to get nominal CPU freq to use it as maximum later if needed */
162 sc->max_mhz = cpu_get_nominal_mhz(dev);
163 /* If that fails, try to measure the current rate */
164 if (sc->max_mhz <= 0) {
165 pc = cpu_get_pcpu(dev);
166 if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
167 sc->max_mhz = rate / 1000000;
169 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
173 * Only initialize one set of sysctls for all CPUs. In the future,
174 * if multiple CPUs can have different settings, we can move these
175 * sysctls to be under every CPU instead of just the first one.
177 numdevs = devclass_get_count(cpufreq_dc);
181 CF_DEBUG("initializing one-time data for %s\n",
182 device_get_nameunit(dev));
183 sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf),
185 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
186 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
187 OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
188 cpufreq_curr_sysctl, "I", "Current CPU frequency");
189 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
190 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
191 OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
192 cpufreq_levels_sysctl, "A", "CPU frequency levels");
195 * Queue a one-shot broadcast that levels have changed.
196 * It will run once the system has completed booting.
198 TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
199 taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
204 /* Handle any work to be done for all drivers that attached during boot. */
206 cpufreq_startup_task(void *ctx, int pending)
209 cpufreq_settings_changed((device_t)ctx);
213 cpufreq_detach(device_t dev)
215 struct cpufreq_softc *sc;
216 struct cf_saved_freq *saved_freq;
219 CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
220 sc = device_get_softc(dev);
221 sysctl_ctx_free(&sc->sysctl_ctx);
223 while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
224 SLIST_REMOVE_HEAD(&sc->saved_freq, link);
225 free(saved_freq, M_TEMP);
228 /* Only clean up these resources when the last device is detaching. */
229 numdevs = devclass_get_count(cpufreq_dc);
231 CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
232 free(sc->levels_buf, M_DEVBUF);
239 cf_set_method(device_t dev, const struct cf_level *level, int priority)
241 struct cpufreq_softc *sc;
242 const struct cf_setting *set;
243 struct cf_saved_freq *saved_freq, *curr_freq;
247 sc = device_get_softc(dev);
252 /* We are going to change levels so notify the pre-change handler. */
253 EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
255 EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
259 CF_MTX_LOCK(&sc->lock);
262 #ifdef EARLY_AP_STARTUP
263 MPASS(mp_ncpus == 1 || smp_started);
266 * If still booting and secondary CPUs not started yet, don't allow
267 * changing the frequency until they're online. This is because we
268 * can't switch to them using sched_bind() and thus we'd only be
269 * switching the main CPU. XXXTODO: Need to think more about how to
270 * handle having different CPUs at different frequencies.
272 if (mp_ncpus > 1 && !smp_started) {
273 device_printf(dev, "rejecting change, SMP not started yet\n");
281 * If the requested level has a lower priority, don't allow
282 * the new level right now.
284 if (priority < sc->curr_priority) {
285 CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
292 * If the caller didn't specify a level and one is saved, prepare to
293 * restore the saved level. If none has been saved, return an error.
296 saved_freq = SLIST_FIRST(&sc->saved_freq);
297 if (saved_freq == NULL) {
298 CF_DEBUG("NULL level, no saved level\n");
302 level = &saved_freq->level;
303 priority = saved_freq->priority;
304 CF_DEBUG("restoring saved level, freq %d prio %d\n",
305 level->total_set.freq, priority);
308 /* Reject levels that are below our specified threshold. */
309 if (level->total_set.freq < cf_lowest_freq) {
310 CF_DEBUG("rejecting freq %d, less than %d limit\n",
311 level->total_set.freq, cf_lowest_freq);
316 /* If already at this level, just return. */
317 if (sc->curr_level.total_set.freq == level->total_set.freq) {
318 CF_DEBUG("skipping freq %d, same as current level %d\n",
319 level->total_set.freq, sc->curr_level.total_set.freq);
323 /* First, set the absolute frequency via its driver. */
324 set = &level->abs_set;
326 if (!device_is_attached(set->dev)) {
331 /* Bind to the target CPU before switching. */
332 pc = cpu_get_pcpu(set->dev);
333 thread_lock(curthread);
334 sched_bind(curthread, pc->pc_cpuid);
335 thread_unlock(curthread);
336 CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
337 device_get_nameunit(set->dev), PCPU_GET(cpuid));
338 error = CPUFREQ_DRV_SET(set->dev, set);
339 thread_lock(curthread);
340 sched_unbind(curthread);
341 thread_unlock(curthread);
347 /* Next, set any/all relative frequencies via their drivers. */
348 for (i = 0; i < level->rel_count; i++) {
349 set = &level->rel_set[i];
350 if (!device_is_attached(set->dev)) {
355 /* Bind to the target CPU before switching. */
356 pc = cpu_get_pcpu(set->dev);
357 thread_lock(curthread);
358 sched_bind(curthread, pc->pc_cpuid);
359 thread_unlock(curthread);
360 CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
361 device_get_nameunit(set->dev), PCPU_GET(cpuid));
362 error = CPUFREQ_DRV_SET(set->dev, set);
363 thread_lock(curthread);
364 sched_unbind(curthread);
365 thread_unlock(curthread);
367 /* XXX Back out any successful setting? */
374 * Before recording the current level, check if we're going to a
375 * higher priority. If so, save the previous level and priority.
377 if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
378 priority > sc->curr_priority) {
379 CF_DEBUG("saving level, freq %d prio %d\n",
380 sc->curr_level.total_set.freq, sc->curr_priority);
381 curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
382 if (curr_freq == NULL) {
386 curr_freq->level = sc->curr_level;
387 curr_freq->priority = sc->curr_priority;
388 SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
390 sc->curr_level = *level;
391 sc->curr_priority = priority;
393 /* If we were restoring a saved state, reset it to "unused". */
394 if (saved_freq != NULL) {
395 CF_DEBUG("resetting saved level\n");
396 sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
397 SLIST_REMOVE_HEAD(&sc->saved_freq, link);
398 free(saved_freq, M_TEMP);
402 CF_MTX_UNLOCK(&sc->lock);
405 * We changed levels (or attempted to) so notify the post-change
406 * handler of new frequency or error.
408 EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
410 device_printf(set->dev, "set freq failed, err %d\n", error);
416 cf_get_method(device_t dev, struct cf_level *level)
418 struct cpufreq_softc *sc;
419 struct cf_level *levels;
420 struct cf_setting *curr_set, set;
423 int bdiff, count, diff, error, i, n, numdevs;
426 sc = device_get_softc(dev);
430 /* If we already know the current frequency, we're done. */
431 CF_MTX_LOCK(&sc->lock);
432 curr_set = &sc->curr_level.total_set;
433 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
434 CF_DEBUG("get returning known freq %d\n", curr_set->freq);
437 CF_MTX_UNLOCK(&sc->lock);
440 * We need to figure out the current level. Loop through every
441 * driver, getting the current setting. Then, attempt to get a best
442 * match of settings against each level.
444 count = CF_MAX_LEVELS;
445 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
448 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
451 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
452 free(levels, M_TEMP);
455 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
457 free(levels, M_TEMP);
462 * Reacquire the lock and search for the given level.
464 * XXX Note: this is not quite right since we really need to go
465 * through each level and compare both absolute and relative
466 * settings for each driver in the system before making a match.
467 * The estimation code below catches this case though.
469 CF_MTX_LOCK(&sc->lock);
470 for (n = 0; n < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; n++) {
471 if (!device_is_attached(devs[n]))
473 if (CPUFREQ_DRV_GET(devs[n], &set) != 0)
475 for (i = 0; i < count; i++) {
476 if (set.freq == levels[i].total_set.freq) {
477 sc->curr_level = levels[i];
483 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
484 CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
489 * We couldn't find an exact match, so attempt to estimate and then
490 * match against a level.
492 pc = cpu_get_pcpu(dev);
497 cpu_est_clockrate(pc->pc_cpuid, &rate);
500 for (i = 0; i < count; i++) {
501 diff = abs(levels[i].total_set.freq - rate);
504 sc->curr_level = levels[i];
507 CF_DEBUG("get estimated freq %d\n", curr_set->freq);
511 *level = sc->curr_level;
513 CF_MTX_UNLOCK(&sc->lock);
515 free(levels, M_TEMP);
520 cf_levels_method(device_t dev, struct cf_level *levels, int *count)
522 struct cf_setting_array *set_arr;
523 struct cf_setting_lst rel_sets;
524 struct cpufreq_softc *sc;
525 struct cf_level *lev;
526 struct cf_setting *sets;
529 int error, i, numdevs, set_count, type;
532 if (levels == NULL || count == NULL)
535 TAILQ_INIT(&rel_sets);
536 sc = device_get_softc(dev);
537 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
540 sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
546 /* Get settings from all cpufreq drivers. */
547 CF_MTX_LOCK(&sc->lock);
548 for (i = 0; i < numdevs; i++) {
549 /* Skip devices that aren't ready. */
550 if (!device_is_attached(devs[i]))
554 * Get settings, skipping drivers that offer no settings or
555 * provide settings for informational purposes only.
557 error = CPUFREQ_DRV_TYPE(devs[i], &type);
558 if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
560 CF_DEBUG("skipping info-only driver %s\n",
561 device_get_nameunit(devs[i]));
565 set_count = MAX_SETTINGS;
566 error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
567 if (error || set_count == 0)
570 /* Add the settings to our absolute/relative lists. */
571 switch (type & CPUFREQ_TYPE_MASK) {
572 case CPUFREQ_TYPE_ABSOLUTE:
573 error = cpufreq_insert_abs(sc, sets, set_count);
575 case CPUFREQ_TYPE_RELATIVE:
576 CF_DEBUG("adding %d relative settings\n", set_count);
577 set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
578 if (set_arr == NULL) {
582 bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
583 set_arr->count = set_count;
584 TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
594 * If there are no absolute levels, create a fake one at 100%. We
595 * then cache the clockrate for later use as our base frequency.
597 if (TAILQ_EMPTY(&sc->all_levels)) {
598 if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
599 sc->max_mhz = cpu_get_nominal_mhz(dev);
601 * If the CPU can't report a rate for 100%, hope
602 * the CPU is running at its nominal rate right now,
603 * and use that instead.
605 if (sc->max_mhz <= 0) {
606 pc = cpu_get_pcpu(dev);
607 cpu_est_clockrate(pc->pc_cpuid, &rate);
608 sc->max_mhz = rate / 1000000;
611 memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
612 sets[0].freq = sc->max_mhz;
614 error = cpufreq_insert_abs(sc, sets, 1);
619 /* Create a combined list of absolute + relative levels. */
620 TAILQ_FOREACH(set_arr, &rel_sets, link)
621 cpufreq_expand_set(sc, set_arr);
623 /* If the caller doesn't have enough space, return the actual count. */
624 if (sc->all_count > *count) {
625 *count = sc->all_count;
630 /* Finally, output the list of levels. */
632 TAILQ_FOREACH(lev, &sc->all_levels, link) {
634 /* Skip levels that have a frequency that is too low. */
635 if (lev->total_set.freq < cf_lowest_freq) {
643 *count = sc->all_count;
647 /* Clear all levels since we regenerate them each time. */
648 while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
649 TAILQ_REMOVE(&sc->all_levels, lev, link);
654 CF_MTX_UNLOCK(&sc->lock);
655 while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
656 TAILQ_REMOVE(&rel_sets, set_arr, link);
657 free(set_arr, M_TEMP);
665 * Create levels for an array of absolute settings and insert them in
666 * sorted order in the specified list.
669 cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
672 struct cf_level_lst *list;
673 struct cf_level *level, *search;
676 CF_MTX_ASSERT(&sc->lock);
678 list = &sc->all_levels;
679 for (i = 0; i < count; i++) {
680 level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
683 level->abs_set = sets[i];
684 level->total_set = sets[i];
685 level->total_set.dev = NULL;
688 if (TAILQ_EMPTY(list)) {
689 CF_DEBUG("adding abs setting %d at head\n",
691 TAILQ_INSERT_HEAD(list, level, link);
695 TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
696 if (sets[i].freq <= search->total_set.freq) {
697 CF_DEBUG("adding abs setting %d after %d\n",
698 sets[i].freq, search->total_set.freq);
699 TAILQ_INSERT_AFTER(list, search, level, link);
708 * Expand a group of relative settings, creating derived levels from them.
711 cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
713 struct cf_level *fill, *search;
714 struct cf_setting *set;
717 CF_MTX_ASSERT(&sc->lock);
720 * Walk the set of all existing levels in reverse. This is so we
721 * create derived states from the lowest absolute settings first
722 * and discard duplicates created from higher absolute settings.
723 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
724 * preferable to 200 Mhz + 25% because absolute settings are more
725 * efficient since they often change the voltage as well.
727 TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
728 /* Add each setting to the level, duplicating if necessary. */
729 for (i = 0; i < set_arr->count; i++) {
730 set = &set_arr->sets[i];
733 * If this setting is less than 100%, split the level
734 * into two and add this setting to the new level.
737 if (set->freq < 10000) {
738 fill = cpufreq_dup_set(sc, search, set);
741 * The new level was a duplicate of an existing
742 * level or its absolute setting is too high
743 * so we freed it. For example, we discard a
744 * derived level of 1000 MHz/25% if a level
745 * of 500 MHz/100% already exists.
751 /* Add this setting to the existing or new level. */
752 KASSERT(fill->rel_count < MAX_SETTINGS,
753 ("cpufreq: too many relative drivers (%d)",
755 fill->rel_set[fill->rel_count] = *set;
758 "expand set added rel setting %d%% to %d level\n",
759 set->freq / 100, fill->total_set.freq);
766 static struct cf_level *
767 cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
768 struct cf_setting *set)
770 struct cf_level_lst *list;
771 struct cf_level *fill, *itr;
772 struct cf_setting *fill_set, *itr_set;
775 CF_MTX_ASSERT(&sc->lock);
778 * Create a new level, copy it from the old one, and update the
779 * total frequency and power by the percentage specified in the
782 fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
786 fill_set = &fill->total_set;
788 ((uint64_t)fill_set->freq * set->freq) / 10000;
789 if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
790 fill_set->power = ((uint64_t)fill_set->power * set->freq)
793 if (set->lat != CPUFREQ_VAL_UNKNOWN) {
794 if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
795 fill_set->lat += set->lat;
797 fill_set->lat = set->lat;
799 CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
802 * If we copied an old level that we already modified (say, at 100%),
803 * we need to remove that setting before adding this one. Since we
804 * process each setting array in order, we know any settings for this
805 * driver will be found at the end.
807 for (i = fill->rel_count; i != 0; i--) {
808 if (fill->rel_set[i - 1].dev != set->dev)
810 CF_DEBUG("removed last relative driver: %s\n",
811 device_get_nameunit(set->dev));
816 * Insert the new level in sorted order. If it is a duplicate of an
817 * existing level (1) or has an absolute setting higher than the
818 * existing level (2), do not add it. We can do this since any such
819 * level is guaranteed use less power. For example (1), a level with
820 * one absolute setting of 800 Mhz uses less power than one composed
821 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
822 * Also for example (2), a level of 800 Mhz/75% is preferable to
823 * 1600 Mhz/25% even though the latter has a lower total frequency.
825 list = &sc->all_levels;
826 KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
827 TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
828 itr_set = &itr->total_set;
829 if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
830 CF_DEBUG("dup set rejecting %d (dupe)\n",
834 } else if (fill_set->freq < itr_set->freq) {
835 if (fill->abs_set.freq <= itr->abs_set.freq) {
837 "dup done, inserting new level %d after %d\n",
838 fill_set->freq, itr_set->freq);
839 TAILQ_INSERT_AFTER(list, itr, fill, link);
842 CF_DEBUG("dup set rejecting %d (abs too big)\n",
850 /* We didn't find a good place for this new level so free it. */
852 CF_DEBUG("dup set freeing new level %d (not optimal)\n",
862 cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
864 struct cpufreq_softc *sc;
865 struct cf_level *levels;
866 int best, count, diff, bdiff, devcount, error, freq, i, n;
871 levels = sc->levels_buf;
873 error = CPUFREQ_GET(sc->dev, &levels[0]);
876 freq = levels[0].total_set.freq;
877 error = sysctl_handle_int(oidp, &freq, 0, req);
878 if (error != 0 || req->newptr == NULL)
882 * While we only call cpufreq_get() on one device (assuming all
883 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
884 * This is needed for some MP systems.
886 error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
889 for (n = 0; n < devcount; n++) {
890 count = CF_MAX_LEVELS;
891 error = CPUFREQ_LEVELS(devs[n], levels, &count);
895 "cpufreq: need to increase CF_MAX_LEVELS\n");
900 for (i = 0; i < count; i++) {
901 diff = abs(levels[i].total_set.freq - freq);
907 error = CPUFREQ_SET(devs[n], &levels[best], CPUFREQ_PRIO_USER);
917 cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
919 struct cpufreq_softc *sc;
920 struct cf_level *levels;
921 struct cf_setting *set;
926 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
928 /* Get settings from the device and generate the output string. */
929 count = CF_MAX_LEVELS;
930 levels = sc->levels_buf;
931 if (levels == NULL) {
935 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
938 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
942 for (i = 0; i < count; i++) {
943 set = &levels[i].total_set;
944 sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
950 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
958 cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
961 struct cf_setting *sets;
963 int error, i, set_count;
965 dev = oidp->oid_arg1;
966 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
968 /* Get settings from the device and generate the output string. */
969 set_count = MAX_SETTINGS;
970 sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
975 error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
979 for (i = 0; i < set_count; i++)
980 sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
985 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
994 cpufreq_register(device_t dev)
996 struct cpufreq_softc *sc;
997 device_t cf_dev, cpu_dev;
999 /* Add a sysctl to get each driver's settings separately. */
1000 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1001 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1002 OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
1003 cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
1006 * Add only one cpufreq device to each CPU. Currently, all CPUs
1007 * must offer the same levels and be switched at the same time.
1009 cpu_dev = device_get_parent(dev);
1010 if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
1011 sc = device_get_softc(cf_dev);
1012 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
1016 /* Add the child device and possibly sysctls. */
1017 cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
1020 device_quiet(cf_dev);
1022 return (device_probe_and_attach(cf_dev));
1026 cpufreq_unregister(device_t dev)
1028 device_t cf_dev, *devs;
1029 int cfcount, devcount, error, i, type;
1032 * If this is the last cpufreq child device, remove the control
1033 * device as well. We identify cpufreq children by calling a method
1036 error = device_get_children(device_get_parent(dev), &devs, &devcount);
1039 cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
1040 if (cf_dev == NULL) {
1042 "warning: cpufreq_unregister called with no cpufreq device active\n");
1047 for (i = 0; i < devcount; i++) {
1048 if (!device_is_attached(devs[i]))
1050 if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
1054 device_delete_child(device_get_parent(cf_dev), cf_dev);
1061 cpufreq_settings_changed(device_t dev)
1064 EVENTHANDLER_INVOKE(cpufreq_levels_changed,
1065 device_get_unit(device_get_parent(dev)));