cpuidle-pseries.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. .flags = CPUIDLE_FLAG_TIME_VALID,
  119. .exit_latency = 0,
  120. .target_residency = 0,
  121. .enter = &snooze_loop },
  122. { /* CEDE */
  123. .name = "CEDE",
  124. .desc = "CEDE",
  125. .flags = CPUIDLE_FLAG_TIME_VALID,
  126. .exit_latency = 10,
  127. .target_residency = 100,
  128. .enter = &dedicated_cede_loop },
  129. };
  130. /*
  131. * States for shared partition case.
  132. */
  133. static struct cpuidle_state shared_states[] = {
  134. { /* Shared Cede */
  135. .name = "Shared Cede",
  136. .desc = "Shared Cede",
  137. .flags = CPUIDLE_FLAG_TIME_VALID,
  138. .exit_latency = 0,
  139. .target_residency = 0,
  140. .enter = &shared_cede_loop },
  141. };
  142. static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
  143. unsigned long action, void *hcpu)
  144. {
  145. int hotcpu = (unsigned long)hcpu;
  146. struct cpuidle_device *dev =
  147. per_cpu(cpuidle_devices, hotcpu);
  148. if (dev && cpuidle_get_driver()) {
  149. switch (action) {
  150. case CPU_ONLINE:
  151. case CPU_ONLINE_FROZEN:
  152. cpuidle_pause_and_lock();
  153. cpuidle_enable_device(dev);
  154. cpuidle_resume_and_unlock();
  155. break;
  156. case CPU_DEAD:
  157. case CPU_DEAD_FROZEN:
  158. cpuidle_pause_and_lock();
  159. cpuidle_disable_device(dev);
  160. cpuidle_resume_and_unlock();
  161. break;
  162. default:
  163. return NOTIFY_DONE;
  164. }
  165. }
  166. return NOTIFY_OK;
  167. }
  168. static struct notifier_block setup_hotplug_notifier = {
  169. .notifier_call = pseries_cpuidle_add_cpu_notifier,
  170. };
  171. /*
  172. * pseries_cpuidle_driver_init()
  173. */
  174. static int pseries_cpuidle_driver_init(void)
  175. {
  176. int idle_state;
  177. struct cpuidle_driver *drv = &pseries_idle_driver;
  178. drv->state_count = 0;
  179. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  180. /* Is the state not enabled? */
  181. if (cpuidle_state_table[idle_state].enter == NULL)
  182. continue;
  183. drv->states[drv->state_count] = /* structure copy */
  184. cpuidle_state_table[idle_state];
  185. drv->state_count += 1;
  186. }
  187. return 0;
  188. }
  189. /*
  190. * pseries_idle_probe()
  191. * Choose state table for shared versus dedicated partition
  192. */
  193. static int pseries_idle_probe(void)
  194. {
  195. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  196. return -ENODEV;
  197. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  198. if (lppaca_shared_proc(get_lppaca())) {
  199. cpuidle_state_table = shared_states;
  200. max_idle_state = ARRAY_SIZE(shared_states);
  201. } else {
  202. cpuidle_state_table = dedicated_states;
  203. max_idle_state = ARRAY_SIZE(dedicated_states);
  204. }
  205. } else
  206. return -ENODEV;
  207. return 0;
  208. }
  209. static int __init pseries_processor_idle_init(void)
  210. {
  211. int retval;
  212. retval = pseries_idle_probe();
  213. if (retval)
  214. return retval;
  215. pseries_cpuidle_driver_init();
  216. retval = cpuidle_register(&pseries_idle_driver, NULL);
  217. if (retval) {
  218. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  219. return retval;
  220. }
  221. register_cpu_notifier(&setup_hotplug_notifier);
  222. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  223. return 0;
  224. }
  225. device_initcall(pseries_processor_idle_init);