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