acpi_cpu_cstate.c revision 1.14 1 /* $NetBSD: acpi_cpu_cstate.c,v 1.14 2010/08/04 10:02:12 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_cstate.c,v 1.14 2010/08/04 10:02:12 jruoho Exp $");
31
32 #include <sys/param.h>
33 #include <sys/cpu.h>
34 #include <sys/device.h>
35 #include <sys/kernel.h>
36 #include <sys/once.h>
37 #include <sys/mutex.h>
38 #include <sys/timetc.h>
39
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcidevs.h>
42
43 #include <dev/acpi/acpireg.h>
44 #include <dev/acpi/acpivar.h>
45 #include <dev/acpi/acpi_cpu.h>
46 #include <dev/acpi/acpi_timer.h>
47
48 #include <machine/acpi_machdep.h>
49
50 #define _COMPONENT ACPI_BUS_COMPONENT
51 ACPI_MODULE_NAME ("acpi_cpu_cstate")
52
53 static void acpicpu_cstate_attach_print(struct acpicpu_softc *);
54 static ACPI_STATUS acpicpu_cstate_cst(struct acpicpu_softc *);
55 static ACPI_STATUS acpicpu_cstate_cst_add(struct acpicpu_softc *,
56 ACPI_OBJECT *);
57 static void acpicpu_cstate_cst_bios(void);
58 static ACPI_STATUS acpicpu_cstate_csd(ACPI_HANDLE, struct acpicpu_dep *);
59 static void acpicpu_cstate_fadt(struct acpicpu_softc *);
60 static void acpicpu_cstate_quirks(struct acpicpu_softc *);
61 static int acpicpu_cstate_quirks_piix4(struct pci_attach_args *);
62 static int acpicpu_cstate_latency(struct acpicpu_softc *);
63 static bool acpicpu_cstate_bm_check(void);
64 static void acpicpu_cstate_idle_enter(struct acpicpu_softc *,int);
65
66 extern struct acpicpu_softc **acpicpu_sc;
67
68 /*
69 * XXX: The local APIC timer (as well as TSC) is typically
70 * stopped in C3. For now, we cannot but disable C3.
71 */
72 #ifdef ACPICPU_ENABLE_C3
73 static int cs_state_max = ACPI_STATE_C3;
74 #else
75 static int cs_state_max = ACPI_STATE_C2;
76 #endif
77
78 void
79 acpicpu_cstate_attach(device_t self)
80 {
81 struct acpicpu_softc *sc = device_private(self);
82 ACPI_STATUS rv;
83
84 /*
85 * Either use the preferred _CST or resort to FADT.
86 */
87 rv = acpicpu_cstate_cst(sc);
88
89 switch (rv) {
90
91 case AE_OK:
92 sc->sc_flags |= ACPICPU_FLAG_C_CST;
93 acpicpu_cstate_cst_bios();
94 break;
95
96 default:
97 sc->sc_flags |= ACPICPU_FLAG_C_FADT;
98 acpicpu_cstate_fadt(sc);
99 break;
100 }
101
102 acpicpu_cstate_quirks(sc);
103 acpicpu_cstate_attach_print(sc);
104 }
105
106 void
107 acpicpu_cstate_attach_print(struct acpicpu_softc *sc)
108 {
109 struct acpicpu_cstate *cs;
110 struct acpicpu_dep dep;
111 const char *method;
112 ACPI_STATUS rv;
113 int i;
114
115 (void)memset(&dep, 0, sizeof(struct acpicpu_dep));
116
117 rv = acpicpu_cstate_csd(sc->sc_node->ad_handle, &dep);
118
119 if (ACPI_SUCCESS(rv)) {
120 aprint_debug_dev(sc->sc_dev, "C%u: _CSD, "
121 "domain 0x%02x / 0x%02x, type 0x%02x\n",
122 dep.dep_index, dep.dep_domain,
123 dep.dep_ncpu, dep.dep_coord);
124 }
125
126 aprint_debug_dev(sc->sc_dev, "Cx: %5s",
127 (sc->sc_flags & ACPICPU_FLAG_C_FADT) != 0 ? "FADT" : "_CST");
128
129 if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
130 aprint_debug(", BM control");
131
132 if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
133 aprint_debug(", BM arbitration");
134
135 if ((sc->sc_flags & ACPICPU_FLAG_C_C1E) != 0)
136 aprint_debug(", C1E");
137
138 if ((sc->sc_flags & ACPICPU_FLAG_C_NOC3) != 0)
139 aprint_debug(", C3 disabled (quirk)");
140
141 aprint_debug("\n");
142
143 for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
144
145 cs = &sc->sc_cstate[i];
146
147 if (cs->cs_method == 0)
148 continue;
149
150 switch (cs->cs_method) {
151
152 case ACPICPU_C_STATE_HALT:
153 method = "HALT";
154 break;
155
156 case ACPICPU_C_STATE_FFH:
157 method = "FFH";
158 break;
159
160 case ACPICPU_C_STATE_SYSIO:
161 method = "SYSIO";
162 break;
163
164 default:
165 panic("NOTREACHED");
166 }
167
168 aprint_debug_dev(sc->sc_dev, "C%d: %5s, "
169 "latency %4u, power %4u, addr 0x%06x, flags 0x%02x\n",
170 i, method, cs->cs_latency, cs->cs_power,
171 (uint32_t)cs->cs_addr, cs->cs_flags);
172 }
173 }
174
175 int
176 acpicpu_cstate_detach(device_t self)
177 {
178 struct acpicpu_softc *sc = device_private(self);
179 static ONCE_DECL(once_detach);
180 int rv;
181
182 rv = RUN_ONCE(&once_detach, acpicpu_md_idle_stop);
183
184 if (rv != 0)
185 return rv;
186
187 sc->sc_flags &= ~ACPICPU_FLAG_C;
188
189 return 0;
190 }
191
192 int
193 acpicpu_cstate_start(device_t self)
194 {
195 struct acpicpu_softc *sc = device_private(self);
196 static ONCE_DECL(once_start);
197 static ONCE_DECL(once_save);
198 int rv;
199
200 /*
201 * Save the existing idle-mechanism and claim the idle_loop(9).
202 * This should be called after all ACPI CPUs have been attached.
203 */
204 rv = RUN_ONCE(&once_save, acpicpu_md_idle_init);
205
206 if (rv != 0)
207 return rv;
208
209 rv = RUN_ONCE(&once_start, acpicpu_md_idle_start);
210
211 if (rv == 0)
212 sc->sc_flags |= ACPICPU_FLAG_C;
213
214 return rv;
215 }
216
217 bool
218 acpicpu_cstate_suspend(device_t self)
219 {
220
221 return true;
222 }
223
224 bool
225 acpicpu_cstate_resume(device_t self)
226 {
227 static const ACPI_OSD_EXEC_CALLBACK func = acpicpu_cstate_callback;
228 struct acpicpu_softc *sc = device_private(self);
229
230 KASSERT((sc->sc_flags & ACPICPU_FLAG_C) != 0);
231
232 if ((sc->sc_flags & ACPICPU_FLAG_C_CST) != 0)
233 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
234
235 return true;
236 }
237
238 void
239 acpicpu_cstate_callback(void *aux)
240 {
241 struct acpicpu_softc *sc;
242 device_t self = aux;
243
244 sc = device_private(self);
245
246 KASSERT((sc->sc_flags & ACPICPU_FLAG_C) != 0);
247
248 if ((sc->sc_flags & ACPICPU_FLAG_C_FADT) != 0) {
249 KASSERT((sc->sc_flags & ACPICPU_FLAG_C_CST) == 0);
250 return;
251 }
252
253 mutex_enter(&sc->sc_mtx);
254 (void)acpicpu_cstate_cst(sc);
255 mutex_exit(&sc->sc_mtx);
256 }
257
258 static ACPI_STATUS
259 acpicpu_cstate_cst(struct acpicpu_softc *sc)
260 {
261 ACPI_OBJECT *elm, *obj;
262 ACPI_BUFFER buf;
263 ACPI_STATUS rv;
264 uint32_t i, n;
265 uint8_t count;
266
267 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_CST", &buf);
268
269 if (ACPI_FAILURE(rv))
270 return rv;
271
272 obj = buf.Pointer;
273
274 if (obj->Type != ACPI_TYPE_PACKAGE) {
275 rv = AE_TYPE;
276 goto out;
277 }
278
279 if (obj->Package.Count < 2) {
280 rv = AE_LIMIT;
281 goto out;
282 }
283
284 elm = obj->Package.Elements;
285
286 if (elm[0].Type != ACPI_TYPE_INTEGER) {
287 rv = AE_TYPE;
288 goto out;
289 }
290
291 n = elm[0].Integer.Value;
292
293 if (n != obj->Package.Count - 1) {
294 rv = AE_BAD_VALUE;
295 goto out;
296 }
297
298 if (n > ACPI_C_STATES_MAX) {
299 rv = AE_LIMIT;
300 goto out;
301 }
302
303 (void)memset(sc->sc_cstate, 0,
304 sizeof(*sc->sc_cstate) * ACPI_C_STATE_COUNT);
305
306 CTASSERT(ACPI_STATE_C0 == 0 && ACPI_STATE_C1 == 1);
307 CTASSERT(ACPI_STATE_C2 == 2 && ACPI_STATE_C3 == 3);
308
309 for (count = 0, i = 1; i <= n; i++) {
310
311 elm = &obj->Package.Elements[i];
312 rv = acpicpu_cstate_cst_add(sc, elm);
313
314 if (ACPI_SUCCESS(rv))
315 count++;
316 }
317
318 rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
319
320 out:
321 if (buf.Pointer != NULL)
322 ACPI_FREE(buf.Pointer);
323
324 if (ACPI_FAILURE(rv))
325 aprint_error_dev(sc->sc_dev, "failed to evaluate "
326 "_CST: %s\n", AcpiFormatException(rv));
327
328 return rv;
329 }
330
331 static ACPI_STATUS
332 acpicpu_cstate_cst_add(struct acpicpu_softc *sc, ACPI_OBJECT *elm)
333 {
334 const struct acpicpu_object *ao = &sc->sc_object;
335 struct acpicpu_cstate *cs = sc->sc_cstate;
336 struct acpicpu_cstate state;
337 struct acpicpu_reg *reg;
338 ACPI_STATUS rv = AE_OK;
339 ACPI_OBJECT *obj;
340 uint32_t type;
341
342 (void)memset(&state, 0, sizeof(*cs));
343
344 state.cs_flags = ACPICPU_FLAG_C_BM_STS;
345
346 if (elm->Type != ACPI_TYPE_PACKAGE) {
347 rv = AE_TYPE;
348 goto out;
349 }
350
351 if (elm->Package.Count != 4) {
352 rv = AE_LIMIT;
353 goto out;
354 }
355
356 /*
357 * Type.
358 */
359 obj = &elm->Package.Elements[1];
360
361 if (obj->Type != ACPI_TYPE_INTEGER) {
362 rv = AE_TYPE;
363 goto out;
364 }
365
366 type = obj->Integer.Value;
367
368 if (type < ACPI_STATE_C1 || type > ACPI_STATE_C3) {
369 rv = AE_TYPE;
370 goto out;
371 }
372
373 /*
374 * Latency.
375 */
376 obj = &elm->Package.Elements[2];
377
378 if (obj->Type != ACPI_TYPE_INTEGER) {
379 rv = AE_TYPE;
380 goto out;
381 }
382
383 state.cs_latency = obj->Integer.Value;
384
385 /*
386 * Power.
387 */
388 obj = &elm->Package.Elements[3];
389
390 if (obj->Type != ACPI_TYPE_INTEGER) {
391 rv = AE_TYPE;
392 goto out;
393 }
394
395 state.cs_power = obj->Integer.Value;
396
397 /*
398 * Register.
399 */
400 obj = &elm->Package.Elements[0];
401
402 if (obj->Type != ACPI_TYPE_BUFFER) {
403 rv = AE_TYPE;
404 goto out;
405 }
406
407 CTASSERT(sizeof(struct acpicpu_reg) == 15);
408
409 if (obj->Buffer.Length < sizeof(struct acpicpu_reg)) {
410 rv = AE_LIMIT;
411 goto out;
412 }
413
414 reg = (struct acpicpu_reg *)obj->Buffer.Pointer;
415
416 switch (reg->reg_spaceid) {
417
418 case ACPI_ADR_SPACE_SYSTEM_IO:
419 state.cs_method = ACPICPU_C_STATE_SYSIO;
420
421 if (reg->reg_addr == 0) {
422 rv = AE_AML_ILLEGAL_ADDRESS;
423 goto out;
424 }
425
426 if (reg->reg_bitwidth != 8) {
427 rv = AE_AML_BAD_RESOURCE_LENGTH;
428 goto out;
429 }
430
431 /*
432 * Check only that the address is in the mapped space.
433 * Systems are allowed to change it when operating
434 * with _CST (see ACPI 4.0, pp. 94-95). For instance,
435 * the offset of P_LVL3 may change depending on whether
436 * acpiacad(4) is connected or disconnected.
437 */
438 if (reg->reg_addr > ao->ao_pblkaddr + ao->ao_pblklen) {
439 rv = AE_BAD_ADDRESS;
440 goto out;
441 }
442
443 state.cs_addr = reg->reg_addr;
444 break;
445
446 case ACPI_ADR_SPACE_FIXED_HARDWARE:
447 state.cs_method = ACPICPU_C_STATE_FFH;
448
449 switch (type) {
450
451 case ACPI_STATE_C1:
452
453 if ((sc->sc_flags & ACPICPU_FLAG_C_MWAIT) == 0)
454 state.cs_method = ACPICPU_C_STATE_HALT;
455
456 break;
457
458 default:
459
460 if ((sc->sc_flags & ACPICPU_FLAG_C_MWAIT) == 0) {
461 rv = AE_AML_BAD_RESOURCE_VALUE;
462 goto out;
463 }
464 }
465
466 if (sc->sc_cap != 0) {
467
468 /*
469 * The _CST FFH GAS encoding may contain
470 * additional hints on Intel processors.
471 * Use these to determine whether we can
472 * avoid the bus master activity check.
473 */
474 if ((reg->reg_accesssize & ACPICPU_PDC_GAS_BM) == 0)
475 state.cs_flags &= ~ACPICPU_FLAG_C_BM_STS;
476 }
477
478 break;
479
480 default:
481 rv = AE_AML_INVALID_SPACE_ID;
482 goto out;
483 }
484
485 if (cs[type].cs_method != 0) {
486 rv = AE_ALREADY_EXISTS;
487 goto out;
488 }
489
490 cs[type].cs_addr = state.cs_addr;
491 cs[type].cs_power = state.cs_power;
492 cs[type].cs_flags = state.cs_flags;
493 cs[type].cs_method = state.cs_method;
494 cs[type].cs_latency = state.cs_latency;
495
496 out:
497 if (ACPI_FAILURE(rv))
498 aprint_verbose_dev(sc->sc_dev,
499 "invalid _CST: %s\n", AcpiFormatException(rv));
500
501 return rv;
502 }
503
504 static void
505 acpicpu_cstate_cst_bios(void)
506 {
507 const uint8_t val = AcpiGbl_FADT.CstControl;
508 const uint32_t addr = AcpiGbl_FADT.SmiCommand;
509
510 if (addr == 0)
511 return;
512
513 (void)AcpiOsWritePort(addr, val, 8);
514 }
515
516 static ACPI_STATUS
517 acpicpu_cstate_csd(ACPI_HANDLE hdl, struct acpicpu_dep *dep)
518 {
519 ACPI_OBJECT *elm, *obj;
520 ACPI_BUFFER buf;
521 ACPI_STATUS rv;
522 int i, n;
523
524 /*
525 * Query the optional _CSD for heuristics.
526 */
527 rv = acpi_eval_struct(hdl, "_CSD", &buf);
528
529 if (ACPI_FAILURE(rv))
530 return rv;
531
532 obj = buf.Pointer;
533
534 if (obj->Type != ACPI_TYPE_PACKAGE) {
535 rv = AE_TYPE;
536 goto out;
537 }
538
539 n = obj->Package.Count;
540
541 if (n != 6) {
542 rv = AE_LIMIT;
543 goto out;
544 }
545
546 elm = obj->Package.Elements;
547
548 for (i = 0; i < n; i++) {
549
550 if (elm[i].Type != ACPI_TYPE_INTEGER) {
551 rv = AE_TYPE;
552 goto out;
553 }
554
555 KDASSERT((uint64_t)elm[i].Integer.Value <= UINT32_MAX);
556 }
557
558 if (elm[0].Integer.Value != 6 || elm[1].Integer.Value != 0) {
559 rv = AE_BAD_DATA;
560 goto out;
561 }
562
563 dep->dep_domain = elm[2].Integer.Value;
564 dep->dep_coord = elm[3].Integer.Value;
565 dep->dep_ncpu = elm[4].Integer.Value;
566 dep->dep_index = elm[5].Integer.Value;
567
568 out:
569 if (buf.Pointer != NULL)
570 ACPI_FREE(buf.Pointer);
571
572 return rv;
573 }
574
575 static void
576 acpicpu_cstate_fadt(struct acpicpu_softc *sc)
577 {
578 struct acpicpu_cstate *cs = sc->sc_cstate;
579
580 (void)memset(cs, 0, sizeof(*cs) * ACPI_C_STATE_COUNT);
581
582 /*
583 * All x86 processors should support C1 (a.k.a. HALT).
584 */
585 if ((AcpiGbl_FADT.Flags & ACPI_FADT_C1_SUPPORTED) != 0)
586 cs[ACPI_STATE_C1].cs_method = ACPICPU_C_STATE_HALT;
587
588 if ((acpicpu_md_cpus_running() > 1) &&
589 (AcpiGbl_FADT.Flags & ACPI_FADT_C2_MP_SUPPORTED) == 0)
590 return;
591
592 cs[ACPI_STATE_C2].cs_method = ACPICPU_C_STATE_SYSIO;
593 cs[ACPI_STATE_C3].cs_method = ACPICPU_C_STATE_SYSIO;
594
595 cs[ACPI_STATE_C2].cs_latency = AcpiGbl_FADT.C2Latency;
596 cs[ACPI_STATE_C3].cs_latency = AcpiGbl_FADT.C3Latency;
597
598 cs[ACPI_STATE_C2].cs_addr = sc->sc_object.ao_pblkaddr + 4;
599 cs[ACPI_STATE_C3].cs_addr = sc->sc_object.ao_pblkaddr + 5;
600
601 /*
602 * The P_BLK length should always be 6. If it
603 * is not, reduce functionality accordingly.
604 * Sanity check also FADT's latency levels.
605 */
606 if (sc->sc_object.ao_pblklen < 5)
607 cs[ACPI_STATE_C2].cs_method = 0;
608
609 if (sc->sc_object.ao_pblklen < 6)
610 cs[ACPI_STATE_C3].cs_method = 0;
611
612 CTASSERT(ACPICPU_C_C2_LATENCY_MAX == 100);
613 CTASSERT(ACPICPU_C_C3_LATENCY_MAX == 1000);
614
615 if (AcpiGbl_FADT.C2Latency > ACPICPU_C_C2_LATENCY_MAX)
616 cs[ACPI_STATE_C2].cs_method = 0;
617
618 if (AcpiGbl_FADT.C3Latency > ACPICPU_C_C3_LATENCY_MAX)
619 cs[ACPI_STATE_C3].cs_method = 0;
620 }
621
622 static void
623 acpicpu_cstate_quirks(struct acpicpu_softc *sc)
624 {
625 const uint32_t reg = AcpiGbl_FADT.Pm2ControlBlock;
626 const uint32_t len = AcpiGbl_FADT.Pm2ControlLength;
627 struct pci_attach_args pa;
628
629 /*
630 * Check bus master arbitration. If ARB_DIS
631 * is not available, processor caches must be
632 * flushed before C3 (ACPI 4.0, section 8.2).
633 */
634 if (reg != 0 && len != 0)
635 sc->sc_flags |= ACPICPU_FLAG_C_ARB;
636 else {
637 /*
638 * Disable C3 entirely if WBINVD is not present.
639 */
640 if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) == 0)
641 sc->sc_flags |= ACPICPU_FLAG_C_NOC3;
642 else {
643 /*
644 * If WBINVD is present and functioning properly,
645 * flush all processor caches before entering C3.
646 */
647 if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0)
648 sc->sc_flags &= ~ACPICPU_FLAG_C_BM;
649 else
650 sc->sc_flags |= ACPICPU_FLAG_C_NOC3;
651 }
652 }
653
654 /*
655 * There are several erratums for PIIX4.
656 */
657 if (pci_find_device(&pa, acpicpu_cstate_quirks_piix4) != 0)
658 sc->sc_flags |= ACPICPU_FLAG_C_NOC3;
659
660 if ((sc->sc_flags & ACPICPU_FLAG_C_NOC3) != 0)
661 sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
662 }
663
664 static int
665 acpicpu_cstate_quirks_piix4(struct pci_attach_args *pa)
666 {
667
668 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
669 return 0;
670
671 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82371AB_ISA ||
672 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82440MX_PMC)
673 return 1;
674
675 return 0;
676 }
677
678 static int
679 acpicpu_cstate_latency(struct acpicpu_softc *sc)
680 {
681 static const uint32_t cs_factor = 3;
682 struct acpicpu_cstate *cs;
683 int i;
684
685 for (i = cs_state_max; i > 0; i--) {
686
687 cs = &sc->sc_cstate[i];
688
689 if (__predict_false(cs->cs_method == 0))
690 continue;
691
692 /*
693 * Choose a state if we have previously slept
694 * longer than the worst case latency of the
695 * state times an arbitrary multiplier.
696 */
697 if (sc->sc_cstate_sleep > cs->cs_latency * cs_factor)
698 return i;
699 }
700
701 return ACPI_STATE_C1;
702 }
703
704 /*
705 * The main idle loop.
706 */
707 void
708 acpicpu_cstate_idle(void)
709 {
710 struct cpu_info *ci = curcpu();
711 struct acpicpu_softc *sc;
712 int state;
713
714 if (__predict_false(ci->ci_want_resched) != 0)
715 return;
716
717 acpi_md_OsDisableInterrupt();
718
719 KASSERT(acpicpu_sc != NULL);
720 KASSERT(ci->ci_acpiid < maxcpus);
721 KASSERT(ci->ci_ilevel == IPL_NONE);
722
723 sc = acpicpu_sc[ci->ci_acpiid];
724
725 if (__predict_false(sc == NULL))
726 goto halt;
727
728 if (__predict_false(sc->sc_cold != false))
729 goto halt;
730
731 if (__predict_false((sc->sc_flags & ACPICPU_FLAG_C) == 0))
732 goto halt;
733
734 if (__predict_false(mutex_tryenter(&sc->sc_mtx) == 0))
735 goto halt;
736
737 mutex_exit(&sc->sc_mtx);
738 state = acpicpu_cstate_latency(sc);
739
740 /*
741 * Check for bus master activity. Note that particularly usb(4)
742 * causes high activity, which may prevent the use of C3 states.
743 */
744 if ((sc->sc_cstate[state].cs_flags & ACPICPU_FLAG_C_BM_STS) != 0) {
745
746 if (acpicpu_cstate_bm_check() != false)
747 state--;
748
749 if (__predict_false(sc->sc_cstate[state].cs_method == 0))
750 state = ACPI_STATE_C1;
751 }
752
753 KASSERT(state != ACPI_STATE_C0);
754
755 if (state != ACPI_STATE_C3) {
756 acpicpu_cstate_idle_enter(sc, state);
757 return;
758 }
759
760 /*
761 * On all recent (Intel) CPUs caches are shared
762 * by CPUs and bus master control is required to
763 * keep these coherent while in C3. Flushing the
764 * CPU caches is only the last resort.
765 */
766 if ((sc->sc_flags & ACPICPU_FLAG_C_BM) == 0)
767 ACPI_FLUSH_CPU_CACHE();
768
769 /*
770 * Allow the bus master to request that any given
771 * CPU should return immediately to C0 from C3.
772 */
773 if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
774 (void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
775
776 /*
777 * It may be necessary to disable bus master arbitration
778 * to ensure that bus master cycles do not occur while
779 * sleeping in C3 (see ACPI 4.0, section 8.1.4).
780 */
781 if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
782 (void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
783
784 acpicpu_cstate_idle_enter(sc, state);
785
786 /*
787 * Disable bus master wake and re-enable the arbiter.
788 */
789 if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
790 (void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
791
792 if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
793 (void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
794
795 return;
796
797 halt:
798 acpicpu_md_idle_enter(ACPICPU_C_STATE_HALT, ACPI_STATE_C1);
799 }
800
801 static void
802 acpicpu_cstate_idle_enter(struct acpicpu_softc *sc, int state)
803 {
804 struct acpicpu_cstate *cs = &sc->sc_cstate[state];
805 uint32_t end, start, val;
806
807 start = acpitimer_read_safe(NULL);
808
809 switch (cs->cs_method) {
810
811 case ACPICPU_C_STATE_FFH:
812 case ACPICPU_C_STATE_HALT:
813 acpicpu_md_idle_enter(cs->cs_method, state);
814 break;
815
816 case ACPICPU_C_STATE_SYSIO:
817 (void)AcpiOsReadPort(cs->cs_addr, &val, 8);
818 break;
819
820 default:
821 acpicpu_md_idle_enter(ACPICPU_C_STATE_HALT, ACPI_STATE_C1);
822 break;
823 }
824
825 cs->cs_stat++;
826
827 end = acpitimer_read_safe(NULL);
828 sc->sc_cstate_sleep = hztoms(acpitimer_delta(end, start)) * 1000;
829
830 acpi_md_OsEnableInterrupt();
831 }
832
833 static bool
834 acpicpu_cstate_bm_check(void)
835 {
836 uint32_t val = 0;
837 ACPI_STATUS rv;
838
839 rv = AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &val);
840
841 if (ACPI_FAILURE(rv) || val == 0)
842 return false;
843
844 (void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
845
846 return true;
847 }
848