xref: /linux/drivers/acpi/processor_throttling.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * processor_throttling.c - Throttling submodule of the ACPI processor driver
4  *
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
10  */
11 
12 #define pr_fmt(fmt) "ACPI: " fmt
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/cpufreq.h>
20 #include <linux/acpi.h>
21 #include <linux/uaccess.h>
22 #include <acpi/processor.h>
23 #include <asm/io.h>
24 #ifdef CONFIG_X86
25 #include <asm/msr.h>
26 #endif
27 
28 /* ignore_tpc:
29  *  0 -> acpi processor driver doesn't ignore _TPC values
30  *  1 -> acpi processor driver ignores _TPC values
31  */
32 static int ignore_tpc;
33 module_param(ignore_tpc, int, 0644);
34 MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
35 
36 struct throttling_tstate {
37 	unsigned int cpu;		/* cpu nr */
38 	int target_state;		/* target T-state */
39 };
40 
41 struct acpi_processor_throttling_arg {
42 	struct acpi_processor *pr;
43 	int target_state;
44 	bool force;
45 };
46 
47 #define THROTTLING_PRECHANGE       (1)
48 #define THROTTLING_POSTCHANGE      (2)
49 
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);
53 
54 static int acpi_processor_update_tsd_coord(void)
55 {
56 	int 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;
63 
64 	if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
65 		return -ENOMEM;
66 
67 	/*
68 	 * Now that we have _TSD data from all CPUs, lets setup T-state
69 	 * coordination between all CPUs.
70 	 */
71 	for_each_possible_cpu(i) {
72 		pr = per_cpu(processors, i);
73 		if (!pr)
74 			continue;
75 
76 		/* Basic validity check for domain info */
77 		pthrottling = &(pr->throttling);
78 
79 		/*
80 		 * If tsd package for one cpu is invalid, the coordination
81 		 * among all CPUs is thought as invalid.
82 		 * Maybe it is ugly.
83 		 */
84 		if (!pthrottling->tsd_valid_flag) {
85 			retval = -EINVAL;
86 			break;
87 		}
88 	}
89 	if (retval)
90 		goto err_ret;
91 
92 	for_each_possible_cpu(i) {
93 		pr = per_cpu(processors, i);
94 		if (!pr)
95 			continue;
96 
97 		if (cpumask_test_cpu(i, covered_cpus))
98 			continue;
99 		pthrottling = &pr->throttling;
100 
101 		pdomain = &(pthrottling->domain_info);
102 		cpumask_set_cpu(i, pthrottling->shared_cpu_map);
103 		cpumask_set_cpu(i, covered_cpus);
104 		/*
105 		 * If the number of processor in the TSD domain is 1, it is
106 		 * unnecessary to parse the coordination for this CPU.
107 		 */
108 		if (pdomain->num_processors <= 1)
109 			continue;
110 
111 		/* Validate the Domain info */
112 		count_target = pdomain->num_processors;
113 
114 		for_each_possible_cpu(j) {
115 			if (i == j)
116 				continue;
117 
118 			match_pr = per_cpu(processors, j);
119 			if (!match_pr)
120 				continue;
121 
122 			match_pthrottling = &(match_pr->throttling);
123 			match_pdomain = &(match_pthrottling->domain_info);
124 			if (match_pdomain->domain != pdomain->domain)
125 				continue;
126 
127 			/* Here i and j are in the same domain.
128 			 * If two TSD packages have the same domain, they
129 			 * should have the same num_porcessors and
130 			 * coordination type. Otherwise it will be regarded
131 			 * as illegal.
132 			 */
133 			if (match_pdomain->num_processors != count_target) {
134 				retval = -EINVAL;
135 				goto err_ret;
136 			}
137 
138 			if (pdomain->coord_type != match_pdomain->coord_type) {
139 				retval = -EINVAL;
140 				goto err_ret;
141 			}
142 
143 			cpumask_set_cpu(j, covered_cpus);
144 			cpumask_set_cpu(j, pthrottling->shared_cpu_map);
145 		}
146 		for_each_possible_cpu(j) {
147 			if (i == j)
148 				continue;
149 
150 			match_pr = per_cpu(processors, j);
151 			if (!match_pr)
152 				continue;
153 
154 			match_pthrottling = &(match_pr->throttling);
155 			match_pdomain = &(match_pthrottling->domain_info);
156 			if (match_pdomain->domain != pdomain->domain)
157 				continue;
158 
159 			/*
160 			 * If some CPUS have the same domain, they
161 			 * will have the same shared_cpu_map.
162 			 */
163 			cpumask_copy(match_pthrottling->shared_cpu_map,
164 				     pthrottling->shared_cpu_map);
165 		}
166 	}
167 
168 err_ret:
169 	free_cpumask_var(covered_cpus);
170 
171 	for_each_possible_cpu(i) {
172 		pr = per_cpu(processors, i);
173 		if (!pr)
174 			continue;
175 
176 		/*
177 		 * Assume no coordination on any error parsing domain info.
178 		 * The coordination type will be forced as SW_ALL.
179 		 */
180 		if (retval) {
181 			pthrottling = &(pr->throttling);
182 			cpumask_clear(pthrottling->shared_cpu_map);
183 			cpumask_set_cpu(i, pthrottling->shared_cpu_map);
184 			pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
185 		}
186 	}
187 
188 	return retval;
189 }
190 
191 /*
192  * Update the T-state coordination after the _TSD
193  * data for all cpus is obtained.
194  */
195 void acpi_processor_throttling_init(void)
196 {
197 	if (acpi_processor_update_tsd_coord())
198 		pr_debug("Assume no T-state coordination\n");
199 }
200 
201 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
202 {
203 	struct throttling_tstate *p_tstate = data;
204 	struct acpi_processor *pr;
205 	unsigned int cpu;
206 	int target_state;
207 	struct acpi_processor_limit *p_limit;
208 	struct acpi_processor_throttling *p_throttling;
209 
210 	cpu = p_tstate->cpu;
211 	pr = per_cpu(processors, cpu);
212 	if (!pr) {
213 		pr_debug("Invalid pr pointer\n");
214 		return 0;
215 	}
216 	if (!pr->flags.throttling) {
217 		acpi_handle_debug(pr->handle,
218 				  "Throttling control unsupported on CPU %d\n",
219 				  cpu);
220 		return 0;
221 	}
222 	target_state = p_tstate->target_state;
223 	p_throttling = &(pr->throttling);
224 	switch (event) {
225 	case THROTTLING_PRECHANGE:
226 		/*
227 		 * Prechange event is used to choose one proper t-state,
228 		 * which meets the limits of thermal, user and _TPC.
229 		 */
230 		p_limit = &pr->limit;
231 		if (p_limit->thermal.tx > target_state)
232 			target_state = p_limit->thermal.tx;
233 		if (p_limit->user.tx > target_state)
234 			target_state = p_limit->user.tx;
235 		if (pr->throttling_platform_limit > target_state)
236 			target_state = pr->throttling_platform_limit;
237 		if (target_state >= p_throttling->state_count) {
238 			pr_warn("Exceed the limit of T-state\n");
239 			target_state = p_throttling->state_count - 1;
240 		}
241 		p_tstate->target_state = target_state;
242 		acpi_handle_debug(pr->handle,
243 				  "PreChange Event: target T-state of CPU %d is T%d\n",
244 				  cpu, target_state);
245 		break;
246 	case THROTTLING_POSTCHANGE:
247 		/*
248 		 * Postchange event is only used to update the
249 		 * T-state flag of acpi_processor_throttling.
250 		 */
251 		p_throttling->state = target_state;
252 		acpi_handle_debug(pr->handle,
253 				  "PostChange Event: CPU %d is switched to T%d\n",
254 				  cpu, target_state);
255 		break;
256 	default:
257 		pr_warn("Unsupported Throttling notifier event\n");
258 		break;
259 	}
260 
261 	return 0;
262 }
263 
264 /*
265  * _TPC - Throttling Present Capabilities
266  */
267 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
268 {
269 	acpi_status status = 0;
270 	unsigned long long tpc = 0;
271 
272 	if (!pr)
273 		return -EINVAL;
274 
275 	if (ignore_tpc)
276 		goto end;
277 
278 	status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
279 	if (ACPI_FAILURE(status)) {
280 		if (status != AE_NOT_FOUND)
281 			acpi_evaluation_failure_warn(pr->handle, "_TPC", status);
282 
283 		return -ENODEV;
284 	}
285 
286 end:
287 	pr->throttling_platform_limit = (int)tpc;
288 	return 0;
289 }
290 
291 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
292 {
293 	int result = 0;
294 	int throttling_limit;
295 	int current_state;
296 	struct acpi_processor_limit *limit;
297 	int target_state;
298 
299 	if (ignore_tpc)
300 		return 0;
301 
302 	result = acpi_processor_get_platform_limit(pr);
303 	if (result) {
304 		/* Throttling Limit is unsupported */
305 		return result;
306 	}
307 
308 	throttling_limit = pr->throttling_platform_limit;
309 	if (throttling_limit >= pr->throttling.state_count) {
310 		/* Uncorrect Throttling Limit */
311 		return -EINVAL;
312 	}
313 
314 	current_state = pr->throttling.state;
315 	if (current_state > throttling_limit) {
316 		/*
317 		 * The current state can meet the requirement of
318 		 * _TPC limit. But it is reasonable that OSPM changes
319 		 * t-states from high to low for better performance.
320 		 * Of course the limit condition of thermal
321 		 * and user should be considered.
322 		 */
323 		limit = &pr->limit;
324 		target_state = throttling_limit;
325 		if (limit->thermal.tx > target_state)
326 			target_state = limit->thermal.tx;
327 		if (limit->user.tx > target_state)
328 			target_state = limit->user.tx;
329 	} else if (current_state == throttling_limit) {
330 		/*
331 		 * Unnecessary to change the throttling state
332 		 */
333 		return 0;
334 	} else {
335 		/*
336 		 * If the current state is lower than the limit of _TPC, it
337 		 * will be forced to switch to the throttling state defined
338 		 * by throttling_platfor_limit.
339 		 * Because the previous state meets with the limit condition
340 		 * of thermal and user, it is unnecessary to check it again.
341 		 */
342 		target_state = throttling_limit;
343 	}
344 	return acpi_processor_set_throttling(pr, target_state, false);
345 }
346 
347 /*
348  * This function is used to reevaluate whether the T-state is valid
349  * after one CPU is onlined/offlined.
350  * It is noted that it won't reevaluate the following properties for
351  * the T-state.
352  *	1. Control method.
353  *	2. the number of supported T-state
354  *	3. TSD domain
355  */
356 void acpi_processor_reevaluate_tstate(struct acpi_processor *pr,
357 					bool is_dead)
358 {
359 	int result = 0;
360 
361 	if (is_dead) {
362 		/* When one CPU is offline, the T-state throttling
363 		 * will be invalidated.
364 		 */
365 		pr->flags.throttling = 0;
366 		return;
367 	}
368 	/* the following is to recheck whether the T-state is valid for
369 	 * the online CPU
370 	 */
371 	if (!pr->throttling.state_count) {
372 		/* If the number of T-state is invalid, it is
373 		 * invalidated.
374 		 */
375 		pr->flags.throttling = 0;
376 		return;
377 	}
378 	pr->flags.throttling = 1;
379 
380 	/* Disable throttling (if enabled).  We'll let subsequent
381 	 * policy (e.g.thermal) decide to lower performance if it
382 	 * so chooses, but for now we'll crank up the speed.
383 	 */
384 
385 	result = acpi_processor_get_throttling(pr);
386 	if (result)
387 		goto end;
388 
389 	if (pr->throttling.state) {
390 		result = acpi_processor_set_throttling(pr, 0, false);
391 		if (result)
392 			goto end;
393 	}
394 
395 end:
396 	if (result)
397 		pr->flags.throttling = 0;
398 }
399 /*
400  * _PTC - Processor Throttling Control (and status) register location
401  */
402 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
403 {
404 	int result = 0;
405 	acpi_status status = 0;
406 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
407 	union acpi_object *ptc = NULL;
408 	union acpi_object obj;
409 	struct acpi_processor_throttling *throttling;
410 
411 	status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
412 	if (ACPI_FAILURE(status)) {
413 		if (status != AE_NOT_FOUND)
414 			acpi_evaluation_failure_warn(pr->handle, "_PTC", status);
415 
416 		return -ENODEV;
417 	}
418 
419 	ptc = (union acpi_object *)buffer.pointer;
420 	if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
421 	    || (ptc->package.count != 2)) {
422 		pr_err("Invalid _PTC data\n");
423 		result = -EFAULT;
424 		goto end;
425 	}
426 
427 	/*
428 	 * control_register
429 	 */
430 
431 	obj = ptc->package.elements[0];
432 
433 	if ((obj.type != ACPI_TYPE_BUFFER)
434 	    || (obj.buffer.length < sizeof(struct acpi_ptc_register))
435 	    || (obj.buffer.pointer == NULL)) {
436 		pr_err("Invalid _PTC data (control_register)\n");
437 		result = -EFAULT;
438 		goto end;
439 	}
440 	memcpy(&pr->throttling.control_register, obj.buffer.pointer,
441 	       sizeof(struct acpi_ptc_register));
442 
443 	/*
444 	 * status_register
445 	 */
446 
447 	obj = ptc->package.elements[1];
448 
449 	if ((obj.type != ACPI_TYPE_BUFFER)
450 	    || (obj.buffer.length < sizeof(struct acpi_ptc_register))
451 	    || (obj.buffer.pointer == NULL)) {
452 		pr_err("Invalid _PTC data (status_register)\n");
453 		result = -EFAULT;
454 		goto end;
455 	}
456 
457 	memcpy(&pr->throttling.status_register, obj.buffer.pointer,
458 	       sizeof(struct acpi_ptc_register));
459 
460 	throttling = &pr->throttling;
461 
462 	if ((throttling->control_register.bit_width +
463 		throttling->control_register.bit_offset) > 32) {
464 		pr_err("Invalid _PTC control register\n");
465 		result = -EFAULT;
466 		goto end;
467 	}
468 
469 	if ((throttling->status_register.bit_width +
470 		throttling->status_register.bit_offset) > 32) {
471 		pr_err("Invalid _PTC status register\n");
472 		result = -EFAULT;
473 		goto end;
474 	}
475 
476 end:
477 	kfree(buffer.pointer);
478 
479 	return result;
480 }
481 
482 /*
483  * _TSS - Throttling Supported States
484  */
485 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
486 {
487 	int result = 0;
488 	acpi_status status = AE_OK;
489 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
490 	struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
491 	struct acpi_buffer state = { 0, NULL };
492 	union acpi_object *tss = NULL;
493 	int i;
494 
495 	status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
496 	if (ACPI_FAILURE(status)) {
497 		if (status != AE_NOT_FOUND)
498 			acpi_evaluation_failure_warn(pr->handle, "_TSS", status);
499 
500 		return -ENODEV;
501 	}
502 
503 	tss = buffer.pointer;
504 	if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
505 		pr_err("Invalid _TSS data\n");
506 		result = -EFAULT;
507 		goto end;
508 	}
509 
510 	acpi_handle_debug(pr->handle, "Found %d throttling states\n",
511 			  tss->package.count);
512 
513 	pr->throttling.state_count = tss->package.count;
514 	pr->throttling.states_tss =
515 	    kmalloc_objs(struct acpi_processor_tx_tss, tss->package.count);
516 	if (!pr->throttling.states_tss) {
517 		result = -ENOMEM;
518 		goto end;
519 	}
520 
521 	for (i = 0; i < pr->throttling.state_count; i++) {
522 
523 		struct acpi_processor_tx_tss *tx =
524 		    (struct acpi_processor_tx_tss *)&(pr->throttling.
525 						      states_tss[i]);
526 
527 		state.length = sizeof(struct acpi_processor_tx_tss);
528 		state.pointer = tx;
529 
530 		acpi_handle_debug(pr->handle, "Extracting state %d\n", i);
531 
532 		status = acpi_extract_package(&(tss->package.elements[i]),
533 					      &format, &state);
534 		if (ACPI_FAILURE(status)) {
535 			acpi_handle_warn(pr->handle, "Invalid _TSS data: %s\n",
536 					 acpi_format_exception(status));
537 			result = -EFAULT;
538 			kfree(pr->throttling.states_tss);
539 			goto end;
540 		}
541 
542 		if (!tx->freqpercentage) {
543 			pr_err("Invalid _TSS data: freq is zero\n");
544 			result = -EFAULT;
545 			kfree(pr->throttling.states_tss);
546 			goto end;
547 		}
548 	}
549 
550 end:
551 	kfree(buffer.pointer);
552 
553 	return result;
554 }
555 
556 /*
557  * _TSD - T-State Dependencies
558  */
559 static int acpi_processor_get_tsd(struct acpi_processor *pr)
560 {
561 	int result = 0;
562 	acpi_status status = AE_OK;
563 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
564 	struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
565 	struct acpi_buffer state = { 0, NULL };
566 	union acpi_object *tsd = NULL;
567 	struct acpi_tsd_package *pdomain;
568 	struct acpi_processor_throttling *pthrottling;
569 
570 	pthrottling = &pr->throttling;
571 	pthrottling->tsd_valid_flag = 0;
572 
573 	status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
574 	if (ACPI_FAILURE(status)) {
575 		if (status != AE_NOT_FOUND)
576 			acpi_evaluation_failure_warn(pr->handle, "_TSD", status);
577 
578 		return -ENODEV;
579 	}
580 
581 	tsd = buffer.pointer;
582 	if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
583 		pr_err("Invalid _TSD data\n");
584 		result = -EFAULT;
585 		goto end;
586 	}
587 
588 	if (tsd->package.count != 1) {
589 		pr_err("Invalid _TSD data\n");
590 		result = -EFAULT;
591 		goto end;
592 	}
593 
594 	pdomain = &(pr->throttling.domain_info);
595 
596 	state.length = sizeof(struct acpi_tsd_package);
597 	state.pointer = pdomain;
598 
599 	status = acpi_extract_package(&(tsd->package.elements[0]),
600 				      &format, &state);
601 	if (ACPI_FAILURE(status)) {
602 		pr_err("Invalid _TSD data\n");
603 		result = -EFAULT;
604 		goto end;
605 	}
606 
607 	if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
608 		pr_err("Unknown _TSD:num_entries\n");
609 		result = -EFAULT;
610 		goto end;
611 	}
612 
613 	if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
614 		pr_err("Unknown _TSD:revision\n");
615 		result = -EFAULT;
616 		goto end;
617 	}
618 
619 	pthrottling = &pr->throttling;
620 	pthrottling->tsd_valid_flag = 1;
621 	pthrottling->shared_type = pdomain->coord_type;
622 	cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
623 	/*
624 	 * If the coordination type is not defined in ACPI spec,
625 	 * the tsd_valid_flag will be clear and coordination type
626 	 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
627 	 */
628 	if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
629 		pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
630 		pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
631 		pthrottling->tsd_valid_flag = 0;
632 		pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
633 	}
634 
635 end:
636 	kfree(buffer.pointer);
637 	return result;
638 }
639 
640 /* --------------------------------------------------------------------------
641                               Throttling Control
642    -------------------------------------------------------------------------- */
643 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
644 {
645 	int state = 0;
646 	u32 value = 0;
647 	u32 duty_mask = 0;
648 	u32 duty_value = 0;
649 
650 	if (!pr)
651 		return -EINVAL;
652 
653 	if (!pr->flags.throttling)
654 		return -ENODEV;
655 
656 	/*
657 	 * We don't care about error returns - we just try to mark
658 	 * these reserved so that nobody else is confused into thinking
659 	 * that this region might be unused..
660 	 *
661 	 * (In particular, allocating the IO range for Cardbus)
662 	 */
663 	request_region(pr->throttling.address, 6, "ACPI CPU throttle");
664 
665 	pr->throttling.state = 0;
666 
667 	duty_mask = pr->throttling.state_count - 1;
668 
669 	duty_mask <<= pr->throttling.duty_offset;
670 
671 	local_irq_disable();
672 
673 	value = inl(pr->throttling.address);
674 
675 	/*
676 	 * Compute the current throttling state when throttling is enabled
677 	 * (bit 4 is on).
678 	 */
679 	if (value & 0x10) {
680 		duty_value = value & duty_mask;
681 		duty_value >>= pr->throttling.duty_offset;
682 
683 		if (duty_value)
684 			state = pr->throttling.state_count - duty_value;
685 	}
686 
687 	pr->throttling.state = state;
688 
689 	local_irq_enable();
690 
691 	acpi_handle_debug(pr->handle,
692 			  "Throttling state is T%d (%d%% throttling applied)\n",
693 			  state, pr->throttling.states[state].performance);
694 
695 	return 0;
696 }
697 
698 #ifdef CONFIG_X86
699 static int acpi_throttling_rdmsr(u64 *value)
700 {
701 	int ret = -1;
702 
703 	if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
704 		!this_cpu_has(X86_FEATURE_ACPI)) {
705 		pr_err("HARDWARE addr space,NOT supported yet\n");
706 	} else {
707 		rdmsrq_safe(MSR_IA32_THERM_CONTROL, value);
708 		ret = 0;
709 	}
710 	return ret;
711 }
712 
713 static int acpi_throttling_wrmsr(u64 value)
714 {
715 	int ret = -1;
716 
717 	if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
718 		!this_cpu_has(X86_FEATURE_ACPI)) {
719 		pr_err("HARDWARE addr space,NOT supported yet\n");
720 	} else {
721 		wrmsrq_safe(MSR_IA32_THERM_CONTROL, value);
722 		ret = 0;
723 	}
724 	return ret;
725 }
726 #else
727 static int acpi_throttling_rdmsr(u64 *value)
728 {
729 	pr_err("HARDWARE addr space,NOT supported yet\n");
730 	return -1;
731 }
732 
733 static int acpi_throttling_wrmsr(u64 value)
734 {
735 	pr_err("HARDWARE addr space,NOT supported yet\n");
736 	return -1;
737 }
738 #endif
739 
740 static int acpi_read_throttling_status(struct acpi_processor *pr,
741 					u64 *value)
742 {
743 	u32 bit_width, bit_offset;
744 	u32 ptc_value;
745 	u64 ptc_mask;
746 	struct acpi_processor_throttling *throttling;
747 	int ret = -1;
748 
749 	throttling = &pr->throttling;
750 	switch (throttling->status_register.space_id) {
751 	case ACPI_ADR_SPACE_SYSTEM_IO:
752 		bit_width = throttling->status_register.bit_width;
753 		bit_offset = throttling->status_register.bit_offset;
754 
755 		acpi_os_read_port((acpi_io_address) throttling->status_register.
756 				  address, &ptc_value,
757 				  (u32) (bit_width + bit_offset));
758 		ptc_mask = (1 << bit_width) - 1;
759 		*value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
760 		ret = 0;
761 		break;
762 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
763 		ret = acpi_throttling_rdmsr(value);
764 		break;
765 	default:
766 		pr_err("Unknown addr space %d\n",
767 		       (u32) (throttling->status_register.space_id));
768 	}
769 	return ret;
770 }
771 
772 static int acpi_write_throttling_state(struct acpi_processor *pr,
773 				u64 value)
774 {
775 	u32 bit_width, bit_offset;
776 	u64 ptc_value;
777 	u64 ptc_mask;
778 	struct acpi_processor_throttling *throttling;
779 	int ret = -1;
780 
781 	throttling = &pr->throttling;
782 	switch (throttling->control_register.space_id) {
783 	case ACPI_ADR_SPACE_SYSTEM_IO:
784 		bit_width = throttling->control_register.bit_width;
785 		bit_offset = throttling->control_register.bit_offset;
786 		ptc_mask = (1 << bit_width) - 1;
787 		ptc_value = value & ptc_mask;
788 
789 		acpi_os_write_port((acpi_io_address) throttling->
790 					control_register.address,
791 					(u32) (ptc_value << bit_offset),
792 					(u32) (bit_width + bit_offset));
793 		ret = 0;
794 		break;
795 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
796 		ret = acpi_throttling_wrmsr(value);
797 		break;
798 	default:
799 		pr_err("Unknown addr space %d\n",
800 		       (u32) (throttling->control_register.space_id));
801 	}
802 	return ret;
803 }
804 
805 static int acpi_get_throttling_state(struct acpi_processor *pr,
806 				u64 value)
807 {
808 	int i;
809 
810 	for (i = 0; i < pr->throttling.state_count; i++) {
811 		struct acpi_processor_tx_tss *tx =
812 		    (struct acpi_processor_tx_tss *)&(pr->throttling.
813 						      states_tss[i]);
814 		if (tx->control == value)
815 			return i;
816 	}
817 	return -1;
818 }
819 
820 static int acpi_get_throttling_value(struct acpi_processor *pr,
821 			int state, u64 *value)
822 {
823 	int ret = -1;
824 
825 	if (state >= 0 && state <= pr->throttling.state_count) {
826 		struct acpi_processor_tx_tss *tx =
827 		    (struct acpi_processor_tx_tss *)&(pr->throttling.
828 						      states_tss[state]);
829 		*value = tx->control;
830 		ret = 0;
831 	}
832 	return ret;
833 }
834 
835 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
836 {
837 	int state = 0;
838 	int ret;
839 	u64 value;
840 
841 	if (!pr)
842 		return -EINVAL;
843 
844 	if (!pr->flags.throttling)
845 		return -ENODEV;
846 
847 	pr->throttling.state = 0;
848 
849 	value = 0;
850 	ret = acpi_read_throttling_status(pr, &value);
851 	if (ret >= 0) {
852 		state = acpi_get_throttling_state(pr, value);
853 		if (state == -1) {
854 			acpi_handle_debug(pr->handle,
855 					  "Invalid throttling state, reset\n");
856 			state = 0;
857 			ret = __acpi_processor_set_throttling(pr, state, true,
858 							      true);
859 			if (ret)
860 				return ret;
861 		}
862 		pr->throttling.state = state;
863 	}
864 
865 	return 0;
866 }
867 
868 static long __acpi_processor_get_throttling(void *data)
869 {
870 	struct acpi_processor *pr = data;
871 
872 	return pr->throttling.acpi_processor_get_throttling(pr);
873 }
874 
875 static int acpi_processor_get_throttling(struct acpi_processor *pr)
876 {
877 	if (!pr)
878 		return -EINVAL;
879 
880 	if (!pr->flags.throttling)
881 		return -ENODEV;
882 
883 	/*
884 	 * This is either called from the CPU hotplug callback of
885 	 * processor_driver or via the ACPI probe function. In the latter
886 	 * case the CPU is not guaranteed to be online. Both call sites are
887 	 * protected against CPU hotplug.
888 	 */
889 	if (!cpu_online(pr->id))
890 		return -ENODEV;
891 
892 	return call_on_cpu(pr->id, __acpi_processor_get_throttling, pr, false);
893 }
894 
895 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
896 {
897 	int i, step;
898 
899 	if (!pr->throttling.address) {
900 		acpi_handle_debug(pr->handle, "No throttling register\n");
901 		return -EINVAL;
902 	} else if (!pr->throttling.duty_width) {
903 		acpi_handle_debug(pr->handle, "No throttling states\n");
904 		return -EINVAL;
905 	}
906 	/* TBD: Support duty_cycle values that span bit 4. */
907 	else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
908 		pr_warn("duty_cycle spans bit 4\n");
909 		return -EINVAL;
910 	}
911 
912 	pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
913 
914 	/*
915 	 * Compute state values. Note that throttling displays a linear power
916 	 * performance relationship (at 50% performance the CPU will consume
917 	 * 50% power).  Values are in 1/10th of a percent to preserve accuracy.
918 	 */
919 
920 	step = (1000 / pr->throttling.state_count);
921 
922 	for (i = 0; i < pr->throttling.state_count; i++) {
923 		pr->throttling.states[i].performance = 1000 - step * i;
924 		pr->throttling.states[i].power = 1000 - step * i;
925 	}
926 	return 0;
927 }
928 
929 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
930 					      int state, bool force)
931 {
932 	u32 value = 0;
933 	u32 duty_mask = 0;
934 	u32 duty_value = 0;
935 
936 	if (!pr)
937 		return -EINVAL;
938 
939 	if ((state < 0) || (state > (pr->throttling.state_count - 1)))
940 		return -EINVAL;
941 
942 	if (!pr->flags.throttling)
943 		return -ENODEV;
944 
945 	if (!force && (state == pr->throttling.state))
946 		return 0;
947 
948 	if (state < pr->throttling_platform_limit)
949 		return -EPERM;
950 	/*
951 	 * Calculate the duty_value and duty_mask.
952 	 */
953 	if (state) {
954 		duty_value = pr->throttling.state_count - state;
955 
956 		duty_value <<= pr->throttling.duty_offset;
957 
958 		/* Used to clear all duty_value bits */
959 		duty_mask = pr->throttling.state_count - 1;
960 
961 		duty_mask <<= acpi_gbl_FADT.duty_offset;
962 		duty_mask = ~duty_mask;
963 	}
964 
965 	local_irq_disable();
966 
967 	/*
968 	 * Disable throttling by writing a 0 to bit 4.  Note that we must
969 	 * turn it off before you can change the duty_value.
970 	 */
971 	value = inl(pr->throttling.address);
972 	if (value & 0x10) {
973 		value &= 0xFFFFFFEF;
974 		outl(value, pr->throttling.address);
975 	}
976 
977 	/*
978 	 * Write the new duty_value and then enable throttling.  Note
979 	 * that a state value of 0 leaves throttling disabled.
980 	 */
981 	if (state) {
982 		value &= duty_mask;
983 		value |= duty_value;
984 		outl(value, pr->throttling.address);
985 
986 		value |= 0x00000010;
987 		outl(value, pr->throttling.address);
988 	}
989 
990 	pr->throttling.state = state;
991 
992 	local_irq_enable();
993 
994 	acpi_handle_debug(pr->handle,
995 			  "Throttling state set to T%d (%d%%)\n", state,
996 			  (pr->throttling.states[state].performance ? pr->
997 			   throttling.states[state].performance / 10 : 0));
998 
999 	return 0;
1000 }
1001 
1002 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
1003 					     int state, bool force)
1004 {
1005 	int ret;
1006 	u64 value;
1007 
1008 	if (!pr)
1009 		return -EINVAL;
1010 
1011 	if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1012 		return -EINVAL;
1013 
1014 	if (!pr->flags.throttling)
1015 		return -ENODEV;
1016 
1017 	if (!force && (state == pr->throttling.state))
1018 		return 0;
1019 
1020 	if (state < pr->throttling_platform_limit)
1021 		return -EPERM;
1022 
1023 	value = 0;
1024 	ret = acpi_get_throttling_value(pr, state, &value);
1025 	if (ret >= 0) {
1026 		acpi_write_throttling_state(pr, value);
1027 		pr->throttling.state = state;
1028 	}
1029 
1030 	return 0;
1031 }
1032 
1033 static long acpi_processor_throttling_fn(void *data)
1034 {
1035 	struct acpi_processor_throttling_arg *arg = data;
1036 	struct acpi_processor *pr = arg->pr;
1037 
1038 	return pr->throttling.acpi_processor_set_throttling(pr,
1039 			arg->target_state, arg->force);
1040 }
1041 
1042 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
1043 					   int state, bool force, bool direct)
1044 {
1045 	int ret = 0;
1046 	unsigned int i;
1047 	struct acpi_processor *match_pr;
1048 	struct acpi_processor_throttling *p_throttling;
1049 	struct acpi_processor_throttling_arg arg;
1050 	struct throttling_tstate t_state;
1051 
1052 	if (!pr)
1053 		return -EINVAL;
1054 
1055 	if (!pr->flags.throttling)
1056 		return -ENODEV;
1057 
1058 	if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1059 		return -EINVAL;
1060 
1061 	if (cpu_is_offline(pr->id)) {
1062 		/*
1063 		 * the cpu pointed by pr->id is offline. Unnecessary to change
1064 		 * the throttling state any more.
1065 		 */
1066 		return -ENODEV;
1067 	}
1068 
1069 	t_state.target_state = state;
1070 	p_throttling = &(pr->throttling);
1071 
1072 	/*
1073 	 * The throttling notifier will be called for every
1074 	 * affected cpu in order to get one proper T-state.
1075 	 * The notifier event is THROTTLING_PRECHANGE.
1076 	 */
1077 	for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1078 		t_state.cpu = i;
1079 		acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1080 							&t_state);
1081 	}
1082 	/*
1083 	 * The function of acpi_processor_set_throttling will be called
1084 	 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1085 	 * it is necessary to call it for every affected cpu. Otherwise
1086 	 * it can be called only for the cpu pointed by pr.
1087 	 */
1088 	if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
1089 		arg.pr = pr;
1090 		arg.target_state = state;
1091 		arg.force = force;
1092 		ret = call_on_cpu(pr->id, acpi_processor_throttling_fn, &arg,
1093 				  direct);
1094 	} else {
1095 		/*
1096 		 * When the T-state coordination is SW_ALL or HW_ALL,
1097 		 * it is necessary to set T-state for every affected
1098 		 * cpus.
1099 		 */
1100 		for_each_cpu_and(i, cpu_online_mask,
1101 		    p_throttling->shared_cpu_map) {
1102 			match_pr = per_cpu(processors, i);
1103 			/*
1104 			 * If the pointer is invalid, we will report the
1105 			 * error message and continue.
1106 			 */
1107 			if (!match_pr) {
1108 				acpi_handle_debug(pr->handle,
1109 					"Invalid Pointer for CPU %d\n", i);
1110 				continue;
1111 			}
1112 			/*
1113 			 * If the throttling control is unsupported on CPU i,
1114 			 * we will report the error message and continue.
1115 			 */
1116 			if (!match_pr->flags.throttling) {
1117 				acpi_handle_debug(pr->handle,
1118 					"Throttling Control unsupported on CPU %d\n", i);
1119 				continue;
1120 			}
1121 
1122 			arg.pr = match_pr;
1123 			arg.target_state = state;
1124 			arg.force = force;
1125 			ret = call_on_cpu(pr->id, acpi_processor_throttling_fn,
1126 					  &arg, direct);
1127 		}
1128 	}
1129 	/*
1130 	 * After the set_throttling is called, the
1131 	 * throttling notifier is called for every
1132 	 * affected cpu to update the T-states.
1133 	 * The notifier event is THROTTLING_POSTCHANGE
1134 	 */
1135 	for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1136 		t_state.cpu = i;
1137 		acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1138 							&t_state);
1139 	}
1140 
1141 	return ret;
1142 }
1143 
1144 int acpi_processor_set_throttling(struct acpi_processor *pr, int state,
1145 				  bool force)
1146 {
1147 	return __acpi_processor_set_throttling(pr, state, force, false);
1148 }
1149 
1150 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1151 {
1152 	int result = 0;
1153 	struct acpi_processor_throttling *pthrottling;
1154 
1155 	acpi_handle_debug(pr->handle,
1156 			  "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1157 			  pr->throttling.address,
1158 			  pr->throttling.duty_offset,
1159 			  pr->throttling.duty_width);
1160 
1161 	/*
1162 	 * Evaluate _PTC, _TSS and _TPC
1163 	 * They must all be present or none of them can be used.
1164 	 */
1165 	if (acpi_processor_get_throttling_control(pr) ||
1166 		acpi_processor_get_throttling_states(pr) ||
1167 		acpi_processor_get_platform_limit(pr)) {
1168 		pr->throttling.acpi_processor_get_throttling =
1169 		    &acpi_processor_get_throttling_fadt;
1170 		pr->throttling.acpi_processor_set_throttling =
1171 		    &acpi_processor_set_throttling_fadt;
1172 		if (acpi_processor_get_fadt_info(pr))
1173 			return 0;
1174 	} else {
1175 		pr->throttling.acpi_processor_get_throttling =
1176 		    &acpi_processor_get_throttling_ptc;
1177 		pr->throttling.acpi_processor_set_throttling =
1178 		    &acpi_processor_set_throttling_ptc;
1179 	}
1180 
1181 	/*
1182 	 * If TSD package for one CPU can't be parsed successfully, it means
1183 	 * that this CPU will have no coordination with other CPUs.
1184 	 */
1185 	if (acpi_processor_get_tsd(pr)) {
1186 		pthrottling = &pr->throttling;
1187 		pthrottling->tsd_valid_flag = 0;
1188 		cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1189 		pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1190 	}
1191 
1192 	/*
1193 	 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1194 	 * This shouldn't be an issue as few (if any) mobile systems ever
1195 	 * used this part.
1196 	 */
1197 	if (errata.piix4.throttle) {
1198 		acpi_handle_debug(pr->handle,
1199 				  "Throttling not supported on PIIX4 A- or B-step\n");
1200 		return 0;
1201 	}
1202 
1203 	acpi_handle_debug(pr->handle, "Found %d throttling states\n",
1204 			  pr->throttling.state_count);
1205 
1206 	pr->flags.throttling = 1;
1207 
1208 	/*
1209 	 * Disable throttling (if enabled).  We'll let subsequent policy (e.g.
1210 	 * thermal) decide to lower performance if it so chooses, but for now
1211 	 * we'll crank up the speed.
1212 	 */
1213 
1214 	result = acpi_processor_get_throttling(pr);
1215 	if (result)
1216 		goto end;
1217 
1218 	if (pr->throttling.state) {
1219 		acpi_handle_debug(pr->handle,
1220 				  "Disabling throttling (was T%d)\n",
1221 				  pr->throttling.state);
1222 		result = acpi_processor_set_throttling(pr, 0, false);
1223 		if (result)
1224 			goto end;
1225 	}
1226 
1227 end:
1228 	if (result)
1229 		pr->flags.throttling = 0;
1230 
1231 	return result;
1232 }
1233 
1234