1141240Snjl/*- 2167905Snjl * Copyright (c) 2004-2007 Nate Lawson (SDG) 3141240Snjl * All rights reserved. 4141240Snjl * 5141240Snjl * Redistribution and use in source and binary forms, with or without 6141240Snjl * modification, are permitted provided that the following conditions 7141240Snjl * are met: 8141240Snjl * 1. Redistributions of source code must retain the above copyright 9141240Snjl * notice, this list of conditions and the following disclaimer. 10141240Snjl * 2. Redistributions in binary form must reproduce the above copyright 11141240Snjl * notice, this list of conditions and the following disclaimer in the 12141240Snjl * documentation and/or other materials provided with the distribution. 13141240Snjl * 14141240Snjl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15141240Snjl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16141240Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17141240Snjl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18141240Snjl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19141240Snjl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20141240Snjl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21141240Snjl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22141240Snjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23141240Snjl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24141240Snjl * SUCH DAMAGE. 25141240Snjl */ 26141240Snjl 27141240Snjl#include <sys/cdefs.h> 28141240Snjl__FBSDID("$FreeBSD: releng/10.3/sys/kern/kern_cpu.c 266165 2014-05-15 18:07:35Z cperciva $"); 29141240Snjl 30141240Snjl#include <sys/param.h> 31141240Snjl#include <sys/bus.h> 32141240Snjl#include <sys/cpu.h> 33141240Snjl#include <sys/eventhandler.h> 34141240Snjl#include <sys/kernel.h> 35142603Snjl#include <sys/lock.h> 36141240Snjl#include <sys/malloc.h> 37141240Snjl#include <sys/module.h> 38141240Snjl#include <sys/proc.h> 39141240Snjl#include <sys/queue.h> 40173204Snjl#include <sys/sbuf.h> 41141240Snjl#include <sys/sched.h> 42173204Snjl#include <sys/smp.h> 43141240Snjl#include <sys/sysctl.h> 44141240Snjl#include <sys/systm.h> 45142603Snjl#include <sys/sx.h> 46141814Snjl#include <sys/timetc.h> 47167905Snjl#include <sys/taskqueue.h> 48141240Snjl 49141240Snjl#include "cpufreq_if.h" 50141240Snjl 51141240Snjl/* 52141240Snjl * Common CPU frequency glue code. Drivers for specific hardware can 53141240Snjl * attach this interface to allow users to get/set the CPU frequency. 54141240Snjl */ 55141240Snjl 56141240Snjl/* 57141240Snjl * Number of levels we can handle. Levels are synthesized from settings 58142395Snjl * so for M settings and N drivers, there may be M*N levels. 59141240Snjl */ 60142395Snjl#define CF_MAX_LEVELS 64 61141240Snjl 62150847Sumestruct cf_saved_freq { 63150847Sume struct cf_level level; 64150847Sume int priority; 65150847Sume SLIST_ENTRY(cf_saved_freq) link; 66150847Sume}; 67150847Sume 68141240Snjlstruct cpufreq_softc { 69142603Snjl struct sx lock; 70141240Snjl struct cf_level curr_level; 71141923Snjl int curr_priority; 72150847Sume SLIST_HEAD(, cf_saved_freq) saved_freq; 73141923Snjl struct cf_level_lst all_levels; 74141413Snjl int all_count; 75141945Snjl int max_mhz; 76141240Snjl device_t dev; 77141240Snjl struct sysctl_ctx_list sysctl_ctx; 78167905Snjl struct task startup_task; 79210422Savg struct cf_level *levels_buf; 80141240Snjl}; 81141240Snjl 82141240Snjlstruct cf_setting_array { 83141240Snjl struct cf_setting sets[MAX_SETTINGS]; 84141240Snjl int count; 85141240Snjl TAILQ_ENTRY(cf_setting_array) link; 86141240Snjl}; 87141240Snjl 88141240SnjlTAILQ_HEAD(cf_setting_lst, cf_setting_array); 89141240Snjl 90142603Snjl#define CF_MTX_INIT(x) sx_init((x), "cpufreq lock") 91142603Snjl#define CF_MTX_LOCK(x) sx_xlock((x)) 92142603Snjl#define CF_MTX_UNLOCK(x) sx_xunlock((x)) 93142603Snjl#define CF_MTX_ASSERT(x) sx_assert((x), SX_XLOCKED) 94142603Snjl 95144876Snjl#define CF_DEBUG(msg...) do { \ 96144876Snjl if (cf_verbose) \ 97144876Snjl printf("cpufreq: " msg); \ 98144876Snjl } while (0) 99144876Snjl 100141240Snjlstatic int cpufreq_attach(device_t dev); 101167905Snjlstatic void cpufreq_startup_task(void *ctx, int pending); 102141240Snjlstatic int cpufreq_detach(device_t dev); 103141240Snjlstatic int cf_set_method(device_t dev, const struct cf_level *level, 104141240Snjl int priority); 105141240Snjlstatic int cf_get_method(device_t dev, struct cf_level *level); 106141240Snjlstatic int cf_levels_method(device_t dev, struct cf_level *levels, 107141240Snjl int *count); 108141413Snjlstatic int cpufreq_insert_abs(struct cpufreq_softc *sc, 109141240Snjl struct cf_setting *sets, int count); 110141413Snjlstatic int cpufreq_expand_set(struct cpufreq_softc *sc, 111141413Snjl struct cf_setting_array *set_arr); 112141413Snjlstatic struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc, 113141413Snjl struct cf_level *dup, struct cf_setting *set); 114141240Snjlstatic int cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS); 115141240Snjlstatic int cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS); 116142114Snjlstatic int cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS); 117141240Snjl 118141240Snjlstatic device_method_t cpufreq_methods[] = { 119141240Snjl DEVMETHOD(device_probe, bus_generic_probe), 120141240Snjl DEVMETHOD(device_attach, cpufreq_attach), 121141240Snjl DEVMETHOD(device_detach, cpufreq_detach), 122141240Snjl 123141240Snjl DEVMETHOD(cpufreq_set, cf_set_method), 124141240Snjl DEVMETHOD(cpufreq_get, cf_get_method), 125141240Snjl DEVMETHOD(cpufreq_levels, cf_levels_method), 126141240Snjl {0, 0} 127141240Snjl}; 128141240Snjlstatic driver_t cpufreq_driver = { 129141240Snjl "cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc) 130141240Snjl}; 131141240Snjlstatic devclass_t cpufreq_dc; 132141240SnjlDRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0); 133141240Snjl 134142590Snjlstatic int cf_lowest_freq; 135144876Snjlstatic int cf_verbose; 136142590SnjlTUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq); 137144876SnjlTUNABLE_INT("debug.cpufreq.verbose", &cf_verbose); 138227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, 139227309Sed "cpufreq debugging"); 140142590SnjlSYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1, 141142590Snjl "Don't provide levels below this frequency."); 142144876SnjlSYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1, 143144876Snjl "Print verbose debugging messages"); 144142590Snjl 145141240Snjlstatic int 146141240Snjlcpufreq_attach(device_t dev) 147141240Snjl{ 148141240Snjl struct cpufreq_softc *sc; 149186154Smav struct pcpu *pc; 150141240Snjl device_t parent; 151186154Smav uint64_t rate; 152141240Snjl int numdevs; 153141240Snjl 154144876Snjl CF_DEBUG("initializing %s\n", device_get_nameunit(dev)); 155141240Snjl sc = device_get_softc(dev); 156141240Snjl parent = device_get_parent(dev); 157141240Snjl sc->dev = dev; 158141240Snjl sysctl_ctx_init(&sc->sysctl_ctx); 159141240Snjl TAILQ_INIT(&sc->all_levels); 160142603Snjl CF_MTX_INIT(&sc->lock); 161141240Snjl sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN; 162150847Sume SLIST_INIT(&sc->saved_freq); 163193155Snwhitehorn /* Try to get nominal CPU freq to use it as maximum later if needed */ 164193155Snwhitehorn sc->max_mhz = cpu_get_nominal_mhz(dev); 165193155Snwhitehorn /* If that fails, try to measure the current rate */ 166193155Snwhitehorn if (sc->max_mhz <= 0) { 167193155Snwhitehorn pc = cpu_get_pcpu(dev); 168193155Snwhitehorn if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0) 169193155Snwhitehorn sc->max_mhz = rate / 1000000; 170193155Snwhitehorn else 171193155Snwhitehorn sc->max_mhz = CPUFREQ_VAL_UNKNOWN; 172193155Snwhitehorn } 173141240Snjl 174141240Snjl /* 175141240Snjl * Only initialize one set of sysctls for all CPUs. In the future, 176141240Snjl * if multiple CPUs can have different settings, we can move these 177141240Snjl * sysctls to be under every CPU instead of just the first one. 178141240Snjl */ 179141240Snjl numdevs = devclass_get_count(cpufreq_dc); 180141240Snjl if (numdevs > 1) 181141240Snjl return (0); 182141240Snjl 183144876Snjl CF_DEBUG("initializing one-time data for %s\n", 184144876Snjl device_get_nameunit(dev)); 185210422Savg sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf), 186210422Savg M_DEVBUF, M_WAITOK); 187141240Snjl SYSCTL_ADD_PROC(&sc->sysctl_ctx, 188141240Snjl SYSCTL_CHILDREN(device_get_sysctl_tree(parent)), 189141240Snjl OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 190141240Snjl cpufreq_curr_sysctl, "I", "Current CPU frequency"); 191141240Snjl SYSCTL_ADD_PROC(&sc->sysctl_ctx, 192141240Snjl SYSCTL_CHILDREN(device_get_sysctl_tree(parent)), 193141240Snjl OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0, 194141240Snjl cpufreq_levels_sysctl, "A", "CPU frequency levels"); 195141240Snjl 196167905Snjl /* 197167905Snjl * Queue a one-shot broadcast that levels have changed. 198167905Snjl * It will run once the system has completed booting. 199167905Snjl */ 200167905Snjl TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev); 201167905Snjl taskqueue_enqueue(taskqueue_thread, &sc->startup_task); 202167905Snjl 203141240Snjl return (0); 204141240Snjl} 205141240Snjl 206167905Snjl/* Handle any work to be done for all drivers that attached during boot. */ 207167905Snjlstatic void 208167905Snjlcpufreq_startup_task(void *ctx, int pending) 209167905Snjl{ 210167905Snjl 211167905Snjl cpufreq_settings_changed((device_t)ctx); 212167905Snjl} 213167905Snjl 214141240Snjlstatic int 215141240Snjlcpufreq_detach(device_t dev) 216141240Snjl{ 217141240Snjl struct cpufreq_softc *sc; 218150847Sume struct cf_saved_freq *saved_freq; 219141240Snjl int numdevs; 220141240Snjl 221144876Snjl CF_DEBUG("shutdown %s\n", device_get_nameunit(dev)); 222141240Snjl sc = device_get_softc(dev); 223141240Snjl sysctl_ctx_free(&sc->sysctl_ctx); 224141240Snjl 225150847Sume while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) { 226150847Sume SLIST_REMOVE_HEAD(&sc->saved_freq, link); 227150847Sume free(saved_freq, M_TEMP); 228150847Sume } 229150847Sume 230141240Snjl /* Only clean up these resources when the last device is detaching. */ 231141240Snjl numdevs = devclass_get_count(cpufreq_dc); 232144876Snjl if (numdevs == 1) { 233144876Snjl CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev)); 234210422Savg free(sc->levels_buf, M_DEVBUF); 235144876Snjl } 236141240Snjl 237141240Snjl return (0); 238141240Snjl} 239141240Snjl 240141240Snjlstatic int 241141240Snjlcf_set_method(device_t dev, const struct cf_level *level, int priority) 242141240Snjl{ 243141240Snjl struct cpufreq_softc *sc; 244141240Snjl const struct cf_setting *set; 245150847Sume struct cf_saved_freq *saved_freq, *curr_freq; 246141814Snjl struct pcpu *pc; 247171898Snjl int error, i; 248141240Snjl 249141240Snjl sc = device_get_softc(dev); 250142603Snjl error = 0; 251142603Snjl set = NULL; 252150847Sume saved_freq = NULL; 253141240Snjl 254167905Snjl /* We are going to change levels so notify the pre-change handler. */ 255167905Snjl EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error); 256167905Snjl if (error != 0) { 257167905Snjl EVENTHANDLER_INVOKE(cpufreq_post_change, level, error); 258167905Snjl return (error); 259156228Smnag } 260141814Snjl 261150847Sume CF_MTX_LOCK(&sc->lock); 262150847Sume 263173204Snjl#ifdef SMP 264141923Snjl /* 265173204Snjl * If still booting and secondary CPUs not started yet, don't allow 266173204Snjl * changing the frequency until they're online. This is because we 267173204Snjl * can't switch to them using sched_bind() and thus we'd only be 268173204Snjl * switching the main CPU. XXXTODO: Need to think more about how to 269173204Snjl * handle having different CPUs at different frequencies. 270173204Snjl */ 271265606Sscottl if (mp_ncpus > 1 && !smp_started) { 272173204Snjl device_printf(dev, "rejecting change, SMP not started yet\n"); 273173204Snjl error = ENXIO; 274173204Snjl goto out; 275173204Snjl } 276173204Snjl#endif /* SMP */ 277173204Snjl 278173204Snjl /* 279150847Sume * If the requested level has a lower priority, don't allow 280150847Sume * the new level right now. 281150847Sume */ 282150847Sume if (priority < sc->curr_priority) { 283150847Sume CF_DEBUG("ignoring, curr prio %d less than %d\n", priority, 284150847Sume sc->curr_priority); 285150847Sume error = EPERM; 286150847Sume goto out; 287150847Sume } 288150847Sume 289150847Sume /* 290141923Snjl * If the caller didn't specify a level and one is saved, prepare to 291141923Snjl * restore the saved level. If none has been saved, return an error. 292141923Snjl */ 293141923Snjl if (level == NULL) { 294150847Sume saved_freq = SLIST_FIRST(&sc->saved_freq); 295150847Sume if (saved_freq == NULL) { 296144876Snjl CF_DEBUG("NULL level, no saved level\n"); 297142603Snjl error = ENXIO; 298142603Snjl goto out; 299142603Snjl } 300150847Sume level = &saved_freq->level; 301150847Sume priority = saved_freq->priority; 302150847Sume CF_DEBUG("restoring saved level, freq %d prio %d\n", 303150847Sume level->total_set.freq, priority); 304142603Snjl } 305141923Snjl 306142590Snjl /* Reject levels that are below our specified threshold. */ 307148972Snjl if (level->total_set.freq < cf_lowest_freq) { 308144876Snjl CF_DEBUG("rejecting freq %d, less than %d limit\n", 309144876Snjl level->total_set.freq, cf_lowest_freq); 310142603Snjl error = EINVAL; 311142603Snjl goto out; 312142603Snjl } 313142590Snjl 314141240Snjl /* If already at this level, just return. */ 315232793Smav if (sc->curr_level.total_set.freq == level->total_set.freq) { 316144876Snjl CF_DEBUG("skipping freq %d, same as current level %d\n", 317144876Snjl level->total_set.freq, sc->curr_level.total_set.freq); 318149239Sume goto skip; 319144876Snjl } 320141240Snjl 321141240Snjl /* First, set the absolute frequency via its driver. */ 322141240Snjl set = &level->abs_set; 323141240Snjl if (set->dev) { 324141240Snjl if (!device_is_attached(set->dev)) { 325141240Snjl error = ENXIO; 326141240Snjl goto out; 327141240Snjl } 328141943Snjl 329171898Snjl /* Bind to the target CPU before switching. */ 330141943Snjl pc = cpu_get_pcpu(set->dev); 331171898Snjl thread_lock(curthread); 332171898Snjl sched_bind(curthread, pc->pc_cpuid); 333171898Snjl thread_unlock(curthread); 334144876Snjl CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq, 335144876Snjl device_get_nameunit(set->dev), PCPU_GET(cpuid)); 336141240Snjl error = CPUFREQ_DRV_SET(set->dev, set); 337171898Snjl thread_lock(curthread); 338171898Snjl sched_unbind(curthread); 339171898Snjl thread_unlock(curthread); 340141240Snjl if (error) { 341141240Snjl goto out; 342141240Snjl } 343141240Snjl } 344141240Snjl 345141413Snjl /* Next, set any/all relative frequencies via their drivers. */ 346141413Snjl for (i = 0; i < level->rel_count; i++) { 347141413Snjl set = &level->rel_set[i]; 348141413Snjl if (!device_is_attached(set->dev)) { 349141413Snjl error = ENXIO; 350141413Snjl goto out; 351141413Snjl } 352141943Snjl 353171898Snjl /* Bind to the target CPU before switching. */ 354141943Snjl pc = cpu_get_pcpu(set->dev); 355171898Snjl thread_lock(curthread); 356171898Snjl sched_bind(curthread, pc->pc_cpuid); 357171898Snjl thread_unlock(curthread); 358144876Snjl CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq, 359144876Snjl device_get_nameunit(set->dev), PCPU_GET(cpuid)); 360141413Snjl error = CPUFREQ_DRV_SET(set->dev, set); 361171898Snjl thread_lock(curthread); 362171898Snjl sched_unbind(curthread); 363171898Snjl thread_unlock(curthread); 364141413Snjl if (error) { 365141413Snjl /* XXX Back out any successful setting? */ 366141413Snjl goto out; 367141413Snjl } 368141413Snjl } 369141240Snjl 370149239Sumeskip: 371141923Snjl /* 372141923Snjl * Before recording the current level, check if we're going to a 373150847Sume * higher priority. If so, save the previous level and priority. 374141923Snjl */ 375141923Snjl if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN && 376150847Sume priority > sc->curr_priority) { 377144876Snjl CF_DEBUG("saving level, freq %d prio %d\n", 378144876Snjl sc->curr_level.total_set.freq, sc->curr_priority); 379150847Sume curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT); 380150847Sume if (curr_freq == NULL) { 381150847Sume error = ENOMEM; 382150847Sume goto out; 383150847Sume } 384150847Sume curr_freq->level = sc->curr_level; 385150847Sume curr_freq->priority = sc->curr_priority; 386150847Sume SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link); 387141923Snjl } 388141240Snjl sc->curr_level = *level; 389141923Snjl sc->curr_priority = priority; 390141240Snjl 391150847Sume /* If we were restoring a saved state, reset it to "unused". */ 392150847Sume if (saved_freq != NULL) { 393150847Sume CF_DEBUG("resetting saved level\n"); 394150847Sume sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN; 395150847Sume SLIST_REMOVE_HEAD(&sc->saved_freq, link); 396150847Sume free(saved_freq, M_TEMP); 397150847Sume } 398150847Sume 399141240Snjlout: 400142603Snjl CF_MTX_UNLOCK(&sc->lock); 401167905Snjl 402167905Snjl /* 403167905Snjl * We changed levels (or attempted to) so notify the post-change 404167905Snjl * handler of new frequency or error. 405167905Snjl */ 406167905Snjl EVENTHANDLER_INVOKE(cpufreq_post_change, level, error); 407142603Snjl if (error && set) 408141240Snjl device_printf(set->dev, "set freq failed, err %d\n", error); 409167905Snjl 410141240Snjl return (error); 411141240Snjl} 412141240Snjl 413141240Snjlstatic int 414141240Snjlcf_get_method(device_t dev, struct cf_level *level) 415141240Snjl{ 416141240Snjl struct cpufreq_softc *sc; 417141240Snjl struct cf_level *levels; 418141240Snjl struct cf_setting *curr_set, set; 419141240Snjl struct pcpu *pc; 420141240Snjl device_t *devs; 421266165Scperciva int bdiff, count, diff, error, i, n, numdevs; 422141240Snjl uint64_t rate; 423141240Snjl 424141240Snjl sc = device_get_softc(dev); 425142603Snjl error = 0; 426141240Snjl levels = NULL; 427141240Snjl 428141240Snjl /* If we already know the current frequency, we're done. */ 429142603Snjl CF_MTX_LOCK(&sc->lock); 430142603Snjl curr_set = &sc->curr_level.total_set; 431144876Snjl if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) { 432144876Snjl CF_DEBUG("get returning known freq %d\n", curr_set->freq); 433141240Snjl goto out; 434144876Snjl } 435142603Snjl CF_MTX_UNLOCK(&sc->lock); 436141240Snjl 437141240Snjl /* 438141240Snjl * We need to figure out the current level. Loop through every 439141240Snjl * driver, getting the current setting. Then, attempt to get a best 440141240Snjl * match of settings against each level. 441141240Snjl */ 442141240Snjl count = CF_MAX_LEVELS; 443141240Snjl levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT); 444141240Snjl if (levels == NULL) 445141240Snjl return (ENOMEM); 446141240Snjl error = CPUFREQ_LEVELS(sc->dev, levels, &count); 447142395Snjl if (error) { 448142395Snjl if (error == E2BIG) 449142395Snjl printf("cpufreq: need to increase CF_MAX_LEVELS\n"); 450142603Snjl free(levels, M_TEMP); 451142603Snjl return (error); 452142395Snjl } 453141240Snjl error = device_get_children(device_get_parent(dev), &devs, &numdevs); 454142603Snjl if (error) { 455142603Snjl free(levels, M_TEMP); 456142603Snjl return (error); 457142603Snjl } 458142603Snjl 459142603Snjl /* 460142603Snjl * Reacquire the lock and search for the given level. 461142603Snjl * 462142603Snjl * XXX Note: this is not quite right since we really need to go 463142603Snjl * through each level and compare both absolute and relative 464142603Snjl * settings for each driver in the system before making a match. 465142603Snjl * The estimation code below catches this case though. 466142603Snjl */ 467142603Snjl CF_MTX_LOCK(&sc->lock); 468171896Snjl for (n = 0; n < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; n++) { 469171896Snjl if (!device_is_attached(devs[n])) 470141240Snjl continue; 471178787Sjhb if (CPUFREQ_DRV_GET(devs[n], &set) != 0) 472141240Snjl continue; 473141240Snjl for (i = 0; i < count; i++) { 474232793Smav if (set.freq == levels[i].total_set.freq) { 475141240Snjl sc->curr_level = levels[i]; 476141240Snjl break; 477141240Snjl } 478141240Snjl } 479141240Snjl } 480141240Snjl free(devs, M_TEMP); 481144876Snjl if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) { 482144876Snjl CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq); 483141240Snjl goto out; 484144876Snjl } 485141240Snjl 486141240Snjl /* 487141240Snjl * We couldn't find an exact match, so attempt to estimate and then 488141240Snjl * match against a level. 489141240Snjl */ 490141240Snjl pc = cpu_get_pcpu(dev); 491141240Snjl if (pc == NULL) { 492141240Snjl error = ENXIO; 493141240Snjl goto out; 494141240Snjl } 495141240Snjl cpu_est_clockrate(pc->pc_cpuid, &rate); 496141240Snjl rate /= 1000000; 497266165Scperciva bdiff = 1 << 30; 498141240Snjl for (i = 0; i < count; i++) { 499266165Scperciva diff = abs(levels[i].total_set.freq - rate); 500266165Scperciva if (diff < bdiff) { 501266165Scperciva bdiff = diff; 502141240Snjl sc->curr_level = levels[i]; 503141240Snjl } 504141240Snjl } 505266165Scperciva CF_DEBUG("get estimated freq %d\n", curr_set->freq); 506141240Snjl 507141240Snjlout: 508142603Snjl if (error == 0) 509142603Snjl *level = sc->curr_level; 510142603Snjl 511142603Snjl CF_MTX_UNLOCK(&sc->lock); 512141240Snjl if (levels) 513141240Snjl free(levels, M_TEMP); 514142603Snjl return (error); 515141240Snjl} 516141240Snjl 517141240Snjlstatic int 518141240Snjlcf_levels_method(device_t dev, struct cf_level *levels, int *count) 519141240Snjl{ 520141413Snjl struct cf_setting_array *set_arr; 521141240Snjl struct cf_setting_lst rel_sets; 522141240Snjl struct cpufreq_softc *sc; 523141240Snjl struct cf_level *lev; 524141240Snjl struct cf_setting *sets; 525141240Snjl struct pcpu *pc; 526141240Snjl device_t *devs; 527141413Snjl int error, i, numdevs, set_count, type; 528141240Snjl uint64_t rate; 529141240Snjl 530141240Snjl if (levels == NULL || count == NULL) 531141240Snjl return (EINVAL); 532141240Snjl 533141240Snjl TAILQ_INIT(&rel_sets); 534141240Snjl sc = device_get_softc(dev); 535141240Snjl error = device_get_children(device_get_parent(dev), &devs, &numdevs); 536141240Snjl if (error) 537141240Snjl return (error); 538141240Snjl sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT); 539141240Snjl if (sets == NULL) { 540141240Snjl free(devs, M_TEMP); 541141240Snjl return (ENOMEM); 542141240Snjl } 543141240Snjl 544141240Snjl /* Get settings from all cpufreq drivers. */ 545142603Snjl CF_MTX_LOCK(&sc->lock); 546141240Snjl for (i = 0; i < numdevs; i++) { 547141824Snjl /* Skip devices that aren't ready. */ 548141240Snjl if (!device_is_attached(devs[i])) 549141240Snjl continue; 550141824Snjl 551141824Snjl /* 552141824Snjl * Get settings, skipping drivers that offer no settings or 553141824Snjl * provide settings for informational purposes only. 554141824Snjl */ 555142032Snjl error = CPUFREQ_DRV_TYPE(devs[i], &type); 556144876Snjl if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) { 557144876Snjl if (error == 0) { 558144876Snjl CF_DEBUG("skipping info-only driver %s\n", 559144876Snjl device_get_nameunit(devs[i])); 560144876Snjl } 561142032Snjl continue; 562144876Snjl } 563141240Snjl set_count = MAX_SETTINGS; 564142032Snjl error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count); 565142032Snjl if (error || set_count == 0) 566141240Snjl continue; 567141413Snjl 568141824Snjl /* Add the settings to our absolute/relative lists. */ 569141814Snjl switch (type & CPUFREQ_TYPE_MASK) { 570141413Snjl case CPUFREQ_TYPE_ABSOLUTE: 571141413Snjl error = cpufreq_insert_abs(sc, sets, set_count); 572141413Snjl break; 573141413Snjl case CPUFREQ_TYPE_RELATIVE: 574144876Snjl CF_DEBUG("adding %d relative settings\n", set_count); 575141413Snjl set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT); 576141413Snjl if (set_arr == NULL) { 577141413Snjl error = ENOMEM; 578141413Snjl goto out; 579141413Snjl } 580141413Snjl bcopy(sets, set_arr->sets, set_count * sizeof(*sets)); 581141413Snjl set_arr->count = set_count; 582141413Snjl TAILQ_INSERT_TAIL(&rel_sets, set_arr, link); 583141413Snjl break; 584141413Snjl default: 585141413Snjl error = EINVAL; 586141413Snjl } 587141240Snjl if (error) 588141240Snjl goto out; 589141240Snjl } 590141240Snjl 591141945Snjl /* 592141945Snjl * If there are no absolute levels, create a fake one at 100%. We 593141945Snjl * then cache the clockrate for later use as our base frequency. 594141945Snjl */ 595141240Snjl if (TAILQ_EMPTY(&sc->all_levels)) { 596141945Snjl if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) { 597193155Snwhitehorn sc->max_mhz = cpu_get_nominal_mhz(dev); 598193155Snwhitehorn /* 599193155Snwhitehorn * If the CPU can't report a rate for 100%, hope 600193155Snwhitehorn * the CPU is running at its nominal rate right now, 601193155Snwhitehorn * and use that instead. 602193155Snwhitehorn */ 603193155Snwhitehorn if (sc->max_mhz <= 0) { 604193155Snwhitehorn pc = cpu_get_pcpu(dev); 605193155Snwhitehorn cpu_est_clockrate(pc->pc_cpuid, &rate); 606193155Snwhitehorn sc->max_mhz = rate / 1000000; 607193155Snwhitehorn } 608141240Snjl } 609141945Snjl memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets)); 610141945Snjl sets[0].freq = sc->max_mhz; 611141945Snjl sets[0].dev = NULL; 612141413Snjl error = cpufreq_insert_abs(sc, sets, 1); 613141240Snjl if (error) 614141240Snjl goto out; 615141240Snjl } 616141240Snjl 617141413Snjl /* Create a combined list of absolute + relative levels. */ 618141413Snjl TAILQ_FOREACH(set_arr, &rel_sets, link) 619141413Snjl cpufreq_expand_set(sc, set_arr); 620141413Snjl 621141413Snjl /* If the caller doesn't have enough space, return the actual count. */ 622141413Snjl if (sc->all_count > *count) { 623141413Snjl *count = sc->all_count; 624141413Snjl error = E2BIG; 625141413Snjl goto out; 626141413Snjl } 627141413Snjl 628141413Snjl /* Finally, output the list of levels. */ 629141240Snjl i = 0; 630141240Snjl TAILQ_FOREACH(lev, &sc->all_levels, link) { 631175376Snjl 632142590Snjl /* Skip levels that have a frequency that is too low. */ 633148972Snjl if (lev->total_set.freq < cf_lowest_freq) { 634142590Snjl sc->all_count--; 635142590Snjl continue; 636142590Snjl } 637142590Snjl 638141240Snjl levels[i] = *lev; 639141240Snjl i++; 640141240Snjl } 641141413Snjl *count = sc->all_count; 642141240Snjl error = 0; 643141240Snjl 644141240Snjlout: 645141240Snjl /* Clear all levels since we regenerate them each time. */ 646141240Snjl while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) { 647141240Snjl TAILQ_REMOVE(&sc->all_levels, lev, link); 648141240Snjl free(lev, M_TEMP); 649141240Snjl } 650142603Snjl sc->all_count = 0; 651142603Snjl 652142603Snjl CF_MTX_UNLOCK(&sc->lock); 653141413Snjl while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) { 654141413Snjl TAILQ_REMOVE(&rel_sets, set_arr, link); 655141413Snjl free(set_arr, M_TEMP); 656141413Snjl } 657141240Snjl free(devs, M_TEMP); 658141240Snjl free(sets, M_TEMP); 659141240Snjl return (error); 660141240Snjl} 661141240Snjl 662141240Snjl/* 663141240Snjl * Create levels for an array of absolute settings and insert them in 664141240Snjl * sorted order in the specified list. 665141240Snjl */ 666141240Snjlstatic int 667141413Snjlcpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets, 668141240Snjl int count) 669141240Snjl{ 670141413Snjl struct cf_level_lst *list; 671141240Snjl struct cf_level *level, *search; 672141240Snjl int i; 673141240Snjl 674142603Snjl CF_MTX_ASSERT(&sc->lock); 675142603Snjl 676141413Snjl list = &sc->all_levels; 677141240Snjl for (i = 0; i < count; i++) { 678141240Snjl level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO); 679141240Snjl if (level == NULL) 680141240Snjl return (ENOMEM); 681141240Snjl level->abs_set = sets[i]; 682141413Snjl level->total_set = sets[i]; 683141413Snjl level->total_set.dev = NULL; 684141413Snjl sc->all_count++; 685141240Snjl 686141240Snjl if (TAILQ_EMPTY(list)) { 687144876Snjl CF_DEBUG("adding abs setting %d at head\n", 688144876Snjl sets[i].freq); 689141240Snjl TAILQ_INSERT_HEAD(list, level, link); 690141240Snjl continue; 691141240Snjl } 692141240Snjl 693141240Snjl TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) { 694141413Snjl if (sets[i].freq <= search->total_set.freq) { 695144876Snjl CF_DEBUG("adding abs setting %d after %d\n", 696144876Snjl sets[i].freq, search->total_set.freq); 697141240Snjl TAILQ_INSERT_AFTER(list, search, level, link); 698141240Snjl break; 699141240Snjl } 700141240Snjl } 701141240Snjl } 702141240Snjl return (0); 703141240Snjl} 704141240Snjl 705141413Snjl/* 706141413Snjl * Expand a group of relative settings, creating derived levels from them. 707141413Snjl */ 708141240Snjlstatic int 709141413Snjlcpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr) 710141413Snjl{ 711141413Snjl struct cf_level *fill, *search; 712141413Snjl struct cf_setting *set; 713141413Snjl int i; 714141413Snjl 715142603Snjl CF_MTX_ASSERT(&sc->lock); 716142603Snjl 717149607Snjl /* 718149607Snjl * Walk the set of all existing levels in reverse. This is so we 719149607Snjl * create derived states from the lowest absolute settings first 720149607Snjl * and discard duplicates created from higher absolute settings. 721149607Snjl * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is 722149607Snjl * preferable to 200 Mhz + 25% because absolute settings are more 723149607Snjl * efficient since they often change the voltage as well. 724149607Snjl */ 725149607Snjl TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) { 726141413Snjl /* Add each setting to the level, duplicating if necessary. */ 727141413Snjl for (i = 0; i < set_arr->count; i++) { 728141413Snjl set = &set_arr->sets[i]; 729141413Snjl 730141413Snjl /* 731141413Snjl * If this setting is less than 100%, split the level 732141413Snjl * into two and add this setting to the new level. 733141413Snjl */ 734141413Snjl fill = search; 735149607Snjl if (set->freq < 10000) { 736141413Snjl fill = cpufreq_dup_set(sc, search, set); 737141413Snjl 738149607Snjl /* 739149607Snjl * The new level was a duplicate of an existing 740149607Snjl * level or its absolute setting is too high 741149607Snjl * so we freed it. For example, we discard a 742149607Snjl * derived level of 1000 MHz/25% if a level 743149607Snjl * of 500 MHz/100% already exists. 744149607Snjl */ 745149607Snjl if (fill == NULL) 746149607Snjl break; 747149607Snjl } 748141413Snjl 749141413Snjl /* Add this setting to the existing or new level. */ 750141413Snjl KASSERT(fill->rel_count < MAX_SETTINGS, 751141413Snjl ("cpufreq: too many relative drivers (%d)", 752141413Snjl MAX_SETTINGS)); 753141413Snjl fill->rel_set[fill->rel_count] = *set; 754141413Snjl fill->rel_count++; 755144876Snjl CF_DEBUG( 756144876Snjl "expand set added rel setting %d%% to %d level\n", 757144876Snjl set->freq / 100, fill->total_set.freq); 758141413Snjl } 759141413Snjl } 760141413Snjl 761141413Snjl return (0); 762141413Snjl} 763141413Snjl 764141413Snjlstatic struct cf_level * 765141413Snjlcpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup, 766141413Snjl struct cf_setting *set) 767141413Snjl{ 768141413Snjl struct cf_level_lst *list; 769141413Snjl struct cf_level *fill, *itr; 770141413Snjl struct cf_setting *fill_set, *itr_set; 771141413Snjl int i; 772141413Snjl 773142603Snjl CF_MTX_ASSERT(&sc->lock); 774142603Snjl 775141413Snjl /* 776141413Snjl * Create a new level, copy it from the old one, and update the 777141413Snjl * total frequency and power by the percentage specified in the 778141413Snjl * relative setting. 779141413Snjl */ 780141413Snjl fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT); 781141413Snjl if (fill == NULL) 782141413Snjl return (NULL); 783141413Snjl *fill = *dup; 784141413Snjl fill_set = &fill->total_set; 785141413Snjl fill_set->freq = 786141413Snjl ((uint64_t)fill_set->freq * set->freq) / 10000; 787141413Snjl if (fill_set->power != CPUFREQ_VAL_UNKNOWN) { 788141413Snjl fill_set->power = ((uint64_t)fill_set->power * set->freq) 789141413Snjl / 10000; 790141413Snjl } 791141413Snjl if (set->lat != CPUFREQ_VAL_UNKNOWN) { 792141413Snjl if (fill_set->lat != CPUFREQ_VAL_UNKNOWN) 793141413Snjl fill_set->lat += set->lat; 794141413Snjl else 795141413Snjl fill_set->lat = set->lat; 796141413Snjl } 797144876Snjl CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq); 798141413Snjl 799141413Snjl /* 800141413Snjl * If we copied an old level that we already modified (say, at 100%), 801141413Snjl * we need to remove that setting before adding this one. Since we 802141413Snjl * process each setting array in order, we know any settings for this 803141413Snjl * driver will be found at the end. 804141413Snjl */ 805141413Snjl for (i = fill->rel_count; i != 0; i--) { 806141413Snjl if (fill->rel_set[i - 1].dev != set->dev) 807141413Snjl break; 808144876Snjl CF_DEBUG("removed last relative driver: %s\n", 809144876Snjl device_get_nameunit(set->dev)); 810141413Snjl fill->rel_count--; 811141413Snjl } 812141413Snjl 813141413Snjl /* 814149607Snjl * Insert the new level in sorted order. If it is a duplicate of an 815149607Snjl * existing level (1) or has an absolute setting higher than the 816149607Snjl * existing level (2), do not add it. We can do this since any such 817149607Snjl * level is guaranteed use less power. For example (1), a level with 818149607Snjl * one absolute setting of 800 Mhz uses less power than one composed 819149607Snjl * of an absolute setting of 1600 Mhz and a relative setting at 50%. 820149607Snjl * Also for example (2), a level of 800 Mhz/75% is preferable to 821149607Snjl * 1600 Mhz/25% even though the latter has a lower total frequency. 822141413Snjl */ 823141413Snjl list = &sc->all_levels; 824149607Snjl KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set")); 825149607Snjl TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) { 826149607Snjl itr_set = &itr->total_set; 827149724Snjl if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) { 828149724Snjl CF_DEBUG("dup set rejecting %d (dupe)\n", 829149724Snjl fill_set->freq); 830149724Snjl itr = NULL; 831149607Snjl break; 832149724Snjl } else if (fill_set->freq < itr_set->freq) { 833149724Snjl if (fill->abs_set.freq <= itr->abs_set.freq) { 834149724Snjl CF_DEBUG( 835149724Snjl "dup done, inserting new level %d after %d\n", 836149724Snjl fill_set->freq, itr_set->freq); 837149724Snjl TAILQ_INSERT_AFTER(list, itr, fill, link); 838149724Snjl sc->all_count++; 839149724Snjl } else { 840149724Snjl CF_DEBUG("dup set rejecting %d (abs too big)\n", 841149724Snjl fill_set->freq); 842149724Snjl itr = NULL; 843149724Snjl } 844149724Snjl break; 845141413Snjl } 846141413Snjl } 847141413Snjl 848149607Snjl /* We didn't find a good place for this new level so free it. */ 849149607Snjl if (itr == NULL) { 850149607Snjl CF_DEBUG("dup set freeing new level %d (not optimal)\n", 851149607Snjl fill_set->freq); 852149607Snjl free(fill, M_TEMP); 853149607Snjl fill = NULL; 854149607Snjl } 855149607Snjl 856141413Snjl return (fill); 857141413Snjl} 858141413Snjl 859141413Snjlstatic int 860141240Snjlcpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS) 861141240Snjl{ 862141240Snjl struct cpufreq_softc *sc; 863141240Snjl struct cf_level *levels; 864232793Smav int best, count, diff, bdiff, devcount, error, freq, i, n; 865141814Snjl device_t *devs; 866141240Snjl 867141814Snjl devs = NULL; 868141240Snjl sc = oidp->oid_arg1; 869210422Savg levels = sc->levels_buf; 870141240Snjl 871141240Snjl error = CPUFREQ_GET(sc->dev, &levels[0]); 872141240Snjl if (error) 873141240Snjl goto out; 874141240Snjl freq = levels[0].total_set.freq; 875141240Snjl error = sysctl_handle_int(oidp, &freq, 0, req); 876141240Snjl if (error != 0 || req->newptr == NULL) 877141240Snjl goto out; 878141240Snjl 879141814Snjl /* 880141814Snjl * While we only call cpufreq_get() on one device (assuming all 881141814Snjl * CPUs have equal levels), we call cpufreq_set() on all CPUs. 882141814Snjl * This is needed for some MP systems. 883141814Snjl */ 884141814Snjl error = devclass_get_devices(cpufreq_dc, &devs, &devcount); 885141240Snjl if (error) 886141240Snjl goto out; 887141814Snjl for (n = 0; n < devcount; n++) { 888141814Snjl count = CF_MAX_LEVELS; 889141814Snjl error = CPUFREQ_LEVELS(devs[n], levels, &count); 890142395Snjl if (error) { 891142395Snjl if (error == E2BIG) 892142395Snjl printf( 893142395Snjl "cpufreq: need to increase CF_MAX_LEVELS\n"); 894141240Snjl break; 895142395Snjl } 896232793Smav best = 0; 897232793Smav bdiff = 1 << 30; 898141814Snjl for (i = 0; i < count; i++) { 899232793Smav diff = abs(levels[i].total_set.freq - freq); 900232793Smav if (diff < bdiff) { 901232793Smav bdiff = diff; 902232793Smav best = i; 903141814Snjl } 904141240Snjl } 905232793Smav error = CPUFREQ_SET(devs[n], &levels[best], CPUFREQ_PRIO_USER); 906141240Snjl } 907141240Snjl 908141240Snjlout: 909141814Snjl if (devs) 910141814Snjl free(devs, M_TEMP); 911141240Snjl return (error); 912141240Snjl} 913141240Snjl 914141240Snjlstatic int 915141240Snjlcpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS) 916141240Snjl{ 917141240Snjl struct cpufreq_softc *sc; 918141240Snjl struct cf_level *levels; 919141240Snjl struct cf_setting *set; 920141240Snjl struct sbuf sb; 921141240Snjl int count, error, i; 922141240Snjl 923141240Snjl sc = oidp->oid_arg1; 924141240Snjl sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND); 925141240Snjl 926141240Snjl /* Get settings from the device and generate the output string. */ 927141240Snjl count = CF_MAX_LEVELS; 928210422Savg levels = sc->levels_buf; 929201848Sbrueffer if (levels == NULL) { 930201848Sbrueffer sbuf_delete(&sb); 931141240Snjl return (ENOMEM); 932201848Sbrueffer } 933141240Snjl error = CPUFREQ_LEVELS(sc->dev, levels, &count); 934142395Snjl if (error) { 935142395Snjl if (error == E2BIG) 936142395Snjl printf("cpufreq: need to increase CF_MAX_LEVELS\n"); 937141240Snjl goto out; 938142395Snjl } 939141240Snjl if (count) { 940141240Snjl for (i = 0; i < count; i++) { 941141240Snjl set = &levels[i].total_set; 942141240Snjl sbuf_printf(&sb, "%d/%d ", set->freq, set->power); 943141240Snjl } 944141240Snjl } else 945141240Snjl sbuf_cpy(&sb, "0"); 946141240Snjl sbuf_trim(&sb); 947141240Snjl sbuf_finish(&sb); 948141240Snjl error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 949141240Snjl 950141240Snjlout: 951141240Snjl sbuf_delete(&sb); 952141240Snjl return (error); 953141240Snjl} 954141240Snjl 955142114Snjlstatic int 956142114Snjlcpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS) 957142114Snjl{ 958142114Snjl device_t dev; 959142114Snjl struct cf_setting *sets; 960142114Snjl struct sbuf sb; 961142114Snjl int error, i, set_count; 962142114Snjl 963142114Snjl dev = oidp->oid_arg1; 964142114Snjl sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND); 965142114Snjl 966142114Snjl /* Get settings from the device and generate the output string. */ 967142114Snjl set_count = MAX_SETTINGS; 968142114Snjl sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT); 969201848Sbrueffer if (sets == NULL) { 970201848Sbrueffer sbuf_delete(&sb); 971142114Snjl return (ENOMEM); 972201848Sbrueffer } 973142114Snjl error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count); 974142114Snjl if (error) 975142114Snjl goto out; 976142114Snjl if (set_count) { 977142114Snjl for (i = 0; i < set_count; i++) 978142114Snjl sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power); 979142114Snjl } else 980142114Snjl sbuf_cpy(&sb, "0"); 981142114Snjl sbuf_trim(&sb); 982142114Snjl sbuf_finish(&sb); 983142114Snjl error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 984142114Snjl 985142114Snjlout: 986142114Snjl free(sets, M_TEMP); 987142114Snjl sbuf_delete(&sb); 988142114Snjl return (error); 989142114Snjl} 990142114Snjl 991141240Snjlint 992141240Snjlcpufreq_register(device_t dev) 993141240Snjl{ 994141945Snjl struct cpufreq_softc *sc; 995141240Snjl device_t cf_dev, cpu_dev; 996141240Snjl 997142114Snjl /* Add a sysctl to get each driver's settings separately. */ 998142114Snjl SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 999142114Snjl SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1000142114Snjl OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0, 1001142114Snjl cpufreq_settings_sysctl, "A", "CPU frequency driver settings"); 1002142114Snjl 1003141240Snjl /* 1004141814Snjl * Add only one cpufreq device to each CPU. Currently, all CPUs 1005141814Snjl * must offer the same levels and be switched at the same time. 1006141240Snjl */ 1007141814Snjl cpu_dev = device_get_parent(dev); 1008141945Snjl if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) { 1009141945Snjl sc = device_get_softc(cf_dev); 1010141945Snjl sc->max_mhz = CPUFREQ_VAL_UNKNOWN; 1011141240Snjl return (0); 1012141945Snjl } 1013141240Snjl 1014141814Snjl /* Add the child device and possibly sysctls. */ 1015141814Snjl cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1); 1016141240Snjl if (cf_dev == NULL) 1017141240Snjl return (ENOMEM); 1018141240Snjl device_quiet(cf_dev); 1019141240Snjl 1020141240Snjl return (device_probe_and_attach(cf_dev)); 1021141240Snjl} 1022141240Snjl 1023141240Snjlint 1024141240Snjlcpufreq_unregister(device_t dev) 1025141240Snjl{ 1026141240Snjl device_t cf_dev, *devs; 1027142032Snjl int cfcount, devcount, error, i, type; 1028141240Snjl 1029141240Snjl /* 1030141240Snjl * If this is the last cpufreq child device, remove the control 1031141240Snjl * device as well. We identify cpufreq children by calling a method 1032141240Snjl * they support. 1033141240Snjl */ 1034141240Snjl error = device_get_children(device_get_parent(dev), &devs, &devcount); 1035141240Snjl if (error) 1036141240Snjl return (error); 1037141945Snjl cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1); 1038144413Snjl if (cf_dev == NULL) { 1039144413Snjl device_printf(dev, 1040144413Snjl "warning: cpufreq_unregister called with no cpufreq device active\n"); 1041265768Sbrueffer free(devs, M_TEMP); 1042144413Snjl return (0); 1043144413Snjl } 1044141240Snjl cfcount = 0; 1045141240Snjl for (i = 0; i < devcount; i++) { 1046141240Snjl if (!device_is_attached(devs[i])) 1047141240Snjl continue; 1048142032Snjl if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0) 1049141240Snjl cfcount++; 1050141240Snjl } 1051141814Snjl if (cfcount <= 1) 1052141240Snjl device_delete_child(device_get_parent(cf_dev), cf_dev); 1053141240Snjl free(devs, M_TEMP); 1054141240Snjl 1055141240Snjl return (0); 1056141240Snjl} 1057167905Snjl 1058167905Snjlint 1059167905Snjlcpufreq_settings_changed(device_t dev) 1060167905Snjl{ 1061167905Snjl 1062167905Snjl EVENTHANDLER_INVOKE(cpufreq_levels_changed, 1063167905Snjl device_get_unit(device_get_parent(dev))); 1064167905Snjl return (0); 1065167905Snjl} 1066