Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

drm/imagination: KUnit test for pvr_gpuid_decode_string()

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

Reviewed-by: Alessio Belle <alessio.belle@imgtec.com>
Link: https://patch.msgid.link/20260113-device-support-info-v1-5-91e5db7f7294@imgtec.com
Signed-off-by: Matt Coster <matt.coster@imgtec.com>

+97 -2
+12
drivers/gpu/drm/imagination/Kconfig
··· 18 18 Technologies PowerVR (Series 6 or later) or IMG GPU. 19 19 20 20 If "M" is selected, the module will be called powervr. 21 + 22 + config DRM_POWERVR_KUNIT_TEST 23 + tristate "KUnit tests for the drm powervr driver" if !KUNIT_ALL_TESTS 24 + depends on DRM_POWERVR && KUNIT 25 + default KUNIT_ALL_TESTS 26 + help 27 + Choose this option to allow the driver to perform selftests under 28 + the kunit framework 29 + 30 + Recommended for driver developers only. 31 + 32 + If in doubt, say "N".
+2
drivers/gpu/drm/imagination/Makefile
··· 32 32 pvr_debugfs.o 33 33 34 34 obj-$(CONFIG_DRM_POWERVR) += powervr.o 35 + 36 + obj-$(CONFIG_DRM_POWERVR_KUNIT_TEST) += pvr_test.o
+4 -1
drivers/gpu/drm/imagination/pvr_device.c
··· 31 31 #include <linux/types.h> 32 32 #include <linux/workqueue.h> 33 33 34 + #include <kunit/visibility.h> 35 + 34 36 /* Major number for the supported version of the firmware. */ 35 37 #define PVR_FW_VERSION_MAJOR 1 36 38 ··· 465 463 * @param_bvnc: GPU ID (BVNC) module parameter. 466 464 * @gpu_id: Output to be updated with the GPU ID. 467 465 */ 468 - static int 466 + VISIBLE_IF_KUNIT int 469 467 pvr_gpuid_decode_string(const struct pvr_device *pvr_dev, 470 468 const char *param_bvnc, struct pvr_gpu_id *gpu_id) 471 469 { ··· 523 521 524 522 return 0; 525 523 } 524 + EXPORT_SYMBOL_IF_KUNIT(pvr_gpuid_decode_string); 526 525 527 526 static char *pvr_gpuid_override; 528 527 module_param_named(gpuid, pvr_gpuid_override, charp, 0400);
+6 -1
drivers/gpu/drm/imagination/pvr_device.h
··· 519 519 * Return: Packed BVNC. 520 520 */ 521 521 static __always_inline u64 522 - pvr_gpu_id_to_packed_bvnc(struct pvr_gpu_id *gpu_id) 522 + pvr_gpu_id_to_packed_bvnc(const struct pvr_gpu_id *gpu_id) 523 523 { 524 524 return PVR_PACKED_BVNC(gpu_id->b, gpu_id->v, gpu_id->n, gpu_id->c); 525 525 } ··· 543 543 pvr_device_has_uapi_enhancement(struct pvr_device *pvr_dev, u32 enhancement); 544 544 bool 545 545 pvr_device_has_feature(struct pvr_device *pvr_dev, u32 feature); 546 + 547 + #if IS_ENABLED(CONFIG_KUNIT) 548 + int pvr_gpuid_decode_string(const struct pvr_device *pvr_dev, 549 + const char *param_bvnc, struct pvr_gpu_id *gpu_id); 550 + #endif 546 551 547 552 /** 548 553 * PVR_CR_FIELD_GET() - Extract a single field from a PowerVR control register
+73
drivers/gpu/drm/imagination/pvr_test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 + /* Copyright (c) 2025 Imagination Technologies Ltd. */ 3 + 4 + #include "pvr_device.h" 5 + 6 + #include <linux/errno.h> 7 + #include <linux/stddef.h> 8 + #include <linux/string.h> 9 + #include <linux/types.h> 10 + 11 + #include <kunit/test.h> 12 + #include <kunit/visibility.h> 13 + 14 + static void decode_gpuid_string(struct kunit *test) 15 + { 16 + const struct pvr_gpu_id bad_gpuid = { 0xdead, 0xbeef, 0xcafe, 0xface }; 17 + const u64 packed_bad_gpuid = pvr_gpu_id_to_packed_bvnc(&bad_gpuid); 18 + 19 + #define GPUID_TEST_CASE(str_, err_, value_) \ 20 + do { \ 21 + struct pvr_gpu_id _gpuid_out = bad_gpuid; \ 22 + int _err; \ 23 + _err = pvr_gpuid_decode_string(NULL, str_, &_gpuid_out); \ 24 + KUNIT_EXPECT_EQ(test, _err, err_); \ 25 + KUNIT_EXPECT_EQ(test, \ 26 + pvr_gpu_id_to_packed_bvnc(&_gpuid_out), \ 27 + value_); \ 28 + } while (0) 29 + 30 + #define GPUID_TEST_CASE_OK(str_, b_, v_, n_, c_) \ 31 + GPUID_TEST_CASE(str_, 0, PVR_PACKED_BVNC(b_, v_, n_, c_)) 32 + 33 + #define GPUID_TEST_CASE_INVAL(str_) \ 34 + GPUID_TEST_CASE(str_, -EINVAL, packed_bad_gpuid) 35 + 36 + GPUID_TEST_CASE_OK("12.34.56.78", 12, 34, 56, 78); 37 + GPUID_TEST_CASE_OK("0.0.0.0", 0, 0, 0, 0); 38 + 39 + GPUID_TEST_CASE_INVAL(""); 40 + GPUID_TEST_CASE_INVAL("42.foobar-invalid.gpuid.bvnc"); 41 + 42 + /* String longer than PVR_GPUID_STRING_MAX_LENGTH. */ 43 + GPUID_TEST_CASE_INVAL("12.34.56.789012345678901234567890123456"); 44 + 45 + /* Single value overflowing u16. */ 46 + GPUID_TEST_CASE_INVAL("12.34.56.999999"); 47 + 48 + /* Wrong number of parts and/or dots. */ 49 + GPUID_TEST_CASE_INVAL("12.34.56.78.90"); 50 + GPUID_TEST_CASE_INVAL("12.34.56..78"); 51 + GPUID_TEST_CASE_INVAL("12.34..56"); 52 + GPUID_TEST_CASE_INVAL("12.34.56"); 53 + 54 + #undef GPUID_TEST_CASE_INVAL 55 + #undef GPUID_TEST_CASE_OK 56 + #undef GPUID_TEST_CASE 57 + } 58 + 59 + static struct kunit_case pvr_tests_cases[] = { 60 + KUNIT_CASE(decode_gpuid_string), 61 + {}, 62 + }; 63 + 64 + static struct kunit_suite pvr_tests_suite = { 65 + .name = "pvr_tests", 66 + .test_cases = pvr_tests_cases, 67 + }; 68 + kunit_test_suite(pvr_tests_suite); 69 + 70 + MODULE_AUTHOR("Imagination Technologies Ltd."); 71 + MODULE_LICENSE("Dual MIT/GPL"); 72 + MODULE_DESCRIPTION("pvr kunit tests"); 73 + MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");