idle.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  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. #include <linux/config.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/smp.h>
  21. #include <linux/cpu.h>
  22. #include <linux/module.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/smp.h>
  25. #include <asm/system.h>
  26. #include <asm/processor.h>
  27. #include <asm/mmu.h>
  28. #include <asm/cputable.h>
  29. #include <asm/time.h>
  30. #include <asm/iSeries/HvCall.h>
  31. #include <asm/iSeries/ItLpQueue.h>
  32. #include <asm/plpar_wrappers.h>
  33. #include <asm/systemcfg.h>
  34. #include <asm/machdep.h>
  35. extern void power4_idle(void);
  36. static int (*idle_loop)(void);
  37. #ifdef CONFIG_PPC_ISERIES
  38. static unsigned long maxYieldTime = 0;
  39. static unsigned long minYieldTime = 0xffffffffffffffffUL;
  40. static inline void process_iSeries_events(void)
  41. {
  42. asm volatile ("li 0,0x5555; sc" : : : "r0", "r3");
  43. }
  44. static void yield_shared_processor(void)
  45. {
  46. unsigned long tb;
  47. unsigned long yieldTime;
  48. HvCall_setEnabledInterrupts(HvCall_MaskIPI |
  49. HvCall_MaskLpEvent |
  50. HvCall_MaskLpProd |
  51. HvCall_MaskTimeout);
  52. tb = get_tb();
  53. /* Compute future tb value when yield should expire */
  54. HvCall_yieldProcessor(HvCall_YieldTimed, tb+tb_ticks_per_jiffy);
  55. yieldTime = get_tb() - tb;
  56. if (yieldTime > maxYieldTime)
  57. maxYieldTime = yieldTime;
  58. if (yieldTime < minYieldTime)
  59. minYieldTime = yieldTime;
  60. /*
  61. * The decrementer stops during the yield. Force a fake decrementer
  62. * here and let the timer_interrupt code sort out the actual time.
  63. */
  64. get_paca()->lppaca.int_dword.fields.decr_int = 1;
  65. process_iSeries_events();
  66. }
  67. static int iSeries_idle(void)
  68. {
  69. struct paca_struct *lpaca;
  70. long oldval;
  71. /* ensure iSeries run light will be out when idle */
  72. ppc64_runlatch_off();
  73. lpaca = get_paca();
  74. while (1) {
  75. if (lpaca->lppaca.shared_proc) {
  76. if (hvlpevent_is_pending())
  77. process_iSeries_events();
  78. if (!need_resched())
  79. yield_shared_processor();
  80. } else {
  81. oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
  82. if (!oldval) {
  83. set_thread_flag(TIF_POLLING_NRFLAG);
  84. while (!need_resched()) {
  85. HMT_medium();
  86. if (hvlpevent_is_pending())
  87. process_iSeries_events();
  88. HMT_low();
  89. }
  90. HMT_medium();
  91. clear_thread_flag(TIF_POLLING_NRFLAG);
  92. } else {
  93. set_need_resched();
  94. }
  95. }
  96. ppc64_runlatch_on();
  97. schedule();
  98. ppc64_runlatch_off();
  99. }
  100. return 0;
  101. }
  102. #else
  103. int default_idle(void)
  104. {
  105. long oldval;
  106. unsigned int cpu = smp_processor_id();
  107. while (1) {
  108. oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
  109. if (!oldval) {
  110. set_thread_flag(TIF_POLLING_NRFLAG);
  111. while (!need_resched() && !cpu_is_offline(cpu)) {
  112. barrier();
  113. /*
  114. * Go into low thread priority and possibly
  115. * low power mode.
  116. */
  117. HMT_low();
  118. HMT_very_low();
  119. }
  120. HMT_medium();
  121. clear_thread_flag(TIF_POLLING_NRFLAG);
  122. } else {
  123. set_need_resched();
  124. }
  125. schedule();
  126. if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
  127. cpu_die();
  128. }
  129. return 0;
  130. }
  131. #ifdef CONFIG_PPC_PSERIES
  132. DECLARE_PER_CPU(unsigned long, smt_snooze_delay);
  133. int dedicated_idle(void)
  134. {
  135. long oldval;
  136. struct paca_struct *lpaca = get_paca(), *ppaca;
  137. unsigned long start_snooze;
  138. unsigned long *smt_snooze_delay = &__get_cpu_var(smt_snooze_delay);
  139. unsigned int cpu = smp_processor_id();
  140. ppaca = &paca[cpu ^ 1];
  141. while (1) {
  142. /*
  143. * Indicate to the HV that we are idle. Now would be
  144. * a good time to find other work to dispatch.
  145. */
  146. lpaca->lppaca.idle = 1;
  147. oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
  148. if (!oldval) {
  149. set_thread_flag(TIF_POLLING_NRFLAG);
  150. start_snooze = __get_tb() +
  151. *smt_snooze_delay * tb_ticks_per_usec;
  152. while (!need_resched() && !cpu_is_offline(cpu)) {
  153. /*
  154. * Go into low thread priority and possibly
  155. * low power mode.
  156. */
  157. HMT_low();
  158. HMT_very_low();
  159. if (*smt_snooze_delay == 0 ||
  160. __get_tb() < start_snooze)
  161. continue;
  162. HMT_medium();
  163. if (!(ppaca->lppaca.idle)) {
  164. local_irq_disable();
  165. /*
  166. * We are about to sleep the thread
  167. * and so wont be polling any
  168. * more.
  169. */
  170. clear_thread_flag(TIF_POLLING_NRFLAG);
  171. /*
  172. * SMT dynamic mode. Cede will result
  173. * in this thread going dormant, if the
  174. * partner thread is still doing work.
  175. * Thread wakes up if partner goes idle,
  176. * an interrupt is presented, or a prod
  177. * occurs. Returning from the cede
  178. * enables external interrupts.
  179. */
  180. if (!need_resched())
  181. cede_processor();
  182. else
  183. local_irq_enable();
  184. } else {
  185. /*
  186. * Give the HV an opportunity at the
  187. * processor, since we are not doing
  188. * any work.
  189. */
  190. poll_pending();
  191. }
  192. }
  193. clear_thread_flag(TIF_POLLING_NRFLAG);
  194. } else {
  195. set_need_resched();
  196. }
  197. HMT_medium();
  198. lpaca->lppaca.idle = 0;
  199. schedule();
  200. if (cpu_is_offline(cpu) && system_state == SYSTEM_RUNNING)
  201. cpu_die();
  202. }
  203. return 0;
  204. }
  205. static int shared_idle(void)
  206. {
  207. struct paca_struct *lpaca = get_paca();
  208. unsigned int cpu = smp_processor_id();
  209. while (1) {
  210. /*
  211. * Indicate to the HV that we are idle. Now would be
  212. * a good time to find other work to dispatch.
  213. */
  214. lpaca->lppaca.idle = 1;
  215. while (!need_resched() && !cpu_is_offline(cpu)) {
  216. local_irq_disable();
  217. /*
  218. * Yield the processor to the hypervisor. We return if
  219. * an external interrupt occurs (which are driven prior
  220. * to returning here) or if a prod occurs from another
  221. * processor. When returning here, external interrupts
  222. * are enabled.
  223. *
  224. * Check need_resched() again with interrupts disabled
  225. * to avoid a race.
  226. */
  227. if (!need_resched())
  228. cede_processor();
  229. else
  230. local_irq_enable();
  231. }
  232. HMT_medium();
  233. lpaca->lppaca.idle = 0;
  234. schedule();
  235. if (cpu_is_offline(smp_processor_id()) &&
  236. system_state == SYSTEM_RUNNING)
  237. cpu_die();
  238. }
  239. return 0;
  240. }
  241. #endif /* CONFIG_PPC_PSERIES */
  242. int native_idle(void)
  243. {
  244. while(1) {
  245. /* check CPU type here */
  246. if (!need_resched())
  247. power4_idle();
  248. if (need_resched())
  249. schedule();
  250. if (cpu_is_offline(raw_smp_processor_id()) &&
  251. system_state == SYSTEM_RUNNING)
  252. cpu_die();
  253. }
  254. return 0;
  255. }
  256. #endif /* CONFIG_PPC_ISERIES */
  257. void cpu_idle(void)
  258. {
  259. BUG_ON(NULL == ppc_md.idle_loop);
  260. ppc_md.idle_loop();
  261. }
  262. int powersave_nap;
  263. #ifdef CONFIG_SYSCTL
  264. /*
  265. * Register the sysctl to set/clear powersave_nap.
  266. */
  267. static ctl_table powersave_nap_ctl_table[]={
  268. {
  269. .ctl_name = KERN_PPC_POWERSAVE_NAP,
  270. .procname = "powersave-nap",
  271. .data = &powersave_nap,
  272. .maxlen = sizeof(int),
  273. .mode = 0644,
  274. .proc_handler = &proc_dointvec,
  275. },
  276. { 0, },
  277. };
  278. static ctl_table powersave_nap_sysctl_root[] = {
  279. { 1, "kernel", NULL, 0, 0755, powersave_nap_ctl_table, },
  280. { 0,},
  281. };
  282. static int __init
  283. register_powersave_nap_sysctl(void)
  284. {
  285. register_sysctl_table(powersave_nap_sysctl_root, 0);
  286. return 0;
  287. }
  288. __initcall(register_powersave_nap_sysctl);
  289. #endif
  290. int idle_setup(void)
  291. {
  292. /*
  293. * Move that junk to each platform specific file, eventually define
  294. * a pSeries_idle for shared processor stuff
  295. */
  296. #ifdef CONFIG_PPC_ISERIES
  297. idle_loop = iSeries_idle;
  298. return 1;
  299. #else
  300. idle_loop = default_idle;
  301. #endif
  302. #ifdef CONFIG_PPC_PSERIES
  303. if (systemcfg->platform & PLATFORM_PSERIES) {
  304. if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
  305. if (get_paca()->lppaca.shared_proc) {
  306. printk(KERN_INFO "Using shared processor idle loop\n");
  307. idle_loop = shared_idle;
  308. } else {
  309. printk(KERN_INFO "Using dedicated idle loop\n");
  310. idle_loop = dedicated_idle;
  311. }
  312. } else {
  313. printk(KERN_INFO "Using default idle loop\n");
  314. idle_loop = default_idle;
  315. }
  316. }
  317. #endif /* CONFIG_PPC_PSERIES */
  318. #ifndef CONFIG_PPC_ISERIES
  319. if (systemcfg->platform == PLATFORM_POWERMAC ||
  320. systemcfg->platform == PLATFORM_MAPLE) {
  321. printk(KERN_INFO "Using native/NAP idle loop\n");
  322. idle_loop = native_idle;
  323. }
  324. #endif /* CONFIG_PPC_ISERIES */
  325. return 1;
  326. }