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>
26 #include <asm/system.h>
27 #include <asm/processor.h>
29 #include <asm/cputable.h>
31 #include <asm/iSeries/HvCall.h>
32 #include <asm/iSeries/ItLpQueue.h>
33 #include <asm/plpar_wrappers.h>
35 extern void power4_idle(void);
37 static int (*idle_loop)(void);
39 #ifdef CONFIG_PPC_ISERIES
40 static unsigned long maxYieldTime = 0;
41 static unsigned long minYieldTime = 0xffffffffffffffffUL;
43 static void yield_shared_processor(void)
46 unsigned long yieldTime;
48 HvCall_setEnabledInterrupts(HvCall_MaskIPI |
54 /* Compute future tb value when yield should expire */
55 HvCall_yieldProcessor(HvCall_YieldTimed, tb+tb_ticks_per_jiffy);
57 yieldTime = get_tb() - tb;
58 if (yieldTime > maxYieldTime)
59 maxYieldTime = yieldTime;
61 if (yieldTime < minYieldTime)
62 minYieldTime = yieldTime;
65 * The decrementer stops during the yield. Force a fake decrementer
66 * here and let the timer_interrupt code sort out the actual time.
68 get_paca()->lppaca.xIntDword.xFields.xDecrInt = 1;
69 process_iSeries_events();
72 static int iSeries_idle(void)
74 struct paca_struct *lpaca;
78 /* ensure iSeries run light will be out when idle */
79 clear_thread_flag(TIF_RUN_LIGHT);
87 if (lpaca->lppaca.xSharedProc) {
88 if (ItLpQueue_isLpIntPending(lpaca->lpqueue_ptr))
89 process_iSeries_events();
91 yield_shared_processor();
93 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
96 set_thread_flag(TIF_POLLING_NRFLAG);
98 while (!need_resched()) {
100 if (ItLpQueue_isLpIntPending(lpaca->lpqueue_ptr))
101 process_iSeries_events();
106 clear_thread_flag(TIF_POLLING_NRFLAG);
120 static int default_idle(void)
123 unsigned int cpu = smp_processor_id();
126 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
129 set_thread_flag(TIF_POLLING_NRFLAG);
131 while (!need_resched() && !cpu_is_offline(cpu)) {
134 * Go into low thread priority and possibly
142 clear_thread_flag(TIF_POLLING_NRFLAG);
148 if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
155 #ifdef CONFIG_PPC_PSERIES
157 DECLARE_PER_CPU(unsigned long, smt_snooze_delay);
159 int dedicated_idle(void)
162 struct paca_struct *lpaca = get_paca(), *ppaca;
163 unsigned long start_snooze;
164 unsigned long *smt_snooze_delay = &__get_cpu_var(smt_snooze_delay);
165 unsigned int cpu = smp_processor_id();
167 ppaca = &paca[cpu ^ 1];
171 * Indicate to the HV that we are idle. Now would be
172 * a good time to find other work to dispatch.
174 lpaca->lppaca.xIdle = 1;
176 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
178 set_thread_flag(TIF_POLLING_NRFLAG);
179 start_snooze = __get_tb() +
180 *smt_snooze_delay * tb_ticks_per_usec;
181 while (!need_resched() && !cpu_is_offline(cpu)) {
183 * Go into low thread priority and possibly
189 if (*smt_snooze_delay == 0 ||
190 __get_tb() < start_snooze)
195 if (!(ppaca->lppaca.xIdle)) {
199 * We are about to sleep the thread
200 * and so wont be polling any
203 clear_thread_flag(TIF_POLLING_NRFLAG);
206 * SMT dynamic mode. Cede will result
207 * in this thread going dormant, if the
208 * partner thread is still doing work.
209 * Thread wakes up if partner goes idle,
210 * an interrupt is presented, or a prod
211 * occurs. Returning from the cede
212 * enables external interrupts.
220 * Give the HV an opportunity at the
221 * processor, since we are not doing
228 clear_thread_flag(TIF_POLLING_NRFLAG);
234 lpaca->lppaca.xIdle = 0;
236 if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
242 static int shared_idle(void)
244 struct paca_struct *lpaca = get_paca();
245 unsigned int cpu = smp_processor_id();
249 * Indicate to the HV that we are idle. Now would be
250 * a good time to find other work to dispatch.
252 lpaca->lppaca.xIdle = 1;
254 while (!need_resched() && !cpu_is_offline(cpu)) {
258 * Yield the processor to the hypervisor. We return if
259 * an external interrupt occurs (which are driven prior
260 * to returning here) or if a prod occurs from another
261 * processor. When returning here, external interrupts
264 * Check need_resched() again with interrupts disabled
274 lpaca->lppaca.xIdle = 0;
276 if (cpu_is_offline(smp_processor_id()) &&
277 system_state == SYSTEM_RUNNING)
284 #endif /* CONFIG_PPC_PSERIES */
286 static int native_idle(void)
289 /* check CPU type here */
298 #endif /* CONFIG_PPC_ISERIES */
310 * Register the sysctl to set/clear powersave_nap.
312 static ctl_table powersave_nap_ctl_table[]={
314 .ctl_name = KERN_PPC_POWERSAVE_NAP,
315 .procname = "powersave-nap",
316 .data = &powersave_nap,
317 .maxlen = sizeof(int),
319 .proc_handler = &proc_dointvec,
323 static ctl_table powersave_nap_sysctl_root[] = {
324 { 1, "kernel", NULL, 0, 0755, powersave_nap_ctl_table, },
329 register_powersave_nap_sysctl(void)
331 register_sysctl_table(powersave_nap_sysctl_root, 0);
335 __initcall(register_powersave_nap_sysctl);
341 * Move that junk to each platform specific file, eventually define
342 * a pSeries_idle for shared processor stuff
344 #ifdef CONFIG_PPC_ISERIES
345 idle_loop = iSeries_idle;
348 idle_loop = default_idle;
350 #ifdef CONFIG_PPC_PSERIES
351 if (systemcfg->platform & PLATFORM_PSERIES) {
352 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
353 if (get_paca()->lppaca.xSharedProc) {
354 printk(KERN_INFO "Using shared processor idle loop\n");
355 idle_loop = shared_idle;
357 printk(KERN_INFO "Using dedicated idle loop\n");
358 idle_loop = dedicated_idle;
361 printk(KERN_INFO "Using default idle loop\n");
362 idle_loop = default_idle;
365 #endif /* CONFIG_PPC_PSERIES */
366 #ifdef CONFIG_PPC_PMAC
367 if (systemcfg->platform == PLATFORM_POWERMAC) {
368 printk(KERN_INFO "Using native/NAP idle loop\n");
369 idle_loop = native_idle;
371 #endif /* CONFIG_PPC_PMAC */