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
xe_ip_kunit_desc(const struct xe_ip * param,char * desc)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
xe_pci_id_kunit_desc(const struct pci_device_id * param,char * desc)24 static void xe_pci_id_kunit_desc(const struct pci_device_id *param, char *desc)
25 {
26 const struct xe_device_desc *dev_desc =
27 (const struct xe_device_desc *)param->driver_data;
28
29 if (dev_desc)
30 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "0x%X (%s)",
31 param->device, dev_desc->platform_name);
32 }
33
34 KUNIT_ARRAY_PARAM(pci_id, pciidlist, xe_pci_id_kunit_desc);
35
36 /**
37 * xe_pci_graphics_ip_gen_param - Generate graphics struct xe_ip parameters
38 * @prev: the pointer to the previous parameter to iterate from or NULL
39 * @desc: output buffer with minimum size of KUNIT_PARAM_DESC_SIZE
40 *
41 * This function prepares struct xe_ip parameter.
42 *
43 * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
44 *
45 * Return: pointer to the next parameter or NULL if no more parameters
46 */
xe_pci_graphics_ip_gen_param(const void * prev,char * desc)47 const void *xe_pci_graphics_ip_gen_param(const void *prev, char *desc)
48 {
49 return graphics_ip_gen_params(prev, desc);
50 }
51 EXPORT_SYMBOL_IF_KUNIT(xe_pci_graphics_ip_gen_param);
52
53 /**
54 * xe_pci_media_ip_gen_param - Generate media struct xe_ip parameters
55 * @prev: the pointer to the previous parameter to iterate from or NULL
56 * @desc: output buffer with minimum size of KUNIT_PARAM_DESC_SIZE
57 *
58 * This function prepares struct xe_ip parameter.
59 *
60 * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
61 *
62 * Return: pointer to the next parameter or NULL if no more parameters
63 */
xe_pci_media_ip_gen_param(const void * prev,char * desc)64 const void *xe_pci_media_ip_gen_param(const void *prev, char *desc)
65 {
66 return media_ip_gen_params(prev, desc);
67 }
68 EXPORT_SYMBOL_IF_KUNIT(xe_pci_media_ip_gen_param);
69
70 /**
71 * xe_pci_id_gen_param - Generate struct pci_device_id parameters
72 * @prev: the pointer to the previous parameter to iterate from or NULL
73 * @desc: output buffer with minimum size of KUNIT_PARAM_DESC_SIZE
74 *
75 * This function prepares struct pci_device_id parameter.
76 *
77 * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
78 *
79 * Return: pointer to the next parameter or NULL if no more parameters
80 */
xe_pci_id_gen_param(const void * prev,char * desc)81 const void *xe_pci_id_gen_param(const void *prev, char *desc)
82 {
83 const struct pci_device_id *pci = pci_id_gen_params(prev, desc);
84
85 return pci->driver_data ? pci : NULL;
86 }
87 EXPORT_SYMBOL_IF_KUNIT(xe_pci_id_gen_param);
88
fake_read_gmdid(struct xe_device * xe,enum xe_gmdid_type type,u32 * ver,u32 * revid)89 static void fake_read_gmdid(struct xe_device *xe, enum xe_gmdid_type type,
90 u32 *ver, u32 *revid)
91 {
92 struct kunit *test = kunit_get_current_test();
93 struct xe_pci_fake_data *data = test->priv;
94
95 if (type == GMDID_MEDIA) {
96 *ver = data->media_verx100;
97 *revid = xe_step_to_gmdid(data->media_step);
98 } else {
99 *ver = data->graphics_verx100;
100 *revid = xe_step_to_gmdid(data->graphics_step);
101 }
102 }
103
xe_pci_fake_device_init(struct xe_device * xe)104 int xe_pci_fake_device_init(struct xe_device *xe)
105 {
106 struct kunit *test = kunit_get_current_test();
107 struct xe_pci_fake_data *data = test->priv;
108 const struct pci_device_id *ent = pciidlist;
109 const struct xe_device_desc *desc;
110 const struct xe_subplatform_desc *subplatform_desc;
111
112 if (!data) {
113 desc = (const void *)ent->driver_data;
114 subplatform_desc = NULL;
115 goto done;
116 }
117
118 for (ent = pciidlist; ent->device; ent++) {
119 desc = (const void *)ent->driver_data;
120 if (desc->platform == data->platform)
121 break;
122 }
123
124 if (!ent->device)
125 return -ENODEV;
126
127 for (subplatform_desc = desc->subplatforms;
128 subplatform_desc && subplatform_desc->subplatform;
129 subplatform_desc++)
130 if (subplatform_desc->subplatform == data->subplatform)
131 break;
132
133 if (data->subplatform != XE_SUBPLATFORM_NONE && !subplatform_desc)
134 return -ENODEV;
135
136 done:
137 xe->sriov.__mode = data && data->sriov_mode ?
138 data->sriov_mode : XE_SRIOV_MODE_NONE;
139
140 kunit_activate_static_stub(test, read_gmdid, fake_read_gmdid);
141
142 xe_info_init_early(xe, desc, subplatform_desc);
143 xe_info_init(xe, desc);
144
145 return 0;
146 }
147 EXPORT_SYMBOL_IF_KUNIT(xe_pci_fake_device_init);
148
149 /**
150 * xe_pci_live_device_gen_param - Helper to iterate Xe devices as KUnit parameters
151 * @prev: the previously returned value, or NULL for the first iteration
152 * @desc: the buffer for a parameter name
153 *
154 * Iterates over the available Xe devices on the system. Uses the device name
155 * as the parameter name.
156 *
157 * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
158 *
159 * Return: pointer to the next &struct xe_device ready to be used as a parameter
160 * or NULL if there are no more Xe devices on the system.
161 */
xe_pci_live_device_gen_param(const void * prev,char * desc)162 const void *xe_pci_live_device_gen_param(const void *prev, char *desc)
163 {
164 const struct xe_device *xe = prev;
165 struct device *dev = xe ? xe->drm.dev : NULL;
166 struct device *next;
167
168 next = driver_find_next_device(&xe_pci_driver.driver, dev);
169 if (dev)
170 put_device(dev);
171 if (!next)
172 return NULL;
173
174 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s", dev_name(next));
175 return pdev_to_xe_device(to_pci_dev(next));
176 }
177 EXPORT_SYMBOL_IF_KUNIT(xe_pci_live_device_gen_param);
178