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 ACPI_PROCESSOR_DRIVER_NAME "ACPI Processor Driver"
45 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
46 ACPI_MODULE_NAME ("acpi_processor")
49 /* --------------------------------------------------------------------------
51 -------------------------------------------------------------------------- */
54 acpi_processor_apply_limit (
55 struct acpi_processor
* pr
)
61 ACPI_FUNCTION_TRACE("acpi_processor_apply_limit");
64 return_VALUE(-EINVAL
);
67 return_VALUE(-ENODEV
);
69 if (pr
->flags
.throttling
) {
70 if (pr
->limit
.user
.tx
> tx
)
71 tx
= pr
->limit
.user
.tx
;
72 if (pr
->limit
.thermal
.tx
> tx
)
73 tx
= pr
->limit
.thermal
.tx
;
75 result
= acpi_processor_set_throttling(pr
, tx
);
80 pr
->limit
.state
.px
= px
;
81 pr
->limit
.state
.tx
= tx
;
83 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Processor [%d] limit set to (P%d:T%d)\n",
90 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Unable to set limit\n"));
96 #ifdef CONFIG_CPU_FREQ
98 /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
99 * offers (in most cases) voltage scaling in addition to frequency scaling, and
100 * thus a cubic (instead of linear) reduction of energy. Also, we allow for
101 * _any_ cpufreq driver and not only the acpi-cpufreq driver.
104 static unsigned int cpufreq_thermal_reduction_pctg
[NR_CPUS
];
105 static unsigned int acpi_thermal_cpufreq_is_init
= 0;
108 static int cpu_has_cpufreq(unsigned int cpu
)
110 struct cpufreq_policy policy
;
111 if (!acpi_thermal_cpufreq_is_init
)
113 if (!cpufreq_get_policy(&policy
, cpu
))
119 static int acpi_thermal_cpufreq_increase(unsigned int cpu
)
121 if (!cpu_has_cpufreq(cpu
))
124 if (cpufreq_thermal_reduction_pctg
[cpu
] < 60) {
125 cpufreq_thermal_reduction_pctg
[cpu
] += 20;
126 cpufreq_update_policy(cpu
);
134 static int acpi_thermal_cpufreq_decrease(unsigned int cpu
)
136 if (!cpu_has_cpufreq(cpu
))
139 if (cpufreq_thermal_reduction_pctg
[cpu
] >= 20) {
140 cpufreq_thermal_reduction_pctg
[cpu
] -= 20;
141 cpufreq_update_policy(cpu
);
149 static int acpi_thermal_cpufreq_notifier(
150 struct notifier_block
*nb
,
154 struct cpufreq_policy
*policy
= data
;
155 unsigned long max_freq
= 0;
157 if (event
!= CPUFREQ_ADJUST
)
160 max_freq
= (policy
->cpuinfo
.max_freq
* (100 - cpufreq_thermal_reduction_pctg
[policy
->cpu
])) / 100;
162 cpufreq_verify_within_limits(policy
, 0, max_freq
);
169 static struct notifier_block acpi_thermal_cpufreq_notifier_block
= {
170 .notifier_call
= acpi_thermal_cpufreq_notifier
,
174 void acpi_thermal_cpufreq_init(void) {
177 for (i
=0; i
<NR_CPUS
; i
++)
178 cpufreq_thermal_reduction_pctg
[i
] = 0;
180 i
= cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block
, CPUFREQ_POLICY_NOTIFIER
);
182 acpi_thermal_cpufreq_is_init
= 1;
185 void acpi_thermal_cpufreq_exit(void) {
186 if (acpi_thermal_cpufreq_is_init
)
187 cpufreq_unregister_notifier(&acpi_thermal_cpufreq_notifier_block
, CPUFREQ_POLICY_NOTIFIER
);
189 acpi_thermal_cpufreq_is_init
= 0;
192 #else /* ! CONFIG_CPU_FREQ */
194 static int acpi_thermal_cpufreq_increase(unsigned int cpu
) { return -ENODEV
; }
195 static int acpi_thermal_cpufreq_decrease(unsigned int cpu
) { return -ENODEV
; }
202 acpi_processor_set_thermal_limit (
207 struct acpi_processor
*pr
= NULL
;
208 struct acpi_device
*device
= NULL
;
211 ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
213 if ((type
< ACPI_PROCESSOR_LIMIT_NONE
)
214 || (type
> ACPI_PROCESSOR_LIMIT_DECREMENT
))
215 return_VALUE(-EINVAL
);
217 result
= acpi_bus_get_device(handle
, &device
);
219 return_VALUE(result
);
221 pr
= (struct acpi_processor
*) acpi_driver_data(device
);
223 return_VALUE(-ENODEV
);
225 /* Thermal limits are always relative to the current Px/Tx state. */
226 if (pr
->flags
.throttling
)
227 pr
->limit
.thermal
.tx
= pr
->throttling
.state
;
230 * Our default policy is to only use throttling at the lowest
234 tx
= pr
->limit
.thermal
.tx
;
238 case ACPI_PROCESSOR_LIMIT_NONE
:
240 result
= acpi_thermal_cpufreq_decrease(pr
->id
);
245 case ACPI_PROCESSOR_LIMIT_INCREMENT
:
246 /* if going up: P-states first, T-states later */
248 result
= acpi_thermal_cpufreq_increase(pr
->id
);
251 else if (result
== -ERANGE
)
252 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
253 "At maximum performance state\n"));
255 if (pr
->flags
.throttling
) {
256 if (tx
== (pr
->throttling
.state_count
- 1))
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
258 "At maximum throttling state\n"));
264 case ACPI_PROCESSOR_LIMIT_DECREMENT
:
265 /* if going down: T-states first, P-states later */
267 if (pr
->flags
.throttling
) {
269 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
270 "At minimum throttling state\n"));
277 result
= acpi_thermal_cpufreq_decrease(pr
->id
);
278 if (result
== -ERANGE
)
279 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
280 "At minimum performance state\n"));
286 if (pr
->flags
.throttling
) {
287 pr
->limit
.thermal
.px
= 0;
288 pr
->limit
.thermal
.tx
= tx
;
290 result
= acpi_processor_apply_limit(pr
);
292 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
293 "Unable to set thermal limit\n"));
295 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Thermal limit now (P%d:T%d)\n",
296 pr
->limit
.thermal
.px
,
297 pr
->limit
.thermal
.tx
));
301 return_VALUE(result
);
306 acpi_processor_get_limit_info (
307 struct acpi_processor
*pr
)
309 ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
312 return_VALUE(-EINVAL
);
314 if (pr
->flags
.throttling
)
321 /* /proc interface */
323 static int acpi_processor_limit_seq_show(struct seq_file
*seq
, void *offset
)
325 struct acpi_processor
*pr
= (struct acpi_processor
*)seq
->private;
327 ACPI_FUNCTION_TRACE("acpi_processor_limit_seq_show");
332 if (!pr
->flags
.limit
) {
333 seq_puts(seq
, "<not supported>\n");
337 seq_printf(seq
, "active limit: P%d:T%d\n"
338 "user limit: P%d:T%d\n"
339 "thermal limit: P%d:T%d\n",
340 pr
->limit
.state
.px
, pr
->limit
.state
.tx
,
341 pr
->limit
.user
.px
, pr
->limit
.user
.tx
,
342 pr
->limit
.thermal
.px
, pr
->limit
.thermal
.tx
);
348 static int acpi_processor_limit_open_fs(struct inode
*inode
, struct file
*file
)
350 return single_open(file
, acpi_processor_limit_seq_show
,
354 ssize_t
acpi_processor_write_limit (
356 const char __user
*buffer
,
361 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
362 struct acpi_processor
*pr
= (struct acpi_processor
*)m
->private;
363 char limit_string
[25] = {'\0'};
367 ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
369 if (!pr
|| (count
> sizeof(limit_string
) - 1)) {
370 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid argument\n"));
371 return_VALUE(-EINVAL
);
374 if (copy_from_user(limit_string
, buffer
, count
)) {
375 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid data\n"));
376 return_VALUE(-EFAULT
);
379 limit_string
[count
] = '\0';
381 if (sscanf(limit_string
, "%d:%d", &px
, &tx
) != 2) {
382 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid data format\n"));
383 return_VALUE(-EINVAL
);
386 if (pr
->flags
.throttling
) {
387 if ((tx
< 0) || (tx
> (pr
->throttling
.state_count
- 1))) {
388 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid tx\n"));
389 return_VALUE(-EINVAL
);
391 pr
->limit
.user
.tx
= tx
;
394 result
= acpi_processor_apply_limit(pr
);
400 struct file_operations acpi_processor_limit_fops
= {
401 .open
= acpi_processor_limit_open_fs
,
404 .release
= single_release
,