Merge tag 'stable/for-linus-3.14-rc0-tag' of git://git.kernel.org/pub/scm/linux/kerne...
[deliverable/linux.git] / arch / powerpc / platforms / pseries / processor_idle.c
1 /*
2 * processor_idle - idle state cpuidle driver.
3 * Adapted from drivers/idle/intel_idle.c and
4 * drivers/acpi/processor_idle.c
5 *
6 */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/moduleparam.h>
12 #include <linux/cpuidle.h>
13 #include <linux/cpu.h>
14 #include <linux/notifier.h>
15
16 #include <asm/paca.h>
17 #include <asm/reg.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/runlatch.h>
21 #include <asm/plpar_wrappers.h>
22
23 struct cpuidle_driver pseries_idle_driver = {
24 .name = "pseries_idle",
25 .owner = THIS_MODULE,
26 };
27
28 #define MAX_IDLE_STATE_COUNT 2
29
30 static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
31 static struct cpuidle_state *cpuidle_state_table;
32
33 static inline void idle_loop_prolog(unsigned long *in_purr)
34 {
35 *in_purr = mfspr(SPRN_PURR);
36 /*
37 * Indicate to the HV that we are idle. Now would be
38 * a good time to find other work to dispatch.
39 */
40 get_lppaca()->idle = 1;
41 }
42
43 static inline void idle_loop_epilog(unsigned long in_purr)
44 {
45 u64 wait_cycles;
46
47 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
48 wait_cycles += mfspr(SPRN_PURR) - in_purr;
49 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
50 get_lppaca()->idle = 0;
51 }
52
53 static int snooze_loop(struct cpuidle_device *dev,
54 struct cpuidle_driver *drv,
55 int index)
56 {
57 unsigned long in_purr;
58 int cpu = dev->cpu;
59
60 idle_loop_prolog(&in_purr);
61 local_irq_enable();
62 set_thread_flag(TIF_POLLING_NRFLAG);
63
64 while ((!need_resched()) && cpu_online(cpu)) {
65 ppc64_runlatch_off();
66 HMT_low();
67 HMT_very_low();
68 }
69
70 HMT_medium();
71 clear_thread_flag(TIF_POLLING_NRFLAG);
72 smp_mb();
73
74 idle_loop_epilog(in_purr);
75
76 return index;
77 }
78
79 static void check_and_cede_processor(void)
80 {
81 /*
82 * Ensure our interrupt state is properly tracked,
83 * also checks if no interrupt has occurred while we
84 * were soft-disabled
85 */
86 if (prep_irq_for_idle()) {
87 cede_processor();
88 #ifdef CONFIG_TRACE_IRQFLAGS
89 /* Ensure that H_CEDE returns with IRQs on */
90 if (WARN_ON(!(mfmsr() & MSR_EE)))
91 __hard_irq_enable();
92 #endif
93 }
94 }
95
96 static int dedicated_cede_loop(struct cpuidle_device *dev,
97 struct cpuidle_driver *drv,
98 int index)
99 {
100 unsigned long in_purr;
101
102 idle_loop_prolog(&in_purr);
103 get_lppaca()->donate_dedicated_cpu = 1;
104
105 ppc64_runlatch_off();
106 HMT_medium();
107 check_and_cede_processor();
108
109 get_lppaca()->donate_dedicated_cpu = 0;
110
111 idle_loop_epilog(in_purr);
112
113 return index;
114 }
115
116 static int shared_cede_loop(struct cpuidle_device *dev,
117 struct cpuidle_driver *drv,
118 int index)
119 {
120 unsigned long in_purr;
121
122 idle_loop_prolog(&in_purr);
123
124 /*
125 * Yield the processor to the hypervisor. We return if
126 * an external interrupt occurs (which are driven prior
127 * to returning here) or if a prod occurs from another
128 * processor. When returning here, external interrupts
129 * are enabled.
130 */
131 check_and_cede_processor();
132
133 idle_loop_epilog(in_purr);
134
135 return index;
136 }
137
138 /*
139 * States for dedicated partition case.
140 */
141 static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
142 { /* Snooze */
143 .name = "snooze",
144 .desc = "snooze",
145 .flags = CPUIDLE_FLAG_TIME_VALID,
146 .exit_latency = 0,
147 .target_residency = 0,
148 .enter = &snooze_loop },
149 { /* CEDE */
150 .name = "CEDE",
151 .desc = "CEDE",
152 .flags = CPUIDLE_FLAG_TIME_VALID,
153 .exit_latency = 10,
154 .target_residency = 100,
155 .enter = &dedicated_cede_loop },
156 };
157
158 /*
159 * States for shared partition case.
160 */
161 static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
162 { /* Shared Cede */
163 .name = "Shared Cede",
164 .desc = "Shared Cede",
165 .flags = CPUIDLE_FLAG_TIME_VALID,
166 .exit_latency = 0,
167 .target_residency = 0,
168 .enter = &shared_cede_loop },
169 };
170
171 void update_smt_snooze_delay(int cpu, int residency)
172 {
173 struct cpuidle_driver *drv = cpuidle_get_driver();
174 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
175
176 if (cpuidle_state_table != dedicated_states)
177 return;
178
179 if (residency < 0) {
180 /* Disable the Nap state on that cpu */
181 if (dev)
182 dev->states_usage[1].disable = 1;
183 } else
184 if (drv)
185 drv->states[1].target_residency = residency;
186 }
187
188 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
189 unsigned long action, void *hcpu)
190 {
191 int hotcpu = (unsigned long)hcpu;
192 struct cpuidle_device *dev =
193 per_cpu_ptr(cpuidle_devices, hotcpu);
194
195 if (dev && cpuidle_get_driver()) {
196 switch (action) {
197 case CPU_ONLINE:
198 case CPU_ONLINE_FROZEN:
199 cpuidle_pause_and_lock();
200 cpuidle_enable_device(dev);
201 cpuidle_resume_and_unlock();
202 break;
203
204 case CPU_DEAD:
205 case CPU_DEAD_FROZEN:
206 cpuidle_pause_and_lock();
207 cpuidle_disable_device(dev);
208 cpuidle_resume_and_unlock();
209 break;
210
211 default:
212 return NOTIFY_DONE;
213 }
214 }
215 return NOTIFY_OK;
216 }
217
218 static struct notifier_block setup_hotplug_notifier = {
219 .notifier_call = pseries_cpuidle_add_cpu_notifier,
220 };
221
222 /*
223 * pseries_cpuidle_driver_init()
224 */
225 static int pseries_cpuidle_driver_init(void)
226 {
227 int idle_state;
228 struct cpuidle_driver *drv = &pseries_idle_driver;
229
230 drv->state_count = 0;
231
232 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
233
234 if (idle_state > max_idle_state)
235 break;
236
237 /* is the state not enabled? */
238 if (cpuidle_state_table[idle_state].enter == NULL)
239 continue;
240
241 drv->states[drv->state_count] = /* structure copy */
242 cpuidle_state_table[idle_state];
243
244 drv->state_count += 1;
245 }
246
247 return 0;
248 }
249
250 /*
251 * pseries_idle_probe()
252 * Choose state table for shared versus dedicated partition
253 */
254 static int pseries_idle_probe(void)
255 {
256
257 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
258 return -ENODEV;
259
260 if (cpuidle_disable != IDLE_NO_OVERRIDE)
261 return -ENODEV;
262
263 if (max_idle_state == 0) {
264 printk(KERN_DEBUG "pseries processor idle disabled.\n");
265 return -EPERM;
266 }
267
268 if (lppaca_shared_proc(get_lppaca()))
269 cpuidle_state_table = shared_states;
270 else
271 cpuidle_state_table = dedicated_states;
272
273 return 0;
274 }
275
276 static int __init pseries_processor_idle_init(void)
277 {
278 int retval;
279
280 retval = pseries_idle_probe();
281 if (retval)
282 return retval;
283
284 pseries_cpuidle_driver_init();
285 retval = cpuidle_register(&pseries_idle_driver, NULL);
286 if (retval) {
287 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
288 return retval;
289 }
290
291 register_cpu_notifier(&setup_hotplug_notifier);
292 printk(KERN_DEBUG "pseries_idle_driver registered\n");
293
294 return 0;
295 }
296
297 static void __exit pseries_processor_idle_exit(void)
298 {
299
300 unregister_cpu_notifier(&setup_hotplug_notifier);
301 cpuidle_unregister(&pseries_idle_driver);
302
303 return;
304 }
305
306 module_init(pseries_processor_idle_init);
307 module_exit(pseries_processor_idle_exit);
308
309 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
310 MODULE_DESCRIPTION("Cpuidle driver for POWER");
311 MODULE_LICENSE("GPL");
This page took 0.036915 seconds and 6 git commands to generate.