xref: /freebsd/sys/dev/acpica/acpi_cpu.c (revision 56a70eadf5d248c14ffb807ddb799f1dadb32df4)
1fec754d4SMike Smith /*-
256a70eadSNate Lawson  * Copyright (c) 2003 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>
3456a70eadSNate Lawson #include <sys/kernel.h>
356b74f9b7SNate Lawson #include <sys/pcpu.h>
3656a70eadSNate Lawson #include <sys/power.h>
3756a70eadSNate Lawson #include <sys/proc.h>
3856a70eadSNate Lawson #include <sys/sbuf.h>
396b74f9b7SNate Lawson #include <sys/smp.h>
40fec754d4SMike Smith 
416b74f9b7SNate Lawson #include <dev/pci/pcivar.h>
4256a70eadSNate Lawson #include <machine/atomic.h>
43fec754d4SMike Smith #include <machine/bus.h>
446b74f9b7SNate Lawson #ifdef __ia64__
456b74f9b7SNate Lawson #include <machine/pal.h>
466b74f9b7SNate Lawson #endif
4756a70eadSNate Lawson #include <sys/rman.h>
48fec754d4SMike Smith 
49fec754d4SMike Smith #include "acpi.h"
50fec754d4SMike Smith #include <dev/acpica/acpivar.h>
51fec754d4SMike Smith 
52fec754d4SMike Smith /*
536b74f9b7SNate Lawson  * Support for ACPI Processor devices, including ACPI 2.0 throttling
546b74f9b7SNate Lawson  * and C[1-3] sleep states.
55fec754d4SMike Smith  *
566b74f9b7SNate Lawson  * TODO: implement scans of all CPUs to be sure all Cx states are
576b74f9b7SNate Lawson  * equivalent.
58fec754d4SMike Smith  */
59fec754d4SMike Smith 
60be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
61fec754d4SMike Smith #define _COMPONENT	ACPI_PROCESSOR
629127281cSMike Smith ACPI_MODULE_NAME("PROCESSOR")
63fec754d4SMike Smith 
646b74f9b7SNate Lawson struct acpi_cx {
656b74f9b7SNate Lawson     struct resource	*p_lvlx;	/* Register to read to enter state. */
666b74f9b7SNate Lawson     uint32_t		 type;		/* C1-3 (C4 and up treated as C3). */
676b74f9b7SNate Lawson     uint32_t		 trans_lat;	/* Transition latency (usec). */
686b74f9b7SNate Lawson     uint32_t		 power;		/* Power consumed (mW). */
696b74f9b7SNate Lawson };
706b74f9b7SNate Lawson #define MAX_CX_STATES	 8
716b74f9b7SNate Lawson 
726b74f9b7SNate Lawson struct acpi_cx_stats {
736b74f9b7SNate Lawson     int			 long_slp;	/* Count of sleeps >= trans_lat. */
746b74f9b7SNate Lawson     int			 short_slp;	/* Count of sleeps < trans_lat. */
756b74f9b7SNate Lawson };
766b74f9b7SNate Lawson 
77fec754d4SMike Smith struct acpi_cpu_softc {
78fec754d4SMike Smith     device_t		 cpu_dev;
79fec754d4SMike Smith     ACPI_HANDLE		 cpu_handle;
806b74f9b7SNate Lawson     uint32_t		 cpu_id;	/* ACPI processor id */
8156a70eadSNate Lawson     uint32_t		 cpu_p_blk;	/* ACPI P_BLK location */
8256a70eadSNate Lawson     uint32_t		 cpu_p_blk_len;	/* P_BLK length (must be 6). */
836b74f9b7SNate Lawson     struct resource	*cpu_p_cnt;	/* Throttling control register */
846b74f9b7SNate Lawson     struct acpi_cx	 cpu_cx_states[MAX_CX_STATES];
8556a70eadSNate Lawson     int			 cpu_cx_count;	/* Number of valid Cx states. */
86fec754d4SMike Smith };
87fec754d4SMike Smith 
886b74f9b7SNate Lawson #define CPU_GET_REG(reg, width) 					\
896b74f9b7SNate Lawson     (bus_space_read_ ## width(rman_get_bustag((reg)), 			\
906b74f9b7SNate Lawson 		      rman_get_bushandle((reg)), 0))
916b74f9b7SNate Lawson #define CPU_SET_REG(reg, width, val)					\
926b74f9b7SNate Lawson     (bus_space_write_ ## width(rman_get_bustag((reg)), 			\
936b74f9b7SNate Lawson 		       rman_get_bushandle((reg)), 0, (val)))
94be2b1797SNate Lawson 
95fec754d4SMike Smith /*
96fec754d4SMike Smith  * Speeds are stored in counts, from 1 - CPU_MAX_SPEED, and
97fec754d4SMike Smith  * reported to the user in tenths of a percent.
98fec754d4SMike Smith  */
996b74f9b7SNate Lawson static uint32_t		 cpu_duty_offset;
1006b74f9b7SNate Lawson static uint32_t		 cpu_duty_width;
101fec754d4SMike Smith #define CPU_MAX_SPEED		(1 << cpu_duty_width)
102fec754d4SMike Smith #define CPU_SPEED_PERCENT(x)	((1000 * (x)) / CPU_MAX_SPEED)
1036b74f9b7SNate Lawson #define CPU_SPEED_PRINTABLE(x)	(CPU_SPEED_PERCENT(x) / 10),	\
1046b74f9b7SNate Lawson 				(CPU_SPEED_PERCENT(x) % 10)
1056b74f9b7SNate Lawson #define CPU_P_CNT_THT_EN (1<<4)
1066b74f9b7SNate Lawson #define PM_USEC(x)	 ((x) >> 2)	/* ~4 clocks per usec (3.57955 Mhz) */
107fec754d4SMike Smith 
1086b74f9b7SNate Lawson #define ACPI_CPU_NOTIFY_PERF_STATES	0x80	/* _PSS changed. */
1096b74f9b7SNate Lawson #define ACPI_CPU_NOTIFY_CX_STATES	0x81	/* _CST changed. */
110fec754d4SMike Smith 
1116b74f9b7SNate Lawson #define CPU_QUIRK_NO_C3		0x0001	/* C3-type states are not usable. */
1126b74f9b7SNate Lawson #define CPU_QUIRK_NO_THROTTLE	0x0002	/* Throttling is not usable. */
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
1206b74f9b7SNate Lawson 
1216b74f9b7SNate Lawson /* Platform hardware resource information. */
1226b74f9b7SNate Lawson static uint32_t		 cpu_smi_cmd;	/* Value to write to SMI_CMD. */
1236b74f9b7SNate Lawson static uint8_t		 cpu_pstate_cnt;/* Register to take over throttling. */
12456a70eadSNate Lawson static uint8_t		 cpu_cst_cnt;	/* Indicate we are _CST aware. */
1256b74f9b7SNate Lawson static uint32_t		 cpu_rid;	/* Driver-wide resource id. */
1266b74f9b7SNate Lawson static uint32_t		 cpu_quirks;	/* Indicate any hardware bugs. */
1276b74f9b7SNate Lawson 
1286b74f9b7SNate Lawson /* Runtime state. */
1296b74f9b7SNate Lawson static int		 cpu_cx_count;	/* Number of valid states */
1306b74f9b7SNate Lawson static uint32_t		 cpu_cx_next;	/* State to use for next sleep. */
1316b74f9b7SNate Lawson static uint32_t		 cpu_non_c3;	/* Index of lowest non-C3 state. */
1326b74f9b7SNate Lawson static struct acpi_cx_stats cpu_cx_stats[MAX_CX_STATES];
13356a70eadSNate Lawson #ifdef SMP
13456a70eadSNate Lawson static int		 cpu_idle_busy;	/* Count of CPUs in acpi_cpu_idle. */
13556a70eadSNate Lawson #endif
1366b74f9b7SNate Lawson 
1376b74f9b7SNate Lawson /* Values for sysctl. */
1386b74f9b7SNate Lawson static uint32_t		 cpu_current_state;
1396b74f9b7SNate Lawson static uint32_t		 cpu_performance_state;
1406b74f9b7SNate Lawson static uint32_t		 cpu_economy_state;
1416b74f9b7SNate Lawson static uint32_t		 cpu_max_state;
1426b74f9b7SNate Lawson static int		 cpu_cx_lowest;
1436b74f9b7SNate Lawson static char 		 cpu_cx_supported[64];
144fec754d4SMike Smith 
145fec754d4SMike Smith static device_t		*cpu_devices;
146fec754d4SMike Smith static int		 cpu_ndevices;
1476b74f9b7SNate Lawson static struct acpi_cpu_softc *cpu_softc[MAXCPU];
148fec754d4SMike Smith 
149fec754d4SMike Smith static struct sysctl_ctx_list	acpi_cpu_sysctl_ctx;
150fec754d4SMike Smith static struct sysctl_oid	*acpi_cpu_sysctl_tree;
151fec754d4SMike Smith 
152fec754d4SMike Smith static int	acpi_cpu_probe(device_t dev);
153fec754d4SMike Smith static int	acpi_cpu_attach(device_t dev);
15456a70eadSNate Lawson static int	acpi_cpu_shutdown(device_t dev);
1556b74f9b7SNate Lawson static int	acpi_cpu_throttle_probe(struct acpi_cpu_softc *sc);
1566b74f9b7SNate Lawson static int	acpi_cpu_cx_probe(struct acpi_cpu_softc *sc);
1576b74f9b7SNate Lawson static int	acpi_cpu_cx_cst(struct acpi_cpu_softc *sc);
1586b74f9b7SNate Lawson static void	acpi_cpu_startup(void *arg);
1596b74f9b7SNate Lawson static void	acpi_cpu_startup_throttling(void);
16056a70eadSNate Lawson static void	acpi_cpu_startup_cx(void);
1616b74f9b7SNate Lawson static void	acpi_cpu_throttle_set(uint32_t speed);
1626b74f9b7SNate Lawson static void	acpi_cpu_idle(void);
1636b74f9b7SNate Lawson static void	acpi_cpu_c1(void);
1646b74f9b7SNate Lawson static void	acpi_pm_ticksub(uint32_t *end, const uint32_t *start);
1656b74f9b7SNate Lawson static void	acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context);
1666b74f9b7SNate Lawson static int	acpi_cpu_quirks(struct acpi_cpu_softc *sc);
167899ccf54SMitsuru IWASAKI static void	acpi_cpu_power_profile(void *arg);
1686b74f9b7SNate Lawson static int	acpi_cpu_throttle_sysctl(SYSCTL_HANDLER_ARGS);
1696b74f9b7SNate Lawson static int	acpi_cpu_history_sysctl(SYSCTL_HANDLER_ARGS);
1706b74f9b7SNate Lawson static int	acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
171fec754d4SMike Smith 
172fec754d4SMike Smith static device_method_t acpi_cpu_methods[] = {
173fec754d4SMike Smith     /* Device interface */
174fec754d4SMike Smith     DEVMETHOD(device_probe,	acpi_cpu_probe),
175fec754d4SMike Smith     DEVMETHOD(device_attach,	acpi_cpu_attach),
17656a70eadSNate Lawson     DEVMETHOD(device_shutdown,	acpi_cpu_shutdown),
177fec754d4SMike Smith 
178fec754d4SMike Smith     {0, 0}
179fec754d4SMike Smith };
180fec754d4SMike Smith 
181fec754d4SMike Smith static driver_t acpi_cpu_driver = {
182fec754d4SMike Smith     "acpi_cpu",
183fec754d4SMike Smith     acpi_cpu_methods,
184fec754d4SMike Smith     sizeof(struct acpi_cpu_softc),
185fec754d4SMike Smith };
186fec754d4SMike Smith 
1873273b005SMike Smith static devclass_t acpi_cpu_devclass;
188fec754d4SMike Smith DRIVER_MODULE(acpi_cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0);
18956a70eadSNate Lawson 
190fec754d4SMike Smith static int
191fec754d4SMike Smith acpi_cpu_probe(device_t dev)
192fec754d4SMike Smith {
193be2b1797SNate Lawson     if (!acpi_disabled("cpu") && acpi_get_type(dev) == ACPI_TYPE_PROCESSOR) {
194be2b1797SNate Lawson 	device_set_desc(dev, "CPU");
195fec754d4SMike Smith 	return (0);
196fec754d4SMike Smith     }
197be2b1797SNate Lawson 
198fec754d4SMike Smith     return (ENXIO);
199fec754d4SMike Smith }
200fec754d4SMike Smith 
201fec754d4SMike Smith static int
202fec754d4SMike Smith acpi_cpu_attach(device_t dev)
203fec754d4SMike Smith {
204fec754d4SMike Smith     struct acpi_cpu_softc *sc;
205fec754d4SMike Smith     struct acpi_softc	  *acpi_sc;
2066b74f9b7SNate Lawson     ACPI_OBJECT		   pobj;
207fec754d4SMike Smith     ACPI_BUFFER		   buf;
208fec754d4SMike Smith     ACPI_STATUS		   status;
2096b74f9b7SNate Lawson     int			   thr_ret, cx_ret, cpu_id;
210fec754d4SMike Smith 
211b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
212fec754d4SMike Smith 
213fec754d4SMike Smith     ACPI_ASSERTLOCK;
214fec754d4SMike Smith 
215fec754d4SMike Smith     sc = device_get_softc(dev);
216fec754d4SMike Smith     sc->cpu_dev = dev;
217fec754d4SMike Smith     sc->cpu_handle = acpi_get_handle(dev);
218fec754d4SMike Smith 
2196b74f9b7SNate Lawson     /* Get our Processor object. */
2206b74f9b7SNate Lawson     buf.Pointer = &pobj;
2216b74f9b7SNate Lawson     buf.Length = sizeof(pobj);
2226b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf);
2236b74f9b7SNate Lawson     if (ACPI_FAILURE(status)) {
2246b74f9b7SNate Lawson 	device_printf(dev, "Couldn't get Processor object - %s\n",
2256b74f9b7SNate Lawson 		      AcpiFormatException(status));
2266b74f9b7SNate Lawson 	return_VALUE (ENXIO);
2276b74f9b7SNate Lawson     }
2286b74f9b7SNate Lawson     if (pobj.Type != ACPI_TYPE_PROCESSOR) {
2296b74f9b7SNate Lawson 	device_printf(dev, "Processor object has bad type %d\n", pobj.Type);
2306b74f9b7SNate Lawson 	return_VALUE (ENXIO);
231fec754d4SMike Smith     }
232be2b1797SNate Lawson 
2336b74f9b7SNate Lawson     cpu_id = 0;
2346b74f9b7SNate Lawson #ifdef SMP
2356b74f9b7SNate Lawson     /*
2366b74f9b7SNate Lawson      * Search all processors for one that matches our ACPI id.  This id
2376b74f9b7SNate Lawson      * is set in MD code early in the boot process from the MADT.  If
2386b74f9b7SNate Lawson      * UP or there is only one installed processor and the ACPI id has
2396b74f9b7SNate Lawson      * not yet been set, set the id ourselves.
2406b74f9b7SNate Lawson      */
2416b74f9b7SNate Lawson     cpu_id = pobj.Processor.ProcId;
2426b74f9b7SNate Lawson     if (cpu_id > MAXCPU - 1) {
24356a70eadSNate Lawson 	device_printf(dev, "CPU id %d too large (%d max)\n", cpu_id,
24456a70eadSNate Lawson 		      MAXCPU - 1);
24556a70eadSNate Lawson 	return_VALUE (ENXIO);
246fec754d4SMike Smith     }
2476b74f9b7SNate Lawson     if (mp_ncpus > 1) {
2486b74f9b7SNate Lawson 	struct pcpu	*pcpu_data;
2496b74f9b7SNate Lawson 	int		 i;
2506b74f9b7SNate Lawson 
2516b74f9b7SNate Lawson 	for (i = 0; i < MAXCPU; i++) {
2526b74f9b7SNate Lawson 	    if (CPU_ABSENT(i))
2536b74f9b7SNate Lawson 		continue;
2546b74f9b7SNate Lawson 	    pcpu_data = pcpu_find(i);
2556b74f9b7SNate Lawson 	    KASSERT(pcpu_data != NULL, ("No pcpu data for %d", i));
2566b74f9b7SNate Lawson 	    if (pcpu_data->pc_acpi_id == cpu_id)
2576b74f9b7SNate Lawson 		break;
2583e759f36SMike Smith 	}
2596b74f9b7SNate Lawson 	if (i == MAXCPU) {
2606b74f9b7SNate Lawson 	    device_printf(dev, "Couldn't find CPU for id %d\n", cpu_id);
26156a70eadSNate Lawson 	    return_VALUE (ENXIO);
2626b74f9b7SNate Lawson 	}
2636b74f9b7SNate Lawson     } else
2646b74f9b7SNate Lawson #endif /* SMP */
2656b74f9b7SNate Lawson     if (PCPU_GET(acpi_id) == 0xffffffff)
2666b74f9b7SNate Lawson 	PCPU_SET(acpi_id, cpu_id);
267fec754d4SMike Smith 
268fec754d4SMike Smith     /*
2696b74f9b7SNate Lawson      * Check if we already probed this processor.  We scan the bus twice
2706b74f9b7SNate Lawson      * so it's possible we've already seen this one.
271fec754d4SMike Smith      */
2726b74f9b7SNate Lawson     if (cpu_softc[cpu_id] != NULL)
2736b74f9b7SNate Lawson 	return (ENXIO);
2746b74f9b7SNate Lawson     cpu_softc[cpu_id] = sc;
2756b74f9b7SNate Lawson     sc->cpu_id = cpu_id;
2766b74f9b7SNate Lawson 
2776b74f9b7SNate Lawson     /*
2786b74f9b7SNate Lawson      * XXX Temporarily call any _INI function under the processor.
2796b74f9b7SNate Lawson      * ACPI-CA will do this after Nov. 2003.  The spec doesn't
2806b74f9b7SNate Lawson      * suggest processors have _INI methods but my Thinkpad T23 does.
2816b74f9b7SNate Lawson      */
2826b74f9b7SNate Lawson     AcpiEvaluateObject(sc->cpu_handle, "_INI", NULL, NULL);
2836b74f9b7SNate Lawson 
2846b74f9b7SNate Lawson     /* Get various global values from the Processor object. */
28556a70eadSNate Lawson     sc->cpu_p_blk = pobj.Processor.PblkAddress;
28656a70eadSNate Lawson     sc->cpu_p_blk_len = pobj.Processor.PblkLength;
28756a70eadSNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_BLK at %#x/%d\n",
28856a70eadSNate Lawson 		     device_get_unit(dev), sc->cpu_p_blk, sc->cpu_p_blk_len));
2896b74f9b7SNate Lawson 
290fec754d4SMike Smith     acpi_sc = acpi_device_get_parent_softc(dev);
291fec754d4SMike Smith     sysctl_ctx_init(&acpi_cpu_sysctl_ctx);
292fec754d4SMike Smith     acpi_cpu_sysctl_tree = SYSCTL_ADD_NODE(&acpi_cpu_sysctl_ctx,
293fec754d4SMike Smith 				SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
294fec754d4SMike Smith 				OID_AUTO, "cpu", CTLFLAG_RD, 0, "");
295fec754d4SMike Smith 
2966b74f9b7SNate Lawson     /* If this is the first device probed, check for quirks. */
2976b74f9b7SNate Lawson     if (device_get_unit(dev) == 0)
2986b74f9b7SNate Lawson 	acpi_cpu_quirks(sc);
2996b74f9b7SNate Lawson 
3006b74f9b7SNate Lawson     /*
3016b74f9b7SNate Lawson      * Probe for throttling and Cx state support.
3026b74f9b7SNate Lawson      * If none of these is present, free up unused resources.
3036b74f9b7SNate Lawson      */
3046b74f9b7SNate Lawson     thr_ret = acpi_cpu_throttle_probe(sc);
3056b74f9b7SNate Lawson     cx_ret = acpi_cpu_cx_probe(sc);
3066b74f9b7SNate Lawson     if (thr_ret == 0 || cx_ret == 0) {
3076b74f9b7SNate Lawson 	status = AcpiInstallNotifyHandler(sc->cpu_handle, ACPI_DEVICE_NOTIFY,
3086b74f9b7SNate Lawson 					  acpi_cpu_notify, sc);
30956a70eadSNate Lawson 	if (device_get_unit(dev) == 0)
31056a70eadSNate Lawson 	    AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cpu_startup, NULL);
3116b74f9b7SNate Lawson     } else {
3126b74f9b7SNate Lawson 	sysctl_ctx_free(&acpi_cpu_sysctl_ctx);
3136b74f9b7SNate Lawson     }
3146b74f9b7SNate Lawson 
3156b74f9b7SNate Lawson     return_VALUE (0);
3166b74f9b7SNate Lawson }
3176b74f9b7SNate Lawson 
3186b74f9b7SNate Lawson static int
31956a70eadSNate Lawson acpi_cpu_shutdown(device_t dev)
32056a70eadSNate Lawson {
32156a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
32256a70eadSNate Lawson 
32356a70eadSNate Lawson     /* Disable any entry to the idle function. */
32456a70eadSNate Lawson     cpu_cx_count = 0;
32556a70eadSNate Lawson 
32656a70eadSNate Lawson #ifdef SMP
32756a70eadSNate Lawson     /* Wait for all processors to exit acpi_cpu_idle(). */
32856a70eadSNate Lawson     smp_rendezvous(NULL, NULL, NULL, NULL);
32956a70eadSNate Lawson     while (cpu_idle_busy > 0)
33056a70eadSNate Lawson 	DELAY(1);
33156a70eadSNate Lawson #endif
33256a70eadSNate Lawson 
33356a70eadSNate Lawson     return_VALUE (0);
33456a70eadSNate Lawson }
33556a70eadSNate Lawson 
33656a70eadSNate Lawson static int
3376b74f9b7SNate Lawson acpi_cpu_throttle_probe(struct acpi_cpu_softc *sc)
3386b74f9b7SNate Lawson {
3396b74f9b7SNate Lawson     uint32_t		 duty_end;
3406b74f9b7SNate Lawson     ACPI_BUFFER		 buf;
3416b74f9b7SNate Lawson     ACPI_OBJECT		 obj;
3426b74f9b7SNate Lawson     ACPI_GENERIC_ADDRESS gas;
3436b74f9b7SNate Lawson     ACPI_STATUS		 status;
3446b74f9b7SNate Lawson 
3456b74f9b7SNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
3466b74f9b7SNate Lawson 
3476b74f9b7SNate Lawson     ACPI_ASSERTLOCK;
3486b74f9b7SNate Lawson 
3496b74f9b7SNate Lawson     /* Get throttling parameters from the FADT.  0 means not supported. */
35056a70eadSNate Lawson     if (device_get_unit(sc->cpu_dev) == 0) {
3516b74f9b7SNate Lawson 	cpu_smi_cmd = AcpiGbl_FADT->SmiCmd;
3526b74f9b7SNate Lawson 	cpu_pstate_cnt = AcpiGbl_FADT->PstateCnt;
35356a70eadSNate Lawson 	cpu_cst_cnt = AcpiGbl_FADT->CstCnt;
3546b74f9b7SNate Lawson 	cpu_duty_offset = AcpiGbl_FADT->DutyOffset;
3556b74f9b7SNate Lawson 	cpu_duty_width = AcpiGbl_FADT->DutyWidth;
35656a70eadSNate Lawson     }
3576b74f9b7SNate Lawson     if (cpu_duty_width == 0 || (cpu_quirks & CPU_QUIRK_NO_THROTTLE) != 0)
3586b74f9b7SNate Lawson 	return (ENXIO);
3596b74f9b7SNate Lawson 
3606b74f9b7SNate Lawson     /* Validate the duty offset/width. */
3616b74f9b7SNate Lawson     duty_end = cpu_duty_offset + cpu_duty_width - 1;
3626b74f9b7SNate Lawson     if (duty_end > 31) {
3636b74f9b7SNate Lawson 	device_printf(sc->cpu_dev, "CLK_VAL field overflows P_CNT register\n");
3646b74f9b7SNate Lawson 	return (ENXIO);
3656b74f9b7SNate Lawson     }
3666b74f9b7SNate Lawson     if (cpu_duty_offset <= 4 && duty_end >= 4) {
3676b74f9b7SNate Lawson 	device_printf(sc->cpu_dev, "CLK_VAL field overlaps THT_EN bit\n");
3686b74f9b7SNate Lawson 	return (ENXIO);
3696b74f9b7SNate Lawson     }
3706b74f9b7SNate Lawson 
3716b74f9b7SNate Lawson     /*
3726b74f9b7SNate Lawson      * If not present, fall back to using the processor's P_BLK to find
3736b74f9b7SNate Lawson      * the P_CNT register.
3746b74f9b7SNate Lawson      *
3756b74f9b7SNate Lawson      * Note that some systems seem to duplicate the P_BLK pointer
3766b74f9b7SNate Lawson      * across multiple CPUs, so not getting the resource is not fatal.
3776b74f9b7SNate Lawson      */
3786b74f9b7SNate Lawson     buf.Pointer = &obj;
3796b74f9b7SNate Lawson     buf.Length = sizeof(obj);
3806b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, "_PTC", NULL, &buf);
3816b74f9b7SNate Lawson     if (ACPI_SUCCESS(status)) {
3826b74f9b7SNate Lawson 	if (obj.Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3) {
3836b74f9b7SNate Lawson 	    device_printf(sc->cpu_dev, "_PTC buffer too small\n");
3846b74f9b7SNate Lawson 	    return (ENXIO);
3856b74f9b7SNate Lawson 	}
3866b74f9b7SNate Lawson 	memcpy(&gas, obj.Buffer.Pointer + 3, sizeof(gas));
3876b74f9b7SNate Lawson 	sc->cpu_p_cnt = acpi_bus_alloc_gas(sc->cpu_dev, &cpu_rid, &gas);
3886b74f9b7SNate Lawson 	if (sc->cpu_p_cnt != NULL) {
38956a70eadSNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_CNT from _PTC\n",
3906b74f9b7SNate Lawson 			     device_get_unit(sc->cpu_dev)));
3916b74f9b7SNate Lawson 	}
3926b74f9b7SNate Lawson     }
3936b74f9b7SNate Lawson 
3946b74f9b7SNate Lawson     /* If _PTC not present or other failure, try the P_BLK. */
3956b74f9b7SNate Lawson     if (sc->cpu_p_cnt == NULL) {
3966b74f9b7SNate Lawson 	/* The spec says P_BLK must be at least 6 bytes long. */
39756a70eadSNate Lawson 	if (sc->cpu_p_blk_len != 6)
3986b74f9b7SNate Lawson 	    return (ENXIO);
39956a70eadSNate Lawson 	gas.Address = sc->cpu_p_blk;
4006b74f9b7SNate Lawson 	gas.AddressSpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
4016b74f9b7SNate Lawson 	gas.RegisterBitWidth = 32;
4026b74f9b7SNate Lawson 	sc->cpu_p_cnt = acpi_bus_alloc_gas(sc->cpu_dev, &cpu_rid, &gas);
4036b74f9b7SNate Lawson 	if (sc->cpu_p_cnt != NULL) {
40456a70eadSNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_CNT from P_BLK\n",
4056b74f9b7SNate Lawson 			     device_get_unit(sc->cpu_dev)));
4066b74f9b7SNate Lawson 	} else {
4076b74f9b7SNate Lawson 	    device_printf(sc->cpu_dev, "Failed to attach throttling P_CNT\n");
4086b74f9b7SNate Lawson 	    return (ENXIO);
4096b74f9b7SNate Lawson 	}
4106b74f9b7SNate Lawson     }
4116b74f9b7SNate Lawson     cpu_rid++;
4126b74f9b7SNate Lawson 
4136b74f9b7SNate Lawson     return (0);
414fec754d4SMike Smith }
415fec754d4SMike Smith 
4166b74f9b7SNate Lawson static int
4176b74f9b7SNate Lawson acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
4186b74f9b7SNate Lawson {
4196b74f9b7SNate Lawson     ACPI_GENERIC_ADDRESS gas;
4206b74f9b7SNate Lawson     struct acpi_cx	*cx_ptr;
42156a70eadSNate Lawson     int			 error;
42256a70eadSNate Lawson 
42356a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
4246b74f9b7SNate Lawson 
4256b74f9b7SNate Lawson     /* Bus mastering arbitration control is needed for C3. */
4266b74f9b7SNate Lawson     if (AcpiGbl_FADT->V1_Pm2CntBlk == 0 || AcpiGbl_FADT->Pm2CntLen == 0) {
4276b74f9b7SNate Lawson 	cpu_quirks |= CPU_QUIRK_NO_C3;
42856a70eadSNate Lawson 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
42956a70eadSNate Lawson 			 "acpi_cpu%d: No BM control, C3 disabled\n",
43056a70eadSNate Lawson 			 device_get_unit(sc->cpu_dev)));
431fec754d4SMike Smith     }
432fec754d4SMike Smith 
433fec754d4SMike Smith     /*
4346b74f9b7SNate Lawson      * First, check for the ACPI 2.0 _CST sleep states object.
4356b74f9b7SNate Lawson      * If not usable, fall back to the P_BLK's P_LVL2 and P_LVL3.
436fec754d4SMike Smith      */
43756a70eadSNate Lawson     sc->cpu_cx_count = 0;
4386b74f9b7SNate Lawson     error = acpi_cpu_cx_cst(sc);
4396b74f9b7SNate Lawson     if (error != 0) {
4406b74f9b7SNate Lawson 	cx_ptr = sc->cpu_cx_states;
441fec754d4SMike Smith 
4426b74f9b7SNate Lawson 	/* C1 has been required since just after ACPI 1.0 */
4436b74f9b7SNate Lawson 	cx_ptr->type = ACPI_STATE_C1;
4446b74f9b7SNate Lawson 	cx_ptr->trans_lat = 0;
4456b74f9b7SNate Lawson 	cpu_non_c3 = 0;
4466b74f9b7SNate Lawson 	cx_ptr++;
44756a70eadSNate Lawson 	sc->cpu_cx_count++;
44856a70eadSNate Lawson 
44956a70eadSNate Lawson 	if (sc->cpu_p_blk_len != 6)
45056a70eadSNate Lawson 	    goto done;
451fec754d4SMike Smith 
4526b74f9b7SNate Lawson 	/* Validate and allocate resources for C2 (P_LVL2). */
4536b74f9b7SNate Lawson 	gas.AddressSpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
4546b74f9b7SNate Lawson 	gas.RegisterBitWidth = 8;
4556b74f9b7SNate Lawson 	if (AcpiGbl_FADT->Plvl2Lat < 100) {
45656a70eadSNate Lawson 	    gas.Address = sc->cpu_p_blk + 4;
4576b74f9b7SNate Lawson 	    cx_ptr->p_lvlx = acpi_bus_alloc_gas(sc->cpu_dev, &cpu_rid, &gas);
4586b74f9b7SNate Lawson 	    if (cx_ptr->p_lvlx != NULL) {
4596b74f9b7SNate Lawson 		cpu_rid++;
4606b74f9b7SNate Lawson 		cx_ptr->type = ACPI_STATE_C2;
4616b74f9b7SNate Lawson 		cx_ptr->trans_lat = AcpiGbl_FADT->Plvl2Lat;
4626b74f9b7SNate Lawson 		cpu_non_c3 = 1;
4636b74f9b7SNate Lawson 		cx_ptr++;
46456a70eadSNate Lawson 		sc->cpu_cx_count++;
465fec754d4SMike Smith 	    }
466fec754d4SMike Smith 	}
467be2b1797SNate Lawson 
4686b74f9b7SNate Lawson 	/* Validate and allocate resources for C3 (P_LVL3). */
4696b74f9b7SNate Lawson 	if (AcpiGbl_FADT->Plvl3Lat < 1000 &&
4706b74f9b7SNate Lawson 	    (cpu_quirks & CPU_QUIRK_NO_C3) == 0) {
4716b74f9b7SNate Lawson 
47256a70eadSNate Lawson 	    gas.Address = sc->cpu_p_blk + 5;
4736b74f9b7SNate Lawson 	    cx_ptr->p_lvlx = acpi_bus_alloc_gas(sc->cpu_dev, &cpu_rid, &gas);
4746b74f9b7SNate Lawson 	    if (cx_ptr->p_lvlx != NULL) {
4756b74f9b7SNate Lawson 		cpu_rid++;
4766b74f9b7SNate Lawson 		cx_ptr->type = ACPI_STATE_C3;
4776b74f9b7SNate Lawson 		cx_ptr->trans_lat = AcpiGbl_FADT->Plvl3Lat;
4786b74f9b7SNate Lawson 		cx_ptr++;
47956a70eadSNate Lawson 		sc->cpu_cx_count++;
4806b74f9b7SNate Lawson 	    }
4816b74f9b7SNate Lawson 	}
4826b74f9b7SNate Lawson     }
4836b74f9b7SNate Lawson 
48456a70eadSNate Lawson done:
4856b74f9b7SNate Lawson     /* If no valid registers were found, don't attach. */
48656a70eadSNate Lawson     if (sc->cpu_cx_count == 0)
4876b74f9b7SNate Lawson 	return (ENXIO);
4886b74f9b7SNate Lawson 
4896b74f9b7SNate Lawson     return (0);
4906b74f9b7SNate Lawson }
4916b74f9b7SNate Lawson 
4926b74f9b7SNate Lawson /*
4936b74f9b7SNate Lawson  * Parse a _CST package and set up its Cx states.  Since the _CST object
4946b74f9b7SNate Lawson  * can change dynamically, our notify handler may call this function
4956b74f9b7SNate Lawson  * to clean up and probe the new _CST package.
4966b74f9b7SNate Lawson  */
4976b74f9b7SNate Lawson static int
4986b74f9b7SNate Lawson acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
4996b74f9b7SNate Lawson {
5006b74f9b7SNate Lawson     struct	 acpi_cx *cx_ptr;
5016b74f9b7SNate Lawson     ACPI_STATUS	 status;
5026b74f9b7SNate Lawson     ACPI_BUFFER	 buf;
5036b74f9b7SNate Lawson     ACPI_OBJECT	*top;
5046b74f9b7SNate Lawson     ACPI_OBJECT	*pkg;
5056b74f9b7SNate Lawson     uint32_t	 count;
5066b74f9b7SNate Lawson     int		 i;
5076b74f9b7SNate Lawson 
50856a70eadSNate Lawson     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
50956a70eadSNate Lawson 
5106b74f9b7SNate Lawson     buf.Pointer = NULL;
5116b74f9b7SNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
5126b74f9b7SNate Lawson     status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
5136b74f9b7SNate Lawson     if (ACPI_FAILURE(status))
5146b74f9b7SNate Lawson 	return (ENXIO);
5156b74f9b7SNate Lawson 
5166b74f9b7SNate Lawson     /* _CST is a package with a count and at least one Cx package. */
5176b74f9b7SNate Lawson     top = (ACPI_OBJECT *)buf.Pointer;
5186b74f9b7SNate Lawson     if (!ACPI_PKG_VALID(top, 2)) {
5196b74f9b7SNate Lawson 	device_printf(sc->cpu_dev, "Invalid _CST package\n");
5206b74f9b7SNate Lawson 	AcpiOsFree(buf.Pointer);
5216b74f9b7SNate Lawson 	return (ENXIO);
5226b74f9b7SNate Lawson     }
5236b74f9b7SNate Lawson     acpi_PkgInt32(sc->cpu_dev, top, 0, &count);
5246b74f9b7SNate Lawson     if (count != top->Package.Count - 1) {
5256b74f9b7SNate Lawson 	device_printf(sc->cpu_dev, "Invalid _CST state count (%d != %d)\n",
5266b74f9b7SNate Lawson 	       count, top->Package.Count - 1);
5276b74f9b7SNate Lawson 	count = top->Package.Count - 1;
5286b74f9b7SNate Lawson     }
5296b74f9b7SNate Lawson     if (count > MAX_CX_STATES) {
53056a70eadSNate Lawson 	device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
5316b74f9b7SNate Lawson 	count = MAX_CX_STATES;
5326b74f9b7SNate Lawson     }
5336b74f9b7SNate Lawson 
5346b74f9b7SNate Lawson     /* Set up all valid states. */
53556a70eadSNate Lawson     sc->cpu_cx_count = 0;
5366b74f9b7SNate Lawson     cx_ptr = sc->cpu_cx_states;
5376b74f9b7SNate Lawson     for (i = 0; i < count; i++) {
5386b74f9b7SNate Lawson 	pkg = &top->Package.Elements[i + 1];
5396b74f9b7SNate Lawson 	if (!ACPI_PKG_VALID(pkg, 4)) {
5406b74f9b7SNate Lawson 	    device_printf(sc->cpu_dev, "Skipping invalid Cx state package\n");
5416b74f9b7SNate Lawson 	    continue;
5426b74f9b7SNate Lawson 	}
5436b74f9b7SNate Lawson 
5446b74f9b7SNate Lawson 	/* Cx type, transition latency, power consumed. */
5456b74f9b7SNate Lawson 	acpi_PkgInt32(sc->cpu_dev, pkg, 1, &cx_ptr->type);
5466b74f9b7SNate Lawson 	acpi_PkgInt32(sc->cpu_dev, pkg, 2, &cx_ptr->trans_lat);
5476b74f9b7SNate Lawson 	acpi_PkgInt32(sc->cpu_dev, pkg, 3, &cx_ptr->power);
5486b74f9b7SNate Lawson 
5496b74f9b7SNate Lawson 	/* Validate the state to see if we should use it. */
5506b74f9b7SNate Lawson 	switch (cx_ptr->type) {
5516b74f9b7SNate Lawson 	case ACPI_STATE_C1:
5526b74f9b7SNate Lawson 	    cpu_non_c3 = i;
5536b74f9b7SNate Lawson 	    cx_ptr++;
55456a70eadSNate Lawson 	    sc->cpu_cx_count++;
5556b74f9b7SNate Lawson 	    continue;
5566b74f9b7SNate Lawson 	case ACPI_STATE_C2:
5576b74f9b7SNate Lawson 	    if (cx_ptr->trans_lat > 100) {
55856a70eadSNate Lawson 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
55956a70eadSNate Lawson 				 "acpi_cpu%d: C2[%d] not available.\n",
56056a70eadSNate Lawson 				 device_get_unit(sc->cpu_dev), i));
5616b74f9b7SNate Lawson 		continue;
5626b74f9b7SNate Lawson 	    }
5636b74f9b7SNate Lawson 	    cpu_non_c3 = i;
5646b74f9b7SNate Lawson 	    break;
5656b74f9b7SNate Lawson 	case ACPI_STATE_C3:
5666b74f9b7SNate Lawson 	default:
5676b74f9b7SNate Lawson 	    if (cx_ptr->trans_lat > 1000 ||
5686b74f9b7SNate Lawson 		(cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
5696b74f9b7SNate Lawson 
57056a70eadSNate Lawson 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
57156a70eadSNate Lawson 				 "acpi_cpu%d: C3[%d] not available.\n",
57256a70eadSNate Lawson 				 device_get_unit(sc->cpu_dev), i));
5736b74f9b7SNate Lawson 		continue;
5746b74f9b7SNate Lawson 	    }
5756b74f9b7SNate Lawson 	    break;
5766b74f9b7SNate Lawson 	}
5776b74f9b7SNate Lawson 
5786b74f9b7SNate Lawson #ifdef notyet
5796b74f9b7SNate Lawson 	/* Free up any previous register. */
5806b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
5816b74f9b7SNate Lawson 	    bus_release_resource(sc->cpu_dev, 0, 0, cx_ptr->p_lvlx);
5826b74f9b7SNate Lawson 	    cx_ptr->p_lvlx = NULL;
5836b74f9b7SNate Lawson 	}
5846b74f9b7SNate Lawson #endif
5856b74f9b7SNate Lawson 
5866b74f9b7SNate Lawson 	/* Allocate the control register for C2 or C3. */
5876b74f9b7SNate Lawson 	acpi_PkgGas(sc->cpu_dev, pkg, 0, &cpu_rid, &cx_ptr->p_lvlx);
5886b74f9b7SNate Lawson 	if (cx_ptr->p_lvlx != NULL) {
5896b74f9b7SNate Lawson 	    cpu_rid++;
59056a70eadSNate Lawson 	    ACPI_DEBUG_PRINT((ACPI_DB_INFO,
59156a70eadSNate Lawson 			     "acpi_cpu%d: Got C%d - %d latency\n",
59256a70eadSNate Lawson 			     device_get_unit(sc->cpu_dev), cx_ptr->type,
59356a70eadSNate Lawson 			     cx_ptr->trans_lat));
5946b74f9b7SNate Lawson 	    cx_ptr++;
59556a70eadSNate Lawson 	    sc->cpu_cx_count++;
5966b74f9b7SNate Lawson 	}
5976b74f9b7SNate Lawson     }
5986b74f9b7SNate Lawson     AcpiOsFree(buf.Pointer);
5996b74f9b7SNate Lawson 
6006b74f9b7SNate Lawson     return (0);
601fec754d4SMike Smith }
602fec754d4SMike Smith 
603fec754d4SMike Smith /*
604fec754d4SMike Smith  * Call this *after* all CPUs have been attached.
605fec754d4SMike Smith  */
606fec754d4SMike Smith static void
6076b74f9b7SNate Lawson acpi_cpu_startup(void *arg)
608fec754d4SMike Smith {
60956a70eadSNate Lawson     struct acpi_cpu_softc *sc;
61056a70eadSNate Lawson     int count, i;
611fec754d4SMike Smith 
612be2b1797SNate Lawson     /* Get set of CPU devices */
613fec754d4SMike Smith     devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
614fec754d4SMike Smith 
6156b74f9b7SNate Lawson     /* Register performance profile change handler */
6166b74f9b7SNate Lawson     EVENTHANDLER_REGISTER(power_profile_change, acpi_cpu_power_profile,
6176b74f9b7SNate Lawson 			  NULL, 0);
6186b74f9b7SNate Lawson 
61956a70eadSNate Lawson     /*
62056a70eadSNate Lawson      * Make sure all the processors' Cx counts match.  We should probably
62156a70eadSNate Lawson      * also check the contents of each.  However, no known systems have
62256a70eadSNate Lawson      * non-matching Cx counts so we'll deal with this later.
62356a70eadSNate Lawson      */
62456a70eadSNate Lawson     count = MAX_CX_STATES;
62556a70eadSNate Lawson     for (i = 0; i < cpu_ndevices; i++) {
62656a70eadSNate Lawson 	sc = device_get_softc(cpu_devices[i]);
62756a70eadSNate Lawson 	count = min(sc->cpu_cx_count, count);
62856a70eadSNate Lawson     }
62956a70eadSNate Lawson     cpu_cx_count = count;
63056a70eadSNate Lawson 
63156a70eadSNate Lawson     /* Perform throttling and Cx final initialization. */
63256a70eadSNate Lawson     sc = device_get_softc(cpu_devices[0]);
6336b74f9b7SNate Lawson     if (sc->cpu_p_cnt != NULL)
6346b74f9b7SNate Lawson 	acpi_cpu_startup_throttling();
63556a70eadSNate Lawson     if (cpu_cx_count > 0)
63656a70eadSNate Lawson 	acpi_cpu_startup_cx();
6376b74f9b7SNate Lawson }
6386b74f9b7SNate Lawson 
63956a70eadSNate Lawson /*
64056a70eadSNate Lawson  * Takes the ACPI lock to avoid fighting anyone over the SMI command
64156a70eadSNate Lawson  * port.
64256a70eadSNate Lawson  */
6436b74f9b7SNate Lawson static void
6446b74f9b7SNate Lawson acpi_cpu_startup_throttling()
6456b74f9b7SNate Lawson {
6466b74f9b7SNate Lawson     int cpu_temp_speed;
64756a70eadSNate Lawson     ACPI_LOCK_DECL;
6486b74f9b7SNate Lawson 
649be2b1797SNate Lawson     /* Initialise throttling states */
650fec754d4SMike Smith     cpu_max_state = CPU_MAX_SPEED;
651fec754d4SMike Smith     cpu_performance_state = cpu_max_state;
652fec754d4SMike Smith     cpu_economy_state = cpu_performance_state / 2;
653be2b1797SNate Lawson 
654be2b1797SNate Lawson     /* 0 is 'reserved' */
655be2b1797SNate Lawson     if (cpu_economy_state == 0)
656fec754d4SMike Smith 	cpu_economy_state++;
657be2b1797SNate Lawson     if (TUNABLE_INT_FETCH("hw.acpi.cpu.performance_speed", &cpu_temp_speed) &&
658be2b1797SNate Lawson 	cpu_temp_speed > 0 && cpu_temp_speed <= cpu_max_state) {
659be2b1797SNate Lawson 
660e5e5b51fSJohn Baldwin 	cpu_performance_state = cpu_temp_speed;
661fec754d4SMike Smith     }
662be2b1797SNate Lawson     if (TUNABLE_INT_FETCH("hw.acpi.cpu.economy_speed", &cpu_temp_speed) &&
663be2b1797SNate Lawson 	cpu_temp_speed > 0 && cpu_temp_speed <= cpu_max_state) {
664be2b1797SNate Lawson 
665be2b1797SNate Lawson 	cpu_economy_state = cpu_temp_speed;
666be2b1797SNate Lawson     }
667be2b1797SNate Lawson 
66856a70eadSNate Lawson     SYSCTL_ADD_INT(&acpi_cpu_sysctl_ctx,
66956a70eadSNate Lawson 		   SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
67056a70eadSNate Lawson 		   OID_AUTO, "max_speed", CTLFLAG_RD,
67156a70eadSNate Lawson 		   &cpu_max_state, 0, "maximum CPU speed");
67256a70eadSNate Lawson     SYSCTL_ADD_INT(&acpi_cpu_sysctl_ctx,
67356a70eadSNate Lawson 		   SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
67456a70eadSNate Lawson 		   OID_AUTO, "current_speed", CTLFLAG_RD,
67556a70eadSNate Lawson 		   &cpu_current_state, 0, "current CPU speed");
67656a70eadSNate Lawson     SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx,
67756a70eadSNate Lawson 		    SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
67856a70eadSNate Lawson 		    OID_AUTO, "performance_speed",
67956a70eadSNate Lawson 		    CTLTYPE_INT | CTLFLAG_RW, &cpu_performance_state,
68056a70eadSNate Lawson 		    0, acpi_cpu_throttle_sysctl, "I", "");
68156a70eadSNate Lawson     SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx,
68256a70eadSNate Lawson 		    SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
68356a70eadSNate Lawson 		    OID_AUTO, "economy_speed",
68456a70eadSNate Lawson 		    CTLTYPE_INT | CTLFLAG_RW, &cpu_economy_state,
68556a70eadSNate Lawson 		    0, acpi_cpu_throttle_sysctl, "I", "");
686fec754d4SMike Smith 
68756a70eadSNate Lawson     /* If ACPI 2.0+, signal platform that we are taking over throttling. */
68856a70eadSNate Lawson     if (cpu_pstate_cnt != 0) {
68956a70eadSNate Lawson 	ACPI_LOCK;
69056a70eadSNate Lawson 	AcpiOsWritePort(cpu_smi_cmd, cpu_pstate_cnt, 8);
691fec754d4SMike Smith 	ACPI_UNLOCK;
69256a70eadSNate Lawson     }
693fec754d4SMike Smith 
694be2b1797SNate Lawson     /* Set initial speed */
695899ccf54SMitsuru IWASAKI     acpi_cpu_power_profile(NULL);
696fec754d4SMike Smith 
697a40f20c7SNate Lawson     printf("acpi_cpu: throttling enabled, %d steps (100%% to %d.%d%%), "
698a40f20c7SNate Lawson 	   "currently %d.%d%%\n", CPU_MAX_SPEED, CPU_SPEED_PRINTABLE(1),
699a40f20c7SNate Lawson 	   CPU_SPEED_PRINTABLE(cpu_current_state));
700fec754d4SMike Smith }
701fec754d4SMike Smith 
70256a70eadSNate Lawson static void
70356a70eadSNate Lawson acpi_cpu_startup_cx()
70456a70eadSNate Lawson {
70556a70eadSNate Lawson     struct acpi_cpu_softc *sc;
70656a70eadSNate Lawson     struct sbuf		 sb;
70756a70eadSNate Lawson     int i;
70856a70eadSNate Lawson     ACPI_LOCK_DECL;
70956a70eadSNate Lawson 
71056a70eadSNate Lawson     sc = device_get_softc(cpu_devices[0]);
71156a70eadSNate Lawson     sbuf_new(&sb, cpu_cx_supported, sizeof(cpu_cx_supported), SBUF_FIXEDLEN);
71256a70eadSNate Lawson     for (i = 0; i < cpu_cx_count; i++) {
71356a70eadSNate Lawson 	sbuf_printf(&sb, "C%d/%d ", sc->cpu_cx_states[i].type,
71456a70eadSNate Lawson 		    sc->cpu_cx_states[i].trans_lat);
71556a70eadSNate Lawson     }
71656a70eadSNate Lawson     sbuf_trim(&sb);
71756a70eadSNate Lawson     sbuf_finish(&sb);
71856a70eadSNate Lawson     SYSCTL_ADD_STRING(&acpi_cpu_sysctl_ctx,
71956a70eadSNate Lawson 		      SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
72056a70eadSNate Lawson 		      OID_AUTO, "cx_supported", CTLFLAG_RD, cpu_cx_supported,
72156a70eadSNate Lawson 		      0, "Cx/microsecond values for supported Cx states");
72256a70eadSNate Lawson     SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx,
72356a70eadSNate Lawson 		    SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
72456a70eadSNate Lawson 		    OID_AUTO, "cx_lowest", CTLTYPE_INT | CTLFLAG_RW,
72556a70eadSNate Lawson 		    NULL, 0, acpi_cpu_cx_lowest_sysctl, "I",
72656a70eadSNate Lawson 		    "lowest Cx sleep state to use");
72756a70eadSNate Lawson     SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx,
72856a70eadSNate Lawson 		    SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
72956a70eadSNate Lawson 		    OID_AUTO, "cx_history", CTLTYPE_STRING | CTLFLAG_RD,
73056a70eadSNate Lawson 		    NULL, 0, acpi_cpu_history_sysctl, "A", "");
73156a70eadSNate Lawson 
73256a70eadSNate Lawson #ifdef notyet
73356a70eadSNate Lawson     /* Signal platform that we can handle _CST notification. */
73456a70eadSNate Lawson     if (cpu_cst_cnt != 0) {
73556a70eadSNate Lawson 	ACPI_LOCK;
73656a70eadSNate Lawson 	AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
73756a70eadSNate Lawson 	ACPI_UNLOCK;
73856a70eadSNate Lawson     }
73956a70eadSNate Lawson #endif
74056a70eadSNate Lawson 
74156a70eadSNate Lawson     /* Take over idling from cpu_idle_default(). */
74256a70eadSNate Lawson     cpu_cx_next = cpu_cx_lowest;
74356a70eadSNate Lawson     cpu_idle_hook = acpi_cpu_idle;
74456a70eadSNate Lawson }
74556a70eadSNate Lawson 
746fec754d4SMike Smith /*
747fec754d4SMike Smith  * Set CPUs to the new state.
748fec754d4SMike Smith  *
749fec754d4SMike Smith  * Must be called with the ACPI lock held.
750fec754d4SMike Smith  */
751fec754d4SMike Smith static void
7526b74f9b7SNate Lawson acpi_cpu_throttle_set(uint32_t speed)
753fec754d4SMike Smith {
754fec754d4SMike Smith     struct acpi_cpu_softc	*sc;
755fec754d4SMike Smith     int				i;
7566b74f9b7SNate Lawson     uint32_t			p_cnt, clk_val;
757fec754d4SMike Smith 
758fec754d4SMike Smith     ACPI_ASSERTLOCK;
759fec754d4SMike Smith 
760be2b1797SNate Lawson     /* Iterate over processors */
761fec754d4SMike Smith     for (i = 0; i < cpu_ndevices; i++) {
762fec754d4SMike Smith 	sc = device_get_softc(cpu_devices[i]);
7636b74f9b7SNate Lawson 	if (sc->cpu_p_cnt == NULL)
764fec754d4SMike Smith 	    continue;
765fec754d4SMike Smith 
766be2b1797SNate Lawson 	/* Get the current P_CNT value and disable throttling */
7676b74f9b7SNate Lawson 	p_cnt = CPU_GET_REG(sc->cpu_p_cnt, 4);
768fec754d4SMike Smith 	p_cnt &= ~CPU_P_CNT_THT_EN;
7696b74f9b7SNate Lawson 	CPU_SET_REG(sc->cpu_p_cnt, 4, p_cnt);
770fec754d4SMike Smith 
771be2b1797SNate Lawson 	/* If we're at maximum speed, that's all */
772fec754d4SMike Smith 	if (speed < CPU_MAX_SPEED) {
773be2b1797SNate Lawson 	    /* Mask the old CLK_VAL off and or-in the new value */
774fec754d4SMike Smith 	    clk_val = CPU_MAX_SPEED << cpu_duty_offset;
775fec754d4SMike Smith 	    p_cnt &= ~clk_val;
776fec754d4SMike Smith 	    p_cnt |= (speed << cpu_duty_offset);
777fec754d4SMike Smith 
778be2b1797SNate Lawson 	    /* Write the new P_CNT value and then enable throttling */
7796b74f9b7SNate Lawson 	    CPU_SET_REG(sc->cpu_p_cnt, 4, p_cnt);
780fec754d4SMike Smith 	    p_cnt |= CPU_P_CNT_THT_EN;
7816b74f9b7SNate Lawson 	    CPU_SET_REG(sc->cpu_p_cnt, 4, p_cnt);
782fec754d4SMike Smith 	}
7836971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->cpu_dev, acpi_device_get_parent_softc(sc->cpu_dev),
7846971b3c7SMitsuru IWASAKI 		    "set speed to %d.%d%%\n", CPU_SPEED_PRINTABLE(speed));
785fec754d4SMike Smith     }
786fec754d4SMike Smith     cpu_current_state = speed;
787fec754d4SMike Smith }
788fec754d4SMike Smith 
789fec754d4SMike Smith /*
7906b74f9b7SNate Lawson  * Idle the CPU in the lowest state possible.
7916b74f9b7SNate Lawson  * This function is called with interrupts disabled.
7926b74f9b7SNate Lawson  */
7936b74f9b7SNate Lawson static void
7946b74f9b7SNate Lawson acpi_cpu_idle()
7956b74f9b7SNate Lawson {
7966b74f9b7SNate Lawson     struct	acpi_cpu_softc *sc;
79756a70eadSNate Lawson     struct	acpi_cx *cx_next;
7986b74f9b7SNate Lawson     uint32_t	start_time, end_time;
79956a70eadSNate Lawson     int		bm_active, i, asleep;
8006b74f9b7SNate Lawson 
80156a70eadSNate Lawson #ifdef SMP
8026b74f9b7SNate Lawson     /* Look up our CPU id and to get our softc. */
8036b74f9b7SNate Lawson     sc = cpu_softc[PCPU_GET(acpi_id)];
80456a70eadSNate Lawson #else
80556a70eadSNate Lawson     sc = cpu_softc[0];
80656a70eadSNate Lawson #endif
8076b74f9b7SNate Lawson     KASSERT(sc != NULL, ("NULL softc for %d", PCPU_GET(acpi_id)));
8086b74f9b7SNate Lawson 
8096b74f9b7SNate Lawson     /* If disabled, return immediately. */
81056a70eadSNate Lawson     if (cpu_cx_count == 0) {
8116b74f9b7SNate Lawson 	ACPI_ENABLE_IRQS();
8126b74f9b7SNate Lawson 	return;
8136b74f9b7SNate Lawson     }
8146b74f9b7SNate Lawson 
81556a70eadSNate Lawson #ifdef SMP
81656a70eadSNate Lawson     /* Record that a CPU is in the idle function. */
81756a70eadSNate Lawson     atomic_add_int(&cpu_idle_busy, 1);
81856a70eadSNate Lawson #endif
81956a70eadSNate Lawson 
8206b74f9b7SNate Lawson     /*
8216b74f9b7SNate Lawson      * Check for bus master activity.  If there was activity, clear
8226b74f9b7SNate Lawson      * the bit and use the lowest non-C3 state.  Note that the USB
8236b74f9b7SNate Lawson      * driver polling for new devices keeps this bit set all the
8246b74f9b7SNate Lawson      * time if USB is enabled.
8256b74f9b7SNate Lawson      */
8266b74f9b7SNate Lawson     AcpiGetRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active,
8276b74f9b7SNate Lawson 		    ACPI_MTX_DO_NOT_LOCK);
8286b74f9b7SNate Lawson     if (bm_active != 0) {
8296b74f9b7SNate Lawson 	AcpiSetRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1,
8306b74f9b7SNate Lawson 			ACPI_MTX_DO_NOT_LOCK);
8316b74f9b7SNate Lawson 	cpu_cx_next = min(cpu_cx_next, cpu_non_c3);
8326b74f9b7SNate Lawson     }
8336b74f9b7SNate Lawson 
8346b74f9b7SNate Lawson     /* Perform the actual sleep based on the Cx-specific semantics. */
8356b74f9b7SNate Lawson     cx_next = &sc->cpu_cx_states[cpu_cx_next];
8366b74f9b7SNate Lawson     switch (cx_next->type) {
83756a70eadSNate Lawson     case ACPI_STATE_C0:
83856a70eadSNate Lawson 	panic("acpi_cpu_idle: attempting to sleep in C0");
83956a70eadSNate Lawson 	/* NOTREACHED */
8406b74f9b7SNate Lawson     case ACPI_STATE_C1:
8416b74f9b7SNate Lawson 	/* Execute HLT (or equivalent) and wait for an interrupt. */
8426b74f9b7SNate Lawson 	acpi_cpu_c1();
8436b74f9b7SNate Lawson 
8446b74f9b7SNate Lawson 	/*
8456b74f9b7SNate Lawson 	 * We can't calculate the time spent in C1 since the place we
8466b74f9b7SNate Lawson 	 * wake up is an ISR.  Use a constant time of 1 ms.
8476b74f9b7SNate Lawson 	 */
8486b74f9b7SNate Lawson 	start_time = 0;
8496b74f9b7SNate Lawson 	end_time = 1000;
8506b74f9b7SNate Lawson 	break;
8516b74f9b7SNate Lawson     case ACPI_STATE_C2:
8526b74f9b7SNate Lawson 	/*
8536b74f9b7SNate Lawson 	 * Read from P_LVLx to enter C2, checking time spent asleep.
8546b74f9b7SNate Lawson 	 * Use the ACPI timer for measuring sleep time.  Since we need to
8556b74f9b7SNate Lawson 	 * get the time very close to the CPU start/stop clock logic, this
8566b74f9b7SNate Lawson 	 * is the only reliable time source.
8576b74f9b7SNate Lawson 	 */
8586b74f9b7SNate Lawson 	AcpiHwLowLevelRead(32, &start_time, &AcpiGbl_FADT->XPmTmrBlk);
8596b74f9b7SNate Lawson 	CPU_GET_REG(cx_next->p_lvlx, 1);
8606b74f9b7SNate Lawson 
8616b74f9b7SNate Lawson 	/*
8626b74f9b7SNate Lawson 	 * Read the end time twice.  Since it may take an arbitrary time
8636b74f9b7SNate Lawson 	 * to enter the idle state, the first read may be executed before
8646b74f9b7SNate Lawson 	 * the processor has stopped.  Doing it again provides enough
8656b74f9b7SNate Lawson 	 * margin that we are certain to have a correct value.
8666b74f9b7SNate Lawson 	 */
8676b74f9b7SNate Lawson 	AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT->XPmTmrBlk);
8686b74f9b7SNate Lawson 	AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT->XPmTmrBlk);
8696b74f9b7SNate Lawson 	ACPI_ENABLE_IRQS();
8706b74f9b7SNate Lawson 	break;
8716b74f9b7SNate Lawson     case ACPI_STATE_C3:
8726b74f9b7SNate Lawson     default:
8736b74f9b7SNate Lawson 	/* Disable bus master arbitration and enable bus master wakeup. */
8746b74f9b7SNate Lawson 	AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 1, ACPI_MTX_DO_NOT_LOCK);
8756b74f9b7SNate Lawson 	AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 1, ACPI_MTX_DO_NOT_LOCK);
8766b74f9b7SNate Lawson 
8776b74f9b7SNate Lawson 	/* Read from P_LVLx to enter C3, checking time spent asleep. */
8786b74f9b7SNate Lawson 	AcpiHwLowLevelRead(32, &start_time, &AcpiGbl_FADT->XPmTmrBlk);
8796b74f9b7SNate Lawson 	CPU_GET_REG(cx_next->p_lvlx, 1);
8806b74f9b7SNate Lawson 
8816b74f9b7SNate Lawson 	/* Read the end time twice.  See comment for C2 above. */
8826b74f9b7SNate Lawson 	AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT->XPmTmrBlk);
8836b74f9b7SNate Lawson 	AcpiHwLowLevelRead(32, &end_time, &AcpiGbl_FADT->XPmTmrBlk);
8846b74f9b7SNate Lawson 
8856b74f9b7SNate Lawson 	/* Enable bus master arbitration and disable bus master wakeup. */
8866b74f9b7SNate Lawson 	AcpiSetRegister(ACPI_BITREG_ARB_DISABLE, 0, ACPI_MTX_DO_NOT_LOCK);
8876b74f9b7SNate Lawson 	AcpiSetRegister(ACPI_BITREG_BUS_MASTER_RLD, 0, ACPI_MTX_DO_NOT_LOCK);
8886b74f9b7SNate Lawson 	ACPI_ENABLE_IRQS();
8896b74f9b7SNate Lawson 	break;
8906b74f9b7SNate Lawson     }
8916b74f9b7SNate Lawson 
8926b74f9b7SNate Lawson     /* Find the actual time asleep in microseconds, minus overhead. */
8936b74f9b7SNate Lawson     acpi_pm_ticksub(&end_time, &start_time);
8946b74f9b7SNate Lawson     asleep = PM_USEC(end_time) - cx_next->trans_lat;
8956b74f9b7SNate Lawson 
8966b74f9b7SNate Lawson     /* Record statistics */
8976b74f9b7SNate Lawson     if (asleep < cx_next->trans_lat)
8986b74f9b7SNate Lawson 	cpu_cx_stats[cpu_cx_next].short_slp++;
8996b74f9b7SNate Lawson     else
9006b74f9b7SNate Lawson 	cpu_cx_stats[cpu_cx_next].long_slp++;
9016b74f9b7SNate Lawson 
9026b74f9b7SNate Lawson     /*
9036b74f9b7SNate Lawson      * If we slept 100 us or more, use the lowest Cx state.
9046b74f9b7SNate Lawson      * Otherwise, find the lowest state that has a latency less than
9056b74f9b7SNate Lawson      * or equal to the length of our last sleep.
9066b74f9b7SNate Lawson      */
9076b74f9b7SNate Lawson     if (asleep >= 100)
9086b74f9b7SNate Lawson 	cpu_cx_next = cpu_cx_lowest;
9096b74f9b7SNate Lawson     else {
9106b74f9b7SNate Lawson 	for (i = cpu_cx_lowest; i >= 0; i--) {
9116b74f9b7SNate Lawson 	    if (sc->cpu_cx_states[i].trans_lat <= asleep) {
9126b74f9b7SNate Lawson 		cpu_cx_next = i;
9136b74f9b7SNate Lawson 		break;
9146b74f9b7SNate Lawson 	    }
9156b74f9b7SNate Lawson 	}
9166b74f9b7SNate Lawson     }
91756a70eadSNate Lawson 
91856a70eadSNate Lawson #ifdef SMP
91956a70eadSNate Lawson     /* Decrement reference count checked by acpi_cpu_shutdown(). */
92056a70eadSNate Lawson     atomic_subtract_int(&cpu_idle_busy, 1);
92156a70eadSNate Lawson #endif
9226b74f9b7SNate Lawson }
9236b74f9b7SNate Lawson 
9246b74f9b7SNate Lawson /* Put the CPU in C1 in a machine-dependant way. */
9256b74f9b7SNate Lawson static void
9266b74f9b7SNate Lawson acpi_cpu_c1()
9276b74f9b7SNate Lawson {
9286b74f9b7SNate Lawson #ifdef __ia64__
9296b74f9b7SNate Lawson     ia64_call_pal_static(PAL_HALT_LIGHT, 0, 0, 0);
9306b74f9b7SNate Lawson #else
9316b74f9b7SNate Lawson     __asm __volatile("sti; hlt");
9326b74f9b7SNate Lawson #endif
9336b74f9b7SNate Lawson }
9346b74f9b7SNate Lawson 
9356b74f9b7SNate Lawson /* Find the difference between two PM tick counts. */
9366b74f9b7SNate Lawson static void
9376b74f9b7SNate Lawson acpi_pm_ticksub(uint32_t *end, const uint32_t *start)
9386b74f9b7SNate Lawson {
9396b74f9b7SNate Lawson     if (*end >= *start)
9406b74f9b7SNate Lawson 	*end = *end - *start;
9416b74f9b7SNate Lawson     else if (AcpiGbl_FADT->TmrValExt == 0)
9426b74f9b7SNate Lawson 	*end = (((0x00FFFFFF - *start) + *end + 1) & 0x00FFFFFF);
9436b74f9b7SNate Lawson     else
9446b74f9b7SNate Lawson 	*end = ((0xFFFFFFFF - *start) + *end + 1);
9456b74f9b7SNate Lawson }
9466b74f9b7SNate Lawson 
9476b74f9b7SNate Lawson /*
9486b74f9b7SNate Lawson  * Re-evaluate the _PSS and _CST objects when we are notified that they
9496b74f9b7SNate Lawson  * have changed.
95056a70eadSNate Lawson  *
9516b74f9b7SNate Lawson  * XXX Re-evaluation disabled until locking is done.
9526b74f9b7SNate Lawson  */
9536b74f9b7SNate Lawson static void
9546b74f9b7SNate Lawson acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context)
9556b74f9b7SNate Lawson {
9566b74f9b7SNate Lawson     struct acpi_cpu_softc *sc = (struct acpi_cpu_softc *)context;
9576b74f9b7SNate Lawson 
9586b74f9b7SNate Lawson     switch (notify) {
9596b74f9b7SNate Lawson     case ACPI_CPU_NOTIFY_PERF_STATES:
9606b74f9b7SNate Lawson 	device_printf(sc->cpu_dev, "Performance states changed\n");
9616b74f9b7SNate Lawson 	/* acpi_cpu_px_available(sc); */
9626b74f9b7SNate Lawson 	break;
9636b74f9b7SNate Lawson     case ACPI_CPU_NOTIFY_CX_STATES:
9646b74f9b7SNate Lawson 	device_printf(sc->cpu_dev, "Cx states changed\n");
9656b74f9b7SNate Lawson 	/* acpi_cpu_cx_cst(sc); */
9666b74f9b7SNate Lawson 	break;
9676b74f9b7SNate Lawson     default:
9686b74f9b7SNate Lawson 	device_printf(sc->cpu_dev, "Unknown notify %#x\n", notify);
9696b74f9b7SNate Lawson 	break;
9706b74f9b7SNate Lawson     }
9716b74f9b7SNate Lawson }
9726b74f9b7SNate Lawson 
9736b74f9b7SNate Lawson static int
9746b74f9b7SNate Lawson acpi_cpu_quirks(struct acpi_cpu_softc *sc)
9756b74f9b7SNate Lawson {
9766b74f9b7SNate Lawson 
9776b74f9b7SNate Lawson #ifdef SMP
9786b74f9b7SNate Lawson     /*
9796b74f9b7SNate Lawson      * C3 is not supported on multiple CPUs since this would require
9806b74f9b7SNate Lawson      * flushing all caches which is currently too expensive.
9816b74f9b7SNate Lawson      */
9826b74f9b7SNate Lawson     if (mp_ncpus > 1)
9836b74f9b7SNate Lawson 	cpu_quirks |= CPU_QUIRK_NO_C3;
9846b74f9b7SNate Lawson #endif
9856b74f9b7SNate Lawson 
9866b74f9b7SNate Lawson #ifdef notyet
9876b74f9b7SNate Lawson     /* Look for various quirks of the PIIX4 part. */
9886b74f9b7SNate Lawson     acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
9896b74f9b7SNate Lawson     if (acpi_dev != NULL) {
9906b74f9b7SNate Lawson 	switch (pci_get_revid(acpi_dev)) {
9916b74f9b7SNate Lawson 	/*
9926b74f9b7SNate Lawson 	 * Disable throttling control on PIIX4 A and B-step.
9936b74f9b7SNate Lawson 	 * See specification changes #13 ("Manual Throttle Duty Cycle")
9946b74f9b7SNate Lawson 	 * and #14 ("Enabling and Disabling Manual Throttle"), plus
9956b74f9b7SNate Lawson 	 * erratum #5 ("STPCLK# Deassertion Time") from the January
9966b74f9b7SNate Lawson 	 * 2002 PIIX4 specification update.  Note that few (if any)
9976b74f9b7SNate Lawson 	 * mobile systems ever used this part.
9986b74f9b7SNate Lawson 	 */
9996b74f9b7SNate Lawson 	case PCI_REVISION_A_STEP:
10006b74f9b7SNate Lawson 	case PCI_REVISION_B_STEP:
10016b74f9b7SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_THROTTLE;
10026b74f9b7SNate Lawson 	    /* FALLTHROUGH */
10036b74f9b7SNate Lawson 	/*
10046b74f9b7SNate Lawson 	 * Disable C3 support for all PIIX4 chipsets.  Some of these parts
10056b74f9b7SNate Lawson 	 * do not report the BMIDE status to the BM status register and
10066b74f9b7SNate Lawson 	 * others have a livelock bug if Type-F DMA is enabled.  Linux
10076b74f9b7SNate Lawson 	 * works around the BMIDE bug by reading the BM status directly
10086b74f9b7SNate Lawson 	 * but we take the simpler approach of disabling C3 for these
10096b74f9b7SNate Lawson 	 * parts.
10106b74f9b7SNate Lawson 	 *
10116b74f9b7SNate Lawson 	 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
10126b74f9b7SNate Lawson 	 * Livelock") from the January 2002 PIIX4 specification update.
10136b74f9b7SNate Lawson 	 * Applies to all PIIX4 models.
10146b74f9b7SNate Lawson 	 */
10156b74f9b7SNate Lawson 	case PCI_REVISION_4E:
10166b74f9b7SNate Lawson 	case PCI_REVISION_4M:
10176b74f9b7SNate Lawson 	    cpu_quirks |= CPU_QUIRK_NO_C3;
10186b74f9b7SNate Lawson 	    break;
10196b74f9b7SNate Lawson 	default:
10206b74f9b7SNate Lawson 	    break;
10216b74f9b7SNate Lawson 	}
10226b74f9b7SNate Lawson     }
10236b74f9b7SNate Lawson #endif
10246b74f9b7SNate Lawson 
10256b74f9b7SNate Lawson     return (0);
10266b74f9b7SNate Lawson }
10276b74f9b7SNate Lawson 
10286b74f9b7SNate Lawson /*
1029fec754d4SMike Smith  * Power profile change hook.
1030fec754d4SMike Smith  *
1031fec754d4SMike Smith  * Uses the ACPI lock to avoid reentrancy.
1032fec754d4SMike Smith  */
1033fec754d4SMike Smith static void
1034899ccf54SMitsuru IWASAKI acpi_cpu_power_profile(void *arg)
1035fec754d4SMike Smith {
1036899ccf54SMitsuru IWASAKI     int		state;
10376b74f9b7SNate Lawson     uint32_t	new;
1038fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
1039fec754d4SMike Smith 
1040899ccf54SMitsuru IWASAKI     state = power_profile_get_state();
1041be2b1797SNate Lawson     if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
1042899ccf54SMitsuru IWASAKI 	return;
1043899ccf54SMitsuru IWASAKI 
1044fec754d4SMike Smith     ACPI_LOCK;
1045fec754d4SMike Smith 
1046899ccf54SMitsuru IWASAKI     switch (state) {
1047899ccf54SMitsuru IWASAKI     case POWER_PROFILE_PERFORMANCE:
1048899ccf54SMitsuru IWASAKI 	new = cpu_performance_state;
1049899ccf54SMitsuru IWASAKI 	break;
1050899ccf54SMitsuru IWASAKI     case POWER_PROFILE_ECONOMY:
1051899ccf54SMitsuru IWASAKI 	new = cpu_economy_state;
1052899ccf54SMitsuru IWASAKI 	break;
1053899ccf54SMitsuru IWASAKI     default:
1054899ccf54SMitsuru IWASAKI 	new = cpu_current_state;
1055899ccf54SMitsuru IWASAKI 	break;
1056899ccf54SMitsuru IWASAKI     }
1057899ccf54SMitsuru IWASAKI 
1058fec754d4SMike Smith     if (cpu_current_state != new)
10596b74f9b7SNate Lawson 	acpi_cpu_throttle_set(new);
1060fec754d4SMike Smith 
1061fec754d4SMike Smith     ACPI_UNLOCK;
1062fec754d4SMike Smith }
1063fec754d4SMike Smith 
1064fec754d4SMike Smith /*
1065fec754d4SMike Smith  * Handle changes in the performance/ecomony CPU settings.
1066fec754d4SMike Smith  *
1067fec754d4SMike Smith  * Does not need the ACPI lock (although setting *argp should
1068fec754d4SMike Smith  * probably be atomic).
1069fec754d4SMike Smith  */
1070fec754d4SMike Smith static int
10716b74f9b7SNate Lawson acpi_cpu_throttle_sysctl(SYSCTL_HANDLER_ARGS)
1072fec754d4SMike Smith {
10736b74f9b7SNate Lawson     uint32_t	*argp;
10746b74f9b7SNate Lawson     uint32_t	 arg;
1075fec754d4SMike Smith     int		 error;
1076fec754d4SMike Smith 
10776b74f9b7SNate Lawson     argp = (uint32_t *)oidp->oid_arg1;
1078fec754d4SMike Smith     arg = *argp;
1079fec754d4SMike Smith     error = sysctl_handle_int(oidp, &arg, 0, req);
1080fec754d4SMike Smith 
1081be2b1797SNate Lawson     /* Error or no new value */
1082be2b1797SNate Lawson     if (error != 0 || req->newptr == NULL)
1083fec754d4SMike Smith 	return (error);
1084be2b1797SNate Lawson     if (arg < 1 || arg > cpu_max_state)
1085fec754d4SMike Smith 	return (EINVAL);
1086fec754d4SMike Smith 
1087be2b1797SNate Lawson     /* Set new value and possibly switch */
1088fec754d4SMike Smith     *argp = arg;
1089899ccf54SMitsuru IWASAKI     acpi_cpu_power_profile(NULL);
1090fec754d4SMike Smith 
1091fec754d4SMike Smith     return (0);
1092fec754d4SMike Smith }
10936b74f9b7SNate Lawson 
10946b74f9b7SNate Lawson static int
10956b74f9b7SNate Lawson acpi_cpu_history_sysctl(SYSCTL_HANDLER_ARGS)
10966b74f9b7SNate Lawson {
10976b74f9b7SNate Lawson     struct sbuf	 sb;
10986b74f9b7SNate Lawson     char	 buf[128];
10996b74f9b7SNate Lawson     int		 i;
11006b74f9b7SNate Lawson 
11016b74f9b7SNate Lawson     sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
11026b74f9b7SNate Lawson     for (i = 0; i < cpu_cx_count; i++) {
11036b74f9b7SNate Lawson 	sbuf_printf(&sb, "%u/%u ", cpu_cx_stats[i].long_slp,
11046b74f9b7SNate Lawson 		    cpu_cx_stats[i].short_slp);
11056b74f9b7SNate Lawson     }
11066b74f9b7SNate Lawson     sbuf_trim(&sb);
11076b74f9b7SNate Lawson     sbuf_finish(&sb);
11086b74f9b7SNate Lawson     sysctl_handle_string(oidp, sbuf_data(&sb), 0, req);
11096b74f9b7SNate Lawson 
11106b74f9b7SNate Lawson     return (0);
11116b74f9b7SNate Lawson }
11126b74f9b7SNate Lawson 
11136b74f9b7SNate Lawson static int
11146b74f9b7SNate Lawson acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
11156b74f9b7SNate Lawson {
11166b74f9b7SNate Lawson     struct	 acpi_cpu_softc *sc;
11176b74f9b7SNate Lawson     int		 val, error, i;
11186b74f9b7SNate Lawson 
11196b74f9b7SNate Lawson     sc = device_get_softc(cpu_devices[0]);
11206b74f9b7SNate Lawson     val = cpu_cx_lowest;
11216b74f9b7SNate Lawson     error = sysctl_handle_int(oidp, &val, 0, req);
11226b74f9b7SNate Lawson     if (error != 0 || req->newptr == NULL)
11236b74f9b7SNate Lawson 	return (error);
112456a70eadSNate Lawson     if (val < 0 || val > cpu_cx_count - 1)
11256b74f9b7SNate Lawson 	return (EINVAL);
11266b74f9b7SNate Lawson 
11276b74f9b7SNate Lawson     /* Use the new value for the next idle slice. */
11286b74f9b7SNate Lawson     cpu_cx_lowest = val;
11296b74f9b7SNate Lawson     cpu_cx_next = val;
11306b74f9b7SNate Lawson 
11316b74f9b7SNate Lawson     /* If not disabling, cache the new lowest non-C3 state. */
11326b74f9b7SNate Lawson     cpu_non_c3 = 0;
11336b74f9b7SNate Lawson     for (i = cpu_cx_lowest; i >= 0; i--) {
11346b74f9b7SNate Lawson 	if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
11356b74f9b7SNate Lawson 	    cpu_non_c3 = i;
11366b74f9b7SNate Lawson 	    break;
11376b74f9b7SNate Lawson 	}
11386b74f9b7SNate Lawson     }
11396b74f9b7SNate Lawson 
11406b74f9b7SNate Lawson     /* Reset the statistics counters. */
11416b74f9b7SNate Lawson     memset(cpu_cx_stats, 0, sizeof(cpu_cx_stats));
11426b74f9b7SNate Lawson 
11436b74f9b7SNate Lawson     return (0);
11446b74f9b7SNate Lawson }
1145