1 /*
2  * Remoteproc Framework
3  *
4  * Copyright(c) 2018 Xilinx Ltd.
5  * Copyright(c) 2011 Texas Instruments, Inc.
6  * Copyright(c) 2011 Google, Inc.
7  * All rights reserved.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  */
11 
12 #ifndef REMOTEPROC_H
13 #define REMOTEPROC_H
14 
15 #include <metal/io.h>
16 #include <metal/mutex.h>
17 #include <openamp/compiler.h>
18 
19 #if defined __cplusplus
20 extern "C" {
21 #endif
22 
23 #define RSC_NOTIFY_ID_ANY 0xFFFFFFFFUL
24 
25 /**
26  * struct resource_table - firmware resource table header
27  * @ver: version number
28  * @num: number of resource entries
29  * @reserved: reserved (must be zero)
30  * @offset: array of offsets pointing at the various resource entries
31  *
32  * A resource table is essentially a list of system resources required
33  * by the remote remote_proc. It may also include configuration entries.
34  * If needed, the remote remote_proc firmware should contain this table
35  * as a dedicated ".resource_table" ELF section.
36  *
37  * Some resources entries are mere announcements, where the host is informed
38  * of specific remoteproc configuration. Other entries require the host to
39  * do something (e.g. allocate a system resource). Sometimes a negotiation
40  * is expected, where the firmware requests a resource, and once allocated,
41  * the host should provide back its details (e.g. address of an allocated
42  * memory region).
43  *
44  * The header of the resource table, as expressed by this structure,
45  * contains a version number (should we need to change this format in the
46  * future), the number of available resource entries, and their offsets
47  * in the table.
48  *
49  * Immediately following this header are the resource entries themselves,
50  * each of which begins with a resource entry header (as described below).
51  */
52 OPENAMP_PACKED_BEGIN
53 struct resource_table {
54 	uint32_t ver;
55 	uint32_t num;
56 	uint32_t reserved[2];
57 	uint32_t offset[0];
58 } OPENAMP_PACKED_END;
59 
60 /**
61  * struct fw_rsc_hdr - firmware resource entry header
62  * @type: resource type
63  * @data: resource data
64  *
65  * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
66  * its @type. The content of the entry itself will immediately follow
67  * this header, and it should be parsed according to the resource type.
68  */
69 OPENAMP_PACKED_BEGIN
70 struct fw_rsc_hdr {
71 	uint32_t type;
72 	uint8_t data[0];
73 } OPENAMP_PACKED_END;
74 
75 /**
76  * enum fw_resource_type - types of resource entries
77  *
78  * @RSC_CARVEOUT:   request for allocation of a physically contiguous
79  *          memory region.
80  * @RSC_DEVMEM:     request to iommu_map a memory-based peripheral.
81  * @RSC_TRACE:      announces the availability of a trace buffer into which
82  *          the remote remote_proc will be writing logs.
83  * @RSC_VDEV:       declare support for a virtio device, and serve as its
84  *          virtio header.
85  * @RSC_VENDOR_START: start of the vendor specific resource types range
86  * @RSC_VENDOR_END  : end of the vendor specific resource types range
87  * @RSC_LAST:       just keep this one at the end
88  *
89  * For more details regarding a specific resource type, please see its
90  * dedicated structure below.
91  *
92  * Please note that these values are used as indices to the rproc_handle_rsc
93  * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
94  * check the validity of an index before the lookup table is accessed, so
95  * please update it as needed.
96  */
97 enum fw_resource_type {
98 	RSC_CARVEOUT = 0,
99 	RSC_DEVMEM = 1,
100 	RSC_TRACE = 2,
101 	RSC_VDEV = 3,
102 	RSC_RPROC_MEM = 4,
103 	RSC_FW_CHKSUM = 5,
104 	RSC_LAST = 6,
105 	RSC_VENDOR_START = 128,
106 	RSC_VENDOR_END = 512,
107 };
108 
109 #define FW_RSC_ADDR_ANY (0xFFFFFFFFFFFFFFFF)
110 #define FW_RSC_U32_ADDR_ANY (0xFFFFFFFF)
111 
112 /**
113  * struct fw_rsc_carveout - physically contiguous memory request
114  * @da: device address
115  * @pa: physical address
116  * @len: length (in bytes)
117  * @flags: iommu protection flags
118  * @reserved: reserved (must be zero)
119  * @name: human-readable name of the requested memory region
120  *
121  * This resource entry requests the host to allocate a physically contiguous
122  * memory region.
123  *
124  * These request entries should precede other firmware resource entries,
125  * as other entries might request placing other data objects inside
126  * these memory regions (e.g. data/code segments, trace resource entries, ...).
127  *
128  * Allocating memory this way helps utilizing the reserved physical memory
129  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
130  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
131  * pressure is important; it may have a substantial impact on performance.
132  *
133  * If the firmware is compiled with static addresses, then @da should specify
134  * the expected device address of this memory region. If @da is set to
135  * FW_RSC_ADDR_ANY, then the host will dynamically allocate it, and then
136  * overwrite @da with the dynamically allocated address.
137  *
138  * We will always use @da to negotiate the device addresses, even if it
139  * isn't using an iommu. In that case, though, it will obviously contain
140  * physical addresses.
141  *
142  * Some remote remote_procs needs to know the allocated physical address
143  * even if they do use an iommu. This is needed, e.g., if they control
144  * hardware accelerators which access the physical memory directly (this
145  * is the case with OMAP4 for instance). In that case, the host will
146  * overwrite @pa with the dynamically allocated physical address.
147  * Generally we don't want to expose physical addresses if we don't have to
148  * (remote remote_procs are generally _not_ trusted), so we might want to
149  * change this to happen _only_ when explicitly required by the hardware.
150  *
151  * @flags is used to provide IOMMU protection flags, and @name should
152  * (optionally) contain a human readable name of this carveout region
153  * (mainly for debugging purposes).
154  */
155 OPENAMP_PACKED_BEGIN
156 struct fw_rsc_carveout {
157 	uint32_t type;
158 	uint32_t da;
159 	uint32_t pa;
160 	uint32_t len;
161 	uint32_t flags;
162 	uint32_t reserved;
163 	uint8_t name[32];
164 } OPENAMP_PACKED_END;
165 
166 /**
167  * struct fw_rsc_devmem - iommu mapping request
168  * @da: device address
169  * @pa: physical address
170  * @len: length (in bytes)
171  * @flags: iommu protection flags
172  * @reserved: reserved (must be zero)
173  * @name: human-readable name of the requested region to be mapped
174  *
175  * This resource entry requests the host to iommu map a physically contiguous
176  * memory region. This is needed in case the remote remote_proc requires
177  * access to certain memory-based peripherals; _never_ use it to access
178  * regular memory.
179  *
180  * This is obviously only needed if the remote remote_proc is accessing memory
181  * via an iommu.
182  *
183  * @da should specify the required device address, @pa should specify
184  * the physical address we want to map, @len should specify the size of
185  * the mapping and @flags is the IOMMU protection flags. As always, @name may
186  * (optionally) contain a human readable name of this mapping (mainly for
187  * debugging purposes).
188  *
189  * Note: at this point we just "trust" those devmem entries to contain valid
190  * physical addresses, but this isn't safe and will be changed: eventually we
191  * want remoteproc implementations to provide us ranges of physical addresses
192  * the firmware is allowed to request, and not allow firmwares to request
193  * access to physical addresses that are outside those ranges.
194  */
195 OPENAMP_PACKED_BEGIN
196 struct fw_rsc_devmem {
197 	uint32_t type;
198 	uint32_t da;
199 	uint32_t pa;
200 	uint32_t len;
201 	uint32_t flags;
202 	uint32_t reserved;
203 	uint8_t name[32];
204 } OPENAMP_PACKED_END;
205 
206 /**
207  * struct fw_rsc_trace - trace buffer declaration
208  * @da: device address
209  * @len: length (in bytes)
210  * @reserved: reserved (must be zero)
211  * @name: human-readable name of the trace buffer
212  *
213  * This resource entry provides the host information about a trace buffer
214  * into which the remote remote_proc will write log messages.
215  *
216  * @da specifies the device address of the buffer, @len specifies
217  * its size, and @name may contain a human readable name of the trace buffer.
218  *
219  * After booting the remote remote_proc, the trace buffers are exposed to the
220  * user via debugfs entries (called trace0, trace1, etc..).
221  */
222 OPENAMP_PACKED_BEGIN
223 struct fw_rsc_trace {
224 	uint32_t type;
225 	uint32_t da;
226 	uint32_t len;
227 	uint32_t reserved;
228 	uint8_t name[32];
229 } OPENAMP_PACKED_END;
230 
231 /**
232  * struct fw_rsc_vdev_vring - vring descriptor entry
233  * @da: device address
234  * @align: the alignment between the consumer and producer parts of the vring
235  * @num: num of buffers supported by this vring (must be power of two)
236  * @notifyid is a unique rproc-wide notify index for this vring. This notify
237  * index is used when kicking a remote remote_proc, to let it know that this
238  * vring is triggered.
239  * @reserved: reserved (must be zero)
240  *
241  * This descriptor is not a resource entry by itself; it is part of the
242  * vdev resource type (see below).
243  *
244  * Note that @da should either contain the device address where
245  * the remote remote_proc is expecting the vring, or indicate that
246  * dynamically allocation of the vring's device address is supported.
247  */
248 OPENAMP_PACKED_BEGIN
249 struct fw_rsc_vdev_vring {
250 	uint32_t da;
251 	uint32_t align;
252 	uint32_t num;
253 	uint32_t notifyid;
254 	uint32_t reserved;
255 } OPENAMP_PACKED_END;
256 
257 /**
258  * struct fw_rsc_vdev - virtio device header
259  * @id: virtio device id (as in virtio_ids.h)
260  * @notifyid is a unique rproc-wide notify index for this vdev. This notify
261  * index is used when kicking a remote remote_proc, to let it know that the
262  * status/features of this vdev have changes.
263  * @dfeatures specifies the virtio device features supported by the firmware
264  * @gfeatures is a place holder used by the host to write back the
265  * negotiated features that are supported by both sides.
266  * @config_len is the size of the virtio config space of this vdev. The config
267  * space lies in the resource table immediate after this vdev header.
268  * @status is a place holder where the host will indicate its virtio progress.
269  * @num_of_vrings indicates how many vrings are described in this vdev header
270  * @reserved: reserved (must be zero)
271  * @vring is an array of @num_of_vrings entries of 'struct fw_rsc_vdev_vring'.
272  *
273  * This resource is a virtio device header: it provides information about
274  * the vdev, and is then used by the host and its peer remote remote_procs
275  * to negotiate and share certain virtio properties.
276  *
277  * By providing this resource entry, the firmware essentially asks remoteproc
278  * to statically allocate a vdev upon registration of the rproc (dynamic vdev
279  * allocation is not yet supported).
280  *
281  * Note: unlike virtualization systems, the term 'host' here means
282  * the Linux side which is running remoteproc to control the remote
283  * remote_procs. We use the name 'gfeatures' to comply with virtio's terms,
284  * though there isn't really any virtualized guest OS here: it's the host
285  * which is responsible for negotiating the final features.
286  * Yeah, it's a bit confusing.
287  *
288  * Note: immediately following this structure is the virtio config space for
289  * this vdev (which is specific to the vdev; for more info, read the virtio
290  * spec). the size of the config space is specified by @config_len.
291  */
292 OPENAMP_PACKED_BEGIN
293 struct fw_rsc_vdev {
294 	uint32_t type;
295 	uint32_t id;
296 	uint32_t notifyid;
297 	uint32_t dfeatures;
298 	uint32_t gfeatures;
299 	uint32_t config_len;
300 	uint8_t status;
301 	uint8_t num_of_vrings;
302 	uint8_t reserved[2];
303 	struct fw_rsc_vdev_vring vring[0];
304 } OPENAMP_PACKED_END;
305 
306 /**
307  * struct fw_rsc_vendor - remote processor vendor specific resource
308  * @len: length of the resource
309  *
310  * This resource entry tells the host the vendor specific resource
311  * required by the remote.
312  *
313  * These request entries should precede other shared resource entries
314  * such as vdevs, vrings.
315  */
316 OPENAMP_PACKED_BEGIN
317 struct fw_rsc_vendor {
318 	uint32_t type;
319 	uint32_t len;
320 } OPENAMP_PACKED_END;
321 
322 /**
323  * struct fw_rsc_rproc_mem - remote processor memory
324  * @da: device address
325  * @pa: physical address
326  * @len: length (in bytes)
327  * @reserved: reserved (must be zero)
328  *
329  * This resource entry tells the host to the remote processor
330  * memory that the host can be used as shared memory.
331  *
332  * These request entries should precede other shared resource entries
333  * such as vdevs, vrings.
334  */
335 OPENAMP_PACKED_BEGIN
336 struct fw_rsc_rproc_mem {
337 	uint32_t type;
338 	uint32_t da;
339 	uint32_t pa;
340 	uint32_t len;
341 	uint32_t reserved;
342 } OPENAMP_PACKED_END;
343 
344 /*
345  * struct fw_rsc_fw_chksum - firmware checksum
346  * @algo: algorithm to generate the cheksum
347  * @chksum: checksum of the firmware loadable sections.
348  *
349  * This resource entry provides checksum for the firmware loadable sections.
350  * It is used to check if the remote already runs with the expected firmware to
351  * decide if it needs to start the remote if the remote is already running.
352  */
353 OPENAMP_PACKED_BEGIN
354 struct fw_rsc_fw_chksum {
355 	uint32_t type;
356 	uint8_t algo[16];
357 	uint8_t chksum[64];
358 } OPENAMP_PACKED_END;
359 
360 struct loader_ops;
361 struct image_store_ops;
362 struct remoteproc_ops;
363 
364 /**
365  * struct remoteproc_mem
366  *
367  * This structure presents the memory used by the remote processor
368  *
369  * @da: device memory
370  * @pa: physical memory
371  * @size: size of the memory
372  * @io: pointer to the I/O region
373  * @node: list node
374  */
375 struct remoteproc_mem {
376 	metal_phys_addr_t da;
377 	metal_phys_addr_t pa;
378 	size_t size;
379 	char name[32];
380 	struct metal_io_region *io;
381 	struct metal_list node;
382 };
383 
384 /**
385  * struct remoteproc
386  *
387  * This structure is maintained by the remoteproc to represent the remote
388  * processor instance. This structure acts as a prime parameter to use
389  * the remoteproc APIs.
390  *
391  * @bootadd: boot address
392  * @loader: executable loader
393  * @lock: mutext lock
394  * @ops: remoteproc operations
395  * @rsc_table: pointer to resource table
396  * @rsc_len: length of resource table
397  * @rsc_io: metal I/O region of resource table
398  * @mems: remoteproc memories
399  * @vdevs: remoteproc virtio devices
400  * @bitmap: bitmap for notify IDs for remoteproc subdevices
401  * @state: remote processor state
402  * @priv: private data
403  */
404 struct remoteproc {
405 	metal_mutex_t lock;
406 	void *rsc_table;
407 	size_t rsc_len;
408 	struct metal_io_region *rsc_io;
409 	struct metal_list mems;
410 	struct metal_list vdevs;
411 	unsigned long bitmap;
412 	struct remoteproc_ops *ops;
413 	metal_phys_addr_t bootaddr;
414 	struct loader_ops *loader;
415 	unsigned int state;
416 	void *priv;
417 };
418 
419 /**
420  * struct remoteproc_ops
421  *
422  * remoteproc operations needs to be implemented by each remoteproc driver
423  *
424  * @init: initialize the remoteproc instance
425  * @remove: remove the remoteproc instance
426  * @mmap: memory mapped the mempory with physical address or destination
427  *        address as input.
428  * @handle_rsc: handle the vendor specific resource
429  * @config: configure the remoteproc to make it ready to load and run
430  *          executable
431  * @start: kick the remoteproc to run application
432  * @stop: stop the remoteproc from running application, the resource such as
433  *        memory may not be off.
434  * @shutdown: shutdown the remoteproc and release its resources.
435  * @notify: notify the remote
436  */
437 struct remoteproc_ops {
438 	struct remoteproc *(*init)(struct remoteproc *rproc,
439 				   struct remoteproc_ops *ops, void *arg);
440 	void (*remove)(struct remoteproc *rproc);
441 	void *(*mmap)(struct remoteproc *rproc,
442 		      metal_phys_addr_t *pa, metal_phys_addr_t *da,
443 		      size_t size, unsigned int attribute,
444 		      struct metal_io_region **io);
445 	int (*handle_rsc)(struct remoteproc *rproc, void *rsc, size_t len);
446 	int (*config)(struct remoteproc *rproc, void *data);
447 	int (*start)(struct remoteproc *rproc);
448 	int (*stop)(struct remoteproc *rproc);
449 	int (*shutdown)(struct remoteproc *rproc);
450 	int (*notify)(struct remoteproc *rproc, uint32_t id);
451 };
452 
453 /* Remoteproc error codes */
454 #define RPROC_EBASE	0
455 #define RPROC_ENOMEM	(RPROC_EBASE + 1)
456 #define RPROC_EINVAL	(RPROC_EBASE + 2)
457 #define RPROC_ENODEV	(RPROC_EBASE + 3)
458 #define RPROC_EAGAIN	(RPROC_EBASE + 4)
459 #define RPROC_ERR_RSC_TAB_TRUNC (RPROC_EBASE + 5)
460 #define RPROC_ERR_RSC_TAB_VER   (RPROC_EBASE + 6)
461 #define RPROC_ERR_RSC_TAB_RSVD  (RPROC_EBASE + 7)
462 #define RPROC_ERR_RSC_TAB_VDEV_NRINGS (RPROC_EBASE + 9)
463 #define RPROC_ERR_RSC_TAB_NP          (RPROC_EBASE + 10)
464 #define RPROC_ERR_RSC_TAB_NS          (RPROC_EBASE + 11)
465 #define RPROC_ERR_LOADER_STATE (RPROC_EBASE + 12)
466 #define RPROC_EMAX	(RPROC_EBASE + 16)
467 #define RPROC_EPTR	(void *)(-1)
468 #define RPROC_EOF	(void *)(-1)
469 
RPROC_PTR_ERR(const void * ptr)470 static inline long RPROC_PTR_ERR(const void *ptr)
471 {
472 	return (long)ptr;
473 }
474 
RPROC_IS_ERR(const void * ptr)475 static inline int RPROC_IS_ERR(const void *ptr)
476 {
477 	if ((unsigned long)ptr >= (unsigned long)(-RPROC_EMAX))
478 		return 1;
479 	else
480 		return 0;
481 }
482 
RPROC_ERR_PTR(long error)483 static inline void *RPROC_ERR_PTR(long error)
484 {
485 	return (void *)error;
486 }
487 
488 /**
489  * enum rproc_state - remote processor states
490  * @RPROC_OFFLINE:	remote is offline
491  * @RPROC_READY:	remote is ready to start
492  * @RPROC_RUNNING:	remote is up and running
493  * @RPROC_SUSPENDED:	remote is suspended
494  * @RPROC_ERROR:	remote has error; need to recover
495  * @RPROC_STOPPED:	remote is stopped
496  * @RPROC_LAST:		just keep this one at the end
497  */
498 enum remoteproc_state {
499 	RPROC_OFFLINE		= 0,
500 	RPROC_CONFIGURED	= 1,
501 	RPROC_READY		= 2,
502 	RPROC_RUNNING		= 3,
503 	RPROC_SUSPENDED		= 4,
504 	RPROC_ERROR		= 5,
505 	RPROC_STOPPED		= 6,
506 	RPROC_LAST		= 7,
507 };
508 
509 /**
510  * remoteproc_init
511  *
512  * Initializes remoteproc resource.
513  *
514  * @rproc - pointer to remoteproc instance
515  * @ops - pointer to remoteproc operations
516  * @priv - pointer to private data
517  *
518  * @returns created remoteproc pointer
519  */
520 struct remoteproc *remoteproc_init(struct remoteproc *rproc,
521 				   struct remoteproc_ops *ops, void *priv);
522 
523 /**
524  * remoteproc_remove
525  *
526  * Remove remoteproc resource
527  *
528  * @rproc - pointer to remoteproc instance
529  *
530  * returns 0 for success, negative value for failure
531  */
532 int remoteproc_remove(struct remoteproc *rproc);
533 
534 /**
535  * remoteproc_init_mem
536  *
537  * Initialize remoteproc memory
538  *
539  * @mem - pointer to remoteproc memory
540  * @char - memory name
541  * @pa - physcial address
542  * @da - device address
543  * @size - memory size
544  * @io - pointer to the I/O region
545  */
546 static inline void
remoteproc_init_mem(struct remoteproc_mem * mem,const char * name,metal_phys_addr_t pa,metal_phys_addr_t da,size_t size,struct metal_io_region * io)547 remoteproc_init_mem(struct remoteproc_mem *mem, const char *name,
548 		    metal_phys_addr_t pa, metal_phys_addr_t da,
549 		    size_t size, struct metal_io_region *io)
550 {
551 	if (!mem)
552 		return;
553 	if (name)
554 		strncpy(mem->name, name, sizeof(mem->name));
555 	else
556 		mem->name[0] = 0;
557 	mem->pa = pa;
558 	mem->da = da;
559 	mem->io = io;
560 	mem->size = size;
561 }
562 
563 /**
564  * remoteproc_add_mem
565  *
566  * Add remoteproc memory
567  *
568  * @rproc - pointer to remoteproc
569  * @mem - pointer to remoteproc memory
570  */
571 static inline void
remoteproc_add_mem(struct remoteproc * rproc,struct remoteproc_mem * mem)572 remoteproc_add_mem(struct remoteproc *rproc, struct remoteproc_mem *mem)
573 {
574 	if (!rproc || !mem)
575 		return;
576 	metal_list_add_tail(&rproc->mems, &mem->node);
577 }
578 
579 /**
580  * remoteproc_get_io_with_name
581  *
582  * get remoteproc memory I/O region with name
583  *
584  * @rproc - pointer to the remote processor
585  * @name - name of the shared memory
586  * @io - pointer to the pointer of the I/O region
587  *
588  * returns metal I/O region pointer, NULL for failure
589  */
590 struct metal_io_region *
591 remoteproc_get_io_with_name(struct remoteproc *rproc,
592 			    const char *name);
593 
594 /**
595  * remoteproc_get_io_with_pa
596  *
597  * get remoteproc memory I/O region with physical address
598  *
599  * @rproc - pointer to the remote processor
600  * @pa - physical address
601  *
602  * returns metal I/O region pointer, NULL for failure
603  */
604 struct metal_io_region *
605 remoteproc_get_io_with_pa(struct remoteproc *rproc,
606 			  metal_phys_addr_t pa);
607 
608 /**
609  * remoteproc_get_io_with_da
610  *
611  * get remoteproc memory I/O region with device address
612  *
613  * @rproc - pointer to the remote processor
614  * @da - device address
615  * @offset - I/O region offset of the device address
616  *
617  * returns metal I/O region pointer, NULL for failure
618  */
619 struct metal_io_region *
620 remoteproc_get_io_with_da(struct remoteproc *rproc,
621 			  metal_phys_addr_t da,
622 			  unsigned long *offset);
623 
624 /**
625  * remoteproc_get_io_with_va
626  *
627  * get remoteproc memory I/O region with virtual address
628  *
629  * @rproc - pointer to the remote processor
630  * @va - virtual address
631  *
632  * returns metal I/O region pointer, NULL for failure
633  */
634 struct metal_io_region *
635 remoteproc_get_io_with_va(struct remoteproc *rproc,
636 			  void *va);
637 
638 /**
639  * remoteproc_mmap
640  *
641  * remoteproc mmap memory
642  *
643  * @rproc - pointer to the remote processor
644  * @pa - physical address pointer
645  * @da - device address pointer
646  * @size - size of the memory
647  * @attribute - memory attribute
648  * @io - pointer to the I/O region
649  *
650  * returns pointer to the memory
651  */
652 void *remoteproc_mmap(struct remoteproc *rproc,
653 		      metal_phys_addr_t *pa, metal_phys_addr_t *da,
654 		      size_t size, unsigned int attribute,
655 		      struct metal_io_region **io);
656 
657 /**
658  * remoteproc_parse_rsc_table
659  *
660  * Parse resource table of remoteproc
661  *
662  * @rproc - pointer to remoteproc instance
663  * @rsc_table - pointer to resource table
664  * @rsc_size - resource table size
665  *
666  * returns 0 for success and negative value for errors
667  */
668 int remoteproc_parse_rsc_table(struct remoteproc *rproc,
669 			       struct resource_table *rsc_table,
670 			       size_t rsc_size);
671 
672 /**
673  * remoteproc_set_rsc_table
674  *
675  * Parse and set resource table of remoteproc
676  *
677  * @rproc - pointer to remoteproc instance
678  * @rsc_table - pointer to resource table
679  * @rsc_size - resource table size
680  *
681  * returns 0 for success and negative value for errors
682  */
683 int remoteproc_set_rsc_table(struct remoteproc *rproc,
684 			     struct resource_table *rsc_table,
685 			     size_t rsc_size);
686 
687 /**
688  * remoteproc_config
689  *
690  * This function configures the remote processor to get it
691  * ready to load and run executable.
692  *
693  * @rproc - pointer to remoteproc instance to start
694  * @data - configuration data
695  *
696  * returns 0 for success and negative value for errors
697  */
698 int remoteproc_config(struct remoteproc *rproc, void *data);
699 
700 /**
701  * remoteproc_start
702  *
703  * This function starts the remote processor.
704  * It assumes the firmware is already loaded,
705  *
706  * @rproc - pointer to remoteproc instance to start
707  *
708  * returns 0 for success and negative value for errors
709  */
710 int remoteproc_start(struct remoteproc *rproc);
711 
712 /**
713  * remoteproc_stop
714  *
715  * This function stops the remote processor but it
716  * will not release its resource.
717  *
718  * @rproc - pointer to remoteproc instance
719  *
720  * returns 0 for success and negative value for errors
721  */
722 int remoteproc_stop(struct remoteproc *rproc);
723 
724 /**
725  * remoteproc_shutdown
726  *
727  * This function shutdown the remote processor and
728  * release its resources.
729  *
730  * @rproc - pointer to remoteproc instance
731  *
732  * returns 0 for success and negative value for errors
733  */
734 int remoteproc_shutdown(struct remoteproc *rproc);
735 
736 /**
737  * remoteproc_load
738  *
739  * load executable, it expects the user application defines how to
740  * open the executable file and how to get data from the executable file
741  * and how to load data to the target memory.
742  *
743  * @rproc: pointer to the remoteproc instance
744  * @path: optional path to the image file
745  * @store: pointer to user defined image store argument
746  * @store_ops: pointer to image store operations
747  * @image_info: pointer to memory which stores image information used
748  *              by remoteproc loader
749  *
750  * return 0 for success and negative value for failure
751  */
752 int remoteproc_load(struct remoteproc *rproc, const char *path,
753 		    void *store, struct image_store_ops *store_ops,
754 		    void **img_info);
755 
756 /**
757  * remoteproc_load_noblock
758  *
759  * load executable, it expects the caller has loaded image data to local
760  * memory and passed to the this function. If the function needs more
761  * image data it will return the next expected image data offset and
762  * the next expected image data length. If the function requires the
763  * caller to download image data to the target memory, it will also
764  * return the target physical address besides the offset and length.
765  * This function can be used to load firmware in stream mode. In this
766  * mode, you cannot do seek to the executable file. If the executable
767  * is ELF, it cannot get the resource table section before it loads
768  * the full ELF file. Furthermore, application usually don't store
769  * the data which is loaded to local memory in streaming mode, and
770  * thus, in this mode, it will load the binrary to the target memory
771  * before it gets the resource table. And thus, when calling this funciton
772  * don't put the target exectuable memory in the resource table, as
773  * this function will parse the resource table after it loads the binary
774  * to target memory.
775  *
776  * @rproc: pointer to the remoteproc instance
777  * @img_data: pointer to image data for remoteproc loader to parse
778  * @offset: image data offset to the beginning of the image file
779  * @len: image data length
780  * @image_info: pointer to memory which stores image information used
781  *              by remoteproc loader
782  * @pa: pointer to the target memory physical address. If the next expected
783  *      data doesn't need to load to the target memory, the function will
784  *      set it to ANY.
785  * @io: pointer to the target memory physical address. If the next expected
786  *      data doesn't need to load to the target memory, the function will
787  *      set it to ANY.
788  * @noffset: pointer to the next image data offset to the beginning of
789  *           the image file needs to load to local or to the target
790  *           memory.
791  * @nlen: pointer to the next image data length needs to load to local
792  *        or to the target memory.
793  * @nmlen: pointer to the memory size. It is only used when the next
794  *         expected data is going to be loaded to the target memory. E.g.
795  *         in ELF, it is possible that loadable segment in memory is
796  *         larger that the segment data in the ELF file. In this case,
797  *         application will need to pad the rest of the memory with
798  *         padding.
799  * @padding: pointer to the padding value. It is only used when the next
800  *           expected data is going to be loaded to the target memory.
801  *           and the target memory size is larger than the segment data in
802  *           the executable file.
803  *
804  * return 0 for success and negative value for failure
805  */
806 int remoteproc_load_noblock(struct remoteproc *rproc,
807 			    const void *img_data, size_t offset, size_t len,
808 			    void **img_info,
809 			    metal_phys_addr_t *pa, struct metal_io_region **io,
810 			    size_t *noffset, size_t *nlen,
811 			    size_t *nmlen, unsigned char *padding);
812 
813 /**
814  * remoteproc_allocate_id
815  *
816  * allocate notifyid for resource
817  *
818  * @rproc - pointer to the remoteproc instance
819  * @start - start of the id range
820  * @end - end of the id range
821  *
822  * return allocated notify id
823  */
824 unsigned int remoteproc_allocate_id(struct remoteproc *rproc,
825 				    unsigned int start,
826 				    unsigned int end);
827 
828 /* remoteproc_create_virtio
829  *
830  * create virtio device, it returns pointer to the created virtio device.
831  *
832  * @rproc: pointer to the remoteproc instance
833  * @vdev_id: virtio device ID
834  * @role: virtio device role
835  * @rst_cb: virtio device reset callback
836  *
837  * return pointer to the created virtio device, NULL for failure.
838  */
839 struct virtio_device *
840 remoteproc_create_virtio(struct remoteproc *rproc,
841 			 int vdev_id, unsigned int role,
842 			 void (*rst_cb)(struct virtio_device *vdev));
843 
844 /* remoteproc_remove_virtio
845  *
846  * Remove virtio device
847  *
848  * @rproc: pointer to the remoteproc instance
849  * @vdev: pointer to the virtio device
850  *
851  */
852 void remoteproc_remove_virtio(struct remoteproc *rproc,
853 			      struct virtio_device *vdev);
854 
855 /* remoteproc_get_notification
856  *
857  * remoteproc is got notified, it will check its subdevices
858  * for the notification
859  *
860  * @rproc -  pointer to the remoteproc instance
861  * @notifyid - notificatin id
862  *
863  * return 0 for succeed, negative value for failure
864  */
865 int remoteproc_get_notification(struct remoteproc *rproc,
866 				uint32_t notifyid);
867 #if defined __cplusplus
868 }
869 #endif
870 
871 #endif /* REMOTEPROC_H_ */
872