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