kern_cpu.c revision 141945
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 141945 2005-02-15 07:43:48Z 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>
35141240Snjl#include <sys/malloc.h>
36141240Snjl#include <sys/module.h>
37141240Snjl#include <sys/proc.h>
38141240Snjl#include <sys/queue.h>
39141240Snjl#include <sys/sched.h>
40141240Snjl#include <sys/sysctl.h>
41141240Snjl#include <sys/systm.h>
42141240Snjl#include <sys/sbuf.h>
43141814Snjl#include <sys/timetc.h>
44141240Snjl
45141240Snjl#include "cpufreq_if.h"
46141240Snjl
47141240Snjl/*
48141240Snjl * Common CPU frequency glue code.  Drivers for specific hardware can
49141240Snjl * attach this interface to allow users to get/set the CPU frequency.
50141240Snjl */
51141240Snjl
52141240Snjl/*
53141240Snjl * Number of levels we can handle.  Levels are synthesized from settings
54141240Snjl * so for N settings there may be N^2 levels.
55141240Snjl */
56141240Snjl#define CF_MAX_LEVELS	32
57141240Snjl
58141240Snjlstruct cpufreq_softc {
59141240Snjl	struct cf_level			curr_level;
60141923Snjl	int				curr_priority;
61141923Snjl	struct cf_level			saved_level;
62141923Snjl	int				saved_priority;
63141923Snjl	struct cf_level_lst		all_levels;
64141413Snjl	int				all_count;
65141945Snjl	int				max_mhz;
66141240Snjl	device_t			dev;
67141240Snjl	struct sysctl_ctx_list		sysctl_ctx;
68141240Snjl};
69141240Snjl
70141240Snjlstruct cf_setting_array {
71141240Snjl	struct cf_setting		sets[MAX_SETTINGS];
72141240Snjl	int				count;
73141240Snjl	TAILQ_ENTRY(cf_setting_array)	link;
74141240Snjl};
75141240Snjl
76141240SnjlTAILQ_HEAD(cf_setting_lst, cf_setting_array);
77141240Snjl
78141240Snjlstatic int	cpufreq_attach(device_t dev);
79141240Snjlstatic int	cpufreq_detach(device_t dev);
80141240Snjlstatic void	cpufreq_evaluate(void *arg);
81141240Snjlstatic int	cf_set_method(device_t dev, const struct cf_level *level,
82141240Snjl		    int priority);
83141240Snjlstatic int	cf_get_method(device_t dev, struct cf_level *level);
84141240Snjlstatic int	cf_levels_method(device_t dev, struct cf_level *levels,
85141240Snjl		    int *count);
86141413Snjlstatic int	cpufreq_insert_abs(struct cpufreq_softc *sc,
87141240Snjl		    struct cf_setting *sets, int count);
88141413Snjlstatic int	cpufreq_expand_set(struct cpufreq_softc *sc,
89141413Snjl		    struct cf_setting_array *set_arr);
90141413Snjlstatic struct cf_level *cpufreq_dup_set(struct cpufreq_softc *sc,
91141413Snjl		    struct cf_level *dup, struct cf_setting *set);
92141240Snjlstatic int	cpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS);
93141240Snjlstatic int	cpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS);
94141240Snjl
95141240Snjlstatic device_method_t cpufreq_methods[] = {
96141240Snjl	DEVMETHOD(device_probe,		bus_generic_probe),
97141240Snjl	DEVMETHOD(device_attach,	cpufreq_attach),
98141240Snjl	DEVMETHOD(device_detach,	cpufreq_detach),
99141240Snjl
100141240Snjl        DEVMETHOD(cpufreq_set,		cf_set_method),
101141240Snjl        DEVMETHOD(cpufreq_get,		cf_get_method),
102141240Snjl        DEVMETHOD(cpufreq_levels,	cf_levels_method),
103141240Snjl	{0, 0}
104141240Snjl};
105141240Snjlstatic driver_t cpufreq_driver = {
106141240Snjl	"cpufreq", cpufreq_methods, sizeof(struct cpufreq_softc)
107141240Snjl};
108141240Snjlstatic devclass_t cpufreq_dc;
109141240SnjlDRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
110141240Snjl
111141240Snjlstatic eventhandler_tag cf_ev_tag;
112141240Snjl
113141240Snjlstatic int
114141240Snjlcpufreq_attach(device_t dev)
115141240Snjl{
116141240Snjl	struct cpufreq_softc *sc;
117141240Snjl	device_t parent;
118141240Snjl	int numdevs;
119141240Snjl
120141240Snjl	sc = device_get_softc(dev);
121141240Snjl	parent = device_get_parent(dev);
122141240Snjl	sc->dev = dev;
123141240Snjl	sysctl_ctx_init(&sc->sysctl_ctx);
124141240Snjl	TAILQ_INIT(&sc->all_levels);
125141240Snjl	sc->curr_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
126141923Snjl	sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
127141945Snjl	sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
128141240Snjl
129141240Snjl	/*
130141240Snjl	 * Only initialize one set of sysctls for all CPUs.  In the future,
131141240Snjl	 * if multiple CPUs can have different settings, we can move these
132141240Snjl	 * sysctls to be under every CPU instead of just the first one.
133141240Snjl	 */
134141240Snjl	numdevs = devclass_get_count(cpufreq_dc);
135141240Snjl	if (numdevs > 1)
136141240Snjl		return (0);
137141240Snjl
138141240Snjl	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
139141240Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
140141240Snjl	    OID_AUTO, "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
141141240Snjl	    cpufreq_curr_sysctl, "I", "Current CPU frequency");
142141240Snjl	SYSCTL_ADD_PROC(&sc->sysctl_ctx,
143141240Snjl	    SYSCTL_CHILDREN(device_get_sysctl_tree(parent)),
144141240Snjl	    OID_AUTO, "freq_levels", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
145141240Snjl	    cpufreq_levels_sysctl, "A", "CPU frequency levels");
146141240Snjl	cf_ev_tag = EVENTHANDLER_REGISTER(cpufreq_changed, cpufreq_evaluate,
147141240Snjl	    NULL, EVENTHANDLER_PRI_ANY);
148141240Snjl
149141240Snjl	return (0);
150141240Snjl}
151141240Snjl
152141240Snjlstatic int
153141240Snjlcpufreq_detach(device_t dev)
154141240Snjl{
155141240Snjl	struct cpufreq_softc *sc;
156141240Snjl	int numdevs;
157141240Snjl
158141240Snjl	sc = device_get_softc(dev);
159141240Snjl	sysctl_ctx_free(&sc->sysctl_ctx);
160141240Snjl
161141240Snjl	/* Only clean up these resources when the last device is detaching. */
162141240Snjl	numdevs = devclass_get_count(cpufreq_dc);
163141240Snjl	if (numdevs == 1)
164141240Snjl		EVENTHANDLER_DEREGISTER(cpufreq_changed, cf_ev_tag);
165141240Snjl
166141240Snjl	return (0);
167141240Snjl}
168141240Snjl
169141240Snjlstatic void
170141240Snjlcpufreq_evaluate(void *arg)
171141240Snjl{
172141240Snjl	/* TODO: Re-evaluate when notified of changes to drivers. */
173141240Snjl}
174141240Snjl
175141240Snjlstatic int
176141240Snjlcf_set_method(device_t dev, const struct cf_level *level, int priority)
177141240Snjl{
178141240Snjl	struct cpufreq_softc *sc;
179141240Snjl	const struct cf_setting *set;
180141814Snjl	struct pcpu *pc;
181141814Snjl	int cpu_id, error, i;
182141240Snjl
183141240Snjl	sc = device_get_softc(dev);
184141240Snjl
185141814Snjl	/*
186141814Snjl	 * Check that the TSC isn't being used as a timecounter.
187141814Snjl	 * If it is, then return EBUSY and refuse to change the
188141814Snjl	 * clock speed.
189141814Snjl	 */
190141814Snjl	if (strcmp(timecounter->tc_name, "TSC") == 0)
191141814Snjl		return (EBUSY);
192141814Snjl
193141923Snjl	/*
194141923Snjl	 * If the caller didn't specify a level and one is saved, prepare to
195141923Snjl	 * restore the saved level.  If none has been saved, return an error.
196141923Snjl	 * If they did specify one, but the requested level has a lower
197141923Snjl	 * priority, don't allow the new level right now.
198141923Snjl	 */
199141923Snjl	if (level == NULL) {
200141923Snjl		if (sc->saved_level.total_set.freq != CPUFREQ_VAL_UNKNOWN) {
201141923Snjl			level = &sc->saved_level;
202141923Snjl			priority = sc->saved_priority;
203141923Snjl		} else
204141923Snjl			return (ENXIO);
205141923Snjl	} else if (priority < sc->curr_priority)
206141923Snjl		return (EPERM);
207141923Snjl
208141240Snjl	/* If already at this level, just return. */
209141240Snjl	if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq))
210141240Snjl		return (0);
211141240Snjl
212141240Snjl	/* First, set the absolute frequency via its driver. */
213141240Snjl	set = &level->abs_set;
214141240Snjl	if (set->dev) {
215141240Snjl		if (!device_is_attached(set->dev)) {
216141240Snjl			error = ENXIO;
217141240Snjl			goto out;
218141240Snjl		}
219141943Snjl
220141943Snjl		/* Bind to the target CPU before switching, if necessary. */
221141943Snjl		cpu_id = PCPU_GET(cpuid);
222141943Snjl		pc = cpu_get_pcpu(set->dev);
223141943Snjl		if (cpu_id != pc->pc_cpuid) {
224141943Snjl			mtx_lock_spin(&sched_lock);
225141943Snjl			sched_bind(curthread, pc->pc_cpuid);
226141943Snjl			mtx_unlock_spin(&sched_lock);
227141943Snjl		}
228141240Snjl		error = CPUFREQ_DRV_SET(set->dev, set);
229141943Snjl		if (cpu_id != pc->pc_cpuid) {
230141943Snjl			mtx_lock_spin(&sched_lock);
231141943Snjl			sched_unbind(curthread);
232141943Snjl			mtx_unlock_spin(&sched_lock);
233141943Snjl		}
234141240Snjl		if (error) {
235141240Snjl			goto out;
236141240Snjl		}
237141240Snjl	}
238141240Snjl
239141413Snjl	/* Next, set any/all relative frequencies via their drivers. */
240141413Snjl	for (i = 0; i < level->rel_count; i++) {
241141413Snjl		set = &level->rel_set[i];
242141413Snjl		if (!device_is_attached(set->dev)) {
243141413Snjl			error = ENXIO;
244141413Snjl			goto out;
245141413Snjl		}
246141943Snjl
247141943Snjl		/* Bind to the target CPU before switching, if necessary. */
248141943Snjl		cpu_id = PCPU_GET(cpuid);
249141943Snjl		pc = cpu_get_pcpu(set->dev);
250141943Snjl		if (cpu_id != pc->pc_cpuid) {
251141943Snjl			mtx_lock_spin(&sched_lock);
252141943Snjl			sched_bind(curthread, pc->pc_cpuid);
253141943Snjl			mtx_unlock_spin(&sched_lock);
254141943Snjl		}
255141413Snjl		error = CPUFREQ_DRV_SET(set->dev, set);
256141943Snjl		if (cpu_id != pc->pc_cpuid) {
257141943Snjl			mtx_lock_spin(&sched_lock);
258141943Snjl			sched_unbind(curthread);
259141943Snjl			mtx_unlock_spin(&sched_lock);
260141943Snjl		}
261141413Snjl		if (error) {
262141413Snjl			/* XXX Back out any successful setting? */
263141413Snjl			goto out;
264141413Snjl		}
265141413Snjl	}
266141240Snjl
267141923Snjl	/* If we were restoring a saved state, reset it to "unused". */
268141923Snjl	if (level == &sc->saved_level) {
269141923Snjl		sc->saved_level.total_set.freq = CPUFREQ_VAL_UNKNOWN;
270141923Snjl		sc->saved_priority = 0;
271141923Snjl	}
272141923Snjl
273141923Snjl	/*
274141923Snjl	 * Before recording the current level, check if we're going to a
275141923Snjl	 * higher priority and have not saved a level yet.  If so, save the
276141923Snjl	 * previous level and priority.
277141923Snjl	 */
278141923Snjl	if (sc->curr_level.total_set.freq != CPUFREQ_VAL_UNKNOWN &&
279141923Snjl	    sc->saved_level.total_set.freq == CPUFREQ_VAL_UNKNOWN &&
280141923Snjl	    priority > sc->curr_priority) {
281141923Snjl		sc->saved_level = sc->curr_level;
282141923Snjl		sc->saved_priority = sc->curr_priority;
283141923Snjl	}
284141240Snjl	sc->curr_level = *level;
285141923Snjl	sc->curr_priority = priority;
286141240Snjl	error = 0;
287141240Snjl
288141240Snjlout:
289141240Snjl	if (error)
290141240Snjl		device_printf(set->dev, "set freq failed, err %d\n", error);
291141240Snjl	return (error);
292141240Snjl}
293141240Snjl
294141240Snjlstatic int
295141240Snjlcf_get_method(device_t dev, struct cf_level *level)
296141240Snjl{
297141240Snjl	struct cpufreq_softc *sc;
298141240Snjl	struct cf_level *levels;
299141240Snjl	struct cf_setting *curr_set, set;
300141240Snjl	struct pcpu *pc;
301141240Snjl	device_t *devs;
302141240Snjl	int count, error, i, numdevs;
303141240Snjl	uint64_t rate;
304141240Snjl
305141240Snjl	sc = device_get_softc(dev);
306141240Snjl	curr_set = &sc->curr_level.total_set;
307141240Snjl	levels = NULL;
308141240Snjl
309141240Snjl	/* If we already know the current frequency, we're done. */
310141240Snjl	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN)
311141240Snjl		goto out;
312141240Snjl
313141240Snjl	/*
314141240Snjl	 * We need to figure out the current level.  Loop through every
315141240Snjl	 * driver, getting the current setting.  Then, attempt to get a best
316141240Snjl	 * match of settings against each level.
317141240Snjl	 */
318141240Snjl	count = CF_MAX_LEVELS;
319141240Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
320141240Snjl	if (levels == NULL)
321141240Snjl		return (ENOMEM);
322141240Snjl	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
323141240Snjl	if (error)
324141240Snjl		goto out;
325141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
326141240Snjl	if (error)
327141240Snjl		goto out;
328141240Snjl	for (i = 0; i < numdevs && curr_set->freq == CPUFREQ_VAL_UNKNOWN; i++) {
329141240Snjl		if (!device_is_attached(devs[i]))
330141240Snjl			continue;
331141240Snjl		error = CPUFREQ_DRV_GET(devs[i], &set);
332141240Snjl		if (error)
333141240Snjl			continue;
334141240Snjl		for (i = 0; i < count; i++) {
335141413Snjl			if (CPUFREQ_CMP(set.freq, levels[i].total_set.freq)) {
336141240Snjl				sc->curr_level = levels[i];
337141240Snjl				break;
338141240Snjl			}
339141240Snjl		}
340141240Snjl	}
341141240Snjl	free(devs, M_TEMP);
342141240Snjl	if (curr_set->freq != CPUFREQ_VAL_UNKNOWN)
343141240Snjl		goto out;
344141240Snjl
345141240Snjl	/*
346141240Snjl	 * We couldn't find an exact match, so attempt to estimate and then
347141240Snjl	 * match against a level.
348141240Snjl	 */
349141240Snjl	pc = cpu_get_pcpu(dev);
350141240Snjl	if (pc == NULL) {
351141240Snjl		error = ENXIO;
352141240Snjl		goto out;
353141240Snjl	}
354141240Snjl	cpu_est_clockrate(pc->pc_cpuid, &rate);
355141240Snjl	rate /= 1000000;
356141240Snjl	for (i = 0; i < count; i++) {
357141240Snjl		if (CPUFREQ_CMP(rate, levels[i].total_set.freq)) {
358141240Snjl			sc->curr_level = levels[i];
359141240Snjl			break;
360141240Snjl		}
361141240Snjl	}
362141240Snjl
363141240Snjlout:
364141240Snjl	if (levels)
365141240Snjl		free(levels, M_TEMP);
366141240Snjl	*level = sc->curr_level;
367141240Snjl	return (0);
368141240Snjl}
369141240Snjl
370141240Snjlstatic int
371141240Snjlcf_levels_method(device_t dev, struct cf_level *levels, int *count)
372141240Snjl{
373141413Snjl	struct cf_setting_array *set_arr;
374141240Snjl	struct cf_setting_lst rel_sets;
375141240Snjl	struct cpufreq_softc *sc;
376141240Snjl	struct cf_level *lev;
377141240Snjl	struct cf_setting *sets;
378141240Snjl	struct pcpu *pc;
379141240Snjl	device_t *devs;
380141413Snjl	int error, i, numdevs, set_count, type;
381141240Snjl	uint64_t rate;
382141240Snjl
383141240Snjl	if (levels == NULL || count == NULL)
384141240Snjl		return (EINVAL);
385141240Snjl
386141240Snjl	TAILQ_INIT(&rel_sets);
387141240Snjl	sc = device_get_softc(dev);
388141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &numdevs);
389141240Snjl	if (error)
390141240Snjl		return (error);
391141240Snjl	sets = malloc(MAX_SETTINGS * sizeof(*sets), M_TEMP, M_NOWAIT);
392141240Snjl	if (sets == NULL) {
393141240Snjl		free(devs, M_TEMP);
394141240Snjl		return (ENOMEM);
395141240Snjl	}
396141240Snjl
397141240Snjl	/* Get settings from all cpufreq drivers. */
398141240Snjl	for (i = 0; i < numdevs; i++) {
399141824Snjl		/* Skip devices that aren't ready. */
400141240Snjl		if (!device_is_attached(devs[i]))
401141240Snjl			continue;
402141824Snjl
403141824Snjl		/*
404141824Snjl		 * Get settings, skipping drivers that offer no settings or
405141824Snjl		 * provide settings for informational purposes only.
406141824Snjl		 */
407141240Snjl		set_count = MAX_SETTINGS;
408141240Snjl		error = CPUFREQ_DRV_SETTINGS(devs[i], sets, &set_count, &type);
409141824Snjl		if (error || set_count == 0 || (type & CPUFREQ_FLAG_INFO_ONLY))
410141240Snjl			continue;
411141413Snjl
412141824Snjl		/* Add the settings to our absolute/relative lists. */
413141814Snjl		switch (type & CPUFREQ_TYPE_MASK) {
414141413Snjl		case CPUFREQ_TYPE_ABSOLUTE:
415141413Snjl			error = cpufreq_insert_abs(sc, sets, set_count);
416141413Snjl			break;
417141413Snjl		case CPUFREQ_TYPE_RELATIVE:
418141413Snjl			set_arr = malloc(sizeof(*set_arr), M_TEMP, M_NOWAIT);
419141413Snjl			if (set_arr == NULL) {
420141413Snjl				error = ENOMEM;
421141413Snjl				goto out;
422141413Snjl			}
423141413Snjl			bcopy(sets, set_arr->sets, set_count * sizeof(*sets));
424141413Snjl			set_arr->count = set_count;
425141413Snjl			TAILQ_INSERT_TAIL(&rel_sets, set_arr, link);
426141413Snjl			break;
427141413Snjl		default:
428141413Snjl			error = EINVAL;
429141413Snjl			break;
430141413Snjl		}
431141240Snjl		if (error)
432141240Snjl			goto out;
433141240Snjl	}
434141240Snjl
435141945Snjl	/*
436141945Snjl	 * If there are no absolute levels, create a fake one at 100%.  We
437141945Snjl	 * then cache the clockrate for later use as our base frequency.
438141945Snjl	 *
439141945Snjl	 * XXX This assumes that the first time through, if we only have
440141945Snjl	 * relative drivers, the CPU is currently running at 100%.
441141945Snjl	 */
442141240Snjl	if (TAILQ_EMPTY(&sc->all_levels)) {
443141945Snjl		if (sc->max_mhz == CPUFREQ_VAL_UNKNOWN) {
444141945Snjl			pc = cpu_get_pcpu(dev);
445141945Snjl			cpu_est_clockrate(pc->pc_cpuid, &rate);
446141945Snjl			sc->max_mhz = rate / 1000000;
447141240Snjl		}
448141945Snjl		memset(&sets[0], CPUFREQ_VAL_UNKNOWN, sizeof(*sets));
449141945Snjl		sets[0].freq = sc->max_mhz;
450141945Snjl		sets[0].dev = NULL;
451141413Snjl		error = cpufreq_insert_abs(sc, sets, 1);
452141240Snjl		if (error)
453141240Snjl			goto out;
454141240Snjl	}
455141240Snjl
456141413Snjl	/* Create a combined list of absolute + relative levels. */
457141413Snjl	TAILQ_FOREACH(set_arr, &rel_sets, link)
458141413Snjl		cpufreq_expand_set(sc, set_arr);
459141413Snjl
460141413Snjl	/* If the caller doesn't have enough space, return the actual count. */
461141413Snjl	if (sc->all_count > *count) {
462141413Snjl		*count = sc->all_count;
463141413Snjl		error = E2BIG;
464141413Snjl		goto out;
465141413Snjl	}
466141413Snjl
467141413Snjl	/* Finally, output the list of levels. */
468141240Snjl	i = 0;
469141240Snjl	TAILQ_FOREACH(lev, &sc->all_levels, link) {
470141240Snjl		levels[i] = *lev;
471141240Snjl		i++;
472141240Snjl	}
473141413Snjl	*count = sc->all_count;
474141240Snjl	error = 0;
475141240Snjl
476141240Snjlout:
477141240Snjl	/* Clear all levels since we regenerate them each time. */
478141240Snjl	while ((lev = TAILQ_FIRST(&sc->all_levels)) != NULL) {
479141240Snjl		TAILQ_REMOVE(&sc->all_levels, lev, link);
480141240Snjl		free(lev, M_TEMP);
481141240Snjl	}
482141413Snjl	while ((set_arr = TAILQ_FIRST(&rel_sets)) != NULL) {
483141413Snjl		TAILQ_REMOVE(&rel_sets, set_arr, link);
484141413Snjl		free(set_arr, M_TEMP);
485141413Snjl	}
486141413Snjl	sc->all_count = 0;
487141240Snjl	free(devs, M_TEMP);
488141240Snjl	free(sets, M_TEMP);
489141240Snjl	return (error);
490141240Snjl}
491141240Snjl
492141240Snjl/*
493141240Snjl * Create levels for an array of absolute settings and insert them in
494141240Snjl * sorted order in the specified list.
495141240Snjl */
496141240Snjlstatic int
497141413Snjlcpufreq_insert_abs(struct cpufreq_softc *sc, struct cf_setting *sets,
498141240Snjl    int count)
499141240Snjl{
500141413Snjl	struct cf_level_lst *list;
501141240Snjl	struct cf_level *level, *search;
502141240Snjl	int i;
503141240Snjl
504141413Snjl	list = &sc->all_levels;
505141240Snjl	for (i = 0; i < count; i++) {
506141240Snjl		level = malloc(sizeof(*level), M_TEMP, M_NOWAIT | M_ZERO);
507141240Snjl		if (level == NULL)
508141240Snjl			return (ENOMEM);
509141240Snjl		level->abs_set = sets[i];
510141413Snjl		level->total_set = sets[i];
511141413Snjl		level->total_set.dev = NULL;
512141413Snjl		sc->all_count++;
513141240Snjl
514141240Snjl		if (TAILQ_EMPTY(list)) {
515141240Snjl			TAILQ_INSERT_HEAD(list, level, link);
516141240Snjl			continue;
517141240Snjl		}
518141240Snjl
519141240Snjl		TAILQ_FOREACH_REVERSE(search, list, cf_level_lst, link) {
520141413Snjl			if (sets[i].freq <= search->total_set.freq) {
521141240Snjl				TAILQ_INSERT_AFTER(list, search, level, link);
522141240Snjl				break;
523141240Snjl			}
524141240Snjl		}
525141240Snjl	}
526141240Snjl	return (0);
527141240Snjl}
528141240Snjl
529141413Snjl/*
530141413Snjl * Expand a group of relative settings, creating derived levels from them.
531141413Snjl */
532141240Snjlstatic int
533141413Snjlcpufreq_expand_set(struct cpufreq_softc *sc, struct cf_setting_array *set_arr)
534141413Snjl{
535141413Snjl	struct cf_level *fill, *search;
536141413Snjl	struct cf_setting *set;
537141413Snjl	int i;
538141413Snjl
539141413Snjl	TAILQ_FOREACH(search, &sc->all_levels, link) {
540141413Snjl		/* Skip this level if we've already modified it. */
541141413Snjl		for (i = 0; i < search->rel_count; i++) {
542141413Snjl			if (search->rel_set[i].dev == set_arr->sets[0].dev)
543141413Snjl				break;
544141413Snjl		}
545141413Snjl		if (i != search->rel_count)
546141413Snjl			continue;
547141413Snjl
548141413Snjl		/* Add each setting to the level, duplicating if necessary. */
549141413Snjl		for (i = 0; i < set_arr->count; i++) {
550141413Snjl			set = &set_arr->sets[i];
551141413Snjl
552141413Snjl			/*
553141413Snjl			 * If this setting is less than 100%, split the level
554141413Snjl			 * into two and add this setting to the new level.
555141413Snjl			 */
556141413Snjl			fill = search;
557141413Snjl			if (set->freq < 10000)
558141413Snjl				fill = cpufreq_dup_set(sc, search, set);
559141413Snjl
560141413Snjl			/*
561141413Snjl			 * The new level was a duplicate of an existing level
562141413Snjl			 * so we freed it.  Go to the next setting.
563141413Snjl			 */
564141413Snjl			if (fill == NULL)
565141413Snjl				continue;
566141413Snjl
567141413Snjl			/* Add this setting to the existing or new level. */
568141413Snjl			KASSERT(fill->rel_count < MAX_SETTINGS,
569141413Snjl			    ("cpufreq: too many relative drivers (%d)",
570141413Snjl			    MAX_SETTINGS));
571141413Snjl			fill->rel_set[fill->rel_count] = *set;
572141413Snjl			fill->rel_count++;
573141413Snjl		}
574141413Snjl	}
575141413Snjl
576141413Snjl	return (0);
577141413Snjl}
578141413Snjl
579141413Snjlstatic struct cf_level *
580141413Snjlcpufreq_dup_set(struct cpufreq_softc *sc, struct cf_level *dup,
581141413Snjl    struct cf_setting *set)
582141413Snjl{
583141413Snjl	struct cf_level_lst *list;
584141413Snjl	struct cf_level *fill, *itr;
585141413Snjl	struct cf_setting *fill_set, *itr_set;
586141413Snjl	int i;
587141413Snjl
588141413Snjl	/*
589141413Snjl	 * Create a new level, copy it from the old one, and update the
590141413Snjl	 * total frequency and power by the percentage specified in the
591141413Snjl	 * relative setting.
592141413Snjl	 */
593141413Snjl	fill = malloc(sizeof(*fill), M_TEMP, M_NOWAIT);
594141413Snjl	if (fill == NULL)
595141413Snjl		return (NULL);
596141413Snjl	*fill = *dup;
597141413Snjl	fill_set = &fill->total_set;
598141413Snjl	fill_set->freq =
599141413Snjl	    ((uint64_t)fill_set->freq * set->freq) / 10000;
600141413Snjl	if (fill_set->power != CPUFREQ_VAL_UNKNOWN) {
601141413Snjl		fill_set->power = ((uint64_t)fill_set->power * set->freq)
602141413Snjl		    / 10000;
603141413Snjl	}
604141413Snjl	if (set->lat != CPUFREQ_VAL_UNKNOWN) {
605141413Snjl		if (fill_set->lat != CPUFREQ_VAL_UNKNOWN)
606141413Snjl			fill_set->lat += set->lat;
607141413Snjl		else
608141413Snjl			fill_set->lat = set->lat;
609141413Snjl	}
610141413Snjl
611141413Snjl	/*
612141413Snjl	 * If we copied an old level that we already modified (say, at 100%),
613141413Snjl	 * we need to remove that setting before adding this one.  Since we
614141413Snjl	 * process each setting array in order, we know any settings for this
615141413Snjl	 * driver will be found at the end.
616141413Snjl	 */
617141413Snjl	for (i = fill->rel_count; i != 0; i--) {
618141413Snjl		if (fill->rel_set[i - 1].dev != set->dev)
619141413Snjl			break;
620141413Snjl		fill->rel_count--;
621141413Snjl	}
622141413Snjl
623141413Snjl	/*
624141413Snjl	 * Insert the new level in sorted order.  If we find a duplicate,
625141413Snjl	 * free the new level.  We can do this since any existing level will
626141413Snjl	 * be guaranteed to have the same or less settings and thus consume
627141413Snjl	 * less power.  For example, a level with one absolute setting of
628141413Snjl	 * 800 Mhz uses less power than one composed of an absolute setting
629141413Snjl	 * of 1600 Mhz and a relative setting at 50%.
630141413Snjl	 */
631141413Snjl	list = &sc->all_levels;
632141413Snjl	if (TAILQ_EMPTY(list)) {
633141413Snjl		TAILQ_INSERT_HEAD(list, fill, link);
634141413Snjl	} else {
635141413Snjl		TAILQ_FOREACH_REVERSE(itr, list, cf_level_lst, link) {
636141413Snjl			itr_set = &itr->total_set;
637141413Snjl			if (CPUFREQ_CMP(fill_set->freq, itr_set->freq)) {
638141413Snjl				free(fill, M_TEMP);
639141413Snjl				fill = NULL;
640141413Snjl				break;
641141413Snjl			} else if (fill_set->freq < itr_set->freq) {
642141413Snjl				TAILQ_INSERT_AFTER(list, itr, fill, link);
643141413Snjl				sc->all_count++;
644141413Snjl				break;
645141413Snjl			}
646141413Snjl		}
647141413Snjl	}
648141413Snjl
649141413Snjl	return (fill);
650141413Snjl}
651141413Snjl
652141413Snjlstatic int
653141240Snjlcpufreq_curr_sysctl(SYSCTL_HANDLER_ARGS)
654141240Snjl{
655141240Snjl	struct cpufreq_softc *sc;
656141240Snjl	struct cf_level *levels;
657141814Snjl	int count, devcount, error, freq, i, n;
658141814Snjl	device_t *devs;
659141240Snjl
660141814Snjl	devs = NULL;
661141240Snjl	sc = oidp->oid_arg1;
662141814Snjl	levels = malloc(CF_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
663141240Snjl	if (levels == NULL)
664141240Snjl		return (ENOMEM);
665141240Snjl
666141240Snjl	error = CPUFREQ_GET(sc->dev, &levels[0]);
667141240Snjl	if (error)
668141240Snjl		goto out;
669141240Snjl	freq = levels[0].total_set.freq;
670141240Snjl	error = sysctl_handle_int(oidp, &freq, 0, req);
671141240Snjl	if (error != 0 || req->newptr == NULL)
672141240Snjl		goto out;
673141240Snjl
674141814Snjl	/*
675141814Snjl	 * While we only call cpufreq_get() on one device (assuming all
676141814Snjl	 * CPUs have equal levels), we call cpufreq_set() on all CPUs.
677141814Snjl	 * This is needed for some MP systems.
678141814Snjl	 */
679141814Snjl	error = devclass_get_devices(cpufreq_dc, &devs, &devcount);
680141240Snjl	if (error)
681141240Snjl		goto out;
682141814Snjl	for (n = 0; n < devcount; n++) {
683141814Snjl		count = CF_MAX_LEVELS;
684141814Snjl		error = CPUFREQ_LEVELS(devs[n], levels, &count);
685141814Snjl		if (error)
686141240Snjl			break;
687141814Snjl		for (i = 0; i < count; i++) {
688141814Snjl			if (CPUFREQ_CMP(levels[i].total_set.freq, freq)) {
689141814Snjl				error = CPUFREQ_SET(devs[n], &levels[i],
690141814Snjl				    CPUFREQ_PRIO_USER);
691141814Snjl				break;
692141814Snjl			}
693141240Snjl		}
694141814Snjl		if (i == count) {
695141814Snjl			error = EINVAL;
696141814Snjl			break;
697141814Snjl		}
698141240Snjl	}
699141240Snjl
700141240Snjlout:
701141814Snjl	if (devs)
702141814Snjl		free(devs, M_TEMP);
703141240Snjl	if (levels)
704141240Snjl		free(levels, M_TEMP);
705141240Snjl	return (error);
706141240Snjl}
707141240Snjl
708141240Snjlstatic int
709141240Snjlcpufreq_levels_sysctl(SYSCTL_HANDLER_ARGS)
710141240Snjl{
711141240Snjl	struct cpufreq_softc *sc;
712141240Snjl	struct cf_level *levels;
713141240Snjl	struct cf_setting *set;
714141240Snjl	struct sbuf sb;
715141240Snjl	int count, error, i;
716141240Snjl
717141240Snjl	sc = oidp->oid_arg1;
718141240Snjl	sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
719141240Snjl
720141240Snjl	/* Get settings from the device and generate the output string. */
721141240Snjl	count = CF_MAX_LEVELS;
722141240Snjl	levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
723141240Snjl	if (levels == NULL)
724141240Snjl		return (ENOMEM);
725141240Snjl	error = CPUFREQ_LEVELS(sc->dev, levels, &count);
726141240Snjl	if (error)
727141240Snjl		goto out;
728141240Snjl	if (count) {
729141240Snjl		for (i = 0; i < count; i++) {
730141240Snjl			set = &levels[i].total_set;
731141240Snjl			sbuf_printf(&sb, "%d/%d ", set->freq, set->power);
732141240Snjl		}
733141240Snjl	} else
734141240Snjl		sbuf_cpy(&sb, "0");
735141240Snjl	sbuf_trim(&sb);
736141240Snjl	sbuf_finish(&sb);
737141240Snjl	error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
738141240Snjl
739141240Snjlout:
740141240Snjl	free(levels, M_TEMP);
741141240Snjl	sbuf_delete(&sb);
742141240Snjl	return (error);
743141240Snjl}
744141240Snjl
745141240Snjlint
746141240Snjlcpufreq_register(device_t dev)
747141240Snjl{
748141945Snjl	struct cpufreq_softc *sc;
749141240Snjl	device_t cf_dev, cpu_dev;
750141240Snjl
751141240Snjl	/*
752141814Snjl	 * Add only one cpufreq device to each CPU.  Currently, all CPUs
753141814Snjl	 * must offer the same levels and be switched at the same time.
754141240Snjl	 */
755141814Snjl	cpu_dev = device_get_parent(dev);
756141945Snjl	if ((cf_dev = device_find_child(cpu_dev, "cpufreq", -1))) {
757141945Snjl		sc = device_get_softc(cf_dev);
758141945Snjl		sc->max_mhz = CPUFREQ_VAL_UNKNOWN;
759141240Snjl		return (0);
760141945Snjl	}
761141240Snjl
762141814Snjl	/* Add the child device and possibly sysctls. */
763141814Snjl	cf_dev = BUS_ADD_CHILD(cpu_dev, 0, "cpufreq", -1);
764141240Snjl	if (cf_dev == NULL)
765141240Snjl		return (ENOMEM);
766141240Snjl	device_quiet(cf_dev);
767141240Snjl
768141240Snjl	return (device_probe_and_attach(cf_dev));
769141240Snjl}
770141240Snjl
771141240Snjlint
772141240Snjlcpufreq_unregister(device_t dev)
773141240Snjl{
774141240Snjl	device_t cf_dev, *devs;
775141240Snjl	int cfcount, count, devcount, error, i, type;
776141240Snjl	struct cf_setting set;
777141240Snjl
778141240Snjl	/*
779141240Snjl	 * If this is the last cpufreq child device, remove the control
780141240Snjl	 * device as well.  We identify cpufreq children by calling a method
781141240Snjl	 * they support.
782141240Snjl	 */
783141240Snjl	error = device_get_children(device_get_parent(dev), &devs, &devcount);
784141240Snjl	if (error)
785141240Snjl		return (error);
786141945Snjl	cf_dev = device_find_child(device_get_parent(dev), "cpufreq", -1);
787141240Snjl	cfcount = 0;
788141240Snjl	for (i = 0; i < devcount; i++) {
789141240Snjl		if (!device_is_attached(devs[i]))
790141240Snjl			continue;
791141240Snjl		count = 1;
792141240Snjl		if (CPUFREQ_DRV_SETTINGS(devs[i], &set, &count, &type) == 0)
793141240Snjl			cfcount++;
794141240Snjl	}
795141814Snjl	if (cfcount <= 1)
796141240Snjl		device_delete_child(device_get_parent(cf_dev), cf_dev);
797141240Snjl	free(devs, M_TEMP);
798141240Snjl
799141240Snjl	return (0);
800141240Snjl}
801