xref: /linux/drivers/gpu/drm/xe/tests/xe_sriov_packet_kunit.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0 AND MIT
2 /*
3  * Copyright © 2026 Intel Corporation
4  */
5 
6 #include <kunit/test.h>
7 #include <kunit/test-bug.h>
8 
9 #include "xe_device.h"
10 #include "xe_guc_klv_helpers.h"
11 #include "xe_kunit_helpers.h"
12 #include "xe_pci_test.h"
13 
14 #define TEST_VF		VFID(1)
15 
16 static int sriov_packet_test_init(struct kunit *test)
17 {
18 	struct xe_pci_fake_data fake = {
19 		.sriov_mode = XE_SRIOV_MODE_PF,
20 		.platform = XE_PANTHERLAKE, /* we need MEMIRQ */
21 		.subplatform = XE_SUBPLATFORM_NONE,
22 		.graphics_verx100 = 3000,
23 		.media_verx100 = 3000,
24 	};
25 	struct xe_device *xe;
26 
27 	test->priv = &fake;
28 	xe_kunit_helper_xe_device_test_init(test);
29 	xe = test->priv;
30 
31 	/* pretend we can support at least VF1 */
32 	xe->sriov.pf.device_total_vfs = 1;
33 	xe->sriov.pf.driver_max_vfs = 1;
34 
35 	KUNIT_ASSERT_EQ(test, 0, xe_sriov_init(xe));
36 	KUNIT_ASSERT_TRUE(test, xe_sriov_pf_migration_supported(xe));
37 
38 	return 0;
39 }
40 
41 static void test_descriptor_init(struct kunit *test)
42 {
43 	struct xe_device *xe = test->priv;
44 	struct xe_sriov_packet **desc;
45 
46 	/* note: with lock held we should avoid KUNIT_ASSERT() */
47 	guard(mutex)(pf_migration_mutex(xe, TEST_VF));
48 
49 	KUNIT_EXPECT_EQ(test, 0, pf_descriptor_init(xe, TEST_VF));
50 	desc = pf_pick_descriptor(xe, TEST_VF);
51 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, *desc);
52 	if (!*desc)
53 		return;
54 	KUNIT_EXPECT_NE(test, (*desc)->hdr.version, 0);
55 	KUNIT_EXPECT_EQ(test, (*desc)->hdr.version, XE_SRIOV_PACKET_SUPPORTED_VERSION);
56 	KUNIT_EXPECT_EQ(test, (*desc)->hdr.type, XE_SRIOV_PACKET_TYPE_DESCRIPTOR);
57 	KUNIT_EXPECT_NE(test, (*desc)->hdr.size, 0);
58 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, (*desc)->vaddr);
59 	if (!(*desc)->vaddr)
60 		return;
61 	KUNIT_EXPECT_EQ(test, 0, xe_sriov_packet_process_descriptor(xe, TEST_VF, *desc));
62 
63 	switch ((*desc)->hdr.version) {
64 	case 1:
65 		/* v1 is KLV based */
66 		KUNIT_EXPECT_TRUE(test, IS_ALIGNED((*desc)->hdr.size, sizeof(u32)));
67 		/* v1 has at least DEVID and REVID KLVs */
68 		KUNIT_EXPECT_LE(test, 2,
69 				xe_guc_klv_count((*desc)->vaddr,
70 						 (*desc)->hdr.size / sizeof(u32)));
71 		break;
72 	default:
73 		kunit_mark_skipped(test, "no test code for version %u\n", (*desc)->hdr.version);
74 		return;
75 	}
76 }
77 
78 static struct kunit_case sriov_packet_test_cases[] = {
79 	KUNIT_CASE(test_descriptor_init),
80 	{}
81 };
82 
83 static struct kunit_suite sriov_packet_suite = {
84 	.name = "sriov_packet",
85 	.test_cases = sriov_packet_test_cases,
86 	.init = sriov_packet_test_init,
87 };
88 
89 kunit_test_suite(sriov_packet_suite);
90