From db2891ec5f3a1dde8d52b73779ec74338cf6bd74 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 24 Apr 2025 11:40:28 -0400 Subject: [PATCH] hk,asahi: move scratch BO to common gl needs this too. Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/lib/agx_abi.h | 2 ++ src/asahi/lib/agx_device.c | 18 ++++++++++++++++++ src/asahi/lib/agx_device.h | 2 +- src/asahi/vulkan/hk_buffer.c | 2 +- src/asahi/vulkan/hk_device.c | 12 +----------- src/asahi/vulkan/hk_device.h | 8 -------- src/asahi/vulkan/hk_queue.c | 2 +- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/asahi/lib/agx_abi.h b/src/asahi/lib/agx_abi.h index bd20e451775..d7981c87fe7 100644 --- a/src/asahi/lib/agx_abi.h +++ b/src/asahi/lib/agx_abi.h @@ -57,3 +57,5 @@ */ #define AGX_ZERO_PAGE_ADDRESS (((uint64_t)1) << 32) #define AGX_ZERO_PAGE_SIZE (16384) + +#define AGX_SCRATCH_PAGE_ADDRESS (AGX_ZERO_PAGE_ADDRESS + AGX_ZERO_PAGE_SIZE) diff --git a/src/asahi/lib/agx_device.c b/src/asahi/lib/agx_device.c index 3e75efe519a..fbc42db621c 100644 --- a/src/asahi/lib/agx_device.c +++ b/src/asahi/lib/agx_device.c @@ -676,6 +676,23 @@ agx_open_device(void *memctx, struct agx_device *dev) dev->zero_bo = bo; } + { + void *bo = agx_bo_create(dev, AIL_PAGESIZE, 0, 0, "Scratch page"); + int ret = agx_bo_bind(dev, bo, AGX_SCRATCH_PAGE_ADDRESS, AIL_PAGESIZE, 0, + DRM_ASAHI_BIND_READ | DRM_ASAHI_BIND_WRITE); + if (ret) { + fprintf(stderr, "Failed to bind zero page"); + return false; + } + + dev->scratch_bo = bo; + + /* The contents of the scratch page are undefined, but making them nonzero + * helps fuzz for bugs where we incorrectly read from the write section. + */ + memset(agx_bo_map(dev->scratch_bo), 0xCA, AIL_PAGESIZE); + } + void *bo = agx_bo_create(dev, LIBAGX_PRINTF_BUFFER_SIZE, 0, AGX_BO_WRITEBACK, "Printf/abort"); @@ -696,6 +713,7 @@ agx_close_device(struct agx_device *dev) { agx_bo_unreference(dev, dev->printf.bo); agx_bo_unreference(dev, dev->zero_bo); + agx_bo_unreference(dev, dev->scratch_bo); u_printf_destroy(&dev->printf); agx_bo_cache_evict_all(dev); util_sparse_array_finish(&dev->bo_map); diff --git a/src/asahi/lib/agx_device.h b/src/asahi/lib/agx_device.h index 52620660862..7366c148c8c 100644 --- a/src/asahi/lib/agx_device.h +++ b/src/asahi/lib/agx_device.h @@ -134,7 +134,7 @@ struct agx_device { */ uint64_t sparse_ro_offset; - struct agx_bo *zero_bo; + struct agx_bo *zero_bo, *scratch_bo; struct renderonly *ro; diff --git a/src/asahi/vulkan/hk_buffer.c b/src/asahi/vulkan/hk_buffer.c index c4672f55c20..b6c886d7700 100644 --- a/src/asahi/vulkan/hk_buffer.c +++ b/src/asahi/vulkan/hk_buffer.c @@ -85,7 +85,7 @@ hk_bind_scratch(struct hk_device *dev, struct agx_va *va, unsigned offset_B, uint32_t flags = DRM_ASAHI_BIND_READ | DRM_ASAHI_BIND_SINGLE_PAGE; /* Map read-write scratch to the primary (bottom half) VA range */ - int ret = agx_bo_bind(&dev->dev, dev->sparse.write, addr, size_B, 0, + int ret = agx_bo_bind(&dev->dev, dev->dev.scratch_bo, addr, size_B, 0, flags | DRM_ASAHI_BIND_WRITE); if (ret) return VK_ERROR_UNKNOWN; diff --git a/src/asahi/vulkan/hk_device.c b/src/asahi/vulkan/hk_device.c index b2e48db03ea..0c826fbf527 100644 --- a/src/asahi/vulkan/hk_device.c +++ b/src/asahi/vulkan/hk_device.c @@ -60,17 +60,9 @@ hk_upload_rodata(struct hk_device *dev) dev->rodata.bo = agx_bo_create(&dev->dev, AGX_SAMPLER_LENGTH, 0, 0, "Read only data"); - dev->sparse.write = - agx_bo_create(&dev->dev, AIL_PAGESIZE, 0, 0, "Sparse write page"); - - if (!dev->rodata.bo || !dev->sparse.write) + if (!dev->rodata.bo) return VK_ERROR_OUT_OF_HOST_MEMORY; - /* The contents of sparse.write are undefined, but making them nonzero helps - * fuzz for bugs where we incorrectly read from the write section. - */ - memset(agx_bo_map(dev->sparse.write), 0xCA, AIL_PAGESIZE); - uint8_t *map = agx_bo_map(dev->rodata.bo); uint32_t offs = 0; @@ -507,7 +499,6 @@ fail_queue: hk_queue_finish(dev, &dev->queue); fail_rodata: agx_bo_unreference(&dev->dev, dev->rodata.bo); - agx_bo_unreference(&dev->dev, dev->sparse.write); fail_bg_eot: agx_bg_eot_cleanup(&dev->bg_eot); fail_internal_shaders_2: @@ -564,7 +555,6 @@ hk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator) hk_descriptor_table_finish(dev, &dev->images); hk_descriptor_table_finish(dev, &dev->occlusion_queries); agx_bo_unreference(&dev->dev, dev->rodata.bo); - agx_bo_unreference(&dev->dev, dev->sparse.write); agx_bo_unreference(&dev->dev, dev->heap); agx_bg_eot_cleanup(&dev->bg_eot); agx_close_device(&dev->dev); diff --git a/src/asahi/vulkan/hk_device.h b/src/asahi/vulkan/hk_device.h index ff6af4a5ed0..6324fb42550 100644 --- a/src/asahi/vulkan/hk_device.h +++ b/src/asahi/vulkan/hk_device.h @@ -88,14 +88,6 @@ struct hk_device { uint64_t heap; } rodata; - /* Pages for backing sparse resources */ - struct { - /* Undefined content, should not be read (except for atomics where the - * result is already undefined). - */ - struct agx_bo *write; - } sparse; - struct hk_internal_shaders prolog_epilog; struct hk_internal_shaders kernels; struct hk_api_shader *null_fs; diff --git a/src/asahi/vulkan/hk_queue.c b/src/asahi/vulkan/hk_queue.c index e1129294abe..5011039d85d 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -476,7 +476,7 @@ hk_flush_bind(struct hk_bind_builder *b) struct drm_asahi_gem_bind_op op; if (!b->mem) { op = (struct drm_asahi_gem_bind_op){ - .handle = b->dev->sparse.write->uapi_handle, + .handle = b->dev->dev.scratch_bo->uapi_handle, .flags = DRM_ASAHI_BIND_READ | DRM_ASAHI_BIND_WRITE | DRM_ASAHI_BIND_SINGLE_PAGE, .addr = b->va->addr + b->resourceOffset,