]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/acpi/processor_throttling.c
Merge master.kernel.org:/home/rmk/linux-2.6-arm
[net-next-2.6.git] / drivers / acpi / processor_throttling.c
CommitLineData
1da177e4
LT
1/*
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>
5a0e3ad6 31#include <linux/slab.h>
1da177e4 32#include <linux/init.h>
357dc4c3 33#include <linux/sched.h>
1da177e4
LT
34#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>
89595b8f 42#include <acpi/acpi_drivers.h>
1da177e4
LT
43#include <acpi/processor.h>
44
a192a958
LB
45#define PREFIX "ACPI: "
46
1da177e4 47#define ACPI_PROCESSOR_CLASS "processor"
1da177e4 48#define _COMPONENT ACPI_PROCESSOR_COMPONENT
f52fd66d 49ACPI_MODULE_NAME("processor_throttling");
1da177e4 50
56c213fa
ZR
51/* 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
e4aa5cb2
ZY
59struct 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
ff55a9ce 67static int acpi_processor_get_throttling(struct acpi_processor *pr);
2a908002
FP
68int acpi_processor_set_throttling(struct acpi_processor *pr,
69 int state, bool force);
01854e69 70
1180509f
ZY
71static int acpi_processor_update_tsd_coord(void)
72{
73 int count, count_target;
74 int retval = 0;
75 unsigned int i, j;
2fdf66b4 76 cpumask_var_t covered_cpus;
1180509f
ZY
77 struct acpi_processor *pr, *match_pr;
78 struct acpi_tsd_package *pdomain, *match_pdomain;
79 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
80
79f55997 81 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
2fdf66b4
RR
82 return -ENOMEM;
83
1180509f
ZY
84 /*
85 * Now that we have _TSD data from all CPUs, lets setup T-state
33a2a529 86 * coordination between all CPUs.
1180509f
ZY
87 */
88 for_each_possible_cpu(i) {
706546d0 89 pr = per_cpu(processors, i);
1180509f
ZY
90 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
1180509f 109 for_each_possible_cpu(i) {
706546d0 110 pr = per_cpu(processors, i);
1180509f
ZY
111 if (!pr)
112 continue;
113
2fdf66b4 114 if (cpumask_test_cpu(i, covered_cpus))
1180509f
ZY
115 continue;
116 pthrottling = &pr->throttling;
117
118 pdomain = &(pthrottling->domain_info);
2fdf66b4
RR
119 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
120 cpumask_set_cpu(i, covered_cpus);
1180509f
ZY
121 /*
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
706546d0 136 match_pr = per_cpu(processors, j);
1180509f
ZY
137 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
2fdf66b4
RR
161 cpumask_set_cpu(j, covered_cpus);
162 cpumask_set_cpu(j, pthrottling->shared_cpu_map);
1180509f
ZY
163 count++;
164 }
165 for_each_possible_cpu(j) {
166 if (i == j)
167 continue;
168
706546d0 169 match_pr = per_cpu(processors, j);
1180509f
ZY
170 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 */
2fdf66b4
RR
182 cpumask_copy(match_pthrottling->shared_cpu_map,
183 pthrottling->shared_cpu_map);
1180509f
ZY
184 }
185 }
186
187err_ret:
2fdf66b4
RR
188 free_cpumask_var(covered_cpus);
189
1180509f 190 for_each_possible_cpu(i) {
706546d0 191 pr = per_cpu(processors, i);
1180509f
ZY
192 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);
2fdf66b4
RR
201 cpumask_clear(pthrottling->shared_cpu_map);
202 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
1180509f
ZY
203 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
e4aa5cb2
ZY
223static 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;
706546d0 233 pr = per_cpu(processors, cpu);
e4aa5cb2
ZY
234 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
c30c620e
LB
287/*
288 * _TPC - Throttling Present Capabilities
289 */
01854e69
LY
290static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
291{
292 acpi_status status = 0;
27663c58 293 unsigned long long tpc = 0;
01854e69 294
ff55a9ce 295 if (!pr)
01854e69 296 return -EINVAL;
56c213fa
ZR
297
298 if (ignore_tpc)
299 goto end;
300
01854e69 301 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
c30c620e
LB
302 if (ACPI_FAILURE(status)) {
303 if (status != AE_NOT_FOUND) {
304 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
305 }
01854e69
LY
306 return -ENODEV;
307 }
56c213fa
ZR
308
309end:
01854e69
LY
310 pr->throttling_platform_limit = (int)tpc;
311 return 0;
312}
313
314int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
315{
ef54d5ad
ZY
316 int result = 0;
317 int throttling_limit;
318 int current_state;
319 struct acpi_processor_limit *limit;
320 int target_state;
321
56c213fa
ZR
322 if (ignore_tpc)
323 return 0;
324
ef54d5ad
ZY
325 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 }
2a908002 367 return acpi_processor_set_throttling(pr, target_state, false);
01854e69
LY
368}
369
c30c620e
LB
370/*
371 * _PTC - Processor Throttling Control (and status) register location
372 */
01854e69
LY
373static 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 };
9bcb2721 380 struct acpi_processor_throttling *throttling;
01854e69
LY
381
382 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
383 if (ACPI_FAILURE(status)) {
c30c620e
LB
384 if (status != AE_NOT_FOUND) {
385 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
386 }
01854e69
LY
387 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)) {
ff55a9ce
LB
407 printk(KERN_ERR PREFIX
408 "Invalid _PTC data (control_register)\n");
01854e69
LY
409 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,
ff55a9ce 430 sizeof(struct acpi_ptc_register));
01854e69 431
9bcb2721
ZY
432 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
ff55a9ce 448 end:
01854e69
LY
449 kfree(buffer.pointer);
450
451 return result;
452}
c30c620e
LB
453
454/*
455 * _TSS - Throttling Supported States
456 */
01854e69
LY
457static 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)) {
c30c620e
LB
469 if (status != AE_NOT_FOUND) {
470 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
471 }
01854e69
LY
472 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
ff55a9ce
LB
496 struct acpi_processor_tx_tss *tx =
497 (struct acpi_processor_tx_tss *)&(pr->throttling.
498 states_tss[i]);
01854e69
LY
499
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
ff55a9ce 516 "Invalid _TSS data: freq is zero\n");
01854e69
LY
517 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}
c30c620e
LB
528
529/*
530 * _TSD - T-State Dependencies
531 */
ff55a9ce 532static int acpi_processor_get_tsd(struct acpi_processor *pr)
01854e69
LY
533{
534 int result = 0;
535 acpi_status status = AE_OK;
ff55a9ce
LB
536 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;
01854e69 540 struct acpi_tsd_package *pdomain;
1180509f
ZY
541 struct acpi_processor_throttling *pthrottling;
542
543 pthrottling = &pr->throttling;
544 pthrottling->tsd_valid_flag = 0;
01854e69
LY
545
546 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
547 if (ACPI_FAILURE(status)) {
c30c620e
LB
548 if (status != AE_NOT_FOUND) {
549 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
550 }
01854e69
LY
551 return -ENODEV;
552 }
553
554 tsd = buffer.pointer;
555 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
55ac9a01 556 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
01854e69
LY
557 result = -EFAULT;
558 goto end;
559 }
560
561 if (tsd->package.count != 1) {
55ac9a01 562 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
01854e69
LY
563 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]),
ff55a9ce 573 &format, &state);
01854e69 574 if (ACPI_FAILURE(status)) {
55ac9a01 575 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
01854e69
LY
576 result = -EFAULT;
577 goto end;
578 }
579
580 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
55ac9a01 581 printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n");
01854e69
LY
582 result = -EFAULT;
583 goto end;
584 }
585
586 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
55ac9a01 587 printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");
01854e69
LY
588 result = -EFAULT;
589 goto end;
590 }
591
1180509f
ZY
592 pthrottling = &pr->throttling;
593 pthrottling->tsd_valid_flag = 1;
594 pthrottling->shared_type = pdomain->coord_type;
2fdf66b4 595 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1180509f
ZY
596 /*
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
ff55a9ce 608 end:
01854e69
LY
609 kfree(buffer.pointer);
610 return result;
611}
612
1da177e4
LT
613/* --------------------------------------------------------------------------
614 Throttling Control
615 -------------------------------------------------------------------------- */
01854e69 616static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
1da177e4 617{
4be44fcd
LB
618 int state = 0;
619 u32 value = 0;
620 u32 duty_mask = 0;
621 u32 duty_value = 0;
1da177e4 622
1da177e4 623 if (!pr)
d550d98d 624 return -EINVAL;
1da177e4
LT
625
626 if (!pr->flags.throttling)
d550d98d 627 return -ENODEV;
1da177e4
LT
628
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)
4be44fcd 648 state = pr->throttling.state_count - duty_value;
1da177e4
LT
649 }
650
651 pr->throttling.state = state;
652
653 local_irq_enable();
654
655 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
656 "Throttling state is T%d (%d%% throttling applied)\n",
657 state, pr->throttling.states[state].performance));
1da177e4 658
d550d98d 659 return 0;
1da177e4
LT
660}
661
f79f06ab
ZY
662#ifdef CONFIG_X86
663static int acpi_throttling_rdmsr(struct acpi_processor *pr,
439913ff 664 u64 *value)
f79f06ab
ZY
665{
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;
357dc4c3 682 rdmsr_safe(MSR_IA32_THERM_CONTROL,
f79f06ab
ZY
683 (u32 *)&msr_low , (u32 *) &msr_high);
684 msr = (msr_high << 32) | msr_low;
439913ff 685 *value = (u64) msr;
f79f06ab
ZY
686 ret = 0;
687 }
688 return ret;
689}
690
439913ff 691static int acpi_throttling_wrmsr(struct acpi_processor *pr, u64 value)
f79f06ab
ZY
692{
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;
357dc4c3 707 wrmsr_safe(MSR_IA32_THERM_CONTROL,
f79f06ab
ZY
708 msr & 0xffffffff, msr >> 32);
709 ret = 0;
710 }
711 return ret;
712}
713#else
714static int acpi_throttling_rdmsr(struct acpi_processor *pr,
439913ff 715 u64 *value)
f79f06ab
ZY
716{
717 printk(KERN_ERR PREFIX
718 "HARDWARE addr space,NOT supported yet\n");
719 return -1;
720}
721
439913ff 722static int acpi_throttling_wrmsr(struct acpi_processor *pr, u64 value)
f79f06ab
ZY
723{
724 printk(KERN_ERR PREFIX
725 "HARDWARE addr space,NOT supported yet\n");
726 return -1;
727}
728#endif
729
0753f6e0 730static int acpi_read_throttling_status(struct acpi_processor *pr,
439913ff 731 u64 *value)
01854e69 732{
9bcb2721 733 u32 bit_width, bit_offset;
0753f6e0 734 u64 ptc_value;
9bcb2721 735 u64 ptc_mask;
0753f6e0
ZY
736 struct acpi_processor_throttling *throttling;
737 int ret = -1;
738
739 throttling = &pr->throttling;
01854e69
LY
740 switch (throttling->status_register.space_id) {
741 case ACPI_ADR_SPACE_SYSTEM_IO:
0753f6e0 742 ptc_value = 0;
9bcb2721
ZY
743 bit_width = throttling->status_register.bit_width;
744 bit_offset = throttling->status_register.bit_offset;
745
ff55a9ce 746 acpi_os_read_port((acpi_io_address) throttling->status_register.
0753f6e0 747 address, (u32 *) &ptc_value,
9bcb2721
ZY
748 (u32) (bit_width + bit_offset));
749 ptc_mask = (1 << bit_width) - 1;
439913ff 750 *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
0753f6e0 751 ret = 0;
01854e69
LY
752 break;
753 case ACPI_ADR_SPACE_FIXED_HARDWARE:
f79f06ab 754 ret = acpi_throttling_rdmsr(pr, value);
01854e69
LY
755 break;
756 default:
757 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
ff55a9ce 758 (u32) (throttling->status_register.space_id));
01854e69 759 }
0753f6e0 760 return ret;
01854e69
LY
761}
762
0753f6e0 763static int acpi_write_throttling_state(struct acpi_processor *pr,
439913ff 764 u64 value)
01854e69 765{
9bcb2721 766 u32 bit_width, bit_offset;
0753f6e0 767 u64 ptc_value;
9bcb2721 768 u64 ptc_mask;
0753f6e0 769 struct acpi_processor_throttling *throttling;
01854e69
LY
770 int ret = -1;
771
0753f6e0 772 throttling = &pr->throttling;
01854e69
LY
773 switch (throttling->control_register.space_id) {
774 case ACPI_ADR_SPACE_SYSTEM_IO:
9bcb2721
ZY
775 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
ff55a9ce 780 acpi_os_write_port((acpi_io_address) throttling->
9bcb2721
ZY
781 control_register.address,
782 (u32) (ptc_value << bit_offset),
783 (u32) (bit_width + bit_offset));
01854e69
LY
784 ret = 0;
785 break;
786 case ACPI_ADR_SPACE_FIXED_HARDWARE:
f79f06ab 787 ret = acpi_throttling_wrmsr(pr, value);
01854e69
LY
788 break;
789 default:
790 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
ff55a9ce 791 (u32) (throttling->control_register.space_id));
01854e69
LY
792 }
793 return ret;
794}
795
0753f6e0 796static int acpi_get_throttling_state(struct acpi_processor *pr,
439913ff 797 u64 value)
01854e69
LY
798{
799 int i;
800
801 for (i = 0; i < pr->throttling.state_count; i++) {
ff55a9ce
LB
802 struct acpi_processor_tx_tss *tx =
803 (struct acpi_processor_tx_tss *)&(pr->throttling.
804 states_tss[i]);
805 if (tx->control == value)
53af9cfb 806 return i;
01854e69 807 }
53af9cfb 808 return -1;
01854e69
LY
809}
810
0753f6e0 811static int acpi_get_throttling_value(struct acpi_processor *pr,
439913ff 812 int state, u64 *value)
01854e69 813{
0753f6e0
ZY
814 int ret = -1;
815
ff55a9ce
LB
816 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]);
0753f6e0
ZY
820 *value = tx->control;
821 ret = 0;
01854e69 822 }
0753f6e0 823 return ret;
01854e69
LY
824}
825
826static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
827{
828 int state = 0;
0753f6e0 829 int ret;
439913ff 830 u64 value;
01854e69 831
01854e69
LY
832 if (!pr)
833 return -EINVAL;
834
835 if (!pr->flags.throttling)
836 return -ENODEV;
837
838 pr->throttling.state = 0;
357dc4c3 839
0753f6e0
ZY
840 value = 0;
841 ret = acpi_read_throttling_status(pr, &value);
842 if (ret >= 0) {
ff55a9ce 843 state = acpi_get_throttling_state(pr, value);
4973b22a 844 if (state == -1) {
bdf57de4
FP
845 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
846 "Invalid throttling state, reset\n"));
4973b22a 847 state = 0;
2a908002 848 ret = acpi_processor_set_throttling(pr, state, true);
4973b22a
ZR
849 if (ret)
850 return ret;
851 }
01854e69
LY
852 pr->throttling.state = state;
853 }
01854e69
LY
854
855 return 0;
856}
857
01854e69
LY
858static int acpi_processor_get_throttling(struct acpi_processor *pr)
859{
2fdf66b4 860 cpumask_var_t saved_mask;
357dc4c3
ZY
861 int ret;
862
87654273
ZY
863 if (!pr)
864 return -EINVAL;
865
866 if (!pr->flags.throttling)
867 return -ENODEV;
2fdf66b4
RR
868
869 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
870 return -ENOMEM;
871
357dc4c3
ZY
872 /*
873 * Migrate task to the cpu pointed by pr.
874 */
2fdf66b4
RR
875 cpumask_copy(saved_mask, &current->cpus_allowed);
876 /* FIXME: use work_on_cpu() */
877 set_cpus_allowed_ptr(current, cpumask_of(pr->id));
357dc4c3
ZY
878 ret = pr->throttling.acpi_processor_get_throttling(pr);
879 /* restore the previous state */
2fdf66b4
RR
880 set_cpus_allowed_ptr(current, saved_mask);
881 free_cpumask_var(saved_mask);
357dc4c3
ZY
882
883 return ret;
01854e69
LY
884}
885
22cc5019
ZY
886static 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
6c5cf8aa 920static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
2a908002 921 int state, bool force)
1da177e4 922{
4be44fcd
LB
923 u32 value = 0;
924 u32 duty_mask = 0;
925 u32 duty_value = 0;
1da177e4 926
1da177e4 927 if (!pr)
d550d98d 928 return -EINVAL;
1da177e4
LT
929
930 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
d550d98d 931 return -EINVAL;
1da177e4
LT
932
933 if (!pr->flags.throttling)
d550d98d 934 return -ENODEV;
1da177e4 935
2a908002 936 if (!force && (state == pr->throttling.state))
d550d98d 937 return 0;
1da177e4 938
01854e69
LY
939 if (state < pr->throttling_platform_limit)
940 return -EPERM;
1da177e4
LT
941 /*
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
cee324b1 952 duty_mask <<= acpi_gbl_FADT.duty_offset;
1da177e4
LT
953 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,
4be44fcd
LB
986 "Throttling state set to T%d (%d%%)\n", state,
987 (pr->throttling.states[state].performance ? pr->
988 throttling.states[state].performance / 10 : 0)));
1da177e4 989
d550d98d 990 return 0;
1da177e4
LT
991}
992
6c5cf8aa 993static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
2a908002 994 int state, bool force)
01854e69 995{
0753f6e0 996 int ret;
439913ff 997 u64 value;
01854e69
LY
998
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
2a908002 1008 if (!force && (state == pr->throttling.state))
01854e69
LY
1009 return 0;
1010
1011 if (state < pr->throttling_platform_limit)
1012 return -EPERM;
1013
0753f6e0
ZY
1014 value = 0;
1015 ret = acpi_get_throttling_value(pr, state, &value);
1016 if (ret >= 0) {
1017 acpi_write_throttling_state(pr, value);
01854e69
LY
1018 pr->throttling.state = state;
1019 }
01854e69
LY
1020
1021 return 0;
1022}
1023
2a908002
FP
1024int acpi_processor_set_throttling(struct acpi_processor *pr,
1025 int state, bool force)
01854e69 1026{
2fdf66b4 1027 cpumask_var_t saved_mask;
3391a76f 1028 int ret = 0;
33a2a529
ZY
1029 unsigned int i;
1030 struct acpi_processor *match_pr;
1031 struct acpi_processor_throttling *p_throttling;
1032 struct throttling_tstate t_state;
2fdf66b4 1033 cpumask_var_t online_throttling_cpus;
87654273
ZY
1034
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
2fdf66b4
RR
1044 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);
33a2a529
ZY
1053 t_state.target_state = state;
1054 p_throttling = &(pr->throttling);
2fdf66b4
RR
1055 cpumask_and(online_throttling_cpus, cpu_online_mask,
1056 p_throttling->shared_cpu_map);
357dc4c3 1057 /*
33a2a529
ZY
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.
357dc4c3 1061 */
2fdf66b4 1062 for_each_cpu(i, online_throttling_cpus) {
33a2a529
ZY
1063 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) {
2fdf66b4
RR
1074 /* FIXME: use work_on_cpu() */
1075 set_cpus_allowed_ptr(current, cpumask_of(pr->id));
33a2a529 1076 ret = p_throttling->acpi_processor_set_throttling(pr,
2a908002 1077 t_state.target_state, force);
33a2a529
ZY
1078 } 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 */
2fdf66b4 1084 for_each_cpu(i, online_throttling_cpus) {
706546d0 1085 match_pr = per_cpu(processors, i);
33a2a529
ZY
1086 /*
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;
2fdf66b4
RR
1106 /* FIXME: use work_on_cpu() */
1107 set_cpus_allowed_ptr(current, cpumask_of(i));
33a2a529
ZY
1108 ret = match_pr->throttling.
1109 acpi_processor_set_throttling(
2a908002 1110 match_pr, t_state.target_state, force);
33a2a529
ZY
1111 }
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 */
2fdf66b4 1119 for_each_cpu(i, online_throttling_cpus) {
33a2a529
ZY
1120 t_state.cpu = i;
1121 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1122 &t_state);
1123 }
357dc4c3 1124 /* restore the previous state */
2fdf66b4
RR
1125 /* 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);
357dc4c3 1129 return ret;
01854e69
LY
1130}
1131
4be44fcd 1132int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1da177e4 1133{
4be44fcd 1134 int result = 0;
1180509f 1135 struct acpi_processor_throttling *pthrottling;
1da177e4 1136
1da177e4 1137 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
1138 "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));
1da177e4 1142
c30c620e
LB
1143 /*
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 {
ff55a9ce
LB
1151 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;
d1154be3
AS
1155 if (acpi_processor_get_fadt_info(pr))
1156 return 0;
01854e69 1157 } else {
ff55a9ce
LB
1158 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;
01854e69 1162 }
1da177e4 1163
1180509f
ZY
1164 /*
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;
2fdf66b4 1171 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1180509f
ZY
1172 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1173 }
c30c620e 1174
1da177e4
LT
1175 /*
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,
4be44fcd 1182 "Throttling not supported on PIIX4 A- or B-step\n"));
d550d98d 1183 return 0;
1da177e4
LT
1184 }
1185
1da177e4 1186 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
4be44fcd 1187 pr->throttling.state_count));
1da177e4
LT
1188
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) {
4be44fcd
LB
1202 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1203 "Disabling throttling (was T%d)\n",
1204 pr->throttling.state));
2a908002 1205 result = acpi_processor_set_throttling(pr, 0, false);
1da177e4
LT
1206 if (result)
1207 goto end;
1208 }
1209
4be44fcd 1210 end:
1da177e4
LT
1211 if (result)
1212 pr->flags.throttling = 0;
1213
d550d98d 1214 return result;
1da177e4
LT
1215}
1216
1da177e4 1217/* proc interface */
4be44fcd
LB
1218static int acpi_processor_throttling_seq_show(struct seq_file *seq,
1219 void *offset)
1da177e4 1220{
50dd0969 1221 struct acpi_processor *pr = seq->private;
4be44fcd
LB
1222 int i = 0;
1223 int result = 0;
1da177e4 1224
1da177e4
LT
1225 if (!pr)
1226 goto end;
1227
1228 if (!(pr->throttling.state_count > 0)) {
1229 seq_puts(seq, "<not supported>\n");
1230 goto end;
1231 }
1232
1233 result = acpi_processor_get_throttling(pr);
1234
1235 if (result) {
4be44fcd
LB
1236 seq_puts(seq,
1237 "Could not determine current throttling state.\n");
1da177e4
LT
1238 goto end;
1239 }
1240
1241 seq_printf(seq, "state count: %d\n"
01854e69 1242 "active state: T%d\n"
ff55a9ce 1243 "state available: T%d to T%d\n",
01854e69 1244 pr->throttling.state_count, pr->throttling.state,
ff55a9ce
LB
1245 pr->throttling_platform_limit,
1246 pr->throttling.state_count - 1);
1da177e4
LT
1247
1248 seq_puts(seq, "states:\n");
3cc2649b
LY
1249 if (pr->throttling.acpi_processor_get_throttling ==
1250 acpi_processor_get_throttling_fadt) {
01854e69
LY
1251 for (i = 0; i < pr->throttling.state_count; i++)
1252 seq_printf(seq, " %cT%d: %02d%%\n",
ff55a9ce
LB
1253 (i == pr->throttling.state ? '*' : ' '), i,
1254 (pr->throttling.states[i].performance ? pr->
1255 throttling.states[i].performance / 10 : 0));
3cc2649b 1256 } else {
01854e69
LY
1257 for (i = 0; i < pr->throttling.state_count; i++)
1258 seq_printf(seq, " %cT%d: %02d%%\n",
ff55a9ce
LB
1259 (i == pr->throttling.state ? '*' : ' '), i,
1260 (int)pr->throttling.states_tss[i].
1261 freqpercentage);
3cc2649b 1262 }
1da177e4 1263
4be44fcd 1264 end:
d550d98d 1265 return 0;
1da177e4
LT
1266}
1267
4be44fcd
LB
1268static int acpi_processor_throttling_open_fs(struct inode *inode,
1269 struct file *file)
1da177e4
LT
1270{
1271 return single_open(file, acpi_processor_throttling_seq_show,
4be44fcd 1272 PDE(inode)->data);
1da177e4
LT
1273}
1274
ff55a9ce 1275static ssize_t acpi_processor_write_throttling(struct file *file,
757b1866
AB
1276 const char __user * buffer,
1277 size_t count, loff_t * data)
1da177e4 1278{
4be44fcd 1279 int result = 0;
50dd0969
JE
1280 struct seq_file *m = file->private_data;
1281 struct acpi_processor *pr = m->private;
3d532d5e
YY
1282 char state_string[5] = "";
1283 char *charp = NULL;
1284 size_t state_val = 0;
1285 char tmpbuf[5] = "";
1da177e4 1286
1da177e4 1287 if (!pr || (count > sizeof(state_string) - 1))
d550d98d 1288 return -EINVAL;
1da177e4
LT
1289
1290 if (copy_from_user(state_string, buffer, count))
d550d98d 1291 return -EFAULT;
1da177e4
LT
1292
1293 state_string[count] = '\0';
3d532d5e
YY
1294 if ((count > 0) && (state_string[count-1] == '\n'))
1295 state_string[count-1] = '\0';
1da177e4 1296
3d532d5e
YY
1297 charp = state_string;
1298 if ((state_string[0] == 't') || (state_string[0] == 'T'))
1299 charp++;
1300
1301 state_val = simple_strtoul(charp, NULL, 0);
1302 if (state_val >= pr->throttling.state_count)
1303 return -EINVAL;
1304
12b2b34e 1305 snprintf(tmpbuf, 5, "%zu", state_val);
3d532d5e
YY
1306
1307 if (strcmp(tmpbuf, charp) != 0)
1308 return -EINVAL;
1309
2a908002 1310 result = acpi_processor_set_throttling(pr, state_val, false);
1da177e4 1311 if (result)
d550d98d 1312 return result;
1da177e4 1313
d550d98d 1314 return count;
1da177e4
LT
1315}
1316
070d8eb1 1317const struct file_operations acpi_processor_throttling_fops = {
cf7acfab 1318 .owner = THIS_MODULE,
4be44fcd
LB
1319 .open = acpi_processor_throttling_open_fs,
1320 .read = seq_read,
d479e908 1321 .write = acpi_processor_write_throttling,
4be44fcd
LB
1322 .llseek = seq_lseek,
1323 .release = single_release,
1da177e4 1324};