cpuidle-pseries.c 6.5 KB

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