kern_cpu.c revision 149239
1141240Snjl/*-
2141240Snjl * Copyright (c) 2004-2005 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 149239 2005-08-18 16:41:25Z ume $");
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>
46141240Snjl
47141240Snjl#include "cpufreq_if.h"
48141240Snjl
49141240Snjl/*
50141240Snjl * Common CPU frequency glue code.  Drivers for specific hardware can
51141240Snjl * attach this interface to allow users to get/set the CPU frequency.
52141240Snjl */
53141240Snjl
54141240Snjl/*
55141240Snjl * Number of levels we can handle.  Levels are synthesized from settings
56142395Snjl * so for M settings and N drivers, there may be M*N levels.
57141240Snjl */
58142395Snjl#define CF_MAX_LEVELS	64
59141240Snjl
60141240Snjlstruct cpufreq_softc {
61142603Snjl	struct sx			lock;
62141240Snjl	struct cf_level			curr_level;
63141923Snjl	int				curr_priority;
64141923Snjl	struct cf_level			saved_level;
65141923Snjl	int				saved_priority;
66141923Snjl	struct cf_level_lst		all_levels;
67141413Snjl	int				all_count;
68141945Snjl	int				max_mhz;
69141240Snjl	device_t			dev;
70141240Snjl	struct sysctl_ctx_list		sysctl_ctx;
71141240Snjl};
72141240Snjl
73141240Snjlstruct cf_setting_array {
74141240Snjl	struct cf_setting		sets[MAX_SETTINGS];
75141240Snjl	int				count;
76141240Snjl	TAILQ_ENTRY(cf_setting_array)	link;
77141240Snjl};
78141240Snjl
79141240SnjlTAILQ_HEAD(cf_setting_lst, cf_setting_array);
80141240Snjl
81142603Snjl#define CF_MTX_INIT(x)		sx_init((x), "cpufreq lock")
82142603Snjl#define CF_MTX_LOCK(x)		sx_xlock((x))
83142603Snjl#define CF_MTX_UNLOCK(x)	sx_xunlock((x))
84142603Snjl#define CF_MTX_ASSERT(x)	sx_assert((x), SX_XLOCKED)
85142603Snjl
86144876Snjl#define CF_DEBUG(msg...)	do {		\
87144876Snjl	if (cf_verbose)				\
88144876Snjl		printf("cpufreq: " msg);	\
89144876Snjl	} while (0)
90144876Snjl
91141240Snjlstatic int	cpufreq_attach(device_t dev);
92141240Snjlstatic int	cpufreq_detach(device_t dev);
93141240Snjlstatic void	cpufreq_evaluate(void *arg);
94141240Snjlstatic int	cf_set_method(device_t dev, const struct cf_level *level,
95141240Snjl		    int priority);
96141240Snjlstatic int	cf_get_method(device_t dev, struct cf_level *level);
97141240Snjlstatic int	cf_levels_method(device_t dev, struct cf_level *levels,
98141240Snjl		    int *count);
99141413Snjlstatic int	cpufreq_insert_abs(struct cpufreq_softc *sc,
100141240Snjl		    struct cf_setting *sets, int count);
101141413Snjlstatic int	cpufreq_expand_set(struct cpufreq_softc *sc,
102141413Snjl		    struct cf_setting_array *set_arr);
103141413Snjlstatic struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
104141413Snjl		    struct cf_level *dup, struct cf_setting *set);
105141240Snjlstatic int	cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
106141240Snjlstatic int	cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
107142114Snjlstatic int	cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS);
108141240Snjl
109141240Snjlstatic device_method_t cpufreq_methods[] = {
110141240Snjl	DEVMETHOD(device_probe,		bus_generic_probe),
111141240Snjl	DEVMETHOD(device_attach,	cpufreq_attach),
112141240Snjl	DEVMETHOD(device_detach,	cpufreq_detach),
113141240Snjl
114141240Snjl        DEVMETHOD(cpufreq_set,		cf_set_method),
115141240Snjl        DEVMETHOD(cpufreq_get,		cf_get_method),
116141240Snjl        DEVMETHOD(cpufreq_levels,	cf_levels_method),
117141240Snjl	{0, 0}
118141240Snjl};
119141240Snjlstatic driver_t cpufreq_driver = {
120141240Snjl	"cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
121141240Snjl};
122141240Snjlstatic devclass_t cpufreq_dc;
123141240SnjlDRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
124141240Snjl
125142590Snjlstatic eventhandler_tag	cf_ev_tag;
126141240Snjl
127142590Snjlstatic int		cf_lowest_freq;
128144876Snjlstatic int		cf_verbose;
129142590SnjlTUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq);
130144876SnjlTUNABLE_INT("debug.cpufreq.verbose", &cf_verbose);
131142590SnjlSYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, "cpufreq debugging");
132142590SnjlSYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1,
133142590Snjl    "Don't provide levels below this frequency.");
134144876SnjlSYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1,
135144876Snjl    "Print verbose debugging messages");
136142590Snjl
137141240Snjlstatic int
138141240Snjlcpufreq_attach(device_t dev)
139141240Snjl{
140141240Snjl	struct cpufreq_softc *sc;
141141240Snjl	device_t parent;
142141240Snjl	int numdevs;
143141240Snjl
144144876Snjl	CF_DEBUG("initializing %s\n", device_get_nameunit(dev));
145141240Snjl	sc = device_get_softc(dev);
146141240Snjl	parent = device_get_parent(dev);
147141240Snjl	sc->dev = dev;
148141240Snjl	sysctl_ctx_init(&sc->sysctl_ctx);
149141240Snjl	TAILQ_INIT(&sc->all_levels);
150142603Snjl	CF_MTX_INIT(&sc->lock);
151141240Snjl	sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
152141923Snjl	sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
153141945Snjl	sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
154141240Snjl
155141240Snjl	/*
156141240Snjl	 * Only initialize one set of sysctls for all CPUs.  In the future,
157141240Snjl	 * if multiple CPUs can have different settings, we can move these
158141240Snjl	 * sysctls to be under every CPU instead of just the first one.
159141240Snjl	 */
160141240Snjl	numdevs = devclass_get_count(cpufreq_dc);
161141240Snjl	if (numdevs > 1)
162141240Snjl		return (0);
163141240Snjl
164144876Snjl	CF_DEBUG("initializing one-time data for %s\n",
165144876Snjl	    device_get_nameunit(dev));
166141240Snjl	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
167141240Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
168141240Snjl	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
169141240Snjl	    cpufreq_curr_sysctl, "I", "Current CPU frequency");
170141240Snjl	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
171141240Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
172141240Snjl	    OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
173141240Snjl	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
174141240Snjl	cf_ev_tag = EVENTHANDLER_REGISTER(cpufreq_changed, cpufreq_evaluate,
175141240Snjl	    NULL, EVENTHANDLER_PRI_ANY);
176141240Snjl
177141240Snjl	return (0);
178141240Snjl}
179141240Snjl
180141240Snjlstatic int
181141240Snjlcpufreq_detach(device_t dev)
182141240Snjl{
183141240Snjl	struct cpufreq_softc *sc;
184141240Snjl	int numdevs;
185141240Snjl
186144876Snjl	CF_DEBUG("shutdown %s\n", device_get_nameunit(dev));
187141240Snjl	sc = device_get_softc(dev);
188141240Snjl	sysctl_ctx_free(&sc->sysctl_ctx);
189141240Snjl
190141240Snjl	/* Only clean up these resources when the last device is detaching. */
191141240Snjl	numdevs = devclass_get_count(cpufreq_dc);
192144876Snjl	if (numdevs == 1) {
193144876Snjl		CF_DEBUG("final shutdown for %s\n", device_get_nameunit(dev));
194141240Snjl		EVENTHANDLER_DEREGISTER(cpufreq_changed, cf_ev_tag);
195144876Snjl	}
196141240Snjl
197141240Snjl	return (0);
198141240Snjl}
199141240Snjl
200141240Snjlstatic void
201141240Snjlcpufreq_evaluate(void *arg)
202141240Snjl{
203141240Snjl	/* TODO: Re-evaluate when notified of changes to drivers. */
204141240Snjl}
205141240Snjl
206141240Snjlstatic int
207141240Snjlcf_set_method(device_t dev, const struct cf_level *level, int priority)
208141240Snjl{
209141240Snjl	struct cpufreq_softc *sc;
210141240Snjl	const struct cf_setting *set;
211141814Snjl	struct pcpu *pc;
212141814Snjl	int cpu_id, error, i;
213141240Snjl
214141240Snjl	sc = device_get_softc(dev);
215142603Snjl	error = 0;
216142603Snjl	set = NULL;
217141240Snjl
218141814Snjl	/*
219141814Snjl	 * Check that the TSC isn't being used as a timecounter.
220141814Snjl	 * If it is, then return EBUSY and refuse to change the
221141814Snjl	 * clock speed.
222141814Snjl	 */
223141814Snjl	if (strcmp(timecounter->tc_name, "TSC") == 0)
224141814Snjl		return (EBUSY);
225141814Snjl
226141923Snjl	/*
227141923Snjl	 * If the caller didn't specify a level and one is saved, prepare to
228141923Snjl	 * restore the saved level.  If none has been saved, return an error.
229141923Snjl	 * If they did specify one, but the requested level has a lower
230141923Snjl	 * priority, don't allow the new level right now.
231141923Snjl	 */
232142603Snjl	CF_MTX_LOCK(&sc->lock);
233141923Snjl	if (level == NULL) {
234141923Snjl		if (sc->saved_level.total_set.freq != CPUFREQ_VAL_UNKNOWN) {
235141923Snjl			level = &sc->saved_level;
236141923Snjl			priority = sc->saved_priority;
237144876Snjl			CF_DEBUG("restoring saved level, freq %d prio %d\n",
238144876Snjl			    level->total_set.freq, priority);
239142603Snjl		} else {
240144876Snjl			CF_DEBUG("NULL level, no saved level\n");
241142603Snjl			error = ENXIO;
242142603Snjl			goto out;
243142603Snjl		}
244142603Snjl	} else if (priority < sc->curr_priority) {
245144876Snjl		CF_DEBUG("ignoring, curr prio %d less than %d\n", priority,
246144876Snjl		    sc->curr_priority);
247142603Snjl		error = EPERM;
248142603Snjl		goto out;
249142603Snjl	}
250141923Snjl
251142590Snjl	/* Reject levels that are below our specified threshold. */
252148972Snjl	if (level->total_set.freq < cf_lowest_freq) {
253144876Snjl		CF_DEBUG("rejecting freq %d, less than %d limit\n",
254144876Snjl		    level->total_set.freq, cf_lowest_freq);
255142603Snjl		error = EINVAL;
256142603Snjl		goto out;
257142603Snjl	}
258142590Snjl
259141240Snjl	/* If already at this level, just return. */
260144876Snjl	if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq)) {
261144876Snjl		CF_DEBUG("skipping freq %d, same as current level %d\n",
262144876Snjl		    level->total_set.freq, sc->curr_level.total_set.freq);
263149239Sume		goto skip;
264144876Snjl	}
265141240Snjl
266141240Snjl	/* First, set the absolute frequency via its driver. */
267141240Snjl	set = &level->abs_set;
268141240Snjl	if (set->dev) {
269141240Snjl		if (!device_is_attached(set->dev)) {
270141240Snjl			error = ENXIO;
271141240Snjl			goto out;
272141240Snjl		}
273141943Snjl
274141943Snjl		/* Bind to the target CPU before switching, if necessary. */
275141943Snjl		cpu_id = PCPU_GET(cpuid);
276141943Snjl		pc = cpu_get_pcpu(set->dev);
277141943Snjl		if (cpu_id != pc->pc_cpuid) {
278141943Snjl			mtx_lock_spin(&sched_lock);
279141943Snjl			sched_bind(curthread, pc->pc_cpuid);
280141943Snjl			mtx_unlock_spin(&sched_lock);
281141943Snjl		}
282144876Snjl		CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq,
283144876Snjl		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
284141240Snjl		error = CPUFREQ_DRV_SET(set->dev, set);
285141943Snjl		if (cpu_id != pc->pc_cpuid) {
286141943Snjl			mtx_lock_spin(&sched_lock);
287141943Snjl			sched_unbind(curthread);
288141943Snjl			mtx_unlock_spin(&sched_lock);
289141943Snjl		}
290141240Snjl		if (error) {
291141240Snjl			goto out;
292141240Snjl		}
293141240Snjl	}
294141240Snjl
295141413Snjl	/* Next, set any/all relative frequencies via their drivers. */
296141413Snjl	for (i = 0; i < level->rel_count; i++) {
297141413Snjl		set = &level->rel_set[i];
298141413Snjl		if (!device_is_attached(set->dev)) {
299141413Snjl			error = ENXIO;
300141413Snjl			goto out;
301141413Snjl		}
302141943Snjl
303141943Snjl		/* Bind to the target CPU before switching, if necessary. */
304141943Snjl		cpu_id = PCPU_GET(cpuid);
305141943Snjl		pc = cpu_get_pcpu(set->dev);
306141943Snjl		if (cpu_id != pc->pc_cpuid) {
307141943Snjl			mtx_lock_spin(&sched_lock);
308141943Snjl			sched_bind(curthread, pc->pc_cpuid);
309141943Snjl			mtx_unlock_spin(&sched_lock);
310141943Snjl		}
311144876Snjl		CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq,
312144876Snjl		    device_get_nameunit(set->dev), PCPU_GET(cpuid));
313141413Snjl		error = CPUFREQ_DRV_SET(set->dev, set);
314141943Snjl		if (cpu_id != pc->pc_cpuid) {
315141943Snjl			mtx_lock_spin(&sched_lock);
316141943Snjl			sched_unbind(curthread);
317141943Snjl			mtx_unlock_spin(&sched_lock);
318141943Snjl		}
319141413Snjl		if (error) {
320141413Snjl			/* XXX Back out any successful setting? */
321141413Snjl			goto out;
322141413Snjl		}
323141413Snjl	}
324141240Snjl
325149239Sumeskip:
326141923Snjl	/* If we were restoring a saved state, reset it to "unused". */
327141923Snjl	if (level == &sc->saved_level) {
328144876Snjl		CF_DEBUG("resetting saved level\n");
329141923Snjl		sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
330141923Snjl		sc->saved_priority = 0;
331141923Snjl	}
332141923Snjl
333141923Snjl	/*
334141923Snjl	 * Before recording the current level, check if we're going to a
335141923Snjl	 * higher priority and have not saved a level yet.  If so, save the
336141923Snjl	 * previous level and priority.
337141923Snjl	 */
338141923Snjl	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
339141923Snjl	    sc->saved_level.total_set.freq == CPUFREQ_VAL_UNKNOWN &&
340149149Sume	    priority > CPUFREQ_PRIO_USER && priority > sc->curr_priority) {
341144876Snjl		CF_DEBUG("saving level, freq %d prio %d\n",
342144876Snjl		    sc->curr_level.total_set.freq, sc->curr_priority);
343141923Snjl		sc->saved_level = sc->curr_level;
344141923Snjl		sc->saved_priority = sc->curr_priority;
345141923Snjl	}
346141240Snjl	sc->curr_level = *level;
347141923Snjl	sc->curr_priority = priority;
348141240Snjl
349141240Snjlout:
350142603Snjl	CF_MTX_UNLOCK(&sc->lock);
351142603Snjl	if (error && set)
352141240Snjl		device_printf(set->dev, "set freq failed, err %d\n", error);
353141240Snjl	return (error);
354141240Snjl}
355141240Snjl
356141240Snjlstatic int
357141240Snjlcf_get_method(device_t dev, struct cf_level *level)
358141240Snjl{
359141240Snjl	struct cpufreq_softc *sc;
360141240Snjl	struct cf_level *levels;
361141240Snjl	struct cf_setting *curr_set, set;
362141240Snjl	struct pcpu *pc;
363141240Snjl	device_t *devs;
364141240Snjl	int count, error, i, numdevs;
365141240Snjl	uint64_t rate;
366141240Snjl
367141240Snjl	sc = device_get_softc(dev);
368142603Snjl	error = 0;
369141240Snjl	levels = NULL;
370141240Snjl
371141240Snjl	/* If we already know the current frequency, we're done. */
372142603Snjl	CF_MTX_LOCK(&sc->lock);
373142603Snjl	curr_set = &sc->curr_level.total_set;
374144876Snjl	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
375144876Snjl		CF_DEBUG("get returning known freq %d\n", curr_set->freq);
376141240Snjl		goto out;
377144876Snjl	}
378142603Snjl	CF_MTX_UNLOCK(&sc->lock);
379141240Snjl
380141240Snjl	/*
381141240Snjl	 * We need to figure out the current level.  Loop through every
382141240Snjl	 * driver, getting the current setting.  Then, attempt to get a best
383141240Snjl	 * match of settings against each level.
384141240Snjl	 */
385141240Snjl	count = CF_MAX_LEVELS;
386141240Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
387141240Snjl	if (levels == NULL)
388141240Snjl		return (ENOMEM);
389141240Snjl	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
390142395Snjl	if (error) {
391142395Snjl		if (error == E2BIG)
392142395Snjl			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
393142603Snjl		free(levels, M_TEMP);
394142603Snjl		return (error);
395142395Snjl	}
396141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
397142603Snjl	if (error) {
398142603Snjl		free(levels, M_TEMP);
399142603Snjl		return (error);
400142603Snjl	}
401142603Snjl
402142603Snjl	/*
403142603Snjl	 * Reacquire the lock and search for the given level.
404142603Snjl	 *
405142603Snjl	 * XXX Note: this is not quite right since we really need to go
406142603Snjl	 * through each level and compare both absolute and relative
407142603Snjl	 * settings for each driver in the system before making a match.
408142603Snjl	 * The estimation code below catches this case though.
409142603Snjl	 */
410142603Snjl	CF_MTX_LOCK(&sc->lock);
411141240Snjl	for (i = 0; i < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; i++) {
412141240Snjl		if (!device_is_attached(devs[i]))
413141240Snjl			continue;
414141240Snjl		error = CPUFREQ_DRV_GET(devs[i], &set);
415141240Snjl		if (error)
416141240Snjl			continue;
417141240Snjl		for (i = 0; i < count; i++) {
418141413Snjl			if (CPUFREQ_CMP(set.freq, levels[i].total_set.freq)) {
419141240Snjl				sc->curr_level = levels[i];
420141240Snjl				break;
421141240Snjl			}
422141240Snjl		}
423141240Snjl	}
424141240Snjl	free(devs, M_TEMP);
425144876Snjl	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN) {
426144876Snjl		CF_DEBUG("get matched freq %d from drivers\n", curr_set->freq);
427141240Snjl		goto out;
428144876Snjl	}
429141240Snjl
430141240Snjl	/*
431141240Snjl	 * We couldn't find an exact match, so attempt to estimate and then
432141240Snjl	 * match against a level.
433141240Snjl	 */
434141240Snjl	pc = cpu_get_pcpu(dev);
435141240Snjl	if (pc == NULL) {
436141240Snjl		error = ENXIO;
437141240Snjl		goto out;
438141240Snjl	}
439141240Snjl	cpu_est_clockrate(pc->pc_cpuid, &rate);
440141240Snjl	rate /= 1000000;
441141240Snjl	for (i = 0; i < count; i++) {
442141240Snjl		if (CPUFREQ_CMP(rate, levels[i].total_set.freq)) {
443141240Snjl			sc->curr_level = levels[i];
444144876Snjl			CF_DEBUG("get estimated freq %d\n", curr_set->freq);
445141240Snjl			break;
446141240Snjl		}
447141240Snjl	}
448141240Snjl
449141240Snjlout:
450142603Snjl	if (error == 0)
451142603Snjl		*level = sc->curr_level;
452142603Snjl
453142603Snjl	CF_MTX_UNLOCK(&sc->lock);
454141240Snjl	if (levels)
455141240Snjl		free(levels, M_TEMP);
456142603Snjl	return (error);
457141240Snjl}
458141240Snjl
459141240Snjlstatic int
460141240Snjlcf_levels_method(device_t dev, struct cf_level *levels, int *count)
461141240Snjl{
462141413Snjl	struct cf_setting_array *set_arr;
463141240Snjl	struct cf_setting_lst rel_sets;
464141240Snjl	struct cpufreq_softc *sc;
465141240Snjl	struct cf_level *lev;
466141240Snjl	struct cf_setting *sets;
467141240Snjl	struct pcpu *pc;
468141240Snjl	device_t *devs;
469141413Snjl	int error, i, numdevs, set_count, type;
470141240Snjl	uint64_t rate;
471141240Snjl
472141240Snjl	if (levels == NULL || count == NULL)
473141240Snjl		return (EINVAL);
474141240Snjl
475141240Snjl	TAILQ_INIT(&rel_sets);
476141240Snjl	sc = device_get_softc(dev);
477141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
478141240Snjl	if (error)
479141240Snjl		return (error);
480141240Snjl	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
481141240Snjl	if (sets == NULL) {
482141240Snjl		free(devs, M_TEMP);
483141240Snjl		return (ENOMEM);
484141240Snjl	}
485141240Snjl
486141240Snjl	/* Get settings from all cpufreq drivers. */
487142603Snjl	CF_MTX_LOCK(&sc->lock);
488141240Snjl	for (i = 0; i < numdevs; i++) {
489141824Snjl		/* Skip devices that aren't ready. */
490141240Snjl		if (!device_is_attached(devs[i]))
491141240Snjl			continue;
492141824Snjl
493141824Snjl		/*
494141824Snjl		 * Get settings, skipping drivers that offer no settings or
495141824Snjl		 * provide settings for informational purposes only.
496141824Snjl		 */
497142032Snjl		error = CPUFREQ_DRV_TYPE(devs[i], &type);
498144876Snjl		if (error || (type & CPUFREQ_FLAG_INFO_ONLY)) {
499144876Snjl			if (error == 0) {
500144876Snjl				CF_DEBUG("skipping info-only driver %s\n",
501144876Snjl				    device_get_nameunit(devs[i]));
502144876Snjl			}
503142032Snjl			continue;
504144876Snjl		}
505141240Snjl		set_count = MAX_SETTINGS;
506142032Snjl		error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count);
507142032Snjl		if (error || set_count == 0)
508141240Snjl			continue;
509141413Snjl
510141824Snjl		/* Add the settings to our absolute/relative lists. */
511141814Snjl		switch (type & CPUFREQ_TYPE_MASK) {
512141413Snjl		case CPUFREQ_TYPE_ABSOLUTE:
513141413Snjl			error = cpufreq_insert_abs(sc, sets, set_count);
514141413Snjl			break;
515141413Snjl		case CPUFREQ_TYPE_RELATIVE:
516144876Snjl			CF_DEBUG("adding %d relative settings\n", set_count);
517141413Snjl			set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
518141413Snjl			if (set_arr == NULL) {
519141413Snjl				error = ENOMEM;
520141413Snjl				goto out;
521141413Snjl			}
522141413Snjl			bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
523141413Snjl			set_arr->count = set_count;
524141413Snjl			TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
525141413Snjl			break;
526141413Snjl		default:
527141413Snjl			error = EINVAL;
528141413Snjl		}
529141240Snjl		if (error)
530141240Snjl			goto out;
531141240Snjl	}
532141240Snjl
533141945Snjl	/*
534141945Snjl	 * If there are no absolute levels, create a fake one at 100%.  We
535141945Snjl	 * then cache the clockrate for later use as our base frequency.
536141945Snjl	 *
537141945Snjl	 * XXX This assumes that the first time through, if we only have
538141945Snjl	 * relative drivers, the CPU is currently running at 100%.
539141945Snjl	 */
540141240Snjl	if (TAILQ_EMPTY(&sc->all_levels)) {
541141945Snjl		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
542141945Snjl			pc = cpu_get_pcpu(dev);
543141945Snjl			cpu_est_clockrate(pc->pc_cpuid, &rate);
544141945Snjl			sc->max_mhz = rate / 1000000;
545141240Snjl		}
546141945Snjl		memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
547141945Snjl		sets[0].freq = sc->max_mhz;
548141945Snjl		sets[0].dev = NULL;
549141413Snjl		error = cpufreq_insert_abs(sc, sets, 1);
550141240Snjl		if (error)
551141240Snjl			goto out;
552141240Snjl	}
553141240Snjl
554141413Snjl	/* Create a combined list of absolute + relative levels. */
555141413Snjl	TAILQ_FOREACH(set_arr, &rel_sets, link)
556141413Snjl		cpufreq_expand_set(sc, set_arr);
557141413Snjl
558141413Snjl	/* If the caller doesn't have enough space, return the actual count. */
559141413Snjl	if (sc->all_count > *count) {
560141413Snjl		*count = sc->all_count;
561141413Snjl		error = E2BIG;
562141413Snjl		goto out;
563141413Snjl	}
564141413Snjl
565141413Snjl	/* Finally, output the list of levels. */
566141240Snjl	i = 0;
567141240Snjl	TAILQ_FOREACH(lev, &sc->all_levels, link) {
568142590Snjl		/* Skip levels that have a frequency that is too low. */
569148972Snjl		if (lev->total_set.freq < cf_lowest_freq) {
570142590Snjl			sc->all_count--;
571142590Snjl			continue;
572142590Snjl		}
573142590Snjl
574141240Snjl		levels[i] = *lev;
575141240Snjl		i++;
576141240Snjl	}
577141413Snjl	*count = sc->all_count;
578141240Snjl	error = 0;
579141240Snjl
580141240Snjlout:
581141240Snjl	/* Clear all levels since we regenerate them each time. */
582141240Snjl	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
583141240Snjl		TAILQ_REMOVE(&sc->all_levels, lev, link);
584141240Snjl		free(lev, M_TEMP);
585141240Snjl	}
586142603Snjl	sc->all_count = 0;
587142603Snjl
588142603Snjl	CF_MTX_UNLOCK(&sc->lock);
589141413Snjl	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
590141413Snjl		TAILQ_REMOVE(&rel_sets, set_arr, link);
591141413Snjl		free(set_arr, M_TEMP);
592141413Snjl	}
593141240Snjl	free(devs, M_TEMP);
594141240Snjl	free(sets, M_TEMP);
595141240Snjl	return (error);
596141240Snjl}
597141240Snjl
598141240Snjl/*
599141240Snjl * Create levels for an array of absolute settings and insert them in
600141240Snjl * sorted order in the specified list.
601141240Snjl */
602141240Snjlstatic int
603141413Snjlcpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
604141240Snjl    int count)
605141240Snjl{
606141413Snjl	struct cf_level_lst *list;
607141240Snjl	struct cf_level *level, *search;
608141240Snjl	int i;
609141240Snjl
610142603Snjl	CF_MTX_ASSERT(&sc->lock);
611142603Snjl
612141413Snjl	list = &sc->all_levels;
613141240Snjl	for (i = 0; i < count; i++) {
614141240Snjl		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
615141240Snjl		if (level == NULL)
616141240Snjl			return (ENOMEM);
617141240Snjl		level->abs_set = sets[i];
618141413Snjl		level->total_set = sets[i];
619141413Snjl		level->total_set.dev = NULL;
620141413Snjl		sc->all_count++;
621141240Snjl
622141240Snjl		if (TAILQ_EMPTY(list)) {
623144876Snjl			CF_DEBUG("adding abs setting %d at head\n",
624144876Snjl			    sets[i].freq);
625141240Snjl			TAILQ_INSERT_HEAD(list, level, link);
626141240Snjl			continue;
627141240Snjl		}
628141240Snjl
629141240Snjl		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
630141413Snjl			if (sets[i].freq <= search->total_set.freq) {
631144876Snjl				CF_DEBUG("adding abs setting %d after %d\n",
632144876Snjl				    sets[i].freq, search->total_set.freq);
633141240Snjl				TAILQ_INSERT_AFTER(list, search, level, link);
634141240Snjl				break;
635141240Snjl			}
636141240Snjl		}
637141240Snjl	}
638141240Snjl	return (0);
639141240Snjl}
640141240Snjl
641141413Snjl/*
642141413Snjl * Expand a group of relative settings, creating derived levels from them.
643141413Snjl */
644141240Snjlstatic int
645141413Snjlcpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
646141413Snjl{
647141413Snjl	struct cf_level *fill, *search;
648141413Snjl	struct cf_setting *set;
649141413Snjl	int i;
650141413Snjl
651142603Snjl	CF_MTX_ASSERT(&sc->lock);
652142603Snjl
653141413Snjl	TAILQ_FOREACH(search, &sc->all_levels, link) {
654141413Snjl		/* Skip this level if we've already modified it. */
655141413Snjl		for (i = 0; i < search->rel_count; i++) {
656141413Snjl			if (search->rel_set[i].dev == set_arr->sets[0].dev)
657141413Snjl				break;
658141413Snjl		}
659144876Snjl		if (i != search->rel_count) {
660144876Snjl			CF_DEBUG("skipping modified level, freq %d (dev %s)\n",
661144876Snjl			    search->total_set.freq,
662144876Snjl			    device_get_nameunit(search->rel_set[i].dev));
663141413Snjl			continue;
664144876Snjl		}
665141413Snjl
666141413Snjl		/* Add each setting to the level, duplicating if necessary. */
667141413Snjl		for (i = 0; i < set_arr->count; i++) {
668141413Snjl			set = &set_arr->sets[i];
669141413Snjl
670141413Snjl			/*
671141413Snjl			 * If this setting is less than 100%, split the level
672141413Snjl			 * into two and add this setting to the new level.
673141413Snjl			 */
674141413Snjl			fill = search;
675141413Snjl			if (set->freq < 10000)
676141413Snjl				fill = cpufreq_dup_set(sc, search, set);
677141413Snjl
678141413Snjl			/*
679141413Snjl			 * The new level was a duplicate of an existing level
680141413Snjl			 * so we freed it.  Go to the next setting.
681141413Snjl			 */
682141413Snjl			if (fill == NULL)
683141413Snjl				continue;
684141413Snjl
685141413Snjl			/* Add this setting to the existing or new level. */
686141413Snjl			KASSERT(fill->rel_count < MAX_SETTINGS,
687141413Snjl			    ("cpufreq: too many relative drivers (%d)",
688141413Snjl			    MAX_SETTINGS));
689141413Snjl			fill->rel_set[fill->rel_count] = *set;
690141413Snjl			fill->rel_count++;
691144876Snjl			CF_DEBUG(
692144876Snjl			"expand set added rel setting %d%% to %d level\n",
693144876Snjl			    set->freq / 100, fill->total_set.freq);
694141413Snjl		}
695141413Snjl	}
696141413Snjl
697141413Snjl	return (0);
698141413Snjl}
699141413Snjl
700141413Snjlstatic struct cf_level *
701141413Snjlcpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
702141413Snjl    struct cf_setting *set)
703141413Snjl{
704141413Snjl	struct cf_level_lst *list;
705141413Snjl	struct cf_level *fill, *itr;
706141413Snjl	struct cf_setting *fill_set, *itr_set;
707141413Snjl	int i;
708141413Snjl
709142603Snjl	CF_MTX_ASSERT(&sc->lock);
710142603Snjl
711141413Snjl	/*
712141413Snjl	 * Create a new level, copy it from the old one, and update the
713141413Snjl	 * total frequency and power by the percentage specified in the
714141413Snjl	 * relative setting.
715141413Snjl	 */
716141413Snjl	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
717141413Snjl	if (fill == NULL)
718141413Snjl		return (NULL);
719141413Snjl	*fill = *dup;
720141413Snjl	fill_set = &fill->total_set;
721141413Snjl	fill_set->freq =
722141413Snjl	    ((uint64_t)fill_set->freq * set->freq) / 10000;
723141413Snjl	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
724141413Snjl		fill_set->power = ((uint64_t)fill_set->power * set->freq)
725141413Snjl		    / 10000;
726141413Snjl	}
727141413Snjl	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
728141413Snjl		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
729141413Snjl			fill_set->lat += set->lat;
730141413Snjl		else
731141413Snjl			fill_set->lat = set->lat;
732141413Snjl	}
733144876Snjl	CF_DEBUG("dup set considering derived setting %d\n", fill_set->freq);
734141413Snjl
735141413Snjl	/*
736141413Snjl	 * If we copied an old level that we already modified (say, at 100%),
737141413Snjl	 * we need to remove that setting before adding this one.  Since we
738141413Snjl	 * process each setting array in order, we know any settings for this
739141413Snjl	 * driver will be found at the end.
740141413Snjl	 */
741141413Snjl	for (i = fill->rel_count; i != 0; i--) {
742141413Snjl		if (fill->rel_set[i - 1].dev != set->dev)
743141413Snjl			break;
744144876Snjl		CF_DEBUG("removed last relative driver: %s\n",
745144876Snjl		    device_get_nameunit(set->dev));
746141413Snjl		fill->rel_count--;
747141413Snjl	}
748141413Snjl
749141413Snjl	/*
750141413Snjl	 * Insert the new level in sorted order.  If we find a duplicate,
751141413Snjl	 * free the new level.  We can do this since any existing level will
752141413Snjl	 * be guaranteed to have the same or less settings and thus consume
753141413Snjl	 * less power.  For example, a level with one absolute setting of
754141413Snjl	 * 800 Mhz uses less power than one composed of an absolute setting
755141413Snjl	 * of 1600 Mhz and a relative setting at 50%.
756141413Snjl	 */
757141413Snjl	list = &sc->all_levels;
758141413Snjl	if (TAILQ_EMPTY(list)) {
759144876Snjl		CF_DEBUG("dup done, inserted %d at head\n", fill_set->freq);
760141413Snjl		TAILQ_INSERT_HEAD(list, fill, link);
761141413Snjl	} else {
762141413Snjl		TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
763141413Snjl			itr_set = &itr->total_set;
764141413Snjl			if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
765144876Snjl				CF_DEBUG(
766144876Snjl			"dup done, freeing new level %d, matches %d\n",
767144876Snjl				    fill_set->freq, itr_set->freq);
768141413Snjl				free(fill, M_TEMP);
769141413Snjl				fill = NULL;
770141413Snjl				break;
771141413Snjl			} else if (fill_set->freq < itr_set->freq) {
772144876Snjl				CF_DEBUG(
773144876Snjl			"dup done, inserting new level %d after %d\n",
774144876Snjl				    fill_set->freq, itr_set->freq);
775141413Snjl				TAILQ_INSERT_AFTER(list, itr, fill, link);
776141413Snjl				sc->all_count++;
777141413Snjl				break;
778141413Snjl			}
779141413Snjl		}
780141413Snjl	}
781141413Snjl
782141413Snjl	return (fill);
783141413Snjl}
784141413Snjl
785141413Snjlstatic int
786141240Snjlcpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
787141240Snjl{
788141240Snjl	struct cpufreq_softc *sc;
789141240Snjl	struct cf_level *levels;
790141814Snjl	int count, devcount, error, freq, i, n;
791141814Snjl	device_t *devs;
792141240Snjl
793141814Snjl	devs = NULL;
794141240Snjl	sc = oidp->oid_arg1;
795141814Snjl	levels = malloc(CF_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
796141240Snjl	if (levels == NULL)
797141240Snjl		return (ENOMEM);
798141240Snjl
799141240Snjl	error = CPUFREQ_GET(sc->dev, &levels[0]);
800141240Snjl	if (error)
801141240Snjl		goto out;
802141240Snjl	freq = levels[0].total_set.freq;
803141240Snjl	error = sysctl_handle_int(oidp, &freq, 0, req);
804141240Snjl	if (error != 0 || req->newptr == NULL)
805141240Snjl		goto out;
806141240Snjl
807141814Snjl	/*
808141814Snjl	 * While we only call cpufreq_get() on one device (assuming all
809141814Snjl	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
810141814Snjl	 * This is needed for some MP systems.
811141814Snjl	 */
812141814Snjl	error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
813141240Snjl	if (error)
814141240Snjl		goto out;
815141814Snjl	for (n = 0; n < devcount; n++) {
816141814Snjl		count = CF_MAX_LEVELS;
817141814Snjl		error = CPUFREQ_LEVELS(devs[n], levels, &count);
818142395Snjl		if (error) {
819142395Snjl			if (error == E2BIG)
820142395Snjl				printf(
821142395Snjl			"cpufreq: need to increase CF_MAX_LEVELS\n");
822141240Snjl			break;
823142395Snjl		}
824141814Snjl		for (i = 0; i < count; i++) {
825141814Snjl			if (CPUFREQ_CMP(levels[i].total_set.freq, freq)) {
826141814Snjl				error = CPUFREQ_SET(devs[n], &levels[i],
827141814Snjl				    CPUFREQ_PRIO_USER);
828141814Snjl				break;
829141814Snjl			}
830141240Snjl		}
831141814Snjl		if (i == count) {
832141814Snjl			error = EINVAL;
833141814Snjl			break;
834141814Snjl		}
835141240Snjl	}
836141240Snjl
837141240Snjlout:
838141814Snjl	if (devs)
839141814Snjl		free(devs, M_TEMP);
840141240Snjl	if (levels)
841141240Snjl		free(levels, M_TEMP);
842141240Snjl	return (error);
843141240Snjl}
844141240Snjl
845141240Snjlstatic int
846141240Snjlcpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
847141240Snjl{
848141240Snjl	struct cpufreq_softc *sc;
849141240Snjl	struct cf_level *levels;
850141240Snjl	struct cf_setting *set;
851141240Snjl	struct sbuf sb;
852141240Snjl	int count, error, i;
853141240Snjl
854141240Snjl	sc = oidp->oid_arg1;
855141240Snjl	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
856141240Snjl
857141240Snjl	/* Get settings from the device and generate the output string. */
858141240Snjl	count = CF_MAX_LEVELS;
859141240Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
860141240Snjl	if (levels == NULL)
861141240Snjl		return (ENOMEM);
862141240Snjl	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
863142395Snjl	if (error) {
864142395Snjl		if (error == E2BIG)
865142395Snjl			printf("cpufreq: need to increase CF_MAX_LEVELS\n");
866141240Snjl		goto out;
867142395Snjl	}
868141240Snjl	if (count) {
869141240Snjl		for (i = 0; i < count; i++) {
870141240Snjl			set = &levels[i].total_set;
871141240Snjl			sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
872141240Snjl		}
873141240Snjl	} else
874141240Snjl		sbuf_cpy(&sb, "0");
875141240Snjl	sbuf_trim(&sb);
876141240Snjl	sbuf_finish(&sb);
877141240Snjl	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
878141240Snjl
879141240Snjlout:
880141240Snjl	free(levels, M_TEMP);
881141240Snjl	sbuf_delete(&sb);
882141240Snjl	return (error);
883141240Snjl}
884141240Snjl
885142114Snjlstatic int
886142114Snjlcpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
887142114Snjl{
888142114Snjl	device_t dev;
889142114Snjl	struct cf_setting *sets;
890142114Snjl	struct sbuf sb;
891142114Snjl	int error, i, set_count;
892142114Snjl
893142114Snjl	dev = oidp->oid_arg1;
894142114Snjl	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
895142114Snjl
896142114Snjl	/* Get settings from the device and generate the output string. */
897142114Snjl	set_count = MAX_SETTINGS;
898142114Snjl	sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
899142114Snjl	if (sets == NULL)
900142114Snjl		return (ENOMEM);
901142114Snjl	error = CPUFREQ_DRV_SETTINGS(dev, sets, &set_count);
902142114Snjl	if (error)
903142114Snjl		goto out;
904142114Snjl	if (set_count) {
905142114Snjl		for (i = 0; i < set_count; i++)
906142114Snjl			sbuf_printf(&sb, "%d/%d ", sets[i].freq, sets[i].power);
907142114Snjl	} else
908142114Snjl		sbuf_cpy(&sb, "0");
909142114Snjl	sbuf_trim(&sb);
910142114Snjl	sbuf_finish(&sb);
911142114Snjl	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
912142114Snjl
913142114Snjlout:
914142114Snjl	free(sets, M_TEMP);
915142114Snjl	sbuf_delete(&sb);
916142114Snjl	return (error);
917142114Snjl}
918142114Snjl
919141240Snjlint
920141240Snjlcpufreq_register(device_t dev)
921141240Snjl{
922141945Snjl	struct cpufreq_softc *sc;
923141240Snjl	device_t cf_dev, cpu_dev;
924141240Snjl
925142114Snjl	/* Add a sysctl to get each driver's settings separately. */
926142114Snjl	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
927142114Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
928142114Snjl	    OID_AUTO, "freq_settings", CTLTYPE_STRING | CTLFLAG_RD, dev, 0,
929142114Snjl	    cpufreq_settings_sysctl, "A", "CPU frequency driver settings");
930142114Snjl
931141240Snjl	/*
932141814Snjl	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
933141814Snjl	 * must offer the same levels and be switched at the same time.
934141240Snjl	 */
935141814Snjl	cpu_dev = device_get_parent(dev);
936141945Snjl	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
937141945Snjl		sc = device_get_softc(cf_dev);
938141945Snjl		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
939141240Snjl		return (0);
940141945Snjl	}
941141240Snjl
942141814Snjl	/* Add the child device and possibly sysctls. */
943141814Snjl	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
944141240Snjl	if (cf_dev == NULL)
945141240Snjl		return (ENOMEM);
946141240Snjl	device_quiet(cf_dev);
947141240Snjl
948141240Snjl	return (device_probe_and_attach(cf_dev));
949141240Snjl}
950141240Snjl
951141240Snjlint
952141240Snjlcpufreq_unregister(device_t dev)
953141240Snjl{
954141240Snjl	device_t cf_dev, *devs;
955142032Snjl	int cfcount, devcount, error, i, type;
956141240Snjl
957141240Snjl	/*
958141240Snjl	 * If this is the last cpufreq child device, remove the control
959141240Snjl	 * device as well.  We identify cpufreq children by calling a method
960141240Snjl	 * they support.
961141240Snjl	 */
962141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &devcount);
963141240Snjl	if (error)
964141240Snjl		return (error);
965141945Snjl	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
966144413Snjl	if (cf_dev == NULL) {
967144413Snjl		device_printf(dev,
968144413Snjl	"warning: cpufreq_unregister called with no cpufreq device active\n");
969144413Snjl		return (0);
970144413Snjl	}
971141240Snjl	cfcount = 0;
972141240Snjl	for (i = 0; i < devcount; i++) {
973141240Snjl		if (!device_is_attached(devs[i]))
974141240Snjl			continue;
975142032Snjl		if (CPUFREQ_DRV_TYPE(devs[i], &type) == 0)
976141240Snjl			cfcount++;
977141240Snjl	}
978141814Snjl	if (cfcount <= 1)
979141240Snjl		device_delete_child(device_get_parent(cf_dev), cf_dev);
980141240Snjl	free(devs, M_TEMP);
981141240Snjl
982141240Snjl	return (0);
983141240Snjl}
984