Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/cpuidle/cpuidle-riscv-sbi.c
26278 views
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 "cpuidle.h"
30
#include "dt_idle_states.h"
31
#include "dt_idle_genpd.h"
32
33
struct sbi_cpuidle_data {
34
u32 *states;
35
struct device *dev;
36
};
37
38
struct sbi_domain_state {
39
bool available;
40
u32 state;
41
};
42
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
if (cpuidle_disabled())
333
return 0;
334
335
ret = cpuidle_register(drv, NULL);
336
if (ret)
337
goto deinit;
338
339
cpuidle_cooling_register(drv);
340
341
return 0;
342
deinit:
343
sbi_cpuidle_deinit_cpu(cpu);
344
return ret;
345
}
346
347
#ifdef CONFIG_DT_IDLE_GENPD
348
349
static int sbi_cpuidle_pd_power_off(struct generic_pm_domain *pd)
350
{
351
struct genpd_power_state *state = &pd->states[pd->state_idx];
352
u32 *pd_state;
353
354
if (!state->data)
355
return 0;
356
357
/* OSI mode is enabled, set the corresponding domain state. */
358
pd_state = state->data;
359
sbi_set_domain_state(*pd_state);
360
361
return 0;
362
}
363
364
struct sbi_pd_provider {
365
struct list_head link;
366
struct device_node *node;
367
};
368
369
static LIST_HEAD(sbi_pd_providers);
370
371
static int sbi_pd_init(struct device_node *np)
372
{
373
struct generic_pm_domain *pd;
374
struct sbi_pd_provider *pd_provider;
375
struct dev_power_governor *pd_gov;
376
int ret = -ENOMEM;
377
378
pd = dt_idle_pd_alloc(np, sbi_dt_parse_state_node);
379
if (!pd)
380
goto out;
381
382
pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
383
if (!pd_provider)
384
goto free_pd;
385
386
pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
387
388
/* Allow power off when OSI is available. */
389
if (sbi_cpuidle_use_osi)
390
pd->power_off = sbi_cpuidle_pd_power_off;
391
else
392
pd->flags |= GENPD_FLAG_ALWAYS_ON;
393
394
/* Use governor for CPU PM domains if it has some states to manage. */
395
pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
396
397
ret = pm_genpd_init(pd, pd_gov, false);
398
if (ret)
399
goto free_pd_prov;
400
401
ret = of_genpd_add_provider_simple(np, pd);
402
if (ret)
403
goto remove_pd;
404
405
pd_provider->node = of_node_get(np);
406
list_add(&pd_provider->link, &sbi_pd_providers);
407
408
pr_debug("init PM domain %s\n", pd->name);
409
return 0;
410
411
remove_pd:
412
pm_genpd_remove(pd);
413
free_pd_prov:
414
kfree(pd_provider);
415
free_pd:
416
dt_idle_pd_free(pd);
417
out:
418
pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
419
return ret;
420
}
421
422
static void sbi_pd_remove(void)
423
{
424
struct sbi_pd_provider *pd_provider, *it;
425
struct generic_pm_domain *genpd;
426
427
list_for_each_entry_safe(pd_provider, it, &sbi_pd_providers, link) {
428
of_genpd_del_provider(pd_provider->node);
429
430
genpd = of_genpd_remove_last(pd_provider->node);
431
if (!IS_ERR(genpd))
432
kfree(genpd);
433
434
of_node_put(pd_provider->node);
435
list_del(&pd_provider->link);
436
kfree(pd_provider);
437
}
438
}
439
440
static int sbi_genpd_probe(struct device_node *np)
441
{
442
int ret = 0, pd_count = 0;
443
444
if (!np)
445
return -ENODEV;
446
447
/*
448
* Parse child nodes for the "#power-domain-cells" property and
449
* initialize a genpd/genpd-of-provider pair when it's found.
450
*/
451
for_each_child_of_node_scoped(np, node) {
452
if (!of_property_present(node, "#power-domain-cells"))
453
continue;
454
455
ret = sbi_pd_init(node);
456
if (ret)
457
goto remove_pd;
458
459
pd_count++;
460
}
461
462
/* Bail out if not using the hierarchical CPU topology. */
463
if (!pd_count)
464
goto no_pd;
465
466
/* Link genpd masters/subdomains to model the CPU topology. */
467
ret = dt_idle_pd_init_topology(np);
468
if (ret)
469
goto remove_pd;
470
471
return 0;
472
473
remove_pd:
474
sbi_pd_remove();
475
pr_err("failed to create CPU PM domains ret=%d\n", ret);
476
no_pd:
477
return ret;
478
}
479
480
#else
481
482
static inline int sbi_genpd_probe(struct device_node *np)
483
{
484
return 0;
485
}
486
487
#endif
488
489
static int sbi_cpuidle_probe(struct platform_device *pdev)
490
{
491
int cpu, ret;
492
struct cpuidle_driver *drv;
493
struct cpuidle_device *dev;
494
struct device_node *pds_node;
495
496
/* Detect OSI support based on CPU DT nodes */
497
sbi_cpuidle_use_osi = true;
498
for_each_possible_cpu(cpu) {
499
struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
500
if (np &&
501
of_property_present(np, "power-domains") &&
502
of_property_present(np, "power-domain-names")) {
503
continue;
504
} else {
505
sbi_cpuidle_use_osi = false;
506
break;
507
}
508
}
509
510
/* Populate generic power domains from DT nodes */
511
pds_node = of_find_node_by_path("/cpus/power-domains");
512
if (pds_node) {
513
ret = sbi_genpd_probe(pds_node);
514
of_node_put(pds_node);
515
if (ret)
516
return ret;
517
}
518
519
/* Initialize CPU idle driver for each present CPU */
520
for_each_present_cpu(cpu) {
521
ret = sbi_cpuidle_init_cpu(&pdev->dev, cpu);
522
if (ret) {
523
pr_debug("HART%ld: idle driver init failed\n",
524
cpuid_to_hartid_map(cpu));
525
goto out_fail;
526
}
527
}
528
529
/* Setup CPU hotplut notifiers */
530
sbi_idle_init_cpuhp();
531
532
if (cpuidle_disabled())
533
pr_info("cpuidle is disabled\n");
534
else
535
pr_info("idle driver registered for all CPUs\n");
536
537
return 0;
538
539
out_fail:
540
while (--cpu >= 0) {
541
dev = per_cpu(cpuidle_devices, cpu);
542
drv = cpuidle_get_cpu_driver(dev);
543
cpuidle_unregister(drv);
544
sbi_cpuidle_deinit_cpu(cpu);
545
}
546
547
return ret;
548
}
549
550
static struct platform_driver sbi_cpuidle_driver = {
551
.probe = sbi_cpuidle_probe,
552
.driver = {
553
.name = "sbi-cpuidle",
554
},
555
};
556
557
static int __init sbi_cpuidle_init(void)
558
{
559
int ret;
560
struct platform_device *pdev;
561
562
if (!riscv_sbi_hsm_is_supported())
563
return 0;
564
565
ret = platform_driver_register(&sbi_cpuidle_driver);
566
if (ret)
567
return ret;
568
569
pdev = platform_device_register_simple("sbi-cpuidle",
570
-1, NULL, 0);
571
if (IS_ERR(pdev)) {
572
platform_driver_unregister(&sbi_cpuidle_driver);
573
return PTR_ERR(pdev);
574
}
575
576
return 0;
577
}
578
arch_initcall(sbi_cpuidle_init);
579
580