1 /* 2 * Copyright © 2008-2015 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 */ 27 28 #include <drm/drmP.h> 29 #include <drm/drm_vma_manager.h> 30 #include <drm/i915_drm.h> 31 #include "i915_drv.h" 32 #include "i915_gem_clflush.h" 33 #include "i915_vgpu.h" 34 #include "i915_trace.h" 35 #include "intel_drv.h" 36 #include "intel_frontbuffer.h" 37 #include "intel_mocs.h" 38 #include "intel_workarounds.h" 39 #include "i915_gemfs.h" 40 #include <linux/dma-fence-array.h> 41 #include <linux/kthread.h> 42 #include <linux/reservation.h> 43 #include <linux/shmem_fs.h> 44 #include <linux/slab.h> 45 #include <linux/stop_machine.h> 46 #include <linux/swap.h> 47 #include <linux/pci.h> 48 #include <linux/dma-buf.h> 49 50 static void i915_gem_flush_free_objects(struct drm_i915_private *i915); 51 52 static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) 53 { 54 if (obj->cache_dirty) 55 return false; 56 57 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) 58 return true; 59 60 return obj->pin_global; /* currently in use by HW, keep flushed */ 61 } 62 63 static int 64 insert_mappable_node(struct i915_ggtt *ggtt, 65 struct drm_mm_node *node, u32 size) 66 { 67 memset(node, 0, sizeof(*node)); 68 return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, 69 size, 0, I915_COLOR_UNEVICTABLE, 70 0, ggtt->mappable_end, 71 DRM_MM_INSERT_LOW); 72 } 73 74 static void 75 remove_mappable_node(struct drm_mm_node *node) 76 { 77 drm_mm_remove_node(node); 78 } 79 80 /* some bookkeeping */ 81 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, 82 u64 size) 83 { 84 spin_lock(&dev_priv->mm.object_stat_lock); 85 dev_priv->mm.object_count++; 86 dev_priv->mm.object_memory += size; 87 spin_unlock(&dev_priv->mm.object_stat_lock); 88 } 89 90 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, 91 u64 size) 92 { 93 spin_lock(&dev_priv->mm.object_stat_lock); 94 dev_priv->mm.object_count--; 95 dev_priv->mm.object_memory -= size; 96 spin_unlock(&dev_priv->mm.object_stat_lock); 97 } 98 99 static int 100 i915_gem_wait_for_error(struct i915_gpu_error *error) 101 { 102 int ret; 103 104 might_sleep(); 105 106 /* 107 * Only wait 10 seconds for the gpu reset to complete to avoid hanging 108 * userspace. If it takes that long something really bad is going on and 109 * we should simply try to bail out and fail as gracefully as possible. 110 */ 111 ret = wait_event_interruptible_timeout(error->reset_queue, 112 !i915_reset_backoff(error), 113 I915_RESET_TIMEOUT); 114 if (ret == 0) { 115 DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); 116 return -EIO; 117 } else if (ret < 0) { 118 return ret; 119 } else { 120 return 0; 121 } 122 } 123 124 int i915_mutex_lock_interruptible(struct drm_device *dev) 125 { 126 struct drm_i915_private *dev_priv = to_i915(dev); 127 int ret; 128 129 ret = i915_gem_wait_for_error(&dev_priv->gpu_error); 130 if (ret) 131 return ret; 132 133 ret = mutex_lock_interruptible(&dev->struct_mutex); 134 if (ret) 135 return ret; 136 137 return 0; 138 } 139 140 static u32 __i915_gem_park(struct drm_i915_private *i915) 141 { 142 GEM_TRACE("\n"); 143 144 lockdep_assert_held(&i915->drm.struct_mutex); 145 GEM_BUG_ON(i915->gt.active_requests); 146 GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); 147 148 if (!i915->gt.awake) 149 return I915_EPOCH_INVALID; 150 151 GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); 152 153 /* 154 * Be paranoid and flush a concurrent interrupt to make sure 155 * we don't reactivate any irq tasklets after parking. 156 * 157 * FIXME: Note that even though we have waited for execlists to be idle, 158 * there may still be an in-flight interrupt even though the CSB 159 * is now empty. synchronize_irq() makes sure that a residual interrupt 160 * is completed before we continue, but it doesn't prevent the HW from 161 * raising a spurious interrupt later. To complete the shield we should 162 * coordinate disabling the CS irq with flushing the interrupts. 163 */ 164 synchronize_irq(i915->drm.irq); 165 166 intel_engines_park(i915); 167 i915_timelines_park(i915); 168 169 i915_pmu_gt_parked(i915); 170 i915_vma_parked(i915); 171 172 i915->gt.awake = false; 173 174 if (INTEL_GEN(i915) >= 6) 175 gen6_rps_idle(i915); 176 177 if (NEEDS_RC6_CTX_CORRUPTION_WA(i915)) { 178 i915_rc6_ctx_wa_check(i915); 179 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); 180 } 181 182 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ); 183 184 intel_runtime_pm_put(i915); 185 186 return i915->gt.epoch; 187 } 188 189 void i915_gem_park(struct drm_i915_private *i915) 190 { 191 GEM_TRACE("\n"); 192 193 lockdep_assert_held(&i915->drm.struct_mutex); 194 GEM_BUG_ON(i915->gt.active_requests); 195 196 if (!i915->gt.awake) 197 return; 198 199 /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ 200 mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); 201 } 202 203 void i915_gem_unpark(struct drm_i915_private *i915) 204 { 205 GEM_TRACE("\n"); 206 207 lockdep_assert_held(&i915->drm.struct_mutex); 208 GEM_BUG_ON(!i915->gt.active_requests); 209 210 if (i915->gt.awake) 211 return; 212 213 intel_runtime_pm_get_noresume(i915); 214 215 /* 216 * It seems that the DMC likes to transition between the DC states a lot 217 * when there are no connected displays (no active power domains) during 218 * command submission. 219 * 220 * This activity has negative impact on the performance of the chip with 221 * huge latencies observed in the interrupt handler and elsewhere. 222 * 223 * Work around it by grabbing a GT IRQ power domain whilst there is any 224 * GT activity, preventing any DC state transitions. 225 */ 226 intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); 227 228 if (NEEDS_RC6_CTX_CORRUPTION_WA(i915)) 229 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); 230 231 i915->gt.awake = true; 232 if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ 233 i915->gt.epoch = 1; 234 235 intel_enable_gt_powersave(i915); 236 i915_update_gfx_val(i915); 237 if (INTEL_GEN(i915) >= 6) 238 gen6_rps_busy(i915); 239 i915_pmu_gt_unparked(i915); 240 241 intel_engines_unpark(i915); 242 243 i915_queue_hangcheck(i915); 244 245 queue_delayed_work(i915->wq, 246 &i915->gt.retire_work, 247 round_jiffies_up_relative(HZ)); 248 } 249 250 int 251 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, 252 struct drm_file *file) 253 { 254 struct drm_i915_private *dev_priv = to_i915(dev); 255 struct i915_ggtt *ggtt = &dev_priv->ggtt; 256 struct drm_i915_gem_get_aperture *args = data; 257 struct i915_vma *vma; 258 u64 pinned; 259 260 pinned = ggtt->vm.reserved; 261 mutex_lock(&dev->struct_mutex); 262 list_for_each_entry(vma, &ggtt->vm.active_list, vm_link) 263 if (i915_vma_is_pinned(vma)) 264 pinned += vma->node.size; 265 list_for_each_entry(vma, &ggtt->vm.inactive_list, vm_link) 266 if (i915_vma_is_pinned(vma)) 267 pinned += vma->node.size; 268 mutex_unlock(&dev->struct_mutex); 269 270 args->aper_size = ggtt->vm.total; 271 args->aper_available_size = args->aper_size - pinned; 272 273 return 0; 274 } 275 276 static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj) 277 { 278 #ifdef __linux__ 279 struct address_space *mapping = obj->base.filp->f_mapping; 280 #endif 281 drm_dma_handle_t *phys; 282 struct sg_table *st; 283 struct scatterlist *sg; 284 char *vaddr; 285 int i; 286 int err; 287 288 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) 289 return -EINVAL; 290 291 /* Always aligning to the object size, allows a single allocation 292 * to handle all possible callers, and given typical object sizes, 293 * the alignment of the buddy allocation will naturally match. 294 */ 295 phys = drm_pci_alloc(obj->base.dev, 296 roundup_pow_of_two(obj->base.size), 297 roundup_pow_of_two(obj->base.size)); 298 if (!phys) 299 return -ENOMEM; 300 301 vaddr = phys->vaddr; 302 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { 303 struct vm_page *page; 304 char *src; 305 306 #ifdef __linux__ 307 page = shmem_read_mapping_page(mapping, i); 308 if (IS_ERR(page)) { 309 err = PTR_ERR(page); 310 goto err_phys; 311 } 312 #else 313 struct pglist plist; 314 TAILQ_INIT(&plist); 315 if (uvm_objwire(obj->base.uao, i * PAGE_SIZE, (i + 1) * PAGE_SIZE, &plist)) { 316 err = -ENOMEM; 317 goto err_phys; 318 } 319 page = TAILQ_FIRST(&plist); 320 #endif 321 322 src = kmap_atomic(page); 323 memcpy(vaddr, src, PAGE_SIZE); 324 drm_clflush_virt_range(vaddr, PAGE_SIZE); 325 kunmap_atomic(src); 326 327 #ifdef __linux__ 328 put_page(page); 329 #else 330 uvm_objunwire(obj->base.uao, i * PAGE_SIZE, (i + 1) * PAGE_SIZE); 331 #endif 332 vaddr += PAGE_SIZE; 333 } 334 335 i915_gem_chipset_flush(to_i915(obj->base.dev)); 336 337 st = kmalloc(sizeof(*st), GFP_KERNEL); 338 if (!st) { 339 err = -ENOMEM; 340 goto err_phys; 341 } 342 343 if (sg_alloc_table(st, 1, GFP_KERNEL)) { 344 kfree(st); 345 err = -ENOMEM; 346 goto err_phys; 347 } 348 349 sg = st->sgl; 350 sg->offset = 0; 351 sg->length = obj->base.size; 352 353 sg_dma_address(sg) = phys->busaddr; 354 sg_dma_len(sg) = obj->base.size; 355 356 obj->phys_handle = phys; 357 358 __i915_gem_object_set_pages(obj, st, sg->length); 359 360 return 0; 361 362 err_phys: 363 drm_pci_free(obj->base.dev, phys); 364 365 return err; 366 } 367 368 static void __start_cpu_write(struct drm_i915_gem_object *obj) 369 { 370 obj->read_domains = I915_GEM_DOMAIN_CPU; 371 obj->write_domain = I915_GEM_DOMAIN_CPU; 372 if (cpu_write_needs_clflush(obj)) 373 obj->cache_dirty = true; 374 } 375 376 static void 377 __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, 378 struct sg_table *pages, 379 bool needs_clflush) 380 { 381 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); 382 383 if (obj->mm.madv == I915_MADV_DONTNEED) 384 obj->mm.dirty = false; 385 386 if (needs_clflush && 387 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && 388 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) 389 drm_clflush_sg(pages); 390 391 __start_cpu_write(obj); 392 } 393 394 static void 395 i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, 396 struct sg_table *pages) 397 { 398 __i915_gem_object_release_shmem(obj, pages, false); 399 400 if (obj->mm.dirty) { 401 #ifdef __linux__ 402 struct address_space *mapping = obj->base.filp->f_mapping; 403 #endif 404 char *vaddr = obj->phys_handle->vaddr; 405 int i; 406 407 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { 408 struct vm_page *page; 409 char *dst; 410 411 #ifdef __linux__ 412 page = shmem_read_mapping_page(mapping, i); 413 if (IS_ERR(page)) 414 continue; 415 #else 416 struct pglist plist; 417 TAILQ_INIT(&plist); 418 if (uvm_objwire(obj->base.uao, i * PAGE_SIZE, (i + 1) * PAGE_SIZE, &plist)) 419 continue; 420 page = TAILQ_FIRST(&plist); 421 #endif 422 423 dst = kmap_atomic(page); 424 drm_clflush_virt_range(vaddr, PAGE_SIZE); 425 memcpy(dst, vaddr, PAGE_SIZE); 426 kunmap_atomic(dst); 427 428 set_page_dirty(page); 429 #ifdef __linux__ 430 if (obj->mm.madv == I915_MADV_WILLNEED) 431 mark_page_accessed(page); 432 put_page(page); 433 #else 434 uvm_objunwire(obj->base.uao, i * PAGE_SIZE, (i + 1) * PAGE_SIZE); 435 #endif 436 vaddr += PAGE_SIZE; 437 } 438 obj->mm.dirty = false; 439 } 440 441 sg_free_table(pages); 442 kfree(pages); 443 444 drm_pci_free(obj->base.dev, obj->phys_handle); 445 } 446 447 static void 448 i915_gem_object_release_phys(struct drm_i915_gem_object *obj) 449 { 450 i915_gem_object_unpin_pages(obj); 451 } 452 453 static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { 454 .get_pages = i915_gem_object_get_pages_phys, 455 .put_pages = i915_gem_object_put_pages_phys, 456 .release = i915_gem_object_release_phys, 457 }; 458 459 static const struct drm_i915_gem_object_ops i915_gem_object_ops; 460 461 int i915_gem_object_unbind(struct drm_i915_gem_object *obj) 462 { 463 struct i915_vma *vma; 464 DRM_LIST_HEAD(still_in_list); 465 int ret; 466 467 lockdep_assert_held(&obj->base.dev->struct_mutex); 468 469 /* Closed vma are removed from the obj->vma_list - but they may 470 * still have an active binding on the object. To remove those we 471 * must wait for all rendering to complete to the object (as unbinding 472 * must anyway), and retire the requests. 473 */ 474 ret = i915_gem_object_set_to_cpu_domain(obj, false); 475 if (ret) 476 return ret; 477 478 while ((vma = list_first_entry_or_null(&obj->vma_list, 479 struct i915_vma, 480 obj_link))) { 481 list_move_tail(&vma->obj_link, &still_in_list); 482 ret = i915_vma_unbind(vma); 483 if (ret) 484 break; 485 } 486 list_splice(&still_in_list, &obj->vma_list); 487 488 return ret; 489 } 490 491 static long 492 i915_gem_object_wait_fence(struct dma_fence *fence, 493 unsigned int flags, 494 long timeout, 495 struct intel_rps_client *rps_client) 496 { 497 struct i915_request *rq; 498 499 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); 500 501 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 502 return timeout; 503 504 if (!dma_fence_is_i915(fence)) 505 return dma_fence_wait_timeout(fence, 506 flags & I915_WAIT_INTERRUPTIBLE, 507 timeout); 508 509 rq = to_request(fence); 510 if (i915_request_completed(rq)) 511 goto out; 512 513 /* 514 * This client is about to stall waiting for the GPU. In many cases 515 * this is undesirable and limits the throughput of the system, as 516 * many clients cannot continue processing user input/output whilst 517 * blocked. RPS autotuning may take tens of milliseconds to respond 518 * to the GPU load and thus incurs additional latency for the client. 519 * We can circumvent that by promoting the GPU frequency to maximum 520 * before we wait. This makes the GPU throttle up much more quickly 521 * (good for benchmarks and user experience, e.g. window animations), 522 * but at a cost of spending more power processing the workload 523 * (bad for battery). Not all clients even want their results 524 * immediately and for them we should just let the GPU select its own 525 * frequency to maximise efficiency. To prevent a single client from 526 * forcing the clocks too high for the whole system, we only allow 527 * each client to waitboost once in a busy period. 528 */ 529 if (rps_client && !i915_request_started(rq)) { 530 if (INTEL_GEN(rq->i915) >= 6) 531 gen6_rps_boost(rq, rps_client); 532 } 533 534 timeout = i915_request_wait(rq, flags, timeout); 535 536 out: 537 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) 538 i915_request_retire_upto(rq); 539 540 return timeout; 541 } 542 543 static long 544 i915_gem_object_wait_reservation(struct reservation_object *resv, 545 unsigned int flags, 546 long timeout, 547 struct intel_rps_client *rps_client) 548 { 549 unsigned int seq = __read_seqcount_begin(&resv->seq); 550 struct dma_fence *excl; 551 bool prune_fences = false; 552 553 if (flags & I915_WAIT_ALL) { 554 struct dma_fence **shared; 555 unsigned int count, i; 556 int ret; 557 558 ret = reservation_object_get_fences_rcu(resv, 559 &excl, &count, &shared); 560 if (ret) 561 return ret; 562 563 for (i = 0; i < count; i++) { 564 timeout = i915_gem_object_wait_fence(shared[i], 565 flags, timeout, 566 rps_client); 567 if (timeout < 0) 568 break; 569 570 dma_fence_put(shared[i]); 571 } 572 573 for (; i < count; i++) 574 dma_fence_put(shared[i]); 575 kfree(shared); 576 577 /* 578 * If both shared fences and an exclusive fence exist, 579 * then by construction the shared fences must be later 580 * than the exclusive fence. If we successfully wait for 581 * all the shared fences, we know that the exclusive fence 582 * must all be signaled. If all the shared fences are 583 * signaled, we can prune the array and recover the 584 * floating references on the fences/requests. 585 */ 586 prune_fences = count && timeout >= 0; 587 } else { 588 excl = reservation_object_get_excl_rcu(resv); 589 } 590 591 if (excl && timeout >= 0) 592 timeout = i915_gem_object_wait_fence(excl, flags, timeout, 593 rps_client); 594 595 dma_fence_put(excl); 596 597 /* 598 * Opportunistically prune the fences iff we know they have *all* been 599 * signaled and that the reservation object has not been changed (i.e. 600 * no new fences have been added). 601 */ 602 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { 603 if (reservation_object_trylock(resv)) { 604 if (!__read_seqcount_retry(&resv->seq, seq)) 605 reservation_object_add_excl_fence(resv, NULL); 606 reservation_object_unlock(resv); 607 } 608 } 609 610 return timeout; 611 } 612 613 static void __fence_set_priority(struct dma_fence *fence, 614 const struct i915_sched_attr *attr) 615 { 616 struct i915_request *rq; 617 struct intel_engine_cs *engine; 618 619 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) 620 return; 621 622 rq = to_request(fence); 623 engine = rq->engine; 624 625 local_bh_disable(); 626 rcu_read_lock(); /* RCU serialisation for set-wedged protection */ 627 if (engine->schedule) 628 engine->schedule(rq, attr); 629 rcu_read_unlock(); 630 local_bh_enable(); /* kick the tasklets if queues were reprioritised */ 631 } 632 633 static void fence_set_priority(struct dma_fence *fence, 634 const struct i915_sched_attr *attr) 635 { 636 /* Recurse once into a fence-array */ 637 if (dma_fence_is_array(fence)) { 638 struct dma_fence_array *array = to_dma_fence_array(fence); 639 int i; 640 641 for (i = 0; i < array->num_fences; i++) 642 __fence_set_priority(array->fences[i], attr); 643 } else { 644 __fence_set_priority(fence, attr); 645 } 646 } 647 648 int 649 i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, 650 unsigned int flags, 651 const struct i915_sched_attr *attr) 652 { 653 struct dma_fence *excl; 654 655 if (flags & I915_WAIT_ALL) { 656 struct dma_fence **shared; 657 unsigned int count, i; 658 int ret; 659 660 ret = reservation_object_get_fences_rcu(obj->resv, 661 &excl, &count, &shared); 662 if (ret) 663 return ret; 664 665 for (i = 0; i < count; i++) { 666 fence_set_priority(shared[i], attr); 667 dma_fence_put(shared[i]); 668 } 669 670 kfree(shared); 671 } else { 672 excl = reservation_object_get_excl_rcu(obj->resv); 673 } 674 675 if (excl) { 676 fence_set_priority(excl, attr); 677 dma_fence_put(excl); 678 } 679 return 0; 680 } 681 682 /** 683 * Waits for rendering to the object to be completed 684 * @obj: i915 gem object 685 * @flags: how to wait (under a lock, for all rendering or just for writes etc) 686 * @timeout: how long to wait 687 * @rps_client: client (user process) to charge for any waitboosting 688 */ 689 int 690 i915_gem_object_wait(struct drm_i915_gem_object *obj, 691 unsigned int flags, 692 long timeout, 693 struct intel_rps_client *rps_client) 694 { 695 might_sleep(); 696 #if IS_ENABLED(CONFIG_LOCKDEP) 697 GEM_BUG_ON(debug_locks && 698 !!lockdep_is_held(&obj->base.dev->struct_mutex) != 699 !!(flags & I915_WAIT_LOCKED)); 700 #endif 701 GEM_BUG_ON(timeout < 0); 702 703 timeout = i915_gem_object_wait_reservation(obj->resv, 704 flags, timeout, 705 rps_client); 706 return timeout < 0 ? timeout : 0; 707 } 708 709 static struct intel_rps_client *to_rps_client(struct drm_file *file) 710 { 711 struct drm_i915_file_private *fpriv = file->driver_priv; 712 713 return &fpriv->rps_client; 714 } 715 716 static int 717 i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, 718 struct drm_i915_gem_pwrite *args, 719 struct drm_file *file) 720 { 721 void *vaddr = obj->phys_handle->vaddr + args->offset; 722 char __user *user_data = u64_to_user_ptr(args->data_ptr); 723 724 /* We manually control the domain here and pretend that it 725 * remains coherent i.e. in the GTT domain, like shmem_pwrite. 726 */ 727 intel_fb_obj_invalidate(obj, ORIGIN_CPU); 728 if (copy_from_user(vaddr, user_data, args->size)) 729 return -EFAULT; 730 731 drm_clflush_virt_range(vaddr, args->size); 732 i915_gem_chipset_flush(to_i915(obj->base.dev)); 733 734 intel_fb_obj_flush(obj, ORIGIN_CPU); 735 return 0; 736 } 737 738 void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) 739 { 740 #ifdef __linux__ 741 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); 742 #else 743 return pool_get(&dev_priv->objects, PR_WAITOK | PR_ZERO); 744 #endif 745 } 746 747 void i915_gem_object_free(struct drm_i915_gem_object *obj) 748 { 749 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 750 #ifdef __linux__ 751 kmem_cache_free(dev_priv->objects, obj); 752 #else 753 pool_put(&dev_priv->objects, obj); 754 #endif 755 } 756 757 static int 758 i915_gem_create(struct drm_file *file, 759 struct drm_i915_private *dev_priv, 760 uint64_t size, 761 uint32_t *handle_p) 762 { 763 struct drm_i915_gem_object *obj; 764 int ret; 765 u32 handle; 766 767 size = roundup(size, PAGE_SIZE); 768 if (size == 0) 769 return -EINVAL; 770 771 /* Allocate the new object */ 772 obj = i915_gem_object_create(dev_priv, size); 773 if (IS_ERR(obj)) 774 return PTR_ERR(obj); 775 776 ret = drm_gem_handle_create(file, &obj->base, &handle); 777 /* drop reference from allocate - handle holds it now */ 778 i915_gem_object_put(obj); 779 if (ret) 780 return ret; 781 782 *handle_p = handle; 783 return 0; 784 } 785 786 int 787 i915_gem_dumb_create(struct drm_file *file, 788 struct drm_device *dev, 789 struct drm_mode_create_dumb *args) 790 { 791 /* have to work out size/pitch and return them */ 792 args->pitch = roundup2(args->width * DIV_ROUND_UP(args->bpp, 8), 64); 793 args->size = args->pitch * args->height; 794 return i915_gem_create(file, to_i915(dev), 795 args->size, &args->handle); 796 } 797 798 static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) 799 { 800 return !(obj->cache_level == I915_CACHE_NONE || 801 obj->cache_level == I915_CACHE_WT); 802 } 803 804 /** 805 * Creates a new mm object and returns a handle to it. 806 * @dev: drm device pointer 807 * @data: ioctl data blob 808 * @file: drm file pointer 809 */ 810 int 811 i915_gem_create_ioctl(struct drm_device *dev, void *data, 812 struct drm_file *file) 813 { 814 struct drm_i915_private *dev_priv = to_i915(dev); 815 struct drm_i915_gem_create *args = data; 816 817 i915_gem_flush_free_objects(dev_priv); 818 819 return i915_gem_create(file, dev_priv, 820 args->size, &args->handle); 821 } 822 823 static inline enum fb_op_origin 824 fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) 825 { 826 return (domain == I915_GEM_DOMAIN_GTT ? 827 obj->frontbuffer_ggtt_origin : ORIGIN_CPU); 828 } 829 830 void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) 831 { 832 /* 833 * No actual flushing is required for the GTT write domain for reads 834 * from the GTT domain. Writes to it "immediately" go to main memory 835 * as far as we know, so there's no chipset flush. It also doesn't 836 * land in the GPU render cache. 837 * 838 * However, we do have to enforce the order so that all writes through 839 * the GTT land before any writes to the device, such as updates to 840 * the GATT itself. 841 * 842 * We also have to wait a bit for the writes to land from the GTT. 843 * An uncached read (i.e. mmio) seems to be ideal for the round-trip 844 * timing. This issue has only been observed when switching quickly 845 * between GTT writes and CPU reads from inside the kernel on recent hw, 846 * and it appears to only affect discrete GTT blocks (i.e. on LLC 847 * system agents we cannot reproduce this behaviour, until Cannonlake 848 * that was!). 849 */ 850 851 i915_gem_chipset_flush(dev_priv); 852 853 intel_runtime_pm_get(dev_priv); 854 spin_lock_irq(&dev_priv->uncore.lock); 855 856 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); 857 858 spin_unlock_irq(&dev_priv->uncore.lock); 859 intel_runtime_pm_put(dev_priv); 860 } 861 862 static void 863 flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) 864 { 865 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 866 struct i915_vma *vma; 867 868 if (!(obj->write_domain & flush_domains)) 869 return; 870 871 switch (obj->write_domain) { 872 case I915_GEM_DOMAIN_GTT: 873 i915_gem_flush_ggtt_writes(dev_priv); 874 875 intel_fb_obj_flush(obj, 876 fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); 877 878 for_each_ggtt_vma(vma, obj) { 879 if (vma->iomap) 880 continue; 881 882 i915_vma_unset_ggtt_write(vma); 883 } 884 break; 885 886 case I915_GEM_DOMAIN_WC: 887 wmb(); 888 break; 889 890 case I915_GEM_DOMAIN_CPU: 891 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); 892 break; 893 894 case I915_GEM_DOMAIN_RENDER: 895 if (gpu_write_needs_clflush(obj)) 896 obj->cache_dirty = true; 897 break; 898 } 899 900 obj->write_domain = 0; 901 } 902 903 static inline int 904 __copy_to_user_swizzled(char __user *cpu_vaddr, 905 const char *gpu_vaddr, int gpu_offset, 906 int length) 907 { 908 int ret, cpu_offset = 0; 909 910 while (length > 0) { 911 int cacheline_end = roundup2(gpu_offset + 1, 64); 912 int this_length = min(cacheline_end - gpu_offset, length); 913 int swizzled_gpu_offset = gpu_offset ^ 64; 914 915 ret = __copy_to_user(cpu_vaddr + cpu_offset, 916 gpu_vaddr + swizzled_gpu_offset, 917 this_length); 918 if (ret) 919 return ret + length; 920 921 cpu_offset += this_length; 922 gpu_offset += this_length; 923 length -= this_length; 924 } 925 926 return 0; 927 } 928 929 static inline int 930 __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, 931 const char __user *cpu_vaddr, 932 int length) 933 { 934 int ret, cpu_offset = 0; 935 936 while (length > 0) { 937 int cacheline_end = roundup2(gpu_offset + 1, 64); 938 int this_length = min(cacheline_end - gpu_offset, length); 939 int swizzled_gpu_offset = gpu_offset ^ 64; 940 941 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, 942 cpu_vaddr + cpu_offset, 943 this_length); 944 if (ret) 945 return ret + length; 946 947 cpu_offset += this_length; 948 gpu_offset += this_length; 949 length -= this_length; 950 } 951 952 return 0; 953 } 954 955 /* 956 * Pins the specified object's pages and synchronizes the object with 957 * GPU accesses. Sets needs_clflush to non-zero if the caller should 958 * flush the object from the CPU cache. 959 */ 960 int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj, 961 unsigned int *needs_clflush) 962 { 963 int ret; 964 965 lockdep_assert_held(&obj->base.dev->struct_mutex); 966 967 *needs_clflush = 0; 968 if (!i915_gem_object_has_struct_page(obj)) 969 return -ENODEV; 970 971 ret = i915_gem_object_wait(obj, 972 I915_WAIT_INTERRUPTIBLE | 973 I915_WAIT_LOCKED, 974 MAX_SCHEDULE_TIMEOUT, 975 NULL); 976 if (ret) 977 return ret; 978 979 ret = i915_gem_object_pin_pages(obj); 980 if (ret) 981 return ret; 982 983 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || 984 !static_cpu_has(X86_FEATURE_CLFLUSH)) { 985 ret = i915_gem_object_set_to_cpu_domain(obj, false); 986 if (ret) 987 goto err_unpin; 988 else 989 goto out; 990 } 991 992 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); 993 994 /* If we're not in the cpu read domain, set ourself into the gtt 995 * read domain and manually flush cachelines (if required). This 996 * optimizes for the case when the gpu will dirty the data 997 * anyway again before the next pread happens. 998 */ 999 if (!obj->cache_dirty && 1000 !(obj->read_domains & I915_GEM_DOMAIN_CPU)) 1001 *needs_clflush = CLFLUSH_BEFORE; 1002 1003 out: 1004 /* return with the pages pinned */ 1005 return 0; 1006 1007 err_unpin: 1008 i915_gem_object_unpin_pages(obj); 1009 return ret; 1010 } 1011 1012 int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, 1013 unsigned int *needs_clflush) 1014 { 1015 int ret; 1016 1017 lockdep_assert_held(&obj->base.dev->struct_mutex); 1018 1019 *needs_clflush = 0; 1020 if (!i915_gem_object_has_struct_page(obj)) 1021 return -ENODEV; 1022 1023 ret = i915_gem_object_wait(obj, 1024 I915_WAIT_INTERRUPTIBLE | 1025 I915_WAIT_LOCKED | 1026 I915_WAIT_ALL, 1027 MAX_SCHEDULE_TIMEOUT, 1028 NULL); 1029 if (ret) 1030 return ret; 1031 1032 ret = i915_gem_object_pin_pages(obj); 1033 if (ret) 1034 return ret; 1035 1036 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || 1037 !static_cpu_has(X86_FEATURE_CLFLUSH)) { 1038 ret = i915_gem_object_set_to_cpu_domain(obj, true); 1039 if (ret) 1040 goto err_unpin; 1041 else 1042 goto out; 1043 } 1044 1045 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); 1046 1047 /* If we're not in the cpu write domain, set ourself into the 1048 * gtt write domain and manually flush cachelines (as required). 1049 * This optimizes for the case when the gpu will use the data 1050 * right away and we therefore have to clflush anyway. 1051 */ 1052 if (!obj->cache_dirty) { 1053 *needs_clflush |= CLFLUSH_AFTER; 1054 1055 /* 1056 * Same trick applies to invalidate partially written 1057 * cachelines read before writing. 1058 */ 1059 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) 1060 *needs_clflush |= CLFLUSH_BEFORE; 1061 } 1062 1063 out: 1064 intel_fb_obj_invalidate(obj, ORIGIN_CPU); 1065 obj->mm.dirty = true; 1066 /* return with the pages pinned */ 1067 return 0; 1068 1069 err_unpin: 1070 i915_gem_object_unpin_pages(obj); 1071 return ret; 1072 } 1073 1074 static void 1075 shmem_clflush_swizzled_range(char *addr, unsigned long length, 1076 bool swizzled) 1077 { 1078 if (unlikely(swizzled)) { 1079 unsigned long start = (unsigned long) addr; 1080 unsigned long end = (unsigned long) addr + length; 1081 1082 /* For swizzling simply ensure that we always flush both 1083 * channels. Lame, but simple and it works. Swizzled 1084 * pwrite/pread is far from a hotpath - current userspace 1085 * doesn't use it at all. */ 1086 start = round_down(start, 128); 1087 end = round_up(end, 128); 1088 1089 drm_clflush_virt_range((void *)start, end - start); 1090 } else { 1091 drm_clflush_virt_range(addr, length); 1092 } 1093 1094 } 1095 1096 /* Only difference to the fast-path function is that this can handle bit17 1097 * and uses non-atomic copy and kmap functions. */ 1098 static int 1099 shmem_pread_slow(struct vm_page *page, int offset, int length, 1100 char __user *user_data, 1101 bool page_do_bit17_swizzling, bool needs_clflush) 1102 { 1103 char *vaddr; 1104 int ret; 1105 1106 vaddr = kmap(page); 1107 if (needs_clflush) 1108 shmem_clflush_swizzled_range(vaddr + offset, length, 1109 page_do_bit17_swizzling); 1110 1111 if (page_do_bit17_swizzling) 1112 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); 1113 else 1114 ret = __copy_to_user(user_data, vaddr + offset, length); 1115 kunmap(vaddr); 1116 1117 return ret ? - EFAULT : 0; 1118 } 1119 1120 static int 1121 shmem_pread(struct vm_page *page, int offset, int length, char __user *user_data, 1122 bool page_do_bit17_swizzling, bool needs_clflush) 1123 { 1124 int ret; 1125 1126 ret = -ENODEV; 1127 if (!page_do_bit17_swizzling) { 1128 char *vaddr = kmap_atomic(page); 1129 1130 if (needs_clflush) 1131 drm_clflush_virt_range(vaddr + offset, length); 1132 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); 1133 kunmap_atomic(vaddr); 1134 } 1135 if (ret == 0) 1136 return 0; 1137 1138 return shmem_pread_slow(page, offset, length, user_data, 1139 page_do_bit17_swizzling, needs_clflush); 1140 } 1141 1142 static int 1143 i915_gem_shmem_pread(struct drm_i915_gem_object *obj, 1144 struct drm_i915_gem_pread *args) 1145 { 1146 char __user *user_data; 1147 u64 remain; 1148 unsigned int obj_do_bit17_swizzling; 1149 unsigned int needs_clflush; 1150 unsigned int idx, offset; 1151 int ret; 1152 1153 obj_do_bit17_swizzling = 0; 1154 if (i915_gem_object_needs_bit17_swizzle(obj)) 1155 obj_do_bit17_swizzling = BIT(17); 1156 1157 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); 1158 if (ret) 1159 return ret; 1160 1161 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); 1162 mutex_unlock(&obj->base.dev->struct_mutex); 1163 if (ret) 1164 return ret; 1165 1166 remain = args->size; 1167 user_data = u64_to_user_ptr(args->data_ptr); 1168 offset = offset_in_page(args->offset); 1169 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { 1170 struct vm_page *page = i915_gem_object_get_page(obj, idx); 1171 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); 1172 1173 ret = shmem_pread(page, offset, length, user_data, 1174 page_to_phys(page) & obj_do_bit17_swizzling, 1175 needs_clflush); 1176 if (ret) 1177 break; 1178 1179 remain -= length; 1180 user_data += length; 1181 offset = 0; 1182 } 1183 1184 i915_gem_obj_finish_shmem_access(obj); 1185 return ret; 1186 } 1187 1188 #ifdef __linux__ 1189 static inline bool 1190 gtt_user_read(struct io_mapping *mapping, 1191 loff_t base, int offset, 1192 char __user *user_data, int length) 1193 { 1194 void __iomem *vaddr; 1195 unsigned long unwritten; 1196 1197 /* We can use the cpu mem copy function because this is X86. */ 1198 vaddr = io_mapping_map_atomic_wc(mapping, base); 1199 unwritten = __copy_to_user_inatomic(user_data, 1200 (void __force *)vaddr + offset, 1201 length); 1202 io_mapping_unmap_atomic(vaddr); 1203 if (unwritten) { 1204 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); 1205 unwritten = copy_to_user(user_data, 1206 (void __force *)vaddr + offset, 1207 length); 1208 io_mapping_unmap(vaddr); 1209 } 1210 return unwritten; 1211 } 1212 #else 1213 static inline bool 1214 gtt_user_read(struct drm_i915_private *dev_priv, 1215 loff_t base, int offset, 1216 char __user *user_data, int length) 1217 { 1218 bus_space_handle_t bsh; 1219 void __iomem *vaddr; 1220 unsigned long unwritten; 1221 1222 /* We can use the cpu mem copy function because this is X86. */ 1223 agp_map_atomic(dev_priv->agph, base, &bsh); 1224 vaddr = bus_space_vaddr(dev_priv->bst, bsh); 1225 unwritten = __copy_to_user_inatomic(user_data, 1226 (void __force *)vaddr + offset, 1227 length); 1228 agp_unmap_atomic(dev_priv->agph, bsh); 1229 if (unwritten) { 1230 agp_map_subregion(dev_priv->agph, base, PAGE_SIZE, &bsh); 1231 vaddr = bus_space_vaddr(dev_priv->bst, bsh); 1232 unwritten = copy_to_user(user_data, 1233 (void __force *)vaddr + offset, 1234 length); 1235 agp_unmap_subregion(dev_priv->agph, bsh, PAGE_SIZE); 1236 } 1237 return unwritten; 1238 } 1239 #endif 1240 1241 static int 1242 i915_gem_gtt_pread(struct drm_i915_gem_object *obj, 1243 const struct drm_i915_gem_pread *args) 1244 { 1245 struct drm_i915_private *i915 = to_i915(obj->base.dev); 1246 struct i915_ggtt *ggtt = &i915->ggtt; 1247 struct drm_mm_node node; 1248 struct i915_vma *vma; 1249 void __user *user_data; 1250 u64 remain, offset; 1251 int ret; 1252 1253 ret = mutex_lock_interruptible(&i915->drm.struct_mutex); 1254 if (ret) 1255 return ret; 1256 1257 intel_runtime_pm_get(i915); 1258 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 1259 PIN_MAPPABLE | 1260 PIN_NONFAULT | 1261 PIN_NONBLOCK); 1262 if (!IS_ERR(vma)) { 1263 node.start = i915_ggtt_offset(vma); 1264 node.allocated = false; 1265 ret = i915_vma_put_fence(vma); 1266 if (ret) { 1267 i915_vma_unpin(vma); 1268 vma = ERR_PTR(ret); 1269 } 1270 } 1271 if (IS_ERR(vma)) { 1272 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); 1273 if (ret) 1274 goto out_unlock; 1275 GEM_BUG_ON(!node.allocated); 1276 } 1277 1278 ret = i915_gem_object_set_to_gtt_domain(obj, false); 1279 if (ret) 1280 goto out_unpin; 1281 1282 mutex_unlock(&i915->drm.struct_mutex); 1283 1284 user_data = u64_to_user_ptr(args->data_ptr); 1285 remain = args->size; 1286 offset = args->offset; 1287 1288 while (remain > 0) { 1289 /* Operation in this page 1290 * 1291 * page_base = page offset within aperture 1292 * page_offset = offset within page 1293 * page_length = bytes to copy for this page 1294 */ 1295 u32 page_base = node.start; 1296 unsigned page_offset = offset_in_page(offset); 1297 unsigned page_length = PAGE_SIZE - page_offset; 1298 page_length = remain < page_length ? remain : page_length; 1299 if (node.allocated) { 1300 wmb(); 1301 ggtt->vm.insert_page(&ggtt->vm, 1302 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), 1303 node.start, I915_CACHE_NONE, 0); 1304 wmb(); 1305 } else { 1306 page_base += offset & ~PAGE_MASK; 1307 } 1308 1309 if (gtt_user_read(i915, page_base, page_offset, 1310 user_data, page_length)) { 1311 ret = -EFAULT; 1312 break; 1313 } 1314 1315 remain -= page_length; 1316 user_data += page_length; 1317 offset += page_length; 1318 } 1319 1320 mutex_lock(&i915->drm.struct_mutex); 1321 out_unpin: 1322 if (node.allocated) { 1323 wmb(); 1324 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); 1325 remove_mappable_node(&node); 1326 } else { 1327 i915_vma_unpin(vma); 1328 } 1329 out_unlock: 1330 intel_runtime_pm_put(i915); 1331 mutex_unlock(&i915->drm.struct_mutex); 1332 1333 return ret; 1334 } 1335 1336 /** 1337 * Reads data from the object referenced by handle. 1338 * @dev: drm device pointer 1339 * @data: ioctl data blob 1340 * @file: drm file pointer 1341 * 1342 * On error, the contents of *data are undefined. 1343 */ 1344 int 1345 i915_gem_pread_ioctl(struct drm_device *dev, void *data, 1346 struct drm_file *file) 1347 { 1348 struct drm_i915_gem_pread *args = data; 1349 struct drm_i915_gem_object *obj; 1350 int ret; 1351 1352 if (args->size == 0) 1353 return 0; 1354 1355 if (!access_ok(VERIFY_WRITE, 1356 u64_to_user_ptr(args->data_ptr), 1357 args->size)) 1358 return -EFAULT; 1359 1360 obj = i915_gem_object_lookup(file, args->handle); 1361 if (!obj) 1362 return -ENOENT; 1363 1364 /* Bounds check source. */ 1365 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { 1366 ret = -EINVAL; 1367 goto out; 1368 } 1369 1370 trace_i915_gem_object_pread(obj, args->offset, args->size); 1371 1372 ret = i915_gem_object_wait(obj, 1373 I915_WAIT_INTERRUPTIBLE, 1374 MAX_SCHEDULE_TIMEOUT, 1375 to_rps_client(file)); 1376 if (ret) 1377 goto out; 1378 1379 ret = i915_gem_object_pin_pages(obj); 1380 if (ret) 1381 goto out; 1382 1383 ret = i915_gem_shmem_pread(obj, args); 1384 if (ret == -EFAULT || ret == -ENODEV) 1385 ret = i915_gem_gtt_pread(obj, args); 1386 1387 i915_gem_object_unpin_pages(obj); 1388 out: 1389 i915_gem_object_put(obj); 1390 return ret; 1391 } 1392 1393 /* This is the fast write path which cannot handle 1394 * page faults in the source data 1395 */ 1396 #ifdef __linux__ 1397 static inline bool 1398 ggtt_write(struct io_mapping *mapping, 1399 loff_t base, int offset, 1400 char __user *user_data, int length) 1401 { 1402 void __iomem *vaddr; 1403 unsigned long unwritten; 1404 1405 /* We can use the cpu mem copy function because this is X86. */ 1406 vaddr = io_mapping_map_atomic_wc(mapping, base); 1407 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, 1408 user_data, length); 1409 io_mapping_unmap_atomic(vaddr); 1410 if (unwritten) { 1411 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); 1412 unwritten = copy_from_user((void __force *)vaddr + offset, 1413 user_data, length); 1414 io_mapping_unmap(vaddr); 1415 } 1416 1417 return unwritten; 1418 } 1419 #else 1420 static inline bool 1421 ggtt_write(struct drm_i915_private *dev_priv, 1422 loff_t base, int offset, 1423 char __user *user_data, int length) 1424 { 1425 bus_space_handle_t bsh; 1426 void __iomem *vaddr; 1427 unsigned long unwritten; 1428 1429 /* We can use the cpu mem copy function because this is X86. */ 1430 agp_map_atomic(dev_priv->agph, base, &bsh); 1431 vaddr = bus_space_vaddr(dev_priv->bst, bsh); 1432 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, 1433 user_data, length); 1434 agp_unmap_atomic(dev_priv->agph, bsh); 1435 if (unwritten) { 1436 agp_map_subregion(dev_priv->agph, base, PAGE_SIZE, &bsh); 1437 vaddr = bus_space_vaddr(dev_priv->bst, bsh); 1438 unwritten = copy_from_user((void __force *)vaddr + offset, 1439 user_data, length); 1440 agp_unmap_subregion(dev_priv->agph, bsh, PAGE_SIZE); 1441 } 1442 1443 return unwritten; 1444 } 1445 #endif 1446 1447 /** 1448 * This is the fast pwrite path, where we copy the data directly from the 1449 * user into the GTT, uncached. 1450 * @obj: i915 GEM object 1451 * @args: pwrite arguments structure 1452 */ 1453 static int 1454 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, 1455 const struct drm_i915_gem_pwrite *args) 1456 { 1457 struct drm_i915_private *i915 = to_i915(obj->base.dev); 1458 struct i915_ggtt *ggtt = &i915->ggtt; 1459 struct drm_mm_node node; 1460 struct i915_vma *vma; 1461 u64 remain, offset; 1462 void __user *user_data; 1463 int ret; 1464 1465 ret = mutex_lock_interruptible(&i915->drm.struct_mutex); 1466 if (ret) 1467 return ret; 1468 1469 if (i915_gem_object_has_struct_page(obj)) { 1470 /* 1471 * Avoid waking the device up if we can fallback, as 1472 * waking/resuming is very slow (worst-case 10-100 ms 1473 * depending on PCI sleeps and our own resume time). 1474 * This easily dwarfs any performance advantage from 1475 * using the cache bypass of indirect GGTT access. 1476 */ 1477 if (!intel_runtime_pm_get_if_in_use(i915)) { 1478 ret = -EFAULT; 1479 goto out_unlock; 1480 } 1481 } else { 1482 /* No backing pages, no fallback, we must force GGTT access */ 1483 intel_runtime_pm_get(i915); 1484 } 1485 1486 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 1487 PIN_MAPPABLE | 1488 PIN_NONFAULT | 1489 PIN_NONBLOCK); 1490 if (!IS_ERR(vma)) { 1491 node.start = i915_ggtt_offset(vma); 1492 node.allocated = false; 1493 ret = i915_vma_put_fence(vma); 1494 if (ret) { 1495 i915_vma_unpin(vma); 1496 vma = ERR_PTR(ret); 1497 } 1498 } 1499 if (IS_ERR(vma)) { 1500 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); 1501 if (ret) 1502 goto out_rpm; 1503 GEM_BUG_ON(!node.allocated); 1504 } 1505 1506 ret = i915_gem_object_set_to_gtt_domain(obj, true); 1507 if (ret) 1508 goto out_unpin; 1509 1510 mutex_unlock(&i915->drm.struct_mutex); 1511 1512 intel_fb_obj_invalidate(obj, ORIGIN_CPU); 1513 1514 user_data = u64_to_user_ptr(args->data_ptr); 1515 offset = args->offset; 1516 remain = args->size; 1517 while (remain) { 1518 /* Operation in this page 1519 * 1520 * page_base = page offset within aperture 1521 * page_offset = offset within page 1522 * page_length = bytes to copy for this page 1523 */ 1524 u32 page_base = node.start; 1525 unsigned int page_offset = offset_in_page(offset); 1526 unsigned int page_length = PAGE_SIZE - page_offset; 1527 page_length = remain < page_length ? remain : page_length; 1528 if (node.allocated) { 1529 wmb(); /* flush the write before we modify the GGTT */ 1530 ggtt->vm.insert_page(&ggtt->vm, 1531 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), 1532 node.start, I915_CACHE_NONE, 0); 1533 wmb(); /* flush modifications to the GGTT (insert_page) */ 1534 } else { 1535 page_base += offset & ~PAGE_MASK; 1536 } 1537 /* If we get a fault while copying data, then (presumably) our 1538 * source page isn't available. Return the error and we'll 1539 * retry in the slow path. 1540 * If the object is non-shmem backed, we retry again with the 1541 * path that handles page fault. 1542 */ 1543 if (ggtt_write(i915, page_base, page_offset, 1544 user_data, page_length)) { 1545 ret = -EFAULT; 1546 break; 1547 } 1548 1549 remain -= page_length; 1550 user_data += page_length; 1551 offset += page_length; 1552 } 1553 intel_fb_obj_flush(obj, ORIGIN_CPU); 1554 1555 mutex_lock(&i915->drm.struct_mutex); 1556 out_unpin: 1557 if (node.allocated) { 1558 wmb(); 1559 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); 1560 remove_mappable_node(&node); 1561 } else { 1562 i915_vma_unpin(vma); 1563 } 1564 out_rpm: 1565 intel_runtime_pm_put(i915); 1566 out_unlock: 1567 mutex_unlock(&i915->drm.struct_mutex); 1568 return ret; 1569 } 1570 1571 static int 1572 shmem_pwrite_slow(struct vm_page *page, int offset, int length, 1573 char __user *user_data, 1574 bool page_do_bit17_swizzling, 1575 bool needs_clflush_before, 1576 bool needs_clflush_after) 1577 { 1578 char *vaddr; 1579 int ret; 1580 1581 vaddr = kmap(page); 1582 if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) 1583 shmem_clflush_swizzled_range(vaddr + offset, length, 1584 page_do_bit17_swizzling); 1585 if (page_do_bit17_swizzling) 1586 ret = __copy_from_user_swizzled(vaddr, offset, user_data, 1587 length); 1588 else 1589 ret = __copy_from_user(vaddr + offset, user_data, length); 1590 if (needs_clflush_after) 1591 shmem_clflush_swizzled_range(vaddr + offset, length, 1592 page_do_bit17_swizzling); 1593 kunmap(vaddr); 1594 1595 return ret ? -EFAULT : 0; 1596 } 1597 1598 /* Per-page copy function for the shmem pwrite fastpath. 1599 * Flushes invalid cachelines before writing to the target if 1600 * needs_clflush_before is set and flushes out any written cachelines after 1601 * writing if needs_clflush is set. 1602 */ 1603 static int 1604 shmem_pwrite(struct vm_page *page, int offset, int len, char __user *user_data, 1605 bool page_do_bit17_swizzling, 1606 bool needs_clflush_before, 1607 bool needs_clflush_after) 1608 { 1609 int ret; 1610 1611 ret = -ENODEV; 1612 if (!page_do_bit17_swizzling) { 1613 char *vaddr = kmap_atomic(page); 1614 1615 if (needs_clflush_before) 1616 drm_clflush_virt_range(vaddr + offset, len); 1617 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); 1618 if (needs_clflush_after) 1619 drm_clflush_virt_range(vaddr + offset, len); 1620 1621 kunmap_atomic(vaddr); 1622 } 1623 if (ret == 0) 1624 return ret; 1625 1626 return shmem_pwrite_slow(page, offset, len, user_data, 1627 page_do_bit17_swizzling, 1628 needs_clflush_before, 1629 needs_clflush_after); 1630 } 1631 1632 static int 1633 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, 1634 const struct drm_i915_gem_pwrite *args) 1635 { 1636 struct drm_i915_private *i915 = to_i915(obj->base.dev); 1637 void __user *user_data; 1638 u64 remain; 1639 unsigned int obj_do_bit17_swizzling; 1640 unsigned int partial_cacheline_write; 1641 unsigned int needs_clflush; 1642 unsigned int offset, idx; 1643 int ret; 1644 1645 ret = mutex_lock_interruptible(&i915->drm.struct_mutex); 1646 if (ret) 1647 return ret; 1648 1649 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); 1650 mutex_unlock(&i915->drm.struct_mutex); 1651 if (ret) 1652 return ret; 1653 1654 obj_do_bit17_swizzling = 0; 1655 if (i915_gem_object_needs_bit17_swizzle(obj)) 1656 obj_do_bit17_swizzling = BIT(17); 1657 1658 /* If we don't overwrite a cacheline completely we need to be 1659 * careful to have up-to-date data by first clflushing. Don't 1660 * overcomplicate things and flush the entire patch. 1661 */ 1662 partial_cacheline_write = 0; 1663 if (needs_clflush & CLFLUSH_BEFORE) 1664 partial_cacheline_write = curcpu()->ci_cflushsz - 1; 1665 1666 user_data = u64_to_user_ptr(args->data_ptr); 1667 remain = args->size; 1668 offset = offset_in_page(args->offset); 1669 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { 1670 struct vm_page *page = i915_gem_object_get_page(obj, idx); 1671 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); 1672 1673 ret = shmem_pwrite(page, offset, length, user_data, 1674 page_to_phys(page) & obj_do_bit17_swizzling, 1675 (offset | length) & partial_cacheline_write, 1676 needs_clflush & CLFLUSH_AFTER); 1677 if (ret) 1678 break; 1679 1680 remain -= length; 1681 user_data += length; 1682 offset = 0; 1683 } 1684 1685 intel_fb_obj_flush(obj, ORIGIN_CPU); 1686 i915_gem_obj_finish_shmem_access(obj); 1687 return ret; 1688 } 1689 1690 /** 1691 * Writes data to the object referenced by handle. 1692 * @dev: drm device 1693 * @data: ioctl data blob 1694 * @file: drm file 1695 * 1696 * On error, the contents of the buffer that were to be modified are undefined. 1697 */ 1698 int 1699 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, 1700 struct drm_file *file) 1701 { 1702 struct drm_i915_gem_pwrite *args = data; 1703 struct drm_i915_gem_object *obj; 1704 int ret; 1705 1706 if (args->size == 0) 1707 return 0; 1708 1709 if (!access_ok(VERIFY_READ, 1710 u64_to_user_ptr(args->data_ptr), 1711 args->size)) 1712 return -EFAULT; 1713 1714 obj = i915_gem_object_lookup(file, args->handle); 1715 if (!obj) 1716 return -ENOENT; 1717 1718 /* Bounds check destination. */ 1719 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { 1720 ret = -EINVAL; 1721 goto err; 1722 } 1723 1724 /* Writes not allowed into this read-only object */ 1725 if (i915_gem_object_is_readonly(obj)) { 1726 ret = -EINVAL; 1727 goto err; 1728 } 1729 1730 trace_i915_gem_object_pwrite(obj, args->offset, args->size); 1731 1732 ret = -ENODEV; 1733 if (obj->ops->pwrite) 1734 ret = obj->ops->pwrite(obj, args); 1735 if (ret != -ENODEV) 1736 goto err; 1737 1738 ret = i915_gem_object_wait(obj, 1739 I915_WAIT_INTERRUPTIBLE | 1740 I915_WAIT_ALL, 1741 MAX_SCHEDULE_TIMEOUT, 1742 to_rps_client(file)); 1743 if (ret) 1744 goto err; 1745 1746 ret = i915_gem_object_pin_pages(obj); 1747 if (ret) 1748 goto err; 1749 1750 ret = -EFAULT; 1751 /* We can only do the GTT pwrite on untiled buffers, as otherwise 1752 * it would end up going through the fenced access, and we'll get 1753 * different detiling behavior between reading and writing. 1754 * pread/pwrite currently are reading and writing from the CPU 1755 * perspective, requiring manual detiling by the client. 1756 */ 1757 if (!i915_gem_object_has_struct_page(obj) || 1758 cpu_write_needs_clflush(obj)) 1759 /* Note that the gtt paths might fail with non-page-backed user 1760 * pointers (e.g. gtt mappings when moving data between 1761 * textures). Fallback to the shmem path in that case. 1762 */ 1763 ret = i915_gem_gtt_pwrite_fast(obj, args); 1764 1765 if (ret == -EFAULT || ret == -ENOSPC) { 1766 if (obj->phys_handle) 1767 ret = i915_gem_phys_pwrite(obj, args, file); 1768 else 1769 ret = i915_gem_shmem_pwrite(obj, args); 1770 } 1771 1772 i915_gem_object_unpin_pages(obj); 1773 err: 1774 i915_gem_object_put(obj); 1775 return ret; 1776 } 1777 1778 static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) 1779 { 1780 struct drm_i915_private *i915; 1781 struct list_head *list; 1782 struct i915_vma *vma; 1783 1784 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 1785 1786 for_each_ggtt_vma(vma, obj) { 1787 if (i915_vma_is_active(vma)) 1788 continue; 1789 1790 if (!drm_mm_node_allocated(&vma->node)) 1791 continue; 1792 1793 list_move_tail(&vma->vm_link, &vma->vm->inactive_list); 1794 } 1795 1796 i915 = to_i915(obj->base.dev); 1797 spin_lock(&i915->mm.obj_lock); 1798 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; 1799 list_move_tail(&obj->mm.link, list); 1800 spin_unlock(&i915->mm.obj_lock); 1801 } 1802 1803 /** 1804 * Called when user space prepares to use an object with the CPU, either 1805 * through the mmap ioctl's mapping or a GTT mapping. 1806 * @dev: drm device 1807 * @data: ioctl data blob 1808 * @file: drm file 1809 */ 1810 int 1811 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, 1812 struct drm_file *file) 1813 { 1814 struct drm_i915_gem_set_domain *args = data; 1815 struct drm_i915_gem_object *obj; 1816 uint32_t read_domains = args->read_domains; 1817 uint32_t write_domain = args->write_domain; 1818 int err; 1819 1820 /* Only handle setting domains to types used by the CPU. */ 1821 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) 1822 return -EINVAL; 1823 1824 /* Having something in the write domain implies it's in the read 1825 * domain, and only that read domain. Enforce that in the request. 1826 */ 1827 if (write_domain != 0 && read_domains != write_domain) 1828 return -EINVAL; 1829 1830 obj = i915_gem_object_lookup(file, args->handle); 1831 if (!obj) 1832 return -ENOENT; 1833 1834 /* Try to flush the object off the GPU without holding the lock. 1835 * We will repeat the flush holding the lock in the normal manner 1836 * to catch cases where we are gazumped. 1837 */ 1838 err = i915_gem_object_wait(obj, 1839 I915_WAIT_INTERRUPTIBLE | 1840 (write_domain ? I915_WAIT_ALL : 0), 1841 MAX_SCHEDULE_TIMEOUT, 1842 to_rps_client(file)); 1843 if (err) 1844 goto out; 1845 1846 /* 1847 * Proxy objects do not control access to the backing storage, ergo 1848 * they cannot be used as a means to manipulate the cache domain 1849 * tracking for that backing storage. The proxy object is always 1850 * considered to be outside of any cache domain. 1851 */ 1852 if (i915_gem_object_is_proxy(obj)) { 1853 err = -ENXIO; 1854 goto out; 1855 } 1856 1857 /* 1858 * Flush and acquire obj->pages so that we are coherent through 1859 * direct access in memory with previous cached writes through 1860 * shmemfs and that our cache domain tracking remains valid. 1861 * For example, if the obj->filp was moved to swap without us 1862 * being notified and releasing the pages, we would mistakenly 1863 * continue to assume that the obj remained out of the CPU cached 1864 * domain. 1865 */ 1866 err = i915_gem_object_pin_pages(obj); 1867 if (err) 1868 goto out; 1869 1870 err = i915_mutex_lock_interruptible(dev); 1871 if (err) 1872 goto out_unpin; 1873 1874 if (read_domains & I915_GEM_DOMAIN_WC) 1875 err = i915_gem_object_set_to_wc_domain(obj, write_domain); 1876 else if (read_domains & I915_GEM_DOMAIN_GTT) 1877 err = i915_gem_object_set_to_gtt_domain(obj, write_domain); 1878 else 1879 err = i915_gem_object_set_to_cpu_domain(obj, write_domain); 1880 1881 /* And bump the LRU for this access */ 1882 i915_gem_object_bump_inactive_ggtt(obj); 1883 1884 mutex_unlock(&dev->struct_mutex); 1885 1886 if (write_domain != 0) 1887 intel_fb_obj_invalidate(obj, 1888 fb_write_origin(obj, write_domain)); 1889 1890 out_unpin: 1891 i915_gem_object_unpin_pages(obj); 1892 out: 1893 i915_gem_object_put(obj); 1894 return err; 1895 } 1896 1897 /** 1898 * Called when user space has done writes to this buffer 1899 * @dev: drm device 1900 * @data: ioctl data blob 1901 * @file: drm file 1902 */ 1903 int 1904 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, 1905 struct drm_file *file) 1906 { 1907 struct drm_i915_gem_sw_finish *args = data; 1908 struct drm_i915_gem_object *obj; 1909 1910 obj = i915_gem_object_lookup(file, args->handle); 1911 if (!obj) 1912 return -ENOENT; 1913 1914 /* 1915 * Proxy objects are barred from CPU access, so there is no 1916 * need to ban sw_finish as it is a nop. 1917 */ 1918 1919 /* Pinned buffers may be scanout, so flush the cache */ 1920 i915_gem_object_flush_if_display(obj); 1921 i915_gem_object_put(obj); 1922 1923 return 0; 1924 } 1925 1926 #ifdef __linux__ 1927 static inline bool 1928 __vma_matches(struct vm_area_struct *vma, struct file *filp, 1929 unsigned long addr, unsigned long size) 1930 { 1931 if (vma->vm_file != filp) 1932 return false; 1933 1934 return vma->vm_start == addr && 1935 (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size); 1936 } 1937 #endif 1938 1939 /** 1940 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address 1941 * it is mapped to. 1942 * @dev: drm device 1943 * @data: ioctl data blob 1944 * @file: drm file 1945 * 1946 * While the mapping holds a reference on the contents of the object, it doesn't 1947 * imply a ref on the object itself. 1948 * 1949 * IMPORTANT: 1950 * 1951 * DRM driver writers who look a this function as an example for how to do GEM 1952 * mmap support, please don't implement mmap support like here. The modern way 1953 * to implement DRM mmap support is with an mmap offset ioctl (like 1954 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. 1955 * That way debug tooling like valgrind will understand what's going on, hiding 1956 * the mmap call in a driver private ioctl will break that. The i915 driver only 1957 * does cpu mmaps this way because we didn't know better. 1958 */ 1959 int 1960 i915_gem_mmap_ioctl(struct drm_device *dev, void *data, 1961 struct drm_file *file) 1962 { 1963 struct drm_i915_gem_mmap *args = data; 1964 struct drm_i915_gem_object *obj; 1965 vaddr_t addr; 1966 vsize_t size; 1967 int ret; 1968 1969 #ifdef __OpenBSD__ 1970 if (args->size == 0 || args->offset & PAGE_MASK) 1971 return -EINVAL; 1972 size = round_page(args->size); 1973 if (args->offset + size < args->offset) 1974 return -EINVAL; 1975 #endif 1976 1977 if (args->flags & ~(I915_MMAP_WC)) 1978 return -EINVAL; 1979 1980 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) 1981 return -ENODEV; 1982 1983 obj = i915_gem_object_lookup(file, args->handle); 1984 if (!obj) 1985 return -ENOENT; 1986 1987 /* prime objects have no backing filp to GEM mmap 1988 * pages from. 1989 */ 1990 if (!obj->base.filp) { 1991 addr = -ENXIO; 1992 goto err; 1993 } 1994 1995 if (range_overflows(args->offset, args->size, (u64)obj->base.size)) { 1996 addr = -EINVAL; 1997 goto err; 1998 } 1999 2000 #ifdef __linux__ 2001 addr = vm_mmap(obj->base.filp, 0, args->size, 2002 PROT_READ | PROT_WRITE, MAP_SHARED, 2003 args->offset); 2004 if (IS_ERR_VALUE(addr)) 2005 goto err; 2006 2007 if (args->flags & I915_MMAP_WC) { 2008 struct mm_struct *mm = current->mm; 2009 struct vm_area_struct *vma; 2010 2011 if (down_write_killable(&mm->mmap_sem)) { 2012 addr = -EINTR; 2013 goto err; 2014 } 2015 vma = find_vma(mm, addr); 2016 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) 2017 vma->vm_page_prot = 2018 pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 2019 else 2020 addr = -ENOMEM; 2021 up_write(&mm->mmap_sem); 2022 if (IS_ERR_VALUE(addr)) 2023 goto err; 2024 2025 /* This may race, but that's ok, it only gets set */ 2026 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); 2027 } 2028 i915_gem_object_put(obj); 2029 #else 2030 addr = 0; 2031 ret = -uvm_map(&curproc->p_vmspace->vm_map, &addr, size, 2032 obj->base.uao, args->offset, 0, UVM_MAPFLAG(PROT_READ | PROT_WRITE, 2033 PROT_READ | PROT_WRITE, MAP_INHERIT_SHARE, MADV_RANDOM, 2034 (args->flags & I915_MMAP_WC) ? UVM_FLAG_WC : 0)); 2035 if (args->flags & I915_MMAP_WC) { 2036 /* This may race, but that's ok, it only gets set */ 2037 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); 2038 } 2039 if (ret == 0) 2040 uao_reference(obj->base.uao); 2041 i915_gem_object_put(obj); 2042 if (ret) 2043 return ret; 2044 #endif 2045 2046 args->addr_ptr = (uint64_t) addr; 2047 return 0; 2048 2049 err: 2050 i915_gem_object_put(obj); 2051 return addr; 2052 } 2053 2054 static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) 2055 { 2056 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; 2057 } 2058 2059 /** 2060 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps 2061 * 2062 * A history of the GTT mmap interface: 2063 * 2064 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to 2065 * aligned and suitable for fencing, and still fit into the available 2066 * mappable space left by the pinned display objects. A classic problem 2067 * we called the page-fault-of-doom where we would ping-pong between 2068 * two objects that could not fit inside the GTT and so the memcpy 2069 * would page one object in at the expense of the other between every 2070 * single byte. 2071 * 2072 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none 2073 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the 2074 * object is too large for the available space (or simply too large 2075 * for the mappable aperture!), a view is created instead and faulted 2076 * into userspace. (This view is aligned and sized appropriately for 2077 * fenced access.) 2078 * 2079 * 2 - Recognise WC as a separate cache domain so that we can flush the 2080 * delayed writes via GTT before performing direct access via WC. 2081 * 2082 * Restrictions: 2083 * 2084 * * snoopable objects cannot be accessed via the GTT. It can cause machine 2085 * hangs on some architectures, corruption on others. An attempt to service 2086 * a GTT page fault from a snoopable object will generate a SIGBUS. 2087 * 2088 * * the object must be able to fit into RAM (physical memory, though no 2089 * limited to the mappable aperture). 2090 * 2091 * 2092 * Caveats: 2093 * 2094 * * a new GTT page fault will synchronize rendering from the GPU and flush 2095 * all data to system memory. Subsequent access will not be synchronized. 2096 * 2097 * * all mappings are revoked on runtime device suspend. 2098 * 2099 * * there are only 8, 16 or 32 fence registers to share between all users 2100 * (older machines require fence register for display and blitter access 2101 * as well). Contention of the fence registers will cause the previous users 2102 * to be unmapped and any new access will generate new page faults. 2103 * 2104 * * running out of memory while servicing a fault may generate a SIGBUS, 2105 * rather than the expected SIGSEGV. 2106 */ 2107 int i915_gem_mmap_gtt_version(void) 2108 { 2109 return 2; 2110 } 2111 2112 static inline struct i915_ggtt_view 2113 compute_partial_view(struct drm_i915_gem_object *obj, 2114 pgoff_t page_offset, 2115 unsigned int chunk) 2116 { 2117 struct i915_ggtt_view view; 2118 2119 if (i915_gem_object_is_tiled(obj)) 2120 chunk = roundup(chunk, tile_row_pages(obj)); 2121 2122 view.type = I915_GGTT_VIEW_PARTIAL; 2123 view.partial.offset = rounddown(page_offset, chunk); 2124 view.partial.size = 2125 min_t(unsigned int, chunk, 2126 (obj->base.size >> PAGE_SHIFT) - view.partial.offset); 2127 2128 /* If the partial covers the entire object, just create a normal VMA. */ 2129 if (chunk >= obj->base.size >> PAGE_SHIFT) 2130 view.type = I915_GGTT_VIEW_NORMAL; 2131 2132 return view; 2133 } 2134 2135 #ifdef __linux__ 2136 2137 /** 2138 * i915_gem_fault - fault a page into the GTT 2139 * @vmf: fault info 2140 * 2141 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped 2142 * from userspace. The fault handler takes care of binding the object to 2143 * the GTT (if needed), allocating and programming a fence register (again, 2144 * only if needed based on whether the old reg is still valid or the object 2145 * is tiled) and inserting a new PTE into the faulting process. 2146 * 2147 * Note that the faulting process may involve evicting existing objects 2148 * from the GTT and/or fence registers to make room. So performance may 2149 * suffer if the GTT working set is large or there are few fence registers 2150 * left. 2151 * 2152 * The current feature set supported by i915_gem_fault() and thus GTT mmaps 2153 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version). 2154 */ 2155 vm_fault_t i915_gem_fault(struct vm_fault *vmf) 2156 { 2157 #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) 2158 struct vm_area_struct *area = vmf->vma; 2159 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data); 2160 struct drm_device *dev = obj->base.dev; 2161 struct drm_i915_private *dev_priv = to_i915(dev); 2162 struct i915_ggtt *ggtt = &dev_priv->ggtt; 2163 bool write = !!(vmf->flags & FAULT_FLAG_WRITE); 2164 struct i915_vma *vma; 2165 pgoff_t page_offset; 2166 int ret; 2167 2168 /* Sanity check that we allow writing into this object */ 2169 if (i915_gem_object_is_readonly(obj) && write) 2170 return VM_FAULT_SIGBUS; 2171 2172 /* We don't use vmf->pgoff since that has the fake offset */ 2173 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; 2174 2175 trace_i915_gem_object_fault(obj, page_offset, true, write); 2176 2177 /* Try to flush the object off the GPU first without holding the lock. 2178 * Upon acquiring the lock, we will perform our sanity checks and then 2179 * repeat the flush holding the lock in the normal manner to catch cases 2180 * where we are gazumped. 2181 */ 2182 ret = i915_gem_object_wait(obj, 2183 I915_WAIT_INTERRUPTIBLE, 2184 MAX_SCHEDULE_TIMEOUT, 2185 NULL); 2186 if (ret) 2187 goto err; 2188 2189 ret = i915_gem_object_pin_pages(obj); 2190 if (ret) 2191 goto err; 2192 2193 intel_runtime_pm_get(dev_priv); 2194 2195 ret = i915_mutex_lock_interruptible(dev); 2196 if (ret) 2197 goto err_rpm; 2198 2199 /* Access to snoopable pages through the GTT is incoherent. */ 2200 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { 2201 ret = -EFAULT; 2202 goto err_unlock; 2203 } 2204 2205 2206 /* Now pin it into the GTT as needed */ 2207 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 2208 PIN_MAPPABLE | 2209 PIN_NONBLOCK | 2210 PIN_NONFAULT); 2211 if (IS_ERR(vma)) { 2212 /* Use a partial view if it is bigger than available space */ 2213 struct i915_ggtt_view view = 2214 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); 2215 unsigned int flags; 2216 2217 flags = PIN_MAPPABLE; 2218 if (view.type == I915_GGTT_VIEW_NORMAL) 2219 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ 2220 2221 /* 2222 * Userspace is now writing through an untracked VMA, abandon 2223 * all hope that the hardware is able to track future writes. 2224 */ 2225 obj->frontbuffer_ggtt_origin = ORIGIN_CPU; 2226 2227 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); 2228 if (IS_ERR(vma) && !view.type) { 2229 flags = PIN_MAPPABLE; 2230 view.type = I915_GGTT_VIEW_PARTIAL; 2231 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); 2232 } 2233 } 2234 if (IS_ERR(vma)) { 2235 ret = PTR_ERR(vma); 2236 goto err_unlock; 2237 } 2238 2239 ret = i915_gem_object_set_to_gtt_domain(obj, write); 2240 if (ret) 2241 goto err_unpin; 2242 2243 ret = i915_vma_pin_fence(vma); 2244 if (ret) 2245 goto err_unpin; 2246 2247 /* Finally, remap it using the new GTT offset */ 2248 ret = remap_io_mapping(area, 2249 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), 2250 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, 2251 min_t(u64, vma->size, area->vm_end - area->vm_start), 2252 &ggtt->iomap); 2253 if (ret) 2254 goto err_fence; 2255 2256 /* Mark as being mmapped into userspace for later revocation */ 2257 assert_rpm_wakelock_held(dev_priv); 2258 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) 2259 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); 2260 GEM_BUG_ON(!obj->userfault_count); 2261 2262 i915_vma_set_ggtt_write(vma); 2263 2264 err_fence: 2265 i915_vma_unpin_fence(vma); 2266 err_unpin: 2267 __i915_vma_unpin(vma); 2268 err_unlock: 2269 mutex_unlock(&dev->struct_mutex); 2270 err_rpm: 2271 intel_runtime_pm_put(dev_priv); 2272 i915_gem_object_unpin_pages(obj); 2273 err: 2274 switch (ret) { 2275 case -EIO: 2276 /* 2277 * We eat errors when the gpu is terminally wedged to avoid 2278 * userspace unduly crashing (gl has no provisions for mmaps to 2279 * fail). But any other -EIO isn't ours (e.g. swap in failure) 2280 * and so needs to be reported. 2281 */ 2282 if (!i915_terminally_wedged(&dev_priv->gpu_error)) 2283 return VM_FAULT_SIGBUS; 2284 /* else: fall through */ 2285 case -EAGAIN: 2286 /* 2287 * EAGAIN means the gpu is hung and we'll wait for the error 2288 * handler to reset everything when re-faulting in 2289 * i915_mutex_lock_interruptible. 2290 */ 2291 case 0: 2292 case -ERESTARTSYS: 2293 case -EINTR: 2294 case -EBUSY: 2295 /* 2296 * EBUSY is ok: this just means that another thread 2297 * already did the job. 2298 */ 2299 return VM_FAULT_NOPAGE; 2300 case -ENOMEM: 2301 return VM_FAULT_OOM; 2302 case -ENOSPC: 2303 case -EFAULT: 2304 return VM_FAULT_SIGBUS; 2305 default: 2306 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); 2307 return VM_FAULT_SIGBUS; 2308 } 2309 } 2310 2311 #else 2312 2313 int 2314 i915_gem_fault(struct drm_gem_object *gem_obj, struct uvm_faultinfo *ufi, 2315 off_t offset, vaddr_t vaddr, vm_page_t *pps, int npages, int centeridx, 2316 vm_prot_t access_type, int flags) 2317 { 2318 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); 2319 struct drm_device *dev = obj->base.dev; 2320 struct drm_i915_private *dev_priv = dev->dev_private; 2321 struct i915_ggtt *ggtt = &dev_priv->ggtt; 2322 paddr_t paddr; 2323 int lcv, ret = 0; 2324 int write = !!(access_type & PROT_WRITE); 2325 struct i915_vma *vma; 2326 vm_prot_t mapprot; 2327 boolean_t locked = TRUE; 2328 2329 /* Sanity check that we allow writing into this object */ 2330 if (i915_gem_object_is_readonly(obj) && write) { 2331 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, 2332 &obj->base.uobj, NULL); 2333 return VM_PAGER_BAD; 2334 } 2335 2336 /* 2337 * If we already own the lock, we must be doing a copyin or 2338 * copyout in one of the fast paths. Return failure such that 2339 * we fall back on the slow path. 2340 */ 2341 if (!drm_vma_node_has_offset(&obj->base.vma_node) || 2342 RWLOCK_OWNER(&dev->struct_mutex) == curproc) { 2343 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, 2344 &obj->base.uobj, NULL); 2345 return VM_PAGER_BAD; 2346 } 2347 2348 offset -= drm_vma_node_offset_addr(&obj->base.vma_node); 2349 2350 if (!mutex_trylock(&dev->struct_mutex)) { 2351 uvmfault_unlockall(ufi, NULL, &obj->base.uobj, NULL); 2352 mutex_lock(&dev->struct_mutex); 2353 locked = uvmfault_relock(ufi); 2354 } 2355 if (!locked) { 2356 mutex_unlock(&dev->struct_mutex); 2357 return VM_PAGER_REFAULT; 2358 } 2359 2360 /* Try to flush the object off the GPU first without holding the lock. 2361 * Upon acquiring the lock, we will perform our sanity checks and then 2362 * repeat the flush holding the lock in the normal manner to catch cases 2363 * where we are gazumped. 2364 */ 2365 ret = i915_gem_object_wait(obj, 2366 I915_WAIT_INTERRUPTIBLE, 2367 MAX_SCHEDULE_TIMEOUT, 2368 NULL); 2369 if (ret) { 2370 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, NULL, NULL); 2371 mutex_unlock(&dev->struct_mutex); 2372 goto err; 2373 } 2374 2375 ret = i915_gem_object_pin_pages(obj); 2376 if (ret) { 2377 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, NULL, NULL); 2378 mutex_unlock(&dev->struct_mutex); 2379 goto err; 2380 } 2381 2382 intel_runtime_pm_get(dev_priv); 2383 2384 /* Access to snoopable pages through the GTT is incoherent. */ 2385 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { 2386 ret = -EINVAL; 2387 goto err_unlock; 2388 } 2389 2390 /* Now pin it into the GTT as needed */ 2391 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 2392 PIN_MAPPABLE | 2393 PIN_NONBLOCK | 2394 PIN_NONFAULT); 2395 if (IS_ERR(vma)) { 2396 /* 2397 * Userspace is now writing through an untracked VMA, abandon 2398 * all hope that the hardware is able to track future writes. 2399 */ 2400 obj->frontbuffer_ggtt_origin = ORIGIN_CPU; 2401 2402 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE); 2403 } 2404 if (IS_ERR(vma)) { 2405 ret = PTR_ERR(vma); 2406 goto err_unlock; 2407 } 2408 2409 ret = i915_gem_object_set_to_gtt_domain(obj, write); 2410 if (ret) 2411 goto err_unpin; 2412 2413 ret = i915_vma_pin_fence(vma); 2414 if (ret) 2415 goto err_unpin; 2416 2417 mapprot = ufi->entry->protection; 2418 /* 2419 * if it's only a read fault, we only put ourselves into the gtt 2420 * read domain, so make sure we fault again and set ourselves to write. 2421 * this prevents us needing userland to do domain management and get 2422 * it wrong, and makes us fully coherent with the gpu re mmap. 2423 */ 2424 if (write == 0) 2425 mapprot &= ~PROT_WRITE; 2426 /* XXX try and be more efficient when we do this */ 2427 for (lcv = 0 ; lcv < npages ; lcv++, offset += PAGE_SIZE, 2428 vaddr += PAGE_SIZE) { 2429 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx) 2430 continue; 2431 2432 if (pps[lcv] == PGO_DONTCARE) 2433 continue; 2434 2435 paddr = ggtt->gmadr.start + vma->node.start + offset; 2436 2437 if (pmap_enter(ufi->orig_map->pmap, vaddr, paddr, 2438 mapprot, PMAP_CANFAIL | mapprot) != 0) { 2439 i915_vma_unpin_fence(vma); 2440 __i915_vma_unpin(vma); 2441 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, 2442 NULL, NULL); 2443 mutex_unlock(&dev->struct_mutex); 2444 pmap_update(ufi->orig_map->pmap); 2445 uvm_wait("intelflt"); 2446 ret = VM_PAGER_REFAULT; 2447 intel_runtime_pm_put(dev_priv); 2448 i915_gem_object_unpin_pages(obj); 2449 return ret; 2450 } 2451 } 2452 2453 /* Mark as being mmapped into userspace for later revocation */ 2454 assert_rpm_wakelock_held(dev_priv); 2455 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) 2456 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); 2457 GEM_BUG_ON(!obj->userfault_count); 2458 2459 i915_vma_set_ggtt_write(vma); 2460 2461 #ifdef notyet 2462 err_fence: 2463 #endif 2464 i915_vma_unpin_fence(vma); 2465 err_unpin: 2466 __i915_vma_unpin(vma); 2467 err_unlock: 2468 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, NULL, NULL); 2469 mutex_unlock(&dev->struct_mutex); 2470 pmap_update(ufi->orig_map->pmap); 2471 #ifdef notyet 2472 err_rpm: 2473 #endif 2474 intel_runtime_pm_put(dev_priv); 2475 i915_gem_object_unpin_pages(obj); 2476 err: 2477 switch (ret) { 2478 case -EIO: 2479 /* 2480 * We eat errors when the gpu is terminally wedged to avoid 2481 * userspace unduly crashing (gl has no provisions for mmaps to 2482 * fail). But any other -EIO isn't ours (e.g. swap in failure) 2483 * and so needs to be reported. 2484 */ 2485 if (!i915_terminally_wedged(&dev_priv->gpu_error)) 2486 return VM_PAGER_ERROR; 2487 /* else: fall through */ 2488 case -EAGAIN: 2489 /* 2490 * EAGAIN means the gpu is hung and we'll wait for the error 2491 * handler to reset everything when re-faulting in 2492 * i915_mutex_lock_interruptible. 2493 */ 2494 case 0: 2495 case -ERESTART: 2496 case -EINTR: 2497 case -EBUSY: 2498 /* 2499 * EBUSY is ok: this just means that another thread 2500 * already did the job. 2501 */ 2502 return VM_PAGER_OK; 2503 case -ENOMEM: 2504 return VM_PAGER_ERROR; 2505 case -ENOSPC: 2506 case -EFAULT: 2507 return VM_PAGER_ERROR; 2508 default: 2509 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); 2510 return VM_PAGER_ERROR; 2511 } 2512 } 2513 2514 #endif 2515 2516 static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) 2517 { 2518 struct i915_vma *vma; 2519 2520 GEM_BUG_ON(!obj->userfault_count); 2521 2522 obj->userfault_count = 0; 2523 list_del(&obj->userfault_link); 2524 #ifdef __linux__ 2525 drm_vma_node_unmap(&obj->base.vma_node, 2526 obj->base.dev->anon_inode->i_mapping); 2527 #else 2528 if (drm_vma_node_has_offset(&obj->base.vma_node)) { 2529 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 2530 struct i915_vma *vma; 2531 struct vm_page *pg; 2532 2533 for_each_ggtt_vma(vma, obj) { 2534 for (pg = &dev_priv->pgs[atop(vma->node.start)]; 2535 pg != &dev_priv->pgs[atop(vma->node.start + vma->size)]; 2536 pg++) 2537 pmap_page_protect(pg, PROT_NONE); 2538 } 2539 } 2540 #endif 2541 2542 for_each_ggtt_vma(vma, obj) 2543 i915_vma_unset_userfault(vma); 2544 } 2545 2546 /** 2547 * i915_gem_release_mmap - remove physical page mappings 2548 * @obj: obj in question 2549 * 2550 * Preserve the reservation of the mmapping with the DRM core code, but 2551 * relinquish ownership of the pages back to the system. 2552 * 2553 * It is vital that we remove the page mapping if we have mapped a tiled 2554 * object through the GTT and then lose the fence register due to 2555 * resource pressure. Similarly if the object has been moved out of the 2556 * aperture, than pages mapped into userspace must be revoked. Removing the 2557 * mapping will then trigger a page fault on the next user access, allowing 2558 * fixup by i915_gem_fault(). 2559 */ 2560 void 2561 i915_gem_release_mmap(struct drm_i915_gem_object *obj) 2562 { 2563 struct drm_i915_private *i915 = to_i915(obj->base.dev); 2564 2565 /* Serialisation between user GTT access and our code depends upon 2566 * revoking the CPU's PTE whilst the mutex is held. The next user 2567 * pagefault then has to wait until we release the mutex. 2568 * 2569 * Note that RPM complicates somewhat by adding an additional 2570 * requirement that operations to the GGTT be made holding the RPM 2571 * wakeref. 2572 */ 2573 lockdep_assert_held(&i915->drm.struct_mutex); 2574 intel_runtime_pm_get(i915); 2575 2576 if (!obj->userfault_count) 2577 goto out; 2578 2579 __i915_gem_object_release_mmap(obj); 2580 2581 /* Ensure that the CPU's PTE are revoked and there are not outstanding 2582 * memory transactions from userspace before we return. The TLB 2583 * flushing implied above by changing the PTE above *should* be 2584 * sufficient, an extra barrier here just provides us with a bit 2585 * of paranoid documentation about our requirement to serialise 2586 * memory writes before touching registers / GSM. 2587 */ 2588 wmb(); 2589 2590 out: 2591 intel_runtime_pm_put(i915); 2592 } 2593 2594 void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) 2595 { 2596 struct drm_i915_gem_object *obj, *on; 2597 int i; 2598 2599 /* 2600 * Only called during RPM suspend. All users of the userfault_list 2601 * must be holding an RPM wakeref to ensure that this can not 2602 * run concurrently with themselves (and use the struct_mutex for 2603 * protection between themselves). 2604 */ 2605 2606 list_for_each_entry_safe(obj, on, 2607 &dev_priv->mm.userfault_list, userfault_link) 2608 __i915_gem_object_release_mmap(obj); 2609 2610 /* The fence will be lost when the device powers down. If any were 2611 * in use by hardware (i.e. they are pinned), we should not be powering 2612 * down! All other fences will be reacquired by the user upon waking. 2613 */ 2614 for (i = 0; i < dev_priv->num_fence_regs; i++) { 2615 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; 2616 2617 /* Ideally we want to assert that the fence register is not 2618 * live at this point (i.e. that no piece of code will be 2619 * trying to write through fence + GTT, as that both violates 2620 * our tracking of activity and associated locking/barriers, 2621 * but also is illegal given that the hw is powered down). 2622 * 2623 * Previously we used reg->pin_count as a "liveness" indicator. 2624 * That is not sufficient, and we need a more fine-grained 2625 * tool if we want to have a sanity check here. 2626 */ 2627 2628 if (!reg->vma) 2629 continue; 2630 2631 GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); 2632 reg->dirty = true; 2633 } 2634 } 2635 2636 static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) 2637 { 2638 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 2639 int err; 2640 2641 err = drm_gem_create_mmap_offset(&obj->base); 2642 if (likely(!err)) 2643 return 0; 2644 2645 /* Attempt to reap some mmap space from dead objects */ 2646 do { 2647 err = i915_gem_wait_for_idle(dev_priv, 2648 I915_WAIT_INTERRUPTIBLE, 2649 MAX_SCHEDULE_TIMEOUT); 2650 if (err) 2651 break; 2652 2653 i915_gem_drain_freed_objects(dev_priv); 2654 err = drm_gem_create_mmap_offset(&obj->base); 2655 if (!err) 2656 break; 2657 2658 } while (flush_delayed_work(&dev_priv->gt.retire_work)); 2659 2660 return err; 2661 } 2662 2663 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) 2664 { 2665 drm_gem_free_mmap_offset(&obj->base); 2666 } 2667 2668 int 2669 i915_gem_mmap_gtt(struct drm_file *file, 2670 struct drm_device *dev, 2671 uint32_t handle, 2672 uint64_t *offset) 2673 { 2674 struct drm_i915_gem_object *obj; 2675 int ret; 2676 2677 obj = i915_gem_object_lookup(file, handle); 2678 if (!obj) 2679 return -ENOENT; 2680 2681 ret = i915_gem_object_create_mmap_offset(obj); 2682 if (ret == 0) 2683 *offset = drm_vma_node_offset_addr(&obj->base.vma_node); 2684 2685 i915_gem_object_put(obj); 2686 return ret; 2687 } 2688 2689 /** 2690 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing 2691 * @dev: DRM device 2692 * @data: GTT mapping ioctl data 2693 * @file: GEM object info 2694 * 2695 * Simply returns the fake offset to userspace so it can mmap it. 2696 * The mmap call will end up in drm_gem_mmap(), which will set things 2697 * up so we can get faults in the handler above. 2698 * 2699 * The fault handler will take care of binding the object into the GTT 2700 * (since it may have been evicted to make room for something), allocating 2701 * a fence register, and mapping the appropriate aperture address into 2702 * userspace. 2703 */ 2704 int 2705 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, 2706 struct drm_file *file) 2707 { 2708 struct drm_i915_gem_mmap_gtt *args = data; 2709 2710 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); 2711 } 2712 2713 /* Immediately discard the backing storage */ 2714 static void 2715 i915_gem_object_truncate(struct drm_i915_gem_object *obj) 2716 { 2717 i915_gem_object_free_mmap_offset(obj); 2718 2719 if (obj->base.filp == NULL) 2720 return; 2721 2722 /* Our goal here is to return as much of the memory as 2723 * is possible back to the system as we are called from OOM. 2724 * To do this we must instruct the shmfs to drop all of its 2725 * backing pages, *now*. 2726 */ 2727 #ifdef __linux__ 2728 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); 2729 #else 2730 obj->base.uao->pgops->pgo_flush(obj->base.uao, 0, obj->base.size, 2731 PGO_ALLPAGES | PGO_FREE); 2732 #endif 2733 obj->mm.madv = __I915_MADV_PURGED; 2734 obj->mm.pages = ERR_PTR(-EFAULT); 2735 } 2736 2737 /* Try to discard unwanted pages */ 2738 void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) 2739 { 2740 #ifdef __linux__ 2741 struct address_space *mapping; 2742 #endif 2743 2744 lockdep_assert_held(&obj->mm.lock); 2745 GEM_BUG_ON(i915_gem_object_has_pages(obj)); 2746 2747 switch (obj->mm.madv) { 2748 case I915_MADV_DONTNEED: 2749 i915_gem_object_truncate(obj); 2750 case __I915_MADV_PURGED: 2751 return; 2752 } 2753 2754 if (obj->base.filp == NULL) 2755 return; 2756 2757 #ifdef __linux__ 2758 mapping = obj->base.filp->f_mapping, 2759 invalidate_mapping_pages(mapping, 0, (loff_t)-1); 2760 #endif 2761 } 2762 2763 static void 2764 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, 2765 struct sg_table *pages) 2766 { 2767 struct sgt_iter sgt_iter; 2768 struct vm_page *page; 2769 2770 __i915_gem_object_release_shmem(obj, pages, true); 2771 2772 i915_gem_gtt_finish_pages(obj, pages); 2773 2774 if (i915_gem_object_needs_bit17_swizzle(obj)) 2775 i915_gem_object_save_bit_17_swizzle(obj, pages); 2776 2777 for_each_sgt_page(page, sgt_iter, pages) { 2778 if (obj->mm.dirty) 2779 set_page_dirty(page); 2780 2781 #ifdef __linux__ 2782 if (obj->mm.madv == I915_MADV_WILLNEED) 2783 mark_page_accessed(page); 2784 2785 put_page(page); 2786 #endif 2787 } 2788 #ifdef __OpenBSD__ 2789 uvm_objunwire(obj->base.uao, 0, obj->base.size); 2790 #endif 2791 obj->mm.dirty = false; 2792 2793 sg_free_table(pages); 2794 kfree(pages); 2795 } 2796 2797 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) 2798 { 2799 struct radix_tree_iter iter; 2800 void __rcu **slot; 2801 2802 rcu_read_lock(); 2803 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) 2804 radix_tree_delete(&obj->mm.get_page.radix, iter.index); 2805 rcu_read_unlock(); 2806 } 2807 2808 static struct sg_table * 2809 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) 2810 { 2811 struct drm_i915_private *i915 = to_i915(obj->base.dev); 2812 struct sg_table *pages; 2813 2814 pages = fetch_and_zero(&obj->mm.pages); 2815 if (!pages) 2816 return NULL; 2817 2818 spin_lock(&i915->mm.obj_lock); 2819 list_del(&obj->mm.link); 2820 spin_unlock(&i915->mm.obj_lock); 2821 2822 if (obj->mm.mapping) { 2823 void *ptr; 2824 2825 ptr = page_mask_bits(obj->mm.mapping); 2826 if (is_vmalloc_addr(ptr)) 2827 vunmap(ptr, obj->base.size); 2828 else 2829 kunmap(kmap_to_page(ptr)); 2830 2831 obj->mm.mapping = NULL; 2832 } 2833 2834 __i915_gem_object_reset_page_iter(obj); 2835 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; 2836 2837 return pages; 2838 } 2839 2840 void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, 2841 enum i915_mm_subclass subclass) 2842 { 2843 struct sg_table *pages; 2844 2845 if (i915_gem_object_has_pinned_pages(obj)) 2846 return; 2847 2848 GEM_BUG_ON(obj->bind_count); 2849 if (!i915_gem_object_has_pages(obj)) 2850 return; 2851 2852 /* May be called by shrinker from within get_pages() (on another bo) */ 2853 mutex_lock_nested(&obj->mm.lock, subclass); 2854 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) 2855 goto unlock; 2856 2857 /* 2858 * ->put_pages might need to allocate memory for the bit17 swizzle 2859 * array, hence protect them from being reaped by removing them from gtt 2860 * lists early. 2861 */ 2862 pages = __i915_gem_object_unset_pages(obj); 2863 if (!IS_ERR(pages)) 2864 obj->ops->put_pages(obj, pages); 2865 2866 unlock: 2867 mutex_unlock(&obj->mm.lock); 2868 } 2869 2870 static bool i915_sg_trim(struct sg_table *orig_st) 2871 { 2872 struct sg_table new_st; 2873 struct scatterlist *sg, *new_sg; 2874 unsigned int i; 2875 2876 if (orig_st->nents == orig_st->orig_nents) 2877 return false; 2878 2879 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN)) 2880 return false; 2881 2882 new_sg = new_st.sgl; 2883 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { 2884 sg_set_page(new_sg, sg_page(sg), sg->length, 0); 2885 /* called before being DMA mapped, no need to copy sg->dma_* */ 2886 new_sg = sg_next(new_sg); 2887 } 2888 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */ 2889 2890 sg_free_table(orig_st); 2891 2892 *orig_st = new_st; 2893 return true; 2894 } 2895 2896 static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) 2897 { 2898 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 2899 const unsigned long page_count = obj->base.size / PAGE_SIZE; 2900 unsigned long i; 2901 struct address_space *mapping; 2902 struct sg_table *st; 2903 struct scatterlist *sg; 2904 struct sgt_iter sgt_iter; 2905 struct pglist plist; 2906 struct vm_page *page; 2907 unsigned long last_pfn = 0; /* suppress gcc warning */ 2908 unsigned int max_segment = i915_sg_segment_size(); 2909 unsigned int sg_page_sizes; 2910 gfp_t noreclaim; 2911 int ret; 2912 2913 /* Assert that the object is not currently in any GPU domain. As it 2914 * wasn't in the GTT, there shouldn't be any way it could have been in 2915 * a GPU cache 2916 */ 2917 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); 2918 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); 2919 2920 st = kmalloc(sizeof(*st), GFP_KERNEL); 2921 if (st == NULL) 2922 return -ENOMEM; 2923 2924 rebuild_st: 2925 if (sg_alloc_table(st, page_count, GFP_KERNEL)) { 2926 kfree(st); 2927 return -ENOMEM; 2928 } 2929 2930 #ifdef __linux__ 2931 /* Get the list of pages out of our struct file. They'll be pinned 2932 * at this point until we release them. 2933 * 2934 * Fail silently without starting the shrinker 2935 */ 2936 mapping = obj->base.filp->f_mapping; 2937 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); 2938 noreclaim |= __GFP_NORETRY | __GFP_NOWARN; 2939 2940 sg = st->sgl; 2941 st->nents = 0; 2942 sg_page_sizes = 0; 2943 for (i = 0; i < page_count; i++) { 2944 const unsigned int shrink[] = { 2945 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, 2946 0, 2947 }, *s = shrink; 2948 gfp_t gfp = noreclaim; 2949 2950 do { 2951 page = shmem_read_mapping_page_gfp(mapping, i, gfp); 2952 if (likely(!IS_ERR(page))) 2953 break; 2954 2955 if (!*s) { 2956 ret = PTR_ERR(page); 2957 goto err_sg; 2958 } 2959 2960 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); 2961 cond_resched(); 2962 2963 /* We've tried hard to allocate the memory by reaping 2964 * our own buffer, now let the real VM do its job and 2965 * go down in flames if truly OOM. 2966 * 2967 * However, since graphics tend to be disposable, 2968 * defer the oom here by reporting the ENOMEM back 2969 * to userspace. 2970 */ 2971 if (!*s) { 2972 /* reclaim and warn, but no oom */ 2973 gfp = mapping_gfp_mask(mapping); 2974 2975 /* Our bo are always dirty and so we require 2976 * kswapd to reclaim our pages (direct reclaim 2977 * does not effectively begin pageout of our 2978 * buffers on its own). However, direct reclaim 2979 * only waits for kswapd when under allocation 2980 * congestion. So as a result __GFP_RECLAIM is 2981 * unreliable and fails to actually reclaim our 2982 * dirty pages -- unless you try over and over 2983 * again with !__GFP_NORETRY. However, we still 2984 * want to fail this allocation rather than 2985 * trigger the out-of-memory killer and for 2986 * this we want __GFP_RETRY_MAYFAIL. 2987 */ 2988 gfp |= __GFP_RETRY_MAYFAIL; 2989 } 2990 } while (1); 2991 2992 if (!i || 2993 sg->length >= max_segment || 2994 page_to_pfn(page) != last_pfn + 1) { 2995 if (i) { 2996 sg_page_sizes |= sg->length; 2997 sg = sg_next(sg); 2998 } 2999 st->nents++; 3000 sg_set_page(sg, page, PAGE_SIZE, 0); 3001 } else { 3002 sg->length += PAGE_SIZE; 3003 } 3004 last_pfn = page_to_pfn(page); 3005 3006 /* Check that the i965g/gm workaround works. */ 3007 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); 3008 } 3009 #else 3010 sg = st->sgl; 3011 st->nents = 0; 3012 sg_page_sizes = 0; 3013 3014 TAILQ_INIT(&plist); 3015 if (uvm_objwire(obj->base.uao, 0, obj->base.size, &plist)) { 3016 ret = -ENOMEM; 3017 goto err_pages; 3018 } 3019 3020 i = 0; 3021 TAILQ_FOREACH(page, &plist, pageq) { 3022 if (i) { 3023 sg_page_sizes |= sg->length; 3024 sg = sg_next(sg); 3025 } 3026 st->nents++; 3027 sg_set_page(sg, page, PAGE_SIZE, 0); 3028 i++; 3029 } 3030 #endif 3031 if (sg) { /* loop terminated early; short sg table */ 3032 sg_page_sizes |= sg->length; 3033 sg_mark_end(sg); 3034 } 3035 3036 /* Trim unused sg entries to avoid wasting memory. */ 3037 i915_sg_trim(st); 3038 3039 ret = i915_gem_gtt_prepare_pages(obj, st); 3040 if (ret) { 3041 /* DMA remapping failed? One possible cause is that 3042 * it could not reserve enough large entries, asking 3043 * for PAGE_SIZE chunks instead may be helpful. 3044 */ 3045 if (max_segment > PAGE_SIZE) { 3046 #ifdef __linux__ 3047 for_each_sgt_page(page, sgt_iter, st) 3048 put_page(page); 3049 #else 3050 uvm_objunwire(obj->base.uao, 0, obj->base.size); 3051 #endif 3052 sg_free_table(st); 3053 3054 max_segment = PAGE_SIZE; 3055 goto rebuild_st; 3056 } else { 3057 dev_warn(&dev_priv->drm.pdev->dev, 3058 "Failed to DMA remap %lu pages\n", 3059 page_count); 3060 goto err_pages; 3061 } 3062 } 3063 3064 if (i915_gem_object_needs_bit17_swizzle(obj)) 3065 i915_gem_object_do_bit_17_swizzle(obj, st); 3066 3067 __i915_gem_object_set_pages(obj, st, sg_page_sizes); 3068 3069 return 0; 3070 3071 #ifdef __linux__ 3072 err_sg: 3073 #endif 3074 sg_mark_end(sg); 3075 err_pages: 3076 #ifdef __linux__ 3077 for_each_sgt_page(page, sgt_iter, st) 3078 put_page(page); 3079 #else 3080 uvm_objunwire(obj->base.uao, 0, obj->base.size); 3081 #endif 3082 sg_free_table(st); 3083 kfree(st); 3084 3085 /* shmemfs first checks if there is enough memory to allocate the page 3086 * and reports ENOSPC should there be insufficient, along with the usual 3087 * ENOMEM for a genuine allocation failure. 3088 * 3089 * We use ENOSPC in our driver to mean that we have run out of aperture 3090 * space and so want to translate the error from shmemfs back to our 3091 * usual understanding of ENOMEM. 3092 */ 3093 if (ret == -ENOSPC) 3094 ret = -ENOMEM; 3095 3096 return ret; 3097 } 3098 3099 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, 3100 struct sg_table *pages, 3101 unsigned int sg_page_sizes) 3102 { 3103 struct drm_i915_private *i915 = to_i915(obj->base.dev); 3104 unsigned long supported = INTEL_INFO(i915)->page_sizes; 3105 int i; 3106 3107 lockdep_assert_held(&obj->mm.lock); 3108 3109 obj->mm.get_page.sg_pos = pages->sgl; 3110 obj->mm.get_page.sg_idx = 0; 3111 3112 obj->mm.pages = pages; 3113 3114 if (i915_gem_object_is_tiled(obj) && 3115 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { 3116 GEM_BUG_ON(obj->mm.quirked); 3117 __i915_gem_object_pin_pages(obj); 3118 obj->mm.quirked = true; 3119 } 3120 3121 GEM_BUG_ON(!sg_page_sizes); 3122 obj->mm.page_sizes.phys = sg_page_sizes; 3123 3124 /* 3125 * Calculate the supported page-sizes which fit into the given 3126 * sg_page_sizes. This will give us the page-sizes which we may be able 3127 * to use opportunistically when later inserting into the GTT. For 3128 * example if phys=2G, then in theory we should be able to use 1G, 2M, 3129 * 64K or 4K pages, although in practice this will depend on a number of 3130 * other factors. 3131 */ 3132 obj->mm.page_sizes.sg = 0; 3133 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { 3134 if (obj->mm.page_sizes.phys & ~0u << i) 3135 obj->mm.page_sizes.sg |= BIT(i); 3136 } 3137 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); 3138 3139 spin_lock(&i915->mm.obj_lock); 3140 list_add(&obj->mm.link, &i915->mm.unbound_list); 3141 spin_unlock(&i915->mm.obj_lock); 3142 } 3143 3144 static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) 3145 { 3146 int err; 3147 3148 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { 3149 DRM_DEBUG("Attempting to obtain a purgeable object\n"); 3150 return -EFAULT; 3151 } 3152 3153 err = obj->ops->get_pages(obj); 3154 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); 3155 3156 return err; 3157 } 3158 3159 /* Ensure that the associated pages are gathered from the backing storage 3160 * and pinned into our object. i915_gem_object_pin_pages() may be called 3161 * multiple times before they are released by a single call to 3162 * i915_gem_object_unpin_pages() - once the pages are no longer referenced 3163 * either as a result of memory pressure (reaping pages under the shrinker) 3164 * or as the object is itself released. 3165 */ 3166 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) 3167 { 3168 int err; 3169 3170 err = mutex_lock_interruptible(&obj->mm.lock); 3171 if (err) 3172 return err; 3173 3174 if (unlikely(!i915_gem_object_has_pages(obj))) { 3175 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); 3176 3177 err = ____i915_gem_object_get_pages(obj); 3178 if (err) 3179 goto unlock; 3180 3181 smp_mb__before_atomic(); 3182 } 3183 atomic_inc(&obj->mm.pages_pin_count); 3184 3185 unlock: 3186 mutex_unlock(&obj->mm.lock); 3187 return err; 3188 } 3189 3190 /* The 'mapping' part of i915_gem_object_pin_map() below */ 3191 static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, 3192 enum i915_map_type type) 3193 { 3194 unsigned long n_pages = obj->base.size >> PAGE_SHIFT; 3195 struct sg_table *sgt = obj->mm.pages; 3196 struct sgt_iter sgt_iter; 3197 struct vm_page *page; 3198 struct vm_page *stack_pages[32]; 3199 struct vm_page **pages = stack_pages; 3200 unsigned long i = 0; 3201 pgprot_t pgprot; 3202 void *addr; 3203 3204 #if 0 3205 /* A single page can always be kmapped */ 3206 if (n_pages == 1 && type == I915_MAP_WB) 3207 return kmap(sg_page(sgt->sgl)); 3208 #endif 3209 3210 if (n_pages > ARRAY_SIZE(stack_pages)) { 3211 /* Too big for stack -- allocate temporary array instead */ 3212 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); 3213 if (!pages) 3214 return NULL; 3215 } 3216 3217 for_each_sgt_page(page, sgt_iter, sgt) 3218 pages[i++] = page; 3219 3220 /* Check that we have the expected number of pages */ 3221 GEM_BUG_ON(i != n_pages); 3222 3223 switch (type) { 3224 default: 3225 MISSING_CASE(type); 3226 /* fallthrough to use PAGE_KERNEL anyway */ 3227 case I915_MAP_WB: 3228 pgprot = PAGE_KERNEL; 3229 break; 3230 case I915_MAP_WC: 3231 pgprot = pgprot_writecombine(PAGE_KERNEL_IO); 3232 break; 3233 } 3234 addr = vmap(pages, n_pages, 0, pgprot); 3235 3236 if (pages != stack_pages) 3237 kvfree(pages); 3238 3239 return addr; 3240 } 3241 3242 /* get, pin, and map the pages of the object into kernel space */ 3243 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, 3244 enum i915_map_type type) 3245 { 3246 enum i915_map_type has_type; 3247 bool pinned; 3248 void *ptr; 3249 int ret; 3250 3251 if (unlikely(!i915_gem_object_has_struct_page(obj))) 3252 return ERR_PTR(-ENXIO); 3253 3254 ret = mutex_lock_interruptible(&obj->mm.lock); 3255 if (ret) 3256 return ERR_PTR(ret); 3257 3258 pinned = !(type & I915_MAP_OVERRIDE); 3259 type &= ~I915_MAP_OVERRIDE; 3260 3261 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { 3262 if (unlikely(!i915_gem_object_has_pages(obj))) { 3263 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); 3264 3265 ret = ____i915_gem_object_get_pages(obj); 3266 if (ret) 3267 goto err_unlock; 3268 3269 smp_mb__before_atomic(); 3270 } 3271 atomic_inc(&obj->mm.pages_pin_count); 3272 pinned = false; 3273 } 3274 GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 3275 3276 ptr = page_unpack_bits(obj->mm.mapping, &has_type); 3277 if (ptr && has_type != type) { 3278 if (pinned) { 3279 ret = -EBUSY; 3280 goto err_unpin; 3281 } 3282 3283 if (is_vmalloc_addr(ptr)) 3284 vunmap(ptr, obj->base.size); 3285 else 3286 kunmap(kmap_to_page(ptr)); 3287 3288 ptr = obj->mm.mapping = NULL; 3289 } 3290 3291 if (!ptr) { 3292 ptr = i915_gem_object_map(obj, type); 3293 if (!ptr) { 3294 ret = -ENOMEM; 3295 goto err_unpin; 3296 } 3297 3298 obj->mm.mapping = page_pack_bits(ptr, type); 3299 } 3300 3301 out_unlock: 3302 mutex_unlock(&obj->mm.lock); 3303 return ptr; 3304 3305 err_unpin: 3306 atomic_dec(&obj->mm.pages_pin_count); 3307 err_unlock: 3308 ptr = ERR_PTR(ret); 3309 goto out_unlock; 3310 } 3311 3312 static int 3313 i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, 3314 const struct drm_i915_gem_pwrite *arg) 3315 { 3316 #ifdef __linux__ 3317 struct address_space *mapping = obj->base.filp->f_mapping; 3318 #endif 3319 char __user *user_data = u64_to_user_ptr(arg->data_ptr); 3320 u64 remain, offset; 3321 unsigned int pg; 3322 3323 /* Before we instantiate/pin the backing store for our use, we 3324 * can prepopulate the shmemfs filp efficiently using a write into 3325 * the pagecache. We avoid the penalty of instantiating all the 3326 * pages, important if the user is just writing to a few and never 3327 * uses the object on the GPU, and using a direct write into shmemfs 3328 * allows it to avoid the cost of retrieving a page (either swapin 3329 * or clearing-before-use) before it is overwritten. 3330 */ 3331 if (i915_gem_object_has_pages(obj)) 3332 return -ENODEV; 3333 3334 if (obj->mm.madv != I915_MADV_WILLNEED) 3335 return -EFAULT; 3336 3337 /* Before the pages are instantiated the object is treated as being 3338 * in the CPU domain. The pages will be clflushed as required before 3339 * use, and we can freely write into the pages directly. If userspace 3340 * races pwrite with any other operation; corruption will ensue - 3341 * that is userspace's prerogative! 3342 */ 3343 3344 remain = arg->size; 3345 offset = arg->offset; 3346 pg = offset_in_page(offset); 3347 3348 do { 3349 unsigned int len, unwritten; 3350 struct vm_page *page; 3351 void *data, *vaddr; 3352 int err; 3353 3354 len = PAGE_SIZE - pg; 3355 if (len > remain) 3356 len = remain; 3357 3358 #ifdef __linux__ 3359 err = pagecache_write_begin(obj->base.filp, mapping, 3360 offset, len, 0, 3361 &page, &data); 3362 if (err < 0) 3363 return err; 3364 #else 3365 struct pglist plist; 3366 TAILQ_INIT(&plist); 3367 if (uvm_objwire(obj->base.uao, trunc_page(offset), 3368 trunc_page(offset) + PAGE_SIZE, &plist)) { 3369 err = -ENOMEM; 3370 return err; 3371 } 3372 page = TAILQ_FIRST(&plist); 3373 #endif 3374 3375 vaddr = kmap(page); 3376 unwritten = copy_from_user(vaddr + pg, user_data, len); 3377 kunmap(vaddr); 3378 3379 #ifdef __linux__ 3380 err = pagecache_write_end(obj->base.filp, mapping, 3381 offset, len, len - unwritten, 3382 page, data); 3383 if (err < 0) 3384 return err; 3385 #else 3386 uvm_objunwire(obj->base.uao, trunc_page(offset), 3387 trunc_page(offset) + PAGE_SIZE); 3388 #endif 3389 3390 if (unwritten) 3391 return -EFAULT; 3392 3393 remain -= len; 3394 user_data += len; 3395 offset += len; 3396 pg = 0; 3397 } while (remain); 3398 3399 return 0; 3400 } 3401 3402 static void i915_gem_client_mark_guilty(struct drm_i915_file_private *file_priv, 3403 const struct i915_gem_context *ctx) 3404 { 3405 unsigned int score; 3406 unsigned long prev_hang; 3407 3408 if (i915_gem_context_is_banned(ctx)) 3409 score = I915_CLIENT_SCORE_CONTEXT_BAN; 3410 else 3411 score = 0; 3412 3413 prev_hang = xchg(&file_priv->hang_timestamp, jiffies); 3414 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES)) 3415 score += I915_CLIENT_SCORE_HANG_FAST; 3416 3417 if (score) { 3418 atomic_add(score, &file_priv->ban_score); 3419 3420 DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n", 3421 ctx->name, score, 3422 atomic_read(&file_priv->ban_score)); 3423 } 3424 } 3425 3426 static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) 3427 { 3428 unsigned int score; 3429 bool banned, bannable; 3430 3431 atomic_inc(&ctx->guilty_count); 3432 3433 bannable = i915_gem_context_is_bannable(ctx); 3434 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score); 3435 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD; 3436 3437 /* Cool contexts don't accumulate client ban score */ 3438 if (!bannable) 3439 return; 3440 3441 if (banned) { 3442 DRM_DEBUG_DRIVER("context %s: guilty %d, score %u, banned\n", 3443 ctx->name, atomic_read(&ctx->guilty_count), 3444 score); 3445 i915_gem_context_set_banned(ctx); 3446 } 3447 3448 if (!IS_ERR_OR_NULL(ctx->file_priv)) 3449 i915_gem_client_mark_guilty(ctx->file_priv, ctx); 3450 } 3451 3452 static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) 3453 { 3454 atomic_inc(&ctx->active_count); 3455 } 3456 3457 struct i915_request * 3458 i915_gem_find_active_request(struct intel_engine_cs *engine) 3459 { 3460 struct i915_request *request, *active = NULL; 3461 unsigned long flags; 3462 3463 /* 3464 * We are called by the error capture, reset and to dump engine 3465 * state at random points in time. In particular, note that neither is 3466 * crucially ordered with an interrupt. After a hang, the GPU is dead 3467 * and we assume that no more writes can happen (we waited long enough 3468 * for all writes that were in transaction to be flushed) - adding an 3469 * extra delay for a recent interrupt is pointless. Hence, we do 3470 * not need an engine->irq_seqno_barrier() before the seqno reads. 3471 * At all other times, we must assume the GPU is still running, but 3472 * we only care about the snapshot of this moment. 3473 */ 3474 spin_lock_irqsave(&engine->timeline.lock, flags); 3475 list_for_each_entry(request, &engine->timeline.requests, link) { 3476 if (__i915_request_completed(request, request->global_seqno)) 3477 continue; 3478 3479 active = request; 3480 break; 3481 } 3482 spin_unlock_irqrestore(&engine->timeline.lock, flags); 3483 3484 return active; 3485 } 3486 3487 /* 3488 * Ensure irq handler finishes, and not run again. 3489 * Also return the active request so that we only search for it once. 3490 */ 3491 struct i915_request * 3492 i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) 3493 { 3494 struct i915_request *request; 3495 3496 /* 3497 * During the reset sequence, we must prevent the engine from 3498 * entering RC6. As the context state is undefined until we restart 3499 * the engine, if it does enter RC6 during the reset, the state 3500 * written to the powercontext is undefined and so we may lose 3501 * GPU state upon resume, i.e. fail to restart after a reset. 3502 */ 3503 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); 3504 3505 request = engine->reset.prepare(engine); 3506 if (request && request->fence.error == -EIO) 3507 request = ERR_PTR(-EIO); /* Previous reset failed! */ 3508 3509 return request; 3510 } 3511 3512 int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) 3513 { 3514 struct intel_engine_cs *engine; 3515 struct i915_request *request; 3516 enum intel_engine_id id; 3517 int err = 0; 3518 3519 for_each_engine(engine, dev_priv, id) { 3520 request = i915_gem_reset_prepare_engine(engine); 3521 if (IS_ERR(request)) { 3522 err = PTR_ERR(request); 3523 continue; 3524 } 3525 3526 engine->hangcheck.active_request = request; 3527 } 3528 3529 i915_gem_revoke_fences(dev_priv); 3530 intel_uc_sanitize(dev_priv); 3531 3532 return err; 3533 } 3534 3535 static void engine_skip_context(struct i915_request *request) 3536 { 3537 struct intel_engine_cs *engine = request->engine; 3538 struct i915_gem_context *hung_ctx = request->gem_context; 3539 struct i915_timeline *timeline = request->timeline; 3540 unsigned long flags; 3541 3542 GEM_BUG_ON(timeline == &engine->timeline); 3543 3544 spin_lock_irqsave(&engine->timeline.lock, flags); 3545 spin_lock(&timeline->lock); 3546 3547 list_for_each_entry_continue(request, &engine->timeline.requests, link) 3548 if (request->gem_context == hung_ctx) 3549 i915_request_skip(request, -EIO); 3550 3551 list_for_each_entry(request, &timeline->requests, link) 3552 i915_request_skip(request, -EIO); 3553 3554 spin_unlock(&timeline->lock); 3555 spin_unlock_irqrestore(&engine->timeline.lock, flags); 3556 } 3557 3558 /* Returns the request if it was guilty of the hang */ 3559 static struct i915_request * 3560 i915_gem_reset_request(struct intel_engine_cs *engine, 3561 struct i915_request *request, 3562 bool stalled) 3563 { 3564 /* The guilty request will get skipped on a hung engine. 3565 * 3566 * Users of client default contexts do not rely on logical 3567 * state preserved between batches so it is safe to execute 3568 * queued requests following the hang. Non default contexts 3569 * rely on preserved state, so skipping a batch loses the 3570 * evolution of the state and it needs to be considered corrupted. 3571 * Executing more queued batches on top of corrupted state is 3572 * risky. But we take the risk by trying to advance through 3573 * the queued requests in order to make the client behaviour 3574 * more predictable around resets, by not throwing away random 3575 * amount of batches it has prepared for execution. Sophisticated 3576 * clients can use gem_reset_stats_ioctl and dma fence status 3577 * (exported via sync_file info ioctl on explicit fences) to observe 3578 * when it loses the context state and should rebuild accordingly. 3579 * 3580 * The context ban, and ultimately the client ban, mechanism are safety 3581 * valves if client submission ends up resulting in nothing more than 3582 * subsequent hangs. 3583 */ 3584 3585 if (i915_request_completed(request)) { 3586 GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n", 3587 engine->name, request->global_seqno, 3588 request->fence.context, request->fence.seqno, 3589 intel_engine_get_seqno(engine)); 3590 stalled = false; 3591 } 3592 3593 if (stalled) { 3594 i915_gem_context_mark_guilty(request->gem_context); 3595 i915_request_skip(request, -EIO); 3596 3597 /* If this context is now banned, skip all pending requests. */ 3598 if (i915_gem_context_is_banned(request->gem_context)) 3599 engine_skip_context(request); 3600 } else { 3601 /* 3602 * Since this is not the hung engine, it may have advanced 3603 * since the hang declaration. Double check by refinding 3604 * the active request at the time of the reset. 3605 */ 3606 request = i915_gem_find_active_request(engine); 3607 if (request) { 3608 unsigned long flags; 3609 3610 i915_gem_context_mark_innocent(request->gem_context); 3611 dma_fence_set_error(&request->fence, -EAGAIN); 3612 3613 /* Rewind the engine to replay the incomplete rq */ 3614 spin_lock_irqsave(&engine->timeline.lock, flags); 3615 request = list_prev_entry(request, link); 3616 if (&request->link == &engine->timeline.requests) 3617 request = NULL; 3618 spin_unlock_irqrestore(&engine->timeline.lock, flags); 3619 } 3620 } 3621 3622 return request; 3623 } 3624 3625 void i915_gem_reset_engine(struct intel_engine_cs *engine, 3626 struct i915_request *request, 3627 bool stalled) 3628 { 3629 /* 3630 * Make sure this write is visible before we re-enable the interrupt 3631 * handlers on another CPU, as tasklet_enable() resolves to just 3632 * a compiler barrier which is insufficient for our purpose here. 3633 */ 3634 smp_store_mb(engine->irq_posted, 0); 3635 3636 if (request) 3637 request = i915_gem_reset_request(engine, request, stalled); 3638 3639 /* Setup the CS to resume from the breadcrumb of the hung request */ 3640 engine->reset.reset(engine, request); 3641 } 3642 3643 void i915_gem_reset(struct drm_i915_private *dev_priv, 3644 unsigned int stalled_mask) 3645 { 3646 struct intel_engine_cs *engine; 3647 enum intel_engine_id id; 3648 3649 lockdep_assert_held(&dev_priv->drm.struct_mutex); 3650 3651 i915_retire_requests(dev_priv); 3652 3653 for_each_engine(engine, dev_priv, id) { 3654 struct intel_context *ce; 3655 3656 i915_gem_reset_engine(engine, 3657 engine->hangcheck.active_request, 3658 stalled_mask & ENGINE_MASK(id)); 3659 ce = fetch_and_zero(&engine->last_retired_context); 3660 if (ce) 3661 intel_context_unpin(ce); 3662 3663 /* 3664 * Ostensibily, we always want a context loaded for powersaving, 3665 * so if the engine is idle after the reset, send a request 3666 * to load our scratch kernel_context. 3667 * 3668 * More mysteriously, if we leave the engine idle after a reset, 3669 * the next userspace batch may hang, with what appears to be 3670 * an incoherent read by the CS (presumably stale TLB). An 3671 * empty request appears sufficient to paper over the glitch. 3672 */ 3673 if (intel_engine_is_idle(engine)) { 3674 struct i915_request *rq; 3675 3676 rq = i915_request_alloc(engine, 3677 dev_priv->kernel_context); 3678 if (!IS_ERR(rq)) 3679 i915_request_add(rq); 3680 } 3681 } 3682 3683 i915_gem_restore_fences(dev_priv); 3684 } 3685 3686 void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) 3687 { 3688 engine->reset.finish(engine); 3689 3690 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); 3691 } 3692 3693 void i915_gem_reset_finish(struct drm_i915_private *dev_priv) 3694 { 3695 struct intel_engine_cs *engine; 3696 enum intel_engine_id id; 3697 3698 lockdep_assert_held(&dev_priv->drm.struct_mutex); 3699 3700 for_each_engine(engine, dev_priv, id) { 3701 engine->hangcheck.active_request = NULL; 3702 i915_gem_reset_finish_engine(engine); 3703 } 3704 } 3705 3706 static void nop_submit_request(struct i915_request *request) 3707 { 3708 GEM_TRACE("%s fence %llx:%d -> -EIO\n", 3709 request->engine->name, 3710 request->fence.context, request->fence.seqno); 3711 dma_fence_set_error(&request->fence, -EIO); 3712 3713 i915_request_submit(request); 3714 } 3715 3716 static void nop_complete_submit_request(struct i915_request *request) 3717 { 3718 unsigned long flags; 3719 3720 GEM_TRACE("%s fence %llx:%d -> -EIO\n", 3721 request->engine->name, 3722 request->fence.context, request->fence.seqno); 3723 dma_fence_set_error(&request->fence, -EIO); 3724 3725 spin_lock_irqsave(&request->engine->timeline.lock, flags); 3726 __i915_request_submit(request); 3727 intel_engine_init_global_seqno(request->engine, request->global_seqno); 3728 spin_unlock_irqrestore(&request->engine->timeline.lock, flags); 3729 } 3730 3731 void i915_gem_set_wedged(struct drm_i915_private *i915) 3732 { 3733 struct intel_engine_cs *engine; 3734 enum intel_engine_id id; 3735 3736 GEM_TRACE("start\n"); 3737 3738 if (GEM_SHOW_DEBUG()) { 3739 struct drm_printer p = drm_debug_printer(__func__); 3740 3741 for_each_engine(engine, i915, id) 3742 intel_engine_dump(engine, &p, "%s\n", engine->name); 3743 } 3744 3745 set_bit(I915_WEDGED, &i915->gpu_error.flags); 3746 smp_mb__after_atomic(); 3747 3748 /* 3749 * First, stop submission to hw, but do not yet complete requests by 3750 * rolling the global seqno forward (since this would complete requests 3751 * for which we haven't set the fence error to EIO yet). 3752 */ 3753 for_each_engine(engine, i915, id) { 3754 i915_gem_reset_prepare_engine(engine); 3755 3756 engine->submit_request = nop_submit_request; 3757 engine->schedule = NULL; 3758 } 3759 i915->caps.scheduler = 0; 3760 3761 /* Even if the GPU reset fails, it should still stop the engines */ 3762 intel_gpu_reset(i915, ALL_ENGINES); 3763 3764 /* 3765 * Make sure no one is running the old callback before we proceed with 3766 * cancelling requests and resetting the completion tracking. Otherwise 3767 * we might submit a request to the hardware which never completes. 3768 */ 3769 synchronize_rcu(); 3770 3771 for_each_engine(engine, i915, id) { 3772 /* Mark all executing requests as skipped */ 3773 engine->cancel_requests(engine); 3774 3775 /* 3776 * Only once we've force-cancelled all in-flight requests can we 3777 * start to complete all requests. 3778 */ 3779 engine->submit_request = nop_complete_submit_request; 3780 } 3781 3782 /* 3783 * Make sure no request can slip through without getting completed by 3784 * either this call here to intel_engine_init_global_seqno, or the one 3785 * in nop_complete_submit_request. 3786 */ 3787 synchronize_rcu(); 3788 3789 for_each_engine(engine, i915, id) { 3790 unsigned long flags; 3791 3792 /* 3793 * Mark all pending requests as complete so that any concurrent 3794 * (lockless) lookup doesn't try and wait upon the request as we 3795 * reset it. 3796 */ 3797 spin_lock_irqsave(&engine->timeline.lock, flags); 3798 intel_engine_init_global_seqno(engine, 3799 intel_engine_last_submit(engine)); 3800 spin_unlock_irqrestore(&engine->timeline.lock, flags); 3801 3802 i915_gem_reset_finish_engine(engine); 3803 } 3804 3805 GEM_TRACE("end\n"); 3806 3807 wake_up_all(&i915->gpu_error.reset_queue); 3808 } 3809 3810 bool i915_gem_unset_wedged(struct drm_i915_private *i915) 3811 { 3812 struct i915_timeline *tl; 3813 3814 lockdep_assert_held(&i915->drm.struct_mutex); 3815 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) 3816 return true; 3817 3818 GEM_TRACE("start\n"); 3819 3820 /* 3821 * Before unwedging, make sure that all pending operations 3822 * are flushed and errored out - we may have requests waiting upon 3823 * third party fences. We marked all inflight requests as EIO, and 3824 * every execbuf since returned EIO, for consistency we want all 3825 * the currently pending requests to also be marked as EIO, which 3826 * is done inside our nop_submit_request - and so we must wait. 3827 * 3828 * No more can be submitted until we reset the wedged bit. 3829 */ 3830 list_for_each_entry(tl, &i915->gt.timelines, link) { 3831 struct i915_request *rq; 3832 3833 rq = i915_gem_active_peek(&tl->last_request, 3834 &i915->drm.struct_mutex); 3835 if (!rq) 3836 continue; 3837 3838 /* 3839 * We can't use our normal waiter as we want to 3840 * avoid recursively trying to handle the current 3841 * reset. The basic dma_fence_default_wait() installs 3842 * a callback for dma_fence_signal(), which is 3843 * triggered by our nop handler (indirectly, the 3844 * callback enables the signaler thread which is 3845 * woken by the nop_submit_request() advancing the seqno 3846 * and when the seqno passes the fence, the signaler 3847 * then signals the fence waking us up). 3848 */ 3849 if (dma_fence_default_wait(&rq->fence, true, 3850 MAX_SCHEDULE_TIMEOUT) < 0) 3851 return false; 3852 } 3853 i915_retire_requests(i915); 3854 GEM_BUG_ON(i915->gt.active_requests); 3855 3856 /* 3857 * Undo nop_submit_request. We prevent all new i915 requests from 3858 * being queued (by disallowing execbuf whilst wedged) so having 3859 * waited for all active requests above, we know the system is idle 3860 * and do not have to worry about a thread being inside 3861 * engine->submit_request() as we swap over. So unlike installing 3862 * the nop_submit_request on reset, we can do this from normal 3863 * context and do not require stop_machine(). 3864 */ 3865 intel_engines_reset_default_submission(i915); 3866 i915_gem_contexts_lost(i915); 3867 3868 GEM_TRACE("end\n"); 3869 3870 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ 3871 clear_bit(I915_WEDGED, &i915->gpu_error.flags); 3872 3873 return true; 3874 } 3875 3876 static void 3877 i915_gem_retire_work_handler(struct work_struct *work) 3878 { 3879 struct drm_i915_private *dev_priv = 3880 container_of(work, typeof(*dev_priv), gt.retire_work.work); 3881 struct drm_device *dev = &dev_priv->drm; 3882 3883 /* Come back later if the device is busy... */ 3884 if (mutex_trylock(&dev->struct_mutex)) { 3885 i915_retire_requests(dev_priv); 3886 mutex_unlock(&dev->struct_mutex); 3887 } 3888 3889 /* 3890 * Keep the retire handler running until we are finally idle. 3891 * We do not need to do this test under locking as in the worst-case 3892 * we queue the retire worker once too often. 3893 */ 3894 if (READ_ONCE(dev_priv->gt.awake)) 3895 queue_delayed_work(dev_priv->wq, 3896 &dev_priv->gt.retire_work, 3897 round_jiffies_up_relative(HZ)); 3898 } 3899 3900 static void shrink_caches(struct drm_i915_private *i915) 3901 { 3902 #ifdef __linux__ 3903 /* 3904 * kmem_cache_shrink() discards empty slabs and reorders partially 3905 * filled slabs to prioritise allocating from the mostly full slabs, 3906 * with the aim of reducing fragmentation. 3907 */ 3908 kmem_cache_shrink(i915->priorities); 3909 kmem_cache_shrink(i915->dependencies); 3910 kmem_cache_shrink(i915->requests); 3911 kmem_cache_shrink(i915->luts); 3912 kmem_cache_shrink(i915->vmas); 3913 kmem_cache_shrink(i915->objects); 3914 #endif 3915 } 3916 3917 struct sleep_rcu_work { 3918 union { 3919 struct rcu_head rcu; 3920 struct work_struct work; 3921 }; 3922 struct drm_i915_private *i915; 3923 unsigned int epoch; 3924 }; 3925 3926 static inline bool 3927 same_epoch(struct drm_i915_private *i915, unsigned int epoch) 3928 { 3929 /* 3930 * There is a small chance that the epoch wrapped since we started 3931 * sleeping. If we assume that epoch is at least a u32, then it will 3932 * take at least 2^32 * 100ms for it to wrap, or about 326 years. 3933 */ 3934 return epoch == READ_ONCE(i915->gt.epoch); 3935 } 3936 3937 static void __sleep_work(struct work_struct *work) 3938 { 3939 struct sleep_rcu_work *s = container_of(work, typeof(*s), work); 3940 struct drm_i915_private *i915 = s->i915; 3941 unsigned int epoch = s->epoch; 3942 3943 kfree(s); 3944 if (same_epoch(i915, epoch)) 3945 shrink_caches(i915); 3946 } 3947 3948 static void __sleep_rcu(struct rcu_head *rcu) 3949 { 3950 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); 3951 struct drm_i915_private *i915 = s->i915; 3952 3953 if (same_epoch(i915, s->epoch)) { 3954 INIT_WORK(&s->work, __sleep_work); 3955 queue_work(i915->wq, &s->work); 3956 } else { 3957 kfree(s); 3958 } 3959 } 3960 3961 static inline bool 3962 new_requests_since_last_retire(const struct drm_i915_private *i915) 3963 { 3964 return (READ_ONCE(i915->gt.active_requests) || 3965 work_pending(&i915->gt.idle_work.work)); 3966 } 3967 3968 static void assert_kernel_context_is_current(struct drm_i915_private *i915) 3969 { 3970 struct intel_engine_cs *engine; 3971 enum intel_engine_id id; 3972 3973 if (i915_terminally_wedged(&i915->gpu_error)) 3974 return; 3975 3976 GEM_BUG_ON(i915->gt.active_requests); 3977 for_each_engine(engine, i915, id) { 3978 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request)); 3979 GEM_BUG_ON(engine->last_retired_context != 3980 to_intel_context(i915->kernel_context, engine)); 3981 } 3982 } 3983 3984 static void 3985 i915_gem_idle_work_handler(struct work_struct *work) 3986 { 3987 struct drm_i915_private *dev_priv = 3988 container_of(work, typeof(*dev_priv), gt.idle_work.work); 3989 unsigned int epoch = I915_EPOCH_INVALID; 3990 bool rearm_hangcheck; 3991 3992 if (!READ_ONCE(dev_priv->gt.awake)) 3993 return; 3994 3995 if (READ_ONCE(dev_priv->gt.active_requests)) 3996 return; 3997 3998 /* 3999 * Flush out the last user context, leaving only the pinned 4000 * kernel context resident. When we are idling on the kernel_context, 4001 * no more new requests (with a context switch) are emitted and we 4002 * can finally rest. A consequence is that the idle work handler is 4003 * always called at least twice before idling (and if the system is 4004 * idle that implies a round trip through the retire worker). 4005 */ 4006 mutex_lock(&dev_priv->drm.struct_mutex); 4007 i915_gem_switch_to_kernel_context(dev_priv); 4008 mutex_unlock(&dev_priv->drm.struct_mutex); 4009 4010 GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n", 4011 READ_ONCE(dev_priv->gt.active_requests)); 4012 4013 /* 4014 * Wait for last execlists context complete, but bail out in case a 4015 * new request is submitted. As we don't trust the hardware, we 4016 * continue on if the wait times out. This is necessary to allow 4017 * the machine to suspend even if the hardware dies, and we will 4018 * try to recover in resume (after depriving the hardware of power, 4019 * it may be in a better mmod). 4020 */ 4021 __wait_for(if (new_requests_since_last_retire(dev_priv)) return, 4022 intel_engines_are_idle(dev_priv), 4023 I915_IDLE_ENGINES_TIMEOUT * 1000, 4024 10, 500); 4025 4026 rearm_hangcheck = 4027 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); 4028 4029 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { 4030 /* Currently busy, come back later */ 4031 mod_delayed_work(dev_priv->wq, 4032 &dev_priv->gt.idle_work, 4033 msecs_to_jiffies(50)); 4034 goto out_rearm; 4035 } 4036 4037 /* 4038 * New request retired after this work handler started, extend active 4039 * period until next instance of the work. 4040 */ 4041 if (new_requests_since_last_retire(dev_priv)) 4042 goto out_unlock; 4043 4044 epoch = __i915_gem_park(dev_priv); 4045 4046 assert_kernel_context_is_current(dev_priv); 4047 4048 rearm_hangcheck = false; 4049 out_unlock: 4050 mutex_unlock(&dev_priv->drm.struct_mutex); 4051 4052 out_rearm: 4053 if (rearm_hangcheck) { 4054 GEM_BUG_ON(!dev_priv->gt.awake); 4055 i915_queue_hangcheck(dev_priv); 4056 } 4057 4058 /* 4059 * When we are idle, it is an opportune time to reap our caches. 4060 * However, we have many objects that utilise RCU and the ordered 4061 * i915->wq that this work is executing on. To try and flush any 4062 * pending frees now we are idle, we first wait for an RCU grace 4063 * period, and then queue a task (that will run last on the wq) to 4064 * shrink and re-optimize the caches. 4065 */ 4066 if (same_epoch(dev_priv, epoch)) { 4067 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); 4068 if (s) { 4069 s->i915 = dev_priv; 4070 s->epoch = epoch; 4071 call_rcu(&s->rcu, __sleep_rcu); 4072 } 4073 } 4074 } 4075 4076 void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) 4077 { 4078 struct drm_i915_private *i915 = to_i915(gem->dev); 4079 struct drm_i915_gem_object *obj = to_intel_bo(gem); 4080 struct drm_i915_file_private *fpriv = file->driver_priv; 4081 struct i915_lut_handle *lut, *ln; 4082 4083 mutex_lock(&i915->drm.struct_mutex); 4084 4085 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { 4086 struct i915_gem_context *ctx = lut->ctx; 4087 struct i915_vma *vma; 4088 4089 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); 4090 if (ctx->file_priv != fpriv) 4091 continue; 4092 4093 vma = radix_tree_delete(&ctx->handles_vma, lut->handle); 4094 GEM_BUG_ON(vma->obj != obj); 4095 4096 /* We allow the process to have multiple handles to the same 4097 * vma, in the same fd namespace, by virtue of flink/open. 4098 */ 4099 GEM_BUG_ON(!vma->open_count); 4100 if (!--vma->open_count && !i915_vma_is_ggtt(vma)) 4101 i915_vma_close(vma); 4102 4103 list_del(&lut->obj_link); 4104 list_del(&lut->ctx_link); 4105 4106 #ifdef __linux__ 4107 kmem_cache_free(i915->luts, lut); 4108 #else 4109 pool_put(&i915->luts, lut); 4110 #endif 4111 __i915_gem_object_release_unless_active(obj); 4112 } 4113 4114 mutex_unlock(&i915->drm.struct_mutex); 4115 } 4116 4117 static unsigned long to_wait_timeout(s64 timeout_ns) 4118 { 4119 if (timeout_ns < 0) 4120 return MAX_SCHEDULE_TIMEOUT; 4121 4122 if (timeout_ns == 0) 4123 return 0; 4124 4125 return nsecs_to_jiffies_timeout(timeout_ns); 4126 } 4127 4128 /** 4129 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT 4130 * @dev: drm device pointer 4131 * @data: ioctl data blob 4132 * @file: drm file pointer 4133 * 4134 * Returns 0 if successful, else an error is returned with the remaining time in 4135 * the timeout parameter. 4136 * -ETIME: object is still busy after timeout 4137 * -ERESTARTSYS: signal interrupted the wait 4138 * -ENONENT: object doesn't exist 4139 * Also possible, but rare: 4140 * -EAGAIN: incomplete, restart syscall 4141 * -ENOMEM: damn 4142 * -ENODEV: Internal IRQ fail 4143 * -E?: The add request failed 4144 * 4145 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any 4146 * non-zero timeout parameter the wait ioctl will wait for the given number of 4147 * nanoseconds on an object becoming unbusy. Since the wait itself does so 4148 * without holding struct_mutex the object may become re-busied before this 4149 * function completes. A similar but shorter * race condition exists in the busy 4150 * ioctl 4151 */ 4152 int 4153 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) 4154 { 4155 struct drm_i915_gem_wait *args = data; 4156 struct drm_i915_gem_object *obj; 4157 ktime_t start; 4158 long ret; 4159 4160 if (args->flags != 0) 4161 return -EINVAL; 4162 4163 obj = i915_gem_object_lookup(file, args->bo_handle); 4164 if (!obj) 4165 return -ENOENT; 4166 4167 start = ktime_get(); 4168 4169 ret = i915_gem_object_wait(obj, 4170 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, 4171 to_wait_timeout(args->timeout_ns), 4172 to_rps_client(file)); 4173 4174 if (args->timeout_ns > 0) { 4175 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); 4176 if (args->timeout_ns < 0) 4177 args->timeout_ns = 0; 4178 4179 /* 4180 * Apparently ktime isn't accurate enough and occasionally has a 4181 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch 4182 * things up to make the test happy. We allow up to 1 jiffy. 4183 * 4184 * This is a regression from the timespec->ktime conversion. 4185 */ 4186 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) 4187 args->timeout_ns = 0; 4188 4189 /* Asked to wait beyond the jiffie/scheduler precision? */ 4190 if (ret == -ETIME && args->timeout_ns) 4191 ret = -EAGAIN; 4192 } 4193 4194 i915_gem_object_put(obj); 4195 return ret; 4196 } 4197 4198 static long wait_for_timeline(struct i915_timeline *tl, 4199 unsigned int flags, long timeout) 4200 { 4201 struct i915_request *rq; 4202 4203 rq = i915_gem_active_get_unlocked(&tl->last_request); 4204 if (!rq) 4205 return timeout; 4206 4207 /* 4208 * "Race-to-idle". 4209 * 4210 * Switching to the kernel context is often used a synchronous 4211 * step prior to idling, e.g. in suspend for flushing all 4212 * current operations to memory before sleeping. These we 4213 * want to complete as quickly as possible to avoid prolonged 4214 * stalls, so allow the gpu to boost to maximum clocks. 4215 */ 4216 if (flags & I915_WAIT_FOR_IDLE_BOOST) 4217 gen6_rps_boost(rq, NULL); 4218 4219 timeout = i915_request_wait(rq, flags, timeout); 4220 i915_request_put(rq); 4221 4222 return timeout; 4223 } 4224 4225 static int wait_for_engines(struct drm_i915_private *i915) 4226 { 4227 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { 4228 dev_err(i915->drm.dev, 4229 "Failed to idle engines, declaring wedged!\n"); 4230 GEM_TRACE_DUMP(); 4231 i915_gem_set_wedged(i915); 4232 return -EIO; 4233 } 4234 4235 return 0; 4236 } 4237 4238 int i915_gem_wait_for_idle(struct drm_i915_private *i915, 4239 unsigned int flags, long timeout) 4240 { 4241 GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", 4242 flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", 4243 timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); 4244 4245 /* If the device is asleep, we have no requests outstanding */ 4246 if (!READ_ONCE(i915->gt.awake)) 4247 return 0; 4248 4249 if (flags & I915_WAIT_LOCKED) { 4250 struct i915_timeline *tl; 4251 int err; 4252 4253 lockdep_assert_held(&i915->drm.struct_mutex); 4254 4255 list_for_each_entry(tl, &i915->gt.timelines, link) { 4256 timeout = wait_for_timeline(tl, flags, timeout); 4257 if (timeout < 0) 4258 return timeout; 4259 } 4260 4261 err = wait_for_engines(i915); 4262 if (err) 4263 return err; 4264 4265 i915_retire_requests(i915); 4266 GEM_BUG_ON(i915->gt.active_requests); 4267 } else { 4268 struct intel_engine_cs *engine; 4269 enum intel_engine_id id; 4270 4271 for_each_engine(engine, i915, id) { 4272 struct i915_timeline *tl = &engine->timeline; 4273 4274 timeout = wait_for_timeline(tl, flags, timeout); 4275 if (timeout < 0) 4276 return timeout; 4277 } 4278 } 4279 4280 return 0; 4281 } 4282 4283 static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) 4284 { 4285 /* 4286 * We manually flush the CPU domain so that we can override and 4287 * force the flush for the display, and perform it asyncrhonously. 4288 */ 4289 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); 4290 if (obj->cache_dirty) 4291 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); 4292 obj->write_domain = 0; 4293 } 4294 4295 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) 4296 { 4297 if (!READ_ONCE(obj->pin_global)) 4298 return; 4299 4300 mutex_lock(&obj->base.dev->struct_mutex); 4301 __i915_gem_object_flush_for_display(obj); 4302 mutex_unlock(&obj->base.dev->struct_mutex); 4303 } 4304 4305 /** 4306 * Moves a single object to the WC read, and possibly write domain. 4307 * @obj: object to act on 4308 * @write: ask for write access or read only 4309 * 4310 * This function returns when the move is complete, including waiting on 4311 * flushes to occur. 4312 */ 4313 int 4314 i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) 4315 { 4316 int ret; 4317 4318 lockdep_assert_held(&obj->base.dev->struct_mutex); 4319 4320 ret = i915_gem_object_wait(obj, 4321 I915_WAIT_INTERRUPTIBLE | 4322 I915_WAIT_LOCKED | 4323 (write ? I915_WAIT_ALL : 0), 4324 MAX_SCHEDULE_TIMEOUT, 4325 NULL); 4326 if (ret) 4327 return ret; 4328 4329 if (obj->write_domain == I915_GEM_DOMAIN_WC) 4330 return 0; 4331 4332 /* Flush and acquire obj->pages so that we are coherent through 4333 * direct access in memory with previous cached writes through 4334 * shmemfs and that our cache domain tracking remains valid. 4335 * For example, if the obj->filp was moved to swap without us 4336 * being notified and releasing the pages, we would mistakenly 4337 * continue to assume that the obj remained out of the CPU cached 4338 * domain. 4339 */ 4340 ret = i915_gem_object_pin_pages(obj); 4341 if (ret) 4342 return ret; 4343 4344 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); 4345 4346 /* Serialise direct access to this object with the barriers for 4347 * coherent writes from the GPU, by effectively invalidating the 4348 * WC domain upon first access. 4349 */ 4350 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) 4351 mb(); 4352 4353 /* It should now be out of any other write domains, and we can update 4354 * the domain values for our changes. 4355 */ 4356 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); 4357 obj->read_domains |= I915_GEM_DOMAIN_WC; 4358 if (write) { 4359 obj->read_domains = I915_GEM_DOMAIN_WC; 4360 obj->write_domain = I915_GEM_DOMAIN_WC; 4361 obj->mm.dirty = true; 4362 } 4363 4364 i915_gem_object_unpin_pages(obj); 4365 return 0; 4366 } 4367 4368 /** 4369 * Moves a single object to the GTT read, and possibly write domain. 4370 * @obj: object to act on 4371 * @write: ask for write access or read only 4372 * 4373 * This function returns when the move is complete, including waiting on 4374 * flushes to occur. 4375 */ 4376 int 4377 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) 4378 { 4379 int ret; 4380 4381 lockdep_assert_held(&obj->base.dev->struct_mutex); 4382 4383 ret = i915_gem_object_wait(obj, 4384 I915_WAIT_INTERRUPTIBLE | 4385 I915_WAIT_LOCKED | 4386 (write ? I915_WAIT_ALL : 0), 4387 MAX_SCHEDULE_TIMEOUT, 4388 NULL); 4389 if (ret) 4390 return ret; 4391 4392 if (obj->write_domain == I915_GEM_DOMAIN_GTT) 4393 return 0; 4394 4395 /* Flush and acquire obj->pages so that we are coherent through 4396 * direct access in memory with previous cached writes through 4397 * shmemfs and that our cache domain tracking remains valid. 4398 * For example, if the obj->filp was moved to swap without us 4399 * being notified and releasing the pages, we would mistakenly 4400 * continue to assume that the obj remained out of the CPU cached 4401 * domain. 4402 */ 4403 ret = i915_gem_object_pin_pages(obj); 4404 if (ret) 4405 return ret; 4406 4407 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); 4408 4409 /* Serialise direct access to this object with the barriers for 4410 * coherent writes from the GPU, by effectively invalidating the 4411 * GTT domain upon first access. 4412 */ 4413 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) 4414 mb(); 4415 4416 /* It should now be out of any other write domains, and we can update 4417 * the domain values for our changes. 4418 */ 4419 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); 4420 obj->read_domains |= I915_GEM_DOMAIN_GTT; 4421 if (write) { 4422 obj->read_domains = I915_GEM_DOMAIN_GTT; 4423 obj->write_domain = I915_GEM_DOMAIN_GTT; 4424 obj->mm.dirty = true; 4425 } 4426 4427 i915_gem_object_unpin_pages(obj); 4428 return 0; 4429 } 4430 4431 /** 4432 * Changes the cache-level of an object across all VMA. 4433 * @obj: object to act on 4434 * @cache_level: new cache level to set for the object 4435 * 4436 * After this function returns, the object will be in the new cache-level 4437 * across all GTT and the contents of the backing storage will be coherent, 4438 * with respect to the new cache-level. In order to keep the backing storage 4439 * coherent for all users, we only allow a single cache level to be set 4440 * globally on the object and prevent it from being changed whilst the 4441 * hardware is reading from the object. That is if the object is currently 4442 * on the scanout it will be set to uncached (or equivalent display 4443 * cache coherency) and all non-MOCS GPU access will also be uncached so 4444 * that all direct access to the scanout remains coherent. 4445 */ 4446 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, 4447 enum i915_cache_level cache_level) 4448 { 4449 struct i915_vma *vma; 4450 int ret; 4451 4452 lockdep_assert_held(&obj->base.dev->struct_mutex); 4453 4454 if (obj->cache_level == cache_level) 4455 return 0; 4456 4457 /* Inspect the list of currently bound VMA and unbind any that would 4458 * be invalid given the new cache-level. This is principally to 4459 * catch the issue of the CS prefetch crossing page boundaries and 4460 * reading an invalid PTE on older architectures. 4461 */ 4462 restart: 4463 list_for_each_entry(vma, &obj->vma_list, obj_link) { 4464 if (!drm_mm_node_allocated(&vma->node)) 4465 continue; 4466 4467 if (i915_vma_is_pinned(vma)) { 4468 DRM_DEBUG("can not change the cache level of pinned objects\n"); 4469 return -EBUSY; 4470 } 4471 4472 if (!i915_vma_is_closed(vma) && 4473 i915_gem_valid_gtt_space(vma, cache_level)) 4474 continue; 4475 4476 ret = i915_vma_unbind(vma); 4477 if (ret) 4478 return ret; 4479 4480 /* As unbinding may affect other elements in the 4481 * obj->vma_list (due to side-effects from retiring 4482 * an active vma), play safe and restart the iterator. 4483 */ 4484 goto restart; 4485 } 4486 4487 /* We can reuse the existing drm_mm nodes but need to change the 4488 * cache-level on the PTE. We could simply unbind them all and 4489 * rebind with the correct cache-level on next use. However since 4490 * we already have a valid slot, dma mapping, pages etc, we may as 4491 * rewrite the PTE in the belief that doing so tramples upon less 4492 * state and so involves less work. 4493 */ 4494 if (obj->bind_count) { 4495 /* Before we change the PTE, the GPU must not be accessing it. 4496 * If we wait upon the object, we know that all the bound 4497 * VMA are no longer active. 4498 */ 4499 ret = i915_gem_object_wait(obj, 4500 I915_WAIT_INTERRUPTIBLE | 4501 I915_WAIT_LOCKED | 4502 I915_WAIT_ALL, 4503 MAX_SCHEDULE_TIMEOUT, 4504 NULL); 4505 if (ret) 4506 return ret; 4507 4508 if (!HAS_LLC(to_i915(obj->base.dev)) && 4509 cache_level != I915_CACHE_NONE) { 4510 /* Access to snoopable pages through the GTT is 4511 * incoherent and on some machines causes a hard 4512 * lockup. Relinquish the CPU mmaping to force 4513 * userspace to refault in the pages and we can 4514 * then double check if the GTT mapping is still 4515 * valid for that pointer access. 4516 */ 4517 i915_gem_release_mmap(obj); 4518 4519 /* As we no longer need a fence for GTT access, 4520 * we can relinquish it now (and so prevent having 4521 * to steal a fence from someone else on the next 4522 * fence request). Note GPU activity would have 4523 * dropped the fence as all snoopable access is 4524 * supposed to be linear. 4525 */ 4526 for_each_ggtt_vma(vma, obj) { 4527 ret = i915_vma_put_fence(vma); 4528 if (ret) 4529 return ret; 4530 } 4531 } else { 4532 /* We either have incoherent backing store and 4533 * so no GTT access or the architecture is fully 4534 * coherent. In such cases, existing GTT mmaps 4535 * ignore the cache bit in the PTE and we can 4536 * rewrite it without confusing the GPU or having 4537 * to force userspace to fault back in its mmaps. 4538 */ 4539 } 4540 4541 list_for_each_entry(vma, &obj->vma_list, obj_link) { 4542 if (!drm_mm_node_allocated(&vma->node)) 4543 continue; 4544 4545 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); 4546 if (ret) 4547 return ret; 4548 } 4549 } 4550 4551 list_for_each_entry(vma, &obj->vma_list, obj_link) 4552 vma->node.color = cache_level; 4553 i915_gem_object_set_cache_coherency(obj, cache_level); 4554 obj->cache_dirty = true; /* Always invalidate stale cachelines */ 4555 4556 return 0; 4557 } 4558 4559 int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, 4560 struct drm_file *file) 4561 { 4562 struct drm_i915_gem_caching *args = data; 4563 struct drm_i915_gem_object *obj; 4564 int err = 0; 4565 4566 rcu_read_lock(); 4567 obj = i915_gem_object_lookup_rcu(file, args->handle); 4568 if (!obj) { 4569 err = -ENOENT; 4570 goto out; 4571 } 4572 4573 switch (obj->cache_level) { 4574 case I915_CACHE_LLC: 4575 case I915_CACHE_L3_LLC: 4576 args->caching = I915_CACHING_CACHED; 4577 break; 4578 4579 case I915_CACHE_WT: 4580 args->caching = I915_CACHING_DISPLAY; 4581 break; 4582 4583 default: 4584 args->caching = I915_CACHING_NONE; 4585 break; 4586 } 4587 out: 4588 rcu_read_unlock(); 4589 return err; 4590 } 4591 4592 int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, 4593 struct drm_file *file) 4594 { 4595 struct drm_i915_private *i915 = to_i915(dev); 4596 struct drm_i915_gem_caching *args = data; 4597 struct drm_i915_gem_object *obj; 4598 enum i915_cache_level level; 4599 int ret = 0; 4600 4601 switch (args->caching) { 4602 case I915_CACHING_NONE: 4603 level = I915_CACHE_NONE; 4604 break; 4605 case I915_CACHING_CACHED: 4606 /* 4607 * Due to a HW issue on BXT A stepping, GPU stores via a 4608 * snooped mapping may leave stale data in a corresponding CPU 4609 * cacheline, whereas normally such cachelines would get 4610 * invalidated. 4611 */ 4612 if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) 4613 return -ENODEV; 4614 4615 level = I915_CACHE_LLC; 4616 break; 4617 case I915_CACHING_DISPLAY: 4618 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; 4619 break; 4620 default: 4621 return -EINVAL; 4622 } 4623 4624 obj = i915_gem_object_lookup(file, args->handle); 4625 if (!obj) 4626 return -ENOENT; 4627 4628 /* 4629 * The caching mode of proxy object is handled by its generator, and 4630 * not allowed to be changed by userspace. 4631 */ 4632 if (i915_gem_object_is_proxy(obj)) { 4633 ret = -ENXIO; 4634 goto out; 4635 } 4636 4637 if (obj->cache_level == level) 4638 goto out; 4639 4640 ret = i915_gem_object_wait(obj, 4641 I915_WAIT_INTERRUPTIBLE, 4642 MAX_SCHEDULE_TIMEOUT, 4643 to_rps_client(file)); 4644 if (ret) 4645 goto out; 4646 4647 ret = i915_mutex_lock_interruptible(dev); 4648 if (ret) 4649 goto out; 4650 4651 ret = i915_gem_object_set_cache_level(obj, level); 4652 mutex_unlock(&dev->struct_mutex); 4653 4654 out: 4655 i915_gem_object_put(obj); 4656 return ret; 4657 } 4658 4659 /* 4660 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from 4661 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined 4662 * (for pageflips). We only flush the caches while preparing the buffer for 4663 * display, the callers are responsible for frontbuffer flush. 4664 */ 4665 struct i915_vma * 4666 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, 4667 u32 alignment, 4668 const struct i915_ggtt_view *view, 4669 unsigned int flags) 4670 { 4671 struct i915_vma *vma; 4672 int ret; 4673 4674 lockdep_assert_held(&obj->base.dev->struct_mutex); 4675 4676 /* Mark the global pin early so that we account for the 4677 * display coherency whilst setting up the cache domains. 4678 */ 4679 obj->pin_global++; 4680 4681 /* The display engine is not coherent with the LLC cache on gen6. As 4682 * a result, we make sure that the pinning that is about to occur is 4683 * done with uncached PTEs. This is lowest common denominator for all 4684 * chipsets. 4685 * 4686 * However for gen6+, we could do better by using the GFDT bit instead 4687 * of uncaching, which would allow us to flush all the LLC-cached data 4688 * with that bit in the PTE to main memory with just one PIPE_CONTROL. 4689 */ 4690 ret = i915_gem_object_set_cache_level(obj, 4691 HAS_WT(to_i915(obj->base.dev)) ? 4692 I915_CACHE_WT : I915_CACHE_NONE); 4693 if (ret) { 4694 vma = ERR_PTR(ret); 4695 goto err_unpin_global; 4696 } 4697 4698 /* As the user may map the buffer once pinned in the display plane 4699 * (e.g. libkms for the bootup splash), we have to ensure that we 4700 * always use map_and_fenceable for all scanout buffers. However, 4701 * it may simply be too big to fit into mappable, in which case 4702 * put it anyway and hope that userspace can cope (but always first 4703 * try to preserve the existing ABI). 4704 */ 4705 vma = ERR_PTR(-ENOSPC); 4706 if ((flags & PIN_MAPPABLE) == 0 && 4707 (!view || view->type == I915_GGTT_VIEW_NORMAL)) 4708 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, 4709 flags | 4710 PIN_MAPPABLE | 4711 PIN_NONBLOCK); 4712 if (IS_ERR(vma)) 4713 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); 4714 if (IS_ERR(vma)) 4715 goto err_unpin_global; 4716 4717 vma->display_alignment = max_t(u64, vma->display_alignment, alignment); 4718 4719 __i915_gem_object_flush_for_display(obj); 4720 4721 /* It should now be out of any other write domains, and we can update 4722 * the domain values for our changes. 4723 */ 4724 obj->read_domains |= I915_GEM_DOMAIN_GTT; 4725 4726 return vma; 4727 4728 err_unpin_global: 4729 obj->pin_global--; 4730 return vma; 4731 } 4732 4733 void 4734 i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) 4735 { 4736 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); 4737 4738 if (WARN_ON(vma->obj->pin_global == 0)) 4739 return; 4740 4741 if (--vma->obj->pin_global == 0) 4742 vma->display_alignment = I915_GTT_MIN_ALIGNMENT; 4743 4744 /* Bump the LRU to try and avoid premature eviction whilst flipping */ 4745 i915_gem_object_bump_inactive_ggtt(vma->obj); 4746 4747 i915_vma_unpin(vma); 4748 } 4749 4750 /** 4751 * Moves a single object to the CPU read, and possibly write domain. 4752 * @obj: object to act on 4753 * @write: requesting write or read-only access 4754 * 4755 * This function returns when the move is complete, including waiting on 4756 * flushes to occur. 4757 */ 4758 int 4759 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) 4760 { 4761 int ret; 4762 4763 lockdep_assert_held(&obj->base.dev->struct_mutex); 4764 4765 ret = i915_gem_object_wait(obj, 4766 I915_WAIT_INTERRUPTIBLE | 4767 I915_WAIT_LOCKED | 4768 (write ? I915_WAIT_ALL : 0), 4769 MAX_SCHEDULE_TIMEOUT, 4770 NULL); 4771 if (ret) 4772 return ret; 4773 4774 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); 4775 4776 /* Flush the CPU cache if it's still invalid. */ 4777 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { 4778 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); 4779 obj->read_domains |= I915_GEM_DOMAIN_CPU; 4780 } 4781 4782 /* It should now be out of any other write domains, and we can update 4783 * the domain values for our changes. 4784 */ 4785 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); 4786 4787 /* If we're writing through the CPU, then the GPU read domains will 4788 * need to be invalidated at next use. 4789 */ 4790 if (write) 4791 __start_cpu_write(obj); 4792 4793 return 0; 4794 } 4795 4796 /* Throttle our rendering by waiting until the ring has completed our requests 4797 * emitted over 20 msec ago. 4798 * 4799 * Note that if we were to use the current jiffies each time around the loop, 4800 * we wouldn't escape the function with any frames outstanding if the time to 4801 * render a frame was over 20ms. 4802 * 4803 * This should get us reasonable parallelism between CPU and GPU but also 4804 * relatively low latency when blocking on a particular request to finish. 4805 */ 4806 static int 4807 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) 4808 { 4809 struct drm_i915_private *dev_priv = to_i915(dev); 4810 struct drm_i915_file_private *file_priv = file->driver_priv; 4811 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; 4812 struct i915_request *request, *target = NULL; 4813 long ret; 4814 4815 /* ABI: return -EIO if already wedged */ 4816 if (i915_terminally_wedged(&dev_priv->gpu_error)) 4817 return -EIO; 4818 4819 spin_lock(&file_priv->mm.lock); 4820 list_for_each_entry(request, &file_priv->mm.request_list, client_link) { 4821 if (time_after_eq(request->emitted_jiffies, recent_enough)) 4822 break; 4823 4824 if (target) { 4825 list_del(&target->client_link); 4826 target->file_priv = NULL; 4827 } 4828 4829 target = request; 4830 } 4831 if (target) 4832 i915_request_get(target); 4833 spin_unlock(&file_priv->mm.lock); 4834 4835 if (target == NULL) 4836 return 0; 4837 4838 ret = i915_request_wait(target, 4839 I915_WAIT_INTERRUPTIBLE, 4840 MAX_SCHEDULE_TIMEOUT); 4841 i915_request_put(target); 4842 4843 return ret < 0 ? ret : 0; 4844 } 4845 4846 struct i915_vma * 4847 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, 4848 const struct i915_ggtt_view *view, 4849 u64 size, 4850 u64 alignment, 4851 u64 flags) 4852 { 4853 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 4854 struct i915_address_space *vm = &dev_priv->ggtt.vm; 4855 4856 return i915_gem_object_pin(obj, vm, view, size, alignment, 4857 flags | PIN_GLOBAL); 4858 } 4859 4860 struct i915_vma * 4861 i915_gem_object_pin(struct drm_i915_gem_object *obj, 4862 struct i915_address_space *vm, 4863 const struct i915_ggtt_view *view, 4864 u64 size, 4865 u64 alignment, 4866 u64 flags) 4867 { 4868 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 4869 struct i915_vma *vma; 4870 int ret; 4871 4872 lockdep_assert_held(&obj->base.dev->struct_mutex); 4873 4874 if (flags & PIN_MAPPABLE && 4875 (!view || view->type == I915_GGTT_VIEW_NORMAL)) { 4876 /* If the required space is larger than the available 4877 * aperture, we will not able to find a slot for the 4878 * object and unbinding the object now will be in 4879 * vain. Worse, doing so may cause us to ping-pong 4880 * the object in and out of the Global GTT and 4881 * waste a lot of cycles under the mutex. 4882 */ 4883 if (obj->base.size > dev_priv->ggtt.mappable_end) 4884 return ERR_PTR(-E2BIG); 4885 4886 /* If NONBLOCK is set the caller is optimistically 4887 * trying to cache the full object within the mappable 4888 * aperture, and *must* have a fallback in place for 4889 * situations where we cannot bind the object. We 4890 * can be a little more lax here and use the fallback 4891 * more often to avoid costly migrations of ourselves 4892 * and other objects within the aperture. 4893 * 4894 * Half-the-aperture is used as a simple heuristic. 4895 * More interesting would to do search for a free 4896 * block prior to making the commitment to unbind. 4897 * That caters for the self-harm case, and with a 4898 * little more heuristics (e.g. NOFAULT, NOEVICT) 4899 * we could try to minimise harm to others. 4900 */ 4901 if (flags & PIN_NONBLOCK && 4902 obj->base.size > dev_priv->ggtt.mappable_end / 2) 4903 return ERR_PTR(-ENOSPC); 4904 } 4905 4906 vma = i915_vma_instance(obj, vm, view); 4907 if (unlikely(IS_ERR(vma))) 4908 return vma; 4909 4910 if (i915_vma_misplaced(vma, size, alignment, flags)) { 4911 if (flags & PIN_NONBLOCK) { 4912 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) 4913 return ERR_PTR(-ENOSPC); 4914 4915 if (flags & PIN_MAPPABLE && 4916 vma->fence_size > dev_priv->ggtt.mappable_end / 2) 4917 return ERR_PTR(-ENOSPC); 4918 } 4919 4920 WARN(i915_vma_is_pinned(vma), 4921 "bo is already pinned in ggtt with incorrect alignment:" 4922 " offset=%08x, req.alignment=%llx," 4923 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", 4924 i915_ggtt_offset(vma), alignment, 4925 !!(flags & PIN_MAPPABLE), 4926 i915_vma_is_map_and_fenceable(vma)); 4927 ret = i915_vma_unbind(vma); 4928 if (ret) 4929 return ERR_PTR(ret); 4930 } 4931 4932 ret = i915_vma_pin(vma, size, alignment, flags); 4933 if (ret) 4934 return ERR_PTR(ret); 4935 4936 return vma; 4937 } 4938 4939 static __always_inline unsigned int __busy_read_flag(unsigned int id) 4940 { 4941 /* Note that we could alias engines in the execbuf API, but 4942 * that would be very unwise as it prevents userspace from 4943 * fine control over engine selection. Ahem. 4944 * 4945 * This should be something like EXEC_MAX_ENGINE instead of 4946 * I915_NUM_ENGINES. 4947 */ 4948 BUILD_BUG_ON(I915_NUM_ENGINES > 16); 4949 return 0x10000 << id; 4950 } 4951 4952 static __always_inline unsigned int __busy_write_id(unsigned int id) 4953 { 4954 /* The uABI guarantees an active writer is also amongst the read 4955 * engines. This would be true if we accessed the activity tracking 4956 * under the lock, but as we perform the lookup of the object and 4957 * its activity locklessly we can not guarantee that the last_write 4958 * being active implies that we have set the same engine flag from 4959 * last_read - hence we always set both read and write busy for 4960 * last_write. 4961 */ 4962 return id | __busy_read_flag(id); 4963 } 4964 4965 static __always_inline unsigned int 4966 __busy_set_if_active(const struct dma_fence *fence, 4967 unsigned int (*flag)(unsigned int id)) 4968 { 4969 struct i915_request *rq; 4970 4971 /* We have to check the current hw status of the fence as the uABI 4972 * guarantees forward progress. We could rely on the idle worker 4973 * to eventually flush us, but to minimise latency just ask the 4974 * hardware. 4975 * 4976 * Note we only report on the status of native fences. 4977 */ 4978 if (!dma_fence_is_i915(fence)) 4979 return 0; 4980 4981 /* opencode to_request() in order to avoid const warnings */ 4982 rq = container_of(fence, struct i915_request, fence); 4983 if (i915_request_completed(rq)) 4984 return 0; 4985 4986 return flag(rq->engine->uabi_id); 4987 } 4988 4989 static __always_inline unsigned int 4990 busy_check_reader(const struct dma_fence *fence) 4991 { 4992 return __busy_set_if_active(fence, __busy_read_flag); 4993 } 4994 4995 static __always_inline unsigned int 4996 busy_check_writer(const struct dma_fence *fence) 4997 { 4998 if (!fence) 4999 return 0; 5000 5001 return __busy_set_if_active(fence, __busy_write_id); 5002 } 5003 5004 int 5005 i915_gem_busy_ioctl(struct drm_device *dev, void *data, 5006 struct drm_file *file) 5007 { 5008 struct drm_i915_gem_busy *args = data; 5009 struct drm_i915_gem_object *obj; 5010 struct reservation_object_list *list; 5011 unsigned int seq; 5012 int err; 5013 5014 err = -ENOENT; 5015 rcu_read_lock(); 5016 obj = i915_gem_object_lookup_rcu(file, args->handle); 5017 if (!obj) 5018 goto out; 5019 5020 /* A discrepancy here is that we do not report the status of 5021 * non-i915 fences, i.e. even though we may report the object as idle, 5022 * a call to set-domain may still stall waiting for foreign rendering. 5023 * This also means that wait-ioctl may report an object as busy, 5024 * where busy-ioctl considers it idle. 5025 * 5026 * We trade the ability to warn of foreign fences to report on which 5027 * i915 engines are active for the object. 5028 * 5029 * Alternatively, we can trade that extra information on read/write 5030 * activity with 5031 * args->busy = 5032 * !reservation_object_test_signaled_rcu(obj->resv, true); 5033 * to report the overall busyness. This is what the wait-ioctl does. 5034 * 5035 */ 5036 retry: 5037 seq = raw_read_seqcount(&obj->resv->seq); 5038 5039 /* Translate the exclusive fence to the READ *and* WRITE engine */ 5040 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); 5041 5042 /* Translate shared fences to READ set of engines */ 5043 list = rcu_dereference(obj->resv->fence); 5044 if (list) { 5045 unsigned int shared_count = list->shared_count, i; 5046 5047 for (i = 0; i < shared_count; ++i) { 5048 struct dma_fence *fence = 5049 rcu_dereference(list->shared[i]); 5050 5051 args->busy |= busy_check_reader(fence); 5052 } 5053 } 5054 5055 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) 5056 goto retry; 5057 5058 err = 0; 5059 out: 5060 rcu_read_unlock(); 5061 return err; 5062 } 5063 5064 int 5065 i915_gem_throttle_ioctl(struct drm_device *dev, void *data, 5066 struct drm_file *file_priv) 5067 { 5068 return i915_gem_ring_throttle(dev, file_priv); 5069 } 5070 5071 int 5072 i915_gem_madvise_ioctl(struct drm_device *dev, void *data, 5073 struct drm_file *file_priv) 5074 { 5075 struct drm_i915_private *dev_priv = to_i915(dev); 5076 struct drm_i915_gem_madvise *args = data; 5077 struct drm_i915_gem_object *obj; 5078 int err; 5079 5080 switch (args->madv) { 5081 case I915_MADV_DONTNEED: 5082 case I915_MADV_WILLNEED: 5083 break; 5084 default: 5085 return -EINVAL; 5086 } 5087 5088 obj = i915_gem_object_lookup(file_priv, args->handle); 5089 if (!obj) 5090 return -ENOENT; 5091 5092 err = mutex_lock_interruptible(&obj->mm.lock); 5093 if (err) 5094 goto out; 5095 5096 if (i915_gem_object_has_pages(obj) && 5097 i915_gem_object_is_tiled(obj) && 5098 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { 5099 if (obj->mm.madv == I915_MADV_WILLNEED) { 5100 GEM_BUG_ON(!obj->mm.quirked); 5101 __i915_gem_object_unpin_pages(obj); 5102 obj->mm.quirked = false; 5103 } 5104 if (args->madv == I915_MADV_WILLNEED) { 5105 GEM_BUG_ON(obj->mm.quirked); 5106 __i915_gem_object_pin_pages(obj); 5107 obj->mm.quirked = true; 5108 } 5109 } 5110 5111 if (obj->mm.madv != __I915_MADV_PURGED) 5112 obj->mm.madv = args->madv; 5113 5114 /* if the object is no longer attached, discard its backing storage */ 5115 if (obj->mm.madv == I915_MADV_DONTNEED && 5116 !i915_gem_object_has_pages(obj)) 5117 i915_gem_object_truncate(obj); 5118 5119 args->retained = obj->mm.madv != __I915_MADV_PURGED; 5120 mutex_unlock(&obj->mm.lock); 5121 5122 out: 5123 i915_gem_object_put(obj); 5124 return err; 5125 } 5126 5127 static void 5128 frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request) 5129 { 5130 struct drm_i915_gem_object *obj = 5131 container_of(active, typeof(*obj), frontbuffer_write); 5132 5133 intel_fb_obj_flush(obj, ORIGIN_CS); 5134 } 5135 5136 void i915_gem_object_init(struct drm_i915_gem_object *obj, 5137 const struct drm_i915_gem_object_ops *ops) 5138 { 5139 rw_init(&obj->mm.lock, "mmlk"); 5140 5141 INIT_LIST_HEAD(&obj->vma_list); 5142 INIT_LIST_HEAD(&obj->lut_list); 5143 INIT_LIST_HEAD(&obj->batch_pool_link); 5144 5145 obj->ops = ops; 5146 5147 reservation_object_init(&obj->__builtin_resv); 5148 obj->resv = &obj->__builtin_resv; 5149 5150 obj->frontbuffer_ggtt_origin = ORIGIN_GTT; 5151 init_request_active(&obj->frontbuffer_write, frontbuffer_retire); 5152 5153 obj->mm.madv = I915_MADV_WILLNEED; 5154 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); 5155 rw_init(&obj->mm.get_page.lock, "mmget"); 5156 5157 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size); 5158 } 5159 5160 static const struct drm_i915_gem_object_ops i915_gem_object_ops = { 5161 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | 5162 I915_GEM_OBJECT_IS_SHRINKABLE, 5163 5164 .get_pages = i915_gem_object_get_pages_gtt, 5165 .put_pages = i915_gem_object_put_pages_gtt, 5166 5167 .pwrite = i915_gem_object_pwrite_gtt, 5168 }; 5169 5170 static int i915_gem_object_create_shmem(struct drm_device *dev, 5171 struct drm_gem_object *obj, 5172 size_t size) 5173 { 5174 #ifdef __linux__ 5175 struct drm_i915_private *i915 = to_i915(dev); 5176 unsigned long flags = VM_NORESERVE; 5177 struct file *filp; 5178 5179 drm_gem_private_object_init(dev, obj, size); 5180 5181 if (i915->mm.gemfs) 5182 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, 5183 flags); 5184 else 5185 filp = shmem_file_setup("i915", size, flags); 5186 5187 if (IS_ERR(filp)) 5188 return PTR_ERR(filp); 5189 5190 obj->filp = filp; 5191 5192 return 0; 5193 #else 5194 drm_gem_private_object_init(dev, obj, size); 5195 5196 return 0; 5197 #endif 5198 } 5199 5200 struct drm_i915_gem_object * 5201 i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) 5202 { 5203 struct drm_i915_gem_object *obj; 5204 struct address_space *mapping; 5205 unsigned int cache_level; 5206 gfp_t mask; 5207 int ret; 5208 5209 /* There is a prevalence of the assumption that we fit the object's 5210 * page count inside a 32bit _signed_ variable. Let's document this and 5211 * catch if we ever need to fix it. In the meantime, if you do spot 5212 * such a local variable, please consider fixing! 5213 */ 5214 if (size >> PAGE_SHIFT > INT_MAX) 5215 return ERR_PTR(-E2BIG); 5216 5217 if (overflows_type(size, obj->base.size)) 5218 return ERR_PTR(-E2BIG); 5219 5220 obj = i915_gem_object_alloc(dev_priv); 5221 if (obj == NULL) 5222 return ERR_PTR(-ENOMEM); 5223 5224 #ifdef __linux__ 5225 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); 5226 #else 5227 ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size); 5228 #endif 5229 if (ret) 5230 goto fail; 5231 5232 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; 5233 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { 5234 /* 965gm cannot relocate objects above 4GiB. */ 5235 mask &= ~__GFP_HIGHMEM; 5236 mask |= __GFP_DMA32; 5237 } 5238 5239 #ifdef __linux__ 5240 mapping = obj->base.filp->f_mapping; 5241 mapping_set_gfp_mask(mapping, mask); 5242 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); 5243 #endif 5244 5245 i915_gem_object_init(obj, &i915_gem_object_ops); 5246 5247 obj->write_domain = I915_GEM_DOMAIN_CPU; 5248 obj->read_domains = I915_GEM_DOMAIN_CPU; 5249 5250 if (HAS_LLC(dev_priv)) 5251 /* On some devices, we can have the GPU use the LLC (the CPU 5252 * cache) for about a 10% performance improvement 5253 * compared to uncached. Graphics requests other than 5254 * display scanout are coherent with the CPU in 5255 * accessing this cache. This means in this mode we 5256 * don't need to clflush on the CPU side, and on the 5257 * GPU side we only need to flush internal caches to 5258 * get data visible to the CPU. 5259 * 5260 * However, we maintain the display planes as UC, and so 5261 * need to rebind when first used as such. 5262 */ 5263 cache_level = I915_CACHE_LLC; 5264 else 5265 cache_level = I915_CACHE_NONE; 5266 5267 i915_gem_object_set_cache_coherency(obj, cache_level); 5268 5269 trace_i915_gem_object_create(obj); 5270 5271 return obj; 5272 5273 fail: 5274 i915_gem_object_free(obj); 5275 return ERR_PTR(ret); 5276 } 5277 5278 static bool discard_backing_storage(struct drm_i915_gem_object *obj) 5279 { 5280 /* If we are the last user of the backing storage (be it shmemfs 5281 * pages or stolen etc), we know that the pages are going to be 5282 * immediately released. In this case, we can then skip copying 5283 * back the contents from the GPU. 5284 */ 5285 5286 if (obj->mm.madv != I915_MADV_WILLNEED) 5287 return false; 5288 5289 if (obj->base.filp == NULL) 5290 return true; 5291 5292 /* At first glance, this looks racy, but then again so would be 5293 * userspace racing mmap against close. However, the first external 5294 * reference to the filp can only be obtained through the 5295 * i915_gem_mmap_ioctl() which safeguards us against the user 5296 * acquiring such a reference whilst we are in the middle of 5297 * freeing the object. 5298 */ 5299 return atomic_long_read(&obj->base.filp->f_count) == 1; 5300 } 5301 5302 static void __i915_gem_free_objects(struct drm_i915_private *i915, 5303 struct llist_node *freed) 5304 { 5305 struct drm_i915_gem_object *obj, *on; 5306 5307 intel_runtime_pm_get(i915); 5308 llist_for_each_entry_safe(obj, on, freed, freed) { 5309 struct i915_vma *vma, *vn; 5310 5311 trace_i915_gem_object_destroy(obj); 5312 5313 mutex_lock(&i915->drm.struct_mutex); 5314 5315 GEM_BUG_ON(i915_gem_object_is_active(obj)); 5316 list_for_each_entry_safe(vma, vn, 5317 &obj->vma_list, obj_link) { 5318 GEM_BUG_ON(i915_vma_is_active(vma)); 5319 vma->flags &= ~I915_VMA_PIN_MASK; 5320 i915_vma_destroy(vma); 5321 } 5322 GEM_BUG_ON(!list_empty(&obj->vma_list)); 5323 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); 5324 5325 /* This serializes freeing with the shrinker. Since the free 5326 * is delayed, first by RCU then by the workqueue, we want the 5327 * shrinker to be able to free pages of unreferenced objects, 5328 * or else we may oom whilst there are plenty of deferred 5329 * freed objects. 5330 */ 5331 if (i915_gem_object_has_pages(obj)) { 5332 spin_lock(&i915->mm.obj_lock); 5333 list_del_init(&obj->mm.link); 5334 spin_unlock(&i915->mm.obj_lock); 5335 } 5336 5337 mutex_unlock(&i915->drm.struct_mutex); 5338 5339 GEM_BUG_ON(obj->bind_count); 5340 GEM_BUG_ON(obj->userfault_count); 5341 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); 5342 GEM_BUG_ON(!list_empty(&obj->lut_list)); 5343 5344 if (obj->ops->release) 5345 obj->ops->release(obj); 5346 5347 if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) 5348 atomic_set(&obj->mm.pages_pin_count, 0); 5349 __i915_gem_object_put_pages(obj, I915_MM_NORMAL); 5350 GEM_BUG_ON(i915_gem_object_has_pages(obj)); 5351 5352 #ifdef notyet 5353 if (obj->base.import_attach) 5354 drm_prime_gem_destroy(&obj->base, NULL); 5355 #endif 5356 5357 reservation_object_fini(&obj->__builtin_resv); 5358 drm_gem_object_release(&obj->base); 5359 i915_gem_info_remove_obj(i915, obj->base.size); 5360 5361 kfree(obj->bit_17); 5362 i915_gem_object_free(obj); 5363 5364 GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); 5365 atomic_dec(&i915->mm.free_count); 5366 5367 if (on) 5368 cond_resched(); 5369 } 5370 intel_runtime_pm_put(i915); 5371 } 5372 5373 static void i915_gem_flush_free_objects(struct drm_i915_private *i915) 5374 { 5375 struct llist_node *freed; 5376 5377 /* Free the oldest, most stale object to keep the free_list short */ 5378 freed = NULL; 5379 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ 5380 /* Only one consumer of llist_del_first() allowed */ 5381 spin_lock(&i915->mm.free_lock); 5382 freed = llist_del_first(&i915->mm.free_list); 5383 spin_unlock(&i915->mm.free_lock); 5384 } 5385 if (unlikely(freed)) { 5386 freed->next = NULL; 5387 __i915_gem_free_objects(i915, freed); 5388 } 5389 } 5390 5391 static void __i915_gem_free_work(struct work_struct *work) 5392 { 5393 struct drm_i915_private *i915 = 5394 container_of(work, struct drm_i915_private, mm.free_work); 5395 struct llist_node *freed; 5396 5397 /* 5398 * All file-owned VMA should have been released by this point through 5399 * i915_gem_close_object(), or earlier by i915_gem_context_close(). 5400 * However, the object may also be bound into the global GTT (e.g. 5401 * older GPUs without per-process support, or for direct access through 5402 * the GTT either for the user or for scanout). Those VMA still need to 5403 * unbound now. 5404 */ 5405 5406 spin_lock(&i915->mm.free_lock); 5407 while ((freed = llist_del_all(&i915->mm.free_list))) { 5408 spin_unlock(&i915->mm.free_lock); 5409 5410 __i915_gem_free_objects(i915, freed); 5411 if (drm_need_resched()) 5412 return; 5413 5414 spin_lock(&i915->mm.free_lock); 5415 } 5416 spin_unlock(&i915->mm.free_lock); 5417 } 5418 5419 static void __i915_gem_free_object_rcu(struct rcu_head *head) 5420 { 5421 struct drm_i915_gem_object *obj = 5422 container_of(head, typeof(*obj), rcu); 5423 struct drm_i915_private *i915 = to_i915(obj->base.dev); 5424 5425 /* 5426 * Since we require blocking on struct_mutex to unbind the freed 5427 * object from the GPU before releasing resources back to the 5428 * system, we can not do that directly from the RCU callback (which may 5429 * be a softirq context), but must instead then defer that work onto a 5430 * kthread. We use the RCU callback rather than move the freed object 5431 * directly onto the work queue so that we can mix between using the 5432 * worker and performing frees directly from subsequent allocations for 5433 * crude but effective memory throttling. 5434 */ 5435 if (llist_add(&obj->freed, &i915->mm.free_list)) 5436 queue_work(i915->wq, &i915->mm.free_work); 5437 } 5438 5439 void i915_gem_free_object(struct drm_gem_object *gem_obj) 5440 { 5441 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); 5442 5443 if (obj->mm.quirked) 5444 __i915_gem_object_unpin_pages(obj); 5445 5446 if (discard_backing_storage(obj)) 5447 obj->mm.madv = I915_MADV_DONTNEED; 5448 5449 /* 5450 * Before we free the object, make sure any pure RCU-only 5451 * read-side critical sections are complete, e.g. 5452 * i915_gem_busy_ioctl(). For the corresponding synchronized 5453 * lookup see i915_gem_object_lookup_rcu(). 5454 */ 5455 atomic_inc(&to_i915(obj->base.dev)->mm.free_count); 5456 call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 5457 } 5458 5459 void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) 5460 { 5461 lockdep_assert_held(&obj->base.dev->struct_mutex); 5462 5463 if (!i915_gem_object_has_active_reference(obj) && 5464 i915_gem_object_is_active(obj)) 5465 i915_gem_object_set_active_reference(obj); 5466 else 5467 i915_gem_object_put(obj); 5468 } 5469 5470 void i915_gem_sanitize(struct drm_i915_private *i915) 5471 { 5472 int err; 5473 5474 GEM_TRACE("\n"); 5475 5476 mutex_lock(&i915->drm.struct_mutex); 5477 5478 intel_runtime_pm_get(i915); 5479 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); 5480 5481 /* 5482 * As we have just resumed the machine and woken the device up from 5483 * deep PCI sleep (presumably D3_cold), assume the HW has been reset 5484 * back to defaults, recovering from whatever wedged state we left it 5485 * in and so worth trying to use the device once more. 5486 */ 5487 if (i915_terminally_wedged(&i915->gpu_error)) 5488 i915_gem_unset_wedged(i915); 5489 5490 /* 5491 * If we inherit context state from the BIOS or earlier occupants 5492 * of the GPU, the GPU may be in an inconsistent state when we 5493 * try to take over. The only way to remove the earlier state 5494 * is by resetting. However, resetting on earlier gen is tricky as 5495 * it may impact the display and we are uncertain about the stability 5496 * of the reset, so this could be applied to even earlier gen. 5497 */ 5498 err = -ENODEV; 5499 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915)) 5500 err = WARN_ON(intel_gpu_reset(i915, ALL_ENGINES)); 5501 if (!err) 5502 intel_engines_sanitize(i915); 5503 5504 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); 5505 intel_runtime_pm_put(i915); 5506 5507 i915_gem_contexts_lost(i915); 5508 mutex_unlock(&i915->drm.struct_mutex); 5509 } 5510 5511 int i915_gem_suspend(struct drm_i915_private *i915) 5512 { 5513 int ret; 5514 5515 GEM_TRACE("\n"); 5516 5517 intel_runtime_pm_get(i915); 5518 intel_suspend_gt_powersave(i915); 5519 5520 mutex_lock(&i915->drm.struct_mutex); 5521 5522 /* 5523 * We have to flush all the executing contexts to main memory so 5524 * that they can saved in the hibernation image. To ensure the last 5525 * context image is coherent, we have to switch away from it. That 5526 * leaves the i915->kernel_context still active when 5527 * we actually suspend, and its image in memory may not match the GPU 5528 * state. Fortunately, the kernel_context is disposable and we do 5529 * not rely on its state. 5530 */ 5531 if (!i915_terminally_wedged(&i915->gpu_error)) { 5532 ret = i915_gem_switch_to_kernel_context(i915); 5533 if (ret) 5534 goto err_unlock; 5535 5536 ret = i915_gem_wait_for_idle(i915, 5537 I915_WAIT_INTERRUPTIBLE | 5538 I915_WAIT_LOCKED | 5539 I915_WAIT_FOR_IDLE_BOOST, 5540 MAX_SCHEDULE_TIMEOUT); 5541 if (ret && ret != -EIO) 5542 goto err_unlock; 5543 5544 assert_kernel_context_is_current(i915); 5545 } 5546 i915_retire_requests(i915); /* ensure we flush after wedging */ 5547 5548 mutex_unlock(&i915->drm.struct_mutex); 5549 5550 intel_uc_suspend(i915); 5551 5552 cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work); 5553 cancel_delayed_work_sync(&i915->gt.retire_work); 5554 5555 /* 5556 * As the idle_work is rearming if it detects a race, play safe and 5557 * repeat the flush until it is definitely idle. 5558 */ 5559 drain_delayed_work(&i915->gt.idle_work); 5560 5561 /* 5562 * Assert that we successfully flushed all the work and 5563 * reset the GPU back to its idle, low power state. 5564 */ 5565 WARN_ON(i915->gt.awake); 5566 if (WARN_ON(!intel_engines_are_idle(i915))) 5567 i915_gem_set_wedged(i915); /* no hope, discard everything */ 5568 5569 intel_runtime_pm_put(i915); 5570 return 0; 5571 5572 err_unlock: 5573 mutex_unlock(&i915->drm.struct_mutex); 5574 intel_runtime_pm_put(i915); 5575 return ret; 5576 } 5577 5578 void i915_gem_suspend_late(struct drm_i915_private *i915) 5579 { 5580 struct drm_i915_gem_object *obj; 5581 struct list_head *phases[] = { 5582 &i915->mm.unbound_list, 5583 &i915->mm.bound_list, 5584 NULL 5585 }, **phase; 5586 5587 /* 5588 * Neither the BIOS, ourselves or any other kernel 5589 * expects the system to be in execlists mode on startup, 5590 * so we need to reset the GPU back to legacy mode. And the only 5591 * known way to disable logical contexts is through a GPU reset. 5592 * 5593 * So in order to leave the system in a known default configuration, 5594 * always reset the GPU upon unload and suspend. Afterwards we then 5595 * clean up the GEM state tracking, flushing off the requests and 5596 * leaving the system in a known idle state. 5597 * 5598 * Note that is of the upmost importance that the GPU is idle and 5599 * all stray writes are flushed *before* we dismantle the backing 5600 * storage for the pinned objects. 5601 * 5602 * However, since we are uncertain that resetting the GPU on older 5603 * machines is a good idea, we don't - just in case it leaves the 5604 * machine in an unusable condition. 5605 */ 5606 5607 mutex_lock(&i915->drm.struct_mutex); 5608 for (phase = phases; *phase; phase++) { 5609 list_for_each_entry(obj, *phase, mm.link) 5610 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); 5611 } 5612 mutex_unlock(&i915->drm.struct_mutex); 5613 5614 intel_uc_sanitize(i915); 5615 i915_gem_sanitize(i915); 5616 } 5617 5618 void i915_gem_resume(struct drm_i915_private *i915) 5619 { 5620 GEM_TRACE("\n"); 5621 5622 WARN_ON(i915->gt.awake); 5623 5624 mutex_lock(&i915->drm.struct_mutex); 5625 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); 5626 5627 i915_gem_restore_gtt_mappings(i915); 5628 i915_gem_restore_fences(i915); 5629 5630 /* 5631 * As we didn't flush the kernel context before suspend, we cannot 5632 * guarantee that the context image is complete. So let's just reset 5633 * it and start again. 5634 */ 5635 i915->gt.resume(i915); 5636 5637 if (i915_gem_init_hw(i915)) 5638 goto err_wedged; 5639 5640 intel_uc_resume(i915); 5641 5642 /* Always reload a context for powersaving. */ 5643 if (i915_gem_switch_to_kernel_context(i915)) 5644 goto err_wedged; 5645 5646 out_unlock: 5647 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); 5648 mutex_unlock(&i915->drm.struct_mutex); 5649 return; 5650 5651 err_wedged: 5652 if (!i915_terminally_wedged(&i915->gpu_error)) { 5653 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); 5654 i915_gem_set_wedged(i915); 5655 } 5656 goto out_unlock; 5657 } 5658 5659 void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) 5660 { 5661 if (INTEL_GEN(dev_priv) < 5 || 5662 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) 5663 return; 5664 5665 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | 5666 DISP_TILE_SURFACE_SWIZZLING); 5667 5668 if (IS_GEN5(dev_priv)) 5669 return; 5670 5671 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); 5672 if (IS_GEN6(dev_priv)) 5673 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); 5674 else if (IS_GEN7(dev_priv)) 5675 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); 5676 else if (IS_GEN8(dev_priv)) 5677 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); 5678 else 5679 BUG(); 5680 } 5681 5682 static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base) 5683 { 5684 I915_WRITE(RING_CTL(base), 0); 5685 I915_WRITE(RING_HEAD(base), 0); 5686 I915_WRITE(RING_TAIL(base), 0); 5687 I915_WRITE(RING_START(base), 0); 5688 } 5689 5690 static void init_unused_rings(struct drm_i915_private *dev_priv) 5691 { 5692 if (IS_I830(dev_priv)) { 5693 init_unused_ring(dev_priv, PRB1_BASE); 5694 init_unused_ring(dev_priv, SRB0_BASE); 5695 init_unused_ring(dev_priv, SRB1_BASE); 5696 init_unused_ring(dev_priv, SRB2_BASE); 5697 init_unused_ring(dev_priv, SRB3_BASE); 5698 } else if (IS_GEN2(dev_priv)) { 5699 init_unused_ring(dev_priv, SRB0_BASE); 5700 init_unused_ring(dev_priv, SRB1_BASE); 5701 } else if (IS_GEN3(dev_priv)) { 5702 init_unused_ring(dev_priv, PRB1_BASE); 5703 init_unused_ring(dev_priv, PRB2_BASE); 5704 } 5705 } 5706 5707 static int __i915_gem_restart_engines(void *data) 5708 { 5709 struct drm_i915_private *i915 = data; 5710 struct intel_engine_cs *engine; 5711 enum intel_engine_id id; 5712 int err; 5713 5714 for_each_engine(engine, i915, id) { 5715 err = engine->init_hw(engine); 5716 if (err) { 5717 DRM_ERROR("Failed to restart %s (%d)\n", 5718 engine->name, err); 5719 return err; 5720 } 5721 } 5722 5723 return 0; 5724 } 5725 5726 int i915_gem_init_hw(struct drm_i915_private *dev_priv) 5727 { 5728 int ret; 5729 5730 dev_priv->gt.last_init_time = ktime_get(); 5731 5732 /* Double layer security blanket, see i915_gem_init() */ 5733 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); 5734 5735 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) 5736 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); 5737 5738 if (IS_HASWELL(dev_priv)) 5739 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? 5740 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); 5741 5742 if (HAS_PCH_NOP(dev_priv)) { 5743 if (IS_IVYBRIDGE(dev_priv)) { 5744 u32 temp = I915_READ(GEN7_MSG_CTL); 5745 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); 5746 I915_WRITE(GEN7_MSG_CTL, temp); 5747 } else if (INTEL_GEN(dev_priv) >= 7) { 5748 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); 5749 temp &= ~RESET_PCH_HANDSHAKE_ENABLE; 5750 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); 5751 } 5752 } 5753 5754 intel_gt_workarounds_apply(dev_priv); 5755 5756 i915_gem_init_swizzling(dev_priv); 5757 5758 /* 5759 * At least 830 can leave some of the unused rings 5760 * "active" (ie. head != tail) after resume which 5761 * will prevent c3 entry. Makes sure all unused rings 5762 * are totally idle. 5763 */ 5764 init_unused_rings(dev_priv); 5765 5766 BUG_ON(!dev_priv->kernel_context); 5767 if (i915_terminally_wedged(&dev_priv->gpu_error)) { 5768 ret = -EIO; 5769 goto out; 5770 } 5771 5772 ret = i915_ppgtt_init_hw(dev_priv); 5773 if (ret) { 5774 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); 5775 goto out; 5776 } 5777 5778 ret = intel_wopcm_init_hw(&dev_priv->wopcm); 5779 if (ret) { 5780 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); 5781 goto out; 5782 } 5783 5784 /* We can't enable contexts until all firmware is loaded */ 5785 ret = intel_uc_init_hw(dev_priv); 5786 if (ret) { 5787 DRM_ERROR("Enabling uc failed (%d)\n", ret); 5788 goto out; 5789 } 5790 5791 intel_mocs_init_l3cc_table(dev_priv); 5792 5793 /* Only when the HW is re-initialised, can we replay the requests */ 5794 ret = __i915_gem_restart_engines(dev_priv); 5795 if (ret) 5796 goto cleanup_uc; 5797 5798 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); 5799 5800 return 0; 5801 5802 cleanup_uc: 5803 intel_uc_fini_hw(dev_priv); 5804 out: 5805 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); 5806 5807 return ret; 5808 } 5809 5810 static int __intel_engines_record_defaults(struct drm_i915_private *i915) 5811 { 5812 struct i915_gem_context *ctx; 5813 struct intel_engine_cs *engine; 5814 enum intel_engine_id id; 5815 int err; 5816 5817 /* 5818 * As we reset the gpu during very early sanitisation, the current 5819 * register state on the GPU should reflect its defaults values. 5820 * We load a context onto the hw (with restore-inhibit), then switch 5821 * over to a second context to save that default register state. We 5822 * can then prime every new context with that state so they all start 5823 * from the same default HW values. 5824 */ 5825 5826 ctx = i915_gem_context_create_kernel(i915, 0); 5827 if (IS_ERR(ctx)) 5828 return PTR_ERR(ctx); 5829 5830 for_each_engine(engine, i915, id) { 5831 struct i915_request *rq; 5832 5833 rq = i915_request_alloc(engine, ctx); 5834 if (IS_ERR(rq)) { 5835 err = PTR_ERR(rq); 5836 goto out_ctx; 5837 } 5838 5839 err = 0; 5840 if (engine->init_context) 5841 err = engine->init_context(rq); 5842 5843 i915_request_add(rq); 5844 if (err) 5845 goto err_active; 5846 } 5847 5848 err = i915_gem_switch_to_kernel_context(i915); 5849 if (err) 5850 goto err_active; 5851 5852 if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) { 5853 i915_gem_set_wedged(i915); 5854 err = -EIO; /* Caller will declare us wedged */ 5855 goto err_active; 5856 } 5857 5858 assert_kernel_context_is_current(i915); 5859 5860 for_each_engine(engine, i915, id) { 5861 struct i915_vma *state; 5862 5863 state = to_intel_context(ctx, engine)->state; 5864 if (!state) 5865 continue; 5866 5867 /* 5868 * As we will hold a reference to the logical state, it will 5869 * not be torn down with the context, and importantly the 5870 * object will hold onto its vma (making it possible for a 5871 * stray GTT write to corrupt our defaults). Unmap the vma 5872 * from the GTT to prevent such accidents and reclaim the 5873 * space. 5874 */ 5875 err = i915_vma_unbind(state); 5876 if (err) 5877 goto err_active; 5878 5879 err = i915_gem_object_set_to_cpu_domain(state->obj, false); 5880 if (err) 5881 goto err_active; 5882 5883 engine->default_state = i915_gem_object_get(state->obj); 5884 } 5885 5886 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { 5887 unsigned int found = intel_engines_has_context_isolation(i915); 5888 5889 /* 5890 * Make sure that classes with multiple engine instances all 5891 * share the same basic configuration. 5892 */ 5893 for_each_engine(engine, i915, id) { 5894 unsigned int bit = BIT(engine->uabi_class); 5895 unsigned int expected = engine->default_state ? bit : 0; 5896 5897 if ((found & bit) != expected) { 5898 DRM_ERROR("mismatching default context state for class %d on engine %s\n", 5899 engine->uabi_class, engine->name); 5900 } 5901 } 5902 } 5903 5904 out_ctx: 5905 i915_gem_context_set_closed(ctx); 5906 i915_gem_context_put(ctx); 5907 return err; 5908 5909 err_active: 5910 /* 5911 * If we have to abandon now, we expect the engines to be idle 5912 * and ready to be torn-down. First try to flush any remaining 5913 * request, ensure we are pointing at the kernel context and 5914 * then remove it. 5915 */ 5916 if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) 5917 goto out_ctx; 5918 5919 if (WARN_ON(i915_gem_wait_for_idle(i915, 5920 I915_WAIT_LOCKED, 5921 MAX_SCHEDULE_TIMEOUT))) 5922 goto out_ctx; 5923 5924 i915_gem_contexts_lost(i915); 5925 goto out_ctx; 5926 } 5927 5928 int i915_gem_init(struct drm_i915_private *dev_priv) 5929 { 5930 int ret; 5931 5932 /* We need to fallback to 4K pages if host doesn't support huge gtt. */ 5933 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv)) 5934 mkwrite_device_info(dev_priv)->page_sizes = 5935 I915_GTT_PAGE_SIZE_4K; 5936 5937 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); 5938 5939 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { 5940 dev_priv->gt.resume = intel_lr_context_resume; 5941 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; 5942 } else { 5943 dev_priv->gt.resume = intel_legacy_submission_resume; 5944 dev_priv->gt.cleanup_engine = intel_engine_cleanup; 5945 } 5946 5947 ret = i915_gem_init_userptr(dev_priv); 5948 if (ret) 5949 return ret; 5950 5951 ret = intel_uc_init_misc(dev_priv); 5952 if (ret) 5953 return ret; 5954 5955 ret = intel_wopcm_init(&dev_priv->wopcm); 5956 if (ret) 5957 goto err_uc_misc; 5958 5959 /* This is just a security blanket to placate dragons. 5960 * On some systems, we very sporadically observe that the first TLBs 5961 * used by the CS may be stale, despite us poking the TLB reset. If 5962 * we hold the forcewake during initialisation these problems 5963 * just magically go away. 5964 */ 5965 mutex_lock(&dev_priv->drm.struct_mutex); 5966 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); 5967 5968 ret = i915_gem_init_ggtt(dev_priv); 5969 if (ret) { 5970 GEM_BUG_ON(ret == -EIO); 5971 goto err_unlock; 5972 } 5973 5974 ret = i915_gem_contexts_init(dev_priv); 5975 if (ret) { 5976 GEM_BUG_ON(ret == -EIO); 5977 goto err_ggtt; 5978 } 5979 5980 ret = intel_engines_init(dev_priv); 5981 if (ret) { 5982 GEM_BUG_ON(ret == -EIO); 5983 goto err_context; 5984 } 5985 5986 intel_init_gt_powersave(dev_priv); 5987 5988 ret = intel_uc_init(dev_priv); 5989 if (ret) 5990 goto err_pm; 5991 5992 ret = i915_gem_init_hw(dev_priv); 5993 if (ret) 5994 goto err_uc_init; 5995 5996 /* 5997 * Despite its name intel_init_clock_gating applies both display 5998 * clock gating workarounds; GT mmio workarounds and the occasional 5999 * GT power context workaround. Worse, sometimes it includes a context 6000 * register workaround which we need to apply before we record the 6001 * default HW state for all contexts. 6002 * 6003 * FIXME: break up the workarounds and apply them at the right time! 6004 */ 6005 intel_init_clock_gating(dev_priv); 6006 6007 ret = __intel_engines_record_defaults(dev_priv); 6008 if (ret) 6009 goto err_init_hw; 6010 6011 if (i915_inject_load_failure()) { 6012 ret = -ENODEV; 6013 goto err_init_hw; 6014 } 6015 6016 if (i915_inject_load_failure()) { 6017 ret = -EIO; 6018 goto err_init_hw; 6019 } 6020 6021 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); 6022 mutex_unlock(&dev_priv->drm.struct_mutex); 6023 6024 return 0; 6025 6026 /* 6027 * Unwinding is complicated by that we want to handle -EIO to mean 6028 * disable GPU submission but keep KMS alive. We want to mark the 6029 * HW as irrevisibly wedged, but keep enough state around that the 6030 * driver doesn't explode during runtime. 6031 */ 6032 err_init_hw: 6033 mutex_unlock(&dev_priv->drm.struct_mutex); 6034 6035 WARN_ON(i915_gem_suspend(dev_priv)); 6036 i915_gem_suspend_late(dev_priv); 6037 6038 i915_gem_drain_workqueue(dev_priv); 6039 6040 mutex_lock(&dev_priv->drm.struct_mutex); 6041 intel_uc_fini_hw(dev_priv); 6042 err_uc_init: 6043 intel_uc_fini(dev_priv); 6044 err_pm: 6045 if (ret != -EIO) { 6046 intel_cleanup_gt_powersave(dev_priv); 6047 i915_gem_cleanup_engines(dev_priv); 6048 } 6049 err_context: 6050 if (ret != -EIO) 6051 i915_gem_contexts_fini(dev_priv); 6052 err_ggtt: 6053 err_unlock: 6054 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); 6055 mutex_unlock(&dev_priv->drm.struct_mutex); 6056 6057 err_uc_misc: 6058 intel_uc_fini_misc(dev_priv); 6059 6060 if (ret != -EIO) 6061 i915_gem_cleanup_userptr(dev_priv); 6062 6063 if (ret == -EIO) { 6064 mutex_lock(&dev_priv->drm.struct_mutex); 6065 6066 /* 6067 * Allow engine initialisation to fail by marking the GPU as 6068 * wedged. But we only want to do this where the GPU is angry, 6069 * for all other failure, such as an allocation failure, bail. 6070 */ 6071 if (!i915_terminally_wedged(&dev_priv->gpu_error)) { 6072 i915_load_error(dev_priv, 6073 "Failed to initialize GPU, declaring it wedged!\n"); 6074 i915_gem_set_wedged(dev_priv); 6075 } 6076 6077 /* Minimal basic recovery for KMS */ 6078 ret = i915_ggtt_enable_hw(dev_priv); 6079 i915_gem_restore_gtt_mappings(dev_priv); 6080 i915_gem_restore_fences(dev_priv); 6081 intel_init_clock_gating(dev_priv); 6082 6083 mutex_unlock(&dev_priv->drm.struct_mutex); 6084 } 6085 6086 i915_gem_drain_freed_objects(dev_priv); 6087 return ret; 6088 } 6089 6090 void i915_gem_fini(struct drm_i915_private *dev_priv) 6091 { 6092 i915_gem_suspend_late(dev_priv); 6093 intel_disable_gt_powersave(dev_priv); 6094 6095 /* Flush any outstanding unpin_work. */ 6096 i915_gem_drain_workqueue(dev_priv); 6097 6098 mutex_lock(&dev_priv->drm.struct_mutex); 6099 intel_uc_fini_hw(dev_priv); 6100 intel_uc_fini(dev_priv); 6101 i915_gem_cleanup_engines(dev_priv); 6102 i915_gem_contexts_fini(dev_priv); 6103 mutex_unlock(&dev_priv->drm.struct_mutex); 6104 6105 intel_cleanup_gt_powersave(dev_priv); 6106 6107 intel_uc_fini_misc(dev_priv); 6108 i915_gem_cleanup_userptr(dev_priv); 6109 6110 i915_gem_drain_freed_objects(dev_priv); 6111 6112 WARN_ON(!list_empty(&dev_priv->contexts.list)); 6113 } 6114 6115 void i915_gem_init_mmio(struct drm_i915_private *i915) 6116 { 6117 i915_gem_sanitize(i915); 6118 } 6119 6120 void 6121 i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) 6122 { 6123 struct intel_engine_cs *engine; 6124 enum intel_engine_id id; 6125 6126 for_each_engine(engine, dev_priv, id) 6127 dev_priv->gt.cleanup_engine(engine); 6128 } 6129 6130 void 6131 i915_gem_load_init_fences(struct drm_i915_private *dev_priv) 6132 { 6133 int i; 6134 6135 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && 6136 !IS_CHERRYVIEW(dev_priv)) 6137 dev_priv->num_fence_regs = 32; 6138 else if (INTEL_GEN(dev_priv) >= 4 || 6139 IS_I945G(dev_priv) || IS_I945GM(dev_priv) || 6140 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) 6141 dev_priv->num_fence_regs = 16; 6142 else 6143 dev_priv->num_fence_regs = 8; 6144 6145 if (intel_vgpu_active(dev_priv)) 6146 dev_priv->num_fence_regs = 6147 I915_READ(vgtif_reg(avail_rs.fence_num)); 6148 6149 /* Initialize fence registers to zero */ 6150 for (i = 0; i < dev_priv->num_fence_regs; i++) { 6151 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; 6152 6153 fence->i915 = dev_priv; 6154 fence->id = i; 6155 list_add_tail(&fence->link, &dev_priv->mm.fence_list); 6156 } 6157 i915_gem_restore_fences(dev_priv); 6158 6159 i915_gem_detect_bit_6_swizzle(dev_priv); 6160 } 6161 6162 static void i915_gem_init__mm(struct drm_i915_private *i915) 6163 { 6164 mtx_init(&i915->mm.object_stat_lock, IPL_NONE); 6165 mtx_init(&i915->mm.obj_lock, IPL_NONE); 6166 mtx_init(&i915->mm.free_lock, IPL_NONE); 6167 6168 init_llist_head(&i915->mm.free_list); 6169 6170 INIT_LIST_HEAD(&i915->mm.unbound_list); 6171 INIT_LIST_HEAD(&i915->mm.bound_list); 6172 INIT_LIST_HEAD(&i915->mm.fence_list); 6173 INIT_LIST_HEAD(&i915->mm.userfault_list); 6174 6175 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); 6176 } 6177 6178 int i915_gem_init_early(struct drm_i915_private *dev_priv) 6179 { 6180 int err = -ENOMEM; 6181 6182 #ifdef __linux__ 6183 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); 6184 if (!dev_priv->objects) 6185 goto err_out; 6186 6187 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); 6188 if (!dev_priv->vmas) 6189 goto err_objects; 6190 6191 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); 6192 if (!dev_priv->luts) 6193 goto err_vmas; 6194 6195 dev_priv->requests = KMEM_CACHE(i915_request, 6196 SLAB_HWCACHE_ALIGN | 6197 SLAB_RECLAIM_ACCOUNT | 6198 SLAB_TYPESAFE_BY_RCU); 6199 if (!dev_priv->requests) 6200 goto err_luts; 6201 6202 dev_priv->dependencies = KMEM_CACHE(i915_dependency, 6203 SLAB_HWCACHE_ALIGN | 6204 SLAB_RECLAIM_ACCOUNT); 6205 if (!dev_priv->dependencies) 6206 goto err_requests; 6207 6208 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); 6209 if (!dev_priv->priorities) 6210 goto err_dependencies; 6211 #else 6212 pool_init(&dev_priv->objects, sizeof(struct drm_i915_gem_object), 6213 0, IPL_NONE, 0, "drmobj", NULL); 6214 pool_init(&dev_priv->vmas, sizeof(struct i915_vma), 6215 0, IPL_NONE, 0, "drmvma", NULL); 6216 pool_init(&dev_priv->luts, sizeof(struct i915_lut_handle), 6217 0, IPL_NONE, 0, "drmlut", NULL); 6218 pool_init(&dev_priv->requests, sizeof(struct i915_request), 6219 0, IPL_TTY, 0, "drmreq", NULL); 6220 pool_init(&dev_priv->dependencies, sizeof(struct i915_dependency), 6221 0, IPL_TTY, 0, "drmdep", NULL); 6222 pool_init(&dev_priv->priorities, sizeof(struct i915_priolist), 6223 0, IPL_TTY, 0, "drmpri", NULL); 6224 #endif 6225 6226 INIT_LIST_HEAD(&dev_priv->gt.timelines); 6227 INIT_LIST_HEAD(&dev_priv->gt.active_rings); 6228 INIT_LIST_HEAD(&dev_priv->gt.closed_vma); 6229 6230 i915_gem_init__mm(dev_priv); 6231 6232 INIT_DELAYED_WORK(&dev_priv->gt.retire_work, 6233 i915_gem_retire_work_handler); 6234 INIT_DELAYED_WORK(&dev_priv->gt.idle_work, 6235 i915_gem_idle_work_handler); 6236 init_waitqueue_head(&dev_priv->gpu_error.wait_queue); 6237 init_waitqueue_head(&dev_priv->gpu_error.reset_queue); 6238 6239 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); 6240 6241 mtx_init(&dev_priv->fb_tracking.lock, IPL_TTY); 6242 6243 err = i915_gemfs_init(dev_priv); 6244 if (err) 6245 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); 6246 6247 return 0; 6248 6249 #ifdef __linux__ 6250 err_dependencies: 6251 kmem_cache_destroy(dev_priv->dependencies); 6252 err_requests: 6253 kmem_cache_destroy(dev_priv->requests); 6254 err_luts: 6255 kmem_cache_destroy(dev_priv->luts); 6256 err_vmas: 6257 kmem_cache_destroy(dev_priv->vmas); 6258 err_objects: 6259 kmem_cache_destroy(dev_priv->objects); 6260 err_out: 6261 return err; 6262 #endif 6263 } 6264 6265 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) 6266 { 6267 i915_gem_drain_freed_objects(dev_priv); 6268 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); 6269 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); 6270 WARN_ON(dev_priv->mm.object_count); 6271 WARN_ON(!list_empty(&dev_priv->gt.timelines)); 6272 6273 #ifdef __linux__ 6274 kmem_cache_destroy(dev_priv->priorities); 6275 kmem_cache_destroy(dev_priv->dependencies); 6276 kmem_cache_destroy(dev_priv->requests); 6277 kmem_cache_destroy(dev_priv->luts); 6278 kmem_cache_destroy(dev_priv->vmas); 6279 kmem_cache_destroy(dev_priv->objects); 6280 #endif 6281 6282 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ 6283 rcu_barrier(); 6284 6285 i915_gemfs_fini(dev_priv); 6286 } 6287 6288 int i915_gem_freeze(struct drm_i915_private *dev_priv) 6289 { 6290 /* Discard all purgeable objects, let userspace recover those as 6291 * required after resuming. 6292 */ 6293 i915_gem_shrink_all(dev_priv); 6294 6295 return 0; 6296 } 6297 6298 int i915_gem_freeze_late(struct drm_i915_private *i915) 6299 { 6300 struct drm_i915_gem_object *obj; 6301 struct list_head *phases[] = { 6302 &i915->mm.unbound_list, 6303 &i915->mm.bound_list, 6304 NULL 6305 }, **phase; 6306 6307 /* 6308 * Called just before we write the hibernation image. 6309 * 6310 * We need to update the domain tracking to reflect that the CPU 6311 * will be accessing all the pages to create and restore from the 6312 * hibernation, and so upon restoration those pages will be in the 6313 * CPU domain. 6314 * 6315 * To make sure the hibernation image contains the latest state, 6316 * we update that state just before writing out the image. 6317 * 6318 * To try and reduce the hibernation image, we manually shrink 6319 * the objects as well, see i915_gem_freeze() 6320 */ 6321 6322 i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); 6323 i915_gem_drain_freed_objects(i915); 6324 6325 mutex_lock(&i915->drm.struct_mutex); 6326 for (phase = phases; *phase; phase++) { 6327 list_for_each_entry(obj, *phase, mm.link) 6328 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); 6329 } 6330 mutex_unlock(&i915->drm.struct_mutex); 6331 6332 return 0; 6333 } 6334 6335 void i915_gem_release(struct drm_device *dev, struct drm_file *file) 6336 { 6337 struct drm_i915_file_private *file_priv = file->driver_priv; 6338 struct i915_request *request; 6339 6340 /* Clean up our request list when the client is going away, so that 6341 * later retire_requests won't dereference our soon-to-be-gone 6342 * file_priv. 6343 */ 6344 spin_lock(&file_priv->mm.lock); 6345 list_for_each_entry(request, &file_priv->mm.request_list, client_link) 6346 request->file_priv = NULL; 6347 spin_unlock(&file_priv->mm.lock); 6348 } 6349 6350 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file) 6351 { 6352 struct drm_i915_file_private *file_priv; 6353 int ret; 6354 6355 DRM_DEBUG("\n"); 6356 6357 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); 6358 if (!file_priv) 6359 return -ENOMEM; 6360 6361 file->driver_priv = file_priv; 6362 file_priv->dev_priv = i915; 6363 file_priv->file = file; 6364 6365 mtx_init(&file_priv->mm.lock, IPL_NONE); 6366 INIT_LIST_HEAD(&file_priv->mm.request_list); 6367 6368 file_priv->bsd_engine = -1; 6369 file_priv->hang_timestamp = jiffies; 6370 6371 ret = i915_gem_context_open(i915, file); 6372 if (ret) 6373 kfree(file_priv); 6374 6375 return ret; 6376 } 6377 6378 /** 6379 * i915_gem_track_fb - update frontbuffer tracking 6380 * @old: current GEM buffer for the frontbuffer slots 6381 * @new: new GEM buffer for the frontbuffer slots 6382 * @frontbuffer_bits: bitmask of frontbuffer slots 6383 * 6384 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them 6385 * from @old and setting them in @new. Both @old and @new can be NULL. 6386 */ 6387 void i915_gem_track_fb(struct drm_i915_gem_object *old, 6388 struct drm_i915_gem_object *new, 6389 unsigned frontbuffer_bits) 6390 { 6391 /* Control of individual bits within the mask are guarded by 6392 * the owning plane->mutex, i.e. we can never see concurrent 6393 * manipulation of individual bits. But since the bitfield as a whole 6394 * is updated using RMW, we need to use atomics in order to update 6395 * the bits. 6396 */ 6397 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > 6398 sizeof(atomic_t) * BITS_PER_BYTE); 6399 6400 if (old) { 6401 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); 6402 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); 6403 } 6404 6405 if (new) { 6406 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); 6407 atomic_or(frontbuffer_bits, &new->frontbuffer_bits); 6408 } 6409 } 6410 6411 /* Allocate a new GEM object and fill it with the supplied data */ 6412 struct drm_i915_gem_object * 6413 i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, 6414 const void *data, size_t size) 6415 { 6416 STUB(); 6417 return NULL; 6418 #ifdef notyet 6419 struct drm_i915_gem_object *obj; 6420 struct file *file; 6421 size_t offset; 6422 int err; 6423 6424 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); 6425 if (IS_ERR(obj)) 6426 return obj; 6427 6428 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); 6429 6430 file = obj->base.filp; 6431 offset = 0; 6432 do { 6433 unsigned int len = min_t(typeof(size), size, PAGE_SIZE); 6434 struct vm_page *page; 6435 void *pgdata, *vaddr; 6436 6437 err = pagecache_write_begin(file, file->f_mapping, 6438 offset, len, 0, 6439 &page, &pgdata); 6440 if (err < 0) 6441 goto fail; 6442 6443 vaddr = kmap(page); 6444 memcpy(vaddr, data, len); 6445 kunmap(vaddr); 6446 6447 err = pagecache_write_end(file, file->f_mapping, 6448 offset, len, len, 6449 page, pgdata); 6450 if (err < 0) 6451 goto fail; 6452 6453 size -= len; 6454 data += len; 6455 offset += len; 6456 } while (size); 6457 6458 return obj; 6459 6460 fail: 6461 i915_gem_object_put(obj); 6462 return ERR_PTR(err); 6463 #endif 6464 } 6465 6466 struct scatterlist * 6467 i915_gem_object_get_sg(struct drm_i915_gem_object *obj, 6468 unsigned int n, 6469 unsigned int *offset) 6470 { 6471 struct i915_gem_object_page_iter *iter = &obj->mm.get_page; 6472 struct scatterlist *sg; 6473 unsigned int idx, count; 6474 6475 might_sleep(); 6476 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); 6477 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 6478 6479 /* As we iterate forward through the sg, we record each entry in a 6480 * radixtree for quick repeated (backwards) lookups. If we have seen 6481 * this index previously, we will have an entry for it. 6482 * 6483 * Initial lookup is O(N), but this is amortized to O(1) for 6484 * sequential page access (where each new request is consecutive 6485 * to the previous one). Repeated lookups are O(lg(obj->base.size)), 6486 * i.e. O(1) with a large constant! 6487 */ 6488 if (n < READ_ONCE(iter->sg_idx)) 6489 goto lookup; 6490 6491 mutex_lock(&iter->lock); 6492 6493 /* We prefer to reuse the last sg so that repeated lookup of this 6494 * (or the subsequent) sg are fast - comparing against the last 6495 * sg is faster than going through the radixtree. 6496 */ 6497 6498 sg = iter->sg_pos; 6499 idx = iter->sg_idx; 6500 count = __sg_page_count(sg); 6501 6502 while (idx + count <= n) { 6503 unsigned long exception, i; 6504 int ret; 6505 6506 /* If we cannot allocate and insert this entry, or the 6507 * individual pages from this range, cancel updating the 6508 * sg_idx so that on this lookup we are forced to linearly 6509 * scan onwards, but on future lookups we will try the 6510 * insertion again (in which case we need to be careful of 6511 * the error return reporting that we have already inserted 6512 * this index). 6513 */ 6514 ret = radix_tree_insert(&iter->radix, idx, sg); 6515 if (ret && ret != -EEXIST) 6516 goto scan; 6517 6518 exception = 6519 RADIX_TREE_EXCEPTIONAL_ENTRY | 6520 idx << RADIX_TREE_EXCEPTIONAL_SHIFT; 6521 for (i = 1; i < count; i++) { 6522 ret = radix_tree_insert(&iter->radix, idx + i, 6523 (void *)exception); 6524 if (ret && ret != -EEXIST) 6525 goto scan; 6526 } 6527 6528 idx += count; 6529 sg = ____sg_next(sg); 6530 count = __sg_page_count(sg); 6531 } 6532 6533 scan: 6534 iter->sg_pos = sg; 6535 iter->sg_idx = idx; 6536 6537 mutex_unlock(&iter->lock); 6538 6539 if (unlikely(n < idx)) /* insertion completed by another thread */ 6540 goto lookup; 6541 6542 /* In case we failed to insert the entry into the radixtree, we need 6543 * to look beyond the current sg. 6544 */ 6545 while (idx + count <= n) { 6546 idx += count; 6547 sg = ____sg_next(sg); 6548 count = __sg_page_count(sg); 6549 } 6550 6551 *offset = n - idx; 6552 return sg; 6553 6554 lookup: 6555 rcu_read_lock(); 6556 6557 sg = radix_tree_lookup(&iter->radix, n); 6558 GEM_BUG_ON(!sg); 6559 6560 /* If this index is in the middle of multi-page sg entry, 6561 * the radixtree will contain an exceptional entry that points 6562 * to the start of that range. We will return the pointer to 6563 * the base page and the offset of this page within the 6564 * sg entry's range. 6565 */ 6566 *offset = 0; 6567 if (unlikely(radix_tree_exception(sg))) { 6568 unsigned long base = 6569 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; 6570 6571 sg = radix_tree_lookup(&iter->radix, base); 6572 GEM_BUG_ON(!sg); 6573 6574 *offset = n - base; 6575 } 6576 6577 rcu_read_unlock(); 6578 6579 return sg; 6580 } 6581 6582 struct vm_page * 6583 i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) 6584 { 6585 struct scatterlist *sg; 6586 unsigned int offset; 6587 6588 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); 6589 6590 sg = i915_gem_object_get_sg(obj, n, &offset); 6591 return nth_page(sg_page(sg), offset); 6592 } 6593 6594 /* Like i915_gem_object_get_page(), but mark the returned page dirty */ 6595 struct vm_page * 6596 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, 6597 unsigned int n) 6598 { 6599 struct vm_page *page; 6600 6601 page = i915_gem_object_get_page(obj, n); 6602 if (!obj->mm.dirty) 6603 set_page_dirty(page); 6604 6605 return page; 6606 } 6607 6608 dma_addr_t 6609 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, 6610 unsigned long n) 6611 { 6612 struct scatterlist *sg; 6613 unsigned int offset; 6614 6615 sg = i915_gem_object_get_sg(obj, n, &offset); 6616 return sg_dma_address(sg) + (offset << PAGE_SHIFT); 6617 } 6618 6619 int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) 6620 { 6621 struct sg_table *pages; 6622 int err; 6623 6624 if (align > obj->base.size) 6625 return -EINVAL; 6626 6627 if (obj->ops == &i915_gem_phys_ops) 6628 return 0; 6629 6630 if (obj->ops != &i915_gem_object_ops) 6631 return -EINVAL; 6632 6633 err = i915_gem_object_unbind(obj); 6634 if (err) 6635 return err; 6636 6637 mutex_lock(&obj->mm.lock); 6638 6639 if (obj->mm.madv != I915_MADV_WILLNEED) { 6640 err = -EFAULT; 6641 goto err_unlock; 6642 } 6643 6644 if (obj->mm.quirked) { 6645 err = -EFAULT; 6646 goto err_unlock; 6647 } 6648 6649 if (obj->mm.mapping) { 6650 err = -EBUSY; 6651 goto err_unlock; 6652 } 6653 6654 pages = __i915_gem_object_unset_pages(obj); 6655 6656 obj->ops = &i915_gem_phys_ops; 6657 6658 err = ____i915_gem_object_get_pages(obj); 6659 if (err) 6660 goto err_xfer; 6661 6662 /* Perma-pin (until release) the physical set of pages */ 6663 __i915_gem_object_pin_pages(obj); 6664 6665 if (!IS_ERR_OR_NULL(pages)) 6666 i915_gem_object_ops.put_pages(obj, pages); 6667 mutex_unlock(&obj->mm.lock); 6668 return 0; 6669 6670 err_xfer: 6671 obj->ops = &i915_gem_object_ops; 6672 if (!IS_ERR_OR_NULL(pages)) { 6673 unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); 6674 6675 __i915_gem_object_set_pages(obj, pages, sg_page_sizes); 6676 } 6677 err_unlock: 6678 mutex_unlock(&obj->mm.lock); 6679 return err; 6680 } 6681 6682 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 6683 #include "selftests/scatterlist.c" 6684 #include "selftests/mock_gem_device.c" 6685 #include "selftests/huge_gem_object.c" 6686 #include "selftests/huge_pages.c" 6687 #include "selftests/i915_gem_object.c" 6688 #include "selftests/i915_gem_coherency.c" 6689 #endif 6690