1 // SPDX-License-Identifier: GPL-2.0-only
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.
9 #define pr_fmt(fmt) "cpuidle-riscv-sbi: " fmt
11 #include <linux/cpuhotplug.h>
12 #include <linux/cpuidle.h>
13 #include <linux/cpumask.h>
14 #include <linux/cpu_pm.h>
15 #include <linux/cpu_cooling.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_domain.h>
22 #include <linux/pm_runtime.h>
23 #include <asm/cpuidle.h>
26 #include <asm/suspend.h>
28 #include "dt_idle_states.h"
29 #include "dt_idle_genpd.h"
31 struct sbi_cpuidle_data {
36 struct sbi_domain_state {
41 static DEFINE_PER_CPU_READ_MOSTLY(struct sbi_cpuidle_data, sbi_cpuidle_data);
42 static DEFINE_PER_CPU(struct sbi_domain_state, domain_state);
43 static bool sbi_cpuidle_use_osi;
44 static bool sbi_cpuidle_use_cpuhp;
45 static bool sbi_cpuidle_pd_allow_domain_state;
47 static inline void sbi_set_domain_state(u32 state)
49 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
51 data->available = true;
55 static inline u32 sbi_get_domain_state(void)
57 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
62 static inline void sbi_clear_domain_state(void)
64 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
66 data->available = false;
69 static inline bool sbi_is_domain_state_available(void)
71 struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
73 return data->available;
76 static __cpuidle int sbi_cpuidle_enter_state(struct cpuidle_device *dev,
77 struct cpuidle_driver *drv, int idx)
79 u32 *states = __this_cpu_read(sbi_cpuidle_data.states);
80 u32 state = states[idx];
82 if (state & SBI_HSM_SUSP_NON_RET_BIT)
83 return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_sbi_hart_suspend, idx, state);
85 return CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(riscv_sbi_hart_suspend,
89 static __cpuidle int __sbi_enter_domain_idle_state(struct cpuidle_device *dev,
90 struct cpuidle_driver *drv, int idx,
93 struct sbi_cpuidle_data *data = this_cpu_ptr(&sbi_cpuidle_data);
94 u32 *states = data->states;
95 struct device *pd_dev = data->dev;
103 /* Do runtime PM to manage a hierarchical CPU toplogy. */
105 dev_pm_genpd_suspend(pd_dev);
107 pm_runtime_put_sync_suspend(pd_dev);
111 if (sbi_is_domain_state_available())
112 state = sbi_get_domain_state();
116 ret = riscv_sbi_hart_suspend(state) ? -1 : idx;
121 dev_pm_genpd_resume(pd_dev);
123 pm_runtime_get_sync(pd_dev);
127 /* Clear the domain state to start fresh when back from idle. */
128 sbi_clear_domain_state();
132 static int sbi_enter_domain_idle_state(struct cpuidle_device *dev,
133 struct cpuidle_driver *drv, int idx)
135 return __sbi_enter_domain_idle_state(dev, drv, idx, false);
138 static int sbi_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
139 struct cpuidle_driver *drv,
142 return __sbi_enter_domain_idle_state(dev, drv, idx, true);
145 static int sbi_cpuidle_cpuhp_up(unsigned int cpu)
147 struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
150 pm_runtime_get_sync(pd_dev);
155 static int sbi_cpuidle_cpuhp_down(unsigned int cpu)
157 struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
160 pm_runtime_put_sync(pd_dev);
161 /* Clear domain state to start fresh at next online. */
162 sbi_clear_domain_state();
168 static void sbi_idle_init_cpuhp(void)
172 if (!sbi_cpuidle_use_cpuhp)
175 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
176 "cpuidle/sbi:online",
177 sbi_cpuidle_cpuhp_up,
178 sbi_cpuidle_cpuhp_down);
180 pr_warn("Failed %d while setup cpuhp state\n", err);
183 static const struct of_device_id sbi_cpuidle_state_match[] = {
184 { .compatible = "riscv,idle-state",
185 .data = sbi_cpuidle_enter_state },
189 static int sbi_dt_parse_state_node(struct device_node *np, u32 *state)
191 int err = of_property_read_u32(np, "riscv,sbi-suspend-param", state);
194 pr_warn("%pOF missing riscv,sbi-suspend-param property\n", np);
198 if (!riscv_sbi_suspend_state_is_valid(*state)) {
199 pr_warn("Invalid SBI suspend state %#x\n", *state);
206 static int sbi_dt_cpu_init_topology(struct cpuidle_driver *drv,
207 struct sbi_cpuidle_data *data,
208 unsigned int state_count, int cpu)
210 /* Currently limit the hierarchical topology to be used in OSI mode. */
211 if (!sbi_cpuidle_use_osi)
214 data->dev = dt_idle_attach_cpu(cpu, "sbi");
215 if (IS_ERR_OR_NULL(data->dev))
216 return PTR_ERR_OR_ZERO(data->dev);
219 * Using the deepest state for the CPU to trigger a potential selection
220 * of a shared state for the domain, assumes the domain states are all
223 drv->states[state_count - 1].flags |= CPUIDLE_FLAG_RCU_IDLE;
224 drv->states[state_count - 1].enter = sbi_enter_domain_idle_state;
225 drv->states[state_count - 1].enter_s2idle =
226 sbi_enter_s2idle_domain_idle_state;
227 sbi_cpuidle_use_cpuhp = true;
232 static int sbi_cpuidle_dt_init_states(struct device *dev,
233 struct cpuidle_driver *drv,
235 unsigned int state_count)
237 struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
238 struct device_node *state_node;
239 struct device_node *cpu_node;
243 cpu_node = of_cpu_device_node_get(cpu);
247 states = devm_kcalloc(dev, state_count, sizeof(*states), GFP_KERNEL);
253 /* Parse SBI specific details from state DT nodes */
254 for (i = 1; i < state_count; i++) {
255 state_node = of_get_cpu_state_node(cpu_node, i - 1);
259 ret = sbi_dt_parse_state_node(state_node, &states[i]);
260 of_node_put(state_node);
265 pr_debug("sbi-state %#x index %d\n", states[i], i);
267 if (i != state_count) {
272 /* Initialize optional data, used for the hierarchical topology. */
273 ret = sbi_dt_cpu_init_topology(drv, data, state_count, cpu);
277 /* Store states in the per-cpu struct. */
278 data->states = states;
281 of_node_put(cpu_node);
286 static void sbi_cpuidle_deinit_cpu(int cpu)
288 struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
290 dt_idle_detach_cpu(data->dev);
291 sbi_cpuidle_use_cpuhp = false;
294 static int sbi_cpuidle_init_cpu(struct device *dev, int cpu)
296 struct cpuidle_driver *drv;
297 unsigned int state_count = 0;
300 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
304 drv->name = "sbi_cpuidle";
305 drv->owner = THIS_MODULE;
306 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
308 /* RISC-V architectural WFI to be represented as state index 0. */
309 drv->states[0].enter = sbi_cpuidle_enter_state;
310 drv->states[0].exit_latency = 1;
311 drv->states[0].target_residency = 1;
312 drv->states[0].power_usage = UINT_MAX;
313 strcpy(drv->states[0].name, "WFI");
314 strcpy(drv->states[0].desc, "RISC-V WFI");
317 * If no DT idle states are detected (ret == 0) let the driver
318 * initialization fail accordingly since there is no reason to
319 * initialize the idle driver if only wfi is supported, the
320 * default archictectural back-end already executes wfi
323 ret = dt_init_idle_driver(drv, sbi_cpuidle_state_match, 1);
325 pr_debug("HART%ld: failed to parse DT idle states\n",
326 cpuid_to_hartid_map(cpu));
327 return ret ? : -ENODEV;
329 state_count = ret + 1; /* Include WFI state as well */
331 /* Initialize idle states from DT. */
332 ret = sbi_cpuidle_dt_init_states(dev, drv, cpu, state_count);
334 pr_err("HART%ld: failed to init idle states\n",
335 cpuid_to_hartid_map(cpu));
339 ret = cpuidle_register(drv, NULL);
343 cpuidle_cooling_register(drv);
347 sbi_cpuidle_deinit_cpu(cpu);
351 static void sbi_cpuidle_domain_sync_state(struct device *dev)
354 * All devices have now been attached/probed to the PM domain
355 * topology, hence it's fine to allow domain states to be picked.
357 sbi_cpuidle_pd_allow_domain_state = true;
360 #ifdef CONFIG_DT_IDLE_GENPD
362 static int sbi_cpuidle_pd_power_off(struct generic_pm_domain *pd)
364 struct genpd_power_state *state = &pd->states[pd->state_idx];
370 if (!sbi_cpuidle_pd_allow_domain_state)
373 /* OSI mode is enabled, set the corresponding domain state. */
374 pd_state = state->data;
375 sbi_set_domain_state(*pd_state);
380 struct sbi_pd_provider {
381 struct list_head link;
382 struct device_node *node;
385 static LIST_HEAD(sbi_pd_providers);
387 static int sbi_pd_init(struct device_node *np)
389 struct generic_pm_domain *pd;
390 struct sbi_pd_provider *pd_provider;
391 struct dev_power_governor *pd_gov;
394 pd = dt_idle_pd_alloc(np, sbi_dt_parse_state_node);
398 pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
402 pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
404 /* Allow power off when OSI is available. */
405 if (sbi_cpuidle_use_osi)
406 pd->power_off = sbi_cpuidle_pd_power_off;
408 pd->flags |= GENPD_FLAG_ALWAYS_ON;
410 /* Use governor for CPU PM domains if it has some states to manage. */
411 pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
413 ret = pm_genpd_init(pd, pd_gov, false);
417 ret = of_genpd_add_provider_simple(np, pd);
421 pd_provider->node = of_node_get(np);
422 list_add(&pd_provider->link, &sbi_pd_providers);
424 pr_debug("init PM domain %s\n", pd->name);
434 pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
438 static void sbi_pd_remove(void)
440 struct sbi_pd_provider *pd_provider, *it;
441 struct generic_pm_domain *genpd;
443 list_for_each_entry_safe(pd_provider, it, &sbi_pd_providers, link) {
444 of_genpd_del_provider(pd_provider->node);
446 genpd = of_genpd_remove_last(pd_provider->node);
450 of_node_put(pd_provider->node);
451 list_del(&pd_provider->link);
456 static int sbi_genpd_probe(struct device_node *np)
458 struct device_node *node;
459 int ret = 0, pd_count = 0;
465 * Parse child nodes for the "#power-domain-cells" property and
466 * initialize a genpd/genpd-of-provider pair when it's found.
468 for_each_child_of_node(np, node) {
469 if (!of_property_present(node, "#power-domain-cells"))
472 ret = sbi_pd_init(node);
479 /* Bail out if not using the hierarchical CPU topology. */
483 /* Link genpd masters/subdomains to model the CPU topology. */
484 ret = dt_idle_pd_init_topology(np);
494 pr_err("failed to create CPU PM domains ret=%d\n", ret);
501 static inline int sbi_genpd_probe(struct device_node *np)
508 static int sbi_cpuidle_probe(struct platform_device *pdev)
511 struct cpuidle_driver *drv;
512 struct cpuidle_device *dev;
513 struct device_node *np, *pds_node;
515 /* Detect OSI support based on CPU DT nodes */
516 sbi_cpuidle_use_osi = true;
517 for_each_possible_cpu(cpu) {
518 np = of_cpu_device_node_get(cpu);
520 of_property_present(np, "power-domains") &&
521 of_property_present(np, "power-domain-names")) {
524 sbi_cpuidle_use_osi = false;
529 /* Populate generic power domains from DT nodes */
530 pds_node = of_find_node_by_path("/cpus/power-domains");
532 ret = sbi_genpd_probe(pds_node);
533 of_node_put(pds_node);
538 /* Initialize CPU idle driver for each CPU */
539 for_each_possible_cpu(cpu) {
540 ret = sbi_cpuidle_init_cpu(&pdev->dev, cpu);
542 pr_debug("HART%ld: idle driver init failed\n",
543 cpuid_to_hartid_map(cpu));
548 /* Setup CPU hotplut notifiers */
549 sbi_idle_init_cpuhp();
551 pr_info("idle driver registered for all CPUs\n");
557 dev = per_cpu(cpuidle_devices, cpu);
558 drv = cpuidle_get_cpu_driver(dev);
559 cpuidle_unregister(drv);
560 sbi_cpuidle_deinit_cpu(cpu);
566 static struct platform_driver sbi_cpuidle_driver = {
567 .probe = sbi_cpuidle_probe,
569 .name = "sbi-cpuidle",
570 .sync_state = sbi_cpuidle_domain_sync_state,
574 static int __init sbi_cpuidle_init(void)
577 struct platform_device *pdev;
579 if (!riscv_sbi_hsm_is_supported())
582 ret = platform_driver_register(&sbi_cpuidle_driver);
586 pdev = platform_device_register_simple("sbi-cpuidle",
589 platform_driver_unregister(&sbi_cpuidle_driver);
590 return PTR_ERR(pdev);
595 device_initcall(sbi_cpuidle_init);