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")
48 /* --------------------------------------------------------------------------
50 -------------------------------------------------------------------------- */
51 static int acpi_processor_apply_limit(struct acpi_processor
*pr
)
57 ACPI_FUNCTION_TRACE("acpi_processor_apply_limit");
60 return_VALUE(-EINVAL
);
63 return_VALUE(-ENODEV
);
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
);
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 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "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 static unsigned int cpufreq_thermal_reduction_pctg
[NR_CPUS
];
99 static unsigned int acpi_thermal_cpufreq_is_init
= 0;
101 static int cpu_has_cpufreq(unsigned int cpu
)
103 struct cpufreq_policy policy
;
104 if (!acpi_thermal_cpufreq_is_init
|| cpufreq_get_policy(&policy
, cpu
))
109 static int acpi_thermal_cpufreq_increase(unsigned int cpu
)
111 if (!cpu_has_cpufreq(cpu
))
114 if (cpufreq_thermal_reduction_pctg
[cpu
] < 60) {
115 cpufreq_thermal_reduction_pctg
[cpu
] += 20;
116 cpufreq_update_policy(cpu
);
123 static int acpi_thermal_cpufreq_decrease(unsigned int cpu
)
125 if (!cpu_has_cpufreq(cpu
))
128 if (cpufreq_thermal_reduction_pctg
[cpu
] > 20)
129 cpufreq_thermal_reduction_pctg
[cpu
] -= 20;
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
];
137 static int acpi_thermal_cpufreq_notifier(struct notifier_block
*nb
,
138 unsigned long event
, void *data
)
140 struct cpufreq_policy
*policy
= data
;
141 unsigned long max_freq
= 0;
143 if (event
!= CPUFREQ_ADJUST
)
147 (policy
->cpuinfo
.max_freq
*
148 (100 - cpufreq_thermal_reduction_pctg
[policy
->cpu
])) / 100;
150 cpufreq_verify_within_limits(policy
, 0, max_freq
);
156 static struct notifier_block acpi_thermal_cpufreq_notifier_block
= {
157 .notifier_call
= acpi_thermal_cpufreq_notifier
,
160 void acpi_thermal_cpufreq_init(void)
164 for (i
= 0; i
< NR_CPUS
; i
++)
165 cpufreq_thermal_reduction_pctg
[i
] = 0;
167 i
= cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block
,
168 CPUFREQ_POLICY_NOTIFIER
);
170 acpi_thermal_cpufreq_is_init
= 1;
173 void acpi_thermal_cpufreq_exit(void)
175 if (acpi_thermal_cpufreq_is_init
)
176 cpufreq_unregister_notifier
177 (&acpi_thermal_cpufreq_notifier_block
,
178 CPUFREQ_POLICY_NOTIFIER
);
180 acpi_thermal_cpufreq_is_init
= 0;
183 #else /* ! CONFIG_CPU_FREQ */
185 static int acpi_thermal_cpufreq_increase(unsigned int cpu
)
189 static int acpi_thermal_cpufreq_decrease(unsigned int cpu
)
196 int acpi_processor_set_thermal_limit(acpi_handle handle
, int type
)
199 struct acpi_processor
*pr
= NULL
;
200 struct acpi_device
*device
= NULL
;
201 int tx
= 0, max_tx_px
= 0;
203 ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
205 if ((type
< ACPI_PROCESSOR_LIMIT_NONE
)
206 || (type
> ACPI_PROCESSOR_LIMIT_DECREMENT
))
207 return_VALUE(-EINVAL
);
209 result
= acpi_bus_get_device(handle
, &device
);
211 return_VALUE(result
);
213 pr
= (struct acpi_processor
*)acpi_driver_data(device
);
215 return_VALUE(-ENODEV
);
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
;
222 * Our default policy is to only use throttling at the lowest
226 tx
= pr
->limit
.thermal
.tx
;
230 case ACPI_PROCESSOR_LIMIT_NONE
:
232 result
= acpi_thermal_cpufreq_decrease(pr
->id
);
237 case ACPI_PROCESSOR_LIMIT_INCREMENT
:
238 /* if going up: P-states first, T-states later */
240 result
= acpi_thermal_cpufreq_increase(pr
->id
);
243 else if (result
== -ERANGE
)
244 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
245 "At maximum performance state\n"));
247 if (pr
->flags
.throttling
) {
248 if (tx
== (pr
->throttling
.state_count
- 1))
249 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
250 "At maximum throttling state\n"));
256 case ACPI_PROCESSOR_LIMIT_DECREMENT
:
257 /* if going down: T-states first, P-states later */
259 if (pr
->flags
.throttling
) {
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
263 "At minimum throttling state\n"));
270 result
= acpi_thermal_cpufreq_decrease(pr
->id
);
273 * We only could get -ERANGE, 1 or 0.
274 * In the first two cases we reached max freq again.
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
277 "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
, pr
->limit
.thermal
.tx
));
302 return_VALUE(result
);
305 int acpi_processor_get_limit_info(struct acpi_processor
*pr
)
307 ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
310 return_VALUE(-EINVAL
);
312 if (pr
->flags
.throttling
)
318 /* /proc interface */
320 static int acpi_processor_limit_seq_show(struct seq_file
*seq
, void *offset
)
322 struct acpi_processor
*pr
= (struct acpi_processor
*)seq
->private;
324 ACPI_FUNCTION_TRACE("acpi_processor_limit_seq_show");
329 if (!pr
->flags
.limit
) {
330 seq_puts(seq
, "<not supported>\n");
334 seq_printf(seq
, "active limit: P%d:T%d\n"
335 "user limit: P%d:T%d\n"
336 "thermal limit: P%d:T%d\n",
337 pr
->limit
.state
.px
, pr
->limit
.state
.tx
,
338 pr
->limit
.user
.px
, pr
->limit
.user
.tx
,
339 pr
->limit
.thermal
.px
, pr
->limit
.thermal
.tx
);
345 static int acpi_processor_limit_open_fs(struct inode
*inode
, struct file
*file
)
347 return single_open(file
, acpi_processor_limit_seq_show
,
351 static ssize_t
acpi_processor_write_limit(struct file
* file
,
352 const char __user
* buffer
,
353 size_t count
, loff_t
* data
)
356 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
357 struct acpi_processor
*pr
= (struct acpi_processor
*)m
->private;
358 char limit_string
[25] = { '\0' };
362 ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
364 if (!pr
|| (count
> sizeof(limit_string
) - 1)) {
365 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid argument\n"));
366 return_VALUE(-EINVAL
);
369 if (copy_from_user(limit_string
, buffer
, count
)) {
370 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid data\n"));
371 return_VALUE(-EFAULT
);
374 limit_string
[count
] = '\0';
376 if (sscanf(limit_string
, "%d:%d", &px
, &tx
) != 2) {
377 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid data format\n"));
378 return_VALUE(-EINVAL
);
381 if (pr
->flags
.throttling
) {
382 if ((tx
< 0) || (tx
> (pr
->throttling
.state_count
- 1))) {
383 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid tx\n"));
384 return_VALUE(-EINVAL
);
386 pr
->limit
.user
.tx
= tx
;
389 result
= acpi_processor_apply_limit(pr
);
394 struct file_operations acpi_processor_limit_fops
= {
395 .open
= acpi_processor_limit_open_fs
,
397 .write
= acpi_processor_write_limit
,
399 .release
= single_release
,