blob: 111158f7daeb45bab3192ac11fcc49cd740a5557 [file] [log] [blame]
Chris Wilsone3c7a1c2017-02-13 17:15:45 +00001/*
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
32static 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
58static struct i915_vma *
59checked_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
102static 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
143static 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
193end:
194 /* Final pass to lookup all created contexts */
195 err = create_vmas(i915, &objects, &contexts);
196out:
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
Chris Wilson782a3e92017-02-13 17:15:46 +0000205struct 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
214static 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
228static 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
236static 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
244static 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
251static 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;
350out:
351 i915_gem_object_put(obj);
352 return err;
353}
354
Chris Wilsone3c7a1c2017-02-13 17:15:45 +0000355int i915_vma_mock_selftests(void)
356{
357 static const struct i915_subtest tests[] = {
358 SUBTEST(igt_vma_create),
Chris Wilson782a3e92017-02-13 17:15:46 +0000359 SUBTEST(igt_vma_pin1),
Chris Wilsone3c7a1c2017-02-13 17:15:45 +0000360 };
361 struct drm_i915_private *i915;
362 int err;
363
364 i915 = mock_gem_device();
365 if (!i915)
366 return -ENOMEM;
367
368 mutex_lock(&i915->drm.struct_mutex);
369 err = i915_subtests(tests, i915);
370 mutex_unlock(&i915->drm.struct_mutex);
371
372 drm_dev_unref(&i915->drm);
373 return err;
374}
375