remoteproc.h 17 KB

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