blob: a271b0fbe8b91eda284b7b18e836285403fb0de9 [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>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053032
33#include <asm/cputhreads.h>
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +053034#include <asm/firmware.h>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053035#include <asm/reg.h>
Srivatsa S. Bhatf3cae352014-04-16 11:35:38 +053036#include <asm/smp.h> /* Required for cpu_sibling_mask() in UP configs */
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +053037#include <asm/opal.h>
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053038
39#define POWERNV_MAX_PSTATES 256
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +053040#define PMSR_PSAFE_ENABLE (1UL << 30)
41#define PMSR_SPR_EM_DISABLE (1UL << 31)
42#define PMSR_MAX(x) ((x >> 32) & 0xFF)
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053043
44static struct cpufreq_frequency_table powernv_freqs[POWERNV_MAX_PSTATES+1];
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +053045static bool rebooting, throttled, occ_reset;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053046
Shilpasri G Bhat053819e2015-07-16 13:34:18 +053047static struct chip {
48 unsigned int id;
49 bool throttled;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +053050 cpumask_t mask;
51 struct work_struct throttle;
Shilpasri G Bhat227942802015-07-16 13:34:23 +053052 bool restore;
Shilpasri G Bhat053819e2015-07-16 13:34:18 +053053} *chips;
54
55static int nr_chips;
56
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +053057/*
58 * Note: The set of pstates consists of contiguous integers, the
59 * smallest of which is indicated by powernv_pstate_info.min, the
60 * largest of which is indicated by powernv_pstate_info.max.
61 *
62 * The nominal pstate is the highest non-turbo pstate in this
63 * platform. This is indicated by powernv_pstate_info.nominal.
64 */
65static struct powernv_pstate_info {
66 int min;
67 int max;
68 int nominal;
69 int nr_pstates;
70} powernv_pstate_info;
71
72/*
73 * Initialize the freq table based on data obtained
74 * from the firmware passed via device-tree
75 */
76static int init_powernv_pstates(void)
77{
78 struct device_node *power_mgt;
79 int i, pstate_min, pstate_max, pstate_nominal, nr_pstates = 0;
80 const __be32 *pstate_ids, *pstate_freqs;
81 u32 len_ids, len_freqs;
82
83 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
84 if (!power_mgt) {
85 pr_warn("power-mgt node not found\n");
86 return -ENODEV;
87 }
88
89 if (of_property_read_u32(power_mgt, "ibm,pstate-min", &pstate_min)) {
90 pr_warn("ibm,pstate-min node not found\n");
91 return -ENODEV;
92 }
93
94 if (of_property_read_u32(power_mgt, "ibm,pstate-max", &pstate_max)) {
95 pr_warn("ibm,pstate-max node not found\n");
96 return -ENODEV;
97 }
98
99 if (of_property_read_u32(power_mgt, "ibm,pstate-nominal",
100 &pstate_nominal)) {
101 pr_warn("ibm,pstate-nominal not found\n");
102 return -ENODEV;
103 }
104 pr_info("cpufreq pstate min %d nominal %d max %d\n", pstate_min,
105 pstate_nominal, pstate_max);
106
107 pstate_ids = of_get_property(power_mgt, "ibm,pstate-ids", &len_ids);
108 if (!pstate_ids) {
109 pr_warn("ibm,pstate-ids not found\n");
110 return -ENODEV;
111 }
112
113 pstate_freqs = of_get_property(power_mgt, "ibm,pstate-frequencies-mhz",
114 &len_freqs);
115 if (!pstate_freqs) {
116 pr_warn("ibm,pstate-frequencies-mhz not found\n");
117 return -ENODEV;
118 }
119
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530120 if (len_ids != len_freqs) {
121 pr_warn("Entries in ibm,pstate-ids and "
122 "ibm,pstate-frequencies-mhz does not match\n");
123 }
124
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530125 nr_pstates = min(len_ids, len_freqs) / sizeof(u32);
126 if (!nr_pstates) {
127 pr_warn("No PStates found\n");
128 return -ENODEV;
129 }
130
131 pr_debug("NR PStates %d\n", nr_pstates);
132 for (i = 0; i < nr_pstates; i++) {
133 u32 id = be32_to_cpu(pstate_ids[i]);
134 u32 freq = be32_to_cpu(pstate_freqs[i]);
135
136 pr_debug("PState id %d freq %d MHz\n", id, freq);
137 powernv_freqs[i].frequency = freq * 1000; /* kHz */
Gautham R. Shenoy0692c692014-04-01 12:43:27 +0530138 powernv_freqs[i].driver_data = id;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530139 }
140 /* End of list marker entry */
141 powernv_freqs[i].frequency = CPUFREQ_TABLE_END;
142
143 powernv_pstate_info.min = pstate_min;
144 powernv_pstate_info.max = pstate_max;
145 powernv_pstate_info.nominal = pstate_nominal;
146 powernv_pstate_info.nr_pstates = nr_pstates;
147
148 return 0;
149}
150
151/* Returns the CPU frequency corresponding to the pstate_id. */
152static unsigned int pstate_id_to_freq(int pstate_id)
153{
154 int i;
155
156 i = powernv_pstate_info.max - pstate_id;
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530157 if (i >= powernv_pstate_info.nr_pstates || i < 0) {
158 pr_warn("PState id %d outside of PState table, "
159 "reporting nominal id %d instead\n",
160 pstate_id, powernv_pstate_info.nominal);
161 i = powernv_pstate_info.max - powernv_pstate_info.nominal;
162 }
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530163
164 return powernv_freqs[i].frequency;
165}
166
167/*
168 * cpuinfo_nominal_freq_show - Show the nominal CPU frequency as indicated by
169 * the firmware
170 */
171static ssize_t cpuinfo_nominal_freq_show(struct cpufreq_policy *policy,
172 char *buf)
173{
174 return sprintf(buf, "%u\n",
175 pstate_id_to_freq(powernv_pstate_info.nominal));
176}
177
178struct freq_attr cpufreq_freq_attr_cpuinfo_nominal_freq =
179 __ATTR_RO(cpuinfo_nominal_freq);
180
181static struct freq_attr *powernv_cpu_freq_attr[] = {
182 &cpufreq_freq_attr_scaling_available_freqs,
183 &cpufreq_freq_attr_cpuinfo_nominal_freq,
184 NULL,
185};
186
187/* Helper routines */
188
189/* Access helpers to power mgt SPR */
190
191static inline unsigned long get_pmspr(unsigned long sprn)
192{
193 switch (sprn) {
194 case SPRN_PMCR:
195 return mfspr(SPRN_PMCR);
196
197 case SPRN_PMICR:
198 return mfspr(SPRN_PMICR);
199
200 case SPRN_PMSR:
201 return mfspr(SPRN_PMSR);
202 }
203 BUG();
204}
205
206static inline void set_pmspr(unsigned long sprn, unsigned long val)
207{
208 switch (sprn) {
209 case SPRN_PMCR:
210 mtspr(SPRN_PMCR, val);
211 return;
212
213 case SPRN_PMICR:
214 mtspr(SPRN_PMICR, val);
215 return;
216 }
217 BUG();
218}
219
220/*
221 * Use objects of this type to query/update
222 * pstates on a remote CPU via smp_call_function.
223 */
224struct powernv_smp_call_data {
225 unsigned int freq;
226 int pstate_id;
227};
228
229/*
230 * powernv_read_cpu_freq: Reads the current frequency on this CPU.
231 *
232 * Called via smp_call_function.
233 *
234 * Note: The caller of the smp_call_function should pass an argument of
235 * the type 'struct powernv_smp_call_data *' along with this function.
236 *
237 * The current frequency on this CPU will be returned via
238 * ((struct powernv_smp_call_data *)arg)->freq;
239 */
240static void powernv_read_cpu_freq(void *arg)
241{
242 unsigned long pmspr_val;
243 s8 local_pstate_id;
244 struct powernv_smp_call_data *freq_data = arg;
245
246 pmspr_val = get_pmspr(SPRN_PMSR);
247
248 /*
249 * The local pstate id corresponds bits 48..55 in the PMSR.
250 * Note: Watch out for the sign!
251 */
252 local_pstate_id = (pmspr_val >> 48) & 0xFF;
253 freq_data->pstate_id = local_pstate_id;
254 freq_data->freq = pstate_id_to_freq(freq_data->pstate_id);
255
256 pr_debug("cpu %d pmsr %016lX pstate_id %d frequency %d kHz\n",
257 raw_smp_processor_id(), pmspr_val, freq_data->pstate_id,
258 freq_data->freq);
259}
260
261/*
262 * powernv_cpufreq_get: Returns the CPU frequency as reported by the
263 * firmware for CPU 'cpu'. This value is reported through the sysfs
264 * file cpuinfo_cur_freq.
265 */
Brian Norris60d1ea42014-05-11 00:51:20 -0700266static unsigned int powernv_cpufreq_get(unsigned int cpu)
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530267{
268 struct powernv_smp_call_data freq_data;
269
270 smp_call_function_any(cpu_sibling_mask(cpu), powernv_read_cpu_freq,
271 &freq_data, 1);
272
273 return freq_data.freq;
274}
275
276/*
277 * set_pstate: Sets the pstate on this CPU.
278 *
279 * This is called via an smp_call_function.
280 *
281 * The caller must ensure that freq_data is of the type
282 * (struct powernv_smp_call_data *) and the pstate_id which needs to be set
283 * on this CPU should be present in freq_data->pstate_id.
284 */
285static void set_pstate(void *freq_data)
286{
287 unsigned long val;
288 unsigned long pstate_ul =
289 ((struct powernv_smp_call_data *) freq_data)->pstate_id;
290
291 val = get_pmspr(SPRN_PMCR);
292 val = val & 0x0000FFFFFFFFFFFFULL;
293
294 pstate_ul = pstate_ul & 0xFF;
295
296 /* Set both global(bits 56..63) and local(bits 48..55) PStates */
297 val = val | (pstate_ul << 56) | (pstate_ul << 48);
298
299 pr_debug("Setting cpu %d pmcr to %016lX\n",
300 raw_smp_processor_id(), val);
301 set_pmspr(SPRN_PMCR, val);
302}
303
304/*
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200305 * get_nominal_index: Returns the index corresponding to the nominal
306 * pstate in the cpufreq table
307 */
308static inline unsigned int get_nominal_index(void)
309{
310 return powernv_pstate_info.max - powernv_pstate_info.nominal;
311}
312
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530313static void powernv_cpufreq_throttle_check(void *data)
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530314{
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530315 unsigned int cpu = smp_processor_id();
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530316 unsigned long pmsr;
Shilpasri G Bhat3dd3ebe2015-07-16 13:34:22 +0530317 int pmsr_pmax, i;
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530318
319 pmsr = get_pmspr(SPRN_PMSR);
320
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530321 for (i = 0; i < nr_chips; i++)
322 if (chips[i].id == cpu_to_chip_id(cpu))
323 break;
324
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530325 /* Check for Pmax Capping */
326 pmsr_pmax = (s8)PMSR_MAX(pmsr);
327 if (pmsr_pmax != powernv_pstate_info.max) {
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530328 if (chips[i].throttled)
329 goto next;
330 chips[i].throttled = true;
Shilpasri G Bhatd43b1b62015-09-14 14:01:47 +0530331 if (pmsr_pmax < powernv_pstate_info.nominal)
332 pr_crit("CPU %d on Chip %u has Pmax reduced below nominal frequency (%d < %d)\n",
333 cpu, chips[i].id, pmsr_pmax,
334 powernv_pstate_info.nominal);
335 else
336 pr_info("CPU %d on Chip %u has Pmax reduced below turbo frequency (%d < %d)\n",
337 cpu, chips[i].id, pmsr_pmax,
338 powernv_pstate_info.max);
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530339 } else if (chips[i].throttled) {
340 chips[i].throttled = false;
341 pr_info("CPU %d on Chip %u has Pmax restored to %d\n", cpu,
342 chips[i].id, pmsr_pmax);
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530343 }
344
Shilpasri G Bhat3dd3ebe2015-07-16 13:34:22 +0530345 /* Check if Psafe_mode_active is set in PMSR. */
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530346next:
Shilpasri G Bhat3dd3ebe2015-07-16 13:34:22 +0530347 if (pmsr & PMSR_PSAFE_ENABLE) {
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530348 throttled = true;
349 pr_info("Pstate set to safe frequency\n");
350 }
351
352 /* Check if SPR_EM_DISABLE is set in PMSR */
353 if (pmsr & PMSR_SPR_EM_DISABLE) {
354 throttled = true;
355 pr_info("Frequency Control disabled from OS\n");
356 }
357
358 if (throttled) {
359 pr_info("PMSR = %16lx\n", pmsr);
360 pr_crit("CPU Frequency could be throttled\n");
361 }
362}
363
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200364/*
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530365 * powernv_cpufreq_target_index: Sets the frequency corresponding to
366 * the cpufreq table entry indexed by new_index on the cpus in the
367 * mask policy->cpus
368 */
369static int powernv_cpufreq_target_index(struct cpufreq_policy *policy,
370 unsigned int new_index)
371{
372 struct powernv_smp_call_data freq_data;
373
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200374 if (unlikely(rebooting) && new_index != get_nominal_index())
375 return 0;
376
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530377 if (!throttled)
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530378 powernv_cpufreq_throttle_check(NULL);
Shilpasri G Bhat09a972d2015-04-01 15:16:34 +0530379
Gautham R. Shenoy0692c692014-04-01 12:43:27 +0530380 freq_data.pstate_id = powernv_freqs[new_index].driver_data;
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530381
382 /*
383 * Use smp_call_function to send IPI and execute the
384 * mtspr on target CPU. We could do that without IPI
385 * if current CPU is within policy->cpus (core)
386 */
387 smp_call_function_any(policy->cpus, set_pstate, &freq_data, 1);
388
389 return 0;
390}
391
392static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy)
393{
394 int base, i;
395
396 base = cpu_first_thread_sibling(policy->cpu);
397
398 for (i = 0; i < threads_per_core; i++)
399 cpumask_set_cpu(base + i, policy->cpus);
400
401 return cpufreq_table_validate_and_show(policy, powernv_freqs);
402}
403
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200404static int powernv_cpufreq_reboot_notifier(struct notifier_block *nb,
405 unsigned long action, void *unused)
406{
407 int cpu;
408 struct cpufreq_policy cpu_policy;
409
410 rebooting = true;
411 for_each_online_cpu(cpu) {
412 cpufreq_get_policy(&cpu_policy, cpu);
413 powernv_cpufreq_target_index(&cpu_policy, get_nominal_index());
414 }
415
416 return NOTIFY_DONE;
417}
418
419static struct notifier_block powernv_cpufreq_reboot_nb = {
420 .notifier_call = powernv_cpufreq_reboot_notifier,
421};
422
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530423void powernv_cpufreq_work_fn(struct work_struct *work)
424{
425 struct chip *chip = container_of(work, struct chip, throttle);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530426 unsigned int cpu;
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530427 cpumask_t mask;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530428
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530429 get_online_cpus();
430 cpumask_and(&mask, &chip->mask, cpu_online_mask);
431 smp_call_function_any(&mask,
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530432 powernv_cpufreq_throttle_check, NULL, 0);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530433
434 if (!chip->restore)
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530435 goto out;
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530436
437 chip->restore = false;
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530438 for_each_cpu(cpu, &mask) {
439 int index;
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530440 struct cpufreq_policy policy;
441
442 cpufreq_get_policy(&policy, cpu);
443 cpufreq_frequency_table_target(&policy, policy.freq_table,
444 policy.cur,
445 CPUFREQ_RELATION_C, &index);
446 powernv_cpufreq_target_index(&policy, index);
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530447 cpumask_andnot(&mask, &mask, policy.cpus);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530448 }
Shilpasri G Bhat6d167a42016-02-03 01:11:38 +0530449out:
450 put_online_cpus();
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530451}
452
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530453static char throttle_reason[][30] = {
454 "No throttling",
455 "Power Cap",
456 "Processor Over Temperature",
457 "Power Supply Failure",
458 "Over Current",
459 "OCC Reset"
460 };
461
462static int powernv_cpufreq_occ_msg(struct notifier_block *nb,
463 unsigned long msg_type, void *_msg)
464{
465 struct opal_msg *msg = _msg;
466 struct opal_occ_msg omsg;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530467 int i;
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530468
469 if (msg_type != OPAL_MSG_OCC)
470 return 0;
471
472 omsg.type = be64_to_cpu(msg->params[0]);
473
474 switch (omsg.type) {
475 case OCC_RESET:
476 occ_reset = true;
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530477 pr_info("OCC (On Chip Controller - enforces hard thermal/power limits) Resetting\n");
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530478 /*
479 * powernv_cpufreq_throttle_check() is called in
480 * target() callback which can detect the throttle state
481 * for governors like ondemand.
482 * But static governors will not call target() often thus
483 * report throttling here.
484 */
485 if (!throttled) {
486 throttled = true;
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530487 pr_crit("CPU frequency is throttled for duration\n");
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530488 }
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530489
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530490 break;
491 case OCC_LOAD:
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530492 pr_info("OCC Loading, CPU frequency is throttled until OCC is started\n");
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530493 break;
494 case OCC_THROTTLE:
495 omsg.chip = be64_to_cpu(msg->params[1]);
496 omsg.throttle_status = be64_to_cpu(msg->params[2]);
497
498 if (occ_reset) {
499 occ_reset = false;
500 throttled = false;
Shilpasri G Bhat309d0632015-08-27 14:41:44 +0530501 pr_info("OCC Active, CPU frequency is no longer throttled\n");
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530502
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530503 for (i = 0; i < nr_chips; i++) {
504 chips[i].restore = true;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530505 schedule_work(&chips[i].throttle);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530506 }
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530507
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530508 return 0;
509 }
510
511 if (omsg.throttle_status &&
512 omsg.throttle_status <= OCC_MAX_THROTTLE_STATUS)
513 pr_info("OCC: Chip %u Pmax reduced due to %s\n",
514 (unsigned int)omsg.chip,
515 throttle_reason[omsg.throttle_status]);
516 else if (!omsg.throttle_status)
517 pr_info("OCC: Chip %u %s\n", (unsigned int)omsg.chip,
518 throttle_reason[omsg.throttle_status]);
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530519 else
520 return 0;
521
522 for (i = 0; i < nr_chips; i++)
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530523 if (chips[i].id == omsg.chip) {
524 if (!omsg.throttle_status)
525 chips[i].restore = true;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530526 schedule_work(&chips[i].throttle);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530527 }
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530528 }
529 return 0;
530}
531
532static struct notifier_block powernv_cpufreq_opal_nb = {
533 .notifier_call = powernv_cpufreq_occ_msg,
534 .next = NULL,
535 .priority = 0,
536};
537
Preeti U Murthyb1203392014-09-29 15:47:53 +0200538static void powernv_cpufreq_stop_cpu(struct cpufreq_policy *policy)
539{
540 struct powernv_smp_call_data freq_data;
541
542 freq_data.pstate_id = powernv_pstate_info.min;
543 smp_call_function_single(policy->cpu, set_pstate, &freq_data, 1);
544}
545
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530546static struct cpufreq_driver powernv_cpufreq_driver = {
547 .name = "powernv-cpufreq",
548 .flags = CPUFREQ_CONST_LOOPS,
549 .init = powernv_cpufreq_cpu_init,
550 .verify = cpufreq_generic_frequency_table_verify,
551 .target_index = powernv_cpufreq_target_index,
552 .get = powernv_cpufreq_get,
Preeti U Murthyb1203392014-09-29 15:47:53 +0200553 .stop_cpu = powernv_cpufreq_stop_cpu,
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530554 .attr = powernv_cpu_freq_attr,
555};
556
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530557static int init_chip_info(void)
558{
559 unsigned int chip[256];
560 unsigned int cpu, i;
561 unsigned int prev_chip_id = UINT_MAX;
562
563 for_each_possible_cpu(cpu) {
564 unsigned int id = cpu_to_chip_id(cpu);
565
566 if (prev_chip_id != id) {
567 prev_chip_id = id;
568 chip[nr_chips++] = id;
569 }
570 }
571
572 chips = kmalloc_array(nr_chips, sizeof(struct chip), GFP_KERNEL);
573 if (!chips)
574 return -ENOMEM;
575
576 for (i = 0; i < nr_chips; i++) {
577 chips[i].id = chip[i];
578 chips[i].throttled = false;
Shilpasri G Bhat735366f2015-07-16 13:34:21 +0530579 cpumask_copy(&chips[i].mask, cpumask_of_node(chip[i]));
580 INIT_WORK(&chips[i].throttle, powernv_cpufreq_work_fn);
Shilpasri G Bhat227942802015-07-16 13:34:23 +0530581 chips[i].restore = false;
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530582 }
583
584 return 0;
585}
586
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530587static int __init powernv_cpufreq_init(void)
588{
589 int rc = 0;
590
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530591 /* Don't probe on pseries (guest) platforms */
Stewart Smithe4d54f72015-12-09 17:18:20 +1100592 if (!firmware_has_feature(FW_FEATURE_OPAL))
Vaidyanathan Srinivasan6174bac2014-08-03 14:54:05 +0530593 return -ENODEV;
594
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530595 /* Discover pstates from device tree and init */
596 rc = init_powernv_pstates();
597 if (rc) {
598 pr_info("powernv-cpufreq disabled. System does not support PState control\n");
599 return rc;
600 }
601
Shilpasri G Bhat053819e2015-07-16 13:34:18 +0530602 /* Populate chip info */
603 rc = init_chip_info();
604 if (rc)
605 return rc;
606
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200607 register_reboot_notifier(&powernv_cpufreq_reboot_nb);
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530608 opal_message_notifier_register(OPAL_MSG_OCC, &powernv_cpufreq_opal_nb);
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530609 return cpufreq_register_driver(&powernv_cpufreq_driver);
610}
611module_init(powernv_cpufreq_init);
612
613static void __exit powernv_cpufreq_exit(void)
614{
Shilpasri G Bhatcf30af762014-09-29 15:49:11 +0200615 unregister_reboot_notifier(&powernv_cpufreq_reboot_nb);
Shilpasri G Bhatcb166fa2015-07-16 13:34:20 +0530616 opal_message_notifier_unregister(OPAL_MSG_OCC,
617 &powernv_cpufreq_opal_nb);
Shilpasri G Bhat86622cb2016-02-03 01:11:37 +0530618 kfree(chips);
Vaidyanathan Srinivasanb3d627a2014-04-01 12:43:26 +0530619 cpufreq_unregister_driver(&powernv_cpufreq_driver);
620}
621module_exit(powernv_cpufreq_exit);
622
623MODULE_LICENSE("GPL");
624MODULE_AUTHOR("Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com>");