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