idle.c 8.2 KB

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