processor_idle.c 7.1 KB

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