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_e2big(const struct i915_vma *vma,
229 			     const struct pin_mode *mode,
230 			     int result)
231 {
232 	return result == -E2BIG;
233 }
234 
235 __maybe_unused
236 static bool assert_pin_enospc(const struct i915_vma *vma,
237 			      const struct pin_mode *mode,
238 			      int result)
239 {
240 	return result == -ENOSPC;
241 }
242 
243 __maybe_unused
244 static bool assert_pin_einval(const struct i915_vma *vma,
245 			      const struct pin_mode *mode,
246 			      int result)
247 {
248 	return result == -EINVAL;
249 }
250 
251 static int igt_vma_pin1(void *arg)
252 {
253 	struct drm_i915_private *i915 = arg;
254 	const struct pin_mode modes[] = {
255 #define VALID(sz, fl) { .size = (sz), .flags = (fl), .assert = assert_pin_valid, .string = #sz ", " #fl ", (valid) " }
256 #define __INVALID(sz, fl, check, eval) { .size = (sz), .flags = (fl), .assert = (check), .string = #sz ", " #fl ", (invalid " #eval ")" }
257 #define INVALID(sz, fl) __INVALID(sz, fl, assert_pin_einval, EINVAL)
258 #define TOOBIG(sz, fl) __INVALID(sz, fl, assert_pin_e2big, E2BIG)
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 | (i915->ggtt.mappable_end - 4096)),
266 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (i915->ggtt.mappable_end - 4096)),
267 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (i915->ggtt.base.total - 4096)),
268 
269 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (i915->ggtt.mappable_end - 4096)),
270 		INVALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | i915->ggtt.mappable_end),
271 		VALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | (i915->ggtt.base.total - 4096)),
272 		INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | i915->ggtt.base.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(i915->ggtt.mappable_end - 4096, PIN_GLOBAL | PIN_MAPPABLE),
278 		VALID(i915->ggtt.mappable_end, PIN_GLOBAL | PIN_MAPPABLE),
279 		TOOBIG(i915->ggtt.mappable_end + 4096, PIN_GLOBAL | PIN_MAPPABLE),
280 		VALID(i915->ggtt.base.total - 4096, PIN_GLOBAL),
281 		VALID(i915->ggtt.base.total, PIN_GLOBAL),
282 		TOOBIG(i915->ggtt.base.total + 4096, PIN_GLOBAL),
283 		TOOBIG(round_down(U64_MAX, PAGE_SIZE), PIN_GLOBAL),
284 		INVALID(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (i915->ggtt.mappable_end - 4096)),
285 		INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (i915->ggtt.base.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 | (i915->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 | i915->ggtt.mappable_end),
297 		NOSPACE(0, PIN_GLOBAL | PIN_OFFSET_BIAS | i915->ggtt.base.total),
298 		NOSPACE(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (i915->ggtt.mappable_end - 4096)),
299 		NOSPACE(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (i915->ggtt.base.total - 4096)),
300 #endif
301 		{ },
302 #undef NOSPACE
303 #undef TOOBIG
304 #undef INVALID
305 #undef __INVALID
306 #undef VALID
307 	}, *m;
308 	struct drm_i915_gem_object *obj;
309 	struct i915_vma *vma;
310 	int err = -EINVAL;
311 
312 	/* Exercise all the weird and wonderful i915_vma_pin requests,
313 	 * focusing on error handling of boundary conditions.
314 	 */
315 
316 	GEM_BUG_ON(!drm_mm_clean(&i915->ggtt.base.mm));
317 
318 	obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
319 	if (IS_ERR(obj))
320 		return PTR_ERR(obj);
321 
322 	vma = checked_vma_instance(obj, &i915->ggtt.base, NULL);
323 	if (IS_ERR(vma))
324 		goto out;
325 
326 	for (m = modes; m->assert; m++) {
327 		err = i915_vma_pin(vma, m->size, 0, m->flags);
328 		if (!m->assert(vma, m, err)) {
329 			pr_err("%s to pin single page into GGTT with mode[%d:%s]: size=%llx flags=%llx, err=%d\n",
330 			       m->assert == assert_pin_valid ? "Failed" : "Unexpectedly succeeded",
331 			       (int)(m - modes), m->string, m->size, m->flags,
332 			       err);
333 			if (!err)
334 				i915_vma_unpin(vma);
335 			err = -EINVAL;
336 			goto out;
337 		}
338 
339 		if (!err) {
340 			i915_vma_unpin(vma);
341 			err = i915_vma_unbind(vma);
342 			if (err) {
343 				pr_err("Failed to unbind single page from GGTT, err=%d\n", err);
344 				goto out;
345 			}
346 		}
347 	}
348 
349 	err = 0;
350 out:
351 	i915_gem_object_put(obj);
352 	return err;
353 }
354 
355 static unsigned long rotated_index(const struct intel_rotation_info *r,
356 				   unsigned int n,
357 				   unsigned int x,
358 				   unsigned int y)
359 {
360 	return (r->plane[n].stride * (r->plane[n].height - y - 1) +
361 		r->plane[n].offset + x);
362 }
363 
364 static struct scatterlist *
365 assert_rotated(struct drm_i915_gem_object *obj,
366 	       const struct intel_rotation_info *r, unsigned int n,
367 	       struct scatterlist *sg)
368 {
369 	unsigned int x, y;
370 
371 	for (x = 0; x < r->plane[n].width; x++) {
372 		for (y = 0; y < r->plane[n].height; y++) {
373 			unsigned long src_idx;
374 			dma_addr_t src;
375 
376 			if (!sg) {
377 				pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
378 				       n, x, y);
379 				return ERR_PTR(-EINVAL);
380 			}
381 
382 			src_idx = rotated_index(r, n, x, y);
383 			src = i915_gem_object_get_dma_address(obj, src_idx);
384 
385 			if (sg_dma_len(sg) != PAGE_SIZE) {
386 				pr_err("Invalid sg.length, found %d, expected %lu for rotated page (%d, %d) [src index %lu]\n",
387 				       sg_dma_len(sg), PAGE_SIZE,
388 				       x, y, src_idx);
389 				return ERR_PTR(-EINVAL);
390 			}
391 
392 			if (sg_dma_address(sg) != src) {
393 				pr_err("Invalid address for rotated page (%d, %d) [src index %lu]\n",
394 				       x, y, src_idx);
395 				return ERR_PTR(-EINVAL);
396 			}
397 
398 			sg = sg_next(sg);
399 		}
400 	}
401 
402 	return sg;
403 }
404 
405 static unsigned int rotated_size(const struct intel_rotation_plane_info *a,
406 				 const struct intel_rotation_plane_info *b)
407 {
408 	return a->width * a->height + b->width * b->height;
409 }
410 
411 static int igt_vma_rotate(void *arg)
412 {
413 	struct drm_i915_private *i915 = arg;
414 	struct i915_address_space *vm = &i915->ggtt.base;
415 	struct drm_i915_gem_object *obj;
416 	const struct intel_rotation_plane_info planes[] = {
417 		{ .width = 1, .height = 1, .stride = 1 },
418 		{ .width = 2, .height = 2, .stride = 2 },
419 		{ .width = 4, .height = 4, .stride = 4 },
420 		{ .width = 8, .height = 8, .stride = 8 },
421 
422 		{ .width = 3, .height = 5, .stride = 3 },
423 		{ .width = 3, .height = 5, .stride = 4 },
424 		{ .width = 3, .height = 5, .stride = 5 },
425 
426 		{ .width = 5, .height = 3, .stride = 5 },
427 		{ .width = 5, .height = 3, .stride = 7 },
428 		{ .width = 5, .height = 3, .stride = 9 },
429 
430 		{ .width = 4, .height = 6, .stride = 6 },
431 		{ .width = 6, .height = 4, .stride = 6 },
432 		{ }
433 	}, *a, *b;
434 	const unsigned int max_pages = 64;
435 	int err = -ENOMEM;
436 
437 	/* Create VMA for many different combinations of planes and check
438 	 * that the page layout within the rotated VMA match our expectations.
439 	 */
440 
441 	obj = i915_gem_object_create_internal(i915, max_pages * PAGE_SIZE);
442 	if (IS_ERR(obj))
443 		goto out;
444 
445 	for (a = planes; a->width; a++) {
446 		for (b = planes + ARRAY_SIZE(planes); b-- != planes; ) {
447 			struct i915_ggtt_view view;
448 			unsigned int n, max_offset;
449 
450 			max_offset = max(a->stride * a->height,
451 					 b->stride * b->height);
452 			GEM_BUG_ON(max_offset > max_pages);
453 			max_offset = max_pages - max_offset;
454 
455 			view.type = I915_GGTT_VIEW_ROTATED;
456 			view.rotated.plane[0] = *a;
457 			view.rotated.plane[1] = *b;
458 
459 			for_each_prime_number_from(view.rotated.plane[0].offset, 0, max_offset) {
460 				for_each_prime_number_from(view.rotated.plane[1].offset, 0, max_offset) {
461 					struct scatterlist *sg;
462 					struct i915_vma *vma;
463 
464 					vma = checked_vma_instance(obj, vm, &view);
465 					if (IS_ERR(vma)) {
466 						err = PTR_ERR(vma);
467 						goto out_object;
468 					}
469 
470 					err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
471 					if (err) {
472 						pr_err("Failed to pin VMA, err=%d\n", err);
473 						goto out_object;
474 					}
475 
476 					if (vma->size != rotated_size(a, b) * PAGE_SIZE) {
477 						pr_err("VMA is wrong size, expected %lu, found %llu\n",
478 						       PAGE_SIZE * rotated_size(a, b), vma->size);
479 						err = -EINVAL;
480 						goto out_object;
481 					}
482 
483 					if (vma->pages->nents != rotated_size(a, b)) {
484 						pr_err("sg table is wrong sizeo, expected %u, found %u nents\n",
485 						       rotated_size(a, b), vma->pages->nents);
486 						err = -EINVAL;
487 						goto out_object;
488 					}
489 
490 					if (vma->node.size < vma->size) {
491 						pr_err("VMA binding too small, expected %llu, found %llu\n",
492 						       vma->size, vma->node.size);
493 						err = -EINVAL;
494 						goto out_object;
495 					}
496 
497 					if (vma->pages == obj->mm.pages) {
498 						pr_err("VMA using unrotated object pages!\n");
499 						err = -EINVAL;
500 						goto out_object;
501 					}
502 
503 					sg = vma->pages->sgl;
504 					for (n = 0; n < ARRAY_SIZE(view.rotated.plane); n++) {
505 						sg = assert_rotated(obj, &view.rotated, n, sg);
506 						if (IS_ERR(sg)) {
507 							pr_err("Inconsistent VMA pages for plane %d: [(%d, %d, %d, %d), (%d, %d, %d, %d)]\n", n,
508 							       view.rotated.plane[0].width,
509 							       view.rotated.plane[0].height,
510 							       view.rotated.plane[0].stride,
511 							       view.rotated.plane[0].offset,
512 							       view.rotated.plane[1].width,
513 							       view.rotated.plane[1].height,
514 							       view.rotated.plane[1].stride,
515 							       view.rotated.plane[1].offset);
516 							err = -EINVAL;
517 							goto out_object;
518 						}
519 					}
520 
521 					i915_vma_unpin(vma);
522 				}
523 			}
524 		}
525 	}
526 
527 out_object:
528 	i915_gem_object_put(obj);
529 out:
530 	return err;
531 }
532 
533 static bool assert_partial(struct drm_i915_gem_object *obj,
534 			   struct i915_vma *vma,
535 			   unsigned long offset,
536 			   unsigned long size)
537 {
538 	struct sgt_iter sgt;
539 	dma_addr_t dma;
540 
541 	for_each_sgt_dma(dma, sgt, vma->pages) {
542 		dma_addr_t src;
543 
544 		if (!size) {
545 			pr_err("Partial scattergather list too long\n");
546 			return false;
547 		}
548 
549 		src = i915_gem_object_get_dma_address(obj, offset);
550 		if (src != dma) {
551 			pr_err("DMA mismatch for partial page offset %lu\n",
552 			       offset);
553 			return false;
554 		}
555 
556 		offset++;
557 		size--;
558 	}
559 
560 	return true;
561 }
562 
563 static bool assert_pin(struct i915_vma *vma,
564 		       struct i915_ggtt_view *view,
565 		       u64 size,
566 		       const char *name)
567 {
568 	bool ok = true;
569 
570 	if (vma->size != size) {
571 		pr_err("(%s) VMA is wrong size, expected %llu, found %llu\n",
572 		       name, size, vma->size);
573 		ok = false;
574 	}
575 
576 	if (vma->node.size < vma->size) {
577 		pr_err("(%s) VMA binding too small, expected %llu, found %llu\n",
578 		       name, vma->size, vma->node.size);
579 		ok = false;
580 	}
581 
582 	if (view && view->type != I915_GGTT_VIEW_NORMAL) {
583 		if (memcmp(&vma->ggtt_view, view, sizeof(*view))) {
584 			pr_err("(%s) VMA mismatch upon creation!\n",
585 			       name);
586 			ok = false;
587 		}
588 
589 		if (vma->pages == vma->obj->mm.pages) {
590 			pr_err("(%s) VMA using original object pages!\n",
591 			       name);
592 			ok = false;
593 		}
594 	} else {
595 		if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) {
596 			pr_err("Not the normal ggtt view! Found %d\n",
597 			       vma->ggtt_view.type);
598 			ok = false;
599 		}
600 
601 		if (vma->pages != vma->obj->mm.pages) {
602 			pr_err("VMA not using object pages!\n");
603 			ok = false;
604 		}
605 	}
606 
607 	return ok;
608 }
609 
610 static int igt_vma_partial(void *arg)
611 {
612 	struct drm_i915_private *i915 = arg;
613 	struct i915_address_space *vm = &i915->ggtt.base;
614 	const unsigned int npages = 1021; /* prime! */
615 	struct drm_i915_gem_object *obj;
616 	const struct phase {
617 		const char *name;
618 	} phases[] = {
619 		{ "create" },
620 		{ "lookup" },
621 		{ },
622 	}, *p;
623 	unsigned int sz, offset;
624 	struct i915_vma *vma;
625 	int err = -ENOMEM;
626 
627 	/* Create lots of different VMA for the object and check that
628 	 * we are returned the same VMA when we later request the same range.
629 	 */
630 
631 	obj = i915_gem_object_create_internal(i915, npages*PAGE_SIZE);
632 	if (IS_ERR(obj))
633 		goto out;
634 
635 	for (p = phases; p->name; p++) { /* exercise both create/lookup */
636 		unsigned int count, nvma;
637 
638 		nvma = 0;
639 		for_each_prime_number_from(sz, 1, npages) {
640 			for_each_prime_number_from(offset, 0, npages - sz) {
641 				struct i915_ggtt_view view;
642 
643 				view.type = I915_GGTT_VIEW_PARTIAL;
644 				view.partial.offset = offset;
645 				view.partial.size = sz;
646 
647 				if (sz == npages)
648 					view.type = I915_GGTT_VIEW_NORMAL;
649 
650 				vma = checked_vma_instance(obj, vm, &view);
651 				if (IS_ERR(vma)) {
652 					err = PTR_ERR(vma);
653 					goto out_object;
654 				}
655 
656 				err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
657 				if (err)
658 					goto out_object;
659 
660 				if (!assert_pin(vma, &view, sz*PAGE_SIZE, p->name)) {
661 					pr_err("(%s) Inconsistent partial pinning for (offset=%d, size=%d)\n",
662 					       p->name, offset, sz);
663 					err = -EINVAL;
664 					goto out_object;
665 				}
666 
667 				if (!assert_partial(obj, vma, offset, sz)) {
668 					pr_err("(%s) Inconsistent partial pages for (offset=%d, size=%d)\n",
669 					       p->name, offset, sz);
670 					err = -EINVAL;
671 					goto out_object;
672 				}
673 
674 				i915_vma_unpin(vma);
675 				nvma++;
676 			}
677 		}
678 
679 		count = 0;
680 		list_for_each_entry(vma, &obj->vma_list, obj_link)
681 			count++;
682 		if (count != nvma) {
683 			pr_err("(%s) All partial vma were not recorded on the obj->vma_list: found %u, expected %u\n",
684 			       p->name, count, nvma);
685 			err = -EINVAL;
686 			goto out_object;
687 		}
688 
689 		/* Check that we did create the whole object mapping */
690 		vma = checked_vma_instance(obj, vm, NULL);
691 		if (IS_ERR(vma)) {
692 			err = PTR_ERR(vma);
693 			goto out_object;
694 		}
695 
696 		err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
697 		if (err)
698 			goto out_object;
699 
700 		if (!assert_pin(vma, NULL, obj->base.size, p->name)) {
701 			pr_err("(%s) inconsistent full pin\n", p->name);
702 			err = -EINVAL;
703 			goto out_object;
704 		}
705 
706 		i915_vma_unpin(vma);
707 
708 		count = 0;
709 		list_for_each_entry(vma, &obj->vma_list, obj_link)
710 			count++;
711 		if (count != nvma) {
712 			pr_err("(%s) allocated an extra full vma!\n", p->name);
713 			err = -EINVAL;
714 			goto out_object;
715 		}
716 	}
717 
718 out_object:
719 	i915_gem_object_put(obj);
720 out:
721 	return err;
722 }
723 
724 int i915_vma_mock_selftests(void)
725 {
726 	static const struct i915_subtest tests[] = {
727 		SUBTEST(igt_vma_create),
728 		SUBTEST(igt_vma_pin1),
729 		SUBTEST(igt_vma_rotate),
730 		SUBTEST(igt_vma_partial),
731 	};
732 	struct drm_i915_private *i915;
733 	int err;
734 
735 	i915 = mock_gem_device();
736 	if (!i915)
737 		return -ENOMEM;
738 
739 	mutex_lock(&i915->drm.struct_mutex);
740 	err = i915_subtests(tests, i915);
741 	mutex_unlock(&i915->drm.struct_mutex);
742 
743 	drm_dev_unref(&i915->drm);
744 	return err;
745 }
746 
747