xref: /linux/drivers/gpu/drm/xe/tests/xe_pci.c (revision 8bdb468dd7a5d17f8556afdd4c8d046939ff965f)
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 /**
16  * xe_call_for_each_graphics_ip - Iterate over all recognized graphics IPs
17  * @xe_fn: Function to call for each device.
18  *
19  * This function iterates over the descriptors for all graphics IPs recognized
20  * by the driver and calls @xe_fn: for each one of them.
21  */
22 void xe_call_for_each_graphics_ip(xe_graphics_fn xe_fn)
23 {
24 	const struct xe_graphics_desc *ip, *last = NULL;
25 
26 	for (int i = 0; i < ARRAY_SIZE(graphics_ip_map); i++) {
27 		ip = graphics_ip_map[i].ip;
28 		if (ip == last)
29 			continue;
30 
31 		xe_fn(ip);
32 		last = ip;
33 	}
34 }
35 EXPORT_SYMBOL_IF_KUNIT(xe_call_for_each_graphics_ip);
36 
37 /**
38  * xe_call_for_each_media_ip - Iterate over all recognized media IPs
39  * @xe_fn: Function to call for each device.
40  *
41  * This function iterates over the descriptors for all media IPs recognized
42  * by the driver and calls @xe_fn: for each one of them.
43  */
44 void xe_call_for_each_media_ip(xe_media_fn xe_fn)
45 {
46 	const struct xe_media_desc *ip, *last = NULL;
47 
48 	for (int i = 0; i < ARRAY_SIZE(media_ip_map); i++) {
49 		ip = media_ip_map[i].ip;
50 		if (ip == last)
51 			continue;
52 
53 		xe_fn(ip);
54 		last = ip;
55 	}
56 }
57 EXPORT_SYMBOL_IF_KUNIT(xe_call_for_each_media_ip);
58 
59 static void fake_read_gmdid(struct xe_device *xe, enum xe_gmdid_type type,
60 			    u32 *ver, u32 *revid)
61 {
62 	struct kunit *test = kunit_get_current_test();
63 	struct xe_pci_fake_data *data = test->priv;
64 
65 	if (type == GMDID_MEDIA) {
66 		*ver = data->media_verx100;
67 		*revid = xe_step_to_gmdid(data->media_step);
68 	} else {
69 		*ver = data->graphics_verx100;
70 		*revid = xe_step_to_gmdid(data->graphics_step);
71 	}
72 }
73 
74 int xe_pci_fake_device_init(struct xe_device *xe)
75 {
76 	struct kunit *test = kunit_get_current_test();
77 	struct xe_pci_fake_data *data = test->priv;
78 	const struct pci_device_id *ent = pciidlist;
79 	const struct xe_device_desc *desc;
80 	const struct xe_subplatform_desc *subplatform_desc;
81 
82 	if (!data) {
83 		desc = (const void *)ent->driver_data;
84 		subplatform_desc = NULL;
85 		goto done;
86 	}
87 
88 	for (ent = pciidlist; ent->device; ent++) {
89 		desc = (const void *)ent->driver_data;
90 		if (desc->platform == data->platform)
91 			break;
92 	}
93 
94 	if (!ent->device)
95 		return -ENODEV;
96 
97 	for (subplatform_desc = desc->subplatforms;
98 	     subplatform_desc && subplatform_desc->subplatform;
99 	     subplatform_desc++)
100 		if (subplatform_desc->subplatform == data->subplatform)
101 			break;
102 
103 	if (data->subplatform != XE_SUBPLATFORM_NONE && !subplatform_desc)
104 		return -ENODEV;
105 
106 done:
107 	xe->sriov.__mode = data && data->sriov_mode ?
108 			   data->sriov_mode : XE_SRIOV_MODE_NONE;
109 
110 	kunit_activate_static_stub(test, read_gmdid, fake_read_gmdid);
111 
112 	xe_info_init_early(xe, desc, subplatform_desc);
113 	xe_info_init(xe, desc->graphics, desc->media);
114 
115 	return 0;
116 }
117 EXPORT_SYMBOL_IF_KUNIT(xe_pci_fake_device_init);
118 
119 /**
120  * xe_pci_live_device_gen_param - Helper to iterate Xe devices as KUnit parameters
121  * @prev: the previously returned value, or NULL for the first iteration
122  * @desc: the buffer for a parameter name
123  *
124  * Iterates over the available Xe devices on the system. Uses the device name
125  * as the parameter name.
126  *
127  * To be used only as a parameter generator function in &KUNIT_CASE_PARAM.
128  *
129  * Return: pointer to the next &struct xe_device ready to be used as a parameter
130  *         or NULL if there are no more Xe devices on the system.
131  */
132 const void *xe_pci_live_device_gen_param(const void *prev, char *desc)
133 {
134 	const struct xe_device *xe = prev;
135 	struct device *dev = xe ? xe->drm.dev : NULL;
136 	struct device *next;
137 
138 	next = driver_find_next_device(&xe_pci_driver.driver, dev);
139 	if (dev)
140 		put_device(dev);
141 	if (!next)
142 		return NULL;
143 
144 	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s", dev_name(next));
145 	return pdev_to_xe_device(to_pci_dev(next));
146 }
147 EXPORT_SYMBOL_IF_KUNIT(xe_pci_live_device_gen_param);
148