blob: ffc859c61393d55fb65bf5b713535b8d6a1492c4 [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>
Zhang Ruid5c68872010-10-08 13:55:15 +080035#ifdef CONFIG_ACPI_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/proc_fs.h>
37#include <linux/seq_file.h>
Zhang Ruid5c68872010-10-08 13:55:15 +080038#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/io.h>
41#include <asm/uaccess.h>
42
43#include <acpi/acpi_bus.h>
Bjorn Helgaas89595b82008-11-07 16:57:45 -070044#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/processor.h>
46
Len Browna192a952009-07-28 16:45:54 -040047#define PREFIX "ACPI: "
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050051ACPI_MODULE_NAME("processor_throttling");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Zhang Rui56c213f2009-05-11 09:35:57 +080053/* ignore_tpc:
54 * 0 -> acpi processor driver doesn't ignore _TPC values
55 * 1 -> acpi processor driver ignores _TPC values
56 */
57static int ignore_tpc;
58module_param(ignore_tpc, int, 0644);
59MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
60
Zhao Yakuie4aa5cb2008-01-28 13:54:46 +080061struct throttling_tstate {
62 unsigned int cpu; /* cpu nr */
63 int target_state; /* target T-state */
64};
65
66#define THROTTLING_PRECHANGE (1)
67#define THROTTLING_POSTCHANGE (2)
68
Len Brownff55a9c2007-06-02 00:15:25 -040069static int acpi_processor_get_throttling(struct acpi_processor *pr);
Frans Pop2a908002009-08-26 14:29:29 -070070int acpi_processor_set_throttling(struct acpi_processor *pr,
71 int state, bool force);
Luming Yu01854e62007-05-26 22:49:58 +080072
Zhao Yakui11805092008-01-28 13:53:42 +080073static int acpi_processor_update_tsd_coord(void)
74{
75 int count, count_target;
76 int retval = 0;
77 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -080078 cpumask_var_t covered_cpus;
Zhao Yakui11805092008-01-28 13:53:42 +080079 struct acpi_processor *pr, *match_pr;
80 struct acpi_tsd_package *pdomain, *match_pdomain;
81 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
82
Li Zefan79f55992009-06-15 14:58:26 +080083 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
Rusty Russell2fdf66b2008-12-31 18:08:47 -080084 return -ENOMEM;
85
Zhao Yakui11805092008-01-28 13:53:42 +080086 /*
87 * Now that we have _TSD data from all CPUs, lets setup T-state
Zhao Yakui33a2a522008-01-28 13:55:56 +080088 * coordination between all CPUs.
Zhao Yakui11805092008-01-28 13:53:42 +080089 */
90 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -070091 pr = per_cpu(processors, i);
Zhao Yakui11805092008-01-28 13:53:42 +080092 if (!pr)
93 continue;
94
95 /* Basic validity check for domain info */
96 pthrottling = &(pr->throttling);
97
98 /*
99 * If tsd package for one cpu is invalid, the coordination
100 * among all CPUs is thought as invalid.
101 * Maybe it is ugly.
102 */
103 if (!pthrottling->tsd_valid_flag) {
104 retval = -EINVAL;
105 break;
106 }
107 }
108 if (retval)
109 goto err_ret;
110
Zhao Yakui11805092008-01-28 13:53:42 +0800111 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700112 pr = per_cpu(processors, i);
Zhao Yakui11805092008-01-28 13:53:42 +0800113 if (!pr)
114 continue;
115
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800116 if (cpumask_test_cpu(i, covered_cpus))
Zhao Yakui11805092008-01-28 13:53:42 +0800117 continue;
118 pthrottling = &pr->throttling;
119
120 pdomain = &(pthrottling->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800121 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
122 cpumask_set_cpu(i, covered_cpus);
Zhao Yakui11805092008-01-28 13:53:42 +0800123 /*
124 * If the number of processor in the TSD domain is 1, it is
125 * unnecessary to parse the coordination for this CPU.
126 */
127 if (pdomain->num_processors <= 1)
128 continue;
129
130 /* Validate the Domain info */
131 count_target = pdomain->num_processors;
132 count = 1;
133
134 for_each_possible_cpu(j) {
135 if (i == j)
136 continue;
137
Mike Travis706546d2008-06-09 16:22:23 -0700138 match_pr = per_cpu(processors, j);
Zhao Yakui11805092008-01-28 13:53:42 +0800139 if (!match_pr)
140 continue;
141
142 match_pthrottling = &(match_pr->throttling);
143 match_pdomain = &(match_pthrottling->domain_info);
144 if (match_pdomain->domain != pdomain->domain)
145 continue;
146
147 /* Here i and j are in the same domain.
148 * If two TSD packages have the same domain, they
149 * should have the same num_porcessors and
150 * coordination type. Otherwise it will be regarded
151 * as illegal.
152 */
153 if (match_pdomain->num_processors != count_target) {
154 retval = -EINVAL;
155 goto err_ret;
156 }
157
158 if (pdomain->coord_type != match_pdomain->coord_type) {
159 retval = -EINVAL;
160 goto err_ret;
161 }
162
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800163 cpumask_set_cpu(j, covered_cpus);
164 cpumask_set_cpu(j, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800165 count++;
166 }
167 for_each_possible_cpu(j) {
168 if (i == j)
169 continue;
170
Mike Travis706546d2008-06-09 16:22:23 -0700171 match_pr = per_cpu(processors, j);
Zhao Yakui11805092008-01-28 13:53:42 +0800172 if (!match_pr)
173 continue;
174
175 match_pthrottling = &(match_pr->throttling);
176 match_pdomain = &(match_pthrottling->domain_info);
177 if (match_pdomain->domain != pdomain->domain)
178 continue;
179
180 /*
181 * If some CPUS have the same domain, they
182 * will have the same shared_cpu_map.
183 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800184 cpumask_copy(match_pthrottling->shared_cpu_map,
185 pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800186 }
187 }
188
189err_ret:
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800190 free_cpumask_var(covered_cpus);
191
Zhao Yakui11805092008-01-28 13:53:42 +0800192 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700193 pr = per_cpu(processors, i);
Zhao Yakui11805092008-01-28 13:53:42 +0800194 if (!pr)
195 continue;
196
197 /*
198 * Assume no coordination on any error parsing domain info.
199 * The coordination type will be forced as SW_ALL.
200 */
201 if (retval) {
202 pthrottling = &(pr->throttling);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800203 cpumask_clear(pthrottling->shared_cpu_map);
204 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800205 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
206 }
207 }
208
209 return retval;
210}
211
212/*
213 * Update the T-state coordination after the _TSD
214 * data for all cpus is obtained.
215 */
216void acpi_processor_throttling_init(void)
217{
218 if (acpi_processor_update_tsd_coord())
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
220 "Assume no T-state coordination\n"));
221
222 return;
223}
224
Zhao Yakuie4aa5cb2008-01-28 13:54:46 +0800225static int acpi_processor_throttling_notifier(unsigned long event, void *data)
226{
227 struct throttling_tstate *p_tstate = data;
228 struct acpi_processor *pr;
229 unsigned int cpu ;
230 int target_state;
231 struct acpi_processor_limit *p_limit;
232 struct acpi_processor_throttling *p_throttling;
233
234 cpu = p_tstate->cpu;
Mike Travis706546d2008-06-09 16:22:23 -0700235 pr = per_cpu(processors, cpu);
Zhao Yakuie4aa5cb2008-01-28 13:54:46 +0800236 if (!pr) {
237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));
238 return 0;
239 }
240 if (!pr->flags.throttling) {
241 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is "
242 "unsupported on CPU %d\n", cpu));
243 return 0;
244 }
245 target_state = p_tstate->target_state;
246 p_throttling = &(pr->throttling);
247 switch (event) {
248 case THROTTLING_PRECHANGE:
249 /*
250 * Prechange event is used to choose one proper t-state,
251 * which meets the limits of thermal, user and _TPC.
252 */
253 p_limit = &pr->limit;
254 if (p_limit->thermal.tx > target_state)
255 target_state = p_limit->thermal.tx;
256 if (p_limit->user.tx > target_state)
257 target_state = p_limit->user.tx;
258 if (pr->throttling_platform_limit > target_state)
259 target_state = pr->throttling_platform_limit;
260 if (target_state >= p_throttling->state_count) {
261 printk(KERN_WARNING
262 "Exceed the limit of T-state \n");
263 target_state = p_throttling->state_count - 1;
264 }
265 p_tstate->target_state = target_state;
266 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:"
267 "target T-state of CPU %d is T%d\n",
268 cpu, target_state));
269 break;
270 case THROTTLING_POSTCHANGE:
271 /*
272 * Postchange event is only used to update the
273 * T-state flag of acpi_processor_throttling.
274 */
275 p_throttling->state = target_state;
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:"
277 "CPU %d is switched to T%d\n",
278 cpu, target_state));
279 break;
280 default:
281 printk(KERN_WARNING
282 "Unsupported Throttling notifier event\n");
283 break;
284 }
285
286 return 0;
287}
288
Len Brownc30c6202007-07-25 00:57:46 -0400289/*
290 * _TPC - Throttling Present Capabilities
291 */
Luming Yu01854e62007-05-26 22:49:58 +0800292static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
293{
294 acpi_status status = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400295 unsigned long long tpc = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800296
Len Brownff55a9c2007-06-02 00:15:25 -0400297 if (!pr)
Luming Yu01854e62007-05-26 22:49:58 +0800298 return -EINVAL;
Zhang Rui56c213f2009-05-11 09:35:57 +0800299
300 if (ignore_tpc)
301 goto end;
302
Luming Yu01854e62007-05-26 22:49:58 +0800303 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
Len Brownc30c6202007-07-25 00:57:46 -0400304 if (ACPI_FAILURE(status)) {
305 if (status != AE_NOT_FOUND) {
306 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
307 }
Luming Yu01854e62007-05-26 22:49:58 +0800308 return -ENODEV;
309 }
Zhang Rui56c213f2009-05-11 09:35:57 +0800310
311end:
Luming Yu01854e62007-05-26 22:49:58 +0800312 pr->throttling_platform_limit = (int)tpc;
313 return 0;
314}
315
316int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
317{
Zhao Yakuief54d5a2007-11-15 16:59:30 +0800318 int result = 0;
319 int throttling_limit;
320 int current_state;
321 struct acpi_processor_limit *limit;
322 int target_state;
323
Zhang Rui56c213f2009-05-11 09:35:57 +0800324 if (ignore_tpc)
325 return 0;
326
Zhao Yakuief54d5a2007-11-15 16:59:30 +0800327 result = acpi_processor_get_platform_limit(pr);
328 if (result) {
329 /* Throttling Limit is unsupported */
330 return result;
331 }
332
333 throttling_limit = pr->throttling_platform_limit;
334 if (throttling_limit >= pr->throttling.state_count) {
335 /* Uncorrect Throttling Limit */
336 return -EINVAL;
337 }
338
339 current_state = pr->throttling.state;
340 if (current_state > throttling_limit) {
341 /*
342 * The current state can meet the requirement of
343 * _TPC limit. But it is reasonable that OSPM changes
344 * t-states from high to low for better performance.
345 * Of course the limit condition of thermal
346 * and user should be considered.
347 */
348 limit = &pr->limit;
349 target_state = throttling_limit;
350 if (limit->thermal.tx > target_state)
351 target_state = limit->thermal.tx;
352 if (limit->user.tx > target_state)
353 target_state = limit->user.tx;
354 } else if (current_state == throttling_limit) {
355 /*
356 * Unnecessary to change the throttling state
357 */
358 return 0;
359 } else {
360 /*
361 * If the current state is lower than the limit of _TPC, it
362 * will be forced to switch to the throttling state defined
363 * by throttling_platfor_limit.
364 * Because the previous state meets with the limit condition
365 * of thermal and user, it is unnecessary to check it again.
366 */
367 target_state = throttling_limit;
368 }
Frans Pop2a908002009-08-26 14:29:29 -0700369 return acpi_processor_set_throttling(pr, target_state, false);
Luming Yu01854e62007-05-26 22:49:58 +0800370}
371
Len Brownc30c6202007-07-25 00:57:46 -0400372/*
373 * _PTC - Processor Throttling Control (and status) register location
374 */
Luming Yu01854e62007-05-26 22:49:58 +0800375static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
376{
377 int result = 0;
378 acpi_status status = 0;
379 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
380 union acpi_object *ptc = NULL;
381 union acpi_object obj = { 0 };
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800382 struct acpi_processor_throttling *throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800383
384 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
385 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400386 if (status != AE_NOT_FOUND) {
387 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
388 }
Luming Yu01854e62007-05-26 22:49:58 +0800389 return -ENODEV;
390 }
391
392 ptc = (union acpi_object *)buffer.pointer;
393 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
394 || (ptc->package.count != 2)) {
395 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
396 result = -EFAULT;
397 goto end;
398 }
399
400 /*
401 * control_register
402 */
403
404 obj = ptc->package.elements[0];
405
406 if ((obj.type != ACPI_TYPE_BUFFER)
407 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
408 || (obj.buffer.pointer == NULL)) {
Len Brownff55a9c2007-06-02 00:15:25 -0400409 printk(KERN_ERR PREFIX
410 "Invalid _PTC data (control_register)\n");
Luming Yu01854e62007-05-26 22:49:58 +0800411 result = -EFAULT;
412 goto end;
413 }
414 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
415 sizeof(struct acpi_ptc_register));
416
417 /*
418 * status_register
419 */
420
421 obj = ptc->package.elements[1];
422
423 if ((obj.type != ACPI_TYPE_BUFFER)
424 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
425 || (obj.buffer.pointer == NULL)) {
426 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
427 result = -EFAULT;
428 goto end;
429 }
430
431 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
Len Brownff55a9c2007-06-02 00:15:25 -0400432 sizeof(struct acpi_ptc_register));
Luming Yu01854e62007-05-26 22:49:58 +0800433
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800434 throttling = &pr->throttling;
435
436 if ((throttling->control_register.bit_width +
437 throttling->control_register.bit_offset) > 32) {
438 printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
439 result = -EFAULT;
440 goto end;
441 }
442
443 if ((throttling->status_register.bit_width +
444 throttling->status_register.bit_offset) > 32) {
445 printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
446 result = -EFAULT;
447 goto end;
448 }
449
Len Brownff55a9c2007-06-02 00:15:25 -0400450 end:
Luming Yu01854e62007-05-26 22:49:58 +0800451 kfree(buffer.pointer);
452
453 return result;
454}
Len Brownc30c6202007-07-25 00:57:46 -0400455
456/*
457 * _TSS - Throttling Supported States
458 */
Luming Yu01854e62007-05-26 22:49:58 +0800459static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
460{
461 int result = 0;
462 acpi_status status = AE_OK;
463 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
464 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
465 struct acpi_buffer state = { 0, NULL };
466 union acpi_object *tss = NULL;
467 int i;
468
469 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
470 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400471 if (status != AE_NOT_FOUND) {
472 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
473 }
Luming Yu01854e62007-05-26 22:49:58 +0800474 return -ENODEV;
475 }
476
477 tss = buffer.pointer;
478 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
479 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
480 result = -EFAULT;
481 goto end;
482 }
483
484 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
485 tss->package.count));
486
487 pr->throttling.state_count = tss->package.count;
488 pr->throttling.states_tss =
489 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
490 GFP_KERNEL);
491 if (!pr->throttling.states_tss) {
492 result = -ENOMEM;
493 goto end;
494 }
495
496 for (i = 0; i < pr->throttling.state_count; i++) {
497
Len Brownff55a9c2007-06-02 00:15:25 -0400498 struct acpi_processor_tx_tss *tx =
499 (struct acpi_processor_tx_tss *)&(pr->throttling.
500 states_tss[i]);
Luming Yu01854e62007-05-26 22:49:58 +0800501
502 state.length = sizeof(struct acpi_processor_tx_tss);
503 state.pointer = tx;
504
505 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
506
507 status = acpi_extract_package(&(tss->package.elements[i]),
508 &format, &state);
509 if (ACPI_FAILURE(status)) {
510 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
511 result = -EFAULT;
512 kfree(pr->throttling.states_tss);
513 goto end;
514 }
515
516 if (!tx->freqpercentage) {
517 printk(KERN_ERR PREFIX
Len Brownff55a9c2007-06-02 00:15:25 -0400518 "Invalid _TSS data: freq is zero\n");
Luming Yu01854e62007-05-26 22:49:58 +0800519 result = -EFAULT;
520 kfree(pr->throttling.states_tss);
521 goto end;
522 }
523 }
524
525 end:
526 kfree(buffer.pointer);
527
528 return result;
529}
Len Brownc30c6202007-07-25 00:57:46 -0400530
531/*
532 * _TSD - T-State Dependencies
533 */
Len Brownff55a9c2007-06-02 00:15:25 -0400534static int acpi_processor_get_tsd(struct acpi_processor *pr)
Luming Yu01854e62007-05-26 22:49:58 +0800535{
536 int result = 0;
537 acpi_status status = AE_OK;
Len Brownff55a9c2007-06-02 00:15:25 -0400538 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
539 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
540 struct acpi_buffer state = { 0, NULL };
541 union acpi_object *tsd = NULL;
Luming Yu01854e62007-05-26 22:49:58 +0800542 struct acpi_tsd_package *pdomain;
Zhao Yakui11805092008-01-28 13:53:42 +0800543 struct acpi_processor_throttling *pthrottling;
544
545 pthrottling = &pr->throttling;
546 pthrottling->tsd_valid_flag = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800547
548 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
549 if (ACPI_FAILURE(status)) {
Len Brownc30c6202007-07-25 00:57:46 -0400550 if (status != AE_NOT_FOUND) {
551 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
552 }
Luming Yu01854e62007-05-26 22:49:58 +0800553 return -ENODEV;
554 }
555
556 tsd = buffer.pointer;
557 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800558 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
Luming Yu01854e62007-05-26 22:49:58 +0800559 result = -EFAULT;
560 goto end;
561 }
562
563 if (tsd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800564 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
Luming Yu01854e62007-05-26 22:49:58 +0800565 result = -EFAULT;
566 goto end;
567 }
568
569 pdomain = &(pr->throttling.domain_info);
570
571 state.length = sizeof(struct acpi_tsd_package);
572 state.pointer = pdomain;
573
574 status = acpi_extract_package(&(tsd->package.elements[0]),
Len Brownff55a9c2007-06-02 00:15:25 -0400575 &format, &state);
Luming Yu01854e62007-05-26 22:49:58 +0800576 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800577 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
Luming Yu01854e62007-05-26 22:49:58 +0800578 result = -EFAULT;
579 goto end;
580 }
581
582 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800583 printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n");
Luming Yu01854e62007-05-26 22:49:58 +0800584 result = -EFAULT;
585 goto end;
586 }
587
588 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800589 printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");
Luming Yu01854e62007-05-26 22:49:58 +0800590 result = -EFAULT;
591 goto end;
592 }
593
Zhao Yakui11805092008-01-28 13:53:42 +0800594 pthrottling = &pr->throttling;
595 pthrottling->tsd_valid_flag = 1;
596 pthrottling->shared_type = pdomain->coord_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800597 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +0800598 /*
599 * If the coordination type is not defined in ACPI spec,
600 * the tsd_valid_flag will be clear and coordination type
601 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
602 */
603 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
604 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
605 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
606 pthrottling->tsd_valid_flag = 0;
607 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
608 }
609
Len Brownff55a9c2007-06-02 00:15:25 -0400610 end:
Luming Yu01854e62007-05-26 22:49:58 +0800611 kfree(buffer.pointer);
612 return result;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/* --------------------------------------------------------------------------
616 Throttling Control
617 -------------------------------------------------------------------------- */
Luming Yu01854e62007-05-26 22:49:58 +0800618static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Len Brown4be44fc2005-08-05 00:44:28 -0400620 int state = 0;
621 u32 value = 0;
622 u32 duty_mask = 0;
623 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (!pr->flags.throttling)
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 pr->throttling.state = 0;
632
633 duty_mask = pr->throttling.state_count - 1;
634
635 duty_mask <<= pr->throttling.duty_offset;
636
637 local_irq_disable();
638
639 value = inl(pr->throttling.address);
640
641 /*
642 * Compute the current throttling state when throttling is enabled
643 * (bit 4 is on).
644 */
645 if (value & 0x10) {
646 duty_value = value & duty_mask;
647 duty_value >>= pr->throttling.duty_offset;
648
649 if (duty_value)
Len Brown4be44fc2005-08-05 00:44:28 -0400650 state = pr->throttling.state_count - duty_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
653 pr->throttling.state = state;
654
655 local_irq_enable();
656
657 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400658 "Throttling state is T%d (%d%% throttling applied)\n",
659 state, pr->throttling.states[state].performance));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Patrick Mocheld550d982006-06-27 00:41:40 -0400661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800664#ifdef CONFIG_X86
665static int acpi_throttling_rdmsr(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800666 u64 *value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800667{
668 struct cpuinfo_x86 *c;
669 u64 msr_high, msr_low;
670 unsigned int cpu;
671 u64 msr = 0;
672 int ret = -1;
673
674 cpu = pr->id;
675 c = &cpu_data(cpu);
676
677 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
678 !cpu_has(c, X86_FEATURE_ACPI)) {
679 printk(KERN_ERR PREFIX
680 "HARDWARE addr space,NOT supported yet\n");
681 } else {
682 msr_low = 0;
683 msr_high = 0;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800684 rdmsr_safe(MSR_IA32_THERM_CONTROL,
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800685 (u32 *)&msr_low , (u32 *) &msr_high);
686 msr = (msr_high << 32) | msr_low;
Lin Ming439913f2010-01-28 10:53:19 +0800687 *value = (u64) msr;
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800688 ret = 0;
689 }
690 return ret;
691}
692
Lin Ming439913f2010-01-28 10:53:19 +0800693static int acpi_throttling_wrmsr(struct acpi_processor *pr, u64 value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800694{
695 struct cpuinfo_x86 *c;
696 unsigned int cpu;
697 int ret = -1;
698 u64 msr;
699
700 cpu = pr->id;
701 c = &cpu_data(cpu);
702
703 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
704 !cpu_has(c, X86_FEATURE_ACPI)) {
705 printk(KERN_ERR PREFIX
706 "HARDWARE addr space,NOT supported yet\n");
707 } else {
708 msr = value;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800709 wrmsr_safe(MSR_IA32_THERM_CONTROL,
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800710 msr & 0xffffffff, msr >> 32);
711 ret = 0;
712 }
713 return ret;
714}
715#else
716static int acpi_throttling_rdmsr(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800717 u64 *value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800718{
719 printk(KERN_ERR PREFIX
720 "HARDWARE addr space,NOT supported yet\n");
721 return -1;
722}
723
Lin Ming439913f2010-01-28 10:53:19 +0800724static int acpi_throttling_wrmsr(struct acpi_processor *pr, u64 value)
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800725{
726 printk(KERN_ERR PREFIX
727 "HARDWARE addr space,NOT supported yet\n");
728 return -1;
729}
730#endif
731
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800732static int acpi_read_throttling_status(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800733 u64 *value)
Luming Yu01854e62007-05-26 22:49:58 +0800734{
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800735 u32 bit_width, bit_offset;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800736 u64 ptc_value;
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800737 u64 ptc_mask;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800738 struct acpi_processor_throttling *throttling;
739 int ret = -1;
740
741 throttling = &pr->throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800742 switch (throttling->status_register.space_id) {
743 case ACPI_ADR_SPACE_SYSTEM_IO:
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800744 ptc_value = 0;
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800745 bit_width = throttling->status_register.bit_width;
746 bit_offset = throttling->status_register.bit_offset;
747
Len Brownff55a9c2007-06-02 00:15:25 -0400748 acpi_os_read_port((acpi_io_address) throttling->status_register.
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800749 address, (u32 *) &ptc_value,
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800750 (u32) (bit_width + bit_offset));
751 ptc_mask = (1 << bit_width) - 1;
Lin Ming439913f2010-01-28 10:53:19 +0800752 *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800753 ret = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800754 break;
755 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800756 ret = acpi_throttling_rdmsr(pr, value);
Luming Yu01854e62007-05-26 22:49:58 +0800757 break;
758 default:
759 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400760 (u32) (throttling->status_register.space_id));
Luming Yu01854e62007-05-26 22:49:58 +0800761 }
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800762 return ret;
Luming Yu01854e62007-05-26 22:49:58 +0800763}
764
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800765static int acpi_write_throttling_state(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800766 u64 value)
Luming Yu01854e62007-05-26 22:49:58 +0800767{
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800768 u32 bit_width, bit_offset;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800769 u64 ptc_value;
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800770 u64 ptc_mask;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800771 struct acpi_processor_throttling *throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800772 int ret = -1;
773
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800774 throttling = &pr->throttling;
Luming Yu01854e62007-05-26 22:49:58 +0800775 switch (throttling->control_register.space_id) {
776 case ACPI_ADR_SPACE_SYSTEM_IO:
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800777 bit_width = throttling->control_register.bit_width;
778 bit_offset = throttling->control_register.bit_offset;
779 ptc_mask = (1 << bit_width) - 1;
780 ptc_value = value & ptc_mask;
781
Len Brownff55a9c2007-06-02 00:15:25 -0400782 acpi_os_write_port((acpi_io_address) throttling->
Zhao Yakui9bcb2722007-11-15 17:05:05 +0800783 control_register.address,
784 (u32) (ptc_value << bit_offset),
785 (u32) (bit_width + bit_offset));
Luming Yu01854e62007-05-26 22:49:58 +0800786 ret = 0;
787 break;
788 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Zhao Yakuif79f06a2007-11-15 17:06:36 +0800789 ret = acpi_throttling_wrmsr(pr, value);
Luming Yu01854e62007-05-26 22:49:58 +0800790 break;
791 default:
792 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
Len Brownff55a9c2007-06-02 00:15:25 -0400793 (u32) (throttling->control_register.space_id));
Luming Yu01854e62007-05-26 22:49:58 +0800794 }
795 return ret;
796}
797
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800798static int acpi_get_throttling_state(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800799 u64 value)
Luming Yu01854e62007-05-26 22:49:58 +0800800{
801 int i;
802
803 for (i = 0; i < pr->throttling.state_count; i++) {
Len Brownff55a9c2007-06-02 00:15:25 -0400804 struct acpi_processor_tx_tss *tx =
805 (struct acpi_processor_tx_tss *)&(pr->throttling.
806 states_tss[i]);
807 if (tx->control == value)
Len Brown53af9cf2009-03-15 23:36:38 -0400808 return i;
Luming Yu01854e62007-05-26 22:49:58 +0800809 }
Len Brown53af9cf2009-03-15 23:36:38 -0400810 return -1;
Luming Yu01854e62007-05-26 22:49:58 +0800811}
812
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800813static int acpi_get_throttling_value(struct acpi_processor *pr,
Lin Ming439913f2010-01-28 10:53:19 +0800814 int state, u64 *value)
Luming Yu01854e62007-05-26 22:49:58 +0800815{
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800816 int ret = -1;
817
Len Brownff55a9c2007-06-02 00:15:25 -0400818 if (state >= 0 && state <= pr->throttling.state_count) {
819 struct acpi_processor_tx_tss *tx =
820 (struct acpi_processor_tx_tss *)&(pr->throttling.
821 states_tss[state]);
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800822 *value = tx->control;
823 ret = 0;
Luming Yu01854e62007-05-26 22:49:58 +0800824 }
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800825 return ret;
Luming Yu01854e62007-05-26 22:49:58 +0800826}
827
828static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
829{
830 int state = 0;
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800831 int ret;
Lin Ming439913f2010-01-28 10:53:19 +0800832 u64 value;
Luming Yu01854e62007-05-26 22:49:58 +0800833
Luming Yu01854e62007-05-26 22:49:58 +0800834 if (!pr)
835 return -EINVAL;
836
837 if (!pr->flags.throttling)
838 return -ENODEV;
839
840 pr->throttling.state = 0;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800841
Zhao Yakui0753f6e2007-11-15 17:03:46 +0800842 value = 0;
843 ret = acpi_read_throttling_status(pr, &value);
844 if (ret >= 0) {
Len Brownff55a9c2007-06-02 00:15:25 -0400845 state = acpi_get_throttling_state(pr, value);
Zhang Rui4973b222009-05-11 09:36:01 +0800846 if (state == -1) {
Frans Popbdf57de2009-08-26 14:29:30 -0700847 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
848 "Invalid throttling state, reset\n"));
Zhang Rui4973b222009-05-11 09:36:01 +0800849 state = 0;
Frans Pop2a908002009-08-26 14:29:29 -0700850 ret = acpi_processor_set_throttling(pr, state, true);
Zhang Rui4973b222009-05-11 09:36:01 +0800851 if (ret)
852 return ret;
853 }
Luming Yu01854e62007-05-26 22:49:58 +0800854 pr->throttling.state = state;
855 }
Luming Yu01854e62007-05-26 22:49:58 +0800856
857 return 0;
858}
859
Luming Yu01854e62007-05-26 22:49:58 +0800860static int acpi_processor_get_throttling(struct acpi_processor *pr)
861{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800862 cpumask_var_t saved_mask;
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800863 int ret;
864
Zhao Yakui87654272008-01-28 13:53:30 +0800865 if (!pr)
866 return -EINVAL;
867
868 if (!pr->flags.throttling)
869 return -ENODEV;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800870
871 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
872 return -ENOMEM;
873
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800874 /*
875 * Migrate task to the cpu pointed by pr.
876 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800877 cpumask_copy(saved_mask, &current->cpus_allowed);
878 /* FIXME: use work_on_cpu() */
Zhao Yakuidaef1f32011-01-10 16:35:44 +0800879 if (set_cpus_allowed_ptr(current, cpumask_of(pr->id))) {
880 /* Can't migrate to the target pr->id CPU. Exit */
881 free_cpumask_var(saved_mask);
882 return -ENODEV;
883 }
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800884 ret = pr->throttling.acpi_processor_get_throttling(pr);
885 /* restore the previous state */
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800886 set_cpus_allowed_ptr(current, saved_mask);
887 free_cpumask_var(saved_mask);
Zhao Yakui357dc4c2007-11-29 16:22:43 +0800888
889 return ret;
Luming Yu01854e62007-05-26 22:49:58 +0800890}
891
Zhao Yakui22cc5012007-11-15 17:02:03 +0800892static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
893{
894 int i, step;
895
896 if (!pr->throttling.address) {
897 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
898 return -EINVAL;
899 } else if (!pr->throttling.duty_width) {
900 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
901 return -EINVAL;
902 }
903 /* TBD: Support duty_cycle values that span bit 4. */
904 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
905 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
906 return -EINVAL;
907 }
908
909 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
910
911 /*
912 * Compute state values. Note that throttling displays a linear power
913 * performance relationship (at 50% performance the CPU will consume
914 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
915 */
916
917 step = (1000 / pr->throttling.state_count);
918
919 for (i = 0; i < pr->throttling.state_count; i++) {
920 pr->throttling.states[i].performance = 1000 - step * i;
921 pr->throttling.states[i].power = 1000 - step * i;
922 }
923 return 0;
924}
925
Adrian Bunk6c5cf8a2007-07-03 00:53:12 -0400926static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
Frans Pop2a908002009-08-26 14:29:29 -0700927 int state, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Len Brown4be44fc2005-08-05 00:44:28 -0400929 u32 value = 0;
930 u32 duty_mask = 0;
931 u32 duty_value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400934 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400937 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (!pr->flags.throttling)
Patrick Mocheld550d982006-06-27 00:41:40 -0400940 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Frans Pop2a908002009-08-26 14:29:29 -0700942 if (!force && (state == pr->throttling.state))
Patrick Mocheld550d982006-06-27 00:41:40 -0400943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Luming Yu01854e62007-05-26 22:49:58 +0800945 if (state < pr->throttling_platform_limit)
946 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
948 * Calculate the duty_value and duty_mask.
949 */
950 if (state) {
951 duty_value = pr->throttling.state_count - state;
952
953 duty_value <<= pr->throttling.duty_offset;
954
955 /* Used to clear all duty_value bits */
956 duty_mask = pr->throttling.state_count - 1;
957
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300958 duty_mask <<= acpi_gbl_FADT.duty_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 duty_mask = ~duty_mask;
960 }
961
962 local_irq_disable();
963
964 /*
965 * Disable throttling by writing a 0 to bit 4. Note that we must
966 * turn it off before you can change the duty_value.
967 */
968 value = inl(pr->throttling.address);
969 if (value & 0x10) {
970 value &= 0xFFFFFFEF;
971 outl(value, pr->throttling.address);
972 }
973
974 /*
975 * Write the new duty_value and then enable throttling. Note
976 * that a state value of 0 leaves throttling disabled.
977 */
978 if (state) {
979 value &= duty_mask;
980 value |= duty_value;
981 outl(value, pr->throttling.address);
982
983 value |= 0x00000010;
984 outl(value, pr->throttling.address);
985 }
986
987 pr->throttling.state = state;
988
989 local_irq_enable();
990
991 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400992 "Throttling state set to T%d (%d%%)\n", state,
993 (pr->throttling.states[state].performance ? pr->
994 throttling.states[state].performance / 10 : 0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Patrick Mocheld550d982006-06-27 00:41:40 -0400996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Adrian Bunk6c5cf8a2007-07-03 00:53:12 -0400999static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
Frans Pop2a908002009-08-26 14:29:29 -07001000 int state, bool force)
Luming Yu01854e62007-05-26 22:49:58 +08001001{
Zhao Yakui0753f6e2007-11-15 17:03:46 +08001002 int ret;
Lin Ming439913f2010-01-28 10:53:19 +08001003 u64 value;
Luming Yu01854e62007-05-26 22:49:58 +08001004
1005 if (!pr)
1006 return -EINVAL;
1007
1008 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1009 return -EINVAL;
1010
1011 if (!pr->flags.throttling)
1012 return -ENODEV;
1013
Frans Pop2a908002009-08-26 14:29:29 -07001014 if (!force && (state == pr->throttling.state))
Luming Yu01854e62007-05-26 22:49:58 +08001015 return 0;
1016
1017 if (state < pr->throttling_platform_limit)
1018 return -EPERM;
1019
Zhao Yakui0753f6e2007-11-15 17:03:46 +08001020 value = 0;
1021 ret = acpi_get_throttling_value(pr, state, &value);
1022 if (ret >= 0) {
1023 acpi_write_throttling_state(pr, value);
Luming Yu01854e62007-05-26 22:49:58 +08001024 pr->throttling.state = state;
1025 }
Luming Yu01854e62007-05-26 22:49:58 +08001026
1027 return 0;
1028}
1029
Frans Pop2a908002009-08-26 14:29:29 -07001030int acpi_processor_set_throttling(struct acpi_processor *pr,
1031 int state, bool force)
Luming Yu01854e62007-05-26 22:49:58 +08001032{
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001033 cpumask_var_t saved_mask;
Len Brown3391a762008-02-02 03:56:18 -05001034 int ret = 0;
Zhao Yakui33a2a522008-01-28 13:55:56 +08001035 unsigned int i;
1036 struct acpi_processor *match_pr;
1037 struct acpi_processor_throttling *p_throttling;
1038 struct throttling_tstate t_state;
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001039 cpumask_var_t online_throttling_cpus;
Zhao Yakui87654272008-01-28 13:53:30 +08001040
1041 if (!pr)
1042 return -EINVAL;
1043
1044 if (!pr->flags.throttling)
1045 return -ENODEV;
1046
1047 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1048 return -EINVAL;
1049
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001050 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
1051 return -ENOMEM;
1052
1053 if (!alloc_cpumask_var(&online_throttling_cpus, GFP_KERNEL)) {
1054 free_cpumask_var(saved_mask);
1055 return -ENOMEM;
1056 }
1057
Zhao Yakuidaef1f32011-01-10 16:35:44 +08001058 if (cpu_is_offline(pr->id)) {
1059 /*
1060 * the cpu pointed by pr->id is offline. Unnecessary to change
1061 * the throttling state any more.
1062 */
1063 return -ENODEV;
1064 }
1065
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001066 cpumask_copy(saved_mask, &current->cpus_allowed);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001067 t_state.target_state = state;
1068 p_throttling = &(pr->throttling);
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001069 cpumask_and(online_throttling_cpus, cpu_online_mask,
1070 p_throttling->shared_cpu_map);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001071 /*
1072 * The throttling notifier will be called for every
1073 * affected cpu in order to get one proper T-state.
1074 * The notifier event is THROTTLING_PRECHANGE.
1075 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001076 for_each_cpu(i, online_throttling_cpus) {
Zhao Yakui33a2a522008-01-28 13:55:56 +08001077 t_state.cpu = i;
1078 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1079 &t_state);
1080 }
1081 /*
1082 * The function of acpi_processor_set_throttling will be called
1083 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1084 * it is necessary to call it for every affected cpu. Otherwise
1085 * it can be called only for the cpu pointed by pr.
1086 */
1087 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001088 /* FIXME: use work_on_cpu() */
Zhao Yakuidaef1f32011-01-10 16:35:44 +08001089 if (set_cpus_allowed_ptr(current, cpumask_of(pr->id))) {
1090 /* Can't migrate to the pr->id CPU. Exit */
1091 ret = -ENODEV;
1092 goto exit;
1093 }
Zhao Yakui33a2a522008-01-28 13:55:56 +08001094 ret = p_throttling->acpi_processor_set_throttling(pr,
Frans Pop2a908002009-08-26 14:29:29 -07001095 t_state.target_state, force);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001096 } else {
1097 /*
1098 * When the T-state coordination is SW_ALL or HW_ALL,
1099 * it is necessary to set T-state for every affected
1100 * cpus.
1101 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001102 for_each_cpu(i, online_throttling_cpus) {
Mike Travis706546d2008-06-09 16:22:23 -07001103 match_pr = per_cpu(processors, i);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001104 /*
1105 * If the pointer is invalid, we will report the
1106 * error message and continue.
1107 */
1108 if (!match_pr) {
1109 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1110 "Invalid Pointer for CPU %d\n", i));
1111 continue;
1112 }
1113 /*
1114 * If the throttling control is unsupported on CPU i,
1115 * we will report the error message and continue.
1116 */
1117 if (!match_pr->flags.throttling) {
1118 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1119 "Throttling Controll is unsupported "
1120 "on CPU %d\n", i));
1121 continue;
1122 }
1123 t_state.cpu = i;
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001124 /* FIXME: use work_on_cpu() */
Zhao Yakuidaef1f32011-01-10 16:35:44 +08001125 if (set_cpus_allowed_ptr(current, cpumask_of(i)))
1126 continue;
Zhao Yakui33a2a522008-01-28 13:55:56 +08001127 ret = match_pr->throttling.
1128 acpi_processor_set_throttling(
Frans Pop2a908002009-08-26 14:29:29 -07001129 match_pr, t_state.target_state, force);
Zhao Yakui33a2a522008-01-28 13:55:56 +08001130 }
1131 }
1132 /*
1133 * After the set_throttling is called, the
1134 * throttling notifier is called for every
1135 * affected cpu to update the T-states.
1136 * The notifier event is THROTTLING_POSTCHANGE
1137 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001138 for_each_cpu(i, online_throttling_cpus) {
Zhao Yakui33a2a522008-01-28 13:55:56 +08001139 t_state.cpu = i;
1140 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1141 &t_state);
1142 }
Zhao Yakui357dc4c2007-11-29 16:22:43 +08001143 /* restore the previous state */
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001144 /* FIXME: use work_on_cpu() */
1145 set_cpus_allowed_ptr(current, saved_mask);
Zhao Yakuidaef1f32011-01-10 16:35:44 +08001146exit:
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001147 free_cpumask_var(online_throttling_cpus);
1148 free_cpumask_var(saved_mask);
Zhao Yakui357dc4c2007-11-29 16:22:43 +08001149 return ret;
Luming Yu01854e62007-05-26 22:49:58 +08001150}
1151
Len Brown4be44fc2005-08-05 00:44:28 -04001152int acpi_processor_get_throttling_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Len Brown4be44fc2005-08-05 00:44:28 -04001154 int result = 0;
Zhao Yakui11805092008-01-28 13:53:42 +08001155 struct acpi_processor_throttling *pthrottling;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001158 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1159 pr->throttling.address,
1160 pr->throttling.duty_offset,
1161 pr->throttling.duty_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Len Brownc30c6202007-07-25 00:57:46 -04001163 /*
1164 * Evaluate _PTC, _TSS and _TPC
1165 * They must all be present or none of them can be used.
1166 */
1167 if (acpi_processor_get_throttling_control(pr) ||
1168 acpi_processor_get_throttling_states(pr) ||
1169 acpi_processor_get_platform_limit(pr))
1170 {
Len Brownff55a9c2007-06-02 00:15:25 -04001171 pr->throttling.acpi_processor_get_throttling =
1172 &acpi_processor_get_throttling_fadt;
1173 pr->throttling.acpi_processor_set_throttling =
1174 &acpi_processor_set_throttling_fadt;
Alexey Starikovskiyd1154be2008-01-15 00:47:47 -05001175 if (acpi_processor_get_fadt_info(pr))
1176 return 0;
Luming Yu01854e62007-05-26 22:49:58 +08001177 } else {
Len Brownff55a9c2007-06-02 00:15:25 -04001178 pr->throttling.acpi_processor_get_throttling =
1179 &acpi_processor_get_throttling_ptc;
1180 pr->throttling.acpi_processor_set_throttling =
1181 &acpi_processor_set_throttling_ptc;
Luming Yu01854e62007-05-26 22:49:58 +08001182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Zhao Yakui11805092008-01-28 13:53:42 +08001184 /*
1185 * If TSD package for one CPU can't be parsed successfully, it means
1186 * that this CPU will have no coordination with other CPUs.
1187 */
1188 if (acpi_processor_get_tsd(pr)) {
1189 pthrottling = &pr->throttling;
1190 pthrottling->tsd_valid_flag = 0;
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001191 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
Zhao Yakui11805092008-01-28 13:53:42 +08001192 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1193 }
Len Brownc30c6202007-07-25 00:57:46 -04001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /*
1196 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1197 * This shouldn't be an issue as few (if any) mobile systems ever
1198 * used this part.
1199 */
1200 if (errata.piix4.throttle) {
1201 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001202 "Throttling not supported on PIIX4 A- or B-step\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001207 pr->throttling.state_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 pr->flags.throttling = 1;
1210
1211 /*
1212 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1213 * thermal) decide to lower performance if it so chooses, but for now
1214 * we'll crank up the speed.
1215 */
1216
1217 result = acpi_processor_get_throttling(pr);
1218 if (result)
1219 goto end;
1220
1221 if (pr->throttling.state) {
Len Brown4be44fc2005-08-05 00:44:28 -04001222 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1223 "Disabling throttling (was T%d)\n",
1224 pr->throttling.state));
Frans Pop2a908002009-08-26 14:29:29 -07001225 result = acpi_processor_set_throttling(pr, 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (result)
1227 goto end;
1228 }
1229
Len Brown4be44fc2005-08-05 00:44:28 -04001230 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (result)
1232 pr->flags.throttling = 0;
1233
Patrick Mocheld550d982006-06-27 00:41:40 -04001234 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Zhang Ruid5c68872010-10-08 13:55:15 +08001237#ifdef CONFIG_ACPI_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/* proc interface */
Len Brown4be44fc2005-08-05 00:44:28 -04001239static int acpi_processor_throttling_seq_show(struct seq_file *seq,
1240 void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001242 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001243 int i = 0;
1244 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!pr)
1247 goto end;
1248
1249 if (!(pr->throttling.state_count > 0)) {
1250 seq_puts(seq, "<not supported>\n");
1251 goto end;
1252 }
1253
1254 result = acpi_processor_get_throttling(pr);
1255
1256 if (result) {
Len Brown4be44fc2005-08-05 00:44:28 -04001257 seq_puts(seq,
1258 "Could not determine current throttling state.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto end;
1260 }
1261
1262 seq_printf(seq, "state count: %d\n"
Luming Yu01854e62007-05-26 22:49:58 +08001263 "active state: T%d\n"
Len Brownff55a9c2007-06-02 00:15:25 -04001264 "state available: T%d to T%d\n",
Luming Yu01854e62007-05-26 22:49:58 +08001265 pr->throttling.state_count, pr->throttling.state,
Len Brownff55a9c2007-06-02 00:15:25 -04001266 pr->throttling_platform_limit,
1267 pr->throttling.state_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 seq_puts(seq, "states:\n");
Luming Yu3cc26492007-07-23 12:39:28 -04001270 if (pr->throttling.acpi_processor_get_throttling ==
1271 acpi_processor_get_throttling_fadt) {
Luming Yu01854e62007-05-26 22:49:58 +08001272 for (i = 0; i < pr->throttling.state_count; i++)
1273 seq_printf(seq, " %cT%d: %02d%%\n",
Len Brownff55a9c2007-06-02 00:15:25 -04001274 (i == pr->throttling.state ? '*' : ' '), i,
1275 (pr->throttling.states[i].performance ? pr->
1276 throttling.states[i].performance / 10 : 0));
Luming Yu3cc26492007-07-23 12:39:28 -04001277 } else {
Luming Yu01854e62007-05-26 22:49:58 +08001278 for (i = 0; i < pr->throttling.state_count; i++)
1279 seq_printf(seq, " %cT%d: %02d%%\n",
Len Brownff55a9c2007-06-02 00:15:25 -04001280 (i == pr->throttling.state ? '*' : ' '), i,
1281 (int)pr->throttling.states_tss[i].
1282 freqpercentage);
Luming Yu3cc26492007-07-23 12:39:28 -04001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Len Brown4be44fc2005-08-05 00:44:28 -04001285 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Len Brown4be44fc2005-08-05 00:44:28 -04001289static int acpi_processor_throttling_open_fs(struct inode *inode,
1290 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 return single_open(file, acpi_processor_throttling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001293 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Len Brownff55a9c2007-06-02 00:15:25 -04001296static ssize_t acpi_processor_write_throttling(struct file *file,
Adrian Bunk757b1862006-01-07 13:19:00 -05001297 const char __user * buffer,
1298 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Len Brown4be44fc2005-08-05 00:44:28 -04001300 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001301 struct seq_file *m = file->private_data;
1302 struct acpi_processor *pr = m->private;
Yi Yang3d532d52008-06-14 00:54:37 -04001303 char state_string[5] = "";
1304 char *charp = NULL;
1305 size_t state_val = 0;
1306 char tmpbuf[5] = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (!pr || (count > sizeof(state_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (copy_from_user(state_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001312 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 state_string[count] = '\0';
Yi Yang3d532d52008-06-14 00:54:37 -04001315 if ((count > 0) && (state_string[count-1] == '\n'))
1316 state_string[count-1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Yi Yang3d532d52008-06-14 00:54:37 -04001318 charp = state_string;
1319 if ((state_string[0] == 't') || (state_string[0] == 'T'))
1320 charp++;
1321
1322 state_val = simple_strtoul(charp, NULL, 0);
1323 if (state_val >= pr->throttling.state_count)
1324 return -EINVAL;
1325
Randy Dunlap12b2b342008-06-17 09:43:41 -07001326 snprintf(tmpbuf, 5, "%zu", state_val);
Yi Yang3d532d52008-06-14 00:54:37 -04001327
1328 if (strcmp(tmpbuf, charp) != 0)
1329 return -EINVAL;
1330
Frans Pop2a908002009-08-26 14:29:29 -07001331 result = acpi_processor_set_throttling(pr, state_val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001333 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Patrick Mocheld550d982006-06-27 00:41:40 -04001335 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Jan Engelhardt070d8eb2009-01-12 00:07:55 +01001338const struct file_operations acpi_processor_throttling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001339 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001340 .open = acpi_processor_throttling_open_fs,
1341 .read = seq_read,
Arjan van de Vend479e902006-01-06 16:47:00 -05001342 .write = acpi_processor_write_throttling,
Len Brown4be44fc2005-08-05 00:44:28 -04001343 .llseek = seq_lseek,
1344 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345};
Zhang Ruid5c68872010-10-08 13:55:15 +08001346#endif