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