361 lines · plain
1==========================2Remote Processor Framework3==========================4 5Introduction6============7 8Modern SoCs typically have heterogeneous remote processor devices in asymmetric9multiprocessing (AMP) configurations, which may be running different instances10of operating system, whether it's Linux or any other flavor of real-time OS.11 12OMAP4, for example, has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP.13In a typical configuration, the dual cortex-A9 is running Linux in a SMP14configuration, and each of the other three cores (two M3 cores and a DSP)15is running its own instance of RTOS in an AMP configuration.16 17The remoteproc framework allows different platforms/architectures to18control (power on, load firmware, power off) those remote processors while19abstracting the hardware differences, so the entire driver doesn't need to be20duplicated. In addition, this framework also adds rpmsg virtio devices21for remote processors that supports this kind of communication. This way,22platform-specific remoteproc drivers only need to provide a few low-level23handlers, and then all rpmsg drivers will then just work24(for more information about the virtio-based rpmsg bus and its drivers,25please read Documentation/staging/rpmsg.rst).26Registration of other types of virtio devices is now also possible. Firmwares27just need to publish what kind of virtio devices do they support, and then28remoteproc will add those devices. This makes it possible to reuse the29existing virtio drivers with remote processor backends at a minimal development30cost.31 32User API33========34 35::36 37 int rproc_boot(struct rproc *rproc)38 39Boot a remote processor (i.e. load its firmware, power it on, ...).40 41If the remote processor is already powered on, this function immediately42returns (successfully).43 44Returns 0 on success, and an appropriate error value otherwise.45Note: to use this function you should already have a valid rproc46handle. There are several ways to achieve that cleanly (devres, pdata,47the way remoteproc_rpmsg.c does this, or, if this becomes prevalent, we48might also consider using dev_archdata for this).49 50::51 52 int rproc_shutdown(struct rproc *rproc)53 54Power off a remote processor (previously booted with rproc_boot()).55In case @rproc is still being used by an additional user(s), then56this function will just decrement the power refcount and exit,57without really powering off the device.58 59Returns 0 on success, and an appropriate error value otherwise.60Every call to rproc_boot() must (eventually) be accompanied by a call61to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.62 63.. note::64 65 we're not decrementing the rproc's refcount, only the power refcount.66 which means that the @rproc handle stays valid even after67 rproc_shutdown() returns, and users can still use it with a subsequent68 rproc_boot(), if needed.69 70::71 72 struct rproc *rproc_get_by_phandle(phandle phandle)73 74Find an rproc handle using a device tree phandle. Returns the rproc75handle on success, and NULL on failure. This function increments76the remote processor's refcount, so always use rproc_put() to77decrement it back once rproc isn't needed anymore.78 79Typical usage80=============81 82::83 84 #include <linux/remoteproc.h>85 86 /* in case we were given a valid 'rproc' handle */87 int dummy_rproc_example(struct rproc *my_rproc)88 {89 int ret;90 91 /* let's power on and boot our remote processor */92 ret = rproc_boot(my_rproc);93 if (ret) {94 /*95 * something went wrong. handle it and leave.96 */97 }98 99 /*100 * our remote processor is now powered on... give it some work101 */102 103 /* let's shut it down now */104 rproc_shutdown(my_rproc);105 }106 107API for implementors108====================109 110::111 112 struct rproc *rproc_alloc(struct device *dev, const char *name,113 const struct rproc_ops *ops,114 const char *firmware, int len)115 116Allocate a new remote processor handle, but don't register117it yet. Required parameters are the underlying device, the118name of this remote processor, platform-specific ops handlers,119the name of the firmware to boot this rproc with, and the120length of private data needed by the allocating rproc driver (in bytes).121 122This function should be used by rproc implementations during123initialization of the remote processor.124 125After creating an rproc handle using this function, and when ready,126implementations should then call rproc_add() to complete127the registration of the remote processor.128 129On success, the new rproc is returned, and on failure, NULL.130 131.. note::132 133 **never** directly deallocate @rproc, even if it was not registered134 yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().135 136::137 138 void rproc_free(struct rproc *rproc)139 140Free an rproc handle that was allocated by rproc_alloc.141 142This function essentially unrolls rproc_alloc(), by decrementing the143rproc's refcount. It doesn't directly free rproc; that would happen144only if there are no other references to rproc and its refcount now145dropped to zero.146 147::148 149 int rproc_add(struct rproc *rproc)150 151Register @rproc with the remoteproc framework, after it has been152allocated with rproc_alloc().153 154This is called by the platform-specific rproc implementation, whenever155a new remote processor device is probed.156 157Returns 0 on success and an appropriate error code otherwise.158Note: this function initiates an asynchronous firmware loading159context, which will look for virtio devices supported by the rproc's160firmware.161 162If found, those virtio devices will be created and added, so as a result163of registering this remote processor, additional virtio drivers might get164probed.165 166::167 168 int rproc_del(struct rproc *rproc)169 170Unroll rproc_add().171 172This function should be called when the platform specific rproc173implementation decides to remove the rproc device. it should174_only_ be called if a previous invocation of rproc_add()175has completed successfully.176 177After rproc_del() returns, @rproc is still valid, and its178last refcount should be decremented by calling rproc_free().179 180Returns 0 on success and -EINVAL if @rproc isn't valid.181 182::183 184 void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)185 186Report a crash in a remoteproc187 188This function must be called every time a crash is detected by the189platform specific rproc implementation. This should not be called from a190non-remoteproc driver. This function can be called from atomic/interrupt191context.192 193Implementation callbacks194========================195 196These callbacks should be provided by platform-specific remoteproc197drivers::198 199 /**200 * struct rproc_ops - platform-specific device handlers201 * @start: power on the device and boot it202 * @stop: power off the device203 * @kick: kick a virtqueue (virtqueue id given as a parameter)204 */205 struct rproc_ops {206 int (*start)(struct rproc *rproc);207 int (*stop)(struct rproc *rproc);208 void (*kick)(struct rproc *rproc, int vqid);209 };210 211Every remoteproc implementation should at least provide the ->start and ->stop212handlers. If rpmsg/virtio functionality is also desired, then the ->kick handler213should be provided as well.214 215The ->start() handler takes an rproc handle and should then power on the216device and boot it (use rproc->priv to access platform-specific private data).217The boot address, in case needed, can be found in rproc->bootaddr (remoteproc218core puts there the ELF entry point).219On success, 0 should be returned, and on failure, an appropriate error code.220 221The ->stop() handler takes an rproc handle and powers the device down.222On success, 0 is returned, and on failure, an appropriate error code.223 224The ->kick() handler takes an rproc handle, and an index of a virtqueue225where new message was placed in. Implementations should interrupt the remote226processor and let it know it has pending messages. Notifying remote processors227the exact virtqueue index to look in is optional: it is easy (and not228too expensive) to go through the existing virtqueues and look for new buffers229in the used rings.230 231Binary Firmware Structure232=========================233 234At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,235it is quite expected that other platforms/devices which we'd want to236support with this framework will be based on different binary formats.237 238When those use cases show up, we will have to decouple the binary format239from the framework core, so we can support several binary formats without240duplicating common code.241 242When the firmware is parsed, its various segments are loaded to memory243according to the specified device address (might be a physical address244if the remote processor is accessing memory directly).245 246In addition to the standard ELF segments, most remote processors would247also include a special section which we call "the resource table".248 249The resource table contains system resources that the remote processor250requires before it should be powered on, such as allocation of physically251contiguous memory, or iommu mapping of certain on-chip peripherals.252Remotecore will only power up the device after all the resource table's253requirement are met.254 255In addition to system resources, the resource table may also contain256resource entries that publish the existence of supported features257or configurations by the remote processor, such as trace buffers and258supported virtio devices (and their configurations).259 260The resource table begins with this header::261 262 /**263 * struct resource_table - firmware resource table header264 * @ver: version number265 * @num: number of resource entries266 * @reserved: reserved (must be zero)267 * @offset: array of offsets pointing at the various resource entries268 *269 * The header of the resource table, as expressed by this structure,270 * contains a version number (should we need to change this format in the271 * future), the number of available resource entries, and their offsets272 * in the table.273 */274 struct resource_table {275 u32 ver;276 u32 num;277 u32 reserved[2];278 u32 offset[0];279 } __packed;280 281Immediately following this header are the resource entries themselves,282each of which begins with the following resource entry header::283 284 /**285 * struct fw_rsc_hdr - firmware resource entry header286 * @type: resource type287 * @data: resource data288 *289 * Every resource entry begins with a 'struct fw_rsc_hdr' header providing290 * its @type. The content of the entry itself will immediately follow291 * this header, and it should be parsed according to the resource type.292 */293 struct fw_rsc_hdr {294 u32 type;295 u8 data[0];296 } __packed;297 298Some resources entries are mere announcements, where the host is informed299of specific remoteproc configuration. Other entries require the host to300do something (e.g. allocate a system resource). Sometimes a negotiation301is expected, where the firmware requests a resource, and once allocated,302the host should provide back its details (e.g. address of an allocated303memory region).304 305Here are the various resource types that are currently supported::306 307 /**308 * enum fw_resource_type - types of resource entries309 *310 * @RSC_CARVEOUT: request for allocation of a physically contiguous311 * memory region.312 * @RSC_DEVMEM: request to iommu_map a memory-based peripheral.313 * @RSC_TRACE: announces the availability of a trace buffer into which314 * the remote processor will be writing logs.315 * @RSC_VDEV: declare support for a virtio device, and serve as its316 * virtio header.317 * @RSC_LAST: just keep this one at the end318 * @RSC_VENDOR_START: start of the vendor specific resource types range319 * @RSC_VENDOR_END: end of the vendor specific resource types range320 *321 * Please note that these values are used as indices to the rproc_handle_rsc322 * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to323 * check the validity of an index before the lookup table is accessed, so324 * please update it as needed.325 */326 enum fw_resource_type {327 RSC_CARVEOUT = 0,328 RSC_DEVMEM = 1,329 RSC_TRACE = 2,330 RSC_VDEV = 3,331 RSC_LAST = 4,332 RSC_VENDOR_START = 128,333 RSC_VENDOR_END = 512,334 };335 336For more details regarding a specific resource type, please see its337dedicated structure in include/linux/remoteproc.h.338 339We also expect that platform-specific resource entries will show up340at some point. When that happens, we could easily add a new RSC_PLATFORM341type, and hand those resources to the platform-specific rproc driver to handle.342 343Virtio and remoteproc344=====================345 346The firmware should provide remoteproc information about virtio devices347that it supports, and their configurations: a RSC_VDEV resource entry348should specify the virtio device id (as in virtio_ids.h), virtio features,349virtio config space, vrings information, etc.350 351When a new remote processor is registered, the remoteproc framework352will look for its resource table and will register the virtio devices353it supports. A firmware may support any number of virtio devices, and354of any type (a single remote processor can also easily support several355rpmsg virtio devices this way, if desired).356 357Of course, RSC_VDEV resource entries are only good enough for static358allocation of virtio devices. Dynamic allocations will also be made possible359using the rpmsg bus (similar to how we already do dynamic allocations of360rpmsg channels; read more about it in rpmsg.txt).361