cpuidle-psci.c 9.4 KB

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