cpuidle-pseries.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. ppc64_runlatch_on();
  47. }
  48. static int snooze_loop(struct cpuidle_device *dev,
  49. struct cpuidle_driver *drv,
  50. int index)
  51. {
  52. unsigned long in_purr;
  53. u64 snooze_exit_time;
  54. set_thread_flag(TIF_POLLING_NRFLAG);
  55. idle_loop_prolog(&in_purr);
  56. local_irq_enable();
  57. snooze_exit_time = get_tb() + snooze_timeout;
  58. while (!need_resched()) {
  59. HMT_low();
  60. HMT_very_low();
  61. if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
  62. /*
  63. * Task has not woken up but we are exiting the polling
  64. * loop anyway. Require a barrier after polling is
  65. * cleared to order subsequent test of need_resched().
  66. */
  67. clear_thread_flag(TIF_POLLING_NRFLAG);
  68. smp_mb();
  69. break;
  70. }
  71. }
  72. HMT_medium();
  73. clear_thread_flag(TIF_POLLING_NRFLAG);
  74. local_irq_disable();
  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. local_irq_disable();
  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. local_irq_disable();
  123. idle_loop_epilog(in_purr);
  124. return index;
  125. }
  126. /*
  127. * States for dedicated partition case.
  128. */
  129. static struct cpuidle_state dedicated_states[] = {
  130. { /* Snooze */
  131. .name = "snooze",
  132. .desc = "snooze",
  133. .exit_latency = 0,
  134. .target_residency = 0,
  135. .enter = &snooze_loop },
  136. { /* CEDE */
  137. .name = "CEDE",
  138. .desc = "CEDE",
  139. .exit_latency = 10,
  140. .target_residency = 100,
  141. .enter = &dedicated_cede_loop },
  142. };
  143. /*
  144. * States for shared partition case.
  145. */
  146. static struct cpuidle_state shared_states[] = {
  147. { /* Snooze */
  148. .name = "snooze",
  149. .desc = "snooze",
  150. .exit_latency = 0,
  151. .target_residency = 0,
  152. .enter = &snooze_loop },
  153. { /* Shared Cede */
  154. .name = "Shared Cede",
  155. .desc = "Shared Cede",
  156. .exit_latency = 10,
  157. .target_residency = 100,
  158. .enter = &shared_cede_loop },
  159. };
  160. static int pseries_cpuidle_cpu_online(unsigned int cpu)
  161. {
  162. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  163. if (dev && cpuidle_get_driver()) {
  164. cpuidle_pause_and_lock();
  165. cpuidle_enable_device(dev);
  166. cpuidle_resume_and_unlock();
  167. }
  168. return 0;
  169. }
  170. static int pseries_cpuidle_cpu_dead(unsigned int cpu)
  171. {
  172. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  173. if (dev && cpuidle_get_driver()) {
  174. cpuidle_pause_and_lock();
  175. cpuidle_disable_device(dev);
  176. cpuidle_resume_and_unlock();
  177. }
  178. return 0;
  179. }
  180. /*
  181. * pseries_cpuidle_driver_init()
  182. */
  183. static int pseries_cpuidle_driver_init(void)
  184. {
  185. int idle_state;
  186. struct cpuidle_driver *drv = &pseries_idle_driver;
  187. drv->state_count = 0;
  188. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  189. /* Is the state not enabled? */
  190. if (cpuidle_state_table[idle_state].enter == NULL)
  191. continue;
  192. drv->states[drv->state_count] = /* structure copy */
  193. cpuidle_state_table[idle_state];
  194. drv->state_count += 1;
  195. }
  196. return 0;
  197. }
  198. /*
  199. * pseries_idle_probe()
  200. * Choose state table for shared versus dedicated partition
  201. */
  202. static int pseries_idle_probe(void)
  203. {
  204. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  205. return -ENODEV;
  206. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  207. /*
  208. * Use local_paca instead of get_lppaca() since
  209. * preemption is not disabled, and it is not required in
  210. * fact, since lppaca_ptr does not need to be the value
  211. * associated to the current CPU, it can be from any CPU.
  212. */
  213. if (lppaca_shared_proc(local_paca->lppaca_ptr)) {
  214. cpuidle_state_table = shared_states;
  215. max_idle_state = ARRAY_SIZE(shared_states);
  216. } else {
  217. cpuidle_state_table = dedicated_states;
  218. max_idle_state = ARRAY_SIZE(dedicated_states);
  219. }
  220. } else
  221. return -ENODEV;
  222. if (max_idle_state > 1) {
  223. snooze_timeout_en = true;
  224. snooze_timeout = cpuidle_state_table[1].target_residency *
  225. tb_ticks_per_usec;
  226. }
  227. return 0;
  228. }
  229. static int __init pseries_processor_idle_init(void)
  230. {
  231. int retval;
  232. retval = pseries_idle_probe();
  233. if (retval)
  234. return retval;
  235. pseries_cpuidle_driver_init();
  236. retval = cpuidle_register(&pseries_idle_driver, NULL);
  237. if (retval) {
  238. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  239. return retval;
  240. }
  241. retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  242. "cpuidle/pseries:online",
  243. pseries_cpuidle_cpu_online, NULL);
  244. WARN_ON(retval < 0);
  245. retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
  246. "cpuidle/pseries:DEAD", NULL,
  247. pseries_cpuidle_cpu_dead);
  248. WARN_ON(retval < 0);
  249. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  250. return 0;
  251. }
  252. device_initcall(pseries_processor_idle_init);