This is a nice self-contained function to serve as the basis of our first
KUnit tests.

Signed-off-by: Matt Coster <[email protected]>
---
 drivers/gpu/drm/imagination/Kconfig      | 12 ++++++
 drivers/gpu/drm/imagination/Makefile     |  2 +
 drivers/gpu/drm/imagination/pvr_device.c |  5 ++-
 drivers/gpu/drm/imagination/pvr_device.h |  7 ++-
 drivers/gpu/drm/imagination/pvr_test.c   | 73 ++++++++++++++++++++++++++++++++
 5 files changed, 97 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/imagination/Kconfig 
b/drivers/gpu/drm/imagination/Kconfig
index 0482bfcefdde3..1fd4c635c2c96 100644
--- a/drivers/gpu/drm/imagination/Kconfig
+++ b/drivers/gpu/drm/imagination/Kconfig
@@ -18,3 +18,15 @@ config DRM_POWERVR
          Technologies PowerVR (Series 6 or later) or IMG GPU.
 
          If "M" is selected, the module will be called powervr.
+
+config DRM_POWERVR_KUNIT_TEST
+       tristate "KUnit tests for the drm powervr driver" if !KUNIT_ALL_TESTS
+       depends on DRM_POWERVR && KUNIT
+       default KUNIT_ALL_TESTS
+       help
+         Choose this option to allow the driver to perform selftests under
+         the kunit framework
+
+         Recommended for driver developers only.
+
+         If in doubt, say "N".
diff --git a/drivers/gpu/drm/imagination/Makefile 
b/drivers/gpu/drm/imagination/Makefile
index ab63eac9ba7f7..f5072f06b4c41 100644
--- a/drivers/gpu/drm/imagination/Makefile
+++ b/drivers/gpu/drm/imagination/Makefile
@@ -32,3 +32,5 @@ powervr-$(CONFIG_DEBUG_FS) += \
        pvr_debugfs.o
 
 obj-$(CONFIG_DRM_POWERVR) += powervr.o
+
+obj-$(CONFIG_DRM_POWERVR_KUNIT_TEST) += pvr_test.o
diff --git a/drivers/gpu/drm/imagination/pvr_device.c 
b/drivers/gpu/drm/imagination/pvr_device.c
index db844e4e2e945..d87557812409a 100644
--- a/drivers/gpu/drm/imagination/pvr_device.c
+++ b/drivers/gpu/drm/imagination/pvr_device.c
@@ -31,6 +31,8 @@
 #include <linux/types.h>
 #include <linux/workqueue.h>
 
+#include <kunit/visibility.h>
+
 /* Major number for the supported version of the firmware. */
 #define PVR_FW_VERSION_MAJOR 1
 
@@ -463,7 +465,7 @@ pvr_gpuid_decode_reg(const struct pvr_device *pvr_dev, 
struct pvr_gpu_id *gpu_id
  * @param_bvnc: GPU ID (BVNC) module parameter.
  * @gpu_id: Output to be updated with the GPU ID.
  */
-static int
+VISIBLE_IF_KUNIT int
 pvr_gpuid_decode_string(const struct pvr_device *pvr_dev,
                        const char *param_bvnc, struct pvr_gpu_id *gpu_id)
 {
@@ -521,6 +523,7 @@ pvr_gpuid_decode_string(const struct pvr_device *pvr_dev,
 
        return 0;
 }
+EXPORT_SYMBOL_IF_KUNIT(pvr_gpuid_decode_string);
 
 static char *pvr_gpuid_override;
 module_param_named(gpuid, pvr_gpuid_override, charp, 0400);
diff --git a/drivers/gpu/drm/imagination/pvr_device.h 
b/drivers/gpu/drm/imagination/pvr_device.h
index 5608a977f6d21..cfda215e7428e 100644
--- a/drivers/gpu/drm/imagination/pvr_device.h
+++ b/drivers/gpu/drm/imagination/pvr_device.h
@@ -519,7 +519,7 @@ struct pvr_file {
  * Return: Packed BVNC.
  */
 static __always_inline u64
-pvr_gpu_id_to_packed_bvnc(struct pvr_gpu_id *gpu_id)
+pvr_gpu_id_to_packed_bvnc(const struct pvr_gpu_id *gpu_id)
 {
        return PVR_PACKED_BVNC(gpu_id->b, gpu_id->v, gpu_id->n, gpu_id->c);
 }
@@ -544,6 +544,11 @@ pvr_device_has_uapi_enhancement(struct pvr_device 
*pvr_dev, u32 enhancement);
 bool
 pvr_device_has_feature(struct pvr_device *pvr_dev, u32 feature);
 
+#if IS_ENABLED(CONFIG_KUNIT)
+int pvr_gpuid_decode_string(const struct pvr_device *pvr_dev,
+                           const char *param_bvnc, struct pvr_gpu_id *gpu_id);
+#endif
+
 /**
  * PVR_CR_FIELD_GET() - Extract a single field from a PowerVR control register
  * @val: Value of the target register.
diff --git a/drivers/gpu/drm/imagination/pvr_test.c 
b/drivers/gpu/drm/imagination/pvr_test.c
new file mode 100644
index 0000000000000..506cfa5a02f1e
--- /dev/null
+++ b/drivers/gpu/drm/imagination/pvr_test.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+/* Copyright (c) 2025 Imagination Technologies Ltd. */
+
+#include "pvr_device.h"
+
+#include <linux/errno.h>
+#include <linux/stddef.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+#include <kunit/test.h>
+#include <kunit/visibility.h>
+
+static void decode_gpuid_string(struct kunit *test)
+{
+       const struct pvr_gpu_id bad_gpuid = { 0xdead, 0xbeef, 0xcafe, 0xface };
+       const u64 packed_bad_gpuid = pvr_gpu_id_to_packed_bvnc(&bad_gpuid);
+
+#define GPUID_TEST_CASE(str_, err_, value_)                                    
\
+       do {                                                                    
\
+               struct pvr_gpu_id _gpuid_out = bad_gpuid;                       
\
+               int _err;                                                       
\
+               _err = pvr_gpuid_decode_string(NULL, str_, &_gpuid_out);        
\
+               KUNIT_EXPECT_EQ(test, _err, err_);                              
\
+               KUNIT_EXPECT_EQ(test,                                           
\
+                               pvr_gpu_id_to_packed_bvnc(&_gpuid_out),         
\
+                               value_);                                        
\
+       } while (0)
+
+#define GPUID_TEST_CASE_OK(str_, b_, v_, n_, c_) \
+       GPUID_TEST_CASE(str_, 0, PVR_PACKED_BVNC(b_, v_, n_, c_))
+
+#define GPUID_TEST_CASE_INVAL(str_) \
+       GPUID_TEST_CASE(str_, -EINVAL, packed_bad_gpuid)
+
+       GPUID_TEST_CASE_OK("12.34.56.78", 12, 34, 56, 78);
+       GPUID_TEST_CASE_OK("0.0.0.0", 0, 0, 0, 0);
+
+       GPUID_TEST_CASE_INVAL("");
+       GPUID_TEST_CASE_INVAL("42.foobar-invalid.gpuid.bvnc");
+
+       /* String longer than PVR_GPUID_STRING_MAX_LENGTH. */
+       GPUID_TEST_CASE_INVAL("12.34.56.789012345678901234567890123456");
+
+       /* Single value overflowing u16. */
+       GPUID_TEST_CASE_INVAL("12.34.56.999999");
+
+       /* Wrong number of parts and/or dots. */
+       GPUID_TEST_CASE_INVAL("12.34.56.78.90");
+       GPUID_TEST_CASE_INVAL("12.34.56..78");
+       GPUID_TEST_CASE_INVAL("12.34..56");
+       GPUID_TEST_CASE_INVAL("12.34.56");
+
+#undef GPUID_TEST_CASE_INVAL
+#undef GPUID_TEST_CASE_OK
+#undef GPUID_TEST_CASE
+}
+
+static struct kunit_case pvr_tests_cases[] = {
+       KUNIT_CASE(decode_gpuid_string),
+       {},
+};
+
+static struct kunit_suite pvr_tests_suite = {
+       .name = "pvr_tests",
+       .test_cases = pvr_tests_cases,
+};
+kunit_test_suite(pvr_tests_suite);
+
+MODULE_AUTHOR("Imagination Technologies Ltd.");
+MODULE_LICENSE("Dual MIT/GPL");
+MODULE_DESCRIPTION("pvr kunit tests");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");

-- 
2.52.0

Reply via email to