remoteproc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 (-1)
  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. * @pa: physical address
  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 pa;
  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. struct firmware;
  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. * @da_to_va: optional platform hook to perform address translations
  321. * @load_rsc_table: load resource table from firmware image
  322. * @find_loaded_rsc_table: find the loaded resouce table
  323. * @load: load firmeware to memory, where the remote processor
  324. * expects to find it
  325. * @sanity_check: sanity check the fw image
  326. * @get_boot_addr: get boot address to entry point specified in firmware
  327. */
  328. struct rproc_ops {
  329. int (*start)(struct rproc *rproc);
  330. int (*stop)(struct rproc *rproc);
  331. void (*kick)(struct rproc *rproc, int vqid);
  332. void * (*da_to_va)(struct rproc *rproc, u64 da, int len);
  333. int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
  334. struct resource_table *(*find_loaded_rsc_table)(
  335. struct rproc *rproc, const struct firmware *fw);
  336. int (*load)(struct rproc *rproc, const struct firmware *fw);
  337. int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
  338. u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
  339. };
  340. /**
  341. * enum rproc_state - remote processor states
  342. * @RPROC_OFFLINE: device is powered off
  343. * @RPROC_SUSPENDED: device is suspended; needs to be woken up to receive
  344. * a message.
  345. * @RPROC_RUNNING: device is up and running
  346. * @RPROC_CRASHED: device has crashed; need to start recovery
  347. * @RPROC_DELETED: device is deleted
  348. * @RPROC_LAST: just keep this one at the end
  349. *
  350. * Please note that the values of these states are used as indices
  351. * to rproc_state_string, a state-to-name lookup table,
  352. * so please keep the two synchronized. @RPROC_LAST is used to check
  353. * the validity of an index before the lookup table is accessed, so
  354. * please update it as needed too.
  355. */
  356. enum rproc_state {
  357. RPROC_OFFLINE = 0,
  358. RPROC_SUSPENDED = 1,
  359. RPROC_RUNNING = 2,
  360. RPROC_CRASHED = 3,
  361. RPROC_DELETED = 4,
  362. RPROC_LAST = 5,
  363. };
  364. /**
  365. * enum rproc_crash_type - remote processor crash types
  366. * @RPROC_MMUFAULT: iommu fault
  367. * @RPROC_WATCHDOG: watchdog bite
  368. * @RPROC_FATAL_ERROR fatal error
  369. *
  370. * Each element of the enum is used as an array index. So that, the value of
  371. * the elements should be always something sane.
  372. *
  373. * Feel free to add more types when needed.
  374. */
  375. enum rproc_crash_type {
  376. RPROC_MMUFAULT,
  377. RPROC_WATCHDOG,
  378. RPROC_FATAL_ERROR,
  379. };
  380. /**
  381. * struct rproc_dump_segment - segment info from ELF header
  382. * @node: list node related to the rproc segment list
  383. * @da: device address of the segment
  384. * @size: size of the segment
  385. */
  386. struct rproc_dump_segment {
  387. struct list_head node;
  388. dma_addr_t da;
  389. size_t size;
  390. loff_t offset;
  391. };
  392. /**
  393. * struct rproc - represents a physical remote processor device
  394. * @node: list node of this rproc object
  395. * @domain: iommu domain
  396. * @name: human readable name of the rproc
  397. * @firmware: name of firmware file to be loaded
  398. * @priv: private data which belongs to the platform-specific rproc module
  399. * @ops: platform-specific start/stop rproc handlers
  400. * @dev: virtual device for refcounting and common remoteproc behavior
  401. * @power: refcount of users who need this rproc powered up
  402. * @state: state of the device
  403. * @lock: lock which protects concurrent manipulations of the rproc
  404. * @dbg_dir: debugfs directory of this rproc device
  405. * @traces: list of trace buffers
  406. * @num_traces: number of trace buffers
  407. * @carveouts: list of physically contiguous memory allocations
  408. * @mappings: list of iommu mappings we initiated, needed on shutdown
  409. * @bootaddr: address of first instruction to boot rproc with (optional)
  410. * @rvdevs: list of remote virtio devices
  411. * @subdevs: list of subdevices, to following the running state
  412. * @notifyids: idr for dynamically assigning rproc-wide unique notify ids
  413. * @index: index of this rproc device
  414. * @crash_handler: workqueue for handling a crash
  415. * @crash_cnt: crash counter
  416. * @recovery_disabled: flag that state if recovery was disabled
  417. * @max_notifyid: largest allocated notify id.
  418. * @table_ptr: pointer to the resource table in effect
  419. * @cached_table: copy of the resource table
  420. * @table_sz: size of @cached_table
  421. * @has_iommu: flag to indicate if remote processor is behind an MMU
  422. * @dump_segments: list of segments in the firmware
  423. */
  424. struct rproc {
  425. struct list_head node;
  426. struct iommu_domain *domain;
  427. const char *name;
  428. char *firmware;
  429. void *priv;
  430. struct rproc_ops *ops;
  431. struct device dev;
  432. atomic_t power;
  433. unsigned int state;
  434. struct mutex lock;
  435. struct dentry *dbg_dir;
  436. struct list_head traces;
  437. int num_traces;
  438. struct list_head carveouts;
  439. struct list_head mappings;
  440. u32 bootaddr;
  441. struct list_head rvdevs;
  442. struct list_head subdevs;
  443. struct idr notifyids;
  444. int index;
  445. struct work_struct crash_handler;
  446. unsigned int crash_cnt;
  447. bool recovery_disabled;
  448. int max_notifyid;
  449. struct resource_table *table_ptr;
  450. struct resource_table *cached_table;
  451. size_t table_sz;
  452. bool has_iommu;
  453. bool auto_boot;
  454. struct list_head dump_segments;
  455. };
  456. /**
  457. * struct rproc_subdev - subdevice tied to a remoteproc
  458. * @node: list node related to the rproc subdevs list
  459. * @prepare: prepare function, called before the rproc is started
  460. * @start: start function, called after the rproc has been started
  461. * @stop: stop function, called before the rproc is stopped; the @crashed
  462. * parameter indicates if this originates from a recovery
  463. * @unprepare: unprepare function, called after the rproc has been stopped
  464. */
  465. struct rproc_subdev {
  466. struct list_head node;
  467. int (*prepare)(struct rproc_subdev *subdev);
  468. int (*start)(struct rproc_subdev *subdev);
  469. void (*stop)(struct rproc_subdev *subdev, bool crashed);
  470. void (*unprepare)(struct rproc_subdev *subdev);
  471. };
  472. /* we currently support only two vrings per rvdev */
  473. #define RVDEV_NUM_VRINGS 2
  474. /**
  475. * struct rproc_vring - remoteproc vring state
  476. * @va: virtual address
  477. * @dma: dma address
  478. * @len: length, in bytes
  479. * @da: device address
  480. * @align: vring alignment
  481. * @notifyid: rproc-specific unique vring index
  482. * @rvdev: remote vdev
  483. * @vq: the virtqueue of this vring
  484. */
  485. struct rproc_vring {
  486. void *va;
  487. dma_addr_t dma;
  488. int len;
  489. u32 da;
  490. u32 align;
  491. int notifyid;
  492. struct rproc_vdev *rvdev;
  493. struct virtqueue *vq;
  494. };
  495. /**
  496. * struct rproc_vdev - remoteproc state for a supported virtio device
  497. * @refcount: reference counter for the vdev and vring allocations
  498. * @subdev: handle for registering the vdev as a rproc subdevice
  499. * @id: virtio device id (as in virtio_ids.h)
  500. * @node: list node
  501. * @rproc: the rproc handle
  502. * @vdev: the virio device
  503. * @vring: the vrings for this vdev
  504. * @rsc_offset: offset of the vdev's resource entry
  505. */
  506. struct rproc_vdev {
  507. struct kref refcount;
  508. struct rproc_subdev subdev;
  509. unsigned int id;
  510. struct list_head node;
  511. struct rproc *rproc;
  512. struct virtio_device vdev;
  513. struct rproc_vring vring[RVDEV_NUM_VRINGS];
  514. u32 rsc_offset;
  515. };
  516. struct rproc *rproc_get_by_phandle(phandle phandle);
  517. struct rproc *rproc_get_by_child(struct device *dev);
  518. struct rproc *rproc_alloc(struct device *dev, const char *name,
  519. const struct rproc_ops *ops,
  520. const char *firmware, int len);
  521. void rproc_put(struct rproc *rproc);
  522. int rproc_add(struct rproc *rproc);
  523. int rproc_del(struct rproc *rproc);
  524. void rproc_free(struct rproc *rproc);
  525. int rproc_boot(struct rproc *rproc);
  526. void rproc_shutdown(struct rproc *rproc);
  527. void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type);
  528. int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size);
  529. static inline struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
  530. {
  531. return container_of(vdev, struct rproc_vdev, vdev);
  532. }
  533. static inline struct rproc *vdev_to_rproc(struct virtio_device *vdev)
  534. {
  535. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  536. return rvdev->rproc;
  537. }
  538. void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev);
  539. void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev);
  540. #endif /* REMOTEPROC_H */