blob: 9ade1a5b32edbef3b07142cac70cfa116ce31b89 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
Zhao Yakui357dc4c2007-11-29 16:22:43 +080033#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/cpufreq.h>
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
37
38#include <asm/io.h>
39#include <asm/uaccess.h>
40
41#include <acpi/acpi_bus.h>
Bjorn Helgaas89595b82008-11-07 16:57:45 -070042#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <acpi/processor.h>
44
Len Browna192a952009-07-28 16:45:54 -040045#define PREFIX "ACPI: "
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050049ACPI_MODULE_NAME("processor_throttling");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Zhang Rui56c213f2009-05-11 09:35:57 +080051/* ignore_tpc:
52 * 0 -> acpi processor driver doesn't ignore _TPC values
53 * 1 -> acpi processor driver ignores _TPC values
54 */
55static int ignore_tpc;
56module_param(ignore_tpc, int, 0644);
57MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
58
Zhao Yakuie4aa5cb2008-01-28 13:54:46 +080059struct throttling_tstate {
60 unsigned int cpu; /* cpu nr */
61 int target_state; /* target T-state */
62};
63
64#define THROTTLING_PRECHANGE (1)
65#define THROTTLING_POSTCHANGE (2)
66
Len Brownff55a9c2007-06-02 00:15:25 -040067static int acpi_processor_get_throttling(struct acpi_processor *pr);
Frans Pop2a908002009-08-26 14:29:29 -070068int acpi_processor_set_throttling(struct acpi_processor *pr,
69 int state, bool force);
Luming Yu01854e62007-05-26 22:49:58 +080070
Zhao Yakui11805092008-01-28 13:53:42 +080071static int acpi_processor_update_tsd_coord(void)
72{
73 int count, count_target;
74 int retval = 0;
75 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -080076 cpumask_var_t covered_cpus;
Zhao Yakui11805092008-01-28 13:53:42 +080077 struct acpi_processor *pr, *match_pr;
78 struct acpi_tsd_package *pdomain, *match_pdomain;
79 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
80
Li Zefan79f55992009-06-15 14:58:26 +080081 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
Rusty Russell2fdf66b2008-12-31 18:08:47 -080082 return -ENOMEM;
83
Zhao Yakui11805092008-01-28 13:53:42 +080084 /*
85 * Now that we have _TSD data from all CPUs, lets setup T-state
Zhao Yakui33a2a522008-01-28 13:55:56 +080086 * coordination between all CPUs.
Zhao Yakui11805092008-01-28 13:53:42 +080087 */
88 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -070089 pr = per_cpu(processors, i);
Zhao Yakui11805092008-01-28 13:53:42 +080090 if (!pr)
91 continue;
92
93 /* Basic validity check for domain info */
94 pthrottling = &(pr->throttling);
95
96 /*
97 * If tsd package for one cpu is invalid, the coordination
98 * among all CPUs is thought as invalid.
99 * Maybe it is ugly.
100 */
101 if (!pthrottling->tsd_valid_flag) {
102 retval = -EINVAL;
103 break;
104 }
105 }
106 if (retval)
107 goto err_ret;
108
Zhao Yakui11805092008-01-28 13:53:42 +0800109 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700110 pr = per_cpu(processors, i);
Zhao Yakui11805092008-01-28 13:53:42 +0800111 if (!pr)
112 continue;
113
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800114 if (cpumask_test_cpu(i, covered_cpus))
Zhao Yakui11805092008-01-28 13:53:42 +0800115 continue;
116 pthrottling = &pr->throttling;
117
118 pdomain = &(pthrottling->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800119 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
120 cpumask_set_cpu(i, covered_cpus);
Zhao Yakui11805092008-01-28 13:53:42 +0800121 /*
122 * If the number of processor in the TSD domain is 1, it is
123 * unnecessary to parse the coordination for this CPU.
124 */
125 if (pdomain->num_processors <= 1)
126 continue;
127
128 /* Validate the Domain info */
129 count_target = pdomain->num_processors;
130 count = 1;
131
132 for_each_possible_cpu(j) {
133 if (i == j)
134 continue;
135
Mike Travis706546d2008-06-09 16:22:23 -0700136 match_pr = per_cpu(processors, j);
Zhao Yakui11805092008-01-28 13:53:42 +0800137 if (!match_pr)
138 continue;
139
140 match_pthrottling = &(match_pr->throttling);
141 match_pdomain = &(match_pthrottling->domain_info);
142 if (match_pdomain->domain != pdomain->domain)
143 continue;
144
145 /* Here i and j are in the same domain.
146 * If two TSD packages have the same domain, they
147 * should have the same num_porcessors and
148 * coordination type. Otherwise it will be regarded
149 * as illegal.
150 */
151 if (match_pdomain->num_processors != count_target) {
152 retval = -EINVAL;
153 goto err_ret;
154 }
155
156 if (pdomain->coord_type != match_pdomain->coord_type) {
157 retval = -EINVAL;
158 goto err_ret;
159 }
160
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800161 cpumask_set_cpu(j, covered_cpus);
162 cpumask_set_cpu(j, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800163 count++;
164 }
165 for_each_possible_cpu(j) {
166 if (i == j)
167 continue;
168
Mike Travis706546d2008-06-09 16:22:23 -0700169 match_pr = per_cpu(processors, j);
Zhao Yakui11805092008-01-28 13:53:42 +0800170 if (!match_pr)
171 continue;
172
173 match_pthrottling = &(match_pr->throttling);
174 match_pdomain = &(match_pthrottling->domain_info);
175 if (match_pdomain->domain != pdomain->domain)
176 continue;
177
178 /*
179 * If some CPUS have the same domain, they
180 * will have the same shared_cpu_map.
181 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800182 cpumask_copy(match_pthrottling->shared_cpu_map,
183 pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800184 }
185 }
186
187err_ret:
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800188 free_cpumask_var(covered_cpus);
189
Zhao Yakui11805092008-01-28 13:53:42 +0800190 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700191 pr = per_cpu(processors, i);
Zhao Yakui11805092008-01-28 13:53:42 +0800192 if (!pr)
193 continue;
194
195 /*
196 * Assume no coordination on any error parsing domain info.
197 * The coordination type will be forced as SW_ALL.
198 */
199 if (retval) {
200 pthrottling = &(pr->throttling);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800201 cpumask_clear(pthrottling->shared_cpu_map);
202 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800203 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
204 }
205 }
206
207 return retval;
208}
209
210/*
211 * Update the T-state coordination after the _TSD
212 * data for all cpus is obtained.
213 */
214void acpi_processor_throttling_init(void)
215{
216 if (acpi_processor_update_tsd_coord())
217 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
218 "Assume no T-state coordination\n"));
219
220 return;
221}
222
Zhao Yakuie4aa5cb2008-01-28 13:54:46 +0800223static int acpi_processor_throttling_notifier(unsigned long event, void *data)
224{
225 struct throttling_tstate *p_tstate = data;
226 struct acpi_processor *pr;
227 unsigned int cpu ;
228 int target_state;
229 struct acpi_processor_limit *p_limit;
230 struct acpi_processor_throttling *p_throttling;
231
232 cpu = p_tstate->cpu;
Mike Travis706546d2008-06-09 16:22:23 -0700233 pr = per_cpu(processors, cpu);
Zhao Yakuie4aa5cb2008-01-28 13:54:46 +0800234 if (!pr) {
235 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));
236 return 0;
237 }
238 if (!pr->flags.throttling) {
239 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is "
240 "unsupported on CPU %d\n", cpu));
241 return 0;
242 }
243 target_state = p_tstate->target_state;
244 p_throttling = &(pr->throttling);
245 switch (event) {
246 case THROTTLING_PRECHANGE:
247 /*
248 * Prechange event is used to choose one proper t-state,
249 * which meets the limits of thermal, user and _TPC.
250 */
251 p_limit = &pr->limit;
252 if (p_limit->thermal.tx > target_state)
253 target_state = p_limit->thermal.tx;
254 if (p_limit->user.tx > target_state)
255 target_state = p_limit->user.tx;
256 if (pr->throttling_platform_limit > target_state)
257 target_state = pr->throttling_platform_limit;
258 if (target_state >= p_throttling->state_count) {
259 printk(KERN_WARNING
260 "Exceed the limit of T-state \n");
261 target_state = p_throttling->state_count - 1;
262 }
263 p_tstate->target_state = target_state;
264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:"
265 "target T-state of CPU %d is T%d\n",
266 cpu, target_state));
267 break;
268 case THROTTLING_POSTCHANGE:
269 /*
270 * Postchange event is only used to update the
271 * T-state flag of acpi_processor_throttling.
272 */
273 p_throttling->state = target_state;
274 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:"
275 "CPU %d is switched to T%d\n",
276 cpu, target_state));
277 break;
278 default:
279 printk(KERN_WARNING
280 "Unsupported Throttling notifier event\n");
281 break;
282 }
283
284 return 0;
285}
286
Len Brownc30c6202007-07-25 00:57:46 -0400287/*
288 * _TPC - Throttling Present Capabilities
289 */
Luming Yu01854e62007-05-26 22:49:58 +0800290static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
291{
292 acpi_status status = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400293 unsigned long long tpc = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800294
Len Brownff55a9c2007-06-02 00:15:25 -0400295 if (!pr)
Luming Yu01854e62007-05-26 22:49:58 +0800296 return -EINVAL;
Zhang Rui56c213f2009-05-11 09:35:57 +0800297
298 if (ignore_tpc)
299 goto end;
300
Luming Yu01854e62007-05-26 22:49:58 +0800301 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
Len Brownc30c6202007-07-25 00:57:46 -0400302 if (ACPI_FAILURE(status)) {
303 if (status != AE_NOT_FOUND) {
304 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
305 }
Luming Yu01854e62007-05-26 22:49:58 +0800306 return -ENODEV;
307 }
Zhang Rui56c213f2009-05-11 09:35:57 +0800308
309end:
Luming Yu01854e62007-05-26 22:49:58 +0800310 pr->throttling_platform_limit = (int)tpc;
311 return 0;
312}
313
314int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
315{
Zhao Yakuief54d5a2007-11-15 16:59:30 +0800316 int result = 0;
317 int throttling_limit;
318 int current_state;
319 struct acpi_processor_limit *limit;
320 int target_state;
321
Zhang Rui56c213f2009-05-11 09:35:57 +0800322 if (ignore_tpc)
323 return 0;
324
Zhao Yakuief54d5a2007-11-15 16:59:30 +0800325 result = acpi_processor_get_platform_limit(pr);
326 if (result) {
327 /* Throttling Limit is unsupported */
328 return result;
329 }
330
331 throttling_limit = pr->throttling_platform_limit;
332 if (throttling_limit >= pr->throttling.state_count) {
333 /* Uncorrect Throttling Limit */
334 return -EINVAL;
335 }
336
337 current_state = pr->throttling.state;
338 if (current_state > throttling_limit) {
339 /*
340 * The current state can meet the requirement of
341 * _TPC limit. But it is reasonable that OSPM changes
342 * t-states from high to low for better performance.
343 * Of course the limit condition of thermal
344 * and user should be considered.
345 */
346 limit = &pr->limit;
347 target_state = throttling_limit;
348 if (limit->thermal.tx > target_state)
349 target_state = limit->thermal.tx;
350 if (limit->user.tx > target_state)
351 target_state = limit->user.tx;
352 } else if (current_state == throttling_limit) {
353 /*
354 * Unnecessary to change the throttling state
355 */
356 return 0;
357 } else {
358 /*
359 * If the current state is lower than the limit of _TPC, it
360 * will be forced to switch to the throttling state defined
361 * by throttling_platfor_limit.
362 * Because the previous state meets with the limit condition
363 * of thermal and user, it is unnecessary to check it again.
364 */
365 target_state = throttling_limit;
366 }
Frans Pop2a908002009-08-26 14:29:29 -0700367 return acpi_processor_set_throttling(pr, target_state, false);
Luming Yu01854e62007-05-26 22:49:58 +0800368}
369
Len Brownc30c6202007-07-25 00:57:46 -0400370/*
371 * _PTC - Processor Throttling Control (and status) register location
372 */
Luming Yu01854e62007-05-26 22:49:58 +0800373static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
374{
375 int result = 0;
376 acpi_status status = 0;
377 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
378 union acpi_object *ptc = NULL;
379 union acpi_object obj = { 0 };
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800380 struct acpi_processor_throttling *throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800381
382 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
383 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400384 if (status != AE_NOT_FOUND) {
385 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
386 }
Luming Yu01854e62007-05-26 22:49:58 +0800387 return -ENODEV;
388 }
389
390 ptc = (union acpi_object *)buffer.pointer;
391 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
392 || (ptc->package.count != 2)) {
393 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
394 result = -EFAULT;
395 goto end;
396 }
397
398 /*
399 * control_register
400 */
401
402 obj = ptc->package.elements[0];
403
404 if ((obj.type != ACPI_TYPE_BUFFER)
405 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
406 || (obj.buffer.pointer == NULL)) {
Len Brownff55a9c2007-06-02 00:15:25 -0400407 printk(KERN_ERR PREFIX
408 "Invalid _PTC data (control_register)\n");
Luming Yu01854e62007-05-26 22:49:58 +0800409 result = -EFAULT;
410 goto end;
411 }
412 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
413 sizeof(struct acpi_ptc_register));
414
415 /*
416 * status_register
417 */
418
419 obj = ptc->package.elements[1];
420
421 if ((obj.type != ACPI_TYPE_BUFFER)
422 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
423 || (obj.buffer.pointer == NULL)) {
424 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
425 result = -EFAULT;
426 goto end;
427 }
428
429 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
Len Brownff55a9c2007-06-02 00:15:25 -0400430 sizeof(struct acpi_ptc_register));
Luming Yu01854e62007-05-26 22:49:58 +0800431
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800432 throttling = &pr->throttling;
433
434 if ((throttling->control_register.bit_width +
435 throttling->control_register.bit_offset) > 32) {
436 printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
437 result = -EFAULT;
438 goto end;
439 }
440
441 if ((throttling->status_register.bit_width +
442 throttling->status_register.bit_offset) > 32) {
443 printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
444 result = -EFAULT;
445 goto end;
446 }
447
Len Brownff55a9c2007-06-02 00:15:25 -0400448 end:
Luming Yu01854e62007-05-26 22:49:58 +0800449 kfree(buffer.pointer);
450
451 return result;
452}
Len Brownc30c6202007-07-25 00:57:46 -0400453
454/*
455 * _TSS - Throttling Supported States
456 */
Luming Yu01854e62007-05-26 22:49:58 +0800457static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
458{
459 int result = 0;
460 acpi_status status = AE_OK;
461 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
462 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
463 struct acpi_buffer state = { 0, NULL };
464 union acpi_object *tss = NULL;
465 int i;
466
467 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
468 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400469 if (status != AE_NOT_FOUND) {
470 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
471 }
Luming Yu01854e62007-05-26 22:49:58 +0800472 return -ENODEV;
473 }
474
475 tss = buffer.pointer;
476 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
477 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
478 result = -EFAULT;
479 goto end;
480 }
481
482 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
483 tss->package.count));
484
485 pr->throttling.state_count = tss->package.count;
486 pr->throttling.states_tss =
487 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
488 GFP_KERNEL);
489 if (!pr->throttling.states_tss) {
490 result = -ENOMEM;
491 goto end;
492 }
493
494 for (i = 0; i < pr->throttling.state_count; i++) {
495
Len Brownff55a9c2007-06-02 00:15:25 -0400496 struct acpi_processor_tx_tss *tx =
497 (struct acpi_processor_tx_tss *)&(pr->throttling.
498 states_tss[i]);
Luming Yu01854e62007-05-26 22:49:58 +0800499
500 state.length = sizeof(struct acpi_processor_tx_tss);
501 state.pointer = tx;
502
503 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
504
505 status = acpi_extract_package(&(tss->package.elements[i]),
506 &format, &state);
507 if (ACPI_FAILURE(status)) {
508 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
509 result = -EFAULT;
510 kfree(pr->throttling.states_tss);
511 goto end;
512 }
513
514 if (!tx->freqpercentage) {
515 printk(KERN_ERR PREFIX
Len Brownff55a9c2007-06-02 00:15:25 -0400516 "Invalid _TSS data: freq is zero\n");
Luming Yu01854e62007-05-26 22:49:58 +0800517 result = -EFAULT;
518 kfree(pr->throttling.states_tss);
519 goto end;
520 }
521 }
522
523 end:
524 kfree(buffer.pointer);
525
526 return result;
527}
Len Brownc30c6202007-07-25 00:57:46 -0400528
529/*
530 * _TSD - T-State Dependencies
531 */
Len Brownff55a9c2007-06-02 00:15:25 -0400532static int acpi_processor_get_tsd(struct acpi_processor *pr)
Luming Yu01854e62007-05-26 22:49:58 +0800533{
534 int result = 0;
535 acpi_status status = AE_OK;
Len Brownff55a9c2007-06-02 00:15:25 -0400536 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
537 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
538 struct acpi_buffer state = { 0, NULL };
539 union acpi_object *tsd = NULL;
Luming Yu01854e62007-05-26 22:49:58 +0800540 struct acpi_tsd_package *pdomain;
Zhao Yakui11805092008-01-28 13:53:42 +0800541 struct acpi_processor_throttling *pthrottling;
542
543 pthrottling = &pr->throttling;
544 pthrottling->tsd_valid_flag = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800545
546 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
547 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400548 if (status != AE_NOT_FOUND) {
549 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
550 }
Luming Yu01854e62007-05-26 22:49:58 +0800551 return -ENODEV;
552 }
553
554 tsd = buffer.pointer;
555 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800556 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
Luming Yu01854e62007-05-26 22:49:58 +0800557 result = -EFAULT;
558 goto end;
559 }
560
561 if (tsd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800562 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
Luming Yu01854e62007-05-26 22:49:58 +0800563 result = -EFAULT;
564 goto end;
565 }
566
567 pdomain = &(pr->throttling.domain_info);
568
569 state.length = sizeof(struct acpi_tsd_package);
570 state.pointer = pdomain;
571
572 status = acpi_extract_package(&(tsd->package.elements[0]),
Len Brownff55a9c2007-06-02 00:15:25 -0400573 &format, &state);
Luming Yu01854e62007-05-26 22:49:58 +0800574 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800575 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
Luming Yu01854e62007-05-26 22:49:58 +0800576 result = -EFAULT;
577 goto end;
578 }
579
580 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800581 printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n");
Luming Yu01854e62007-05-26 22:49:58 +0800582 result = -EFAULT;
583 goto end;
584 }
585
586 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800587 printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");
Luming Yu01854e62007-05-26 22:49:58 +0800588 result = -EFAULT;
589 goto end;
590 }
591
Zhao Yakui11805092008-01-28 13:53:42 +0800592 pthrottling = &pr->throttling;
593 pthrottling->tsd_valid_flag = 1;
594 pthrottling->shared_type = pdomain->coord_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800595 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800596 /*
597 * If the coordination type is not defined in ACPI spec,
598 * the tsd_valid_flag will be clear and coordination type
599 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
600 */
601 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
602 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
603 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
604 pthrottling->tsd_valid_flag = 0;
605 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
606 }
607
Len Brownff55a9c2007-06-02 00:15:25 -0400608 end:
Luming Yu01854e62007-05-26 22:49:58 +0800609 kfree(buffer.pointer);
610 return result;
611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/* --------------------------------------------------------------------------
614 Throttling Control
615 -------------------------------------------------------------------------- */
Luming Yu01854e62007-05-26 22:49:58 +0800616static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Len Brown4be44fc2005-08-05 00:44:28 -0400618 int state = 0;
619 u32 value = 0;
620 u32 duty_mask = 0;
621 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400624 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 if (!pr->flags.throttling)
Patrick Mocheld550d982006-06-27 00:41:40 -0400627 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 pr->throttling.state = 0;
630
631 duty_mask = pr->throttling.state_count - 1;
632
633 duty_mask <<= pr->throttling.duty_offset;
634
635 local_irq_disable();
636
637 value = inl(pr->throttling.address);
638
639 /*
640 * Compute the current throttling state when throttling is enabled
641 * (bit 4 is on).
642 */
643 if (value & 0x10) {
644 duty_value = value & duty_mask;
645 duty_value >>= pr->throttling.duty_offset;
646
647 if (duty_value)
Len Brown4be44fc2005-08-05 00:44:28 -0400648 state = pr->throttling.state_count - duty_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
651 pr->throttling.state = state;
652
653 local_irq_enable();
654
655 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400656 "Throttling state is T%d (%d%% throttling applied)\n",
657 state, pr->throttling.states[state].performance));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800662#ifdef CONFIG_X86
663static int acpi_throttling_rdmsr(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800664 u64 *value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800665{
666 struct cpuinfo_x86 *c;
667 u64 msr_high, msr_low;
668 unsigned int cpu;
669 u64 msr = 0;
670 int ret = -1;
671
672 cpu = pr->id;
673 c = &cpu_data(cpu);
674
675 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
676 !cpu_has(c, X86_FEATURE_ACPI)) {
677 printk(KERN_ERR PREFIX
678 "HARDWARE addr space,NOT supported yet\n");
679 } else {
680 msr_low = 0;
681 msr_high = 0;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800682 rdmsr_safe(MSR_IA32_THERM_CONTROL,
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800683 (u32 *)&msr_low , (u32 *) &msr_high);
684 msr = (msr_high << 32) | msr_low;
Lin Ming439913f2010-01-28 10:53:19 +0800685 *value = (u64) msr;
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800686 ret = 0;
687 }
688 return ret;
689}
690
Lin Ming439913f2010-01-28 10:53:19 +0800691static int acpi_throttling_wrmsr(struct acpi_processor *pr, u64 value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800692{
693 struct cpuinfo_x86 *c;
694 unsigned int cpu;
695 int ret = -1;
696 u64 msr;
697
698 cpu = pr->id;
699 c = &cpu_data(cpu);
700
701 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
702 !cpu_has(c, X86_FEATURE_ACPI)) {
703 printk(KERN_ERR PREFIX
704 "HARDWARE addr space,NOT supported yet\n");
705 } else {
706 msr = value;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800707 wrmsr_safe(MSR_IA32_THERM_CONTROL,
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800708 msr & 0xffffffff, msr >> 32);
709 ret = 0;
710 }
711 return ret;
712}
713#else
714static int acpi_throttling_rdmsr(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800715 u64 *value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800716{
717 printk(KERN_ERR PREFIX
718 "HARDWARE addr space,NOT supported yet\n");
719 return -1;
720}
721
Lin Ming439913f2010-01-28 10:53:19 +0800722static int acpi_throttling_wrmsr(struct acpi_processor *pr, u64 value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800723{
724 printk(KERN_ERR PREFIX
725 "HARDWARE addr space,NOT supported yet\n");
726 return -1;
727}
728#endif
729
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800730static int acpi_read_throttling_status(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800731 u64 *value)
Luming Yu01854e62007-05-26 22:49:58 +0800732{
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800733 u32 bit_width, bit_offset;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800734 u64 ptc_value;
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800735 u64 ptc_mask;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800736 struct acpi_processor_throttling *throttling;
737 int ret = -1;
738
739 throttling = &pr->throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800740 switch (throttling->status_register.space_id) {
741 case ACPI_ADR_SPACE_SYSTEM_IO:
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800742 ptc_value = 0;
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800743 bit_width = throttling->status_register.bit_width;
744 bit_offset = throttling->status_register.bit_offset;
745
Len Brownff55a9c2007-06-02 00:15:25 -0400746 acpi_os_read_port((acpi_io_address) throttling->status_register.
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800747 address, (u32 *) &ptc_value,
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800748 (u32) (bit_width + bit_offset));
749 ptc_mask = (1 << bit_width) - 1;
Lin Ming439913f2010-01-28 10:53:19 +0800750 *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800751 ret = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800752 break;
753 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800754 ret = acpi_throttling_rdmsr(pr, value);
Luming Yu01854e62007-05-26 22:49:58 +0800755 break;
756 default:
757 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400758 (u32) (throttling->status_register.space_id));
Luming Yu01854e62007-05-26 22:49:58 +0800759 }
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800760 return ret;
Luming Yu01854e62007-05-26 22:49:58 +0800761}
762
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800763static int acpi_write_throttling_state(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800764 u64 value)
Luming Yu01854e62007-05-26 22:49:58 +0800765{
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800766 u32 bit_width, bit_offset;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800767 u64 ptc_value;
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800768 u64 ptc_mask;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800769 struct acpi_processor_throttling *throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800770 int ret = -1;
771
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800772 throttling = &pr->throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800773 switch (throttling->control_register.space_id) {
774 case ACPI_ADR_SPACE_SYSTEM_IO:
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800775 bit_width = throttling->control_register.bit_width;
776 bit_offset = throttling->control_register.bit_offset;
777 ptc_mask = (1 << bit_width) - 1;
778 ptc_value = value & ptc_mask;
779
Len Brownff55a9c2007-06-02 00:15:25 -0400780 acpi_os_write_port((acpi_io_address) throttling->
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800781 control_register.address,
782 (u32) (ptc_value << bit_offset),
783 (u32) (bit_width + bit_offset));
Luming Yu01854e62007-05-26 22:49:58 +0800784 ret = 0;
785 break;
786 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800787 ret = acpi_throttling_wrmsr(pr, value);
Luming Yu01854e62007-05-26 22:49:58 +0800788 break;
789 default:
790 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400791 (u32) (throttling->control_register.space_id));
Luming Yu01854e62007-05-26 22:49:58 +0800792 }
793 return ret;
794}
795
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800796static int acpi_get_throttling_state(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800797 u64 value)
Luming Yu01854e62007-05-26 22:49:58 +0800798{
799 int i;
800
801 for (i = 0; i < pr->throttling.state_count; i++) {
Len Brownff55a9c2007-06-02 00:15:25 -0400802 struct acpi_processor_tx_tss *tx =
803 (struct acpi_processor_tx_tss *)&(pr->throttling.
804 states_tss[i]);
805 if (tx->control == value)
Len Brown53af9cf2009-03-15 23:36:38 -0400806 return i;
Luming Yu01854e62007-05-26 22:49:58 +0800807 }
Len Brown53af9cf2009-03-15 23:36:38 -0400808 return -1;
Luming Yu01854e62007-05-26 22:49:58 +0800809}
810
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800811static int acpi_get_throttling_value(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800812 int state, u64 *value)
Luming Yu01854e62007-05-26 22:49:58 +0800813{
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800814 int ret = -1;
815
Len Brownff55a9c2007-06-02 00:15:25 -0400816 if (state >= 0 && state <= pr->throttling.state_count) {
817 struct acpi_processor_tx_tss *tx =
818 (struct acpi_processor_tx_tss *)&(pr->throttling.
819 states_tss[state]);
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800820 *value = tx->control;
821 ret = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800822 }
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800823 return ret;
Luming Yu01854e62007-05-26 22:49:58 +0800824}
825
826static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
827{
828 int state = 0;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800829 int ret;
Lin Ming439913f2010-01-28 10:53:19 +0800830 u64 value;
Luming Yu01854e62007-05-26 22:49:58 +0800831
Luming Yu01854e62007-05-26 22:49:58 +0800832 if (!pr)
833 return -EINVAL;
834
835 if (!pr->flags.throttling)
836 return -ENODEV;
837
838 pr->throttling.state = 0;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800839
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800840 value = 0;
841 ret = acpi_read_throttling_status(pr, &value);
842 if (ret >= 0) {
Len Brownff55a9c2007-06-02 00:15:25 -0400843 state = acpi_get_throttling_state(pr, value);
Zhang Rui4973b222009-05-11 09:36:01 +0800844 if (state == -1) {
Frans Popbdf57de2009-08-26 14:29:30 -0700845 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
846 "Invalid throttling state, reset\n"));
Zhang Rui4973b222009-05-11 09:36:01 +0800847 state = 0;
Frans Pop2a908002009-08-26 14:29:29 -0700848 ret = acpi_processor_set_throttling(pr, state, true);
Zhang Rui4973b222009-05-11 09:36:01 +0800849 if (ret)
850 return ret;
851 }
Luming Yu01854e62007-05-26 22:49:58 +0800852 pr->throttling.state = state;
853 }
Luming Yu01854e62007-05-26 22:49:58 +0800854
855 return 0;
856}
857
Luming Yu01854e62007-05-26 22:49:58 +0800858static int acpi_processor_get_throttling(struct acpi_processor *pr)
859{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800860 cpumask_var_t saved_mask;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800861 int ret;
862
Zhao Yakui87654272008-01-28 13:53:30 +0800863 if (!pr)
864 return -EINVAL;
865
866 if (!pr->flags.throttling)
867 return -ENODEV;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800868
869 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
870 return -ENOMEM;
871
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800872 /*
873 * Migrate task to the cpu pointed by pr.
874 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800875 cpumask_copy(saved_mask, &current->cpus_allowed);
876 /* FIXME: use work_on_cpu() */
877 set_cpus_allowed_ptr(current, cpumask_of(pr->id));
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800878 ret = pr->throttling.acpi_processor_get_throttling(pr);
879 /* restore the previous state */
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800880 set_cpus_allowed_ptr(current, saved_mask);
881 free_cpumask_var(saved_mask);
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800882
883 return ret;
Luming Yu01854e62007-05-26 22:49:58 +0800884}
885
Zhao Yakui22cc5012007-11-15 17:02:03 +0800886static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
887{
888 int i, step;
889
890 if (!pr->throttling.address) {
891 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
892 return -EINVAL;
893 } else if (!pr->throttling.duty_width) {
894 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
895 return -EINVAL;
896 }
897 /* TBD: Support duty_cycle values that span bit 4. */
898 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
899 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
900 return -EINVAL;
901 }
902
903 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
904
905 /*
906 * Compute state values. Note that throttling displays a linear power
907 * performance relationship (at 50% performance the CPU will consume
908 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
909 */
910
911 step = (1000 / pr->throttling.state_count);
912
913 for (i = 0; i < pr->throttling.state_count; i++) {
914 pr->throttling.states[i].performance = 1000 - step * i;
915 pr->throttling.states[i].power = 1000 - step * i;
916 }
917 return 0;
918}
919
Adrian Bunk6c5cf8a2007-07-03 00:53:12 -0400920static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
Frans Pop2a908002009-08-26 14:29:29 -0700921 int state, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Len Brown4be44fc2005-08-05 00:44:28 -0400923 u32 value = 0;
924 u32 duty_mask = 0;
925 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400928 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (!pr->flags.throttling)
Patrick Mocheld550d982006-06-27 00:41:40 -0400934 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Frans Pop2a908002009-08-26 14:29:29 -0700936 if (!force && (state == pr->throttling.state))
Patrick Mocheld550d982006-06-27 00:41:40 -0400937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Luming Yu01854e62007-05-26 22:49:58 +0800939 if (state < pr->throttling_platform_limit)
940 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /*
942 * Calculate the duty_value and duty_mask.
943 */
944 if (state) {
945 duty_value = pr->throttling.state_count - state;
946
947 duty_value <<= pr->throttling.duty_offset;
948
949 /* Used to clear all duty_value bits */
950 duty_mask = pr->throttling.state_count - 1;
951
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300952 duty_mask <<= acpi_gbl_FADT.duty_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 duty_mask = ~duty_mask;
954 }
955
956 local_irq_disable();
957
958 /*
959 * Disable throttling by writing a 0 to bit 4. Note that we must
960 * turn it off before you can change the duty_value.
961 */
962 value = inl(pr->throttling.address);
963 if (value & 0x10) {
964 value &= 0xFFFFFFEF;
965 outl(value, pr->throttling.address);
966 }
967
968 /*
969 * Write the new duty_value and then enable throttling. Note
970 * that a state value of 0 leaves throttling disabled.
971 */
972 if (state) {
973 value &= duty_mask;
974 value |= duty_value;
975 outl(value, pr->throttling.address);
976
977 value |= 0x00000010;
978 outl(value, pr->throttling.address);
979 }
980
981 pr->throttling.state = state;
982
983 local_irq_enable();
984
985 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400986 "Throttling state set to T%d (%d%%)\n", state,
987 (pr->throttling.states[state].performance ? pr->
988 throttling.states[state].performance / 10 : 0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Patrick Mocheld550d982006-06-27 00:41:40 -0400990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Adrian Bunk6c5cf8a2007-07-03 00:53:12 -0400993static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
Frans Pop2a908002009-08-26 14:29:29 -0700994 int state, bool force)
Luming Yu01854e62007-05-26 22:49:58 +0800995{
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800996 int ret;
Lin Ming439913f2010-01-28 10:53:19 +0800997 u64 value;
Luming Yu01854e62007-05-26 22:49:58 +0800998
999 if (!pr)
1000 return -EINVAL;
1001
1002 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1003 return -EINVAL;
1004
1005 if (!pr->flags.throttling)
1006 return -ENODEV;
1007
Frans Pop2a908002009-08-26 14:29:29 -07001008 if (!force && (state == pr->throttling.state))
Luming Yu01854e62007-05-26 22:49:58 +08001009 return 0;
1010
1011 if (state < pr->throttling_platform_limit)
1012 return -EPERM;
1013
Zhao Yakui0753f6e2007-11-15 17:03:46 +08001014 value = 0;
1015 ret = acpi_get_throttling_value(pr, state, &value);
1016 if (ret >= 0) {
1017 acpi_write_throttling_state(pr, value);
Luming Yu01854e62007-05-26 22:49:58 +08001018 pr->throttling.state = state;
1019 }
Luming Yu01854e62007-05-26 22:49:58 +08001020
1021 return 0;
1022}
1023
Frans Pop2a908002009-08-26 14:29:29 -07001024int acpi_processor_set_throttling(struct acpi_processor *pr,
1025 int state, bool force)
Luming Yu01854e62007-05-26 22:49:58 +08001026{
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001027 cpumask_var_t saved_mask;
Len Brown3391a762008-02-02 03:56:18 -05001028 int ret = 0;
Zhao Yakui33a2a522008-01-28 13:55:56 +08001029 unsigned int i;
1030 struct acpi_processor *match_pr;
1031 struct acpi_processor_throttling *p_throttling;
1032 struct throttling_tstate t_state;
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001033 cpumask_var_t online_throttling_cpus;
Zhao Yakui87654272008-01-28 13:53:30 +08001034
1035 if (!pr)
1036 return -EINVAL;
1037
1038 if (!pr->flags.throttling)
1039 return -ENODEV;
1040
1041 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1042 return -EINVAL;
1043
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001044 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
1045 return -ENOMEM;
1046
1047 if (!alloc_cpumask_var(&online_throttling_cpus, GFP_KERNEL)) {
1048 free_cpumask_var(saved_mask);
1049 return -ENOMEM;
1050 }
1051
1052 cpumask_copy(saved_mask, &current->cpus_allowed);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001053 t_state.target_state = state;
1054 p_throttling = &(pr->throttling);
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001055 cpumask_and(online_throttling_cpus, cpu_online_mask,
1056 p_throttling->shared_cpu_map);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001057 /*
1058 * The throttling notifier will be called for every
1059 * affected cpu in order to get one proper T-state.
1060 * The notifier event is THROTTLING_PRECHANGE.
1061 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001062 for_each_cpu(i, online_throttling_cpus) {
Zhao Yakui33a2a522008-01-28 13:55:56 +08001063 t_state.cpu = i;
1064 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1065 &t_state);
1066 }
1067 /*
1068 * The function of acpi_processor_set_throttling will be called
1069 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1070 * it is necessary to call it for every affected cpu. Otherwise
1071 * it can be called only for the cpu pointed by pr.
1072 */
1073 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001074 /* FIXME: use work_on_cpu() */
1075 set_cpus_allowed_ptr(current, cpumask_of(pr->id));
Zhao Yakui33a2a522008-01-28 13:55:56 +08001076 ret = p_throttling->acpi_processor_set_throttling(pr,
Frans Pop2a908002009-08-26 14:29:29 -07001077 t_state.target_state, force);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001078 } else {
1079 /*
1080 * When the T-state coordination is SW_ALL or HW_ALL,
1081 * it is necessary to set T-state for every affected
1082 * cpus.
1083 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001084 for_each_cpu(i, online_throttling_cpus) {
Mike Travis706546d2008-06-09 16:22:23 -07001085 match_pr = per_cpu(processors, i);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001086 /*
1087 * If the pointer is invalid, we will report the
1088 * error message and continue.
1089 */
1090 if (!match_pr) {
1091 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1092 "Invalid Pointer for CPU %d\n", i));
1093 continue;
1094 }
1095 /*
1096 * If the throttling control is unsupported on CPU i,
1097 * we will report the error message and continue.
1098 */
1099 if (!match_pr->flags.throttling) {
1100 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1101 "Throttling Controll is unsupported "
1102 "on CPU %d\n", i));
1103 continue;
1104 }
1105 t_state.cpu = i;
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001106 /* FIXME: use work_on_cpu() */
1107 set_cpus_allowed_ptr(current, cpumask_of(i));
Zhao Yakui33a2a522008-01-28 13:55:56 +08001108 ret = match_pr->throttling.
1109 acpi_processor_set_throttling(
Frans Pop2a908002009-08-26 14:29:29 -07001110 match_pr, t_state.target_state, force);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001111 }
1112 }
1113 /*
1114 * After the set_throttling is called, the
1115 * throttling notifier is called for every
1116 * affected cpu to update the T-states.
1117 * The notifier event is THROTTLING_POSTCHANGE
1118 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001119 for_each_cpu(i, online_throttling_cpus) {
Zhao Yakui33a2a522008-01-28 13:55:56 +08001120 t_state.cpu = i;
1121 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1122 &t_state);
1123 }
Zhao Yakui357dc4c2007-11-29 16:22:43 +08001124 /* restore the previous state */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001125 /* FIXME: use work_on_cpu() */
1126 set_cpus_allowed_ptr(current, saved_mask);
1127 free_cpumask_var(online_throttling_cpus);
1128 free_cpumask_var(saved_mask);
Zhao Yakui357dc4c2007-11-29 16:22:43 +08001129 return ret;
Luming Yu01854e62007-05-26 22:49:58 +08001130}
1131
Len Brown4be44fc2005-08-05 00:44:28 -04001132int acpi_processor_get_throttling_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Len Brown4be44fc2005-08-05 00:44:28 -04001134 int result = 0;
Zhao Yakui11805092008-01-28 13:53:42 +08001135 struct acpi_processor_throttling *pthrottling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001138 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1139 pr->throttling.address,
1140 pr->throttling.duty_offset,
1141 pr->throttling.duty_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Len Brownc30c6202007-07-25 00:57:46 -04001143 /*
1144 * Evaluate _PTC, _TSS and _TPC
1145 * They must all be present or none of them can be used.
1146 */
1147 if (acpi_processor_get_throttling_control(pr) ||
1148 acpi_processor_get_throttling_states(pr) ||
1149 acpi_processor_get_platform_limit(pr))
1150 {
Len Brownff55a9c2007-06-02 00:15:25 -04001151 pr->throttling.acpi_processor_get_throttling =
1152 &acpi_processor_get_throttling_fadt;
1153 pr->throttling.acpi_processor_set_throttling =
1154 &acpi_processor_set_throttling_fadt;
Alexey Starikovskiyd1154be2008-01-15 00:47:47 -05001155 if (acpi_processor_get_fadt_info(pr))
1156 return 0;
Luming Yu01854e62007-05-26 22:49:58 +08001157 } else {
Len Brownff55a9c2007-06-02 00:15:25 -04001158 pr->throttling.acpi_processor_get_throttling =
1159 &acpi_processor_get_throttling_ptc;
1160 pr->throttling.acpi_processor_set_throttling =
1161 &acpi_processor_set_throttling_ptc;
Luming Yu01854e62007-05-26 22:49:58 +08001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Zhao Yakui11805092008-01-28 13:53:42 +08001164 /*
1165 * If TSD package for one CPU can't be parsed successfully, it means
1166 * that this CPU will have no coordination with other CPUs.
1167 */
1168 if (acpi_processor_get_tsd(pr)) {
1169 pthrottling = &pr->throttling;
1170 pthrottling->tsd_valid_flag = 0;
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001171 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +08001172 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1173 }
Len Brownc30c6202007-07-25 00:57:46 -04001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1177 * This shouldn't be an issue as few (if any) mobile systems ever
1178 * used this part.
1179 */
1180 if (errata.piix4.throttle) {
1181 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001182 "Throttling not supported on PIIX4 A- or B-step\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001187 pr->throttling.state_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 pr->flags.throttling = 1;
1190
1191 /*
1192 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1193 * thermal) decide to lower performance if it so chooses, but for now
1194 * we'll crank up the speed.
1195 */
1196
1197 result = acpi_processor_get_throttling(pr);
1198 if (result)
1199 goto end;
1200
1201 if (pr->throttling.state) {
Len Brown4be44fc2005-08-05 00:44:28 -04001202 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1203 "Disabling throttling (was T%d)\n",
1204 pr->throttling.state));
Frans Pop2a908002009-08-26 14:29:29 -07001205 result = acpi_processor_set_throttling(pr, 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (result)
1207 goto end;
1208 }
1209
Len Brown4be44fc2005-08-05 00:44:28 -04001210 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (result)
1212 pr->flags.throttling = 0;
1213
Patrick Mocheld550d982006-06-27 00:41:40 -04001214 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/* proc interface */
Zhao Yakui74cad4e2009-06-24 11:49:49 +08001218#ifdef CONFIG_ACPI_PROCFS
Len Brown4be44fc2005-08-05 00:44:28 -04001219static int acpi_processor_throttling_seq_show(struct seq_file *seq,
1220 void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001222 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001223 int i = 0;
1224 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!pr)
1227 goto end;
1228
1229 if (!(pr->throttling.state_count > 0)) {
1230 seq_puts(seq, "<not supported>\n");
1231 goto end;
1232 }
1233
1234 result = acpi_processor_get_throttling(pr);
1235
1236 if (result) {
Len Brown4be44fc2005-08-05 00:44:28 -04001237 seq_puts(seq,
1238 "Could not determine current throttling state.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto end;
1240 }
1241
1242 seq_printf(seq, "state count: %d\n"
Luming Yu01854e62007-05-26 22:49:58 +08001243 "active state: T%d\n"
Len Brownff55a9c2007-06-02 00:15:25 -04001244 "state available: T%d to T%d\n",
Luming Yu01854e62007-05-26 22:49:58 +08001245 pr->throttling.state_count, pr->throttling.state,
Len Brownff55a9c2007-06-02 00:15:25 -04001246 pr->throttling_platform_limit,
1247 pr->throttling.state_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 seq_puts(seq, "states:\n");
Luming Yu3cc26492007-07-23 12:39:28 -04001250 if (pr->throttling.acpi_processor_get_throttling ==
1251 acpi_processor_get_throttling_fadt) {
Luming Yu01854e62007-05-26 22:49:58 +08001252 for (i = 0; i < pr->throttling.state_count; i++)
1253 seq_printf(seq, " %cT%d: %02d%%\n",
Len Brownff55a9c2007-06-02 00:15:25 -04001254 (i == pr->throttling.state ? '*' : ' '), i,
1255 (pr->throttling.states[i].performance ? pr->
1256 throttling.states[i].performance / 10 : 0));
Luming Yu3cc26492007-07-23 12:39:28 -04001257 } else {
Luming Yu01854e62007-05-26 22:49:58 +08001258 for (i = 0; i < pr->throttling.state_count; i++)
1259 seq_printf(seq, " %cT%d: %02d%%\n",
Len Brownff55a9c2007-06-02 00:15:25 -04001260 (i == pr->throttling.state ? '*' : ' '), i,
1261 (int)pr->throttling.states_tss[i].
1262 freqpercentage);
Luming Yu3cc26492007-07-23 12:39:28 -04001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Len Brown4be44fc2005-08-05 00:44:28 -04001265 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Len Brown4be44fc2005-08-05 00:44:28 -04001269static int acpi_processor_throttling_open_fs(struct inode *inode,
1270 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 return single_open(file, acpi_processor_throttling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001273 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Len Brownff55a9c2007-06-02 00:15:25 -04001276static ssize_t acpi_processor_write_throttling(struct file *file,
Adrian Bunk757b1862006-01-07 13:19:00 -05001277 const char __user * buffer,
1278 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Len Brown4be44fc2005-08-05 00:44:28 -04001280 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001281 struct seq_file *m = file->private_data;
1282 struct acpi_processor *pr = m->private;
Yi Yang3d532d52008-06-14 00:54:37 -04001283 char state_string[5] = "";
1284 char *charp = NULL;
1285 size_t state_val = 0;
1286 char tmpbuf[5] = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (!pr || (count > sizeof(state_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 if (copy_from_user(state_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001292 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 state_string[count] = '\0';
Yi Yang3d532d52008-06-14 00:54:37 -04001295 if ((count > 0) && (state_string[count-1] == '\n'))
1296 state_string[count-1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Yi Yang3d532d52008-06-14 00:54:37 -04001298 charp = state_string;
1299 if ((state_string[0] == 't') || (state_string[0] == 'T'))
1300 charp++;
1301
1302 state_val = simple_strtoul(charp, NULL, 0);
1303 if (state_val >= pr->throttling.state_count)
1304 return -EINVAL;
1305
Randy Dunlap12b2b342008-06-17 09:43:41 -07001306 snprintf(tmpbuf, 5, "%zu", state_val);
Yi Yang3d532d52008-06-14 00:54:37 -04001307
1308 if (strcmp(tmpbuf, charp) != 0)
1309 return -EINVAL;
1310
Frans Pop2a908002009-08-26 14:29:29 -07001311 result = acpi_processor_set_throttling(pr, state_val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001313 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Patrick Mocheld550d982006-06-27 00:41:40 -04001315 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Jan Engelhardt070d8eb2009-01-12 00:07:55 +01001318const struct file_operations acpi_processor_throttling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001319 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001320 .open = acpi_processor_throttling_open_fs,
1321 .read = seq_read,
Arjan van de Vend479e902006-01-06 16:47:00 -05001322 .write = acpi_processor_write_throttling,
Len Brown4be44fc2005-08-05 00:44:28 -04001323 .llseek = seq_lseek,
1324 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325};
Zhao Yakui74cad4e2009-06-24 11:49:49 +08001326#endif