cpuidle-powernv.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. u32 len_flags, flags;
  138. int i;
  139. /* Currently we have snooze statically defined */
  140. power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
  141. if (!power_mgt) {
  142. pr_warn("opal: PowerMgmt Node not found\n");
  143. return nr_idle_states;
  144. }
  145. idle_state_flags = of_get_property(power_mgt, "ibm,cpu-idle-state-flags", &len_flags);
  146. if (!idle_state_flags) {
  147. pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
  148. return nr_idle_states;
  149. }
  150. dt_idle_states = len_flags / sizeof(u32);
  151. for (i = 0; i < dt_idle_states; i++) {
  152. flags = be32_to_cpu(idle_state_flags[i]);
  153. if (flags & IDLE_USE_INST_NAP) {
  154. /* Add NAP state */
  155. strcpy(powernv_states[nr_idle_states].name, "Nap");
  156. strcpy(powernv_states[nr_idle_states].desc, "Nap");
  157. powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIME_VALID;
  158. powernv_states[nr_idle_states].exit_latency = 10;
  159. powernv_states[nr_idle_states].target_residency = 100;
  160. powernv_states[nr_idle_states].enter = &nap_loop;
  161. nr_idle_states++;
  162. }
  163. if (flags & IDLE_USE_INST_SLEEP) {
  164. /* Add FASTSLEEP state */
  165. strcpy(powernv_states[nr_idle_states].name, "FastSleep");
  166. strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
  167. powernv_states[nr_idle_states].flags =
  168. CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
  169. powernv_states[nr_idle_states].exit_latency = 300;
  170. powernv_states[nr_idle_states].target_residency = 1000000;
  171. powernv_states[nr_idle_states].enter = &fastsleep_loop;
  172. nr_idle_states++;
  173. }
  174. }
  175. return nr_idle_states;
  176. }
  177. /*
  178. * powernv_idle_probe()
  179. * Choose state table for shared versus dedicated partition
  180. */
  181. static int powernv_idle_probe(void)
  182. {
  183. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  184. return -ENODEV;
  185. if (firmware_has_feature(FW_FEATURE_OPALv3)) {
  186. cpuidle_state_table = powernv_states;
  187. /* Device tree can indicate more idle states */
  188. max_idle_state = powernv_add_idle_states();
  189. } else
  190. return -ENODEV;
  191. return 0;
  192. }
  193. static int __init powernv_processor_idle_init(void)
  194. {
  195. int retval;
  196. retval = powernv_idle_probe();
  197. if (retval)
  198. return retval;
  199. powernv_cpuidle_driver_init();
  200. retval = cpuidle_register(&powernv_idle_driver, NULL);
  201. if (retval) {
  202. printk(KERN_DEBUG "Registration of powernv driver failed.\n");
  203. return retval;
  204. }
  205. register_cpu_notifier(&setup_hotplug_notifier);
  206. printk(KERN_DEBUG "powernv_idle_driver registered\n");
  207. return 0;
  208. }
  209. device_initcall(powernv_processor_idle_init);