remoteproc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #include <linux/idr.h>
  43. /**
  44. * struct resource_table - firmware resource table header
  45. * @ver: version number
  46. * @num: number of resource entries
  47. * @reserved: reserved (must be zero)
  48. * @offset: array of offsets pointing at the various resource entries
  49. *
  50. * A resource table is essentially a list of system resources required
  51. * by the remote processor. It may also include configuration entries.
  52. * If needed, the remote processor firmware should contain this table
  53. * as a dedicated ".resource_table" ELF section.
  54. *
  55. * Some resources entries are mere announcements, where the host is informed
  56. * of specific remoteproc configuration. Other entries require the host to
  57. * do something (e.g. allocate a system resource). Sometimes a negotiation
  58. * is expected, where the firmware requests a resource, and once allocated,
  59. * the host should provide back its details (e.g. address of an allocated
  60. * memory region).
  61. *
  62. * The header of the resource table, as expressed by this structure,
  63. * contains a version number (should we need to change this format in the
  64. * future), the number of available resource entries, and their offsets
  65. * in the table.
  66. *
  67. * Immediately following this header are the resource entries themselves,
  68. * each of which begins with a resource entry header (as described below).
  69. */
  70. struct resource_table {
  71. u32 ver;
  72. u32 num;
  73. u32 reserved[2];
  74. u32 offset[0];
  75. } __packed;
  76. /**
  77. * struct fw_rsc_hdr - firmware resource entry header
  78. * @type: resource type
  79. * @data: resource data
  80. *
  81. * Every resource entry begins with a 'struct fw_rsc_hdr' header providing
  82. * its @type. The content of the entry itself will immediately follow
  83. * this header, and it should be parsed according to the resource type.
  84. */
  85. struct fw_rsc_hdr {
  86. u32 type;
  87. u8 data[0];
  88. } __packed;
  89. /**
  90. * enum fw_resource_type - types of resource entries
  91. *
  92. * @RSC_CARVEOUT: request for allocation of a physically contiguous
  93. * memory region.
  94. * @RSC_DEVMEM: request to iommu_map a memory-based peripheral.
  95. * @RSC_TRACE: announces the availability of a trace buffer into which
  96. * the remote processor will be writing logs.
  97. * @RSC_VDEV: declare support for a virtio device, and serve as its
  98. * virtio header.
  99. * @RSC_LAST: just keep this one at the end
  100. *
  101. * For more details regarding a specific resource type, please see its
  102. * dedicated structure below.
  103. *
  104. * Please note that these values are used as indices to the rproc_handle_rsc
  105. * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to
  106. * check the validity of an index before the lookup table is accessed, so
  107. * please update it as needed.
  108. */
  109. enum fw_resource_type {
  110. RSC_CARVEOUT = 0,
  111. RSC_DEVMEM = 1,
  112. RSC_TRACE = 2,
  113. RSC_VDEV = 3,
  114. RSC_LAST = 4,
  115. };
  116. #define FW_RSC_ADDR_ANY (0xFFFFFFFFFFFFFFFF)
  117. /**
  118. * struct fw_rsc_carveout - physically contiguous memory request
  119. * @da: device address
  120. * @pa: physical address
  121. * @len: length (in bytes)
  122. * @flags: iommu protection flags
  123. * @reserved: reserved (must be zero)
  124. * @name: human-readable name of the requested memory region
  125. *
  126. * This resource entry requests the host to allocate a physically contiguous
  127. * memory region.
  128. *
  129. * These request entries should precede other firmware resource entries,
  130. * as other entries might request placing other data objects inside
  131. * these memory regions (e.g. data/code segments, trace resource entries, ...).
  132. *
  133. * Allocating memory this way helps utilizing the reserved physical memory
  134. * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
  135. * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
  136. * pressure is important; it may have a substantial impact on performance.
  137. *
  138. * If the firmware is compiled with static addresses, then @da should specify
  139. * the expected device address of this memory region. If @da is set to
  140. * FW_RSC_ADDR_ANY, then the host will dynamically allocate it, and then
  141. * overwrite @da with the dynamically allocated address.
  142. *
  143. * We will always use @da to negotiate the device addresses, even if it
  144. * isn't using an iommu. In that case, though, it will obviously contain
  145. * physical addresses.
  146. *
  147. * Some remote processors needs to know the allocated physical address
  148. * even if they do use an iommu. This is needed, e.g., if they control
  149. * hardware accelerators which access the physical memory directly (this
  150. * is the case with OMAP4 for instance). In that case, the host will
  151. * overwrite @pa with the dynamically allocated physical address.
  152. * Generally we don't want to expose physical addresses if we don't have to
  153. * (remote processors are generally _not_ trusted), so we might want to
  154. * change this to happen _only_ when explicitly required by the hardware.
  155. *
  156. * @flags is used to provide IOMMU protection flags, and @name should
  157. * (optionally) contain a human readable name of this carveout region
  158. * (mainly for debugging purposes).
  159. */
  160. struct fw_rsc_carveout {
  161. u32 da;
  162. u32 pa;
  163. u32 len;
  164. u32 flags;
  165. u32 reserved;
  166. u8 name[32];
  167. } __packed;
  168. /**
  169. * struct fw_rsc_devmem - iommu mapping request
  170. * @da: device address
  171. * @pa: physical address
  172. * @len: length (in bytes)
  173. * @flags: iommu protection flags
  174. * @reserved: reserved (must be zero)
  175. * @name: human-readable name of the requested region to be mapped
  176. *
  177. * This resource entry requests the host to iommu map a physically contiguous
  178. * memory region. This is needed in case the remote processor requires
  179. * access to certain memory-based peripherals; _never_ use it to access
  180. * regular memory.
  181. *
  182. * This is obviously only needed if the remote processor is accessing memory
  183. * via an iommu.
  184. *
  185. * @da should specify the required device address, @pa should specify
  186. * the physical address we want to map, @len should specify the size of
  187. * the mapping and @flags is the IOMMU protection flags. As always, @name may
  188. * (optionally) contain a human readable name of this mapping (mainly for
  189. * debugging purposes).
  190. *
  191. * Note: at this point we just "trust" those devmem entries to contain valid
  192. * physical addresses, but this isn't safe and will be changed: eventually we
  193. * want remoteproc implementations to provide us ranges of physical addresses
  194. * the firmware is allowed to request, and not allow firmwares to request
  195. * access to physical addresses that are outside those ranges.
  196. */
  197. struct fw_rsc_devmem {
  198. u32 da;
  199. u32 pa;
  200. u32 len;
  201. u32 flags;
  202. u32 reserved;
  203. u8 name[32];
  204. } __packed;
  205. /**
  206. * struct fw_rsc_trace - trace buffer declaration
  207. * @da: device address
  208. * @len: length (in bytes)
  209. * @reserved: reserved (must be zero)
  210. * @name: human-readable name of the trace buffer
  211. *
  212. * This resource entry provides the host information about a trace buffer
  213. * into which the remote processor will write log messages.
  214. *
  215. * @da specifies the device address of the buffer, @len specifies
  216. * its size, and @name may contain a human readable name of the trace buffer.
  217. *
  218. * After booting the remote processor, the trace buffers are exposed to the
  219. * user via debugfs entries (called trace0, trace1, etc..).
  220. */
  221. struct fw_rsc_trace {
  222. u32 da;
  223. u32 len;
  224. u32 reserved;
  225. u8 name[32];
  226. } __packed;
  227. /**
  228. * struct fw_rsc_vdev_vring - vring descriptor entry
  229. * @da: device address
  230. * @align: the alignment between the consumer and producer parts of the vring
  231. * @num: num of buffers supported by this vring (must be power of two)
  232. * @notifyid is a unique rproc-wide notify index for this vring. This notify
  233. * index is used when kicking a remote processor, to let it know that this
  234. * vring is triggered.
  235. * @reserved: reserved (must be zero)
  236. *
  237. * This descriptor is not a resource entry by itself; it is part of the
  238. * vdev resource type (see below).
  239. *
  240. * Note that @da should either contain the device address where
  241. * the remote processor is expecting the vring, or indicate that
  242. * dynamically allocation of the vring's device address is supported.
  243. */
  244. struct fw_rsc_vdev_vring {
  245. u32 da;
  246. u32 align;
  247. u32 num;
  248. u32 notifyid;
  249. u32 reserved;
  250. } __packed;
  251. /**
  252. * struct fw_rsc_vdev - virtio device header
  253. * @id: virtio device id (as in virtio_ids.h)
  254. * @notifyid is a unique rproc-wide notify index for this vdev. This notify
  255. * index is used when kicking a remote processor, to let it know that the
  256. * status/features of this vdev have changes.
  257. * @dfeatures specifies the virtio device features supported by the firmware
  258. * @gfeatures is a place holder used by the host to write back the
  259. * negotiated features that are supported by both sides.
  260. * @config_len is the size of the virtio config space of this vdev. The config
  261. * space lies in the resource table immediate after this vdev header.
  262. * @status is a place holder where the host will indicate its virtio progress.
  263. * @num_of_vrings indicates how many vrings are described in this vdev header
  264. * @reserved: reserved (must be zero)
  265. * @vring is an array of @num_of_vrings entries of 'struct fw_rsc_vdev_vring'.
  266. *
  267. * This resource is a virtio device header: it provides information about
  268. * the vdev, and is then used by the host and its peer remote processors
  269. * to negotiate and share certain virtio properties.
  270. *
  271. * By providing this resource entry, the firmware essentially asks remoteproc
  272. * to statically allocate a vdev upon registration of the rproc (dynamic vdev
  273. * allocation is not yet supported).
  274. *
  275. * Note: unlike virtualization systems, the term 'host' here means
  276. * the Linux side which is running remoteproc to control the remote
  277. * processors. We use the name 'gfeatures' to comply with virtio's terms,
  278. * though there isn't really any virtualized guest OS here: it's the host
  279. * which is responsible for negotiating the final features.
  280. * Yeah, it's a bit confusing.
  281. *
  282. * Note: immediately following this structure is the virtio config space for
  283. * this vdev (which is specific to the vdev; for more info, read the virtio
  284. * spec). the size of the config space is specified by @config_len.
  285. */
  286. struct fw_rsc_vdev {
  287. u32 id;
  288. u32 notifyid;
  289. u32 dfeatures;
  290. u32 gfeatures;
  291. u32 config_len;
  292. u8 status;
  293. u8 num_of_vrings;
  294. u8 reserved[2];
  295. struct fw_rsc_vdev_vring vring[0];
  296. } __packed;
  297. /**
  298. * struct rproc_mem_entry - memory entry descriptor
  299. * @va: virtual address
  300. * @dma: dma address
  301. * @len: length, in bytes
  302. * @da: device address
  303. * @priv: associated data
  304. * @node: list node
  305. */
  306. struct rproc_mem_entry {
  307. void *va;
  308. dma_addr_t dma;
  309. int len;
  310. u32 da;
  311. void *priv;
  312. struct list_head node;
  313. };
  314. struct rproc;
  315. /**
  316. * struct rproc_ops - platform-specific device handlers
  317. * @start: power on the device and boot it
  318. * @stop: power off the device
  319. * @kick: kick a virtqueue (virtqueue id given as a parameter)
  320. */
  321. struct rproc_ops {
  322. int (*start)(struct rproc *rproc);
  323. int (*stop)(struct rproc *rproc);
  324. void (*kick)(struct rproc *rproc, int vqid);
  325. };
  326. /**
  327. * enum rproc_state - remote processor states
  328. * @RPROC_OFFLINE: device is powered off
  329. * @RPROC_SUSPENDED: device is suspended; needs to be woken up to receive
  330. * a message.
  331. * @RPROC_RUNNING: device is up and running
  332. * @RPROC_CRASHED: device has crashed; need to start recovery
  333. * @RPROC_LAST: just keep this one at the end
  334. *
  335. * Please note that the values of these states are used as indices
  336. * to rproc_state_string, a state-to-name lookup table,
  337. * so please keep the two synchronized. @RPROC_LAST is used to check
  338. * the validity of an index before the lookup table is accessed, so
  339. * please update it as needed too.
  340. */
  341. enum rproc_state {
  342. RPROC_OFFLINE = 0,
  343. RPROC_SUSPENDED = 1,
  344. RPROC_RUNNING = 2,
  345. RPROC_CRASHED = 3,
  346. RPROC_LAST = 4,
  347. };
  348. /**
  349. * struct rproc - represents a physical remote processor device
  350. * @node: klist node of this rproc object
  351. * @domain: iommu domain
  352. * @name: human readable name of the rproc
  353. * @firmware: name of firmware file to be loaded
  354. * @priv: private data which belongs to the platform-specific rproc module
  355. * @ops: platform-specific start/stop rproc handlers
  356. * @dev: underlying device
  357. * @refcount: refcount of users that have a valid pointer to this rproc
  358. * @power: refcount of users who need this rproc powered up
  359. * @state: state of the device
  360. * @lock: lock which protects concurrent manipulations of the rproc
  361. * @dbg_dir: debugfs directory of this rproc device
  362. * @traces: list of trace buffers
  363. * @num_traces: number of trace buffers
  364. * @carveouts: list of physically contiguous memory allocations
  365. * @mappings: list of iommu mappings we initiated, needed on shutdown
  366. * @firmware_loading_complete: marks e/o asynchronous firmware loading
  367. * @bootaddr: address of first instruction to boot rproc with (optional)
  368. * @rvdevs: list of remote virtio devices
  369. * @notifyids: idr for dynamically assigning rproc-wide unique notify ids
  370. */
  371. struct rproc {
  372. struct klist_node node;
  373. struct iommu_domain *domain;
  374. const char *name;
  375. const char *firmware;
  376. void *priv;
  377. const struct rproc_ops *ops;
  378. struct device *dev;
  379. struct kref refcount;
  380. atomic_t power;
  381. unsigned int state;
  382. struct mutex lock;
  383. struct dentry *dbg_dir;
  384. struct list_head traces;
  385. int num_traces;
  386. struct list_head carveouts;
  387. struct list_head mappings;
  388. struct completion firmware_loading_complete;
  389. u32 bootaddr;
  390. struct list_head rvdevs;
  391. struct idr notifyids;
  392. };
  393. /* we currently support only two vrings per rvdev */
  394. #define RVDEV_NUM_VRINGS 2
  395. /**
  396. * struct rproc_vring - remoteproc vring state
  397. * @va: virtual address
  398. * @dma: dma address
  399. * @len: length, in bytes
  400. * @da: device address
  401. * @align: vring alignment
  402. * @notifyid: rproc-specific unique vring index
  403. * @rvdev: remote vdev
  404. * @vq: the virtqueue of this vring
  405. */
  406. struct rproc_vring {
  407. void *va;
  408. dma_addr_t dma;
  409. int len;
  410. u32 da;
  411. u32 align;
  412. int notifyid;
  413. struct rproc_vdev *rvdev;
  414. struct virtqueue *vq;
  415. };
  416. /**
  417. * struct rproc_vdev - remoteproc state for a supported virtio device
  418. * @node: list node
  419. * @rproc: the rproc handle
  420. * @vdev: the virio device
  421. * @vring: the vrings for this vdev
  422. * @dfeatures: virtio device features
  423. * @gfeatures: virtio guest features
  424. */
  425. struct rproc_vdev {
  426. struct list_head node;
  427. struct rproc *rproc;
  428. struct virtio_device vdev;
  429. struct rproc_vring vring[RVDEV_NUM_VRINGS];
  430. unsigned long dfeatures;
  431. unsigned long gfeatures;
  432. };
  433. struct rproc *rproc_get_by_name(const char *name);
  434. void rproc_put(struct rproc *rproc);
  435. struct rproc *rproc_alloc(struct device *dev, const char *name,
  436. const struct rproc_ops *ops,
  437. const char *firmware, int len);
  438. void rproc_free(struct rproc *rproc);
  439. int rproc_register(struct rproc *rproc);
  440. int rproc_unregister(struct rproc *rproc);
  441. int rproc_boot(struct rproc *rproc);
  442. void rproc_shutdown(struct rproc *rproc);
  443. static inline struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
  444. {
  445. return container_of(vdev, struct rproc_vdev, vdev);
  446. }
  447. static inline struct rproc *vdev_to_rproc(struct virtio_device *vdev)
  448. {
  449. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  450. return rvdev->rproc;
  451. }
  452. #endif /* REMOTEPROC_H */