vg_lite_kernel.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2014 - 2022 Vivante Corporation
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (C) 2014 - 2022 Vivante Corporation
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software Foundation,
  43. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. *
  45. *****************************************************************************
  46. *
  47. * Note: This software is released under dual MIT and GPL licenses. A
  48. * recipient may use this file under the terms of either the MIT license or
  49. * GPL License. If you wish to use only one license not the other, you can
  50. * indicate your decision by deleting one of the above license notices in your
  51. * version of this file.
  52. *
  53. *****************************************************************************/
  54. #ifndef _vg_lite_kernel_h_
  55. #define _vg_lite_kernel_h_
  56. #include "../VGLite/vg_lite_options.h"
  57. #include "vg_lite_option.h"
  58. /* Interrupt IDs from GPU. */
  59. #define EVENT_UNEXPECTED_MESH 0x80000000
  60. #define EVENT_CMD_BAD_WRITE 0x40000000
  61. #define EVENT_ERROR_RECOVER 0x20000000
  62. #define EVENT_CMD_SWITCH 0x10000000
  63. #define EVENT_MCU_BAD_WRITE 0x08000000
  64. #define EVENT_END 0
  65. #define EVENT_FRAME_END 1
  66. #define MAX_CONTIGUOUS_SIZE 0x04000000
  67. #define VG_LITE_INFINITE 0xFFFFFFFF
  68. #if gcFEATURE_VG_SINGLE_COMMAND_BUFFER
  69. #define CMDBUF_COUNT 1
  70. #else
  71. #define CMDBUF_COUNT 2
  72. #endif
  73. #define VG_LITE_ALIGN(number, alignment) \
  74. (((number) + ((alignment) - 1)) & ~((alignment) - 1))
  75. #ifndef BIT
  76. #define BIT(x) (1 << x)
  77. #endif
  78. #define VG_LITE_KERNEL_IS_GPU_IDLE() \
  79. ((vg_lite_hal_peek(VG_LITE_HW_IDLE) & VG_LITE_HW_IDLE_STATE) == VG_LITE_HW_IDLE_STATE)
  80. /* Hardware chip Ids */
  81. #define GPU_CHIP_ID_GCNANOLITEV 0x255
  82. #define GPU_CHIP_ID_GC355 0x355
  83. #define GPU_CHIP_ID_GCNANOULTRAV 0x265
  84. /* vg_lite_kernel_map_t flag type */
  85. #define VG_LITE_HAL_MAP_DMABUF 0x00000004
  86. #define VG_LITE_HAL_MAP_USER_MEMORY 0x00000008
  87. #define VG_LITE_HAL_ALLOC_4G 0x00000010
  88. /* vg_lite_kernel_allocate_t flag type */
  89. #define VG_LITE_RESERVED_ALLOCATOR 0x10000000
  90. #define VG_LITE_GFP_ALLOCATOR 0x20000000
  91. #define VG_LITE_DMA_ALLOCATOR 0x40000000
  92. #define VG_LITE_MEMORY_ALLOCATOR_FLAG 0x70000000
  93. #ifdef __cplusplus
  94. extern "C" {
  95. #endif
  96. #ifndef VG_LITE_ERROR
  97. #define VG_LITE_ERROR 1
  98. /*!
  99. @abstract Error codes that the vg_lite functions can return.
  100. @discussion
  101. All API functions return a status code. On success, VG_LITE_SUCCESS will be returned when a function is
  102. successful. This value is set to zero, so if any function returns a non-zero value, an error has occurred.
  103. */
  104. typedef enum vg_lite_error
  105. {
  106. VG_LITE_SUCCESS = 0, /*! Success. */
  107. VG_LITE_INVALID_ARGUMENT, /*! An invalid argument was specified. */
  108. VG_LITE_OUT_OF_MEMORY, /*! Out of GPU memory */
  109. VG_LITE_NO_CONTEXT, /*! No context or an unintialized context specified. */
  110. VG_LITE_TIMEOUT, /*! A timeout has occurred during a wait. */
  111. VG_LITE_OUT_OF_RESOURCES, /*! Out of system resources. */
  112. VG_LITE_GENERIC_IO, /*! Cannot communicate with the kernel driver. */
  113. VG_LITE_NOT_SUPPORT, /*! Function call not supported. */
  114. VG_LITE_ALREADY_EXISTS, /*! Object already exists */
  115. VG_LITE_NOT_ALIGNED, /*! Data alignment error */
  116. VG_LITE_FLEXA_TIME_OUT, /*! VG timeout requesting for segment buffer */
  117. VG_LITE_FLEXA_HANDSHAKE_FAIL, /*! VG and SBI synchronizer handshake failed */
  118. VG_LITE_SYSTEM_CALL_FAIL, /*! kernel api call fail */
  119. }
  120. vg_lite_error_t;
  121. #endif
  122. typedef enum vg_lite_kernel_counter
  123. {
  124. /* Dont't touch the counter. */
  125. VG_LITE_NONE,
  126. /* Turn the counter on. */
  127. VG_LITE_ON,
  128. /* Turn the counter off. */
  129. VG_LITE_OFF,
  130. /* Query the counter and reset its values. */
  131. VG_LITE_QUERY,
  132. }
  133. vg_lite_kernel_counter_t;
  134. typedef enum vg_lite_kernel_command
  135. {
  136. /* Initialize the GPU. */
  137. VG_LITE_INITIALIZE,
  138. /* Terminate the GPU. */
  139. VG_LITE_TERMINATE,
  140. /* Allocate memory. */
  141. VG_LITE_ALLOCATE,
  142. /* Free memory. */
  143. VG_LITE_FREE,
  144. /* Submit a command buffer to the GPU. */
  145. VG_LITE_SUBMIT,
  146. /* Wait for the GPU to be completed. */
  147. VG_LITE_WAIT,
  148. /* Reset the GPU. */
  149. VG_LITE_RESET,
  150. /* Debug commands. */
  151. VG_LITE_DEBUG,
  152. /* Map memory. */
  153. VG_LITE_MAP,
  154. /* Unmap memory. */
  155. VG_LITE_UNMAP,
  156. /* Check info. */
  157. VG_LITE_CHECK,
  158. /* Query mem. */
  159. VG_LITE_QUERY_MEM,
  160. /* Flexa disable */
  161. VG_LITE_FLEXA_DISABLE,
  162. /* Flexa enable */
  163. VG_LITE_FLEXA_ENABLE,
  164. /* Flexa stop frame */
  165. VG_LITE_FLEXA_STOP_FRAME,
  166. /* Set background address */
  167. VG_LITE_FLEXA_SET_BACKGROUND_ADDRESS,
  168. /* Map memory to user */
  169. VG_LITE_MAP_MEMORY,
  170. /* Unmap memory to user */
  171. VG_LITE_UNMAP_MEMORY,
  172. /* Close gpu */
  173. VG_LITE_CLOSE,
  174. /* Operation cache */
  175. VG_LITE_CACHE,
  176. /* Export memory */
  177. VG_LITE_EXPORT_MEMORY,
  178. /* Record GPU hardware running time */
  179. VG_LITE_RECORD_RUNNING_TIME,
  180. /* Start delay resume state */
  181. VG_LITE_START_DELAY_RESUME,
  182. /* Query delay resume state */
  183. VG_LITE_QUERY_DELAY_RESUME,
  184. /* Stop delay resume state */
  185. VG_LITE_STOP_DELAY_RESUME,
  186. }
  187. vg_lite_kernel_command_t;
  188. typedef enum vg_lite_cache_op {
  189. VG_LITE_CACHE_CLEAN,
  190. VG_LITE_CACHE_INVALIDATE,
  191. VG_LITE_CACHE_FLUSH,
  192. }
  193. vg_lite_cache_op_t;
  194. typedef enum vg_lite_vidmem_pool {
  195. VG_LITE_POOL_RESERVED_MEMORY1 = 0,
  196. VG_LITE_POOL_RESERVED_MEMORY2 = 1,
  197. }
  198. vg_lite_vidmem_pool_t;
  199. typedef enum vg_lite_gpu_execute_state {
  200. VG_LITE_GPU_STOP = 0,
  201. VG_LITE_GPU_RUN = 1,
  202. }
  203. vg_lite_gpu_execute_state_t;
  204. /* Context structure. */
  205. typedef struct vg_lite_kernel_context {
  206. /* Command buffer. */
  207. void *command_buffer[CMDBUF_COUNT];
  208. void *command_buffer_logical[CMDBUF_COUNT];
  209. void *command_buffer_klogical[CMDBUF_COUNT];
  210. uint32_t command_buffer_physical[CMDBUF_COUNT];
  211. uint32_t end_of_frame;
  212. /* Tessellation buffer. */
  213. void *tess_buffer;
  214. void *tessbuf_logical;
  215. void *tessbuf_klogical;
  216. uint32_t tessbuf_physical;
  217. /* power context buffer */
  218. void *power_context;
  219. void *power_context_logical;
  220. void *power_context_klogical;
  221. uint32_t power_context_physical;
  222. uint32_t power_context_size;
  223. uint32_t power_context_capacity;
  224. }
  225. vg_lite_kernel_context_t;
  226. typedef struct capabilities
  227. {
  228. uint32_t tiled : 2;
  229. uint32_t l2_cache : 1;
  230. }
  231. capabilities_t;
  232. typedef union vg_lite_capabilities
  233. {
  234. capabilities_t cap;
  235. uint32_t data;
  236. }
  237. vg_lite_capabilities_t;
  238. typedef struct vg_lite_kernel_initialize
  239. {
  240. /* INPUT */
  241. /* Command buffer size. */
  242. uint32_t command_buffer_size;
  243. /* Tessellation buffer width. */
  244. int32_t tess_width;
  245. /* Tessellation buffer height. */
  246. int32_t tess_height;
  247. /* Memory pool for command buffer. */
  248. vg_lite_vidmem_pool_t command_buffer_pool;
  249. /* Memory pool for tessellation buffer. */
  250. vg_lite_vidmem_pool_t tess_buffer_pool;
  251. /* OUTPUT */
  252. /* Context pointer. */
  253. vg_lite_kernel_context_t * context;
  254. /* Capabilities. */
  255. vg_lite_capabilities_t capabilities;
  256. /* Allocated command buffer. */
  257. void * command_buffer[CMDBUF_COUNT];
  258. /* GPU address for command buffer. */
  259. uint32_t command_buffer_gpu[CMDBUF_COUNT];
  260. /* GPU addresses for tesselation buffers. */
  261. uint32_t physical_addr;
  262. /* Logic addresses for tessellation buffers: used by SW Tessellator. */
  263. uint8_t *logical_addr;
  264. /* Size of each level of the tesselation buffer. */
  265. uint32_t tessbuf_size;
  266. /* Size of each level of the vg count buffer. */
  267. uint32_t countbuf_size;
  268. /* Width and height of tessellation buffer. */
  269. uint32_t tess_w_h;
  270. }
  271. vg_lite_kernel_initialize_t;
  272. typedef struct vg_lite_kernel_terminate
  273. {
  274. /* Context to terminate. */
  275. vg_lite_kernel_context_t * context;
  276. }
  277. vg_lite_kernel_terminate_t;
  278. typedef struct vg_lite_kernel_allocate
  279. {
  280. /* INPUT */
  281. /* Number of bytes to allocate. */
  282. uint32_t bytes;
  283. /* Flag to indicate whether the allocated memory is contiguous or not. */
  284. int32_t contiguous;
  285. /* Flag to indicate where to allocate memory */
  286. uint32_t flags;
  287. /* select reserved memory pool */
  288. vg_lite_vidmem_pool_t pool;
  289. /* OUTPUT */
  290. /* Memory handle. */
  291. void * memory_handle;
  292. /* Allocated memory. */
  293. void * memory;
  294. /* kernel memory */
  295. void * kmemory;
  296. /* GPU address of allocated memory. */
  297. uint32_t memory_gpu;
  298. }
  299. vg_lite_kernel_allocate_t;
  300. typedef struct vg_lite_kernel_free
  301. {
  302. /* Memory handle to free. */
  303. void * memory_handle;
  304. }
  305. vg_lite_kernel_free_t;
  306. typedef struct vg_lite_kernel_submit
  307. {
  308. /* Context to submit to. */
  309. vg_lite_kernel_context_t * context;
  310. /* Pointer to command buffer. */
  311. void * commands;
  312. /* Number of bytes in command buffer. */
  313. uint32_t command_size;
  314. /* Command Buffer ID. */
  315. uint32_t command_id;
  316. }
  317. vg_lite_kernel_submit_t;
  318. typedef enum vg_lite_gpu_reset_type
  319. {
  320. RESTORE_INIT_COMMAND = 0,
  321. RESTORE_LAST_COMMAND = 1,
  322. RESTORE_ALL_COMMAND = 2,
  323. RESTORE_NONE = 3,
  324. }
  325. vg_lite_gpu_reset_type_t;
  326. typedef struct vg_lite_kernel_wait
  327. {
  328. /* Context to wait for. */
  329. vg_lite_kernel_context_t * context;
  330. /* Timeout in milliseconds. */
  331. uint32_t timeout_ms;
  332. /* The event to wait. */
  333. uint32_t event_mask;
  334. /* The event(s) got after waiting. */
  335. uint32_t event_got;
  336. /* After GPU reset, select submit command */
  337. vg_lite_gpu_reset_type_t reset_type;
  338. }
  339. vg_lite_kernel_wait_t;
  340. typedef struct vg_lite_kernel_reset
  341. {
  342. /* Context to reset. */
  343. vg_lite_kernel_context_t * context;
  344. uint32_t delay_resume_flag;
  345. }
  346. vg_lite_kernel_reset_t;
  347. typedef struct vg_lite_kernel_debug
  348. {
  349. /* Context to debug. */
  350. vg_lite_kernel_context_t * context;
  351. /* Bandwidth counter enabler. */
  352. vg_lite_kernel_counter_t bandwidth_counter;
  353. /* Pixel counter enabler. */
  354. vg_lite_kernel_counter_t pixel_counters;
  355. /* OUTPUT */
  356. /* Bandwidth counters:
  357. * [0] - burst of 8.
  358. * [1] - burst of 16.
  359. * [2] - burst of 32.
  360. * [3] - burst of 64.
  361. */
  362. uint32_t bandwidth[4];
  363. /* Pixel counters:.
  364. * [0] - Number of tessellated pixels.
  365. * [1] - Number of imaged pixels.
  366. * [2] - Number of rendered pixels.
  367. */
  368. uint32_t pixels[3];
  369. }
  370. vg_lite_kernel_debug_t;
  371. typedef struct vg_lite_kernel_map
  372. {
  373. /* INPUT */
  374. uint32_t flags;
  375. /* user memory */
  376. /* Number of bytes to map. */
  377. uint32_t bytes;
  378. /* Logical memory address or NULL. */
  379. void * logical;
  380. /* Physical memory address or 0. */
  381. uint32_t physical;
  382. /* dma_buf */
  383. /* dma_buf fd */
  384. int32_t dma_buf_fd;
  385. /* OUTPUT */
  386. /* Memory handle for mapped memory. */
  387. void * memory_handle;
  388. /* GPU address of mapped memory. */
  389. uint32_t memory_gpu;
  390. }
  391. vg_lite_kernel_map_t;
  392. typedef struct vg_lite_kernel_unmap
  393. {
  394. /* Memory handle to unmap. */
  395. void * memory_handle;
  396. }
  397. vg_lite_kernel_unmap_t;
  398. typedef struct vg_lite_kernel_cache
  399. {
  400. vg_lite_cache_op_t cache_op;
  401. /* Memory handle to operation. */
  402. void * memory_handle;
  403. }
  404. vg_lite_kernel_cache_t;
  405. typedef struct vg_lite_kernel_info
  406. {
  407. /* Register's address. */
  408. uint32_t addr;
  409. /* Check register info. */
  410. uint32_t reg;
  411. }
  412. vg_lite_kernel_info_t;
  413. typedef struct vg_lite_kernel_flexa_info
  414. {
  415. uint32_t sbi_mode;
  416. uint32_t sync_mode;
  417. uint32_t flexa_mode;
  418. uint32_t stream_id;
  419. uint32_t segment_address;
  420. uint32_t segment_count;
  421. uint32_t segment_size;
  422. uint32_t stop_flag;
  423. uint32_t start_flag;
  424. uint32_t reset_flag;
  425. }
  426. vg_lite_kernel_flexa_info_t;
  427. typedef struct vg_lite_kernel_mem
  428. {
  429. uint32_t bytes;
  430. vg_lite_vidmem_pool_t pool;
  431. }
  432. vg_lite_kernel_mem_t;
  433. typedef struct vg_lite_kernel_map_memory
  434. {
  435. /* Number of bytes to map. */
  436. uint32_t bytes;
  437. /* Physical memory address. */
  438. uint32_t physical;
  439. /* Logical memory address. */
  440. void * logical;
  441. }
  442. vg_lite_kernel_map_memory_t;
  443. typedef struct vg_lite_kernel_unmap_memory
  444. {
  445. /* Number of bytes to map. */
  446. uint32_t bytes;
  447. /* Logical memory address. */
  448. void * logical;
  449. }
  450. vg_lite_kernel_unmap_memory_t;
  451. typedef struct vg_lite_kernel_close
  452. {
  453. vg_lite_kernel_context_t * context;
  454. }
  455. vg_lite_kernel_close_t;
  456. typedef struct vg_lite_kernel_export_memory
  457. {
  458. int32_t fd;
  459. }
  460. vg_lite_kernel_export_memory_t;
  461. typedef struct vg_lite_kernel_hardware_running_time
  462. {
  463. unsigned long run_time;
  464. int32_t hertz;
  465. }
  466. vg_lite_kernel_hardware_running_time_t;
  467. typedef struct vg_lite_kernel_delay_resume
  468. {
  469. uint32_t set_delay_resume;
  470. uint32_t query_delay_resume;
  471. }
  472. vg_lite_kernel_delay_resume_t;
  473. typedef struct vg_lite_kernel_gpu_clock_state
  474. {
  475. uint32_t state;
  476. }
  477. vg_lite_kernel_gpu_clock_state_t;
  478. vg_lite_error_t vg_lite_kernel(vg_lite_kernel_command_t command, void * data);
  479. vg_lite_error_t record_running_time(void);
  480. extern uint32_t init_buffer[12];
  481. extern uint32_t is_init;
  482. extern size_t physical_address;
  483. #ifdef __cplusplus
  484. }
  485. #endif
  486. #endif /* _vg_lite_kernel_h_ */