remoteproc.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Remote Processor Framework
  3. *
  4. * Copyright(c) 2011 Texas Instruments, Inc.
  5. * Copyright(c) 2011 Google, Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name Texas Instruments nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef REMOTEPROC_H
  35. #define REMOTEPROC_H
  36. #include <linux/types.h>
  37. #include <linux/kref.h>
  38. #include <linux/klist.h>
  39. #include <linux/mutex.h>
  40. #include <linux/virtio.h>
  41. #include <linux/completion.h>
  42. /*
  43. * The alignment between the consumer and producer parts of the vring.
  44. * Note: this is part of the "wire" protocol. If you change this, you need
  45. * to update your peers too.
  46. */
  47. #define AMP_VRING_ALIGN (4096)
  48. /**
  49. * struct fw_resource - describes an entry from the resource section
  50. * @type: resource type
  51. * @id: index number of the resource
  52. * @da: device address of the resource
  53. * @pa: physical address of the resource
  54. * @len: size, in bytes, of the resource
  55. * @flags: properties of the resource, e.g. iommu protection required
  56. * @reserved: must be 0 atm
  57. * @name: name of resource
  58. *
  59. * The remote processor firmware should contain a "resource table":
  60. * array of 'struct fw_resource' entries.
  61. *
  62. * Some resources entries are mere announcements, where the host is informed
  63. * of specific remoteproc configuration. Other entries require the host to
  64. * do something (e.g. reserve a requested resource) and possibly also reply
  65. * by overwriting a member inside 'struct fw_resource' with info about the
  66. * allocated resource.
  67. *
  68. * Different resource entries use different members of this struct,
  69. * with different meanings. This is pretty limiting and error-prone,
  70. * so the plan is to move to variable-length TLV-based resource entries,
  71. * where each resource type will have its own structure.
  72. */
  73. struct fw_resource {
  74. u32 type;
  75. u32 id;
  76. u64 da;
  77. u64 pa;
  78. u32 len;
  79. u32 flags;
  80. u8 reserved[16];
  81. u8 name[48];
  82. } __packed;
  83. /**
  84. * enum fw_resource_type - types of resource entries
  85. *
  86. * @RSC_CARVEOUT: request for allocation of a physically contiguous
  87. * memory region.
  88. * @RSC_DEVMEM: request to iommu_map a memory-based peripheral.
  89. * @RSC_TRACE: announces the availability of a trace buffer into which
  90. * the remote processor will be writing logs. In this case,
  91. * 'da' indicates the device address where logs are written to,
  92. * and 'len' is the size of the trace buffer.
  93. * @RSC_VRING: request for allocation of a virtio vring (address should
  94. * be indicated in 'da', and 'len' should contain the number
  95. * of buffers supported by the vring).
  96. * @RSC_VIRTIO_DEV: this entry declares about support for a virtio device,
  97. * and serves as the virtio header. 'da' holds the
  98. * the virtio device features, 'pa' holds the virtio guest
  99. * features, 'len' holds the virtio status, and 'flags' holds
  100. * the virtio id (currently only VIRTIO_ID_RPMSG is supported).
  101. * @RSC_LAST: just keep this one at the end
  102. *
  103. * Most of the resource entries share the basic idea of address/length
  104. * negotiation with the host: the firmware usually asks (on behalf of the
  105. * remote processor that will soon be booted with it) for memory
  106. * of size 'len' bytes, and the host needs to allocate it and provide
  107. * the device/physical address (when relevant) in 'da'/'pa' respectively.
  108. *
  109. * If the firmware is compiled with hard coded device addresses, and
  110. * can't handle dynamically allocated 'da' values, then the 'da' field
  111. * will contain the expected device addresses (today we actually only support
  112. * this scheme, as there aren't yet any use cases for dynamically allocated
  113. * device addresses).
  114. *
  115. * Please note that these values are used as indices to the rproc_handle_rsc
  116. * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
  117. * check the validity of an index before the lookup table is accessed, so
  118. * please update it as needed.
  119. */
  120. enum fw_resource_type {
  121. RSC_CARVEOUT = 0,
  122. RSC_DEVMEM = 1,
  123. RSC_TRACE = 2,
  124. RSC_VRING = 3,
  125. RSC_VIRTIO_DEV = 4,
  126. RSC_LAST = 5,
  127. };
  128. /**
  129. * struct rproc_mem_entry - memory entry descriptor
  130. * @va: virtual address
  131. * @dma: dma address
  132. * @len: length, in bytes
  133. * @da: device address
  134. * @priv: associated data
  135. * @node: list node
  136. */
  137. struct rproc_mem_entry {
  138. void *va;
  139. dma_addr_t dma;
  140. int len;
  141. u64 da;
  142. void *priv;
  143. struct list_head node;
  144. };
  145. struct rproc;
  146. /**
  147. * struct rproc_ops - platform-specific device handlers
  148. * @start: power on the device and boot it
  149. * @stop: power off the device
  150. * @kick: kick a virtqueue (virtqueue id given as a parameter)
  151. */
  152. struct rproc_ops {
  153. int (*start)(struct rproc *rproc);
  154. int (*stop)(struct rproc *rproc);
  155. void (*kick)(struct rproc *rproc, int vqid);
  156. };
  157. /**
  158. * enum rproc_state - remote processor states
  159. * @RPROC_OFFLINE: device is powered off
  160. * @RPROC_SUSPENDED: device is suspended; needs to be woken up to receive
  161. * a message.
  162. * @RPROC_RUNNING: device is up and running
  163. * @RPROC_CRASHED: device has crashed; need to start recovery
  164. * @RPROC_LAST: just keep this one at the end
  165. *
  166. * Please note that the values of these states are used as indices
  167. * to rproc_state_string, a state-to-name lookup table,
  168. * so please keep the two synchronized. @RPROC_LAST is used to check
  169. * the validity of an index before the lookup table is accessed, so
  170. * please update it as needed too.
  171. */
  172. enum rproc_state {
  173. RPROC_OFFLINE = 0,
  174. RPROC_SUSPENDED = 1,
  175. RPROC_RUNNING = 2,
  176. RPROC_CRASHED = 3,
  177. RPROC_LAST = 4,
  178. };
  179. /**
  180. * struct rproc - represents a physical remote processor device
  181. * @node: klist node of this rproc object
  182. * @domain: iommu domain
  183. * @name: human readable name of the rproc
  184. * @firmware: name of firmware file to be loaded
  185. * @priv: private data which belongs to the platform-specific rproc module
  186. * @ops: platform-specific start/stop rproc handlers
  187. * @dev: underlying device
  188. * @refcount: refcount of users that have a valid pointer to this rproc
  189. * @power: refcount of users who need this rproc powered up
  190. * @state: state of the device
  191. * @lock: lock which protects concurrent manipulations of the rproc
  192. * @dbg_dir: debugfs directory of this rproc device
  193. * @traces: list of trace buffers
  194. * @num_traces: number of trace buffers
  195. * @carveouts: list of physically contiguous memory allocations
  196. * @mappings: list of iommu mappings we initiated, needed on shutdown
  197. * @firmware_loading_complete: marks e/o asynchronous firmware loading
  198. * @bootaddr: address of first instruction to boot rproc with (optional)
  199. * @rvdev: virtio device (we only support a single rpmsg virtio device for now)
  200. */
  201. struct rproc {
  202. struct klist_node node;
  203. struct iommu_domain *domain;
  204. const char *name;
  205. const char *firmware;
  206. void *priv;
  207. const struct rproc_ops *ops;
  208. struct device *dev;
  209. struct kref refcount;
  210. atomic_t power;
  211. unsigned int state;
  212. struct mutex lock;
  213. struct dentry *dbg_dir;
  214. struct list_head traces;
  215. int num_traces;
  216. struct list_head carveouts;
  217. struct list_head mappings;
  218. struct completion firmware_loading_complete;
  219. u64 bootaddr;
  220. struct rproc_vdev *rvdev;
  221. };
  222. /**
  223. * struct rproc_vdev - remoteproc state for a supported virtio device
  224. * @rproc: the rproc handle
  225. * @vdev: the virio device
  226. * @vq: the virtqueues for this vdev
  227. * @vring: the vrings for this vdev
  228. * @dfeatures: virtio device features
  229. * @gfeatures: virtio guest features
  230. */
  231. struct rproc_vdev {
  232. struct rproc *rproc;
  233. struct virtio_device vdev;
  234. struct virtqueue *vq[2];
  235. struct rproc_mem_entry vring[2];
  236. unsigned long dfeatures;
  237. unsigned long gfeatures;
  238. };
  239. struct rproc *rproc_get_by_name(const char *name);
  240. void rproc_put(struct rproc *rproc);
  241. struct rproc *rproc_alloc(struct device *dev, const char *name,
  242. const struct rproc_ops *ops,
  243. const char *firmware, int len);
  244. void rproc_free(struct rproc *rproc);
  245. int rproc_register(struct rproc *rproc);
  246. int rproc_unregister(struct rproc *rproc);
  247. int rproc_boot(struct rproc *rproc);
  248. void rproc_shutdown(struct rproc *rproc);
  249. static inline struct rproc *vdev_to_rproc(struct virtio_device *vdev)
  250. {
  251. struct rproc_vdev *rvdev = container_of(vdev, struct rproc_vdev, vdev);
  252. return rvdev->rproc;
  253. }
  254. #endif /* REMOTEPROC_H */