cpufreq: powernv: Report Psafe only if PMSR.psafe_mode_active bit is set
[deliverable/linux.git] / drivers / cpufreq / powernv-cpufreq.c
CommitLineData
b3d627a5
VS
1/*
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>
cf30af76 29#include <linux/reboot.h>
053819e0 30#include <linux/slab.h>
b3d627a5
VS
31
32#include <asm/cputhreads.h>
6174bac8 33#include <asm/firmware.h>
b3d627a5 34#include <asm/reg.h>
f3cae355 35#include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
cb166fa9 36#include <asm/opal.h>
b3d627a5
VS
37
38#define POWERNV_MAX_PSTATES 256
09a972d1
SB
39#define PMSR_PSAFE_ENABLE (1UL << 30)
40#define PMSR_SPR_EM_DISABLE (1UL << 31)
41#define PMSR_MAX(x) ((x >> 32) & 0xFF)
b3d627a5
VS
42
43static struct cpufreq_frequency_table powernv_freqs[POWERNV_MAX_PSTATES+1];
cb166fa9 44static bool rebooting, throttled, occ_reset;
b3d627a5 45
053819e0
SB
46static struct chip {
47 unsigned int id;
48 bool throttled;
735366fc
SB
49 cpumask_t mask;
50 struct work_struct throttle;
053819e0
SB
51} *chips;
52
53static int nr_chips;
54
b3d627a5
VS
55/*
56 * Note: The set of pstates consists of contiguous integers, the
57 * smallest of which is indicated by powernv_pstate_info.min, the
58 * largest of which is indicated by powernv_pstate_info.max.
59 *
60 * The nominal pstate is the highest non-turbo pstate in this
61 * platform. This is indicated by powernv_pstate_info.nominal.
62 */
63static struct powernv_pstate_info {
64 int min;
65 int max;
66 int nominal;
67 int nr_pstates;
68} powernv_pstate_info;
69
70/*
71 * Initialize the freq table based on data obtained
72 * from the firmware passed via device-tree
73 */
74static int init_powernv_pstates(void)
75{
76 struct device_node *power_mgt;
77 int i, pstate_min, pstate_max, pstate_nominal, nr_pstates = 0;
78 const __be32 *pstate_ids, *pstate_freqs;
79 u32 len_ids, len_freqs;
80
81 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
82 if (!power_mgt) {
83 pr_warn("power-mgt node not found\n");
84 return -ENODEV;
85 }
86
87 if (of_property_read_u32(power_mgt, "ibm,pstate-min", &pstate_min)) {
88 pr_warn("ibm,pstate-min node not found\n");
89 return -ENODEV;
90 }
91
92 if (of_property_read_u32(power_mgt, "ibm,pstate-max", &pstate_max)) {
93 pr_warn("ibm,pstate-max node not found\n");
94 return -ENODEV;
95 }
96
97 if (of_property_read_u32(power_mgt, "ibm,pstate-nominal",
98 &pstate_nominal)) {
99 pr_warn("ibm,pstate-nominal not found\n");
100 return -ENODEV;
101 }
102 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min,
103 pstate_nominal, pstate_max);
104
105 pstate_ids = of_get_property(power_mgt, "ibm,pstate-ids", &len_ids);
106 if (!pstate_ids) {
107 pr_warn("ibm,pstate-ids not found\n");
108 return -ENODEV;
109 }
110
111 pstate_freqs = of_get_property(power_mgt, "ibm,pstate-frequencies-mhz",
112 &len_freqs);
113 if (!pstate_freqs) {
114 pr_warn("ibm,pstate-frequencies-mhz not found\n");
115 return -ENODEV;
116 }
117
6174bac8
VS
118 if (len_ids != len_freqs) {
119 pr_warn("Entries in ibm,pstate-ids and "
120 "ibm,pstate-frequencies-mhz does not match\n");
121 }
122
b3d627a5
VS
123 nr_pstates = min(len_ids, len_freqs) / sizeof(u32);
124 if (!nr_pstates) {
125 pr_warn("No PStates found\n");
126 return -ENODEV;
127 }
128
129 pr_debug("NR PStates %d\n", nr_pstates);
130 for (i = 0; i < nr_pstates; i++) {
131 u32 id = be32_to_cpu(pstate_ids[i]);
132 u32 freq = be32_to_cpu(pstate_freqs[i]);
133
134 pr_debug("PState id %d freq %d MHz\n", id, freq);
135 powernv_freqs[i].frequency = freq * 1000; /* kHz */
0692c691 136 powernv_freqs[i].driver_data = id;
b3d627a5
VS
137 }
138 /* End of list marker entry */
139 powernv_freqs[i].frequency = CPUFREQ_TABLE_END;
140
141 powernv_pstate_info.min = pstate_min;
142 powernv_pstate_info.max = pstate_max;
143 powernv_pstate_info.nominal = pstate_nominal;
144 powernv_pstate_info.nr_pstates = nr_pstates;
145
146 return 0;
147}
148
149/* Returns the CPU frequency corresponding to the pstate_id. */
150static unsigned int pstate_id_to_freq(int pstate_id)
151{
152 int i;
153
154 i = powernv_pstate_info.max - pstate_id;
6174bac8
VS
155 if (i >= powernv_pstate_info.nr_pstates || i < 0) {
156 pr_warn("PState id %d outside of PState table, "
157 "reporting nominal id %d instead\n",
158 pstate_id, powernv_pstate_info.nominal);
159 i = powernv_pstate_info.max - powernv_pstate_info.nominal;
160 }
b3d627a5
VS
161
162 return powernv_freqs[i].frequency;
163}
164
165/*
166 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
167 * the firmware
168 */
169static ssize_t cpuinfo_nominal_freq_show(struct cpufreq_policy *policy,
170 char *buf)
171{
172 return sprintf(buf, "%u\n",
173 pstate_id_to_freq(powernv_pstate_info.nominal));
174}
175
176struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq =
177 __ATTR_RO(cpuinfo_nominal_freq);
178
179static struct freq_attr *powernv_cpu_freq_attr[] = {
180 &cpufreq_freq_attr_scaling_available_freqs,
181 &cpufreq_freq_attr_cpuinfo_nominal_freq,
182 NULL,
183};
184
185/* Helper routines */
186
187/* Access helpers to power mgt SPR */
188
189static inline unsigned long get_pmspr(unsigned long sprn)
190{
191 switch (sprn) {
192 case SPRN_PMCR:
193 return mfspr(SPRN_PMCR);
194
195 case SPRN_PMICR:
196 return mfspr(SPRN_PMICR);
197
198 case SPRN_PMSR:
199 return mfspr(SPRN_PMSR);
200 }
201 BUG();
202}
203
204static inline void set_pmspr(unsigned long sprn, unsigned long val)
205{
206 switch (sprn) {
207 case SPRN_PMCR:
208 mtspr(SPRN_PMCR, val);
209 return;
210
211 case SPRN_PMICR:
212 mtspr(SPRN_PMICR, val);
213 return;
214 }
215 BUG();
216}
217
218/*
219 * Use objects of this type to query/update
220 * pstates on a remote CPU via smp_call_function.
221 */
222struct powernv_smp_call_data {
223 unsigned int freq;
224 int pstate_id;
225};
226
227/*
228 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
229 *
230 * Called via smp_call_function.
231 *
232 * Note: The caller of the smp_call_function should pass an argument of
233 * the type 'struct powernv_smp_call_data *' along with this function.
234 *
235 * The current frequency on this CPU will be returned via
236 * ((struct powernv_smp_call_data *)arg)->freq;
237 */
238static void powernv_read_cpu_freq(void *arg)
239{
240 unsigned long pmspr_val;
241 s8 local_pstate_id;
242 struct powernv_smp_call_data *freq_data = arg;
243
244 pmspr_val = get_pmspr(SPRN_PMSR);
245
246 /*
247 * The local pstate id corresponds bits 48..55 in the PMSR.
248 * Note: Watch out for the sign!
249 */
250 local_pstate_id = (pmspr_val >> 48) & 0xFF;
251 freq_data->pstate_id = local_pstate_id;
252 freq_data->freq = pstate_id_to_freq(freq_data->pstate_id);
253
254 pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n",
255 raw_smp_processor_id(), pmspr_val, freq_data->pstate_id,
256 freq_data->freq);
257}
258
259/*
260 * powernv_cpufreq_get: Returns the CPU frequency as reported by the
261 * firmware for CPU 'cpu'. This value is reported through the sysfs
262 * file cpuinfo_cur_freq.
263 */
60d1ea4e 264static unsigned int powernv_cpufreq_get(unsigned int cpu)
b3d627a5
VS
265{
266 struct powernv_smp_call_data freq_data;
267
268 smp_call_function_any(cpu_sibling_mask(cpu), powernv_read_cpu_freq,
269 &freq_data, 1);
270
271 return freq_data.freq;
272}
273
274/*
275 * set_pstate: Sets the pstate on this CPU.
276 *
277 * This is called via an smp_call_function.
278 *
279 * The caller must ensure that freq_data is of the type
280 * (struct powernv_smp_call_data *) and the pstate_id which needs to be set
281 * on this CPU should be present in freq_data->pstate_id.
282 */
283static void set_pstate(void *freq_data)
284{
285 unsigned long val;
286 unsigned long pstate_ul =
287 ((struct powernv_smp_call_data *) freq_data)->pstate_id;
288
289 val = get_pmspr(SPRN_PMCR);
290 val = val & 0x0000FFFFFFFFFFFFULL;
291
292 pstate_ul = pstate_ul & 0xFF;
293
294 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
295 val = val | (pstate_ul << 56) | (pstate_ul << 48);
296
297 pr_debug("Setting cpu %d pmcr to %016lX\n",
298 raw_smp_processor_id(), val);
299 set_pmspr(SPRN_PMCR, val);
300}
301
cf30af76
SB
302/*
303 * get_nominal_index: Returns the index corresponding to the nominal
304 * pstate in the cpufreq table
305 */
306static inline unsigned int get_nominal_index(void)
307{
308 return powernv_pstate_info.max - powernv_pstate_info.nominal;
309}
310
735366fc 311static void powernv_cpufreq_throttle_check(void *data)
09a972d1 312{
735366fc 313 unsigned int cpu = smp_processor_id();
09a972d1 314 unsigned long pmsr;
3dd3ebe5 315 int pmsr_pmax, i;
09a972d1
SB
316
317 pmsr = get_pmspr(SPRN_PMSR);
318
053819e0
SB
319 for (i = 0; i < nr_chips; i++)
320 if (chips[i].id == cpu_to_chip_id(cpu))
321 break;
322
09a972d1
SB
323 /* Check for Pmax Capping */
324 pmsr_pmax = (s8)PMSR_MAX(pmsr);
325 if (pmsr_pmax != powernv_pstate_info.max) {
053819e0
SB
326 if (chips[i].throttled)
327 goto next;
328 chips[i].throttled = true;
329 pr_info("CPU %d on Chip %u has Pmax reduced to %d\n", cpu,
330 chips[i].id, pmsr_pmax);
331 } else if (chips[i].throttled) {
332 chips[i].throttled = false;
333 pr_info("CPU %d on Chip %u has Pmax restored to %d\n", cpu,
334 chips[i].id, pmsr_pmax);
09a972d1
SB
335 }
336
3dd3ebe5 337 /* Check if Psafe_mode_active is set in PMSR. */
053819e0 338next:
3dd3ebe5 339 if (pmsr & PMSR_PSAFE_ENABLE) {
09a972d1
SB
340 throttled = true;
341 pr_info("Pstate set to safe frequency\n");
342 }
343
344 /* Check if SPR_EM_DISABLE is set in PMSR */
345 if (pmsr & PMSR_SPR_EM_DISABLE) {
346 throttled = true;
347 pr_info("Frequency Control disabled from OS\n");
348 }
349
350 if (throttled) {
351 pr_info("PMSR = %16lx\n", pmsr);
352 pr_crit("CPU Frequency could be throttled\n");
353 }
354}
355
b3d627a5
VS
356/*
357 * powernv_cpufreq_target_index: Sets the frequency corresponding to
358 * the cpufreq table entry indexed by new_index on the cpus in the
359 * mask policy->cpus
360 */
361static int powernv_cpufreq_target_index(struct cpufreq_policy *policy,
362 unsigned int new_index)
363{
364 struct powernv_smp_call_data freq_data;
365
cf30af76
SB
366 if (unlikely(rebooting) && new_index != get_nominal_index())
367 return 0;
368
09a972d1 369 if (!throttled)
735366fc 370 powernv_cpufreq_throttle_check(NULL);
09a972d1 371
0692c691 372 freq_data.pstate_id = powernv_freqs[new_index].driver_data;
b3d627a5
VS
373
374 /*
375 * Use smp_call_function to send IPI and execute the
376 * mtspr on target CPU. We could do that without IPI
377 * if current CPU is within policy->cpus (core)
378 */
379 smp_call_function_any(policy->cpus, set_pstate, &freq_data, 1);
380
381 return 0;
382}
383
384static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy)
385{
386 int base, i;
387
388 base = cpu_first_thread_sibling(policy->cpu);
389
390 for (i = 0; i < threads_per_core; i++)
391 cpumask_set_cpu(base + i, policy->cpus);
392
393 return cpufreq_table_validate_and_show(policy, powernv_freqs);
394}
395
cf30af76
SB
396static int powernv_cpufreq_reboot_notifier(struct notifier_block *nb,
397 unsigned long action, void *unused)
398{
399 int cpu;
400 struct cpufreq_policy cpu_policy;
401
402 rebooting = true;
403 for_each_online_cpu(cpu) {
404 cpufreq_get_policy(&cpu_policy, cpu);
405 powernv_cpufreq_target_index(&cpu_policy, get_nominal_index());
406 }
407
408 return NOTIFY_DONE;
409}
410
411static struct notifier_block powernv_cpufreq_reboot_nb = {
412 .notifier_call = powernv_cpufreq_reboot_notifier,
413};
414
735366fc
SB
415void powernv_cpufreq_work_fn(struct work_struct *work)
416{
417 struct chip *chip = container_of(work, struct chip, throttle);
418
419 smp_call_function_any(&chip->mask,
420 powernv_cpufreq_throttle_check, NULL, 0);
421}
422
cb166fa9
SB
423static char throttle_reason[][30] = {
424 "No throttling",
425 "Power Cap",
426 "Processor Over Temperature",
427 "Power Supply Failure",
428 "Over Current",
429 "OCC Reset"
430 };
431
432static int powernv_cpufreq_occ_msg(struct notifier_block *nb,
433 unsigned long msg_type, void *_msg)
434{
435 struct opal_msg *msg = _msg;
436 struct opal_occ_msg omsg;
735366fc 437 int i;
cb166fa9
SB
438
439 if (msg_type != OPAL_MSG_OCC)
440 return 0;
441
442 omsg.type = be64_to_cpu(msg->params[0]);
443
444 switch (omsg.type) {
445 case OCC_RESET:
446 occ_reset = true;
447 /*
448 * powernv_cpufreq_throttle_check() is called in
449 * target() callback which can detect the throttle state
450 * for governors like ondemand.
451 * But static governors will not call target() often thus
452 * report throttling here.
453 */
454 if (!throttled) {
455 throttled = true;
456 pr_crit("CPU Frequency is throttled\n");
457 }
458 pr_info("OCC: Reset\n");
459 break;
460 case OCC_LOAD:
461 pr_info("OCC: Loaded\n");
462 break;
463 case OCC_THROTTLE:
464 omsg.chip = be64_to_cpu(msg->params[1]);
465 omsg.throttle_status = be64_to_cpu(msg->params[2]);
466
467 if (occ_reset) {
468 occ_reset = false;
469 throttled = false;
470 pr_info("OCC: Active\n");
735366fc
SB
471
472 for (i = 0; i < nr_chips; i++)
473 schedule_work(&chips[i].throttle);
474
cb166fa9
SB
475 return 0;
476 }
477
478 if (omsg.throttle_status &&
479 omsg.throttle_status <= OCC_MAX_THROTTLE_STATUS)
480 pr_info("OCC: Chip %u Pmax reduced due to %s\n",
481 (unsigned int)omsg.chip,
482 throttle_reason[omsg.throttle_status]);
483 else if (!omsg.throttle_status)
484 pr_info("OCC: Chip %u %s\n", (unsigned int)omsg.chip,
485 throttle_reason[omsg.throttle_status]);
735366fc
SB
486 else
487 return 0;
488
489 for (i = 0; i < nr_chips; i++)
490 if (chips[i].id == omsg.chip)
491 schedule_work(&chips[i].throttle);
cb166fa9
SB
492 }
493 return 0;
494}
495
496static struct notifier_block powernv_cpufreq_opal_nb = {
497 .notifier_call = powernv_cpufreq_occ_msg,
498 .next = NULL,
499 .priority = 0,
500};
501
b120339c
PM
502static void powernv_cpufreq_stop_cpu(struct cpufreq_policy *policy)
503{
504 struct powernv_smp_call_data freq_data;
505
506 freq_data.pstate_id = powernv_pstate_info.min;
507 smp_call_function_single(policy->cpu, set_pstate, &freq_data, 1);
508}
509
b3d627a5
VS
510static struct cpufreq_driver powernv_cpufreq_driver = {
511 .name = "powernv-cpufreq",
512 .flags = CPUFREQ_CONST_LOOPS,
513 .init = powernv_cpufreq_cpu_init,
514 .verify = cpufreq_generic_frequency_table_verify,
515 .target_index = powernv_cpufreq_target_index,
516 .get = powernv_cpufreq_get,
b120339c 517 .stop_cpu = powernv_cpufreq_stop_cpu,
b3d627a5
VS
518 .attr = powernv_cpu_freq_attr,
519};
520
053819e0
SB
521static int init_chip_info(void)
522{
523 unsigned int chip[256];
524 unsigned int cpu, i;
525 unsigned int prev_chip_id = UINT_MAX;
526
527 for_each_possible_cpu(cpu) {
528 unsigned int id = cpu_to_chip_id(cpu);
529
530 if (prev_chip_id != id) {
531 prev_chip_id = id;
532 chip[nr_chips++] = id;
533 }
534 }
535
536 chips = kmalloc_array(nr_chips, sizeof(struct chip), GFP_KERNEL);
537 if (!chips)
538 return -ENOMEM;
539
540 for (i = 0; i < nr_chips; i++) {
541 chips[i].id = chip[i];
542 chips[i].throttled = false;
735366fc
SB
543 cpumask_copy(&chips[i].mask, cpumask_of_node(chip[i]));
544 INIT_WORK(&chips[i].throttle, powernv_cpufreq_work_fn);
053819e0
SB
545 }
546
547 return 0;
548}
549
b3d627a5
VS
550static int __init powernv_cpufreq_init(void)
551{
552 int rc = 0;
553
6174bac8
VS
554 /* Don't probe on pseries (guest) platforms */
555 if (!firmware_has_feature(FW_FEATURE_OPALv3))
556 return -ENODEV;
557
b3d627a5
VS
558 /* Discover pstates from device tree and init */
559 rc = init_powernv_pstates();
560 if (rc) {
561 pr_info("powernv-cpufreq disabled. System does not support PState control\n");
562 return rc;
563 }
564
053819e0
SB
565 /* Populate chip info */
566 rc = init_chip_info();
567 if (rc)
568 return rc;
569
cf30af76 570 register_reboot_notifier(&powernv_cpufreq_reboot_nb);
cb166fa9 571 opal_message_notifier_register(OPAL_MSG_OCC, &powernv_cpufreq_opal_nb);
b3d627a5
VS
572 return cpufreq_register_driver(&powernv_cpufreq_driver);
573}
574module_init(powernv_cpufreq_init);
575
576static void __exit powernv_cpufreq_exit(void)
577{
cf30af76 578 unregister_reboot_notifier(&powernv_cpufreq_reboot_nb);
cb166fa9
SB
579 opal_message_notifier_unregister(OPAL_MSG_OCC,
580 &powernv_cpufreq_opal_nb);
b3d627a5
VS
581 cpufreq_unregister_driver(&powernv_cpufreq_driver);
582}
583module_exit(powernv_cpufreq_exit);
584
585MODULE_LICENSE("GPL");
586MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");
This page took 0.15424 seconds and 5 git commands to generate.