cpuidle-powernv.c 6.6 KB

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