cpuidle-powernv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 snooze_timeout __read_mostly;
  39. static bool snooze_timeout_en __read_mostly;
  40. static int snooze_loop(struct cpuidle_device *dev,
  41. struct cpuidle_driver *drv,
  42. int index)
  43. {
  44. u64 snooze_exit_time;
  45. set_thread_flag(TIF_POLLING_NRFLAG);
  46. local_irq_enable();
  47. snooze_exit_time = get_tb() + snooze_timeout;
  48. ppc64_runlatch_off();
  49. HMT_very_low();
  50. while (!need_resched()) {
  51. if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
  52. /*
  53. * Task has not woken up but we are exiting the polling
  54. * loop anyway. Require a barrier after polling is
  55. * cleared to order subsequent test of need_resched().
  56. */
  57. clear_thread_flag(TIF_POLLING_NRFLAG);
  58. smp_mb();
  59. break;
  60. }
  61. }
  62. HMT_medium();
  63. ppc64_runlatch_on();
  64. clear_thread_flag(TIF_POLLING_NRFLAG);
  65. local_irq_disable();
  66. return index;
  67. }
  68. static int nap_loop(struct cpuidle_device *dev,
  69. struct cpuidle_driver *drv,
  70. int index)
  71. {
  72. power7_idle_type(PNV_THREAD_NAP);
  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_idle_type(PNV_THREAD_SLEEP);
  92. mtspr(SPRN_LPCR, old_lpcr);
  93. return index;
  94. }
  95. #endif
  96. static int stop_loop(struct cpuidle_device *dev,
  97. struct cpuidle_driver *drv,
  98. int index)
  99. {
  100. power9_idle_type(stop_psscr_table[index].val,
  101. stop_psscr_table[index].mask);
  102. return index;
  103. }
  104. /*
  105. * States for dedicated partition case.
  106. */
  107. static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
  108. { /* Snooze */
  109. .name = "snooze",
  110. .desc = "snooze",
  111. .exit_latency = 0,
  112. .target_residency = 0,
  113. .enter = snooze_loop },
  114. };
  115. static int powernv_cpuidle_cpu_online(unsigned int cpu)
  116. {
  117. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  118. if (dev && cpuidle_get_driver()) {
  119. cpuidle_pause_and_lock();
  120. cpuidle_enable_device(dev);
  121. cpuidle_resume_and_unlock();
  122. }
  123. return 0;
  124. }
  125. static int powernv_cpuidle_cpu_dead(unsigned int cpu)
  126. {
  127. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  128. if (dev && cpuidle_get_driver()) {
  129. cpuidle_pause_and_lock();
  130. cpuidle_disable_device(dev);
  131. cpuidle_resume_and_unlock();
  132. }
  133. return 0;
  134. }
  135. /*
  136. * powernv_cpuidle_driver_init()
  137. */
  138. static int powernv_cpuidle_driver_init(void)
  139. {
  140. int idle_state;
  141. struct cpuidle_driver *drv = &powernv_idle_driver;
  142. drv->state_count = 0;
  143. for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
  144. /* Is the state not enabled? */
  145. if (cpuidle_state_table[idle_state].enter == NULL)
  146. continue;
  147. drv->states[drv->state_count] = /* structure copy */
  148. cpuidle_state_table[idle_state];
  149. drv->state_count += 1;
  150. }
  151. /*
  152. * On the PowerNV platform cpu_present may be less than cpu_possible in
  153. * cases when firmware detects the CPU, but it is not available to the
  154. * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
  155. * run time and hence cpu_devices are not created for those CPUs by the
  156. * generic topology_init().
  157. *
  158. * drv->cpumask defaults to cpu_possible_mask in
  159. * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
  160. * cpu_devices are not created for CPUs in cpu_possible_mask that
  161. * cannot be hot-added later at run time.
  162. *
  163. * Trying cpuidle_register_device() on a CPU without a cpu_device is
  164. * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
  165. */
  166. drv->cpumask = (struct cpumask *)cpu_present_mask;
  167. return 0;
  168. }
  169. static inline void add_powernv_state(int index, const char *name,
  170. unsigned int flags,
  171. int (*idle_fn)(struct cpuidle_device *,
  172. struct cpuidle_driver *,
  173. int),
  174. unsigned int target_residency,
  175. unsigned int exit_latency,
  176. u64 psscr_val, u64 psscr_mask)
  177. {
  178. strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
  179. strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
  180. powernv_states[index].flags = flags;
  181. powernv_states[index].target_residency = target_residency;
  182. powernv_states[index].exit_latency = exit_latency;
  183. powernv_states[index].enter = idle_fn;
  184. stop_psscr_table[index].val = psscr_val;
  185. stop_psscr_table[index].mask = psscr_mask;
  186. }
  187. /*
  188. * Returns 0 if prop1_len == prop2_len. Else returns -1
  189. */
  190. static inline int validate_dt_prop_sizes(const char *prop1, int prop1_len,
  191. const char *prop2, int prop2_len)
  192. {
  193. if (prop1_len == prop2_len)
  194. return 0;
  195. pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n",
  196. prop1, prop2);
  197. return -1;
  198. }
  199. extern u32 pnv_get_supported_cpuidle_states(void);
  200. static int powernv_add_idle_states(void)
  201. {
  202. struct device_node *power_mgt;
  203. int nr_idle_states = 1; /* Snooze */
  204. int dt_idle_states, count;
  205. u32 latency_ns[CPUIDLE_STATE_MAX];
  206. u32 residency_ns[CPUIDLE_STATE_MAX];
  207. u32 flags[CPUIDLE_STATE_MAX];
  208. u64 psscr_val[CPUIDLE_STATE_MAX];
  209. u64 psscr_mask[CPUIDLE_STATE_MAX];
  210. const char *names[CPUIDLE_STATE_MAX];
  211. u32 has_stop_states = 0;
  212. int i, rc;
  213. u32 supported_flags = pnv_get_supported_cpuidle_states();
  214. /* Currently we have snooze statically defined */
  215. power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
  216. if (!power_mgt) {
  217. pr_warn("opal: PowerMgmt Node not found\n");
  218. goto out;
  219. }
  220. /* Read values of any property to determine the num of idle states */
  221. dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags");
  222. if (dt_idle_states < 0) {
  223. pr_warn("cpuidle-powernv: no idle states found in the DT\n");
  224. goto out;
  225. }
  226. count = of_property_count_u32_elems(power_mgt,
  227. "ibm,cpu-idle-state-latencies-ns");
  228. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states,
  229. "ibm,cpu-idle-state-latencies-ns",
  230. count) != 0)
  231. goto out;
  232. count = of_property_count_strings(power_mgt,
  233. "ibm,cpu-idle-state-names");
  234. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states,
  235. "ibm,cpu-idle-state-names",
  236. count) != 0)
  237. goto out;
  238. /*
  239. * Since snooze is used as first idle state, max idle states allowed is
  240. * CPUIDLE_STATE_MAX -1
  241. */
  242. if (dt_idle_states > CPUIDLE_STATE_MAX - 1) {
  243. pr_warn("cpuidle-powernv: discovered idle states more than allowed");
  244. dt_idle_states = CPUIDLE_STATE_MAX - 1;
  245. }
  246. if (of_property_read_u32_array(power_mgt,
  247. "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
  248. pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
  249. goto out;
  250. }
  251. if (of_property_read_u32_array(power_mgt,
  252. "ibm,cpu-idle-state-latencies-ns", latency_ns,
  253. dt_idle_states)) {
  254. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
  255. goto out;
  256. }
  257. if (of_property_read_string_array(power_mgt,
  258. "ibm,cpu-idle-state-names", names, dt_idle_states) < 0) {
  259. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
  260. goto out;
  261. }
  262. /*
  263. * If the idle states use stop instruction, probe for psscr values
  264. * and psscr mask which are necessary to specify required stop level.
  265. */
  266. has_stop_states = (flags[0] &
  267. (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
  268. if (has_stop_states) {
  269. count = of_property_count_u64_elems(power_mgt,
  270. "ibm,cpu-idle-state-psscr");
  271. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
  272. dt_idle_states,
  273. "ibm,cpu-idle-state-psscr",
  274. count) != 0)
  275. goto out;
  276. count = of_property_count_u64_elems(power_mgt,
  277. "ibm,cpu-idle-state-psscr-mask");
  278. if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
  279. dt_idle_states,
  280. "ibm,cpu-idle-state-psscr-mask",
  281. count) != 0)
  282. goto out;
  283. if (of_property_read_u64_array(power_mgt,
  284. "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) {
  285. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
  286. goto out;
  287. }
  288. if (of_property_read_u64_array(power_mgt,
  289. "ibm,cpu-idle-state-psscr-mask",
  290. psscr_mask, dt_idle_states)) {
  291. pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
  292. goto out;
  293. }
  294. }
  295. count = of_property_count_u32_elems(power_mgt,
  296. "ibm,cpu-idle-state-residency-ns");
  297. if (count < 0) {
  298. rc = count;
  299. } else if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
  300. dt_idle_states,
  301. "ibm,cpu-idle-state-residency-ns",
  302. count) != 0) {
  303. goto out;
  304. } else {
  305. rc = of_property_read_u32_array(power_mgt,
  306. "ibm,cpu-idle-state-residency-ns",
  307. residency_ns, dt_idle_states);
  308. }
  309. for (i = 0; i < dt_idle_states; i++) {
  310. unsigned int exit_latency, target_residency;
  311. bool stops_timebase = false;
  312. /*
  313. * Skip the platform idle state whose flag isn't in
  314. * the supported_cpuidle_states flag mask.
  315. */
  316. if ((flags[i] & supported_flags) != flags[i])
  317. continue;
  318. /*
  319. * If an idle state has exit latency beyond
  320. * POWERNV_THRESHOLD_LATENCY_NS then don't use it
  321. * in cpu-idle.
  322. */
  323. if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS)
  324. continue;
  325. /*
  326. * Firmware passes residency and latency values in ns.
  327. * cpuidle expects it in us.
  328. */
  329. exit_latency = DIV_ROUND_UP(latency_ns[i], 1000);
  330. if (!rc)
  331. target_residency = DIV_ROUND_UP(residency_ns[i], 1000);
  332. else
  333. target_residency = 0;
  334. if (has_stop_states) {
  335. int err = validate_psscr_val_mask(&psscr_val[i],
  336. &psscr_mask[i],
  337. flags[i]);
  338. if (err) {
  339. report_invalid_psscr_val(psscr_val[i], err);
  340. continue;
  341. }
  342. }
  343. if (flags[i] & OPAL_PM_TIMEBASE_STOP)
  344. stops_timebase = true;
  345. /*
  346. * For nap and fastsleep, use default target_residency
  347. * values if f/w does not expose it.
  348. */
  349. if (flags[i] & OPAL_PM_NAP_ENABLED) {
  350. if (!rc)
  351. target_residency = 100;
  352. /* Add NAP state */
  353. add_powernv_state(nr_idle_states, "Nap",
  354. CPUIDLE_FLAG_NONE, nap_loop,
  355. target_residency, exit_latency, 0, 0);
  356. } else if (has_stop_states && !stops_timebase) {
  357. add_powernv_state(nr_idle_states, names[i],
  358. CPUIDLE_FLAG_NONE, stop_loop,
  359. target_residency, exit_latency,
  360. psscr_val[i], psscr_mask[i]);
  361. }
  362. /*
  363. * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
  364. * within this config dependency check.
  365. */
  366. #ifdef CONFIG_TICK_ONESHOT
  367. else if (flags[i] & OPAL_PM_SLEEP_ENABLED ||
  368. flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) {
  369. if (!rc)
  370. target_residency = 300000;
  371. /* Add FASTSLEEP state */
  372. add_powernv_state(nr_idle_states, "FastSleep",
  373. CPUIDLE_FLAG_TIMER_STOP,
  374. fastsleep_loop,
  375. target_residency, exit_latency, 0, 0);
  376. } else if (has_stop_states && stops_timebase) {
  377. add_powernv_state(nr_idle_states, names[i],
  378. CPUIDLE_FLAG_TIMER_STOP, stop_loop,
  379. target_residency, exit_latency,
  380. psscr_val[i], psscr_mask[i]);
  381. }
  382. #endif
  383. else
  384. continue;
  385. nr_idle_states++;
  386. }
  387. out:
  388. return nr_idle_states;
  389. }
  390. /*
  391. * powernv_idle_probe()
  392. * Choose state table for shared versus dedicated partition
  393. */
  394. static int powernv_idle_probe(void)
  395. {
  396. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  397. return -ENODEV;
  398. if (firmware_has_feature(FW_FEATURE_OPAL)) {
  399. cpuidle_state_table = powernv_states;
  400. /* Device tree can indicate more idle states */
  401. max_idle_state = powernv_add_idle_states();
  402. if (max_idle_state > 1) {
  403. snooze_timeout_en = true;
  404. snooze_timeout = powernv_states[1].target_residency *
  405. tb_ticks_per_usec;
  406. }
  407. } else
  408. return -ENODEV;
  409. return 0;
  410. }
  411. static int __init powernv_processor_idle_init(void)
  412. {
  413. int retval;
  414. retval = powernv_idle_probe();
  415. if (retval)
  416. return retval;
  417. powernv_cpuidle_driver_init();
  418. retval = cpuidle_register(&powernv_idle_driver, NULL);
  419. if (retval) {
  420. printk(KERN_DEBUG "Registration of powernv driver failed.\n");
  421. return retval;
  422. }
  423. retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  424. "cpuidle/powernv:online",
  425. powernv_cpuidle_cpu_online, NULL);
  426. WARN_ON(retval < 0);
  427. retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
  428. "cpuidle/powernv:dead", NULL,
  429. powernv_cpuidle_cpu_dead);
  430. WARN_ON(retval < 0);
  431. printk(KERN_DEBUG "powernv_idle_driver registered\n");
  432. return 0;
  433. }
  434. device_initcall(powernv_processor_idle_init);