blob: c8869fc217fd4276088cd7c3ef97ee9d3ea31c08 [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 Wilsona231bf62017-02-13 17:15:47 +0000355static 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
364static struct scatterlist *
365assert_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
405static 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
411static 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
527out_object:
528 i915_gem_object_put(obj);
529out:
530 return err;
531}
532
Chris Wilsone3c7a1c2017-02-13 17:15:45 +0000533int i915_vma_mock_selftests(void)
534{
535 static const struct i915_subtest tests[] = {
536 SUBTEST(igt_vma_create),
Chris Wilson782a3e92017-02-13 17:15:46 +0000537 SUBTEST(igt_vma_pin1),
Chris Wilsona231bf62017-02-13 17:15:47 +0000538 SUBTEST(igt_vma_rotate),
Chris Wilsone3c7a1c2017-02-13 17:15:45 +0000539 };
540 struct drm_i915_private *i915;
541 int err;
542
543 i915 = mock_gem_device();
544 if (!i915)
545 return -ENOMEM;
546
547 mutex_lock(&i915->drm.struct_mutex);
548 err = i915_subtests(tests, i915);
549 mutex_unlock(&i915->drm.struct_mutex);
550
551 drm_dev_unref(&i915->drm);
552 return err;
553}
554