xref: /dflybsd-src/sys/dev/acpica/acpi_cpu_cstate.c (revision fed0eead9b6fe9540972cbf8f7ce8525415b3a0c)
1 /*-
2  * Copyright (c) 2003-2005 Nate Lawson (SDG)
3  * Copyright (c) 2001 Michael Smith
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/acpica/acpi_cpu.c,v 1.72 2008/04/12 12:06:00 rpaulo Exp $
28  */
29 
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/globaldata.h>
36 #include <sys/power.h>
37 #include <sys/proc.h>
38 #include <sys/sbuf.h>
39 #include <sys/thread2.h>
40 #include <sys/serialize.h>
41 
42 #include <bus/pci/pcivar.h>
43 #include <machine/atomic.h>
44 #include <machine/globaldata.h>
45 #include <machine/md_var.h>
46 #include <machine/smp.h>
47 #include <sys/rman.h>
48 
49 #include "acpi.h"
50 #include "acpivar.h"
51 #include "acpi_cpu.h"
52 
53 /*
54  * Support for ACPI Processor devices, including C[1-3] sleep states.
55  */
56 
57 /* Hooks for the ACPI CA debugging infrastructure */
58 #define _COMPONENT	ACPI_PROCESSOR
59 ACPI_MODULE_NAME("PROCESSOR")
60 
61 struct acpi_cx {
62     struct resource	*p_lvlx;	/* Register to read to enter state. */
63     int			 rid;		/* rid of p_lvlx */
64     uint32_t		 type;		/* C1-3 (C4 and up treated as C3). */
65     uint32_t		 trans_lat;	/* Transition latency (usec). */
66     uint32_t		 power;		/* Power consumed (mW). */
67     int			 res_type;	/* Resource type for p_lvlx. */
68 };
69 #define MAX_CX_STATES	 8
70 
71 struct acpi_cpu_softc {
72     device_t		 cpu_dev;
73     struct acpi_cpux_softc *cpu_parent;
74     ACPI_HANDLE		 cpu_handle;
75     int			 cpu_id;
76     uint32_t		 cpu_p_blk;	/* ACPI P_BLK location */
77     uint32_t		 cpu_p_blk_len;	/* P_BLK length (must be 6). */
78     struct acpi_cx	 cpu_cx_states[MAX_CX_STATES];
79     int			 cpu_cx_count;	/* Number of valid Cx states. */
80     int			 cpu_prev_sleep;/* Last idle sleep duration. */
81     /* Runtime state. */
82     int			 cpu_non_c3;	/* Index of lowest non-C3 state. */
83     u_long		 cpu_cx_stats[MAX_CX_STATES];/* Cx usage history. */
84     /* Values for sysctl. */
85     int			 cpu_cx_lowest; /* Current Cx lowest */
86     int			 cpu_cx_lowest_req; /* Requested Cx lowest */
87     char 		 cpu_cx_supported[64];
88 };
89 
90 struct acpi_cpu_device {
91     struct resource_list	ad_rl;
92 };
93 
94 #define CPU_GET_REG(reg, width) 					\
95     (bus_space_read_ ## width(rman_get_bustag((reg)), 			\
96 		      rman_get_bushandle((reg)), 0))
97 #define CPU_SET_REG(reg, width, val)					\
98     (bus_space_write_ ## width(rman_get_bustag((reg)), 			\
99 		       rman_get_bushandle((reg)), 0, (val)))
100 
101 #define PM_USEC(x)	 ((x) >> 2)	/* ~4 clocks per usec (3.57955 Mhz) */
102 
103 #define ACPI_NOTIFY_CX_STATES	0x81	/* _CST changed. */
104 
105 #define CPU_QUIRK_NO_C3		(1<<0)	/* C3-type states are not usable. */
106 #define CPU_QUIRK_NO_BM_CTRL	(1<<2)	/* No bus mastering control. */
107 
108 #define PCI_VENDOR_INTEL	0x8086
109 #define PCI_DEVICE_82371AB_3	0x7113	/* PIIX4 chipset for quirks. */
110 #define PCI_REVISION_A_STEP	0
111 #define PCI_REVISION_B_STEP	1
112 #define PCI_REVISION_4E		2
113 #define PCI_REVISION_4M		3
114 #define PIIX4_DEVACTB_REG	0x58
115 #define PIIX4_BRLD_EN_IRQ0	(1<<0)
116 #define PIIX4_BRLD_EN_IRQ	(1<<1)
117 #define PIIX4_BRLD_EN_IRQ8	(1<<5)
118 #define PIIX4_STOP_BREAK_MASK	(PIIX4_BRLD_EN_IRQ0 | PIIX4_BRLD_EN_IRQ | PIIX4_BRLD_EN_IRQ8)
119 #define PIIX4_PCNTRL_BST_EN	(1<<10)
120 
121 /* Platform hardware resource information. */
122 static uint32_t		 cpu_smi_cmd;	/* Value to write to SMI_CMD. */
123 static uint8_t		 cpu_cst_cnt;	/* Indicate we are _CST aware. */
124 static int		 cpu_quirks;	/* Indicate any hardware bugs. */
125 
126 /* Runtime state. */
127 static int		 cpu_disable_idle; /* Disable entry to idle function */
128 static int		 cpu_cx_count;	/* Number of valid Cx states */
129 
130 /* Values for sysctl. */
131 static int		 cpu_cx_generic;
132 static int		 cpu_cx_lowest; /* Current Cx lowest */
133 static int		 cpu_cx_lowest_req; /* Requested Cx lowest */
134 static struct lwkt_serialize cpu_cx_slize = LWKT_SERIALIZE_INITIALIZER;
135 
136 /* C3 state transition */
137 static int		 cpu_c3_ncpus;
138 
139 static device_t		*cpu_devices;
140 static int		 cpu_ndevices;
141 static struct acpi_cpu_softc **cpu_softc;
142 
143 static int	acpi_cpu_cst_probe(device_t dev);
144 static int	acpi_cpu_cst_attach(device_t dev);
145 static int	acpi_cpu_cst_suspend(device_t dev);
146 static int	acpi_cpu_cst_resume(device_t dev);
147 static struct resource_list *acpi_cpu_cst_get_rlist(device_t dev,
148 		    device_t child);
149 static device_t	acpi_cpu_cst_add_child(device_t bus, device_t parent,
150 		    int order, const char *name, int unit);
151 static int	acpi_cpu_cst_read_ivar(device_t dev, device_t child,
152 		    int index, uintptr_t *result);
153 static int	acpi_cpu_cst_shutdown(device_t dev);
154 static void	acpi_cpu_cx_probe(struct acpi_cpu_softc *sc);
155 static void	acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc);
156 static int	acpi_cpu_cx_cst(struct acpi_cpu_softc *sc);
157 static void	acpi_cpu_startup(void *arg);
158 static void	acpi_cpu_startup_cx(struct acpi_cpu_softc *sc);
159 static void	acpi_cpu_cx_list(struct acpi_cpu_softc *sc);
160 static void	acpi_cpu_idle(void);
161 static void	acpi_cpu_cst_notify(device_t);
162 static int	acpi_cpu_quirks(void);
163 static int	acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS);
164 static int	acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val);
165 static int	acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
166 static int	acpi_cpu_cx_lowest_use_sysctl(SYSCTL_HANDLER_ARGS);
167 static int	acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
168 static int	acpi_cpu_global_cx_lowest_use_sysctl(SYSCTL_HANDLER_ARGS);
169 static void	acpi_cpu_cx_non_c3(struct acpi_cpu_softc *sc);
170 
171 static void	acpi_cpu_c1(void);	/* XXX */
172 
173 static device_method_t acpi_cpu_cst_methods[] = {
174     /* Device interface */
175     DEVMETHOD(device_probe,	acpi_cpu_cst_probe),
176     DEVMETHOD(device_attach,	acpi_cpu_cst_attach),
177     DEVMETHOD(device_detach,	bus_generic_detach),
178     DEVMETHOD(device_shutdown,	acpi_cpu_cst_shutdown),
179     DEVMETHOD(device_suspend,	acpi_cpu_cst_suspend),
180     DEVMETHOD(device_resume,	acpi_cpu_cst_resume),
181 
182     /* Bus interface */
183     DEVMETHOD(bus_add_child,	acpi_cpu_cst_add_child),
184     DEVMETHOD(bus_read_ivar,	acpi_cpu_cst_read_ivar),
185     DEVMETHOD(bus_get_resource_list, acpi_cpu_cst_get_rlist),
186     DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
187     DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
188     DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
189     DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
190     DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
191     DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
192     DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
193     DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
194     DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
195     DEVMETHOD_END
196 };
197 
198 static driver_t acpi_cpu_cst_driver = {
199     "cpu_cst",
200     acpi_cpu_cst_methods,
201     sizeof(struct acpi_cpu_softc),
202 };
203 
204 static devclass_t acpi_cpu_cst_devclass;
205 DRIVER_MODULE(cpu_cst, cpu, acpi_cpu_cst_driver, acpi_cpu_cst_devclass, NULL, NULL);
206 MODULE_DEPEND(cpu_cst, acpi, 1, 1, 1);
207 
208 static int
209 acpi_cpu_cst_probe(device_t dev)
210 {
211     int cpu_id;
212 
213     if (acpi_disabled("cpu_cst") || acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
214 	return (ENXIO);
215 
216     cpu_id = acpi_get_magic(dev);
217 
218     if (cpu_softc == NULL)
219 	cpu_softc = kmalloc(sizeof(struct acpi_cpu_softc *) *
220 	    SMP_MAXCPU, M_TEMP /* XXX */, M_INTWAIT | M_ZERO);
221 
222     /*
223      * Check if we already probed this processor.  We scan the bus twice
224      * so it's possible we've already seen this one.
225      */
226     if (cpu_softc[cpu_id] != NULL) {
227 	device_printf(dev, "CPU%d cstate already exist\n", cpu_id);
228 	return (ENXIO);
229     }
230 
231     /* Mark this processor as in-use and save our derived id for attach. */
232     cpu_softc[cpu_id] = (void *)1;
233     device_set_desc(dev, "ACPI CPU C-State");
234 
235     return (0);
236 }
237 
238 static int
239 acpi_cpu_cst_attach(device_t dev)
240 {
241     ACPI_BUFFER		   buf;
242     ACPI_OBJECT		   *obj;
243     struct acpi_cpu_softc *sc;
244     ACPI_STATUS		   status;
245 
246     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
247 
248     sc = device_get_softc(dev);
249     sc->cpu_dev = dev;
250     sc->cpu_parent = device_get_softc(device_get_parent(dev));
251     sc->cpu_handle = acpi_get_handle(dev);
252     sc->cpu_id = acpi_get_magic(dev);
253     cpu_softc[sc->cpu_id] = sc;
254     cpu_smi_cmd = AcpiGbl_FADT.SmiCommand;
255     cpu_cst_cnt = AcpiGbl_FADT.CstControl;
256 
257     buf.Pointer = NULL;
258     buf.Length = ACPI_ALLOCATE_BUFFER;
259     status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf);
260     if (ACPI_FAILURE(status)) {
261 	device_printf(dev, "attach failed to get Processor obj - %s\n",
262 		      AcpiFormatException(status));
263 	return (ENXIO);
264     }
265     obj = (ACPI_OBJECT *)buf.Pointer;
266     sc->cpu_p_blk = obj->Processor.PblkAddress;
267     sc->cpu_p_blk_len = obj->Processor.PblkLength;
268     AcpiOsFree(obj);
269     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_BLK at %#x/%d\n",
270 		     device_get_unit(dev), sc->cpu_p_blk, sc->cpu_p_blk_len));
271 
272     /*
273      * If this is the first cpu we attach, create and initialize the generic
274      * resources that will be used by all acpi cpu devices.
275      */
276     if (device_get_unit(dev) == 0) {
277 	/* Assume we won't be using generic Cx mode by default */
278 	cpu_cx_generic = FALSE;
279 
280 	/* Queue post cpu-probing task handler */
281 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cpu_startup, NULL);
282     }
283 
284     /* Probe for Cx state support. */
285     acpi_cpu_cx_probe(sc);
286 
287     /* Finally,  call identify and probe/attach for child devices. */
288     bus_generic_probe(dev);
289     bus_generic_attach(dev);
290 
291     return (0);
292 }
293 
294 /*
295  * Disable any entry to the idle function during suspend and re-enable it
296  * during resume.
297  */
298 static int
299 acpi_cpu_cst_suspend(device_t dev)
300 {
301     int error;
302 
303     error = bus_generic_suspend(dev);
304     if (error)
305 	return (error);
306     cpu_disable_idle = TRUE;
307     return (0);
308 }
309 
310 static int
311 acpi_cpu_cst_resume(device_t dev)
312 {
313 
314     cpu_disable_idle = FALSE;
315     return (bus_generic_resume(dev));
316 }
317 
318 static struct resource_list *
319 acpi_cpu_cst_get_rlist(device_t dev, device_t child)
320 {
321     struct acpi_cpu_device *ad;
322 
323     ad = device_get_ivars(child);
324     if (ad == NULL)
325 	return (NULL);
326     return (&ad->ad_rl);
327 }
328 
329 static device_t
330 acpi_cpu_cst_add_child(device_t bus, device_t parent, int order,
331     const char *name, int unit)
332 {
333     struct acpi_cpu_device *ad;
334     device_t child;
335 
336     if ((ad = kmalloc(sizeof(*ad), M_TEMP, M_NOWAIT | M_ZERO)) == NULL)
337 	return (NULL);
338 
339     resource_list_init(&ad->ad_rl);
340 
341     child = device_add_child_ordered(parent, order, name, unit);
342     if (child != NULL)
343 	device_set_ivars(child, ad);
344     else
345 	kfree(ad, M_TEMP);
346     return (child);
347 }
348 
349 static int
350 acpi_cpu_cst_read_ivar(device_t dev, device_t child, int index,
351     uintptr_t *result)
352 {
353     struct acpi_cpu_softc *sc;
354 
355     sc = device_get_softc(dev);
356     switch (index) {
357     case ACPI_IVAR_HANDLE:
358 	*result = (uintptr_t)sc->cpu_handle;
359 	break;
360 #if 0
361     case CPU_IVAR_PCPU:
362 	*result = (uintptr_t)sc->cpu_pcpu;
363 	break;
364 #endif
365     default:
366 	return (ENOENT);
367     }
368     return (0);
369 }
370 
371 static int
372 acpi_cpu_cst_shutdown(device_t dev)
373 {
374     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
375 
376     /* Allow children to shutdown first. */
377     bus_generic_shutdown(dev);
378 
379     /*
380      * Disable any entry to the idle function.  There is a small race where
381      * an idle thread have passed this check but not gone to sleep.  This
382      * is ok since device_shutdown() does not free the softc, otherwise
383      * we'd have to be sure all threads were evicted before returning.
384      */
385     cpu_disable_idle = TRUE;
386 
387     return_VALUE (0);
388 }
389 
390 static void
391 acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
392 {
393     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
394 
395     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
396     sc->cpu_prev_sleep = 1000000;
397     sc->cpu_cx_lowest = 0;
398     sc->cpu_cx_lowest_req = 0;
399 
400     /*
401      * Check for the ACPI 2.0 _CST sleep states object. If we can't find
402      * any, we'll revert to generic FADT/P_BLK Cx control method which will
403      * be handled by acpi_cpu_startup. We need to defer to after having
404      * probed all the cpus in the system before probing for generic Cx
405      * states as we may already have found cpus with valid _CST packages
406      */
407     if (!cpu_cx_generic && acpi_cpu_cx_cst(sc) != 0) {
408 	/*
409 	 * We were unable to find a _CST package for this cpu or there
410 	 * was an error parsing it. Switch back to generic mode.
411 	 */
412 	cpu_cx_generic = TRUE;
413 	if (bootverbose)
414 	    device_printf(sc->cpu_dev, "switching to generic Cx mode\n");
415     }
416 
417     /*
418      * TODO: _CSD Package should be checked here.
419      */
420 }
421 
422 static void
423 acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc)
424 {
425     ACPI_GENERIC_ADDRESS	 gas;
426     struct acpi_cx		*cx_ptr;
427 
428     sc->cpu_cx_count = 0;
429     cx_ptr = sc->cpu_cx_states;
430 
431     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
432     sc->cpu_prev_sleep = 1000000;
433 
434     /* C1 has been required since just after ACPI 1.0 */
435     cx_ptr->type = ACPI_STATE_C1;
436     cx_ptr->trans_lat = 0;
437     cx_ptr++;
438     sc->cpu_cx_count++;
439 
440     /*
441      * The spec says P_BLK must be 6 bytes long.  However, some systems
442      * use it to indicate a fractional set of features present so we
443      * take 5 as C2.  Some may also have a value of 7 to indicate
444      * another C3 but most use _CST for this (as required) and having
445      * "only" C1-C3 is not a hardship.
446      */
447     if (sc->cpu_p_blk_len < 5)
448 	return;
449 
450     /* Validate and allocate resources for C2 (P_LVL2). */
451     gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
452     gas.BitWidth = 8;
453     if (AcpiGbl_FADT.C2Latency <= 100) {
454 	gas.Address = sc->cpu_p_blk + 4;
455 
456 	cx_ptr->rid = sc->cpu_parent->cpux_next_rid;
457 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->type, &cx_ptr->rid, &gas, &cx_ptr->p_lvlx,
458 					    RF_SHAREABLE);
459 	if (cx_ptr->p_lvlx != NULL) {
460 	    sc->cpu_parent->cpux_next_rid++;
461 	    cx_ptr->type = ACPI_STATE_C2;
462 	    cx_ptr->trans_lat = AcpiGbl_FADT.C2Latency;
463 	    cx_ptr++;
464 	    sc->cpu_cx_count++;
465 	    sc->cpu_non_c3 = 1;
466 	}
467     }
468     if (sc->cpu_p_blk_len < 6)
469 	return;
470 
471     /* Validate and allocate resources for C3 (P_LVL3). */
472     if (AcpiGbl_FADT.C3Latency <= 1000 && !(cpu_quirks & CPU_QUIRK_NO_C3)) {
473 	gas.Address = sc->cpu_p_blk + 5;
474 
475 	cx_ptr->rid = sc->cpu_parent->cpux_next_rid;
476 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->type, &cx_ptr->rid, &gas,
477 					    &cx_ptr->p_lvlx, RF_SHAREABLE);
478 	if (cx_ptr->p_lvlx != NULL) {
479 	    sc->cpu_parent->cpux_next_rid++;
480 	    cx_ptr->type = ACPI_STATE_C3;
481 	    cx_ptr->trans_lat = AcpiGbl_FADT.C3Latency;
482 	    cx_ptr++;
483 	    sc->cpu_cx_count++;
484 	}
485     }
486 }
487 
488 /*
489  * Parse a _CST package and set up its Cx states.  Since the _CST object
490  * can change dynamically, our notify handler may call this function
491  * to clean up and probe the new _CST package.
492  */
493 static int
494 acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
495 {
496     struct	 acpi_cx *cx_ptr;
497     ACPI_STATUS	 status;
498     ACPI_BUFFER	 buf;
499     ACPI_OBJECT	*top;
500     ACPI_OBJECT	*pkg;
501     uint32_t	 count;
502     int		 i;
503 
504     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
505 
506     buf.Pointer = NULL;
507     buf.Length = ACPI_ALLOCATE_BUFFER;
508     status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
509     if (ACPI_FAILURE(status))
510 	return (ENXIO);
511 
512     /* _CST is a package with a count and at least one Cx package. */
513     top = (ACPI_OBJECT *)buf.Pointer;
514     if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
515 	device_printf(sc->cpu_dev, "invalid _CST package\n");
516 	AcpiOsFree(buf.Pointer);
517 	return (ENXIO);
518     }
519     if (count != top->Package.Count - 1) {
520 	device_printf(sc->cpu_dev, "invalid _CST state count (%d != %d)\n",
521 	       count, top->Package.Count - 1);
522 	count = top->Package.Count - 1;
523     }
524     if (count > MAX_CX_STATES) {
525 	device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
526 	count = MAX_CX_STATES;
527     }
528 
529     /* Set up all valid states. */
530     sc->cpu_cx_count = 0;
531     cx_ptr = sc->cpu_cx_states;
532     for (i = 0; i < count; i++) {
533 	pkg = &top->Package.Elements[i + 1];
534 	if (!ACPI_PKG_VALID(pkg, 4) ||
535 	    acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
536 	    acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
537 	    acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
538 
539 	    device_printf(sc->cpu_dev, "skipping invalid Cx state package\n");
540 	    continue;
541 	}
542 
543 	/* Validate the state to see if we should use it. */
544 	switch (cx_ptr->type) {
545 	case ACPI_STATE_C1:
546 	    sc->cpu_non_c3 = i;
547 	    cx_ptr++;
548 	    sc->cpu_cx_count++;
549 	    continue;
550 	case ACPI_STATE_C2:
551 	    sc->cpu_non_c3 = i;
552 	    break;
553 	case ACPI_STATE_C3:
554 	default:
555 	    if ((cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
556 
557 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
558 				 "acpi_cpu%d: C3[%d] not available.\n",
559 				 device_get_unit(sc->cpu_dev), i));
560 		continue;
561 	    }
562 	    break;
563 	}
564 
565 #ifdef notyet
566 	/* Free up any previous register. */
567 	if (cx_ptr->p_lvlx != NULL) {
568 	    bus_release_resource(sc->cpu_dev, 0, 0, cx_ptr->p_lvlx);
569 	    cx_ptr->p_lvlx = NULL;
570 	}
571 #endif
572 
573 	/* Allocate the control register for C2 or C3. */
574 	cx_ptr->rid = sc->cpu_parent->cpux_next_rid;
575 	acpi_PkgGas(sc->cpu_dev, pkg, 0, &cx_ptr->res_type, &cx_ptr->rid, &cx_ptr->p_lvlx,
576 		    RF_SHAREABLE);
577 	if (cx_ptr->p_lvlx) {
578 	    sc->cpu_parent->cpux_next_rid++;
579 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
580 			     "acpi_cpu%d: Got C%d - %d latency\n",
581 			     device_get_unit(sc->cpu_dev), cx_ptr->type,
582 			     cx_ptr->trans_lat));
583 	    cx_ptr++;
584 	    sc->cpu_cx_count++;
585 	}
586     }
587     AcpiOsFree(buf.Pointer);
588 
589     /*
590      * Fix up the lowest Cx being used
591      */
592     if (sc->cpu_cx_lowest_req < sc->cpu_cx_count)
593 	sc->cpu_cx_lowest = sc->cpu_cx_lowest_req;
594     if (sc->cpu_cx_lowest > sc->cpu_cx_count - 1)
595 	sc->cpu_cx_lowest = sc->cpu_cx_count - 1;
596 
597     /*
598      * Cache the lowest non-C3 state.
599      * NOTE: must after cpu_cx_lowest is set.
600      */
601     acpi_cpu_cx_non_c3(sc);
602 
603     return (0);
604 }
605 
606 /*
607  * Call this *after* all CPUs have been attached.
608  */
609 static void
610 acpi_cpu_startup(void *arg)
611 {
612     struct acpi_cpu_softc *sc;
613     int i;
614 
615     /* Get set of CPU devices */
616     devclass_get_devices(acpi_cpu_cst_devclass, &cpu_devices, &cpu_ndevices);
617 
618     /*
619      * Setup any quirks that might necessary now that we have probed
620      * all the CPUs
621      */
622     acpi_cpu_quirks();
623 
624     cpu_cx_count = 0;
625     if (cpu_cx_generic) {
626 	/*
627 	 * We are using generic Cx mode, probe for available Cx states
628 	 * for all processors.
629 	 */
630 	for (i = 0; i < cpu_ndevices; i++) {
631 	    sc = device_get_softc(cpu_devices[i]);
632 	    acpi_cpu_generic_cx_probe(sc);
633 	    if (sc->cpu_cx_count > cpu_cx_count)
634 		cpu_cx_count = sc->cpu_cx_count;
635 	}
636 
637 	/*
638 	 * Find the highest Cx state common to all CPUs
639 	 * in the system, taking quirks into account.
640 	 */
641 	for (i = 0; i < cpu_ndevices; i++) {
642 	    sc = device_get_softc(cpu_devices[i]);
643 	    if (sc->cpu_cx_count < cpu_cx_count)
644 		cpu_cx_count = sc->cpu_cx_count;
645 	}
646     } else {
647 	/*
648 	 * We are using _CST mode, remove C3 state if necessary.
649 	 * Update the largest Cx state supported in the global cpu_cx_count.
650 	 * It will be used in the global Cx sysctl handler.
651 	 * As we now know for sure that we will be using _CST mode
652 	 * install our notify handler.
653 	 */
654 	for (i = 0; i < cpu_ndevices; i++) {
655 	    sc = device_get_softc(cpu_devices[i]);
656 	    if (cpu_quirks & CPU_QUIRK_NO_C3)
657 		sc->cpu_cx_count = sc->cpu_non_c3 + 1;
658 	    if (sc->cpu_cx_count > cpu_cx_count)
659 		cpu_cx_count = sc->cpu_cx_count;
660 	    sc->cpu_parent->cpux_cst_notify = acpi_cpu_cst_notify;
661 	}
662     }
663 
664     /* Perform Cx final initialization. */
665     for (i = 0; i < cpu_ndevices; i++) {
666 	sc = device_get_softc(cpu_devices[i]);
667 	acpi_cpu_startup_cx(sc);
668 
669 	if (sc->cpu_parent->glob_sysctl_tree != NULL) {
670 	    struct acpi_cpux_softc *cpux = sc->cpu_parent;
671 
672 	    /* Add a sysctl handler to handle global Cx lowest setting */
673 	    SYSCTL_ADD_PROC(&cpux->glob_sysctl_ctx,
674 	    		    SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
675 			    OID_AUTO, "cx_lowest",
676 			    CTLTYPE_STRING | CTLFLAG_RW, NULL, 0,
677 			    acpi_cpu_global_cx_lowest_sysctl, "A",
678 			    "Requested global lowest Cx sleep state");
679 	    SYSCTL_ADD_PROC(&cpux->glob_sysctl_ctx,
680 	    		    SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
681 			    OID_AUTO, "cx_lowest_use",
682 			    CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
683 			    acpi_cpu_global_cx_lowest_use_sysctl, "A",
684 			    "Global lowest Cx sleep state to use");
685 	}
686     }
687 
688     /* Take over idling from cpu_idle_default(). */
689     cpu_cx_lowest = 0;
690     cpu_cx_lowest_req = 0;
691     cpu_disable_idle = FALSE;
692     cpu_idle_hook = acpi_cpu_idle;
693 }
694 
695 static void
696 acpi_cpu_cx_list(struct acpi_cpu_softc *sc)
697 {
698     struct sbuf sb;
699     int i;
700 
701     /*
702      * Set up the list of Cx states
703      */
704     sbuf_new(&sb, sc->cpu_cx_supported, sizeof(sc->cpu_cx_supported),
705 	SBUF_FIXEDLEN);
706     for (i = 0; i < sc->cpu_cx_count; i++)
707 	sbuf_printf(&sb, "C%d/%d ", i + 1, sc->cpu_cx_states[i].trans_lat);
708     sbuf_trim(&sb);
709     sbuf_finish(&sb);
710 }
711 
712 static void
713 acpi_cpu_startup_cx(struct acpi_cpu_softc *sc)
714 {
715     struct acpi_cpux_softc *cpux = sc->cpu_parent;
716 
717     acpi_cpu_cx_list(sc);
718 
719     SYSCTL_ADD_STRING(&cpux->pcpu_sysctl_ctx,
720 		      SYSCTL_CHILDREN(cpux->pcpu_sysctl_tree),
721 		      OID_AUTO, "cx_supported", CTLFLAG_RD,
722 		      sc->cpu_cx_supported, 0,
723 		      "Cx/microsecond values for supported Cx states");
724     SYSCTL_ADD_PROC(&cpux->pcpu_sysctl_ctx,
725 		    SYSCTL_CHILDREN(cpux->pcpu_sysctl_tree),
726 		    OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
727 		    (void *)sc, 0, acpi_cpu_cx_lowest_sysctl, "A",
728 		    "requested lowest Cx sleep state");
729     SYSCTL_ADD_PROC(&cpux->pcpu_sysctl_ctx,
730 		    SYSCTL_CHILDREN(cpux->pcpu_sysctl_tree),
731 		    OID_AUTO, "cx_lowest_use", CTLTYPE_STRING | CTLFLAG_RD,
732 		    (void *)sc, 0, acpi_cpu_cx_lowest_use_sysctl, "A",
733 		    "lowest Cx sleep state to use");
734     SYSCTL_ADD_PROC(&cpux->pcpu_sysctl_ctx,
735 		    SYSCTL_CHILDREN(cpux->pcpu_sysctl_tree),
736 		    OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
737 		    (void *)sc, 0, acpi_cpu_usage_sysctl, "A",
738 		    "percent usage for each Cx state");
739 
740 #ifdef notyet
741     /* Signal platform that we can handle _CST notification. */
742     if (!cpu_cx_generic && cpu_cst_cnt != 0) {
743 	ACPI_LOCK(acpi);
744 	AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
745 	ACPI_UNLOCK(acpi);
746     }
747 #endif
748 }
749 
750 /*
751  * Idle the CPU in the lowest state possible.  This function is called with
752  * interrupts disabled.  Note that once it re-enables interrupts, a task
753  * switch can occur so do not access shared data (i.e. the softc) after
754  * interrupts are re-enabled.
755  */
756 static void
757 acpi_cpu_idle(void)
758 {
759     struct	acpi_cpu_softc *sc;
760     struct	acpi_cx *cx_next;
761     uint64_t	start_time, end_time;
762     int		bm_active, cx_next_idx, i;
763 
764     /* If disabled, return immediately. */
765     if (cpu_disable_idle) {
766 	ACPI_ENABLE_IRQS();
767 	return;
768     }
769 
770     /*
771      * Look up our CPU id to get our softc.  If it's NULL, we'll use C1
772      * since there is no ACPI processor object for this CPU.  This occurs
773      * for logical CPUs in the HTT case.
774      */
775     sc = cpu_softc[mdcpu->mi.gd_cpuid];
776     if (sc == NULL) {
777 	acpi_cpu_c1();
778 	return;
779     }
780 
781     /* Find the lowest state that has small enough latency. */
782     cx_next_idx = 0;
783     for (i = sc->cpu_cx_lowest; i >= 0; i--) {
784 	if (sc->cpu_cx_states[i].trans_lat * 3 <= sc->cpu_prev_sleep) {
785 	    cx_next_idx = i;
786 	    break;
787 	}
788     }
789 
790     /*
791      * Check for bus master activity.  If there was activity, clear
792      * the bit and use the lowest non-C3 state.  Note that the USB
793      * driver polling for new devices keeps this bit set all the
794      * time if USB is loaded.
795      */
796     if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
797 	AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active);
798 	if (bm_active != 0) {
799 	    AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
800 	    cx_next_idx = min(cx_next_idx, sc->cpu_non_c3);
801 	}
802     }
803 
804     /* Select the next state and update statistics. */
805     cx_next = &sc->cpu_cx_states[cx_next_idx];
806     sc->cpu_cx_stats[cx_next_idx]++;
807     KASSERT(cx_next->type != ACPI_STATE_C0, ("acpi_cpu_idle: C0 sleep"));
808 
809     /*
810      * Execute HLT (or equivalent) and wait for an interrupt.  We can't
811      * calculate the time spent in C1 since the place we wake up is an
812      * ISR.  Assume we slept half of quantum and return.
813      */
814     if (cx_next->type == ACPI_STATE_C1) {
815 	sc->cpu_prev_sleep = (sc->cpu_prev_sleep * 3 + 500000 / hz) / 4;
816 	acpi_cpu_c1();
817 	return;
818     }
819 
820     /*
821      * For C3(+), disable bus master arbitration and enable bus master wake
822      * if BM control is available, otherwise flush the CPU cache.
823      */
824     if (cx_next->type >= ACPI_STATE_C3) {
825 	if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
826 	    AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
827 	    AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
828 	} else
829 	    ACPI_FLUSH_CPU_CACHE();
830     }
831 
832     /*
833      * Read from P_LVLx to enter C2(+), checking time spent asleep.
834      * Use the ACPI timer for measuring sleep time.  Since we need to
835      * get the time very close to the CPU start/stop clock logic, this
836      * is the only reliable time source.
837      */
838     AcpiRead(&start_time, &AcpiGbl_FADT.XPmTimerBlock);
839     CPU_GET_REG(cx_next->p_lvlx, 1);
840 
841     /*
842      * Read the end time twice.  Since it may take an arbitrary time
843      * to enter the idle state, the first read may be executed before
844      * the processor has stopped.  Doing it again provides enough
845      * margin that we are certain to have a correct value.
846      */
847     AcpiRead(&end_time, &AcpiGbl_FADT.XPmTimerBlock);
848     AcpiRead(&end_time, &AcpiGbl_FADT.XPmTimerBlock);
849 
850     /* Enable bus master arbitration and disable bus master wakeup. */
851     if (cx_next->type >= ACPI_STATE_C3) {
852 	if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
853 	    AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
854 	    AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
855 	}
856     }
857     ACPI_ENABLE_IRQS();
858 
859     /* Find the actual time asleep in microseconds. */
860     end_time = acpi_TimerDelta(end_time, start_time);
861     sc->cpu_prev_sleep = (sc->cpu_prev_sleep * 3 + PM_USEC(end_time)) / 4;
862 }
863 
864 /*
865  * Re-evaluate the _CST object when we are notified that it changed.
866  */
867 static void
868 acpi_cpu_cst_notify(device_t dev)
869 {
870     struct acpi_cpu_softc *sc = device_get_softc(dev);
871     struct acpi_cpu_softc *isc;
872     int i;
873 
874     KASSERT(curthread->td_type != TD_TYPE_NETISR,
875         ("notify in netisr%d", mycpuid));
876 
877     lwkt_serialize_enter(&cpu_cx_slize);
878 
879     /* Update the list of Cx states. */
880     acpi_cpu_cx_cst(sc);
881     acpi_cpu_cx_list(sc);
882 
883     /* Update the new lowest useable Cx state for all CPUs. */
884     cpu_cx_count = 0;
885     for (i = 0; i < cpu_ndevices; i++) {
886 	isc = device_get_softc(cpu_devices[i]);
887 	if (isc->cpu_cx_count > cpu_cx_count)
888 	    cpu_cx_count = isc->cpu_cx_count;
889     }
890 
891     /*
892      * Fix up the lowest Cx being used
893      */
894     if (cpu_cx_lowest_req < cpu_cx_count)
895 	cpu_cx_lowest = cpu_cx_lowest_req;
896     if (cpu_cx_lowest > cpu_cx_count - 1)
897 	cpu_cx_lowest = cpu_cx_count - 1;
898 
899     lwkt_serialize_exit(&cpu_cx_slize);
900 }
901 
902 static int
903 acpi_cpu_quirks(void)
904 {
905     device_t acpi_dev;
906     uint32_t val;
907 
908     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
909 
910     /*
911      * Bus mastering arbitration control is needed to keep caches coherent
912      * while sleeping in C3.  If it's not present but a working flush cache
913      * instruction is present, flush the caches before entering C3 instead.
914      * Otherwise, just disable C3 completely.
915      */
916     if (AcpiGbl_FADT.Pm2ControlBlock == 0 ||
917 	AcpiGbl_FADT.Pm2ControlLength == 0) {
918 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) &&
919 	    (AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0) {
920 	    cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
921 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
922 		"acpi_cpu: no BM control, using flush cache method\n"));
923 	} else {
924 	    cpu_quirks |= CPU_QUIRK_NO_C3;
925 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
926 		"acpi_cpu: no BM control, C3 not available\n"));
927 	}
928     }
929 
930     /*
931      * If we are using generic Cx mode, C3 on multiple CPUs requires using
932      * the expensive flush cache instruction.
933      */
934     if (cpu_cx_generic && ncpus > 1) {
935 	cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
936 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
937 	    "acpi_cpu: SMP, using flush cache mode for C3\n"));
938     }
939 
940     /* Look for various quirks of the PIIX4 part. */
941     acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
942     if (acpi_dev != NULL) {
943 	switch (pci_get_revid(acpi_dev)) {
944 	/*
945 	 * Disable C3 support for all PIIX4 chipsets.  Some of these parts
946 	 * do not report the BMIDE status to the BM status register and
947 	 * others have a livelock bug if Type-F DMA is enabled.  Linux
948 	 * works around the BMIDE bug by reading the BM status directly
949 	 * but we take the simpler approach of disabling C3 for these
950 	 * parts.
951 	 *
952 	 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
953 	 * Livelock") from the January 2002 PIIX4 specification update.
954 	 * Applies to all PIIX4 models.
955 	 *
956 	 * Also, make sure that all interrupts cause a "Stop Break"
957 	 * event to exit from C2 state.
958 	 * Also, BRLD_EN_BM (ACPI_BITREG_BUS_MASTER_RLD in ACPI-speak)
959 	 * should be set to zero, otherwise it causes C2 to short-sleep.
960 	 * PIIX4 doesn't properly support C3 and bus master activity
961 	 * need not break out of C2.
962 	 */
963 	case PCI_REVISION_A_STEP:
964 	case PCI_REVISION_B_STEP:
965 	case PCI_REVISION_4E:
966 	case PCI_REVISION_4M:
967 	    cpu_quirks |= CPU_QUIRK_NO_C3;
968 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
969 		"acpi_cpu: working around PIIX4 bug, disabling C3\n"));
970 
971 	    val = pci_read_config(acpi_dev, PIIX4_DEVACTB_REG, 4);
972 	    if ((val & PIIX4_STOP_BREAK_MASK) != PIIX4_STOP_BREAK_MASK) {
973 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
974 		    "acpi_cpu: PIIX4: enabling IRQs to generate Stop Break\n"));
975 	    	val |= PIIX4_STOP_BREAK_MASK;
976 		pci_write_config(acpi_dev, PIIX4_DEVACTB_REG, val, 4);
977 	    }
978 	    AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_RLD, &val);
979 	    if (val) {
980 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
981 		    "acpi_cpu: PIIX4: reset BRLD_EN_BM\n"));
982 		AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
983 	    }
984 	    break;
985 	default:
986 	    break;
987 	}
988     }
989 
990     return (0);
991 }
992 
993 static int
994 acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS)
995 {
996     struct acpi_cpu_softc *sc;
997     struct sbuf	 sb;
998     char	 buf[128];
999     int		 i;
1000     uintmax_t	 fract, sum, whole;
1001 
1002     sc = (struct acpi_cpu_softc *) arg1;
1003     sum = 0;
1004     for (i = 0; i < sc->cpu_cx_count; i++)
1005 	sum += sc->cpu_cx_stats[i];
1006     sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1007     for (i = 0; i < sc->cpu_cx_count; i++) {
1008 	if (sum > 0) {
1009 	    whole = (uintmax_t)sc->cpu_cx_stats[i] * 100;
1010 	    fract = (whole % sum) * 100;
1011 	    sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
1012 		(u_int)(fract / sum));
1013 	} else
1014 	    sbuf_printf(&sb, "0.00%% ");
1015     }
1016     sbuf_printf(&sb, "last %dus", sc->cpu_prev_sleep);
1017     sbuf_trim(&sb);
1018     sbuf_finish(&sb);
1019     sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1020     sbuf_delete(&sb);
1021 
1022     return (0);
1023 }
1024 
1025 static int
1026 acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val)
1027 {
1028     int old_lowest, error = 0;
1029     uint32_t old_type, type;
1030 
1031     sc->cpu_cx_lowest_req = val;
1032     if (val > sc->cpu_cx_count - 1)
1033 	val = sc->cpu_cx_count - 1;
1034     old_lowest = atomic_swap_int(&sc->cpu_cx_lowest, val);
1035 
1036     old_type = sc->cpu_cx_states[old_lowest].type;
1037     type = sc->cpu_cx_states[val].type;
1038     if (old_type >= ACPI_STATE_C3 && type < ACPI_STATE_C3) {
1039 	KKASSERT(cpu_c3_ncpus > 0);
1040 	if (atomic_fetchadd_int(&cpu_c3_ncpus, -1) == 1) {
1041 	    /*
1042 	     * All of the CPUs exit C3 state, use a better
1043 	     * one shot timer.
1044 	     */
1045 	    error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_NONE);
1046 	    KKASSERT(!error || error == ERESTART);
1047 	    if (error == ERESTART) {
1048 		if (bootverbose)
1049 		    kprintf("exit C3, restart intr cputimer\n");
1050 		cputimer_intr_restart();
1051 	    }
1052     	}
1053     } else if (type >= ACPI_STATE_C3 && old_type < ACPI_STATE_C3) {
1054 	if (atomic_fetchadd_int(&cpu_c3_ncpus, 1) == 0) {
1055 	    /*
1056 	     * When the first CPU enters C3(+) state, switch
1057 	     * to an one shot timer, which could handle
1058 	     * C3(+) state, i.e. the timer will not hang.
1059 	     */
1060 	    error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_PS);
1061 	    if (error == ERESTART) {
1062 		if (bootverbose)
1063 		    kprintf("enter C3, restart intr cputimer\n");
1064 		cputimer_intr_restart();
1065 	    } else if (error) {
1066 		kprintf("no suitable intr cputimer found\n");
1067 
1068 		/* Restore */
1069 		sc->cpu_cx_lowest = old_lowest;
1070 		atomic_fetchadd_int(&cpu_c3_ncpus, -1);
1071 	    }
1072 	}
1073     }
1074 
1075     if (error)
1076 	return error;
1077 
1078     /* Cache the new lowest non-C3 state. */
1079     acpi_cpu_cx_non_c3(sc);
1080 
1081     /* Reset the statistics counters. */
1082     bzero(sc->cpu_cx_stats, sizeof(sc->cpu_cx_stats));
1083     return (0);
1084 }
1085 
1086 static int
1087 acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1088 {
1089     struct	 acpi_cpu_softc *sc;
1090     char	 state[8];
1091     int		 val, error;
1092 
1093     sc = (struct acpi_cpu_softc *)arg1;
1094     ksnprintf(state, sizeof(state), "C%d", sc->cpu_cx_lowest_req + 1);
1095     error = sysctl_handle_string(oidp, state, sizeof(state), req);
1096     if (error != 0 || req->newptr == NULL)
1097 	return (error);
1098     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1099 	return (EINVAL);
1100     val = (int) strtol(state + 1, NULL, 10) - 1;
1101     if (val < 0)
1102 	return (EINVAL);
1103 
1104     lwkt_serialize_enter(&cpu_cx_slize);
1105     error = acpi_cpu_set_cx_lowest(sc, val);
1106     lwkt_serialize_exit(&cpu_cx_slize);
1107 
1108     return error;
1109 }
1110 
1111 static int
1112 acpi_cpu_cx_lowest_use_sysctl(SYSCTL_HANDLER_ARGS)
1113 {
1114     struct	 acpi_cpu_softc *sc;
1115     char	 state[8];
1116 
1117     sc = (struct acpi_cpu_softc *)arg1;
1118     ksnprintf(state, sizeof(state), "C%d", sc->cpu_cx_lowest + 1);
1119     return sysctl_handle_string(oidp, state, sizeof(state), req);
1120 }
1121 
1122 static int
1123 acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1124 {
1125     struct	acpi_cpu_softc *sc;
1126     char	state[8];
1127     int		val, error, i;
1128 
1129     ksnprintf(state, sizeof(state), "C%d", cpu_cx_lowest_req + 1);
1130     error = sysctl_handle_string(oidp, state, sizeof(state), req);
1131     if (error != 0 || req->newptr == NULL)
1132 	return (error);
1133     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1134 	return (EINVAL);
1135     val = (int) strtol(state + 1, NULL, 10) - 1;
1136     if (val < 0)
1137 	return (EINVAL);
1138 
1139     lwkt_serialize_enter(&cpu_cx_slize);
1140 
1141     cpu_cx_lowest_req = val;
1142     cpu_cx_lowest = val;
1143     if (cpu_cx_lowest > cpu_cx_count - 1)
1144 	cpu_cx_lowest = cpu_cx_count - 1;
1145 
1146     /* Update the new lowest useable Cx state for all CPUs. */
1147     for (i = 0; i < cpu_ndevices; i++) {
1148 	sc = device_get_softc(cpu_devices[i]);
1149 	error = acpi_cpu_set_cx_lowest(sc, val);
1150 	if (error) {
1151 	    KKASSERT(i == 0);
1152 	    break;
1153 	}
1154     }
1155 
1156     lwkt_serialize_exit(&cpu_cx_slize);
1157 
1158     return error;
1159 }
1160 
1161 static int
1162 acpi_cpu_global_cx_lowest_use_sysctl(SYSCTL_HANDLER_ARGS)
1163 {
1164     char	state[8];
1165 
1166     ksnprintf(state, sizeof(state), "C%d", cpu_cx_lowest + 1);
1167     return sysctl_handle_string(oidp, state, sizeof(state), req);
1168 }
1169 
1170 /*
1171  * Put the CPU in C1 in a machine-dependant way.
1172  * XXX: shouldn't be here!
1173  */
1174 static void
1175 acpi_cpu_c1(void)
1176 {
1177 #ifdef __ia64__
1178     ia64_call_pal_static(PAL_HALT_LIGHT, 0, 0, 0);
1179 #else
1180     splz();
1181     if ((mycpu->gd_reqflags & RQF_IDLECHECK_WK_MASK) == 0)
1182         __asm __volatile("sti; hlt");
1183     else
1184         __asm __volatile("sti; pause");
1185 #endif /* !__ia64__ */
1186 }
1187 
1188 static void
1189 acpi_cpu_cx_non_c3(struct acpi_cpu_softc *sc)
1190 {
1191     int i;
1192 
1193     sc->cpu_non_c3 = 0;
1194     for (i = sc->cpu_cx_lowest; i >= 0; i--) {
1195 	if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
1196 	    sc->cpu_non_c3 = i;
1197 	    break;
1198 	}
1199     }
1200     if (bootverbose)
1201 	device_printf(sc->cpu_dev, "non-C3 %d\n", sc->cpu_non_c3);
1202 }
1203