summaryrefslogtreecommitdiff
path: root/drivers/remoteproc/ti_k3_dsp_rproc.c
blob: 3361555c5367afafb8c6fb3389540b7360e30403 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// SPDX-License-Identifier: GPL-2.0+
/*
 * Texas Instruments' K3 DSP Remoteproc driver
 *
 * Copyright (C) 2018-2020 Texas Instruments Incorporated - http://www.ti.com/
 *	Lokesh Vutla <lokeshvutla@ti.com>
 *	Suman Anna <s-anna@ti.com>
 */

#include <common.h>
#include <dm.h>
#include <log.h>
#include <malloc.h>
#include <remoteproc.h>
#include <errno.h>
#include <clk.h>
#include <reset.h>
#include <asm/io.h>
#include <power-domain.h>
#include <dm/device_compat.h>
#include <linux/err.h>
#include <linux/sizes.h>
#include <linux/soc/ti/ti_sci_protocol.h>
#include "ti_sci_proc.h"

#define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK	(SZ_16M - 1)

/**
 * struct k3_dsp_mem - internal memory structure
 * @cpu_addr: MPU virtual address of the memory region
 * @bus_addr: Bus address used to access the memory region
 * @dev_addr: Device address from remoteproc view
 * @size: Size of the memory region
 */
struct k3_dsp_mem {
	void __iomem *cpu_addr;
	phys_addr_t bus_addr;
	phys_addr_t dev_addr;
	size_t size;
};

/**
 * struct k3_dsp_boot_data - internal data structure used for boot
 * @boot_align_addr: Boot vector address alignment granularity
 * @uses_lreset: Flag to denote the need for local reset management
 */
struct k3_dsp_boot_data {
	u32 boot_align_addr;
	bool uses_lreset;
};

/**
 * struct k3_dsp_privdata - Structure representing Remote processor data.
 * @rproc_rst:		rproc reset control data
 * @tsp:		Pointer to TISCI proc contrl handle
 * @data:		Pointer to DSP specific boot data structure
 * @mem:		Array of available memories
 * @num_mem:		Number of available memories
 */
struct k3_dsp_privdata {
	struct reset_ctl dsp_rst;
	struct ti_sci_proc tsp;
	struct k3_dsp_boot_data *data;
	struct k3_dsp_mem *mem;
	int num_mems;
};

/*
 * The C66x DSP cores have a local reset that affects only the CPU, and a
 * generic module reset that powers on the device and allows the DSP internal
 * memories to be accessed while the local reset is asserted. This function is
 * used to release the global reset on C66x DSPs to allow loading into the DSP
 * internal RAMs. This helper function is invoked in k3_dsp_load() before any
 * actual firmware loading and is undone only in k3_dsp_stop(). The local reset
 * on C71x cores is a no-op and the global reset cannot be released on C71x
 * cores until after the firmware images are loaded, so this function does
 * nothing for C71x cores.
 */
static int k3_dsp_prepare(struct udevice *dev)
{
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
	struct k3_dsp_boot_data *data = dsp->data;
	int ret;

	/* local reset is no-op on C71x processors */
	if (!data->uses_lreset)
		return 0;

	ret = ti_sci_proc_power_domain_on(&dsp->tsp);
	if (ret)
		dev_err(dev, "cannot enable internal RAM loading, ret = %d\n",
			ret);

	return ret;
}

/*
 * This function is the counterpart to k3_dsp_prepare() and is used to assert
 * the global reset on C66x DSP cores (no-op for C71x DSP cores). This completes
 * the second step of powering down the C66x DSP cores. The cores themselves
 * are halted through the local reset in first step. This function is invoked
 * in k3_dsp_stop() after the local reset is asserted.
 */
static int k3_dsp_unprepare(struct udevice *dev)
{
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
	struct k3_dsp_boot_data *data = dsp->data;

	/* local reset is no-op on C71x processors */
	if (!data->uses_lreset)
		return 0;

	return ti_sci_proc_power_domain_off(&dsp->tsp);
}

/**
 * k3_dsp_load() - Load up the Remote processor image
 * @dev:	rproc device pointer
 * @addr:	Address at which image is available
 * @size:	size of the image
 *
 * Return: 0 if all goes good, else appropriate error message.
 */
static int k3_dsp_load(struct udevice *dev, ulong addr, ulong size)
{
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
	struct k3_dsp_boot_data *data = dsp->data;
	u32 boot_vector;
	int ret;

	dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
	ret = ti_sci_proc_request(&dsp->tsp);
	if (ret)
		return ret;

	ret = k3_dsp_prepare(dev);
	if (ret) {
		dev_err(dev, "DSP prepare failed for core %d\n",
			dsp->tsp.proc_id);
		goto proc_release;
	}

	ret = rproc_elf_load_image(dev, addr, size);
	if (ret < 0) {
		dev_err(dev, "Loading elf failed %d\n", ret);
		goto unprepare;
	}

	boot_vector = rproc_elf_get_boot_addr(dev, addr);
	if (boot_vector & (data->boot_align_addr - 1)) {
		ret = -EINVAL;
		dev_err(dev, "Boot vector 0x%x not aligned on 0x%x boundary\n",
			boot_vector, data->boot_align_addr);
		goto proc_release;
	}

	dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector);

	ret = ti_sci_proc_set_config(&dsp->tsp, boot_vector, 0, 0);
unprepare:
	if (ret)
		k3_dsp_unprepare(dev);
proc_release:
	ti_sci_proc_release(&dsp->tsp);
	return ret;
}

/**
 * k3_dsp_start() - Start the remote processor
 * @dev:	rproc device pointer
 *
 * Return: 0 if all went ok, else return appropriate error
 */
static int k3_dsp_start(struct udevice *dev)
{
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
	struct k3_dsp_boot_data *data = dsp->data;
	int ret;

	dev_dbg(dev, "%s\n", __func__);

	ret = ti_sci_proc_request(&dsp->tsp);
	if (ret)
		return ret;

	if (!data->uses_lreset) {
		ret = ti_sci_proc_power_domain_on(&dsp->tsp);
		if (ret)
			goto proc_release;
	}

	ret = reset_deassert(&dsp->dsp_rst);
	if (ret) {
		if (!data->uses_lreset)
			ti_sci_proc_power_domain_off(&dsp->tsp);
	}

proc_release:
	ti_sci_proc_release(&dsp->tsp);

	return ret;
}

static int k3_dsp_stop(struct udevice *dev)
{
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);

	dev_dbg(dev, "%s\n", __func__);

	ti_sci_proc_request(&dsp->tsp);
	reset_assert(&dsp->dsp_rst);
	ti_sci_proc_power_domain_off(&dsp->tsp);
	ti_sci_proc_release(&dsp->tsp);

	return 0;
}

/**
 * k3_dsp_init() - Initialize the remote processor
 * @dev:	rproc device pointer
 *
 * Return: 0 if all went ok, else return appropriate error
 */
static int k3_dsp_init(struct udevice *dev)
{
	dev_dbg(dev, "%s\n", __func__);

	return 0;
}

static int k3_dsp_reset(struct udevice *dev)
{
	dev_dbg(dev, "%s\n", __func__);

	return 0;
}

static void *k3_dsp_da_to_va(struct udevice *dev, ulong da, ulong len)
{
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
	phys_addr_t bus_addr, dev_addr;
	void __iomem *va = NULL;
	size_t size;
	u32 offset;
	int i;

	dev_dbg(dev, "%s\n", __func__);

	if (len <= 0)
		return NULL;

	for (i = 0; i < dsp->num_mems; i++) {
		bus_addr = dsp->mem[i].bus_addr;
		dev_addr = dsp->mem[i].dev_addr;
		size = dsp->mem[i].size;

		if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
			offset = da - dev_addr;
			va = dsp->mem[i].cpu_addr + offset;
			return (__force void *)va;
		}

		if (da >= bus_addr && (da + len) <= (bus_addr + size)) {
			offset = da - bus_addr;
			va = dsp->mem[i].cpu_addr + offset;
			return (__force void *)va;
		}
	}

	/* Assume it is DDR region and return da */
	return map_physmem(da, len, MAP_NOCACHE);
}

static const struct dm_rproc_ops k3_dsp_ops = {
	.init = k3_dsp_init,
	.load = k3_dsp_load,
	.start = k3_dsp_start,
	.stop = k3_dsp_stop,
	.reset = k3_dsp_reset,
	.device_to_virt = k3_dsp_da_to_va,
};

static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp)
{
	u32 ids[2];
	int ret;

	dev_dbg(dev, "%s\n", __func__);

	tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci");
	if (IS_ERR(tsp->sci)) {
		dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci));
		return PTR_ERR(tsp->sci);
	}

	ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2);
	if (ret) {
		dev_err(dev, "Proc IDs not populated %d\n", ret);
		return ret;
	}

	tsp->ops = &tsp->sci->ops.proc_ops;
	tsp->proc_id = ids[0];
	tsp->host_id = ids[1];
	tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id",
					   TI_SCI_RESOURCE_NULL);
	if (tsp->dev_id == TI_SCI_RESOURCE_NULL) {
		dev_err(dev, "Device ID not populated %d\n", ret);
		return -ENODEV;
	}

	return 0;
}

static int k3_dsp_of_get_memories(struct udevice *dev)
{
	static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
	int i;

	dev_dbg(dev, "%s\n", __func__);

	dsp->num_mems = ARRAY_SIZE(mem_names);
	dsp->mem = calloc(dsp->num_mems, sizeof(*dsp->mem));
	if (!dsp->mem)
		return -ENOMEM;

	for (i = 0; i < dsp->num_mems; i++) {
		/* C71 cores only have a L1P Cache, there are no L1P SRAMs */
		if (device_is_compatible(dev, "ti,j721e-c71-dsp") &&
		    !strcmp(mem_names[i], "l1pram")) {
			dsp->mem[i].bus_addr = FDT_ADDR_T_NONE;
			dsp->mem[i].dev_addr = FDT_ADDR_T_NONE;
			dsp->mem[i].cpu_addr = NULL;
			dsp->mem[i].size = 0;
			continue;
		}

		dsp->mem[i].bus_addr = dev_read_addr_size_name(dev, mem_names[i],
					  (fdt_addr_t *)&dsp->mem[i].size);
		if (dsp->mem[i].bus_addr == FDT_ADDR_T_NONE) {
			dev_err(dev, "%s bus address not found\n", mem_names[i]);
			return -EINVAL;
		}
		dsp->mem[i].cpu_addr = map_physmem(dsp->mem[i].bus_addr,
						   dsp->mem[i].size,
						   MAP_NOCACHE);
		dsp->mem[i].dev_addr = dsp->mem[i].bus_addr &
					KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;

		dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da %pa\n",
			mem_names[i], &dsp->mem[i].bus_addr,
			dsp->mem[i].size, dsp->mem[i].cpu_addr,
			&dsp->mem[i].dev_addr);
	}

	return 0;
}

/**
 * k3_of_to_priv() - generate private data from device tree
 * @dev:	corresponding k3 dsp processor device
 * @dsp:	pointer to driver specific private data
 *
 * Return: 0 if all goes good, else appropriate error message.
 */
static int k3_dsp_of_to_priv(struct udevice *dev, struct k3_dsp_privdata *dsp)
{
	int ret;

	dev_dbg(dev, "%s\n", __func__);

	ret = reset_get_by_index(dev, 0, &dsp->dsp_rst);
	if (ret) {
		dev_err(dev, "reset_get() failed: %d\n", ret);
		return ret;
	}

	ret = ti_sci_proc_of_to_priv(dev, &dsp->tsp);
	if (ret)
		return ret;

	ret =  k3_dsp_of_get_memories(dev);
	if (ret)
		return ret;

	dsp->data = (struct k3_dsp_boot_data *)dev_get_driver_data(dev);

	return 0;
}

/**
 * k3_dsp_probe() - Basic probe
 * @dev:	corresponding k3 remote processor device
 *
 * Return: 0 if all goes good, else appropriate error message.
 */
static int k3_dsp_probe(struct udevice *dev)
{
	struct k3_dsp_privdata *dsp;
	int ret;

	dev_dbg(dev, "%s\n", __func__);

	dsp = dev_get_priv(dev);

	ret = k3_dsp_of_to_priv(dev, dsp);
	if (ret) {
		dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret);
		return ret;
	}

	/*
	 * The DSP local resets are deasserted by default on Power-On-Reset.
	 * Assert the local resets to ensure the DSPs don't execute bogus code
	 * in .load() callback when the module reset is released to support
	 * internal memory loading. This is needed for C66x DSPs, and is a
	 * no-op on C71x DSPs.
	 */
	reset_assert(&dsp->dsp_rst);

	dev_dbg(dev, "Remoteproc successfully probed\n");

	return 0;
}

static int k3_dsp_remove(struct udevice *dev)
{
	struct k3_dsp_privdata *dsp = dev_get_priv(dev);

	free(dsp->mem);

	return 0;
}

static const struct k3_dsp_boot_data c66_data = {
	.boot_align_addr = SZ_1K,
	.uses_lreset = true,
};

static const struct k3_dsp_boot_data c71_data = {
	.boot_align_addr = SZ_2M,
	.uses_lreset = false,
};

static const struct udevice_id k3_dsp_ids[] = {
	{ .compatible = "ti,j721e-c66-dsp", .data = (ulong)&c66_data, },
	{ .compatible = "ti,j721e-c71-dsp", .data = (ulong)&c71_data, },
	{}
};

U_BOOT_DRIVER(k3_dsp) = {
	.name = "k3_dsp",
	.of_match = k3_dsp_ids,
	.id = UCLASS_REMOTEPROC,
	.ops = &k3_dsp_ops,
	.probe = k3_dsp_probe,
	.remove = k3_dsp_remove,
	.priv_auto_alloc_size = sizeof(struct k3_dsp_privdata),
};