cpuidle-psci.c 10 KB

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