acpi_cpu_pstate.c revision 1.42 1 /* $NetBSD: acpi_cpu_pstate.c,v 1.42 2011/03/01 04:35:48 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2010, 2011 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.42 2011/03/01 04:35:48 jruoho Exp $");
31
32 #include <sys/param.h>
33 #include <sys/evcnt.h>
34 #include <sys/kmem.h>
35 #include <sys/once.h>
36 #include <sys/xcall.h>
37
38 #include <dev/acpi/acpireg.h>
39 #include <dev/acpi/acpivar.h>
40 #include <dev/acpi/acpi_cpu.h>
41
42 #define _COMPONENT ACPI_BUS_COMPONENT
43 ACPI_MODULE_NAME ("acpi_cpu_pstate")
44
45 static void acpicpu_pstate_attach_print(struct acpicpu_softc *);
46 static void acpicpu_pstate_attach_evcnt(struct acpicpu_softc *);
47 static void acpicpu_pstate_detach_evcnt(struct acpicpu_softc *);
48 static ACPI_STATUS acpicpu_pstate_pss(struct acpicpu_softc *);
49 static ACPI_STATUS acpicpu_pstate_pss_add(struct acpicpu_pstate *,
50 ACPI_OBJECT *);
51 static ACPI_STATUS acpicpu_pstate_xpss(struct acpicpu_softc *);
52 static ACPI_STATUS acpicpu_pstate_xpss_add(struct acpicpu_pstate *,
53 ACPI_OBJECT *);
54 static ACPI_STATUS acpicpu_pstate_pct(struct acpicpu_softc *);
55 static ACPI_STATUS acpicpu_pstate_dep(struct acpicpu_softc *);
56 static int acpicpu_pstate_max(struct acpicpu_softc *);
57 static int acpicpu_pstate_min(struct acpicpu_softc *);
58 static void acpicpu_pstate_change(struct acpicpu_softc *);
59 static void acpicpu_pstate_reset(struct acpicpu_softc *);
60 static void acpicpu_pstate_bios(void);
61 static void acpicpu_pstate_set_xcall(void *, void *);
62
63 static uint32_t acpicpu_pstate_saved = 0;
64 extern struct acpicpu_softc **acpicpu_sc;
65
66 void
67 acpicpu_pstate_attach(device_t self)
68 {
69 struct acpicpu_softc *sc = device_private(self);
70 const char *str;
71 ACPI_HANDLE tmp;
72 ACPI_STATUS rv;
73
74 rv = acpicpu_pstate_pss(sc);
75
76 if (ACPI_FAILURE(rv)) {
77 str = "_PSS";
78 goto fail;
79 }
80
81 /*
82 * Append additional information from the extended _PSS,
83 * if available. Note that XPSS can not be used on Intel
84 * systems that use either _PDC or _OSC. From the XPSS
85 * method specification:
86 *
87 * "The platform must not require the use of the
88 * optional _PDC or _OSC methods to coordinate
89 * between the operating system and firmware for
90 * the purposes of enabling specific processor
91 * power management features or implementations."
92 */
93 if (sc->sc_cap == 0) {
94
95 rv = acpicpu_pstate_xpss(sc);
96
97 if (ACPI_SUCCESS(rv))
98 sc->sc_flags |= ACPICPU_FLAG_P_XPSS;
99 }
100
101 rv = acpicpu_pstate_pct(sc);
102
103 if (ACPI_FAILURE(rv)) {
104 str = "_PCT";
105 goto fail;
106 }
107
108 /*
109 * The ACPI 3.0 and 4.0 specifications mandate three
110 * objects for P-states: _PSS, _PCT, and _PPC. A less
111 * strict wording is however used in the earlier 2.0
112 * standard, and some systems conforming to ACPI 2.0
113 * do not have _PPC, the method for dynamic maximum.
114 */
115 rv = AcpiGetHandle(sc->sc_node->ad_handle, "_PPC", &tmp);
116
117 if (ACPI_FAILURE(rv))
118 aprint_debug_dev(self, "_PPC missing\n");
119
120 /*
121 * Employ the XPSS structure by filling
122 * it with MD information required for FFH.
123 */
124 rv = acpicpu_md_pstate_pss(sc);
125
126 if (rv != 0) {
127 rv = AE_SUPPORT;
128 goto fail;
129 }
130
131 /*
132 * Query the optional _PSD.
133 */
134 rv = acpicpu_pstate_dep(sc);
135
136 if (ACPI_SUCCESS(rv))
137 sc->sc_flags |= ACPICPU_FLAG_P_DEP;
138
139 sc->sc_flags |= ACPICPU_FLAG_P;
140
141 acpicpu_pstate_bios();
142 acpicpu_pstate_reset(sc);
143 acpicpu_pstate_attach_evcnt(sc);
144 acpicpu_pstate_attach_print(sc);
145
146 return;
147
148 fail:
149 switch (rv) {
150
151 case AE_NOT_FOUND:
152 return;
153
154 case AE_SUPPORT:
155 aprint_verbose_dev(self, "P-states not supported\n");
156 return;
157
158 default:
159 aprint_error_dev(self, "failed to evaluate "
160 "%s: %s\n", str, AcpiFormatException(rv));
161 }
162 }
163
164 static void
165 acpicpu_pstate_attach_print(struct acpicpu_softc *sc)
166 {
167 const uint8_t method = sc->sc_pstate_control.reg_spaceid;
168 struct acpicpu_pstate *ps;
169 static bool once = false;
170 const char *str;
171 uint32_t i;
172
173 if (once != false)
174 return;
175
176 str = (method != ACPI_ADR_SPACE_SYSTEM_IO) ? "FFH" : "I/O";
177
178 for (i = 0; i < sc->sc_pstate_count; i++) {
179
180 ps = &sc->sc_pstate[i];
181
182 if (ps->ps_freq == 0)
183 continue;
184
185 aprint_verbose_dev(sc->sc_dev, "P%d: %3s, "
186 "lat %3u us, pow %5u mW, %4u MHz\n", i, str,
187 ps->ps_latency, ps->ps_power, ps->ps_freq);
188 }
189
190 once = true;
191 }
192
193 static void
194 acpicpu_pstate_attach_evcnt(struct acpicpu_softc *sc)
195 {
196 struct acpicpu_pstate *ps;
197 uint32_t i;
198
199 for (i = 0; i < sc->sc_pstate_count; i++) {
200
201 ps = &sc->sc_pstate[i];
202
203 if (ps->ps_freq == 0)
204 continue;
205
206 (void)snprintf(ps->ps_name, sizeof(ps->ps_name),
207 "P%u (%u MHz)", i, ps->ps_freq);
208
209 evcnt_attach_dynamic(&ps->ps_evcnt, EVCNT_TYPE_MISC,
210 NULL, device_xname(sc->sc_dev), ps->ps_name);
211 }
212 }
213
214 int
215 acpicpu_pstate_detach(device_t self)
216 {
217 struct acpicpu_softc *sc = device_private(self);
218 static ONCE_DECL(once_detach);
219 size_t size;
220 int rv;
221
222 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
223 return 0;
224
225 rv = RUN_ONCE(&once_detach, acpicpu_md_pstate_stop);
226
227 if (rv != 0)
228 return rv;
229
230 size = sc->sc_pstate_count * sizeof(*sc->sc_pstate);
231
232 if (sc->sc_pstate != NULL)
233 kmem_free(sc->sc_pstate, size);
234
235 sc->sc_flags &= ~ACPICPU_FLAG_P;
236 acpicpu_pstate_detach_evcnt(sc);
237
238 return 0;
239 }
240
241 static void
242 acpicpu_pstate_detach_evcnt(struct acpicpu_softc *sc)
243 {
244 struct acpicpu_pstate *ps;
245 uint32_t i;
246
247 for (i = 0; i < sc->sc_pstate_count; i++) {
248
249 ps = &sc->sc_pstate[i];
250
251 if (ps->ps_freq != 0)
252 evcnt_detach(&ps->ps_evcnt);
253 }
254 }
255
256 void
257 acpicpu_pstate_start(device_t self)
258 {
259 struct acpicpu_softc *sc = device_private(self);
260 struct acpicpu_pstate *ps;
261 uint32_t i;
262 int rv;
263
264 rv = acpicpu_md_pstate_start(sc);
265
266 if (rv != 0)
267 goto fail;
268
269 /*
270 * Initialize the states to P0.
271 */
272 for (i = 0, rv = ENXIO; i < sc->sc_pstate_count; i++) {
273
274 ps = &sc->sc_pstate[i];
275
276 if (ps->ps_freq != 0) {
277 acpicpu_pstate_set(sc->sc_ci, ps->ps_freq);
278 return;
279 }
280 }
281
282 fail:
283 sc->sc_flags &= ~ACPICPU_FLAG_P;
284
285 aprint_error_dev(self, "failed to start P-states (err %d)\n", rv);
286 }
287
288 bool
289 acpicpu_pstate_suspend(device_t self)
290 {
291 struct acpicpu_softc *sc = device_private(self);
292 struct acpicpu_pstate *ps = NULL;
293 int32_t i;
294
295 mutex_enter(&sc->sc_mtx);
296 acpicpu_pstate_reset(sc);
297 mutex_exit(&sc->sc_mtx);
298
299 if (acpicpu_pstate_saved != 0)
300 return true;
301
302 /*
303 * Following design notes for Windows, we set the highest
304 * P-state when entering any of the system sleep states.
305 * When resuming, the saved P-state will be restored.
306 *
307 * Microsoft Corporation: Windows Native Processor
308 * Performance Control. Version 1.1a, November, 2002.
309 */
310 for (i = sc->sc_pstate_count - 1; i >= 0; i--) {
311
312 if (sc->sc_pstate[i].ps_freq != 0) {
313 ps = &sc->sc_pstate[i];
314 break;
315 }
316 }
317
318 if (__predict_false(ps == NULL))
319 return true;
320
321 mutex_enter(&sc->sc_mtx);
322 acpicpu_pstate_saved = sc->sc_pstate_current;
323 mutex_exit(&sc->sc_mtx);
324
325 if (acpicpu_pstate_saved == ps->ps_freq)
326 return true;
327
328 acpicpu_pstate_set(sc->sc_ci, ps->ps_freq);
329
330 return true;
331 }
332
333 bool
334 acpicpu_pstate_resume(device_t self)
335 {
336 struct acpicpu_softc *sc = device_private(self);
337
338 if (acpicpu_pstate_saved != 0) {
339 acpicpu_pstate_set(sc->sc_ci, acpicpu_pstate_saved);
340 acpicpu_pstate_saved = 0;
341 }
342
343 return true;
344 }
345
346 void
347 acpicpu_pstate_callback(void *aux)
348 {
349 struct acpicpu_softc *sc;
350 device_t self = aux;
351 uint32_t old, new;
352
353 sc = device_private(self);
354
355 mutex_enter(&sc->sc_mtx);
356
357 old = sc->sc_pstate_max;
358 acpicpu_pstate_change(sc);
359 new = sc->sc_pstate_max;
360
361 if (old == new) {
362 mutex_exit(&sc->sc_mtx);
363 return;
364 }
365
366 mutex_exit(&sc->sc_mtx);
367
368 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "maximum frequency "
369 "changed from P%u (%u MHz) to P%u (%u MHz)\n",
370 old, sc->sc_pstate[old].ps_freq, new,
371 sc->sc_pstate[sc->sc_pstate_max].ps_freq));
372
373 acpicpu_pstate_set(sc->sc_ci, sc->sc_pstate[new].ps_freq);
374 }
375
376 ACPI_STATUS
377 acpicpu_pstate_pss(struct acpicpu_softc *sc)
378 {
379 struct acpicpu_pstate *ps;
380 ACPI_OBJECT *obj;
381 ACPI_BUFFER buf;
382 ACPI_STATUS rv;
383 uint32_t count;
384 uint32_t i, j;
385
386 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PSS", &buf);
387
388 if (ACPI_FAILURE(rv))
389 return rv;
390
391 obj = buf.Pointer;
392
393 if (obj->Type != ACPI_TYPE_PACKAGE) {
394 rv = AE_TYPE;
395 goto out;
396 }
397
398 sc->sc_pstate_count = obj->Package.Count;
399
400 if (sc->sc_pstate_count == 0) {
401 rv = AE_NOT_EXIST;
402 goto out;
403 }
404
405 if (sc->sc_pstate_count > ACPICPU_P_STATE_MAX) {
406 rv = AE_LIMIT;
407 goto out;
408 }
409
410 sc->sc_pstate = kmem_zalloc(sc->sc_pstate_count *
411 sizeof(struct acpicpu_pstate), KM_SLEEP);
412
413 if (sc->sc_pstate == NULL) {
414 rv = AE_NO_MEMORY;
415 goto out;
416 }
417
418 for (count = i = 0; i < sc->sc_pstate_count; i++) {
419
420 ps = &sc->sc_pstate[i];
421 rv = acpicpu_pstate_pss_add(ps, &obj->Package.Elements[i]);
422
423 if (ACPI_FAILURE(rv)) {
424 aprint_error_dev(sc->sc_dev, "failed to add "
425 "P-state: %s\n", AcpiFormatException(rv));
426 ps->ps_freq = 0;
427 continue;
428 }
429
430 for (j = 0; j < i; j++) {
431
432 if (ps->ps_freq >= sc->sc_pstate[j].ps_freq) {
433 ps->ps_freq = 0;
434 break;
435 }
436 }
437
438 if (ps->ps_freq != 0)
439 count++;
440 }
441
442 rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
443
444 out:
445 if (buf.Pointer != NULL)
446 ACPI_FREE(buf.Pointer);
447
448 return rv;
449 }
450
451 static ACPI_STATUS
452 acpicpu_pstate_pss_add(struct acpicpu_pstate *ps, ACPI_OBJECT *obj)
453 {
454 ACPI_OBJECT *elm;
455 int i;
456
457 if (obj->Type != ACPI_TYPE_PACKAGE)
458 return AE_TYPE;
459
460 if (obj->Package.Count != 6)
461 return AE_BAD_DATA;
462
463 elm = obj->Package.Elements;
464
465 for (i = 0; i < 6; i++) {
466
467 if (elm[i].Type != ACPI_TYPE_INTEGER)
468 return AE_TYPE;
469
470 if (elm[i].Integer.Value > UINT32_MAX)
471 return AE_AML_NUMERIC_OVERFLOW;
472 }
473
474 ps->ps_freq = elm[0].Integer.Value;
475 ps->ps_power = elm[1].Integer.Value;
476 ps->ps_latency = elm[2].Integer.Value;
477 ps->ps_latency_bm = elm[3].Integer.Value;
478 ps->ps_control = elm[4].Integer.Value;
479 ps->ps_status = elm[5].Integer.Value;
480
481 if (ps->ps_freq == 0 || ps->ps_freq > 9999)
482 return AE_BAD_DECIMAL_CONSTANT;
483
484 if (ps->ps_latency == 0 || ps->ps_latency > 1000)
485 ps->ps_latency = 1;
486
487 return AE_OK;
488 }
489
490 static ACPI_STATUS
491 acpicpu_pstate_xpss(struct acpicpu_softc *sc)
492 {
493 struct acpicpu_pstate *ps;
494 ACPI_OBJECT *obj;
495 ACPI_BUFFER buf;
496 ACPI_STATUS rv;
497 uint32_t i = 0;
498
499 rv = acpi_eval_struct(sc->sc_node->ad_handle, "XPSS", &buf);
500
501 if (ACPI_FAILURE(rv))
502 goto out;
503
504 obj = buf.Pointer;
505
506 if (obj->Type != ACPI_TYPE_PACKAGE) {
507 rv = AE_TYPE;
508 goto out;
509 }
510
511 if (obj->Package.Count != sc->sc_pstate_count) {
512 rv = AE_LIMIT;
513 goto out;
514 }
515
516 while (i < sc->sc_pstate_count) {
517
518 ps = &sc->sc_pstate[i];
519 acpicpu_pstate_xpss_add(ps, &obj->Package.Elements[i]);
520
521 i++;
522 }
523
524 out:
525 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
526 aprint_error_dev(sc->sc_dev, "failed to evaluate "
527 "XPSS: %s\n", AcpiFormatException(rv));
528
529 if (buf.Pointer != NULL)
530 ACPI_FREE(buf.Pointer);
531
532 return rv;
533 }
534
535 static ACPI_STATUS
536 acpicpu_pstate_xpss_add(struct acpicpu_pstate *ps, ACPI_OBJECT *obj)
537 {
538 ACPI_OBJECT *elm;
539 int i;
540
541 if (obj->Type != ACPI_TYPE_PACKAGE)
542 return AE_TYPE;
543
544 if (obj->Package.Count != 8)
545 return AE_BAD_DATA;
546
547 elm = obj->Package.Elements;
548
549 for (i = 0; i < 4; i++) {
550
551 if (elm[i].Type != ACPI_TYPE_INTEGER)
552 return AE_TYPE;
553
554 if (elm[i].Integer.Value > UINT32_MAX)
555 return AE_AML_NUMERIC_OVERFLOW;
556 }
557
558 for (; i < 8; i++) {
559
560 if (elm[i].Type != ACPI_TYPE_BUFFER)
561 return AE_TYPE;
562
563 if (elm[i].Buffer.Length != 8)
564 return AE_LIMIT;
565 }
566
567 /*
568 * Only overwrite the elements that were
569 * not available from the conventional _PSS.
570 */
571 if (ps->ps_freq == 0)
572 ps->ps_freq = elm[0].Integer.Value;
573
574 if (ps->ps_power == 0)
575 ps->ps_power = elm[1].Integer.Value;
576
577 if (ps->ps_latency == 0)
578 ps->ps_latency = elm[2].Integer.Value;
579
580 if (ps->ps_latency_bm == 0)
581 ps->ps_latency_bm = elm[3].Integer.Value;
582
583 if (ps->ps_control == 0)
584 ps->ps_control = ACPI_GET64(elm[4].Buffer.Pointer);
585
586 if (ps->ps_status == 0)
587 ps->ps_status = ACPI_GET64(elm[5].Buffer.Pointer);
588
589 if (ps->ps_control_mask == 0)
590 ps->ps_control_mask = ACPI_GET64(elm[6].Buffer.Pointer);
591
592 if (ps->ps_status_mask == 0)
593 ps->ps_status_mask = ACPI_GET64(elm[7].Buffer.Pointer);
594
595 ps->ps_flags |= ACPICPU_FLAG_P_XPSS;
596
597 if (ps->ps_freq == 0 || ps->ps_freq > 9999)
598 return AE_BAD_DECIMAL_CONSTANT;
599
600 if (ps->ps_latency == 0 || ps->ps_latency > 1000)
601 ps->ps_latency = 1;
602
603 return AE_OK;
604 }
605
606 ACPI_STATUS
607 acpicpu_pstate_pct(struct acpicpu_softc *sc)
608 {
609 static const size_t size = sizeof(struct acpicpu_reg);
610 struct acpicpu_reg *reg[2];
611 struct acpicpu_pstate *ps;
612 ACPI_OBJECT *elm, *obj;
613 ACPI_BUFFER buf;
614 ACPI_STATUS rv;
615 uint8_t width;
616 uint32_t i;
617
618 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PCT", &buf);
619
620 if (ACPI_FAILURE(rv))
621 return rv;
622
623 obj = buf.Pointer;
624
625 if (obj->Type != ACPI_TYPE_PACKAGE) {
626 rv = AE_TYPE;
627 goto out;
628 }
629
630 if (obj->Package.Count != 2) {
631 rv = AE_LIMIT;
632 goto out;
633 }
634
635 for (i = 0; i < 2; i++) {
636
637 elm = &obj->Package.Elements[i];
638
639 if (elm->Type != ACPI_TYPE_BUFFER) {
640 rv = AE_TYPE;
641 goto out;
642 }
643
644 if (size > elm->Buffer.Length) {
645 rv = AE_AML_BAD_RESOURCE_LENGTH;
646 goto out;
647 }
648
649 reg[i] = (struct acpicpu_reg *)elm->Buffer.Pointer;
650
651 switch (reg[i]->reg_spaceid) {
652
653 case ACPI_ADR_SPACE_SYSTEM_IO:
654
655 if (reg[i]->reg_addr == 0) {
656 rv = AE_AML_ILLEGAL_ADDRESS;
657 goto out;
658 }
659
660 width = reg[i]->reg_bitwidth;
661
662 if (width + reg[i]->reg_bitoffset > 32) {
663 rv = AE_AML_BAD_RESOURCE_VALUE;
664 goto out;
665 }
666
667 if (width != 8 && width != 16 && width != 32) {
668 rv = AE_AML_BAD_RESOURCE_VALUE;
669 goto out;
670 }
671
672 break;
673
674 case ACPI_ADR_SPACE_FIXED_HARDWARE:
675
676 if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) != 0) {
677
678 if (reg[i]->reg_bitwidth != 64) {
679 rv = AE_AML_BAD_RESOURCE_VALUE;
680 goto out;
681 }
682
683 if (reg[i]->reg_bitoffset != 0) {
684 rv = AE_AML_BAD_RESOURCE_VALUE;
685 goto out;
686 }
687
688 break;
689 }
690
691 if ((sc->sc_flags & ACPICPU_FLAG_P_FFH) == 0) {
692 rv = AE_SUPPORT;
693 goto out;
694 }
695
696 break;
697
698 default:
699 rv = AE_AML_INVALID_SPACE_ID;
700 goto out;
701 }
702 }
703
704 if (reg[0]->reg_spaceid != reg[1]->reg_spaceid) {
705 rv = AE_AML_INVALID_SPACE_ID;
706 goto out;
707 }
708
709 (void)memcpy(&sc->sc_pstate_control, reg[0], size);
710 (void)memcpy(&sc->sc_pstate_status, reg[1], size);
711
712 if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) == 0)
713 goto out;
714
715 /*
716 * In XPSS the control address can not be zero,
717 * but the status address may be. In this case,
718 * comparable to T-states, we can ignore the status
719 * check during the P-state (FFH) transition.
720 */
721 if (sc->sc_pstate_control.reg_addr == 0) {
722 rv = AE_AML_BAD_RESOURCE_LENGTH;
723 goto out;
724 }
725
726 /*
727 * If XPSS is present, copy the MSR addresses
728 * to the P-state structures for convenience.
729 */
730 for (i = 0; i < sc->sc_pstate_count; i++) {
731
732 ps = &sc->sc_pstate[i];
733
734 if (ps->ps_freq == 0)
735 continue;
736
737 ps->ps_status_addr = sc->sc_pstate_status.reg_addr;
738 ps->ps_control_addr = sc->sc_pstate_control.reg_addr;
739 }
740
741 out:
742 if (buf.Pointer != NULL)
743 ACPI_FREE(buf.Pointer);
744
745 return rv;
746 }
747
748 static ACPI_STATUS
749 acpicpu_pstate_dep(struct acpicpu_softc *sc)
750 {
751 ACPI_OBJECT *elm, *obj;
752 ACPI_BUFFER buf;
753 ACPI_STATUS rv;
754 uint32_t val;
755 uint8_t i, n;
756
757 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PSD", &buf);
758
759 if (ACPI_FAILURE(rv))
760 goto out;
761
762 obj = buf.Pointer;
763
764 if (obj->Type != ACPI_TYPE_PACKAGE) {
765 rv = AE_TYPE;
766 goto out;
767 }
768
769 if (obj->Package.Count != 1) {
770 rv = AE_LIMIT;
771 goto out;
772 }
773
774 elm = &obj->Package.Elements[0];
775
776 if (obj->Type != ACPI_TYPE_PACKAGE) {
777 rv = AE_TYPE;
778 goto out;
779 }
780
781 n = elm->Package.Count;
782
783 if (n != 5) {
784 rv = AE_LIMIT;
785 goto out;
786 }
787
788 elm = elm->Package.Elements;
789
790 for (i = 0; i < n; i++) {
791
792 if (elm[i].Type != ACPI_TYPE_INTEGER) {
793 rv = AE_TYPE;
794 goto out;
795 }
796
797 if (elm[i].Integer.Value > UINT32_MAX) {
798 rv = AE_AML_NUMERIC_OVERFLOW;
799 goto out;
800 }
801 }
802
803 val = elm[1].Integer.Value;
804
805 if (val != 0)
806 aprint_debug_dev(sc->sc_dev, "invalid revision in _PSD\n");
807
808 val = elm[3].Integer.Value;
809
810 if (val < ACPICPU_DEP_SW_ALL || val > ACPICPU_DEP_HW_ALL) {
811 rv = AE_AML_BAD_RESOURCE_VALUE;
812 goto out;
813 }
814
815 val = elm[4].Integer.Value;
816
817 if (val > sc->sc_ncpus) {
818 rv = AE_BAD_VALUE;
819 goto out;
820 }
821
822 sc->sc_pstate_dep.dep_domain = elm[2].Integer.Value;
823 sc->sc_pstate_dep.dep_type = elm[3].Integer.Value;
824 sc->sc_pstate_dep.dep_ncpus = elm[4].Integer.Value;
825
826 out:
827 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
828 aprint_debug_dev(sc->sc_dev, "failed to evaluate "
829 "_PSD: %s\n", AcpiFormatException(rv));
830
831 if (buf.Pointer != NULL)
832 ACPI_FREE(buf.Pointer);
833
834 return rv;
835 }
836
837 static int
838 acpicpu_pstate_max(struct acpicpu_softc *sc)
839 {
840 ACPI_INTEGER val;
841 ACPI_STATUS rv;
842
843 /*
844 * Evaluate the currently highest P-state that can be used.
845 * If available, we can use either this state or any lower
846 * power (i.e. higher numbered) state from the _PSS object.
847 * Note that the return value must match the _OST parameter.
848 */
849 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PPC", &val);
850
851 if (ACPI_SUCCESS(rv) && val < sc->sc_pstate_count) {
852
853 if (sc->sc_pstate[val].ps_freq != 0) {
854 sc->sc_pstate_max = val;
855 return 0;
856 }
857 }
858
859 return 1;
860 }
861
862 static int
863 acpicpu_pstate_min(struct acpicpu_softc *sc)
864 {
865 ACPI_INTEGER val;
866 ACPI_STATUS rv;
867
868 /*
869 * The _PDL object defines the minimum when passive cooling
870 * is being performed. If available, we can use the returned
871 * state or any higher power (i.e. lower numbered) state.
872 */
873 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PDL", &val);
874
875 if (ACPI_SUCCESS(rv) && val < sc->sc_pstate_count) {
876
877 if (sc->sc_pstate[val].ps_freq == 0)
878 return 1;
879
880 if (val >= sc->sc_pstate_max) {
881 sc->sc_pstate_min = val;
882 return 0;
883 }
884 }
885
886 return 1;
887 }
888
889 static void
890 acpicpu_pstate_change(struct acpicpu_softc *sc)
891 {
892 static ACPI_STATUS rv = AE_OK;
893 ACPI_OBJECT_LIST arg;
894 ACPI_OBJECT obj[2];
895 static int val = 0;
896
897 acpicpu_pstate_reset(sc);
898
899 /*
900 * Cache the checks as the optional
901 * _PDL and _OST are rarely present.
902 */
903 if (val == 0)
904 val = acpicpu_pstate_min(sc);
905
906 arg.Count = 2;
907 arg.Pointer = obj;
908
909 obj[0].Type = ACPI_TYPE_INTEGER;
910 obj[1].Type = ACPI_TYPE_INTEGER;
911
912 obj[0].Integer.Value = ACPICPU_P_NOTIFY;
913 obj[1].Integer.Value = acpicpu_pstate_max(sc);
914
915 if (ACPI_FAILURE(rv))
916 return;
917
918 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_OST", &arg, NULL);
919 }
920
921 static void
922 acpicpu_pstate_reset(struct acpicpu_softc *sc)
923 {
924
925 sc->sc_pstate_max = 0;
926 sc->sc_pstate_min = sc->sc_pstate_count - 1;
927
928 }
929
930 static void
931 acpicpu_pstate_bios(void)
932 {
933 const uint8_t val = AcpiGbl_FADT.PstateControl;
934 const uint32_t addr = AcpiGbl_FADT.SmiCommand;
935
936 if (addr == 0 || val == 0)
937 return;
938
939 (void)AcpiOsWritePort(addr, val, 8);
940 }
941
942 int
943 acpicpu_pstate_get(struct cpu_info *ci, uint32_t *freq)
944 {
945 struct acpicpu_pstate *ps = NULL;
946 struct acpicpu_softc *sc;
947 uint32_t i, val = 0;
948 uint64_t addr;
949 uint8_t width;
950 int rv;
951
952 sc = acpicpu_sc[ci->ci_acpiid];
953
954 if (__predict_false(sc == NULL)) {
955 rv = ENXIO;
956 goto fail;
957 }
958
959 if (__predict_false(sc->sc_cold != false)) {
960 rv = EBUSY;
961 goto fail;
962 }
963
964 if (__predict_false((sc->sc_flags & ACPICPU_FLAG_P) == 0)) {
965 rv = ENODEV;
966 goto fail;
967 }
968
969 mutex_enter(&sc->sc_mtx);
970
971 /*
972 * Use the cached value, if available.
973 */
974 if (sc->sc_pstate_current != ACPICPU_P_STATE_UNKNOWN) {
975 *freq = sc->sc_pstate_current;
976 mutex_exit(&sc->sc_mtx);
977 return 0;
978 }
979
980 mutex_exit(&sc->sc_mtx);
981
982 switch (sc->sc_pstate_status.reg_spaceid) {
983
984 case ACPI_ADR_SPACE_FIXED_HARDWARE:
985
986 rv = acpicpu_md_pstate_get(sc, freq);
987
988 if (__predict_false(rv != 0))
989 goto fail;
990
991 break;
992
993 case ACPI_ADR_SPACE_SYSTEM_IO:
994
995 addr = sc->sc_pstate_status.reg_addr;
996 width = sc->sc_pstate_status.reg_bitwidth;
997
998 (void)AcpiOsReadPort(addr, &val, width);
999
1000 if (val == 0) {
1001 rv = EIO;
1002 goto fail;
1003 }
1004
1005 for (i = 0; i < sc->sc_pstate_count; i++) {
1006
1007 if (sc->sc_pstate[i].ps_freq == 0)
1008 continue;
1009
1010 if (val == sc->sc_pstate[i].ps_status) {
1011 ps = &sc->sc_pstate[i];
1012 break;
1013 }
1014 }
1015
1016 if (ps == NULL) {
1017 rv = EIO;
1018 goto fail;
1019 }
1020
1021 *freq = ps->ps_freq;
1022 break;
1023
1024 default:
1025 rv = ENOTTY;
1026 goto fail;
1027 }
1028
1029 mutex_enter(&sc->sc_mtx);
1030 sc->sc_pstate_current = *freq;
1031 mutex_exit(&sc->sc_mtx);
1032
1033 return 0;
1034
1035 fail:
1036 aprint_error_dev(sc->sc_dev, "failed "
1037 "to get frequency (err %d)\n", rv);
1038
1039 mutex_enter(&sc->sc_mtx);
1040 *freq = sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
1041 mutex_exit(&sc->sc_mtx);
1042
1043 return rv;
1044 }
1045
1046 void
1047 acpicpu_pstate_set(struct cpu_info *ci, uint32_t freq)
1048 {
1049 uint64_t xc;
1050
1051 xc = xc_broadcast(0, acpicpu_pstate_set_xcall, &freq, NULL);
1052 xc_wait(xc);
1053 }
1054
1055 static void
1056 acpicpu_pstate_set_xcall(void *arg1, void *arg2)
1057 {
1058 struct acpicpu_pstate *ps = NULL;
1059 struct cpu_info *ci = curcpu();
1060 struct acpicpu_softc *sc;
1061 uint32_t freq, i, val;
1062 uint64_t addr;
1063 uint8_t width;
1064 int rv;
1065
1066 freq = *(uint32_t *)arg1;
1067 sc = acpicpu_sc[ci->ci_acpiid];
1068
1069 if (__predict_false(sc == NULL)) {
1070 rv = ENXIO;
1071 goto fail;
1072 }
1073
1074 if (__predict_false(sc->sc_cold != false)) {
1075 rv = EBUSY;
1076 goto fail;
1077 }
1078
1079 if (__predict_false((sc->sc_flags & ACPICPU_FLAG_P) == 0)) {
1080 rv = ENODEV;
1081 goto fail;
1082 }
1083
1084 mutex_enter(&sc->sc_mtx);
1085
1086 if (sc->sc_pstate_current == freq) {
1087 mutex_exit(&sc->sc_mtx);
1088 return;
1089 }
1090
1091 /*
1092 * Verify that the requested frequency is available.
1093 *
1094 * The access needs to be protected since the currently
1095 * available maximum and minimum may change dynamically.
1096 */
1097 for (i = sc->sc_pstate_max; i <= sc->sc_pstate_min; i++) {
1098
1099 if (__predict_false(sc->sc_pstate[i].ps_freq == 0))
1100 continue;
1101
1102 if (sc->sc_pstate[i].ps_freq == freq) {
1103 ps = &sc->sc_pstate[i];
1104 break;
1105 }
1106 }
1107
1108 mutex_exit(&sc->sc_mtx);
1109
1110 if (__predict_false(ps == NULL)) {
1111 rv = EINVAL;
1112 goto fail;
1113 }
1114
1115 switch (sc->sc_pstate_control.reg_spaceid) {
1116
1117 case ACPI_ADR_SPACE_FIXED_HARDWARE:
1118
1119 rv = acpicpu_md_pstate_set(ps);
1120
1121 if (__predict_false(rv != 0))
1122 goto fail;
1123
1124 break;
1125
1126 case ACPI_ADR_SPACE_SYSTEM_IO:
1127
1128 addr = sc->sc_pstate_control.reg_addr;
1129 width = sc->sc_pstate_control.reg_bitwidth;
1130
1131 (void)AcpiOsWritePort(addr, ps->ps_control, width);
1132
1133 addr = sc->sc_pstate_status.reg_addr;
1134 width = sc->sc_pstate_status.reg_bitwidth;
1135
1136 /*
1137 * Some systems take longer to respond
1138 * than the reported worst-case latency.
1139 */
1140 for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
1141
1142 (void)AcpiOsReadPort(addr, &val, width);
1143
1144 if (val == ps->ps_status)
1145 break;
1146
1147 DELAY(ps->ps_latency);
1148 }
1149
1150 if (i == ACPICPU_P_STATE_RETRY) {
1151 rv = EAGAIN;
1152 goto fail;
1153 }
1154
1155 break;
1156
1157 default:
1158 rv = ENOTTY;
1159 goto fail;
1160 }
1161
1162 mutex_enter(&sc->sc_mtx);
1163 ps->ps_evcnt.ev_count++;
1164 sc->sc_pstate_current = freq;
1165 mutex_exit(&sc->sc_mtx);
1166
1167 return;
1168
1169 fail:
1170 aprint_error_dev(sc->sc_dev, "failed to set "
1171 "frequency to %u (err %d)\n", freq, rv);
1172
1173 mutex_enter(&sc->sc_mtx);
1174 sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
1175 mutex_exit(&sc->sc_mtx);
1176 }
1177