[PATCH] skge: expand ethtool debug register dump
[linux-2.6] / 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/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
46 ACPI_MODULE_NAME("acpi_processor")
47
48 /* --------------------------------------------------------------------------
49                                  Limit Interface
50    -------------------------------------------------------------------------- */
51 static int acpi_processor_apply_limit(struct acpi_processor *pr)
52 {
53         int result = 0;
54         u16 px = 0;
55         u16 tx = 0;
56
57         ACPI_FUNCTION_TRACE("acpi_processor_apply_limit");
58
59         if (!pr)
60                 return_VALUE(-EINVAL);
61
62         if (!pr->flags.limit)
63                 return_VALUE(-ENODEV);
64
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;
70
71                 result = acpi_processor_set_throttling(pr, tx);
72                 if (result)
73                         goto end;
74         }
75
76         pr->limit.state.px = px;
77         pr->limit.state.tx = tx;
78
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));
82
83       end:
84         if (result)
85                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to set limit\n"));
86
87         return_VALUE(result);
88 }
89
90 #ifdef CONFIG_CPU_FREQ
91
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.
96  */
97
98 static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
99 static unsigned int acpi_thermal_cpufreq_is_init = 0;
100
101 static int cpu_has_cpufreq(unsigned int cpu)
102 {
103         struct cpufreq_policy policy;
104         if (!acpi_thermal_cpufreq_is_init)
105                 return -ENODEV;
106         if (!cpufreq_get_policy(&policy, cpu))
107                 return -ENODEV;
108         return 0;
109 }
110
111 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
112 {
113         if (!cpu_has_cpufreq(cpu))
114                 return -ENODEV;
115
116         if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
117                 cpufreq_thermal_reduction_pctg[cpu] += 20;
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 (cpufreq_thermal_reduction_pctg[cpu] >= 20) {
131                 cpufreq_thermal_reduction_pctg[cpu] -= 20;
132                 cpufreq_update_policy(cpu);
133                 return 0;
134         }
135
136         return -ERANGE;
137 }
138
139 static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
140                                          unsigned long event, void *data)
141 {
142         struct cpufreq_policy *policy = data;
143         unsigned long max_freq = 0;
144
145         if (event != CPUFREQ_ADJUST)
146                 goto out;
147
148         max_freq =
149             (policy->cpuinfo.max_freq *
150              (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
151
152         cpufreq_verify_within_limits(policy, 0, max_freq);
153
154       out:
155         return 0;
156 }
157
158 static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
159         .notifier_call = acpi_thermal_cpufreq_notifier,
160 };
161
162 void acpi_thermal_cpufreq_init(void)
163 {
164         int i;
165
166         for (i = 0; i < NR_CPUS; i++)
167                 cpufreq_thermal_reduction_pctg[i] = 0;
168
169         i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
170                                       CPUFREQ_POLICY_NOTIFIER);
171         if (!i)
172                 acpi_thermal_cpufreq_is_init = 1;
173 }
174
175 void acpi_thermal_cpufreq_exit(void)
176 {
177         if (acpi_thermal_cpufreq_is_init)
178                 cpufreq_unregister_notifier
179                     (&acpi_thermal_cpufreq_notifier_block,
180                      CPUFREQ_POLICY_NOTIFIER);
181
182         acpi_thermal_cpufreq_is_init = 0;
183 }
184
185 #else                           /* ! CONFIG_CPU_FREQ */
186
187 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
188 {
189         return -ENODEV;
190 }
191 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
192 {
193         return -ENODEV;
194 }
195
196 #endif
197
198 int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
199 {
200         int result = 0;
201         struct acpi_processor *pr = NULL;
202         struct acpi_device *device = NULL;
203         int tx = 0;
204
205         ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
206
207         if ((type < ACPI_PROCESSOR_LIMIT_NONE)
208             || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
209                 return_VALUE(-EINVAL);
210
211         result = acpi_bus_get_device(handle, &device);
212         if (result)
213                 return_VALUE(result);
214
215         pr = (struct acpi_processor *)acpi_driver_data(device);
216         if (!pr)
217                 return_VALUE(-ENODEV);
218
219         /* Thermal limits are always relative to the current Px/Tx state. */
220         if (pr->flags.throttling)
221                 pr->limit.thermal.tx = pr->throttling.state;
222
223         /*
224          * Our default policy is to only use throttling at the lowest
225          * performance state.
226          */
227
228         tx = pr->limit.thermal.tx;
229
230         switch (type) {
231
232         case ACPI_PROCESSOR_LIMIT_NONE:
233                 do {
234                         result = acpi_thermal_cpufreq_decrease(pr->id);
235                 } while (!result);
236                 tx = 0;
237                 break;
238
239         case ACPI_PROCESSOR_LIMIT_INCREMENT:
240                 /* if going up: P-states first, T-states later */
241
242                 result = acpi_thermal_cpufreq_increase(pr->id);
243                 if (!result)
244                         goto end;
245                 else if (result == -ERANGE)
246                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
247                                           "At maximum performance state\n"));
248
249                 if (pr->flags.throttling) {
250                         if (tx == (pr->throttling.state_count - 1))
251                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
252                                                   "At maximum throttling state\n"));
253                         else
254                                 tx++;
255                 }
256                 break;
257
258         case ACPI_PROCESSOR_LIMIT_DECREMENT:
259                 /* if going down: T-states first, P-states later */
260
261                 if (pr->flags.throttling) {
262                         if (tx == 0)
263                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
264                                                   "At minimum throttling state\n"));
265                         else {
266                                 tx--;
267                                 goto end;
268                         }
269                 }
270
271                 result = acpi_thermal_cpufreq_decrease(pr->id);
272                 if (result == -ERANGE)
273                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274                                           "At minimum performance state\n"));
275
276                 break;
277         }
278
279       end:
280         if (pr->flags.throttling) {
281                 pr->limit.thermal.px = 0;
282                 pr->limit.thermal.tx = tx;
283
284                 result = acpi_processor_apply_limit(pr);
285                 if (result)
286                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
287                                           "Unable to set thermal limit\n"));
288
289                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
290                                   pr->limit.thermal.px, pr->limit.thermal.tx));
291         } else
292                 result = 0;
293
294         return_VALUE(result);
295 }
296
297 int acpi_processor_get_limit_info(struct acpi_processor *pr)
298 {
299         ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
300
301         if (!pr)
302                 return_VALUE(-EINVAL);
303
304         if (pr->flags.throttling)
305                 pr->flags.limit = 1;
306
307         return_VALUE(0);
308 }
309
310 /* /proc interface */
311
312 static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
313 {
314         struct acpi_processor *pr = (struct acpi_processor *)seq->private;
315
316         ACPI_FUNCTION_TRACE("acpi_processor_limit_seq_show");
317
318         if (!pr)
319                 goto end;
320
321         if (!pr->flags.limit) {
322                 seq_puts(seq, "<not supported>\n");
323                 goto end;
324         }
325
326         seq_printf(seq, "active limit:            P%d:T%d\n"
327                    "user limit:              P%d:T%d\n"
328                    "thermal limit:           P%d:T%d\n",
329                    pr->limit.state.px, pr->limit.state.tx,
330                    pr->limit.user.px, pr->limit.user.tx,
331                    pr->limit.thermal.px, pr->limit.thermal.tx);
332
333       end:
334         return_VALUE(0);
335 }
336
337 static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
338 {
339         return single_open(file, acpi_processor_limit_seq_show,
340                            PDE(inode)->data);
341 }
342
343 ssize_t acpi_processor_write_limit(struct file * file,
344                                    const char __user * buffer,
345                                    size_t count, loff_t * data)
346 {
347         int result = 0;
348         struct seq_file *m = (struct seq_file *)file->private_data;
349         struct acpi_processor *pr = (struct acpi_processor *)m->private;
350         char limit_string[25] = { '\0' };
351         int px = 0;
352         int tx = 0;
353
354         ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
355
356         if (!pr || (count > sizeof(limit_string) - 1)) {
357                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
358                 return_VALUE(-EINVAL);
359         }
360
361         if (copy_from_user(limit_string, buffer, count)) {
362                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
363                 return_VALUE(-EFAULT);
364         }
365
366         limit_string[count] = '\0';
367
368         if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
369                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
370                 return_VALUE(-EINVAL);
371         }
372
373         if (pr->flags.throttling) {
374                 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
375                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid tx\n"));
376                         return_VALUE(-EINVAL);
377                 }
378                 pr->limit.user.tx = tx;
379         }
380
381         result = acpi_processor_apply_limit(pr);
382
383         return_VALUE(count);
384 }
385
386 struct file_operations acpi_processor_limit_fops = {
387         .open = acpi_processor_limit_open_fs,
388         .read = seq_read,
389         .llseek = seq_lseek,
390         .release = single_release,
391 };