xref: /linux/arch/powerpc/kernel/idle.c (revision 61e9916eba35dfb76d38013a5aae9a59cc50877a)
1a0652fc9SPaul Mackerras /*
2a0652fc9SPaul Mackerras  * Idle daemon for PowerPC.  Idle daemon will handle any action
3a0652fc9SPaul Mackerras  * that needs to be taken when the system becomes idle.
4a0652fc9SPaul Mackerras  *
5a0652fc9SPaul Mackerras  * Originally written by Cort Dougan (cort@cs.nmt.edu).
6a0652fc9SPaul Mackerras  * Subsequent 32-bit hacking by Tom Rini, Armin Kuster,
7a0652fc9SPaul Mackerras  * Paul Mackerras and others.
8a0652fc9SPaul Mackerras  *
9a0652fc9SPaul Mackerras  * iSeries supported added by Mike Corrigan <mikejc@us.ibm.com>
10a0652fc9SPaul Mackerras  *
11a0652fc9SPaul Mackerras  * Additional shared processor, SMT, and firmware support
12a0652fc9SPaul Mackerras  *    Copyright (c) 2003 Dave Engebretsen <engebret@us.ibm.com>
13a0652fc9SPaul Mackerras  *
14a0652fc9SPaul Mackerras  * 32-bit and 64-bit versions merged by Paul Mackerras <paulus@samba.org>
15a0652fc9SPaul Mackerras  *
16a0652fc9SPaul Mackerras  * This program is free software; you can redistribute it and/or
17a0652fc9SPaul Mackerras  * modify it under the terms of the GNU General Public License
18a0652fc9SPaul Mackerras  * as published by the Free Software Foundation; either version
19a0652fc9SPaul Mackerras  * 2 of the License, or (at your option) any later version.
20a0652fc9SPaul Mackerras  */
21a0652fc9SPaul Mackerras 
22a0652fc9SPaul Mackerras #include <linux/sched.h>
23a0652fc9SPaul Mackerras #include <linux/kernel.h>
24a0652fc9SPaul Mackerras #include <linux/smp.h>
25a0652fc9SPaul Mackerras #include <linux/cpu.h>
26a0652fc9SPaul Mackerras #include <linux/sysctl.h>
271ad74998STony Breeds #include <linux/tick.h>
28a0652fc9SPaul Mackerras 
29a0652fc9SPaul Mackerras #include <asm/system.h>
30a0652fc9SPaul Mackerras #include <asm/processor.h>
31a0652fc9SPaul Mackerras #include <asm/cputable.h>
32a0652fc9SPaul Mackerras #include <asm/time.h>
33a0652fc9SPaul Mackerras #include <asm/machdep.h>
34a0652fc9SPaul Mackerras #include <asm/smp.h>
35a0652fc9SPaul Mackerras 
36a0652fc9SPaul Mackerras #ifdef CONFIG_HOTPLUG_CPU
37*61e9916eSJohannes Berg #define cpu_should_die()	cpu_is_offline(smp_processor_id())
38a0652fc9SPaul Mackerras #else
39a0652fc9SPaul Mackerras #define cpu_should_die()	0
40a0652fc9SPaul Mackerras #endif
41a0652fc9SPaul Mackerras 
42302eca18Sarnd@arndb.de static int __init powersave_off(char *arg)
43302eca18Sarnd@arndb.de {
44302eca18Sarnd@arndb.de 	ppc_md.power_save = NULL;
45302eca18Sarnd@arndb.de 	return 0;
46302eca18Sarnd@arndb.de }
47302eca18Sarnd@arndb.de __setup("powersave=off", powersave_off);
48302eca18Sarnd@arndb.de 
49a0652fc9SPaul Mackerras /*
50a0652fc9SPaul Mackerras  * The body of the idle task.
51a0652fc9SPaul Mackerras  */
52a0652fc9SPaul Mackerras void cpu_idle(void)
53a0652fc9SPaul Mackerras {
54a0652fc9SPaul Mackerras 	if (ppc_md.idle_loop)
55a0652fc9SPaul Mackerras 		ppc_md.idle_loop();	/* doesn't return */
56a0652fc9SPaul Mackerras 
57a0652fc9SPaul Mackerras 	set_thread_flag(TIF_POLLING_NRFLAG);
58a0652fc9SPaul Mackerras 	while (1) {
59b8f8c3cfSThomas Gleixner 		tick_nohz_stop_sched_tick(1);
60ddafddcfSAnton Blanchard 		while (!need_resched() && !cpu_should_die()) {
61a0652fc9SPaul Mackerras 			ppc64_runlatch_off();
62a0652fc9SPaul Mackerras 
63a0652fc9SPaul Mackerras 			if (ppc_md.power_save) {
64a0652fc9SPaul Mackerras 				clear_thread_flag(TIF_POLLING_NRFLAG);
65a0652fc9SPaul Mackerras 				/*
66a0652fc9SPaul Mackerras 				 * smp_mb is so clearing of TIF_POLLING_NRFLAG
67a0652fc9SPaul Mackerras 				 * is ordered w.r.t. need_resched() test.
68a0652fc9SPaul Mackerras 				 */
69a0652fc9SPaul Mackerras 				smp_mb();
70a0652fc9SPaul Mackerras 				local_irq_disable();
71a0652fc9SPaul Mackerras 
72a0652fc9SPaul Mackerras 				/* check again after disabling irqs */
73a0652fc9SPaul Mackerras 				if (!need_resched() && !cpu_should_die())
74a0652fc9SPaul Mackerras 					ppc_md.power_save();
75a0652fc9SPaul Mackerras 
76a0652fc9SPaul Mackerras 				local_irq_enable();
77a0652fc9SPaul Mackerras 				set_thread_flag(TIF_POLLING_NRFLAG);
78a0652fc9SPaul Mackerras 
79a0652fc9SPaul Mackerras 			} else {
80a0652fc9SPaul Mackerras 				/*
81a0652fc9SPaul Mackerras 				 * Go into low thread priority and possibly
82a0652fc9SPaul Mackerras 				 * low power mode.
83a0652fc9SPaul Mackerras 				 */
84a0652fc9SPaul Mackerras 				HMT_low();
85a0652fc9SPaul Mackerras 				HMT_very_low();
86a0652fc9SPaul Mackerras 			}
87a0652fc9SPaul Mackerras 		}
88a0652fc9SPaul Mackerras 
89a0652fc9SPaul Mackerras 		HMT_medium();
90a0652fc9SPaul Mackerras 		ppc64_runlatch_on();
911ad74998STony Breeds 		tick_nohz_restart_sched_tick();
92a0652fc9SPaul Mackerras 		if (cpu_should_die())
93a0652fc9SPaul Mackerras 			cpu_die();
94a0652fc9SPaul Mackerras 		preempt_enable_no_resched();
95a0652fc9SPaul Mackerras 		schedule();
96a0652fc9SPaul Mackerras 		preempt_disable();
97a0652fc9SPaul Mackerras 	}
98a0652fc9SPaul Mackerras }
99a0652fc9SPaul Mackerras 
100a0652fc9SPaul Mackerras int powersave_nap;
101a0652fc9SPaul Mackerras 
102a0652fc9SPaul Mackerras #ifdef CONFIG_SYSCTL
103a0652fc9SPaul Mackerras /*
104a0652fc9SPaul Mackerras  * Register the sysctl to set/clear powersave_nap.
105a0652fc9SPaul Mackerras  */
106a0652fc9SPaul Mackerras static ctl_table powersave_nap_ctl_table[]={
107a0652fc9SPaul Mackerras 	{
108a0652fc9SPaul Mackerras 		.ctl_name	= KERN_PPC_POWERSAVE_NAP,
109a0652fc9SPaul Mackerras 		.procname	= "powersave-nap",
110a0652fc9SPaul Mackerras 		.data		= &powersave_nap,
111a0652fc9SPaul Mackerras 		.maxlen		= sizeof(int),
112a0652fc9SPaul Mackerras 		.mode		= 0644,
113a0652fc9SPaul Mackerras 		.proc_handler	= &proc_dointvec,
114a0652fc9SPaul Mackerras 	},
115f5f10678SEric W. Biederman 	{}
116a0652fc9SPaul Mackerras };
117a0652fc9SPaul Mackerras static ctl_table powersave_nap_sysctl_root[] = {
118f5f10678SEric W. Biederman 	{
119f5f10678SEric W. Biederman 		.ctl_name	= CTL_KERN,
120f5f10678SEric W. Biederman 		.procname	= "kernel",
121fb293ae1SAlexey Dobriyan 		.mode		= 0555,
122f5f10678SEric W. Biederman 		.child		= powersave_nap_ctl_table,
123f5f10678SEric W. Biederman 	},
124f5f10678SEric W. Biederman 	{}
125a0652fc9SPaul Mackerras };
126a0652fc9SPaul Mackerras 
127a0652fc9SPaul Mackerras static int __init
128a0652fc9SPaul Mackerras register_powersave_nap_sysctl(void)
129a0652fc9SPaul Mackerras {
1300b4d4147SEric W. Biederman 	register_sysctl_table(powersave_nap_sysctl_root);
131a0652fc9SPaul Mackerras 
132a0652fc9SPaul Mackerras 	return 0;
133a0652fc9SPaul Mackerras }
134a0652fc9SPaul Mackerras __initcall(register_powersave_nap_sysctl);
135a0652fc9SPaul Mackerras #endif
136