blob: b966549ec000487eecce9f3e1edc53a72cd51964 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * processor_throttling.c - Throttling 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/io.h>
37#include <asm/uaccess.h>
38
39#include <acpi/acpi_bus.h>
40#include <acpi/processor.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
Len Brown4be44fc2005-08-05 00:44:28 -040046ACPI_MODULE_NAME("acpi_processor")
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* --------------------------------------------------------------------------
49 Throttling Control
50 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -040051static int acpi_processor_get_throttling(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Len Brown4be44fc2005-08-05 00:44:28 -040053 int state = 0;
54 u32 value = 0;
55 u32 duty_mask = 0;
56 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 ACPI_FUNCTION_TRACE("acpi_processor_get_throttling");
59
60 if (!pr)
61 return_VALUE(-EINVAL);
62
63 if (!pr->flags.throttling)
64 return_VALUE(-ENODEV);
65
66 pr->throttling.state = 0;
67
68 duty_mask = pr->throttling.state_count - 1;
69
70 duty_mask <<= pr->throttling.duty_offset;
71
72 local_irq_disable();
73
74 value = inl(pr->throttling.address);
75
76 /*
77 * Compute the current throttling state when throttling is enabled
78 * (bit 4 is on).
79 */
80 if (value & 0x10) {
81 duty_value = value & duty_mask;
82 duty_value >>= pr->throttling.duty_offset;
83
84 if (duty_value)
Len Brown4be44fc2005-08-05 00:44:28 -040085 state = pr->throttling.state_count - duty_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
88 pr->throttling.state = state;
89
90 local_irq_enable();
91
92 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -040093 "Throttling state is T%d (%d%% throttling applied)\n",
94 state, pr->throttling.states[state].performance));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 return_VALUE(0);
97}
98
Len Brown4be44fc2005-08-05 00:44:28 -040099int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Len Brown4be44fc2005-08-05 00:44:28 -0400101 u32 value = 0;
102 u32 duty_mask = 0;
103 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 ACPI_FUNCTION_TRACE("acpi_processor_set_throttling");
106
107 if (!pr)
108 return_VALUE(-EINVAL);
109
110 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
111 return_VALUE(-EINVAL);
112
113 if (!pr->flags.throttling)
114 return_VALUE(-ENODEV);
115
116 if (state == pr->throttling.state)
117 return_VALUE(0);
118
119 /*
120 * Calculate the duty_value and duty_mask.
121 */
122 if (state) {
123 duty_value = pr->throttling.state_count - state;
124
125 duty_value <<= pr->throttling.duty_offset;
126
127 /* Used to clear all duty_value bits */
128 duty_mask = pr->throttling.state_count - 1;
129
130 duty_mask <<= acpi_fadt.duty_offset;
131 duty_mask = ~duty_mask;
132 }
133
134 local_irq_disable();
135
136 /*
137 * Disable throttling by writing a 0 to bit 4. Note that we must
138 * turn it off before you can change the duty_value.
139 */
140 value = inl(pr->throttling.address);
141 if (value & 0x10) {
142 value &= 0xFFFFFFEF;
143 outl(value, pr->throttling.address);
144 }
145
146 /*
147 * Write the new duty_value and then enable throttling. Note
148 * that a state value of 0 leaves throttling disabled.
149 */
150 if (state) {
151 value &= duty_mask;
152 value |= duty_value;
153 outl(value, pr->throttling.address);
154
155 value |= 0x00000010;
156 outl(value, pr->throttling.address);
157 }
158
159 pr->throttling.state = state;
160
161 local_irq_enable();
162
163 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400164 "Throttling state set to T%d (%d%%)\n", state,
165 (pr->throttling.states[state].performance ? pr->
166 throttling.states[state].performance / 10 : 0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return_VALUE(0);
169}
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171int acpi_processor_get_throttling_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Len Brown4be44fc2005-08-05 00:44:28 -0400173 int result = 0;
174 int step = 0;
175 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 ACPI_FUNCTION_TRACE("acpi_processor_get_throttling_info");
178
179 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400180 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
181 pr->throttling.address,
182 pr->throttling.duty_offset,
183 pr->throttling.duty_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (!pr)
186 return_VALUE(-EINVAL);
187
188 /* TBD: Support ACPI 2.0 objects */
189
190 if (!pr->throttling.address) {
191 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
192 return_VALUE(0);
Len Brown4be44fc2005-08-05 00:44:28 -0400193 } else if (!pr->throttling.duty_width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
195 return_VALUE(0);
196 }
197 /* TBD: Support duty_cycle values that span bit 4. */
Len Brown4be44fc2005-08-05 00:44:28 -0400198 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "duty_cycle spans bit 4\n"));
200 return_VALUE(0);
201 }
202
203 /*
204 * PIIX4 Errata: We don't support throttling on the original PIIX4.
205 * This shouldn't be an issue as few (if any) mobile systems ever
206 * used this part.
207 */
208 if (errata.piix4.throttle) {
209 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400210 "Throttling not supported on PIIX4 A- or B-step\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return_VALUE(0);
212 }
213
214 pr->throttling.state_count = 1 << acpi_fadt.duty_width;
215
216 /*
217 * Compute state values. Note that throttling displays a linear power/
218 * performance relationship (at 50% performance the CPU will consume
219 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
220 */
221
222 step = (1000 / pr->throttling.state_count);
223
Len Brown4be44fc2005-08-05 00:44:28 -0400224 for (i = 0; i < pr->throttling.state_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 pr->throttling.states[i].performance = step * i;
226 pr->throttling.states[i].power = step * i;
227 }
228
229 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400230 pr->throttling.state_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 pr->flags.throttling = 1;
233
234 /*
235 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
236 * thermal) decide to lower performance if it so chooses, but for now
237 * we'll crank up the speed.
238 */
239
240 result = acpi_processor_get_throttling(pr);
241 if (result)
242 goto end;
243
244 if (pr->throttling.state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400245 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
246 "Disabling throttling (was T%d)\n",
247 pr->throttling.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 result = acpi_processor_set_throttling(pr, 0);
249 if (result)
250 goto end;
251 }
252
Len Brown4be44fc2005-08-05 00:44:28 -0400253 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (result)
255 pr->flags.throttling = 0;
256
257 return_VALUE(result);
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/* proc interface */
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262static int acpi_processor_throttling_seq_show(struct seq_file *seq,
263 void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Len Brown4be44fc2005-08-05 00:44:28 -0400265 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
266 int i = 0;
267 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 ACPI_FUNCTION_TRACE("acpi_processor_throttling_seq_show");
270
271 if (!pr)
272 goto end;
273
274 if (!(pr->throttling.state_count > 0)) {
275 seq_puts(seq, "<not supported>\n");
276 goto end;
277 }
278
279 result = acpi_processor_get_throttling(pr);
280
281 if (result) {
Len Brown4be44fc2005-08-05 00:44:28 -0400282 seq_puts(seq,
283 "Could not determine current throttling state.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto end;
285 }
286
287 seq_printf(seq, "state count: %d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400288 "active state: T%d\n",
289 pr->throttling.state_count, pr->throttling.state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 seq_puts(seq, "states:\n");
292 for (i = 0; i < pr->throttling.state_count; i++)
293 seq_printf(seq, " %cT%d: %02d%%\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400294 (i == pr->throttling.state ? '*' : ' '), i,
295 (pr->throttling.states[i].performance ? pr->
296 throttling.states[i].performance / 10 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Len Brown4be44fc2005-08-05 00:44:28 -0400298 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return_VALUE(0);
300}
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302static int acpi_processor_throttling_open_fs(struct inode *inode,
303 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 return single_open(file, acpi_processor_throttling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400306 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Adrian Bunk757b1862006-01-07 13:19:00 -0500309static ssize_t acpi_processor_write_throttling(struct file * file,
310 const char __user * buffer,
311 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Len Brown4be44fc2005-08-05 00:44:28 -0400313 int result = 0;
314 struct seq_file *m = (struct seq_file *)file->private_data;
315 struct acpi_processor *pr = (struct acpi_processor *)m->private;
316 char state_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 ACPI_FUNCTION_TRACE("acpi_processor_write_throttling");
319
320 if (!pr || (count > sizeof(state_string) - 1))
321 return_VALUE(-EINVAL);
322
323 if (copy_from_user(state_string, buffer, count))
324 return_VALUE(-EFAULT);
325
326 state_string[count] = '\0';
327
328 result = acpi_processor_set_throttling(pr,
Len Brown4be44fc2005-08-05 00:44:28 -0400329 simple_strtoul(state_string,
330 NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (result)
332 return_VALUE(result);
333
334 return_VALUE(count);
335}
336
337struct file_operations acpi_processor_throttling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400338 .open = acpi_processor_throttling_open_fs,
339 .read = seq_read,
Arjan van de Vend479e902006-01-06 16:47:00 -0500340 .write = acpi_processor_write_throttling,
Len Brown4be44fc2005-08-05 00:44:28 -0400341 .llseek = seq_lseek,
342 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};