Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / acpi / processor_throttling.c
blobb1876534324b56954ced8f23a46161f1f67246ee
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * processor_throttling.c - Throttling submodule of the ACPI processor driver
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
8 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
9 * - Added processor hotplug support
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/cpufreq.h>
18 #include <linux/acpi.h>
19 #include <acpi/processor.h>
20 #include <asm/io.h>
21 #include <linux/uaccess.h>
23 #define PREFIX "ACPI: "
25 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
26 ACPI_MODULE_NAME("processor_throttling");
28 /* ignore_tpc:
29 * 0 -> acpi processor driver doesn't ignore _TPC values
30 * 1 -> acpi processor driver ignores _TPC values
32 static int ignore_tpc;
33 module_param(ignore_tpc, int, 0644);
34 MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
36 struct throttling_tstate {
37 unsigned int cpu; /* cpu nr */
38 int target_state; /* target T-state */
41 struct acpi_processor_throttling_arg {
42 struct acpi_processor *pr;
43 int target_state;
44 bool force;
47 #define THROTTLING_PRECHANGE (1)
48 #define THROTTLING_POSTCHANGE (2)
50 static int acpi_processor_get_throttling(struct acpi_processor *pr);
51 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
52 int state, bool force, bool direct);
54 static int acpi_processor_update_tsd_coord(void)
56 int count, count_target;
57 int retval = 0;
58 unsigned int i, j;
59 cpumask_var_t covered_cpus;
60 struct acpi_processor *pr, *match_pr;
61 struct acpi_tsd_package *pdomain, *match_pdomain;
62 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
64 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
65 return -ENOMEM;
68 * Now that we have _TSD data from all CPUs, lets setup T-state
69 * coordination between all CPUs.
71 for_each_possible_cpu(i) {
72 pr = per_cpu(processors, i);
73 if (!pr)
74 continue;
76 /* Basic validity check for domain info */
77 pthrottling = &(pr->throttling);
80 * If tsd package for one cpu is invalid, the coordination
81 * among all CPUs is thought as invalid.
82 * Maybe it is ugly.
84 if (!pthrottling->tsd_valid_flag) {
85 retval = -EINVAL;
86 break;
89 if (retval)
90 goto err_ret;
92 for_each_possible_cpu(i) {
93 pr = per_cpu(processors, i);
94 if (!pr)
95 continue;
97 if (cpumask_test_cpu(i, covered_cpus))
98 continue;
99 pthrottling = &pr->throttling;
101 pdomain = &(pthrottling->domain_info);
102 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
103 cpumask_set_cpu(i, covered_cpus);
105 * If the number of processor in the TSD domain is 1, it is
106 * unnecessary to parse the coordination for this CPU.
108 if (pdomain->num_processors <= 1)
109 continue;
111 /* Validate the Domain info */
112 count_target = pdomain->num_processors;
113 count = 1;
115 for_each_possible_cpu(j) {
116 if (i == j)
117 continue;
119 match_pr = per_cpu(processors, j);
120 if (!match_pr)
121 continue;
123 match_pthrottling = &(match_pr->throttling);
124 match_pdomain = &(match_pthrottling->domain_info);
125 if (match_pdomain->domain != pdomain->domain)
126 continue;
128 /* Here i and j are in the same domain.
129 * If two TSD packages have the same domain, they
130 * should have the same num_porcessors and
131 * coordination type. Otherwise it will be regarded
132 * as illegal.
134 if (match_pdomain->num_processors != count_target) {
135 retval = -EINVAL;
136 goto err_ret;
139 if (pdomain->coord_type != match_pdomain->coord_type) {
140 retval = -EINVAL;
141 goto err_ret;
144 cpumask_set_cpu(j, covered_cpus);
145 cpumask_set_cpu(j, pthrottling->shared_cpu_map);
146 count++;
148 for_each_possible_cpu(j) {
149 if (i == j)
150 continue;
152 match_pr = per_cpu(processors, j);
153 if (!match_pr)
154 continue;
156 match_pthrottling = &(match_pr->throttling);
157 match_pdomain = &(match_pthrottling->domain_info);
158 if (match_pdomain->domain != pdomain->domain)
159 continue;
162 * If some CPUS have the same domain, they
163 * will have the same shared_cpu_map.
165 cpumask_copy(match_pthrottling->shared_cpu_map,
166 pthrottling->shared_cpu_map);
170 err_ret:
171 free_cpumask_var(covered_cpus);
173 for_each_possible_cpu(i) {
174 pr = per_cpu(processors, i);
175 if (!pr)
176 continue;
179 * Assume no coordination on any error parsing domain info.
180 * The coordination type will be forced as SW_ALL.
182 if (retval) {
183 pthrottling = &(pr->throttling);
184 cpumask_clear(pthrottling->shared_cpu_map);
185 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
186 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
190 return retval;
194 * Update the T-state coordination after the _TSD
195 * data for all cpus is obtained.
197 void acpi_processor_throttling_init(void)
199 if (acpi_processor_update_tsd_coord()) {
200 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
201 "Assume no T-state coordination\n"));
204 return;
207 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
209 struct throttling_tstate *p_tstate = data;
210 struct acpi_processor *pr;
211 unsigned int cpu ;
212 int target_state;
213 struct acpi_processor_limit *p_limit;
214 struct acpi_processor_throttling *p_throttling;
216 cpu = p_tstate->cpu;
217 pr = per_cpu(processors, cpu);
218 if (!pr) {
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));
220 return 0;
222 if (!pr->flags.throttling) {
223 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is "
224 "unsupported on CPU %d\n", cpu));
225 return 0;
227 target_state = p_tstate->target_state;
228 p_throttling = &(pr->throttling);
229 switch (event) {
230 case THROTTLING_PRECHANGE:
232 * Prechange event is used to choose one proper t-state,
233 * which meets the limits of thermal, user and _TPC.
235 p_limit = &pr->limit;
236 if (p_limit->thermal.tx > target_state)
237 target_state = p_limit->thermal.tx;
238 if (p_limit->user.tx > target_state)
239 target_state = p_limit->user.tx;
240 if (pr->throttling_platform_limit > target_state)
241 target_state = pr->throttling_platform_limit;
242 if (target_state >= p_throttling->state_count) {
243 printk(KERN_WARNING
244 "Exceed the limit of T-state \n");
245 target_state = p_throttling->state_count - 1;
247 p_tstate->target_state = target_state;
248 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:"
249 "target T-state of CPU %d is T%d\n",
250 cpu, target_state));
251 break;
252 case THROTTLING_POSTCHANGE:
254 * Postchange event is only used to update the
255 * T-state flag of acpi_processor_throttling.
257 p_throttling->state = target_state;
258 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:"
259 "CPU %d is switched to T%d\n",
260 cpu, target_state));
261 break;
262 default:
263 printk(KERN_WARNING
264 "Unsupported Throttling notifier event\n");
265 break;
268 return 0;
272 * _TPC - Throttling Present Capabilities
274 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
276 acpi_status status = 0;
277 unsigned long long tpc = 0;
279 if (!pr)
280 return -EINVAL;
282 if (ignore_tpc)
283 goto end;
285 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
286 if (ACPI_FAILURE(status)) {
287 if (status != AE_NOT_FOUND) {
288 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
290 return -ENODEV;
293 end:
294 pr->throttling_platform_limit = (int)tpc;
295 return 0;
298 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
300 int result = 0;
301 int throttling_limit;
302 int current_state;
303 struct acpi_processor_limit *limit;
304 int target_state;
306 if (ignore_tpc)
307 return 0;
309 result = acpi_processor_get_platform_limit(pr);
310 if (result) {
311 /* Throttling Limit is unsupported */
312 return result;
315 throttling_limit = pr->throttling_platform_limit;
316 if (throttling_limit >= pr->throttling.state_count) {
317 /* Uncorrect Throttling Limit */
318 return -EINVAL;
321 current_state = pr->throttling.state;
322 if (current_state > throttling_limit) {
324 * The current state can meet the requirement of
325 * _TPC limit. But it is reasonable that OSPM changes
326 * t-states from high to low for better performance.
327 * Of course the limit condition of thermal
328 * and user should be considered.
330 limit = &pr->limit;
331 target_state = throttling_limit;
332 if (limit->thermal.tx > target_state)
333 target_state = limit->thermal.tx;
334 if (limit->user.tx > target_state)
335 target_state = limit->user.tx;
336 } else if (current_state == throttling_limit) {
338 * Unnecessary to change the throttling state
340 return 0;
341 } else {
343 * If the current state is lower than the limit of _TPC, it
344 * will be forced to switch to the throttling state defined
345 * by throttling_platfor_limit.
346 * Because the previous state meets with the limit condition
347 * of thermal and user, it is unnecessary to check it again.
349 target_state = throttling_limit;
351 return acpi_processor_set_throttling(pr, target_state, false);
355 * This function is used to reevaluate whether the T-state is valid
356 * after one CPU is onlined/offlined.
357 * It is noted that it won't reevaluate the following properties for
358 * the T-state.
359 * 1. Control method.
360 * 2. the number of supported T-state
361 * 3. TSD domain
363 void acpi_processor_reevaluate_tstate(struct acpi_processor *pr,
364 bool is_dead)
366 int result = 0;
368 if (is_dead) {
369 /* When one CPU is offline, the T-state throttling
370 * will be invalidated.
372 pr->flags.throttling = 0;
373 return;
375 /* the following is to recheck whether the T-state is valid for
376 * the online CPU
378 if (!pr->throttling.state_count) {
379 /* If the number of T-state is invalid, it is
380 * invalidated.
382 pr->flags.throttling = 0;
383 return;
385 pr->flags.throttling = 1;
387 /* Disable throttling (if enabled). We'll let subsequent
388 * policy (e.g.thermal) decide to lower performance if it
389 * so chooses, but for now we'll crank up the speed.
392 result = acpi_processor_get_throttling(pr);
393 if (result)
394 goto end;
396 if (pr->throttling.state) {
397 result = acpi_processor_set_throttling(pr, 0, false);
398 if (result)
399 goto end;
402 end:
403 if (result)
404 pr->flags.throttling = 0;
407 * _PTC - Processor Throttling Control (and status) register location
409 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
411 int result = 0;
412 acpi_status status = 0;
413 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
414 union acpi_object *ptc = NULL;
415 union acpi_object obj = { 0 };
416 struct acpi_processor_throttling *throttling;
418 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
419 if (ACPI_FAILURE(status)) {
420 if (status != AE_NOT_FOUND) {
421 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
423 return -ENODEV;
426 ptc = (union acpi_object *)buffer.pointer;
427 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
428 || (ptc->package.count != 2)) {
429 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
430 result = -EFAULT;
431 goto end;
435 * control_register
438 obj = ptc->package.elements[0];
440 if ((obj.type != ACPI_TYPE_BUFFER)
441 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
442 || (obj.buffer.pointer == NULL)) {
443 printk(KERN_ERR PREFIX
444 "Invalid _PTC data (control_register)\n");
445 result = -EFAULT;
446 goto end;
448 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
449 sizeof(struct acpi_ptc_register));
452 * status_register
455 obj = ptc->package.elements[1];
457 if ((obj.type != ACPI_TYPE_BUFFER)
458 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
459 || (obj.buffer.pointer == NULL)) {
460 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
461 result = -EFAULT;
462 goto end;
465 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
466 sizeof(struct acpi_ptc_register));
468 throttling = &pr->throttling;
470 if ((throttling->control_register.bit_width +
471 throttling->control_register.bit_offset) > 32) {
472 printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
473 result = -EFAULT;
474 goto end;
477 if ((throttling->status_register.bit_width +
478 throttling->status_register.bit_offset) > 32) {
479 printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
480 result = -EFAULT;
481 goto end;
484 end:
485 kfree(buffer.pointer);
487 return result;
491 * _TSS - Throttling Supported States
493 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
495 int result = 0;
496 acpi_status status = AE_OK;
497 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
498 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
499 struct acpi_buffer state = { 0, NULL };
500 union acpi_object *tss = NULL;
501 int i;
503 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
504 if (ACPI_FAILURE(status)) {
505 if (status != AE_NOT_FOUND) {
506 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
508 return -ENODEV;
511 tss = buffer.pointer;
512 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
513 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
514 result = -EFAULT;
515 goto end;
518 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
519 tss->package.count));
521 pr->throttling.state_count = tss->package.count;
522 pr->throttling.states_tss =
523 kmalloc_array(tss->package.count,
524 sizeof(struct acpi_processor_tx_tss),
525 GFP_KERNEL);
526 if (!pr->throttling.states_tss) {
527 result = -ENOMEM;
528 goto end;
531 for (i = 0; i < pr->throttling.state_count; i++) {
533 struct acpi_processor_tx_tss *tx =
534 (struct acpi_processor_tx_tss *)&(pr->throttling.
535 states_tss[i]);
537 state.length = sizeof(struct acpi_processor_tx_tss);
538 state.pointer = tx;
540 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
542 status = acpi_extract_package(&(tss->package.elements[i]),
543 &format, &state);
544 if (ACPI_FAILURE(status)) {
545 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
546 result = -EFAULT;
547 kfree(pr->throttling.states_tss);
548 goto end;
551 if (!tx->freqpercentage) {
552 printk(KERN_ERR PREFIX
553 "Invalid _TSS data: freq is zero\n");
554 result = -EFAULT;
555 kfree(pr->throttling.states_tss);
556 goto end;
560 end:
561 kfree(buffer.pointer);
563 return result;
567 * _TSD - T-State Dependencies
569 static int acpi_processor_get_tsd(struct acpi_processor *pr)
571 int result = 0;
572 acpi_status status = AE_OK;
573 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
574 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
575 struct acpi_buffer state = { 0, NULL };
576 union acpi_object *tsd = NULL;
577 struct acpi_tsd_package *pdomain;
578 struct acpi_processor_throttling *pthrottling;
580 pthrottling = &pr->throttling;
581 pthrottling->tsd_valid_flag = 0;
583 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
584 if (ACPI_FAILURE(status)) {
585 if (status != AE_NOT_FOUND) {
586 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
588 return -ENODEV;
591 tsd = buffer.pointer;
592 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
593 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
594 result = -EFAULT;
595 goto end;
598 if (tsd->package.count != 1) {
599 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
600 result = -EFAULT;
601 goto end;
604 pdomain = &(pr->throttling.domain_info);
606 state.length = sizeof(struct acpi_tsd_package);
607 state.pointer = pdomain;
609 status = acpi_extract_package(&(tsd->package.elements[0]),
610 &format, &state);
611 if (ACPI_FAILURE(status)) {
612 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
613 result = -EFAULT;
614 goto end;
617 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
618 printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n");
619 result = -EFAULT;
620 goto end;
623 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
624 printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");
625 result = -EFAULT;
626 goto end;
629 pthrottling = &pr->throttling;
630 pthrottling->tsd_valid_flag = 1;
631 pthrottling->shared_type = pdomain->coord_type;
632 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
634 * If the coordination type is not defined in ACPI spec,
635 * the tsd_valid_flag will be clear and coordination type
636 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
638 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
639 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
640 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
641 pthrottling->tsd_valid_flag = 0;
642 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
645 end:
646 kfree(buffer.pointer);
647 return result;
650 /* --------------------------------------------------------------------------
651 Throttling Control
652 -------------------------------------------------------------------------- */
653 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
655 int state = 0;
656 u32 value = 0;
657 u32 duty_mask = 0;
658 u32 duty_value = 0;
660 if (!pr)
661 return -EINVAL;
663 if (!pr->flags.throttling)
664 return -ENODEV;
667 * We don't care about error returns - we just try to mark
668 * these reserved so that nobody else is confused into thinking
669 * that this region might be unused..
671 * (In particular, allocating the IO range for Cardbus)
673 request_region(pr->throttling.address, 6, "ACPI CPU throttle");
675 pr->throttling.state = 0;
677 duty_mask = pr->throttling.state_count - 1;
679 duty_mask <<= pr->throttling.duty_offset;
681 local_irq_disable();
683 value = inl(pr->throttling.address);
686 * Compute the current throttling state when throttling is enabled
687 * (bit 4 is on).
689 if (value & 0x10) {
690 duty_value = value & duty_mask;
691 duty_value >>= pr->throttling.duty_offset;
693 if (duty_value)
694 state = pr->throttling.state_count - duty_value;
697 pr->throttling.state = state;
699 local_irq_enable();
701 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
702 "Throttling state is T%d (%d%% throttling applied)\n",
703 state, pr->throttling.states[state].performance));
705 return 0;
708 #ifdef CONFIG_X86
709 static int acpi_throttling_rdmsr(u64 *value)
711 u64 msr_high, msr_low;
712 u64 msr = 0;
713 int ret = -1;
715 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
716 !this_cpu_has(X86_FEATURE_ACPI)) {
717 printk(KERN_ERR PREFIX
718 "HARDWARE addr space,NOT supported yet\n");
719 } else {
720 msr_low = 0;
721 msr_high = 0;
722 rdmsr_safe(MSR_IA32_THERM_CONTROL,
723 (u32 *)&msr_low , (u32 *) &msr_high);
724 msr = (msr_high << 32) | msr_low;
725 *value = (u64) msr;
726 ret = 0;
728 return ret;
731 static int acpi_throttling_wrmsr(u64 value)
733 int ret = -1;
734 u64 msr;
736 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
737 !this_cpu_has(X86_FEATURE_ACPI)) {
738 printk(KERN_ERR PREFIX
739 "HARDWARE addr space,NOT supported yet\n");
740 } else {
741 msr = value;
742 wrmsr_safe(MSR_IA32_THERM_CONTROL,
743 msr & 0xffffffff, msr >> 32);
744 ret = 0;
746 return ret;
748 #else
749 static int acpi_throttling_rdmsr(u64 *value)
751 printk(KERN_ERR PREFIX
752 "HARDWARE addr space,NOT supported yet\n");
753 return -1;
756 static int acpi_throttling_wrmsr(u64 value)
758 printk(KERN_ERR PREFIX
759 "HARDWARE addr space,NOT supported yet\n");
760 return -1;
762 #endif
764 static int acpi_read_throttling_status(struct acpi_processor *pr,
765 u64 *value)
767 u32 bit_width, bit_offset;
768 u32 ptc_value;
769 u64 ptc_mask;
770 struct acpi_processor_throttling *throttling;
771 int ret = -1;
773 throttling = &pr->throttling;
774 switch (throttling->status_register.space_id) {
775 case ACPI_ADR_SPACE_SYSTEM_IO:
776 bit_width = throttling->status_register.bit_width;
777 bit_offset = throttling->status_register.bit_offset;
779 acpi_os_read_port((acpi_io_address) throttling->status_register.
780 address, &ptc_value,
781 (u32) (bit_width + bit_offset));
782 ptc_mask = (1 << bit_width) - 1;
783 *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
784 ret = 0;
785 break;
786 case ACPI_ADR_SPACE_FIXED_HARDWARE:
787 ret = acpi_throttling_rdmsr(value);
788 break;
789 default:
790 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
791 (u32) (throttling->status_register.space_id));
793 return ret;
796 static int acpi_write_throttling_state(struct acpi_processor *pr,
797 u64 value)
799 u32 bit_width, bit_offset;
800 u64 ptc_value;
801 u64 ptc_mask;
802 struct acpi_processor_throttling *throttling;
803 int ret = -1;
805 throttling = &pr->throttling;
806 switch (throttling->control_register.space_id) {
807 case ACPI_ADR_SPACE_SYSTEM_IO:
808 bit_width = throttling->control_register.bit_width;
809 bit_offset = throttling->control_register.bit_offset;
810 ptc_mask = (1 << bit_width) - 1;
811 ptc_value = value & ptc_mask;
813 acpi_os_write_port((acpi_io_address) throttling->
814 control_register.address,
815 (u32) (ptc_value << bit_offset),
816 (u32) (bit_width + bit_offset));
817 ret = 0;
818 break;
819 case ACPI_ADR_SPACE_FIXED_HARDWARE:
820 ret = acpi_throttling_wrmsr(value);
821 break;
822 default:
823 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
824 (u32) (throttling->control_register.space_id));
826 return ret;
829 static int acpi_get_throttling_state(struct acpi_processor *pr,
830 u64 value)
832 int i;
834 for (i = 0; i < pr->throttling.state_count; i++) {
835 struct acpi_processor_tx_tss *tx =
836 (struct acpi_processor_tx_tss *)&(pr->throttling.
837 states_tss[i]);
838 if (tx->control == value)
839 return i;
841 return -1;
844 static int acpi_get_throttling_value(struct acpi_processor *pr,
845 int state, u64 *value)
847 int ret = -1;
849 if (state >= 0 && state <= pr->throttling.state_count) {
850 struct acpi_processor_tx_tss *tx =
851 (struct acpi_processor_tx_tss *)&(pr->throttling.
852 states_tss[state]);
853 *value = tx->control;
854 ret = 0;
856 return ret;
859 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
861 int state = 0;
862 int ret;
863 u64 value;
865 if (!pr)
866 return -EINVAL;
868 if (!pr->flags.throttling)
869 return -ENODEV;
871 pr->throttling.state = 0;
873 value = 0;
874 ret = acpi_read_throttling_status(pr, &value);
875 if (ret >= 0) {
876 state = acpi_get_throttling_state(pr, value);
877 if (state == -1) {
878 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
879 "Invalid throttling state, reset\n"));
880 state = 0;
881 ret = __acpi_processor_set_throttling(pr, state, true,
882 true);
883 if (ret)
884 return ret;
886 pr->throttling.state = state;
889 return 0;
892 static long __acpi_processor_get_throttling(void *data)
894 struct acpi_processor *pr = data;
896 return pr->throttling.acpi_processor_get_throttling(pr);
899 static int acpi_processor_get_throttling(struct acpi_processor *pr)
901 if (!pr)
902 return -EINVAL;
904 if (!pr->flags.throttling)
905 return -ENODEV;
908 * This is either called from the CPU hotplug callback of
909 * processor_driver or via the ACPI probe function. In the latter
910 * case the CPU is not guaranteed to be online. Both call sites are
911 * protected against CPU hotplug.
913 if (!cpu_online(pr->id))
914 return -ENODEV;
916 return call_on_cpu(pr->id, __acpi_processor_get_throttling, pr, false);
919 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
921 int i, step;
923 if (!pr->throttling.address) {
924 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
925 return -EINVAL;
926 } else if (!pr->throttling.duty_width) {
927 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
928 return -EINVAL;
930 /* TBD: Support duty_cycle values that span bit 4. */
931 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
932 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
933 return -EINVAL;
936 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
939 * Compute state values. Note that throttling displays a linear power
940 * performance relationship (at 50% performance the CPU will consume
941 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
944 step = (1000 / pr->throttling.state_count);
946 for (i = 0; i < pr->throttling.state_count; i++) {
947 pr->throttling.states[i].performance = 1000 - step * i;
948 pr->throttling.states[i].power = 1000 - step * i;
950 return 0;
953 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
954 int state, bool force)
956 u32 value = 0;
957 u32 duty_mask = 0;
958 u32 duty_value = 0;
960 if (!pr)
961 return -EINVAL;
963 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
964 return -EINVAL;
966 if (!pr->flags.throttling)
967 return -ENODEV;
969 if (!force && (state == pr->throttling.state))
970 return 0;
972 if (state < pr->throttling_platform_limit)
973 return -EPERM;
975 * Calculate the duty_value and duty_mask.
977 if (state) {
978 duty_value = pr->throttling.state_count - state;
980 duty_value <<= pr->throttling.duty_offset;
982 /* Used to clear all duty_value bits */
983 duty_mask = pr->throttling.state_count - 1;
985 duty_mask <<= acpi_gbl_FADT.duty_offset;
986 duty_mask = ~duty_mask;
989 local_irq_disable();
992 * Disable throttling by writing a 0 to bit 4. Note that we must
993 * turn it off before you can change the duty_value.
995 value = inl(pr->throttling.address);
996 if (value & 0x10) {
997 value &= 0xFFFFFFEF;
998 outl(value, pr->throttling.address);
1002 * Write the new duty_value and then enable throttling. Note
1003 * that a state value of 0 leaves throttling disabled.
1005 if (state) {
1006 value &= duty_mask;
1007 value |= duty_value;
1008 outl(value, pr->throttling.address);
1010 value |= 0x00000010;
1011 outl(value, pr->throttling.address);
1014 pr->throttling.state = state;
1016 local_irq_enable();
1018 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1019 "Throttling state set to T%d (%d%%)\n", state,
1020 (pr->throttling.states[state].performance ? pr->
1021 throttling.states[state].performance / 10 : 0)));
1023 return 0;
1026 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
1027 int state, bool force)
1029 int ret;
1030 u64 value;
1032 if (!pr)
1033 return -EINVAL;
1035 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1036 return -EINVAL;
1038 if (!pr->flags.throttling)
1039 return -ENODEV;
1041 if (!force && (state == pr->throttling.state))
1042 return 0;
1044 if (state < pr->throttling_platform_limit)
1045 return -EPERM;
1047 value = 0;
1048 ret = acpi_get_throttling_value(pr, state, &value);
1049 if (ret >= 0) {
1050 acpi_write_throttling_state(pr, value);
1051 pr->throttling.state = state;
1054 return 0;
1057 static long acpi_processor_throttling_fn(void *data)
1059 struct acpi_processor_throttling_arg *arg = data;
1060 struct acpi_processor *pr = arg->pr;
1062 return pr->throttling.acpi_processor_set_throttling(pr,
1063 arg->target_state, arg->force);
1066 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
1067 int state, bool force, bool direct)
1069 int ret = 0;
1070 unsigned int i;
1071 struct acpi_processor *match_pr;
1072 struct acpi_processor_throttling *p_throttling;
1073 struct acpi_processor_throttling_arg arg;
1074 struct throttling_tstate t_state;
1076 if (!pr)
1077 return -EINVAL;
1079 if (!pr->flags.throttling)
1080 return -ENODEV;
1082 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1083 return -EINVAL;
1085 if (cpu_is_offline(pr->id)) {
1087 * the cpu pointed by pr->id is offline. Unnecessary to change
1088 * the throttling state any more.
1090 return -ENODEV;
1093 t_state.target_state = state;
1094 p_throttling = &(pr->throttling);
1097 * The throttling notifier will be called for every
1098 * affected cpu in order to get one proper T-state.
1099 * The notifier event is THROTTLING_PRECHANGE.
1101 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1102 t_state.cpu = i;
1103 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1104 &t_state);
1107 * The function of acpi_processor_set_throttling will be called
1108 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1109 * it is necessary to call it for every affected cpu. Otherwise
1110 * it can be called only for the cpu pointed by pr.
1112 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
1113 arg.pr = pr;
1114 arg.target_state = state;
1115 arg.force = force;
1116 ret = call_on_cpu(pr->id, acpi_processor_throttling_fn, &arg,
1117 direct);
1118 } else {
1120 * When the T-state coordination is SW_ALL or HW_ALL,
1121 * it is necessary to set T-state for every affected
1122 * cpus.
1124 for_each_cpu_and(i, cpu_online_mask,
1125 p_throttling->shared_cpu_map) {
1126 match_pr = per_cpu(processors, i);
1128 * If the pointer is invalid, we will report the
1129 * error message and continue.
1131 if (!match_pr) {
1132 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1133 "Invalid Pointer for CPU %d\n", i));
1134 continue;
1137 * If the throttling control is unsupported on CPU i,
1138 * we will report the error message and continue.
1140 if (!match_pr->flags.throttling) {
1141 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1142 "Throttling Control is unsupported "
1143 "on CPU %d\n", i));
1144 continue;
1147 arg.pr = match_pr;
1148 arg.target_state = state;
1149 arg.force = force;
1150 ret = call_on_cpu(pr->id, acpi_processor_throttling_fn,
1151 &arg, direct);
1155 * After the set_throttling is called, the
1156 * throttling notifier is called for every
1157 * affected cpu to update the T-states.
1158 * The notifier event is THROTTLING_POSTCHANGE
1160 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1161 t_state.cpu = i;
1162 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1163 &t_state);
1166 return ret;
1169 int acpi_processor_set_throttling(struct acpi_processor *pr, int state,
1170 bool force)
1172 return __acpi_processor_set_throttling(pr, state, force, false);
1175 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1177 int result = 0;
1178 struct acpi_processor_throttling *pthrottling;
1180 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1181 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1182 pr->throttling.address,
1183 pr->throttling.duty_offset,
1184 pr->throttling.duty_width));
1187 * Evaluate _PTC, _TSS and _TPC
1188 * They must all be present or none of them can be used.
1190 if (acpi_processor_get_throttling_control(pr) ||
1191 acpi_processor_get_throttling_states(pr) ||
1192 acpi_processor_get_platform_limit(pr))
1194 pr->throttling.acpi_processor_get_throttling =
1195 &acpi_processor_get_throttling_fadt;
1196 pr->throttling.acpi_processor_set_throttling =
1197 &acpi_processor_set_throttling_fadt;
1198 if (acpi_processor_get_fadt_info(pr))
1199 return 0;
1200 } else {
1201 pr->throttling.acpi_processor_get_throttling =
1202 &acpi_processor_get_throttling_ptc;
1203 pr->throttling.acpi_processor_set_throttling =
1204 &acpi_processor_set_throttling_ptc;
1208 * If TSD package for one CPU can't be parsed successfully, it means
1209 * that this CPU will have no coordination with other CPUs.
1211 if (acpi_processor_get_tsd(pr)) {
1212 pthrottling = &pr->throttling;
1213 pthrottling->tsd_valid_flag = 0;
1214 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1215 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1219 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1220 * This shouldn't be an issue as few (if any) mobile systems ever
1221 * used this part.
1223 if (errata.piix4.throttle) {
1224 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1225 "Throttling not supported on PIIX4 A- or B-step\n"));
1226 return 0;
1229 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
1230 pr->throttling.state_count));
1232 pr->flags.throttling = 1;
1235 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1236 * thermal) decide to lower performance if it so chooses, but for now
1237 * we'll crank up the speed.
1240 result = acpi_processor_get_throttling(pr);
1241 if (result)
1242 goto end;
1244 if (pr->throttling.state) {
1245 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1246 "Disabling throttling (was T%d)\n",
1247 pr->throttling.state));
1248 result = acpi_processor_set_throttling(pr, 0, false);
1249 if (result)
1250 goto end;
1253 end:
1254 if (result)
1255 pr->flags.throttling = 0;
1257 return result;