1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Texas Instruments Incorporated - https://www.ti.com/
11 * Note: The platform data support is not meant for use with newer
12 * platforms. This is meant only for legacy devices. This mode of
13 * initialization *will* be eventually removed once all necessary
14 * platforms have moved to dm/fdt.
16 #include <dm/platdata.h> /* For platform data support - non dt world */
17 #include <linux/errno.h>
20 * struct fw_rsc_hdr - firmware resource entry header
21 * @type: resource type
22 * @data: resource data
24 * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
25 * its @type. The content of the entry itself will immediately follow
26 * this header, and it should be parsed according to the resource type.
34 * enum fw_resource_type - types of resource entries
36 * @RSC_CARVEOUT: request for allocation of a physically contiguous
38 * @RSC_DEVMEM: request to iommu_map a memory-based peripheral.
39 * @RSC_TRACE: announces the availability of a trace buffer into which
40 * the remote processor will be writing logs.
41 * @RSC_VDEV: declare support for a virtio device, and serve as its
43 * @RSC_PRELOAD_VENDOR: a vendor resource type that needs to be handled by
44 * remoteproc implementations before loading
45 * @RSC_POSTLOAD_VENDOR: a vendor resource type that needs to be handled by
46 * remoteproc implementations after loading
47 * @RSC_LAST: just keep this one at the end
49 * For more details regarding a specific resource type, please see its
50 * dedicated structure below.
52 * Please note that these values are used as indices to the rproc_handle_rsc
53 * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
54 * check the validity of an index before the lookup table is accessed, so
55 * please update it as needed.
57 enum fw_resource_type
{
62 RSC_PRELOAD_VENDOR
= 4,
63 RSC_POSTLOAD_VENDOR
= 5,
67 #define FW_RSC_ADDR_ANY (-1)
70 * struct fw_rsc_carveout - physically contiguous memory request
72 * @pa: physical address
73 * @len: length (in bytes)
74 * @flags: iommu protection flags
75 * @reserved: reserved (must be zero)
76 * @name: human-readable name of the requested memory region
78 * This resource entry requests the host to allocate a physically contiguous
81 * These request entries should precede other firmware resource entries,
82 * as other entries might request placing other data objects inside
83 * these memory regions (e.g. data/code segments, trace resource entries, ...).
85 * Allocating memory this way helps utilizing the reserved physical memory
86 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
87 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
88 * pressure is important; it may have a substantial impact on performance.
90 * If the firmware is compiled with static addresses, then @da should specify
91 * the expected device address of this memory region. If @da is set to
92 * FW_RSC_ADDR_ANY, then the host will dynamically allocate it, and then
93 * overwrite @da with the dynamically allocated address.
95 * We will always use @da to negotiate the device addresses, even if it
96 * isn't using an iommu. In that case, though, it will obviously contain
99 * Some remote processors needs to know the allocated physical address
100 * even if they do use an iommu. This is needed, e.g., if they control
101 * hardware accelerators which access the physical memory directly (this
102 * is the case with OMAP4 for instance). In that case, the host will
103 * overwrite @pa with the dynamically allocated physical address.
104 * Generally we don't want to expose physical addresses if we don't have to
105 * (remote processors are generally _not_ trusted), so we might want to
106 * change this to happen _only_ when explicitly required by the hardware.
108 * @flags is used to provide IOMMU protection flags, and @name should
109 * (optionally) contain a human readable name of this carveout region
110 * (mainly for debugging purposes).
112 struct fw_rsc_carveout
{
122 * struct fw_rsc_devmem - iommu mapping request
123 * @da: device address
124 * @pa: physical address
125 * @len: length (in bytes)
126 * @flags: iommu protection flags
127 * @reserved: reserved (must be zero)
128 * @name: human-readable name of the requested region to be mapped
130 * This resource entry requests the host to iommu map a physically contiguous
131 * memory region. This is needed in case the remote processor requires
132 * access to certain memory-based peripherals; _never_ use it to access
135 * This is obviously only needed if the remote processor is accessing memory
138 * @da should specify the required device address, @pa should specify
139 * the physical address we want to map, @len should specify the size of
140 * the mapping and @flags is the IOMMU protection flags. As always, @name may
141 * (optionally) contain a human readable name of this mapping (mainly for
142 * debugging purposes).
144 * Note: at this point we just "trust" those devmem entries to contain valid
145 * physical addresses, but this isn't safe and will be changed: eventually we
146 * want remoteproc implementations to provide us ranges of physical addresses
147 * the firmware is allowed to request, and not allow firmwares to request
148 * access to physical addresses that are outside those ranges.
150 struct fw_rsc_devmem
{
160 * struct fw_rsc_trace - trace buffer declaration
161 * @da: device address
162 * @len: length (in bytes)
163 * @reserved: reserved (must be zero)
164 * @name: human-readable name of the trace buffer
166 * This resource entry provides the host information about a trace buffer
167 * into which the remote processor will write log messages.
169 * @da specifies the device address of the buffer, @len specifies
170 * its size, and @name may contain a human readable name of the trace buffer.
172 * After booting the remote processor, the trace buffers are exposed to the
173 * user via debugfs entries (called trace0, trace1, etc..).
175 struct fw_rsc_trace
{
183 * struct fw_rsc_vdev_vring - vring descriptor entry
184 * @da: device address
185 * @align: the alignment between the consumer and producer parts of the vring
186 * @num: num of buffers supported by this vring (must be power of two)
187 * @notifyid is a unique rproc-wide notify index for this vring. This notify
188 * index is used when kicking a remote processor, to let it know that this
189 * vring is triggered.
190 * @pa: physical address
192 * This descriptor is not a resource entry by itself; it is part of the
193 * vdev resource type (see below).
195 * Note that @da should either contain the device address where
196 * the remote processor is expecting the vring, or indicate that
197 * dynamically allocation of the vring's device address is supported.
199 struct fw_rsc_vdev_vring
{
208 * struct fw_rsc_vdev - virtio device header
209 * @id: virtio device id (as in virtio_ids.h)
210 * @notifyid is a unique rproc-wide notify index for this vdev. This notify
211 * index is used when kicking a remote processor, to let it know that the
212 * status/features of this vdev have changes.
213 * @dfeatures specifies the virtio device features supported by the firmware
214 * @gfeatures is a place holder used by the host to write back the
215 * negotiated features that are supported by both sides.
216 * @config_len is the size of the virtio config space of this vdev. The config
217 * space lies in the resource table immediate after this vdev header.
218 * @status is a place holder where the host will indicate its virtio progress.
219 * @num_of_vrings indicates how many vrings are described in this vdev header
220 * @reserved: reserved (must be zero)
221 * @vring is an array of @num_of_vrings entries of 'struct fw_rsc_vdev_vring'.
223 * This resource is a virtio device header: it provides information about
224 * the vdev, and is then used by the host and its peer remote processors
225 * to negotiate and share certain virtio properties.
227 * By providing this resource entry, the firmware essentially asks remoteproc
228 * to statically allocate a vdev upon registration of the rproc (dynamic vdev
229 * allocation is not yet supported).
231 * Note: unlike virtualization systems, the term 'host' here means
232 * the Linux side which is running remoteproc to control the remote
233 * processors. We use the name 'gfeatures' to comply with virtio's terms,
234 * though there isn't really any virtualized guest OS here: it's the host
235 * which is responsible for negotiating the final features.
236 * Yeah, it's a bit confusing.
238 * Note: immediately following this structure is the virtio config space for
239 * this vdev (which is specific to the vdev; for more info, read the virtio
240 * spec). the size of the config space is specified by @config_len.
251 struct fw_rsc_vdev_vring vring
[0];
255 * struct rproc_mem_entry - memory entry descriptor
256 * @va: virtual address
258 * @len: length, in bytes
259 * @da: device address
260 * @priv: associated data
261 * @name: associated memory region name (optional)
264 struct rproc_mem_entry
{
271 struct list_head node
;
276 typedef u32(*init_func_proto
) (u32 core_id
, struct rproc
*cfg
);
284 struct rproc_intmem_to_l3_mapping
{
286 struct l3_map mappings
[16];
290 * enum rproc_crash_type - remote processor crash types
291 * @RPROC_MMUFAULT: iommu fault
292 * @RPROC_WATCHDOG: watchdog bite
293 * @RPROC_FATAL_ERROR fatal error
295 * Each element of the enum is used as an array index. So that, the value of
296 * the elements should be always something sane.
298 * Feel free to add more types when needed.
300 enum rproc_crash_type
{
306 /* we currently support only two vrings per rvdev */
307 #define RVDEV_NUM_VRINGS 2
309 #define RPMSG_NUM_BUFS (512)
310 #define RPMSG_BUF_SIZE (512)
311 #define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE)
314 * struct rproc_vring - remoteproc vring state
315 * @va: virtual address
317 * @len: length, in bytes
318 * @da: device address
319 * @align: vring alignment
320 * @notifyid: rproc-specific unique vring index
321 * @rvdev: remote vdev
322 * @vq: the virtqueue of this vring
331 struct rproc_vdev
*rvdev
;
332 struct virtqueue
*vq
;
335 /** struct rproc - structure with all processor specific information for
336 * loading remotecore from boot loader.
338 * @num_iommus: Number of IOMMUs for this remote core. Zero indicates that the
339 * processor does not have an IOMMU.
341 * @cma_base: Base address of the carveout for this remotecore.
343 * @cma_size: Length of the carveout in bytes.
345 * @page_table_addr: array with the physical address of the page table. We are
346 * using the same page table for both IOMMU's. There is currently no strong
347 * usecase for maintaining different page tables for different MMU's servicing
350 * @mmu_base_addr: base address of the MMU
352 * @entry_point: address that is the entry point for the remote core. This
353 * address is in the memory view of the remotecore.
355 * @load_addr: Address to which the bootloader loads the firmware from
356 * persistent storage before invoking the ELF loader. Keeping this address
357 * configurable allows future optimizations such as loading the firmware from
358 * storage for remotecore2 via EDMA while the CPU is processing the ELF image
359 * of remotecore1. This address is in the memory view of the A15.
361 * @firmware_name: Name of the file that is expected to contain the ELF image.
363 * @has_rsc_table: Flag populated after parsing the ELF binary on target.
368 unsigned long cma_base
;
370 unsigned long page_table_addr
;
371 unsigned long mmu_base_addr
[2];
372 unsigned long load_addr
;
373 unsigned long entry_point
;
377 init_func_proto start_clocks
;
378 init_func_proto config_mmu
;
379 init_func_proto config_peripherals
;
380 init_func_proto start_core
;
382 struct rproc_intmem_to_l3_mapping
*intmem_to_l3_mapping
;
387 extern struct rproc
*rproc_cfg_arr
[2];
389 * enum rproc_mem_type - What type of memory model does the rproc use
390 * @RPROC_INTERNAL_MEMORY_MAPPED: Remote processor uses own memory and is memory
391 * mapped to the host processor over an address range.
393 * Please note that this is an enumeration of memory model of different types
394 * of remote processors. Few of the remote processors do have own internal
395 * memories, while others use external memory for instruction and data.
397 enum rproc_mem_type
{
398 RPROC_INTERNAL_MEMORY_MAPPED
= 0,
402 * struct dm_rproc_uclass_pdata - platform data for a CPU
403 * @name: Platform-specific way of naming the Remote proc
404 * @mem_type: one of 'enum rproc_mem_type'
405 * @driver_plat_data: driver specific platform data that may be needed.
406 * @fw_name: firmware name
408 * This can be accessed with dev_get_uclass_plat() for any UCLASS_REMOTEPROC
412 struct dm_rproc_uclass_pdata
{
414 enum rproc_mem_type mem_type
;
415 void *driver_plat_data
;
420 * struct dm_rproc_ops - Driver model remote proc operations.
422 * This defines the operations provided by remote proc driver.
424 struct dm_rproc_ops
{
426 * init() - Initialize the remoteproc device (optional)
428 * This is called after the probe is completed allowing the remote
429 * processor drivers to split up the initializations between probe and
432 * @dev: Remote proc device
433 * @return 0 if all ok, else appropriate error value.
435 int (*init
)(struct udevice
*dev
);
438 * load() - Load the remoteproc device using data provided (mandatory)
440 * Load the remoteproc device with an image, do not start the device.
442 * @dev: Remote proc device
443 * @addr: Address of the image to be loaded
444 * @size: Size of the image to be loaded
445 * @return 0 if all ok, else appropriate error value.
447 int (*load
)(struct udevice
*dev
, ulong addr
, ulong size
);
450 * start() - Start the remoteproc device (mandatory)
452 * @dev: Remote proc device
453 * @return 0 if all ok, else appropriate error value.
455 int (*start
)(struct udevice
*dev
);
458 * stop() - Stop the remoteproc device (optional)
460 * @dev: Remote proc device
461 * @return 0 if all ok, else appropriate error value.
463 int (*stop
)(struct udevice
*dev
);
466 * reset() - Reset the remoteproc device (optional)
468 * @dev: Remote proc device
469 * @return 0 if all ok, else appropriate error value.
471 int (*reset
)(struct udevice
*dev
);
474 * is_running() - Check if the remote processor is running (optional)
476 * @dev: Remote proc device
477 * @return 0 if running, 1 if not running, -ve on error.
479 int (*is_running
)(struct udevice
*dev
);
482 * ping() - Ping the remote device for basic communication (optional)
484 * @dev: Remote proc device
485 * @return 0 on success, 1 if not responding, -ve on other errors.
487 int (*ping
)(struct udevice
*dev
);
490 * device_to_virt() - Return translated virtual address (optional)
492 * Translate a device address (remote processor view) to virtual
493 * address (main processor view).
495 * @dev: Remote proc device
496 * @da: Device address
497 * @size: Size of the memory region @da is pointing to
498 * @return virtual address.
500 void * (*device_to_virt
)(struct udevice
*dev
, ulong da
, ulong size
);
501 int (*add_res
)(struct udevice
*dev
,
502 struct rproc_mem_entry
*mapping
);
503 void * (*alloc_mem
)(struct udevice
*dev
, unsigned long len
,
504 unsigned long align
);
505 unsigned int (*config_pagetable
)(struct udevice
*dev
, unsigned int virt
,
506 unsigned int phys
, unsigned int len
);
510 #define rproc_get_ops(dev) ((struct dm_rproc_ops *)(dev)->driver->ops)
512 #if CONFIG_IS_ENABLED(REMOTEPROC)
514 * rproc_init() - Initialize all bound remote proc devices
515 * Return: 0 if all ok, else appropriate error value.
517 int rproc_init(void);
520 * rproc_dev_init() - Initialize a remote proc device based on id
521 * @id: id of the remote processor
522 * Return: 0 if all ok, else appropriate error value.
524 int rproc_dev_init(int id
);
527 * rproc_is_initialized() - check to see if remoteproc devices are initialized
528 * Return: true if all devices are initialized, false otherwise.
530 bool rproc_is_initialized(void);
533 * rproc_load() - load binary or elf to a remote processor
534 * @id: id of the remote processor
535 * @addr: address in memory where the image is located
536 * @size: size of the image
537 * Return: 0 if all ok, else appropriate error value.
539 int rproc_load(int id
, ulong addr
, ulong size
);
542 * rproc_start() - Start a remote processor
543 * @id: id of the remote processor
544 * Return: 0 if all ok, else appropriate error value.
546 int rproc_start(int id
);
549 * rproc_stop() - Stop a remote processor
550 * @id: id of the remote processor
551 * Return: 0 if all ok, else appropriate error value.
553 int rproc_stop(int id
);
556 * rproc_reset() - reset a remote processor
557 * @id: id of the remote processor
558 * Return: 0 if all ok, else appropriate error value.
560 int rproc_reset(int id
);
563 * rproc_ping() - ping a remote processor to check if it can communicate
564 * @id: id of the remote processor
565 * Return: 0 if all ok, else appropriate error value.
567 * NOTE: this might need communication path available, which is not implemented
568 * as part of remoteproc framework - hook on to appropriate bus architecture to
571 int rproc_ping(int id
);
574 * rproc_is_running() - check to see if remote processor is running
575 * @id: id of the remote processor
576 * Return: 0 if running, 1 if not running, -ve on error.
578 * NOTE: this may not involve actual communication capability of the remote
579 * processor, but just ensures that it is out of reset and executing code.
581 int rproc_is_running(int id
);
584 * rproc_elf32_sanity_check() - Verify if an image is a valid ELF32 one
586 * Check if a valid ELF32 image exists at the given memory location. Verify
587 * basic ELF32 format requirements like magic number and sections size.
589 * @addr: address of the image to verify
590 * @size: size of the image
591 * Return: 0 if the image looks good, else appropriate error value.
593 int rproc_elf32_sanity_check(ulong addr
, ulong size
);
596 * rproc_elf64_sanity_check() - Verify if an image is a valid ELF32 one
598 * Check if a valid ELF64 image exists at the given memory location. Verify
599 * basic ELF64 format requirements like magic number and sections size.
601 * @addr: address of the image to verify
602 * @size: size of the image
603 * Return: 0 if the image looks good, else appropriate error value.
605 int rproc_elf64_sanity_check(ulong addr
, ulong size
);
608 * rproc_elf32_load_image() - load an ELF32 image
609 * @dev: device loading the ELF32 image
610 * @addr: valid ELF32 image address
611 * @size: size of the image
612 * Return: 0 if the image is successfully loaded, else appropriate error value.
614 int rproc_elf32_load_image(struct udevice
*dev
, unsigned long addr
, ulong size
);
617 * rproc_elf64_load_image() - load an ELF64 image
618 * @dev: device loading the ELF64 image
619 * @addr: valid ELF64 image address
620 * @size: size of the image
621 * Return: 0 if the image is successfully loaded, else appropriate error value.
623 int rproc_elf64_load_image(struct udevice
*dev
, ulong addr
, ulong size
);
626 * rproc_elf_load_image() - load an ELF image
627 * @dev: device loading the ELF image
628 * @addr: valid ELF image address
629 * @size: size of the image
631 * Auto detects if the image is ELF32 or ELF64 image and load accordingly.
632 * Return: 0 if the image is successfully loaded, else appropriate error value.
634 int rproc_elf_load_image(struct udevice
*dev
, unsigned long addr
, ulong size
);
637 * rproc_elf_get_boot_addr() - Get rproc's boot address.
638 * @dev: device loading the ELF image
639 * @addr: valid ELF image address
641 * This function returns the entry point address of the ELF
644 ulong
rproc_elf_get_boot_addr(struct udevice
*dev
, ulong addr
);
647 * rproc_elf32_load_rsc_table() - load the resource table from an ELF32 image
649 * Search for the resource table in an ELF32 image, and if found, copy it to
652 * @dev: device loading the resource table
653 * @fw_addr: ELF image address
654 * @fw_size: size of the ELF image
655 * @rsc_addr: pointer to the found resource table address. Updated on
657 * @rsc_size: pointer to the found resource table size. Updated on operation
660 * Return: 0 if a valid resource table is successfully loaded, -ENODATA if there
661 * is no resource table (which is optional), or another appropriate error value.
663 int rproc_elf32_load_rsc_table(struct udevice
*dev
, ulong fw_addr
,
664 ulong fw_size
, ulong
*rsc_addr
, ulong
*rsc_size
);
666 * rproc_elf64_load_rsc_table() - load the resource table from an ELF64 image
668 * Search for the resource table in an ELF64 image, and if found, copy it to
671 * @dev: device loading the resource table
672 * @fw_addr: ELF image address
673 * @fw_size: size of the ELF image
674 * @rsc_addr: pointer to the found resource table address. Updated on
676 * @rsc_size: pointer to the found resource table size. Updated on operation
679 * Return: 0 if a valid resource table is successfully loaded, -ENODATA if there
680 * is no resource table (which is optional), or another appropriate error value.
682 int rproc_elf64_load_rsc_table(struct udevice
*dev
, ulong fw_addr
,
683 ulong fw_size
, ulong
*rsc_addr
, ulong
*rsc_size
);
685 * rproc_elf_load_rsc_table() - load the resource table from an ELF image
687 * Auto detects if the image is ELF32 or ELF64 image and search accordingly for
688 * the resource table, and if found, copy it to device memory.
690 * @dev: device loading the resource table
691 * @fw_addr: ELF image address
692 * @fw_size: size of the ELF image
693 * @rsc_addr: pointer to the found resource table address. Updated on
695 * @rsc_size: pointer to the found resource table size. Updated on operation
698 * Return: 0 if a valid resource table is successfully loaded, -ENODATA if there
699 * is no resource table (which is optional), or another appropriate error value.
701 int rproc_elf_load_rsc_table(struct udevice
*dev
, ulong fw_addr
,
702 ulong fw_size
, ulong
*rsc_addr
, ulong
*rsc_size
);
704 unsigned long rproc_parse_resource_table(struct udevice
*dev
,
707 struct resource_table
*rproc_find_resource_table(struct udevice
*dev
,
711 * rproc_set_firmware() - assign a new firmware name
712 * @rproc_dev: device for which new firmware name is being assigned
713 * @fw_name: new firmware name to be assigned
715 * This function allows remoteproc drivers or clients to configure a custom
716 * firmware name. The function does not trigger a remote processor boot,
717 * only sets the firmware name used for a subsequent boot.
719 * This function sets the fw_name field in uclass pdata of the Remote proc
721 * Return: 0 on success or a negative value upon failure
723 int rproc_set_firmware(struct udevice
*rproc_dev
, const char *fw_name
);
726 * rproc_boot() - boot a remote processor
727 * @rproc_dev: rproc device to boot
729 * Boot a remote processor (i.e. load its firmware, power it on, ...).
731 * This function first loads the firmware set in the uclass pdata of Remote
732 * processor to a buffer and then loads firmware to the remote processor
733 * using rproc_load().
735 * Return: 0 on success, and an appropriate error value otherwise
737 int rproc_boot(struct udevice
*rproc_dev
);
739 static inline int rproc_init(void) { return -ENOSYS
; }
740 static inline int rproc_dev_init(int id
) { return -ENOSYS
; }
741 static inline bool rproc_is_initialized(void) { return false; }
742 static inline int rproc_load(int id
, ulong addr
, ulong size
) { return -ENOSYS
; }
743 static inline int rproc_start(int id
) { return -ENOSYS
; }
744 static inline int rproc_stop(int id
) { return -ENOSYS
; }
745 static inline int rproc_reset(int id
) { return -ENOSYS
; }
746 static inline int rproc_ping(int id
) { return -ENOSYS
; }
747 static inline int rproc_is_running(int id
) { return -ENOSYS
; }
748 static inline int rproc_elf32_sanity_check(ulong addr
,
749 ulong size
) { return -ENOSYS
; }
750 static inline int rproc_elf64_sanity_check(ulong addr
,
751 ulong size
) { return -ENOSYS
; }
752 static inline int rproc_elf_sanity_check(ulong addr
,
753 ulong size
) { return -ENOSYS
; }
754 static inline int rproc_elf32_load_image(struct udevice
*dev
,
755 unsigned long addr
, ulong size
)
757 static inline int rproc_elf64_load_image(struct udevice
*dev
, ulong addr
,
760 static inline int rproc_elf_load_image(struct udevice
*dev
, ulong addr
,
763 static inline ulong
rproc_elf_get_boot_addr(struct udevice
*dev
, ulong addr
)
765 static inline int rproc_elf32_load_rsc_table(struct udevice
*dev
, ulong fw_addr
,
766 ulong fw_size
, ulong
*rsc_addr
,
769 static inline int rproc_elf64_load_rsc_table(struct udevice
*dev
, ulong fw_addr
,
770 ulong fw_size
, ulong
*rsc_addr
,
773 static inline int rproc_elf_load_rsc_table(struct udevice
*dev
, ulong fw_addr
,
774 ulong fw_size
, ulong
*rsc_addr
,
777 static inline int rproc_set_firmware(struct udevice
*rproc_dev
, const char *fw_name
)
779 static inline int rproc_boot(struct udevice
*rproc_dev
)
783 #endif /* _RPROC_H_ */