blob: a64be578dab2e30d75d06989385f9fd03c55ba03 [file] [log] [blame]
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +05301/*
2 * cpuidle-powernv - idle state cpuidle driver.
3 * Adapted from drivers/cpuidle/cpuidle-pseries
4 *
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/moduleparam.h>
11#include <linux/cpuidle.h>
12#include <linux/cpu.h>
13#include <linux/notifier.h>
Preeti U Murthy0d948732014-02-26 05:39:06 +053014#include <linux/clockchips.h>
Preeti U Murthy08888392014-02-26 05:39:20 +053015#include <linux/of.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053016
17#include <asm/machdep.h>
18#include <asm/firmware.h>
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050019#include <asm/runlatch.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053020
Preeti U Murthy08888392014-02-26 05:39:20 +053021/* Flags and constants used in PowerNV platform */
22
23#define MAX_POWERNV_IDLE_STATES 8
24#define IDLE_USE_INST_NAP 0x00010000 /* Use nap instruction */
25#define IDLE_USE_INST_SLEEP 0x00020000 /* Use sleep instruction */
26
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053027struct cpuidle_driver powernv_idle_driver = {
28 .name = "powernv_idle",
29 .owner = THIS_MODULE,
30};
31
32static int max_idle_state;
33static struct cpuidle_state *cpuidle_state_table;
34
35static int snooze_loop(struct cpuidle_device *dev,
36 struct cpuidle_driver *drv,
37 int index)
38{
39 local_irq_enable();
40 set_thread_flag(TIF_POLLING_NRFLAG);
41
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050042 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053043 while (!need_resched()) {
44 HMT_low();
45 HMT_very_low();
46 }
47
48 HMT_medium();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050049 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053050 clear_thread_flag(TIF_POLLING_NRFLAG);
51 smp_mb();
52 return index;
53}
54
55static int nap_loop(struct cpuidle_device *dev,
56 struct cpuidle_driver *drv,
57 int index)
58{
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050059 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053060 power7_idle();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050061 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053062 return index;
63}
64
Preeti U Murthy0d948732014-02-26 05:39:06 +053065static int fastsleep_loop(struct cpuidle_device *dev,
66 struct cpuidle_driver *drv,
67 int index)
68{
69 unsigned long old_lpcr = mfspr(SPRN_LPCR);
70 unsigned long new_lpcr;
71
72 if (unlikely(system_state < SYSTEM_RUNNING))
73 return index;
74
75 new_lpcr = old_lpcr;
Michael Neuling9b6a68d2014-06-11 15:59:27 +100076 /* Do not exit powersave upon decrementer as we've setup the timer
77 * offload.
Preeti U Murthy0d948732014-02-26 05:39:06 +053078 */
Michael Neuling9b6a68d2014-06-11 15:59:27 +100079 new_lpcr &= ~LPCR_PECE1;
Preeti U Murthy0d948732014-02-26 05:39:06 +053080
81 mtspr(SPRN_LPCR, new_lpcr);
82 power7_sleep();
83
84 mtspr(SPRN_LPCR, old_lpcr);
85
86 return index;
87}
88
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053089/*
90 * States for dedicated partition case.
91 */
Preeti U Murthy08888392014-02-26 05:39:20 +053092static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053093 { /* Snooze */
94 .name = "snooze",
95 .desc = "snooze",
96 .flags = CPUIDLE_FLAG_TIME_VALID,
97 .exit_latency = 0,
98 .target_residency = 0,
99 .enter = &snooze_loop },
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530100};
101
102static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
103 unsigned long action, void *hcpu)
104{
105 int hotcpu = (unsigned long)hcpu;
106 struct cpuidle_device *dev =
107 per_cpu(cpuidle_devices, hotcpu);
108
109 if (dev && cpuidle_get_driver()) {
110 switch (action) {
111 case CPU_ONLINE:
112 case CPU_ONLINE_FROZEN:
113 cpuidle_pause_and_lock();
114 cpuidle_enable_device(dev);
115 cpuidle_resume_and_unlock();
116 break;
117
118 case CPU_DEAD:
119 case CPU_DEAD_FROZEN:
120 cpuidle_pause_and_lock();
121 cpuidle_disable_device(dev);
122 cpuidle_resume_and_unlock();
123 break;
124
125 default:
126 return NOTIFY_DONE;
127 }
128 }
129 return NOTIFY_OK;
130}
131
132static struct notifier_block setup_hotplug_notifier = {
133 .notifier_call = powernv_cpuidle_add_cpu_notifier,
134};
135
136/*
137 * powernv_cpuidle_driver_init()
138 */
139static int powernv_cpuidle_driver_init(void)
140{
141 int idle_state;
142 struct cpuidle_driver *drv = &powernv_idle_driver;
143
144 drv->state_count = 0;
145
146 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
147 /* Is the state not enabled? */
148 if (cpuidle_state_table[idle_state].enter == NULL)
149 continue;
150
151 drv->states[drv->state_count] = /* structure copy */
152 cpuidle_state_table[idle_state];
153
154 drv->state_count += 1;
155 }
156
157 return 0;
158}
159
Preeti U Murthy08888392014-02-26 05:39:20 +0530160static int powernv_add_idle_states(void)
161{
162 struct device_node *power_mgt;
Preeti U Murthy08888392014-02-26 05:39:20 +0530163 int nr_idle_states = 1; /* Snooze */
164 int dt_idle_states;
Vaidyanathan Srinivasan95707d82014-08-03 13:23:08 +0530165 const __be32 *idle_state_flags;
166 u32 len_flags, flags;
Preeti U Murthy08888392014-02-26 05:39:20 +0530167 int i;
168
169 /* Currently we have snooze statically defined */
170
171 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
172 if (!power_mgt) {
173 pr_warn("opal: PowerMgmt Node not found\n");
174 return nr_idle_states;
175 }
176
Vaidyanathan Srinivasan95707d82014-08-03 13:23:08 +0530177 idle_state_flags = of_get_property(power_mgt, "ibm,cpu-idle-state-flags", &len_flags);
178 if (!idle_state_flags) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530179 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
180 return nr_idle_states;
181 }
182
Vaidyanathan Srinivasan95707d82014-08-03 13:23:08 +0530183 dt_idle_states = len_flags / sizeof(u32);
Preeti U Murthy08888392014-02-26 05:39:20 +0530184
185 for (i = 0; i < dt_idle_states; i++) {
186
Vaidyanathan Srinivasan95707d82014-08-03 13:23:08 +0530187 flags = be32_to_cpu(idle_state_flags[i]);
188 if (flags & IDLE_USE_INST_NAP) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530189 /* Add NAP state */
190 strcpy(powernv_states[nr_idle_states].name, "Nap");
191 strcpy(powernv_states[nr_idle_states].desc, "Nap");
192 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIME_VALID;
193 powernv_states[nr_idle_states].exit_latency = 10;
194 powernv_states[nr_idle_states].target_residency = 100;
195 powernv_states[nr_idle_states].enter = &nap_loop;
196 nr_idle_states++;
197 }
198
Vaidyanathan Srinivasan95707d82014-08-03 13:23:08 +0530199 if (flags & IDLE_USE_INST_SLEEP) {
Preeti U Murthy08888392014-02-26 05:39:20 +0530200 /* Add FASTSLEEP state */
201 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
202 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
203 powernv_states[nr_idle_states].flags =
204 CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
205 powernv_states[nr_idle_states].exit_latency = 300;
206 powernv_states[nr_idle_states].target_residency = 1000000;
207 powernv_states[nr_idle_states].enter = &fastsleep_loop;
208 nr_idle_states++;
209 }
210 }
211
212 return nr_idle_states;
213}
214
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530215/*
216 * powernv_idle_probe()
217 * Choose state table for shared versus dedicated partition
218 */
219static int powernv_idle_probe(void)
220{
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530221 if (cpuidle_disable != IDLE_NO_OVERRIDE)
222 return -ENODEV;
223
224 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
225 cpuidle_state_table = powernv_states;
Preeti U Murthy08888392014-02-26 05:39:20 +0530226 /* Device tree can indicate more idle states */
227 max_idle_state = powernv_add_idle_states();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530228 } else
229 return -ENODEV;
230
231 return 0;
232}
233
234static int __init powernv_processor_idle_init(void)
235{
236 int retval;
237
238 retval = powernv_idle_probe();
239 if (retval)
240 return retval;
241
242 powernv_cpuidle_driver_init();
243 retval = cpuidle_register(&powernv_idle_driver, NULL);
244 if (retval) {
245 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
246 return retval;
247 }
248
249 register_cpu_notifier(&setup_hotplug_notifier);
250 printk(KERN_DEBUG "powernv_idle_driver registered\n");
251 return 0;
252}
253
254device_initcall(powernv_processor_idle_init);