Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / i915 / selftests / i915_gem_dmabuf.c
blob89dc25a5a53b87182967d9603a842d5aa2300c3e
1 /*
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
13 * Software.
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
21 * IN THE SOFTWARE.
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);
37 if (IS_ERR(obj))
38 return PTR_ERR(obj);
40 dmabuf = i915_gem_prime_export(&i915->drm, &obj->base, 0);
41 i915_gem_object_put(obj);
42 if (IS_ERR(dmabuf)) {
43 pr_err("i915_gem_prime_export failed with err=%d\n",
44 (int)PTR_ERR(dmabuf));
45 return PTR_ERR(dmabuf);
48 dma_buf_put(dmabuf);
49 return 0;
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;
58 int err;
60 obj = i915_gem_object_create(i915, PAGE_SIZE);
61 if (IS_ERR(obj))
62 return PTR_ERR(obj);
64 dmabuf = i915_gem_prime_export(&i915->drm, &obj->base, 0);
65 if (IS_ERR(dmabuf)) {
66 pr_err("i915_gem_prime_export failed with err=%d\n",
67 (int)PTR_ERR(dmabuf));
68 err = PTR_ERR(dmabuf);
69 goto out;
72 import = i915_gem_prime_import(&i915->drm, dmabuf);
73 if (IS_ERR(import)) {
74 pr_err("i915_gem_prime_import failed with err=%d\n",
75 (int)PTR_ERR(import));
76 err = PTR_ERR(import);
77 goto out_dmabuf;
80 if (import != &obj->base) {
81 pr_err("i915_gem_prime_import created a new object!\n");
82 err = -EINVAL;
83 goto out_import;
86 err = 0;
87 out_import:
88 i915_gem_object_put(to_intel_bo(import));
89 out_dmabuf:
90 dma_buf_put(dmabuf);
91 out:
92 i915_gem_object_put(obj);
93 return err;
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 };
103 int err, i;
105 dmabuf = mock_dmabuf(1);
106 if (IS_ERR(dmabuf))
107 return PTR_ERR(dmabuf);
109 obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf));
110 if (IS_ERR(obj)) {
111 pr_err("i915_gem_prime_import failed with err=%d\n",
112 (int)PTR_ERR(obj));
113 err = PTR_ERR(obj);
114 goto out_dmabuf;
117 if (obj->base.dev != &i915->drm) {
118 pr_err("i915_gem_prime_import created a non-i915 object!\n");
119 err = -EINVAL;
120 goto out_obj;
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);
126 err = -EINVAL;
127 goto out_obj;
130 dma_map = dma_buf_vmap(dmabuf);
131 if (!dma_map) {
132 pr_err("dma_buf_vmap failed\n");
133 err = -ENOMEM;
134 goto out_obj;
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);
142 goto out_dma_map;
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)) {
148 err = -EINVAL;
149 pr_err("imported vmap not all set to %x!\n", pattern[i]);
150 i915_gem_object_unpin_map(obj);
151 goto out_dma_map;
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)) {
158 err = -EINVAL;
159 pr_err("exported vmap not all set to %x!\n", pattern[i]);
160 i915_gem_object_unpin_map(obj);
161 goto out_dma_map;
165 i915_gem_object_unpin_map(obj);
168 err = 0;
169 out_dma_map:
170 dma_buf_vunmap(dmabuf, dma_map);
171 out_obj:
172 i915_gem_object_put(obj);
173 out_dmabuf:
174 dma_buf_put(dmabuf);
175 return err;
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;
183 void *ptr;
184 int err;
186 dmabuf = mock_dmabuf(1);
187 if (IS_ERR(dmabuf))
188 return PTR_ERR(dmabuf);
190 ptr = dma_buf_vmap(dmabuf);
191 if (!ptr) {
192 pr_err("dma_buf_vmap failed\n");
193 err = -ENOMEM;
194 goto err_dmabuf;
197 memset(ptr, 0xc5, PAGE_SIZE);
198 dma_buf_vunmap(dmabuf, ptr);
200 obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf));
201 if (IS_ERR(obj)) {
202 pr_err("i915_gem_prime_import failed with err=%d\n",
203 (int)PTR_ERR(obj));
204 err = PTR_ERR(obj);
205 goto err_dmabuf;
208 dma_buf_put(dmabuf);
210 err = i915_gem_object_pin_pages(obj);
211 if (err) {
212 pr_err("i915_gem_object_pin_pages failed with err=%d\n", err);
213 goto out_obj;
216 err = 0;
217 i915_gem_object_unpin_pages(obj);
218 out_obj:
219 i915_gem_object_put(obj);
220 return err;
222 err_dmabuf:
223 dma_buf_put(dmabuf);
224 return err;
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;
232 void *ptr;
233 int err;
235 obj = i915_gem_object_create(i915, PAGE_SIZE);
236 if (IS_ERR(obj))
237 return PTR_ERR(obj);
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);
244 goto err_obj;
246 i915_gem_object_put(obj);
248 ptr = dma_buf_vmap(dmabuf);
249 if (!ptr) {
250 pr_err("dma_buf_vmap failed\n");
251 err = -ENOMEM;
252 goto out;
255 if (memchr_inv(ptr, 0, dmabuf->size)) {
256 pr_err("Exported object not initialiased to zero!\n");
257 err = -EINVAL;
258 goto out;
261 memset(ptr, 0xc5, dmabuf->size);
263 err = 0;
264 dma_buf_vunmap(dmabuf, ptr);
265 out:
266 dma_buf_put(dmabuf);
267 return err;
269 err_obj:
270 i915_gem_object_put(obj);
271 return err;
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;
279 void *ptr;
280 int err;
282 obj = i915_gem_object_create(i915, 2*PAGE_SIZE);
283 if (IS_ERR(obj))
284 return PTR_ERR(obj);
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);
291 return err;
294 ptr = dma_buf_kmap(dmabuf, 0);
295 if (!ptr) {
296 pr_err("dma_buf_kmap failed\n");
297 err = -ENOMEM;
298 goto err;
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");
304 err = -EINVAL;
305 goto err;
308 memset(ptr, 0xc5, PAGE_SIZE);
309 dma_buf_kunmap(dmabuf, 0, ptr);
311 ptr = i915_gem_object_pin_map(obj, I915_MAP_WB);
312 if (IS_ERR(ptr)) {
313 err = PTR_ERR(ptr);
314 pr_err("i915_gem_object_pin_map failed with err=%d\n", err);
315 goto err;
317 memset(ptr + PAGE_SIZE, 0xaa, PAGE_SIZE);
318 i915_gem_object_unpin_map(obj);
320 ptr = dma_buf_kmap(dmabuf, 1);
321 if (!ptr) {
322 pr_err("dma_buf_kmap failed\n");
323 err = -ENOMEM;
324 goto err;
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");
330 err = -EINVAL;
331 goto err;
334 memset(ptr, 0xc5, PAGE_SIZE);
335 dma_buf_kunmap(dmabuf, 1, ptr);
337 ptr = dma_buf_kmap(dmabuf, 0);
338 if (!ptr) {
339 pr_err("dma_buf_kmap failed\n");
340 err = -ENOMEM;
341 goto err;
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");
346 err = -EINVAL;
347 goto err;
349 dma_buf_kunmap(dmabuf, 0, ptr);
351 ptr = dma_buf_kmap(dmabuf, 2);
352 if (ptr) {
353 pr_err("Erroneously kmapped beyond the end of the object!\n");
354 dma_buf_kunmap(dmabuf, 2, ptr);
355 err = -EINVAL;
356 goto err;
359 ptr = dma_buf_kmap(dmabuf, -1);
360 if (ptr) {
361 pr_err("Erroneously kmapped before the start of the object!\n");
362 dma_buf_kunmap(dmabuf, -1, ptr);
363 err = -EINVAL;
364 goto err;
367 err = 0;
368 err:
369 dma_buf_put(dmabuf);
370 return err;
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;
384 int err;
386 i915 = mock_gem_device();
387 if (!i915)
388 return -ENOMEM;
390 err = i915_subtests(tests, i915);
392 drm_dev_unref(&i915->drm);
393 return err;
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);