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