processor_idle.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. * Interrupts are soft-disabled at this point,
  85. * but not hard disabled. So an interrupt might have
  86. * occurred before entering NAP, and would be potentially
  87. * lost (edge events, decrementer events, etc...) unless
  88. * we first hard disable then check.
  89. */
  90. hard_irq_disable();
  91. if (get_paca()->irq_happened == 0)
  92. cede_processor();
  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. ktime_t kt_before;
  100. idle_loop_prolog(&in_purr, &kt_before);
  101. get_lppaca()->donate_dedicated_cpu = 1;
  102. ppc64_runlatch_off();
  103. HMT_medium();
  104. check_and_cede_processor();
  105. get_lppaca()->donate_dedicated_cpu = 0;
  106. dev->last_residency =
  107. (int)idle_loop_epilog(in_purr, kt_before);
  108. return index;
  109. }
  110. static int shared_cede_loop(struct cpuidle_device *dev,
  111. struct cpuidle_driver *drv,
  112. int index)
  113. {
  114. unsigned long in_purr;
  115. ktime_t kt_before;
  116. idle_loop_prolog(&in_purr, &kt_before);
  117. /*
  118. * Yield the processor to the hypervisor. We return if
  119. * an external interrupt occurs (which are driven prior
  120. * to returning here) or if a prod occurs from another
  121. * processor. When returning here, external interrupts
  122. * are enabled.
  123. */
  124. check_and_cede_processor();
  125. dev->last_residency =
  126. (int)idle_loop_epilog(in_purr, kt_before);
  127. return index;
  128. }
  129. /*
  130. * States for dedicated partition case.
  131. */
  132. static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
  133. { /* Snooze */
  134. .name = "snooze",
  135. .desc = "snooze",
  136. .flags = CPUIDLE_FLAG_TIME_VALID,
  137. .exit_latency = 0,
  138. .target_residency = 0,
  139. .enter = &snooze_loop },
  140. { /* CEDE */
  141. .name = "CEDE",
  142. .desc = "CEDE",
  143. .flags = CPUIDLE_FLAG_TIME_VALID,
  144. .exit_latency = 1,
  145. .target_residency = 10,
  146. .enter = &dedicated_cede_loop },
  147. };
  148. /*
  149. * States for shared partition case.
  150. */
  151. static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
  152. { /* Shared Cede */
  153. .name = "Shared Cede",
  154. .desc = "Shared Cede",
  155. .flags = CPUIDLE_FLAG_TIME_VALID,
  156. .exit_latency = 0,
  157. .target_residency = 0,
  158. .enter = &shared_cede_loop },
  159. };
  160. int pseries_notify_cpuidle_add_cpu(int cpu)
  161. {
  162. struct cpuidle_device *dev =
  163. per_cpu_ptr(pseries_cpuidle_devices, cpu);
  164. if (dev && cpuidle_get_driver()) {
  165. cpuidle_disable_device(dev);
  166. cpuidle_enable_device(dev);
  167. }
  168. return 0;
  169. }
  170. /*
  171. * pseries_cpuidle_driver_init()
  172. */
  173. static int pseries_cpuidle_driver_init(void)
  174. {
  175. int idle_state;
  176. struct cpuidle_driver *drv = &pseries_idle_driver;
  177. drv->state_count = 0;
  178. for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
  179. if (idle_state > max_idle_state)
  180. break;
  181. /* is the state not enabled? */
  182. if (cpuidle_state_table[idle_state].enter == NULL)
  183. continue;
  184. drv->states[drv->state_count] = /* structure copy */
  185. cpuidle_state_table[idle_state];
  186. if (cpuidle_state_table == dedicated_states)
  187. drv->states[drv->state_count].target_residency =
  188. __get_cpu_var(smt_snooze_delay);
  189. drv->state_count += 1;
  190. }
  191. return 0;
  192. }
  193. /* pseries_idle_devices_uninit(void)
  194. * unregister cpuidle devices and de-allocate memory
  195. */
  196. static void pseries_idle_devices_uninit(void)
  197. {
  198. int i;
  199. struct cpuidle_device *dev;
  200. for_each_possible_cpu(i) {
  201. dev = per_cpu_ptr(pseries_cpuidle_devices, i);
  202. cpuidle_unregister_device(dev);
  203. }
  204. free_percpu(pseries_cpuidle_devices);
  205. return;
  206. }
  207. /* pseries_idle_devices_init()
  208. * allocate, initialize and register cpuidle device
  209. */
  210. static int pseries_idle_devices_init(void)
  211. {
  212. int i;
  213. struct cpuidle_driver *drv = &pseries_idle_driver;
  214. struct cpuidle_device *dev;
  215. pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  216. if (pseries_cpuidle_devices == NULL)
  217. return -ENOMEM;
  218. for_each_possible_cpu(i) {
  219. dev = per_cpu_ptr(pseries_cpuidle_devices, i);
  220. dev->state_count = drv->state_count;
  221. dev->cpu = i;
  222. if (cpuidle_register_device(dev)) {
  223. printk(KERN_DEBUG \
  224. "cpuidle_register_device %d failed!\n", i);
  225. return -EIO;
  226. }
  227. }
  228. return 0;
  229. }
  230. /*
  231. * pseries_idle_probe()
  232. * Choose state table for shared versus dedicated partition
  233. */
  234. static int pseries_idle_probe(void)
  235. {
  236. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  237. return -ENODEV;
  238. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  239. return -ENODEV;
  240. if (max_idle_state == 0) {
  241. printk(KERN_DEBUG "pseries processor idle disabled.\n");
  242. return -EPERM;
  243. }
  244. if (get_lppaca()->shared_proc)
  245. cpuidle_state_table = shared_states;
  246. else
  247. cpuidle_state_table = dedicated_states;
  248. return 0;
  249. }
  250. static int __init pseries_processor_idle_init(void)
  251. {
  252. int retval;
  253. retval = pseries_idle_probe();
  254. if (retval)
  255. return retval;
  256. pseries_cpuidle_driver_init();
  257. retval = cpuidle_register_driver(&pseries_idle_driver);
  258. if (retval) {
  259. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  260. return retval;
  261. }
  262. retval = pseries_idle_devices_init();
  263. if (retval) {
  264. pseries_idle_devices_uninit();
  265. cpuidle_unregister_driver(&pseries_idle_driver);
  266. return retval;
  267. }
  268. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  269. return 0;
  270. }
  271. static void __exit pseries_processor_idle_exit(void)
  272. {
  273. pseries_idle_devices_uninit();
  274. cpuidle_unregister_driver(&pseries_idle_driver);
  275. return;
  276. }
  277. module_init(pseries_processor_idle_init);
  278. module_exit(pseries_processor_idle_exit);
  279. MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
  280. MODULE_DESCRIPTION("Cpuidle driver for POWER");
  281. MODULE_LICENSE("GPL");