cpuidle-pseries.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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;
  25. static struct cpuidle_state *cpuidle_state_table;
  26. static u64 snooze_timeout;
  27. static bool snooze_timeout_en;
  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. idle_loop_prolog(&in_purr);
  56. local_irq_enable();
  57. set_thread_flag(TIF_POLLING_NRFLAG);
  58. snooze_exit_time = get_tb() + snooze_timeout;
  59. while (!need_resched()) {
  60. HMT_low();
  61. HMT_very_low();
  62. if (snooze_timeout_en && get_tb() > snooze_exit_time)
  63. break;
  64. }
  65. HMT_medium();
  66. clear_thread_flag(TIF_POLLING_NRFLAG);
  67. smp_mb();
  68. idle_loop_epilog(in_purr);
  69. return index;
  70. }
  71. static void check_and_cede_processor(void)
  72. {
  73. /*
  74. * Ensure our interrupt state is properly tracked,
  75. * also checks if no interrupt has occurred while we
  76. * were soft-disabled
  77. */
  78. if (prep_irq_for_idle()) {
  79. cede_processor();
  80. #ifdef CONFIG_TRACE_IRQFLAGS
  81. /* Ensure that H_CEDE returns with IRQs on */
  82. if (WARN_ON(!(mfmsr() & MSR_EE)))
  83. __hard_irq_enable();
  84. #endif
  85. }
  86. }
  87. static int dedicated_cede_loop(struct cpuidle_device *dev,
  88. struct cpuidle_driver *drv,
  89. int index)
  90. {
  91. unsigned long in_purr;
  92. idle_loop_prolog(&in_purr);
  93. get_lppaca()->donate_dedicated_cpu = 1;
  94. HMT_medium();
  95. check_and_cede_processor();
  96. get_lppaca()->donate_dedicated_cpu = 0;
  97. idle_loop_epilog(in_purr);
  98. return index;
  99. }
  100. static int shared_cede_loop(struct cpuidle_device *dev,
  101. struct cpuidle_driver *drv,
  102. int index)
  103. {
  104. unsigned long in_purr;
  105. idle_loop_prolog(&in_purr);
  106. /*
  107. * Yield the processor to the hypervisor. We return if
  108. * an external interrupt occurs (which are driven prior
  109. * to returning here) or if a prod occurs from another
  110. * processor. When returning here, external interrupts
  111. * are enabled.
  112. */
  113. check_and_cede_processor();
  114. idle_loop_epilog(in_purr);
  115. return index;
  116. }
  117. /*
  118. * States for dedicated partition case.
  119. */
  120. static struct cpuidle_state dedicated_states[] = {
  121. { /* Snooze */
  122. .name = "snooze",
  123. .desc = "snooze",
  124. .exit_latency = 0,
  125. .target_residency = 0,
  126. .enter = &snooze_loop },
  127. { /* CEDE */
  128. .name = "CEDE",
  129. .desc = "CEDE",
  130. .exit_latency = 10,
  131. .target_residency = 100,
  132. .enter = &dedicated_cede_loop },
  133. };
  134. /*
  135. * States for shared partition case.
  136. */
  137. static struct cpuidle_state shared_states[] = {
  138. { /* Shared Cede */
  139. .name = "Shared Cede",
  140. .desc = "Shared Cede",
  141. .exit_latency = 0,
  142. .target_residency = 0,
  143. .enter = &shared_cede_loop },
  144. };
  145. static int pseries_cpuidle_cpu_online(unsigned int cpu)
  146. {
  147. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  148. if (dev && cpuidle_get_driver()) {
  149. cpuidle_pause_and_lock();
  150. cpuidle_enable_device(dev);
  151. cpuidle_resume_and_unlock();
  152. }
  153. return 0;
  154. }
  155. static int pseries_cpuidle_cpu_dead(unsigned int cpu)
  156. {
  157. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  158. if (dev && cpuidle_get_driver()) {
  159. cpuidle_pause_and_lock();
  160. cpuidle_disable_device(dev);
  161. cpuidle_resume_and_unlock();
  162. }
  163. return 0;
  164. }
  165. /*
  166. * pseries_cpuidle_driver_init()
  167. */
  168. static int pseries_cpuidle_driver_init(void)
  169. {
  170. int idle_state;
  171. struct cpuidle_driver *drv = &pseries_idle_driver;
  172. drv->state_count = 0;
  173. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  174. /* Is the state not enabled? */
  175. if (cpuidle_state_table[idle_state].enter == NULL)
  176. continue;
  177. drv->states[drv->state_count] = /* structure copy */
  178. cpuidle_state_table[idle_state];
  179. drv->state_count += 1;
  180. }
  181. return 0;
  182. }
  183. /*
  184. * pseries_idle_probe()
  185. * Choose state table for shared versus dedicated partition
  186. */
  187. static int pseries_idle_probe(void)
  188. {
  189. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  190. return -ENODEV;
  191. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  192. if (lppaca_shared_proc(get_lppaca())) {
  193. cpuidle_state_table = shared_states;
  194. max_idle_state = ARRAY_SIZE(shared_states);
  195. } else {
  196. cpuidle_state_table = dedicated_states;
  197. max_idle_state = ARRAY_SIZE(dedicated_states);
  198. }
  199. } else
  200. return -ENODEV;
  201. if (max_idle_state > 1) {
  202. snooze_timeout_en = true;
  203. snooze_timeout = cpuidle_state_table[1].target_residency *
  204. tb_ticks_per_usec;
  205. }
  206. return 0;
  207. }
  208. static int __init pseries_processor_idle_init(void)
  209. {
  210. int retval;
  211. retval = pseries_idle_probe();
  212. if (retval)
  213. return retval;
  214. pseries_cpuidle_driver_init();
  215. retval = cpuidle_register(&pseries_idle_driver, NULL);
  216. if (retval) {
  217. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  218. return retval;
  219. }
  220. retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  221. "cpuidle/pseries:online",
  222. pseries_cpuidle_cpu_online, NULL);
  223. WARN_ON(retval < 0);
  224. retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
  225. "cpuidle/pseries:DEAD", NULL,
  226. pseries_cpuidle_cpu_dead);
  227. WARN_ON(retval < 0);
  228. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  229. return 0;
  230. }
  231. device_initcall(pseries_processor_idle_init);