Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
To: igt-dev@lists.freedesktop.org
Cc: dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	intel-xe@lists.freedesktop.org,
	"Christian Koenig" <christian.koenig@amd.com>,
	maarten.lankhorst@linux.intel.com,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Natalie Vock" <natalie.vock@gmx.de>,
	kernel-dev@igalia.com,
	"Tvrtko Ursulin" <tvrtko.ursulin@igalia.com>,
	"Thadeu Lima de Souza Cascardo" <cascardo@igalia.com>
Subject: [PATCH i-g-t 5/8] igt_dmem_driver: add amdgpu support
Date: Tue, 12 May 2026 18:51:52 -0300	[thread overview]
Message-ID: <20260512215156.4083082-6-cascardo@igalia.com> (raw)
In-Reply-To: <20260512215156.4083082-1-cascardo@igalia.com>

Allocate a BO from VRAM domain. That will try to place BOs on VRAM, but may
fallback to GTT. That can still be tracked with dmem.current.

Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
 lib/amdgpu/amd_dmem.c    | 94 ++++++++++++++++++++++++++++++++++++++++
 lib/igt_dmem_driver.h    |  1 +
 lib/meson.build          |  1 +
 tests/drv_dmem_cgroups.c |  6 ++-
 4 files changed, 100 insertions(+), 2 deletions(-)
 create mode 100644 lib/amdgpu/amd_dmem.c

diff --git a/lib/amdgpu/amd_dmem.c b/lib/amdgpu/amd_dmem.c
new file mode 100644
index 000000000000..1c0825af43b6
--- /dev/null
+++ b/lib/amdgpu/amd_dmem.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
+ */
+
+#include "igt_dmem_driver.h"
+
+#include <errno.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+#include "lib/amdgpu/amd_memory.h"
+
+struct amdgpu_dmem_ctx {
+	int fd;
+	amdgpu_device_handle device;
+	amdgpu_bo_handle *handles;
+};
+
+static int amdgpu_dmem_init(void **ctx, int fd, int max_bo)
+{
+	struct amdgpu_dmem_ctx *actx;
+	uint32_t major, minor;
+	int err = -ENOMEM;
+
+	actx = malloc(sizeof(*actx));
+	if (!actx)
+		return -ENOMEM;
+
+	actx->handles = calloc(max_bo, sizeof(actx->handles[0]));
+	if (!actx->handles)
+		goto out;
+
+	err = amdgpu_device_initialize(fd, &major, &minor, &actx->device);
+	if (err)
+		goto out;
+
+	*ctx = actx;
+
+	return 0;
+
+out:
+	if (actx->handles)
+		free(actx->handles);
+	free(actx);
+
+	return err;
+}
+
+static void amdgpu_dmem_deinit(void *ctx)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;
+
+	amdgpu_device_deinitialize(actx->device);
+	free(actx->handles);
+	free(actx);
+}
+
+static int amdgpu_dmem_allocate_vram(void *ctx, int n_bo, size_t len)
+{
+	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;
+
+	actx->handles[n_bo] = handle;
+
+	return 0;
+}
+
+static void amdgpu_dmem_free_vram(void *ctx, int n_bo, size_t len)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;
+	int i;
+	for (i = 0; i < n_bo; i++) {
+		if (actx->handles[i])
+			amdgpu_bo_free(actx->handles[i]);
+	}
+}
+
+const struct igt_dmem_driver amdgpu_dmem_driver = {
+	.name = "amdgpu",
+	.get_region_name = amdgpu_cgroup_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 869356fbf2c2..d43e5140d4f6 100644
--- a/lib/igt_dmem_driver.h
+++ b/lib/igt_dmem_driver.h
@@ -20,5 +20,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 269f3b9f0af8..dd02d087875b 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -188,6 +188,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')
diff --git a/tests/drv_dmem_cgroups.c b/tests/drv_dmem_cgroups.c
index 6f4f779f3c2c..0e26b7e2bb9a 100644
--- a/tests/drv_dmem_cgroups.c
+++ b/tests/drv_dmem_cgroups.c
@@ -116,8 +116,9 @@ static int fill_vram(const struct igt_dmem_driver *drv, void *ctx, int fd, int m
 			break;
 	}
 
-	igt_assert_f(err == -ENOMEM || err == -ENOSPC,
-		     "Expected -ENOMEM or -ENOSPC, got %d (%s)\n",
+	/* amdgpu will fallback to GTT if it cannot allocate on VRAM */
+	igt_assert_f(err == -ENOMEM || err == -ENOSPC || err == 0,
+		     "Expected -ENOMEM,-ENOSPC or success, got %d (%s)\n",
 		     err, strerror(-err));
 
 	return n_bo;
@@ -236,6 +237,7 @@ static const struct {
 	const struct igt_dmem_driver *driver;
 } drivers[] = {
 	{ DRIVER_XE, &xe_dmem_driver },
+	{ DRIVER_AMDGPU, &amdgpu_dmem_driver },
 	{ },
 };
 
-- 
2.47.3


  parent reply	other threads:[~2026-05-12 21:52 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12 21:51 [PATCH i-g-t 0/8] dmem: add amdgpu support and one more test Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` [PATCH i-g-t 1/8] Introduce dmem driver and implement Xe support Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` [PATCH i-g-t 2/8] Adjust xe_cgroups test to use igt_dmem_driver Thadeu Lima de Souza Cascardo
2026-05-13 15:18   ` Kamil Konieczny
2026-05-12 21:51 ` [PATCH i-g-t 3/8] Make xe_cgroup test a generic test Thadeu Lima de Souza Cascardo
2026-05-13 15:21   ` Kamil Konieczny
2026-05-12 21:51 ` [PATCH i-g-t 4/8] amdgpu: add amdgpu_cgroup_region_name Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` Thadeu Lima de Souza Cascardo [this message]
2026-05-13 15:26   ` [PATCH i-g-t 5/8] igt_dmem_driver: add amdgpu support Kamil Konieczny
2026-05-12 21:51 ` [PATCH i-g-t 6/8] dmem: add test for current/max Thadeu Lima de Souza Cascardo
2026-05-13 15:31   ` Kamil Konieczny
2026-05-12 21:51 ` [PATCH i-g-t 7/8] dmem: only check for dmem availability once Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` [PATCH i-g-t 8/8] dmem: get region once per driver Thadeu Lima de Souza Cascardo
2026-05-12 22:27 ` [PATCH i-g-t 0/8] dmem: add amdgpu support and one more test Thadeu Lima de Souza Cascardo
2026-05-13  8:06 ` Christian König
2026-05-13  8:54   ` Thomas Hellström

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260512215156.4083082-6-cascardo@igalia.com \
    --to=cascardo@igalia.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=kernel-dev@igalia.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=natalie.vock@gmx.de \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tvrtko.ursulin@igalia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox