cpuidle-powernv.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 default_snooze_timeout;
  28. static bool snooze_timeout_en;
  29. static u64 get_snooze_timeout(struct cpuidle_device *dev,
  30. struct cpuidle_driver *drv,
  31. int index)
  32. {
  33. int i;
  34. if (unlikely(!snooze_timeout_en))
  35. return default_snooze_timeout;
  36. for (i = index + 1; i < drv->state_count; i++) {
  37. struct cpuidle_state *s = &drv->states[i];
  38. struct cpuidle_state_usage *su = &dev->states_usage[i];
  39. if (s->disabled || su->disable)
  40. continue;
  41. return s->target_residency * tb_ticks_per_usec;
  42. }
  43. return default_snooze_timeout;
  44. }
  45. static int snooze_loop(struct cpuidle_device *dev,
  46. struct cpuidle_driver *drv,
  47. int index)
  48. {
  49. u64 snooze_exit_time;
  50. local_irq_enable();
  51. set_thread_flag(TIF_POLLING_NRFLAG);
  52. snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index);
  53. ppc64_runlatch_off();
  54. while (!need_resched()) {
  55. HMT_low();
  56. HMT_very_low();
  57. if (snooze_timeout_en && get_tb() > snooze_exit_time)
  58. break;
  59. }
  60. HMT_medium();
  61. ppc64_runlatch_on();
  62. clear_thread_flag(TIF_POLLING_NRFLAG);
  63. smp_mb();
  64. return index;
  65. }
  66. static int nap_loop(struct cpuidle_device *dev,
  67. struct cpuidle_driver *drv,
  68. int index)
  69. {
  70. ppc64_runlatch_off();
  71. power7_idle();
  72. ppc64_runlatch_on();
  73. return index;
  74. }
  75. /* Register for fastsleep only in oneshot mode of broadcast */
  76. #ifdef CONFIG_TICK_ONESHOT
  77. static int fastsleep_loop(struct cpuidle_device *dev,
  78. struct cpuidle_driver *drv,
  79. int index)
  80. {
  81. unsigned long old_lpcr = mfspr(SPRN_LPCR);
  82. unsigned long new_lpcr;
  83. if (unlikely(system_state < SYSTEM_RUNNING))
  84. return index;
  85. new_lpcr = old_lpcr;
  86. /* Do not exit powersave upon decrementer as we've setup the timer
  87. * offload.
  88. */
  89. new_lpcr &= ~LPCR_PECE1;
  90. mtspr(SPRN_LPCR, new_lpcr);
  91. power7_sleep();
  92. mtspr(SPRN_LPCR, old_lpcr);
  93. return index;
  94. }
  95. #endif
  96. /*
  97. * States for dedicated partition case.
  98. */
  99. static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
  100. { /* Snooze */
  101. .name = "snooze",
  102. .desc = "snooze",
  103. .exit_latency = 0,
  104. .target_residency = 0,
  105. .enter = &snooze_loop },
  106. };
  107. static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
  108. unsigned long action, void *hcpu)
  109. {
  110. int hotcpu = (unsigned long)hcpu;
  111. struct cpuidle_device *dev =
  112. per_cpu(cpuidle_devices, hotcpu);
  113. if (dev && cpuidle_get_driver()) {
  114. switch (action) {
  115. case CPU_ONLINE:
  116. case CPU_ONLINE_FROZEN:
  117. cpuidle_pause_and_lock();
  118. cpuidle_enable_device(dev);
  119. cpuidle_resume_and_unlock();
  120. break;
  121. case CPU_DEAD:
  122. case CPU_DEAD_FROZEN:
  123. cpuidle_pause_and_lock();
  124. cpuidle_disable_device(dev);
  125. cpuidle_resume_and_unlock();
  126. break;
  127. default:
  128. return NOTIFY_DONE;
  129. }
  130. }
  131. return NOTIFY_OK;
  132. }
  133. static struct notifier_block setup_hotplug_notifier = {
  134. .notifier_call = powernv_cpuidle_add_cpu_notifier,
  135. };
  136. /*
  137. * powernv_cpuidle_driver_init()
  138. */
  139. static int powernv_cpuidle_driver_init(void)
  140. {
  141. int idle_state;
  142. struct cpuidle_driver *drv = &powernv_idle_driver;
  143. drv->state_count = 0;
  144. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  145. /* Is the state not enabled? */
  146. if (cpuidle_state_table[idle_state].enter == NULL)
  147. continue;
  148. drv->states[drv->state_count] = /* structure copy */
  149. cpuidle_state_table[idle_state];
  150. drv->state_count += 1;
  151. }
  152. /*
  153. * On the PowerNV platform cpu_present may be less than cpu_possible in
  154. * cases when firmware detects the CPU, but it is not available to the
  155. * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
  156. * run time and hence cpu_devices are not created for those CPUs by the
  157. * generic topology_init().
  158. *
  159. * drv->cpumask defaults to cpu_possible_mask in
  160. * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
  161. * cpu_devices are not created for CPUs in cpu_possible_mask that
  162. * cannot be hot-added later at run time.
  163. *
  164. * Trying cpuidle_register_device() on a CPU without a cpu_device is
  165. * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
  166. */
  167. drv->cpumask = (struct cpumask *)cpu_present_mask;
  168. return 0;
  169. }
  170. static int powernv_add_idle_states(void)
  171. {
  172. struct device_node *power_mgt;
  173. int nr_idle_states = 1; /* Snooze */
  174. int dt_idle_states;
  175. u32 *latency_ns, *residency_ns, *flags;
  176. int i, rc;
  177. /* Currently we have snooze statically defined */
  178. power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
  179. if (!power_mgt) {
  180. pr_warn("opal: PowerMgmt Node not found\n");
  181. goto out;
  182. }
  183. /* Read values of any property to determine the num of idle states */
  184. dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
  185. if (dt_idle_states < 0) {
  186. pr_warn("cpuidle-powernv: no idle states found in the DT\n");
  187. goto out;
  188. }
  189. flags = kzalloc(sizeof(*flags) * dt_idle_states, GFP_KERNEL);
  190. if (of_property_read_u32_array(power_mgt,
  191. "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
  192. pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
  193. goto out_free_flags;
  194. }
  195. latency_ns = kzalloc(sizeof(*latency_ns) * dt_idle_states, GFP_KERNEL);
  196. rc = of_property_read_u32_array(power_mgt,
  197. "ibm,cpu-idle-state-latencies-ns", latency_ns, dt_idle_states);
  198. if (rc) {
  199. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
  200. goto out_free_latency;
  201. }
  202. residency_ns = kzalloc(sizeof(*residency_ns) * dt_idle_states, GFP_KERNEL);
  203. rc = of_property_read_u32_array(power_mgt,
  204. "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states);
  205. for (i = 0; i < dt_idle_states; i++) {
  206. /*
  207. * Cpuidle accepts exit_latency and target_residency in us.
  208. * Use default target_residency values if f/w does not expose it.
  209. */
  210. if (flags[i] & OPAL_PM_NAP_ENABLED) {
  211. /* Add NAP state */
  212. strcpy(powernv_states[nr_idle_states].name, "Nap");
  213. strcpy(powernv_states[nr_idle_states].desc, "Nap");
  214. powernv_states[nr_idle_states].flags = 0;
  215. powernv_states[nr_idle_states].target_residency = 100;
  216. powernv_states[nr_idle_states].enter = &nap_loop;
  217. }
  218. /*
  219. * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
  220. * within this config dependency check.
  221. */
  222. #ifdef CONFIG_TICK_ONESHOT
  223. if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
  224. flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
  225. /* Add FASTSLEEP state */
  226. strcpy(powernv_states[nr_idle_states].name, "FastSleep");
  227. strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
  228. powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP;
  229. powernv_states[nr_idle_states].target_residency = 300000;
  230. powernv_states[nr_idle_states].enter = &fastsleep_loop;
  231. }
  232. #endif
  233. powernv_states[nr_idle_states].exit_latency =
  234. ((unsigned int)latency_ns[i]) / 1000;
  235. if (!rc) {
  236. powernv_states[nr_idle_states].target_residency =
  237. ((unsigned int)residency_ns[i]) / 1000;
  238. }
  239. nr_idle_states++;
  240. }
  241. kfree(residency_ns);
  242. out_free_latency:
  243. kfree(latency_ns);
  244. out_free_flags:
  245. kfree(flags);
  246. out:
  247. return nr_idle_states;
  248. }
  249. /*
  250. * powernv_idle_probe()
  251. * Choose state table for shared versus dedicated partition
  252. */
  253. static int powernv_idle_probe(void)
  254. {
  255. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  256. return -ENODEV;
  257. if (firmware_has_feature(FW_FEATURE_OPAL)) {
  258. cpuidle_state_table = powernv_states;
  259. /* Device tree can indicate more idle states */
  260. max_idle_state = powernv_add_idle_states();
  261. default_snooze_timeout = TICK_USEC * tb_ticks_per_usec;
  262. if (max_idle_state > 1)
  263. snooze_timeout_en = true;
  264. } else
  265. return -ENODEV;
  266. return 0;
  267. }
  268. static int __init powernv_processor_idle_init(void)
  269. {
  270. int retval;
  271. retval = powernv_idle_probe();
  272. if (retval)
  273. return retval;
  274. powernv_cpuidle_driver_init();
  275. retval = cpuidle_register(&powernv_idle_driver, NULL);
  276. if (retval) {
  277. printk(KERN_DEBUG "Registration of powernv driver failed.\n");
  278. return retval;
  279. }
  280. register_cpu_notifier(&setup_hotplug_notifier);
  281. printk(KERN_DEBUG "powernv_idle_driver registered\n");
  282. return 0;
  283. }
  284. device_initcall(powernv_processor_idle_init);