Skip to content
Merged
47 changes: 34 additions & 13 deletions src/asahi/lib/agx_bo.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,37 @@ agx_bo_ensure_ro_shadow(struct agx_device *dev, struct agx_bo *bo)
return ret;
}

/* Hands out a freshly allocated or reused BO. */
static struct agx_bo *
agx_bo_publish(struct agx_device *dev, struct agx_bo *bo, const char *label)
{
bo->label = label;
p_atomic_set(&bo->refcnt, 1);

if (dev->debug &
(AGX_DBG_TRACE | AGX_DBG_ASYNCTRACE)) {
agx_bo_map(bo);
agxdecode_track_alloc(dev->agxdecode, bo);
}

agx_bo_dump_all_periodic(dev);
return bo;
}

/* A BO whose only GPU address is `va`, which it takes over, also when it
* fails. Never cached: the address is free again once the BO is. */
struct agx_bo *
agx_bo_create_at(struct agx_device *dev, size_t size, enum agx_bo_flags flags,
struct agx_va *va, const char *label)
{
assert(size > 0 && va != NULL);
flags |= AGX_BO_NO_CACHE;

struct agx_bo *bo = dev->ops.bo_alloc(dev, ALIGN_POT(size, AIL_PAGESIZE),
AIL_PAGESIZE, flags, va);
return bo ? agx_bo_publish(dev, bo, label) : NULL;
}

struct agx_bo *
agx_bo_create_checked(struct agx_device *dev, size_t size, unsigned align,
enum agx_bo_flags flags, const char *label,
Expand Down Expand Up @@ -786,14 +817,14 @@ agx_bo_create_checked(struct agx_device *dev, size_t size, unsigned align,
* flush the cache to make space for the new allocation.
*/
if (!bo)
bo = dev->ops.bo_alloc(dev, size, align, flags);
bo = dev->ops.bo_alloc(dev, size, align, flags, NULL);
if (!bo) {
bo = agx_bo_cache_fetch(dev, size, align, flags, false);
reused = bo != NULL;
}
if (!bo) {
agx_bo_cache_evict_all(dev);
bo = dev->ops.bo_alloc(dev, size, align, flags);
bo = dev->ops.bo_alloc(dev, size, align, flags, NULL);
}

if (!bo) {
Expand Down Expand Up @@ -839,17 +870,7 @@ agx_bo_create_checked(struct agx_device *dev, size_t size, unsigned align,
memset(map, 0, bo->size);
}

bo->label = label;
p_atomic_set(&bo->refcnt, 1);

if (dev->debug &
(AGX_DBG_TRACE | AGX_DBG_ASYNCTRACE)) {
agx_bo_map(bo);
agxdecode_track_alloc(dev->agxdecode, bo);
}

agx_bo_dump_all_periodic(dev);
return bo;
return agx_bo_publish(dev, bo, label);
}

struct agx_bo *
Expand Down
3 changes: 3 additions & 0 deletions src/asahi/lib/agx_bo.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ struct agx_bo *agx_bo_create_checked(struct agx_device *dev, size_t size,
struct agx_bo *agx_bo_create(struct agx_device *dev, size_t size,
unsigned align, enum agx_bo_flags flags,
const char *label);
struct agx_bo *agx_bo_create_at(struct agx_device *dev, size_t size,
enum agx_bo_flags flags, struct agx_va *va,
const char *label);

int agx_bo_ensure_ro_shadow(struct agx_device *dev, struct agx_bo *bo);

Expand Down
5 changes: 3 additions & 2 deletions src/asahi/lib/agx_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ agx_bind_timestamps(struct agx_device *dev, struct agx_bo *bo, uint32_t *handle)

static struct agx_bo *
agx_bo_alloc(struct agx_device *dev, size_t size, size_t align,
enum agx_bo_flags flags)
enum agx_bo_flags flags, struct agx_va *va)
{
struct agx_bo *bo;
unsigned handle = 0;
Expand All @@ -430,6 +430,7 @@ agx_bo_alloc(struct agx_device *dev, size_t size, size_t align,
int ret = drmIoctl(dev->fd, DRM_IOCTL_ASAHI_GEM_CREATE, &gem_create);
if (ret) {
fprintf(stderr, "DRM_IOCTL_ASAHI_GEM_CREATE failed: %m\n");
agx_va_free(dev, va, false);
return NULL;
}

Expand All @@ -451,7 +452,7 @@ agx_bo_alloc(struct agx_device *dev, size_t size, size_t align,
bo->prime_fd = -1;

enum agx_va_flags va_flags = flags & AGX_BO_LOW_VA ? AGX_VA_USC : 0;
bo->va = agx_va_alloc(dev, size, bo->align, va_flags, 0);
bo->va = va ?: agx_va_alloc(dev, size, bo->align, va_flags, 0);
if (!bo->va) {
fprintf(stderr, "Failed to allocate BO VMA\n");
agx_bo_free(dev, bo);
Expand Down
4 changes: 3 additions & 1 deletion src/asahi/lib/agx_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ struct agx_submit_virt {
};

typedef struct {
/* A BO at `va` (agx_bo_create_at()), which it takes over and frees on
* failure too, or at a VA of its own when `va` is NULL. */
struct agx_bo *(*bo_alloc)(struct agx_device *dev, size_t size, size_t align,
enum agx_bo_flags flags);
enum agx_bo_flags flags, struct agx_va *va);
int (*bo_bind)(struct agx_device *dev, struct drm_asahi_gem_bind_op *ops,
uint32_t count);
void (*bo_mmap)(struct agx_device *dev, struct agx_bo *bo, void *fixed_addr,
Expand Down
6 changes: 4 additions & 2 deletions src/asahi/lib/agx_device_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ agx_virtio_simple_ioctl(struct agx_device *dev, unsigned cmd, void *_req)

static struct agx_bo *
agx_virtio_bo_alloc(struct agx_device *dev, size_t size, size_t align,
enum agx_bo_flags flags)
enum agx_bo_flags flags, struct agx_va *va)
{
struct agx_bo *bo;
unsigned handle = 0;
Expand All @@ -79,7 +79,8 @@ agx_virtio_bo_alloc(struct agx_device *dev, size_t size, size_t align,
uint32_t blob_id = p_atomic_inc_return(&dev->next_blob_id);

enum agx_va_flags va_flags = flags & AGX_BO_LOW_VA ? AGX_VA_USC : 0;
struct agx_va *va = agx_va_alloc(dev, size, align, va_flags, 0);
if (!va)
va = agx_va_alloc(dev, size, align, va_flags, 0);
if (!va) {
fprintf(stderr, "Failed to allocate BO VMA\n");
return NULL;
Expand All @@ -92,6 +93,7 @@ agx_virtio_bo_alloc(struct agx_device *dev, size_t size, size_t align,
handle = vdrm_bo_create(dev->vdrm, size, blob_flags, blob_id, 0, &req.hdr);
if (!handle) {
fprintf(stderr, "vdrm_bo_created failed\n");
agx_va_free(dev, va, false);
return NULL;
}

Expand Down
3 changes: 2 additions & 1 deletion src/asahi/lib/tests/test-agx-bo-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ static struct mock_state mock;

static struct agx_bo *
mock_bo_alloc(struct agx_device *dev, size_t size, size_t align,
enum agx_bo_flags flags)
enum agx_bo_flags flags, struct agx_va *va)
{
assert(va == NULL);
mock.alloc_calls++;
const uint32_t handle = ++mock.next_handle;

Expand Down
37 changes: 37 additions & 0 deletions src/asahi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,43 @@ if with_tools.contains('drm-shim') and with_asahi_vk and with_tests
timeout : 120,
)

# Buffer device address capture/replay through capture/replay memory alone.
foreach chip : ['g13g', 'g17p']
test(
'brl-' + chip + '-capture-replay',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_capture_replay,
],
env : [
'ASAHI_SHIM_CHIP=' + chip,
'VK_DRIVER_FILES=' + _dev_icd.full_path(),
],
depends : [_dev_icd, libvulkan_asahi],
suite : ['asahi'],
)
endforeach

# A wait for a host-set event, also one without barriers, gives its
# command buffer memory scope at its end, before memory first aliases.
test(
'brl-g17p-event-alias',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_event_alias,
],
env : [
'ASAHI_SHIM_CHIP=g17p',
'VK_DRIVER_FILES=' + _dev_icd.full_path(),
],
depends : [_dev_icd, libvulkan_asahi],
suite : ['asahi'],
)

# Explicit DRM plane layouts: placement, requirements, subresource layouts
# and validation, without executing on the GPU.
foreach chip : ['g13g', 'g17p']
Expand Down
35 changes: 30 additions & 5 deletions src/asahi/vulkan/hk_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,26 @@ hk_CreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo,
if (buffer->vk.create_flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
p_atomic_set(&dev->sparse_used, 1);

if (buffer->vk.create_flags &
VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT) {
uint64_t sparse_map_addr;
hk_get_buffer_replay_addrs(pCreateInfo, &buffer->opaque_capture_address,
&sparse_map_addr);
}

/* A sparse buffer has an address of its own, and so has a capture/replay
* buffer of VK_EXT_buffer_device_address, whose memory need not replay
* and whose address may be asked before the bind
* (VUID-vkGetBufferDeviceAddress-bufferDeviceAddress-03324).
* Any other buffer takes its memory's address plus the offset, which
* replays with capture/replay memory
* (VUID-VkBindBufferMemoryInfo-bufferDeviceAddressCaptureReplay-09200,
* VUID-VkBindBufferMemoryInfo-buffer-09201). */
if (buffer->vk.size > 0 &&
(buffer->vk.create_flags &
(VK_BUFFER_CREATE_SPARSE_BINDING_BIT |
VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT))) {
((buffer->vk.create_flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) ||
((buffer->vk.create_flags &
VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT) &&
dev->vk.enabled_features.bufferDeviceAddressCaptureReplayEXT))) {

const uint32_t alignment = hk_get_buffer_alignment(
hk_device_physical(dev), buffer->vk.usage, buffer->vk.create_flags);
Expand Down Expand Up @@ -736,8 +752,8 @@ hk_BindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
assert(buffer->vk.device_address == 0);
buffer->vk.device_address =
hk_device_memory_addr(mem) + pBindInfos[i].memoryOffset;
if (!mem->replay_va &&
!(mem->bo->flags & (AGX_BO_SHARED | AGX_BO_SHAREABLE | AGX_BO_HOST_PTR))) {
if (!(mem->bo->flags &
(AGX_BO_SHARED | AGX_BO_SHAREABLE | AGX_BO_HOST_PTR))) {
buffer->storage_backing_handle = mem->bo->handle;
buffer->storage_backing_offset = pBindInfos[i].memoryOffset;
}
Expand Down Expand Up @@ -792,6 +808,15 @@ hk_GetBufferOpaqueCaptureAddress(UNUSED VkDevice device,
{
VK_FROM_HANDLE(hk_buffer, buffer, pInfo->buffer);

/* The address asked for is the answer (vkGetBufferOpaqueCaptureAddress),
* also before the bind, which this query does not need. A KHR
* capture/replay buffer has no address of its own until then: 0 is no
* specific address. */
if (buffer->opaque_capture_address)
return buffer->opaque_capture_address;
if (!buffer->vk.device_address)
return 0;

return hk_buffer_address_rw(buffer, 0);
}

Expand Down
3 changes: 3 additions & 0 deletions src/asahi/vulkan/hk_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ struct hk_buffer {
/** Reserved VA for sparse buffers, NULL otherwise. */
struct agx_va *va;

/** Capture/replay address the application asked for, 0 for none. */
uint64_t opaque_capture_address;

/** Bytes of that VA counted in hk_device::sparse_scratch_B. */
uint64_t sparse_scratch_B;

Expand Down
32 changes: 9 additions & 23 deletions src/asahi/vulkan/hk_cmd_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,25 +229,8 @@ hk_cmd_buffer_move_sparse_sites(struct hk_cmd_buffer *cmd, const void *from,
}
}

void
hk_cmd_buffer_move_alias_sites(struct hk_cmd_buffer *cmd, const void *from,
const void *from_end, uint8_t *to, size_t skip)
{
util_dynarray_foreach(&cmd->alias_sites, struct hk_alias_site, site) {
const uint8_t *word = (const uint8_t *)site->word;
if (word < (const uint8_t *)from || word >= (const uint8_t *)from_end)
continue;

/* A site in the folded leading record moves to the trailing record
* that absorbed it. */
const size_t offset = word - (const uint8_t *)from;
site->word = (uint32_t *)(offset < skip ? to - AGX3_CDM_BARRIER_SIZE
: to + offset - skip);
}
}

/* At vkEndCommandBuffer, once nothing moves the streams any more: a command
* buffer that uses events gets memory scope throughout
* buffer that waits for a host-set event gets memory scope throughout
* (hk_cmd_alias_scope()), one recorded while a resource may alias by now is
* written at once, and any other waits in host_alias.cmd_buffers for
* hk_device_patch_alias_sites(), besides the check at submission. */
Expand All @@ -257,7 +240,7 @@ hk_cmd_buffer_end_alias_sites(struct hk_cmd_buffer *cmd)
if (!cmd->alias_sites.size)
return;

if (cmd->state.uses_events) {
if (cmd->state.alias_host_wait) {
hk_cmd_buffer_patch_alias_sites(cmd);
return;
}
Expand Down Expand Up @@ -1318,8 +1301,8 @@ hk_EndCommandBuffer(VkCommandBuffer commandBuffer)
* with ENOTSUPP ("G18 compute admission rejected ... stream_bytes=52").
*/
if (cs->type == HK_CS_CDM && !cs->g18_dispatch_sealed) {
/* With host_alias.dynamic, every stream holding work ends with the
* memory-scoped strong flush, also where its last record is an
/* M5 with host_alias.dynamic: every stream holding work ends with
* the memory-scoped strong flush, also where its last record is an
* application barrier recorded without memory scope. Each command
* then writes back what it wrote by its end, as the memory scope of
* each barrier did under the per-device rule, so a later command
Expand All @@ -1329,8 +1312,11 @@ hk_EndCommandBuffer(VkCommandBuffer commandBuffer)
* another device's, process's or API's mapping of the same pages,
* which Vulkan orders after this work only at a submission
* boundary. Submit packing keeps the scope only at a packet's end
* while no resource may alias. */
if (dev->host_alias.dynamic &&
* while no resource may alias. The end of a G17P command already
* publishes its writes to every address (bt xsub with the second
* address created while the writer runs reads exact), so G17P
* streams end with their own records. */
if (dev->host_alias.dynamic && agx_chip_g17_m5(dev->dev.chip) &&
hk_cdm_visibility_flush_needed(dev, cs, true)) {
if (cs->current != cs->weak_barrier_end &&
cs->current != cs->strong_flush_end)
Expand Down
Loading