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