xref: /freebsd/sys/kern/kern_cpu.c (revision d288bcc4df65569fda08cc7fa86b78b70aeee4a3)
173347b07SNate Lawson /*-
20d4ac62aSNate Lawson  * Copyright (c) 2004-2007 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>
40a15e947dSNate Lawson #include <sys/sbuf.h>
4173347b07SNate Lawson #include <sys/sched.h>
42a15e947dSNate Lawson #include <sys/smp.h>
4373347b07SNate Lawson #include <sys/sysctl.h>
4473347b07SNate Lawson #include <sys/systm.h>
45789f03ceSNate Lawson #include <sys/sx.h>
460325089dSNate Lawson #include <sys/timetc.h>
470d4ac62aSNate Lawson #include <sys/taskqueue.h>
4873347b07SNate Lawson 
4973347b07SNate Lawson #include "cpufreq_if.h"
5073347b07SNate Lawson 
5173347b07SNate Lawson /*
5273347b07SNate Lawson  * Common CPU frequency glue code.  Drivers for specific hardware can
5373347b07SNate Lawson  * attach this interface to allow users to get/set the CPU frequency.
5473347b07SNate Lawson  */
5573347b07SNate Lawson 
5673347b07SNate Lawson /*
5773347b07SNate Lawson  * Number of levels we can handle.  Levels are synthesized from settings
58d269386aSNate Lawson  * so for M settings and N drivers, there may be M*N levels.
5973347b07SNate Lawson  */
60d269386aSNate Lawson #define CF_MAX_LEVELS	64
6173347b07SNate Lawson 
6256e5a87aSHajimu UMEMOTO struct cf_saved_freq {
6356e5a87aSHajimu UMEMOTO 	struct cf_level			level;
6456e5a87aSHajimu UMEMOTO 	int				priority;
6556e5a87aSHajimu UMEMOTO 	SLIST_ENTRY(cf_saved_freq)	link;
6656e5a87aSHajimu UMEMOTO };
6756e5a87aSHajimu UMEMOTO 
6873347b07SNate Lawson struct cpufreq_softc {
69789f03ceSNate Lawson 	struct sx			lock;
7073347b07SNate Lawson 	struct cf_level			curr_level;
715f0afa04SNate Lawson 	int				curr_priority;
7256e5a87aSHajimu UMEMOTO 	SLIST_HEAD(, cf_saved_freq)	saved_freq;
7373347b07SNate Lawson 	struct cf_level_lst		all_levels;
745f0afa04SNate Lawson 	int				all_count;
7567c8649fSNate Lawson 	int				max_mhz;
7673347b07SNate Lawson 	device_t			dev;
7773347b07SNate Lawson 	struct sysctl_ctx_list		sysctl_ctx;
780d4ac62aSNate Lawson 	struct task			startup_task;
7973347b07SNate Lawson };
8073347b07SNate Lawson 
8173347b07SNate Lawson struct cf_setting_array {
8273347b07SNate Lawson 	struct cf_setting		sets[MAX_SETTINGS];
8373347b07SNate Lawson 	int				count;
8473347b07SNate Lawson 	TAILQ_ENTRY(cf_setting_array)	link;
8573347b07SNate Lawson };
8673347b07SNate Lawson 
8773347b07SNate Lawson TAILQ_HEAD(cf_setting_lst, cf_setting_array);
8873347b07SNate Lawson 
89789f03ceSNate Lawson #define CF_MTX_INIT(x)		sx_init((x), "cpufreq lock")
90789f03ceSNate Lawson #define CF_MTX_LOCK(x)		sx_xlock((x))
91789f03ceSNate Lawson #define CF_MTX_UNLOCK(x)	sx_xunlock((x))
92789f03ceSNate Lawson #define CF_MTX_ASSERT(x)	sx_assert((x), SX_XLOCKED)
93789f03ceSNate Lawson 
948d913481SNate Lawson #define CF_DEBUG(msg...)	do {		\
958d913481SNate Lawson 	if (cf_verbose)				\
968d913481SNate Lawson 		printf("cpufreq: " msg);	\
978d913481SNate Lawson 	} while (0)
988d913481SNate Lawson 
9973347b07SNate Lawson static int	cpufreq_attach(device_t dev);
1000d4ac62aSNate Lawson static void	cpufreq_startup_task(void *ctx, int pending);
10173347b07SNate Lawson static int	cpufreq_detach(device_t dev);
10273347b07SNate Lawson static int	cf_set_method(device_t dev, const struct cf_level *level,
10373347b07SNate Lawson 		    int priority);
10473347b07SNate Lawson static int	cf_get_method(device_t dev, struct cf_level *level);
10573347b07SNate Lawson static int	cf_levels_method(device_t dev, struct cf_level *levels,
10673347b07SNate Lawson 		    int *count);
10788c9b54cSNate Lawson static int	cpufreq_insert_abs(struct cpufreq_softc *sc,
10873347b07SNate Lawson 		    struct cf_setting *sets, int count);
10988c9b54cSNate Lawson static int	cpufreq_expand_set(struct cpufreq_softc *sc,
11088c9b54cSNate Lawson 		    struct cf_setting_array *set_arr);
11188c9b54cSNate Lawson static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
11288c9b54cSNate Lawson 		    struct cf_level *dup, struct cf_setting *set);
11373347b07SNate Lawson static int	cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
11473347b07SNate Lawson static int	cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
115e959a70bSNate Lawson static int	cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
11673347b07SNate Lawson 
11773347b07SNate Lawson static device_method_t cpufreq_methods[] = {
11873347b07SNate Lawson 	DEVMETHOD(device_probe,		bus_generic_probe),
11973347b07SNate Lawson 	DEVMETHOD(device_attach,	cpufreq_attach),
12073347b07SNate Lawson 	DEVMETHOD(device_detach,	cpufreq_detach),
12173347b07SNate Lawson 
12273347b07SNate Lawson         DEVMETHOD(cpufreq_set,		cf_set_method),
12373347b07SNate Lawson         DEVMETHOD(cpufreq_get,		cf_get_method),
12473347b07SNate Lawson         DEVMETHOD(cpufreq_levels,	cf_levels_method),
12573347b07SNate Lawson 	{0, 0}
12673347b07SNate Lawson };
12773347b07SNate Lawson static driver_t cpufreq_driver = {
12873347b07SNate Lawson 	"cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
12973347b07SNate Lawson };
13073347b07SNate Lawson static devclass_t cpufreq_dc;
13173347b07SNate Lawson DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
13273347b07SNate Lawson 
133b070969bSNate Lawson static int		cf_lowest_freq;
1348d913481SNate Lawson static int		cf_verbose;
135b070969bSNate Lawson TUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq);
1368d913481SNate Lawson TUNABLE_INT("debug.cpufreq.verbose", &cf_verbose);
137b070969bSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, "cpufreq debugging");
138b070969bSNate Lawson SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1,
139b070969bSNate Lawson     "Don't provide levels below this frequency.");
1408d913481SNate Lawson SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1,
1418d913481SNate Lawson     "Print verbose debugging messages");
142b070969bSNate Lawson 
14373347b07SNate Lawson static int
14473347b07SNate Lawson cpufreq_attach(device_t dev)
14573347b07SNate Lawson {
14673347b07SNate Lawson 	struct cpufreq_softc *sc;
147d288bcc4SAlexander Motin 	struct pcpu *pc;
14873347b07SNate Lawson 	device_t parent;
149d288bcc4SAlexander Motin 	uint64_t rate;
15073347b07SNate Lawson 	int numdevs;
15173347b07SNate Lawson 
1528d913481SNate Lawson 	CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
15373347b07SNate Lawson 	sc = device_get_softc(dev);
15473347b07SNate Lawson 	parent = device_get_parent(dev);
15573347b07SNate Lawson 	sc->dev = dev;
15673347b07SNate Lawson 	sysctl_ctx_init(&sc->sysctl_ctx);
15773347b07SNate Lawson 	TAILQ_INIT(&sc->all_levels);
158789f03ceSNate Lawson 	CF_MTX_INIT(&sc->lock);
15973347b07SNate Lawson 	sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
16056e5a87aSHajimu UMEMOTO 	SLIST_INIT(&sc->saved_freq);
161d288bcc4SAlexander Motin 	/* Try to get current CPU freq to use it as maximum later if needed */
162d288bcc4SAlexander Motin 	pc = cpu_get_pcpu(dev);
163d288bcc4SAlexander Motin 	if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
164d288bcc4SAlexander Motin 		sc->max_mhz = rate / 1000000;
165d288bcc4SAlexander Motin 	else
16667c8649fSNate Lawson 		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
16773347b07SNate Lawson 
16873347b07SNate Lawson 	/*
16973347b07SNate Lawson 	 * Only initialize one set of sysctls for all CPUs.  In the future,
17073347b07SNate Lawson 	 * if multiple CPUs can have different settings, we can move these
17173347b07SNate Lawson 	 * sysctls to be under every CPU instead of just the first one.
17273347b07SNate Lawson 	 */
17373347b07SNate Lawson 	numdevs = devclass_get_count(cpufreq_dc);
17473347b07SNate Lawson 	if (numdevs > 1)
17573347b07SNate Lawson 		return (0);
17673347b07SNate Lawson 
1778d913481SNate Lawson 	CF_DEBUG("initializing one-time data for %s\n",
1788d913481SNate Lawson 	    device_get_nameunit(dev));
17973347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
18073347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
18173347b07SNate Lawson 	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
18273347b07SNate Lawson 	    cpufreq_curr_sysctl, "I", "Current CPU frequency");
18373347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
18473347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
18573347b07SNate Lawson 	    OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
18673347b07SNate Lawson 	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
1870d4ac62aSNate Lawson 
1880d4ac62aSNate Lawson 	/*
1890d4ac62aSNate Lawson 	 * Queue a one-shot broadcast that levels have changed.
1900d4ac62aSNate Lawson 	 * It will run once the system has completed booting.
1910d4ac62aSNate Lawson 	 */
1920d4ac62aSNate Lawson 	TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
1930d4ac62aSNate Lawson 	taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
19473347b07SNate Lawson 
19573347b07SNate Lawson 	return (0);
19673347b07SNate Lawson }
19773347b07SNate Lawson 
1980d4ac62aSNate Lawson /* Handle any work to be done for all drivers that attached during boot. */
1990d4ac62aSNate Lawson static void
2000d4ac62aSNate Lawson cpufreq_startup_task(void *ctx, int pending)
2010d4ac62aSNate Lawson {
2020d4ac62aSNate Lawson 
2030d4ac62aSNate Lawson 	cpufreq_settings_changed((device_t)ctx);
2040d4ac62aSNate Lawson }
2050d4ac62aSNate Lawson 
20673347b07SNate Lawson static int
20773347b07SNate Lawson cpufreq_detach(device_t dev)
20873347b07SNate Lawson {
20973347b07SNate Lawson 	struct cpufreq_softc *sc;
21056e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq;
21173347b07SNate Lawson 	int numdevs;
21273347b07SNate Lawson 
2138d913481SNate Lawson 	CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
21473347b07SNate Lawson 	sc = device_get_softc(dev);
21573347b07SNate Lawson 	sysctl_ctx_free(&sc->sysctl_ctx);
21673347b07SNate Lawson 
21756e5a87aSHajimu UMEMOTO 	while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
21856e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
21956e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
22056e5a87aSHajimu UMEMOTO 	}
22156e5a87aSHajimu UMEMOTO 
22273347b07SNate Lawson 	/* Only clean up these resources when the last device is detaching. */
22373347b07SNate Lawson 	numdevs = devclass_get_count(cpufreq_dc);
2248d913481SNate Lawson 	if (numdevs == 1) {
2258d913481SNate Lawson 		CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
2268d913481SNate Lawson 	}
22773347b07SNate Lawson 
22873347b07SNate Lawson 	return (0);
22973347b07SNate Lawson }
23073347b07SNate Lawson 
23173347b07SNate Lawson static int
23273347b07SNate Lawson cf_set_method(device_t dev, const struct cf_level *level, int priority)
23373347b07SNate Lawson {
23473347b07SNate Lawson 	struct cpufreq_softc *sc;
23573347b07SNate Lawson 	const struct cf_setting *set;
23656e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq, *curr_freq;
2370325089dSNate Lawson 	struct pcpu *pc;
23862db376aSNate Lawson 	int error, i;
23973347b07SNate Lawson 
24073347b07SNate Lawson 	sc = device_get_softc(dev);
241789f03ceSNate Lawson 	error = 0;
242789f03ceSNate Lawson 	set = NULL;
24356e5a87aSHajimu UMEMOTO 	saved_freq = NULL;
24473347b07SNate Lawson 
2450d4ac62aSNate Lawson 	/* We are going to change levels so notify the pre-change handler. */
2460d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
2470d4ac62aSNate Lawson 	if (error != 0) {
2480d4ac62aSNate Lawson 		EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
2490d4ac62aSNate Lawson 		return (error);
250b4130b8aSMarcus Alves Grando 	}
2510325089dSNate Lawson 
252789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
25356e5a87aSHajimu UMEMOTO 
254a15e947dSNate Lawson #ifdef SMP
255a15e947dSNate Lawson 	/*
256a15e947dSNate Lawson 	 * If still booting and secondary CPUs not started yet, don't allow
257a15e947dSNate Lawson 	 * changing the frequency until they're online.  This is because we
258a15e947dSNate Lawson 	 * can't switch to them using sched_bind() and thus we'd only be
259a15e947dSNate Lawson 	 * switching the main CPU.  XXXTODO: Need to think more about how to
260a15e947dSNate Lawson 	 * handle having different CPUs at different frequencies.
261a15e947dSNate Lawson 	 */
262a15e947dSNate Lawson 	if (mp_ncpus > 1 && !smp_active) {
263a15e947dSNate Lawson 		device_printf(dev, "rejecting change, SMP not started yet\n");
264a15e947dSNate Lawson 		error = ENXIO;
265a15e947dSNate Lawson 		goto out;
266a15e947dSNate Lawson 	}
267a15e947dSNate Lawson #endif /* SMP */
268a15e947dSNate Lawson 
26956e5a87aSHajimu UMEMOTO 	/*
27056e5a87aSHajimu UMEMOTO 	 * If the requested level has a lower priority, don't allow
27156e5a87aSHajimu UMEMOTO 	 * the new level right now.
27256e5a87aSHajimu UMEMOTO 	 */
27356e5a87aSHajimu UMEMOTO 	if (priority < sc->curr_priority) {
2748d913481SNate Lawson 		CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
2758d913481SNate Lawson 		    sc->curr_priority);
276789f03ceSNate Lawson 		error = EPERM;
277789f03ceSNate Lawson 		goto out;
278789f03ceSNate Lawson 	}
2795f0afa04SNate Lawson 
28056e5a87aSHajimu UMEMOTO 	/*
28156e5a87aSHajimu UMEMOTO 	 * If the caller didn't specify a level and one is saved, prepare to
28256e5a87aSHajimu UMEMOTO 	 * restore the saved level.  If none has been saved, return an error.
28356e5a87aSHajimu UMEMOTO 	 */
28456e5a87aSHajimu UMEMOTO 	if (level == NULL) {
28556e5a87aSHajimu UMEMOTO 		saved_freq = SLIST_FIRST(&sc->saved_freq);
28656e5a87aSHajimu UMEMOTO 		if (saved_freq == NULL) {
28756e5a87aSHajimu UMEMOTO 			CF_DEBUG("NULL level, no saved level\n");
28856e5a87aSHajimu UMEMOTO 			error = ENXIO;
28956e5a87aSHajimu UMEMOTO 			goto out;
29056e5a87aSHajimu UMEMOTO 		}
29156e5a87aSHajimu UMEMOTO 		level = &saved_freq->level;
29256e5a87aSHajimu UMEMOTO 		priority = saved_freq->priority;
29356e5a87aSHajimu UMEMOTO 		CF_DEBUG("restoring saved level, freq %d prio %d\n",
29456e5a87aSHajimu UMEMOTO 		    level->total_set.freq, priority);
29556e5a87aSHajimu UMEMOTO 	}
29656e5a87aSHajimu UMEMOTO 
297b070969bSNate Lawson 	/* Reject levels that are below our specified threshold. */
298da8a77c1SNate Lawson 	if (level->total_set.freq < cf_lowest_freq) {
2998d913481SNate Lawson 		CF_DEBUG("rejecting freq %d, less than %d limit\n",
3008d913481SNate Lawson 		    level->total_set.freq, cf_lowest_freq);
301789f03ceSNate Lawson 		error = EINVAL;
302789f03ceSNate Lawson 		goto out;
303789f03ceSNate Lawson 	}
304b070969bSNate Lawson 
30573347b07SNate Lawson 	/* If already at this level, just return. */
3068d913481SNate Lawson 	if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq)) {
3078d913481SNate Lawson 		CF_DEBUG("skipping freq %d, same as current level %d\n",
3088d913481SNate Lawson 		    level->total_set.freq, sc->curr_level.total_set.freq);
3091fea6ce7SHajimu UMEMOTO 		goto skip;
3108d913481SNate Lawson 	}
31173347b07SNate Lawson 
31273347b07SNate Lawson 	/* First, set the absolute frequency via its driver. */
31373347b07SNate Lawson 	set = &level->abs_set;
31473347b07SNate Lawson 	if (set->dev) {
31573347b07SNate Lawson 		if (!device_is_attached(set->dev)) {
31673347b07SNate Lawson 			error = ENXIO;
31773347b07SNate Lawson 			goto out;
31873347b07SNate Lawson 		}
3191196826aSNate Lawson 
32062db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3211196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
322982d11f8SJeff Roberson 		thread_lock(curthread);
3231196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
324982d11f8SJeff Roberson 		thread_unlock(curthread);
3258d913481SNate Lawson 		CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
3268d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
32773347b07SNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
328982d11f8SJeff Roberson 		thread_lock(curthread);
3291196826aSNate Lawson 		sched_unbind(curthread);
330982d11f8SJeff Roberson 		thread_unlock(curthread);
33173347b07SNate Lawson 		if (error) {
33273347b07SNate Lawson 			goto out;
33373347b07SNate Lawson 		}
33473347b07SNate Lawson 	}
33573347b07SNate Lawson 
33688c9b54cSNate Lawson 	/* Next, set any/all relative frequencies via their drivers. */
33788c9b54cSNate Lawson 	for (i = 0; i < level->rel_count; i++) {
33888c9b54cSNate Lawson 		set = &level->rel_set[i];
33988c9b54cSNate Lawson 		if (!device_is_attached(set->dev)) {
34088c9b54cSNate Lawson 			error = ENXIO;
34188c9b54cSNate Lawson 			goto out;
34288c9b54cSNate Lawson 		}
3431196826aSNate Lawson 
34462db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3451196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
346982d11f8SJeff Roberson 		thread_lock(curthread);
3471196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
348982d11f8SJeff Roberson 		thread_unlock(curthread);
3498d913481SNate Lawson 		CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
3508d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
35188c9b54cSNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
352982d11f8SJeff Roberson 		thread_lock(curthread);
3531196826aSNate Lawson 		sched_unbind(curthread);
354982d11f8SJeff Roberson 		thread_unlock(curthread);
35588c9b54cSNate Lawson 		if (error) {
35688c9b54cSNate Lawson 			/* XXX Back out any successful setting? */
35788c9b54cSNate Lawson 			goto out;
35888c9b54cSNate Lawson 		}
35988c9b54cSNate Lawson 	}
36073347b07SNate Lawson 
3611fea6ce7SHajimu UMEMOTO skip:
3625f0afa04SNate Lawson 	/*
3635f0afa04SNate Lawson 	 * Before recording the current level, check if we're going to a
36456e5a87aSHajimu UMEMOTO 	 * higher priority.  If so, save the previous level and priority.
3655f0afa04SNate Lawson 	 */
3665f0afa04SNate Lawson 	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
36756e5a87aSHajimu UMEMOTO 	    priority > sc->curr_priority) {
3688d913481SNate Lawson 		CF_DEBUG("saving level, freq %d prio %d\n",
3698d913481SNate Lawson 		    sc->curr_level.total_set.freq, sc->curr_priority);
37056e5a87aSHajimu UMEMOTO 		curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
37156e5a87aSHajimu UMEMOTO 		if (curr_freq == NULL) {
37256e5a87aSHajimu UMEMOTO 			error = ENOMEM;
37356e5a87aSHajimu UMEMOTO 			goto out;
37456e5a87aSHajimu UMEMOTO 		}
37556e5a87aSHajimu UMEMOTO 		curr_freq->level = sc->curr_level;
37656e5a87aSHajimu UMEMOTO 		curr_freq->priority = sc->curr_priority;
37756e5a87aSHajimu UMEMOTO 		SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
3785f0afa04SNate Lawson 	}
37973347b07SNate Lawson 	sc->curr_level = *level;
3805f0afa04SNate Lawson 	sc->curr_priority = priority;
38173347b07SNate Lawson 
38256e5a87aSHajimu UMEMOTO 	/* If we were restoring a saved state, reset it to "unused". */
38356e5a87aSHajimu UMEMOTO 	if (saved_freq != NULL) {
38456e5a87aSHajimu UMEMOTO 		CF_DEBUG("resetting saved level\n");
38556e5a87aSHajimu UMEMOTO 		sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
38656e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
38756e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
38856e5a87aSHajimu UMEMOTO 	}
38956e5a87aSHajimu UMEMOTO 
39073347b07SNate Lawson out:
391789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
3920d4ac62aSNate Lawson 
3930d4ac62aSNate Lawson 	/*
3940d4ac62aSNate Lawson 	 * We changed levels (or attempted to) so notify the post-change
3950d4ac62aSNate Lawson 	 * handler of new frequency or error.
3960d4ac62aSNate Lawson 	 */
3970d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
398789f03ceSNate Lawson 	if (error && set)
39973347b07SNate Lawson 		device_printf(set->dev, "set freq failed, err %d\n", error);
4000d4ac62aSNate Lawson 
40173347b07SNate Lawson 	return (error);
40273347b07SNate Lawson }
40373347b07SNate Lawson 
40473347b07SNate Lawson static int
40573347b07SNate Lawson cf_get_method(device_t dev, struct cf_level *level)
40673347b07SNate Lawson {
40773347b07SNate Lawson 	struct cpufreq_softc *sc;
40873347b07SNate Lawson 	struct cf_level *levels;
40973347b07SNate Lawson 	struct cf_setting *curr_set, set;
41073347b07SNate Lawson 	struct pcpu *pc;
41173347b07SNate Lawson 	device_t *devs;
4122145b9d2SNate Lawson 	int count, error, i, n, numdevs;
41373347b07SNate Lawson 	uint64_t rate;
41473347b07SNate Lawson 
41573347b07SNate Lawson 	sc = device_get_softc(dev);
416789f03ceSNate Lawson 	error = 0;
41773347b07SNate Lawson 	levels = NULL;
41873347b07SNate Lawson 
41973347b07SNate Lawson 	/* If we already know the current frequency, we're done. */
420789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
421789f03ceSNate Lawson 	curr_set = &sc->curr_level.total_set;
4228d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4238d913481SNate Lawson 		CF_DEBUG("get returning known freq %d\n", curr_set->freq);
42473347b07SNate Lawson 		goto out;
4258d913481SNate Lawson 	}
426789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
42773347b07SNate Lawson 
42873347b07SNate Lawson 	/*
42973347b07SNate Lawson 	 * We need to figure out the current level.  Loop through every
43073347b07SNate Lawson 	 * driver, getting the current setting.  Then, attempt to get a best
43173347b07SNate Lawson 	 * match of settings against each level.
43273347b07SNate Lawson 	 */
43373347b07SNate Lawson 	count = CF_MAX_LEVELS;
43473347b07SNate Lawson 	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
43573347b07SNate Lawson 	if (levels == NULL)
43673347b07SNate Lawson 		return (ENOMEM);
43773347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
438d269386aSNate Lawson 	if (error) {
439d269386aSNate Lawson 		if (error == E2BIG)
440d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
441789f03ceSNate Lawson 		free(levels, M_TEMP);
442789f03ceSNate Lawson 		return (error);
443d269386aSNate Lawson 	}
44473347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
445789f03ceSNate Lawson 	if (error) {
446789f03ceSNate Lawson 		free(levels, M_TEMP);
447789f03ceSNate Lawson 		return (error);
448789f03ceSNate Lawson 	}
449789f03ceSNate Lawson 
450789f03ceSNate Lawson 	/*
451789f03ceSNate Lawson 	 * Reacquire the lock and search for the given level.
452789f03ceSNate Lawson 	 *
453789f03ceSNate Lawson 	 * XXX Note: this is not quite right since we really need to go
454789f03ceSNate Lawson 	 * through each level and compare both absolute and relative
455789f03ceSNate Lawson 	 * settings for each driver in the system before making a match.
456789f03ceSNate Lawson 	 * The estimation code below catches this case though.
457789f03ceSNate Lawson 	 */
458789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
4592145b9d2SNate Lawson 	for (n = 0; n < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; n++) {
4602145b9d2SNate Lawson 		if (!device_is_attached(devs[n]))
46173347b07SNate Lawson 			continue;
462be00f605SJohn Baldwin 		if (CPUFREQ_DRV_GET(devs[n], &set) != 0)
46373347b07SNate Lawson 			continue;
46473347b07SNate Lawson 		for (i = 0; i < count; i++) {
46588c9b54cSNate Lawson 			if (CPUFREQ_CMP(set.freq, levels[i].total_set.freq)) {
46673347b07SNate Lawson 				sc->curr_level = levels[i];
46773347b07SNate Lawson 				break;
46873347b07SNate Lawson 			}
46973347b07SNate Lawson 		}
47073347b07SNate Lawson 	}
47173347b07SNate Lawson 	free(devs, M_TEMP);
4728d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4738d913481SNate Lawson 		CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
47473347b07SNate Lawson 		goto out;
4758d913481SNate Lawson 	}
47673347b07SNate Lawson 
47773347b07SNate Lawson 	/*
47873347b07SNate Lawson 	 * We couldn't find an exact match, so attempt to estimate and then
47973347b07SNate Lawson 	 * match against a level.
48073347b07SNate Lawson 	 */
48173347b07SNate Lawson 	pc = cpu_get_pcpu(dev);
48273347b07SNate Lawson 	if (pc == NULL) {
48373347b07SNate Lawson 		error = ENXIO;
48473347b07SNate Lawson 		goto out;
48573347b07SNate Lawson 	}
48673347b07SNate Lawson 	cpu_est_clockrate(pc->pc_cpuid, &rate);
48773347b07SNate Lawson 	rate /= 1000000;
48873347b07SNate Lawson 	for (i = 0; i < count; i++) {
48973347b07SNate Lawson 		if (CPUFREQ_CMP(rate, levels[i].total_set.freq)) {
49073347b07SNate Lawson 			sc->curr_level = levels[i];
4918d913481SNate Lawson 			CF_DEBUG("get estimated freq %d\n", curr_set->freq);
492be00f605SJohn Baldwin 			goto out;
49373347b07SNate Lawson 		}
49473347b07SNate Lawson 	}
495be00f605SJohn Baldwin 	error = ENXIO;
49673347b07SNate Lawson 
49773347b07SNate Lawson out:
498789f03ceSNate Lawson 	if (error == 0)
499789f03ceSNate Lawson 		*level = sc->curr_level;
500789f03ceSNate Lawson 
501789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
50273347b07SNate Lawson 	if (levels)
50373347b07SNate Lawson 		free(levels, M_TEMP);
504789f03ceSNate Lawson 	return (error);
50573347b07SNate Lawson }
50673347b07SNate Lawson 
50773347b07SNate Lawson static int
50873347b07SNate Lawson cf_levels_method(device_t dev, struct cf_level *levels, int *count)
50973347b07SNate Lawson {
51088c9b54cSNate Lawson 	struct cf_setting_array *set_arr;
51173347b07SNate Lawson 	struct cf_setting_lst rel_sets;
51273347b07SNate Lawson 	struct cpufreq_softc *sc;
51373347b07SNate Lawson 	struct cf_level *lev;
51473347b07SNate Lawson 	struct cf_setting *sets;
51573347b07SNate Lawson 	struct pcpu *pc;
51673347b07SNate Lawson 	device_t *devs;
51788c9b54cSNate Lawson 	int error, i, numdevs, set_count, type;
51873347b07SNate Lawson 	uint64_t rate;
51973347b07SNate Lawson 
52073347b07SNate Lawson 	if (levels == NULL || count == NULL)
52173347b07SNate Lawson 		return (EINVAL);
52273347b07SNate Lawson 
52373347b07SNate Lawson 	TAILQ_INIT(&rel_sets);
52473347b07SNate Lawson 	sc = device_get_softc(dev);
52573347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
52673347b07SNate Lawson 	if (error)
52773347b07SNate Lawson 		return (error);
52873347b07SNate Lawson 	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
52973347b07SNate Lawson 	if (sets == NULL) {
53073347b07SNate Lawson 		free(devs, M_TEMP);
53173347b07SNate Lawson 		return (ENOMEM);
53273347b07SNate Lawson 	}
53373347b07SNate Lawson 
53473347b07SNate Lawson 	/* Get settings from all cpufreq drivers. */
535789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
53673347b07SNate Lawson 	for (i = 0; i < numdevs; i++) {
537e22cd41cSNate Lawson 		/* Skip devices that aren't ready. */
53873347b07SNate Lawson 		if (!device_is_attached(devs[i]))
53973347b07SNate Lawson 			continue;
540e22cd41cSNate Lawson 
541e22cd41cSNate Lawson 		/*
542e22cd41cSNate Lawson 		 * Get settings, skipping drivers that offer no settings or
543e22cd41cSNate Lawson 		 * provide settings for informational purposes only.
544e22cd41cSNate Lawson 		 */
545e94a0c1aSNate Lawson 		error = CPUFREQ_DRV_TYPE(devs[i], &type);
5468d913481SNate Lawson 		if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
5478d913481SNate Lawson 			if (error == 0) {
5488d913481SNate Lawson 				CF_DEBUG("skipping info-only driver %s\n",
5498d913481SNate Lawson 				    device_get_nameunit(devs[i]));
5508d913481SNate Lawson 			}
551e94a0c1aSNate Lawson 			continue;
5528d913481SNate Lawson 		}
55373347b07SNate Lawson 		set_count = MAX_SETTINGS;
554e94a0c1aSNate Lawson 		error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
555e94a0c1aSNate Lawson 		if (error || set_count == 0)
55673347b07SNate Lawson 			continue;
55773347b07SNate Lawson 
558e22cd41cSNate Lawson 		/* Add the settings to our absolute/relative lists. */
5590325089dSNate Lawson 		switch (type & CPUFREQ_TYPE_MASK) {
56088c9b54cSNate Lawson 		case CPUFREQ_TYPE_ABSOLUTE:
56188c9b54cSNate Lawson 			error = cpufreq_insert_abs(sc, sets, set_count);
56288c9b54cSNate Lawson 			break;
56388c9b54cSNate Lawson 		case CPUFREQ_TYPE_RELATIVE:
5648d913481SNate Lawson 			CF_DEBUG("adding %d relative settings\n", set_count);
56588c9b54cSNate Lawson 			set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
56688c9b54cSNate Lawson 			if (set_arr == NULL) {
56788c9b54cSNate Lawson 				error = ENOMEM;
56888c9b54cSNate Lawson 				goto out;
56988c9b54cSNate Lawson 			}
57088c9b54cSNate Lawson 			bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
57188c9b54cSNate Lawson 			set_arr->count = set_count;
57288c9b54cSNate Lawson 			TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
57388c9b54cSNate Lawson 			break;
57488c9b54cSNate Lawson 		default:
57588c9b54cSNate Lawson 			error = EINVAL;
57688c9b54cSNate Lawson 		}
57788c9b54cSNate Lawson 		if (error)
57873347b07SNate Lawson 			goto out;
57973347b07SNate Lawson 	}
58073347b07SNate Lawson 
58167c8649fSNate Lawson 	/*
58267c8649fSNate Lawson 	 * If there are no absolute levels, create a fake one at 100%.  We
58367c8649fSNate Lawson 	 * then cache the clockrate for later use as our base frequency.
58467c8649fSNate Lawson 	 *
58567c8649fSNate Lawson 	 * XXX This assumes that the first time through, if we only have
58667c8649fSNate Lawson 	 * relative drivers, the CPU is currently running at 100%.
58767c8649fSNate Lawson 	 */
58873347b07SNate Lawson 	if (TAILQ_EMPTY(&sc->all_levels)) {
58967c8649fSNate Lawson 		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
59073347b07SNate Lawson 			pc = cpu_get_pcpu(dev);
59173347b07SNate Lawson 			cpu_est_clockrate(pc->pc_cpuid, &rate);
59267c8649fSNate Lawson 			sc->max_mhz = rate / 1000000;
59367c8649fSNate Lawson 		}
59467c8649fSNate Lawson 		memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
59567c8649fSNate Lawson 		sets[0].freq = sc->max_mhz;
59667c8649fSNate Lawson 		sets[0].dev = NULL;
59788c9b54cSNate Lawson 		error = cpufreq_insert_abs(sc, sets, 1);
59873347b07SNate Lawson 		if (error)
59973347b07SNate Lawson 			goto out;
60073347b07SNate Lawson 	}
60173347b07SNate Lawson 
60288c9b54cSNate Lawson 	/* Create a combined list of absolute + relative levels. */
60388c9b54cSNate Lawson 	TAILQ_FOREACH(set_arr, &rel_sets, link)
60488c9b54cSNate Lawson 		cpufreq_expand_set(sc, set_arr);
60588c9b54cSNate Lawson 
60688c9b54cSNate Lawson 	/* If the caller doesn't have enough space, return the actual count. */
60788c9b54cSNate Lawson 	if (sc->all_count > *count) {
60888c9b54cSNate Lawson 		*count = sc->all_count;
60988c9b54cSNate Lawson 		error = E2BIG;
61088c9b54cSNate Lawson 		goto out;
61188c9b54cSNate Lawson 	}
61288c9b54cSNate Lawson 
61388c9b54cSNate Lawson 	/* Finally, output the list of levels. */
61473347b07SNate Lawson 	i = 0;
61573347b07SNate Lawson 	TAILQ_FOREACH(lev, &sc->all_levels, link) {
616e1f13773SNate Lawson 		/*
617e1f13773SNate Lawson 		 * Skip levels that are too close in frequency to the
618e1f13773SNate Lawson 		 * previous levels.  Some systems report bogus duplicate
619e1f13773SNate Lawson 		 * settings (i.e., for acpi_perf).
620e1f13773SNate Lawson 		 */
621e1f13773SNate Lawson 		if (i > 0 && CPUFREQ_CMP(lev->total_set.freq,
622e1f13773SNate Lawson 		    levels[i - 1].total_set.freq)) {
623e1f13773SNate Lawson 			sc->all_count--;
624e1f13773SNate Lawson 			continue;
625e1f13773SNate Lawson 		}
626e1f13773SNate Lawson 
627b070969bSNate Lawson 		/* Skip levels that have a frequency that is too low. */
628da8a77c1SNate Lawson 		if (lev->total_set.freq < cf_lowest_freq) {
629b070969bSNate Lawson 			sc->all_count--;
630b070969bSNate Lawson 			continue;
631b070969bSNate Lawson 		}
632b070969bSNate Lawson 
63373347b07SNate Lawson 		levels[i] = *lev;
63473347b07SNate Lawson 		i++;
63573347b07SNate Lawson 	}
63688c9b54cSNate Lawson 	*count = sc->all_count;
63773347b07SNate Lawson 	error = 0;
63873347b07SNate Lawson 
63973347b07SNate Lawson out:
64073347b07SNate Lawson 	/* Clear all levels since we regenerate them each time. */
64173347b07SNate Lawson 	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
64273347b07SNate Lawson 		TAILQ_REMOVE(&sc->all_levels, lev, link);
64373347b07SNate Lawson 		free(lev, M_TEMP);
64473347b07SNate Lawson 	}
645789f03ceSNate Lawson 	sc->all_count = 0;
646789f03ceSNate Lawson 
647789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
64888c9b54cSNate Lawson 	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
64988c9b54cSNate Lawson 		TAILQ_REMOVE(&rel_sets, set_arr, link);
65088c9b54cSNate Lawson 		free(set_arr, M_TEMP);
65188c9b54cSNate Lawson 	}
65273347b07SNate Lawson 	free(devs, M_TEMP);
65373347b07SNate Lawson 	free(sets, M_TEMP);
65473347b07SNate Lawson 	return (error);
65573347b07SNate Lawson }
65673347b07SNate Lawson 
65773347b07SNate Lawson /*
65873347b07SNate Lawson  * Create levels for an array of absolute settings and insert them in
65973347b07SNate Lawson  * sorted order in the specified list.
66073347b07SNate Lawson  */
66173347b07SNate Lawson static int
66288c9b54cSNate Lawson cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
66373347b07SNate Lawson     int count)
66473347b07SNate Lawson {
66588c9b54cSNate Lawson 	struct cf_level_lst *list;
66673347b07SNate Lawson 	struct cf_level *level, *search;
66773347b07SNate Lawson 	int i;
66873347b07SNate Lawson 
669789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
670789f03ceSNate Lawson 
67188c9b54cSNate Lawson 	list = &sc->all_levels;
67273347b07SNate Lawson 	for (i = 0; i < count; i++) {
67373347b07SNate Lawson 		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
67473347b07SNate Lawson 		if (level == NULL)
67573347b07SNate Lawson 			return (ENOMEM);
67673347b07SNate Lawson 		level->abs_set = sets[i];
67788c9b54cSNate Lawson 		level->total_set = sets[i];
67888c9b54cSNate Lawson 		level->total_set.dev = NULL;
67988c9b54cSNate Lawson 		sc->all_count++;
68073347b07SNate Lawson 
68173347b07SNate Lawson 		if (TAILQ_EMPTY(list)) {
6828d913481SNate Lawson 			CF_DEBUG("adding abs setting %d at head\n",
6838d913481SNate Lawson 			    sets[i].freq);
68473347b07SNate Lawson 			TAILQ_INSERT_HEAD(list, level, link);
68573347b07SNate Lawson 			continue;
68673347b07SNate Lawson 		}
68773347b07SNate Lawson 
68873347b07SNate Lawson 		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
68988c9b54cSNate Lawson 			if (sets[i].freq <= search->total_set.freq) {
6908d913481SNate Lawson 				CF_DEBUG("adding abs setting %d after %d\n",
6918d913481SNate Lawson 				    sets[i].freq, search->total_set.freq);
69273347b07SNate Lawson 				TAILQ_INSERT_AFTER(list, search, level, link);
69373347b07SNate Lawson 				break;
69473347b07SNate Lawson 			}
69573347b07SNate Lawson 		}
69673347b07SNate Lawson 	}
69773347b07SNate Lawson 	return (0);
69873347b07SNate Lawson }
69973347b07SNate Lawson 
70088c9b54cSNate Lawson /*
70188c9b54cSNate Lawson  * Expand a group of relative settings, creating derived levels from them.
70288c9b54cSNate Lawson  */
70388c9b54cSNate Lawson static int
70488c9b54cSNate Lawson cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
70588c9b54cSNate Lawson {
70688c9b54cSNate Lawson 	struct cf_level *fill, *search;
70788c9b54cSNate Lawson 	struct cf_setting *set;
70888c9b54cSNate Lawson 	int i;
70988c9b54cSNate Lawson 
710789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
711789f03ceSNate Lawson 
7125308b2a6SNate Lawson 	/*
7135308b2a6SNate Lawson 	 * Walk the set of all existing levels in reverse.  This is so we
7145308b2a6SNate Lawson 	 * create derived states from the lowest absolute settings first
7155308b2a6SNate Lawson 	 * and discard duplicates created from higher absolute settings.
7165308b2a6SNate Lawson 	 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
7175308b2a6SNate Lawson 	 * preferable to 200 Mhz + 25% because absolute settings are more
7185308b2a6SNate Lawson 	 * efficient since they often change the voltage as well.
7195308b2a6SNate Lawson 	 */
7205308b2a6SNate Lawson 	TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
72188c9b54cSNate Lawson 		/* Add each setting to the level, duplicating if necessary. */
72288c9b54cSNate Lawson 		for (i = 0; i < set_arr->count; i++) {
72388c9b54cSNate Lawson 			set = &set_arr->sets[i];
72488c9b54cSNate Lawson 
72588c9b54cSNate Lawson 			/*
72688c9b54cSNate Lawson 			 * If this setting is less than 100%, split the level
72788c9b54cSNate Lawson 			 * into two and add this setting to the new level.
72888c9b54cSNate Lawson 			 */
72988c9b54cSNate Lawson 			fill = search;
7305308b2a6SNate Lawson 			if (set->freq < 10000) {
73188c9b54cSNate Lawson 				fill = cpufreq_dup_set(sc, search, set);
73288c9b54cSNate Lawson 
73388c9b54cSNate Lawson 				/*
7345308b2a6SNate Lawson 				 * The new level was a duplicate of an existing
7355308b2a6SNate Lawson 				 * level or its absolute setting is too high
7365308b2a6SNate Lawson 				 * so we freed it.  For example, we discard a
7375308b2a6SNate Lawson 				 * derived level of 1000 MHz/25% if a level
7385308b2a6SNate Lawson 				 * of 500 MHz/100% already exists.
73988c9b54cSNate Lawson 				 */
74088c9b54cSNate Lawson 				if (fill == NULL)
7415308b2a6SNate Lawson 					break;
7425308b2a6SNate Lawson 			}
74388c9b54cSNate Lawson 
74488c9b54cSNate Lawson 			/* Add this setting to the existing or new level. */
74588c9b54cSNate Lawson 			KASSERT(fill->rel_count < MAX_SETTINGS,
74688c9b54cSNate Lawson 			    ("cpufreq: too many relative drivers (%d)",
74788c9b54cSNate Lawson 			    MAX_SETTINGS));
74888c9b54cSNate Lawson 			fill->rel_set[fill->rel_count] = *set;
74988c9b54cSNate Lawson 			fill->rel_count++;
7508d913481SNate Lawson 			CF_DEBUG(
7518d913481SNate Lawson 			"expand set added rel setting %d%% to %d level\n",
7528d913481SNate Lawson 			    set->freq / 100, fill->total_set.freq);
75388c9b54cSNate Lawson 		}
75488c9b54cSNate Lawson 	}
75588c9b54cSNate Lawson 
75688c9b54cSNate Lawson 	return (0);
75788c9b54cSNate Lawson }
75888c9b54cSNate Lawson 
75988c9b54cSNate Lawson static struct cf_level *
76088c9b54cSNate Lawson cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
76188c9b54cSNate Lawson     struct cf_setting *set)
76288c9b54cSNate Lawson {
76388c9b54cSNate Lawson 	struct cf_level_lst *list;
76488c9b54cSNate Lawson 	struct cf_level *fill, *itr;
76588c9b54cSNate Lawson 	struct cf_setting *fill_set, *itr_set;
76688c9b54cSNate Lawson 	int i;
76788c9b54cSNate Lawson 
768789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
769789f03ceSNate Lawson 
77088c9b54cSNate Lawson 	/*
77188c9b54cSNate Lawson 	 * Create a new level, copy it from the old one, and update the
77288c9b54cSNate Lawson 	 * total frequency and power by the percentage specified in the
77388c9b54cSNate Lawson 	 * relative setting.
77488c9b54cSNate Lawson 	 */
77588c9b54cSNate Lawson 	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
77688c9b54cSNate Lawson 	if (fill == NULL)
77788c9b54cSNate Lawson 		return (NULL);
77888c9b54cSNate Lawson 	*fill = *dup;
77988c9b54cSNate Lawson 	fill_set = &fill->total_set;
78088c9b54cSNate Lawson 	fill_set->freq =
78188c9b54cSNate Lawson 	    ((uint64_t)fill_set->freq * set->freq) / 10000;
78288c9b54cSNate Lawson 	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
78388c9b54cSNate Lawson 		fill_set->power = ((uint64_t)fill_set->power * set->freq)
78488c9b54cSNate Lawson 		    / 10000;
78588c9b54cSNate Lawson 	}
78688c9b54cSNate Lawson 	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
78788c9b54cSNate Lawson 		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
78888c9b54cSNate Lawson 			fill_set->lat += set->lat;
78988c9b54cSNate Lawson 		else
79088c9b54cSNate Lawson 			fill_set->lat = set->lat;
79188c9b54cSNate Lawson 	}
7928d913481SNate Lawson 	CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
79388c9b54cSNate Lawson 
79488c9b54cSNate Lawson 	/*
79588c9b54cSNate Lawson 	 * If we copied an old level that we already modified (say, at 100%),
79688c9b54cSNate Lawson 	 * we need to remove that setting before adding this one.  Since we
79788c9b54cSNate Lawson 	 * process each setting array in order, we know any settings for this
79888c9b54cSNate Lawson 	 * driver will be found at the end.
79988c9b54cSNate Lawson 	 */
80088c9b54cSNate Lawson 	for (i = fill->rel_count; i != 0; i--) {
80188c9b54cSNate Lawson 		if (fill->rel_set[i - 1].dev != set->dev)
80288c9b54cSNate Lawson 			break;
8038d913481SNate Lawson 		CF_DEBUG("removed last relative driver: %s\n",
8048d913481SNate Lawson 		    device_get_nameunit(set->dev));
80588c9b54cSNate Lawson 		fill->rel_count--;
80688c9b54cSNate Lawson 	}
80788c9b54cSNate Lawson 
80888c9b54cSNate Lawson 	/*
8095308b2a6SNate Lawson 	 * Insert the new level in sorted order.  If it is a duplicate of an
8105308b2a6SNate Lawson 	 * existing level (1) or has an absolute setting higher than the
8115308b2a6SNate Lawson 	 * existing level (2), do not add it.  We can do this since any such
8125308b2a6SNate Lawson 	 * level is guaranteed use less power.  For example (1), a level with
8135308b2a6SNate Lawson 	 * one absolute setting of 800 Mhz uses less power than one composed
8145308b2a6SNate Lawson 	 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
8155308b2a6SNate Lawson 	 * Also for example (2), a level of 800 Mhz/75% is preferable to
8165308b2a6SNate Lawson 	 * 1600 Mhz/25% even though the latter has a lower total frequency.
81788c9b54cSNate Lawson 	 */
81888c9b54cSNate Lawson 	list = &sc->all_levels;
8195308b2a6SNate Lawson 	KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
82088c9b54cSNate Lawson 	TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
82188c9b54cSNate Lawson 		itr_set = &itr->total_set;
8229000b91eSNate Lawson 		if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
8239000b91eSNate Lawson 			CF_DEBUG("dup set rejecting %d (dupe)\n",
8249000b91eSNate Lawson 			    fill_set->freq);
8259000b91eSNate Lawson 			itr = NULL;
8269000b91eSNate Lawson 			break;
8279000b91eSNate Lawson 		} else if (fill_set->freq < itr_set->freq) {
8289000b91eSNate Lawson 			if (fill->abs_set.freq <= itr->abs_set.freq) {
8298d913481SNate Lawson 				CF_DEBUG(
8308d913481SNate Lawson 			"dup done, inserting new level %d after %d\n",
8318d913481SNate Lawson 				    fill_set->freq, itr_set->freq);
83288c9b54cSNate Lawson 				TAILQ_INSERT_AFTER(list, itr, fill, link);
83388c9b54cSNate Lawson 				sc->all_count++;
8349000b91eSNate Lawson 			} else {
8359000b91eSNate Lawson 				CF_DEBUG("dup set rejecting %d (abs too big)\n",
8369000b91eSNate Lawson 				    fill_set->freq);
8379000b91eSNate Lawson 				itr = NULL;
8389000b91eSNate Lawson 			}
83988c9b54cSNate Lawson 			break;
84088c9b54cSNate Lawson 		}
84188c9b54cSNate Lawson 	}
8425308b2a6SNate Lawson 
8435308b2a6SNate Lawson 	/* We didn't find a good place for this new level so free it. */
8445308b2a6SNate Lawson 	if (itr == NULL) {
8455308b2a6SNate Lawson 		CF_DEBUG("dup set freeing new level %d (not optimal)\n",
8465308b2a6SNate Lawson 		    fill_set->freq);
8475308b2a6SNate Lawson 		free(fill, M_TEMP);
8485308b2a6SNate Lawson 		fill = NULL;
84988c9b54cSNate Lawson 	}
85088c9b54cSNate Lawson 
85188c9b54cSNate Lawson 	return (fill);
85288c9b54cSNate Lawson }
85388c9b54cSNate Lawson 
85473347b07SNate Lawson static int
85573347b07SNate Lawson cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
85673347b07SNate Lawson {
85773347b07SNate Lawson 	struct cpufreq_softc *sc;
85873347b07SNate Lawson 	struct cf_level *levels;
8590325089dSNate Lawson 	int count, devcount, error, freq, i, n;
8600325089dSNate Lawson 	device_t *devs;
86173347b07SNate Lawson 
8620325089dSNate Lawson 	devs = NULL;
86373347b07SNate Lawson 	sc = oidp->oid_arg1;
8640325089dSNate Lawson 	levels = malloc(CF_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
86573347b07SNate Lawson 	if (levels == NULL)
86673347b07SNate Lawson 		return (ENOMEM);
86773347b07SNate Lawson 
86873347b07SNate Lawson 	error = CPUFREQ_GET(sc->dev, &levels[0]);
86973347b07SNate Lawson 	if (error)
87073347b07SNate Lawson 		goto out;
87173347b07SNate Lawson 	freq = levels[0].total_set.freq;
87273347b07SNate Lawson 	error = sysctl_handle_int(oidp, &freq, 0, req);
87373347b07SNate Lawson 	if (error != 0 || req->newptr == NULL)
87473347b07SNate Lawson 		goto out;
87573347b07SNate Lawson 
8760325089dSNate Lawson 	/*
8770325089dSNate Lawson 	 * While we only call cpufreq_get() on one device (assuming all
8780325089dSNate Lawson 	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
8790325089dSNate Lawson 	 * This is needed for some MP systems.
8800325089dSNate Lawson 	 */
8810325089dSNate Lawson 	error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
88273347b07SNate Lawson 	if (error)
88373347b07SNate Lawson 		goto out;
8840325089dSNate Lawson 	for (n = 0; n < devcount; n++) {
8850325089dSNate Lawson 		count = CF_MAX_LEVELS;
8860325089dSNate Lawson 		error = CPUFREQ_LEVELS(devs[n], levels, &count);
887d269386aSNate Lawson 		if (error) {
888d269386aSNate Lawson 			if (error == E2BIG)
889d269386aSNate Lawson 				printf(
890d269386aSNate Lawson 			"cpufreq: need to increase CF_MAX_LEVELS\n");
8910325089dSNate Lawson 			break;
892d269386aSNate Lawson 		}
89373347b07SNate Lawson 		for (i = 0; i < count; i++) {
89473347b07SNate Lawson 			if (CPUFREQ_CMP(levels[i].total_set.freq, freq)) {
8950325089dSNate Lawson 				error = CPUFREQ_SET(devs[n], &levels[i],
89673347b07SNate Lawson 				    CPUFREQ_PRIO_USER);
89773347b07SNate Lawson 				break;
89873347b07SNate Lawson 			}
89973347b07SNate Lawson 		}
9000325089dSNate Lawson 		if (i == count) {
90173347b07SNate Lawson 			error = EINVAL;
9020325089dSNate Lawson 			break;
9030325089dSNate Lawson 		}
9040325089dSNate Lawson 	}
90573347b07SNate Lawson 
90673347b07SNate Lawson out:
9070325089dSNate Lawson 	if (devs)
9080325089dSNate Lawson 		free(devs, M_TEMP);
90973347b07SNate Lawson 	if (levels)
91073347b07SNate Lawson 		free(levels, M_TEMP);
91173347b07SNate Lawson 	return (error);
91273347b07SNate Lawson }
91373347b07SNate Lawson 
91473347b07SNate Lawson static int
91573347b07SNate Lawson cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
91673347b07SNate Lawson {
91773347b07SNate Lawson 	struct cpufreq_softc *sc;
91873347b07SNate Lawson 	struct cf_level *levels;
91973347b07SNate Lawson 	struct cf_setting *set;
92073347b07SNate Lawson 	struct sbuf sb;
92173347b07SNate Lawson 	int count, error, i;
92273347b07SNate Lawson 
92373347b07SNate Lawson 	sc = oidp->oid_arg1;
92473347b07SNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
92573347b07SNate Lawson 
92673347b07SNate Lawson 	/* Get settings from the device and generate the output string. */
92773347b07SNate Lawson 	count = CF_MAX_LEVELS;
92873347b07SNate Lawson 	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
92973347b07SNate Lawson 	if (levels == NULL)
93073347b07SNate Lawson 		return (ENOMEM);
93173347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
932d269386aSNate Lawson 	if (error) {
933d269386aSNate Lawson 		if (error == E2BIG)
934d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
93573347b07SNate Lawson 		goto out;
936d269386aSNate Lawson 	}
93773347b07SNate Lawson 	if (count) {
93873347b07SNate Lawson 		for (i = 0; i < count; i++) {
93973347b07SNate Lawson 			set = &levels[i].total_set;
94073347b07SNate Lawson 			sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
94173347b07SNate Lawson 		}
94273347b07SNate Lawson 	} else
94373347b07SNate Lawson 		sbuf_cpy(&sb, "0");
94473347b07SNate Lawson 	sbuf_trim(&sb);
94573347b07SNate Lawson 	sbuf_finish(&sb);
94673347b07SNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
94773347b07SNate Lawson 
94873347b07SNate Lawson out:
94973347b07SNate Lawson 	free(levels, M_TEMP);
95073347b07SNate Lawson 	sbuf_delete(&sb);
95173347b07SNate Lawson 	return (error);
95273347b07SNate Lawson }
95373347b07SNate Lawson 
954e959a70bSNate Lawson static int
955e959a70bSNate Lawson cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
956e959a70bSNate Lawson {
957e959a70bSNate Lawson 	device_t dev;
958e959a70bSNate Lawson 	struct cf_setting *sets;
959e959a70bSNate Lawson 	struct sbuf sb;
960e959a70bSNate Lawson 	int error, i, set_count;
961e959a70bSNate Lawson 
962e959a70bSNate Lawson 	dev = oidp->oid_arg1;
963e959a70bSNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
964e959a70bSNate Lawson 
965e959a70bSNate Lawson 	/* Get settings from the device and generate the output string. */
966e959a70bSNate Lawson 	set_count = MAX_SETTINGS;
967e959a70bSNate Lawson 	sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
968e959a70bSNate Lawson 	if (sets == NULL)
969e959a70bSNate Lawson 		return (ENOMEM);
970e959a70bSNate Lawson 	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
971e959a70bSNate Lawson 	if (error)
972e959a70bSNate Lawson 		goto out;
973e959a70bSNate Lawson 	if (set_count) {
974e959a70bSNate Lawson 		for (i = 0; i < set_count; i++)
975e959a70bSNate Lawson 			sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
976e959a70bSNate Lawson 	} else
977e959a70bSNate Lawson 		sbuf_cpy(&sb, "0");
978e959a70bSNate Lawson 	sbuf_trim(&sb);
979e959a70bSNate Lawson 	sbuf_finish(&sb);
980e959a70bSNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
981e959a70bSNate Lawson 
982e959a70bSNate Lawson out:
983e959a70bSNate Lawson 	free(sets, M_TEMP);
984e959a70bSNate Lawson 	sbuf_delete(&sb);
985e959a70bSNate Lawson 	return (error);
986e959a70bSNate Lawson }
987e959a70bSNate Lawson 
98873347b07SNate Lawson int
98973347b07SNate Lawson cpufreq_register(device_t dev)
99073347b07SNate Lawson {
99167c8649fSNate Lawson 	struct cpufreq_softc *sc;
99273347b07SNate Lawson 	device_t cf_dev, cpu_dev;
99373347b07SNate Lawson 
994e959a70bSNate Lawson 	/* Add a sysctl to get each driver's settings separately. */
995e959a70bSNate Lawson 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
996e959a70bSNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
997e959a70bSNate Lawson 	    OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
998e959a70bSNate Lawson 	    cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
999e959a70bSNate Lawson 
100073347b07SNate Lawson 	/*
10010325089dSNate Lawson 	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
10020325089dSNate Lawson 	 * must offer the same levels and be switched at the same time.
100373347b07SNate Lawson 	 */
10040325089dSNate Lawson 	cpu_dev = device_get_parent(dev);
100567c8649fSNate Lawson 	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
100667c8649fSNate Lawson 		sc = device_get_softc(cf_dev);
100767c8649fSNate Lawson 		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
100873347b07SNate Lawson 		return (0);
100967c8649fSNate Lawson 	}
101073347b07SNate Lawson 
10110325089dSNate Lawson 	/* Add the child device and possibly sysctls. */
10120325089dSNate Lawson 	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
101373347b07SNate Lawson 	if (cf_dev == NULL)
101473347b07SNate Lawson 		return (ENOMEM);
101573347b07SNate Lawson 	device_quiet(cf_dev);
101673347b07SNate Lawson 
101773347b07SNate Lawson 	return (device_probe_and_attach(cf_dev));
101873347b07SNate Lawson }
101973347b07SNate Lawson 
102073347b07SNate Lawson int
102173347b07SNate Lawson cpufreq_unregister(device_t dev)
102273347b07SNate Lawson {
102373347b07SNate Lawson 	device_t cf_dev, *devs;
1024e94a0c1aSNate Lawson 	int cfcount, devcount, error, i, type;
102573347b07SNate Lawson 
102673347b07SNate Lawson 	/*
102773347b07SNate Lawson 	 * If this is the last cpufreq child device, remove the control
102873347b07SNate Lawson 	 * device as well.  We identify cpufreq children by calling a method
102973347b07SNate Lawson 	 * they support.
103073347b07SNate Lawson 	 */
103173347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &devcount);
103273347b07SNate Lawson 	if (error)
103373347b07SNate Lawson 		return (error);
103467c8649fSNate Lawson 	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
103571ab130cSNate Lawson 	if (cf_dev == NULL) {
103671ab130cSNate Lawson 		device_printf(dev,
103771ab130cSNate Lawson 	"warning: cpufreq_unregister called with no cpufreq device active\n");
103871ab130cSNate Lawson 		return (0);
103971ab130cSNate Lawson 	}
104073347b07SNate Lawson 	cfcount = 0;
104173347b07SNate Lawson 	for (i = 0; i < devcount; i++) {
104273347b07SNate Lawson 		if (!device_is_attached(devs[i]))
104373347b07SNate Lawson 			continue;
1044e94a0c1aSNate Lawson 		if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
104573347b07SNate Lawson 			cfcount++;
104673347b07SNate Lawson 	}
10470325089dSNate Lawson 	if (cfcount <= 1)
104873347b07SNate Lawson 		device_delete_child(device_get_parent(cf_dev), cf_dev);
104973347b07SNate Lawson 	free(devs, M_TEMP);
105073347b07SNate Lawson 
105173347b07SNate Lawson 	return (0);
105273347b07SNate Lawson }
10530d4ac62aSNate Lawson 
10540d4ac62aSNate Lawson int
10550d4ac62aSNate Lawson cpufreq_settings_changed(device_t dev)
10560d4ac62aSNate Lawson {
10570d4ac62aSNate Lawson 
10580d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_levels_changed,
10590d4ac62aSNate Lawson 	    device_get_unit(device_get_parent(dev)));
10600d4ac62aSNate Lawson 	return (0);
10610d4ac62aSNate Lawson }
1062