cpuidle-riscv-sbi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * RISC-V SBI CPU idle driver.
  4. *
  5. * Copyright (c) 2021 Western Digital Corporation or its affiliates.
  6. * Copyright (c) 2022 Ventana Micro Systems Inc.
  7. */
  8. #define pr_fmt(fmt) "cpuidle-riscv-sbi: " fmt
  9. #include <linux/cleanup.h>
  10. #include <linux/cpuhotplug.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/cpu_pm.h>
  14. #include <linux/cpu_cooling.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. #include <linux/string.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_domain.h>
  22. #include <linux/pm_runtime.h>
  23. #include <asm/cpuidle.h>
  24. #include <asm/sbi.h>
  25. #include <asm/smp.h>
  26. #include <asm/suspend.h>
  27. #include "cpuidle.h"
  28. #include "dt_idle_states.h"
  29. #include "dt_idle_genpd.h"
  30. struct sbi_cpuidle_data {
  31. u32 *states;
  32. struct device *dev;
  33. };
  34. struct sbi_domain_state {
  35. bool available;
  36. u32 state;
  37. };
  38. static DEFINE_PER_CPU_READ_MOSTLY(struct sbi_cpuidle_data, sbi_cpuidle_data);
  39. static DEFINE_PER_CPU(struct sbi_domain_state, domain_state);
  40. static bool sbi_cpuidle_use_osi;
  41. static bool sbi_cpuidle_use_cpuhp;
  42. static inline void sbi_set_domain_state(u32 state)
  43. {
  44. struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
  45. data->available = true;
  46. data->state = state;
  47. }
  48. static inline u32 sbi_get_domain_state(void)
  49. {
  50. struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
  51. return data->state;
  52. }
  53. static inline void sbi_clear_domain_state(void)
  54. {
  55. struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
  56. data->available = false;
  57. }
  58. static inline bool sbi_is_domain_state_available(void)
  59. {
  60. struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
  61. return data->available;
  62. }
  63. static __cpuidle int sbi_cpuidle_enter_state(struct cpuidle_device *dev,
  64. struct cpuidle_driver *drv, int idx)
  65. {
  66. u32 *states = __this_cpu_read(sbi_cpuidle_data.states);
  67. u32 state = states[idx];
  68. if (state & SBI_HSM_SUSP_NON_RET_BIT)
  69. return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_sbi_hart_suspend, idx, state);
  70. else
  71. return CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(riscv_sbi_hart_suspend,
  72. idx, state);
  73. }
  74. static __cpuidle int __sbi_enter_domain_idle_state(struct cpuidle_device *dev,
  75. struct cpuidle_driver *drv, int idx,
  76. bool s2idle)
  77. {
  78. struct sbi_cpuidle_data *data = this_cpu_ptr(&sbi_cpuidle_data);
  79. u32 *states = data->states;
  80. struct device *pd_dev = data->dev;
  81. u32 state;
  82. int ret;
  83. ret = cpu_pm_enter();
  84. if (ret)
  85. return -1;
  86. /* Do runtime PM to manage a hierarchical CPU toplogy. */
  87. if (s2idle)
  88. dev_pm_genpd_suspend(pd_dev);
  89. else
  90. pm_runtime_put_sync_suspend(pd_dev);
  91. ct_cpuidle_enter();
  92. if (sbi_is_domain_state_available())
  93. state = sbi_get_domain_state();
  94. else
  95. state = states[idx];
  96. ret = riscv_sbi_hart_suspend(state) ? -1 : idx;
  97. ct_cpuidle_exit();
  98. if (s2idle)
  99. dev_pm_genpd_resume(pd_dev);
  100. else
  101. pm_runtime_get_sync(pd_dev);
  102. cpu_pm_exit();
  103. /* Clear the domain state to start fresh when back from idle. */
  104. sbi_clear_domain_state();
  105. return ret;
  106. }
  107. static int sbi_enter_domain_idle_state(struct cpuidle_device *dev,
  108. struct cpuidle_driver *drv, int idx)
  109. {
  110. return __sbi_enter_domain_idle_state(dev, drv, idx, false);
  111. }
  112. static int sbi_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
  113. struct cpuidle_driver *drv,
  114. int idx)
  115. {
  116. return __sbi_enter_domain_idle_state(dev, drv, idx, true);
  117. }
  118. static int sbi_cpuidle_cpuhp_up(unsigned int cpu)
  119. {
  120. struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
  121. if (pd_dev)
  122. pm_runtime_get_sync(pd_dev);
  123. return 0;
  124. }
  125. static int sbi_cpuidle_cpuhp_down(unsigned int cpu)
  126. {
  127. struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
  128. if (pd_dev) {
  129. pm_runtime_put_sync(pd_dev);
  130. /* Clear domain state to start fresh at next online. */
  131. sbi_clear_domain_state();
  132. }
  133. return 0;
  134. }
  135. static void sbi_idle_init_cpuhp(void)
  136. {
  137. int err;
  138. if (!sbi_cpuidle_use_cpuhp)
  139. return;
  140. err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
  141. "cpuidle/sbi:online",
  142. sbi_cpuidle_cpuhp_up,
  143. sbi_cpuidle_cpuhp_down);
  144. if (err)
  145. pr_warn("Failed %d while setup cpuhp state\n", err);
  146. }
  147. static const struct of_device_id sbi_cpuidle_state_match[] = {
  148. { .compatible = "riscv,idle-state",
  149. .data = sbi_cpuidle_enter_state },
  150. { },
  151. };
  152. static int sbi_dt_parse_state_node(struct device_node *np, u32 *state)
  153. {
  154. int err = of_property_read_u32(np, "riscv,sbi-suspend-param", state);
  155. if (err) {
  156. pr_warn("%pOF missing riscv,sbi-suspend-param property\n", np);
  157. return err;
  158. }
  159. if (!riscv_sbi_suspend_state_is_valid(*state)) {
  160. pr_warn("Invalid SBI suspend state %#x\n", *state);
  161. return -EINVAL;
  162. }
  163. return 0;
  164. }
  165. static int sbi_dt_cpu_init_topology(struct cpuidle_driver *drv,
  166. struct sbi_cpuidle_data *data,
  167. unsigned int state_count, int cpu)
  168. {
  169. /* Currently limit the hierarchical topology to be used in OSI mode. */
  170. if (!sbi_cpuidle_use_osi)
  171. return 0;
  172. data->dev = dt_idle_attach_cpu(cpu, "sbi");
  173. if (IS_ERR_OR_NULL(data->dev))
  174. return PTR_ERR_OR_ZERO(data->dev);
  175. /*
  176. * Using the deepest state for the CPU to trigger a potential selection
  177. * of a shared state for the domain, assumes the domain states are all
  178. * deeper states.
  179. */
  180. drv->states[state_count - 1].flags |= CPUIDLE_FLAG_RCU_IDLE;
  181. drv->states[state_count - 1].enter = sbi_enter_domain_idle_state;
  182. drv->states[state_count - 1].enter_s2idle =
  183. sbi_enter_s2idle_domain_idle_state;
  184. sbi_cpuidle_use_cpuhp = true;
  185. return 0;
  186. }
  187. static int sbi_cpuidle_dt_init_states(struct device *dev,
  188. struct cpuidle_driver *drv,
  189. unsigned int cpu,
  190. unsigned int state_count)
  191. {
  192. struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
  193. struct device_node *state_node;
  194. u32 *states;
  195. int i, ret;
  196. struct device_node *cpu_node __free(device_node) = of_cpu_device_node_get(cpu);
  197. if (!cpu_node)
  198. return -ENODEV;
  199. states = devm_kcalloc(dev, state_count, sizeof(*states), GFP_KERNEL);
  200. if (!states)
  201. return -ENOMEM;
  202. /* Parse SBI specific details from state DT nodes */
  203. for (i = 1; i < state_count; i++) {
  204. state_node = of_get_cpu_state_node(cpu_node, i - 1);
  205. if (!state_node)
  206. break;
  207. ret = sbi_dt_parse_state_node(state_node, &states[i]);
  208. of_node_put(state_node);
  209. if (ret)
  210. return ret;
  211. pr_debug("sbi-state %#x index %d\n", states[i], i);
  212. }
  213. if (i != state_count)
  214. return -ENODEV;
  215. /* Initialize optional data, used for the hierarchical topology. */
  216. ret = sbi_dt_cpu_init_topology(drv, data, state_count, cpu);
  217. if (ret < 0)
  218. return ret;
  219. /* Store states in the per-cpu struct. */
  220. data->states = states;
  221. return 0;
  222. }
  223. static void sbi_cpuidle_deinit_cpu(int cpu)
  224. {
  225. struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
  226. dt_idle_detach_cpu(data->dev);
  227. sbi_cpuidle_use_cpuhp = false;
  228. }
  229. static int sbi_cpuidle_init_cpu(struct device *dev, int cpu)
  230. {
  231. struct cpuidle_driver *drv;
  232. unsigned int state_count = 0;
  233. int ret = 0;
  234. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  235. if (!drv)
  236. return -ENOMEM;
  237. drv->name = "sbi_cpuidle";
  238. drv->owner = THIS_MODULE;
  239. drv->cpumask = (struct cpumask *)cpumask_of(cpu);
  240. /* RISC-V architectural WFI to be represented as state index 0. */
  241. drv->states[0].enter = sbi_cpuidle_enter_state;
  242. drv->states[0].exit_latency = 1;
  243. drv->states[0].target_residency = 1;
  244. drv->states[0].power_usage = UINT_MAX;
  245. strscpy(drv->states[0].name, "WFI");
  246. strscpy(drv->states[0].desc, "RISC-V WFI");
  247. /*
  248. * If no DT idle states are detected (ret == 0) let the driver
  249. * initialization fail accordingly since there is no reason to
  250. * initialize the idle driver if only wfi is supported, the
  251. * default archictectural back-end already executes wfi
  252. * on idle entry.
  253. */
  254. ret = dt_init_idle_driver(drv, sbi_cpuidle_state_match, 1);
  255. if (ret <= 0) {
  256. pr_debug("HART%ld: failed to parse DT idle states\n",
  257. cpuid_to_hartid_map(cpu));
  258. return ret ? : -ENODEV;
  259. }
  260. state_count = ret + 1; /* Include WFI state as well */
  261. /* Initialize idle states from DT. */
  262. ret = sbi_cpuidle_dt_init_states(dev, drv, cpu, state_count);
  263. if (ret) {
  264. pr_err("HART%ld: failed to init idle states\n",
  265. cpuid_to_hartid_map(cpu));
  266. return ret;
  267. }
  268. if (cpuidle_disabled())
  269. return 0;
  270. ret = cpuidle_register(drv, NULL);
  271. if (ret)
  272. goto deinit;
  273. cpuidle_cooling_register(drv);
  274. return 0;
  275. deinit:
  276. sbi_cpuidle_deinit_cpu(cpu);
  277. return ret;
  278. }
  279. #ifdef CONFIG_DT_IDLE_GENPD
  280. static int sbi_cpuidle_pd_power_off(struct generic_pm_domain *pd)
  281. {
  282. struct genpd_power_state *state = &pd->states[pd->state_idx];
  283. u32 *pd_state;
  284. if (!state->data)
  285. return 0;
  286. /* OSI mode is enabled, set the corresponding domain state. */
  287. pd_state = state->data;
  288. sbi_set_domain_state(*pd_state);
  289. return 0;
  290. }
  291. struct sbi_pd_provider {
  292. struct list_head link;
  293. struct device_node *node;
  294. };
  295. static LIST_HEAD(sbi_pd_providers);
  296. static int sbi_pd_init(struct device_node *np)
  297. {
  298. struct generic_pm_domain *pd;
  299. struct sbi_pd_provider *pd_provider;
  300. struct dev_power_governor *pd_gov;
  301. int ret = -ENOMEM;
  302. pd = dt_idle_pd_alloc(np, sbi_dt_parse_state_node);
  303. if (!pd)
  304. goto out;
  305. pd_provider = kzalloc_obj(*pd_provider);
  306. if (!pd_provider)
  307. goto free_pd;
  308. pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
  309. /* Allow power off when OSI is available. */
  310. if (sbi_cpuidle_use_osi)
  311. pd->power_off = sbi_cpuidle_pd_power_off;
  312. else
  313. pd->flags |= GENPD_FLAG_ALWAYS_ON;
  314. /* Use governor for CPU PM domains if it has some states to manage. */
  315. pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
  316. ret = pm_genpd_init(pd, pd_gov, false);
  317. if (ret)
  318. goto free_pd_prov;
  319. ret = of_genpd_add_provider_simple(np, pd);
  320. if (ret)
  321. goto remove_pd;
  322. pd_provider->node = of_node_get(np);
  323. list_add(&pd_provider->link, &sbi_pd_providers);
  324. pr_debug("init PM domain %s\n", pd->name);
  325. return 0;
  326. remove_pd:
  327. pm_genpd_remove(pd);
  328. free_pd_prov:
  329. kfree(pd_provider);
  330. free_pd:
  331. dt_idle_pd_free(pd);
  332. out:
  333. pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
  334. return ret;
  335. }
  336. static void sbi_pd_remove(void)
  337. {
  338. struct sbi_pd_provider *pd_provider, *it;
  339. struct generic_pm_domain *genpd;
  340. list_for_each_entry_safe(pd_provider, it, &sbi_pd_providers, link) {
  341. of_genpd_del_provider(pd_provider->node);
  342. genpd = of_genpd_remove_last(pd_provider->node);
  343. if (!IS_ERR(genpd))
  344. kfree(genpd);
  345. of_node_put(pd_provider->node);
  346. list_del(&pd_provider->link);
  347. kfree(pd_provider);
  348. }
  349. }
  350. static int sbi_genpd_probe(struct device_node *np)
  351. {
  352. int ret = 0, pd_count = 0;
  353. if (!np)
  354. return -ENODEV;
  355. /*
  356. * Parse child nodes for the "#power-domain-cells" property and
  357. * initialize a genpd/genpd-of-provider pair when it's found.
  358. */
  359. for_each_child_of_node_scoped(np, node) {
  360. if (!of_property_present(node, "#power-domain-cells"))
  361. continue;
  362. ret = sbi_pd_init(node);
  363. if (ret)
  364. goto remove_pd;
  365. pd_count++;
  366. }
  367. /* Bail out if not using the hierarchical CPU topology. */
  368. if (!pd_count)
  369. goto no_pd;
  370. /* Link genpd masters/subdomains to model the CPU topology. */
  371. ret = dt_idle_pd_init_topology(np);
  372. if (ret)
  373. goto remove_pd;
  374. return 0;
  375. remove_pd:
  376. sbi_pd_remove();
  377. pr_err("failed to create CPU PM domains ret=%d\n", ret);
  378. no_pd:
  379. return ret;
  380. }
  381. #else
  382. static inline int sbi_genpd_probe(struct device_node *np)
  383. {
  384. return 0;
  385. }
  386. #endif
  387. static int sbi_cpuidle_probe(struct platform_device *pdev)
  388. {
  389. int cpu, ret;
  390. struct cpuidle_driver *drv;
  391. struct cpuidle_device *dev;
  392. struct device_node *pds_node;
  393. /* Detect OSI support based on CPU DT nodes */
  394. sbi_cpuidle_use_osi = true;
  395. for_each_possible_cpu(cpu) {
  396. struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
  397. if (np &&
  398. of_property_present(np, "power-domains") &&
  399. of_property_present(np, "power-domain-names")) {
  400. continue;
  401. } else {
  402. sbi_cpuidle_use_osi = false;
  403. break;
  404. }
  405. }
  406. /* Populate generic power domains from DT nodes */
  407. pds_node = of_find_node_by_path("/cpus/power-domains");
  408. if (pds_node) {
  409. ret = sbi_genpd_probe(pds_node);
  410. of_node_put(pds_node);
  411. if (ret)
  412. return ret;
  413. }
  414. /* Initialize CPU idle driver for each present CPU */
  415. for_each_present_cpu(cpu) {
  416. ret = sbi_cpuidle_init_cpu(&pdev->dev, cpu);
  417. if (ret) {
  418. pr_debug("HART%ld: idle driver init failed\n",
  419. cpuid_to_hartid_map(cpu));
  420. goto out_fail;
  421. }
  422. }
  423. /* Setup CPU hotplut notifiers */
  424. sbi_idle_init_cpuhp();
  425. if (cpuidle_disabled())
  426. pr_info("cpuidle is disabled\n");
  427. else
  428. pr_info("idle driver registered for all CPUs\n");
  429. return 0;
  430. out_fail:
  431. while (--cpu >= 0) {
  432. dev = per_cpu(cpuidle_devices, cpu);
  433. drv = cpuidle_get_cpu_driver(dev);
  434. cpuidle_unregister(drv);
  435. sbi_cpuidle_deinit_cpu(cpu);
  436. }
  437. return ret;
  438. }
  439. static struct platform_driver sbi_cpuidle_driver = {
  440. .probe = sbi_cpuidle_probe,
  441. .driver = {
  442. .name = "sbi-cpuidle",
  443. },
  444. };
  445. static int __init sbi_cpuidle_init(void)
  446. {
  447. int ret;
  448. struct platform_device *pdev;
  449. if (!riscv_sbi_hsm_is_supported())
  450. return 0;
  451. ret = platform_driver_register(&sbi_cpuidle_driver);
  452. if (ret)
  453. return ret;
  454. pdev = platform_device_register_simple("sbi-cpuidle",
  455. -1, NULL, 0);
  456. if (IS_ERR(pdev)) {
  457. platform_driver_unregister(&sbi_cpuidle_driver);
  458. return PTR_ERR(pdev);
  459. }
  460. return 0;
  461. }
  462. arch_initcall(sbi_cpuidle_init);