processor_idle.c 8.1 KB

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