blob: 40fecd67ad8371674372a77c73e9e3b0fb991cdd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * processor_thermal.c - Passive cooling submodule of the ACPI processor driver
3 *
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
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
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.
16 *
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.
21 *
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.
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 */
28
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
36#include <asm/uaccess.h>
37
38#include <acpi/acpi_bus.h>
39#include <acpi/processor.h>
40#include <acpi/acpi_drivers.h>
41
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
Len Brown4be44fc2005-08-05 00:44:28 -040046ACPI_MODULE_NAME("acpi_processor")
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* --------------------------------------------------------------------------
49 Limit Interface
50 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -040051static int acpi_processor_apply_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Len Brown4be44fc2005-08-05 00:44:28 -040053 int result = 0;
54 u16 px = 0;
55 u16 tx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -040059 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (!pr->flags.limit)
Patrick Mocheld550d982006-06-27 00:41:40 -040062 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
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;
69
70 result = acpi_processor_set_throttling(pr, tx);
71 if (result)
72 goto end;
73 }
74
75 pr->limit.state.px = px;
76 pr->limit.state.tx = tx;
77
Len Brown4be44fc2005-08-05 00:44:28 -040078 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Len Brown4be44fc2005-08-05 00:44:28 -040082 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (result)
Len Brown64684632006-06-26 23:41:38 -040084 printk(KERN_ERR PREFIX "Unable to set limit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Patrick Mocheld550d982006-06-27 00:41:40 -040086 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#ifdef CONFIG_CPU_FREQ
90
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.
95 */
96
97static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
98static unsigned int acpi_thermal_cpufreq_is_init = 0;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int cpu_has_cpufreq(unsigned int cpu)
101{
102 struct cpufreq_policy policy;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400103 if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
Thomas Renninger75b245b32005-12-21 01:29:00 -0500104 return 0;
105 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int acpi_thermal_cpufreq_increase(unsigned int cpu)
109{
110 if (!cpu_has_cpufreq(cpu))
111 return -ENODEV;
112
113 if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
114 cpufreq_thermal_reduction_pctg[cpu] += 20;
115 cpufreq_update_policy(cpu);
116 return 0;
117 }
118
119 return -ERANGE;
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
123{
124 if (!cpu_has_cpufreq(cpu))
125 return -ENODEV;
126
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400127 if (cpufreq_thermal_reduction_pctg[cpu] > 20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 cpufreq_thermal_reduction_pctg[cpu] -= 20;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400129 else
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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Len Brown4be44fc2005-08-05 00:44:28 -0400136static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
137 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct cpufreq_policy *policy = data;
140 unsigned long max_freq = 0;
141
142 if (event != CPUFREQ_ADJUST)
143 goto out;
144
Len Brown4be44fc2005-08-05 00:44:28 -0400145 max_freq =
146 (policy->cpuinfo.max_freq *
147 (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 cpufreq_verify_within_limits(policy, 0, max_freq);
150
Len Brown4be44fc2005-08-05 00:44:28 -0400151 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return 0;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
156 .notifier_call = acpi_thermal_cpufreq_notifier,
157};
158
Len Brown4be44fc2005-08-05 00:44:28 -0400159void acpi_thermal_cpufreq_init(void)
160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int i;
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163 for (i = 0; i < NR_CPUS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 cpufreq_thermal_reduction_pctg[i] = 0;
165
Len Brown4be44fc2005-08-05 00:44:28 -0400166 i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
167 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (!i)
169 acpi_thermal_cpufreq_is_init = 1;
170}
171
Len Brown4be44fc2005-08-05 00:44:28 -0400172void acpi_thermal_cpufreq_exit(void)
173{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (acpi_thermal_cpufreq_is_init)
Len Brown4be44fc2005-08-05 00:44:28 -0400175 cpufreq_unregister_notifier
176 (&acpi_thermal_cpufreq_notifier_block,
177 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 acpi_thermal_cpufreq_is_init = 0;
180}
181
Len Brown4be44fc2005-08-05 00:44:28 -0400182#else /* ! CONFIG_CPU_FREQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Len Brown4be44fc2005-08-05 00:44:28 -0400184static int acpi_thermal_cpufreq_increase(unsigned int cpu)
185{
186 return -ENODEV;
187}
188static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
189{
190 return -ENODEV;
191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193#endif
194
Len Brown4be44fc2005-08-05 00:44:28 -0400195int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Len Brown4be44fc2005-08-05 00:44:28 -0400197 int result = 0;
198 struct acpi_processor *pr = NULL;
199 struct acpi_device *device = NULL;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400200 int tx = 0, max_tx_px = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if ((type < ACPI_PROCESSOR_LIMIT_NONE)
Len Brown4be44fc2005-08-05 00:44:28 -0400204 || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
Patrick Mocheld550d982006-06-27 00:41:40 -0400205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 result = acpi_bus_get_device(handle, &device);
208 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200211 pr = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400213 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
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;
218
219 /*
220 * Our default policy is to only use throttling at the lowest
221 * performance state.
222 */
223
224 tx = pr->limit.thermal.tx;
225
226 switch (type) {
227
228 case ACPI_PROCESSOR_LIMIT_NONE:
229 do {
230 result = acpi_thermal_cpufreq_decrease(pr->id);
231 } while (!result);
232 tx = 0;
233 break;
234
235 case ACPI_PROCESSOR_LIMIT_INCREMENT:
236 /* if going up: P-states first, T-states later */
237
238 result = acpi_thermal_cpufreq_increase(pr->id);
239 if (!result)
240 goto end;
241 else if (result == -ERANGE)
242 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400243 "At maximum performance state\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if (pr->flags.throttling) {
246 if (tx == (pr->throttling.state_count - 1))
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400248 "At maximum throttling state\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 else
250 tx++;
251 }
252 break;
253
254 case ACPI_PROCESSOR_LIMIT_DECREMENT:
255 /* if going down: T-states first, P-states later */
256
257 if (pr->flags.throttling) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400258 if (tx == 0) {
259 max_tx_px = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400261 "At minimum throttling state\n"));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400262 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 tx--;
264 goto end;
265 }
266 }
267
268 result = acpi_thermal_cpufreq_decrease(pr->id);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400269 if (result) {
270 /*
271 * We only could get -ERANGE, 1 or 0.
272 * In the first two cases we reached max freq again.
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400275 "At minimum performance state\n"));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400276 max_tx_px = 1;
277 } else
278 max_tx_px = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 break;
281 }
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (pr->flags.throttling) {
285 pr->limit.thermal.px = 0;
286 pr->limit.thermal.tx = tx;
287
288 result = acpi_processor_apply_limit(pr);
289 if (result)
Len Brown64684632006-06-26 23:41:38 -0400290 printk(KERN_ERR PREFIX "Unable to set thermal limit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400293 pr->limit.thermal.px, pr->limit.thermal.tx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 } else
295 result = 0;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400296 if (max_tx_px)
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return 1;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400298 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400299 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302int acpi_processor_get_limit_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (pr->flags.throttling)
309 pr->flags.limit = 1;
310
Patrick Mocheld550d982006-06-27 00:41:40 -0400311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* /proc interface */
315
316static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
317{
Len Brown4be44fc2005-08-05 00:44:28 -0400318 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (!pr)
322 goto end;
323
324 if (!pr->flags.limit) {
325 seq_puts(seq, "<not supported>\n");
326 goto end;
327 }
328
329 seq_printf(seq, "active limit: P%d:T%d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400330 "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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Len Brown4be44fc2005-08-05 00:44:28 -0400336 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
341{
342 return single_open(file, acpi_processor_limit_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400343 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Adrian Bunk757b1862006-01-07 13:19:00 -0500346static ssize_t acpi_processor_write_limit(struct file * file,
347 const char __user * buffer,
348 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Len Brown4be44fc2005-08-05 00:44:28 -0400350 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200351 struct seq_file *m = file->private_data;
352 struct acpi_processor *pr = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400353 char limit_string[25] = { '\0' };
354 int px = 0;
355 int tx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (!pr || (count > sizeof(limit_string) - 1)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 if (copy_from_user(limit_string, buffer, count)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400363 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 limit_string[count] = '\0';
367
368 if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
Len Brown64684632006-06-26 23:41:38 -0400369 printk(KERN_ERR PREFIX "Invalid data format\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 if (pr->flags.throttling) {
374 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
Len Brown64684632006-06-26 23:41:38 -0400375 printk(KERN_ERR PREFIX "Invalid tx\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 pr->limit.user.tx = tx;
379 }
380
381 result = acpi_processor_apply_limit(pr);
382
Patrick Mocheld550d982006-06-27 00:41:40 -0400383 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386struct file_operations acpi_processor_limit_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400387 .open = acpi_processor_limit_open_fs,
388 .read = seq_read,
Arjan van de Vend479e902006-01-06 16:47:00 -0500389 .write = acpi_processor_write_limit,
Len Brown4be44fc2005-08-05 00:44:28 -0400390 .llseek = seq_lseek,
391 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392};