]> Pileus Git - ~andy/linux/blob - drivers/acpi/processor_thermal.c
ACPI: remove dead code
[~andy/linux] / drivers / acpi / processor_thermal.c
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/sysdev.h>
34
35 #include <asm/uaccess.h>
36
37 #include <acpi/acpi_bus.h>
38 #include <acpi/processor.h>
39 #include <acpi/acpi_drivers.h>
40
41 #define PREFIX "ACPI: "
42
43 #define ACPI_PROCESSOR_CLASS            "processor"
44 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
45 ACPI_MODULE_NAME("processor_thermal");
46
47 /* --------------------------------------------------------------------------
48                                  Limit Interface
49    -------------------------------------------------------------------------- */
50 static int acpi_processor_apply_limit(struct acpi_processor *pr)
51 {
52         int result = 0;
53         u16 px = 0;
54         u16 tx = 0;
55
56
57         if (!pr)
58                 return -EINVAL;
59
60         if (!pr->flags.limit)
61                 return -ENODEV;
62
63         if (pr->flags.throttling) {
64                 if (pr->limit.user.tx > tx)
65                         tx = pr->limit.user.tx;
66                 if (pr->limit.thermal.tx > tx)
67                         tx = pr->limit.thermal.tx;
68
69                 result = acpi_processor_set_throttling(pr, tx, false);
70                 if (result)
71                         goto end;
72         }
73
74         pr->limit.state.px = px;
75         pr->limit.state.tx = tx;
76
77         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
78                           "Processor [%d] limit set to (P%d:T%d)\n", pr->id,
79                           pr->limit.state.px, pr->limit.state.tx));
80
81       end:
82         if (result)
83                 printk(KERN_ERR PREFIX "Unable to set limit\n");
84
85         return result;
86 }
87
88 #ifdef CONFIG_CPU_FREQ
89
90 /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
91  * offers (in most cases) voltage scaling in addition to frequency scaling, and
92  * thus a cubic (instead of linear) reduction of energy. Also, we allow for
93  * _any_ cpufreq driver and not only the acpi-cpufreq driver.
94  */
95
96 #define CPUFREQ_THERMAL_MIN_STEP 0
97 #define CPUFREQ_THERMAL_MAX_STEP 3
98
99 static DEFINE_PER_CPU(unsigned int, cpufreq_thermal_reduction_pctg);
100 static unsigned int acpi_thermal_cpufreq_is_init = 0;
101
102 static int cpu_has_cpufreq(unsigned int cpu)
103 {
104         struct cpufreq_policy policy;
105         if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
106                 return 0;
107         return 1;
108 }
109
110 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
111 {
112         if (!cpu_has_cpufreq(cpu))
113                 return -ENODEV;
114
115         if (per_cpu(cpufreq_thermal_reduction_pctg, cpu) <
116                 CPUFREQ_THERMAL_MAX_STEP) {
117                 per_cpu(cpufreq_thermal_reduction_pctg, cpu)++;
118                 cpufreq_update_policy(cpu);
119                 return 0;
120         }
121
122         return -ERANGE;
123 }
124
125 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
126 {
127         if (!cpu_has_cpufreq(cpu))
128                 return -ENODEV;
129
130         if (per_cpu(cpufreq_thermal_reduction_pctg, cpu) >
131                 (CPUFREQ_THERMAL_MIN_STEP + 1))
132                 per_cpu(cpufreq_thermal_reduction_pctg, cpu)--;
133         else
134                 per_cpu(cpufreq_thermal_reduction_pctg, cpu) = 0;
135         cpufreq_update_policy(cpu);
136         /* We reached max freq again and can leave passive mode */
137         return !per_cpu(cpufreq_thermal_reduction_pctg, cpu);
138 }
139
140 static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
141                                          unsigned long event, void *data)
142 {
143         struct cpufreq_policy *policy = data;
144         unsigned long max_freq = 0;
145
146         if (event != CPUFREQ_ADJUST)
147                 goto out;
148
149         max_freq = (
150             policy->cpuinfo.max_freq *
151             (100 - per_cpu(cpufreq_thermal_reduction_pctg, policy->cpu) * 20)
152         ) / 100;
153
154         cpufreq_verify_within_limits(policy, 0, max_freq);
155
156       out:
157         return 0;
158 }
159
160 static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
161         .notifier_call = acpi_thermal_cpufreq_notifier,
162 };
163
164 static int cpufreq_get_max_state(unsigned int cpu)
165 {
166         if (!cpu_has_cpufreq(cpu))
167                 return 0;
168
169         return CPUFREQ_THERMAL_MAX_STEP;
170 }
171
172 static int cpufreq_get_cur_state(unsigned int cpu)
173 {
174         if (!cpu_has_cpufreq(cpu))
175                 return 0;
176
177         return per_cpu(cpufreq_thermal_reduction_pctg, cpu);
178 }
179
180 static int cpufreq_set_cur_state(unsigned int cpu, int state)
181 {
182         if (!cpu_has_cpufreq(cpu))
183                 return 0;
184
185         per_cpu(cpufreq_thermal_reduction_pctg, cpu) = state;
186         cpufreq_update_policy(cpu);
187         return 0;
188 }
189
190 void acpi_thermal_cpufreq_init(void)
191 {
192         int i;
193
194         for (i = 0; i < nr_cpu_ids; i++)
195                 if (cpu_present(i))
196                         per_cpu(cpufreq_thermal_reduction_pctg, i) = 0;
197
198         i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
199                                       CPUFREQ_POLICY_NOTIFIER);
200         if (!i)
201                 acpi_thermal_cpufreq_is_init = 1;
202 }
203
204 void acpi_thermal_cpufreq_exit(void)
205 {
206         if (acpi_thermal_cpufreq_is_init)
207                 cpufreq_unregister_notifier
208                     (&acpi_thermal_cpufreq_notifier_block,
209                      CPUFREQ_POLICY_NOTIFIER);
210
211         acpi_thermal_cpufreq_is_init = 0;
212 }
213
214 #else                           /* ! CONFIG_CPU_FREQ */
215 static int cpufreq_get_max_state(unsigned int cpu)
216 {
217         return 0;
218 }
219
220 static int cpufreq_get_cur_state(unsigned int cpu)
221 {
222         return 0;
223 }
224
225 static int cpufreq_set_cur_state(unsigned int cpu, int state)
226 {
227         return 0;
228 }
229
230 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
231 {
232         return -ENODEV;
233 }
234 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
235 {
236         return -ENODEV;
237 }
238
239 #endif
240
241 int acpi_processor_get_limit_info(struct acpi_processor *pr)
242 {
243
244         if (!pr)
245                 return -EINVAL;
246
247         if (pr->flags.throttling)
248                 pr->flags.limit = 1;
249
250         return 0;
251 }
252
253 /* thermal coolign device callbacks */
254 static int acpi_processor_max_state(struct acpi_processor *pr)
255 {
256         int max_state = 0;
257
258         /*
259          * There exists four states according to
260          * cpufreq_thermal_reduction_ptg. 0, 1, 2, 3
261          */
262         max_state += cpufreq_get_max_state(pr->id);
263         if (pr->flags.throttling)
264                 max_state += (pr->throttling.state_count -1);
265
266         return max_state;
267 }
268 static int
269 processor_get_max_state(struct thermal_cooling_device *cdev,
270                         unsigned long *state)
271 {
272         struct acpi_device *device = cdev->devdata;
273         struct acpi_processor *pr = acpi_driver_data(device);
274
275         if (!device || !pr)
276                 return -EINVAL;
277
278         *state = acpi_processor_max_state(pr);
279         return 0;
280 }
281
282 static int
283 processor_get_cur_state(struct thermal_cooling_device *cdev,
284                         unsigned long *cur_state)
285 {
286         struct acpi_device *device = cdev->devdata;
287         struct acpi_processor *pr = acpi_driver_data(device);
288
289         if (!device || !pr)
290                 return -EINVAL;
291
292         *cur_state = cpufreq_get_cur_state(pr->id);
293         if (pr->flags.throttling)
294                 *cur_state += pr->throttling.state;
295         return 0;
296 }
297
298 static int
299 processor_set_cur_state(struct thermal_cooling_device *cdev,
300                         unsigned long state)
301 {
302         struct acpi_device *device = cdev->devdata;
303         struct acpi_processor *pr = acpi_driver_data(device);
304         int result = 0;
305         int max_pstate;
306
307         if (!device || !pr)
308                 return -EINVAL;
309
310         max_pstate = cpufreq_get_max_state(pr->id);
311
312         if (state > acpi_processor_max_state(pr))
313                 return -EINVAL;
314
315         if (state <= max_pstate) {
316                 if (pr->flags.throttling && pr->throttling.state)
317                         result = acpi_processor_set_throttling(pr, 0, false);
318                 cpufreq_set_cur_state(pr->id, state);
319         } else {
320                 cpufreq_set_cur_state(pr->id, max_pstate);
321                 result = acpi_processor_set_throttling(pr,
322                                 state - max_pstate, false);
323         }
324         return result;
325 }
326
327 struct thermal_cooling_device_ops processor_cooling_ops = {
328         .get_max_state = processor_get_max_state,
329         .get_cur_state = processor_get_cur_state,
330         .set_cur_state = processor_set_cur_state,
331 };