xref: /linux/drivers/cpufreq/powernv-cpufreq.c (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1 /*
2  * POWERNV cpufreq driver for the IBM POWER processors
3  *
4  * (C) Copyright IBM 2014
5  *
6  * Author: Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19 
20 #define pr_fmt(fmt)	"powernv-cpufreq: " fmt
21 
22 #include <linux/kernel.h>
23 #include <linux/sysfs.h>
24 #include <linux/cpumask.h>
25 #include <linux/module.h>
26 #include <linux/cpufreq.h>
27 #include <linux/smp.h>
28 #include <linux/of.h>
29 
30 #include <asm/cputhreads.h>
31 #include <asm/firmware.h>
32 #include <asm/reg.h>
33 #include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
34 
35 #define POWERNV_MAX_PSTATES	256
36 
37 static struct cpufreq_frequency_table powernv_freqs[POWERNV_MAX_PSTATES+1];
38 
39 /*
40  * Note: The set of pstates consists of contiguous integers, the
41  * smallest of which is indicated by powernv_pstate_info.min, the
42  * largest of which is indicated by powernv_pstate_info.max.
43  *
44  * The nominal pstate is the highest non-turbo pstate in this
45  * platform. This is indicated by powernv_pstate_info.nominal.
46  */
47 static struct powernv_pstate_info {
48 	int min;
49 	int max;
50 	int nominal;
51 	int nr_pstates;
52 } powernv_pstate_info;
53 
54 /*
55  * Initialize the freq table based on data obtained
56  * from the firmware passed via device-tree
57  */
58 static int init_powernv_pstates(void)
59 {
60 	struct device_node *power_mgt;
61 	int i, pstate_min, pstate_max, pstate_nominal, nr_pstates = 0;
62 	const __be32 *pstate_ids, *pstate_freqs;
63 	u32 len_ids, len_freqs;
64 
65 	power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
66 	if (!power_mgt) {
67 		pr_warn("power-mgt node not found\n");
68 		return -ENODEV;
69 	}
70 
71 	if (of_property_read_u32(power_mgt, "ibm,pstate-min", &pstate_min)) {
72 		pr_warn("ibm,pstate-min node not found\n");
73 		return -ENODEV;
74 	}
75 
76 	if (of_property_read_u32(power_mgt, "ibm,pstate-max", &pstate_max)) {
77 		pr_warn("ibm,pstate-max node not found\n");
78 		return -ENODEV;
79 	}
80 
81 	if (of_property_read_u32(power_mgt, "ibm,pstate-nominal",
82 				 &pstate_nominal)) {
83 		pr_warn("ibm,pstate-nominal not found\n");
84 		return -ENODEV;
85 	}
86 	pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min,
87 		pstate_nominal, pstate_max);
88 
89 	pstate_ids = of_get_property(power_mgt, "ibm,pstate-ids", &len_ids);
90 	if (!pstate_ids) {
91 		pr_warn("ibm,pstate-ids not found\n");
92 		return -ENODEV;
93 	}
94 
95 	pstate_freqs = of_get_property(power_mgt, "ibm,pstate-frequencies-mhz",
96 				      &len_freqs);
97 	if (!pstate_freqs) {
98 		pr_warn("ibm,pstate-frequencies-mhz not found\n");
99 		return -ENODEV;
100 	}
101 
102 	if (len_ids != len_freqs) {
103 		pr_warn("Entries in ibm,pstate-ids and "
104 			"ibm,pstate-frequencies-mhz does not match\n");
105 	}
106 
107 	nr_pstates = min(len_ids, len_freqs) / sizeof(u32);
108 	if (!nr_pstates) {
109 		pr_warn("No PStates found\n");
110 		return -ENODEV;
111 	}
112 
113 	pr_debug("NR PStates %d\n", nr_pstates);
114 	for (i = 0; i < nr_pstates; i++) {
115 		u32 id = be32_to_cpu(pstate_ids[i]);
116 		u32 freq = be32_to_cpu(pstate_freqs[i]);
117 
118 		pr_debug("PState id %d freq %d MHz\n", id, freq);
119 		powernv_freqs[i].frequency = freq * 1000; /* kHz */
120 		powernv_freqs[i].driver_data = id;
121 	}
122 	/* End of list marker entry */
123 	powernv_freqs[i].frequency = CPUFREQ_TABLE_END;
124 
125 	powernv_pstate_info.min = pstate_min;
126 	powernv_pstate_info.max = pstate_max;
127 	powernv_pstate_info.nominal = pstate_nominal;
128 	powernv_pstate_info.nr_pstates = nr_pstates;
129 
130 	return 0;
131 }
132 
133 /* Returns the CPU frequency corresponding to the pstate_id. */
134 static unsigned int pstate_id_to_freq(int pstate_id)
135 {
136 	int i;
137 
138 	i = powernv_pstate_info.max - pstate_id;
139 	if (i >= powernv_pstate_info.nr_pstates || i < 0) {
140 		pr_warn("PState id %d outside of PState table, "
141 			"reporting nominal id %d instead\n",
142 			pstate_id, powernv_pstate_info.nominal);
143 		i = powernv_pstate_info.max - powernv_pstate_info.nominal;
144 	}
145 
146 	return powernv_freqs[i].frequency;
147 }
148 
149 /*
150  * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
151  * the firmware
152  */
153 static ssize_t cpuinfo_nominal_freq_show(struct cpufreq_policy *policy,
154 					char *buf)
155 {
156 	return sprintf(buf, "%u\n",
157 		pstate_id_to_freq(powernv_pstate_info.nominal));
158 }
159 
160 struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq =
161 	__ATTR_RO(cpuinfo_nominal_freq);
162 
163 static struct freq_attr *powernv_cpu_freq_attr[] = {
164 	&cpufreq_freq_attr_scaling_available_freqs,
165 	&cpufreq_freq_attr_cpuinfo_nominal_freq,
166 	NULL,
167 };
168 
169 /* Helper routines */
170 
171 /* Access helpers to power mgt SPR */
172 
173 static inline unsigned long get_pmspr(unsigned long sprn)
174 {
175 	switch (sprn) {
176 	case SPRN_PMCR:
177 		return mfspr(SPRN_PMCR);
178 
179 	case SPRN_PMICR:
180 		return mfspr(SPRN_PMICR);
181 
182 	case SPRN_PMSR:
183 		return mfspr(SPRN_PMSR);
184 	}
185 	BUG();
186 }
187 
188 static inline void set_pmspr(unsigned long sprn, unsigned long val)
189 {
190 	switch (sprn) {
191 	case SPRN_PMCR:
192 		mtspr(SPRN_PMCR, val);
193 		return;
194 
195 	case SPRN_PMICR:
196 		mtspr(SPRN_PMICR, val);
197 		return;
198 	}
199 	BUG();
200 }
201 
202 /*
203  * Use objects of this type to query/update
204  * pstates on a remote CPU via smp_call_function.
205  */
206 struct powernv_smp_call_data {
207 	unsigned int freq;
208 	int pstate_id;
209 };
210 
211 /*
212  * powernv_read_cpu_freq: Reads the current frequency on this CPU.
213  *
214  * Called via smp_call_function.
215  *
216  * Note: The caller of the smp_call_function should pass an argument of
217  * the type 'struct powernv_smp_call_data *' along with this function.
218  *
219  * The current frequency on this CPU will be returned via
220  * ((struct powernv_smp_call_data *)arg)->freq;
221  */
222 static void powernv_read_cpu_freq(void *arg)
223 {
224 	unsigned long pmspr_val;
225 	s8 local_pstate_id;
226 	struct powernv_smp_call_data *freq_data = arg;
227 
228 	pmspr_val = get_pmspr(SPRN_PMSR);
229 
230 	/*
231 	 * The local pstate id corresponds bits 48..55 in the PMSR.
232 	 * Note: Watch out for the sign!
233 	 */
234 	local_pstate_id = (pmspr_val >> 48) & 0xFF;
235 	freq_data->pstate_id = local_pstate_id;
236 	freq_data->freq = pstate_id_to_freq(freq_data->pstate_id);
237 
238 	pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n",
239 		raw_smp_processor_id(), pmspr_val, freq_data->pstate_id,
240 		freq_data->freq);
241 }
242 
243 /*
244  * powernv_cpufreq_get: Returns the CPU frequency as reported by the
245  * firmware for CPU 'cpu'. This value is reported through the sysfs
246  * file cpuinfo_cur_freq.
247  */
248 static unsigned int powernv_cpufreq_get(unsigned int cpu)
249 {
250 	struct powernv_smp_call_data freq_data;
251 
252 	smp_call_function_any(cpu_sibling_mask(cpu), powernv_read_cpu_freq,
253 			&freq_data, 1);
254 
255 	return freq_data.freq;
256 }
257 
258 /*
259  * set_pstate: Sets the pstate on this CPU.
260  *
261  * This is called via an smp_call_function.
262  *
263  * The caller must ensure that freq_data is of the type
264  * (struct powernv_smp_call_data *) and the pstate_id which needs to be set
265  * on this CPU should be present in freq_data->pstate_id.
266  */
267 static void set_pstate(void *freq_data)
268 {
269 	unsigned long val;
270 	unsigned long pstate_ul =
271 		((struct powernv_smp_call_data *) freq_data)->pstate_id;
272 
273 	val = get_pmspr(SPRN_PMCR);
274 	val = val & 0x0000FFFFFFFFFFFFULL;
275 
276 	pstate_ul = pstate_ul & 0xFF;
277 
278 	/* Set both global(bits 56..63) and local(bits 48..55) PStates */
279 	val = val | (pstate_ul << 56) | (pstate_ul << 48);
280 
281 	pr_debug("Setting cpu %d pmcr to %016lX\n",
282 			raw_smp_processor_id(), val);
283 	set_pmspr(SPRN_PMCR, val);
284 }
285 
286 /*
287  * powernv_cpufreq_target_index: Sets the frequency corresponding to
288  * the cpufreq table entry indexed by new_index on the cpus in the
289  * mask policy->cpus
290  */
291 static int powernv_cpufreq_target_index(struct cpufreq_policy *policy,
292 					unsigned int new_index)
293 {
294 	struct powernv_smp_call_data freq_data;
295 
296 	freq_data.pstate_id = powernv_freqs[new_index].driver_data;
297 
298 	/*
299 	 * Use smp_call_function to send IPI and execute the
300 	 * mtspr on target CPU.  We could do that without IPI
301 	 * if current CPU is within policy->cpus (core)
302 	 */
303 	smp_call_function_any(policy->cpus, set_pstate, &freq_data, 1);
304 
305 	return 0;
306 }
307 
308 static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy)
309 {
310 	int base, i;
311 
312 	base = cpu_first_thread_sibling(policy->cpu);
313 
314 	for (i = 0; i < threads_per_core; i++)
315 		cpumask_set_cpu(base + i, policy->cpus);
316 
317 	return cpufreq_table_validate_and_show(policy, powernv_freqs);
318 }
319 
320 static struct cpufreq_driver powernv_cpufreq_driver = {
321 	.name		= "powernv-cpufreq",
322 	.flags		= CPUFREQ_CONST_LOOPS,
323 	.init		= powernv_cpufreq_cpu_init,
324 	.verify		= cpufreq_generic_frequency_table_verify,
325 	.target_index	= powernv_cpufreq_target_index,
326 	.get		= powernv_cpufreq_get,
327 	.attr		= powernv_cpu_freq_attr,
328 };
329 
330 static int __init powernv_cpufreq_init(void)
331 {
332 	int rc = 0;
333 
334 	/* Don't probe on pseries (guest) platforms */
335 	if (!firmware_has_feature(FW_FEATURE_OPALv3))
336 		return -ENODEV;
337 
338 	/* Discover pstates from device tree and init */
339 	rc = init_powernv_pstates();
340 	if (rc) {
341 		pr_info("powernv-cpufreq disabled. System does not support PState control\n");
342 		return rc;
343 	}
344 
345 	return cpufreq_register_driver(&powernv_cpufreq_driver);
346 }
347 module_init(powernv_cpufreq_init);
348 
349 static void __exit powernv_cpufreq_exit(void)
350 {
351 	cpufreq_unregister_driver(&powernv_cpufreq_driver);
352 }
353 module_exit(powernv_cpufreq_exit);
354 
355 MODULE_LICENSE("GPL");
356 MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");
357