remoteproc.txt 14 KB

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