acpi_perf.c revision 144684
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 144684 2005-04-05 19:39:44Z jhb $");
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/* Offsets in struct cf_setting array for storing driver-specific values. */
68#define PX_SPEC_CONTROL	0
69#define PX_SPEC_STATUS	1
70
71#define MAX_PX_STATES	16
72
73struct acpi_perf_softc {
74	device_t	 dev;
75	ACPI_HANDLE	 handle;
76	struct resource	*perf_ctrl;	/* Set new performance state. */
77	int		 perf_ctrl_type; /* Resource type for perf_ctrl. */
78	struct resource	*perf_status;	/* Check that transition succeeded. */
79	int		 perf_sts_type;	/* Resource type for perf_status. */
80	struct acpi_px	*px_states;	/* ACPI perf states. */
81	uint32_t	 px_count;	/* Total number of perf states. */
82	uint32_t	 px_max_avail;	/* Lowest index state available. */
83	int		 px_curr_state;	/* Active state index. */
84	int		 px_rid;
85	int		 info_only;	/* Can we set new states? */
86};
87
88#define PX_GET_REG(reg) 				\
89	(bus_space_read_4(rman_get_bustag((reg)), 	\
90	    rman_get_bushandle((reg)), 0))
91#define PX_SET_REG(reg, val)				\
92	(bus_space_write_4(rman_get_bustag((reg)), 	\
93	    rman_get_bushandle((reg)), 0, (val)))
94
95#define ACPI_NOTIFY_PERF_STATES		0x80	/* _PSS changed. */
96
97static void	acpi_perf_identify(driver_t *driver, device_t parent);
98static int	acpi_perf_probe(device_t dev);
99static int	acpi_perf_attach(device_t dev);
100static int	acpi_perf_detach(device_t dev);
101static int	acpi_perf_evaluate(device_t dev);
102static int	acpi_px_to_set(device_t dev, struct acpi_px *px,
103		    struct cf_setting *set);
104static void	acpi_px_available(struct acpi_perf_softc *sc);
105static void	acpi_px_startup(void *arg);
106static void	acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context);
107static int	acpi_px_settings(device_t dev, struct cf_setting *sets,
108		    int *count);
109static int	acpi_px_set(device_t dev, const struct cf_setting *set);
110static int	acpi_px_get(device_t dev, struct cf_setting *set);
111static int	acpi_px_type(device_t dev, int *type);
112
113static device_method_t acpi_perf_methods[] = {
114	/* Device interface */
115	DEVMETHOD(device_identify,	acpi_perf_identify),
116	DEVMETHOD(device_probe,		acpi_perf_probe),
117	DEVMETHOD(device_attach,	acpi_perf_attach),
118	DEVMETHOD(device_detach,	acpi_perf_detach),
119
120	/* cpufreq interface */
121	DEVMETHOD(cpufreq_drv_set,	acpi_px_set),
122	DEVMETHOD(cpufreq_drv_get,	acpi_px_get),
123	DEVMETHOD(cpufreq_drv_type,	acpi_px_type),
124	DEVMETHOD(cpufreq_drv_settings,	acpi_px_settings),
125	{0, 0}
126};
127
128static driver_t acpi_perf_driver = {
129	"acpi_perf",
130	acpi_perf_methods,
131	sizeof(struct acpi_perf_softc),
132};
133
134static devclass_t acpi_perf_devclass;
135DRIVER_MODULE(acpi_perf, cpu, acpi_perf_driver, acpi_perf_devclass, 0, 0);
136MODULE_DEPEND(acpi_perf, acpi, 1, 1, 1);
137
138MALLOC_DEFINE(M_ACPIPERF, "acpi_perf", "ACPI Performance states");
139
140static void
141acpi_perf_identify(driver_t *driver, device_t parent)
142{
143	ACPI_HANDLE handle;
144	device_t dev;
145
146	/* Make sure we're not being doubly invoked. */
147	if (device_find_child(parent, "acpi_perf", -1) != NULL)
148		return;
149
150	/* Get the handle for the Processor object and check for perf states. */
151	handle = acpi_get_handle(parent);
152	if (handle == NULL)
153		return;
154	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PSS", NULL, NULL)))
155		return;
156
157	/*
158	 * Add a child to every CPU that has the right methods.  In future
159	 * versions of the ACPI spec, CPUs can have different settings.
160	 * We probe this child now so that other devices that depend
161	 * on it (i.e., for info about supported states) will see it.
162	 */
163	if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_perf", -1)) != NULL)
164		device_probe_and_attach(dev);
165	else
166		device_printf(parent, "add acpi_perf child failed\n");
167}
168
169static int
170acpi_perf_probe(device_t dev)
171{
172	ACPI_HANDLE handle;
173	ACPI_OBJECT *pkg;
174	struct resource *res;
175	ACPI_BUFFER buf;
176	int error, rid, type;
177
178	if (resource_disabled("acpi_perf", 0))
179		return (ENXIO);
180
181	/*
182	 * Check the performance state registers.  If they are of type
183	 * "functional fixed hardware", we attach quietly since we will
184	 * only be providing information on settings to other drivers.
185	 */
186	error = ENXIO;
187	handle = acpi_get_handle(dev);
188	buf.Pointer = NULL;
189	buf.Length = ACPI_ALLOCATE_BUFFER;
190	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PCT", NULL, &buf)))
191		return (error);
192	pkg = (ACPI_OBJECT *)buf.Pointer;
193	if (ACPI_PKG_VALID(pkg, 2)) {
194		rid = 0;
195		error = acpi_PkgGas(dev, pkg, 0, &type, &rid, &res);
196		switch (error) {
197		case 0:
198			bus_release_resource(dev, type, rid, res);
199			bus_delete_resource(dev, type, rid);
200			device_set_desc(dev, "ACPI CPU Frequency Control");
201			break;
202		case EOPNOTSUPP:
203			device_quiet(dev);
204			error = 0;
205			break;
206		}
207	}
208	AcpiOsFree(buf.Pointer);
209
210	return (error);
211}
212
213static int
214acpi_perf_attach(device_t dev)
215{
216	struct acpi_perf_softc *sc;
217
218	sc = device_get_softc(dev);
219	sc->dev = dev;
220	sc->handle = acpi_get_handle(dev);
221	sc->px_max_avail = 0;
222	sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
223	if (acpi_perf_evaluate(dev) != 0)
224		return (ENXIO);
225	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_px_startup, NULL);
226	if (!sc->info_only)
227		cpufreq_register(dev);
228
229	return (0);
230}
231
232static int
233acpi_perf_detach(device_t dev)
234{
235	/* TODO: teardown registers, remove notify handler. */
236	return (ENXIO);
237}
238
239/* Probe and setup any valid performance states (Px). */
240static int
241acpi_perf_evaluate(device_t dev)
242{
243	struct acpi_perf_softc *sc;
244	ACPI_BUFFER buf;
245	ACPI_OBJECT *pkg, *res;
246	ACPI_STATUS status;
247	int count, error, i, j;
248	uint32_t *p;
249
250	/* Get the control values and parameters for each state. */
251	error = ENXIO;
252	sc = device_get_softc(dev);
253	buf.Pointer = NULL;
254	buf.Length = ACPI_ALLOCATE_BUFFER;
255	status = AcpiEvaluateObject(sc->handle, "_PSS", NULL, &buf);
256	if (ACPI_FAILURE(status))
257		return (ENXIO);
258
259	pkg = (ACPI_OBJECT *)buf.Pointer;
260	if (!ACPI_PKG_VALID(pkg, 1)) {
261		device_printf(dev, "invalid top level _PSS package\n");
262		goto out;
263	}
264	sc->px_count = pkg->Package.Count;
265
266	sc->px_states = malloc(sc->px_count * sizeof(struct acpi_px),
267	    M_ACPIPERF, M_WAITOK | M_ZERO);
268	if (sc->px_states == NULL)
269		goto out;
270
271	/*
272	 * Each state is a package of {CoreFreq, Power, TransitionLatency,
273	 * BusMasterLatency, ControlVal, StatusVal}, sorted from highest
274	 * performance to lowest.
275	 */
276	count = 0;
277	for (i = 0; i < sc->px_count; i++) {
278		res = &pkg->Package.Elements[i];
279		if (!ACPI_PKG_VALID(res, 6)) {
280			device_printf(dev, "invalid _PSS package\n");
281			continue;
282		}
283
284		/* Parse the rest of the package into the struct. */
285		p = &sc->px_states[count].core_freq;
286		for (j = 0; j < 6; j++, p++)
287			acpi_PkgInt32(res, j, p);
288
289		/*
290		 * Check for some impossible frequencies that some systems
291		 * use to indicate they don't actually support this Px state.
292		 */
293		if (sc->px_states[count].core_freq == 0 ||
294		    sc->px_states[count].core_freq == 9999 ||
295		    sc->px_states[count].core_freq == 0x9999 ||
296		    sc->px_states[count].core_freq >= 0xffff)
297			continue;
298
299		count++;
300	}
301
302	/* No valid Px state found. */
303	if (count == 0)
304		goto out;
305	AcpiOsFree(buf.Pointer);
306	sc->px_count = count;
307
308	/* Get the control and status registers (one of each). */
309	buf.Pointer = NULL;
310	buf.Length = ACPI_ALLOCATE_BUFFER;
311	status = AcpiEvaluateObject(sc->handle, "_PCT", NULL, &buf);
312	if (ACPI_FAILURE(status))
313		goto out;
314
315	/* Check the package of two registers, each a Buffer in GAS format. */
316	pkg = (ACPI_OBJECT *)buf.Pointer;
317	if (!ACPI_PKG_VALID(pkg, 2)) {
318		device_printf(dev, "invalid perf register package\n");
319		goto out;
320	}
321
322	error = acpi_PkgGas(sc->dev, pkg, 0, &sc->perf_ctrl_type, &sc->px_rid,
323	    &sc->perf_ctrl);
324	if (error) {
325		/*
326		 * If the register is of type FFixedHW, we can only return
327		 * info, we can't get or set new settings.
328		 */
329		if (error == EOPNOTSUPP) {
330			sc->info_only = TRUE;
331			error = 0;
332		} else
333			device_printf(dev, "failed in PERF_CTL attach\n");
334		goto out;
335	}
336	sc->px_rid++;
337
338	error = acpi_PkgGas(sc->dev, pkg, 1, &sc->perf_sts_type, &sc->px_rid,
339	    &sc->perf_status);
340	if (error) {
341		if (error == EOPNOTSUPP) {
342			sc->info_only = TRUE;
343			error = 0;
344		} else
345			device_printf(dev, "failed in PERF_STATUS attach\n");
346		goto out;
347	}
348	sc->px_rid++;
349
350	/* Get our current limit and register for notifies. */
351	acpi_px_available(sc);
352	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
353	    acpi_px_notify, sc);
354	error = 0;
355
356out:
357	if (error) {
358		if (sc->px_states) {
359			free(sc->px_states, M_ACPIPERF);
360			sc->px_states = NULL;
361		}
362		if (sc->perf_ctrl) {
363			bus_release_resource(sc->dev, sc->perf_ctrl_type, 0,
364			    sc->perf_ctrl);
365			bus_delete_resource(sc->dev, sc->perf_ctrl_type, 0);
366			sc->perf_ctrl = NULL;
367		}
368		if (sc->perf_status) {
369			bus_release_resource(sc->dev, sc->perf_sts_type, 1,
370			    sc->perf_status);
371			bus_delete_resource(sc->dev, sc->perf_sts_type, 1);
372			sc->perf_status = NULL;
373		}
374		sc->px_rid = 0;
375		sc->px_count = 0;
376	}
377	if (buf.Pointer)
378		AcpiOsFree(buf.Pointer);
379	return (error);
380}
381
382static void
383acpi_px_startup(void *arg)
384{
385
386	/* Signal to the platform that we are taking over CPU control. */
387	if (AcpiGbl_FADT->PstateCnt == 0)
388		return;
389	ACPI_LOCK(acpi);
390	AcpiOsWritePort(AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->PstateCnt, 8);
391	ACPI_UNLOCK(acpi);
392}
393
394static void
395acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context)
396{
397	struct acpi_perf_softc *sc;
398
399	sc = context;
400	if (notify != ACPI_NOTIFY_PERF_STATES)
401		return;
402
403	acpi_px_available(sc);
404
405	/* TODO: Implement notification when frequency changes. */
406}
407
408/*
409 * Find the highest currently-supported performance state.
410 * This can be called at runtime (e.g., due to a docking event) at
411 * the request of a Notify on the processor object.
412 */
413static void
414acpi_px_available(struct acpi_perf_softc *sc)
415{
416	ACPI_STATUS status;
417	struct cf_setting set;
418
419	status = acpi_GetInteger(sc->handle, "_PPC", &sc->px_max_avail);
420
421	/* If the old state is too high, set current state to the new max. */
422	if (ACPI_SUCCESS(status)) {
423		if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN &&
424		    sc->px_curr_state > sc->px_max_avail) {
425			acpi_px_to_set(sc->dev,
426			    &sc->px_states[sc->px_max_avail], &set);
427			acpi_px_set(sc->dev, &set);
428		}
429	} else
430		sc->px_max_avail = 0;
431}
432
433static int
434acpi_px_to_set(device_t dev, struct acpi_px *px, struct cf_setting *set)
435{
436
437	if (px == NULL || set == NULL)
438		return (EINVAL);
439
440	set->freq = px->core_freq;
441	set->power = px->power;
442	/* XXX Include BM latency too? */
443	set->lat = px->trans_lat;
444	set->volts = CPUFREQ_VAL_UNKNOWN;
445	set->dev = dev;
446	set->spec[PX_SPEC_CONTROL] = px->ctrl_val;
447	set->spec[PX_SPEC_STATUS] = px->sts_val;
448
449	return (0);
450}
451
452static int
453acpi_px_settings(device_t dev, struct cf_setting *sets, int *count)
454{
455	struct acpi_perf_softc *sc;
456	int x, y;
457
458	sc = device_get_softc(dev);
459	if (sets == NULL || count == NULL)
460		return (EINVAL);
461	if (*count < sc->px_count - sc->px_max_avail)
462		return (E2BIG);
463
464	/* Return a list of settings that are currently valid. */
465	y = 0;
466	for (x = sc->px_max_avail; x < sc->px_count; x++, y++)
467		acpi_px_to_set(dev, &sc->px_states[x], &sets[y]);
468	*count = sc->px_count - sc->px_max_avail;
469
470	return (0);
471}
472
473static int
474acpi_px_set(device_t dev, const struct cf_setting *set)
475{
476	struct acpi_perf_softc *sc;
477	int i, status, sts_val, tries;
478
479	if (set == NULL)
480		return (EINVAL);
481	sc = device_get_softc(dev);
482
483	/* If we can't set new states, return immediately. */
484	if (sc->info_only)
485		return (ENXIO);
486
487	/* Look up appropriate state, based on frequency. */
488	for (i = sc->px_max_avail; i < sc->px_count; i++) {
489		if (CPUFREQ_CMP(set->freq, sc->px_states[i].core_freq))
490			break;
491	}
492	if (i == sc->px_count)
493		return (EINVAL);
494
495	/* Write the appropriate value to the register. */
496	PX_SET_REG(sc->perf_ctrl, sc->px_states[i].ctrl_val);
497
498	/*
499	 * Try for up to 10 ms to verify the desired state was selected.
500	 * This is longer than the standard says (1 ms) but in some modes,
501	 * systems may take longer to respond.
502	 */
503	sts_val = sc->px_states[i].sts_val;
504	for (tries = 0; tries < 1000; tries++) {
505		status = PX_GET_REG(sc->perf_status);
506
507		/*
508		 * If we match the status or the desired status is 8 bits
509		 * and matches the relevant bits, assume we succeeded.  It
510		 * appears some systems (IBM R32) expect byte-wide access
511		 * even though the standard says the register is 32-bit.
512		 */
513		if (status == sts_val ||
514		    ((sts_val & ~0xff) == 0 && (status & 0xff) == sts_val))
515			break;
516		DELAY(10);
517	}
518	if (tries == 1000) {
519		device_printf(dev, "Px transition to %d failed\n",
520		    sc->px_states[i].core_freq);
521		return (ENXIO);
522	}
523	sc->px_curr_state = i;
524
525	return (0);
526}
527
528static int
529acpi_px_get(device_t dev, struct cf_setting *set)
530{
531	struct acpi_perf_softc *sc;
532	uint64_t rate;
533	int i;
534	struct pcpu *pc;
535
536	if (set == NULL)
537		return (EINVAL);
538	sc = device_get_softc(dev);
539
540	/* If we can't get new states, return immediately. */
541	if (sc->info_only)
542		return (ENXIO);
543
544	/* If we've set the rate before, use the cached value. */
545	if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN) {
546		acpi_px_to_set(dev, &sc->px_states[sc->px_curr_state], set);
547		return (0);
548	}
549
550	/* Otherwise, estimate and try to match against our settings. */
551	pc = cpu_get_pcpu(dev);
552	if (pc == NULL)
553		return (ENXIO);
554	cpu_est_clockrate(pc->pc_cpuid, &rate);
555	rate /= 1000000;
556	for (i = 0; i < sc->px_count; i++) {
557		if (CPUFREQ_CMP(sc->px_states[i].core_freq, rate)) {
558			sc->px_curr_state = i;
559			acpi_px_to_set(dev, &sc->px_states[i], set);
560			break;
561		}
562	}
563
564	/* No match, give up. */
565	if (i == sc->px_count) {
566		sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
567		set->freq = CPUFREQ_VAL_UNKNOWN;
568	}
569
570	return (0);
571}
572
573static int
574acpi_px_type(device_t dev, int *type)
575{
576	struct acpi_perf_softc *sc;
577
578	if (type == NULL)
579		return (EINVAL);
580	sc = device_get_softc(dev);
581
582	*type = CPUFREQ_TYPE_ABSOLUTE;
583	if (sc->info_only)
584		*type |= CPUFREQ_FLAG_INFO_ONLY;
585	return (0);
586}
587