]> Git Repo - linux.git/blob - drivers/cpuidle/cpuidle-pseries.c
cpuidle: powerpc: cpuidle set polling before enabling irqs
[linux.git] / drivers / cpuidle / cpuidle-pseries.c
1 /*
2  *  cpuidle-pseries - 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 static int max_idle_state;
29 static struct cpuidle_state *cpuidle_state_table;
30 static u64 snooze_timeout;
31 static bool snooze_timeout_en;
32
33 static inline void idle_loop_prolog(unsigned long *in_purr)
34 {
35         ppc64_runlatch_off();
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
44 static inline void idle_loop_epilog(unsigned long in_purr)
45 {
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);
51         get_lppaca()->idle = 0;
52
53         if (irqs_disabled())
54                 local_irq_enable();
55         ppc64_runlatch_on();
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;
63         u64 snooze_exit_time;
64
65         set_thread_flag(TIF_POLLING_NRFLAG);
66
67         idle_loop_prolog(&in_purr);
68         local_irq_enable();
69         snooze_exit_time = get_tb() + snooze_timeout;
70
71         while (!need_resched()) {
72                 HMT_low();
73                 HMT_very_low();
74                 if (snooze_timeout_en && get_tb() > snooze_exit_time)
75                         break;
76         }
77
78         HMT_medium();
79         clear_thread_flag(TIF_POLLING_NRFLAG);
80         smp_mb();
81
82         idle_loop_epilog(in_purr);
83
84         return index;
85 }
86
87 static void check_and_cede_processor(void)
88 {
89         /*
90          * Ensure our interrupt state is properly tracked,
91          * also checks if no interrupt has occurred while we
92          * were soft-disabled
93          */
94         if (prep_irq_for_idle()) {
95                 cede_processor();
96 #ifdef CONFIG_TRACE_IRQFLAGS
97                 /* Ensure that H_CEDE returns with IRQs on */
98                 if (WARN_ON(!(mfmsr() & MSR_EE)))
99                         __hard_irq_enable();
100 #endif
101         }
102 }
103
104 static int dedicated_cede_loop(struct cpuidle_device *dev,
105                                 struct cpuidle_driver *drv,
106                                 int index)
107 {
108         unsigned long in_purr;
109
110         idle_loop_prolog(&in_purr);
111         get_lppaca()->donate_dedicated_cpu = 1;
112
113         HMT_medium();
114         check_and_cede_processor();
115
116         get_lppaca()->donate_dedicated_cpu = 0;
117
118         idle_loop_epilog(in_purr);
119
120         return index;
121 }
122
123 static int shared_cede_loop(struct cpuidle_device *dev,
124                         struct cpuidle_driver *drv,
125                         int index)
126 {
127         unsigned long in_purr;
128
129         idle_loop_prolog(&in_purr);
130
131         /*
132          * Yield the processor to the hypervisor.  We return if
133          * an external interrupt occurs (which are driven prior
134          * to returning here) or if a prod occurs from another
135          * processor. When returning here, external interrupts
136          * are enabled.
137          */
138         check_and_cede_processor();
139
140         idle_loop_epilog(in_purr);
141
142         return index;
143 }
144
145 /*
146  * States for dedicated partition case.
147  */
148 static struct cpuidle_state dedicated_states[] = {
149         { /* Snooze */
150                 .name = "snooze",
151                 .desc = "snooze",
152                 .exit_latency = 0,
153                 .target_residency = 0,
154                 .enter = &snooze_loop },
155         { /* CEDE */
156                 .name = "CEDE",
157                 .desc = "CEDE",
158                 .exit_latency = 10,
159                 .target_residency = 100,
160                 .enter = &dedicated_cede_loop },
161 };
162
163 /*
164  * States for shared partition case.
165  */
166 static struct cpuidle_state shared_states[] = {
167         { /* Shared Cede */
168                 .name = "Shared Cede",
169                 .desc = "Shared Cede",
170                 .exit_latency = 0,
171                 .target_residency = 0,
172                 .enter = &shared_cede_loop },
173 };
174
175 static int pseries_cpuidle_cpu_online(unsigned int cpu)
176 {
177         struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
178
179         if (dev && cpuidle_get_driver()) {
180                 cpuidle_pause_and_lock();
181                 cpuidle_enable_device(dev);
182                 cpuidle_resume_and_unlock();
183         }
184         return 0;
185 }
186
187 static int pseries_cpuidle_cpu_dead(unsigned int cpu)
188 {
189         struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
190
191         if (dev && cpuidle_get_driver()) {
192                 cpuidle_pause_and_lock();
193                 cpuidle_disable_device(dev);
194                 cpuidle_resume_and_unlock();
195         }
196         return 0;
197 }
198
199 /*
200  * pseries_cpuidle_driver_init()
201  */
202 static int pseries_cpuidle_driver_init(void)
203 {
204         int idle_state;
205         struct cpuidle_driver *drv = &pseries_idle_driver;
206
207         drv->state_count = 0;
208
209         for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
210                 /* Is the state not enabled? */
211                 if (cpuidle_state_table[idle_state].enter == NULL)
212                         continue;
213
214                 drv->states[drv->state_count] = /* structure copy */
215                         cpuidle_state_table[idle_state];
216
217                 drv->state_count += 1;
218         }
219
220         return 0;
221 }
222
223 /*
224  * pseries_idle_probe()
225  * Choose state table for shared versus dedicated partition
226  */
227 static int pseries_idle_probe(void)
228 {
229
230         if (cpuidle_disable != IDLE_NO_OVERRIDE)
231                 return -ENODEV;
232
233         if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
234                 if (lppaca_shared_proc(get_lppaca())) {
235                         cpuidle_state_table = shared_states;
236                         max_idle_state = ARRAY_SIZE(shared_states);
237                 } else {
238                         cpuidle_state_table = dedicated_states;
239                         max_idle_state = ARRAY_SIZE(dedicated_states);
240                 }
241         } else
242                 return -ENODEV;
243
244         if (max_idle_state > 1) {
245                 snooze_timeout_en = true;
246                 snooze_timeout = cpuidle_state_table[1].target_residency *
247                                  tb_ticks_per_usec;
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();
261         retval = cpuidle_register(&pseries_idle_driver, NULL);
262         if (retval) {
263                 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
264                 return retval;
265         }
266
267         retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
268                                            "cpuidle/pseries:online",
269                                            pseries_cpuidle_cpu_online, NULL);
270         WARN_ON(retval < 0);
271         retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
272                                            "cpuidle/pseries:DEAD", NULL,
273                                            pseries_cpuidle_cpu_dead);
274         WARN_ON(retval < 0);
275         printk(KERN_DEBUG "pseries_idle_driver registered\n");
276         return 0;
277 }
278
279 device_initcall(pseries_processor_idle_init);
This page took 0.049418 seconds and 4 git commands to generate.