2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #include <linux/prime_numbers.h>
27 #include "../i915_selftest.h"
29 #include "mock_gem_device.h"
30 #include "mock_context.h"
32 static bool assert_vma(struct i915_vma *vma,
33 struct drm_i915_gem_object *obj,
34 struct i915_gem_context *ctx)
38 if (vma->vm != &ctx->ppgtt->base) {
39 pr_err("VMA created with wrong VM\n");
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);
49 if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) {
50 pr_err("VMA created with wrong type [%d]\n",
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)
66 vma = i915_vma_instance(obj, vm, view);
70 /* Manual checks, will be reinforced by i915_vma_compare! */
72 pr_err("VMA's vm [%p] does not match request [%p]\n",
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));
83 if (i915_vma_compare(vma, vm, view)) {
84 pr_err("i915_vma_compare failed with create parmaters!\n");
85 return ERR_PTR(-EINVAL);
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);
95 pr_err("i915_vma_compare failed to detect the difference!\n");
96 return ERR_PTR(-EINVAL);
102 static int create_vmas(struct drm_i915_private *i915,
103 struct list_head *objects,
104 struct list_head *contexts)
106 struct drm_i915_gem_object *obj;
107 struct i915_gem_context *ctx;
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 =
115 struct i915_vma *vma;
118 vma = checked_vma_instance(obj, vm, NULL);
122 if (!assert_vma(vma, obj, ctx)) {
123 pr_err("VMA lookup/create failed\n");
128 err = i915_vma_pin(vma, 0, 0, PIN_USER);
130 pr_err("Failed to pin VMA\n");
143 static int igt_vma_create(void *arg)
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);
155 /* Exercise creating many vma amonst many objections, checking the
156 * vma creation and lookup routines.
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);
166 list_add(&obj->st_link, &objects);
170 for_each_prime_number(num_ctx, MAX_CONTEXT_HW_ID) {
171 for (; nc < num_ctx; nc++) {
172 ctx = mock_context(i915, "mock");
176 list_move(&ctx->link, &contexts);
179 err = create_vmas(i915, &objects, &contexts);
183 if (igt_timeout(end_time,
184 "%s timed out: after %lu objects in %lu contexts\n",
189 list_for_each_entry_safe(ctx, cn, &contexts, link)
190 mock_context_close(ctx);
194 /* Final pass to lookup all created contexts */
195 err = create_vmas(i915, &objects, &contexts);
197 list_for_each_entry_safe(ctx, cn, &contexts, link)
198 mock_context_close(ctx);
200 list_for_each_entry_safe(obj, on, &objects, st_link)
201 i915_gem_object_put(obj);
208 bool (*assert)(const struct i915_vma *,
209 const struct pin_mode *mode,
214 static bool assert_pin_valid(const struct i915_vma *vma,
215 const struct pin_mode *mode,
221 if (i915_vma_misplaced(vma, mode->size, 0, mode->flags))
228 static bool assert_pin_e2big(const struct i915_vma *vma,
229 const struct pin_mode *mode,
232 return result == -E2BIG;
236 static bool assert_pin_enospc(const struct i915_vma *vma,
237 const struct pin_mode *mode,
240 return result == -ENOSPC;
244 static bool assert_pin_einval(const struct i915_vma *vma,
245 const struct pin_mode *mode,
248 return result == -EINVAL;
251 static int igt_vma_pin1(void *arg)
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),
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)),
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)),
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)),
288 VALID(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (i915->ggtt.mappable_end - 4096)),
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.
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)),
308 struct drm_i915_gem_object *obj;
309 struct i915_vma *vma;
312 /* Exercise all the weird and wonderful i915_vma_pin requests,
313 * focusing on error handling of boundary conditions.
316 GEM_BUG_ON(!drm_mm_clean(&i915->ggtt.base.mm));
318 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
322 vma = checked_vma_instance(obj, &i915->ggtt.base, NULL);
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,
341 err = i915_vma_unbind(vma);
343 pr_err("Failed to unbind single page from GGTT, err=%d\n", err);
351 i915_gem_object_put(obj);
355 static unsigned long rotated_index(const struct intel_rotation_info *r,
360 return (r->plane[n].stride * (r->plane[n].height - y - 1) +
361 r->plane[n].offset + x);
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)
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;
377 pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n",
379 return ERR_PTR(-EINVAL);
382 src_idx = rotated_index(r, n, x, y);
383 src = i915_gem_object_get_dma_address(obj, src_idx);
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,
389 return ERR_PTR(-EINVAL);
392 if (sg_dma_address(sg) != src) {
393 pr_err("Invalid address for rotated page (%d, %d) [src index %lu]\n",
395 return ERR_PTR(-EINVAL);
405 static unsigned int rotated_size(const struct intel_rotation_plane_info *a,
406 const struct intel_rotation_plane_info *b)
408 return a->width * a->height + b->width * b->height;
411 static int igt_vma_rotate(void *arg)
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 },
422 { .width = 3, .height = 5, .stride = 3 },
423 { .width = 3, .height = 5, .stride = 4 },
424 { .width = 3, .height = 5, .stride = 5 },
426 { .width = 5, .height = 3, .stride = 5 },
427 { .width = 5, .height = 3, .stride = 7 },
428 { .width = 5, .height = 3, .stride = 9 },
430 { .width = 4, .height = 6, .stride = 6 },
431 { .width = 6, .height = 4, .stride = 6 },
434 const unsigned int max_pages = 64;
437 /* Create VMA for many different combinations of planes and check
438 * that the page layout within the rotated VMA match our expectations.
441 obj = i915_gem_object_create_internal(i915, max_pages * PAGE_SIZE);
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;
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;
455 view.type = I915_GGTT_VIEW_ROTATED;
456 view.rotated.plane[0] = *a;
457 view.rotated.plane[1] = *b;
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;
464 vma = checked_vma_instance(obj, vm, &view);
470 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
472 pr_err("Failed to pin VMA, err=%d\n", err);
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);
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);
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);
497 if (vma->pages == obj->mm.pages) {
498 pr_err("VMA using unrotated object pages!\n");
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);
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);
528 i915_gem_object_put(obj);
533 static bool assert_partial(struct drm_i915_gem_object *obj,
534 struct i915_vma *vma,
535 unsigned long offset,
541 for_each_sgt_dma(dma, sgt, vma->pages) {
545 pr_err("Partial scattergather list too long\n");
549 src = i915_gem_object_get_dma_address(obj, offset);
551 pr_err("DMA mismatch for partial page offset %lu\n",
563 static bool assert_pin(struct i915_vma *vma,
564 struct i915_ggtt_view *view,
570 if (vma->size != size) {
571 pr_err("(%s) VMA is wrong size, expected %llu, found %llu\n",
572 name, size, vma->size);
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);
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",
589 if (vma->pages == vma->obj->mm.pages) {
590 pr_err("(%s) VMA using original object pages!\n",
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);
601 if (vma->pages != vma->obj->mm.pages) {
602 pr_err("VMA not using object pages!\n");
610 static int igt_vma_partial(void *arg)
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;
623 unsigned int sz, offset;
624 struct i915_vma *vma;
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.
631 obj = i915_gem_object_create_internal(i915, npages*PAGE_SIZE);
635 for (p = phases; p->name; p++) { /* exercise both create/lookup */
636 unsigned int count, nvma;
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;
643 view.type = I915_GGTT_VIEW_PARTIAL;
644 view.partial.offset = offset;
645 view.partial.size = sz;
648 view.type = I915_GGTT_VIEW_NORMAL;
650 vma = checked_vma_instance(obj, vm, &view);
656 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
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);
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);
680 list_for_each_entry(vma, &obj->vma_list, obj_link)
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);
689 /* Check that we did create the whole object mapping */
690 vma = checked_vma_instance(obj, vm, NULL);
696 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
700 if (!assert_pin(vma, NULL, obj->base.size, p->name)) {
701 pr_err("(%s) inconsistent full pin\n", p->name);
709 list_for_each_entry(vma, &obj->vma_list, obj_link)
712 pr_err("(%s) allocated an extra full vma!\n", p->name);
719 i915_gem_object_put(obj);
724 int i915_vma_mock_selftests(void)
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),
732 struct drm_i915_private *i915;
735 i915 = mock_gem_device();
739 mutex_lock(&i915->drm.struct_mutex);
740 err = i915_subtests(tests, i915);
741 mutex_unlock(&i915->drm.struct_mutex);
743 drm_dev_unref(&i915->drm);