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>
31 #include <asm/firmware.h>
33 #include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
35 #define POWERNV_MAX_PSTATES 256
37 static struct cpufreq_frequency_table powernv_freqs
[POWERNV_MAX_PSTATES
+1];
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.
44 * The nominal pstate is the highest non-turbo pstate in this
45 * platform. This is indicated by powernv_pstate_info.nominal.
47 static struct powernv_pstate_info
{
52 } powernv_pstate_info
;
55 * Initialize the freq table based on data obtained
56 * from the firmware passed via device-tree
58 static int init_powernv_pstates(void)
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
;
65 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
67 pr_warn("power-mgt node not found\n");
71 if (of_property_read_u32(power_mgt
, "ibm,pstate-min", &pstate_min
)) {
72 pr_warn("ibm,pstate-min node not found\n");
76 if (of_property_read_u32(power_mgt
, "ibm,pstate-max", &pstate_max
)) {
77 pr_warn("ibm,pstate-max node not found\n");
81 if (of_property_read_u32(power_mgt
, "ibm,pstate-nominal",
83 pr_warn("ibm,pstate-nominal not found\n");
86 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min
,
87 pstate_nominal
, pstate_max
);
89 pstate_ids
= of_get_property(power_mgt
, "ibm,pstate-ids", &len_ids
);
91 pr_warn("ibm,pstate-ids not found\n");
95 pstate_freqs
= of_get_property(power_mgt
, "ibm,pstate-frequencies-mhz",
98 pr_warn("ibm,pstate-frequencies-mhz not found\n");
102 if (len_ids
!= len_freqs
) {
103 pr_warn("Entries in ibm,pstate-ids and "
104 "ibm,pstate-frequencies-mhz does not match\n");
107 nr_pstates
= min(len_ids
, len_freqs
) / sizeof(u32
);
109 pr_warn("No PStates found\n");
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
]);
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
;
122 /* End of list marker entry */
123 powernv_freqs
[i
].frequency
= CPUFREQ_TABLE_END
;
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
;
133 /* Returns the CPU frequency corresponding to the pstate_id. */
134 static unsigned int pstate_id_to_freq(int pstate_id
)
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
;
146 return powernv_freqs
[i
].frequency
;
150 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
153 static ssize_t
cpuinfo_nominal_freq_show(struct cpufreq_policy
*policy
,
156 return sprintf(buf
, "%u\n",
157 pstate_id_to_freq(powernv_pstate_info
.nominal
));
160 struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq
=
161 __ATTR_RO(cpuinfo_nominal_freq
);
163 static struct freq_attr
*powernv_cpu_freq_attr
[] = {
164 &cpufreq_freq_attr_scaling_available_freqs
,
165 &cpufreq_freq_attr_cpuinfo_nominal_freq
,
169 /* Helper routines */
171 /* Access helpers to power mgt SPR */
173 static inline unsigned long get_pmspr(unsigned long sprn
)
177 return mfspr(SPRN_PMCR
);
180 return mfspr(SPRN_PMICR
);
183 return mfspr(SPRN_PMSR
);
188 static inline void set_pmspr(unsigned long sprn
, unsigned long val
)
192 mtspr(SPRN_PMCR
, val
);
196 mtspr(SPRN_PMICR
, val
);
203 * Use objects of this type to query/update
204 * pstates on a remote CPU via smp_call_function.
206 struct powernv_smp_call_data
{
212 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
214 * Called via smp_call_function.
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.
219 * The current frequency on this CPU will be returned via
220 * ((struct powernv_smp_call_data *)arg)->freq;
222 static void powernv_read_cpu_freq(void *arg
)
224 unsigned long pmspr_val
;
226 struct powernv_smp_call_data
*freq_data
= arg
;
228 pmspr_val
= get_pmspr(SPRN_PMSR
);
231 * The local pstate id corresponds bits 48..55 in the PMSR.
232 * Note: Watch out for the sign!
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
);
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
,
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.
248 static unsigned int powernv_cpufreq_get(unsigned int cpu
)
250 struct powernv_smp_call_data freq_data
;
252 smp_call_function_any(cpu_sibling_mask(cpu
), powernv_read_cpu_freq
,
255 return freq_data
.freq
;
259 * set_pstate: Sets the pstate on this CPU.
261 * This is called via an smp_call_function.
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.
267 static void set_pstate(void *freq_data
)
270 unsigned long pstate_ul
=
271 ((struct powernv_smp_call_data
*) freq_data
)->pstate_id
;
273 val
= get_pmspr(SPRN_PMCR
);
274 val
= val
& 0x0000FFFFFFFFFFFFULL
;
276 pstate_ul
= pstate_ul
& 0xFF;
278 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
279 val
= val
| (pstate_ul
<< 56) | (pstate_ul
<< 48);
281 pr_debug("Setting cpu %d pmcr to %016lX\n",
282 raw_smp_processor_id(), val
);
283 set_pmspr(SPRN_PMCR
, val
);
287 * powernv_cpufreq_target_index: Sets the frequency corresponding to
288 * the cpufreq table entry indexed by new_index on the cpus in the
291 static int powernv_cpufreq_target_index(struct cpufreq_policy
*policy
,
292 unsigned int new_index
)
294 struct powernv_smp_call_data freq_data
;
296 freq_data
.pstate_id
= powernv_freqs
[new_index
].driver_data
;
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)
303 smp_call_function_any(policy
->cpus
, set_pstate
, &freq_data
, 1);
308 static int powernv_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
312 base
= cpu_first_thread_sibling(policy
->cpu
);
314 for (i
= 0; i
< threads_per_core
; i
++)
315 cpumask_set_cpu(base
+ i
, policy
->cpus
);
317 return cpufreq_table_validate_and_show(policy
, powernv_freqs
);
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
,
330 static int __init
powernv_cpufreq_init(void)
334 /* Don't probe on pseries (guest) platforms */
335 if (!firmware_has_feature(FW_FEATURE_OPALv3
))
338 /* Discover pstates from device tree and init */
339 rc
= init_powernv_pstates();
341 pr_info("powernv-cpufreq disabled. System does not support PState control\n");
345 return cpufreq_register_driver(&powernv_cpufreq_driver
);
347 module_init(powernv_cpufreq_init
);
349 static void __exit
powernv_cpufreq_exit(void)
351 cpufreq_unregister_driver(&powernv_cpufreq_driver
);
353 module_exit(powernv_cpufreq_exit
);
355 MODULE_LICENSE("GPL");
356 MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");