kern_cpu.c revision 167905
1141240Snjl/*-
2167905Snjl * Copyright (c) 2004-2007 Nate Lawson (SDG)
3141240Snjl * All rights reserved.
4141240Snjl *
5141240Snjl * Redistribution and use in source and binary forms, with or without
6141240Snjl * modification, are permitted provided that the following conditions
7141240Snjl * are met:
8141240Snjl * 1. Redistributions of source code must retain the above copyright
9141240Snjl *    notice, this list of conditions and the following disclaimer.
10141240Snjl * 2. Redistributions in binary form must reproduce the above copyright
11141240Snjl *    notice, this list of conditions and the following disclaimer in the
12141240Snjl *    documentation and/or other materials provided with the distribution.
13141240Snjl *
14141240Snjl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15141240Snjl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16141240Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17141240Snjl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18141240Snjl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19141240Snjl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20141240Snjl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21141240Snjl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22141240Snjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23141240Snjl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24141240Snjl * SUCH DAMAGE.
25141240Snjl */
26141240Snjl
27141240Snjl#include <sys/cdefs.h>
28141240Snjl__FBSDID("$FreeBSD: head/sys/kern/kern_cpu.c 167905 2007-03-26 18:03:29Z njl $");
29141240Snjl
30141240Snjl#include <sys/param.h>
31141240Snjl#include <sys/bus.h>
32141240Snjl#include <sys/cpu.h>
33141240Snjl#include <sys/eventhandler.h>
34141240Snjl#include <sys/kernel.h>
35142603Snjl#include <sys/lock.h>
36141240Snjl#include <sys/malloc.h>
37141240Snjl#include <sys/module.h>
38141240Snjl#include <sys/proc.h>
39141240Snjl#include <sys/queue.h>
40141240Snjl#include <sys/sched.h>
41141240Snjl#include <sys/sysctl.h>
42141240Snjl#include <sys/systm.h>
43141240Snjl#include <sys/sbuf.h>
44142603Snjl#include <sys/sx.h>
45141814Snjl#include <sys/timetc.h>
46167905Snjl#include <sys/taskqueue.h>
47141240Snjl
48141240Snjl#include "cpufreq_if.h"
49141240Snjl
50141240Snjl/*
51141240Snjl * Common CPU frequency glue code.  Drivers for specific hardware can
52141240Snjl * attach this interface to allow users to get/set the CPU frequency.
53141240Snjl */
54141240Snjl
55141240Snjl/*
56141240Snjl * Number of levels we can handle.  Levels are synthesized from settings
57142395Snjl * so for M settings and N drivers, there may be M*N levels.
58141240Snjl */
59142395Snjl#define CF_MAX_LEVELS	64
60141240Snjl
61150847Sumestruct cf_saved_freq {
62150847Sume	struct cf_level			level;
63150847Sume	int				priority;
64150847Sume	SLIST_ENTRY(cf_saved_freq)	link;
65150847Sume};
66150847Sume
67141240Snjlstruct cpufreq_softc {
68142603Snjl	struct sx			lock;
69141240Snjl	struct cf_level			curr_level;
70141923Snjl	int				curr_priority;
71150847Sume	SLIST_HEAD(, cf_saved_freq)	saved_freq;
72141923Snjl	struct cf_level_lst		all_levels;
73141413Snjl	int				all_count;
74141945Snjl	int				max_mhz;
75141240Snjl	device_t			dev;
76141240Snjl	struct sysctl_ctx_list		sysctl_ctx;
77167905Snjl	struct task			startup_task;
78141240Snjl};
79141240Snjl
80141240Snjlstruct cf_setting_array {
81141240Snjl	struct cf_setting		sets[MAX_SETTINGS];
82141240Snjl	int				count;
83141240Snjl	TAILQ_ENTRY(cf_setting_array)	link;
84141240Snjl};
85141240Snjl
86141240SnjlTAILQ_HEAD(cf_setting_lst, cf_setting_array);
87141240Snjl
88142603Snjl#define CF_MTX_INIT(x)		sx_init((x), "cpufreq lock")
89142603Snjl#define CF_MTX_LOCK(x)		sx_xlock((x))
90142603Snjl#define CF_MTX_UNLOCK(x)	sx_xunlock((x))
91142603Snjl#define CF_MTX_ASSERT(x)	sx_assert((x), SX_XLOCKED)
92142603Snjl
93144876Snjl#define CF_DEBUG(msg...)	do {		\
94144876Snjl	if (cf_verbose)				\
95144876Snjl		printf("cpufreq: " msg);	\
96144876Snjl	} while (0)
97144876Snjl
98141240Snjlstatic int	cpufreq_attach(device_t dev);
99167905Snjlstatic void	cpufreq_startup_task(void *ctx, int pending);
100141240Snjlstatic int	cpufreq_detach(device_t dev);
101141240Snjlstatic int	cf_set_method(device_t dev, const struct cf_level *level,
102141240Snjl		    int priority);
103141240Snjlstatic int	cf_get_method(device_t dev, struct cf_level *level);
104141240Snjlstatic int	cf_levels_method(device_t dev, struct cf_level *levels,
105141240Snjl		    int *count);
106141413Snjlstatic int	cpufreq_insert_abs(struct cpufreq_softc *sc,
107141240Snjl		    struct cf_setting *sets, int count);
108141413Snjlstatic int	cpufreq_expand_set(struct cpufreq_softc *sc,
109141413Snjl		    struct cf_setting_array *set_arr);
110141413Snjlstatic struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
111141413Snjl		    struct cf_level *dup, struct cf_setting *set);
112141240Snjlstatic int	cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
113141240Snjlstatic int	cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
114142114Snjlstatic int	cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
115141240Snjl
116141240Snjlstatic device_method_t cpufreq_methods[] = {
117141240Snjl	DEVMETHOD(device_probe,		bus_generic_probe),
118141240Snjl	DEVMETHOD(device_attach,	cpufreq_attach),
119141240Snjl	DEVMETHOD(device_detach,	cpufreq_detach),
120141240Snjl
121141240Snjl        DEVMETHOD(cpufreq_set,		cf_set_method),
122141240Snjl        DEVMETHOD(cpufreq_get,		cf_get_method),
123141240Snjl        DEVMETHOD(cpufreq_levels,	cf_levels_method),
124141240Snjl	{0, 0}
125141240Snjl};
126141240Snjlstatic driver_t cpufreq_driver = {
127141240Snjl	"cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
128141240Snjl};
129141240Snjlstatic devclass_t cpufreq_dc;
130141240SnjlDRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
131141240Snjl
132142590Snjlstatic int		cf_lowest_freq;
133144876Snjlstatic int		cf_verbose;
134142590SnjlTUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq);
135144876SnjlTUNABLE_INT("debug.cpufreq.verbose", &cf_verbose);
136142590SnjlSYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, "cpufreq debugging");
137142590SnjlSYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1,
138142590Snjl    "Don't provide levels below this frequency.");
139144876SnjlSYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1,
140144876Snjl    "Print verbose debugging messages");
141142590Snjl
142141240Snjlstatic int
143141240Snjlcpufreq_attach(device_t dev)
144141240Snjl{
145141240Snjl	struct cpufreq_softc *sc;
146141240Snjl	device_t parent;
147141240Snjl	int numdevs;
148141240Snjl
149144876Snjl	CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
150141240Snjl	sc = device_get_softc(dev);
151141240Snjl	parent = device_get_parent(dev);
152141240Snjl	sc->dev = dev;
153141240Snjl	sysctl_ctx_init(&sc->sysctl_ctx);
154141240Snjl	TAILQ_INIT(&sc->all_levels);
155142603Snjl	CF_MTX_INIT(&sc->lock);
156141240Snjl	sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
157150847Sume	SLIST_INIT(&sc->saved_freq);
158141945Snjl	sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
159141240Snjl
160141240Snjl	/*
161141240Snjl	 * Only initialize one set of sysctls for all CPUs.  In the future,
162141240Snjl	 * if multiple CPUs can have different settings, we can move these
163141240Snjl	 * sysctls to be under every CPU instead of just the first one.
164141240Snjl	 */
165141240Snjl	numdevs = devclass_get_count(cpufreq_dc);
166141240Snjl	if (numdevs > 1)
167141240Snjl		return (0);
168141240Snjl
169144876Snjl	CF_DEBUG("initializing one-time data for %s\n",
170144876Snjl	    device_get_nameunit(dev));
171141240Snjl	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
172141240Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
173141240Snjl	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
174141240Snjl	    cpufreq_curr_sysctl, "I", "Current CPU frequency");
175141240Snjl	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
176141240Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
177141240Snjl	    OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
178141240Snjl	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
179141240Snjl
180167905Snjl	/*
181167905Snjl	 * Queue a one-shot broadcast that levels have changed.
182167905Snjl	 * It will run once the system has completed booting.
183167905Snjl	 */
184167905Snjl	TASK_INIT(&sc->startup_task, 0, cpufreq_startup_task, dev);
185167905Snjl	taskqueue_enqueue(taskqueue_thread, &sc->startup_task);
186167905Snjl
187141240Snjl	return (0);
188141240Snjl}
189141240Snjl
190167905Snjl/* Handle any work to be done for all drivers that attached during boot. */
191167905Snjlstatic void
192167905Snjlcpufreq_startup_task(void *ctx, int pending)
193167905Snjl{
194167905Snjl
195167905Snjl	cpufreq_settings_changed((device_t)ctx);
196167905Snjl}
197167905Snjl
198141240Snjlstatic int
199141240Snjlcpufreq_detach(device_t dev)
200141240Snjl{
201141240Snjl	struct cpufreq_softc *sc;
202150847Sume	struct cf_saved_freq *saved_freq;
203141240Snjl	int numdevs;
204141240Snjl
205144876Snjl	CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
206141240Snjl	sc = device_get_softc(dev);
207141240Snjl	sysctl_ctx_free(&sc->sysctl_ctx);
208141240Snjl
209150847Sume	while ((saved_freq = SLIST_FIRST(&sc->saved_freq)) != NULL) {
210150847Sume		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
211150847Sume		free(saved_freq, M_TEMP);
212150847Sume	}
213150847Sume
214141240Snjl	/* Only clean up these resources when the last device is detaching. */
215141240Snjl	numdevs = devclass_get_count(cpufreq_dc);
216144876Snjl	if (numdevs == 1) {
217144876Snjl		CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
218144876Snjl	}
219141240Snjl
220141240Snjl	return (0);
221141240Snjl}
222141240Snjl
223141240Snjlstatic int
224141240Snjlcf_set_method(device_t dev, const struct cf_level *level, int priority)
225141240Snjl{
226141240Snjl	struct cpufreq_softc *sc;
227141240Snjl	const struct cf_setting *set;
228150847Sume	struct cf_saved_freq *saved_freq, *curr_freq;
229141814Snjl	struct pcpu *pc;
230141814Snjl	int cpu_id, error, i;
231141240Snjl
232141240Snjl	sc = device_get_softc(dev);
233142603Snjl	error = 0;
234142603Snjl	set = NULL;
235150847Sume	saved_freq = NULL;
236141240Snjl
237167905Snjl	/* We are going to change levels so notify the pre-change handler. */
238167905Snjl	EVENTHANDLER_INVOKE(cpufreq_pre_change, level, &error);
239167905Snjl	if (error != 0) {
240167905Snjl		EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
241167905Snjl		return (error);
242156228Smnag	}
243141814Snjl
244150847Sume	CF_MTX_LOCK(&sc->lock);
245150847Sume
246141923Snjl	/*
247150847Sume	 * If the requested level has a lower priority, don't allow
248150847Sume	 * the new level right now.
249150847Sume	 */
250150847Sume	if (priority < sc->curr_priority) {
251150847Sume		CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
252150847Sume		    sc->curr_priority);
253150847Sume		error = EPERM;
254150847Sume		goto out;
255150847Sume	}
256150847Sume
257150847Sume	/*
258141923Snjl	 * If the caller didn't specify a level and one is saved, prepare to
259141923Snjl	 * restore the saved level.  If none has been saved, return an error.
260141923Snjl	 */
261141923Snjl	if (level == NULL) {
262150847Sume		saved_freq = SLIST_FIRST(&sc->saved_freq);
263150847Sume		if (saved_freq == NULL) {
264144876Snjl			CF_DEBUG("NULL level, no saved level\n");
265142603Snjl			error = ENXIO;
266142603Snjl			goto out;
267142603Snjl		}
268150847Sume		level = &saved_freq->level;
269150847Sume		priority = saved_freq->priority;
270150847Sume		CF_DEBUG("restoring saved level, freq %d prio %d\n",
271150847Sume		    level->total_set.freq, priority);
272142603Snjl	}
273141923Snjl
274142590Snjl	/* Reject levels that are below our specified threshold. */
275148972Snjl	if (level->total_set.freq < cf_lowest_freq) {
276144876Snjl		CF_DEBUG("rejecting freq %d, less than %d limit\n",
277144876Snjl		    level->total_set.freq, cf_lowest_freq);
278142603Snjl		error = EINVAL;
279142603Snjl		goto out;
280142603Snjl	}
281142590Snjl
282141240Snjl	/* If already at this level, just return. */
283144876Snjl	if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq)) {
284144876Snjl		CF_DEBUG("skipping freq %d, same as current level %d\n",
285144876Snjl		    level->total_set.freq, sc->curr_level.total_set.freq);
286149239Sume		goto skip;
287144876Snjl	}
288141240Snjl
289141240Snjl	/* First, set the absolute frequency via its driver. */
290141240Snjl	set = &level->abs_set;
291141240Snjl	if (set->dev) {
292141240Snjl		if (!device_is_attached(set->dev)) {
293141240Snjl			error = ENXIO;
294141240Snjl			goto out;
295141240Snjl		}
296141943Snjl
297141943Snjl		/* Bind to the target CPU before switching, if necessary. */
298141943Snjl		cpu_id = PCPU_GET(cpuid);
299141943Snjl		pc = cpu_get_pcpu(set->dev);
300141943Snjl		if (cpu_id != pc->pc_cpuid) {
301141943Snjl			mtx_lock_spin(&sched_lock);
302141943Snjl			sched_bind(curthread, pc->pc_cpuid);
303141943Snjl			mtx_unlock_spin(&sched_lock);
304141943Snjl		}
305144876Snjl		CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
306144876Snjl		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
307141240Snjl		error = CPUFREQ_DRV_SET(set->dev, set);
308141943Snjl		if (cpu_id != pc->pc_cpuid) {
309141943Snjl			mtx_lock_spin(&sched_lock);
310141943Snjl			sched_unbind(curthread);
311141943Snjl			mtx_unlock_spin(&sched_lock);
312141943Snjl		}
313141240Snjl		if (error) {
314141240Snjl			goto out;
315141240Snjl		}
316141240Snjl	}
317141240Snjl
318141413Snjl	/* Next, set any/all relative frequencies via their drivers. */
319141413Snjl	for (i = 0; i < level->rel_count; i++) {
320141413Snjl		set = &level->rel_set[i];
321141413Snjl		if (!device_is_attached(set->dev)) {
322141413Snjl			error = ENXIO;
323141413Snjl			goto out;
324141413Snjl		}
325141943Snjl
326141943Snjl		/* Bind to the target CPU before switching, if necessary. */
327141943Snjl		cpu_id = PCPU_GET(cpuid);
328141943Snjl		pc = cpu_get_pcpu(set->dev);
329141943Snjl		if (cpu_id != pc->pc_cpuid) {
330141943Snjl			mtx_lock_spin(&sched_lock);
331141943Snjl			sched_bind(curthread, pc->pc_cpuid);
332141943Snjl			mtx_unlock_spin(&sched_lock);
333141943Snjl		}
334144876Snjl		CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
335144876Snjl		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
336141413Snjl		error = CPUFREQ_DRV_SET(set->dev, set);
337141943Snjl		if (cpu_id != pc->pc_cpuid) {
338141943Snjl			mtx_lock_spin(&sched_lock);
339141943Snjl			sched_unbind(curthread);
340141943Snjl			mtx_unlock_spin(&sched_lock);
341141943Snjl		}
342141413Snjl		if (error) {
343141413Snjl			/* XXX Back out any successful setting? */
344141413Snjl			goto out;
345141413Snjl		}
346141413Snjl	}
347141240Snjl
348149239Sumeskip:
349141923Snjl	/*
350141923Snjl	 * Before recording the current level, check if we're going to a
351150847Sume	 * higher priority.  If so, save the previous level and priority.
352141923Snjl	 */
353141923Snjl	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
354150847Sume	    priority > sc->curr_priority) {
355144876Snjl		CF_DEBUG("saving level, freq %d prio %d\n",
356144876Snjl		    sc->curr_level.total_set.freq, sc->curr_priority);
357150847Sume		curr_freq = malloc(sizeof(*curr_freq), M_TEMP, M_NOWAIT);
358150847Sume		if (curr_freq == NULL) {
359150847Sume			error = ENOMEM;
360150847Sume			goto out;
361150847Sume		}
362150847Sume		curr_freq->level = sc->curr_level;
363150847Sume		curr_freq->priority = sc->curr_priority;
364150847Sume		SLIST_INSERT_HEAD(&sc->saved_freq, curr_freq, link);
365141923Snjl	}
366141240Snjl	sc->curr_level = *level;
367141923Snjl	sc->curr_priority = priority;
368141240Snjl
369150847Sume	/* If we were restoring a saved state, reset it to "unused". */
370150847Sume	if (saved_freq != NULL) {
371150847Sume		CF_DEBUG("resetting saved level\n");
372150847Sume		sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
373150847Sume		SLIST_REMOVE_HEAD(&sc->saved_freq, link);
374150847Sume		free(saved_freq, M_TEMP);
375150847Sume	}
376150847Sume
377141240Snjlout:
378142603Snjl	CF_MTX_UNLOCK(&sc->lock);
379167905Snjl
380167905Snjl	/*
381167905Snjl	 * We changed levels (or attempted to) so notify the post-change
382167905Snjl	 * handler of new frequency or error.
383167905Snjl	 */
384167905Snjl	EVENTHANDLER_INVOKE(cpufreq_post_change, level, error);
385142603Snjl	if (error && set)
386141240Snjl		device_printf(set->dev, "set freq failed, err %d\n", error);
387167905Snjl
388141240Snjl	return (error);
389141240Snjl}
390141240Snjl
391141240Snjlstatic int
392141240Snjlcf_get_method(device_t dev, struct cf_level *level)
393141240Snjl{
394141240Snjl	struct cpufreq_softc *sc;
395141240Snjl	struct cf_level *levels;
396141240Snjl	struct cf_setting *curr_set, set;
397141240Snjl	struct pcpu *pc;
398141240Snjl	device_t *devs;
399141240Snjl	int count, error, i, numdevs;
400141240Snjl	uint64_t rate;
401141240Snjl
402141240Snjl	sc = device_get_softc(dev);
403142603Snjl	error = 0;
404141240Snjl	levels = NULL;
405141240Snjl
406141240Snjl	/* If we already know the current frequency, we're done. */
407142603Snjl	CF_MTX_LOCK(&sc->lock);
408142603Snjl	curr_set = &sc->curr_level.total_set;
409144876Snjl	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
410144876Snjl		CF_DEBUG("get returning known freq %d\n", curr_set->freq);
411141240Snjl		goto out;
412144876Snjl	}
413142603Snjl	CF_MTX_UNLOCK(&sc->lock);
414141240Snjl
415141240Snjl	/*
416141240Snjl	 * We need to figure out the current level.  Loop through every
417141240Snjl	 * driver, getting the current setting.  Then, attempt to get a best
418141240Snjl	 * match of settings against each level.
419141240Snjl	 */
420141240Snjl	count = CF_MAX_LEVELS;
421141240Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
422141240Snjl	if (levels == NULL)
423141240Snjl		return (ENOMEM);
424141240Snjl	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
425142395Snjl	if (error) {
426142395Snjl		if (error == E2BIG)
427142395Snjl			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
428142603Snjl		free(levels, M_TEMP);
429142603Snjl		return (error);
430142395Snjl	}
431141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
432142603Snjl	if (error) {
433142603Snjl		free(levels, M_TEMP);
434142603Snjl		return (error);
435142603Snjl	}
436142603Snjl
437142603Snjl	/*
438142603Snjl	 * Reacquire the lock and search for the given level.
439142603Snjl	 *
440142603Snjl	 * XXX Note: this is not quite right since we really need to go
441142603Snjl	 * through each level and compare both absolute and relative
442142603Snjl	 * settings for each driver in the system before making a match.
443142603Snjl	 * The estimation code below catches this case though.
444142603Snjl	 */
445142603Snjl	CF_MTX_LOCK(&sc->lock);
446141240Snjl	for (i = 0; i < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; i++) {
447141240Snjl		if (!device_is_attached(devs[i]))
448141240Snjl			continue;
449141240Snjl		error = CPUFREQ_DRV_GET(devs[i], &set);
450141240Snjl		if (error)
451141240Snjl			continue;
452141240Snjl		for (i = 0; i < count; i++) {
453141413Snjl			if (CPUFREQ_CMP(set.freq, levels[i].total_set.freq)) {
454141240Snjl				sc->curr_level = levels[i];
455141240Snjl				break;
456141240Snjl			}
457141240Snjl		}
458141240Snjl	}
459141240Snjl	free(devs, M_TEMP);
460144876Snjl	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
461144876Snjl		CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
462141240Snjl		goto out;
463144876Snjl	}
464141240Snjl
465141240Snjl	/*
466141240Snjl	 * We couldn't find an exact match, so attempt to estimate and then
467141240Snjl	 * match against a level.
468141240Snjl	 */
469141240Snjl	pc = cpu_get_pcpu(dev);
470141240Snjl	if (pc == NULL) {
471141240Snjl		error = ENXIO;
472141240Snjl		goto out;
473141240Snjl	}
474141240Snjl	cpu_est_clockrate(pc->pc_cpuid, &rate);
475141240Snjl	rate /= 1000000;
476141240Snjl	for (i = 0; i < count; i++) {
477141240Snjl		if (CPUFREQ_CMP(rate, levels[i].total_set.freq)) {
478141240Snjl			sc->curr_level = levels[i];
479144876Snjl			CF_DEBUG("get estimated freq %d\n", curr_set->freq);
480141240Snjl			break;
481141240Snjl		}
482141240Snjl	}
483141240Snjl
484141240Snjlout:
485142603Snjl	if (error == 0)
486142603Snjl		*level = sc->curr_level;
487142603Snjl
488142603Snjl	CF_MTX_UNLOCK(&sc->lock);
489141240Snjl	if (levels)
490141240Snjl		free(levels, M_TEMP);
491142603Snjl	return (error);
492141240Snjl}
493141240Snjl
494141240Snjlstatic int
495141240Snjlcf_levels_method(device_t dev, struct cf_level *levels, int *count)
496141240Snjl{
497141413Snjl	struct cf_setting_array *set_arr;
498141240Snjl	struct cf_setting_lst rel_sets;
499141240Snjl	struct cpufreq_softc *sc;
500141240Snjl	struct cf_level *lev;
501141240Snjl	struct cf_setting *sets;
502141240Snjl	struct pcpu *pc;
503141240Snjl	device_t *devs;
504141413Snjl	int error, i, numdevs, set_count, type;
505141240Snjl	uint64_t rate;
506141240Snjl
507141240Snjl	if (levels == NULL || count == NULL)
508141240Snjl		return (EINVAL);
509141240Snjl
510141240Snjl	TAILQ_INIT(&rel_sets);
511141240Snjl	sc = device_get_softc(dev);
512141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
513141240Snjl	if (error)
514141240Snjl		return (error);
515141240Snjl	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
516141240Snjl	if (sets == NULL) {
517141240Snjl		free(devs, M_TEMP);
518141240Snjl		return (ENOMEM);
519141240Snjl	}
520141240Snjl
521141240Snjl	/* Get settings from all cpufreq drivers. */
522142603Snjl	CF_MTX_LOCK(&sc->lock);
523141240Snjl	for (i = 0; i < numdevs; i++) {
524141824Snjl		/* Skip devices that aren't ready. */
525141240Snjl		if (!device_is_attached(devs[i]))
526141240Snjl			continue;
527141824Snjl
528141824Snjl		/*
529141824Snjl		 * Get settings, skipping drivers that offer no settings or
530141824Snjl		 * provide settings for informational purposes only.
531141824Snjl		 */
532142032Snjl		error = CPUFREQ_DRV_TYPE(devs[i], &type);
533144876Snjl		if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
534144876Snjl			if (error == 0) {
535144876Snjl				CF_DEBUG("skipping info-only driver %s\n",
536144876Snjl				    device_get_nameunit(devs[i]));
537144876Snjl			}
538142032Snjl			continue;
539144876Snjl		}
540141240Snjl		set_count = MAX_SETTINGS;
541142032Snjl		error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
542142032Snjl		if (error || set_count == 0)
543141240Snjl			continue;
544141413Snjl
545141824Snjl		/* Add the settings to our absolute/relative lists. */
546141814Snjl		switch (type & CPUFREQ_TYPE_MASK) {
547141413Snjl		case CPUFREQ_TYPE_ABSOLUTE:
548141413Snjl			error = cpufreq_insert_abs(sc, sets, set_count);
549141413Snjl			break;
550141413Snjl		case CPUFREQ_TYPE_RELATIVE:
551144876Snjl			CF_DEBUG("adding %d relative settings\n", set_count);
552141413Snjl			set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
553141413Snjl			if (set_arr == NULL) {
554141413Snjl				error = ENOMEM;
555141413Snjl				goto out;
556141413Snjl			}
557141413Snjl			bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
558141413Snjl			set_arr->count = set_count;
559141413Snjl			TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
560141413Snjl			break;
561141413Snjl		default:
562141413Snjl			error = EINVAL;
563141413Snjl		}
564141240Snjl		if (error)
565141240Snjl			goto out;
566141240Snjl	}
567141240Snjl
568141945Snjl	/*
569141945Snjl	 * If there are no absolute levels, create a fake one at 100%.  We
570141945Snjl	 * then cache the clockrate for later use as our base frequency.
571141945Snjl	 *
572141945Snjl	 * XXX This assumes that the first time through, if we only have
573141945Snjl	 * relative drivers, the CPU is currently running at 100%.
574141945Snjl	 */
575141240Snjl	if (TAILQ_EMPTY(&sc->all_levels)) {
576141945Snjl		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
577141945Snjl			pc = cpu_get_pcpu(dev);
578141945Snjl			cpu_est_clockrate(pc->pc_cpuid, &rate);
579141945Snjl			sc->max_mhz = rate / 1000000;
580141240Snjl		}
581141945Snjl		memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
582141945Snjl		sets[0].freq = sc->max_mhz;
583141945Snjl		sets[0].dev = NULL;
584141413Snjl		error = cpufreq_insert_abs(sc, sets, 1);
585141240Snjl		if (error)
586141240Snjl			goto out;
587141240Snjl	}
588141240Snjl
589141413Snjl	/* Create a combined list of absolute + relative levels. */
590141413Snjl	TAILQ_FOREACH(set_arr, &rel_sets, link)
591141413Snjl		cpufreq_expand_set(sc, set_arr);
592141413Snjl
593141413Snjl	/* If the caller doesn't have enough space, return the actual count. */
594141413Snjl	if (sc->all_count > *count) {
595141413Snjl		*count = sc->all_count;
596141413Snjl		error = E2BIG;
597141413Snjl		goto out;
598141413Snjl	}
599141413Snjl
600141413Snjl	/* Finally, output the list of levels. */
601141240Snjl	i = 0;
602141240Snjl	TAILQ_FOREACH(lev, &sc->all_levels, link) {
603142590Snjl		/* Skip levels that have a frequency that is too low. */
604148972Snjl		if (lev->total_set.freq < cf_lowest_freq) {
605142590Snjl			sc->all_count--;
606142590Snjl			continue;
607142590Snjl		}
608142590Snjl
609141240Snjl		levels[i] = *lev;
610141240Snjl		i++;
611141240Snjl	}
612141413Snjl	*count = sc->all_count;
613141240Snjl	error = 0;
614141240Snjl
615141240Snjlout:
616141240Snjl	/* Clear all levels since we regenerate them each time. */
617141240Snjl	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
618141240Snjl		TAILQ_REMOVE(&sc->all_levels, lev, link);
619141240Snjl		free(lev, M_TEMP);
620141240Snjl	}
621142603Snjl	sc->all_count = 0;
622142603Snjl
623142603Snjl	CF_MTX_UNLOCK(&sc->lock);
624141413Snjl	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
625141413Snjl		TAILQ_REMOVE(&rel_sets, set_arr, link);
626141413Snjl		free(set_arr, M_TEMP);
627141413Snjl	}
628141240Snjl	free(devs, M_TEMP);
629141240Snjl	free(sets, M_TEMP);
630141240Snjl	return (error);
631141240Snjl}
632141240Snjl
633141240Snjl/*
634141240Snjl * Create levels for an array of absolute settings and insert them in
635141240Snjl * sorted order in the specified list.
636141240Snjl */
637141240Snjlstatic int
638141413Snjlcpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
639141240Snjl    int count)
640141240Snjl{
641141413Snjl	struct cf_level_lst *list;
642141240Snjl	struct cf_level *level, *search;
643141240Snjl	int i;
644141240Snjl
645142603Snjl	CF_MTX_ASSERT(&sc->lock);
646142603Snjl
647141413Snjl	list = &sc->all_levels;
648141240Snjl	for (i = 0; i < count; i++) {
649141240Snjl		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
650141240Snjl		if (level == NULL)
651141240Snjl			return (ENOMEM);
652141240Snjl		level->abs_set = sets[i];
653141413Snjl		level->total_set = sets[i];
654141413Snjl		level->total_set.dev = NULL;
655141413Snjl		sc->all_count++;
656141240Snjl
657141240Snjl		if (TAILQ_EMPTY(list)) {
658144876Snjl			CF_DEBUG("adding abs setting %d at head\n",
659144876Snjl			    sets[i].freq);
660141240Snjl			TAILQ_INSERT_HEAD(list, level, link);
661141240Snjl			continue;
662141240Snjl		}
663141240Snjl
664141240Snjl		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
665141413Snjl			if (sets[i].freq <= search->total_set.freq) {
666144876Snjl				CF_DEBUG("adding abs setting %d after %d\n",
667144876Snjl				    sets[i].freq, search->total_set.freq);
668141240Snjl				TAILQ_INSERT_AFTER(list, search, level, link);
669141240Snjl				break;
670141240Snjl			}
671141240Snjl		}
672141240Snjl	}
673141240Snjl	return (0);
674141240Snjl}
675141240Snjl
676141413Snjl/*
677141413Snjl * Expand a group of relative settings, creating derived levels from them.
678141413Snjl */
679141240Snjlstatic int
680141413Snjlcpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
681141413Snjl{
682141413Snjl	struct cf_level *fill, *search;
683141413Snjl	struct cf_setting *set;
684141413Snjl	int i;
685141413Snjl
686142603Snjl	CF_MTX_ASSERT(&sc->lock);
687142603Snjl
688149607Snjl	/*
689149607Snjl	 * Walk the set of all existing levels in reverse.  This is so we
690149607Snjl	 * create derived states from the lowest absolute settings first
691149607Snjl	 * and discard duplicates created from higher absolute settings.
692149607Snjl	 * For instance, a level of 50 Mhz derived from 100 Mhz + 50% is
693149607Snjl	 * preferable to 200 Mhz + 25% because absolute settings are more
694149607Snjl	 * efficient since they often change the voltage as well.
695149607Snjl	 */
696149607Snjl	TAILQ_FOREACH_REVERSE(search, &sc->all_levels, cf_level_lst, link) {
697141413Snjl		/* Add each setting to the level, duplicating if necessary. */
698141413Snjl		for (i = 0; i < set_arr->count; i++) {
699141413Snjl			set = &set_arr->sets[i];
700141413Snjl
701141413Snjl			/*
702141413Snjl			 * If this setting is less than 100%, split the level
703141413Snjl			 * into two and add this setting to the new level.
704141413Snjl			 */
705141413Snjl			fill = search;
706149607Snjl			if (set->freq < 10000) {
707141413Snjl				fill = cpufreq_dup_set(sc, search, set);
708141413Snjl
709149607Snjl				/*
710149607Snjl				 * The new level was a duplicate of an existing
711149607Snjl				 * level or its absolute setting is too high
712149607Snjl				 * so we freed it.  For example, we discard a
713149607Snjl				 * derived level of 1000 MHz/25% if a level
714149607Snjl				 * of 500 MHz/100% already exists.
715149607Snjl				 */
716149607Snjl				if (fill == NULL)
717149607Snjl					break;
718149607Snjl			}
719141413Snjl
720141413Snjl			/* Add this setting to the existing or new level. */
721141413Snjl			KASSERT(fill->rel_count < MAX_SETTINGS,
722141413Snjl			    ("cpufreq: too many relative drivers (%d)",
723141413Snjl			    MAX_SETTINGS));
724141413Snjl			fill->rel_set[fill->rel_count] = *set;
725141413Snjl			fill->rel_count++;
726144876Snjl			CF_DEBUG(
727144876Snjl			"expand set added rel setting %d%% to %d level\n",
728144876Snjl			    set->freq / 100, fill->total_set.freq);
729141413Snjl		}
730141413Snjl	}
731141413Snjl
732141413Snjl	return (0);
733141413Snjl}
734141413Snjl
735141413Snjlstatic struct cf_level *
736141413Snjlcpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
737141413Snjl    struct cf_setting *set)
738141413Snjl{
739141413Snjl	struct cf_level_lst *list;
740141413Snjl	struct cf_level *fill, *itr;
741141413Snjl	struct cf_setting *fill_set, *itr_set;
742141413Snjl	int i;
743141413Snjl
744142603Snjl	CF_MTX_ASSERT(&sc->lock);
745142603Snjl
746141413Snjl	/*
747141413Snjl	 * Create a new level, copy it from the old one, and update the
748141413Snjl	 * total frequency and power by the percentage specified in the
749141413Snjl	 * relative setting.
750141413Snjl	 */
751141413Snjl	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
752141413Snjl	if (fill == NULL)
753141413Snjl		return (NULL);
754141413Snjl	*fill = *dup;
755141413Snjl	fill_set = &fill->total_set;
756141413Snjl	fill_set->freq =
757141413Snjl	    ((uint64_t)fill_set->freq * set->freq) / 10000;
758141413Snjl	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
759141413Snjl		fill_set->power = ((uint64_t)fill_set->power * set->freq)
760141413Snjl		    / 10000;
761141413Snjl	}
762141413Snjl	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
763141413Snjl		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
764141413Snjl			fill_set->lat += set->lat;
765141413Snjl		else
766141413Snjl			fill_set->lat = set->lat;
767141413Snjl	}
768144876Snjl	CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
769141413Snjl
770141413Snjl	/*
771141413Snjl	 * If we copied an old level that we already modified (say, at 100%),
772141413Snjl	 * we need to remove that setting before adding this one.  Since we
773141413Snjl	 * process each setting array in order, we know any settings for this
774141413Snjl	 * driver will be found at the end.
775141413Snjl	 */
776141413Snjl	for (i = fill->rel_count; i != 0; i--) {
777141413Snjl		if (fill->rel_set[i - 1].dev != set->dev)
778141413Snjl			break;
779144876Snjl		CF_DEBUG("removed last relative driver: %s\n",
780144876Snjl		    device_get_nameunit(set->dev));
781141413Snjl		fill->rel_count--;
782141413Snjl	}
783141413Snjl
784141413Snjl	/*
785149607Snjl	 * Insert the new level in sorted order.  If it is a duplicate of an
786149607Snjl	 * existing level (1) or has an absolute setting higher than the
787149607Snjl	 * existing level (2), do not add it.  We can do this since any such
788149607Snjl	 * level is guaranteed use less power.  For example (1), a level with
789149607Snjl	 * one absolute setting of 800 Mhz uses less power than one composed
790149607Snjl	 * of an absolute setting of 1600 Mhz and a relative setting at 50%.
791149607Snjl	 * Also for example (2), a level of 800 Mhz/75% is preferable to
792149607Snjl	 * 1600 Mhz/25% even though the latter has a lower total frequency.
793141413Snjl	 */
794141413Snjl	list = &sc->all_levels;
795149607Snjl	KASSERT(!TAILQ_EMPTY(list), ("all levels list empty in dup set"));
796149607Snjl	TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
797149607Snjl		itr_set = &itr->total_set;
798149724Snjl		if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
799149724Snjl			CF_DEBUG("dup set rejecting %d (dupe)\n",
800149724Snjl			    fill_set->freq);
801149724Snjl			itr = NULL;
802149607Snjl			break;
803149724Snjl		} else if (fill_set->freq < itr_set->freq) {
804149724Snjl			if (fill->abs_set.freq <= itr->abs_set.freq) {
805149724Snjl				CF_DEBUG(
806149724Snjl			"dup done, inserting new level %d after %d\n",
807149724Snjl				    fill_set->freq, itr_set->freq);
808149724Snjl				TAILQ_INSERT_AFTER(list, itr, fill, link);
809149724Snjl				sc->all_count++;
810149724Snjl			} else {
811149724Snjl				CF_DEBUG("dup set rejecting %d (abs too big)\n",
812149724Snjl				    fill_set->freq);
813149724Snjl				itr = NULL;
814149724Snjl			}
815149724Snjl			break;
816141413Snjl		}
817141413Snjl	}
818141413Snjl
819149607Snjl	/* We didn't find a good place for this new level so free it. */
820149607Snjl	if (itr == NULL) {
821149607Snjl		CF_DEBUG("dup set freeing new level %d (not optimal)\n",
822149607Snjl		    fill_set->freq);
823149607Snjl		free(fill, M_TEMP);
824149607Snjl		fill = NULL;
825149607Snjl	}
826149607Snjl
827141413Snjl	return (fill);
828141413Snjl}
829141413Snjl
830141413Snjlstatic int
831141240Snjlcpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
832141240Snjl{
833141240Snjl	struct cpufreq_softc *sc;
834141240Snjl	struct cf_level *levels;
835141814Snjl	int count, devcount, error, freq, i, n;
836141814Snjl	device_t *devs;
837141240Snjl
838141814Snjl	devs = NULL;
839141240Snjl	sc = oidp->oid_arg1;
840141814Snjl	levels = malloc(CF_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
841141240Snjl	if (levels == NULL)
842141240Snjl		return (ENOMEM);
843141240Snjl
844141240Snjl	error = CPUFREQ_GET(sc->dev, &levels[0]);
845141240Snjl	if (error)
846141240Snjl		goto out;
847141240Snjl	freq = levels[0].total_set.freq;
848141240Snjl	error = sysctl_handle_int(oidp, &freq, 0, req);
849141240Snjl	if (error != 0 || req->newptr == NULL)
850141240Snjl		goto out;
851141240Snjl
852141814Snjl	/*
853141814Snjl	 * While we only call cpufreq_get() on one device (assuming all
854141814Snjl	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
855141814Snjl	 * This is needed for some MP systems.
856141814Snjl	 */
857141814Snjl	error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
858141240Snjl	if (error)
859141240Snjl		goto out;
860141814Snjl	for (n = 0; n < devcount; n++) {
861141814Snjl		count = CF_MAX_LEVELS;
862141814Snjl		error = CPUFREQ_LEVELS(devs[n], levels, &count);
863142395Snjl		if (error) {
864142395Snjl			if (error == E2BIG)
865142395Snjl				printf(
866142395Snjl			"cpufreq: need to increase CF_MAX_LEVELS\n");
867141240Snjl			break;
868142395Snjl		}
869141814Snjl		for (i = 0; i < count; i++) {
870141814Snjl			if (CPUFREQ_CMP(levels[i].total_set.freq, freq)) {
871141814Snjl				error = CPUFREQ_SET(devs[n], &levels[i],
872141814Snjl				    CPUFREQ_PRIO_USER);
873141814Snjl				break;
874141814Snjl			}
875141240Snjl		}
876141814Snjl		if (i == count) {
877141814Snjl			error = EINVAL;
878141814Snjl			break;
879141814Snjl		}
880141240Snjl	}
881141240Snjl
882141240Snjlout:
883141814Snjl	if (devs)
884141814Snjl		free(devs, M_TEMP);
885141240Snjl	if (levels)
886141240Snjl		free(levels, M_TEMP);
887141240Snjl	return (error);
888141240Snjl}
889141240Snjl
890141240Snjlstatic int
891141240Snjlcpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
892141240Snjl{
893141240Snjl	struct cpufreq_softc *sc;
894141240Snjl	struct cf_level *levels;
895141240Snjl	struct cf_setting *set;
896141240Snjl	struct sbuf sb;
897141240Snjl	int count, error, i;
898141240Snjl
899141240Snjl	sc = oidp->oid_arg1;
900141240Snjl	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
901141240Snjl
902141240Snjl	/* Get settings from the device and generate the output string. */
903141240Snjl	count = CF_MAX_LEVELS;
904141240Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
905141240Snjl	if (levels == NULL)
906141240Snjl		return (ENOMEM);
907141240Snjl	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
908142395Snjl	if (error) {
909142395Snjl		if (error == E2BIG)
910142395Snjl			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
911141240Snjl		goto out;
912142395Snjl	}
913141240Snjl	if (count) {
914141240Snjl		for (i = 0; i < count; i++) {
915141240Snjl			set = &levels[i].total_set;
916141240Snjl			sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
917141240Snjl		}
918141240Snjl	} else
919141240Snjl		sbuf_cpy(&sb, "0");
920141240Snjl	sbuf_trim(&sb);
921141240Snjl	sbuf_finish(&sb);
922141240Snjl	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
923141240Snjl
924141240Snjlout:
925141240Snjl	free(levels, M_TEMP);
926141240Snjl	sbuf_delete(&sb);
927141240Snjl	return (error);
928141240Snjl}
929141240Snjl
930142114Snjlstatic int
931142114Snjlcpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
932142114Snjl{
933142114Snjl	device_t dev;
934142114Snjl	struct cf_setting *sets;
935142114Snjl	struct sbuf sb;
936142114Snjl	int error, i, set_count;
937142114Snjl
938142114Snjl	dev = oidp->oid_arg1;
939142114Snjl	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
940142114Snjl
941142114Snjl	/* Get settings from the device and generate the output string. */
942142114Snjl	set_count = MAX_SETTINGS;
943142114Snjl	sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
944142114Snjl	if (sets == NULL)
945142114Snjl		return (ENOMEM);
946142114Snjl	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
947142114Snjl	if (error)
948142114Snjl		goto out;
949142114Snjl	if (set_count) {
950142114Snjl		for (i = 0; i < set_count; i++)
951142114Snjl			sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
952142114Snjl	} else
953142114Snjl		sbuf_cpy(&sb, "0");
954142114Snjl	sbuf_trim(&sb);
955142114Snjl	sbuf_finish(&sb);
956142114Snjl	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
957142114Snjl
958142114Snjlout:
959142114Snjl	free(sets, M_TEMP);
960142114Snjl	sbuf_delete(&sb);
961142114Snjl	return (error);
962142114Snjl}
963142114Snjl
964141240Snjlint
965141240Snjlcpufreq_register(device_t dev)
966141240Snjl{
967141945Snjl	struct cpufreq_softc *sc;
968141240Snjl	device_t cf_dev, cpu_dev;
969141240Snjl
970142114Snjl	/* Add a sysctl to get each driver's settings separately. */
971142114Snjl	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
972142114Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
973142114Snjl	    OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
974142114Snjl	    cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
975142114Snjl
976141240Snjl	/*
977141814Snjl	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
978141814Snjl	 * must offer the same levels and be switched at the same time.
979141240Snjl	 */
980141814Snjl	cpu_dev = device_get_parent(dev);
981141945Snjl	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
982141945Snjl		sc = device_get_softc(cf_dev);
983141945Snjl		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
984141240Snjl		return (0);
985141945Snjl	}
986141240Snjl
987141814Snjl	/* Add the child device and possibly sysctls. */
988141814Snjl	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
989141240Snjl	if (cf_dev == NULL)
990141240Snjl		return (ENOMEM);
991141240Snjl	device_quiet(cf_dev);
992141240Snjl
993141240Snjl	return (device_probe_and_attach(cf_dev));
994141240Snjl}
995141240Snjl
996141240Snjlint
997141240Snjlcpufreq_unregister(device_t dev)
998141240Snjl{
999141240Snjl	device_t cf_dev, *devs;
1000142032Snjl	int cfcount, devcount, error, i, type;
1001141240Snjl
1002141240Snjl	/*
1003141240Snjl	 * If this is the last cpufreq child device, remove the control
1004141240Snjl	 * device as well.  We identify cpufreq children by calling a method
1005141240Snjl	 * they support.
1006141240Snjl	 */
1007141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &devcount);
1008141240Snjl	if (error)
1009141240Snjl		return (error);
1010141945Snjl	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
1011144413Snjl	if (cf_dev == NULL) {
1012144413Snjl		device_printf(dev,
1013144413Snjl	"warning: cpufreq_unregister called with no cpufreq device active\n");
1014144413Snjl		return (0);
1015144413Snjl	}
1016141240Snjl	cfcount = 0;
1017141240Snjl	for (i = 0; i < devcount; i++) {
1018141240Snjl		if (!device_is_attached(devs[i]))
1019141240Snjl			continue;
1020142032Snjl		if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
1021141240Snjl			cfcount++;
1022141240Snjl	}
1023141814Snjl	if (cfcount <= 1)
1024141240Snjl		device_delete_child(device_get_parent(cf_dev), cf_dev);
1025141240Snjl	free(devs, M_TEMP);
1026141240Snjl
1027141240Snjl	return (0);
1028141240Snjl}
1029167905Snjl
1030167905Snjlint
1031167905Snjlcpufreq_settings_changed(device_t dev)
1032167905Snjl{
1033167905Snjl
1034167905Snjl	EVENTHANDLER_INVOKE(cpufreq_levels_changed,
1035167905Snjl	    device_get_unit(device_get_parent(dev)));
1036167905Snjl	return (0);
1037167905Snjl}
1038