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. Note that the gem object dma_buf member isn't 61 * set from xe_gem_prime_export(), and it's needed for the move_notify() 62 * functionality, so hack that up here. Evicting the exported bo will 63 * evict also the imported bo through the move_notify() functionality if 64 * importer is on a different device. If they're on the same device, 65 * the exporter and the importer should be the same bo. 66 */ 67 swap(exported->ttm.base.dma_buf, dmabuf); 68 ret = xe_bo_evict(exported); 69 swap(exported->ttm.base.dma_buf, dmabuf); 70 if (ret) { 71 if (ret != -EINTR && ret != -ERESTARTSYS) 72 KUNIT_FAIL(test, "Evicting exporter failed with err=%d.\n", 73 ret); 74 return; 75 } 76 77 /* Verify that also importer has been evicted to SYSTEM */ 78 if (exported != imported && !xe_bo_is_mem_type(imported, XE_PL_SYSTEM)) { 79 KUNIT_FAIL(test, "Importer wasn't properly evicted.\n"); 80 return; 81 } 82 83 /* Re-validate the importer. This should move also exporter in. */ 84 ret = xe_bo_validate(imported, NULL, false); 85 if (ret) { 86 if (ret != -EINTR && ret != -ERESTARTSYS) 87 KUNIT_FAIL(test, "Validating importer failed with err=%d.\n", 88 ret); 89 return; 90 } 91 92 KUNIT_EXPECT_TRUE(test, xe_bo_is_mem_type(exported, mem_type)); 93 94 if (params->force_different_devices) 95 KUNIT_EXPECT_TRUE(test, xe_bo_is_mem_type(imported, XE_PL_TT)); 96 else 97 KUNIT_EXPECT_TRUE(test, exported == imported); 98 } 99 100 static void xe_test_dmabuf_import_same_driver(struct xe_device *xe) 101 { 102 struct kunit *test = kunit_get_current_test(); 103 struct dma_buf_test_params *params = to_dma_buf_test_params(test->priv); 104 struct drm_gem_object *import; 105 struct dma_buf *dmabuf; 106 struct xe_bo *bo; 107 size_t size; 108 109 /* No VRAM on this device? */ 110 if (!ttm_manager_type(&xe->ttm, XE_PL_VRAM0) && 111 (params->mem_mask & XE_BO_FLAG_VRAM0)) 112 return; 113 114 size = PAGE_SIZE; 115 if ((params->mem_mask & XE_BO_FLAG_VRAM0) && 116 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) 117 size = SZ_64K; 118 119 kunit_info(test, "running %s\n", __func__); 120 bo = xe_bo_create_user(xe, NULL, NULL, size, DRM_XE_GEM_CPU_CACHING_WC, 121 params->mem_mask); 122 if (IS_ERR(bo)) { 123 KUNIT_FAIL(test, "xe_bo_create() failed with err=%ld\n", 124 PTR_ERR(bo)); 125 return; 126 } 127 128 dmabuf = xe_gem_prime_export(&bo->ttm.base, 0); 129 if (IS_ERR(dmabuf)) { 130 KUNIT_FAIL(test, "xe_gem_prime_export() failed with err=%ld\n", 131 PTR_ERR(dmabuf)); 132 goto out; 133 } 134 135 import = xe_gem_prime_import(&xe->drm, dmabuf); 136 if (!IS_ERR(import)) { 137 struct xe_bo *import_bo = gem_to_xe_bo(import); 138 139 /* 140 * Did import succeed when it shouldn't due to lack of p2p support? 141 */ 142 if (params->force_different_devices && 143 !p2p_enabled(params) && 144 !(params->mem_mask & XE_BO_FLAG_SYSTEM)) { 145 KUNIT_FAIL(test, 146 "xe_gem_prime_import() succeeded when it shouldn't have\n"); 147 } else { 148 int err; 149 150 /* Is everything where we expect it to be? */ 151 xe_bo_lock(import_bo, false); 152 err = xe_bo_validate(import_bo, NULL, false); 153 154 /* Pinning in VRAM is not allowed. */ 155 if (!is_dynamic(params) && 156 params->force_different_devices && 157 !(params->mem_mask & XE_BO_FLAG_SYSTEM)) 158 KUNIT_EXPECT_EQ(test, err, -EINVAL); 159 /* Otherwise only expect interrupts or success. */ 160 else if (err && err != -EINTR && err != -ERESTARTSYS) 161 KUNIT_EXPECT_TRUE(test, !err || err == -EINTR || 162 err == -ERESTARTSYS); 163 164 if (!err) 165 check_residency(test, bo, import_bo, dmabuf); 166 xe_bo_unlock(import_bo); 167 } 168 drm_gem_object_put(import); 169 } else if (PTR_ERR(import) != -EOPNOTSUPP) { 170 /* Unexpected error code. */ 171 KUNIT_FAIL(test, 172 "xe_gem_prime_import failed with the wrong err=%ld\n", 173 PTR_ERR(import)); 174 } else if (!params->force_different_devices || 175 p2p_enabled(params) || 176 (params->mem_mask & XE_BO_FLAG_SYSTEM)) { 177 /* Shouldn't fail if we can reuse same bo, use p2p or use system */ 178 KUNIT_FAIL(test, "dynamic p2p attachment failed with err=%ld\n", 179 PTR_ERR(import)); 180 } 181 dma_buf_put(dmabuf); 182 out: 183 drm_gem_object_put(&bo->ttm.base); 184 } 185 186 static const struct dma_buf_attach_ops nop2p_attach_ops = { 187 .allow_peer2peer = false, 188 .move_notify = xe_dma_buf_move_notify 189 }; 190 191 /* 192 * We test the implementation with bos of different residency and with 193 * importers with different capabilities; some lacking p2p support and some 194 * lacking dynamic capabilities (attach_ops == NULL). We also fake 195 * different devices avoiding the import shortcut that just reuses the same 196 * gem object. 197 */ 198 static const struct dma_buf_test_params test_params[] = { 199 {.mem_mask = XE_BO_FLAG_VRAM0, 200 .attach_ops = &xe_dma_buf_attach_ops}, 201 {.mem_mask = XE_BO_FLAG_VRAM0, 202 .attach_ops = &xe_dma_buf_attach_ops, 203 .force_different_devices = true}, 204 205 {.mem_mask = XE_BO_FLAG_VRAM0, 206 .attach_ops = &nop2p_attach_ops}, 207 {.mem_mask = XE_BO_FLAG_VRAM0, 208 .attach_ops = &nop2p_attach_ops, 209 .force_different_devices = true}, 210 211 {.mem_mask = XE_BO_FLAG_VRAM0}, 212 {.mem_mask = XE_BO_FLAG_VRAM0, 213 .force_different_devices = true}, 214 215 {.mem_mask = XE_BO_FLAG_SYSTEM, 216 .attach_ops = &xe_dma_buf_attach_ops}, 217 {.mem_mask = XE_BO_FLAG_SYSTEM, 218 .attach_ops = &xe_dma_buf_attach_ops, 219 .force_different_devices = true}, 220 221 {.mem_mask = XE_BO_FLAG_SYSTEM, 222 .attach_ops = &nop2p_attach_ops}, 223 {.mem_mask = XE_BO_FLAG_SYSTEM, 224 .attach_ops = &nop2p_attach_ops, 225 .force_different_devices = true}, 226 227 {.mem_mask = XE_BO_FLAG_SYSTEM}, 228 {.mem_mask = XE_BO_FLAG_SYSTEM, 229 .force_different_devices = true}, 230 231 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 232 .attach_ops = &xe_dma_buf_attach_ops}, 233 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 234 .attach_ops = &xe_dma_buf_attach_ops, 235 .force_different_devices = true}, 236 237 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 238 .attach_ops = &nop2p_attach_ops}, 239 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 240 .attach_ops = &nop2p_attach_ops, 241 .force_different_devices = true}, 242 243 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0}, 244 {.mem_mask = XE_BO_FLAG_SYSTEM | XE_BO_FLAG_VRAM0, 245 .force_different_devices = true}, 246 247 {} 248 }; 249 250 static int dma_buf_run_device(struct xe_device *xe) 251 { 252 const struct dma_buf_test_params *params; 253 struct kunit *test = kunit_get_current_test(); 254 255 xe_pm_runtime_get(xe); 256 for (params = test_params; params->mem_mask; ++params) { 257 struct dma_buf_test_params p = *params; 258 259 p.base.id = XE_TEST_LIVE_DMA_BUF; 260 test->priv = &p; 261 xe_test_dmabuf_import_same_driver(xe); 262 } 263 xe_pm_runtime_put(xe); 264 265 /* A non-zero return would halt iteration over driver devices */ 266 return 0; 267 } 268 269 static void xe_dma_buf_kunit(struct kunit *test) 270 { 271 struct xe_device *xe = test->priv; 272 273 dma_buf_run_device(xe); 274 } 275 276 static struct kunit_case xe_dma_buf_tests[] = { 277 KUNIT_CASE_PARAM(xe_dma_buf_kunit, xe_pci_live_device_gen_param), 278 {} 279 }; 280 281 VISIBLE_IF_KUNIT 282 struct kunit_suite xe_dma_buf_test_suite = { 283 .name = "xe_dma_buf", 284 .test_cases = xe_dma_buf_tests, 285 .init = xe_kunit_helper_xe_device_live_test_init, 286 }; 287 EXPORT_SYMBOL_IF_KUNIT(xe_dma_buf_test_suite); 288