Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Piórkowski, Piotr" <piotr.piorkowski@intel.com>
To: <intel-xe@lists.freedesktop.org>
Cc: "Piotr Piórkowski" <piotr.piorkowski@intel.com>
Subject: [PATCH v3 4/4] drm/xe/kunit: Add tests for PF-mem regions
Date: Mon, 24 Aug 2026 10:09:08 +0200	[thread overview]
Message-ID: <20260824080908.701133-5-piotr.piorkowski@intel.com> (raw)
In-Reply-To: <20260824080908.701133-1-piotr.piorkowski@intel.com>

From: Piotr Piórkowski <piotr.piorkowski@intel.com>

Add coverage for PF-mem VRAM region metadata, bounds checking, and
full, partial, and missing CPU visibility.

v2:
 - Use a mock BAR mapping that fits into a pointer on 32-bit builds, where
   SZ_4G truncates to NULL and makes region init fail with -ENOMEM
   (Sashiko).
 - Use a page aligned out-of-bounds offset so the bounds check is actually
   exercised, and cover the unaligned offset/size cases separately
   (Sashiko).

Assisted-by: Claude:claude-5-sonnet
Signed-off-by: Piotr Piórkowski <piotr.piorkowski@intel.com>
---
 drivers/gpu/drm/xe/tests/xe_vram.c | 118 +++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_vram.c       |   4 +
 2 files changed, 122 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/tests/xe_vram.c

diff --git a/drivers/gpu/drm/xe/tests/xe_vram.c b/drivers/gpu/drm/xe/tests/xe_vram.c
new file mode 100644
index 000000000000..98787a1221cc
--- /dev/null
+++ b/drivers/gpu/drm/xe/tests/xe_vram.c
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2026 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "xe_kunit_helpers.h"
+#include "xe_pci_test.h"
+
+static int xe_vram_test_init(struct kunit *test)
+{
+	struct xe_pci_fake_data fake = {
+		.platform = XE_BATTLEMAGE,
+		.graphics_verx100 = 2001,
+	};
+
+	test->priv = &fake;
+	xe_kunit_helper_xe_device_test_init(test);
+
+	return 0;
+}
+
+static struct xe_vram_region pfmem_parent(struct xe_device *xe, u64 io_size)
+{
+	struct xe_vram_region parent = {
+		.xe = xe,
+		.id = 0,
+		.io_start = SZ_1G,
+		.io_size = io_size,
+		.dpa_base = SZ_2G,
+		.mapping = (__force void __iomem *)SZ_64K,
+		.usable_size = SZ_64M,
+	};
+
+	return parent;
+}
+
+static void pfmem_create(struct kunit *test)
+{
+	struct xe_device *xe = test->priv;
+	struct xe_vram_region parent = pfmem_parent(xe, SZ_64M);
+	struct xe_vram_region *vram;
+
+	vram = xe_vram_region_alloc_pfmem(xe, 0);
+	KUNIT_ASSERT_NOT_NULL(test, vram);
+	KUNIT_ASSERT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_4M, SZ_16M), 0);
+	KUNIT_EXPECT_EQ(test, vram->binding, XE_VRAM_BINDING_PFMEM);
+	KUNIT_EXPECT_EQ(test, vram->placement, (u32)XE_PL_VRAM0_PFMEM);
+	KUNIT_EXPECT_EQ(test, vram->id, (u8)0);
+	KUNIT_EXPECT_EQ(test, vram->actual_physical_size, (resource_size_t)SZ_16M);
+	KUNIT_EXPECT_EQ(test, vram->usable_size, (resource_size_t)SZ_16M);
+	KUNIT_EXPECT_EQ(test, vram->io_start, (resource_size_t)(SZ_1G + SZ_4M));
+	KUNIT_EXPECT_EQ(test, vram->io_size, (resource_size_t)SZ_16M);
+	KUNIT_EXPECT_EQ(test, vram->dpa_base, (resource_size_t)(SZ_2G + SZ_4M));
+	KUNIT_EXPECT_PTR_EQ(test, vram->mapping, parent.mapping + SZ_4M);
+}
+
+static void pfmem_small_bar(struct kunit *test)
+{
+	struct xe_device *xe = test->priv;
+	struct xe_vram_region parent = pfmem_parent(xe, SZ_8M);
+	struct xe_vram_region *vram;
+
+	vram = xe_vram_region_alloc_pfmem(xe, 0);
+	KUNIT_ASSERT_NOT_NULL(test, vram);
+	KUNIT_ASSERT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_4M, SZ_16M), 0);
+	KUNIT_EXPECT_EQ(test, vram->io_size, (resource_size_t)SZ_4M);
+
+	vram = xe_vram_region_alloc_pfmem(xe, 0);
+	KUNIT_ASSERT_NOT_NULL(test, vram);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_8M, SZ_16M), -EINVAL);
+}
+
+static void pfmem_invalid_range(struct kunit *test)
+{
+	struct xe_device *xe = test->priv;
+	struct xe_vram_region parent = pfmem_parent(xe, SZ_64M);
+	struct xe_vram_region *vram;
+
+	vram = xe_vram_region_alloc_pfmem(xe, 0);
+	KUNIT_ASSERT_NOT_NULL(test, vram);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, 0, 0), -EINVAL);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_4M + 1, SZ_4M), -EINVAL);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_4M, SZ_4M + 1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_64M + SZ_4M, SZ_4M),
+			-EINVAL);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_64M - SZ_4M, SZ_8M),
+			-EINVAL);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, SZ_64M, SZ_4M), -EINVAL);
+}
+
+static void pfmem_no_io(struct kunit *test)
+{
+	struct xe_device *xe = test->priv;
+	struct xe_vram_region parent = pfmem_parent(xe, 0);
+	struct xe_vram_region *vram;
+
+	vram = xe_vram_region_alloc_pfmem(xe, 0);
+	KUNIT_ASSERT_NOT_NULL(test, vram);
+	KUNIT_EXPECT_EQ(test, xe_vram_region_init_pfmem(vram, &parent, 0, SZ_4M), -EINVAL);
+}
+
+static struct kunit_case xe_vram_tests[] = {
+	KUNIT_CASE(pfmem_create),
+	KUNIT_CASE(pfmem_small_bar),
+	KUNIT_CASE(pfmem_invalid_range),
+	KUNIT_CASE(pfmem_no_io),
+	{}
+};
+
+static struct kunit_suite xe_vram_test_suite = {
+	.name = "xe_vram",
+	.test_cases = xe_vram_tests,
+	.init = xe_vram_test_init,
+};
+
+kunit_test_suite(xe_vram_test_suite);
diff --git a/drivers/gpu/drm/xe/xe_vram.c b/drivers/gpu/drm/xe/xe_vram.c
index 61fb4a543cb0..2fa809831662 100644
--- a/drivers/gpu/drm/xe/xe_vram.c
+++ b/drivers/gpu/drm/xe/xe_vram.c
@@ -483,3 +483,7 @@ resource_size_t xe_vram_region_actual_physical_size(const struct xe_vram_region
 	return vram ? vram->actual_physical_size : 0;
 }
 EXPORT_SYMBOL_IF_KUNIT(xe_vram_region_actual_physical_size);
+
+#if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
+#include "tests/xe_vram.c"
+#endif
-- 
2.34.1


  parent reply	other threads:[~2026-08-24  8:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-24  8:09 [PATCH v3 0/4] Introduce PF-mem VRAM regions Piórkowski, Piotr
2026-08-24  8:09 ` [PATCH v3 1/4] drm/xe/vram: Add binding information to " Piórkowski, Piotr
2026-08-24  8:09 ` [PATCH v3 2/4] drm/xe/ttm: Add PF-mem VRAM placement types for TTM Piórkowski, Piotr
2026-08-24  8:09 ` [PATCH v3 3/4] drm/xe/vram: Add initial support for PF-mem regions Piórkowski, Piotr
2026-08-24  8:09 ` Piórkowski, Piotr [this message]
2026-08-25  5:39 ` ✗ CI.checkpatch: warning for Introduce PF-mem VRAM regions (rev3) Patchwork
2026-08-25  5:40 ` ✓ CI.KUnit: success " Patchwork
2026-08-25  6:19 ` ✓ Xe.CI.BAT: " Patchwork
2026-08-25 10:19 ` ✗ Xe.CI.FULL: failure " Patchwork

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=20260824080908.701133-5-piotr.piorkowski@intel.com \
    --to=piotr.piorkowski@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    /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