remoteproc.h 16 KB

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