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 
9 #define pr_fmt(fmt) "cpuidle-riscv-sbi: " fmt
10 
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>
28 
29 #include "dt_idle_states.h"
30 #include "dt_idle_genpd.h"
31 
32 struct sbi_cpuidle_data {
33 	u32 *states;
34 	struct device *dev;
35 };
36 
37 struct sbi_domain_state {
38 	bool available;
39 	u32 state;
40 };
41 
42 static DEFINE_PER_CPU_READ_MOSTLY(struct sbi_cpuidle_data, sbi_cpuidle_data);
43 static DEFINE_PER_CPU(struct sbi_domain_state, domain_state);
44 static bool sbi_cpuidle_use_osi;
45 static bool sbi_cpuidle_use_cpuhp;
46 static bool sbi_cpuidle_pd_allow_domain_state;
47 
sbi_set_domain_state(u32 state)48 static inline void sbi_set_domain_state(u32 state)
49 {
50 	struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
51 
52 	data->available = true;
53 	data->state = state;
54 }
55 
sbi_get_domain_state(void)56 static inline u32 sbi_get_domain_state(void)
57 {
58 	struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
59 
60 	return data->state;
61 }
62 
sbi_clear_domain_state(void)63 static inline void sbi_clear_domain_state(void)
64 {
65 	struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
66 
67 	data->available = false;
68 }
69 
sbi_is_domain_state_available(void)70 static inline bool sbi_is_domain_state_available(void)
71 {
72 	struct sbi_domain_state *data = this_cpu_ptr(&domain_state);
73 
74 	return data->available;
75 }
76 
sbi_cpuidle_enter_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)77 static __cpuidle int sbi_cpuidle_enter_state(struct cpuidle_device *dev,
78 					     struct cpuidle_driver *drv, int idx)
79 {
80 	u32 *states = __this_cpu_read(sbi_cpuidle_data.states);
81 	u32 state = states[idx];
82 
83 	if (state & SBI_HSM_SUSP_NON_RET_BIT)
84 		return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_sbi_hart_suspend, idx, state);
85 	else
86 		return CPU_PM_CPU_IDLE_ENTER_RETENTION_PARAM(riscv_sbi_hart_suspend,
87 							     idx, state);
88 }
89 
__sbi_enter_domain_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx,bool s2idle)90 static __cpuidle int __sbi_enter_domain_idle_state(struct cpuidle_device *dev,
91 						   struct cpuidle_driver *drv, int idx,
92 						   bool s2idle)
93 {
94 	struct sbi_cpuidle_data *data = this_cpu_ptr(&sbi_cpuidle_data);
95 	u32 *states = data->states;
96 	struct device *pd_dev = data->dev;
97 	u32 state;
98 	int ret;
99 
100 	ret = cpu_pm_enter();
101 	if (ret)
102 		return -1;
103 
104 	/* Do runtime PM to manage a hierarchical CPU toplogy. */
105 	if (s2idle)
106 		dev_pm_genpd_suspend(pd_dev);
107 	else
108 		pm_runtime_put_sync_suspend(pd_dev);
109 
110 	ct_cpuidle_enter();
111 
112 	if (sbi_is_domain_state_available())
113 		state = sbi_get_domain_state();
114 	else
115 		state = states[idx];
116 
117 	ret = riscv_sbi_hart_suspend(state) ? -1 : idx;
118 
119 	ct_cpuidle_exit();
120 
121 	if (s2idle)
122 		dev_pm_genpd_resume(pd_dev);
123 	else
124 		pm_runtime_get_sync(pd_dev);
125 
126 	cpu_pm_exit();
127 
128 	/* Clear the domain state to start fresh when back from idle. */
129 	sbi_clear_domain_state();
130 	return ret;
131 }
132 
sbi_enter_domain_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)133 static int sbi_enter_domain_idle_state(struct cpuidle_device *dev,
134 				       struct cpuidle_driver *drv, int idx)
135 {
136 	return __sbi_enter_domain_idle_state(dev, drv, idx, false);
137 }
138 
sbi_enter_s2idle_domain_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)139 static int sbi_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
140 					      struct cpuidle_driver *drv,
141 					      int idx)
142 {
143 	return __sbi_enter_domain_idle_state(dev, drv, idx, true);
144 }
145 
sbi_cpuidle_cpuhp_up(unsigned int cpu)146 static int sbi_cpuidle_cpuhp_up(unsigned int cpu)
147 {
148 	struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
149 
150 	if (pd_dev)
151 		pm_runtime_get_sync(pd_dev);
152 
153 	return 0;
154 }
155 
sbi_cpuidle_cpuhp_down(unsigned int cpu)156 static int sbi_cpuidle_cpuhp_down(unsigned int cpu)
157 {
158 	struct device *pd_dev = __this_cpu_read(sbi_cpuidle_data.dev);
159 
160 	if (pd_dev) {
161 		pm_runtime_put_sync(pd_dev);
162 		/* Clear domain state to start fresh at next online. */
163 		sbi_clear_domain_state();
164 	}
165 
166 	return 0;
167 }
168 
sbi_idle_init_cpuhp(void)169 static void sbi_idle_init_cpuhp(void)
170 {
171 	int err;
172 
173 	if (!sbi_cpuidle_use_cpuhp)
174 		return;
175 
176 	err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
177 					"cpuidle/sbi:online",
178 					sbi_cpuidle_cpuhp_up,
179 					sbi_cpuidle_cpuhp_down);
180 	if (err)
181 		pr_warn("Failed %d while setup cpuhp state\n", err);
182 }
183 
184 static const struct of_device_id sbi_cpuidle_state_match[] = {
185 	{ .compatible = "riscv,idle-state",
186 	  .data = sbi_cpuidle_enter_state },
187 	{ },
188 };
189 
sbi_dt_parse_state_node(struct device_node * np,u32 * state)190 static int sbi_dt_parse_state_node(struct device_node *np, u32 *state)
191 {
192 	int err = of_property_read_u32(np, "riscv,sbi-suspend-param", state);
193 
194 	if (err) {
195 		pr_warn("%pOF missing riscv,sbi-suspend-param property\n", np);
196 		return err;
197 	}
198 
199 	if (!riscv_sbi_suspend_state_is_valid(*state)) {
200 		pr_warn("Invalid SBI suspend state %#x\n", *state);
201 		return -EINVAL;
202 	}
203 
204 	return 0;
205 }
206 
sbi_dt_cpu_init_topology(struct cpuidle_driver * drv,struct sbi_cpuidle_data * data,unsigned int state_count,int cpu)207 static int sbi_dt_cpu_init_topology(struct cpuidle_driver *drv,
208 				     struct sbi_cpuidle_data *data,
209 				     unsigned int state_count, int cpu)
210 {
211 	/* Currently limit the hierarchical topology to be used in OSI mode. */
212 	if (!sbi_cpuidle_use_osi)
213 		return 0;
214 
215 	data->dev = dt_idle_attach_cpu(cpu, "sbi");
216 	if (IS_ERR_OR_NULL(data->dev))
217 		return PTR_ERR_OR_ZERO(data->dev);
218 
219 	/*
220 	 * Using the deepest state for the CPU to trigger a potential selection
221 	 * of a shared state for the domain, assumes the domain states are all
222 	 * deeper states.
223 	 */
224 	drv->states[state_count - 1].flags |= CPUIDLE_FLAG_RCU_IDLE;
225 	drv->states[state_count - 1].enter = sbi_enter_domain_idle_state;
226 	drv->states[state_count - 1].enter_s2idle =
227 					sbi_enter_s2idle_domain_idle_state;
228 	sbi_cpuidle_use_cpuhp = true;
229 
230 	return 0;
231 }
232 
sbi_cpuidle_dt_init_states(struct device * dev,struct cpuidle_driver * drv,unsigned int cpu,unsigned int state_count)233 static int sbi_cpuidle_dt_init_states(struct device *dev,
234 					struct cpuidle_driver *drv,
235 					unsigned int cpu,
236 					unsigned int state_count)
237 {
238 	struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
239 	struct device_node *state_node;
240 	u32 *states;
241 	int i, ret;
242 
243 	struct device_node *cpu_node __free(device_node) = of_cpu_device_node_get(cpu);
244 	if (!cpu_node)
245 		return -ENODEV;
246 
247 	states = devm_kcalloc(dev, state_count, sizeof(*states), GFP_KERNEL);
248 	if (!states)
249 		return -ENOMEM;
250 
251 	/* Parse SBI specific details from state DT nodes */
252 	for (i = 1; i < state_count; i++) {
253 		state_node = of_get_cpu_state_node(cpu_node, i - 1);
254 		if (!state_node)
255 			break;
256 
257 		ret = sbi_dt_parse_state_node(state_node, &states[i]);
258 		of_node_put(state_node);
259 
260 		if (ret)
261 			return ret;
262 
263 		pr_debug("sbi-state %#x index %d\n", states[i], i);
264 	}
265 	if (i != state_count)
266 		return -ENODEV;
267 
268 	/* Initialize optional data, used for the hierarchical topology. */
269 	ret = sbi_dt_cpu_init_topology(drv, data, state_count, cpu);
270 	if (ret < 0)
271 		return ret;
272 
273 	/* Store states in the per-cpu struct. */
274 	data->states = states;
275 
276 	return 0;
277 }
278 
sbi_cpuidle_deinit_cpu(int cpu)279 static void sbi_cpuidle_deinit_cpu(int cpu)
280 {
281 	struct sbi_cpuidle_data *data = per_cpu_ptr(&sbi_cpuidle_data, cpu);
282 
283 	dt_idle_detach_cpu(data->dev);
284 	sbi_cpuidle_use_cpuhp = false;
285 }
286 
sbi_cpuidle_init_cpu(struct device * dev,int cpu)287 static int sbi_cpuidle_init_cpu(struct device *dev, int cpu)
288 {
289 	struct cpuidle_driver *drv;
290 	unsigned int state_count = 0;
291 	int ret = 0;
292 
293 	drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
294 	if (!drv)
295 		return -ENOMEM;
296 
297 	drv->name = "sbi_cpuidle";
298 	drv->owner = THIS_MODULE;
299 	drv->cpumask = (struct cpumask *)cpumask_of(cpu);
300 
301 	/* RISC-V architectural WFI to be represented as state index 0. */
302 	drv->states[0].enter = sbi_cpuidle_enter_state;
303 	drv->states[0].exit_latency = 1;
304 	drv->states[0].target_residency = 1;
305 	drv->states[0].power_usage = UINT_MAX;
306 	strcpy(drv->states[0].name, "WFI");
307 	strcpy(drv->states[0].desc, "RISC-V WFI");
308 
309 	/*
310 	 * If no DT idle states are detected (ret == 0) let the driver
311 	 * initialization fail accordingly since there is no reason to
312 	 * initialize the idle driver if only wfi is supported, the
313 	 * default archictectural back-end already executes wfi
314 	 * on idle entry.
315 	 */
316 	ret = dt_init_idle_driver(drv, sbi_cpuidle_state_match, 1);
317 	if (ret <= 0) {
318 		pr_debug("HART%ld: failed to parse DT idle states\n",
319 			 cpuid_to_hartid_map(cpu));
320 		return ret ? : -ENODEV;
321 	}
322 	state_count = ret + 1; /* Include WFI state as well */
323 
324 	/* Initialize idle states from DT. */
325 	ret = sbi_cpuidle_dt_init_states(dev, drv, cpu, state_count);
326 	if (ret) {
327 		pr_err("HART%ld: failed to init idle states\n",
328 		       cpuid_to_hartid_map(cpu));
329 		return ret;
330 	}
331 
332 	ret = cpuidle_register(drv, NULL);
333 	if (ret)
334 		goto deinit;
335 
336 	cpuidle_cooling_register(drv);
337 
338 	return 0;
339 deinit:
340 	sbi_cpuidle_deinit_cpu(cpu);
341 	return ret;
342 }
343 
sbi_cpuidle_domain_sync_state(struct device * dev)344 static void sbi_cpuidle_domain_sync_state(struct device *dev)
345 {
346 	/*
347 	 * All devices have now been attached/probed to the PM domain
348 	 * topology, hence it's fine to allow domain states to be picked.
349 	 */
350 	sbi_cpuidle_pd_allow_domain_state = true;
351 }
352 
353 #ifdef CONFIG_DT_IDLE_GENPD
354 
sbi_cpuidle_pd_power_off(struct generic_pm_domain * pd)355 static int sbi_cpuidle_pd_power_off(struct generic_pm_domain *pd)
356 {
357 	struct genpd_power_state *state = &pd->states[pd->state_idx];
358 	u32 *pd_state;
359 
360 	if (!state->data)
361 		return 0;
362 
363 	if (!sbi_cpuidle_pd_allow_domain_state)
364 		return -EBUSY;
365 
366 	/* OSI mode is enabled, set the corresponding domain state. */
367 	pd_state = state->data;
368 	sbi_set_domain_state(*pd_state);
369 
370 	return 0;
371 }
372 
373 struct sbi_pd_provider {
374 	struct list_head link;
375 	struct device_node *node;
376 };
377 
378 static LIST_HEAD(sbi_pd_providers);
379 
sbi_pd_init(struct device_node * np)380 static int sbi_pd_init(struct device_node *np)
381 {
382 	struct generic_pm_domain *pd;
383 	struct sbi_pd_provider *pd_provider;
384 	struct dev_power_governor *pd_gov;
385 	int ret = -ENOMEM;
386 
387 	pd = dt_idle_pd_alloc(np, sbi_dt_parse_state_node);
388 	if (!pd)
389 		goto out;
390 
391 	pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
392 	if (!pd_provider)
393 		goto free_pd;
394 
395 	pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
396 
397 	/* Allow power off when OSI is available. */
398 	if (sbi_cpuidle_use_osi)
399 		pd->power_off = sbi_cpuidle_pd_power_off;
400 	else
401 		pd->flags |= GENPD_FLAG_ALWAYS_ON;
402 
403 	/* Use governor for CPU PM domains if it has some states to manage. */
404 	pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
405 
406 	ret = pm_genpd_init(pd, pd_gov, false);
407 	if (ret)
408 		goto free_pd_prov;
409 
410 	ret = of_genpd_add_provider_simple(np, pd);
411 	if (ret)
412 		goto remove_pd;
413 
414 	pd_provider->node = of_node_get(np);
415 	list_add(&pd_provider->link, &sbi_pd_providers);
416 
417 	pr_debug("init PM domain %s\n", pd->name);
418 	return 0;
419 
420 remove_pd:
421 	pm_genpd_remove(pd);
422 free_pd_prov:
423 	kfree(pd_provider);
424 free_pd:
425 	dt_idle_pd_free(pd);
426 out:
427 	pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
428 	return ret;
429 }
430 
sbi_pd_remove(void)431 static void sbi_pd_remove(void)
432 {
433 	struct sbi_pd_provider *pd_provider, *it;
434 	struct generic_pm_domain *genpd;
435 
436 	list_for_each_entry_safe(pd_provider, it, &sbi_pd_providers, link) {
437 		of_genpd_del_provider(pd_provider->node);
438 
439 		genpd = of_genpd_remove_last(pd_provider->node);
440 		if (!IS_ERR(genpd))
441 			kfree(genpd);
442 
443 		of_node_put(pd_provider->node);
444 		list_del(&pd_provider->link);
445 		kfree(pd_provider);
446 	}
447 }
448 
sbi_genpd_probe(struct device_node * np)449 static int sbi_genpd_probe(struct device_node *np)
450 {
451 	int ret = 0, pd_count = 0;
452 
453 	if (!np)
454 		return -ENODEV;
455 
456 	/*
457 	 * Parse child nodes for the "#power-domain-cells" property and
458 	 * initialize a genpd/genpd-of-provider pair when it's found.
459 	 */
460 	for_each_child_of_node_scoped(np, node) {
461 		if (!of_property_present(node, "#power-domain-cells"))
462 			continue;
463 
464 		ret = sbi_pd_init(node);
465 		if (ret)
466 			goto remove_pd;
467 
468 		pd_count++;
469 	}
470 
471 	/* Bail out if not using the hierarchical CPU topology. */
472 	if (!pd_count)
473 		goto no_pd;
474 
475 	/* Link genpd masters/subdomains to model the CPU topology. */
476 	ret = dt_idle_pd_init_topology(np);
477 	if (ret)
478 		goto remove_pd;
479 
480 	return 0;
481 
482 remove_pd:
483 	sbi_pd_remove();
484 	pr_err("failed to create CPU PM domains ret=%d\n", ret);
485 no_pd:
486 	return ret;
487 }
488 
489 #else
490 
sbi_genpd_probe(struct device_node * np)491 static inline int sbi_genpd_probe(struct device_node *np)
492 {
493 	return 0;
494 }
495 
496 #endif
497 
sbi_cpuidle_probe(struct platform_device * pdev)498 static int sbi_cpuidle_probe(struct platform_device *pdev)
499 {
500 	int cpu, ret;
501 	struct cpuidle_driver *drv;
502 	struct cpuidle_device *dev;
503 	struct device_node *np, *pds_node;
504 
505 	/* Detect OSI support based on CPU DT nodes */
506 	sbi_cpuidle_use_osi = true;
507 	for_each_possible_cpu(cpu) {
508 		np = of_cpu_device_node_get(cpu);
509 		if (np &&
510 		    of_property_present(np, "power-domains") &&
511 		    of_property_present(np, "power-domain-names")) {
512 			continue;
513 		} else {
514 			sbi_cpuidle_use_osi = false;
515 			break;
516 		}
517 	}
518 
519 	/* Populate generic power domains from DT nodes */
520 	pds_node = of_find_node_by_path("/cpus/power-domains");
521 	if (pds_node) {
522 		ret = sbi_genpd_probe(pds_node);
523 		of_node_put(pds_node);
524 		if (ret)
525 			return ret;
526 	}
527 
528 	/* Initialize CPU idle driver for each CPU */
529 	for_each_possible_cpu(cpu) {
530 		ret = sbi_cpuidle_init_cpu(&pdev->dev, cpu);
531 		if (ret) {
532 			pr_debug("HART%ld: idle driver init failed\n",
533 				 cpuid_to_hartid_map(cpu));
534 			goto out_fail;
535 		}
536 	}
537 
538 	/* Setup CPU hotplut notifiers */
539 	sbi_idle_init_cpuhp();
540 
541 	pr_info("idle driver registered for all CPUs\n");
542 
543 	return 0;
544 
545 out_fail:
546 	while (--cpu >= 0) {
547 		dev = per_cpu(cpuidle_devices, cpu);
548 		drv = cpuidle_get_cpu_driver(dev);
549 		cpuidle_unregister(drv);
550 		sbi_cpuidle_deinit_cpu(cpu);
551 	}
552 
553 	return ret;
554 }
555 
556 static struct platform_driver sbi_cpuidle_driver = {
557 	.probe = sbi_cpuidle_probe,
558 	.driver = {
559 		.name = "sbi-cpuidle",
560 		.sync_state = sbi_cpuidle_domain_sync_state,
561 	},
562 };
563 
sbi_cpuidle_init(void)564 static int __init sbi_cpuidle_init(void)
565 {
566 	int ret;
567 	struct platform_device *pdev;
568 
569 	if (!riscv_sbi_hsm_is_supported())
570 		return 0;
571 
572 	ret = platform_driver_register(&sbi_cpuidle_driver);
573 	if (ret)
574 		return ret;
575 
576 	pdev = platform_device_register_simple("sbi-cpuidle",
577 						-1, NULL, 0);
578 	if (IS_ERR(pdev)) {
579 		platform_driver_unregister(&sbi_cpuidle_driver);
580 		return PTR_ERR(pdev);
581 	}
582 
583 	return 0;
584 }
585 device_initcall(sbi_cpuidle_init);
586