Debian Codegraph @

src/asahi/vulkan/hk_device.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7 #include "hk_device.h"
8
9 #include "agx_bg_eot.h"
10 #include "agx_helpers.h"
11 #include "agx_scratch.h"
12 #include "hk_cmd_buffer.h"
13 #include "hk_descriptor_table.h"
14 #include "hk_entrypoints.h"
15 #include "hk_instance.h"
16 #include "hk_physical_device.h"
17 #include "hk_shader.h"
18
19 #include "asahi/genxml/agx_pack.h"
20 #include "asahi/lib/agx_bo.h"
21 #include "asahi/lib/agx_device.h"
22 #include "compiler/nir/nir_builder.h"
23 #include "poly/geometry.h"
24 #include "util/hash_table.h"
25 #include "util/ralloc.h"
26 #include "util/simple_mtx.h"
27 #include "vulkan/vulkan_core.h"
28 #include "vulkan/wsi/wsi_common.h"
29 #include "layout.h"
30 #include "vk_cmd_enqueue_entrypoints.h"
31 #include "vk_common_entrypoints.h"
32 #include "vk_debug_utils.h"
33 #include "vk_device.h"
34 #include "vk_pipeline_cache.h"
35 #include "vk_shader.h"
36
37 #include <fcntl.h>
38 #include <xf86drm.h>
39
40 /* clang-format off */
41 static const struct debug_named_value hk_perf_options[] = {
42 {"notess", HK_PERF_NOTESS, "Skip draws with tessellation"},
43 {"noborder", HK_PERF_NOBORDER, "Disable custom border colour emulation"},
44 {"nobarrier", HK_PERF_NOBARRIER,"Ignore pipeline barriers"},
45 {"batch", HK_PERF_BATCH, "Batch submissions"},
46 {"norobust", HK_PERF_NOROBUST, "Disable robustness"},
47 DEBUG_NAMED_VALUE_END
48 };
49 /* clang-format on */
50
51 /*
52 * We preupload some constants so we can cheaply reference later without extra
53 * allocation and copying.
54 *
55 * TODO: This is small, don't waste a whole BO.
56 */
57 static VkResult
58 hk_upload_rodata(struct hk_device *dev)
59 {
60 dev->rodata.bo =
61 agx_bo_create(&dev->dev, AGX_SAMPLER_LENGTH, 0, 0, "Read only data");
62
63 if (!dev->rodata.bo)
64 return VK_ERROR_OUT_OF_HOST_MEMORY;
65
66 uint8_t *map = agx_bo_map(dev->rodata.bo);
67 uint32_t offs = 0;
68
69 offs = align(offs, 8);
70 agx_pack(&dev->dev.txf_sampler, USC_SAMPLER, cfg) {
71 cfg.start = 0;
72 cfg.count = 1;
73 cfg.buffer = dev->rodata.bo->va->addr + offs;
74 }
75
76 agx_pack_txf_sampler((struct agx_sampler_packed *)(map + offs));
77 offs += AGX_SAMPLER_LENGTH;
78
79 /* The heap descriptor isn't strictly readonly data, but we only have a
80 * single instance of it device-wide and -- after initializing at heap
81 * allocate time -- it is read-only from the CPU perspective. The GPU uses it
82 * for scratch, but is required to reset it after use to ensure resubmitting
83 * the same command buffer works.
84 *
85 * So, we allocate it here for convenience.
86 */
87 offs = align(offs, sizeof(uint64_t));
88 dev->rodata.heap = dev->rodata.bo->va->addr + offs;
89 offs += sizeof(struct poly_heap);
90
91 return VK_SUCCESS;
92 }
93
94 static uint32_t
95 internal_key_hash(const void *key_)
96 {
97 const struct hk_internal_key *key = key_;
98
99 return _mesa_hash_data(key, sizeof(struct hk_internal_key) + key->key_size);
100 }
101
102 static bool
103 internal_key_equal(const void *a_, const void *b_)
104 {
105 const struct hk_internal_key *a = a_;
106 const struct hk_internal_key *b = b_;
107
108 return a->builder == b->builder && a->key_size == b->key_size &&
109 memcmp(a->key, b->key, a->key_size) == 0;
110 }
111
112 static VkResult
113 hk_init_internal_shaders(struct hk_internal_shaders *s)
114 {
115 s->ht = _mesa_hash_table_create(NULL, internal_key_hash, internal_key_equal);
116 if (!s->ht)
117 return VK_ERROR_OUT_OF_HOST_MEMORY;
118
119 simple_mtx_init(&s->lock, mtx_plain);
120 return VK_SUCCESS;
121 }
122
123 static void
124 hk_destroy_internal_shaders(struct hk_device *dev,
125 struct hk_internal_shaders *s, bool part)
126 {
127 hash_table_foreach(s->ht, ent) {
128 if (part) {
129 struct agx_shader_part *part = ent->data;
130 free(part->binary);
131
132 /* The agx_shader_part itself is ralloc'd against the hash table so
133 * will be freed.
134 */
135 } else {
136 struct hk_api_shader *obj = ent->data;
137 hk_api_shader_destroy(&dev->vk, &obj->vk, NULL);
138 }
139 }
140
141 _mesa_hash_table_destroy(s->ht, NULL);
142 simple_mtx_destroy(&s->lock);
143 }
144
145 DERIVE_HASH_TABLE(agx_sampler_packed);
146
147 static VkResult
148 hk_init_sampler_heap(struct hk_device *dev, struct hk_sampler_heap *h)
149 {
150 h->ht = agx_sampler_packed_table_create(NULL);
151 if (!h->ht)
152 return VK_ERROR_OUT_OF_HOST_MEMORY;
153
154 VkResult result =
155 hk_descriptor_table_init(dev, &h->table, AGX_SAMPLER_LENGTH, 1024, 1024);
156
157 if (result != VK_SUCCESS) {
158 ralloc_free(h->ht);
159 return result;
160 }
161
162 simple_mtx_init(&h->lock, mtx_plain);
163 return VK_SUCCESS;
164 }
165
166 static void
167 hk_destroy_sampler_heap(struct hk_device *dev, struct hk_sampler_heap *h)
168 {
169 hk_descriptor_table_finish(dev, &h->table);
170 ralloc_free(h->ht);
171 simple_mtx_destroy(&h->lock);
172 }
173
174 static VkResult
175 hk_sampler_heap_add_locked(struct hk_device *dev, struct hk_sampler_heap *h,
176 struct agx_sampler_packed desc,
177 struct hk_rc_sampler **out)
178 {
179 struct hash_entry *ent = _mesa_hash_table_search(h->ht, &desc);
180 if (ent != NULL) {
181 *out = ent->data;
182
183 assert((*out)->refcount != 0);
184 (*out)->refcount++;
185
186 return VK_SUCCESS;
187 }
188
189 struct hk_rc_sampler *rc = ralloc(h->ht, struct hk_rc_sampler);
190 if (!rc)
191 return VK_ERROR_OUT_OF_HOST_MEMORY;
192
193 uint32_t index;
194 VkResult result =
195 hk_descriptor_table_add(dev, &h->table, &desc, sizeof(desc), &index);
196 if (result != VK_SUCCESS) {
197 ralloc_free(rc);
198 return result;
199 }
200
201 *rc = (struct hk_rc_sampler){
202 .key = desc,
203 .refcount = 1,
204 .index = index,
205 };
206
207 _mesa_hash_table_insert(h->ht, &rc->key, rc);
208 *out = rc;
209
210 return VK_SUCCESS;
211 }
212
213 VkResult
214 hk_sampler_heap_add(struct hk_device *dev, struct agx_sampler_packed desc,
215 struct hk_rc_sampler **out)
216 {
217 struct hk_sampler_heap *h = &dev->samplers;
218
219 simple_mtx_lock(&h->lock);
220 VkResult result = hk_sampler_heap_add_locked(dev, h, desc, out);
221 simple_mtx_unlock(&h->lock);
222
223 return result;
224 }
225
226 static void
227 hk_sampler_heap_remove_locked(struct hk_device *dev, struct hk_sampler_heap *h,
228 struct hk_rc_sampler *rc)
229 {
230 assert(rc->refcount != 0);
231 rc->refcount--;
232
233 if (rc->refcount == 0) {
234 hk_descriptor_table_remove(dev, &h->table, rc->index);
235 _mesa_hash_table_remove_key(h->ht, &rc->key);
236 ralloc_free(rc);
237 }
238 }
239
240 void
241 hk_sampler_heap_remove(struct hk_device *dev, struct hk_rc_sampler *rc)
242 {
243 struct hk_sampler_heap *h = &dev->samplers;
244
245 simple_mtx_lock(&h->lock);
246 hk_sampler_heap_remove_locked(dev, h, rc);
247 simple_mtx_unlock(&h->lock);
248 }
249
250 static VkResult
251 hk_check_status(struct vk_device *device)
252 {
253 struct hk_device *dev = container_of(device, struct hk_device, vk);
254 return vk_check_printf_status(&dev->vk, &dev->dev.printf);
255 }
256
257 static VkResult
258 hk_get_timestamp(struct vk_device *device, uint64_t *timestamp)
259 {
260 struct hk_device *dev = container_of(device, struct hk_device, vk);
261 *timestamp = agx_get_gpu_timestamp(&dev->dev);
262 return VK_SUCCESS;
263 }
264
265 VKAPI_ATTR VkResult VKAPI_CALL
266 hk_CreateDevice(VkPhysicalDevice physicalDevice,
267 const VkDeviceCreateInfo *pCreateInfo,
268 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice)
269 {
270 VK_FROM_HANDLE(hk_physical_device, pdev, physicalDevice);
271 VkResult result = VK_ERROR_OUT_OF_HOST_MEMORY;
272 struct hk_device *dev;
273 struct hk_instance *instance = (struct hk_instance *)pdev->vk.instance;
274
275 dev = vk_zalloc2(&instance->vk.alloc, pAllocator, sizeof(*dev), 8,
276 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
277 if (!dev)
278 return vk_error(pdev, VK_ERROR_OUT_OF_HOST_MEMORY);
279
280 struct vk_device_dispatch_table dispatch_table;
281
282 /* For secondary command buffer support, overwrite any command entrypoints
283 * in the main device-level dispatch table with
284 * vk_cmd_enqueue_unless_primary_Cmd*.
285 */
286 vk_device_dispatch_table_from_entrypoints(
287 &dispatch_table, &vk_cmd_enqueue_unless_primary_device_entrypoints, true);
288
289 vk_device_dispatch_table_from_entrypoints(&dispatch_table,
290 &hk_device_entrypoints, false);
291 vk_device_dispatch_table_from_entrypoints(&dispatch_table,
292 &wsi_device_entrypoints, false);
293
294 /* Populate primary cmd_dispatch table */
295 vk_device_dispatch_table_from_entrypoints(&dev->cmd_dispatch,
296 &hk_device_entrypoints, true);
297 vk_device_dispatch_table_from_entrypoints(&dev->cmd_dispatch,
298 &wsi_device_entrypoints, false);
299 vk_device_dispatch_table_from_entrypoints(
300 &dev->cmd_dispatch, &vk_common_device_entrypoints, false);
301
302 result = vk_device_init(&dev->vk, &pdev->vk, &dispatch_table, pCreateInfo,
303 pAllocator);
304 if (result != VK_SUCCESS)
305 goto fail_alloc;
306
307 dev->vk.shader_ops = &hk_device_shader_ops;
308 dev->vk.command_dispatch_table = &dev->cmd_dispatch;
309
310 drmDevicePtr drm_device = NULL;
311 int ret = drmGetDeviceFromDevId(pdev->render_dev, 0, &drm_device);
312 if (ret != 0) {
313 result = vk_errorf(dev, VK_ERROR_INITIALIZATION_FAILED,
314 "Failed to get DRM device: %m");
315 goto fail_init;
316 }
317
318 const char *path = drm_device->nodes[DRM_NODE_RENDER];
319 dev->dev.fd = open(path, O_RDWR | O_CLOEXEC);
320 if (dev->dev.fd < 0) {
321 drmFreeDevice(&drm_device);
322 result = vk_errorf(dev, VK_ERROR_INITIALIZATION_FAILED,
323 "failed to open device %s", path);
324 goto fail_init;
325 }
326
327 dev->perftest = debug_get_flags_option("HK_PERFTEST", hk_perf_options, 0);
328
329 if (instance->no_border) {
330 dev->perftest |= HK_PERF_NOBORDER;
331 }
332
333 if (HK_PERF(dev, NOROBUST)) {
334 dev->vk.enabled_features.robustBufferAccess = false;
335 dev->vk.enabled_features.robustBufferAccess2 = false;
336 dev->vk.enabled_features.robustImageAccess = false;
337 dev->vk.enabled_features.robustImageAccess2 = false;
338 dev->vk.enabled_features.pipelineRobustness = false;
339 }
340
341 bool succ = agx_open_device(NULL, &dev->dev);
342 drmFreeDevice(&drm_device);
343 if (!succ) {
344 result = vk_errorf(dev, VK_ERROR_INITIALIZATION_FAILED,
345 "Failed to get DRM device: %m");
346 goto fail_fd;
347 }
348
349 vk_device_set_drm_fd(&dev->vk, dev->dev.fd);
350 dev->vk.command_buffer_ops = &hk_cmd_buffer_ops;
351 dev->vk.check_status = hk_check_status;
352 dev->vk.get_timestamp = hk_get_timestamp;
353
354 /* This holds for current platforms. We do not currently implement
355 * timestamp scaling, this would require changes in the query copy kernel
356 * as well. Calibrated timestamps depends on this.
357 */
358 assert(dev->dev.user_timestamp_to_ns.num ==
359 dev->dev.user_timestamp_to_ns.den &&
360 "user timestamps are in ns");
361
362 result = hk_init_sampler_heap(dev, &dev->samplers);
363 if (result != VK_SUCCESS)
364 goto fail_dev;
365
366 result = hk_descriptor_table_init(
367 dev, &dev->occlusion_queries, sizeof(uint64_t), AGX_MAX_OCCLUSION_QUERIES,
368 AGX_MAX_OCCLUSION_QUERIES);
369 if (result != VK_SUCCESS)
370 goto fail_samplers;
371
372 result = hk_upload_rodata(dev);
373 if (result != VK_SUCCESS)
374 goto fail_queries;
375
376 /* XXX: error handling, and should this even go on the device? */
377 agx_bg_eot_init(&dev->bg_eot, &dev->dev);
378 if (!dev->bg_eot.ht) {
379 result = VK_ERROR_OUT_OF_HOST_MEMORY;
380 goto fail_rodata;
381 }
382
383 result = hk_init_internal_shaders(&dev->prolog_epilog);
384 if (result != VK_SUCCESS)
385 goto fail_bg_eot;
386
387 result = hk_init_internal_shaders(&dev->kernels);
388 if (result != VK_SUCCESS)
389 goto fail_internal_shaders;
390
391 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
392 for (unsigned q = 0; q < pCreateInfo->pQueueCreateInfos[i].queueCount;
393 q++) {
394 result = hk_queue_init(dev, &pCreateInfo->pQueueCreateInfos[i], q);
395 if (result != VK_SUCCESS)
396 goto fail_queues;
397 }
398 }
399
400 struct vk_pipeline_cache_create_info cache_info = {
401 .weak_ref = true,
402 };
403 dev->vk.mem_cache = vk_pipeline_cache_create(&dev->vk, &cache_info, NULL);
404 if (dev->vk.mem_cache == NULL) {
405 result = VK_ERROR_OUT_OF_HOST_MEMORY;
406 goto fail_queues;
407 }
408
409 result = hk_device_init_meta(dev);
410 if (result != VK_SUCCESS)
411 goto fail_mem_cache;
412
413 /* Precompile an empty fragment shader that can be used to handle API-level
414 * null fragment shaders. We do this at device-time to make binds cheap.
415 * Regardless, compiling this shader should be fast.
416 */
417 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT,
418 &agx_nir_options, "empty FS");
419 struct vk_shader_compile_info info = {
420 .nir = b.shader,
421 .robustness = &vk_robustness_disabled,
422 .stage = MESA_SHADER_FRAGMENT,
423 };
424 hk_compile_shader(dev, &info, NULL, NULL, pAllocator, &dev->null_fs);
425 if (!dev->null_fs) {
426 result = VK_ERROR_OUT_OF_HOST_MEMORY;
427 goto fail_meta;
428 }
429
430 *pDevice = hk_device_to_handle(dev);
431
432 simple_mtx_init(&dev->scratch.lock, mtx_plain);
433 agx_scratch_init(&dev->dev, &dev->scratch.vs);
434 agx_scratch_init(&dev->dev, &dev->scratch.fs);
435 agx_scratch_init(&dev->dev, &dev->scratch.cs);
436
437 u_rwlock_init(&dev->external_bos.lock);
438 dev->external_bos.counts = UTIL_DYNARRAY_INIT;
439 dev->external_bos.list = UTIL_DYNARRAY_INIT;
440
441 return VK_SUCCESS;
442
443 fail_meta:
444 hk_device_finish_meta(dev);
445 fail_mem_cache:
446 vk_pipeline_cache_destroy(dev->vk.mem_cache, NULL);
447 fail_queues:
448 vk_foreach_queue_safe(iter, &dev->vk) {
449 struct hk_queue *queue = container_of(iter, struct hk_queue, vk);
450 hk_queue_finish(dev, queue);
451 }
452 hk_destroy_internal_shaders(dev, &dev->kernels, false);
453 fail_internal_shaders:
454 hk_destroy_internal_shaders(dev, &dev->prolog_epilog, true);
455 fail_bg_eot:
456 agx_bg_eot_cleanup(&dev->bg_eot);
457 fail_rodata:
458 agx_bo_unreference(&dev->dev, dev->rodata.bo);
459 fail_queries:
460 hk_descriptor_table_finish(dev, &dev->occlusion_queries);
461 fail_samplers:
462 hk_destroy_sampler_heap(dev, &dev->samplers);
463 fail_dev:
464 agx_close_device(&dev->dev);
465 fail_fd:
466 close(dev->dev.fd);
467 fail_init:
468 vk_device_finish(&dev->vk);
469 fail_alloc:
470 vk_free(&dev->vk.alloc, dev);
471 return result;
472 }
473
474 VKAPI_ATTR void VKAPI_CALL
475 hk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
476 {
477 VK_FROM_HANDLE(hk_device, dev, _device);
478
479 if (!dev)
480 return;
481
482 util_dynarray_fini(&dev->external_bos.counts);
483 util_dynarray_fini(&dev->external_bos.list);
484 u_rwlock_destroy(&dev->external_bos.lock);
485
486 hk_device_finish_meta(dev);
487 hk_destroy_internal_shaders(dev, &dev->kernels, false);
488 hk_destroy_internal_shaders(dev, &dev->prolog_epilog, true);
489
490 vk_pipeline_cache_destroy(dev->vk.mem_cache, NULL);
491
492 vk_foreach_queue_safe(iter, &dev->vk) {
493 struct hk_queue *queue = container_of(iter, struct hk_queue, vk);
494 hk_queue_finish(dev, queue);
495 }
496
497 vk_device_finish(&dev->vk);
498
499 agx_scratch_fini(&dev->scratch.vs);
500 agx_scratch_fini(&dev->scratch.fs);
501 agx_scratch_fini(&dev->scratch.cs);
502 simple_mtx_destroy(&dev->scratch.lock);
503
504 if (dev->null_fs) {
505 hk_api_shader_destroy(&dev->vk, &dev->null_fs->vk, pAllocator);
506 }
507
508 hk_destroy_sampler_heap(dev, &dev->samplers);
509 hk_descriptor_table_finish(dev, &dev->occlusion_queries);
510 agx_bo_unreference(&dev->dev, dev->rodata.bo);
511 agx_bo_unreference(&dev->dev, dev->heap);
512 agx_bg_eot_cleanup(&dev->bg_eot);
513 agx_close_device(&dev->dev);
514 vk_free(&dev->vk.alloc, dev);
515 }