summaryrefstree
path: root/kernel/drivers/interconnect/qcom/epss-l3.c
blob: 2fa494db60eb8cd5f9e0469a4d6c87d646cb232b (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
 *
 */

#include <dt-bindings/interconnect/qcom,epss-l3.h>
#include <dt-bindings/interconnect/qcom,lahaina.h>
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/interconnect-provider.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>

#include <linux/sec_debug.h>
#include <linux/sec_smem.h>

#define LUT_MAX_ENTRIES			40U
#define LUT_SRC				GENMASK(30, 30)
#define LUT_L_VAL			GENMASK(7, 0)
#define LUT_ROW_SIZE			4
#define CLK_HW_DIV			2

/* Register offsets */
#define REG_L3_VOTE			0x90
#define REG_FREQ_LUT			0x100
#define REG_PERF_STATE			0x320
#define EPSS_DOMAIN_OFFSET		0x1000
#define EPSS_CORE_OFFSET		0x4
#define EPSS_L3_VOTE_REG(base, domain, cpu)\
			((base + REG_L3_VOTE) +\
			(domain * EPSS_DOMAIN_OFFSET) +\
			(cpu * EPSS_CORE_OFFSET))

#define EPSS_L3_MAX_LINKS		9

#define to_qcom_provider(_provider) \
	container_of(_provider, struct qcom_epss_l3_icc_provider, provider)

enum {
	LAHAINA_MASTER_EPSS_L3_APPS = SLAVE_TCU + 1,
	LAHAINA_SLAVE_EPSS_L3_CPU0,
	LAHAINA_SLAVE_EPSS_L3_CPU1,
	LAHAINA_SLAVE_EPSS_L3_CPU2,
	LAHAINA_SLAVE_EPSS_L3_CPU3,
	LAHAINA_SLAVE_EPSS_L3_CPU4,
	LAHAINA_SLAVE_EPSS_L3_CPU5,
	LAHAINA_SLAVE_EPSS_L3_CPU6,
	LAHAINA_SLAVE_EPSS_L3_CPU7,
	LAHAINA_SLAVE_EPSS_L3_SHARED,
};

struct qcom_epss_l3_icc_provider {
	void __iomem *base;
	unsigned int max_state;
	unsigned long lut_freqs[LUT_MAX_ENTRIES];
	struct icc_provider provider;
};

/**
 * struct qcom_icc_node - QTI specific interconnect nodes
 * @name: the node name used in debugfs
 * @links: an array of nodes where we can go next while traversing
 * @id: a unique node identifier
 * @num_links: the total number of @links
 * @buswidth: width of the interconnect between a node and the bus
 * @domain: clock domain of the cpu node
 * @cpu: cpu instance within its clock domain
 */
struct qcom_icc_node {
	const char *name;
	u16 links[EPSS_L3_MAX_LINKS];
	u16 id;
	u16 num_links;
	u16 buswidth;
	u16 domain;
	u16 cpu;
};

struct qcom_icc_desc {
	struct qcom_icc_node **nodes;
	size_t num_nodes;
};

#define DEFINE_QNODE(_name, _id, _buswidth, _domain, _cpu, ...)		\
		static struct qcom_icc_node _name = {			\
		.name = #_name,						\
		.id = _id,						\
		.buswidth = _buswidth,					\
		.domain = _domain,					\
		.cpu = _cpu,						\
		.num_links = ARRAY_SIZE(((int[]){ __VA_ARGS__ })),	\
		.links = { __VA_ARGS__ },				\
	}

DEFINE_QNODE(mas_epss_l3_apps, LAHAINA_MASTER_EPSS_L3_APPS, 1, 0, 0,
		LAHAINA_SLAVE_EPSS_L3_CPU0, LAHAINA_SLAVE_EPSS_L3_CPU1,
		LAHAINA_SLAVE_EPSS_L3_CPU2, LAHAINA_SLAVE_EPSS_L3_CPU3,
		LAHAINA_SLAVE_EPSS_L3_CPU4, LAHAINA_SLAVE_EPSS_L3_CPU5,
		LAHAINA_SLAVE_EPSS_L3_CPU6, LAHAINA_SLAVE_EPSS_L3_CPU7,
		LAHAINA_SLAVE_EPSS_L3_SHARED);

DEFINE_QNODE(slv_epss_l3_cpu0, LAHAINA_SLAVE_EPSS_L3_CPU0, 1, 1, 0);
DEFINE_QNODE(slv_epss_l3_cpu1, LAHAINA_SLAVE_EPSS_L3_CPU1, 1, 1, 1);
DEFINE_QNODE(slv_epss_l3_cpu2, LAHAINA_SLAVE_EPSS_L3_CPU2, 1, 1, 2);
DEFINE_QNODE(slv_epss_l3_cpu3, LAHAINA_SLAVE_EPSS_L3_CPU3, 1, 1, 3);
DEFINE_QNODE(slv_epss_l3_cpu4, LAHAINA_SLAVE_EPSS_L3_CPU4, 1, 2, 0);
DEFINE_QNODE(slv_epss_l3_cpu5, LAHAINA_SLAVE_EPSS_L3_CPU5, 1, 2, 1);
DEFINE_QNODE(slv_epss_l3_cpu6, LAHAINA_SLAVE_EPSS_L3_CPU6, 1, 2, 2);
DEFINE_QNODE(slv_epss_l3_cpu7, LAHAINA_SLAVE_EPSS_L3_CPU7, 1, 3, 0);
DEFINE_QNODE(slv_epss_l3_shared, LAHAINA_SLAVE_EPSS_L3_SHARED, 1, 0, 0);

static struct qcom_icc_node *lahaina_epss_l3_nodes[] = {
	[MASTER_EPSS_L3_APPS] = &mas_epss_l3_apps,
	[SLAVE_EPSS_L3_CPU0] = &slv_epss_l3_cpu0,
	[SLAVE_EPSS_L3_CPU1] = &slv_epss_l3_cpu1,
	[SLAVE_EPSS_L3_CPU2] = &slv_epss_l3_cpu2,
	[SLAVE_EPSS_L3_CPU3] = &slv_epss_l3_cpu3,
	[SLAVE_EPSS_L3_CPU4] = &slv_epss_l3_cpu4,
	[SLAVE_EPSS_L3_CPU5] = &slv_epss_l3_cpu5,
	[SLAVE_EPSS_L3_CPU6] = &slv_epss_l3_cpu6,
	[SLAVE_EPSS_L3_CPU7] = &slv_epss_l3_cpu7,
	[SLAVE_EPSS_L3_SHARED] = &slv_epss_l3_shared,
};

static struct qcom_icc_desc lahaina_epss_l3 = {
	.nodes = lahaina_epss_l3_nodes,
	.num_nodes = ARRAY_SIZE(lahaina_epss_l3_nodes),
};

static int qcom_icc_aggregate(struct icc_node *node, u32 tag, u32 avg_bw,
			      u32 peak_bw, u32 *agg_avg, u32 *agg_peak)
{
	*agg_avg += avg_bw;
	*agg_peak = max_t(u32, *agg_peak, peak_bw);

	return 0;
}

static int qcom_icc_l3_cpu_set(struct icc_node *src, struct icc_node *dst)
{
	struct qcom_epss_l3_icc_provider *qp;
	struct icc_provider *provider;
	struct qcom_icc_node *qn;
	unsigned int index;
	u64 rate;

	qn = dst->data;
	provider = src->provider;
	qp = to_qcom_provider(provider);

	rate = dst->peak_bw;

	for (index = 0; index < qp->max_state; index++) {
		if (qp->lut_freqs[index] >= rate)
			break;
	}
#if IS_ENABLED(CONFIG_SEC_DEBUG_APPS_CLK_LOGGING)
	sec_smem_clk_osm_add_log_l3(qp->lut_freqs[index] / 1000);
#endif
	writel_relaxed(index, EPSS_L3_VOTE_REG(qp->base, qn->domain, qn->cpu));
	return 0;
}

static int qcom_epss_l3_remove(struct platform_device *pdev)
{
	struct qcom_epss_l3_icc_provider *qp = platform_get_drvdata(pdev);
	struct icc_provider *provider = &qp->provider;
	struct icc_node *n;

	list_for_each_entry(n, &provider->nodes, node_list) {
		icc_node_del(n);
		icc_node_destroy(n->id);
	}

	return icc_provider_del(provider);
}

static int qcom_epss_l3_probe(struct platform_device *pdev)
{
	u32 info, src, lval, i, prev_freq = 0, freq;
	static unsigned long hw_rate, xo_rate;
	struct qcom_epss_l3_icc_provider *qp;
	const struct qcom_icc_desc *desc;
	struct icc_onecell_data *data;
	struct icc_provider *provider;
	struct qcom_icc_node **qnodes;
	const char *compat = NULL;
	struct icc_node *node;
	struct resource *res;
	size_t num_nodes;
	struct clk *clk;
	int compatlen;
	int ret;

	clk = clk_get(&pdev->dev, "xo");
	if (IS_ERR(clk))
		return PTR_ERR(clk);

	xo_rate = clk_get_rate(clk);
	clk_put(clk);

	clk = clk_get(&pdev->dev, "alternate");
	if (IS_ERR(clk))
		return PTR_ERR(clk);

	hw_rate = clk_get_rate(clk) / CLK_HW_DIV;
	clk_put(clk);

	qp = devm_kzalloc(&pdev->dev, sizeof(*qp), GFP_KERNEL);
	if (!qp)
		return -ENOMEM;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENOMEM;

	qp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
	if (IS_ERR(qp->base))
		return PTR_ERR(qp->base);

	for (i = 0; i < LUT_MAX_ENTRIES; i++) {
		info = readl_relaxed(qp->base + REG_FREQ_LUT +
				     i * LUT_ROW_SIZE);
		src = FIELD_GET(LUT_SRC, info);
		lval = FIELD_GET(LUT_L_VAL, info);
		if (src)
			freq = xo_rate * lval;
		else
			freq = hw_rate;

		/*
		 * Two of the same frequencies means end of table.
		 */
		if (i > 0 && prev_freq == freq)
			break;

		qp->lut_freqs[i] = freq;
		prev_freq = freq;
	}
	qp->max_state = i;

	desc = of_device_get_match_data(&pdev->dev);
	if (!desc)
		return -EINVAL;

	qnodes = desc->nodes;
	num_nodes = desc->num_nodes;

	data = devm_kcalloc(&pdev->dev, num_nodes, sizeof(*node), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	provider = &qp->provider;
	provider->dev = &pdev->dev;
	provider->set = qcom_icc_l3_cpu_set;
	provider->aggregate = qcom_icc_aggregate;
	provider->xlate = of_icc_xlate_onecell;
	INIT_LIST_HEAD(&provider->nodes);
	provider->data = data;

	compat = of_get_property(pdev->dev.of_node, "compatible", &compatlen);
	if (!compat || (compatlen <= 0))
		return -EINVAL;

	ret = icc_provider_add(provider);
	if (ret) {
		dev_err(&pdev->dev, "error adding interconnect provider\n");
		return ret;
	}

	for (i = 0; i < num_nodes; i++) {
		size_t j;

		node = icc_node_create(qnodes[i]->id);
		if (IS_ERR(node)) {
			ret = PTR_ERR(node);
			goto err;
		}

		node->name = qnodes[i]->name;
		node->data = qnodes[i];
		icc_node_add(node, provider);

		dev_dbg(&pdev->dev, "registered node %s %d\n",
			qnodes[i]->name, node->id);

		/* populate links */
		for (j = 0; j < qnodes[i]->num_links; j++)
			icc_link_create(node, qnodes[i]->links[j]);

		data->nodes[i] = node;
	}
	data->num_nodes = num_nodes;

	platform_set_drvdata(pdev, qp);

	return ret;
err:
	qcom_epss_l3_remove(pdev);
	return ret;
}

static const struct of_device_id epss_l3_of_match[] = {
	{ .compatible = "qcom,lahaina-epss-l3-cpu", .data = &lahaina_epss_l3 },
	{ },
};
MODULE_DEVICE_TABLE(of, epss_l3_of_match);

static struct platform_driver epss_l3_driver = {
	.probe = qcom_epss_l3_probe,
	.remove = qcom_epss_l3_remove,
	.driver = {
		.name = "epss-l3",
		.of_match_table = epss_l3_of_match,
	},
};
module_platform_driver(epss_l3_driver);

MODULE_DESCRIPTION("QTI EPSS L3 interconnect driver");
MODULE_LICENSE("GPL v2");