Commit | Line | Data |
---|---|---|
707827f3 | 1 | /* |
962e7bd4 | 2 | * cpuidle-pseries - idle state cpuidle driver. |
707827f3 DD |
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> | |
16aaaff6 | 14 | #include <linux/notifier.h> |
707827f3 DD |
15 | |
16 | #include <asm/paca.h> | |
17 | #include <asm/reg.h> | |
707827f3 DD |
18 | #include <asm/machdep.h> |
19 | #include <asm/firmware.h> | |
212bebb4 | 20 | #include <asm/plpar_wrappers.h> |
707827f3 DD |
21 | |
22 | struct cpuidle_driver pseries_idle_driver = { | |
1ca80944 DL |
23 | .name = "pseries_idle", |
24 | .owner = THIS_MODULE, | |
707827f3 DD |
25 | }; |
26 | ||
bf7f61f2 | 27 | static int max_idle_state; |
707827f3 DD |
28 | static struct cpuidle_state *cpuidle_state_table; |
29 | ||
1ca80944 | 30 | static inline void idle_loop_prolog(unsigned long *in_purr) |
707827f3 | 31 | { |
707827f3 DD |
32 | *in_purr = mfspr(SPRN_PURR); |
33 | /* | |
34 | * Indicate to the HV that we are idle. Now would be | |
35 | * a good time to find other work to dispatch. | |
36 | */ | |
37 | get_lppaca()->idle = 1; | |
38 | } | |
39 | ||
1ca80944 | 40 | static inline void idle_loop_epilog(unsigned long in_purr) |
707827f3 | 41 | { |
7ffcf8ec AB |
42 | u64 wait_cycles; |
43 | ||
44 | wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles); | |
45 | wait_cycles += mfspr(SPRN_PURR) - in_purr; | |
46 | get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles); | |
707827f3 | 47 | get_lppaca()->idle = 0; |
707827f3 DD |
48 | } |
49 | ||
50 | static int snooze_loop(struct cpuidle_device *dev, | |
51 | struct cpuidle_driver *drv, | |
52 | int index) | |
53 | { | |
54 | unsigned long in_purr; | |
707827f3 | 55 | |
1ca80944 | 56 | idle_loop_prolog(&in_purr); |
83dac594 DD |
57 | local_irq_enable(); |
58 | set_thread_flag(TIF_POLLING_NRFLAG); | |
707827f3 | 59 | |
b69dbba0 | 60 | while (!need_resched()) { |
83dac594 DD |
61 | HMT_low(); |
62 | HMT_very_low(); | |
707827f3 DD |
63 | } |
64 | ||
707827f3 | 65 | HMT_medium(); |
83dac594 DD |
66 | clear_thread_flag(TIF_POLLING_NRFLAG); |
67 | smp_mb(); | |
68 | ||
1ca80944 DL |
69 | idle_loop_epilog(in_purr); |
70 | ||
707827f3 DD |
71 | return index; |
72 | } | |
73 | ||
7230c564 BH |
74 | static void check_and_cede_processor(void) |
75 | { | |
76 | /* | |
be2cf20a BH |
77 | * Ensure our interrupt state is properly tracked, |
78 | * also checks if no interrupt has occurred while we | |
79 | * were soft-disabled | |
7230c564 | 80 | */ |
be2cf20a | 81 | if (prep_irq_for_idle()) { |
7230c564 | 82 | cede_processor(); |
be2cf20a BH |
83 | #ifdef CONFIG_TRACE_IRQFLAGS |
84 | /* Ensure that H_CEDE returns with IRQs on */ | |
85 | if (WARN_ON(!(mfmsr() & MSR_EE))) | |
86 | __hard_irq_enable(); | |
87 | #endif | |
88 | } | |
7230c564 BH |
89 | } |
90 | ||
707827f3 DD |
91 | static int dedicated_cede_loop(struct cpuidle_device *dev, |
92 | struct cpuidle_driver *drv, | |
93 | int index) | |
94 | { | |
95 | unsigned long in_purr; | |
707827f3 | 96 | |
1ca80944 | 97 | idle_loop_prolog(&in_purr); |
707827f3 DD |
98 | get_lppaca()->donate_dedicated_cpu = 1; |
99 | ||
707827f3 | 100 | HMT_medium(); |
7230c564 | 101 | check_and_cede_processor(); |
707827f3 DD |
102 | |
103 | get_lppaca()->donate_dedicated_cpu = 0; | |
1ca80944 DL |
104 | |
105 | idle_loop_epilog(in_purr); | |
106 | ||
707827f3 DD |
107 | return index; |
108 | } | |
109 | ||
110 | static int shared_cede_loop(struct cpuidle_device *dev, | |
111 | struct cpuidle_driver *drv, | |
112 | int index) | |
113 | { | |
114 | unsigned long in_purr; | |
707827f3 | 115 | |
1ca80944 | 116 | idle_loop_prolog(&in_purr); |
707827f3 DD |
117 | |
118 | /* | |
119 | * Yield the processor to the hypervisor. We return if | |
120 | * an external interrupt occurs (which are driven prior | |
121 | * to returning here) or if a prod occurs from another | |
122 | * processor. When returning here, external interrupts | |
123 | * are enabled. | |
124 | */ | |
7230c564 | 125 | check_and_cede_processor(); |
707827f3 | 126 | |
1ca80944 DL |
127 | idle_loop_epilog(in_purr); |
128 | ||
707827f3 DD |
129 | return index; |
130 | } | |
131 | ||
132 | /* | |
133 | * States for dedicated partition case. | |
134 | */ | |
bf7f61f2 | 135 | static struct cpuidle_state dedicated_states[] = { |
707827f3 DD |
136 | { /* Snooze */ |
137 | .name = "snooze", | |
138 | .desc = "snooze", | |
139 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
140 | .exit_latency = 0, | |
141 | .target_residency = 0, | |
142 | .enter = &snooze_loop }, | |
143 | { /* CEDE */ | |
144 | .name = "CEDE", | |
145 | .desc = "CEDE", | |
146 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
83dac594 DD |
147 | .exit_latency = 10, |
148 | .target_residency = 100, | |
707827f3 DD |
149 | .enter = &dedicated_cede_loop }, |
150 | }; | |
151 | ||
152 | /* | |
153 | * States for shared partition case. | |
154 | */ | |
bf7f61f2 | 155 | static struct cpuidle_state shared_states[] = { |
707827f3 DD |
156 | { /* Shared Cede */ |
157 | .name = "Shared Cede", | |
158 | .desc = "Shared Cede", | |
159 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
160 | .exit_latency = 0, | |
161 | .target_residency = 0, | |
162 | .enter = &shared_cede_loop }, | |
163 | }; | |
164 | ||
8ea959a1 DD |
165 | void update_smt_snooze_delay(int cpu, int residency) |
166 | { | |
167 | struct cpuidle_driver *drv = cpuidle_get_driver(); | |
168 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); | |
169 | ||
170 | if (cpuidle_state_table != dedicated_states) | |
171 | return; | |
172 | ||
173 | if (residency < 0) { | |
174 | /* Disable the Nap state on that cpu */ | |
175 | if (dev) | |
176 | dev->states_usage[1].disable = 1; | |
177 | } else | |
178 | if (drv) | |
83dac594 | 179 | drv->states[1].target_residency = residency; |
8ea959a1 DD |
180 | } |
181 | ||
16aaaff6 DD |
182 | static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n, |
183 | unsigned long action, void *hcpu) | |
707827f3 | 184 | { |
16aaaff6 | 185 | int hotcpu = (unsigned long)hcpu; |
707827f3 | 186 | struct cpuidle_device *dev = |
b69dbba0 | 187 | per_cpu(cpuidle_devices, hotcpu); |
16aaaff6 | 188 | |
852d8cb1 DD |
189 | if (dev && cpuidle_get_driver()) { |
190 | switch (action) { | |
191 | case CPU_ONLINE: | |
192 | case CPU_ONLINE_FROZEN: | |
193 | cpuidle_pause_and_lock(); | |
16aaaff6 | 194 | cpuidle_enable_device(dev); |
852d8cb1 DD |
195 | cpuidle_resume_and_unlock(); |
196 | break; | |
197 | ||
198 | case CPU_DEAD: | |
199 | case CPU_DEAD_FROZEN: | |
200 | cpuidle_pause_and_lock(); | |
201 | cpuidle_disable_device(dev); | |
202 | cpuidle_resume_and_unlock(); | |
203 | break; | |
204 | ||
205 | default: | |
206 | return NOTIFY_DONE; | |
16aaaff6 | 207 | } |
707827f3 | 208 | } |
16aaaff6 | 209 | return NOTIFY_OK; |
707827f3 DD |
210 | } |
211 | ||
16aaaff6 DD |
212 | static struct notifier_block setup_hotplug_notifier = { |
213 | .notifier_call = pseries_cpuidle_add_cpu_notifier, | |
214 | }; | |
215 | ||
707827f3 DD |
216 | /* |
217 | * pseries_cpuidle_driver_init() | |
218 | */ | |
219 | static int pseries_cpuidle_driver_init(void) | |
220 | { | |
221 | int idle_state; | |
222 | struct cpuidle_driver *drv = &pseries_idle_driver; | |
223 | ||
224 | drv->state_count = 0; | |
225 | ||
bf7f61f2 DD |
226 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
227 | /* Is the state not enabled? */ | |
707827f3 DD |
228 | if (cpuidle_state_table[idle_state].enter == NULL) |
229 | continue; | |
230 | ||
231 | drv->states[drv->state_count] = /* structure copy */ | |
232 | cpuidle_state_table[idle_state]; | |
233 | ||
707827f3 DD |
234 | drv->state_count += 1; |
235 | } | |
236 | ||
237 | return 0; | |
238 | } | |
239 | ||
707827f3 DD |
240 | /* |
241 | * pseries_idle_probe() | |
242 | * Choose state table for shared versus dedicated partition | |
243 | */ | |
244 | static int pseries_idle_probe(void) | |
245 | { | |
246 | ||
e8bb3e00 DD |
247 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
248 | return -ENODEV; | |
249 | ||
b69dbba0 | 250 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) { |
bf7f61f2 | 251 | if (lppaca_shared_proc(get_lppaca())) { |
b69dbba0 | 252 | cpuidle_state_table = shared_states; |
bf7f61f2 DD |
253 | max_idle_state = ARRAY_SIZE(shared_states); |
254 | } else { | |
b69dbba0 | 255 | cpuidle_state_table = dedicated_states; |
bf7f61f2 DD |
256 | max_idle_state = ARRAY_SIZE(dedicated_states); |
257 | } | |
b69dbba0 DD |
258 | } else |
259 | return -ENODEV; | |
707827f3 DD |
260 | |
261 | return 0; | |
262 | } | |
263 | ||
264 | static int __init pseries_processor_idle_init(void) | |
265 | { | |
266 | int retval; | |
267 | ||
268 | retval = pseries_idle_probe(); | |
269 | if (retval) | |
270 | return retval; | |
271 | ||
272 | pseries_cpuidle_driver_init(); | |
b69dbba0 | 273 | retval = cpuidle_register(&pseries_idle_driver, NULL); |
707827f3 DD |
274 | if (retval) { |
275 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); | |
276 | return retval; | |
277 | } | |
278 | ||
16aaaff6 | 279 | register_cpu_notifier(&setup_hotplug_notifier); |
707827f3 | 280 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
707827f3 DD |
281 | return 0; |
282 | } | |
283 | ||
12431c64 | 284 | device_initcall(pseries_processor_idle_init); |