processor_idle.c 7.6 KB

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