cpuidle-psci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PSCI CPU idle driver.
  4. *
  5. * Copyright (C) 2019 ARM Ltd.
  6. * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
  7. */
  8. #define pr_fmt(fmt) "CPUidle PSCI: " fmt
  9. #include <linux/cpuhotplug.h>
  10. #include <linux/cpu_cooling.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/cpu_pm.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/device/faux.h>
  18. #include <linux/psci.h>
  19. #include <linux/pm_domain.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include <linux/syscore_ops.h>
  24. #include <asm/cpuidle.h>
  25. #include <trace/events/power.h>
  26. #include "cpuidle-psci.h"
  27. #include "dt_idle_states.h"
  28. #include "dt_idle_genpd.h"
  29. struct psci_cpuidle_data {
  30. u32 *psci_states;
  31. struct device *dev;
  32. };
  33. struct psci_cpuidle_domain_state {
  34. struct generic_pm_domain *pd;
  35. unsigned int state_idx;
  36. u32 state;
  37. };
  38. static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
  39. static DEFINE_PER_CPU(struct psci_cpuidle_domain_state, psci_domain_state);
  40. static bool psci_cpuidle_use_syscore;
  41. void psci_set_domain_state(struct generic_pm_domain *pd, unsigned int state_idx,
  42. u32 state)
  43. {
  44. struct psci_cpuidle_domain_state *ds = this_cpu_ptr(&psci_domain_state);
  45. ds->pd = pd;
  46. ds->state_idx = state_idx;
  47. ds->state = state;
  48. }
  49. static inline void psci_clear_domain_state(void)
  50. {
  51. __this_cpu_write(psci_domain_state.state, 0);
  52. }
  53. static __cpuidle int __psci_enter_domain_idle_state(struct cpuidle_device *dev,
  54. struct cpuidle_driver *drv, int idx,
  55. bool s2idle)
  56. {
  57. struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
  58. u32 *states = data->psci_states;
  59. struct device *pd_dev = data->dev;
  60. struct psci_cpuidle_domain_state *ds;
  61. u32 state = states[idx];
  62. int ret;
  63. ret = cpu_pm_enter();
  64. if (ret)
  65. return -1;
  66. /* Do runtime PM to manage a hierarchical CPU toplogy. */
  67. if (s2idle)
  68. dev_pm_genpd_suspend(pd_dev);
  69. else
  70. pm_runtime_put_sync_suspend(pd_dev);
  71. ds = this_cpu_ptr(&psci_domain_state);
  72. if (ds->state)
  73. state = ds->state;
  74. trace_psci_domain_idle_enter(dev->cpu, state, s2idle);
  75. ret = psci_cpu_suspend_enter(state) ? -1 : idx;
  76. trace_psci_domain_idle_exit(dev->cpu, state, s2idle);
  77. if (s2idle)
  78. dev_pm_genpd_resume(pd_dev);
  79. else
  80. pm_runtime_get_sync(pd_dev);
  81. cpu_pm_exit();
  82. /* Correct domain-idlestate statistics if we failed to enter. */
  83. if (ret == -1 && ds->state)
  84. pm_genpd_inc_rejected(ds->pd, ds->state_idx);
  85. /* Clear the domain state to start fresh when back from idle. */
  86. psci_clear_domain_state();
  87. return ret;
  88. }
  89. static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
  90. struct cpuidle_driver *drv, int idx)
  91. {
  92. return __psci_enter_domain_idle_state(dev, drv, idx, false);
  93. }
  94. static int psci_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
  95. struct cpuidle_driver *drv,
  96. int idx)
  97. {
  98. return __psci_enter_domain_idle_state(dev, drv, idx, true);
  99. }
  100. static int psci_idle_cpuhp_up(unsigned int cpu)
  101. {
  102. struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
  103. if (pd_dev) {
  104. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  105. pm_runtime_get_sync(pd_dev);
  106. else
  107. dev_pm_genpd_resume(pd_dev);
  108. }
  109. return 0;
  110. }
  111. static int psci_idle_cpuhp_down(unsigned int cpu)
  112. {
  113. struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
  114. if (pd_dev) {
  115. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  116. pm_runtime_put_sync(pd_dev);
  117. else
  118. dev_pm_genpd_suspend(pd_dev);
  119. /* Clear domain state to start fresh at next online. */
  120. psci_clear_domain_state();
  121. }
  122. return 0;
  123. }
  124. static void psci_idle_syscore_switch(bool suspend)
  125. {
  126. bool cleared = false;
  127. struct device *dev;
  128. int cpu;
  129. for_each_possible_cpu(cpu) {
  130. dev = per_cpu_ptr(&psci_cpuidle_data, cpu)->dev;
  131. if (dev && suspend) {
  132. dev_pm_genpd_suspend(dev);
  133. } else if (dev) {
  134. dev_pm_genpd_resume(dev);
  135. /* Account for userspace having offlined a CPU. */
  136. if (pm_runtime_status_suspended(dev))
  137. pm_runtime_set_active(dev);
  138. /* Clear domain state to re-start fresh. */
  139. if (!cleared) {
  140. psci_clear_domain_state();
  141. cleared = true;
  142. }
  143. }
  144. }
  145. }
  146. static int psci_idle_syscore_suspend(void *data)
  147. {
  148. psci_idle_syscore_switch(true);
  149. return 0;
  150. }
  151. static void psci_idle_syscore_resume(void *data)
  152. {
  153. psci_idle_syscore_switch(false);
  154. }
  155. static const struct syscore_ops psci_idle_syscore_ops = {
  156. .suspend = psci_idle_syscore_suspend,
  157. .resume = psci_idle_syscore_resume,
  158. };
  159. static struct syscore psci_idle_syscore = {
  160. .ops = &psci_idle_syscore_ops,
  161. };
  162. static void psci_idle_init_syscore(void)
  163. {
  164. if (psci_cpuidle_use_syscore)
  165. register_syscore(&psci_idle_syscore);
  166. }
  167. static void psci_idle_init_cpuhp(void)
  168. {
  169. int err;
  170. err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
  171. "cpuidle/psci:online",
  172. psci_idle_cpuhp_up,
  173. psci_idle_cpuhp_down);
  174. if (err)
  175. pr_warn("Failed %d while setup cpuhp state\n", err);
  176. }
  177. static __cpuidle int psci_enter_idle_state(struct cpuidle_device *dev,
  178. struct cpuidle_driver *drv, int idx)
  179. {
  180. u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
  181. return CPU_PM_CPU_IDLE_ENTER_PARAM_RCU(psci_cpu_suspend_enter, idx, state[idx]);
  182. }
  183. static const struct of_device_id psci_idle_state_match[] = {
  184. { .compatible = "arm,idle-state",
  185. .data = psci_enter_idle_state },
  186. { },
  187. };
  188. int psci_dt_parse_state_node(struct device_node *np, u32 *state)
  189. {
  190. int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
  191. if (err) {
  192. pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
  193. return err;
  194. }
  195. if (!psci_power_state_is_valid(*state)) {
  196. pr_warn("Invalid PSCI power state %#x\n", *state);
  197. return -EINVAL;
  198. }
  199. return 0;
  200. }
  201. static int psci_dt_cpu_init_topology(struct cpuidle_driver *drv,
  202. struct psci_cpuidle_data *data,
  203. unsigned int state_count, int cpu)
  204. {
  205. /* Currently limit the hierarchical topology to be used in OSI mode. */
  206. if (!psci_has_osi_support())
  207. return 0;
  208. data->dev = dt_idle_attach_cpu(cpu, "psci");
  209. if (IS_ERR_OR_NULL(data->dev))
  210. return PTR_ERR_OR_ZERO(data->dev);
  211. psci_cpuidle_use_syscore = true;
  212. /*
  213. * Using the deepest state for the CPU to trigger a potential selection
  214. * of a shared state for the domain, assumes the domain states are all
  215. * deeper states. On PREEMPT_RT the hierarchical topology is limited to
  216. * s2ram and s2idle.
  217. */
  218. drv->states[state_count - 1].enter_s2idle = psci_enter_s2idle_domain_idle_state;
  219. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  220. drv->states[state_count - 1].enter = psci_enter_domain_idle_state;
  221. return 0;
  222. }
  223. static int psci_dt_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
  224. struct device_node *cpu_node,
  225. unsigned int state_count, int cpu)
  226. {
  227. int i, ret = 0;
  228. u32 *psci_states;
  229. struct device_node *state_node;
  230. struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
  231. state_count++; /* Add WFI state too */
  232. psci_states = devm_kcalloc(dev, state_count, sizeof(*psci_states),
  233. GFP_KERNEL);
  234. if (!psci_states)
  235. return -ENOMEM;
  236. for (i = 1; i < state_count; i++) {
  237. state_node = of_get_cpu_state_node(cpu_node, i - 1);
  238. if (!state_node)
  239. break;
  240. ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
  241. of_node_put(state_node);
  242. if (ret)
  243. return ret;
  244. pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
  245. }
  246. if (i != state_count)
  247. return -ENODEV;
  248. /* Initialize optional data, used for the hierarchical topology. */
  249. ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu);
  250. if (ret < 0)
  251. return ret;
  252. /* Idle states parsed correctly, store them in the per-cpu struct. */
  253. data->psci_states = psci_states;
  254. return 0;
  255. }
  256. static int psci_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
  257. unsigned int cpu, unsigned int state_count)
  258. {
  259. struct device_node *cpu_node;
  260. int ret;
  261. /*
  262. * If the PSCI cpu_suspend function hook has not been initialized
  263. * idle states must not be enabled, so bail out
  264. */
  265. if (!psci_ops.cpu_suspend)
  266. return -EOPNOTSUPP;
  267. cpu_node = of_cpu_device_node_get(cpu);
  268. if (!cpu_node)
  269. return -ENODEV;
  270. ret = psci_dt_cpu_init_idle(dev, drv, cpu_node, state_count, cpu);
  271. of_node_put(cpu_node);
  272. return ret;
  273. }
  274. static void psci_cpu_deinit_idle(int cpu)
  275. {
  276. struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
  277. dt_idle_detach_cpu(data->dev);
  278. psci_cpuidle_use_syscore = false;
  279. }
  280. static int psci_idle_init_cpu(struct device *dev, int cpu)
  281. {
  282. struct cpuidle_driver *drv;
  283. struct device_node *cpu_node;
  284. const char *enable_method;
  285. int ret = 0;
  286. cpu_node = of_cpu_device_node_get(cpu);
  287. if (!cpu_node)
  288. return -ENODEV;
  289. /*
  290. * Check whether the enable-method for the cpu is PSCI, fail
  291. * if it is not.
  292. */
  293. enable_method = of_get_property(cpu_node, "enable-method", NULL);
  294. if (!enable_method || (strcmp(enable_method, "psci")))
  295. ret = -ENODEV;
  296. of_node_put(cpu_node);
  297. if (ret)
  298. return ret;
  299. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  300. if (!drv)
  301. return -ENOMEM;
  302. drv->name = "psci_idle";
  303. drv->owner = THIS_MODULE;
  304. drv->cpumask = (struct cpumask *)cpumask_of(cpu);
  305. /*
  306. * PSCI idle states relies on architectural WFI to be represented as
  307. * state index 0.
  308. */
  309. drv->states[0].enter = psci_enter_idle_state;
  310. drv->states[0].exit_latency = 1;
  311. drv->states[0].target_residency = 1;
  312. drv->states[0].power_usage = UINT_MAX;
  313. strscpy(drv->states[0].name, "WFI");
  314. strscpy(drv->states[0].desc, "ARM WFI");
  315. /*
  316. * If no DT idle states are detected (ret == 0) let the driver
  317. * initialization fail accordingly since there is no reason to
  318. * initialize the idle driver if only wfi is supported, the
  319. * default archictectural back-end already executes wfi
  320. * on idle entry.
  321. */
  322. ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
  323. if (ret <= 0)
  324. return ret ? : -ENODEV;
  325. /*
  326. * Initialize PSCI idle states.
  327. */
  328. ret = psci_cpu_init_idle(dev, drv, cpu, ret);
  329. if (ret) {
  330. pr_err("CPU %d failed to PSCI idle\n", cpu);
  331. return ret;
  332. }
  333. ret = cpuidle_register(drv, NULL);
  334. if (ret)
  335. goto deinit;
  336. cpuidle_cooling_register(drv);
  337. return 0;
  338. deinit:
  339. psci_cpu_deinit_idle(cpu);
  340. return ret;
  341. }
  342. /*
  343. * psci_idle_probe - Initializes PSCI cpuidle driver
  344. *
  345. * Initializes PSCI cpuidle driver for all present CPUs, if any CPU fails
  346. * to register cpuidle driver then rollback to cancel all CPUs
  347. * registration.
  348. */
  349. static int psci_cpuidle_probe(struct faux_device *fdev)
  350. {
  351. int cpu, ret;
  352. struct cpuidle_driver *drv;
  353. struct cpuidle_device *dev;
  354. for_each_present_cpu(cpu) {
  355. ret = psci_idle_init_cpu(&fdev->dev, cpu);
  356. if (ret)
  357. goto out_fail;
  358. }
  359. psci_idle_init_syscore();
  360. psci_idle_init_cpuhp();
  361. return 0;
  362. out_fail:
  363. while (--cpu >= 0) {
  364. dev = per_cpu(cpuidle_devices, cpu);
  365. drv = cpuidle_get_cpu_driver(dev);
  366. cpuidle_unregister(drv);
  367. psci_cpu_deinit_idle(cpu);
  368. }
  369. return ret;
  370. }
  371. static struct faux_device_ops psci_cpuidle_ops = {
  372. .probe = psci_cpuidle_probe,
  373. };
  374. static bool __init dt_idle_state_present(void)
  375. {
  376. struct device_node *cpu_node __free(device_node) =
  377. of_cpu_device_node_get(cpumask_first(cpu_possible_mask));
  378. if (!cpu_node)
  379. return false;
  380. struct device_node *state_node __free(device_node) =
  381. of_get_cpu_state_node(cpu_node, 0);
  382. if (!state_node)
  383. return false;
  384. return !!of_match_node(psci_idle_state_match, state_node);
  385. }
  386. static int __init psci_idle_init(void)
  387. {
  388. struct faux_device *fdev;
  389. if (!dt_idle_state_present())
  390. return 0;
  391. fdev = faux_device_create("psci-cpuidle", NULL, &psci_cpuidle_ops);
  392. if (!fdev) {
  393. pr_err("Failed to create psci-cpuidle device\n");
  394. return -ENODEV;
  395. }
  396. return 0;
  397. }
  398. device_initcall(psci_idle_init);