2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "../i915_selftest.h"
27 #include "mock_gem_device.h"
28 #include "mock_dmabuf.h"
30 static int igt_dmabuf_export(void *arg
)
32 struct drm_i915_private
*i915
= arg
;
33 struct drm_i915_gem_object
*obj
;
34 struct dma_buf
*dmabuf
;
36 obj
= i915_gem_object_create(i915
, PAGE_SIZE
);
40 dmabuf
= i915_gem_prime_export(&i915
->drm
, &obj
->base
, 0);
41 i915_gem_object_put(obj
);
43 pr_err("i915_gem_prime_export failed with err=%d\n",
44 (int)PTR_ERR(dmabuf
));
45 return PTR_ERR(dmabuf
);
52 static int igt_dmabuf_import_self(void *arg
)
54 struct drm_i915_private
*i915
= arg
;
55 struct drm_i915_gem_object
*obj
;
56 struct drm_gem_object
*import
;
57 struct dma_buf
*dmabuf
;
60 obj
= i915_gem_object_create(i915
, PAGE_SIZE
);
64 dmabuf
= i915_gem_prime_export(&i915
->drm
, &obj
->base
, 0);
66 pr_err("i915_gem_prime_export failed with err=%d\n",
67 (int)PTR_ERR(dmabuf
));
68 err
= PTR_ERR(dmabuf
);
72 import
= i915_gem_prime_import(&i915
->drm
, dmabuf
);
74 pr_err("i915_gem_prime_import failed with err=%d\n",
75 (int)PTR_ERR(import
));
76 err
= PTR_ERR(import
);
80 if (import
!= &obj
->base
) {
81 pr_err("i915_gem_prime_import created a new object!\n");
88 i915_gem_object_put(to_intel_bo(import
));
92 i915_gem_object_put(obj
);
96 static int igt_dmabuf_import(void *arg
)
98 struct drm_i915_private
*i915
= arg
;
99 struct drm_i915_gem_object
*obj
;
100 struct dma_buf
*dmabuf
;
101 void *obj_map
, *dma_map
;
102 u32 pattern
[] = { 0, 0xaa, 0xcc, 0x55, 0xff };
105 dmabuf
= mock_dmabuf(1);
107 return PTR_ERR(dmabuf
);
109 obj
= to_intel_bo(i915_gem_prime_import(&i915
->drm
, dmabuf
));
111 pr_err("i915_gem_prime_import failed with err=%d\n",
117 if (obj
->base
.dev
!= &i915
->drm
) {
118 pr_err("i915_gem_prime_import created a non-i915 object!\n");
123 if (obj
->base
.size
!= PAGE_SIZE
) {
124 pr_err("i915_gem_prime_import is wrong size found %lld, expected %ld\n",
125 (long long)obj
->base
.size
, PAGE_SIZE
);
130 dma_map
= dma_buf_vmap(dmabuf
);
132 pr_err("dma_buf_vmap failed\n");
137 if (0) { /* Can not yet map dmabuf */
138 obj_map
= i915_gem_object_pin_map(obj
, I915_MAP_WB
);
139 if (IS_ERR(obj_map
)) {
140 err
= PTR_ERR(obj_map
);
141 pr_err("i915_gem_object_pin_map failed with err=%d\n", err
);
145 for (i
= 0; i
< ARRAY_SIZE(pattern
); i
++) {
146 memset(dma_map
, pattern
[i
], PAGE_SIZE
);
147 if (memchr_inv(obj_map
, pattern
[i
], PAGE_SIZE
)) {
149 pr_err("imported vmap not all set to %x!\n", pattern
[i
]);
150 i915_gem_object_unpin_map(obj
);
155 for (i
= 0; i
< ARRAY_SIZE(pattern
); i
++) {
156 memset(obj_map
, pattern
[i
], PAGE_SIZE
);
157 if (memchr_inv(dma_map
, pattern
[i
], PAGE_SIZE
)) {
159 pr_err("exported vmap not all set to %x!\n", pattern
[i
]);
160 i915_gem_object_unpin_map(obj
);
165 i915_gem_object_unpin_map(obj
);
170 dma_buf_vunmap(dmabuf
, dma_map
);
172 i915_gem_object_put(obj
);
178 static int igt_dmabuf_import_ownership(void *arg
)
180 struct drm_i915_private
*i915
= arg
;
181 struct drm_i915_gem_object
*obj
;
182 struct dma_buf
*dmabuf
;
186 dmabuf
= mock_dmabuf(1);
188 return PTR_ERR(dmabuf
);
190 ptr
= dma_buf_vmap(dmabuf
);
192 pr_err("dma_buf_vmap failed\n");
197 memset(ptr
, 0xc5, PAGE_SIZE
);
198 dma_buf_vunmap(dmabuf
, ptr
);
200 obj
= to_intel_bo(i915_gem_prime_import(&i915
->drm
, dmabuf
));
202 pr_err("i915_gem_prime_import failed with err=%d\n",
210 err
= i915_gem_object_pin_pages(obj
);
212 pr_err("i915_gem_object_pin_pages failed with err=%d\n", err
);
217 i915_gem_object_unpin_pages(obj
);
219 i915_gem_object_put(obj
);
227 static int igt_dmabuf_export_vmap(void *arg
)
229 struct drm_i915_private
*i915
= arg
;
230 struct drm_i915_gem_object
*obj
;
231 struct dma_buf
*dmabuf
;
235 obj
= i915_gem_object_create(i915
, PAGE_SIZE
);
239 dmabuf
= i915_gem_prime_export(&i915
->drm
, &obj
->base
, 0);
240 if (IS_ERR(dmabuf
)) {
241 pr_err("i915_gem_prime_export failed with err=%d\n",
242 (int)PTR_ERR(dmabuf
));
243 err
= PTR_ERR(dmabuf
);
246 i915_gem_object_put(obj
);
248 ptr
= dma_buf_vmap(dmabuf
);
250 pr_err("dma_buf_vmap failed\n");
255 if (memchr_inv(ptr
, 0, dmabuf
->size
)) {
256 pr_err("Exported object not initialiased to zero!\n");
261 memset(ptr
, 0xc5, dmabuf
->size
);
264 dma_buf_vunmap(dmabuf
, ptr
);
270 i915_gem_object_put(obj
);
274 static int igt_dmabuf_export_kmap(void *arg
)
276 struct drm_i915_private
*i915
= arg
;
277 struct drm_i915_gem_object
*obj
;
278 struct dma_buf
*dmabuf
;
282 obj
= i915_gem_object_create(i915
, 2*PAGE_SIZE
);
286 dmabuf
= i915_gem_prime_export(&i915
->drm
, &obj
->base
, 0);
287 i915_gem_object_put(obj
);
288 if (IS_ERR(dmabuf
)) {
289 err
= PTR_ERR(dmabuf
);
290 pr_err("i915_gem_prime_export failed with err=%d\n", err
);
294 ptr
= dma_buf_kmap(dmabuf
, 0);
296 pr_err("dma_buf_kmap failed\n");
301 if (memchr_inv(ptr
, 0, PAGE_SIZE
)) {
302 dma_buf_kunmap(dmabuf
, 0, ptr
);
303 pr_err("Exported page[0] not initialiased to zero!\n");
308 memset(ptr
, 0xc5, PAGE_SIZE
);
309 dma_buf_kunmap(dmabuf
, 0, ptr
);
311 ptr
= i915_gem_object_pin_map(obj
, I915_MAP_WB
);
314 pr_err("i915_gem_object_pin_map failed with err=%d\n", err
);
317 memset(ptr
+ PAGE_SIZE
, 0xaa, PAGE_SIZE
);
318 i915_gem_object_unpin_map(obj
);
320 ptr
= dma_buf_kmap(dmabuf
, 1);
322 pr_err("dma_buf_kmap failed\n");
327 if (memchr_inv(ptr
, 0xaa, PAGE_SIZE
)) {
328 dma_buf_kunmap(dmabuf
, 1, ptr
);
329 pr_err("Exported page[1] not set to 0xaa!\n");
334 memset(ptr
, 0xc5, PAGE_SIZE
);
335 dma_buf_kunmap(dmabuf
, 1, ptr
);
337 ptr
= dma_buf_kmap(dmabuf
, 0);
339 pr_err("dma_buf_kmap failed\n");
343 if (memchr_inv(ptr
, 0xc5, PAGE_SIZE
)) {
344 dma_buf_kunmap(dmabuf
, 0, ptr
);
345 pr_err("Exported page[0] did not retain 0xc5!\n");
349 dma_buf_kunmap(dmabuf
, 0, ptr
);
351 ptr
= dma_buf_kmap(dmabuf
, 2);
353 pr_err("Erroneously kmapped beyond the end of the object!\n");
354 dma_buf_kunmap(dmabuf
, 2, ptr
);
359 ptr
= dma_buf_kmap(dmabuf
, -1);
361 pr_err("Erroneously kmapped before the start of the object!\n");
362 dma_buf_kunmap(dmabuf
, -1, ptr
);
373 int i915_gem_dmabuf_mock_selftests(void)
375 static const struct i915_subtest tests
[] = {
376 SUBTEST(igt_dmabuf_export
),
377 SUBTEST(igt_dmabuf_import_self
),
378 SUBTEST(igt_dmabuf_import
),
379 SUBTEST(igt_dmabuf_import_ownership
),
380 SUBTEST(igt_dmabuf_export_vmap
),
381 SUBTEST(igt_dmabuf_export_kmap
),
383 struct drm_i915_private
*i915
;
386 i915
= mock_gem_device();
390 err
= i915_subtests(tests
, i915
);
392 drm_dev_unref(&i915
->drm
);
396 int i915_gem_dmabuf_live_selftests(struct drm_i915_private
*i915
)
398 static const struct i915_subtest tests
[] = {
399 SUBTEST(igt_dmabuf_export
),
402 return i915_subtests(tests
, i915
);