blob: 20d82f55ce5fa078c2aa5f9bbbaa9c57a1c94772 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050045ACPI_MODULE_NAME("processor_throttling");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Len Brownff55a9c2007-06-02 00:15:25 -040047static int acpi_processor_get_throttling(struct acpi_processor *pr);
48int acpi_processor_set_throttling(struct acpi_processor *pr, int state);
Luming Yu01854e62007-05-26 22:49:58 +080049
Len Brownc30c6202007-07-25 00:57:46 -040050/*
51 * _TPC - Throttling Present Capabilities
52 */
Luming Yu01854e62007-05-26 22:49:58 +080053static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
54{
55 acpi_status status = 0;
56 unsigned long tpc = 0;
57
Len Brownff55a9c2007-06-02 00:15:25 -040058 if (!pr)
Luming Yu01854e62007-05-26 22:49:58 +080059 return -EINVAL;
60 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
Len Brownc30c6202007-07-25 00:57:46 -040061 if (ACPI_FAILURE(status)) {
62 if (status != AE_NOT_FOUND) {
63 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
64 }
Luming Yu01854e62007-05-26 22:49:58 +080065 return -ENODEV;
66 }
67 pr->throttling_platform_limit = (int)tpc;
68 return 0;
69}
70
71int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
72{
Zhao Yakuief54d5a2007-11-15 16:59:30 +080073 int result = 0;
74 int throttling_limit;
75 int current_state;
76 struct acpi_processor_limit *limit;
77 int target_state;
78
79 result = acpi_processor_get_platform_limit(pr);
80 if (result) {
81 /* Throttling Limit is unsupported */
82 return result;
83 }
84
85 throttling_limit = pr->throttling_platform_limit;
86 if (throttling_limit >= pr->throttling.state_count) {
87 /* Uncorrect Throttling Limit */
88 return -EINVAL;
89 }
90
91 current_state = pr->throttling.state;
92 if (current_state > throttling_limit) {
93 /*
94 * The current state can meet the requirement of
95 * _TPC limit. But it is reasonable that OSPM changes
96 * t-states from high to low for better performance.
97 * Of course the limit condition of thermal
98 * and user should be considered.
99 */
100 limit = &pr->limit;
101 target_state = throttling_limit;
102 if (limit->thermal.tx > target_state)
103 target_state = limit->thermal.tx;
104 if (limit->user.tx > target_state)
105 target_state = limit->user.tx;
106 } else if (current_state == throttling_limit) {
107 /*
108 * Unnecessary to change the throttling state
109 */
110 return 0;
111 } else {
112 /*
113 * If the current state is lower than the limit of _TPC, it
114 * will be forced to switch to the throttling state defined
115 * by throttling_platfor_limit.
116 * Because the previous state meets with the limit condition
117 * of thermal and user, it is unnecessary to check it again.
118 */
119 target_state = throttling_limit;
120 }
121 return acpi_processor_set_throttling(pr, target_state);
Luming Yu01854e62007-05-26 22:49:58 +0800122}
123
Len Brownc30c6202007-07-25 00:57:46 -0400124/*
125 * _PTC - Processor Throttling Control (and status) register location
126 */
Luming Yu01854e62007-05-26 22:49:58 +0800127static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
128{
129 int result = 0;
130 acpi_status status = 0;
131 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
132 union acpi_object *ptc = NULL;
133 union acpi_object obj = { 0 };
134
135 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
136 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400137 if (status != AE_NOT_FOUND) {
138 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
139 }
Luming Yu01854e62007-05-26 22:49:58 +0800140 return -ENODEV;
141 }
142
143 ptc = (union acpi_object *)buffer.pointer;
144 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
145 || (ptc->package.count != 2)) {
146 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
147 result = -EFAULT;
148 goto end;
149 }
150
151 /*
152 * control_register
153 */
154
155 obj = ptc->package.elements[0];
156
157 if ((obj.type != ACPI_TYPE_BUFFER)
158 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
159 || (obj.buffer.pointer == NULL)) {
Len Brownff55a9c2007-06-02 00:15:25 -0400160 printk(KERN_ERR PREFIX
161 "Invalid _PTC data (control_register)\n");
Luming Yu01854e62007-05-26 22:49:58 +0800162 result = -EFAULT;
163 goto end;
164 }
165 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
166 sizeof(struct acpi_ptc_register));
167
168 /*
169 * status_register
170 */
171
172 obj = ptc->package.elements[1];
173
174 if ((obj.type != ACPI_TYPE_BUFFER)
175 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
176 || (obj.buffer.pointer == NULL)) {
177 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
178 result = -EFAULT;
179 goto end;
180 }
181
182 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
Len Brownff55a9c2007-06-02 00:15:25 -0400183 sizeof(struct acpi_ptc_register));
Luming Yu01854e62007-05-26 22:49:58 +0800184
Len Brownff55a9c2007-06-02 00:15:25 -0400185 end:
Luming Yu01854e62007-05-26 22:49:58 +0800186 kfree(buffer.pointer);
187
188 return result;
189}
Len Brownc30c6202007-07-25 00:57:46 -0400190
191/*
192 * _TSS - Throttling Supported States
193 */
Luming Yu01854e62007-05-26 22:49:58 +0800194static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
195{
196 int result = 0;
197 acpi_status status = AE_OK;
198 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
199 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
200 struct acpi_buffer state = { 0, NULL };
201 union acpi_object *tss = NULL;
202 int i;
203
204 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
205 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400206 if (status != AE_NOT_FOUND) {
207 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
208 }
Luming Yu01854e62007-05-26 22:49:58 +0800209 return -ENODEV;
210 }
211
212 tss = buffer.pointer;
213 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
214 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
215 result = -EFAULT;
216 goto end;
217 }
218
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
220 tss->package.count));
221
222 pr->throttling.state_count = tss->package.count;
223 pr->throttling.states_tss =
224 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
225 GFP_KERNEL);
226 if (!pr->throttling.states_tss) {
227 result = -ENOMEM;
228 goto end;
229 }
230
231 for (i = 0; i < pr->throttling.state_count; i++) {
232
Len Brownff55a9c2007-06-02 00:15:25 -0400233 struct acpi_processor_tx_tss *tx =
234 (struct acpi_processor_tx_tss *)&(pr->throttling.
235 states_tss[i]);
Luming Yu01854e62007-05-26 22:49:58 +0800236
237 state.length = sizeof(struct acpi_processor_tx_tss);
238 state.pointer = tx;
239
240 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
241
242 status = acpi_extract_package(&(tss->package.elements[i]),
243 &format, &state);
244 if (ACPI_FAILURE(status)) {
245 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
246 result = -EFAULT;
247 kfree(pr->throttling.states_tss);
248 goto end;
249 }
250
251 if (!tx->freqpercentage) {
252 printk(KERN_ERR PREFIX
Len Brownff55a9c2007-06-02 00:15:25 -0400253 "Invalid _TSS data: freq is zero\n");
Luming Yu01854e62007-05-26 22:49:58 +0800254 result = -EFAULT;
255 kfree(pr->throttling.states_tss);
256 goto end;
257 }
258 }
259
260 end:
261 kfree(buffer.pointer);
262
263 return result;
264}
Len Brownc30c6202007-07-25 00:57:46 -0400265
266/*
267 * _TSD - T-State Dependencies
268 */
Len Brownff55a9c2007-06-02 00:15:25 -0400269static int acpi_processor_get_tsd(struct acpi_processor *pr)
Luming Yu01854e62007-05-26 22:49:58 +0800270{
271 int result = 0;
272 acpi_status status = AE_OK;
Len Brownff55a9c2007-06-02 00:15:25 -0400273 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
274 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
275 struct acpi_buffer state = { 0, NULL };
276 union acpi_object *tsd = NULL;
Luming Yu01854e62007-05-26 22:49:58 +0800277 struct acpi_tsd_package *pdomain;
278
279 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
280 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400281 if (status != AE_NOT_FOUND) {
282 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
283 }
Luming Yu01854e62007-05-26 22:49:58 +0800284 return -ENODEV;
285 }
286
287 tsd = buffer.pointer;
288 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
289 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
290 result = -EFAULT;
291 goto end;
292 }
293
294 if (tsd->package.count != 1) {
295 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
296 result = -EFAULT;
297 goto end;
298 }
299
300 pdomain = &(pr->throttling.domain_info);
301
302 state.length = sizeof(struct acpi_tsd_package);
303 state.pointer = pdomain;
304
305 status = acpi_extract_package(&(tsd->package.elements[0]),
Len Brownff55a9c2007-06-02 00:15:25 -0400306 &format, &state);
Luming Yu01854e62007-05-26 22:49:58 +0800307 if (ACPI_FAILURE(status)) {
308 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
309 result = -EFAULT;
310 goto end;
311 }
312
313 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
314 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
315 result = -EFAULT;
316 goto end;
317 }
318
319 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
320 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
321 result = -EFAULT;
322 goto end;
323 }
324
Len Brownff55a9c2007-06-02 00:15:25 -0400325 end:
Luming Yu01854e62007-05-26 22:49:58 +0800326 kfree(buffer.pointer);
327 return result;
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/* --------------------------------------------------------------------------
331 Throttling Control
332 -------------------------------------------------------------------------- */
Luming Yu01854e62007-05-26 22:49:58 +0800333static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Len Brown4be44fc2005-08-05 00:44:28 -0400335 int state = 0;
336 u32 value = 0;
337 u32 duty_mask = 0;
338 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (!pr->flags.throttling)
Patrick Mocheld550d982006-06-27 00:41:40 -0400344 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 pr->throttling.state = 0;
347
348 duty_mask = pr->throttling.state_count - 1;
349
350 duty_mask <<= pr->throttling.duty_offset;
351
352 local_irq_disable();
353
354 value = inl(pr->throttling.address);
355
356 /*
357 * Compute the current throttling state when throttling is enabled
358 * (bit 4 is on).
359 */
360 if (value & 0x10) {
361 duty_value = value & duty_mask;
362 duty_value >>= pr->throttling.duty_offset;
363
364 if (duty_value)
Len Brown4be44fc2005-08-05 00:44:28 -0400365 state = pr->throttling.state_count - duty_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
368 pr->throttling.state = state;
369
370 local_irq_enable();
371
372 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400373 "Throttling state is T%d (%d%% throttling applied)\n",
374 state, pr->throttling.states[state].performance));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Patrick Mocheld550d982006-06-27 00:41:40 -0400376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Len Brownff55a9c2007-06-02 00:15:25 -0400379static int acpi_read_throttling_status(struct acpi_processor_throttling
380 *throttling)
Luming Yu01854e62007-05-26 22:49:58 +0800381{
382 int value = -1;
383 switch (throttling->status_register.space_id) {
384 case ACPI_ADR_SPACE_SYSTEM_IO:
Len Brownff55a9c2007-06-02 00:15:25 -0400385 acpi_os_read_port((acpi_io_address) throttling->status_register.
386 address, &value,
387 (u32) throttling->status_register.bit_width *
388 8);
Luming Yu01854e62007-05-26 22:49:58 +0800389 break;
390 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Len Brownff55a9c2007-06-02 00:15:25 -0400391 printk(KERN_ERR PREFIX
392 "HARDWARE addr space,NOT supported yet\n");
Luming Yu01854e62007-05-26 22:49:58 +0800393 break;
394 default:
395 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400396 (u32) (throttling->status_register.space_id));
Luming Yu01854e62007-05-26 22:49:58 +0800397 }
398 return value;
399}
400
Len Brownff55a9c2007-06-02 00:15:25 -0400401static int acpi_write_throttling_state(struct acpi_processor_throttling
402 *throttling, int value)
Luming Yu01854e62007-05-26 22:49:58 +0800403{
404 int ret = -1;
405
406 switch (throttling->control_register.space_id) {
407 case ACPI_ADR_SPACE_SYSTEM_IO:
Len Brownff55a9c2007-06-02 00:15:25 -0400408 acpi_os_write_port((acpi_io_address) throttling->
409 control_register.address, value,
410 (u32) throttling->control_register.
411 bit_width * 8);
Luming Yu01854e62007-05-26 22:49:58 +0800412 ret = 0;
413 break;
414 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Len Brownff55a9c2007-06-02 00:15:25 -0400415 printk(KERN_ERR PREFIX
416 "HARDWARE addr space,NOT supported yet\n");
Luming Yu01854e62007-05-26 22:49:58 +0800417 break;
418 default:
419 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400420 (u32) (throttling->control_register.space_id));
Luming Yu01854e62007-05-26 22:49:58 +0800421 }
422 return ret;
423}
424
Len Brownff55a9c2007-06-02 00:15:25 -0400425static int acpi_get_throttling_state(struct acpi_processor *pr, int value)
Luming Yu01854e62007-05-26 22:49:58 +0800426{
427 int i;
428
429 for (i = 0; i < pr->throttling.state_count; i++) {
Len Brownff55a9c2007-06-02 00:15:25 -0400430 struct acpi_processor_tx_tss *tx =
431 (struct acpi_processor_tx_tss *)&(pr->throttling.
432 states_tss[i]);
433 if (tx->control == value)
Luming Yu01854e62007-05-26 22:49:58 +0800434 break;
435 }
Len Brownff55a9c2007-06-02 00:15:25 -0400436 if (i > pr->throttling.state_count)
437 i = -1;
Luming Yu01854e62007-05-26 22:49:58 +0800438 return i;
439}
440
Len Brownff55a9c2007-06-02 00:15:25 -0400441static int acpi_get_throttling_value(struct acpi_processor *pr, int state)
Luming Yu01854e62007-05-26 22:49:58 +0800442{
443 int value = -1;
Len Brownff55a9c2007-06-02 00:15:25 -0400444 if (state >= 0 && state <= pr->throttling.state_count) {
445 struct acpi_processor_tx_tss *tx =
446 (struct acpi_processor_tx_tss *)&(pr->throttling.
447 states_tss[state]);
Luming Yu01854e62007-05-26 22:49:58 +0800448 value = tx->control;
449 }
450 return value;
451}
452
453static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
454{
455 int state = 0;
456 u32 value = 0;
457
Luming Yu01854e62007-05-26 22:49:58 +0800458 if (!pr)
459 return -EINVAL;
460
461 if (!pr->flags.throttling)
462 return -ENODEV;
463
464 pr->throttling.state = 0;
465 local_irq_disable();
466 value = acpi_read_throttling_status(&pr->throttling);
Len Brownff55a9c2007-06-02 00:15:25 -0400467 if (value >= 0) {
468 state = acpi_get_throttling_state(pr, value);
Luming Yu01854e62007-05-26 22:49:58 +0800469 pr->throttling.state = state;
470 }
471 local_irq_enable();
472
473 return 0;
474}
475
Luming Yu01854e62007-05-26 22:49:58 +0800476static int acpi_processor_get_throttling(struct acpi_processor *pr)
477{
478 return pr->throttling.acpi_processor_get_throttling(pr);
479}
480
Zhao Yakui22cc5012007-11-15 17:02:03 +0800481static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
482{
483 int i, step;
484
485 if (!pr->throttling.address) {
486 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
487 return -EINVAL;
488 } else if (!pr->throttling.duty_width) {
489 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
490 return -EINVAL;
491 }
492 /* TBD: Support duty_cycle values that span bit 4. */
493 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
494 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
495 return -EINVAL;
496 }
497
498 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
499
500 /*
501 * Compute state values. Note that throttling displays a linear power
502 * performance relationship (at 50% performance the CPU will consume
503 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
504 */
505
506 step = (1000 / pr->throttling.state_count);
507
508 for (i = 0; i < pr->throttling.state_count; i++) {
509 pr->throttling.states[i].performance = 1000 - step * i;
510 pr->throttling.states[i].power = 1000 - step * i;
511 }
512 return 0;
513}
514
Adrian Bunk6c5cf8a2007-07-03 00:53:12 -0400515static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
516 int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Len Brown4be44fc2005-08-05 00:44:28 -0400518 u32 value = 0;
519 u32 duty_mask = 0;
520 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400526 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (!pr->flags.throttling)
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (state == pr->throttling.state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Luming Yu01854e62007-05-26 22:49:58 +0800534 if (state < pr->throttling_platform_limit)
535 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
537 * Calculate the duty_value and duty_mask.
538 */
539 if (state) {
540 duty_value = pr->throttling.state_count - state;
541
542 duty_value <<= pr->throttling.duty_offset;
543
544 /* Used to clear all duty_value bits */
545 duty_mask = pr->throttling.state_count - 1;
546
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300547 duty_mask <<= acpi_gbl_FADT.duty_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 duty_mask = ~duty_mask;
549 }
550
551 local_irq_disable();
552
553 /*
554 * Disable throttling by writing a 0 to bit 4. Note that we must
555 * turn it off before you can change the duty_value.
556 */
557 value = inl(pr->throttling.address);
558 if (value & 0x10) {
559 value &= 0xFFFFFFEF;
560 outl(value, pr->throttling.address);
561 }
562
563 /*
564 * Write the new duty_value and then enable throttling. Note
565 * that a state value of 0 leaves throttling disabled.
566 */
567 if (state) {
568 value &= duty_mask;
569 value |= duty_value;
570 outl(value, pr->throttling.address);
571
572 value |= 0x00000010;
573 outl(value, pr->throttling.address);
574 }
575
576 pr->throttling.state = state;
577
578 local_irq_enable();
579
580 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400581 "Throttling state set to T%d (%d%%)\n", state,
582 (pr->throttling.states[state].performance ? pr->
583 throttling.states[state].performance / 10 : 0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Patrick Mocheld550d982006-06-27 00:41:40 -0400585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Adrian Bunk6c5cf8a2007-07-03 00:53:12 -0400588static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
589 int state)
Luming Yu01854e62007-05-26 22:49:58 +0800590{
591 u32 value = 0;
592
593 if (!pr)
594 return -EINVAL;
595
596 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
597 return -EINVAL;
598
599 if (!pr->flags.throttling)
600 return -ENODEV;
601
602 if (state == pr->throttling.state)
603 return 0;
604
605 if (state < pr->throttling_platform_limit)
606 return -EPERM;
607
608 local_irq_disable();
609
Len Brownff55a9c2007-06-02 00:15:25 -0400610 value = acpi_get_throttling_value(pr, state);
611 if (value >= 0) {
612 acpi_write_throttling_state(&pr->throttling, value);
Luming Yu01854e62007-05-26 22:49:58 +0800613 pr->throttling.state = state;
614 }
615 local_irq_enable();
616
617 return 0;
618}
619
620int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
621{
Len Brownff55a9c2007-06-02 00:15:25 -0400622 return pr->throttling.acpi_processor_set_throttling(pr, state);
Luming Yu01854e62007-05-26 22:49:58 +0800623}
624
Len Brown4be44fc2005-08-05 00:44:28 -0400625int acpi_processor_get_throttling_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Len Brown4be44fc2005-08-05 00:44:28 -0400627 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400630 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
631 pr->throttling.address,
632 pr->throttling.duty_offset,
633 pr->throttling.duty_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Len Brownc30c6202007-07-25 00:57:46 -0400638 /*
639 * Evaluate _PTC, _TSS and _TPC
640 * They must all be present or none of them can be used.
641 */
642 if (acpi_processor_get_throttling_control(pr) ||
643 acpi_processor_get_throttling_states(pr) ||
644 acpi_processor_get_platform_limit(pr))
645 {
Zhao Yakui22cc5012007-11-15 17:02:03 +0800646 if (acpi_processor_get_fadt_info(pr))
647 return 0;
Len Brownff55a9c2007-06-02 00:15:25 -0400648 pr->throttling.acpi_processor_get_throttling =
649 &acpi_processor_get_throttling_fadt;
650 pr->throttling.acpi_processor_set_throttling =
651 &acpi_processor_set_throttling_fadt;
Luming Yu01854e62007-05-26 22:49:58 +0800652 } else {
Len Brownff55a9c2007-06-02 00:15:25 -0400653 pr->throttling.acpi_processor_get_throttling =
654 &acpi_processor_get_throttling_ptc;
655 pr->throttling.acpi_processor_set_throttling =
656 &acpi_processor_set_throttling_ptc;
Luming Yu01854e62007-05-26 22:49:58 +0800657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Len Brownc30c6202007-07-25 00:57:46 -0400659 acpi_processor_get_tsd(pr);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * PIIX4 Errata: We don't support throttling on the original PIIX4.
663 * This shouldn't be an issue as few (if any) mobile systems ever
664 * used this part.
665 */
666 if (errata.piix4.throttle) {
667 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400668 "Throttling not supported on PIIX4 A- or B-step\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400673 pr->throttling.state_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 pr->flags.throttling = 1;
676
677 /*
678 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
679 * thermal) decide to lower performance if it so chooses, but for now
680 * we'll crank up the speed.
681 */
682
683 result = acpi_processor_get_throttling(pr);
684 if (result)
685 goto end;
686
687 if (pr->throttling.state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400688 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
689 "Disabling throttling (was T%d)\n",
690 pr->throttling.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 result = acpi_processor_set_throttling(pr, 0);
692 if (result)
693 goto end;
694 }
695
Len Brown4be44fc2005-08-05 00:44:28 -0400696 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (result)
698 pr->flags.throttling = 0;
699
Patrick Mocheld550d982006-06-27 00:41:40 -0400700 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/* proc interface */
704
Len Brown4be44fc2005-08-05 00:44:28 -0400705static int acpi_processor_throttling_seq_show(struct seq_file *seq,
706 void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200708 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400709 int i = 0;
710 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!pr)
713 goto end;
714
715 if (!(pr->throttling.state_count > 0)) {
716 seq_puts(seq, "<not supported>\n");
717 goto end;
718 }
719
720 result = acpi_processor_get_throttling(pr);
721
722 if (result) {
Len Brown4be44fc2005-08-05 00:44:28 -0400723 seq_puts(seq,
724 "Could not determine current throttling state.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto end;
726 }
727
728 seq_printf(seq, "state count: %d\n"
Luming Yu01854e62007-05-26 22:49:58 +0800729 "active state: T%d\n"
Len Brownff55a9c2007-06-02 00:15:25 -0400730 "state available: T%d to T%d\n",
Luming Yu01854e62007-05-26 22:49:58 +0800731 pr->throttling.state_count, pr->throttling.state,
Len Brownff55a9c2007-06-02 00:15:25 -0400732 pr->throttling_platform_limit,
733 pr->throttling.state_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 seq_puts(seq, "states:\n");
Luming Yu3cc26492007-07-23 12:39:28 -0400736 if (pr->throttling.acpi_processor_get_throttling ==
737 acpi_processor_get_throttling_fadt) {
Luming Yu01854e62007-05-26 22:49:58 +0800738 for (i = 0; i < pr->throttling.state_count; i++)
739 seq_printf(seq, " %cT%d: %02d%%\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400740 (i == pr->throttling.state ? '*' : ' '), i,
741 (pr->throttling.states[i].performance ? pr->
742 throttling.states[i].performance / 10 : 0));
Luming Yu3cc26492007-07-23 12:39:28 -0400743 } else {
Luming Yu01854e62007-05-26 22:49:58 +0800744 for (i = 0; i < pr->throttling.state_count; i++)
745 seq_printf(seq, " %cT%d: %02d%%\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400746 (i == pr->throttling.state ? '*' : ' '), i,
747 (int)pr->throttling.states_tss[i].
748 freqpercentage);
Luming Yu3cc26492007-07-23 12:39:28 -0400749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Len Brown4be44fc2005-08-05 00:44:28 -0400751 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Len Brown4be44fc2005-08-05 00:44:28 -0400755static int acpi_processor_throttling_open_fs(struct inode *inode,
756 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 return single_open(file, acpi_processor_throttling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400759 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Len Brownff55a9c2007-06-02 00:15:25 -0400762static ssize_t acpi_processor_write_throttling(struct file *file,
Adrian Bunk757b1862006-01-07 13:19:00 -0500763 const char __user * buffer,
764 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Len Brown4be44fc2005-08-05 00:44:28 -0400766 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200767 struct seq_file *m = file->private_data;
768 struct acpi_processor *pr = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400769 char state_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!pr || (count > sizeof(state_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (copy_from_user(state_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400775 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 state_string[count] = '\0';
778
779 result = acpi_processor_set_throttling(pr,
Len Brown4be44fc2005-08-05 00:44:28 -0400780 simple_strtoul(state_string,
781 NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400783 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Patrick Mocheld550d982006-06-27 00:41:40 -0400785 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788struct file_operations acpi_processor_throttling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400789 .open = acpi_processor_throttling_open_fs,
790 .read = seq_read,
Arjan van de Vend479e902006-01-06 16:47:00 -0500791 .write = acpi_processor_write_throttling,
Len Brown4be44fc2005-08-05 00:44:28 -0400792 .llseek = seq_lseek,
793 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794};