blob: e64354dd52f1bf3b962dc5201be76b03dc67829d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nishanth Menon08b9dc22015-09-17 15:42:39 -05002/*
3 * (C) Copyright 2015
Nishanth Menoneaa39c62023-11-01 15:56:03 -05004 * Texas Instruments Incorporated - https://www.ti.com/
Nishanth Menon08b9dc22015-09-17 15:42:39 -05005 */
Patrick Delaunay81313352021-04-27 11:02:19 +02006
7#define LOG_CATEGORY UCLASS_REMOTEPROC
8
Nishanth Menon08b9dc22015-09-17 15:42:39 -05009#define pr_fmt(fmt) "%s: " fmt, __func__
Keerthy844db202022-01-27 13:16:55 +010010#include <elf.h>
Nishanth Menon08b9dc22015-09-17 15:42:39 -050011#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Nishanth Menon08b9dc22015-09-17 15:42:39 -050013#include <malloc.h>
Keerthy844db202022-01-27 13:16:55 +010014#include <virtio_ring.h>
MD Danish Anwardb1e4db2024-03-21 15:58:19 +053015#include <fs_loader.h>
Nishanth Menon08b9dc22015-09-17 15:42:39 -050016#include <remoteproc.h>
17#include <asm/io.h>
18#include <dm/device-internal.h>
19#include <dm.h>
20#include <dm/uclass.h>
21#include <dm/uclass-internal.h>
Keerthy844db202022-01-27 13:16:55 +010022#include <linux/compat.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060023#include <linux/printk.h>
Keerthy844db202022-01-27 13:16:55 +010024
25DECLARE_GLOBAL_DATA_PTR;
26
27struct resource_table {
28 u32 ver;
29 u32 num;
30 u32 reserved[2];
31 u32 offset[0];
32} __packed;
33
34typedef int (*handle_resource_t) (struct udevice *, void *, int offset, int avail);
35
36static struct resource_table *rsc_table;
Nishanth Menon08b9dc22015-09-17 15:42:39 -050037
Nishanth Menon08b9dc22015-09-17 15:42:39 -050038/**
39 * for_each_remoteproc_device() - iterate through the list of rproc devices
40 * @fn: check function to call per match, if this function returns fail,
41 * iteration is aborted with the resultant error value
42 * @skip_dev: Device to skip calling the callback about.
43 * @data: Data to pass to the callback function
44 *
45 * Return: 0 if none of the callback returned a non 0 result, else returns the
46 * result from the callback function
47 */
48static int for_each_remoteproc_device(int (*fn) (struct udevice *dev,
49 struct dm_rproc_uclass_pdata *uc_pdata,
50 const void *data),
51 struct udevice *skip_dev,
52 const void *data)
53{
54 struct udevice *dev;
55 struct dm_rproc_uclass_pdata *uc_pdata;
56 int ret;
57
58 for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev;
59 ret = uclass_find_next_device(&dev)) {
60 if (ret || dev == skip_dev)
61 continue;
Simon Glass71fa5b42020-12-03 16:55:18 -070062 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menon08b9dc22015-09-17 15:42:39 -050063 ret = fn(dev, uc_pdata, data);
64 if (ret)
65 return ret;
66 }
67
68 return 0;
69}
70
71/**
72 * _rproc_name_is_unique() - iteration helper to check if rproc name is unique
73 * @dev: device that we are checking name for
74 * @uc_pdata: uclass platform data
75 * @data: compare data (this is the name we want to ensure is unique)
76 *
77 * Return: 0 is there is no match(is unique); if there is a match(we dont
78 * have a unique name), return -EINVAL.
79 */
80static int _rproc_name_is_unique(struct udevice *dev,
81 struct dm_rproc_uclass_pdata *uc_pdata,
82 const void *data)
83{
84 const char *check_name = data;
85
86 /* devices not yet populated with data - so skip them */
Nishanth Menon3ab6a4e2015-11-30 22:05:58 -060087 if (!uc_pdata->name || !check_name)
Nishanth Menon08b9dc22015-09-17 15:42:39 -050088 return 0;
89
90 /* Return 0 to search further if we dont match */
91 if (strlen(uc_pdata->name) != strlen(check_name))
92 return 0;
93
94 if (!strcmp(uc_pdata->name, check_name))
95 return -EINVAL;
96
97 return 0;
98}
99
100/**
101 * rproc_name_is_unique() - Check if the rproc name is unique
102 * @check_dev: Device we are attempting to ensure is unique
103 * @check_name: Name we are trying to ensure is unique.
104 *
105 * Return: true if we have a unique name, false if name is not unique.
106 */
107static bool rproc_name_is_unique(struct udevice *check_dev,
108 const char *check_name)
109{
110 int ret;
111
112 ret = for_each_remoteproc_device(_rproc_name_is_unique,
113 check_dev, check_name);
114 return ret ? false : true;
115}
116
117/**
118 * rproc_pre_probe() - Pre probe accessor for the uclass
119 * @dev: device for which we are preprobing
120 *
121 * Parses and fills up the uclass pdata for use as needed by core and
122 * remote proc drivers.
123 *
124 * Return: 0 if all wernt ok, else appropriate error value.
125 */
126static int rproc_pre_probe(struct udevice *dev)
127{
128 struct dm_rproc_uclass_pdata *uc_pdata;
129 const struct dm_rproc_ops *ops;
130
Simon Glass71fa5b42020-12-03 16:55:18 -0700131 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500132
133 /* See if we need to populate via fdt */
134
Simon Glass95588622020-12-22 19:30:28 -0700135 if (!dev_get_plat(dev)) {
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500136#if CONFIG_IS_ENABLED(OF_CONTROL)
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500137 bool tmp;
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500138 debug("'%s': using fdt\n", dev->name);
Patrick Delaunayc249ae52021-09-20 17:56:06 +0200139 uc_pdata->name = dev_read_string(dev, "remoteproc-name");
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500140
141 /* Default is internal memory mapped */
142 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
Patrick Delaunayc249ae52021-09-20 17:56:06 +0200143 tmp = dev_read_bool(dev, "remoteproc-internal-memory-mapped");
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500144 if (tmp)
145 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
146#else
147 /* Nothing much we can do about this, can we? */
148 return -EINVAL;
149#endif
150
151 } else {
Simon Glass95588622020-12-22 19:30:28 -0700152 struct dm_rproc_uclass_pdata *pdata = dev_get_plat(dev);
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500153
154 debug("'%s': using legacy data\n", dev->name);
155 if (pdata->name)
156 uc_pdata->name = pdata->name;
157 uc_pdata->mem_type = pdata->mem_type;
158 uc_pdata->driver_plat_data = pdata->driver_plat_data;
159 }
160
161 /* Else try using device Name */
162 if (!uc_pdata->name)
163 uc_pdata->name = dev->name;
164 if (!uc_pdata->name) {
165 debug("Unnamed device!");
166 return -EINVAL;
167 }
168
169 if (!rproc_name_is_unique(dev, uc_pdata->name)) {
170 debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name);
171 return -EINVAL;
172 }
173
174 ops = rproc_get_ops(dev);
175 if (!ops) {
176 debug("%s driver has no ops?\n", dev->name);
177 return -EINVAL;
178 }
179
180 if (!ops->load || !ops->start) {
181 debug("%s driver has missing mandatory ops?\n", dev->name);
182 return -EINVAL;
183 }
184
185 return 0;
186}
187
188/**
189 * rproc_post_probe() - post probe accessor for the uclass
190 * @dev: deivce we finished probing
191 *
192 * initiate init function after the probe is completed. This allows
193 * the remote processor drivers to split up the initializations between
194 * probe and init as needed.
195 *
196 * Return: if the remote proc driver has a init routine, invokes it and
197 * hands over the return value. overall, 0 if all went well, else appropriate
198 * error value.
199 */
200static int rproc_post_probe(struct udevice *dev)
201{
202 const struct dm_rproc_ops *ops;
203
204 ops = rproc_get_ops(dev);
205 if (!ops) {
206 debug("%s driver has no ops?\n", dev->name);
207 return -EINVAL;
208 }
209
210 if (ops->init)
211 return ops->init(dev);
212
213 return 0;
214}
215
Keerthy844db202022-01-27 13:16:55 +0100216/**
217 * rproc_add_res() - After parsing the resource table add the mappings
218 * @dev: device we finished probing
219 * @mapping: rproc_mem_entry for the resource
220 *
221 * Return: if the remote proc driver has a add_res routine, invokes it and
222 * hands over the return value. overall, 0 if all went well, else appropriate
223 * error value.
224 */
225static int rproc_add_res(struct udevice *dev, struct rproc_mem_entry *mapping)
226{
227 const struct dm_rproc_ops *ops = rproc_get_ops(dev);
228
229 if (!ops->add_res)
230 return -ENOSYS;
231
232 return ops->add_res(dev, mapping);
233}
234
235/**
236 * rproc_alloc_mem() - After parsing the resource table allocat mem
237 * @dev: device we finished probing
238 * @len: rproc_mem_entry for the resource
239 * @align: alignment for the resource
240 *
241 * Return: if the remote proc driver has a add_res routine, invokes it and
242 * hands over the return value. overall, 0 if all went well, else appropriate
243 * error value.
244 */
245static void *rproc_alloc_mem(struct udevice *dev, unsigned long len,
246 unsigned long align)
247{
248 const struct dm_rproc_ops *ops;
249
250 ops = rproc_get_ops(dev);
251 if (!ops) {
252 debug("%s driver has no ops?\n", dev->name);
253 return NULL;
254 }
255
256 if (ops->alloc_mem)
257 return ops->alloc_mem(dev, len, align);
258
259 return NULL;
260}
261
262/**
263 * rproc_config_pagetable() - Configure page table for remote processor
264 * @dev: device we finished probing
265 * @virt: Virtual address of the resource
266 * @phys: Physical address the resource
267 * @len: length the resource
268 *
269 * Return: if the remote proc driver has a add_res routine, invokes it and
270 * hands over the return value. overall, 0 if all went well, else appropriate
271 * error value.
272 */
273static int rproc_config_pagetable(struct udevice *dev, unsigned int virt,
274 unsigned int phys, unsigned int len)
275{
276 const struct dm_rproc_ops *ops;
277
278 ops = rproc_get_ops(dev);
279 if (!ops) {
280 debug("%s driver has no ops?\n", dev->name);
281 return -EINVAL;
282 }
283
284 if (ops->config_pagetable)
285 return ops->config_pagetable(dev, virt, phys, len);
286
287 return 0;
288}
289
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500290UCLASS_DRIVER(rproc) = {
291 .id = UCLASS_REMOTEPROC,
292 .name = "remoteproc",
293 .flags = DM_UC_FLAG_SEQ_ALIAS,
294 .pre_probe = rproc_pre_probe,
295 .post_probe = rproc_post_probe,
Simon Glass33b2efb2020-12-03 16:55:22 -0700296 .per_device_plat_auto = sizeof(struct dm_rproc_uclass_pdata),
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500297};
298
299/* Remoteproc subsystem access functions */
300/**
301 * _rproc_probe_dev() - iteration helper to probe a rproc device
302 * @dev: device to probe
303 * @uc_pdata: uclass data allocated for the device
304 * @data: unused
305 *
306 * Return: 0 if all ok, else appropriate error value.
307 */
308static int _rproc_probe_dev(struct udevice *dev,
309 struct dm_rproc_uclass_pdata *uc_pdata,
310 const void *data)
311{
312 int ret;
313
314 ret = device_probe(dev);
315
316 if (ret)
317 debug("%s: Failed to initialize - %d\n", dev->name, ret);
318 return ret;
319}
320
321/**
322 * _rproc_dev_is_probed() - check if the device has been probed
323 * @dev: device to check
324 * @uc_pdata: unused
325 * @data: unused
326 *
327 * Return: -EAGAIN if not probed else return 0
328 */
329static int _rproc_dev_is_probed(struct udevice *dev,
330 struct dm_rproc_uclass_pdata *uc_pdata,
331 const void *data)
332{
Simon Glass6211d762020-12-19 10:40:10 -0700333 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500334 return 0;
335
336 return -EAGAIN;
337}
338
339bool rproc_is_initialized(void)
340{
341 int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL);
342 return ret ? false : true;
343}
344
345int rproc_init(void)
346{
347 int ret;
348
349 if (rproc_is_initialized()) {
350 debug("Already initialized\n");
351 return -EINVAL;
352 }
353
354 ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL);
355 return ret;
356}
357
Lokesh Vutladdca80e2018-08-27 15:57:50 +0530358int rproc_dev_init(int id)
359{
360 struct udevice *dev = NULL;
361 int ret;
362
363 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
364 if (ret) {
365 debug("Unknown remote processor id '%d' requested(%d)\n",
366 id, ret);
367 return ret;
368 }
369
370 ret = device_probe(dev);
371 if (ret)
372 debug("%s: Failed to initialize - %d\n", dev->name, ret);
373
374 return ret;
375}
376
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500377int rproc_load(int id, ulong addr, ulong size)
378{
379 struct udevice *dev = NULL;
380 struct dm_rproc_uclass_pdata *uc_pdata;
381 const struct dm_rproc_ops *ops;
382 int ret;
383
384 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
385 if (ret) {
386 debug("Unknown remote processor id '%d' requested(%d)\n",
387 id, ret);
388 return ret;
389 }
390
Simon Glass71fa5b42020-12-03 16:55:18 -0700391 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500392
393 ops = rproc_get_ops(dev);
394 if (!ops) {
395 debug("%s driver has no ops?\n", dev->name);
396 return -EINVAL;
397 }
398
399 debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n",
400 uc_pdata->name, addr, size);
401 if (ops->load)
402 return ops->load(dev, addr, size);
403
404 debug("%s: data corruption?? mandatory function is missing!\n",
405 dev->name);
406
407 return -EINVAL;
408};
409
410/*
411 * Completely internal helper enums..
412 * Keeping this isolated helps this code evolve independent of other
413 * parts..
414 */
415enum rproc_ops {
416 RPROC_START,
417 RPROC_STOP,
418 RPROC_RESET,
419 RPROC_PING,
420 RPROC_RUNNING,
421};
422
423/**
424 * _rproc_ops_wrapper() - wrapper for invoking remote proc driver callback
425 * @id: id of the remote processor
426 * @op: one of rproc_ops that indicate what operation to invoke
427 *
428 * Most of the checks and verification for remoteproc operations are more
429 * or less same for almost all operations. This allows us to put a wrapper
430 * and use the common checks to allow the driver to function appropriately.
431 *
432 * Return: 0 if all ok, else appropriate error value.
433 */
434static int _rproc_ops_wrapper(int id, enum rproc_ops op)
435{
436 struct udevice *dev = NULL;
437 struct dm_rproc_uclass_pdata *uc_pdata;
438 const struct dm_rproc_ops *ops;
439 int (*fn)(struct udevice *dev);
440 bool mandatory = false;
441 char *op_str;
442 int ret;
443
444 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
445 if (ret) {
446 debug("Unknown remote processor id '%d' requested(%d)\n",
447 id, ret);
448 return ret;
449 }
450
Simon Glass71fa5b42020-12-03 16:55:18 -0700451 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menon08b9dc22015-09-17 15:42:39 -0500452
453 ops = rproc_get_ops(dev);
454 if (!ops) {
455 debug("%s driver has no ops?\n", dev->name);
456 return -EINVAL;
457 }
458 switch (op) {
459 case RPROC_START:
460 fn = ops->start;
461 mandatory = true;
462 op_str = "Starting";
463 break;
464 case RPROC_STOP:
465 fn = ops->stop;
466 op_str = "Stopping";
467 break;
468 case RPROC_RESET:
469 fn = ops->reset;
470 op_str = "Resetting";
471 break;
472 case RPROC_RUNNING:
473 fn = ops->is_running;
474 op_str = "Checking if running:";
475 break;
476 case RPROC_PING:
477 fn = ops->ping;
478 op_str = "Pinging";
479 break;
480 default:
481 debug("what is '%d' operation??\n", op);
482 return -EINVAL;
483 }
484
485 debug("%s %s...\n", op_str, uc_pdata->name);
486 if (fn)
487 return fn(dev);
488
489 if (mandatory)
490 debug("%s: data corruption?? mandatory function is missing!\n",
491 dev->name);
492
493 return -ENOSYS;
494}
495
496int rproc_start(int id)
497{
498 return _rproc_ops_wrapper(id, RPROC_START);
499};
500
501int rproc_stop(int id)
502{
503 return _rproc_ops_wrapper(id, RPROC_STOP);
504};
505
506int rproc_reset(int id)
507{
508 return _rproc_ops_wrapper(id, RPROC_RESET);
509};
510
511int rproc_ping(int id)
512{
513 return _rproc_ops_wrapper(id, RPROC_PING);
514};
515
516int rproc_is_running(int id)
517{
518 return _rproc_ops_wrapper(id, RPROC_RUNNING);
519};
Keerthy844db202022-01-27 13:16:55 +0100520
Keerthy844db202022-01-27 13:16:55 +0100521static int handle_trace(struct udevice *dev, struct fw_rsc_trace *rsc,
522 int offset, int avail)
523{
524 if (sizeof(*rsc) > avail) {
525 debug("trace rsc is truncated\n");
526 return -EINVAL;
527 }
528
529 /*
530 * make sure reserved bytes are zeroes
531 */
532 if (rsc->reserved) {
533 debug("trace rsc has non zero reserved bytes\n");
534 return -EINVAL;
535 }
536
537 debug("trace rsc: da 0x%x, len 0x%x\n", rsc->da, rsc->len);
538
539 return 0;
540}
541
542static int handle_devmem(struct udevice *dev, struct fw_rsc_devmem *rsc,
543 int offset, int avail)
544{
545 struct rproc_mem_entry *mapping;
546
547 if (sizeof(*rsc) > avail) {
548 debug("devmem rsc is truncated\n");
549 return -EINVAL;
550 }
551
552 /*
553 * make sure reserved bytes are zeroes
554 */
555 if (rsc->reserved) {
556 debug("devmem rsc has non zero reserved bytes\n");
557 return -EINVAL;
558 }
559
560 debug("devmem rsc: pa 0x%x, da 0x%x, len 0x%x\n",
561 rsc->pa, rsc->da, rsc->len);
562
563 rproc_config_pagetable(dev, rsc->da, rsc->pa, rsc->len);
564
565 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
566 if (!mapping)
567 return -ENOMEM;
568
569 /*
570 * We'll need this info later when we'll want to unmap everything
571 * (e.g. on shutdown).
572 *
573 * We can't trust the remote processor not to change the resource
574 * table, so we must maintain this info independently.
575 */
576 mapping->dma = rsc->pa;
577 mapping->da = rsc->da;
578 mapping->len = rsc->len;
579 rproc_add_res(dev, mapping);
580
581 debug("mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
582 rsc->pa, rsc->da, rsc->len);
583
584 return 0;
585}
586
587static int handle_carveout(struct udevice *dev, struct fw_rsc_carveout *rsc,
588 int offset, int avail)
589{
590 struct rproc_mem_entry *mapping;
591
592 if (sizeof(*rsc) > avail) {
593 debug("carveout rsc is truncated\n");
594 return -EINVAL;
595 }
596
597 /*
598 * make sure reserved bytes are zeroes
599 */
600 if (rsc->reserved) {
601 debug("carveout rsc has non zero reserved bytes\n");
602 return -EINVAL;
603 }
604
605 debug("carveout rsc: da %x, pa %x, len %x, flags %x\n",
606 rsc->da, rsc->pa, rsc->len, rsc->flags);
607
608 rsc->pa = (uintptr_t)rproc_alloc_mem(dev, rsc->len, 8);
609 if (!rsc->pa) {
610 debug
611 ("failed to allocate carveout rsc: da %x, pa %x, len %x, flags %x\n",
612 rsc->da, rsc->pa, rsc->len, rsc->flags);
613 return -ENOMEM;
614 }
615 rproc_config_pagetable(dev, rsc->da, rsc->pa, rsc->len);
616
617 /*
618 * Ok, this is non-standard.
619 *
620 * Sometimes we can't rely on the generic iommu-based DMA API
621 * to dynamically allocate the device address and then set the IOMMU
622 * tables accordingly, because some remote processors might
623 * _require_ us to use hard coded device addresses that their
624 * firmware was compiled with.
625 *
626 * In this case, we must use the IOMMU API directly and map
627 * the memory to the device address as expected by the remote
628 * processor.
629 *
630 * Obviously such remote processor devices should not be configured
631 * to use the iommu-based DMA API: we expect 'dma' to contain the
632 * physical address in this case.
633 */
634 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
635 if (!mapping)
636 return -ENOMEM;
637
638 /*
639 * We'll need this info later when we'll want to unmap
640 * everything (e.g. on shutdown).
641 *
642 * We can't trust the remote processor not to change the
643 * resource table, so we must maintain this info independently.
644 */
645 mapping->dma = rsc->pa;
646 mapping->da = rsc->da;
647 mapping->len = rsc->len;
648 rproc_add_res(dev, mapping);
649
650 debug("carveout mapped 0x%x to 0x%x\n", rsc->da, rsc->pa);
651
652 return 0;
653}
654
655#define RPROC_PAGE_SHIFT 12
656#define RPROC_PAGE_SIZE BIT(RPROC_PAGE_SHIFT)
657#define RPROC_PAGE_ALIGN(x) (((x) + (RPROC_PAGE_SIZE - 1)) & ~(RPROC_PAGE_SIZE - 1))
658
659static int alloc_vring(struct udevice *dev, struct fw_rsc_vdev *rsc, int i)
660{
661 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
662 int size;
663 int order;
664 void *pa;
665
666 debug("vdev rsc: vring%d: da %x, qsz %d, align %d\n",
667 i, vring->da, vring->num, vring->align);
668
669 /*
670 * verify queue size and vring alignment are sane
671 */
672 if (!vring->num || !vring->align) {
673 debug("invalid qsz (%d) or alignment (%d)\n", vring->num,
674 vring->align);
675 return -EINVAL;
676 }
677
678 /*
679 * actual size of vring (in bytes)
680 */
681 size = RPROC_PAGE_ALIGN(vring_size(vring->num, vring->align));
682 order = vring->align >> RPROC_PAGE_SHIFT;
683
684 pa = rproc_alloc_mem(dev, size, order);
685 if (!pa) {
686 debug("failed to allocate vring rsc\n");
687 return -ENOMEM;
688 }
689 debug("alloc_mem(%#x, %d): %p\n", size, order, pa);
690 vring->da = (uintptr_t)pa;
691
Dan Carpenter21a74062023-07-26 10:00:33 +0300692 return 0;
Keerthy844db202022-01-27 13:16:55 +0100693}
694
695static int handle_vdev(struct udevice *dev, struct fw_rsc_vdev *rsc,
696 int offset, int avail)
697{
698 int i, ret;
699 void *pa;
700
701 /*
702 * make sure resource isn't truncated
703 */
704 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
705 + rsc->config_len > avail) {
706 debug("vdev rsc is truncated\n");
707 return -EINVAL;
708 }
709
710 /*
711 * make sure reserved bytes are zeroes
712 */
713 if (rsc->reserved[0] || rsc->reserved[1]) {
714 debug("vdev rsc has non zero reserved bytes\n");
715 return -EINVAL;
716 }
717
718 debug("vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
719 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
720
721 /*
722 * we currently support only two vrings per rvdev
723 */
724 if (rsc->num_of_vrings > 2) {
725 debug("too many vrings: %d\n", rsc->num_of_vrings);
726 return -EINVAL;
727 }
728
729 /*
730 * allocate the vrings
731 */
732 for (i = 0; i < rsc->num_of_vrings; i++) {
733 ret = alloc_vring(dev, rsc, i);
734 if (ret)
735 goto alloc_error;
736 }
737
738 pa = rproc_alloc_mem(dev, RPMSG_TOTAL_BUF_SPACE, 6);
739 if (!pa) {
740 debug("failed to allocate vdev rsc\n");
741 return -ENOMEM;
742 }
743 debug("vring buffer alloc_mem(%#x, 6): %p\n", RPMSG_TOTAL_BUF_SPACE,
744 pa);
745
746 return 0;
747
748 alloc_error:
749 return ret;
750}
751
752/*
753 * A lookup table for resource handlers. The indices are defined in
754 * enum fw_resource_type.
755 */
756static handle_resource_t loading_handlers[RSC_LAST] = {
757 [RSC_CARVEOUT] = (handle_resource_t)handle_carveout,
758 [RSC_DEVMEM] = (handle_resource_t)handle_devmem,
759 [RSC_TRACE] = (handle_resource_t)handle_trace,
760 [RSC_VDEV] = (handle_resource_t)handle_vdev,
761};
762
763/*
764 * handle firmware resource entries before booting the remote processor
765 */
766static int handle_resources(struct udevice *dev, int len,
767 handle_resource_t handlers[RSC_LAST])
768{
769 handle_resource_t handler;
770 int ret = 0, i;
771
772 for (i = 0; i < rsc_table->num; i++) {
773 int offset = rsc_table->offset[i];
774 struct fw_rsc_hdr *hdr = (void *)rsc_table + offset;
775 int avail = len - offset - sizeof(*hdr);
776 void *rsc = (void *)hdr + sizeof(*hdr);
777
778 /*
779 * make sure table isn't truncated
780 */
781 if (avail < 0) {
782 debug("rsc table is truncated\n");
783 return -EINVAL;
784 }
785
786 debug("rsc: type %d\n", hdr->type);
787
788 if (hdr->type >= RSC_LAST) {
789 debug("unsupported resource %d\n", hdr->type);
790 continue;
791 }
792
793 handler = handlers[hdr->type];
794 if (!handler)
795 continue;
796
797 ret = handler(dev, rsc, offset + sizeof(*hdr), avail);
798 if (ret)
799 break;
800 }
801
802 return ret;
803}
804
805static int
806handle_intmem_to_l3_mapping(struct udevice *dev,
807 struct rproc_intmem_to_l3_mapping *l3_mapping)
808{
809 u32 i = 0;
810
811 for (i = 0; i < l3_mapping->num_entries; i++) {
812 struct l3_map *curr_map = &l3_mapping->mappings[i];
813 struct rproc_mem_entry *mapping;
814
815 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
816 if (!mapping)
817 return -ENOMEM;
818
819 mapping->dma = curr_map->l3_addr;
820 mapping->da = curr_map->priv_addr;
821 mapping->len = curr_map->len;
822 rproc_add_res(dev, mapping);
823 }
824
825 return 0;
826}
827
828static Elf32_Shdr *rproc_find_table(unsigned int addr)
829{
830 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
831 Elf32_Shdr *shdr; /* Section header structure pointer */
832 Elf32_Shdr sectionheader;
833 int i;
834 u8 *elf_data;
835 char *name_table;
836 struct resource_table *ptable;
837
838 ehdr = (Elf32_Ehdr *)(uintptr_t)addr;
839 elf_data = (u8 *)ehdr;
840 shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
841 memcpy(&sectionheader, &shdr[ehdr->e_shstrndx], sizeof(sectionheader));
842 name_table = (char *)(elf_data + sectionheader.sh_offset);
843
844 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
845 memcpy(&sectionheader, shdr, sizeof(sectionheader));
846 u32 size = sectionheader.sh_size;
847 u32 offset = sectionheader.sh_offset;
848
849 if (strcmp
850 (name_table + sectionheader.sh_name, ".resource_table"))
851 continue;
852
853 ptable = (struct resource_table *)(elf_data + offset);
854
855 /*
856 * make sure table has at least the header
857 */
858 if (sizeof(struct resource_table) > size) {
859 debug("header-less resource table\n");
860 return NULL;
861 }
862
863 /*
864 * we don't support any version beyond the first
865 */
866 if (ptable->ver != 1) {
867 debug("unsupported fw ver: %d\n", ptable->ver);
868 return NULL;
869 }
870
871 /*
872 * make sure reserved bytes are zeroes
873 */
874 if (ptable->reserved[0] || ptable->reserved[1]) {
875 debug("non zero reserved bytes\n");
876 return NULL;
877 }
878
879 /*
880 * make sure the offsets array isn't truncated
881 */
882 if (ptable->num * sizeof(ptable->offset[0]) +
883 sizeof(struct resource_table) > size) {
884 debug("resource table incomplete\n");
885 return NULL;
886 }
887
888 return shdr;
889 }
890
891 return NULL;
892}
893
894struct resource_table *rproc_find_resource_table(struct udevice *dev,
895 unsigned int addr,
896 int *tablesz)
897{
898 Elf32_Shdr *shdr;
899 Elf32_Shdr sectionheader;
900 struct resource_table *ptable;
901 u8 *elf_data = (u8 *)(uintptr_t)addr;
902
903 shdr = rproc_find_table(addr);
904 if (!shdr) {
905 debug("%s: failed to get resource section header\n", __func__);
906 return NULL;
907 }
908
909 memcpy(&sectionheader, shdr, sizeof(sectionheader));
910 ptable = (struct resource_table *)(elf_data + sectionheader.sh_offset);
911 if (tablesz)
912 *tablesz = sectionheader.sh_size;
913
914 return ptable;
915}
916
917unsigned long rproc_parse_resource_table(struct udevice *dev, struct rproc *cfg)
918{
919 struct resource_table *ptable = NULL;
920 int tablesz;
921 int ret;
922 unsigned long addr;
923
924 addr = cfg->load_addr;
925
926 ptable = rproc_find_resource_table(dev, addr, &tablesz);
927 if (!ptable) {
928 debug("%s : failed to find resource table\n", __func__);
929 return 0;
930 }
931
932 debug("%s : found resource table\n", __func__);
933 rsc_table = kzalloc(tablesz, GFP_KERNEL);
934 if (!rsc_table) {
935 debug("resource table alloc failed!\n");
936 return 0;
937 }
938
939 /*
940 * Copy the resource table into a local buffer before handling the
941 * resource table.
942 */
943 memcpy(rsc_table, ptable, tablesz);
944 if (cfg->intmem_to_l3_mapping)
945 handle_intmem_to_l3_mapping(dev, cfg->intmem_to_l3_mapping);
946 ret = handle_resources(dev, tablesz, loading_handlers);
947 if (ret) {
948 debug("handle_resources failed: %d\n", ret);
949 return 0;
950 }
951
952 /*
953 * Instead of trying to mimic the kernel flow of copying the
954 * processed resource table into its post ELF load location in DDR
955 * copying it into its original location.
956 */
957 memcpy(ptable, rsc_table, tablesz);
958 free(rsc_table);
959 rsc_table = NULL;
960
961 return 1;
962}
MD Danish Anwardb1e4db2024-03-21 15:58:19 +0530963
964int rproc_set_firmware(struct udevice *rproc_dev, const char *fw_name)
965{
966 struct dm_rproc_uclass_pdata *uc_pdata;
967 int len;
968 char *p;
969
970 if (!rproc_dev || !fw_name)
971 return -EINVAL;
972
973 uc_pdata = dev_get_uclass_plat(rproc_dev);
974 if (!uc_pdata)
975 return -EINVAL;
976
977 len = strcspn(fw_name, "\n");
978 if (!len) {
979 debug("invalid firmware name\n");
980 return -EINVAL;
981 }
982
983 if (uc_pdata->fw_name)
984 free(uc_pdata->fw_name);
985
986 p = strndup(fw_name, len);
987 if (!p)
988 return -ENOMEM;
989
990 uc_pdata->fw_name = p;
991
992 return 0;
993}
994
995#if CONFIG_IS_ENABLED(FS_LOADER)
996int rproc_boot(struct udevice *rproc_dev)
997{
998 struct dm_rproc_uclass_pdata *uc_pdata;
999 struct udevice *fs_loader;
1000 int core_id, ret = 0;
1001 char *firmware;
1002 void *addr;
1003
1004 if (!rproc_dev)
1005 return -EINVAL;
1006
1007 uc_pdata = dev_get_uclass_plat(rproc_dev);
1008 if (!uc_pdata)
1009 return -EINVAL;
1010
1011 core_id = dev_seq(rproc_dev);
1012 firmware = uc_pdata->fw_name;
1013 if (!firmware) {
1014 debug("No firmware name set for rproc core %d\n", core_id);
1015 return -EINVAL;
1016 }
1017
1018 /* Initialize all rproc cores */
1019 if (!rproc_is_initialized()) {
1020 ret = rproc_init();
1021 if (ret) {
1022 debug("rproc_init() failed: %d\n", ret);
1023 return ret;
1024 }
1025 }
1026
1027 /* Loading firmware to a given address */
1028 ret = get_fs_loader(&fs_loader);
1029 if (ret) {
1030 debug("could not get fs loader: %d\n", ret);
1031 return ret;
1032 }
1033
1034 if (CONFIG_REMOTEPROC_MAX_FW_SIZE) {
1035 addr = malloc(CONFIG_REMOTEPROC_MAX_FW_SIZE);
1036 if (!addr)
1037 return -ENOMEM;
1038 } else {
1039 debug("CONFIG_REMOTEPROC_MAX_FW_SIZE not defined\n");
1040 return -EINVAL;
1041 }
1042
1043 ret = request_firmware_into_buf(fs_loader, firmware, addr, CONFIG_REMOTEPROC_MAX_FW_SIZE,
1044 0);
1045 if (ret < 0) {
1046 debug("could not request %s: %d\n", firmware, ret);
1047 goto free_buffer;
1048 }
1049
1050 ret = rproc_load(core_id, (ulong)addr, ret);
1051 if (ret) {
1052 debug("failed to load %s to rproc core %d from addr 0x%08lX err %d\n",
1053 uc_pdata->fw_name, core_id, (ulong)addr, ret);
1054 goto free_buffer;
1055 }
1056
1057 ret = rproc_start(core_id);
1058 if (ret)
1059 debug("failed to start rproc core %d\n", core_id);
1060
1061free_buffer:
1062 free(addr);
1063 return ret;
1064}
1065#endif