cpuidle-pseries.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_add_cpu_notifier(struct notifier_block *n,
  146. unsigned long action, void *hcpu)
  147. {
  148. int hotcpu = (unsigned long)hcpu;
  149. struct cpuidle_device *dev =
  150. per_cpu(cpuidle_devices, hotcpu);
  151. if (dev && cpuidle_get_driver()) {
  152. switch (action) {
  153. case CPU_ONLINE:
  154. case CPU_ONLINE_FROZEN:
  155. cpuidle_pause_and_lock();
  156. cpuidle_enable_device(dev);
  157. cpuidle_resume_and_unlock();
  158. break;
  159. case CPU_DEAD:
  160. case CPU_DEAD_FROZEN:
  161. cpuidle_pause_and_lock();
  162. cpuidle_disable_device(dev);
  163. cpuidle_resume_and_unlock();
  164. break;
  165. default:
  166. return NOTIFY_DONE;
  167. }
  168. }
  169. return NOTIFY_OK;
  170. }
  171. static struct notifier_block setup_hotplug_notifier = {
  172. .notifier_call = pseries_cpuidle_add_cpu_notifier,
  173. };
  174. /*
  175. * pseries_cpuidle_driver_init()
  176. */
  177. static int pseries_cpuidle_driver_init(void)
  178. {
  179. int idle_state;
  180. struct cpuidle_driver *drv = &pseries_idle_driver;
  181. drv->state_count = 0;
  182. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  183. /* Is the state not enabled? */
  184. if (cpuidle_state_table[idle_state].enter == NULL)
  185. continue;
  186. drv->states[drv->state_count] = /* structure copy */
  187. cpuidle_state_table[idle_state];
  188. drv->state_count += 1;
  189. }
  190. return 0;
  191. }
  192. /*
  193. * pseries_idle_probe()
  194. * Choose state table for shared versus dedicated partition
  195. */
  196. static int pseries_idle_probe(void)
  197. {
  198. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  199. return -ENODEV;
  200. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  201. /*
  202. * Use local_paca instead of get_lppaca() since
  203. * preemption is not disabled, and it is not required in
  204. * fact, since lppaca_ptr does not need to be the value
  205. * associated to the current CPU, it can be from any CPU.
  206. */
  207. if (lppaca_shared_proc(local_paca->lppaca_ptr)) {
  208. cpuidle_state_table = shared_states;
  209. max_idle_state = ARRAY_SIZE(shared_states);
  210. } else {
  211. cpuidle_state_table = dedicated_states;
  212. max_idle_state = ARRAY_SIZE(dedicated_states);
  213. }
  214. } else
  215. return -ENODEV;
  216. if (max_idle_state > 1) {
  217. snooze_timeout_en = true;
  218. snooze_timeout = cpuidle_state_table[1].target_residency *
  219. tb_ticks_per_usec;
  220. }
  221. return 0;
  222. }
  223. static int __init pseries_processor_idle_init(void)
  224. {
  225. int retval;
  226. retval = pseries_idle_probe();
  227. if (retval)
  228. return retval;
  229. pseries_cpuidle_driver_init();
  230. retval = cpuidle_register(&pseries_idle_driver, NULL);
  231. if (retval) {
  232. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  233. return retval;
  234. }
  235. register_cpu_notifier(&setup_hotplug_notifier);
  236. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  237. return 0;
  238. }
  239. device_initcall(pseries_processor_idle_init);