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 "../i915_selftest.h"
28 
29 #include "mock_gem_device.h"
30 #include "mock_context.h"
31 #include "mock_gtt.h"
32 
33 static bool assert_vma(struct i915_vma *vma,
34 		       struct drm_i915_gem_object *obj,
35 		       struct i915_gem_context *ctx)
36 {
37 	bool ok = true;
38 
39 	if (vma->vm != &ctx->ppgtt->vm) {
40 		pr_err("VMA created with wrong VM\n");
41 		ok = false;
42 	}
43 
44 	if (vma->size != obj->base.size) {
45 		pr_err("VMA created with wrong size, found %llu, expected %zu\n",
46 		       vma->size, obj->base.size);
47 		ok = false;
48 	}
49 
50 	if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) {
51 		pr_err("VMA created with wrong type [%d]\n",
52 		       vma->ggtt_view.type);
53 		ok = false;
54 	}
55 
56 	return ok;
57 }
58 
59 static struct i915_vma *
60 checked_vma_instance(struct drm_i915_gem_object *obj,
61 		     struct i915_address_space *vm,
62 		     struct i915_ggtt_view *view)
63 {
64 	struct i915_vma *vma;
65 	bool ok = true;
66 
67 	vma = i915_vma_instance(obj, vm, view);
68 	if (IS_ERR(vma))
69 		return vma;
70 
71 	/* Manual checks, will be reinforced by i915_vma_compare! */
72 	if (vma->vm != vm) {
73 		pr_err("VMA's vm [%p] does not match request [%p]\n",
74 		       vma->vm, vm);
75 		ok = false;
76 	}
77 
78 	if (i915_is_ggtt(vm) != i915_vma_is_ggtt(vma)) {
79 		pr_err("VMA ggtt status [%d] does not match parent [%d]\n",
80 		       i915_vma_is_ggtt(vma), i915_is_ggtt(vm));
81 		ok = false;
82 	}
83 
84 	if (i915_vma_compare(vma, vm, view)) {
85 		pr_err("i915_vma_compare failed with create parameters!\n");
86 		return ERR_PTR(-EINVAL);
87 	}
88 
89 	if (i915_vma_compare(vma, vma->vm,
90 			     i915_vma_is_ggtt(vma) ? &vma->ggtt_view : NULL)) {
91 		pr_err("i915_vma_compare failed with itself\n");
92 		return ERR_PTR(-EINVAL);
93 	}
94 
95 	if (!ok) {
96 		pr_err("i915_vma_compare failed to detect the difference!\n");
97 		return ERR_PTR(-EINVAL);
98 	}
99 
100 	return vma;
101 }
102 
103 static int create_vmas(struct drm_i915_private *i915,
104 		       struct list_head *objects,
105 		       struct list_head *contexts)
106 {
107 	struct drm_i915_gem_object *obj;
108 	struct i915_gem_context *ctx;
109 	int pinned;
110 
111 	list_for_each_entry(obj, objects, st_link) {
112 		for (pinned = 0; pinned <= 1; pinned++) {
113 			list_for_each_entry(ctx, contexts, link) {
114 				struct i915_address_space *vm = &ctx->ppgtt->vm;
115 				struct i915_vma *vma;
116 				int err;
117 
118 				vma = checked_vma_instance(obj, vm, NULL);
119 				if (IS_ERR(vma))
120 					return PTR_ERR(vma);
121 
122 				if (!assert_vma(vma, obj, ctx)) {
123 					pr_err("VMA lookup/create failed\n");
124 					return -EINVAL;
125 				}
126 
127 				if (!pinned) {
128 					err = i915_vma_pin(vma, 0, 0, PIN_USER);
129 					if (err) {
130 						pr_err("Failed to pin VMA\n");
131 						return err;
132 					}
133 				} else {
134 					i915_vma_unpin(vma);
135 				}
136 			}
137 		}
138 	}
139 
140 	return 0;
141 }
142 
143 static int igt_vma_create(void *arg)
144 {
145 	struct i915_ggtt *ggtt = arg;
146 	struct drm_i915_private *i915 = ggtt->vm.i915;
147 	struct drm_i915_gem_object *obj, *on;
148 	struct i915_gem_context *ctx, *cn;
149 	unsigned long num_obj, num_ctx;
150 	unsigned long no, nc;
151 	IGT_TIMEOUT(end_time);
152 	LIST_HEAD(contexts);
153 	LIST_HEAD(objects);
154 	int err = -ENOMEM;
155 
156 	/* Exercise creating many vma amonst many objections, checking the
157 	 * vma creation and lookup routines.
158 	 */
159 
160 	no = 0;
161 	for_each_prime_number(num_obj, ULONG_MAX - 1) {
162 		for (; no < num_obj; no++) {
163 			obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
164 			if (IS_ERR(obj))
165 				goto out;
166 
167 			list_add(&obj->st_link, &objects);
168 		}
169 
170 		nc = 0;
171 		for_each_prime_number(num_ctx, MAX_CONTEXT_HW_ID) {
172 			for (; nc < num_ctx; nc++) {
173 				ctx = mock_context(i915, "mock");
174 				if (!ctx)
175 					goto out;
176 
177 				list_move(&ctx->link, &contexts);
178 			}
179 
180 			err = create_vmas(i915, &objects, &contexts);
181 			if (err)
182 				goto out;
183 
184 			if (igt_timeout(end_time,
185 					"%s timed out: after %lu objects in %lu contexts\n",
186 					__func__, no, nc))
187 				goto end;
188 		}
189 
190 		list_for_each_entry_safe(ctx, cn, &contexts, link) {
191 			list_del_init(&ctx->link);
192 			mock_context_close(ctx);
193 		}
194 	}
195 
196 end:
197 	/* Final pass to lookup all created contexts */
198 	err = create_vmas(i915, &objects, &contexts);
199 out:
200 	list_for_each_entry_safe(ctx, cn, &contexts, link) {
201 		list_del_init(&ctx->link);
202 		mock_context_close(ctx);
203 	}
204 
205 	list_for_each_entry_safe(obj, on, &objects, st_link)
206 		i915_gem_object_put(obj);
207 	return err;
208 }
209 
210 struct pin_mode {
211 	u64 size;
212 	u64 flags;
213 	bool (*assert)(const struct i915_vma *,
214 		       const struct pin_mode *mode,
215 		       int result);
216 	const char *string;
217 };
218 
219 static bool assert_pin_valid(const struct i915_vma *vma,
220 			     const struct pin_mode *mode,
221 			     int result)
222 {
223 	if (result)
224 		return false;
225 
226 	if (i915_vma_misplaced(vma, mode->size, 0, mode->flags))
227 		return false;
228 
229 	return true;
230 }
231 
232 __maybe_unused
233 static bool assert_pin_enospc(const struct i915_vma *vma,
234 			      const struct pin_mode *mode,
235 			      int result)
236 {
237 	return result == -ENOSPC;
238 }
239 
240 __maybe_unused
241 static bool assert_pin_einval(const struct i915_vma *vma,
242 			      const struct pin_mode *mode,
243 			      int result)
244 {
245 	return result == -EINVAL;
246 }
247 
248 static int igt_vma_pin1(void *arg)
249 {
250 	struct i915_ggtt *ggtt = arg;
251 	const struct pin_mode modes[] = {
252 #define VALID(sz, fl) { .size = (sz), .flags = (fl), .assert = assert_pin_valid, .string = #sz ", " #fl ", (valid) " }
253 #define __INVALID(sz, fl, check, eval) { .size = (sz), .flags = (fl), .assert = (check), .string = #sz ", " #fl ", (invalid " #eval ")" }
254 #define INVALID(sz, fl) __INVALID(sz, fl, assert_pin_einval, EINVAL)
255 #define NOSPACE(sz, fl) __INVALID(sz, fl, assert_pin_enospc, ENOSPC)
256 		VALID(0, PIN_GLOBAL),
257 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE),
258 
259 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 4096),
260 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 8192),
261 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
262 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
263 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)),
264 
265 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)),
266 		INVALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | ggtt->mappable_end),
267 		VALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)),
268 		INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | ggtt->vm.total),
269 		INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | round_down(U64_MAX, PAGE_SIZE)),
270 
271 		VALID(4096, PIN_GLOBAL),
272 		VALID(8192, PIN_GLOBAL),
273 		VALID(ggtt->mappable_end - 4096, PIN_GLOBAL | PIN_MAPPABLE),
274 		VALID(ggtt->mappable_end, PIN_GLOBAL | PIN_MAPPABLE),
275 		NOSPACE(ggtt->mappable_end + 4096, PIN_GLOBAL | PIN_MAPPABLE),
276 		VALID(ggtt->vm.total - 4096, PIN_GLOBAL),
277 		VALID(ggtt->vm.total, PIN_GLOBAL),
278 		NOSPACE(ggtt->vm.total + 4096, PIN_GLOBAL),
279 		NOSPACE(round_down(U64_MAX, PAGE_SIZE), PIN_GLOBAL),
280 		INVALID(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)),
281 		INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)),
282 		INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (round_down(U64_MAX, PAGE_SIZE) - 4096)),
283 
284 		VALID(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
285 
286 #if !IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
287 		/* Misusing BIAS is a programming error (it is not controllable
288 		 * from userspace) so when debugging is enabled, it explodes.
289 		 * However, the tests are still quite interesting for checking
290 		 * variable start, end and size.
291 		 */
292 		NOSPACE(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | ggtt->mappable_end),
293 		NOSPACE(0, PIN_GLOBAL | PIN_OFFSET_BIAS | ggtt->vm.total),
294 		NOSPACE(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
295 		NOSPACE(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)),
296 #endif
297 		{ },
298 #undef NOSPACE
299 #undef INVALID
300 #undef __INVALID
301 #undef VALID
302 	}, *m;
303 	struct drm_i915_gem_object *obj;
304 	struct i915_vma *vma;
305 	int err = -EINVAL;
306 
307 	/* Exercise all the weird and wonderful i915_vma_pin requests,
308 	 * focusing on error handling of boundary conditions.
309 	 */
310 
311 	GEM_BUG_ON(!drm_mm_clean(&ggtt->vm.mm));
312 
313 	obj = i915_gem_object_create_internal(ggtt->vm.i915, PAGE_SIZE);
314 	if (IS_ERR(obj))
315 		return PTR_ERR(obj);
316 
317 	vma = checked_vma_instance(obj, &ggtt->vm, NULL);
318 	if (IS_ERR(vma))
319 		goto out;
320 
321 	for (m = modes; m->assert; m++) {
322 		err = i915_vma_pin(vma, m->size, 0, m->flags);
323 		if (!m->assert(vma, m, err)) {
324 			pr_err("%s to pin single page into GGTT with mode[%d:%s]: size=%llx flags=%llx, err=%d\n",
325 			       m->assert == assert_pin_valid ? "Failed" : "Unexpectedly succeeded",
326 			       (int)(m - modes), m->string, m->size, m->flags,
327 			       err);
328 			if (!err)
329 				i915_vma_unpin(vma);
330 			err = -EINVAL;
331 			goto out;
332 		}
333 
334 		if (!err) {
335 			i915_vma_unpin(vma);
336 			err = i915_vma_unbind(vma);
337 			if (err) {
338 				pr_err("Failed to unbind single page from GGTT, err=%d\n", err);
339 				goto out;
340 			}
341 		}
342 	}
343 
344 	err = 0;
345 out:
346 	i915_gem_object_put(obj);
347 	return err;
348 }
349 
350 static unsigned long rotated_index(const struct intel_rotation_info *r,
351 				   unsigned int n,
352 				   unsigned int x,
353 				   unsigned int y)
354 {
355 	return (r->plane[n].stride * (r->plane[n].height - y - 1) +
356 		r->plane[n].offset + x);
357 }
358 
359 static struct scatterlist *
360 assert_rotated(struct drm_i915_gem_object *obj,
361 	       const struct intel_rotation_info *r, unsigned int n,
362 	       struct scatterlist *sg)
363 {
364 	unsigned int x, y;
365 
366 	for (x = 0; x < r->plane[n].width; x++) {
367 		for (y = 0; y < r->plane[n].height; y++) {
368 			unsigned long src_idx;
369 			dma_addr_t src;
370 
371 			if (!sg) {
372 				pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
373 				       n, x, y);
374 				return ERR_PTR(-EINVAL);
375 			}
376 
377 			src_idx = rotated_index(r, n, x, y);
378 			src = i915_gem_object_get_dma_address(obj, src_idx);
379 
380 			if (sg_dma_len(sg) != PAGE_SIZE) {
381 				pr_err("Invalid sg.length, found %d, expected %lu for rotated page (%d, %d) [src index %lu]\n",
382 				       sg_dma_len(sg), PAGE_SIZE,
383 				       x, y, src_idx);
384 				return ERR_PTR(-EINVAL);
385 			}
386 
387 			if (sg_dma_address(sg) != src) {
388 				pr_err("Invalid address for rotated page (%d, %d) [src index %lu]\n",
389 				       x, y, src_idx);
390 				return ERR_PTR(-EINVAL);
391 			}
392 
393 			sg = sg_next(sg);
394 		}
395 	}
396 
397 	return sg;
398 }
399 
400 static unsigned int rotated_size(const struct intel_rotation_plane_info *a,
401 				 const struct intel_rotation_plane_info *b)
402 {
403 	return a->width * a->height + b->width * b->height;
404 }
405 
406 static int igt_vma_rotate(void *arg)
407 {
408 	struct i915_ggtt *ggtt = arg;
409 	struct i915_address_space *vm = &ggtt->vm;
410 	struct drm_i915_gem_object *obj;
411 	const struct intel_rotation_plane_info planes[] = {
412 		{ .width = 1, .height = 1, .stride = 1 },
413 		{ .width = 2, .height = 2, .stride = 2 },
414 		{ .width = 4, .height = 4, .stride = 4 },
415 		{ .width = 8, .height = 8, .stride = 8 },
416 
417 		{ .width = 3, .height = 5, .stride = 3 },
418 		{ .width = 3, .height = 5, .stride = 4 },
419 		{ .width = 3, .height = 5, .stride = 5 },
420 
421 		{ .width = 5, .height = 3, .stride = 5 },
422 		{ .width = 5, .height = 3, .stride = 7 },
423 		{ .width = 5, .height = 3, .stride = 9 },
424 
425 		{ .width = 4, .height = 6, .stride = 6 },
426 		{ .width = 6, .height = 4, .stride = 6 },
427 		{ }
428 	}, *a, *b;
429 	const unsigned int max_pages = 64;
430 	int err = -ENOMEM;
431 
432 	/* Create VMA for many different combinations of planes and check
433 	 * that the page layout within the rotated VMA match our expectations.
434 	 */
435 
436 	obj = i915_gem_object_create_internal(vm->i915, max_pages * PAGE_SIZE);
437 	if (IS_ERR(obj))
438 		goto out;
439 
440 	for (a = planes; a->width; a++) {
441 		for (b = planes + ARRAY_SIZE(planes); b-- != planes; ) {
442 			struct i915_ggtt_view view;
443 			unsigned int n, max_offset;
444 
445 			max_offset = max(a->stride * a->height,
446 					 b->stride * b->height);
447 			GEM_BUG_ON(max_offset > max_pages);
448 			max_offset = max_pages - max_offset;
449 
450 			view.type = I915_GGTT_VIEW_ROTATED;
451 			view.rotated.plane[0] = *a;
452 			view.rotated.plane[1] = *b;
453 
454 			for_each_prime_number_from(view.rotated.plane[0].offset, 0, max_offset) {
455 				for_each_prime_number_from(view.rotated.plane[1].offset, 0, max_offset) {
456 					struct scatterlist *sg;
457 					struct i915_vma *vma;
458 
459 					vma = checked_vma_instance(obj, vm, &view);
460 					if (IS_ERR(vma)) {
461 						err = PTR_ERR(vma);
462 						goto out_object;
463 					}
464 
465 					err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
466 					if (err) {
467 						pr_err("Failed to pin VMA, err=%d\n", err);
468 						goto out_object;
469 					}
470 
471 					if (vma->size != rotated_size(a, b) * PAGE_SIZE) {
472 						pr_err("VMA is wrong size, expected %lu, found %llu\n",
473 						       PAGE_SIZE * rotated_size(a, b), vma->size);
474 						err = -EINVAL;
475 						goto out_object;
476 					}
477 
478 					if (vma->pages->nents != rotated_size(a, b)) {
479 						pr_err("sg table is wrong sizeo, expected %u, found %u nents\n",
480 						       rotated_size(a, b), vma->pages->nents);
481 						err = -EINVAL;
482 						goto out_object;
483 					}
484 
485 					if (vma->node.size < vma->size) {
486 						pr_err("VMA binding too small, expected %llu, found %llu\n",
487 						       vma->size, vma->node.size);
488 						err = -EINVAL;
489 						goto out_object;
490 					}
491 
492 					if (vma->pages == obj->mm.pages) {
493 						pr_err("VMA using unrotated object pages!\n");
494 						err = -EINVAL;
495 						goto out_object;
496 					}
497 
498 					sg = vma->pages->sgl;
499 					for (n = 0; n < ARRAY_SIZE(view.rotated.plane); n++) {
500 						sg = assert_rotated(obj, &view.rotated, n, sg);
501 						if (IS_ERR(sg)) {
502 							pr_err("Inconsistent VMA pages for plane %d: [(%d, %d, %d, %d), (%d, %d, %d, %d)]\n", n,
503 							       view.rotated.plane[0].width,
504 							       view.rotated.plane[0].height,
505 							       view.rotated.plane[0].stride,
506 							       view.rotated.plane[0].offset,
507 							       view.rotated.plane[1].width,
508 							       view.rotated.plane[1].height,
509 							       view.rotated.plane[1].stride,
510 							       view.rotated.plane[1].offset);
511 							err = -EINVAL;
512 							goto out_object;
513 						}
514 					}
515 
516 					i915_vma_unpin(vma);
517 				}
518 			}
519 		}
520 	}
521 
522 out_object:
523 	i915_gem_object_put(obj);
524 out:
525 	return err;
526 }
527 
528 static bool assert_partial(struct drm_i915_gem_object *obj,
529 			   struct i915_vma *vma,
530 			   unsigned long offset,
531 			   unsigned long size)
532 {
533 	struct sgt_iter sgt;
534 	dma_addr_t dma;
535 
536 	for_each_sgt_dma(dma, sgt, vma->pages) {
537 		dma_addr_t src;
538 
539 		if (!size) {
540 			pr_err("Partial scattergather list too long\n");
541 			return false;
542 		}
543 
544 		src = i915_gem_object_get_dma_address(obj, offset);
545 		if (src != dma) {
546 			pr_err("DMA mismatch for partial page offset %lu\n",
547 			       offset);
548 			return false;
549 		}
550 
551 		offset++;
552 		size--;
553 	}
554 
555 	return true;
556 }
557 
558 static bool assert_pin(struct i915_vma *vma,
559 		       struct i915_ggtt_view *view,
560 		       u64 size,
561 		       const char *name)
562 {
563 	bool ok = true;
564 
565 	if (vma->size != size) {
566 		pr_err("(%s) VMA is wrong size, expected %llu, found %llu\n",
567 		       name, size, vma->size);
568 		ok = false;
569 	}
570 
571 	if (vma->node.size < vma->size) {
572 		pr_err("(%s) VMA binding too small, expected %llu, found %llu\n",
573 		       name, vma->size, vma->node.size);
574 		ok = false;
575 	}
576 
577 	if (view && view->type != I915_GGTT_VIEW_NORMAL) {
578 		if (memcmp(&vma->ggtt_view, view, sizeof(*view))) {
579 			pr_err("(%s) VMA mismatch upon creation!\n",
580 			       name);
581 			ok = false;
582 		}
583 
584 		if (vma->pages == vma->obj->mm.pages) {
585 			pr_err("(%s) VMA using original object pages!\n",
586 			       name);
587 			ok = false;
588 		}
589 	} else {
590 		if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) {
591 			pr_err("Not the normal ggtt view! Found %d\n",
592 			       vma->ggtt_view.type);
593 			ok = false;
594 		}
595 
596 		if (vma->pages != vma->obj->mm.pages) {
597 			pr_err("VMA not using object pages!\n");
598 			ok = false;
599 		}
600 	}
601 
602 	return ok;
603 }
604 
605 static int igt_vma_partial(void *arg)
606 {
607 	struct i915_ggtt *ggtt = arg;
608 	struct i915_address_space *vm = &ggtt->vm;
609 	const unsigned int npages = 1021; /* prime! */
610 	struct drm_i915_gem_object *obj;
611 	const struct phase {
612 		const char *name;
613 	} phases[] = {
614 		{ "create" },
615 		{ "lookup" },
616 		{ },
617 	}, *p;
618 	unsigned int sz, offset;
619 	struct i915_vma *vma;
620 	int err = -ENOMEM;
621 
622 	/* Create lots of different VMA for the object and check that
623 	 * we are returned the same VMA when we later request the same range.
624 	 */
625 
626 	obj = i915_gem_object_create_internal(vm->i915, npages * PAGE_SIZE);
627 	if (IS_ERR(obj))
628 		goto out;
629 
630 	for (p = phases; p->name; p++) { /* exercise both create/lookup */
631 		unsigned int count, nvma;
632 
633 		nvma = 0;
634 		for_each_prime_number_from(sz, 1, npages) {
635 			for_each_prime_number_from(offset, 0, npages - sz) {
636 				struct i915_ggtt_view view;
637 
638 				view.type = I915_GGTT_VIEW_PARTIAL;
639 				view.partial.offset = offset;
640 				view.partial.size = sz;
641 
642 				if (sz == npages)
643 					view.type = I915_GGTT_VIEW_NORMAL;
644 
645 				vma = checked_vma_instance(obj, vm, &view);
646 				if (IS_ERR(vma)) {
647 					err = PTR_ERR(vma);
648 					goto out_object;
649 				}
650 
651 				err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
652 				if (err)
653 					goto out_object;
654 
655 				if (!assert_pin(vma, &view, sz*PAGE_SIZE, p->name)) {
656 					pr_err("(%s) Inconsistent partial pinning for (offset=%d, size=%d)\n",
657 					       p->name, offset, sz);
658 					err = -EINVAL;
659 					goto out_object;
660 				}
661 
662 				if (!assert_partial(obj, vma, offset, sz)) {
663 					pr_err("(%s) Inconsistent partial pages for (offset=%d, size=%d)\n",
664 					       p->name, offset, sz);
665 					err = -EINVAL;
666 					goto out_object;
667 				}
668 
669 				i915_vma_unpin(vma);
670 				nvma++;
671 			}
672 		}
673 
674 		count = 0;
675 		list_for_each_entry(vma, &obj->vma.list, obj_link)
676 			count++;
677 		if (count != nvma) {
678 			pr_err("(%s) All partial vma were not recorded on the obj->vma_list: found %u, expected %u\n",
679 			       p->name, count, nvma);
680 			err = -EINVAL;
681 			goto out_object;
682 		}
683 
684 		/* Check that we did create the whole object mapping */
685 		vma = checked_vma_instance(obj, vm, NULL);
686 		if (IS_ERR(vma)) {
687 			err = PTR_ERR(vma);
688 			goto out_object;
689 		}
690 
691 		err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
692 		if (err)
693 			goto out_object;
694 
695 		if (!assert_pin(vma, NULL, obj->base.size, p->name)) {
696 			pr_err("(%s) inconsistent full pin\n", p->name);
697 			err = -EINVAL;
698 			goto out_object;
699 		}
700 
701 		i915_vma_unpin(vma);
702 
703 		count = 0;
704 		list_for_each_entry(vma, &obj->vma.list, obj_link)
705 			count++;
706 		if (count != nvma) {
707 			pr_err("(%s) allocated an extra full vma!\n", p->name);
708 			err = -EINVAL;
709 			goto out_object;
710 		}
711 	}
712 
713 out_object:
714 	i915_gem_object_put(obj);
715 out:
716 	return err;
717 }
718 
719 int i915_vma_mock_selftests(void)
720 {
721 	static const struct i915_subtest tests[] = {
722 		SUBTEST(igt_vma_create),
723 		SUBTEST(igt_vma_pin1),
724 		SUBTEST(igt_vma_rotate),
725 		SUBTEST(igt_vma_partial),
726 	};
727 	struct drm_i915_private *i915;
728 	struct i915_ggtt ggtt;
729 	int err;
730 
731 	i915 = mock_gem_device();
732 	if (!i915)
733 		return -ENOMEM;
734 
735 	mock_init_ggtt(i915, &ggtt);
736 
737 	mutex_lock(&i915->drm.struct_mutex);
738 	err = i915_subtests(tests, &ggtt);
739 	mock_device_flush(i915);
740 	mutex_unlock(&i915->drm.struct_mutex);
741 
742 	i915_gem_drain_freed_objects(i915);
743 
744 	mock_fini_ggtt(&ggtt);
745 	drm_dev_put(&i915->drm);
746 
747 	return err;
748 }
749