ACPI: Remove unnecessary from/to-void* and to-void casts in drivers/acpi
[deliverable/linux.git] / drivers / acpi / processor_thermal.c
CommitLineData
1da177e4
LT
1/*
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
4be44fcd 46ACPI_MODULE_NAME("acpi_processor")
1da177e4
LT
47
48/* --------------------------------------------------------------------------
49 Limit Interface
50 -------------------------------------------------------------------------- */
4be44fcd 51static int acpi_processor_apply_limit(struct acpi_processor *pr)
1da177e4 52{
4be44fcd
LB
53 int result = 0;
54 u16 px = 0;
55 u16 tx = 0;
1da177e4 56
1da177e4
LT
57
58 if (!pr)
d550d98d 59 return -EINVAL;
1da177e4
LT
60
61 if (!pr->flags.limit)
d550d98d 62 return -ENODEV;
1da177e4
LT
63
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
4be44fcd
LB
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));
1da177e4 81
4be44fcd 82 end:
1da177e4 83 if (result)
6468463a 84 printk(KERN_ERR PREFIX "Unable to set limit\n");
1da177e4 85
d550d98d 86 return result;
1da177e4
LT
87}
88
1da177e4
LT
89#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
1da177e4
LT
100static int cpu_has_cpufreq(unsigned int cpu)
101{
102 struct cpufreq_policy policy;
1cbf4c56 103 if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
75b245b3
TR
104 return 0;
105 return 1;
1da177e4
LT
106}
107
1da177e4
LT
108static 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
1da177e4
LT
122static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
123{
124 if (!cpu_has_cpufreq(cpu))
125 return -ENODEV;
126
1cbf4c56 127 if (cpufreq_thermal_reduction_pctg[cpu] > 20)
1da177e4 128 cpufreq_thermal_reduction_pctg[cpu] -= 20;
1cbf4c56
TR
129 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];
1da177e4
LT
134}
135
4be44fcd
LB
136static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
137 unsigned long event, void *data)
1da177e4
LT
138{
139 struct cpufreq_policy *policy = data;
140 unsigned long max_freq = 0;
141
142 if (event != CPUFREQ_ADJUST)
143 goto out;
144
4be44fcd
LB
145 max_freq =
146 (policy->cpuinfo.max_freq *
147 (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
1da177e4
LT
148
149 cpufreq_verify_within_limits(policy, 0, max_freq);
150
4be44fcd 151 out:
1da177e4
LT
152 return 0;
153}
154
1da177e4
LT
155static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
156 .notifier_call = acpi_thermal_cpufreq_notifier,
157};
158
4be44fcd
LB
159void acpi_thermal_cpufreq_init(void)
160{
1da177e4
LT
161 int i;
162
4be44fcd 163 for (i = 0; i < NR_CPUS; i++)
1da177e4
LT
164 cpufreq_thermal_reduction_pctg[i] = 0;
165
4be44fcd
LB
166 i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
167 CPUFREQ_POLICY_NOTIFIER);
1da177e4
LT
168 if (!i)
169 acpi_thermal_cpufreq_is_init = 1;
170}
171
4be44fcd
LB
172void acpi_thermal_cpufreq_exit(void)
173{
1da177e4 174 if (acpi_thermal_cpufreq_is_init)
4be44fcd
LB
175 cpufreq_unregister_notifier
176 (&acpi_thermal_cpufreq_notifier_block,
177 CPUFREQ_POLICY_NOTIFIER);
1da177e4
LT
178
179 acpi_thermal_cpufreq_is_init = 0;
180}
181
4be44fcd 182#else /* ! CONFIG_CPU_FREQ */
1da177e4 183
4be44fcd
LB
184static 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}
1da177e4
LT
192
193#endif
194
4be44fcd 195int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
1da177e4 196{
4be44fcd
LB
197 int result = 0;
198 struct acpi_processor *pr = NULL;
199 struct acpi_device *device = NULL;
1cbf4c56 200 int tx = 0, max_tx_px = 0;
1da177e4 201
1da177e4
LT
202
203 if ((type < ACPI_PROCESSOR_LIMIT_NONE)
4be44fcd 204 || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
d550d98d 205 return -EINVAL;
1da177e4
LT
206
207 result = acpi_bus_get_device(handle, &device);
208 if (result)
d550d98d 209 return result;
1da177e4 210
50dd0969 211 pr = acpi_driver_data(device);
1da177e4 212 if (!pr)
d550d98d 213 return -ENODEV;
1da177e4
LT
214
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,
4be44fcd 243 "At maximum performance state\n"));
1da177e4
LT
244
245 if (pr->flags.throttling) {
246 if (tx == (pr->throttling.state_count - 1))
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 248 "At maximum throttling state\n"));
1da177e4
LT
249 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) {
1cbf4c56
TR
258 if (tx == 0) {
259 max_tx_px = 1;
1da177e4 260 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 261 "At minimum throttling state\n"));
1cbf4c56 262 } else {
1da177e4
LT
263 tx--;
264 goto end;
265 }
266 }
267
268 result = acpi_thermal_cpufreq_decrease(pr->id);
1cbf4c56
TR
269 if (result) {
270 /*
271 * We only could get -ERANGE, 1 or 0.
272 * In the first two cases we reached max freq again.
273 */
1da177e4 274 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 275 "At minimum performance state\n"));
1cbf4c56
TR
276 max_tx_px = 1;
277 } else
278 max_tx_px = 0;
1da177e4
LT
279
280 break;
281 }
282
4be44fcd 283 end:
1da177e4
LT
284 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)
6468463a 290 printk(KERN_ERR PREFIX "Unable to set thermal limit\n");
1da177e4
LT
291
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
4be44fcd 293 pr->limit.thermal.px, pr->limit.thermal.tx));
1da177e4
LT
294 } else
295 result = 0;
1cbf4c56 296 if (max_tx_px)
d550d98d 297 return 1;
1cbf4c56 298 else
d550d98d 299 return result;
1da177e4
LT
300}
301
4be44fcd 302int acpi_processor_get_limit_info(struct acpi_processor *pr)
1da177e4 303{
1da177e4
LT
304
305 if (!pr)
d550d98d 306 return -EINVAL;
1da177e4
LT
307
308 if (pr->flags.throttling)
309 pr->flags.limit = 1;
310
d550d98d 311 return 0;
1da177e4
LT
312}
313
1da177e4
LT
314/* /proc interface */
315
316static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
317{
4be44fcd 318 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
1da177e4 319
1da177e4
LT
320
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"
4be44fcd
LB
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);
1da177e4 335
4be44fcd 336 end:
d550d98d 337 return 0;
1da177e4
LT
338}
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,
4be44fcd 343 PDE(inode)->data);
1da177e4
LT
344}
345
757b1866
AB
346static ssize_t acpi_processor_write_limit(struct file * file,
347 const char __user * buffer,
348 size_t count, loff_t * data)
1da177e4 349{
4be44fcd 350 int result = 0;
50dd0969
JE
351 struct seq_file *m = file->private_data;
352 struct acpi_processor *pr = m->private;
4be44fcd
LB
353 char limit_string[25] = { '\0' };
354 int px = 0;
355 int tx = 0;
1da177e4 356
1da177e4
LT
357
358 if (!pr || (count > sizeof(limit_string) - 1)) {
d550d98d 359 return -EINVAL;
1da177e4
LT
360 }
361
362 if (copy_from_user(limit_string, buffer, count)) {
d550d98d 363 return -EFAULT;
1da177e4
LT
364 }
365
366 limit_string[count] = '\0';
367
368 if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
6468463a 369 printk(KERN_ERR PREFIX "Invalid data format\n");
d550d98d 370 return -EINVAL;
1da177e4
LT
371 }
372
373 if (pr->flags.throttling) {
374 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
6468463a 375 printk(KERN_ERR PREFIX "Invalid tx\n");
d550d98d 376 return -EINVAL;
1da177e4
LT
377 }
378 pr->limit.user.tx = tx;
379 }
380
381 result = acpi_processor_apply_limit(pr);
382
d550d98d 383 return count;
1da177e4
LT
384}
385
1da177e4 386struct file_operations acpi_processor_limit_fops = {
4be44fcd
LB
387 .open = acpi_processor_limit_open_fs,
388 .read = seq_read,
d479e908 389 .write = acpi_processor_write_limit,
4be44fcd
LB
390 .llseek = seq_lseek,
391 .release = single_release,
1da177e4 392};
This page took 0.153798 seconds and 5 git commands to generate.