blob: d0c18c9ce1ff211e62ec6d96e0820c8132df8ffe [file] [log] [blame]
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +05301/*
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>
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +020029#include <linux/reboot.h>
Shilpasri G Bhat053819e2015-07-16 13:34:18 +053030#include <linux/slab.h>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053031
32#include <asm/cputhreads.h>
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +053033#include <asm/firmware.h>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053034#include <asm/reg.h>
Srivatsa S. Bhatf3cae352014-04-16 11:35:38 +053035#include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053036
37#define POWERNV_MAX_PSTATES 256
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +053038#define PMSR_PSAFE_ENABLE (1UL << 30)
39#define PMSR_SPR_EM_DISABLE (1UL << 31)
40#define PMSR_MAX(x) ((x >> 32) & 0xFF)
41#define PMSR_LP(x) ((x >> 48) & 0xFF)
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053042
43static struct cpufreq_frequency_table powernv_freqs[POWERNV_MAX_PSTATES+1];
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +053044static bool rebooting, throttled;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053045
Shilpasri G Bhat053819e2015-07-16 13:34:18 +053046static struct chip {
47 unsigned int id;
48 bool throttled;
49} *chips;
50
51static int nr_chips;
52
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053053/*
54 * Note: The set of pstates consists of contiguous integers, the
55 * smallest of which is indicated by powernv_pstate_info.min, the
56 * largest of which is indicated by powernv_pstate_info.max.
57 *
58 * The nominal pstate is the highest non-turbo pstate in this
59 * platform. This is indicated by powernv_pstate_info.nominal.
60 */
61static struct powernv_pstate_info {
62 int min;
63 int max;
64 int nominal;
65 int nr_pstates;
66} powernv_pstate_info;
67
68/*
69 * Initialize the freq table based on data obtained
70 * from the firmware passed via device-tree
71 */
72static int init_powernv_pstates(void)
73{
74 struct device_node *power_mgt;
75 int i, pstate_min, pstate_max, pstate_nominal, nr_pstates = 0;
76 const __be32 *pstate_ids, *pstate_freqs;
77 u32 len_ids, len_freqs;
78
79 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
80 if (!power_mgt) {
81 pr_warn("power-mgt node not found\n");
82 return -ENODEV;
83 }
84
85 if (of_property_read_u32(power_mgt, "ibm,pstate-min", &pstate_min)) {
86 pr_warn("ibm,pstate-min node not found\n");
87 return -ENODEV;
88 }
89
90 if (of_property_read_u32(power_mgt, "ibm,pstate-max", &pstate_max)) {
91 pr_warn("ibm,pstate-max node not found\n");
92 return -ENODEV;
93 }
94
95 if (of_property_read_u32(power_mgt, "ibm,pstate-nominal",
96 &pstate_nominal)) {
97 pr_warn("ibm,pstate-nominal not found\n");
98 return -ENODEV;
99 }
100 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min,
101 pstate_nominal, pstate_max);
102
103 pstate_ids = of_get_property(power_mgt, "ibm,pstate-ids", &len_ids);
104 if (!pstate_ids) {
105 pr_warn("ibm,pstate-ids not found\n");
106 return -ENODEV;
107 }
108
109 pstate_freqs = of_get_property(power_mgt, "ibm,pstate-frequencies-mhz",
110 &len_freqs);
111 if (!pstate_freqs) {
112 pr_warn("ibm,pstate-frequencies-mhz not found\n");
113 return -ENODEV;
114 }
115
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530116 if (len_ids != len_freqs) {
117 pr_warn("Entries in ibm,pstate-ids and "
118 "ibm,pstate-frequencies-mhz does not match\n");
119 }
120
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530121 nr_pstates = min(len_ids, len_freqs) / sizeof(u32);
122 if (!nr_pstates) {
123 pr_warn("No PStates found\n");
124 return -ENODEV;
125 }
126
127 pr_debug("NR PStates %d\n", nr_pstates);
128 for (i = 0; i < nr_pstates; i++) {
129 u32 id = be32_to_cpu(pstate_ids[i]);
130 u32 freq = be32_to_cpu(pstate_freqs[i]);
131
132 pr_debug("PState id %d freq %d MHz\n", id, freq);
133 powernv_freqs[i].frequency = freq * 1000; /* kHz */
Gautham R. Shenoy0692c692014-04-01 12:43:27 +0530134 powernv_freqs[i].driver_data = id;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530135 }
136 /* End of list marker entry */
137 powernv_freqs[i].frequency = CPUFREQ_TABLE_END;
138
139 powernv_pstate_info.min = pstate_min;
140 powernv_pstate_info.max = pstate_max;
141 powernv_pstate_info.nominal = pstate_nominal;
142 powernv_pstate_info.nr_pstates = nr_pstates;
143
144 return 0;
145}
146
147/* Returns the CPU frequency corresponding to the pstate_id. */
148static unsigned int pstate_id_to_freq(int pstate_id)
149{
150 int i;
151
152 i = powernv_pstate_info.max - pstate_id;
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530153 if (i >= powernv_pstate_info.nr_pstates || i < 0) {
154 pr_warn("PState id %d outside of PState table, "
155 "reporting nominal id %d instead\n",
156 pstate_id, powernv_pstate_info.nominal);
157 i = powernv_pstate_info.max - powernv_pstate_info.nominal;
158 }
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530159
160 return powernv_freqs[i].frequency;
161}
162
163/*
164 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
165 * the firmware
166 */
167static ssize_t cpuinfo_nominal_freq_show(struct cpufreq_policy *policy,
168 char *buf)
169{
170 return sprintf(buf, "%u\n",
171 pstate_id_to_freq(powernv_pstate_info.nominal));
172}
173
174struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq =
175 __ATTR_RO(cpuinfo_nominal_freq);
176
177static struct freq_attr *powernv_cpu_freq_attr[] = {
178 &cpufreq_freq_attr_scaling_available_freqs,
179 &cpufreq_freq_attr_cpuinfo_nominal_freq,
180 NULL,
181};
182
183/* Helper routines */
184
185/* Access helpers to power mgt SPR */
186
187static inline unsigned long get_pmspr(unsigned long sprn)
188{
189 switch (sprn) {
190 case SPRN_PMCR:
191 return mfspr(SPRN_PMCR);
192
193 case SPRN_PMICR:
194 return mfspr(SPRN_PMICR);
195
196 case SPRN_PMSR:
197 return mfspr(SPRN_PMSR);
198 }
199 BUG();
200}
201
202static inline void set_pmspr(unsigned long sprn, unsigned long val)
203{
204 switch (sprn) {
205 case SPRN_PMCR:
206 mtspr(SPRN_PMCR, val);
207 return;
208
209 case SPRN_PMICR:
210 mtspr(SPRN_PMICR, val);
211 return;
212 }
213 BUG();
214}
215
216/*
217 * Use objects of this type to query/update
218 * pstates on a remote CPU via smp_call_function.
219 */
220struct powernv_smp_call_data {
221 unsigned int freq;
222 int pstate_id;
223};
224
225/*
226 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
227 *
228 * Called via smp_call_function.
229 *
230 * Note: The caller of the smp_call_function should pass an argument of
231 * the type 'struct powernv_smp_call_data *' along with this function.
232 *
233 * The current frequency on this CPU will be returned via
234 * ((struct powernv_smp_call_data *)arg)->freq;
235 */
236static void powernv_read_cpu_freq(void *arg)
237{
238 unsigned long pmspr_val;
239 s8 local_pstate_id;
240 struct powernv_smp_call_data *freq_data = arg;
241
242 pmspr_val = get_pmspr(SPRN_PMSR);
243
244 /*
245 * The local pstate id corresponds bits 48..55 in the PMSR.
246 * Note: Watch out for the sign!
247 */
248 local_pstate_id = (pmspr_val >> 48) & 0xFF;
249 freq_data->pstate_id = local_pstate_id;
250 freq_data->freq = pstate_id_to_freq(freq_data->pstate_id);
251
252 pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n",
253 raw_smp_processor_id(), pmspr_val, freq_data->pstate_id,
254 freq_data->freq);
255}
256
257/*
258 * powernv_cpufreq_get: Returns the CPU frequency as reported by the
259 * firmware for CPU 'cpu'. This value is reported through the sysfs
260 * file cpuinfo_cur_freq.
261 */
Brian Norris60d1ea42014-05-11 00:51:20 -0700262static unsigned int powernv_cpufreq_get(unsigned int cpu)
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530263{
264 struct powernv_smp_call_data freq_data;
265
266 smp_call_function_any(cpu_sibling_mask(cpu), powernv_read_cpu_freq,
267 &freq_data, 1);
268
269 return freq_data.freq;
270}
271
272/*
273 * set_pstate: Sets the pstate on this CPU.
274 *
275 * This is called via an smp_call_function.
276 *
277 * The caller must ensure that freq_data is of the type
278 * (struct powernv_smp_call_data *) and the pstate_id which needs to be set
279 * on this CPU should be present in freq_data->pstate_id.
280 */
281static void set_pstate(void *freq_data)
282{
283 unsigned long val;
284 unsigned long pstate_ul =
285 ((struct powernv_smp_call_data *) freq_data)->pstate_id;
286
287 val = get_pmspr(SPRN_PMCR);
288 val = val & 0x0000FFFFFFFFFFFFULL;
289
290 pstate_ul = pstate_ul & 0xFF;
291
292 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
293 val = val | (pstate_ul << 56) | (pstate_ul << 48);
294
295 pr_debug("Setting cpu %d pmcr to %016lX\n",
296 raw_smp_processor_id(), val);
297 set_pmspr(SPRN_PMCR, val);
298}
299
300/*
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200301 * get_nominal_index: Returns the index corresponding to the nominal
302 * pstate in the cpufreq table
303 */
304static inline unsigned int get_nominal_index(void)
305{
306 return powernv_pstate_info.max - powernv_pstate_info.nominal;
307}
308
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530309static void powernv_cpufreq_throttle_check(unsigned int cpu)
310{
311 unsigned long pmsr;
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530312 int pmsr_pmax, pmsr_lp, i;
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530313
314 pmsr = get_pmspr(SPRN_PMSR);
315
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530316 for (i = 0; i < nr_chips; i++)
317 if (chips[i].id == cpu_to_chip_id(cpu))
318 break;
319
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530320 /* Check for Pmax Capping */
321 pmsr_pmax = (s8)PMSR_MAX(pmsr);
322 if (pmsr_pmax != powernv_pstate_info.max) {
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530323 if (chips[i].throttled)
324 goto next;
325 chips[i].throttled = true;
326 pr_info("CPU %d on Chip %u has Pmax reduced to %d\n", cpu,
327 chips[i].id, pmsr_pmax);
328 } else if (chips[i].throttled) {
329 chips[i].throttled = false;
330 pr_info("CPU %d on Chip %u has Pmax restored to %d\n", cpu,
331 chips[i].id, pmsr_pmax);
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530332 }
333
334 /*
335 * Check for Psafe by reading LocalPstate
336 * or check if Psafe_mode_active is set in PMSR.
337 */
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530338next:
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530339 pmsr_lp = (s8)PMSR_LP(pmsr);
340 if ((pmsr_lp < powernv_pstate_info.min) ||
341 (pmsr & PMSR_PSAFE_ENABLE)) {
342 throttled = true;
343 pr_info("Pstate set to safe frequency\n");
344 }
345
346 /* Check if SPR_EM_DISABLE is set in PMSR */
347 if (pmsr & PMSR_SPR_EM_DISABLE) {
348 throttled = true;
349 pr_info("Frequency Control disabled from OS\n");
350 }
351
352 if (throttled) {
353 pr_info("PMSR = %16lx\n", pmsr);
354 pr_crit("CPU Frequency could be throttled\n");
355 }
356}
357
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200358/*
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530359 * powernv_cpufreq_target_index: Sets the frequency corresponding to
360 * the cpufreq table entry indexed by new_index on the cpus in the
361 * mask policy->cpus
362 */
363static int powernv_cpufreq_target_index(struct cpufreq_policy *policy,
364 unsigned int new_index)
365{
366 struct powernv_smp_call_data freq_data;
367
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200368 if (unlikely(rebooting) && new_index != get_nominal_index())
369 return 0;
370
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530371 if (!throttled)
372 powernv_cpufreq_throttle_check(smp_processor_id());
373
Gautham R. Shenoy0692c692014-04-01 12:43:27 +0530374 freq_data.pstate_id = powernv_freqs[new_index].driver_data;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530375
376 /*
377 * Use smp_call_function to send IPI and execute the
378 * mtspr on target CPU. We could do that without IPI
379 * if current CPU is within policy->cpus (core)
380 */
381 smp_call_function_any(policy->cpus, set_pstate, &freq_data, 1);
382
383 return 0;
384}
385
386static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy)
387{
388 int base, i;
389
390 base = cpu_first_thread_sibling(policy->cpu);
391
392 for (i = 0; i < threads_per_core; i++)
393 cpumask_set_cpu(base + i, policy->cpus);
394
395 return cpufreq_table_validate_and_show(policy, powernv_freqs);
396}
397
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200398static int powernv_cpufreq_reboot_notifier(struct notifier_block *nb,
399 unsigned long action, void *unused)
400{
401 int cpu;
402 struct cpufreq_policy cpu_policy;
403
404 rebooting = true;
405 for_each_online_cpu(cpu) {
406 cpufreq_get_policy(&cpu_policy, cpu);
407 powernv_cpufreq_target_index(&cpu_policy, get_nominal_index());
408 }
409
410 return NOTIFY_DONE;
411}
412
413static struct notifier_block powernv_cpufreq_reboot_nb = {
414 .notifier_call = powernv_cpufreq_reboot_notifier,
415};
416
Preeti U Murthyb1203392014-09-29 15:47:53 +0200417static void powernv_cpufreq_stop_cpu(struct cpufreq_policy *policy)
418{
419 struct powernv_smp_call_data freq_data;
420
421 freq_data.pstate_id = powernv_pstate_info.min;
422 smp_call_function_single(policy->cpu, set_pstate, &freq_data, 1);
423}
424
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530425static struct cpufreq_driver powernv_cpufreq_driver = {
426 .name = "powernv-cpufreq",
427 .flags = CPUFREQ_CONST_LOOPS,
428 .init = powernv_cpufreq_cpu_init,
429 .verify = cpufreq_generic_frequency_table_verify,
430 .target_index = powernv_cpufreq_target_index,
431 .get = powernv_cpufreq_get,
Preeti U Murthyb1203392014-09-29 15:47:53 +0200432 .stop_cpu = powernv_cpufreq_stop_cpu,
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530433 .attr = powernv_cpu_freq_attr,
434};
435
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530436static int init_chip_info(void)
437{
438 unsigned int chip[256];
439 unsigned int cpu, i;
440 unsigned int prev_chip_id = UINT_MAX;
441
442 for_each_possible_cpu(cpu) {
443 unsigned int id = cpu_to_chip_id(cpu);
444
445 if (prev_chip_id != id) {
446 prev_chip_id = id;
447 chip[nr_chips++] = id;
448 }
449 }
450
451 chips = kmalloc_array(nr_chips, sizeof(struct chip), GFP_KERNEL);
452 if (!chips)
453 return -ENOMEM;
454
455 for (i = 0; i < nr_chips; i++) {
456 chips[i].id = chip[i];
457 chips[i].throttled = false;
458 }
459
460 return 0;
461}
462
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530463static int __init powernv_cpufreq_init(void)
464{
465 int rc = 0;
466
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530467 /* Don't probe on pseries (guest) platforms */
468 if (!firmware_has_feature(FW_FEATURE_OPALv3))
469 return -ENODEV;
470
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530471 /* Discover pstates from device tree and init */
472 rc = init_powernv_pstates();
473 if (rc) {
474 pr_info("powernv-cpufreq disabled. System does not support PState control\n");
475 return rc;
476 }
477
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530478 /* Populate chip info */
479 rc = init_chip_info();
480 if (rc)
481 return rc;
482
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200483 register_reboot_notifier(&powernv_cpufreq_reboot_nb);
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530484 return cpufreq_register_driver(&powernv_cpufreq_driver);
485}
486module_init(powernv_cpufreq_init);
487
488static void __exit powernv_cpufreq_exit(void)
489{
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200490 unregister_reboot_notifier(&powernv_cpufreq_reboot_nb);
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530491 cpufreq_unregister_driver(&powernv_cpufreq_driver);
492}
493module_exit(powernv_cpufreq_exit);
494
495MODULE_LICENSE("GPL");
496MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");