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>
29 #include <linux/reboot.h>
31 #include <asm/cputhreads.h>
32 #include <asm/firmware.h>
34 #include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
36 #define POWERNV_MAX_PSTATES 256
38 static struct cpufreq_frequency_table powernv_freqs
[POWERNV_MAX_PSTATES
+1];
39 static bool rebooting
;
42 * Note: The set of pstates consists of contiguous integers, the
43 * smallest of which is indicated by powernv_pstate_info.min, the
44 * largest of which is indicated by powernv_pstate_info.max.
46 * The nominal pstate is the highest non-turbo pstate in this
47 * platform. This is indicated by powernv_pstate_info.nominal.
49 static struct powernv_pstate_info
{
54 } powernv_pstate_info
;
57 * Initialize the freq table based on data obtained
58 * from the firmware passed via device-tree
60 static int init_powernv_pstates(void)
62 struct device_node
*power_mgt
;
63 int i
, pstate_min
, pstate_max
, pstate_nominal
, nr_pstates
= 0;
64 const __be32
*pstate_ids
, *pstate_freqs
;
65 u32 len_ids
, len_freqs
;
67 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
69 pr_warn("power-mgt node not found\n");
73 if (of_property_read_u32(power_mgt
, "ibm,pstate-min", &pstate_min
)) {
74 pr_warn("ibm,pstate-min node not found\n");
78 if (of_property_read_u32(power_mgt
, "ibm,pstate-max", &pstate_max
)) {
79 pr_warn("ibm,pstate-max node not found\n");
83 if (of_property_read_u32(power_mgt
, "ibm,pstate-nominal",
85 pr_warn("ibm,pstate-nominal not found\n");
88 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min
,
89 pstate_nominal
, pstate_max
);
91 pstate_ids
= of_get_property(power_mgt
, "ibm,pstate-ids", &len_ids
);
93 pr_warn("ibm,pstate-ids not found\n");
97 pstate_freqs
= of_get_property(power_mgt
, "ibm,pstate-frequencies-mhz",
100 pr_warn("ibm,pstate-frequencies-mhz not found\n");
104 if (len_ids
!= len_freqs
) {
105 pr_warn("Entries in ibm,pstate-ids and "
106 "ibm,pstate-frequencies-mhz does not match\n");
109 nr_pstates
= min(len_ids
, len_freqs
) / sizeof(u32
);
111 pr_warn("No PStates found\n");
115 pr_debug("NR PStates %d\n", nr_pstates
);
116 for (i
= 0; i
< nr_pstates
; i
++) {
117 u32 id
= be32_to_cpu(pstate_ids
[i
]);
118 u32 freq
= be32_to_cpu(pstate_freqs
[i
]);
120 pr_debug("PState id %d freq %d MHz\n", id
, freq
);
121 powernv_freqs
[i
].frequency
= freq
* 1000; /* kHz */
122 powernv_freqs
[i
].driver_data
= id
;
124 /* End of list marker entry */
125 powernv_freqs
[i
].frequency
= CPUFREQ_TABLE_END
;
127 powernv_pstate_info
.min
= pstate_min
;
128 powernv_pstate_info
.max
= pstate_max
;
129 powernv_pstate_info
.nominal
= pstate_nominal
;
130 powernv_pstate_info
.nr_pstates
= nr_pstates
;
135 /* Returns the CPU frequency corresponding to the pstate_id. */
136 static unsigned int pstate_id_to_freq(int pstate_id
)
140 i
= powernv_pstate_info
.max
- pstate_id
;
141 if (i
>= powernv_pstate_info
.nr_pstates
|| i
< 0) {
142 pr_warn("PState id %d outside of PState table, "
143 "reporting nominal id %d instead\n",
144 pstate_id
, powernv_pstate_info
.nominal
);
145 i
= powernv_pstate_info
.max
- powernv_pstate_info
.nominal
;
148 return powernv_freqs
[i
].frequency
;
152 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
155 static ssize_t
cpuinfo_nominal_freq_show(struct cpufreq_policy
*policy
,
158 return sprintf(buf
, "%u\n",
159 pstate_id_to_freq(powernv_pstate_info
.nominal
));
162 struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq
=
163 __ATTR_RO(cpuinfo_nominal_freq
);
165 static struct freq_attr
*powernv_cpu_freq_attr
[] = {
166 &cpufreq_freq_attr_scaling_available_freqs
,
167 &cpufreq_freq_attr_cpuinfo_nominal_freq
,
171 /* Helper routines */
173 /* Access helpers to power mgt SPR */
175 static inline unsigned long get_pmspr(unsigned long sprn
)
179 return mfspr(SPRN_PMCR
);
182 return mfspr(SPRN_PMICR
);
185 return mfspr(SPRN_PMSR
);
190 static inline void set_pmspr(unsigned long sprn
, unsigned long val
)
194 mtspr(SPRN_PMCR
, val
);
198 mtspr(SPRN_PMICR
, val
);
205 * Use objects of this type to query/update
206 * pstates on a remote CPU via smp_call_function.
208 struct powernv_smp_call_data
{
214 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
216 * Called via smp_call_function.
218 * Note: The caller of the smp_call_function should pass an argument of
219 * the type 'struct powernv_smp_call_data *' along with this function.
221 * The current frequency on this CPU will be returned via
222 * ((struct powernv_smp_call_data *)arg)->freq;
224 static void powernv_read_cpu_freq(void *arg
)
226 unsigned long pmspr_val
;
228 struct powernv_smp_call_data
*freq_data
= arg
;
230 pmspr_val
= get_pmspr(SPRN_PMSR
);
233 * The local pstate id corresponds bits 48..55 in the PMSR.
234 * Note: Watch out for the sign!
236 local_pstate_id
= (pmspr_val
>> 48) & 0xFF;
237 freq_data
->pstate_id
= local_pstate_id
;
238 freq_data
->freq
= pstate_id_to_freq(freq_data
->pstate_id
);
240 pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n",
241 raw_smp_processor_id(), pmspr_val
, freq_data
->pstate_id
,
246 * powernv_cpufreq_get: Returns the CPU frequency as reported by the
247 * firmware for CPU 'cpu'. This value is reported through the sysfs
248 * file cpuinfo_cur_freq.
250 static unsigned int powernv_cpufreq_get(unsigned int cpu
)
252 struct powernv_smp_call_data freq_data
;
254 smp_call_function_any(cpu_sibling_mask(cpu
), powernv_read_cpu_freq
,
257 return freq_data
.freq
;
261 * set_pstate: Sets the pstate on this CPU.
263 * This is called via an smp_call_function.
265 * The caller must ensure that freq_data is of the type
266 * (struct powernv_smp_call_data *) and the pstate_id which needs to be set
267 * on this CPU should be present in freq_data->pstate_id.
269 static void set_pstate(void *freq_data
)
272 unsigned long pstate_ul
=
273 ((struct powernv_smp_call_data
*) freq_data
)->pstate_id
;
275 val
= get_pmspr(SPRN_PMCR
);
276 val
= val
& 0x0000FFFFFFFFFFFFULL
;
278 pstate_ul
= pstate_ul
& 0xFF;
280 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
281 val
= val
| (pstate_ul
<< 56) | (pstate_ul
<< 48);
283 pr_debug("Setting cpu %d pmcr to %016lX\n",
284 raw_smp_processor_id(), val
);
285 set_pmspr(SPRN_PMCR
, val
);
289 * get_nominal_index: Returns the index corresponding to the nominal
290 * pstate in the cpufreq table
292 static inline unsigned int get_nominal_index(void)
294 return powernv_pstate_info
.max
- powernv_pstate_info
.nominal
;
298 * powernv_cpufreq_target_index: Sets the frequency corresponding to
299 * the cpufreq table entry indexed by new_index on the cpus in the
302 static int powernv_cpufreq_target_index(struct cpufreq_policy
*policy
,
303 unsigned int new_index
)
305 struct powernv_smp_call_data freq_data
;
307 if (unlikely(rebooting
) && new_index
!= get_nominal_index())
310 freq_data
.pstate_id
= powernv_freqs
[new_index
].driver_data
;
313 * Use smp_call_function to send IPI and execute the
314 * mtspr on target CPU. We could do that without IPI
315 * if current CPU is within policy->cpus (core)
317 smp_call_function_any(policy
->cpus
, set_pstate
, &freq_data
, 1);
322 static int powernv_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
326 base
= cpu_first_thread_sibling(policy
->cpu
);
328 for (i
= 0; i
< threads_per_core
; i
++)
329 cpumask_set_cpu(base
+ i
, policy
->cpus
);
331 return cpufreq_table_validate_and_show(policy
, powernv_freqs
);
334 static int powernv_cpufreq_reboot_notifier(struct notifier_block
*nb
,
335 unsigned long action
, void *unused
)
338 struct cpufreq_policy cpu_policy
;
341 for_each_online_cpu(cpu
) {
342 cpufreq_get_policy(&cpu_policy
, cpu
);
343 powernv_cpufreq_target_index(&cpu_policy
, get_nominal_index());
349 static struct notifier_block powernv_cpufreq_reboot_nb
= {
350 .notifier_call
= powernv_cpufreq_reboot_notifier
,
353 static void powernv_cpufreq_stop_cpu(struct cpufreq_policy
*policy
)
355 struct powernv_smp_call_data freq_data
;
357 freq_data
.pstate_id
= powernv_pstate_info
.min
;
358 smp_call_function_single(policy
->cpu
, set_pstate
, &freq_data
, 1);
361 static struct cpufreq_driver powernv_cpufreq_driver
= {
362 .name
= "powernv-cpufreq",
363 .flags
= CPUFREQ_CONST_LOOPS
,
364 .init
= powernv_cpufreq_cpu_init
,
365 .verify
= cpufreq_generic_frequency_table_verify
,
366 .target_index
= powernv_cpufreq_target_index
,
367 .get
= powernv_cpufreq_get
,
368 .stop_cpu
= powernv_cpufreq_stop_cpu
,
369 .attr
= powernv_cpu_freq_attr
,
372 static int __init
powernv_cpufreq_init(void)
376 /* Don't probe on pseries (guest) platforms */
377 if (!firmware_has_feature(FW_FEATURE_OPALv3
))
380 /* Discover pstates from device tree and init */
381 rc
= init_powernv_pstates();
383 pr_info("powernv-cpufreq disabled. System does not support PState control\n");
387 register_reboot_notifier(&powernv_cpufreq_reboot_nb
);
388 return cpufreq_register_driver(&powernv_cpufreq_driver
);
390 module_init(powernv_cpufreq_init
);
392 static void __exit
powernv_cpufreq_exit(void)
394 unregister_reboot_notifier(&powernv_cpufreq_reboot_nb
);
395 cpufreq_unregister_driver(&powernv_cpufreq_driver
);
397 module_exit(powernv_cpufreq_exit
);
399 MODULE_LICENSE("GPL");
400 MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");