cyclades: merge cy_startup tails
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / processor_throttling.c
blobae39797aab55cc860debf1a749a1cf858edd9b64
1 /*
2 * processor_throttling.c - Throttling submodule of the ACPI processor driver
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
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/sched.h>
33 #include <linux/cpufreq.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
37 #include <asm/io.h>
38 #include <asm/uaccess.h>
40 #include <acpi/acpi_bus.h>
41 #include <acpi/acpi_drivers.h>
42 #include <acpi/processor.h>
44 #define ACPI_PROCESSOR_CLASS "processor"
45 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
46 ACPI_MODULE_NAME("processor_throttling");
48 /* ignore_tpc:
49 * 0 -> acpi processor driver doesn't ignore _TPC values
50 * 1 -> acpi processor driver ignores _TPC values
52 static int ignore_tpc;
53 module_param(ignore_tpc, int, 0644);
54 MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
56 struct throttling_tstate {
57 unsigned int cpu; /* cpu nr */
58 int target_state; /* target T-state */
61 #define THROTTLING_PRECHANGE (1)
62 #define THROTTLING_POSTCHANGE (2)
64 static int acpi_processor_get_throttling(struct acpi_processor *pr);
65 int acpi_processor_set_throttling(struct acpi_processor *pr,
66 int state, bool force);
68 static int acpi_processor_update_tsd_coord(void)
70 int count, count_target;
71 int retval = 0;
72 unsigned int i, j;
73 cpumask_var_t covered_cpus;
74 struct acpi_processor *pr, *match_pr;
75 struct acpi_tsd_package *pdomain, *match_pdomain;
76 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
78 if (!alloc_cpumask_var(&covered_cpus, GFP_KERNEL))
79 return -ENOMEM;
82 * Now that we have _TSD data from all CPUs, lets setup T-state
83 * coordination between all CPUs.
85 for_each_possible_cpu(i) {
86 pr = per_cpu(processors, i);
87 if (!pr)
88 continue;
90 /* Basic validity check for domain info */
91 pthrottling = &(pr->throttling);
94 * If tsd package for one cpu is invalid, the coordination
95 * among all CPUs is thought as invalid.
96 * Maybe it is ugly.
98 if (!pthrottling->tsd_valid_flag) {
99 retval = -EINVAL;
100 break;
103 if (retval)
104 goto err_ret;
106 cpumask_clear(covered_cpus);
107 for_each_possible_cpu(i) {
108 pr = per_cpu(processors, i);
109 if (!pr)
110 continue;
112 if (cpumask_test_cpu(i, covered_cpus))
113 continue;
114 pthrottling = &pr->throttling;
116 pdomain = &(pthrottling->domain_info);
117 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
118 cpumask_set_cpu(i, covered_cpus);
120 * If the number of processor in the TSD domain is 1, it is
121 * unnecessary to parse the coordination for this CPU.
123 if (pdomain->num_processors <= 1)
124 continue;
126 /* Validate the Domain info */
127 count_target = pdomain->num_processors;
128 count = 1;
130 for_each_possible_cpu(j) {
131 if (i == j)
132 continue;
134 match_pr = per_cpu(processors, j);
135 if (!match_pr)
136 continue;
138 match_pthrottling = &(match_pr->throttling);
139 match_pdomain = &(match_pthrottling->domain_info);
140 if (match_pdomain->domain != pdomain->domain)
141 continue;
143 /* Here i and j are in the same domain.
144 * If two TSD packages have the same domain, they
145 * should have the same num_porcessors and
146 * coordination type. Otherwise it will be regarded
147 * as illegal.
149 if (match_pdomain->num_processors != count_target) {
150 retval = -EINVAL;
151 goto err_ret;
154 if (pdomain->coord_type != match_pdomain->coord_type) {
155 retval = -EINVAL;
156 goto err_ret;
159 cpumask_set_cpu(j, covered_cpus);
160 cpumask_set_cpu(j, pthrottling->shared_cpu_map);
161 count++;
163 for_each_possible_cpu(j) {
164 if (i == j)
165 continue;
167 match_pr = per_cpu(processors, j);
168 if (!match_pr)
169 continue;
171 match_pthrottling = &(match_pr->throttling);
172 match_pdomain = &(match_pthrottling->domain_info);
173 if (match_pdomain->domain != pdomain->domain)
174 continue;
177 * If some CPUS have the same domain, they
178 * will have the same shared_cpu_map.
180 cpumask_copy(match_pthrottling->shared_cpu_map,
181 pthrottling->shared_cpu_map);
185 err_ret:
186 free_cpumask_var(covered_cpus);
188 for_each_possible_cpu(i) {
189 pr = per_cpu(processors, i);
190 if (!pr)
191 continue;
194 * Assume no coordination on any error parsing domain info.
195 * The coordination type will be forced as SW_ALL.
197 if (retval) {
198 pthrottling = &(pr->throttling);
199 cpumask_clear(pthrottling->shared_cpu_map);
200 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
201 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
205 return retval;
209 * Update the T-state coordination after the _TSD
210 * data for all cpus is obtained.
212 void acpi_processor_throttling_init(void)
214 if (acpi_processor_update_tsd_coord())
215 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
216 "Assume no T-state coordination\n"));
218 return;
221 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
223 struct throttling_tstate *p_tstate = data;
224 struct acpi_processor *pr;
225 unsigned int cpu ;
226 int target_state;
227 struct acpi_processor_limit *p_limit;
228 struct acpi_processor_throttling *p_throttling;
230 cpu = p_tstate->cpu;
231 pr = per_cpu(processors, cpu);
232 if (!pr) {
233 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));
234 return 0;
236 if (!pr->flags.throttling) {
237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is "
238 "unsupported on CPU %d\n", cpu));
239 return 0;
241 target_state = p_tstate->target_state;
242 p_throttling = &(pr->throttling);
243 switch (event) {
244 case THROTTLING_PRECHANGE:
246 * Prechange event is used to choose one proper t-state,
247 * which meets the limits of thermal, user and _TPC.
249 p_limit = &pr->limit;
250 if (p_limit->thermal.tx > target_state)
251 target_state = p_limit->thermal.tx;
252 if (p_limit->user.tx > target_state)
253 target_state = p_limit->user.tx;
254 if (pr->throttling_platform_limit > target_state)
255 target_state = pr->throttling_platform_limit;
256 if (target_state >= p_throttling->state_count) {
257 printk(KERN_WARNING
258 "Exceed the limit of T-state \n");
259 target_state = p_throttling->state_count - 1;
261 p_tstate->target_state = target_state;
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:"
263 "target T-state of CPU %d is T%d\n",
264 cpu, target_state));
265 break;
266 case THROTTLING_POSTCHANGE:
268 * Postchange event is only used to update the
269 * T-state flag of acpi_processor_throttling.
271 p_throttling->state = target_state;
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:"
273 "CPU %d is switched to T%d\n",
274 cpu, target_state));
275 break;
276 default:
277 printk(KERN_WARNING
278 "Unsupported Throttling notifier event\n");
279 break;
282 return 0;
286 * _TPC - Throttling Present Capabilities
288 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
290 acpi_status status = 0;
291 unsigned long long tpc = 0;
293 if (!pr)
294 return -EINVAL;
296 if (ignore_tpc)
297 goto end;
299 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
300 if (ACPI_FAILURE(status)) {
301 if (status != AE_NOT_FOUND) {
302 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
304 return -ENODEV;
307 end:
308 pr->throttling_platform_limit = (int)tpc;
309 return 0;
312 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
314 int result = 0;
315 int throttling_limit;
316 int current_state;
317 struct acpi_processor_limit *limit;
318 int target_state;
320 if (ignore_tpc)
321 return 0;
323 result = acpi_processor_get_platform_limit(pr);
324 if (result) {
325 /* Throttling Limit is unsupported */
326 return result;
329 throttling_limit = pr->throttling_platform_limit;
330 if (throttling_limit >= pr->throttling.state_count) {
331 /* Uncorrect Throttling Limit */
332 return -EINVAL;
335 current_state = pr->throttling.state;
336 if (current_state > throttling_limit) {
338 * The current state can meet the requirement of
339 * _TPC limit. But it is reasonable that OSPM changes
340 * t-states from high to low for better performance.
341 * Of course the limit condition of thermal
342 * and user should be considered.
344 limit = &pr->limit;
345 target_state = throttling_limit;
346 if (limit->thermal.tx > target_state)
347 target_state = limit->thermal.tx;
348 if (limit->user.tx > target_state)
349 target_state = limit->user.tx;
350 } else if (current_state == throttling_limit) {
352 * Unnecessary to change the throttling state
354 return 0;
355 } else {
357 * If the current state is lower than the limit of _TPC, it
358 * will be forced to switch to the throttling state defined
359 * by throttling_platfor_limit.
360 * Because the previous state meets with the limit condition
361 * of thermal and user, it is unnecessary to check it again.
363 target_state = throttling_limit;
365 return acpi_processor_set_throttling(pr, target_state, false);
369 * _PTC - Processor Throttling Control (and status) register location
371 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
373 int result = 0;
374 acpi_status status = 0;
375 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
376 union acpi_object *ptc = NULL;
377 union acpi_object obj = { 0 };
378 struct acpi_processor_throttling *throttling;
380 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
381 if (ACPI_FAILURE(status)) {
382 if (status != AE_NOT_FOUND) {
383 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
385 return -ENODEV;
388 ptc = (union acpi_object *)buffer.pointer;
389 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
390 || (ptc->package.count != 2)) {
391 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
392 result = -EFAULT;
393 goto end;
397 * control_register
400 obj = ptc->package.elements[0];
402 if ((obj.type != ACPI_TYPE_BUFFER)
403 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
404 || (obj.buffer.pointer == NULL)) {
405 printk(KERN_ERR PREFIX
406 "Invalid _PTC data (control_register)\n");
407 result = -EFAULT;
408 goto end;
410 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
411 sizeof(struct acpi_ptc_register));
414 * status_register
417 obj = ptc->package.elements[1];
419 if ((obj.type != ACPI_TYPE_BUFFER)
420 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
421 || (obj.buffer.pointer == NULL)) {
422 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
423 result = -EFAULT;
424 goto end;
427 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
428 sizeof(struct acpi_ptc_register));
430 throttling = &pr->throttling;
432 if ((throttling->control_register.bit_width +
433 throttling->control_register.bit_offset) > 32) {
434 printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
435 result = -EFAULT;
436 goto end;
439 if ((throttling->status_register.bit_width +
440 throttling->status_register.bit_offset) > 32) {
441 printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
442 result = -EFAULT;
443 goto end;
446 end:
447 kfree(buffer.pointer);
449 return result;
453 * _TSS - Throttling Supported States
455 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
457 int result = 0;
458 acpi_status status = AE_OK;
459 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
460 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
461 struct acpi_buffer state = { 0, NULL };
462 union acpi_object *tss = NULL;
463 int i;
465 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
466 if (ACPI_FAILURE(status)) {
467 if (status != AE_NOT_FOUND) {
468 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
470 return -ENODEV;
473 tss = buffer.pointer;
474 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
475 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
476 result = -EFAULT;
477 goto end;
480 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
481 tss->package.count));
483 pr->throttling.state_count = tss->package.count;
484 pr->throttling.states_tss =
485 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
486 GFP_KERNEL);
487 if (!pr->throttling.states_tss) {
488 result = -ENOMEM;
489 goto end;
492 for (i = 0; i < pr->throttling.state_count; i++) {
494 struct acpi_processor_tx_tss *tx =
495 (struct acpi_processor_tx_tss *)&(pr->throttling.
496 states_tss[i]);
498 state.length = sizeof(struct acpi_processor_tx_tss);
499 state.pointer = tx;
501 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
503 status = acpi_extract_package(&(tss->package.elements[i]),
504 &format, &state);
505 if (ACPI_FAILURE(status)) {
506 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
507 result = -EFAULT;
508 kfree(pr->throttling.states_tss);
509 goto end;
512 if (!tx->freqpercentage) {
513 printk(KERN_ERR PREFIX
514 "Invalid _TSS data: freq is zero\n");
515 result = -EFAULT;
516 kfree(pr->throttling.states_tss);
517 goto end;
521 end:
522 kfree(buffer.pointer);
524 return result;
528 * _TSD - T-State Dependencies
530 static int acpi_processor_get_tsd(struct acpi_processor *pr)
532 int result = 0;
533 acpi_status status = AE_OK;
534 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
535 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
536 struct acpi_buffer state = { 0, NULL };
537 union acpi_object *tsd = NULL;
538 struct acpi_tsd_package *pdomain;
539 struct acpi_processor_throttling *pthrottling;
541 pthrottling = &pr->throttling;
542 pthrottling->tsd_valid_flag = 0;
544 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
545 if (ACPI_FAILURE(status)) {
546 if (status != AE_NOT_FOUND) {
547 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
549 return -ENODEV;
552 tsd = buffer.pointer;
553 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
554 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
555 result = -EFAULT;
556 goto end;
559 if (tsd->package.count != 1) {
560 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
561 result = -EFAULT;
562 goto end;
565 pdomain = &(pr->throttling.domain_info);
567 state.length = sizeof(struct acpi_tsd_package);
568 state.pointer = pdomain;
570 status = acpi_extract_package(&(tsd->package.elements[0]),
571 &format, &state);
572 if (ACPI_FAILURE(status)) {
573 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
574 result = -EFAULT;
575 goto end;
578 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
579 printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n");
580 result = -EFAULT;
581 goto end;
584 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
585 printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");
586 result = -EFAULT;
587 goto end;
590 pthrottling = &pr->throttling;
591 pthrottling->tsd_valid_flag = 1;
592 pthrottling->shared_type = pdomain->coord_type;
593 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
595 * If the coordination type is not defined in ACPI spec,
596 * the tsd_valid_flag will be clear and coordination type
597 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
599 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
600 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
601 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
602 pthrottling->tsd_valid_flag = 0;
603 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
606 end:
607 kfree(buffer.pointer);
608 return result;
611 /* --------------------------------------------------------------------------
612 Throttling Control
613 -------------------------------------------------------------------------- */
614 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
616 int state = 0;
617 u32 value = 0;
618 u32 duty_mask = 0;
619 u32 duty_value = 0;
621 if (!pr)
622 return -EINVAL;
624 if (!pr->flags.throttling)
625 return -ENODEV;
627 pr->throttling.state = 0;
629 duty_mask = pr->throttling.state_count - 1;
631 duty_mask <<= pr->throttling.duty_offset;
633 local_irq_disable();
635 value = inl(pr->throttling.address);
638 * Compute the current throttling state when throttling is enabled
639 * (bit 4 is on).
641 if (value & 0x10) {
642 duty_value = value & duty_mask;
643 duty_value >>= pr->throttling.duty_offset;
645 if (duty_value)
646 state = pr->throttling.state_count - duty_value;
649 pr->throttling.state = state;
651 local_irq_enable();
653 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
654 "Throttling state is T%d (%d%% throttling applied)\n",
655 state, pr->throttling.states[state].performance));
657 return 0;
660 #ifdef CONFIG_X86
661 static int acpi_throttling_rdmsr(struct acpi_processor *pr,
662 acpi_integer * value)
664 struct cpuinfo_x86 *c;
665 u64 msr_high, msr_low;
666 unsigned int cpu;
667 u64 msr = 0;
668 int ret = -1;
670 cpu = pr->id;
671 c = &cpu_data(cpu);
673 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
674 !cpu_has(c, X86_FEATURE_ACPI)) {
675 printk(KERN_ERR PREFIX
676 "HARDWARE addr space,NOT supported yet\n");
677 } else {
678 msr_low = 0;
679 msr_high = 0;
680 rdmsr_safe(MSR_IA32_THERM_CONTROL,
681 (u32 *)&msr_low , (u32 *) &msr_high);
682 msr = (msr_high << 32) | msr_low;
683 *value = (acpi_integer) msr;
684 ret = 0;
686 return ret;
689 static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
691 struct cpuinfo_x86 *c;
692 unsigned int cpu;
693 int ret = -1;
694 u64 msr;
696 cpu = pr->id;
697 c = &cpu_data(cpu);
699 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
700 !cpu_has(c, X86_FEATURE_ACPI)) {
701 printk(KERN_ERR PREFIX
702 "HARDWARE addr space,NOT supported yet\n");
703 } else {
704 msr = value;
705 wrmsr_safe(MSR_IA32_THERM_CONTROL,
706 msr & 0xffffffff, msr >> 32);
707 ret = 0;
709 return ret;
711 #else
712 static int acpi_throttling_rdmsr(struct acpi_processor *pr,
713 acpi_integer * value)
715 printk(KERN_ERR PREFIX
716 "HARDWARE addr space,NOT supported yet\n");
717 return -1;
720 static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
722 printk(KERN_ERR PREFIX
723 "HARDWARE addr space,NOT supported yet\n");
724 return -1;
726 #endif
728 static int acpi_read_throttling_status(struct acpi_processor *pr,
729 acpi_integer *value)
731 u32 bit_width, bit_offset;
732 u64 ptc_value;
733 u64 ptc_mask;
734 struct acpi_processor_throttling *throttling;
735 int ret = -1;
737 throttling = &pr->throttling;
738 switch (throttling->status_register.space_id) {
739 case ACPI_ADR_SPACE_SYSTEM_IO:
740 ptc_value = 0;
741 bit_width = throttling->status_register.bit_width;
742 bit_offset = throttling->status_register.bit_offset;
744 acpi_os_read_port((acpi_io_address) throttling->status_register.
745 address, (u32 *) &ptc_value,
746 (u32) (bit_width + bit_offset));
747 ptc_mask = (1 << bit_width) - 1;
748 *value = (acpi_integer) ((ptc_value >> bit_offset) & ptc_mask);
749 ret = 0;
750 break;
751 case ACPI_ADR_SPACE_FIXED_HARDWARE:
752 ret = acpi_throttling_rdmsr(pr, value);
753 break;
754 default:
755 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
756 (u32) (throttling->status_register.space_id));
758 return ret;
761 static int acpi_write_throttling_state(struct acpi_processor *pr,
762 acpi_integer value)
764 u32 bit_width, bit_offset;
765 u64 ptc_value;
766 u64 ptc_mask;
767 struct acpi_processor_throttling *throttling;
768 int ret = -1;
770 throttling = &pr->throttling;
771 switch (throttling->control_register.space_id) {
772 case ACPI_ADR_SPACE_SYSTEM_IO:
773 bit_width = throttling->control_register.bit_width;
774 bit_offset = throttling->control_register.bit_offset;
775 ptc_mask = (1 << bit_width) - 1;
776 ptc_value = value & ptc_mask;
778 acpi_os_write_port((acpi_io_address) throttling->
779 control_register.address,
780 (u32) (ptc_value << bit_offset),
781 (u32) (bit_width + bit_offset));
782 ret = 0;
783 break;
784 case ACPI_ADR_SPACE_FIXED_HARDWARE:
785 ret = acpi_throttling_wrmsr(pr, value);
786 break;
787 default:
788 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
789 (u32) (throttling->control_register.space_id));
791 return ret;
794 static int acpi_get_throttling_state(struct acpi_processor *pr,
795 acpi_integer value)
797 int i;
799 for (i = 0; i < pr->throttling.state_count; i++) {
800 struct acpi_processor_tx_tss *tx =
801 (struct acpi_processor_tx_tss *)&(pr->throttling.
802 states_tss[i]);
803 if (tx->control == value)
804 return i;
806 return -1;
809 static int acpi_get_throttling_value(struct acpi_processor *pr,
810 int state, acpi_integer *value)
812 int ret = -1;
814 if (state >= 0 && state <= pr->throttling.state_count) {
815 struct acpi_processor_tx_tss *tx =
816 (struct acpi_processor_tx_tss *)&(pr->throttling.
817 states_tss[state]);
818 *value = tx->control;
819 ret = 0;
821 return ret;
824 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
826 int state = 0;
827 int ret;
828 acpi_integer value;
830 if (!pr)
831 return -EINVAL;
833 if (!pr->flags.throttling)
834 return -ENODEV;
836 pr->throttling.state = 0;
838 value = 0;
839 ret = acpi_read_throttling_status(pr, &value);
840 if (ret >= 0) {
841 state = acpi_get_throttling_state(pr, value);
842 if (state == -1) {
843 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
844 "Invalid throttling state, reset\n"));
845 state = 0;
846 ret = acpi_processor_set_throttling(pr, state, true);
847 if (ret)
848 return ret;
850 pr->throttling.state = state;
853 return 0;
856 static int acpi_processor_get_throttling(struct acpi_processor *pr)
858 cpumask_var_t saved_mask;
859 int ret;
861 if (!pr)
862 return -EINVAL;
864 if (!pr->flags.throttling)
865 return -ENODEV;
867 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
868 return -ENOMEM;
871 * Migrate task to the cpu pointed by pr.
873 cpumask_copy(saved_mask, &current->cpus_allowed);
874 /* FIXME: use work_on_cpu() */
875 set_cpus_allowed_ptr(current, cpumask_of(pr->id));
876 ret = pr->throttling.acpi_processor_get_throttling(pr);
877 /* restore the previous state */
878 set_cpus_allowed_ptr(current, saved_mask);
879 free_cpumask_var(saved_mask);
881 return ret;
884 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
886 int i, step;
888 if (!pr->throttling.address) {
889 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
890 return -EINVAL;
891 } else if (!pr->throttling.duty_width) {
892 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
893 return -EINVAL;
895 /* TBD: Support duty_cycle values that span bit 4. */
896 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
897 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
898 return -EINVAL;
901 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
904 * Compute state values. Note that throttling displays a linear power
905 * performance relationship (at 50% performance the CPU will consume
906 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
909 step = (1000 / pr->throttling.state_count);
911 for (i = 0; i < pr->throttling.state_count; i++) {
912 pr->throttling.states[i].performance = 1000 - step * i;
913 pr->throttling.states[i].power = 1000 - step * i;
915 return 0;
918 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
919 int state, bool force)
921 u32 value = 0;
922 u32 duty_mask = 0;
923 u32 duty_value = 0;
925 if (!pr)
926 return -EINVAL;
928 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
929 return -EINVAL;
931 if (!pr->flags.throttling)
932 return -ENODEV;
934 if (!force && (state == pr->throttling.state))
935 return 0;
937 if (state < pr->throttling_platform_limit)
938 return -EPERM;
940 * Calculate the duty_value and duty_mask.
942 if (state) {
943 duty_value = pr->throttling.state_count - state;
945 duty_value <<= pr->throttling.duty_offset;
947 /* Used to clear all duty_value bits */
948 duty_mask = pr->throttling.state_count - 1;
950 duty_mask <<= acpi_gbl_FADT.duty_offset;
951 duty_mask = ~duty_mask;
954 local_irq_disable();
957 * Disable throttling by writing a 0 to bit 4. Note that we must
958 * turn it off before you can change the duty_value.
960 value = inl(pr->throttling.address);
961 if (value & 0x10) {
962 value &= 0xFFFFFFEF;
963 outl(value, pr->throttling.address);
967 * Write the new duty_value and then enable throttling. Note
968 * that a state value of 0 leaves throttling disabled.
970 if (state) {
971 value &= duty_mask;
972 value |= duty_value;
973 outl(value, pr->throttling.address);
975 value |= 0x00000010;
976 outl(value, pr->throttling.address);
979 pr->throttling.state = state;
981 local_irq_enable();
983 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
984 "Throttling state set to T%d (%d%%)\n", state,
985 (pr->throttling.states[state].performance ? pr->
986 throttling.states[state].performance / 10 : 0)));
988 return 0;
991 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
992 int state, bool force)
994 int ret;
995 acpi_integer value;
997 if (!pr)
998 return -EINVAL;
1000 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1001 return -EINVAL;
1003 if (!pr->flags.throttling)
1004 return -ENODEV;
1006 if (!force && (state == pr->throttling.state))
1007 return 0;
1009 if (state < pr->throttling_platform_limit)
1010 return -EPERM;
1012 value = 0;
1013 ret = acpi_get_throttling_value(pr, state, &value);
1014 if (ret >= 0) {
1015 acpi_write_throttling_state(pr, value);
1016 pr->throttling.state = state;
1019 return 0;
1022 int acpi_processor_set_throttling(struct acpi_processor *pr,
1023 int state, bool force)
1025 cpumask_var_t saved_mask;
1026 int ret = 0;
1027 unsigned int i;
1028 struct acpi_processor *match_pr;
1029 struct acpi_processor_throttling *p_throttling;
1030 struct throttling_tstate t_state;
1031 cpumask_var_t online_throttling_cpus;
1033 if (!pr)
1034 return -EINVAL;
1036 if (!pr->flags.throttling)
1037 return -ENODEV;
1039 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1040 return -EINVAL;
1042 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
1043 return -ENOMEM;
1045 if (!alloc_cpumask_var(&online_throttling_cpus, GFP_KERNEL)) {
1046 free_cpumask_var(saved_mask);
1047 return -ENOMEM;
1050 cpumask_copy(saved_mask, &current->cpus_allowed);
1051 t_state.target_state = state;
1052 p_throttling = &(pr->throttling);
1053 cpumask_and(online_throttling_cpus, cpu_online_mask,
1054 p_throttling->shared_cpu_map);
1056 * The throttling notifier will be called for every
1057 * affected cpu in order to get one proper T-state.
1058 * The notifier event is THROTTLING_PRECHANGE.
1060 for_each_cpu(i, online_throttling_cpus) {
1061 t_state.cpu = i;
1062 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1063 &t_state);
1066 * The function of acpi_processor_set_throttling will be called
1067 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1068 * it is necessary to call it for every affected cpu. Otherwise
1069 * it can be called only for the cpu pointed by pr.
1071 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
1072 /* FIXME: use work_on_cpu() */
1073 set_cpus_allowed_ptr(current, cpumask_of(pr->id));
1074 ret = p_throttling->acpi_processor_set_throttling(pr,
1075 t_state.target_state, force);
1076 } else {
1078 * When the T-state coordination is SW_ALL or HW_ALL,
1079 * it is necessary to set T-state for every affected
1080 * cpus.
1082 for_each_cpu(i, online_throttling_cpus) {
1083 match_pr = per_cpu(processors, i);
1085 * If the pointer is invalid, we will report the
1086 * error message and continue.
1088 if (!match_pr) {
1089 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1090 "Invalid Pointer for CPU %d\n", i));
1091 continue;
1094 * If the throttling control is unsupported on CPU i,
1095 * we will report the error message and continue.
1097 if (!match_pr->flags.throttling) {
1098 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1099 "Throttling Controll is unsupported "
1100 "on CPU %d\n", i));
1101 continue;
1103 t_state.cpu = i;
1104 /* FIXME: use work_on_cpu() */
1105 set_cpus_allowed_ptr(current, cpumask_of(i));
1106 ret = match_pr->throttling.
1107 acpi_processor_set_throttling(
1108 match_pr, t_state.target_state, force);
1112 * After the set_throttling is called, the
1113 * throttling notifier is called for every
1114 * affected cpu to update the T-states.
1115 * The notifier event is THROTTLING_POSTCHANGE
1117 for_each_cpu(i, online_throttling_cpus) {
1118 t_state.cpu = i;
1119 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1120 &t_state);
1122 /* restore the previous state */
1123 /* FIXME: use work_on_cpu() */
1124 set_cpus_allowed_ptr(current, saved_mask);
1125 free_cpumask_var(online_throttling_cpus);
1126 free_cpumask_var(saved_mask);
1127 return ret;
1130 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1132 int result = 0;
1133 struct acpi_processor_throttling *pthrottling;
1135 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1136 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1137 pr->throttling.address,
1138 pr->throttling.duty_offset,
1139 pr->throttling.duty_width));
1141 if (!pr)
1142 return -EINVAL;
1145 * Evaluate _PTC, _TSS and _TPC
1146 * They must all be present or none of them can be used.
1148 if (acpi_processor_get_throttling_control(pr) ||
1149 acpi_processor_get_throttling_states(pr) ||
1150 acpi_processor_get_platform_limit(pr))
1152 pr->throttling.acpi_processor_get_throttling =
1153 &acpi_processor_get_throttling_fadt;
1154 pr->throttling.acpi_processor_set_throttling =
1155 &acpi_processor_set_throttling_fadt;
1156 if (acpi_processor_get_fadt_info(pr))
1157 return 0;
1158 } else {
1159 pr->throttling.acpi_processor_get_throttling =
1160 &acpi_processor_get_throttling_ptc;
1161 pr->throttling.acpi_processor_set_throttling =
1162 &acpi_processor_set_throttling_ptc;
1166 * If TSD package for one CPU can't be parsed successfully, it means
1167 * that this CPU will have no coordination with other CPUs.
1169 if (acpi_processor_get_tsd(pr)) {
1170 pthrottling = &pr->throttling;
1171 pthrottling->tsd_valid_flag = 0;
1172 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1173 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1177 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1178 * This shouldn't be an issue as few (if any) mobile systems ever
1179 * used this part.
1181 if (errata.piix4.throttle) {
1182 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1183 "Throttling not supported on PIIX4 A- or B-step\n"));
1184 return 0;
1187 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
1188 pr->throttling.state_count));
1190 pr->flags.throttling = 1;
1193 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1194 * thermal) decide to lower performance if it so chooses, but for now
1195 * we'll crank up the speed.
1198 result = acpi_processor_get_throttling(pr);
1199 if (result)
1200 goto end;
1202 if (pr->throttling.state) {
1203 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1204 "Disabling throttling (was T%d)\n",
1205 pr->throttling.state));
1206 result = acpi_processor_set_throttling(pr, 0, false);
1207 if (result)
1208 goto end;
1211 end:
1212 if (result)
1213 pr->flags.throttling = 0;
1215 return result;
1218 /* proc interface */
1220 static int acpi_processor_throttling_seq_show(struct seq_file *seq,
1221 void *offset)
1223 struct acpi_processor *pr = seq->private;
1224 int i = 0;
1225 int result = 0;
1227 if (!pr)
1228 goto end;
1230 if (!(pr->throttling.state_count > 0)) {
1231 seq_puts(seq, "<not supported>\n");
1232 goto end;
1235 result = acpi_processor_get_throttling(pr);
1237 if (result) {
1238 seq_puts(seq,
1239 "Could not determine current throttling state.\n");
1240 goto end;
1243 seq_printf(seq, "state count: %d\n"
1244 "active state: T%d\n"
1245 "state available: T%d to T%d\n",
1246 pr->throttling.state_count, pr->throttling.state,
1247 pr->throttling_platform_limit,
1248 pr->throttling.state_count - 1);
1250 seq_puts(seq, "states:\n");
1251 if (pr->throttling.acpi_processor_get_throttling ==
1252 acpi_processor_get_throttling_fadt) {
1253 for (i = 0; i < pr->throttling.state_count; i++)
1254 seq_printf(seq, " %cT%d: %02d%%\n",
1255 (i == pr->throttling.state ? '*' : ' '), i,
1256 (pr->throttling.states[i].performance ? pr->
1257 throttling.states[i].performance / 10 : 0));
1258 } else {
1259 for (i = 0; i < pr->throttling.state_count; i++)
1260 seq_printf(seq, " %cT%d: %02d%%\n",
1261 (i == pr->throttling.state ? '*' : ' '), i,
1262 (int)pr->throttling.states_tss[i].
1263 freqpercentage);
1266 end:
1267 return 0;
1270 static int acpi_processor_throttling_open_fs(struct inode *inode,
1271 struct file *file)
1273 return single_open(file, acpi_processor_throttling_seq_show,
1274 PDE(inode)->data);
1277 static ssize_t acpi_processor_write_throttling(struct file *file,
1278 const char __user * buffer,
1279 size_t count, loff_t * data)
1281 int result = 0;
1282 struct seq_file *m = file->private_data;
1283 struct acpi_processor *pr = m->private;
1284 char state_string[5] = "";
1285 char *charp = NULL;
1286 size_t state_val = 0;
1287 char tmpbuf[5] = "";
1289 if (!pr || (count > sizeof(state_string) - 1))
1290 return -EINVAL;
1292 if (copy_from_user(state_string, buffer, count))
1293 return -EFAULT;
1295 state_string[count] = '\0';
1296 if ((count > 0) && (state_string[count-1] == '\n'))
1297 state_string[count-1] = '\0';
1299 charp = state_string;
1300 if ((state_string[0] == 't') || (state_string[0] == 'T'))
1301 charp++;
1303 state_val = simple_strtoul(charp, NULL, 0);
1304 if (state_val >= pr->throttling.state_count)
1305 return -EINVAL;
1307 snprintf(tmpbuf, 5, "%zu", state_val);
1309 if (strcmp(tmpbuf, charp) != 0)
1310 return -EINVAL;
1312 result = acpi_processor_set_throttling(pr, state_val, false);
1313 if (result)
1314 return result;
1316 return count;
1319 const struct file_operations acpi_processor_throttling_fops = {
1320 .owner = THIS_MODULE,
1321 .open = acpi_processor_throttling_open_fs,
1322 .read = seq_read,
1323 .write = acpi_processor_write_throttling,
1324 .llseek = seq_lseek,
1325 .release = single_release,