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
;
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
);
63 if (import
!= &obj
->base
) {
64 pr_err("i915_gem_prime_import created a new object!\n");
71 i915_gem_object_put(to_intel_bo(import
));
75 i915_gem_object_put(obj
);
79 static int igt_dmabuf_import(void *arg
)
81 struct drm_i915_private
*i915
= arg
;
82 struct drm_i915_gem_object
*obj
;
83 struct dma_buf
*dmabuf
;
84 void *obj_map
, *dma_map
;
85 struct dma_buf_map map
;
86 u32 pattern
[] = { 0, 0xaa, 0xcc, 0x55, 0xff };
89 dmabuf
= mock_dmabuf(1);
91 return PTR_ERR(dmabuf
);
93 obj
= to_intel_bo(i915_gem_prime_import(&i915
->drm
, dmabuf
));
95 pr_err("i915_gem_prime_import failed with err=%d\n",
101 if (obj
->base
.dev
!= &i915
->drm
) {
102 pr_err("i915_gem_prime_import created a non-i915 object!\n");
107 if (obj
->base
.size
!= PAGE_SIZE
) {
108 pr_err("i915_gem_prime_import is wrong size found %lld, expected %ld\n",
109 (long long)obj
->base
.size
, PAGE_SIZE
);
114 err
= dma_buf_vmap(dmabuf
, &map
);
115 dma_map
= err
? NULL
: map
.vaddr
;
117 pr_err("dma_buf_vmap failed\n");
122 if (0) { /* Can not yet map dmabuf */
123 obj_map
= i915_gem_object_pin_map(obj
, I915_MAP_WB
);
124 if (IS_ERR(obj_map
)) {
125 err
= PTR_ERR(obj_map
);
126 pr_err("i915_gem_object_pin_map failed with err=%d\n", err
);
130 for (i
= 0; i
< ARRAY_SIZE(pattern
); i
++) {
131 memset(dma_map
, pattern
[i
], PAGE_SIZE
);
132 if (memchr_inv(obj_map
, pattern
[i
], PAGE_SIZE
)) {
134 pr_err("imported vmap not all set to %x!\n", pattern
[i
]);
135 i915_gem_object_unpin_map(obj
);
140 for (i
= 0; i
< ARRAY_SIZE(pattern
); i
++) {
141 memset(obj_map
, pattern
[i
], PAGE_SIZE
);
142 if (memchr_inv(dma_map
, pattern
[i
], PAGE_SIZE
)) {
144 pr_err("exported vmap not all set to %x!\n", pattern
[i
]);
145 i915_gem_object_unpin_map(obj
);
150 i915_gem_object_unpin_map(obj
);
155 dma_buf_vunmap(dmabuf
, &map
);
157 i915_gem_object_put(obj
);
163 static int igt_dmabuf_import_ownership(void *arg
)
165 struct drm_i915_private
*i915
= arg
;
166 struct drm_i915_gem_object
*obj
;
167 struct dma_buf
*dmabuf
;
168 struct dma_buf_map map
;
172 dmabuf
= mock_dmabuf(1);
174 return PTR_ERR(dmabuf
);
176 err
= dma_buf_vmap(dmabuf
, &map
);
177 ptr
= err
? NULL
: map
.vaddr
;
179 pr_err("dma_buf_vmap failed\n");
184 memset(ptr
, 0xc5, PAGE_SIZE
);
185 dma_buf_vunmap(dmabuf
, &map
);
187 obj
= to_intel_bo(i915_gem_prime_import(&i915
->drm
, dmabuf
));
189 pr_err("i915_gem_prime_import failed with err=%d\n",
197 err
= i915_gem_object_pin_pages(obj
);
199 pr_err("i915_gem_object_pin_pages failed with err=%d\n", err
);
204 i915_gem_object_unpin_pages(obj
);
206 i915_gem_object_put(obj
);
214 static int igt_dmabuf_export_vmap(void *arg
)
216 struct drm_i915_private
*i915
= arg
;
217 struct drm_i915_gem_object
*obj
;
218 struct dma_buf
*dmabuf
;
219 struct dma_buf_map map
;
223 obj
= i915_gem_object_create_shmem(i915
, PAGE_SIZE
);
227 dmabuf
= i915_gem_prime_export(&obj
->base
, 0);
228 if (IS_ERR(dmabuf
)) {
229 pr_err("i915_gem_prime_export failed with err=%d\n",
230 (int)PTR_ERR(dmabuf
));
231 err
= PTR_ERR(dmabuf
);
234 i915_gem_object_put(obj
);
236 err
= dma_buf_vmap(dmabuf
, &map
);
237 ptr
= err
? NULL
: map
.vaddr
;
239 pr_err("dma_buf_vmap failed\n");
244 if (memchr_inv(ptr
, 0, dmabuf
->size
)) {
245 pr_err("Exported object not initialiased to zero!\n");
250 memset(ptr
, 0xc5, dmabuf
->size
);
253 dma_buf_vunmap(dmabuf
, &map
);
259 i915_gem_object_put(obj
);
263 int i915_gem_dmabuf_mock_selftests(void)
265 static const struct i915_subtest tests
[] = {
266 SUBTEST(igt_dmabuf_export
),
267 SUBTEST(igt_dmabuf_import_self
),
268 SUBTEST(igt_dmabuf_import
),
269 SUBTEST(igt_dmabuf_import_ownership
),
270 SUBTEST(igt_dmabuf_export_vmap
),
272 struct drm_i915_private
*i915
;
275 i915
= mock_gem_device();
279 err
= i915_subtests(tests
, i915
);
281 mock_destroy_device(i915
);
285 int i915_gem_dmabuf_live_selftests(struct drm_i915_private
*i915
)
287 static const struct i915_subtest tests
[] = {
288 SUBTEST(igt_dmabuf_export
),
291 return i915_subtests(tests
, i915
);