cpuidle-pseries.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 inline void idle_loop_prolog(unsigned long *in_purr)
  27. {
  28. ppc64_runlatch_off();
  29. *in_purr = mfspr(SPRN_PURR);
  30. /*
  31. * Indicate to the HV that we are idle. Now would be
  32. * a good time to find other work to dispatch.
  33. */
  34. get_lppaca()->idle = 1;
  35. }
  36. static inline void idle_loop_epilog(unsigned long in_purr)
  37. {
  38. u64 wait_cycles;
  39. wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
  40. wait_cycles += mfspr(SPRN_PURR) - in_purr;
  41. get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
  42. get_lppaca()->idle = 0;
  43. if (irqs_disabled())
  44. local_irq_enable();
  45. ppc64_runlatch_on();
  46. }
  47. static int snooze_loop(struct cpuidle_device *dev,
  48. struct cpuidle_driver *drv,
  49. int index)
  50. {
  51. unsigned long in_purr;
  52. idle_loop_prolog(&in_purr);
  53. local_irq_enable();
  54. set_thread_flag(TIF_POLLING_NRFLAG);
  55. while (!need_resched()) {
  56. HMT_low();
  57. HMT_very_low();
  58. }
  59. HMT_medium();
  60. clear_thread_flag(TIF_POLLING_NRFLAG);
  61. smp_mb();
  62. idle_loop_epilog(in_purr);
  63. return index;
  64. }
  65. static void check_and_cede_processor(void)
  66. {
  67. /*
  68. * Ensure our interrupt state is properly tracked,
  69. * also checks if no interrupt has occurred while we
  70. * were soft-disabled
  71. */
  72. if (prep_irq_for_idle()) {
  73. cede_processor();
  74. #ifdef CONFIG_TRACE_IRQFLAGS
  75. /* Ensure that H_CEDE returns with IRQs on */
  76. if (WARN_ON(!(mfmsr() & MSR_EE)))
  77. __hard_irq_enable();
  78. #endif
  79. }
  80. }
  81. static int dedicated_cede_loop(struct cpuidle_device *dev,
  82. struct cpuidle_driver *drv,
  83. int index)
  84. {
  85. unsigned long in_purr;
  86. idle_loop_prolog(&in_purr);
  87. get_lppaca()->donate_dedicated_cpu = 1;
  88. HMT_medium();
  89. check_and_cede_processor();
  90. get_lppaca()->donate_dedicated_cpu = 0;
  91. idle_loop_epilog(in_purr);
  92. return index;
  93. }
  94. static int shared_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. /*
  101. * Yield the processor to the hypervisor. We return if
  102. * an external interrupt occurs (which are driven prior
  103. * to returning here) or if a prod occurs from another
  104. * processor. When returning here, external interrupts
  105. * are enabled.
  106. */
  107. check_and_cede_processor();
  108. idle_loop_epilog(in_purr);
  109. return index;
  110. }
  111. /*
  112. * States for dedicated partition case.
  113. */
  114. static struct cpuidle_state dedicated_states[] = {
  115. { /* Snooze */
  116. .name = "snooze",
  117. .desc = "snooze",
  118. .exit_latency = 0,
  119. .target_residency = 0,
  120. .enter = &snooze_loop },
  121. { /* CEDE */
  122. .name = "CEDE",
  123. .desc = "CEDE",
  124. .exit_latency = 10,
  125. .target_residency = 100,
  126. .enter = &dedicated_cede_loop },
  127. };
  128. /*
  129. * States for shared partition case.
  130. */
  131. static struct cpuidle_state shared_states[] = {
  132. { /* Shared Cede */
  133. .name = "Shared Cede",
  134. .desc = "Shared Cede",
  135. .exit_latency = 0,
  136. .target_residency = 0,
  137. .enter = &shared_cede_loop },
  138. };
  139. static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
  140. unsigned long action, void *hcpu)
  141. {
  142. int hotcpu = (unsigned long)hcpu;
  143. struct cpuidle_device *dev =
  144. per_cpu(cpuidle_devices, hotcpu);
  145. if (dev && cpuidle_get_driver()) {
  146. switch (action) {
  147. case CPU_ONLINE:
  148. case CPU_ONLINE_FROZEN:
  149. cpuidle_pause_and_lock();
  150. cpuidle_enable_device(dev);
  151. cpuidle_resume_and_unlock();
  152. break;
  153. case CPU_DEAD:
  154. case CPU_DEAD_FROZEN:
  155. cpuidle_pause_and_lock();
  156. cpuidle_disable_device(dev);
  157. cpuidle_resume_and_unlock();
  158. break;
  159. default:
  160. return NOTIFY_DONE;
  161. }
  162. }
  163. return NOTIFY_OK;
  164. }
  165. static struct notifier_block setup_hotplug_notifier = {
  166. .notifier_call = pseries_cpuidle_add_cpu_notifier,
  167. };
  168. /*
  169. * pseries_cpuidle_driver_init()
  170. */
  171. static int pseries_cpuidle_driver_init(void)
  172. {
  173. int idle_state;
  174. struct cpuidle_driver *drv = &pseries_idle_driver;
  175. drv->state_count = 0;
  176. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  177. /* Is the state not enabled? */
  178. if (cpuidle_state_table[idle_state].enter == NULL)
  179. continue;
  180. drv->states[drv->state_count] = /* structure copy */
  181. cpuidle_state_table[idle_state];
  182. drv->state_count += 1;
  183. }
  184. return 0;
  185. }
  186. /*
  187. * pseries_idle_probe()
  188. * Choose state table for shared versus dedicated partition
  189. */
  190. static int pseries_idle_probe(void)
  191. {
  192. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  193. return -ENODEV;
  194. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  195. if (lppaca_shared_proc(get_lppaca())) {
  196. cpuidle_state_table = shared_states;
  197. max_idle_state = ARRAY_SIZE(shared_states);
  198. } else {
  199. cpuidle_state_table = dedicated_states;
  200. max_idle_state = ARRAY_SIZE(dedicated_states);
  201. }
  202. } else
  203. return -ENODEV;
  204. return 0;
  205. }
  206. static int __init pseries_processor_idle_init(void)
  207. {
  208. int retval;
  209. retval = pseries_idle_probe();
  210. if (retval)
  211. return retval;
  212. pseries_cpuidle_driver_init();
  213. retval = cpuidle_register(&pseries_idle_driver, NULL);
  214. if (retval) {
  215. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  216. return retval;
  217. }
  218. register_cpu_notifier(&setup_hotplug_notifier);
  219. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  220. return 0;
  221. }
  222. device_initcall(pseries_processor_idle_init);