cpuidle-psci-domain.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PM domains for CPUs via genpd - managed by cpuidle-psci.
  4. *
  5. * Copyright (C) 2019 Linaro Ltd.
  6. * Author: Ulf Hansson <ulf.hansson@linaro.org>
  7. *
  8. */
  9. #define pr_fmt(fmt) "CPUidle PSCI: " fmt
  10. #include <linux/cpu.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/psci.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include "cpuidle-psci.h"
  20. struct psci_pd_provider {
  21. struct list_head link;
  22. struct device_node *node;
  23. };
  24. static LIST_HEAD(psci_pd_providers);
  25. static bool psci_pd_allow_domain_state;
  26. static int psci_pd_power_off(struct generic_pm_domain *pd)
  27. {
  28. struct genpd_power_state *state = &pd->states[pd->state_idx];
  29. u32 *pd_state;
  30. if (!state->data)
  31. return 0;
  32. if (!psci_pd_allow_domain_state)
  33. return -EBUSY;
  34. /* OSI mode is enabled, set the corresponding domain state. */
  35. pd_state = state->data;
  36. psci_set_domain_state(*pd_state);
  37. return 0;
  38. }
  39. static int psci_pd_parse_state_nodes(struct genpd_power_state *states,
  40. int state_count)
  41. {
  42. int i, ret;
  43. u32 psci_state, *psci_state_buf;
  44. for (i = 0; i < state_count; i++) {
  45. ret = psci_dt_parse_state_node(to_of_node(states[i].fwnode),
  46. &psci_state);
  47. if (ret)
  48. goto free_state;
  49. psci_state_buf = kmalloc(sizeof(u32), GFP_KERNEL);
  50. if (!psci_state_buf) {
  51. ret = -ENOMEM;
  52. goto free_state;
  53. }
  54. *psci_state_buf = psci_state;
  55. states[i].data = psci_state_buf;
  56. }
  57. return 0;
  58. free_state:
  59. i--;
  60. for (; i >= 0; i--)
  61. kfree(states[i].data);
  62. return ret;
  63. }
  64. static int psci_pd_parse_states(struct device_node *np,
  65. struct genpd_power_state **states, int *state_count)
  66. {
  67. int ret;
  68. /* Parse the domain idle states. */
  69. ret = of_genpd_parse_idle_states(np, states, state_count);
  70. if (ret)
  71. return ret;
  72. /* Fill out the PSCI specifics for each found state. */
  73. ret = psci_pd_parse_state_nodes(*states, *state_count);
  74. if (ret)
  75. kfree(*states);
  76. return ret;
  77. }
  78. static void psci_pd_free_states(struct genpd_power_state *states,
  79. unsigned int state_count)
  80. {
  81. int i;
  82. for (i = 0; i < state_count; i++)
  83. kfree(states[i].data);
  84. kfree(states);
  85. }
  86. static int psci_pd_init(struct device_node *np, bool use_osi)
  87. {
  88. struct generic_pm_domain *pd;
  89. struct psci_pd_provider *pd_provider;
  90. struct dev_power_governor *pd_gov;
  91. struct genpd_power_state *states = NULL;
  92. int ret = -ENOMEM, state_count = 0;
  93. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  94. if (!pd)
  95. goto out;
  96. pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
  97. if (!pd_provider)
  98. goto free_pd;
  99. pd->name = kasprintf(GFP_KERNEL, "%pOF", np);
  100. if (!pd->name)
  101. goto free_pd_prov;
  102. /*
  103. * Parse the domain idle states and let genpd manage the state selection
  104. * for those being compatible with "domain-idle-state".
  105. */
  106. ret = psci_pd_parse_states(np, &states, &state_count);
  107. if (ret)
  108. goto free_name;
  109. pd->free_states = psci_pd_free_states;
  110. pd->name = kbasename(pd->name);
  111. pd->states = states;
  112. pd->state_count = state_count;
  113. pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
  114. /* Allow power off when OSI has been successfully enabled. */
  115. if (use_osi)
  116. pd->power_off = psci_pd_power_off;
  117. else
  118. pd->flags |= GENPD_FLAG_ALWAYS_ON;
  119. /* Use governor for CPU PM domains if it has some states to manage. */
  120. pd_gov = state_count > 0 ? &pm_domain_cpu_gov : NULL;
  121. ret = pm_genpd_init(pd, pd_gov, false);
  122. if (ret) {
  123. psci_pd_free_states(states, state_count);
  124. goto free_name;
  125. }
  126. ret = of_genpd_add_provider_simple(np, pd);
  127. if (ret)
  128. goto remove_pd;
  129. pd_provider->node = of_node_get(np);
  130. list_add(&pd_provider->link, &psci_pd_providers);
  131. pr_debug("init PM domain %s\n", pd->name);
  132. return 0;
  133. remove_pd:
  134. pm_genpd_remove(pd);
  135. free_name:
  136. kfree(pd->name);
  137. free_pd_prov:
  138. kfree(pd_provider);
  139. free_pd:
  140. kfree(pd);
  141. out:
  142. pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
  143. return ret;
  144. }
  145. static void psci_pd_remove(void)
  146. {
  147. struct psci_pd_provider *pd_provider, *it;
  148. struct generic_pm_domain *genpd;
  149. list_for_each_entry_safe(pd_provider, it, &psci_pd_providers, link) {
  150. of_genpd_del_provider(pd_provider->node);
  151. genpd = of_genpd_remove_last(pd_provider->node);
  152. if (!IS_ERR(genpd))
  153. kfree(genpd);
  154. of_node_put(pd_provider->node);
  155. list_del(&pd_provider->link);
  156. kfree(pd_provider);
  157. }
  158. }
  159. static int psci_pd_init_topology(struct device_node *np)
  160. {
  161. struct device_node *node;
  162. struct of_phandle_args child, parent;
  163. int ret;
  164. for_each_child_of_node(np, node) {
  165. if (of_parse_phandle_with_args(node, "power-domains",
  166. "#power-domain-cells", 0, &parent))
  167. continue;
  168. child.np = node;
  169. child.args_count = 0;
  170. ret = of_genpd_add_subdomain(&parent, &child);
  171. of_node_put(parent.np);
  172. if (ret) {
  173. of_node_put(node);
  174. return ret;
  175. }
  176. }
  177. return 0;
  178. }
  179. static bool psci_pd_try_set_osi_mode(void)
  180. {
  181. int ret;
  182. if (!psci_has_osi_support())
  183. return false;
  184. ret = psci_set_osi_mode(true);
  185. if (ret) {
  186. pr_warn("failed to enable OSI mode: %d\n", ret);
  187. return false;
  188. }
  189. return true;
  190. }
  191. static void psci_cpuidle_domain_sync_state(struct device *dev)
  192. {
  193. /*
  194. * All devices have now been attached/probed to the PM domain topology,
  195. * hence it's fine to allow domain states to be picked.
  196. */
  197. psci_pd_allow_domain_state = true;
  198. }
  199. static const struct of_device_id psci_of_match[] = {
  200. { .compatible = "arm,psci-1.0" },
  201. {}
  202. };
  203. static int psci_cpuidle_domain_probe(struct platform_device *pdev)
  204. {
  205. struct device_node *np = pdev->dev.of_node;
  206. struct device_node *node;
  207. bool use_osi;
  208. int ret = 0, pd_count = 0;
  209. if (!np)
  210. return -ENODEV;
  211. /* If OSI mode is supported, let's try to enable it. */
  212. use_osi = psci_pd_try_set_osi_mode();
  213. /*
  214. * Parse child nodes for the "#power-domain-cells" property and
  215. * initialize a genpd/genpd-of-provider pair when it's found.
  216. */
  217. for_each_child_of_node(np, node) {
  218. if (!of_find_property(node, "#power-domain-cells", NULL))
  219. continue;
  220. ret = psci_pd_init(node, use_osi);
  221. if (ret)
  222. goto put_node;
  223. pd_count++;
  224. }
  225. /* Bail out if not using the hierarchical CPU topology. */
  226. if (!pd_count)
  227. goto no_pd;
  228. /* Link genpd masters/subdomains to model the CPU topology. */
  229. ret = psci_pd_init_topology(np);
  230. if (ret)
  231. goto remove_pd;
  232. pr_info("Initialized CPU PM domain topology\n");
  233. return 0;
  234. put_node:
  235. of_node_put(node);
  236. remove_pd:
  237. psci_pd_remove();
  238. pr_err("failed to create CPU PM domains ret=%d\n", ret);
  239. no_pd:
  240. if (use_osi)
  241. psci_set_osi_mode(false);
  242. return ret;
  243. }
  244. static struct platform_driver psci_cpuidle_domain_driver = {
  245. .probe = psci_cpuidle_domain_probe,
  246. .driver = {
  247. .name = "psci-cpuidle-domain",
  248. .of_match_table = psci_of_match,
  249. .sync_state = psci_cpuidle_domain_sync_state,
  250. },
  251. };
  252. static int __init psci_idle_init_domains(void)
  253. {
  254. return platform_driver_register(&psci_cpuidle_domain_driver);
  255. }
  256. subsys_initcall(psci_idle_init_domains);
  257. struct device *psci_dt_attach_cpu(int cpu)
  258. {
  259. struct device *dev;
  260. dev = dev_pm_domain_attach_by_name(get_cpu_device(cpu), "psci");
  261. if (IS_ERR_OR_NULL(dev))
  262. return dev;
  263. pm_runtime_irq_safe(dev);
  264. if (cpu_online(cpu))
  265. pm_runtime_get_sync(dev);
  266. dev_pm_syscore_device(dev, true);
  267. return dev;
  268. }
  269. void psci_dt_detach_cpu(struct device *dev)
  270. {
  271. if (IS_ERR_OR_NULL(dev))
  272. return;
  273. dev_pm_domain_detach(dev, false);
  274. }