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 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 */ 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 */ 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 */ 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 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 104 static void fake_xe_info_probe_tile_count(struct xe_device *xe) 105 { 106 /* Nothing to do, just use the statically defined value. */ 107 } 108 109 int xe_pci_fake_device_init(struct xe_device *xe) 110 { 111 struct kunit *test = kunit_get_current_test(); 112 struct xe_pci_fake_data *data = test->priv; 113 const struct pci_device_id *ent = pciidlist; 114 const struct xe_device_desc *desc; 115 const struct xe_subplatform_desc *subplatform_desc; 116 117 if (!data) { 118 desc = (const void *)ent->driver_data; 119 subplatform_desc = NULL; 120 goto done; 121 } 122 123 for (ent = pciidlist; ent->device; ent++) { 124 desc = (const void *)ent->driver_data; 125 if (desc->platform == data->platform) 126 break; 127 } 128 129 if (!ent->device) 130 return -ENODEV; 131 132 for (subplatform_desc = desc->subplatforms; 133 subplatform_desc && subplatform_desc->subplatform; 134 subplatform_desc++) 135 if (subplatform_desc->subplatform == data->subplatform) 136 break; 137 138 if (data->subplatform != XE_SUBPLATFORM_NONE && !subplatform_desc) 139 return -ENODEV; 140 141 done: 142 xe->sriov.__mode = data && data->sriov_mode ? 143 data->sriov_mode : XE_SRIOV_MODE_NONE; 144 145 kunit_activate_static_stub(test, read_gmdid, fake_read_gmdid); 146 kunit_activate_static_stub(test, xe_info_probe_tile_count, 147 fake_xe_info_probe_tile_count); 148 149 xe_info_init_early(xe, desc, subplatform_desc); 150 xe_info_init(xe, desc); 151 152 return 0; 153 } 154 EXPORT_SYMBOL_IF_KUNIT(xe_pci_fake_device_init); 155 156 /** 157 * xe_pci_live_device_gen_param - Helper to iterate Xe devices as KUnit parameters 158 * @prev: the previously returned value, or NULL for the first iteration 159 * @desc: the buffer for a parameter name 160 * 161 * Iterates over the available Xe devices on the system. Uses the device name 162 * as the parameter name. 163 * 164 * To be used only as a parameter generator function in &KUNIT_CASE_PARAM. 165 * 166 * Return: pointer to the next &struct xe_device ready to be used as a parameter 167 * or NULL if there are no more Xe devices on the system. 168 */ 169 const void *xe_pci_live_device_gen_param(const void *prev, char *desc) 170 { 171 const struct xe_device *xe = prev; 172 struct device *dev = xe ? xe->drm.dev : NULL; 173 struct device *next; 174 175 next = driver_find_next_device(&xe_pci_driver.driver, dev); 176 if (dev) 177 put_device(dev); 178 if (!next) 179 return NULL; 180 181 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s", dev_name(next)); 182 return pdev_to_xe_device(to_pci_dev(next)); 183 } 184 EXPORT_SYMBOL_IF_KUNIT(xe_pci_live_device_gen_param); 185