Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Wajdeczko <michal.wajdeczko@intel.com>
To: intel-xe@lists.freedesktop.org
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Subject: [PATCH v3 5/9] drm/xe/kunit: Define helper functions to allocate mock device
Date: Mon, 11 Dec 2023 21:04:20 +0100	[thread overview]
Message-ID: <20231211200424.1703-6-michal.wajdeczko@intel.com> (raw)
In-Reply-To: <20231211200424.1703-1-michal.wajdeczko@intel.com>

There will be more KUnit tests added that will require mock device.
Define generic helper functions to avoid code duplications.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
---
 drivers/gpu/drm/xe/Makefile                 |  3 +
 drivers/gpu/drm/xe/tests/xe_kunit_helpers.c | 85 +++++++++++++++++++++
 drivers/gpu/drm/xe/tests/xe_kunit_helpers.h | 19 +++++
 3 files changed, 107 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/tests/xe_kunit_helpers.c
 create mode 100644 drivers/gpu/drm/xe/tests/xe_kunit_helpers.h

diff --git a/drivers/gpu/drm/xe/Makefile b/drivers/gpu/drm/xe/Makefile
index 0c2e247dc188..14857923d217 100644
--- a/drivers/gpu/drm/xe/Makefile
+++ b/drivers/gpu/drm/xe/Makefile
@@ -145,6 +145,9 @@ xe-$(CONFIG_PCI_IOV) += \
 	xe_lmtt_2l.o \
 	xe_lmtt_ml.o
 
+xe-$(CONFIG_DRM_XE_KUNIT_TEST) += \
+	tests/xe_kunit_helpers.o
+
 # i915 Display compat #defines and #includes
 subdir-ccflags-$(CONFIG_DRM_XE_DISPLAY) += \
 	-I$(srctree)/$(src)/display/ext \
diff --git a/drivers/gpu/drm/xe/tests/xe_kunit_helpers.c b/drivers/gpu/drm/xe/tests/xe_kunit_helpers.c
new file mode 100644
index 000000000000..236f24d3ca17
--- /dev/null
+++ b/drivers/gpu/drm/xe/tests/xe_kunit_helpers.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+#include <kunit/static_stub.h>
+#include <kunit/visibility.h>
+
+#include <drm/drm_drv.h>
+#include <drm/drm_kunit_helpers.h>
+
+#include "tests/xe_kunit_helpers.h"
+#include "tests/xe_pci_test.h"
+#include "xe_device_types.h"
+
+/**
+ * __xe_kunit_helper_alloc_xe_device - Allocate a mock &xe_device for a KUnit test
+ * @test: the &kunit where this &xe_device will be used
+ * @data: the &xe_pci_fake_data with desired variant of the mock device (optional)
+ *
+ * This function creates a &xe_device based on parameters from @data.
+ * This allocation is managed. See drm_kunit_helper_alloc_device() and
+ * drm_kunit_helper_alloc_drm_device() for details.
+ *
+ * Return: A pointer to mock &xe_device described by &data
+ */
+struct xe_device *__xe_kunit_helper_alloc_xe_device(struct kunit *test,
+						    struct xe_pci_fake_data *data)
+{
+	struct xe_device *xe;
+	struct device *dev;
+	int err;
+
+	dev = drm_kunit_helper_alloc_device(test);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	xe = drm_kunit_helper_alloc_drm_device(test, dev,
+					       struct xe_device,
+					       drm, DRIVER_GEM);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xe);
+
+	test->priv = data;
+	err = xe_pci_fake_device_init(xe);
+	test->priv = NULL;
+	KUNIT_ASSERT_EQ(test, 0, err);
+
+	xe->drm.dev = dev;
+	return xe;
+}
+EXPORT_SYMBOL_IF_KUNIT(__xe_kunit_helper_alloc_xe_device);
+
+/**
+ * xe_kunit_helper_alloc_xe_device - Allocate a mock &xe_device for KUnit test.
+ * @test: the &kunit where this &xe_device will be used
+ *
+ * See __xe_kunit_helper_alloc_xe_device() for details.
+ *
+ * Return: A pointer to default mock &xe_device.
+ */
+struct xe_device *xe_kunit_helper_alloc_xe_device(struct kunit *test)
+{
+	return __xe_kunit_helper_alloc_xe_device(test, NULL);
+}
+EXPORT_SYMBOL_IF_KUNIT(xe_kunit_helper_alloc_xe_device);
+
+/**
+ * xe_kunit_helper_xe_device_test_init - Allocate a mock &xe_device for KUnit test.
+ * @test: the &kunit where this fake &xe_device will be used
+ *
+ * This function allocates a default mock &xe_device and stores its pointer as
+ * &kunit.priv to allow test code to access it.
+ *
+ * This function can be directly used as custom implementation of &kunit_suite.init.
+ *
+ * See xe_kunit_helper_alloc_xe_device() for details.
+ *
+ * Return: Always 0.
+ */
+int xe_kunit_helper_xe_device_test_init(struct kunit *test)
+{
+	test->priv = xe_kunit_helper_alloc_xe_device(test);
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(xe_kunit_helper_xe_device_test_init);
diff --git a/drivers/gpu/drm/xe/tests/xe_kunit_helpers.h b/drivers/gpu/drm/xe/tests/xe_kunit_helpers.h
new file mode 100644
index 000000000000..1bbd0a58c1b2
--- /dev/null
+++ b/drivers/gpu/drm/xe/tests/xe_kunit_helpers.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 AND MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_KUNIT_HELPERS_H_
+#define _XE_KUNIT_HELPERS_H_
+
+struct kunit;
+struct xe_device;
+struct xe_pci_fake_data;
+
+struct xe_device *__xe_kunit_helper_alloc_xe_device(struct kunit *test,
+						    struct xe_pci_fake_data *data);
+struct xe_device *xe_kunit_helper_alloc_xe_device(struct kunit *test);
+
+int xe_kunit_helper_xe_device_test_init(struct kunit *test);
+
+#endif
-- 
2.25.1


  parent reply	other threads:[~2023-12-11 20:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-11 20:04 [PATCH v3 0/9] Introduce GuC Doorbells Manager Michal Wajdeczko
2023-12-11 20:04 ` [PATCH v3 1/9] drm/xe: Add GT oriented drm_printers Michal Wajdeczko
2023-12-11 20:04 ` [PATCH v3 2/9] drm/xe: Use GT oriented functions to report TLB timeout Michal Wajdeczko
2023-12-11 20:04 ` [PATCH v3 3/9] drm/xe: Introduce GuC Doorbells Manager Michal Wajdeczko
2023-12-12  7:51   ` Piotr Piórkowski
2023-12-11 20:04 ` [PATCH v3 4/9] drm/xe/kunit: Set SR-IOV mode of the fake device Michal Wajdeczko
2023-12-12  4:57   ` Lucas De Marchi
2023-12-11 20:04 ` Michal Wajdeczko [this message]
2023-12-12  5:09   ` [PATCH v3 5/9] drm/xe/kunit: Define helper functions to allocate mock device Lucas De Marchi
2023-12-11 20:04 ` [PATCH v3 6/9] drm/xe/kunit: Use xe kunit helpers in RTP test Michal Wajdeczko
2023-12-11 20:04 ` [PATCH v3 7/9] drm/xe/kunit: Use xe kunit helpers in WA test Michal Wajdeczko
2023-12-11 20:04 ` [PATCH v3 8/9] drm/xe/kunit: Enable CONFIG_LOCKDEP in tests Michal Wajdeczko
2023-12-11 20:04 ` [PATCH v3 9/9] drm/xe/kunit: Add GuC Doorbells Manager tests Michal Wajdeczko
2023-12-12  8:04   ` Piotr Piórkowski
2023-12-11 23:09 ` ✓ CI.Patch_applied: success for Introduce GuC Doorbells Manager (rev3) Patchwork
2023-12-11 23:10 ` ✗ CI.checkpatch: warning " Patchwork
2023-12-11 23:11 ` ✓ CI.KUnit: success " Patchwork
2023-12-11 23:18 ` ✓ CI.Build: " Patchwork
2023-12-11 23:19 ` ✓ CI.Hooks: " Patchwork
2023-12-11 23:20 ` ✓ CI.checksparse: " Patchwork
2023-12-11 23:55 ` ✓ CI.BAT: " 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=20231211200424.1703-6-michal.wajdeczko@intel.com \
    --to=michal.wajdeczko@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=lucas.demarchi@intel.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