idle.c 8.2 KB

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