cpuidle-powernv.c 14 KB

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