xref: /freebsd/sys/kern/kern_cpu.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
173347b07SNate Lawson /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
3073347b07SNate Lawson __FBSDID("$FreeBSD$");
3173347b07SNate Lawson 
3273347b07SNate Lawson #include <sys/param.h>
3373347b07SNate Lawson #include <sys/bus.h>
3473347b07SNate Lawson #include <sys/cpu.h>
3573347b07SNate Lawson #include <sys/eventhandler.h>
3673347b07SNate Lawson #include <sys/kernel.h>
37789f03ceSNate Lawson #include <sys/lock.h>
3873347b07SNate Lawson #include <sys/malloc.h>
3973347b07SNate Lawson #include <sys/module.h>
4073347b07SNate Lawson #include <sys/proc.h>
4173347b07SNate Lawson #include <sys/queue.h>
42a15e947dSNate Lawson #include <sys/sbuf.h>
4373347b07SNate Lawson #include <sys/sched.h>
44a15e947dSNate Lawson #include <sys/smp.h>
4573347b07SNate Lawson #include <sys/sysctl.h>
4673347b07SNate Lawson #include <sys/systm.h>
47789f03ceSNate Lawson #include <sys/sx.h>
480325089dSNate Lawson #include <sys/timetc.h>
490d4ac62aSNate Lawson #include <sys/taskqueue.h>
5073347b07SNate Lawson 
5173347b07SNate Lawson #include "cpufreq_if.h"
5273347b07SNate Lawson 
5373347b07SNate Lawson /*
5473347b07SNate Lawson  * Common CPU frequency glue code.  Drivers for specific hardware can
5573347b07SNate Lawson  * attach this interface to allow users to get/set the CPU frequency.
5673347b07SNate Lawson  */
5773347b07SNate Lawson 
5873347b07SNate Lawson /*
5973347b07SNate Lawson  * Number of levels we can handle.  Levels are synthesized from settings
60d269386aSNate Lawson  * so for M settings and N drivers, there may be M*N levels.
6173347b07SNate Lawson  */
6222c1b4c0SJustin Hibbits #define CF_MAX_LEVELS	256
6373347b07SNate Lawson 
6456e5a87aSHajimu UMEMOTO struct cf_saved_freq {
6556e5a87aSHajimu UMEMOTO 	struct cf_level			level;
6656e5a87aSHajimu UMEMOTO 	int				priority;
6756e5a87aSHajimu UMEMOTO 	SLIST_ENTRY(cf_saved_freq)	link;
6856e5a87aSHajimu UMEMOTO };
6956e5a87aSHajimu UMEMOTO 
7073347b07SNate Lawson struct cpufreq_softc {
71789f03ceSNate Lawson 	struct sx			lock;
7273347b07SNate Lawson 	struct cf_level			curr_level;
735f0afa04SNate Lawson 	int				curr_priority;
7456e5a87aSHajimu UMEMOTO 	SLIST_HEAD(, cf_saved_freq)	saved_freq;
7573347b07SNate Lawson 	struct cf_level_lst		all_levels;
765f0afa04SNate Lawson 	int				all_count;
7767c8649fSNate Lawson 	int				max_mhz;
7873347b07SNate Lawson 	device_t			dev;
794577cf37SConrad Meyer 	device_t			cf_drv_dev;
8073347b07SNate Lawson 	struct sysctl_ctx_list		sysctl_ctx;
810d4ac62aSNate Lawson 	struct task			startup_task;
82dac50931SAndriy Gapon 	struct cf_level			*levels_buf;
8373347b07SNate Lawson };
8473347b07SNate Lawson 
8573347b07SNate Lawson struct cf_setting_array {
8673347b07SNate Lawson 	struct cf_setting		sets[MAX_SETTINGS];
8773347b07SNate Lawson 	int				count;
8873347b07SNate Lawson 	TAILQ_ENTRY(cf_setting_array)	link;
8973347b07SNate Lawson };
9073347b07SNate Lawson 
9173347b07SNate Lawson TAILQ_HEAD(cf_setting_lst, cf_setting_array);
9273347b07SNate Lawson 
93789f03ceSNate Lawson #define CF_MTX_INIT(x)		sx_init((x), "cpufreq lock")
94789f03ceSNate Lawson #define CF_MTX_LOCK(x)		sx_xlock((x))
95789f03ceSNate Lawson #define CF_MTX_UNLOCK(x)	sx_xunlock((x))
96789f03ceSNate Lawson #define CF_MTX_ASSERT(x)	sx_assert((x), SX_XLOCKED)
97789f03ceSNate Lawson 
988d913481SNate Lawson #define CF_DEBUG(msg...)	do {		\
998d913481SNate Lawson 	if (cf_verbose)				\
1008d913481SNate Lawson 		printf("cpufreq: " msg);	\
1018d913481SNate Lawson 	} while (0)
1028d913481SNate Lawson 
10373347b07SNate Lawson static int	cpufreq_attach(device_t dev);
1040d4ac62aSNate Lawson static void	cpufreq_startup_task(void *ctx, int pending);
10573347b07SNate Lawson static int	cpufreq_detach(device_t dev);
10673347b07SNate Lawson static int	cf_set_method(device_t dev, const struct cf_level *level,
10773347b07SNate Lawson 		    int priority);
10873347b07SNate Lawson static int	cf_get_method(device_t dev, struct cf_level *level);
10973347b07SNate Lawson static int	cf_levels_method(device_t dev, struct cf_level *levels,
11073347b07SNate Lawson 		    int *count);
11188c9b54cSNate Lawson static int	cpufreq_insert_abs(struct cpufreq_softc *sc,
11273347b07SNate Lawson 		    struct cf_setting *sets, int count);
11388c9b54cSNate Lawson static int	cpufreq_expand_set(struct cpufreq_softc *sc,
11488c9b54cSNate Lawson 		    struct cf_setting_array *set_arr);
11588c9b54cSNate Lawson static struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
11688c9b54cSNate Lawson 		    struct cf_level *dup, struct cf_setting *set);
11773347b07SNate Lawson static int	cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
11873347b07SNate Lawson static int	cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
119e959a70bSNate Lawson static int	cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
12073347b07SNate Lawson 
12173347b07SNate Lawson static device_method_t cpufreq_methods[] = {
12273347b07SNate Lawson 	DEVMETHOD(device_probe,		bus_generic_probe),
12373347b07SNate Lawson 	DEVMETHOD(device_attach,	cpufreq_attach),
12473347b07SNate Lawson 	DEVMETHOD(device_detach,	cpufreq_detach),
12573347b07SNate Lawson 
12673347b07SNate Lawson         DEVMETHOD(cpufreq_set,		cf_set_method),
12773347b07SNate Lawson         DEVMETHOD(cpufreq_get,		cf_get_method),
12873347b07SNate Lawson         DEVMETHOD(cpufreq_levels,	cf_levels_method),
12973347b07SNate Lawson 	{0, 0}
13073347b07SNate Lawson };
13173347b07SNate Lawson static driver_t cpufreq_driver = {
13273347b07SNate Lawson 	"cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
13373347b07SNate Lawson };
13473347b07SNate Lawson static devclass_t cpufreq_dc;
13573347b07SNate Lawson DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
13673347b07SNate Lawson 
137b070969bSNate Lawson static int		cf_lowest_freq;
1388d913481SNate Lawson static int		cf_verbose;
139*7029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1406472ac3dSEd Schouten     "cpufreq debugging");
141af3b2549SHans Petter Selasky SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RWTUN, &cf_lowest_freq, 1,
142b070969bSNate Lawson     "Don't provide levels below this frequency.");
143af3b2549SHans Petter Selasky SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RWTUN, &cf_verbose, 1,
1448d913481SNate Lawson     "Print verbose debugging messages");
145b070969bSNate Lawson 
1464577cf37SConrad Meyer /*
1474577cf37SConrad Meyer  * This is called as the result of a hardware specific frequency control driver
1484577cf37SConrad Meyer  * calling cpufreq_register. It provides a general interface for system wide
1494577cf37SConrad Meyer  * frequency controls and operates on a per cpu basis.
1504577cf37SConrad Meyer  */
15173347b07SNate Lawson static int
15273347b07SNate Lawson cpufreq_attach(device_t dev)
15373347b07SNate Lawson {
15473347b07SNate Lawson 	struct cpufreq_softc *sc;
155d288bcc4SAlexander Motin 	struct pcpu *pc;
15673347b07SNate Lawson 	device_t parent;
157d288bcc4SAlexander Motin 	uint64_t rate;
15873347b07SNate Lawson 
1598d913481SNate Lawson 	CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
16073347b07SNate Lawson 	sc = device_get_softc(dev);
16173347b07SNate Lawson 	parent = device_get_parent(dev);
16273347b07SNate Lawson 	sc->dev = dev;
16373347b07SNate Lawson 	sysctl_ctx_init(&sc->sysctl_ctx);
16473347b07SNate Lawson 	TAILQ_INIT(&sc->all_levels);
165789f03ceSNate Lawson 	CF_MTX_INIT(&sc->lock);
16673347b07SNate Lawson 	sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
16756e5a87aSHajimu UMEMOTO 	SLIST_INIT(&sc->saved_freq);
168f436f175SNathan Whitehorn 	/* Try to get nominal CPU freq to use it as maximum later if needed */
169f436f175SNathan Whitehorn 	sc->max_mhz = cpu_get_nominal_mhz(dev);
170f436f175SNathan Whitehorn 	/* If that fails, try to measure the current rate */
171f436f175SNathan Whitehorn 	if (sc->max_mhz <= 0) {
1724577cf37SConrad Meyer 		CF_DEBUG("Unable to obtain nominal frequency.\n");
173d288bcc4SAlexander Motin 		pc = cpu_get_pcpu(dev);
174d288bcc4SAlexander Motin 		if (cpu_est_clockrate(pc->pc_cpuid, &rate) == 0)
175d288bcc4SAlexander Motin 			sc->max_mhz = rate / 1000000;
176d288bcc4SAlexander Motin 		else
17767c8649fSNate Lawson 			sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
178f436f175SNathan Whitehorn 	}
17973347b07SNate Lawson 
1808d913481SNate Lawson 	CF_DEBUG("initializing one-time data for %s\n",
1818d913481SNate Lawson 	    device_get_nameunit(dev));
182dac50931SAndriy Gapon 	sc->levels_buf = malloc(CF_MAX_LEVELS * sizeof(*sc->levels_buf),
183dac50931SAndriy Gapon 	    M_DEVBUF, M_WAITOK);
18473347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
18573347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
186*7029da5cSPawel Biernacki 	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
187*7029da5cSPawel Biernacki 	    sc, 0, cpufreq_curr_sysctl, "I", "Current CPU frequency");
18873347b07SNate Lawson 	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
18973347b07SNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
190*7029da5cSPawel Biernacki 	    OID_AUTO, "freq_levels",
191*7029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
19273347b07SNate Lawson 	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
1930d4ac62aSNate Lawson 
1940d4ac62aSNate Lawson 	/*
1950d4ac62aSNate Lawson 	 * Queue a one-shot broadcast that levels have changed.
1960d4ac62aSNate Lawson 	 * It will run once the system has completed booting.
1970d4ac62aSNate Lawson 	 */
1980d4ac62aSNate Lawson 	TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
1990d4ac62aSNate Lawson 	taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
20073347b07SNate Lawson 
20173347b07SNate Lawson 	return (0);
20273347b07SNate Lawson }
20373347b07SNate Lawson 
2040d4ac62aSNate Lawson /* Handle any work to be done for all drivers that attached during boot. */
2050d4ac62aSNate Lawson static void
2060d4ac62aSNate Lawson cpufreq_startup_task(void *ctx, int pending)
2070d4ac62aSNate Lawson {
2080d4ac62aSNate Lawson 
2090d4ac62aSNate Lawson 	cpufreq_settings_changed((device_t)ctx);
2100d4ac62aSNate Lawson }
2110d4ac62aSNate Lawson 
21273347b07SNate Lawson static int
21373347b07SNate Lawson cpufreq_detach(device_t dev)
21473347b07SNate Lawson {
21573347b07SNate Lawson 	struct cpufreq_softc *sc;
21656e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq;
21773347b07SNate Lawson 
2188d913481SNate Lawson 	CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
21973347b07SNate Lawson 	sc = device_get_softc(dev);
22073347b07SNate Lawson 	sysctl_ctx_free(&sc->sysctl_ctx);
22173347b07SNate Lawson 
22256e5a87aSHajimu UMEMOTO 	while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
22356e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
22456e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
22556e5a87aSHajimu UMEMOTO 	}
22656e5a87aSHajimu UMEMOTO 
227dac50931SAndriy Gapon 	free(sc->levels_buf, M_DEVBUF);
22873347b07SNate Lawson 
22973347b07SNate Lawson 	return (0);
23073347b07SNate Lawson }
23173347b07SNate Lawson 
23273347b07SNate Lawson static int
23373347b07SNate Lawson cf_set_method(device_t dev, const struct cf_level *level, int priority)
23473347b07SNate Lawson {
23573347b07SNate Lawson 	struct cpufreq_softc *sc;
23673347b07SNate Lawson 	const struct cf_setting *set;
23756e5a87aSHajimu UMEMOTO 	struct cf_saved_freq *saved_freq, *curr_freq;
2380325089dSNate Lawson 	struct pcpu *pc;
23962db376aSNate Lawson 	int error, i;
240e7bd0750SAndrew Gallatin 	u_char pri;
24173347b07SNate Lawson 
24273347b07SNate Lawson 	sc = device_get_softc(dev);
243789f03ceSNate Lawson 	error = 0;
244789f03ceSNate Lawson 	set = NULL;
24556e5a87aSHajimu UMEMOTO 	saved_freq = NULL;
24673347b07SNate Lawson 
2470d4ac62aSNate Lawson 	/* We are going to change levels so notify the pre-change handler. */
2480d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
2490d4ac62aSNate Lawson 	if (error != 0) {
2500d4ac62aSNate Lawson 		EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
2510d4ac62aSNate Lawson 		return (error);
252b4130b8aSMarcus Alves Grando 	}
2530325089dSNate Lawson 
254789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
25556e5a87aSHajimu UMEMOTO 
256a15e947dSNate Lawson #ifdef SMP
257fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
258fdce57a0SJohn Baldwin 	MPASS(mp_ncpus == 1 || smp_started);
259fdce57a0SJohn Baldwin #else
260a15e947dSNate Lawson 	/*
261a15e947dSNate Lawson 	 * If still booting and secondary CPUs not started yet, don't allow
262a15e947dSNate Lawson 	 * changing the frequency until they're online.  This is because we
263a15e947dSNate Lawson 	 * can't switch to them using sched_bind() and thus we'd only be
264a15e947dSNate Lawson 	 * switching the main CPU.  XXXTODO: Need to think more about how to
265a15e947dSNate Lawson 	 * handle having different CPUs at different frequencies.
266a15e947dSNate Lawson 	 */
26760ad8150SScott Long 	if (mp_ncpus > 1 && !smp_started) {
268a15e947dSNate Lawson 		device_printf(dev, "rejecting change, SMP not started yet\n");
269a15e947dSNate Lawson 		error = ENXIO;
270a15e947dSNate Lawson 		goto out;
271a15e947dSNate Lawson 	}
272fdce57a0SJohn Baldwin #endif
273a15e947dSNate Lawson #endif /* SMP */
274a15e947dSNate Lawson 
27556e5a87aSHajimu UMEMOTO 	/*
27656e5a87aSHajimu UMEMOTO 	 * If the requested level has a lower priority, don't allow
27756e5a87aSHajimu UMEMOTO 	 * the new level right now.
27856e5a87aSHajimu UMEMOTO 	 */
27956e5a87aSHajimu UMEMOTO 	if (priority < sc->curr_priority) {
2808d913481SNate Lawson 		CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
2818d913481SNate Lawson 		    sc->curr_priority);
282789f03ceSNate Lawson 		error = EPERM;
283789f03ceSNate Lawson 		goto out;
284789f03ceSNate Lawson 	}
2855f0afa04SNate Lawson 
28656e5a87aSHajimu UMEMOTO 	/*
28756e5a87aSHajimu UMEMOTO 	 * If the caller didn't specify a level and one is saved, prepare to
28856e5a87aSHajimu UMEMOTO 	 * restore the saved level.  If none has been saved, return an error.
28956e5a87aSHajimu UMEMOTO 	 */
29056e5a87aSHajimu UMEMOTO 	if (level == NULL) {
29156e5a87aSHajimu UMEMOTO 		saved_freq = SLIST_FIRST(&sc->saved_freq);
29256e5a87aSHajimu UMEMOTO 		if (saved_freq == NULL) {
29356e5a87aSHajimu UMEMOTO 			CF_DEBUG("NULL level, no saved level\n");
29456e5a87aSHajimu UMEMOTO 			error = ENXIO;
29556e5a87aSHajimu UMEMOTO 			goto out;
29656e5a87aSHajimu UMEMOTO 		}
29756e5a87aSHajimu UMEMOTO 		level = &saved_freq->level;
29856e5a87aSHajimu UMEMOTO 		priority = saved_freq->priority;
29956e5a87aSHajimu UMEMOTO 		CF_DEBUG("restoring saved level, freq %d prio %d\n",
30056e5a87aSHajimu UMEMOTO 		    level->total_set.freq, priority);
30156e5a87aSHajimu UMEMOTO 	}
30256e5a87aSHajimu UMEMOTO 
303b070969bSNate Lawson 	/* Reject levels that are below our specified threshold. */
304da8a77c1SNate Lawson 	if (level->total_set.freq < cf_lowest_freq) {
3058d913481SNate Lawson 		CF_DEBUG("rejecting freq %d, less than %d limit\n",
3068d913481SNate Lawson 		    level->total_set.freq, cf_lowest_freq);
307789f03ceSNate Lawson 		error = EINVAL;
308789f03ceSNate Lawson 		goto out;
309789f03ceSNate Lawson 	}
310b070969bSNate Lawson 
31173347b07SNate Lawson 	/* If already at this level, just return. */
3125f3818a5SAlexander Motin 	if (sc->curr_level.total_set.freq == level->total_set.freq) {
3138d913481SNate Lawson 		CF_DEBUG("skipping freq %d, same as current level %d\n",
3148d913481SNate Lawson 		    level->total_set.freq, sc->curr_level.total_set.freq);
3151fea6ce7SHajimu UMEMOTO 		goto skip;
3168d913481SNate Lawson 	}
31773347b07SNate Lawson 
31873347b07SNate Lawson 	/* First, set the absolute frequency via its driver. */
31973347b07SNate Lawson 	set = &level->abs_set;
32073347b07SNate Lawson 	if (set->dev) {
32173347b07SNate Lawson 		if (!device_is_attached(set->dev)) {
32273347b07SNate Lawson 			error = ENXIO;
32373347b07SNate Lawson 			goto out;
32473347b07SNate Lawson 		}
3251196826aSNate Lawson 
32662db376aSNate Lawson 		/* Bind to the target CPU before switching. */
3271196826aSNate Lawson 		pc = cpu_get_pcpu(set->dev);
328982d11f8SJeff Roberson 		thread_lock(curthread);
329e7bd0750SAndrew Gallatin 		pri = curthread->td_priority;
330e7bd0750SAndrew Gallatin 		sched_prio(curthread, PRI_MIN);
3311196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
332982d11f8SJeff Roberson 		thread_unlock(curthread);
3338d913481SNate Lawson 		CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
3348d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
33573347b07SNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
336982d11f8SJeff Roberson 		thread_lock(curthread);
3371196826aSNate Lawson 		sched_unbind(curthread);
338e7bd0750SAndrew Gallatin 		sched_prio(curthread, pri);
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);
356e7bd0750SAndrew Gallatin 		pri = curthread->td_priority;
357e7bd0750SAndrew Gallatin 		sched_prio(curthread, PRI_MIN);
3581196826aSNate Lawson 		sched_bind(curthread, pc->pc_cpuid);
359982d11f8SJeff Roberson 		thread_unlock(curthread);
3608d913481SNate Lawson 		CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
3618d913481SNate Lawson 		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
36288c9b54cSNate Lawson 		error = CPUFREQ_DRV_SET(set->dev, set);
363982d11f8SJeff Roberson 		thread_lock(curthread);
3641196826aSNate Lawson 		sched_unbind(curthread);
365e7bd0750SAndrew Gallatin 		sched_prio(curthread, pri);
366982d11f8SJeff Roberson 		thread_unlock(curthread);
36788c9b54cSNate Lawson 		if (error) {
36888c9b54cSNate Lawson 			/* XXX Back out any successful setting? */
36988c9b54cSNate Lawson 			goto out;
37088c9b54cSNate Lawson 		}
37188c9b54cSNate Lawson 	}
37273347b07SNate Lawson 
3731fea6ce7SHajimu UMEMOTO skip:
3745f0afa04SNate Lawson 	/*
3755f0afa04SNate Lawson 	 * Before recording the current level, check if we're going to a
37656e5a87aSHajimu UMEMOTO 	 * higher priority.  If so, save the previous level and priority.
3775f0afa04SNate Lawson 	 */
3785f0afa04SNate Lawson 	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
37956e5a87aSHajimu UMEMOTO 	    priority > sc->curr_priority) {
3808d913481SNate Lawson 		CF_DEBUG("saving level, freq %d prio %d\n",
3818d913481SNate Lawson 		    sc->curr_level.total_set.freq, sc->curr_priority);
38256e5a87aSHajimu UMEMOTO 		curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
38356e5a87aSHajimu UMEMOTO 		if (curr_freq == NULL) {
38456e5a87aSHajimu UMEMOTO 			error = ENOMEM;
38556e5a87aSHajimu UMEMOTO 			goto out;
38656e5a87aSHajimu UMEMOTO 		}
38756e5a87aSHajimu UMEMOTO 		curr_freq->level = sc->curr_level;
38856e5a87aSHajimu UMEMOTO 		curr_freq->priority = sc->curr_priority;
38956e5a87aSHajimu UMEMOTO 		SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
3905f0afa04SNate Lawson 	}
39173347b07SNate Lawson 	sc->curr_level = *level;
3925f0afa04SNate Lawson 	sc->curr_priority = priority;
39373347b07SNate Lawson 
39456e5a87aSHajimu UMEMOTO 	/* If we were restoring a saved state, reset it to "unused". */
39556e5a87aSHajimu UMEMOTO 	if (saved_freq != NULL) {
39656e5a87aSHajimu UMEMOTO 		CF_DEBUG("resetting saved level\n");
39756e5a87aSHajimu UMEMOTO 		sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
39856e5a87aSHajimu UMEMOTO 		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
39956e5a87aSHajimu UMEMOTO 		free(saved_freq, M_TEMP);
40056e5a87aSHajimu UMEMOTO 	}
40156e5a87aSHajimu UMEMOTO 
40273347b07SNate Lawson out:
403789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
4040d4ac62aSNate Lawson 
4050d4ac62aSNate Lawson 	/*
4060d4ac62aSNate Lawson 	 * We changed levels (or attempted to) so notify the post-change
4070d4ac62aSNate Lawson 	 * handler of new frequency or error.
4080d4ac62aSNate Lawson 	 */
4090d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
410789f03ceSNate Lawson 	if (error && set)
41173347b07SNate Lawson 		device_printf(set->dev, "set freq failed, err %d\n", error);
4120d4ac62aSNate Lawson 
41373347b07SNate Lawson 	return (error);
41473347b07SNate Lawson }
41573347b07SNate Lawson 
41673347b07SNate Lawson static int
4174577cf37SConrad Meyer cpufreq_get_frequency(device_t dev)
4184577cf37SConrad Meyer {
4194577cf37SConrad Meyer 	struct cf_setting set;
4204577cf37SConrad Meyer 
4214577cf37SConrad Meyer 	if (CPUFREQ_DRV_GET(dev, &set) != 0)
4224577cf37SConrad Meyer 		return (-1);
4234577cf37SConrad Meyer 
4244577cf37SConrad Meyer 	return (set.freq);
4254577cf37SConrad Meyer }
4264577cf37SConrad Meyer 
4274577cf37SConrad Meyer /* Returns the index into *levels with the match */
4284577cf37SConrad Meyer static int
4294577cf37SConrad Meyer cpufreq_get_level(device_t dev, struct cf_level *levels, int count)
4304577cf37SConrad Meyer {
4314577cf37SConrad Meyer 	int i, freq;
4324577cf37SConrad Meyer 
4334577cf37SConrad Meyer 	if ((freq = cpufreq_get_frequency(dev)) < 0)
4344577cf37SConrad Meyer 		return (-1);
4354577cf37SConrad Meyer 	for (i = 0; i < count; i++)
4364577cf37SConrad Meyer 		if (freq == levels[i].total_set.freq)
4374577cf37SConrad Meyer 			return (i);
4384577cf37SConrad Meyer 
4394577cf37SConrad Meyer 	return (-1);
4404577cf37SConrad Meyer }
4414577cf37SConrad Meyer 
4424577cf37SConrad Meyer /*
4434577cf37SConrad Meyer  * Used by the cpufreq core, this function will populate *level with the current
4444577cf37SConrad Meyer  * frequency as either determined by a cached value sc->curr_level, or in the
4454577cf37SConrad Meyer  * case the lower level driver has set the CPUFREQ_FLAG_UNCACHED flag, it will
4464577cf37SConrad Meyer  * obtain the frequency from the driver itself.
4474577cf37SConrad Meyer  */
4484577cf37SConrad Meyer static int
44973347b07SNate Lawson cf_get_method(device_t dev, struct cf_level *level)
45073347b07SNate Lawson {
45173347b07SNate Lawson 	struct cpufreq_softc *sc;
45273347b07SNate Lawson 	struct cf_level *levels;
4534577cf37SConrad Meyer 	struct cf_setting *curr_set;
45473347b07SNate Lawson 	struct pcpu *pc;
4554577cf37SConrad Meyer 	int bdiff, count, diff, error, i, type;
45673347b07SNate Lawson 	uint64_t rate;
45773347b07SNate Lawson 
45873347b07SNate Lawson 	sc = device_get_softc(dev);
459789f03ceSNate Lawson 	error = 0;
46073347b07SNate Lawson 	levels = NULL;
46173347b07SNate Lawson 
4624577cf37SConrad Meyer 	/*
4634577cf37SConrad Meyer 	 * If we already know the current frequency, and the driver didn't ask
4644577cf37SConrad Meyer 	 * for uncached usage, we're done.
4654577cf37SConrad Meyer 	 */
466789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
467789f03ceSNate Lawson 	curr_set = &sc->curr_level.total_set;
4684577cf37SConrad Meyer 	error = CPUFREQ_DRV_TYPE(sc->cf_drv_dev, &type);
4694577cf37SConrad Meyer 	if (error == 0 && (type & CPUFREQ_FLAG_UNCACHED)) {
4704577cf37SConrad Meyer 		struct cf_setting set;
4714577cf37SConrad Meyer 
4724577cf37SConrad Meyer 		/*
4734577cf37SConrad Meyer 		 * If the driver wants to always report back the real frequency,
4744577cf37SConrad Meyer 		 * first try the driver and if that fails, fall back to
4754577cf37SConrad Meyer 		 * estimating.
4764577cf37SConrad Meyer 		 */
47707a65f9dSConrad Meyer 		if (CPUFREQ_DRV_GET(sc->cf_drv_dev, &set) == 0) {
4784577cf37SConrad Meyer 			sc->curr_level.total_set = set;
47907a65f9dSConrad Meyer 			CF_DEBUG("get returning immediate freq %d\n",
48007a65f9dSConrad Meyer 			    curr_set->freq);
4814577cf37SConrad Meyer 			goto out;
48207a65f9dSConrad Meyer 		}
4834577cf37SConrad Meyer 	} else if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
4848d913481SNate Lawson 		CF_DEBUG("get returning known freq %d\n", curr_set->freq);
4854577cf37SConrad Meyer 		error = 0;
48673347b07SNate Lawson 		goto out;
4878d913481SNate Lawson 	}
488789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
48973347b07SNate Lawson 
49073347b07SNate Lawson 	/*
49173347b07SNate Lawson 	 * We need to figure out the current level.  Loop through every
49273347b07SNate Lawson 	 * driver, getting the current setting.  Then, attempt to get a best
49373347b07SNate Lawson 	 * match of settings against each level.
49473347b07SNate Lawson 	 */
49573347b07SNate Lawson 	count = CF_MAX_LEVELS;
496ac2fffa4SPedro F. Giffuni 	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
49773347b07SNate Lawson 	if (levels == NULL)
49873347b07SNate Lawson 		return (ENOMEM);
49973347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
500d269386aSNate Lawson 	if (error) {
501d269386aSNate Lawson 		if (error == E2BIG)
502d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
503789f03ceSNate Lawson 		free(levels, M_TEMP);
504789f03ceSNate Lawson 		return (error);
505d269386aSNate Lawson 	}
506789f03ceSNate Lawson 
507789f03ceSNate Lawson 	/*
508789f03ceSNate Lawson 	 * Reacquire the lock and search for the given level.
509789f03ceSNate Lawson 	 *
510789f03ceSNate Lawson 	 * XXX Note: this is not quite right since we really need to go
511789f03ceSNate Lawson 	 * through each level and compare both absolute and relative
512789f03ceSNate Lawson 	 * settings for each driver in the system before making a match.
513789f03ceSNate Lawson 	 * The estimation code below catches this case though.
514789f03ceSNate Lawson 	 */
515789f03ceSNate Lawson 	CF_MTX_LOCK(&sc->lock);
5164577cf37SConrad Meyer 	i = cpufreq_get_level(sc->cf_drv_dev, levels, count);
5174577cf37SConrad Meyer 	if (i >= 0)
51873347b07SNate Lawson 		sc->curr_level = levels[i];
5194577cf37SConrad Meyer 	else
5204577cf37SConrad Meyer 		CF_DEBUG("Couldn't find supported level for %s\n",
5214577cf37SConrad Meyer 		    device_get_nameunit(sc->cf_drv_dev));
5224577cf37SConrad Meyer 
5238d913481SNate Lawson 	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
5248d913481SNate Lawson 		CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
52573347b07SNate Lawson 		goto out;
5268d913481SNate Lawson 	}
52773347b07SNate Lawson 
52873347b07SNate Lawson 	/*
52973347b07SNate Lawson 	 * We couldn't find an exact match, so attempt to estimate and then
53073347b07SNate Lawson 	 * match against a level.
53173347b07SNate Lawson 	 */
53273347b07SNate Lawson 	pc = cpu_get_pcpu(dev);
53373347b07SNate Lawson 	if (pc == NULL) {
53473347b07SNate Lawson 		error = ENXIO;
53573347b07SNate Lawson 		goto out;
53673347b07SNate Lawson 	}
53773347b07SNate Lawson 	cpu_est_clockrate(pc->pc_cpuid, &rate);
53873347b07SNate Lawson 	rate /= 1000000;
539760f4decSColin Percival 	bdiff = 1 << 30;
54073347b07SNate Lawson 	for (i = 0; i < count; i++) {
541760f4decSColin Percival 		diff = abs(levels[i].total_set.freq - rate);
542760f4decSColin Percival 		if (diff < bdiff) {
543760f4decSColin Percival 			bdiff = diff;
54473347b07SNate Lawson 			sc->curr_level = levels[i];
545760f4decSColin Percival 		}
546760f4decSColin Percival 	}
5478d913481SNate Lawson 	CF_DEBUG("get estimated freq %d\n", curr_set->freq);
54873347b07SNate Lawson 
54973347b07SNate Lawson out:
550789f03ceSNate Lawson 	if (error == 0)
551789f03ceSNate Lawson 		*level = sc->curr_level;
552789f03ceSNate Lawson 
553789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
55473347b07SNate Lawson 	if (levels)
55573347b07SNate Lawson 		free(levels, M_TEMP);
556789f03ceSNate Lawson 	return (error);
55773347b07SNate Lawson }
55873347b07SNate Lawson 
5594577cf37SConrad Meyer /*
5604577cf37SConrad Meyer  * Either directly obtain settings from the cpufreq driver, or build a list of
5614577cf37SConrad Meyer  * relative settings to be integrated later against an absolute max.
5624577cf37SConrad Meyer  */
56373347b07SNate Lawson static int
5644577cf37SConrad Meyer cpufreq_add_levels(device_t cf_dev, struct cf_setting_lst *rel_sets)
56573347b07SNate Lawson {
56688c9b54cSNate Lawson 	struct cf_setting_array *set_arr;
56773347b07SNate Lawson 	struct cf_setting *sets;
5684577cf37SConrad Meyer 	device_t dev;
5694577cf37SConrad Meyer 	struct cpufreq_softc *sc;
5704577cf37SConrad Meyer 	int type, set_count, error;
57173347b07SNate Lawson 
5724577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
5734577cf37SConrad Meyer 	dev = sc->cf_drv_dev;
57473347b07SNate Lawson 
575e22cd41cSNate Lawson 	/* Skip devices that aren't ready. */
5764577cf37SConrad Meyer 	if (!device_is_attached(cf_dev))
5774577cf37SConrad Meyer 		return (0);
578e22cd41cSNate Lawson 
579e22cd41cSNate Lawson 	/*
580e22cd41cSNate Lawson 	 * Get settings, skipping drivers that offer no settings or
581e22cd41cSNate Lawson 	 * provide settings for informational purposes only.
582e22cd41cSNate Lawson 	 */
5834577cf37SConrad Meyer 	error = CPUFREQ_DRV_TYPE(dev, &type);
5844577cf37SConrad Meyer 	if (error != 0 || (type & CPUFREQ_FLAG_INFO_ONLY)) {
5858d913481SNate Lawson 		if (error == 0) {
5868d913481SNate Lawson 			CF_DEBUG("skipping info-only driver %s\n",
5874577cf37SConrad Meyer 			    device_get_nameunit(cf_dev));
5888d913481SNate Lawson 		}
5894577cf37SConrad Meyer 		return (error);
5908d913481SNate Lawson 	}
5914577cf37SConrad Meyer 
5924577cf37SConrad Meyer 	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
5934577cf37SConrad Meyer 	if (sets == NULL)
5944577cf37SConrad Meyer 		return (ENOMEM);
5954577cf37SConrad Meyer 
59673347b07SNate Lawson 	set_count = MAX_SETTINGS;
5974577cf37SConrad Meyer 	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
5984577cf37SConrad Meyer 	if (error != 0 || set_count == 0)
5994577cf37SConrad Meyer 		goto out;
60073347b07SNate Lawson 
601e22cd41cSNate Lawson 	/* Add the settings to our absolute/relative lists. */
6020325089dSNate Lawson 	switch (type & CPUFREQ_TYPE_MASK) {
60388c9b54cSNate Lawson 	case CPUFREQ_TYPE_ABSOLUTE:
60488c9b54cSNate Lawson 		error = cpufreq_insert_abs(sc, sets, set_count);
60588c9b54cSNate Lawson 		break;
60688c9b54cSNate Lawson 	case CPUFREQ_TYPE_RELATIVE:
6078d913481SNate Lawson 		CF_DEBUG("adding %d relative settings\n", set_count);
60888c9b54cSNate Lawson 		set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
60988c9b54cSNate Lawson 		if (set_arr == NULL) {
61088c9b54cSNate Lawson 			error = ENOMEM;
61188c9b54cSNate Lawson 			goto out;
61288c9b54cSNate Lawson 		}
61388c9b54cSNate Lawson 		bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
61488c9b54cSNate Lawson 		set_arr->count = set_count;
6154577cf37SConrad Meyer 		TAILQ_INSERT_TAIL(rel_sets, set_arr, link);
61688c9b54cSNate Lawson 		break;
61788c9b54cSNate Lawson 	default:
61888c9b54cSNate Lawson 		error = EINVAL;
61988c9b54cSNate Lawson 	}
6204577cf37SConrad Meyer 
6214577cf37SConrad Meyer out:
6224577cf37SConrad Meyer 	free(sets, M_TEMP);
6234577cf37SConrad Meyer 	return (error);
6244577cf37SConrad Meyer }
6254577cf37SConrad Meyer 
6264577cf37SConrad Meyer static int
6274577cf37SConrad Meyer cf_levels_method(device_t dev, struct cf_level *levels, int *count)
6284577cf37SConrad Meyer {
6294577cf37SConrad Meyer 	struct cf_setting_array *set_arr;
6304577cf37SConrad Meyer 	struct cf_setting_lst rel_sets;
6314577cf37SConrad Meyer 	struct cpufreq_softc *sc;
6324577cf37SConrad Meyer 	struct cf_level *lev;
6334577cf37SConrad Meyer 	struct pcpu *pc;
6344577cf37SConrad Meyer 	int error, i;
6354577cf37SConrad Meyer 	uint64_t rate;
6364577cf37SConrad Meyer 
6374577cf37SConrad Meyer 	if (levels == NULL || count == NULL)
6384577cf37SConrad Meyer 		return (EINVAL);
6394577cf37SConrad Meyer 
6404577cf37SConrad Meyer 	TAILQ_INIT(&rel_sets);
6414577cf37SConrad Meyer 	sc = device_get_softc(dev);
6424577cf37SConrad Meyer 
6434577cf37SConrad Meyer 	CF_MTX_LOCK(&sc->lock);
6444577cf37SConrad Meyer 	error = cpufreq_add_levels(sc->dev, &rel_sets);
64588c9b54cSNate Lawson 	if (error)
64673347b07SNate Lawson 		goto out;
64773347b07SNate Lawson 
64867c8649fSNate Lawson 	/*
64967c8649fSNate Lawson 	 * If there are no absolute levels, create a fake one at 100%.  We
65067c8649fSNate Lawson 	 * then cache the clockrate for later use as our base frequency.
65167c8649fSNate Lawson 	 */
65273347b07SNate Lawson 	if (TAILQ_EMPTY(&sc->all_levels)) {
6534577cf37SConrad Meyer 		struct cf_setting set;
6544577cf37SConrad Meyer 
6554577cf37SConrad Meyer 		CF_DEBUG("No absolute levels returned by driver\n");
6564577cf37SConrad Meyer 
65767c8649fSNate Lawson 		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
658f436f175SNathan Whitehorn 			sc->max_mhz = cpu_get_nominal_mhz(dev);
659f436f175SNathan Whitehorn 			/*
660f436f175SNathan Whitehorn 			 * If the CPU can't report a rate for 100%, hope
661f436f175SNathan Whitehorn 			 * the CPU is running at its nominal rate right now,
662f436f175SNathan Whitehorn 			 * and use that instead.
663f436f175SNathan Whitehorn 			 */
664f436f175SNathan Whitehorn 			if (sc->max_mhz <= 0) {
66573347b07SNate Lawson 				pc = cpu_get_pcpu(dev);
66673347b07SNate Lawson 				cpu_est_clockrate(pc->pc_cpuid, &rate);
66767c8649fSNate Lawson 				sc->max_mhz = rate / 1000000;
66867c8649fSNate Lawson 			}
669f436f175SNathan Whitehorn 		}
6704577cf37SConrad Meyer 		memset(&set, CPUFREQ_VAL_UNKNOWN, sizeof(set));
6714577cf37SConrad Meyer 		set.freq = sc->max_mhz;
6724577cf37SConrad Meyer 		set.dev = NULL;
6734577cf37SConrad Meyer 		error = cpufreq_insert_abs(sc, &set, 1);
67473347b07SNate Lawson 		if (error)
67573347b07SNate Lawson 			goto out;
67673347b07SNate Lawson 	}
67773347b07SNate Lawson 
67888c9b54cSNate Lawson 	/* Create a combined list of absolute + relative levels. */
67988c9b54cSNate Lawson 	TAILQ_FOREACH(set_arr, &rel_sets, link)
68088c9b54cSNate Lawson 		cpufreq_expand_set(sc, set_arr);
68188c9b54cSNate Lawson 
68288c9b54cSNate Lawson 	/* If the caller doesn't have enough space, return the actual count. */
68388c9b54cSNate Lawson 	if (sc->all_count > *count) {
68488c9b54cSNate Lawson 		*count = sc->all_count;
68588c9b54cSNate Lawson 		error = E2BIG;
68688c9b54cSNate Lawson 		goto out;
68788c9b54cSNate Lawson 	}
68888c9b54cSNate Lawson 
68988c9b54cSNate Lawson 	/* Finally, output the list of levels. */
69073347b07SNate Lawson 	i = 0;
69173347b07SNate Lawson 	TAILQ_FOREACH(lev, &sc->all_levels, link) {
692e1f13773SNate Lawson 
693b070969bSNate Lawson 		/* Skip levels that have a frequency that is too low. */
694da8a77c1SNate Lawson 		if (lev->total_set.freq < cf_lowest_freq) {
695b070969bSNate Lawson 			sc->all_count--;
696b070969bSNate Lawson 			continue;
697b070969bSNate Lawson 		}
698b070969bSNate Lawson 
69973347b07SNate Lawson 		levels[i] = *lev;
70073347b07SNate Lawson 		i++;
70173347b07SNate Lawson 	}
70288c9b54cSNate Lawson 	*count = sc->all_count;
70373347b07SNate Lawson 	error = 0;
70473347b07SNate Lawson 
70573347b07SNate Lawson out:
70673347b07SNate Lawson 	/* Clear all levels since we regenerate them each time. */
70773347b07SNate Lawson 	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
70873347b07SNate Lawson 		TAILQ_REMOVE(&sc->all_levels, lev, link);
70973347b07SNate Lawson 		free(lev, M_TEMP);
71073347b07SNate Lawson 	}
711789f03ceSNate Lawson 	sc->all_count = 0;
712789f03ceSNate Lawson 
713789f03ceSNate Lawson 	CF_MTX_UNLOCK(&sc->lock);
71488c9b54cSNate Lawson 	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
71588c9b54cSNate Lawson 		TAILQ_REMOVE(&rel_sets, set_arr, link);
71688c9b54cSNate Lawson 		free(set_arr, M_TEMP);
71788c9b54cSNate Lawson 	}
71873347b07SNate Lawson 	return (error);
71973347b07SNate Lawson }
72073347b07SNate Lawson 
72173347b07SNate Lawson /*
72273347b07SNate Lawson  * Create levels for an array of absolute settings and insert them in
72373347b07SNate Lawson  * sorted order in the specified list.
72473347b07SNate Lawson  */
72573347b07SNate Lawson static int
72688c9b54cSNate Lawson cpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
72773347b07SNate Lawson     int count)
72873347b07SNate Lawson {
72988c9b54cSNate Lawson 	struct cf_level_lst *list;
73073347b07SNate Lawson 	struct cf_level *level, *search;
73132686761SEmmanuel Vadot 	int i, inserted;
73273347b07SNate Lawson 
733789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
734789f03ceSNate Lawson 
73588c9b54cSNate Lawson 	list = &sc->all_levels;
73673347b07SNate Lawson 	for (i = 0; i < count; i++) {
73773347b07SNate Lawson 		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
73873347b07SNate Lawson 		if (level == NULL)
73973347b07SNate Lawson 			return (ENOMEM);
74073347b07SNate Lawson 		level->abs_set = sets[i];
74188c9b54cSNate Lawson 		level->total_set = sets[i];
74288c9b54cSNate Lawson 		level->total_set.dev = NULL;
74388c9b54cSNate Lawson 		sc->all_count++;
74432686761SEmmanuel Vadot 		inserted = 0;
74573347b07SNate Lawson 
74673347b07SNate Lawson 		if (TAILQ_EMPTY(list)) {
7478d913481SNate Lawson 			CF_DEBUG("adding abs setting %d at head\n",
7488d913481SNate Lawson 			    sets[i].freq);
74973347b07SNate Lawson 			TAILQ_INSERT_HEAD(list, level, link);
75073347b07SNate Lawson 			continue;
75173347b07SNate Lawson 		}
75273347b07SNate Lawson 
75332686761SEmmanuel Vadot 		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link)
75488c9b54cSNate Lawson 			if (sets[i].freq <= search->total_set.freq) {
7558d913481SNate Lawson 				CF_DEBUG("adding abs setting %d after %d\n",
7568d913481SNate Lawson 				    sets[i].freq, search->total_set.freq);
75773347b07SNate Lawson 				TAILQ_INSERT_AFTER(list, search, level, link);
75832686761SEmmanuel Vadot 				inserted = 1;
75932686761SEmmanuel Vadot 				break;
76032686761SEmmanuel Vadot 			}
76132686761SEmmanuel Vadot 
76232686761SEmmanuel Vadot 		if (inserted == 0) {
76332686761SEmmanuel Vadot 			TAILQ_FOREACH(search, list, link)
76432686761SEmmanuel Vadot 				if (sets[i].freq >= search->total_set.freq) {
76532686761SEmmanuel Vadot 					CF_DEBUG("adding abs setting %d before %d\n",
76632686761SEmmanuel Vadot 					    sets[i].freq, search->total_set.freq);
76732686761SEmmanuel Vadot 					TAILQ_INSERT_BEFORE(search, level, link);
76873347b07SNate Lawson 					break;
76973347b07SNate Lawson 				}
77073347b07SNate Lawson 		}
77173347b07SNate Lawson 	}
77232686761SEmmanuel Vadot 
77373347b07SNate Lawson 	return (0);
77473347b07SNate Lawson }
77573347b07SNate Lawson 
77688c9b54cSNate Lawson /*
77788c9b54cSNate Lawson  * Expand a group of relative settings, creating derived levels from them.
77888c9b54cSNate Lawson  */
77988c9b54cSNate Lawson static int
78088c9b54cSNate Lawson cpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
78188c9b54cSNate Lawson {
78288c9b54cSNate Lawson 	struct cf_level *fill, *search;
78388c9b54cSNate Lawson 	struct cf_setting *set;
78488c9b54cSNate Lawson 	int i;
78588c9b54cSNate Lawson 
786789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
787789f03ceSNate Lawson 
7885308b2a6SNate Lawson 	/*
7895308b2a6SNate Lawson 	 * Walk the set of all existing levels in reverse.  This is so we
7905308b2a6SNate Lawson 	 * create derived states from the lowest absolute settings first
7915308b2a6SNate Lawson 	 * and discard duplicates created from higher absolute settings.
7925308b2a6SNate Lawson 	 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
7935308b2a6SNate Lawson 	 * preferable to 200 Mhz + 25% because absolute settings are more
7945308b2a6SNate Lawson 	 * efficient since they often change the voltage as well.
7955308b2a6SNate Lawson 	 */
7965308b2a6SNate Lawson 	TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
79788c9b54cSNate Lawson 		/* Add each setting to the level, duplicating if necessary. */
79888c9b54cSNate Lawson 		for (i = 0; i < set_arr->count; i++) {
79988c9b54cSNate Lawson 			set = &set_arr->sets[i];
80088c9b54cSNate Lawson 
80188c9b54cSNate Lawson 			/*
80288c9b54cSNate Lawson 			 * If this setting is less than 100%, split the level
80388c9b54cSNate Lawson 			 * into two and add this setting to the new level.
80488c9b54cSNate Lawson 			 */
80588c9b54cSNate Lawson 			fill = search;
8065308b2a6SNate Lawson 			if (set->freq < 10000) {
80788c9b54cSNate Lawson 				fill = cpufreq_dup_set(sc, search, set);
80888c9b54cSNate Lawson 
80988c9b54cSNate Lawson 				/*
8105308b2a6SNate Lawson 				 * The new level was a duplicate of an existing
8115308b2a6SNate Lawson 				 * level or its absolute setting is too high
8125308b2a6SNate Lawson 				 * so we freed it.  For example, we discard a
8135308b2a6SNate Lawson 				 * derived level of 1000 MHz/25% if a level
8145308b2a6SNate Lawson 				 * of 500 MHz/100% already exists.
81588c9b54cSNate Lawson 				 */
81688c9b54cSNate Lawson 				if (fill == NULL)
8175308b2a6SNate Lawson 					break;
8185308b2a6SNate Lawson 			}
81988c9b54cSNate Lawson 
82088c9b54cSNate Lawson 			/* Add this setting to the existing or new level. */
82188c9b54cSNate Lawson 			KASSERT(fill->rel_count < MAX_SETTINGS,
82288c9b54cSNate Lawson 			    ("cpufreq: too many relative drivers (%d)",
82388c9b54cSNate Lawson 			    MAX_SETTINGS));
82488c9b54cSNate Lawson 			fill->rel_set[fill->rel_count] = *set;
82588c9b54cSNate Lawson 			fill->rel_count++;
8268d913481SNate Lawson 			CF_DEBUG(
8278d913481SNate Lawson 			"expand set added rel setting %d%% to %d level\n",
8288d913481SNate Lawson 			    set->freq / 100, fill->total_set.freq);
82988c9b54cSNate Lawson 		}
83088c9b54cSNate Lawson 	}
83188c9b54cSNate Lawson 
83288c9b54cSNate Lawson 	return (0);
83388c9b54cSNate Lawson }
83488c9b54cSNate Lawson 
83588c9b54cSNate Lawson static struct cf_level *
83688c9b54cSNate Lawson cpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
83788c9b54cSNate Lawson     struct cf_setting *set)
83888c9b54cSNate Lawson {
83988c9b54cSNate Lawson 	struct cf_level_lst *list;
84088c9b54cSNate Lawson 	struct cf_level *fill, *itr;
84188c9b54cSNate Lawson 	struct cf_setting *fill_set, *itr_set;
84288c9b54cSNate Lawson 	int i;
84388c9b54cSNate Lawson 
844789f03ceSNate Lawson 	CF_MTX_ASSERT(&sc->lock);
845789f03ceSNate Lawson 
84688c9b54cSNate Lawson 	/*
84788c9b54cSNate Lawson 	 * Create a new level, copy it from the old one, and update the
84888c9b54cSNate Lawson 	 * total frequency and power by the percentage specified in the
84988c9b54cSNate Lawson 	 * relative setting.
85088c9b54cSNate Lawson 	 */
85188c9b54cSNate Lawson 	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
85288c9b54cSNate Lawson 	if (fill == NULL)
85388c9b54cSNate Lawson 		return (NULL);
85488c9b54cSNate Lawson 	*fill = *dup;
85588c9b54cSNate Lawson 	fill_set = &fill->total_set;
85688c9b54cSNate Lawson 	fill_set->freq =
85788c9b54cSNate Lawson 	    ((uint64_t)fill_set->freq * set->freq) / 10000;
85888c9b54cSNate Lawson 	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
85988c9b54cSNate Lawson 		fill_set->power = ((uint64_t)fill_set->power * set->freq)
86088c9b54cSNate Lawson 		    / 10000;
86188c9b54cSNate Lawson 	}
86288c9b54cSNate Lawson 	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
86388c9b54cSNate Lawson 		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
86488c9b54cSNate Lawson 			fill_set->lat += set->lat;
86588c9b54cSNate Lawson 		else
86688c9b54cSNate Lawson 			fill_set->lat = set->lat;
86788c9b54cSNate Lawson 	}
8688d913481SNate Lawson 	CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
86988c9b54cSNate Lawson 
87088c9b54cSNate Lawson 	/*
87188c9b54cSNate Lawson 	 * If we copied an old level that we already modified (say, at 100%),
87288c9b54cSNate Lawson 	 * we need to remove that setting before adding this one.  Since we
87388c9b54cSNate Lawson 	 * process each setting array in order, we know any settings for this
87488c9b54cSNate Lawson 	 * driver will be found at the end.
87588c9b54cSNate Lawson 	 */
87688c9b54cSNate Lawson 	for (i = fill->rel_count; i != 0; i--) {
87788c9b54cSNate Lawson 		if (fill->rel_set[i - 1].dev != set->dev)
87888c9b54cSNate Lawson 			break;
8798d913481SNate Lawson 		CF_DEBUG("removed last relative driver: %s\n",
8808d913481SNate Lawson 		    device_get_nameunit(set->dev));
88188c9b54cSNate Lawson 		fill->rel_count--;
88288c9b54cSNate Lawson 	}
88388c9b54cSNate Lawson 
88488c9b54cSNate Lawson 	/*
8855308b2a6SNate Lawson 	 * Insert the new level in sorted order.  If it is a duplicate of an
8865308b2a6SNate Lawson 	 * existing level (1) or has an absolute setting higher than the
8875308b2a6SNate Lawson 	 * existing level (2), do not add it.  We can do this since any such
8885308b2a6SNate Lawson 	 * level is guaranteed use less power.  For example (1), a level with
8895308b2a6SNate Lawson 	 * one absolute setting of 800 Mhz uses less power than one composed
8905308b2a6SNate Lawson 	 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
8915308b2a6SNate Lawson 	 * Also for example (2), a level of 800 Mhz/75% is preferable to
8925308b2a6SNate Lawson 	 * 1600 Mhz/25% even though the latter has a lower total frequency.
89388c9b54cSNate Lawson 	 */
89488c9b54cSNate Lawson 	list = &sc->all_levels;
8955308b2a6SNate Lawson 	KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
89688c9b54cSNate Lawson 	TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
89788c9b54cSNate Lawson 		itr_set = &itr->total_set;
8989000b91eSNate Lawson 		if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
8999000b91eSNate Lawson 			CF_DEBUG("dup set rejecting %d (dupe)\n",
9009000b91eSNate Lawson 			    fill_set->freq);
9019000b91eSNate Lawson 			itr = NULL;
9029000b91eSNate Lawson 			break;
9039000b91eSNate Lawson 		} else if (fill_set->freq < itr_set->freq) {
9049000b91eSNate Lawson 			if (fill->abs_set.freq <= itr->abs_set.freq) {
9058d913481SNate Lawson 				CF_DEBUG(
9068d913481SNate Lawson 			"dup done, inserting new level %d after %d\n",
9078d913481SNate Lawson 				    fill_set->freq, itr_set->freq);
90888c9b54cSNate Lawson 				TAILQ_INSERT_AFTER(list, itr, fill, link);
90988c9b54cSNate Lawson 				sc->all_count++;
9109000b91eSNate Lawson 			} else {
9119000b91eSNate Lawson 				CF_DEBUG("dup set rejecting %d (abs too big)\n",
9129000b91eSNate Lawson 				    fill_set->freq);
9139000b91eSNate Lawson 				itr = NULL;
9149000b91eSNate Lawson 			}
91588c9b54cSNate Lawson 			break;
91688c9b54cSNate Lawson 		}
91788c9b54cSNate Lawson 	}
9185308b2a6SNate Lawson 
9195308b2a6SNate Lawson 	/* We didn't find a good place for this new level so free it. */
9205308b2a6SNate Lawson 	if (itr == NULL) {
9215308b2a6SNate Lawson 		CF_DEBUG("dup set freeing new level %d (not optimal)\n",
9225308b2a6SNate Lawson 		    fill_set->freq);
9235308b2a6SNate Lawson 		free(fill, M_TEMP);
9245308b2a6SNate Lawson 		fill = NULL;
92588c9b54cSNate Lawson 	}
92688c9b54cSNate Lawson 
92788c9b54cSNate Lawson 	return (fill);
92888c9b54cSNate Lawson }
92988c9b54cSNate Lawson 
93073347b07SNate Lawson static int
93173347b07SNate Lawson cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
93273347b07SNate Lawson {
93373347b07SNate Lawson 	struct cpufreq_softc *sc;
93473347b07SNate Lawson 	struct cf_level *levels;
9355f3818a5SAlexander Motin 	int best, count, diff, bdiff, devcount, error, freq, i, n;
9360325089dSNate Lawson 	device_t *devs;
93773347b07SNate Lawson 
9380325089dSNate Lawson 	devs = NULL;
93973347b07SNate Lawson 	sc = oidp->oid_arg1;
940dac50931SAndriy Gapon 	levels = sc->levels_buf;
94173347b07SNate Lawson 
94273347b07SNate Lawson 	error = CPUFREQ_GET(sc->dev, &levels[0]);
94373347b07SNate Lawson 	if (error)
94473347b07SNate Lawson 		goto out;
94573347b07SNate Lawson 	freq = levels[0].total_set.freq;
94673347b07SNate Lawson 	error = sysctl_handle_int(oidp, &freq, 0, req);
94773347b07SNate Lawson 	if (error != 0 || req->newptr == NULL)
94873347b07SNate Lawson 		goto out;
94973347b07SNate Lawson 
9500325089dSNate Lawson 	/*
9510325089dSNate Lawson 	 * While we only call cpufreq_get() on one device (assuming all
9520325089dSNate Lawson 	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
9530325089dSNate Lawson 	 * This is needed for some MP systems.
9540325089dSNate Lawson 	 */
9550325089dSNate Lawson 	error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
95673347b07SNate Lawson 	if (error)
95773347b07SNate Lawson 		goto out;
9580325089dSNate Lawson 	for (n = 0; n < devcount; n++) {
9590325089dSNate Lawson 		count = CF_MAX_LEVELS;
9600325089dSNate Lawson 		error = CPUFREQ_LEVELS(devs[n], levels, &count);
961d269386aSNate Lawson 		if (error) {
962d269386aSNate Lawson 			if (error == E2BIG)
963d269386aSNate Lawson 				printf(
964d269386aSNate Lawson 			"cpufreq: need to increase CF_MAX_LEVELS\n");
9650325089dSNate Lawson 			break;
966d269386aSNate Lawson 		}
9675f3818a5SAlexander Motin 		best = 0;
9685f3818a5SAlexander Motin 		bdiff = 1 << 30;
96973347b07SNate Lawson 		for (i = 0; i < count; i++) {
9705f3818a5SAlexander Motin 			diff = abs(levels[i].total_set.freq - freq);
9715f3818a5SAlexander Motin 			if (diff < bdiff) {
9725f3818a5SAlexander Motin 				bdiff = diff;
9735f3818a5SAlexander Motin 				best = i;
97473347b07SNate Lawson 			}
97573347b07SNate Lawson 		}
9765f3818a5SAlexander Motin 		error = CPUFREQ_SET(devs[n], &levels[best], CPUFREQ_PRIO_USER);
9770325089dSNate Lawson 	}
97873347b07SNate Lawson 
97973347b07SNate Lawson out:
9800325089dSNate Lawson 	if (devs)
9810325089dSNate Lawson 		free(devs, M_TEMP);
98273347b07SNate Lawson 	return (error);
98373347b07SNate Lawson }
98473347b07SNate Lawson 
98573347b07SNate Lawson static int
98673347b07SNate Lawson cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
98773347b07SNate Lawson {
98873347b07SNate Lawson 	struct cpufreq_softc *sc;
98973347b07SNate Lawson 	struct cf_level *levels;
99073347b07SNate Lawson 	struct cf_setting *set;
99173347b07SNate Lawson 	struct sbuf sb;
99273347b07SNate Lawson 	int count, error, i;
99373347b07SNate Lawson 
99473347b07SNate Lawson 	sc = oidp->oid_arg1;
99573347b07SNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
99673347b07SNate Lawson 
99773347b07SNate Lawson 	/* Get settings from the device and generate the output string. */
99873347b07SNate Lawson 	count = CF_MAX_LEVELS;
999dac50931SAndriy Gapon 	levels = sc->levels_buf;
100030215f48SChristian Brueffer 	if (levels == NULL) {
100130215f48SChristian Brueffer 		sbuf_delete(&sb);
100273347b07SNate Lawson 		return (ENOMEM);
100330215f48SChristian Brueffer 	}
100473347b07SNate Lawson 	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
1005d269386aSNate Lawson 	if (error) {
1006d269386aSNate Lawson 		if (error == E2BIG)
1007d269386aSNate Lawson 			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
100873347b07SNate Lawson 		goto out;
1009d269386aSNate Lawson 	}
101073347b07SNate Lawson 	if (count) {
101173347b07SNate Lawson 		for (i = 0; i < count; i++) {
101273347b07SNate Lawson 			set = &levels[i].total_set;
101373347b07SNate Lawson 			sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
101473347b07SNate Lawson 		}
101573347b07SNate Lawson 	} else
101673347b07SNate Lawson 		sbuf_cpy(&sb, "0");
101773347b07SNate Lawson 	sbuf_trim(&sb);
101873347b07SNate Lawson 	sbuf_finish(&sb);
101973347b07SNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
102073347b07SNate Lawson 
102173347b07SNate Lawson out:
102273347b07SNate Lawson 	sbuf_delete(&sb);
102373347b07SNate Lawson 	return (error);
102473347b07SNate Lawson }
102573347b07SNate Lawson 
1026e959a70bSNate Lawson static int
1027e959a70bSNate Lawson cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
1028e959a70bSNate Lawson {
1029e959a70bSNate Lawson 	device_t dev;
1030e959a70bSNate Lawson 	struct cf_setting *sets;
1031e959a70bSNate Lawson 	struct sbuf sb;
1032e959a70bSNate Lawson 	int error, i, set_count;
1033e959a70bSNate Lawson 
1034e959a70bSNate Lawson 	dev = oidp->oid_arg1;
1035e959a70bSNate Lawson 	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
1036e959a70bSNate Lawson 
1037e959a70bSNate Lawson 	/* Get settings from the device and generate the output string. */
1038e959a70bSNate Lawson 	set_count = MAX_SETTINGS;
1039ac2fffa4SPedro F. Giffuni 	sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
104030215f48SChristian Brueffer 	if (sets == NULL) {
104130215f48SChristian Brueffer 		sbuf_delete(&sb);
1042e959a70bSNate Lawson 		return (ENOMEM);
104330215f48SChristian Brueffer 	}
1044e959a70bSNate Lawson 	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
1045e959a70bSNate Lawson 	if (error)
1046e959a70bSNate Lawson 		goto out;
1047e959a70bSNate Lawson 	if (set_count) {
1048e959a70bSNate Lawson 		for (i = 0; i < set_count; i++)
1049e959a70bSNate Lawson 			sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
1050e959a70bSNate Lawson 	} else
1051e959a70bSNate Lawson 		sbuf_cpy(&sb, "0");
1052e959a70bSNate Lawson 	sbuf_trim(&sb);
1053e959a70bSNate Lawson 	sbuf_finish(&sb);
1054e959a70bSNate Lawson 	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1055e959a70bSNate Lawson 
1056e959a70bSNate Lawson out:
1057e959a70bSNate Lawson 	free(sets, M_TEMP);
1058e959a70bSNate Lawson 	sbuf_delete(&sb);
1059e959a70bSNate Lawson 	return (error);
1060e959a70bSNate Lawson }
1061e959a70bSNate Lawson 
10624577cf37SConrad Meyer static void
10634577cf37SConrad Meyer cpufreq_add_freq_driver_sysctl(device_t cf_dev)
10644577cf37SConrad Meyer {
10654577cf37SConrad Meyer 	struct cpufreq_softc *sc;
10664577cf37SConrad Meyer 
10674577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
10684577cf37SConrad Meyer 	SYSCTL_ADD_CONST_STRING(&sc->sysctl_ctx,
10694577cf37SConrad Meyer 	    SYSCTL_CHILDREN(device_get_sysctl_tree(cf_dev)), OID_AUTO,
10704577cf37SConrad Meyer 	    "freq_driver", CTLFLAG_RD, device_get_nameunit(sc->cf_drv_dev),
10714577cf37SConrad Meyer 	    "cpufreq driver used by this cpu");
10724577cf37SConrad Meyer }
10734577cf37SConrad Meyer 
107473347b07SNate Lawson int
107573347b07SNate Lawson cpufreq_register(device_t dev)
107673347b07SNate Lawson {
107767c8649fSNate Lawson 	struct cpufreq_softc *sc;
107873347b07SNate Lawson 	device_t cf_dev, cpu_dev;
10794577cf37SConrad Meyer 	int error;
108073347b07SNate Lawson 
1081e959a70bSNate Lawson 	/* Add a sysctl to get each driver's settings separately. */
1082e959a70bSNate Lawson 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1083e959a70bSNate Lawson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1084*7029da5cSPawel Biernacki 	    OID_AUTO, "freq_settings",
1085*7029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, dev, 0,
1086e959a70bSNate Lawson 	    cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
1087e959a70bSNate Lawson 
108873347b07SNate Lawson 	/*
10890325089dSNate Lawson 	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
10900325089dSNate Lawson 	 * must offer the same levels and be switched at the same time.
109173347b07SNate Lawson 	 */
10920325089dSNate Lawson 	cpu_dev = device_get_parent(dev);
109367c8649fSNate Lawson 	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
109467c8649fSNate Lawson 		sc = device_get_softc(cf_dev);
109567c8649fSNate Lawson 		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
10964577cf37SConrad Meyer 		MPASS(sc->cf_drv_dev != NULL);
109773347b07SNate Lawson 		return (0);
109867c8649fSNate Lawson 	}
109973347b07SNate Lawson 
11000325089dSNate Lawson 	/* Add the child device and possibly sysctls. */
11010325089dSNate Lawson 	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
110273347b07SNate Lawson 	if (cf_dev == NULL)
110373347b07SNate Lawson 		return (ENOMEM);
110473347b07SNate Lawson 	device_quiet(cf_dev);
110573347b07SNate Lawson 
11064577cf37SConrad Meyer 	error = device_probe_and_attach(cf_dev);
11074577cf37SConrad Meyer 	if (error)
11084577cf37SConrad Meyer 		return (error);
11094577cf37SConrad Meyer 
11104577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
11114577cf37SConrad Meyer 	sc->cf_drv_dev = dev;
11124577cf37SConrad Meyer 	cpufreq_add_freq_driver_sysctl(cf_dev);
11134577cf37SConrad Meyer 	return (error);
111473347b07SNate Lawson }
111573347b07SNate Lawson 
111673347b07SNate Lawson int
111773347b07SNate Lawson cpufreq_unregister(device_t dev)
111873347b07SNate Lawson {
11194577cf37SConrad Meyer 	device_t cf_dev;
11204577cf37SConrad Meyer 	struct cpufreq_softc *sc;
112173347b07SNate Lawson 
112273347b07SNate Lawson 	/*
112373347b07SNate Lawson 	 * If this is the last cpufreq child device, remove the control
112473347b07SNate Lawson 	 * device as well.  We identify cpufreq children by calling a method
112573347b07SNate Lawson 	 * they support.
112673347b07SNate Lawson 	 */
112767c8649fSNate Lawson 	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
112871ab130cSNate Lawson 	if (cf_dev == NULL) {
112971ab130cSNate Lawson 		device_printf(dev,
113071ab130cSNate Lawson 	"warning: cpufreq_unregister called with no cpufreq device active\n");
113171ab130cSNate Lawson 		return (0);
113271ab130cSNate Lawson 	}
11334577cf37SConrad Meyer 	sc = device_get_softc(cf_dev);
11344577cf37SConrad Meyer 	MPASS(sc->cf_drv_dev == dev);
113573347b07SNate Lawson 	device_delete_child(device_get_parent(cf_dev), cf_dev);
113673347b07SNate Lawson 
113773347b07SNate Lawson 	return (0);
113873347b07SNate Lawson }
11390d4ac62aSNate Lawson 
11400d4ac62aSNate Lawson int
11410d4ac62aSNate Lawson cpufreq_settings_changed(device_t dev)
11420d4ac62aSNate Lawson {
11430d4ac62aSNate Lawson 
11440d4ac62aSNate Lawson 	EVENTHANDLER_INVOKE(cpufreq_levels_changed,
11450d4ac62aSNate Lawson 	    device_get_unit(device_get_parent(dev)));
11460d4ac62aSNate Lawson 	return (0);
11470d4ac62aSNate Lawson }
1148