ARM: 8060/1: mm: allow sub-architectures to override PCI I/O memory type
[linux/fpc-iii.git] / drivers / cpufreq / powernv-cpufreq.c
blob9edccc63245df25d93c22adfb6d6d608b069dfb2
1 /*
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)
11 * any later version.
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>
28 #include <linux/of.h>
30 #include <asm/cputhreads.h>
31 #include <asm/reg.h>
33 #define POWERNV_MAX_PSTATES 256
35 static struct cpufreq_frequency_table powernv_freqs[POWERNV_MAX_PSTATES+1];
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.
42 * The nominal pstate is the highest non-turbo pstate in this
43 * platform. This is indicated by powernv_pstate_info.nominal.
45 static struct powernv_pstate_info {
46 int min;
47 int max;
48 int nominal;
49 int nr_pstates;
50 } powernv_pstate_info;
53 * Initialize the freq table based on data obtained
54 * from the firmware passed via device-tree
56 static int init_powernv_pstates(void)
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;
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;
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;
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;
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;
84 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min,
85 pstate_nominal, pstate_max);
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;
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;
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;
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]);
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;
116 /* End of list marker entry */
117 powernv_freqs[i].frequency = CPUFREQ_TABLE_END;
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;
124 return 0;
127 /* Returns the CPU frequency corresponding to the pstate_id. */
128 static unsigned int pstate_id_to_freq(int pstate_id)
130 int i;
132 i = powernv_pstate_info.max - pstate_id;
133 BUG_ON(i >= powernv_pstate_info.nr_pstates || i < 0);
135 return powernv_freqs[i].frequency;
139 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
140 * the firmware
142 static ssize_t cpuinfo_nominal_freq_show(struct cpufreq_policy *policy,
143 char *buf)
145 return sprintf(buf, "%u\n",
146 pstate_id_to_freq(powernv_pstate_info.nominal));
149 struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq =
150 __ATTR_RO(cpuinfo_nominal_freq);
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,
158 /* Helper routines */
160 /* Access helpers to power mgt SPR */
162 static inline unsigned long get_pmspr(unsigned long sprn)
164 switch (sprn) {
165 case SPRN_PMCR:
166 return mfspr(SPRN_PMCR);
168 case SPRN_PMICR:
169 return mfspr(SPRN_PMICR);
171 case SPRN_PMSR:
172 return mfspr(SPRN_PMSR);
174 BUG();
177 static inline void set_pmspr(unsigned long sprn, unsigned long val)
179 switch (sprn) {
180 case SPRN_PMCR:
181 mtspr(SPRN_PMCR, val);
182 return;
184 case SPRN_PMICR:
185 mtspr(SPRN_PMICR, val);
186 return;
188 BUG();
192 * Use objects of this type to query/update
193 * pstates on a remote CPU via smp_call_function.
195 struct powernv_smp_call_data {
196 unsigned int freq;
197 int pstate_id;
201 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
203 * Called via smp_call_function.
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.
208 * The current frequency on this CPU will be returned via
209 * ((struct powernv_smp_call_data *)arg)->freq;
211 static void powernv_read_cpu_freq(void *arg)
213 unsigned long pmspr_val;
214 s8 local_pstate_id;
215 struct powernv_smp_call_data *freq_data = arg;
217 pmspr_val = get_pmspr(SPRN_PMSR);
220 * The local pstate id corresponds bits 48..55 in the PMSR.
221 * Note: Watch out for the sign!
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);
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);
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.
237 unsigned int powernv_cpufreq_get(unsigned int cpu)
239 struct powernv_smp_call_data freq_data;
241 smp_call_function_any(cpu_sibling_mask(cpu), powernv_read_cpu_freq,
242 &freq_data, 1);
244 return freq_data.freq;
248 * set_pstate: Sets the pstate on this CPU.
250 * This is called via an smp_call_function.
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.
256 static void set_pstate(void *freq_data)
258 unsigned long val;
259 unsigned long pstate_ul =
260 ((struct powernv_smp_call_data *) freq_data)->pstate_id;
262 val = get_pmspr(SPRN_PMCR);
263 val = val & 0x0000FFFFFFFFFFFFULL;
265 pstate_ul = pstate_ul & 0xFF;
267 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
268 val = val | (pstate_ul << 56) | (pstate_ul << 48);
270 pr_debug("Setting cpu %d pmcr to %016lX\n",
271 raw_smp_processor_id(), val);
272 set_pmspr(SPRN_PMCR, val);
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
280 static int powernv_cpufreq_target_index(struct cpufreq_policy *policy,
281 unsigned int new_index)
283 struct powernv_smp_call_data freq_data;
285 freq_data.pstate_id = powernv_freqs[new_index].driver_data;
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)
292 smp_call_function_any(policy->cpus, set_pstate, &freq_data, 1);
294 return 0;
297 static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy)
299 int base, i;
301 base = cpu_first_thread_sibling(policy->cpu);
303 for (i = 0; i < threads_per_core; i++)
304 cpumask_set_cpu(base + i, policy->cpus);
306 return cpufreq_table_validate_and_show(policy, powernv_freqs);
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,
319 static int __init powernv_cpufreq_init(void)
321 int rc = 0;
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;
330 return cpufreq_register_driver(&powernv_cpufreq_driver);
332 module_init(powernv_cpufreq_init);
334 static void __exit powernv_cpufreq_exit(void)
336 cpufreq_unregister_driver(&powernv_cpufreq_driver);
338 module_exit(powernv_cpufreq_exit);
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");