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