On 03/09/2026 23:00, Thadeu Lima de Souza Cascardo wrote:
This allows dmem cgroups tests to run on top of amdgpu driver, adding
support to allocate and release VRAM memory.
This does this by allocating a BO from VRAM domain, which will try to
place BOs on VRAM, but may fallback to GTT.
Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>
---
lib/amdgpu/amd_dmem.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++
lib/igt_dmem_driver.h | 1 +
lib/meson.build | 1 +
3 files changed, 92 insertions(+)
diff --git a/lib/amdgpu/amd_dmem.c b/lib/amdgpu/amd_dmem.c
new file mode 100644
index 000000000000..cb0fed4b870a
--- /dev/null
+++ b/lib/amdgpu/amd_dmem.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ * Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#include <errno.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+#include "lib/amdgpu/amd_memory.h"
+
+struct amdgpu_dmem_ctx {
+ int fd;
+ amdgpu_device_handle device;
+};
+
+static int amdgpu_dmem_init(void **ctx, int fd)
+{
+ struct amdgpu_dmem_ctx *actx;
+ uint32_t major, minor;
+ int err;
+
+ actx = malloc(sizeof(*actx));
+ if (!actx)
+ return -ENOMEM;
+
+ err = amdgpu_device_initialize(fd, &major, &minor, &actx->device);
+ if (err)
+ goto out;
+
+ actx->fd = fd;
+
+ *ctx = actx;
+
+ return 0;
+
+out:
+ free(actx);
+
+ return err;
+}
+
+static void amdgpu_dmem_deinit(void *ctx)
+{
+ struct amdgpu_dmem_ctx *actx = ctx;
+
+ amdgpu_device_deinitialize(actx->device);
+ free(actx);
+}
+
+static char * amdgpu_dmem_get_region_name(void *ctx)
+{
+ struct amdgpu_dmem_ctx *actx = ctx;
+
+ return amdgpu_cgroup_region_name(actx->fd);
+}
+
+static int amdgpu_dmem_allocate_vram(void *ctx, size_t len, void **ret_handle)
+{
+ struct amdgpu_dmem_ctx *actx = ctx;
+ amdgpu_bo_handle handle;
+ int err;
+
+ err = amdgpu_bo_alloc_wrap(actx->device, len, 4096,
+ AMDGPU_GEM_DOMAIN_VRAM, 0, &handle);
+ if (err)
+ return err;
+
+ if (ret_handle)
+ *ret_handle = (void *) handle;
Is there an use case for ret_handle == NULL? Xe backend does not bother
with the check. This aside, the rest looks good to me:
Reviewed-by: Tvrtko Ursulin <[email protected]>
Regards,
Tvrtko
+
+ return 0;
+}
+
+static void amdgpu_dmem_free_vram(void *ctx, void *handle)
+{
+ amdgpu_bo_free(handle);
+}
+
+const struct igt_dmem_driver amdgpu_dmem_driver = {
+ .name = "amdgpu",
+ .get_region_name = amdgpu_dmem_get_region_name,
+ .init = amdgpu_dmem_init,
+ .deinit = amdgpu_dmem_deinit,
+ .allocate_vram = amdgpu_dmem_allocate_vram,
+ .free_vram = amdgpu_dmem_free_vram,
+};
diff --git a/lib/igt_dmem_driver.h b/lib/igt_dmem_driver.h
index e6998387eff9..f2d9d74196db 100644
--- a/lib/igt_dmem_driver.h
+++ b/lib/igt_dmem_driver.h
@@ -30,5 +30,6 @@ struct igt_dmem_driver {
};
extern const struct igt_dmem_driver xe_dmem_driver;
+extern const struct igt_dmem_driver amdgpu_dmem_driver;
#endif
diff --git a/lib/meson.build b/lib/meson.build
index 022408ce6864..e191793f0097 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -205,6 +205,7 @@ if libdrm_amdgpu.found()
'amdgpu/amd_mmd_shared.c',
'amdgpu/amd_jpeg_shared.c',
'amdgpu/amd_utils.c',
+ 'amdgpu/amd_dmem.c',
'amdgpu/amd_vcn_shared.c'
]
if libdrm_amdgpu.version().version_compare('> 2.4.99')