xref: /freebsd/sys/dev/acpica/acpi_cpu.c (revision aad970f1)
1fec754d4SMike Smith /*-
2fec754d4SMike Smith  * Copyright (c) 2001 Michael Smith
3fec754d4SMike Smith  * All rights reserved.
4fec754d4SMike Smith  *
5fec754d4SMike Smith  * Redistribution and use in source and binary forms, with or without
6fec754d4SMike Smith  * modification, are permitted provided that the following conditions
7fec754d4SMike Smith  * are met:
8fec754d4SMike Smith  * 1. Redistributions of source code must retain the above copyright
9fec754d4SMike Smith  *    notice, this list of conditions and the following disclaimer.
10fec754d4SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
11fec754d4SMike Smith  *    notice, this list of conditions and the following disclaimer in the
12fec754d4SMike Smith  *    documentation and/or other materials provided with the distribution.
13fec754d4SMike Smith  *
14fec754d4SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fec754d4SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fec754d4SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fec754d4SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18fec754d4SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fec754d4SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fec754d4SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fec754d4SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fec754d4SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fec754d4SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fec754d4SMike Smith  * SUCH DAMAGE.
25fec754d4SMike Smith  */
26fec754d4SMike Smith 
27aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
28aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
29aad970f1SDavid E. O'Brien 
30fec754d4SMike Smith #include "opt_acpi.h"
31fec754d4SMike Smith #include <sys/param.h>
32fec754d4SMike Smith #include <sys/kernel.h>
33fec754d4SMike Smith #include <sys/bus.h>
34899ccf54SMitsuru IWASAKI #include <sys/power.h>
35fec754d4SMike Smith 
36fec754d4SMike Smith #include <machine/bus_pio.h>
37fec754d4SMike Smith #include <machine/bus.h>
38fec754d4SMike Smith #include <machine/resource.h>
39fec754d4SMike Smith #include <sys/rman.h>
40fec754d4SMike Smith 
41fec754d4SMike Smith #include "acpi.h"
42fec754d4SMike Smith 
43fec754d4SMike Smith #include <dev/acpica/acpivar.h>
44fec754d4SMike Smith 
45fec754d4SMike Smith /*
46fec754d4SMike Smith  * Support for ACPI Processor devices.
47fec754d4SMike Smith  *
48fec754d4SMike Smith  * Note that this only provides ACPI 1.0 support (with the exception of the
49fec754d4SMike Smith  * PSTATE_CNT field).  2.0 support will involve implementing _PTC, _PCT,
50fec754d4SMike Smith  * _PSS and _PPC.
51fec754d4SMike Smith  */
52fec754d4SMike Smith 
53fec754d4SMike Smith /*
54fec754d4SMike Smith  * Hooks for the ACPI CA debugging infrastructure
55fec754d4SMike Smith  */
56fec754d4SMike Smith #define _COMPONENT	ACPI_PROCESSOR
579127281cSMike Smith ACPI_MODULE_NAME("PROCESSOR")
58fec754d4SMike Smith 
59fec754d4SMike Smith struct acpi_cpu_softc {
60fec754d4SMike Smith     device_t		cpu_dev;
61fec754d4SMike Smith     ACPI_HANDLE		cpu_handle;
62fec754d4SMike Smith 
63fec754d4SMike Smith     u_int32_t		cpu_id;
64fec754d4SMike Smith 
65fec754d4SMike Smith     /* CPU throttling control register */
66fec754d4SMike Smith     struct resource	*cpu_p_blk;
67fec754d4SMike Smith #define CPU_GET_P_CNT(sc)	(bus_space_read_4(rman_get_bustag((sc)->cpu_p_blk), 	\
68fec754d4SMike Smith 						  rman_get_bushandle((sc)->cpu_p_blk),	\
69fec754d4SMike Smith 						  0))
70fec754d4SMike Smith #define CPU_SET_P_CNT(sc, val)	(bus_space_write_4(rman_get_bustag((sc)->cpu_p_blk), 	\
71fec754d4SMike Smith 						  rman_get_bushandle((sc)->cpu_p_blk),	\
72fec754d4SMike Smith 						  0, (val)))
73fec754d4SMike Smith #define CPU_P_CNT_THT_EN	(1<<4)
74fec754d4SMike Smith };
75fec754d4SMike Smith 
76fec754d4SMike Smith /*
77fec754d4SMike Smith  * Speeds are stored in counts, from 1 - CPU_MAX_SPEED, and
78fec754d4SMike Smith  * reported to the user in tenths of a percent.
79fec754d4SMike Smith  */
80fec754d4SMike Smith static u_int32_t	cpu_duty_offset;
81fec754d4SMike Smith static u_int32_t	cpu_duty_width;
82fec754d4SMike Smith #define CPU_MAX_SPEED		(1 << cpu_duty_width)
83fec754d4SMike Smith #define CPU_SPEED_PERCENT(x)	((1000 * (x)) / CPU_MAX_SPEED)
84fec754d4SMike Smith #define CPU_SPEED_PRINTABLE(x)	(CPU_SPEED_PERCENT(x) / 10),(CPU_SPEED_PERCENT(x) % 10)
85fec754d4SMike Smith 
86fec754d4SMike Smith static u_int32_t	cpu_smi_cmd;	/* should be a generic way to do this */
87fec754d4SMike Smith static u_int8_t		cpu_pstate_cnt;
88fec754d4SMike Smith 
89fec754d4SMike Smith static u_int32_t	cpu_current_state;
90fec754d4SMike Smith static u_int32_t	cpu_performance_state;
91fec754d4SMike Smith static u_int32_t	cpu_economy_state;
92fec754d4SMike Smith static u_int32_t	cpu_max_state;
93fec754d4SMike Smith 
94fec754d4SMike Smith static device_t		*cpu_devices;
95fec754d4SMike Smith static int		cpu_ndevices;
96fec754d4SMike Smith 
97fec754d4SMike Smith static struct sysctl_ctx_list	acpi_cpu_sysctl_ctx;
98fec754d4SMike Smith static struct sysctl_oid	*acpi_cpu_sysctl_tree;
99fec754d4SMike Smith 
100fec754d4SMike Smith static int	acpi_cpu_probe(device_t dev);
101fec754d4SMike Smith static int	acpi_cpu_attach(device_t dev);
102fec754d4SMike Smith static void	acpi_cpu_init_throttling(void *arg);
103fec754d4SMike Smith static void	acpi_cpu_set_speed(u_int32_t speed);
104899ccf54SMitsuru IWASAKI static void	acpi_cpu_power_profile(void *arg);
105fec754d4SMike Smith static int	acpi_cpu_speed_sysctl(SYSCTL_HANDLER_ARGS);
106fec754d4SMike Smith 
107fec754d4SMike Smith static device_method_t acpi_cpu_methods[] = {
108fec754d4SMike Smith     /* Device interface */
109fec754d4SMike Smith     DEVMETHOD(device_probe,	acpi_cpu_probe),
110fec754d4SMike Smith     DEVMETHOD(device_attach,	acpi_cpu_attach),
111fec754d4SMike Smith 
112fec754d4SMike Smith     {0, 0}
113fec754d4SMike Smith };
114fec754d4SMike Smith 
115fec754d4SMike Smith static driver_t acpi_cpu_driver = {
116fec754d4SMike Smith     "acpi_cpu",
117fec754d4SMike Smith     acpi_cpu_methods,
118fec754d4SMike Smith     sizeof(struct acpi_cpu_softc),
119fec754d4SMike Smith };
120fec754d4SMike Smith 
1213273b005SMike Smith static devclass_t acpi_cpu_devclass;
122fec754d4SMike Smith DRIVER_MODULE(acpi_cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0);
123fec754d4SMike Smith 
124fec754d4SMike Smith static int
125fec754d4SMike Smith acpi_cpu_probe(device_t dev)
126fec754d4SMike Smith {
127f48bf2d7SMike Smith     if (!acpi_disabled("cpu") &&
128f48bf2d7SMike Smith 	(acpi_get_type(dev) == ACPI_TYPE_PROCESSOR)) {
129fec754d4SMike Smith 	device_set_desc(dev, "CPU");	/* XXX get more verbose description? */
130fec754d4SMike Smith 	return(0);
131fec754d4SMike Smith     }
132fec754d4SMike Smith     return(ENXIO);
133fec754d4SMike Smith }
134fec754d4SMike Smith 
135fec754d4SMike Smith static int
136fec754d4SMike Smith acpi_cpu_attach(device_t dev)
137fec754d4SMike Smith {
138fec754d4SMike Smith     struct acpi_cpu_softc	*sc;
139fec754d4SMike Smith     struct acpi_softc		*acpi_sc;
140fec754d4SMike Smith     ACPI_OBJECT			processor;
141fec754d4SMike Smith     ACPI_BUFFER			buf;
142fec754d4SMike Smith     ACPI_STATUS			status;
143fec754d4SMike Smith     u_int32_t			p_blk;
144fec754d4SMike Smith     u_int32_t			p_blk_length;
145fec754d4SMike Smith     u_int32_t			duty_end;
146fec754d4SMike Smith     int				rid;
147fec754d4SMike Smith 
148b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
149fec754d4SMike Smith 
150fec754d4SMike Smith     ACPI_ASSERTLOCK;
151fec754d4SMike Smith 
152fec754d4SMike Smith     sc = device_get_softc(dev);
153fec754d4SMike Smith     sc->cpu_dev = dev;
154fec754d4SMike Smith     sc->cpu_handle = acpi_get_handle(dev);
155fec754d4SMike Smith 
156fec754d4SMike Smith     /*
157fec754d4SMike Smith      * Get global parameters from the FADT.
158fec754d4SMike Smith      */
159fec754d4SMike Smith     if (device_get_unit(sc->cpu_dev) == 0) {
160ad5dc75bSMike Smith 	cpu_duty_offset = AcpiGbl_FADT->DutyOffset;
161ad5dc75bSMike Smith 	cpu_duty_width = AcpiGbl_FADT->DutyWidth;
162ad5dc75bSMike Smith 	cpu_smi_cmd = AcpiGbl_FADT->SmiCmd;
163ad5dc75bSMike Smith 	cpu_pstate_cnt = AcpiGbl_FADT->PstateCnt;
164fec754d4SMike Smith 
165fec754d4SMike Smith 	/* validate the offset/width */
1663e759f36SMike Smith 	if (cpu_duty_width > 0) {
167fec754d4SMike Smith 		duty_end = cpu_duty_offset + cpu_duty_width - 1;
168fec754d4SMike Smith 		/* check that it fits */
169fec754d4SMike Smith 		if (duty_end > 31) {
170fec754d4SMike Smith 			printf("acpi_cpu: CLK_VAL field overflows P_CNT register\n");
171fec754d4SMike Smith 			cpu_duty_width = 0;
172fec754d4SMike Smith 		}
173fec754d4SMike Smith 		/* check for overlap with the THT_EN bit */
174fec754d4SMike Smith 		if ((cpu_duty_offset <= 4) && (duty_end >= 4)) {
175fec754d4SMike Smith 			printf("acpi_cpu: CLK_VAL field overlaps THT_EN bit\n");
176fec754d4SMike Smith 			cpu_duty_width = 0;
177fec754d4SMike Smith 		}
1783e759f36SMike Smith 	}
179fec754d4SMike Smith 
180fec754d4SMike Smith 	/*
181fec754d4SMike Smith 	 * Start the throttling process once the probe phase completes, if we think that
182fec754d4SMike Smith 	 * it's going to be useful.  If the duty width value is zero, there are no significant
183fec754d4SMike Smith 	 * bits in the register and thus no throttled states.
184fec754d4SMike Smith 	 */
185fec754d4SMike Smith 	if (cpu_duty_width > 0) {
186fec754d4SMike Smith 	    AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cpu_init_throttling, NULL);
187fec754d4SMike Smith 
188fec754d4SMike Smith 	    acpi_sc = acpi_device_get_parent_softc(dev);
189fec754d4SMike Smith 	    sysctl_ctx_init(&acpi_cpu_sysctl_ctx);
190fec754d4SMike Smith 	    acpi_cpu_sysctl_tree = SYSCTL_ADD_NODE(&acpi_cpu_sysctl_ctx,
191fec754d4SMike Smith 						  SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
192fec754d4SMike Smith 						  OID_AUTO, "cpu", CTLFLAG_RD, 0, "");
193fec754d4SMike Smith 
194fec754d4SMike Smith 	    SYSCTL_ADD_INT(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
195fec754d4SMike Smith 			   OID_AUTO, "max_speed", CTLFLAG_RD,
196fec754d4SMike Smith 			   &cpu_max_state, 0, "maximum CPU speed");
197fec754d4SMike Smith 	    SYSCTL_ADD_INT(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
198fec754d4SMike Smith 			   OID_AUTO, "current_speed", CTLFLAG_RD,
199fec754d4SMike Smith 			   &cpu_current_state, 0, "current CPU speed");
200fec754d4SMike Smith 	    SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
201fec754d4SMike Smith 			    OID_AUTO, "performance_speed", CTLTYPE_INT | CTLFLAG_RW,
202fec754d4SMike Smith 			    &cpu_performance_state, 0, acpi_cpu_speed_sysctl, "I", "");
203fec754d4SMike Smith 	    SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree),
204fec754d4SMike Smith 			    OID_AUTO, "economy_speed", CTLTYPE_INT | CTLFLAG_RW,
205fec754d4SMike Smith 			    &cpu_economy_state, 0, acpi_cpu_speed_sysctl, "I", "");
206fec754d4SMike Smith 	}
207fec754d4SMike Smith     }
208fec754d4SMike Smith 
209fec754d4SMike Smith     /*
210fec754d4SMike Smith      * Get the processor object.
211fec754d4SMike Smith      */
212fec754d4SMike Smith     buf.Pointer = &processor;
213fec754d4SMike Smith     buf.Length = sizeof(processor);
214fec754d4SMike Smith     if (ACPI_FAILURE(status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf))) {
215bfae45aaSMike Smith 	device_printf(sc->cpu_dev, "couldn't get Processor object - %s\n", AcpiFormatException(status));
216fec754d4SMike Smith 	return_VALUE(ENXIO);
217fec754d4SMike Smith     }
218fec754d4SMike Smith     if (processor.Type != ACPI_TYPE_PROCESSOR) {
219fec754d4SMike Smith 	device_printf(sc->cpu_dev, "Processor object has bad type %d\n", processor.Type);
220fec754d4SMike Smith 	return_VALUE(ENXIO);
221fec754d4SMike Smith     }
222fec754d4SMike Smith     sc->cpu_id = processor.Processor.ProcId;
223fec754d4SMike Smith 
224fec754d4SMike Smith     /*
225fec754d4SMike Smith      * If it looks like we support throttling, find this CPU's P_BLK.
226fec754d4SMike Smith      *
227fec754d4SMike Smith      * Note that some systems seem to duplicate the P_BLK pointer across
228fec754d4SMike Smith      * multiple CPUs, so not getting the resource is not fatal.
229fec754d4SMike Smith      *
230fec754d4SMike Smith      * XXX should support _PTC here as well, once we work out how to parse it.
231fec754d4SMike Smith      *
232fec754d4SMike Smith      * XXX is it valid to assume that the P_BLK must be 6 bytes long?
233fec754d4SMike Smith      */
234fec754d4SMike Smith     if (cpu_duty_width > 0) {
235fec754d4SMike Smith 	p_blk = processor.Processor.PblkAddress;
236fec754d4SMike Smith 	p_blk_length = processor.Processor.PblkLength;
237fec754d4SMike Smith 
238fec754d4SMike Smith 	/* allocate bus space if possible */
239fec754d4SMike Smith 	if ((p_blk > 0) && (p_blk_length == 6)) {
240fec754d4SMike Smith 	    rid = 0;
241fec754d4SMike Smith 	    bus_set_resource(sc->cpu_dev, SYS_RES_IOPORT, rid, p_blk, p_blk_length);
242fec754d4SMike Smith 	    sc->cpu_p_blk = bus_alloc_resource(sc->cpu_dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
243fec754d4SMike Smith 					       RF_ACTIVE);
244fec754d4SMike Smith 
2454c1cdee6SMike Smith 	    ACPI_DEBUG_PRINT((ACPI_DB_IO, "acpi_cpu%d: throttling with P_BLK at 0x%x/%d%s\n",
246fec754d4SMike Smith 			      device_get_unit(sc->cpu_dev), p_blk, p_blk_length,
247f0987736SMitsuru IWASAKI 			      sc->cpu_p_blk ? "" : " (shadowed)"));
248fec754d4SMike Smith 	}
249fec754d4SMike Smith     }
250fec754d4SMike Smith     return_VALUE(0);
251fec754d4SMike Smith }
252fec754d4SMike Smith 
253fec754d4SMike Smith /*
254fec754d4SMike Smith  * Call this *after* all CPUs have been attached.
255fec754d4SMike Smith  *
256fec754d4SMike Smith  * Takes the ACPI lock to avoid fighting anyone over the SMI command
257fec754d4SMike Smith  * port.  Could probably lock less code.
258fec754d4SMike Smith  */
259fec754d4SMike Smith static void
260fec754d4SMike Smith acpi_cpu_init_throttling(void *arg)
261fec754d4SMike Smith {
262e5e5b51fSJohn Baldwin     int cpu_temp_speed;
263fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
264fec754d4SMike Smith 
265fec754d4SMike Smith     ACPI_LOCK;
266fec754d4SMike Smith 
267fec754d4SMike Smith     /* get set of CPU devices */
268fec754d4SMike Smith     devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
269fec754d4SMike Smith 
270fec754d4SMike Smith     /* initialise throttling states */
271fec754d4SMike Smith     cpu_max_state = CPU_MAX_SPEED;
272fec754d4SMike Smith     cpu_performance_state = cpu_max_state;
273fec754d4SMike Smith     cpu_economy_state = cpu_performance_state / 2;
274fec754d4SMike Smith     if (cpu_economy_state == 0)		/* 0 is 'reserved' */
275fec754d4SMike Smith 	cpu_economy_state++;
276e5e5b51fSJohn Baldwin     if (TUNABLE_INT_FETCH("hw.acpi.cpu.performance_speed",
277e5e5b51fSJohn Baldwin 	&cpu_temp_speed) && cpu_temp_speed > 0 &&
278e5e5b51fSJohn Baldwin 	cpu_temp_speed <= cpu_max_state)
279e5e5b51fSJohn Baldwin 	cpu_performance_state = cpu_temp_speed;
280e5e5b51fSJohn Baldwin     if (TUNABLE_INT_FETCH("hw.acpi.cpu.economy_speed",
281e5e5b51fSJohn Baldwin 	&cpu_temp_speed) && cpu_temp_speed > 0 &&
282e5e5b51fSJohn Baldwin 	cpu_temp_speed <= cpu_max_state)
283e5e5b51fSJohn Baldwin 	cpu_economy_state = cpu_temp_speed;
284fec754d4SMike Smith 
285fec754d4SMike Smith     /* register performance profile change handler */
286899ccf54SMitsuru IWASAKI     EVENTHANDLER_REGISTER(power_profile_change, acpi_cpu_power_profile, NULL, 0);
287fec754d4SMike Smith 
288fec754d4SMike Smith     /* if ACPI 2.0+, signal platform that we are taking over throttling */
289fec754d4SMike Smith     if (cpu_pstate_cnt != 0) {
290fec754d4SMike Smith 	/* XXX should be a generic interface for this */
291ad5dc75bSMike Smith 	AcpiOsWritePort(cpu_smi_cmd, cpu_pstate_cnt, 8);
292fec754d4SMike Smith     }
293fec754d4SMike Smith 
294fec754d4SMike Smith     ACPI_UNLOCK;
295fec754d4SMike Smith 
296fec754d4SMike Smith     /* set initial speed */
297899ccf54SMitsuru IWASAKI     acpi_cpu_power_profile(NULL);
298fec754d4SMike Smith 
299a40f20c7SNate Lawson     printf("acpi_cpu: throttling enabled, %d steps (100%% to %d.%d%%), "
300a40f20c7SNate Lawson 	   "currently %d.%d%%\n", CPU_MAX_SPEED, CPU_SPEED_PRINTABLE(1),
301a40f20c7SNate Lawson 	   CPU_SPEED_PRINTABLE(cpu_current_state));
302fec754d4SMike Smith }
303fec754d4SMike Smith 
304fec754d4SMike Smith /*
305fec754d4SMike Smith  * Set CPUs to the new state.
306fec754d4SMike Smith  *
307fec754d4SMike Smith  * Must be called with the ACPI lock held.
308fec754d4SMike Smith  */
309fec754d4SMike Smith static void
310fec754d4SMike Smith acpi_cpu_set_speed(u_int32_t speed)
311fec754d4SMike Smith {
312fec754d4SMike Smith     struct acpi_cpu_softc	*sc;
313fec754d4SMike Smith     int				i;
314fec754d4SMike Smith     u_int32_t			p_cnt, clk_val;
315fec754d4SMike Smith 
316fec754d4SMike Smith     ACPI_ASSERTLOCK;
317fec754d4SMike Smith 
318fec754d4SMike Smith     /* iterate over processors */
319fec754d4SMike Smith     for (i = 0; i < cpu_ndevices; i++) {
320fec754d4SMike Smith 	sc = device_get_softc(cpu_devices[i]);
321fec754d4SMike Smith 	if (sc->cpu_p_blk == NULL)
322fec754d4SMike Smith 	    continue;
323fec754d4SMike Smith 
324fec754d4SMike Smith 	/* get the current P_CNT value and disable throttling */
325fec754d4SMike Smith 	p_cnt = CPU_GET_P_CNT(sc);
326fec754d4SMike Smith 	p_cnt &= ~CPU_P_CNT_THT_EN;
327fec754d4SMike Smith 	CPU_SET_P_CNT(sc, p_cnt);
328fec754d4SMike Smith 
329fec754d4SMike Smith 	/* if we're at maximum speed, that's all */
330fec754d4SMike Smith 	if (speed < CPU_MAX_SPEED) {
331fec754d4SMike Smith 
332fec754d4SMike Smith 	    /* mask the old CLK_VAL off and or-in the new value */
333fec754d4SMike Smith 	    clk_val = CPU_MAX_SPEED << cpu_duty_offset;
334fec754d4SMike Smith 	    p_cnt &= ~clk_val;
335fec754d4SMike Smith 	    p_cnt |= (speed << cpu_duty_offset);
336fec754d4SMike Smith 
337fec754d4SMike Smith 	    /* write the new P_CNT value and then enable throttling */
338fec754d4SMike Smith 	    CPU_SET_P_CNT(sc, p_cnt);
339fec754d4SMike Smith 	    p_cnt |= CPU_P_CNT_THT_EN;
340fec754d4SMike Smith 	    CPU_SET_P_CNT(sc, p_cnt);
341fec754d4SMike Smith 	}
3426971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->cpu_dev, acpi_device_get_parent_softc(sc->cpu_dev),
3436971b3c7SMitsuru IWASAKI 	    "set speed to %d.%d%%\n", CPU_SPEED_PRINTABLE(speed));
344fec754d4SMike Smith     }
345fec754d4SMike Smith     cpu_current_state = speed;
346fec754d4SMike Smith }
347fec754d4SMike Smith 
348fec754d4SMike Smith /*
349fec754d4SMike Smith  * Power profile change hook.
350fec754d4SMike Smith  *
351fec754d4SMike Smith  * Uses the ACPI lock to avoid reentrancy.
352fec754d4SMike Smith  */
353fec754d4SMike Smith static void
354899ccf54SMitsuru IWASAKI acpi_cpu_power_profile(void *arg)
355fec754d4SMike Smith {
356899ccf54SMitsuru IWASAKI     int		state;
357fec754d4SMike Smith     u_int32_t	new;
358fc0ea94aSJohn Baldwin     ACPI_LOCK_DECL;
359fec754d4SMike Smith 
360899ccf54SMitsuru IWASAKI     state = power_profile_get_state();
361899ccf54SMitsuru IWASAKI     if (state != POWER_PROFILE_PERFORMANCE &&
362899ccf54SMitsuru IWASAKI         state != POWER_PROFILE_ECONOMY) {
363899ccf54SMitsuru IWASAKI 	return;
364899ccf54SMitsuru IWASAKI     }
365899ccf54SMitsuru IWASAKI 
366fec754d4SMike Smith     ACPI_LOCK;
367fec754d4SMike Smith 
368899ccf54SMitsuru IWASAKI     switch (state) {
369899ccf54SMitsuru IWASAKI     case POWER_PROFILE_PERFORMANCE:
370899ccf54SMitsuru IWASAKI 	new = cpu_performance_state;
371899ccf54SMitsuru IWASAKI 	break;
372899ccf54SMitsuru IWASAKI     case POWER_PROFILE_ECONOMY:
373899ccf54SMitsuru IWASAKI 	new = cpu_economy_state;
374899ccf54SMitsuru IWASAKI 	break;
375899ccf54SMitsuru IWASAKI     default:
376899ccf54SMitsuru IWASAKI 	new = cpu_current_state;
377899ccf54SMitsuru IWASAKI 	break;
378899ccf54SMitsuru IWASAKI     }
379899ccf54SMitsuru IWASAKI 
380fec754d4SMike Smith     if (cpu_current_state != new)
381fec754d4SMike Smith 	acpi_cpu_set_speed(new);
382fec754d4SMike Smith 
383fec754d4SMike Smith     ACPI_UNLOCK;
384fec754d4SMike Smith }
385fec754d4SMike Smith 
386fec754d4SMike Smith /*
387fec754d4SMike Smith  * Handle changes in the performance/ecomony CPU settings.
388fec754d4SMike Smith  *
389fec754d4SMike Smith  * Does not need the ACPI lock (although setting *argp should
390fec754d4SMike Smith  * probably be atomic).
391fec754d4SMike Smith  */
392fec754d4SMike Smith static int
393fec754d4SMike Smith acpi_cpu_speed_sysctl(SYSCTL_HANDLER_ARGS)
394fec754d4SMike Smith {
395fec754d4SMike Smith     u_int32_t	*argp;
396fec754d4SMike Smith     u_int32_t	arg;
397fec754d4SMike Smith     int		error;
398fec754d4SMike Smith 
399fec754d4SMike Smith     argp = (u_int32_t *)oidp->oid_arg1;
400fec754d4SMike Smith     arg = *argp;
401fec754d4SMike Smith     error = sysctl_handle_int(oidp, &arg, 0, req);
402fec754d4SMike Smith 
403fec754d4SMike Smith     /* error or no new value */
404fec754d4SMike Smith     if ((error != 0) || (req->newptr == NULL))
405fec754d4SMike Smith 	return(error);
406fec754d4SMike Smith 
407fec754d4SMike Smith     /* range check */
408f0987736SMitsuru IWASAKI     if ((arg < 1) || (arg > cpu_max_state))
409fec754d4SMike Smith 	return(EINVAL);
410fec754d4SMike Smith 
411fec754d4SMike Smith     /* set new value and possibly switch */
412fec754d4SMike Smith     *argp = arg;
413899ccf54SMitsuru IWASAKI     acpi_cpu_power_profile(NULL);
414fec754d4SMike Smith 
415fec754d4SMike Smith     return(0);
416fec754d4SMike Smith }
417