xref: /freebsd/sys/kern/kern_cpu.c (revision af3b2549c4ba2ef00a7cbb4cb6836598bf0aefbe)
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;
79dac50931SAndriy Gapon 	struct cf_level			*levels_buf;
8073347b07SNate Lawson };
8173347b07SNate Lawson 
8273347b07SNate Lawson struct cf_setting_array {
8373347b07SNate Lawson 	struct cf_setting		sets[MAX_SETTINGS];
8473347b07SNate Lawson 	int				count;
8573347b07SNate Lawson 	TAILQ_ENTRY(cf_setting_array)	link;
8673347b07SNate Lawson };
8773347b07SNate Lawson 
8873347b07SNate Lawson TAILQ_HEAD(cf_setting_lst, cf_setting_array);
8973347b07SNate Lawson 
90789f03ceSNate Lawson #define CF_MTX_INIT(x)		sx_init((x), "cpufreq lock")
91789f03ceSNate Lawson #define CF_MTX_LOCK(x)		sx_xlock((x))
92789f03ceSNate Lawson #define CF_MTX_UNLOCK(x)	sx_xunlock((x))
93789f03ceSNate Lawson #define CF_MTX_ASSERT(x)	sx_assert((x), SX_XLOCKED)
94789f03ceSNate Lawson 
958d913481SNate Lawson #define CF_DEBUG(msg...)	do {		\
968d913481SNate Lawson 	if (cf_verbose)				\
978d913481SNate Lawson 		printf("cpufreq: " msg);	\
988d913481SNate Lawson 	} while (0)
998d913481SNate Lawson 
10073347b07SNate Lawson static int	cpufreq_attach(device_t dev);
1010d4ac62aSNate Lawson static void	cpufreq_startup_task(void *ctx, int pending);
10273347b07SNate Lawson static int	cpufreq_detach(device_t dev);
10373347b07SNate Lawson static int	cf_set_method(device_t dev, const struct cf_level *level,
10473347b07SNate Lawson 		    int priority);
10573347b07SNate Lawson static int	cf_get_method(device_t dev, struct cf_level *level);
10673347b07SNate Lawson static int	cf_levels_method(device_t dev, struct cf_level *levels,
10773347b07SNate Lawson 		    int *count);
10888c9b54cSNate Lawson static int	cpufreq_insert_abs(struct cpufreq_softc *sc,
10973347b07SNate Lawson 		    struct cf_setting *sets, int count);
11088c9b54cSNate Lawson static int	cpufreq_expand_set(struct cpufreq_softc *sc,
11188c9b54cSNate Lawson 		    struct cf_setting_array *set_arr);
11288c9b54cSNate Lawson static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
11388c9b54cSNate Lawson 		    struct cf_level *dup, struct cf_setting *set);
11473347b07SNate Lawson static int	cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
11573347b07SNate Lawson static int	cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
116e959a70bSNate Lawson static int	cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
11773347b07SNate Lawson 
11873347b07SNate Lawson static device_method_t cpufreq_methods[] = {
11973347b07SNate Lawson 	DEVMETHOD(device_probe,		bus_generic_probe),
12073347b07SNate Lawson 	DEVMETHOD(device_attach,	cpufreq_attach),
12173347b07SNate Lawson 	DEVMETHOD(device_detach,	cpufreq_detach),
12273347b07SNate Lawson 
12373347b07SNate Lawson         DEVMETHOD(cpufreq_set,		cf_set_method),
12473347b07SNate Lawson         DEVMETHOD(cpufreq_get,		cf_get_method),
12573347b07SNate Lawson         DEVMETHOD(cpufreq_levels,	cf_levels_method),
12673347b07SNate Lawson 	{0, 0}
12773347b07SNate Lawson };
12873347b07SNate Lawson static driver_t cpufreq_driver = {
12973347b07SNate Lawson 	"cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
13073347b07SNate Lawson };
13173347b07SNate Lawson static devclass_t cpufreq_dc;
13273347b07SNate Lawson DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
13373347b07SNate Lawson 
134b070969bSNate Lawson static int		cf_lowest_freq;
1358d913481SNate Lawson static int		cf_verbose;
1366472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL,
1376472ac3dSEd Schouten     "cpufreq debugging");
138*af3b2549SHans Petter Selasky SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RWTUN, &cf_lowest_freq, 1,
139b070969bSNate Lawson     "Don't provide levels below this frequency.");
140*af3b2549SHans Petter Selasky SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RWTUN, &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);
161f436f175SNathan Whitehorn 	/* Try to get nominal CPU freq to use it as maximum later if needed */
162f436f175SNathan Whitehorn 	sc->max_mhz = cpu_get_nominal_mhz(dev);
163f436f175SNathan Whitehorn 	/* If that fails, try to measure the current rate */
164f436f175SNathan Whitehorn 	if (sc->max_mhz <= 0) {
165d288bcc4SAlexander Motin 		pc = cpu_get_pcpu(dev);
166d288bcc4SAlexander Motin 		if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
167d288bcc4SAlexander Motin 			sc->max_mhz = rate / 1000000;
168d288bcc4SAlexander Motin 		else
16967c8649fSNate Lawson 			sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
170f436f175SNathan Whitehorn 	}
17173347b07SNate Lawson 
17273347b07SNate Lawson 	/*
17373347b07SNate Lawson 	 * Only initialize one set of sysctls for all CPUs.  In the future,
17473347b07SNate Lawson 	 * if multiple CPUs can have different settings, we can move these
17573347b07SNate Lawson 	 * sysctls to be under every CPU instead of just the first one.
17673347b07SNate Lawson 	 */
17773347b07SNate Lawson 	numdevs = devclass_get_count(cpufreq_dc);
17873347b07SNate Lawson 	if (numdevs > 1)
17973347b07SNate Lawson 		return (0);
18073347b07SNate Lawson 
1818d913481SNate Lawson 	CF_DEBUG("initializing one-time data for %s\n",
1828d913481SNate Lawson 	    device_get_nameunit(dev));
183dac50931SAndriy Gapon 	sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf),
184dac50931SAndriy Gapon 	    M_DEVBUF, M_WAITOK);
18573347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
18673347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
18773347b07SNate Lawson 	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
18873347b07SNate Lawson 	    cpufreq_curr_sysctl, "I", "Current CPU frequency");
18973347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
19073347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
19173347b07SNate Lawson 	    OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
19273347b07SNate Lawson 	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
1930d4ac62aSNate Lawson 
1940d4ac62aSNate Lawson 	/*
1950d4ac62aSNate Lawson 	 * Queue a one-shot broadcast that levels have changed.
1960d4ac62aSNate Lawson 	 * It will run once the system has completed booting.
1970d4ac62aSNate Lawson 	 */
1980d4ac62aSNate Lawson 	TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
1990d4ac62aSNate Lawson 	taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
20073347b07SNate Lawson 
20173347b07SNate Lawson 	return (0);
20273347b07SNate Lawson }
20373347b07SNate Lawson 
2040d4ac62aSNate Lawson /* Handle any work to be done for all drivers that attached during boot. */
2050d4ac62aSNate Lawson static void
2060d4ac62aSNate Lawson cpufreq_startup_task(void *ctx, int pending)
2070d4ac62aSNate Lawson {
2080d4ac62aSNate Lawson 
2090d4ac62aSNate Lawson 	cpufreq_settings_changed((device_t)ctx);
2100d4ac62aSNate Lawson }
2110d4ac62aSNate Lawson 
21273347b07SNate Lawson static int
21373347b07SNate Lawson cpufreq_detach(device_t dev)
21473347b07SNate Lawson {
21573347b07SNate Lawson 	struct cpufreq_softc *sc;
21656e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq;
21773347b07SNate Lawson 	int numdevs;
21873347b07SNate Lawson 
2198d913481SNate Lawson 	CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
22073347b07SNate Lawson 	sc = device_get_softc(dev);
22173347b07SNate Lawson 	sysctl_ctx_free(&sc->sysctl_ctx);
22273347b07SNate Lawson 
22356e5a87aSHajimu UMEMOTO 	while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
22456e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
22556e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
22656e5a87aSHajimu UMEMOTO 	}
22756e5a87aSHajimu UMEMOTO 
22873347b07SNate Lawson 	/* Only clean up these resources when the last device is detaching. */
22973347b07SNate Lawson 	numdevs = devclass_get_count(cpufreq_dc);
2308d913481SNate Lawson 	if (numdevs == 1) {
2318d913481SNate Lawson 		CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
232dac50931SAndriy Gapon 		free(sc->levels_buf, M_DEVBUF);
2338d913481SNate Lawson 	}
23473347b07SNate Lawson 
23573347b07SNate Lawson 	return (0);
23673347b07SNate Lawson }
23773347b07SNate Lawson 
23873347b07SNate Lawson static int
23973347b07SNate Lawson cf_set_method(device_t dev, const struct cf_level *level, int priority)
24073347b07SNate Lawson {
24173347b07SNate Lawson 	struct cpufreq_softc *sc;
24273347b07SNate Lawson 	const struct cf_setting *set;
24356e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq, *curr_freq;
2440325089dSNate Lawson 	struct pcpu *pc;
24562db376aSNate Lawson 	int error, i;
24673347b07SNate Lawson 
24773347b07SNate Lawson 	sc = device_get_softc(dev);
248789f03ceSNate Lawson 	error = 0;
249789f03ceSNate Lawson 	set = NULL;
25056e5a87aSHajimu UMEMOTO 	saved_freq = NULL;
25173347b07SNate Lawson 
2520d4ac62aSNate Lawson 	/* We are going to change levels so notify the pre-change handler. */
2530d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
2540d4ac62aSNate Lawson 	if (error != 0) {
2550d4ac62aSNate Lawson 		EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
2560d4ac62aSNate Lawson 		return (error);
257b4130b8aSMarcus Alves Grando 	}
2580325089dSNate Lawson 
259789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
26056e5a87aSHajimu UMEMOTO 
261a15e947dSNate Lawson #ifdef SMP
262a15e947dSNate Lawson 	/*
263a15e947dSNate Lawson 	 * If still booting and secondary CPUs not started yet, don't allow
264a15e947dSNate Lawson 	 * changing the frequency until they're online.  This is because we
265a15e947dSNate Lawson 	 * can't switch to them using sched_bind() and thus we'd only be
266a15e947dSNate Lawson 	 * switching the main CPU.  XXXTODO: Need to think more about how to
267a15e947dSNate Lawson 	 * handle having different CPUs at different frequencies.
268a15e947dSNate Lawson 	 */
26960ad8150SScott Long 	if (mp_ncpus > 1 && !smp_started) {
270a15e947dSNate Lawson 		device_printf(dev, "rejecting change, SMP not started yet\n");
271a15e947dSNate Lawson 		error = ENXIO;
272a15e947dSNate Lawson 		goto out;
273a15e947dSNate Lawson 	}
274a15e947dSNate Lawson #endif /* SMP */
275a15e947dSNate Lawson 
27656e5a87aSHajimu UMEMOTO 	/*
27756e5a87aSHajimu UMEMOTO 	 * If the requested level has a lower priority, don't allow
27856e5a87aSHajimu UMEMOTO 	 * the new level right now.
27956e5a87aSHajimu UMEMOTO 	 */
28056e5a87aSHajimu UMEMOTO 	if (priority < sc->curr_priority) {
2818d913481SNate Lawson 		CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
2828d913481SNate Lawson 		    sc->curr_priority);
283789f03ceSNate Lawson 		error = EPERM;
284789f03ceSNate Lawson 		goto out;
285789f03ceSNate Lawson 	}
2865f0afa04SNate Lawson 
28756e5a87aSHajimu UMEMOTO 	/*
28856e5a87aSHajimu UMEMOTO 	 * If the caller didn't specify a level and one is saved, prepare to
28956e5a87aSHajimu UMEMOTO 	 * restore the saved level.  If none has been saved, return an error.
29056e5a87aSHajimu UMEMOTO 	 */
29156e5a87aSHajimu UMEMOTO 	if (level == NULL) {
29256e5a87aSHajimu UMEMOTO 		saved_freq = SLIST_FIRST(&sc->saved_freq);
29356e5a87aSHajimu UMEMOTO 		if (saved_freq == NULL) {
29456e5a87aSHajimu UMEMOTO 			CF_DEBUG("NULL level, no saved level\n");
29556e5a87aSHajimu UMEMOTO 			error = ENXIO;
29656e5a87aSHajimu UMEMOTO 			goto out;
29756e5a87aSHajimu UMEMOTO 		}
29856e5a87aSHajimu UMEMOTO 		level = &saved_freq->level;
29956e5a87aSHajimu UMEMOTO 		priority = saved_freq->priority;
30056e5a87aSHajimu UMEMOTO 		CF_DEBUG("restoring saved level, freq %d prio %d\n",
30156e5a87aSHajimu UMEMOTO 		    level->total_set.freq, priority);
30256e5a87aSHajimu UMEMOTO 	}
30356e5a87aSHajimu UMEMOTO 
304b070969bSNate Lawson 	/* Reject levels that are below our specified threshold. */
305da8a77c1SNate Lawson 	if (level->total_set.freq < cf_lowest_freq) {
3068d913481SNate Lawson 		CF_DEBUG("rejecting freq %d, less than %d limit\n",
3078d913481SNate Lawson 		    level->total_set.freq, cf_lowest_freq);
308789f03ceSNate Lawson 		error = EINVAL;
309789f03ceSNate Lawson 		goto out;
310789f03ceSNate Lawson 	}
311b070969bSNate Lawson 
31273347b07SNate Lawson 	/* If already at this level, just return. */
3135f3818a5SAlexander Motin 	if (sc->curr_level.total_set.freq == level->total_set.freq) {
3148d913481SNate Lawson 		CF_DEBUG("skipping freq %d, same as current level %d\n",
3158d913481SNate Lawson 		    level->total_set.freq, sc->curr_level.total_set.freq);
3161fea6ce7SHajimu UMEMOTO 		goto skip;
3178d913481SNate Lawson 	}
31873347b07SNate Lawson 
31973347b07SNate Lawson 	/* First, set the absolute frequency via its driver. */
32073347b07SNate Lawson 	set = &level->abs_set;
32173347b07SNate Lawson 	if (set->dev) {
32273347b07SNate Lawson 		if (!device_is_attached(set->dev)) {
32373347b07SNate Lawson 			error = ENXIO;
32473347b07SNate Lawson 			goto out;
32573347b07SNate Lawson 		}
3261196826aSNate Lawson 
32762db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3281196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
329982d11f8SJeff Roberson 		thread_lock(curthread);
3301196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
331982d11f8SJeff Roberson 		thread_unlock(curthread);
3328d913481SNate Lawson 		CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
3338d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
33473347b07SNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
335982d11f8SJeff Roberson 		thread_lock(curthread);
3361196826aSNate Lawson 		sched_unbind(curthread);
337982d11f8SJeff Roberson 		thread_unlock(curthread);
33873347b07SNate Lawson 		if (error) {
33973347b07SNate Lawson 			goto out;
34073347b07SNate Lawson 		}
34173347b07SNate Lawson 	}
34273347b07SNate Lawson 
34388c9b54cSNate Lawson 	/* Next, set any/all relative frequencies via their drivers. */
34488c9b54cSNate Lawson 	for (i = 0; i < level->rel_count; i++) {
34588c9b54cSNate Lawson 		set = &level->rel_set[i];
34688c9b54cSNate Lawson 		if (!device_is_attached(set->dev)) {
34788c9b54cSNate Lawson 			error = ENXIO;
34888c9b54cSNate Lawson 			goto out;
34988c9b54cSNate Lawson 		}
3501196826aSNate Lawson 
35162db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3521196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
353982d11f8SJeff Roberson 		thread_lock(curthread);
3541196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
355982d11f8SJeff Roberson 		thread_unlock(curthread);
3568d913481SNate Lawson 		CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
3578d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
35888c9b54cSNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
359982d11f8SJeff Roberson 		thread_lock(curthread);
3601196826aSNate Lawson 		sched_unbind(curthread);
361982d11f8SJeff Roberson 		thread_unlock(curthread);
36288c9b54cSNate Lawson 		if (error) {
36388c9b54cSNate Lawson 			/* XXX Back out any successful setting? */
36488c9b54cSNate Lawson 			goto out;
36588c9b54cSNate Lawson 		}
36688c9b54cSNate Lawson 	}
36773347b07SNate Lawson 
3681fea6ce7SHajimu UMEMOTO skip:
3695f0afa04SNate Lawson 	/*
3705f0afa04SNate Lawson 	 * Before recording the current level, check if we're going to a
37156e5a87aSHajimu UMEMOTO 	 * higher priority.  If so, save the previous level and priority.
3725f0afa04SNate Lawson 	 */
3735f0afa04SNate Lawson 	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
37456e5a87aSHajimu UMEMOTO 	    priority > sc->curr_priority) {
3758d913481SNate Lawson 		CF_DEBUG("saving level, freq %d prio %d\n",
3768d913481SNate Lawson 		    sc->curr_level.total_set.freq, sc->curr_priority);
37756e5a87aSHajimu UMEMOTO 		curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
37856e5a87aSHajimu UMEMOTO 		if (curr_freq == NULL) {
37956e5a87aSHajimu UMEMOTO 			error = ENOMEM;
38056e5a87aSHajimu UMEMOTO 			goto out;
38156e5a87aSHajimu UMEMOTO 		}
38256e5a87aSHajimu UMEMOTO 		curr_freq->level = sc->curr_level;
38356e5a87aSHajimu UMEMOTO 		curr_freq->priority = sc->curr_priority;
38456e5a87aSHajimu UMEMOTO 		SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
3855f0afa04SNate Lawson 	}
38673347b07SNate Lawson 	sc->curr_level = *level;
3875f0afa04SNate Lawson 	sc->curr_priority = priority;
38873347b07SNate Lawson 
38956e5a87aSHajimu UMEMOTO 	/* If we were restoring a saved state, reset it to "unused". */
39056e5a87aSHajimu UMEMOTO 	if (saved_freq != NULL) {
39156e5a87aSHajimu UMEMOTO 		CF_DEBUG("resetting saved level\n");
39256e5a87aSHajimu UMEMOTO 		sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
39356e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
39456e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
39556e5a87aSHajimu UMEMOTO 	}
39656e5a87aSHajimu UMEMOTO 
39773347b07SNate Lawson out:
398789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
3990d4ac62aSNate Lawson 
4000d4ac62aSNate Lawson 	/*
4010d4ac62aSNate Lawson 	 * We changed levels (or attempted to) so notify the post-change
4020d4ac62aSNate Lawson 	 * handler of new frequency or error.
4030d4ac62aSNate Lawson 	 */
4040d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
405789f03ceSNate Lawson 	if (error && set)
40673347b07SNate Lawson 		device_printf(set->dev, "set freq failed, err %d\n", error);
4070d4ac62aSNate Lawson 
40873347b07SNate Lawson 	return (error);
40973347b07SNate Lawson }
41073347b07SNate Lawson 
41173347b07SNate Lawson static int
41273347b07SNate Lawson cf_get_method(device_t dev, struct cf_level *level)
41373347b07SNate Lawson {
41473347b07SNate Lawson 	struct cpufreq_softc *sc;
41573347b07SNate Lawson 	struct cf_level *levels;
41673347b07SNate Lawson 	struct cf_setting *curr_set, set;
41773347b07SNate Lawson 	struct pcpu *pc;
41873347b07SNate Lawson 	device_t *devs;
419760f4decSColin Percival 	int bdiff, count, diff, error, i, n, numdevs;
42073347b07SNate Lawson 	uint64_t rate;
42173347b07SNate Lawson 
42273347b07SNate Lawson 	sc = device_get_softc(dev);
423789f03ceSNate Lawson 	error = 0;
42473347b07SNate Lawson 	levels = NULL;
42573347b07SNate Lawson 
42673347b07SNate Lawson 	/* If we already know the current frequency, we're done. */
427789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
428789f03ceSNate Lawson 	curr_set = &sc->curr_level.total_set;
4298d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4308d913481SNate Lawson 		CF_DEBUG("get returning known freq %d\n", curr_set->freq);
43173347b07SNate Lawson 		goto out;
4328d913481SNate Lawson 	}
433789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
43473347b07SNate Lawson 
43573347b07SNate Lawson 	/*
43673347b07SNate Lawson 	 * We need to figure out the current level.  Loop through every
43773347b07SNate Lawson 	 * driver, getting the current setting.  Then, attempt to get a best
43873347b07SNate Lawson 	 * match of settings against each level.
43973347b07SNate Lawson 	 */
44073347b07SNate Lawson 	count = CF_MAX_LEVELS;
44173347b07SNate Lawson 	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
44273347b07SNate Lawson 	if (levels == NULL)
44373347b07SNate Lawson 		return (ENOMEM);
44473347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
445d269386aSNate Lawson 	if (error) {
446d269386aSNate Lawson 		if (error == E2BIG)
447d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
448789f03ceSNate Lawson 		free(levels, M_TEMP);
449789f03ceSNate Lawson 		return (error);
450d269386aSNate Lawson 	}
45173347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
452789f03ceSNate Lawson 	if (error) {
453789f03ceSNate Lawson 		free(levels, M_TEMP);
454789f03ceSNate Lawson 		return (error);
455789f03ceSNate Lawson 	}
456789f03ceSNate Lawson 
457789f03ceSNate Lawson 	/*
458789f03ceSNate Lawson 	 * Reacquire the lock and search for the given level.
459789f03ceSNate Lawson 	 *
460789f03ceSNate Lawson 	 * XXX Note: this is not quite right since we really need to go
461789f03ceSNate Lawson 	 * through each level and compare both absolute and relative
462789f03ceSNate Lawson 	 * settings for each driver in the system before making a match.
463789f03ceSNate Lawson 	 * The estimation code below catches this case though.
464789f03ceSNate Lawson 	 */
465789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
4662145b9d2SNate Lawson 	for (n = 0; n < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; n++) {
4672145b9d2SNate Lawson 		if (!device_is_attached(devs[n]))
46873347b07SNate Lawson 			continue;
469be00f605SJohn Baldwin 		if (CPUFREQ_DRV_GET(devs[n], &set) != 0)
47073347b07SNate Lawson 			continue;
47173347b07SNate Lawson 		for (i = 0; i < count; i++) {
4725f3818a5SAlexander Motin 			if (set.freq == levels[i].total_set.freq) {
47373347b07SNate Lawson 				sc->curr_level = levels[i];
47473347b07SNate Lawson 				break;
47573347b07SNate Lawson 			}
47673347b07SNate Lawson 		}
47773347b07SNate Lawson 	}
47873347b07SNate Lawson 	free(devs, M_TEMP);
4798d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4808d913481SNate Lawson 		CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
48173347b07SNate Lawson 		goto out;
4828d913481SNate Lawson 	}
48373347b07SNate Lawson 
48473347b07SNate Lawson 	/*
48573347b07SNate Lawson 	 * We couldn't find an exact match, so attempt to estimate and then
48673347b07SNate Lawson 	 * match against a level.
48773347b07SNate Lawson 	 */
48873347b07SNate Lawson 	pc = cpu_get_pcpu(dev);
48973347b07SNate Lawson 	if (pc == NULL) {
49073347b07SNate Lawson 		error = ENXIO;
49173347b07SNate Lawson 		goto out;
49273347b07SNate Lawson 	}
49373347b07SNate Lawson 	cpu_est_clockrate(pc->pc_cpuid, &rate);
49473347b07SNate Lawson 	rate /= 1000000;
495760f4decSColin Percival 	bdiff = 1 << 30;
49673347b07SNate Lawson 	for (i = 0; i < count; i++) {
497760f4decSColin Percival 		diff = abs(levels[i].total_set.freq - rate);
498760f4decSColin Percival 		if (diff < bdiff) {
499760f4decSColin Percival 			bdiff = diff;
50073347b07SNate Lawson 			sc->curr_level = levels[i];
501760f4decSColin Percival 		}
502760f4decSColin Percival 	}
5038d913481SNate Lawson 	CF_DEBUG("get estimated freq %d\n", curr_set->freq);
50473347b07SNate Lawson 
50573347b07SNate Lawson out:
506789f03ceSNate Lawson 	if (error == 0)
507789f03ceSNate Lawson 		*level = sc->curr_level;
508789f03ceSNate Lawson 
509789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
51073347b07SNate Lawson 	if (levels)
51173347b07SNate Lawson 		free(levels, M_TEMP);
512789f03ceSNate Lawson 	return (error);
51373347b07SNate Lawson }
51473347b07SNate Lawson 
51573347b07SNate Lawson static int
51673347b07SNate Lawson cf_levels_method(device_t dev, struct cf_level *levels, int *count)
51773347b07SNate Lawson {
51888c9b54cSNate Lawson 	struct cf_setting_array *set_arr;
51973347b07SNate Lawson 	struct cf_setting_lst rel_sets;
52073347b07SNate Lawson 	struct cpufreq_softc *sc;
52173347b07SNate Lawson 	struct cf_level *lev;
52273347b07SNate Lawson 	struct cf_setting *sets;
52373347b07SNate Lawson 	struct pcpu *pc;
52473347b07SNate Lawson 	device_t *devs;
52588c9b54cSNate Lawson 	int error, i, numdevs, set_count, type;
52673347b07SNate Lawson 	uint64_t rate;
52773347b07SNate Lawson 
52873347b07SNate Lawson 	if (levels == NULL || count == NULL)
52973347b07SNate Lawson 		return (EINVAL);
53073347b07SNate Lawson 
53173347b07SNate Lawson 	TAILQ_INIT(&rel_sets);
53273347b07SNate Lawson 	sc = device_get_softc(dev);
53373347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
53473347b07SNate Lawson 	if (error)
53573347b07SNate Lawson 		return (error);
53673347b07SNate Lawson 	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
53773347b07SNate Lawson 	if (sets == NULL) {
53873347b07SNate Lawson 		free(devs, M_TEMP);
53973347b07SNate Lawson 		return (ENOMEM);
54073347b07SNate Lawson 	}
54173347b07SNate Lawson 
54273347b07SNate Lawson 	/* Get settings from all cpufreq drivers. */
543789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
54473347b07SNate Lawson 	for (i = 0; i < numdevs; i++) {
545e22cd41cSNate Lawson 		/* Skip devices that aren't ready. */
54673347b07SNate Lawson 		if (!device_is_attached(devs[i]))
54773347b07SNate Lawson 			continue;
548e22cd41cSNate Lawson 
549e22cd41cSNate Lawson 		/*
550e22cd41cSNate Lawson 		 * Get settings, skipping drivers that offer no settings or
551e22cd41cSNate Lawson 		 * provide settings for informational purposes only.
552e22cd41cSNate Lawson 		 */
553e94a0c1aSNate Lawson 		error = CPUFREQ_DRV_TYPE(devs[i], &type);
5548d913481SNate Lawson 		if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
5558d913481SNate Lawson 			if (error == 0) {
5568d913481SNate Lawson 				CF_DEBUG("skipping info-only driver %s\n",
5578d913481SNate Lawson 				    device_get_nameunit(devs[i]));
5588d913481SNate Lawson 			}
559e94a0c1aSNate Lawson 			continue;
5608d913481SNate Lawson 		}
56173347b07SNate Lawson 		set_count = MAX_SETTINGS;
562e94a0c1aSNate Lawson 		error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
563e94a0c1aSNate Lawson 		if (error || set_count == 0)
56473347b07SNate Lawson 			continue;
56573347b07SNate Lawson 
566e22cd41cSNate Lawson 		/* Add the settings to our absolute/relative lists. */
5670325089dSNate Lawson 		switch (type & CPUFREQ_TYPE_MASK) {
56888c9b54cSNate Lawson 		case CPUFREQ_TYPE_ABSOLUTE:
56988c9b54cSNate Lawson 			error = cpufreq_insert_abs(sc, sets, set_count);
57088c9b54cSNate Lawson 			break;
57188c9b54cSNate Lawson 		case CPUFREQ_TYPE_RELATIVE:
5728d913481SNate Lawson 			CF_DEBUG("adding %d relative settings\n", set_count);
57388c9b54cSNate Lawson 			set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
57488c9b54cSNate Lawson 			if (set_arr == NULL) {
57588c9b54cSNate Lawson 				error = ENOMEM;
57688c9b54cSNate Lawson 				goto out;
57788c9b54cSNate Lawson 			}
57888c9b54cSNate Lawson 			bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
57988c9b54cSNate Lawson 			set_arr->count = set_count;
58088c9b54cSNate Lawson 			TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
58188c9b54cSNate Lawson 			break;
58288c9b54cSNate Lawson 		default:
58388c9b54cSNate Lawson 			error = EINVAL;
58488c9b54cSNate Lawson 		}
58588c9b54cSNate Lawson 		if (error)
58673347b07SNate Lawson 			goto out;
58773347b07SNate Lawson 	}
58873347b07SNate Lawson 
58967c8649fSNate Lawson 	/*
59067c8649fSNate Lawson 	 * If there are no absolute levels, create a fake one at 100%.  We
59167c8649fSNate Lawson 	 * then cache the clockrate for later use as our base frequency.
59267c8649fSNate Lawson 	 */
59373347b07SNate Lawson 	if (TAILQ_EMPTY(&sc->all_levels)) {
59467c8649fSNate Lawson 		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
595f436f175SNathan Whitehorn 			sc->max_mhz = cpu_get_nominal_mhz(dev);
596f436f175SNathan Whitehorn 			/*
597f436f175SNathan Whitehorn 			 * If the CPU can't report a rate for 100%, hope
598f436f175SNathan Whitehorn 			 * the CPU is running at its nominal rate right now,
599f436f175SNathan Whitehorn 			 * and use that instead.
600f436f175SNathan Whitehorn 			 */
601f436f175SNathan Whitehorn 			if (sc->max_mhz <= 0) {
60273347b07SNate Lawson 				pc = cpu_get_pcpu(dev);
60373347b07SNate Lawson 				cpu_est_clockrate(pc->pc_cpuid, &rate);
60467c8649fSNate Lawson 				sc->max_mhz = rate / 1000000;
60567c8649fSNate Lawson 			}
606f436f175SNathan Whitehorn 		}
60767c8649fSNate Lawson 		memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
60867c8649fSNate Lawson 		sets[0].freq = sc->max_mhz;
60967c8649fSNate Lawson 		sets[0].dev = NULL;
61088c9b54cSNate Lawson 		error = cpufreq_insert_abs(sc, sets, 1);
61173347b07SNate Lawson 		if (error)
61273347b07SNate Lawson 			goto out;
61373347b07SNate Lawson 	}
61473347b07SNate Lawson 
61588c9b54cSNate Lawson 	/* Create a combined list of absolute + relative levels. */
61688c9b54cSNate Lawson 	TAILQ_FOREACH(set_arr, &rel_sets, link)
61788c9b54cSNate Lawson 		cpufreq_expand_set(sc, set_arr);
61888c9b54cSNate Lawson 
61988c9b54cSNate Lawson 	/* If the caller doesn't have enough space, return the actual count. */
62088c9b54cSNate Lawson 	if (sc->all_count > *count) {
62188c9b54cSNate Lawson 		*count = sc->all_count;
62288c9b54cSNate Lawson 		error = E2BIG;
62388c9b54cSNate Lawson 		goto out;
62488c9b54cSNate Lawson 	}
62588c9b54cSNate Lawson 
62688c9b54cSNate Lawson 	/* Finally, output the list of levels. */
62773347b07SNate Lawson 	i = 0;
62873347b07SNate Lawson 	TAILQ_FOREACH(lev, &sc->all_levels, link) {
629e1f13773SNate Lawson 
630b070969bSNate Lawson 		/* Skip levels that have a frequency that is too low. */
631da8a77c1SNate Lawson 		if (lev->total_set.freq < cf_lowest_freq) {
632b070969bSNate Lawson 			sc->all_count--;
633b070969bSNate Lawson 			continue;
634b070969bSNate Lawson 		}
635b070969bSNate Lawson 
63673347b07SNate Lawson 		levels[i] = *lev;
63773347b07SNate Lawson 		i++;
63873347b07SNate Lawson 	}
63988c9b54cSNate Lawson 	*count = sc->all_count;
64073347b07SNate Lawson 	error = 0;
64173347b07SNate Lawson 
64273347b07SNate Lawson out:
64373347b07SNate Lawson 	/* Clear all levels since we regenerate them each time. */
64473347b07SNate Lawson 	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
64573347b07SNate Lawson 		TAILQ_REMOVE(&sc->all_levels, lev, link);
64673347b07SNate Lawson 		free(lev, M_TEMP);
64773347b07SNate Lawson 	}
648789f03ceSNate Lawson 	sc->all_count = 0;
649789f03ceSNate Lawson 
650789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
65188c9b54cSNate Lawson 	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
65288c9b54cSNate Lawson 		TAILQ_REMOVE(&rel_sets, set_arr, link);
65388c9b54cSNate Lawson 		free(set_arr, M_TEMP);
65488c9b54cSNate Lawson 	}
65573347b07SNate Lawson 	free(devs, M_TEMP);
65673347b07SNate Lawson 	free(sets, M_TEMP);
65773347b07SNate Lawson 	return (error);
65873347b07SNate Lawson }
65973347b07SNate Lawson 
66073347b07SNate Lawson /*
66173347b07SNate Lawson  * Create levels for an array of absolute settings and insert them in
66273347b07SNate Lawson  * sorted order in the specified list.
66373347b07SNate Lawson  */
66473347b07SNate Lawson static int
66588c9b54cSNate Lawson cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
66673347b07SNate Lawson     int count)
66773347b07SNate Lawson {
66888c9b54cSNate Lawson 	struct cf_level_lst *list;
66973347b07SNate Lawson 	struct cf_level *level, *search;
67073347b07SNate Lawson 	int i;
67173347b07SNate Lawson 
672789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
673789f03ceSNate Lawson 
67488c9b54cSNate Lawson 	list = &sc->all_levels;
67573347b07SNate Lawson 	for (i = 0; i < count; i++) {
67673347b07SNate Lawson 		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
67773347b07SNate Lawson 		if (level == NULL)
67873347b07SNate Lawson 			return (ENOMEM);
67973347b07SNate Lawson 		level->abs_set = sets[i];
68088c9b54cSNate Lawson 		level->total_set = sets[i];
68188c9b54cSNate Lawson 		level->total_set.dev = NULL;
68288c9b54cSNate Lawson 		sc->all_count++;
68373347b07SNate Lawson 
68473347b07SNate Lawson 		if (TAILQ_EMPTY(list)) {
6858d913481SNate Lawson 			CF_DEBUG("adding abs setting %d at head\n",
6868d913481SNate Lawson 			    sets[i].freq);
68773347b07SNate Lawson 			TAILQ_INSERT_HEAD(list, level, link);
68873347b07SNate Lawson 			continue;
68973347b07SNate Lawson 		}
69073347b07SNate Lawson 
69173347b07SNate Lawson 		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
69288c9b54cSNate Lawson 			if (sets[i].freq <= search->total_set.freq) {
6938d913481SNate Lawson 				CF_DEBUG("adding abs setting %d after %d\n",
6948d913481SNate Lawson 				    sets[i].freq, search->total_set.freq);
69573347b07SNate Lawson 				TAILQ_INSERT_AFTER(list, search, level, link);
69673347b07SNate Lawson 				break;
69773347b07SNate Lawson 			}
69873347b07SNate Lawson 		}
69973347b07SNate Lawson 	}
70073347b07SNate Lawson 	return (0);
70173347b07SNate Lawson }
70273347b07SNate Lawson 
70388c9b54cSNate Lawson /*
70488c9b54cSNate Lawson  * Expand a group of relative settings, creating derived levels from them.
70588c9b54cSNate Lawson  */
70688c9b54cSNate Lawson static int
70788c9b54cSNate Lawson cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
70888c9b54cSNate Lawson {
70988c9b54cSNate Lawson 	struct cf_level *fill, *search;
71088c9b54cSNate Lawson 	struct cf_setting *set;
71188c9b54cSNate Lawson 	int i;
71288c9b54cSNate Lawson 
713789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
714789f03ceSNate Lawson 
7155308b2a6SNate Lawson 	/*
7165308b2a6SNate Lawson 	 * Walk the set of all existing levels in reverse.  This is so we
7175308b2a6SNate Lawson 	 * create derived states from the lowest absolute settings first
7185308b2a6SNate Lawson 	 * and discard duplicates created from higher absolute settings.
7195308b2a6SNate Lawson 	 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
7205308b2a6SNate Lawson 	 * preferable to 200 Mhz + 25% because absolute settings are more
7215308b2a6SNate Lawson 	 * efficient since they often change the voltage as well.
7225308b2a6SNate Lawson 	 */
7235308b2a6SNate Lawson 	TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
72488c9b54cSNate Lawson 		/* Add each setting to the level, duplicating if necessary. */
72588c9b54cSNate Lawson 		for (i = 0; i < set_arr->count; i++) {
72688c9b54cSNate Lawson 			set = &set_arr->sets[i];
72788c9b54cSNate Lawson 
72888c9b54cSNate Lawson 			/*
72988c9b54cSNate Lawson 			 * If this setting is less than 100%, split the level
73088c9b54cSNate Lawson 			 * into two and add this setting to the new level.
73188c9b54cSNate Lawson 			 */
73288c9b54cSNate Lawson 			fill = search;
7335308b2a6SNate Lawson 			if (set->freq < 10000) {
73488c9b54cSNate Lawson 				fill = cpufreq_dup_set(sc, search, set);
73588c9b54cSNate Lawson 
73688c9b54cSNate Lawson 				/*
7375308b2a6SNate Lawson 				 * The new level was a duplicate of an existing
7385308b2a6SNate Lawson 				 * level or its absolute setting is too high
7395308b2a6SNate Lawson 				 * so we freed it.  For example, we discard a
7405308b2a6SNate Lawson 				 * derived level of 1000 MHz/25% if a level
7415308b2a6SNate Lawson 				 * of 500 MHz/100% already exists.
74288c9b54cSNate Lawson 				 */
74388c9b54cSNate Lawson 				if (fill == NULL)
7445308b2a6SNate Lawson 					break;
7455308b2a6SNate Lawson 			}
74688c9b54cSNate Lawson 
74788c9b54cSNate Lawson 			/* Add this setting to the existing or new level. */
74888c9b54cSNate Lawson 			KASSERT(fill->rel_count < MAX_SETTINGS,
74988c9b54cSNate Lawson 			    ("cpufreq: too many relative drivers (%d)",
75088c9b54cSNate Lawson 			    MAX_SETTINGS));
75188c9b54cSNate Lawson 			fill->rel_set[fill->rel_count] = *set;
75288c9b54cSNate Lawson 			fill->rel_count++;
7538d913481SNate Lawson 			CF_DEBUG(
7548d913481SNate Lawson 			"expand set added rel setting %d%% to %d level\n",
7558d913481SNate Lawson 			    set->freq / 100, fill->total_set.freq);
75688c9b54cSNate Lawson 		}
75788c9b54cSNate Lawson 	}
75888c9b54cSNate Lawson 
75988c9b54cSNate Lawson 	return (0);
76088c9b54cSNate Lawson }
76188c9b54cSNate Lawson 
76288c9b54cSNate Lawson static struct cf_level *
76388c9b54cSNate Lawson cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
76488c9b54cSNate Lawson     struct cf_setting *set)
76588c9b54cSNate Lawson {
76688c9b54cSNate Lawson 	struct cf_level_lst *list;
76788c9b54cSNate Lawson 	struct cf_level *fill, *itr;
76888c9b54cSNate Lawson 	struct cf_setting *fill_set, *itr_set;
76988c9b54cSNate Lawson 	int i;
77088c9b54cSNate Lawson 
771789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
772789f03ceSNate Lawson 
77388c9b54cSNate Lawson 	/*
77488c9b54cSNate Lawson 	 * Create a new level, copy it from the old one, and update the
77588c9b54cSNate Lawson 	 * total frequency and power by the percentage specified in the
77688c9b54cSNate Lawson 	 * relative setting.
77788c9b54cSNate Lawson 	 */
77888c9b54cSNate Lawson 	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
77988c9b54cSNate Lawson 	if (fill == NULL)
78088c9b54cSNate Lawson 		return (NULL);
78188c9b54cSNate Lawson 	*fill = *dup;
78288c9b54cSNate Lawson 	fill_set = &fill->total_set;
78388c9b54cSNate Lawson 	fill_set->freq =
78488c9b54cSNate Lawson 	    ((uint64_t)fill_set->freq * set->freq) / 10000;
78588c9b54cSNate Lawson 	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
78688c9b54cSNate Lawson 		fill_set->power = ((uint64_t)fill_set->power * set->freq)
78788c9b54cSNate Lawson 		    / 10000;
78888c9b54cSNate Lawson 	}
78988c9b54cSNate Lawson 	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
79088c9b54cSNate Lawson 		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
79188c9b54cSNate Lawson 			fill_set->lat += set->lat;
79288c9b54cSNate Lawson 		else
79388c9b54cSNate Lawson 			fill_set->lat = set->lat;
79488c9b54cSNate Lawson 	}
7958d913481SNate Lawson 	CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
79688c9b54cSNate Lawson 
79788c9b54cSNate Lawson 	/*
79888c9b54cSNate Lawson 	 * If we copied an old level that we already modified (say, at 100%),
79988c9b54cSNate Lawson 	 * we need to remove that setting before adding this one.  Since we
80088c9b54cSNate Lawson 	 * process each setting array in order, we know any settings for this
80188c9b54cSNate Lawson 	 * driver will be found at the end.
80288c9b54cSNate Lawson 	 */
80388c9b54cSNate Lawson 	for (i = fill->rel_count; i != 0; i--) {
80488c9b54cSNate Lawson 		if (fill->rel_set[i - 1].dev != set->dev)
80588c9b54cSNate Lawson 			break;
8068d913481SNate Lawson 		CF_DEBUG("removed last relative driver: %s\n",
8078d913481SNate Lawson 		    device_get_nameunit(set->dev));
80888c9b54cSNate Lawson 		fill->rel_count--;
80988c9b54cSNate Lawson 	}
81088c9b54cSNate Lawson 
81188c9b54cSNate Lawson 	/*
8125308b2a6SNate Lawson 	 * Insert the new level in sorted order.  If it is a duplicate of an
8135308b2a6SNate Lawson 	 * existing level (1) or has an absolute setting higher than the
8145308b2a6SNate Lawson 	 * existing level (2), do not add it.  We can do this since any such
8155308b2a6SNate Lawson 	 * level is guaranteed use less power.  For example (1), a level with
8165308b2a6SNate Lawson 	 * one absolute setting of 800 Mhz uses less power than one composed
8175308b2a6SNate Lawson 	 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
8185308b2a6SNate Lawson 	 * Also for example (2), a level of 800 Mhz/75% is preferable to
8195308b2a6SNate Lawson 	 * 1600 Mhz/25% even though the latter has a lower total frequency.
82088c9b54cSNate Lawson 	 */
82188c9b54cSNate Lawson 	list = &sc->all_levels;
8225308b2a6SNate Lawson 	KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
82388c9b54cSNate Lawson 	TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
82488c9b54cSNate Lawson 		itr_set = &itr->total_set;
8259000b91eSNate Lawson 		if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
8269000b91eSNate Lawson 			CF_DEBUG("dup set rejecting %d (dupe)\n",
8279000b91eSNate Lawson 			    fill_set->freq);
8289000b91eSNate Lawson 			itr = NULL;
8299000b91eSNate Lawson 			break;
8309000b91eSNate Lawson 		} else if (fill_set->freq < itr_set->freq) {
8319000b91eSNate Lawson 			if (fill->abs_set.freq <= itr->abs_set.freq) {
8328d913481SNate Lawson 				CF_DEBUG(
8338d913481SNate Lawson 			"dup done, inserting new level %d after %d\n",
8348d913481SNate Lawson 				    fill_set->freq, itr_set->freq);
83588c9b54cSNate Lawson 				TAILQ_INSERT_AFTER(list, itr, fill, link);
83688c9b54cSNate Lawson 				sc->all_count++;
8379000b91eSNate Lawson 			} else {
8389000b91eSNate Lawson 				CF_DEBUG("dup set rejecting %d (abs too big)\n",
8399000b91eSNate Lawson 				    fill_set->freq);
8409000b91eSNate Lawson 				itr = NULL;
8419000b91eSNate Lawson 			}
84288c9b54cSNate Lawson 			break;
84388c9b54cSNate Lawson 		}
84488c9b54cSNate Lawson 	}
8455308b2a6SNate Lawson 
8465308b2a6SNate Lawson 	/* We didn't find a good place for this new level so free it. */
8475308b2a6SNate Lawson 	if (itr == NULL) {
8485308b2a6SNate Lawson 		CF_DEBUG("dup set freeing new level %d (not optimal)\n",
8495308b2a6SNate Lawson 		    fill_set->freq);
8505308b2a6SNate Lawson 		free(fill, M_TEMP);
8515308b2a6SNate Lawson 		fill = NULL;
85288c9b54cSNate Lawson 	}
85388c9b54cSNate Lawson 
85488c9b54cSNate Lawson 	return (fill);
85588c9b54cSNate Lawson }
85688c9b54cSNate Lawson 
85773347b07SNate Lawson static int
85873347b07SNate Lawson cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
85973347b07SNate Lawson {
86073347b07SNate Lawson 	struct cpufreq_softc *sc;
86173347b07SNate Lawson 	struct cf_level *levels;
8625f3818a5SAlexander Motin 	int best, count, diff, bdiff, devcount, error, freq, i, n;
8630325089dSNate Lawson 	device_t *devs;
86473347b07SNate Lawson 
8650325089dSNate Lawson 	devs = NULL;
86673347b07SNate Lawson 	sc = oidp->oid_arg1;
867dac50931SAndriy Gapon 	levels = sc->levels_buf;
86873347b07SNate Lawson 
86973347b07SNate Lawson 	error = CPUFREQ_GET(sc->dev, &levels[0]);
87073347b07SNate Lawson 	if (error)
87173347b07SNate Lawson 		goto out;
87273347b07SNate Lawson 	freq = levels[0].total_set.freq;
87373347b07SNate Lawson 	error = sysctl_handle_int(oidp, &freq, 0, req);
87473347b07SNate Lawson 	if (error != 0 || req->newptr == NULL)
87573347b07SNate Lawson 		goto out;
87673347b07SNate Lawson 
8770325089dSNate Lawson 	/*
8780325089dSNate Lawson 	 * While we only call cpufreq_get() on one device (assuming all
8790325089dSNate Lawson 	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
8800325089dSNate Lawson 	 * This is needed for some MP systems.
8810325089dSNate Lawson 	 */
8820325089dSNate Lawson 	error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
88373347b07SNate Lawson 	if (error)
88473347b07SNate Lawson 		goto out;
8850325089dSNate Lawson 	for (n = 0; n < devcount; n++) {
8860325089dSNate Lawson 		count = CF_MAX_LEVELS;
8870325089dSNate Lawson 		error = CPUFREQ_LEVELS(devs[n], levels, &count);
888d269386aSNate Lawson 		if (error) {
889d269386aSNate Lawson 			if (error == E2BIG)
890d269386aSNate Lawson 				printf(
891d269386aSNate Lawson 			"cpufreq: need to increase CF_MAX_LEVELS\n");
8920325089dSNate Lawson 			break;
893d269386aSNate Lawson 		}
8945f3818a5SAlexander Motin 		best = 0;
8955f3818a5SAlexander Motin 		bdiff = 1 << 30;
89673347b07SNate Lawson 		for (i = 0; i < count; i++) {
8975f3818a5SAlexander Motin 			diff = abs(levels[i].total_set.freq - freq);
8985f3818a5SAlexander Motin 			if (diff < bdiff) {
8995f3818a5SAlexander Motin 				bdiff = diff;
9005f3818a5SAlexander Motin 				best = i;
90173347b07SNate Lawson 			}
90273347b07SNate Lawson 		}
9035f3818a5SAlexander Motin 		error = CPUFREQ_SET(devs[n], &levels[best], CPUFREQ_PRIO_USER);
9040325089dSNate Lawson 	}
90573347b07SNate Lawson 
90673347b07SNate Lawson out:
9070325089dSNate Lawson 	if (devs)
9080325089dSNate Lawson 		free(devs, M_TEMP);
90973347b07SNate Lawson 	return (error);
91073347b07SNate Lawson }
91173347b07SNate Lawson 
91273347b07SNate Lawson static int
91373347b07SNate Lawson cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
91473347b07SNate Lawson {
91573347b07SNate Lawson 	struct cpufreq_softc *sc;
91673347b07SNate Lawson 	struct cf_level *levels;
91773347b07SNate Lawson 	struct cf_setting *set;
91873347b07SNate Lawson 	struct sbuf sb;
91973347b07SNate Lawson 	int count, error, i;
92073347b07SNate Lawson 
92173347b07SNate Lawson 	sc = oidp->oid_arg1;
92273347b07SNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
92373347b07SNate Lawson 
92473347b07SNate Lawson 	/* Get settings from the device and generate the output string. */
92573347b07SNate Lawson 	count = CF_MAX_LEVELS;
926dac50931SAndriy Gapon 	levels = sc->levels_buf;
92730215f48SChristian Brueffer 	if (levels == NULL) {
92830215f48SChristian Brueffer 		sbuf_delete(&sb);
92973347b07SNate Lawson 		return (ENOMEM);
93030215f48SChristian Brueffer 	}
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 	sbuf_delete(&sb);
95073347b07SNate Lawson 	return (error);
95173347b07SNate Lawson }
95273347b07SNate Lawson 
953e959a70bSNate Lawson static int
954e959a70bSNate Lawson cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
955e959a70bSNate Lawson {
956e959a70bSNate Lawson 	device_t dev;
957e959a70bSNate Lawson 	struct cf_setting *sets;
958e959a70bSNate Lawson 	struct sbuf sb;
959e959a70bSNate Lawson 	int error, i, set_count;
960e959a70bSNate Lawson 
961e959a70bSNate Lawson 	dev = oidp->oid_arg1;
962e959a70bSNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
963e959a70bSNate Lawson 
964e959a70bSNate Lawson 	/* Get settings from the device and generate the output string. */
965e959a70bSNate Lawson 	set_count = MAX_SETTINGS;
966e959a70bSNate Lawson 	sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
96730215f48SChristian Brueffer 	if (sets == NULL) {
96830215f48SChristian Brueffer 		sbuf_delete(&sb);
969e959a70bSNate Lawson 		return (ENOMEM);
97030215f48SChristian Brueffer 	}
971e959a70bSNate Lawson 	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
972e959a70bSNate Lawson 	if (error)
973e959a70bSNate Lawson 		goto out;
974e959a70bSNate Lawson 	if (set_count) {
975e959a70bSNate Lawson 		for (i = 0; i < set_count; i++)
976e959a70bSNate Lawson 			sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
977e959a70bSNate Lawson 	} else
978e959a70bSNate Lawson 		sbuf_cpy(&sb, "0");
979e959a70bSNate Lawson 	sbuf_trim(&sb);
980e959a70bSNate Lawson 	sbuf_finish(&sb);
981e959a70bSNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
982e959a70bSNate Lawson 
983e959a70bSNate Lawson out:
984e959a70bSNate Lawson 	free(sets, M_TEMP);
985e959a70bSNate Lawson 	sbuf_delete(&sb);
986e959a70bSNate Lawson 	return (error);
987e959a70bSNate Lawson }
988e959a70bSNate Lawson 
98973347b07SNate Lawson int
99073347b07SNate Lawson cpufreq_register(device_t dev)
99173347b07SNate Lawson {
99267c8649fSNate Lawson 	struct cpufreq_softc *sc;
99373347b07SNate Lawson 	device_t cf_dev, cpu_dev;
99473347b07SNate Lawson 
995e959a70bSNate Lawson 	/* Add a sysctl to get each driver's settings separately. */
996e959a70bSNate Lawson 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
997e959a70bSNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
998e959a70bSNate Lawson 	    OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
999e959a70bSNate Lawson 	    cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
1000e959a70bSNate Lawson 
100173347b07SNate Lawson 	/*
10020325089dSNate Lawson 	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
10030325089dSNate Lawson 	 * must offer the same levels and be switched at the same time.
100473347b07SNate Lawson 	 */
10050325089dSNate Lawson 	cpu_dev = device_get_parent(dev);
100667c8649fSNate Lawson 	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
100767c8649fSNate Lawson 		sc = device_get_softc(cf_dev);
100867c8649fSNate Lawson 		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
100973347b07SNate Lawson 		return (0);
101067c8649fSNate Lawson 	}
101173347b07SNate Lawson 
10120325089dSNate Lawson 	/* Add the child device and possibly sysctls. */
10130325089dSNate Lawson 	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
101473347b07SNate Lawson 	if (cf_dev == NULL)
101573347b07SNate Lawson 		return (ENOMEM);
101673347b07SNate Lawson 	device_quiet(cf_dev);
101773347b07SNate Lawson 
101873347b07SNate Lawson 	return (device_probe_and_attach(cf_dev));
101973347b07SNate Lawson }
102073347b07SNate Lawson 
102173347b07SNate Lawson int
102273347b07SNate Lawson cpufreq_unregister(device_t dev)
102373347b07SNate Lawson {
102473347b07SNate Lawson 	device_t cf_dev, *devs;
1025e94a0c1aSNate Lawson 	int cfcount, devcount, error, i, type;
102673347b07SNate Lawson 
102773347b07SNate Lawson 	/*
102873347b07SNate Lawson 	 * If this is the last cpufreq child device, remove the control
102973347b07SNate Lawson 	 * device as well.  We identify cpufreq children by calling a method
103073347b07SNate Lawson 	 * they support.
103173347b07SNate Lawson 	 */
103273347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &devcount);
103373347b07SNate Lawson 	if (error)
103473347b07SNate Lawson 		return (error);
103567c8649fSNate Lawson 	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
103671ab130cSNate Lawson 	if (cf_dev == NULL) {
103771ab130cSNate Lawson 		device_printf(dev,
103871ab130cSNate Lawson 	"warning: cpufreq_unregister called with no cpufreq device active\n");
1039ed472910SChristian Brueffer 		free(devs, M_TEMP);
104071ab130cSNate Lawson 		return (0);
104171ab130cSNate Lawson 	}
104273347b07SNate Lawson 	cfcount = 0;
104373347b07SNate Lawson 	for (i = 0; i < devcount; i++) {
104473347b07SNate Lawson 		if (!device_is_attached(devs[i]))
104573347b07SNate Lawson 			continue;
1046e94a0c1aSNate Lawson 		if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
104773347b07SNate Lawson 			cfcount++;
104873347b07SNate Lawson 	}
10490325089dSNate Lawson 	if (cfcount <= 1)
105073347b07SNate Lawson 		device_delete_child(device_get_parent(cf_dev), cf_dev);
105173347b07SNate Lawson 	free(devs, M_TEMP);
105273347b07SNate Lawson 
105373347b07SNate Lawson 	return (0);
105473347b07SNate Lawson }
10550d4ac62aSNate Lawson 
10560d4ac62aSNate Lawson int
10570d4ac62aSNate Lawson cpufreq_settings_changed(device_t dev)
10580d4ac62aSNate Lawson {
10590d4ac62aSNate Lawson 
10600d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_levels_changed,
10610d4ac62aSNate Lawson 	    device_get_unit(device_get_parent(dev)));
10620d4ac62aSNate Lawson 	return (0);
10630d4ac62aSNate Lawson }
1064