vha.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*!
  2. *****************************************************************************
  3. *
  4. * @File vha.h
  5. * ---------------------------------------------------------------------------
  6. *
  7. * Copyright (c) Imagination Technologies Ltd.
  8. *
  9. * The contents of this file are subject to the MIT license as set out below.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * Alternatively, the contents of this file may be used under the terms of the
  30. * GNU General Public License Version 2 ("GPL")in which case the provisions of
  31. * GPL are applicable instead of those above.
  32. *
  33. * If you wish to allow use of your version of this file only under the terms
  34. * of GPL, and not to allow others to use your version of this file under the
  35. * terms of the MIT license, indicate your decision by deleting the provisions
  36. * above and replace them with the notice and other provisions required by GPL
  37. * as set out in the file called "GPLHEADER" included in this distribution. If
  38. * you do not delete the provisions above, a recipient may use your version of
  39. * this file under the terms of either the MIT license or GPL.
  40. *
  41. * This License is also included in this distribution in the file called
  42. * "MIT_COPYING".
  43. *
  44. *****************************************************************************/
  45. #ifndef _VHA_UAPI_H
  46. #define _VHA_UAPI_H
  47. #if defined(__KERNEL__)
  48. #include <linux/ioctl.h>
  49. #include <linux/types.h>
  50. #elif defined(__linux__)
  51. #include <sys/ioctl.h>
  52. #include <inttypes.h>
  53. #else
  54. #error unsupported build
  55. #endif
  56. #if defined(__cplusplus)
  57. extern "C" {
  58. #endif
  59. #define VHA_OCM_MAX_NUM_PAGES 128
  60. #define VHA_CORE_MAX_ALT_ADDRS 16
  61. #define VHA_MAX_CORES 8
  62. // represents OCM types,
  63. #define VHA_LOCAL_OCM 0 /* Local OCM */
  64. #define VHA_SHARED_OCM 1 /* Shared OCM */
  65. #define VHA_OCM_TYPE_MAX 2
  66. /* device hw properties */
  67. struct vha_hw_props {
  68. uint64_t product_id;
  69. uint64_t core_id;
  70. uint64_t soc_axi;
  71. uint8_t mmu_width; /* MMU address width: 40, or 0 if no MMU */
  72. uint8_t mmu_ver; /* MMU version */
  73. uint32_t mmu_pagesize; /* MMU page size */
  74. union {
  75. struct {
  76. unsigned rtm: 1;
  77. unsigned parity: 1;
  78. } supported;
  79. uint8_t features;
  80. };
  81. bool dummy_dev;
  82. bool skip_bvnc_check;
  83. bool use_pdump;
  84. uint8_t num_cnn_core_devs;
  85. uint32_t locm_size_bytes; /* per core */
  86. uint32_t socm_size_bytes; /* total size for all cores */
  87. uint32_t socm_core_size_bytes; /* per core */
  88. uint32_t clock_freq; /* hardware clock rate, kHz */
  89. } __attribute__((aligned(8)));
  90. struct vha_cnn_props {
  91. /* TOBEDONE */
  92. };
  93. /* command sent to device */
  94. enum vha_cmd_type {
  95. VHA_CMD_INVALID = 0x000,
  96. VHA_CMD_CNN_SUBMIT = 0x101,
  97. VHA_CMD_CNN_SUBMIT_MULTI,
  98. VHA_CMD_CNN_PDUMP_MSG
  99. };
  100. /* optional flags for commands */
  101. #define VHA_CMDFLAG_NOTIFY 0x0001 /* send response when cmd complete */
  102. #define VHA_CHECK_CRC 0x0002 /* check the combined CRCs */
  103. #define VHA_EXEC_TIME_SET 0x0004 /* execution time is valid */
  104. /*
  105. * message from user to be sent to VHA (write).
  106. * A command will contain a number of input and ouput buffers
  107. * and some command specific parameters.
  108. * Buffers must be identified by their buffer id.
  109. * All buffer ids must *precede* any other parameters:
  110. * input buf ids,
  111. * output buf ids,
  112. * followed by other parameters.
  113. */
  114. struct vha_user_cmd {
  115. uint32_t cmd_id; /* arbitrary id for cmd */
  116. uint16_t cmd_type; /* enum vha_cmd_type */
  117. uint16_t flags; /* VHA_CMDFLAG_xxx */
  118. uint8_t priority; /* WL priority */
  119. uint8_t padding; /* padding to keep data 32bit aligned */
  120. uint8_t num_bufs; /* total number of buffers */
  121. uint8_t num_inbufs; /* number of input buffers */
  122. uint32_t data[0]; /* 0-N words: input bufids
  123. * followed by other bufids
  124. * followed by other parameters */
  125. };
  126. /* Structure defining hardware issues */
  127. struct vha_hw_brns {
  128. union {
  129. struct {
  130. unsigned bRN71649: 1;
  131. unsigned bRN71556: 1;
  132. unsigned bRN71338: 1;
  133. } bit;
  134. uint64_t map;
  135. };
  136. };
  137. /*
  138. * CNN_SUBMIT message written from user to VHA.
  139. * 3 input buffers: cmdstream, input image, coefficients
  140. * 1 output buffer
  141. * 1 internal buffer (optional)
  142. * offsets into the input and output buffers
  143. * and a register map: this tells the driver which alt-register-N
  144. * will contain the address of which buffer.
  145. */
  146. struct vha_subseg_info {
  147. uint32_t cmdbuf_offset;
  148. uint32_t cmdbuf_size;
  149. };
  150. struct vha_user_cnn_submit_cmd {
  151. struct vha_user_cmd msg;
  152. uint32_t cmdbuf; /* bufid of cmdstream buffer */
  153. uint32_t bufs[VHA_CORE_MAX_ALT_ADDRS]; /* bufid of IN, COEFF, OUT, INTERNAL,CRC,DBG,WB buffers */
  154. uint32_t bufoffsets[VHA_CORE_MAX_ALT_ADDRS]; /* offsets into inbufs and outbufs buffers */
  155. uint32_t bufsizes[VHA_CORE_MAX_ALT_ADDRS]; /* sizes of the inbufs and outbufs buffers */
  156. uint8_t regidx[VHA_CORE_MAX_ALT_ADDRS]; /* register to be used for inbufs and outbufs */
  157. uint32_t onchipram_map_id; /* OCM mapping id - hot pages */
  158. uint32_t onchipram_bufs[VHA_OCM_TYPE_MAX]; /* OCM linear mapping buffers */
  159. uint32_t estimated_cycles; /* estimated number of cycles for this command */
  160. uint64_t expected_ip_capab; /* expected BVNC */
  161. uint64_t hw_brns; /* BRNSs bit map */
  162. uint32_t subseg_num; /* number of subsegments in subseg_info array */
  163. struct vha_subseg_info subseg_info[1]; /* there's always at least one subsegment */
  164. } __attribute__((aligned(8)));
  165. /*
  166. * CNN_SUBMIT_MULTI message written from user to VHA.
  167. * 3 input buffers: cmdstream(s), input image, coefficients
  168. * 1 output buffer
  169. * 1 internal buffer (optional)
  170. * offsets into the input and output buffers
  171. * and a register map: this tells the driver which alt-register-N
  172. * will contain the address of which buffer.
  173. */
  174. struct vha_user_cnn_submit_multi_cmd {
  175. struct vha_user_cmd msg;
  176. uint32_t cmdbuf[VHA_MAX_CORES]; /* bufid of cmdstream buffer */
  177. uint32_t bufs[VHA_CORE_MAX_ALT_ADDRS]; /* bufid of IN, COEFF, OUT, INTERNAL,CRC,DBG,WB buffers */
  178. uint32_t bufoffsets[VHA_CORE_MAX_ALT_ADDRS]; /* offsets into inbufs and outbufs buffers */
  179. uint32_t bufsizes[VHA_CORE_MAX_ALT_ADDRS]; /* sizes of the inbufs and outbufs buffers */
  180. uint8_t regidx[VHA_CORE_MAX_ALT_ADDRS]; /* register to be used for inbufs and outbufs */
  181. uint8_t num_cores; /* number of cores required for this workload */
  182. uint32_t onchipram_bufs[VHA_OCM_TYPE_MAX]; /* OCM linear mapping buffers */
  183. uint32_t crcs[VHA_MAX_CORES]; /* golden CRCs */
  184. uint64_t exec_time; /* expected execution time */
  185. uint32_t shared_circ_buf_offs; /* circular buffer offset in the shared memory */
  186. uint32_t estimated_cycles; /* estimated number of cycles for this command */
  187. uint64_t expected_ip_capab; /* expected BVNC */
  188. uint64_t hw_brns; /* BRNSs bit map */
  189. } __attribute__((aligned(8)));
  190. /*
  191. * response from from kernel module to user.
  192. */
  193. struct vha_user_rsp {
  194. uint32_t cmd_id; /* arbitrary id to identify cmd */
  195. uint32_t err_no; /* 0 if successful, else -ve */
  196. uint64_t rsp_err_flags;
  197. uint32_t data[0]; /* 0-N words of additional info */
  198. };
  199. /*
  200. * response returned after CNN_SUBMIT.
  201. */
  202. struct vha_user_cnn_submit_rsp {
  203. struct vha_user_rsp msg;
  204. uint64_t last_proc_us; /* processing time in us,
  205. measured with system clock */
  206. uint32_t mem_usage; /* device memory used */
  207. uint32_t hw_cycles; /* hardware cycles used */
  208. } __attribute__((aligned(8)));
  209. #define MAX_VHA_USER_RSP_SIZE (sizeof(struct vha_user_cnn_submit_rsp))
  210. /* response returned when querying for heaps */
  211. struct vha_heap_data {
  212. uint32_t id; /* Heap ID */
  213. uint32_t type; /* Heap type */
  214. uint32_t attributes; /* Heap attributes
  215. defining capabilities that user may treat as hint
  216. when selecting the heap id during allocation/importing */
  217. };
  218. #define VHA_MAX_HEAPS 16
  219. struct vha_heaps_data {
  220. struct vha_heap_data heaps[VHA_MAX_HEAPS]; /* [OUT] Heap data */
  221. } __attribute__((aligned(8)));
  222. /* parameters to allocate a device buffer */
  223. struct vha_alloc_data {
  224. uint64_t size; /* [IN] Size of device memory (in bytes) */
  225. uint32_t heap_id; /* [IN] Heap ID of allocator
  226. or VHA_USE_DEFAULT_MEM_HEAP */
  227. uint32_t attributes; /* [IN] Attributes of buffer: img_mem_attr */
  228. char name[8]; /* [IN] short name for buffer */
  229. uint32_t buf_id; /* [OUT] Generated buffer ID */
  230. } __attribute__((aligned(8)));
  231. /* parameters to import a device buffer */
  232. struct vha_import_data {
  233. uint64_t size; /* [IN] Size of device memory (in bytes) */
  234. uint64_t buf_hnd; /* [IN] File descriptor/cpu pointer
  235. of buffer to import */
  236. uint32_t heap_id; /* [IN] Heap ID of allocator */
  237. uint32_t attributes; /* [IN] Attributes of buffer */
  238. char name[8]; /* [IN] short name for buffer */
  239. uint32_t buf_id; /* [OUT] Generated buffer ID */
  240. } __attribute__((aligned(8)));
  241. /* parameters to export a device buffer */
  242. struct vha_export_data {
  243. uint32_t buf_id; /* [IN] Buffer ID to be exported */
  244. uint64_t size; /* [IN] Size to be exported */
  245. uint32_t attributes; /* [IN] Attributes of buffer */
  246. uint64_t buf_hnd; /* [OUT] Buffer handle (file descriptor) */
  247. } __attribute__((aligned(8)));
  248. struct vha_free_data {
  249. uint32_t buf_id; /* [IN] ID of device buffer to free */
  250. };
  251. enum vha_map_flags {
  252. VHA_MAP_FLAG_NONE = 0x0,
  253. VHA_MAP_FLAG_READ_ONLY = 0x1,
  254. VHA_MAP_FLAG_WRITE_ONLY = 0x2,
  255. VHA_MAP_FLAG_IO = 0x4,
  256. VHA_MAP_FLAG_MODEL = 0x8,
  257. };
  258. /* parameters to map a buffer into device */
  259. struct vha_map_to_onchip_data {
  260. uint64_t virt_addr; /* [IN] Device virtual address of a mapping */
  261. uint32_t buf_id; /* [IN] ID of device buffer to map to VHA */
  262. uint32_t page_size; /* [IN] Page size */
  263. uint32_t num_pages; /* [IN] The number of pages to be mapped */
  264. uint32_t page_idxs[VHA_OCM_MAX_NUM_PAGES];
  265. /* [IN] Indexes of pages to be mapped */
  266. uint32_t map_id; /* [IN/OUT] if map_id == 0, creates new mapping
  267. and generates new map_id,
  268. otherwise using existing map_id*/
  269. } __attribute__((aligned(8)));
  270. /* parameters to map a buffer into device */
  271. struct vha_map_data {
  272. uint64_t virt_addr; /* [IN] Device virtual address to map */
  273. uint32_t buf_id; /* [IN] ID of device buffer to map to VHA */
  274. uint32_t flags; /* [IN] Mapping flags, see vha_map_flags */
  275. } __attribute__((aligned(8)));
  276. struct vha_unmap_data {
  277. uint32_t buf_id; /* [IN] ID of device buffer to unmap from VHA */
  278. } __attribute__((aligned(8)));
  279. enum vha_buf_status {
  280. VHA_BUF_UNFILLED,
  281. VHA_BUF_FILLED_BY_SW,
  282. VHA_BUF_FILLED_BY_HW
  283. };
  284. #define VHA_SYNC_NONE (-1)
  285. /* parameters to set buffer status ("filled" or "unfilled") */
  286. struct vha_buf_status_data {
  287. uint32_t buf_id;
  288. uint32_t status; /* enum vha_buf_status */
  289. int in_sync_fd; /* input sync to attach */
  290. bool out_sync_sig; /* output sync signal */
  291. } __attribute__((aligned(8)));
  292. enum vha_sync_op {
  293. VHA_SYNC_OP_CREATE_OUT, /* create output sync_fd */
  294. VHA_SYNC_OP_MERGE_IN, /* merge input sync_fds */
  295. VHA_SYNC_OP_RELEASE /* release syncs */
  296. };
  297. /* parameters to manage sync_fds */
  298. #define VHA_SYNC_MAX_BUF_IDS (VHA_CORE_MAX_ALT_ADDRS)
  299. #define VHA_SYNC_MAX_IN_SYNC_FDS (VHA_CORE_MAX_ALT_ADDRS)
  300. struct vha_sync_create_data {
  301. uint32_t buf_id_count; /* [IN] number of output buffers */
  302. uint32_t buf_ids[VHA_SYNC_MAX_BUF_IDS]; /* [IN] list of output buffer ids */
  303. };
  304. struct vha_sync_merge_data {
  305. uint32_t in_sync_fd_count; /* [IN] number of input sync_fds */
  306. int in_sync_fds[VHA_SYNC_MAX_IN_SYNC_FDS]; /* [IN] list of input sync_fds */
  307. };
  308. struct vha_sync_release_data {
  309. uint32_t buf_id_count; /* [IN] number of buffers */
  310. uint32_t buf_ids[VHA_SYNC_MAX_BUF_IDS]; /* [IN] list of buffer ids */
  311. };
  312. struct vha_sync_data {
  313. enum vha_sync_op op;
  314. union {
  315. struct vha_sync_create_data create_data; /* create output sync_fd data */
  316. struct vha_sync_merge_data merge_data; /* merge input sync_fds data */
  317. struct vha_sync_release_data release_data; /* release syncs data */
  318. };
  319. int sync_fd; /* [OUT] output sync_fd/sync_fd for merged input sync_fds */
  320. } __attribute__((aligned(8)));
  321. struct vha_cancel_data {
  322. uint32_t cmd_id; /* [IN] masked ID of commands to be cancelled */
  323. uint32_t cmd_id_mask; /* [IN] mask for command IDs to be cancelled */
  324. bool respond; /* [IN] if true, respond to this cancel request */
  325. } __attribute__((aligned(8)));
  326. struct vha_version_data {
  327. char digest[33]; /* [OUT] digest of this interface file */
  328. } __attribute__((aligned(8)));
  329. #define VHA_IOC_MAGIC 'q'
  330. #define VHA_IOC_HW_PROPS _IOR(VHA_IOC_MAGIC, 0, struct vha_hw_props)
  331. #define VHA_IOC_QUERY_HEAPS _IOR(VHA_IOC_MAGIC, 1, struct vha_heaps_data)
  332. #define VHA_IOC_ALLOC _IOWR(VHA_IOC_MAGIC, 2, struct vha_alloc_data)
  333. #define VHA_IOC_IMPORT _IOWR(VHA_IOC_MAGIC, 3, struct vha_import_data)
  334. #define VHA_IOC_EXPORT _IOWR(VHA_IOC_MAGIC, 4, struct vha_export_data)
  335. #define VHA_IOC_FREE _IOW(VHA_IOC_MAGIC, 5, struct vha_free_data)
  336. #define VHA_IOC_VHA_MAP_TO_ONCHIP _IOW(VHA_IOC_MAGIC, 6, struct vha_map_to_onchip_data)
  337. #define VHA_IOC_VHA_MAP _IOW(VHA_IOC_MAGIC, 7, struct vha_map_data)
  338. #define VHA_IOC_VHA_UNMAP _IOW(VHA_IOC_MAGIC, 8, struct vha_unmap_data)
  339. #define VHA_IOC_BUF_STATUS _IOW(VHA_IOC_MAGIC, 9, struct vha_buf_status_data)
  340. #define VHA_IOC_SYNC _IOWR(VHA_IOC_MAGIC, 10, struct vha_sync_data)
  341. #define VHA_IOC_CANCEL _IOW(VHA_IOC_MAGIC, 11, struct vha_cancel_data)
  342. #define VHA_IOC_VERSION _IOW(VHA_IOC_MAGIC, 16, struct vha_version_data)
  343. #define VHA_SCOPE_DEV_NAME "vha_scope"
  344. /* vha scope context
  345. * */
  346. struct vha_trace_ctx {
  347. unsigned model_id; /* model id */
  348. unsigned frame_id; /* inference id */
  349. unsigned dev_id; /* device id */
  350. unsigned osid; /* device id */
  351. unsigned pid; /* process id */
  352. unsigned tid; /* thread id */
  353. };
  354. /* Event information, available from vha_info */
  355. struct vha_timing_data {
  356. unsigned evt_type; /* event type */
  357. unsigned seqno; /* continually increments */
  358. unsigned dev_id; /* device id */
  359. unsigned timestamp_lo; /* in microseconds */
  360. unsigned timestamp_hi;
  361. unsigned type; /* either SUBMIT or COMPLETE or ERROR */
  362. unsigned cycles; /* HW cycle count */
  363. unsigned pid; /* process id */
  364. };
  365. enum vha_scope_evt_type {
  366. VHA_EVENT_TIMING,
  367. VHA_EVENT_NUM
  368. };
  369. enum vha_timing_data_type {
  370. VHA_EVENT_TYPE_ENQUEUE,
  371. VHA_EVENT_TYPE_SUBMIT,
  372. VHA_EVENT_TYPE_COMPLETE,
  373. VHA_EVENT_TYPE_ERROR,
  374. VHA_EVENT_TYPE_NUM
  375. };
  376. #if defined(__cplusplus)
  377. }
  378. #endif
  379. #endif /* _VHA_UAPI_H */