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