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 <linux/prime_numbers.h>
26 
27 #include "gem/selftests/mock_context.h"
28 
29 #include "i915_scatterlist.h"
30 #include "i915_selftest.h"
31 
32 #include "mock_gem_device.h"
33 #include "mock_gtt.h"
34 
35 static bool assert_vma(struct i915_vma *vma,
36 		       struct drm_i915_gem_object *obj,
37 		       struct i915_gem_context *ctx)
38 {
39 	bool ok = true;
40 
41 	if (vma->vm != ctx->vm) {
42 		pr_err("VMA created with wrong VM\n");
43 		ok = false;
44 	}
45 
46 	if (vma->size != obj->base.size) {
47 		pr_err("VMA created with wrong size, found %llu, expected %zu\n",
48 		       vma->size, obj->base.size);
49 		ok = false;
50 	}
51 
52 	if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) {
53 		pr_err("VMA created with wrong type [%d]\n",
54 		       vma->ggtt_view.type);
55 		ok = false;
56 	}
57 
58 	return ok;
59 }
60 
61 static struct i915_vma *
62 checked_vma_instance(struct drm_i915_gem_object *obj,
63 		     struct i915_address_space *vm,
64 		     const struct i915_ggtt_view *view)
65 {
66 	struct i915_vma *vma;
67 	bool ok = true;
68 
69 	vma = i915_vma_instance(obj, vm, view);
70 	if (IS_ERR(vma))
71 		return vma;
72 
73 	/* Manual checks, will be reinforced by i915_vma_compare! */
74 	if (vma->vm != vm) {
75 		pr_err("VMA's vm [%p] does not match request [%p]\n",
76 		       vma->vm, vm);
77 		ok = false;
78 	}
79 
80 	if (i915_is_ggtt(vm) != i915_vma_is_ggtt(vma)) {
81 		pr_err("VMA ggtt status [%d] does not match parent [%d]\n",
82 		       i915_vma_is_ggtt(vma), i915_is_ggtt(vm));
83 		ok = false;
84 	}
85 
86 	if (i915_vma_compare(vma, vm, view)) {
87 		pr_err("i915_vma_compare failed with create parameters!\n");
88 		return ERR_PTR(-EINVAL);
89 	}
90 
91 	if (i915_vma_compare(vma, vma->vm,
92 			     i915_vma_is_ggtt(vma) ? &vma->ggtt_view : NULL)) {
93 		pr_err("i915_vma_compare failed with itself\n");
94 		return ERR_PTR(-EINVAL);
95 	}
96 
97 	if (!ok) {
98 		pr_err("i915_vma_compare failed to detect the difference!\n");
99 		return ERR_PTR(-EINVAL);
100 	}
101 
102 	return vma;
103 }
104 
105 static int create_vmas(struct drm_i915_private *i915,
106 		       struct list_head *objects,
107 		       struct list_head *contexts)
108 {
109 	struct drm_i915_gem_object *obj;
110 	struct i915_gem_context *ctx;
111 	int pinned;
112 
113 	list_for_each_entry(obj, objects, st_link) {
114 		for (pinned = 0; pinned <= 1; pinned++) {
115 			list_for_each_entry(ctx, contexts, link) {
116 				struct i915_address_space *vm = ctx->vm;
117 				struct i915_vma *vma;
118 				int err;
119 
120 				vma = checked_vma_instance(obj, vm, NULL);
121 				if (IS_ERR(vma))
122 					return PTR_ERR(vma);
123 
124 				if (!assert_vma(vma, obj, ctx)) {
125 					pr_err("VMA lookup/create failed\n");
126 					return -EINVAL;
127 				}
128 
129 				if (!pinned) {
130 					err = i915_vma_pin(vma, 0, 0, PIN_USER);
131 					if (err) {
132 						pr_err("Failed to pin VMA\n");
133 						return err;
134 					}
135 				} else {
136 					i915_vma_unpin(vma);
137 				}
138 			}
139 		}
140 	}
141 
142 	return 0;
143 }
144 
145 static int igt_vma_create(void *arg)
146 {
147 	struct i915_ggtt *ggtt = arg;
148 	struct drm_i915_private *i915 = ggtt->vm.i915;
149 	struct drm_i915_gem_object *obj, *on;
150 	struct i915_gem_context *ctx, *cn;
151 	unsigned long num_obj, num_ctx;
152 	unsigned long no, nc;
153 	IGT_TIMEOUT(end_time);
154 	LIST_HEAD(contexts);
155 	LIST_HEAD(objects);
156 	int err = -ENOMEM;
157 
158 	/* Exercise creating many vma amonst many objections, checking the
159 	 * vma creation and lookup routines.
160 	 */
161 
162 	no = 0;
163 	for_each_prime_number(num_obj, ULONG_MAX - 1) {
164 		for (; no < num_obj; no++) {
165 			obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
166 			if (IS_ERR(obj))
167 				goto out;
168 
169 			list_add(&obj->st_link, &objects);
170 		}
171 
172 		nc = 0;
173 		for_each_prime_number(num_ctx, MAX_CONTEXT_HW_ID) {
174 			for (; nc < num_ctx; nc++) {
175 				ctx = mock_context(i915, "mock");
176 				if (!ctx)
177 					goto out;
178 
179 				list_move(&ctx->link, &contexts);
180 			}
181 
182 			err = create_vmas(i915, &objects, &contexts);
183 			if (err)
184 				goto out;
185 
186 			if (igt_timeout(end_time,
187 					"%s timed out: after %lu objects in %lu contexts\n",
188 					__func__, no, nc))
189 				goto end;
190 		}
191 
192 		list_for_each_entry_safe(ctx, cn, &contexts, link) {
193 			list_del_init(&ctx->link);
194 			mock_context_close(ctx);
195 		}
196 
197 		cond_resched();
198 	}
199 
200 end:
201 	/* Final pass to lookup all created contexts */
202 	err = create_vmas(i915, &objects, &contexts);
203 out:
204 	list_for_each_entry_safe(ctx, cn, &contexts, link) {
205 		list_del_init(&ctx->link);
206 		mock_context_close(ctx);
207 	}
208 
209 	list_for_each_entry_safe(obj, on, &objects, st_link)
210 		i915_gem_object_put(obj);
211 	return err;
212 }
213 
214 struct pin_mode {
215 	u64 size;
216 	u64 flags;
217 	bool (*assert)(const struct i915_vma *,
218 		       const struct pin_mode *mode,
219 		       int result);
220 	const char *string;
221 };
222 
223 static bool assert_pin_valid(const struct i915_vma *vma,
224 			     const struct pin_mode *mode,
225 			     int result)
226 {
227 	if (result)
228 		return false;
229 
230 	if (i915_vma_misplaced(vma, mode->size, 0, mode->flags))
231 		return false;
232 
233 	return true;
234 }
235 
236 __maybe_unused
237 static bool assert_pin_enospc(const struct i915_vma *vma,
238 			      const struct pin_mode *mode,
239 			      int result)
240 {
241 	return result == -ENOSPC;
242 }
243 
244 __maybe_unused
245 static bool assert_pin_einval(const struct i915_vma *vma,
246 			      const struct pin_mode *mode,
247 			      int result)
248 {
249 	return result == -EINVAL;
250 }
251 
252 static int igt_vma_pin1(void *arg)
253 {
254 	struct i915_ggtt *ggtt = arg;
255 	const struct pin_mode modes[] = {
256 #define VALID(sz, fl) { .size = (sz), .flags = (fl), .assert = assert_pin_valid, .string = #sz ", " #fl ", (valid) " }
257 #define __INVALID(sz, fl, check, eval) { .size = (sz), .flags = (fl), .assert = (check), .string = #sz ", " #fl ", (invalid " #eval ")" }
258 #define INVALID(sz, fl) __INVALID(sz, fl, assert_pin_einval, EINVAL)
259 #define NOSPACE(sz, fl) __INVALID(sz, fl, assert_pin_enospc, ENOSPC)
260 		VALID(0, PIN_GLOBAL),
261 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE),
262 
263 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 4096),
264 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 8192),
265 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
266 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
267 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)),
268 
269 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)),
270 		INVALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | ggtt->mappable_end),
271 		VALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)),
272 		INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | ggtt->vm.total),
273 		INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | round_down(U64_MAX, PAGE_SIZE)),
274 
275 		VALID(4096, PIN_GLOBAL),
276 		VALID(8192, PIN_GLOBAL),
277 		VALID(ggtt->mappable_end - 4096, PIN_GLOBAL | PIN_MAPPABLE),
278 		VALID(ggtt->mappable_end, PIN_GLOBAL | PIN_MAPPABLE),
279 		NOSPACE(ggtt->mappable_end + 4096, PIN_GLOBAL | PIN_MAPPABLE),
280 		VALID(ggtt->vm.total - 4096, PIN_GLOBAL),
281 		VALID(ggtt->vm.total, PIN_GLOBAL),
282 		NOSPACE(ggtt->vm.total + 4096, PIN_GLOBAL),
283 		NOSPACE(round_down(U64_MAX, PAGE_SIZE), PIN_GLOBAL),
284 		INVALID(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)),
285 		INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)),
286 		INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (round_down(U64_MAX, PAGE_SIZE) - 4096)),
287 
288 		VALID(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
289 
290 #if !IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
291 		/* Misusing BIAS is a programming error (it is not controllable
292 		 * from userspace) so when debugging is enabled, it explodes.
293 		 * However, the tests are still quite interesting for checking
294 		 * variable start, end and size.
295 		 */
296 		NOSPACE(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | ggtt->mappable_end),
297 		NOSPACE(0, PIN_GLOBAL | PIN_OFFSET_BIAS | ggtt->vm.total),
298 		NOSPACE(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
299 		NOSPACE(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)),
300 #endif
301 		{ },
302 #undef NOSPACE
303 #undef INVALID
304 #undef __INVALID
305 #undef VALID
306 	}, *m;
307 	struct drm_i915_gem_object *obj;
308 	struct i915_vma *vma;
309 	int err = -EINVAL;
310 
311 	/* Exercise all the weird and wonderful i915_vma_pin requests,
312 	 * focusing on error handling of boundary conditions.
313 	 */
314 
315 	GEM_BUG_ON(!drm_mm_clean(&ggtt->vm.mm));
316 
317 	obj = i915_gem_object_create_internal(ggtt->vm.i915, PAGE_SIZE);
318 	if (IS_ERR(obj))
319 		return PTR_ERR(obj);
320 
321 	vma = checked_vma_instance(obj, &ggtt->vm, NULL);
322 	if (IS_ERR(vma))
323 		goto out;
324 
325 	for (m = modes; m->assert; m++) {
326 		err = i915_vma_pin(vma, m->size, 0, m->flags);
327 		if (!m->assert(vma, m, err)) {
328 			pr_err("%s to pin single page into GGTT with mode[%d:%s]: size=%llx flags=%llx, err=%d\n",
329 			       m->assert == assert_pin_valid ? "Failed" : "Unexpectedly succeeded",
330 			       (int)(m - modes), m->string, m->size, m->flags,
331 			       err);
332 			if (!err)
333 				i915_vma_unpin(vma);
334 			err = -EINVAL;
335 			goto out;
336 		}
337 
338 		if (!err) {
339 			i915_vma_unpin(vma);
340 			err = i915_vma_unbind(vma);
341 			if (err) {
342 				pr_err("Failed to unbind single page from GGTT, err=%d\n", err);
343 				goto out;
344 			}
345 		}
346 
347 		cond_resched();
348 	}
349 
350 	err = 0;
351 out:
352 	i915_gem_object_put(obj);
353 	return err;
354 }
355 
356 static unsigned long rotated_index(const struct intel_rotation_info *r,
357 				   unsigned int n,
358 				   unsigned int x,
359 				   unsigned int y)
360 {
361 	return (r->plane[n].stride * (r->plane[n].height - y - 1) +
362 		r->plane[n].offset + x);
363 }
364 
365 static struct scatterlist *
366 assert_rotated(struct drm_i915_gem_object *obj,
367 	       const struct intel_rotation_info *r, unsigned int n,
368 	       struct scatterlist *sg)
369 {
370 	unsigned int x, y;
371 
372 	for (x = 0; x < r->plane[n].width; x++) {
373 		for (y = 0; y < r->plane[n].height; y++) {
374 			unsigned long src_idx;
375 			dma_addr_t src;
376 
377 			if (!sg) {
378 				pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
379 				       n, x, y);
380 				return ERR_PTR(-EINVAL);
381 			}
382 
383 			src_idx = rotated_index(r, n, x, y);
384 			src = i915_gem_object_get_dma_address(obj, src_idx);
385 
386 			if (sg_dma_len(sg) != PAGE_SIZE) {
387 				pr_err("Invalid sg.length, found %d, expected %lu for rotated page (%d, %d) [src index %lu]\n",
388 				       sg_dma_len(sg), PAGE_SIZE,
389 				       x, y, src_idx);
390 				return ERR_PTR(-EINVAL);
391 			}
392 
393 			if (sg_dma_address(sg) != src) {
394 				pr_err("Invalid address for rotated page (%d, %d) [src index %lu]\n",
395 				       x, y, src_idx);
396 				return ERR_PTR(-EINVAL);
397 			}
398 
399 			sg = sg_next(sg);
400 		}
401 	}
402 
403 	return sg;
404 }
405 
406 static unsigned long remapped_index(const struct intel_remapped_info *r,
407 				    unsigned int n,
408 				    unsigned int x,
409 				    unsigned int y)
410 {
411 	return (r->plane[n].stride * y +
412 		r->plane[n].offset + x);
413 }
414 
415 static struct scatterlist *
416 assert_remapped(struct drm_i915_gem_object *obj,
417 		const struct intel_remapped_info *r, unsigned int n,
418 		struct scatterlist *sg)
419 {
420 	unsigned int x, y;
421 	unsigned int left = 0;
422 	unsigned int offset;
423 
424 	for (y = 0; y < r->plane[n].height; y++) {
425 		for (x = 0; x < r->plane[n].width; x++) {
426 			unsigned long src_idx;
427 			dma_addr_t src;
428 
429 			if (!sg) {
430 				pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
431 				       n, x, y);
432 				return ERR_PTR(-EINVAL);
433 			}
434 			if (!left) {
435 				offset = 0;
436 				left = sg_dma_len(sg);
437 			}
438 
439 			src_idx = remapped_index(r, n, x, y);
440 			src = i915_gem_object_get_dma_address(obj, src_idx);
441 
442 			if (left < PAGE_SIZE || left & (PAGE_SIZE-1)) {
443 				pr_err("Invalid sg.length, found %d, expected %lu for remapped page (%d, %d) [src index %lu]\n",
444 				       sg_dma_len(sg), PAGE_SIZE,
445 				       x, y, src_idx);
446 				return ERR_PTR(-EINVAL);
447 			}
448 
449 			if (sg_dma_address(sg) + offset != src) {
450 				pr_err("Invalid address for remapped page (%d, %d) [src index %lu]\n",
451 				       x, y, src_idx);
452 				return ERR_PTR(-EINVAL);
453 			}
454 
455 			left -= PAGE_SIZE;
456 			offset += PAGE_SIZE;
457 
458 
459 			if (!left)
460 				sg = sg_next(sg);
461 		}
462 	}
463 
464 	return sg;
465 }
466 
467 static unsigned int rotated_size(const struct intel_remapped_plane_info *a,
468 				 const struct intel_remapped_plane_info *b)
469 {
470 	return a->width * a->height + b->width * b->height;
471 }
472 
473 static int igt_vma_rotate_remap(void *arg)
474 {
475 	struct i915_ggtt *ggtt = arg;
476 	struct i915_address_space *vm = &ggtt->vm;
477 	struct drm_i915_gem_object *obj;
478 	const struct intel_remapped_plane_info planes[] = {
479 		{ .width = 1, .height = 1, .stride = 1 },
480 		{ .width = 2, .height = 2, .stride = 2 },
481 		{ .width = 4, .height = 4, .stride = 4 },
482 		{ .width = 8, .height = 8, .stride = 8 },
483 
484 		{ .width = 3, .height = 5, .stride = 3 },
485 		{ .width = 3, .height = 5, .stride = 4 },
486 		{ .width = 3, .height = 5, .stride = 5 },
487 
488 		{ .width = 5, .height = 3, .stride = 5 },
489 		{ .width = 5, .height = 3, .stride = 7 },
490 		{ .width = 5, .height = 3, .stride = 9 },
491 
492 		{ .width = 4, .height = 6, .stride = 6 },
493 		{ .width = 6, .height = 4, .stride = 6 },
494 		{ }
495 	}, *a, *b;
496 	enum i915_ggtt_view_type types[] = {
497 		I915_GGTT_VIEW_ROTATED,
498 		I915_GGTT_VIEW_REMAPPED,
499 		0,
500 	}, *t;
501 	const unsigned int max_pages = 64;
502 	int err = -ENOMEM;
503 
504 	/* Create VMA for many different combinations of planes and check
505 	 * that the page layout within the rotated VMA match our expectations.
506 	 */
507 
508 	obj = i915_gem_object_create_internal(vm->i915, max_pages * PAGE_SIZE);
509 	if (IS_ERR(obj))
510 		goto out;
511 
512 	for (t = types; *t; t++) {
513 	for (a = planes; a->width; a++) {
514 		for (b = planes + ARRAY_SIZE(planes); b-- != planes; ) {
515 			struct i915_ggtt_view view;
516 			unsigned int n, max_offset;
517 
518 			max_offset = max(a->stride * a->height,
519 					 b->stride * b->height);
520 			GEM_BUG_ON(max_offset > max_pages);
521 			max_offset = max_pages - max_offset;
522 
523 			view.type = *t;
524 			view.rotated.plane[0] = *a;
525 			view.rotated.plane[1] = *b;
526 
527 			for_each_prime_number_from(view.rotated.plane[0].offset, 0, max_offset) {
528 				for_each_prime_number_from(view.rotated.plane[1].offset, 0, max_offset) {
529 					struct scatterlist *sg;
530 					struct i915_vma *vma;
531 
532 					vma = checked_vma_instance(obj, vm, &view);
533 					if (IS_ERR(vma)) {
534 						err = PTR_ERR(vma);
535 						goto out_object;
536 					}
537 
538 					err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
539 					if (err) {
540 						pr_err("Failed to pin VMA, err=%d\n", err);
541 						goto out_object;
542 					}
543 
544 					if (view.type == I915_GGTT_VIEW_ROTATED &&
545 					    vma->size != rotated_size(a, b) * PAGE_SIZE) {
546 						pr_err("VMA is wrong size, expected %lu, found %llu\n",
547 						       PAGE_SIZE * rotated_size(a, b), vma->size);
548 						err = -EINVAL;
549 						goto out_object;
550 					}
551 
552 					if (view.type == I915_GGTT_VIEW_REMAPPED &&
553 					    vma->size > rotated_size(a, b) * PAGE_SIZE) {
554 						pr_err("VMA is wrong size, expected %lu, found %llu\n",
555 						       PAGE_SIZE * rotated_size(a, b), vma->size);
556 						err = -EINVAL;
557 						goto out_object;
558 					}
559 
560 					if (vma->pages->nents > rotated_size(a, b)) {
561 						pr_err("sg table is wrong sizeo, expected %u, found %u nents\n",
562 						       rotated_size(a, b), vma->pages->nents);
563 						err = -EINVAL;
564 						goto out_object;
565 					}
566 
567 					if (vma->node.size < vma->size) {
568 						pr_err("VMA binding too small, expected %llu, found %llu\n",
569 						       vma->size, vma->node.size);
570 						err = -EINVAL;
571 						goto out_object;
572 					}
573 
574 					if (vma->pages == obj->mm.pages) {
575 						pr_err("VMA using unrotated object pages!\n");
576 						err = -EINVAL;
577 						goto out_object;
578 					}
579 
580 					sg = vma->pages->sgl;
581 					for (n = 0; n < ARRAY_SIZE(view.rotated.plane); n++) {
582 						if (view.type == I915_GGTT_VIEW_ROTATED)
583 							sg = assert_rotated(obj, &view.rotated, n, sg);
584 						else
585 							sg = assert_remapped(obj, &view.remapped, n, sg);
586 						if (IS_ERR(sg)) {
587 							pr_err("Inconsistent %s VMA pages for plane %d: [(%d, %d, %d, %d), (%d, %d, %d, %d)]\n",
588 							       view.type == I915_GGTT_VIEW_ROTATED ?
589 							       "rotated" : "remapped", n,
590 							       view.rotated.plane[0].width,
591 							       view.rotated.plane[0].height,
592 							       view.rotated.plane[0].stride,
593 							       view.rotated.plane[0].offset,
594 							       view.rotated.plane[1].width,
595 							       view.rotated.plane[1].height,
596 							       view.rotated.plane[1].stride,
597 							       view.rotated.plane[1].offset);
598 							err = -EINVAL;
599 							goto out_object;
600 						}
601 					}
602 
603 					i915_vma_unpin(vma);
604 
605 					cond_resched();
606 				}
607 			}
608 		}
609 	}
610 	}
611 
612 out_object:
613 	i915_gem_object_put(obj);
614 out:
615 	return err;
616 }
617 
618 static bool assert_partial(struct drm_i915_gem_object *obj,
619 			   struct i915_vma *vma,
620 			   unsigned long offset,
621 			   unsigned long size)
622 {
623 	struct sgt_iter sgt;
624 	dma_addr_t dma;
625 
626 	for_each_sgt_dma(dma, sgt, vma->pages) {
627 		dma_addr_t src;
628 
629 		if (!size) {
630 			pr_err("Partial scattergather list too long\n");
631 			return false;
632 		}
633 
634 		src = i915_gem_object_get_dma_address(obj, offset);
635 		if (src != dma) {
636 			pr_err("DMA mismatch for partial page offset %lu\n",
637 			       offset);
638 			return false;
639 		}
640 
641 		offset++;
642 		size--;
643 	}
644 
645 	return true;
646 }
647 
648 static bool assert_pin(struct i915_vma *vma,
649 		       struct i915_ggtt_view *view,
650 		       u64 size,
651 		       const char *name)
652 {
653 	bool ok = true;
654 
655 	if (vma->size != size) {
656 		pr_err("(%s) VMA is wrong size, expected %llu, found %llu\n",
657 		       name, size, vma->size);
658 		ok = false;
659 	}
660 
661 	if (vma->node.size < vma->size) {
662 		pr_err("(%s) VMA binding too small, expected %llu, found %llu\n",
663 		       name, vma->size, vma->node.size);
664 		ok = false;
665 	}
666 
667 	if (view && view->type != I915_GGTT_VIEW_NORMAL) {
668 		if (memcmp(&vma->ggtt_view, view, sizeof(*view))) {
669 			pr_err("(%s) VMA mismatch upon creation!\n",
670 			       name);
671 			ok = false;
672 		}
673 
674 		if (vma->pages == vma->obj->mm.pages) {
675 			pr_err("(%s) VMA using original object pages!\n",
676 			       name);
677 			ok = false;
678 		}
679 	} else {
680 		if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) {
681 			pr_err("Not the normal ggtt view! Found %d\n",
682 			       vma->ggtt_view.type);
683 			ok = false;
684 		}
685 
686 		if (vma->pages != vma->obj->mm.pages) {
687 			pr_err("VMA not using object pages!\n");
688 			ok = false;
689 		}
690 	}
691 
692 	return ok;
693 }
694 
695 static int igt_vma_partial(void *arg)
696 {
697 	struct i915_ggtt *ggtt = arg;
698 	struct i915_address_space *vm = &ggtt->vm;
699 	const unsigned int npages = 1021; /* prime! */
700 	struct drm_i915_gem_object *obj;
701 	const struct phase {
702 		const char *name;
703 	} phases[] = {
704 		{ "create" },
705 		{ "lookup" },
706 		{ },
707 	}, *p;
708 	unsigned int sz, offset;
709 	struct i915_vma *vma;
710 	int err = -ENOMEM;
711 
712 	/* Create lots of different VMA for the object and check that
713 	 * we are returned the same VMA when we later request the same range.
714 	 */
715 
716 	obj = i915_gem_object_create_internal(vm->i915, npages * PAGE_SIZE);
717 	if (IS_ERR(obj))
718 		goto out;
719 
720 	for (p = phases; p->name; p++) { /* exercise both create/lookup */
721 		unsigned int count, nvma;
722 
723 		nvma = 0;
724 		for_each_prime_number_from(sz, 1, npages) {
725 			for_each_prime_number_from(offset, 0, npages - sz) {
726 				struct i915_ggtt_view view;
727 
728 				view.type = I915_GGTT_VIEW_PARTIAL;
729 				view.partial.offset = offset;
730 				view.partial.size = sz;
731 
732 				if (sz == npages)
733 					view.type = I915_GGTT_VIEW_NORMAL;
734 
735 				vma = checked_vma_instance(obj, vm, &view);
736 				if (IS_ERR(vma)) {
737 					err = PTR_ERR(vma);
738 					goto out_object;
739 				}
740 
741 				err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
742 				if (err)
743 					goto out_object;
744 
745 				if (!assert_pin(vma, &view, sz*PAGE_SIZE, p->name)) {
746 					pr_err("(%s) Inconsistent partial pinning for (offset=%d, size=%d)\n",
747 					       p->name, offset, sz);
748 					err = -EINVAL;
749 					goto out_object;
750 				}
751 
752 				if (!assert_partial(obj, vma, offset, sz)) {
753 					pr_err("(%s) Inconsistent partial pages for (offset=%d, size=%d)\n",
754 					       p->name, offset, sz);
755 					err = -EINVAL;
756 					goto out_object;
757 				}
758 
759 				i915_vma_unpin(vma);
760 				nvma++;
761 
762 				cond_resched();
763 			}
764 		}
765 
766 		count = 0;
767 		list_for_each_entry(vma, &obj->vma.list, obj_link)
768 			count++;
769 		if (count != nvma) {
770 			pr_err("(%s) All partial vma were not recorded on the obj->vma_list: found %u, expected %u\n",
771 			       p->name, count, nvma);
772 			err = -EINVAL;
773 			goto out_object;
774 		}
775 
776 		/* Check that we did create the whole object mapping */
777 		vma = checked_vma_instance(obj, vm, NULL);
778 		if (IS_ERR(vma)) {
779 			err = PTR_ERR(vma);
780 			goto out_object;
781 		}
782 
783 		err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
784 		if (err)
785 			goto out_object;
786 
787 		if (!assert_pin(vma, NULL, obj->base.size, p->name)) {
788 			pr_err("(%s) inconsistent full pin\n", p->name);
789 			err = -EINVAL;
790 			goto out_object;
791 		}
792 
793 		i915_vma_unpin(vma);
794 
795 		count = 0;
796 		list_for_each_entry(vma, &obj->vma.list, obj_link)
797 			count++;
798 		if (count != nvma) {
799 			pr_err("(%s) allocated an extra full vma!\n", p->name);
800 			err = -EINVAL;
801 			goto out_object;
802 		}
803 	}
804 
805 out_object:
806 	i915_gem_object_put(obj);
807 out:
808 	return err;
809 }
810 
811 int i915_vma_mock_selftests(void)
812 {
813 	static const struct i915_subtest tests[] = {
814 		SUBTEST(igt_vma_create),
815 		SUBTEST(igt_vma_pin1),
816 		SUBTEST(igt_vma_rotate_remap),
817 		SUBTEST(igt_vma_partial),
818 	};
819 	struct drm_i915_private *i915;
820 	struct i915_ggtt *ggtt;
821 	int err;
822 
823 	i915 = mock_gem_device();
824 	if (!i915)
825 		return -ENOMEM;
826 
827 	ggtt = kmalloc(sizeof(*ggtt), GFP_KERNEL);
828 	if (!ggtt) {
829 		err = -ENOMEM;
830 		goto out_put;
831 	}
832 	mock_init_ggtt(i915, ggtt);
833 
834 	mutex_lock(&i915->drm.struct_mutex);
835 	err = i915_subtests(tests, ggtt);
836 	mock_device_flush(i915);
837 	mutex_unlock(&i915->drm.struct_mutex);
838 
839 	i915_gem_drain_freed_objects(i915);
840 
841 	mock_fini_ggtt(ggtt);
842 	kfree(ggtt);
843 out_put:
844 	drm_dev_put(&i915->drm);
845 	return err;
846 }
847 
848 static int igt_vma_remapped_gtt(void *arg)
849 {
850 	struct drm_i915_private *i915 = arg;
851 	const struct intel_remapped_plane_info planes[] = {
852 		{ .width = 1, .height = 1, .stride = 1 },
853 		{ .width = 2, .height = 2, .stride = 2 },
854 		{ .width = 4, .height = 4, .stride = 4 },
855 		{ .width = 8, .height = 8, .stride = 8 },
856 
857 		{ .width = 3, .height = 5, .stride = 3 },
858 		{ .width = 3, .height = 5, .stride = 4 },
859 		{ .width = 3, .height = 5, .stride = 5 },
860 
861 		{ .width = 5, .height = 3, .stride = 5 },
862 		{ .width = 5, .height = 3, .stride = 7 },
863 		{ .width = 5, .height = 3, .stride = 9 },
864 
865 		{ .width = 4, .height = 6, .stride = 6 },
866 		{ .width = 6, .height = 4, .stride = 6 },
867 		{ }
868 	}, *p;
869 	enum i915_ggtt_view_type types[] = {
870 		I915_GGTT_VIEW_ROTATED,
871 		I915_GGTT_VIEW_REMAPPED,
872 		0,
873 	}, *t;
874 	struct drm_i915_gem_object *obj;
875 	intel_wakeref_t wakeref;
876 	int err = 0;
877 
878 	obj = i915_gem_object_create_internal(i915, 10 * 10 * PAGE_SIZE);
879 	if (IS_ERR(obj))
880 		return PTR_ERR(obj);
881 
882 	mutex_lock(&i915->drm.struct_mutex);
883 
884 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
885 
886 	for (t = types; *t; t++) {
887 		for (p = planes; p->width; p++) {
888 			struct i915_ggtt_view view = {
889 				.type = *t,
890 				.rotated.plane[0] = *p,
891 			};
892 			struct i915_vma *vma;
893 			u32 __iomem *map;
894 			unsigned int x, y;
895 			int err;
896 
897 			i915_gem_object_lock(obj);
898 			err = i915_gem_object_set_to_gtt_domain(obj, true);
899 			i915_gem_object_unlock(obj);
900 			if (err)
901 				goto out;
902 
903 			vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
904 			if (IS_ERR(vma)) {
905 				err = PTR_ERR(vma);
906 				goto out;
907 			}
908 
909 			GEM_BUG_ON(vma->ggtt_view.type != *t);
910 
911 			map = i915_vma_pin_iomap(vma);
912 			i915_vma_unpin(vma);
913 			if (IS_ERR(map)) {
914 				err = PTR_ERR(map);
915 				goto out;
916 			}
917 
918 			for (y = 0 ; y < p->height; y++) {
919 				for (x = 0 ; x < p->width; x++) {
920 					unsigned int offset;
921 					u32 val = y << 16 | x;
922 
923 					if (*t == I915_GGTT_VIEW_ROTATED)
924 						offset = (x * p->height + y) * PAGE_SIZE;
925 					else
926 						offset = (y * p->width + x) * PAGE_SIZE;
927 
928 					iowrite32(val, &map[offset / sizeof(*map)]);
929 				}
930 			}
931 
932 			i915_vma_unpin_iomap(vma);
933 
934 			vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
935 			if (IS_ERR(vma)) {
936 				err = PTR_ERR(vma);
937 				goto out;
938 			}
939 
940 			GEM_BUG_ON(vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL);
941 
942 			map = i915_vma_pin_iomap(vma);
943 			i915_vma_unpin(vma);
944 			if (IS_ERR(map)) {
945 				err = PTR_ERR(map);
946 				goto out;
947 			}
948 
949 			for (y = 0 ; y < p->height; y++) {
950 				for (x = 0 ; x < p->width; x++) {
951 					unsigned int offset, src_idx;
952 					u32 exp = y << 16 | x;
953 					u32 val;
954 
955 					if (*t == I915_GGTT_VIEW_ROTATED)
956 						src_idx = rotated_index(&view.rotated, 0, x, y);
957 					else
958 						src_idx = remapped_index(&view.remapped, 0, x, y);
959 					offset = src_idx * PAGE_SIZE;
960 
961 					val = ioread32(&map[offset / sizeof(*map)]);
962 					if (val != exp) {
963 						pr_err("%s VMA write test failed, expected 0x%x, found 0x%x\n",
964 						       *t == I915_GGTT_VIEW_ROTATED ? "Rotated" : "Remapped",
965 						       val, exp);
966 						i915_vma_unpin_iomap(vma);
967 						goto out;
968 					}
969 				}
970 			}
971 			i915_vma_unpin_iomap(vma);
972 
973 			cond_resched();
974 		}
975 	}
976 
977 out:
978 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
979 	mutex_unlock(&i915->drm.struct_mutex);
980 	i915_gem_object_put(obj);
981 
982 	return err;
983 }
984 
985 int i915_vma_live_selftests(struct drm_i915_private *i915)
986 {
987 	static const struct i915_subtest tests[] = {
988 		SUBTEST(igt_vma_remapped_gtt),
989 	};
990 
991 	return i915_subtests(tests, i915);
992 }
993