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 struct kunit_test_data { 16 int ndevs; 17 xe_device_fn xe_fn; 18 }; 19 20 static int dev_to_xe_device_fn(struct device *dev, void *__data) 21 22 { 23 struct drm_device *drm = dev_get_drvdata(dev); 24 struct kunit_test_data *data = __data; 25 int ret = 0; 26 int idx; 27 28 data->ndevs++; 29 30 if (drm_dev_enter(drm, &idx)) 31 ret = data->xe_fn(to_xe_device(dev_get_drvdata(dev))); 32 drm_dev_exit(idx); 33 34 return ret; 35 } 36 37 /** 38 * xe_call_for_each_device - Iterate over all devices this driver binds to 39 * @xe_fn: Function to call for each device. 40 * 41 * This function iterated over all devices this driver binds to, and calls 42 * @xe_fn: for each one of them. If the called function returns anything else 43 * than 0, iteration is stopped and the return value is returned by this 44 * function. Across each function call, drm_dev_enter() / drm_dev_exit() is 45 * called for the corresponding drm device. 46 * 47 * Return: Number of devices iterated or 48 * the error code of a call to @xe_fn returning an error code. 49 */ 50 int xe_call_for_each_device(xe_device_fn xe_fn) 51 { 52 int ret; 53 struct kunit_test_data data = { 54 .xe_fn = xe_fn, 55 .ndevs = 0, 56 }; 57 58 ret = driver_for_each_device(&xe_pci_driver.driver, NULL, 59 &data, dev_to_xe_device_fn); 60 61 if (!data.ndevs) 62 kunit_skip(current->kunit_test, "test runs only on hardware\n"); 63 64 return ret ?: data.ndevs; 65 } 66 67 /** 68 * xe_call_for_each_graphics_ip - Iterate over all recognized graphics IPs 69 * @xe_fn: Function to call for each device. 70 * 71 * This function iterates over the descriptors for all graphics IPs recognized 72 * by the driver and calls @xe_fn: for each one of them. 73 */ 74 void xe_call_for_each_graphics_ip(xe_graphics_fn xe_fn) 75 { 76 const struct xe_graphics_desc *ip, *last = NULL; 77 78 for (int i = 0; i < ARRAY_SIZE(graphics_ip_map); i++) { 79 ip = graphics_ip_map[i].ip; 80 if (ip == last) 81 continue; 82 83 xe_fn(ip); 84 last = ip; 85 } 86 } 87 EXPORT_SYMBOL_IF_KUNIT(xe_call_for_each_graphics_ip); 88 89 /** 90 * xe_call_for_each_media_ip - Iterate over all recognized media IPs 91 * @xe_fn: Function to call for each device. 92 * 93 * This function iterates over the descriptors for all media IPs recognized 94 * by the driver and calls @xe_fn: for each one of them. 95 */ 96 void xe_call_for_each_media_ip(xe_media_fn xe_fn) 97 { 98 const struct xe_media_desc *ip, *last = NULL; 99 100 for (int i = 0; i < ARRAY_SIZE(media_ip_map); i++) { 101 ip = media_ip_map[i].ip; 102 if (ip == last) 103 continue; 104 105 xe_fn(ip); 106 last = ip; 107 } 108 } 109 EXPORT_SYMBOL_IF_KUNIT(xe_call_for_each_media_ip); 110 111 static void fake_read_gmdid(struct xe_device *xe, enum xe_gmdid_type type, 112 u32 *ver, u32 *revid) 113 { 114 struct kunit *test = kunit_get_current_test(); 115 struct xe_pci_fake_data *data = test->priv; 116 117 if (type == GMDID_MEDIA) { 118 *ver = data->media_verx100; 119 *revid = xe_step_to_gmdid(data->media_step); 120 } else { 121 *ver = data->graphics_verx100; 122 *revid = xe_step_to_gmdid(data->graphics_step); 123 } 124 } 125 126 int xe_pci_fake_device_init(struct xe_device *xe) 127 { 128 struct kunit *test = kunit_get_current_test(); 129 struct xe_pci_fake_data *data = test->priv; 130 const struct pci_device_id *ent = pciidlist; 131 const struct xe_device_desc *desc; 132 const struct xe_subplatform_desc *subplatform_desc; 133 134 if (!data) { 135 desc = (const void *)ent->driver_data; 136 subplatform_desc = NULL; 137 goto done; 138 } 139 140 for (ent = pciidlist; ent->device; ent++) { 141 desc = (const void *)ent->driver_data; 142 if (desc->platform == data->platform) 143 break; 144 } 145 146 if (!ent->device) 147 return -ENODEV; 148 149 for (subplatform_desc = desc->subplatforms; 150 subplatform_desc && subplatform_desc->subplatform; 151 subplatform_desc++) 152 if (subplatform_desc->subplatform == data->subplatform) 153 break; 154 155 if (data->subplatform != XE_SUBPLATFORM_NONE && !subplatform_desc) 156 return -ENODEV; 157 158 done: 159 xe->sriov.__mode = data && data->sriov_mode ? 160 data->sriov_mode : XE_SRIOV_MODE_NONE; 161 162 kunit_activate_static_stub(test, read_gmdid, fake_read_gmdid); 163 164 xe_info_init_early(xe, desc, subplatform_desc); 165 xe_info_init(xe, desc->graphics, desc->media); 166 167 return 0; 168 } 169 EXPORT_SYMBOL_IF_KUNIT(xe_pci_fake_device_init); 170