powerpc/powernv: Enable Offline CPUs to enter deep idle states
[deliverable/linux.git] / drivers / cpuidle / cpuidle-powernv.c
CommitLineData
2c2e6ecf
DD
1/*
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>
0d948730 14#include <linux/clockchips.h>
0888839c 15#include <linux/of.h>
2c2e6ecf
DD
16
17#include <asm/machdep.h>
18#include <asm/firmware.h>
8eb8ac89 19#include <asm/opal.h>
591ac0cb 20#include <asm/runlatch.h>
2c2e6ecf 21
0888839c 22#define MAX_POWERNV_IDLE_STATES 8
0888839c 23
2c2e6ecf
DD
24struct cpuidle_driver powernv_idle_driver = {
25 .name = "powernv_idle",
26 .owner = THIS_MODULE,
27};
28
29static int max_idle_state;
30static struct cpuidle_state *cpuidle_state_table;
31
32static int snooze_loop(struct cpuidle_device *dev,
33 struct cpuidle_driver *drv,
34 int index)
35{
36 local_irq_enable();
37 set_thread_flag(TIF_POLLING_NRFLAG);
38
591ac0cb 39 ppc64_runlatch_off();
2c2e6ecf
DD
40 while (!need_resched()) {
41 HMT_low();
42 HMT_very_low();
43 }
44
45 HMT_medium();
591ac0cb 46 ppc64_runlatch_on();
2c2e6ecf
DD
47 clear_thread_flag(TIF_POLLING_NRFLAG);
48 smp_mb();
49 return index;
50}
51
52static int nap_loop(struct cpuidle_device *dev,
53 struct cpuidle_driver *drv,
54 int index)
55{
591ac0cb 56 ppc64_runlatch_off();
2c2e6ecf 57 power7_idle();
591ac0cb 58 ppc64_runlatch_on();
2c2e6ecf
DD
59 return index;
60}
61
0d948730
PM
62static int fastsleep_loop(struct cpuidle_device *dev,
63 struct cpuidle_driver *drv,
64 int index)
65{
66 unsigned long old_lpcr = mfspr(SPRN_LPCR);
67 unsigned long new_lpcr;
68
69 if (unlikely(system_state < SYSTEM_RUNNING))
70 return index;
71
72 new_lpcr = old_lpcr;
9b6a68d9
MN
73 /* Do not exit powersave upon decrementer as we've setup the timer
74 * offload.
0d948730 75 */
9b6a68d9 76 new_lpcr &= ~LPCR_PECE1;
0d948730
PM
77
78 mtspr(SPRN_LPCR, new_lpcr);
79 power7_sleep();
80
81 mtspr(SPRN_LPCR, old_lpcr);
82
83 return index;
84}
85
2c2e6ecf
DD
86/*
87 * States for dedicated partition case.
88 */
0888839c 89static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
2c2e6ecf
DD
90 { /* Snooze */
91 .name = "snooze",
92 .desc = "snooze",
93 .flags = CPUIDLE_FLAG_TIME_VALID,
94 .exit_latency = 0,
95 .target_residency = 0,
96 .enter = &snooze_loop },
2c2e6ecf
DD
97};
98
99static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
100 unsigned long action, void *hcpu)
101{
102 int hotcpu = (unsigned long)hcpu;
103 struct cpuidle_device *dev =
104 per_cpu(cpuidle_devices, hotcpu);
105
106 if (dev && cpuidle_get_driver()) {
107 switch (action) {
108 case CPU_ONLINE:
109 case CPU_ONLINE_FROZEN:
110 cpuidle_pause_and_lock();
111 cpuidle_enable_device(dev);
112 cpuidle_resume_and_unlock();
113 break;
114
115 case CPU_DEAD:
116 case CPU_DEAD_FROZEN:
117 cpuidle_pause_and_lock();
118 cpuidle_disable_device(dev);
119 cpuidle_resume_and_unlock();
120 break;
121
122 default:
123 return NOTIFY_DONE;
124 }
125 }
126 return NOTIFY_OK;
127}
128
129static struct notifier_block setup_hotplug_notifier = {
130 .notifier_call = powernv_cpuidle_add_cpu_notifier,
131};
132
133/*
134 * powernv_cpuidle_driver_init()
135 */
136static int powernv_cpuidle_driver_init(void)
137{
138 int idle_state;
139 struct cpuidle_driver *drv = &powernv_idle_driver;
140
141 drv->state_count = 0;
142
143 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
144 /* Is the state not enabled? */
145 if (cpuidle_state_table[idle_state].enter == NULL)
146 continue;
147
148 drv->states[drv->state_count] = /* structure copy */
149 cpuidle_state_table[idle_state];
150
151 drv->state_count += 1;
152 }
153
154 return 0;
155}
156
0888839c
PM
157static int powernv_add_idle_states(void)
158{
159 struct device_node *power_mgt;
0888839c
PM
160 int nr_idle_states = 1; /* Snooze */
161 int dt_idle_states;
95707d85 162 const __be32 *idle_state_flags;
74aa51b5
PM
163 const __be32 *idle_state_latency;
164 u32 len_flags, flags, latency_ns;
0888839c
PM
165 int i;
166
167 /* Currently we have snooze statically defined */
168
169 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
170 if (!power_mgt) {
171 pr_warn("opal: PowerMgmt Node not found\n");
172 return nr_idle_states;
173 }
174
95707d85
VS
175 idle_state_flags = of_get_property(power_mgt, "ibm,cpu-idle-state-flags", &len_flags);
176 if (!idle_state_flags) {
0888839c
PM
177 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
178 return nr_idle_states;
179 }
180
74aa51b5
PM
181 idle_state_latency = of_get_property(power_mgt,
182 "ibm,cpu-idle-state-latencies-ns", NULL);
183 if (!idle_state_latency) {
184 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-latencies-ns\n");
185 return nr_idle_states;
186 }
187
95707d85 188 dt_idle_states = len_flags / sizeof(u32);
0888839c
PM
189
190 for (i = 0; i < dt_idle_states; i++) {
191
95707d85 192 flags = be32_to_cpu(idle_state_flags[i]);
74aa51b5
PM
193
194 /* Cpuidle accepts exit_latency in us and we estimate
195 * target residency to be 10x exit_latency
196 */
197 latency_ns = be32_to_cpu(idle_state_latency[i]);
8eb8ac89 198 if (flags & OPAL_PM_NAP_ENABLED) {
0888839c
PM
199 /* Add NAP state */
200 strcpy(powernv_states[nr_idle_states].name, "Nap");
201 strcpy(powernv_states[nr_idle_states].desc, "Nap");
202 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIME_VALID;
74aa51b5
PM
203 powernv_states[nr_idle_states].exit_latency =
204 ((unsigned int)latency_ns) / 1000;
205 powernv_states[nr_idle_states].target_residency =
206 ((unsigned int)latency_ns / 100);
0888839c
PM
207 powernv_states[nr_idle_states].enter = &nap_loop;
208 nr_idle_states++;
209 }
210
8eb8ac89 211 if (flags & OPAL_PM_SLEEP_ENABLED) {
0888839c
PM
212 /* Add FASTSLEEP state */
213 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
214 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
215 powernv_states[nr_idle_states].flags =
216 CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
74aa51b5
PM
217 powernv_states[nr_idle_states].exit_latency =
218 ((unsigned int)latency_ns) / 1000;
219 powernv_states[nr_idle_states].target_residency =
220 ((unsigned int)latency_ns / 100);
0888839c
PM
221 powernv_states[nr_idle_states].enter = &fastsleep_loop;
222 nr_idle_states++;
223 }
224 }
225
226 return nr_idle_states;
227}
228
2c2e6ecf
DD
229/*
230 * powernv_idle_probe()
231 * Choose state table for shared versus dedicated partition
232 */
233static int powernv_idle_probe(void)
234{
2c2e6ecf
DD
235 if (cpuidle_disable != IDLE_NO_OVERRIDE)
236 return -ENODEV;
237
238 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
239 cpuidle_state_table = powernv_states;
0888839c
PM
240 /* Device tree can indicate more idle states */
241 max_idle_state = powernv_add_idle_states();
2c2e6ecf
DD
242 } else
243 return -ENODEV;
244
245 return 0;
246}
247
248static int __init powernv_processor_idle_init(void)
249{
250 int retval;
251
252 retval = powernv_idle_probe();
253 if (retval)
254 return retval;
255
256 powernv_cpuidle_driver_init();
257 retval = cpuidle_register(&powernv_idle_driver, NULL);
258 if (retval) {
259 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
260 return retval;
261 }
262
263 register_cpu_notifier(&setup_hotplug_notifier);
264 printk(KERN_DEBUG "powernv_idle_driver registered\n");
265 return 0;
266}
267
268device_initcall(powernv_processor_idle_init);
This page took 0.066244 seconds and 5 git commands to generate.