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