2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016 Intel Corporation
8 #include "i915_selftest.h"
10 #include "mock_dmabuf.h"
11 #include "selftests/mock_gem_device.h"
13 static int igt_dmabuf_export(void *arg)
15 struct drm_i915_private *i915 = arg;
16 struct drm_i915_gem_object *obj;
17 struct dma_buf *dmabuf;
19 obj = i915_gem_object_create_shmem(i915, PAGE_SIZE);
23 dmabuf = i915_gem_prime_export(&obj->base, 0);
24 i915_gem_object_put(obj);
26 pr_err("i915_gem_prime_export failed with err=%d\n",
27 (int)PTR_ERR(dmabuf));
28 return PTR_ERR(dmabuf);
35 static int igt_dmabuf_import_self(void *arg)
37 struct drm_i915_private *i915 = arg;
38 struct drm_i915_gem_object *obj, *import_obj;
39 struct drm_gem_object *import;
40 struct dma_buf *dmabuf;
43 obj = i915_gem_object_create_shmem(i915, PAGE_SIZE);
47 dmabuf = i915_gem_prime_export(&obj->base, 0);
49 pr_err("i915_gem_prime_export failed with err=%d\n",
50 (int)PTR_ERR(dmabuf));
51 err = PTR_ERR(dmabuf);
55 import = i915_gem_prime_import(&i915->drm, dmabuf);
57 pr_err("i915_gem_prime_import failed with err=%d\n",
58 (int)PTR_ERR(import));
59 err = PTR_ERR(import);
62 import_obj = to_intel_bo(import);
64 if (import != &obj->base) {
65 pr_err("i915_gem_prime_import created a new object!\n");
70 i915_gem_object_lock(import_obj, NULL);
71 err = __i915_gem_object_get_pages(import_obj);
72 i915_gem_object_unlock(import_obj);
74 pr_err("Same object dma-buf get_pages failed!\n");
80 i915_gem_object_put(import_obj);
84 i915_gem_object_put(obj);
88 static int igt_dmabuf_import_same_driver_lmem(void *arg)
90 struct drm_i915_private *i915 = arg;
91 struct intel_memory_region *lmem = i915->mm.regions[INTEL_REGION_LMEM];
92 struct drm_i915_gem_object *obj;
93 struct drm_gem_object *import;
94 struct dma_buf *dmabuf;
100 force_different_devices = true;
102 obj = __i915_gem_object_create_user(i915, PAGE_SIZE, &lmem, 1);
104 pr_err("__i915_gem_object_create_user failed with err=%ld\n",
110 dmabuf = i915_gem_prime_export(&obj->base, 0);
111 if (IS_ERR(dmabuf)) {
112 pr_err("i915_gem_prime_export failed with err=%ld\n",
114 err = PTR_ERR(dmabuf);
119 * We expect an import of an LMEM-only object to fail with
120 * -EOPNOTSUPP because it can't be migrated to SMEM.
122 import = i915_gem_prime_import(&i915->drm, dmabuf);
123 if (!IS_ERR(import)) {
124 drm_gem_object_put(import);
125 pr_err("i915_gem_prime_import succeeded when it shouldn't have\n");
127 } else if (PTR_ERR(import) != -EOPNOTSUPP) {
128 pr_err("i915_gem_prime_import failed with the wrong err=%ld\n",
130 err = PTR_ERR(import);
137 i915_gem_object_put(obj);
139 force_different_devices = false;
143 static int igt_dmabuf_import_same_driver(struct drm_i915_private *i915,
144 struct intel_memory_region **regions,
145 unsigned int num_regions)
147 struct drm_i915_gem_object *obj, *import_obj;
148 struct drm_gem_object *import;
149 struct dma_buf *dmabuf;
150 struct dma_buf_attachment *import_attach;
155 force_different_devices = true;
157 obj = __i915_gem_object_create_user(i915, PAGE_SIZE,
158 regions, num_regions);
160 pr_err("__i915_gem_object_create_user failed with err=%ld\n",
166 dmabuf = i915_gem_prime_export(&obj->base, 0);
167 if (IS_ERR(dmabuf)) {
168 pr_err("i915_gem_prime_export failed with err=%ld\n",
170 err = PTR_ERR(dmabuf);
174 import = i915_gem_prime_import(&i915->drm, dmabuf);
175 if (IS_ERR(import)) {
176 pr_err("i915_gem_prime_import failed with err=%ld\n",
178 err = PTR_ERR(import);
181 import_obj = to_intel_bo(import);
183 if (import == &obj->base) {
184 pr_err("i915_gem_prime_import reused gem object!\n");
189 i915_gem_object_lock(import_obj, NULL);
190 err = __i915_gem_object_get_pages(import_obj);
192 pr_err("Different objects dma-buf get_pages failed!\n");
193 i915_gem_object_unlock(import_obj);
198 * If the exported object is not in system memory, something
199 * weird is going on. TODO: When p2p is supported, this is no
200 * longer considered weird.
202 if (obj->mm.region != i915->mm.regions[INTEL_REGION_SMEM]) {
203 pr_err("Exported dma-buf is not in system memory\n");
207 i915_gem_object_unlock(import_obj);
209 /* Now try a fake an importer */
210 import_attach = dma_buf_attach(dmabuf, obj->base.dev->dev);
211 if (IS_ERR(import_attach)) {
212 err = PTR_ERR(import_attach);
216 st = dma_buf_map_attachment(import_attach, DMA_BIDIRECTIONAL);
222 timeout = dma_resv_wait_timeout(dmabuf->resv, false, true, 5 * HZ);
224 pr_err("dmabuf wait for exclusive fence timed out.\n");
227 err = timeout > 0 ? 0 : timeout;
228 dma_buf_unmap_attachment(import_attach, st, DMA_BIDIRECTIONAL);
230 dma_buf_detach(dmabuf, import_attach);
232 i915_gem_object_put(import_obj);
236 i915_gem_object_put(obj);
238 force_different_devices = false;
242 static int igt_dmabuf_import_same_driver_smem(void *arg)
244 struct drm_i915_private *i915 = arg;
245 struct intel_memory_region *smem = i915->mm.regions[INTEL_REGION_SMEM];
247 return igt_dmabuf_import_same_driver(i915, &smem, 1);
250 static int igt_dmabuf_import_same_driver_lmem_smem(void *arg)
252 struct drm_i915_private *i915 = arg;
253 struct intel_memory_region *regions[2];
255 if (!i915->mm.regions[INTEL_REGION_LMEM])
258 regions[0] = i915->mm.regions[INTEL_REGION_LMEM];
259 regions[1] = i915->mm.regions[INTEL_REGION_SMEM];
260 return igt_dmabuf_import_same_driver(i915, regions, 2);
263 static int igt_dmabuf_import(void *arg)
265 struct drm_i915_private *i915 = arg;
266 struct drm_i915_gem_object *obj;
267 struct dma_buf *dmabuf;
268 void *obj_map, *dma_map;
269 struct dma_buf_map map;
270 u32 pattern[] = { 0, 0xaa, 0xcc, 0x55, 0xff };
273 dmabuf = mock_dmabuf(1);
275 return PTR_ERR(dmabuf);
277 obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf));
279 pr_err("i915_gem_prime_import failed with err=%d\n",
285 if (obj->base.dev != &i915->drm) {
286 pr_err("i915_gem_prime_import created a non-i915 object!\n");
291 if (obj->base.size != PAGE_SIZE) {
292 pr_err("i915_gem_prime_import is wrong size found %lld, expected %ld\n",
293 (long long)obj->base.size, PAGE_SIZE);
298 err = dma_buf_vmap(dmabuf, &map);
299 dma_map = err ? NULL : map.vaddr;
301 pr_err("dma_buf_vmap failed\n");
306 if (0) { /* Can not yet map dmabuf */
307 obj_map = i915_gem_object_pin_map(obj, I915_MAP_WB);
308 if (IS_ERR(obj_map)) {
309 err = PTR_ERR(obj_map);
310 pr_err("i915_gem_object_pin_map failed with err=%d\n", err);
314 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
315 memset(dma_map, pattern[i], PAGE_SIZE);
316 if (memchr_inv(obj_map, pattern[i], PAGE_SIZE)) {
318 pr_err("imported vmap not all set to %x!\n", pattern[i]);
319 i915_gem_object_unpin_map(obj);
324 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
325 memset(obj_map, pattern[i], PAGE_SIZE);
326 if (memchr_inv(dma_map, pattern[i], PAGE_SIZE)) {
328 pr_err("exported vmap not all set to %x!\n", pattern[i]);
329 i915_gem_object_unpin_map(obj);
334 i915_gem_object_unpin_map(obj);
339 dma_buf_vunmap(dmabuf, &map);
341 i915_gem_object_put(obj);
347 static int igt_dmabuf_import_ownership(void *arg)
349 struct drm_i915_private *i915 = arg;
350 struct drm_i915_gem_object *obj;
351 struct dma_buf *dmabuf;
352 struct dma_buf_map map;
356 dmabuf = mock_dmabuf(1);
358 return PTR_ERR(dmabuf);
360 err = dma_buf_vmap(dmabuf, &map);
361 ptr = err ? NULL : map.vaddr;
363 pr_err("dma_buf_vmap failed\n");
368 memset(ptr, 0xc5, PAGE_SIZE);
369 dma_buf_vunmap(dmabuf, &map);
371 obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf));
373 pr_err("i915_gem_prime_import failed with err=%d\n",
381 err = i915_gem_object_pin_pages_unlocked(obj);
383 pr_err("i915_gem_object_pin_pages failed with err=%d\n", err);
388 i915_gem_object_unpin_pages(obj);
390 i915_gem_object_put(obj);
398 static int igt_dmabuf_export_vmap(void *arg)
400 struct drm_i915_private *i915 = arg;
401 struct drm_i915_gem_object *obj;
402 struct dma_buf *dmabuf;
403 struct dma_buf_map map;
407 obj = i915_gem_object_create_shmem(i915, PAGE_SIZE);
411 dmabuf = i915_gem_prime_export(&obj->base, 0);
412 if (IS_ERR(dmabuf)) {
413 pr_err("i915_gem_prime_export failed with err=%d\n",
414 (int)PTR_ERR(dmabuf));
415 err = PTR_ERR(dmabuf);
418 i915_gem_object_put(obj);
420 err = dma_buf_vmap(dmabuf, &map);
421 ptr = err ? NULL : map.vaddr;
423 pr_err("dma_buf_vmap failed\n");
428 if (memchr_inv(ptr, 0, dmabuf->size)) {
429 pr_err("Exported object not initialiased to zero!\n");
434 memset(ptr, 0xc5, dmabuf->size);
437 dma_buf_vunmap(dmabuf, &map);
443 i915_gem_object_put(obj);
447 int i915_gem_dmabuf_mock_selftests(void)
449 static const struct i915_subtest tests[] = {
450 SUBTEST(igt_dmabuf_export),
451 SUBTEST(igt_dmabuf_import_self),
452 SUBTEST(igt_dmabuf_import),
453 SUBTEST(igt_dmabuf_import_ownership),
454 SUBTEST(igt_dmabuf_export_vmap),
456 struct drm_i915_private *i915;
459 i915 = mock_gem_device();
463 err = i915_subtests(tests, i915);
465 mock_destroy_device(i915);
469 int i915_gem_dmabuf_live_selftests(struct drm_i915_private *i915)
471 static const struct i915_subtest tests[] = {
472 SUBTEST(igt_dmabuf_export),
473 SUBTEST(igt_dmabuf_import_same_driver_lmem),
474 SUBTEST(igt_dmabuf_import_same_driver_smem),
475 SUBTEST(igt_dmabuf_import_same_driver_lmem_smem),
478 return i915_subtests(tests, i915);