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