cpuidle-powernv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cpuidle-powernv - idle state cpuidle driver.
  4. * Adapted from drivers/cpuidle/cpuidle-pseries
  5. *
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/cpu.h>
  13. #include <linux/notifier.h>
  14. #include <linux/clockchips.h>
  15. #include <linux/of.h>
  16. #include <linux/slab.h>
  17. #include <asm/machdep.h>
  18. #include <asm/firmware.h>
  19. #include <asm/opal.h>
  20. #include <asm/runlatch.h>
  21. #include <asm/cpuidle.h>
  22. /*
  23. * Expose only those Hardware idle states via the cpuidle framework
  24. * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
  25. */
  26. #define POWERNV_THRESHOLD_LATENCY_NS 200000
  27. static struct cpuidle_driver powernv_idle_driver = {
  28. .name = "powernv_idle",
  29. .owner = THIS_MODULE,
  30. };
  31. static int max_idle_state __read_mostly;
  32. static struct cpuidle_state *cpuidle_state_table __read_mostly;
  33. struct stop_psscr_table {
  34. u64 val;
  35. u64 mask;
  36. };
  37. static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX] __read_mostly;
  38. static u64 default_snooze_timeout __read_mostly;
  39. static bool snooze_timeout_en __read_mostly;
  40. static u64 get_snooze_timeout(struct cpuidle_device *dev,
  41. struct cpuidle_driver *drv,
  42. int index)
  43. {
  44. int i;
  45. if (unlikely(!snooze_timeout_en))
  46. return default_snooze_timeout;
  47. for (i = index + 1; i < drv->state_count; i++) {
  48. if (dev->states_usage[i].disable)
  49. continue;
  50. return drv->states[i].target_residency * tb_ticks_per_usec;
  51. }
  52. return default_snooze_timeout;
  53. }
  54. static int snooze_loop(struct cpuidle_device *dev,
  55. struct cpuidle_driver *drv,
  56. int index)
  57. {
  58. u64 snooze_exit_time;
  59. set_thread_flag(TIF_POLLING_NRFLAG);
  60. local_irq_enable();
  61. snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index);
  62. ppc64_runlatch_off();
  63. HMT_very_low();
  64. while (!need_resched()) {
  65. if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
  66. /*
  67. * Task has not woken up but we are exiting the polling
  68. * loop anyway. Require a barrier after polling is
  69. * cleared to order subsequent test of need_resched().
  70. */
  71. clear_thread_flag(TIF_POLLING_NRFLAG);
  72. smp_mb();
  73. break;
  74. }
  75. }
  76. HMT_medium();
  77. ppc64_runlatch_on();
  78. clear_thread_flag(TIF_POLLING_NRFLAG);
  79. local_irq_disable();
  80. return index;
  81. }
  82. static int nap_loop(struct cpuidle_device *dev,
  83. struct cpuidle_driver *drv,
  84. int index)
  85. {
  86. power7_idle_type(PNV_THREAD_NAP);
  87. return index;
  88. }
  89. /* Register for fastsleep only in oneshot mode of broadcast */
  90. #ifdef CONFIG_TICK_ONESHOT
  91. static int fastsleep_loop(struct cpuidle_device *dev,
  92. struct cpuidle_driver *drv,
  93. int index)
  94. {
  95. unsigned long old_lpcr = mfspr(SPRN_LPCR);
  96. unsigned long new_lpcr;
  97. if (unlikely(system_state < SYSTEM_RUNNING))
  98. return index;
  99. new_lpcr = old_lpcr;
  100. /* Do not exit powersave upon decrementer as we've setup the timer
  101. * offload.
  102. */
  103. new_lpcr &= ~LPCR_PECE1;
  104. mtspr(SPRN_LPCR, new_lpcr);
  105. power7_idle_type(PNV_THREAD_SLEEP);
  106. mtspr(SPRN_LPCR, old_lpcr);
  107. return index;
  108. }
  109. #endif
  110. static int stop_loop(struct cpuidle_device *dev,
  111. struct cpuidle_driver *drv,
  112. int index)
  113. {
  114. arch300_idle_type(stop_psscr_table[index].val,
  115. stop_psscr_table[index].mask);
  116. return index;
  117. }
  118. /*
  119. * States for dedicated partition case.
  120. */
  121. static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
  122. { /* Snooze */
  123. .name = "snooze",
  124. .desc = "snooze",
  125. .exit_latency = 0,
  126. .target_residency = 0,
  127. .enter = snooze_loop },
  128. };
  129. static int powernv_cpuidle_cpu_online(unsigned int cpu)
  130. {
  131. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  132. if (dev && cpuidle_get_driver()) {
  133. cpuidle_pause_and_lock();
  134. cpuidle_enable_device(dev);
  135. cpuidle_resume_and_unlock();
  136. }
  137. return 0;
  138. }
  139. static int powernv_cpuidle_cpu_dead(unsigned int cpu)
  140. {
  141. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  142. if (dev && cpuidle_get_driver()) {
  143. cpuidle_pause_and_lock();
  144. cpuidle_disable_device(dev);
  145. cpuidle_resume_and_unlock();
  146. }
  147. return 0;
  148. }
  149. /*
  150. * powernv_cpuidle_driver_init()
  151. */
  152. static int powernv_cpuidle_driver_init(void)
  153. {
  154. int idle_state;
  155. struct cpuidle_driver *drv = &powernv_idle_driver;
  156. drv->state_count = 0;
  157. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  158. /* Is the state not enabled? */
  159. if (cpuidle_state_table[idle_state].enter == NULL)
  160. continue;
  161. drv->states[drv->state_count] = /* structure copy */
  162. cpuidle_state_table[idle_state];
  163. drv->state_count += 1;
  164. }
  165. /*
  166. * On the PowerNV platform cpu_present may be less than cpu_possible in
  167. * cases when firmware detects the CPU, but it is not available to the
  168. * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
  169. * run time and hence cpu_devices are not created for those CPUs by the
  170. * generic topology_init().
  171. *
  172. * drv->cpumask defaults to cpu_possible_mask in
  173. * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
  174. * cpu_devices are not created for CPUs in cpu_possible_mask that
  175. * cannot be hot-added later at run time.
  176. *
  177. * Trying cpuidle_register_device() on a CPU without a cpu_device is
  178. * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
  179. */
  180. drv->cpumask = (struct cpumask *)cpu_present_mask;
  181. return 0;
  182. }
  183. static inline void add_powernv_state(int index, const char *name,
  184. unsigned int flags,
  185. int (*idle_fn)(struct cpuidle_device *,
  186. struct cpuidle_driver *,
  187. int),
  188. unsigned int target_residency,
  189. unsigned int exit_latency,
  190. u64 psscr_val, u64 psscr_mask)
  191. {
  192. strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
  193. strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
  194. powernv_states[index].flags = flags;
  195. powernv_states[index].target_residency = target_residency;
  196. powernv_states[index].exit_latency = exit_latency;
  197. powernv_states[index].enter = idle_fn;
  198. /* For power8 and below psscr_* will be 0 */
  199. stop_psscr_table[index].val = psscr_val;
  200. stop_psscr_table[index].mask = psscr_mask;
  201. }
  202. extern u32 pnv_get_supported_cpuidle_states(void);
  203. static int powernv_add_idle_states(void)
  204. {
  205. int nr_idle_states = 1; /* Snooze */
  206. int dt_idle_states;
  207. u32 has_stop_states = 0;
  208. int i;
  209. u32 supported_flags = pnv_get_supported_cpuidle_states();
  210. /* Currently we have snooze statically defined */
  211. if (nr_pnv_idle_states <= 0) {
  212. pr_warn("cpuidle-powernv : Only Snooze is available\n");
  213. goto out;
  214. }
  215. /* TODO: Count only states which are eligible for cpuidle */
  216. dt_idle_states = nr_pnv_idle_states;
  217. /*
  218. * Since snooze is used as first idle state, max idle states allowed is
  219. * CPUIDLE_STATE_MAX -1
  220. */
  221. if (nr_pnv_idle_states > CPUIDLE_STATE_MAX - 1) {
  222. pr_warn("cpuidle-powernv: discovered idle states more than allowed");
  223. dt_idle_states = CPUIDLE_STATE_MAX - 1;
  224. }
  225. /*
  226. * If the idle states use stop instruction, probe for psscr values
  227. * and psscr mask which are necessary to specify required stop level.
  228. */
  229. has_stop_states = (pnv_idle_states[0].flags &
  230. (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
  231. for (i = 0; i < dt_idle_states; i++) {
  232. unsigned int exit_latency, target_residency;
  233. bool stops_timebase = false;
  234. struct pnv_idle_states_t *state = &pnv_idle_states[i];
  235. /*
  236. * Skip the platform idle state whose flag isn't in
  237. * the supported_cpuidle_states flag mask.
  238. */
  239. if ((state->flags & supported_flags) != state->flags)
  240. continue;
  241. /*
  242. * If an idle state has exit latency beyond
  243. * POWERNV_THRESHOLD_LATENCY_NS then don't use it
  244. * in cpu-idle.
  245. */
  246. if (state->latency_ns > POWERNV_THRESHOLD_LATENCY_NS)
  247. continue;
  248. /*
  249. * Firmware passes residency and latency values in ns.
  250. * cpuidle expects it in us.
  251. */
  252. exit_latency = DIV_ROUND_UP(state->latency_ns, 1000);
  253. target_residency = DIV_ROUND_UP(state->residency_ns, 1000);
  254. if (has_stop_states && !(state->valid))
  255. continue;
  256. if (state->flags & OPAL_PM_TIMEBASE_STOP)
  257. stops_timebase = true;
  258. if (state->flags & OPAL_PM_NAP_ENABLED) {
  259. /* Add NAP state */
  260. add_powernv_state(nr_idle_states, "Nap",
  261. CPUIDLE_FLAG_NONE, nap_loop,
  262. target_residency, exit_latency, 0, 0);
  263. } else if (has_stop_states && !stops_timebase) {
  264. add_powernv_state(nr_idle_states, state->name,
  265. CPUIDLE_FLAG_NONE, stop_loop,
  266. target_residency, exit_latency,
  267. state->psscr_val,
  268. state->psscr_mask);
  269. }
  270. /*
  271. * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
  272. * within this config dependency check.
  273. */
  274. #ifdef CONFIG_TICK_ONESHOT
  275. else if (state->flags & OPAL_PM_SLEEP_ENABLED ||
  276. state->flags & OPAL_PM_SLEEP_ENABLED_ER1) {
  277. /* Add FASTSLEEP state */
  278. add_powernv_state(nr_idle_states, "FastSleep",
  279. CPUIDLE_FLAG_TIMER_STOP,
  280. fastsleep_loop,
  281. target_residency, exit_latency, 0, 0);
  282. } else if (has_stop_states && stops_timebase) {
  283. add_powernv_state(nr_idle_states, state->name,
  284. CPUIDLE_FLAG_TIMER_STOP, stop_loop,
  285. target_residency, exit_latency,
  286. state->psscr_val,
  287. state->psscr_mask);
  288. }
  289. #endif
  290. else
  291. continue;
  292. nr_idle_states++;
  293. }
  294. out:
  295. return nr_idle_states;
  296. }
  297. /*
  298. * powernv_idle_probe()
  299. * Choose state table for shared versus dedicated partition
  300. */
  301. static int powernv_idle_probe(void)
  302. {
  303. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  304. return -ENODEV;
  305. if (firmware_has_feature(FW_FEATURE_OPAL)) {
  306. cpuidle_state_table = powernv_states;
  307. /* Device tree can indicate more idle states */
  308. max_idle_state = powernv_add_idle_states();
  309. default_snooze_timeout = TICK_USEC * tb_ticks_per_usec;
  310. if (max_idle_state > 1)
  311. snooze_timeout_en = true;
  312. } else
  313. return -ENODEV;
  314. return 0;
  315. }
  316. static int __init powernv_processor_idle_init(void)
  317. {
  318. int retval;
  319. retval = powernv_idle_probe();
  320. if (retval)
  321. return retval;
  322. powernv_cpuidle_driver_init();
  323. retval = cpuidle_register(&powernv_idle_driver, NULL);
  324. if (retval) {
  325. printk(KERN_DEBUG "Registration of powernv driver failed.\n");
  326. return retval;
  327. }
  328. retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  329. "cpuidle/powernv:online",
  330. powernv_cpuidle_cpu_online, NULL);
  331. WARN_ON(retval < 0);
  332. retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
  333. "cpuidle/powernv:dead", NULL,
  334. powernv_cpuidle_cpu_dead);
  335. WARN_ON(retval < 0);
  336. printk(KERN_DEBUG "powernv_idle_driver registered\n");
  337. return 0;
  338. }
  339. device_initcall(powernv_processor_idle_init);