turnip: Only include msm_drm in tu_drm.c
[mesa.git] / src / freedreno / vulkan / tu_private.h
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27
28 #ifndef TU_PRIVATE_H
29 #define TU_PRIVATE_H
30
31 #include <assert.h>
32 #include <pthread.h>
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_VALGRIND
39 #include <memcheck.h>
40 #include <valgrind.h>
41 #define VG(x) x
42 #else
43 #define VG(x) ((void)0)
44 #endif
45
46 #include "c11/threads.h"
47 #include "main/macros.h"
48 #include "util/list.h"
49 #include "util/macros.h"
50 #include "util/u_atomic.h"
51 #include "vk_alloc.h"
52 #include "vk_object.h"
53 #include "vk_debug_report.h"
54 #include "wsi_common.h"
55
56 #include "ir3/ir3_compiler.h"
57 #include "ir3/ir3_shader.h"
58
59 #include "adreno_common.xml.h"
60 #include "adreno_pm4.xml.h"
61 #include "a6xx.xml.h"
62 #include "fdl/freedreno_layout.h"
63
64 #include "tu_descriptor_set.h"
65 #include "tu_extensions.h"
66 #include "tu_util.h"
67
68 /* Pre-declarations needed for WSI entrypoints */
69 struct wl_surface;
70 struct wl_display;
71 typedef struct xcb_connection_t xcb_connection_t;
72 typedef uint32_t xcb_visualid_t;
73 typedef uint32_t xcb_window_t;
74
75 #include <vulkan/vk_android_native_buffer.h>
76 #include <vulkan/vk_icd.h>
77 #include <vulkan/vulkan.h>
78 #include <vulkan/vulkan_intel.h>
79
80 #include "tu_entrypoints.h"
81
82 #include "vk_format.h"
83
84 #define MAX_VBS 32
85 #define MAX_VERTEX_ATTRIBS 32
86 #define MAX_RTS 8
87 #define MAX_VSC_PIPES 32
88 #define MAX_VIEWPORTS 1
89 #define MAX_SCISSORS 16
90 #define MAX_DISCARD_RECTANGLES 4
91 #define MAX_PUSH_CONSTANTS_SIZE 128
92 #define MAX_PUSH_DESCRIPTORS 32
93 #define MAX_DYNAMIC_UNIFORM_BUFFERS 16
94 #define MAX_DYNAMIC_STORAGE_BUFFERS 8
95 #define MAX_DYNAMIC_BUFFERS \
96 (MAX_DYNAMIC_UNIFORM_BUFFERS + MAX_DYNAMIC_STORAGE_BUFFERS)
97 #define TU_MAX_DRM_DEVICES 8
98 #define MAX_VIEWS 8
99 #define MAX_BIND_POINTS 2 /* compute + graphics */
100 /* The Qualcomm driver exposes 0x20000058 */
101 #define MAX_STORAGE_BUFFER_RANGE 0x20000000
102 /* We use ldc for uniform buffer loads, just like the Qualcomm driver, so
103 * expose the same maximum range.
104 * TODO: The SIZE bitfield is 15 bits, and in 4-dword units, so the actual
105 * range might be higher.
106 */
107 #define MAX_UNIFORM_BUFFER_RANGE 0x10000
108
109 #define A6XX_TEX_CONST_DWORDS 16
110 #define A6XX_TEX_SAMP_DWORDS 4
111
112 #define tu_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
113
114 static inline uint32_t
115 tu_minify(uint32_t n, uint32_t levels)
116 {
117 if (unlikely(n == 0))
118 return 0;
119 else
120 return MAX2(n >> levels, 1);
121 }
122
123 #define for_each_bit(b, dword) \
124 for (uint32_t __dword = (dword); \
125 (b) = __builtin_ffs(__dword) - 1, __dword; __dword &= ~(1 << (b)))
126
127 #define typed_memcpy(dest, src, count) \
128 ({ \
129 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
130 memcpy((dest), (src), (count) * sizeof(*(src))); \
131 })
132
133 #define COND(bool, val) ((bool) ? (val) : 0)
134 #define BIT(bit) (1u << (bit))
135
136 /* Whenever we generate an error, pass it through this function. Useful for
137 * debugging, where we can break on it. Only call at error site, not when
138 * propagating errors. Might be useful to plug in a stack trace here.
139 */
140
141 struct tu_instance;
142
143 VkResult
144 __vk_errorf(struct tu_instance *instance,
145 VkResult error,
146 const char *file,
147 int line,
148 const char *format,
149 ...);
150
151 #define vk_error(instance, error) \
152 __vk_errorf(instance, error, __FILE__, __LINE__, NULL);
153 #define vk_errorf(instance, error, format, ...) \
154 __vk_errorf(instance, error, __FILE__, __LINE__, format, ##__VA_ARGS__);
155
156 void
157 __tu_finishme(const char *file, int line, const char *format, ...)
158 tu_printflike(3, 4);
159 void
160 tu_loge(const char *format, ...) tu_printflike(1, 2);
161 void
162 tu_logi(const char *format, ...) tu_printflike(1, 2);
163
164 /**
165 * Print a FINISHME message, including its source location.
166 */
167 #define tu_finishme(format, ...) \
168 do { \
169 static bool reported = false; \
170 if (!reported) { \
171 __tu_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
172 reported = true; \
173 } \
174 } while (0)
175
176 #define tu_stub() \
177 do { \
178 tu_finishme("stub %s", __func__); \
179 } while (0)
180
181 void *
182 tu_lookup_entrypoint_unchecked(const char *name);
183 void *
184 tu_lookup_entrypoint_checked(
185 const char *name,
186 uint32_t core_version,
187 const struct tu_instance_extension_table *instance,
188 const struct tu_device_extension_table *device);
189
190 struct tu_physical_device
191 {
192 struct vk_object_base base;
193
194 struct tu_instance *instance;
195
196 char path[20];
197 char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
198 uint8_t driver_uuid[VK_UUID_SIZE];
199 uint8_t device_uuid[VK_UUID_SIZE];
200 uint8_t cache_uuid[VK_UUID_SIZE];
201
202 struct wsi_device wsi_device;
203
204 int local_fd;
205 int master_fd;
206
207 unsigned gpu_id;
208 uint32_t gmem_size;
209 uint64_t gmem_base;
210 uint32_t ccu_offset_gmem;
211 uint32_t ccu_offset_bypass;
212 /* alignment for size of tiles */
213 uint32_t tile_align_w;
214 #define TILE_ALIGN_H 16
215 /* gmem store/load granularity */
216 #define GMEM_ALIGN_W 16
217 #define GMEM_ALIGN_H 4
218
219 struct {
220 uint32_t PC_UNKNOWN_9805;
221 uint32_t SP_UNKNOWN_A0F8;
222 } magic;
223
224 int msm_major_version;
225 int msm_minor_version;
226
227 bool limited_z24s8;
228
229 /* This is the drivers on-disk cache used as a fallback as opposed to
230 * the pipeline cache defined by apps.
231 */
232 struct disk_cache *disk_cache;
233
234 struct tu_device_extension_table supported_extensions;
235 };
236
237 enum tu_debug_flags
238 {
239 TU_DEBUG_STARTUP = 1 << 0,
240 TU_DEBUG_NIR = 1 << 1,
241 TU_DEBUG_IR3 = 1 << 2,
242 TU_DEBUG_NOBIN = 1 << 3,
243 TU_DEBUG_SYSMEM = 1 << 4,
244 TU_DEBUG_FORCEBIN = 1 << 5,
245 TU_DEBUG_NOUBWC = 1 << 6,
246 };
247
248 struct tu_instance
249 {
250 struct vk_object_base base;
251
252 VkAllocationCallbacks alloc;
253
254 uint32_t api_version;
255 int physical_device_count;
256 struct tu_physical_device physical_devices[TU_MAX_DRM_DEVICES];
257
258 enum tu_debug_flags debug_flags;
259
260 struct vk_debug_report_instance debug_report_callbacks;
261
262 struct tu_instance_extension_table enabled_extensions;
263 };
264
265 VkResult
266 tu_wsi_init(struct tu_physical_device *physical_device);
267 void
268 tu_wsi_finish(struct tu_physical_device *physical_device);
269
270 bool
271 tu_instance_extension_supported(const char *name);
272 uint32_t
273 tu_physical_device_api_version(struct tu_physical_device *dev);
274 bool
275 tu_physical_device_extension_supported(struct tu_physical_device *dev,
276 const char *name);
277
278 struct cache_entry;
279
280 struct tu_pipeline_cache
281 {
282 struct vk_object_base base;
283
284 struct tu_device *device;
285 pthread_mutex_t mutex;
286
287 uint32_t total_size;
288 uint32_t table_size;
289 uint32_t kernel_count;
290 struct cache_entry **hash_table;
291 bool modified;
292
293 VkAllocationCallbacks alloc;
294 };
295
296 struct tu_pipeline_key
297 {
298 };
299
300
301 /* queue types */
302 #define TU_QUEUE_GENERAL 0
303
304 #define TU_MAX_QUEUE_FAMILIES 1
305
306 struct tu_fence
307 {
308 struct vk_object_base base;
309 struct wsi_fence *fence_wsi;
310 bool signaled;
311 int fd;
312 };
313
314 void
315 tu_fence_init(struct tu_fence *fence, bool signaled);
316 void
317 tu_fence_finish(struct tu_fence *fence);
318 void
319 tu_fence_update_fd(struct tu_fence *fence, int fd);
320 void
321 tu_fence_copy(struct tu_fence *fence, const struct tu_fence *src);
322 void
323 tu_fence_signal(struct tu_fence *fence);
324 void
325 tu_fence_wait_idle(struct tu_fence *fence);
326
327 struct tu_queue
328 {
329 struct vk_object_base base;
330
331 struct tu_device *device;
332 uint32_t queue_family_index;
333 int queue_idx;
334 VkDeviceQueueCreateFlags flags;
335
336 uint32_t msm_queue_id;
337 struct tu_fence submit_fence;
338 };
339
340 struct tu_bo
341 {
342 uint32_t gem_handle;
343 uint64_t size;
344 uint64_t iova;
345 void *map;
346 };
347
348 enum global_shader {
349 GLOBAL_SH_VS,
350 GLOBAL_SH_FS_BLIT,
351 GLOBAL_SH_FS_CLEAR0,
352 GLOBAL_SH_FS_CLEAR_MAX = GLOBAL_SH_FS_CLEAR0 + MAX_RTS,
353 GLOBAL_SH_COUNT,
354 };
355
356 /* This struct defines the layout of the global_bo */
357 struct tu6_global
358 {
359 /* 6 bcolor_entry entries, one for each VK_BORDER_COLOR */
360 uint8_t border_color[128 * 6];
361
362 /* clear/blit shaders, all <= 16 instrs (16 instr = 1 instrlen unit) */
363 instr_t shaders[GLOBAL_SH_COUNT][16];
364
365 uint32_t seqno_dummy; /* dummy seqno for CP_EVENT_WRITE */
366 uint32_t _pad0;
367 volatile uint32_t vsc_draw_overflow;
368 uint32_t _pad1;
369 volatile uint32_t vsc_prim_overflow;
370 uint32_t _pad2;
371 uint64_t predicate;
372
373 /* scratch space for VPC_SO[i].FLUSH_BASE_LO/HI, start on 32 byte boundary. */
374 struct {
375 uint32_t offset;
376 uint32_t pad[7];
377 } flush_base[4];
378 };
379 #define gb_offset(member) offsetof(struct tu6_global, member)
380 #define global_iova(cmd, member) ((cmd)->device->global_bo.iova + gb_offset(member))
381
382 void tu_init_clear_blit_shaders(struct tu6_global *global);
383
384 /* extra space in vsc draw/prim streams */
385 #define VSC_PAD 0x40
386
387 struct tu_device
388 {
389 struct vk_device vk;
390 struct tu_instance *instance;
391
392 struct tu_queue *queues[TU_MAX_QUEUE_FAMILIES];
393 int queue_count[TU_MAX_QUEUE_FAMILIES];
394
395 struct tu_physical_device *physical_device;
396 int _lost;
397
398 struct ir3_compiler *compiler;
399
400 /* Backup in-memory cache to be used if the app doesn't provide one */
401 struct tu_pipeline_cache *mem_cache;
402
403 #define MIN_SCRATCH_BO_SIZE_LOG2 12 /* A page */
404
405 /* Currently the kernel driver uses a 32-bit GPU address space, but it
406 * should be impossible to go beyond 48 bits.
407 */
408 struct {
409 struct tu_bo bo;
410 mtx_t construct_mtx;
411 bool initialized;
412 } scratch_bos[48 - MIN_SCRATCH_BO_SIZE_LOG2];
413
414 struct tu_bo global_bo;
415
416 struct tu_device_extension_table enabled_extensions;
417
418 uint32_t vsc_draw_strm_pitch;
419 uint32_t vsc_prim_strm_pitch;
420 mtx_t vsc_pitch_mtx;
421 };
422
423 VkResult _tu_device_set_lost(struct tu_device *device,
424 const char *file, int line,
425 const char *msg, ...) PRINTFLIKE(4, 5);
426 #define tu_device_set_lost(dev, ...) \
427 _tu_device_set_lost(dev, __FILE__, __LINE__, __VA_ARGS__)
428
429 static inline bool
430 tu_device_is_lost(struct tu_device *device)
431 {
432 return unlikely(p_atomic_read(&device->_lost));
433 }
434
435 VkResult
436 tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size);
437 VkResult
438 tu_bo_init_dmabuf(struct tu_device *dev,
439 struct tu_bo *bo,
440 uint64_t size,
441 int fd);
442 int
443 tu_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo);
444 void
445 tu_bo_finish(struct tu_device *dev, struct tu_bo *bo);
446 VkResult
447 tu_bo_map(struct tu_device *dev, struct tu_bo *bo);
448
449 /* Get a scratch bo for use inside a command buffer. This will always return
450 * the same bo given the same size or similar sizes, so only one scratch bo
451 * can be used at the same time. It's meant for short-lived things where we
452 * need to write to some piece of memory, read from it, and then immediately
453 * discard it.
454 */
455 VkResult
456 tu_get_scratch_bo(struct tu_device *dev, uint64_t size, struct tu_bo **bo);
457
458 struct tu_cs_entry
459 {
460 /* No ownership */
461 const struct tu_bo *bo;
462
463 uint32_t size;
464 uint32_t offset;
465 };
466
467 struct tu_cs_memory {
468 uint32_t *map;
469 uint64_t iova;
470 };
471
472 struct tu_draw_state {
473 uint64_t iova : 48;
474 uint32_t size : 16;
475 };
476
477 enum tu_dynamic_state
478 {
479 /* re-use VK_DYNAMIC_STATE_ enums for non-extended dynamic states */
480 TU_DYNAMIC_STATE_SAMPLE_LOCATIONS = VK_DYNAMIC_STATE_STENCIL_REFERENCE + 1,
481 TU_DYNAMIC_STATE_COUNT,
482 };
483
484 enum tu_draw_state_group_id
485 {
486 TU_DRAW_STATE_PROGRAM,
487 TU_DRAW_STATE_PROGRAM_BINNING,
488 TU_DRAW_STATE_TESS,
489 TU_DRAW_STATE_VB,
490 TU_DRAW_STATE_VI,
491 TU_DRAW_STATE_VI_BINNING,
492 TU_DRAW_STATE_RAST,
493 TU_DRAW_STATE_DS,
494 TU_DRAW_STATE_BLEND,
495 TU_DRAW_STATE_VS_CONST,
496 TU_DRAW_STATE_HS_CONST,
497 TU_DRAW_STATE_DS_CONST,
498 TU_DRAW_STATE_GS_CONST,
499 TU_DRAW_STATE_FS_CONST,
500 TU_DRAW_STATE_DESC_SETS,
501 TU_DRAW_STATE_DESC_SETS_LOAD,
502 TU_DRAW_STATE_VS_PARAMS,
503 TU_DRAW_STATE_INPUT_ATTACHMENTS_GMEM,
504 TU_DRAW_STATE_INPUT_ATTACHMENTS_SYSMEM,
505
506 /* dynamic state related draw states */
507 TU_DRAW_STATE_DYNAMIC,
508 TU_DRAW_STATE_COUNT = TU_DRAW_STATE_DYNAMIC + TU_DYNAMIC_STATE_COUNT,
509 };
510
511 enum tu_cs_mode
512 {
513
514 /*
515 * A command stream in TU_CS_MODE_GROW mode grows automatically whenever it
516 * is full. tu_cs_begin must be called before command packet emission and
517 * tu_cs_end must be called after.
518 *
519 * This mode may create multiple entries internally. The entries must be
520 * submitted together.
521 */
522 TU_CS_MODE_GROW,
523
524 /*
525 * A command stream in TU_CS_MODE_EXTERNAL mode wraps an external,
526 * fixed-size buffer. tu_cs_begin and tu_cs_end are optional and have no
527 * effect on it.
528 *
529 * This mode does not create any entry or any BO.
530 */
531 TU_CS_MODE_EXTERNAL,
532
533 /*
534 * A command stream in TU_CS_MODE_SUB_STREAM mode does not support direct
535 * command packet emission. tu_cs_begin_sub_stream must be called to get a
536 * sub-stream to emit comamnd packets to. When done with the sub-stream,
537 * tu_cs_end_sub_stream must be called.
538 *
539 * This mode does not create any entry internally.
540 */
541 TU_CS_MODE_SUB_STREAM,
542 };
543
544 struct tu_cs
545 {
546 uint32_t *start;
547 uint32_t *cur;
548 uint32_t *reserved_end;
549 uint32_t *end;
550
551 struct tu_device *device;
552 enum tu_cs_mode mode;
553 uint32_t next_bo_size;
554
555 struct tu_cs_entry *entries;
556 uint32_t entry_count;
557 uint32_t entry_capacity;
558
559 struct tu_bo **bos;
560 uint32_t bo_count;
561 uint32_t bo_capacity;
562
563 /* state for cond_exec_start/cond_exec_end */
564 uint32_t cond_flags;
565 uint32_t *cond_dwords;
566 };
567
568 struct tu_device_memory
569 {
570 struct vk_object_base base;
571
572 struct tu_bo bo;
573 VkDeviceSize size;
574
575 /* for dedicated allocations */
576 struct tu_image *image;
577 struct tu_buffer *buffer;
578
579 uint32_t type_index;
580 void *map;
581 void *user_ptr;
582 };
583
584 struct tu_descriptor_range
585 {
586 uint64_t va;
587 uint32_t size;
588 };
589
590 struct tu_descriptor_set
591 {
592 struct vk_object_base base;
593
594 const struct tu_descriptor_set_layout *layout;
595 struct tu_descriptor_pool *pool;
596 uint32_t size;
597
598 uint64_t va;
599 uint32_t *mapped_ptr;
600
601 uint32_t *dynamic_descriptors;
602
603 struct tu_bo *buffers[0];
604 };
605
606 struct tu_push_descriptor_set
607 {
608 struct tu_descriptor_set set;
609 uint32_t capacity;
610 };
611
612 struct tu_descriptor_pool_entry
613 {
614 uint32_t offset;
615 uint32_t size;
616 struct tu_descriptor_set *set;
617 };
618
619 struct tu_descriptor_pool
620 {
621 struct vk_object_base base;
622
623 struct tu_bo bo;
624 uint64_t current_offset;
625 uint64_t size;
626
627 uint8_t *host_memory_base;
628 uint8_t *host_memory_ptr;
629 uint8_t *host_memory_end;
630
631 uint32_t entry_count;
632 uint32_t max_entry_count;
633 struct tu_descriptor_pool_entry entries[0];
634 };
635
636 struct tu_descriptor_update_template_entry
637 {
638 VkDescriptorType descriptor_type;
639
640 /* The number of descriptors to update */
641 uint32_t descriptor_count;
642
643 /* Into mapped_ptr or dynamic_descriptors, in units of the respective array
644 */
645 uint32_t dst_offset;
646
647 /* In dwords. Not valid/used for dynamic descriptors */
648 uint32_t dst_stride;
649
650 uint32_t buffer_offset;
651
652 /* Only valid for combined image samplers and samplers */
653 uint16_t has_sampler;
654
655 /* In bytes */
656 size_t src_offset;
657 size_t src_stride;
658
659 /* For push descriptors */
660 const uint32_t *immutable_samplers;
661 };
662
663 struct tu_descriptor_update_template
664 {
665 struct vk_object_base base;
666
667 uint32_t entry_count;
668 struct tu_descriptor_update_template_entry entry[0];
669 };
670
671 struct tu_buffer
672 {
673 struct vk_object_base base;
674
675 VkDeviceSize size;
676
677 VkBufferUsageFlags usage;
678 VkBufferCreateFlags flags;
679
680 struct tu_bo *bo;
681 VkDeviceSize bo_offset;
682 };
683
684 static inline uint64_t
685 tu_buffer_iova(struct tu_buffer *buffer)
686 {
687 return buffer->bo->iova + buffer->bo_offset;
688 }
689
690 struct tu_vertex_binding
691 {
692 struct tu_buffer *buffer;
693 VkDeviceSize offset;
694 };
695
696 const char *
697 tu_get_debug_option_name(int id);
698
699 const char *
700 tu_get_perftest_option_name(int id);
701
702 struct tu_descriptor_state
703 {
704 struct tu_descriptor_set *sets[MAX_SETS];
705 uint32_t dynamic_descriptors[MAX_DYNAMIC_BUFFERS * A6XX_TEX_CONST_DWORDS];
706 };
707
708 enum tu_cmd_dirty_bits
709 {
710 TU_CMD_DIRTY_VERTEX_BUFFERS = 1 << 2,
711 TU_CMD_DIRTY_DESC_SETS_LOAD = 1 << 3,
712 TU_CMD_DIRTY_COMPUTE_DESC_SETS_LOAD = 1 << 4,
713 TU_CMD_DIRTY_SHADER_CONSTS = 1 << 5,
714 /* all draw states were disabled and need to be re-enabled: */
715 TU_CMD_DIRTY_DRAW_STATE = 1 << 7,
716 };
717
718 /* There are only three cache domains we have to care about: the CCU, or
719 * color cache unit, which is used for color and depth/stencil attachments
720 * and copy/blit destinations, and is split conceptually into color and depth,
721 * and the universal cache or UCHE which is used for pretty much everything
722 * else, except for the CP (uncached) and host. We need to flush whenever data
723 * crosses these boundaries.
724 */
725
726 enum tu_cmd_access_mask {
727 TU_ACCESS_UCHE_READ = 1 << 0,
728 TU_ACCESS_UCHE_WRITE = 1 << 1,
729 TU_ACCESS_CCU_COLOR_READ = 1 << 2,
730 TU_ACCESS_CCU_COLOR_WRITE = 1 << 3,
731 TU_ACCESS_CCU_DEPTH_READ = 1 << 4,
732 TU_ACCESS_CCU_DEPTH_WRITE = 1 << 5,
733
734 /* Experiments have shown that while it's safe to avoid flushing the CCU
735 * after each blit/renderpass, it's not safe to assume that subsequent
736 * lookups with a different attachment state will hit unflushed cache
737 * entries. That is, the CCU needs to be flushed and possibly invalidated
738 * when accessing memory with a different attachment state. Writing to an
739 * attachment under the following conditions after clearing using the
740 * normal 2d engine path is known to have issues:
741 *
742 * - It isn't the 0'th layer.
743 * - There are more than one attachment, and this isn't the 0'th attachment
744 * (this seems to also depend on the cpp of the attachments).
745 *
746 * Our best guess is that the layer/MRT state is used when computing
747 * the location of a cache entry in CCU, to avoid conflicts. We assume that
748 * any access in a renderpass after or before an access by a transfer needs
749 * a flush/invalidate, and use the _INCOHERENT variants to represent access
750 * by a transfer.
751 */
752 TU_ACCESS_CCU_COLOR_INCOHERENT_READ = 1 << 6,
753 TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE = 1 << 7,
754 TU_ACCESS_CCU_DEPTH_INCOHERENT_READ = 1 << 8,
755 TU_ACCESS_CCU_DEPTH_INCOHERENT_WRITE = 1 << 9,
756
757 /* Accesses by the host */
758 TU_ACCESS_HOST_READ = 1 << 10,
759 TU_ACCESS_HOST_WRITE = 1 << 11,
760
761 /* Accesses by a GPU engine which bypasses any cache. e.g. writes via
762 * CP_EVENT_WRITE::BLIT and the CP are SYSMEM_WRITE.
763 */
764 TU_ACCESS_SYSMEM_READ = 1 << 12,
765 TU_ACCESS_SYSMEM_WRITE = 1 << 13,
766
767 /* Set if a WFI is required. This can be required for:
768 * - 2D engine which (on some models) doesn't wait for flushes to complete
769 * before starting
770 * - CP draw indirect opcodes, where we need to wait for any flushes to
771 * complete but the CP implicitly waits for WFI's to complete and
772 * therefore we only need a WFI after the flushes.
773 */
774 TU_ACCESS_WFI_READ = 1 << 14,
775
776 /* Set if a CP_WAIT_FOR_ME is required due to the data being read by the CP
777 * without it waiting for any WFI.
778 */
779 TU_ACCESS_WFM_READ = 1 << 15,
780
781 /* Memory writes from the CP start in-order with draws and event writes,
782 * but execute asynchronously and hence need a CP_WAIT_MEM_WRITES if read.
783 */
784 TU_ACCESS_CP_WRITE = 1 << 16,
785
786 TU_ACCESS_READ =
787 TU_ACCESS_UCHE_READ |
788 TU_ACCESS_CCU_COLOR_READ |
789 TU_ACCESS_CCU_DEPTH_READ |
790 TU_ACCESS_CCU_COLOR_INCOHERENT_READ |
791 TU_ACCESS_CCU_DEPTH_INCOHERENT_READ |
792 TU_ACCESS_HOST_READ |
793 TU_ACCESS_SYSMEM_READ |
794 TU_ACCESS_WFI_READ |
795 TU_ACCESS_WFM_READ,
796
797 TU_ACCESS_WRITE =
798 TU_ACCESS_UCHE_WRITE |
799 TU_ACCESS_CCU_COLOR_WRITE |
800 TU_ACCESS_CCU_COLOR_INCOHERENT_WRITE |
801 TU_ACCESS_CCU_DEPTH_WRITE |
802 TU_ACCESS_CCU_DEPTH_INCOHERENT_WRITE |
803 TU_ACCESS_HOST_WRITE |
804 TU_ACCESS_SYSMEM_WRITE |
805 TU_ACCESS_CP_WRITE,
806
807 TU_ACCESS_ALL =
808 TU_ACCESS_READ |
809 TU_ACCESS_WRITE,
810 };
811
812 enum tu_cmd_flush_bits {
813 TU_CMD_FLAG_CCU_FLUSH_DEPTH = 1 << 0,
814 TU_CMD_FLAG_CCU_FLUSH_COLOR = 1 << 1,
815 TU_CMD_FLAG_CCU_INVALIDATE_DEPTH = 1 << 2,
816 TU_CMD_FLAG_CCU_INVALIDATE_COLOR = 1 << 3,
817 TU_CMD_FLAG_CACHE_FLUSH = 1 << 4,
818 TU_CMD_FLAG_CACHE_INVALIDATE = 1 << 5,
819 TU_CMD_FLAG_WAIT_MEM_WRITES = 1 << 6,
820 TU_CMD_FLAG_WAIT_FOR_IDLE = 1 << 7,
821 TU_CMD_FLAG_WAIT_FOR_ME = 1 << 8,
822
823 TU_CMD_FLAG_ALL_FLUSH =
824 TU_CMD_FLAG_CCU_FLUSH_DEPTH |
825 TU_CMD_FLAG_CCU_FLUSH_COLOR |
826 TU_CMD_FLAG_CACHE_FLUSH |
827 /* Treat the CP as a sort of "cache" which may need to be "flushed" via
828 * waiting for writes to land with WAIT_FOR_MEM_WRITES.
829 */
830 TU_CMD_FLAG_WAIT_MEM_WRITES,
831
832 TU_CMD_FLAG_GPU_INVALIDATE =
833 TU_CMD_FLAG_CCU_INVALIDATE_DEPTH |
834 TU_CMD_FLAG_CCU_INVALIDATE_COLOR |
835 TU_CMD_FLAG_CACHE_INVALIDATE,
836
837 TU_CMD_FLAG_ALL_INVALIDATE =
838 TU_CMD_FLAG_GPU_INVALIDATE |
839 /* Treat the CP as a sort of "cache" which may need to be "invalidated"
840 * via waiting for UCHE/CCU flushes to land with WFI/WFM.
841 */
842 TU_CMD_FLAG_WAIT_FOR_IDLE |
843 TU_CMD_FLAG_WAIT_FOR_ME,
844 };
845
846 /* Changing the CCU from sysmem mode to gmem mode or vice-versa is pretty
847 * heavy, involving a CCU cache flush/invalidate and a WFI in order to change
848 * which part of the gmem is used by the CCU. Here we keep track of what the
849 * state of the CCU.
850 */
851 enum tu_cmd_ccu_state {
852 TU_CMD_CCU_SYSMEM,
853 TU_CMD_CCU_GMEM,
854 TU_CMD_CCU_UNKNOWN,
855 };
856
857 struct tu_cache_state {
858 /* Caches which must be made available (flushed) eventually if there are
859 * any users outside that cache domain, and caches which must be
860 * invalidated eventually if there are any reads.
861 */
862 enum tu_cmd_flush_bits pending_flush_bits;
863 /* Pending flushes */
864 enum tu_cmd_flush_bits flush_bits;
865 };
866
867 struct tu_cmd_state
868 {
869 uint32_t dirty;
870
871 struct tu_pipeline *pipeline;
872 struct tu_pipeline *compute_pipeline;
873
874 /* Vertex buffers */
875 struct
876 {
877 struct tu_buffer *buffers[MAX_VBS];
878 VkDeviceSize offsets[MAX_VBS];
879 } vb;
880
881 /* for dynamic states that can't be emitted directly */
882 uint32_t dynamic_stencil_mask;
883 uint32_t dynamic_stencil_wrmask;
884 uint32_t dynamic_stencil_ref;
885 uint32_t dynamic_gras_su_cntl;
886
887 /* saved states to re-emit in TU_CMD_DIRTY_DRAW_STATE case */
888 struct tu_draw_state dynamic_state[TU_DYNAMIC_STATE_COUNT];
889 struct tu_draw_state vertex_buffers;
890 struct tu_draw_state shader_const[MESA_SHADER_STAGES];
891 struct tu_draw_state desc_sets;
892
893 struct tu_draw_state vs_params;
894
895 /* Index buffer */
896 uint64_t index_va;
897 uint32_t max_index_count;
898 uint8_t index_size;
899
900 /* because streamout base has to be 32-byte aligned
901 * there is an extra offset to deal with when it is
902 * unaligned
903 */
904 uint8_t streamout_offset[IR3_MAX_SO_BUFFERS];
905
906 /* Renderpasses are tricky, because we may need to flush differently if
907 * using sysmem vs. gmem and therefore we have to delay any flushing that
908 * happens before a renderpass. So we have to have two copies of the flush
909 * state, one for intra-renderpass flushes (i.e. renderpass dependencies)
910 * and one for outside a renderpass.
911 */
912 struct tu_cache_state cache;
913 struct tu_cache_state renderpass_cache;
914
915 enum tu_cmd_ccu_state ccu_state;
916
917 const struct tu_render_pass *pass;
918 const struct tu_subpass *subpass;
919 const struct tu_framebuffer *framebuffer;
920 VkRect2D render_area;
921
922 struct tu_cs_entry tile_store_ib;
923
924 bool xfb_used;
925 bool has_tess;
926 bool has_subpass_predication;
927 bool predication_active;
928 };
929
930 struct tu_cmd_pool
931 {
932 struct vk_object_base base;
933
934 VkAllocationCallbacks alloc;
935 struct list_head cmd_buffers;
936 struct list_head free_cmd_buffers;
937 uint32_t queue_family_index;
938 };
939
940 struct tu_cmd_buffer_upload
941 {
942 uint8_t *map;
943 unsigned offset;
944 uint64_t size;
945 struct list_head list;
946 };
947
948 enum tu_cmd_buffer_status
949 {
950 TU_CMD_BUFFER_STATUS_INVALID,
951 TU_CMD_BUFFER_STATUS_INITIAL,
952 TU_CMD_BUFFER_STATUS_RECORDING,
953 TU_CMD_BUFFER_STATUS_EXECUTABLE,
954 TU_CMD_BUFFER_STATUS_PENDING,
955 };
956
957 #ifndef MSM_SUBMIT_BO_READ
958 #define MSM_SUBMIT_BO_READ 0x0001
959 #define MSM_SUBMIT_BO_WRITE 0x0002
960 #define MSM_SUBMIT_BO_DUMP 0x0004
961
962 struct drm_msm_gem_submit_bo {
963 uint32_t flags; /* in, mask of MSM_SUBMIT_BO_x */
964 uint32_t handle; /* in, GEM handle */
965 uint64_t presumed; /* in/out, presumed buffer address */
966 };
967 #endif
968
969 struct tu_bo_list
970 {
971 uint32_t count;
972 uint32_t capacity;
973 struct drm_msm_gem_submit_bo *bo_infos;
974 };
975
976 #define TU_BO_LIST_FAILED (~0)
977
978 void
979 tu_bo_list_init(struct tu_bo_list *list);
980 void
981 tu_bo_list_destroy(struct tu_bo_list *list);
982 void
983 tu_bo_list_reset(struct tu_bo_list *list);
984 uint32_t
985 tu_bo_list_add(struct tu_bo_list *list,
986 const struct tu_bo *bo,
987 uint32_t flags);
988 VkResult
989 tu_bo_list_merge(struct tu_bo_list *list, const struct tu_bo_list *other);
990
991 struct tu_cmd_buffer
992 {
993 struct vk_object_base base;
994
995 struct tu_device *device;
996
997 struct tu_cmd_pool *pool;
998 struct list_head pool_link;
999
1000 VkCommandBufferUsageFlags usage_flags;
1001 VkCommandBufferLevel level;
1002 enum tu_cmd_buffer_status status;
1003
1004 struct tu_cmd_state state;
1005 struct tu_vertex_binding vertex_bindings[MAX_VBS];
1006 uint32_t vertex_bindings_set;
1007 uint32_t queue_family_index;
1008
1009 uint32_t push_constants[MAX_PUSH_CONSTANTS_SIZE / 4];
1010 VkShaderStageFlags push_constant_stages;
1011 struct tu_descriptor_set meta_push_descriptors;
1012
1013 struct tu_descriptor_state descriptors[MAX_BIND_POINTS];
1014
1015 struct tu_cmd_buffer_upload upload;
1016
1017 VkResult record_result;
1018
1019 struct tu_bo_list bo_list;
1020 struct tu_cs cs;
1021 struct tu_cs draw_cs;
1022 struct tu_cs draw_epilogue_cs;
1023 struct tu_cs sub_cs;
1024
1025 uint32_t vsc_draw_strm_pitch;
1026 uint32_t vsc_prim_strm_pitch;
1027 };
1028
1029 /* Temporary struct for tracking a register state to be written, used by
1030 * a6xx-pack.h and tu_cs_emit_regs()
1031 */
1032 struct tu_reg_value {
1033 uint32_t reg;
1034 uint64_t value;
1035 bool is_address;
1036 struct tu_bo *bo;
1037 bool bo_write;
1038 uint32_t bo_offset;
1039 uint32_t bo_shift;
1040 };
1041
1042
1043 void tu_emit_cache_flush_renderpass(struct tu_cmd_buffer *cmd_buffer,
1044 struct tu_cs *cs);
1045
1046 void tu_emit_cache_flush_ccu(struct tu_cmd_buffer *cmd_buffer,
1047 struct tu_cs *cs,
1048 enum tu_cmd_ccu_state ccu_state);
1049
1050 void
1051 tu6_emit_event_write(struct tu_cmd_buffer *cmd,
1052 struct tu_cs *cs,
1053 enum vgt_event_type event);
1054
1055 static inline struct tu_descriptor_state *
1056 tu_get_descriptors_state(struct tu_cmd_buffer *cmd_buffer,
1057 VkPipelineBindPoint bind_point)
1058 {
1059 return &cmd_buffer->descriptors[bind_point];
1060 }
1061
1062 struct tu_event
1063 {
1064 struct vk_object_base base;
1065 struct tu_bo bo;
1066 };
1067
1068 struct tu_shader_module
1069 {
1070 struct vk_object_base base;
1071
1072 unsigned char sha1[20];
1073
1074 uint32_t code_size;
1075 const uint32_t *code[0];
1076 };
1077
1078 struct tu_push_constant_range
1079 {
1080 uint32_t lo;
1081 uint32_t count;
1082 };
1083
1084 struct tu_shader
1085 {
1086 struct ir3_shader *ir3_shader;
1087
1088 struct tu_push_constant_range push_consts;
1089 uint8_t active_desc_sets;
1090 };
1091
1092 struct tu_shader *
1093 tu_shader_create(struct tu_device *dev,
1094 gl_shader_stage stage,
1095 const VkPipelineShaderStageCreateInfo *stage_info,
1096 struct tu_pipeline_layout *layout,
1097 const VkAllocationCallbacks *alloc);
1098
1099 void
1100 tu_shader_destroy(struct tu_device *dev,
1101 struct tu_shader *shader,
1102 const VkAllocationCallbacks *alloc);
1103
1104 struct tu_program_descriptor_linkage
1105 {
1106 struct ir3_const_state const_state;
1107
1108 uint32_t constlen;
1109
1110 struct tu_push_constant_range push_consts;
1111 };
1112
1113 struct tu_pipeline
1114 {
1115 struct vk_object_base base;
1116
1117 struct tu_cs cs;
1118
1119 struct tu_pipeline_layout *layout;
1120
1121 bool need_indirect_descriptor_sets;
1122 VkShaderStageFlags active_stages;
1123 uint32_t active_desc_sets;
1124
1125 /* mask of enabled dynamic states
1126 * if BIT(i) is set, pipeline->dynamic_state[i] is *NOT* used
1127 */
1128 uint32_t dynamic_state_mask;
1129 struct tu_draw_state dynamic_state[TU_DYNAMIC_STATE_COUNT];
1130
1131 /* gras_su_cntl without line width, used for dynamic line width state */
1132 uint32_t gras_su_cntl;
1133
1134 /* draw states for the pipeline */
1135 struct tu_draw_state load_state, rast_state, ds_state, blend_state;
1136
1137 struct
1138 {
1139 struct tu_draw_state state;
1140 struct tu_draw_state binning_state;
1141
1142 struct tu_program_descriptor_linkage link[MESA_SHADER_STAGES];
1143 } program;
1144
1145 struct
1146 {
1147 struct tu_draw_state state;
1148 struct tu_draw_state binning_state;
1149 uint32_t bindings_used;
1150 } vi;
1151
1152 struct
1153 {
1154 enum pc_di_primtype primtype;
1155 bool primitive_restart;
1156 } ia;
1157
1158 struct
1159 {
1160 uint32_t patch_type;
1161 uint32_t param_stride;
1162 uint32_t hs_bo_regid;
1163 uint32_t ds_bo_regid;
1164 bool upper_left_domain_origin;
1165 } tess;
1166
1167 struct
1168 {
1169 uint32_t local_size[3];
1170 } compute;
1171 };
1172
1173 void
1174 tu6_emit_viewport(struct tu_cs *cs, const VkViewport *viewport);
1175
1176 void
1177 tu6_emit_scissor(struct tu_cs *cs, const VkRect2D *scissor);
1178
1179 void
1180 tu6_emit_sample_locations(struct tu_cs *cs, const VkSampleLocationsInfoEXT *samp_loc);
1181
1182 void
1183 tu6_emit_depth_bias(struct tu_cs *cs,
1184 float constant_factor,
1185 float clamp,
1186 float slope_factor);
1187
1188 void tu6_emit_msaa(struct tu_cs *cs, VkSampleCountFlagBits samples);
1189
1190 void tu6_emit_window_scissor(struct tu_cs *cs, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2);
1191
1192 void tu6_emit_window_offset(struct tu_cs *cs, uint32_t x1, uint32_t y1);
1193
1194 void
1195 tu6_emit_xs_config(struct tu_cs *cs,
1196 gl_shader_stage stage,
1197 const struct ir3_shader_variant *xs,
1198 uint64_t binary_iova);
1199
1200 void
1201 tu6_emit_vpc(struct tu_cs *cs,
1202 const struct ir3_shader_variant *vs,
1203 const struct ir3_shader_variant *hs,
1204 const struct ir3_shader_variant *ds,
1205 const struct ir3_shader_variant *gs,
1206 const struct ir3_shader_variant *fs,
1207 uint32_t patch_control_points,
1208 bool vshs_workgroup);
1209
1210 void
1211 tu6_emit_fs_inputs(struct tu_cs *cs, const struct ir3_shader_variant *fs);
1212
1213 struct tu_image_view;
1214
1215 void
1216 tu_resolve_sysmem(struct tu_cmd_buffer *cmd,
1217 struct tu_cs *cs,
1218 struct tu_image_view *src,
1219 struct tu_image_view *dst,
1220 uint32_t layers,
1221 const VkRect2D *rect);
1222
1223 void
1224 tu_clear_sysmem_attachment(struct tu_cmd_buffer *cmd,
1225 struct tu_cs *cs,
1226 uint32_t a,
1227 const VkRenderPassBeginInfo *info);
1228
1229 void
1230 tu_clear_gmem_attachment(struct tu_cmd_buffer *cmd,
1231 struct tu_cs *cs,
1232 uint32_t a,
1233 const VkRenderPassBeginInfo *info);
1234
1235 void
1236 tu_load_gmem_attachment(struct tu_cmd_buffer *cmd,
1237 struct tu_cs *cs,
1238 uint32_t a,
1239 bool force_load);
1240
1241 /* expose this function to be able to emit load without checking LOAD_OP */
1242 void
1243 tu_emit_load_gmem_attachment(struct tu_cmd_buffer *cmd, struct tu_cs *cs, uint32_t a);
1244
1245 /* note: gmem store can also resolve */
1246 void
1247 tu_store_gmem_attachment(struct tu_cmd_buffer *cmd,
1248 struct tu_cs *cs,
1249 uint32_t a,
1250 uint32_t gmem_a);
1251
1252 enum tu_supported_formats {
1253 FMT_VERTEX = 1,
1254 FMT_TEXTURE = 2,
1255 FMT_COLOR = 4,
1256 };
1257
1258 struct tu_native_format
1259 {
1260 enum a6xx_format fmt : 8;
1261 enum a3xx_color_swap swap : 8;
1262 enum a6xx_tile_mode tile_mode : 8;
1263 enum tu_supported_formats supported : 8;
1264 };
1265
1266 struct tu_native_format tu6_format_vtx(VkFormat format);
1267 struct tu_native_format tu6_format_color(VkFormat format, enum a6xx_tile_mode tile_mode);
1268 struct tu_native_format tu6_format_texture(VkFormat format, enum a6xx_tile_mode tile_mode);
1269
1270 static inline enum a6xx_format
1271 tu6_base_format(VkFormat format)
1272 {
1273 /* note: tu6_format_color doesn't care about tiling for .fmt field */
1274 return tu6_format_color(format, TILE6_LINEAR).fmt;
1275 }
1276
1277 struct tu_image
1278 {
1279 struct vk_object_base base;
1280
1281 VkImageType type;
1282 /* The original VkFormat provided by the client. This may not match any
1283 * of the actual surface formats.
1284 */
1285 VkFormat vk_format;
1286 VkImageAspectFlags aspects;
1287 VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
1288 VkImageTiling tiling; /** VkImageCreateInfo::tiling */
1289 VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
1290 VkExtent3D extent;
1291 uint32_t level_count;
1292 uint32_t layer_count;
1293 VkSampleCountFlagBits samples;
1294
1295 struct fdl_layout layout[3];
1296 uint32_t total_size;
1297
1298 unsigned queue_family_mask;
1299 bool exclusive;
1300 bool shareable;
1301
1302 /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */
1303 VkDeviceMemory owned_memory;
1304
1305 /* Set when bound */
1306 struct tu_bo *bo;
1307 VkDeviceSize bo_offset;
1308 };
1309
1310 static inline uint32_t
1311 tu_get_layerCount(const struct tu_image *image,
1312 const VkImageSubresourceRange *range)
1313 {
1314 return range->layerCount == VK_REMAINING_ARRAY_LAYERS
1315 ? image->layer_count - range->baseArrayLayer
1316 : range->layerCount;
1317 }
1318
1319 static inline uint32_t
1320 tu_get_levelCount(const struct tu_image *image,
1321 const VkImageSubresourceRange *range)
1322 {
1323 return range->levelCount == VK_REMAINING_MIP_LEVELS
1324 ? image->level_count - range->baseMipLevel
1325 : range->levelCount;
1326 }
1327
1328 struct tu_image_view
1329 {
1330 struct vk_object_base base;
1331
1332 struct tu_image *image; /**< VkImageViewCreateInfo::image */
1333
1334 uint64_t base_addr;
1335 uint64_t ubwc_addr;
1336 uint32_t layer_size;
1337 uint32_t ubwc_layer_size;
1338
1339 /* used to determine if fast gmem store path can be used */
1340 VkExtent2D extent;
1341 bool need_y2_align;
1342
1343 bool ubwc_enabled;
1344
1345 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1346
1347 /* Descriptor for use as a storage image as opposed to a sampled image.
1348 * This has a few differences for cube maps (e.g. type).
1349 */
1350 uint32_t storage_descriptor[A6XX_TEX_CONST_DWORDS];
1351
1352 /* pre-filled register values */
1353 uint32_t PITCH;
1354 uint32_t FLAG_BUFFER_PITCH;
1355
1356 uint32_t RB_MRT_BUF_INFO;
1357 uint32_t SP_FS_MRT_REG;
1358
1359 uint32_t SP_PS_2D_SRC_INFO;
1360 uint32_t SP_PS_2D_SRC_SIZE;
1361
1362 uint32_t RB_2D_DST_INFO;
1363
1364 uint32_t RB_BLIT_DST_INFO;
1365
1366 /* for d32s8 separate stencil */
1367 uint64_t stencil_base_addr;
1368 uint32_t stencil_layer_size;
1369 uint32_t stencil_PITCH;
1370 };
1371
1372 struct tu_sampler_ycbcr_conversion {
1373 struct vk_object_base base;
1374
1375 VkFormat format;
1376 VkSamplerYcbcrModelConversion ycbcr_model;
1377 VkSamplerYcbcrRange ycbcr_range;
1378 VkComponentMapping components;
1379 VkChromaLocation chroma_offsets[2];
1380 VkFilter chroma_filter;
1381 };
1382
1383 struct tu_sampler {
1384 struct vk_object_base base;
1385
1386 uint32_t descriptor[A6XX_TEX_SAMP_DWORDS];
1387 struct tu_sampler_ycbcr_conversion *ycbcr_sampler;
1388 };
1389
1390 void
1391 tu_cs_image_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1392
1393 void
1394 tu_cs_image_ref_2d(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer, bool src);
1395
1396 void
1397 tu_cs_image_flag_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1398
1399 void
1400 tu_cs_image_stencil_ref(struct tu_cs *cs, const struct tu_image_view *iview, uint32_t layer);
1401
1402 #define tu_image_view_stencil(iview, x) \
1403 ((iview->x & ~A6XX_##x##_COLOR_FORMAT__MASK) | A6XX_##x##_COLOR_FORMAT(FMT6_8_UINT))
1404
1405 VkResult
1406 tu_image_create(VkDevice _device,
1407 const VkImageCreateInfo *pCreateInfo,
1408 const VkAllocationCallbacks *alloc,
1409 VkImage *pImage,
1410 uint64_t modifier,
1411 const VkSubresourceLayout *plane_layouts);
1412
1413 VkResult
1414 tu_image_from_gralloc(VkDevice device_h,
1415 const VkImageCreateInfo *base_info,
1416 const VkNativeBufferANDROID *gralloc_info,
1417 const VkAllocationCallbacks *alloc,
1418 VkImage *out_image_h);
1419
1420 void
1421 tu_image_view_init(struct tu_image_view *iview,
1422 const VkImageViewCreateInfo *pCreateInfo,
1423 bool limited_z24s8);
1424
1425 struct tu_buffer_view
1426 {
1427 struct vk_object_base base;
1428
1429 uint32_t descriptor[A6XX_TEX_CONST_DWORDS];
1430
1431 struct tu_buffer *buffer;
1432 };
1433 void
1434 tu_buffer_view_init(struct tu_buffer_view *view,
1435 struct tu_device *device,
1436 const VkBufferViewCreateInfo *pCreateInfo);
1437
1438 struct tu_attachment_info
1439 {
1440 struct tu_image_view *attachment;
1441 };
1442
1443 struct tu_framebuffer
1444 {
1445 struct vk_object_base base;
1446
1447 uint32_t width;
1448 uint32_t height;
1449 uint32_t layers;
1450
1451 /* size of the first tile */
1452 VkExtent2D tile0;
1453 /* number of tiles */
1454 VkExtent2D tile_count;
1455
1456 /* size of the first VSC pipe */
1457 VkExtent2D pipe0;
1458 /* number of VSC pipes */
1459 VkExtent2D pipe_count;
1460
1461 /* pipe register values */
1462 uint32_t pipe_config[MAX_VSC_PIPES];
1463 uint32_t pipe_sizes[MAX_VSC_PIPES];
1464
1465 uint32_t attachment_count;
1466 struct tu_attachment_info attachments[0];
1467 };
1468
1469 void
1470 tu_framebuffer_tiling_config(struct tu_framebuffer *fb,
1471 const struct tu_device *device,
1472 const struct tu_render_pass *pass);
1473
1474 struct tu_subpass_barrier {
1475 VkPipelineStageFlags src_stage_mask;
1476 VkAccessFlags src_access_mask;
1477 VkAccessFlags dst_access_mask;
1478 bool incoherent_ccu_color, incoherent_ccu_depth;
1479 };
1480
1481 struct tu_subpass_attachment
1482 {
1483 uint32_t attachment;
1484 };
1485
1486 struct tu_subpass
1487 {
1488 uint32_t input_count;
1489 uint32_t color_count;
1490 struct tu_subpass_attachment *input_attachments;
1491 struct tu_subpass_attachment *color_attachments;
1492 struct tu_subpass_attachment *resolve_attachments;
1493 struct tu_subpass_attachment depth_stencil_attachment;
1494
1495 VkSampleCountFlagBits samples;
1496
1497 uint32_t srgb_cntl;
1498
1499 struct tu_subpass_barrier start_barrier;
1500 };
1501
1502 struct tu_render_pass_attachment
1503 {
1504 VkFormat format;
1505 uint32_t samples;
1506 uint32_t cpp;
1507 VkImageAspectFlags clear_mask;
1508 bool load;
1509 bool store;
1510 int32_t gmem_offset;
1511 /* for D32S8 separate stencil: */
1512 bool load_stencil;
1513 bool store_stencil;
1514 int32_t gmem_offset_stencil;
1515 };
1516
1517 struct tu_render_pass
1518 {
1519 struct vk_object_base base;
1520
1521 uint32_t attachment_count;
1522 uint32_t subpass_count;
1523 uint32_t gmem_pixels;
1524 uint32_t tile_align_w;
1525 struct tu_subpass_attachment *subpass_attachments;
1526 struct tu_render_pass_attachment *attachments;
1527 struct tu_subpass_barrier end_barrier;
1528 struct tu_subpass subpasses[0];
1529 };
1530
1531 struct tu_query_pool
1532 {
1533 struct vk_object_base base;
1534
1535 VkQueryType type;
1536 uint32_t stride;
1537 uint64_t size;
1538 uint32_t pipeline_statistics;
1539 struct tu_bo bo;
1540 };
1541
1542 enum tu_semaphore_kind
1543 {
1544 TU_SEMAPHORE_NONE,
1545 TU_SEMAPHORE_SYNCOBJ,
1546 };
1547
1548 struct tu_semaphore_part
1549 {
1550 enum tu_semaphore_kind kind;
1551 union {
1552 uint32_t syncobj;
1553 };
1554 };
1555
1556 struct tu_semaphore
1557 {
1558 struct vk_object_base base;
1559
1560 struct tu_semaphore_part permanent;
1561 struct tu_semaphore_part temporary;
1562 };
1563
1564 void
1565 tu_set_descriptor_set(struct tu_cmd_buffer *cmd_buffer,
1566 VkPipelineBindPoint bind_point,
1567 struct tu_descriptor_set *set,
1568 unsigned idx);
1569
1570 void
1571 tu_update_descriptor_sets(struct tu_device *device,
1572 struct tu_cmd_buffer *cmd_buffer,
1573 VkDescriptorSet overrideSet,
1574 uint32_t descriptorWriteCount,
1575 const VkWriteDescriptorSet *pDescriptorWrites,
1576 uint32_t descriptorCopyCount,
1577 const VkCopyDescriptorSet *pDescriptorCopies);
1578
1579 void
1580 tu_update_descriptor_set_with_template(
1581 struct tu_device *device,
1582 struct tu_cmd_buffer *cmd_buffer,
1583 struct tu_descriptor_set *set,
1584 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1585 const void *pData);
1586
1587 VkResult
1588 tu_physical_device_init(struct tu_physical_device *device,
1589 struct tu_instance *instance);
1590 VkResult
1591 tu_enumerate_devices(struct tu_instance *instance);
1592
1593 int
1594 tu_drm_submitqueue_new(const struct tu_device *dev,
1595 int priority,
1596 uint32_t *queue_id);
1597
1598 void
1599 tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id);
1600
1601 #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \
1602 \
1603 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1604 { \
1605 return (struct __tu_type *) _handle; \
1606 } \
1607 \
1608 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1609 { \
1610 return (__VkType) _obj; \
1611 }
1612
1613 #define TU_DEFINE_NONDISP_HANDLE_CASTS(__tu_type, __VkType) \
1614 \
1615 static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \
1616 { \
1617 return (struct __tu_type *) (uintptr_t) _handle; \
1618 } \
1619 \
1620 static inline __VkType __tu_type##_to_handle(struct __tu_type *_obj) \
1621 { \
1622 return (__VkType)(uintptr_t) _obj; \
1623 }
1624
1625 #define TU_FROM_HANDLE(__tu_type, __name, __handle) \
1626 struct __tu_type *__name = __tu_type##_from_handle(__handle)
1627
1628 TU_DEFINE_HANDLE_CASTS(tu_cmd_buffer, VkCommandBuffer)
1629 TU_DEFINE_HANDLE_CASTS(tu_device, VkDevice)
1630 TU_DEFINE_HANDLE_CASTS(tu_instance, VkInstance)
1631 TU_DEFINE_HANDLE_CASTS(tu_physical_device, VkPhysicalDevice)
1632 TU_DEFINE_HANDLE_CASTS(tu_queue, VkQueue)
1633
1634 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_cmd_pool, VkCommandPool)
1635 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer, VkBuffer)
1636 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_buffer_view, VkBufferView)
1637 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_pool, VkDescriptorPool)
1638 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set, VkDescriptorSet)
1639 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set_layout,
1640 VkDescriptorSetLayout)
1641 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_update_template,
1642 VkDescriptorUpdateTemplate)
1643 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_device_memory, VkDeviceMemory)
1644 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_fence, VkFence)
1645 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_event, VkEvent)
1646 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_framebuffer, VkFramebuffer)
1647 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image, VkImage)
1648 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_image_view, VkImageView);
1649 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_cache, VkPipelineCache)
1650 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline, VkPipeline)
1651 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_layout, VkPipelineLayout)
1652 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_query_pool, VkQueryPool)
1653 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_render_pass, VkRenderPass)
1654 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler, VkSampler)
1655 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler_ycbcr_conversion, VkSamplerYcbcrConversion)
1656 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_shader_module, VkShaderModule)
1657 TU_DEFINE_NONDISP_HANDLE_CASTS(tu_semaphore, VkSemaphore)
1658
1659 #endif /* TU_PRIVATE_H */