xref: /linux/drivers/gpu/drm/xe/tests/xe_pci.c (revision 917b10d90990fd2138b5dbc2d22cfa428c070ade)
1 // SPDX-License-Identifier: GPL-2.0 AND MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include "tests/xe_pci_test.h"
7 
8 #include "tests/xe_test.h"
9 
10 #include <kunit/test-bug.h>
11 #include <kunit/test.h>
12 #include <kunit/test-bug.h>
13 #include <kunit/visibility.h>
14 
15 static void xe_ip_kunit_desc(const struct xe_ip *param, char *desc)
16 {
17 	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%u.%02u %s",
18 		 param->verx100 / 100, param->verx100 % 100, param->name);
19 }
20 
21 KUNIT_ARRAY_PARAM(graphics_ip, graphics_ips, xe_ip_kunit_desc);
22 KUNIT_ARRAY_PARAM(media_ip, media_ips, xe_ip_kunit_desc);
23 
24 /**
25  * xe_pci_graphics_ip_gen_param - Generate graphics struct xe_ip parameters
26  * @prev: the pointer to the previous parameter to iterate from or NULL
27  * @desc: output buffer with minimum size of KUNIT_PARAM_DESC_SIZE
28  *
29  * This function prepares struct xe_ip parameter.
30  *
31  * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
32  *
33  * Return: pointer to the next parameter or NULL if no more parameters
34  */
35 const void *xe_pci_graphics_ip_gen_param(const void *prev, char *desc)
36 {
37 	return graphics_ip_gen_params(prev, desc);
38 }
39 EXPORT_SYMBOL_IF_KUNIT(xe_pci_graphics_ip_gen_param);
40 
41 /**
42  * xe_pci_media_ip_gen_param - Generate media struct xe_ip parameters
43  * @prev: the pointer to the previous parameter to iterate from or NULL
44  * @desc: output buffer with minimum size of KUNIT_PARAM_DESC_SIZE
45  *
46  * This function prepares struct xe_ip parameter.
47  *
48  * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
49  *
50  * Return: pointer to the next parameter or NULL if no more parameters
51  */
52 const void *xe_pci_media_ip_gen_param(const void *prev, char *desc)
53 {
54 	return media_ip_gen_params(prev, desc);
55 }
56 EXPORT_SYMBOL_IF_KUNIT(xe_pci_media_ip_gen_param);
57 
58 static void fake_read_gmdid(struct xe_device *xe, enum xe_gmdid_type type,
59 			    u32 *ver, u32 *revid)
60 {
61 	struct kunit *test = kunit_get_current_test();
62 	struct xe_pci_fake_data *data = test->priv;
63 
64 	if (type == GMDID_MEDIA) {
65 		*ver = data->media_verx100;
66 		*revid = xe_step_to_gmdid(data->media_step);
67 	} else {
68 		*ver = data->graphics_verx100;
69 		*revid = xe_step_to_gmdid(data->graphics_step);
70 	}
71 }
72 
73 int xe_pci_fake_device_init(struct xe_device *xe)
74 {
75 	struct kunit *test = kunit_get_current_test();
76 	struct xe_pci_fake_data *data = test->priv;
77 	const struct pci_device_id *ent = pciidlist;
78 	const struct xe_device_desc *desc;
79 	const struct xe_subplatform_desc *subplatform_desc;
80 
81 	if (!data) {
82 		desc = (const void *)ent->driver_data;
83 		subplatform_desc = NULL;
84 		goto done;
85 	}
86 
87 	for (ent = pciidlist; ent->device; ent++) {
88 		desc = (const void *)ent->driver_data;
89 		if (desc->platform == data->platform)
90 			break;
91 	}
92 
93 	if (!ent->device)
94 		return -ENODEV;
95 
96 	for (subplatform_desc = desc->subplatforms;
97 	     subplatform_desc && subplatform_desc->subplatform;
98 	     subplatform_desc++)
99 		if (subplatform_desc->subplatform == data->subplatform)
100 			break;
101 
102 	if (data->subplatform != XE_SUBPLATFORM_NONE && !subplatform_desc)
103 		return -ENODEV;
104 
105 done:
106 	xe->sriov.__mode = data && data->sriov_mode ?
107 			   data->sriov_mode : XE_SRIOV_MODE_NONE;
108 
109 	kunit_activate_static_stub(test, read_gmdid, fake_read_gmdid);
110 
111 	xe_info_init_early(xe, desc, subplatform_desc);
112 	xe_info_init(xe, desc);
113 
114 	return 0;
115 }
116 EXPORT_SYMBOL_IF_KUNIT(xe_pci_fake_device_init);
117 
118 /**
119  * xe_pci_live_device_gen_param - Helper to iterate Xe devices as KUnit parameters
120  * @prev: the previously returned value, or NULL for the first iteration
121  * @desc: the buffer for a parameter name
122  *
123  * Iterates over the available Xe devices on the system. Uses the device name
124  * as the parameter name.
125  *
126  * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
127  *
128  * Return: pointer to the next &struct xe_device ready to be used as a parameter
129  *         or NULL if there are no more Xe devices on the system.
130  */
131 const void *xe_pci_live_device_gen_param(const void *prev, char *desc)
132 {
133 	const struct xe_device *xe = prev;
134 	struct device *dev = xe ? xe->drm.dev : NULL;
135 	struct device *next;
136 
137 	next = driver_find_next_device(&xe_pci_driver.driver, dev);
138 	if (dev)
139 		put_device(dev);
140 	if (!next)
141 		return NULL;
142 
143 	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s", dev_name(next));
144 	return pdev_to_xe_device(to_pci_dev(next));
145 }
146 EXPORT_SYMBOL_IF_KUNIT(xe_pci_live_device_gen_param);
147