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