blob: e270055e73e2cf15bd290a6ace4d5959ce7552f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Idle daemon for PowerPC. Idle daemon will handle any action
3 * that needs to be taken when the system becomes idle.
4 *
5 * Originally Written by Cort Dougan (cort@cs.nmt.edu)
6 *
7 * iSeries supported added by Mike Corrigan <mikejc@us.ibm.com>
8 *
9 * Additional shared processor, SMT, and firmware support
10 * Copyright (c) 2003 Dave Engebretsen <engebret@us.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18#include <linux/config.h>
19#include <linux/sched.h>
20#include <linux/kernel.h>
21#include <linux/smp.h>
22#include <linux/cpu.h>
23#include <linux/module.h>
24#include <linux/sysctl.h>
25#include <linux/smp.h>
26
27#include <asm/system.h>
28#include <asm/processor.h>
29#include <asm/mmu.h>
30#include <asm/cputable.h>
31#include <asm/time.h>
32#include <asm/iSeries/HvCall.h>
33#include <asm/iSeries/ItLpQueue.h>
34#include <asm/plpar_wrappers.h>
35#include <asm/systemcfg.h>
Michael Ellermanfd899c02005-07-07 17:56:28 -070036#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38extern void power4_idle(void);
39
40static int (*idle_loop)(void);
41
42#ifdef CONFIG_PPC_ISERIES
43static unsigned long maxYieldTime = 0;
44static unsigned long minYieldTime = 0xffffffffffffffffUL;
45
Stephen Rothwellc92877e2005-06-21 17:15:39 -070046static inline void process_iSeries_events(void)
47{
48 asm volatile ("li 0,0x5555; sc" : : : "r0", "r3");
49}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static void yield_shared_processor(void)
52{
53 unsigned long tb;
54 unsigned long yieldTime;
55
56 HvCall_setEnabledInterrupts(HvCall_MaskIPI |
57 HvCall_MaskLpEvent |
58 HvCall_MaskLpProd |
59 HvCall_MaskTimeout);
60
61 tb = get_tb();
62 /* Compute future tb value when yield should expire */
63 HvCall_yieldProcessor(HvCall_YieldTimed, tb+tb_ticks_per_jiffy);
64
65 yieldTime = get_tb() - tb;
66 if (yieldTime > maxYieldTime)
67 maxYieldTime = yieldTime;
68
69 if (yieldTime < minYieldTime)
70 minYieldTime = yieldTime;
71
72 /*
73 * The decrementer stops during the yield. Force a fake decrementer
74 * here and let the timer_interrupt code sort out the actual time.
75 */
76 get_paca()->lppaca.int_dword.fields.decr_int = 1;
77 process_iSeries_events();
78}
79
80static int iSeries_idle(void)
81{
82 struct paca_struct *lpaca;
83 long oldval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* ensure iSeries run light will be out when idle */
Anton Blanchard6dc2f0c2005-06-02 14:02:02 -070086 ppc64_runlatch_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 lpaca = get_paca();
89
90 while (1) {
91 if (lpaca->lppaca.shared_proc) {
Michael Ellerman937b31b2005-06-30 15:15:42 +100092 if (hvlpevent_is_pending())
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 process_iSeries_events();
94 if (!need_resched())
95 yield_shared_processor();
96 } else {
97 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
98
99 if (!oldval) {
100 set_thread_flag(TIF_POLLING_NRFLAG);
101
102 while (!need_resched()) {
103 HMT_medium();
Michael Ellerman937b31b2005-06-30 15:15:42 +1000104 if (hvlpevent_is_pending())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 process_iSeries_events();
106 HMT_low();
107 }
108
109 HMT_medium();
110 clear_thread_flag(TIF_POLLING_NRFLAG);
111 } else {
112 set_need_resched();
113 }
114 }
115
Anton Blanchard6dc2f0c2005-06-02 14:02:02 -0700116 ppc64_runlatch_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 schedule();
Anton Blanchard6dc2f0c2005-06-02 14:02:02 -0700118 ppc64_runlatch_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
121 return 0;
122}
123
124#else
125
Michael Ellermanfd899c02005-07-07 17:56:28 -0700126int default_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 long oldval;
129 unsigned int cpu = smp_processor_id();
130
131 while (1) {
132 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
133
134 if (!oldval) {
135 set_thread_flag(TIF_POLLING_NRFLAG);
136
137 while (!need_resched() && !cpu_is_offline(cpu)) {
138 barrier();
139 /*
140 * Go into low thread priority and possibly
141 * low power mode.
142 */
143 HMT_low();
144 HMT_very_low();
145 }
146
147 HMT_medium();
148 clear_thread_flag(TIF_POLLING_NRFLAG);
149 } else {
150 set_need_resched();
151 }
152
153 schedule();
154 if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
155 cpu_die();
156 }
157
158 return 0;
159}
160
161#ifdef CONFIG_PPC_PSERIES
162
163DECLARE_PER_CPU(unsigned long, smt_snooze_delay);
164
165int dedicated_idle(void)
166{
167 long oldval;
168 struct paca_struct *lpaca = get_paca(), *ppaca;
169 unsigned long start_snooze;
170 unsigned long *smt_snooze_delay = &__get_cpu_var(smt_snooze_delay);
171 unsigned int cpu = smp_processor_id();
172
173 ppaca = &paca[cpu ^ 1];
174
175 while (1) {
176 /*
177 * Indicate to the HV that we are idle. Now would be
178 * a good time to find other work to dispatch.
179 */
180 lpaca->lppaca.idle = 1;
181
182 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
183 if (!oldval) {
184 set_thread_flag(TIF_POLLING_NRFLAG);
185 start_snooze = __get_tb() +
186 *smt_snooze_delay * tb_ticks_per_usec;
187 while (!need_resched() && !cpu_is_offline(cpu)) {
188 /*
189 * Go into low thread priority and possibly
190 * low power mode.
191 */
192 HMT_low();
193 HMT_very_low();
194
195 if (*smt_snooze_delay == 0 ||
196 __get_tb() < start_snooze)
197 continue;
198
199 HMT_medium();
200
201 if (!(ppaca->lppaca.idle)) {
202 local_irq_disable();
203
204 /*
205 * We are about to sleep the thread
206 * and so wont be polling any
207 * more.
208 */
209 clear_thread_flag(TIF_POLLING_NRFLAG);
210
211 /*
212 * SMT dynamic mode. Cede will result
213 * in this thread going dormant, if the
214 * partner thread is still doing work.
215 * Thread wakes up if partner goes idle,
216 * an interrupt is presented, or a prod
217 * occurs. Returning from the cede
218 * enables external interrupts.
219 */
220 if (!need_resched())
221 cede_processor();
222 else
223 local_irq_enable();
224 } else {
225 /*
226 * Give the HV an opportunity at the
227 * processor, since we are not doing
228 * any work.
229 */
230 poll_pending();
231 }
232 }
233
234 clear_thread_flag(TIF_POLLING_NRFLAG);
235 } else {
236 set_need_resched();
237 }
238
239 HMT_medium();
240 lpaca->lppaca.idle = 0;
241 schedule();
242 if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
243 cpu_die();
244 }
245 return 0;
246}
247
248static int shared_idle(void)
249{
250 struct paca_struct *lpaca = get_paca();
251 unsigned int cpu = smp_processor_id();
252
253 while (1) {
254 /*
255 * Indicate to the HV that we are idle. Now would be
256 * a good time to find other work to dispatch.
257 */
258 lpaca->lppaca.idle = 1;
259
260 while (!need_resched() && !cpu_is_offline(cpu)) {
261 local_irq_disable();
262
263 /*
264 * Yield the processor to the hypervisor. We return if
265 * an external interrupt occurs (which are driven prior
266 * to returning here) or if a prod occurs from another
267 * processor. When returning here, external interrupts
268 * are enabled.
269 *
270 * Check need_resched() again with interrupts disabled
271 * to avoid a race.
272 */
273 if (!need_resched())
274 cede_processor();
275 else
276 local_irq_enable();
277 }
278
279 HMT_medium();
280 lpaca->lppaca.idle = 0;
281 schedule();
282 if (cpu_is_offline(smp_processor_id()) &&
283 system_state == SYSTEM_RUNNING)
284 cpu_die();
285 }
286
287 return 0;
288}
289
290#endif /* CONFIG_PPC_PSERIES */
291
Michael Ellermanfd899c02005-07-07 17:56:28 -0700292int native_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 while(1) {
295 /* check CPU type here */
296 if (!need_resched())
297 power4_idle();
298 if (need_resched())
299 schedule();
300
Ingo Molnar39c715b2005-06-21 17:14:34 -0700301 if (cpu_is_offline(raw_smp_processor_id()) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 system_state == SYSTEM_RUNNING)
303 cpu_die();
304 }
305 return 0;
306}
307
308#endif /* CONFIG_PPC_ISERIES */
309
310void cpu_idle(void)
311{
Michael Ellermanfd899c02005-07-07 17:56:28 -0700312 BUG_ON(NULL == ppc_md.idle_loop);
313 ppc_md.idle_loop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316int powersave_nap;
317
318#ifdef CONFIG_SYSCTL
319/*
320 * Register the sysctl to set/clear powersave_nap.
321 */
322static ctl_table powersave_nap_ctl_table[]={
323 {
324 .ctl_name = KERN_PPC_POWERSAVE_NAP,
325 .procname = "powersave-nap",
326 .data = &powersave_nap,
327 .maxlen = sizeof(int),
328 .mode = 0644,
329 .proc_handler = &proc_dointvec,
330 },
331 { 0, },
332};
333static ctl_table powersave_nap_sysctl_root[] = {
334 { 1, "kernel", NULL, 0, 0755, powersave_nap_ctl_table, },
335 { 0,},
336};
337
338static int __init
339register_powersave_nap_sysctl(void)
340{
341 register_sysctl_table(powersave_nap_sysctl_root, 0);
342
343 return 0;
344}
345__initcall(register_powersave_nap_sysctl);
346#endif
347
348int idle_setup(void)
349{
350 /*
351 * Move that junk to each platform specific file, eventually define
352 * a pSeries_idle for shared processor stuff
353 */
354#ifdef CONFIG_PPC_ISERIES
355 idle_loop = iSeries_idle;
356 return 1;
357#else
358 idle_loop = default_idle;
359#endif
360#ifdef CONFIG_PPC_PSERIES
361 if (systemcfg->platform & PLATFORM_PSERIES) {
362 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
363 if (get_paca()->lppaca.shared_proc) {
364 printk(KERN_INFO "Using shared processor idle loop\n");
365 idle_loop = shared_idle;
366 } else {
367 printk(KERN_INFO "Using dedicated idle loop\n");
368 idle_loop = dedicated_idle;
369 }
370 } else {
371 printk(KERN_INFO "Using default idle loop\n");
372 idle_loop = default_idle;
373 }
374 }
375#endif /* CONFIG_PPC_PSERIES */
376#ifndef CONFIG_PPC_ISERIES
377 if (systemcfg->platform == PLATFORM_POWERMAC ||
378 systemcfg->platform == PLATFORM_MAPLE) {
379 printk(KERN_INFO "Using native/NAP idle loop\n");
380 idle_loop = native_idle;
381 }
382#endif /* CONFIG_PPC_ISERIES */
383
384 return 1;
385}