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 TUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq);
137 TUNABLE_INT("debug.cpufreq.verbose", &cf_verbose);
138 SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, "cpufreq debugging");
139 SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1,
140 "Don't provide levels below this frequency.");
141 SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1,
142 "Print verbose debugging messages");
145 cpufreq_attach(device_t dev)
147 struct cpufreq_softc *sc;
153 CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
154 sc = device_get_softc(dev);
155 parent = device_get_parent(dev);
157 sysctl_ctx_init(&sc->sysctl_ctx);
158 TAILQ_INIT(&sc->all_levels);
159 CF_MTX_INIT(&sc->lock);
160 sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
161 SLIST_INIT(&sc->saved_freq);
162 /* Try to get nominal CPU freq to use it as maximum later if needed */
163 sc->max_mhz = cpu_get_nominal_mhz(dev);
164 /* If that fails, try to measure the current rate */
165 if (sc->max_mhz <= 0) {
166 pc = cpu_get_pcpu(dev);
167 if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
168 sc->max_mhz = rate / 1000000;
170 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
174 * Only initialize one set of sysctls for all CPUs. In the future,
175 * if multiple CPUs can have different settings, we can move these
176 * sysctls to be under every CPU instead of just the first one.
178 numdevs = devclass_get_count(cpufreq_dc);
182 CF_DEBUG("initializing one-time data for %s\n",
183 device_get_nameunit(dev));
184 sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf),
186 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
187 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
188 OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
189 cpufreq_curr_sysctl, "I", "Current CPU frequency");
190 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
191 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
192 OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
193 cpufreq_levels_sysctl, "A", "CPU frequency levels");
196 * Queue a one-shot broadcast that levels have changed.
197 * It will run once the system has completed booting.
199 TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
200 taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
205 /* Handle any work to be done for all drivers that attached during boot. */
207 cpufreq_startup_task(void *ctx, int pending)
210 cpufreq_settings_changed((device_t)ctx);
214 cpufreq_detach(device_t dev)
216 struct cpufreq_softc *sc;
217 struct cf_saved_freq *saved_freq;
220 CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
221 sc = device_get_softc(dev);
222 sysctl_ctx_free(&sc->sysctl_ctx);
224 while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
225 SLIST_REMOVE_HEAD(&sc->saved_freq, link);
226 free(saved_freq, M_TEMP);
229 /* Only clean up these resources when the last device is detaching. */
230 numdevs = devclass_get_count(cpufreq_dc);
232 CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
233 free(sc->levels_buf, M_DEVBUF);
240 cf_set_method(device_t dev, const struct cf_level *level, int priority)
242 struct cpufreq_softc *sc;
243 const struct cf_setting *set;
244 struct cf_saved_freq *saved_freq, *curr_freq;
248 sc = device_get_softc(dev);
253 /* We are going to change levels so notify the pre-change handler. */
254 EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
256 EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
260 CF_MTX_LOCK(&sc->lock);
264 * If still booting and secondary CPUs not started yet, don't allow
265 * changing the frequency until they're online. This is because we
266 * can't switch to them using sched_bind() and thus we'd only be
267 * switching the main CPU. XXXTODO: Need to think more about how to
268 * handle having different CPUs at different frequencies.
270 if (mp_ncpus > 1 && !smp_active) {
271 device_printf(dev, "rejecting change, SMP not started yet\n");
278 * If the requested level has a lower priority, don't allow
279 * the new level right now.
281 if (priority < sc->curr_priority) {
282 CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
289 * If the caller didn't specify a level and one is saved, prepare to
290 * restore the saved level. If none has been saved, return an error.
293 saved_freq = SLIST_FIRST(&sc->saved_freq);
294 if (saved_freq == NULL) {
295 CF_DEBUG("NULL level, no saved level\n");
299 level = &saved_freq->level;
300 priority = saved_freq->priority;
301 CF_DEBUG("restoring saved level, freq %d prio %d\n",
302 level->total_set.freq, priority);
305 /* Reject levels that are below our specified threshold. */
306 if (level->total_set.freq < cf_lowest_freq) {
307 CF_DEBUG("rejecting freq %d, less than %d limit\n",
308 level->total_set.freq, cf_lowest_freq);
313 /* If already at this level, just return. */
314 if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq)) {
315 CF_DEBUG("skipping freq %d, same as current level %d\n",
316 level->total_set.freq, sc->curr_level.total_set.freq);
320 /* First, set the absolute frequency via its driver. */
321 set = &level->abs_set;
323 if (!device_is_attached(set->dev)) {
328 /* Bind to the target CPU before switching. */
329 pc = cpu_get_pcpu(set->dev);
330 thread_lock(curthread);
331 sched_bind(curthread, pc->pc_cpuid);
332 thread_unlock(curthread);
333 CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
334 device_get_nameunit(set->dev), PCPU_GET(cpuid));
335 error = CPUFREQ_DRV_SET(set->dev, set);
336 thread_lock(curthread);
337 sched_unbind(curthread);
338 thread_unlock(curthread);
344 /* Next, set any/all relative frequencies via their drivers. */
345 for (i = 0; i < level->rel_count; i++) {
346 set = &level->rel_set[i];
347 if (!device_is_attached(set->dev)) {
352 /* Bind to the target CPU before switching. */
353 pc = cpu_get_pcpu(set->dev);
354 thread_lock(curthread);
355 sched_bind(curthread, pc->pc_cpuid);
356 thread_unlock(curthread);
357 CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
358 device_get_nameunit(set->dev), PCPU_GET(cpuid));
359 error = CPUFREQ_DRV_SET(set->dev, set);
360 thread_lock(curthread);
361 sched_unbind(curthread);
362 thread_unlock(curthread);
364 /* XXX Back out any successful setting? */
371 * Before recording the current level, check if we're going to a
372 * higher priority. If so, save the previous level and priority.
374 if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
375 priority > sc->curr_priority) {
376 CF_DEBUG("saving level, freq %d prio %d\n",
377 sc->curr_level.total_set.freq, sc->curr_priority);
378 curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
379 if (curr_freq == NULL) {
383 curr_freq->level = sc->curr_level;
384 curr_freq->priority = sc->curr_priority;
385 SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
387 sc->curr_level = *level;
388 sc->curr_priority = priority;
390 /* If we were restoring a saved state, reset it to "unused". */
391 if (saved_freq != NULL) {
392 CF_DEBUG("resetting saved level\n");
393 sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
394 SLIST_REMOVE_HEAD(&sc->saved_freq, link);
395 free(saved_freq, M_TEMP);
399 CF_MTX_UNLOCK(&sc->lock);
402 * We changed levels (or attempted to) so notify the post-change
403 * handler of new frequency or error.
405 EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
407 device_printf(set->dev, "set freq failed, err %d\n", error);
413 cf_get_method(device_t dev, struct cf_level *level)
415 struct cpufreq_softc *sc;
416 struct cf_level *levels;
417 struct cf_setting *curr_set, set;
420 int count, error, i, n, numdevs;
423 sc = device_get_softc(dev);
427 /* If we already know the current frequency, we're done. */
428 CF_MTX_LOCK(&sc->lock);
429 curr_set = &sc->curr_level.total_set;
430 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
431 CF_DEBUG("get returning known freq %d\n", curr_set->freq);
434 CF_MTX_UNLOCK(&sc->lock);
437 * We need to figure out the current level. Loop through every
438 * driver, getting the current setting. Then, attempt to get a best
439 * match of settings against each level.
441 count = CF_MAX_LEVELS;
442 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
445 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
448 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
449 free(levels, M_TEMP);
452 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
454 free(levels, M_TEMP);
459 * Reacquire the lock and search for the given level.
461 * XXX Note: this is not quite right since we really need to go
462 * through each level and compare both absolute and relative
463 * settings for each driver in the system before making a match.
464 * The estimation code below catches this case though.
466 CF_MTX_LOCK(&sc->lock);
467 for (n = 0; n < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; n++) {
468 if (!device_is_attached(devs[n]))
470 if (CPUFREQ_DRV_GET(devs[n], &set) != 0)
472 for (i = 0; i < count; i++) {
473 if (CPUFREQ_CMP(set.freq, levels[i].total_set.freq)) {
474 sc->curr_level = levels[i];
480 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
481 CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
486 * We couldn't find an exact match, so attempt to estimate and then
487 * match against a level.
489 pc = cpu_get_pcpu(dev);
494 cpu_est_clockrate(pc->pc_cpuid, &rate);
496 for (i = 0; i < count; i++) {
497 if (CPUFREQ_CMP(rate, levels[i].total_set.freq)) {
498 sc->curr_level = levels[i];
499 CF_DEBUG("get estimated freq %d\n", curr_set->freq);
507 *level = sc->curr_level;
509 CF_MTX_UNLOCK(&sc->lock);
511 free(levels, M_TEMP);
516 cf_levels_method(device_t dev, struct cf_level *levels, int *count)
518 struct cf_setting_array *set_arr;
519 struct cf_setting_lst rel_sets;
520 struct cpufreq_softc *sc;
521 struct cf_level *lev;
522 struct cf_setting *sets;
525 int error, i, numdevs, set_count, type;
528 if (levels == NULL || count == NULL)
531 TAILQ_INIT(&rel_sets);
532 sc = device_get_softc(dev);
533 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
536 sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
542 /* Get settings from all cpufreq drivers. */
543 CF_MTX_LOCK(&sc->lock);
544 for (i = 0; i < numdevs; i++) {
545 /* Skip devices that aren't ready. */
546 if (!device_is_attached(devs[i]))
550 * Get settings, skipping drivers that offer no settings or
551 * provide settings for informational purposes only.
553 error = CPUFREQ_DRV_TYPE(devs[i], &type);
554 if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
556 CF_DEBUG("skipping info-only driver %s\n",
557 device_get_nameunit(devs[i]));
561 set_count = MAX_SETTINGS;
562 error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
563 if (error || set_count == 0)
566 /* Add the settings to our absolute/relative lists. */
567 switch (type & CPUFREQ_TYPE_MASK) {
568 case CPUFREQ_TYPE_ABSOLUTE:
569 error = cpufreq_insert_abs(sc, sets, set_count);
571 case CPUFREQ_TYPE_RELATIVE:
572 CF_DEBUG("adding %d relative settings\n", set_count);
573 set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
574 if (set_arr == NULL) {
578 bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
579 set_arr->count = set_count;
580 TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
590 * If there are no absolute levels, create a fake one at 100%. We
591 * then cache the clockrate for later use as our base frequency.
593 if (TAILQ_EMPTY(&sc->all_levels)) {
594 if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
595 sc->max_mhz = cpu_get_nominal_mhz(dev);
597 * If the CPU can't report a rate for 100%, hope
598 * the CPU is running at its nominal rate right now,
599 * and use that instead.
601 if (sc->max_mhz <= 0) {
602 pc = cpu_get_pcpu(dev);
603 cpu_est_clockrate(pc->pc_cpuid, &rate);
604 sc->max_mhz = rate / 1000000;
607 memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
608 sets[0].freq = sc->max_mhz;
610 error = cpufreq_insert_abs(sc, sets, 1);
615 /* Create a combined list of absolute + relative levels. */
616 TAILQ_FOREACH(set_arr, &rel_sets, link)
617 cpufreq_expand_set(sc, set_arr);
619 /* If the caller doesn't have enough space, return the actual count. */
620 if (sc->all_count > *count) {
621 *count = sc->all_count;
626 /* Finally, output the list of levels. */
628 TAILQ_FOREACH(lev, &sc->all_levels, link) {
630 * Skip levels that are too close in frequency to the
631 * previous levels. Some systems report bogus duplicate
632 * settings (i.e., for acpi_perf).
634 if (i > 0 && CPUFREQ_CMP(lev->total_set.freq,
635 levels[i - 1].total_set.freq)) {
640 /* Skip levels that have a frequency that is too low. */
641 if (lev->total_set.freq < cf_lowest_freq) {
649 *count = sc->all_count;
653 /* Clear all levels since we regenerate them each time. */
654 while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
655 TAILQ_REMOVE(&sc->all_levels, lev, link);
660 CF_MTX_UNLOCK(&sc->lock);
661 while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
662 TAILQ_REMOVE(&rel_sets, set_arr, link);
663 free(set_arr, M_TEMP);
671 * Create levels for an array of absolute settings and insert them in
672 * sorted order in the specified list.
675 cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
678 struct cf_level_lst *list;
679 struct cf_level *level, *search;
682 CF_MTX_ASSERT(&sc->lock);
684 list = &sc->all_levels;
685 for (i = 0; i < count; i++) {
686 level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
689 level->abs_set = sets[i];
690 level->total_set = sets[i];
691 level->total_set.dev = NULL;
694 if (TAILQ_EMPTY(list)) {
695 CF_DEBUG("adding abs setting %d at head\n",
697 TAILQ_INSERT_HEAD(list, level, link);
701 TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
702 if (sets[i].freq <= search->total_set.freq) {
703 CF_DEBUG("adding abs setting %d after %d\n",
704 sets[i].freq, search->total_set.freq);
705 TAILQ_INSERT_AFTER(list, search, level, link);
714 * Expand a group of relative settings, creating derived levels from them.
717 cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
719 struct cf_level *fill, *search;
720 struct cf_setting *set;
723 CF_MTX_ASSERT(&sc->lock);
726 * Walk the set of all existing levels in reverse. This is so we
727 * create derived states from the lowest absolute settings first
728 * and discard duplicates created from higher absolute settings.
729 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
730 * preferable to 200 Mhz + 25% because absolute settings are more
731 * efficient since they often change the voltage as well.
733 TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
734 /* Add each setting to the level, duplicating if necessary. */
735 for (i = 0; i < set_arr->count; i++) {
736 set = &set_arr->sets[i];
739 * If this setting is less than 100%, split the level
740 * into two and add this setting to the new level.
743 if (set->freq < 10000) {
744 fill = cpufreq_dup_set(sc, search, set);
747 * The new level was a duplicate of an existing
748 * level or its absolute setting is too high
749 * so we freed it. For example, we discard a
750 * derived level of 1000 MHz/25% if a level
751 * of 500 MHz/100% already exists.
757 /* Add this setting to the existing or new level. */
758 KASSERT(fill->rel_count < MAX_SETTINGS,
759 ("cpufreq: too many relative drivers (%d)",
761 fill->rel_set[fill->rel_count] = *set;
764 "expand set added rel setting %d%% to %d level\n",
765 set->freq / 100, fill->total_set.freq);
772 static struct cf_level *
773 cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
774 struct cf_setting *set)
776 struct cf_level_lst *list;
777 struct cf_level *fill, *itr;
778 struct cf_setting *fill_set, *itr_set;
781 CF_MTX_ASSERT(&sc->lock);
784 * Create a new level, copy it from the old one, and update the
785 * total frequency and power by the percentage specified in the
788 fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
792 fill_set = &fill->total_set;
794 ((uint64_t)fill_set->freq * set->freq) / 10000;
795 if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
796 fill_set->power = ((uint64_t)fill_set->power * set->freq)
799 if (set->lat != CPUFREQ_VAL_UNKNOWN) {
800 if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
801 fill_set->lat += set->lat;
803 fill_set->lat = set->lat;
805 CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
808 * If we copied an old level that we already modified (say, at 100%),
809 * we need to remove that setting before adding this one. Since we
810 * process each setting array in order, we know any settings for this
811 * driver will be found at the end.
813 for (i = fill->rel_count; i != 0; i--) {
814 if (fill->rel_set[i - 1].dev != set->dev)
816 CF_DEBUG("removed last relative driver: %s\n",
817 device_get_nameunit(set->dev));
822 * Insert the new level in sorted order. If it is a duplicate of an
823 * existing level (1) or has an absolute setting higher than the
824 * existing level (2), do not add it. We can do this since any such
825 * level is guaranteed use less power. For example (1), a level with
826 * one absolute setting of 800 Mhz uses less power than one composed
827 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
828 * Also for example (2), a level of 800 Mhz/75% is preferable to
829 * 1600 Mhz/25% even though the latter has a lower total frequency.
831 list = &sc->all_levels;
832 KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
833 TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
834 itr_set = &itr->total_set;
835 if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
836 CF_DEBUG("dup set rejecting %d (dupe)\n",
840 } else if (fill_set->freq < itr_set->freq) {
841 if (fill->abs_set.freq <= itr->abs_set.freq) {
843 "dup done, inserting new level %d after %d\n",
844 fill_set->freq, itr_set->freq);
845 TAILQ_INSERT_AFTER(list, itr, fill, link);
848 CF_DEBUG("dup set rejecting %d (abs too big)\n",
856 /* We didn't find a good place for this new level so free it. */
858 CF_DEBUG("dup set freeing new level %d (not optimal)\n",
868 cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
870 struct cpufreq_softc *sc;
871 struct cf_level *levels;
872 int count, devcount, error, freq, i, n;
877 levels = sc->levels_buf;
879 error = CPUFREQ_GET(sc->dev, &levels[0]);
882 freq = levels[0].total_set.freq;
883 error = sysctl_handle_int(oidp, &freq, 0, req);
884 if (error != 0 || req->newptr == NULL)
888 * While we only call cpufreq_get() on one device (assuming all
889 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
890 * This is needed for some MP systems.
892 error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
895 for (n = 0; n < devcount; n++) {
896 count = CF_MAX_LEVELS;
897 error = CPUFREQ_LEVELS(devs[n], levels, &count);
901 "cpufreq: need to increase CF_MAX_LEVELS\n");
904 for (i = 0; i < count; i++) {
905 if (CPUFREQ_CMP(levels[i].total_set.freq, freq)) {
906 error = CPUFREQ_SET(devs[n], &levels[i],
924 cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
926 struct cpufreq_softc *sc;
927 struct cf_level *levels;
928 struct cf_setting *set;
933 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
935 /* Get settings from the device and generate the output string. */
936 count = CF_MAX_LEVELS;
937 levels = sc->levels_buf;
938 if (levels == NULL) {
942 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
945 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
949 for (i = 0; i < count; i++) {
950 set = &levels[i].total_set;
951 sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
957 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
965 cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
968 struct cf_setting *sets;
970 int error, i, set_count;
972 dev = oidp->oid_arg1;
973 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
975 /* Get settings from the device and generate the output string. */
976 set_count = MAX_SETTINGS;
977 sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
982 error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
986 for (i = 0; i < set_count; i++)
987 sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
992 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1001 cpufreq_register(device_t dev)
1003 struct cpufreq_softc *sc;
1004 device_t cf_dev, cpu_dev;
1006 /* Add a sysctl to get each driver's settings separately. */
1007 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1008 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1009 OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
1010 cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
1013 * Add only one cpufreq device to each CPU. Currently, all CPUs
1014 * must offer the same levels and be switched at the same time.
1016 cpu_dev = device_get_parent(dev);
1017 if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
1018 sc = device_get_softc(cf_dev);
1019 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
1023 /* Add the child device and possibly sysctls. */
1024 cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
1027 device_quiet(cf_dev);
1029 return (device_probe_and_attach(cf_dev));
1033 cpufreq_unregister(device_t dev)
1035 device_t cf_dev, *devs;
1036 int cfcount, devcount, error, i, type;
1039 * If this is the last cpufreq child device, remove the control
1040 * device as well. We identify cpufreq children by calling a method
1043 error = device_get_children(device_get_parent(dev), &devs, &devcount);
1046 cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
1047 if (cf_dev == NULL) {
1049 "warning: cpufreq_unregister called with no cpufreq device active\n");
1053 for (i = 0; i < devcount; i++) {
1054 if (!device_is_attached(devs[i]))
1056 if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
1060 device_delete_child(device_get_parent(cf_dev), cf_dev);
1067 cpufreq_settings_changed(device_t dev)
1070 EVENTHANDLER_INVOKE(cpufreq_levels_changed,
1071 device_get_unit(device_get_parent(dev)));