173347b07SNate Lawson /*- 273347b07SNate Lawson * Copyright (c) 2004-2005 Nate Lawson (SDG) 373347b07SNate Lawson * All rights reserved. 473347b07SNate Lawson * 573347b07SNate Lawson * Redistribution and use in source and binary forms, with or without 673347b07SNate Lawson * modification, are permitted provided that the following conditions 773347b07SNate Lawson * are met: 873347b07SNate Lawson * 1. Redistributions of source code must retain the above copyright 973347b07SNate Lawson * notice, this list of conditions and the following disclaimer. 1073347b07SNate Lawson * 2. Redistributions in binary form must reproduce the above copyright 1173347b07SNate Lawson * notice, this list of conditions and the following disclaimer in the 1273347b07SNate Lawson * documentation and/or other materials provided with the distribution. 1373347b07SNate Lawson * 1473347b07SNate Lawson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1573347b07SNate Lawson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1673347b07SNate Lawson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1773347b07SNate Lawson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1873347b07SNate Lawson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1973347b07SNate Lawson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2073347b07SNate Lawson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2173347b07SNate Lawson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2273347b07SNate Lawson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2373347b07SNate Lawson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2473347b07SNate Lawson * SUCH DAMAGE. 2573347b07SNate Lawson */ 2673347b07SNate Lawson 2773347b07SNate Lawson #include <sys/cdefs.h> 2873347b07SNate Lawson __FBSDID("$FreeBSD$"); 2973347b07SNate Lawson 3073347b07SNate Lawson #include <sys/param.h> 3173347b07SNate Lawson #include <sys/bus.h> 3273347b07SNate Lawson #include <sys/cpu.h> 3373347b07SNate Lawson #include <sys/eventhandler.h> 3473347b07SNate Lawson #include <sys/kernel.h> 35789f03ceSNate Lawson #include <sys/lock.h> 3673347b07SNate Lawson #include <sys/malloc.h> 3773347b07SNate Lawson #include <sys/module.h> 3873347b07SNate Lawson #include <sys/proc.h> 3973347b07SNate Lawson #include <sys/queue.h> 4073347b07SNate Lawson #include <sys/sched.h> 4173347b07SNate Lawson #include <sys/sysctl.h> 4273347b07SNate Lawson #include <sys/systm.h> 4373347b07SNate Lawson #include <sys/sbuf.h> 44789f03ceSNate Lawson #include <sys/sx.h> 450325089dSNate Lawson #include <sys/timetc.h> 4673347b07SNate Lawson 4773347b07SNate Lawson #include "cpufreq_if.h" 4873347b07SNate Lawson 4973347b07SNate Lawson /* 5073347b07SNate Lawson * Common CPU frequency glue code. Drivers for specific hardware can 5173347b07SNate Lawson * attach this interface to allow users to get/set the CPU frequency. 5273347b07SNate Lawson */ 5373347b07SNate Lawson 5473347b07SNate Lawson /* 5573347b07SNate Lawson * Number of levels we can handle. Levels are synthesized from settings 56d269386aSNate Lawson * so for M settings and N drivers, there may be M*N levels. 5773347b07SNate Lawson */ 58d269386aSNate Lawson #define CF_MAX_LEVELS 64 5973347b07SNate Lawson 6073347b07SNate Lawson struct cpufreq_softc { 61789f03ceSNate Lawson struct sx lock; 6273347b07SNate Lawson struct cf_level curr_level; 635f0afa04SNate Lawson int curr_priority; 645f0afa04SNate Lawson struct cf_level saved_level; 655f0afa04SNate Lawson int saved_priority; 6673347b07SNate Lawson struct cf_level_lst all_levels; 675f0afa04SNate Lawson int all_count; 6867c8649fSNate Lawson int max_mhz; 6973347b07SNate Lawson device_t dev; 7073347b07SNate Lawson struct sysctl_ctx_list sysctl_ctx; 7173347b07SNate Lawson }; 7273347b07SNate Lawson 7373347b07SNate Lawson struct cf_setting_array { 7473347b07SNate Lawson struct cf_setting sets[MAX_SETTINGS]; 7573347b07SNate Lawson int count; 7673347b07SNate Lawson TAILQ_ENTRY(cf_setting_array) link; 7773347b07SNate Lawson }; 7873347b07SNate Lawson 7973347b07SNate Lawson TAILQ_HEAD(cf_setting_lst, cf_setting_array); 8073347b07SNate Lawson 81789f03ceSNate Lawson #define CF_MTX_INIT(x) sx_init((x), "cpufreq lock") 82789f03ceSNate Lawson #define CF_MTX_LOCK(x) sx_xlock((x)) 83789f03ceSNate Lawson #define CF_MTX_UNLOCK(x) sx_xunlock((x)) 84789f03ceSNate Lawson #define CF_MTX_ASSERT(x) sx_assert((x), SX_XLOCKED) 85789f03ceSNate Lawson 868d913481SNate Lawson #define CF_DEBUG(msg...) do { \ 878d913481SNate Lawson if (cf_verbose) \ 888d913481SNate Lawson printf("cpufreq: " msg); \ 898d913481SNate Lawson } while (0) 908d913481SNate Lawson 9173347b07SNate Lawson static int cpufreq_attach(device_t dev); 9273347b07SNate Lawson static int cpufreq_detach(device_t dev); 9373347b07SNate Lawson static void cpufreq_evaluate(void *arg); 9473347b07SNate Lawson static int cf_set_method(device_t dev, const struct cf_level *level, 9573347b07SNate Lawson int priority); 9673347b07SNate Lawson static int cf_get_method(device_t dev, struct cf_level *level); 9773347b07SNate Lawson static int cf_levels_method(device_t dev, struct cf_level *levels, 9873347b07SNate Lawson int *count); 9988c9b54cSNate Lawson static int cpufreq_insert_abs(struct cpufreq_softc *sc, 10073347b07SNate Lawson struct cf_setting *sets, int count); 10188c9b54cSNate Lawson static int cpufreq_expand_set(struct cpufreq_softc *sc, 10288c9b54cSNate Lawson struct cf_setting_array *set_arr); 10388c9b54cSNate Lawson static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc, 10488c9b54cSNate Lawson struct cf_level *dup, struct cf_setting *set); 10573347b07SNate Lawson static int cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS); 10673347b07SNate Lawson static int cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS); 107e959a70bSNate Lawson static int cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS); 10873347b07SNate Lawson 10973347b07SNate Lawson static device_method_t cpufreq_methods[] = { 11073347b07SNate Lawson DEVMETHOD(device_probe, bus_generic_probe), 11173347b07SNate Lawson DEVMETHOD(device_attach, cpufreq_attach), 11273347b07SNate Lawson DEVMETHOD(device_detach, cpufreq_detach), 11373347b07SNate Lawson 11473347b07SNate Lawson DEVMETHOD(cpufreq_set, cf_set_method), 11573347b07SNate Lawson DEVMETHOD(cpufreq_get, cf_get_method), 11673347b07SNate Lawson DEVMETHOD(cpufreq_levels, cf_levels_method), 11773347b07SNate Lawson {0, 0} 11873347b07SNate Lawson }; 11973347b07SNate Lawson static driver_t cpufreq_driver = { 12073347b07SNate Lawson "cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc) 12173347b07SNate Lawson }; 12273347b07SNate Lawson static devclass_t cpufreq_dc; 12373347b07SNate Lawson DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0); 12473347b07SNate Lawson 12573347b07SNate Lawson static eventhandler_tag cf_ev_tag; 12673347b07SNate Lawson 127b070969bSNate Lawson static int cf_lowest_freq; 1288d913481SNate Lawson static int cf_verbose; 129b070969bSNate Lawson TUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq); 1308d913481SNate Lawson TUNABLE_INT("debug.cpufreq.verbose", &cf_verbose); 131b070969bSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, "cpufreq debugging"); 132b070969bSNate Lawson SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1, 133b070969bSNate Lawson "Don't provide levels below this frequency."); 1348d913481SNate Lawson SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1, 1358d913481SNate Lawson "Print verbose debugging messages"); 136b070969bSNate Lawson 13773347b07SNate Lawson static int 13873347b07SNate Lawson cpufreq_attach(device_t dev) 13973347b07SNate Lawson { 14073347b07SNate Lawson struct cpufreq_softc *sc; 14173347b07SNate Lawson device_t parent; 14273347b07SNate Lawson int numdevs; 14373347b07SNate Lawson 1448d913481SNate Lawson CF_DEBUG("initializing %s\n", device_get_nameunit(dev)); 14573347b07SNate Lawson sc = device_get_softc(dev); 14673347b07SNate Lawson parent = device_get_parent(dev); 14773347b07SNate Lawson sc->dev = dev; 14873347b07SNate Lawson sysctl_ctx_init(&sc->sysctl_ctx); 14973347b07SNate Lawson TAILQ_INIT(&sc->all_levels); 150789f03ceSNate Lawson CF_MTX_INIT(&sc->lock); 15173347b07SNate Lawson sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN; 1525f0afa04SNate Lawson sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN; 15367c8649fSNate Lawson sc->max_mhz = CPUFREQ_VAL_UNKNOWN; 15473347b07SNate Lawson 15573347b07SNate Lawson /* 15673347b07SNate Lawson * Only initialize one set of sysctls for all CPUs. In the future, 15773347b07SNate Lawson * if multiple CPUs can have different settings, we can move these 15873347b07SNate Lawson * sysctls to be under every CPU instead of just the first one. 15973347b07SNate Lawson */ 16073347b07SNate Lawson numdevs = devclass_get_count(cpufreq_dc); 16173347b07SNate Lawson if (numdevs > 1) 16273347b07SNate Lawson return (0); 16373347b07SNate Lawson 1648d913481SNate Lawson CF_DEBUG("initializing one-time data for %s\n", 1658d913481SNate Lawson device_get_nameunit(dev)); 16673347b07SNate Lawson SYSCTL_ADD_PROC(&sc->sysctl_ctx, 16773347b07SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(parent)), 16873347b07SNate Lawson OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 16973347b07SNate Lawson cpufreq_curr_sysctl, "I", "Current CPU frequency"); 17073347b07SNate Lawson SYSCTL_ADD_PROC(&sc->sysctl_ctx, 17173347b07SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(parent)), 17273347b07SNate Lawson OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0, 17373347b07SNate Lawson cpufreq_levels_sysctl, "A", "CPU frequency levels"); 17473347b07SNate Lawson cf_ev_tag = EVENTHANDLER_REGISTER(cpufreq_changed, cpufreq_evaluate, 17573347b07SNate Lawson NULL, EVENTHANDLER_PRI_ANY); 17673347b07SNate Lawson 17773347b07SNate Lawson return (0); 17873347b07SNate Lawson } 17973347b07SNate Lawson 18073347b07SNate Lawson static int 18173347b07SNate Lawson cpufreq_detach(device_t dev) 18273347b07SNate Lawson { 18373347b07SNate Lawson struct cpufreq_softc *sc; 18473347b07SNate Lawson int numdevs; 18573347b07SNate Lawson 1868d913481SNate Lawson CF_DEBUG("shutdown %s\n", device_get_nameunit(dev)); 18773347b07SNate Lawson sc = device_get_softc(dev); 18873347b07SNate Lawson sysctl_ctx_free(&sc->sysctl_ctx); 18973347b07SNate Lawson 19073347b07SNate Lawson /* Only clean up these resources when the last device is detaching. */ 19173347b07SNate Lawson numdevs = devclass_get_count(cpufreq_dc); 1928d913481SNate Lawson if (numdevs == 1) { 1938d913481SNate Lawson CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev)); 19473347b07SNate Lawson EVENTHANDLER_DEREGISTER(cpufreq_changed, cf_ev_tag); 1958d913481SNate Lawson } 19673347b07SNate Lawson 19773347b07SNate Lawson return (0); 19873347b07SNate Lawson } 19973347b07SNate Lawson 20073347b07SNate Lawson static void 20173347b07SNate Lawson cpufreq_evaluate(void *arg) 20273347b07SNate Lawson { 20373347b07SNate Lawson /* TODO: Re-evaluate when notified of changes to drivers. */ 20473347b07SNate Lawson } 20573347b07SNate Lawson 20673347b07SNate Lawson static int 20773347b07SNate Lawson cf_set_method(device_t dev, const struct cf_level *level, int priority) 20873347b07SNate Lawson { 20973347b07SNate Lawson struct cpufreq_softc *sc; 21073347b07SNate Lawson const struct cf_setting *set; 2110325089dSNate Lawson struct pcpu *pc; 2120325089dSNate Lawson int cpu_id, error, i; 21373347b07SNate Lawson 21473347b07SNate Lawson sc = device_get_softc(dev); 215789f03ceSNate Lawson error = 0; 216789f03ceSNate Lawson set = NULL; 21773347b07SNate Lawson 2180325089dSNate Lawson /* 2190325089dSNate Lawson * Check that the TSC isn't being used as a timecounter. 2200325089dSNate Lawson * If it is, then return EBUSY and refuse to change the 2210325089dSNate Lawson * clock speed. 2220325089dSNate Lawson */ 2230325089dSNate Lawson if (strcmp(timecounter->tc_name, "TSC") == 0) 2240325089dSNate Lawson return (EBUSY); 2250325089dSNate Lawson 2265f0afa04SNate Lawson /* 2275f0afa04SNate Lawson * If the caller didn't specify a level and one is saved, prepare to 2285f0afa04SNate Lawson * restore the saved level. If none has been saved, return an error. 2295f0afa04SNate Lawson * If they did specify one, but the requested level has a lower 2305f0afa04SNate Lawson * priority, don't allow the new level right now. 2315f0afa04SNate Lawson */ 232789f03ceSNate Lawson CF_MTX_LOCK(&sc->lock); 2335f0afa04SNate Lawson if (level == NULL) { 2345f0afa04SNate Lawson if (sc->saved_level.total_set.freq != CPUFREQ_VAL_UNKNOWN) { 2355f0afa04SNate Lawson level = &sc->saved_level; 2365f0afa04SNate Lawson priority = sc->saved_priority; 2378d913481SNate Lawson CF_DEBUG("restoring saved level, freq %d prio %d\n", 2388d913481SNate Lawson level->total_set.freq, priority); 239789f03ceSNate Lawson } else { 2408d913481SNate Lawson CF_DEBUG("NULL level, no saved level\n"); 241789f03ceSNate Lawson error = ENXIO; 242789f03ceSNate Lawson goto out; 243789f03ceSNate Lawson } 244789f03ceSNate Lawson } else if (priority < sc->curr_priority) { 2458d913481SNate Lawson CF_DEBUG("ignoring, curr prio %d less than %d\n", priority, 2468d913481SNate Lawson sc->curr_priority); 247789f03ceSNate Lawson error = EPERM; 248789f03ceSNate Lawson goto out; 249789f03ceSNate Lawson } 2505f0afa04SNate Lawson 251b070969bSNate Lawson /* Reject levels that are below our specified threshold. */ 252da8a77c1SNate Lawson if (level->total_set.freq < cf_lowest_freq) { 2538d913481SNate Lawson CF_DEBUG("rejecting freq %d, less than %d limit\n", 2548d913481SNate Lawson level->total_set.freq, cf_lowest_freq); 255789f03ceSNate Lawson error = EINVAL; 256789f03ceSNate Lawson goto out; 257789f03ceSNate Lawson } 258b070969bSNate Lawson 25973347b07SNate Lawson /* If already at this level, just return. */ 2608d913481SNate Lawson if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq)) { 2618d913481SNate Lawson CF_DEBUG("skipping freq %d, same as current level %d\n", 2628d913481SNate Lawson level->total_set.freq, sc->curr_level.total_set.freq); 2631fea6ce7SHajimu UMEMOTO goto skip; 2648d913481SNate Lawson } 26573347b07SNate Lawson 26673347b07SNate Lawson /* First, set the absolute frequency via its driver. */ 26773347b07SNate Lawson set = &level->abs_set; 26873347b07SNate Lawson if (set->dev) { 26973347b07SNate Lawson if (!device_is_attached(set->dev)) { 27073347b07SNate Lawson error = ENXIO; 27173347b07SNate Lawson goto out; 27273347b07SNate Lawson } 2731196826aSNate Lawson 2741196826aSNate Lawson /* Bind to the target CPU before switching, if necessary. */ 2751196826aSNate Lawson cpu_id = PCPU_GET(cpuid); 2761196826aSNate Lawson pc = cpu_get_pcpu(set->dev); 2771196826aSNate Lawson if (cpu_id != pc->pc_cpuid) { 2781196826aSNate Lawson mtx_lock_spin(&sched_lock); 2791196826aSNate Lawson sched_bind(curthread, pc->pc_cpuid); 2801196826aSNate Lawson mtx_unlock_spin(&sched_lock); 2811196826aSNate Lawson } 2828d913481SNate Lawson CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq, 2838d913481SNate Lawson device_get_nameunit(set->dev), PCPU_GET(cpuid)); 28473347b07SNate Lawson error = CPUFREQ_DRV_SET(set->dev, set); 2851196826aSNate Lawson if (cpu_id != pc->pc_cpuid) { 2861196826aSNate Lawson mtx_lock_spin(&sched_lock); 2871196826aSNate Lawson sched_unbind(curthread); 2881196826aSNate Lawson mtx_unlock_spin(&sched_lock); 2891196826aSNate Lawson } 29073347b07SNate Lawson if (error) { 29173347b07SNate Lawson goto out; 29273347b07SNate Lawson } 29373347b07SNate Lawson } 29473347b07SNate Lawson 29588c9b54cSNate Lawson /* Next, set any/all relative frequencies via their drivers. */ 29688c9b54cSNate Lawson for (i = 0; i < level->rel_count; i++) { 29788c9b54cSNate Lawson set = &level->rel_set[i]; 29888c9b54cSNate Lawson if (!device_is_attached(set->dev)) { 29988c9b54cSNate Lawson error = ENXIO; 30088c9b54cSNate Lawson goto out; 30188c9b54cSNate Lawson } 3021196826aSNate Lawson 3031196826aSNate Lawson /* Bind to the target CPU before switching, if necessary. */ 3041196826aSNate Lawson cpu_id = PCPU_GET(cpuid); 3051196826aSNate Lawson pc = cpu_get_pcpu(set->dev); 3061196826aSNate Lawson if (cpu_id != pc->pc_cpuid) { 3071196826aSNate Lawson mtx_lock_spin(&sched_lock); 3081196826aSNate Lawson sched_bind(curthread, pc->pc_cpuid); 3091196826aSNate Lawson mtx_unlock_spin(&sched_lock); 3101196826aSNate Lawson } 3118d913481SNate Lawson CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq, 3128d913481SNate Lawson device_get_nameunit(set->dev), PCPU_GET(cpuid)); 31388c9b54cSNate Lawson error = CPUFREQ_DRV_SET(set->dev, set); 3141196826aSNate Lawson if (cpu_id != pc->pc_cpuid) { 3151196826aSNate Lawson mtx_lock_spin(&sched_lock); 3161196826aSNate Lawson sched_unbind(curthread); 3171196826aSNate Lawson mtx_unlock_spin(&sched_lock); 3181196826aSNate Lawson } 31988c9b54cSNate Lawson if (error) { 32088c9b54cSNate Lawson /* XXX Back out any successful setting? */ 32188c9b54cSNate Lawson goto out; 32288c9b54cSNate Lawson } 32388c9b54cSNate Lawson } 32473347b07SNate Lawson 3251fea6ce7SHajimu UMEMOTO skip: 3265f0afa04SNate Lawson /* If we were restoring a saved state, reset it to "unused". */ 3275f0afa04SNate Lawson if (level == &sc->saved_level) { 3288d913481SNate Lawson CF_DEBUG("resetting saved level\n"); 3295f0afa04SNate Lawson sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN; 3305f0afa04SNate Lawson sc->saved_priority = 0; 3315f0afa04SNate Lawson } 3325f0afa04SNate Lawson 3335f0afa04SNate Lawson /* 3345f0afa04SNate Lawson * Before recording the current level, check if we're going to a 3355f0afa04SNate Lawson * higher priority and have not saved a level yet. If so, save the 3365f0afa04SNate Lawson * previous level and priority. 3375f0afa04SNate Lawson */ 3385f0afa04SNate Lawson if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN && 3395f0afa04SNate Lawson sc->saved_level.total_set.freq == CPUFREQ_VAL_UNKNOWN && 340961f7f91SHajimu UMEMOTO priority > CPUFREQ_PRIO_USER && priority > sc->curr_priority) { 3418d913481SNate Lawson CF_DEBUG("saving level, freq %d prio %d\n", 3428d913481SNate Lawson sc->curr_level.total_set.freq, sc->curr_priority); 3435f0afa04SNate Lawson sc->saved_level = sc->curr_level; 3445f0afa04SNate Lawson sc->saved_priority = sc->curr_priority; 3455f0afa04SNate Lawson } 34673347b07SNate Lawson sc->curr_level = *level; 3475f0afa04SNate Lawson sc->curr_priority = priority; 34873347b07SNate Lawson 34973347b07SNate Lawson out: 350789f03ceSNate Lawson CF_MTX_UNLOCK(&sc->lock); 351789f03ceSNate Lawson if (error && set) 35273347b07SNate Lawson device_printf(set->dev, "set freq failed, err %d\n", error); 35373347b07SNate Lawson return (error); 35473347b07SNate Lawson } 35573347b07SNate Lawson 35673347b07SNate Lawson static int 35773347b07SNate Lawson cf_get_method(device_t dev, struct cf_level *level) 35873347b07SNate Lawson { 35973347b07SNate Lawson struct cpufreq_softc *sc; 36073347b07SNate Lawson struct cf_level *levels; 36173347b07SNate Lawson struct cf_setting *curr_set, set; 36273347b07SNate Lawson struct pcpu *pc; 36373347b07SNate Lawson device_t *devs; 36473347b07SNate Lawson int count, error, i, numdevs; 36573347b07SNate Lawson uint64_t rate; 36673347b07SNate Lawson 36773347b07SNate Lawson sc = device_get_softc(dev); 368789f03ceSNate Lawson error = 0; 36973347b07SNate Lawson levels = NULL; 37073347b07SNate Lawson 37173347b07SNate Lawson /* If we already know the current frequency, we're done. */ 372789f03ceSNate Lawson CF_MTX_LOCK(&sc->lock); 373789f03ceSNate Lawson curr_set = &sc->curr_level.total_set; 3748d913481SNate Lawson if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) { 3758d913481SNate Lawson CF_DEBUG("get returning known freq %d\n", curr_set->freq); 37673347b07SNate Lawson goto out; 3778d913481SNate Lawson } 378789f03ceSNate Lawson CF_MTX_UNLOCK(&sc->lock); 37973347b07SNate Lawson 38073347b07SNate Lawson /* 38173347b07SNate Lawson * We need to figure out the current level. Loop through every 38273347b07SNate Lawson * driver, getting the current setting. Then, attempt to get a best 38373347b07SNate Lawson * match of settings against each level. 38473347b07SNate Lawson */ 38573347b07SNate Lawson count = CF_MAX_LEVELS; 38673347b07SNate Lawson levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT); 38773347b07SNate Lawson if (levels == NULL) 38873347b07SNate Lawson return (ENOMEM); 38973347b07SNate Lawson error = CPUFREQ_LEVELS(sc->dev, levels, &count); 390d269386aSNate Lawson if (error) { 391d269386aSNate Lawson if (error == E2BIG) 392d269386aSNate Lawson printf("cpufreq: need to increase CF_MAX_LEVELS\n"); 393789f03ceSNate Lawson free(levels, M_TEMP); 394789f03ceSNate Lawson return (error); 395d269386aSNate Lawson } 39673347b07SNate Lawson error = device_get_children(device_get_parent(dev), &devs, &numdevs); 397789f03ceSNate Lawson if (error) { 398789f03ceSNate Lawson free(levels, M_TEMP); 399789f03ceSNate Lawson return (error); 400789f03ceSNate Lawson } 401789f03ceSNate Lawson 402789f03ceSNate Lawson /* 403789f03ceSNate Lawson * Reacquire the lock and search for the given level. 404789f03ceSNate Lawson * 405789f03ceSNate Lawson * XXX Note: this is not quite right since we really need to go 406789f03ceSNate Lawson * through each level and compare both absolute and relative 407789f03ceSNate Lawson * settings for each driver in the system before making a match. 408789f03ceSNate Lawson * The estimation code below catches this case though. 409789f03ceSNate Lawson */ 410789f03ceSNate Lawson CF_MTX_LOCK(&sc->lock); 41173347b07SNate Lawson for (i = 0; i < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; i++) { 41273347b07SNate Lawson if (!device_is_attached(devs[i])) 41373347b07SNate Lawson continue; 41473347b07SNate Lawson error = CPUFREQ_DRV_GET(devs[i], &set); 41573347b07SNate Lawson if (error) 41673347b07SNate Lawson continue; 41773347b07SNate Lawson for (i = 0; i < count; i++) { 41888c9b54cSNate Lawson if (CPUFREQ_CMP(set.freq, levels[i].total_set.freq)) { 41973347b07SNate Lawson sc->curr_level = levels[i]; 42073347b07SNate Lawson break; 42173347b07SNate Lawson } 42273347b07SNate Lawson } 42373347b07SNate Lawson } 42473347b07SNate Lawson free(devs, M_TEMP); 4258d913481SNate Lawson if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) { 4268d913481SNate Lawson CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq); 42773347b07SNate Lawson goto out; 4288d913481SNate Lawson } 42973347b07SNate Lawson 43073347b07SNate Lawson /* 43173347b07SNate Lawson * We couldn't find an exact match, so attempt to estimate and then 43273347b07SNate Lawson * match against a level. 43373347b07SNate Lawson */ 43473347b07SNate Lawson pc = cpu_get_pcpu(dev); 43573347b07SNate Lawson if (pc == NULL) { 43673347b07SNate Lawson error = ENXIO; 43773347b07SNate Lawson goto out; 43873347b07SNate Lawson } 43973347b07SNate Lawson cpu_est_clockrate(pc->pc_cpuid, &rate); 44073347b07SNate Lawson rate /= 1000000; 44173347b07SNate Lawson for (i = 0; i < count; i++) { 44273347b07SNate Lawson if (CPUFREQ_CMP(rate, levels[i].total_set.freq)) { 44373347b07SNate Lawson sc->curr_level = levels[i]; 4448d913481SNate Lawson CF_DEBUG("get estimated freq %d\n", curr_set->freq); 44573347b07SNate Lawson break; 44673347b07SNate Lawson } 44773347b07SNate Lawson } 44873347b07SNate Lawson 44973347b07SNate Lawson out: 450789f03ceSNate Lawson if (error == 0) 451789f03ceSNate Lawson *level = sc->curr_level; 452789f03ceSNate Lawson 453789f03ceSNate Lawson CF_MTX_UNLOCK(&sc->lock); 45473347b07SNate Lawson if (levels) 45573347b07SNate Lawson free(levels, M_TEMP); 456789f03ceSNate Lawson return (error); 45773347b07SNate Lawson } 45873347b07SNate Lawson 45973347b07SNate Lawson static int 46073347b07SNate Lawson cf_levels_method(device_t dev, struct cf_level *levels, int *count) 46173347b07SNate Lawson { 46288c9b54cSNate Lawson struct cf_setting_array *set_arr; 46373347b07SNate Lawson struct cf_setting_lst rel_sets; 46473347b07SNate Lawson struct cpufreq_softc *sc; 46573347b07SNate Lawson struct cf_level *lev; 46673347b07SNate Lawson struct cf_setting *sets; 46773347b07SNate Lawson struct pcpu *pc; 46873347b07SNate Lawson device_t *devs; 46988c9b54cSNate Lawson int error, i, numdevs, set_count, type; 47073347b07SNate Lawson uint64_t rate; 47173347b07SNate Lawson 47273347b07SNate Lawson if (levels == NULL || count == NULL) 47373347b07SNate Lawson return (EINVAL); 47473347b07SNate Lawson 47573347b07SNate Lawson TAILQ_INIT(&rel_sets); 47673347b07SNate Lawson sc = device_get_softc(dev); 47773347b07SNate Lawson error = device_get_children(device_get_parent(dev), &devs, &numdevs); 47873347b07SNate Lawson if (error) 47973347b07SNate Lawson return (error); 48073347b07SNate Lawson sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT); 48173347b07SNate Lawson if (sets == NULL) { 48273347b07SNate Lawson free(devs, M_TEMP); 48373347b07SNate Lawson return (ENOMEM); 48473347b07SNate Lawson } 48573347b07SNate Lawson 48673347b07SNate Lawson /* Get settings from all cpufreq drivers. */ 487789f03ceSNate Lawson CF_MTX_LOCK(&sc->lock); 48873347b07SNate Lawson for (i = 0; i < numdevs; i++) { 489e22cd41cSNate Lawson /* Skip devices that aren't ready. */ 49073347b07SNate Lawson if (!device_is_attached(devs[i])) 49173347b07SNate Lawson continue; 492e22cd41cSNate Lawson 493e22cd41cSNate Lawson /* 494e22cd41cSNate Lawson * Get settings, skipping drivers that offer no settings or 495e22cd41cSNate Lawson * provide settings for informational purposes only. 496e22cd41cSNate Lawson */ 497e94a0c1aSNate Lawson error = CPUFREQ_DRV_TYPE(devs[i], &type); 4988d913481SNate Lawson if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) { 4998d913481SNate Lawson if (error == 0) { 5008d913481SNate Lawson CF_DEBUG("skipping info-only driver %s\n", 5018d913481SNate Lawson device_get_nameunit(devs[i])); 5028d913481SNate Lawson } 503e94a0c1aSNate Lawson continue; 5048d913481SNate Lawson } 50573347b07SNate Lawson set_count = MAX_SETTINGS; 506e94a0c1aSNate Lawson error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count); 507e94a0c1aSNate Lawson if (error || set_count == 0) 50873347b07SNate Lawson continue; 50973347b07SNate Lawson 510e22cd41cSNate Lawson /* Add the settings to our absolute/relative lists. */ 5110325089dSNate Lawson switch (type & CPUFREQ_TYPE_MASK) { 51288c9b54cSNate Lawson case CPUFREQ_TYPE_ABSOLUTE: 51388c9b54cSNate Lawson error = cpufreq_insert_abs(sc, sets, set_count); 51488c9b54cSNate Lawson break; 51588c9b54cSNate Lawson case CPUFREQ_TYPE_RELATIVE: 5168d913481SNate Lawson CF_DEBUG("adding %d relative settings\n", set_count); 51788c9b54cSNate Lawson set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT); 51888c9b54cSNate Lawson if (set_arr == NULL) { 51988c9b54cSNate Lawson error = ENOMEM; 52088c9b54cSNate Lawson goto out; 52188c9b54cSNate Lawson } 52288c9b54cSNate Lawson bcopy(sets, set_arr->sets, set_count * sizeof(*sets)); 52388c9b54cSNate Lawson set_arr->count = set_count; 52488c9b54cSNate Lawson TAILQ_INSERT_TAIL(&rel_sets, set_arr, link); 52588c9b54cSNate Lawson break; 52688c9b54cSNate Lawson default: 52788c9b54cSNate Lawson error = EINVAL; 52888c9b54cSNate Lawson } 52988c9b54cSNate Lawson if (error) 53073347b07SNate Lawson goto out; 53173347b07SNate Lawson } 53273347b07SNate Lawson 53367c8649fSNate Lawson /* 53467c8649fSNate Lawson * If there are no absolute levels, create a fake one at 100%. We 53567c8649fSNate Lawson * then cache the clockrate for later use as our base frequency. 53667c8649fSNate Lawson * 53767c8649fSNate Lawson * XXX This assumes that the first time through, if we only have 53867c8649fSNate Lawson * relative drivers, the CPU is currently running at 100%. 53967c8649fSNate Lawson */ 54073347b07SNate Lawson if (TAILQ_EMPTY(&sc->all_levels)) { 54167c8649fSNate Lawson if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) { 54273347b07SNate Lawson pc = cpu_get_pcpu(dev); 54373347b07SNate Lawson cpu_est_clockrate(pc->pc_cpuid, &rate); 54467c8649fSNate Lawson sc->max_mhz = rate / 1000000; 54567c8649fSNate Lawson } 54667c8649fSNate Lawson memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets)); 54767c8649fSNate Lawson sets[0].freq = sc->max_mhz; 54867c8649fSNate Lawson sets[0].dev = NULL; 54988c9b54cSNate Lawson error = cpufreq_insert_abs(sc, sets, 1); 55073347b07SNate Lawson if (error) 55173347b07SNate Lawson goto out; 55273347b07SNate Lawson } 55373347b07SNate Lawson 55488c9b54cSNate Lawson /* Create a combined list of absolute + relative levels. */ 55588c9b54cSNate Lawson TAILQ_FOREACH(set_arr, &rel_sets, link) 55688c9b54cSNate Lawson cpufreq_expand_set(sc, set_arr); 55788c9b54cSNate Lawson 55888c9b54cSNate Lawson /* If the caller doesn't have enough space, return the actual count. */ 55988c9b54cSNate Lawson if (sc->all_count > *count) { 56088c9b54cSNate Lawson *count = sc->all_count; 56188c9b54cSNate Lawson error = E2BIG; 56288c9b54cSNate Lawson goto out; 56388c9b54cSNate Lawson } 56488c9b54cSNate Lawson 56588c9b54cSNate Lawson /* Finally, output the list of levels. */ 56673347b07SNate Lawson i = 0; 56773347b07SNate Lawson TAILQ_FOREACH(lev, &sc->all_levels, link) { 568b070969bSNate Lawson /* Skip levels that have a frequency that is too low. */ 569da8a77c1SNate Lawson if (lev->total_set.freq < cf_lowest_freq) { 570b070969bSNate Lawson sc->all_count--; 571b070969bSNate Lawson continue; 572b070969bSNate Lawson } 573b070969bSNate Lawson 57473347b07SNate Lawson levels[i] = *lev; 57573347b07SNate Lawson i++; 57673347b07SNate Lawson } 57788c9b54cSNate Lawson *count = sc->all_count; 57873347b07SNate Lawson error = 0; 57973347b07SNate Lawson 58073347b07SNate Lawson out: 58173347b07SNate Lawson /* Clear all levels since we regenerate them each time. */ 58273347b07SNate Lawson while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) { 58373347b07SNate Lawson TAILQ_REMOVE(&sc->all_levels, lev, link); 58473347b07SNate Lawson free(lev, M_TEMP); 58573347b07SNate Lawson } 586789f03ceSNate Lawson sc->all_count = 0; 587789f03ceSNate Lawson 588789f03ceSNate Lawson CF_MTX_UNLOCK(&sc->lock); 58988c9b54cSNate Lawson while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) { 59088c9b54cSNate Lawson TAILQ_REMOVE(&rel_sets, set_arr, link); 59188c9b54cSNate Lawson free(set_arr, M_TEMP); 59288c9b54cSNate Lawson } 59373347b07SNate Lawson free(devs, M_TEMP); 59473347b07SNate Lawson free(sets, M_TEMP); 59573347b07SNate Lawson return (error); 59673347b07SNate Lawson } 59773347b07SNate Lawson 59873347b07SNate Lawson /* 59973347b07SNate Lawson * Create levels for an array of absolute settings and insert them in 60073347b07SNate Lawson * sorted order in the specified list. 60173347b07SNate Lawson */ 60273347b07SNate Lawson static int 60388c9b54cSNate Lawson cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets, 60473347b07SNate Lawson int count) 60573347b07SNate Lawson { 60688c9b54cSNate Lawson struct cf_level_lst *list; 60773347b07SNate Lawson struct cf_level *level, *search; 60873347b07SNate Lawson int i; 60973347b07SNate Lawson 610789f03ceSNate Lawson CF_MTX_ASSERT(&sc->lock); 611789f03ceSNate Lawson 61288c9b54cSNate Lawson list = &sc->all_levels; 61373347b07SNate Lawson for (i = 0; i < count; i++) { 61473347b07SNate Lawson level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO); 61573347b07SNate Lawson if (level == NULL) 61673347b07SNate Lawson return (ENOMEM); 61773347b07SNate Lawson level->abs_set = sets[i]; 61888c9b54cSNate Lawson level->total_set = sets[i]; 61988c9b54cSNate Lawson level->total_set.dev = NULL; 62088c9b54cSNate Lawson sc->all_count++; 62173347b07SNate Lawson 62273347b07SNate Lawson if (TAILQ_EMPTY(list)) { 6238d913481SNate Lawson CF_DEBUG("adding abs setting %d at head\n", 6248d913481SNate Lawson sets[i].freq); 62573347b07SNate Lawson TAILQ_INSERT_HEAD(list, level, link); 62673347b07SNate Lawson continue; 62773347b07SNate Lawson } 62873347b07SNate Lawson 62973347b07SNate Lawson TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) { 63088c9b54cSNate Lawson if (sets[i].freq <= search->total_set.freq) { 6318d913481SNate Lawson CF_DEBUG("adding abs setting %d after %d\n", 6328d913481SNate Lawson sets[i].freq, search->total_set.freq); 63373347b07SNate Lawson TAILQ_INSERT_AFTER(list, search, level, link); 63473347b07SNate Lawson break; 63573347b07SNate Lawson } 63673347b07SNate Lawson } 63773347b07SNate Lawson } 63873347b07SNate Lawson return (0); 63973347b07SNate Lawson } 64073347b07SNate Lawson 64188c9b54cSNate Lawson /* 64288c9b54cSNate Lawson * Expand a group of relative settings, creating derived levels from them. 64388c9b54cSNate Lawson */ 64488c9b54cSNate Lawson static int 64588c9b54cSNate Lawson cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr) 64688c9b54cSNate Lawson { 64788c9b54cSNate Lawson struct cf_level *fill, *search; 64888c9b54cSNate Lawson struct cf_setting *set; 64988c9b54cSNate Lawson int i; 65088c9b54cSNate Lawson 651789f03ceSNate Lawson CF_MTX_ASSERT(&sc->lock); 652789f03ceSNate Lawson 6535308b2a6SNate Lawson /* 6545308b2a6SNate Lawson * Walk the set of all existing levels in reverse. This is so we 6555308b2a6SNate Lawson * create derived states from the lowest absolute settings first 6565308b2a6SNate Lawson * and discard duplicates created from higher absolute settings. 6575308b2a6SNate Lawson * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is 6585308b2a6SNate Lawson * preferable to 200 Mhz + 25% because absolute settings are more 6595308b2a6SNate Lawson * efficient since they often change the voltage as well. 6605308b2a6SNate Lawson */ 6615308b2a6SNate Lawson TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) { 66288c9b54cSNate Lawson /* Add each setting to the level, duplicating if necessary. */ 66388c9b54cSNate Lawson for (i = 0; i < set_arr->count; i++) { 66488c9b54cSNate Lawson set = &set_arr->sets[i]; 66588c9b54cSNate Lawson 66688c9b54cSNate Lawson /* 66788c9b54cSNate Lawson * If this setting is less than 100%, split the level 66888c9b54cSNate Lawson * into two and add this setting to the new level. 66988c9b54cSNate Lawson */ 67088c9b54cSNate Lawson fill = search; 6715308b2a6SNate Lawson if (set->freq < 10000) { 67288c9b54cSNate Lawson fill = cpufreq_dup_set(sc, search, set); 67388c9b54cSNate Lawson 67488c9b54cSNate Lawson /* 6755308b2a6SNate Lawson * The new level was a duplicate of an existing 6765308b2a6SNate Lawson * level or its absolute setting is too high 6775308b2a6SNate Lawson * so we freed it. For example, we discard a 6785308b2a6SNate Lawson * derived level of 1000 MHz/25% if a level 6795308b2a6SNate Lawson * of 500 MHz/100% already exists. 68088c9b54cSNate Lawson */ 68188c9b54cSNate Lawson if (fill == NULL) 6825308b2a6SNate Lawson break; 6835308b2a6SNate Lawson } 68488c9b54cSNate Lawson 68588c9b54cSNate Lawson /* Add this setting to the existing or new level. */ 68688c9b54cSNate Lawson KASSERT(fill->rel_count < MAX_SETTINGS, 68788c9b54cSNate Lawson ("cpufreq: too many relative drivers (%d)", 68888c9b54cSNate Lawson MAX_SETTINGS)); 68988c9b54cSNate Lawson fill->rel_set[fill->rel_count] = *set; 69088c9b54cSNate Lawson fill->rel_count++; 6918d913481SNate Lawson CF_DEBUG( 6928d913481SNate Lawson "expand set added rel setting %d%% to %d level\n", 6938d913481SNate Lawson set->freq / 100, fill->total_set.freq); 69488c9b54cSNate Lawson } 69588c9b54cSNate Lawson } 69688c9b54cSNate Lawson 69788c9b54cSNate Lawson return (0); 69888c9b54cSNate Lawson } 69988c9b54cSNate Lawson 70088c9b54cSNate Lawson static struct cf_level * 70188c9b54cSNate Lawson cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup, 70288c9b54cSNate Lawson struct cf_setting *set) 70388c9b54cSNate Lawson { 70488c9b54cSNate Lawson struct cf_level_lst *list; 70588c9b54cSNate Lawson struct cf_level *fill, *itr; 70688c9b54cSNate Lawson struct cf_setting *fill_set, *itr_set; 70788c9b54cSNate Lawson int i; 70888c9b54cSNate Lawson 709789f03ceSNate Lawson CF_MTX_ASSERT(&sc->lock); 710789f03ceSNate Lawson 71188c9b54cSNate Lawson /* 71288c9b54cSNate Lawson * Create a new level, copy it from the old one, and update the 71388c9b54cSNate Lawson * total frequency and power by the percentage specified in the 71488c9b54cSNate Lawson * relative setting. 71588c9b54cSNate Lawson */ 71688c9b54cSNate Lawson fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT); 71788c9b54cSNate Lawson if (fill == NULL) 71888c9b54cSNate Lawson return (NULL); 71988c9b54cSNate Lawson *fill = *dup; 72088c9b54cSNate Lawson fill_set = &fill->total_set; 72188c9b54cSNate Lawson fill_set->freq = 72288c9b54cSNate Lawson ((uint64_t)fill_set->freq * set->freq) / 10000; 72388c9b54cSNate Lawson if (fill_set->power != CPUFREQ_VAL_UNKNOWN) { 72488c9b54cSNate Lawson fill_set->power = ((uint64_t)fill_set->power * set->freq) 72588c9b54cSNate Lawson / 10000; 72688c9b54cSNate Lawson } 72788c9b54cSNate Lawson if (set->lat != CPUFREQ_VAL_UNKNOWN) { 72888c9b54cSNate Lawson if (fill_set->lat != CPUFREQ_VAL_UNKNOWN) 72988c9b54cSNate Lawson fill_set->lat += set->lat; 73088c9b54cSNate Lawson else 73188c9b54cSNate Lawson fill_set->lat = set->lat; 73288c9b54cSNate Lawson } 7338d913481SNate Lawson CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq); 73488c9b54cSNate Lawson 73588c9b54cSNate Lawson /* 73688c9b54cSNate Lawson * If we copied an old level that we already modified (say, at 100%), 73788c9b54cSNate Lawson * we need to remove that setting before adding this one. Since we 73888c9b54cSNate Lawson * process each setting array in order, we know any settings for this 73988c9b54cSNate Lawson * driver will be found at the end. 74088c9b54cSNate Lawson */ 74188c9b54cSNate Lawson for (i = fill->rel_count; i != 0; i--) { 74288c9b54cSNate Lawson if (fill->rel_set[i - 1].dev != set->dev) 74388c9b54cSNate Lawson break; 7448d913481SNate Lawson CF_DEBUG("removed last relative driver: %s\n", 7458d913481SNate Lawson device_get_nameunit(set->dev)); 74688c9b54cSNate Lawson fill->rel_count--; 74788c9b54cSNate Lawson } 74888c9b54cSNate Lawson 74988c9b54cSNate Lawson /* 7505308b2a6SNate Lawson * Insert the new level in sorted order. If it is a duplicate of an 7515308b2a6SNate Lawson * existing level (1) or has an absolute setting higher than the 7525308b2a6SNate Lawson * existing level (2), do not add it. We can do this since any such 7535308b2a6SNate Lawson * level is guaranteed use less power. For example (1), a level with 7545308b2a6SNate Lawson * one absolute setting of 800 Mhz uses less power than one composed 7555308b2a6SNate Lawson * of an absolute setting of 1600 Mhz and a relative setting at 50%. 7565308b2a6SNate Lawson * Also for example (2), a level of 800 Mhz/75% is preferable to 7575308b2a6SNate Lawson * 1600 Mhz/25% even though the latter has a lower total frequency. 75888c9b54cSNate Lawson */ 75988c9b54cSNate Lawson list = &sc->all_levels; 7605308b2a6SNate Lawson KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set")); 76188c9b54cSNate Lawson TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) { 76288c9b54cSNate Lawson itr_set = &itr->total_set; 7635308b2a6SNate Lawson if (fill_set->freq < itr_set->freq && 7645308b2a6SNate Lawson !CPUFREQ_CMP(fill_set->freq, itr_set->freq) && 7655308b2a6SNate Lawson fill->abs_set.freq <= itr->abs_set.freq) { 7668d913481SNate Lawson CF_DEBUG( 7678d913481SNate Lawson "dup done, inserting new level %d after %d\n", 7688d913481SNate Lawson fill_set->freq, itr_set->freq); 76988c9b54cSNate Lawson TAILQ_INSERT_AFTER(list, itr, fill, link); 77088c9b54cSNate Lawson sc->all_count++; 77188c9b54cSNate Lawson break; 77288c9b54cSNate Lawson } 77388c9b54cSNate Lawson } 7745308b2a6SNate Lawson 7755308b2a6SNate Lawson /* We didn't find a good place for this new level so free it. */ 7765308b2a6SNate Lawson if (itr == NULL) { 7775308b2a6SNate Lawson CF_DEBUG("dup set freeing new level %d (not optimal)\n", 7785308b2a6SNate Lawson fill_set->freq); 7795308b2a6SNate Lawson free(fill, M_TEMP); 7805308b2a6SNate Lawson fill = NULL; 78188c9b54cSNate Lawson } 78288c9b54cSNate Lawson 78388c9b54cSNate Lawson return (fill); 78488c9b54cSNate Lawson } 78588c9b54cSNate Lawson 78673347b07SNate Lawson static int 78773347b07SNate Lawson cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS) 78873347b07SNate Lawson { 78973347b07SNate Lawson struct cpufreq_softc *sc; 79073347b07SNate Lawson struct cf_level *levels; 7910325089dSNate Lawson int count, devcount, error, freq, i, n; 7920325089dSNate Lawson device_t *devs; 79373347b07SNate Lawson 7940325089dSNate Lawson devs = NULL; 79573347b07SNate Lawson sc = oidp->oid_arg1; 7960325089dSNate Lawson levels = malloc(CF_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT); 79773347b07SNate Lawson if (levels == NULL) 79873347b07SNate Lawson return (ENOMEM); 79973347b07SNate Lawson 80073347b07SNate Lawson error = CPUFREQ_GET(sc->dev, &levels[0]); 80173347b07SNate Lawson if (error) 80273347b07SNate Lawson goto out; 80373347b07SNate Lawson freq = levels[0].total_set.freq; 80473347b07SNate Lawson error = sysctl_handle_int(oidp, &freq, 0, req); 80573347b07SNate Lawson if (error != 0 || req->newptr == NULL) 80673347b07SNate Lawson goto out; 80773347b07SNate Lawson 8080325089dSNate Lawson /* 8090325089dSNate Lawson * While we only call cpufreq_get() on one device (assuming all 8100325089dSNate Lawson * CPUs have equal levels), we call cpufreq_set() on all CPUs. 8110325089dSNate Lawson * This is needed for some MP systems. 8120325089dSNate Lawson */ 8130325089dSNate Lawson error = devclass_get_devices(cpufreq_dc, &devs, &devcount); 81473347b07SNate Lawson if (error) 81573347b07SNate Lawson goto out; 8160325089dSNate Lawson for (n = 0; n < devcount; n++) { 8170325089dSNate Lawson count = CF_MAX_LEVELS; 8180325089dSNate Lawson error = CPUFREQ_LEVELS(devs[n], levels, &count); 819d269386aSNate Lawson if (error) { 820d269386aSNate Lawson if (error == E2BIG) 821d269386aSNate Lawson printf( 822d269386aSNate Lawson "cpufreq: need to increase CF_MAX_LEVELS\n"); 8230325089dSNate Lawson break; 824d269386aSNate Lawson } 82573347b07SNate Lawson for (i = 0; i < count; i++) { 82673347b07SNate Lawson if (CPUFREQ_CMP(levels[i].total_set.freq, freq)) { 8270325089dSNate Lawson error = CPUFREQ_SET(devs[n], &levels[i], 82873347b07SNate Lawson CPUFREQ_PRIO_USER); 82973347b07SNate Lawson break; 83073347b07SNate Lawson } 83173347b07SNate Lawson } 8320325089dSNate Lawson if (i == count) { 83373347b07SNate Lawson error = EINVAL; 8340325089dSNate Lawson break; 8350325089dSNate Lawson } 8360325089dSNate Lawson } 83773347b07SNate Lawson 83873347b07SNate Lawson out: 8390325089dSNate Lawson if (devs) 8400325089dSNate Lawson free(devs, M_TEMP); 84173347b07SNate Lawson if (levels) 84273347b07SNate Lawson free(levels, M_TEMP); 84373347b07SNate Lawson return (error); 84473347b07SNate Lawson } 84573347b07SNate Lawson 84673347b07SNate Lawson static int 84773347b07SNate Lawson cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS) 84873347b07SNate Lawson { 84973347b07SNate Lawson struct cpufreq_softc *sc; 85073347b07SNate Lawson struct cf_level *levels; 85173347b07SNate Lawson struct cf_setting *set; 85273347b07SNate Lawson struct sbuf sb; 85373347b07SNate Lawson int count, error, i; 85473347b07SNate Lawson 85573347b07SNate Lawson sc = oidp->oid_arg1; 85673347b07SNate Lawson sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND); 85773347b07SNate Lawson 85873347b07SNate Lawson /* Get settings from the device and generate the output string. */ 85973347b07SNate Lawson count = CF_MAX_LEVELS; 86073347b07SNate Lawson levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT); 86173347b07SNate Lawson if (levels == NULL) 86273347b07SNate Lawson return (ENOMEM); 86373347b07SNate Lawson error = CPUFREQ_LEVELS(sc->dev, levels, &count); 864d269386aSNate Lawson if (error) { 865d269386aSNate Lawson if (error == E2BIG) 866d269386aSNate Lawson printf("cpufreq: need to increase CF_MAX_LEVELS\n"); 86773347b07SNate Lawson goto out; 868d269386aSNate Lawson } 86973347b07SNate Lawson if (count) { 87073347b07SNate Lawson for (i = 0; i < count; i++) { 87173347b07SNate Lawson set = &levels[i].total_set; 87273347b07SNate Lawson sbuf_printf(&sb, "%d/%d ", set->freq, set->power); 87373347b07SNate Lawson } 87473347b07SNate Lawson } else 87573347b07SNate Lawson sbuf_cpy(&sb, "0"); 87673347b07SNate Lawson sbuf_trim(&sb); 87773347b07SNate Lawson sbuf_finish(&sb); 87873347b07SNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 87973347b07SNate Lawson 88073347b07SNate Lawson out: 88173347b07SNate Lawson free(levels, M_TEMP); 88273347b07SNate Lawson sbuf_delete(&sb); 88373347b07SNate Lawson return (error); 88473347b07SNate Lawson } 88573347b07SNate Lawson 886e959a70bSNate Lawson static int 887e959a70bSNate Lawson cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS) 888e959a70bSNate Lawson { 889e959a70bSNate Lawson device_t dev; 890e959a70bSNate Lawson struct cf_setting *sets; 891e959a70bSNate Lawson struct sbuf sb; 892e959a70bSNate Lawson int error, i, set_count; 893e959a70bSNate Lawson 894e959a70bSNate Lawson dev = oidp->oid_arg1; 895e959a70bSNate Lawson sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND); 896e959a70bSNate Lawson 897e959a70bSNate Lawson /* Get settings from the device and generate the output string. */ 898e959a70bSNate Lawson set_count = MAX_SETTINGS; 899e959a70bSNate Lawson sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT); 900e959a70bSNate Lawson if (sets == NULL) 901e959a70bSNate Lawson return (ENOMEM); 902e959a70bSNate Lawson error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count); 903e959a70bSNate Lawson if (error) 904e959a70bSNate Lawson goto out; 905e959a70bSNate Lawson if (set_count) { 906e959a70bSNate Lawson for (i = 0; i < set_count; i++) 907e959a70bSNate Lawson sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power); 908e959a70bSNate Lawson } else 909e959a70bSNate Lawson sbuf_cpy(&sb, "0"); 910e959a70bSNate Lawson sbuf_trim(&sb); 911e959a70bSNate Lawson sbuf_finish(&sb); 912e959a70bSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 913e959a70bSNate Lawson 914e959a70bSNate Lawson out: 915e959a70bSNate Lawson free(sets, M_TEMP); 916e959a70bSNate Lawson sbuf_delete(&sb); 917e959a70bSNate Lawson return (error); 918e959a70bSNate Lawson } 919e959a70bSNate Lawson 92073347b07SNate Lawson int 92173347b07SNate Lawson cpufreq_register(device_t dev) 92273347b07SNate Lawson { 92367c8649fSNate Lawson struct cpufreq_softc *sc; 92473347b07SNate Lawson device_t cf_dev, cpu_dev; 92573347b07SNate Lawson 926e959a70bSNate Lawson /* Add a sysctl to get each driver's settings separately. */ 927e959a70bSNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 928e959a70bSNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 929e959a70bSNate Lawson OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0, 930e959a70bSNate Lawson cpufreq_settings_sysctl, "A", "CPU frequency driver settings"); 931e959a70bSNate Lawson 93273347b07SNate Lawson /* 9330325089dSNate Lawson * Add only one cpufreq device to each CPU. Currently, all CPUs 9340325089dSNate Lawson * must offer the same levels and be switched at the same time. 93573347b07SNate Lawson */ 9360325089dSNate Lawson cpu_dev = device_get_parent(dev); 93767c8649fSNate Lawson if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) { 93867c8649fSNate Lawson sc = device_get_softc(cf_dev); 93967c8649fSNate Lawson sc->max_mhz = CPUFREQ_VAL_UNKNOWN; 94073347b07SNate Lawson return (0); 94167c8649fSNate Lawson } 94273347b07SNate Lawson 9430325089dSNate Lawson /* Add the child device and possibly sysctls. */ 9440325089dSNate Lawson cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1); 94573347b07SNate Lawson if (cf_dev == NULL) 94673347b07SNate Lawson return (ENOMEM); 94773347b07SNate Lawson device_quiet(cf_dev); 94873347b07SNate Lawson 94973347b07SNate Lawson return (device_probe_and_attach(cf_dev)); 95073347b07SNate Lawson } 95173347b07SNate Lawson 95273347b07SNate Lawson int 95373347b07SNate Lawson cpufreq_unregister(device_t dev) 95473347b07SNate Lawson { 95573347b07SNate Lawson device_t cf_dev, *devs; 956e94a0c1aSNate Lawson int cfcount, devcount, error, i, type; 95773347b07SNate Lawson 95873347b07SNate Lawson /* 95973347b07SNate Lawson * If this is the last cpufreq child device, remove the control 96073347b07SNate Lawson * device as well. We identify cpufreq children by calling a method 96173347b07SNate Lawson * they support. 96273347b07SNate Lawson */ 96373347b07SNate Lawson error = device_get_children(device_get_parent(dev), &devs, &devcount); 96473347b07SNate Lawson if (error) 96573347b07SNate Lawson return (error); 96667c8649fSNate Lawson cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1); 96771ab130cSNate Lawson if (cf_dev == NULL) { 96871ab130cSNate Lawson device_printf(dev, 96971ab130cSNate Lawson "warning: cpufreq_unregister called with no cpufreq device active\n"); 97071ab130cSNate Lawson return (0); 97171ab130cSNate Lawson } 97273347b07SNate Lawson cfcount = 0; 97373347b07SNate Lawson for (i = 0; i < devcount; i++) { 97473347b07SNate Lawson if (!device_is_attached(devs[i])) 97573347b07SNate Lawson continue; 976e94a0c1aSNate Lawson if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0) 97773347b07SNate Lawson cfcount++; 97873347b07SNate Lawson } 9790325089dSNate Lawson if (cfcount <= 1) 98073347b07SNate Lawson device_delete_child(device_get_parent(cf_dev), cf_dev); 98173347b07SNate Lawson free(devs, M_TEMP); 98273347b07SNate Lawson 98373347b07SNate Lawson return (0); 98473347b07SNate Lawson } 985