cpuidle-powernv.c 6.3 KB

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