remoteproc.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. 2. User API
  20. int rproc_boot(struct rproc *rproc)
  21. - Boot a remote processor (i.e. load its firmware, power it on, ...).
  22. If the remote processor is already powered on, this function immediately
  23. returns (successfully).
  24. Returns 0 on success, and an appropriate error value otherwise.
  25. Note: to use this function you should already have a valid rproc
  26. handle. There are several ways to achieve that cleanly (devres, pdata,
  27. the way remoteproc_rpmsg.c does this, or, if this becomes prevalent, we
  28. might also consider using dev_archdata for this). See also
  29. rproc_get_by_name() below.
  30. void rproc_shutdown(struct rproc *rproc)
  31. - Power off a remote processor (previously booted with rproc_boot()).
  32. In case @rproc is still being used by an additional user(s), then
  33. this function will just decrement the power refcount and exit,
  34. without really powering off the device.
  35. Every call to rproc_boot() must (eventually) be accompanied by a call
  36. to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  37. Notes:
  38. - we're not decrementing the rproc's refcount, only the power refcount.
  39. which means that the @rproc handle stays valid even after
  40. rproc_shutdown() returns, and users can still use it with a subsequent
  41. rproc_boot(), if needed.
  42. - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
  43. because rproc_shutdown() _does not_ decrement the refcount of @rproc.
  44. To decrement the refcount of @rproc, use rproc_put() (but _only_ if
  45. you acquired @rproc using rproc_get_by_name()).
  46. struct rproc *rproc_get_by_name(const char *name)
  47. - Find an rproc handle using the remote processor's name, and then
  48. boot it. If it's already powered on, then just immediately return
  49. (successfully). Returns the rproc handle on success, and NULL on failure.
  50. This function increments the remote processor's refcount, so always
  51. use rproc_put() to decrement it back once rproc isn't needed anymore.
  52. Note: currently rproc_get_by_name() and rproc_put() are not used anymore
  53. by the rpmsg bus and its drivers. We need to scrutinize the use cases
  54. that still need them, and see if we can migrate them to use the non
  55. name-based boot/shutdown interface.
  56. void rproc_put(struct rproc *rproc)
  57. - Decrement @rproc's power refcount and shut it down if it reaches zero
  58. (essentially by just calling rproc_shutdown), and then decrement @rproc's
  59. validity refcount too.
  60. After this function returns, @rproc may _not_ be used anymore, and its
  61. handle should be considered invalid.
  62. This function should be called _iff_ the @rproc handle was grabbed by
  63. calling rproc_get_by_name().
  64. 3. Typical usage
  65. #include <linux/remoteproc.h>
  66. /* in case we were given a valid 'rproc' handle */
  67. int dummy_rproc_example(struct rproc *my_rproc)
  68. {
  69. int ret;
  70. /* let's power on and boot our remote processor */
  71. ret = rproc_boot(my_rproc);
  72. if (ret) {
  73. /*
  74. * something went wrong. handle it and leave.
  75. */
  76. }
  77. /*
  78. * our remote processor is now powered on... give it some work
  79. */
  80. /* let's shut it down now */
  81. rproc_shutdown(my_rproc);
  82. }
  83. 4. API for implementors
  84. struct rproc *rproc_alloc(struct device *dev, const char *name,
  85. const struct rproc_ops *ops,
  86. const char *firmware, int len)
  87. - Allocate a new remote processor handle, but don't register
  88. it yet. Required parameters are the underlying device, the
  89. name of this remote processor, platform-specific ops handlers,
  90. the name of the firmware to boot this rproc with, and the
  91. length of private data needed by the allocating rproc driver (in bytes).
  92. This function should be used by rproc implementations during
  93. initialization of the remote processor.
  94. After creating an rproc handle using this function, and when ready,
  95. implementations should then call rproc_register() to complete
  96. the registration of the remote processor.
  97. On success, the new rproc is returned, and on failure, NULL.
  98. Note: _never_ directly deallocate @rproc, even if it was not registered
  99. yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free().
  100. void rproc_free(struct rproc *rproc)
  101. - Free an rproc handle that was allocated by rproc_alloc.
  102. This function should _only_ be used if @rproc was only allocated,
  103. but not registered yet.
  104. If @rproc was already successfully registered (by calling
  105. rproc_register()), then use rproc_unregister() instead.
  106. int rproc_register(struct rproc *rproc)
  107. - Register @rproc with the remoteproc framework, after it has been
  108. allocated with rproc_alloc().
  109. This is called by the platform-specific rproc implementation, whenever
  110. a new remote processor device is probed.
  111. Returns 0 on success and an appropriate error code otherwise.
  112. Note: this function initiates an asynchronous firmware loading
  113. context, which will look for virtio devices supported by the rproc's
  114. firmware.
  115. If found, those virtio devices will be created and added, so as a result
  116. of registering this remote processor, additional virtio drivers might get
  117. probed.
  118. Currently, though, we only support a single RPMSG virtio vdev per remote
  119. processor.
  120. int rproc_unregister(struct rproc *rproc)
  121. - Unregister a remote processor, and decrement its refcount.
  122. If its refcount drops to zero, then @rproc will be freed. If not,
  123. it will be freed later once the last reference is dropped.
  124. This function should be called when the platform specific rproc
  125. implementation decides to remove the rproc device. it should
  126. _only_ be called if a previous invocation of rproc_register()
  127. has completed successfully.
  128. After rproc_unregister() returns, @rproc is _not_ valid anymore and
  129. it shouldn't be used. More specifically, don't call rproc_free()
  130. or try to directly free @rproc after rproc_unregister() returns;
  131. none of these are needed, and calling them is a bug.
  132. Returns 0 on success and -EINVAL if @rproc isn't valid.
  133. 5. Implementation callbacks
  134. These callbacks should be provided by platform-specific remoteproc
  135. drivers:
  136. /**
  137. * struct rproc_ops - platform-specific device handlers
  138. * @start: power on the device and boot it
  139. * @stop: power off the device
  140. * @kick: kick a virtqueue (virtqueue id given as a parameter)
  141. */
  142. struct rproc_ops {
  143. int (*start)(struct rproc *rproc);
  144. int (*stop)(struct rproc *rproc);
  145. void (*kick)(struct rproc *rproc, int vqid);
  146. };
  147. Every remoteproc implementation should at least provide the ->start and ->stop
  148. handlers. If rpmsg functionality is also desired, then the ->kick handler
  149. should be provided as well.
  150. The ->start() handler takes an rproc handle and should then power on the
  151. device and boot it (use rproc->priv to access platform-specific private data).
  152. The boot address, in case needed, can be found in rproc->bootaddr (remoteproc
  153. core puts there the ELF entry point).
  154. On success, 0 should be returned, and on failure, an appropriate error code.
  155. The ->stop() handler takes an rproc handle and powers the device down.
  156. On success, 0 is returned, and on failure, an appropriate error code.
  157. The ->kick() handler takes an rproc handle, and an index of a virtqueue
  158. where new message was placed in. Implementations should interrupt the remote
  159. processor and let it know it has pending messages. Notifying remote processors
  160. the exact virtqueue index to look in is optional: it is easy (and not
  161. too expensive) to go through the existing virtqueues and look for new buffers
  162. in the used rings.
  163. 6. Binary Firmware Structure
  164. At this point remoteproc only supports ELF32 firmware binaries. However,
  165. it is quite expected that other platforms/devices which we'd want to
  166. support with this framework will be based on different binary formats.
  167. When those use cases show up, we will have to decouple the binary format
  168. from the framework core, so we can support several binary formats without
  169. duplicating common code.
  170. When the firmware is parsed, its various segments are loaded to memory
  171. according to the specified device address (might be a physical address
  172. if the remote processor is accessing memory directly).
  173. In addition to the standard ELF segments, most remote processors would
  174. also include a special section which we call "the resource table".
  175. The resource table contains system resources that the remote processor
  176. requires before it should be powered on, such as allocation of physically
  177. contiguous memory, or iommu mapping of certain on-chip peripherals.
  178. Remotecore will only power up the device after all the resource table's
  179. requirement are met.
  180. In addition to system resources, the resource table may also contain
  181. resource entries that publish the existence of supported features
  182. or configurations by the remote processor, such as trace buffers and
  183. supported virtio devices (and their configurations).
  184. Currently the resource table is just an array of:
  185. /**
  186. * struct fw_resource - describes an entry from the resource section
  187. * @type: resource type
  188. * @id: index number of the resource
  189. * @da: device address of the resource
  190. * @pa: physical address of the resource
  191. * @len: size, in bytes, of the resource
  192. * @flags: properties of the resource, e.g. iommu protection required
  193. * @reserved: must be 0 atm
  194. * @name: name of resource
  195. */
  196. struct fw_resource {
  197. u32 type;
  198. u32 id;
  199. u64 da;
  200. u64 pa;
  201. u32 len;
  202. u32 flags;
  203. u8 reserved[16];
  204. u8 name[48];
  205. } __packed;
  206. Some resources entries are mere announcements, where the host is informed
  207. of specific remoteproc configuration. Other entries require the host to
  208. do something (e.g. reserve a requested resource) and possibly also reply
  209. by overwriting a member inside 'struct fw_resource' with info about the
  210. allocated resource.
  211. Different resource entries use different members of this struct,
  212. with different meanings. This is pretty limiting and error-prone,
  213. so the plan is to move to variable-length TLV-based resource entries,
  214. where each resource will begin with a type and length fields, followed by
  215. its own specific structure.
  216. Here are the resource types that are currently being used:
  217. /**
  218. * enum fw_resource_type - types of resource entries
  219. *
  220. * @RSC_CARVEOUT: request for allocation of a physically contiguous
  221. * memory region.
  222. * @RSC_DEVMEM: request to iommu_map a memory-based peripheral.
  223. * @RSC_TRACE: announces the availability of a trace buffer into which
  224. * the remote processor will be writing logs. In this case,
  225. * 'da' indicates the device address where logs are written to,
  226. * and 'len' is the size of the trace buffer.
  227. * @RSC_VRING: request for allocation of a virtio vring (address should
  228. * be indicated in 'da', and 'len' should contain the number
  229. * of buffers supported by the vring).
  230. * @RSC_VIRTIO_DEV: announces support for a virtio device, and serves as
  231. * the virtio header. 'da' contains the virtio device
  232. * features, 'pa' holds the virtio guest features (host
  233. * will write them here after they're negotiated), 'len'
  234. * holds the virtio status, and 'flags' holds the virtio
  235. * device id (currently only VIRTIO_ID_RPMSG is supported).
  236. */
  237. enum fw_resource_type {
  238. RSC_CARVEOUT = 0,
  239. RSC_DEVMEM = 1,
  240. RSC_TRACE = 2,
  241. RSC_VRING = 3,
  242. RSC_VIRTIO_DEV = 4,
  243. RSC_VIRTIO_CFG = 5,
  244. };
  245. Most of the resource entries share the basic idea of address/length
  246. negotiation with the host: the firmware usually asks for memory
  247. of size 'len' bytes, and the host needs to allocate it and provide
  248. the device/physical address (when relevant) in 'da'/'pa' respectively.
  249. If the firmware is compiled with hard coded device addresses, and
  250. can't handle dynamically allocated 'da' values, then the 'da' field
  251. will contain the expected device addresses (today we actually only support
  252. this scheme, as there aren't yet any use cases for dynamically allocated
  253. device addresses).
  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_PLAFORM
  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_VIRTIO_DEV resource entry
  260. should specify the virtio device id, and subsequent RSC_VRING resource entries
  261. should indicate the vring size (i.e. how many buffers do they support) and
  262. where should they be mapped (i.e. which device address). Note: the alignment
  263. between the consumer and producer parts of the vring is assumed to be 4096.
  264. At this point we only support a single virtio rpmsg device per remote
  265. processor, but the plan is to remove this limitation. In addition, once we
  266. move to TLV-based resource table, the plan is to have a single RSC_VIRTIO
  267. entry per supported virtio device, which will include the virtio header,
  268. the vrings information and the virtio config space.
  269. Of course, RSC_VIRTIO resource entries are only good enough for static
  270. allocation of virtio devices. Dynamic allocations will also be made possible
  271. using the rpmsg bus (similar to how we already do dynamic allocations of
  272. rpmsg channels; read more about it in rpmsg.txt).