2 * POWERNV cpufreq driver for the IBM POWER processors
4 * (C) Copyright IBM 2014
6 * Author: Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>
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)
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.
20 #define pr_fmt(fmt) "powernv-cpufreq: " fmt
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>
30 #include <asm/cputhreads.h>
32 #include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
34 #define POWERNV_MAX_PSTATES 256
36 static struct cpufreq_frequency_table powernv_freqs
[POWERNV_MAX_PSTATES
+1];
39 * Note: The set of pstates consists of contiguous integers, the
40 * smallest of which is indicated by powernv_pstate_info.min, the
41 * largest of which is indicated by powernv_pstate_info.max.
43 * The nominal pstate is the highest non-turbo pstate in this
44 * platform. This is indicated by powernv_pstate_info.nominal.
46 static struct powernv_pstate_info
{
51 } powernv_pstate_info
;
54 * Initialize the freq table based on data obtained
55 * from the firmware passed via device-tree
57 static int init_powernv_pstates(void)
59 struct device_node
*power_mgt
;
60 int i
, pstate_min
, pstate_max
, pstate_nominal
, nr_pstates
= 0;
61 const __be32
*pstate_ids
, *pstate_freqs
;
62 u32 len_ids
, len_freqs
;
64 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
66 pr_warn("power-mgt node not found\n");
70 if (of_property_read_u32(power_mgt
, "ibm,pstate-min", &pstate_min
)) {
71 pr_warn("ibm,pstate-min node not found\n");
75 if (of_property_read_u32(power_mgt
, "ibm,pstate-max", &pstate_max
)) {
76 pr_warn("ibm,pstate-max node not found\n");
80 if (of_property_read_u32(power_mgt
, "ibm,pstate-nominal",
82 pr_warn("ibm,pstate-nominal not found\n");
85 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min
,
86 pstate_nominal
, pstate_max
);
88 pstate_ids
= of_get_property(power_mgt
, "ibm,pstate-ids", &len_ids
);
90 pr_warn("ibm,pstate-ids not found\n");
94 pstate_freqs
= of_get_property(power_mgt
, "ibm,pstate-frequencies-mhz",
97 pr_warn("ibm,pstate-frequencies-mhz not found\n");
101 WARN_ON(len_ids
!= len_freqs
);
102 nr_pstates
= min(len_ids
, len_freqs
) / sizeof(u32
);
104 pr_warn("No PStates found\n");
108 pr_debug("NR PStates %d\n", nr_pstates
);
109 for (i
= 0; i
< nr_pstates
; i
++) {
110 u32 id
= be32_to_cpu(pstate_ids
[i
]);
111 u32 freq
= be32_to_cpu(pstate_freqs
[i
]);
113 pr_debug("PState id %d freq %d MHz\n", id
, freq
);
114 powernv_freqs
[i
].frequency
= freq
* 1000; /* kHz */
115 powernv_freqs
[i
].driver_data
= id
;
117 /* End of list marker entry */
118 powernv_freqs
[i
].frequency
= CPUFREQ_TABLE_END
;
120 powernv_pstate_info
.min
= pstate_min
;
121 powernv_pstate_info
.max
= pstate_max
;
122 powernv_pstate_info
.nominal
= pstate_nominal
;
123 powernv_pstate_info
.nr_pstates
= nr_pstates
;
128 /* Returns the CPU frequency corresponding to the pstate_id. */
129 static unsigned int pstate_id_to_freq(int pstate_id
)
133 i
= powernv_pstate_info
.max
- pstate_id
;
134 BUG_ON(i
>= powernv_pstate_info
.nr_pstates
|| i
< 0);
136 return powernv_freqs
[i
].frequency
;
140 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
143 static ssize_t
cpuinfo_nominal_freq_show(struct cpufreq_policy
*policy
,
146 return sprintf(buf
, "%u\n",
147 pstate_id_to_freq(powernv_pstate_info
.nominal
));
150 struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq
=
151 __ATTR_RO(cpuinfo_nominal_freq
);
153 static struct freq_attr
*powernv_cpu_freq_attr
[] = {
154 &cpufreq_freq_attr_scaling_available_freqs
,
155 &cpufreq_freq_attr_cpuinfo_nominal_freq
,
159 /* Helper routines */
161 /* Access helpers to power mgt SPR */
163 static inline unsigned long get_pmspr(unsigned long sprn
)
167 return mfspr(SPRN_PMCR
);
170 return mfspr(SPRN_PMICR
);
173 return mfspr(SPRN_PMSR
);
178 static inline void set_pmspr(unsigned long sprn
, unsigned long val
)
182 mtspr(SPRN_PMCR
, val
);
186 mtspr(SPRN_PMICR
, val
);
193 * Use objects of this type to query/update
194 * pstates on a remote CPU via smp_call_function.
196 struct powernv_smp_call_data
{
202 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
204 * Called via smp_call_function.
206 * Note: The caller of the smp_call_function should pass an argument of
207 * the type 'struct powernv_smp_call_data *' along with this function.
209 * The current frequency on this CPU will be returned via
210 * ((struct powernv_smp_call_data *)arg)->freq;
212 static void powernv_read_cpu_freq(void *arg
)
214 unsigned long pmspr_val
;
216 struct powernv_smp_call_data
*freq_data
= arg
;
218 pmspr_val
= get_pmspr(SPRN_PMSR
);
221 * The local pstate id corresponds bits 48..55 in the PMSR.
222 * Note: Watch out for the sign!
224 local_pstate_id
= (pmspr_val
>> 48) & 0xFF;
225 freq_data
->pstate_id
= local_pstate_id
;
226 freq_data
->freq
= pstate_id_to_freq(freq_data
->pstate_id
);
228 pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n",
229 raw_smp_processor_id(), pmspr_val
, freq_data
->pstate_id
,
234 * powernv_cpufreq_get: Returns the CPU frequency as reported by the
235 * firmware for CPU 'cpu'. This value is reported through the sysfs
236 * file cpuinfo_cur_freq.
238 static unsigned int powernv_cpufreq_get(unsigned int cpu
)
240 struct powernv_smp_call_data freq_data
;
242 smp_call_function_any(cpu_sibling_mask(cpu
), powernv_read_cpu_freq
,
245 return freq_data
.freq
;
249 * set_pstate: Sets the pstate on this CPU.
251 * This is called via an smp_call_function.
253 * The caller must ensure that freq_data is of the type
254 * (struct powernv_smp_call_data *) and the pstate_id which needs to be set
255 * on this CPU should be present in freq_data->pstate_id.
257 static void set_pstate(void *freq_data
)
260 unsigned long pstate_ul
=
261 ((struct powernv_smp_call_data
*) freq_data
)->pstate_id
;
263 val
= get_pmspr(SPRN_PMCR
);
264 val
= val
& 0x0000FFFFFFFFFFFFULL
;
266 pstate_ul
= pstate_ul
& 0xFF;
268 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
269 val
= val
| (pstate_ul
<< 56) | (pstate_ul
<< 48);
271 pr_debug("Setting cpu %d pmcr to %016lX\n",
272 raw_smp_processor_id(), val
);
273 set_pmspr(SPRN_PMCR
, val
);
277 * powernv_cpufreq_target_index: Sets the frequency corresponding to
278 * the cpufreq table entry indexed by new_index on the cpus in the
281 static int powernv_cpufreq_target_index(struct cpufreq_policy
*policy
,
282 unsigned int new_index
)
284 struct powernv_smp_call_data freq_data
;
286 freq_data
.pstate_id
= powernv_freqs
[new_index
].driver_data
;
289 * Use smp_call_function to send IPI and execute the
290 * mtspr on target CPU. We could do that without IPI
291 * if current CPU is within policy->cpus (core)
293 smp_call_function_any(policy
->cpus
, set_pstate
, &freq_data
, 1);
298 static int powernv_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
302 base
= cpu_first_thread_sibling(policy
->cpu
);
304 for (i
= 0; i
< threads_per_core
; i
++)
305 cpumask_set_cpu(base
+ i
, policy
->cpus
);
307 return cpufreq_table_validate_and_show(policy
, powernv_freqs
);
310 static struct cpufreq_driver powernv_cpufreq_driver
= {
311 .name
= "powernv-cpufreq",
312 .flags
= CPUFREQ_CONST_LOOPS
,
313 .init
= powernv_cpufreq_cpu_init
,
314 .verify
= cpufreq_generic_frequency_table_verify
,
315 .target_index
= powernv_cpufreq_target_index
,
316 .get
= powernv_cpufreq_get
,
317 .attr
= powernv_cpu_freq_attr
,
320 static int __init
powernv_cpufreq_init(void)
324 /* Discover pstates from device tree and init */
325 rc
= init_powernv_pstates();
327 pr_info("powernv-cpufreq disabled. System does not support PState control\n");
331 return cpufreq_register_driver(&powernv_cpufreq_driver
);
333 module_init(powernv_cpufreq_init
);
335 static void __exit
powernv_cpufreq_exit(void)
337 cpufreq_unregister_driver(&powernv_cpufreq_driver
);
339 module_exit(powernv_cpufreq_exit
);
341 MODULE_LICENSE("GPL");
342 MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");