1 // SPDX-License-Identifier: GPL-2.0 AND MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include <uapi/drm/xe_drm.h> 7 8 #include <kunit/test.h> 9 #include <kunit/visibility.h> 10 11 #include "tests/xe_kunit_helpers.h" 12 #include "tests/xe_pci_test.h" 13 14 #include "xe_pci.h" 15 #include "xe_pm.h" 16 17 static bool p2p_enabled(struct dma_buf_test_params *params) 18 { 19 return IS_ENABLED(CONFIG_PCI_P2PDMA) && params->attach_ops && 20 params->attach_ops->allow_peer2peer; 21 } 22 23 static bool is_dynamic(struct dma_buf_test_params *params) 24 { 25 return IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY) && params->attach_ops && 26 params->attach_ops->move_notify; 27 } 28 29 static void check_residency(struct kunit *test, struct xe_bo *exported, 30 struct xe_bo *imported, struct dma_buf *dmabuf) 31 { 32 struct dma_buf_test_params *params = to_dma_buf_test_params(test->priv); 33 u32 mem_type; 34 int ret; 35 36 xe_bo_assert_held(exported); 37 xe_bo_assert_held(imported); 38 39 mem_type = XE_PL_VRAM0; 40 if (!(params->mem_mask & XE_BO_FLAG_VRAM0)) 41 /* No VRAM allowed */ 42 mem_type = XE_PL_TT; 43 else if (params->force_different_devices && !p2p_enabled(params)) 44 /* No P2P */ 45 mem_type = XE_PL_TT; 46 else if (params->force_different_devices && !is_dynamic(params) && 47 (params->mem_mask & XE_BO_FLAG_SYSTEM)) 48 /* Pin migrated to TT */ 49 mem_type = XE_PL_TT; 50 51 if (!xe_bo_is_mem_type(exported, mem_type)) { 52 KUNIT_FAIL(test, "Exported bo was not in expected memory type.\n"); 53 return; 54 } 55 56 if (xe_bo_is_pinned(exported)) 57 return; 58 59 /* 60 * Evict exporter. Evicting the exported bo will 61 * evict also the imported bo through the move_notify() functionality if 62 * importer is on a different device. If they're on the same device, 63 * the exporter and the importer should be the same bo. 64 */ 65 ret = xe_bo_evict(exported); 66 if (ret) { 67 if (ret != -EINTR && ret != -ERESTARTSYS) 68 KUNIT_FAIL(test, "Evicting exporter failed with err=%d.\n", 69 ret); 70 return; 71 } 72 73 /* Verify that also importer has been evicted to SYSTEM */ 74 if (exported != imported && !xe_bo_is_mem_type(imported, XE_PL_SYSTEM)) { 75 KUNIT_FAIL(test, "Importer wasn't properly evicted.\n"); 76 return; 77 } 78 79 /* Re-validate the importer. This should move also exporter in. */ 80 ret = xe_bo_validate(imported, NULL, false); 81 if (ret) { 82 if (ret != -EINTR && ret != -ERESTARTSYS) 83 KUNIT_FAIL(test, "Validating importer failed with err=%d.\n", 84 ret); 85 return; 86 } 87 88 /* 89 * If on different devices, the exporter is kept in system if 90 * possible, saving a migration step as the transfer is just 91 * likely as fast from system memory. 92 */ 93 if (params->mem_mask & XE_BO_FLAG_SYSTEM) 94 KUNIT_EXPECT_TRUE(test, xe_bo_is_mem_type(exported, XE_PL_TT)); 95 else 96 KUNIT_EXPECT_TRUE(test, xe_bo_is_mem_type(exported, mem_type)); 97 98 if (params->force_different_devices) 99 KUNIT_EXPECT_TRUE(test, xe_bo_is_mem_type(imported, XE_PL_TT)); 100 else 101 KUNIT_EXPECT_TRUE(test, exported == imported); 102 } 103 104 static void xe_test_dmabuf_import_same_driver(struct xe_device *xe) 105 { 106 struct kunit *test = kunit_get_current_test(); 107 struct dma_buf_test_params *params = to_dma_buf_test_params(test->priv); 108 struct drm_gem_object *import; 109 struct dma_buf *dmabuf; 110 struct xe_bo *bo; 111 size_t size; 112 113 /* No VRAM on this device? */ 114 if (!ttm_manager_type(&xe->ttm, XE_PL_VRAM0) && 115 (params->mem_mask & XE_BO_FLAG_VRAM0)) 116 return; 117 118 size = PAGE_SIZE; 119 if ((params->mem_mask & XE_BO_FLAG_VRAM0) && 120 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) 121 size = SZ_64K; 122 123 kunit_info(test, "running %s\n", __func__); 124 bo = xe_bo_create_user(xe, NULL, NULL, size, DRM_XE_GEM_CPU_CACHING_WC, 125 params->mem_mask); 126 if (IS_ERR(bo)) { 127 KUNIT_FAIL(test, "xe_bo_create() failed with err=%ld\n", 128 PTR_ERR(bo)); 129 return; 130 } 131 132 dmabuf = xe_gem_prime_export(&bo->ttm.base, 0); 133 if (IS_ERR(dmabuf)) { 134 KUNIT_FAIL(test, "xe_gem_prime_export() failed with err=%ld\n", 135 PTR_ERR(dmabuf)); 136 goto out; 137 } 138 bo->ttm.base.dma_buf = dmabuf; 139 140 import = xe_gem_prime_import(&xe->drm, dmabuf); 141 if (!IS_ERR(import)) { 142 struct xe_bo *import_bo = gem_to_xe_bo(import); 143 144 /* 145 * Did import succeed when it shouldn't due to lack of p2p support? 146 */ 147 if (params->force_different_devices && 148 !p2p_enabled(params) && 149 !(params->mem_mask & XE_BO_FLAG_SYSTEM)) { 150 KUNIT_FAIL(test, 151 "xe_gem_prime_import() succeeded when it shouldn't have\n"); 152 } else { 153 int err; 154 155 /* Is everything where we expect it to be? */ 156 xe_bo_lock(import_bo, false); 157 err = xe_bo_validate(import_bo, NULL, false); 158 159 /* Pinning in VRAM is not allowed. */ 160 if (!is_dynamic(params) && 161 params->force_different_devices && 162 !(params->mem_mask & XE_BO_FLAG_SYSTEM)) 163 KUNIT_EXPECT_EQ(test, err, -EINVAL); 164 /* Otherwise only expect interrupts or success. */ 165 else if (err && err != -EINTR && err != -ERESTARTSYS) 166 KUNIT_EXPECT_TRUE(test, !err || err == -EINTR || 167 err == -ERESTARTSYS); 168 169 if (!err) 170 check_residency(test, bo, import_bo, dmabuf); 171 xe_bo_unlock(import_bo); 172 } 173 drm_gem_object_put(import); 174 } else if (PTR_ERR(import) != -EOPNOTSUPP) { 175 /* Unexpected error code. */ 176 KUNIT_FAIL(test, 177 "xe_gem_prime_import failed with the wrong err=%ld\n", 178 PTR_ERR(import)); 179 } else if (!params->force_different_devices || 180 p2p_enabled(params) || 181 (params->mem_mask & XE_BO_FLAG_SYSTEM)) { 182 /* Shouldn't fail if we can reuse same bo, use p2p or use system */ 183 KUNIT_FAIL(test, "dynamic p2p attachment failed with err=%ld\n", 184 PTR_ERR(import)); 185 } 186 bo->ttm.base.dma_buf = NULL; 187 dma_buf_put(dmabuf); 188 out: 189 drm_gem_object_put(&bo->ttm.base); 190 } 191 192 static const struct dma_buf_attach_ops nop2p_attach_ops = { 193 .allow_peer2peer = false, 194 .move_notify = xe_dma_buf_move_notify 195 }; 196 197 /* 198 * We test the implementation with bos of different residency and with 199 * importers with different capabilities; some lacking p2p support and some 200 * lacking dynamic capabilities (attach_ops == NULL). We also fake 201 * different devices avoiding the import shortcut that just reuses the same 202 * gem object. 203 */ 204 static const struct dma_buf_test_params test_params[] = { 205 {.mem_mask = XE_BO_FLAG_VRAM0, 206 .attach_ops = &xe_dma_buf_attach_ops}, 207 {.mem_mask = XE_BO_FLAG_VRAM0 | XE_BO_FLAG_NEEDS_CPU_ACCESS, 208 .attach_ops = &xe_dma_buf_attach_ops, 209 .force_different_devices = true}, 210 211 {.mem_mask = XE_BO_FLAG_VRAM0, 212 .attach_ops = &nop2p_attach_ops}, 213 {.mem_mask = XE_BO_FLAG_VRAM0, 214 .attach_ops = &nop2p_attach_ops, 215 .force_different_devices = true}, 216 217 {.mem_mask = XE_BO_FLAG_VRAM0}, 218 {.mem_mask = XE_BO_FLAG_VRAM0, 219 .force_different_devices = true}, 220 221 {.mem_mask = XE_BO_FLAG_SYSTEM, 222 .attach_ops = &xe_dma_buf_attach_ops}, 223 {.mem_mask = XE_BO_FLAG_SYSTEM, 224 .attach_ops = &xe_dma_buf_attach_ops, 225 .force_different_devices = true}, 226 227 {.mem_mask = XE_BO_FLAG_SYSTEM, 228 .attach_ops = &nop2p_attach_ops}, 229 {.mem_mask = XE_BO_FLAG_SYSTEM, 230 .attach_ops = &nop2p_attach_ops, 231 .force_different_devices = true}, 232 233 {.mem_mask = XE_BO_FLAG_SYSTEM}, 234 {.mem_mask = XE_BO_FLAG_SYSTEM, 235 .force_different_devices = true}, 236 237 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 238 .attach_ops = &xe_dma_buf_attach_ops}, 239 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0 | 240 XE_BO_FLAG_NEEDS_CPU_ACCESS, 241 .attach_ops = &xe_dma_buf_attach_ops, 242 .force_different_devices = true}, 243 244 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 245 .attach_ops = &nop2p_attach_ops}, 246 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 247 .attach_ops = &nop2p_attach_ops, 248 .force_different_devices = true}, 249 250 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0}, 251 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 252 .force_different_devices = true}, 253 254 {} 255 }; 256 257 static int dma_buf_run_device(struct xe_device *xe) 258 { 259 const struct dma_buf_test_params *params; 260 struct kunit *test = kunit_get_current_test(); 261 262 xe_pm_runtime_get(xe); 263 for (params = test_params; params->mem_mask; ++params) { 264 struct dma_buf_test_params p = *params; 265 266 p.base.id = XE_TEST_LIVE_DMA_BUF; 267 test->priv = &p; 268 xe_test_dmabuf_import_same_driver(xe); 269 } 270 xe_pm_runtime_put(xe); 271 272 /* A non-zero return would halt iteration over driver devices */ 273 return 0; 274 } 275 276 static void xe_dma_buf_kunit(struct kunit *test) 277 { 278 struct xe_device *xe = test->priv; 279 280 dma_buf_run_device(xe); 281 } 282 283 static struct kunit_case xe_dma_buf_tests[] = { 284 KUNIT_CASE_PARAM(xe_dma_buf_kunit, xe_pci_live_device_gen_param), 285 {} 286 }; 287 288 VISIBLE_IF_KUNIT 289 struct kunit_suite xe_dma_buf_test_suite = { 290 .name = "xe_dma_buf", 291 .test_cases = xe_dma_buf_tests, 292 .init = xe_kunit_helper_xe_device_live_test_init, 293 }; 294 EXPORT_SYMBOL_IF_KUNIT(xe_dma_buf_test_suite); 295