acpi_cpu_tstate.c revision 1.30.8.1 1 /* $NetBSD: acpi_cpu_tstate.c,v 1.30.8.1 2013/11/25 08:23:31 bouyer 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_tstate.c,v 1.30.8.1 2013/11/25 08:23:31 bouyer Exp $");
31
32 #include <sys/param.h>
33 #include <sys/kmem.h>
34 #include <sys/xcall.h>
35
36 #include <dev/acpi/acpireg.h>
37 #include <dev/acpi/acpivar.h>
38 #include <dev/acpi/acpi_cpu.h>
39
40 #define _COMPONENT ACPI_BUS_COMPONENT
41 ACPI_MODULE_NAME ("acpi_cpu_tstate")
42
43 static ACPI_STATUS acpicpu_tstate_tss(struct acpicpu_softc *);
44 static ACPI_STATUS acpicpu_tstate_tss_add(struct acpicpu_tstate *,
45 ACPI_OBJECT *);
46 static ACPI_STATUS acpicpu_tstate_ptc(struct acpicpu_softc *);
47 static ACPI_STATUS acpicpu_tstate_dep(struct acpicpu_softc *);
48 static ACPI_STATUS acpicpu_tstate_fadt(struct acpicpu_softc *);
49 static ACPI_STATUS acpicpu_tstate_change(struct acpicpu_softc *);
50 static void acpicpu_tstate_reset(struct acpicpu_softc *);
51 static void acpicpu_tstate_set_xcall(void *, void *);
52
53 extern struct acpicpu_softc **acpicpu_sc;
54
55 void
56 acpicpu_tstate_attach(device_t self)
57 {
58 struct acpicpu_softc *sc = device_private(self);
59 const char *str;
60 ACPI_HANDLE tmp;
61 ACPI_STATUS rv;
62
63 /*
64 * Disable T-states for PIIX4.
65 */
66 if ((sc->sc_flags & ACPICPU_FLAG_PIIX4) != 0)
67 return;
68
69 rv = acpicpu_tstate_tss(sc);
70
71 if (ACPI_FAILURE(rv)) {
72 str = "_TSS";
73 goto out;
74 }
75
76 rv = acpicpu_tstate_ptc(sc);
77
78 if (ACPI_FAILURE(rv)) {
79 str = "_PTC";
80 goto out;
81 }
82
83 /*
84 * Query the optional _TSD.
85 */
86 rv = acpicpu_tstate_dep(sc);
87
88 if (ACPI_SUCCESS(rv))
89 sc->sc_flags |= ACPICPU_FLAG_T_DEP;
90
91 /*
92 * Comparable to P-states, the _TPC object may
93 * be absent in some systems, even though it is
94 * required by ACPI 3.0 along with _TSS and _PTC.
95 */
96 rv = AcpiGetHandle(sc->sc_node->ad_handle, "_TPC", &tmp);
97
98 if (ACPI_FAILURE(rv)) {
99 aprint_debug_dev(self, "_TPC missing\n");
100 rv = AE_OK;
101 }
102
103 out:
104 if (ACPI_FAILURE(rv)) {
105
106 if (rv != AE_NOT_FOUND)
107 aprint_error_dev(sc->sc_dev, "failed to evaluate "
108 "%s: %s\n", str, AcpiFormatException(rv));
109
110 rv = acpicpu_tstate_fadt(sc);
111
112 if (ACPI_FAILURE(rv))
113 return;
114
115 sc->sc_flags |= ACPICPU_FLAG_T_FADT;
116 }
117
118 sc->sc_flags |= ACPICPU_FLAG_T;
119
120 acpicpu_tstate_reset(sc);
121 }
122
123 void
124 acpicpu_tstate_detach(device_t self)
125 {
126 struct acpicpu_softc *sc = device_private(self);
127 size_t size;
128
129 if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
130 return;
131
132 size = sc->sc_tstate_count * sizeof(*sc->sc_tstate);
133
134 if (sc->sc_tstate != NULL)
135 kmem_free(sc->sc_tstate, size);
136
137 sc->sc_flags &= ~ACPICPU_FLAG_T;
138 }
139
140 void
141 acpicpu_tstate_start(device_t self)
142 {
143 /* Nothing. */
144 }
145
146 void
147 acpicpu_tstate_suspend(void *aux)
148 {
149 struct acpicpu_softc *sc;
150 device_t self = aux;
151
152 sc = device_private(self);
153
154 mutex_enter(&sc->sc_mtx);
155 acpicpu_tstate_reset(sc);
156 mutex_exit(&sc->sc_mtx);
157 }
158
159 void
160 acpicpu_tstate_resume(void *aux)
161 {
162 /* Nothing. */
163 }
164
165 void
166 acpicpu_tstate_callback(void *aux)
167 {
168 struct acpicpu_softc *sc;
169 device_t self = aux;
170 uint32_t omax, omin;
171 int i;
172
173 sc = device_private(self);
174
175 if ((sc->sc_flags & ACPICPU_FLAG_T_FADT) != 0)
176 return;
177
178 mutex_enter(&sc->sc_mtx);
179
180 /*
181 * If P-states are in use, we should ignore
182 * the interrupt unless we are in the highest
183 * P-state (see ACPI 4.0, section 8.4.3.3).
184 */
185 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0) {
186
187 for (i = sc->sc_pstate_count - 1; i >= 0; i--) {
188
189 if (sc->sc_pstate[i].ps_freq != 0)
190 break;
191 }
192
193 if (sc->sc_pstate_current != sc->sc_pstate[i].ps_freq) {
194 mutex_exit(&sc->sc_mtx);
195 return;
196 }
197 }
198
199 omax = sc->sc_tstate_max;
200 omin = sc->sc_tstate_min;
201
202 (void)acpicpu_tstate_change(sc);
203
204 if (omax != sc->sc_tstate_max || omin != sc->sc_tstate_min) {
205
206 aprint_debug_dev(sc->sc_dev, "throttling window "
207 "changed from %u-%u %% to %u-%u %%\n",
208 sc->sc_tstate[omax].ts_percent,
209 sc->sc_tstate[omin].ts_percent,
210 sc->sc_tstate[sc->sc_tstate_max].ts_percent,
211 sc->sc_tstate[sc->sc_tstate_min].ts_percent);
212 }
213
214 mutex_exit(&sc->sc_mtx);
215 }
216
217 static ACPI_STATUS
218 acpicpu_tstate_tss(struct acpicpu_softc *sc)
219 {
220 struct acpicpu_tstate *ts;
221 ACPI_OBJECT *obj;
222 ACPI_BUFFER buf;
223 ACPI_STATUS rv;
224 uint32_t count;
225 uint32_t i, j;
226
227 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_TSS", &buf);
228
229 if (ACPI_FAILURE(rv))
230 return rv;
231
232 obj = buf.Pointer;
233
234 if (obj->Type != ACPI_TYPE_PACKAGE) {
235 rv = AE_TYPE;
236 goto out;
237 }
238
239 sc->sc_tstate_count = obj->Package.Count;
240
241 if (sc->sc_tstate_count == 0) {
242 rv = AE_NOT_EXIST;
243 goto out;
244 }
245
246 if (sc->sc_tstate_count > ACPICPU_T_STATE_MAX) {
247 rv = AE_LIMIT;
248 goto out;
249 }
250
251 sc->sc_tstate = kmem_zalloc(sc->sc_tstate_count *
252 sizeof(struct acpicpu_tstate), KM_SLEEP);
253
254 if (sc->sc_tstate == NULL) {
255 rv = AE_NO_MEMORY;
256 goto out;
257 }
258
259 for (count = i = 0; i < sc->sc_tstate_count; i++) {
260
261 ts = &sc->sc_tstate[i];
262 rv = acpicpu_tstate_tss_add(ts, &obj->Package.Elements[i]);
263
264 if (ACPI_FAILURE(rv)) {
265 ts->ts_percent = 0;
266 continue;
267 }
268
269 for (j = 0; j < i; j++) {
270
271 if (ts->ts_percent >= sc->sc_tstate[j].ts_percent) {
272 ts->ts_percent = 0;
273 break;
274 }
275 }
276
277 if (ts->ts_percent != 0)
278 count++;
279 }
280
281 if (count == 0) {
282 rv = AE_NOT_EXIST;
283 goto out;
284 }
285
286 /*
287 * There must be an entry with the percent
288 * field of 100. If this is not true, and if
289 * this entry is not in the expected index,
290 * invalidate the use of T-states via _TSS.
291 */
292 if (sc->sc_tstate[0].ts_percent != 100) {
293 rv = AE_BAD_DECIMAL_CONSTANT;
294 goto out;
295 }
296
297 out:
298 if (buf.Pointer != NULL)
299 ACPI_FREE(buf.Pointer);
300
301 return rv;
302 }
303
304 static ACPI_STATUS
305 acpicpu_tstate_tss_add(struct acpicpu_tstate *ts, ACPI_OBJECT *obj)
306 {
307 ACPI_OBJECT *elm;
308 uint32_t val[5];
309 uint32_t *p;
310 int i;
311
312 if (obj->Type != ACPI_TYPE_PACKAGE)
313 return AE_TYPE;
314
315 if (obj->Package.Count != 5)
316 return AE_BAD_DATA;
317
318 elm = obj->Package.Elements;
319
320 for (i = 0; i < 5; i++) {
321
322 if (elm[i].Type != ACPI_TYPE_INTEGER)
323 return AE_TYPE;
324
325 if (elm[i].Integer.Value > UINT32_MAX)
326 return AE_AML_NUMERIC_OVERFLOW;
327
328 val[i] = elm[i].Integer.Value;
329 }
330
331 p = &ts->ts_percent;
332
333 for (i = 0; i < 5; i++, p++)
334 *p = val[i];
335
336 /*
337 * The minimum should be either 12.5 % or 6.5 %,
338 * the latter 4-bit dynamic range being available
339 * in some newer models; see Section 14.5.3.1 in
340 *
341 * Intel 64 and IA-32 Architectures Software
342 * Developer's Manual. Volume 3B, Part 2. 2013.
343 */
344 if (ts->ts_percent < 6 || ts->ts_percent > 100)
345 return AE_BAD_DECIMAL_CONSTANT;
346
347 if (ts->ts_latency == 0 || ts->ts_latency > 1000)
348 ts->ts_latency = 1;
349
350 return AE_OK;
351 }
352
353 ACPI_STATUS
354 acpicpu_tstate_ptc(struct acpicpu_softc *sc)
355 {
356 static const size_t size = sizeof(struct acpicpu_reg);
357 struct acpicpu_reg *reg[2];
358 ACPI_OBJECT *elm, *obj;
359 ACPI_BUFFER buf;
360 ACPI_STATUS rv;
361 int i;
362
363 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PTC", &buf);
364
365 if (ACPI_FAILURE(rv))
366 return rv;
367
368 obj = buf.Pointer;
369
370 if (obj->Type != ACPI_TYPE_PACKAGE) {
371 rv = AE_TYPE;
372 goto out;
373 }
374
375 if (obj->Package.Count != 2) {
376 rv = AE_LIMIT;
377 goto out;
378 }
379
380 for (i = 0; i < 2; i++) {
381
382 elm = &obj->Package.Elements[i];
383
384 if (elm->Type != ACPI_TYPE_BUFFER) {
385 rv = AE_TYPE;
386 goto out;
387 }
388
389 if (size > elm->Buffer.Length) {
390 rv = AE_AML_BAD_RESOURCE_LENGTH;
391 goto out;
392 }
393
394 reg[i] = (struct acpicpu_reg *)elm->Buffer.Pointer;
395
396 switch (reg[i]->reg_spaceid) {
397
398 case ACPI_ADR_SPACE_SYSTEM_IO:
399
400 if (reg[i]->reg_addr == 0) {
401 rv = AE_AML_ILLEGAL_ADDRESS;
402 goto out;
403 }
404
405 /*
406 * Check that the values match the IA32 clock
407 * modulation MSR, where the bit 0 is reserved,
408 * bits 1 through 3 define the duty cycle, and
409 * the fourth bit enables the modulation.
410 */
411 if (reg[i]->reg_bitwidth != 4) {
412 rv = AE_AML_BAD_RESOURCE_VALUE;
413 goto out;
414 }
415
416 if (reg[i]->reg_bitoffset != 1) {
417 rv = AE_AML_BAD_RESOURCE_VALUE;
418 goto out;
419 }
420
421 break;
422
423 case ACPI_ADR_SPACE_FIXED_HARDWARE:
424
425 if ((sc->sc_flags & ACPICPU_FLAG_T_FFH) == 0) {
426 rv = AE_SUPPORT;
427 goto out;
428 }
429
430 break;
431
432 default:
433 rv = AE_AML_INVALID_SPACE_ID;
434 goto out;
435 }
436 }
437
438 if (reg[0]->reg_spaceid != reg[1]->reg_spaceid) {
439 rv = AE_AML_INVALID_SPACE_ID;
440 goto out;
441 }
442
443 (void)memcpy(&sc->sc_tstate_control, reg[0], size);
444 (void)memcpy(&sc->sc_tstate_status, reg[1], size);
445
446 out:
447 if (buf.Pointer != NULL)
448 ACPI_FREE(buf.Pointer);
449
450 return rv;
451 }
452
453 static ACPI_STATUS
454 acpicpu_tstate_dep(struct acpicpu_softc *sc)
455 {
456 ACPI_OBJECT *elm, *obj;
457 ACPI_BUFFER buf;
458 ACPI_STATUS rv;
459 uint32_t val;
460 uint8_t i, n;
461
462 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_TSD", &buf);
463
464 if (ACPI_FAILURE(rv))
465 goto out;
466
467 obj = buf.Pointer;
468
469 if (obj->Type != ACPI_TYPE_PACKAGE) {
470 rv = AE_TYPE;
471 goto out;
472 }
473
474 if (obj->Package.Count != 1) {
475 rv = AE_LIMIT;
476 goto out;
477 }
478
479 elm = &obj->Package.Elements[0];
480
481 if (obj->Type != ACPI_TYPE_PACKAGE) {
482 rv = AE_TYPE;
483 goto out;
484 }
485
486 n = elm->Package.Count;
487
488 if (n != 5) {
489 rv = AE_LIMIT;
490 goto out;
491 }
492
493 elm = elm->Package.Elements;
494
495 for (i = 0; i < n; i++) {
496
497 if (elm[i].Type != ACPI_TYPE_INTEGER) {
498 rv = AE_TYPE;
499 goto out;
500 }
501
502 if (elm[i].Integer.Value > UINT32_MAX) {
503 rv = AE_AML_NUMERIC_OVERFLOW;
504 goto out;
505 }
506 }
507
508 val = elm[1].Integer.Value;
509
510 if (val != 0)
511 aprint_debug_dev(sc->sc_dev, "invalid revision in _TSD\n");
512
513 val = elm[3].Integer.Value;
514
515 if (val < ACPICPU_DEP_SW_ALL || val > ACPICPU_DEP_HW_ALL) {
516 rv = AE_AML_BAD_RESOURCE_VALUE;
517 goto out;
518 }
519
520 val = elm[4].Integer.Value;
521
522 if (val > sc->sc_ncpus) {
523 rv = AE_BAD_VALUE;
524 goto out;
525 }
526
527 sc->sc_tstate_dep.dep_domain = elm[2].Integer.Value;
528 sc->sc_tstate_dep.dep_type = elm[3].Integer.Value;
529 sc->sc_tstate_dep.dep_ncpus = elm[4].Integer.Value;
530
531 out:
532 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
533 aprint_debug_dev(sc->sc_dev, "failed to evaluate "
534 "_TSD: %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_tstate_fadt(struct acpicpu_softc *sc)
544 {
545 static const size_t size = sizeof(struct acpicpu_tstate);
546 const uint8_t offset = AcpiGbl_FADT.DutyOffset;
547 const uint8_t width = AcpiGbl_FADT.DutyWidth;
548 uint8_t beta, count, i;
549
550 if (sc->sc_object.ao_pblkaddr == 0)
551 return AE_AML_ILLEGAL_ADDRESS;
552
553 /*
554 * A zero DUTY_WIDTH may be used announce
555 * that T-states are not available via FADT
556 * (ACPI 4.0, p. 121). See also (section 9.3):
557 *
558 * Advanced Micro Devices: BIOS and Kernel
559 * Developer's Guide for AMD Athlon 64 and
560 * AMD Opteron Processors. Revision 3.30,
561 * February 2006.
562 */
563 if (width == 0 || width + offset > 4)
564 return AE_AML_BAD_RESOURCE_VALUE;
565
566 count = 1 << width;
567
568 if (count > ACPICPU_T_STATE_MAX)
569 return AE_LIMIT;
570
571 if (sc->sc_tstate != NULL)
572 kmem_free(sc->sc_tstate, sc->sc_tstate_count * size);
573
574 sc->sc_tstate = kmem_zalloc(count * size, KM_SLEEP);
575
576 if (sc->sc_tstate == NULL)
577 return ENOMEM;
578
579 sc->sc_tstate_count = count;
580
581 /*
582 * Approximate duty cycles and set the MSR values.
583 */
584 for (beta = 100 / count, i = 0; i < count; i++) {
585 sc->sc_tstate[i].ts_percent = 100 - beta * i;
586 sc->sc_tstate[i].ts_latency = 1;
587 }
588
589 for (i = 1; i < count; i++)
590 sc->sc_tstate[i].ts_control = (count - i) | __BIT(3);
591
592 /*
593 * Fake values for throttling registers.
594 */
595 (void)memset(&sc->sc_tstate_status, 0, sizeof(struct acpicpu_reg));
596 (void)memset(&sc->sc_tstate_control, 0, sizeof(struct acpicpu_reg));
597
598 sc->sc_tstate_status.reg_bitwidth = width;
599 sc->sc_tstate_status.reg_bitoffset = offset;
600 sc->sc_tstate_status.reg_addr = sc->sc_object.ao_pblkaddr;
601 sc->sc_tstate_status.reg_spaceid = ACPI_ADR_SPACE_SYSTEM_IO;
602
603 sc->sc_tstate_control.reg_bitwidth = width;
604 sc->sc_tstate_control.reg_bitoffset = offset;
605 sc->sc_tstate_control.reg_addr = sc->sc_object.ao_pblkaddr;
606 sc->sc_tstate_control.reg_spaceid = ACPI_ADR_SPACE_SYSTEM_IO;
607
608 return AE_OK;
609 }
610
611 static ACPI_STATUS
612 acpicpu_tstate_change(struct acpicpu_softc *sc)
613 {
614 ACPI_INTEGER val;
615 ACPI_STATUS rv;
616
617 acpicpu_tstate_reset(sc);
618
619 /*
620 * Evaluate the available T-state window:
621 *
622 * _TPC : either this maximum or any lower power
623 * (i.e. higher numbered) state may be used.
624 *
625 * _TDL : either this minimum or any higher power
626 * (i.e. lower numbered) state may be used.
627 *
628 * _TDL >= _TPC || _TDL >= _TSS[last entry].
629 */
630 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TPC", &val);
631
632 if (ACPI_SUCCESS(rv) && val < sc->sc_tstate_count) {
633
634 if (sc->sc_tstate[val].ts_percent != 0)
635 sc->sc_tstate_max = val;
636 }
637
638 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TDL", &val);
639
640 if (ACPI_SUCCESS(rv) && val < sc->sc_tstate_count) {
641
642 if (val >= sc->sc_tstate_max &&
643 sc->sc_tstate[val].ts_percent != 0)
644 sc->sc_tstate_min = val;
645 }
646
647 return AE_OK;
648 }
649
650 static void
651 acpicpu_tstate_reset(struct acpicpu_softc *sc)
652 {
653
654 sc->sc_tstate_max = 0;
655 sc->sc_tstate_min = sc->sc_tstate_count - 1;
656 }
657
658 int
659 acpicpu_tstate_get(struct cpu_info *ci, uint32_t *percent)
660 {
661 struct acpicpu_tstate *ts = NULL;
662 struct acpicpu_softc *sc;
663 uint32_t i, val = 0;
664 uint8_t offset;
665 uint64_t addr;
666 int rv;
667
668 sc = acpicpu_sc[ci->ci_acpiid];
669
670 if (__predict_false(sc == NULL)) {
671 rv = ENXIO;
672 goto fail;
673 }
674
675 if (__predict_false(sc->sc_cold != false)) {
676 rv = EBUSY;
677 goto fail;
678 }
679
680 if (__predict_false((sc->sc_flags & ACPICPU_FLAG_T) == 0)) {
681 rv = ENODEV;
682 goto fail;
683 }
684
685 mutex_enter(&sc->sc_mtx);
686
687 if (sc->sc_tstate_current != ACPICPU_T_STATE_UNKNOWN) {
688 *percent = sc->sc_tstate_current;
689 mutex_exit(&sc->sc_mtx);
690 return 0;
691 }
692
693 mutex_exit(&sc->sc_mtx);
694
695 switch (sc->sc_tstate_status.reg_spaceid) {
696
697 case ACPI_ADR_SPACE_FIXED_HARDWARE:
698
699 rv = acpicpu_md_tstate_get(sc, percent);
700
701 if (__predict_false(rv != 0))
702 goto fail;
703
704 break;
705
706 case ACPI_ADR_SPACE_SYSTEM_IO:
707
708 addr = sc->sc_tstate_status.reg_addr;
709 offset = sc->sc_tstate_status.reg_bitoffset;
710
711 (void)AcpiOsReadPort(addr, &val, 8);
712
713 val = (val >> offset) & 0x0F;
714
715 for (i = 0; i < sc->sc_tstate_count; i++) {
716
717 if (sc->sc_tstate[i].ts_percent == 0)
718 continue;
719
720 if (val == sc->sc_tstate[i].ts_status) {
721 ts = &sc->sc_tstate[i];
722 break;
723 }
724 }
725
726 if (ts == NULL) {
727 rv = EIO;
728 goto fail;
729 }
730
731 *percent = ts->ts_percent;
732 break;
733
734 default:
735 rv = ENOTTY;
736 goto fail;
737 }
738
739 mutex_enter(&sc->sc_mtx);
740 sc->sc_tstate_current = *percent;
741 mutex_exit(&sc->sc_mtx);
742
743 return 0;
744
745 fail:
746 aprint_error_dev(sc->sc_dev, "failed "
747 "to get T-state (err %d)\n", rv);
748
749 mutex_enter(&sc->sc_mtx);
750 *percent = sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
751 mutex_exit(&sc->sc_mtx);
752
753 return rv;
754 }
755
756 void
757 acpicpu_tstate_set(struct cpu_info *ci, uint32_t percent)
758 {
759 uint64_t xc;
760
761 xc = xc_broadcast(0, acpicpu_tstate_set_xcall, &percent, NULL);
762 xc_wait(xc);
763 }
764
765 static void
766 acpicpu_tstate_set_xcall(void *arg1, void *arg2)
767 {
768 struct acpicpu_tstate *ts = NULL;
769 struct cpu_info *ci = curcpu();
770 struct acpicpu_softc *sc;
771 uint32_t i, percent, val;
772 uint8_t offset;
773 uint64_t addr;
774 int rv;
775
776 percent = *(uint32_t *)arg1;
777 sc = acpicpu_sc[ci->ci_acpiid];
778
779 if (__predict_false(sc == NULL)) {
780 rv = ENXIO;
781 goto fail;
782 }
783
784 if (__predict_false(sc->sc_cold != false)) {
785 rv = EBUSY;
786 goto fail;
787 }
788
789 if (__predict_false((sc->sc_flags & ACPICPU_FLAG_T) == 0)) {
790 rv = ENODEV;
791 goto fail;
792 }
793
794 mutex_enter(&sc->sc_mtx);
795
796 if (sc->sc_tstate_current == percent) {
797 mutex_exit(&sc->sc_mtx);
798 return;
799 }
800
801 for (i = sc->sc_tstate_max; i <= sc->sc_tstate_min; i++) {
802
803 if (__predict_false(sc->sc_tstate[i].ts_percent == 0))
804 continue;
805
806 if (sc->sc_tstate[i].ts_percent == percent) {
807 ts = &sc->sc_tstate[i];
808 break;
809 }
810 }
811
812 mutex_exit(&sc->sc_mtx);
813
814 if (__predict_false(ts == NULL)) {
815 rv = EINVAL;
816 goto fail;
817 }
818
819 switch (sc->sc_tstate_control.reg_spaceid) {
820
821 case ACPI_ADR_SPACE_FIXED_HARDWARE:
822
823 rv = acpicpu_md_tstate_set(ts);
824
825 if (__predict_false(rv != 0))
826 goto fail;
827
828 break;
829
830 case ACPI_ADR_SPACE_SYSTEM_IO:
831
832 addr = sc->sc_tstate_control.reg_addr;
833 offset = sc->sc_tstate_control.reg_bitoffset;
834
835 val = (ts->ts_control & 0x0F) << offset;
836
837 if (ts->ts_percent != 100 && (val & __BIT(4)) == 0) {
838 rv = EINVAL;
839 goto fail;
840 }
841
842 (void)AcpiOsWritePort(addr, val, 8);
843
844 /*
845 * If the status field is zero, the transition is
846 * specified to be "asynchronous" and there is no
847 * need to check the status (ACPI 4.0, 8.4.3.2).
848 */
849 if (ts->ts_status == 0)
850 break;
851
852 addr = sc->sc_tstate_status.reg_addr;
853 offset = sc->sc_tstate_status.reg_bitoffset;
854
855 for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
856
857 (void)AcpiOsReadPort(addr, &val, 8);
858
859 val = (val >> offset) & 0x0F;
860
861 if (val == ts->ts_status)
862 break;
863
864 DELAY(ts->ts_latency);
865 }
866
867 if (i == ACPICPU_T_STATE_RETRY) {
868 rv = EAGAIN;
869 goto fail;
870 }
871
872 break;
873
874 default:
875 rv = ENOTTY;
876 goto fail;
877 }
878
879 mutex_enter(&sc->sc_mtx);
880 ts->ts_evcnt.ev_count++;
881 sc->sc_tstate_current = percent;
882 mutex_exit(&sc->sc_mtx);
883
884 return;
885
886 fail:
887 if (rv != EINVAL)
888 aprint_error_dev(sc->sc_dev, "failed to "
889 "throttle to %u %% (err %d)\n", percent, rv);
890
891 mutex_enter(&sc->sc_mtx);
892 sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
893 mutex_exit(&sc->sc_mtx);
894 }
895