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