cpuidle-psci.c 10 KB

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