blob: c2095ab7f0dd11f293f379c1d90ed2fc450b514b [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
57 ACPI_FUNCTION_TRACE("acpi_processor_apply_limit");
58
59 if (!pr)
60 return_VALUE(-EINVAL);
61
62 if (!pr->flags.limit)
63 return_VALUE(-ENODEV);
64
65 if (pr->flags.throttling) {
66 if (pr->limit.user.tx > tx)
67 tx = pr->limit.user.tx;
68 if (pr->limit.thermal.tx > tx)
69 tx = pr->limit.thermal.tx;
70
71 result = acpi_processor_set_throttling(pr, tx);
72 if (result)
73 goto end;
74 }
75
76 pr->limit.state.px = px;
77 pr->limit.state.tx = tx;
78
Len Brown4be44fc2005-08-05 00:44:28 -040079 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
80 "Processor [%d] limit set to (P%d:T%d)\n", pr->id,
81 pr->limit.state.px, pr->limit.state.tx));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Len Brown4be44fc2005-08-05 00:44:28 -040083 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (result)
Thomas Renningera6fc6722006-06-26 23:58:43 -040085 ACPI_ERROR((AE_INFO, "Unable to set limit"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 return_VALUE(result);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#ifdef CONFIG_CPU_FREQ
91
92/* If a passive cooling situation is detected, primarily CPUfreq is used, as it
93 * offers (in most cases) voltage scaling in addition to frequency scaling, and
94 * thus a cubic (instead of linear) reduction of energy. Also, we allow for
95 * _any_ cpufreq driver and not only the acpi-cpufreq driver.
96 */
97
98static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
99static unsigned int acpi_thermal_cpufreq_is_init = 0;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int cpu_has_cpufreq(unsigned int cpu)
102{
103 struct cpufreq_policy policy;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400104 if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
Thomas Renninger75b245b32005-12-21 01:29:00 -0500105 return 0;
106 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int acpi_thermal_cpufreq_increase(unsigned int cpu)
110{
111 if (!cpu_has_cpufreq(cpu))
112 return -ENODEV;
113
114 if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
115 cpufreq_thermal_reduction_pctg[cpu] += 20;
116 cpufreq_update_policy(cpu);
117 return 0;
118 }
119
120 return -ERANGE;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
124{
125 if (!cpu_has_cpufreq(cpu))
126 return -ENODEV;
127
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400128 if (cpufreq_thermal_reduction_pctg[cpu] > 20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 cpufreq_thermal_reduction_pctg[cpu] -= 20;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400130 else
131 cpufreq_thermal_reduction_pctg[cpu] = 0;
132 cpufreq_update_policy(cpu);
133 /* We reached max freq again and can leave passive mode */
134 return !cpufreq_thermal_reduction_pctg[cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Len Brown4be44fc2005-08-05 00:44:28 -0400137static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
138 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct cpufreq_policy *policy = data;
141 unsigned long max_freq = 0;
142
143 if (event != CPUFREQ_ADJUST)
144 goto out;
145
Len Brown4be44fc2005-08-05 00:44:28 -0400146 max_freq =
147 (policy->cpuinfo.max_freq *
148 (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 cpufreq_verify_within_limits(policy, 0, max_freq);
151
Len Brown4be44fc2005-08-05 00:44:28 -0400152 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
157 .notifier_call = acpi_thermal_cpufreq_notifier,
158};
159
Len Brown4be44fc2005-08-05 00:44:28 -0400160void acpi_thermal_cpufreq_init(void)
161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int i;
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164 for (i = 0; i < NR_CPUS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 cpufreq_thermal_reduction_pctg[i] = 0;
166
Len Brown4be44fc2005-08-05 00:44:28 -0400167 i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
168 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (!i)
170 acpi_thermal_cpufreq_is_init = 1;
171}
172
Len Brown4be44fc2005-08-05 00:44:28 -0400173void acpi_thermal_cpufreq_exit(void)
174{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (acpi_thermal_cpufreq_is_init)
Len Brown4be44fc2005-08-05 00:44:28 -0400176 cpufreq_unregister_notifier
177 (&acpi_thermal_cpufreq_notifier_block,
178 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 acpi_thermal_cpufreq_is_init = 0;
181}
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183#else /* ! CONFIG_CPU_FREQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Len Brown4be44fc2005-08-05 00:44:28 -0400185static int acpi_thermal_cpufreq_increase(unsigned int cpu)
186{
187 return -ENODEV;
188}
189static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
190{
191 return -ENODEV;
192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194#endif
195
Len Brown4be44fc2005-08-05 00:44:28 -0400196int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Len Brown4be44fc2005-08-05 00:44:28 -0400198 int result = 0;
199 struct acpi_processor *pr = NULL;
200 struct acpi_device *device = NULL;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400201 int tx = 0, max_tx_px = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
204
205 if ((type < ACPI_PROCESSOR_LIMIT_NONE)
Len Brown4be44fc2005-08-05 00:44:28 -0400206 || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return_VALUE(-EINVAL);
208
209 result = acpi_bus_get_device(handle, &device);
210 if (result)
211 return_VALUE(result);
212
Len Brown4be44fc2005-08-05 00:44:28 -0400213 pr = (struct acpi_processor *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!pr)
215 return_VALUE(-ENODEV);
216
217 /* Thermal limits are always relative to the current Px/Tx state. */
218 if (pr->flags.throttling)
219 pr->limit.thermal.tx = pr->throttling.state;
220
221 /*
222 * Our default policy is to only use throttling at the lowest
223 * performance state.
224 */
225
226 tx = pr->limit.thermal.tx;
227
228 switch (type) {
229
230 case ACPI_PROCESSOR_LIMIT_NONE:
231 do {
232 result = acpi_thermal_cpufreq_decrease(pr->id);
233 } while (!result);
234 tx = 0;
235 break;
236
237 case ACPI_PROCESSOR_LIMIT_INCREMENT:
238 /* if going up: P-states first, T-states later */
239
240 result = acpi_thermal_cpufreq_increase(pr->id);
241 if (!result)
242 goto end;
243 else if (result == -ERANGE)
244 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400245 "At maximum performance state\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (pr->flags.throttling) {
248 if (tx == (pr->throttling.state_count - 1))
249 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400250 "At maximum throttling state\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 else
252 tx++;
253 }
254 break;
255
256 case ACPI_PROCESSOR_LIMIT_DECREMENT:
257 /* if going down: T-states first, P-states later */
258
259 if (pr->flags.throttling) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400260 if (tx == 0) {
261 max_tx_px = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400263 "At minimum throttling state\n"));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400264 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 tx--;
266 goto end;
267 }
268 }
269
270 result = acpi_thermal_cpufreq_decrease(pr->id);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400271 if (result) {
272 /*
273 * We only could get -ERANGE, 1 or 0.
274 * In the first two cases we reached max freq again.
275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400277 "At minimum performance state\n"));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400278 max_tx_px = 1;
279 } else
280 max_tx_px = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 break;
283 }
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (pr->flags.throttling) {
287 pr->limit.thermal.px = 0;
288 pr->limit.thermal.tx = tx;
289
290 result = acpi_processor_apply_limit(pr);
291 if (result)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400292 ACPI_ERROR((AE_INFO, "Unable to set thermal limit"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400295 pr->limit.thermal.px, pr->limit.thermal.tx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 } else
297 result = 0;
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400298 if (max_tx_px)
299 return_VALUE(1);
300 else
301 return_VALUE(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Len Brown4be44fc2005-08-05 00:44:28 -0400304int acpi_processor_get_limit_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
307
308 if (!pr)
309 return_VALUE(-EINVAL);
310
311 if (pr->flags.throttling)
312 pr->flags.limit = 1;
313
314 return_VALUE(0);
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/* /proc interface */
318
319static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
320{
Len Brown4be44fc2005-08-05 00:44:28 -0400321 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 ACPI_FUNCTION_TRACE("acpi_processor_limit_seq_show");
324
325 if (!pr)
326 goto end;
327
328 if (!pr->flags.limit) {
329 seq_puts(seq, "<not supported>\n");
330 goto end;
331 }
332
333 seq_printf(seq, "active limit: P%d:T%d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400334 "user limit: P%d:T%d\n"
335 "thermal limit: P%d:T%d\n",
336 pr->limit.state.px, pr->limit.state.tx,
337 pr->limit.user.px, pr->limit.user.tx,
338 pr->limit.thermal.px, pr->limit.thermal.tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Len Brown4be44fc2005-08-05 00:44:28 -0400340 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return_VALUE(0);
342}
343
344static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
345{
346 return single_open(file, acpi_processor_limit_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400347 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Adrian Bunk757b1862006-01-07 13:19:00 -0500350static ssize_t acpi_processor_write_limit(struct file * file,
351 const char __user * buffer,
352 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Len Brown4be44fc2005-08-05 00:44:28 -0400354 int result = 0;
355 struct seq_file *m = (struct seq_file *)file->private_data;
356 struct acpi_processor *pr = (struct acpi_processor *)m->private;
357 char limit_string[25] = { '\0' };
358 int px = 0;
359 int tx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
362
363 if (!pr || (count > sizeof(limit_string) - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return_VALUE(-EINVAL);
365 }
366
367 if (copy_from_user(limit_string, buffer, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return_VALUE(-EFAULT);
369 }
370
371 limit_string[count] = '\0';
372
373 if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400374 ACPI_ERROR((AE_INFO, "Invalid data format"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return_VALUE(-EINVAL);
376 }
377
378 if (pr->flags.throttling) {
379 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400380 ACPI_ERROR((AE_INFO, "Invalid tx"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return_VALUE(-EINVAL);
382 }
383 pr->limit.user.tx = tx;
384 }
385
386 result = acpi_processor_apply_limit(pr);
387
388 return_VALUE(count);
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391struct file_operations acpi_processor_limit_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400392 .open = acpi_processor_limit_open_fs,
393 .read = seq_read,
Arjan van de Vend479e902006-01-06 16:47:00 -0500394 .write = acpi_processor_write_limit,
Len Brown4be44fc2005-08-05 00:44:28 -0400395 .llseek = seq_lseek,
396 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};