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>
35 #include <linux/sysdev.h>
37 #include <asm/uaccess.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/processor.h>
41 #include <acpi/acpi_drivers.h>
43 #define ACPI_PROCESSOR_COMPONENT 0x01000000
44 #define ACPI_PROCESSOR_CLASS "processor"
45 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
46 ACPI_MODULE_NAME("processor_thermal");
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 #define CPUFREQ_THERMAL_MIN_STEP 0
98 #define CPUFREQ_THERMAL_MAX_STEP 3
100 static DEFINE_PER_CPU(unsigned int, cpufreq_thermal_reduction_pctg);
101 static unsigned int acpi_thermal_cpufreq_is_init = 0;
103 static int cpu_has_cpufreq(unsigned int cpu)
105 struct cpufreq_policy policy;
106 if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
111 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
113 if (!cpu_has_cpufreq(cpu))
116 if (per_cpu(cpufreq_thermal_reduction_pctg, cpu) <
117 CPUFREQ_THERMAL_MAX_STEP) {
118 per_cpu(cpufreq_thermal_reduction_pctg, cpu)++;
119 cpufreq_update_policy(cpu);
126 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
128 if (!cpu_has_cpufreq(cpu))
131 if (per_cpu(cpufreq_thermal_reduction_pctg, cpu) >
132 (CPUFREQ_THERMAL_MIN_STEP + 1))
133 per_cpu(cpufreq_thermal_reduction_pctg, cpu)--;
135 per_cpu(cpufreq_thermal_reduction_pctg, cpu) = 0;
136 cpufreq_update_policy(cpu);
137 /* We reached max freq again and can leave passive mode */
138 return !per_cpu(cpufreq_thermal_reduction_pctg, cpu);
141 static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
142 unsigned long event, void *data)
144 struct cpufreq_policy *policy = data;
145 unsigned long max_freq = 0;
147 if (event != CPUFREQ_ADJUST)
151 policy->cpuinfo.max_freq *
152 (100 - per_cpu(cpufreq_thermal_reduction_pctg, policy->cpu) * 20)
155 cpufreq_verify_within_limits(policy, 0, max_freq);
161 static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
162 .notifier_call = acpi_thermal_cpufreq_notifier,
165 static int cpufreq_get_max_state(unsigned int cpu)
167 if (!cpu_has_cpufreq(cpu))
170 return CPUFREQ_THERMAL_MAX_STEP;
173 static int cpufreq_get_cur_state(unsigned int cpu)
175 if (!cpu_has_cpufreq(cpu))
178 return per_cpu(cpufreq_thermal_reduction_pctg, cpu);
181 static int cpufreq_set_cur_state(unsigned int cpu, int state)
183 if (!cpu_has_cpufreq(cpu))
186 per_cpu(cpufreq_thermal_reduction_pctg, cpu) = state;
187 cpufreq_update_policy(cpu);
191 void acpi_thermal_cpufreq_init(void)
195 for (i = 0; i < nr_cpu_ids; i++)
197 per_cpu(cpufreq_thermal_reduction_pctg, i) = 0;
199 i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
200 CPUFREQ_POLICY_NOTIFIER);
202 acpi_thermal_cpufreq_is_init = 1;
205 void acpi_thermal_cpufreq_exit(void)
207 if (acpi_thermal_cpufreq_is_init)
208 cpufreq_unregister_notifier
209 (&acpi_thermal_cpufreq_notifier_block,
210 CPUFREQ_POLICY_NOTIFIER);
212 acpi_thermal_cpufreq_is_init = 0;
215 #else /* ! CONFIG_CPU_FREQ */
216 static int cpufreq_get_max_state(unsigned int cpu)
221 static int cpufreq_get_cur_state(unsigned int cpu)
226 static int cpufreq_set_cur_state(unsigned int cpu, int state)
231 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
235 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
242 int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
245 struct acpi_processor *pr = NULL;
246 struct acpi_device *device = NULL;
247 int tx = 0, max_tx_px = 0;
250 if ((type < ACPI_PROCESSOR_LIMIT_NONE)
251 || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
254 result = acpi_bus_get_device(handle, &device);
258 pr = acpi_driver_data(device);
262 /* Thermal limits are always relative to the current Px/Tx state. */
263 if (pr->flags.throttling)
264 pr->limit.thermal.tx = pr->throttling.state;
267 * Our default policy is to only use throttling at the lowest
271 tx = pr->limit.thermal.tx;
275 case ACPI_PROCESSOR_LIMIT_NONE:
277 result = acpi_thermal_cpufreq_decrease(pr->id);
282 case ACPI_PROCESSOR_LIMIT_INCREMENT:
283 /* if going up: P-states first, T-states later */
285 result = acpi_thermal_cpufreq_increase(pr->id);
288 else if (result == -ERANGE)
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
290 "At maximum performance state\n"));
292 if (pr->flags.throttling) {
293 if (tx == (pr->throttling.state_count - 1))
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 "At maximum throttling state\n"));
301 case ACPI_PROCESSOR_LIMIT_DECREMENT:
302 /* if going down: T-states first, P-states later */
304 if (pr->flags.throttling) {
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308 "At minimum throttling state\n"));
315 result = acpi_thermal_cpufreq_decrease(pr->id);
318 * We only could get -ERANGE, 1 or 0.
319 * In the first two cases we reached max freq again.
321 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
322 "At minimum performance state\n"));
331 if (pr->flags.throttling) {
332 pr->limit.thermal.px = 0;
333 pr->limit.thermal.tx = tx;
335 result = acpi_processor_apply_limit(pr);
337 printk(KERN_ERR PREFIX "Unable to set thermal limit\n");
339 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
340 pr->limit.thermal.px, pr->limit.thermal.tx));
349 int acpi_processor_get_limit_info(struct acpi_processor *pr)
355 if (pr->flags.throttling)
361 /* thermal coolign device callbacks */
362 static int acpi_processor_max_state(struct acpi_processor *pr)
367 * There exists four states according to
368 * cpufreq_thermal_reduction_ptg. 0, 1, 2, 3
370 max_state += cpufreq_get_max_state(pr->id);
371 if (pr->flags.throttling)
372 max_state += (pr->throttling.state_count -1);
377 processor_get_max_state(struct thermal_cooling_device *cdev, char *buf)
379 struct acpi_device *device = cdev->devdata;
380 struct acpi_processor *pr = acpi_driver_data(device);
385 return sprintf(buf, "%d\n", acpi_processor_max_state(pr));
389 processor_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
391 struct acpi_device *device = cdev->devdata;
392 struct acpi_processor *pr = acpi_driver_data(device);
398 cur_state = cpufreq_get_cur_state(pr->id);
399 if (pr->flags.throttling)
400 cur_state += pr->throttling.state;
402 return sprintf(buf, "%d\n", cur_state);
406 processor_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
408 struct acpi_device *device = cdev->devdata;
409 struct acpi_processor *pr = acpi_driver_data(device);
416 max_pstate = cpufreq_get_max_state(pr->id);
418 if (state > acpi_processor_max_state(pr))
421 if (state <= max_pstate) {
422 if (pr->flags.throttling && pr->throttling.state)
423 result = acpi_processor_set_throttling(pr, 0);
424 cpufreq_set_cur_state(pr->id, state);
426 cpufreq_set_cur_state(pr->id, max_pstate);
427 result = acpi_processor_set_throttling(pr,
433 struct thermal_cooling_device_ops processor_cooling_ops = {
434 .get_max_state = processor_get_max_state,
435 .get_cur_state = processor_get_cur_state,
436 .set_cur_state = processor_set_cur_state,
439 /* /proc interface */
441 static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
443 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
449 if (!pr->flags.limit) {
450 seq_puts(seq, "<not supported>\n");
454 seq_printf(seq, "active limit: P%d:T%d\n"
455 "user limit: P%d:T%d\n"
456 "thermal limit: P%d:T%d\n",
457 pr->limit.state.px, pr->limit.state.tx,
458 pr->limit.user.px, pr->limit.user.tx,
459 pr->limit.thermal.px, pr->limit.thermal.tx);
465 static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
467 return single_open(file, acpi_processor_limit_seq_show,
471 static ssize_t acpi_processor_write_limit(struct file * file,
472 const char __user * buffer,
473 size_t count, loff_t * data)
476 struct seq_file *m = file->private_data;
477 struct acpi_processor *pr = m->private;
478 char limit_string[25] = { '\0' };
483 if (!pr || (count > sizeof(limit_string) - 1)) {
487 if (copy_from_user(limit_string, buffer, count)) {
491 limit_string[count] = '\0';
493 if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
494 printk(KERN_ERR PREFIX "Invalid data format\n");
498 if (pr->flags.throttling) {
499 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
500 printk(KERN_ERR PREFIX "Invalid tx\n");
503 pr->limit.user.tx = tx;
506 result = acpi_processor_apply_limit(pr);
511 struct file_operations acpi_processor_limit_fops = {
512 .open = acpi_processor_limit_open_fs,
514 .write = acpi_processor_write_limit,
516 .release = single_release,