xref: /freebsd/sys/dev/acpica/acpi_cpu.c (revision 412ef220846a26e2cba2c477f894b14f41ac2d7b)
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>
47e1c9d39eSJung-uk Kim #if defined(__amd64__) || defined(__i386__)
48e1c9d39eSJung-uk Kim #include <machine/clock.h>
49e1c9d39eSJung-uk Kim #endif
5056a70eadSNate Lawson #include <sys/rman.h>
51fec754d4SMike Smith 
52129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
53129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
54129d3046SJung-uk Kim 
55fec754d4SMike Smith #include <dev/acpica/acpivar.h>
56fec754d4SMike Smith 
57fec754d4SMike Smith /*
588c5468e3SNate Lawson  * Support for ACPI Processor devices, including C[1-3] sleep states.
59fec754d4SMike Smith  */
60fec754d4SMike Smith 
61be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
62fec754d4SMike Smith #define _COMPONENT	ACPI_PROCESSOR
639127281cSMike Smith ACPI_MODULE_NAME("PROCESSOR")
64fec754d4SMike Smith 
656b74f9b7SNate Lawson struct acpi_cx {
666b74f9b7SNate Lawson     struct resource	*p_lvlx;	/* Register to read to enter state. */
676b74f9b7SNate Lawson     uint32_t		 type;		/* C1-3 (C4 and up treated as C3). */
686b74f9b7SNate Lawson     uint32_t		 trans_lat;	/* Transition latency (usec). */
696b74f9b7SNate Lawson     uint32_t		 power;		/* Power consumed (mW). */
70f4eb0418SNate Lawson     int			 res_type;	/* Resource type for p_lvlx. */
716b74f9b7SNate Lawson };
726b74f9b7SNate Lawson #define MAX_CX_STATES	 8
736b74f9b7SNate Lawson 
74fec754d4SMike Smith struct acpi_cpu_softc {
75fec754d4SMike Smith     device_t		 cpu_dev;
76fec754d4SMike Smith     ACPI_HANDLE		 cpu_handle;
7798aa9cd0SNate Lawson     struct pcpu		*cpu_pcpu;
7898aa9cd0SNate Lawson     uint32_t		 cpu_acpi_id;	/* ACPI processor id */
7956a70eadSNate Lawson     uint32_t		 cpu_p_blk;	/* ACPI P_BLK location */
8056a70eadSNate Lawson     uint32_t		 cpu_p_blk_len;	/* P_BLK length (must be 6). */
816b74f9b7SNate Lawson     struct acpi_cx	 cpu_cx_states[MAX_CX_STATES];
8256a70eadSNate Lawson     int			 cpu_cx_count;	/* Number of valid Cx states. */
83a2afe45aSNate Lawson     int			 cpu_prev_sleep;/* Last idle sleep duration. */
84b29224c2SNate Lawson     int			 cpu_features;	/* Child driver supported features. */
85907b6777SNate Lawson     /* Runtime state. */
86907b6777SNate Lawson     int			 cpu_non_c3;	/* Index of lowest non-C3 state. */
87907b6777SNate Lawson     u_int		 cpu_cx_stats[MAX_CX_STATES];/* Cx usage history. */
88907b6777SNate Lawson     /* Values for sysctl. */
89907b6777SNate Lawson     struct sysctl_ctx_list cpu_sysctl_ctx;
90907b6777SNate Lawson     struct sysctl_oid	*cpu_sysctl_tree;
9155fb7f36SSean Bruno     int			 cpu_cx_lowest;
92907b6777SNate Lawson     char 		 cpu_cx_supported[64];
93907b6777SNate Lawson     int			 cpu_rid;
94fec754d4SMike Smith };
95fec754d4SMike Smith 
9698aa9cd0SNate Lawson struct acpi_cpu_device {
9798aa9cd0SNate Lawson     struct resource_list	ad_rl;
9898aa9cd0SNate Lawson };
9998aa9cd0SNate Lawson 
1006b74f9b7SNate Lawson #define CPU_GET_REG(reg, width) 					\
1016b74f9b7SNate Lawson     (bus_space_read_ ## width(rman_get_bustag((reg)), 			\
1026b74f9b7SNate Lawson 		      rman_get_bushandle((reg)), 0))
1036b74f9b7SNate Lawson #define CPU_SET_REG(reg, width, val)					\
1046b74f9b7SNate Lawson     (bus_space_write_ ## width(rman_get_bustag((reg)), 			\
1056b74f9b7SNate Lawson 		       rman_get_bushandle((reg)), 0, (val)))
106be2b1797SNate Lawson 
1076b74f9b7SNate Lawson #define PM_USEC(x)	 ((x) >> 2)	/* ~4 clocks per usec (3.57955 Mhz) */
108fec754d4SMike Smith 
1098b888c66SNate Lawson #define ACPI_NOTIFY_CX_STATES	0x81	/* _CST changed. */
110fec754d4SMike Smith 
11118ececa0SNate Lawson #define CPU_QUIRK_NO_C3		(1<<0)	/* C3-type states are not usable. */
11218ececa0SNate Lawson #define CPU_QUIRK_NO_BM_CTRL	(1<<2)	/* No bus mastering control. */
1136b74f9b7SNate Lawson 
1146b74f9b7SNate Lawson #define PCI_VENDOR_INTEL	0x8086
1156b74f9b7SNate Lawson #define PCI_DEVICE_82371AB_3	0x7113	/* PIIX4 chipset for quirks. */
1166b74f9b7SNate Lawson #define PCI_REVISION_A_STEP	0
1176b74f9b7SNate Lawson #define PCI_REVISION_B_STEP	1
1186b74f9b7SNate Lawson #define PCI_REVISION_4E		2
1196b74f9b7SNate Lawson #define PCI_REVISION_4M		3
1208a000acaSRui Paulo #define PIIX4_DEVACTB_REG	0x58
1218a000acaSRui Paulo #define PIIX4_BRLD_EN_IRQ0	(1<<0)
1228a000acaSRui Paulo #define PIIX4_BRLD_EN_IRQ	(1<<1)
1238a000acaSRui Paulo #define PIIX4_BRLD_EN_IRQ8	(1<<5)
1248a000acaSRui Paulo #define PIIX4_STOP_BREAK_MASK	(PIIX4_BRLD_EN_IRQ0 | PIIX4_BRLD_EN_IRQ | PIIX4_BRLD_EN_IRQ8)
1258a000acaSRui Paulo #define PIIX4_PCNTRL_BST_EN	(1<<10)
1266b74f9b7SNate Lawson 
127e4cd9dcfSJohn Baldwin /* Allow users to ignore processor orders in MADT. */
128e4cd9dcfSJohn Baldwin static int cpu_unordered;
129e4cd9dcfSJohn Baldwin TUNABLE_INT("debug.acpi.cpu_unordered", &cpu_unordered);
130e4cd9dcfSJohn Baldwin SYSCTL_INT(_debug_acpi, OID_AUTO, cpu_unordered, CTLFLAG_RDTUN,
131e4cd9dcfSJohn Baldwin     &cpu_unordered, 0,
132e4cd9dcfSJohn Baldwin     "Do not use the MADT to match ACPI Processor objects to CPUs.");
133e4cd9dcfSJohn Baldwin 
1346b74f9b7SNate Lawson /* Platform hardware resource information. */
1356b74f9b7SNate Lawson static uint32_t		 cpu_smi_cmd;	/* Value to write to SMI_CMD. */
13656a70eadSNate Lawson static uint8_t		 cpu_cst_cnt;	/* Indicate we are _CST aware. */
137a2afe45aSNate Lawson static int		 cpu_quirks;	/* Indicate any hardware bugs. */
1386b74f9b7SNate Lawson 
1396b74f9b7SNate Lawson /* Runtime state. */
140907b6777SNate Lawson static int		 cpu_disable_idle; /* Disable entry to idle function */
141907b6777SNate Lawson static int		 cpu_cx_count;	/* Number of valid Cx states */
1426b74f9b7SNate Lawson 
1436b74f9b7SNate Lawson /* Values for sysctl. */
144907b6777SNate Lawson static struct sysctl_ctx_list cpu_sysctl_ctx;
145907b6777SNate Lawson static struct sysctl_oid *cpu_sysctl_tree;
146907b6777SNate Lawson static int		 cpu_cx_generic;
14755fb7f36SSean Bruno static int		 cpu_cx_lowest;
148fec754d4SMike Smith 
149fec754d4SMike Smith static device_t		*cpu_devices;
150fec754d4SMike Smith static int		 cpu_ndevices;
151447a5fa1SJohn Baldwin static struct acpi_cpu_softc **cpu_softc;
152d92a2ebdSNate Lawson ACPI_SERIAL_DECL(cpu, "ACPI CPU");
153fec754d4SMike Smith 
154fec754d4SMike Smith static int	acpi_cpu_probe(device_t dev);
155fec754d4SMike Smith static int	acpi_cpu_attach(device_t dev);
1563331373cSNate Lawson static int	acpi_cpu_suspend(device_t dev);
1573331373cSNate Lawson static int	acpi_cpu_resume(device_t dev);
158e4cd9dcfSJohn Baldwin static int	acpi_pcpu_get_id(device_t dev, uint32_t *acpi_id,
159b6426963SNate Lawson 		    uint32_t *cpu_id);
16098aa9cd0SNate Lawson static struct resource_list *acpi_cpu_get_rlist(device_t dev, device_t child);
1613d844eddSAndriy Gapon static device_t	acpi_cpu_add_child(device_t dev, u_int order, const char *name,
16298aa9cd0SNate Lawson 		    int unit);
16398aa9cd0SNate Lawson static int	acpi_cpu_read_ivar(device_t dev, device_t child, int index,
16498aa9cd0SNate Lawson 		    uintptr_t *result);
16556a70eadSNate Lawson static int	acpi_cpu_shutdown(device_t dev);
166907b6777SNate Lawson static void	acpi_cpu_cx_probe(struct acpi_cpu_softc *sc);
167907b6777SNate Lawson static void	acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc);
1686b74f9b7SNate Lawson static int	acpi_cpu_cx_cst(struct acpi_cpu_softc *sc);
1696b74f9b7SNate Lawson static void	acpi_cpu_startup(void *arg);
170907b6777SNate Lawson static void	acpi_cpu_startup_cx(struct acpi_cpu_softc *sc);
17189ab2a7aSRui Paulo static void	acpi_cpu_cx_list(struct acpi_cpu_softc *sc);
1726b74f9b7SNate Lawson static void	acpi_cpu_idle(void);
1736b74f9b7SNate Lawson static void	acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context);
174907b6777SNate Lawson static int	acpi_cpu_quirks(void);
175a2afe45aSNate Lawson static int	acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS);
1763331373cSNate Lawson static int	acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val);
1776b74f9b7SNate Lawson static int	acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
178907b6777SNate Lawson static int	acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
179fec754d4SMike Smith 
180fec754d4SMike Smith static device_method_t acpi_cpu_methods[] = {
181fec754d4SMike Smith     /* Device interface */
182fec754d4SMike Smith     DEVMETHOD(device_probe,	acpi_cpu_probe),
183fec754d4SMike Smith     DEVMETHOD(device_attach,	acpi_cpu_attach),
18498aa9cd0SNate Lawson     DEVMETHOD(device_detach,	bus_generic_detach),
18556a70eadSNate Lawson     DEVMETHOD(device_shutdown,	acpi_cpu_shutdown),
1863331373cSNate Lawson     DEVMETHOD(device_suspend,	acpi_cpu_suspend),
1873331373cSNate Lawson     DEVMETHOD(device_resume,	acpi_cpu_resume),
18898aa9cd0SNate Lawson 
18998aa9cd0SNate Lawson     /* Bus interface */
19098aa9cd0SNate Lawson     DEVMETHOD(bus_add_child,	acpi_cpu_add_child),
19198aa9cd0SNate Lawson     DEVMETHOD(bus_read_ivar,	acpi_cpu_read_ivar),
19298aa9cd0SNate Lawson     DEVMETHOD(bus_get_resource_list, acpi_cpu_get_rlist),
19398aa9cd0SNate Lawson     DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
19498aa9cd0SNate Lawson     DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
19598aa9cd0SNate Lawson     DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
19698aa9cd0SNate Lawson     DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
19798aa9cd0SNate Lawson     DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
19898aa9cd0SNate Lawson     DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
19998aa9cd0SNate Lawson     DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
20098aa9cd0SNate Lawson     DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
201fec754d4SMike Smith 
2024b7ec270SMarius Strobl     DEVMETHOD_END
203fec754d4SMike Smith };
204fec754d4SMike Smith 
205fec754d4SMike Smith static driver_t acpi_cpu_driver = {
206b0e2b625SNate Lawson     "cpu",
207fec754d4SMike Smith     acpi_cpu_methods,
208fec754d4SMike Smith     sizeof(struct acpi_cpu_softc),
209fec754d4SMike Smith };
210fec754d4SMike Smith 
2113045c8afSNate Lawson static devclass_t acpi_cpu_devclass;
2123045c8afSNate Lawson DRIVER_MODULE(cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0);
213b0e2b625SNate Lawson MODULE_DEPEND(cpu, acpi, 1, 1, 1);
21456a70eadSNate Lawson 
215fec754d4SMike Smith static int
216fec754d4SMike Smith acpi_cpu_probe(device_t dev)
217fec754d4SMike Smith {
218b29224c2SNate Lawson     int			   acpi_id, cpu_id;
219b0e2b625SNate Lawson     ACPI_BUFFER		   buf;
220b0e2b625SNate Lawson     ACPI_HANDLE		   handle;
221b0e2b625SNate Lawson     ACPI_OBJECT		   *obj;
222b0e2b625SNate Lawson     ACPI_STATUS		   status;
223b0e2b625SNate Lawson 
224b0e2b625SNate Lawson     if (acpi_disabled("cpu") || acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
225b0e2b625SNate Lawson 	return (ENXIO);
226b0e2b625SNate Lawson 
227b0e2b625SNate Lawson     handle = acpi_get_handle(dev);
228447a5fa1SJohn Baldwin     if (cpu_softc == NULL)
229447a5fa1SJohn Baldwin 	cpu_softc = malloc(sizeof(struct acpi_cpu_softc *) *
230447a5fa1SJohn Baldwin 	    (mp_maxid + 1), M_TEMP /* XXX */, M_WAITOK | M_ZERO);
231b0e2b625SNate Lawson 
232b0e2b625SNate Lawson     /* Get our Processor object. */
233b0e2b625SNate Lawson     buf.Pointer = NULL;
234b0e2b625SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
235b0e2b625SNate Lawson     status = AcpiEvaluateObject(handle, NULL, NULL, &buf);
236b0e2b625SNate Lawson     if (ACPI_FAILURE(status)) {
237b0e2b625SNate Lawson 	device_printf(dev, "probe failed to get Processor obj - %s\n",
238b0e2b625SNate Lawson 		      AcpiFormatException(status));
239b0e2b625SNate Lawson 	return (ENXIO);
240b0e2b625SNate Lawson     }
241b0e2b625SNate Lawson     obj = (ACPI_OBJECT *)buf.Pointer;
242b0e2b625SNate Lawson     if (obj->Type != ACPI_TYPE_PROCESSOR) {
243b0e2b625SNate Lawson 	device_printf(dev, "Processor object has bad type %d\n", obj->Type);
244b0e2b625SNate Lawson 	AcpiOsFree(obj);
245b0e2b625SNate Lawson 	return (ENXIO);
246fec754d4SMike Smith     }
247be2b1797SNate Lawson 
248b0e2b625SNate Lawson     /*
249b0e2b625SNate Lawson      * Find the processor associated with our unit.  We could use the
250b0e2b625SNate Lawson      * ProcId as a key, however, some boxes do not have the same values
251b0e2b625SNate Lawson      * in their Processor object as the ProcId values in the MADT.
252b0e2b625SNate Lawson      */
253b0e2b625SNate Lawson     acpi_id = obj->Processor.ProcId;
254b0e2b625SNate Lawson     AcpiOsFree(obj);
255e4cd9dcfSJohn Baldwin     if (acpi_pcpu_get_id(dev, &acpi_id, &cpu_id) != 0)
256fec754d4SMike Smith 	return (ENXIO);
257b0e2b625SNate Lawson 
258b0e2b625SNate Lawson     /*
259b0e2b625SNate Lawson      * Check if we already probed this processor.  We scan the bus twice
260b0e2b625SNate Lawson      * so it's possible we've already seen this one.
261b0e2b625SNate Lawson      */
262b0e2b625SNate Lawson     if (cpu_softc[cpu_id] != NULL)
263b0e2b625SNate Lawson 	return (ENXIO);
264b0e2b625SNate Lawson 
265b0e2b625SNate Lawson     /* Mark this processor as in-use and save our derived id for attach. */
266b0e2b625SNate Lawson     cpu_softc[cpu_id] = (void *)1;
267f6eb382cSAndriy Gapon     acpi_set_private(dev, (void*)(intptr_t)cpu_id);
268b29224c2SNate Lawson     device_set_desc(dev, "ACPI CPU");
269b0e2b625SNate Lawson 
270b0e2b625SNate Lawson     return (0);
271fec754d4SMike Smith }
272fec754d4SMike Smith 
273fec754d4SMike Smith static int
274fec754d4SMike Smith acpi_cpu_attach(device_t dev)
275fec754d4SMike Smith {
276b0e2b625SNate Lawson     ACPI_BUFFER		   buf;
277c961facaSNate Lawson     ACPI_OBJECT		   arg[4], *obj;
278bce92885SNate Lawson     ACPI_OBJECT_LIST	   arglist;
27998aa9cd0SNate Lawson     struct pcpu		   *pcpu_data;
280fec754d4SMike Smith     struct acpi_cpu_softc *sc;
281fec754d4SMike Smith     struct acpi_softc	  *acpi_sc;
282fec754d4SMike Smith     ACPI_STATUS		   status;
283b29224c2SNate Lawson     u_int		   features;
284b29224c2SNate Lawson     int			   cpu_id, drv_count, i;
285b29224c2SNate Lawson     driver_t 		  **drivers;
286bce92885SNate Lawson     uint32_t		   cap_set[3];
287fec754d4SMike Smith 
288c961facaSNate Lawson     /* UUID needed by _OSC evaluation */
289c961facaSNate Lawson     static uint8_t cpu_oscuuid[16] = { 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29,
290c961facaSNate Lawson 				       0xBE, 0x47, 0x9E, 0xBD, 0xD8, 0x70,
291c961facaSNate Lawson 				       0x58, 0x71, 0x39, 0x53 };
292c961facaSNate Lawson 
293b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
294fec754d4SMike Smith 
295fec754d4SMike Smith     sc = device_get_softc(dev);
296fec754d4SMike Smith     sc->cpu_dev = dev;
297fec754d4SMike Smith     sc->cpu_handle = acpi_get_handle(dev);
298f6eb382cSAndriy Gapon     cpu_id = (int)(intptr_t)acpi_get_private(dev);
29998aa9cd0SNate Lawson     cpu_softc[cpu_id] = sc;
30098aa9cd0SNate Lawson     pcpu_data = pcpu_find(cpu_id);
30198aa9cd0SNate Lawson     pcpu_data->pc_device = dev;
30298aa9cd0SNate Lawson     sc->cpu_pcpu = pcpu_data;
3032be4e471SJung-uk Kim     cpu_smi_cmd = AcpiGbl_FADT.SmiCommand;
3042be4e471SJung-uk Kim     cpu_cst_cnt = AcpiGbl_FADT.CstControl;
305fec754d4SMike Smith 
306b0e2b625SNate Lawson     buf.Pointer = NULL;
307b0e2b625SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
3086b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf);
3096b74f9b7SNate Lawson     if (ACPI_FAILURE(status)) {
310b0e2b625SNate Lawson 	device_printf(dev, "attach failed to get Processor obj - %s\n",
3116b74f9b7SNate Lawson 		      AcpiFormatException(status));
3126b74f9b7SNate Lawson 	return (ENXIO);
313b0e2b625SNate Lawson     }
314b0e2b625SNate Lawson     obj = (ACPI_OBJECT *)buf.Pointer;
315b0e2b625SNate Lawson     sc->cpu_p_blk = obj->Processor.PblkAddress;
316b0e2b625SNate Lawson     sc->cpu_p_blk_len = obj->Processor.PblkLength;
31798aa9cd0SNate Lawson     sc->cpu_acpi_id = obj->Processor.ProcId;
318b0e2b625SNate Lawson     AcpiOsFree(obj);
31956a70eadSNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_BLK at %#x/%d\n",
32056a70eadSNate Lawson 		     device_get_unit(dev), sc->cpu_p_blk, sc->cpu_p_blk_len));
3216b74f9b7SNate Lawson 
322907b6777SNate Lawson     /*
323907b6777SNate Lawson      * If this is the first cpu we attach, create and initialize the generic
324907b6777SNate Lawson      * resources that will be used by all acpi cpu devices.
325907b6777SNate Lawson      */
326907b6777SNate Lawson     if (device_get_unit(dev) == 0) {
327907b6777SNate Lawson 	/* Assume we won't be using generic Cx mode by default */
328907b6777SNate Lawson 	cpu_cx_generic = FALSE;
329907b6777SNate Lawson 
330907b6777SNate Lawson 	/* Install hw.acpi.cpu sysctl tree */
331fec754d4SMike Smith 	acpi_sc = acpi_device_get_parent_softc(dev);
332907b6777SNate Lawson 	sysctl_ctx_init(&cpu_sysctl_ctx);
333907b6777SNate Lawson 	cpu_sysctl_tree = SYSCTL_ADD_NODE(&cpu_sysctl_ctx,
33498aa9cd0SNate Lawson 	    SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, "cpu",
335907b6777SNate Lawson 	    CTLFLAG_RD, 0, "node for CPU children");
336907b6777SNate Lawson 
337907b6777SNate Lawson 	/* Queue post cpu-probing task handler */
3382be4e471SJung-uk Kim 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cpu_startup, NULL);
339907b6777SNate Lawson     }
340fec754d4SMike Smith 
3416b74f9b7SNate Lawson     /*
342b29224c2SNate Lawson      * Before calling any CPU methods, collect child driver feature hints
343f2d94257SNate Lawson      * and notify ACPI of them.  We support unified SMP power control
344f2d94257SNate Lawson      * so advertise this ourselves.  Note this is not the same as independent
345f2d94257SNate Lawson      * SMP control where each CPU can have different settings.
346b29224c2SNate Lawson      */
347f2d94257SNate Lawson     sc->cpu_features = ACPI_CAP_SMP_SAME | ACPI_CAP_SMP_SAME_C3;
348b29224c2SNate Lawson     if (devclass_get_drivers(acpi_cpu_devclass, &drivers, &drv_count) == 0) {
349b29224c2SNate Lawson 	for (i = 0; i < drv_count; i++) {
350b29224c2SNate Lawson 	    if (ACPI_GET_FEATURES(drivers[i], &features) == 0)
351b29224c2SNate Lawson 		sc->cpu_features |= features;
352b29224c2SNate Lawson 	}
353b29224c2SNate Lawson 	free(drivers, M_TEMP);
354b29224c2SNate Lawson     }
355bce92885SNate Lawson 
356bce92885SNate Lawson     /*
357f4ab0cccSAndriy Gapon      * CPU capabilities are specified in
358f4ab0cccSAndriy Gapon      * Intel Processor Vendor-Specific ACPI Interface Specification.
359bce92885SNate Lawson      */
360bce92885SNate Lawson     if (sc->cpu_features) {
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;
373877a6d99SAndriy Gapon 	cap_set[0] = 0;				/* status */
374877a6d99SAndriy Gapon 	cap_set[1] = sc->cpu_features;
375f4ab0cccSAndriy Gapon 	status = AcpiEvaluateObject(sc->cpu_handle, "_OSC", &arglist, NULL);
376f4ab0cccSAndriy Gapon 	if (ACPI_SUCCESS(status)) {
377f4ab0cccSAndriy Gapon 	    if (cap_set[0] != 0)
378f4ab0cccSAndriy Gapon 		device_printf(dev, "_OSC returned status %#x\n", cap_set[0]);
379f4ab0cccSAndriy Gapon 	}
380f4ab0cccSAndriy Gapon 	else {
381f4ab0cccSAndriy Gapon 	    arglist.Pointer = arg;
382f4ab0cccSAndriy Gapon 	    arglist.Count = 1;
383f4ab0cccSAndriy Gapon 	    arg[0].Type = ACPI_TYPE_BUFFER;
384f4ab0cccSAndriy Gapon 	    arg[0].Buffer.Length = sizeof(cap_set);
385f4ab0cccSAndriy Gapon 	    arg[0].Buffer.Pointer = (uint8_t *)cap_set;
386f4ab0cccSAndriy Gapon 	    cap_set[0] = 1; /* revision */
387f4ab0cccSAndriy Gapon 	    cap_set[1] = 1; /* number of capabilities integers */
388f4ab0cccSAndriy Gapon 	    cap_set[2] = sc->cpu_features;
389f4ab0cccSAndriy Gapon 	    AcpiEvaluateObject(sc->cpu_handle, "_PDC", &arglist, NULL);
390f4ab0cccSAndriy Gapon 	}
391bce92885SNate Lawson     }
392b29224c2SNate Lawson 
393907b6777SNate Lawson     /* Probe for Cx state support. */
394907b6777SNate Lawson     acpi_cpu_cx_probe(sc);
3956b74f9b7SNate Lawson 
39698aa9cd0SNate Lawson     return (0);
3976b74f9b7SNate Lawson }
3986b74f9b7SNate Lawson 
399aa835160SAndriy Gapon static void
400aa835160SAndriy Gapon acpi_cpu_postattach(void *unused __unused)
401aa835160SAndriy Gapon {
402aa835160SAndriy Gapon     device_t *devices;
403aa835160SAndriy Gapon     int err;
404aa835160SAndriy Gapon     int i, n;
405aa835160SAndriy Gapon 
406aa835160SAndriy Gapon     err = devclass_get_devices(acpi_cpu_devclass, &devices, &n);
407aa835160SAndriy Gapon     if (err != 0) {
408aa835160SAndriy Gapon 	printf("devclass_get_devices(acpi_cpu_devclass) failed\n");
409aa835160SAndriy Gapon 	return;
410aa835160SAndriy Gapon     }
411aa835160SAndriy Gapon     for (i = 0; i < n; i++)
412aa835160SAndriy Gapon 	bus_generic_probe(devices[i]);
413aa835160SAndriy Gapon     for (i = 0; i < n; i++)
414aa835160SAndriy Gapon 	bus_generic_attach(devices[i]);
415aa835160SAndriy Gapon     free(devices, M_TEMP);
416aa835160SAndriy Gapon }
417aa835160SAndriy Gapon 
418aa835160SAndriy Gapon SYSINIT(acpi_cpu, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
419aa835160SAndriy Gapon     acpi_cpu_postattach, NULL);
420aa835160SAndriy Gapon 
421b6426963SNate Lawson /*
4223331373cSNate Lawson  * Disable any entry to the idle function during suspend and re-enable it
4233331373cSNate Lawson  * during resume.
4243331373cSNate Lawson  */
4253331373cSNate Lawson static int
4263331373cSNate Lawson acpi_cpu_suspend(device_t dev)
4273331373cSNate Lawson {
4283331373cSNate Lawson     int error;
4293331373cSNate Lawson 
4303331373cSNate Lawson     error = bus_generic_suspend(dev);
4313331373cSNate Lawson     if (error)
4323331373cSNate Lawson 	return (error);
4333331373cSNate Lawson     cpu_disable_idle = TRUE;
4343331373cSNate Lawson     return (0);
4353331373cSNate Lawson }
4363331373cSNate Lawson 
4373331373cSNate Lawson static int
4383331373cSNate Lawson acpi_cpu_resume(device_t dev)
4393331373cSNate Lawson {
4403331373cSNate Lawson 
4413331373cSNate Lawson     cpu_disable_idle = FALSE;
4423331373cSNate Lawson     return (bus_generic_resume(dev));
4433331373cSNate Lawson }
4443331373cSNate Lawson 
4453331373cSNate Lawson /*
446e4cd9dcfSJohn Baldwin  * Find the processor associated with a given ACPI ID.  By default,
447e4cd9dcfSJohn Baldwin  * use the MADT to map ACPI IDs to APIC IDs and use that to locate a
448e4cd9dcfSJohn Baldwin  * processor.  Some systems have inconsistent ASL and MADT however.
449e4cd9dcfSJohn Baldwin  * For these systems the cpu_unordered tunable can be set in which
450e4cd9dcfSJohn Baldwin  * case we assume that Processor objects are listed in the same order
451e4cd9dcfSJohn Baldwin  * in both the MADT and ASL.
452b6426963SNate Lawson  */
453b6426963SNate Lawson static int
454e4cd9dcfSJohn Baldwin acpi_pcpu_get_id(device_t dev, uint32_t *acpi_id, uint32_t *cpu_id)
455b6426963SNate Lawson {
456e4cd9dcfSJohn Baldwin     struct pcpu	*pc;
457e4cd9dcfSJohn Baldwin     uint32_t	 i, idx;
458b6426963SNate Lawson 
459b6426963SNate Lawson     KASSERT(acpi_id != NULL, ("Null acpi_id"));
460b6426963SNate Lawson     KASSERT(cpu_id != NULL, ("Null cpu_id"));
461e4cd9dcfSJohn Baldwin     idx = device_get_unit(dev);
462e4cd9dcfSJohn Baldwin 
463e4cd9dcfSJohn Baldwin     /*
464e4cd9dcfSJohn Baldwin      * If pc_acpi_id for CPU 0 is not initialized (e.g. a non-APIC
465e4cd9dcfSJohn Baldwin      * UP box) use the ACPI ID from the first processor we find.
466e4cd9dcfSJohn Baldwin      */
467e4cd9dcfSJohn Baldwin     if (idx == 0 && mp_ncpus == 1) {
468e4cd9dcfSJohn Baldwin 	pc = pcpu_find(0);
469e4cd9dcfSJohn Baldwin 	if (pc->pc_acpi_id == 0xffffffff)
470e4cd9dcfSJohn Baldwin 	    pc->pc_acpi_id = *acpi_id;
471e4cd9dcfSJohn Baldwin 	*cpu_id = 0;
472e4cd9dcfSJohn Baldwin 	return (0);
473e4cd9dcfSJohn Baldwin     }
474e4cd9dcfSJohn Baldwin 
4753aa6d94eSJohn Baldwin     CPU_FOREACH(i) {
476e4cd9dcfSJohn Baldwin 	pc = pcpu_find(i);
477e4cd9dcfSJohn Baldwin 	KASSERT(pc != NULL, ("no pcpu data for %d", i));
478e4cd9dcfSJohn Baldwin 	if (cpu_unordered) {
479b6426963SNate Lawson 	    if (idx-- == 0) {
480b6426963SNate Lawson 		/*
481e4cd9dcfSJohn Baldwin 		 * If pc_acpi_id doesn't match the ACPI ID from the
482e4cd9dcfSJohn Baldwin 		 * ASL, prefer the MADT-derived value.
483b6426963SNate Lawson 		 */
484e4cd9dcfSJohn Baldwin 		if (pc->pc_acpi_id != *acpi_id)
485e4cd9dcfSJohn Baldwin 		    *acpi_id = pc->pc_acpi_id;
486e4cd9dcfSJohn Baldwin 		*cpu_id = pc->pc_cpuid;
487e4cd9dcfSJohn Baldwin 		return (0);
488e4cd9dcfSJohn Baldwin 	    }
489e4cd9dcfSJohn Baldwin 	} else {
490e4cd9dcfSJohn Baldwin 	    if (pc->pc_acpi_id == *acpi_id) {
491e4cd9dcfSJohn Baldwin 		if (bootverbose)
4922aa7a9e6SJung-uk Kim 		    device_printf(dev,
4932aa7a9e6SJung-uk Kim 			"Processor %s (ACPI ID %u) -> APIC ID %d\n",
4942aa7a9e6SJung-uk Kim 			acpi_name(acpi_get_handle(dev)), *acpi_id,
4952aa7a9e6SJung-uk Kim 			pc->pc_cpuid);
496e4cd9dcfSJohn Baldwin 		*cpu_id = pc->pc_cpuid;
497b6426963SNate Lawson 		return (0);
498b6426963SNate Lawson 	    }
499b6426963SNate Lawson 	}
500e4cd9dcfSJohn Baldwin     }
501e4cd9dcfSJohn Baldwin 
502e4cd9dcfSJohn Baldwin     if (bootverbose)
503e4cd9dcfSJohn Baldwin 	printf("ACPI: Processor %s (ACPI ID %u) ignored\n",
504e4cd9dcfSJohn Baldwin 	    acpi_name(acpi_get_handle(dev)), *acpi_id);
505b6426963SNate Lawson 
506b6426963SNate Lawson     return (ESRCH);
507b6426963SNate Lawson }
508b6426963SNate Lawson 
50998aa9cd0SNate Lawson static struct resource_list *
51098aa9cd0SNate Lawson acpi_cpu_get_rlist(device_t dev, device_t child)
51198aa9cd0SNate Lawson {
51298aa9cd0SNate Lawson     struct acpi_cpu_device *ad;
51398aa9cd0SNate Lawson 
51498aa9cd0SNate Lawson     ad = device_get_ivars(child);
51598aa9cd0SNate Lawson     if (ad == NULL)
51698aa9cd0SNate Lawson 	return (NULL);
51798aa9cd0SNate Lawson     return (&ad->ad_rl);
51898aa9cd0SNate Lawson }
51998aa9cd0SNate Lawson 
52098aa9cd0SNate Lawson static device_t
5213d844eddSAndriy Gapon acpi_cpu_add_child(device_t dev, u_int order, const char *name, int unit)
52298aa9cd0SNate Lawson {
52398aa9cd0SNate Lawson     struct acpi_cpu_device *ad;
52498aa9cd0SNate Lawson     device_t child;
52598aa9cd0SNate Lawson 
52698aa9cd0SNate Lawson     if ((ad = malloc(sizeof(*ad), M_TEMP, M_NOWAIT | M_ZERO)) == NULL)
52798aa9cd0SNate Lawson 	return (NULL);
52898aa9cd0SNate Lawson 
52998aa9cd0SNate Lawson     resource_list_init(&ad->ad_rl);
53098aa9cd0SNate Lawson 
53198aa9cd0SNate Lawson     child = device_add_child_ordered(dev, order, name, unit);
53298aa9cd0SNate Lawson     if (child != NULL)
53398aa9cd0SNate Lawson 	device_set_ivars(child, ad);
53443ce1c77SNate Lawson     else
53543ce1c77SNate Lawson 	free(ad, M_TEMP);
53698aa9cd0SNate Lawson     return (child);
53798aa9cd0SNate Lawson }
53898aa9cd0SNate Lawson 
53998aa9cd0SNate Lawson static int
54098aa9cd0SNate Lawson acpi_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
54198aa9cd0SNate Lawson {
54298aa9cd0SNate Lawson     struct acpi_cpu_softc *sc;
54398aa9cd0SNate Lawson 
54498aa9cd0SNate Lawson     sc = device_get_softc(dev);
54598aa9cd0SNate Lawson     switch (index) {
54698aa9cd0SNate Lawson     case ACPI_IVAR_HANDLE:
54798aa9cd0SNate Lawson 	*result = (uintptr_t)sc->cpu_handle;
54898aa9cd0SNate Lawson 	break;
54998aa9cd0SNate Lawson     case CPU_IVAR_PCPU:
55098aa9cd0SNate Lawson 	*result = (uintptr_t)sc->cpu_pcpu;
55198aa9cd0SNate Lawson 	break;
552e1c9d39eSJung-uk Kim #if defined(__amd64__) || defined(__i386__)
553e1c9d39eSJung-uk Kim     case CPU_IVAR_NOMINAL_MHZ:
554e1c9d39eSJung-uk Kim 	if (tsc_is_invariant) {
5553453537fSJung-uk Kim 	    *result = (uintptr_t)(atomic_load_acq_64(&tsc_freq) / 1000000);
556e1c9d39eSJung-uk Kim 	    break;
557e1c9d39eSJung-uk Kim 	}
558e1c9d39eSJung-uk Kim 	/* FALLTHROUGH */
559e1c9d39eSJung-uk Kim #endif
56098aa9cd0SNate Lawson     default:
56198aa9cd0SNate Lawson 	return (ENOENT);
56298aa9cd0SNate Lawson     }
56398aa9cd0SNate Lawson     return (0);
56498aa9cd0SNate Lawson }
56598aa9cd0SNate Lawson 
5666b74f9b7SNate Lawson static int
56756a70eadSNate Lawson acpi_cpu_shutdown(device_t dev)
56856a70eadSNate Lawson {
56956a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
57056a70eadSNate Lawson 
57198aa9cd0SNate Lawson     /* Allow children to shutdown first. */
57298aa9cd0SNate Lawson     bus_generic_shutdown(dev);
57398aa9cd0SNate Lawson 
574cc3c11f9SNate Lawson     /*
575cc3c11f9SNate Lawson      * Disable any entry to the idle function.  There is a small race where
576cc3c11f9SNate Lawson      * an idle thread have passed this check but not gone to sleep.  This
577cc3c11f9SNate Lawson      * is ok since device_shutdown() does not free the softc, otherwise
578cc3c11f9SNate Lawson      * we'd have to be sure all threads were evicted before returning.
579cc3c11f9SNate Lawson      */
580907b6777SNate Lawson     cpu_disable_idle = TRUE;
58156a70eadSNate Lawson 
58256a70eadSNate Lawson     return_VALUE (0);
58356a70eadSNate Lawson }
58456a70eadSNate Lawson 
585907b6777SNate Lawson static void
5866b74f9b7SNate Lawson acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
5876b74f9b7SNate Lawson {
588907b6777SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
589907b6777SNate Lawson 
590907b6777SNate Lawson     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
591907b6777SNate Lawson     sc->cpu_prev_sleep = 1000000;
592907b6777SNate Lawson     sc->cpu_cx_lowest = 0;
593907b6777SNate Lawson 
594907b6777SNate Lawson     /*
595907b6777SNate Lawson      * Check for the ACPI 2.0 _CST sleep states object. If we can't find
596907b6777SNate Lawson      * any, we'll revert to generic FADT/P_BLK Cx control method which will
597907b6777SNate Lawson      * be handled by acpi_cpu_startup. We need to defer to after having
598907b6777SNate Lawson      * probed all the cpus in the system before probing for generic Cx
599907b6777SNate Lawson      * states as we may already have found cpus with valid _CST packages
600907b6777SNate Lawson      */
601907b6777SNate Lawson     if (!cpu_cx_generic && acpi_cpu_cx_cst(sc) != 0) {
602907b6777SNate Lawson 	/*
603907b6777SNate Lawson 	 * We were unable to find a _CST package for this cpu or there
604907b6777SNate Lawson 	 * was an error parsing it. Switch back to generic mode.
605907b6777SNate Lawson 	 */
606907b6777SNate Lawson 	cpu_cx_generic = TRUE;
607bd826803SNate Lawson 	if (bootverbose)
608bd826803SNate Lawson 	    device_printf(sc->cpu_dev, "switching to generic Cx mode\n");
609907b6777SNate Lawson     }
610907b6777SNate Lawson 
611907b6777SNate Lawson     /*
612907b6777SNate Lawson      * TODO: _CSD Package should be checked here.
613907b6777SNate Lawson      */
614907b6777SNate Lawson }
615907b6777SNate Lawson 
616907b6777SNate Lawson static void
617907b6777SNate Lawson acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc)
618907b6777SNate Lawson {
6196b74f9b7SNate Lawson     ACPI_GENERIC_ADDRESS	 gas;
6206b74f9b7SNate Lawson     struct acpi_cx		*cx_ptr;
62156a70eadSNate Lawson 
62256a70eadSNate Lawson     sc->cpu_cx_count = 0;
6236b74f9b7SNate Lawson     cx_ptr = sc->cpu_cx_states;
624fec754d4SMike Smith 
625907b6777SNate Lawson     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
626907b6777SNate Lawson     sc->cpu_prev_sleep = 1000000;
627907b6777SNate Lawson 
6286b74f9b7SNate Lawson     /* C1 has been required since just after ACPI 1.0 */
6296b74f9b7SNate Lawson     cx_ptr->type = ACPI_STATE_C1;
6306b74f9b7SNate Lawson     cx_ptr->trans_lat = 0;
6316b74f9b7SNate Lawson     cx_ptr++;
63256a70eadSNate Lawson     sc->cpu_cx_count++;
63356a70eadSNate Lawson 
634c181b89bSNate Lawson     /*
635c181b89bSNate Lawson      * The spec says P_BLK must be 6 bytes long.  However, some systems
636c181b89bSNate Lawson      * use it to indicate a fractional set of features present so we
637c181b89bSNate Lawson      * take 5 as C2.  Some may also have a value of 7 to indicate
638c181b89bSNate Lawson      * another C3 but most use _CST for this (as required) and having
639c181b89bSNate Lawson      * "only" C1-C3 is not a hardship.
640c181b89bSNate Lawson      */
641c181b89bSNate Lawson     if (sc->cpu_p_blk_len < 5)
642907b6777SNate Lawson 	return;
643fec754d4SMike Smith 
6446b74f9b7SNate Lawson     /* Validate and allocate resources for C2 (P_LVL2). */
6452be4e471SJung-uk Kim     gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
6462be4e471SJung-uk Kim     gas.BitWidth = 8;
6472be4e471SJung-uk Kim     if (AcpiGbl_FADT.C2Latency <= 100) {
64856a70eadSNate Lawson 	gas.Address = sc->cpu_p_blk + 4;
649907b6777SNate Lawson 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid,
650907b6777SNate Lawson 	    &gas, &cx_ptr->p_lvlx, RF_SHAREABLE);
6516b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
652907b6777SNate Lawson 	    sc->cpu_rid++;
6536b74f9b7SNate Lawson 	    cx_ptr->type = ACPI_STATE_C2;
6542be4e471SJung-uk Kim 	    cx_ptr->trans_lat = AcpiGbl_FADT.C2Latency;
6556b74f9b7SNate Lawson 	    cx_ptr++;
65656a70eadSNate Lawson 	    sc->cpu_cx_count++;
657fec754d4SMike Smith 	}
658fec754d4SMike Smith     }
659c181b89bSNate Lawson     if (sc->cpu_p_blk_len < 6)
660907b6777SNate Lawson 	return;
661be2b1797SNate Lawson 
6626b74f9b7SNate Lawson     /* Validate and allocate resources for C3 (P_LVL3). */
6636e1de64dSRui Paulo     if (AcpiGbl_FADT.C3Latency <= 1000 && !(cpu_quirks & CPU_QUIRK_NO_C3)) {
66456a70eadSNate Lawson 	gas.Address = sc->cpu_p_blk + 5;
665907b6777SNate Lawson 	acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &sc->cpu_rid, &gas,
666907b6777SNate Lawson 	    &cx_ptr->p_lvlx, RF_SHAREABLE);
6676b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
668907b6777SNate Lawson 	    sc->cpu_rid++;
6696b74f9b7SNate Lawson 	    cx_ptr->type = ACPI_STATE_C3;
6702be4e471SJung-uk Kim 	    cx_ptr->trans_lat = AcpiGbl_FADT.C3Latency;
6716b74f9b7SNate Lawson 	    cx_ptr++;
67256a70eadSNate Lawson 	    sc->cpu_cx_count++;
673*412ef220SAndriy Gapon 	    cpu_can_deep_sleep = 1;
6746b74f9b7SNate Lawson 	}
6756b74f9b7SNate Lawson     }
6766b74f9b7SNate Lawson }
6776b74f9b7SNate Lawson 
6786b74f9b7SNate Lawson /*
6796b74f9b7SNate Lawson  * Parse a _CST package and set up its Cx states.  Since the _CST object
6806b74f9b7SNate Lawson  * can change dynamically, our notify handler may call this function
6816b74f9b7SNate Lawson  * to clean up and probe the new _CST package.
6826b74f9b7SNate Lawson  */
6836b74f9b7SNate Lawson static int
6846b74f9b7SNate Lawson acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
6856b74f9b7SNate Lawson {
6866b74f9b7SNate Lawson     struct	 acpi_cx *cx_ptr;
6876b74f9b7SNate Lawson     ACPI_STATUS	 status;
6886b74f9b7SNate Lawson     ACPI_BUFFER	 buf;
6896b74f9b7SNate Lawson     ACPI_OBJECT	*top;
6906b74f9b7SNate Lawson     ACPI_OBJECT	*pkg;
6916b74f9b7SNate Lawson     uint32_t	 count;
6926b74f9b7SNate Lawson     int		 i;
6936b74f9b7SNate Lawson 
69456a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
69556a70eadSNate Lawson 
6966b74f9b7SNate Lawson     buf.Pointer = NULL;
6976b74f9b7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
6986b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
699bd826803SNate Lawson     if (ACPI_FAILURE(status))
7006b74f9b7SNate Lawson 	return (ENXIO);
7016b74f9b7SNate Lawson 
7026b74f9b7SNate Lawson     /* _CST is a package with a count and at least one Cx package. */
7036b74f9b7SNate Lawson     top = (ACPI_OBJECT *)buf.Pointer;
70421cea91fSNate Lawson     if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
705bd826803SNate Lawson 	device_printf(sc->cpu_dev, "invalid _CST package\n");
7066b74f9b7SNate Lawson 	AcpiOsFree(buf.Pointer);
7076b74f9b7SNate Lawson 	return (ENXIO);
7086b74f9b7SNate Lawson     }
7096b74f9b7SNate Lawson     if (count != top->Package.Count - 1) {
710bd826803SNate Lawson 	device_printf(sc->cpu_dev, "invalid _CST state count (%d != %d)\n",
7116b74f9b7SNate Lawson 	       count, top->Package.Count - 1);
7126b74f9b7SNate Lawson 	count = top->Package.Count - 1;
7136b74f9b7SNate Lawson     }
7146b74f9b7SNate Lawson     if (count > MAX_CX_STATES) {
71556a70eadSNate Lawson 	device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
7166b74f9b7SNate Lawson 	count = MAX_CX_STATES;
7176b74f9b7SNate Lawson     }
7186b74f9b7SNate Lawson 
71968d5e11cSJung-uk Kim     sc->cpu_non_c3 = 0;
72056a70eadSNate Lawson     sc->cpu_cx_count = 0;
7216b74f9b7SNate Lawson     cx_ptr = sc->cpu_cx_states;
72268d5e11cSJung-uk Kim 
72368d5e11cSJung-uk Kim     /*
72468d5e11cSJung-uk Kim      * C1 has been required since just after ACPI 1.0.
72568d5e11cSJung-uk Kim      * Reserve the first slot for it.
72668d5e11cSJung-uk Kim      */
72768d5e11cSJung-uk Kim     cx_ptr->type = ACPI_STATE_C0;
72868d5e11cSJung-uk Kim     cx_ptr++;
72968d5e11cSJung-uk Kim     sc->cpu_cx_count++;
73068d5e11cSJung-uk Kim 
73168d5e11cSJung-uk Kim     /* Set up all valid states. */
7326b74f9b7SNate Lawson     for (i = 0; i < count; i++) {
7336b74f9b7SNate Lawson 	pkg = &top->Package.Elements[i + 1];
73421cea91fSNate Lawson 	if (!ACPI_PKG_VALID(pkg, 4) ||
73521cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
73621cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
73721cea91fSNate Lawson 	    acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
73821cea91fSNate Lawson 
73980f006a1SNate Lawson 	    device_printf(sc->cpu_dev, "skipping invalid Cx state package\n");
7406b74f9b7SNate Lawson 	    continue;
7416b74f9b7SNate Lawson 	}
7426b74f9b7SNate Lawson 
7436b74f9b7SNate Lawson 	/* Validate the state to see if we should use it. */
7446b74f9b7SNate Lawson 	switch (cx_ptr->type) {
7456b74f9b7SNate Lawson 	case ACPI_STATE_C1:
74668d5e11cSJung-uk Kim 	    if (sc->cpu_cx_states[0].type == ACPI_STATE_C0) {
74768d5e11cSJung-uk Kim 		/* This is the first C1 state.  Use the reserved slot. */
74868d5e11cSJung-uk Kim 		sc->cpu_cx_states[0] = *cx_ptr;
74968d5e11cSJung-uk Kim 	    } else {
75056101001SAndriy Gapon 		sc->cpu_non_c3 = sc->cpu_cx_count;
7516b74f9b7SNate Lawson 		cx_ptr++;
75256a70eadSNate Lawson 		sc->cpu_cx_count++;
75368d5e11cSJung-uk Kim 	    }
7546b74f9b7SNate Lawson 	    continue;
7556b74f9b7SNate Lawson 	case ACPI_STATE_C2:
75656101001SAndriy Gapon 	    sc->cpu_non_c3 = sc->cpu_cx_count;
7576b74f9b7SNate Lawson 	    break;
7586b74f9b7SNate Lawson 	case ACPI_STATE_C3:
7596b74f9b7SNate Lawson 	default:
76009c22c66SAndriy Gapon 	    if ((cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
76156a70eadSNate Lawson 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
76256a70eadSNate Lawson 				 "acpi_cpu%d: C3[%d] not available.\n",
76356a70eadSNate Lawson 				 device_get_unit(sc->cpu_dev), i));
7646b74f9b7SNate Lawson 		continue;
765*412ef220SAndriy Gapon 	    } else
766*412ef220SAndriy Gapon 		cpu_can_deep_sleep = 1;
7676b74f9b7SNate Lawson 	    break;
7686b74f9b7SNate Lawson 	}
7696b74f9b7SNate Lawson 
7706b74f9b7SNate Lawson #ifdef notyet
7716b74f9b7SNate Lawson 	/* Free up any previous register. */
7726b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
7736b74f9b7SNate Lawson 	    bus_release_resource(sc->cpu_dev, 0, 0, cx_ptr->p_lvlx);
7746b74f9b7SNate Lawson 	    cx_ptr->p_lvlx = NULL;
7756b74f9b7SNate Lawson 	}
7766b74f9b7SNate Lawson #endif
7776b74f9b7SNate Lawson 
7786b74f9b7SNate Lawson 	/* Allocate the control register for C2 or C3. */
779907b6777SNate Lawson 	acpi_PkgGas(sc->cpu_dev, pkg, 0, &cx_ptr->res_type, &sc->cpu_rid,
780907b6777SNate Lawson 	    &cx_ptr->p_lvlx, RF_SHAREABLE);
781f4eb0418SNate Lawson 	if (cx_ptr->p_lvlx) {
782907b6777SNate Lawson 	    sc->cpu_rid++;
78356a70eadSNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
78456a70eadSNate Lawson 			     "acpi_cpu%d: Got C%d - %d latency\n",
78556a70eadSNate Lawson 			     device_get_unit(sc->cpu_dev), cx_ptr->type,
78656a70eadSNate Lawson 			     cx_ptr->trans_lat));
7876b74f9b7SNate Lawson 	    cx_ptr++;
78856a70eadSNate Lawson 	    sc->cpu_cx_count++;
7896b74f9b7SNate Lawson 	}
7906b74f9b7SNate Lawson     }
7916b74f9b7SNate Lawson     AcpiOsFree(buf.Pointer);
7926b74f9b7SNate Lawson 
79368d5e11cSJung-uk Kim     /* If C1 state was not found, we need one now. */
79468d5e11cSJung-uk Kim     cx_ptr = sc->cpu_cx_states;
79568d5e11cSJung-uk Kim     if (cx_ptr->type == ACPI_STATE_C0) {
79668d5e11cSJung-uk Kim 	cx_ptr->type = ACPI_STATE_C1;
79768d5e11cSJung-uk Kim 	cx_ptr->trans_lat = 0;
79868d5e11cSJung-uk Kim     }
79968d5e11cSJung-uk Kim 
8006b74f9b7SNate Lawson     return (0);
801fec754d4SMike Smith }
802fec754d4SMike Smith 
803fec754d4SMike Smith /*
804fec754d4SMike Smith  * Call this *after* all CPUs have been attached.
805fec754d4SMike Smith  */
806fec754d4SMike Smith static void
8076b74f9b7SNate Lawson acpi_cpu_startup(void *arg)
808fec754d4SMike Smith {
80956a70eadSNate Lawson     struct acpi_cpu_softc *sc;
810907b6777SNate Lawson     int i;
811fec754d4SMike Smith 
812be2b1797SNate Lawson     /* Get set of CPU devices */
8133045c8afSNate Lawson     devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
814fec754d4SMike Smith 
81556a70eadSNate Lawson     /*
816907b6777SNate Lawson      * Setup any quirks that might necessary now that we have probed
817907b6777SNate Lawson      * all the CPUs
81856a70eadSNate Lawson      */
819907b6777SNate Lawson     acpi_cpu_quirks();
820907b6777SNate Lawson 
821907b6777SNate Lawson     cpu_cx_count = 0;
822907b6777SNate Lawson     if (cpu_cx_generic) {
823907b6777SNate Lawson 	/*
824907b6777SNate Lawson 	 * We are using generic Cx mode, probe for available Cx states
825907b6777SNate Lawson 	 * for all processors.
826907b6777SNate Lawson 	 */
82756a70eadSNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
82856a70eadSNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
829907b6777SNate Lawson 	    acpi_cpu_generic_cx_probe(sc);
830ea667177SJohn Baldwin 	    if (sc->cpu_cx_count > cpu_cx_count)
831ea667177SJohn Baldwin 		    cpu_cx_count = sc->cpu_cx_count;
83256a70eadSNate Lawson 	}
833907b6777SNate Lawson 
834907b6777SNate Lawson 	/*
835907b6777SNate Lawson 	 * Find the highest Cx state common to all CPUs
836907b6777SNate Lawson 	 * in the system, taking quirks into account.
837907b6777SNate Lawson 	 */
838907b6777SNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
839907b6777SNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
840907b6777SNate Lawson 	    if (sc->cpu_cx_count < cpu_cx_count)
841907b6777SNate Lawson 		cpu_cx_count = sc->cpu_cx_count;
842907b6777SNate Lawson 	}
843907b6777SNate Lawson     } else {
844907b6777SNate Lawson 	/*
845907b6777SNate Lawson 	 * We are using _CST mode, remove C3 state if necessary.
846907b6777SNate Lawson 	 * Update the largest Cx state supported in the global cpu_cx_count.
847907b6777SNate Lawson 	 * It will be used in the global Cx sysctl handler.
848907b6777SNate Lawson 	 * As we now know for sure that we will be using _CST mode
849907b6777SNate Lawson 	 * install our notify handler.
850907b6777SNate Lawson 	 */
851907b6777SNate Lawson 	for (i = 0; i < cpu_ndevices; i++) {
852907b6777SNate Lawson 	    sc = device_get_softc(cpu_devices[i]);
8537a310721SJohn Baldwin 	    if (cpu_quirks & CPU_QUIRK_NO_C3) {
854907b6777SNate Lawson 		sc->cpu_cx_count = sc->cpu_non_c3 + 1;
855907b6777SNate Lawson 	    }
856907b6777SNate Lawson 	    if (sc->cpu_cx_count > cpu_cx_count)
857907b6777SNate Lawson 		cpu_cx_count = sc->cpu_cx_count;
858907b6777SNate Lawson 	    AcpiInstallNotifyHandler(sc->cpu_handle, ACPI_DEVICE_NOTIFY,
859907b6777SNate Lawson 		acpi_cpu_notify, sc);
860907b6777SNate Lawson 	}
861907b6777SNate Lawson     }
86256a70eadSNate Lawson 
8638c5468e3SNate Lawson     /* Perform Cx final initialization. */
864907b6777SNate Lawson     for (i = 0; i < cpu_ndevices; i++) {
865907b6777SNate Lawson 	sc = device_get_softc(cpu_devices[i]);
866907b6777SNate Lawson 	acpi_cpu_startup_cx(sc);
867907b6777SNate Lawson     }
868907b6777SNate Lawson 
869907b6777SNate Lawson     /* Add a sysctl handler to handle global Cx lowest setting */
870907b6777SNate Lawson     SYSCTL_ADD_PROC(&cpu_sysctl_ctx, SYSCTL_CHILDREN(cpu_sysctl_tree),
871907b6777SNate Lawson 	OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
872907b6777SNate Lawson 	NULL, 0, acpi_cpu_global_cx_lowest_sysctl, "A",
873907b6777SNate Lawson 	"Global lowest Cx sleep state to use");
874907b6777SNate Lawson 
875907b6777SNate Lawson     /* Take over idling from cpu_idle_default(). */
87655fb7f36SSean Bruno     cpu_cx_lowest = 0;
877907b6777SNate Lawson     cpu_disable_idle = FALSE;
878907b6777SNate Lawson     cpu_idle_hook = acpi_cpu_idle;
8796b74f9b7SNate Lawson }
8806b74f9b7SNate Lawson 
88156a70eadSNate Lawson static void
88289ab2a7aSRui Paulo acpi_cpu_cx_list(struct acpi_cpu_softc *sc)
88356a70eadSNate Lawson {
88456a70eadSNate Lawson     struct sbuf sb;
88556a70eadSNate Lawson     int i;
88656a70eadSNate Lawson 
887ae56b59fSNate Lawson     /*
888907b6777SNate Lawson      * Set up the list of Cx states
889ae56b59fSNate Lawson      */
890907b6777SNate Lawson     sbuf_new(&sb, sc->cpu_cx_supported, sizeof(sc->cpu_cx_supported),
891907b6777SNate Lawson 	SBUF_FIXEDLEN);
892*412ef220SAndriy Gapon     for (i = 0; i < sc->cpu_cx_count; i++)
89355fb7f36SSean Bruno 	sbuf_printf(&sb, "C%d/%d ", i + 1, sc->cpu_cx_states[i].trans_lat);
89456a70eadSNate Lawson     sbuf_trim(&sb);
89556a70eadSNate Lawson     sbuf_finish(&sb);
89689ab2a7aSRui Paulo }
89789ab2a7aSRui Paulo 
89889ab2a7aSRui Paulo static void
89989ab2a7aSRui Paulo acpi_cpu_startup_cx(struct acpi_cpu_softc *sc)
90089ab2a7aSRui Paulo {
90189ab2a7aSRui Paulo     acpi_cpu_cx_list(sc);
902907b6777SNate Lawson 
903907b6777SNate Lawson     SYSCTL_ADD_STRING(&sc->cpu_sysctl_ctx,
904907b6777SNate Lawson 		      SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
905907b6777SNate Lawson 		      OID_AUTO, "cx_supported", CTLFLAG_RD,
906907b6777SNate Lawson 		      sc->cpu_cx_supported, 0,
907907b6777SNate Lawson 		      "Cx/microsecond values for supported Cx states");
908907b6777SNate Lawson     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
909907b6777SNate Lawson 		    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
910ccc09458SNate Lawson 		    OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
911907b6777SNate Lawson 		    (void *)sc, 0, acpi_cpu_cx_lowest_sysctl, "A",
91256a70eadSNate Lawson 		    "lowest Cx sleep state to use");
913907b6777SNate Lawson     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
914907b6777SNate Lawson 		    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
915a2afe45aSNate Lawson 		    OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
916907b6777SNate Lawson 		    (void *)sc, 0, acpi_cpu_usage_sysctl, "A",
917a2afe45aSNate Lawson 		    "percent usage for each Cx state");
91856a70eadSNate Lawson 
91956a70eadSNate Lawson #ifdef notyet
92056a70eadSNate Lawson     /* Signal platform that we can handle _CST notification. */
921907b6777SNate Lawson     if (!cpu_cx_generic && cpu_cst_cnt != 0) {
922d92a2ebdSNate Lawson 	ACPI_LOCK(acpi);
92356a70eadSNate Lawson 	AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
924d92a2ebdSNate Lawson 	ACPI_UNLOCK(acpi);
92556a70eadSNate Lawson     }
92656a70eadSNate Lawson #endif
92756a70eadSNate Lawson }
92856a70eadSNate Lawson 
929fec754d4SMike Smith /*
930a2afe45aSNate Lawson  * Idle the CPU in the lowest state possible.  This function is called with
931a2afe45aSNate Lawson  * interrupts disabled.  Note that once it re-enables interrupts, a task
932a2afe45aSNate Lawson  * switch can occur so do not access shared data (i.e. the softc) after
933a2afe45aSNate Lawson  * interrupts are re-enabled.
9346b74f9b7SNate Lawson  */
9356b74f9b7SNate Lawson static void
9366b74f9b7SNate Lawson acpi_cpu_idle()
9376b74f9b7SNate Lawson {
9386b74f9b7SNate Lawson     struct	acpi_cpu_softc *sc;
93956a70eadSNate Lawson     struct	acpi_cx *cx_next;
9406b74f9b7SNate Lawson     uint32_t	start_time, end_time;
941a2afe45aSNate Lawson     int		bm_active, cx_next_idx, i;
9426b74f9b7SNate Lawson 
9436b74f9b7SNate Lawson     /* If disabled, return immediately. */
944907b6777SNate Lawson     if (cpu_disable_idle) {
9456b74f9b7SNate Lawson 	ACPI_ENABLE_IRQS();
9466b74f9b7SNate Lawson 	return;
9476b74f9b7SNate Lawson     }
9486b74f9b7SNate Lawson 
949cd1f3db9SNate Lawson     /*
950cd1f3db9SNate Lawson      * Look up our CPU id to get our softc.  If it's NULL, we'll use C1
951cd1f3db9SNate Lawson      * since there is no ACPI processor object for this CPU.  This occurs
952cd1f3db9SNate Lawson      * for logical CPUs in the HTT case.
953cd1f3db9SNate Lawson      */
954cd1f3db9SNate Lawson     sc = cpu_softc[PCPU_GET(cpuid)];
955cd1f3db9SNate Lawson     if (sc == NULL) {
956cd1f3db9SNate Lawson 	acpi_cpu_c1();
957cd1f3db9SNate Lawson 	return;
958cd1f3db9SNate Lawson     }
959cd1f3db9SNate Lawson 
960bb1d6ad5SAlexander Motin     /* Find the lowest state that has small enough latency. */
961b0baaaaeSAlexander Motin     cx_next_idx = 0;
96255fb7f36SSean Bruno     if (cpu_disable_deep_sleep)
96348fe2e67SAlexander Motin 	i = min(sc->cpu_cx_lowest, sc->cpu_non_c3);
96455fb7f36SSean Bruno     else
965a157e425SAlexander Motin 	i = sc->cpu_cx_lowest;
966a157e425SAlexander Motin     for (; i >= 0; i--) {
967b0baaaaeSAlexander Motin 	if (sc->cpu_cx_states[i].trans_lat * 3 <= sc->cpu_prev_sleep) {
968a2afe45aSNate Lawson 	    cx_next_idx = i;
969a2afe45aSNate Lawson 	    break;
970a2afe45aSNate Lawson 	}
97180f006a1SNate Lawson     }
97256a70eadSNate Lawson 
9736b74f9b7SNate Lawson     /*
9746b74f9b7SNate Lawson      * Check for bus master activity.  If there was activity, clear
9756b74f9b7SNate Lawson      * the bit and use the lowest non-C3 state.  Note that the USB
9766b74f9b7SNate Lawson      * driver polling for new devices keeps this bit set all the
977f435261dSNate Lawson      * time if USB is loaded.
9786b74f9b7SNate Lawson      */
97918ececa0SNate Lawson     if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
980129d3046SJung-uk Kim 	AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active);
9816b74f9b7SNate Lawson 	if (bm_active != 0) {
982129d3046SJung-uk Kim 	    AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
983907b6777SNate Lawson 	    cx_next_idx = min(cx_next_idx, sc->cpu_non_c3);
9846b74f9b7SNate Lawson 	}
985f435261dSNate Lawson     }
9866b74f9b7SNate Lawson 
987a2afe45aSNate Lawson     /* Select the next state and update statistics. */
988a2afe45aSNate Lawson     cx_next = &sc->cpu_cx_states[cx_next_idx];
989907b6777SNate Lawson     sc->cpu_cx_stats[cx_next_idx]++;
990a2afe45aSNate Lawson     KASSERT(cx_next->type != ACPI_STATE_C0, ("acpi_cpu_idle: C0 sleep"));
9916b74f9b7SNate Lawson 
9926b74f9b7SNate Lawson     /*
993a2afe45aSNate Lawson      * Execute HLT (or equivalent) and wait for an interrupt.  We can't
9947150e671SAlexander Motin      * precisely calculate the time spent in C1 since the place we wake up
995a157e425SAlexander Motin      * is an ISR.  Assume we slept no more then half of quantum, unless
996a157e425SAlexander Motin      * we are called inside critical section, delaying context switch.
9976b74f9b7SNate Lawson      */
998a2afe45aSNate Lawson     if (cx_next->type == ACPI_STATE_C1) {
9997150e671SAlexander Motin 	AcpiHwRead(&start_time, &AcpiGbl_FADT.XPmTimerBlock);
1000a2afe45aSNate Lawson 	acpi_cpu_c1();
10017150e671SAlexander Motin 	AcpiHwRead(&end_time, &AcpiGbl_FADT.XPmTimerBlock);
1002a157e425SAlexander Motin         end_time = PM_USEC(acpi_TimerDelta(end_time, start_time));
1003a157e425SAlexander Motin         if (curthread->td_critnest == 0)
1004a157e425SAlexander Motin 		end_time = min(end_time, 500000 / hz);
1005a157e425SAlexander Motin 	sc->cpu_prev_sleep = (sc->cpu_prev_sleep * 3 + end_time) / 4;
1006a2afe45aSNate Lawson 	return;
1007a2afe45aSNate Lawson     }
1008a2afe45aSNate Lawson 
1009f435261dSNate Lawson     /*
1010f435261dSNate Lawson      * For C3, disable bus master arbitration and enable bus master wake
1011f435261dSNate Lawson      * if BM control is available, otherwise flush the CPU cache.
1012f435261dSNate Lawson      */
1013a2afe45aSNate Lawson     if (cx_next->type == ACPI_STATE_C3) {
1014f435261dSNate Lawson 	if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
1015129d3046SJung-uk Kim 	    AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
1016129d3046SJung-uk Kim 	    AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
1017f435261dSNate Lawson 	} else
1018f435261dSNate Lawson 	    ACPI_FLUSH_CPU_CACHE();
1019a2afe45aSNate Lawson     }
1020a2afe45aSNate Lawson 
10216b74f9b7SNate Lawson     /*
1022a2afe45aSNate Lawson      * Read from P_LVLx to enter C2(+), checking time spent asleep.
10236b74f9b7SNate Lawson      * Use the ACPI timer for measuring sleep time.  Since we need to
10246b74f9b7SNate Lawson      * get the time very close to the CPU start/stop clock logic, this
10256b74f9b7SNate Lawson      * is the only reliable time source.
10266b74f9b7SNate Lawson      */
102792488a57SJung-uk Kim     AcpiHwRead(&start_time, &AcpiGbl_FADT.XPmTimerBlock);
10286b74f9b7SNate Lawson     CPU_GET_REG(cx_next->p_lvlx, 1);
10296b74f9b7SNate Lawson 
10306b74f9b7SNate Lawson     /*
10316b74f9b7SNate Lawson      * Read the end time twice.  Since it may take an arbitrary time
10326b74f9b7SNate Lawson      * to enter the idle state, the first read may be executed before
10336b74f9b7SNate Lawson      * the processor has stopped.  Doing it again provides enough
10346b74f9b7SNate Lawson      * margin that we are certain to have a correct value.
10356b74f9b7SNate Lawson      */
103692488a57SJung-uk Kim     AcpiHwRead(&end_time, &AcpiGbl_FADT.XPmTimerBlock);
103792488a57SJung-uk Kim     AcpiHwRead(&end_time, &AcpiGbl_FADT.XPmTimerBlock);
10386b74f9b7SNate Lawson 
10396b74f9b7SNate Lawson     /* Enable bus master arbitration and disable bus master wakeup. */
104018ececa0SNate Lawson     if (cx_next->type == ACPI_STATE_C3 &&
104118ececa0SNate Lawson 	(cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
1042129d3046SJung-uk Kim 	AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
1043129d3046SJung-uk Kim 	AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
10446b74f9b7SNate Lawson     }
104580f006a1SNate Lawson     ACPI_ENABLE_IRQS();
10466b74f9b7SNate Lawson 
1047b0baaaaeSAlexander Motin     /* Find the actual time asleep in microseconds. */
1048eea17c34SNate Lawson     end_time = acpi_TimerDelta(end_time, start_time);
1049b0baaaaeSAlexander Motin     sc->cpu_prev_sleep = (sc->cpu_prev_sleep * 3 + PM_USEC(end_time)) / 4;
10506b74f9b7SNate Lawson }
10516b74f9b7SNate Lawson 
10526b74f9b7SNate Lawson /*
10538b888c66SNate Lawson  * Re-evaluate the _CST object when we are notified that it changed.
105456a70eadSNate Lawson  *
10556b74f9b7SNate Lawson  * XXX Re-evaluation disabled until locking is done.
10566b74f9b7SNate Lawson  */
10576b74f9b7SNate Lawson static void
10586b74f9b7SNate Lawson acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context)
10596b74f9b7SNate Lawson {
10606b74f9b7SNate Lawson     struct acpi_cpu_softc *sc = (struct acpi_cpu_softc *)context;
106189ab2a7aSRui Paulo     struct acpi_cpu_softc *isc;
106289ab2a7aSRui Paulo     int i;
10636b74f9b7SNate Lawson 
10648b888c66SNate Lawson     if (notify != ACPI_NOTIFY_CX_STATES)
10658b888c66SNate Lawson 	return;
10668b888c66SNate Lawson 
106789ab2a7aSRui Paulo     /* Update the list of Cx states. */
106889ab2a7aSRui Paulo     acpi_cpu_cx_cst(sc);
106989ab2a7aSRui Paulo     acpi_cpu_cx_list(sc);
107089ab2a7aSRui Paulo 
107189ab2a7aSRui Paulo     /* Update the new lowest useable Cx state for all CPUs. */
107289ab2a7aSRui Paulo     ACPI_SERIAL_BEGIN(cpu);
107389ab2a7aSRui Paulo     cpu_cx_count = 0;
107489ab2a7aSRui Paulo     for (i = 0; i < cpu_ndevices; i++) {
107589ab2a7aSRui Paulo 	isc = device_get_softc(cpu_devices[i]);
107689ab2a7aSRui Paulo 	if (isc->cpu_cx_count > cpu_cx_count)
107789ab2a7aSRui Paulo 	    cpu_cx_count = isc->cpu_cx_count;
107889ab2a7aSRui Paulo     }
107955fb7f36SSean Bruno     if (sc->cpu_cx_lowest < cpu_cx_lowest)
108055fb7f36SSean Bruno 	acpi_cpu_set_cx_lowest(sc, min(cpu_cx_lowest, sc->cpu_cx_count - 1));
108189ab2a7aSRui Paulo     ACPI_SERIAL_END(cpu);
10826b74f9b7SNate Lawson }
10836b74f9b7SNate Lawson 
10846b74f9b7SNate Lawson static int
1085907b6777SNate Lawson acpi_cpu_quirks(void)
10866b74f9b7SNate Lawson {
1087ae56b59fSNate Lawson     device_t acpi_dev;
10888a000acaSRui Paulo     uint32_t val;
10896b74f9b7SNate Lawson 
10907826bf98SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
10917826bf98SNate Lawson 
10926b74f9b7SNate Lawson     /*
1093907b6777SNate Lawson      * Bus mastering arbitration control is needed to keep caches coherent
1094907b6777SNate Lawson      * while sleeping in C3.  If it's not present but a working flush cache
1095907b6777SNate Lawson      * instruction is present, flush the caches before entering C3 instead.
1096907b6777SNate Lawson      * Otherwise, just disable C3 completely.
10976b74f9b7SNate Lawson      */
10982be4e471SJung-uk Kim     if (AcpiGbl_FADT.Pm2ControlBlock == 0 ||
10992be4e471SJung-uk Kim 	AcpiGbl_FADT.Pm2ControlLength == 0) {
11002be4e471SJung-uk Kim 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) &&
11012be4e471SJung-uk Kim 	    (AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0) {
1102907b6777SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
1103907b6777SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
110430dd6af3SNate Lawson 		"acpi_cpu: no BM control, using flush cache method\n"));
1105907b6777SNate Lawson 	} else {
1106907b6777SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_C3;
1107907b6777SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
110830dd6af3SNate Lawson 		"acpi_cpu: no BM control, C3 not available\n"));
1109907b6777SNate Lawson 	}
1110907b6777SNate Lawson     }
1111907b6777SNate Lawson 
1112907b6777SNate Lawson     /*
1113907b6777SNate Lawson      * If we are using generic Cx mode, C3 on multiple CPUs requires using
1114907b6777SNate Lawson      * the expensive flush cache instruction.
1115907b6777SNate Lawson      */
111630dd6af3SNate Lawson     if (cpu_cx_generic && mp_ncpus > 1) {
111718ececa0SNate Lawson 	cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
111830dd6af3SNate Lawson 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
111930dd6af3SNate Lawson 	    "acpi_cpu: SMP, using flush cache mode for C3\n"));
112030dd6af3SNate Lawson     }
11216b74f9b7SNate Lawson 
11226b74f9b7SNate Lawson     /* Look for various quirks of the PIIX4 part. */
11236b74f9b7SNate Lawson     acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
11246b74f9b7SNate Lawson     if (acpi_dev != NULL) {
11256b74f9b7SNate Lawson 	switch (pci_get_revid(acpi_dev)) {
11266b74f9b7SNate Lawson 	/*
11276b74f9b7SNate Lawson 	 * Disable C3 support for all PIIX4 chipsets.  Some of these parts
11286b74f9b7SNate Lawson 	 * do not report the BMIDE status to the BM status register and
11296b74f9b7SNate Lawson 	 * others have a livelock bug if Type-F DMA is enabled.  Linux
11306b74f9b7SNate Lawson 	 * works around the BMIDE bug by reading the BM status directly
11316b74f9b7SNate Lawson 	 * but we take the simpler approach of disabling C3 for these
11326b74f9b7SNate Lawson 	 * parts.
11336b74f9b7SNate Lawson 	 *
11346b74f9b7SNate Lawson 	 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
11356b74f9b7SNate Lawson 	 * Livelock") from the January 2002 PIIX4 specification update.
11366b74f9b7SNate Lawson 	 * Applies to all PIIX4 models.
11378a000acaSRui Paulo 	 *
11388a000acaSRui Paulo 	 * Also, make sure that all interrupts cause a "Stop Break"
11398a000acaSRui Paulo 	 * event to exit from C2 state.
1140f1e1ddc3SAndriy Gapon 	 * Also, BRLD_EN_BM (ACPI_BITREG_BUS_MASTER_RLD in ACPI-speak)
1141f1e1ddc3SAndriy Gapon 	 * should be set to zero, otherwise it causes C2 to short-sleep.
1142f1e1ddc3SAndriy Gapon 	 * PIIX4 doesn't properly support C3 and bus master activity
1143f1e1ddc3SAndriy Gapon 	 * need not break out of C2.
11446b74f9b7SNate Lawson 	 */
11458a000acaSRui Paulo 	case PCI_REVISION_A_STEP:
11468a000acaSRui Paulo 	case PCI_REVISION_B_STEP:
11476b74f9b7SNate Lawson 	case PCI_REVISION_4E:
11486b74f9b7SNate Lawson 	case PCI_REVISION_4M:
11496b74f9b7SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_C3;
115030dd6af3SNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
115130dd6af3SNate Lawson 		"acpi_cpu: working around PIIX4 bug, disabling C3\n"));
11528a000acaSRui Paulo 
11538a000acaSRui Paulo 	    val = pci_read_config(acpi_dev, PIIX4_DEVACTB_REG, 4);
11548a000acaSRui Paulo 	    if ((val & PIIX4_STOP_BREAK_MASK) != PIIX4_STOP_BREAK_MASK) {
11558a000acaSRui Paulo 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1156f1e1ddc3SAndriy Gapon 		    "acpi_cpu: PIIX4: enabling IRQs to generate Stop Break\n"));
11578a000acaSRui Paulo 	    	val |= PIIX4_STOP_BREAK_MASK;
11588a000acaSRui Paulo 		pci_write_config(acpi_dev, PIIX4_DEVACTB_REG, val, 4);
11598a000acaSRui Paulo 	    }
1160129d3046SJung-uk Kim 	    AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_RLD, &val);
1161f1e1ddc3SAndriy Gapon 	    if (val) {
1162f1e1ddc3SAndriy Gapon 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1163f1e1ddc3SAndriy Gapon 		    "acpi_cpu: PIIX4: reset BRLD_EN_BM\n"));
1164129d3046SJung-uk Kim 		AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
1165f1e1ddc3SAndriy Gapon 	    }
11666b74f9b7SNate Lawson 	    break;
11676b74f9b7SNate Lawson 	default:
11686b74f9b7SNate Lawson 	    break;
11696b74f9b7SNate Lawson 	}
11706b74f9b7SNate Lawson     }
11716b74f9b7SNate Lawson 
11726b74f9b7SNate Lawson     return (0);
11736b74f9b7SNate Lawson }
11746b74f9b7SNate Lawson 
11756b74f9b7SNate Lawson static int
1176a2afe45aSNate Lawson acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS)
11776b74f9b7SNate Lawson {
1178907b6777SNate Lawson     struct acpi_cpu_softc *sc;
11796b74f9b7SNate Lawson     struct sbuf	 sb;
11806b74f9b7SNate Lawson     char	 buf[128];
11816b74f9b7SNate Lawson     int		 i;
11824a03551dSNate Lawson     uintmax_t	 fract, sum, whole;
11836b74f9b7SNate Lawson 
1184907b6777SNate Lawson     sc = (struct acpi_cpu_softc *) arg1;
11853e7fa136SNate Lawson     sum = 0;
1186907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++)
1187907b6777SNate Lawson 	sum += sc->cpu_cx_stats[i];
11886b74f9b7SNate Lawson     sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1189907b6777SNate Lawson     for (i = 0; i < sc->cpu_cx_count; i++) {
11903e7fa136SNate Lawson 	if (sum > 0) {
1191907b6777SNate Lawson 	    whole = (uintmax_t)sc->cpu_cx_stats[i] * 100;
11923e7fa136SNate Lawson 	    fract = (whole % sum) * 100;
11933e7fa136SNate Lawson 	    sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
11943e7fa136SNate Lawson 		(u_int)(fract / sum));
11953e7fa136SNate Lawson 	} else
11962500b6d9SAlexander Motin 	    sbuf_printf(&sb, "0.00%% ");
11973e7fa136SNate Lawson     }
11982500b6d9SAlexander Motin     sbuf_printf(&sb, "last %dus", sc->cpu_prev_sleep);
11996b74f9b7SNate Lawson     sbuf_trim(&sb);
12006b74f9b7SNate Lawson     sbuf_finish(&sb);
1201ccc09458SNate Lawson     sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1202ccc09458SNate Lawson     sbuf_delete(&sb);
12036b74f9b7SNate Lawson 
12046b74f9b7SNate Lawson     return (0);
12056b74f9b7SNate Lawson }
12066b74f9b7SNate Lawson 
12076b74f9b7SNate Lawson static int
1208b13cf774SNate Lawson acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc, int val)
12096b74f9b7SNate Lawson {
1210b13cf774SNate Lawson     int i;
12116b74f9b7SNate Lawson 
1212b13cf774SNate Lawson     ACPI_SERIAL_ASSERT(cpu);
121355fb7f36SSean Bruno     sc->cpu_cx_lowest = val;
1214907b6777SNate Lawson 
1215907b6777SNate Lawson     /* If not disabling, cache the new lowest non-C3 state. */
1216907b6777SNate Lawson     sc->cpu_non_c3 = 0;
1217907b6777SNate Lawson     for (i = sc->cpu_cx_lowest; i >= 0; i--) {
1218907b6777SNate Lawson 	if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
1219907b6777SNate Lawson 	    sc->cpu_non_c3 = i;
1220907b6777SNate Lawson 	    break;
1221907b6777SNate Lawson 	}
1222907b6777SNate Lawson     }
1223907b6777SNate Lawson 
1224907b6777SNate Lawson     /* Reset the statistics counters. */
1225907b6777SNate Lawson     bzero(sc->cpu_cx_stats, sizeof(sc->cpu_cx_stats));
1226b13cf774SNate Lawson     return (0);
1227b13cf774SNate Lawson }
1228b13cf774SNate Lawson 
1229b13cf774SNate Lawson static int
1230b13cf774SNate Lawson acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1231b13cf774SNate Lawson {
1232b13cf774SNate Lawson     struct	 acpi_cpu_softc *sc;
1233b13cf774SNate Lawson     char	 state[8];
1234b13cf774SNate Lawson     int		 val, error;
1235b13cf774SNate Lawson 
1236b13cf774SNate Lawson     sc = (struct acpi_cpu_softc *) arg1;
123755fb7f36SSean Bruno     snprintf(state, sizeof(state), "C%d", sc->cpu_cx_lowest + 1);
1238b13cf774SNate Lawson     error = sysctl_handle_string(oidp, state, sizeof(state), req);
1239b13cf774SNate Lawson     if (error != 0 || req->newptr == NULL)
1240b13cf774SNate Lawson 	return (error);
1241b13cf774SNate Lawson     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1242b13cf774SNate Lawson 	return (EINVAL);
124355fb7f36SSean Bruno     val = (int) strtol(state + 1, NULL, 10) - 1;
124455fb7f36SSean Bruno     if (val < 0 || val > sc->cpu_cx_count - 1)
1245b13cf774SNate Lawson 	return (EINVAL);
1246b13cf774SNate Lawson 
1247b13cf774SNate Lawson     ACPI_SERIAL_BEGIN(cpu);
124855fb7f36SSean Bruno     acpi_cpu_set_cx_lowest(sc, val);
1249907b6777SNate Lawson     ACPI_SERIAL_END(cpu);
1250907b6777SNate Lawson 
125155fb7f36SSean Bruno     return (0);
1252907b6777SNate Lawson }
1253907b6777SNate Lawson 
1254907b6777SNate Lawson static int
1255907b6777SNate Lawson acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
1256907b6777SNate Lawson {
1257907b6777SNate Lawson     struct	acpi_cpu_softc *sc;
1258907b6777SNate Lawson     char	state[8];
1259b13cf774SNate Lawson     int		val, error, i;
1260907b6777SNate Lawson 
126155fb7f36SSean Bruno     snprintf(state, sizeof(state), "C%d", cpu_cx_lowest + 1);
1262ccc09458SNate Lawson     error = sysctl_handle_string(oidp, state, sizeof(state), req);
12636b74f9b7SNate Lawson     if (error != 0 || req->newptr == NULL)
12646b74f9b7SNate Lawson 	return (error);
1265ccc09458SNate Lawson     if (strlen(state) < 2 || toupper(state[0]) != 'C')
1266ccc09458SNate Lawson 	return (EINVAL);
126755fb7f36SSean Bruno     val = (int) strtol(state + 1, NULL, 10) - 1;
126855fb7f36SSean Bruno     if (val < 0 || val > cpu_cx_count - 1)
12696b74f9b7SNate Lawson 	return (EINVAL);
127055fb7f36SSean Bruno     cpu_cx_lowest = val;
12716b74f9b7SNate Lawson 
1272b13cf774SNate Lawson     /* Update the new lowest useable Cx state for all CPUs. */
1273907b6777SNate Lawson     ACPI_SERIAL_BEGIN(cpu);
1274907b6777SNate Lawson     for (i = 0; i < cpu_ndevices; i++) {
1275907b6777SNate Lawson 	sc = device_get_softc(cpu_devices[i]);
127655fb7f36SSean Bruno 	acpi_cpu_set_cx_lowest(sc, min(val, sc->cpu_cx_count - 1));
1277907b6777SNate Lawson     }
1278d92a2ebdSNate Lawson     ACPI_SERIAL_END(cpu);
12796b74f9b7SNate Lawson 
12806b74f9b7SNate Lawson     return (0);
12816b74f9b7SNate Lawson }
1282