xref: /freebsd/sys/dev/acpica/acpi_cpu.c (revision 7a3107219353a5399cd3886bc3ff0a06d4aef8e2)
1fec754d4SMike Smith /*-
298aa9cd0SNate Lawson  * Copyright (c) 2003-2005 Nate Lawson (SDG)
3fec754d4SMike Smith  * Copyright (c) 2001 Michael Smith
4fec754d4SMike Smith  * All rights reserved.
5fec754d4SMike Smith  *
6fec754d4SMike Smith  * Redistribution and use in source and binary forms, with or without
7fec754d4SMike Smith  * modification, are permitted provided that the following conditions
8fec754d4SMike Smith  * are met:
9fec754d4SMike Smith  * 1. Redistributions of source code must retain the above copyright
10fec754d4SMike Smith  *    notice, this list of conditions and the following disclaimer.
11fec754d4SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
12fec754d4SMike Smith  *    notice, this list of conditions and the following disclaimer in the
13fec754d4SMike Smith  *    documentation and/or other materials provided with the distribution.
14fec754d4SMike Smith  *
15fec754d4SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16fec754d4SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17fec754d4SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18fec754d4SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19fec754d4SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20fec754d4SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21fec754d4SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22fec754d4SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23fec754d4SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24fec754d4SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25fec754d4SMike Smith  * SUCH DAMAGE.
26fec754d4SMike Smith  */
27fec754d4SMike Smith 
28aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
29aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
30aad970f1SDavid E. O'Brien 
31fec754d4SMike Smith #include "opt_acpi.h"
32fec754d4SMike Smith #include <sys/param.h>
33fec754d4SMike Smith #include <sys/bus.h>
3498aa9cd0SNate Lawson #include <sys/cpu.h>
3556a70eadSNate Lawson #include <sys/kernel.h>
36447a5fa1SJohn Baldwin #include <sys/malloc.h>
37fe12f24bSPoul-Henning Kamp #include <sys/module.h>
386b74f9b7SNate Lawson #include <sys/pcpu.h>
3956a70eadSNate Lawson #include <sys/power.h>
4056a70eadSNate Lawson #include <sys/proc.h>
4156a70eadSNate Lawson #include <sys/sbuf.h>
426b74f9b7SNate Lawson #include <sys/smp.h>
43fec754d4SMike Smith 
446b74f9b7SNate Lawson #include <dev/pci/pcivar.h>
4556a70eadSNate Lawson #include <machine/atomic.h>
46fec754d4SMike Smith #include <machine/bus.h>
4756a70eadSNate Lawson #include <sys/rman.h>
48fec754d4SMike Smith 
492a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h>
50fec754d4SMike Smith #include <dev/acpica/acpivar.h>
51fec754d4SMike Smith 
52fec754d4SMike Smith /*
538c5468e3SNate Lawson  * Support for ACPI Processor devices, including C[1-3] sleep states.
54fec754d4SMike Smith  */
55fec754d4SMike Smith 
56be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
57fec754d4SMike Smith #define _COMPONENT	ACPI_PROCESSOR
589127281cSMike Smith ACPI_MODULE_NAME("PROCESSOR")
59fec754d4SMike Smith 
606b74f9b7SNate Lawson struct acpi_cx {
616b74f9b7SNate Lawson     struct resource	*p_lvlx;	/* Register to read to enter state. */
626b74f9b7SNate Lawson     uint32_t		 type;		/* C1-3 (C4 and up treated as C3). */
636b74f9b7SNate Lawson     uint32_t		 trans_lat;	/* Transition latency (usec). */
646b74f9b7SNate Lawson     uint32_t		 power;		/* Power consumed (mW). */
65f4eb0418SNate Lawson     int			 res_type;	/* Resource type for p_lvlx. */
666b74f9b7SNate Lawson };
676b74f9b7SNate Lawson #define MAX_CX_STATES	 8
686b74f9b7SNate Lawson 
69fec754d4SMike Smith struct acpi_cpu_softc {
70fec754d4SMike Smith     device_t		 cpu_dev;
71fec754d4SMike Smith     ACPI_HANDLE		 cpu_handle;
7298aa9cd0SNate Lawson     struct pcpu		*cpu_pcpu;
7398aa9cd0SNate Lawson     uint32_t		 cpu_acpi_id;	/* ACPI processor id */
7456a70eadSNate Lawson     uint32_t		 cpu_p_blk;	/* ACPI P_BLK location */
7556a70eadSNate Lawson     uint32_t		 cpu_p_blk_len;	/* P_BLK length (must be 6). */
766b74f9b7SNate Lawson     struct acpi_cx	 cpu_cx_states[MAX_CX_STATES];
7756a70eadSNate Lawson     int			 cpu_cx_count;	/* Number of valid Cx states. */
78a2afe45aSNate Lawson     int			 cpu_prev_sleep;/* Last idle sleep duration. */
79b29224c2SNate Lawson     int			 cpu_features;	/* Child driver supported features. */
80907b6777SNate Lawson     /* Runtime state. */
81907b6777SNate Lawson     int			 cpu_non_c3;	/* Index of lowest non-C3 state. */
82907b6777SNate Lawson     int			 cpu_short_slp;	/* Count of < 1us sleeps. */
83907b6777SNate Lawson     u_int		 cpu_cx_stats[MAX_CX_STATES];/* Cx usage history. */
84907b6777SNate Lawson     /* Values for sysctl. */
85907b6777SNate Lawson     struct sysctl_ctx_list cpu_sysctl_ctx;
86907b6777SNate Lawson     struct sysctl_oid	*cpu_sysctl_tree;
87907b6777SNate Lawson     int			 cpu_cx_lowest;
88907b6777SNate Lawson     char 		 cpu_cx_supported[64];
89907b6777SNate Lawson     int			 cpu_rid;
90fec754d4SMike Smith };
91fec754d4SMike Smith 
9298aa9cd0SNate Lawson struct acpi_cpu_device {
9398aa9cd0SNate Lawson     struct resource_list	ad_rl;
9498aa9cd0SNate Lawson };
9598aa9cd0SNate Lawson 
966b74f9b7SNate Lawson #define CPU_GET_REG(reg, width) 					\
976b74f9b7SNate Lawson     (bus_space_read_ ## width(rman_get_bustag((reg)), 			\
986b74f9b7SNate Lawson 		      rman_get_bushandle((reg)), 0))
996b74f9b7SNate Lawson #define CPU_SET_REG(reg, width, val)					\
1006b74f9b7SNate Lawson     (bus_space_write_ ## width(rman_get_bustag((reg)), 			\
1016b74f9b7SNate Lawson 		       rman_get_bushandle((reg)), 0, (val)))
102be2b1797SNate Lawson 
1036b74f9b7SNate Lawson #define PM_USEC(x)	 ((x) >> 2)	/* ~4 clocks per usec (3.57955 Mhz) */
104fec754d4SMike Smith 
1058b888c66SNate Lawson #define ACPI_NOTIFY_CX_STATES	0x81	/* _CST changed. */
106fec754d4SMike Smith 
10718ececa0SNate Lawson #define CPU_QUIRK_NO_C3		(1<<0)	/* C3-type states are not usable. */
10818ececa0SNate Lawson #define CPU_QUIRK_NO_BM_CTRL	(1<<2)	/* No bus mastering control. */
1096b74f9b7SNate Lawson 
1106b74f9b7SNate Lawson #define PCI_VENDOR_INTEL	0x8086
1116b74f9b7SNate Lawson #define PCI_DEVICE_82371AB_3	0x7113	/* PIIX4 chipset for quirks. */
1126b74f9b7SNate Lawson #define PCI_REVISION_A_STEP	0
1136b74f9b7SNate Lawson #define PCI_REVISION_B_STEP	1
1146b74f9b7SNate Lawson #define PCI_REVISION_4E		2
1156b74f9b7SNate Lawson #define PCI_REVISION_4M		3
1166b74f9b7SNate Lawson 
1176b74f9b7SNate Lawson /* Platform hardware resource information. */
1186b74f9b7SNate Lawson static uint32_t		 cpu_smi_cmd;	/* Value to write to SMI_CMD. */
11956a70eadSNate Lawson static uint8_t		 cpu_cst_cnt;	/* Indicate we are _CST aware. */
120a2afe45aSNate Lawson static int		 cpu_quirks;	/* Indicate any hardware bugs. */
1216b74f9b7SNate Lawson 
1226b74f9b7SNate Lawson /* Runtime state. */
123907b6777SNate Lawson static int		 cpu_disable_idle; /* Disable entry to idle function */
124907b6777SNate Lawson static int		 cpu_cx_count;	/* Number of valid Cx states */
1256b74f9b7SNate Lawson 
1266b74f9b7SNate Lawson /* Values for sysctl. */
127907b6777SNate Lawson static struct sysctl_ctx_list cpu_sysctl_ctx;
128907b6777SNate Lawson static struct sysctl_oid *cpu_sysctl_tree;
129907b6777SNate Lawson static int		 cpu_cx_generic;
1306b74f9b7SNate Lawson static int		 cpu_cx_lowest;
131fec754d4SMike Smith 
132fec754d4SMike Smith static device_t		*cpu_devices;
133fec754d4SMike Smith static int		 cpu_ndevices;
134447a5fa1SJohn Baldwin static struct acpi_cpu_softc **cpu_softc;
135d92a2ebdSNate Lawson ACPI_SERIAL_DECL(cpu, "ACPI CPU");
136fec754d4SMike Smith 
137fec754d4SMike Smith static int	acpi_cpu_probe(device_t dev);
138fec754d4SMike Smith static int	acpi_cpu_attach(device_t dev);
1393331373cSNate Lawson static int	acpi_cpu_suspend(device_t dev);
1403331373cSNate Lawson static int	acpi_cpu_resume(device_t dev);
141b6426963SNate Lawson static int	acpi_pcpu_get_id(uint32_t idx, uint32_t *acpi_id,
142b6426963SNate Lawson 		    uint32_t *cpu_id);
14398aa9cd0SNate Lawson static struct resource_list *acpi_cpu_get_rlist(device_t dev, device_t child);
14498aa9cd0SNate Lawson static device_t	acpi_cpu_add_child(device_t dev, int order, const char *name,
14598aa9cd0SNate Lawson 		    int unit);
14698aa9cd0SNate Lawson static int	acpi_cpu_read_ivar(device_t dev, device_t child, int index,
14798aa9cd0SNate Lawson 		    uintptr_t *result);
14856a70eadSNate Lawson static int	acpi_cpu_shutdown(device_t dev);
149907b6777SNate Lawson static void	acpi_cpu_cx_probe(struct acpi_cpu_softc *sc);
150907b6777SNate Lawson static void	acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc);
1516b74f9b7SNate Lawson static int	acpi_cpu_cx_cst(struct acpi_cpu_softc *sc);
1526b74f9b7SNate Lawson static void	acpi_cpu_startup(void *arg);
153907b6777SNate Lawson static void	acpi_cpu_startup_cx(struct acpi_cpu_softc *sc);
1546b74f9b7SNate Lawson static void	acpi_cpu_idle(void);
1556b74f9b7SNate Lawson static void	acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context);
156907b6777SNate Lawson static int	acpi_cpu_quirks(void);
157a2afe45aSNate Lawson static int	acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS);
1583331373cSNate Lawson static int	acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val);
1596b74f9b7SNate Lawson static int	acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
160907b6777SNate Lawson static int	acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
161fec754d4SMike Smith 
162fec754d4SMike Smith static device_method_t acpi_cpu_methods[] = {
163fec754d4SMike Smith     /* Device interface */
164fec754d4SMike Smith     DEVMETHOD(device_probe,	acpi_cpu_probe),
165fec754d4SMike Smith     DEVMETHOD(device_attach,	acpi_cpu_attach),
16698aa9cd0SNate Lawson     DEVMETHOD(device_detach,	bus_generic_detach),
16756a70eadSNate Lawson     DEVMETHOD(device_shutdown,	acpi_cpu_shutdown),
1683331373cSNate Lawson     DEVMETHOD(device_suspend,	acpi_cpu_suspend),
1693331373cSNate Lawson     DEVMETHOD(device_resume,	acpi_cpu_resume),
17098aa9cd0SNate Lawson 
17198aa9cd0SNate Lawson     /* Bus interface */
17298aa9cd0SNate Lawson     DEVMETHOD(bus_add_child,	acpi_cpu_add_child),
17398aa9cd0SNate Lawson     DEVMETHOD(bus_read_ivar,	acpi_cpu_read_ivar),
17498aa9cd0SNate Lawson     DEVMETHOD(bus_get_resource_list, acpi_cpu_get_rlist),
17598aa9cd0SNate Lawson     DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
17698aa9cd0SNate Lawson     DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
17798aa9cd0SNate Lawson     DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
17898aa9cd0SNate Lawson     DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
17998aa9cd0SNate Lawson     DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
18098aa9cd0SNate Lawson     DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
18198aa9cd0SNate Lawson     DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
18298aa9cd0SNate Lawson     DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
18398aa9cd0SNate Lawson     DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
184fec754d4SMike Smith 
185fec754d4SMike Smith     {0, 0}
186fec754d4SMike Smith };
187fec754d4SMike Smith 
188fec754d4SMike Smith static driver_t acpi_cpu_driver = {
189b0e2b625SNate Lawson     "cpu",
190fec754d4SMike Smith     acpi_cpu_methods,
191fec754d4SMike Smith     sizeof(struct acpi_cpu_softc),
192fec754d4SMike Smith };
193fec754d4SMike Smith 
1943045c8afSNate Lawson static devclass_t acpi_cpu_devclass;
1953045c8afSNate Lawson DRIVER_MODULE(cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0);
196b0e2b625SNate Lawson MODULE_DEPEND(cpu, acpi, 1, 1, 1);
19756a70eadSNate Lawson 
198fec754d4SMike Smith static int
199fec754d4SMike Smith acpi_cpu_probe(device_t dev)
200fec754d4SMike Smith {
201b29224c2SNate Lawson     int			   acpi_id, cpu_id;
202b0e2b625SNate Lawson     ACPI_BUFFER		   buf;
203b0e2b625SNate Lawson     ACPI_HANDLE		   handle;
204b0e2b625SNate Lawson     ACPI_OBJECT		   *obj;
205b0e2b625SNate Lawson     ACPI_STATUS		   status;
206b0e2b625SNate Lawson 
207b0e2b625SNate Lawson     if (acpi_disabled("cpu") || acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
208b0e2b625SNate Lawson 	return (ENXIO);
209b0e2b625SNate Lawson 
210b0e2b625SNate Lawson     handle = acpi_get_handle(dev);
211447a5fa1SJohn Baldwin     if (cpu_softc == NULL)
212447a5fa1SJohn Baldwin 	cpu_softc = malloc(sizeof(struct acpi_cpu_softc *) *
213447a5fa1SJohn Baldwin 	    (mp_maxid + 1), M_TEMP /* XXX */, M_WAITOK | M_ZERO);
214b0e2b625SNate Lawson 
215b0e2b625SNate Lawson     /* Get our Processor object. */
216b0e2b625SNate Lawson     buf.Pointer = NULL;
217b0e2b625SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
218b0e2b625SNate Lawson     status = AcpiEvaluateObject(handle, NULL, NULL, &buf);
219b0e2b625SNate Lawson     if (ACPI_FAILURE(status)) {
220b0e2b625SNate Lawson 	device_printf(dev, "probe failed to get Processor obj - %s\n",
221b0e2b625SNate Lawson 		      AcpiFormatException(status));
222b0e2b625SNate Lawson 	return (ENXIO);
223b0e2b625SNate Lawson     }
224b0e2b625SNate Lawson     obj = (ACPI_OBJECT *)buf.Pointer;
225b0e2b625SNate Lawson     if (obj->Type != ACPI_TYPE_PROCESSOR) {
226b0e2b625SNate Lawson 	device_printf(dev, "Processor object has bad type %d\n", obj->Type);
227b0e2b625SNate Lawson 	AcpiOsFree(obj);
228b0e2b625SNate Lawson 	return (ENXIO);
229fec754d4SMike Smith     }
230be2b1797SNate Lawson 
231b0e2b625SNate Lawson     /*
232b0e2b625SNate Lawson      * Find the processor associated with our unit.  We could use the
233b0e2b625SNate Lawson      * ProcId as a key, however, some boxes do not have the same values
234b0e2b625SNate Lawson      * in their Processor object as the ProcId values in the MADT.
235b0e2b625SNate Lawson      */
236b0e2b625SNate Lawson     acpi_id = obj->Processor.ProcId;
237b0e2b625SNate Lawson     AcpiOsFree(obj);
238b0e2b625SNate Lawson     if (acpi_pcpu_get_id(device_get_unit(dev), &acpi_id, &cpu_id) != 0)
239fec754d4SMike Smith 	return (ENXIO);
240b0e2b625SNate Lawson 
241b0e2b625SNate Lawson     /*
242b0e2b625SNate Lawson      * Check if we already probed this processor.  We scan the bus twice
243b0e2b625SNate Lawson      * so it's possible we've already seen this one.
244b0e2b625SNate Lawson      */
245b0e2b625SNate Lawson     if (cpu_softc[cpu_id] != NULL)
246b0e2b625SNate Lawson 	return (ENXIO);
247b0e2b625SNate Lawson 
248b0e2b625SNate Lawson     /* Mark this processor as in-use and save our derived id for attach. */
249b0e2b625SNate Lawson     cpu_softc[cpu_id] = (void *)1;
250b0e2b625SNate Lawson     acpi_set_magic(dev, cpu_id);
251b29224c2SNate Lawson     device_set_desc(dev, "ACPI CPU");
252b0e2b625SNate Lawson 
253b0e2b625SNate Lawson     return (0);
254fec754d4SMike Smith }
255fec754d4SMike Smith 
256fec754d4SMike Smith static int
257fec754d4SMike Smith acpi_cpu_attach(device_t dev)
258fec754d4SMike Smith {
259b0e2b625SNate Lawson     ACPI_BUFFER		   buf;
260c961facaSNate Lawson     ACPI_OBJECT		   arg[4], *obj;
261bce92885SNate Lawson     ACPI_OBJECT_LIST	   arglist;
26298aa9cd0SNate Lawson     struct pcpu		   *pcpu_data;
263fec754d4SMike Smith     struct acpi_cpu_softc *sc;
264fec754d4SMike Smith     struct acpi_softc	  *acpi_sc;
265fec754d4SMike Smith     ACPI_STATUS		   status;
266b29224c2SNate Lawson     u_int		   features;
267b29224c2SNate Lawson     int			   cpu_id, drv_count, i;
268b29224c2SNate Lawson     driver_t 		  **drivers;
269bce92885SNate Lawson     uint32_t		   cap_set[3];
270fec754d4SMike Smith 
271c961facaSNate Lawson     /* UUID needed by _OSC evaluation */
272c961facaSNate Lawson     static uint8_t cpu_oscuuid[16] = { 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29,
273c961facaSNate Lawson 				       0xBE, 0x47, 0x9E, 0xBD, 0xD8, 0x70,
274c961facaSNate Lawson 				       0x58, 0x71, 0x39, 0x53 };
275c961facaSNate Lawson 
276b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
277fec754d4SMike Smith 
278fec754d4SMike Smith     sc = device_get_softc(dev);
279fec754d4SMike Smith     sc->cpu_dev = dev;
280fec754d4SMike Smith     sc->cpu_handle = acpi_get_handle(dev);
28198aa9cd0SNate Lawson     cpu_id = acpi_get_magic(dev);
28298aa9cd0SNate Lawson     cpu_softc[cpu_id] = sc;
28398aa9cd0SNate Lawson     pcpu_data = pcpu_find(cpu_id);
28498aa9cd0SNate Lawson     pcpu_data->pc_device = dev;
28598aa9cd0SNate Lawson     sc->cpu_pcpu = pcpu_data;
2862be4e471SJung-uk Kim     cpu_smi_cmd = AcpiGbl_FADT.SmiCommand;
2872be4e471SJung-uk Kim     cpu_cst_cnt = AcpiGbl_FADT.CstControl;
288fec754d4SMike Smith 
289b0e2b625SNate Lawson     buf.Pointer = NULL;
290b0e2b625SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
2916b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf);
2926b74f9b7SNate Lawson     if (ACPI_FAILURE(status)) {
293b0e2b625SNate Lawson 	device_printf(dev, "attach failed to get Processor obj - %s\n",
2946b74f9b7SNate Lawson 		      AcpiFormatException(status));
2956b74f9b7SNate Lawson 	return (ENXIO);
296b0e2b625SNate Lawson     }
297b0e2b625SNate Lawson     obj = (ACPI_OBJECT *)buf.Pointer;
298b0e2b625SNate Lawson     sc->cpu_p_blk = obj->Processor.PblkAddress;
299b0e2b625SNate Lawson     sc->cpu_p_blk_len = obj->Processor.PblkLength;
30098aa9cd0SNate Lawson     sc->cpu_acpi_id = obj->Processor.ProcId;
301b0e2b625SNate Lawson     AcpiOsFree(obj);
30256a70eadSNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_BLK at %#x/%d\n",
30356a70eadSNate Lawson 		     device_get_unit(dev), sc->cpu_p_blk, sc->cpu_p_blk_len));
3046b74f9b7SNate Lawson 
305907b6777SNate Lawson     /*
306907b6777SNate Lawson      * If this is the first cpu we attach, create and initialize the generic
307907b6777SNate Lawson      * resources that will be used by all acpi cpu devices.
308907b6777SNate Lawson      */
309907b6777SNate Lawson     if (device_get_unit(dev) == 0) {
310907b6777SNate Lawson 	/* Assume we won't be using generic Cx mode by default */
311907b6777SNate Lawson 	cpu_cx_generic = FALSE;
312907b6777SNate Lawson 
313907b6777SNate Lawson 	/* Install hw.acpi.cpu sysctl tree */
314fec754d4SMike Smith 	acpi_sc = acpi_device_get_parent_softc(dev);
315907b6777SNate Lawson 	sysctl_ctx_init(&cpu_sysctl_ctx);
316907b6777SNate Lawson 	cpu_sysctl_tree = SYSCTL_ADD_NODE(&cpu_sysctl_ctx,
31798aa9cd0SNate Lawson 	    SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, "cpu",
318907b6777SNate Lawson 	    CTLFLAG_RD, 0, "node for CPU children");
319907b6777SNate Lawson 
320907b6777SNate Lawson 	/* Queue post cpu-probing task handler */
3212be4e471SJung-uk Kim 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cpu_startup, NULL);
322907b6777SNate Lawson     }
323fec754d4SMike Smith 
3246b74f9b7SNate Lawson     /*
325b29224c2SNate Lawson      * Before calling any CPU methods, collect child driver feature hints
326f2d94257SNate Lawson      * and notify ACPI of them.  We support unified SMP power control
327f2d94257SNate Lawson      * so advertise this ourselves.  Note this is not the same as independent
328f2d94257SNate Lawson      * SMP control where each CPU can have different settings.
329b29224c2SNate Lawson      */
330f2d94257SNate Lawson     sc->cpu_features = ACPI_CAP_SMP_SAME | ACPI_CAP_SMP_SAME_C3;
331b29224c2SNate Lawson     if (devclass_get_drivers(acpi_cpu_devclass, &drivers, &drv_count) == 0) {
332b29224c2SNate Lawson 	for (i = 0; i < drv_count; i++) {
333b29224c2SNate Lawson 	    if (ACPI_GET_FEATURES(drivers[i], &features) == 0)
334b29224c2SNate Lawson 		sc->cpu_features |= features;
335b29224c2SNate Lawson 	}
336b29224c2SNate Lawson 	free(drivers, M_TEMP);
337b29224c2SNate Lawson     }
338bce92885SNate Lawson 
339bce92885SNate Lawson     /*
340bce92885SNate Lawson      * CPU capabilities are specified as a buffer of 32-bit integers:
341bce92885SNate Lawson      * revision, count, and one or more capabilities.  The revision of
342c961facaSNate Lawson      * "1" is not specified anywhere but seems to match Linux.
343bce92885SNate Lawson      */
344bce92885SNate Lawson     if (sc->cpu_features) {
345c961facaSNate Lawson 	arglist.Pointer = arg;
346bce92885SNate Lawson 	arglist.Count = 1;
347c961facaSNate Lawson 	arg[0].Type = ACPI_TYPE_BUFFER;
348c961facaSNate Lawson 	arg[0].Buffer.Length = sizeof(cap_set);
349c961facaSNate Lawson 	arg[0].Buffer.Pointer = (uint8_t *)cap_set;
350bce92885SNate Lawson 	cap_set[0] = 1; /* revision */
351bce92885SNate Lawson 	cap_set[1] = 1; /* number of capabilities integers */
352bce92885SNate Lawson 	cap_set[2] = sc->cpu_features;
353bce92885SNate Lawson 	AcpiEvaluateObject(sc->cpu_handle, "_PDC", &arglist, NULL);
354c961facaSNate Lawson 
355c961facaSNate Lawson 	/*
356c961facaSNate Lawson 	 * On some systems we need to evaluate _OSC so that the ASL
357c961facaSNate Lawson 	 * loads the _PSS and/or _PDC methods at runtime.
358c961facaSNate Lawson 	 *
359c961facaSNate Lawson 	 * TODO: evaluate failure of _OSC.
360c961facaSNate Lawson 	 */
361c961facaSNate Lawson 	arglist.Pointer = arg;
362c961facaSNate Lawson 	arglist.Count = 4;
363c961facaSNate Lawson 	arg[0].Type = ACPI_TYPE_BUFFER;
364c961facaSNate Lawson 	arg[0].Buffer.Length = sizeof(cpu_oscuuid);
365c961facaSNate Lawson 	arg[0].Buffer.Pointer = cpu_oscuuid;	/* UUID */
366c961facaSNate Lawson 	arg[1].Type = ACPI_TYPE_INTEGER;
367c961facaSNate Lawson 	arg[1].Integer.Value = 1;		/* revision */
368c961facaSNate Lawson 	arg[2].Type = ACPI_TYPE_INTEGER;
369c961facaSNate Lawson 	arg[2].Integer.Value = 1;		/* count */
370c961facaSNate Lawson 	arg[3].Type = ACPI_TYPE_BUFFER;
371c961facaSNate Lawson 	arg[3].Buffer.Length = sizeof(cap_set);	/* Capabilities buffer */
372c961facaSNate Lawson 	arg[3].Buffer.Pointer = (uint8_t *)cap_set;
373c961facaSNate Lawson 	cap_set[0] = 0;
374c961facaSNate Lawson 	AcpiEvaluateObject(sc->cpu_handle, "_OSC", &arglist, NULL);
375bce92885SNate Lawson     }
376b29224c2SNate Lawson 
377907b6777SNate Lawson     /* Probe for Cx state support. */
378907b6777SNate Lawson     acpi_cpu_cx_probe(sc);
3796b74f9b7SNate Lawson 
380b29224c2SNate Lawson     /* Finally,  call identify and probe/attach for child devices. */
38198aa9cd0SNate Lawson     bus_generic_probe(dev);
38298aa9cd0SNate Lawson     bus_generic_attach(dev);
38398aa9cd0SNate Lawson 
38498aa9cd0SNate Lawson     return (0);
3856b74f9b7SNate Lawson }
3866b74f9b7SNate Lawson 
387b6426963SNate Lawson /*
3883331373cSNate Lawson  * Disable any entry to the idle function during suspend and re-enable it
3893331373cSNate Lawson  * during resume.
3903331373cSNate Lawson  */
3913331373cSNate Lawson static int
3923331373cSNate Lawson acpi_cpu_suspend(device_t dev)
3933331373cSNate Lawson {
3943331373cSNate Lawson     int error;
3953331373cSNate Lawson 
3963331373cSNate Lawson     error = bus_generic_suspend(dev);
3973331373cSNate Lawson     if (error)
3983331373cSNate Lawson 	return (error);
3993331373cSNate Lawson     cpu_disable_idle = TRUE;
4003331373cSNate Lawson     return (0);
4013331373cSNate Lawson }
4023331373cSNate Lawson 
4033331373cSNate Lawson static int
4043331373cSNate Lawson acpi_cpu_resume(device_t dev)
4053331373cSNate Lawson {
4063331373cSNate Lawson 
4073331373cSNate Lawson     cpu_disable_idle = FALSE;
4083331373cSNate Lawson     return (bus_generic_resume(dev));
4093331373cSNate Lawson }
4103331373cSNate Lawson 
4113331373cSNate Lawson /*
412b6426963SNate Lawson  * Find the nth present CPU and return its pc_cpuid as well as set the
413b6426963SNate Lawson  * pc_acpi_id from the most reliable source.
414b6426963SNate Lawson  */
415b6426963SNate Lawson static int
416b6426963SNate Lawson acpi_pcpu_get_id(uint32_t idx, uint32_t *acpi_id, uint32_t *cpu_id)
417b6426963SNate Lawson {
418b6426963SNate Lawson     struct pcpu	*pcpu_data;
419b6426963SNate Lawson     uint32_t	 i;
420b6426963SNate Lawson 
421b6426963SNate Lawson     KASSERT(acpi_id != NULL, ("Null acpi_id"));
422b6426963SNate Lawson     KASSERT(cpu_id != NULL, ("Null cpu_id"));
423447a5fa1SJohn Baldwin     for (i = 0; i <= mp_maxid; i++) {
424b6426963SNate Lawson 	if (CPU_ABSENT(i))
425b6426963SNate Lawson 	    continue;
426b6426963SNate Lawson 	pcpu_data = pcpu_find(i);
427b6426963SNate Lawson 	KASSERT(pcpu_data != NULL, ("no pcpu data for %d", i));
428b6426963SNate Lawson 	if (idx-- == 0) {
429b6426963SNate Lawson 	    /*
430b6426963SNate Lawson 	     * If pc_acpi_id was not initialized (e.g., a non-APIC UP box)
431b6426963SNate Lawson 	     * override it with the value from the ASL.  Otherwise, if the
432b6426963SNate Lawson 	     * two don't match, prefer the MADT-derived value.  Finally,
433b6426963SNate Lawson 	     * return the pc_cpuid to reference this processor.
434b6426963SNate Lawson 	     */
435b6426963SNate Lawson 	    if (pcpu_data->pc_acpi_id == 0xffffffff)
436b6426963SNate Lawson 		pcpu_data->pc_acpi_id = *acpi_id;
437b6426963SNate Lawson 	    else if (pcpu_data->pc_acpi_id != *acpi_id)
438b6426963SNate Lawson 		*acpi_id = pcpu_data->pc_acpi_id;
439b6426963SNate Lawson 	    *cpu_id = pcpu_data->pc_cpuid;
440b6426963SNate Lawson 	    return (0);
441b6426963SNate Lawson 	}
442b6426963SNate Lawson     }
443b6426963SNate Lawson 
444b6426963SNate Lawson     return (ESRCH);
445b6426963SNate Lawson }
446b6426963SNate Lawson 
44798aa9cd0SNate Lawson static struct resource_list *
44898aa9cd0SNate Lawson acpi_cpu_get_rlist(device_t dev, device_t child)
44998aa9cd0SNate Lawson {
45098aa9cd0SNate Lawson     struct acpi_cpu_device *ad;
45198aa9cd0SNate Lawson 
45298aa9cd0SNate Lawson     ad = device_get_ivars(child);
45398aa9cd0SNate Lawson     if (ad == NULL)
45498aa9cd0SNate Lawson 	return (NULL);
45598aa9cd0SNate Lawson     return (&ad->ad_rl);
45698aa9cd0SNate Lawson }
45798aa9cd0SNate Lawson 
45898aa9cd0SNate Lawson static device_t
45998aa9cd0SNate Lawson acpi_cpu_add_child(device_t dev, int order, const char *name, int unit)
46098aa9cd0SNate Lawson {
46198aa9cd0SNate Lawson     struct acpi_cpu_device *ad;
46298aa9cd0SNate Lawson     device_t child;
46398aa9cd0SNate Lawson 
46498aa9cd0SNate Lawson     if ((ad = malloc(sizeof(*ad), M_TEMP, M_NOWAIT | M_ZERO)) == NULL)
46598aa9cd0SNate Lawson 	return (NULL);
46698aa9cd0SNate Lawson 
46798aa9cd0SNate Lawson     resource_list_init(&ad->ad_rl);
46898aa9cd0SNate Lawson 
46998aa9cd0SNate Lawson     child = device_add_child_ordered(dev, order, name, unit);
47098aa9cd0SNate Lawson     if (child != NULL)
47198aa9cd0SNate Lawson 	device_set_ivars(child, ad);
47243ce1c77SNate Lawson     else
47343ce1c77SNate Lawson 	free(ad, M_TEMP);
47498aa9cd0SNate Lawson     return (child);
47598aa9cd0SNate Lawson }
47698aa9cd0SNate Lawson 
47798aa9cd0SNate Lawson static int
47898aa9cd0SNate Lawson acpi_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
47998aa9cd0SNate Lawson {
48098aa9cd0SNate Lawson     struct acpi_cpu_softc *sc;
48198aa9cd0SNate Lawson 
48298aa9cd0SNate Lawson     sc = device_get_softc(dev);
48398aa9cd0SNate Lawson     switch (index) {
48498aa9cd0SNate Lawson     case ACPI_IVAR_HANDLE:
48598aa9cd0SNate Lawson 	*result = (uintptr_t)sc->cpu_handle;
48698aa9cd0SNate Lawson 	break;
48798aa9cd0SNate Lawson     case CPU_IVAR_PCPU:
48898aa9cd0SNate Lawson 	*result = (uintptr_t)sc->cpu_pcpu;
48998aa9cd0SNate Lawson 	break;
49098aa9cd0SNate Lawson     default:
49198aa9cd0SNate Lawson 	return (ENOENT);
49298aa9cd0SNate Lawson     }
49398aa9cd0SNate Lawson     return (0);
49498aa9cd0SNate Lawson }
49598aa9cd0SNate Lawson 
4966b74f9b7SNate Lawson static int
49756a70eadSNate Lawson acpi_cpu_shutdown(device_t dev)
49856a70eadSNate Lawson {
49956a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
50056a70eadSNate Lawson 
50198aa9cd0SNate Lawson     /* Allow children to shutdown first. */
50298aa9cd0SNate Lawson     bus_generic_shutdown(dev);
50398aa9cd0SNate Lawson 
504cc3c11f9SNate Lawson     /*
505cc3c11f9SNate Lawson      * Disable any entry to the idle function.  There is a small race where
506cc3c11f9SNate Lawson      * an idle thread have passed this check but not gone to sleep.  This
507cc3c11f9SNate Lawson      * is ok since device_shutdown() does not free the softc, otherwise
508cc3c11f9SNate Lawson      * we'd have to be sure all threads were evicted before returning.
509cc3c11f9SNate Lawson      */
510907b6777SNate Lawson     cpu_disable_idle = TRUE;
51156a70eadSNate Lawson 
51256a70eadSNate Lawson     return_VALUE (0);
51356a70eadSNate Lawson }
51456a70eadSNate Lawson 
515907b6777SNate Lawson static void
5166b74f9b7SNate Lawson acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
5176b74f9b7SNate Lawson {
518907b6777SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
519907b6777SNate Lawson 
520907b6777SNate Lawson     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
521907b6777SNate Lawson     sc->cpu_prev_sleep = 1000000;
522907b6777SNate Lawson     sc->cpu_cx_lowest = 0;
523907b6777SNate Lawson 
524907b6777SNate Lawson     /*
525907b6777SNate Lawson      * Check for the ACPI 2.0 _CST sleep states object. If we can't find
526907b6777SNate Lawson      * any, we'll revert to generic FADT/P_BLK Cx control method which will
527907b6777SNate Lawson      * be handled by acpi_cpu_startup. We need to defer to after having
528907b6777SNate Lawson      * probed all the cpus in the system before probing for generic Cx
529907b6777SNate Lawson      * states as we may already have found cpus with valid _CST packages
530907b6777SNate Lawson      */
531907b6777SNate Lawson     if (!cpu_cx_generic && acpi_cpu_cx_cst(sc) != 0) {
532907b6777SNate Lawson 	/*
533907b6777SNate Lawson 	 * We were unable to find a _CST package for this cpu or there
534907b6777SNate Lawson 	 * was an error parsing it. Switch back to generic mode.
535907b6777SNate Lawson 	 */
536907b6777SNate Lawson 	cpu_cx_generic = TRUE;
537bd826803SNate Lawson 	if (bootverbose)
538bd826803SNate Lawson 	    device_printf(sc->cpu_dev, "switching to generic Cx mode\n");
539907b6777SNate Lawson     }
540907b6777SNate Lawson 
541907b6777SNate Lawson     /*
542907b6777SNate Lawson      * TODO: _CSD Package should be checked here.
543907b6777SNate Lawson      */
544907b6777SNate Lawson }
545907b6777SNate Lawson 
546907b6777SNate Lawson static void
547907b6777SNate Lawson acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc)
548907b6777SNate Lawson {
5496b74f9b7SNate Lawson     ACPI_GENERIC_ADDRESS	 gas;
5506b74f9b7SNate Lawson     struct acpi_cx		*cx_ptr;
55156a70eadSNate Lawson 
55256a70eadSNate Lawson     sc->cpu_cx_count = 0;
5536b74f9b7SNate Lawson     cx_ptr = sc->cpu_cx_states;
554fec754d4SMike Smith 
555907b6777SNate Lawson     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
556907b6777SNate Lawson     sc->cpu_prev_sleep = 1000000;
557907b6777SNate Lawson 
5586b74f9b7SNate Lawson     /* C1 has been required since just after ACPI 1.0 */
5596b74f9b7SNate Lawson     cx_ptr->type = ACPI_STATE_C1;
5606b74f9b7SNate Lawson     cx_ptr->trans_lat = 0;
5616b74f9b7SNate Lawson     cx_ptr++;
56256a70eadSNate Lawson     sc->cpu_cx_count++;
56356a70eadSNate Lawson 
564c181b89bSNate Lawson     /*
565c181b89bSNate Lawson      * The spec says P_BLK must be 6 bytes long.  However, some systems
566c181b89bSNate Lawson      * use it to indicate a fractional set of features present so we
567c181b89bSNate Lawson      * take 5 as C2.  Some may also have a value of 7 to indicate
568c181b89bSNate Lawson      * another C3 but most use _CST for this (as required) and having
569c181b89bSNate Lawson      * "only" C1-C3 is not a hardship.
570c181b89bSNate Lawson      */
571c181b89bSNate Lawson     if (sc->cpu_p_blk_len < 5)
572907b6777SNate Lawson 	return;
573fec754d4SMike Smith 
5746b74f9b7SNate Lawson     /* Validate and allocate resources for C2 (P_LVL2). */
5752be4e471SJung-uk Kim     gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
5762be4e471SJung-uk Kim     gas.BitWidth = 8;
5772be4e471SJung-uk Kim     if (AcpiGbl_FADT.C2Latency <= 100) {
57856a70eadSNate Lawson 	gas.Address = sc->cpu_p_blk + 4;
579907b6777SNate Lawson 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid,
580907b6777SNate Lawson 	    &gas, &cx_ptr->p_lvlx, RF_SHAREABLE);
5816b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
582907b6777SNate Lawson 	    sc->cpu_rid++;
5836b74f9b7SNate Lawson 	    cx_ptr->type = ACPI_STATE_C2;
5842be4e471SJung-uk Kim 	    cx_ptr->trans_lat = AcpiGbl_FADT.C2Latency;
5856b74f9b7SNate Lawson 	    cx_ptr++;
58656a70eadSNate Lawson 	    sc->cpu_cx_count++;
587fec754d4SMike Smith 	}
588fec754d4SMike Smith     }
589c181b89bSNate Lawson     if (sc->cpu_p_blk_len < 6)
590907b6777SNate Lawson 	return;
591be2b1797SNate Lawson 
5926b74f9b7SNate Lawson     /* Validate and allocate resources for C3 (P_LVL3). */
5932be4e471SJung-uk Kim     if (AcpiGbl_FADT.C3Latency <= 1000) {
59456a70eadSNate Lawson 	gas.Address = sc->cpu_p_blk + 5;
595907b6777SNate Lawson 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid, &gas,
596907b6777SNate Lawson 	    &cx_ptr->p_lvlx, RF_SHAREABLE);
5976b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
598907b6777SNate Lawson 	    sc->cpu_rid++;
5996b74f9b7SNate Lawson 	    cx_ptr->type = ACPI_STATE_C3;
6002be4e471SJung-uk Kim 	    cx_ptr->trans_lat = AcpiGbl_FADT.C3Latency;
6016b74f9b7SNate Lawson 	    cx_ptr++;
60256a70eadSNate Lawson 	    sc->cpu_cx_count++;
6036b74f9b7SNate Lawson 	}
6046b74f9b7SNate Lawson     }
6056b74f9b7SNate Lawson 
606907b6777SNate Lawson     /* Update the largest cx_count seen so far */
607907b6777SNate Lawson     if (sc->cpu_cx_count > cpu_cx_count)
608907b6777SNate Lawson 	cpu_cx_count = sc->cpu_cx_count;
6096b74f9b7SNate Lawson }
6106b74f9b7SNate Lawson 
6116b74f9b7SNate Lawson /*
6126b74f9b7SNate Lawson  * Parse a _CST package and set up its Cx states.  Since the _CST object
6136b74f9b7SNate Lawson  * can change dynamically, our notify handler may call this function
6146b74f9b7SNate Lawson  * to clean up and probe the new _CST package.
6156b74f9b7SNate Lawson  */
6166b74f9b7SNate Lawson static int
6176b74f9b7SNate Lawson acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
6186b74f9b7SNate Lawson {
6196b74f9b7SNate Lawson     struct	 acpi_cx *cx_ptr;
6206b74f9b7SNate Lawson     ACPI_STATUS	 status;
6216b74f9b7SNate Lawson     ACPI_BUFFER	 buf;
6226b74f9b7SNate Lawson     ACPI_OBJECT	*top;
6236b74f9b7SNate Lawson     ACPI_OBJECT	*pkg;
6246b74f9b7SNate Lawson     uint32_t	 count;
6256b74f9b7SNate Lawson     int		 i;
6266b74f9b7SNate Lawson 
62756a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
62856a70eadSNate Lawson 
6296b74f9b7SNate Lawson     buf.Pointer = NULL;
6306b74f9b7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
6316b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
632bd826803SNate Lawson     if (ACPI_FAILURE(status))
6336b74f9b7SNate Lawson 	return (ENXIO);
6346b74f9b7SNate Lawson 
6356b74f9b7SNate Lawson     /* _CST is a package with a count and at least one Cx package. */
6366b74f9b7SNate Lawson     top = (ACPI_OBJECT *)buf.Pointer;
63721cea91fSNate Lawson     if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
638bd826803SNate Lawson 	device_printf(sc->cpu_dev, "invalid _CST package\n");
6396b74f9b7SNate Lawson 	AcpiOsFree(buf.Pointer);
6406b74f9b7SNate Lawson 	return (ENXIO);
6416b74f9b7SNate Lawson     }
6426b74f9b7SNate Lawson     if (count != top->Package.Count - 1) {
643bd826803SNate Lawson 	device_printf(sc->cpu_dev, "invalid _CST state count (%d != %d)\n",
6446b74f9b7SNate Lawson 	       count, top->Package.Count - 1);
6456b74f9b7SNate Lawson 	count = top->Package.Count - 1;
6466b74f9b7SNate Lawson     }
6476b74f9b7SNate Lawson     if (count > MAX_CX_STATES) {
64856a70eadSNate Lawson 	device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
6496b74f9b7SNate Lawson 	count = MAX_CX_STATES;
6506b74f9b7SNate Lawson     }
6516b74f9b7SNate Lawson 
6526b74f9b7SNate Lawson     /* Set up all valid states. */
65356a70eadSNate Lawson     sc->cpu_cx_count = 0;
6546b74f9b7SNate Lawson     cx_ptr = sc->cpu_cx_states;
6556b74f9b7SNate Lawson     for (i = 0; i < count; i++) {
6566b74f9b7SNate Lawson 	pkg = &top->Package.Elements[i + 1];
65721cea91fSNate Lawson 	if (!ACPI_PKG_VALID(pkg, 4) ||
65821cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
65921cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
66021cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
66121cea91fSNate Lawson 
66280f006a1SNate Lawson 	    device_printf(sc->cpu_dev, "skipping invalid Cx state package\n");
6636b74f9b7SNate Lawson 	    continue;
6646b74f9b7SNate Lawson 	}
6656b74f9b7SNate Lawson 
6666b74f9b7SNate Lawson 	/* Validate the state to see if we should use it. */
6676b74f9b7SNate Lawson 	switch (cx_ptr->type) {
6686b74f9b7SNate Lawson 	case ACPI_STATE_C1:
669907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
6706b74f9b7SNate Lawson 	    cx_ptr++;
67156a70eadSNate Lawson 	    sc->cpu_cx_count++;
6726b74f9b7SNate Lawson 	    continue;
6736b74f9b7SNate Lawson 	case ACPI_STATE_C2:
6746b74f9b7SNate Lawson 	    if (cx_ptr->trans_lat > 100) {
67556a70eadSNate Lawson 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
67656a70eadSNate Lawson 				 "acpi_cpu%d: C2[%d] not available.\n",
67756a70eadSNate Lawson 				 device_get_unit(sc->cpu_dev), i));
6786b74f9b7SNate Lawson 		continue;
6796b74f9b7SNate Lawson 	    }
680907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
6816b74f9b7SNate Lawson 	    break;
6826b74f9b7SNate Lawson 	case ACPI_STATE_C3:
6836b74f9b7SNate Lawson 	default:
6846b74f9b7SNate Lawson 	    if (cx_ptr->trans_lat > 1000 ||
6856b74f9b7SNate Lawson 		(cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
6866b74f9b7SNate Lawson 
68756a70eadSNate Lawson 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
68856a70eadSNate Lawson 				 "acpi_cpu%d: C3[%d] not available.\n",
68956a70eadSNate Lawson 				 device_get_unit(sc->cpu_dev), i));
6906b74f9b7SNate Lawson 		continue;
6916b74f9b7SNate Lawson 	    }
6926b74f9b7SNate Lawson 	    break;
6936b74f9b7SNate Lawson 	}
6946b74f9b7SNate Lawson 
6956b74f9b7SNate Lawson #ifdef notyet
6966b74f9b7SNate Lawson 	/* Free up any previous register. */
6976b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
6986b74f9b7SNate Lawson 	    bus_release_resource(sc->cpu_dev, 0, 0, cx_ptr->p_lvlx);
6996b74f9b7SNate Lawson 	    cx_ptr->p_lvlx = NULL;
7006b74f9b7SNate Lawson 	}
7016b74f9b7SNate Lawson #endif
7026b74f9b7SNate Lawson 
7036b74f9b7SNate Lawson 	/* Allocate the control register for C2 or C3. */
704907b6777SNate Lawson 	acpi_PkgGas(sc->cpu_dev, pkg, 0, &cx_ptr->res_type, &sc->cpu_rid,
705907b6777SNate Lawson 	    &cx_ptr->p_lvlx, RF_SHAREABLE);
706f4eb0418SNate Lawson 	if (cx_ptr->p_lvlx) {
707907b6777SNate Lawson 	    sc->cpu_rid++;
70856a70eadSNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
70956a70eadSNate Lawson 			     "acpi_cpu%d: Got C%d - %d latency\n",
71056a70eadSNate Lawson 			     device_get_unit(sc->cpu_dev), cx_ptr->type,
71156a70eadSNate Lawson 			     cx_ptr->trans_lat));
7126b74f9b7SNate Lawson 	    cx_ptr++;
71356a70eadSNate Lawson 	    sc->cpu_cx_count++;
7146b74f9b7SNate Lawson 	}
7156b74f9b7SNate Lawson     }
7166b74f9b7SNate Lawson     AcpiOsFree(buf.Pointer);
7176b74f9b7SNate Lawson 
7186b74f9b7SNate Lawson     return (0);
719fec754d4SMike Smith }
720fec754d4SMike Smith 
721fec754d4SMike Smith /*
722fec754d4SMike Smith  * Call this *after* all CPUs have been attached.
723fec754d4SMike Smith  */
724fec754d4SMike Smith static void
7256b74f9b7SNate Lawson acpi_cpu_startup(void *arg)
726fec754d4SMike Smith {
72756a70eadSNate Lawson     struct acpi_cpu_softc *sc;
728907b6777SNate Lawson     int i;
729fec754d4SMike Smith 
730be2b1797SNate Lawson     /* Get set of CPU devices */
7313045c8afSNate Lawson     devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
732fec754d4SMike Smith 
73356a70eadSNate Lawson     /*
734907b6777SNate Lawson      * Setup any quirks that might necessary now that we have probed
735907b6777SNate Lawson      * all the CPUs
73656a70eadSNate Lawson      */
737907b6777SNate Lawson     acpi_cpu_quirks();
738907b6777SNate Lawson 
739907b6777SNate Lawson     cpu_cx_count = 0;
740907b6777SNate Lawson     if (cpu_cx_generic) {
741907b6777SNate Lawson 	/*
742907b6777SNate Lawson 	 * We are using generic Cx mode, probe for available Cx states
743907b6777SNate Lawson 	 * for all processors.
744907b6777SNate Lawson 	 */
74556a70eadSNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
74656a70eadSNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
747907b6777SNate Lawson 	    acpi_cpu_generic_cx_probe(sc);
74856a70eadSNate Lawson 	}
749907b6777SNate Lawson 
750907b6777SNate Lawson 	/*
751907b6777SNate Lawson 	 * Find the highest Cx state common to all CPUs
752907b6777SNate Lawson 	 * in the system, taking quirks into account.
753907b6777SNate Lawson 	 */
754907b6777SNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
755907b6777SNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
756907b6777SNate Lawson 	    if (sc->cpu_cx_count < cpu_cx_count)
757907b6777SNate Lawson 		cpu_cx_count = sc->cpu_cx_count;
758907b6777SNate Lawson 	}
759907b6777SNate Lawson     } else {
760907b6777SNate Lawson 	/*
761907b6777SNate Lawson 	 * We are using _CST mode, remove C3 state if necessary.
762907b6777SNate Lawson 	 * Update the largest Cx state supported in the global cpu_cx_count.
763907b6777SNate Lawson 	 * It will be used in the global Cx sysctl handler.
764907b6777SNate Lawson 	 * As we now know for sure that we will be using _CST mode
765907b6777SNate Lawson 	 * install our notify handler.
766907b6777SNate Lawson 	 */
767907b6777SNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
768907b6777SNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
7697a310721SJohn Baldwin 	    if (cpu_quirks & CPU_QUIRK_NO_C3) {
770907b6777SNate Lawson 		sc->cpu_cx_count = sc->cpu_non_c3 + 1;
771907b6777SNate Lawson 	    }
772907b6777SNate Lawson 	    if (sc->cpu_cx_count > cpu_cx_count)
773907b6777SNate Lawson 		cpu_cx_count = sc->cpu_cx_count;
774907b6777SNate Lawson 	    AcpiInstallNotifyHandler(sc->cpu_handle, ACPI_DEVICE_NOTIFY,
775907b6777SNate Lawson 		acpi_cpu_notify, sc);
776907b6777SNate Lawson 	}
777907b6777SNate Lawson     }
77856a70eadSNate Lawson 
7798c5468e3SNate Lawson     /* Perform Cx final initialization. */
780907b6777SNate Lawson     for (i = 0; i < cpu_ndevices; i++) {
781907b6777SNate Lawson 	sc = device_get_softc(cpu_devices[i]);
782907b6777SNate Lawson 	acpi_cpu_startup_cx(sc);
783907b6777SNate Lawson     }
784907b6777SNate Lawson 
785907b6777SNate Lawson     /* Add a sysctl handler to handle global Cx lowest setting */
786907b6777SNate Lawson     SYSCTL_ADD_PROC(&cpu_sysctl_ctx, SYSCTL_CHILDREN(cpu_sysctl_tree),
787907b6777SNate Lawson 	OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
788907b6777SNate Lawson 	NULL, 0, acpi_cpu_global_cx_lowest_sysctl, "A",
789907b6777SNate Lawson 	"Global lowest Cx sleep state to use");
790907b6777SNate Lawson 
791907b6777SNate Lawson     /* Take over idling from cpu_idle_default(). */
792907b6777SNate Lawson     cpu_cx_lowest = 0;
793907b6777SNate Lawson     cpu_disable_idle = FALSE;
794907b6777SNate Lawson     cpu_idle_hook = acpi_cpu_idle;
7956b74f9b7SNate Lawson }
7966b74f9b7SNate Lawson 
79756a70eadSNate Lawson static void
798907b6777SNate Lawson acpi_cpu_startup_cx(struct acpi_cpu_softc *sc)
79956a70eadSNate Lawson {
80056a70eadSNate Lawson     struct sbuf sb;
80156a70eadSNate Lawson     int i;
80256a70eadSNate Lawson 
803ae56b59fSNate Lawson     /*
804907b6777SNate Lawson      * Set up the list of Cx states
805ae56b59fSNate Lawson      */
806907b6777SNate Lawson     sc->cpu_non_c3 = 0;
807907b6777SNate Lawson     sbuf_new(&sb, sc->cpu_cx_supported, sizeof(sc->cpu_cx_supported),
808907b6777SNate Lawson 	SBUF_FIXEDLEN);
809907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++) {
810ccc09458SNate Lawson 	sbuf_printf(&sb, "C%d/%d ", i + 1, sc->cpu_cx_states[i].trans_lat);
811907b6777SNate Lawson 	if (sc->cpu_cx_states[i].type < ACPI_STATE_C3)
812907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
813ae56b59fSNate Lawson     }
81456a70eadSNate Lawson     sbuf_trim(&sb);
81556a70eadSNate Lawson     sbuf_finish(&sb);
816907b6777SNate Lawson 
817907b6777SNate Lawson     SYSCTL_ADD_STRING(&sc->cpu_sysctl_ctx,
818907b6777SNate Lawson 		      SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
819907b6777SNate Lawson 		      OID_AUTO, "cx_supported", CTLFLAG_RD,
820907b6777SNate Lawson 		      sc->cpu_cx_supported, 0,
821907b6777SNate Lawson 		      "Cx/microsecond values for supported Cx states");
822907b6777SNate Lawson     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
823907b6777SNate Lawson 		    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
824ccc09458SNate Lawson 		    OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
825907b6777SNate Lawson 		    (void *)sc, 0, acpi_cpu_cx_lowest_sysctl, "A",
82656a70eadSNate Lawson 		    "lowest Cx sleep state to use");
827907b6777SNate Lawson     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
828907b6777SNate Lawson 		    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
829a2afe45aSNate Lawson 		    OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
830907b6777SNate Lawson 		    (void *)sc, 0, acpi_cpu_usage_sysctl, "A",
831a2afe45aSNate Lawson 		    "percent usage for each Cx state");
83256a70eadSNate Lawson 
83356a70eadSNate Lawson #ifdef notyet
83456a70eadSNate Lawson     /* Signal platform that we can handle _CST notification. */
835907b6777SNate Lawson     if (!cpu_cx_generic && cpu_cst_cnt != 0) {
836d92a2ebdSNate Lawson 	ACPI_LOCK(acpi);
83756a70eadSNate Lawson 	AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
838d92a2ebdSNate Lawson 	ACPI_UNLOCK(acpi);
83956a70eadSNate Lawson     }
84056a70eadSNate Lawson #endif
84156a70eadSNate Lawson }
84256a70eadSNate Lawson 
843fec754d4SMike Smith /*
844a2afe45aSNate Lawson  * Idle the CPU in the lowest state possible.  This function is called with
845a2afe45aSNate Lawson  * interrupts disabled.  Note that once it re-enables interrupts, a task
846a2afe45aSNate Lawson  * switch can occur so do not access shared data (i.e. the softc) after
847a2afe45aSNate Lawson  * interrupts are re-enabled.
8486b74f9b7SNate Lawson  */
8496b74f9b7SNate Lawson static void
8506b74f9b7SNate Lawson acpi_cpu_idle()
8516b74f9b7SNate Lawson {
8526b74f9b7SNate Lawson     struct	acpi_cpu_softc *sc;
85356a70eadSNate Lawson     struct	acpi_cx *cx_next;
8546b74f9b7SNate Lawson     uint32_t	start_time, end_time;
855a2afe45aSNate Lawson     int		bm_active, cx_next_idx, i;
8566b74f9b7SNate Lawson 
8576b74f9b7SNate Lawson     /* If disabled, return immediately. */
858907b6777SNate Lawson     if (cpu_disable_idle) {
8596b74f9b7SNate Lawson 	ACPI_ENABLE_IRQS();
8606b74f9b7SNate Lawson 	return;
8616b74f9b7SNate Lawson     }
8626b74f9b7SNate Lawson 
863cd1f3db9SNate Lawson     /*
864cd1f3db9SNate Lawson      * Look up our CPU id to get our softc.  If it's NULL, we'll use C1
865cd1f3db9SNate Lawson      * since there is no ACPI processor object for this CPU.  This occurs
866cd1f3db9SNate Lawson      * for logical CPUs in the HTT case.
867cd1f3db9SNate Lawson      */
868cd1f3db9SNate Lawson     sc = cpu_softc[PCPU_GET(cpuid)];
869cd1f3db9SNate Lawson     if (sc == NULL) {
870cd1f3db9SNate Lawson 	acpi_cpu_c1();
871cd1f3db9SNate Lawson 	return;
872cd1f3db9SNate Lawson     }
873cd1f3db9SNate Lawson 
874a2afe45aSNate Lawson     /*
875a2afe45aSNate Lawson      * If we slept 100 us or more, use the lowest Cx state.  Otherwise,
876a2afe45aSNate Lawson      * find the lowest state that has a latency less than or equal to
877a2afe45aSNate Lawson      * the length of our last sleep.
878a2afe45aSNate Lawson      */
879907b6777SNate Lawson     cx_next_idx = sc->cpu_cx_lowest;
88080f006a1SNate Lawson     if (sc->cpu_prev_sleep < 100) {
88180f006a1SNate Lawson 	/*
88280f006a1SNate Lawson 	 * If we sleep too short all the time, this system may not implement
88380f006a1SNate Lawson 	 * C2/3 correctly (i.e. reads return immediately).  In this case,
88480f006a1SNate Lawson 	 * back off and use the next higher level.
885907b6777SNate Lawson 	 * It seems that when you have a dual core cpu (like the Intel Core Duo)
886907b6777SNate Lawson 	 * that both cores will get out of C3 state as soon as one of them
887907b6777SNate Lawson 	 * requires it. This breaks the sleep detection logic as the sleep
888907b6777SNate Lawson 	 * counter is local to each cpu. Disable the sleep logic for now as a
889907b6777SNate Lawson 	 * workaround if there's more than one CPU. The right fix would probably
890907b6777SNate Lawson 	 * be to add quirks for system that don't really support C3 state.
89180f006a1SNate Lawson 	 */
892907b6777SNate Lawson 	if (mp_ncpus < 2 && sc->cpu_prev_sleep <= 1) {
893907b6777SNate Lawson 	    sc->cpu_short_slp++;
894907b6777SNate Lawson 	    if (sc->cpu_short_slp == 1000 && sc->cpu_cx_lowest != 0) {
895907b6777SNate Lawson 		if (sc->cpu_non_c3 == sc->cpu_cx_lowest && sc->cpu_non_c3 != 0)
896907b6777SNate Lawson 		    sc->cpu_non_c3--;
897907b6777SNate Lawson 		sc->cpu_cx_lowest--;
898907b6777SNate Lawson 		sc->cpu_short_slp = 0;
89980f006a1SNate Lawson 		device_printf(sc->cpu_dev,
90080f006a1SNate Lawson 		    "too many short sleeps, backing off to C%d\n",
901907b6777SNate Lawson 		    sc->cpu_cx_lowest + 1);
90280f006a1SNate Lawson 	    }
90380f006a1SNate Lawson 	} else
904907b6777SNate Lawson 	    sc->cpu_short_slp = 0;
90580f006a1SNate Lawson 
906907b6777SNate Lawson 	for (i = sc->cpu_cx_lowest; i >= 0; i--)
907a2afe45aSNate Lawson 	    if (sc->cpu_cx_states[i].trans_lat <= sc->cpu_prev_sleep) {
908a2afe45aSNate Lawson 		cx_next_idx = i;
909a2afe45aSNate Lawson 		break;
910a2afe45aSNate Lawson 	    }
91180f006a1SNate Lawson     }
91256a70eadSNate Lawson 
9136b74f9b7SNate Lawson     /*
9146b74f9b7SNate Lawson      * Check for bus master activity.  If there was activity, clear
9156b74f9b7SNate Lawson      * the bit and use the lowest non-C3 state.  Note that the USB
9166b74f9b7SNate Lawson      * driver polling for new devices keeps this bit set all the
917f435261dSNate Lawson      * time if USB is loaded.
9186b74f9b7SNate Lawson      */
91918ececa0SNate Lawson     if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
9202be4e471SJung-uk Kim 	AcpiGetRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active);
9216b74f9b7SNate Lawson 	if (bm_active != 0) {
9222be4e471SJung-uk Kim 	    AcpiSetRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
923907b6777SNate Lawson 	    cx_next_idx = min(cx_next_idx, sc->cpu_non_c3);
9246b74f9b7SNate Lawson 	}
925f435261dSNate Lawson     }
9266b74f9b7SNate Lawson 
927a2afe45aSNate Lawson     /* Select the next state and update statistics. */
928a2afe45aSNate Lawson     cx_next = &sc->cpu_cx_states[cx_next_idx];
929907b6777SNate Lawson     sc->cpu_cx_stats[cx_next_idx]++;
930a2afe45aSNate Lawson     KASSERT(cx_next->type != ACPI_STATE_C0, ("acpi_cpu_idle: C0 sleep"));
9316b74f9b7SNate Lawson 
9326b74f9b7SNate Lawson     /*
933a2afe45aSNate Lawson      * Execute HLT (or equivalent) and wait for an interrupt.  We can't
934a2afe45aSNate Lawson      * calculate the time spent in C1 since the place we wake up is an
935a2afe45aSNate Lawson      * ISR.  Assume we slept one quantum and return.
9366b74f9b7SNate Lawson      */
937a2afe45aSNate Lawson     if (cx_next->type == ACPI_STATE_C1) {
938a2afe45aSNate Lawson 	sc->cpu_prev_sleep = 1000000 / hz;
939a2afe45aSNate Lawson 	acpi_cpu_c1();
940a2afe45aSNate Lawson 	return;
941a2afe45aSNate Lawson     }
942a2afe45aSNate Lawson 
943f435261dSNate Lawson     /*
944f435261dSNate Lawson      * For C3, disable bus master arbitration and enable bus master wake
945f435261dSNate Lawson      * if BM control is available, otherwise flush the CPU cache.
946f435261dSNate Lawson      */
947a2afe45aSNate Lawson     if (cx_next->type == ACPI_STATE_C3) {
948f435261dSNate Lawson 	if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
9492be4e471SJung-uk Kim 	    AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 1);
9502be4e471SJung-uk Kim 	    AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
951f435261dSNate Lawson 	} else
952f435261dSNate Lawson 	    ACPI_FLUSH_CPU_CACHE();
953a2afe45aSNate Lawson     }
954a2afe45aSNate Lawson 
9556b74f9b7SNate Lawson     /*
956a2afe45aSNate Lawson      * Read from P_LVLx to enter C2(+), checking time spent asleep.
9576b74f9b7SNate Lawson      * Use the ACPI timer for measuring sleep time.  Since we need to
9586b74f9b7SNate Lawson      * get the time very close to the CPU start/stop clock logic, this
9596b74f9b7SNate Lawson      * is the only reliable time source.
9606b74f9b7SNate Lawson      */
9612be4e471SJung-uk Kim     AcpiHwLowLevelRead(32, &start_time, &AcpiGbl_FADT.XPmTimerBlock);
9626b74f9b7SNate Lawson     CPU_GET_REG(cx_next->p_lvlx, 1);
9636b74f9b7SNate Lawson 
9646b74f9b7SNate Lawson     /*
9656b74f9b7SNate Lawson      * Read the end time twice.  Since it may take an arbitrary time
9666b74f9b7SNate Lawson      * to enter the idle state, the first read may be executed before
9676b74f9b7SNate Lawson      * the processor has stopped.  Doing it again provides enough
9686b74f9b7SNate Lawson      * margin that we are certain to have a correct value.
9696b74f9b7SNate Lawson      */
9702be4e471SJung-uk Kim     AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT.XPmTimerBlock);
9712be4e471SJung-uk Kim     AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT.XPmTimerBlock);
9726b74f9b7SNate Lawson 
9736b74f9b7SNate Lawson     /* Enable bus master arbitration and disable bus master wakeup. */
97418ececa0SNate Lawson     if (cx_next->type == ACPI_STATE_C3 &&
97518ececa0SNate Lawson 	(cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
9762be4e471SJung-uk Kim 	AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 0);
9772be4e471SJung-uk Kim 	AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
9786b74f9b7SNate Lawson     }
97980f006a1SNate Lawson     ACPI_ENABLE_IRQS();
9806b74f9b7SNate Lawson 
9816b74f9b7SNate Lawson     /* Find the actual time asleep in microseconds, minus overhead. */
982eea17c34SNate Lawson     end_time = acpi_TimerDelta(end_time, start_time);
983a2afe45aSNate Lawson     sc->cpu_prev_sleep = PM_USEC(end_time) - cx_next->trans_lat;
9846b74f9b7SNate Lawson }
9856b74f9b7SNate Lawson 
9866b74f9b7SNate Lawson /*
9878b888c66SNate Lawson  * Re-evaluate the _CST object when we are notified that it changed.
98856a70eadSNate Lawson  *
9896b74f9b7SNate Lawson  * XXX Re-evaluation disabled until locking is done.
9906b74f9b7SNate Lawson  */
9916b74f9b7SNate Lawson static void
9926b74f9b7SNate Lawson acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context)
9936b74f9b7SNate Lawson {
9946b74f9b7SNate Lawson     struct acpi_cpu_softc *sc = (struct acpi_cpu_softc *)context;
9956b74f9b7SNate Lawson 
9968b888c66SNate Lawson     if (notify != ACPI_NOTIFY_CX_STATES)
9978b888c66SNate Lawson 	return;
9988b888c66SNate Lawson 
9996b74f9b7SNate Lawson     device_printf(sc->cpu_dev, "Cx states changed\n");
10006b74f9b7SNate Lawson     /* acpi_cpu_cx_cst(sc); */
10016b74f9b7SNate Lawson }
10026b74f9b7SNate Lawson 
10036b74f9b7SNate Lawson static int
1004907b6777SNate Lawson acpi_cpu_quirks(void)
10056b74f9b7SNate Lawson {
1006ae56b59fSNate Lawson     device_t acpi_dev;
10076b74f9b7SNate Lawson 
10087826bf98SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10097826bf98SNate Lawson 
10106b74f9b7SNate Lawson     /*
1011907b6777SNate Lawson      * Bus mastering arbitration control is needed to keep caches coherent
1012907b6777SNate Lawson      * while sleeping in C3.  If it's not present but a working flush cache
1013907b6777SNate Lawson      * instruction is present, flush the caches before entering C3 instead.
1014907b6777SNate Lawson      * Otherwise, just disable C3 completely.
10156b74f9b7SNate Lawson      */
10162be4e471SJung-uk Kim     if (AcpiGbl_FADT.Pm2ControlBlock == 0 ||
10172be4e471SJung-uk Kim 	AcpiGbl_FADT.Pm2ControlLength == 0) {
10182be4e471SJung-uk Kim 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) &&
10192be4e471SJung-uk Kim 	    (AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0) {
1020907b6777SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
1021907b6777SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
102230dd6af3SNate Lawson 		"acpi_cpu: no BM control, using flush cache method\n"));
1023907b6777SNate Lawson 	} else {
1024907b6777SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_C3;
1025907b6777SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
102630dd6af3SNate Lawson 		"acpi_cpu: no BM control, C3 not available\n"));
1027907b6777SNate Lawson 	}
1028907b6777SNate Lawson     }
1029907b6777SNate Lawson 
1030907b6777SNate Lawson     /*
1031907b6777SNate Lawson      * If we are using generic Cx mode, C3 on multiple CPUs requires using
1032907b6777SNate Lawson      * the expensive flush cache instruction.
1033907b6777SNate Lawson      */
103430dd6af3SNate Lawson     if (cpu_cx_generic && mp_ncpus > 1) {
103518ececa0SNate Lawson 	cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
103630dd6af3SNate Lawson 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
103730dd6af3SNate Lawson 	    "acpi_cpu: SMP, using flush cache mode for C3\n"));
103830dd6af3SNate Lawson     }
10396b74f9b7SNate Lawson 
10406b74f9b7SNate Lawson     /* Look for various quirks of the PIIX4 part. */
10416b74f9b7SNate Lawson     acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
10426b74f9b7SNate Lawson     if (acpi_dev != NULL) {
10436b74f9b7SNate Lawson 	switch (pci_get_revid(acpi_dev)) {
10446b74f9b7SNate Lawson 	/*
10456b74f9b7SNate Lawson 	 * Disable C3 support for all PIIX4 chipsets.  Some of these parts
10466b74f9b7SNate Lawson 	 * do not report the BMIDE status to the BM status register and
10476b74f9b7SNate Lawson 	 * others have a livelock bug if Type-F DMA is enabled.  Linux
10486b74f9b7SNate Lawson 	 * works around the BMIDE bug by reading the BM status directly
10496b74f9b7SNate Lawson 	 * but we take the simpler approach of disabling C3 for these
10506b74f9b7SNate Lawson 	 * parts.
10516b74f9b7SNate Lawson 	 *
10526b74f9b7SNate Lawson 	 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
10536b74f9b7SNate Lawson 	 * Livelock") from the January 2002 PIIX4 specification update.
10546b74f9b7SNate Lawson 	 * Applies to all PIIX4 models.
10556b74f9b7SNate Lawson 	 */
10566b74f9b7SNate Lawson 	case PCI_REVISION_4E:
10576b74f9b7SNate Lawson 	case PCI_REVISION_4M:
10586b74f9b7SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_C3;
105930dd6af3SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
106030dd6af3SNate Lawson 		"acpi_cpu: working around PIIX4 bug, disabling C3\n"));
10616b74f9b7SNate Lawson 	    break;
10626b74f9b7SNate Lawson 	default:
10636b74f9b7SNate Lawson 	    break;
10646b74f9b7SNate Lawson 	}
10656b74f9b7SNate Lawson     }
10666b74f9b7SNate Lawson 
10676b74f9b7SNate Lawson     return (0);
10686b74f9b7SNate Lawson }
10696b74f9b7SNate Lawson 
10706b74f9b7SNate Lawson static int
1071a2afe45aSNate Lawson acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS)
10726b74f9b7SNate Lawson {
1073907b6777SNate Lawson     struct acpi_cpu_softc *sc;
10746b74f9b7SNate Lawson     struct sbuf	 sb;
10756b74f9b7SNate Lawson     char	 buf[128];
10766b74f9b7SNate Lawson     int		 i;
10774a03551dSNate Lawson     uintmax_t	 fract, sum, whole;
10786b74f9b7SNate Lawson 
1079907b6777SNate Lawson     sc = (struct acpi_cpu_softc *) arg1;
10803e7fa136SNate Lawson     sum = 0;
1081907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++)
1082907b6777SNate Lawson 	sum += sc->cpu_cx_stats[i];
10836b74f9b7SNate Lawson     sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1084907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++) {
10853e7fa136SNate Lawson 	if (sum > 0) {
1086907b6777SNate Lawson 	    whole = (uintmax_t)sc->cpu_cx_stats[i] * 100;
10873e7fa136SNate Lawson 	    fract = (whole % sum) * 100;
10883e7fa136SNate Lawson 	    sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
10893e7fa136SNate Lawson 		(u_int)(fract / sum));
10903e7fa136SNate Lawson 	} else
10913e7fa136SNate Lawson 	    sbuf_printf(&sb, "0%% ");
10923e7fa136SNate Lawson     }
10936b74f9b7SNate Lawson     sbuf_trim(&sb);
10946b74f9b7SNate Lawson     sbuf_finish(&sb);
1095ccc09458SNate Lawson     sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1096ccc09458SNate Lawson     sbuf_delete(&sb);
10976b74f9b7SNate Lawson 
10986b74f9b7SNate Lawson     return (0);
10996b74f9b7SNate Lawson }
11006b74f9b7SNate Lawson 
11016b74f9b7SNate Lawson static int
1102b13cf774SNate Lawson acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val)
11036b74f9b7SNate Lawson {
1104b13cf774SNate Lawson     int i;
11056b74f9b7SNate Lawson 
1106b13cf774SNate Lawson     ACPI_SERIAL_ASSERT(cpu);
1107907b6777SNate Lawson     sc->cpu_cx_lowest = val;
1108907b6777SNate Lawson 
1109907b6777SNate Lawson     /* If not disabling, cache the new lowest non-C3 state. */
1110907b6777SNate Lawson     sc->cpu_non_c3 = 0;
1111907b6777SNate Lawson     for (i = sc->cpu_cx_lowest; i >= 0; i--) {
1112907b6777SNate Lawson 	if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
1113907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
1114907b6777SNate Lawson 	    break;
1115907b6777SNate Lawson 	}
1116907b6777SNate Lawson     }
1117907b6777SNate Lawson 
1118907b6777SNate Lawson     /* Reset the statistics counters. */
1119907b6777SNate Lawson     bzero(sc->cpu_cx_stats, sizeof(sc->cpu_cx_stats));
1120b13cf774SNate Lawson     return (0);
1121b13cf774SNate Lawson }
1122b13cf774SNate Lawson 
1123b13cf774SNate Lawson static int
1124b13cf774SNate Lawson acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1125b13cf774SNate Lawson {
1126b13cf774SNate Lawson     struct	 acpi_cpu_softc *sc;
1127b13cf774SNate Lawson     char	 state[8];
1128b13cf774SNate Lawson     int		 val, error;
1129b13cf774SNate Lawson 
1130b13cf774SNate Lawson     sc = (struct acpi_cpu_softc *) arg1;
1131b13cf774SNate Lawson     snprintf(state, sizeof(state), "C%d", sc->cpu_cx_lowest + 1);
1132b13cf774SNate Lawson     error = sysctl_handle_string(oidp, state, sizeof(state), req);
1133b13cf774SNate Lawson     if (error != 0 || req->newptr == NULL)
1134b13cf774SNate Lawson 	return (error);
1135b13cf774SNate Lawson     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1136b13cf774SNate Lawson 	return (EINVAL);
1137b13cf774SNate Lawson     val = (int) strtol(state + 1, NULL, 10) - 1;
1138b13cf774SNate Lawson     if (val < 0 || val > sc->cpu_cx_count - 1)
1139b13cf774SNate Lawson 	return (EINVAL);
1140b13cf774SNate Lawson 
1141b13cf774SNate Lawson     ACPI_SERIAL_BEGIN(cpu);
1142b13cf774SNate Lawson     acpi_cpu_set_cx_lowest(sc, val);
1143907b6777SNate Lawson     ACPI_SERIAL_END(cpu);
1144907b6777SNate Lawson 
1145907b6777SNate Lawson     return (0);
1146907b6777SNate Lawson }
1147907b6777SNate Lawson 
1148907b6777SNate Lawson static int
1149907b6777SNate Lawson acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1150907b6777SNate Lawson {
1151907b6777SNate Lawson     struct	acpi_cpu_softc *sc;
1152907b6777SNate Lawson     char	state[8];
1153b13cf774SNate Lawson     int		val, error, i;
1154907b6777SNate Lawson 
1155ccc09458SNate Lawson     snprintf(state, sizeof(state), "C%d", cpu_cx_lowest + 1);
1156ccc09458SNate Lawson     error = sysctl_handle_string(oidp, state, sizeof(state), req);
11576b74f9b7SNate Lawson     if (error != 0 || req->newptr == NULL)
11586b74f9b7SNate Lawson 	return (error);
1159ccc09458SNate Lawson     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1160ccc09458SNate Lawson 	return (EINVAL);
1161ccc09458SNate Lawson     val = (int) strtol(state + 1, NULL, 10) - 1;
116256a70eadSNate Lawson     if (val < 0 || val > cpu_cx_count - 1)
11636b74f9b7SNate Lawson 	return (EINVAL);
11646b74f9b7SNate Lawson     cpu_cx_lowest = val;
11656b74f9b7SNate Lawson 
1166b13cf774SNate Lawson     /* Update the new lowest useable Cx state for all CPUs. */
1167907b6777SNate Lawson     ACPI_SERIAL_BEGIN(cpu);
1168907b6777SNate Lawson     for (i = 0; i < cpu_ndevices; i++) {
1169907b6777SNate Lawson 	sc = device_get_softc(cpu_devices[i]);
1170b13cf774SNate Lawson 	acpi_cpu_set_cx_lowest(sc, val);
1171907b6777SNate Lawson     }
1172d92a2ebdSNate Lawson     ACPI_SERIAL_END(cpu);
11736b74f9b7SNate Lawson 
11746b74f9b7SNate Lawson     return (0);
11756b74f9b7SNate Lawson }
1176