2 * Idle daemon for PowerPC. Idle daemon will handle any action
3 * that needs to be taken when the system becomes idle.
5 * Originally Written by Cort Dougan (cort@cs.nmt.edu)
7 * iSeries supported added by Mike Corrigan <mikejc@us.ibm.com>
9 * Additional shared processor, SMT, and firmware support
10 * Copyright (c) 2003 Dave Engebretsen <engebret@us.ibm.com>
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.
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>
27 #include <asm/system.h>
28 #include <asm/processor.h>
30 #include <asm/cputable.h>
32 #include <asm/iSeries/HvCall.h>
33 #include <asm/iSeries/ItLpQueue.h>
34 #include <asm/plpar_wrappers.h>
35 #include <asm/systemcfg.h>
37 extern void power4_idle(void);
39 static int (*idle_loop)(void);
41 #ifdef CONFIG_PPC_ISERIES
42 static unsigned long maxYieldTime = 0;
43 static unsigned long minYieldTime = 0xffffffffffffffffUL;
45 static inline void process_iSeries_events(void)
47 asm volatile ("li 0,0x5555; sc" : : : "r0", "r3");
50 static void yield_shared_processor(void)
53 unsigned long yieldTime;
55 HvCall_setEnabledInterrupts(HvCall_MaskIPI |
61 /* Compute future tb value when yield should expire */
62 HvCall_yieldProcessor(HvCall_YieldTimed, tb+tb_ticks_per_jiffy);
64 yieldTime = get_tb() - tb;
65 if (yieldTime > maxYieldTime)
66 maxYieldTime = yieldTime;
68 if (yieldTime < minYieldTime)
69 minYieldTime = yieldTime;
72 * The decrementer stops during the yield. Force a fake decrementer
73 * here and let the timer_interrupt code sort out the actual time.
75 get_paca()->lppaca.int_dword.fields.decr_int = 1;
76 process_iSeries_events();
79 static int iSeries_idle(void)
81 struct paca_struct *lpaca;
84 /* ensure iSeries run light will be out when idle */
90 if (lpaca->lppaca.shared_proc) {
91 if (ItLpQueue_isLpIntPending(lpaca->lpqueue_ptr))
92 process_iSeries_events();
94 yield_shared_processor();
96 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
99 set_thread_flag(TIF_POLLING_NRFLAG);
101 while (!need_resched()) {
103 if (ItLpQueue_isLpIntPending(lpaca->lpqueue_ptr))
104 process_iSeries_events();
109 clear_thread_flag(TIF_POLLING_NRFLAG);
117 ppc64_runlatch_off();
125 static int default_idle(void)
128 unsigned int cpu = smp_processor_id();
131 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
134 set_thread_flag(TIF_POLLING_NRFLAG);
136 while (!need_resched() && !cpu_is_offline(cpu)) {
139 * Go into low thread priority and possibly
147 clear_thread_flag(TIF_POLLING_NRFLAG);
153 if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
160 #ifdef CONFIG_PPC_PSERIES
162 DECLARE_PER_CPU(unsigned long, smt_snooze_delay);
164 int dedicated_idle(void)
167 struct paca_struct *lpaca = get_paca(), *ppaca;
168 unsigned long start_snooze;
169 unsigned long *smt_snooze_delay = &__get_cpu_var(smt_snooze_delay);
170 unsigned int cpu = smp_processor_id();
172 ppaca = &paca[cpu ^ 1];
176 * Indicate to the HV that we are idle. Now would be
177 * a good time to find other work to dispatch.
179 lpaca->lppaca.idle = 1;
181 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
183 set_thread_flag(TIF_POLLING_NRFLAG);
184 start_snooze = __get_tb() +
185 *smt_snooze_delay * tb_ticks_per_usec;
186 while (!need_resched() && !cpu_is_offline(cpu)) {
188 * Go into low thread priority and possibly
194 if (*smt_snooze_delay == 0 ||
195 __get_tb() < start_snooze)
200 if (!(ppaca->lppaca.idle)) {
204 * We are about to sleep the thread
205 * and so wont be polling any
208 clear_thread_flag(TIF_POLLING_NRFLAG);
211 * SMT dynamic mode. Cede will result
212 * in this thread going dormant, if the
213 * partner thread is still doing work.
214 * Thread wakes up if partner goes idle,
215 * an interrupt is presented, or a prod
216 * occurs. Returning from the cede
217 * enables external interrupts.
225 * Give the HV an opportunity at the
226 * processor, since we are not doing
233 clear_thread_flag(TIF_POLLING_NRFLAG);
239 lpaca->lppaca.idle = 0;
241 if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
247 static int shared_idle(void)
249 struct paca_struct *lpaca = get_paca();
250 unsigned int cpu = smp_processor_id();
254 * Indicate to the HV that we are idle. Now would be
255 * a good time to find other work to dispatch.
257 lpaca->lppaca.idle = 1;
259 while (!need_resched() && !cpu_is_offline(cpu)) {
263 * Yield the processor to the hypervisor. We return if
264 * an external interrupt occurs (which are driven prior
265 * to returning here) or if a prod occurs from another
266 * processor. When returning here, external interrupts
269 * Check need_resched() again with interrupts disabled
279 lpaca->lppaca.idle = 0;
281 if (cpu_is_offline(smp_processor_id()) &&
282 system_state == SYSTEM_RUNNING)
289 #endif /* CONFIG_PPC_PSERIES */
291 static int native_idle(void)
294 /* check CPU type here */
300 if (cpu_is_offline(raw_smp_processor_id()) &&
301 system_state == SYSTEM_RUNNING)
307 #endif /* CONFIG_PPC_ISERIES */
318 * Register the sysctl to set/clear powersave_nap.
320 static ctl_table powersave_nap_ctl_table[]={
322 .ctl_name = KERN_PPC_POWERSAVE_NAP,
323 .procname = "powersave-nap",
324 .data = &powersave_nap,
325 .maxlen = sizeof(int),
327 .proc_handler = &proc_dointvec,
331 static ctl_table powersave_nap_sysctl_root[] = {
332 { 1, "kernel", NULL, 0, 0755, powersave_nap_ctl_table, },
337 register_powersave_nap_sysctl(void)
339 register_sysctl_table(powersave_nap_sysctl_root, 0);
343 __initcall(register_powersave_nap_sysctl);
349 * Move that junk to each platform specific file, eventually define
350 * a pSeries_idle for shared processor stuff
352 #ifdef CONFIG_PPC_ISERIES
353 idle_loop = iSeries_idle;
356 idle_loop = default_idle;
358 #ifdef CONFIG_PPC_PSERIES
359 if (systemcfg->platform & PLATFORM_PSERIES) {
360 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
361 if (get_paca()->lppaca.shared_proc) {
362 printk(KERN_INFO "Using shared processor idle loop\n");
363 idle_loop = shared_idle;
365 printk(KERN_INFO "Using dedicated idle loop\n");
366 idle_loop = dedicated_idle;
369 printk(KERN_INFO "Using default idle loop\n");
370 idle_loop = default_idle;
373 #endif /* CONFIG_PPC_PSERIES */
374 #ifndef CONFIG_PPC_ISERIES
375 if (systemcfg->platform == PLATFORM_POWERMAC ||
376 systemcfg->platform == PLATFORM_MAPLE) {
377 printk(KERN_INFO "Using native/NAP idle loop\n");
378 idle_loop = native_idle;
380 #endif /* CONFIG_PPC_ISERIES */