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 #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>
22 
23 #define PREFIX "ACPI: "
24 
25 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
26 ACPI_MODULE_NAME("processor_throttling");
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, 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 		count = 1;
114 
115 		for_each_possible_cpu(j) {
116 			if (i == j)
117 				continue;
118 
119 			match_pr = per_cpu(processors, j);
120 			if (!match_pr)
121 				continue;
122 
123 			match_pthrottling = &(match_pr->throttling);
124 			match_pdomain = &(match_pthrottling->domain_info);
125 			if (match_pdomain->domain != pdomain->domain)
126 				continue;
127 
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.
133 			 */
134 			if (match_pdomain->num_processors != count_target) {
135 				retval = -EINVAL;
136 				goto err_ret;
137 			}
138 
139 			if (pdomain->coord_type != match_pdomain->coord_type) {
140 				retval = -EINVAL;
141 				goto err_ret;
142 			}
143 
144 			cpumask_set_cpu(j, covered_cpus);
145 			cpumask_set_cpu(j, pthrottling->shared_cpu_map);
146 			count++;
147 		}
148 		for_each_possible_cpu(j) {
149 			if (i == j)
150 				continue;
151 
152 			match_pr = per_cpu(processors, j);
153 			if (!match_pr)
154 				continue;
155 
156 			match_pthrottling = &(match_pr->throttling);
157 			match_pdomain = &(match_pthrottling->domain_info);
158 			if (match_pdomain->domain != pdomain->domain)
159 				continue;
160 
161 			/*
162 			 * If some CPUS have the same domain, they
163 			 * will have the same shared_cpu_map.
164 			 */
165 			cpumask_copy(match_pthrottling->shared_cpu_map,
166 				     pthrottling->shared_cpu_map);
167 		}
168 	}
169 
170 err_ret:
171 	free_cpumask_var(covered_cpus);
172 
173 	for_each_possible_cpu(i) {
174 		pr = per_cpu(processors, i);
175 		if (!pr)
176 			continue;
177 
178 		/*
179 		 * Assume no coordination on any error parsing domain info.
180 		 * The coordination type will be forced as SW_ALL.
181 		 */
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;
187 		}
188 	}
189 
190 	return retval;
191 }
192 
193 /*
194  * Update the T-state coordination after the _TSD
195  * data for all cpus is obtained.
196  */
197 void acpi_processor_throttling_init(void)
198 {
199 	if (acpi_processor_update_tsd_coord()) {
200 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
201 			"Assume no T-state coordination\n"));
202 	}
203 
204 	return;
205 }
206 
207 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
208 {
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;
215 
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;
221 	}
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;
226 	}
227 	target_state = p_tstate->target_state;
228 	p_throttling = &(pr->throttling);
229 	switch (event) {
230 	case THROTTLING_PRECHANGE:
231 		/*
232 		 * Prechange event is used to choose one proper t-state,
233 		 * which meets the limits of thermal, user and _TPC.
234 		 */
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;
246 		}
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:
253 		/*
254 		 * Postchange event is only used to update the
255 		 * T-state flag of acpi_processor_throttling.
256 		 */
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;
266 	}
267 
268 	return 0;
269 }
270 
271 /*
272  * _TPC - Throttling Present Capabilities
273  */
274 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
275 {
276 	acpi_status status = 0;
277 	unsigned long long tpc = 0;
278 
279 	if (!pr)
280 		return -EINVAL;
281 
282 	if (ignore_tpc)
283 		goto end;
284 
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"));
289 		}
290 		return -ENODEV;
291 	}
292 
293 end:
294 	pr->throttling_platform_limit = (int)tpc;
295 	return 0;
296 }
297 
298 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
299 {
300 	int result = 0;
301 	int throttling_limit;
302 	int current_state;
303 	struct acpi_processor_limit *limit;
304 	int target_state;
305 
306 	if (ignore_tpc)
307 		return 0;
308 
309 	result = acpi_processor_get_platform_limit(pr);
310 	if (result) {
311 		/* Throttling Limit is unsupported */
312 		return result;
313 	}
314 
315 	throttling_limit = pr->throttling_platform_limit;
316 	if (throttling_limit >= pr->throttling.state_count) {
317 		/* Uncorrect Throttling Limit */
318 		return -EINVAL;
319 	}
320 
321 	current_state = pr->throttling.state;
322 	if (current_state > throttling_limit) {
323 		/*
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.
329 		 */
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) {
337 		/*
338 		 * Unnecessary to change the throttling state
339 		 */
340 		return 0;
341 	} else {
342 		/*
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.
348 		 */
349 		target_state = throttling_limit;
350 	}
351 	return acpi_processor_set_throttling(pr, target_state, false);
352 }
353 
354 /*
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
362  */
363 void acpi_processor_reevaluate_tstate(struct acpi_processor *pr,
364 					bool is_dead)
365 {
366 	int result = 0;
367 
368 	if (is_dead) {
369 		/* When one CPU is offline, the T-state throttling
370 		 * will be invalidated.
371 		 */
372 		pr->flags.throttling = 0;
373 		return;
374 	}
375 	/* the following is to recheck whether the T-state is valid for
376 	 * the online CPU
377 	 */
378 	if (!pr->throttling.state_count) {
379 		/* If the number of T-state is invalid, it is
380 		 * invalidated.
381 		 */
382 		pr->flags.throttling = 0;
383 		return;
384 	}
385 	pr->flags.throttling = 1;
386 
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.
390 	 */
391 
392 	result = acpi_processor_get_throttling(pr);
393 	if (result)
394 		goto end;
395 
396 	if (pr->throttling.state) {
397 		result = acpi_processor_set_throttling(pr, 0, false);
398 		if (result)
399 			goto end;
400 	}
401 
402 end:
403 	if (result)
404 		pr->flags.throttling = 0;
405 }
406 /*
407  * _PTC - Processor Throttling Control (and status) register location
408  */
409 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
410 {
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;
417 
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"));
422 		}
423 		return -ENODEV;
424 	}
425 
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;
432 	}
433 
434 	/*
435 	 * control_register
436 	 */
437 
438 	obj = ptc->package.elements[0];
439 
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;
447 	}
448 	memcpy(&pr->throttling.control_register, obj.buffer.pointer,
449 	       sizeof(struct acpi_ptc_register));
450 
451 	/*
452 	 * status_register
453 	 */
454 
455 	obj = ptc->package.elements[1];
456 
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;
463 	}
464 
465 	memcpy(&pr->throttling.status_register, obj.buffer.pointer,
466 	       sizeof(struct acpi_ptc_register));
467 
468 	throttling = &pr->throttling;
469 
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;
475 	}
476 
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;
482 	}
483 
484       end:
485 	kfree(buffer.pointer);
486 
487 	return result;
488 }
489 
490 /*
491  * _TSS - Throttling Supported States
492  */
493 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
494 {
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;
502 
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"));
507 		}
508 		return -ENODEV;
509 	}
510 
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;
516 	}
517 
518 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
519 			  tss->package.count));
520 
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;
529 	}
530 
531 	for (i = 0; i < pr->throttling.state_count; i++) {
532 
533 		struct acpi_processor_tx_tss *tx =
534 		    (struct acpi_processor_tx_tss *)&(pr->throttling.
535 						      states_tss[i]);
536 
537 		state.length = sizeof(struct acpi_processor_tx_tss);
538 		state.pointer = tx;
539 
540 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
541 
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;
549 		}
550 
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;
557 		}
558 	}
559 
560       end:
561 	kfree(buffer.pointer);
562 
563 	return result;
564 }
565 
566 /*
567  * _TSD - T-State Dependencies
568  */
569 static int acpi_processor_get_tsd(struct acpi_processor *pr)
570 {
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;
579 
580 	pthrottling = &pr->throttling;
581 	pthrottling->tsd_valid_flag = 0;
582 
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"));
587 		}
588 		return -ENODEV;
589 	}
590 
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;
596 	}
597 
598 	if (tsd->package.count != 1) {
599 		printk(KERN_ERR PREFIX "Invalid _TSD data\n");
600 		result = -EFAULT;
601 		goto end;
602 	}
603 
604 	pdomain = &(pr->throttling.domain_info);
605 
606 	state.length = sizeof(struct acpi_tsd_package);
607 	state.pointer = pdomain;
608 
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;
615 	}
616 
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;
621 	}
622 
623 	if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
624 		printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");
625 		result = -EFAULT;
626 		goto end;
627 	}
628 
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);
633 	/*
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.
637 	 */
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;
643 	}
644 
645       end:
646 	kfree(buffer.pointer);
647 	return result;
648 }
649 
650 /* --------------------------------------------------------------------------
651                               Throttling Control
652    -------------------------------------------------------------------------- */
653 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
654 {
655 	int state = 0;
656 	u32 value = 0;
657 	u32 duty_mask = 0;
658 	u32 duty_value = 0;
659 
660 	if (!pr)
661 		return -EINVAL;
662 
663 	if (!pr->flags.throttling)
664 		return -ENODEV;
665 
666 	/*
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..
670 	 *
671 	 * (In particular, allocating the IO range for Cardbus)
672 	 */
673 	request_region(pr->throttling.address, 6, "ACPI CPU throttle");
674 
675 	pr->throttling.state = 0;
676 
677 	duty_mask = pr->throttling.state_count - 1;
678 
679 	duty_mask <<= pr->throttling.duty_offset;
680 
681 	local_irq_disable();
682 
683 	value = inl(pr->throttling.address);
684 
685 	/*
686 	 * Compute the current throttling state when throttling is enabled
687 	 * (bit 4 is on).
688 	 */
689 	if (value & 0x10) {
690 		duty_value = value & duty_mask;
691 		duty_value >>= pr->throttling.duty_offset;
692 
693 		if (duty_value)
694 			state = pr->throttling.state_count - duty_value;
695 	}
696 
697 	pr->throttling.state = state;
698 
699 	local_irq_enable();
700 
701 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
702 			  "Throttling state is T%d (%d%% throttling applied)\n",
703 			  state, pr->throttling.states[state].performance));
704 
705 	return 0;
706 }
707 
708 #ifdef CONFIG_X86
709 static int acpi_throttling_rdmsr(u64 *value)
710 {
711 	u64 msr_high, msr_low;
712 	u64 msr = 0;
713 	int ret = -1;
714 
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;
727 	}
728 	return ret;
729 }
730 
731 static int acpi_throttling_wrmsr(u64 value)
732 {
733 	int ret = -1;
734 	u64 msr;
735 
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;
745 	}
746 	return ret;
747 }
748 #else
749 static int acpi_throttling_rdmsr(u64 *value)
750 {
751 	printk(KERN_ERR PREFIX
752 		"HARDWARE addr space,NOT supported yet\n");
753 	return -1;
754 }
755 
756 static int acpi_throttling_wrmsr(u64 value)
757 {
758 	printk(KERN_ERR PREFIX
759 		"HARDWARE addr space,NOT supported yet\n");
760 	return -1;
761 }
762 #endif
763 
764 static int acpi_read_throttling_status(struct acpi_processor *pr,
765 					u64 *value)
766 {
767 	u32 bit_width, bit_offset;
768 	u32 ptc_value;
769 	u64 ptc_mask;
770 	struct acpi_processor_throttling *throttling;
771 	int ret = -1;
772 
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;
778 
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));
792 	}
793 	return ret;
794 }
795 
796 static int acpi_write_throttling_state(struct acpi_processor *pr,
797 				u64 value)
798 {
799 	u32 bit_width, bit_offset;
800 	u64 ptc_value;
801 	u64 ptc_mask;
802 	struct acpi_processor_throttling *throttling;
803 	int ret = -1;
804 
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;
812 
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));
825 	}
826 	return ret;
827 }
828 
829 static int acpi_get_throttling_state(struct acpi_processor *pr,
830 				u64 value)
831 {
832 	int i;
833 
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;
840 	}
841 	return -1;
842 }
843 
844 static int acpi_get_throttling_value(struct acpi_processor *pr,
845 			int state, u64 *value)
846 {
847 	int ret = -1;
848 
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;
855 	}
856 	return ret;
857 }
858 
859 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
860 {
861 	int state = 0;
862 	int ret;
863 	u64 value;
864 
865 	if (!pr)
866 		return -EINVAL;
867 
868 	if (!pr->flags.throttling)
869 		return -ENODEV;
870 
871 	pr->throttling.state = 0;
872 
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;
885 		}
886 		pr->throttling.state = state;
887 	}
888 
889 	return 0;
890 }
891 
892 static long __acpi_processor_get_throttling(void *data)
893 {
894 	struct acpi_processor *pr = data;
895 
896 	return pr->throttling.acpi_processor_get_throttling(pr);
897 }
898 
899 static int acpi_processor_get_throttling(struct acpi_processor *pr)
900 {
901 	if (!pr)
902 		return -EINVAL;
903 
904 	if (!pr->flags.throttling)
905 		return -ENODEV;
906 
907 	/*
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.
912 	 */
913 	if (!cpu_online(pr->id))
914 		return -ENODEV;
915 
916 	return call_on_cpu(pr->id, __acpi_processor_get_throttling, pr, false);
917 }
918 
919 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
920 {
921 	int i, step;
922 
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;
929 	}
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;
934 	}
935 
936 	pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
937 
938 	/*
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.
942 	 */
943 
944 	step = (1000 / pr->throttling.state_count);
945 
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;
949 	}
950 	return 0;
951 }
952 
953 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
954 					      int state, bool force)
955 {
956 	u32 value = 0;
957 	u32 duty_mask = 0;
958 	u32 duty_value = 0;
959 
960 	if (!pr)
961 		return -EINVAL;
962 
963 	if ((state < 0) || (state > (pr->throttling.state_count - 1)))
964 		return -EINVAL;
965 
966 	if (!pr->flags.throttling)
967 		return -ENODEV;
968 
969 	if (!force && (state == pr->throttling.state))
970 		return 0;
971 
972 	if (state < pr->throttling_platform_limit)
973 		return -EPERM;
974 	/*
975 	 * Calculate the duty_value and duty_mask.
976 	 */
977 	if (state) {
978 		duty_value = pr->throttling.state_count - state;
979 
980 		duty_value <<= pr->throttling.duty_offset;
981 
982 		/* Used to clear all duty_value bits */
983 		duty_mask = pr->throttling.state_count - 1;
984 
985 		duty_mask <<= acpi_gbl_FADT.duty_offset;
986 		duty_mask = ~duty_mask;
987 	}
988 
989 	local_irq_disable();
990 
991 	/*
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.
994 	 */
995 	value = inl(pr->throttling.address);
996 	if (value & 0x10) {
997 		value &= 0xFFFFFFEF;
998 		outl(value, pr->throttling.address);
999 	}
1000 
1001 	/*
1002 	 * Write the new duty_value and then enable throttling.  Note
1003 	 * that a state value of 0 leaves throttling disabled.
1004 	 */
1005 	if (state) {
1006 		value &= duty_mask;
1007 		value |= duty_value;
1008 		outl(value, pr->throttling.address);
1009 
1010 		value |= 0x00000010;
1011 		outl(value, pr->throttling.address);
1012 	}
1013 
1014 	pr->throttling.state = state;
1015 
1016 	local_irq_enable();
1017 
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)));
1022 
1023 	return 0;
1024 }
1025 
1026 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
1027 					     int state, bool force)
1028 {
1029 	int ret;
1030 	u64 value;
1031 
1032 	if (!pr)
1033 		return -EINVAL;
1034 
1035 	if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1036 		return -EINVAL;
1037 
1038 	if (!pr->flags.throttling)
1039 		return -ENODEV;
1040 
1041 	if (!force && (state == pr->throttling.state))
1042 		return 0;
1043 
1044 	if (state < pr->throttling_platform_limit)
1045 		return -EPERM;
1046 
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;
1052 	}
1053 
1054 	return 0;
1055 }
1056 
1057 static long acpi_processor_throttling_fn(void *data)
1058 {
1059 	struct acpi_processor_throttling_arg *arg = data;
1060 	struct acpi_processor *pr = arg->pr;
1061 
1062 	return pr->throttling.acpi_processor_set_throttling(pr,
1063 			arg->target_state, arg->force);
1064 }
1065 
1066 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
1067 					   int state, bool force, bool direct)
1068 {
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;
1075 
1076 	if (!pr)
1077 		return -EINVAL;
1078 
1079 	if (!pr->flags.throttling)
1080 		return -ENODEV;
1081 
1082 	if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1083 		return -EINVAL;
1084 
1085 	if (cpu_is_offline(pr->id)) {
1086 		/*
1087 		 * the cpu pointed by pr->id is offline. Unnecessary to change
1088 		 * the throttling state any more.
1089 		 */
1090 		return -ENODEV;
1091 	}
1092 
1093 	t_state.target_state = state;
1094 	p_throttling = &(pr->throttling);
1095 
1096 	/*
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.
1100 	 */
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);
1105 	}
1106 	/*
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.
1111 	 */
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 {
1119 		/*
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.
1123 		 */
1124 		for_each_cpu_and(i, cpu_online_mask,
1125 		    p_throttling->shared_cpu_map) {
1126 			match_pr = per_cpu(processors, i);
1127 			/*
1128 			 * If the pointer is invalid, we will report the
1129 			 * error message and continue.
1130 			 */
1131 			if (!match_pr) {
1132 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1133 					"Invalid Pointer for CPU %d\n", i));
1134 				continue;
1135 			}
1136 			/*
1137 			 * If the throttling control is unsupported on CPU i,
1138 			 * we will report the error message and continue.
1139 			 */
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;
1145 			}
1146 
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);
1152 		}
1153 	}
1154 	/*
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
1159 	 */
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);
1164 	}
1165 
1166 	return ret;
1167 }
1168 
1169 int acpi_processor_set_throttling(struct acpi_processor *pr, int state,
1170 				  bool force)
1171 {
1172 	return __acpi_processor_set_throttling(pr, state, force, false);
1173 }
1174 
1175 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1176 {
1177 	int result = 0;
1178 	struct acpi_processor_throttling *pthrottling;
1179 
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));
1185 
1186 	/*
1187 	 * Evaluate _PTC, _TSS and _TPC
1188 	 * They must all be present or none of them can be used.
1189 	 */
1190 	if (acpi_processor_get_throttling_control(pr) ||
1191 		acpi_processor_get_throttling_states(pr) ||
1192 		acpi_processor_get_platform_limit(pr))
1193 	{
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;
1205 	}
1206 
1207 	/*
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.
1210 	 */
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;
1216 	}
1217 
1218 	/*
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.
1222 	 */
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;
1227 	}
1228 
1229 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
1230 			  pr->throttling.state_count));
1231 
1232 	pr->flags.throttling = 1;
1233 
1234 	/*
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.
1238 	 */
1239 
1240 	result = acpi_processor_get_throttling(pr);
1241 	if (result)
1242 		goto end;
1243 
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;
1251 	}
1252 
1253       end:
1254 	if (result)
1255 		pr->flags.throttling = 0;
1256 
1257 	return result;
1258 }
1259 
1260