1 /****************************************************************************
2 *
3 *    The MIT License (MIT)
4 *
5 *    Copyright (c) 2014 - 2020 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 #ifndef _vg_lite_kernel_h_
28 #define _vg_lite_kernel_h_
29 
30 #include "vg_lite_os.h"
31 
32 /* Interrupt IDs from GPU. */
33 #define EVENT_UNEXPECTED_MESH  0x80000000
34 #define EVENT_CMD_BAD_WRITE    0x40000000
35 #define EVENT_ERROR_RECOVER    0x20000000
36 #define EVENT_CMD_SWITCH       0x10000000
37 #define EVENT_MCU_BAD_WRITE    0x08000000
38 #define EVENT_END              0
39 
40 #define MAX_CONTIGUOUS_SIZE 0x02000000
41 
42 #define VG_LITE_INFINITE       0xFFFFFFFF
43 #define VG_LITE_MAX_WAIT_TIME  0x130000
44 #define CMDBUF_COUNT        2
45 
46 #define VG_LITE_ALIGN(number, alignment)    \
47         (((number) + ((alignment) - 1)) & ~((alignment) - 1))
48 
49 /* Available function optimization levels */
50 #if (defined(__ICCARM__))
51 #define VG_LITE_ATTR_OPTIMIZE_LOW               _Pragma("optimize=none")
52 #define VG_LITE_ATTR_OPTIMIZE_MEDIUM            _Pragma("optimize=medium")
53 #define VG_LITE_ATTR_OPTIMIZE_HIGH              _Pragma("optimize=high")
54 #else /* ARMGCC */
55 #define VG_LITE_ATTR_OPTIMIZE_LOW               __attribute__((optimize(1)))
56 #define VG_LITE_ATTR_OPTIMIZE_MEDIUM            __attribute__((optimize(2)))
57 #define VG_LITE_ATTR_OPTIMIZE_HIGH              __attribute__((optimize(3)))
58 #endif /* defined(__ICCARM__) */
59 
60 /* Allow develpers to force a function optimization level */
61 #define VG_LITE_OPTIMIZE(level)                 VG_LITE_ATTR_OPTIMIZE_##level
62 
63 #define VG_LITE_KERNEL_IS_GPU_IDLE() \
64 ((vg_lite_hal_peek(VG_LITE_HW_IDLE) & VG_LITE_HW_IDLE_STATE) == VG_LITE_HW_IDLE_STATE)
65 
66 /* Hardware chip Ids */
67 #define GPU_CHIP_ID_GCNanoliteV         0x255
68 #define GPU_CHIP_ID_GC355               0x355
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 #ifndef VG_LITE_ERROR
75 #define VG_LITE_ERROR  1
76 /*!
77     @abstract Error codes that the vg_lite functions can return.
78 
79     @discussion
80     All API functions return a status code. On success, <code>VG_LITE_SUCCESS</code> will be returned when a function is
81     successful. This value is set to zero, so if any function returns a non-zero value, an error has occured.
82     */
83 typedef enum vg_lite_error
84 {
85     VG_LITE_SUCCESS = 0,        /*! Success. */
86     VG_LITE_INVALID_ARGUMENT,   /*! An invalid argument was specified. */
87     VG_LITE_OUT_OF_MEMORY,      /*! Out of memory. */
88     VG_LITE_NO_CONTEXT,         /*! No context or an unintialized context specified. */
89     VG_LITE_TIMEOUT,            /*! A timeout has occured during a wait. */
90     VG_LITE_OUT_OF_RESOURCES,   /*! Out of system resources. */
91     VG_LITE_GENERIC_IO,         /*! Cannot communicate with the kernel driver. */
92     VG_LITE_NOT_SUPPORT,        /*! Function call not supported. */
93     VG_LITE_MULTI_THREAD_FAIL,  /*! Multi-thread/tasks fail. */
94     VG_LITE_ALREADY_EXISTS,     /*! Object already exists */
95     VG_LITE_NOT_ALIGNED,        /*! Data alignment error */
96 }
97 vg_lite_error_t;
98 #endif
99 
100 #if !defined(VG_DRIVER_SINGLE_THREAD)
101 typedef enum vg_lite_buffer_signal
102 {
103     VG_LITE_IDLE = 0,        /*! Buffer available. */
104     VG_LITE_HW_FINISHED,     /*! HW has completed command buffer. */
105     VG_LITE_IN_QUEUE,        /*! Buffer has been send to queue. */
106 }
107 vg_lite_buffer_signal_t;
108 #endif /* not defined(VG_DRIVER_SINGLE_THREAD) */
109 
110 typedef enum vg_lite_kernel_counter
111 {
112     /* Dont't touch the counter. */
113     VG_LITE_NONE,
114 
115     /* Turn the counter on. */
116     VG_LITE_ON,
117 
118     /* Turn the counter off. */
119     VG_LITE_OFF,
120 
121     /* Query the counter and reset its values. */
122     VG_LITE_QUERY,
123 }
124 vg_lite_kernel_counter_t;
125 
126 typedef enum vg_lite_kernel_command
127 {
128     /* Initialize the GPU. */
129     VG_LITE_INITIALIZE,
130 
131     /* Terminate the GPU. */
132     VG_LITE_TERMINATE,
133 
134     /* Allocate memory. */
135     VG_LITE_ALLOCATE,
136 
137     /* Free memory. */
138     VG_LITE_FREE,
139 
140     /* Submit a command buffer to the GPU. */
141     VG_LITE_SUBMIT,
142 
143     /* Wait for the GPU to be completed. */
144     VG_LITE_WAIT,
145 
146     /* Reset the GPU. */
147     VG_LITE_RESET,
148 
149     /* Debug commands. */
150     VG_LITE_DEBUG,
151 
152     /* Map memory. */
153     VG_LITE_MAP,
154 
155     /* Unmap memory. */
156     VG_LITE_UNMAP,
157 
158     /* Check info. */
159     VG_LITE_CHECK,
160 
161     /* Query mem. */
162     VG_LITE_QUERY_MEM,
163 
164 #if !defined(VG_DRIVER_SINGLE_THREAD)
165     /* Mutex lock. */
166     VG_LITE_LOCK,
167 
168     /* Mutex unlock. */
169     VG_LITE_UNLOCK,
170 
171     /* query context switch. */
172     VG_LITE_QUERY_CONTEXT_SWITCH,
173 #endif /* not defined(VG_DRIVER_SINGLE_THREAD) */
174 
175 }
176 vg_lite_kernel_command_t;
177 
178 struct vg_lite_kernel_context {
179     /* Command buffer. */
180     void      * command_buffer[CMDBUF_COUNT];
181     void      * command_buffer_logical[CMDBUF_COUNT];
182     uint32_t    command_buffer_physical[CMDBUF_COUNT];
183 
184 #if !defined(VG_DRIVER_SINGLE_THREAD)
185     vg_lite_os_async_event_t async_event[CMDBUF_COUNT];
186 #endif /* not defined(VG_DRIVER_SINGLE_THREAD) */
187 
188     /* Tessellation buffer. */
189     void      * tessellation_buffer;
190     void      * tessellation_buffer_logical;
191     uint32_t    tessellation_buffer_physical;
192 
193 #if !defined(VG_DRIVER_SINGLE_THREAD)
194     /* context buffer. */
195     void      * context_buffer[CMDBUF_COUNT];
196     void      * context_buffer_logical[CMDBUF_COUNT];
197     uint32_t    context_buffer_physical[CMDBUF_COUNT];
198 #endif /* not defined(VG_DRIVER_SINGLE_THREAD) */
199 
200 };
201 
202 /* Context structure. */
203 typedef struct vg_lite_kernel_context vg_lite_kernel_context_t;
204 
205 typedef struct capabilities
206 {
207     uint32_t tiled : 2;
208     uint32_t l2_cache : 1;
209 }
210 capabilities_t;
211 
212 typedef union vg_lite_capabilities
213 {
214     capabilities_t cap;
215     uint32_t       data;
216 }
217 vg_lite_capabilities_t;
218 
219 typedef struct vg_lite_kernel_initialize
220 {
221     /* Command buffer size. */
222     uint32_t command_buffer_size;
223 
224 #if !defined(VG_DRIVER_SINGLE_THREAD)
225     /* Context buffer size. */
226     uint32_t context_buffer_size;
227 #endif /* not defined(VG_DRIVER_SINGLE_THREAD) */
228 
229     /* Tessellation buffer width. */
230     int32_t tessellation_width;
231 
232     /* Tessellation buffer height. */
233     int32_t tessellation_height;
234 
235     /* OUTPUT */
236 
237     /* Context pointer. */
238     vg_lite_kernel_context_t * context;
239 
240     /* Capabilities. */
241     vg_lite_capabilities_t capabilities;
242 
243     /* Allocated command buffer. */
244     void * command_buffer[CMDBUF_COUNT];
245 
246     /* GPU address for command buffer. */
247     uint32_t command_buffer_gpu[CMDBUF_COUNT];
248 
249 #if !defined(VG_DRIVER_SINGLE_THREAD)
250     /* Allocated context buffer. */
251     void * context_buffer[CMDBUF_COUNT];
252 
253     /* GPU address for context buffer. */
254     uint32_t context_buffer_gpu[CMDBUF_COUNT];
255 #endif /* not defined(VG_DRIVER_SINGLE_THREAD) */
256 
257     /* GPU addresses for tesselation buffers. */
258     uint32_t tessellation_buffer_gpu[3];
259 
260     /* Logic addresses for tessellation buffers: used by SW Tessellator. */
261     uint8_t *tessellation_buffer_logic[3];
262 
263     /* Size of each level of the tesselation buffer. */
264     uint32_t tessellation_buffer_size[3];
265 
266     /* Stride of the tessellation buffer. */
267     uint32_t tessellation_stride;
268 
269     /* Width and height of tessellation buffer. */
270     uint32_t tessellation_width_height;
271 
272     /* Tessellation config: shift. */
273     uint32_t tessellation_shift;
274 }
275 vg_lite_kernel_initialize_t;
276 
277 typedef struct vg_lite_kernel_terminate
278 {
279     /* Context to terminate. */
280     vg_lite_kernel_context_t * context;
281 }
282 vg_lite_kernel_terminate_t;
283 
284 typedef struct vg_lite_kernel_allocate
285 {
286     /* Number of bytes to allocate. */
287     uint32_t bytes;
288 
289     /* Flag to indicate whether the allocated memory is contiguous or not. */
290     int32_t contiguous;
291 
292     /* OUTPUT */
293 
294     /* Memory handle. */
295     void * memory_handle;
296 
297     /* Allocated memory. */
298     void * memory;
299 
300     /* GPU address of allocated memory. */
301     uint32_t memory_gpu;
302 }
303 vg_lite_kernel_allocate_t;
304 
305 typedef struct vg_lite_kernel_free
306 {
307     /* Memory handle to free. */
308     void * memory_handle;
309 }
310 vg_lite_kernel_free_t;
311 
312 typedef struct vg_lite_kernel_submit
313 {
314     /* Context to submit to. */
315     vg_lite_kernel_context_t * context;
316 
317     /* Pointer to command buffer. */
318     void * commands;
319 
320     /* Number of bytes in command buffer. */
321     uint32_t command_size;
322 
323     /* Command Buffer ID. */
324     uint32_t command_id;
325 }
326 vg_lite_kernel_submit_t;
327 
328 typedef struct vg_lite_kernel_wait
329 {
330     /* Context to wait for. */
331     vg_lite_kernel_context_t * context;
332 
333     /* Timeout in milliseconds. */
334     uint32_t timeout_ms;
335 
336 #if defined(VG_DRIVER_SINGLE_THREAD)
337     /* The event to wait. */
338     uint32_t event_mask;
339 
340     /* The event(s) got after waiting. */
341     uint32_t event_got;
342 #else
343     /* Command Buffer ID. */
344     uint32_t command_id;
345 #endif /* VG_DRIVER_SINGLE_THREAD */
346 }
347 vg_lite_kernel_wait_t;
348 
349 typedef struct vg_lite_kernel_reset
350 {
351     /* Context to reset. */
352     vg_lite_kernel_context_t * context;
353 }
354 vg_lite_kernel_reset_t;
355 
356 typedef struct vg_lite_kernel_debug
357 {
358     /* Context to debug. */
359     vg_lite_kernel_context_t * context;
360 
361     /* Bandwidth counter enabler. */
362     vg_lite_kernel_counter_t bandwidth_counter;
363 
364     /* Pixel counter enabler. */
365     vg_lite_kernel_counter_t pixel_counters;
366 
367     /* OUTPUT */
368 
369     /* Bandwidth counters:
370      *  [0] - burst of 8.
371      *  [1] - burst of 16.
372      *  [2] - burst of 32.
373      *  [3] - burst of 64.
374      */
375     uint32_t bandwidth[4];
376 
377     /* Pixel counters:.
378      *  [0] - Number of tessellated pixels.
379      *  [1] - Number of imaged pixels.
380      *  [2] - Number of rendered pixels.
381      */
382     uint32_t pixels[3];
383 }
384 vg_lite_kernel_debug_t;
385 
386 typedef struct vg_lite_kernel_map
387 {
388     /* Number of bytes to map. */
389     uint32_t bytes;
390 
391     /* Logical memory address or NULL. */
392     void * logical;
393 
394     /* Physical memory address or 0. */
395     uint32_t physical;
396 
397     /* OUTPUT */
398 
399     /* Memory handle for mapped memory. */
400     void * memory_handle;
401 
402     /* GPU address of mapped memory. */
403     uint32_t memory_gpu;
404 }
405 vg_lite_kernel_map_t;
406 
407 typedef struct vg_lite_kernel_unmap
408 {
409     /* Memory handle to unmap. */
410     void * memory_handle;
411 }
412 vg_lite_kernel_unmap_t;
413 
414 typedef struct vg_lite_kernel_info
415 {
416     /* Register's address. */
417     uint32_t addr;
418 
419     /* Check register info. */
420     uint32_t reg;
421 }
422 vg_lite_kernel_info_t;
423 
424 typedef struct vg_lite_kernel_mem
425 {
426     uint32_t bytes;
427 }
428 vg_lite_kernel_mem_t;
429 
430 #if !defined(VG_DRIVER_SINGLE_THREAD)
431 typedef struct vg_lite_kernel_context_switch
432 {
433     uint8_t isContextSwitched;
434     uint32_t context;
435 }
436 vg_lite_kernel_context_switch_t;
437 #endif /* not defined(VG_DRIVER_SINGLE_THREAD) */
438 
439 vg_lite_error_t vg_lite_kernel(vg_lite_kernel_command_t command, void * data);
440 
441 #ifdef  __cplusplus
442 }
443 #endif
444 #endif /* _vg_lite_kernel_h_ */
445