summaryrefslogtreecommitdiffstats
path: root/drivers/cpuidle/dt_idle_genpd.c
blob: 1af63c189039e9d40d3fd0863b37585feca271a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: GPL-2.0-only
/*
 * PM domains for CPUs via genpd.
 *
 * Copyright (C) 2019 Linaro Ltd.
 * Author: Ulf Hansson <ulf.hansson@linaro.org>
 *
 * Copyright (c) 2021 Western Digital Corporation or its affiliates.
 * Copyright (c) 2022 Ventana Micro Systems Inc.
 */

#define pr_fmt(fmt) "dt-idle-genpd: " fmt

#include <linux/cpu.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/pm_domain.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/string.h>

#include "dt_idle_genpd.h"

static int pd_parse_state_nodes(
			int (*parse_state)(struct device_node *, u32 *),
			struct genpd_power_state *states, int state_count)
{
	int i, ret;
	u32 state, *state_buf;

	for (i = 0; i < state_count; i++) {
		ret = parse_state(to_of_node(states[i].fwnode), &state);
		if (ret)
			goto free_state;

		state_buf = kmalloc(sizeof(u32), GFP_KERNEL);
		if (!state_buf) {
			ret = -ENOMEM;
			goto free_state;
		}
		*state_buf = state;
		states[i].data = state_buf;
	}

	return 0;

free_state:
	i--;
	for (; i >= 0; i--)
		kfree(states[i].data);
	return ret;
}

static int pd_parse_states(struct device_node *np,
			   int (*parse_state)(struct device_node *, u32 *),
			   struct genpd_power_state **states,
			   int *state_count)
{
	int ret;

	/* Parse the domain idle states. */
	ret = of_genpd_parse_idle_states(np, states, state_count);
	if (ret)
		return ret;

	/* Fill out the dt specifics for each found state. */
	ret = pd_parse_state_nodes(parse_state, *states, *state_count);
	if (ret)
		kfree(*states);

	return ret;
}

static void pd_free_states(struct genpd_power_state *states,
			    unsigned int state_count)
{
	int i;

	for (i = 0; i < state_count; i++)
		kfree(states[i].data);
	kfree(states);
}

void dt_idle_pd_free(struct generic_pm_domain *pd)
{
	pd_free_states(pd->states, pd->state_count);
	kfree(pd->name);
	kfree(pd);
}

struct generic_pm_domain *dt_idle_pd_alloc(struct device_node *np,
			int (*parse_state)(struct device_node *, u32 *))
{
	struct generic_pm_domain *pd;
	struct genpd_power_state *states = NULL;
	int ret, state_count = 0;

	pd = kzalloc(sizeof(*pd), GFP_KERNEL);
	if (!pd)
		goto out;

	pd->name = kasprintf(GFP_KERNEL, "%pOF", np);
	if (!pd->name)
		goto free_pd;

	/*
	 * Parse the domain idle states and let genpd manage the state selection
	 * for those being compatible with "domain-idle-state".
	 */
	ret = pd_parse_states(np, parse_state, &states, &state_count);
	if (ret)
		goto free_name;

	pd->free_states = pd_free_states;
	pd->name = kbasename(pd->name);
	pd->states = states;
	pd->state_count = state_count;

	pr_debug("alloc PM domain %s\n", pd->name);
	return pd;

free_name:
	kfree(pd->name);
free_pd:
	kfree(pd);
out:
	pr_err("failed to alloc PM domain %pOF\n", np);
	return NULL;
}

int dt_idle_pd_init_topology(struct device_node *np)
{
	struct device_node *node;
	struct of_phandle_args child, parent;
	int ret;

	for_each_child_of_node(np, node) {
		if (of_parse_phandle_with_args(node, "power-domains",
					"#power-domain-cells", 0, &parent))
			continue;

		child.np = node;
		child.args_count = 0;
		ret = of_genpd_add_subdomain(&parent, &child);
		of_node_put(parent.np);
		if (ret) {
			of_node_put(node);
			return ret;
		}
	}

	return 0;
}

int dt_idle_pd_remove_topology(struct device_node *np)
{
	struct device_node *node;
	struct of_phandle_args child, parent;
	int ret;

	for_each_child_of_node(np, node) {
		if (of_parse_phandle_with_args(node, "power-domains",
					"#power-domain-cells", 0, &parent))
			continue;

		child.np = node;
		child.args_count = 0;
		ret = of_genpd_remove_subdomain(&parent, &child);
		of_node_put(parent.np);
		if (ret) {
			of_node_put(node);
			return ret;
		}
	}

	return 0;
}

struct device *dt_idle_attach_cpu(int cpu, const char *name)
{
	struct device *dev;

	dev = dev_pm_domain_attach_by_name(get_cpu_device(cpu), name);
	if (IS_ERR_OR_NULL(dev))
		return dev;

	pm_runtime_irq_safe(dev);
	if (cpu_online(cpu))
		pm_runtime_get_sync(dev);

	dev_pm_syscore_device(dev, true);

	return dev;
}

void dt_idle_detach_cpu(struct device *dev)
{
	if (IS_ERR_OR_NULL(dev))
		return;

	dev_pm_domain_detach(dev, false);
}