cpuidle-powernv.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * cpuidle-powernv - idle state cpuidle driver.
  3. * Adapted from drivers/cpuidle/cpuidle-pseries
  4. *
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/cpuidle.h>
  11. #include <linux/cpu.h>
  12. #include <linux/notifier.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/of.h>
  15. #include <linux/slab.h>
  16. #include <asm/machdep.h>
  17. #include <asm/firmware.h>
  18. #include <asm/opal.h>
  19. #include <asm/runlatch.h>
  20. #define MAX_POWERNV_IDLE_STATES 8
  21. struct cpuidle_driver powernv_idle_driver = {
  22. .name = "powernv_idle",
  23. .owner = THIS_MODULE,
  24. };
  25. static int max_idle_state;
  26. static struct cpuidle_state *cpuidle_state_table;
  27. static u64 snooze_timeout;
  28. static bool snooze_timeout_en;
  29. static int snooze_loop(struct cpuidle_device *dev,
  30. struct cpuidle_driver *drv,
  31. int index)
  32. {
  33. u64 snooze_exit_time;
  34. local_irq_enable();
  35. set_thread_flag(TIF_POLLING_NRFLAG);
  36. snooze_exit_time = get_tb() + snooze_timeout;
  37. ppc64_runlatch_off();
  38. while (!need_resched()) {
  39. HMT_low();
  40. HMT_very_low();
  41. if (snooze_timeout_en && get_tb() > snooze_exit_time)
  42. break;
  43. }
  44. HMT_medium();
  45. ppc64_runlatch_on();
  46. clear_thread_flag(TIF_POLLING_NRFLAG);
  47. smp_mb();
  48. return index;
  49. }
  50. static int nap_loop(struct cpuidle_device *dev,
  51. struct cpuidle_driver *drv,
  52. int index)
  53. {
  54. ppc64_runlatch_off();
  55. power7_idle();
  56. ppc64_runlatch_on();
  57. return index;
  58. }
  59. /* Register for fastsleep only in oneshot mode of broadcast */
  60. #ifdef CONFIG_TICK_ONESHOT
  61. static int fastsleep_loop(struct cpuidle_device *dev,
  62. struct cpuidle_driver *drv,
  63. int index)
  64. {
  65. unsigned long old_lpcr = mfspr(SPRN_LPCR);
  66. unsigned long new_lpcr;
  67. if (unlikely(system_state < SYSTEM_RUNNING))
  68. return index;
  69. new_lpcr = old_lpcr;
  70. /* Do not exit powersave upon decrementer as we've setup the timer
  71. * offload.
  72. */
  73. new_lpcr &= ~LPCR_PECE1;
  74. mtspr(SPRN_LPCR, new_lpcr);
  75. power7_sleep();
  76. mtspr(SPRN_LPCR, old_lpcr);
  77. return index;
  78. }
  79. #endif
  80. /*
  81. * States for dedicated partition case.
  82. */
  83. static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
  84. { /* Snooze */
  85. .name = "snooze",
  86. .desc = "snooze",
  87. .exit_latency = 0,
  88. .target_residency = 0,
  89. .enter = &snooze_loop },
  90. };
  91. static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
  92. unsigned long action, void *hcpu)
  93. {
  94. int hotcpu = (unsigned long)hcpu;
  95. struct cpuidle_device *dev =
  96. per_cpu(cpuidle_devices, hotcpu);
  97. if (dev && cpuidle_get_driver()) {
  98. switch (action) {
  99. case CPU_ONLINE:
  100. case CPU_ONLINE_FROZEN:
  101. cpuidle_pause_and_lock();
  102. cpuidle_enable_device(dev);
  103. cpuidle_resume_and_unlock();
  104. break;
  105. case CPU_DEAD:
  106. case CPU_DEAD_FROZEN:
  107. cpuidle_pause_and_lock();
  108. cpuidle_disable_device(dev);
  109. cpuidle_resume_and_unlock();
  110. break;
  111. default:
  112. return NOTIFY_DONE;
  113. }
  114. }
  115. return NOTIFY_OK;
  116. }
  117. static struct notifier_block setup_hotplug_notifier = {
  118. .notifier_call = powernv_cpuidle_add_cpu_notifier,
  119. };
  120. /*
  121. * powernv_cpuidle_driver_init()
  122. */
  123. static int powernv_cpuidle_driver_init(void)
  124. {
  125. int idle_state;
  126. struct cpuidle_driver *drv = &powernv_idle_driver;
  127. drv->state_count = 0;
  128. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  129. /* Is the state not enabled? */
  130. if (cpuidle_state_table[idle_state].enter == NULL)
  131. continue;
  132. drv->states[drv->state_count] = /* structure copy */
  133. cpuidle_state_table[idle_state];
  134. drv->state_count += 1;
  135. }
  136. return 0;
  137. }
  138. static int powernv_add_idle_states(void)
  139. {
  140. struct device_node *power_mgt;
  141. int nr_idle_states = 1; /* Snooze */
  142. int dt_idle_states;
  143. u32 *latency_ns, *residency_ns, *flags;
  144. int i, rc;
  145. /* Currently we have snooze statically defined */
  146. power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
  147. if (!power_mgt) {
  148. pr_warn("opal: PowerMgmt Node not found\n");
  149. goto out;
  150. }
  151. /* Read values of any property to determine the num of idle states */
  152. dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
  153. if (dt_idle_states < 0) {
  154. pr_warn("cpuidle-powernv: no idle states found in the DT\n");
  155. goto out;
  156. }
  157. flags = kzalloc(sizeof(*flags) * dt_idle_states, GFP_KERNEL);
  158. if (of_property_read_u32_array(power_mgt,
  159. "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
  160. pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
  161. goto out_free_flags;
  162. }
  163. latency_ns = kzalloc(sizeof(*latency_ns) * dt_idle_states, GFP_KERNEL);
  164. rc = of_property_read_u32_array(power_mgt,
  165. "ibm,cpu-idle-state-latencies-ns", latency_ns, dt_idle_states);
  166. if (rc) {
  167. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
  168. goto out_free_latency;
  169. }
  170. residency_ns = kzalloc(sizeof(*residency_ns) * dt_idle_states, GFP_KERNEL);
  171. rc = of_property_read_u32_array(power_mgt,
  172. "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
  173. for (i = 0; i < dt_idle_states; i++) {
  174. /*
  175. * Cpuidle accepts exit_latency and target_residency in us.
  176. * Use default target_residency values if f/w does not expose it.
  177. */
  178. if (flags[i] & OPAL_PM_NAP_ENABLED) {
  179. /* Add NAP state */
  180. strcpy(powernv_states[nr_idle_states].name, "Nap");
  181. strcpy(powernv_states[nr_idle_states].desc, "Nap");
  182. powernv_states[nr_idle_states].flags = 0;
  183. powernv_states[nr_idle_states].target_residency = 100;
  184. powernv_states[nr_idle_states].enter = &nap_loop;
  185. }
  186. /*
  187. * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
  188. * within this config dependency check.
  189. */
  190. #ifdef CONFIG_TICK_ONESHOT
  191. if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
  192. flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
  193. /* Add FASTSLEEP state */
  194. strcpy(powernv_states[nr_idle_states].name, "FastSleep");
  195. strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
  196. powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
  197. powernv_states[nr_idle_states].target_residency = 300000;
  198. powernv_states[nr_idle_states].enter = &fastsleep_loop;
  199. }
  200. #endif
  201. powernv_states[nr_idle_states].exit_latency =
  202. ((unsigned int)latency_ns[i]) / 1000;
  203. if (!rc) {
  204. powernv_states[nr_idle_states].target_residency =
  205. ((unsigned int)residency_ns[i]) / 1000;
  206. }
  207. nr_idle_states++;
  208. }
  209. kfree(residency_ns);
  210. out_free_latency:
  211. kfree(latency_ns);
  212. out_free_flags:
  213. kfree(flags);
  214. out:
  215. return nr_idle_states;
  216. }
  217. /*
  218. * powernv_idle_probe()
  219. * Choose state table for shared versus dedicated partition
  220. */
  221. static int powernv_idle_probe(void)
  222. {
  223. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  224. return -ENODEV;
  225. if (firmware_has_feature(FW_FEATURE_OPALv3)) {
  226. cpuidle_state_table = powernv_states;
  227. /* Device tree can indicate more idle states */
  228. max_idle_state = powernv_add_idle_states();
  229. if (max_idle_state > 1) {
  230. snooze_timeout_en = true;
  231. snooze_timeout = powernv_states[1].target_residency *
  232. tb_ticks_per_usec;
  233. }
  234. } else
  235. return -ENODEV;
  236. return 0;
  237. }
  238. static int __init powernv_processor_idle_init(void)
  239. {
  240. int retval;
  241. retval = powernv_idle_probe();
  242. if (retval)
  243. return retval;
  244. powernv_cpuidle_driver_init();
  245. retval = cpuidle_register(&powernv_idle_driver, NULL);
  246. if (retval) {
  247. printk(KERN_DEBUG "Registration of powernv driver failed.\n");
  248. return retval;
  249. }
  250. register_cpu_notifier(&setup_hotplug_notifier);
  251. printk(KERN_DEBUG "powernv_idle_driver registered\n");
  252. return 0;
  253. }
  254. device_initcall(powernv_processor_idle_init);