xref: /freebsd/sys/kern/kern_cpu.c (revision ce968b095e75262ba3605491acf6850ce331481d)
173347b07SNate Lawson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
40d4ac62aSNate Lawson  * Copyright (c) 2004-2007 Nate Lawson (SDG)
573347b07SNate Lawson  * All rights reserved.
673347b07SNate Lawson  *
773347b07SNate Lawson  * Redistribution and use in source and binary forms, with or without
873347b07SNate Lawson  * modification, are permitted provided that the following conditions
973347b07SNate Lawson  * are met:
1073347b07SNate Lawson  * 1. Redistributions of source code must retain the above copyright
1173347b07SNate Lawson  *    notice, this list of conditions and the following disclaimer.
1273347b07SNate Lawson  * 2. Redistributions in binary form must reproduce the above copyright
1373347b07SNate Lawson  *    notice, this list of conditions and the following disclaimer in the
1473347b07SNate Lawson  *    documentation and/or other materials provided with the distribution.
1573347b07SNate Lawson  *
1673347b07SNate Lawson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1773347b07SNate Lawson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1873347b07SNate Lawson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1973347b07SNate Lawson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2073347b07SNate Lawson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2173347b07SNate Lawson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2273347b07SNate Lawson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2373347b07SNate Lawson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2473347b07SNate Lawson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2573347b07SNate Lawson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2673347b07SNate Lawson  * SUCH DAMAGE.
2773347b07SNate Lawson  */
2873347b07SNate Lawson 
2973347b07SNate Lawson #include <sys/param.h>
3073347b07SNate Lawson #include <sys/bus.h>
3173347b07SNate Lawson #include <sys/cpu.h>
3273347b07SNate Lawson #include <sys/eventhandler.h>
3373347b07SNate Lawson #include <sys/kernel.h>
34789f03ceSNate Lawson #include <sys/lock.h>
3573347b07SNate Lawson #include <sys/malloc.h>
3673347b07SNate Lawson #include <sys/module.h>
3773347b07SNate Lawson #include <sys/proc.h>
3873347b07SNate Lawson #include <sys/queue.h>
39a15e947dSNate Lawson #include <sys/sbuf.h>
4073347b07SNate Lawson #include <sys/sched.h>
41a15e947dSNate Lawson #include <sys/smp.h>
4273347b07SNate Lawson #include <sys/sysctl.h>
4373347b07SNate Lawson #include <sys/systm.h>
44789f03ceSNate Lawson #include <sys/sx.h>
450325089dSNate Lawson #include <sys/timetc.h>
460d4ac62aSNate Lawson #include <sys/taskqueue.h>
4773347b07SNate Lawson 
4873347b07SNate Lawson #include "cpufreq_if.h"
4973347b07SNate Lawson 
5073347b07SNate Lawson /*
5173347b07SNate Lawson  * Common CPU frequency glue code.  Drivers for specific hardware can
5273347b07SNate Lawson  * attach this interface to allow users to get/set the CPU frequency.
5373347b07SNate Lawson  */
5473347b07SNate Lawson 
5573347b07SNate Lawson /*
5673347b07SNate Lawson  * Number of levels we can handle.  Levels are synthesized from settings
57d269386aSNate Lawson  * so for M settings and N drivers, there may be M*N levels.
5873347b07SNate Lawson  */
5922c1b4c0SJustin Hibbits #define CF_MAX_LEVELS	256
6073347b07SNate Lawson 
6156e5a87aSHajimu UMEMOTO struct cf_saved_freq {
6256e5a87aSHajimu UMEMOTO 	struct cf_level			level;
6356e5a87aSHajimu UMEMOTO 	int				priority;
6456e5a87aSHajimu UMEMOTO 	SLIST_ENTRY(cf_saved_freq)	link;
6556e5a87aSHajimu UMEMOTO };
6656e5a87aSHajimu UMEMOTO 
6773347b07SNate Lawson struct cpufreq_softc {
68789f03ceSNate Lawson 	struct sx			lock;
6973347b07SNate Lawson 	struct cf_level			curr_level;
705f0afa04SNate Lawson 	int				curr_priority;
7156e5a87aSHajimu UMEMOTO 	SLIST_HEAD(, cf_saved_freq)	saved_freq;
7273347b07SNate Lawson 	struct cf_level_lst		all_levels;
735f0afa04SNate Lawson 	int				all_count;
7467c8649fSNate Lawson 	int				max_mhz;
7573347b07SNate Lawson 	device_t			dev;
764577cf37SConrad Meyer 	device_t			cf_drv_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 
100*ce968b09SJohn Baldwin static int	cpufreq_probe(device_t dev);
10173347b07SNate Lawson static int	cpufreq_attach(device_t dev);
1020d4ac62aSNate Lawson static void	cpufreq_startup_task(void *ctx, int pending);
10373347b07SNate Lawson static int	cpufreq_detach(device_t dev);
10473347b07SNate Lawson static int	cf_set_method(device_t dev, const struct cf_level *level,
10573347b07SNate Lawson 		    int priority);
10673347b07SNate Lawson static int	cf_get_method(device_t dev, struct cf_level *level);
10773347b07SNate Lawson static int	cf_levels_method(device_t dev, struct cf_level *levels,
10873347b07SNate Lawson 		    int *count);
10988c9b54cSNate Lawson static int	cpufreq_insert_abs(struct cpufreq_softc *sc,
11073347b07SNate Lawson 		    struct cf_setting *sets, int count);
11188c9b54cSNate Lawson static int	cpufreq_expand_set(struct cpufreq_softc *sc,
11288c9b54cSNate Lawson 		    struct cf_setting_array *set_arr);
11388c9b54cSNate Lawson static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
11488c9b54cSNate Lawson 		    struct cf_level *dup, struct cf_setting *set);
11573347b07SNate Lawson static int	cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
11673347b07SNate Lawson static int	cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
117e959a70bSNate Lawson static int	cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
11873347b07SNate Lawson 
11973347b07SNate Lawson static device_method_t cpufreq_methods[] = {
120*ce968b09SJohn Baldwin 	DEVMETHOD(device_probe,		cpufreq_probe),
12173347b07SNate Lawson 	DEVMETHOD(device_attach,	cpufreq_attach),
12273347b07SNate Lawson 	DEVMETHOD(device_detach,	cpufreq_detach),
12373347b07SNate Lawson 
12473347b07SNate Lawson         DEVMETHOD(cpufreq_set,		cf_set_method),
12573347b07SNate Lawson         DEVMETHOD(cpufreq_get,		cf_get_method),
12673347b07SNate Lawson         DEVMETHOD(cpufreq_levels,	cf_levels_method),
12773347b07SNate Lawson 	{0, 0}
12873347b07SNate Lawson };
1292fdcc2efSJohn Baldwin 
13073347b07SNate Lawson static driver_t cpufreq_driver = {
13173347b07SNate Lawson 	"cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
13273347b07SNate Lawson };
1332fdcc2efSJohn Baldwin 
1342fdcc2efSJohn Baldwin DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, 0, 0);
13573347b07SNate Lawson 
136b070969bSNate Lawson static int		cf_lowest_freq;
1378d913481SNate Lawson static int		cf_verbose;
1387029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1396472ac3dSEd Schouten     "cpufreq debugging");
140af3b2549SHans Petter Selasky SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RWTUN, &cf_lowest_freq, 1,
141b070969bSNate Lawson     "Don't provide levels below this frequency.");
142af3b2549SHans Petter Selasky SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RWTUN, &cf_verbose, 1,
1438d913481SNate Lawson     "Print verbose debugging messages");
144b070969bSNate Lawson 
145*ce968b09SJohn Baldwin static int
cpufreq_probe(device_t dev)146*ce968b09SJohn Baldwin cpufreq_probe(device_t dev)
147*ce968b09SJohn Baldwin {
148*ce968b09SJohn Baldwin 	device_set_desc(dev, "CPU frequency control");
149*ce968b09SJohn Baldwin 	return (BUS_PROBE_DEFAULT);
150*ce968b09SJohn Baldwin }
151*ce968b09SJohn Baldwin 
1524577cf37SConrad Meyer /*
1534577cf37SConrad Meyer  * This is called as the result of a hardware specific frequency control driver
1544577cf37SConrad Meyer  * calling cpufreq_register. It provides a general interface for system wide
1554577cf37SConrad Meyer  * frequency controls and operates on a per cpu basis.
1564577cf37SConrad Meyer  */
15773347b07SNate Lawson static int
cpufreq_attach(device_t dev)15873347b07SNate Lawson cpufreq_attach(device_t dev)
15973347b07SNate Lawson {
16073347b07SNate Lawson 	struct cpufreq_softc *sc;
161d288bcc4SAlexander Motin 	struct pcpu *pc;
16273347b07SNate Lawson 	device_t parent;
163d288bcc4SAlexander Motin 	uint64_t rate;
16473347b07SNate Lawson 
1658d913481SNate Lawson 	CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
16673347b07SNate Lawson 	sc = device_get_softc(dev);
16773347b07SNate Lawson 	parent = device_get_parent(dev);
16873347b07SNate Lawson 	sc->dev = dev;
16973347b07SNate Lawson 	sysctl_ctx_init(&sc->sysctl_ctx);
17073347b07SNate Lawson 	TAILQ_INIT(&sc->all_levels);
171789f03ceSNate Lawson 	CF_MTX_INIT(&sc->lock);
17273347b07SNate Lawson 	sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
17356e5a87aSHajimu UMEMOTO 	SLIST_INIT(&sc->saved_freq);
174f436f175SNathan Whitehorn 	/* Try to get nominal CPU freq to use it as maximum later if needed */
175f436f175SNathan Whitehorn 	sc->max_mhz = cpu_get_nominal_mhz(dev);
176f436f175SNathan Whitehorn 	/* If that fails, try to measure the current rate */
177f436f175SNathan Whitehorn 	if (sc->max_mhz <= 0) {
1784577cf37SConrad Meyer 		CF_DEBUG("Unable to obtain nominal frequency.\n");
179d288bcc4SAlexander Motin 		pc = cpu_get_pcpu(dev);
180d288bcc4SAlexander Motin 		if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
181d288bcc4SAlexander Motin 			sc->max_mhz = rate / 1000000;
182d288bcc4SAlexander Motin 		else
18367c8649fSNate Lawson 			sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
184f436f175SNathan Whitehorn 	}
18573347b07SNate Lawson 
1868d913481SNate Lawson 	CF_DEBUG("initializing one-time data for %s\n",
1878d913481SNate Lawson 	    device_get_nameunit(dev));
188dac50931SAndriy Gapon 	sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf),
189dac50931SAndriy Gapon 	    M_DEVBUF, M_WAITOK);
19073347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
19173347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
1927029da5cSPawel Biernacki 	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1937029da5cSPawel Biernacki 	    sc, 0, cpufreq_curr_sysctl, "I", "Current CPU frequency");
19473347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
19573347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
1967029da5cSPawel Biernacki 	    OID_AUTO, "freq_levels",
1977029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
19873347b07SNate Lawson 	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
1990d4ac62aSNate Lawson 
2000d4ac62aSNate Lawson 	/*
2010d4ac62aSNate Lawson 	 * Queue a one-shot broadcast that levels have changed.
2020d4ac62aSNate Lawson 	 * It will run once the system has completed booting.
2030d4ac62aSNate Lawson 	 */
2040d4ac62aSNate Lawson 	TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
2050d4ac62aSNate Lawson 	taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
20673347b07SNate Lawson 
20773347b07SNate Lawson 	return (0);
20873347b07SNate Lawson }
20973347b07SNate Lawson 
2100d4ac62aSNate Lawson /* Handle any work to be done for all drivers that attached during boot. */
2110d4ac62aSNate Lawson static void
cpufreq_startup_task(void * ctx,int pending)2120d4ac62aSNate Lawson cpufreq_startup_task(void *ctx, int pending)
2130d4ac62aSNate Lawson {
2140d4ac62aSNate Lawson 
2150d4ac62aSNate Lawson 	cpufreq_settings_changed((device_t)ctx);
2160d4ac62aSNate Lawson }
2170d4ac62aSNate Lawson 
21873347b07SNate Lawson static int
cpufreq_detach(device_t dev)21973347b07SNate Lawson cpufreq_detach(device_t dev)
22073347b07SNate Lawson {
22173347b07SNate Lawson 	struct cpufreq_softc *sc;
22256e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq;
22373347b07SNate Lawson 
2248d913481SNate Lawson 	CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
22573347b07SNate Lawson 	sc = device_get_softc(dev);
22673347b07SNate Lawson 	sysctl_ctx_free(&sc->sysctl_ctx);
22773347b07SNate Lawson 
22856e5a87aSHajimu UMEMOTO 	while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
22956e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
23056e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
23156e5a87aSHajimu UMEMOTO 	}
23256e5a87aSHajimu UMEMOTO 
233dac50931SAndriy Gapon 	free(sc->levels_buf, M_DEVBUF);
23473347b07SNate Lawson 
23573347b07SNate Lawson 	return (0);
23673347b07SNate Lawson }
23773347b07SNate Lawson 
23873347b07SNate Lawson static int
cf_set_method(device_t dev,const struct cf_level * level,int priority)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;
246e7bd0750SAndrew Gallatin 	u_char pri;
24773347b07SNate Lawson 
24873347b07SNate Lawson 	sc = device_get_softc(dev);
249789f03ceSNate Lawson 	error = 0;
250789f03ceSNate Lawson 	set = NULL;
25156e5a87aSHajimu UMEMOTO 	saved_freq = NULL;
25273347b07SNate Lawson 
2530d4ac62aSNate Lawson 	/* We are going to change levels so notify the pre-change handler. */
2540d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
2550d4ac62aSNate Lawson 	if (error != 0) {
2560d4ac62aSNate Lawson 		EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
2570d4ac62aSNate Lawson 		return (error);
258b4130b8aSMarcus Alves Grando 	}
2590325089dSNate Lawson 
260789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
26156e5a87aSHajimu UMEMOTO 
262a15e947dSNate Lawson #ifdef SMP
263fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
264fdce57a0SJohn Baldwin 	MPASS(mp_ncpus == 1 || smp_started);
265fdce57a0SJohn Baldwin #else
266a15e947dSNate Lawson 	/*
267a15e947dSNate Lawson 	 * If still booting and secondary CPUs not started yet, don't allow
268a15e947dSNate Lawson 	 * changing the frequency until they're online.  This is because we
269a15e947dSNate Lawson 	 * can't switch to them using sched_bind() and thus we'd only be
270a15e947dSNate Lawson 	 * switching the main CPU.  XXXTODO: Need to think more about how to
271a15e947dSNate Lawson 	 * handle having different CPUs at different frequencies.
272a15e947dSNate Lawson 	 */
27360ad8150SScott Long 	if (mp_ncpus > 1 && !smp_started) {
274a15e947dSNate Lawson 		device_printf(dev, "rejecting change, SMP not started yet\n");
275a15e947dSNate Lawson 		error = ENXIO;
276a15e947dSNate Lawson 		goto out;
277a15e947dSNate Lawson 	}
278fdce57a0SJohn Baldwin #endif
279a15e947dSNate Lawson #endif /* SMP */
280a15e947dSNate Lawson 
28156e5a87aSHajimu UMEMOTO 	/*
28256e5a87aSHajimu UMEMOTO 	 * If the requested level has a lower priority, don't allow
28356e5a87aSHajimu UMEMOTO 	 * the new level right now.
28456e5a87aSHajimu UMEMOTO 	 */
28556e5a87aSHajimu UMEMOTO 	if (priority < sc->curr_priority) {
2868d913481SNate Lawson 		CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
2878d913481SNate Lawson 		    sc->curr_priority);
288789f03ceSNate Lawson 		error = EPERM;
289789f03ceSNate Lawson 		goto out;
290789f03ceSNate Lawson 	}
2915f0afa04SNate Lawson 
29256e5a87aSHajimu UMEMOTO 	/*
29356e5a87aSHajimu UMEMOTO 	 * If the caller didn't specify a level and one is saved, prepare to
29456e5a87aSHajimu UMEMOTO 	 * restore the saved level.  If none has been saved, return an error.
29556e5a87aSHajimu UMEMOTO 	 */
29656e5a87aSHajimu UMEMOTO 	if (level == NULL) {
29756e5a87aSHajimu UMEMOTO 		saved_freq = SLIST_FIRST(&sc->saved_freq);
29856e5a87aSHajimu UMEMOTO 		if (saved_freq == NULL) {
29956e5a87aSHajimu UMEMOTO 			CF_DEBUG("NULL level, no saved level\n");
30056e5a87aSHajimu UMEMOTO 			error = ENXIO;
30156e5a87aSHajimu UMEMOTO 			goto out;
30256e5a87aSHajimu UMEMOTO 		}
30356e5a87aSHajimu UMEMOTO 		level = &saved_freq->level;
30456e5a87aSHajimu UMEMOTO 		priority = saved_freq->priority;
30556e5a87aSHajimu UMEMOTO 		CF_DEBUG("restoring saved level, freq %d prio %d\n",
30656e5a87aSHajimu UMEMOTO 		    level->total_set.freq, priority);
30756e5a87aSHajimu UMEMOTO 	}
30856e5a87aSHajimu UMEMOTO 
309b070969bSNate Lawson 	/* Reject levels that are below our specified threshold. */
310da8a77c1SNate Lawson 	if (level->total_set.freq < cf_lowest_freq) {
3118d913481SNate Lawson 		CF_DEBUG("rejecting freq %d, less than %d limit\n",
3128d913481SNate Lawson 		    level->total_set.freq, cf_lowest_freq);
313789f03ceSNate Lawson 		error = EINVAL;
314789f03ceSNate Lawson 		goto out;
315789f03ceSNate Lawson 	}
316b070969bSNate Lawson 
31773347b07SNate Lawson 	/* If already at this level, just return. */
3185f3818a5SAlexander Motin 	if (sc->curr_level.total_set.freq == level->total_set.freq) {
3198d913481SNate Lawson 		CF_DEBUG("skipping freq %d, same as current level %d\n",
3208d913481SNate Lawson 		    level->total_set.freq, sc->curr_level.total_set.freq);
3211fea6ce7SHajimu UMEMOTO 		goto skip;
3228d913481SNate Lawson 	}
32373347b07SNate Lawson 
32473347b07SNate Lawson 	/* First, set the absolute frequency via its driver. */
32573347b07SNate Lawson 	set = &level->abs_set;
32673347b07SNate Lawson 	if (set->dev) {
32773347b07SNate Lawson 		if (!device_is_attached(set->dev)) {
32873347b07SNate Lawson 			error = ENXIO;
32973347b07SNate Lawson 			goto out;
33073347b07SNate Lawson 		}
3311196826aSNate Lawson 
33262db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3331196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
3345ab94f48SMichal Meloun 
3355ab94f48SMichal Meloun 		/* Skip settings if CPU is not started. */
3365ab94f48SMichal Meloun 		if (pc == NULL) {
3375ab94f48SMichal Meloun 			error = 0;
3385ab94f48SMichal Meloun 			goto out;
3395ab94f48SMichal Meloun 		}
340982d11f8SJeff Roberson 		thread_lock(curthread);
341e7bd0750SAndrew Gallatin 		pri = curthread->td_priority;
342e7bd0750SAndrew Gallatin 		sched_prio(curthread, PRI_MIN);
3431196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
344982d11f8SJeff Roberson 		thread_unlock(curthread);
3458d913481SNate Lawson 		CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
3468d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
34773347b07SNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
348982d11f8SJeff Roberson 		thread_lock(curthread);
3491196826aSNate Lawson 		sched_unbind(curthread);
350e7bd0750SAndrew Gallatin 		sched_prio(curthread, pri);
351982d11f8SJeff Roberson 		thread_unlock(curthread);
35273347b07SNate Lawson 		if (error) {
35373347b07SNate Lawson 			goto out;
35473347b07SNate Lawson 		}
35573347b07SNate Lawson 	}
35673347b07SNate Lawson 
35788c9b54cSNate Lawson 	/* Next, set any/all relative frequencies via their drivers. */
35888c9b54cSNate Lawson 	for (i = 0; i < level->rel_count; i++) {
35988c9b54cSNate Lawson 		set = &level->rel_set[i];
36088c9b54cSNate Lawson 		if (!device_is_attached(set->dev)) {
36188c9b54cSNate Lawson 			error = ENXIO;
36288c9b54cSNate Lawson 			goto out;
36388c9b54cSNate Lawson 		}
3641196826aSNate Lawson 
36562db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3661196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
367982d11f8SJeff Roberson 		thread_lock(curthread);
368e7bd0750SAndrew Gallatin 		pri = curthread->td_priority;
369e7bd0750SAndrew Gallatin 		sched_prio(curthread, PRI_MIN);
3701196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
371982d11f8SJeff Roberson 		thread_unlock(curthread);
3728d913481SNate Lawson 		CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
3738d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
37488c9b54cSNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
375982d11f8SJeff Roberson 		thread_lock(curthread);
3761196826aSNate Lawson 		sched_unbind(curthread);
377e7bd0750SAndrew Gallatin 		sched_prio(curthread, pri);
378982d11f8SJeff Roberson 		thread_unlock(curthread);
37988c9b54cSNate Lawson 		if (error) {
38088c9b54cSNate Lawson 			/* XXX Back out any successful setting? */
38188c9b54cSNate Lawson 			goto out;
38288c9b54cSNate Lawson 		}
38388c9b54cSNate Lawson 	}
38473347b07SNate Lawson 
3851fea6ce7SHajimu UMEMOTO skip:
3865f0afa04SNate Lawson 	/*
3875f0afa04SNate Lawson 	 * Before recording the current level, check if we're going to a
38856e5a87aSHajimu UMEMOTO 	 * higher priority.  If so, save the previous level and priority.
3895f0afa04SNate Lawson 	 */
3905f0afa04SNate Lawson 	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
39156e5a87aSHajimu UMEMOTO 	    priority > sc->curr_priority) {
3928d913481SNate Lawson 		CF_DEBUG("saving level, freq %d prio %d\n",
3938d913481SNate Lawson 		    sc->curr_level.total_set.freq, sc->curr_priority);
39456e5a87aSHajimu UMEMOTO 		curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
39556e5a87aSHajimu UMEMOTO 		if (curr_freq == NULL) {
39656e5a87aSHajimu UMEMOTO 			error = ENOMEM;
39756e5a87aSHajimu UMEMOTO 			goto out;
39856e5a87aSHajimu UMEMOTO 		}
39956e5a87aSHajimu UMEMOTO 		curr_freq->level = sc->curr_level;
40056e5a87aSHajimu UMEMOTO 		curr_freq->priority = sc->curr_priority;
40156e5a87aSHajimu UMEMOTO 		SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
4025f0afa04SNate Lawson 	}
40373347b07SNate Lawson 	sc->curr_level = *level;
4045f0afa04SNate Lawson 	sc->curr_priority = priority;
40573347b07SNate Lawson 
40656e5a87aSHajimu UMEMOTO 	/* If we were restoring a saved state, reset it to "unused". */
40756e5a87aSHajimu UMEMOTO 	if (saved_freq != NULL) {
40856e5a87aSHajimu UMEMOTO 		CF_DEBUG("resetting saved level\n");
40956e5a87aSHajimu UMEMOTO 		sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
41056e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
41156e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
41256e5a87aSHajimu UMEMOTO 	}
41356e5a87aSHajimu UMEMOTO 
41473347b07SNate Lawson out:
415789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
4160d4ac62aSNate Lawson 
4170d4ac62aSNate Lawson 	/*
4180d4ac62aSNate Lawson 	 * We changed levels (or attempted to) so notify the post-change
4190d4ac62aSNate Lawson 	 * handler of new frequency or error.
4200d4ac62aSNate Lawson 	 */
4210d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
422789f03ceSNate Lawson 	if (error && set)
42373347b07SNate Lawson 		device_printf(set->dev, "set freq failed, err %d\n", error);
4240d4ac62aSNate Lawson 
42573347b07SNate Lawson 	return (error);
42673347b07SNate Lawson }
42773347b07SNate Lawson 
42873347b07SNate Lawson static int
cpufreq_get_frequency(device_t dev)4294577cf37SConrad Meyer cpufreq_get_frequency(device_t dev)
4304577cf37SConrad Meyer {
4314577cf37SConrad Meyer 	struct cf_setting set;
4324577cf37SConrad Meyer 
4334577cf37SConrad Meyer 	if (CPUFREQ_DRV_GET(dev, &set) != 0)
4344577cf37SConrad Meyer 		return (-1);
4354577cf37SConrad Meyer 
4364577cf37SConrad Meyer 	return (set.freq);
4374577cf37SConrad Meyer }
4384577cf37SConrad Meyer 
4394577cf37SConrad Meyer /* Returns the index into *levels with the match */
4404577cf37SConrad Meyer static int
cpufreq_get_level(device_t dev,struct cf_level * levels,int count)4414577cf37SConrad Meyer cpufreq_get_level(device_t dev, struct cf_level *levels, int count)
4424577cf37SConrad Meyer {
4434577cf37SConrad Meyer 	int i, freq;
4444577cf37SConrad Meyer 
4454577cf37SConrad Meyer 	if ((freq = cpufreq_get_frequency(dev)) < 0)
4464577cf37SConrad Meyer 		return (-1);
4474577cf37SConrad Meyer 	for (i = 0; i < count; i++)
4484577cf37SConrad Meyer 		if (freq == levels[i].total_set.freq)
4494577cf37SConrad Meyer 			return (i);
4504577cf37SConrad Meyer 
4514577cf37SConrad Meyer 	return (-1);
4524577cf37SConrad Meyer }
4534577cf37SConrad Meyer 
4544577cf37SConrad Meyer /*
4554577cf37SConrad Meyer  * Used by the cpufreq core, this function will populate *level with the current
4564577cf37SConrad Meyer  * frequency as either determined by a cached value sc->curr_level, or in the
4574577cf37SConrad Meyer  * case the lower level driver has set the CPUFREQ_FLAG_UNCACHED flag, it will
4584577cf37SConrad Meyer  * obtain the frequency from the driver itself.
4594577cf37SConrad Meyer  */
4604577cf37SConrad Meyer static int
cf_get_method(device_t dev,struct cf_level * level)46173347b07SNate Lawson cf_get_method(device_t dev, struct cf_level *level)
46273347b07SNate Lawson {
46373347b07SNate Lawson 	struct cpufreq_softc *sc;
46473347b07SNate Lawson 	struct cf_level *levels;
4654577cf37SConrad Meyer 	struct cf_setting *curr_set;
46673347b07SNate Lawson 	struct pcpu *pc;
4674577cf37SConrad Meyer 	int bdiff, count, diff, error, i, type;
46873347b07SNate Lawson 	uint64_t rate;
46973347b07SNate Lawson 
47073347b07SNate Lawson 	sc = device_get_softc(dev);
471789f03ceSNate Lawson 	error = 0;
47273347b07SNate Lawson 	levels = NULL;
47373347b07SNate Lawson 
4744577cf37SConrad Meyer 	/*
4754577cf37SConrad Meyer 	 * If we already know the current frequency, and the driver didn't ask
4764577cf37SConrad Meyer 	 * for uncached usage, we're done.
4774577cf37SConrad Meyer 	 */
478789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
479789f03ceSNate Lawson 	curr_set = &sc->curr_level.total_set;
4804577cf37SConrad Meyer 	error = CPUFREQ_DRV_TYPE(sc->cf_drv_dev, &type);
4814577cf37SConrad Meyer 	if (error == 0 && (type & CPUFREQ_FLAG_UNCACHED)) {
4824577cf37SConrad Meyer 		struct cf_setting set;
4834577cf37SConrad Meyer 
4844577cf37SConrad Meyer 		/*
4854577cf37SConrad Meyer 		 * If the driver wants to always report back the real frequency,
4864577cf37SConrad Meyer 		 * first try the driver and if that fails, fall back to
4874577cf37SConrad Meyer 		 * estimating.
4884577cf37SConrad Meyer 		 */
48907a65f9dSConrad Meyer 		if (CPUFREQ_DRV_GET(sc->cf_drv_dev, &set) == 0) {
4904577cf37SConrad Meyer 			sc->curr_level.total_set = set;
49107a65f9dSConrad Meyer 			CF_DEBUG("get returning immediate freq %d\n",
49207a65f9dSConrad Meyer 			    curr_set->freq);
4934577cf37SConrad Meyer 			goto out;
49407a65f9dSConrad Meyer 		}
4954577cf37SConrad Meyer 	} else if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4968d913481SNate Lawson 		CF_DEBUG("get returning known freq %d\n", curr_set->freq);
4974577cf37SConrad Meyer 		error = 0;
49873347b07SNate Lawson 		goto out;
4998d913481SNate Lawson 	}
500789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
50173347b07SNate Lawson 
50273347b07SNate Lawson 	/*
50373347b07SNate Lawson 	 * We need to figure out the current level.  Loop through every
50473347b07SNate Lawson 	 * driver, getting the current setting.  Then, attempt to get a best
50573347b07SNate Lawson 	 * match of settings against each level.
50673347b07SNate Lawson 	 */
50773347b07SNate Lawson 	count = CF_MAX_LEVELS;
508ac2fffa4SPedro F. Giffuni 	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
50973347b07SNate Lawson 	if (levels == NULL)
51073347b07SNate Lawson 		return (ENOMEM);
51173347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
512d269386aSNate Lawson 	if (error) {
513d269386aSNate Lawson 		if (error == E2BIG)
514d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
515789f03ceSNate Lawson 		free(levels, M_TEMP);
516789f03ceSNate Lawson 		return (error);
517d269386aSNate Lawson 	}
518789f03ceSNate Lawson 
519789f03ceSNate Lawson 	/*
520789f03ceSNate Lawson 	 * Reacquire the lock and search for the given level.
521789f03ceSNate Lawson 	 *
522789f03ceSNate Lawson 	 * XXX Note: this is not quite right since we really need to go
523789f03ceSNate Lawson 	 * through each level and compare both absolute and relative
524789f03ceSNate Lawson 	 * settings for each driver in the system before making a match.
525789f03ceSNate Lawson 	 * The estimation code below catches this case though.
526789f03ceSNate Lawson 	 */
527789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
5284577cf37SConrad Meyer 	i = cpufreq_get_level(sc->cf_drv_dev, levels, count);
5294577cf37SConrad Meyer 	if (i >= 0)
53073347b07SNate Lawson 		sc->curr_level = levels[i];
5314577cf37SConrad Meyer 	else
5324577cf37SConrad Meyer 		CF_DEBUG("Couldn't find supported level for %s\n",
5334577cf37SConrad Meyer 		    device_get_nameunit(sc->cf_drv_dev));
5344577cf37SConrad Meyer 
5358d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
5368d913481SNate Lawson 		CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
53773347b07SNate Lawson 		goto out;
5388d913481SNate Lawson 	}
53973347b07SNate Lawson 
54073347b07SNate Lawson 	/*
54173347b07SNate Lawson 	 * We couldn't find an exact match, so attempt to estimate and then
54273347b07SNate Lawson 	 * match against a level.
54373347b07SNate Lawson 	 */
54473347b07SNate Lawson 	pc = cpu_get_pcpu(dev);
54573347b07SNate Lawson 	if (pc == NULL) {
54673347b07SNate Lawson 		error = ENXIO;
54773347b07SNate Lawson 		goto out;
54873347b07SNate Lawson 	}
54973347b07SNate Lawson 	cpu_est_clockrate(pc->pc_cpuid, &rate);
55073347b07SNate Lawson 	rate /= 1000000;
551760f4decSColin Percival 	bdiff = 1 << 30;
55273347b07SNate Lawson 	for (i = 0; i < count; i++) {
553760f4decSColin Percival 		diff = abs(levels[i].total_set.freq - rate);
554760f4decSColin Percival 		if (diff < bdiff) {
555760f4decSColin Percival 			bdiff = diff;
55673347b07SNate Lawson 			sc->curr_level = levels[i];
557760f4decSColin Percival 		}
558760f4decSColin Percival 	}
5598d913481SNate Lawson 	CF_DEBUG("get estimated freq %d\n", curr_set->freq);
56073347b07SNate Lawson 
56173347b07SNate Lawson out:
562789f03ceSNate Lawson 	if (error == 0)
563789f03ceSNate Lawson 		*level = sc->curr_level;
564789f03ceSNate Lawson 
565789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
56673347b07SNate Lawson 	if (levels)
56773347b07SNate Lawson 		free(levels, M_TEMP);
568789f03ceSNate Lawson 	return (error);
56973347b07SNate Lawson }
57073347b07SNate Lawson 
5714577cf37SConrad Meyer /*
5724577cf37SConrad Meyer  * Either directly obtain settings from the cpufreq driver, or build a list of
5734577cf37SConrad Meyer  * relative settings to be integrated later against an absolute max.
5744577cf37SConrad Meyer  */
57573347b07SNate Lawson static int
cpufreq_add_levels(device_t cf_dev,struct cf_setting_lst * rel_sets)5764577cf37SConrad Meyer cpufreq_add_levels(device_t cf_dev, struct cf_setting_lst *rel_sets)
57773347b07SNate Lawson {
57888c9b54cSNate Lawson 	struct cf_setting_array *set_arr;
57973347b07SNate Lawson 	struct cf_setting *sets;
5804577cf37SConrad Meyer 	device_t dev;
5814577cf37SConrad Meyer 	struct cpufreq_softc *sc;
5824577cf37SConrad Meyer 	int type, set_count, error;
58373347b07SNate Lawson 
5844577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
5854577cf37SConrad Meyer 	dev = sc->cf_drv_dev;
58673347b07SNate Lawson 
587e22cd41cSNate Lawson 	/* Skip devices that aren't ready. */
5884577cf37SConrad Meyer 	if (!device_is_attached(cf_dev))
5894577cf37SConrad Meyer 		return (0);
590e22cd41cSNate Lawson 
591e22cd41cSNate Lawson 	/*
592e22cd41cSNate Lawson 	 * Get settings, skipping drivers that offer no settings or
593e22cd41cSNate Lawson 	 * provide settings for informational purposes only.
594e22cd41cSNate Lawson 	 */
5954577cf37SConrad Meyer 	error = CPUFREQ_DRV_TYPE(dev, &type);
5964577cf37SConrad Meyer 	if (error != 0 || (type & CPUFREQ_FLAG_INFO_ONLY)) {
5978d913481SNate Lawson 		if (error == 0) {
5988d913481SNate Lawson 			CF_DEBUG("skipping info-only driver %s\n",
5994577cf37SConrad Meyer 			    device_get_nameunit(cf_dev));
6008d913481SNate Lawson 		}
6014577cf37SConrad Meyer 		return (error);
6028d913481SNate Lawson 	}
6034577cf37SConrad Meyer 
6044577cf37SConrad Meyer 	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
6054577cf37SConrad Meyer 	if (sets == NULL)
6064577cf37SConrad Meyer 		return (ENOMEM);
6074577cf37SConrad Meyer 
60873347b07SNate Lawson 	set_count = MAX_SETTINGS;
6094577cf37SConrad Meyer 	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
6104577cf37SConrad Meyer 	if (error != 0 || set_count == 0)
6114577cf37SConrad Meyer 		goto out;
61273347b07SNate Lawson 
613e22cd41cSNate Lawson 	/* Add the settings to our absolute/relative lists. */
6140325089dSNate Lawson 	switch (type & CPUFREQ_TYPE_MASK) {
61588c9b54cSNate Lawson 	case CPUFREQ_TYPE_ABSOLUTE:
61688c9b54cSNate Lawson 		error = cpufreq_insert_abs(sc, sets, set_count);
61788c9b54cSNate Lawson 		break;
61888c9b54cSNate Lawson 	case CPUFREQ_TYPE_RELATIVE:
6198d913481SNate Lawson 		CF_DEBUG("adding %d relative settings\n", set_count);
62088c9b54cSNate Lawson 		set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
62188c9b54cSNate Lawson 		if (set_arr == NULL) {
62288c9b54cSNate Lawson 			error = ENOMEM;
62388c9b54cSNate Lawson 			goto out;
62488c9b54cSNate Lawson 		}
62588c9b54cSNate Lawson 		bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
62688c9b54cSNate Lawson 		set_arr->count = set_count;
6274577cf37SConrad Meyer 		TAILQ_INSERT_TAIL(rel_sets, set_arr, link);
62888c9b54cSNate Lawson 		break;
62988c9b54cSNate Lawson 	default:
63088c9b54cSNate Lawson 		error = EINVAL;
63188c9b54cSNate Lawson 	}
6324577cf37SConrad Meyer 
6334577cf37SConrad Meyer out:
6344577cf37SConrad Meyer 	free(sets, M_TEMP);
6354577cf37SConrad Meyer 	return (error);
6364577cf37SConrad Meyer }
6374577cf37SConrad Meyer 
6384577cf37SConrad Meyer static int
cf_levels_method(device_t dev,struct cf_level * levels,int * count)6394577cf37SConrad Meyer cf_levels_method(device_t dev, struct cf_level *levels, int *count)
6404577cf37SConrad Meyer {
6414577cf37SConrad Meyer 	struct cf_setting_array *set_arr;
6424577cf37SConrad Meyer 	struct cf_setting_lst rel_sets;
6434577cf37SConrad Meyer 	struct cpufreq_softc *sc;
6444577cf37SConrad Meyer 	struct cf_level *lev;
6454577cf37SConrad Meyer 	struct pcpu *pc;
6464577cf37SConrad Meyer 	int error, i;
6474577cf37SConrad Meyer 	uint64_t rate;
6484577cf37SConrad Meyer 
6494577cf37SConrad Meyer 	if (levels == NULL || count == NULL)
6504577cf37SConrad Meyer 		return (EINVAL);
6514577cf37SConrad Meyer 
6524577cf37SConrad Meyer 	TAILQ_INIT(&rel_sets);
6534577cf37SConrad Meyer 	sc = device_get_softc(dev);
6544577cf37SConrad Meyer 
6554577cf37SConrad Meyer 	CF_MTX_LOCK(&sc->lock);
6564577cf37SConrad Meyer 	error = cpufreq_add_levels(sc->dev, &rel_sets);
65788c9b54cSNate Lawson 	if (error)
65873347b07SNate Lawson 		goto out;
65973347b07SNate Lawson 
66067c8649fSNate Lawson 	/*
66167c8649fSNate Lawson 	 * If there are no absolute levels, create a fake one at 100%.  We
66267c8649fSNate Lawson 	 * then cache the clockrate for later use as our base frequency.
66367c8649fSNate Lawson 	 */
66473347b07SNate Lawson 	if (TAILQ_EMPTY(&sc->all_levels)) {
6654577cf37SConrad Meyer 		struct cf_setting set;
6664577cf37SConrad Meyer 
6674577cf37SConrad Meyer 		CF_DEBUG("No absolute levels returned by driver\n");
6684577cf37SConrad Meyer 
66967c8649fSNate Lawson 		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
670f436f175SNathan Whitehorn 			sc->max_mhz = cpu_get_nominal_mhz(dev);
671f436f175SNathan Whitehorn 			/*
672f436f175SNathan Whitehorn 			 * If the CPU can't report a rate for 100%, hope
673f436f175SNathan Whitehorn 			 * the CPU is running at its nominal rate right now,
674f436f175SNathan Whitehorn 			 * and use that instead.
675f436f175SNathan Whitehorn 			 */
676f436f175SNathan Whitehorn 			if (sc->max_mhz <= 0) {
67773347b07SNate Lawson 				pc = cpu_get_pcpu(dev);
67873347b07SNate Lawson 				cpu_est_clockrate(pc->pc_cpuid, &rate);
67967c8649fSNate Lawson 				sc->max_mhz = rate / 1000000;
68067c8649fSNate Lawson 			}
681f436f175SNathan Whitehorn 		}
6824577cf37SConrad Meyer 		memset(&set, CPUFREQ_VAL_UNKNOWN, sizeof(set));
6834577cf37SConrad Meyer 		set.freq = sc->max_mhz;
6844577cf37SConrad Meyer 		set.dev = NULL;
6854577cf37SConrad Meyer 		error = cpufreq_insert_abs(sc, &set, 1);
68673347b07SNate Lawson 		if (error)
68773347b07SNate Lawson 			goto out;
68873347b07SNate Lawson 	}
68973347b07SNate Lawson 
69088c9b54cSNate Lawson 	/* Create a combined list of absolute + relative levels. */
69188c9b54cSNate Lawson 	TAILQ_FOREACH(set_arr, &rel_sets, link)
69288c9b54cSNate Lawson 		cpufreq_expand_set(sc, set_arr);
69388c9b54cSNate Lawson 
69488c9b54cSNate Lawson 	/* If the caller doesn't have enough space, return the actual count. */
69588c9b54cSNate Lawson 	if (sc->all_count > *count) {
69688c9b54cSNate Lawson 		*count = sc->all_count;
69788c9b54cSNate Lawson 		error = E2BIG;
69888c9b54cSNate Lawson 		goto out;
69988c9b54cSNate Lawson 	}
70088c9b54cSNate Lawson 
70188c9b54cSNate Lawson 	/* Finally, output the list of levels. */
70273347b07SNate Lawson 	i = 0;
70373347b07SNate Lawson 	TAILQ_FOREACH(lev, &sc->all_levels, link) {
704b070969bSNate Lawson 		/* Skip levels that have a frequency that is too low. */
705da8a77c1SNate Lawson 		if (lev->total_set.freq < cf_lowest_freq) {
706b070969bSNate Lawson 			sc->all_count--;
707b070969bSNate Lawson 			continue;
708b070969bSNate Lawson 		}
709b070969bSNate Lawson 
71073347b07SNate Lawson 		levels[i] = *lev;
71173347b07SNate Lawson 		i++;
71273347b07SNate Lawson 	}
71388c9b54cSNate Lawson 	*count = sc->all_count;
71473347b07SNate Lawson 	error = 0;
71573347b07SNate Lawson 
71673347b07SNate Lawson out:
71773347b07SNate Lawson 	/* Clear all levels since we regenerate them each time. */
71873347b07SNate Lawson 	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
71973347b07SNate Lawson 		TAILQ_REMOVE(&sc->all_levels, lev, link);
72073347b07SNate Lawson 		free(lev, M_TEMP);
72173347b07SNate Lawson 	}
722789f03ceSNate Lawson 	sc->all_count = 0;
723789f03ceSNate Lawson 
724789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
72588c9b54cSNate Lawson 	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
72688c9b54cSNate Lawson 		TAILQ_REMOVE(&rel_sets, set_arr, link);
72788c9b54cSNate Lawson 		free(set_arr, M_TEMP);
72888c9b54cSNate Lawson 	}
72973347b07SNate Lawson 	return (error);
73073347b07SNate Lawson }
73173347b07SNate Lawson 
73273347b07SNate Lawson /*
73373347b07SNate Lawson  * Create levels for an array of absolute settings and insert them in
73473347b07SNate Lawson  * sorted order in the specified list.
73573347b07SNate Lawson  */
73673347b07SNate Lawson static int
cpufreq_insert_abs(struct cpufreq_softc * sc,struct cf_setting * sets,int count)73788c9b54cSNate Lawson cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
73873347b07SNate Lawson     int count)
73973347b07SNate Lawson {
74088c9b54cSNate Lawson 	struct cf_level_lst *list;
74173347b07SNate Lawson 	struct cf_level *level, *search;
74232686761SEmmanuel Vadot 	int i, inserted;
74373347b07SNate Lawson 
744789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
745789f03ceSNate Lawson 
74688c9b54cSNate Lawson 	list = &sc->all_levels;
74773347b07SNate Lawson 	for (i = 0; i < count; i++) {
74873347b07SNate Lawson 		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
74973347b07SNate Lawson 		if (level == NULL)
75073347b07SNate Lawson 			return (ENOMEM);
75173347b07SNate Lawson 		level->abs_set = sets[i];
75288c9b54cSNate Lawson 		level->total_set = sets[i];
75388c9b54cSNate Lawson 		level->total_set.dev = NULL;
75488c9b54cSNate Lawson 		sc->all_count++;
75532686761SEmmanuel Vadot 		inserted = 0;
75673347b07SNate Lawson 
75773347b07SNate Lawson 		if (TAILQ_EMPTY(list)) {
7588d913481SNate Lawson 			CF_DEBUG("adding abs setting %d at head\n",
7598d913481SNate Lawson 			    sets[i].freq);
76073347b07SNate Lawson 			TAILQ_INSERT_HEAD(list, level, link);
76173347b07SNate Lawson 			continue;
76273347b07SNate Lawson 		}
76373347b07SNate Lawson 
76432686761SEmmanuel Vadot 		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link)
76588c9b54cSNate Lawson 			if (sets[i].freq <= search->total_set.freq) {
7668d913481SNate Lawson 				CF_DEBUG("adding abs setting %d after %d\n",
7678d913481SNate Lawson 				    sets[i].freq, search->total_set.freq);
76873347b07SNate Lawson 				TAILQ_INSERT_AFTER(list, search, level, link);
76932686761SEmmanuel Vadot 				inserted = 1;
77032686761SEmmanuel Vadot 				break;
77132686761SEmmanuel Vadot 			}
77232686761SEmmanuel Vadot 
77332686761SEmmanuel Vadot 		if (inserted == 0) {
77432686761SEmmanuel Vadot 			TAILQ_FOREACH(search, list, link)
77532686761SEmmanuel Vadot 				if (sets[i].freq >= search->total_set.freq) {
77632686761SEmmanuel Vadot 					CF_DEBUG("adding abs setting %d before %d\n",
77732686761SEmmanuel Vadot 					    sets[i].freq, search->total_set.freq);
77832686761SEmmanuel Vadot 					TAILQ_INSERT_BEFORE(search, level, link);
77973347b07SNate Lawson 					break;
78073347b07SNate Lawson 				}
78173347b07SNate Lawson 		}
78273347b07SNate Lawson 	}
78332686761SEmmanuel Vadot 
78473347b07SNate Lawson 	return (0);
78573347b07SNate Lawson }
78673347b07SNate Lawson 
78788c9b54cSNate Lawson /*
78888c9b54cSNate Lawson  * Expand a group of relative settings, creating derived levels from them.
78988c9b54cSNate Lawson  */
79088c9b54cSNate Lawson static int
cpufreq_expand_set(struct cpufreq_softc * sc,struct cf_setting_array * set_arr)79188c9b54cSNate Lawson cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
79288c9b54cSNate Lawson {
79388c9b54cSNate Lawson 	struct cf_level *fill, *search;
79488c9b54cSNate Lawson 	struct cf_setting *set;
79588c9b54cSNate Lawson 	int i;
79688c9b54cSNate Lawson 
797789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
798789f03ceSNate Lawson 
7995308b2a6SNate Lawson 	/*
8005308b2a6SNate Lawson 	 * Walk the set of all existing levels in reverse.  This is so we
8015308b2a6SNate Lawson 	 * create derived states from the lowest absolute settings first
8025308b2a6SNate Lawson 	 * and discard duplicates created from higher absolute settings.
8035308b2a6SNate Lawson 	 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
8045308b2a6SNate Lawson 	 * preferable to 200 Mhz + 25% because absolute settings are more
8055308b2a6SNate Lawson 	 * efficient since they often change the voltage as well.
8065308b2a6SNate Lawson 	 */
8075308b2a6SNate Lawson 	TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
80888c9b54cSNate Lawson 		/* Add each setting to the level, duplicating if necessary. */
80988c9b54cSNate Lawson 		for (i = 0; i < set_arr->count; i++) {
81088c9b54cSNate Lawson 			set = &set_arr->sets[i];
81188c9b54cSNate Lawson 
81288c9b54cSNate Lawson 			/*
81388c9b54cSNate Lawson 			 * If this setting is less than 100%, split the level
81488c9b54cSNate Lawson 			 * into two and add this setting to the new level.
81588c9b54cSNate Lawson 			 */
81688c9b54cSNate Lawson 			fill = search;
8175308b2a6SNate Lawson 			if (set->freq < 10000) {
81888c9b54cSNate Lawson 				fill = cpufreq_dup_set(sc, search, set);
81988c9b54cSNate Lawson 
82088c9b54cSNate Lawson 				/*
8215308b2a6SNate Lawson 				 * The new level was a duplicate of an existing
8225308b2a6SNate Lawson 				 * level or its absolute setting is too high
8235308b2a6SNate Lawson 				 * so we freed it.  For example, we discard a
8245308b2a6SNate Lawson 				 * derived level of 1000 MHz/25% if a level
8255308b2a6SNate Lawson 				 * of 500 MHz/100% already exists.
82688c9b54cSNate Lawson 				 */
82788c9b54cSNate Lawson 				if (fill == NULL)
8285308b2a6SNate Lawson 					break;
8295308b2a6SNate Lawson 			}
83088c9b54cSNate Lawson 
83188c9b54cSNate Lawson 			/* Add this setting to the existing or new level. */
83288c9b54cSNate Lawson 			KASSERT(fill->rel_count < MAX_SETTINGS,
83388c9b54cSNate Lawson 			    ("cpufreq: too many relative drivers (%d)",
83488c9b54cSNate Lawson 			    MAX_SETTINGS));
83588c9b54cSNate Lawson 			fill->rel_set[fill->rel_count] = *set;
83688c9b54cSNate Lawson 			fill->rel_count++;
8378d913481SNate Lawson 			CF_DEBUG(
8388d913481SNate Lawson 			"expand set added rel setting %d%% to %d level\n",
8398d913481SNate Lawson 			    set->freq / 100, fill->total_set.freq);
84088c9b54cSNate Lawson 		}
84188c9b54cSNate Lawson 	}
84288c9b54cSNate Lawson 
84388c9b54cSNate Lawson 	return (0);
84488c9b54cSNate Lawson }
84588c9b54cSNate Lawson 
84688c9b54cSNate Lawson static struct cf_level *
cpufreq_dup_set(struct cpufreq_softc * sc,struct cf_level * dup,struct cf_setting * set)84788c9b54cSNate Lawson cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
84888c9b54cSNate Lawson     struct cf_setting *set)
84988c9b54cSNate Lawson {
85088c9b54cSNate Lawson 	struct cf_level_lst *list;
85188c9b54cSNate Lawson 	struct cf_level *fill, *itr;
85288c9b54cSNate Lawson 	struct cf_setting *fill_set, *itr_set;
85388c9b54cSNate Lawson 	int i;
85488c9b54cSNate Lawson 
855789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
856789f03ceSNate Lawson 
85788c9b54cSNate Lawson 	/*
85888c9b54cSNate Lawson 	 * Create a new level, copy it from the old one, and update the
85988c9b54cSNate Lawson 	 * total frequency and power by the percentage specified in the
86088c9b54cSNate Lawson 	 * relative setting.
86188c9b54cSNate Lawson 	 */
86288c9b54cSNate Lawson 	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
86388c9b54cSNate Lawson 	if (fill == NULL)
86488c9b54cSNate Lawson 		return (NULL);
86588c9b54cSNate Lawson 	*fill = *dup;
86688c9b54cSNate Lawson 	fill_set = &fill->total_set;
86788c9b54cSNate Lawson 	fill_set->freq =
86888c9b54cSNate Lawson 	    ((uint64_t)fill_set->freq * set->freq) / 10000;
86988c9b54cSNate Lawson 	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
87088c9b54cSNate Lawson 		fill_set->power = ((uint64_t)fill_set->power * set->freq)
87188c9b54cSNate Lawson 		    / 10000;
87288c9b54cSNate Lawson 	}
87388c9b54cSNate Lawson 	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
87488c9b54cSNate Lawson 		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
87588c9b54cSNate Lawson 			fill_set->lat += set->lat;
87688c9b54cSNate Lawson 		else
87788c9b54cSNate Lawson 			fill_set->lat = set->lat;
87888c9b54cSNate Lawson 	}
8798d913481SNate Lawson 	CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
88088c9b54cSNate Lawson 
88188c9b54cSNate Lawson 	/*
88288c9b54cSNate Lawson 	 * If we copied an old level that we already modified (say, at 100%),
88388c9b54cSNate Lawson 	 * we need to remove that setting before adding this one.  Since we
88488c9b54cSNate Lawson 	 * process each setting array in order, we know any settings for this
88588c9b54cSNate Lawson 	 * driver will be found at the end.
88688c9b54cSNate Lawson 	 */
88788c9b54cSNate Lawson 	for (i = fill->rel_count; i != 0; i--) {
88888c9b54cSNate Lawson 		if (fill->rel_set[i - 1].dev != set->dev)
88988c9b54cSNate Lawson 			break;
8908d913481SNate Lawson 		CF_DEBUG("removed last relative driver: %s\n",
8918d913481SNate Lawson 		    device_get_nameunit(set->dev));
89288c9b54cSNate Lawson 		fill->rel_count--;
89388c9b54cSNate Lawson 	}
89488c9b54cSNate Lawson 
89588c9b54cSNate Lawson 	/*
8965308b2a6SNate Lawson 	 * Insert the new level in sorted order.  If it is a duplicate of an
8975308b2a6SNate Lawson 	 * existing level (1) or has an absolute setting higher than the
8985308b2a6SNate Lawson 	 * existing level (2), do not add it.  We can do this since any such
8995308b2a6SNate Lawson 	 * level is guaranteed use less power.  For example (1), a level with
9005308b2a6SNate Lawson 	 * one absolute setting of 800 Mhz uses less power than one composed
9015308b2a6SNate Lawson 	 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
9025308b2a6SNate Lawson 	 * Also for example (2), a level of 800 Mhz/75% is preferable to
9035308b2a6SNate Lawson 	 * 1600 Mhz/25% even though the latter has a lower total frequency.
90488c9b54cSNate Lawson 	 */
90588c9b54cSNate Lawson 	list = &sc->all_levels;
9065308b2a6SNate Lawson 	KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
90788c9b54cSNate Lawson 	TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
90888c9b54cSNate Lawson 		itr_set = &itr->total_set;
9099000b91eSNate Lawson 		if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
9109000b91eSNate Lawson 			CF_DEBUG("dup set rejecting %d (dupe)\n",
9119000b91eSNate Lawson 			    fill_set->freq);
9129000b91eSNate Lawson 			itr = NULL;
9139000b91eSNate Lawson 			break;
9149000b91eSNate Lawson 		} else if (fill_set->freq < itr_set->freq) {
9159000b91eSNate Lawson 			if (fill->abs_set.freq <= itr->abs_set.freq) {
9168d913481SNate Lawson 				CF_DEBUG(
9178d913481SNate Lawson 			"dup done, inserting new level %d after %d\n",
9188d913481SNate Lawson 				    fill_set->freq, itr_set->freq);
91988c9b54cSNate Lawson 				TAILQ_INSERT_AFTER(list, itr, fill, link);
92088c9b54cSNate Lawson 				sc->all_count++;
9219000b91eSNate Lawson 			} else {
9229000b91eSNate Lawson 				CF_DEBUG("dup set rejecting %d (abs too big)\n",
9239000b91eSNate Lawson 				    fill_set->freq);
9249000b91eSNate Lawson 				itr = NULL;
9259000b91eSNate Lawson 			}
92688c9b54cSNate Lawson 			break;
92788c9b54cSNate Lawson 		}
92888c9b54cSNate Lawson 	}
9295308b2a6SNate Lawson 
9305308b2a6SNate Lawson 	/* We didn't find a good place for this new level so free it. */
9315308b2a6SNate Lawson 	if (itr == NULL) {
9325308b2a6SNate Lawson 		CF_DEBUG("dup set freeing new level %d (not optimal)\n",
9335308b2a6SNate Lawson 		    fill_set->freq);
9345308b2a6SNate Lawson 		free(fill, M_TEMP);
9355308b2a6SNate Lawson 		fill = NULL;
93688c9b54cSNate Lawson 	}
93788c9b54cSNate Lawson 
93888c9b54cSNate Lawson 	return (fill);
93988c9b54cSNate Lawson }
94088c9b54cSNate Lawson 
94173347b07SNate Lawson static int
cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)94273347b07SNate Lawson cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
94373347b07SNate Lawson {
94473347b07SNate Lawson 	struct cpufreq_softc *sc;
94573347b07SNate Lawson 	struct cf_level *levels;
9465f3818a5SAlexander Motin 	int best, count, diff, bdiff, devcount, error, freq, i, n;
9470325089dSNate Lawson 	device_t *devs;
94873347b07SNate Lawson 
9490325089dSNate Lawson 	devs = NULL;
95073347b07SNate Lawson 	sc = oidp->oid_arg1;
951dac50931SAndriy Gapon 	levels = sc->levels_buf;
95273347b07SNate Lawson 
95373347b07SNate Lawson 	error = CPUFREQ_GET(sc->dev, &levels[0]);
95473347b07SNate Lawson 	if (error)
95573347b07SNate Lawson 		goto out;
95673347b07SNate Lawson 	freq = levels[0].total_set.freq;
95773347b07SNate Lawson 	error = sysctl_handle_int(oidp, &freq, 0, req);
95873347b07SNate Lawson 	if (error != 0 || req->newptr == NULL)
95973347b07SNate Lawson 		goto out;
96073347b07SNate Lawson 
9610325089dSNate Lawson 	/*
9620325089dSNate Lawson 	 * While we only call cpufreq_get() on one device (assuming all
9630325089dSNate Lawson 	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
9640325089dSNate Lawson 	 * This is needed for some MP systems.
9650325089dSNate Lawson 	 */
9663d7e90fcSJohn Baldwin 	error = devclass_get_devices(devclass_find("cpufreq"), &devs, &devcount);
96773347b07SNate Lawson 	if (error)
96873347b07SNate Lawson 		goto out;
9690325089dSNate Lawson 	for (n = 0; n < devcount; n++) {
9700325089dSNate Lawson 		count = CF_MAX_LEVELS;
9710325089dSNate Lawson 		error = CPUFREQ_LEVELS(devs[n], levels, &count);
972d269386aSNate Lawson 		if (error) {
973d269386aSNate Lawson 			if (error == E2BIG)
974d269386aSNate Lawson 				printf(
975d269386aSNate Lawson 			"cpufreq: need to increase CF_MAX_LEVELS\n");
9760325089dSNate Lawson 			break;
977d269386aSNate Lawson 		}
9785f3818a5SAlexander Motin 		best = 0;
9795f3818a5SAlexander Motin 		bdiff = 1 << 30;
98073347b07SNate Lawson 		for (i = 0; i < count; i++) {
9815f3818a5SAlexander Motin 			diff = abs(levels[i].total_set.freq - freq);
9825f3818a5SAlexander Motin 			if (diff < bdiff) {
9835f3818a5SAlexander Motin 				bdiff = diff;
9845f3818a5SAlexander Motin 				best = i;
98573347b07SNate Lawson 			}
98673347b07SNate Lawson 		}
9875f3818a5SAlexander Motin 		error = CPUFREQ_SET(devs[n], &levels[best], CPUFREQ_PRIO_USER);
9880325089dSNate Lawson 	}
98973347b07SNate Lawson 
99073347b07SNate Lawson out:
9910325089dSNate Lawson 	if (devs)
9920325089dSNate Lawson 		free(devs, M_TEMP);
99373347b07SNate Lawson 	return (error);
99473347b07SNate Lawson }
99573347b07SNate Lawson 
99673347b07SNate Lawson static int
cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)99773347b07SNate Lawson cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
99873347b07SNate Lawson {
99973347b07SNate Lawson 	struct cpufreq_softc *sc;
100073347b07SNate Lawson 	struct cf_level *levels;
100173347b07SNate Lawson 	struct cf_setting *set;
100273347b07SNate Lawson 	struct sbuf sb;
100373347b07SNate Lawson 	int count, error, i;
100473347b07SNate Lawson 
100573347b07SNate Lawson 	sc = oidp->oid_arg1;
100673347b07SNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
100773347b07SNate Lawson 
100873347b07SNate Lawson 	/* Get settings from the device and generate the output string. */
100973347b07SNate Lawson 	count = CF_MAX_LEVELS;
1010dac50931SAndriy Gapon 	levels = sc->levels_buf;
101130215f48SChristian Brueffer 	if (levels == NULL) {
101230215f48SChristian Brueffer 		sbuf_delete(&sb);
101373347b07SNate Lawson 		return (ENOMEM);
101430215f48SChristian Brueffer 	}
101573347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
1016d269386aSNate Lawson 	if (error) {
1017d269386aSNate Lawson 		if (error == E2BIG)
1018d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
101973347b07SNate Lawson 		goto out;
1020d269386aSNate Lawson 	}
102173347b07SNate Lawson 	if (count) {
102273347b07SNate Lawson 		for (i = 0; i < count; i++) {
102373347b07SNate Lawson 			set = &levels[i].total_set;
102473347b07SNate Lawson 			sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
102573347b07SNate Lawson 		}
102673347b07SNate Lawson 	} else
102773347b07SNate Lawson 		sbuf_cpy(&sb, "0");
102873347b07SNate Lawson 	sbuf_trim(&sb);
102973347b07SNate Lawson 	sbuf_finish(&sb);
103073347b07SNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
103173347b07SNate Lawson 
103273347b07SNate Lawson out:
103373347b07SNate Lawson 	sbuf_delete(&sb);
103473347b07SNate Lawson 	return (error);
103573347b07SNate Lawson }
103673347b07SNate Lawson 
1037e959a70bSNate Lawson static int
cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)1038e959a70bSNate Lawson cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
1039e959a70bSNate Lawson {
1040e959a70bSNate Lawson 	device_t dev;
1041e959a70bSNate Lawson 	struct cf_setting *sets;
1042e959a70bSNate Lawson 	struct sbuf sb;
1043e959a70bSNate Lawson 	int error, i, set_count;
1044e959a70bSNate Lawson 
1045e959a70bSNate Lawson 	dev = oidp->oid_arg1;
1046e959a70bSNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
1047e959a70bSNate Lawson 
1048e959a70bSNate Lawson 	/* Get settings from the device and generate the output string. */
1049e959a70bSNate Lawson 	set_count = MAX_SETTINGS;
1050ac2fffa4SPedro F. Giffuni 	sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
105130215f48SChristian Brueffer 	if (sets == NULL) {
105230215f48SChristian Brueffer 		sbuf_delete(&sb);
1053e959a70bSNate Lawson 		return (ENOMEM);
105430215f48SChristian Brueffer 	}
1055e959a70bSNate Lawson 	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
1056e959a70bSNate Lawson 	if (error)
1057e959a70bSNate Lawson 		goto out;
1058e959a70bSNate Lawson 	if (set_count) {
1059e959a70bSNate Lawson 		for (i = 0; i < set_count; i++)
1060e959a70bSNate Lawson 			sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
1061e959a70bSNate Lawson 	} else
1062e959a70bSNate Lawson 		sbuf_cpy(&sb, "0");
1063e959a70bSNate Lawson 	sbuf_trim(&sb);
1064e959a70bSNate Lawson 	sbuf_finish(&sb);
1065e959a70bSNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1066e959a70bSNate Lawson 
1067e959a70bSNate Lawson out:
1068e959a70bSNate Lawson 	free(sets, M_TEMP);
1069e959a70bSNate Lawson 	sbuf_delete(&sb);
1070e959a70bSNate Lawson 	return (error);
1071e959a70bSNate Lawson }
1072e959a70bSNate Lawson 
10734577cf37SConrad Meyer static void
cpufreq_add_freq_driver_sysctl(device_t cf_dev)10744577cf37SConrad Meyer cpufreq_add_freq_driver_sysctl(device_t cf_dev)
10754577cf37SConrad Meyer {
10764577cf37SConrad Meyer 	struct cpufreq_softc *sc;
10774577cf37SConrad Meyer 
10784577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
10794577cf37SConrad Meyer 	SYSCTL_ADD_CONST_STRING(&sc->sysctl_ctx,
10804577cf37SConrad Meyer 	    SYSCTL_CHILDREN(device_get_sysctl_tree(cf_dev)), OID_AUTO,
10814577cf37SConrad Meyer 	    "freq_driver", CTLFLAG_RD, device_get_nameunit(sc->cf_drv_dev),
10824577cf37SConrad Meyer 	    "cpufreq driver used by this cpu");
10834577cf37SConrad Meyer }
10844577cf37SConrad Meyer 
108573347b07SNate Lawson int
cpufreq_register(device_t dev)108673347b07SNate Lawson cpufreq_register(device_t dev)
108773347b07SNate Lawson {
108867c8649fSNate Lawson 	struct cpufreq_softc *sc;
108973347b07SNate Lawson 	device_t cf_dev, cpu_dev;
10904577cf37SConrad Meyer 	int error;
109173347b07SNate Lawson 
1092e959a70bSNate Lawson 	/* Add a sysctl to get each driver's settings separately. */
1093e959a70bSNate Lawson 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1094e959a70bSNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
10957029da5cSPawel Biernacki 	    OID_AUTO, "freq_settings",
10967029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, dev, 0,
1097e959a70bSNate Lawson 	    cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
1098e959a70bSNate Lawson 
109973347b07SNate Lawson 	/*
11000325089dSNate Lawson 	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
11010325089dSNate Lawson 	 * must offer the same levels and be switched at the same time.
110273347b07SNate Lawson 	 */
11030325089dSNate Lawson 	cpu_dev = device_get_parent(dev);
110467c8649fSNate Lawson 	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
110567c8649fSNate Lawson 		sc = device_get_softc(cf_dev);
110667c8649fSNate Lawson 		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
11074577cf37SConrad Meyer 		MPASS(sc->cf_drv_dev != NULL);
110873347b07SNate Lawson 		return (0);
110967c8649fSNate Lawson 	}
111073347b07SNate Lawson 
11110325089dSNate Lawson 	/* Add the child device and possibly sysctls. */
1112d3a8f98aSAlexander Motin 	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", device_get_unit(cpu_dev));
111373347b07SNate Lawson 	if (cf_dev == NULL)
111473347b07SNate Lawson 		return (ENOMEM);
111573347b07SNate Lawson 	device_quiet(cf_dev);
111673347b07SNate Lawson 
11174577cf37SConrad Meyer 	error = device_probe_and_attach(cf_dev);
11184577cf37SConrad Meyer 	if (error)
11194577cf37SConrad Meyer 		return (error);
11204577cf37SConrad Meyer 
11214577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
11224577cf37SConrad Meyer 	sc->cf_drv_dev = dev;
11234577cf37SConrad Meyer 	cpufreq_add_freq_driver_sysctl(cf_dev);
11244577cf37SConrad Meyer 	return (error);
112573347b07SNate Lawson }
112673347b07SNate Lawson 
112773347b07SNate Lawson int
cpufreq_unregister(device_t dev)112873347b07SNate Lawson cpufreq_unregister(device_t dev)
112973347b07SNate Lawson {
11304577cf37SConrad Meyer 	device_t cf_dev;
113195d35d7aSScott Long 	struct cpufreq_softc *sc __diagused;
113273347b07SNate Lawson 
113373347b07SNate Lawson 	/*
113473347b07SNate Lawson 	 * If this is the last cpufreq child device, remove the control
113573347b07SNate Lawson 	 * device as well.  We identify cpufreq children by calling a method
113673347b07SNate Lawson 	 * they support.
113773347b07SNate Lawson 	 */
113867c8649fSNate Lawson 	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
113971ab130cSNate Lawson 	if (cf_dev == NULL) {
114071ab130cSNate Lawson 		device_printf(dev,
114171ab130cSNate Lawson 	"warning: cpufreq_unregister called with no cpufreq device active\n");
114271ab130cSNate Lawson 		return (0);
114371ab130cSNate Lawson 	}
11444577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
11454577cf37SConrad Meyer 	MPASS(sc->cf_drv_dev == dev);
114673347b07SNate Lawson 	device_delete_child(device_get_parent(cf_dev), cf_dev);
114773347b07SNate Lawson 
114873347b07SNate Lawson 	return (0);
114973347b07SNate Lawson }
11500d4ac62aSNate Lawson 
11510d4ac62aSNate Lawson int
cpufreq_settings_changed(device_t dev)11520d4ac62aSNate Lawson cpufreq_settings_changed(device_t dev)
11530d4ac62aSNate Lawson {
11540d4ac62aSNate Lawson 
11550d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_levels_changed,
11560d4ac62aSNate Lawson 	    device_get_unit(device_get_parent(dev)));
11570d4ac62aSNate Lawson 	return (0);
11580d4ac62aSNate Lawson }
1159