2 * Copyright (c) 2004-2005 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>
40 #include <sys/sched.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
45 #include <sys/timetc.h>
47 #include "cpufreq_if.h"
50 * Common CPU frequency glue code. Drivers for specific hardware can
51 * attach this interface to allow users to get/set the CPU frequency.
55 * Number of levels we can handle. Levels are synthesized from settings
56 * so for M settings and N drivers, there may be M*N levels.
58 #define CF_MAX_LEVELS 64
60 struct cpufreq_softc {
62 struct cf_level curr_level;
64 struct cf_level saved_level;
66 struct cf_level_lst all_levels;
70 struct sysctl_ctx_list sysctl_ctx;
73 struct cf_setting_array {
74 struct cf_setting sets[MAX_SETTINGS];
76 TAILQ_ENTRY(cf_setting_array) link;
79 TAILQ_HEAD(cf_setting_lst, cf_setting_array);
81 #define CF_MTX_INIT(x) sx_init((x), "cpufreq lock")
82 #define CF_MTX_LOCK(x) sx_xlock((x))
83 #define CF_MTX_UNLOCK(x) sx_xunlock((x))
84 #define CF_MTX_ASSERT(x) sx_assert((x), SX_XLOCKED)
86 #define CF_DEBUG(msg...) do { \
88 printf("cpufreq: " msg); \
91 static int cpufreq_attach(device_t dev);
92 static int cpufreq_detach(device_t dev);
93 static void cpufreq_evaluate(void *arg);
94 static int cf_set_method(device_t dev, const struct cf_level *level,
96 static int cf_get_method(device_t dev, struct cf_level *level);
97 static int cf_levels_method(device_t dev, struct cf_level *levels,
99 static int cpufreq_insert_abs(struct cpufreq_softc *sc,
100 struct cf_setting *sets, int count);
101 static int cpufreq_expand_set(struct cpufreq_softc *sc,
102 struct cf_setting_array *set_arr);
103 static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
104 struct cf_level *dup, struct cf_setting *set);
105 static int cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
106 static int cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
107 static int cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
109 static device_method_t cpufreq_methods[] = {
110 DEVMETHOD(device_probe, bus_generic_probe),
111 DEVMETHOD(device_attach, cpufreq_attach),
112 DEVMETHOD(device_detach, cpufreq_detach),
114 DEVMETHOD(cpufreq_set, cf_set_method),
115 DEVMETHOD(cpufreq_get, cf_get_method),
116 DEVMETHOD(cpufreq_levels, cf_levels_method),
119 static driver_t cpufreq_driver = {
120 "cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
122 static devclass_t cpufreq_dc;
123 DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
125 static eventhandler_tag cf_ev_tag;
127 static int cf_lowest_freq;
128 static int cf_verbose;
129 TUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq);
130 TUNABLE_INT("debug.cpufreq.verbose", &cf_verbose);
131 SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, "cpufreq debugging");
132 SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1,
133 "Don't provide levels below this frequency.");
134 SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1,
135 "Print verbose debugging messages");
138 cpufreq_attach(device_t dev)
140 struct cpufreq_softc *sc;
144 CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
145 sc = device_get_softc(dev);
146 parent = device_get_parent(dev);
148 sysctl_ctx_init(&sc->sysctl_ctx);
149 TAILQ_INIT(&sc->all_levels);
150 CF_MTX_INIT(&sc->lock);
151 sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
152 sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
153 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
156 * Only initialize one set of sysctls for all CPUs. In the future,
157 * if multiple CPUs can have different settings, we can move these
158 * sysctls to be under every CPU instead of just the first one.
160 numdevs = devclass_get_count(cpufreq_dc);
164 CF_DEBUG("initializing one-time data for %s\n",
165 device_get_nameunit(dev));
166 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
167 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
168 OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
169 cpufreq_curr_sysctl, "I", "Current CPU frequency");
170 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
171 SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
172 OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
173 cpufreq_levels_sysctl, "A", "CPU frequency levels");
174 cf_ev_tag = EVENTHANDLER_REGISTER(cpufreq_changed, cpufreq_evaluate,
175 NULL, EVENTHANDLER_PRI_ANY);
181 cpufreq_detach(device_t dev)
183 struct cpufreq_softc *sc;
186 CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
187 sc = device_get_softc(dev);
188 sysctl_ctx_free(&sc->sysctl_ctx);
190 /* Only clean up these resources when the last device is detaching. */
191 numdevs = devclass_get_count(cpufreq_dc);
193 CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
194 EVENTHANDLER_DEREGISTER(cpufreq_changed, cf_ev_tag);
201 cpufreq_evaluate(void *arg)
203 /* TODO: Re-evaluate when notified of changes to drivers. */
207 cf_set_method(device_t dev, const struct cf_level *level, int priority)
209 struct cpufreq_softc *sc;
210 const struct cf_setting *set;
212 int cpu_id, error, i;
214 sc = device_get_softc(dev);
219 * Check that the TSC isn't being used as a timecounter.
220 * If it is, then return EBUSY and refuse to change the
223 if (strcmp(timecounter->tc_name, "TSC") == 0)
227 * If the caller didn't specify a level and one is saved, prepare to
228 * restore the saved level. If none has been saved, return an error.
229 * If they did specify one, but the requested level has a lower
230 * priority, don't allow the new level right now.
232 CF_MTX_LOCK(&sc->lock);
234 if (sc->saved_level.total_set.freq != CPUFREQ_VAL_UNKNOWN) {
235 level = &sc->saved_level;
236 priority = sc->saved_priority;
237 CF_DEBUG("restoring saved level, freq %d prio %d\n",
238 level->total_set.freq, priority);
240 CF_DEBUG("NULL level, no saved level\n");
244 } else if (priority < sc->curr_priority) {
245 CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
251 /* Reject levels that are below our specified threshold. */
252 if (level->total_set.freq <= cf_lowest_freq) {
253 CF_DEBUG("rejecting freq %d, less than %d limit\n",
254 level->total_set.freq, cf_lowest_freq);
259 /* If already at this level, just return. */
260 if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq)) {
261 CF_DEBUG("skipping freq %d, same as current level %d\n",
262 level->total_set.freq, sc->curr_level.total_set.freq);
266 /* First, set the absolute frequency via its driver. */
267 set = &level->abs_set;
269 if (!device_is_attached(set->dev)) {
274 /* Bind to the target CPU before switching, if necessary. */
275 cpu_id = PCPU_GET(cpuid);
276 pc = cpu_get_pcpu(set->dev);
277 if (cpu_id != pc->pc_cpuid) {
278 mtx_lock_spin(&sched_lock);
279 sched_bind(curthread, pc->pc_cpuid);
280 mtx_unlock_spin(&sched_lock);
282 CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
283 device_get_nameunit(set->dev), PCPU_GET(cpuid));
284 error = CPUFREQ_DRV_SET(set->dev, set);
285 if (cpu_id != pc->pc_cpuid) {
286 mtx_lock_spin(&sched_lock);
287 sched_unbind(curthread);
288 mtx_unlock_spin(&sched_lock);
295 /* Next, set any/all relative frequencies via their drivers. */
296 for (i = 0; i < level->rel_count; i++) {
297 set = &level->rel_set[i];
298 if (!device_is_attached(set->dev)) {
303 /* Bind to the target CPU before switching, if necessary. */
304 cpu_id = PCPU_GET(cpuid);
305 pc = cpu_get_pcpu(set->dev);
306 if (cpu_id != pc->pc_cpuid) {
307 mtx_lock_spin(&sched_lock);
308 sched_bind(curthread, pc->pc_cpuid);
309 mtx_unlock_spin(&sched_lock);
311 CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
312 device_get_nameunit(set->dev), PCPU_GET(cpuid));
313 error = CPUFREQ_DRV_SET(set->dev, set);
314 if (cpu_id != pc->pc_cpuid) {
315 mtx_lock_spin(&sched_lock);
316 sched_unbind(curthread);
317 mtx_unlock_spin(&sched_lock);
320 /* XXX Back out any successful setting? */
325 /* If we were restoring a saved state, reset it to "unused". */
326 if (level == &sc->saved_level) {
327 CF_DEBUG("resetting saved level\n");
328 sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
329 sc->saved_priority = 0;
333 * Before recording the current level, check if we're going to a
334 * higher priority and have not saved a level yet. If so, save the
335 * previous level and priority.
337 if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
338 sc->saved_level.total_set.freq == CPUFREQ_VAL_UNKNOWN &&
339 priority > sc->curr_priority) {
340 CF_DEBUG("saving level, freq %d prio %d\n",
341 sc->curr_level.total_set.freq, sc->curr_priority);
342 sc->saved_level = sc->curr_level;
343 sc->saved_priority = sc->curr_priority;
345 sc->curr_level = *level;
346 sc->curr_priority = priority;
350 CF_MTX_UNLOCK(&sc->lock);
352 device_printf(set->dev, "set freq failed, err %d\n", error);
357 cf_get_method(device_t dev, struct cf_level *level)
359 struct cpufreq_softc *sc;
360 struct cf_level *levels;
361 struct cf_setting *curr_set, set;
364 int count, error, i, numdevs;
367 sc = device_get_softc(dev);
371 /* If we already know the current frequency, we're done. */
372 CF_MTX_LOCK(&sc->lock);
373 curr_set = &sc->curr_level.total_set;
374 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
375 CF_DEBUG("get returning known freq %d\n", curr_set->freq);
378 CF_MTX_UNLOCK(&sc->lock);
381 * We need to figure out the current level. Loop through every
382 * driver, getting the current setting. Then, attempt to get a best
383 * match of settings against each level.
385 count = CF_MAX_LEVELS;
386 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
389 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
392 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
393 free(levels, M_TEMP);
396 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
398 free(levels, M_TEMP);
403 * Reacquire the lock and search for the given level.
405 * XXX Note: this is not quite right since we really need to go
406 * through each level and compare both absolute and relative
407 * settings for each driver in the system before making a match.
408 * The estimation code below catches this case though.
410 CF_MTX_LOCK(&sc->lock);
411 for (i = 0; i < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; i++) {
412 if (!device_is_attached(devs[i]))
414 error = CPUFREQ_DRV_GET(devs[i], &set);
417 for (i = 0; i < count; i++) {
418 if (CPUFREQ_CMP(set.freq, levels[i].total_set.freq)) {
419 sc->curr_level = levels[i];
425 if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
426 CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
431 * We couldn't find an exact match, so attempt to estimate and then
432 * match against a level.
434 pc = cpu_get_pcpu(dev);
439 cpu_est_clockrate(pc->pc_cpuid, &rate);
441 for (i = 0; i < count; i++) {
442 if (CPUFREQ_CMP(rate, levels[i].total_set.freq)) {
443 sc->curr_level = levels[i];
444 CF_DEBUG("get estimated freq %d\n", curr_set->freq);
451 *level = sc->curr_level;
453 CF_MTX_UNLOCK(&sc->lock);
455 free(levels, M_TEMP);
460 cf_levels_method(device_t dev, struct cf_level *levels, int *count)
462 struct cf_setting_array *set_arr;
463 struct cf_setting_lst rel_sets;
464 struct cpufreq_softc *sc;
465 struct cf_level *lev;
466 struct cf_setting *sets;
469 int error, i, numdevs, set_count, type;
472 if (levels == NULL || count == NULL)
475 TAILQ_INIT(&rel_sets);
476 sc = device_get_softc(dev);
477 error = device_get_children(device_get_parent(dev), &devs, &numdevs);
480 sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
486 /* Get settings from all cpufreq drivers. */
487 CF_MTX_LOCK(&sc->lock);
488 for (i = 0; i < numdevs; i++) {
489 /* Skip devices that aren't ready. */
490 if (!device_is_attached(devs[i]))
494 * Get settings, skipping drivers that offer no settings or
495 * provide settings for informational purposes only.
497 error = CPUFREQ_DRV_TYPE(devs[i], &type);
498 if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
500 CF_DEBUG("skipping info-only driver %s\n",
501 device_get_nameunit(devs[i]));
505 set_count = MAX_SETTINGS;
506 error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
507 if (error || set_count == 0)
510 /* Add the settings to our absolute/relative lists. */
511 switch (type & CPUFREQ_TYPE_MASK) {
512 case CPUFREQ_TYPE_ABSOLUTE:
513 error = cpufreq_insert_abs(sc, sets, set_count);
515 case CPUFREQ_TYPE_RELATIVE:
516 CF_DEBUG("adding %d relative settings\n", set_count);
517 set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
518 if (set_arr == NULL) {
522 bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
523 set_arr->count = set_count;
524 TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
534 * If there are no absolute levels, create a fake one at 100%. We
535 * then cache the clockrate for later use as our base frequency.
537 * XXX This assumes that the first time through, if we only have
538 * relative drivers, the CPU is currently running at 100%.
540 if (TAILQ_EMPTY(&sc->all_levels)) {
541 if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
542 pc = cpu_get_pcpu(dev);
543 cpu_est_clockrate(pc->pc_cpuid, &rate);
544 sc->max_mhz = rate / 1000000;
546 memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
547 sets[0].freq = sc->max_mhz;
549 error = cpufreq_insert_abs(sc, sets, 1);
554 /* Create a combined list of absolute + relative levels. */
555 TAILQ_FOREACH(set_arr, &rel_sets, link)
556 cpufreq_expand_set(sc, set_arr);
558 /* If the caller doesn't have enough space, return the actual count. */
559 if (sc->all_count > *count) {
560 *count = sc->all_count;
565 /* Finally, output the list of levels. */
567 TAILQ_FOREACH(lev, &sc->all_levels, link) {
568 /* Skip levels that have a frequency that is too low. */
569 if (lev->total_set.freq <= cf_lowest_freq) {
577 *count = sc->all_count;
581 /* Clear all levels since we regenerate them each time. */
582 while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
583 TAILQ_REMOVE(&sc->all_levels, lev, link);
588 CF_MTX_UNLOCK(&sc->lock);
589 while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
590 TAILQ_REMOVE(&rel_sets, set_arr, link);
591 free(set_arr, M_TEMP);
599 * Create levels for an array of absolute settings and insert them in
600 * sorted order in the specified list.
603 cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
606 struct cf_level_lst *list;
607 struct cf_level *level, *search;
610 CF_MTX_ASSERT(&sc->lock);
612 list = &sc->all_levels;
613 for (i = 0; i < count; i++) {
614 level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
617 level->abs_set = sets[i];
618 level->total_set = sets[i];
619 level->total_set.dev = NULL;
622 if (TAILQ_EMPTY(list)) {
623 CF_DEBUG("adding abs setting %d at head\n",
625 TAILQ_INSERT_HEAD(list, level, link);
629 TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
630 if (sets[i].freq <= search->total_set.freq) {
631 CF_DEBUG("adding abs setting %d after %d\n",
632 sets[i].freq, search->total_set.freq);
633 TAILQ_INSERT_AFTER(list, search, level, link);
642 * Expand a group of relative settings, creating derived levels from them.
645 cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
647 struct cf_level *fill, *search;
648 struct cf_setting *set;
651 CF_MTX_ASSERT(&sc->lock);
653 TAILQ_FOREACH(search, &sc->all_levels, link) {
654 /* Skip this level if we've already modified it. */
655 for (i = 0; i < search->rel_count; i++) {
656 if (search->rel_set[i].dev == set_arr->sets[0].dev)
659 if (i != search->rel_count) {
660 CF_DEBUG("skipping modified level, freq %d (dev %s)\n",
661 search->total_set.freq,
662 device_get_nameunit(search->rel_set[i].dev));
666 /* Add each setting to the level, duplicating if necessary. */
667 for (i = 0; i < set_arr->count; i++) {
668 set = &set_arr->sets[i];
671 * If this setting is less than 100%, split the level
672 * into two and add this setting to the new level.
675 if (set->freq < 10000)
676 fill = cpufreq_dup_set(sc, search, set);
679 * The new level was a duplicate of an existing level
680 * so we freed it. Go to the next setting.
685 /* Add this setting to the existing or new level. */
686 KASSERT(fill->rel_count < MAX_SETTINGS,
687 ("cpufreq: too many relative drivers (%d)",
689 fill->rel_set[fill->rel_count] = *set;
692 "expand set added rel setting %d%% to %d level\n",
693 set->freq / 100, fill->total_set.freq);
700 static struct cf_level *
701 cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
702 struct cf_setting *set)
704 struct cf_level_lst *list;
705 struct cf_level *fill, *itr;
706 struct cf_setting *fill_set, *itr_set;
709 CF_MTX_ASSERT(&sc->lock);
712 * Create a new level, copy it from the old one, and update the
713 * total frequency and power by the percentage specified in the
716 fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
720 fill_set = &fill->total_set;
722 ((uint64_t)fill_set->freq * set->freq) / 10000;
723 if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
724 fill_set->power = ((uint64_t)fill_set->power * set->freq)
727 if (set->lat != CPUFREQ_VAL_UNKNOWN) {
728 if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
729 fill_set->lat += set->lat;
731 fill_set->lat = set->lat;
733 CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
736 * If we copied an old level that we already modified (say, at 100%),
737 * we need to remove that setting before adding this one. Since we
738 * process each setting array in order, we know any settings for this
739 * driver will be found at the end.
741 for (i = fill->rel_count; i != 0; i--) {
742 if (fill->rel_set[i - 1].dev != set->dev)
744 CF_DEBUG("removed last relative driver: %s\n",
745 device_get_nameunit(set->dev));
750 * Insert the new level in sorted order. If we find a duplicate,
751 * free the new level. We can do this since any existing level will
752 * be guaranteed to have the same or less settings and thus consume
753 * less power. For example, a level with one absolute setting of
754 * 800 Mhz uses less power than one composed of an absolute setting
755 * of 1600 Mhz and a relative setting at 50%.
757 list = &sc->all_levels;
758 if (TAILQ_EMPTY(list)) {
759 CF_DEBUG("dup done, inserted %d at head\n", fill_set->freq);
760 TAILQ_INSERT_HEAD(list, fill, link);
762 TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
763 itr_set = &itr->total_set;
764 if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
766 "dup done, freeing new level %d, matches %d\n",
767 fill_set->freq, itr_set->freq);
771 } else if (fill_set->freq < itr_set->freq) {
773 "dup done, inserting new level %d after %d\n",
774 fill_set->freq, itr_set->freq);
775 TAILQ_INSERT_AFTER(list, itr, fill, link);
786 cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
788 struct cpufreq_softc *sc;
789 struct cf_level *levels;
790 int count, devcount, error, freq, i, n;
795 levels = malloc(CF_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
799 error = CPUFREQ_GET(sc->dev, &levels[0]);
802 freq = levels[0].total_set.freq;
803 error = sysctl_handle_int(oidp, &freq, 0, req);
804 if (error != 0 || req->newptr == NULL)
808 * While we only call cpufreq_get() on one device (assuming all
809 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
810 * This is needed for some MP systems.
812 error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
815 for (n = 0; n < devcount; n++) {
816 count = CF_MAX_LEVELS;
817 error = CPUFREQ_LEVELS(devs[n], levels, &count);
821 "cpufreq: need to increase CF_MAX_LEVELS\n");
824 for (i = 0; i < count; i++) {
825 if (CPUFREQ_CMP(levels[i].total_set.freq, freq)) {
826 error = CPUFREQ_SET(devs[n], &levels[i],
841 free(levels, M_TEMP);
846 cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
848 struct cpufreq_softc *sc;
849 struct cf_level *levels;
850 struct cf_setting *set;
855 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
857 /* Get settings from the device and generate the output string. */
858 count = CF_MAX_LEVELS;
859 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
862 error = CPUFREQ_LEVELS(sc->dev, levels, &count);
865 printf("cpufreq: need to increase CF_MAX_LEVELS\n");
869 for (i = 0; i < count; i++) {
870 set = &levels[i].total_set;
871 sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
877 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
880 free(levels, M_TEMP);
886 cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
889 struct cf_setting *sets;
891 int error, i, set_count;
893 dev = oidp->oid_arg1;
894 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
896 /* Get settings from the device and generate the output string. */
897 set_count = MAX_SETTINGS;
898 sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
901 error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
905 for (i = 0; i < set_count; i++)
906 sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
911 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
920 cpufreq_register(device_t dev)
922 struct cpufreq_softc *sc;
923 device_t cf_dev, cpu_dev;
925 /* Add a sysctl to get each driver's settings separately. */
926 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
927 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
928 OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
929 cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
932 * Add only one cpufreq device to each CPU. Currently, all CPUs
933 * must offer the same levels and be switched at the same time.
935 cpu_dev = device_get_parent(dev);
936 if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
937 sc = device_get_softc(cf_dev);
938 sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
942 /* Add the child device and possibly sysctls. */
943 cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
946 device_quiet(cf_dev);
948 return (device_probe_and_attach(cf_dev));
952 cpufreq_unregister(device_t dev)
954 device_t cf_dev, *devs;
955 int cfcount, devcount, error, i, type;
958 * If this is the last cpufreq child device, remove the control
959 * device as well. We identify cpufreq children by calling a method
962 error = device_get_children(device_get_parent(dev), &devs, &devcount);
965 cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
966 if (cf_dev == NULL) {
968 "warning: cpufreq_unregister called with no cpufreq device active\n");
972 for (i = 0; i < devcount; i++) {
973 if (!device_is_attached(devs[i]))
975 if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
979 device_delete_child(device_get_parent(cf_dev), cf_dev);