acpi_perf.c revision 142032
1/*-
2 * Copyright (c) 2003-2005 Nate Lawson (SDG)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_perf.c 142032 2005-02-18 00:23:36Z njl $");
29
30#include "opt_acpi.h"
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/proc.h>
34#include <sys/sched.h>
35#include <sys/bus.h>
36#include <sys/cpu.h>
37#include <sys/power.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/sbuf.h>
41#include <sys/pcpu.h>
42
43#include <machine/bus_pio.h>
44#include <machine/bus.h>
45#include <machine/resource.h>
46#include <sys/rman.h>
47
48#include "acpi.h"
49#include <dev/acpica/acpivar.h>
50
51#include "cpufreq_if.h"
52
53/*
54 * Support for ACPI processor performance states (Px) according to
55 * section 8.3.3 of the ACPI 2.0c specification.
56 */
57
58struct acpi_px {
59	uint32_t	 core_freq;
60	uint32_t	 power;
61	uint32_t	 trans_lat;
62	uint32_t	 bm_lat;
63	uint32_t	 ctrl_val;
64	uint32_t	 sts_val;
65};
66
67#define MAX_PX_STATES	 16
68
69struct acpi_perf_softc {
70	device_t	 dev;
71	ACPI_HANDLE	 handle;
72	struct resource	*perf_ctrl;	/* Set new performance state. */
73	int		 perf_ctrl_type; /* Resource type for perf_ctrl. */
74	struct resource	*perf_status;	/* Check that transition succeeded. */
75	int		 perf_sts_type;	/* Resource type for perf_status. */
76	struct acpi_px	*px_states;	/* ACPI perf states. */
77	uint32_t	 px_count;	/* Total number of perf states. */
78	uint32_t	 px_max_avail;	/* Lowest index state available. */
79	int		 px_curr_state;	/* Active state index. */
80	int		 px_rid;
81	int		 info_only;	/* Can we set new states? */
82};
83
84#define PX_GET_REG(reg) 				\
85	(bus_space_read_4(rman_get_bustag((reg)), 	\
86	    rman_get_bushandle((reg)), 0))
87#define PX_SET_REG(reg, val)				\
88	(bus_space_write_4(rman_get_bustag((reg)), 	\
89	    rman_get_bushandle((reg)), 0, (val)))
90
91#define ACPI_NOTIFY_PERF_STATES		0x80	/* _PSS changed. */
92
93static void	acpi_perf_identify(driver_t *driver, device_t parent);
94static int	acpi_perf_probe(device_t dev);
95static int	acpi_perf_attach(device_t dev);
96static int	acpi_perf_detach(device_t dev);
97static int	acpi_perf_evaluate(device_t dev);
98static int	acpi_px_to_set(device_t dev, struct acpi_px *px,
99		    struct cf_setting *set);
100static void	acpi_px_available(struct acpi_perf_softc *sc);
101static void	acpi_px_startup(void *arg);
102static void	acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context);
103static int	acpi_px_settings(device_t dev, struct cf_setting *sets,
104		    int *count);
105static int	acpi_px_set(device_t dev, const struct cf_setting *set);
106static int	acpi_px_get(device_t dev, struct cf_setting *set);
107static int	acpi_px_type(device_t dev, int *type);
108
109static device_method_t acpi_perf_methods[] = {
110	/* Device interface */
111	DEVMETHOD(device_identify,	acpi_perf_identify),
112	DEVMETHOD(device_probe,		acpi_perf_probe),
113	DEVMETHOD(device_attach,	acpi_perf_attach),
114	DEVMETHOD(device_detach,	acpi_perf_detach),
115
116	/* cpufreq interface */
117	DEVMETHOD(cpufreq_drv_set,	acpi_px_set),
118	DEVMETHOD(cpufreq_drv_get,	acpi_px_get),
119	DEVMETHOD(cpufreq_drv_type,	acpi_px_type),
120	DEVMETHOD(cpufreq_drv_settings,	acpi_px_settings),
121	{0, 0}
122};
123
124static driver_t acpi_perf_driver = {
125	"acpi_perf",
126	acpi_perf_methods,
127	sizeof(struct acpi_perf_softc),
128};
129
130static devclass_t acpi_perf_devclass;
131DRIVER_MODULE(acpi_perf, cpu, acpi_perf_driver, acpi_perf_devclass, 0, 0);
132MODULE_DEPEND(acpi_perf, acpi, 1, 1, 1);
133
134MALLOC_DEFINE(M_ACPIPERF, "acpi_perf", "ACPI Performance states");
135
136static void
137acpi_perf_identify(driver_t *driver, device_t parent)
138{
139	ACPI_HANDLE handle;
140
141	/* Make sure we're not being doubly invoked. */
142	if (device_find_child(parent, "acpi_perf", -1) != NULL)
143		return;
144
145	/* Get the handle for the Processor object and check for perf states. */
146	handle = acpi_get_handle(parent);
147	if (handle == NULL)
148		return;
149	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PSS", NULL, NULL)))
150		return;
151	if (BUS_ADD_CHILD(parent, 0, "acpi_perf", -1) == NULL)
152		device_printf(parent, "add acpi_perf child failed\n");
153}
154
155static int
156acpi_perf_probe(device_t dev)
157{
158	ACPI_HANDLE handle;
159	ACPI_OBJECT *pkg;
160	struct resource *res;
161	ACPI_BUFFER buf;
162	int error, rid, type;
163
164	/*
165	 * Check the performance state registers.  If they are of type
166	 * "functional fixed hardware", we attach quietly since we will
167	 * only be providing information on settings to other drivers.
168	 */
169	error = ENXIO;
170	handle = acpi_get_handle(dev);
171	buf.Pointer = NULL;
172	buf.Length = ACPI_ALLOCATE_BUFFER;
173	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PCT", NULL, &buf)))
174		return (error);
175	pkg = (ACPI_OBJECT *)buf.Pointer;
176	if (ACPI_PKG_VALID(pkg, 2)) {
177		rid = 0;
178		error = acpi_PkgGas(dev, pkg, 0, &type, &rid, &res);
179		switch (error) {
180		case 0:
181			bus_release_resource(dev, type, rid, res);
182			device_set_desc(dev, "ACPI CPU Frequency Control");
183			break;
184		case EOPNOTSUPP:
185			device_quiet(dev);
186			error = 0;
187			break;
188		}
189	}
190	AcpiOsFree(buf.Pointer);
191
192	return (error);
193}
194
195static int
196acpi_perf_attach(device_t dev)
197{
198	struct acpi_perf_softc *sc;
199
200	sc = device_get_softc(dev);
201	sc->dev = dev;
202	sc->handle = acpi_get_handle(dev);
203	sc->px_max_avail = 0;
204	sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
205	if (acpi_perf_evaluate(dev) != 0)
206		return (ENXIO);
207	cpufreq_register(dev);
208	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_px_startup, NULL);
209
210	return (0);
211}
212
213static int
214acpi_perf_detach(device_t dev)
215{
216	/* TODO: teardown registers, remove notify handler. */
217	return (ENXIO);
218}
219
220/* Probe and setup any valid performance states (Px). */
221static int
222acpi_perf_evaluate(device_t dev)
223{
224	struct acpi_perf_softc *sc;
225	ACPI_BUFFER buf;
226	ACPI_OBJECT *pkg, *res;
227	ACPI_STATUS status;
228	int error, i, j;
229	uint32_t *p;
230
231	/* Get the control values and parameters for each state. */
232	error = ENXIO;
233	sc = device_get_softc(dev);
234	buf.Pointer = NULL;
235	buf.Length = ACPI_ALLOCATE_BUFFER;
236	status = AcpiEvaluateObject(sc->handle, "_PSS", NULL, &buf);
237	if (ACPI_FAILURE(status))
238		return (ENXIO);
239
240	pkg = (ACPI_OBJECT *)buf.Pointer;
241	if (!ACPI_PKG_VALID(pkg, 1)) {
242		device_printf(dev, "invalid top level _PSS package\n");
243		goto out;
244	}
245	sc->px_count = pkg->Package.Count;
246
247	sc->px_states = malloc(sc->px_count * sizeof(struct acpi_px),
248	    M_ACPIPERF, M_WAITOK | M_ZERO);
249	if (sc->px_states == NULL)
250		goto out;
251
252	/*
253	 * Each state is a package of {CoreFreq, Power, TransitionLatency,
254	 * BusMasterLatency, ControlVal, StatusVal}, sorted from highest
255	 * performance to lowest.
256	 */
257	for (i = 0; i < sc->px_count; i++) {
258		res = &pkg->Package.Elements[i];
259		if (!ACPI_PKG_VALID(res, 6)) {
260			device_printf(dev, "invalid _PSS package\n");
261			continue;
262		}
263		p = &sc->px_states[i].core_freq;
264		for (j = 0; j < 6; j++, p++)
265			acpi_PkgInt32(res, j, p);
266	}
267	AcpiOsFree(buf.Pointer);
268
269	/* Get the control and status registers (one of each). */
270	buf.Pointer = NULL;
271	buf.Length = ACPI_ALLOCATE_BUFFER;
272	status = AcpiEvaluateObject(sc->handle, "_PCT", NULL, &buf);
273	if (ACPI_FAILURE(status))
274		goto out;
275
276	/* Check the package of two registers, each a Buffer in GAS format. */
277	pkg = (ACPI_OBJECT *)buf.Pointer;
278	if (!ACPI_PKG_VALID(pkg, 2)) {
279		device_printf(dev, "invalid perf register package\n");
280		goto out;
281	}
282
283	error = acpi_PkgGas(sc->dev, pkg, 0, &sc->perf_ctrl_type, &sc->px_rid,
284	    &sc->perf_ctrl);
285	if (error) {
286		/*
287		 * If the register is of type FFixedHW, we can only return
288		 * info, we can't get or set new settings.
289		 */
290		if (error == EOPNOTSUPP) {
291			sc->info_only = TRUE;
292			error = 0;
293		} else
294			device_printf(dev, "failed in PERF_CTL attach\n");
295		goto out;
296	}
297	sc->px_rid++;
298
299	error = acpi_PkgGas(sc->dev, pkg, 1, &sc->perf_sts_type, &sc->px_rid,
300	    &sc->perf_status);
301	if (error) {
302		if (error == EOPNOTSUPP) {
303			sc->info_only = TRUE;
304			error = 0;
305		} else
306			device_printf(dev, "failed in PERF_STATUS attach\n");
307		goto out;
308	}
309	sc->px_rid++;
310
311	/* Get our current limit and register for notifies. */
312	acpi_px_available(sc);
313	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
314	    acpi_px_notify, sc);
315	error = 0;
316
317out:
318	if (error) {
319		if (sc->px_states)
320			free(sc->px_states, M_ACPIPERF);
321		sc->px_count = 0;
322	}
323	if (buf.Pointer)
324		AcpiOsFree(buf.Pointer);
325	return (error);
326}
327
328static void
329acpi_px_startup(void *arg)
330{
331
332	/* Signal to the platform that we are taking over CPU control. */
333	if (AcpiGbl_FADT->PstateCnt == 0)
334		return;
335	ACPI_LOCK(acpi);
336	AcpiOsWritePort(AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->PstateCnt, 8);
337	ACPI_UNLOCK(acpi);
338}
339
340static void
341acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context)
342{
343	struct acpi_perf_softc *sc;
344
345	sc = context;
346	if (notify != ACPI_NOTIFY_PERF_STATES)
347		return;
348
349	acpi_px_available(sc);
350
351	/* TODO: Implement notification when frequency changes. */
352}
353
354/*
355 * Find the highest currently-supported performance state.
356 * This can be called at runtime (e.g., due to a docking event) at
357 * the request of a Notify on the processor object.
358 */
359static void
360acpi_px_available(struct acpi_perf_softc *sc)
361{
362	ACPI_STATUS status;
363	struct cf_setting set;
364
365	status = acpi_GetInteger(sc->handle, "_PPC", &sc->px_max_avail);
366
367	/* If the old state is too high, set current state to the new max. */
368	if (ACPI_SUCCESS(status)) {
369		if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN &&
370		    sc->px_curr_state > sc->px_max_avail) {
371			acpi_px_to_set(sc->dev,
372			    &sc->px_states[sc->px_max_avail], &set);
373			acpi_px_set(sc->dev, &set);
374		}
375	} else
376		sc->px_max_avail = 0;
377}
378
379static int
380acpi_px_to_set(device_t dev, struct acpi_px *px, struct cf_setting *set)
381{
382
383	if (px == NULL || set == NULL)
384		return (EINVAL);
385
386	set->freq = px->core_freq;
387	set->power = px->power;
388	/* XXX Include BM latency too? */
389	set->lat = px->trans_lat;
390	set->volts = CPUFREQ_VAL_UNKNOWN;
391	set->dev = dev;
392
393	return (0);
394}
395
396static int
397acpi_px_settings(device_t dev, struct cf_setting *sets, int *count)
398{
399	struct acpi_perf_softc *sc;
400	int x, y;
401
402	sc = device_get_softc(dev);
403	if (sets == NULL || count == NULL)
404		return (EINVAL);
405	if (*count < sc->px_count - sc->px_max_avail)
406		return (E2BIG);
407
408	/* Return a list of settings that are currently valid. */
409	y = 0;
410	for (x = sc->px_max_avail; x < sc->px_count; x++, y++)
411		acpi_px_to_set(dev, &sc->px_states[x], &sets[y]);
412	*count = sc->px_count - sc->px_max_avail;
413
414	return (0);
415}
416
417static int
418acpi_px_set(device_t dev, const struct cf_setting *set)
419{
420	struct acpi_perf_softc *sc;
421	int i, status, sts_val, tries;
422
423	if (set == NULL)
424		return (EINVAL);
425	sc = device_get_softc(dev);
426
427	/* If we can't set new states, return immediately. */
428	if (sc->info_only)
429		return (ENXIO);
430
431	/* Look up appropriate state, based on frequency. */
432	for (i = sc->px_max_avail; i < sc->px_count; i++) {
433		if (CPUFREQ_CMP(set->freq, sc->px_states[i].core_freq))
434			break;
435	}
436	if (i == sc->px_count)
437		return (EINVAL);
438
439	/* Write the appropriate value to the register. */
440	PX_SET_REG(sc->perf_ctrl, sc->px_states[i].ctrl_val);
441
442	/* Try for up to 1 ms to verify the desired state was selected. */
443	sts_val = sc->px_states[i].sts_val;
444	for (tries = 0; tries < 100; tries++) {
445		status = PX_GET_REG(sc->perf_status);
446		if (status == sts_val)
447			break;
448		DELAY(10);
449	}
450	if (tries == 100) {
451		device_printf(dev, "Px transition to %d failed\n",
452		    sc->px_states[i].core_freq);
453		return (ENXIO);
454	}
455	sc->px_curr_state = i;
456
457	return (0);
458}
459
460static int
461acpi_px_get(device_t dev, struct cf_setting *set)
462{
463	struct acpi_perf_softc *sc;
464	uint64_t rate;
465	int i;
466	struct pcpu *pc;
467
468	if (set == NULL)
469		return (EINVAL);
470	sc = device_get_softc(dev);
471
472	/* If we can't get new states, return immediately. */
473	if (sc->info_only)
474		return (ENXIO);
475
476	/* If we've set the rate before, use the cached value. */
477	if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN) {
478		acpi_px_to_set(dev, &sc->px_states[sc->px_curr_state], set);
479		return (0);
480	}
481
482	/* Otherwise, estimate and try to match against our settings. */
483	pc = cpu_get_pcpu(dev);
484	if (pc == NULL)
485		return (ENXIO);
486	cpu_est_clockrate(pc->pc_cpuid, &rate);
487	rate /= 1000000;
488	for (i = 0; i < sc->px_count; i++) {
489		if (CPUFREQ_CMP(sc->px_states[i].core_freq, rate)) {
490			sc->px_curr_state = i;
491			acpi_px_to_set(dev, &sc->px_states[i], set);
492			break;
493		}
494	}
495
496	/* No match, give up. */
497	if (i == sc->px_count) {
498		sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
499		set->freq = CPUFREQ_VAL_UNKNOWN;
500	}
501
502	return (0);
503}
504
505static int
506acpi_px_type(device_t dev, int *type)
507{
508	struct acpi_perf_softc *sc;
509
510	if (type == NULL)
511		return (EINVAL);
512	sc = device_get_softc(dev);
513
514	*type = CPUFREQ_TYPE_ABSOLUTE;
515	if (sc->info_only)
516		*type |= CPUFREQ_FLAG_INFO_ONLY;
517	return (0);
518}
519