idle.c 8.2 KB

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