Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / cpuidle / cpuidle-riscv-sbi.c
blob14462c092039ea444ac5a07b95d8a0c7e30c20bd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * RISC-V SBI CPU idle driver.
5 * Copyright (c) 2021 Western Digital Corporation or its affiliates.
6 * Copyright (c) 2022 Ventana Micro Systems Inc.
7 */
9 #define pr_fmt(fmt) "cpuidle-riscv-sbi: " fmt
11 #include <linux/cleanup.h>
12 #include <linux/cpuhotplug.h>
13 #include <linux/cpuidle.h>
14 #include <linux/cpumask.h>
15 #include <linux/cpu_pm.h>
16 #include <linux/cpu_cooling.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_domain.h>
23 #include <linux/pm_runtime.h>
24 #include <asm/cpuidle.h>
25 #include <asm/sbi.h>
26 #include <asm/smp.h>
27 #include <asm/suspend.h>
29 #include "cpuidle.h"
30 #include "dt_idle_states.h"
31 #include "dt_idle_genpd.h"
33 struct sbi_cpuidle_data {
34 u32 *states;
35 struct device *dev;
38 struct sbi_domain_state {
39 bool available;
40 u32 state;
43 static DEFINE_PER_CPU_READ_MOSTLY(struct sbi_cpuidle_data, sbi_cpuidle_data);
44 static DEFINE_PER_CPU(struct sbi_domain_state, domain_state);
45 static bool sbi_cpuidle_use_osi;
46 static bool sbi_cpuidle_use_cpuhp;
47 static bool sbi_cpuidle_pd_allow_domain_state;
49 static inline void sbi_set_domain_state(u32 state)
51 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
53 data->available = true;
54 data->state = state;
57 static inline u32 sbi_get_domain_state(void)
59 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
61 return data->state;
64 static inline void sbi_clear_domain_state(void)
66 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
68 data->available = false;
71 static inline bool sbi_is_domain_state_available(void)
73 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
75 return data->available;
78 static __cpuidle int sbi_cpuidle_enter_state(struct cpuidle_device *dev,
79 struct cpuidle_driver *drv, int idx)
81 u32 *states = __this_cpu_read(sbi_cpuidle_data.states);
82 u32 state = states[idx];
84 if (state & SBI_HSM_SUSP_NON_RET_BIT)
85 return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_sbi_hart_suspend, idx, state);
86 else
87 return CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(riscv_sbi_hart_suspend,
88 idx, state);
91 static __cpuidle int __sbi_enter_domain_idle_state(struct cpuidle_device *dev,
92 struct cpuidle_driver *drv, int idx,
93 bool s2idle)
95 struct sbi_cpuidle_data *data = this_cpu_ptr(&sbi_cpuidle_data);
96 u32 *states = data->states;
97 struct device *pd_dev = data->dev;
98 u32 state;
99 int ret;
101 ret = cpu_pm_enter();
102 if (ret)
103 return -1;
105 /* Do runtime PM to manage a hierarchical CPU toplogy. */
106 if (s2idle)
107 dev_pm_genpd_suspend(pd_dev);
108 else
109 pm_runtime_put_sync_suspend(pd_dev);
111 ct_cpuidle_enter();
113 if (sbi_is_domain_state_available())
114 state = sbi_get_domain_state();
115 else
116 state = states[idx];
118 ret = riscv_sbi_hart_suspend(state) ? -1 : idx;
120 ct_cpuidle_exit();
122 if (s2idle)
123 dev_pm_genpd_resume(pd_dev);
124 else
125 pm_runtime_get_sync(pd_dev);
127 cpu_pm_exit();
129 /* Clear the domain state to start fresh when back from idle. */
130 sbi_clear_domain_state();
131 return ret;
134 static int sbi_enter_domain_idle_state(struct cpuidle_device *dev,
135 struct cpuidle_driver *drv, int idx)
137 return __sbi_enter_domain_idle_state(dev, drv, idx, false);
140 static int sbi_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
141 struct cpuidle_driver *drv,
142 int idx)
144 return __sbi_enter_domain_idle_state(dev, drv, idx, true);
147 static int sbi_cpuidle_cpuhp_up(unsigned int cpu)
149 struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
151 if (pd_dev)
152 pm_runtime_get_sync(pd_dev);
154 return 0;
157 static int sbi_cpuidle_cpuhp_down(unsigned int cpu)
159 struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
161 if (pd_dev) {
162 pm_runtime_put_sync(pd_dev);
163 /* Clear domain state to start fresh at next online. */
164 sbi_clear_domain_state();
167 return 0;
170 static void sbi_idle_init_cpuhp(void)
172 int err;
174 if (!sbi_cpuidle_use_cpuhp)
175 return;
177 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
178 "cpuidle/sbi:online",
179 sbi_cpuidle_cpuhp_up,
180 sbi_cpuidle_cpuhp_down);
181 if (err)
182 pr_warn("Failed %d while setup cpuhp state\n", err);
185 static const struct of_device_id sbi_cpuidle_state_match[] = {
186 { .compatible = "riscv,idle-state",
187 .data = sbi_cpuidle_enter_state },
188 { },
191 static int sbi_dt_parse_state_node(struct device_node *np, u32 *state)
193 int err = of_property_read_u32(np, "riscv,sbi-suspend-param", state);
195 if (err) {
196 pr_warn("%pOF missing riscv,sbi-suspend-param property\n", np);
197 return err;
200 if (!riscv_sbi_suspend_state_is_valid(*state)) {
201 pr_warn("Invalid SBI suspend state %#x\n", *state);
202 return -EINVAL;
205 return 0;
208 static int sbi_dt_cpu_init_topology(struct cpuidle_driver *drv,
209 struct sbi_cpuidle_data *data,
210 unsigned int state_count, int cpu)
212 /* Currently limit the hierarchical topology to be used in OSI mode. */
213 if (!sbi_cpuidle_use_osi)
214 return 0;
216 data->dev = dt_idle_attach_cpu(cpu, "sbi");
217 if (IS_ERR_OR_NULL(data->dev))
218 return PTR_ERR_OR_ZERO(data->dev);
221 * Using the deepest state for the CPU to trigger a potential selection
222 * of a shared state for the domain, assumes the domain states are all
223 * deeper states.
225 drv->states[state_count - 1].flags |= CPUIDLE_FLAG_RCU_IDLE;
226 drv->states[state_count - 1].enter = sbi_enter_domain_idle_state;
227 drv->states[state_count - 1].enter_s2idle =
228 sbi_enter_s2idle_domain_idle_state;
229 sbi_cpuidle_use_cpuhp = true;
231 return 0;
234 static int sbi_cpuidle_dt_init_states(struct device *dev,
235 struct cpuidle_driver *drv,
236 unsigned int cpu,
237 unsigned int state_count)
239 struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
240 struct device_node *state_node;
241 u32 *states;
242 int i, ret;
244 struct device_node *cpu_node __free(device_node) = of_cpu_device_node_get(cpu);
245 if (!cpu_node)
246 return -ENODEV;
248 states = devm_kcalloc(dev, state_count, sizeof(*states), GFP_KERNEL);
249 if (!states)
250 return -ENOMEM;
252 /* Parse SBI specific details from state DT nodes */
253 for (i = 1; i < state_count; i++) {
254 state_node = of_get_cpu_state_node(cpu_node, i - 1);
255 if (!state_node)
256 break;
258 ret = sbi_dt_parse_state_node(state_node, &states[i]);
259 of_node_put(state_node);
261 if (ret)
262 return ret;
264 pr_debug("sbi-state %#x index %d\n", states[i], i);
266 if (i != state_count)
267 return -ENODEV;
269 /* Initialize optional data, used for the hierarchical topology. */
270 ret = sbi_dt_cpu_init_topology(drv, data, state_count, cpu);
271 if (ret < 0)
272 return ret;
274 /* Store states in the per-cpu struct. */
275 data->states = states;
277 return 0;
280 static void sbi_cpuidle_deinit_cpu(int cpu)
282 struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
284 dt_idle_detach_cpu(data->dev);
285 sbi_cpuidle_use_cpuhp = false;
288 static int sbi_cpuidle_init_cpu(struct device *dev, int cpu)
290 struct cpuidle_driver *drv;
291 unsigned int state_count = 0;
292 int ret = 0;
294 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
295 if (!drv)
296 return -ENOMEM;
298 drv->name = "sbi_cpuidle";
299 drv->owner = THIS_MODULE;
300 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
302 /* RISC-V architectural WFI to be represented as state index 0. */
303 drv->states[0].enter = sbi_cpuidle_enter_state;
304 drv->states[0].exit_latency = 1;
305 drv->states[0].target_residency = 1;
306 drv->states[0].power_usage = UINT_MAX;
307 strcpy(drv->states[0].name, "WFI");
308 strcpy(drv->states[0].desc, "RISC-V WFI");
311 * If no DT idle states are detected (ret == 0) let the driver
312 * initialization fail accordingly since there is no reason to
313 * initialize the idle driver if only wfi is supported, the
314 * default archictectural back-end already executes wfi
315 * on idle entry.
317 ret = dt_init_idle_driver(drv, sbi_cpuidle_state_match, 1);
318 if (ret <= 0) {
319 pr_debug("HART%ld: failed to parse DT idle states\n",
320 cpuid_to_hartid_map(cpu));
321 return ret ? : -ENODEV;
323 state_count = ret + 1; /* Include WFI state as well */
325 /* Initialize idle states from DT. */
326 ret = sbi_cpuidle_dt_init_states(dev, drv, cpu, state_count);
327 if (ret) {
328 pr_err("HART%ld: failed to init idle states\n",
329 cpuid_to_hartid_map(cpu));
330 return ret;
333 if (cpuidle_disabled())
334 return 0;
336 ret = cpuidle_register(drv, NULL);
337 if (ret)
338 goto deinit;
340 cpuidle_cooling_register(drv);
342 return 0;
343 deinit:
344 sbi_cpuidle_deinit_cpu(cpu);
345 return ret;
348 static void sbi_cpuidle_domain_sync_state(struct device *dev)
351 * All devices have now been attached/probed to the PM domain
352 * topology, hence it's fine to allow domain states to be picked.
354 sbi_cpuidle_pd_allow_domain_state = true;
357 #ifdef CONFIG_DT_IDLE_GENPD
359 static int sbi_cpuidle_pd_power_off(struct generic_pm_domain *pd)
361 struct genpd_power_state *state = &pd->states[pd->state_idx];
362 u32 *pd_state;
364 if (!state->data)
365 return 0;
367 if (!sbi_cpuidle_pd_allow_domain_state)
368 return -EBUSY;
370 /* OSI mode is enabled, set the corresponding domain state. */
371 pd_state = state->data;
372 sbi_set_domain_state(*pd_state);
374 return 0;
377 struct sbi_pd_provider {
378 struct list_head link;
379 struct device_node *node;
382 static LIST_HEAD(sbi_pd_providers);
384 static int sbi_pd_init(struct device_node *np)
386 struct generic_pm_domain *pd;
387 struct sbi_pd_provider *pd_provider;
388 struct dev_power_governor *pd_gov;
389 int ret = -ENOMEM;
391 pd = dt_idle_pd_alloc(np, sbi_dt_parse_state_node);
392 if (!pd)
393 goto out;
395 pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
396 if (!pd_provider)
397 goto free_pd;
399 pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
401 /* Allow power off when OSI is available. */
402 if (sbi_cpuidle_use_osi)
403 pd->power_off = sbi_cpuidle_pd_power_off;
404 else
405 pd->flags |= GENPD_FLAG_ALWAYS_ON;
407 /* Use governor for CPU PM domains if it has some states to manage. */
408 pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
410 ret = pm_genpd_init(pd, pd_gov, false);
411 if (ret)
412 goto free_pd_prov;
414 ret = of_genpd_add_provider_simple(np, pd);
415 if (ret)
416 goto remove_pd;
418 pd_provider->node = of_node_get(np);
419 list_add(&pd_provider->link, &sbi_pd_providers);
421 pr_debug("init PM domain %s\n", pd->name);
422 return 0;
424 remove_pd:
425 pm_genpd_remove(pd);
426 free_pd_prov:
427 kfree(pd_provider);
428 free_pd:
429 dt_idle_pd_free(pd);
430 out:
431 pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
432 return ret;
435 static void sbi_pd_remove(void)
437 struct sbi_pd_provider *pd_provider, *it;
438 struct generic_pm_domain *genpd;
440 list_for_each_entry_safe(pd_provider, it, &sbi_pd_providers, link) {
441 of_genpd_del_provider(pd_provider->node);
443 genpd = of_genpd_remove_last(pd_provider->node);
444 if (!IS_ERR(genpd))
445 kfree(genpd);
447 of_node_put(pd_provider->node);
448 list_del(&pd_provider->link);
449 kfree(pd_provider);
453 static int sbi_genpd_probe(struct device_node *np)
455 int ret = 0, pd_count = 0;
457 if (!np)
458 return -ENODEV;
461 * Parse child nodes for the "#power-domain-cells" property and
462 * initialize a genpd/genpd-of-provider pair when it's found.
464 for_each_child_of_node_scoped(np, node) {
465 if (!of_property_present(node, "#power-domain-cells"))
466 continue;
468 ret = sbi_pd_init(node);
469 if (ret)
470 goto remove_pd;
472 pd_count++;
475 /* Bail out if not using the hierarchical CPU topology. */
476 if (!pd_count)
477 goto no_pd;
479 /* Link genpd masters/subdomains to model the CPU topology. */
480 ret = dt_idle_pd_init_topology(np);
481 if (ret)
482 goto remove_pd;
484 return 0;
486 remove_pd:
487 sbi_pd_remove();
488 pr_err("failed to create CPU PM domains ret=%d\n", ret);
489 no_pd:
490 return ret;
493 #else
495 static inline int sbi_genpd_probe(struct device_node *np)
497 return 0;
500 #endif
502 static int sbi_cpuidle_probe(struct platform_device *pdev)
504 int cpu, ret;
505 struct cpuidle_driver *drv;
506 struct cpuidle_device *dev;
507 struct device_node *np, *pds_node;
509 /* Detect OSI support based on CPU DT nodes */
510 sbi_cpuidle_use_osi = true;
511 for_each_possible_cpu(cpu) {
512 np = of_cpu_device_node_get(cpu);
513 if (np &&
514 of_property_present(np, "power-domains") &&
515 of_property_present(np, "power-domain-names")) {
516 continue;
517 } else {
518 sbi_cpuidle_use_osi = false;
519 break;
523 /* Populate generic power domains from DT nodes */
524 pds_node = of_find_node_by_path("/cpus/power-domains");
525 if (pds_node) {
526 ret = sbi_genpd_probe(pds_node);
527 of_node_put(pds_node);
528 if (ret)
529 return ret;
532 /* Initialize CPU idle driver for each CPU */
533 for_each_possible_cpu(cpu) {
534 ret = sbi_cpuidle_init_cpu(&pdev->dev, cpu);
535 if (ret) {
536 pr_debug("HART%ld: idle driver init failed\n",
537 cpuid_to_hartid_map(cpu));
538 goto out_fail;
542 /* Setup CPU hotplut notifiers */
543 sbi_idle_init_cpuhp();
545 if (cpuidle_disabled())
546 pr_info("cpuidle is disabled\n");
547 else
548 pr_info("idle driver registered for all CPUs\n");
550 return 0;
552 out_fail:
553 while (--cpu >= 0) {
554 dev = per_cpu(cpuidle_devices, cpu);
555 drv = cpuidle_get_cpu_driver(dev);
556 cpuidle_unregister(drv);
557 sbi_cpuidle_deinit_cpu(cpu);
560 return ret;
563 static struct platform_driver sbi_cpuidle_driver = {
564 .probe = sbi_cpuidle_probe,
565 .driver = {
566 .name = "sbi-cpuidle",
567 .sync_state = sbi_cpuidle_domain_sync_state,
571 static int __init sbi_cpuidle_init(void)
573 int ret;
574 struct platform_device *pdev;
576 if (!riscv_sbi_hsm_is_supported())
577 return 0;
579 ret = platform_driver_register(&sbi_cpuidle_driver);
580 if (ret)
581 return ret;
583 pdev = platform_device_register_simple("sbi-cpuidle",
584 -1, NULL, 0);
585 if (IS_ERR(pdev)) {
586 platform_driver_unregister(&sbi_cpuidle_driver);
587 return PTR_ERR(pdev);
590 return 0;
592 arch_initcall(sbi_cpuidle_init);