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 PREFIX "ACPI: "
45 #define ACPI_PROCESSOR_CLASS "processor"
46 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
47 ACPI_MODULE_NAME("processor_thermal");
49 /* --------------------------------------------------------------------------
51 -------------------------------------------------------------------------- */
52 static int acpi_processor_apply_limit(struct acpi_processor
*pr
)
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
;
71 result
= acpi_processor_set_throttling(pr
, tx
, false);
76 pr
->limit
.state
.px
= px
;
77 pr
->limit
.state
.tx
= tx
;
79 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
));
85 printk(KERN_ERR PREFIX
"Unable to set limit\n");
90 #ifdef CONFIG_CPU_FREQ
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.
98 #define CPUFREQ_THERMAL_MIN_STEP 0
99 #define CPUFREQ_THERMAL_MAX_STEP 3
101 static DEFINE_PER_CPU(unsigned int, cpufreq_thermal_reduction_pctg
);
102 static unsigned int acpi_thermal_cpufreq_is_init
= 0;
104 static int cpu_has_cpufreq(unsigned int cpu
)
106 struct cpufreq_policy policy
;
107 if (!acpi_thermal_cpufreq_is_init
|| cpufreq_get_policy(&policy
, cpu
))
112 static int acpi_thermal_cpufreq_increase(unsigned int cpu
)
114 if (!cpu_has_cpufreq(cpu
))
117 if (per_cpu(cpufreq_thermal_reduction_pctg
, cpu
) <
118 CPUFREQ_THERMAL_MAX_STEP
) {
119 per_cpu(cpufreq_thermal_reduction_pctg
, cpu
)++;
120 cpufreq_update_policy(cpu
);
127 static int acpi_thermal_cpufreq_decrease(unsigned int cpu
)
129 if (!cpu_has_cpufreq(cpu
))
132 if (per_cpu(cpufreq_thermal_reduction_pctg
, cpu
) >
133 (CPUFREQ_THERMAL_MIN_STEP
+ 1))
134 per_cpu(cpufreq_thermal_reduction_pctg
, cpu
)--;
136 per_cpu(cpufreq_thermal_reduction_pctg
, cpu
) = 0;
137 cpufreq_update_policy(cpu
);
138 /* We reached max freq again and can leave passive mode */
139 return !per_cpu(cpufreq_thermal_reduction_pctg
, cpu
);
142 static int acpi_thermal_cpufreq_notifier(struct notifier_block
*nb
,
143 unsigned long event
, void *data
)
145 struct cpufreq_policy
*policy
= data
;
146 unsigned long max_freq
= 0;
148 if (event
!= CPUFREQ_ADJUST
)
152 policy
->cpuinfo
.max_freq
*
153 (100 - per_cpu(cpufreq_thermal_reduction_pctg
, policy
->cpu
) * 20)
156 cpufreq_verify_within_limits(policy
, 0, max_freq
);
162 static struct notifier_block acpi_thermal_cpufreq_notifier_block
= {
163 .notifier_call
= acpi_thermal_cpufreq_notifier
,
166 static int cpufreq_get_max_state(unsigned int cpu
)
168 if (!cpu_has_cpufreq(cpu
))
171 return CPUFREQ_THERMAL_MAX_STEP
;
174 static int cpufreq_get_cur_state(unsigned int cpu
)
176 if (!cpu_has_cpufreq(cpu
))
179 return per_cpu(cpufreq_thermal_reduction_pctg
, cpu
);
182 static int cpufreq_set_cur_state(unsigned int cpu
, int state
)
184 if (!cpu_has_cpufreq(cpu
))
187 per_cpu(cpufreq_thermal_reduction_pctg
, cpu
) = state
;
188 cpufreq_update_policy(cpu
);
192 void acpi_thermal_cpufreq_init(void)
196 for (i
= 0; i
< nr_cpu_ids
; i
++)
198 per_cpu(cpufreq_thermal_reduction_pctg
, i
) = 0;
200 i
= cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block
,
201 CPUFREQ_POLICY_NOTIFIER
);
203 acpi_thermal_cpufreq_is_init
= 1;
206 void acpi_thermal_cpufreq_exit(void)
208 if (acpi_thermal_cpufreq_is_init
)
209 cpufreq_unregister_notifier
210 (&acpi_thermal_cpufreq_notifier_block
,
211 CPUFREQ_POLICY_NOTIFIER
);
213 acpi_thermal_cpufreq_is_init
= 0;
216 #else /* ! CONFIG_CPU_FREQ */
217 static int cpufreq_get_max_state(unsigned int cpu
)
222 static int cpufreq_get_cur_state(unsigned int cpu
)
227 static int cpufreq_set_cur_state(unsigned int cpu
, int state
)
232 static int acpi_thermal_cpufreq_increase(unsigned int cpu
)
236 static int acpi_thermal_cpufreq_decrease(unsigned int cpu
)
243 int acpi_processor_set_thermal_limit(acpi_handle handle
, int type
)
246 struct acpi_processor
*pr
= NULL
;
247 struct acpi_device
*device
= NULL
;
248 int tx
= 0, max_tx_px
= 0;
251 if ((type
< ACPI_PROCESSOR_LIMIT_NONE
)
252 || (type
> ACPI_PROCESSOR_LIMIT_DECREMENT
))
255 result
= acpi_bus_get_device(handle
, &device
);
259 pr
= acpi_driver_data(device
);
263 /* Thermal limits are always relative to the current Px/Tx state. */
264 if (pr
->flags
.throttling
)
265 pr
->limit
.thermal
.tx
= pr
->throttling
.state
;
268 * Our default policy is to only use throttling at the lowest
272 tx
= pr
->limit
.thermal
.tx
;
276 case ACPI_PROCESSOR_LIMIT_NONE
:
278 result
= acpi_thermal_cpufreq_decrease(pr
->id
);
283 case ACPI_PROCESSOR_LIMIT_INCREMENT
:
284 /* if going up: P-states first, T-states later */
286 result
= acpi_thermal_cpufreq_increase(pr
->id
);
289 else if (result
== -ERANGE
)
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
291 "At maximum performance state\n"));
293 if (pr
->flags
.throttling
) {
294 if (tx
== (pr
->throttling
.state_count
- 1))
295 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
296 "At maximum throttling state\n"));
302 case ACPI_PROCESSOR_LIMIT_DECREMENT
:
303 /* if going down: T-states first, P-states later */
305 if (pr
->flags
.throttling
) {
308 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
309 "At minimum throttling state\n"));
316 result
= acpi_thermal_cpufreq_decrease(pr
->id
);
319 * We only could get -ERANGE, 1 or 0.
320 * In the first two cases we reached max freq again.
322 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
323 "At minimum performance state\n"));
332 if (pr
->flags
.throttling
) {
333 pr
->limit
.thermal
.px
= 0;
334 pr
->limit
.thermal
.tx
= tx
;
336 result
= acpi_processor_apply_limit(pr
);
338 printk(KERN_ERR PREFIX
"Unable to set thermal limit\n");
340 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Thermal limit now (P%d:T%d)\n",
341 pr
->limit
.thermal
.px
, pr
->limit
.thermal
.tx
));
350 int acpi_processor_get_limit_info(struct acpi_processor
*pr
)
356 if (pr
->flags
.throttling
)
362 /* thermal coolign device callbacks */
363 static int acpi_processor_max_state(struct acpi_processor
*pr
)
368 * There exists four states according to
369 * cpufreq_thermal_reduction_ptg. 0, 1, 2, 3
371 max_state
+= cpufreq_get_max_state(pr
->id
);
372 if (pr
->flags
.throttling
)
373 max_state
+= (pr
->throttling
.state_count
-1);
378 processor_get_max_state(struct thermal_cooling_device
*cdev
,
379 unsigned long *state
)
381 struct acpi_device
*device
= cdev
->devdata
;
382 struct acpi_processor
*pr
= acpi_driver_data(device
);
387 *state
= acpi_processor_max_state(pr
);
392 processor_get_cur_state(struct thermal_cooling_device
*cdev
,
393 unsigned long *cur_state
)
395 struct acpi_device
*device
= cdev
->devdata
;
396 struct acpi_processor
*pr
= acpi_driver_data(device
);
401 *cur_state
= cpufreq_get_cur_state(pr
->id
);
402 if (pr
->flags
.throttling
)
403 *cur_state
+= pr
->throttling
.state
;
408 processor_set_cur_state(struct thermal_cooling_device
*cdev
,
411 struct acpi_device
*device
= cdev
->devdata
;
412 struct acpi_processor
*pr
= acpi_driver_data(device
);
419 max_pstate
= cpufreq_get_max_state(pr
->id
);
421 if (state
> acpi_processor_max_state(pr
))
424 if (state
<= max_pstate
) {
425 if (pr
->flags
.throttling
&& pr
->throttling
.state
)
426 result
= acpi_processor_set_throttling(pr
, 0, false);
427 cpufreq_set_cur_state(pr
->id
, state
);
429 cpufreq_set_cur_state(pr
->id
, max_pstate
);
430 result
= acpi_processor_set_throttling(pr
,
431 state
- max_pstate
, false);
436 struct thermal_cooling_device_ops processor_cooling_ops
= {
437 .get_max_state
= processor_get_max_state
,
438 .get_cur_state
= processor_get_cur_state
,
439 .set_cur_state
= processor_set_cur_state
,
442 /* /proc interface */
443 #ifdef CONFIG_ACPI_PROCFS
444 static int acpi_processor_limit_seq_show(struct seq_file
*seq
, void *offset
)
446 struct acpi_processor
*pr
= (struct acpi_processor
*)seq
->private;
452 if (!pr
->flags
.limit
) {
453 seq_puts(seq
, "<not supported>\n");
457 seq_printf(seq
, "active limit: P%d:T%d\n"
458 "user limit: P%d:T%d\n"
459 "thermal limit: P%d:T%d\n",
460 pr
->limit
.state
.px
, pr
->limit
.state
.tx
,
461 pr
->limit
.user
.px
, pr
->limit
.user
.tx
,
462 pr
->limit
.thermal
.px
, pr
->limit
.thermal
.tx
);
468 static int acpi_processor_limit_open_fs(struct inode
*inode
, struct file
*file
)
470 return single_open(file
, acpi_processor_limit_seq_show
,
474 static ssize_t
acpi_processor_write_limit(struct file
* file
,
475 const char __user
* buffer
,
476 size_t count
, loff_t
* data
)
479 struct seq_file
*m
= file
->private_data
;
480 struct acpi_processor
*pr
= m
->private;
481 char limit_string
[25] = { '\0' };
486 if (!pr
|| (count
> sizeof(limit_string
) - 1)) {
490 if (copy_from_user(limit_string
, buffer
, count
)) {
494 limit_string
[count
] = '\0';
496 if (sscanf(limit_string
, "%d:%d", &px
, &tx
) != 2) {
497 printk(KERN_ERR PREFIX
"Invalid data format\n");
501 if (pr
->flags
.throttling
) {
502 if ((tx
< 0) || (tx
> (pr
->throttling
.state_count
- 1))) {
503 printk(KERN_ERR PREFIX
"Invalid tx\n");
506 pr
->limit
.user
.tx
= tx
;
509 result
= acpi_processor_apply_limit(pr
);
514 const struct file_operations acpi_processor_limit_fops
= {
515 .owner
= THIS_MODULE
,
516 .open
= acpi_processor_limit_open_fs
,
518 .write
= acpi_processor_write_limit
,
520 .release
= single_release
,