remoteproc.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. ==========================
  2. Remote Processor Framework
  3. ==========================
  4. Introduction
  5. ============
  6. Modern SoCs typically have heterogeneous remote processor devices in asymmetric
  7. multiprocessing (AMP) configurations, which may be running different instances
  8. of operating system, whether it's Linux or any other flavor of real-time OS.
  9. OMAP4, for example, has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP.
  10. In a typical configuration, the dual cortex-A9 is running Linux in a SMP
  11. configuration, and each of the other three cores (two M3 cores and a DSP)
  12. is running its own instance of RTOS in an AMP configuration.
  13. The remoteproc framework allows different platforms/architectures to
  14. control (power on, load firmware, power off) those remote processors while
  15. abstracting the hardware differences, so the entire driver doesn't need to be
  16. duplicated. In addition, this framework also adds rpmsg virtio devices
  17. for remote processors that supports this kind of communication. This way,
  18. platform-specific remoteproc drivers only need to provide a few low-level
  19. handlers, and then all rpmsg drivers will then just work
  20. (for more information about the virtio-based rpmsg bus and its drivers,
  21. please read Documentation/staging/rpmsg.rst).
  22. Registration of other types of virtio devices is now also possible. Firmwares
  23. just need to publish what kind of virtio devices do they support, and then
  24. remoteproc will add those devices. This makes it possible to reuse the
  25. existing virtio drivers with remote processor backends at a minimal development
  26. cost.
  27. User API
  28. ========
  29. ::
  30. int rproc_boot(struct rproc *rproc)
  31. Boot a remote processor (i.e. load its firmware, power it on, ...).
  32. If the remote processor is already powered on, this function immediately
  33. returns (successfully).
  34. Returns 0 on success, and an appropriate error value otherwise.
  35. Note: to use this function you should already have a valid rproc
  36. handle. There are several ways to achieve that cleanly (devres, pdata,
  37. the way remoteproc_rpmsg.c does this, or, if this becomes prevalent, we
  38. might also consider using dev_archdata for this).
  39. ::
  40. void rproc_shutdown(struct rproc *rproc)
  41. Power off a remote processor (previously booted with rproc_boot()).
  42. In case @rproc is still being used by an additional user(s), then
  43. this function will just decrement the power refcount and exit,
  44. without really powering off the device.
  45. Every call to rproc_boot() must (eventually) be accompanied by a call
  46. to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  47. .. note::
  48. we're not decrementing the rproc's refcount, only the power refcount.
  49. which means that the @rproc handle stays valid even after
  50. rproc_shutdown() returns, and users can still use it with a subsequent
  51. rproc_boot(), if needed.
  52. ::
  53. struct rproc *rproc_get_by_phandle(phandle phandle)
  54. Find an rproc handle using a device tree phandle. Returns the rproc
  55. handle on success, and NULL on failure. This function increments
  56. the remote processor's refcount, so always use rproc_put() to
  57. decrement it back once rproc isn't needed anymore.
  58. Typical usage
  59. =============
  60. ::
  61. #include <linux/remoteproc.h>
  62. /* in case we were given a valid 'rproc' handle */
  63. int dummy_rproc_example(struct rproc *my_rproc)
  64. {
  65. int ret;
  66. /* let's power on and boot our remote processor */
  67. ret = rproc_boot(my_rproc);
  68. if (ret) {
  69. /*
  70. * something went wrong. handle it and leave.
  71. */
  72. }
  73. /*
  74. * our remote processor is now powered on... give it some work
  75. */
  76. /* let's shut it down now */
  77. rproc_shutdown(my_rproc);
  78. }
  79. API for implementors
  80. ====================
  81. ::
  82. struct rproc *rproc_alloc(struct device *dev, const char *name,
  83. const struct rproc_ops *ops,
  84. const char *firmware, int len)
  85. Allocate a new remote processor handle, but don't register
  86. it yet. Required parameters are the underlying device, the
  87. name of this remote processor, platform-specific ops handlers,
  88. the name of the firmware to boot this rproc with, and the
  89. length of private data needed by the allocating rproc driver (in bytes).
  90. This function should be used by rproc implementations during
  91. initialization of the remote processor.
  92. After creating an rproc handle using this function, and when ready,
  93. implementations should then call rproc_add() to complete
  94. the registration of the remote processor.
  95. On success, the new rproc is returned, and on failure, NULL.
  96. .. note::
  97. **never** directly deallocate @rproc, even if it was not registered
  98. yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
  99. ::
  100. void rproc_free(struct rproc *rproc)
  101. Free an rproc handle that was allocated by rproc_alloc.
  102. This function essentially unrolls rproc_alloc(), by decrementing the
  103. rproc's refcount. It doesn't directly free rproc; that would happen
  104. only if there are no other references to rproc and its refcount now
  105. dropped to zero.
  106. ::
  107. int rproc_add(struct rproc *rproc)
  108. Register @rproc with the remoteproc framework, after it has been
  109. allocated with rproc_alloc().
  110. This is called by the platform-specific rproc implementation, whenever
  111. a new remote processor device is probed.
  112. Returns 0 on success and an appropriate error code otherwise.
  113. Note: this function initiates an asynchronous firmware loading
  114. context, which will look for virtio devices supported by the rproc's
  115. firmware.
  116. If found, those virtio devices will be created and added, so as a result
  117. of registering this remote processor, additional virtio drivers might get
  118. probed.
  119. ::
  120. int rproc_del(struct rproc *rproc)
  121. Unroll rproc_add().
  122. This function should be called when the platform specific rproc
  123. implementation decides to remove the rproc device. it should
  124. _only_ be called if a previous invocation of rproc_add()
  125. has completed successfully.
  126. After rproc_del() returns, @rproc is still valid, and its
  127. last refcount should be decremented by calling rproc_free().
  128. Returns 0 on success and -EINVAL if @rproc isn't valid.
  129. ::
  130. void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
  131. Report a crash in a remoteproc
  132. This function must be called every time a crash is detected by the
  133. platform specific rproc implementation. This should not be called from a
  134. non-remoteproc driver. This function can be called from atomic/interrupt
  135. context.
  136. Implementation callbacks
  137. ========================
  138. These callbacks should be provided by platform-specific remoteproc
  139. drivers::
  140. /**
  141. * struct rproc_ops - platform-specific device handlers
  142. * @start: power on the device and boot it
  143. * @stop: power off the device
  144. * @kick: kick a virtqueue (virtqueue id given as a parameter)
  145. */
  146. struct rproc_ops {
  147. int (*start)(struct rproc *rproc);
  148. int (*stop)(struct rproc *rproc);
  149. void (*kick)(struct rproc *rproc, int vqid);
  150. };
  151. Every remoteproc implementation should at least provide the ->start and ->stop
  152. handlers. If rpmsg/virtio functionality is also desired, then the ->kick handler
  153. should be provided as well.
  154. The ->start() handler takes an rproc handle and should then power on the
  155. device and boot it (use rproc->priv to access platform-specific private data).
  156. The boot address, in case needed, can be found in rproc->bootaddr (remoteproc
  157. core puts there the ELF entry point).
  158. On success, 0 should be returned, and on failure, an appropriate error code.
  159. The ->stop() handler takes an rproc handle and powers the device down.
  160. On success, 0 is returned, and on failure, an appropriate error code.
  161. The ->kick() handler takes an rproc handle, and an index of a virtqueue
  162. where new message was placed in. Implementations should interrupt the remote
  163. processor and let it know it has pending messages. Notifying remote processors
  164. the exact virtqueue index to look in is optional: it is easy (and not
  165. too expensive) to go through the existing virtqueues and look for new buffers
  166. in the used rings.
  167. Binary Firmware Structure
  168. =========================
  169. At this point remoteproc supports ELF32 and ELF64 firmware binaries. However,
  170. it is quite expected that other platforms/devices which we'd want to
  171. support with this framework will be based on different binary formats.
  172. When those use cases show up, we will have to decouple the binary format
  173. from the framework core, so we can support several binary formats without
  174. duplicating common code.
  175. When the firmware is parsed, its various segments are loaded to memory
  176. according to the specified device address (might be a physical address
  177. if the remote processor is accessing memory directly).
  178. In addition to the standard ELF segments, most remote processors would
  179. also include a special section which we call "the resource table".
  180. The resource table contains system resources that the remote processor
  181. requires before it should be powered on, such as allocation of physically
  182. contiguous memory, or iommu mapping of certain on-chip peripherals.
  183. Remotecore will only power up the device after all the resource table's
  184. requirement are met.
  185. In addition to system resources, the resource table may also contain
  186. resource entries that publish the existence of supported features
  187. or configurations by the remote processor, such as trace buffers and
  188. supported virtio devices (and their configurations).
  189. The resource table begins with this header::
  190. /**
  191. * struct resource_table - firmware resource table header
  192. * @ver: version number
  193. * @num: number of resource entries
  194. * @reserved: reserved (must be zero)
  195. * @offset: array of offsets pointing at the various resource entries
  196. *
  197. * The header of the resource table, as expressed by this structure,
  198. * contains a version number (should we need to change this format in the
  199. * future), the number of available resource entries, and their offsets
  200. * in the table.
  201. */
  202. struct resource_table {
  203. u32 ver;
  204. u32 num;
  205. u32 reserved[2];
  206. u32 offset[0];
  207. } __packed;
  208. Immediately following this header are the resource entries themselves,
  209. each of which begins with the following resource entry header::
  210. /**
  211. * struct fw_rsc_hdr - firmware resource entry header
  212. * @type: resource type
  213. * @data: resource data
  214. *
  215. * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
  216. * its @type. The content of the entry itself will immediately follow
  217. * this header, and it should be parsed according to the resource type.
  218. */
  219. struct fw_rsc_hdr {
  220. u32 type;
  221. u8 data[0];
  222. } __packed;
  223. Some resources entries are mere announcements, where the host is informed
  224. of specific remoteproc configuration. Other entries require the host to
  225. do something (e.g. allocate a system resource). Sometimes a negotiation
  226. is expected, where the firmware requests a resource, and once allocated,
  227. the host should provide back its details (e.g. address of an allocated
  228. memory region).
  229. Here are the various resource types that are currently supported::
  230. /**
  231. * enum fw_resource_type - types of resource entries
  232. *
  233. * @RSC_CARVEOUT: request for allocation of a physically contiguous
  234. * memory region.
  235. * @RSC_DEVMEM: request to iommu_map a memory-based peripheral.
  236. * @RSC_TRACE: announces the availability of a trace buffer into which
  237. * the remote processor will be writing logs.
  238. * @RSC_VDEV: declare support for a virtio device, and serve as its
  239. * virtio header.
  240. * @RSC_LAST: just keep this one at the end
  241. * @RSC_VENDOR_START: start of the vendor specific resource types range
  242. * @RSC_VENDOR_END: end of the vendor specific resource types range
  243. *
  244. * Please note that these values are used as indices to the rproc_handle_rsc
  245. * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
  246. * check the validity of an index before the lookup table is accessed, so
  247. * please update it as needed.
  248. */
  249. enum fw_resource_type {
  250. RSC_CARVEOUT = 0,
  251. RSC_DEVMEM = 1,
  252. RSC_TRACE = 2,
  253. RSC_VDEV = 3,
  254. RSC_LAST = 4,
  255. RSC_VENDOR_START = 128,
  256. RSC_VENDOR_END = 512,
  257. };
  258. For more details regarding a specific resource type, please see its
  259. dedicated structure in include/linux/remoteproc.h.
  260. We also expect that platform-specific resource entries will show up
  261. at some point. When that happens, we could easily add a new RSC_PLATFORM
  262. type, and hand those resources to the platform-specific rproc driver to handle.
  263. Virtio and remoteproc
  264. =====================
  265. The firmware should provide remoteproc information about virtio devices
  266. that it supports, and their configurations: a RSC_VDEV resource entry
  267. should specify the virtio device id (as in virtio_ids.h), virtio features,
  268. virtio config space, vrings information, etc.
  269. When a new remote processor is registered, the remoteproc framework
  270. will look for its resource table and will register the virtio devices
  271. it supports. A firmware may support any number of virtio devices, and
  272. of any type (a single remote processor can also easily support several
  273. rpmsg virtio devices this way, if desired).
  274. Of course, RSC_VDEV resource entries are only good enough for static
  275. allocation of virtio devices. Dynamic allocations will also be made possible
  276. using the rpmsg bus (similar to how we already do dynamic allocations of
  277. rpmsg channels; read more about it in rpmsg.txt).