xref: /freebsd/sys/dev/acpica/acpi_perf.c (revision 6b806d21d144c25f4fad714e1c0cf780f5e27d7e)
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$");
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 x of the ACPI specification.
56  */
57 
58 struct 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 
69 struct 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 };
82 
83 #define PX_GET_REG(reg) 				\
84 	(bus_space_read_4(rman_get_bustag((reg)), 	\
85 	    rman_get_bushandle((reg)), 0))
86 #define PX_SET_REG(reg, val)				\
87 	(bus_space_write_4(rman_get_bustag((reg)), 	\
88 	    rman_get_bushandle((reg)), 0, (val)))
89 
90 #define ACPI_NOTIFY_PERF_STATES		0x80	/* _PSS changed. */
91 
92 static void	acpi_perf_identify(driver_t *driver, device_t parent);
93 static int	acpi_perf_probe(device_t dev);
94 static int	acpi_perf_attach(device_t dev);
95 static int	acpi_perf_detach(device_t dev);
96 static int	acpi_perf_evaluate(device_t dev);
97 static int	acpi_px_to_set(device_t dev, struct acpi_px *px,
98 		    struct cf_setting *set);
99 static void	acpi_px_available(struct acpi_perf_softc *sc);
100 static void	acpi_px_startup(void *arg);
101 static void	acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context);
102 static int	acpi_px_settings(device_t dev, struct cf_setting *sets,
103 		    int *count, int *type);
104 static int	acpi_px_set(device_t dev, const struct cf_setting *set);
105 static int	acpi_px_get(device_t dev, struct cf_setting *set);
106 
107 static device_method_t acpi_perf_methods[] = {
108 	/* Device interface */
109 	DEVMETHOD(device_identify,	acpi_perf_identify),
110 	DEVMETHOD(device_probe,		acpi_perf_probe),
111 	DEVMETHOD(device_attach,	acpi_perf_attach),
112 	DEVMETHOD(device_detach,	acpi_perf_detach),
113 
114 	/* cpufreq interface */
115 	DEVMETHOD(cpufreq_drv_set,	acpi_px_set),
116 	DEVMETHOD(cpufreq_drv_get,	acpi_px_get),
117 	DEVMETHOD(cpufreq_drv_settings,	acpi_px_settings),
118 	{0, 0}
119 };
120 
121 static driver_t acpi_perf_driver = {
122 	"acpi_perf",
123 	acpi_perf_methods,
124 	sizeof(struct acpi_perf_softc),
125 };
126 
127 static devclass_t acpi_perf_devclass;
128 DRIVER_MODULE(acpi_perf, cpu, acpi_perf_driver, acpi_perf_devclass, 0, 0);
129 MODULE_DEPEND(acpi_perf, acpi, 1, 1, 1);
130 
131 MALLOC_DEFINE(M_ACPIPERF, "acpi_perf", "ACPI Performance states");
132 
133 static void
134 acpi_perf_identify(driver_t *driver, device_t parent)
135 {
136 	ACPI_HANDLE handle;
137 
138 	/* Make sure we're not being doubly invoked. */
139 	if (device_find_child(parent, "acpi_perf", 0) != NULL)
140 		return;
141 
142 	/* Get the handle for the Processor object and check for perf states. */
143 	handle = acpi_get_handle(parent);
144 	if (handle == NULL)
145 		return;
146 	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PSS", NULL, NULL)))
147 		return;
148 	if (BUS_ADD_CHILD(parent, 0, "acpi_perf", 0) == NULL)
149 		device_printf(parent, "add acpi_perf child failed\n");
150 }
151 
152 static int
153 acpi_perf_probe(device_t dev)
154 {
155 	ACPI_HANDLE handle;
156 	ACPI_OBJECT *pkg;
157 	struct resource *res;
158 	ACPI_BUFFER buf;
159 	int error, rid, type;
160 
161 	/*
162 	 * Check the performance state registers.  If they are of type
163 	 * functional fixed hardware, we don't attach to allow a more
164 	 * specific hardware driver to manage this CPU.
165 	 */
166 	error = ENXIO;
167 	handle = acpi_get_handle(dev);
168 	buf.Pointer = NULL;
169 	buf.Length = ACPI_ALLOCATE_BUFFER;
170 	if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PCT", NULL, &buf)))
171 		return (error);
172 	pkg = (ACPI_OBJECT *)buf.Pointer;
173 
174 	rid = 0;
175 	if (ACPI_PKG_VALID(pkg, 2) &&
176 	    acpi_PkgGas(dev, pkg, 0, &type, &rid, &res) == 0) {
177 		bus_release_resource(dev, type, rid, res);
178 		device_set_desc(dev, "ACPI CPU Frequency Control");
179 		error = -10;
180 	}
181 	AcpiOsFree(buf.Pointer);
182 
183 	return (error);
184 }
185 
186 static int
187 acpi_perf_attach(device_t dev)
188 {
189 	struct acpi_perf_softc *sc;
190 
191 	sc = device_get_softc(dev);
192 	sc->dev = dev;
193 	sc->handle = acpi_get_handle(dev);
194 	sc->px_max_avail = 0;
195 	sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
196 	if (acpi_perf_evaluate(dev) != 0)
197 		return (ENXIO);
198 	cpufreq_register(dev);
199 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_px_startup, NULL);
200 
201 	return (0);
202 }
203 
204 static int
205 acpi_perf_detach(device_t dev)
206 {
207 	/* TODO: teardown registers, remove notify handler. */
208 	return (ENXIO);
209 }
210 
211 /* Probe and setup any valid performance states (Px). */
212 static int
213 acpi_perf_evaluate(device_t dev)
214 {
215 	struct acpi_perf_softc *sc;
216 	ACPI_BUFFER buf;
217 	ACPI_OBJECT *pkg, *res;
218 	ACPI_STATUS status;
219 	int error, i, j;
220 	uint32_t *p;
221 
222 	/* Get the control values and parameters for each state. */
223 	error = ENXIO;
224 	sc = device_get_softc(dev);
225 	buf.Pointer = NULL;
226 	buf.Length = ACPI_ALLOCATE_BUFFER;
227 	status = AcpiEvaluateObject(sc->handle, "_PSS", NULL, &buf);
228 	if (ACPI_FAILURE(status))
229 		return (ENXIO);
230 
231 	pkg = (ACPI_OBJECT *)buf.Pointer;
232 	if (!ACPI_PKG_VALID(pkg, 1)) {
233 		device_printf(dev, "invalid top level _PSS package\n");
234 		goto out;
235 	}
236 	sc->px_count = pkg->Package.Count;
237 
238 	sc->px_states = malloc(sc->px_count * sizeof(struct acpi_px),
239 	    M_ACPIPERF, M_WAITOK | M_ZERO);
240 	if (sc->px_states == NULL)
241 		goto out;
242 
243 	/*
244 	 * Each state is a package of {CoreFreq, Power, TransitionLatency,
245 	 * BusMasterLatency, ControlVal, StatusVal}, sorted from highest
246 	 * performance to lowest.
247 	 */
248 	for (i = 0; i < sc->px_count; i++) {
249 		res = &pkg->Package.Elements[i];
250 		if (!ACPI_PKG_VALID(res, 6)) {
251 			device_printf(dev, "invalid _PSS package\n");
252 			continue;
253 		}
254 		p = &sc->px_states[i].core_freq;
255 		for (j = 0; j < 6; j++, p++)
256 			acpi_PkgInt32(res, j, p);
257 	}
258 	AcpiOsFree(buf.Pointer);
259 
260 	/* Get the control and status registers (one of each). */
261 	buf.Pointer = NULL;
262 	buf.Length = ACPI_ALLOCATE_BUFFER;
263 	status = AcpiEvaluateObject(sc->handle, "_PCT", NULL, &buf);
264 	if (ACPI_FAILURE(status))
265 		goto out;
266 
267 	/* Check the package of two registers, each a Buffer in GAS format. */
268 	pkg = (ACPI_OBJECT *)buf.Pointer;
269 	if (!ACPI_PKG_VALID(pkg, 2)) {
270 		device_printf(dev, "invalid perf register package\n");
271 		goto out;
272 	}
273 
274 	error = acpi_PkgGas(sc->dev, pkg, 0, &sc->perf_ctrl_type, &sc->px_rid,
275 	    &sc->perf_ctrl);
276 	if (error) {
277 		if (error != EOPNOTSUPP)
278 			device_printf(dev, "failed in PERF_CTL attach\n");
279 		goto out;
280 	}
281 	sc->px_rid++;
282 
283 	error = acpi_PkgGas(sc->dev, pkg, 1, &sc->perf_sts_type, &sc->px_rid,
284 	    &sc->perf_status);
285 	if (error) {
286 		if (error != EOPNOTSUPP)
287 			device_printf(dev, "failed in PERF_STATUS attach\n");
288 		goto out;
289 	}
290 	sc->px_rid++;
291 
292 	/* Get our current limit and register for notifies. */
293 	acpi_px_available(sc);
294 	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
295 	    acpi_px_notify, sc);
296 	error = 0;
297 
298 out:
299 	if (error) {
300 		if (sc->px_states)
301 			free(sc->px_states, M_ACPIPERF);
302 		sc->px_count = 0;
303 	}
304 	if (buf.Pointer)
305 		AcpiOsFree(buf.Pointer);
306 	return (error);
307 }
308 
309 static void
310 acpi_px_startup(void *arg)
311 {
312 
313 	/* Signal to the platform that we are taking over CPU control. */
314 	if (AcpiGbl_FADT->PstateCnt == 0)
315 		return;
316 	ACPI_LOCK(acpi);
317 	AcpiOsWritePort(AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->PstateCnt, 8);
318 	ACPI_UNLOCK(acpi);
319 }
320 
321 static void
322 acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context)
323 {
324 	struct acpi_perf_softc *sc;
325 
326 	sc = context;
327 	if (notify != ACPI_NOTIFY_PERF_STATES)
328 		return;
329 
330 	acpi_px_available(sc);
331 
332 	/* TODO: Implement notification when frequency changes. */
333 }
334 
335 /*
336  * Find the highest currently-supported performance state.
337  * This can be called at runtime (e.g., due to a docking event) at
338  * the request of a Notify on the processor object.
339  */
340 static void
341 acpi_px_available(struct acpi_perf_softc *sc)
342 {
343 	ACPI_STATUS status;
344 	struct cf_setting set;
345 
346 	status = acpi_GetInteger(sc->handle, "_PPC", &sc->px_max_avail);
347 
348 	/* If the old state is too high, set current state to the new max. */
349 	if (ACPI_SUCCESS(status)) {
350 		if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN &&
351 		    sc->px_curr_state > sc->px_max_avail) {
352 			acpi_px_to_set(sc->dev,
353 			    &sc->px_states[sc->px_max_avail], &set);
354 			acpi_px_set(sc->dev, &set);
355 		}
356 	} else
357 		sc->px_max_avail = 0;
358 }
359 
360 static int
361 acpi_px_to_set(device_t dev, struct acpi_px *px, struct cf_setting *set)
362 {
363 
364 	if (px == NULL || set == NULL)
365 		return (EINVAL);
366 
367 	set->freq = px->core_freq;
368 	set->power = px->power;
369 	/* XXX Include BM latency too? */
370 	set->lat = px->trans_lat;
371 	set->volts = CPUFREQ_VAL_UNKNOWN;
372 	set->dev = dev;
373 
374 	return (0);
375 }
376 
377 static int
378 acpi_px_settings(device_t dev, struct cf_setting *sets, int *count, int *type)
379 {
380 	struct acpi_perf_softc *sc;
381 	int x, y;
382 
383 	sc = device_get_softc(dev);
384 	if (sets == NULL || count == NULL)
385 		return (EINVAL);
386 	if (*count < sc->px_count - sc->px_max_avail)
387 		return (ENOMEM);
388 
389 	/* Return a list of settings that are currently valid. */
390 	y = 0;
391 	for (x = sc->px_max_avail; x < sc->px_count; x++, y++)
392 		acpi_px_to_set(dev, &sc->px_states[x], &sets[y]);
393 	*count = sc->px_count - sc->px_max_avail;
394 	*type = CPUFREQ_TYPE_ABSOLUTE;
395 
396 	return (0);
397 }
398 
399 static int
400 acpi_px_set(device_t dev, const struct cf_setting *set)
401 {
402 	struct acpi_perf_softc *sc;
403 	int i, status, sts_val, tries;
404 
405 	if (set == NULL)
406 		return (EINVAL);
407 	sc = device_get_softc(dev);
408 
409 	/* Look up appropriate state, based on frequency. */
410 	for (i = sc->px_max_avail; i < sc->px_count; i++) {
411 		if (CPUFREQ_CMP(set->freq, sc->px_states[i].core_freq))
412 			break;
413 	}
414 	if (i == sc->px_count)
415 		return (EINVAL);
416 
417 	/* Write the appropriate value to the register. */
418 	PX_SET_REG(sc->perf_ctrl, sc->px_states[i].ctrl_val);
419 
420 	/* Try for up to 1 ms to verify the desired state was selected. */
421 	sts_val = sc->px_states[i].sts_val;
422 	for (tries = 0; tries < 100; tries++) {
423 		status = PX_GET_REG(sc->perf_status);
424 		if (status == sts_val)
425 			break;
426 		DELAY(10);
427 	}
428 	if (tries == 100) {
429 		device_printf(dev, "Px transition to %d failed\n",
430 		    sc->px_states[i].core_freq);
431 		return (ENXIO);
432 	}
433 	sc->px_curr_state = i;
434 
435 	return (0);
436 }
437 
438 static int
439 acpi_px_get(device_t dev, struct cf_setting *set)
440 {
441 	struct acpi_perf_softc *sc;
442 	uint64_t rate;
443 	int i;
444 	struct pcpu *pc;
445 
446 	if (set == NULL)
447 		return (EINVAL);
448 	sc = device_get_softc(dev);
449 
450 	/* If we've set the rate before, use the cached value. */
451 	if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN) {
452 		acpi_px_to_set(dev, &sc->px_states[sc->px_curr_state], set);
453 		return (0);
454 	}
455 
456 	/* Otherwise, estimate and try to match against our settings. */
457 	pc = cpu_get_pcpu(dev);
458 	if (pc == NULL)
459 		return (ENXIO);
460 	cpu_est_clockrate(pc->pc_cpuid, &rate);
461 	rate /= 1000000;
462 	for (i = 0; i < sc->px_count; i++) {
463 		if (CPUFREQ_CMP(sc->px_states[i].core_freq, rate)) {
464 			sc->px_curr_state = i;
465 			acpi_px_to_set(dev, &sc->px_states[i], set);
466 			break;
467 		}
468 	}
469 
470 	/* No match, give up. */
471 	if (i == sc->px_count) {
472 		sc->px_curr_state = CPUFREQ_VAL_UNKNOWN;
473 		set->freq = CPUFREQ_VAL_UNKNOWN;
474 	}
475 
476 	return (0);
477 }
478