xref: /freebsd/sys/dev/acpica/acpi_cpu.c (revision c961faca8cbe1ef16544e91283322e7abc8eeb13)
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 
50456a70eadSNate Lawson     /* Disable any entry to the idle function. */
505907b6777SNate Lawson     cpu_disable_idle = TRUE;
50656a70eadSNate Lawson 
507a2afe45aSNate Lawson     /* Signal and wait for all processors to exit acpi_cpu_idle(). */
50856a70eadSNate Lawson     smp_rendezvous(NULL, NULL, NULL, NULL);
50956a70eadSNate Lawson 
51056a70eadSNate Lawson     return_VALUE (0);
51156a70eadSNate Lawson }
51256a70eadSNate Lawson 
513907b6777SNate Lawson static void
5146b74f9b7SNate Lawson acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
5156b74f9b7SNate Lawson {
516907b6777SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
517907b6777SNate Lawson 
518907b6777SNate Lawson     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
519907b6777SNate Lawson     sc->cpu_prev_sleep = 1000000;
520907b6777SNate Lawson     sc->cpu_cx_lowest = 0;
521907b6777SNate Lawson 
522907b6777SNate Lawson     /*
523907b6777SNate Lawson      * Check for the ACPI 2.0 _CST sleep states object. If we can't find
524907b6777SNate Lawson      * any, we'll revert to generic FADT/P_BLK Cx control method which will
525907b6777SNate Lawson      * be handled by acpi_cpu_startup. We need to defer to after having
526907b6777SNate Lawson      * probed all the cpus in the system before probing for generic Cx
527907b6777SNate Lawson      * states as we may already have found cpus with valid _CST packages
528907b6777SNate Lawson      */
529907b6777SNate Lawson     if (!cpu_cx_generic && acpi_cpu_cx_cst(sc) != 0) {
530907b6777SNate Lawson 	/*
531907b6777SNate Lawson 	 * We were unable to find a _CST package for this cpu or there
532907b6777SNate Lawson 	 * was an error parsing it. Switch back to generic mode.
533907b6777SNate Lawson 	 */
534907b6777SNate Lawson 	cpu_cx_generic = TRUE;
535bd826803SNate Lawson 	if (bootverbose)
536bd826803SNate Lawson 	    device_printf(sc->cpu_dev, "switching to generic Cx mode\n");
537907b6777SNate Lawson     }
538907b6777SNate Lawson 
539907b6777SNate Lawson     /*
540907b6777SNate Lawson      * TODO: _CSD Package should be checked here.
541907b6777SNate Lawson      */
542907b6777SNate Lawson }
543907b6777SNate Lawson 
544907b6777SNate Lawson static void
545907b6777SNate Lawson acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc)
546907b6777SNate Lawson {
5476b74f9b7SNate Lawson     ACPI_GENERIC_ADDRESS	 gas;
5486b74f9b7SNate Lawson     struct acpi_cx		*cx_ptr;
54956a70eadSNate Lawson 
55056a70eadSNate Lawson     sc->cpu_cx_count = 0;
5516b74f9b7SNate Lawson     cx_ptr = sc->cpu_cx_states;
552fec754d4SMike Smith 
553907b6777SNate Lawson     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
554907b6777SNate Lawson     sc->cpu_prev_sleep = 1000000;
555907b6777SNate Lawson 
5566b74f9b7SNate Lawson     /* C1 has been required since just after ACPI 1.0 */
5576b74f9b7SNate Lawson     cx_ptr->type = ACPI_STATE_C1;
5586b74f9b7SNate Lawson     cx_ptr->trans_lat = 0;
5596b74f9b7SNate Lawson     cx_ptr++;
56056a70eadSNate Lawson     sc->cpu_cx_count++;
56156a70eadSNate Lawson 
562c181b89bSNate Lawson     /*
563c181b89bSNate Lawson      * The spec says P_BLK must be 6 bytes long.  However, some systems
564c181b89bSNate Lawson      * use it to indicate a fractional set of features present so we
565c181b89bSNate Lawson      * take 5 as C2.  Some may also have a value of 7 to indicate
566c181b89bSNate Lawson      * another C3 but most use _CST for this (as required) and having
567c181b89bSNate Lawson      * "only" C1-C3 is not a hardship.
568c181b89bSNate Lawson      */
569c181b89bSNate Lawson     if (sc->cpu_p_blk_len < 5)
570907b6777SNate Lawson 	return;
571fec754d4SMike Smith 
5726b74f9b7SNate Lawson     /* Validate and allocate resources for C2 (P_LVL2). */
5732be4e471SJung-uk Kim     gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
5742be4e471SJung-uk Kim     gas.BitWidth = 8;
5752be4e471SJung-uk Kim     if (AcpiGbl_FADT.C2Latency <= 100) {
57656a70eadSNate Lawson 	gas.Address = sc->cpu_p_blk + 4;
577907b6777SNate Lawson 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid,
578907b6777SNate Lawson 	    &gas, &cx_ptr->p_lvlx, RF_SHAREABLE);
5796b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
580907b6777SNate Lawson 	    sc->cpu_rid++;
5816b74f9b7SNate Lawson 	    cx_ptr->type = ACPI_STATE_C2;
5822be4e471SJung-uk Kim 	    cx_ptr->trans_lat = AcpiGbl_FADT.C2Latency;
5836b74f9b7SNate Lawson 	    cx_ptr++;
58456a70eadSNate Lawson 	    sc->cpu_cx_count++;
585fec754d4SMike Smith 	}
586fec754d4SMike Smith     }
587c181b89bSNate Lawson     if (sc->cpu_p_blk_len < 6)
588907b6777SNate Lawson 	return;
589be2b1797SNate Lawson 
5906b74f9b7SNate Lawson     /* Validate and allocate resources for C3 (P_LVL3). */
5912be4e471SJung-uk Kim     if (AcpiGbl_FADT.C3Latency <= 1000) {
59256a70eadSNate Lawson 	gas.Address = sc->cpu_p_blk + 5;
593907b6777SNate Lawson 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid, &gas,
594907b6777SNate Lawson 	    &cx_ptr->p_lvlx, RF_SHAREABLE);
5956b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
596907b6777SNate Lawson 	    sc->cpu_rid++;
5976b74f9b7SNate Lawson 	    cx_ptr->type = ACPI_STATE_C3;
5982be4e471SJung-uk Kim 	    cx_ptr->trans_lat = AcpiGbl_FADT.C3Latency;
5996b74f9b7SNate Lawson 	    cx_ptr++;
60056a70eadSNate Lawson 	    sc->cpu_cx_count++;
6016b74f9b7SNate Lawson 	}
6026b74f9b7SNate Lawson     }
6036b74f9b7SNate Lawson 
604907b6777SNate Lawson     /* Update the largest cx_count seen so far */
605907b6777SNate Lawson     if (sc->cpu_cx_count > cpu_cx_count)
606907b6777SNate Lawson 	cpu_cx_count = sc->cpu_cx_count;
6076b74f9b7SNate Lawson }
6086b74f9b7SNate Lawson 
6096b74f9b7SNate Lawson /*
6106b74f9b7SNate Lawson  * Parse a _CST package and set up its Cx states.  Since the _CST object
6116b74f9b7SNate Lawson  * can change dynamically, our notify handler may call this function
6126b74f9b7SNate Lawson  * to clean up and probe the new _CST package.
6136b74f9b7SNate Lawson  */
6146b74f9b7SNate Lawson static int
6156b74f9b7SNate Lawson acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
6166b74f9b7SNate Lawson {
6176b74f9b7SNate Lawson     struct	 acpi_cx *cx_ptr;
6186b74f9b7SNate Lawson     ACPI_STATUS	 status;
6196b74f9b7SNate Lawson     ACPI_BUFFER	 buf;
6206b74f9b7SNate Lawson     ACPI_OBJECT	*top;
6216b74f9b7SNate Lawson     ACPI_OBJECT	*pkg;
6226b74f9b7SNate Lawson     uint32_t	 count;
6236b74f9b7SNate Lawson     int		 i;
6246b74f9b7SNate Lawson 
62556a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
62656a70eadSNate Lawson 
6276b74f9b7SNate Lawson     buf.Pointer = NULL;
6286b74f9b7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
6296b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
630bd826803SNate Lawson     if (ACPI_FAILURE(status))
6316b74f9b7SNate Lawson 	return (ENXIO);
6326b74f9b7SNate Lawson 
6336b74f9b7SNate Lawson     /* _CST is a package with a count and at least one Cx package. */
6346b74f9b7SNate Lawson     top = (ACPI_OBJECT *)buf.Pointer;
63521cea91fSNate Lawson     if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
636bd826803SNate Lawson 	device_printf(sc->cpu_dev, "invalid _CST package\n");
6376b74f9b7SNate Lawson 	AcpiOsFree(buf.Pointer);
6386b74f9b7SNate Lawson 	return (ENXIO);
6396b74f9b7SNate Lawson     }
6406b74f9b7SNate Lawson     if (count != top->Package.Count - 1) {
641bd826803SNate Lawson 	device_printf(sc->cpu_dev, "invalid _CST state count (%d != %d)\n",
6426b74f9b7SNate Lawson 	       count, top->Package.Count - 1);
6436b74f9b7SNate Lawson 	count = top->Package.Count - 1;
6446b74f9b7SNate Lawson     }
6456b74f9b7SNate Lawson     if (count > MAX_CX_STATES) {
64656a70eadSNate Lawson 	device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
6476b74f9b7SNate Lawson 	count = MAX_CX_STATES;
6486b74f9b7SNate Lawson     }
6496b74f9b7SNate Lawson 
6506b74f9b7SNate Lawson     /* Set up all valid states. */
65156a70eadSNate Lawson     sc->cpu_cx_count = 0;
6526b74f9b7SNate Lawson     cx_ptr = sc->cpu_cx_states;
6536b74f9b7SNate Lawson     for (i = 0; i < count; i++) {
6546b74f9b7SNate Lawson 	pkg = &top->Package.Elements[i + 1];
65521cea91fSNate Lawson 	if (!ACPI_PKG_VALID(pkg, 4) ||
65621cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
65721cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
65821cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
65921cea91fSNate Lawson 
66080f006a1SNate Lawson 	    device_printf(sc->cpu_dev, "skipping invalid Cx state package\n");
6616b74f9b7SNate Lawson 	    continue;
6626b74f9b7SNate Lawson 	}
6636b74f9b7SNate Lawson 
6646b74f9b7SNate Lawson 	/* Validate the state to see if we should use it. */
6656b74f9b7SNate Lawson 	switch (cx_ptr->type) {
6666b74f9b7SNate Lawson 	case ACPI_STATE_C1:
667907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
6686b74f9b7SNate Lawson 	    cx_ptr++;
66956a70eadSNate Lawson 	    sc->cpu_cx_count++;
6706b74f9b7SNate Lawson 	    continue;
6716b74f9b7SNate Lawson 	case ACPI_STATE_C2:
6726b74f9b7SNate Lawson 	    if (cx_ptr->trans_lat > 100) {
67356a70eadSNate Lawson 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
67456a70eadSNate Lawson 				 "acpi_cpu%d: C2[%d] not available.\n",
67556a70eadSNate Lawson 				 device_get_unit(sc->cpu_dev), i));
6766b74f9b7SNate Lawson 		continue;
6776b74f9b7SNate Lawson 	    }
678907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
6796b74f9b7SNate Lawson 	    break;
6806b74f9b7SNate Lawson 	case ACPI_STATE_C3:
6816b74f9b7SNate Lawson 	default:
6826b74f9b7SNate Lawson 	    if (cx_ptr->trans_lat > 1000 ||
6836b74f9b7SNate Lawson 		(cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
6846b74f9b7SNate Lawson 
68556a70eadSNate Lawson 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
68656a70eadSNate Lawson 				 "acpi_cpu%d: C3[%d] not available.\n",
68756a70eadSNate Lawson 				 device_get_unit(sc->cpu_dev), i));
6886b74f9b7SNate Lawson 		continue;
6896b74f9b7SNate Lawson 	    }
6906b74f9b7SNate Lawson 	    break;
6916b74f9b7SNate Lawson 	}
6926b74f9b7SNate Lawson 
6936b74f9b7SNate Lawson #ifdef notyet
6946b74f9b7SNate Lawson 	/* Free up any previous register. */
6956b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
6966b74f9b7SNate Lawson 	    bus_release_resource(sc->cpu_dev, 0, 0, cx_ptr->p_lvlx);
6976b74f9b7SNate Lawson 	    cx_ptr->p_lvlx = NULL;
6986b74f9b7SNate Lawson 	}
6996b74f9b7SNate Lawson #endif
7006b74f9b7SNate Lawson 
7016b74f9b7SNate Lawson 	/* Allocate the control register for C2 or C3. */
702907b6777SNate Lawson 	acpi_PkgGas(sc->cpu_dev, pkg, 0, &cx_ptr->res_type, &sc->cpu_rid,
703907b6777SNate Lawson 	    &cx_ptr->p_lvlx, RF_SHAREABLE);
704f4eb0418SNate Lawson 	if (cx_ptr->p_lvlx) {
705907b6777SNate Lawson 	    sc->cpu_rid++;
70656a70eadSNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
70756a70eadSNate Lawson 			     "acpi_cpu%d: Got C%d - %d latency\n",
70856a70eadSNate Lawson 			     device_get_unit(sc->cpu_dev), cx_ptr->type,
70956a70eadSNate Lawson 			     cx_ptr->trans_lat));
7106b74f9b7SNate Lawson 	    cx_ptr++;
71156a70eadSNate Lawson 	    sc->cpu_cx_count++;
7126b74f9b7SNate Lawson 	}
7136b74f9b7SNate Lawson     }
7146b74f9b7SNate Lawson     AcpiOsFree(buf.Pointer);
7156b74f9b7SNate Lawson 
7166b74f9b7SNate Lawson     return (0);
717fec754d4SMike Smith }
718fec754d4SMike Smith 
719fec754d4SMike Smith /*
720fec754d4SMike Smith  * Call this *after* all CPUs have been attached.
721fec754d4SMike Smith  */
722fec754d4SMike Smith static void
7236b74f9b7SNate Lawson acpi_cpu_startup(void *arg)
724fec754d4SMike Smith {
72556a70eadSNate Lawson     struct acpi_cpu_softc *sc;
726907b6777SNate Lawson     int i;
727fec754d4SMike Smith 
728be2b1797SNate Lawson     /* Get set of CPU devices */
7293045c8afSNate Lawson     devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
730fec754d4SMike Smith 
73156a70eadSNate Lawson     /*
732907b6777SNate Lawson      * Setup any quirks that might necessary now that we have probed
733907b6777SNate Lawson      * all the CPUs
73456a70eadSNate Lawson      */
735907b6777SNate Lawson     acpi_cpu_quirks();
736907b6777SNate Lawson 
737907b6777SNate Lawson     cpu_cx_count = 0;
738907b6777SNate Lawson     if (cpu_cx_generic) {
739907b6777SNate Lawson 	/*
740907b6777SNate Lawson 	 * We are using generic Cx mode, probe for available Cx states
741907b6777SNate Lawson 	 * for all processors.
742907b6777SNate Lawson 	 */
74356a70eadSNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
74456a70eadSNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
745907b6777SNate Lawson 	    acpi_cpu_generic_cx_probe(sc);
74656a70eadSNate Lawson 	}
747907b6777SNate Lawson 
748907b6777SNate Lawson 	/*
749907b6777SNate Lawson 	 * Find the highest Cx state common to all CPUs
750907b6777SNate Lawson 	 * in the system, taking quirks into account.
751907b6777SNate Lawson 	 */
752907b6777SNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
753907b6777SNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
754907b6777SNate Lawson 	    if (sc->cpu_cx_count < cpu_cx_count)
755907b6777SNate Lawson 		cpu_cx_count = sc->cpu_cx_count;
756907b6777SNate Lawson 	}
757907b6777SNate Lawson     } else {
758907b6777SNate Lawson 	/*
759907b6777SNate Lawson 	 * We are using _CST mode, remove C3 state if necessary.
760907b6777SNate Lawson 	 * Update the largest Cx state supported in the global cpu_cx_count.
761907b6777SNate Lawson 	 * It will be used in the global Cx sysctl handler.
762907b6777SNate Lawson 	 * As we now know for sure that we will be using _CST mode
763907b6777SNate Lawson 	 * install our notify handler.
764907b6777SNate Lawson 	 */
765907b6777SNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
766907b6777SNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
767907b6777SNate Lawson 	    if (cpu_quirks && CPU_QUIRK_NO_C3) {
768907b6777SNate Lawson 		sc->cpu_cx_count = sc->cpu_non_c3 + 1;
769907b6777SNate Lawson 	    }
770907b6777SNate Lawson 	    if (sc->cpu_cx_count > cpu_cx_count)
771907b6777SNate Lawson 		cpu_cx_count = sc->cpu_cx_count;
772907b6777SNate Lawson 	    AcpiInstallNotifyHandler(sc->cpu_handle, ACPI_DEVICE_NOTIFY,
773907b6777SNate Lawson 		acpi_cpu_notify, sc);
774907b6777SNate Lawson 	}
775907b6777SNate Lawson     }
77656a70eadSNate Lawson 
7778c5468e3SNate Lawson     /* Perform Cx final initialization. */
778907b6777SNate Lawson     for (i = 0; i < cpu_ndevices; i++) {
779907b6777SNate Lawson 	sc = device_get_softc(cpu_devices[i]);
780907b6777SNate Lawson 	acpi_cpu_startup_cx(sc);
781907b6777SNate Lawson     }
782907b6777SNate Lawson 
783907b6777SNate Lawson     /* Add a sysctl handler to handle global Cx lowest setting */
784907b6777SNate Lawson     SYSCTL_ADD_PROC(&cpu_sysctl_ctx, SYSCTL_CHILDREN(cpu_sysctl_tree),
785907b6777SNate Lawson 	OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
786907b6777SNate Lawson 	NULL, 0, acpi_cpu_global_cx_lowest_sysctl, "A",
787907b6777SNate Lawson 	"Global lowest Cx sleep state to use");
788907b6777SNate Lawson 
789907b6777SNate Lawson     /* Take over idling from cpu_idle_default(). */
790907b6777SNate Lawson     cpu_cx_lowest = 0;
791907b6777SNate Lawson     cpu_disable_idle = FALSE;
792907b6777SNate Lawson     cpu_idle_hook = acpi_cpu_idle;
7936b74f9b7SNate Lawson }
7946b74f9b7SNate Lawson 
79556a70eadSNate Lawson static void
796907b6777SNate Lawson acpi_cpu_startup_cx(struct acpi_cpu_softc *sc)
79756a70eadSNate Lawson {
79856a70eadSNate Lawson     struct sbuf sb;
79956a70eadSNate Lawson     int i;
80056a70eadSNate Lawson 
801ae56b59fSNate Lawson     /*
802907b6777SNate Lawson      * Set up the list of Cx states
803ae56b59fSNate Lawson      */
804907b6777SNate Lawson     sc->cpu_non_c3 = 0;
805907b6777SNate Lawson     sbuf_new(&sb, sc->cpu_cx_supported, sizeof(sc->cpu_cx_supported),
806907b6777SNate Lawson 	SBUF_FIXEDLEN);
807907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++) {
808ccc09458SNate Lawson 	sbuf_printf(&sb, "C%d/%d ", i + 1, sc->cpu_cx_states[i].trans_lat);
809907b6777SNate Lawson 	if (sc->cpu_cx_states[i].type < ACPI_STATE_C3)
810907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
811ae56b59fSNate Lawson     }
81256a70eadSNate Lawson     sbuf_trim(&sb);
81356a70eadSNate Lawson     sbuf_finish(&sb);
814907b6777SNate Lawson 
815907b6777SNate Lawson     SYSCTL_ADD_STRING(&sc->cpu_sysctl_ctx,
816907b6777SNate Lawson 		      SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
817907b6777SNate Lawson 		      OID_AUTO, "cx_supported", CTLFLAG_RD,
818907b6777SNate Lawson 		      sc->cpu_cx_supported, 0,
819907b6777SNate Lawson 		      "Cx/microsecond values for supported Cx states");
820907b6777SNate Lawson     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
821907b6777SNate Lawson 		    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
822ccc09458SNate Lawson 		    OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
823907b6777SNate Lawson 		    (void *)sc, 0, acpi_cpu_cx_lowest_sysctl, "A",
82456a70eadSNate Lawson 		    "lowest Cx sleep state to use");
825907b6777SNate Lawson     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
826907b6777SNate Lawson 		    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
827a2afe45aSNate Lawson 		    OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
828907b6777SNate Lawson 		    (void *)sc, 0, acpi_cpu_usage_sysctl, "A",
829a2afe45aSNate Lawson 		    "percent usage for each Cx state");
83056a70eadSNate Lawson 
83156a70eadSNate Lawson #ifdef notyet
83256a70eadSNate Lawson     /* Signal platform that we can handle _CST notification. */
833907b6777SNate Lawson     if (!cpu_cx_generic && cpu_cst_cnt != 0) {
834d92a2ebdSNate Lawson 	ACPI_LOCK(acpi);
83556a70eadSNate Lawson 	AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
836d92a2ebdSNate Lawson 	ACPI_UNLOCK(acpi);
83756a70eadSNate Lawson     }
83856a70eadSNate Lawson #endif
83956a70eadSNate Lawson }
84056a70eadSNate Lawson 
841fec754d4SMike Smith /*
842a2afe45aSNate Lawson  * Idle the CPU in the lowest state possible.  This function is called with
843a2afe45aSNate Lawson  * interrupts disabled.  Note that once it re-enables interrupts, a task
844a2afe45aSNate Lawson  * switch can occur so do not access shared data (i.e. the softc) after
845a2afe45aSNate Lawson  * interrupts are re-enabled.
8466b74f9b7SNate Lawson  */
8476b74f9b7SNate Lawson static void
8486b74f9b7SNate Lawson acpi_cpu_idle()
8496b74f9b7SNate Lawson {
8506b74f9b7SNate Lawson     struct	acpi_cpu_softc *sc;
85156a70eadSNate Lawson     struct	acpi_cx *cx_next;
8526b74f9b7SNate Lawson     uint32_t	start_time, end_time;
853a2afe45aSNate Lawson     int		bm_active, cx_next_idx, i;
8546b74f9b7SNate Lawson 
8556b74f9b7SNate Lawson     /* If disabled, return immediately. */
856907b6777SNate Lawson     if (cpu_disable_idle) {
8576b74f9b7SNate Lawson 	ACPI_ENABLE_IRQS();
8586b74f9b7SNate Lawson 	return;
8596b74f9b7SNate Lawson     }
8606b74f9b7SNate Lawson 
861cd1f3db9SNate Lawson     /*
862cd1f3db9SNate Lawson      * Look up our CPU id to get our softc.  If it's NULL, we'll use C1
863cd1f3db9SNate Lawson      * since there is no ACPI processor object for this CPU.  This occurs
864cd1f3db9SNate Lawson      * for logical CPUs in the HTT case.
865cd1f3db9SNate Lawson      */
866cd1f3db9SNate Lawson     sc = cpu_softc[PCPU_GET(cpuid)];
867cd1f3db9SNate Lawson     if (sc == NULL) {
868cd1f3db9SNate Lawson 	acpi_cpu_c1();
869cd1f3db9SNate Lawson 	return;
870cd1f3db9SNate Lawson     }
871cd1f3db9SNate Lawson 
872a2afe45aSNate Lawson     /*
873a2afe45aSNate Lawson      * If we slept 100 us or more, use the lowest Cx state.  Otherwise,
874a2afe45aSNate Lawson      * find the lowest state that has a latency less than or equal to
875a2afe45aSNate Lawson      * the length of our last sleep.
876a2afe45aSNate Lawson      */
877907b6777SNate Lawson     cx_next_idx = sc->cpu_cx_lowest;
87880f006a1SNate Lawson     if (sc->cpu_prev_sleep < 100) {
87980f006a1SNate Lawson 	/*
88080f006a1SNate Lawson 	 * If we sleep too short all the time, this system may not implement
88180f006a1SNate Lawson 	 * C2/3 correctly (i.e. reads return immediately).  In this case,
88280f006a1SNate Lawson 	 * back off and use the next higher level.
883907b6777SNate Lawson 	 * It seems that when you have a dual core cpu (like the Intel Core Duo)
884907b6777SNate Lawson 	 * that both cores will get out of C3 state as soon as one of them
885907b6777SNate Lawson 	 * requires it. This breaks the sleep detection logic as the sleep
886907b6777SNate Lawson 	 * counter is local to each cpu. Disable the sleep logic for now as a
887907b6777SNate Lawson 	 * workaround if there's more than one CPU. The right fix would probably
888907b6777SNate Lawson 	 * be to add quirks for system that don't really support C3 state.
88980f006a1SNate Lawson 	 */
890907b6777SNate Lawson 	if (mp_ncpus < 2 && sc->cpu_prev_sleep <= 1) {
891907b6777SNate Lawson 	    sc->cpu_short_slp++;
892907b6777SNate Lawson 	    if (sc->cpu_short_slp == 1000 && sc->cpu_cx_lowest != 0) {
893907b6777SNate Lawson 		if (sc->cpu_non_c3 == sc->cpu_cx_lowest && sc->cpu_non_c3 != 0)
894907b6777SNate Lawson 		    sc->cpu_non_c3--;
895907b6777SNate Lawson 		sc->cpu_cx_lowest--;
896907b6777SNate Lawson 		sc->cpu_short_slp = 0;
89780f006a1SNate Lawson 		device_printf(sc->cpu_dev,
89880f006a1SNate Lawson 		    "too many short sleeps, backing off to C%d\n",
899907b6777SNate Lawson 		    sc->cpu_cx_lowest + 1);
90080f006a1SNate Lawson 	    }
90180f006a1SNate Lawson 	} else
902907b6777SNate Lawson 	    sc->cpu_short_slp = 0;
90380f006a1SNate Lawson 
904907b6777SNate Lawson 	for (i = sc->cpu_cx_lowest; i >= 0; i--)
905a2afe45aSNate Lawson 	    if (sc->cpu_cx_states[i].trans_lat <= sc->cpu_prev_sleep) {
906a2afe45aSNate Lawson 		cx_next_idx = i;
907a2afe45aSNate Lawson 		break;
908a2afe45aSNate Lawson 	    }
90980f006a1SNate Lawson     }
91056a70eadSNate Lawson 
9116b74f9b7SNate Lawson     /*
9126b74f9b7SNate Lawson      * Check for bus master activity.  If there was activity, clear
9136b74f9b7SNate Lawson      * the bit and use the lowest non-C3 state.  Note that the USB
9146b74f9b7SNate Lawson      * driver polling for new devices keeps this bit set all the
915f435261dSNate Lawson      * time if USB is loaded.
9166b74f9b7SNate Lawson      */
91718ececa0SNate Lawson     if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
9182be4e471SJung-uk Kim 	AcpiGetRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active);
9196b74f9b7SNate Lawson 	if (bm_active != 0) {
9202be4e471SJung-uk Kim 	    AcpiSetRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
921907b6777SNate Lawson 	    cx_next_idx = min(cx_next_idx, sc->cpu_non_c3);
9226b74f9b7SNate Lawson 	}
923f435261dSNate Lawson     }
9246b74f9b7SNate Lawson 
925a2afe45aSNate Lawson     /* Select the next state and update statistics. */
926a2afe45aSNate Lawson     cx_next = &sc->cpu_cx_states[cx_next_idx];
927907b6777SNate Lawson     sc->cpu_cx_stats[cx_next_idx]++;
928a2afe45aSNate Lawson     KASSERT(cx_next->type != ACPI_STATE_C0, ("acpi_cpu_idle: C0 sleep"));
9296b74f9b7SNate Lawson 
9306b74f9b7SNate Lawson     /*
931a2afe45aSNate Lawson      * Execute HLT (or equivalent) and wait for an interrupt.  We can't
932a2afe45aSNate Lawson      * calculate the time spent in C1 since the place we wake up is an
933a2afe45aSNate Lawson      * ISR.  Assume we slept one quantum and return.
9346b74f9b7SNate Lawson      */
935a2afe45aSNate Lawson     if (cx_next->type == ACPI_STATE_C1) {
936a2afe45aSNate Lawson 	sc->cpu_prev_sleep = 1000000 / hz;
937a2afe45aSNate Lawson 	acpi_cpu_c1();
938a2afe45aSNate Lawson 	return;
939a2afe45aSNate Lawson     }
940a2afe45aSNate Lawson 
941f435261dSNate Lawson     /*
942f435261dSNate Lawson      * For C3, disable bus master arbitration and enable bus master wake
943f435261dSNate Lawson      * if BM control is available, otherwise flush the CPU cache.
944f435261dSNate Lawson      */
945a2afe45aSNate Lawson     if (cx_next->type == ACPI_STATE_C3) {
946f435261dSNate Lawson 	if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
9472be4e471SJung-uk Kim 	    AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 1);
9482be4e471SJung-uk Kim 	    AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
949f435261dSNate Lawson 	} else
950f435261dSNate Lawson 	    ACPI_FLUSH_CPU_CACHE();
951a2afe45aSNate Lawson     }
952a2afe45aSNate Lawson 
9536b74f9b7SNate Lawson     /*
954a2afe45aSNate Lawson      * Read from P_LVLx to enter C2(+), checking time spent asleep.
9556b74f9b7SNate Lawson      * Use the ACPI timer for measuring sleep time.  Since we need to
9566b74f9b7SNate Lawson      * get the time very close to the CPU start/stop clock logic, this
9576b74f9b7SNate Lawson      * is the only reliable time source.
9586b74f9b7SNate Lawson      */
9592be4e471SJung-uk Kim     AcpiHwLowLevelRead(32, &start_time, &AcpiGbl_FADT.XPmTimerBlock);
9606b74f9b7SNate Lawson     CPU_GET_REG(cx_next->p_lvlx, 1);
9616b74f9b7SNate Lawson 
9626b74f9b7SNate Lawson     /*
9636b74f9b7SNate Lawson      * Read the end time twice.  Since it may take an arbitrary time
9646b74f9b7SNate Lawson      * to enter the idle state, the first read may be executed before
9656b74f9b7SNate Lawson      * the processor has stopped.  Doing it again provides enough
9666b74f9b7SNate Lawson      * margin that we are certain to have a correct value.
9676b74f9b7SNate Lawson      */
9682be4e471SJung-uk Kim     AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT.XPmTimerBlock);
9692be4e471SJung-uk Kim     AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT.XPmTimerBlock);
9706b74f9b7SNate Lawson 
9716b74f9b7SNate Lawson     /* Enable bus master arbitration and disable bus master wakeup. */
97218ececa0SNate Lawson     if (cx_next->type == ACPI_STATE_C3 &&
97318ececa0SNate Lawson 	(cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
9742be4e471SJung-uk Kim 	AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 0);
9752be4e471SJung-uk Kim 	AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
9766b74f9b7SNate Lawson     }
97780f006a1SNate Lawson     ACPI_ENABLE_IRQS();
9786b74f9b7SNate Lawson 
9796b74f9b7SNate Lawson     /* Find the actual time asleep in microseconds, minus overhead. */
980eea17c34SNate Lawson     end_time = acpi_TimerDelta(end_time, start_time);
981a2afe45aSNate Lawson     sc->cpu_prev_sleep = PM_USEC(end_time) - cx_next->trans_lat;
9826b74f9b7SNate Lawson }
9836b74f9b7SNate Lawson 
9846b74f9b7SNate Lawson /*
9858b888c66SNate Lawson  * Re-evaluate the _CST object when we are notified that it changed.
98656a70eadSNate Lawson  *
9876b74f9b7SNate Lawson  * XXX Re-evaluation disabled until locking is done.
9886b74f9b7SNate Lawson  */
9896b74f9b7SNate Lawson static void
9906b74f9b7SNate Lawson acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context)
9916b74f9b7SNate Lawson {
9926b74f9b7SNate Lawson     struct acpi_cpu_softc *sc = (struct acpi_cpu_softc *)context;
9936b74f9b7SNate Lawson 
9948b888c66SNate Lawson     if (notify != ACPI_NOTIFY_CX_STATES)
9958b888c66SNate Lawson 	return;
9968b888c66SNate Lawson 
9976b74f9b7SNate Lawson     device_printf(sc->cpu_dev, "Cx states changed\n");
9986b74f9b7SNate Lawson     /* acpi_cpu_cx_cst(sc); */
9996b74f9b7SNate Lawson }
10006b74f9b7SNate Lawson 
10016b74f9b7SNate Lawson static int
1002907b6777SNate Lawson acpi_cpu_quirks(void)
10036b74f9b7SNate Lawson {
1004ae56b59fSNate Lawson     device_t acpi_dev;
10056b74f9b7SNate Lawson 
10067826bf98SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10077826bf98SNate Lawson 
10086b74f9b7SNate Lawson     /*
1009907b6777SNate Lawson      * Bus mastering arbitration control is needed to keep caches coherent
1010907b6777SNate Lawson      * while sleeping in C3.  If it's not present but a working flush cache
1011907b6777SNate Lawson      * instruction is present, flush the caches before entering C3 instead.
1012907b6777SNate Lawson      * Otherwise, just disable C3 completely.
10136b74f9b7SNate Lawson      */
10142be4e471SJung-uk Kim     if (AcpiGbl_FADT.Pm2ControlBlock == 0 ||
10152be4e471SJung-uk Kim 	AcpiGbl_FADT.Pm2ControlLength == 0) {
10162be4e471SJung-uk Kim 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) &&
10172be4e471SJung-uk Kim 	    (AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0) {
1018907b6777SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
1019907b6777SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
102030dd6af3SNate Lawson 		"acpi_cpu: no BM control, using flush cache method\n"));
1021907b6777SNate Lawson 	} else {
1022907b6777SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_C3;
1023907b6777SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
102430dd6af3SNate Lawson 		"acpi_cpu: no BM control, C3 not available\n"));
1025907b6777SNate Lawson 	}
1026907b6777SNate Lawson     }
1027907b6777SNate Lawson 
1028907b6777SNate Lawson     /*
1029907b6777SNate Lawson      * If we are using generic Cx mode, C3 on multiple CPUs requires using
1030907b6777SNate Lawson      * the expensive flush cache instruction.
1031907b6777SNate Lawson      */
103230dd6af3SNate Lawson     if (cpu_cx_generic && mp_ncpus > 1) {
103318ececa0SNate Lawson 	cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
103430dd6af3SNate Lawson 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
103530dd6af3SNate Lawson 	    "acpi_cpu: SMP, using flush cache mode for C3\n"));
103630dd6af3SNate Lawson     }
10376b74f9b7SNate Lawson 
10386b74f9b7SNate Lawson     /* Look for various quirks of the PIIX4 part. */
10396b74f9b7SNate Lawson     acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
10406b74f9b7SNate Lawson     if (acpi_dev != NULL) {
10416b74f9b7SNate Lawson 	switch (pci_get_revid(acpi_dev)) {
10426b74f9b7SNate Lawson 	/*
10436b74f9b7SNate Lawson 	 * Disable C3 support for all PIIX4 chipsets.  Some of these parts
10446b74f9b7SNate Lawson 	 * do not report the BMIDE status to the BM status register and
10456b74f9b7SNate Lawson 	 * others have a livelock bug if Type-F DMA is enabled.  Linux
10466b74f9b7SNate Lawson 	 * works around the BMIDE bug by reading the BM status directly
10476b74f9b7SNate Lawson 	 * but we take the simpler approach of disabling C3 for these
10486b74f9b7SNate Lawson 	 * parts.
10496b74f9b7SNate Lawson 	 *
10506b74f9b7SNate Lawson 	 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
10516b74f9b7SNate Lawson 	 * Livelock") from the January 2002 PIIX4 specification update.
10526b74f9b7SNate Lawson 	 * Applies to all PIIX4 models.
10536b74f9b7SNate Lawson 	 */
10546b74f9b7SNate Lawson 	case PCI_REVISION_4E:
10556b74f9b7SNate Lawson 	case PCI_REVISION_4M:
10566b74f9b7SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_C3;
105730dd6af3SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
105830dd6af3SNate Lawson 		"acpi_cpu: working around PIIX4 bug, disabling C3\n"));
10596b74f9b7SNate Lawson 	    break;
10606b74f9b7SNate Lawson 	default:
10616b74f9b7SNate Lawson 	    break;
10626b74f9b7SNate Lawson 	}
10636b74f9b7SNate Lawson     }
10646b74f9b7SNate Lawson 
10656b74f9b7SNate Lawson     return (0);
10666b74f9b7SNate Lawson }
10676b74f9b7SNate Lawson 
10686b74f9b7SNate Lawson static int
1069a2afe45aSNate Lawson acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS)
10706b74f9b7SNate Lawson {
1071907b6777SNate Lawson     struct acpi_cpu_softc *sc;
10726b74f9b7SNate Lawson     struct sbuf	 sb;
10736b74f9b7SNate Lawson     char	 buf[128];
10746b74f9b7SNate Lawson     int		 i;
10754a03551dSNate Lawson     uintmax_t	 fract, sum, whole;
10766b74f9b7SNate Lawson 
1077907b6777SNate Lawson     sc = (struct acpi_cpu_softc *) arg1;
10783e7fa136SNate Lawson     sum = 0;
1079907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++)
1080907b6777SNate Lawson 	sum += sc->cpu_cx_stats[i];
10816b74f9b7SNate Lawson     sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1082907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++) {
10833e7fa136SNate Lawson 	if (sum > 0) {
1084907b6777SNate Lawson 	    whole = (uintmax_t)sc->cpu_cx_stats[i] * 100;
10853e7fa136SNate Lawson 	    fract = (whole % sum) * 100;
10863e7fa136SNate Lawson 	    sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
10873e7fa136SNate Lawson 		(u_int)(fract / sum));
10883e7fa136SNate Lawson 	} else
10893e7fa136SNate Lawson 	    sbuf_printf(&sb, "0%% ");
10903e7fa136SNate Lawson     }
10916b74f9b7SNate Lawson     sbuf_trim(&sb);
10926b74f9b7SNate Lawson     sbuf_finish(&sb);
1093ccc09458SNate Lawson     sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1094ccc09458SNate Lawson     sbuf_delete(&sb);
10956b74f9b7SNate Lawson 
10966b74f9b7SNate Lawson     return (0);
10976b74f9b7SNate Lawson }
10986b74f9b7SNate Lawson 
10996b74f9b7SNate Lawson static int
1100b13cf774SNate Lawson acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val)
11016b74f9b7SNate Lawson {
1102b13cf774SNate Lawson     int i;
11036b74f9b7SNate Lawson 
1104b13cf774SNate Lawson     ACPI_SERIAL_ASSERT(cpu);
1105907b6777SNate Lawson     sc->cpu_cx_lowest = val;
1106907b6777SNate Lawson 
1107907b6777SNate Lawson     /* If not disabling, cache the new lowest non-C3 state. */
1108907b6777SNate Lawson     sc->cpu_non_c3 = 0;
1109907b6777SNate Lawson     for (i = sc->cpu_cx_lowest; i >= 0; i--) {
1110907b6777SNate Lawson 	if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
1111907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
1112907b6777SNate Lawson 	    break;
1113907b6777SNate Lawson 	}
1114907b6777SNate Lawson     }
1115907b6777SNate Lawson 
1116907b6777SNate Lawson     /* Reset the statistics counters. */
1117907b6777SNate Lawson     bzero(sc->cpu_cx_stats, sizeof(sc->cpu_cx_stats));
1118b13cf774SNate Lawson     return (0);
1119b13cf774SNate Lawson }
1120b13cf774SNate Lawson 
1121b13cf774SNate Lawson static int
1122b13cf774SNate Lawson acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1123b13cf774SNate Lawson {
1124b13cf774SNate Lawson     struct	 acpi_cpu_softc *sc;
1125b13cf774SNate Lawson     char	 state[8];
1126b13cf774SNate Lawson     int		 val, error;
1127b13cf774SNate Lawson 
1128b13cf774SNate Lawson     sc = (struct acpi_cpu_softc *) arg1;
1129b13cf774SNate Lawson     snprintf(state, sizeof(state), "C%d", sc->cpu_cx_lowest + 1);
1130b13cf774SNate Lawson     error = sysctl_handle_string(oidp, state, sizeof(state), req);
1131b13cf774SNate Lawson     if (error != 0 || req->newptr == NULL)
1132b13cf774SNate Lawson 	return (error);
1133b13cf774SNate Lawson     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1134b13cf774SNate Lawson 	return (EINVAL);
1135b13cf774SNate Lawson     val = (int) strtol(state + 1, NULL, 10) - 1;
1136b13cf774SNate Lawson     if (val < 0 || val > sc->cpu_cx_count - 1)
1137b13cf774SNate Lawson 	return (EINVAL);
1138b13cf774SNate Lawson 
1139b13cf774SNate Lawson     ACPI_SERIAL_BEGIN(cpu);
1140b13cf774SNate Lawson     acpi_cpu_set_cx_lowest(sc, val);
1141907b6777SNate Lawson     ACPI_SERIAL_END(cpu);
1142907b6777SNate Lawson 
1143907b6777SNate Lawson     return (0);
1144907b6777SNate Lawson }
1145907b6777SNate Lawson 
1146907b6777SNate Lawson static int
1147907b6777SNate Lawson acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1148907b6777SNate Lawson {
1149907b6777SNate Lawson     struct	acpi_cpu_softc *sc;
1150907b6777SNate Lawson     char	state[8];
1151b13cf774SNate Lawson     int		val, error, i;
1152907b6777SNate Lawson 
1153ccc09458SNate Lawson     snprintf(state, sizeof(state), "C%d", cpu_cx_lowest + 1);
1154ccc09458SNate Lawson     error = sysctl_handle_string(oidp, state, sizeof(state), req);
11556b74f9b7SNate Lawson     if (error != 0 || req->newptr == NULL)
11566b74f9b7SNate Lawson 	return (error);
1157ccc09458SNate Lawson     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1158ccc09458SNate Lawson 	return (EINVAL);
1159ccc09458SNate Lawson     val = (int) strtol(state + 1, NULL, 10) - 1;
116056a70eadSNate Lawson     if (val < 0 || val > cpu_cx_count - 1)
11616b74f9b7SNate Lawson 	return (EINVAL);
11626b74f9b7SNate Lawson     cpu_cx_lowest = val;
11636b74f9b7SNate Lawson 
1164b13cf774SNate Lawson     /* Update the new lowest useable Cx state for all CPUs. */
1165907b6777SNate Lawson     ACPI_SERIAL_BEGIN(cpu);
1166907b6777SNate Lawson     for (i = 0; i < cpu_ndevices; i++) {
1167907b6777SNate Lawson 	sc = device_get_softc(cpu_devices[i]);
1168b13cf774SNate Lawson 	acpi_cpu_set_cx_lowest(sc, val);
1169907b6777SNate Lawson     }
1170d92a2ebdSNate Lawson     ACPI_SERIAL_END(cpu);
11716b74f9b7SNate Lawson 
11726b74f9b7SNate Lawson     return (0);
11736b74f9b7SNate Lawson }
1174