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 u32 pattern
[] = { 0, 0xaa, 0xcc, 0x55, 0xff };
88 dmabuf
= mock_dmabuf(1);
90 return PTR_ERR(dmabuf
);
92 obj
= to_intel_bo(i915_gem_prime_import(&i915
->drm
, dmabuf
));
94 pr_err("i915_gem_prime_import failed with err=%d\n",
100 if (obj
->base
.dev
!= &i915
->drm
) {
101 pr_err("i915_gem_prime_import created a non-i915 object!\n");
106 if (obj
->base
.size
!= PAGE_SIZE
) {
107 pr_err("i915_gem_prime_import is wrong size found %lld, expected %ld\n",
108 (long long)obj
->base
.size
, PAGE_SIZE
);
113 dma_map
= dma_buf_vmap(dmabuf
);
115 pr_err("dma_buf_vmap failed\n");
120 if (0) { /* Can not yet map dmabuf */
121 obj_map
= i915_gem_object_pin_map(obj
, I915_MAP_WB
);
122 if (IS_ERR(obj_map
)) {
123 err
= PTR_ERR(obj_map
);
124 pr_err("i915_gem_object_pin_map failed with err=%d\n", err
);
128 for (i
= 0; i
< ARRAY_SIZE(pattern
); i
++) {
129 memset(dma_map
, pattern
[i
], PAGE_SIZE
);
130 if (memchr_inv(obj_map
, pattern
[i
], PAGE_SIZE
)) {
132 pr_err("imported vmap not all set to %x!\n", pattern
[i
]);
133 i915_gem_object_unpin_map(obj
);
138 for (i
= 0; i
< ARRAY_SIZE(pattern
); i
++) {
139 memset(obj_map
, pattern
[i
], PAGE_SIZE
);
140 if (memchr_inv(dma_map
, pattern
[i
], PAGE_SIZE
)) {
142 pr_err("exported vmap not all set to %x!\n", pattern
[i
]);
143 i915_gem_object_unpin_map(obj
);
148 i915_gem_object_unpin_map(obj
);
153 dma_buf_vunmap(dmabuf
, dma_map
);
155 i915_gem_object_put(obj
);
161 static int igt_dmabuf_import_ownership(void *arg
)
163 struct drm_i915_private
*i915
= arg
;
164 struct drm_i915_gem_object
*obj
;
165 struct dma_buf
*dmabuf
;
169 dmabuf
= mock_dmabuf(1);
171 return PTR_ERR(dmabuf
);
173 ptr
= dma_buf_vmap(dmabuf
);
175 pr_err("dma_buf_vmap failed\n");
180 memset(ptr
, 0xc5, PAGE_SIZE
);
181 dma_buf_vunmap(dmabuf
, ptr
);
183 obj
= to_intel_bo(i915_gem_prime_import(&i915
->drm
, dmabuf
));
185 pr_err("i915_gem_prime_import failed with err=%d\n",
193 err
= i915_gem_object_pin_pages(obj
);
195 pr_err("i915_gem_object_pin_pages failed with err=%d\n", err
);
200 i915_gem_object_unpin_pages(obj
);
202 i915_gem_object_put(obj
);
210 static int igt_dmabuf_export_vmap(void *arg
)
212 struct drm_i915_private
*i915
= arg
;
213 struct drm_i915_gem_object
*obj
;
214 struct dma_buf
*dmabuf
;
218 obj
= i915_gem_object_create_shmem(i915
, PAGE_SIZE
);
222 dmabuf
= i915_gem_prime_export(&obj
->base
, 0);
223 if (IS_ERR(dmabuf
)) {
224 pr_err("i915_gem_prime_export failed with err=%d\n",
225 (int)PTR_ERR(dmabuf
));
226 err
= PTR_ERR(dmabuf
);
229 i915_gem_object_put(obj
);
231 ptr
= dma_buf_vmap(dmabuf
);
233 pr_err("dma_buf_vmap failed\n");
238 if (memchr_inv(ptr
, 0, dmabuf
->size
)) {
239 pr_err("Exported object not initialiased to zero!\n");
244 memset(ptr
, 0xc5, dmabuf
->size
);
247 dma_buf_vunmap(dmabuf
, ptr
);
253 i915_gem_object_put(obj
);
257 int i915_gem_dmabuf_mock_selftests(void)
259 static const struct i915_subtest tests
[] = {
260 SUBTEST(igt_dmabuf_export
),
261 SUBTEST(igt_dmabuf_import_self
),
262 SUBTEST(igt_dmabuf_import
),
263 SUBTEST(igt_dmabuf_import_ownership
),
264 SUBTEST(igt_dmabuf_export_vmap
),
266 struct drm_i915_private
*i915
;
269 i915
= mock_gem_device();
273 err
= i915_subtests(tests
, i915
);
275 drm_dev_put(&i915
->drm
);
279 int i915_gem_dmabuf_live_selftests(struct drm_i915_private
*i915
)
281 static const struct i915_subtest tests
[] = {
282 SUBTEST(igt_dmabuf_export
),
285 return i915_subtests(tests
, i915
);