1 /*
2  * Copyright © 2022 Friedrich Vock
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef VK_RMV_TOKENS_H
25 #define VK_RMV_TOKENS_H
26 
27 #include <stdint.h>
28 #include <string.h>
29 #include "util/os_time.h"
30 #include <vulkan/vulkan_core.h>
31 
32 /*
33  * Implemented types of tokens.
34  */
35 enum vk_rmv_token_type {
36    VK_RMV_TOKEN_TYPE_USERDATA,
37    VK_RMV_TOKEN_TYPE_MISC,
38    VK_RMV_TOKEN_TYPE_RESOURCE_BIND,
39    VK_RMV_TOKEN_TYPE_RESOURCE_REFERENCE,
40    VK_RMV_TOKEN_TYPE_PAGE_TABLE_UPDATE,
41    VK_RMV_TOKEN_TYPE_CPU_MAP,
42    VK_RMV_TOKEN_TYPE_VIRTUAL_FREE,
43    VK_RMV_TOKEN_TYPE_VIRTUAL_ALLOCATE,
44    VK_RMV_TOKEN_TYPE_RESOURCE_CREATE,
45    VK_RMV_TOKEN_TYPE_RESOURCE_DESTROY
46 };
47 
48 /*
49  * The type of miscellaneous event reported through a MISC token.
50  */
51 enum vk_rmv_misc_event_type {
52    VK_RMV_MISC_EVENT_TYPE_SUBMIT_GRAPHICS,
53    VK_RMV_MISC_EVENT_TYPE_SUBMIT_COMPUTE,
54    VK_RMV_MISC_EVENT_TYPE_SUBMIT_COPY,
55    VK_RMV_MISC_EVENT_TYPE_PRESENT,
56    VK_RMV_MISC_EVENT_TYPE_INVALIDATE_RANGES,
57    VK_RMV_MISC_EVENT_TYPE_FLUSH_MAPPED_RANGE,
58    VK_RMV_MISC_EVENT_TYPE_TRIM_MEMORY
59 };
60 
61 enum vk_rmv_resource_type {
62    VK_RMV_RESOURCE_TYPE_IMAGE,
63    VK_RMV_RESOURCE_TYPE_BUFFER,
64    VK_RMV_RESOURCE_TYPE_GPU_EVENT,
65    VK_RMV_RESOURCE_TYPE_BORDER_COLOR_PALETTE,
66    VK_RMV_RESOURCE_TYPE_INDIRECT_CMD_GENERATOR,
67    VK_RMV_RESOURCE_TYPE_MOTION_ESTIMATOR,
68    VK_RMV_RESOURCE_TYPE_PERF_EXPERIMENT,
69    VK_RMV_RESOURCE_TYPE_QUERY_HEAP,
70    VK_RMV_RESOURCE_TYPE_VIDEO_DECODER,
71    VK_RMV_RESOURCE_TYPE_VIDEO_ENCODER,
72    VK_RMV_RESOURCE_TYPE_TIMESTAMP,
73    VK_RMV_RESOURCE_TYPE_HEAP,
74    VK_RMV_RESOURCE_TYPE_PIPELINE,
75    VK_RMV_RESOURCE_TYPE_DESCRIPTOR_HEAP,
76    VK_RMV_RESOURCE_TYPE_DESCRIPTOR_POOL,
77    VK_RMV_RESOURCE_TYPE_COMMAND_ALLOCATOR,
78    VK_RMV_RESOURCE_TYPE_MISC_INTERNAL
79 };
80 
81 /*
82  * Token data for all types of tokens.
83  */
84 
85 struct vk_rmv_timestamp_token {
86    uint64_t value;
87 };
88 
89 struct vk_rmv_userdata_token {
90    char *name;
91    uint32_t resource_id;
92 };
93 
94 struct vk_rmv_misc_token {
95    enum vk_rmv_misc_event_type type;
96 };
97 
98 struct vk_rmv_resource_bind_token {
99    uint64_t address;
100    uint64_t size;
101    bool is_system_memory;
102    uint32_t resource_id;
103 };
104 
105 struct vk_rmv_resource_reference_token {
106    uint64_t virtual_address;
107    bool residency_removed;
108 };
109 
110 enum vk_rmv_page_table_update_type {
111    VK_RMV_PAGE_TABLE_UPDATE_TYPE_DISCARD,
112    VK_RMV_PAGE_TABLE_UPDATE_TYPE_UPDATE,
113    VK_RMV_PAGE_TABLE_UPDATE_TYPE_TRANSFER
114 };
115 
116 struct vk_rmv_page_table_update_token {
117    uint64_t virtual_address;
118    uint64_t physical_address;
119    uint64_t page_count;
120    uint32_t page_size;
121    int pid;
122    bool is_unmap;
123    enum vk_rmv_page_table_update_type type;
124 };
125 
126 struct vk_rmv_cpu_map_token {
127    uint64_t address;
128    bool unmapped;
129 };
130 
131 struct vk_rmv_virtual_free_token {
132    uint64_t address;
133 };
134 
135 enum vk_rmv_kernel_memory_domain {
136    VK_RMV_KERNEL_MEMORY_DOMAIN_CPU = 0x1,
137    VK_RMV_KERNEL_MEMORY_DOMAIN_GTT = 0x2,
138    VK_RMV_KERNEL_MEMORY_DOMAIN_VRAM = 0x4
139 };
140 
141 struct vk_rmv_virtual_allocate_token {
142    uint32_t page_count;
143    bool is_driver_internal;
144    bool is_in_invisible_vram;
145    uint64_t address;
146    enum vk_rmv_kernel_memory_domain preferred_domains;
147 };
148 
149 struct vk_rmv_image_description {
150    VkImageCreateFlags create_flags;
151    VkImageUsageFlags usage_flags;
152    VkImageType type;
153    VkExtent3D extent;
154    VkFormat format;
155    uint32_t num_mips;
156    uint32_t num_slices;
157    VkImageTiling tiling;
158 
159    uint32_t log2_samples;
160    uint32_t log2_storage_samples;
161 
162    uint32_t alignment_log2;
163    uint32_t metadata_alignment_log2;
164    uint32_t image_alignment_log2;
165 
166    uint64_t size;
167    uint64_t metadata_size;
168    uint64_t metadata_header_size;
169 
170    uint64_t metadata_offset;
171    uint64_t metadata_header_offset;
172 
173    bool presentable;
174 };
175 
176 struct vk_rmv_event_description {
177    VkEventCreateFlags flags;
178 };
179 
180 struct vk_rmv_border_color_palette_description {
181    uint8_t num_entries;
182 };
183 
184 struct vk_rmv_buffer_description {
185    VkBufferCreateFlags create_flags;
186    VkBufferUsageFlags usage_flags;
187    uint64_t size;
188 };
189 
190 struct vk_rmv_query_pool_description {
191    VkQueryType type;
192    bool has_cpu_access;
193 };
194 
195 /* The heap description refers to a VkDeviceMemory resource. */
196 struct vk_rmv_heap_description {
197    VkMemoryAllocateFlags alloc_flags;
198    uint64_t size;
199    uint32_t alignment;
200    uint32_t heap_index;
201 };
202 
203 struct vk_rmv_pipeline_description {
204    bool is_internal;
205    uint64_t hash_lo;
206    uint64_t hash_hi;
207    VkShaderStageFlags shader_stages;
208    bool is_ngg;
209 };
210 
211 struct vk_rmv_descriptor_pool_description {
212    uint32_t max_sets;
213    uint32_t pool_size_count;
214    VkDescriptorPoolSize *pool_sizes;
215 };
216 
217 struct vk_rmv_command_buffer_description {
218    enum vk_rmv_kernel_memory_domain preferred_domain;
219    uint64_t executable_size;
220    uint64_t app_available_executable_size;
221    uint64_t embedded_data_size;
222    uint64_t app_available_embedded_data_size;
223    uint64_t scratch_size;
224    uint64_t app_available_scratch_size;
225 };
226 
227 struct vk_rmv_resource_create_token {
228    uint32_t resource_id;
229    bool is_driver_internal;
230    enum vk_rmv_resource_type type;
231    union {
232       struct vk_rmv_event_description event;
233       struct vk_rmv_border_color_palette_description border_color_palette;
234       struct vk_rmv_image_description image;
235       struct vk_rmv_buffer_description buffer;
236       struct vk_rmv_query_pool_description query_pool;
237       struct vk_rmv_heap_description heap;
238       struct vk_rmv_pipeline_description pipeline;
239       struct vk_rmv_descriptor_pool_description descriptor_pool;
240       struct vk_rmv_command_buffer_description command_buffer;
241    };
242 };
243 
244 struct vk_rmv_resource_destroy_token {
245    uint32_t resource_id;
246 };
247 
248 struct vk_rmv_token {
249    enum vk_rmv_token_type type;
250    uint64_t timestamp;
251    union {
252       struct vk_rmv_timestamp_token timestamp;
253       struct vk_rmv_userdata_token userdata;
254       struct vk_rmv_misc_token misc;
255       struct vk_rmv_resource_bind_token resource_bind;
256       struct vk_rmv_resource_reference_token resource_reference;
257       struct vk_rmv_page_table_update_token page_table_update;
258       struct vk_rmv_cpu_map_token cpu_map;
259       struct vk_rmv_virtual_free_token virtual_free;
260       struct vk_rmv_virtual_allocate_token virtual_allocate;
261       struct vk_rmv_resource_create_token resource_create;
262       struct vk_rmv_resource_destroy_token resource_destroy;
263    } data;
264 };
265 
266 static inline size_t
vk_rmv_token_size_from_type(enum vk_rmv_token_type type)267 vk_rmv_token_size_from_type(enum vk_rmv_token_type type)
268 {
269    switch (type) {
270    case VK_RMV_TOKEN_TYPE_USERDATA:
271       return sizeof(struct vk_rmv_userdata_token);
272    case VK_RMV_TOKEN_TYPE_MISC:
273       return sizeof(struct vk_rmv_misc_token);
274    case VK_RMV_TOKEN_TYPE_RESOURCE_BIND:
275       return sizeof(struct vk_rmv_resource_bind_token);
276    case VK_RMV_TOKEN_TYPE_RESOURCE_REFERENCE:
277       return sizeof(struct vk_rmv_resource_reference_token);
278    case VK_RMV_TOKEN_TYPE_PAGE_TABLE_UPDATE:
279       return sizeof(struct vk_rmv_page_table_update_token);
280    case VK_RMV_TOKEN_TYPE_CPU_MAP:
281       return sizeof(struct vk_rmv_cpu_map_token);
282    case VK_RMV_TOKEN_TYPE_VIRTUAL_FREE:
283       return sizeof(struct vk_rmv_virtual_free_token);
284    case VK_RMV_TOKEN_TYPE_VIRTUAL_ALLOCATE:
285       return sizeof(struct vk_rmv_virtual_allocate_token);
286    case VK_RMV_TOKEN_TYPE_RESOURCE_CREATE:
287       return sizeof(struct vk_rmv_resource_create_token);
288    case VK_RMV_TOKEN_TYPE_RESOURCE_DESTROY:
289       return sizeof(struct vk_rmv_resource_destroy_token);
290    default:
291       unreachable("invalid token type");
292    }
293 }
294 
295 #endif
296