blob: a00bcc2cef09b35bc5455c531776fde940a77204 [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>
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +053031#include <linux/cpu.h>
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +053032#include <trace/events/power.h>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053033
34#include <asm/cputhreads.h>
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +053035#include <asm/firmware.h>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053036#include <asm/reg.h>
Srivatsa S. Bhatf3cae352014-04-16 11:35:38 +053037#include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +053038#include <asm/opal.h>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053039
40#define POWERNV_MAX_PSTATES 256
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +053041#define PMSR_PSAFE_ENABLE (1UL << 30)
42#define PMSR_SPR_EM_DISABLE (1UL << 31)
43#define PMSR_MAX(x) ((x >> 32) & 0xFF)
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053044
45static struct cpufreq_frequency_table powernv_freqs[POWERNV_MAX_PSTATES+1];
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +053046static bool rebooting, throttled, occ_reset;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053047
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +053048static const char * const throttle_reason[] = {
49 "No throttling",
50 "Power Cap",
51 "Processor Over Temperature",
52 "Power Supply Failure",
53 "Over Current",
54 "OCC Reset"
55};
56
Shilpasri G Bhat053819e2015-07-16 13:34:18 +053057static struct chip {
58 unsigned int id;
59 bool throttled;
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +053060 bool restore;
61 u8 throttle_reason;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +053062 cpumask_t mask;
63 struct work_struct throttle;
Shilpasri G Bhat053819e2015-07-16 13:34:18 +053064} *chips;
65
66static int nr_chips;
Michael Neuling3e5963b2016-03-21 22:24:52 +053067static DEFINE_PER_CPU(struct chip *, chip_info);
Shilpasri G Bhat053819e2015-07-16 13:34:18 +053068
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053069/*
70 * Note: The set of pstates consists of contiguous integers, the
71 * smallest of which is indicated by powernv_pstate_info.min, the
72 * largest of which is indicated by powernv_pstate_info.max.
73 *
74 * The nominal pstate is the highest non-turbo pstate in this
75 * platform. This is indicated by powernv_pstate_info.nominal.
76 */
77static struct powernv_pstate_info {
78 int min;
79 int max;
80 int nominal;
81 int nr_pstates;
82} powernv_pstate_info;
83
84/*
85 * Initialize the freq table based on data obtained
86 * from the firmware passed via device-tree
87 */
88static int init_powernv_pstates(void)
89{
90 struct device_node *power_mgt;
91 int i, pstate_min, pstate_max, pstate_nominal, nr_pstates = 0;
92 const __be32 *pstate_ids, *pstate_freqs;
93 u32 len_ids, len_freqs;
94
95 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
96 if (!power_mgt) {
97 pr_warn("power-mgt node not found\n");
98 return -ENODEV;
99 }
100
101 if (of_property_read_u32(power_mgt, "ibm,pstate-min", &pstate_min)) {
102 pr_warn("ibm,pstate-min node not found\n");
103 return -ENODEV;
104 }
105
106 if (of_property_read_u32(power_mgt, "ibm,pstate-max", &pstate_max)) {
107 pr_warn("ibm,pstate-max node not found\n");
108 return -ENODEV;
109 }
110
111 if (of_property_read_u32(power_mgt, "ibm,pstate-nominal",
112 &pstate_nominal)) {
113 pr_warn("ibm,pstate-nominal not found\n");
114 return -ENODEV;
115 }
116 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min,
117 pstate_nominal, pstate_max);
118
119 pstate_ids = of_get_property(power_mgt, "ibm,pstate-ids", &len_ids);
120 if (!pstate_ids) {
121 pr_warn("ibm,pstate-ids not found\n");
122 return -ENODEV;
123 }
124
125 pstate_freqs = of_get_property(power_mgt, "ibm,pstate-frequencies-mhz",
126 &len_freqs);
127 if (!pstate_freqs) {
128 pr_warn("ibm,pstate-frequencies-mhz not found\n");
129 return -ENODEV;
130 }
131
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530132 if (len_ids != len_freqs) {
133 pr_warn("Entries in ibm,pstate-ids and "
134 "ibm,pstate-frequencies-mhz does not match\n");
135 }
136
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530137 nr_pstates = min(len_ids, len_freqs) / sizeof(u32);
138 if (!nr_pstates) {
139 pr_warn("No PStates found\n");
140 return -ENODEV;
141 }
142
143 pr_debug("NR PStates %d\n", nr_pstates);
144 for (i = 0; i < nr_pstates; i++) {
145 u32 id = be32_to_cpu(pstate_ids[i]);
146 u32 freq = be32_to_cpu(pstate_freqs[i]);
147
148 pr_debug("PState id %d freq %d MHz\n", id, freq);
149 powernv_freqs[i].frequency = freq * 1000; /* kHz */
Gautham R. Shenoy0692c692014-04-01 12:43:27 +0530150 powernv_freqs[i].driver_data = id;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530151 }
152 /* End of list marker entry */
153 powernv_freqs[i].frequency = CPUFREQ_TABLE_END;
154
155 powernv_pstate_info.min = pstate_min;
156 powernv_pstate_info.max = pstate_max;
157 powernv_pstate_info.nominal = pstate_nominal;
158 powernv_pstate_info.nr_pstates = nr_pstates;
159
160 return 0;
161}
162
163/* Returns the CPU frequency corresponding to the pstate_id. */
164static unsigned int pstate_id_to_freq(int pstate_id)
165{
166 int i;
167
168 i = powernv_pstate_info.max - pstate_id;
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530169 if (i >= powernv_pstate_info.nr_pstates || i < 0) {
170 pr_warn("PState id %d outside of PState table, "
171 "reporting nominal id %d instead\n",
172 pstate_id, powernv_pstate_info.nominal);
173 i = powernv_pstate_info.max - powernv_pstate_info.nominal;
174 }
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530175
176 return powernv_freqs[i].frequency;
177}
178
179/*
180 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
181 * the firmware
182 */
183static ssize_t cpuinfo_nominal_freq_show(struct cpufreq_policy *policy,
184 char *buf)
185{
186 return sprintf(buf, "%u\n",
187 pstate_id_to_freq(powernv_pstate_info.nominal));
188}
189
190struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq =
191 __ATTR_RO(cpuinfo_nominal_freq);
192
193static struct freq_attr *powernv_cpu_freq_attr[] = {
194 &cpufreq_freq_attr_scaling_available_freqs,
195 &cpufreq_freq_attr_cpuinfo_nominal_freq,
196 NULL,
197};
198
199/* Helper routines */
200
201/* Access helpers to power mgt SPR */
202
203static inline unsigned long get_pmspr(unsigned long sprn)
204{
205 switch (sprn) {
206 case SPRN_PMCR:
207 return mfspr(SPRN_PMCR);
208
209 case SPRN_PMICR:
210 return mfspr(SPRN_PMICR);
211
212 case SPRN_PMSR:
213 return mfspr(SPRN_PMSR);
214 }
215 BUG();
216}
217
218static inline void set_pmspr(unsigned long sprn, unsigned long val)
219{
220 switch (sprn) {
221 case SPRN_PMCR:
222 mtspr(SPRN_PMCR, val);
223 return;
224
225 case SPRN_PMICR:
226 mtspr(SPRN_PMICR, val);
227 return;
228 }
229 BUG();
230}
231
232/*
233 * Use objects of this type to query/update
234 * pstates on a remote CPU via smp_call_function.
235 */
236struct powernv_smp_call_data {
237 unsigned int freq;
238 int pstate_id;
239};
240
241/*
242 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
243 *
244 * Called via smp_call_function.
245 *
246 * Note: The caller of the smp_call_function should pass an argument of
247 * the type 'struct powernv_smp_call_data *' along with this function.
248 *
249 * The current frequency on this CPU will be returned via
250 * ((struct powernv_smp_call_data *)arg)->freq;
251 */
252static void powernv_read_cpu_freq(void *arg)
253{
254 unsigned long pmspr_val;
255 s8 local_pstate_id;
256 struct powernv_smp_call_data *freq_data = arg;
257
258 pmspr_val = get_pmspr(SPRN_PMSR);
259
260 /*
261 * The local pstate id corresponds bits 48..55 in the PMSR.
262 * Note: Watch out for the sign!
263 */
264 local_pstate_id = (pmspr_val >> 48) & 0xFF;
265 freq_data->pstate_id = local_pstate_id;
266 freq_data->freq = pstate_id_to_freq(freq_data->pstate_id);
267
268 pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n",
269 raw_smp_processor_id(), pmspr_val, freq_data->pstate_id,
270 freq_data->freq);
271}
272
273/*
274 * powernv_cpufreq_get: Returns the CPU frequency as reported by the
275 * firmware for CPU 'cpu'. This value is reported through the sysfs
276 * file cpuinfo_cur_freq.
277 */
Brian Norris60d1ea42014-05-11 00:51:20 -0700278static unsigned int powernv_cpufreq_get(unsigned int cpu)
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530279{
280 struct powernv_smp_call_data freq_data;
281
282 smp_call_function_any(cpu_sibling_mask(cpu), powernv_read_cpu_freq,
283 &freq_data, 1);
284
285 return freq_data.freq;
286}
287
288/*
289 * set_pstate: Sets the pstate on this CPU.
290 *
291 * This is called via an smp_call_function.
292 *
293 * The caller must ensure that freq_data is of the type
294 * (struct powernv_smp_call_data *) and the pstate_id which needs to be set
295 * on this CPU should be present in freq_data->pstate_id.
296 */
297static void set_pstate(void *freq_data)
298{
299 unsigned long val;
300 unsigned long pstate_ul =
301 ((struct powernv_smp_call_data *) freq_data)->pstate_id;
302
303 val = get_pmspr(SPRN_PMCR);
304 val = val & 0x0000FFFFFFFFFFFFULL;
305
306 pstate_ul = pstate_ul & 0xFF;
307
308 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
309 val = val | (pstate_ul << 56) | (pstate_ul << 48);
310
311 pr_debug("Setting cpu %d pmcr to %016lX\n",
312 raw_smp_processor_id(), val);
313 set_pmspr(SPRN_PMCR, val);
314}
315
316/*
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200317 * get_nominal_index: Returns the index corresponding to the nominal
318 * pstate in the cpufreq table
319 */
320static inline unsigned int get_nominal_index(void)
321{
322 return powernv_pstate_info.max - powernv_pstate_info.nominal;
323}
324
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530325static void powernv_cpufreq_throttle_check(void *data)
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530326{
Michael Neuling3e5963b2016-03-21 22:24:52 +0530327 struct chip *chip;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530328 unsigned int cpu = smp_processor_id();
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530329 unsigned long pmsr;
Michael Neuling3e5963b2016-03-21 22:24:52 +0530330 int pmsr_pmax;
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530331
332 pmsr = get_pmspr(SPRN_PMSR);
Michael Neuling3e5963b2016-03-21 22:24:52 +0530333 chip = this_cpu_read(chip_info);
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530334
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530335 /* Check for Pmax Capping */
336 pmsr_pmax = (s8)PMSR_MAX(pmsr);
337 if (pmsr_pmax != powernv_pstate_info.max) {
Michael Neuling3e5963b2016-03-21 22:24:52 +0530338 if (chip->throttled)
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530339 goto next;
Michael Neuling3e5963b2016-03-21 22:24:52 +0530340 chip->throttled = true;
Shilpasri G Bhatd43b1b62015-09-14 14:01:47 +0530341 if (pmsr_pmax < powernv_pstate_info.nominal)
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530342 pr_warn_once("CPU %d on Chip %u has Pmax reduced below nominal frequency (%d < %d)\n",
Michael Neuling3e5963b2016-03-21 22:24:52 +0530343 cpu, chip->id, pmsr_pmax,
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530344 powernv_pstate_info.nominal);
Michael Neuling3e5963b2016-03-21 22:24:52 +0530345 trace_powernv_throttle(chip->id,
346 throttle_reason[chip->throttle_reason],
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530347 pmsr_pmax);
Michael Neuling3e5963b2016-03-21 22:24:52 +0530348 } else if (chip->throttled) {
349 chip->throttled = false;
350 trace_powernv_throttle(chip->id,
351 throttle_reason[chip->throttle_reason],
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530352 pmsr_pmax);
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530353 }
354
Shilpasri G Bhat3dd3ebe2015-07-16 13:34:22 +0530355 /* Check if Psafe_mode_active is set in PMSR. */
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530356next:
Shilpasri G Bhat3dd3ebe2015-07-16 13:34:22 +0530357 if (pmsr & PMSR_PSAFE_ENABLE) {
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530358 throttled = true;
359 pr_info("Pstate set to safe frequency\n");
360 }
361
362 /* Check if SPR_EM_DISABLE is set in PMSR */
363 if (pmsr & PMSR_SPR_EM_DISABLE) {
364 throttled = true;
365 pr_info("Frequency Control disabled from OS\n");
366 }
367
368 if (throttled) {
369 pr_info("PMSR = %16lx\n", pmsr);
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530370 pr_warn("CPU Frequency could be throttled\n");
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530371 }
372}
373
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200374/*
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530375 * powernv_cpufreq_target_index: Sets the frequency corresponding to
376 * the cpufreq table entry indexed by new_index on the cpus in the
377 * mask policy->cpus
378 */
379static int powernv_cpufreq_target_index(struct cpufreq_policy *policy,
380 unsigned int new_index)
381{
382 struct powernv_smp_call_data freq_data;
383
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200384 if (unlikely(rebooting) && new_index != get_nominal_index())
385 return 0;
386
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530387 if (!throttled)
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530388 powernv_cpufreq_throttle_check(NULL);
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530389
Gautham R. Shenoy0692c692014-04-01 12:43:27 +0530390 freq_data.pstate_id = powernv_freqs[new_index].driver_data;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530391
392 /*
393 * Use smp_call_function to send IPI and execute the
394 * mtspr on target CPU. We could do that without IPI
395 * if current CPU is within policy->cpus (core)
396 */
397 smp_call_function_any(policy->cpus, set_pstate, &freq_data, 1);
398
399 return 0;
400}
401
402static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy)
403{
404 int base, i;
405
406 base = cpu_first_thread_sibling(policy->cpu);
407
408 for (i = 0; i < threads_per_core; i++)
409 cpumask_set_cpu(base + i, policy->cpus);
410
411 return cpufreq_table_validate_and_show(policy, powernv_freqs);
412}
413
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200414static int powernv_cpufreq_reboot_notifier(struct notifier_block *nb,
415 unsigned long action, void *unused)
416{
417 int cpu;
418 struct cpufreq_policy cpu_policy;
419
420 rebooting = true;
421 for_each_online_cpu(cpu) {
422 cpufreq_get_policy(&cpu_policy, cpu);
423 powernv_cpufreq_target_index(&cpu_policy, get_nominal_index());
424 }
425
426 return NOTIFY_DONE;
427}
428
429static struct notifier_block powernv_cpufreq_reboot_nb = {
430 .notifier_call = powernv_cpufreq_reboot_notifier,
431};
432
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530433void powernv_cpufreq_work_fn(struct work_struct *work)
434{
435 struct chip *chip = container_of(work, struct chip, throttle);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530436 unsigned int cpu;
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530437 cpumask_t mask;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530438
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530439 get_online_cpus();
440 cpumask_and(&mask, &chip->mask, cpu_online_mask);
441 smp_call_function_any(&mask,
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530442 powernv_cpufreq_throttle_check, NULL, 0);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530443
444 if (!chip->restore)
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530445 goto out;
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530446
447 chip->restore = false;
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530448 for_each_cpu(cpu, &mask) {
449 int index;
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530450 struct cpufreq_policy policy;
451
452 cpufreq_get_policy(&policy, cpu);
453 cpufreq_frequency_table_target(&policy, policy.freq_table,
454 policy.cur,
455 CPUFREQ_RELATION_C, &index);
456 powernv_cpufreq_target_index(&policy, index);
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530457 cpumask_andnot(&mask, &mask, policy.cpus);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530458 }
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530459out:
460 put_online_cpus();
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530461}
462
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530463static int powernv_cpufreq_occ_msg(struct notifier_block *nb,
464 unsigned long msg_type, void *_msg)
465{
466 struct opal_msg *msg = _msg;
467 struct opal_occ_msg omsg;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530468 int i;
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530469
470 if (msg_type != OPAL_MSG_OCC)
471 return 0;
472
473 omsg.type = be64_to_cpu(msg->params[0]);
474
475 switch (omsg.type) {
476 case OCC_RESET:
477 occ_reset = true;
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530478 pr_info("OCC (On Chip Controller - enforces hard thermal/power limits) Resetting\n");
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530479 /*
480 * powernv_cpufreq_throttle_check() is called in
481 * target() callback which can detect the throttle state
482 * for governors like ondemand.
483 * But static governors will not call target() often thus
484 * report throttling here.
485 */
486 if (!throttled) {
487 throttled = true;
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530488 pr_warn("CPU frequency is throttled for duration\n");
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530489 }
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530490
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530491 break;
492 case OCC_LOAD:
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530493 pr_info("OCC Loading, CPU frequency is throttled until OCC is started\n");
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530494 break;
495 case OCC_THROTTLE:
496 omsg.chip = be64_to_cpu(msg->params[1]);
497 omsg.throttle_status = be64_to_cpu(msg->params[2]);
498
499 if (occ_reset) {
500 occ_reset = false;
501 throttled = false;
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530502 pr_info("OCC Active, CPU frequency is no longer throttled\n");
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530503
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530504 for (i = 0; i < nr_chips; i++) {
505 chips[i].restore = true;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530506 schedule_work(&chips[i].throttle);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530507 }
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530508
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530509 return 0;
510 }
511
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530512 for (i = 0; i < nr_chips; i++)
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530513 if (chips[i].id == omsg.chip)
514 break;
515
516 if (omsg.throttle_status >= 0 &&
517 omsg.throttle_status <= OCC_MAX_THROTTLE_STATUS)
518 chips[i].throttle_reason = omsg.throttle_status;
519
520 if (!omsg.throttle_status)
521 chips[i].restore = true;
522
523 schedule_work(&chips[i].throttle);
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530524 }
525 return 0;
526}
527
528static struct notifier_block powernv_cpufreq_opal_nb = {
529 .notifier_call = powernv_cpufreq_occ_msg,
530 .next = NULL,
531 .priority = 0,
532};
533
Preeti U Murthyb1203392014-09-29 15:47:53 +0200534static void powernv_cpufreq_stop_cpu(struct cpufreq_policy *policy)
535{
536 struct powernv_smp_call_data freq_data;
537
538 freq_data.pstate_id = powernv_pstate_info.min;
539 smp_call_function_single(policy->cpu, set_pstate, &freq_data, 1);
540}
541
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530542static struct cpufreq_driver powernv_cpufreq_driver = {
543 .name = "powernv-cpufreq",
544 .flags = CPUFREQ_CONST_LOOPS,
545 .init = powernv_cpufreq_cpu_init,
546 .verify = cpufreq_generic_frequency_table_verify,
547 .target_index = powernv_cpufreq_target_index,
548 .get = powernv_cpufreq_get,
Preeti U Murthyb1203392014-09-29 15:47:53 +0200549 .stop_cpu = powernv_cpufreq_stop_cpu,
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530550 .attr = powernv_cpu_freq_attr,
551};
552
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530553static int init_chip_info(void)
554{
555 unsigned int chip[256];
556 unsigned int cpu, i;
557 unsigned int prev_chip_id = UINT_MAX;
558
Michael Neuling3e5963b2016-03-21 22:24:52 +0530559 for_each_possible_cpu(cpu) {
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530560 unsigned int id = cpu_to_chip_id(cpu);
561
562 if (prev_chip_id != id) {
563 prev_chip_id = id;
564 chip[nr_chips++] = id;
565 }
566 }
567
Shilpasri G Bhatc89f2682016-02-03 01:11:41 +0530568 chips = kcalloc(nr_chips, sizeof(struct chip), GFP_KERNEL);
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530569 if (!chips)
Michael Neuling3e5963b2016-03-21 22:24:52 +0530570 return -ENOMEM;
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530571
572 for (i = 0; i < nr_chips; i++) {
573 chips[i].id = chip[i];
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530574 cpumask_copy(&chips[i].mask, cpumask_of_node(chip[i]));
575 INIT_WORK(&chips[i].throttle, powernv_cpufreq_work_fn);
Michael Neuling3e5963b2016-03-21 22:24:52 +0530576 for_each_cpu(cpu, &chips[i].mask)
577 per_cpu(chip_info, cpu) = &chips[i];
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530578 }
579
580 return 0;
581}
582
Shilpasri G Bhatc5e29ea2016-02-26 16:06:51 +0530583static inline void clean_chip_info(void)
584{
585 kfree(chips);
Shilpasri G Bhatc5e29ea2016-02-26 16:06:51 +0530586}
587
588static inline void unregister_all_notifiers(void)
589{
590 opal_message_notifier_unregister(OPAL_MSG_OCC,
591 &powernv_cpufreq_opal_nb);
592 unregister_reboot_notifier(&powernv_cpufreq_reboot_nb);
593}
594
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530595static int __init powernv_cpufreq_init(void)
596{
597 int rc = 0;
598
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530599 /* Don't probe on pseries (guest) platforms */
Stewart Smithe4d54f72015-12-09 17:18:20 +1100600 if (!firmware_has_feature(FW_FEATURE_OPAL))
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530601 return -ENODEV;
602
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530603 /* Discover pstates from device tree and init */
604 rc = init_powernv_pstates();
Shilpasri G Bhatc5e29ea2016-02-26 16:06:51 +0530605 if (rc)
606 goto out;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530607
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530608 /* Populate chip info */
609 rc = init_chip_info();
610 if (rc)
Shilpasri G Bhatc5e29ea2016-02-26 16:06:51 +0530611 goto out;
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530612
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200613 register_reboot_notifier(&powernv_cpufreq_reboot_nb);
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530614 opal_message_notifier_register(OPAL_MSG_OCC, &powernv_cpufreq_opal_nb);
Shilpasri G Bhatc5e29ea2016-02-26 16:06:51 +0530615
616 rc = cpufreq_register_driver(&powernv_cpufreq_driver);
617 if (!rc)
618 return 0;
619
620 pr_info("Failed to register the cpufreq driver (%d)\n", rc);
621 unregister_all_notifiers();
622 clean_chip_info();
623out:
624 pr_info("Platform driver disabled. System does not support PState control\n");
625 return rc;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530626}
627module_init(powernv_cpufreq_init);
628
629static void __exit powernv_cpufreq_exit(void)
630{
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530631 cpufreq_unregister_driver(&powernv_cpufreq_driver);
Shilpasri G Bhatc5e29ea2016-02-26 16:06:51 +0530632 unregister_all_notifiers();
633 clean_chip_info();
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530634}
635module_exit(powernv_cpufreq_exit);
636
637MODULE_LICENSE("GPL");
638MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");