processor_idle.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 <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 "plpar_wrappers.h"
  20. #include "pseries.h"
  21. struct cpuidle_driver pseries_idle_driver = {
  22. .name = "pseries_idle",
  23. .owner = THIS_MODULE,
  24. .en_core_tk_irqen = 1,
  25. };
  26. #define MAX_IDLE_STATE_COUNT 2
  27. static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
  28. static struct cpuidle_device __percpu *pseries_cpuidle_devices;
  29. static struct cpuidle_state *cpuidle_state_table;
  30. static inline void idle_loop_prolog(unsigned long *in_purr)
  31. {
  32. *in_purr = mfspr(SPRN_PURR);
  33. /*
  34. * Indicate to the HV that we are idle. Now would be
  35. * a good time to find other work to dispatch.
  36. */
  37. get_lppaca()->idle = 1;
  38. }
  39. static inline void idle_loop_epilog(unsigned long in_purr)
  40. {
  41. get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
  42. get_lppaca()->idle = 0;
  43. }
  44. static int snooze_loop(struct cpuidle_device *dev,
  45. struct cpuidle_driver *drv,
  46. int index)
  47. {
  48. unsigned long in_purr;
  49. int cpu = dev->cpu;
  50. idle_loop_prolog(&in_purr);
  51. local_irq_enable();
  52. set_thread_flag(TIF_POLLING_NRFLAG);
  53. while ((!need_resched()) && cpu_online(cpu)) {
  54. ppc64_runlatch_off();
  55. HMT_low();
  56. HMT_very_low();
  57. }
  58. HMT_medium();
  59. clear_thread_flag(TIF_POLLING_NRFLAG);
  60. smp_mb();
  61. idle_loop_epilog(in_purr);
  62. return index;
  63. }
  64. static void check_and_cede_processor(void)
  65. {
  66. /*
  67. * Ensure our interrupt state is properly tracked,
  68. * also checks if no interrupt has occurred while we
  69. * were soft-disabled
  70. */
  71. if (prep_irq_for_idle()) {
  72. cede_processor();
  73. #ifdef CONFIG_TRACE_IRQFLAGS
  74. /* Ensure that H_CEDE returns with IRQs on */
  75. if (WARN_ON(!(mfmsr() & MSR_EE)))
  76. __hard_irq_enable();
  77. #endif
  78. }
  79. }
  80. static int dedicated_cede_loop(struct cpuidle_device *dev,
  81. struct cpuidle_driver *drv,
  82. int index)
  83. {
  84. unsigned long in_purr;
  85. idle_loop_prolog(&in_purr);
  86. get_lppaca()->donate_dedicated_cpu = 1;
  87. ppc64_runlatch_off();
  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[MAX_IDLE_STATE_COUNT] = {
  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[MAX_IDLE_STATE_COUNT] = {
  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. void update_smt_snooze_delay(int cpu, int residency)
  143. {
  144. struct cpuidle_driver *drv = cpuidle_get_driver();
  145. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  146. if (cpuidle_state_table != dedicated_states)
  147. return;
  148. if (residency < 0) {
  149. /* Disable the Nap state on that cpu */
  150. if (dev)
  151. dev->states_usage[1].disable = 1;
  152. } else
  153. if (drv)
  154. drv->states[1].target_residency = residency;
  155. }
  156. static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
  157. unsigned long action, void *hcpu)
  158. {
  159. int hotcpu = (unsigned long)hcpu;
  160. struct cpuidle_device *dev =
  161. per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
  162. if (dev && cpuidle_get_driver()) {
  163. switch (action) {
  164. case CPU_ONLINE:
  165. case CPU_ONLINE_FROZEN:
  166. cpuidle_pause_and_lock();
  167. cpuidle_enable_device(dev);
  168. cpuidle_resume_and_unlock();
  169. break;
  170. case CPU_DEAD:
  171. case CPU_DEAD_FROZEN:
  172. cpuidle_pause_and_lock();
  173. cpuidle_disable_device(dev);
  174. cpuidle_resume_and_unlock();
  175. break;
  176. default:
  177. return NOTIFY_DONE;
  178. }
  179. }
  180. return NOTIFY_OK;
  181. }
  182. static struct notifier_block setup_hotplug_notifier = {
  183. .notifier_call = pseries_cpuidle_add_cpu_notifier,
  184. };
  185. /*
  186. * pseries_cpuidle_driver_init()
  187. */
  188. static int pseries_cpuidle_driver_init(void)
  189. {
  190. int idle_state;
  191. struct cpuidle_driver *drv = &pseries_idle_driver;
  192. drv->state_count = 0;
  193. for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
  194. if (idle_state > max_idle_state)
  195. break;
  196. /* is the state not enabled? */
  197. if (cpuidle_state_table[idle_state].enter == NULL)
  198. continue;
  199. drv->states[drv->state_count] = /* structure copy */
  200. cpuidle_state_table[idle_state];
  201. drv->state_count += 1;
  202. }
  203. return 0;
  204. }
  205. /* pseries_idle_devices_uninit(void)
  206. * unregister cpuidle devices and de-allocate memory
  207. */
  208. static void pseries_idle_devices_uninit(void)
  209. {
  210. int i;
  211. struct cpuidle_device *dev;
  212. for_each_possible_cpu(i) {
  213. dev = per_cpu_ptr(pseries_cpuidle_devices, i);
  214. cpuidle_unregister_device(dev);
  215. }
  216. free_percpu(pseries_cpuidle_devices);
  217. return;
  218. }
  219. /* pseries_idle_devices_init()
  220. * allocate, initialize and register cpuidle device
  221. */
  222. static int pseries_idle_devices_init(void)
  223. {
  224. int i;
  225. struct cpuidle_driver *drv = &pseries_idle_driver;
  226. struct cpuidle_device *dev;
  227. pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  228. if (pseries_cpuidle_devices == NULL)
  229. return -ENOMEM;
  230. for_each_possible_cpu(i) {
  231. dev = per_cpu_ptr(pseries_cpuidle_devices, i);
  232. dev->state_count = drv->state_count;
  233. dev->cpu = i;
  234. if (cpuidle_register_device(dev)) {
  235. printk(KERN_DEBUG \
  236. "cpuidle_register_device %d failed!\n", i);
  237. return -EIO;
  238. }
  239. }
  240. return 0;
  241. }
  242. /*
  243. * pseries_idle_probe()
  244. * Choose state table for shared versus dedicated partition
  245. */
  246. static int pseries_idle_probe(void)
  247. {
  248. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  249. return -ENODEV;
  250. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  251. return -ENODEV;
  252. if (max_idle_state == 0) {
  253. printk(KERN_DEBUG "pseries processor idle disabled.\n");
  254. return -EPERM;
  255. }
  256. if (get_lppaca()->shared_proc)
  257. cpuidle_state_table = shared_states;
  258. else
  259. cpuidle_state_table = dedicated_states;
  260. return 0;
  261. }
  262. static int __init pseries_processor_idle_init(void)
  263. {
  264. int retval;
  265. retval = pseries_idle_probe();
  266. if (retval)
  267. return retval;
  268. pseries_cpuidle_driver_init();
  269. retval = cpuidle_register_driver(&pseries_idle_driver);
  270. if (retval) {
  271. printk(KERN_DEBUG "Registration of pseries driver failed.\n");
  272. return retval;
  273. }
  274. retval = pseries_idle_devices_init();
  275. if (retval) {
  276. pseries_idle_devices_uninit();
  277. cpuidle_unregister_driver(&pseries_idle_driver);
  278. return retval;
  279. }
  280. register_cpu_notifier(&setup_hotplug_notifier);
  281. printk(KERN_DEBUG "pseries_idle_driver registered\n");
  282. return 0;
  283. }
  284. static void __exit pseries_processor_idle_exit(void)
  285. {
  286. unregister_cpu_notifier(&setup_hotplug_notifier);
  287. pseries_idle_devices_uninit();
  288. cpuidle_unregister_driver(&pseries_idle_driver);
  289. return;
  290. }
  291. module_init(pseries_processor_idle_init);
  292. module_exit(pseries_processor_idle_exit);
  293. MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
  294. MODULE_DESCRIPTION("Cpuidle driver for POWER");
  295. MODULE_LICENSE("GPL");