xref: /freebsd/sys/kern/kern_cpu.c (revision 760f4dec67f4cef8b2721022cbd167799668d38b)
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;
136b070969bSNate Lawson TUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq);
1378d913481SNate Lawson TUNABLE_INT("debug.cpufreq.verbose", &cf_verbose);
1386472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL,
1396472ac3dSEd Schouten     "cpufreq debugging");
140b070969bSNate Lawson SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1,
141b070969bSNate Lawson     "Don't provide levels below this frequency.");
1428d913481SNate Lawson SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1,
1438d913481SNate Lawson     "Print verbose debugging messages");
144b070969bSNate Lawson 
14573347b07SNate Lawson static int
14673347b07SNate Lawson cpufreq_attach(device_t dev)
14773347b07SNate Lawson {
14873347b07SNate Lawson 	struct cpufreq_softc *sc;
149d288bcc4SAlexander Motin 	struct pcpu *pc;
15073347b07SNate Lawson 	device_t parent;
151d288bcc4SAlexander Motin 	uint64_t rate;
15273347b07SNate Lawson 	int numdevs;
15373347b07SNate Lawson 
1548d913481SNate Lawson 	CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
15573347b07SNate Lawson 	sc = device_get_softc(dev);
15673347b07SNate Lawson 	parent = device_get_parent(dev);
15773347b07SNate Lawson 	sc->dev = dev;
15873347b07SNate Lawson 	sysctl_ctx_init(&sc->sysctl_ctx);
15973347b07SNate Lawson 	TAILQ_INIT(&sc->all_levels);
160789f03ceSNate Lawson 	CF_MTX_INIT(&sc->lock);
16173347b07SNate Lawson 	sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
16256e5a87aSHajimu UMEMOTO 	SLIST_INIT(&sc->saved_freq);
163f436f175SNathan Whitehorn 	/* Try to get nominal CPU freq to use it as maximum later if needed */
164f436f175SNathan Whitehorn 	sc->max_mhz = cpu_get_nominal_mhz(dev);
165f436f175SNathan Whitehorn 	/* If that fails, try to measure the current rate */
166f436f175SNathan Whitehorn 	if (sc->max_mhz <= 0) {
167d288bcc4SAlexander Motin 		pc = cpu_get_pcpu(dev);
168d288bcc4SAlexander Motin 		if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
169d288bcc4SAlexander Motin 			sc->max_mhz = rate / 1000000;
170d288bcc4SAlexander Motin 		else
17167c8649fSNate Lawson 			sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
172f436f175SNathan Whitehorn 	}
17373347b07SNate Lawson 
17473347b07SNate Lawson 	/*
17573347b07SNate Lawson 	 * Only initialize one set of sysctls for all CPUs.  In the future,
17673347b07SNate Lawson 	 * if multiple CPUs can have different settings, we can move these
17773347b07SNate Lawson 	 * sysctls to be under every CPU instead of just the first one.
17873347b07SNate Lawson 	 */
17973347b07SNate Lawson 	numdevs = devclass_get_count(cpufreq_dc);
18073347b07SNate Lawson 	if (numdevs > 1)
18173347b07SNate Lawson 		return (0);
18273347b07SNate Lawson 
1838d913481SNate Lawson 	CF_DEBUG("initializing one-time data for %s\n",
1848d913481SNate Lawson 	    device_get_nameunit(dev));
185dac50931SAndriy Gapon 	sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf),
186dac50931SAndriy Gapon 	    M_DEVBUF, M_WAITOK);
18773347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
18873347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
18973347b07SNate Lawson 	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
19073347b07SNate Lawson 	    cpufreq_curr_sysctl, "I", "Current CPU frequency");
19173347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
19273347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
19373347b07SNate Lawson 	    OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
19473347b07SNate Lawson 	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
1950d4ac62aSNate Lawson 
1960d4ac62aSNate Lawson 	/*
1970d4ac62aSNate Lawson 	 * Queue a one-shot broadcast that levels have changed.
1980d4ac62aSNate Lawson 	 * It will run once the system has completed booting.
1990d4ac62aSNate Lawson 	 */
2000d4ac62aSNate Lawson 	TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
2010d4ac62aSNate Lawson 	taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
20273347b07SNate Lawson 
20373347b07SNate Lawson 	return (0);
20473347b07SNate Lawson }
20573347b07SNate Lawson 
2060d4ac62aSNate Lawson /* Handle any work to be done for all drivers that attached during boot. */
2070d4ac62aSNate Lawson static void
2080d4ac62aSNate Lawson cpufreq_startup_task(void *ctx, int pending)
2090d4ac62aSNate Lawson {
2100d4ac62aSNate Lawson 
2110d4ac62aSNate Lawson 	cpufreq_settings_changed((device_t)ctx);
2120d4ac62aSNate Lawson }
2130d4ac62aSNate Lawson 
21473347b07SNate Lawson static int
21573347b07SNate Lawson cpufreq_detach(device_t dev)
21673347b07SNate Lawson {
21773347b07SNate Lawson 	struct cpufreq_softc *sc;
21856e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq;
21973347b07SNate Lawson 	int numdevs;
22073347b07SNate Lawson 
2218d913481SNate Lawson 	CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
22273347b07SNate Lawson 	sc = device_get_softc(dev);
22373347b07SNate Lawson 	sysctl_ctx_free(&sc->sysctl_ctx);
22473347b07SNate Lawson 
22556e5a87aSHajimu UMEMOTO 	while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
22656e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
22756e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
22856e5a87aSHajimu UMEMOTO 	}
22956e5a87aSHajimu UMEMOTO 
23073347b07SNate Lawson 	/* Only clean up these resources when the last device is detaching. */
23173347b07SNate Lawson 	numdevs = devclass_get_count(cpufreq_dc);
2328d913481SNate Lawson 	if (numdevs == 1) {
2338d913481SNate Lawson 		CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
234dac50931SAndriy Gapon 		free(sc->levels_buf, M_DEVBUF);
2358d913481SNate Lawson 	}
23673347b07SNate Lawson 
23773347b07SNate Lawson 	return (0);
23873347b07SNate Lawson }
23973347b07SNate Lawson 
24073347b07SNate Lawson static int
24173347b07SNate Lawson cf_set_method(device_t dev, const struct cf_level *level, int priority)
24273347b07SNate Lawson {
24373347b07SNate Lawson 	struct cpufreq_softc *sc;
24473347b07SNate Lawson 	const struct cf_setting *set;
24556e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq, *curr_freq;
2460325089dSNate Lawson 	struct pcpu *pc;
24762db376aSNate Lawson 	int error, i;
24873347b07SNate Lawson 
24973347b07SNate Lawson 	sc = device_get_softc(dev);
250789f03ceSNate Lawson 	error = 0;
251789f03ceSNate Lawson 	set = NULL;
25256e5a87aSHajimu UMEMOTO 	saved_freq = NULL;
25373347b07SNate Lawson 
2540d4ac62aSNate Lawson 	/* We are going to change levels so notify the pre-change handler. */
2550d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
2560d4ac62aSNate Lawson 	if (error != 0) {
2570d4ac62aSNate Lawson 		EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
2580d4ac62aSNate Lawson 		return (error);
259b4130b8aSMarcus Alves Grando 	}
2600325089dSNate Lawson 
261789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
26256e5a87aSHajimu UMEMOTO 
263a15e947dSNate Lawson #ifdef SMP
264a15e947dSNate Lawson 	/*
265a15e947dSNate Lawson 	 * If still booting and secondary CPUs not started yet, don't allow
266a15e947dSNate Lawson 	 * changing the frequency until they're online.  This is because we
267a15e947dSNate Lawson 	 * can't switch to them using sched_bind() and thus we'd only be
268a15e947dSNate Lawson 	 * switching the main CPU.  XXXTODO: Need to think more about how to
269a15e947dSNate Lawson 	 * handle having different CPUs at different frequencies.
270a15e947dSNate Lawson 	 */
27160ad8150SScott Long 	if (mp_ncpus > 1 && !smp_started) {
272a15e947dSNate Lawson 		device_printf(dev, "rejecting change, SMP not started yet\n");
273a15e947dSNate Lawson 		error = ENXIO;
274a15e947dSNate Lawson 		goto out;
275a15e947dSNate Lawson 	}
276a15e947dSNate Lawson #endif /* SMP */
277a15e947dSNate Lawson 
27856e5a87aSHajimu UMEMOTO 	/*
27956e5a87aSHajimu UMEMOTO 	 * If the requested level has a lower priority, don't allow
28056e5a87aSHajimu UMEMOTO 	 * the new level right now.
28156e5a87aSHajimu UMEMOTO 	 */
28256e5a87aSHajimu UMEMOTO 	if (priority < sc->curr_priority) {
2838d913481SNate Lawson 		CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
2848d913481SNate Lawson 		    sc->curr_priority);
285789f03ceSNate Lawson 		error = EPERM;
286789f03ceSNate Lawson 		goto out;
287789f03ceSNate Lawson 	}
2885f0afa04SNate Lawson 
28956e5a87aSHajimu UMEMOTO 	/*
29056e5a87aSHajimu UMEMOTO 	 * If the caller didn't specify a level and one is saved, prepare to
29156e5a87aSHajimu UMEMOTO 	 * restore the saved level.  If none has been saved, return an error.
29256e5a87aSHajimu UMEMOTO 	 */
29356e5a87aSHajimu UMEMOTO 	if (level == NULL) {
29456e5a87aSHajimu UMEMOTO 		saved_freq = SLIST_FIRST(&sc->saved_freq);
29556e5a87aSHajimu UMEMOTO 		if (saved_freq == NULL) {
29656e5a87aSHajimu UMEMOTO 			CF_DEBUG("NULL level, no saved level\n");
29756e5a87aSHajimu UMEMOTO 			error = ENXIO;
29856e5a87aSHajimu UMEMOTO 			goto out;
29956e5a87aSHajimu UMEMOTO 		}
30056e5a87aSHajimu UMEMOTO 		level = &saved_freq->level;
30156e5a87aSHajimu UMEMOTO 		priority = saved_freq->priority;
30256e5a87aSHajimu UMEMOTO 		CF_DEBUG("restoring saved level, freq %d prio %d\n",
30356e5a87aSHajimu UMEMOTO 		    level->total_set.freq, priority);
30456e5a87aSHajimu UMEMOTO 	}
30556e5a87aSHajimu UMEMOTO 
306b070969bSNate Lawson 	/* Reject levels that are below our specified threshold. */
307da8a77c1SNate Lawson 	if (level->total_set.freq < cf_lowest_freq) {
3088d913481SNate Lawson 		CF_DEBUG("rejecting freq %d, less than %d limit\n",
3098d913481SNate Lawson 		    level->total_set.freq, cf_lowest_freq);
310789f03ceSNate Lawson 		error = EINVAL;
311789f03ceSNate Lawson 		goto out;
312789f03ceSNate Lawson 	}
313b070969bSNate Lawson 
31473347b07SNate Lawson 	/* If already at this level, just return. */
3155f3818a5SAlexander Motin 	if (sc->curr_level.total_set.freq == level->total_set.freq) {
3168d913481SNate Lawson 		CF_DEBUG("skipping freq %d, same as current level %d\n",
3178d913481SNate Lawson 		    level->total_set.freq, sc->curr_level.total_set.freq);
3181fea6ce7SHajimu UMEMOTO 		goto skip;
3198d913481SNate Lawson 	}
32073347b07SNate Lawson 
32173347b07SNate Lawson 	/* First, set the absolute frequency via its driver. */
32273347b07SNate Lawson 	set = &level->abs_set;
32373347b07SNate Lawson 	if (set->dev) {
32473347b07SNate Lawson 		if (!device_is_attached(set->dev)) {
32573347b07SNate Lawson 			error = ENXIO;
32673347b07SNate Lawson 			goto out;
32773347b07SNate Lawson 		}
3281196826aSNate Lawson 
32962db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3301196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
331982d11f8SJeff Roberson 		thread_lock(curthread);
3321196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
333982d11f8SJeff Roberson 		thread_unlock(curthread);
3348d913481SNate Lawson 		CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
3358d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
33673347b07SNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
337982d11f8SJeff Roberson 		thread_lock(curthread);
3381196826aSNate Lawson 		sched_unbind(curthread);
339982d11f8SJeff Roberson 		thread_unlock(curthread);
34073347b07SNate Lawson 		if (error) {
34173347b07SNate Lawson 			goto out;
34273347b07SNate Lawson 		}
34373347b07SNate Lawson 	}
34473347b07SNate Lawson 
34588c9b54cSNate Lawson 	/* Next, set any/all relative frequencies via their drivers. */
34688c9b54cSNate Lawson 	for (i = 0; i < level->rel_count; i++) {
34788c9b54cSNate Lawson 		set = &level->rel_set[i];
34888c9b54cSNate Lawson 		if (!device_is_attached(set->dev)) {
34988c9b54cSNate Lawson 			error = ENXIO;
35088c9b54cSNate Lawson 			goto out;
35188c9b54cSNate Lawson 		}
3521196826aSNate Lawson 
35362db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3541196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
355982d11f8SJeff Roberson 		thread_lock(curthread);
3561196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
357982d11f8SJeff Roberson 		thread_unlock(curthread);
3588d913481SNate Lawson 		CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
3598d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
36088c9b54cSNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
361982d11f8SJeff Roberson 		thread_lock(curthread);
3621196826aSNate Lawson 		sched_unbind(curthread);
363982d11f8SJeff Roberson 		thread_unlock(curthread);
36488c9b54cSNate Lawson 		if (error) {
36588c9b54cSNate Lawson 			/* XXX Back out any successful setting? */
36688c9b54cSNate Lawson 			goto out;
36788c9b54cSNate Lawson 		}
36888c9b54cSNate Lawson 	}
36973347b07SNate Lawson 
3701fea6ce7SHajimu UMEMOTO skip:
3715f0afa04SNate Lawson 	/*
3725f0afa04SNate Lawson 	 * Before recording the current level, check if we're going to a
37356e5a87aSHajimu UMEMOTO 	 * higher priority.  If so, save the previous level and priority.
3745f0afa04SNate Lawson 	 */
3755f0afa04SNate Lawson 	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
37656e5a87aSHajimu UMEMOTO 	    priority > sc->curr_priority) {
3778d913481SNate Lawson 		CF_DEBUG("saving level, freq %d prio %d\n",
3788d913481SNate Lawson 		    sc->curr_level.total_set.freq, sc->curr_priority);
37956e5a87aSHajimu UMEMOTO 		curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
38056e5a87aSHajimu UMEMOTO 		if (curr_freq == NULL) {
38156e5a87aSHajimu UMEMOTO 			error = ENOMEM;
38256e5a87aSHajimu UMEMOTO 			goto out;
38356e5a87aSHajimu UMEMOTO 		}
38456e5a87aSHajimu UMEMOTO 		curr_freq->level = sc->curr_level;
38556e5a87aSHajimu UMEMOTO 		curr_freq->priority = sc->curr_priority;
38656e5a87aSHajimu UMEMOTO 		SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
3875f0afa04SNate Lawson 	}
38873347b07SNate Lawson 	sc->curr_level = *level;
3895f0afa04SNate Lawson 	sc->curr_priority = priority;
39073347b07SNate Lawson 
39156e5a87aSHajimu UMEMOTO 	/* If we were restoring a saved state, reset it to "unused". */
39256e5a87aSHajimu UMEMOTO 	if (saved_freq != NULL) {
39356e5a87aSHajimu UMEMOTO 		CF_DEBUG("resetting saved level\n");
39456e5a87aSHajimu UMEMOTO 		sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
39556e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
39656e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
39756e5a87aSHajimu UMEMOTO 	}
39856e5a87aSHajimu UMEMOTO 
39973347b07SNate Lawson out:
400789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
4010d4ac62aSNate Lawson 
4020d4ac62aSNate Lawson 	/*
4030d4ac62aSNate Lawson 	 * We changed levels (or attempted to) so notify the post-change
4040d4ac62aSNate Lawson 	 * handler of new frequency or error.
4050d4ac62aSNate Lawson 	 */
4060d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
407789f03ceSNate Lawson 	if (error && set)
40873347b07SNate Lawson 		device_printf(set->dev, "set freq failed, err %d\n", error);
4090d4ac62aSNate Lawson 
41073347b07SNate Lawson 	return (error);
41173347b07SNate Lawson }
41273347b07SNate Lawson 
41373347b07SNate Lawson static int
41473347b07SNate Lawson cf_get_method(device_t dev, struct cf_level *level)
41573347b07SNate Lawson {
41673347b07SNate Lawson 	struct cpufreq_softc *sc;
41773347b07SNate Lawson 	struct cf_level *levels;
41873347b07SNate Lawson 	struct cf_setting *curr_set, set;
41973347b07SNate Lawson 	struct pcpu *pc;
42073347b07SNate Lawson 	device_t *devs;
421*760f4decSColin Percival 	int bdiff, count, diff, error, i, n, numdevs;
42273347b07SNate Lawson 	uint64_t rate;
42373347b07SNate Lawson 
42473347b07SNate Lawson 	sc = device_get_softc(dev);
425789f03ceSNate Lawson 	error = 0;
42673347b07SNate Lawson 	levels = NULL;
42773347b07SNate Lawson 
42873347b07SNate Lawson 	/* If we already know the current frequency, we're done. */
429789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
430789f03ceSNate Lawson 	curr_set = &sc->curr_level.total_set;
4318d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4328d913481SNate Lawson 		CF_DEBUG("get returning known freq %d\n", curr_set->freq);
43373347b07SNate Lawson 		goto out;
4348d913481SNate Lawson 	}
435789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
43673347b07SNate Lawson 
43773347b07SNate Lawson 	/*
43873347b07SNate Lawson 	 * We need to figure out the current level.  Loop through every
43973347b07SNate Lawson 	 * driver, getting the current setting.  Then, attempt to get a best
44073347b07SNate Lawson 	 * match of settings against each level.
44173347b07SNate Lawson 	 */
44273347b07SNate Lawson 	count = CF_MAX_LEVELS;
44373347b07SNate Lawson 	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
44473347b07SNate Lawson 	if (levels == NULL)
44573347b07SNate Lawson 		return (ENOMEM);
44673347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
447d269386aSNate Lawson 	if (error) {
448d269386aSNate Lawson 		if (error == E2BIG)
449d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
450789f03ceSNate Lawson 		free(levels, M_TEMP);
451789f03ceSNate Lawson 		return (error);
452d269386aSNate Lawson 	}
45373347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
454789f03ceSNate Lawson 	if (error) {
455789f03ceSNate Lawson 		free(levels, M_TEMP);
456789f03ceSNate Lawson 		return (error);
457789f03ceSNate Lawson 	}
458789f03ceSNate Lawson 
459789f03ceSNate Lawson 	/*
460789f03ceSNate Lawson 	 * Reacquire the lock and search for the given level.
461789f03ceSNate Lawson 	 *
462789f03ceSNate Lawson 	 * XXX Note: this is not quite right since we really need to go
463789f03ceSNate Lawson 	 * through each level and compare both absolute and relative
464789f03ceSNate Lawson 	 * settings for each driver in the system before making a match.
465789f03ceSNate Lawson 	 * The estimation code below catches this case though.
466789f03ceSNate Lawson 	 */
467789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
4682145b9d2SNate Lawson 	for (n = 0; n < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; n++) {
4692145b9d2SNate Lawson 		if (!device_is_attached(devs[n]))
47073347b07SNate Lawson 			continue;
471be00f605SJohn Baldwin 		if (CPUFREQ_DRV_GET(devs[n], &set) != 0)
47273347b07SNate Lawson 			continue;
47373347b07SNate Lawson 		for (i = 0; i < count; i++) {
4745f3818a5SAlexander Motin 			if (set.freq == levels[i].total_set.freq) {
47573347b07SNate Lawson 				sc->curr_level = levels[i];
47673347b07SNate Lawson 				break;
47773347b07SNate Lawson 			}
47873347b07SNate Lawson 		}
47973347b07SNate Lawson 	}
48073347b07SNate Lawson 	free(devs, M_TEMP);
4818d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4828d913481SNate Lawson 		CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
48373347b07SNate Lawson 		goto out;
4848d913481SNate Lawson 	}
48573347b07SNate Lawson 
48673347b07SNate Lawson 	/*
48773347b07SNate Lawson 	 * We couldn't find an exact match, so attempt to estimate and then
48873347b07SNate Lawson 	 * match against a level.
48973347b07SNate Lawson 	 */
49073347b07SNate Lawson 	pc = cpu_get_pcpu(dev);
49173347b07SNate Lawson 	if (pc == NULL) {
49273347b07SNate Lawson 		error = ENXIO;
49373347b07SNate Lawson 		goto out;
49473347b07SNate Lawson 	}
49573347b07SNate Lawson 	cpu_est_clockrate(pc->pc_cpuid, &rate);
49673347b07SNate Lawson 	rate /= 1000000;
497*760f4decSColin Percival 	bdiff = 1 << 30;
49873347b07SNate Lawson 	for (i = 0; i < count; i++) {
499*760f4decSColin Percival 		diff = abs(levels[i].total_set.freq - rate);
500*760f4decSColin Percival 		if (diff < bdiff) {
501*760f4decSColin Percival 			bdiff = diff;
50273347b07SNate Lawson 			sc->curr_level = levels[i];
503*760f4decSColin Percival 		}
504*760f4decSColin Percival 	}
5058d913481SNate Lawson 	CF_DEBUG("get estimated freq %d\n", curr_set->freq);
50673347b07SNate Lawson 
50773347b07SNate Lawson out:
508789f03ceSNate Lawson 	if (error == 0)
509789f03ceSNate Lawson 		*level = sc->curr_level;
510789f03ceSNate Lawson 
511789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
51273347b07SNate Lawson 	if (levels)
51373347b07SNate Lawson 		free(levels, M_TEMP);
514789f03ceSNate Lawson 	return (error);
51573347b07SNate Lawson }
51673347b07SNate Lawson 
51773347b07SNate Lawson static int
51873347b07SNate Lawson cf_levels_method(device_t dev, struct cf_level *levels, int *count)
51973347b07SNate Lawson {
52088c9b54cSNate Lawson 	struct cf_setting_array *set_arr;
52173347b07SNate Lawson 	struct cf_setting_lst rel_sets;
52273347b07SNate Lawson 	struct cpufreq_softc *sc;
52373347b07SNate Lawson 	struct cf_level *lev;
52473347b07SNate Lawson 	struct cf_setting *sets;
52573347b07SNate Lawson 	struct pcpu *pc;
52673347b07SNate Lawson 	device_t *devs;
52788c9b54cSNate Lawson 	int error, i, numdevs, set_count, type;
52873347b07SNate Lawson 	uint64_t rate;
52973347b07SNate Lawson 
53073347b07SNate Lawson 	if (levels == NULL || count == NULL)
53173347b07SNate Lawson 		return (EINVAL);
53273347b07SNate Lawson 
53373347b07SNate Lawson 	TAILQ_INIT(&rel_sets);
53473347b07SNate Lawson 	sc = device_get_softc(dev);
53573347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
53673347b07SNate Lawson 	if (error)
53773347b07SNate Lawson 		return (error);
53873347b07SNate Lawson 	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
53973347b07SNate Lawson 	if (sets == NULL) {
54073347b07SNate Lawson 		free(devs, M_TEMP);
54173347b07SNate Lawson 		return (ENOMEM);
54273347b07SNate Lawson 	}
54373347b07SNate Lawson 
54473347b07SNate Lawson 	/* Get settings from all cpufreq drivers. */
545789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
54673347b07SNate Lawson 	for (i = 0; i < numdevs; i++) {
547e22cd41cSNate Lawson 		/* Skip devices that aren't ready. */
54873347b07SNate Lawson 		if (!device_is_attached(devs[i]))
54973347b07SNate Lawson 			continue;
550e22cd41cSNate Lawson 
551e22cd41cSNate Lawson 		/*
552e22cd41cSNate Lawson 		 * Get settings, skipping drivers that offer no settings or
553e22cd41cSNate Lawson 		 * provide settings for informational purposes only.
554e22cd41cSNate Lawson 		 */
555e94a0c1aSNate Lawson 		error = CPUFREQ_DRV_TYPE(devs[i], &type);
5568d913481SNate Lawson 		if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
5578d913481SNate Lawson 			if (error == 0) {
5588d913481SNate Lawson 				CF_DEBUG("skipping info-only driver %s\n",
5598d913481SNate Lawson 				    device_get_nameunit(devs[i]));
5608d913481SNate Lawson 			}
561e94a0c1aSNate Lawson 			continue;
5628d913481SNate Lawson 		}
56373347b07SNate Lawson 		set_count = MAX_SETTINGS;
564e94a0c1aSNate Lawson 		error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
565e94a0c1aSNate Lawson 		if (error || set_count == 0)
56673347b07SNate Lawson 			continue;
56773347b07SNate Lawson 
568e22cd41cSNate Lawson 		/* Add the settings to our absolute/relative lists. */
5690325089dSNate Lawson 		switch (type & CPUFREQ_TYPE_MASK) {
57088c9b54cSNate Lawson 		case CPUFREQ_TYPE_ABSOLUTE:
57188c9b54cSNate Lawson 			error = cpufreq_insert_abs(sc, sets, set_count);
57288c9b54cSNate Lawson 			break;
57388c9b54cSNate Lawson 		case CPUFREQ_TYPE_RELATIVE:
5748d913481SNate Lawson 			CF_DEBUG("adding %d relative settings\n", set_count);
57588c9b54cSNate Lawson 			set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
57688c9b54cSNate Lawson 			if (set_arr == NULL) {
57788c9b54cSNate Lawson 				error = ENOMEM;
57888c9b54cSNate Lawson 				goto out;
57988c9b54cSNate Lawson 			}
58088c9b54cSNate Lawson 			bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
58188c9b54cSNate Lawson 			set_arr->count = set_count;
58288c9b54cSNate Lawson 			TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
58388c9b54cSNate Lawson 			break;
58488c9b54cSNate Lawson 		default:
58588c9b54cSNate Lawson 			error = EINVAL;
58688c9b54cSNate Lawson 		}
58788c9b54cSNate Lawson 		if (error)
58873347b07SNate Lawson 			goto out;
58973347b07SNate Lawson 	}
59073347b07SNate Lawson 
59167c8649fSNate Lawson 	/*
59267c8649fSNate Lawson 	 * If there are no absolute levels, create a fake one at 100%.  We
59367c8649fSNate Lawson 	 * then cache the clockrate for later use as our base frequency.
59467c8649fSNate Lawson 	 */
59573347b07SNate Lawson 	if (TAILQ_EMPTY(&sc->all_levels)) {
59667c8649fSNate Lawson 		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
597f436f175SNathan Whitehorn 			sc->max_mhz = cpu_get_nominal_mhz(dev);
598f436f175SNathan Whitehorn 			/*
599f436f175SNathan Whitehorn 			 * If the CPU can't report a rate for 100%, hope
600f436f175SNathan Whitehorn 			 * the CPU is running at its nominal rate right now,
601f436f175SNathan Whitehorn 			 * and use that instead.
602f436f175SNathan Whitehorn 			 */
603f436f175SNathan Whitehorn 			if (sc->max_mhz <= 0) {
60473347b07SNate Lawson 				pc = cpu_get_pcpu(dev);
60573347b07SNate Lawson 				cpu_est_clockrate(pc->pc_cpuid, &rate);
60667c8649fSNate Lawson 				sc->max_mhz = rate / 1000000;
60767c8649fSNate Lawson 			}
608f436f175SNathan Whitehorn 		}
60967c8649fSNate Lawson 		memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
61067c8649fSNate Lawson 		sets[0].freq = sc->max_mhz;
61167c8649fSNate Lawson 		sets[0].dev = NULL;
61288c9b54cSNate Lawson 		error = cpufreq_insert_abs(sc, sets, 1);
61373347b07SNate Lawson 		if (error)
61473347b07SNate Lawson 			goto out;
61573347b07SNate Lawson 	}
61673347b07SNate Lawson 
61788c9b54cSNate Lawson 	/* Create a combined list of absolute + relative levels. */
61888c9b54cSNate Lawson 	TAILQ_FOREACH(set_arr, &rel_sets, link)
61988c9b54cSNate Lawson 		cpufreq_expand_set(sc, set_arr);
62088c9b54cSNate Lawson 
62188c9b54cSNate Lawson 	/* If the caller doesn't have enough space, return the actual count. */
62288c9b54cSNate Lawson 	if (sc->all_count > *count) {
62388c9b54cSNate Lawson 		*count = sc->all_count;
62488c9b54cSNate Lawson 		error = E2BIG;
62588c9b54cSNate Lawson 		goto out;
62688c9b54cSNate Lawson 	}
62788c9b54cSNate Lawson 
62888c9b54cSNate Lawson 	/* Finally, output the list of levels. */
62973347b07SNate Lawson 	i = 0;
63073347b07SNate Lawson 	TAILQ_FOREACH(lev, &sc->all_levels, link) {
631e1f13773SNate Lawson 
632b070969bSNate Lawson 		/* Skip levels that have a frequency that is too low. */
633da8a77c1SNate Lawson 		if (lev->total_set.freq < cf_lowest_freq) {
634b070969bSNate Lawson 			sc->all_count--;
635b070969bSNate Lawson 			continue;
636b070969bSNate Lawson 		}
637b070969bSNate Lawson 
63873347b07SNate Lawson 		levels[i] = *lev;
63973347b07SNate Lawson 		i++;
64073347b07SNate Lawson 	}
64188c9b54cSNate Lawson 	*count = sc->all_count;
64273347b07SNate Lawson 	error = 0;
64373347b07SNate Lawson 
64473347b07SNate Lawson out:
64573347b07SNate Lawson 	/* Clear all levels since we regenerate them each time. */
64673347b07SNate Lawson 	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
64773347b07SNate Lawson 		TAILQ_REMOVE(&sc->all_levels, lev, link);
64873347b07SNate Lawson 		free(lev, M_TEMP);
64973347b07SNate Lawson 	}
650789f03ceSNate Lawson 	sc->all_count = 0;
651789f03ceSNate Lawson 
652789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
65388c9b54cSNate Lawson 	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
65488c9b54cSNate Lawson 		TAILQ_REMOVE(&rel_sets, set_arr, link);
65588c9b54cSNate Lawson 		free(set_arr, M_TEMP);
65688c9b54cSNate Lawson 	}
65773347b07SNate Lawson 	free(devs, M_TEMP);
65873347b07SNate Lawson 	free(sets, M_TEMP);
65973347b07SNate Lawson 	return (error);
66073347b07SNate Lawson }
66173347b07SNate Lawson 
66273347b07SNate Lawson /*
66373347b07SNate Lawson  * Create levels for an array of absolute settings and insert them in
66473347b07SNate Lawson  * sorted order in the specified list.
66573347b07SNate Lawson  */
66673347b07SNate Lawson static int
66788c9b54cSNate Lawson cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
66873347b07SNate Lawson     int count)
66973347b07SNate Lawson {
67088c9b54cSNate Lawson 	struct cf_level_lst *list;
67173347b07SNate Lawson 	struct cf_level *level, *search;
67273347b07SNate Lawson 	int i;
67373347b07SNate Lawson 
674789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
675789f03ceSNate Lawson 
67688c9b54cSNate Lawson 	list = &sc->all_levels;
67773347b07SNate Lawson 	for (i = 0; i < count; i++) {
67873347b07SNate Lawson 		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
67973347b07SNate Lawson 		if (level == NULL)
68073347b07SNate Lawson 			return (ENOMEM);
68173347b07SNate Lawson 		level->abs_set = sets[i];
68288c9b54cSNate Lawson 		level->total_set = sets[i];
68388c9b54cSNate Lawson 		level->total_set.dev = NULL;
68488c9b54cSNate Lawson 		sc->all_count++;
68573347b07SNate Lawson 
68673347b07SNate Lawson 		if (TAILQ_EMPTY(list)) {
6878d913481SNate Lawson 			CF_DEBUG("adding abs setting %d at head\n",
6888d913481SNate Lawson 			    sets[i].freq);
68973347b07SNate Lawson 			TAILQ_INSERT_HEAD(list, level, link);
69073347b07SNate Lawson 			continue;
69173347b07SNate Lawson 		}
69273347b07SNate Lawson 
69373347b07SNate Lawson 		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
69488c9b54cSNate Lawson 			if (sets[i].freq <= search->total_set.freq) {
6958d913481SNate Lawson 				CF_DEBUG("adding abs setting %d after %d\n",
6968d913481SNate Lawson 				    sets[i].freq, search->total_set.freq);
69773347b07SNate Lawson 				TAILQ_INSERT_AFTER(list, search, level, link);
69873347b07SNate Lawson 				break;
69973347b07SNate Lawson 			}
70073347b07SNate Lawson 		}
70173347b07SNate Lawson 	}
70273347b07SNate Lawson 	return (0);
70373347b07SNate Lawson }
70473347b07SNate Lawson 
70588c9b54cSNate Lawson /*
70688c9b54cSNate Lawson  * Expand a group of relative settings, creating derived levels from them.
70788c9b54cSNate Lawson  */
70888c9b54cSNate Lawson static int
70988c9b54cSNate Lawson cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
71088c9b54cSNate Lawson {
71188c9b54cSNate Lawson 	struct cf_level *fill, *search;
71288c9b54cSNate Lawson 	struct cf_setting *set;
71388c9b54cSNate Lawson 	int i;
71488c9b54cSNate Lawson 
715789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
716789f03ceSNate Lawson 
7175308b2a6SNate Lawson 	/*
7185308b2a6SNate Lawson 	 * Walk the set of all existing levels in reverse.  This is so we
7195308b2a6SNate Lawson 	 * create derived states from the lowest absolute settings first
7205308b2a6SNate Lawson 	 * and discard duplicates created from higher absolute settings.
7215308b2a6SNate Lawson 	 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
7225308b2a6SNate Lawson 	 * preferable to 200 Mhz + 25% because absolute settings are more
7235308b2a6SNate Lawson 	 * efficient since they often change the voltage as well.
7245308b2a6SNate Lawson 	 */
7255308b2a6SNate Lawson 	TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
72688c9b54cSNate Lawson 		/* Add each setting to the level, duplicating if necessary. */
72788c9b54cSNate Lawson 		for (i = 0; i < set_arr->count; i++) {
72888c9b54cSNate Lawson 			set = &set_arr->sets[i];
72988c9b54cSNate Lawson 
73088c9b54cSNate Lawson 			/*
73188c9b54cSNate Lawson 			 * If this setting is less than 100%, split the level
73288c9b54cSNate Lawson 			 * into two and add this setting to the new level.
73388c9b54cSNate Lawson 			 */
73488c9b54cSNate Lawson 			fill = search;
7355308b2a6SNate Lawson 			if (set->freq < 10000) {
73688c9b54cSNate Lawson 				fill = cpufreq_dup_set(sc, search, set);
73788c9b54cSNate Lawson 
73888c9b54cSNate Lawson 				/*
7395308b2a6SNate Lawson 				 * The new level was a duplicate of an existing
7405308b2a6SNate Lawson 				 * level or its absolute setting is too high
7415308b2a6SNate Lawson 				 * so we freed it.  For example, we discard a
7425308b2a6SNate Lawson 				 * derived level of 1000 MHz/25% if a level
7435308b2a6SNate Lawson 				 * of 500 MHz/100% already exists.
74488c9b54cSNate Lawson 				 */
74588c9b54cSNate Lawson 				if (fill == NULL)
7465308b2a6SNate Lawson 					break;
7475308b2a6SNate Lawson 			}
74888c9b54cSNate Lawson 
74988c9b54cSNate Lawson 			/* Add this setting to the existing or new level. */
75088c9b54cSNate Lawson 			KASSERT(fill->rel_count < MAX_SETTINGS,
75188c9b54cSNate Lawson 			    ("cpufreq: too many relative drivers (%d)",
75288c9b54cSNate Lawson 			    MAX_SETTINGS));
75388c9b54cSNate Lawson 			fill->rel_set[fill->rel_count] = *set;
75488c9b54cSNate Lawson 			fill->rel_count++;
7558d913481SNate Lawson 			CF_DEBUG(
7568d913481SNate Lawson 			"expand set added rel setting %d%% to %d level\n",
7578d913481SNate Lawson 			    set->freq / 100, fill->total_set.freq);
75888c9b54cSNate Lawson 		}
75988c9b54cSNate Lawson 	}
76088c9b54cSNate Lawson 
76188c9b54cSNate Lawson 	return (0);
76288c9b54cSNate Lawson }
76388c9b54cSNate Lawson 
76488c9b54cSNate Lawson static struct cf_level *
76588c9b54cSNate Lawson cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
76688c9b54cSNate Lawson     struct cf_setting *set)
76788c9b54cSNate Lawson {
76888c9b54cSNate Lawson 	struct cf_level_lst *list;
76988c9b54cSNate Lawson 	struct cf_level *fill, *itr;
77088c9b54cSNate Lawson 	struct cf_setting *fill_set, *itr_set;
77188c9b54cSNate Lawson 	int i;
77288c9b54cSNate Lawson 
773789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
774789f03ceSNate Lawson 
77588c9b54cSNate Lawson 	/*
77688c9b54cSNate Lawson 	 * Create a new level, copy it from the old one, and update the
77788c9b54cSNate Lawson 	 * total frequency and power by the percentage specified in the
77888c9b54cSNate Lawson 	 * relative setting.
77988c9b54cSNate Lawson 	 */
78088c9b54cSNate Lawson 	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
78188c9b54cSNate Lawson 	if (fill == NULL)
78288c9b54cSNate Lawson 		return (NULL);
78388c9b54cSNate Lawson 	*fill = *dup;
78488c9b54cSNate Lawson 	fill_set = &fill->total_set;
78588c9b54cSNate Lawson 	fill_set->freq =
78688c9b54cSNate Lawson 	    ((uint64_t)fill_set->freq * set->freq) / 10000;
78788c9b54cSNate Lawson 	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
78888c9b54cSNate Lawson 		fill_set->power = ((uint64_t)fill_set->power * set->freq)
78988c9b54cSNate Lawson 		    / 10000;
79088c9b54cSNate Lawson 	}
79188c9b54cSNate Lawson 	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
79288c9b54cSNate Lawson 		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
79388c9b54cSNate Lawson 			fill_set->lat += set->lat;
79488c9b54cSNate Lawson 		else
79588c9b54cSNate Lawson 			fill_set->lat = set->lat;
79688c9b54cSNate Lawson 	}
7978d913481SNate Lawson 	CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
79888c9b54cSNate Lawson 
79988c9b54cSNate Lawson 	/*
80088c9b54cSNate Lawson 	 * If we copied an old level that we already modified (say, at 100%),
80188c9b54cSNate Lawson 	 * we need to remove that setting before adding this one.  Since we
80288c9b54cSNate Lawson 	 * process each setting array in order, we know any settings for this
80388c9b54cSNate Lawson 	 * driver will be found at the end.
80488c9b54cSNate Lawson 	 */
80588c9b54cSNate Lawson 	for (i = fill->rel_count; i != 0; i--) {
80688c9b54cSNate Lawson 		if (fill->rel_set[i - 1].dev != set->dev)
80788c9b54cSNate Lawson 			break;
8088d913481SNate Lawson 		CF_DEBUG("removed last relative driver: %s\n",
8098d913481SNate Lawson 		    device_get_nameunit(set->dev));
81088c9b54cSNate Lawson 		fill->rel_count--;
81188c9b54cSNate Lawson 	}
81288c9b54cSNate Lawson 
81388c9b54cSNate Lawson 	/*
8145308b2a6SNate Lawson 	 * Insert the new level in sorted order.  If it is a duplicate of an
8155308b2a6SNate Lawson 	 * existing level (1) or has an absolute setting higher than the
8165308b2a6SNate Lawson 	 * existing level (2), do not add it.  We can do this since any such
8175308b2a6SNate Lawson 	 * level is guaranteed use less power.  For example (1), a level with
8185308b2a6SNate Lawson 	 * one absolute setting of 800 Mhz uses less power than one composed
8195308b2a6SNate Lawson 	 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
8205308b2a6SNate Lawson 	 * Also for example (2), a level of 800 Mhz/75% is preferable to
8215308b2a6SNate Lawson 	 * 1600 Mhz/25% even though the latter has a lower total frequency.
82288c9b54cSNate Lawson 	 */
82388c9b54cSNate Lawson 	list = &sc->all_levels;
8245308b2a6SNate Lawson 	KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
82588c9b54cSNate Lawson 	TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
82688c9b54cSNate Lawson 		itr_set = &itr->total_set;
8279000b91eSNate Lawson 		if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
8289000b91eSNate Lawson 			CF_DEBUG("dup set rejecting %d (dupe)\n",
8299000b91eSNate Lawson 			    fill_set->freq);
8309000b91eSNate Lawson 			itr = NULL;
8319000b91eSNate Lawson 			break;
8329000b91eSNate Lawson 		} else if (fill_set->freq < itr_set->freq) {
8339000b91eSNate Lawson 			if (fill->abs_set.freq <= itr->abs_set.freq) {
8348d913481SNate Lawson 				CF_DEBUG(
8358d913481SNate Lawson 			"dup done, inserting new level %d after %d\n",
8368d913481SNate Lawson 				    fill_set->freq, itr_set->freq);
83788c9b54cSNate Lawson 				TAILQ_INSERT_AFTER(list, itr, fill, link);
83888c9b54cSNate Lawson 				sc->all_count++;
8399000b91eSNate Lawson 			} else {
8409000b91eSNate Lawson 				CF_DEBUG("dup set rejecting %d (abs too big)\n",
8419000b91eSNate Lawson 				    fill_set->freq);
8429000b91eSNate Lawson 				itr = NULL;
8439000b91eSNate Lawson 			}
84488c9b54cSNate Lawson 			break;
84588c9b54cSNate Lawson 		}
84688c9b54cSNate Lawson 	}
8475308b2a6SNate Lawson 
8485308b2a6SNate Lawson 	/* We didn't find a good place for this new level so free it. */
8495308b2a6SNate Lawson 	if (itr == NULL) {
8505308b2a6SNate Lawson 		CF_DEBUG("dup set freeing new level %d (not optimal)\n",
8515308b2a6SNate Lawson 		    fill_set->freq);
8525308b2a6SNate Lawson 		free(fill, M_TEMP);
8535308b2a6SNate Lawson 		fill = NULL;
85488c9b54cSNate Lawson 	}
85588c9b54cSNate Lawson 
85688c9b54cSNate Lawson 	return (fill);
85788c9b54cSNate Lawson }
85888c9b54cSNate Lawson 
85973347b07SNate Lawson static int
86073347b07SNate Lawson cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
86173347b07SNate Lawson {
86273347b07SNate Lawson 	struct cpufreq_softc *sc;
86373347b07SNate Lawson 	struct cf_level *levels;
8645f3818a5SAlexander Motin 	int best, count, diff, bdiff, devcount, error, freq, i, n;
8650325089dSNate Lawson 	device_t *devs;
86673347b07SNate Lawson 
8670325089dSNate Lawson 	devs = NULL;
86873347b07SNate Lawson 	sc = oidp->oid_arg1;
869dac50931SAndriy Gapon 	levels = sc->levels_buf;
87073347b07SNate Lawson 
87173347b07SNate Lawson 	error = CPUFREQ_GET(sc->dev, &levels[0]);
87273347b07SNate Lawson 	if (error)
87373347b07SNate Lawson 		goto out;
87473347b07SNate Lawson 	freq = levels[0].total_set.freq;
87573347b07SNate Lawson 	error = sysctl_handle_int(oidp, &freq, 0, req);
87673347b07SNate Lawson 	if (error != 0 || req->newptr == NULL)
87773347b07SNate Lawson 		goto out;
87873347b07SNate Lawson 
8790325089dSNate Lawson 	/*
8800325089dSNate Lawson 	 * While we only call cpufreq_get() on one device (assuming all
8810325089dSNate Lawson 	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
8820325089dSNate Lawson 	 * This is needed for some MP systems.
8830325089dSNate Lawson 	 */
8840325089dSNate Lawson 	error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
88573347b07SNate Lawson 	if (error)
88673347b07SNate Lawson 		goto out;
8870325089dSNate Lawson 	for (n = 0; n < devcount; n++) {
8880325089dSNate Lawson 		count = CF_MAX_LEVELS;
8890325089dSNate Lawson 		error = CPUFREQ_LEVELS(devs[n], levels, &count);
890d269386aSNate Lawson 		if (error) {
891d269386aSNate Lawson 			if (error == E2BIG)
892d269386aSNate Lawson 				printf(
893d269386aSNate Lawson 			"cpufreq: need to increase CF_MAX_LEVELS\n");
8940325089dSNate Lawson 			break;
895d269386aSNate Lawson 		}
8965f3818a5SAlexander Motin 		best = 0;
8975f3818a5SAlexander Motin 		bdiff = 1 << 30;
89873347b07SNate Lawson 		for (i = 0; i < count; i++) {
8995f3818a5SAlexander Motin 			diff = abs(levels[i].total_set.freq - freq);
9005f3818a5SAlexander Motin 			if (diff < bdiff) {
9015f3818a5SAlexander Motin 				bdiff = diff;
9025f3818a5SAlexander Motin 				best = i;
90373347b07SNate Lawson 			}
90473347b07SNate Lawson 		}
9055f3818a5SAlexander Motin 		error = CPUFREQ_SET(devs[n], &levels[best], CPUFREQ_PRIO_USER);
9060325089dSNate Lawson 	}
90773347b07SNate Lawson 
90873347b07SNate Lawson out:
9090325089dSNate Lawson 	if (devs)
9100325089dSNate Lawson 		free(devs, 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;
928dac50931SAndriy Gapon 	levels = sc->levels_buf;
92930215f48SChristian Brueffer 	if (levels == NULL) {
93030215f48SChristian Brueffer 		sbuf_delete(&sb);
93173347b07SNate Lawson 		return (ENOMEM);
93230215f48SChristian Brueffer 	}
93373347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
934d269386aSNate Lawson 	if (error) {
935d269386aSNate Lawson 		if (error == E2BIG)
936d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
93773347b07SNate Lawson 		goto out;
938d269386aSNate Lawson 	}
93973347b07SNate Lawson 	if (count) {
94073347b07SNate Lawson 		for (i = 0; i < count; i++) {
94173347b07SNate Lawson 			set = &levels[i].total_set;
94273347b07SNate Lawson 			sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
94373347b07SNate Lawson 		}
94473347b07SNate Lawson 	} else
94573347b07SNate Lawson 		sbuf_cpy(&sb, "0");
94673347b07SNate Lawson 	sbuf_trim(&sb);
94773347b07SNate Lawson 	sbuf_finish(&sb);
94873347b07SNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
94973347b07SNate Lawson 
95073347b07SNate Lawson out:
95173347b07SNate Lawson 	sbuf_delete(&sb);
95273347b07SNate Lawson 	return (error);
95373347b07SNate Lawson }
95473347b07SNate Lawson 
955e959a70bSNate Lawson static int
956e959a70bSNate Lawson cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
957e959a70bSNate Lawson {
958e959a70bSNate Lawson 	device_t dev;
959e959a70bSNate Lawson 	struct cf_setting *sets;
960e959a70bSNate Lawson 	struct sbuf sb;
961e959a70bSNate Lawson 	int error, i, set_count;
962e959a70bSNate Lawson 
963e959a70bSNate Lawson 	dev = oidp->oid_arg1;
964e959a70bSNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
965e959a70bSNate Lawson 
966e959a70bSNate Lawson 	/* Get settings from the device and generate the output string. */
967e959a70bSNate Lawson 	set_count = MAX_SETTINGS;
968e959a70bSNate Lawson 	sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
96930215f48SChristian Brueffer 	if (sets == NULL) {
97030215f48SChristian Brueffer 		sbuf_delete(&sb);
971e959a70bSNate Lawson 		return (ENOMEM);
97230215f48SChristian Brueffer 	}
973e959a70bSNate Lawson 	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
974e959a70bSNate Lawson 	if (error)
975e959a70bSNate Lawson 		goto out;
976e959a70bSNate Lawson 	if (set_count) {
977e959a70bSNate Lawson 		for (i = 0; i < set_count; i++)
978e959a70bSNate Lawson 			sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
979e959a70bSNate Lawson 	} else
980e959a70bSNate Lawson 		sbuf_cpy(&sb, "0");
981e959a70bSNate Lawson 	sbuf_trim(&sb);
982e959a70bSNate Lawson 	sbuf_finish(&sb);
983e959a70bSNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
984e959a70bSNate Lawson 
985e959a70bSNate Lawson out:
986e959a70bSNate Lawson 	free(sets, M_TEMP);
987e959a70bSNate Lawson 	sbuf_delete(&sb);
988e959a70bSNate Lawson 	return (error);
989e959a70bSNate Lawson }
990e959a70bSNate Lawson 
99173347b07SNate Lawson int
99273347b07SNate Lawson cpufreq_register(device_t dev)
99373347b07SNate Lawson {
99467c8649fSNate Lawson 	struct cpufreq_softc *sc;
99573347b07SNate Lawson 	device_t cf_dev, cpu_dev;
99673347b07SNate Lawson 
997e959a70bSNate Lawson 	/* Add a sysctl to get each driver's settings separately. */
998e959a70bSNate Lawson 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
999e959a70bSNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1000e959a70bSNate Lawson 	    OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
1001e959a70bSNate Lawson 	    cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
1002e959a70bSNate Lawson 
100373347b07SNate Lawson 	/*
10040325089dSNate Lawson 	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
10050325089dSNate Lawson 	 * must offer the same levels and be switched at the same time.
100673347b07SNate Lawson 	 */
10070325089dSNate Lawson 	cpu_dev = device_get_parent(dev);
100867c8649fSNate Lawson 	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
100967c8649fSNate Lawson 		sc = device_get_softc(cf_dev);
101067c8649fSNate Lawson 		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
101173347b07SNate Lawson 		return (0);
101267c8649fSNate Lawson 	}
101373347b07SNate Lawson 
10140325089dSNate Lawson 	/* Add the child device and possibly sysctls. */
10150325089dSNate Lawson 	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
101673347b07SNate Lawson 	if (cf_dev == NULL)
101773347b07SNate Lawson 		return (ENOMEM);
101873347b07SNate Lawson 	device_quiet(cf_dev);
101973347b07SNate Lawson 
102073347b07SNate Lawson 	return (device_probe_and_attach(cf_dev));
102173347b07SNate Lawson }
102273347b07SNate Lawson 
102373347b07SNate Lawson int
102473347b07SNate Lawson cpufreq_unregister(device_t dev)
102573347b07SNate Lawson {
102673347b07SNate Lawson 	device_t cf_dev, *devs;
1027e94a0c1aSNate Lawson 	int cfcount, devcount, error, i, type;
102873347b07SNate Lawson 
102973347b07SNate Lawson 	/*
103073347b07SNate Lawson 	 * If this is the last cpufreq child device, remove the control
103173347b07SNate Lawson 	 * device as well.  We identify cpufreq children by calling a method
103273347b07SNate Lawson 	 * they support.
103373347b07SNate Lawson 	 */
103473347b07SNate Lawson 	error = device_get_children(device_get_parent(dev), &devs, &devcount);
103573347b07SNate Lawson 	if (error)
103673347b07SNate Lawson 		return (error);
103767c8649fSNate Lawson 	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
103871ab130cSNate Lawson 	if (cf_dev == NULL) {
103971ab130cSNate Lawson 		device_printf(dev,
104071ab130cSNate Lawson 	"warning: cpufreq_unregister called with no cpufreq device active\n");
1041ed472910SChristian Brueffer 		free(devs, M_TEMP);
104271ab130cSNate Lawson 		return (0);
104371ab130cSNate Lawson 	}
104473347b07SNate Lawson 	cfcount = 0;
104573347b07SNate Lawson 	for (i = 0; i < devcount; i++) {
104673347b07SNate Lawson 		if (!device_is_attached(devs[i]))
104773347b07SNate Lawson 			continue;
1048e94a0c1aSNate Lawson 		if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
104973347b07SNate Lawson 			cfcount++;
105073347b07SNate Lawson 	}
10510325089dSNate Lawson 	if (cfcount <= 1)
105273347b07SNate Lawson 		device_delete_child(device_get_parent(cf_dev), cf_dev);
105373347b07SNate Lawson 	free(devs, M_TEMP);
105473347b07SNate Lawson 
105573347b07SNate Lawson 	return (0);
105673347b07SNate Lawson }
10570d4ac62aSNate Lawson 
10580d4ac62aSNate Lawson int
10590d4ac62aSNate Lawson cpufreq_settings_changed(device_t dev)
10600d4ac62aSNate Lawson {
10610d4ac62aSNate Lawson 
10620d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_levels_changed,
10630d4ac62aSNate Lawson 	    device_get_unit(device_get_parent(dev)));
10640d4ac62aSNate Lawson 	return (0);
10650d4ac62aSNate Lawson }
1066