2 * processor_thermal.c - Passive cooling submodule of the ACPI processor driver
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
7 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8 * - Added processor hotplug support
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/cpufreq.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
36 #include <asm/uaccess.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/processor.h>
40 #include <acpi/acpi_drivers.h>
42 #define ACPI_PROCESSOR_COMPONENT 0x01000000
43 #define ACPI_PROCESSOR_CLASS "processor"
44 #define ACPI_PROCESSOR_DRIVER_NAME "ACPI Processor Driver"
45 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
46 ACPI_MODULE_NAME("acpi_processor")
48 /* --------------------------------------------------------------------------
50 -------------------------------------------------------------------------- */
51 static int acpi_processor_apply_limit(struct acpi_processor *pr)
64 if (pr->flags.throttling) {
65 if (pr->limit.user.tx > tx)
66 tx = pr->limit.user.tx;
67 if (pr->limit.thermal.tx > tx)
68 tx = pr->limit.thermal.tx;
70 result = acpi_processor_set_throttling(pr, tx);
75 pr->limit.state.px = px;
76 pr->limit.state.tx = tx;
78 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
79 "Processor [%d] limit set to (P%d:T%d)\n", pr->id,
80 pr->limit.state.px, pr->limit.state.tx));
84 printk(KERN_ERR PREFIX "Unable to set limit\n");
89 #ifdef CONFIG_CPU_FREQ
91 /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
92 * offers (in most cases) voltage scaling in addition to frequency scaling, and
93 * thus a cubic (instead of linear) reduction of energy. Also, we allow for
94 * _any_ cpufreq driver and not only the acpi-cpufreq driver.
97 static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
98 static unsigned int acpi_thermal_cpufreq_is_init = 0;
100 static int cpu_has_cpufreq(unsigned int cpu)
102 struct cpufreq_policy policy;
103 if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
108 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
110 if (!cpu_has_cpufreq(cpu))
113 if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
114 cpufreq_thermal_reduction_pctg[cpu] += 20;
115 cpufreq_update_policy(cpu);
122 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
124 if (!cpu_has_cpufreq(cpu))
127 if (cpufreq_thermal_reduction_pctg[cpu] > 20)
128 cpufreq_thermal_reduction_pctg[cpu] -= 20;
130 cpufreq_thermal_reduction_pctg[cpu] = 0;
131 cpufreq_update_policy(cpu);
132 /* We reached max freq again and can leave passive mode */
133 return !cpufreq_thermal_reduction_pctg[cpu];
136 static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
137 unsigned long event, void *data)
139 struct cpufreq_policy *policy = data;
140 unsigned long max_freq = 0;
142 if (event != CPUFREQ_ADJUST)
146 (policy->cpuinfo.max_freq *
147 (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
149 cpufreq_verify_within_limits(policy, 0, max_freq);
155 static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
156 .notifier_call = acpi_thermal_cpufreq_notifier,
159 void acpi_thermal_cpufreq_init(void)
163 for (i = 0; i < NR_CPUS; i++)
164 cpufreq_thermal_reduction_pctg[i] = 0;
166 i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
167 CPUFREQ_POLICY_NOTIFIER);
169 acpi_thermal_cpufreq_is_init = 1;
172 void acpi_thermal_cpufreq_exit(void)
174 if (acpi_thermal_cpufreq_is_init)
175 cpufreq_unregister_notifier
176 (&acpi_thermal_cpufreq_notifier_block,
177 CPUFREQ_POLICY_NOTIFIER);
179 acpi_thermal_cpufreq_is_init = 0;
182 #else /* ! CONFIG_CPU_FREQ */
184 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
188 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
195 int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
198 struct acpi_processor *pr = NULL;
199 struct acpi_device *device = NULL;
200 int tx = 0, max_tx_px = 0;
203 if ((type < ACPI_PROCESSOR_LIMIT_NONE)
204 || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
207 result = acpi_bus_get_device(handle, &device);
211 pr = acpi_driver_data(device);
215 /* Thermal limits are always relative to the current Px/Tx state. */
216 if (pr->flags.throttling)
217 pr->limit.thermal.tx = pr->throttling.state;
220 * Our default policy is to only use throttling at the lowest
224 tx = pr->limit.thermal.tx;
228 case ACPI_PROCESSOR_LIMIT_NONE:
230 result = acpi_thermal_cpufreq_decrease(pr->id);
235 case ACPI_PROCESSOR_LIMIT_INCREMENT:
236 /* if going up: P-states first, T-states later */
238 result = acpi_thermal_cpufreq_increase(pr->id);
241 else if (result == -ERANGE)
242 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
243 "At maximum performance state\n"));
245 if (pr->flags.throttling) {
246 if (tx == (pr->throttling.state_count - 1))
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
248 "At maximum throttling state\n"));
254 case ACPI_PROCESSOR_LIMIT_DECREMENT:
255 /* if going down: T-states first, P-states later */
257 if (pr->flags.throttling) {
260 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
261 "At minimum throttling state\n"));
268 result = acpi_thermal_cpufreq_decrease(pr->id);
271 * We only could get -ERANGE, 1 or 0.
272 * In the first two cases we reached max freq again.
274 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
275 "At minimum performance state\n"));
284 if (pr->flags.throttling) {
285 pr->limit.thermal.px = 0;
286 pr->limit.thermal.tx = tx;
288 result = acpi_processor_apply_limit(pr);
290 printk(KERN_ERR PREFIX "Unable to set thermal limit\n");
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
293 pr->limit.thermal.px, pr->limit.thermal.tx));
302 int acpi_processor_get_limit_info(struct acpi_processor *pr)
308 if (pr->flags.throttling)
314 /* /proc interface */
316 static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
318 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
324 if (!pr->flags.limit) {
325 seq_puts(seq, "<not supported>\n");
329 seq_printf(seq, "active limit: P%d:T%d\n"
330 "user limit: P%d:T%d\n"
331 "thermal limit: P%d:T%d\n",
332 pr->limit.state.px, pr->limit.state.tx,
333 pr->limit.user.px, pr->limit.user.tx,
334 pr->limit.thermal.px, pr->limit.thermal.tx);
340 static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
342 return single_open(file, acpi_processor_limit_seq_show,
346 static ssize_t acpi_processor_write_limit(struct file * file,
347 const char __user * buffer,
348 size_t count, loff_t * data)
351 struct seq_file *m = file->private_data;
352 struct acpi_processor *pr = m->private;
353 char limit_string[25] = { '\0' };
358 if (!pr || (count > sizeof(limit_string) - 1)) {
362 if (copy_from_user(limit_string, buffer, count)) {
366 limit_string[count] = '\0';
368 if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
369 printk(KERN_ERR PREFIX "Invalid data format\n");
373 if (pr->flags.throttling) {
374 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
375 printk(KERN_ERR PREFIX "Invalid tx\n");
378 pr->limit.user.tx = tx;
381 result = acpi_processor_apply_limit(pr);
386 struct file_operations acpi_processor_limit_fops = {
387 .open = acpi_processor_limit_open_fs,
389 .write = acpi_processor_write_limit,
391 .release = single_release,