cpuidle-pseries.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * cpuidle-pseries - idle state cpuidle driver.
  3. * Adapted from drivers/idle/intel_idle.c and
  4. * drivers/acpi/processor_idle.c
  5. *
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/cpu.h>
  13. #include <linux/notifier.h>
  14. #include <asm/paca.h>
  15. #include <asm/reg.h>
  16. #include <asm/machdep.h>
  17. #include <asm/firmware.h>
  18. #include <asm/runlatch.h>
  19. #include <asm/plpar_wrappers.h>
  20. struct cpuidle_driver pseries_idle_driver = {
  21. .name = "pseries_idle",
  22. .owner = THIS_MODULE,
  23. };
  24. static int max_idle_state __read_mostly;
  25. static struct cpuidle_state *cpuidle_state_table __read_mostly;
  26. static u64 snooze_timeout __read_mostly;
  27. static bool snooze_timeout_en __read_mostly;
  28. static inline void idle_loop_prolog(unsigned long *in_purr)
  29. {
  30. ppc64_runlatch_off();
  31. *in_purr = mfspr(SPRN_PURR);
  32. /*
  33. * Indicate to the HV that we are idle. Now would be
  34. * a good time to find other work to dispatch.
  35. */
  36. get_lppaca()->idle = 1;
  37. }
  38. static inline void idle_loop_epilog(unsigned long in_purr)
  39. {
  40. u64 wait_cycles;
  41. wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
  42. wait_cycles += mfspr(SPRN_PURR) - in_purr;
  43. get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
  44. get_lppaca()->idle = 0;
  45. if (irqs_disabled())
  46. local_irq_enable();
  47. ppc64_runlatch_on();
  48. }
  49. static int snooze_loop(struct cpuidle_device *dev,
  50. struct cpuidle_driver *drv,
  51. int index)
  52. {
  53. unsigned long in_purr;
  54. u64 snooze_exit_time;
  55. set_thread_flag(TIF_POLLING_NRFLAG);
  56. idle_loop_prolog(&in_purr);
  57. local_irq_enable();
  58. snooze_exit_time = get_tb() + snooze_timeout;
  59. while (!need_resched()) {
  60. HMT_low();
  61. HMT_very_low();
  62. if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
  63. /*
  64. * Task has not woken up but we are exiting the polling
  65. * loop anyway. Require a barrier after polling is
  66. * cleared to order subsequent test of need_resched().
  67. */
  68. clear_thread_flag(TIF_POLLING_NRFLAG);
  69. smp_mb();
  70. break;
  71. }
  72. }
  73. HMT_medium();
  74. clear_thread_flag(TIF_POLLING_NRFLAG);
  75. idle_loop_epilog(in_purr);
  76. return index;
  77. }
  78. static void check_and_cede_processor(void)
  79. {
  80. /*
  81. * Ensure our interrupt state is properly tracked,
  82. * also checks if no interrupt has occurred while we
  83. * were soft-disabled
  84. */
  85. if (prep_irq_for_idle()) {
  86. cede_processor();
  87. #ifdef CONFIG_TRACE_IRQFLAGS
  88. /* Ensure that H_CEDE returns with IRQs on */
  89. if (WARN_ON(!(mfmsr() & MSR_EE)))
  90. __hard_irq_enable();
  91. #endif
  92. }
  93. }
  94. static int dedicated_cede_loop(struct cpuidle_device *dev,
  95. struct cpuidle_driver *drv,
  96. int index)
  97. {
  98. unsigned long in_purr;
  99. idle_loop_prolog(&in_purr);
  100. get_lppaca()->donate_dedicated_cpu = 1;
  101. HMT_medium();
  102. check_and_cede_processor();
  103. get_lppaca()->donate_dedicated_cpu = 0;
  104. idle_loop_epilog(in_purr);
  105. return index;
  106. }
  107. static int shared_cede_loop(struct cpuidle_device *dev,
  108. struct cpuidle_driver *drv,
  109. int index)
  110. {
  111. unsigned long in_purr;
  112. idle_loop_prolog(&in_purr);
  113. /*
  114. * Yield the processor to the hypervisor. We return if
  115. * an external interrupt occurs (which are driven prior
  116. * to returning here) or if a prod occurs from another
  117. * processor. When returning here, external interrupts
  118. * are enabled.
  119. */
  120. check_and_cede_processor();
  121. idle_loop_epilog(in_purr);
  122. return index;
  123. }
  124. /*
  125. * States for dedicated partition case.
  126. */
  127. static struct cpuidle_state dedicated_states[] = {
  128. { /* Snooze */
  129. .name = "snooze",
  130. .desc = "snooze",
  131. .exit_latency = 0,
  132. .target_residency = 0,
  133. .enter = &snooze_loop },
  134. { /* CEDE */
  135. .name = "CEDE",
  136. .desc = "CEDE",
  137. .exit_latency = 10,
  138. .target_residency = 100,
  139. .enter = &dedicated_cede_loop },
  140. };
  141. /*
  142. * States for shared partition case.
  143. */
  144. static struct cpuidle_state shared_states[] = {
  145. { /* Shared Cede */
  146. .name = "Shared Cede",
  147. .desc = "Shared Cede",
  148. .exit_latency = 0,
  149. .target_residency = 0,
  150. .enter = &shared_cede_loop },
  151. };
  152. static int pseries_cpuidle_cpu_online(unsigned int cpu)
  153. {
  154. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  155. if (dev && cpuidle_get_driver()) {
  156. cpuidle_pause_and_lock();
  157. cpuidle_enable_device(dev);
  158. cpuidle_resume_and_unlock();
  159. }
  160. return 0;
  161. }
  162. static int pseries_cpuidle_cpu_dead(unsigned int cpu)
  163. {
  164. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  165. if (dev && cpuidle_get_driver()) {
  166. cpuidle_pause_and_lock();
  167. cpuidle_disable_device(dev);
  168. cpuidle_resume_and_unlock();
  169. }
  170. return 0;
  171. }
  172. /*
  173. * pseries_cpuidle_driver_init()
  174. */
  175. static int pseries_cpuidle_driver_init(void)
  176. {
  177. int idle_state;
  178. struct cpuidle_driver *drv = &pseries_idle_driver;
  179. drv->state_count = 0;
  180. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  181. /* Is the state not enabled? */
  182. if (cpuidle_state_table[idle_state].enter == NULL)
  183. continue;
  184. drv->states[drv->state_count] = /* structure copy */
  185. cpuidle_state_table[idle_state];
  186. drv->state_count += 1;
  187. }
  188. return 0;
  189. }
  190. /*
  191. * pseries_idle_probe()
  192. * Choose state table for shared versus dedicated partition
  193. */
  194. static int pseries_idle_probe(void)
  195. {
  196. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  197. return -ENODEV;
  198. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  199. if (lppaca_shared_proc(get_lppaca())) {
  200. cpuidle_state_table = shared_states;
  201. max_idle_state = ARRAY_SIZE(shared_states);
  202. } else {
  203. cpuidle_state_table = dedicated_states;
  204. max_idle_state = ARRAY_SIZE(dedicated_states);
  205. }
  206. } else
  207. return -ENODEV;
  208. if (max_idle_state > 1) {
  209. snooze_timeout_en = true;
  210. snooze_timeout = cpuidle_state_table[1].target_residency *
  211. tb_ticks_per_usec;
  212. }
  213. return 0;
  214. }
  215. static int __init pseries_processor_idle_init(void)
  216. {
  217. int retval;
  218. retval = pseries_idle_probe();
  219. if (retval)
  220. return retval;
  221. pseries_cpuidle_driver_init();
  222. retval = cpuidle_register(&pseries_idle_driver, NULL);
  223. if (retval) {
  224. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  225. return retval;
  226. }
  227. retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  228. "cpuidle/pseries:online",
  229. pseries_cpuidle_cpu_online, NULL);
  230. WARN_ON(retval < 0);
  231. retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
  232. "cpuidle/pseries:DEAD", NULL,
  233. pseries_cpuidle_cpu_dead);
  234. WARN_ON(retval < 0);
  235. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  236. return 0;
  237. }
  238. device_initcall(pseries_processor_idle_init);