hk,asahi: move scratch BO to common

gl needs this too.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35797>
This commit is contained in:
Alyssa Rosenzweig 2025-04-24 11:40:28 -04:00 committed by Marge Bot
parent d3adef3164
commit db2891ec5f
7 changed files with 24 additions and 22 deletions

View file

@ -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)

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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,