vpu_ioctl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (C) 2016 MediaTek Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #ifndef __VPU_IOCTL_H__
  14. #define __VPU_IOCTL_H__
  15. #include <linux/types.h>
  16. #define VPU_DEV_NAME "vpu"
  17. #define VPU_DEV_PATH "/dev/vpu"
  18. #define VPU_MAX_NUM_PORTS 32
  19. #define VPU_MAX_NUM_PROPS 32
  20. #define VPU_MAX_NUM_CORES 3
  21. #define VPU_TRYLOCK_CORENUM 0x87
  22. #define VPU_MAX_NAME_SIZE 32
  23. extern unsigned int efuse_data;
  24. typedef uint8_t vpu_id_t;
  25. /* the last byte of string must be '/0' */
  26. //typedef char vpu_name_t[32];
  27. /**
  28. * Documentation index:
  29. * S1. Introduction
  30. * S2. Requirement
  31. * S3. Sample Use Cases
  32. */
  33. /**
  34. * S1. Introduction
  35. * VPU driver is a transparent platform for data exchange with VPU firmware.
  36. * VPU firmware can dynamically load an algorithm and do image post-processing.
  37. *
  38. * VPU driver implements a model based on aspect of algorithm's requirements.
  39. * An algorithm needs the buffers of input and output, and execution arguments.
  40. * For all mentioned above, VPU driver defines 'Port' to describe the buffers
  41. * of input and output, and 'Info' to describe the specification of algorithm.
  42. * According the 'Port' and 'Info', a user could enque requests for doing
  43. * image post-processing. The diagram is as follows:
  44. *
  45. * +---------------+
  46. * | algo |
  47. * | ------ |
  48. * input port1-> | [info1] | ->output port1
  49. * | [info2] |
  50. * input port2-> | [info...] |
  51. * +---------------+
  52. *
  53. * With Algo's properties, a user can get enough information to do processing,
  54. * and assign the buffers to the matching ports. Moreover, a user algo can
  55. * specify execution arguments to a request.
  56. *
  57. * +------------------------+
  58. * | request |
  59. * | ------------- |
  60. * | [buffer1]=>input port1 |
  61. * | [buffer2]=>input port2 |
  62. * | [buffer3]=>input port3 |
  63. * | [setting1] |
  64. * | [setting2] |
  65. * | [setting...] |
  66. * +------------------------+
  67. *
  68. */
  69. /**
  70. * S2. Requirement
  71. * 1. The processing order is FIFO. User should deque the request in order.
  72. * 2. The buffer address must be accessible by VPU. Use iommu to remap address
  73. * to the specific region.
  74. *
  75. */
  76. /**
  77. * S3. Sample Use Cases
  78. * Provide 4 essential ioctl commands:
  79. * - VPU_IOCTL_GET_ALGO_INFO: get algo's port and info.
  80. *
  81. * struct vpu_algo algo;
  82. * strncpy(algo_n->name, "algo_name", sizeof(algo_n->name));
  83. * ioctl(fd, VPU_IOCTL_GET_ALGO_INFO, algo);
  84. *
  85. * - VPU_IOCTL_ENQUE_REQUEST: enque a request to user?™s own queue.
  86. *
  87. * struct vpu_request req;
  88. * struct vpu_buffer *buf;
  89. * req->algo_id = algo->id;
  90. * req->buffer_count = 1;
  91. * buf = &req->buffers[0];
  92. * buf->format = VPU_BUF_FORMAT_IMG_Y8;
  93. * buf->width = 640;
  94. * buf->height = 360;
  95. * buf->plane_count = 1;
  96. * ioctl(fd, VPU_IOCTL_ENQ_REQUEST, req);
  97. *
  98. * - VPU_IOCTL_DEQUE_REQUEST: wait for request done, and get processing result.
  99. *
  100. * struct vpu_request req;
  101. * ioctl(fd, VPU_IOCTL_DEQUE_REQUEST, req);
  102. *
  103. * - VPU_IOCTL_FLUSH_REQUEST: flush all running request, and return failure if
  104. * not finished
  105. *
  106. * ioctl(fd, VPU_IOCTL_FLUSH_REQUEST, 0);
  107. *
  108. * - VPU_IOCTL_SET_POWER: request power mode and the performance.
  109. *
  110. * struct vpu_power power;
  111. * power.mode = VPU_POWER_MODE_DYNAMIC;
  112. * power.opp = VPU_POWER_OPP_UNREQUEST;
  113. * ioctl(fd, VPU_IOCTL_SET_POWER, power);
  114. *
  115. */
  116. /*---------------------------------------------------------------------------*/
  117. /* VPU Property */
  118. /*---------------------------------------------------------------------------*/
  119. enum vpu_prop_type {
  120. VPU_PROP_TYPE_CHAR,
  121. VPU_PROP_TYPE_INT32,
  122. VPU_PROP_TYPE_INT64,
  123. VPU_PROP_TYPE_FLOAT,
  124. VPU_PROP_TYPE_DOUBLE,
  125. VPU_NUM_PROP_TYPES
  126. };
  127. enum vpu_prop_access {
  128. VPU_PROP_ACCESS_RDONLY,
  129. VPU_PROP_ACCESS_RDWR
  130. };
  131. /*
  132. * The description of properties contains the information about property values,
  133. * which are stored as compact memory. With the offset, it can get the specific
  134. * value from compact data.
  135. *
  136. * The example of struct vpu_prop_desc is as follows:
  137. * +--------+---------------------+--------+--------+-------+--------+
  138. * | id | name | offset | type | count | access |
  139. * +--------+---------------------+--------+--------+-------+--------+
  140. * | 0 | algo_version | 0 | int32 | 1 | RDONLY |
  141. * +--------+---------------------+--------+--------+-------+--------+
  142. * | 1 | field_1 | 4 | int32 | 2 | RDWR |
  143. * +--------+---------------------+--------+--------+-------+--------+
  144. * | 2 | field_2 | 12 | int64 | 1 | RDWR |
  145. * +--------+---------------------+--------+--------+-------+--------+
  146. *
  147. * Use a buffer to store all property data, which is a compact-format data.
  148. * The buffer's layout is described by prop_desc, using the offset could
  149. * get the specific data.
  150. *
  151. * The example of compact-format memory is as follows:
  152. * +--------+--------+--------+--------+--------+
  153. * | 0~3 | 4~7 | 8~11 | 12~15 | 16~23 |
  154. * +--------+--------+--------+--------+--------+
  155. * |alg_vers| field_1 | field_2 |
  156. * +--------+--------+--------+--------+--------+
  157. *
  158. */
  159. struct vpu_prop_desc {
  160. vpu_id_t id;
  161. uint8_t type; /* the property's data type */
  162. uint8_t access; /* directional data exchange */
  163. uint32_t offset; /* offset = previous offset + previous size */
  164. uint32_t count; /* size = sizeof(type) x count */
  165. char name[VPU_MAX_NAME_SIZE];
  166. };
  167. /*---------------------------------------------------------------------------*/
  168. /* VPU Ports */
  169. /*---------------------------------------------------------------------------*/
  170. enum vpu_port_usage {
  171. VPU_PORT_USAGE_IMAGE,
  172. VPU_PORT_USAGE_DATA,
  173. VPU_NUM_PORT_USAGES
  174. };
  175. enum vpu_port_dir {
  176. VPU_PORT_DIR_IN,
  177. VPU_PORT_DIR_OUT,
  178. VPU_PORT_DIR_IN_OUT,
  179. VPU_NUM_PORT_DIRS
  180. };
  181. /*
  182. * The ports contains the information about algorithm's input and output.
  183. * The each buffer on the vpu_request should be assigned a port id,
  184. * to let algorithm recognize every buffer's purpose.
  185. *
  186. * The example of vpu_port table is as follows:
  187. * +--------+---------------------+--------+--------+
  188. * | id | name | type | dir |
  189. * +--------+---------------------+--------+--------+
  190. * | 0 | image-in | IMAGE | IN |
  191. * +--------+---------------------+--------+--------+
  192. * | 1 | data-in | DATA | IN |
  193. * +--------+---------------------+--------+--------+
  194. * | 2 | image-out | IMAGE | OUT |
  195. * +--------+---------------------+--------+--------+
  196. * | 3 | image-temp | IMAGE | INOUT |
  197. * +--------+---------------------+--------+--------+
  198. *
  199. */
  200. struct vpu_port {
  201. vpu_id_t id;
  202. uint8_t usage;
  203. uint8_t dir;
  204. char name[VPU_MAX_NAME_SIZE];
  205. };
  206. /*---------------------------------------------------------------------------*/
  207. /* VPU Algo */
  208. /*---------------------------------------------------------------------------*/
  209. struct vpu_algo {
  210. vpu_id_t id[VPU_MAX_NUM_CORES];
  211. uint8_t port_count;
  212. uint8_t info_desc_count;
  213. uint8_t sett_desc_count;
  214. uint32_t info_length; /* the size of info data buffer */
  215. uint32_t sett_length;
  216. uint32_t bin_length;
  217. uint64_t info_ptr; /* the pointer to info data buffer */
  218. /* mva of algo bin, which is accessible by VPU */
  219. uint64_t bin_ptr;
  220. char name[VPU_MAX_NAME_SIZE];
  221. struct vpu_prop_desc info_descs[VPU_MAX_NUM_PROPS];
  222. struct vpu_prop_desc sett_descs[VPU_MAX_NUM_PROPS];
  223. struct vpu_port ports[VPU_MAX_NUM_PORTS];
  224. };
  225. struct vpu_create_algo {
  226. uint32_t core;
  227. char name[VPU_MAX_NAME_SIZE];
  228. uint32_t algo_length;
  229. uint64_t algo_ptr;
  230. };
  231. /*---------------------------------------------------------------------------*/
  232. /* VPU Register */
  233. /*---------------------------------------------------------------------------*/
  234. struct vpu_reg_value {
  235. uint32_t field;
  236. uint32_t value;
  237. };
  238. struct vpu_reg_values {
  239. uint8_t reg_count;
  240. struct vpu_reg_value *regs;
  241. };
  242. /*---------------------------------------------------------------------------*/
  243. /* VPU Power */
  244. /*---------------------------------------------------------------------------*/
  245. /*
  246. * Provide two power modes:
  247. * - dynamic: power-saving mode, it's on request to power on device.
  248. * - on: power on immediately
  249. */
  250. enum vpu_power_mode {
  251. VPU_POWER_MODE_DYNAMIC,
  252. VPU_POWER_MODE_ON,
  253. };
  254. /*
  255. * Provide a set of OPPs(operation performance point)
  256. * The default opp is at the minimun performance,
  257. * and users could request the performance.
  258. */
  259. enum vpu_power_opp {
  260. VPU_POWER_OPP_UNREQUEST = 0xFF,
  261. };
  262. struct vpu_power {
  263. uint8_t opp_step;
  264. uint8_t freq_step;
  265. uint32_t bw; /* unit: MByte/s */
  266. /* align with core index defined in user space header file */
  267. unsigned int core;
  268. uint8_t boost_value;
  269. };
  270. /*---------------------------------------------------------------------------*/
  271. /* VPU Plane */
  272. /*---------------------------------------------------------------------------*/
  273. struct vpu_plane {
  274. uint32_t stride; /* if buffer type is image */
  275. uint32_t length;
  276. uint64_t ptr; /* mva which is accessible by VPU */
  277. };
  278. enum vpu_buf_format {
  279. VPU_BUF_FORMAT_DATA,
  280. VPU_BUF_FORMAT_IMG_Y8,
  281. VPU_BUF_FORMAT_IMG_YV12,
  282. VPU_BUF_FORMAT_IMG_NV21,
  283. VPU_BUF_FORMAT_IMG_YUY2,
  284. VPU_BUF_FORMAT_IMPL_DEFINED = 0xFF,
  285. };
  286. struct vpu_buffer {
  287. vpu_id_t port_id;
  288. uint8_t format;
  289. uint8_t plane_count;
  290. uint32_t width;
  291. uint32_t height;
  292. struct vpu_plane planes[3];
  293. };
  294. enum vpu_req_status {
  295. VPU_REQ_STATUS_SUCCESS,
  296. VPU_REQ_STATUS_BUSY,
  297. VPU_REQ_STATUS_TIMEOUT,
  298. VPU_REQ_STATUS_INVALID,
  299. VPU_REQ_STATUS_FLUSH,
  300. VPU_REQ_STATUS_FAILURE,
  301. };
  302. /*3 prioritys of req*/
  303. #define VPU_REQ_MAX_NUM_PRIORITY 21
  304. struct vpu_request {
  305. /* to recognize the request is from which user */
  306. unsigned long *user_id;
  307. /* to recognize the request object id for unorder enque/deque
  308. * procedure
  309. */
  310. uint64_t request_id;
  311. /* core index that user want to run the request on */
  312. unsigned int requested_core;
  313. /* the final occupied core index for request,
  314. * especially for request in common pool
  315. */
  316. unsigned int occupied_core;
  317. vpu_id_t algo_id[VPU_MAX_NUM_CORES];
  318. int frame_magic; /* mapping for user space/kernel space */
  319. uint8_t status;
  320. uint8_t buffer_count;
  321. uint32_t sett_length;
  322. uint64_t sett_ptr; /* pointer to the request setting */
  323. uint64_t priv; /* reserved for user */
  324. struct vpu_buffer buffers[VPU_MAX_NUM_PORTS];
  325. /* driver usage only, fd in user space / ion handle in kernel */
  326. uint64_t buf_ion_infos[VPU_MAX_NUM_PORTS * 3];
  327. struct vpu_power power_param;
  328. uint64_t busy_time;
  329. uint32_t bandwidth;
  330. uint8_t priority;
  331. };
  332. struct vpu_status {
  333. int vpu_core_index;
  334. bool vpu_core_available;
  335. int pool_list_size;
  336. };
  337. struct vpu_dev_debug_info {
  338. int dev_fd;
  339. char callername[VPU_MAX_NAME_SIZE];
  340. pid_t open_pid;
  341. pid_t open_tgid;
  342. };
  343. enum VPU_OPP_PRIORIYY {
  344. DEBUG = 0,
  345. THERMAL = 1,
  346. POWER_HAL = 2,
  347. EARA_QOS = 3,
  348. NORMAL = 4,
  349. VPU_OPP_PRIORIYY_NUM
  350. };
  351. struct vpu_lock_power {
  352. /* align with core index defined in user space header file*/
  353. unsigned int core;
  354. uint8_t max_boost_value;
  355. uint8_t min_boost_value;
  356. bool lock;
  357. enum VPU_OPP_PRIORIYY priority;
  358. };
  359. #ifdef CONFIG_GZ_SUPPORT_SDSP
  360. extern int mtee_sdsp_enable(u32 on);
  361. #endif
  362. /*---------------------------------------------------------------------------*/
  363. /* VPU BUF */
  364. /*---------------------------------------------------------------------------*/
  365. enum vbuf_alloc_type {
  366. VBUF_TYPE_ALLOC,
  367. VBUF_TYPE_IMPORT,
  368. };
  369. enum vbuf_sync_dir {
  370. VBUF_SYNC_TO_DEVICE,
  371. VBUF_SYNC_TO_CPU,
  372. };
  373. struct vbuf_info {
  374. uint32_t method_sel;
  375. };
  376. struct vbuf_alloc {
  377. uint32_t type; /* alloc or import */
  378. uint32_t req_size; /* Type: alloc */
  379. uint32_t cache_flag; /* Type: alloc, to enable cache */
  380. uint32_t exp_flag; /* Type: alloc, to get exp_fd */
  381. uint32_t imp_fd; /* Type: import */
  382. uint64_t handle; /* output */
  383. uint32_t buf_size; /* output */
  384. uint32_t iova_addr; /* output */
  385. uint32_t iova_size; /* output */
  386. uint32_t exp_fd; /* output */
  387. };
  388. struct vbuf_free {
  389. uint64_t handle;
  390. };
  391. struct vbuf_sync {
  392. uint64_t handle;
  393. uint32_t direction;
  394. };
  395. /*---------------------------------------------------------------------------*/
  396. /* IOCTL Command */
  397. /*---------------------------------------------------------------------------*/
  398. #define VPU_MAGICNO 'v'
  399. #define VPU_IOCTL_SET_POWER _IOW(VPU_MAGICNO, 0, int)
  400. #define VPU_IOCTL_ENQUE_REQUEST _IOW(VPU_MAGICNO, 1, int)
  401. #define VPU_IOCTL_DEQUE_REQUEST _IOWR(VPU_MAGICNO, 2, int)
  402. #define VPU_IOCTL_FLUSH_REQUEST _IOW(VPU_MAGICNO, 3, int)
  403. #define VPU_IOCTL_GET_ALGO_INFO _IOWR(VPU_MAGICNO, 4, int)
  404. #define VPU_IOCTL_LOCK _IOW(VPU_MAGICNO, 5, int)
  405. #define VPU_IOCTL_UNLOCK _IOW(VPU_MAGICNO, 6, int)
  406. #define VPU_IOCTL_LOAD_ALG_TO_POOL _IOW(VPU_MAGICNO, 7, int)
  407. #define VPU_IOCTL_REG_WRITE _IOW(VPU_MAGICNO, 8, int)
  408. #define VPU_IOCTL_REG_READ _IOWR(VPU_MAGICNO, 9, int)
  409. #define VPU_IOCTL_GET_CORE_STATUS _IOWR(VPU_MAGICNO, 10, int)
  410. #define VPU_IOCTL_OPEN_DEV_NOTICE _IOWR(VPU_MAGICNO, 11, int)
  411. #define VPU_IOCTL_CLOSE_DEV_NOTICE _IOWR(VPU_MAGICNO, 12, int)
  412. #define VPU_IOCTL_EARA_LOCK_POWER _IOW(VPU_MAGICNO, 13, int)
  413. #define VPU_IOCTL_POWER_HAL_LOCK_POWER _IOW(VPU_MAGICNO, 14, int)
  414. #define VPU_IOCTL_EARA_UNLOCK_POWER _IOW(VPU_MAGICNO, 15, int)
  415. #define VPU_IOCTL_POWER_HAL_UNLOCK_POWER _IOW(VPU_MAGICNO, 16, int)
  416. #define VPU_IOCTL_CREATE_ALGO _IOWR(VPU_MAGICNO, 17, int)
  417. #define VPU_IOCTL_FREE_ALGO _IOWR(VPU_MAGICNO, 18, int)
  418. #define VPU_IOCTL_VBUF_INFO _IOWR(VPU_MAGICNO, 51, \
  419. struct vbuf_info)
  420. #define VPU_IOCTL_VBUF_ALLOC _IOWR(VPU_MAGICNO, 52, \
  421. struct vbuf_alloc)
  422. #define VPU_IOCTL_VBUF_FREE _IOWR(VPU_MAGICNO, 53, \
  423. struct vbuf_free)
  424. #define VPU_IOCTL_VBUF_SYNC _IOWR(VPU_MAGICNO, 54, \
  425. struct vbuf_sync)
  426. #define VPU_IOCTL_SDSP_SEC_LOCK _IOW(VPU_MAGICNO, 60, int)
  427. #define VPU_IOCTL_SDSP_SEC_UNLOCK _IOW(VPU_MAGICNO, 61, int)
  428. #endif