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