processor_idle.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * processor_idle - 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 <asm/paca.h>
  14. #include <asm/reg.h>
  15. #include <asm/machdep.h>
  16. #include <asm/firmware.h>
  17. #include <asm/runlatch.h>
  18. #include "plpar_wrappers.h"
  19. #include "pseries.h"
  20. struct cpuidle_driver pseries_idle_driver = {
  21. .name = "pseries_idle",
  22. .owner = THIS_MODULE,
  23. };
  24. #define MAX_IDLE_STATE_COUNT 2
  25. static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
  26. static struct cpuidle_device __percpu *pseries_cpuidle_devices;
  27. static struct cpuidle_state *cpuidle_state_table;
  28. void update_smt_snooze_delay(int snooze)
  29. {
  30. struct cpuidle_driver *drv = cpuidle_get_driver();
  31. if (drv)
  32. drv->states[0].target_residency = snooze;
  33. }
  34. static inline void idle_loop_prolog(unsigned long *in_purr, ktime_t *kt_before)
  35. {
  36. *kt_before = ktime_get_real();
  37. *in_purr = mfspr(SPRN_PURR);
  38. /*
  39. * Indicate to the HV that we are idle. Now would be
  40. * a good time to find other work to dispatch.
  41. */
  42. get_lppaca()->idle = 1;
  43. }
  44. static inline s64 idle_loop_epilog(unsigned long in_purr, ktime_t kt_before)
  45. {
  46. get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
  47. get_lppaca()->idle = 0;
  48. return ktime_to_us(ktime_sub(ktime_get_real(), kt_before));
  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. ktime_t kt_before;
  56. unsigned long start_snooze;
  57. long snooze = drv->states[0].target_residency;
  58. idle_loop_prolog(&in_purr, &kt_before);
  59. if (snooze) {
  60. start_snooze = get_tb() + snooze * tb_ticks_per_usec;
  61. local_irq_enable();
  62. set_thread_flag(TIF_POLLING_NRFLAG);
  63. while ((snooze < 0) || (get_tb() < start_snooze)) {
  64. if (need_resched() || cpu_is_offline(dev->cpu))
  65. goto out;
  66. ppc64_runlatch_off();
  67. HMT_low();
  68. HMT_very_low();
  69. }
  70. HMT_medium();
  71. clear_thread_flag(TIF_POLLING_NRFLAG);
  72. smp_mb();
  73. local_irq_disable();
  74. }
  75. out:
  76. HMT_medium();
  77. dev->last_residency =
  78. (int)idle_loop_epilog(in_purr, kt_before);
  79. return index;
  80. }
  81. static void check_and_cede_processor(void)
  82. {
  83. /*
  84. * Ensure our interrupt state is properly tracked,
  85. * also checks if no interrupt has occurred while we
  86. * were soft-disabled
  87. */
  88. if (prep_irq_for_idle()) {
  89. cede_processor();
  90. #ifdef CONFIG_TRACE_IRQFLAGS
  91. /* Ensure that H_CEDE returns with IRQs on */
  92. if (WARN_ON(!(mfmsr() & MSR_EE)))
  93. __hard_irq_enable();
  94. #endif
  95. }
  96. }
  97. static int dedicated_cede_loop(struct cpuidle_device *dev,
  98. struct cpuidle_driver *drv,
  99. int index)
  100. {
  101. unsigned long in_purr;
  102. ktime_t kt_before;
  103. idle_loop_prolog(&in_purr, &kt_before);
  104. get_lppaca()->donate_dedicated_cpu = 1;
  105. ppc64_runlatch_off();
  106. HMT_medium();
  107. check_and_cede_processor();
  108. get_lppaca()->donate_dedicated_cpu = 0;
  109. dev->last_residency =
  110. (int)idle_loop_epilog(in_purr, kt_before);
  111. return index;
  112. }
  113. static int shared_cede_loop(struct cpuidle_device *dev,
  114. struct cpuidle_driver *drv,
  115. int index)
  116. {
  117. unsigned long in_purr;
  118. ktime_t kt_before;
  119. idle_loop_prolog(&in_purr, &kt_before);
  120. /*
  121. * Yield the processor to the hypervisor. We return if
  122. * an external interrupt occurs (which are driven prior
  123. * to returning here) or if a prod occurs from another
  124. * processor. When returning here, external interrupts
  125. * are enabled.
  126. */
  127. check_and_cede_processor();
  128. dev->last_residency =
  129. (int)idle_loop_epilog(in_purr, kt_before);
  130. return index;
  131. }
  132. /*
  133. * States for dedicated partition case.
  134. */
  135. static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
  136. { /* Snooze */
  137. .name = "snooze",
  138. .desc = "snooze",
  139. .flags = CPUIDLE_FLAG_TIME_VALID,
  140. .exit_latency = 0,
  141. .target_residency = 0,
  142. .enter = &snooze_loop },
  143. { /* CEDE */
  144. .name = "CEDE",
  145. .desc = "CEDE",
  146. .flags = CPUIDLE_FLAG_TIME_VALID,
  147. .exit_latency = 1,
  148. .target_residency = 10,
  149. .enter = &dedicated_cede_loop },
  150. };
  151. /*
  152. * States for shared partition case.
  153. */
  154. static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
  155. { /* Shared Cede */
  156. .name = "Shared Cede",
  157. .desc = "Shared Cede",
  158. .flags = CPUIDLE_FLAG_TIME_VALID,
  159. .exit_latency = 0,
  160. .target_residency = 0,
  161. .enter = &shared_cede_loop },
  162. };
  163. int pseries_notify_cpuidle_add_cpu(int cpu)
  164. {
  165. struct cpuidle_device *dev =
  166. per_cpu_ptr(pseries_cpuidle_devices, cpu);
  167. if (dev && cpuidle_get_driver()) {
  168. cpuidle_disable_device(dev);
  169. cpuidle_enable_device(dev);
  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_COUNT; ++idle_state) {
  182. if (idle_state > max_idle_state)
  183. break;
  184. /* is the state not enabled? */
  185. if (cpuidle_state_table[idle_state].enter == NULL)
  186. continue;
  187. drv->states[drv->state_count] = /* structure copy */
  188. cpuidle_state_table[idle_state];
  189. if (cpuidle_state_table == dedicated_states)
  190. drv->states[drv->state_count].target_residency =
  191. __get_cpu_var(smt_snooze_delay);
  192. drv->state_count += 1;
  193. }
  194. return 0;
  195. }
  196. /* pseries_idle_devices_uninit(void)
  197. * unregister cpuidle devices and de-allocate memory
  198. */
  199. static void pseries_idle_devices_uninit(void)
  200. {
  201. int i;
  202. struct cpuidle_device *dev;
  203. for_each_possible_cpu(i) {
  204. dev = per_cpu_ptr(pseries_cpuidle_devices, i);
  205. cpuidle_unregister_device(dev);
  206. }
  207. free_percpu(pseries_cpuidle_devices);
  208. return;
  209. }
  210. /* pseries_idle_devices_init()
  211. * allocate, initialize and register cpuidle device
  212. */
  213. static int pseries_idle_devices_init(void)
  214. {
  215. int i;
  216. struct cpuidle_driver *drv = &pseries_idle_driver;
  217. struct cpuidle_device *dev;
  218. pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  219. if (pseries_cpuidle_devices == NULL)
  220. return -ENOMEM;
  221. for_each_possible_cpu(i) {
  222. dev = per_cpu_ptr(pseries_cpuidle_devices, i);
  223. dev->state_count = drv->state_count;
  224. dev->cpu = i;
  225. if (cpuidle_register_device(dev)) {
  226. printk(KERN_DEBUG \
  227. "cpuidle_register_device %d failed!\n", i);
  228. return -EIO;
  229. }
  230. }
  231. return 0;
  232. }
  233. /*
  234. * pseries_idle_probe()
  235. * Choose state table for shared versus dedicated partition
  236. */
  237. static int pseries_idle_probe(void)
  238. {
  239. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  240. return -ENODEV;
  241. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  242. return -ENODEV;
  243. if (max_idle_state == 0) {
  244. printk(KERN_DEBUG "pseries processor idle disabled.\n");
  245. return -EPERM;
  246. }
  247. if (get_lppaca()->shared_proc)
  248. cpuidle_state_table = shared_states;
  249. else
  250. cpuidle_state_table = dedicated_states;
  251. return 0;
  252. }
  253. static int __init pseries_processor_idle_init(void)
  254. {
  255. int retval;
  256. retval = pseries_idle_probe();
  257. if (retval)
  258. return retval;
  259. pseries_cpuidle_driver_init();
  260. retval = cpuidle_register_driver(&pseries_idle_driver);
  261. if (retval) {
  262. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  263. return retval;
  264. }
  265. retval = pseries_idle_devices_init();
  266. if (retval) {
  267. pseries_idle_devices_uninit();
  268. cpuidle_unregister_driver(&pseries_idle_driver);
  269. return retval;
  270. }
  271. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  272. return 0;
  273. }
  274. static void __exit pseries_processor_idle_exit(void)
  275. {
  276. pseries_idle_devices_uninit();
  277. cpuidle_unregister_driver(&pseries_idle_driver);
  278. return;
  279. }
  280. module_init(pseries_processor_idle_init);
  281. module_exit(pseries_processor_idle_exit);
  282. MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
  283. MODULE_DESCRIPTION("Cpuidle driver for POWER");
  284. MODULE_LICENSE("GPL");