1 /*
2  * Copyright © 2016 Intel Corporation
3  *
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:
10  *
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.
14  *
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.
22  *
23  */
24 
25 #include "../i915_selftest.h"
26 
27 #include "mock_gem_device.h"
28 #include "mock_dmabuf.h"
29 
igt_dmabuf_export(void * arg)30 static int igt_dmabuf_export(void *arg)
31 {
32 	struct drm_i915_private *i915 = arg;
33 	struct drm_i915_gem_object *obj;
34 	struct dma_buf *dmabuf;
35 
36 	obj = i915_gem_object_create(i915, PAGE_SIZE);
37 	if (IS_ERR(obj))
38 		return PTR_ERR(obj);
39 
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);
46 	}
47 
48 	dma_buf_put(dmabuf);
49 	return 0;
50 }
51 
igt_dmabuf_import_self(void * arg)52 static int igt_dmabuf_import_self(void *arg)
53 {
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;
59 
60 	obj = i915_gem_object_create(i915, PAGE_SIZE);
61 	if (IS_ERR(obj))
62 		return PTR_ERR(obj);
63 
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;
70 	}
71 
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;
78 	}
79 
80 	if (import != &obj->base) {
81 		pr_err("i915_gem_prime_import created a new object!\n");
82 		err = -EINVAL;
83 		goto out_import;
84 	}
85 
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;
94 }
95 
igt_dmabuf_import(void * arg)96 static int igt_dmabuf_import(void *arg)
97 {
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;
104 
105 	dmabuf = mock_dmabuf(1);
106 	if (IS_ERR(dmabuf))
107 		return PTR_ERR(dmabuf);
108 
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;
115 	}
116 
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;
121 	}
122 
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;
128 	}
129 
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;
135 	}
136 
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;
143 		}
144 
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;
152 			}
153 		}
154 
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;
162 			}
163 		}
164 
165 		i915_gem_object_unpin_map(obj);
166 	}
167 
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;
176 }
177 
igt_dmabuf_import_ownership(void * arg)178 static int igt_dmabuf_import_ownership(void *arg)
179 {
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;
185 
186 	dmabuf = mock_dmabuf(1);
187 	if (IS_ERR(dmabuf))
188 		return PTR_ERR(dmabuf);
189 
190 	ptr = dma_buf_vmap(dmabuf);
191 	if (!ptr) {
192 		pr_err("dma_buf_vmap failed\n");
193 		err = -ENOMEM;
194 		goto err_dmabuf;
195 	}
196 
197 	memset(ptr, 0xc5, PAGE_SIZE);
198 	dma_buf_vunmap(dmabuf, ptr);
199 
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;
206 	}
207 
208 	dma_buf_put(dmabuf);
209 
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;
214 	}
215 
216 	err = 0;
217 	i915_gem_object_unpin_pages(obj);
218 out_obj:
219 	i915_gem_object_put(obj);
220 	return err;
221 
222 err_dmabuf:
223 	dma_buf_put(dmabuf);
224 	return err;
225 }
226 
igt_dmabuf_export_vmap(void * arg)227 static int igt_dmabuf_export_vmap(void *arg)
228 {
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;
234 
235 	obj = i915_gem_object_create(i915, PAGE_SIZE);
236 	if (IS_ERR(obj))
237 		return PTR_ERR(obj);
238 
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;
245 	}
246 	i915_gem_object_put(obj);
247 
248 	ptr = dma_buf_vmap(dmabuf);
249 	if (!ptr) {
250 		pr_err("dma_buf_vmap failed\n");
251 		err = -ENOMEM;
252 		goto out;
253 	}
254 
255 	if (memchr_inv(ptr, 0, dmabuf->size)) {
256 		pr_err("Exported object not initialiased to zero!\n");
257 		err = -EINVAL;
258 		goto out;
259 	}
260 
261 	memset(ptr, 0xc5, dmabuf->size);
262 
263 	err = 0;
264 	dma_buf_vunmap(dmabuf, ptr);
265 out:
266 	dma_buf_put(dmabuf);
267 	return err;
268 
269 err_obj:
270 	i915_gem_object_put(obj);
271 	return err;
272 }
273 
igt_dmabuf_export_kmap(void * arg)274 static int igt_dmabuf_export_kmap(void *arg)
275 {
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;
281 
282 	obj = i915_gem_object_create(i915, 2*PAGE_SIZE);
283 	if (IS_ERR(obj))
284 		return PTR_ERR(obj);
285 
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;
292 	}
293 
294 	ptr = dma_buf_kmap(dmabuf, 0);
295 	if (!ptr) {
296 		pr_err("dma_buf_kmap failed\n");
297 		err = -ENOMEM;
298 		goto err;
299 	}
300 
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;
306 	}
307 
308 	memset(ptr, 0xc5, PAGE_SIZE);
309 	dma_buf_kunmap(dmabuf, 0, ptr);
310 
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;
316 	}
317 	memset(ptr + PAGE_SIZE, 0xaa, PAGE_SIZE);
318 	i915_gem_object_unpin_map(obj);
319 
320 	ptr = dma_buf_kmap(dmabuf, 1);
321 	if (!ptr) {
322 		pr_err("dma_buf_kmap failed\n");
323 		err = -ENOMEM;
324 		goto err;
325 	}
326 
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;
332 	}
333 
334 	memset(ptr, 0xc5, PAGE_SIZE);
335 	dma_buf_kunmap(dmabuf, 1, ptr);
336 
337 	ptr = dma_buf_kmap(dmabuf, 0);
338 	if (!ptr) {
339 		pr_err("dma_buf_kmap failed\n");
340 		err = -ENOMEM;
341 		goto err;
342 	}
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;
348 	}
349 	dma_buf_kunmap(dmabuf, 0, ptr);
350 
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;
357 	}
358 
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;
365 	}
366 
367 	err = 0;
368 err:
369 	dma_buf_put(dmabuf);
370 	return err;
371 }
372 
i915_gem_dmabuf_mock_selftests(void)373 int i915_gem_dmabuf_mock_selftests(void)
374 {
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),
382 	};
383 	struct drm_i915_private *i915;
384 	int err;
385 
386 	i915 = mock_gem_device();
387 	if (!i915)
388 		return -ENOMEM;
389 
390 	err = i915_subtests(tests, i915);
391 
392 	drm_dev_put(&i915->drm);
393 	return err;
394 }
395 
i915_gem_dmabuf_live_selftests(struct drm_i915_private * i915)396 int i915_gem_dmabuf_live_selftests(struct drm_i915_private *i915)
397 {
398 	static const struct i915_subtest tests[] = {
399 		SUBTEST(igt_dmabuf_export),
400 	};
401 
402 	return i915_subtests(tests, i915);
403 }
404