xref: /linux/drivers/gpu/drm/i915/selftests/i915_vma.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1e3c7a1c5SChris Wilson /*
2e3c7a1c5SChris Wilson  * Copyright © 2016 Intel Corporation
3e3c7a1c5SChris Wilson  *
4e3c7a1c5SChris Wilson  * Permission is hereby granted, free of charge, to any person obtaining a
5e3c7a1c5SChris Wilson  * copy of this software and associated documentation files (the "Software"),
6e3c7a1c5SChris Wilson  * to deal in the Software without restriction, including without limitation
7e3c7a1c5SChris Wilson  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3c7a1c5SChris Wilson  * and/or sell copies of the Software, and to permit persons to whom the
9e3c7a1c5SChris Wilson  * Software is furnished to do so, subject to the following conditions:
10e3c7a1c5SChris Wilson  *
11e3c7a1c5SChris Wilson  * The above copyright notice and this permission notice (including the next
12e3c7a1c5SChris Wilson  * paragraph) shall be included in all copies or substantial portions of the
13e3c7a1c5SChris Wilson  * Software.
14e3c7a1c5SChris Wilson  *
15e3c7a1c5SChris Wilson  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3c7a1c5SChris Wilson  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3c7a1c5SChris Wilson  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3c7a1c5SChris Wilson  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3c7a1c5SChris Wilson  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3c7a1c5SChris Wilson  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e3c7a1c5SChris Wilson  * IN THE SOFTWARE.
22e3c7a1c5SChris Wilson  *
23e3c7a1c5SChris Wilson  */
24e3c7a1c5SChris Wilson 
25e3c7a1c5SChris Wilson #include <linux/prime_numbers.h>
26e3c7a1c5SChris Wilson 
27a4e7ccdaSChris Wilson #include "gem/i915_gem_context.h"
28b508d01fSJani Nikula #include "gem/i915_gem_internal.h"
2910be98a7SChris Wilson #include "gem/selftests/mock_context.h"
3010be98a7SChris Wilson 
3137d63f8fSChris Wilson #include "i915_scatterlist.h"
3210be98a7SChris Wilson #include "i915_selftest.h"
33e3c7a1c5SChris Wilson 
34e3c7a1c5SChris Wilson #include "mock_gem_device.h"
35c95e7ce3SChris Wilson #include "mock_gtt.h"
36e3c7a1c5SChris Wilson 
assert_vma(struct i915_vma * vma,struct drm_i915_gem_object * obj,struct i915_gem_context * ctx)37e3c7a1c5SChris Wilson static bool assert_vma(struct i915_vma *vma,
38e3c7a1c5SChris Wilson 		       struct drm_i915_gem_object *obj,
39e3c7a1c5SChris Wilson 		       struct i915_gem_context *ctx)
40e3c7a1c5SChris Wilson {
41e3c7a1c5SChris Wilson 	bool ok = true;
42e3c7a1c5SChris Wilson 
439ec8795eSDaniel Vetter 	if (vma->vm != ctx->vm) {
44e3c7a1c5SChris Wilson 		pr_err("VMA created with wrong VM\n");
45e3c7a1c5SChris Wilson 		ok = false;
46e3c7a1c5SChris Wilson 	}
47e3c7a1c5SChris Wilson 
48e3c7a1c5SChris Wilson 	if (vma->size != obj->base.size) {
49e3c7a1c5SChris Wilson 		pr_err("VMA created with wrong size, found %llu, expected %zu\n",
50e3c7a1c5SChris Wilson 		       vma->size, obj->base.size);
51e3c7a1c5SChris Wilson 		ok = false;
52e3c7a1c5SChris Wilson 	}
53e3c7a1c5SChris Wilson 
54*3bb6a442SNiranjana Vishwanathapura 	if (vma->gtt_view.type != I915_GTT_VIEW_NORMAL) {
55e3c7a1c5SChris Wilson 		pr_err("VMA created with wrong type [%d]\n",
56*3bb6a442SNiranjana Vishwanathapura 		       vma->gtt_view.type);
57e3c7a1c5SChris Wilson 		ok = false;
58e3c7a1c5SChris Wilson 	}
59e3c7a1c5SChris Wilson 
60e3c7a1c5SChris Wilson 	return ok;
61e3c7a1c5SChris Wilson }
62e3c7a1c5SChris Wilson 
63e3c7a1c5SChris Wilson static struct i915_vma *
checked_vma_instance(struct drm_i915_gem_object * obj,struct i915_address_space * vm,const struct i915_gtt_view * view)64e3c7a1c5SChris Wilson checked_vma_instance(struct drm_i915_gem_object *obj,
65e3c7a1c5SChris Wilson 		     struct i915_address_space *vm,
66*3bb6a442SNiranjana Vishwanathapura 		     const struct i915_gtt_view *view)
67e3c7a1c5SChris Wilson {
68e3c7a1c5SChris Wilson 	struct i915_vma *vma;
69e3c7a1c5SChris Wilson 	bool ok = true;
70e3c7a1c5SChris Wilson 
71e3c7a1c5SChris Wilson 	vma = i915_vma_instance(obj, vm, view);
72e3c7a1c5SChris Wilson 	if (IS_ERR(vma))
73e3c7a1c5SChris Wilson 		return vma;
74e3c7a1c5SChris Wilson 
75e3c7a1c5SChris Wilson 	/* Manual checks, will be reinforced by i915_vma_compare! */
76e3c7a1c5SChris Wilson 	if (vma->vm != vm) {
77e3c7a1c5SChris Wilson 		pr_err("VMA's vm [%p] does not match request [%p]\n",
78e3c7a1c5SChris Wilson 		       vma->vm, vm);
79e3c7a1c5SChris Wilson 		ok = false;
80e3c7a1c5SChris Wilson 	}
81e3c7a1c5SChris Wilson 
82e3c7a1c5SChris Wilson 	if (i915_is_ggtt(vm) != i915_vma_is_ggtt(vma)) {
83e3c7a1c5SChris Wilson 		pr_err("VMA ggtt status [%d] does not match parent [%d]\n",
84e3c7a1c5SChris Wilson 		       i915_vma_is_ggtt(vma), i915_is_ggtt(vm));
85e3c7a1c5SChris Wilson 		ok = false;
86e3c7a1c5SChris Wilson 	}
87e3c7a1c5SChris Wilson 
88e3c7a1c5SChris Wilson 	if (i915_vma_compare(vma, vm, view)) {
89dc74f6feSColin Ian King 		pr_err("i915_vma_compare failed with create parameters!\n");
90e3c7a1c5SChris Wilson 		return ERR_PTR(-EINVAL);
91e3c7a1c5SChris Wilson 	}
92e3c7a1c5SChris Wilson 
93e3c7a1c5SChris Wilson 	if (i915_vma_compare(vma, vma->vm,
94*3bb6a442SNiranjana Vishwanathapura 			     i915_vma_is_ggtt(vma) ? &vma->gtt_view : NULL)) {
95e3c7a1c5SChris Wilson 		pr_err("i915_vma_compare failed with itself\n");
96e3c7a1c5SChris Wilson 		return ERR_PTR(-EINVAL);
97e3c7a1c5SChris Wilson 	}
98e3c7a1c5SChris Wilson 
99e3c7a1c5SChris Wilson 	if (!ok) {
100e3c7a1c5SChris Wilson 		pr_err("i915_vma_compare failed to detect the difference!\n");
101e3c7a1c5SChris Wilson 		return ERR_PTR(-EINVAL);
102e3c7a1c5SChris Wilson 	}
103e3c7a1c5SChris Wilson 
104e3c7a1c5SChris Wilson 	return vma;
105e3c7a1c5SChris Wilson }
106e3c7a1c5SChris Wilson 
create_vmas(struct drm_i915_private * i915,struct list_head * objects,struct list_head * contexts)107e3c7a1c5SChris Wilson static int create_vmas(struct drm_i915_private *i915,
108e3c7a1c5SChris Wilson 		       struct list_head *objects,
109e3c7a1c5SChris Wilson 		       struct list_head *contexts)
110e3c7a1c5SChris Wilson {
111e3c7a1c5SChris Wilson 	struct drm_i915_gem_object *obj;
112e3c7a1c5SChris Wilson 	struct i915_gem_context *ctx;
113e3c7a1c5SChris Wilson 	int pinned;
114e3c7a1c5SChris Wilson 
115e3c7a1c5SChris Wilson 	list_for_each_entry(obj, objects, st_link) {
116e3c7a1c5SChris Wilson 		for (pinned = 0; pinned <= 1; pinned++) {
117e3c7a1c5SChris Wilson 			list_for_each_entry(ctx, contexts, link) {
118a4e7ccdaSChris Wilson 				struct i915_address_space *vm;
119e3c7a1c5SChris Wilson 				struct i915_vma *vma;
120e3c7a1c5SChris Wilson 				int err;
121e3c7a1c5SChris Wilson 
122c6d04e48SDaniel Vetter 				vm = i915_gem_context_get_eb_vm(ctx);
123e3c7a1c5SChris Wilson 				vma = checked_vma_instance(obj, vm, NULL);
124a4e7ccdaSChris Wilson 				i915_vm_put(vm);
125e3c7a1c5SChris Wilson 				if (IS_ERR(vma))
126e3c7a1c5SChris Wilson 					return PTR_ERR(vma);
127e3c7a1c5SChris Wilson 
128e3c7a1c5SChris Wilson 				if (!assert_vma(vma, obj, ctx)) {
129e3c7a1c5SChris Wilson 					pr_err("VMA lookup/create failed\n");
130e3c7a1c5SChris Wilson 					return -EINVAL;
131e3c7a1c5SChris Wilson 				}
132e3c7a1c5SChris Wilson 
133e3c7a1c5SChris Wilson 				if (!pinned) {
134e3c7a1c5SChris Wilson 					err = i915_vma_pin(vma, 0, 0, PIN_USER);
135e3c7a1c5SChris Wilson 					if (err) {
136e3c7a1c5SChris Wilson 						pr_err("Failed to pin VMA\n");
137e3c7a1c5SChris Wilson 						return err;
138e3c7a1c5SChris Wilson 					}
139e3c7a1c5SChris Wilson 				} else {
140e3c7a1c5SChris Wilson 					i915_vma_unpin(vma);
141e3c7a1c5SChris Wilson 				}
142e3c7a1c5SChris Wilson 			}
143e3c7a1c5SChris Wilson 		}
144e3c7a1c5SChris Wilson 	}
145e3c7a1c5SChris Wilson 
146e3c7a1c5SChris Wilson 	return 0;
147e3c7a1c5SChris Wilson }
148e3c7a1c5SChris Wilson 
igt_vma_create(void * arg)149e3c7a1c5SChris Wilson static int igt_vma_create(void *arg)
150e3c7a1c5SChris Wilson {
151c95e7ce3SChris Wilson 	struct i915_ggtt *ggtt = arg;
152c95e7ce3SChris Wilson 	struct drm_i915_private *i915 = ggtt->vm.i915;
153e3c7a1c5SChris Wilson 	struct drm_i915_gem_object *obj, *on;
154e3c7a1c5SChris Wilson 	struct i915_gem_context *ctx, *cn;
155e3c7a1c5SChris Wilson 	unsigned long num_obj, num_ctx;
156e3c7a1c5SChris Wilson 	unsigned long no, nc;
157e3c7a1c5SChris Wilson 	IGT_TIMEOUT(end_time);
158e3c7a1c5SChris Wilson 	LIST_HEAD(contexts);
159e3c7a1c5SChris Wilson 	LIST_HEAD(objects);
1606e128141SChris Wilson 	int err = -ENOMEM;
161e3c7a1c5SChris Wilson 
162e3c7a1c5SChris Wilson 	/* Exercise creating many vma amonst many objections, checking the
163e3c7a1c5SChris Wilson 	 * vma creation and lookup routines.
164e3c7a1c5SChris Wilson 	 */
165e3c7a1c5SChris Wilson 
166e3c7a1c5SChris Wilson 	no = 0;
167e3c7a1c5SChris Wilson 	for_each_prime_number(num_obj, ULONG_MAX - 1) {
168e3c7a1c5SChris Wilson 		for (; no < num_obj; no++) {
169e3c7a1c5SChris Wilson 			obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
170e3c7a1c5SChris Wilson 			if (IS_ERR(obj))
171e3c7a1c5SChris Wilson 				goto out;
172e3c7a1c5SChris Wilson 
173e3c7a1c5SChris Wilson 			list_add(&obj->st_link, &objects);
174e3c7a1c5SChris Wilson 		}
175e3c7a1c5SChris Wilson 
176e3c7a1c5SChris Wilson 		nc = 0;
1771bc6a601SChris Wilson 		for_each_prime_number(num_ctx, 2 * BITS_PER_LONG) {
178e3c7a1c5SChris Wilson 			for (; nc < num_ctx; nc++) {
179e3c7a1c5SChris Wilson 				ctx = mock_context(i915, "mock");
180e3c7a1c5SChris Wilson 				if (!ctx)
181e3c7a1c5SChris Wilson 					goto out;
182e3c7a1c5SChris Wilson 
183e3c7a1c5SChris Wilson 				list_move(&ctx->link, &contexts);
184e3c7a1c5SChris Wilson 			}
185e3c7a1c5SChris Wilson 
186e3c7a1c5SChris Wilson 			err = create_vmas(i915, &objects, &contexts);
187e3c7a1c5SChris Wilson 			if (err)
188e3c7a1c5SChris Wilson 				goto out;
189e3c7a1c5SChris Wilson 
190e3c7a1c5SChris Wilson 			if (igt_timeout(end_time,
191e3c7a1c5SChris Wilson 					"%s timed out: after %lu objects in %lu contexts\n",
192e3c7a1c5SChris Wilson 					__func__, no, nc))
193e3c7a1c5SChris Wilson 				goto end;
194e3c7a1c5SChris Wilson 		}
195e3c7a1c5SChris Wilson 
1965f09a9c8SChris Wilson 		list_for_each_entry_safe(ctx, cn, &contexts, link) {
1975f09a9c8SChris Wilson 			list_del_init(&ctx->link);
198e3c7a1c5SChris Wilson 			mock_context_close(ctx);
199e3c7a1c5SChris Wilson 		}
200d8bf0e76SChris Wilson 
201d8bf0e76SChris Wilson 		cond_resched();
2025f09a9c8SChris Wilson 	}
203e3c7a1c5SChris Wilson 
204e3c7a1c5SChris Wilson end:
205e3c7a1c5SChris Wilson 	/* Final pass to lookup all created contexts */
206e3c7a1c5SChris Wilson 	err = create_vmas(i915, &objects, &contexts);
207e3c7a1c5SChris Wilson out:
2085f09a9c8SChris Wilson 	list_for_each_entry_safe(ctx, cn, &contexts, link) {
2095f09a9c8SChris Wilson 		list_del_init(&ctx->link);
210e3c7a1c5SChris Wilson 		mock_context_close(ctx);
2115f09a9c8SChris Wilson 	}
212e3c7a1c5SChris Wilson 
213e3c7a1c5SChris Wilson 	list_for_each_entry_safe(obj, on, &objects, st_link)
214e3c7a1c5SChris Wilson 		i915_gem_object_put(obj);
215e3c7a1c5SChris Wilson 	return err;
216e3c7a1c5SChris Wilson }
217e3c7a1c5SChris Wilson 
218782a3e9eSChris Wilson struct pin_mode {
219782a3e9eSChris Wilson 	u64 size;
220782a3e9eSChris Wilson 	u64 flags;
221782a3e9eSChris Wilson 	bool (*assert)(const struct i915_vma *,
222782a3e9eSChris Wilson 		       const struct pin_mode *mode,
223782a3e9eSChris Wilson 		       int result);
224782a3e9eSChris Wilson 	const char *string;
225782a3e9eSChris Wilson };
226782a3e9eSChris Wilson 
assert_pin_valid(const struct i915_vma * vma,const struct pin_mode * mode,int result)227782a3e9eSChris Wilson static bool assert_pin_valid(const struct i915_vma *vma,
228782a3e9eSChris Wilson 			     const struct pin_mode *mode,
229782a3e9eSChris Wilson 			     int result)
230782a3e9eSChris Wilson {
231782a3e9eSChris Wilson 	if (result)
232782a3e9eSChris Wilson 		return false;
233782a3e9eSChris Wilson 
234782a3e9eSChris Wilson 	if (i915_vma_misplaced(vma, mode->size, 0, mode->flags))
235782a3e9eSChris Wilson 		return false;
236782a3e9eSChris Wilson 
237782a3e9eSChris Wilson 	return true;
238782a3e9eSChris Wilson }
239782a3e9eSChris Wilson 
240782a3e9eSChris Wilson __maybe_unused
assert_pin_enospc(const struct i915_vma * vma,const struct pin_mode * mode,int result)241782a3e9eSChris Wilson static bool assert_pin_enospc(const struct i915_vma *vma,
242782a3e9eSChris Wilson 			      const struct pin_mode *mode,
243782a3e9eSChris Wilson 			      int result)
244782a3e9eSChris Wilson {
245782a3e9eSChris Wilson 	return result == -ENOSPC;
246782a3e9eSChris Wilson }
247782a3e9eSChris Wilson 
248782a3e9eSChris Wilson __maybe_unused
assert_pin_einval(const struct i915_vma * vma,const struct pin_mode * mode,int result)249782a3e9eSChris Wilson static bool assert_pin_einval(const struct i915_vma *vma,
250782a3e9eSChris Wilson 			      const struct pin_mode *mode,
251782a3e9eSChris Wilson 			      int result)
252782a3e9eSChris Wilson {
253782a3e9eSChris Wilson 	return result == -EINVAL;
254782a3e9eSChris Wilson }
255782a3e9eSChris Wilson 
igt_vma_pin1(void * arg)256782a3e9eSChris Wilson static int igt_vma_pin1(void *arg)
257782a3e9eSChris Wilson {
258c95e7ce3SChris Wilson 	struct i915_ggtt *ggtt = arg;
259782a3e9eSChris Wilson 	const struct pin_mode modes[] = {
260782a3e9eSChris Wilson #define VALID(sz, fl) { .size = (sz), .flags = (fl), .assert = assert_pin_valid, .string = #sz ", " #fl ", (valid) " }
261782a3e9eSChris Wilson #define __INVALID(sz, fl, check, eval) { .size = (sz), .flags = (fl), .assert = (check), .string = #sz ", " #fl ", (invalid " #eval ")" }
262782a3e9eSChris Wilson #define INVALID(sz, fl) __INVALID(sz, fl, assert_pin_einval, EINVAL)
263782a3e9eSChris Wilson #define NOSPACE(sz, fl) __INVALID(sz, fl, assert_pin_enospc, ENOSPC)
264782a3e9eSChris Wilson 		VALID(0, PIN_GLOBAL),
265782a3e9eSChris Wilson 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE),
266782a3e9eSChris Wilson 
267782a3e9eSChris Wilson 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 4096),
268782a3e9eSChris Wilson 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 8192),
269c95e7ce3SChris Wilson 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
270c95e7ce3SChris Wilson 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
271c95e7ce3SChris Wilson 		VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)),
272782a3e9eSChris Wilson 
273c95e7ce3SChris Wilson 		VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)),
274c95e7ce3SChris Wilson 		INVALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | ggtt->mappable_end),
275c95e7ce3SChris Wilson 		VALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)),
276c95e7ce3SChris Wilson 		INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | ggtt->vm.total),
277782a3e9eSChris Wilson 		INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | round_down(U64_MAX, PAGE_SIZE)),
278782a3e9eSChris Wilson 
279782a3e9eSChris Wilson 		VALID(4096, PIN_GLOBAL),
280782a3e9eSChris Wilson 		VALID(8192, PIN_GLOBAL),
281c95e7ce3SChris Wilson 		VALID(ggtt->mappable_end - 4096, PIN_GLOBAL | PIN_MAPPABLE),
282c95e7ce3SChris Wilson 		VALID(ggtt->mappable_end, PIN_GLOBAL | PIN_MAPPABLE),
283c95e7ce3SChris Wilson 		NOSPACE(ggtt->mappable_end + 4096, PIN_GLOBAL | PIN_MAPPABLE),
284c95e7ce3SChris Wilson 		VALID(ggtt->vm.total - 4096, PIN_GLOBAL),
285c95e7ce3SChris Wilson 		VALID(ggtt->vm.total, PIN_GLOBAL),
286c95e7ce3SChris Wilson 		NOSPACE(ggtt->vm.total + 4096, PIN_GLOBAL),
2872889caa9SChris Wilson 		NOSPACE(round_down(U64_MAX, PAGE_SIZE), PIN_GLOBAL),
288c95e7ce3SChris Wilson 		INVALID(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)),
289c95e7ce3SChris Wilson 		INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)),
290782a3e9eSChris Wilson 		INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (round_down(U64_MAX, PAGE_SIZE) - 4096)),
291782a3e9eSChris Wilson 
292c95e7ce3SChris Wilson 		VALID(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
293782a3e9eSChris Wilson 
294782a3e9eSChris Wilson #if !IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
295782a3e9eSChris Wilson 		/* Misusing BIAS is a programming error (it is not controllable
296782a3e9eSChris Wilson 		 * from userspace) so when debugging is enabled, it explodes.
297782a3e9eSChris Wilson 		 * However, the tests are still quite interesting for checking
298782a3e9eSChris Wilson 		 * variable start, end and size.
299782a3e9eSChris Wilson 		 */
300c95e7ce3SChris Wilson 		NOSPACE(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | ggtt->mappable_end),
301c95e7ce3SChris Wilson 		NOSPACE(0, PIN_GLOBAL | PIN_OFFSET_BIAS | ggtt->vm.total),
302c95e7ce3SChris Wilson 		NOSPACE(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)),
303c95e7ce3SChris Wilson 		NOSPACE(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)),
304782a3e9eSChris Wilson #endif
305782a3e9eSChris Wilson 		{ },
306782a3e9eSChris Wilson #undef NOSPACE
307782a3e9eSChris Wilson #undef INVALID
308782a3e9eSChris Wilson #undef __INVALID
309782a3e9eSChris Wilson #undef VALID
310782a3e9eSChris Wilson 	}, *m;
311782a3e9eSChris Wilson 	struct drm_i915_gem_object *obj;
312782a3e9eSChris Wilson 	struct i915_vma *vma;
313782a3e9eSChris Wilson 	int err = -EINVAL;
314782a3e9eSChris Wilson 
315782a3e9eSChris Wilson 	/* Exercise all the weird and wonderful i915_vma_pin requests,
316782a3e9eSChris Wilson 	 * focusing on error handling of boundary conditions.
317782a3e9eSChris Wilson 	 */
318782a3e9eSChris Wilson 
319c95e7ce3SChris Wilson 	GEM_BUG_ON(!drm_mm_clean(&ggtt->vm.mm));
320782a3e9eSChris Wilson 
321c95e7ce3SChris Wilson 	obj = i915_gem_object_create_internal(ggtt->vm.i915, PAGE_SIZE);
322782a3e9eSChris Wilson 	if (IS_ERR(obj))
323782a3e9eSChris Wilson 		return PTR_ERR(obj);
324782a3e9eSChris Wilson 
325c95e7ce3SChris Wilson 	vma = checked_vma_instance(obj, &ggtt->vm, NULL);
326782a3e9eSChris Wilson 	if (IS_ERR(vma))
327782a3e9eSChris Wilson 		goto out;
328782a3e9eSChris Wilson 
329782a3e9eSChris Wilson 	for (m = modes; m->assert; m++) {
330782a3e9eSChris Wilson 		err = i915_vma_pin(vma, m->size, 0, m->flags);
331782a3e9eSChris Wilson 		if (!m->assert(vma, m, err)) {
332782a3e9eSChris Wilson 			pr_err("%s to pin single page into GGTT with mode[%d:%s]: size=%llx flags=%llx, err=%d\n",
333782a3e9eSChris Wilson 			       m->assert == assert_pin_valid ? "Failed" : "Unexpectedly succeeded",
334782a3e9eSChris Wilson 			       (int)(m - modes), m->string, m->size, m->flags,
335782a3e9eSChris Wilson 			       err);
336782a3e9eSChris Wilson 			if (!err)
337782a3e9eSChris Wilson 				i915_vma_unpin(vma);
338782a3e9eSChris Wilson 			err = -EINVAL;
339782a3e9eSChris Wilson 			goto out;
340782a3e9eSChris Wilson 		}
341782a3e9eSChris Wilson 
342782a3e9eSChris Wilson 		if (!err) {
343782a3e9eSChris Wilson 			i915_vma_unpin(vma);
3440f341974SMaarten Lankhorst 			err = i915_vma_unbind_unlocked(vma);
345782a3e9eSChris Wilson 			if (err) {
346782a3e9eSChris Wilson 				pr_err("Failed to unbind single page from GGTT, err=%d\n", err);
347782a3e9eSChris Wilson 				goto out;
348782a3e9eSChris Wilson 			}
349782a3e9eSChris Wilson 		}
350d8bf0e76SChris Wilson 
351d8bf0e76SChris Wilson 		cond_resched();
352782a3e9eSChris Wilson 	}
353782a3e9eSChris Wilson 
354782a3e9eSChris Wilson 	err = 0;
355782a3e9eSChris Wilson out:
356782a3e9eSChris Wilson 	i915_gem_object_put(obj);
357782a3e9eSChris Wilson 	return err;
358782a3e9eSChris Wilson }
359782a3e9eSChris Wilson 
rotated_index(const struct intel_rotation_info * r,unsigned int n,unsigned int x,unsigned int y)360a231bf64SChris Wilson static unsigned long rotated_index(const struct intel_rotation_info *r,
361a231bf64SChris Wilson 				   unsigned int n,
362a231bf64SChris Wilson 				   unsigned int x,
363a231bf64SChris Wilson 				   unsigned int y)
364a231bf64SChris Wilson {
3656d80f430SImre Deak 	return (r->plane[n].src_stride * (r->plane[n].height - y - 1) +
366a231bf64SChris Wilson 		r->plane[n].offset + x);
367a231bf64SChris Wilson }
368a231bf64SChris Wilson 
369a231bf64SChris Wilson static struct scatterlist *
assert_rotated(struct drm_i915_gem_object * obj,const struct intel_rotation_info * r,unsigned int n,struct scatterlist * sg)370a231bf64SChris Wilson assert_rotated(struct drm_i915_gem_object *obj,
371a231bf64SChris Wilson 	       const struct intel_rotation_info *r, unsigned int n,
372a231bf64SChris Wilson 	       struct scatterlist *sg)
373a231bf64SChris Wilson {
374a231bf64SChris Wilson 	unsigned int x, y;
375a231bf64SChris Wilson 
376a231bf64SChris Wilson 	for (x = 0; x < r->plane[n].width; x++) {
37725926cd8SImre Deak 		unsigned int left;
37825926cd8SImre Deak 
379a231bf64SChris Wilson 		for (y = 0; y < r->plane[n].height; y++) {
380a231bf64SChris Wilson 			unsigned long src_idx;
381a231bf64SChris Wilson 			dma_addr_t src;
382a231bf64SChris Wilson 
383a231bf64SChris Wilson 			if (!sg) {
384a231bf64SChris Wilson 				pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
385a231bf64SChris Wilson 				       n, x, y);
386a231bf64SChris Wilson 				return ERR_PTR(-EINVAL);
387a231bf64SChris Wilson 			}
388a231bf64SChris Wilson 
389a231bf64SChris Wilson 			src_idx = rotated_index(r, n, x, y);
390a231bf64SChris Wilson 			src = i915_gem_object_get_dma_address(obj, src_idx);
391a231bf64SChris Wilson 
392a231bf64SChris Wilson 			if (sg_dma_len(sg) != PAGE_SIZE) {
393a231bf64SChris Wilson 				pr_err("Invalid sg.length, found %d, expected %lu for rotated page (%d, %d) [src index %lu]\n",
394a231bf64SChris Wilson 				       sg_dma_len(sg), PAGE_SIZE,
395a231bf64SChris Wilson 				       x, y, src_idx);
396a231bf64SChris Wilson 				return ERR_PTR(-EINVAL);
397a231bf64SChris Wilson 			}
398a231bf64SChris Wilson 
399a231bf64SChris Wilson 			if (sg_dma_address(sg) != src) {
400a231bf64SChris Wilson 				pr_err("Invalid address for rotated page (%d, %d) [src index %lu]\n",
401a231bf64SChris Wilson 				       x, y, src_idx);
402a231bf64SChris Wilson 				return ERR_PTR(-EINVAL);
403a231bf64SChris Wilson 			}
404a231bf64SChris Wilson 
405a231bf64SChris Wilson 			sg = sg_next(sg);
406a231bf64SChris Wilson 		}
40725926cd8SImre Deak 
40825926cd8SImre Deak 		left = (r->plane[n].dst_stride - y) * PAGE_SIZE;
40925926cd8SImre Deak 
41025926cd8SImre Deak 		if (!left)
41125926cd8SImre Deak 			continue;
41225926cd8SImre Deak 
41325926cd8SImre Deak 		if (!sg) {
41425926cd8SImre Deak 			pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
41525926cd8SImre Deak 			       n, x, y);
41625926cd8SImre Deak 			return ERR_PTR(-EINVAL);
41725926cd8SImre Deak 		}
41825926cd8SImre Deak 
41925926cd8SImre Deak 		if (sg_dma_len(sg) != left) {
42025926cd8SImre Deak 			pr_err("Invalid sg.length, found %d, expected %u for rotated page (%d, %d)\n",
42125926cd8SImre Deak 			       sg_dma_len(sg), left, x, y);
42225926cd8SImre Deak 			return ERR_PTR(-EINVAL);
42325926cd8SImre Deak 		}
42425926cd8SImre Deak 
42525926cd8SImre Deak 		if (sg_dma_address(sg) != 0) {
42625926cd8SImre Deak 			pr_err("Invalid address, found %pad, expected 0 for remapped page (%d, %d)\n",
42725926cd8SImre Deak 			       &sg_dma_address(sg), x, y);
42825926cd8SImre Deak 			return ERR_PTR(-EINVAL);
42925926cd8SImre Deak 		}
43025926cd8SImre Deak 
43125926cd8SImre Deak 		sg = sg_next(sg);
432a231bf64SChris Wilson 	}
433a231bf64SChris Wilson 
434a231bf64SChris Wilson 	return sg;
435a231bf64SChris Wilson }
436a231bf64SChris Wilson 
remapped_index(const struct intel_remapped_info * r,unsigned int n,unsigned int x,unsigned int y)437e2e394bfSVille Syrjälä static unsigned long remapped_index(const struct intel_remapped_info *r,
438e2e394bfSVille Syrjälä 				    unsigned int n,
439e2e394bfSVille Syrjälä 				    unsigned int x,
440e2e394bfSVille Syrjälä 				    unsigned int y)
441e2e394bfSVille Syrjälä {
4426d80f430SImre Deak 	return (r->plane[n].src_stride * y +
443e2e394bfSVille Syrjälä 		r->plane[n].offset + x);
444e2e394bfSVille Syrjälä }
445e2e394bfSVille Syrjälä 
446e2e394bfSVille Syrjälä static struct scatterlist *
assert_remapped(struct drm_i915_gem_object * obj,const struct intel_remapped_info * r,unsigned int n,struct scatterlist * sg)447e2e394bfSVille Syrjälä assert_remapped(struct drm_i915_gem_object *obj,
448e2e394bfSVille Syrjälä 		const struct intel_remapped_info *r, unsigned int n,
449e2e394bfSVille Syrjälä 		struct scatterlist *sg)
450e2e394bfSVille Syrjälä {
451e2e394bfSVille Syrjälä 	unsigned int x, y;
452e2e394bfSVille Syrjälä 	unsigned int left = 0;
453e2e394bfSVille Syrjälä 	unsigned int offset;
454e2e394bfSVille Syrjälä 
455e2e394bfSVille Syrjälä 	for (y = 0; y < r->plane[n].height; y++) {
456e2e394bfSVille Syrjälä 		for (x = 0; x < r->plane[n].width; x++) {
457e2e394bfSVille Syrjälä 			unsigned long src_idx;
458e2e394bfSVille Syrjälä 			dma_addr_t src;
459e2e394bfSVille Syrjälä 
460e2e394bfSVille Syrjälä 			if (!sg) {
461e2e394bfSVille Syrjälä 				pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
462e2e394bfSVille Syrjälä 				       n, x, y);
463e2e394bfSVille Syrjälä 				return ERR_PTR(-EINVAL);
464e2e394bfSVille Syrjälä 			}
465e2e394bfSVille Syrjälä 			if (!left) {
466e2e394bfSVille Syrjälä 				offset = 0;
467e2e394bfSVille Syrjälä 				left = sg_dma_len(sg);
468e2e394bfSVille Syrjälä 			}
469e2e394bfSVille Syrjälä 
470e2e394bfSVille Syrjälä 			src_idx = remapped_index(r, n, x, y);
471e2e394bfSVille Syrjälä 			src = i915_gem_object_get_dma_address(obj, src_idx);
472e2e394bfSVille Syrjälä 
473e2e394bfSVille Syrjälä 			if (left < PAGE_SIZE || left & (PAGE_SIZE-1)) {
474e2e394bfSVille Syrjälä 				pr_err("Invalid sg.length, found %d, expected %lu for remapped page (%d, %d) [src index %lu]\n",
475e2e394bfSVille Syrjälä 				       sg_dma_len(sg), PAGE_SIZE,
476e2e394bfSVille Syrjälä 				       x, y, src_idx);
477e2e394bfSVille Syrjälä 				return ERR_PTR(-EINVAL);
478e2e394bfSVille Syrjälä 			}
479e2e394bfSVille Syrjälä 
480e2e394bfSVille Syrjälä 			if (sg_dma_address(sg) + offset != src) {
481e2e394bfSVille Syrjälä 				pr_err("Invalid address for remapped page (%d, %d) [src index %lu]\n",
482e2e394bfSVille Syrjälä 				       x, y, src_idx);
483e2e394bfSVille Syrjälä 				return ERR_PTR(-EINVAL);
484e2e394bfSVille Syrjälä 			}
485e2e394bfSVille Syrjälä 
486e2e394bfSVille Syrjälä 			left -= PAGE_SIZE;
487e2e394bfSVille Syrjälä 			offset += PAGE_SIZE;
488e2e394bfSVille Syrjälä 
489e2e394bfSVille Syrjälä 
490e2e394bfSVille Syrjälä 			if (!left)
491e2e394bfSVille Syrjälä 				sg = sg_next(sg);
492e2e394bfSVille Syrjälä 		}
49325926cd8SImre Deak 
49425926cd8SImre Deak 		if (left) {
49525926cd8SImre Deak 			pr_err("Unexpected sg tail with %d size for remapped page (%d, %d)\n",
49625926cd8SImre Deak 			       left,
49725926cd8SImre Deak 			       x, y);
49825926cd8SImre Deak 			return ERR_PTR(-EINVAL);
49925926cd8SImre Deak 		}
50025926cd8SImre Deak 
50125926cd8SImre Deak 		left = (r->plane[n].dst_stride - r->plane[n].width) * PAGE_SIZE;
50225926cd8SImre Deak 
50325926cd8SImre Deak 		if (!left)
50425926cd8SImre Deak 			continue;
50525926cd8SImre Deak 
50625926cd8SImre Deak 		if (!sg) {
50725926cd8SImre Deak 			pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
50825926cd8SImre Deak 			       n, x, y);
50925926cd8SImre Deak 			return ERR_PTR(-EINVAL);
51025926cd8SImre Deak 		}
51125926cd8SImre Deak 
51225926cd8SImre Deak 		if (sg_dma_len(sg) != left) {
51325926cd8SImre Deak 			pr_err("Invalid sg.length, found %u, expected %u for remapped page (%d, %d)\n",
51425926cd8SImre Deak 			       sg_dma_len(sg), left,
51525926cd8SImre Deak 			       x, y);
51625926cd8SImre Deak 			return ERR_PTR(-EINVAL);
51725926cd8SImre Deak 		}
51825926cd8SImre Deak 
51925926cd8SImre Deak 		if (sg_dma_address(sg) != 0) {
52025926cd8SImre Deak 			pr_err("Invalid address, found %pad, expected 0 for remapped page (%d, %d)\n",
52125926cd8SImre Deak 			       &sg_dma_address(sg),
52225926cd8SImre Deak 			       x, y);
52325926cd8SImre Deak 			return ERR_PTR(-EINVAL);
52425926cd8SImre Deak 		}
52525926cd8SImre Deak 
52625926cd8SImre Deak 		sg = sg_next(sg);
52725926cd8SImre Deak 		left = 0;
528e2e394bfSVille Syrjälä 	}
529e2e394bfSVille Syrjälä 
530e2e394bfSVille Syrjälä 	return sg;
531e2e394bfSVille Syrjälä }
532e2e394bfSVille Syrjälä 
remapped_size(enum i915_gtt_view_type view_type,const struct intel_remapped_plane_info * a,const struct intel_remapped_plane_info * b)533*3bb6a442SNiranjana Vishwanathapura static unsigned int remapped_size(enum i915_gtt_view_type view_type,
53425926cd8SImre Deak 				  const struct intel_remapped_plane_info *a,
5351a74fc0bSVille Syrjälä 				  const struct intel_remapped_plane_info *b)
536a231bf64SChris Wilson {
53725926cd8SImre Deak 
538*3bb6a442SNiranjana Vishwanathapura 	if (view_type == I915_GTT_VIEW_ROTATED)
53925926cd8SImre Deak 		return a->dst_stride * a->width + b->dst_stride * b->width;
54025926cd8SImre Deak 	else
54125926cd8SImre Deak 		return a->dst_stride * a->height + b->dst_stride * b->height;
542a231bf64SChris Wilson }
543a231bf64SChris Wilson 
igt_vma_rotate_remap(void * arg)544e2e394bfSVille Syrjälä static int igt_vma_rotate_remap(void *arg)
545a231bf64SChris Wilson {
546c95e7ce3SChris Wilson 	struct i915_ggtt *ggtt = arg;
547c95e7ce3SChris Wilson 	struct i915_address_space *vm = &ggtt->vm;
548a231bf64SChris Wilson 	struct drm_i915_gem_object *obj;
5491a74fc0bSVille Syrjälä 	const struct intel_remapped_plane_info planes[] = {
5506d80f430SImre Deak 		{ .width = 1, .height = 1, .src_stride = 1 },
5516d80f430SImre Deak 		{ .width = 2, .height = 2, .src_stride = 2 },
5526d80f430SImre Deak 		{ .width = 4, .height = 4, .src_stride = 4 },
5536d80f430SImre Deak 		{ .width = 8, .height = 8, .src_stride = 8 },
554a231bf64SChris Wilson 
5556d80f430SImre Deak 		{ .width = 3, .height = 5, .src_stride = 3 },
5566d80f430SImre Deak 		{ .width = 3, .height = 5, .src_stride = 4 },
5576d80f430SImre Deak 		{ .width = 3, .height = 5, .src_stride = 5 },
558a231bf64SChris Wilson 
5596d80f430SImre Deak 		{ .width = 5, .height = 3, .src_stride = 5 },
5606d80f430SImre Deak 		{ .width = 5, .height = 3, .src_stride = 7 },
5616d80f430SImre Deak 		{ .width = 5, .height = 3, .src_stride = 9 },
562a231bf64SChris Wilson 
5636d80f430SImre Deak 		{ .width = 4, .height = 6, .src_stride = 6 },
5646d80f430SImre Deak 		{ .width = 6, .height = 4, .src_stride = 6 },
56525926cd8SImre Deak 
56625926cd8SImre Deak 		{ .width = 2, .height = 2, .src_stride = 2, .dst_stride = 2 },
56725926cd8SImre Deak 		{ .width = 3, .height = 3, .src_stride = 3, .dst_stride = 4 },
56825926cd8SImre Deak 		{ .width = 5, .height = 6, .src_stride = 7, .dst_stride = 8 },
56925926cd8SImre Deak 
570a231bf64SChris Wilson 		{ }
571a231bf64SChris Wilson 	}, *a, *b;
572*3bb6a442SNiranjana Vishwanathapura 	enum i915_gtt_view_type types[] = {
573*3bb6a442SNiranjana Vishwanathapura 		I915_GTT_VIEW_ROTATED,
574*3bb6a442SNiranjana Vishwanathapura 		I915_GTT_VIEW_REMAPPED,
575e2e394bfSVille Syrjälä 		0,
576e2e394bfSVille Syrjälä 	}, *t;
577a231bf64SChris Wilson 	const unsigned int max_pages = 64;
578a231bf64SChris Wilson 	int err = -ENOMEM;
579a231bf64SChris Wilson 
580a231bf64SChris Wilson 	/* Create VMA for many different combinations of planes and check
581a231bf64SChris Wilson 	 * that the page layout within the rotated VMA match our expectations.
582a231bf64SChris Wilson 	 */
583a231bf64SChris Wilson 
584c95e7ce3SChris Wilson 	obj = i915_gem_object_create_internal(vm->i915, max_pages * PAGE_SIZE);
585a231bf64SChris Wilson 	if (IS_ERR(obj))
586a231bf64SChris Wilson 		goto out;
587a231bf64SChris Wilson 
588e2e394bfSVille Syrjälä 	for (t = types; *t; t++) {
589a231bf64SChris Wilson 	for (a = planes; a->width; a++) {
590a231bf64SChris Wilson 		for (b = planes + ARRAY_SIZE(planes); b-- != planes; ) {
591*3bb6a442SNiranjana Vishwanathapura 			struct i915_gtt_view view = {
592b05787aeSImre Deak 				.type = *t,
593b05787aeSImre Deak 				.remapped.plane[0] = *a,
594b05787aeSImre Deak 				.remapped.plane[1] = *b,
595b05787aeSImre Deak 			};
596b05787aeSImre Deak 			struct intel_remapped_plane_info *plane_info = view.remapped.plane;
597a231bf64SChris Wilson 			unsigned int n, max_offset;
598a231bf64SChris Wilson 
5996d80f430SImre Deak 			max_offset = max(plane_info[0].src_stride * plane_info[0].height,
6006d80f430SImre Deak 					 plane_info[1].src_stride * plane_info[1].height);
601a231bf64SChris Wilson 			GEM_BUG_ON(max_offset > max_pages);
602a231bf64SChris Wilson 			max_offset = max_pages - max_offset;
603a231bf64SChris Wilson 
604a4606d45SImre Deak 			if (!plane_info[0].dst_stride)
605*3bb6a442SNiranjana Vishwanathapura 				plane_info[0].dst_stride = view.type == I915_GTT_VIEW_ROTATED ?
606a4606d45SImre Deak 									plane_info[0].height :
607a4606d45SImre Deak 									plane_info[0].width;
608a4606d45SImre Deak 			if (!plane_info[1].dst_stride)
609*3bb6a442SNiranjana Vishwanathapura 				plane_info[1].dst_stride = view.type == I915_GTT_VIEW_ROTATED ?
610a4606d45SImre Deak 									plane_info[1].height :
611a4606d45SImre Deak 									plane_info[1].width;
612a4606d45SImre Deak 
613b05787aeSImre Deak 			for_each_prime_number_from(plane_info[0].offset, 0, max_offset) {
614b05787aeSImre Deak 				for_each_prime_number_from(plane_info[1].offset, 0, max_offset) {
615a231bf64SChris Wilson 					struct scatterlist *sg;
616a231bf64SChris Wilson 					struct i915_vma *vma;
617b05787aeSImre Deak 					unsigned int expected_pages;
618a231bf64SChris Wilson 
619a231bf64SChris Wilson 					vma = checked_vma_instance(obj, vm, &view);
620a231bf64SChris Wilson 					if (IS_ERR(vma)) {
621a231bf64SChris Wilson 						err = PTR_ERR(vma);
622a231bf64SChris Wilson 						goto out_object;
623a231bf64SChris Wilson 					}
624a231bf64SChris Wilson 
625a231bf64SChris Wilson 					err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
626a231bf64SChris Wilson 					if (err) {
627a231bf64SChris Wilson 						pr_err("Failed to pin VMA, err=%d\n", err);
628a231bf64SChris Wilson 						goto out_object;
629a231bf64SChris Wilson 					}
630a231bf64SChris Wilson 
63125926cd8SImre Deak 					expected_pages = remapped_size(view.type, &plane_info[0], &plane_info[1]);
632b05787aeSImre Deak 
633*3bb6a442SNiranjana Vishwanathapura 					if (view.type == I915_GTT_VIEW_ROTATED &&
634b05787aeSImre Deak 					    vma->size != expected_pages * PAGE_SIZE) {
635a231bf64SChris Wilson 						pr_err("VMA is wrong size, expected %lu, found %llu\n",
636b05787aeSImre Deak 						       PAGE_SIZE * expected_pages, vma->size);
637a231bf64SChris Wilson 						err = -EINVAL;
638a231bf64SChris Wilson 						goto out_object;
639a231bf64SChris Wilson 					}
640a231bf64SChris Wilson 
641*3bb6a442SNiranjana Vishwanathapura 					if (view.type == I915_GTT_VIEW_REMAPPED &&
642b05787aeSImre Deak 					    vma->size > expected_pages * PAGE_SIZE) {
643e2e394bfSVille Syrjälä 						pr_err("VMA is wrong size, expected %lu, found %llu\n",
644b05787aeSImre Deak 						       PAGE_SIZE * expected_pages, vma->size);
645e2e394bfSVille Syrjälä 						err = -EINVAL;
646e2e394bfSVille Syrjälä 						goto out_object;
647e2e394bfSVille Syrjälä 					}
648e2e394bfSVille Syrjälä 
649b05787aeSImre Deak 					if (vma->pages->nents > expected_pages) {
650a231bf64SChris Wilson 						pr_err("sg table is wrong sizeo, expected %u, found %u nents\n",
651b05787aeSImre Deak 						       expected_pages, vma->pages->nents);
652a231bf64SChris Wilson 						err = -EINVAL;
653a231bf64SChris Wilson 						goto out_object;
654a231bf64SChris Wilson 					}
655a231bf64SChris Wilson 
656a231bf64SChris Wilson 					if (vma->node.size < vma->size) {
657a231bf64SChris Wilson 						pr_err("VMA binding too small, expected %llu, found %llu\n",
658a231bf64SChris Wilson 						       vma->size, vma->node.size);
659a231bf64SChris Wilson 						err = -EINVAL;
660a231bf64SChris Wilson 						goto out_object;
661a231bf64SChris Wilson 					}
662a231bf64SChris Wilson 
663a231bf64SChris Wilson 					if (vma->pages == obj->mm.pages) {
664a231bf64SChris Wilson 						pr_err("VMA using unrotated object pages!\n");
665a231bf64SChris Wilson 						err = -EINVAL;
666a231bf64SChris Wilson 						goto out_object;
667a231bf64SChris Wilson 					}
668a231bf64SChris Wilson 
669a231bf64SChris Wilson 					sg = vma->pages->sgl;
670a231bf64SChris Wilson 					for (n = 0; n < ARRAY_SIZE(view.rotated.plane); n++) {
671*3bb6a442SNiranjana Vishwanathapura 						if (view.type == I915_GTT_VIEW_ROTATED)
672a231bf64SChris Wilson 							sg = assert_rotated(obj, &view.rotated, n, sg);
673e2e394bfSVille Syrjälä 						else
674e2e394bfSVille Syrjälä 							sg = assert_remapped(obj, &view.remapped, n, sg);
675a231bf64SChris Wilson 						if (IS_ERR(sg)) {
67625926cd8SImre Deak 							pr_err("Inconsistent %s VMA pages for plane %d: [(%d, %d, %d, %d, %d), (%d, %d, %d, %d, %d)]\n",
677*3bb6a442SNiranjana Vishwanathapura 							       view.type == I915_GTT_VIEW_ROTATED ?
678e2e394bfSVille Syrjälä 							       "rotated" : "remapped", n,
679b05787aeSImre Deak 							       plane_info[0].width,
680b05787aeSImre Deak 							       plane_info[0].height,
6816d80f430SImre Deak 							       plane_info[0].src_stride,
68225926cd8SImre Deak 							       plane_info[0].dst_stride,
683b05787aeSImre Deak 							       plane_info[0].offset,
684b05787aeSImre Deak 							       plane_info[1].width,
685b05787aeSImre Deak 							       plane_info[1].height,
6866d80f430SImre Deak 							       plane_info[1].src_stride,
68725926cd8SImre Deak 							       plane_info[1].dst_stride,
688b05787aeSImre Deak 							       plane_info[1].offset);
689a231bf64SChris Wilson 							err = -EINVAL;
690a231bf64SChris Wilson 							goto out_object;
691a231bf64SChris Wilson 						}
692a231bf64SChris Wilson 					}
693a231bf64SChris Wilson 
694a231bf64SChris Wilson 					i915_vma_unpin(vma);
6950f341974SMaarten Lankhorst 					err = i915_vma_unbind_unlocked(vma);
6969606ca2eSMaarten Lankhorst 					if (err) {
6979606ca2eSMaarten Lankhorst 						pr_err("Unbinding returned %i\n", err);
6989606ca2eSMaarten Lankhorst 						goto out_object;
6999606ca2eSMaarten Lankhorst 					}
700d8bf0e76SChris Wilson 					cond_resched();
701a231bf64SChris Wilson 				}
702a231bf64SChris Wilson 			}
703a231bf64SChris Wilson 		}
704a231bf64SChris Wilson 	}
705e2e394bfSVille Syrjälä 	}
706a231bf64SChris Wilson 
707a231bf64SChris Wilson out_object:
708a231bf64SChris Wilson 	i915_gem_object_put(obj);
709a231bf64SChris Wilson out:
710a231bf64SChris Wilson 	return err;
711a231bf64SChris Wilson }
712a231bf64SChris Wilson 
assert_partial(struct drm_i915_gem_object * obj,struct i915_vma * vma,unsigned long offset,unsigned long size)713af1f83a1SChris Wilson static bool assert_partial(struct drm_i915_gem_object *obj,
714af1f83a1SChris Wilson 			   struct i915_vma *vma,
715af1f83a1SChris Wilson 			   unsigned long offset,
716af1f83a1SChris Wilson 			   unsigned long size)
717af1f83a1SChris Wilson {
718af1f83a1SChris Wilson 	struct sgt_iter sgt;
719af1f83a1SChris Wilson 	dma_addr_t dma;
720af1f83a1SChris Wilson 
72131444afbSMatthew Auld 	for_each_sgt_daddr(dma, sgt, vma->pages) {
722af1f83a1SChris Wilson 		dma_addr_t src;
723af1f83a1SChris Wilson 
724af1f83a1SChris Wilson 		if (!size) {
725af1f83a1SChris Wilson 			pr_err("Partial scattergather list too long\n");
726af1f83a1SChris Wilson 			return false;
727af1f83a1SChris Wilson 		}
728af1f83a1SChris Wilson 
729af1f83a1SChris Wilson 		src = i915_gem_object_get_dma_address(obj, offset);
730af1f83a1SChris Wilson 		if (src != dma) {
731af1f83a1SChris Wilson 			pr_err("DMA mismatch for partial page offset %lu\n",
732af1f83a1SChris Wilson 			       offset);
733af1f83a1SChris Wilson 			return false;
734af1f83a1SChris Wilson 		}
735af1f83a1SChris Wilson 
736af1f83a1SChris Wilson 		offset++;
737af1f83a1SChris Wilson 		size--;
738af1f83a1SChris Wilson 	}
739af1f83a1SChris Wilson 
740af1f83a1SChris Wilson 	return true;
741af1f83a1SChris Wilson }
742af1f83a1SChris Wilson 
assert_pin(struct i915_vma * vma,struct i915_gtt_view * view,u64 size,const char * name)743af1f83a1SChris Wilson static bool assert_pin(struct i915_vma *vma,
744*3bb6a442SNiranjana Vishwanathapura 		       struct i915_gtt_view *view,
745af1f83a1SChris Wilson 		       u64 size,
746af1f83a1SChris Wilson 		       const char *name)
747af1f83a1SChris Wilson {
748af1f83a1SChris Wilson 	bool ok = true;
749af1f83a1SChris Wilson 
750af1f83a1SChris Wilson 	if (vma->size != size) {
751af1f83a1SChris Wilson 		pr_err("(%s) VMA is wrong size, expected %llu, found %llu\n",
752af1f83a1SChris Wilson 		       name, size, vma->size);
753af1f83a1SChris Wilson 		ok = false;
754af1f83a1SChris Wilson 	}
755af1f83a1SChris Wilson 
756af1f83a1SChris Wilson 	if (vma->node.size < vma->size) {
757af1f83a1SChris Wilson 		pr_err("(%s) VMA binding too small, expected %llu, found %llu\n",
758af1f83a1SChris Wilson 		       name, vma->size, vma->node.size);
759af1f83a1SChris Wilson 		ok = false;
760af1f83a1SChris Wilson 	}
761af1f83a1SChris Wilson 
762*3bb6a442SNiranjana Vishwanathapura 	if (view && view->type != I915_GTT_VIEW_NORMAL) {
763*3bb6a442SNiranjana Vishwanathapura 		if (memcmp(&vma->gtt_view, view, sizeof(*view))) {
764af1f83a1SChris Wilson 			pr_err("(%s) VMA mismatch upon creation!\n",
765af1f83a1SChris Wilson 			       name);
766af1f83a1SChris Wilson 			ok = false;
767af1f83a1SChris Wilson 		}
768af1f83a1SChris Wilson 
769af1f83a1SChris Wilson 		if (vma->pages == vma->obj->mm.pages) {
770af1f83a1SChris Wilson 			pr_err("(%s) VMA using original object pages!\n",
771af1f83a1SChris Wilson 			       name);
772af1f83a1SChris Wilson 			ok = false;
773af1f83a1SChris Wilson 		}
774af1f83a1SChris Wilson 	} else {
775*3bb6a442SNiranjana Vishwanathapura 		if (vma->gtt_view.type != I915_GTT_VIEW_NORMAL) {
776af1f83a1SChris Wilson 			pr_err("Not the normal ggtt view! Found %d\n",
777*3bb6a442SNiranjana Vishwanathapura 			       vma->gtt_view.type);
778af1f83a1SChris Wilson 			ok = false;
779af1f83a1SChris Wilson 		}
780af1f83a1SChris Wilson 
781af1f83a1SChris Wilson 		if (vma->pages != vma->obj->mm.pages) {
782af1f83a1SChris Wilson 			pr_err("VMA not using object pages!\n");
783af1f83a1SChris Wilson 			ok = false;
784af1f83a1SChris Wilson 		}
785af1f83a1SChris Wilson 	}
786af1f83a1SChris Wilson 
787af1f83a1SChris Wilson 	return ok;
788af1f83a1SChris Wilson }
789af1f83a1SChris Wilson 
igt_vma_partial(void * arg)790af1f83a1SChris Wilson static int igt_vma_partial(void *arg)
791af1f83a1SChris Wilson {
792c95e7ce3SChris Wilson 	struct i915_ggtt *ggtt = arg;
793c95e7ce3SChris Wilson 	struct i915_address_space *vm = &ggtt->vm;
794af1f83a1SChris Wilson 	const unsigned int npages = 1021; /* prime! */
795af1f83a1SChris Wilson 	struct drm_i915_gem_object *obj;
796af1f83a1SChris Wilson 	const struct phase {
797af1f83a1SChris Wilson 		const char *name;
798af1f83a1SChris Wilson 	} phases[] = {
799af1f83a1SChris Wilson 		{ "create" },
800af1f83a1SChris Wilson 		{ "lookup" },
801af1f83a1SChris Wilson 		{ },
802af1f83a1SChris Wilson 	}, *p;
803af1f83a1SChris Wilson 	unsigned int sz, offset;
804af1f83a1SChris Wilson 	struct i915_vma *vma;
805af1f83a1SChris Wilson 	int err = -ENOMEM;
806af1f83a1SChris Wilson 
807af1f83a1SChris Wilson 	/* Create lots of different VMA for the object and check that
808af1f83a1SChris Wilson 	 * we are returned the same VMA when we later request the same range.
809af1f83a1SChris Wilson 	 */
810af1f83a1SChris Wilson 
811c95e7ce3SChris Wilson 	obj = i915_gem_object_create_internal(vm->i915, npages * PAGE_SIZE);
812af1f83a1SChris Wilson 	if (IS_ERR(obj))
813af1f83a1SChris Wilson 		goto out;
814af1f83a1SChris Wilson 
815af1f83a1SChris Wilson 	for (p = phases; p->name; p++) { /* exercise both create/lookup */
816af1f83a1SChris Wilson 		unsigned int count, nvma;
817af1f83a1SChris Wilson 
818af1f83a1SChris Wilson 		nvma = 0;
819af1f83a1SChris Wilson 		for_each_prime_number_from(sz, 1, npages) {
820af1f83a1SChris Wilson 			for_each_prime_number_from(offset, 0, npages - sz) {
821*3bb6a442SNiranjana Vishwanathapura 				struct i915_gtt_view view;
822af1f83a1SChris Wilson 
823*3bb6a442SNiranjana Vishwanathapura 				view.type = I915_GTT_VIEW_PARTIAL;
824af1f83a1SChris Wilson 				view.partial.offset = offset;
825af1f83a1SChris Wilson 				view.partial.size = sz;
826af1f83a1SChris Wilson 
827af1f83a1SChris Wilson 				if (sz == npages)
828*3bb6a442SNiranjana Vishwanathapura 					view.type = I915_GTT_VIEW_NORMAL;
829af1f83a1SChris Wilson 
830af1f83a1SChris Wilson 				vma = checked_vma_instance(obj, vm, &view);
831af1f83a1SChris Wilson 				if (IS_ERR(vma)) {
832af1f83a1SChris Wilson 					err = PTR_ERR(vma);
833af1f83a1SChris Wilson 					goto out_object;
834af1f83a1SChris Wilson 				}
835af1f83a1SChris Wilson 
836af1f83a1SChris Wilson 				err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
837af1f83a1SChris Wilson 				if (err)
838af1f83a1SChris Wilson 					goto out_object;
839af1f83a1SChris Wilson 
840af1f83a1SChris Wilson 				if (!assert_pin(vma, &view, sz*PAGE_SIZE, p->name)) {
841af1f83a1SChris Wilson 					pr_err("(%s) Inconsistent partial pinning for (offset=%d, size=%d)\n",
842af1f83a1SChris Wilson 					       p->name, offset, sz);
843af1f83a1SChris Wilson 					err = -EINVAL;
844af1f83a1SChris Wilson 					goto out_object;
845af1f83a1SChris Wilson 				}
846af1f83a1SChris Wilson 
847af1f83a1SChris Wilson 				if (!assert_partial(obj, vma, offset, sz)) {
848af1f83a1SChris Wilson 					pr_err("(%s) Inconsistent partial pages for (offset=%d, size=%d)\n",
849af1f83a1SChris Wilson 					       p->name, offset, sz);
850af1f83a1SChris Wilson 					err = -EINVAL;
851af1f83a1SChris Wilson 					goto out_object;
852af1f83a1SChris Wilson 				}
853af1f83a1SChris Wilson 
854af1f83a1SChris Wilson 				i915_vma_unpin(vma);
855af1f83a1SChris Wilson 				nvma++;
8560f341974SMaarten Lankhorst 				err = i915_vma_unbind_unlocked(vma);
8579606ca2eSMaarten Lankhorst 				if (err) {
8589606ca2eSMaarten Lankhorst 					pr_err("Unbinding returned %i\n", err);
8599606ca2eSMaarten Lankhorst 					goto out_object;
8609606ca2eSMaarten Lankhorst 				}
861d8bf0e76SChris Wilson 
862d8bf0e76SChris Wilson 				cond_resched();
863af1f83a1SChris Wilson 			}
864af1f83a1SChris Wilson 		}
865af1f83a1SChris Wilson 
866af1f83a1SChris Wilson 		count = 0;
867528cbd17SChris Wilson 		list_for_each_entry(vma, &obj->vma.list, obj_link)
868af1f83a1SChris Wilson 			count++;
869af1f83a1SChris Wilson 		if (count != nvma) {
870af1f83a1SChris Wilson 			pr_err("(%s) All partial vma were not recorded on the obj->vma_list: found %u, expected %u\n",
871af1f83a1SChris Wilson 			       p->name, count, nvma);
872af1f83a1SChris Wilson 			err = -EINVAL;
873af1f83a1SChris Wilson 			goto out_object;
874af1f83a1SChris Wilson 		}
875af1f83a1SChris Wilson 
876af1f83a1SChris Wilson 		/* Check that we did create the whole object mapping */
877af1f83a1SChris Wilson 		vma = checked_vma_instance(obj, vm, NULL);
878af1f83a1SChris Wilson 		if (IS_ERR(vma)) {
879af1f83a1SChris Wilson 			err = PTR_ERR(vma);
880af1f83a1SChris Wilson 			goto out_object;
881af1f83a1SChris Wilson 		}
882af1f83a1SChris Wilson 
883af1f83a1SChris Wilson 		err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
884af1f83a1SChris Wilson 		if (err)
885af1f83a1SChris Wilson 			goto out_object;
886af1f83a1SChris Wilson 
887af1f83a1SChris Wilson 		if (!assert_pin(vma, NULL, obj->base.size, p->name)) {
888af1f83a1SChris Wilson 			pr_err("(%s) inconsistent full pin\n", p->name);
889af1f83a1SChris Wilson 			err = -EINVAL;
890af1f83a1SChris Wilson 			goto out_object;
891af1f83a1SChris Wilson 		}
892af1f83a1SChris Wilson 
893af1f83a1SChris Wilson 		i915_vma_unpin(vma);
894af1f83a1SChris Wilson 
8950f341974SMaarten Lankhorst 		err = i915_vma_unbind_unlocked(vma);
8969606ca2eSMaarten Lankhorst 		if (err) {
8979606ca2eSMaarten Lankhorst 			pr_err("Unbinding returned %i\n", err);
8989606ca2eSMaarten Lankhorst 			goto out_object;
8999606ca2eSMaarten Lankhorst 		}
9009606ca2eSMaarten Lankhorst 
901af1f83a1SChris Wilson 		count = 0;
902528cbd17SChris Wilson 		list_for_each_entry(vma, &obj->vma.list, obj_link)
903af1f83a1SChris Wilson 			count++;
904af1f83a1SChris Wilson 		if (count != nvma) {
905af1f83a1SChris Wilson 			pr_err("(%s) allocated an extra full vma!\n", p->name);
906af1f83a1SChris Wilson 			err = -EINVAL;
907af1f83a1SChris Wilson 			goto out_object;
908af1f83a1SChris Wilson 		}
909af1f83a1SChris Wilson 	}
910af1f83a1SChris Wilson 
911af1f83a1SChris Wilson out_object:
912af1f83a1SChris Wilson 	i915_gem_object_put(obj);
913af1f83a1SChris Wilson out:
914af1f83a1SChris Wilson 	return err;
915af1f83a1SChris Wilson }
916af1f83a1SChris Wilson 
i915_vma_mock_selftests(void)917e3c7a1c5SChris Wilson int i915_vma_mock_selftests(void)
918e3c7a1c5SChris Wilson {
919e3c7a1c5SChris Wilson 	static const struct i915_subtest tests[] = {
920e3c7a1c5SChris Wilson 		SUBTEST(igt_vma_create),
921782a3e9eSChris Wilson 		SUBTEST(igt_vma_pin1),
922e2e394bfSVille Syrjälä 		SUBTEST(igt_vma_rotate_remap),
923af1f83a1SChris Wilson 		SUBTEST(igt_vma_partial),
924e3c7a1c5SChris Wilson 	};
925e3c7a1c5SChris Wilson 	struct drm_i915_private *i915;
926cdeea858SAndi Shyti 	struct intel_gt *gt;
927e3c7a1c5SChris Wilson 	int err;
928e3c7a1c5SChris Wilson 
929e3c7a1c5SChris Wilson 	i915 = mock_gem_device();
930e3c7a1c5SChris Wilson 	if (!i915)
931e3c7a1c5SChris Wilson 		return -ENOMEM;
932e3c7a1c5SChris Wilson 
933cdeea858SAndi Shyti 	/* allocate the ggtt */
934cdeea858SAndi Shyti 	err = intel_gt_assign_ggtt(to_gt(i915));
935cdeea858SAndi Shyti 	if (err)
93683e3a215SChris Wilson 		goto out_put;
937c95e7ce3SChris Wilson 
938cdeea858SAndi Shyti 	gt = to_gt(i915);
939cdeea858SAndi Shyti 
940cdeea858SAndi Shyti 	mock_init_ggtt(gt);
941cdeea858SAndi Shyti 
942cdeea858SAndi Shyti 	err = i915_subtests(tests, gt->ggtt);
9432850748eSChris Wilson 
944c95e7ce3SChris Wilson 	mock_device_flush(i915);
945c95e7ce3SChris Wilson 	i915_gem_drain_freed_objects(i915);
946cdeea858SAndi Shyti 	mock_fini_ggtt(gt->ggtt);
947cdeea858SAndi Shyti 
94883e3a215SChris Wilson out_put:
94982be0d75SDaniel Vetter 	mock_destroy_device(i915);
950e3c7a1c5SChris Wilson 	return err;
951e3c7a1c5SChris Wilson }
952bb211c3dSVille Syrjälä 
igt_vma_remapped_gtt(void * arg)953bb211c3dSVille Syrjälä static int igt_vma_remapped_gtt(void *arg)
954bb211c3dSVille Syrjälä {
955bb211c3dSVille Syrjälä 	struct drm_i915_private *i915 = arg;
956bb211c3dSVille Syrjälä 	const struct intel_remapped_plane_info planes[] = {
9576d80f430SImre Deak 		{ .width = 1, .height = 1, .src_stride = 1 },
9586d80f430SImre Deak 		{ .width = 2, .height = 2, .src_stride = 2 },
9596d80f430SImre Deak 		{ .width = 4, .height = 4, .src_stride = 4 },
9606d80f430SImre Deak 		{ .width = 8, .height = 8, .src_stride = 8 },
961bb211c3dSVille Syrjälä 
9626d80f430SImre Deak 		{ .width = 3, .height = 5, .src_stride = 3 },
9636d80f430SImre Deak 		{ .width = 3, .height = 5, .src_stride = 4 },
9646d80f430SImre Deak 		{ .width = 3, .height = 5, .src_stride = 5 },
965bb211c3dSVille Syrjälä 
9666d80f430SImre Deak 		{ .width = 5, .height = 3, .src_stride = 5 },
9676d80f430SImre Deak 		{ .width = 5, .height = 3, .src_stride = 7 },
9686d80f430SImre Deak 		{ .width = 5, .height = 3, .src_stride = 9 },
969bb211c3dSVille Syrjälä 
9706d80f430SImre Deak 		{ .width = 4, .height = 6, .src_stride = 6 },
9716d80f430SImre Deak 		{ .width = 6, .height = 4, .src_stride = 6 },
97225926cd8SImre Deak 
97325926cd8SImre Deak 		{ .width = 2, .height = 2, .src_stride = 2, .dst_stride = 2 },
97425926cd8SImre Deak 		{ .width = 3, .height = 3, .src_stride = 3, .dst_stride = 4 },
97525926cd8SImre Deak 		{ .width = 5, .height = 6, .src_stride = 7, .dst_stride = 8 },
97625926cd8SImre Deak 
977bb211c3dSVille Syrjälä 		{ }
978bb211c3dSVille Syrjälä 	}, *p;
979*3bb6a442SNiranjana Vishwanathapura 	enum i915_gtt_view_type types[] = {
980*3bb6a442SNiranjana Vishwanathapura 		I915_GTT_VIEW_ROTATED,
981*3bb6a442SNiranjana Vishwanathapura 		I915_GTT_VIEW_REMAPPED,
982bb211c3dSVille Syrjälä 		0,
983bb211c3dSVille Syrjälä 	}, *t;
984bb211c3dSVille Syrjälä 	struct drm_i915_gem_object *obj;
985bb211c3dSVille Syrjälä 	intel_wakeref_t wakeref;
986bb211c3dSVille Syrjälä 	int err = 0;
987bb211c3dSVille Syrjälä 
98817190a34SMichał Winiarski 	if (!i915_ggtt_has_aperture(to_gt(i915)->ggtt))
989f44b733eSChris Wilson 		return 0;
990f44b733eSChris Wilson 
991bb211c3dSVille Syrjälä 	obj = i915_gem_object_create_internal(i915, 10 * 10 * PAGE_SIZE);
992bb211c3dSVille Syrjälä 	if (IS_ERR(obj))
993bb211c3dSVille Syrjälä 		return PTR_ERR(obj);
994bb211c3dSVille Syrjälä 
995d858d569SDaniele Ceraolo Spurio 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
996bb211c3dSVille Syrjälä 
997bb211c3dSVille Syrjälä 	for (t = types; *t; t++) {
998bb211c3dSVille Syrjälä 		for (p = planes; p->width; p++) {
999*3bb6a442SNiranjana Vishwanathapura 			struct i915_gtt_view view = {
1000bb211c3dSVille Syrjälä 				.type = *t,
1001bb211c3dSVille Syrjälä 				.rotated.plane[0] = *p,
1002bb211c3dSVille Syrjälä 			};
1003b05787aeSImre Deak 			struct intel_remapped_plane_info *plane_info = view.rotated.plane;
1004bb211c3dSVille Syrjälä 			struct i915_vma *vma;
1005bb211c3dSVille Syrjälä 			u32 __iomem *map;
1006bb211c3dSVille Syrjälä 			unsigned int x, y;
1007bb211c3dSVille Syrjälä 
100880f0b679SMaarten Lankhorst 			i915_gem_object_lock(obj, NULL);
1009bb211c3dSVille Syrjälä 			err = i915_gem_object_set_to_gtt_domain(obj, true);
10106951e589SChris Wilson 			i915_gem_object_unlock(obj);
1011bb211c3dSVille Syrjälä 			if (err)
1012bb211c3dSVille Syrjälä 				goto out;
1013bb211c3dSVille Syrjälä 
1014a4606d45SImre Deak 			if (!plane_info[0].dst_stride)
1015*3bb6a442SNiranjana Vishwanathapura 				plane_info[0].dst_stride = *t == I915_GTT_VIEW_ROTATED ?
1016a4606d45SImre Deak 								 p->height : p->width;
1017a4606d45SImre Deak 
1018bb211c3dSVille Syrjälä 			vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1019bb211c3dSVille Syrjälä 			if (IS_ERR(vma)) {
1020bb211c3dSVille Syrjälä 				err = PTR_ERR(vma);
1021bb211c3dSVille Syrjälä 				goto out;
1022bb211c3dSVille Syrjälä 			}
1023bb211c3dSVille Syrjälä 
1024*3bb6a442SNiranjana Vishwanathapura 			GEM_BUG_ON(vma->gtt_view.type != *t);
1025bb211c3dSVille Syrjälä 
1026bb211c3dSVille Syrjälä 			map = i915_vma_pin_iomap(vma);
1027bb211c3dSVille Syrjälä 			i915_vma_unpin(vma);
1028bb211c3dSVille Syrjälä 			if (IS_ERR(map)) {
1029bb211c3dSVille Syrjälä 				err = PTR_ERR(map);
1030bb211c3dSVille Syrjälä 				goto out;
1031bb211c3dSVille Syrjälä 			}
1032bb211c3dSVille Syrjälä 
1033b05787aeSImre Deak 			for (y = 0 ; y < plane_info[0].height; y++) {
1034b05787aeSImre Deak 				for (x = 0 ; x < plane_info[0].width; x++) {
1035bb211c3dSVille Syrjälä 					unsigned int offset;
1036bb211c3dSVille Syrjälä 					u32 val = y << 16 | x;
1037bb211c3dSVille Syrjälä 
1038*3bb6a442SNiranjana Vishwanathapura 					if (*t == I915_GTT_VIEW_ROTATED)
103925926cd8SImre Deak 						offset = (x * plane_info[0].dst_stride + y) * PAGE_SIZE;
1040bb211c3dSVille Syrjälä 					else
104125926cd8SImre Deak 						offset = (y * plane_info[0].dst_stride + x) * PAGE_SIZE;
1042bb211c3dSVille Syrjälä 
1043bb211c3dSVille Syrjälä 					iowrite32(val, &map[offset / sizeof(*map)]);
1044bb211c3dSVille Syrjälä 				}
1045bb211c3dSVille Syrjälä 			}
1046bb211c3dSVille Syrjälä 
1047bb211c3dSVille Syrjälä 			i915_vma_unpin_iomap(vma);
1048bb211c3dSVille Syrjälä 
1049bb211c3dSVille Syrjälä 			vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
1050bb211c3dSVille Syrjälä 			if (IS_ERR(vma)) {
1051bb211c3dSVille Syrjälä 				err = PTR_ERR(vma);
1052bb211c3dSVille Syrjälä 				goto out;
1053bb211c3dSVille Syrjälä 			}
1054bb211c3dSVille Syrjälä 
1055*3bb6a442SNiranjana Vishwanathapura 			GEM_BUG_ON(vma->gtt_view.type != I915_GTT_VIEW_NORMAL);
1056bb211c3dSVille Syrjälä 
1057bb211c3dSVille Syrjälä 			map = i915_vma_pin_iomap(vma);
1058bb211c3dSVille Syrjälä 			i915_vma_unpin(vma);
1059bb211c3dSVille Syrjälä 			if (IS_ERR(map)) {
1060bb211c3dSVille Syrjälä 				err = PTR_ERR(map);
1061bb211c3dSVille Syrjälä 				goto out;
1062bb211c3dSVille Syrjälä 			}
1063bb211c3dSVille Syrjälä 
1064b05787aeSImre Deak 			for (y = 0 ; y < plane_info[0].height; y++) {
1065b05787aeSImre Deak 				for (x = 0 ; x < plane_info[0].width; x++) {
1066bb211c3dSVille Syrjälä 					unsigned int offset, src_idx;
1067bb211c3dSVille Syrjälä 					u32 exp = y << 16 | x;
1068bb211c3dSVille Syrjälä 					u32 val;
1069bb211c3dSVille Syrjälä 
1070*3bb6a442SNiranjana Vishwanathapura 					if (*t == I915_GTT_VIEW_ROTATED)
1071bb211c3dSVille Syrjälä 						src_idx = rotated_index(&view.rotated, 0, x, y);
1072bb211c3dSVille Syrjälä 					else
1073bb211c3dSVille Syrjälä 						src_idx = remapped_index(&view.remapped, 0, x, y);
1074bb211c3dSVille Syrjälä 					offset = src_idx * PAGE_SIZE;
1075bb211c3dSVille Syrjälä 
1076bb211c3dSVille Syrjälä 					val = ioread32(&map[offset / sizeof(*map)]);
1077bb211c3dSVille Syrjälä 					if (val != exp) {
1078bb211c3dSVille Syrjälä 						pr_err("%s VMA write test failed, expected 0x%x, found 0x%x\n",
1079*3bb6a442SNiranjana Vishwanathapura 						       *t == I915_GTT_VIEW_ROTATED ? "Rotated" : "Remapped",
1080911e0332SImre Deak 						       exp, val);
1081bb211c3dSVille Syrjälä 						i915_vma_unpin_iomap(vma);
108269e331b4SImre Deak 						err = -EINVAL;
1083bb211c3dSVille Syrjälä 						goto out;
1084bb211c3dSVille Syrjälä 					}
1085bb211c3dSVille Syrjälä 				}
1086bb211c3dSVille Syrjälä 			}
1087bb211c3dSVille Syrjälä 			i915_vma_unpin_iomap(vma);
1088d8bf0e76SChris Wilson 
1089d8bf0e76SChris Wilson 			cond_resched();
1090bb211c3dSVille Syrjälä 		}
1091bb211c3dSVille Syrjälä 	}
1092bb211c3dSVille Syrjälä 
1093bb211c3dSVille Syrjälä out:
1094d858d569SDaniele Ceraolo Spurio 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1095bb211c3dSVille Syrjälä 	i915_gem_object_put(obj);
1096bb211c3dSVille Syrjälä 
1097bb211c3dSVille Syrjälä 	return err;
1098bb211c3dSVille Syrjälä }
1099bb211c3dSVille Syrjälä 
i915_vma_live_selftests(struct drm_i915_private * i915)1100bb211c3dSVille Syrjälä int i915_vma_live_selftests(struct drm_i915_private *i915)
1101bb211c3dSVille Syrjälä {
1102bb211c3dSVille Syrjälä 	static const struct i915_subtest tests[] = {
1103bb211c3dSVille Syrjälä 		SUBTEST(igt_vma_remapped_gtt),
1104bb211c3dSVille Syrjälä 	};
1105bb211c3dSVille Syrjälä 
110661faec5fSMatthew Brost 	return i915_live_subtests(tests, i915);
1107bb211c3dSVille Syrjälä }
1108