1 /* 2 * Copyright © 2008 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 * Authors: 24 * Eric Anholt <eric@anholt.net> 25 * 26 * Copyright (c) 2011 The FreeBSD Foundation 27 * All rights reserved. 28 * 29 * This software was developed by Konstantin Belousov under sponsorship from 30 * the FreeBSD Foundation. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 */ 54 55 #include <machine/md_var.h> 56 57 #include <drm/drmP.h> 58 #include <drm/drm_vma_manager.h> 59 #include <drm/i915_drm.h> 60 #include "i915_drv.h" 61 #include "i915_trace.h" 62 #include "intel_drv.h" 63 #include <linux/shmem_fs.h> 64 #include <linux/slab.h> 65 #include <linux/swap.h> 66 #include <linux/pci.h> 67 68 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj); 69 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj, 70 bool force); 71 static __must_check int 72 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, 73 bool readonly); 74 static __must_check int 75 i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj, 76 struct i915_address_space *vm, 77 unsigned alignment, 78 bool map_and_fenceable, 79 bool nonblocking); 80 static int i915_gem_phys_pwrite(struct drm_device *dev, 81 struct drm_i915_gem_object *obj, 82 struct drm_i915_gem_pwrite *args, 83 struct drm_file *file); 84 85 static void i915_gem_write_fence(struct drm_device *dev, int reg, 86 struct drm_i915_gem_object *obj); 87 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, 88 struct drm_i915_fence_reg *fence, 89 bool enable); 90 91 static unsigned long i915_gem_purge(struct drm_i915_private *dev_priv, long target); 92 static unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv); 93 static void i915_gem_object_truncate(struct drm_i915_gem_object *obj); 94 95 static bool cpu_cache_is_coherent(struct drm_device *dev, 96 enum i915_cache_level level) 97 { 98 return HAS_LLC(dev) || level != I915_CACHE_NONE; 99 } 100 101 static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) 102 { 103 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) 104 return true; 105 106 return obj->pin_display; 107 } 108 109 static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj) 110 { 111 if (obj->tiling_mode) 112 i915_gem_release_mmap(obj); 113 114 /* As we do not have an associated fence register, we will force 115 * a tiling change if we ever need to acquire one. 116 */ 117 obj->fence_dirty = false; 118 obj->fence_reg = I915_FENCE_REG_NONE; 119 } 120 121 /* some bookkeeping */ 122 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, 123 size_t size) 124 { 125 spin_lock(&dev_priv->mm.object_stat_lock); 126 dev_priv->mm.object_count++; 127 dev_priv->mm.object_memory += size; 128 spin_unlock(&dev_priv->mm.object_stat_lock); 129 } 130 131 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, 132 size_t size) 133 { 134 spin_lock(&dev_priv->mm.object_stat_lock); 135 dev_priv->mm.object_count--; 136 dev_priv->mm.object_memory -= size; 137 spin_unlock(&dev_priv->mm.object_stat_lock); 138 } 139 140 static int 141 i915_gem_wait_for_error(struct i915_gpu_error *error) 142 { 143 int ret; 144 145 #define EXIT_COND (!i915_reset_in_progress(error) || \ 146 i915_terminally_wedged(error)) 147 if (EXIT_COND) 148 return 0; 149 150 /* 151 * Only wait 10 seconds for the gpu reset to complete to avoid hanging 152 * userspace. If it takes that long something really bad is going on and 153 * we should simply try to bail out and fail as gracefully as possible. 154 */ 155 ret = wait_event_interruptible_timeout(error->reset_queue, 156 EXIT_COND, 157 10*HZ); 158 if (ret == 0) { 159 DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); 160 return -EIO; 161 } else if (ret < 0) { 162 return ret; 163 } 164 #undef EXIT_COND 165 166 return 0; 167 } 168 169 int i915_mutex_lock_interruptible(struct drm_device *dev) 170 { 171 struct drm_i915_private *dev_priv = dev->dev_private; 172 int ret; 173 174 ret = i915_gem_wait_for_error(&dev_priv->gpu_error); 175 if (ret) 176 return ret; 177 178 ret = lockmgr(&dev->struct_mutex, LK_EXCLUSIVE|LK_SLEEPFAIL); 179 if (ret) 180 return -EINTR; 181 182 WARN_ON(i915_verify_lists(dev)); 183 return 0; 184 } 185 186 static inline bool 187 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj) 188 { 189 return i915_gem_obj_bound_any(obj) && !obj->active; 190 } 191 192 int 193 i915_gem_init_ioctl(struct drm_device *dev, void *data, 194 struct drm_file *file) 195 { 196 struct drm_i915_private *dev_priv = dev->dev_private; 197 struct drm_i915_gem_init *args = data; 198 199 if (drm_core_check_feature(dev, DRIVER_MODESET)) 200 return -ENODEV; 201 202 if (args->gtt_start >= args->gtt_end || 203 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1)) 204 return -EINVAL; 205 206 /* GEM with user mode setting was never supported on ilk and later. */ 207 if (INTEL_INFO(dev)->gen >= 5) 208 return -ENODEV; 209 210 mutex_lock(&dev->struct_mutex); 211 dev_priv->gtt.mappable_end = args->gtt_end; 212 kprintf("INITGLOBALGTT GTT_START %016jx\n", (uintmax_t)args->gtt_start); 213 i915_gem_init_global_gtt(dev); 214 #if 0 215 i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end, 216 args->gtt_end); 217 #endif 218 mutex_unlock(&dev->struct_mutex); 219 220 return 0; 221 } 222 223 int 224 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, 225 struct drm_file *file) 226 { 227 struct drm_i915_private *dev_priv = dev->dev_private; 228 struct drm_i915_gem_get_aperture *args = data; 229 struct drm_i915_gem_object *obj; 230 size_t pinned; 231 232 pinned = 0; 233 mutex_lock(&dev->struct_mutex); 234 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) 235 if (obj->pin_count) 236 pinned += i915_gem_obj_ggtt_size(obj); 237 mutex_unlock(&dev->struct_mutex); 238 239 args->aper_size = dev_priv->gtt.base.total; 240 args->aper_available_size = args->aper_size - pinned; 241 242 return 0; 243 } 244 245 void *i915_gem_object_alloc(struct drm_device *dev) 246 { 247 return kmalloc(sizeof(struct drm_i915_gem_object), 248 M_DRM, M_WAITOK | M_ZERO); 249 } 250 251 void i915_gem_object_free(struct drm_i915_gem_object *obj) 252 { 253 kfree(obj); 254 } 255 256 static int 257 i915_gem_create(struct drm_file *file, 258 struct drm_device *dev, 259 uint64_t size, 260 uint32_t *handle_p) 261 { 262 struct drm_i915_gem_object *obj; 263 int ret; 264 u32 handle; 265 266 size = roundup(size, PAGE_SIZE); 267 if (size == 0) 268 return -EINVAL; 269 270 /* Allocate the new object */ 271 obj = i915_gem_alloc_object(dev, size); 272 if (obj == NULL) 273 return -ENOMEM; 274 275 ret = drm_gem_handle_create(file, &obj->base, &handle); 276 /* drop reference from allocate - handle holds it now */ 277 drm_gem_object_unreference_unlocked(&obj->base); 278 if (ret) 279 return ret; 280 281 *handle_p = handle; 282 return 0; 283 } 284 285 int 286 i915_gem_dumb_create(struct drm_file *file, 287 struct drm_device *dev, 288 struct drm_mode_create_dumb *args) 289 { 290 /* have to work out size/pitch and return them */ 291 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); 292 args->size = args->pitch * args->height; 293 return i915_gem_create(file, dev, 294 args->size, &args->handle); 295 } 296 297 /** 298 * Creates a new mm object and returns a handle to it. 299 */ 300 int 301 i915_gem_create_ioctl(struct drm_device *dev, void *data, 302 struct drm_file *file) 303 { 304 struct drm_i915_gem_create *args = data; 305 306 return i915_gem_create(file, dev, 307 args->size, &args->handle); 308 } 309 310 static inline int 311 __copy_to_user_swizzled(char __user *cpu_vaddr, 312 const char *gpu_vaddr, int gpu_offset, 313 int length) 314 { 315 int ret, cpu_offset = 0; 316 317 while (length > 0) { 318 int cacheline_end = ALIGN(gpu_offset + 1, 64); 319 int this_length = min(cacheline_end - gpu_offset, length); 320 int swizzled_gpu_offset = gpu_offset ^ 64; 321 322 ret = __copy_to_user(cpu_vaddr + cpu_offset, 323 gpu_vaddr + swizzled_gpu_offset, 324 this_length); 325 if (ret) 326 return ret + length; 327 328 cpu_offset += this_length; 329 gpu_offset += this_length; 330 length -= this_length; 331 } 332 333 return 0; 334 } 335 336 static inline int 337 __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, 338 const char __user *cpu_vaddr, 339 int length) 340 { 341 int ret, cpu_offset = 0; 342 343 while (length > 0) { 344 int cacheline_end = ALIGN(gpu_offset + 1, 64); 345 int this_length = min(cacheline_end - gpu_offset, length); 346 int swizzled_gpu_offset = gpu_offset ^ 64; 347 348 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, 349 cpu_vaddr + cpu_offset, 350 this_length); 351 if (ret) 352 return ret + length; 353 354 cpu_offset += this_length; 355 gpu_offset += this_length; 356 length -= this_length; 357 } 358 359 return 0; 360 } 361 362 /* Per-page copy function for the shmem pread fastpath. 363 * Flushes invalid cachelines before reading the target if 364 * needs_clflush is set. */ 365 static int 366 shmem_pread_fast(struct vm_page *page, int shmem_page_offset, int page_length, 367 char __user *user_data, 368 bool page_do_bit17_swizzling, bool needs_clflush) 369 { 370 char *vaddr; 371 int ret; 372 373 if (unlikely(page_do_bit17_swizzling)) 374 return -EINVAL; 375 376 vaddr = kmap_atomic(page); 377 if (needs_clflush) 378 drm_clflush_virt_range(vaddr + shmem_page_offset, 379 page_length); 380 ret = __copy_to_user_inatomic(user_data, 381 vaddr + shmem_page_offset, 382 page_length); 383 kunmap_atomic(vaddr); 384 385 return ret ? -EFAULT : 0; 386 } 387 388 static void 389 shmem_clflush_swizzled_range(char *addr, unsigned long length, 390 bool swizzled) 391 { 392 if (unlikely(swizzled)) { 393 unsigned long start = (unsigned long) addr; 394 unsigned long end = (unsigned long) addr + length; 395 396 /* For swizzling simply ensure that we always flush both 397 * channels. Lame, but simple and it works. Swizzled 398 * pwrite/pread is far from a hotpath - current userspace 399 * doesn't use it at all. */ 400 start = round_down(start, 128); 401 end = round_up(end, 128); 402 403 drm_clflush_virt_range((void *)start, end - start); 404 } else { 405 drm_clflush_virt_range(addr, length); 406 } 407 408 } 409 410 /* Only difference to the fast-path function is that this can handle bit17 411 * and uses non-atomic copy and kmap functions. */ 412 static int 413 shmem_pread_slow(struct vm_page *page, int shmem_page_offset, int page_length, 414 char __user *user_data, 415 bool page_do_bit17_swizzling, bool needs_clflush) 416 { 417 char *vaddr; 418 int ret; 419 420 vaddr = kmap(page); 421 if (needs_clflush) 422 shmem_clflush_swizzled_range(vaddr + shmem_page_offset, 423 page_length, 424 page_do_bit17_swizzling); 425 426 if (page_do_bit17_swizzling) 427 ret = __copy_to_user_swizzled(user_data, 428 vaddr, shmem_page_offset, 429 page_length); 430 else 431 ret = __copy_to_user(user_data, 432 vaddr + shmem_page_offset, 433 page_length); 434 kunmap(page); 435 436 return ret ? - EFAULT : 0; 437 } 438 439 static int 440 i915_gem_shmem_pread(struct drm_device *dev, 441 struct drm_i915_gem_object *obj, 442 struct drm_i915_gem_pread *args, 443 struct drm_file *file) 444 { 445 char __user *user_data; 446 ssize_t remain; 447 loff_t offset; 448 int shmem_page_offset, page_length, ret = 0; 449 int obj_do_bit17_swizzling, page_do_bit17_swizzling; 450 int prefaulted = 0; 451 int needs_clflush = 0; 452 int i; 453 454 user_data = to_user_ptr(args->data_ptr); 455 remain = args->size; 456 457 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); 458 459 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) { 460 /* If we're not in the cpu read domain, set ourself into the gtt 461 * read domain and manually flush cachelines (if required). This 462 * optimizes for the case when the gpu will dirty the data 463 * anyway again before the next pread happens. */ 464 needs_clflush = !cpu_cache_is_coherent(dev, obj->cache_level); 465 ret = i915_gem_object_wait_rendering(obj, true); 466 if (ret) 467 return ret; 468 } 469 470 ret = i915_gem_object_get_pages(obj); 471 if (ret) 472 return ret; 473 474 i915_gem_object_pin_pages(obj); 475 476 offset = args->offset; 477 478 for (i = 0; i < (obj->base.size >> PAGE_SHIFT); i++) { 479 struct vm_page *page = obj->pages[i]; 480 481 if (remain <= 0) 482 break; 483 484 /* Operation in this page 485 * 486 * shmem_page_offset = offset within page in shmem file 487 * page_length = bytes to copy for this page 488 */ 489 shmem_page_offset = offset_in_page(offset); 490 page_length = remain; 491 if ((shmem_page_offset + page_length) > PAGE_SIZE) 492 page_length = PAGE_SIZE - shmem_page_offset; 493 494 page_do_bit17_swizzling = obj_do_bit17_swizzling && 495 (page_to_phys(page) & (1 << 17)) != 0; 496 497 ret = shmem_pread_fast(page, shmem_page_offset, page_length, 498 user_data, page_do_bit17_swizzling, 499 needs_clflush); 500 if (ret == 0) 501 goto next_page; 502 503 mutex_unlock(&dev->struct_mutex); 504 505 if (likely(!i915_prefault_disable) && !prefaulted) { 506 ret = fault_in_multipages_writeable(user_data, remain); 507 /* Userspace is tricking us, but we've already clobbered 508 * its pages with the prefault and promised to write the 509 * data up to the first fault. Hence ignore any errors 510 * and just continue. */ 511 (void)ret; 512 prefaulted = 1; 513 } 514 515 ret = shmem_pread_slow(page, shmem_page_offset, page_length, 516 user_data, page_do_bit17_swizzling, 517 needs_clflush); 518 519 mutex_lock(&dev->struct_mutex); 520 521 next_page: 522 mark_page_accessed(page); 523 524 if (ret) 525 goto out; 526 527 remain -= page_length; 528 user_data += page_length; 529 offset += page_length; 530 } 531 532 out: 533 i915_gem_object_unpin_pages(obj); 534 535 return ret; 536 } 537 538 /** 539 * Reads data from the object referenced by handle. 540 * 541 * On error, the contents of *data are undefined. 542 */ 543 int 544 i915_gem_pread_ioctl(struct drm_device *dev, void *data, 545 struct drm_file *file) 546 { 547 struct drm_i915_gem_pread *args = data; 548 struct drm_i915_gem_object *obj; 549 int ret = 0; 550 551 if (args->size == 0) 552 return 0; 553 554 ret = i915_mutex_lock_interruptible(dev); 555 if (ret) 556 return ret; 557 558 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 559 if (&obj->base == NULL) { 560 ret = -ENOENT; 561 goto unlock; 562 } 563 564 /* Bounds check source. */ 565 if (args->offset > obj->base.size || 566 args->size > obj->base.size - args->offset) { 567 ret = -EINVAL; 568 goto out; 569 } 570 571 trace_i915_gem_object_pread(obj, args->offset, args->size); 572 573 ret = i915_gem_shmem_pread(dev, obj, args, file); 574 575 out: 576 drm_gem_object_unreference(&obj->base); 577 unlock: 578 mutex_unlock(&dev->struct_mutex); 579 return ret; 580 } 581 582 /* This is the fast write path which cannot handle 583 * page faults in the source data 584 */ 585 586 #if 0 /* XXX: buggy on core2 machines */ 587 static inline int 588 fast_user_write(struct io_mapping *mapping, 589 loff_t page_base, int page_offset, 590 char __user *user_data, 591 int length) 592 { 593 void __iomem *vaddr_atomic; 594 void *vaddr; 595 unsigned long unwritten; 596 597 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); 598 /* We can use the cpu mem copy function because this is X86. */ 599 vaddr = (char __force*)vaddr_atomic + page_offset; 600 unwritten = __copy_from_user_inatomic_nocache(vaddr, 601 user_data, length); 602 io_mapping_unmap_atomic(vaddr_atomic); 603 return unwritten; 604 } 605 #endif 606 607 static int 608 i915_gem_gtt_write(struct drm_device *dev, struct drm_i915_gem_object *obj, 609 uint64_t data_ptr, uint64_t size, uint64_t offset, struct drm_file *file) 610 { 611 vm_offset_t mkva; 612 int ret; 613 614 /* 615 * Pass the unaligned physical address and size to pmap_mapdev_attr() 616 * so it can properly calculate whether an extra page needs to be 617 * mapped or not to cover the requested range. The function will 618 * add the page offset into the returned mkva for us. 619 */ 620 mkva = (vm_offset_t)pmap_mapdev_attr(dev->agp->base + 621 i915_gem_obj_ggtt_offset(obj) + offset, size, PAT_WRITE_COMBINING); 622 ret = -copyin_nofault((void *)(uintptr_t)data_ptr, (char *)mkva, size); 623 pmap_unmapdev(mkva, size); 624 return ret; 625 } 626 627 /** 628 * This is the fast pwrite path, where we copy the data directly from the 629 * user into the GTT, uncached. 630 */ 631 static int 632 i915_gem_gtt_pwrite_fast(struct drm_device *dev, 633 struct drm_i915_gem_object *obj, 634 struct drm_i915_gem_pwrite *args, 635 struct drm_file *file) 636 { 637 ssize_t remain; 638 loff_t offset, page_base; 639 char __user *user_data; 640 int page_offset, page_length, ret; 641 642 ret = i915_gem_obj_ggtt_pin(obj, 0, true, true); 643 if (ret) 644 goto out; 645 646 ret = i915_gem_object_set_to_gtt_domain(obj, true); 647 if (ret) 648 goto out_unpin; 649 650 ret = i915_gem_object_put_fence(obj); 651 if (ret) 652 goto out_unpin; 653 654 user_data = to_user_ptr(args->data_ptr); 655 remain = args->size; 656 657 offset = i915_gem_obj_ggtt_offset(obj) + args->offset; 658 659 while (remain > 0) { 660 /* Operation in this page 661 * 662 * page_base = page offset within aperture 663 * page_offset = offset within page 664 * page_length = bytes to copy for this page 665 */ 666 page_base = offset & ~PAGE_MASK; 667 page_offset = offset_in_page(offset); 668 page_length = remain; 669 if ((page_offset + remain) > PAGE_SIZE) 670 page_length = PAGE_SIZE - page_offset; 671 672 /* If we get a fault while copying data, then (presumably) our 673 * source page isn't available. Return the error and we'll 674 * retry in the slow path. 675 */ 676 #if 0 677 if (fast_user_write(dev_priv->gtt.mappable, page_base, 678 page_offset, user_data, page_length)) { 679 #else 680 if (i915_gem_gtt_write(dev, obj, args->data_ptr, args->size, args->offset, file)) { 681 #endif 682 ret = -EFAULT; 683 goto out_unpin; 684 } 685 686 remain -= page_length; 687 user_data += page_length; 688 offset += page_length; 689 } 690 691 out_unpin: 692 i915_gem_object_unpin(obj); 693 out: 694 return ret; 695 } 696 697 /* Per-page copy function for the shmem pwrite fastpath. 698 * Flushes invalid cachelines before writing to the target if 699 * needs_clflush_before is set and flushes out any written cachelines after 700 * writing if needs_clflush is set. */ 701 static int 702 shmem_pwrite_fast(struct vm_page *page, int shmem_page_offset, int page_length, 703 char __user *user_data, 704 bool page_do_bit17_swizzling, 705 bool needs_clflush_before, 706 bool needs_clflush_after) 707 { 708 char *vaddr; 709 int ret; 710 711 if (unlikely(page_do_bit17_swizzling)) 712 return -EINVAL; 713 714 vaddr = kmap_atomic(page); 715 if (needs_clflush_before) 716 drm_clflush_virt_range(vaddr + shmem_page_offset, 717 page_length); 718 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset, 719 user_data, 720 page_length); 721 if (needs_clflush_after) 722 drm_clflush_virt_range(vaddr + shmem_page_offset, 723 page_length); 724 kunmap_atomic(vaddr); 725 726 return ret ? -EFAULT : 0; 727 } 728 729 /* Only difference to the fast-path function is that this can handle bit17 730 * and uses non-atomic copy and kmap functions. */ 731 static int 732 shmem_pwrite_slow(struct vm_page *page, int shmem_page_offset, int page_length, 733 char __user *user_data, 734 bool page_do_bit17_swizzling, 735 bool needs_clflush_before, 736 bool needs_clflush_after) 737 { 738 char *vaddr; 739 int ret; 740 741 vaddr = kmap(page); 742 if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) 743 shmem_clflush_swizzled_range(vaddr + shmem_page_offset, 744 page_length, 745 page_do_bit17_swizzling); 746 if (page_do_bit17_swizzling) 747 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset, 748 user_data, 749 page_length); 750 else 751 ret = __copy_from_user(vaddr + shmem_page_offset, 752 user_data, 753 page_length); 754 if (needs_clflush_after) 755 shmem_clflush_swizzled_range(vaddr + shmem_page_offset, 756 page_length, 757 page_do_bit17_swizzling); 758 kunmap(page); 759 760 return ret ? -EFAULT : 0; 761 } 762 763 static int 764 i915_gem_shmem_pwrite(struct drm_device *dev, 765 struct drm_i915_gem_object *obj, 766 struct drm_i915_gem_pwrite *args, 767 struct drm_file *file) 768 { 769 ssize_t remain; 770 loff_t offset; 771 char __user *user_data; 772 int shmem_page_offset, page_length, ret = 0; 773 int obj_do_bit17_swizzling, page_do_bit17_swizzling; 774 int hit_slowpath = 0; 775 int needs_clflush_after = 0; 776 int needs_clflush_before = 0; 777 int i; 778 779 user_data = to_user_ptr(args->data_ptr); 780 remain = args->size; 781 782 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); 783 784 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) { 785 /* If we're not in the cpu write domain, set ourself into the gtt 786 * write domain and manually flush cachelines (if required). This 787 * optimizes for the case when the gpu will use the data 788 * right away and we therefore have to clflush anyway. */ 789 needs_clflush_after = cpu_write_needs_clflush(obj); 790 ret = i915_gem_object_wait_rendering(obj, false); 791 if (ret) 792 return ret; 793 } 794 /* Same trick applies to invalidate partially written cachelines read 795 * before writing. */ 796 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) 797 needs_clflush_before = 798 !cpu_cache_is_coherent(dev, obj->cache_level); 799 800 ret = i915_gem_object_get_pages(obj); 801 if (ret) 802 return ret; 803 804 i915_gem_object_pin_pages(obj); 805 806 offset = args->offset; 807 obj->dirty = 1; 808 809 VM_OBJECT_LOCK(obj->base.vm_obj); 810 vm_object_pip_add(obj->base.vm_obj, 1); 811 for (i = 0; i < (obj->base.size >> PAGE_SHIFT); i++) { 812 struct vm_page *page = obj->pages[i]; 813 int partial_cacheline_write; 814 815 if (i < offset >> PAGE_SHIFT) 816 continue; 817 818 if (remain <= 0) 819 break; 820 821 /* Operation in this page 822 * 823 * shmem_page_offset = offset within page in shmem file 824 * page_length = bytes to copy for this page 825 */ 826 shmem_page_offset = offset_in_page(offset); 827 828 page_length = remain; 829 if ((shmem_page_offset + page_length) > PAGE_SIZE) 830 page_length = PAGE_SIZE - shmem_page_offset; 831 832 /* If we don't overwrite a cacheline completely we need to be 833 * careful to have up-to-date data by first clflushing. Don't 834 * overcomplicate things and flush the entire patch. */ 835 partial_cacheline_write = needs_clflush_before && 836 ((shmem_page_offset | page_length) 837 & (cpu_clflush_line_size - 1)); 838 839 page_do_bit17_swizzling = obj_do_bit17_swizzling && 840 (page_to_phys(page) & (1 << 17)) != 0; 841 842 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length, 843 user_data, page_do_bit17_swizzling, 844 partial_cacheline_write, 845 needs_clflush_after); 846 if (ret == 0) 847 goto next_page; 848 849 hit_slowpath = 1; 850 mutex_unlock(&dev->struct_mutex); 851 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length, 852 user_data, page_do_bit17_swizzling, 853 partial_cacheline_write, 854 needs_clflush_after); 855 856 mutex_lock(&dev->struct_mutex); 857 858 next_page: 859 set_page_dirty(page); 860 mark_page_accessed(page); 861 862 if (ret) 863 goto out; 864 865 remain -= page_length; 866 user_data += page_length; 867 offset += page_length; 868 } 869 vm_object_pip_wakeup(obj->base.vm_obj); 870 VM_OBJECT_UNLOCK(obj->base.vm_obj); 871 872 out: 873 i915_gem_object_unpin_pages(obj); 874 875 if (hit_slowpath) { 876 /* 877 * Fixup: Flush cpu caches in case we didn't flush the dirty 878 * cachelines in-line while writing and the object moved 879 * out of the cpu write domain while we've dropped the lock. 880 */ 881 if (!needs_clflush_after && 882 obj->base.write_domain != I915_GEM_DOMAIN_CPU) { 883 if (i915_gem_clflush_object(obj, obj->pin_display)) 884 i915_gem_chipset_flush(dev); 885 } 886 } 887 888 if (needs_clflush_after) 889 i915_gem_chipset_flush(dev); 890 891 return ret; 892 } 893 894 /** 895 * Writes data to the object referenced by handle. 896 * 897 * On error, the contents of the buffer that were to be modified are undefined. 898 */ 899 int 900 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, 901 struct drm_file *file) 902 { 903 struct drm_i915_gem_pwrite *args = data; 904 struct drm_i915_gem_object *obj; 905 int ret; 906 907 if (args->size == 0) 908 return 0; 909 910 if (likely(!i915_prefault_disable)) { 911 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr), 912 args->size); 913 if (ret) 914 return -EFAULT; 915 } 916 917 ret = i915_mutex_lock_interruptible(dev); 918 if (ret) 919 return ret; 920 921 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 922 if (&obj->base == NULL) { 923 ret = -ENOENT; 924 goto unlock; 925 } 926 927 /* Bounds check destination. */ 928 if (args->offset > obj->base.size || 929 args->size > obj->base.size - args->offset) { 930 ret = -EINVAL; 931 goto out; 932 } 933 934 trace_i915_gem_object_pwrite(obj, args->offset, args->size); 935 936 ret = -EFAULT; 937 /* We can only do the GTT pwrite on untiled buffers, as otherwise 938 * it would end up going through the fenced access, and we'll get 939 * different detiling behavior between reading and writing. 940 * pread/pwrite currently are reading and writing from the CPU 941 * perspective, requiring manual detiling by the client. 942 */ 943 if (obj->phys_obj) { 944 ret = i915_gem_phys_pwrite(dev, obj, args, file); 945 goto out; 946 } 947 948 if (obj->tiling_mode == I915_TILING_NONE && 949 obj->base.write_domain != I915_GEM_DOMAIN_CPU && 950 cpu_write_needs_clflush(obj)) { 951 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file); 952 /* Note that the gtt paths might fail with non-page-backed user 953 * pointers (e.g. gtt mappings when moving data between 954 * textures). Fallback to the shmem path in that case. */ 955 } 956 957 if (ret == -EFAULT || ret == -ENOSPC) 958 ret = i915_gem_shmem_pwrite(dev, obj, args, file); 959 960 out: 961 drm_gem_object_unreference(&obj->base); 962 unlock: 963 mutex_unlock(&dev->struct_mutex); 964 return ret; 965 } 966 967 int 968 i915_gem_check_wedge(struct i915_gpu_error *error, 969 bool interruptible) 970 { 971 if (i915_reset_in_progress(error)) { 972 /* Non-interruptible callers can't handle -EAGAIN, hence return 973 * -EIO unconditionally for these. */ 974 if (!interruptible) 975 return -EIO; 976 977 /* Recovery complete, but the reset failed ... */ 978 if (i915_terminally_wedged(error)) 979 return -EIO; 980 981 return -EAGAIN; 982 } 983 984 return 0; 985 } 986 987 /* 988 * Compare seqno against outstanding lazy request. Emit a request if they are 989 * equal. 990 */ 991 static int 992 i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno) 993 { 994 int ret; 995 996 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex)); 997 998 ret = 0; 999 if (seqno == ring->outstanding_lazy_seqno) 1000 ret = i915_add_request(ring, NULL); 1001 1002 return ret; 1003 } 1004 1005 #if 0 1006 static void fake_irq(unsigned long data) 1007 { 1008 wake_up_process((struct task_struct *)data); 1009 } 1010 1011 static bool missed_irq(struct drm_i915_private *dev_priv, 1012 struct intel_ring_buffer *ring) 1013 { 1014 return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings); 1015 } 1016 1017 static bool can_wait_boost(struct drm_i915_file_private *file_priv) 1018 { 1019 if (file_priv == NULL) 1020 return true; 1021 1022 return !atomic_xchg(&file_priv->rps_wait_boost, true); 1023 } 1024 #endif 1025 1026 /** 1027 * __wait_seqno - wait until execution of seqno has finished 1028 * @ring: the ring expected to report seqno 1029 * @seqno: duh! 1030 * @reset_counter: reset sequence associated with the given seqno 1031 * @interruptible: do an interruptible wait (normally yes) 1032 * @timeout: in - how long to wait (NULL forever); out - how much time remaining 1033 * 1034 * Note: It is of utmost importance that the passed in seqno and reset_counter 1035 * values have been read by the caller in an smp safe manner. Where read-side 1036 * locks are involved, it is sufficient to read the reset_counter before 1037 * unlocking the lock that protects the seqno. For lockless tricks, the 1038 * reset_counter _must_ be read before, and an appropriate smp_rmb must be 1039 * inserted. 1040 * 1041 * Returns 0 if the seqno was found within the alloted time. Else returns the 1042 * errno with remaining time filled in timeout argument. 1043 */ 1044 static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno, 1045 unsigned reset_counter, 1046 bool interruptible, struct timespec *timeout) 1047 { 1048 drm_i915_private_t *dev_priv = ring->dev->dev_private; 1049 struct timespec before, now, wait_time={1,0}; 1050 unsigned long timeout_jiffies; 1051 long end; 1052 bool wait_forever = true; 1053 int ret; 1054 1055 WARN(dev_priv->pc8.irqs_disabled, "IRQs disabled\n"); 1056 1057 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno)) 1058 return 0; 1059 1060 trace_i915_gem_request_wait_begin(ring, seqno); 1061 1062 if (timeout != NULL) { 1063 wait_time = *timeout; 1064 wait_forever = false; 1065 } 1066 1067 timeout_jiffies = timespec_to_jiffies_timeout(&wait_time); 1068 1069 if (WARN_ON(!ring->irq_get(ring))) 1070 return -ENODEV; 1071 1072 /* Record current time in case interrupted by signal, or wedged * */ 1073 getrawmonotonic(&before); 1074 1075 #define EXIT_COND \ 1076 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \ 1077 i915_reset_in_progress(&dev_priv->gpu_error) || \ 1078 reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) 1079 do { 1080 if (interruptible) 1081 end = wait_event_interruptible_timeout(ring->irq_queue, 1082 EXIT_COND, 1083 timeout_jiffies); 1084 else 1085 end = wait_event_timeout(ring->irq_queue, EXIT_COND, 1086 timeout_jiffies); 1087 1088 /* We need to check whether any gpu reset happened in between 1089 * the caller grabbing the seqno and now ... */ 1090 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) 1091 end = -EAGAIN; 1092 1093 /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely 1094 * gone. */ 1095 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible); 1096 if (ret) 1097 end = ret; 1098 } while (end == 0 && wait_forever); 1099 1100 getrawmonotonic(&now); 1101 1102 ring->irq_put(ring); 1103 trace_i915_gem_request_wait_end(ring, seqno); 1104 #undef EXIT_COND 1105 1106 if (timeout) { 1107 struct timespec sleep_time = timespec_sub(now, before); 1108 *timeout = timespec_sub(*timeout, sleep_time); 1109 if (!timespec_valid(timeout)) /* i.e. negative time remains */ 1110 set_normalized_timespec(timeout, 0, 0); 1111 } 1112 1113 switch (end) { 1114 case -EIO: 1115 case -EAGAIN: /* Wedged */ 1116 case -ERESTARTSYS: /* Signal */ 1117 return (int)end; 1118 case 0: /* Timeout */ 1119 return -ETIMEDOUT; /* -ETIME on Linux */ 1120 default: /* Completed */ 1121 WARN_ON(end < 0); /* We're not aware of other errors */ 1122 return 0; 1123 } 1124 } 1125 1126 /** 1127 * Waits for a sequence number to be signaled, and cleans up the 1128 * request and object lists appropriately for that event. 1129 */ 1130 int 1131 i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno) 1132 { 1133 struct drm_device *dev = ring->dev; 1134 struct drm_i915_private *dev_priv = dev->dev_private; 1135 bool interruptible = dev_priv->mm.interruptible; 1136 int ret; 1137 1138 BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 1139 BUG_ON(seqno == 0); 1140 1141 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible); 1142 if (ret) 1143 return ret; 1144 1145 ret = i915_gem_check_olr(ring, seqno); 1146 if (ret) 1147 return ret; 1148 1149 return __wait_seqno(ring, seqno, 1150 atomic_read(&dev_priv->gpu_error.reset_counter), 1151 interruptible, NULL); 1152 } 1153 1154 static int 1155 i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj, 1156 struct intel_ring_buffer *ring) 1157 { 1158 i915_gem_retire_requests_ring(ring); 1159 1160 /* Manually manage the write flush as we may have not yet 1161 * retired the buffer. 1162 * 1163 * Note that the last_write_seqno is always the earlier of 1164 * the two (read/write) seqno, so if we haved successfully waited, 1165 * we know we have passed the last write. 1166 */ 1167 obj->last_write_seqno = 0; 1168 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS; 1169 1170 return 0; 1171 } 1172 1173 /** 1174 * Ensures that all rendering to the object has completed and the object is 1175 * safe to unbind from the GTT or access from the CPU. 1176 */ 1177 static __must_check int 1178 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, 1179 bool readonly) 1180 { 1181 struct intel_ring_buffer *ring = obj->ring; 1182 u32 seqno; 1183 int ret; 1184 1185 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno; 1186 if (seqno == 0) 1187 return 0; 1188 1189 ret = i915_wait_seqno(ring, seqno); 1190 if (ret) 1191 return ret; 1192 1193 return i915_gem_object_wait_rendering__tail(obj, ring); 1194 } 1195 1196 /* A nonblocking variant of the above wait. This is a highly dangerous routine 1197 * as the object state may change during this call. 1198 */ 1199 static __must_check int 1200 i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj, 1201 bool readonly) 1202 { 1203 struct drm_device *dev = obj->base.dev; 1204 struct drm_i915_private *dev_priv = dev->dev_private; 1205 struct intel_ring_buffer *ring = obj->ring; 1206 unsigned reset_counter; 1207 u32 seqno; 1208 int ret; 1209 1210 BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 1211 BUG_ON(!dev_priv->mm.interruptible); 1212 1213 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno; 1214 if (seqno == 0) 1215 return 0; 1216 1217 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true); 1218 if (ret) 1219 return ret; 1220 1221 ret = i915_gem_check_olr(ring, seqno); 1222 if (ret) 1223 return ret; 1224 1225 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); 1226 mutex_unlock(&dev->struct_mutex); 1227 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL); 1228 mutex_lock(&dev->struct_mutex); 1229 if (ret) 1230 return ret; 1231 1232 return i915_gem_object_wait_rendering__tail(obj, ring); 1233 } 1234 1235 /** 1236 * Called when user space prepares to use an object with the CPU, either 1237 * through the mmap ioctl's mapping or a GTT mapping. 1238 */ 1239 int 1240 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, 1241 struct drm_file *file) 1242 { 1243 struct drm_i915_gem_set_domain *args = data; 1244 struct drm_i915_gem_object *obj; 1245 uint32_t read_domains = args->read_domains; 1246 uint32_t write_domain = args->write_domain; 1247 int ret; 1248 1249 /* Only handle setting domains to types used by the CPU. */ 1250 if (write_domain & I915_GEM_GPU_DOMAINS) 1251 return -EINVAL; 1252 1253 if (read_domains & I915_GEM_GPU_DOMAINS) 1254 return -EINVAL; 1255 1256 /* Having something in the write domain implies it's in the read 1257 * domain, and only that read domain. Enforce that in the request. 1258 */ 1259 if (write_domain != 0 && read_domains != write_domain) 1260 return -EINVAL; 1261 1262 ret = i915_mutex_lock_interruptible(dev); 1263 if (ret) 1264 return ret; 1265 1266 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 1267 if (&obj->base == NULL) { 1268 ret = -ENOENT; 1269 goto unlock; 1270 } 1271 1272 /* Try to flush the object off the GPU without holding the lock. 1273 * We will repeat the flush holding the lock in the normal manner 1274 * to catch cases where we are gazumped. 1275 */ 1276 ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain); 1277 if (ret) 1278 goto unref; 1279 1280 if (read_domains & I915_GEM_DOMAIN_GTT) { 1281 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); 1282 1283 /* Silently promote "you're not bound, there was nothing to do" 1284 * to success, since the client was just asking us to 1285 * make sure everything was done. 1286 */ 1287 if (ret == -EINVAL) 1288 ret = 0; 1289 } else { 1290 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); 1291 } 1292 1293 unref: 1294 drm_gem_object_unreference(&obj->base); 1295 unlock: 1296 mutex_unlock(&dev->struct_mutex); 1297 return ret; 1298 } 1299 1300 /** 1301 * Called when user space has done writes to this buffer 1302 */ 1303 int 1304 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, 1305 struct drm_file *file) 1306 { 1307 struct drm_i915_gem_sw_finish *args = data; 1308 struct drm_i915_gem_object *obj; 1309 int ret = 0; 1310 1311 ret = i915_mutex_lock_interruptible(dev); 1312 if (ret) 1313 return ret; 1314 1315 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 1316 if (&obj->base == NULL) { 1317 ret = -ENOENT; 1318 goto unlock; 1319 } 1320 1321 /* Pinned buffers may be scanout, so flush the cache */ 1322 if (obj->pin_display) 1323 i915_gem_object_flush_cpu_write_domain(obj, true); 1324 1325 drm_gem_object_unreference(&obj->base); 1326 unlock: 1327 mutex_unlock(&dev->struct_mutex); 1328 return ret; 1329 } 1330 1331 /** 1332 * Maps the contents of an object, returning the address it is mapped 1333 * into. 1334 * 1335 * While the mapping holds a reference on the contents of the object, it doesn't 1336 * imply a ref on the object itself. 1337 */ 1338 int 1339 i915_gem_mmap_ioctl(struct drm_device *dev, void *data, 1340 struct drm_file *file) 1341 { 1342 struct drm_i915_gem_mmap *args = data; 1343 struct drm_gem_object *obj; 1344 unsigned long addr; 1345 struct proc *p = curproc; 1346 vm_map_t map = &p->p_vmspace->vm_map; 1347 vm_size_t size; 1348 int error = 0, rv; 1349 1350 obj = drm_gem_object_lookup(dev, file, args->handle); 1351 if (obj == NULL) 1352 return -ENOENT; 1353 1354 if (args->size == 0) 1355 goto out; 1356 1357 size = round_page(args->size); 1358 if (map->size + size > p->p_rlimit[RLIMIT_VMEM].rlim_cur) { 1359 error = -ENOMEM; 1360 goto out; 1361 } 1362 1363 /* 1364 * Call hint to ensure that NULL is not returned as a valid address 1365 * and to reduce vm_map traversals. 1366 */ 1367 addr = vm_map_hint(p, 0, PROT_READ|PROT_WRITE); 1368 1369 /* 1370 * Use 256KB alignment. It is unclear why this matters for a 1371 * virtual address but it appears to fix a number of application/X 1372 * crashes and kms console switching is much faster. 1373 */ 1374 vm_object_hold(obj->vm_obj); 1375 vm_object_reference_locked(obj->vm_obj); 1376 vm_object_drop(obj->vm_obj); 1377 1378 rv = vm_map_find(map, obj->vm_obj, NULL, 1379 args->offset, &addr, args->size, 1380 256 * 1024, /* align */ 1381 TRUE, /* fitit */ 1382 VM_MAPTYPE_NORMAL, /* maptype */ 1383 VM_PROT_READ | VM_PROT_WRITE, /* prot */ 1384 VM_PROT_READ | VM_PROT_WRITE, /* max */ 1385 MAP_SHARED /* cow */); 1386 if (rv != KERN_SUCCESS) { 1387 vm_object_deallocate(obj->vm_obj); 1388 error = -vm_mmap_to_errno(rv); 1389 } else { 1390 args->addr_ptr = (uint64_t)addr; 1391 } 1392 out: 1393 drm_gem_object_unreference(obj); 1394 return (error); 1395 } 1396 1397 /** 1398 * i915_gem_fault - fault a page into the GTT 1399 * vma: VMA in question 1400 * vmf: fault info 1401 * 1402 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped 1403 * from userspace. The fault handler takes care of binding the object to 1404 * the GTT (if needed), allocating and programming a fence register (again, 1405 * only if needed based on whether the old reg is still valid or the object 1406 * is tiled) and inserting a new PTE into the faulting process. 1407 * 1408 * Note that the faulting process may involve evicting existing objects 1409 * from the GTT and/or fence registers to make room. So performance may 1410 * suffer if the GTT working set is large or there are few fence registers 1411 * left. 1412 */ 1413 int i915_gem_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot, vm_page_t *mres) 1414 { 1415 struct drm_i915_gem_object *obj = to_intel_bo(vm_obj->handle); 1416 struct drm_device *dev = obj->base.dev; 1417 drm_i915_private_t *dev_priv = dev->dev_private; 1418 unsigned long page_offset; 1419 vm_page_t m, oldm = NULL; 1420 int ret = 0; 1421 bool write = !!(prot & VM_PROT_WRITE); 1422 1423 intel_runtime_pm_get(dev_priv); 1424 1425 /* We don't use vmf->pgoff since that has the fake offset */ 1426 page_offset = (unsigned long)offset; 1427 1428 /* Magic FreeBSD VM stuff */ 1429 vm_object_pip_add(vm_obj, 1); 1430 1431 /* 1432 * Remove the placeholder page inserted by vm_fault() from the 1433 * object before dropping the object lock. If 1434 * i915_gem_release_mmap() is active in parallel on this gem 1435 * object, then it owns the drm device sx and might find the 1436 * placeholder already. Then, since the page is busy, 1437 * i915_gem_release_mmap() sleeps waiting for the busy state 1438 * of the page cleared. We will be not able to acquire drm 1439 * device lock until i915_gem_release_mmap() is able to make a 1440 * progress. 1441 */ 1442 if (*mres != NULL) { 1443 oldm = *mres; 1444 vm_page_remove(oldm); 1445 *mres = NULL; 1446 } else 1447 oldm = NULL; 1448 retry: 1449 VM_OBJECT_UNLOCK(vm_obj); 1450 unlocked_vmobj: 1451 ret = 0; 1452 m = NULL; 1453 1454 mutex_lock(&dev->struct_mutex); 1455 1456 /* 1457 * Since the object lock was dropped, other thread might have 1458 * faulted on the same GTT address and instantiated the 1459 * mapping for the page. Recheck. 1460 */ 1461 VM_OBJECT_LOCK(vm_obj); 1462 m = vm_page_lookup(vm_obj, OFF_TO_IDX(offset)); 1463 if (m != NULL) { 1464 if ((m->flags & PG_BUSY) != 0) { 1465 mutex_unlock(&dev->struct_mutex); 1466 goto retry; 1467 } 1468 goto have_page; 1469 } else 1470 VM_OBJECT_UNLOCK(vm_obj); 1471 /* End magic VM stuff */ 1472 1473 trace_i915_gem_object_fault(obj, page_offset, true, write); 1474 1475 /* Access to snoopable pages through the GTT is incoherent. */ 1476 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) { 1477 ret = -EINVAL; 1478 goto unlock; 1479 } 1480 1481 /* Now bind it into the GTT if needed */ 1482 ret = i915_gem_obj_ggtt_pin(obj, 0, true, false); 1483 if (ret) 1484 goto unlock; 1485 1486 ret = i915_gem_object_set_to_gtt_domain(obj, write); 1487 if (ret) 1488 goto unpin; 1489 1490 ret = i915_gem_object_get_fence(obj); 1491 if (ret) 1492 goto unpin; 1493 1494 obj->fault_mappable = true; 1495 1496 VM_OBJECT_LOCK(vm_obj); 1497 m = vm_phys_fictitious_to_vm_page(dev->agp->base + 1498 i915_gem_obj_ggtt_offset(obj) + offset); 1499 if (m == NULL) { 1500 ret = -EFAULT; 1501 goto unpin; 1502 } 1503 KASSERT((m->flags & PG_FICTITIOUS) != 0, 1504 ("not fictitious %p", m)); 1505 KASSERT(m->wire_count == 1, ("wire_count not 1 %p", m)); 1506 1507 if ((m->flags & PG_BUSY) != 0) { 1508 i915_gem_object_unpin(obj); 1509 mutex_unlock(&dev->struct_mutex); 1510 goto retry; 1511 } 1512 m->valid = VM_PAGE_BITS_ALL; 1513 1514 /* Finally, remap it using the new GTT offset */ 1515 vm_page_insert(m, vm_obj, OFF_TO_IDX(offset)); 1516 have_page: 1517 *mres = m; 1518 vm_page_busy_try(m, false); 1519 1520 i915_gem_object_unpin(obj); 1521 mutex_unlock(&dev->struct_mutex); 1522 if (oldm != NULL) { 1523 vm_page_free(oldm); 1524 } 1525 vm_object_pip_wakeup(vm_obj); 1526 return (VM_PAGER_OK); 1527 1528 unpin: 1529 i915_gem_object_unpin(obj); 1530 unlock: 1531 mutex_unlock(&dev->struct_mutex); 1532 1533 KASSERT(ret != 0, ("i915_gem_pager_fault: wrong return")); 1534 switch (ret) { 1535 case -EIO: 1536 case -EAGAIN: 1537 case -EINTR: 1538 goto unlocked_vmobj; 1539 default: 1540 VM_OBJECT_LOCK(vm_obj); 1541 vm_object_pip_wakeup(vm_obj); 1542 ret = VM_PAGER_ERROR; 1543 } 1544 1545 intel_runtime_pm_put(dev_priv); 1546 return ret; 1547 } 1548 1549 void i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv) 1550 { 1551 struct i915_vma *vma; 1552 1553 /* 1554 * Only the global gtt is relevant for gtt memory mappings, so restrict 1555 * list traversal to objects bound into the global address space. Note 1556 * that the active list should be empty, but better safe than sorry. 1557 */ 1558 WARN_ON(!list_empty(&dev_priv->gtt.base.active_list)); 1559 list_for_each_entry(vma, &dev_priv->gtt.base.active_list, mm_list) 1560 i915_gem_release_mmap(vma->obj); 1561 list_for_each_entry(vma, &dev_priv->gtt.base.inactive_list, mm_list) 1562 i915_gem_release_mmap(vma->obj); 1563 } 1564 1565 /** 1566 * i915_gem_release_mmap - remove physical page mappings 1567 * @obj: obj in question 1568 * 1569 * Preserve the reservation of the mmapping with the DRM core code, but 1570 * relinquish ownership of the pages back to the system. 1571 * 1572 * It is vital that we remove the page mapping if we have mapped a tiled 1573 * object through the GTT and then lose the fence register due to 1574 * resource pressure. Similarly if the object has been moved out of the 1575 * aperture, than pages mapped into userspace must be revoked. Removing the 1576 * mapping will then trigger a page fault on the next user access, allowing 1577 * fixup by i915_gem_fault(). 1578 */ 1579 void 1580 i915_gem_release_mmap(struct drm_i915_gem_object *obj) 1581 { 1582 vm_object_t devobj; 1583 vm_page_t m; 1584 int i, page_count; 1585 1586 if (!obj->fault_mappable) 1587 return; 1588 1589 devobj = cdev_pager_lookup(obj); 1590 if (devobj != NULL) { 1591 page_count = OFF_TO_IDX(obj->base.size); 1592 1593 VM_OBJECT_LOCK(devobj); 1594 for (i = 0; i < page_count; i++) { 1595 m = vm_page_lookup_busy_wait(devobj, i, TRUE, "915unm"); 1596 if (m == NULL) 1597 continue; 1598 cdev_pager_free_page(devobj, m); 1599 } 1600 VM_OBJECT_UNLOCK(devobj); 1601 vm_object_deallocate(devobj); 1602 } 1603 1604 obj->fault_mappable = false; 1605 } 1606 1607 uint32_t 1608 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode) 1609 { 1610 uint32_t gtt_size; 1611 1612 if (INTEL_INFO(dev)->gen >= 4 || 1613 tiling_mode == I915_TILING_NONE) 1614 return size; 1615 1616 /* Previous chips need a power-of-two fence region when tiling */ 1617 if (INTEL_INFO(dev)->gen == 3) 1618 gtt_size = 1024*1024; 1619 else 1620 gtt_size = 512*1024; 1621 1622 while (gtt_size < size) 1623 gtt_size <<= 1; 1624 1625 return gtt_size; 1626 } 1627 1628 /** 1629 * i915_gem_get_gtt_alignment - return required GTT alignment for an object 1630 * @obj: object to check 1631 * 1632 * Return the required GTT alignment for an object, taking into account 1633 * potential fence register mapping. 1634 */ 1635 uint32_t 1636 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size, 1637 int tiling_mode, bool fenced) 1638 { 1639 /* 1640 * Minimum alignment is 4k (GTT page size), but might be greater 1641 * if a fence register is needed for the object. 1642 */ 1643 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) || 1644 tiling_mode == I915_TILING_NONE) 1645 return 4096; 1646 1647 /* 1648 * Previous chips need to be aligned to the size of the smallest 1649 * fence register that can contain the object. 1650 */ 1651 return i915_gem_get_gtt_size(dev, size, tiling_mode); 1652 } 1653 1654 static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) 1655 { 1656 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 1657 int ret; 1658 1659 #if 0 1660 if (drm_vma_node_has_offset(&obj->base.vma_node)) 1661 return 0; 1662 #endif 1663 1664 dev_priv->mm.shrinker_no_lock_stealing = true; 1665 1666 ret = drm_gem_create_mmap_offset(&obj->base); 1667 if (ret != -ENOSPC) 1668 goto out; 1669 1670 /* Badly fragmented mmap space? The only way we can recover 1671 * space is by destroying unwanted objects. We can't randomly release 1672 * mmap_offsets as userspace expects them to be persistent for the 1673 * lifetime of the objects. The closest we can is to release the 1674 * offsets on purgeable objects by truncating it and marking it purged, 1675 * which prevents userspace from ever using that object again. 1676 */ 1677 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT); 1678 ret = drm_gem_create_mmap_offset(&obj->base); 1679 if (ret != -ENOSPC) 1680 goto out; 1681 1682 i915_gem_shrink_all(dev_priv); 1683 ret = drm_gem_create_mmap_offset(&obj->base); 1684 out: 1685 dev_priv->mm.shrinker_no_lock_stealing = false; 1686 1687 return ret; 1688 } 1689 1690 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) 1691 { 1692 drm_gem_free_mmap_offset(&obj->base); 1693 } 1694 1695 int 1696 i915_gem_mmap_gtt(struct drm_file *file, 1697 struct drm_device *dev, 1698 uint32_t handle, 1699 uint64_t *offset) 1700 { 1701 struct drm_i915_private *dev_priv = dev->dev_private; 1702 struct drm_i915_gem_object *obj; 1703 int ret; 1704 1705 ret = i915_mutex_lock_interruptible(dev); 1706 if (ret) 1707 return ret; 1708 1709 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle)); 1710 if (&obj->base == NULL) { 1711 ret = -ENOENT; 1712 goto unlock; 1713 } 1714 1715 if (obj->base.size > dev_priv->gtt.mappable_end) { 1716 ret = -E2BIG; 1717 goto out; 1718 } 1719 1720 if (obj->madv != I915_MADV_WILLNEED) { 1721 DRM_ERROR("Attempting to mmap a purgeable buffer\n"); 1722 ret = -EINVAL; 1723 goto out; 1724 } 1725 1726 ret = i915_gem_object_create_mmap_offset(obj); 1727 if (ret) 1728 goto out; 1729 1730 *offset = DRM_GEM_MAPPING_OFF(obj->base.map_list.key) | 1731 DRM_GEM_MAPPING_KEY; 1732 1733 out: 1734 drm_gem_object_unreference(&obj->base); 1735 unlock: 1736 mutex_unlock(&dev->struct_mutex); 1737 return ret; 1738 } 1739 1740 /** 1741 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing 1742 * @dev: DRM device 1743 * @data: GTT mapping ioctl data 1744 * @file: GEM object info 1745 * 1746 * Simply returns the fake offset to userspace so it can mmap it. 1747 * The mmap call will end up in drm_gem_mmap(), which will set things 1748 * up so we can get faults in the handler above. 1749 * 1750 * The fault handler will take care of binding the object into the GTT 1751 * (since it may have been evicted to make room for something), allocating 1752 * a fence register, and mapping the appropriate aperture address into 1753 * userspace. 1754 */ 1755 int 1756 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, 1757 struct drm_file *file) 1758 { 1759 struct drm_i915_gem_mmap_gtt *args = data; 1760 1761 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); 1762 } 1763 1764 /* Immediately discard the backing storage */ 1765 static void 1766 i915_gem_object_truncate(struct drm_i915_gem_object *obj) 1767 { 1768 vm_object_t vm_obj; 1769 1770 vm_obj = obj->base.vm_obj; 1771 VM_OBJECT_LOCK(vm_obj); 1772 vm_object_page_remove(vm_obj, 0, 0, false); 1773 VM_OBJECT_UNLOCK(vm_obj); 1774 1775 obj->madv = __I915_MADV_PURGED; 1776 } 1777 1778 static inline int 1779 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj) 1780 { 1781 return obj->madv == I915_MADV_DONTNEED; 1782 } 1783 1784 static void 1785 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj) 1786 { 1787 int page_count = obj->base.size / PAGE_SIZE; 1788 int i, ret; 1789 1790 if (!obj->pages) 1791 return; 1792 1793 BUG_ON(obj->madv == __I915_MADV_PURGED); 1794 1795 ret = i915_gem_object_set_to_cpu_domain(obj, true); 1796 if (ret) { 1797 /* In the event of a disaster, abandon all caches and 1798 * hope for the best. 1799 */ 1800 WARN_ON(ret != -EIO); 1801 i915_gem_clflush_object(obj, true); 1802 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU; 1803 } 1804 1805 if (i915_gem_object_needs_bit17_swizzle(obj)) 1806 i915_gem_object_save_bit_17_swizzle(obj); 1807 1808 if (obj->madv == I915_MADV_DONTNEED) 1809 obj->dirty = 0; 1810 1811 for (i = 0; i < page_count; i++) { 1812 struct vm_page *page = obj->pages[i]; 1813 1814 if (obj->dirty) 1815 set_page_dirty(page); 1816 1817 if (obj->madv == I915_MADV_WILLNEED) 1818 mark_page_accessed(page); 1819 1820 vm_page_busy_wait(obj->pages[i], FALSE, "i915gem"); 1821 vm_page_unwire(obj->pages[i], 1); 1822 vm_page_wakeup(obj->pages[i]); 1823 } 1824 obj->dirty = 0; 1825 1826 kfree(obj->pages); 1827 obj->pages = NULL; 1828 } 1829 1830 int 1831 i915_gem_object_put_pages(struct drm_i915_gem_object *obj) 1832 { 1833 const struct drm_i915_gem_object_ops *ops = obj->ops; 1834 1835 if (obj->pages == NULL) 1836 return 0; 1837 1838 if (obj->pages_pin_count) 1839 return -EBUSY; 1840 1841 BUG_ON(i915_gem_obj_bound_any(obj)); 1842 1843 /* ->put_pages might need to allocate memory for the bit17 swizzle 1844 * array, hence protect them from being reaped by removing them from gtt 1845 * lists early. */ 1846 list_del(&obj->global_list); 1847 1848 ops->put_pages(obj); 1849 obj->pages = NULL; 1850 1851 if (i915_gem_object_is_purgeable(obj)) 1852 i915_gem_object_truncate(obj); 1853 1854 return 0; 1855 } 1856 1857 static unsigned long 1858 __i915_gem_shrink(struct drm_i915_private *dev_priv, long target, 1859 bool purgeable_only) 1860 { 1861 struct drm_i915_gem_object *obj, *next; 1862 unsigned long count = 0; 1863 1864 list_for_each_entry_safe(obj, next, 1865 &dev_priv->mm.unbound_list, 1866 global_list) { 1867 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) && 1868 i915_gem_object_put_pages(obj) == 0) { 1869 count += obj->base.size >> PAGE_SHIFT; 1870 if (count >= target) 1871 return count; 1872 } 1873 } 1874 1875 list_for_each_entry_safe(obj, next, &dev_priv->mm.bound_list, 1876 global_list) { 1877 struct i915_vma *vma, *v; 1878 1879 if (!i915_gem_object_is_purgeable(obj) && purgeable_only) 1880 continue; 1881 1882 list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link) 1883 if (i915_vma_unbind(vma)) 1884 break; 1885 1886 if (!i915_gem_object_put_pages(obj)) { 1887 count += obj->base.size >> PAGE_SHIFT; 1888 if (count >= target) 1889 return count; 1890 } 1891 } 1892 1893 return count; 1894 } 1895 1896 static unsigned long 1897 i915_gem_purge(struct drm_i915_private *dev_priv, long target) 1898 { 1899 return __i915_gem_shrink(dev_priv, target, true); 1900 } 1901 1902 static unsigned long 1903 i915_gem_shrink_all(struct drm_i915_private *dev_priv) 1904 { 1905 struct drm_i915_gem_object *obj, *next; 1906 long freed = 0; 1907 1908 i915_gem_evict_everything(dev_priv->dev); 1909 1910 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, 1911 global_list) { 1912 if (i915_gem_object_put_pages(obj) == 0) 1913 freed += obj->base.size >> PAGE_SHIFT; 1914 } 1915 return freed; 1916 } 1917 1918 static int 1919 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) 1920 { 1921 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 1922 int page_count, i, j; 1923 vm_object_t vm_obj; 1924 struct vm_page *page; 1925 1926 /* Assert that the object is not currently in any GPU domain. As it 1927 * wasn't in the GTT, there shouldn't be any way it could have been in 1928 * a GPU cache 1929 */ 1930 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); 1931 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); 1932 1933 page_count = obj->base.size / PAGE_SIZE; 1934 obj->pages = kmalloc(page_count * sizeof(vm_page_t), M_DRM, 1935 M_WAITOK); 1936 1937 /* Get the list of pages out of our struct file. They'll be pinned 1938 * at this point until we release them. 1939 * 1940 * Fail silently without starting the shrinker 1941 */ 1942 vm_obj = obj->base.vm_obj; 1943 VM_OBJECT_LOCK(vm_obj); 1944 for (i = 0; i < page_count; i++) { 1945 page = shmem_read_mapping_page(vm_obj, i); 1946 if (IS_ERR(page)) { 1947 i915_gem_purge(dev_priv, page_count); 1948 page = shmem_read_mapping_page(vm_obj, i); 1949 } 1950 if (IS_ERR(page)) { 1951 /* We've tried hard to allocate the memory by reaping 1952 * our own buffer, now let the real VM do its job and 1953 * go down in flames if truly OOM. 1954 */ 1955 1956 i915_gem_shrink_all(dev_priv); 1957 page = shmem_read_mapping_page(vm_obj, i); 1958 if (IS_ERR(page)) 1959 goto err_pages; 1960 } 1961 #ifdef CONFIG_SWIOTLB 1962 if (swiotlb_nr_tbl()) { 1963 st->nents++; 1964 sg_set_page(sg, page, PAGE_SIZE, 0); 1965 sg = sg_next(sg); 1966 continue; 1967 } 1968 #endif 1969 obj->pages[i] = page; 1970 } 1971 #ifdef CONFIG_SWIOTLB 1972 if (!swiotlb_nr_tbl()) 1973 #endif 1974 VM_OBJECT_UNLOCK(vm_obj); 1975 1976 if (i915_gem_object_needs_bit17_swizzle(obj)) 1977 i915_gem_object_do_bit_17_swizzle(obj); 1978 1979 return 0; 1980 1981 err_pages: 1982 for (j = 0; j < i; j++) { 1983 page = obj->pages[j]; 1984 vm_page_busy_wait(page, FALSE, "i915gem"); 1985 vm_page_unwire(page, 0); 1986 vm_page_wakeup(page); 1987 } 1988 VM_OBJECT_UNLOCK(vm_obj); 1989 kfree(obj->pages); 1990 obj->pages = NULL; 1991 return (-EIO); 1992 } 1993 1994 /* Ensure that the associated pages are gathered from the backing storage 1995 * and pinned into our object. i915_gem_object_get_pages() may be called 1996 * multiple times before they are released by a single call to 1997 * i915_gem_object_put_pages() - once the pages are no longer referenced 1998 * either as a result of memory pressure (reaping pages under the shrinker) 1999 * or as the object is itself released. 2000 */ 2001 int 2002 i915_gem_object_get_pages(struct drm_i915_gem_object *obj) 2003 { 2004 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 2005 const struct drm_i915_gem_object_ops *ops = obj->ops; 2006 int ret; 2007 2008 if (obj->pages) 2009 return 0; 2010 2011 if (obj->madv != I915_MADV_WILLNEED) { 2012 DRM_ERROR("Attempting to obtain a purgeable object\n"); 2013 return -EINVAL; 2014 } 2015 2016 BUG_ON(obj->pages_pin_count); 2017 2018 ret = ops->get_pages(obj); 2019 if (ret) 2020 return ret; 2021 2022 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list); 2023 return 0; 2024 } 2025 2026 static void 2027 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj, 2028 struct intel_ring_buffer *ring) 2029 { 2030 struct drm_device *dev = obj->base.dev; 2031 struct drm_i915_private *dev_priv = dev->dev_private; 2032 u32 seqno = intel_ring_get_seqno(ring); 2033 2034 BUG_ON(ring == NULL); 2035 if (obj->ring != ring && obj->last_write_seqno) { 2036 /* Keep the seqno relative to the current ring */ 2037 obj->last_write_seqno = seqno; 2038 } 2039 obj->ring = ring; 2040 2041 /* Add a reference if we're newly entering the active list. */ 2042 if (!obj->active) { 2043 drm_gem_object_reference(&obj->base); 2044 obj->active = 1; 2045 } 2046 2047 list_move_tail(&obj->ring_list, &ring->active_list); 2048 2049 obj->last_read_seqno = seqno; 2050 2051 if (obj->fenced_gpu_access) { 2052 obj->last_fenced_seqno = seqno; 2053 2054 /* Bump MRU to take account of the delayed flush */ 2055 if (obj->fence_reg != I915_FENCE_REG_NONE) { 2056 struct drm_i915_fence_reg *reg; 2057 2058 reg = &dev_priv->fence_regs[obj->fence_reg]; 2059 list_move_tail(®->lru_list, 2060 &dev_priv->mm.fence_list); 2061 } 2062 } 2063 } 2064 2065 void i915_vma_move_to_active(struct i915_vma *vma, 2066 struct intel_ring_buffer *ring) 2067 { 2068 list_move_tail(&vma->mm_list, &vma->vm->active_list); 2069 return i915_gem_object_move_to_active(vma->obj, ring); 2070 } 2071 2072 static void 2073 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj) 2074 { 2075 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 2076 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; 2077 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); 2078 2079 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS); 2080 BUG_ON(!obj->active); 2081 2082 list_move_tail(&vma->mm_list, &ggtt_vm->inactive_list); 2083 2084 list_del_init(&obj->ring_list); 2085 obj->ring = NULL; 2086 2087 obj->last_read_seqno = 0; 2088 obj->last_write_seqno = 0; 2089 obj->base.write_domain = 0; 2090 2091 obj->last_fenced_seqno = 0; 2092 obj->fenced_gpu_access = false; 2093 2094 obj->active = 0; 2095 drm_gem_object_unreference(&obj->base); 2096 2097 WARN_ON(i915_verify_lists(dev)); 2098 } 2099 2100 static int 2101 i915_gem_init_seqno(struct drm_device *dev, u32 seqno) 2102 { 2103 struct drm_i915_private *dev_priv = dev->dev_private; 2104 struct intel_ring_buffer *ring; 2105 int ret, i, j; 2106 2107 /* Carefully retire all requests without writing to the rings */ 2108 for_each_ring(ring, dev_priv, i) { 2109 ret = intel_ring_idle(ring); 2110 if (ret) 2111 return ret; 2112 } 2113 i915_gem_retire_requests(dev); 2114 2115 /* Finally reset hw state */ 2116 for_each_ring(ring, dev_priv, i) { 2117 intel_ring_init_seqno(ring, seqno); 2118 2119 for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++) 2120 ring->sync_seqno[j] = 0; 2121 } 2122 2123 return 0; 2124 } 2125 2126 int i915_gem_set_seqno(struct drm_device *dev, u32 seqno) 2127 { 2128 struct drm_i915_private *dev_priv = dev->dev_private; 2129 int ret; 2130 2131 if (seqno == 0) 2132 return -EINVAL; 2133 2134 /* HWS page needs to be set less than what we 2135 * will inject to ring 2136 */ 2137 ret = i915_gem_init_seqno(dev, seqno - 1); 2138 if (ret) 2139 return ret; 2140 2141 /* Carefully set the last_seqno value so that wrap 2142 * detection still works 2143 */ 2144 dev_priv->next_seqno = seqno; 2145 dev_priv->last_seqno = seqno - 1; 2146 if (dev_priv->last_seqno == 0) 2147 dev_priv->last_seqno--; 2148 2149 return 0; 2150 } 2151 2152 int 2153 i915_gem_get_seqno(struct drm_device *dev, u32 *seqno) 2154 { 2155 struct drm_i915_private *dev_priv = dev->dev_private; 2156 2157 /* reserve 0 for non-seqno */ 2158 if (dev_priv->next_seqno == 0) { 2159 int ret = i915_gem_init_seqno(dev, 0); 2160 if (ret) 2161 return ret; 2162 2163 dev_priv->next_seqno = 1; 2164 } 2165 2166 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++; 2167 return 0; 2168 } 2169 2170 int __i915_add_request(struct intel_ring_buffer *ring, 2171 struct drm_file *file, 2172 struct drm_i915_gem_object *obj, 2173 u32 *out_seqno) 2174 { 2175 drm_i915_private_t *dev_priv = ring->dev->dev_private; 2176 struct drm_i915_gem_request *request; 2177 u32 request_ring_position, request_start; 2178 int was_empty; 2179 int ret; 2180 2181 request_start = intel_ring_get_tail(ring); 2182 /* 2183 * Emit any outstanding flushes - execbuf can fail to emit the flush 2184 * after having emitted the batchbuffer command. Hence we need to fix 2185 * things up similar to emitting the lazy request. The difference here 2186 * is that the flush _must_ happen before the next request, no matter 2187 * what. 2188 */ 2189 ret = intel_ring_flush_all_caches(ring); 2190 if (ret) 2191 return ret; 2192 2193 request = ring->preallocated_lazy_request; 2194 if (WARN_ON(request == NULL)) 2195 return -ENOMEM; 2196 2197 /* Record the position of the start of the request so that 2198 * should we detect the updated seqno part-way through the 2199 * GPU processing the request, we never over-estimate the 2200 * position of the head. 2201 */ 2202 request_ring_position = intel_ring_get_tail(ring); 2203 2204 ret = ring->add_request(ring); 2205 if (ret) 2206 return ret; 2207 2208 request->seqno = intel_ring_get_seqno(ring); 2209 request->ring = ring; 2210 request->head = request_start; 2211 request->tail = request_ring_position; 2212 2213 /* Whilst this request exists, batch_obj will be on the 2214 * active_list, and so will hold the active reference. Only when this 2215 * request is retired will the the batch_obj be moved onto the 2216 * inactive_list and lose its active reference. Hence we do not need 2217 * to explicitly hold another reference here. 2218 */ 2219 request->batch_obj = obj; 2220 2221 /* Hold a reference to the current context so that we can inspect 2222 * it later in case a hangcheck error event fires. 2223 */ 2224 request->ctx = ring->last_context; 2225 if (request->ctx) 2226 i915_gem_context_reference(request->ctx); 2227 2228 request->emitted_jiffies = jiffies; 2229 was_empty = list_empty(&ring->request_list); 2230 list_add_tail(&request->list, &ring->request_list); 2231 request->file_priv = NULL; 2232 2233 if (file) { 2234 struct drm_i915_file_private *file_priv = file->driver_priv; 2235 2236 spin_lock(&file_priv->mm.lock); 2237 request->file_priv = file_priv; 2238 list_add_tail(&request->client_list, 2239 &file_priv->mm.request_list); 2240 spin_unlock(&file_priv->mm.lock); 2241 } 2242 2243 trace_i915_gem_request_add(ring, request->seqno); 2244 ring->outstanding_lazy_seqno = 0; 2245 ring->preallocated_lazy_request = NULL; 2246 2247 if (!dev_priv->ums.mm_suspended) { 2248 i915_queue_hangcheck(ring->dev); 2249 2250 if (was_empty) { 2251 cancel_delayed_work_sync(&dev_priv->mm.idle_work); 2252 queue_delayed_work(dev_priv->wq, 2253 &dev_priv->mm.retire_work, 2254 round_jiffies_up_relative(HZ)); 2255 intel_mark_busy(dev_priv->dev); 2256 } 2257 } 2258 2259 if (out_seqno) 2260 *out_seqno = request->seqno; 2261 return 0; 2262 } 2263 2264 static inline void 2265 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) 2266 { 2267 struct drm_i915_file_private *file_priv = request->file_priv; 2268 2269 if (!file_priv) 2270 return; 2271 2272 spin_lock(&file_priv->mm.lock); 2273 if (request->file_priv) { 2274 list_del(&request->client_list); 2275 request->file_priv = NULL; 2276 } 2277 spin_unlock(&file_priv->mm.lock); 2278 } 2279 2280 static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj, 2281 struct i915_address_space *vm) 2282 { 2283 if (acthd >= i915_gem_obj_offset(obj, vm) && 2284 acthd < i915_gem_obj_offset(obj, vm) + obj->base.size) 2285 return true; 2286 2287 return false; 2288 } 2289 2290 static bool i915_head_inside_request(const u32 acthd_unmasked, 2291 const u32 request_start, 2292 const u32 request_end) 2293 { 2294 const u32 acthd = acthd_unmasked & HEAD_ADDR; 2295 2296 if (request_start < request_end) { 2297 if (acthd >= request_start && acthd < request_end) 2298 return true; 2299 } else if (request_start > request_end) { 2300 if (acthd >= request_start || acthd < request_end) 2301 return true; 2302 } 2303 2304 return false; 2305 } 2306 2307 static struct i915_address_space * 2308 request_to_vm(struct drm_i915_gem_request *request) 2309 { 2310 struct drm_i915_private *dev_priv = request->ring->dev->dev_private; 2311 struct i915_address_space *vm; 2312 2313 vm = &dev_priv->gtt.base; 2314 2315 return vm; 2316 } 2317 2318 static bool i915_request_guilty(struct drm_i915_gem_request *request, 2319 const u32 acthd, bool *inside) 2320 { 2321 /* There is a possibility that unmasked head address 2322 * pointing inside the ring, matches the batch_obj address range. 2323 * However this is extremely unlikely. 2324 */ 2325 if (request->batch_obj) { 2326 if (i915_head_inside_object(acthd, request->batch_obj, 2327 request_to_vm(request))) { 2328 *inside = true; 2329 return true; 2330 } 2331 } 2332 2333 if (i915_head_inside_request(acthd, request->head, request->tail)) { 2334 *inside = false; 2335 return true; 2336 } 2337 2338 return false; 2339 } 2340 2341 static bool i915_context_is_banned(const struct i915_ctx_hang_stats *hs) 2342 { 2343 const unsigned long elapsed = get_seconds() - hs->guilty_ts; 2344 2345 if (hs->banned) 2346 return true; 2347 2348 if (elapsed <= DRM_I915_CTX_BAN_PERIOD) { 2349 DRM_ERROR("context hanging too fast, declaring banned!\n"); 2350 return true; 2351 } 2352 2353 return false; 2354 } 2355 2356 static void i915_set_reset_status(struct intel_ring_buffer *ring, 2357 struct drm_i915_gem_request *request, 2358 u32 acthd) 2359 { 2360 struct i915_ctx_hang_stats *hs = NULL; 2361 bool inside, guilty; 2362 unsigned long offset = 0; 2363 2364 /* Innocent until proven guilty */ 2365 guilty = false; 2366 2367 if (request->batch_obj) 2368 offset = i915_gem_obj_offset(request->batch_obj, 2369 request_to_vm(request)); 2370 2371 if (ring->hangcheck.action != HANGCHECK_WAIT && 2372 i915_request_guilty(request, acthd, &inside)) { 2373 DRM_DEBUG("%s hung %s bo (0x%lx ctx %d) at 0x%x\n", 2374 ring->name, 2375 inside ? "inside" : "flushing", 2376 offset, 2377 request->ctx ? request->ctx->id : 0, 2378 acthd); 2379 2380 guilty = true; 2381 } 2382 2383 /* If contexts are disabled or this is the default context, use 2384 * file_priv->reset_state 2385 */ 2386 if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID) 2387 hs = &request->ctx->hang_stats; 2388 else if (request->file_priv) 2389 hs = &request->file_priv->hang_stats; 2390 2391 if (hs) { 2392 if (guilty) { 2393 hs->banned = i915_context_is_banned(hs); 2394 hs->batch_active++; 2395 hs->guilty_ts = get_seconds(); 2396 } else { 2397 hs->batch_pending++; 2398 } 2399 } 2400 } 2401 2402 static void i915_gem_free_request(struct drm_i915_gem_request *request) 2403 { 2404 list_del(&request->list); 2405 i915_gem_request_remove_from_client(request); 2406 2407 if (request->ctx) 2408 i915_gem_context_unreference(request->ctx); 2409 2410 kfree(request); 2411 } 2412 2413 static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv, 2414 struct intel_ring_buffer *ring) 2415 { 2416 u32 completed_seqno = ring->get_seqno(ring, false); 2417 u32 acthd = intel_ring_get_active_head(ring); 2418 struct drm_i915_gem_request *request; 2419 2420 list_for_each_entry(request, &ring->request_list, list) { 2421 if (i915_seqno_passed(completed_seqno, request->seqno)) 2422 continue; 2423 2424 i915_set_reset_status(ring, request, acthd); 2425 } 2426 } 2427 2428 static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv, 2429 struct intel_ring_buffer *ring) 2430 { 2431 while (!list_empty(&ring->active_list)) { 2432 struct drm_i915_gem_object *obj; 2433 2434 obj = list_first_entry(&ring->active_list, 2435 struct drm_i915_gem_object, 2436 ring_list); 2437 2438 i915_gem_object_move_to_inactive(obj); 2439 } 2440 2441 /* 2442 * We must free the requests after all the corresponding objects have 2443 * been moved off active lists. Which is the same order as the normal 2444 * retire_requests function does. This is important if object hold 2445 * implicit references on things like e.g. ppgtt address spaces through 2446 * the request. 2447 */ 2448 while (!list_empty(&ring->request_list)) { 2449 struct drm_i915_gem_request *request; 2450 2451 request = list_first_entry(&ring->request_list, 2452 struct drm_i915_gem_request, 2453 list); 2454 2455 i915_gem_free_request(request); 2456 } 2457 } 2458 2459 void i915_gem_restore_fences(struct drm_device *dev) 2460 { 2461 struct drm_i915_private *dev_priv = dev->dev_private; 2462 int i; 2463 2464 for (i = 0; i < dev_priv->num_fence_regs; i++) { 2465 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; 2466 2467 /* 2468 * Commit delayed tiling changes if we have an object still 2469 * attached to the fence, otherwise just clear the fence. 2470 */ 2471 if (reg->obj) { 2472 i915_gem_object_update_fence(reg->obj, reg, 2473 reg->obj->tiling_mode); 2474 } else { 2475 i915_gem_write_fence(dev, i, NULL); 2476 } 2477 } 2478 } 2479 2480 void i915_gem_reset(struct drm_device *dev) 2481 { 2482 struct drm_i915_private *dev_priv = dev->dev_private; 2483 struct intel_ring_buffer *ring; 2484 int i; 2485 2486 /* 2487 * Before we free the objects from the requests, we need to inspect 2488 * them for finding the guilty party. As the requests only borrow 2489 * their reference to the objects, the inspection must be done first. 2490 */ 2491 for_each_ring(ring, dev_priv, i) 2492 i915_gem_reset_ring_status(dev_priv, ring); 2493 2494 for_each_ring(ring, dev_priv, i) 2495 i915_gem_reset_ring_cleanup(dev_priv, ring); 2496 2497 i915_gem_cleanup_ringbuffer(dev); 2498 2499 i915_gem_restore_fences(dev); 2500 } 2501 2502 /** 2503 * This function clears the request list as sequence numbers are passed. 2504 */ 2505 void 2506 i915_gem_retire_requests_ring(struct intel_ring_buffer *ring) 2507 { 2508 uint32_t seqno; 2509 2510 if (list_empty(&ring->request_list)) 2511 return; 2512 2513 WARN_ON(i915_verify_lists(ring->dev)); 2514 2515 seqno = ring->get_seqno(ring, true); 2516 2517 while (!list_empty(&ring->request_list)) { 2518 struct drm_i915_gem_request *request; 2519 2520 request = list_first_entry(&ring->request_list, 2521 struct drm_i915_gem_request, 2522 list); 2523 2524 if (!i915_seqno_passed(seqno, request->seqno)) 2525 break; 2526 2527 trace_i915_gem_request_retire(ring, request->seqno); 2528 /* We know the GPU must have read the request to have 2529 * sent us the seqno + interrupt, so use the position 2530 * of tail of the request to update the last known position 2531 * of the GPU head. 2532 */ 2533 ring->last_retired_head = request->tail; 2534 2535 i915_gem_free_request(request); 2536 } 2537 2538 /* Move any buffers on the active list that are no longer referenced 2539 * by the ringbuffer to the flushing/inactive lists as appropriate. 2540 */ 2541 while (!list_empty(&ring->active_list)) { 2542 struct drm_i915_gem_object *obj; 2543 2544 obj = list_first_entry(&ring->active_list, 2545 struct drm_i915_gem_object, 2546 ring_list); 2547 2548 if (!i915_seqno_passed(seqno, obj->last_read_seqno)) 2549 break; 2550 2551 i915_gem_object_move_to_inactive(obj); 2552 } 2553 2554 if (unlikely(ring->trace_irq_seqno && 2555 i915_seqno_passed(seqno, ring->trace_irq_seqno))) { 2556 ring->irq_put(ring); 2557 ring->trace_irq_seqno = 0; 2558 } 2559 2560 WARN_ON(i915_verify_lists(ring->dev)); 2561 } 2562 2563 bool 2564 i915_gem_retire_requests(struct drm_device *dev) 2565 { 2566 drm_i915_private_t *dev_priv = dev->dev_private; 2567 struct intel_ring_buffer *ring; 2568 bool idle = true; 2569 int i; 2570 2571 for_each_ring(ring, dev_priv, i) { 2572 i915_gem_retire_requests_ring(ring); 2573 idle &= list_empty(&ring->request_list); 2574 } 2575 2576 return idle; 2577 } 2578 2579 static void 2580 i915_gem_retire_work_handler(struct work_struct *work) 2581 { 2582 drm_i915_private_t *dev_priv; 2583 struct drm_device *dev; 2584 struct intel_ring_buffer *ring; 2585 bool idle; 2586 int i; 2587 2588 dev_priv = container_of(work, drm_i915_private_t, 2589 mm.retire_work.work); 2590 dev = dev_priv->dev; 2591 2592 /* Come back later if the device is busy... */ 2593 if (lockmgr(&dev->struct_mutex, LK_EXCLUSIVE|LK_NOWAIT)) { 2594 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 2595 round_jiffies_up_relative(HZ)); 2596 return; 2597 } 2598 2599 i915_gem_retire_requests(dev); 2600 2601 /* Send a periodic flush down the ring so we don't hold onto GEM 2602 * objects indefinitely. 2603 */ 2604 idle = true; 2605 for_each_ring(ring, dev_priv, i) { 2606 if (ring->gpu_caches_dirty) 2607 i915_add_request(ring, NULL); 2608 2609 idle &= list_empty(&ring->request_list); 2610 } 2611 2612 if (!dev_priv->ums.mm_suspended && !idle) 2613 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 2614 round_jiffies_up_relative(HZ)); 2615 if (idle) 2616 intel_mark_idle(dev); 2617 2618 mutex_unlock(&dev->struct_mutex); 2619 } 2620 2621 static void 2622 i915_gem_idle_work_handler(struct work_struct *work) 2623 { 2624 struct drm_i915_private *dev_priv = 2625 container_of(work, typeof(*dev_priv), mm.idle_work.work); 2626 2627 intel_mark_idle(dev_priv->dev); 2628 } 2629 2630 /** 2631 * Ensures that an object will eventually get non-busy by flushing any required 2632 * write domains, emitting any outstanding lazy request and retiring and 2633 * completed requests. 2634 */ 2635 static int 2636 i915_gem_object_flush_active(struct drm_i915_gem_object *obj) 2637 { 2638 int ret; 2639 2640 if (obj->active) { 2641 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno); 2642 if (ret) 2643 return ret; 2644 2645 i915_gem_retire_requests_ring(obj->ring); 2646 } 2647 2648 return 0; 2649 } 2650 2651 /** 2652 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT 2653 * @DRM_IOCTL_ARGS: standard ioctl arguments 2654 * 2655 * Returns 0 if successful, else an error is returned with the remaining time in 2656 * the timeout parameter. 2657 * -ETIME: object is still busy after timeout 2658 * -ERESTARTSYS: signal interrupted the wait 2659 * -ENONENT: object doesn't exist 2660 * Also possible, but rare: 2661 * -EAGAIN: GPU wedged 2662 * -ENOMEM: damn 2663 * -ENODEV: Internal IRQ fail 2664 * -E?: The add request failed 2665 * 2666 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any 2667 * non-zero timeout parameter the wait ioctl will wait for the given number of 2668 * nanoseconds on an object becoming unbusy. Since the wait itself does so 2669 * without holding struct_mutex the object may become re-busied before this 2670 * function completes. A similar but shorter * race condition exists in the busy 2671 * ioctl 2672 */ 2673 int 2674 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) 2675 { 2676 drm_i915_private_t *dev_priv = dev->dev_private; 2677 struct drm_i915_gem_wait *args = data; 2678 struct drm_i915_gem_object *obj; 2679 struct intel_ring_buffer *ring = NULL; 2680 struct timespec timeout_stack, *timeout = NULL; 2681 unsigned reset_counter; 2682 u32 seqno = 0; 2683 int ret = 0; 2684 2685 if (args->timeout_ns >= 0) { 2686 timeout_stack = ns_to_timespec(args->timeout_ns); 2687 timeout = &timeout_stack; 2688 } 2689 2690 ret = i915_mutex_lock_interruptible(dev); 2691 if (ret) 2692 return ret; 2693 2694 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle)); 2695 if (&obj->base == NULL) { 2696 mutex_unlock(&dev->struct_mutex); 2697 return -ENOENT; 2698 } 2699 2700 /* Need to make sure the object gets inactive eventually. */ 2701 ret = i915_gem_object_flush_active(obj); 2702 if (ret) 2703 goto out; 2704 2705 if (obj->active) { 2706 seqno = obj->last_read_seqno; 2707 ring = obj->ring; 2708 } 2709 2710 if (seqno == 0) 2711 goto out; 2712 2713 /* Do this after OLR check to make sure we make forward progress polling 2714 * on this IOCTL with a 0 timeout (like busy ioctl) 2715 */ 2716 if (!args->timeout_ns) { 2717 ret = -ETIMEDOUT; 2718 goto out; 2719 } 2720 2721 drm_gem_object_unreference(&obj->base); 2722 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); 2723 mutex_unlock(&dev->struct_mutex); 2724 2725 ret = __wait_seqno(ring, seqno, reset_counter, true, timeout); 2726 if (timeout) 2727 args->timeout_ns = timespec_to_ns(timeout); 2728 return ret; 2729 2730 out: 2731 drm_gem_object_unreference(&obj->base); 2732 mutex_unlock(&dev->struct_mutex); 2733 return ret; 2734 } 2735 2736 /** 2737 * i915_gem_object_sync - sync an object to a ring. 2738 * 2739 * @obj: object which may be in use on another ring. 2740 * @to: ring we wish to use the object on. May be NULL. 2741 * 2742 * This code is meant to abstract object synchronization with the GPU. 2743 * Calling with NULL implies synchronizing the object with the CPU 2744 * rather than a particular GPU ring. 2745 * 2746 * Returns 0 if successful, else propagates up the lower layer error. 2747 */ 2748 int 2749 i915_gem_object_sync(struct drm_i915_gem_object *obj, 2750 struct intel_ring_buffer *to) 2751 { 2752 struct intel_ring_buffer *from = obj->ring; 2753 u32 seqno; 2754 int ret, idx; 2755 2756 if (from == NULL || to == from) 2757 return 0; 2758 2759 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev)) 2760 return i915_gem_object_wait_rendering(obj, false); 2761 2762 idx = intel_ring_sync_index(from, to); 2763 2764 seqno = obj->last_read_seqno; 2765 if (seqno <= from->sync_seqno[idx]) 2766 return 0; 2767 2768 ret = i915_gem_check_olr(obj->ring, seqno); 2769 if (ret) 2770 return ret; 2771 2772 trace_i915_gem_ring_sync_to(from, to, seqno); 2773 ret = to->sync_to(to, from, seqno); 2774 if (!ret) 2775 /* We use last_read_seqno because sync_to() 2776 * might have just caused seqno wrap under 2777 * the radar. 2778 */ 2779 from->sync_seqno[idx] = obj->last_read_seqno; 2780 2781 return ret; 2782 } 2783 2784 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj) 2785 { 2786 u32 old_write_domain, old_read_domains; 2787 2788 /* Force a pagefault for domain tracking on next user access */ 2789 i915_gem_release_mmap(obj); 2790 2791 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) 2792 return; 2793 2794 /* Wait for any direct GTT access to complete */ 2795 mb(); 2796 2797 old_read_domains = obj->base.read_domains; 2798 old_write_domain = obj->base.write_domain; 2799 2800 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT; 2801 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT; 2802 2803 trace_i915_gem_object_change_domain(obj, 2804 old_read_domains, 2805 old_write_domain); 2806 } 2807 2808 int i915_vma_unbind(struct i915_vma *vma) 2809 { 2810 struct drm_i915_gem_object *obj = vma->obj; 2811 drm_i915_private_t *dev_priv = obj->base.dev->dev_private; 2812 int ret; 2813 2814 /* For now we only ever use 1 vma per object */ 2815 #if 0 2816 WARN_ON(!list_is_singular(&obj->vma_list)); 2817 #endif 2818 2819 if (list_empty(&vma->vma_link)) 2820 return 0; 2821 2822 if (!drm_mm_node_allocated(&vma->node)) { 2823 i915_gem_vma_destroy(vma); 2824 2825 return 0; 2826 } 2827 2828 if (obj->pin_count) 2829 return -EBUSY; 2830 2831 BUG_ON(obj->pages == NULL); 2832 2833 ret = i915_gem_object_finish_gpu(obj); 2834 if (ret) 2835 return ret; 2836 /* Continue on if we fail due to EIO, the GPU is hung so we 2837 * should be safe and we need to cleanup or else we might 2838 * cause memory corruption through use-after-free. 2839 */ 2840 2841 i915_gem_object_finish_gtt(obj); 2842 2843 /* release the fence reg _after_ flushing */ 2844 ret = i915_gem_object_put_fence(obj); 2845 if (ret) 2846 return ret; 2847 2848 trace_i915_vma_unbind(vma); 2849 2850 if (obj->has_global_gtt_mapping) 2851 i915_gem_gtt_unbind_object(obj); 2852 if (obj->has_aliasing_ppgtt_mapping) { 2853 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj); 2854 obj->has_aliasing_ppgtt_mapping = 0; 2855 } 2856 i915_gem_gtt_finish_object(obj); 2857 2858 list_del(&vma->mm_list); 2859 /* Avoid an unnecessary call to unbind on rebind. */ 2860 if (i915_is_ggtt(vma->vm)) 2861 obj->map_and_fenceable = true; 2862 2863 drm_mm_remove_node(&vma->node); 2864 i915_gem_vma_destroy(vma); 2865 2866 /* Since the unbound list is global, only move to that list if 2867 * no more VMAs exist. */ 2868 if (list_empty(&obj->vma_list)) 2869 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list); 2870 2871 /* And finally now the object is completely decoupled from this vma, 2872 * we can drop its hold on the backing storage and allow it to be 2873 * reaped by the shrinker. 2874 */ 2875 i915_gem_object_unpin_pages(obj); 2876 2877 return 0; 2878 } 2879 2880 /** 2881 * Unbinds an object from the global GTT aperture. 2882 */ 2883 int 2884 i915_gem_object_ggtt_unbind(struct drm_i915_gem_object *obj) 2885 { 2886 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 2887 struct i915_address_space *ggtt = &dev_priv->gtt.base; 2888 2889 if (!i915_gem_obj_ggtt_bound(obj)) 2890 return 0; 2891 2892 if (obj->pin_count) 2893 return -EBUSY; 2894 2895 BUG_ON(obj->pages == NULL); 2896 2897 return i915_vma_unbind(i915_gem_obj_to_vma(obj, ggtt)); 2898 } 2899 2900 int i915_gpu_idle(struct drm_device *dev) 2901 { 2902 drm_i915_private_t *dev_priv = dev->dev_private; 2903 struct intel_ring_buffer *ring; 2904 int ret, i; 2905 2906 /* Flush everything onto the inactive list. */ 2907 for_each_ring(ring, dev_priv, i) { 2908 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID); 2909 if (ret) 2910 return ret; 2911 2912 ret = intel_ring_idle(ring); 2913 if (ret) 2914 return ret; 2915 } 2916 2917 return 0; 2918 } 2919 2920 static void i965_write_fence_reg(struct drm_device *dev, int reg, 2921 struct drm_i915_gem_object *obj) 2922 { 2923 drm_i915_private_t *dev_priv = dev->dev_private; 2924 int fence_reg; 2925 int fence_pitch_shift; 2926 2927 if (INTEL_INFO(dev)->gen >= 6) { 2928 fence_reg = FENCE_REG_SANDYBRIDGE_0; 2929 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT; 2930 } else { 2931 fence_reg = FENCE_REG_965_0; 2932 fence_pitch_shift = I965_FENCE_PITCH_SHIFT; 2933 } 2934 2935 fence_reg += reg * 8; 2936 2937 /* To w/a incoherency with non-atomic 64-bit register updates, 2938 * we split the 64-bit update into two 32-bit writes. In order 2939 * for a partial fence not to be evaluated between writes, we 2940 * precede the update with write to turn off the fence register, 2941 * and only enable the fence as the last step. 2942 * 2943 * For extra levels of paranoia, we make sure each step lands 2944 * before applying the next step. 2945 */ 2946 I915_WRITE(fence_reg, 0); 2947 POSTING_READ(fence_reg); 2948 2949 if (obj) { 2950 u32 size = i915_gem_obj_ggtt_size(obj); 2951 uint64_t val; 2952 2953 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) & 2954 0xfffff000) << 32; 2955 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000; 2956 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift; 2957 if (obj->tiling_mode == I915_TILING_Y) 2958 val |= 1 << I965_FENCE_TILING_Y_SHIFT; 2959 val |= I965_FENCE_REG_VALID; 2960 2961 I915_WRITE(fence_reg + 4, val >> 32); 2962 POSTING_READ(fence_reg + 4); 2963 2964 I915_WRITE(fence_reg + 0, val); 2965 POSTING_READ(fence_reg); 2966 } else { 2967 I915_WRITE(fence_reg + 4, 0); 2968 POSTING_READ(fence_reg + 4); 2969 } 2970 } 2971 2972 static void i915_write_fence_reg(struct drm_device *dev, int reg, 2973 struct drm_i915_gem_object *obj) 2974 { 2975 drm_i915_private_t *dev_priv = dev->dev_private; 2976 u32 val; 2977 2978 if (obj) { 2979 u32 size = i915_gem_obj_ggtt_size(obj); 2980 int pitch_val; 2981 int tile_width; 2982 2983 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) || 2984 (size & -size) != size || 2985 (i915_gem_obj_ggtt_offset(obj) & (size - 1)), 2986 "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n", 2987 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size); 2988 2989 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)) 2990 tile_width = 128; 2991 else 2992 tile_width = 512; 2993 2994 /* Note: pitch better be a power of two tile widths */ 2995 pitch_val = obj->stride / tile_width; 2996 pitch_val = ffs(pitch_val) - 1; 2997 2998 val = i915_gem_obj_ggtt_offset(obj); 2999 if (obj->tiling_mode == I915_TILING_Y) 3000 val |= 1 << I830_FENCE_TILING_Y_SHIFT; 3001 val |= I915_FENCE_SIZE_BITS(size); 3002 val |= pitch_val << I830_FENCE_PITCH_SHIFT; 3003 val |= I830_FENCE_REG_VALID; 3004 } else 3005 val = 0; 3006 3007 if (reg < 8) 3008 reg = FENCE_REG_830_0 + reg * 4; 3009 else 3010 reg = FENCE_REG_945_8 + (reg - 8) * 4; 3011 3012 I915_WRITE(reg, val); 3013 POSTING_READ(reg); 3014 } 3015 3016 static void i830_write_fence_reg(struct drm_device *dev, int reg, 3017 struct drm_i915_gem_object *obj) 3018 { 3019 drm_i915_private_t *dev_priv = dev->dev_private; 3020 uint32_t val; 3021 3022 if (obj) { 3023 u32 size = i915_gem_obj_ggtt_size(obj); 3024 uint32_t pitch_val; 3025 3026 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) || 3027 (size & -size) != size || 3028 (i915_gem_obj_ggtt_offset(obj) & (size - 1)), 3029 "object 0x%08lx not 512K or pot-size 0x%08x aligned\n", 3030 i915_gem_obj_ggtt_offset(obj), size); 3031 3032 pitch_val = obj->stride / 128; 3033 pitch_val = ffs(pitch_val) - 1; 3034 3035 val = i915_gem_obj_ggtt_offset(obj); 3036 if (obj->tiling_mode == I915_TILING_Y) 3037 val |= 1 << I830_FENCE_TILING_Y_SHIFT; 3038 val |= I830_FENCE_SIZE_BITS(size); 3039 val |= pitch_val << I830_FENCE_PITCH_SHIFT; 3040 val |= I830_FENCE_REG_VALID; 3041 } else 3042 val = 0; 3043 3044 I915_WRITE(FENCE_REG_830_0 + reg * 4, val); 3045 POSTING_READ(FENCE_REG_830_0 + reg * 4); 3046 } 3047 3048 inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj) 3049 { 3050 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT; 3051 } 3052 3053 static void i915_gem_write_fence(struct drm_device *dev, int reg, 3054 struct drm_i915_gem_object *obj) 3055 { 3056 struct drm_i915_private *dev_priv = dev->dev_private; 3057 3058 /* Ensure that all CPU reads are completed before installing a fence 3059 * and all writes before removing the fence. 3060 */ 3061 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj)) 3062 mb(); 3063 3064 WARN(obj && (!obj->stride || !obj->tiling_mode), 3065 "bogus fence setup with stride: 0x%x, tiling mode: %i\n", 3066 obj->stride, obj->tiling_mode); 3067 3068 switch (INTEL_INFO(dev)->gen) { 3069 case 8: 3070 case 7: 3071 case 6: 3072 case 5: 3073 case 4: i965_write_fence_reg(dev, reg, obj); break; 3074 case 3: i915_write_fence_reg(dev, reg, obj); break; 3075 case 2: i830_write_fence_reg(dev, reg, obj); break; 3076 default: BUG(); 3077 } 3078 3079 /* And similarly be paranoid that no direct access to this region 3080 * is reordered to before the fence is installed. 3081 */ 3082 if (i915_gem_object_needs_mb(obj)) 3083 mb(); 3084 } 3085 3086 static inline int fence_number(struct drm_i915_private *dev_priv, 3087 struct drm_i915_fence_reg *fence) 3088 { 3089 return fence - dev_priv->fence_regs; 3090 } 3091 3092 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, 3093 struct drm_i915_fence_reg *fence, 3094 bool enable) 3095 { 3096 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 3097 int reg = fence_number(dev_priv, fence); 3098 3099 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL); 3100 3101 if (enable) { 3102 obj->fence_reg = reg; 3103 fence->obj = obj; 3104 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list); 3105 } else { 3106 obj->fence_reg = I915_FENCE_REG_NONE; 3107 fence->obj = NULL; 3108 list_del_init(&fence->lru_list); 3109 } 3110 obj->fence_dirty = false; 3111 } 3112 3113 static int 3114 i915_gem_object_wait_fence(struct drm_i915_gem_object *obj) 3115 { 3116 if (obj->last_fenced_seqno) { 3117 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno); 3118 if (ret) 3119 return ret; 3120 3121 obj->last_fenced_seqno = 0; 3122 } 3123 3124 obj->fenced_gpu_access = false; 3125 return 0; 3126 } 3127 3128 int 3129 i915_gem_object_put_fence(struct drm_i915_gem_object *obj) 3130 { 3131 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 3132 struct drm_i915_fence_reg *fence; 3133 int ret; 3134 3135 ret = i915_gem_object_wait_fence(obj); 3136 if (ret) 3137 return ret; 3138 3139 if (obj->fence_reg == I915_FENCE_REG_NONE) 3140 return 0; 3141 3142 fence = &dev_priv->fence_regs[obj->fence_reg]; 3143 3144 i915_gem_object_fence_lost(obj); 3145 i915_gem_object_update_fence(obj, fence, false); 3146 3147 return 0; 3148 } 3149 3150 static struct drm_i915_fence_reg * 3151 i915_find_fence_reg(struct drm_device *dev) 3152 { 3153 struct drm_i915_private *dev_priv = dev->dev_private; 3154 struct drm_i915_fence_reg *reg, *avail; 3155 int i; 3156 3157 /* First try to find a free reg */ 3158 avail = NULL; 3159 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { 3160 reg = &dev_priv->fence_regs[i]; 3161 if (!reg->obj) 3162 return reg; 3163 3164 if (!reg->pin_count) 3165 avail = reg; 3166 } 3167 3168 if (avail == NULL) 3169 goto deadlock; 3170 3171 /* None available, try to steal one or wait for a user to finish */ 3172 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) { 3173 if (reg->pin_count) 3174 continue; 3175 3176 return reg; 3177 } 3178 3179 deadlock: 3180 /* Wait for completion of pending flips which consume fences */ 3181 if (intel_has_pending_fb_unpin(dev)) 3182 return ERR_PTR(-EAGAIN); 3183 3184 return ERR_PTR(-EDEADLK); 3185 } 3186 3187 /** 3188 * i915_gem_object_get_fence - set up fencing for an object 3189 * @obj: object to map through a fence reg 3190 * 3191 * When mapping objects through the GTT, userspace wants to be able to write 3192 * to them without having to worry about swizzling if the object is tiled. 3193 * This function walks the fence regs looking for a free one for @obj, 3194 * stealing one if it can't find any. 3195 * 3196 * It then sets up the reg based on the object's properties: address, pitch 3197 * and tiling format. 3198 * 3199 * For an untiled surface, this removes any existing fence. 3200 */ 3201 int 3202 i915_gem_object_get_fence(struct drm_i915_gem_object *obj) 3203 { 3204 struct drm_device *dev = obj->base.dev; 3205 struct drm_i915_private *dev_priv = dev->dev_private; 3206 bool enable = obj->tiling_mode != I915_TILING_NONE; 3207 struct drm_i915_fence_reg *reg; 3208 int ret; 3209 3210 /* Have we updated the tiling parameters upon the object and so 3211 * will need to serialise the write to the associated fence register? 3212 */ 3213 if (obj->fence_dirty) { 3214 ret = i915_gem_object_wait_fence(obj); 3215 if (ret) 3216 return ret; 3217 } 3218 3219 /* Just update our place in the LRU if our fence is getting reused. */ 3220 if (obj->fence_reg != I915_FENCE_REG_NONE) { 3221 reg = &dev_priv->fence_regs[obj->fence_reg]; 3222 if (!obj->fence_dirty) { 3223 list_move_tail(®->lru_list, 3224 &dev_priv->mm.fence_list); 3225 return 0; 3226 } 3227 } else if (enable) { 3228 reg = i915_find_fence_reg(dev); 3229 if (IS_ERR(reg)) 3230 return PTR_ERR(reg); 3231 3232 if (reg->obj) { 3233 struct drm_i915_gem_object *old = reg->obj; 3234 3235 ret = i915_gem_object_wait_fence(old); 3236 if (ret) 3237 return ret; 3238 3239 i915_gem_object_fence_lost(old); 3240 } 3241 } else 3242 return 0; 3243 3244 i915_gem_object_update_fence(obj, reg, enable); 3245 3246 return 0; 3247 } 3248 3249 static bool i915_gem_valid_gtt_space(struct drm_device *dev, 3250 struct drm_mm_node *gtt_space, 3251 unsigned long cache_level) 3252 { 3253 struct drm_mm_node *other; 3254 3255 /* On non-LLC machines we have to be careful when putting differing 3256 * types of snoopable memory together to avoid the prefetcher 3257 * crossing memory domains and dying. 3258 */ 3259 if (HAS_LLC(dev)) 3260 return true; 3261 3262 if (!drm_mm_node_allocated(gtt_space)) 3263 return true; 3264 3265 if (list_empty(>t_space->node_list)) 3266 return true; 3267 3268 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list); 3269 if (other->allocated && !other->hole_follows && other->color != cache_level) 3270 return false; 3271 3272 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list); 3273 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level) 3274 return false; 3275 3276 return true; 3277 } 3278 3279 static void i915_gem_verify_gtt(struct drm_device *dev) 3280 { 3281 #if WATCH_GTT 3282 struct drm_i915_private *dev_priv = dev->dev_private; 3283 struct drm_i915_gem_object *obj; 3284 int err = 0; 3285 3286 list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) { 3287 if (obj->gtt_space == NULL) { 3288 printk(KERN_ERR "object found on GTT list with no space reserved\n"); 3289 err++; 3290 continue; 3291 } 3292 3293 if (obj->cache_level != obj->gtt_space->color) { 3294 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n", 3295 i915_gem_obj_ggtt_offset(obj), 3296 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj), 3297 obj->cache_level, 3298 obj->gtt_space->color); 3299 err++; 3300 continue; 3301 } 3302 3303 if (!i915_gem_valid_gtt_space(dev, 3304 obj->gtt_space, 3305 obj->cache_level)) { 3306 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n", 3307 i915_gem_obj_ggtt_offset(obj), 3308 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj), 3309 obj->cache_level); 3310 err++; 3311 continue; 3312 } 3313 } 3314 3315 WARN_ON(err); 3316 #endif 3317 } 3318 3319 /** 3320 * Finds free space in the GTT aperture and binds the object there. 3321 */ 3322 static int 3323 i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj, 3324 struct i915_address_space *vm, 3325 unsigned alignment, 3326 bool map_and_fenceable, 3327 bool nonblocking) 3328 { 3329 struct drm_device *dev = obj->base.dev; 3330 drm_i915_private_t *dev_priv = dev->dev_private; 3331 u32 size, fence_size, fence_alignment, unfenced_alignment; 3332 size_t gtt_max = 3333 map_and_fenceable ? dev_priv->gtt.mappable_end : vm->total; 3334 struct i915_vma *vma; 3335 int ret; 3336 3337 fence_size = i915_gem_get_gtt_size(dev, 3338 obj->base.size, 3339 obj->tiling_mode); 3340 fence_alignment = i915_gem_get_gtt_alignment(dev, 3341 obj->base.size, 3342 obj->tiling_mode, true); 3343 unfenced_alignment = 3344 i915_gem_get_gtt_alignment(dev, 3345 obj->base.size, 3346 obj->tiling_mode, false); 3347 3348 if (alignment == 0) 3349 alignment = map_and_fenceable ? fence_alignment : 3350 unfenced_alignment; 3351 if (map_and_fenceable && alignment & (fence_alignment - 1)) { 3352 DRM_ERROR("Invalid object alignment requested %u\n", alignment); 3353 return -EINVAL; 3354 } 3355 3356 size = map_and_fenceable ? fence_size : obj->base.size; 3357 3358 /* If the object is bigger than the entire aperture, reject it early 3359 * before evicting everything in a vain attempt to find space. 3360 */ 3361 if (obj->base.size > gtt_max) { 3362 DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n", 3363 obj->base.size, 3364 map_and_fenceable ? "mappable" : "total", 3365 gtt_max); 3366 return -E2BIG; 3367 } 3368 3369 ret = i915_gem_object_get_pages(obj); 3370 if (ret) 3371 return ret; 3372 3373 i915_gem_object_pin_pages(obj); 3374 3375 BUG_ON(!i915_is_ggtt(vm)); 3376 3377 vma = i915_gem_obj_lookup_or_create_vma(obj, vm); 3378 if (IS_ERR(vma)) { 3379 ret = PTR_ERR(vma); 3380 goto err_unpin; 3381 } 3382 3383 /* For now we only ever use 1 vma per object */ 3384 #if 0 3385 WARN_ON(!list_is_singular(&obj->vma_list)); 3386 #endif 3387 3388 search_free: 3389 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node, 3390 size, alignment, 3391 obj->cache_level, 0, gtt_max, 3392 DRM_MM_SEARCH_DEFAULT); 3393 if (ret) { 3394 ret = i915_gem_evict_something(dev, vm, size, alignment, 3395 obj->cache_level, 3396 map_and_fenceable, 3397 nonblocking); 3398 if (ret == 0) 3399 goto search_free; 3400 3401 goto err_free_vma; 3402 } 3403 if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node, 3404 obj->cache_level))) { 3405 ret = -EINVAL; 3406 goto err_remove_node; 3407 } 3408 3409 ret = i915_gem_gtt_prepare_object(obj); 3410 if (ret) 3411 goto err_remove_node; 3412 3413 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list); 3414 list_add_tail(&vma->mm_list, &vm->inactive_list); 3415 3416 if (i915_is_ggtt(vm)) { 3417 bool mappable, fenceable; 3418 3419 fenceable = (vma->node.size == fence_size && 3420 (vma->node.start & (fence_alignment - 1)) == 0); 3421 3422 mappable = (vma->node.start + obj->base.size <= 3423 dev_priv->gtt.mappable_end); 3424 3425 obj->map_and_fenceable = mappable && fenceable; 3426 } 3427 3428 WARN_ON(map_and_fenceable && !obj->map_and_fenceable); 3429 3430 trace_i915_vma_bind(vma, map_and_fenceable); 3431 i915_gem_verify_gtt(dev); 3432 return 0; 3433 3434 err_remove_node: 3435 drm_mm_remove_node(&vma->node); 3436 err_free_vma: 3437 i915_gem_vma_destroy(vma); 3438 err_unpin: 3439 i915_gem_object_unpin_pages(obj); 3440 return ret; 3441 } 3442 3443 bool 3444 i915_gem_clflush_object(struct drm_i915_gem_object *obj, 3445 bool force) 3446 { 3447 /* If we don't have a page list set up, then we're not pinned 3448 * to GPU, and we can ignore the cache flush because it'll happen 3449 * again at bind time. 3450 */ 3451 if (obj->pages == NULL) 3452 return false; 3453 3454 /* 3455 * Stolen memory is always coherent with the GPU as it is explicitly 3456 * marked as wc by the system, or the system is cache-coherent. 3457 */ 3458 if (obj->stolen) 3459 return false; 3460 3461 /* If the GPU is snooping the contents of the CPU cache, 3462 * we do not need to manually clear the CPU cache lines. However, 3463 * the caches are only snooped when the render cache is 3464 * flushed/invalidated. As we always have to emit invalidations 3465 * and flushes when moving into and out of the RENDER domain, correct 3466 * snooping behaviour occurs naturally as the result of our domain 3467 * tracking. 3468 */ 3469 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) 3470 return false; 3471 3472 trace_i915_gem_object_clflush(obj); 3473 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE); 3474 3475 return true; 3476 } 3477 3478 /** Flushes the GTT write domain for the object if it's dirty. */ 3479 static void 3480 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj) 3481 { 3482 uint32_t old_write_domain; 3483 3484 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT) 3485 return; 3486 3487 /* No actual flushing is required for the GTT write domain. Writes 3488 * to it immediately go to main memory as far as we know, so there's 3489 * no chipset flush. It also doesn't land in render cache. 3490 * 3491 * However, we do have to enforce the order so that all writes through 3492 * the GTT land before any writes to the device, such as updates to 3493 * the GATT itself. 3494 */ 3495 wmb(); 3496 3497 old_write_domain = obj->base.write_domain; 3498 obj->base.write_domain = 0; 3499 3500 trace_i915_gem_object_change_domain(obj, 3501 obj->base.read_domains, 3502 old_write_domain); 3503 } 3504 3505 /** Flushes the CPU write domain for the object if it's dirty. */ 3506 static void 3507 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj, 3508 bool force) 3509 { 3510 uint32_t old_write_domain; 3511 3512 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) 3513 return; 3514 3515 if (i915_gem_clflush_object(obj, force)) 3516 i915_gem_chipset_flush(obj->base.dev); 3517 3518 old_write_domain = obj->base.write_domain; 3519 obj->base.write_domain = 0; 3520 3521 trace_i915_gem_object_change_domain(obj, 3522 obj->base.read_domains, 3523 old_write_domain); 3524 } 3525 3526 /** 3527 * Moves a single object to the GTT read, and possibly write domain. 3528 * 3529 * This function returns when the move is complete, including waiting on 3530 * flushes to occur. 3531 */ 3532 int 3533 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) 3534 { 3535 drm_i915_private_t *dev_priv = obj->base.dev->dev_private; 3536 uint32_t old_write_domain, old_read_domains; 3537 int ret; 3538 3539 /* Not valid to be called on unbound objects. */ 3540 if (!i915_gem_obj_bound_any(obj)) 3541 return -EINVAL; 3542 3543 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) 3544 return 0; 3545 3546 ret = i915_gem_object_wait_rendering(obj, !write); 3547 if (ret) 3548 return ret; 3549 3550 i915_gem_object_flush_cpu_write_domain(obj, false); 3551 3552 /* Serialise direct access to this object with the barriers for 3553 * coherent writes from the GPU, by effectively invalidating the 3554 * GTT domain upon first access. 3555 */ 3556 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) 3557 mb(); 3558 3559 old_write_domain = obj->base.write_domain; 3560 old_read_domains = obj->base.read_domains; 3561 3562 /* It should now be out of any other write domains, and we can update 3563 * the domain values for our changes. 3564 */ 3565 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); 3566 obj->base.read_domains |= I915_GEM_DOMAIN_GTT; 3567 if (write) { 3568 obj->base.read_domains = I915_GEM_DOMAIN_GTT; 3569 obj->base.write_domain = I915_GEM_DOMAIN_GTT; 3570 obj->dirty = 1; 3571 } 3572 3573 trace_i915_gem_object_change_domain(obj, 3574 old_read_domains, 3575 old_write_domain); 3576 3577 /* And bump the LRU for this access */ 3578 if (i915_gem_object_is_inactive(obj)) { 3579 struct i915_vma *vma = i915_gem_obj_to_ggtt(obj); 3580 if (vma) 3581 list_move_tail(&vma->mm_list, 3582 &dev_priv->gtt.base.inactive_list); 3583 3584 } 3585 3586 return 0; 3587 } 3588 3589 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, 3590 enum i915_cache_level cache_level) 3591 { 3592 struct drm_device *dev = obj->base.dev; 3593 drm_i915_private_t *dev_priv = dev->dev_private; 3594 struct i915_vma *vma; 3595 int ret; 3596 3597 if (obj->cache_level == cache_level) 3598 return 0; 3599 3600 if (obj->pin_count) { 3601 DRM_DEBUG("can not change the cache level of pinned objects\n"); 3602 return -EBUSY; 3603 } 3604 3605 list_for_each_entry(vma, &obj->vma_list, vma_link) { 3606 if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) { 3607 ret = i915_vma_unbind(vma); 3608 if (ret) 3609 return ret; 3610 3611 break; 3612 } 3613 } 3614 3615 if (i915_gem_obj_bound_any(obj)) { 3616 ret = i915_gem_object_finish_gpu(obj); 3617 if (ret) 3618 return ret; 3619 3620 i915_gem_object_finish_gtt(obj); 3621 3622 /* Before SandyBridge, you could not use tiling or fence 3623 * registers with snooped memory, so relinquish any fences 3624 * currently pointing to our region in the aperture. 3625 */ 3626 if (INTEL_INFO(dev)->gen < 6) { 3627 ret = i915_gem_object_put_fence(obj); 3628 if (ret) 3629 return ret; 3630 } 3631 3632 if (obj->has_global_gtt_mapping) 3633 i915_gem_gtt_bind_object(obj, cache_level); 3634 if (obj->has_aliasing_ppgtt_mapping) 3635 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt, 3636 obj, cache_level); 3637 } 3638 3639 list_for_each_entry(vma, &obj->vma_list, vma_link) 3640 vma->node.color = cache_level; 3641 obj->cache_level = cache_level; 3642 3643 if (cpu_write_needs_clflush(obj)) { 3644 u32 old_read_domains, old_write_domain; 3645 3646 /* If we're coming from LLC cached, then we haven't 3647 * actually been tracking whether the data is in the 3648 * CPU cache or not, since we only allow one bit set 3649 * in obj->write_domain and have been skipping the clflushes. 3650 * Just set it to the CPU cache for now. 3651 */ 3652 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU); 3653 3654 old_read_domains = obj->base.read_domains; 3655 old_write_domain = obj->base.write_domain; 3656 3657 obj->base.read_domains = I915_GEM_DOMAIN_CPU; 3658 obj->base.write_domain = I915_GEM_DOMAIN_CPU; 3659 3660 trace_i915_gem_object_change_domain(obj, 3661 old_read_domains, 3662 old_write_domain); 3663 } 3664 3665 i915_gem_verify_gtt(dev); 3666 return 0; 3667 } 3668 3669 int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, 3670 struct drm_file *file) 3671 { 3672 struct drm_i915_gem_caching *args = data; 3673 struct drm_i915_gem_object *obj; 3674 int ret; 3675 3676 ret = i915_mutex_lock_interruptible(dev); 3677 if (ret) 3678 return ret; 3679 3680 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 3681 if (&obj->base == NULL) { 3682 ret = -ENOENT; 3683 goto unlock; 3684 } 3685 3686 switch (obj->cache_level) { 3687 case I915_CACHE_LLC: 3688 case I915_CACHE_L3_LLC: 3689 args->caching = I915_CACHING_CACHED; 3690 break; 3691 3692 case I915_CACHE_WT: 3693 args->caching = I915_CACHING_DISPLAY; 3694 break; 3695 3696 default: 3697 args->caching = I915_CACHING_NONE; 3698 break; 3699 } 3700 3701 drm_gem_object_unreference(&obj->base); 3702 unlock: 3703 mutex_unlock(&dev->struct_mutex); 3704 return ret; 3705 } 3706 3707 int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, 3708 struct drm_file *file) 3709 { 3710 struct drm_i915_gem_caching *args = data; 3711 struct drm_i915_gem_object *obj; 3712 enum i915_cache_level level; 3713 int ret; 3714 3715 switch (args->caching) { 3716 case I915_CACHING_NONE: 3717 level = I915_CACHE_NONE; 3718 break; 3719 case I915_CACHING_CACHED: 3720 level = I915_CACHE_LLC; 3721 break; 3722 case I915_CACHING_DISPLAY: 3723 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE; 3724 break; 3725 default: 3726 return -EINVAL; 3727 } 3728 3729 ret = i915_mutex_lock_interruptible(dev); 3730 if (ret) 3731 return ret; 3732 3733 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 3734 if (&obj->base == NULL) { 3735 ret = -ENOENT; 3736 goto unlock; 3737 } 3738 3739 ret = i915_gem_object_set_cache_level(obj, level); 3740 3741 drm_gem_object_unreference(&obj->base); 3742 unlock: 3743 mutex_unlock(&dev->struct_mutex); 3744 return ret; 3745 } 3746 3747 static bool is_pin_display(struct drm_i915_gem_object *obj) 3748 { 3749 /* There are 3 sources that pin objects: 3750 * 1. The display engine (scanouts, sprites, cursors); 3751 * 2. Reservations for execbuffer; 3752 * 3. The user. 3753 * 3754 * We can ignore reservations as we hold the struct_mutex and 3755 * are only called outside of the reservation path. The user 3756 * can only increment pin_count once, and so if after 3757 * subtracting the potential reference by the user, any pin_count 3758 * remains, it must be due to another use by the display engine. 3759 */ 3760 return obj->pin_count - !!obj->user_pin_count; 3761 } 3762 3763 /* 3764 * Prepare buffer for display plane (scanout, cursors, etc). 3765 * Can be called from an uninterruptible phase (modesetting) and allows 3766 * any flushes to be pipelined (for pageflips). 3767 */ 3768 int 3769 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, 3770 u32 alignment, 3771 struct intel_ring_buffer *pipelined) 3772 { 3773 u32 old_read_domains, old_write_domain; 3774 int ret; 3775 3776 if (pipelined != obj->ring) { 3777 ret = i915_gem_object_sync(obj, pipelined); 3778 if (ret) 3779 return ret; 3780 } 3781 3782 /* Mark the pin_display early so that we account for the 3783 * display coherency whilst setting up the cache domains. 3784 */ 3785 obj->pin_display = true; 3786 3787 /* The display engine is not coherent with the LLC cache on gen6. As 3788 * a result, we make sure that the pinning that is about to occur is 3789 * done with uncached PTEs. This is lowest common denominator for all 3790 * chipsets. 3791 * 3792 * However for gen6+, we could do better by using the GFDT bit instead 3793 * of uncaching, which would allow us to flush all the LLC-cached data 3794 * with that bit in the PTE to main memory with just one PIPE_CONTROL. 3795 */ 3796 ret = i915_gem_object_set_cache_level(obj, 3797 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE); 3798 if (ret) 3799 goto err_unpin_display; 3800 3801 /* As the user may map the buffer once pinned in the display plane 3802 * (e.g. libkms for the bootup splash), we have to ensure that we 3803 * always use map_and_fenceable for all scanout buffers. 3804 */ 3805 ret = i915_gem_obj_ggtt_pin(obj, alignment, true, false); 3806 if (ret) 3807 goto err_unpin_display; 3808 3809 i915_gem_object_flush_cpu_write_domain(obj, true); 3810 3811 old_write_domain = obj->base.write_domain; 3812 old_read_domains = obj->base.read_domains; 3813 3814 /* It should now be out of any other write domains, and we can update 3815 * the domain values for our changes. 3816 */ 3817 obj->base.write_domain = 0; 3818 obj->base.read_domains |= I915_GEM_DOMAIN_GTT; 3819 3820 trace_i915_gem_object_change_domain(obj, 3821 old_read_domains, 3822 old_write_domain); 3823 3824 return 0; 3825 3826 err_unpin_display: 3827 obj->pin_display = is_pin_display(obj); 3828 return ret; 3829 } 3830 3831 void 3832 i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj) 3833 { 3834 i915_gem_object_unpin(obj); 3835 obj->pin_display = is_pin_display(obj); 3836 } 3837 3838 int 3839 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj) 3840 { 3841 int ret; 3842 3843 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0) 3844 return 0; 3845 3846 ret = i915_gem_object_wait_rendering(obj, false); 3847 if (ret) 3848 return ret; 3849 3850 /* Ensure that we invalidate the GPU's caches and TLBs. */ 3851 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS; 3852 return 0; 3853 } 3854 3855 /** 3856 * Moves a single object to the CPU read, and possibly write domain. 3857 * 3858 * This function returns when the move is complete, including waiting on 3859 * flushes to occur. 3860 */ 3861 int 3862 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) 3863 { 3864 uint32_t old_write_domain, old_read_domains; 3865 int ret; 3866 3867 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) 3868 return 0; 3869 3870 ret = i915_gem_object_wait_rendering(obj, !write); 3871 if (ret) 3872 return ret; 3873 3874 i915_gem_object_flush_gtt_write_domain(obj); 3875 3876 old_write_domain = obj->base.write_domain; 3877 old_read_domains = obj->base.read_domains; 3878 3879 /* Flush the CPU cache if it's still invalid. */ 3880 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { 3881 i915_gem_clflush_object(obj, false); 3882 3883 obj->base.read_domains |= I915_GEM_DOMAIN_CPU; 3884 } 3885 3886 /* It should now be out of any other write domains, and we can update 3887 * the domain values for our changes. 3888 */ 3889 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); 3890 3891 /* If we're writing through the CPU, then the GPU read domains will 3892 * need to be invalidated at next use. 3893 */ 3894 if (write) { 3895 obj->base.read_domains = I915_GEM_DOMAIN_CPU; 3896 obj->base.write_domain = I915_GEM_DOMAIN_CPU; 3897 } 3898 3899 trace_i915_gem_object_change_domain(obj, 3900 old_read_domains, 3901 old_write_domain); 3902 3903 return 0; 3904 } 3905 3906 /* Throttle our rendering by waiting until the ring has completed our requests 3907 * emitted over 20 msec ago. 3908 * 3909 * Note that if we were to use the current jiffies each time around the loop, 3910 * we wouldn't escape the function with any frames outstanding if the time to 3911 * render a frame was over 20ms. 3912 * 3913 * This should get us reasonable parallelism between CPU and GPU but also 3914 * relatively low latency when blocking on a particular request to finish. 3915 */ 3916 static int 3917 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) 3918 { 3919 struct drm_i915_private *dev_priv = dev->dev_private; 3920 struct drm_i915_file_private *file_priv = file->driver_priv; 3921 unsigned long recent_enough = jiffies - msecs_to_jiffies(20); 3922 struct drm_i915_gem_request *request; 3923 struct intel_ring_buffer *ring = NULL; 3924 unsigned reset_counter; 3925 u32 seqno = 0; 3926 int ret; 3927 3928 ret = i915_gem_wait_for_error(&dev_priv->gpu_error); 3929 if (ret) 3930 return ret; 3931 3932 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false); 3933 if (ret) 3934 return ret; 3935 3936 spin_lock(&file_priv->mm.lock); 3937 list_for_each_entry(request, &file_priv->mm.request_list, client_list) { 3938 if (time_after_eq(request->emitted_jiffies, recent_enough)) 3939 break; 3940 3941 ring = request->ring; 3942 seqno = request->seqno; 3943 } 3944 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); 3945 spin_unlock(&file_priv->mm.lock); 3946 3947 if (seqno == 0) 3948 return 0; 3949 3950 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL); 3951 if (ret == 0) 3952 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0); 3953 3954 return ret; 3955 } 3956 3957 int 3958 i915_gem_object_pin(struct drm_i915_gem_object *obj, 3959 struct i915_address_space *vm, 3960 uint32_t alignment, 3961 bool map_and_fenceable, 3962 bool nonblocking) 3963 { 3964 struct i915_vma *vma; 3965 int ret; 3966 3967 if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT)) 3968 return -EBUSY; 3969 3970 WARN_ON(map_and_fenceable && !i915_is_ggtt(vm)); 3971 3972 vma = i915_gem_obj_to_vma(obj, vm); 3973 3974 if (vma) { 3975 if ((alignment && 3976 vma->node.start & (alignment - 1)) || 3977 (map_and_fenceable && !obj->map_and_fenceable)) { 3978 WARN(obj->pin_count, 3979 "bo is already pinned with incorrect alignment:" 3980 " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d," 3981 " obj->map_and_fenceable=%d\n", 3982 i915_gem_obj_offset(obj, vm), alignment, 3983 map_and_fenceable, 3984 obj->map_and_fenceable); 3985 ret = i915_vma_unbind(vma); 3986 if (ret) 3987 return ret; 3988 } 3989 } 3990 3991 if (!i915_gem_obj_bound(obj, vm)) { 3992 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 3993 3994 ret = i915_gem_object_bind_to_vm(obj, vm, alignment, 3995 map_and_fenceable, 3996 nonblocking); 3997 if (ret) 3998 return ret; 3999 4000 if (!dev_priv->mm.aliasing_ppgtt) 4001 i915_gem_gtt_bind_object(obj, obj->cache_level); 4002 } 4003 4004 if (!obj->has_global_gtt_mapping && map_and_fenceable) 4005 i915_gem_gtt_bind_object(obj, obj->cache_level); 4006 4007 obj->pin_count++; 4008 obj->pin_mappable |= map_and_fenceable; 4009 4010 return 0; 4011 } 4012 4013 void 4014 i915_gem_object_unpin(struct drm_i915_gem_object *obj) 4015 { 4016 BUG_ON(obj->pin_count == 0); 4017 BUG_ON(!i915_gem_obj_bound_any(obj)); 4018 4019 if (--obj->pin_count == 0) 4020 obj->pin_mappable = false; 4021 } 4022 4023 int 4024 i915_gem_pin_ioctl(struct drm_device *dev, void *data, 4025 struct drm_file *file) 4026 { 4027 struct drm_i915_gem_pin *args = data; 4028 struct drm_i915_gem_object *obj; 4029 int ret; 4030 4031 ret = i915_mutex_lock_interruptible(dev); 4032 if (ret) 4033 return ret; 4034 4035 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 4036 if (&obj->base == NULL) { 4037 ret = -ENOENT; 4038 goto unlock; 4039 } 4040 4041 if (obj->madv != I915_MADV_WILLNEED) { 4042 DRM_ERROR("Attempting to pin a purgeable buffer\n"); 4043 ret = -EINVAL; 4044 goto out; 4045 } 4046 4047 if (obj->pin_filp != NULL && obj->pin_filp != file) { 4048 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n", 4049 args->handle); 4050 ret = -EINVAL; 4051 goto out; 4052 } 4053 4054 if (obj->user_pin_count == ULONG_MAX) { 4055 ret = -EBUSY; 4056 goto out; 4057 } 4058 4059 if (obj->user_pin_count == 0) { 4060 ret = i915_gem_obj_ggtt_pin(obj, args->alignment, true, false); 4061 if (ret) 4062 goto out; 4063 } 4064 4065 obj->user_pin_count++; 4066 obj->pin_filp = file; 4067 4068 args->offset = i915_gem_obj_ggtt_offset(obj); 4069 out: 4070 drm_gem_object_unreference(&obj->base); 4071 unlock: 4072 mutex_unlock(&dev->struct_mutex); 4073 return ret; 4074 } 4075 4076 int 4077 i915_gem_unpin_ioctl(struct drm_device *dev, void *data, 4078 struct drm_file *file) 4079 { 4080 struct drm_i915_gem_pin *args = data; 4081 struct drm_i915_gem_object *obj; 4082 int ret; 4083 4084 ret = i915_mutex_lock_interruptible(dev); 4085 if (ret) 4086 return ret; 4087 4088 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 4089 if (&obj->base == NULL) { 4090 ret = -ENOENT; 4091 goto unlock; 4092 } 4093 4094 if (obj->pin_filp != file) { 4095 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", 4096 args->handle); 4097 ret = -EINVAL; 4098 goto out; 4099 } 4100 obj->user_pin_count--; 4101 if (obj->user_pin_count == 0) { 4102 obj->pin_filp = NULL; 4103 i915_gem_object_unpin(obj); 4104 } 4105 4106 out: 4107 drm_gem_object_unreference(&obj->base); 4108 unlock: 4109 mutex_unlock(&dev->struct_mutex); 4110 return ret; 4111 } 4112 4113 int 4114 i915_gem_busy_ioctl(struct drm_device *dev, void *data, 4115 struct drm_file *file) 4116 { 4117 struct drm_i915_gem_busy *args = data; 4118 struct drm_i915_gem_object *obj; 4119 int ret; 4120 4121 ret = i915_mutex_lock_interruptible(dev); 4122 if (ret) 4123 return ret; 4124 4125 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); 4126 if (&obj->base == NULL) { 4127 ret = -ENOENT; 4128 goto unlock; 4129 } 4130 4131 /* Count all active objects as busy, even if they are currently not used 4132 * by the gpu. Users of this interface expect objects to eventually 4133 * become non-busy without any further actions, therefore emit any 4134 * necessary flushes here. 4135 */ 4136 ret = i915_gem_object_flush_active(obj); 4137 4138 args->busy = obj->active; 4139 if (obj->ring) { 4140 args->busy |= intel_ring_flag(obj->ring) << 16; 4141 } 4142 4143 drm_gem_object_unreference(&obj->base); 4144 unlock: 4145 mutex_unlock(&dev->struct_mutex); 4146 return ret; 4147 } 4148 4149 int 4150 i915_gem_throttle_ioctl(struct drm_device *dev, void *data, 4151 struct drm_file *file_priv) 4152 { 4153 return i915_gem_ring_throttle(dev, file_priv); 4154 } 4155 4156 int 4157 i915_gem_madvise_ioctl(struct drm_device *dev, void *data, 4158 struct drm_file *file_priv) 4159 { 4160 struct drm_i915_gem_madvise *args = data; 4161 struct drm_i915_gem_object *obj; 4162 int ret; 4163 4164 switch (args->madv) { 4165 case I915_MADV_DONTNEED: 4166 case I915_MADV_WILLNEED: 4167 break; 4168 default: 4169 return -EINVAL; 4170 } 4171 4172 ret = i915_mutex_lock_interruptible(dev); 4173 if (ret) 4174 return ret; 4175 4176 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle)); 4177 if (&obj->base == NULL) { 4178 ret = -ENOENT; 4179 goto unlock; 4180 } 4181 4182 if (obj->pin_count) { 4183 ret = -EINVAL; 4184 goto out; 4185 } 4186 4187 if (obj->madv != __I915_MADV_PURGED) 4188 obj->madv = args->madv; 4189 4190 /* if the object is no longer attached, discard its backing storage */ 4191 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL) 4192 i915_gem_object_truncate(obj); 4193 4194 args->retained = obj->madv != __I915_MADV_PURGED; 4195 4196 out: 4197 drm_gem_object_unreference(&obj->base); 4198 unlock: 4199 mutex_unlock(&dev->struct_mutex); 4200 return ret; 4201 } 4202 4203 void i915_gem_object_init(struct drm_i915_gem_object *obj, 4204 const struct drm_i915_gem_object_ops *ops) 4205 { 4206 INIT_LIST_HEAD(&obj->global_list); 4207 INIT_LIST_HEAD(&obj->ring_list); 4208 INIT_LIST_HEAD(&obj->obj_exec_link); 4209 INIT_LIST_HEAD(&obj->vma_list); 4210 4211 obj->ops = ops; 4212 4213 obj->fence_reg = I915_FENCE_REG_NONE; 4214 obj->madv = I915_MADV_WILLNEED; 4215 /* Avoid an unnecessary call to unbind on the first bind. */ 4216 obj->map_and_fenceable = true; 4217 4218 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size); 4219 } 4220 4221 static const struct drm_i915_gem_object_ops i915_gem_object_ops = { 4222 .get_pages = i915_gem_object_get_pages_gtt, 4223 .put_pages = i915_gem_object_put_pages_gtt, 4224 }; 4225 4226 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev, 4227 size_t size) 4228 { 4229 struct drm_i915_gem_object *obj; 4230 #if 0 4231 struct address_space *mapping; 4232 gfp_t mask; 4233 #endif 4234 4235 obj = i915_gem_object_alloc(dev); 4236 if (obj == NULL) 4237 return NULL; 4238 4239 if (drm_gem_object_init(dev, &obj->base, size) != 0) { 4240 i915_gem_object_free(obj); 4241 return NULL; 4242 } 4243 4244 #if 0 4245 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; 4246 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) { 4247 /* 965gm cannot relocate objects above 4GiB. */ 4248 mask &= ~__GFP_HIGHMEM; 4249 mask |= __GFP_DMA32; 4250 } 4251 4252 mapping = file_inode(obj->base.filp)->i_mapping; 4253 mapping_set_gfp_mask(mapping, mask); 4254 #endif 4255 4256 i915_gem_object_init(obj, &i915_gem_object_ops); 4257 4258 obj->base.write_domain = I915_GEM_DOMAIN_CPU; 4259 obj->base.read_domains = I915_GEM_DOMAIN_CPU; 4260 4261 if (HAS_LLC(dev)) { 4262 /* On some devices, we can have the GPU use the LLC (the CPU 4263 * cache) for about a 10% performance improvement 4264 * compared to uncached. Graphics requests other than 4265 * display scanout are coherent with the CPU in 4266 * accessing this cache. This means in this mode we 4267 * don't need to clflush on the CPU side, and on the 4268 * GPU side we only need to flush internal caches to 4269 * get data visible to the CPU. 4270 * 4271 * However, we maintain the display planes as UC, and so 4272 * need to rebind when first used as such. 4273 */ 4274 obj->cache_level = I915_CACHE_LLC; 4275 } else 4276 obj->cache_level = I915_CACHE_NONE; 4277 4278 trace_i915_gem_object_create(obj); 4279 4280 return obj; 4281 } 4282 4283 void i915_gem_free_object(struct drm_gem_object *gem_obj) 4284 { 4285 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); 4286 struct drm_device *dev = obj->base.dev; 4287 drm_i915_private_t *dev_priv = dev->dev_private; 4288 struct i915_vma *vma, *next; 4289 4290 intel_runtime_pm_get(dev_priv); 4291 4292 trace_i915_gem_object_destroy(obj); 4293 4294 if (obj->phys_obj) 4295 i915_gem_detach_phys_object(dev, obj); 4296 4297 obj->pin_count = 0; 4298 /* NB: 0 or 1 elements */ 4299 #if 0 4300 WARN_ON(!list_empty(&obj->vma_list) && 4301 !list_is_singular(&obj->vma_list)); 4302 #endif 4303 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) { 4304 int ret = i915_vma_unbind(vma); 4305 if (WARN_ON(ret == -ERESTARTSYS)) { 4306 bool was_interruptible; 4307 4308 was_interruptible = dev_priv->mm.interruptible; 4309 dev_priv->mm.interruptible = false; 4310 4311 WARN_ON(i915_vma_unbind(vma)); 4312 4313 dev_priv->mm.interruptible = was_interruptible; 4314 } 4315 } 4316 4317 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up 4318 * before progressing. */ 4319 if (obj->stolen) 4320 i915_gem_object_unpin_pages(obj); 4321 4322 if (WARN_ON(obj->pages_pin_count)) 4323 obj->pages_pin_count = 0; 4324 i915_gem_object_put_pages(obj); 4325 i915_gem_object_free_mmap_offset(obj); 4326 4327 BUG_ON(obj->pages); 4328 4329 #if 0 4330 if (obj->base.import_attach) 4331 drm_prime_gem_destroy(&obj->base, NULL); 4332 #endif 4333 4334 drm_gem_object_release(&obj->base); 4335 i915_gem_info_remove_obj(dev_priv, obj->base.size); 4336 4337 kfree(obj->bit_17); 4338 i915_gem_object_free(obj); 4339 4340 intel_runtime_pm_put(dev_priv); 4341 } 4342 4343 struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj, 4344 struct i915_address_space *vm) 4345 { 4346 struct i915_vma *vma; 4347 list_for_each_entry(vma, &obj->vma_list, vma_link) 4348 if (vma->vm == vm) 4349 return vma; 4350 4351 return NULL; 4352 } 4353 4354 static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj, 4355 struct i915_address_space *vm) 4356 { 4357 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL); 4358 if (vma == NULL) 4359 return ERR_PTR(-ENOMEM); 4360 4361 INIT_LIST_HEAD(&vma->vma_link); 4362 INIT_LIST_HEAD(&vma->mm_list); 4363 INIT_LIST_HEAD(&vma->exec_list); 4364 vma->vm = vm; 4365 vma->obj = obj; 4366 4367 /* Keep GGTT vmas first to make debug easier */ 4368 if (i915_is_ggtt(vm)) 4369 list_add(&vma->vma_link, &obj->vma_list); 4370 else 4371 list_add_tail(&vma->vma_link, &obj->vma_list); 4372 4373 return vma; 4374 } 4375 4376 struct i915_vma * 4377 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj, 4378 struct i915_address_space *vm) 4379 { 4380 struct i915_vma *vma; 4381 4382 vma = i915_gem_obj_to_vma(obj, vm); 4383 if (!vma) 4384 vma = __i915_gem_vma_create(obj, vm); 4385 4386 return vma; 4387 } 4388 4389 void i915_gem_vma_destroy(struct i915_vma *vma) 4390 { 4391 WARN_ON(vma->node.allocated); 4392 4393 /* Keep the vma as a placeholder in the execbuffer reservation lists */ 4394 if (!list_empty(&vma->exec_list)) 4395 return; 4396 4397 list_del(&vma->vma_link); 4398 4399 kfree(vma); 4400 } 4401 4402 int 4403 i915_gem_suspend(struct drm_device *dev) 4404 { 4405 drm_i915_private_t *dev_priv = dev->dev_private; 4406 int ret = 0; 4407 4408 mutex_lock(&dev->struct_mutex); 4409 if (dev_priv->ums.mm_suspended) 4410 goto err; 4411 4412 ret = i915_gpu_idle(dev); 4413 if (ret) 4414 goto err; 4415 4416 i915_gem_retire_requests(dev); 4417 4418 /* Under UMS, be paranoid and evict. */ 4419 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 4420 i915_gem_evict_everything(dev); 4421 4422 i915_kernel_lost_context(dev); 4423 i915_gem_cleanup_ringbuffer(dev); 4424 4425 /* Hack! Don't let anybody do execbuf while we don't control the chip. 4426 * We need to replace this with a semaphore, or something. 4427 * And not confound ums.mm_suspended! 4428 */ 4429 dev_priv->ums.mm_suspended = !drm_core_check_feature(dev, 4430 DRIVER_MODESET); 4431 mutex_unlock(&dev->struct_mutex); 4432 4433 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer); 4434 cancel_delayed_work_sync(&dev_priv->mm.retire_work); 4435 cancel_delayed_work_sync(&dev_priv->mm.idle_work); 4436 4437 return 0; 4438 4439 err: 4440 mutex_unlock(&dev->struct_mutex); 4441 return ret; 4442 } 4443 4444 int i915_gem_l3_remap(struct intel_ring_buffer *ring, int slice) 4445 { 4446 struct drm_device *dev = ring->dev; 4447 drm_i915_private_t *dev_priv = dev->dev_private; 4448 u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200); 4449 u32 *remap_info = dev_priv->l3_parity.remap_info[slice]; 4450 int i, ret; 4451 4452 if (!HAS_L3_DPF(dev) || !remap_info) 4453 return 0; 4454 4455 ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3); 4456 if (ret) 4457 return ret; 4458 4459 /* 4460 * Note: We do not worry about the concurrent register cacheline hang 4461 * here because no other code should access these registers other than 4462 * at initialization time. 4463 */ 4464 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) { 4465 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); 4466 intel_ring_emit(ring, reg_base + i); 4467 intel_ring_emit(ring, remap_info[i/4]); 4468 } 4469 4470 intel_ring_advance(ring); 4471 4472 return ret; 4473 } 4474 4475 void i915_gem_init_swizzling(struct drm_device *dev) 4476 { 4477 drm_i915_private_t *dev_priv = dev->dev_private; 4478 4479 if (INTEL_INFO(dev)->gen < 5 || 4480 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) 4481 return; 4482 4483 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | 4484 DISP_TILE_SURFACE_SWIZZLING); 4485 4486 if (IS_GEN5(dev)) 4487 return; 4488 4489 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); 4490 if (IS_GEN6(dev)) 4491 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); 4492 else if (IS_GEN7(dev)) 4493 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); 4494 else if (IS_GEN8(dev)) 4495 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); 4496 else 4497 BUG(); 4498 } 4499 4500 static bool 4501 intel_enable_blt(struct drm_device *dev) 4502 { 4503 int revision; 4504 4505 if (!HAS_BLT(dev)) 4506 return false; 4507 4508 /* The blitter was dysfunctional on early prototypes */ 4509 revision = pci_read_config(dev->dev, PCIR_REVID, 1); 4510 if (IS_GEN6(dev) && revision < 8) { 4511 DRM_INFO("BLT not supported on this pre-production hardware;" 4512 " graphics performance will be degraded.\n"); 4513 return false; 4514 } 4515 4516 return true; 4517 } 4518 4519 static int i915_gem_init_rings(struct drm_device *dev) 4520 { 4521 struct drm_i915_private *dev_priv = dev->dev_private; 4522 int ret; 4523 4524 ret = intel_init_render_ring_buffer(dev); 4525 if (ret) 4526 return ret; 4527 4528 if (HAS_BSD(dev)) { 4529 ret = intel_init_bsd_ring_buffer(dev); 4530 if (ret) 4531 goto cleanup_render_ring; 4532 } 4533 4534 if (intel_enable_blt(dev)) { 4535 ret = intel_init_blt_ring_buffer(dev); 4536 if (ret) 4537 goto cleanup_bsd_ring; 4538 } 4539 4540 if (HAS_VEBOX(dev)) { 4541 ret = intel_init_vebox_ring_buffer(dev); 4542 if (ret) 4543 goto cleanup_blt_ring; 4544 } 4545 4546 4547 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000)); 4548 if (ret) 4549 goto cleanup_vebox_ring; 4550 4551 return 0; 4552 4553 cleanup_vebox_ring: 4554 intel_cleanup_ring_buffer(&dev_priv->ring[VECS]); 4555 cleanup_blt_ring: 4556 intel_cleanup_ring_buffer(&dev_priv->ring[BCS]); 4557 cleanup_bsd_ring: 4558 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]); 4559 cleanup_render_ring: 4560 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]); 4561 4562 return ret; 4563 } 4564 4565 int 4566 i915_gem_init_hw(struct drm_device *dev) 4567 { 4568 drm_i915_private_t *dev_priv = dev->dev_private; 4569 int ret, i; 4570 4571 #if 0 4572 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt()) 4573 return -EIO; 4574 #endif 4575 4576 if (dev_priv->ellc_size) 4577 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); 4578 4579 if (IS_HASWELL(dev)) 4580 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ? 4581 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); 4582 4583 if (HAS_PCH_NOP(dev)) { 4584 u32 temp = I915_READ(GEN7_MSG_CTL); 4585 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); 4586 I915_WRITE(GEN7_MSG_CTL, temp); 4587 } 4588 4589 i915_gem_init_swizzling(dev); 4590 4591 ret = i915_gem_init_rings(dev); 4592 if (ret) 4593 return ret; 4594 4595 for (i = 0; i < NUM_L3_SLICES(dev); i++) 4596 i915_gem_l3_remap(&dev_priv->ring[RCS], i); 4597 4598 /* 4599 * XXX: There was some w/a described somewhere suggesting loading 4600 * contexts before PPGTT. 4601 */ 4602 ret = i915_gem_context_init(dev); 4603 if (ret) { 4604 i915_gem_cleanup_ringbuffer(dev); 4605 DRM_ERROR("Context initialization failed %d\n", ret); 4606 return ret; 4607 } 4608 4609 if (dev_priv->mm.aliasing_ppgtt) { 4610 ret = dev_priv->mm.aliasing_ppgtt->enable(dev); 4611 if (ret) { 4612 i915_gem_cleanup_aliasing_ppgtt(dev); 4613 DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n"); 4614 } 4615 } 4616 4617 return 0; 4618 } 4619 4620 int i915_gem_init(struct drm_device *dev) 4621 { 4622 struct drm_i915_private *dev_priv = dev->dev_private; 4623 int ret; 4624 4625 mutex_lock(&dev->struct_mutex); 4626 4627 if (IS_VALLEYVIEW(dev)) { 4628 /* VLVA0 (potential hack), BIOS isn't actually waking us */ 4629 I915_WRITE(VLV_GTLC_WAKE_CTRL, 1); 4630 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10)) 4631 DRM_DEBUG_DRIVER("allow wake ack timed out\n"); 4632 } 4633 4634 i915_gem_init_global_gtt(dev); 4635 4636 ret = i915_gem_init_hw(dev); 4637 mutex_unlock(&dev->struct_mutex); 4638 if (ret) { 4639 i915_gem_cleanup_aliasing_ppgtt(dev); 4640 return ret; 4641 } 4642 4643 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */ 4644 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 4645 dev_priv->dri1.allow_batchbuffer = 1; 4646 return 0; 4647 } 4648 4649 void 4650 i915_gem_cleanup_ringbuffer(struct drm_device *dev) 4651 { 4652 drm_i915_private_t *dev_priv = dev->dev_private; 4653 struct intel_ring_buffer *ring; 4654 int i; 4655 4656 for_each_ring(ring, dev_priv, i) 4657 intel_cleanup_ring_buffer(ring); 4658 } 4659 4660 int 4661 i915_gem_entervt_ioctl(struct drm_device *dev, void *data, 4662 struct drm_file *file_priv) 4663 { 4664 struct drm_i915_private *dev_priv = dev->dev_private; 4665 int ret; 4666 4667 if (drm_core_check_feature(dev, DRIVER_MODESET)) 4668 return 0; 4669 4670 if (i915_reset_in_progress(&dev_priv->gpu_error)) { 4671 DRM_ERROR("Reenabling wedged hardware, good luck\n"); 4672 atomic_set(&dev_priv->gpu_error.reset_counter, 0); 4673 } 4674 4675 mutex_lock(&dev->struct_mutex); 4676 dev_priv->ums.mm_suspended = 0; 4677 4678 ret = i915_gem_init_hw(dev); 4679 if (ret != 0) { 4680 mutex_unlock(&dev->struct_mutex); 4681 return ret; 4682 } 4683 4684 BUG_ON(!list_empty(&dev_priv->gtt.base.active_list)); 4685 mutex_unlock(&dev->struct_mutex); 4686 4687 ret = drm_irq_install(dev); 4688 if (ret) 4689 goto cleanup_ringbuffer; 4690 4691 return 0; 4692 4693 cleanup_ringbuffer: 4694 mutex_lock(&dev->struct_mutex); 4695 i915_gem_cleanup_ringbuffer(dev); 4696 dev_priv->ums.mm_suspended = 1; 4697 mutex_unlock(&dev->struct_mutex); 4698 4699 return ret; 4700 } 4701 4702 int 4703 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, 4704 struct drm_file *file_priv) 4705 { 4706 if (drm_core_check_feature(dev, DRIVER_MODESET)) 4707 return 0; 4708 4709 drm_irq_uninstall(dev); 4710 4711 return i915_gem_suspend(dev); 4712 } 4713 4714 void 4715 i915_gem_lastclose(struct drm_device *dev) 4716 { 4717 int ret; 4718 4719 if (drm_core_check_feature(dev, DRIVER_MODESET)) 4720 return; 4721 4722 ret = i915_gem_suspend(dev); 4723 if (ret) 4724 DRM_ERROR("failed to idle hardware: %d\n", ret); 4725 } 4726 4727 static void 4728 init_ring_lists(struct intel_ring_buffer *ring) 4729 { 4730 INIT_LIST_HEAD(&ring->active_list); 4731 INIT_LIST_HEAD(&ring->request_list); 4732 } 4733 4734 static void i915_init_vm(struct drm_i915_private *dev_priv, 4735 struct i915_address_space *vm) 4736 { 4737 vm->dev = dev_priv->dev; 4738 INIT_LIST_HEAD(&vm->active_list); 4739 INIT_LIST_HEAD(&vm->inactive_list); 4740 INIT_LIST_HEAD(&vm->global_link); 4741 list_add(&vm->global_link, &dev_priv->vm_list); 4742 } 4743 4744 void 4745 i915_gem_load(struct drm_device *dev) 4746 { 4747 drm_i915_private_t *dev_priv = dev->dev_private; 4748 int i; 4749 4750 INIT_LIST_HEAD(&dev_priv->vm_list); 4751 i915_init_vm(dev_priv, &dev_priv->gtt.base); 4752 4753 INIT_LIST_HEAD(&dev_priv->context_list); 4754 INIT_LIST_HEAD(&dev_priv->mm.unbound_list); 4755 INIT_LIST_HEAD(&dev_priv->mm.bound_list); 4756 INIT_LIST_HEAD(&dev_priv->mm.fence_list); 4757 for (i = 0; i < I915_NUM_RINGS; i++) 4758 init_ring_lists(&dev_priv->ring[i]); 4759 for (i = 0; i < I915_MAX_NUM_FENCES; i++) 4760 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); 4761 INIT_DELAYED_WORK(&dev_priv->mm.retire_work, 4762 i915_gem_retire_work_handler); 4763 INIT_DELAYED_WORK(&dev_priv->mm.idle_work, 4764 i915_gem_idle_work_handler); 4765 init_waitqueue_head(&dev_priv->gpu_error.reset_queue); 4766 4767 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */ 4768 if (IS_GEN3(dev)) { 4769 I915_WRITE(MI_ARB_STATE, 4770 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE)); 4771 } 4772 4773 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL; 4774 4775 /* Old X drivers will take 0-2 for front, back, depth buffers */ 4776 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 4777 dev_priv->fence_reg_start = 3; 4778 4779 if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev)) 4780 dev_priv->num_fence_regs = 32; 4781 else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) 4782 dev_priv->num_fence_regs = 16; 4783 else 4784 dev_priv->num_fence_regs = 8; 4785 4786 /* Initialize fence registers to zero */ 4787 INIT_LIST_HEAD(&dev_priv->mm.fence_list); 4788 i915_gem_restore_fences(dev); 4789 4790 i915_gem_detect_bit_6_swizzle(dev); 4791 init_waitqueue_head(&dev_priv->pending_flip_queue); 4792 4793 dev_priv->mm.interruptible = true; 4794 4795 #if 0 4796 dev_priv->mm.inactive_shrinker.scan_objects = i915_gem_inactive_scan; 4797 dev_priv->mm.inactive_shrinker.count_objects = i915_gem_inactive_count; 4798 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS; 4799 register_shrinker(&dev_priv->mm.inactive_shrinker); 4800 /* Old FreeBSD code */ 4801 dev_priv->mm.inactive_shrinker = EVENTHANDLER_REGISTER(vm_lowmem, 4802 i915_gem_inactive_shrink, dev, EVENTHANDLER_PRI_ANY); 4803 #endif 4804 } 4805 4806 /* 4807 * Create a physically contiguous memory object for this object 4808 * e.g. for cursor + overlay regs 4809 */ 4810 static int i915_gem_init_phys_object(struct drm_device *dev, 4811 int id, int size, int align) 4812 { 4813 drm_i915_private_t *dev_priv = dev->dev_private; 4814 struct drm_i915_gem_phys_object *phys_obj; 4815 int ret; 4816 4817 if (dev_priv->mm.phys_objs[id - 1] || !size) 4818 return 0; 4819 4820 phys_obj = kzalloc(sizeof(*phys_obj), GFP_KERNEL); 4821 if (!phys_obj) 4822 return -ENOMEM; 4823 4824 phys_obj->id = id; 4825 4826 phys_obj->handle = drm_pci_alloc(dev, size, align); 4827 if (!phys_obj->handle) { 4828 ret = -ENOMEM; 4829 goto kfree_obj; 4830 } 4831 #ifdef CONFIG_X86 4832 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); 4833 #endif 4834 pmap_change_attr((vm_offset_t)phys_obj->handle->vaddr, 4835 size / PAGE_SIZE, PAT_WRITE_COMBINING); 4836 4837 dev_priv->mm.phys_objs[id - 1] = phys_obj; 4838 4839 return 0; 4840 kfree_obj: 4841 kfree(phys_obj); 4842 return ret; 4843 } 4844 4845 static void i915_gem_free_phys_object(struct drm_device *dev, int id) 4846 { 4847 drm_i915_private_t *dev_priv = dev->dev_private; 4848 struct drm_i915_gem_phys_object *phys_obj; 4849 4850 if (!dev_priv->mm.phys_objs[id - 1]) 4851 return; 4852 4853 phys_obj = dev_priv->mm.phys_objs[id - 1]; 4854 if (phys_obj->cur_obj) { 4855 i915_gem_detach_phys_object(dev, phys_obj->cur_obj); 4856 } 4857 4858 #ifdef CONFIG_X86 4859 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); 4860 #endif 4861 drm_pci_free(dev, phys_obj->handle); 4862 kfree(phys_obj); 4863 dev_priv->mm.phys_objs[id - 1] = NULL; 4864 } 4865 4866 void i915_gem_free_all_phys_object(struct drm_device *dev) 4867 { 4868 int i; 4869 4870 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++) 4871 i915_gem_free_phys_object(dev, i); 4872 } 4873 4874 void i915_gem_detach_phys_object(struct drm_device *dev, 4875 struct drm_i915_gem_object *obj) 4876 { 4877 struct vm_object *mapping = obj->base.vm_obj; 4878 char *vaddr; 4879 int i; 4880 int page_count; 4881 4882 if (!obj->phys_obj) 4883 return; 4884 vaddr = obj->phys_obj->handle->vaddr; 4885 4886 page_count = obj->base.size / PAGE_SIZE; 4887 for (i = 0; i < page_count; i++) { 4888 struct vm_page *page = shmem_read_mapping_page(mapping, i); 4889 if (!IS_ERR(page)) { 4890 char *dst = kmap_atomic(page); 4891 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE); 4892 kunmap_atomic(dst); 4893 4894 drm_clflush_pages(&page, 1); 4895 4896 set_page_dirty(page); 4897 mark_page_accessed(page); 4898 #if 0 4899 page_cache_release(page); 4900 #endif 4901 vm_page_busy_wait(page, FALSE, "i915gem"); 4902 vm_page_unwire(page, 0); 4903 vm_page_wakeup(page); 4904 } 4905 } 4906 i915_gem_chipset_flush(dev); 4907 4908 obj->phys_obj->cur_obj = NULL; 4909 obj->phys_obj = NULL; 4910 } 4911 4912 int 4913 i915_gem_attach_phys_object(struct drm_device *dev, 4914 struct drm_i915_gem_object *obj, 4915 int id, 4916 int align) 4917 { 4918 struct vm_object *mapping = obj->base.vm_obj; 4919 drm_i915_private_t *dev_priv = dev->dev_private; 4920 int ret = 0; 4921 int page_count; 4922 int i; 4923 4924 if (id > I915_MAX_PHYS_OBJECT) 4925 return -EINVAL; 4926 4927 if (obj->phys_obj) { 4928 if (obj->phys_obj->id == id) 4929 return 0; 4930 i915_gem_detach_phys_object(dev, obj); 4931 } 4932 4933 /* create a new object */ 4934 if (!dev_priv->mm.phys_objs[id - 1]) { 4935 ret = i915_gem_init_phys_object(dev, id, 4936 obj->base.size, align); 4937 if (ret) { 4938 DRM_ERROR("failed to init phys object %d size: %zu\n", 4939 id, obj->base.size); 4940 return ret; 4941 } 4942 } 4943 4944 /* bind to the object */ 4945 obj->phys_obj = dev_priv->mm.phys_objs[id - 1]; 4946 obj->phys_obj->cur_obj = obj; 4947 4948 page_count = obj->base.size / PAGE_SIZE; 4949 4950 for (i = 0; i < page_count; i++) { 4951 struct vm_page *page; 4952 char *dst, *src; 4953 4954 page = shmem_read_mapping_page(mapping, i); 4955 if (IS_ERR(page)) 4956 return PTR_ERR(page); 4957 4958 src = kmap_atomic(page); 4959 dst = (char*)obj->phys_obj->handle->vaddr + (i * PAGE_SIZE); 4960 memcpy(dst, src, PAGE_SIZE); 4961 kunmap_atomic(src); 4962 4963 mark_page_accessed(page); 4964 #if 0 4965 page_cache_release(page); 4966 #endif 4967 vm_page_busy_wait(page, FALSE, "i915gem"); 4968 vm_page_unwire(page, 0); 4969 vm_page_wakeup(page); 4970 } 4971 4972 return 0; 4973 } 4974 4975 static int 4976 i915_gem_phys_pwrite(struct drm_device *dev, 4977 struct drm_i915_gem_object *obj, 4978 struct drm_i915_gem_pwrite *args, 4979 struct drm_file *file_priv) 4980 { 4981 void *vaddr = (char *)obj->phys_obj->handle->vaddr + args->offset; 4982 char __user *user_data = to_user_ptr(args->data_ptr); 4983 4984 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) { 4985 unsigned long unwritten; 4986 4987 /* The physical object once assigned is fixed for the lifetime 4988 * of the obj, so we can safely drop the lock and continue 4989 * to access vaddr. 4990 */ 4991 mutex_unlock(&dev->struct_mutex); 4992 unwritten = copy_from_user(vaddr, user_data, args->size); 4993 mutex_lock(&dev->struct_mutex); 4994 if (unwritten) 4995 return -EFAULT; 4996 } 4997 4998 i915_gem_chipset_flush(dev); 4999 return 0; 5000 } 5001 5002 void i915_gem_release(struct drm_device *dev, struct drm_file *file) 5003 { 5004 struct drm_i915_file_private *file_priv = file->driver_priv; 5005 5006 /* Clean up our request list when the client is going away, so that 5007 * later retire_requests won't dereference our soon-to-be-gone 5008 * file_priv. 5009 */ 5010 spin_lock(&file_priv->mm.lock); 5011 while (!list_empty(&file_priv->mm.request_list)) { 5012 struct drm_i915_gem_request *request; 5013 5014 request = list_first_entry(&file_priv->mm.request_list, 5015 struct drm_i915_gem_request, 5016 client_list); 5017 list_del(&request->client_list); 5018 request->file_priv = NULL; 5019 } 5020 spin_unlock(&file_priv->mm.lock); 5021 } 5022 5023 int 5024 i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 5025 vm_ooffset_t foff, struct ucred *cred, u_short *color) 5026 { 5027 *color = 0; /* XXXKIB */ 5028 return (0); 5029 } 5030 5031 void 5032 i915_gem_pager_dtor(void *handle) 5033 { 5034 struct drm_gem_object *obj; 5035 struct drm_device *dev; 5036 5037 obj = handle; 5038 dev = obj->dev; 5039 5040 mutex_lock(&dev->struct_mutex); 5041 drm_gem_free_mmap_offset(obj); 5042 i915_gem_release_mmap(to_intel_bo(obj)); 5043 drm_gem_object_unreference(obj); 5044 mutex_unlock(&dev->struct_mutex); 5045 } 5046 5047 static void 5048 i915_gem_file_idle_work_handler(struct work_struct *work) 5049 { 5050 struct drm_i915_file_private *file_priv = 5051 container_of(work, typeof(*file_priv), mm.idle_work.work); 5052 5053 atomic_set(&file_priv->rps_wait_boost, false); 5054 } 5055 5056 int i915_gem_open(struct drm_device *dev, struct drm_file *file) 5057 { 5058 struct drm_i915_file_private *file_priv; 5059 5060 DRM_DEBUG_DRIVER("\n"); 5061 5062 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); 5063 if (!file_priv) 5064 return -ENOMEM; 5065 5066 file->driver_priv = file_priv; 5067 file_priv->dev_priv = dev->dev_private; 5068 5069 spin_init(&file_priv->mm.lock, "i915_priv"); 5070 INIT_LIST_HEAD(&file_priv->mm.request_list); 5071 INIT_DELAYED_WORK(&file_priv->mm.idle_work, 5072 i915_gem_file_idle_work_handler); 5073 5074 idr_init(&file_priv->context_idr); 5075 5076 return 0; 5077 } 5078 5079 #if 0 5080 static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task) 5081 { 5082 if (!mutex_is_locked(mutex)) 5083 return false; 5084 5085 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES) 5086 return mutex->owner == task; 5087 #else 5088 /* Since UP may be pre-empted, we cannot assume that we own the lock */ 5089 return false; 5090 #endif 5091 } 5092 #endif 5093 5094 #if 0 5095 static unsigned long 5096 i915_gem_inactive_count(struct shrinker *shrinker, struct shrink_control *sc) 5097 { 5098 struct drm_i915_private *dev_priv = 5099 container_of(shrinker, 5100 struct drm_i915_private, 5101 mm.inactive_shrinker); 5102 struct drm_device *dev = dev_priv->dev; 5103 struct drm_i915_gem_object *obj; 5104 bool unlock = true; 5105 unsigned long count; 5106 5107 if (!mutex_trylock(&dev->struct_mutex)) { 5108 if (!mutex_is_locked_by(&dev->struct_mutex, current)) 5109 return 0; 5110 5111 if (dev_priv->mm.shrinker_no_lock_stealing) 5112 return 0; 5113 5114 unlock = false; 5115 } 5116 5117 count = 0; 5118 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) 5119 if (obj->pages_pin_count == 0) 5120 count += obj->base.size >> PAGE_SHIFT; 5121 5122 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { 5123 if (obj->active) 5124 continue; 5125 5126 if (obj->pin_count == 0 && obj->pages_pin_count == 0) 5127 count += obj->base.size >> PAGE_SHIFT; 5128 } 5129 5130 if (unlock) 5131 mutex_unlock(&dev->struct_mutex); 5132 5133 return count; 5134 } 5135 #endif 5136 5137 /* All the new VM stuff */ 5138 unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o, 5139 struct i915_address_space *vm) 5140 { 5141 struct drm_i915_private *dev_priv = o->base.dev->dev_private; 5142 struct i915_vma *vma; 5143 5144 if (vm == &dev_priv->mm.aliasing_ppgtt->base) 5145 vm = &dev_priv->gtt.base; 5146 5147 BUG_ON(list_empty(&o->vma_list)); 5148 list_for_each_entry(vma, &o->vma_list, vma_link) { 5149 if (vma->vm == vm) 5150 return vma->node.start; 5151 5152 } 5153 return -1; 5154 } 5155 5156 bool i915_gem_obj_bound(struct drm_i915_gem_object *o, 5157 struct i915_address_space *vm) 5158 { 5159 struct i915_vma *vma; 5160 5161 list_for_each_entry(vma, &o->vma_list, vma_link) 5162 if (vma->vm == vm && drm_mm_node_allocated(&vma->node)) 5163 return true; 5164 5165 return false; 5166 } 5167 5168 bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o) 5169 { 5170 struct i915_vma *vma; 5171 5172 list_for_each_entry(vma, &o->vma_list, vma_link) 5173 if (drm_mm_node_allocated(&vma->node)) 5174 return true; 5175 5176 return false; 5177 } 5178 5179 unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o, 5180 struct i915_address_space *vm) 5181 { 5182 struct drm_i915_private *dev_priv = o->base.dev->dev_private; 5183 struct i915_vma *vma; 5184 5185 if (vm == &dev_priv->mm.aliasing_ppgtt->base) 5186 vm = &dev_priv->gtt.base; 5187 5188 BUG_ON(list_empty(&o->vma_list)); 5189 5190 list_for_each_entry(vma, &o->vma_list, vma_link) 5191 if (vma->vm == vm) 5192 return vma->node.size; 5193 5194 return 0; 5195 } 5196 5197 #if 0 5198 static unsigned long 5199 i915_gem_inactive_scan(struct shrinker *shrinker, struct shrink_control *sc) 5200 { 5201 struct drm_i915_private *dev_priv = 5202 container_of(shrinker, 5203 struct drm_i915_private, 5204 mm.inactive_shrinker); 5205 struct drm_device *dev = dev_priv->dev; 5206 unsigned long freed; 5207 bool unlock = true; 5208 5209 if (!mutex_trylock(&dev->struct_mutex)) { 5210 if (!mutex_is_locked_by(&dev->struct_mutex, current)) 5211 return SHRINK_STOP; 5212 5213 if (dev_priv->mm.shrinker_no_lock_stealing) 5214 return SHRINK_STOP; 5215 5216 unlock = false; 5217 } 5218 5219 freed = i915_gem_purge(dev_priv, sc->nr_to_scan); 5220 if (freed < sc->nr_to_scan) 5221 freed += __i915_gem_shrink(dev_priv, 5222 sc->nr_to_scan - freed, 5223 false); 5224 if (freed < sc->nr_to_scan) 5225 freed += i915_gem_shrink_all(dev_priv); 5226 5227 if (unlock) 5228 mutex_unlock(&dev->struct_mutex); 5229 5230 return freed; 5231 } 5232 #endif 5233 5234 struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj) 5235 { 5236 struct i915_vma *vma; 5237 5238 if (WARN_ON(list_empty(&obj->vma_list))) 5239 return NULL; 5240 5241 vma = list_first_entry(&obj->vma_list, typeof(*vma), vma_link); 5242 if (WARN_ON(vma->vm != obj_to_ggtt(obj))) 5243 return NULL; 5244 5245 return vma; 5246 } 5247