processor_idle.c 7.7 KB

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