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 _COMPONENT ACPI_PROCESSOR_COMPONENT
45 ACPI_MODULE_NAME("processor_thermal");
47 /* --------------------------------------------------------------------------
49 -------------------------------------------------------------------------- */
50 static int acpi_processor_apply_limit(struct acpi_processor *pr)
63 if (pr->flags.throttling) {
64 if (pr->limit.user.tx > tx)
65 tx = pr->limit.user.tx;
66 if (pr->limit.thermal.tx > tx)
67 tx = pr->limit.thermal.tx;
69 result = acpi_processor_set_throttling(pr, tx);
74 pr->limit.state.px = px;
75 pr->limit.state.tx = tx;
77 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
78 "Processor [%d] limit set to (P%d:T%d)\n", pr->id,
79 pr->limit.state.px, pr->limit.state.tx));
83 printk(KERN_ERR PREFIX "Unable to set limit\n");
88 #ifdef CONFIG_CPU_FREQ
90 /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
91 * offers (in most cases) voltage scaling in addition to frequency scaling, and
92 * thus a cubic (instead of linear) reduction of energy. Also, we allow for
93 * _any_ cpufreq driver and not only the acpi-cpufreq driver.
96 static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
97 static unsigned int acpi_thermal_cpufreq_is_init = 0;
99 static int cpu_has_cpufreq(unsigned int cpu)
101 struct cpufreq_policy policy;
102 if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
107 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
109 if (!cpu_has_cpufreq(cpu))
112 if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
113 cpufreq_thermal_reduction_pctg[cpu] += 20;
114 cpufreq_update_policy(cpu);
121 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
123 if (!cpu_has_cpufreq(cpu))
126 if (cpufreq_thermal_reduction_pctg[cpu] > 20)
127 cpufreq_thermal_reduction_pctg[cpu] -= 20;
129 cpufreq_thermal_reduction_pctg[cpu] = 0;
130 cpufreq_update_policy(cpu);
131 /* We reached max freq again and can leave passive mode */
132 return !cpufreq_thermal_reduction_pctg[cpu];
135 static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
136 unsigned long event, void *data)
138 struct cpufreq_policy *policy = data;
139 unsigned long max_freq = 0;
141 if (event != CPUFREQ_ADJUST)
145 (policy->cpuinfo.max_freq *
146 (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
148 cpufreq_verify_within_limits(policy, 0, max_freq);
154 static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
155 .notifier_call = acpi_thermal_cpufreq_notifier,
158 void acpi_thermal_cpufreq_init(void)
162 for (i = 0; i < NR_CPUS; i++)
163 cpufreq_thermal_reduction_pctg[i] = 0;
165 i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
166 CPUFREQ_POLICY_NOTIFIER);
168 acpi_thermal_cpufreq_is_init = 1;
171 void acpi_thermal_cpufreq_exit(void)
173 if (acpi_thermal_cpufreq_is_init)
174 cpufreq_unregister_notifier
175 (&acpi_thermal_cpufreq_notifier_block,
176 CPUFREQ_POLICY_NOTIFIER);
178 acpi_thermal_cpufreq_is_init = 0;
181 #else /* ! CONFIG_CPU_FREQ */
183 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
187 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
194 int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
197 struct acpi_processor *pr = NULL;
198 struct acpi_device *device = NULL;
199 int tx = 0, max_tx_px = 0;
202 if ((type < ACPI_PROCESSOR_LIMIT_NONE)
203 || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
206 result = acpi_bus_get_device(handle, &device);
210 pr = acpi_driver_data(device);
214 /* Thermal limits are always relative to the current Px/Tx state. */
215 if (pr->flags.throttling)
216 pr->limit.thermal.tx = pr->throttling.state;
219 * Our default policy is to only use throttling at the lowest
223 tx = pr->limit.thermal.tx;
227 case ACPI_PROCESSOR_LIMIT_NONE:
229 result = acpi_thermal_cpufreq_decrease(pr->id);
234 case ACPI_PROCESSOR_LIMIT_INCREMENT:
235 /* if going up: P-states first, T-states later */
237 result = acpi_thermal_cpufreq_increase(pr->id);
240 else if (result == -ERANGE)
241 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
242 "At maximum performance state\n"));
244 if (pr->flags.throttling) {
245 if (tx == (pr->throttling.state_count - 1))
246 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
247 "At maximum throttling state\n"));
253 case ACPI_PROCESSOR_LIMIT_DECREMENT:
254 /* if going down: T-states first, P-states later */
256 if (pr->flags.throttling) {
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
260 "At minimum throttling state\n"));
267 result = acpi_thermal_cpufreq_decrease(pr->id);
270 * We only could get -ERANGE, 1 or 0.
271 * In the first two cases we reached max freq again.
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274 "At minimum performance state\n"));
283 if (pr->flags.throttling) {
284 pr->limit.thermal.px = 0;
285 pr->limit.thermal.tx = tx;
287 result = acpi_processor_apply_limit(pr);
289 printk(KERN_ERR PREFIX "Unable to set thermal limit\n");
291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
292 pr->limit.thermal.px, pr->limit.thermal.tx));
301 int acpi_processor_get_limit_info(struct acpi_processor *pr)
307 if (pr->flags.throttling)
313 /* /proc interface */
315 static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
317 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
323 if (!pr->flags.limit) {
324 seq_puts(seq, "<not supported>\n");
328 seq_printf(seq, "active limit: P%d:T%d\n"
329 "user limit: P%d:T%d\n"
330 "thermal limit: P%d:T%d\n",
331 pr->limit.state.px, pr->limit.state.tx,
332 pr->limit.user.px, pr->limit.user.tx,
333 pr->limit.thermal.px, pr->limit.thermal.tx);
339 static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
341 return single_open(file, acpi_processor_limit_seq_show,
345 static ssize_t acpi_processor_write_limit(struct file * file,
346 const char __user * buffer,
347 size_t count, loff_t * data)
350 struct seq_file *m = file->private_data;
351 struct acpi_processor *pr = m->private;
352 char limit_string[25] = { '\0' };
357 if (!pr || (count > sizeof(limit_string) - 1)) {
361 if (copy_from_user(limit_string, buffer, count)) {
365 limit_string[count] = '\0';
367 if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
368 printk(KERN_ERR PREFIX "Invalid data format\n");
372 if (pr->flags.throttling) {
373 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
374 printk(KERN_ERR PREFIX "Invalid tx\n");
377 pr->limit.user.tx = tx;
380 result = acpi_processor_apply_limit(pr);
385 struct file_operations acpi_processor_limit_fops = {
386 .open = acpi_processor_limit_open_fs,
388 .write = acpi_processor_write_limit,
390 .release = single_release,