1 /* 2 * Copyright © 2016 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 */ 24 25 #include <linux/prime_numbers.h> 26 27 #include "../i915_selftest.h" 28 29 #include "mock_gem_device.h" 30 #include "mock_context.h" 31 #include "mock_gtt.h" 32 33 static bool assert_vma(struct i915_vma *vma, 34 struct drm_i915_gem_object *obj, 35 struct i915_gem_context *ctx) 36 { 37 bool ok = true; 38 39 if (vma->vm != &ctx->ppgtt->vm) { 40 pr_err("VMA created with wrong VM\n"); 41 ok = false; 42 } 43 44 if (vma->size != obj->base.size) { 45 pr_err("VMA created with wrong size, found %llu, expected %zu\n", 46 vma->size, obj->base.size); 47 ok = false; 48 } 49 50 if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) { 51 pr_err("VMA created with wrong type [%d]\n", 52 vma->ggtt_view.type); 53 ok = false; 54 } 55 56 return ok; 57 } 58 59 static struct i915_vma * 60 checked_vma_instance(struct drm_i915_gem_object *obj, 61 struct i915_address_space *vm, 62 const struct i915_ggtt_view *view) 63 { 64 struct i915_vma *vma; 65 bool ok = true; 66 67 vma = i915_vma_instance(obj, vm, view); 68 if (IS_ERR(vma)) 69 return vma; 70 71 /* Manual checks, will be reinforced by i915_vma_compare! */ 72 if (vma->vm != vm) { 73 pr_err("VMA's vm [%p] does not match request [%p]\n", 74 vma->vm, vm); 75 ok = false; 76 } 77 78 if (i915_is_ggtt(vm) != i915_vma_is_ggtt(vma)) { 79 pr_err("VMA ggtt status [%d] does not match parent [%d]\n", 80 i915_vma_is_ggtt(vma), i915_is_ggtt(vm)); 81 ok = false; 82 } 83 84 if (i915_vma_compare(vma, vm, view)) { 85 pr_err("i915_vma_compare failed with create parameters!\n"); 86 return ERR_PTR(-EINVAL); 87 } 88 89 if (i915_vma_compare(vma, vma->vm, 90 i915_vma_is_ggtt(vma) ? &vma->ggtt_view : NULL)) { 91 pr_err("i915_vma_compare failed with itself\n"); 92 return ERR_PTR(-EINVAL); 93 } 94 95 if (!ok) { 96 pr_err("i915_vma_compare failed to detect the difference!\n"); 97 return ERR_PTR(-EINVAL); 98 } 99 100 return vma; 101 } 102 103 static int create_vmas(struct drm_i915_private *i915, 104 struct list_head *objects, 105 struct list_head *contexts) 106 { 107 struct drm_i915_gem_object *obj; 108 struct i915_gem_context *ctx; 109 int pinned; 110 111 list_for_each_entry(obj, objects, st_link) { 112 for (pinned = 0; pinned <= 1; pinned++) { 113 list_for_each_entry(ctx, contexts, link) { 114 struct i915_address_space *vm = &ctx->ppgtt->vm; 115 struct i915_vma *vma; 116 int err; 117 118 vma = checked_vma_instance(obj, vm, NULL); 119 if (IS_ERR(vma)) 120 return PTR_ERR(vma); 121 122 if (!assert_vma(vma, obj, ctx)) { 123 pr_err("VMA lookup/create failed\n"); 124 return -EINVAL; 125 } 126 127 if (!pinned) { 128 err = i915_vma_pin(vma, 0, 0, PIN_USER); 129 if (err) { 130 pr_err("Failed to pin VMA\n"); 131 return err; 132 } 133 } else { 134 i915_vma_unpin(vma); 135 } 136 } 137 } 138 } 139 140 return 0; 141 } 142 143 static int igt_vma_create(void *arg) 144 { 145 struct i915_ggtt *ggtt = arg; 146 struct drm_i915_private *i915 = ggtt->vm.i915; 147 struct drm_i915_gem_object *obj, *on; 148 struct i915_gem_context *ctx, *cn; 149 unsigned long num_obj, num_ctx; 150 unsigned long no, nc; 151 IGT_TIMEOUT(end_time); 152 LIST_HEAD(contexts); 153 LIST_HEAD(objects); 154 int err = -ENOMEM; 155 156 /* Exercise creating many vma amonst many objections, checking the 157 * vma creation and lookup routines. 158 */ 159 160 no = 0; 161 for_each_prime_number(num_obj, ULONG_MAX - 1) { 162 for (; no < num_obj; no++) { 163 obj = i915_gem_object_create_internal(i915, PAGE_SIZE); 164 if (IS_ERR(obj)) 165 goto out; 166 167 list_add(&obj->st_link, &objects); 168 } 169 170 nc = 0; 171 for_each_prime_number(num_ctx, MAX_CONTEXT_HW_ID) { 172 for (; nc < num_ctx; nc++) { 173 ctx = mock_context(i915, "mock"); 174 if (!ctx) 175 goto out; 176 177 list_move(&ctx->link, &contexts); 178 } 179 180 err = create_vmas(i915, &objects, &contexts); 181 if (err) 182 goto out; 183 184 if (igt_timeout(end_time, 185 "%s timed out: after %lu objects in %lu contexts\n", 186 __func__, no, nc)) 187 goto end; 188 } 189 190 list_for_each_entry_safe(ctx, cn, &contexts, link) { 191 list_del_init(&ctx->link); 192 mock_context_close(ctx); 193 } 194 } 195 196 end: 197 /* Final pass to lookup all created contexts */ 198 err = create_vmas(i915, &objects, &contexts); 199 out: 200 list_for_each_entry_safe(ctx, cn, &contexts, link) { 201 list_del_init(&ctx->link); 202 mock_context_close(ctx); 203 } 204 205 list_for_each_entry_safe(obj, on, &objects, st_link) 206 i915_gem_object_put(obj); 207 return err; 208 } 209 210 struct pin_mode { 211 u64 size; 212 u64 flags; 213 bool (*assert)(const struct i915_vma *, 214 const struct pin_mode *mode, 215 int result); 216 const char *string; 217 }; 218 219 static bool assert_pin_valid(const struct i915_vma *vma, 220 const struct pin_mode *mode, 221 int result) 222 { 223 if (result) 224 return false; 225 226 if (i915_vma_misplaced(vma, mode->size, 0, mode->flags)) 227 return false; 228 229 return true; 230 } 231 232 __maybe_unused 233 static bool assert_pin_enospc(const struct i915_vma *vma, 234 const struct pin_mode *mode, 235 int result) 236 { 237 return result == -ENOSPC; 238 } 239 240 __maybe_unused 241 static bool assert_pin_einval(const struct i915_vma *vma, 242 const struct pin_mode *mode, 243 int result) 244 { 245 return result == -EINVAL; 246 } 247 248 static int igt_vma_pin1(void *arg) 249 { 250 struct i915_ggtt *ggtt = arg; 251 const struct pin_mode modes[] = { 252 #define VALID(sz, fl) { .size = (sz), .flags = (fl), .assert = assert_pin_valid, .string = #sz ", " #fl ", (valid) " } 253 #define __INVALID(sz, fl, check, eval) { .size = (sz), .flags = (fl), .assert = (check), .string = #sz ", " #fl ", (invalid " #eval ")" } 254 #define INVALID(sz, fl) __INVALID(sz, fl, assert_pin_einval, EINVAL) 255 #define NOSPACE(sz, fl) __INVALID(sz, fl, assert_pin_enospc, ENOSPC) 256 VALID(0, PIN_GLOBAL), 257 VALID(0, PIN_GLOBAL | PIN_MAPPABLE), 258 259 VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 4096), 260 VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | 8192), 261 VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)), 262 VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)), 263 VALID(0, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)), 264 265 VALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)), 266 INVALID(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | ggtt->mappable_end), 267 VALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)), 268 INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | ggtt->vm.total), 269 INVALID(0, PIN_GLOBAL | PIN_OFFSET_FIXED | round_down(U64_MAX, PAGE_SIZE)), 270 271 VALID(4096, PIN_GLOBAL), 272 VALID(8192, PIN_GLOBAL), 273 VALID(ggtt->mappable_end - 4096, PIN_GLOBAL | PIN_MAPPABLE), 274 VALID(ggtt->mappable_end, PIN_GLOBAL | PIN_MAPPABLE), 275 NOSPACE(ggtt->mappable_end + 4096, PIN_GLOBAL | PIN_MAPPABLE), 276 VALID(ggtt->vm.total - 4096, PIN_GLOBAL), 277 VALID(ggtt->vm.total, PIN_GLOBAL), 278 NOSPACE(ggtt->vm.total + 4096, PIN_GLOBAL), 279 NOSPACE(round_down(U64_MAX, PAGE_SIZE), PIN_GLOBAL), 280 INVALID(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_FIXED | (ggtt->mappable_end - 4096)), 281 INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (ggtt->vm.total - 4096)), 282 INVALID(8192, PIN_GLOBAL | PIN_OFFSET_FIXED | (round_down(U64_MAX, PAGE_SIZE) - 4096)), 283 284 VALID(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)), 285 286 #if !IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) 287 /* Misusing BIAS is a programming error (it is not controllable 288 * from userspace) so when debugging is enabled, it explodes. 289 * However, the tests are still quite interesting for checking 290 * variable start, end and size. 291 */ 292 NOSPACE(0, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | ggtt->mappable_end), 293 NOSPACE(0, PIN_GLOBAL | PIN_OFFSET_BIAS | ggtt->vm.total), 294 NOSPACE(8192, PIN_GLOBAL | PIN_MAPPABLE | PIN_OFFSET_BIAS | (ggtt->mappable_end - 4096)), 295 NOSPACE(8192, PIN_GLOBAL | PIN_OFFSET_BIAS | (ggtt->vm.total - 4096)), 296 #endif 297 { }, 298 #undef NOSPACE 299 #undef INVALID 300 #undef __INVALID 301 #undef VALID 302 }, *m; 303 struct drm_i915_gem_object *obj; 304 struct i915_vma *vma; 305 int err = -EINVAL; 306 307 /* Exercise all the weird and wonderful i915_vma_pin requests, 308 * focusing on error handling of boundary conditions. 309 */ 310 311 GEM_BUG_ON(!drm_mm_clean(&ggtt->vm.mm)); 312 313 obj = i915_gem_object_create_internal(ggtt->vm.i915, PAGE_SIZE); 314 if (IS_ERR(obj)) 315 return PTR_ERR(obj); 316 317 vma = checked_vma_instance(obj, &ggtt->vm, NULL); 318 if (IS_ERR(vma)) 319 goto out; 320 321 for (m = modes; m->assert; m++) { 322 err = i915_vma_pin(vma, m->size, 0, m->flags); 323 if (!m->assert(vma, m, err)) { 324 pr_err("%s to pin single page into GGTT with mode[%d:%s]: size=%llx flags=%llx, err=%d\n", 325 m->assert == assert_pin_valid ? "Failed" : "Unexpectedly succeeded", 326 (int)(m - modes), m->string, m->size, m->flags, 327 err); 328 if (!err) 329 i915_vma_unpin(vma); 330 err = -EINVAL; 331 goto out; 332 } 333 334 if (!err) { 335 i915_vma_unpin(vma); 336 err = i915_vma_unbind(vma); 337 if (err) { 338 pr_err("Failed to unbind single page from GGTT, err=%d\n", err); 339 goto out; 340 } 341 } 342 } 343 344 err = 0; 345 out: 346 i915_gem_object_put(obj); 347 return err; 348 } 349 350 static unsigned long rotated_index(const struct intel_rotation_info *r, 351 unsigned int n, 352 unsigned int x, 353 unsigned int y) 354 { 355 return (r->plane[n].stride * (r->plane[n].height - y - 1) + 356 r->plane[n].offset + x); 357 } 358 359 static struct scatterlist * 360 assert_rotated(struct drm_i915_gem_object *obj, 361 const struct intel_rotation_info *r, unsigned int n, 362 struct scatterlist *sg) 363 { 364 unsigned int x, y; 365 366 for (x = 0; x < r->plane[n].width; x++) { 367 for (y = 0; y < r->plane[n].height; y++) { 368 unsigned long src_idx; 369 dma_addr_t src; 370 371 if (!sg) { 372 pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n", 373 n, x, y); 374 return ERR_PTR(-EINVAL); 375 } 376 377 src_idx = rotated_index(r, n, x, y); 378 src = i915_gem_object_get_dma_address(obj, src_idx); 379 380 if (sg_dma_len(sg) != PAGE_SIZE) { 381 pr_err("Invalid sg.length, found %d, expected %lu for rotated page (%d, %d) [src index %lu]\n", 382 sg_dma_len(sg), PAGE_SIZE, 383 x, y, src_idx); 384 return ERR_PTR(-EINVAL); 385 } 386 387 if (sg_dma_address(sg) != src) { 388 pr_err("Invalid address for rotated page (%d, %d) [src index %lu]\n", 389 x, y, src_idx); 390 return ERR_PTR(-EINVAL); 391 } 392 393 sg = sg_next(sg); 394 } 395 } 396 397 return sg; 398 } 399 400 static unsigned long remapped_index(const struct intel_remapped_info *r, 401 unsigned int n, 402 unsigned int x, 403 unsigned int y) 404 { 405 return (r->plane[n].stride * y + 406 r->plane[n].offset + x); 407 } 408 409 static struct scatterlist * 410 assert_remapped(struct drm_i915_gem_object *obj, 411 const struct intel_remapped_info *r, unsigned int n, 412 struct scatterlist *sg) 413 { 414 unsigned int x, y; 415 unsigned int left = 0; 416 unsigned int offset; 417 418 for (y = 0; y < r->plane[n].height; y++) { 419 for (x = 0; x < r->plane[n].width; x++) { 420 unsigned long src_idx; 421 dma_addr_t src; 422 423 if (!sg) { 424 pr_err("Invalid sg table: too short at plane %d, (%d, %d)!\n", 425 n, x, y); 426 return ERR_PTR(-EINVAL); 427 } 428 if (!left) { 429 offset = 0; 430 left = sg_dma_len(sg); 431 } 432 433 src_idx = remapped_index(r, n, x, y); 434 src = i915_gem_object_get_dma_address(obj, src_idx); 435 436 if (left < PAGE_SIZE || left & (PAGE_SIZE-1)) { 437 pr_err("Invalid sg.length, found %d, expected %lu for remapped page (%d, %d) [src index %lu]\n", 438 sg_dma_len(sg), PAGE_SIZE, 439 x, y, src_idx); 440 return ERR_PTR(-EINVAL); 441 } 442 443 if (sg_dma_address(sg) + offset != src) { 444 pr_err("Invalid address for remapped page (%d, %d) [src index %lu]\n", 445 x, y, src_idx); 446 return ERR_PTR(-EINVAL); 447 } 448 449 left -= PAGE_SIZE; 450 offset += PAGE_SIZE; 451 452 453 if (!left) 454 sg = sg_next(sg); 455 } 456 } 457 458 return sg; 459 } 460 461 static unsigned int rotated_size(const struct intel_remapped_plane_info *a, 462 const struct intel_remapped_plane_info *b) 463 { 464 return a->width * a->height + b->width * b->height; 465 } 466 467 static int igt_vma_rotate_remap(void *arg) 468 { 469 struct i915_ggtt *ggtt = arg; 470 struct i915_address_space *vm = &ggtt->vm; 471 struct drm_i915_gem_object *obj; 472 const struct intel_remapped_plane_info planes[] = { 473 { .width = 1, .height = 1, .stride = 1 }, 474 { .width = 2, .height = 2, .stride = 2 }, 475 { .width = 4, .height = 4, .stride = 4 }, 476 { .width = 8, .height = 8, .stride = 8 }, 477 478 { .width = 3, .height = 5, .stride = 3 }, 479 { .width = 3, .height = 5, .stride = 4 }, 480 { .width = 3, .height = 5, .stride = 5 }, 481 482 { .width = 5, .height = 3, .stride = 5 }, 483 { .width = 5, .height = 3, .stride = 7 }, 484 { .width = 5, .height = 3, .stride = 9 }, 485 486 { .width = 4, .height = 6, .stride = 6 }, 487 { .width = 6, .height = 4, .stride = 6 }, 488 { } 489 }, *a, *b; 490 enum i915_ggtt_view_type types[] = { 491 I915_GGTT_VIEW_ROTATED, 492 I915_GGTT_VIEW_REMAPPED, 493 0, 494 }, *t; 495 const unsigned int max_pages = 64; 496 int err = -ENOMEM; 497 498 /* Create VMA for many different combinations of planes and check 499 * that the page layout within the rotated VMA match our expectations. 500 */ 501 502 obj = i915_gem_object_create_internal(vm->i915, max_pages * PAGE_SIZE); 503 if (IS_ERR(obj)) 504 goto out; 505 506 for (t = types; *t; t++) { 507 for (a = planes; a->width; a++) { 508 for (b = planes + ARRAY_SIZE(planes); b-- != planes; ) { 509 struct i915_ggtt_view view; 510 unsigned int n, max_offset; 511 512 max_offset = max(a->stride * a->height, 513 b->stride * b->height); 514 GEM_BUG_ON(max_offset > max_pages); 515 max_offset = max_pages - max_offset; 516 517 view.type = *t; 518 view.rotated.plane[0] = *a; 519 view.rotated.plane[1] = *b; 520 521 for_each_prime_number_from(view.rotated.plane[0].offset, 0, max_offset) { 522 for_each_prime_number_from(view.rotated.plane[1].offset, 0, max_offset) { 523 struct scatterlist *sg; 524 struct i915_vma *vma; 525 526 vma = checked_vma_instance(obj, vm, &view); 527 if (IS_ERR(vma)) { 528 err = PTR_ERR(vma); 529 goto out_object; 530 } 531 532 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL); 533 if (err) { 534 pr_err("Failed to pin VMA, err=%d\n", err); 535 goto out_object; 536 } 537 538 if (view.type == I915_GGTT_VIEW_ROTATED && 539 vma->size != rotated_size(a, b) * PAGE_SIZE) { 540 pr_err("VMA is wrong size, expected %lu, found %llu\n", 541 PAGE_SIZE * rotated_size(a, b), vma->size); 542 err = -EINVAL; 543 goto out_object; 544 } 545 546 if (view.type == I915_GGTT_VIEW_REMAPPED && 547 vma->size > rotated_size(a, b) * PAGE_SIZE) { 548 pr_err("VMA is wrong size, expected %lu, found %llu\n", 549 PAGE_SIZE * rotated_size(a, b), vma->size); 550 err = -EINVAL; 551 goto out_object; 552 } 553 554 if (vma->pages->nents > rotated_size(a, b)) { 555 pr_err("sg table is wrong sizeo, expected %u, found %u nents\n", 556 rotated_size(a, b), vma->pages->nents); 557 err = -EINVAL; 558 goto out_object; 559 } 560 561 if (vma->node.size < vma->size) { 562 pr_err("VMA binding too small, expected %llu, found %llu\n", 563 vma->size, vma->node.size); 564 err = -EINVAL; 565 goto out_object; 566 } 567 568 if (vma->pages == obj->mm.pages) { 569 pr_err("VMA using unrotated object pages!\n"); 570 err = -EINVAL; 571 goto out_object; 572 } 573 574 sg = vma->pages->sgl; 575 for (n = 0; n < ARRAY_SIZE(view.rotated.plane); n++) { 576 if (view.type == I915_GGTT_VIEW_ROTATED) 577 sg = assert_rotated(obj, &view.rotated, n, sg); 578 else 579 sg = assert_remapped(obj, &view.remapped, n, sg); 580 if (IS_ERR(sg)) { 581 pr_err("Inconsistent %s VMA pages for plane %d: [(%d, %d, %d, %d), (%d, %d, %d, %d)]\n", 582 view.type == I915_GGTT_VIEW_ROTATED ? 583 "rotated" : "remapped", n, 584 view.rotated.plane[0].width, 585 view.rotated.plane[0].height, 586 view.rotated.plane[0].stride, 587 view.rotated.plane[0].offset, 588 view.rotated.plane[1].width, 589 view.rotated.plane[1].height, 590 view.rotated.plane[1].stride, 591 view.rotated.plane[1].offset); 592 err = -EINVAL; 593 goto out_object; 594 } 595 } 596 597 i915_vma_unpin(vma); 598 } 599 } 600 } 601 } 602 } 603 604 out_object: 605 i915_gem_object_put(obj); 606 out: 607 return err; 608 } 609 610 static bool assert_partial(struct drm_i915_gem_object *obj, 611 struct i915_vma *vma, 612 unsigned long offset, 613 unsigned long size) 614 { 615 struct sgt_iter sgt; 616 dma_addr_t dma; 617 618 for_each_sgt_dma(dma, sgt, vma->pages) { 619 dma_addr_t src; 620 621 if (!size) { 622 pr_err("Partial scattergather list too long\n"); 623 return false; 624 } 625 626 src = i915_gem_object_get_dma_address(obj, offset); 627 if (src != dma) { 628 pr_err("DMA mismatch for partial page offset %lu\n", 629 offset); 630 return false; 631 } 632 633 offset++; 634 size--; 635 } 636 637 return true; 638 } 639 640 static bool assert_pin(struct i915_vma *vma, 641 struct i915_ggtt_view *view, 642 u64 size, 643 const char *name) 644 { 645 bool ok = true; 646 647 if (vma->size != size) { 648 pr_err("(%s) VMA is wrong size, expected %llu, found %llu\n", 649 name, size, vma->size); 650 ok = false; 651 } 652 653 if (vma->node.size < vma->size) { 654 pr_err("(%s) VMA binding too small, expected %llu, found %llu\n", 655 name, vma->size, vma->node.size); 656 ok = false; 657 } 658 659 if (view && view->type != I915_GGTT_VIEW_NORMAL) { 660 if (memcmp(&vma->ggtt_view, view, sizeof(*view))) { 661 pr_err("(%s) VMA mismatch upon creation!\n", 662 name); 663 ok = false; 664 } 665 666 if (vma->pages == vma->obj->mm.pages) { 667 pr_err("(%s) VMA using original object pages!\n", 668 name); 669 ok = false; 670 } 671 } else { 672 if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) { 673 pr_err("Not the normal ggtt view! Found %d\n", 674 vma->ggtt_view.type); 675 ok = false; 676 } 677 678 if (vma->pages != vma->obj->mm.pages) { 679 pr_err("VMA not using object pages!\n"); 680 ok = false; 681 } 682 } 683 684 return ok; 685 } 686 687 static int igt_vma_partial(void *arg) 688 { 689 struct i915_ggtt *ggtt = arg; 690 struct i915_address_space *vm = &ggtt->vm; 691 const unsigned int npages = 1021; /* prime! */ 692 struct drm_i915_gem_object *obj; 693 const struct phase { 694 const char *name; 695 } phases[] = { 696 { "create" }, 697 { "lookup" }, 698 { }, 699 }, *p; 700 unsigned int sz, offset; 701 struct i915_vma *vma; 702 int err = -ENOMEM; 703 704 /* Create lots of different VMA for the object and check that 705 * we are returned the same VMA when we later request the same range. 706 */ 707 708 obj = i915_gem_object_create_internal(vm->i915, npages * PAGE_SIZE); 709 if (IS_ERR(obj)) 710 goto out; 711 712 for (p = phases; p->name; p++) { /* exercise both create/lookup */ 713 unsigned int count, nvma; 714 715 nvma = 0; 716 for_each_prime_number_from(sz, 1, npages) { 717 for_each_prime_number_from(offset, 0, npages - sz) { 718 struct i915_ggtt_view view; 719 720 view.type = I915_GGTT_VIEW_PARTIAL; 721 view.partial.offset = offset; 722 view.partial.size = sz; 723 724 if (sz == npages) 725 view.type = I915_GGTT_VIEW_NORMAL; 726 727 vma = checked_vma_instance(obj, vm, &view); 728 if (IS_ERR(vma)) { 729 err = PTR_ERR(vma); 730 goto out_object; 731 } 732 733 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL); 734 if (err) 735 goto out_object; 736 737 if (!assert_pin(vma, &view, sz*PAGE_SIZE, p->name)) { 738 pr_err("(%s) Inconsistent partial pinning for (offset=%d, size=%d)\n", 739 p->name, offset, sz); 740 err = -EINVAL; 741 goto out_object; 742 } 743 744 if (!assert_partial(obj, vma, offset, sz)) { 745 pr_err("(%s) Inconsistent partial pages for (offset=%d, size=%d)\n", 746 p->name, offset, sz); 747 err = -EINVAL; 748 goto out_object; 749 } 750 751 i915_vma_unpin(vma); 752 nvma++; 753 } 754 } 755 756 count = 0; 757 list_for_each_entry(vma, &obj->vma.list, obj_link) 758 count++; 759 if (count != nvma) { 760 pr_err("(%s) All partial vma were not recorded on the obj->vma_list: found %u, expected %u\n", 761 p->name, count, nvma); 762 err = -EINVAL; 763 goto out_object; 764 } 765 766 /* Check that we did create the whole object mapping */ 767 vma = checked_vma_instance(obj, vm, NULL); 768 if (IS_ERR(vma)) { 769 err = PTR_ERR(vma); 770 goto out_object; 771 } 772 773 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL); 774 if (err) 775 goto out_object; 776 777 if (!assert_pin(vma, NULL, obj->base.size, p->name)) { 778 pr_err("(%s) inconsistent full pin\n", p->name); 779 err = -EINVAL; 780 goto out_object; 781 } 782 783 i915_vma_unpin(vma); 784 785 count = 0; 786 list_for_each_entry(vma, &obj->vma.list, obj_link) 787 count++; 788 if (count != nvma) { 789 pr_err("(%s) allocated an extra full vma!\n", p->name); 790 err = -EINVAL; 791 goto out_object; 792 } 793 } 794 795 out_object: 796 i915_gem_object_put(obj); 797 out: 798 return err; 799 } 800 801 int i915_vma_mock_selftests(void) 802 { 803 static const struct i915_subtest tests[] = { 804 SUBTEST(igt_vma_create), 805 SUBTEST(igt_vma_pin1), 806 SUBTEST(igt_vma_rotate_remap), 807 SUBTEST(igt_vma_partial), 808 }; 809 struct drm_i915_private *i915; 810 struct i915_ggtt *ggtt; 811 int err; 812 813 i915 = mock_gem_device(); 814 if (!i915) 815 return -ENOMEM; 816 817 ggtt = kmalloc(sizeof(*ggtt), GFP_KERNEL); 818 if (!ggtt) { 819 err = -ENOMEM; 820 goto out_put; 821 } 822 mock_init_ggtt(i915, ggtt); 823 824 mutex_lock(&i915->drm.struct_mutex); 825 err = i915_subtests(tests, ggtt); 826 mock_device_flush(i915); 827 mutex_unlock(&i915->drm.struct_mutex); 828 829 i915_gem_drain_freed_objects(i915); 830 831 mock_fini_ggtt(ggtt); 832 kfree(ggtt); 833 out_put: 834 drm_dev_put(&i915->drm); 835 return err; 836 } 837 838 static int igt_vma_remapped_gtt(void *arg) 839 { 840 struct drm_i915_private *i915 = arg; 841 const struct intel_remapped_plane_info planes[] = { 842 { .width = 1, .height = 1, .stride = 1 }, 843 { .width = 2, .height = 2, .stride = 2 }, 844 { .width = 4, .height = 4, .stride = 4 }, 845 { .width = 8, .height = 8, .stride = 8 }, 846 847 { .width = 3, .height = 5, .stride = 3 }, 848 { .width = 3, .height = 5, .stride = 4 }, 849 { .width = 3, .height = 5, .stride = 5 }, 850 851 { .width = 5, .height = 3, .stride = 5 }, 852 { .width = 5, .height = 3, .stride = 7 }, 853 { .width = 5, .height = 3, .stride = 9 }, 854 855 { .width = 4, .height = 6, .stride = 6 }, 856 { .width = 6, .height = 4, .stride = 6 }, 857 { } 858 }, *p; 859 enum i915_ggtt_view_type types[] = { 860 I915_GGTT_VIEW_ROTATED, 861 I915_GGTT_VIEW_REMAPPED, 862 0, 863 }, *t; 864 struct drm_i915_gem_object *obj; 865 intel_wakeref_t wakeref; 866 int err = 0; 867 868 obj = i915_gem_object_create_internal(i915, 10 * 10 * PAGE_SIZE); 869 if (IS_ERR(obj)) 870 return PTR_ERR(obj); 871 872 mutex_lock(&i915->drm.struct_mutex); 873 874 wakeref = intel_runtime_pm_get(i915); 875 876 for (t = types; *t; t++) { 877 for (p = planes; p->width; p++) { 878 struct i915_ggtt_view view = { 879 .type = *t, 880 .rotated.plane[0] = *p, 881 }; 882 struct i915_vma *vma; 883 u32 __iomem *map; 884 unsigned int x, y; 885 int err; 886 887 err = i915_gem_object_set_to_gtt_domain(obj, true); 888 if (err) 889 goto out; 890 891 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); 892 if (IS_ERR(vma)) { 893 err = PTR_ERR(vma); 894 goto out; 895 } 896 897 GEM_BUG_ON(vma->ggtt_view.type != *t); 898 899 map = i915_vma_pin_iomap(vma); 900 i915_vma_unpin(vma); 901 if (IS_ERR(map)) { 902 err = PTR_ERR(map); 903 goto out; 904 } 905 906 for (y = 0 ; y < p->height; y++) { 907 for (x = 0 ; x < p->width; x++) { 908 unsigned int offset; 909 u32 val = y << 16 | x; 910 911 if (*t == I915_GGTT_VIEW_ROTATED) 912 offset = (x * p->height + y) * PAGE_SIZE; 913 else 914 offset = (y * p->width + x) * PAGE_SIZE; 915 916 iowrite32(val, &map[offset / sizeof(*map)]); 917 } 918 } 919 920 i915_vma_unpin_iomap(vma); 921 922 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE); 923 if (IS_ERR(vma)) { 924 err = PTR_ERR(vma); 925 goto out; 926 } 927 928 GEM_BUG_ON(vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL); 929 930 map = i915_vma_pin_iomap(vma); 931 i915_vma_unpin(vma); 932 if (IS_ERR(map)) { 933 err = PTR_ERR(map); 934 goto out; 935 } 936 937 for (y = 0 ; y < p->height; y++) { 938 for (x = 0 ; x < p->width; x++) { 939 unsigned int offset, src_idx; 940 u32 exp = y << 16 | x; 941 u32 val; 942 943 if (*t == I915_GGTT_VIEW_ROTATED) 944 src_idx = rotated_index(&view.rotated, 0, x, y); 945 else 946 src_idx = remapped_index(&view.remapped, 0, x, y); 947 offset = src_idx * PAGE_SIZE; 948 949 val = ioread32(&map[offset / sizeof(*map)]); 950 if (val != exp) { 951 pr_err("%s VMA write test failed, expected 0x%x, found 0x%x\n", 952 *t == I915_GGTT_VIEW_ROTATED ? "Rotated" : "Remapped", 953 val, exp); 954 i915_vma_unpin_iomap(vma); 955 goto out; 956 } 957 } 958 } 959 i915_vma_unpin_iomap(vma); 960 } 961 } 962 963 out: 964 intel_runtime_pm_put(i915, wakeref); 965 mutex_unlock(&i915->drm.struct_mutex); 966 i915_gem_object_put(obj); 967 968 return err; 969 } 970 971 int i915_vma_live_selftests(struct drm_i915_private *i915) 972 { 973 static const struct i915_subtest tests[] = { 974 SUBTEST(igt_vma_remapped_gtt), 975 }; 976 977 return i915_subtests(tests, i915); 978 } 979