acpi_cpu_pstate.c revision 1.17 1 /* $NetBSD: acpi_cpu_pstate.c,v 1.17 2010/08/13 19:48:25 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Jukka Ruohonen <jruohonen (at) iki.fi>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: acpi_cpu_pstate.c,v 1.17 2010/08/13 19:48:25 jruoho Exp $");
31
32 #include <sys/param.h>
33 #include <sys/evcnt.h>
34 #include <sys/kmem.h>
35 #include <sys/once.h>
36
37 #include <dev/acpi/acpireg.h>
38 #include <dev/acpi/acpivar.h>
39 #include <dev/acpi/acpi_cpu.h>
40
41 #define _COMPONENT ACPI_BUS_COMPONENT
42 ACPI_MODULE_NAME ("acpi_cpu_pstate")
43
44 static void acpicpu_pstate_attach_print(struct acpicpu_softc *);
45 static void acpicpu_pstate_attach_evcnt(struct acpicpu_softc *);
46 static void acpicpu_pstate_detach_evcnt(struct acpicpu_softc *);
47 static ACPI_STATUS acpicpu_pstate_pss(struct acpicpu_softc *sc);
48 static ACPI_STATUS acpicpu_pstate_pss_add(struct acpicpu_pstate *,
49 ACPI_OBJECT *);
50 static ACPI_STATUS acpicpu_pstate_pct(struct acpicpu_softc *);
51 static int acpicpu_pstate_max(struct acpicpu_softc *);
52 static void acpicpu_pstate_change(struct acpicpu_softc *);
53 static void acpicpu_pstate_bios(void);
54
55 void
56 acpicpu_pstate_attach(device_t self)
57 {
58 struct acpicpu_softc *sc = device_private(self);
59 const char *str;
60 ACPI_STATUS rv;
61
62 /*
63 * Three control methods are mandatory
64 * for P-states; _PSS, _PCT, and _PPC.
65 */
66 rv = acpicpu_pstate_pss(sc);
67
68 if (ACPI_FAILURE(rv)) {
69 str = "_PSS";
70 goto fail;
71 }
72
73 rv = acpicpu_pstate_pct(sc);
74
75 if (ACPI_FAILURE(rv)) {
76 str = "_PCT";
77 goto fail;
78 }
79
80 rv = acpicpu_pstate_max(sc);
81
82 if (rv != 0) {
83 str = "_PPC";
84 goto fail;
85 }
86
87 sc->sc_flags |= ACPICPU_FLAG_P;
88 sc->sc_pstate_current = sc->sc_pstate[0].ps_freq;
89
90 acpicpu_pstate_bios();
91 acpicpu_pstate_attach_evcnt(sc);
92 acpicpu_pstate_attach_print(sc);
93
94 return;
95
96 fail:
97 switch (rv) {
98
99 case AE_NOT_FOUND:
100 return;
101
102 case AE_SUPPORT:
103 aprint_verbose_dev(sc->sc_dev, "P-states not supported\n");
104 return;
105
106 default:
107 aprint_error_dev(sc->sc_dev, "failed to evaluate "
108 "%s: %s\n", str, AcpiFormatException(rv));
109 }
110 }
111
112 static void
113 acpicpu_pstate_attach_print(struct acpicpu_softc *sc)
114 {
115 const uint8_t method = sc->sc_pstate_control.reg_spaceid;
116 struct acpicpu_pstate *ps;
117 static bool once = false;
118 const char *str;
119 uint32_t i;
120
121 if (once != false)
122 return;
123
124 str = (method != ACPI_ADR_SPACE_SYSTEM_IO) ? "FFH" : "I/O";
125
126 for (i = 0; i < sc->sc_pstate_count; i++) {
127
128 ps = &sc->sc_pstate[i];
129
130 if (ps->ps_freq == 0)
131 continue;
132
133 aprint_debug_dev(sc->sc_dev, "P%d: %3s, "
134 "lat %3u us, pow %5u mW, %4u MHz\n", i, str,
135 ps->ps_latency, ps->ps_power, ps->ps_freq);
136 }
137
138 once = true;
139 }
140
141 static void
142 acpicpu_pstate_attach_evcnt(struct acpicpu_softc *sc)
143 {
144 struct acpicpu_pstate *ps;
145 uint32_t i;
146
147 for (i = 0; i < sc->sc_pstate_count; i++) {
148
149 ps = &sc->sc_pstate[i];
150
151 if (ps->ps_freq == 0)
152 continue;
153
154 (void)snprintf(ps->ps_name, sizeof(ps->ps_name),
155 "P%u (%u MHz)", i, ps->ps_freq);
156
157 evcnt_attach_dynamic(&ps->ps_evcnt, EVCNT_TYPE_MISC,
158 NULL, device_xname(sc->sc_dev), ps->ps_name);
159 }
160 }
161
162 int
163 acpicpu_pstate_detach(device_t self)
164 {
165 struct acpicpu_softc *sc = device_private(self);
166 static ONCE_DECL(once_detach);
167 size_t size;
168 int rv;
169
170 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
171 return 0;
172
173 rv = RUN_ONCE(&once_detach, acpicpu_md_pstate_stop);
174
175 if (rv != 0)
176 return rv;
177
178 size = sc->sc_pstate_count * sizeof(*sc->sc_pstate);
179
180 if (sc->sc_pstate != NULL)
181 kmem_free(sc->sc_pstate, size);
182
183 sc->sc_flags &= ~ACPICPU_FLAG_P;
184 acpicpu_pstate_detach_evcnt(sc);
185
186 return 0;
187 }
188
189 static void
190 acpicpu_pstate_detach_evcnt(struct acpicpu_softc *sc)
191 {
192 struct acpicpu_pstate *ps;
193 uint32_t i;
194
195 for (i = 0; i < sc->sc_pstate_count; i++) {
196
197 ps = &sc->sc_pstate[i];
198
199 if (ps->ps_freq != 0)
200 evcnt_detach(&ps->ps_evcnt);
201 }
202 }
203
204 int
205 acpicpu_pstate_start(device_t self)
206 {
207 struct acpicpu_softc *sc = device_private(self);
208 static ONCE_DECL(once_start);
209
210 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
211 return 0;
212
213 return RUN_ONCE(&once_start, acpicpu_md_pstate_start);
214 }
215
216 bool
217 acpicpu_pstate_suspend(device_t self)
218 {
219
220 return true;
221 }
222
223 bool
224 acpicpu_pstate_resume(device_t self)
225 {
226 static const ACPI_OSD_EXEC_CALLBACK func = acpicpu_pstate_callback;
227 struct acpicpu_softc *sc = device_private(self);
228
229 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
230
231 return true;
232 }
233
234 void
235 acpicpu_pstate_callback(void *aux)
236 {
237 struct acpicpu_softc *sc;
238 device_t self = aux;
239 uint32_t old, new;
240
241 sc = device_private(self);
242
243 mutex_enter(&sc->sc_mtx);
244 old = sc->sc_pstate_max;
245 acpicpu_pstate_change(sc);
246 new = sc->sc_pstate_max;
247 mutex_exit(&sc->sc_mtx);
248
249 if (old != new) {
250
251 aprint_debug_dev(sc->sc_dev, "maximum frequency "
252 "changed from P%u (%u MHz) to P%u (%u MHz)\n",
253 old, sc->sc_pstate[old].ps_freq, new,
254 sc->sc_pstate[sc->sc_pstate_max].ps_freq);
255 #if 0
256 /*
257 * If the maximum changed, proactively
258 * raise or lower the target frequency.
259 */
260 acpicpu_pstate_set(sc, sc->sc_pstate[new].ps_freq);
261
262 #endif
263 }
264 }
265
266 ACPI_STATUS
267 acpicpu_pstate_pss(struct acpicpu_softc *sc)
268 {
269 struct acpicpu_pstate *ps;
270 ACPI_OBJECT *obj;
271 ACPI_BUFFER buf;
272 ACPI_STATUS rv;
273 uint32_t count;
274 uint32_t i, j;
275
276 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PSS", &buf);
277
278 if (ACPI_FAILURE(rv))
279 return rv;
280
281 obj = buf.Pointer;
282
283 if (obj->Type != ACPI_TYPE_PACKAGE) {
284 rv = AE_TYPE;
285 goto out;
286 }
287
288 sc->sc_pstate_count = obj->Package.Count;
289
290 if (sc->sc_pstate_count == 0) {
291 rv = AE_NOT_EXIST;
292 goto out;
293 }
294
295 if (sc->sc_pstate_count > ACPICPU_P_STATE_MAX) {
296 rv = AE_LIMIT;
297 goto out;
298 }
299
300 sc->sc_pstate = kmem_zalloc(sc->sc_pstate_count *
301 sizeof(struct acpicpu_pstate), KM_SLEEP);
302
303 if (sc->sc_pstate == NULL) {
304 rv = AE_NO_MEMORY;
305 goto out;
306 }
307
308 for (count = i = 0; i < sc->sc_pstate_count; i++) {
309
310 ps = &sc->sc_pstate[i];
311 rv = acpicpu_pstate_pss_add(ps, &obj->Package.Elements[i]);
312
313 if (ACPI_FAILURE(rv)) {
314 ps->ps_freq = 0;
315 continue;
316 }
317
318 for (j = 0; j < i; j++) {
319
320 if (ps->ps_freq >= sc->sc_pstate[j].ps_freq) {
321 ps->ps_freq = 0;
322 break;
323 }
324 }
325
326 if (ps->ps_freq != 0)
327 count++;
328 }
329
330 rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
331
332 out:
333 if (buf.Pointer != NULL)
334 ACPI_FREE(buf.Pointer);
335
336 return rv;
337 }
338
339 static ACPI_STATUS
340 acpicpu_pstate_pss_add(struct acpicpu_pstate *ps, ACPI_OBJECT *obj)
341 {
342 ACPI_OBJECT *elm;
343 uint32_t val[6];
344 uint32_t *p;
345 int i;
346
347 if (obj->Type != ACPI_TYPE_PACKAGE)
348 return AE_TYPE;
349
350 if (obj->Package.Count != 6)
351 return AE_BAD_DATA;
352
353 elm = obj->Package.Elements;
354
355 for (i = 0; i < 6; i++) {
356
357 if (elm[i].Type != ACPI_TYPE_INTEGER)
358 return AE_TYPE;
359
360 if (elm[i].Integer.Value > UINT32_MAX)
361 return AE_AML_NUMERIC_OVERFLOW;
362
363 val[i] = elm[i].Integer.Value;
364 }
365
366 p = &ps->ps_freq;
367
368 for (i = 0; i < 6; i++, p++)
369 *p = val[i];
370
371 if (ps->ps_freq == 0 || ps->ps_freq > 9999)
372 return AE_BAD_DECIMAL_CONSTANT;
373
374 /*
375 * The latency is typically around 10 usec
376 * on Intel CPUs. Use that as the minimum.
377 */
378 if (ps->ps_latency < 10)
379 ps->ps_latency = 10;
380
381 return AE_OK;
382 }
383
384 ACPI_STATUS
385 acpicpu_pstate_pct(struct acpicpu_softc *sc)
386 {
387 static const size_t size = sizeof(struct acpicpu_reg);
388 struct acpicpu_reg *reg[2];
389 ACPI_OBJECT *elm, *obj;
390 ACPI_BUFFER buf;
391 ACPI_STATUS rv;
392 uint8_t width;
393 int i;
394
395 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PCT", &buf);
396
397 if (ACPI_FAILURE(rv))
398 return rv;
399
400 obj = buf.Pointer;
401
402 if (obj->Type != ACPI_TYPE_PACKAGE) {
403 rv = AE_TYPE;
404 goto out;
405 }
406
407 if (obj->Package.Count != 2) {
408 rv = AE_LIMIT;
409 goto out;
410 }
411
412 for (i = 0; i < 2; i++) {
413
414 elm = &obj->Package.Elements[i];
415
416 if (elm->Type != ACPI_TYPE_BUFFER) {
417 rv = AE_TYPE;
418 goto out;
419 }
420
421 if (size > elm->Buffer.Length) {
422 rv = AE_AML_BAD_RESOURCE_LENGTH;
423 goto out;
424 }
425
426 reg[i] = (struct acpicpu_reg *)elm->Buffer.Pointer;
427
428 switch (reg[i]->reg_spaceid) {
429
430 case ACPI_ADR_SPACE_SYSTEM_IO:
431
432 if (reg[i]->reg_addr == 0) {
433 rv = AE_AML_ILLEGAL_ADDRESS;
434 goto out;
435 }
436
437 width = reg[i]->reg_bitwidth;
438
439 if (width + reg[i]->reg_bitoffset > 32) {
440 rv = AE_AML_BAD_RESOURCE_VALUE;
441 goto out;
442 }
443
444 if (width != 8 && width != 16 && width != 32) {
445 rv = AE_AML_BAD_RESOURCE_VALUE;
446 goto out;
447 }
448
449 break;
450
451 case ACPI_ADR_SPACE_FIXED_HARDWARE:
452
453 if ((sc->sc_flags & ACPICPU_FLAG_P_FFH) == 0) {
454 rv = AE_SUPPORT;
455 goto out;
456 }
457
458 break;
459
460 default:
461 rv = AE_AML_INVALID_SPACE_ID;
462 goto out;
463 }
464 }
465
466 if (reg[0]->reg_spaceid != reg[1]->reg_spaceid) {
467 rv = AE_AML_INVALID_SPACE_ID;
468 goto out;
469 }
470
471 (void)memcpy(&sc->sc_pstate_control, reg[0], size);
472 (void)memcpy(&sc->sc_pstate_status, reg[1], size);
473
474 out:
475 if (buf.Pointer != NULL)
476 ACPI_FREE(buf.Pointer);
477
478 return rv;
479 }
480
481 static int
482 acpicpu_pstate_max(struct acpicpu_softc *sc)
483 {
484 ACPI_INTEGER val;
485 ACPI_STATUS rv;
486
487 /*
488 * Evaluate the currently highest P-state that can be used.
489 * If available, we can use either this state or any lower
490 * power (i.e. higher numbered) state from the _PSS object.
491 */
492 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PPC", &val);
493
494 sc->sc_pstate_max = 0;
495
496 if (ACPI_FAILURE(rv))
497 return 1;
498
499 if (val > sc->sc_pstate_count - 1)
500 return 1;
501
502 if (sc->sc_pstate[val].ps_freq == 0)
503 return 1;
504
505 sc->sc_pstate_max = val;
506
507 return 0;
508 }
509
510 static void
511 acpicpu_pstate_change(struct acpicpu_softc *sc)
512 {
513 ACPI_OBJECT_LIST arg;
514 ACPI_OBJECT obj[2];
515
516 arg.Count = 2;
517 arg.Pointer = obj;
518
519 obj[0].Type = ACPI_TYPE_INTEGER;
520 obj[1].Type = ACPI_TYPE_INTEGER;
521
522 obj[0].Integer.Value = ACPICPU_P_NOTIFY;
523 obj[1].Integer.Value = acpicpu_pstate_max(sc);
524
525 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "_OST", &arg, NULL);
526 }
527
528 static void
529 acpicpu_pstate_bios(void)
530 {
531 const uint8_t val = AcpiGbl_FADT.PstateControl;
532 const uint32_t addr = AcpiGbl_FADT.SmiCommand;
533
534 if (addr == 0)
535 return;
536
537 (void)AcpiOsWritePort(addr, val, 8);
538 }
539
540 int
541 acpicpu_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
542 {
543 const uint8_t method = sc->sc_pstate_control.reg_spaceid;
544 struct acpicpu_pstate *ps = NULL;
545 uint32_t i, val = 0;
546 uint64_t addr;
547 uint8_t width;
548 int rv;
549
550 if (sc->sc_cold != false) {
551 rv = EBUSY;
552 goto fail;
553 }
554
555 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0) {
556 rv = ENODEV;
557 goto fail;
558 }
559
560 mutex_enter(&sc->sc_mtx);
561
562 if (sc->sc_pstate_current != ACPICPU_P_STATE_UNKNOWN) {
563 *freq = sc->sc_pstate_current;
564 mutex_exit(&sc->sc_mtx);
565 return 0;
566 }
567
568 mutex_exit(&sc->sc_mtx);
569
570 switch (method) {
571
572 case ACPI_ADR_SPACE_FIXED_HARDWARE:
573
574 rv = acpicpu_md_pstate_get(sc, freq);
575
576 if (rv != 0)
577 goto fail;
578
579 break;
580
581 case ACPI_ADR_SPACE_SYSTEM_IO:
582
583 addr = sc->sc_pstate_status.reg_addr;
584 width = sc->sc_pstate_status.reg_bitwidth;
585
586 (void)AcpiOsReadPort(addr, &val, width);
587
588 if (val == 0) {
589 rv = EIO;
590 goto fail;
591 }
592
593 for (i = 0; i < sc->sc_pstate_count; i++) {
594
595 if (sc->sc_pstate[i].ps_freq == 0)
596 continue;
597
598 if (val == sc->sc_pstate[i].ps_status) {
599 ps = &sc->sc_pstate[i];
600 break;
601 }
602 }
603
604 if (__predict_false(ps == NULL)) {
605 rv = EIO;
606 goto fail;
607 }
608
609 *freq = ps->ps_freq;
610 break;
611
612 default:
613 rv = ENOTTY;
614 goto fail;
615 }
616
617 mutex_enter(&sc->sc_mtx);
618 sc->sc_pstate_current = *freq;
619 mutex_exit(&sc->sc_mtx);
620
621 return 0;
622
623 fail:
624 aprint_error_dev(sc->sc_dev, "failed "
625 "to get frequency (err %d)\n", rv);
626
627 mutex_enter(&sc->sc_mtx);
628 *freq = sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
629 mutex_exit(&sc->sc_mtx);
630
631 return rv;
632 }
633
634 int
635 acpicpu_pstate_set(struct acpicpu_softc *sc, uint32_t freq)
636 {
637 const uint8_t method = sc->sc_pstate_control.reg_spaceid;
638 struct acpicpu_pstate *ps = NULL;
639 uint32_t i, val;
640 uint64_t addr;
641 uint8_t width;
642 int rv;
643
644 if (sc->sc_cold != false) {
645 rv = EBUSY;
646 goto fail;
647 }
648
649 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0) {
650 rv = ENODEV;
651 goto fail;
652 }
653
654 mutex_enter(&sc->sc_mtx);
655
656 for (i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
657
658 if (sc->sc_pstate[i].ps_freq == 0)
659 continue;
660
661 if (sc->sc_pstate[i].ps_freq == freq) {
662 ps = &sc->sc_pstate[i];
663 break;
664 }
665 }
666
667 mutex_exit(&sc->sc_mtx);
668
669 if (__predict_false(ps == NULL)) {
670 rv = EINVAL;
671 goto fail;
672 }
673
674 switch (method) {
675
676 case ACPI_ADR_SPACE_FIXED_HARDWARE:
677
678 rv = acpicpu_md_pstate_set(ps);
679
680 if (rv != 0)
681 goto fail;
682
683 break;
684
685 case ACPI_ADR_SPACE_SYSTEM_IO:
686
687 addr = sc->sc_pstate_control.reg_addr;
688 width = sc->sc_pstate_control.reg_bitwidth;
689
690 (void)AcpiOsWritePort(addr, ps->ps_control, width);
691
692 addr = sc->sc_pstate_status.reg_addr;
693 width = sc->sc_pstate_status.reg_bitwidth;
694
695 /*
696 * Some systems take longer to respond
697 * than the reported worst-case latency.
698 */
699 for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
700
701 (void)AcpiOsReadPort(addr, &val, width);
702
703 if (val == ps->ps_status)
704 break;
705
706 DELAY(ps->ps_latency);
707 }
708
709 if (i == ACPICPU_P_STATE_RETRY) {
710 rv = EAGAIN;
711 goto fail;
712 }
713
714 break;
715
716 default:
717 rv = ENOTTY;
718 goto fail;
719 }
720
721 mutex_enter(&sc->sc_mtx);
722 ps->ps_evcnt.ev_count++;
723 sc->sc_pstate_current = freq;
724 mutex_exit(&sc->sc_mtx);
725
726 return 0;
727
728 fail:
729 aprint_error_dev(sc->sc_dev, "failed to set "
730 "frequency to %u (err %d)\n", freq, rv);
731
732 mutex_enter(&sc->sc_mtx);
733 sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
734 mutex_exit(&sc->sc_mtx);
735
736 return rv;
737 }
738