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_vgpu.h" 33 #include "i915_trace.h" 34 #include "intel_drv.h" 35 #include "intel_frontbuffer.h" 36 #include "intel_mocs.h" 37 #include <linux/dma-fence-array.h> 38 #include <linux/reservation.h> 39 #include <linux/shmem_fs.h> 40 #include <linux/slab.h> 41 #include <linux/swap.h> 42 #include <linux/pci.h> 43 #include <linux/dma-buf.h> 44 45 #include <sys/mman.h> 46 #include <vm/vm_map.h> 47 #include <vm/vm_param.h> 48 49 static void i915_gem_flush_free_objects(struct drm_i915_private *i915); 50 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj); 51 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj); 52 53 static bool cpu_cache_is_coherent(struct drm_device *dev, 54 enum i915_cache_level level) 55 { 56 return HAS_LLC(to_i915(dev)) || level != I915_CACHE_NONE; 57 } 58 59 static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) 60 { 61 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) 62 return false; 63 64 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) 65 return true; 66 67 return obj->pin_display; 68 } 69 70 static int 71 insert_mappable_node(struct i915_ggtt *ggtt, 72 struct drm_mm_node *node, u32 size) 73 { 74 memset(node, 0, sizeof(*node)); 75 return drm_mm_insert_node_in_range_generic(&ggtt->base.mm, node, 76 size, 0, -1, 77 0, ggtt->mappable_end, 78 DRM_MM_SEARCH_DEFAULT, 79 DRM_MM_CREATE_DEFAULT); 80 } 81 82 static void 83 remove_mappable_node(struct drm_mm_node *node) 84 { 85 drm_mm_remove_node(node); 86 } 87 88 /* some bookkeeping */ 89 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, 90 u64 size) 91 { 92 lockmgr(&dev_priv->mm.object_stat_lock, LK_EXCLUSIVE); 93 dev_priv->mm.object_count++; 94 dev_priv->mm.object_memory += size; 95 lockmgr(&dev_priv->mm.object_stat_lock, LK_RELEASE); 96 } 97 98 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, 99 u64 size) 100 { 101 lockmgr(&dev_priv->mm.object_stat_lock, LK_EXCLUSIVE); 102 dev_priv->mm.object_count--; 103 dev_priv->mm.object_memory -= size; 104 lockmgr(&dev_priv->mm.object_stat_lock, LK_RELEASE); 105 } 106 107 static int 108 i915_gem_wait_for_error(struct i915_gpu_error *error) 109 { 110 int ret; 111 112 might_sleep(); 113 114 if (!i915_reset_in_progress(error)) 115 return 0; 116 117 /* 118 * Only wait 10 seconds for the gpu reset to complete to avoid hanging 119 * userspace. If it takes that long something really bad is going on and 120 * we should simply try to bail out and fail as gracefully as possible. 121 */ 122 ret = wait_event_interruptible_timeout(error->reset_queue, 123 !i915_reset_in_progress(error), 124 I915_RESET_TIMEOUT); 125 if (ret == 0) { 126 DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); 127 return -EIO; 128 } else if (ret < 0) { 129 return ret; 130 } else { 131 return 0; 132 } 133 } 134 135 int i915_mutex_lock_interruptible(struct drm_device *dev) 136 { 137 struct drm_i915_private *dev_priv = to_i915(dev); 138 int ret; 139 140 ret = i915_gem_wait_for_error(&dev_priv->gpu_error); 141 if (ret) 142 return ret; 143 144 ret = mutex_lock_interruptible(&dev->struct_mutex); 145 if (ret) 146 return ret; 147 148 return 0; 149 } 150 151 int 152 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, 153 struct drm_file *file) 154 { 155 struct drm_i915_private *dev_priv = to_i915(dev); 156 struct i915_ggtt *ggtt = &dev_priv->ggtt; 157 struct drm_i915_gem_get_aperture *args = data; 158 struct i915_vma *vma; 159 size_t pinned; 160 161 pinned = 0; 162 mutex_lock(&dev->struct_mutex); 163 list_for_each_entry(vma, &ggtt->base.active_list, vm_link) 164 if (i915_vma_is_pinned(vma)) 165 pinned += vma->node.size; 166 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link) 167 if (i915_vma_is_pinned(vma)) 168 pinned += vma->node.size; 169 mutex_unlock(&dev->struct_mutex); 170 171 args->aper_size = ggtt->base.total; 172 args->aper_available_size = args->aper_size - pinned; 173 174 return 0; 175 } 176 177 static struct sg_table * 178 i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj) 179 { 180 #if 0 181 struct address_space *mapping = obj->base.filp->f_mapping; 182 #else 183 vm_object_t vm_obj = obj->base.filp; 184 #endif 185 drm_dma_handle_t *phys; 186 struct sg_table *st; 187 struct scatterlist *sg; 188 char *vaddr; 189 int i; 190 191 /* Always aligning to the object size, allows a single allocation 192 * to handle all possible callers, and given typical object sizes, 193 * the alignment of the buddy allocation will naturally match. 194 */ 195 phys = drm_pci_alloc(obj->base.dev, 196 obj->base.size, 197 roundup_pow_of_two(obj->base.size)); 198 if (!phys) 199 return ERR_PTR(-ENOMEM); 200 201 vaddr = phys->vaddr; 202 VM_OBJECT_LOCK(vm_obj); 203 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { 204 struct page *page; 205 char *src; 206 207 #if 0 208 page = shmem_read_mapping_page(mapping, i); 209 #else 210 page = shmem_read_mapping_page(vm_obj, i); 211 #endif 212 if (IS_ERR(page)) { 213 st = ERR_CAST(page); 214 goto err_phys; 215 } 216 217 src = kmap_atomic(page); 218 memcpy(vaddr, src, PAGE_SIZE); 219 drm_clflush_virt_range(vaddr, PAGE_SIZE); 220 kunmap_atomic(src); 221 222 put_page(page); 223 vaddr += PAGE_SIZE; 224 } 225 VM_OBJECT_UNLOCK(vm_obj); 226 227 i915_gem_chipset_flush(to_i915(obj->base.dev)); 228 229 st = kmalloc(sizeof(*st), M_DRM, GFP_KERNEL); 230 if (!st) { 231 st = ERR_PTR(-ENOMEM); 232 goto err_phys; 233 } 234 235 if (sg_alloc_table(st, 1, GFP_KERNEL)) { 236 kfree(st); 237 st = ERR_PTR(-ENOMEM); 238 goto err_phys; 239 } 240 241 sg = st->sgl; 242 sg->offset = 0; 243 sg->length = obj->base.size; 244 245 sg_dma_address(sg) = phys->busaddr; 246 sg_dma_len(sg) = obj->base.size; 247 248 obj->phys_handle = phys; 249 return st; 250 251 err_phys: 252 drm_pci_free(obj->base.dev, phys); 253 return st; 254 } 255 256 static void 257 __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, 258 struct sg_table *pages, 259 bool needs_clflush) 260 { 261 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); 262 263 if (obj->mm.madv == I915_MADV_DONTNEED) 264 obj->mm.dirty = false; 265 266 if (needs_clflush && 267 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 && 268 !cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) 269 drm_clflush_sg(pages); 270 271 obj->base.read_domains = I915_GEM_DOMAIN_CPU; 272 obj->base.write_domain = I915_GEM_DOMAIN_CPU; 273 } 274 275 static void 276 i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, 277 struct sg_table *pages) 278 { 279 __i915_gem_object_release_shmem(obj, pages, false); 280 281 if (obj->mm.dirty) { 282 #if 0 283 struct address_space *mapping = obj->base.filp->f_mapping; 284 #else 285 vm_object_t mapping = obj->base.filp; 286 #endif 287 char *vaddr = obj->phys_handle->vaddr; 288 int i; 289 290 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { 291 struct page *page; 292 char *dst; 293 294 page = shmem_read_mapping_page(mapping, i); 295 if (IS_ERR(page)) 296 continue; 297 298 dst = kmap_atomic(page); 299 drm_clflush_virt_range(vaddr, PAGE_SIZE); 300 memcpy(dst, vaddr, PAGE_SIZE); 301 kunmap_atomic(dst); 302 303 set_page_dirty(page); 304 if (obj->mm.madv == I915_MADV_WILLNEED) 305 mark_page_accessed(page); 306 put_page(page); 307 vaddr += PAGE_SIZE; 308 } 309 obj->mm.dirty = false; 310 } 311 312 sg_free_table(pages); 313 kfree(pages); 314 315 drm_pci_free(obj->base.dev, obj->phys_handle); 316 } 317 318 static void 319 i915_gem_object_release_phys(struct drm_i915_gem_object *obj) 320 { 321 i915_gem_object_unpin_pages(obj); 322 } 323 324 static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { 325 .get_pages = i915_gem_object_get_pages_phys, 326 .put_pages = i915_gem_object_put_pages_phys, 327 .release = i915_gem_object_release_phys, 328 }; 329 330 int i915_gem_object_unbind(struct drm_i915_gem_object *obj) 331 { 332 struct i915_vma *vma; 333 LINUX_LIST_HEAD(still_in_list); 334 int ret; 335 336 lockdep_assert_held(&obj->base.dev->struct_mutex); 337 338 /* Closed vma are removed from the obj->vma_list - but they may 339 * still have an active binding on the object. To remove those we 340 * must wait for all rendering to complete to the object (as unbinding 341 * must anyway), and retire the requests. 342 */ 343 ret = i915_gem_object_wait(obj, 344 I915_WAIT_INTERRUPTIBLE | 345 I915_WAIT_LOCKED | 346 I915_WAIT_ALL, 347 MAX_SCHEDULE_TIMEOUT, 348 NULL); 349 if (ret) 350 return ret; 351 352 i915_gem_retire_requests(to_i915(obj->base.dev)); 353 354 while ((vma = list_first_entry_or_null(&obj->vma_list, 355 struct i915_vma, 356 obj_link))) { 357 list_move_tail(&vma->obj_link, &still_in_list); 358 ret = i915_vma_unbind(vma); 359 if (ret) 360 break; 361 } 362 list_splice(&still_in_list, &obj->vma_list); 363 364 return ret; 365 } 366 367 static long 368 i915_gem_object_wait_fence(struct dma_fence *fence, 369 unsigned int flags, 370 long timeout, 371 struct intel_rps_client *rps) 372 { 373 struct drm_i915_gem_request *rq; 374 375 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); 376 377 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 378 return timeout; 379 380 if (!dma_fence_is_i915(fence)) 381 return dma_fence_wait_timeout(fence, 382 flags & I915_WAIT_INTERRUPTIBLE, 383 timeout); 384 385 rq = to_request(fence); 386 if (i915_gem_request_completed(rq)) 387 goto out; 388 389 /* This client is about to stall waiting for the GPU. In many cases 390 * this is undesirable and limits the throughput of the system, as 391 * many clients cannot continue processing user input/output whilst 392 * blocked. RPS autotuning may take tens of milliseconds to respond 393 * to the GPU load and thus incurs additional latency for the client. 394 * We can circumvent that by promoting the GPU frequency to maximum 395 * before we wait. This makes the GPU throttle up much more quickly 396 * (good for benchmarks and user experience, e.g. window animations), 397 * but at a cost of spending more power processing the workload 398 * (bad for battery). Not all clients even want their results 399 * immediately and for them we should just let the GPU select its own 400 * frequency to maximise efficiency. To prevent a single client from 401 * forcing the clocks too high for the whole system, we only allow 402 * each client to waitboost once in a busy period. 403 */ 404 if (rps) { 405 if (INTEL_GEN(rq->i915) >= 6) 406 gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies); 407 else 408 rps = NULL; 409 } 410 411 timeout = i915_wait_request(rq, flags, timeout); 412 413 out: 414 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq)) 415 i915_gem_request_retire_upto(rq); 416 417 if (rps && rq->global_seqno == intel_engine_last_submit(rq->engine)) { 418 /* The GPU is now idle and this client has stalled. 419 * Since no other client has submitted a request in the 420 * meantime, assume that this client is the only one 421 * supplying work to the GPU but is unable to keep that 422 * work supplied because it is waiting. Since the GPU is 423 * then never kept fully busy, RPS autoclocking will 424 * keep the clocks relatively low, causing further delays. 425 * Compensate by giving the synchronous client credit for 426 * a waitboost next time. 427 */ 428 lockmgr(&rq->i915->rps.client_lock, LK_EXCLUSIVE); 429 list_del_init(&rps->link); 430 lockmgr(&rq->i915->rps.client_lock, LK_RELEASE); 431 } 432 433 return timeout; 434 } 435 436 static long 437 i915_gem_object_wait_reservation(struct reservation_object *resv, 438 unsigned int flags, 439 long timeout, 440 struct intel_rps_client *rps) 441 { 442 struct dma_fence *excl; 443 444 if (flags & I915_WAIT_ALL) { 445 struct dma_fence **shared; 446 unsigned int count, i; 447 int ret; 448 449 ret = reservation_object_get_fences_rcu(resv, 450 &excl, &count, &shared); 451 if (ret) 452 return ret; 453 454 for (i = 0; i < count; i++) { 455 timeout = i915_gem_object_wait_fence(shared[i], 456 flags, timeout, 457 rps); 458 if (timeout < 0) 459 break; 460 461 dma_fence_put(shared[i]); 462 } 463 464 for (; i < count; i++) 465 dma_fence_put(shared[i]); 466 kfree(shared); 467 } else { 468 excl = reservation_object_get_excl_rcu(resv); 469 } 470 471 if (excl && timeout >= 0) 472 timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps); 473 474 dma_fence_put(excl); 475 476 return timeout; 477 } 478 479 static void __fence_set_priority(struct dma_fence *fence, int prio) 480 { 481 struct drm_i915_gem_request *rq; 482 struct intel_engine_cs *engine; 483 484 if (!dma_fence_is_i915(fence)) 485 return; 486 487 rq = to_request(fence); 488 engine = rq->engine; 489 if (!engine->schedule) 490 return; 491 492 engine->schedule(rq, prio); 493 } 494 495 static void fence_set_priority(struct dma_fence *fence, int prio) 496 { 497 /* Recurse once into a fence-array */ 498 if (dma_fence_is_array(fence)) { 499 struct dma_fence_array *array = to_dma_fence_array(fence); 500 int i; 501 502 for (i = 0; i < array->num_fences; i++) 503 __fence_set_priority(array->fences[i], prio); 504 } else { 505 __fence_set_priority(fence, prio); 506 } 507 } 508 509 int 510 i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, 511 unsigned int flags, 512 int prio) 513 { 514 struct dma_fence *excl; 515 516 if (flags & I915_WAIT_ALL) { 517 struct dma_fence **shared; 518 unsigned int count, i; 519 int ret; 520 521 ret = reservation_object_get_fences_rcu(obj->resv, 522 &excl, &count, &shared); 523 if (ret) 524 return ret; 525 526 for (i = 0; i < count; i++) { 527 fence_set_priority(shared[i], prio); 528 dma_fence_put(shared[i]); 529 } 530 531 kfree(shared); 532 } else { 533 excl = reservation_object_get_excl_rcu(obj->resv); 534 } 535 536 if (excl) { 537 fence_set_priority(excl, prio); 538 dma_fence_put(excl); 539 } 540 return 0; 541 } 542 543 /** 544 * Waits for rendering to the object to be completed 545 * @obj: i915 gem object 546 * @flags: how to wait (under a lock, for all rendering or just for writes etc) 547 * @timeout: how long to wait 548 * @rps: client (user process) to charge for any waitboosting 549 */ 550 int 551 i915_gem_object_wait(struct drm_i915_gem_object *obj, 552 unsigned int flags, 553 long timeout, 554 struct intel_rps_client *rps) 555 { 556 might_sleep(); 557 #if IS_ENABLED(CONFIG_LOCKDEP) 558 GEM_BUG_ON(debug_locks && 559 !!lockdep_is_held(&obj->base.dev->struct_mutex) != 560 !!(flags & I915_WAIT_LOCKED)); 561 #endif 562 GEM_BUG_ON(timeout < 0); 563 564 timeout = i915_gem_object_wait_reservation(obj->resv, 565 flags, timeout, 566 rps); 567 return timeout < 0 ? timeout : 0; 568 } 569 570 static struct intel_rps_client *to_rps_client(struct drm_file *file) 571 { 572 struct drm_i915_file_private *fpriv = file->driver_priv; 573 574 return &fpriv->rps; 575 } 576 577 int 578 i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, 579 int align) 580 { 581 int ret; 582 583 if (align > obj->base.size) 584 return -EINVAL; 585 586 if (obj->ops == &i915_gem_phys_ops) 587 return 0; 588 589 if (obj->mm.madv != I915_MADV_WILLNEED) 590 return -EFAULT; 591 592 if (obj->base.filp == NULL) 593 return -EINVAL; 594 595 ret = i915_gem_object_unbind(obj); 596 if (ret) 597 return ret; 598 599 __i915_gem_object_put_pages(obj, I915_MM_NORMAL); 600 if (obj->mm.pages) 601 return -EBUSY; 602 603 obj->ops = &i915_gem_phys_ops; 604 605 return i915_gem_object_pin_pages(obj); 606 } 607 608 static int 609 i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, 610 struct drm_i915_gem_pwrite *args, 611 struct drm_file *file) 612 { 613 void *vaddr = obj->phys_handle->vaddr + args->offset; 614 char __user *user_data = u64_to_user_ptr(args->data_ptr); 615 616 /* We manually control the domain here and pretend that it 617 * remains coherent i.e. in the GTT domain, like shmem_pwrite. 618 */ 619 intel_fb_obj_invalidate(obj, ORIGIN_CPU); 620 if (copy_from_user(vaddr, user_data, args->size)) 621 return -EFAULT; 622 623 drm_clflush_virt_range(vaddr, args->size); 624 i915_gem_chipset_flush(to_i915(obj->base.dev)); 625 626 intel_fb_obj_flush(obj, false, ORIGIN_CPU); 627 return 0; 628 } 629 630 void *i915_gem_object_alloc(struct drm_device *dev) 631 { 632 struct drm_i915_private *dev_priv = to_i915(dev); 633 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); 634 } 635 636 void i915_gem_object_free(struct drm_i915_gem_object *obj) 637 { 638 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 639 kmem_cache_free(dev_priv->objects, obj); 640 } 641 642 static int 643 i915_gem_create(struct drm_file *file, 644 struct drm_device *dev, 645 uint64_t size, 646 uint32_t *handle_p) 647 { 648 struct drm_i915_gem_object *obj; 649 int ret; 650 u32 handle; 651 652 size = roundup(size, PAGE_SIZE); 653 if (size == 0) 654 return -EINVAL; 655 656 /* Allocate the new object */ 657 obj = i915_gem_object_create(dev, size); 658 if (IS_ERR(obj)) 659 return PTR_ERR(obj); 660 661 ret = drm_gem_handle_create(file, &obj->base, &handle); 662 /* drop reference from allocate - handle holds it now */ 663 i915_gem_object_put(obj); 664 if (ret) 665 return ret; 666 667 *handle_p = handle; 668 return 0; 669 } 670 671 int 672 i915_gem_dumb_create(struct drm_file *file, 673 struct drm_device *dev, 674 struct drm_mode_create_dumb *args) 675 { 676 /* have to work out size/pitch and return them */ 677 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); 678 args->size = args->pitch * args->height; 679 return i915_gem_create(file, dev, 680 args->size, &args->handle); 681 } 682 683 /** 684 * Creates a new mm object and returns a handle to it. 685 * @dev: drm device pointer 686 * @data: ioctl data blob 687 * @file: drm file pointer 688 */ 689 int 690 i915_gem_create_ioctl(struct drm_device *dev, void *data, 691 struct drm_file *file) 692 { 693 struct drm_i915_gem_create *args = data; 694 695 i915_gem_flush_free_objects(to_i915(dev)); 696 697 return i915_gem_create(file, dev, 698 args->size, &args->handle); 699 } 700 701 static inline int 702 __copy_to_user_swizzled(char __user *cpu_vaddr, 703 const char *gpu_vaddr, int gpu_offset, 704 int length) 705 { 706 int ret, cpu_offset = 0; 707 708 while (length > 0) { 709 int cacheline_end = ALIGN(gpu_offset + 1, 64); 710 int this_length = min(cacheline_end - gpu_offset, length); 711 int swizzled_gpu_offset = gpu_offset ^ 64; 712 713 ret = __copy_to_user(cpu_vaddr + cpu_offset, 714 gpu_vaddr + swizzled_gpu_offset, 715 this_length); 716 if (ret) 717 return ret + length; 718 719 cpu_offset += this_length; 720 gpu_offset += this_length; 721 length -= this_length; 722 } 723 724 return 0; 725 } 726 727 static inline int 728 __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, 729 const char __user *cpu_vaddr, 730 int length) 731 { 732 int ret, cpu_offset = 0; 733 734 while (length > 0) { 735 int cacheline_end = ALIGN(gpu_offset + 1, 64); 736 int this_length = min(cacheline_end - gpu_offset, length); 737 int swizzled_gpu_offset = gpu_offset ^ 64; 738 739 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, 740 cpu_vaddr + cpu_offset, 741 this_length); 742 if (ret) 743 return ret + length; 744 745 cpu_offset += this_length; 746 gpu_offset += this_length; 747 length -= this_length; 748 } 749 750 return 0; 751 } 752 753 /* 754 * Pins the specified object's pages and synchronizes the object with 755 * GPU accesses. Sets needs_clflush to non-zero if the caller should 756 * flush the object from the CPU cache. 757 */ 758 int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj, 759 unsigned int *needs_clflush) 760 { 761 int ret; 762 763 lockdep_assert_held(&obj->base.dev->struct_mutex); 764 765 *needs_clflush = 0; 766 if (!i915_gem_object_has_struct_page(obj)) 767 return -ENODEV; 768 769 ret = i915_gem_object_wait(obj, 770 I915_WAIT_INTERRUPTIBLE | 771 I915_WAIT_LOCKED, 772 MAX_SCHEDULE_TIMEOUT, 773 NULL); 774 if (ret) 775 return ret; 776 777 ret = i915_gem_object_pin_pages(obj); 778 if (ret) 779 return ret; 780 781 i915_gem_object_flush_gtt_write_domain(obj); 782 783 /* If we're not in the cpu read domain, set ourself into the gtt 784 * read domain and manually flush cachelines (if required). This 785 * optimizes for the case when the gpu will dirty the data 786 * anyway again before the next pread happens. 787 */ 788 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) 789 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev, 790 obj->cache_level); 791 792 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) { 793 ret = i915_gem_object_set_to_cpu_domain(obj, false); 794 if (ret) 795 goto err_unpin; 796 797 *needs_clflush = 0; 798 } 799 800 /* return with the pages pinned */ 801 return 0; 802 803 err_unpin: 804 i915_gem_object_unpin_pages(obj); 805 return ret; 806 } 807 808 int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, 809 unsigned int *needs_clflush) 810 { 811 int ret; 812 813 lockdep_assert_held(&obj->base.dev->struct_mutex); 814 815 *needs_clflush = 0; 816 if (!i915_gem_object_has_struct_page(obj)) 817 return -ENODEV; 818 819 ret = i915_gem_object_wait(obj, 820 I915_WAIT_INTERRUPTIBLE | 821 I915_WAIT_LOCKED | 822 I915_WAIT_ALL, 823 MAX_SCHEDULE_TIMEOUT, 824 NULL); 825 if (ret) 826 return ret; 827 828 ret = i915_gem_object_pin_pages(obj); 829 if (ret) 830 return ret; 831 832 i915_gem_object_flush_gtt_write_domain(obj); 833 834 /* If we're not in the cpu write domain, set ourself into the 835 * gtt write domain and manually flush cachelines (as required). 836 * This optimizes for the case when the gpu will use the data 837 * right away and we therefore have to clflush anyway. 838 */ 839 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) 840 *needs_clflush |= cpu_write_needs_clflush(obj) << 1; 841 842 /* Same trick applies to invalidate partially written cachelines read 843 * before writing. 844 */ 845 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) 846 *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev, 847 obj->cache_level); 848 849 if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) { 850 ret = i915_gem_object_set_to_cpu_domain(obj, true); 851 if (ret) 852 goto err_unpin; 853 854 *needs_clflush = 0; 855 } 856 857 if ((*needs_clflush & CLFLUSH_AFTER) == 0) 858 obj->cache_dirty = true; 859 860 intel_fb_obj_invalidate(obj, ORIGIN_CPU); 861 obj->mm.dirty = true; 862 /* return with the pages pinned */ 863 return 0; 864 865 err_unpin: 866 i915_gem_object_unpin_pages(obj); 867 return ret; 868 } 869 870 static void 871 shmem_clflush_swizzled_range(char *addr, unsigned long length, 872 bool swizzled) 873 { 874 if (unlikely(swizzled)) { 875 unsigned long start = (unsigned long) addr; 876 unsigned long end = (unsigned long) addr + length; 877 878 /* For swizzling simply ensure that we always flush both 879 * channels. Lame, but simple and it works. Swizzled 880 * pwrite/pread is far from a hotpath - current userspace 881 * doesn't use it at all. */ 882 start = round_down(start, 128); 883 end = round_up(end, 128); 884 885 drm_clflush_virt_range((void *)start, end - start); 886 } else { 887 drm_clflush_virt_range(addr, length); 888 } 889 890 } 891 892 /* Only difference to the fast-path function is that this can handle bit17 893 * and uses non-atomic copy and kmap functions. */ 894 static int 895 shmem_pread_slow(struct page *page, int offset, int length, 896 char __user *user_data, 897 bool page_do_bit17_swizzling, bool needs_clflush) 898 { 899 char *vaddr; 900 int ret; 901 902 vaddr = kmap(page); 903 if (needs_clflush) 904 shmem_clflush_swizzled_range(vaddr + offset, length, 905 page_do_bit17_swizzling); 906 907 if (page_do_bit17_swizzling) 908 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); 909 else 910 ret = __copy_to_user(user_data, vaddr + offset, length); 911 kunmap(page); 912 913 return ret ? - EFAULT : 0; 914 } 915 916 static int 917 shmem_pread(struct page *page, int offset, int length, char __user *user_data, 918 bool page_do_bit17_swizzling, bool needs_clflush) 919 { 920 int ret; 921 922 ret = -ENODEV; 923 if (!page_do_bit17_swizzling) { 924 char *vaddr = kmap_atomic(page); 925 926 if (needs_clflush) 927 drm_clflush_virt_range(vaddr + offset, length); 928 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); 929 kunmap_atomic(vaddr); 930 } 931 if (ret == 0) 932 return 0; 933 934 return shmem_pread_slow(page, offset, length, user_data, 935 page_do_bit17_swizzling, needs_clflush); 936 } 937 938 static int 939 i915_gem_shmem_pread(struct drm_i915_gem_object *obj, 940 struct drm_i915_gem_pread *args) 941 { 942 char __user *user_data; 943 u64 remain; 944 unsigned int obj_do_bit17_swizzling; 945 unsigned int needs_clflush; 946 unsigned int idx, offset; 947 int ret; 948 949 obj_do_bit17_swizzling = 0; 950 if (i915_gem_object_needs_bit17_swizzle(obj)) 951 obj_do_bit17_swizzling = BIT(17); 952 953 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); 954 if (ret) 955 return ret; 956 957 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); 958 mutex_unlock(&obj->base.dev->struct_mutex); 959 if (ret) 960 return ret; 961 962 remain = args->size; 963 user_data = u64_to_user_ptr(args->data_ptr); 964 offset = offset_in_page(args->offset); 965 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { 966 struct page *page = i915_gem_object_get_page(obj, idx); 967 int length; 968 969 length = remain; 970 if (offset + length > PAGE_SIZE) 971 length = PAGE_SIZE - offset; 972 973 ret = shmem_pread(page, offset, length, user_data, 974 page_to_phys(page) & obj_do_bit17_swizzling, 975 needs_clflush); 976 if (ret) 977 break; 978 979 remain -= length; 980 user_data += length; 981 offset = 0; 982 } 983 984 i915_gem_obj_finish_shmem_access(obj); 985 return ret; 986 } 987 988 static inline bool 989 gtt_user_read(struct io_mapping *mapping, 990 loff_t base, int offset, 991 char __user *user_data, int length) 992 { 993 void *vaddr; 994 unsigned long unwritten; 995 996 /* We can use the cpu mem copy function because this is X86. */ 997 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base); 998 unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length); 999 io_mapping_unmap_atomic(vaddr); 1000 if (unwritten) { 1001 vaddr = (void __force *) 1002 io_mapping_map_wc(mapping, base, PAGE_SIZE); 1003 unwritten = copy_to_user(user_data, vaddr + offset, length); 1004 io_mapping_unmap(vaddr); 1005 } 1006 return unwritten; 1007 } 1008 1009 static int 1010 i915_gem_gtt_pread(struct drm_i915_gem_object *obj, 1011 const struct drm_i915_gem_pread *args) 1012 { 1013 struct drm_i915_private *i915 = to_i915(obj->base.dev); 1014 struct i915_ggtt *ggtt = &i915->ggtt; 1015 struct drm_mm_node node; 1016 struct i915_vma *vma; 1017 void __user *user_data; 1018 u64 remain, offset; 1019 int ret; 1020 1021 ret = mutex_lock_interruptible(&i915->drm.struct_mutex); 1022 if (ret) 1023 return ret; 1024 1025 intel_runtime_pm_get(i915); 1026 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 1027 PIN_MAPPABLE | PIN_NONBLOCK); 1028 if (!IS_ERR(vma)) { 1029 node.start = i915_ggtt_offset(vma); 1030 node.allocated = false; 1031 ret = i915_vma_put_fence(vma); 1032 if (ret) { 1033 i915_vma_unpin(vma); 1034 vma = ERR_PTR(ret); 1035 } 1036 } 1037 if (IS_ERR(vma)) { 1038 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); 1039 if (ret) 1040 goto out_unlock; 1041 GEM_BUG_ON(!node.allocated); 1042 } 1043 1044 ret = i915_gem_object_set_to_gtt_domain(obj, false); 1045 if (ret) 1046 goto out_unpin; 1047 1048 mutex_unlock(&i915->drm.struct_mutex); 1049 1050 user_data = u64_to_user_ptr(args->data_ptr); 1051 remain = args->size; 1052 offset = args->offset; 1053 1054 while (remain > 0) { 1055 /* Operation in this page 1056 * 1057 * page_base = page offset within aperture 1058 * page_offset = offset within page 1059 * page_length = bytes to copy for this page 1060 */ 1061 u32 page_base = node.start; 1062 unsigned page_offset = offset_in_page(offset); 1063 unsigned page_length = PAGE_SIZE - page_offset; 1064 page_length = remain < page_length ? remain : page_length; 1065 if (node.allocated) { 1066 wmb(); 1067 ggtt->base.insert_page(&ggtt->base, 1068 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), 1069 node.start, I915_CACHE_NONE, 0); 1070 wmb(); 1071 } else { 1072 page_base += offset & LINUX_PAGE_MASK; 1073 } 1074 1075 if (gtt_user_read(&ggtt->mappable, page_base, page_offset, 1076 user_data, page_length)) { 1077 ret = -EFAULT; 1078 break; 1079 } 1080 1081 remain -= page_length; 1082 user_data += page_length; 1083 offset += page_length; 1084 } 1085 1086 mutex_lock(&i915->drm.struct_mutex); 1087 out_unpin: 1088 if (node.allocated) { 1089 wmb(); 1090 ggtt->base.clear_range(&ggtt->base, 1091 node.start, node.size); 1092 remove_mappable_node(&node); 1093 } else { 1094 i915_vma_unpin(vma); 1095 } 1096 out_unlock: 1097 intel_runtime_pm_put(i915); 1098 mutex_unlock(&i915->drm.struct_mutex); 1099 1100 return ret; 1101 } 1102 1103 /** 1104 * Reads data from the object referenced by handle. 1105 * @dev: drm device pointer 1106 * @data: ioctl data blob 1107 * @file: drm file pointer 1108 * 1109 * On error, the contents of *data are undefined. 1110 */ 1111 int 1112 i915_gem_pread_ioctl(struct drm_device *dev, void *data, 1113 struct drm_file *file) 1114 { 1115 struct drm_i915_gem_pread *args = data; 1116 struct drm_i915_gem_object *obj; 1117 int ret; 1118 1119 if (args->size == 0) 1120 return 0; 1121 1122 #if 0 1123 if (!access_ok(VERIFY_WRITE, 1124 u64_to_user_ptr(args->data_ptr), 1125 args->size)) 1126 return -EFAULT; 1127 #endif 1128 1129 obj = i915_gem_object_lookup(file, args->handle); 1130 if (!obj) 1131 return -ENOENT; 1132 1133 /* Bounds check source. */ 1134 if (args->offset > obj->base.size || 1135 args->size > obj->base.size - args->offset) { 1136 ret = -EINVAL; 1137 goto out; 1138 } 1139 1140 trace_i915_gem_object_pread(obj, args->offset, args->size); 1141 1142 ret = i915_gem_object_wait(obj, 1143 I915_WAIT_INTERRUPTIBLE, 1144 MAX_SCHEDULE_TIMEOUT, 1145 to_rps_client(file)); 1146 if (ret) 1147 goto out; 1148 1149 ret = i915_gem_object_pin_pages(obj); 1150 if (ret) 1151 goto out; 1152 1153 ret = i915_gem_shmem_pread(obj, args); 1154 if (ret == -EFAULT || ret == -ENODEV) 1155 ret = i915_gem_gtt_pread(obj, args); 1156 1157 i915_gem_object_unpin_pages(obj); 1158 out: 1159 i915_gem_object_put(obj); 1160 return ret; 1161 } 1162 1163 /* This is the fast write path which cannot handle 1164 * page faults in the source data 1165 */ 1166 1167 static inline bool 1168 ggtt_write(struct io_mapping *mapping, 1169 loff_t base, int offset, 1170 char __user *user_data, int length) 1171 { 1172 void *vaddr; 1173 unsigned long unwritten; 1174 1175 /* We can use the cpu mem copy function because this is X86. */ 1176 vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base); 1177 unwritten = __copy_from_user_inatomic_nocache(vaddr + offset, 1178 user_data, length); 1179 io_mapping_unmap_atomic(vaddr); 1180 if (unwritten) { 1181 vaddr = (void __force *) 1182 io_mapping_map_wc(mapping, base, PAGE_SIZE); 1183 unwritten = copy_from_user(vaddr + offset, user_data, length); 1184 io_mapping_unmap(vaddr); 1185 } 1186 1187 return unwritten; 1188 } 1189 1190 /** 1191 * This is the fast pwrite path, where we copy the data directly from the 1192 * user into the GTT, uncached. 1193 * @obj: i915 GEM object 1194 * @args: pwrite arguments structure 1195 */ 1196 static int 1197 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, 1198 const struct drm_i915_gem_pwrite *args) 1199 { 1200 struct drm_i915_private *i915 = to_i915(obj->base.dev); 1201 struct i915_ggtt *ggtt = &i915->ggtt; 1202 struct drm_mm_node node; 1203 struct i915_vma *vma; 1204 u64 remain, offset; 1205 void __user *user_data; 1206 int ret; 1207 1208 ret = mutex_lock_interruptible(&i915->drm.struct_mutex); 1209 if (ret) 1210 return ret; 1211 1212 intel_runtime_pm_get(i915); 1213 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 1214 PIN_MAPPABLE | PIN_NONBLOCK); 1215 if (!IS_ERR(vma)) { 1216 node.start = i915_ggtt_offset(vma); 1217 node.allocated = false; 1218 ret = i915_vma_put_fence(vma); 1219 if (ret) { 1220 i915_vma_unpin(vma); 1221 vma = ERR_PTR(ret); 1222 } 1223 } 1224 if (IS_ERR(vma)) { 1225 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); 1226 if (ret) 1227 goto out_unlock; 1228 GEM_BUG_ON(!node.allocated); 1229 } 1230 1231 ret = i915_gem_object_set_to_gtt_domain(obj, true); 1232 if (ret) 1233 goto out_unpin; 1234 1235 mutex_unlock(&i915->drm.struct_mutex); 1236 1237 intel_fb_obj_invalidate(obj, ORIGIN_CPU); 1238 1239 user_data = u64_to_user_ptr(args->data_ptr); 1240 offset = args->offset; 1241 remain = args->size; 1242 while (remain) { 1243 /* Operation in this page 1244 * 1245 * page_base = page offset within aperture 1246 * page_offset = offset within page 1247 * page_length = bytes to copy for this page 1248 */ 1249 u32 page_base = node.start; 1250 unsigned int page_offset = offset_in_page(offset); 1251 unsigned int page_length = PAGE_SIZE - page_offset; 1252 page_length = remain < page_length ? remain : page_length; 1253 if (node.allocated) { 1254 wmb(); /* flush the write before we modify the GGTT */ 1255 ggtt->base.insert_page(&ggtt->base, 1256 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), 1257 node.start, I915_CACHE_NONE, 0); 1258 wmb(); /* flush modifications to the GGTT (insert_page) */ 1259 } else { 1260 page_base += offset & LINUX_PAGE_MASK; 1261 } 1262 /* If we get a fault while copying data, then (presumably) our 1263 * source page isn't available. Return the error and we'll 1264 * retry in the slow path. 1265 * If the object is non-shmem backed, we retry again with the 1266 * path that handles page fault. 1267 */ 1268 if (ggtt_write(&ggtt->mappable, page_base, page_offset, 1269 user_data, page_length)) { 1270 ret = -EFAULT; 1271 break; 1272 } 1273 1274 remain -= page_length; 1275 user_data += page_length; 1276 offset += page_length; 1277 } 1278 intel_fb_obj_flush(obj, false, ORIGIN_CPU); 1279 1280 mutex_lock(&i915->drm.struct_mutex); 1281 out_unpin: 1282 if (node.allocated) { 1283 wmb(); 1284 ggtt->base.clear_range(&ggtt->base, 1285 node.start, node.size); 1286 remove_mappable_node(&node); 1287 } else { 1288 i915_vma_unpin(vma); 1289 } 1290 out_unlock: 1291 intel_runtime_pm_put(i915); 1292 mutex_unlock(&i915->drm.struct_mutex); 1293 return ret; 1294 } 1295 1296 static int 1297 shmem_pwrite_slow(struct page *page, int offset, int length, 1298 char __user *user_data, 1299 bool page_do_bit17_swizzling, 1300 bool needs_clflush_before, 1301 bool needs_clflush_after) 1302 { 1303 char *vaddr; 1304 int ret; 1305 1306 vaddr = kmap(page); 1307 if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) 1308 shmem_clflush_swizzled_range(vaddr + offset, length, 1309 page_do_bit17_swizzling); 1310 if (page_do_bit17_swizzling) 1311 ret = __copy_from_user_swizzled(vaddr, offset, user_data, 1312 length); 1313 else 1314 ret = __copy_from_user(vaddr + offset, user_data, length); 1315 if (needs_clflush_after) 1316 shmem_clflush_swizzled_range(vaddr + offset, length, 1317 page_do_bit17_swizzling); 1318 kunmap(page); 1319 1320 return ret ? -EFAULT : 0; 1321 } 1322 1323 /* Per-page copy function for the shmem pwrite fastpath. 1324 * Flushes invalid cachelines before writing to the target if 1325 * needs_clflush_before is set and flushes out any written cachelines after 1326 * writing if needs_clflush is set. 1327 */ 1328 static int 1329 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, 1330 bool page_do_bit17_swizzling, 1331 bool needs_clflush_before, 1332 bool needs_clflush_after) 1333 { 1334 int ret; 1335 1336 ret = -ENODEV; 1337 if (!page_do_bit17_swizzling) { 1338 char *vaddr = kmap_atomic(page); 1339 1340 if (needs_clflush_before) 1341 drm_clflush_virt_range(vaddr + offset, len); 1342 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); 1343 if (needs_clflush_after) 1344 drm_clflush_virt_range(vaddr + offset, len); 1345 1346 kunmap_atomic(vaddr); 1347 } 1348 if (ret == 0) 1349 return ret; 1350 1351 return shmem_pwrite_slow(page, offset, len, user_data, 1352 page_do_bit17_swizzling, 1353 needs_clflush_before, 1354 needs_clflush_after); 1355 } 1356 1357 static int 1358 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, 1359 const struct drm_i915_gem_pwrite *args) 1360 { 1361 struct drm_i915_private *i915 = to_i915(obj->base.dev); 1362 void __user *user_data; 1363 u64 remain; 1364 unsigned int obj_do_bit17_swizzling; 1365 unsigned int partial_cacheline_write; 1366 unsigned int needs_clflush; 1367 unsigned int offset, idx; 1368 int ret; 1369 1370 ret = mutex_lock_interruptible(&i915->drm.struct_mutex); 1371 if (ret) 1372 return ret; 1373 1374 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); 1375 mutex_unlock(&i915->drm.struct_mutex); 1376 if (ret) 1377 return ret; 1378 1379 obj_do_bit17_swizzling = 0; 1380 if (i915_gem_object_needs_bit17_swizzle(obj)) 1381 obj_do_bit17_swizzling = BIT(17); 1382 1383 /* If we don't overwrite a cacheline completely we need to be 1384 * careful to have up-to-date data by first clflushing. Don't 1385 * overcomplicate things and flush the entire patch. 1386 */ 1387 partial_cacheline_write = 0; 1388 if (needs_clflush & CLFLUSH_BEFORE) 1389 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; 1390 1391 user_data = u64_to_user_ptr(args->data_ptr); 1392 remain = args->size; 1393 offset = offset_in_page(args->offset); 1394 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { 1395 struct page *page = i915_gem_object_get_page(obj, idx); 1396 int length; 1397 1398 length = remain; 1399 if (offset + length > PAGE_SIZE) 1400 length = PAGE_SIZE - offset; 1401 1402 ret = shmem_pwrite(page, offset, length, user_data, 1403 page_to_phys(page) & obj_do_bit17_swizzling, 1404 (offset | length) & partial_cacheline_write, 1405 needs_clflush & CLFLUSH_AFTER); 1406 if (ret) 1407 break; 1408 1409 remain -= length; 1410 user_data += length; 1411 offset = 0; 1412 } 1413 1414 intel_fb_obj_flush(obj, false, ORIGIN_CPU); 1415 i915_gem_obj_finish_shmem_access(obj); 1416 return ret; 1417 } 1418 1419 /** 1420 * Writes data to the object referenced by handle. 1421 * @dev: drm device 1422 * @data: ioctl data blob 1423 * @file: drm file 1424 * 1425 * On error, the contents of the buffer that were to be modified are undefined. 1426 */ 1427 int 1428 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, 1429 struct drm_file *file) 1430 { 1431 struct drm_i915_gem_pwrite *args = data; 1432 struct drm_i915_gem_object *obj; 1433 int ret; 1434 1435 if (args->size == 0) 1436 return 0; 1437 1438 #if 0 1439 if (!access_ok(VERIFY_READ, 1440 u64_to_user_ptr(args->data_ptr), 1441 args->size)) 1442 return -EFAULT; 1443 #endif 1444 1445 obj = i915_gem_object_lookup(file, args->handle); 1446 if (!obj) 1447 return -ENOENT; 1448 1449 /* Bounds check destination. */ 1450 if (args->offset > obj->base.size || 1451 args->size > obj->base.size - args->offset) { 1452 ret = -EINVAL; 1453 goto err; 1454 } 1455 1456 trace_i915_gem_object_pwrite(obj, args->offset, args->size); 1457 1458 ret = i915_gem_object_wait(obj, 1459 I915_WAIT_INTERRUPTIBLE | 1460 I915_WAIT_ALL, 1461 MAX_SCHEDULE_TIMEOUT, 1462 to_rps_client(file)); 1463 if (ret) 1464 goto err; 1465 1466 ret = i915_gem_object_pin_pages(obj); 1467 if (ret) 1468 goto err; 1469 1470 ret = -EFAULT; 1471 /* We can only do the GTT pwrite on untiled buffers, as otherwise 1472 * it would end up going through the fenced access, and we'll get 1473 * different detiling behavior between reading and writing. 1474 * pread/pwrite currently are reading and writing from the CPU 1475 * perspective, requiring manual detiling by the client. 1476 */ 1477 if (!i915_gem_object_has_struct_page(obj) || 1478 cpu_write_needs_clflush(obj)) 1479 /* Note that the gtt paths might fail with non-page-backed user 1480 * pointers (e.g. gtt mappings when moving data between 1481 * textures). Fallback to the shmem path in that case. 1482 */ 1483 ret = i915_gem_gtt_pwrite_fast(obj, args); 1484 1485 if (ret == -EFAULT || ret == -ENOSPC) { 1486 if (obj->phys_handle) 1487 ret = i915_gem_phys_pwrite(obj, args, file); 1488 else 1489 ret = i915_gem_shmem_pwrite(obj, args); 1490 } 1491 1492 i915_gem_object_unpin_pages(obj); 1493 err: 1494 i915_gem_object_put(obj); 1495 return ret; 1496 } 1497 1498 static inline enum fb_op_origin 1499 write_origin(struct drm_i915_gem_object *obj, unsigned domain) 1500 { 1501 return (domain == I915_GEM_DOMAIN_GTT ? 1502 obj->frontbuffer_ggtt_origin : ORIGIN_CPU); 1503 } 1504 1505 static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) 1506 { 1507 struct drm_i915_private *i915; 1508 struct list_head *list; 1509 struct i915_vma *vma; 1510 1511 list_for_each_entry(vma, &obj->vma_list, obj_link) { 1512 if (!i915_vma_is_ggtt(vma)) 1513 continue; 1514 1515 if (i915_vma_is_active(vma)) 1516 continue; 1517 1518 if (!drm_mm_node_allocated(&vma->node)) 1519 continue; 1520 1521 list_move_tail(&vma->vm_link, &vma->vm->inactive_list); 1522 } 1523 1524 i915 = to_i915(obj->base.dev); 1525 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; 1526 list_move_tail(&obj->global_link, list); 1527 } 1528 1529 /** 1530 * Called when user space prepares to use an object with the CPU, either 1531 * through the mmap ioctl's mapping or a GTT mapping. 1532 * @dev: drm device 1533 * @data: ioctl data blob 1534 * @file: drm file 1535 */ 1536 int 1537 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, 1538 struct drm_file *file) 1539 { 1540 struct drm_i915_gem_set_domain *args = data; 1541 struct drm_i915_gem_object *obj; 1542 uint32_t read_domains = args->read_domains; 1543 uint32_t write_domain = args->write_domain; 1544 int err; 1545 1546 /* Only handle setting domains to types used by the CPU. */ 1547 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) 1548 return -EINVAL; 1549 1550 /* Having something in the write domain implies it's in the read 1551 * domain, and only that read domain. Enforce that in the request. 1552 */ 1553 if (write_domain != 0 && read_domains != write_domain) 1554 return -EINVAL; 1555 1556 obj = i915_gem_object_lookup(file, args->handle); 1557 if (!obj) 1558 return -ENOENT; 1559 1560 /* Try to flush the object off the GPU without holding the lock. 1561 * We will repeat the flush holding the lock in the normal manner 1562 * to catch cases where we are gazumped. 1563 */ 1564 err = i915_gem_object_wait(obj, 1565 I915_WAIT_INTERRUPTIBLE | 1566 (write_domain ? I915_WAIT_ALL : 0), 1567 MAX_SCHEDULE_TIMEOUT, 1568 to_rps_client(file)); 1569 if (err) 1570 goto out; 1571 1572 /* Flush and acquire obj->pages so that we are coherent through 1573 * direct access in memory with previous cached writes through 1574 * shmemfs and that our cache domain tracking remains valid. 1575 * For example, if the obj->filp was moved to swap without us 1576 * being notified and releasing the pages, we would mistakenly 1577 * continue to assume that the obj remained out of the CPU cached 1578 * domain. 1579 */ 1580 err = i915_gem_object_pin_pages(obj); 1581 if (err) 1582 goto out; 1583 1584 err = i915_mutex_lock_interruptible(dev); 1585 if (err) 1586 goto out_unpin; 1587 1588 if (read_domains & I915_GEM_DOMAIN_GTT) 1589 err = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); 1590 else 1591 err = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); 1592 1593 /* And bump the LRU for this access */ 1594 i915_gem_object_bump_inactive_ggtt(obj); 1595 1596 mutex_unlock(&dev->struct_mutex); 1597 1598 if (write_domain != 0) 1599 intel_fb_obj_invalidate(obj, write_origin(obj, write_domain)); 1600 1601 out_unpin: 1602 i915_gem_object_unpin_pages(obj); 1603 out: 1604 i915_gem_object_put(obj); 1605 return err; 1606 } 1607 1608 /** 1609 * Called when user space has done writes to this buffer 1610 * @dev: drm device 1611 * @data: ioctl data blob 1612 * @file: drm file 1613 */ 1614 int 1615 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, 1616 struct drm_file *file) 1617 { 1618 struct drm_i915_gem_sw_finish *args = data; 1619 struct drm_i915_gem_object *obj; 1620 int err = 0; 1621 1622 obj = i915_gem_object_lookup(file, args->handle); 1623 if (!obj) 1624 return -ENOENT; 1625 1626 /* Pinned buffers may be scanout, so flush the cache */ 1627 if (READ_ONCE(obj->pin_display)) { 1628 err = i915_mutex_lock_interruptible(dev); 1629 if (!err) { 1630 i915_gem_object_flush_cpu_write_domain(obj); 1631 mutex_unlock(&dev->struct_mutex); 1632 } 1633 } 1634 1635 i915_gem_object_put(obj); 1636 return err; 1637 } 1638 1639 /** 1640 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address 1641 * it is mapped to. 1642 * @dev: drm device 1643 * @data: ioctl data blob 1644 * @file: drm file 1645 * 1646 * While the mapping holds a reference on the contents of the object, it doesn't 1647 * imply a ref on the object itself. 1648 * 1649 * IMPORTANT: 1650 * 1651 * DRM driver writers who look a this function as an example for how to do GEM 1652 * mmap support, please don't implement mmap support like here. The modern way 1653 * to implement DRM mmap support is with an mmap offset ioctl (like 1654 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. 1655 * That way debug tooling like valgrind will understand what's going on, hiding 1656 * the mmap call in a driver private ioctl will break that. The i915 driver only 1657 * does cpu mmaps this way because we didn't know better. 1658 */ 1659 int 1660 i915_gem_mmap_ioctl(struct drm_device *dev, void *data, 1661 struct drm_file *file) 1662 { 1663 struct drm_i915_gem_mmap *args = data; 1664 struct drm_i915_gem_object *obj; 1665 unsigned long addr; 1666 struct proc *p = curproc; 1667 vm_map_t map = &p->p_vmspace->vm_map; 1668 vm_size_t size; 1669 int error = 0, rv; 1670 1671 if (args->flags & ~(I915_MMAP_WC)) 1672 return -EINVAL; 1673 1674 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) 1675 return -ENODEV; 1676 1677 obj = i915_gem_object_lookup(file, args->handle); 1678 if (!obj) 1679 return -ENOENT; 1680 1681 /* prime objects have no backing filp to GEM mmap 1682 * pages from. 1683 */ 1684 if (!obj->base.filp) { 1685 i915_gem_object_put(obj); 1686 return -EINVAL; 1687 } 1688 1689 if (args->size == 0) 1690 goto out; 1691 1692 size = round_page(args->size); 1693 if (map->size + size > p->p_rlimit[RLIMIT_VMEM].rlim_cur) { 1694 error = -ENOMEM; 1695 goto out; 1696 } 1697 1698 /* 1699 * Call hint to ensure that NULL is not returned as a valid address 1700 * and to reduce vm_map traversals. XXX causes instability, use a 1701 * fixed low address as the start point instead to avoid the NULL 1702 * return issue. 1703 */ 1704 addr = PAGE_SIZE; 1705 1706 /* 1707 * Use 256KB alignment. It is unclear why this matters for a 1708 * virtual address but it appears to fix a number of application/X 1709 * crashes and kms console switching is much faster. 1710 */ 1711 vm_object_hold(obj->base.filp); 1712 vm_object_reference_locked(obj->base.filp); 1713 vm_object_drop(obj->base.filp); 1714 1715 /* Something gets wrong here: fails to mmap 4096 */ 1716 rv = vm_map_find(map, obj->base.filp, NULL, 1717 args->offset, &addr, args->size, 1718 256 * 1024, /* align */ 1719 TRUE, /* fitit */ 1720 VM_MAPTYPE_NORMAL, VM_SUBSYS_DRM_GEM, 1721 VM_PROT_READ | VM_PROT_WRITE, /* prot */ 1722 VM_PROT_READ | VM_PROT_WRITE, /* max */ 1723 MAP_SHARED /* cow */); 1724 if (rv != KERN_SUCCESS) { 1725 vm_object_deallocate(obj->base.filp); 1726 error = -vm_mmap_to_errno(rv); 1727 } else { 1728 args->addr_ptr = (uint64_t)addr; 1729 } 1730 1731 if (args->flags & I915_MMAP_WC) { /* I915_PARAM_MMAP_VERSION */ 1732 #if 0 1733 struct mm_struct *mm = current->mm; 1734 struct vm_area_struct *vma; 1735 1736 if (down_write_killable(&mm->mmap_sem)) { 1737 i915_gem_object_put(obj); 1738 return -EINTR; 1739 } 1740 vma = find_vma(mm, addr); 1741 if (vma) 1742 vma->vm_page_prot = 1743 pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 1744 else 1745 addr = -ENOMEM; 1746 up_write(&mm->mmap_sem); 1747 #endif 1748 1749 /* This may race, but that's ok, it only gets set */ 1750 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); 1751 } 1752 1753 out: 1754 i915_gem_object_put(obj); 1755 if (error != 0) 1756 return error; 1757 1758 args->addr_ptr = (uint64_t) addr; 1759 1760 return 0; 1761 } 1762 1763 static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) 1764 { 1765 u64 size; 1766 1767 size = i915_gem_object_get_stride(obj); 1768 size *= i915_gem_object_get_tiling(obj) == I915_TILING_Y ? 32 : 8; 1769 1770 return size >> PAGE_SHIFT; 1771 } 1772 1773 /** 1774 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps 1775 * 1776 * A history of the GTT mmap interface: 1777 * 1778 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to 1779 * aligned and suitable for fencing, and still fit into the available 1780 * mappable space left by the pinned display objects. A classic problem 1781 * we called the page-fault-of-doom where we would ping-pong between 1782 * two objects that could not fit inside the GTT and so the memcpy 1783 * would page one object in at the expense of the other between every 1784 * single byte. 1785 * 1786 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none 1787 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the 1788 * object is too large for the available space (or simply too large 1789 * for the mappable aperture!), a view is created instead and faulted 1790 * into userspace. (This view is aligned and sized appropriately for 1791 * fenced access.) 1792 * 1793 * Restrictions: 1794 * 1795 * * snoopable objects cannot be accessed via the GTT. It can cause machine 1796 * hangs on some architectures, corruption on others. An attempt to service 1797 * a GTT page fault from a snoopable object will generate a SIGBUS. 1798 * 1799 * * the object must be able to fit into RAM (physical memory, though no 1800 * limited to the mappable aperture). 1801 * 1802 * 1803 * Caveats: 1804 * 1805 * * a new GTT page fault will synchronize rendering from the GPU and flush 1806 * all data to system memory. Subsequent access will not be synchronized. 1807 * 1808 * * all mappings are revoked on runtime device suspend. 1809 * 1810 * * there are only 8, 16 or 32 fence registers to share between all users 1811 * (older machines require fence register for display and blitter access 1812 * as well). Contention of the fence registers will cause the previous users 1813 * to be unmapped and any new access will generate new page faults. 1814 * 1815 * * running out of memory while servicing a fault may generate a SIGBUS, 1816 * rather than the expected SIGSEGV. 1817 */ 1818 int i915_gem_mmap_gtt_version(void) 1819 { 1820 return 1; 1821 } 1822 1823 /** 1824 * i915_gem_fault - fault a page into the GTT 1825 * 1826 * vm_obj is locked on entry and expected to be locked on return. 1827 * 1828 * The vm_pager has placemarked the object with an anonymous memory page 1829 * which we must replace atomically to avoid races against concurrent faults 1830 * on the same page. XXX we currently are unable to do this atomically. 1831 * 1832 * If we are to return an error we should not touch the anonymous page, 1833 * the caller will deallocate it. 1834 * 1835 * XXX Most GEM calls appear to be interruptable, but we can't hard loop 1836 * in that case. Release all resources and wait 1 tick before retrying. 1837 * This is a huge problem which needs to be fixed by getting rid of most 1838 * of the interruptability. The linux code does not retry but does appear 1839 * to have some sort of mechanism (VM_FAULT_NOPAGE ?) for the higher level 1840 * to be able to retry. 1841 * 1842 * -- 1843 * @area: CPU VMA in question 1844 * @vmf: fault info 1845 * 1846 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped 1847 * from userspace. The fault handler takes care of binding the object to 1848 * the GTT (if needed), allocating and programming a fence register (again, 1849 * only if needed based on whether the old reg is still valid or the object 1850 * is tiled) and inserting a new PTE into the faulting process. 1851 * 1852 * Note that the faulting process may involve evicting existing objects 1853 * from the GTT and/or fence registers to make room. So performance may 1854 * suffer if the GTT working set is large or there are few fence registers 1855 * left. 1856 * 1857 * The current feature set supported by i915_gem_fault() and thus GTT mmaps 1858 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version). 1859 * vm_obj is locked on entry and expected to be locked on return. The VM 1860 * pager has placed an anonymous memory page at (obj,offset) which we have 1861 * to replace. 1862 */ 1863 int i915_gem_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot, vm_page_t *mres) 1864 { 1865 #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */ 1866 struct drm_i915_gem_object *obj = to_intel_bo(vm_obj->handle); 1867 struct drm_device *dev = obj->base.dev; 1868 struct drm_i915_private *dev_priv = to_i915(dev); 1869 struct i915_ggtt *ggtt = &dev_priv->ggtt; 1870 bool write = !!(prot & VM_PROT_WRITE); 1871 struct i915_vma *vma; 1872 unsigned long page_offset; 1873 unsigned int flags; 1874 int ret; 1875 #ifdef __DragonFly__ 1876 int didref = 0; 1877 vm_page_t m; 1878 struct vm_area_struct tmp_vm_area; 1879 struct vm_area_struct *area = &tmp_vm_area; 1880 1881 /* Fill-in vm_area_struct */ 1882 area->vm_private_data = vm_obj->handle; 1883 /* These values are bogus, but effectively limit view sizes to 8MB */ 1884 area->vm_start = 0; 1885 area->vm_end = 8*1024*1024; 1886 #endif 1887 1888 /* We don't use vmf->pgoff since that has the fake offset */ 1889 page_offset = offset; 1890 1891 /* 1892 * vm_fault() has supplied us with a busied page placeholding 1893 * the operation. This presents a lock order reversal issue 1894 * again i915_gem_release_mmap() for our device mutex. 1895 * 1896 * Deal with the problem by getting rid of the placeholder now, 1897 * and then dealing with the potential for a new placeholder when 1898 * we try to insert later. 1899 */ 1900 if (*mres != NULL) { 1901 m = *mres; 1902 *mres = NULL; 1903 if ((m->busy_count & PBUSY_LOCKED) == 0) 1904 kprintf("i915_gem_fault: Page was not busy\n"); 1905 else 1906 vm_page_remove(m); 1907 vm_page_free(m); 1908 } 1909 m = NULL; 1910 1911 retry: 1912 trace_i915_gem_object_fault(obj, page_offset, true, write); 1913 1914 /* Try to flush the object off the GPU first without holding the lock. 1915 * Upon acquiring the lock, we will perform our sanity checks and then 1916 * repeat the flush holding the lock in the normal manner to catch cases 1917 * where we are gazumped. 1918 */ 1919 ret = i915_gem_object_wait(obj, 1920 I915_WAIT_INTERRUPTIBLE, 1921 MAX_SCHEDULE_TIMEOUT, 1922 NULL); 1923 if (ret) { 1924 kprintf("i915: caught bug(%d) (unsafe_wait_rend)\n", ret); 1925 } 1926 1927 ret = i915_gem_object_pin_pages(obj); 1928 if (ret) 1929 goto err; 1930 1931 intel_runtime_pm_get(dev_priv); 1932 1933 ret = i915_mutex_lock_interruptible(dev); 1934 if (ret) { 1935 if (ret != -EINTR) 1936 kprintf("i915: caught bug(%d) (mutex_lock_inter)\n", ret); 1937 goto err_rpm; 1938 } 1939 1940 /* Access to snoopable pages through the GTT is incoherent. */ 1941 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { 1942 kprintf("i915: caught bug() (cache_level %d %d)\n", 1943 (obj->cache_level), !HAS_LLC(dev_priv)); 1944 ret = -EFAULT; 1945 goto err_unlock; 1946 } 1947 1948 /* If the object is smaller than a couple of partial vma, it is 1949 * not worth only creating a single partial vma - we may as well 1950 * clear enough space for the full object. 1951 */ 1952 flags = PIN_MAPPABLE; 1953 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT) 1954 flags |= PIN_NONBLOCK | PIN_NONFAULT; 1955 1956 /* Now pin it into the GTT as needed */ 1957 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags); 1958 if (IS_ERR(vma)) { 1959 struct i915_ggtt_view view; 1960 unsigned int chunk_size; 1961 1962 /* Use a partial view if it is bigger than available space */ 1963 chunk_size = MIN_CHUNK_PAGES; 1964 if (i915_gem_object_is_tiled(obj)) 1965 chunk_size = roundup(chunk_size, tile_row_pages(obj)); 1966 1967 memset(&view, 0, sizeof(view)); 1968 view.type = I915_GGTT_VIEW_PARTIAL; 1969 view.params.partial.offset = rounddown(page_offset, chunk_size); 1970 view.params.partial.size = 1971 min_t(unsigned int, chunk_size, 1972 vma_pages(area) - view.params.partial.offset); 1973 1974 /* If the partial covers the entire object, just create a 1975 * normal VMA. 1976 */ 1977 if (chunk_size >= obj->base.size >> PAGE_SHIFT) 1978 view.type = I915_GGTT_VIEW_NORMAL; 1979 1980 /* Userspace is now writing through an untracked VMA, abandon 1981 * all hope that the hardware is able to track future writes. 1982 */ 1983 obj->frontbuffer_ggtt_origin = ORIGIN_CPU; 1984 1985 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); 1986 } 1987 if (IS_ERR(vma)) { 1988 kprintf("i915: caught bug() (VMA error %ld objsize %ld)\n", 1989 PTR_ERR(vma), obj->base.size); 1990 ret = PTR_ERR(vma); 1991 goto err_unlock; 1992 } 1993 1994 ret = i915_gem_object_set_to_gtt_domain(obj, write); 1995 if (ret) { 1996 kprintf("i915: caught bug(%d) (set_to_gtt_dom)\n", ret); 1997 goto err_unpin; 1998 } 1999 2000 ret = i915_vma_get_fence(vma); 2001 if (ret) { 2002 kprintf("i915: caught bug(%d) (vma_get_fence)\n", ret); 2003 goto err_unpin; 2004 } 2005 2006 /* 2007 * START FREEBSD MAGIC 2008 * 2009 * Add a pip count to avoid destruction and certain other 2010 * complex operations (such as collapses?) while unlocked. 2011 */ 2012 vm_object_pip_add(vm_obj, 1); 2013 didref = 1; 2014 2015 ret = 0; 2016 m = NULL; 2017 2018 /* 2019 * Since the object lock was dropped, another thread might have 2020 * faulted on the same GTT address and instantiated the mapping. 2021 * Recheck. 2022 */ 2023 m = vm_page_lookup(vm_obj, OFF_TO_IDX(offset)); 2024 if (m != NULL) { 2025 /* 2026 * Try to busy the page, retry on failure (non-zero ret). 2027 */ 2028 if (vm_page_busy_try(m, false)) { 2029 kprintf("i915_gem_fault: BUSY\n"); 2030 ret = -EINTR; 2031 goto err_unpin; 2032 } 2033 goto have_page; 2034 } 2035 /* END FREEBSD MAGIC */ 2036 2037 /* Mark as being mmapped into userspace for later revocation */ 2038 assert_rpm_wakelock_held(dev_priv); 2039 if (list_empty(&obj->userfault_link)) 2040 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); 2041 2042 /* Finally, remap it using the new GTT offset */ 2043 #if 0 2044 ret = remap_io_mapping(area, 2045 area->vm_start + (vma->ggtt_view.params.partial.offset << PAGE_SHIFT), 2046 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT, 2047 min_t(u64, vma->size, area->vm_end - area->vm_start), 2048 &ggtt->mappable); 2049 #else 2050 m = vm_phys_fictitious_to_vm_page(ggtt->mappable_base + 2051 i915_ggtt_offset(vma) + offset); 2052 if (m == NULL) { 2053 kprintf("i915: caught bug() (phys_fict_to_vm)\n"); 2054 ret = -EFAULT; 2055 goto err_unpin; 2056 } 2057 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("not fictitious %p", m)); 2058 KASSERT(m->wire_count == 1, ("wire_count not 1 %p", m)); 2059 2060 /* 2061 * Try to busy the page. Fails on non-zero return. 2062 */ 2063 if (vm_page_busy_try(m, false)) { 2064 kprintf("i915_gem_fault: BUSY(2)\n"); 2065 ret = -EINTR; 2066 goto err_unpin; 2067 } 2068 m->valid = VM_PAGE_BITS_ALL; 2069 2070 /* 2071 * This should always work since we already checked via a lookup 2072 * above. 2073 */ 2074 if (vm_page_insert(m, vm_obj, OFF_TO_IDX(offset)) == FALSE) { 2075 kprintf("i915:gem_fault: page %p,%jd already in object\n", 2076 vm_obj, 2077 OFF_TO_IDX(offset)); 2078 vm_page_wakeup(m); 2079 ret = -EINTR; 2080 goto err_unpin; 2081 } 2082 #endif 2083 2084 have_page: 2085 *mres = m; 2086 2087 __i915_vma_unpin(vma); 2088 mutex_unlock(&dev->struct_mutex); 2089 ret = VM_PAGER_OK; 2090 goto done; 2091 2092 /* 2093 * ALTERNATIVE ERROR RETURN. 2094 * 2095 * OBJECT EXPECTED TO BE LOCKED. 2096 */ 2097 err_unpin: 2098 __i915_vma_unpin(vma); 2099 err_unlock: 2100 mutex_unlock(&dev->struct_mutex); 2101 err_rpm: 2102 intel_runtime_pm_put(dev_priv); 2103 i915_gem_object_unpin_pages(obj); 2104 err: 2105 switch (ret) { 2106 case -EIO: 2107 /* 2108 * We eat errors when the gpu is terminally wedged to avoid 2109 * userspace unduly crashing (gl has no provisions for mmaps to 2110 * fail). But any other -EIO isn't ours (e.g. swap in failure) 2111 * and so needs to be reported. 2112 */ 2113 if (!i915_terminally_wedged(&dev_priv->gpu_error)) { 2114 // ret = VM_FAULT_SIGBUS; 2115 break; 2116 } 2117 case -EAGAIN: 2118 /* 2119 * EAGAIN means the gpu is hung and we'll wait for the error 2120 * handler to reset everything when re-faulting in 2121 * i915_mutex_lock_interruptible. 2122 */ 2123 case -ERESTARTSYS: 2124 case -EINTR: 2125 if (didref) { 2126 kprintf("i915: caught bug(%d) (retry)\n", ret); 2127 vm_object_pip_wakeup(vm_obj); 2128 didref = 0; 2129 } 2130 VM_OBJECT_UNLOCK(vm_obj); 2131 int dummy; 2132 tsleep(&dummy, 0, "delay", 1); /* XXX */ 2133 VM_OBJECT_LOCK(vm_obj); 2134 goto retry; 2135 default: 2136 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); 2137 ret = VM_PAGER_ERROR; 2138 break; 2139 } 2140 2141 done: 2142 if (didref) 2143 vm_object_pip_wakeup(vm_obj); 2144 else 2145 kprintf("i915: caught bug(%d)\n", ret); 2146 2147 return ret; 2148 } 2149 2150 #ifdef __DragonFly__ 2151 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node, 2152 struct address_space *file_mapping) 2153 { 2154 struct drm_i915_gem_object *obj = container_of( 2155 node,struct drm_i915_gem_object, base.vma_node); 2156 vm_object_t devobj; 2157 vm_page_t m; 2158 int i, page_count; 2159 2160 devobj = cdev_pager_lookup(obj); 2161 if (devobj != NULL) { 2162 page_count = OFF_TO_IDX(obj->base.size); 2163 2164 VM_OBJECT_LOCK(devobj); 2165 for (i = 0; i < page_count; i++) { 2166 m = vm_page_lookup_busy_wait(devobj, i, TRUE, "915unm"); 2167 if (m == NULL) 2168 continue; 2169 cdev_pager_free_page(devobj, m); 2170 } 2171 VM_OBJECT_UNLOCK(devobj); 2172 vm_object_deallocate(devobj); 2173 } 2174 } 2175 #endif 2176 2177 /** 2178 * i915_gem_release_mmap - remove physical page mappings 2179 * @obj: obj in question 2180 * 2181 * Preserve the reservation of the mmapping with the DRM core code, but 2182 * relinquish ownership of the pages back to the system. 2183 * 2184 * It is vital that we remove the page mapping if we have mapped a tiled 2185 * object through the GTT and then lose the fence register due to 2186 * resource pressure. Similarly if the object has been moved out of the 2187 * aperture, than pages mapped into userspace must be revoked. Removing the 2188 * mapping will then trigger a page fault on the next user access, allowing 2189 * fixup by i915_gem_fault(). 2190 */ 2191 void 2192 i915_gem_release_mmap(struct drm_i915_gem_object *obj) 2193 { 2194 struct drm_i915_private *i915 = to_i915(obj->base.dev); 2195 2196 /* Serialisation between user GTT access and our code depends upon 2197 * revoking the CPU's PTE whilst the mutex is held. The next user 2198 * pagefault then has to wait until we release the mutex. 2199 * 2200 * Note that RPM complicates somewhat by adding an additional 2201 * requirement that operations to the GGTT be made holding the RPM 2202 * wakeref. 2203 */ 2204 lockdep_assert_held(&i915->drm.struct_mutex); 2205 intel_runtime_pm_get(i915); 2206 2207 if (list_empty(&obj->userfault_link)) 2208 goto out; 2209 2210 list_del_init(&obj->userfault_link); 2211 #ifndef __DragonFly__ 2212 drm_vma_node_unmap(&obj->base.vma_node, 2213 obj->base.dev->anon_inode->i_mapping); 2214 #else 2215 drm_vma_node_unmap(&obj->base.vma_node, NULL); 2216 #endif 2217 2218 /* Ensure that the CPU's PTE are revoked and there are not outstanding 2219 * memory transactions from userspace before we return. The TLB 2220 * flushing implied above by changing the PTE above *should* be 2221 * sufficient, an extra barrier here just provides us with a bit 2222 * of paranoid documentation about our requirement to serialise 2223 * memory writes before touching registers / GSM. 2224 */ 2225 wmb(); 2226 2227 out: 2228 intel_runtime_pm_put(i915); 2229 } 2230 2231 void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) 2232 { 2233 struct drm_i915_gem_object *obj, *on; 2234 int i; 2235 2236 /* 2237 * Only called during RPM suspend. All users of the userfault_list 2238 * must be holding an RPM wakeref to ensure that this can not 2239 * run concurrently with themselves (and use the struct_mutex for 2240 * protection between themselves). 2241 */ 2242 2243 list_for_each_entry_safe(obj, on, 2244 &dev_priv->mm.userfault_list, userfault_link) { 2245 list_del_init(&obj->userfault_link); 2246 #ifndef __DragonFly__ 2247 drm_vma_node_unmap(&obj->base.vma_node, 2248 obj->base.dev->anon_inode->i_mapping); 2249 #else 2250 drm_vma_node_unmap(&obj->base.vma_node, NULL); 2251 #endif 2252 } 2253 2254 /* The fence will be lost when the device powers down. If any were 2255 * in use by hardware (i.e. they are pinned), we should not be powering 2256 * down! All other fences will be reacquired by the user upon waking. 2257 */ 2258 for (i = 0; i < dev_priv->num_fence_regs; i++) { 2259 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; 2260 2261 /* Ideally we want to assert that the fence register is not 2262 * live at this point (i.e. that no piece of code will be 2263 * trying to write through fence + GTT, as that both violates 2264 * our tracking of activity and associated locking/barriers, 2265 * but also is illegal given that the hw is powered down). 2266 * 2267 * Previously we used reg->pin_count as a "liveness" indicator. 2268 * That is not sufficient, and we need a more fine-grained 2269 * tool if we want to have a sanity check here. 2270 */ 2271 2272 if (!reg->vma) 2273 continue; 2274 2275 GEM_BUG_ON(!list_empty(®->vma->obj->userfault_link)); 2276 reg->dirty = true; 2277 } 2278 } 2279 2280 /** 2281 * i915_gem_get_ggtt_size - return required global GTT size for an object 2282 * @dev_priv: i915 device 2283 * @size: object size 2284 * @tiling_mode: tiling mode 2285 * 2286 * Return the required global GTT size for an object, taking into account 2287 * potential fence register mapping. 2288 */ 2289 u64 i915_gem_get_ggtt_size(struct drm_i915_private *dev_priv, 2290 u64 size, int tiling_mode) 2291 { 2292 u64 ggtt_size; 2293 2294 GEM_BUG_ON(size == 0); 2295 2296 if (INTEL_GEN(dev_priv) >= 4 || 2297 tiling_mode == I915_TILING_NONE) 2298 return size; 2299 2300 /* Previous chips need a power-of-two fence region when tiling */ 2301 if (IS_GEN3(dev_priv)) 2302 ggtt_size = 1024*1024; 2303 else 2304 ggtt_size = 512*1024; 2305 2306 while (ggtt_size < size) 2307 ggtt_size <<= 1; 2308 2309 return ggtt_size; 2310 } 2311 2312 /** 2313 * i915_gem_get_ggtt_alignment - return required global GTT alignment 2314 * @dev_priv: i915 device 2315 * @size: object size 2316 * @tiling_mode: tiling mode 2317 * @fenced: is fenced alignment required or not 2318 * 2319 * Return the required global GTT alignment for an object, taking into account 2320 * potential fence register mapping. 2321 */ 2322 u64 i915_gem_get_ggtt_alignment(struct drm_i915_private *dev_priv, u64 size, 2323 int tiling_mode, bool fenced) 2324 { 2325 GEM_BUG_ON(size == 0); 2326 2327 /* 2328 * Minimum alignment is 4k (GTT page size), but might be greater 2329 * if a fence register is needed for the object. 2330 */ 2331 if (INTEL_GEN(dev_priv) >= 4 || (!fenced && IS_G33(dev_priv)) || 2332 tiling_mode == I915_TILING_NONE) 2333 return 4096; 2334 2335 /* 2336 * Previous chips need to be aligned to the size of the smallest 2337 * fence register that can contain the object. 2338 */ 2339 return i915_gem_get_ggtt_size(dev_priv, size, tiling_mode); 2340 } 2341 2342 static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) 2343 { 2344 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 2345 int err; 2346 2347 err = drm_gem_create_mmap_offset(&obj->base); 2348 if (!err) 2349 return 0; 2350 2351 /* We can idle the GPU locklessly to flush stale objects, but in order 2352 * to claim that space for ourselves, we need to take the big 2353 * struct_mutex to free the requests+objects and allocate our slot. 2354 */ 2355 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE); 2356 if (err) 2357 return err; 2358 2359 err = i915_mutex_lock_interruptible(&dev_priv->drm); 2360 if (!err) { 2361 i915_gem_retire_requests(dev_priv); 2362 err = drm_gem_create_mmap_offset(&obj->base); 2363 mutex_unlock(&dev_priv->drm.struct_mutex); 2364 } 2365 2366 return err; 2367 } 2368 2369 #if 0 2370 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) 2371 { 2372 drm_gem_free_mmap_offset(&obj->base); 2373 } 2374 #endif 2375 2376 int 2377 i915_gem_mmap_gtt(struct drm_file *file, 2378 struct drm_device *dev, 2379 uint32_t handle, 2380 uint64_t *offset) 2381 { 2382 struct drm_i915_gem_object *obj; 2383 int ret; 2384 2385 obj = i915_gem_object_lookup(file, handle); 2386 if (!obj) 2387 return -ENOENT; 2388 2389 ret = i915_gem_object_create_mmap_offset(obj); 2390 if (ret == 0) 2391 #if 0 2392 *offset = drm_vma_node_offset_addr(&obj->base.vma_node); 2393 #endif 2394 *offset = DRM_GEM_MAPPING_OFF(obj->base.map_list.key) | 2395 DRM_GEM_MAPPING_KEY; 2396 2397 i915_gem_object_put(obj); 2398 return ret; 2399 } 2400 2401 /** 2402 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing 2403 * @dev: DRM device 2404 * @data: GTT mapping ioctl data 2405 * @file: GEM object info 2406 * 2407 * Simply returns the fake offset to userspace so it can mmap it. 2408 * The mmap call will end up in drm_gem_mmap(), which will set things 2409 * up so we can get faults in the handler above. 2410 * 2411 * The fault handler will take care of binding the object into the GTT 2412 * (since it may have been evicted to make room for something), allocating 2413 * a fence register, and mapping the appropriate aperture address into 2414 * userspace. 2415 */ 2416 int 2417 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, 2418 struct drm_file *file) 2419 { 2420 struct drm_i915_gem_mmap_gtt *args = data; 2421 2422 return i915_gem_mmap_gtt(file, dev, args->handle, (uint64_t *)&args->offset); 2423 } 2424 2425 /* Immediately discard the backing storage */ 2426 static void 2427 i915_gem_object_truncate(struct drm_i915_gem_object *obj) 2428 { 2429 vm_object_t vm_obj = obj->base.filp; 2430 2431 if (obj->base.filp == NULL) 2432 return; 2433 2434 VM_OBJECT_LOCK(vm_obj); 2435 vm_object_page_remove(vm_obj, 0, 0, false); 2436 VM_OBJECT_UNLOCK(vm_obj); 2437 2438 /* Our goal here is to return as much of the memory as 2439 * is possible back to the system as we are called from OOM. 2440 * To do this we must instruct the shmfs to drop all of its 2441 * backing pages, *now*. 2442 */ 2443 #if 0 2444 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); 2445 #endif 2446 obj->mm.madv = __I915_MADV_PURGED; 2447 obj->mm.pages = ERR_PTR(-EFAULT); 2448 } 2449 2450 /* Try to discard unwanted pages */ 2451 void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) 2452 { 2453 #if 0 2454 struct address_space *mapping; 2455 #endif 2456 2457 lockdep_assert_held(&obj->mm.lock); 2458 GEM_BUG_ON(obj->mm.pages); 2459 2460 switch (obj->mm.madv) { 2461 case I915_MADV_DONTNEED: 2462 i915_gem_object_truncate(obj); 2463 case __I915_MADV_PURGED: 2464 return; 2465 } 2466 2467 if (obj->base.filp == NULL) 2468 return; 2469 2470 #if 0 2471 mapping = file_inode(obj->base.filp)->i_mapping, 2472 invalidate_mapping_pages(mapping, 0, (loff_t)-1); 2473 #else 2474 invalidate_mapping_pages(obj->base.filp, 0, (loff_t)-1); 2475 #endif 2476 } 2477 2478 static void 2479 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, 2480 struct sg_table *pages) 2481 { 2482 struct sgt_iter sgt_iter; 2483 struct page *page; 2484 2485 #ifdef __DragonFly__ 2486 if (pages == NULL) 2487 return; 2488 #endif 2489 2490 __i915_gem_object_release_shmem(obj, pages, true); 2491 2492 i915_gem_gtt_finish_pages(obj, pages); 2493 2494 if (i915_gem_object_needs_bit17_swizzle(obj)) 2495 i915_gem_object_save_bit_17_swizzle(obj, pages); 2496 2497 for_each_sgt_page(page, sgt_iter, pages) { 2498 if (obj->mm.dirty) 2499 set_page_dirty(page); 2500 2501 if (obj->mm.madv == I915_MADV_WILLNEED) 2502 mark_page_accessed(page); 2503 2504 vm_page_busy_wait((struct vm_page *)page, FALSE, "i915gem"); 2505 vm_page_unwire((struct vm_page *)page, 1); 2506 vm_page_wakeup((struct vm_page *)page); 2507 } 2508 obj->mm.dirty = false; 2509 2510 sg_free_table(pages); 2511 kfree(pages); 2512 } 2513 2514 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) 2515 { 2516 struct radix_tree_iter iter; 2517 void **slot; 2518 2519 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) 2520 radix_tree_delete(&obj->mm.get_page.radix, iter.index); 2521 } 2522 2523 void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, 2524 enum i915_mm_subclass subclass) 2525 { 2526 struct sg_table *pages; 2527 2528 if (i915_gem_object_has_pinned_pages(obj)) 2529 return; 2530 2531 GEM_BUG_ON(obj->bind_count); 2532 if (!READ_ONCE(obj->mm.pages)) 2533 return; 2534 2535 /* May be called by shrinker from within get_pages() (on another bo) */ 2536 mutex_lock_nested(&obj->mm.lock, subclass); 2537 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) 2538 goto unlock; 2539 2540 /* ->put_pages might need to allocate memory for the bit17 swizzle 2541 * array, hence protect them from being reaped by removing them from gtt 2542 * lists early. */ 2543 pages = fetch_and_zero(&obj->mm.pages); 2544 GEM_BUG_ON(!pages); 2545 2546 if (obj->mm.mapping) { 2547 void *ptr; 2548 2549 ptr = ptr_mask_bits(obj->mm.mapping); 2550 if (is_vmalloc_addr(ptr)) 2551 vunmap(ptr); 2552 else 2553 kunmap(kmap_to_page(ptr)); 2554 2555 obj->mm.mapping = NULL; 2556 } 2557 2558 __i915_gem_object_reset_page_iter(obj); 2559 2560 if (!IS_ERR(pages)) 2561 obj->ops->put_pages(obj, pages); 2562 2563 unlock: 2564 mutex_unlock(&obj->mm.lock); 2565 } 2566 2567 static void i915_sg_trim(struct sg_table *orig_st) 2568 { 2569 struct sg_table new_st; 2570 struct scatterlist *sg, *new_sg; 2571 unsigned int i; 2572 2573 if (orig_st->nents == orig_st->orig_nents) 2574 return; 2575 2576 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN)) 2577 return; 2578 2579 new_sg = new_st.sgl; 2580 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { 2581 sg_set_page(new_sg, sg_page(sg), sg->length, 0); 2582 /* called before being DMA mapped, no need to copy sg->dma_* */ 2583 new_sg = sg_next(new_sg); 2584 } 2585 2586 sg_free_table(orig_st); 2587 2588 *orig_st = new_st; 2589 } 2590 2591 static struct sg_table * 2592 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) 2593 { 2594 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 2595 const unsigned long page_count = obj->base.size / PAGE_SIZE; 2596 unsigned long i; 2597 vm_object_t vm_obj; 2598 struct sg_table *st; 2599 struct scatterlist *sg; 2600 struct sgt_iter sgt_iter; 2601 struct page *page; 2602 unsigned long last_pfn = 0; /* suppress gcc warning */ 2603 unsigned int max_segment; 2604 int ret; 2605 2606 /* Assert that the object is not currently in any GPU domain. As it 2607 * wasn't in the GTT, there shouldn't be any way it could have been in 2608 * a GPU cache 2609 */ 2610 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); 2611 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); 2612 2613 max_segment = swiotlb_max_segment(); 2614 if (!max_segment) 2615 max_segment = rounddown(UINT_MAX, PAGE_SIZE); 2616 2617 st = kmalloc(sizeof(*st), M_DRM, GFP_KERNEL); 2618 if (st == NULL) 2619 return ERR_PTR(-ENOMEM); 2620 2621 rebuild_st: 2622 if (sg_alloc_table(st, page_count, GFP_KERNEL)) { 2623 kfree(st); 2624 return ERR_PTR(-ENOMEM); 2625 } 2626 2627 /* Get the list of pages out of our struct file. They'll be pinned 2628 * at this point until we release them. 2629 * 2630 * Fail silently without starting the shrinker 2631 */ 2632 vm_obj = obj->base.filp; 2633 VM_OBJECT_LOCK(vm_obj); 2634 sg = st->sgl; 2635 st->nents = 0; 2636 for (i = 0; i < page_count; i++) { 2637 page = shmem_read_mapping_page(vm_obj, i); 2638 if (IS_ERR(page)) { 2639 i915_gem_shrink(dev_priv, 2640 page_count, 2641 I915_SHRINK_BOUND | 2642 I915_SHRINK_UNBOUND | 2643 I915_SHRINK_PURGEABLE); 2644 page = shmem_read_mapping_page(vm_obj, i); 2645 } 2646 if (IS_ERR(page)) { 2647 /* We've tried hard to allocate the memory by reaping 2648 * our own buffer, now let the real VM do its job and 2649 * go down in flames if truly OOM. 2650 */ 2651 page = shmem_read_mapping_page(vm_obj, i); 2652 if (IS_ERR(page)) { 2653 ret = PTR_ERR(page); 2654 goto err_sg; 2655 } 2656 } 2657 if (!i || 2658 sg->length >= max_segment || 2659 page_to_pfn(page) != last_pfn + 1) { 2660 if (i) 2661 sg = sg_next(sg); 2662 st->nents++; 2663 sg_set_page(sg, page, PAGE_SIZE, 0); 2664 } else { 2665 sg->length += PAGE_SIZE; 2666 } 2667 last_pfn = page_to_pfn(page); 2668 2669 /* Check that the i965g/gm workaround works. */ 2670 } 2671 if (sg) /* loop terminated early; short sg table */ 2672 sg_mark_end(sg); 2673 VM_OBJECT_UNLOCK(vm_obj); 2674 2675 /* Trim unused sg entries to avoid wasting memory. */ 2676 i915_sg_trim(st); 2677 2678 ret = i915_gem_gtt_prepare_pages(obj, st); 2679 if (ret) { 2680 /* DMA remapping failed? One possible cause is that 2681 * it could not reserve enough large entries, asking 2682 * for PAGE_SIZE chunks instead may be helpful. 2683 */ 2684 if (max_segment > PAGE_SIZE) { 2685 for_each_sgt_page(page, sgt_iter, st) 2686 put_page(page); 2687 sg_free_table(st); 2688 2689 max_segment = PAGE_SIZE; 2690 goto rebuild_st; 2691 } else { 2692 dev_warn(&dev_priv->drm.pdev->dev, 2693 "Failed to DMA remap %lu pages\n", 2694 page_count); 2695 goto err_pages; 2696 } 2697 } 2698 2699 if (i915_gem_object_needs_bit17_swizzle(obj)) 2700 i915_gem_object_do_bit_17_swizzle(obj, st); 2701 2702 return st; 2703 2704 err_sg: 2705 sg_mark_end(sg); 2706 err_pages: 2707 for_each_sgt_page(page, sgt_iter, st) 2708 { 2709 /* 2710 XXX DragonFly: using put_page() causes problems here 2711 it currently always uses vm_page_unwire(vmp, 1); 2712 */ 2713 struct vm_page *vmp = (struct vm_page *)page; 2714 vm_page_busy_wait(vmp, FALSE, "i915gem"); 2715 vm_page_unwire(vmp, 0); 2716 vm_page_wakeup(vmp); 2717 } 2718 VM_OBJECT_UNLOCK(vm_obj); 2719 sg_free_table(st); 2720 kfree(st); 2721 2722 /* shmemfs first checks if there is enough memory to allocate the page 2723 * and reports ENOSPC should there be insufficient, along with the usual 2724 * ENOMEM for a genuine allocation failure. 2725 * 2726 * We use ENOSPC in our driver to mean that we have run out of aperture 2727 * space and so want to translate the error from shmemfs back to our 2728 * usual understanding of ENOMEM. 2729 */ 2730 if (ret == -ENOSPC) 2731 ret = -ENOMEM; 2732 2733 return ERR_PTR(ret); 2734 } 2735 2736 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, 2737 struct sg_table *pages) 2738 { 2739 lockdep_assert_held(&obj->mm.lock); 2740 2741 obj->mm.get_page.sg_pos = pages->sgl; 2742 obj->mm.get_page.sg_idx = 0; 2743 2744 obj->mm.pages = pages; 2745 2746 if (i915_gem_object_is_tiled(obj) && 2747 to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) { 2748 GEM_BUG_ON(obj->mm.quirked); 2749 __i915_gem_object_pin_pages(obj); 2750 obj->mm.quirked = true; 2751 } 2752 } 2753 2754 static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) 2755 { 2756 struct sg_table *pages; 2757 2758 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); 2759 2760 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { 2761 DRM_DEBUG("Attempting to obtain a purgeable object\n"); 2762 return -EFAULT; 2763 } 2764 2765 pages = obj->ops->get_pages(obj); 2766 if (unlikely(IS_ERR(pages))) 2767 return PTR_ERR(pages); 2768 2769 __i915_gem_object_set_pages(obj, pages); 2770 return 0; 2771 } 2772 2773 /* Ensure that the associated pages are gathered from the backing storage 2774 * and pinned into our object. i915_gem_object_pin_pages() may be called 2775 * multiple times before they are released by a single call to 2776 * i915_gem_object_unpin_pages() - once the pages are no longer referenced 2777 * either as a result of memory pressure (reaping pages under the shrinker) 2778 * or as the object is itself released. 2779 */ 2780 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) 2781 { 2782 int err; 2783 2784 err = mutex_lock_interruptible(&obj->mm.lock); 2785 if (err) 2786 return err; 2787 2788 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) { 2789 err = ____i915_gem_object_get_pages(obj); 2790 if (err) 2791 goto unlock; 2792 2793 smp_mb__before_atomic(); 2794 } 2795 atomic_inc(&obj->mm.pages_pin_count); 2796 2797 unlock: 2798 mutex_unlock(&obj->mm.lock); 2799 return err; 2800 } 2801 2802 /* The 'mapping' part of i915_gem_object_pin_map() below */ 2803 static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, 2804 enum i915_map_type type) 2805 { 2806 unsigned long n_pages = obj->base.size >> PAGE_SHIFT; 2807 struct sg_table *sgt = obj->mm.pages; 2808 struct sgt_iter sgt_iter; 2809 struct page *page; 2810 struct page *stack_pages[32]; 2811 struct page **pages = stack_pages; 2812 unsigned long i = 0; 2813 pgprot_t pgprot; 2814 void *addr; 2815 2816 /* A single page can always be kmapped */ 2817 if (n_pages == 1 && type == I915_MAP_WB) 2818 return kmap(sg_page(sgt->sgl)); 2819 2820 if (n_pages > ARRAY_SIZE(stack_pages)) { 2821 /* Too big for stack -- allocate temporary array instead */ 2822 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY); 2823 if (!pages) 2824 return NULL; 2825 } 2826 2827 for_each_sgt_page(page, sgt_iter, sgt) 2828 pages[i++] = page; 2829 2830 /* Check that we have the expected number of pages */ 2831 GEM_BUG_ON(i != n_pages); 2832 2833 switch (type) { 2834 case I915_MAP_WB: 2835 pgprot = PAGE_KERNEL; 2836 break; 2837 case I915_MAP_WC: 2838 pgprot = pgprot_writecombine(PAGE_KERNEL_IO); 2839 break; 2840 } 2841 addr = vmap(pages, n_pages, 0, pgprot); 2842 2843 if (pages != stack_pages) 2844 drm_free_large(pages); 2845 2846 return addr; 2847 } 2848 2849 /* get, pin, and map the pages of the object into kernel space */ 2850 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, 2851 enum i915_map_type type) 2852 { 2853 enum i915_map_type has_type; 2854 bool pinned; 2855 void *ptr; 2856 int ret; 2857 2858 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); 2859 2860 ret = mutex_lock_interruptible(&obj->mm.lock); 2861 if (ret) 2862 return ERR_PTR(ret); 2863 2864 pinned = true; 2865 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { 2866 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) { 2867 ret = ____i915_gem_object_get_pages(obj); 2868 if (ret) 2869 goto err_unlock; 2870 2871 smp_mb__before_atomic(); 2872 } 2873 atomic_inc(&obj->mm.pages_pin_count); 2874 pinned = false; 2875 } 2876 GEM_BUG_ON(!obj->mm.pages); 2877 2878 ptr = ptr_unpack_bits(obj->mm.mapping, has_type); 2879 if (ptr && has_type != type) { 2880 if (pinned) { 2881 ret = -EBUSY; 2882 goto err_unpin; 2883 } 2884 2885 if (is_vmalloc_addr(ptr)) 2886 vunmap(ptr); 2887 else 2888 kunmap(kmap_to_page(ptr)); 2889 2890 ptr = obj->mm.mapping = NULL; 2891 } 2892 2893 if (!ptr) { 2894 ptr = i915_gem_object_map(obj, type); 2895 if (!ptr) { 2896 ret = -ENOMEM; 2897 goto err_unpin; 2898 } 2899 2900 obj->mm.mapping = ptr_pack_bits(ptr, type); 2901 } 2902 2903 out_unlock: 2904 mutex_unlock(&obj->mm.lock); 2905 return ptr; 2906 2907 err_unpin: 2908 atomic_dec(&obj->mm.pages_pin_count); 2909 err_unlock: 2910 ptr = ERR_PTR(ret); 2911 goto out_unlock; 2912 } 2913 2914 static bool i915_context_is_banned(const struct i915_gem_context *ctx) 2915 { 2916 unsigned long elapsed; 2917 2918 if (ctx->hang_stats.banned) 2919 return true; 2920 2921 elapsed = get_seconds() - ctx->hang_stats.guilty_ts; 2922 if (ctx->hang_stats.ban_period_seconds && 2923 elapsed <= ctx->hang_stats.ban_period_seconds) { 2924 DRM_DEBUG("context hanging too fast, banning!\n"); 2925 return true; 2926 } 2927 2928 return false; 2929 } 2930 2931 static void i915_set_reset_status(struct i915_gem_context *ctx, 2932 const bool guilty) 2933 { 2934 struct i915_ctx_hang_stats *hs = &ctx->hang_stats; 2935 2936 if (guilty) { 2937 hs->banned = i915_context_is_banned(ctx); 2938 hs->batch_active++; 2939 hs->guilty_ts = get_seconds(); 2940 } else { 2941 hs->batch_pending++; 2942 } 2943 } 2944 2945 struct drm_i915_gem_request * 2946 i915_gem_find_active_request(struct intel_engine_cs *engine) 2947 { 2948 struct drm_i915_gem_request *request; 2949 2950 /* We are called by the error capture and reset at a random 2951 * point in time. In particular, note that neither is crucially 2952 * ordered with an interrupt. After a hang, the GPU is dead and we 2953 * assume that no more writes can happen (we waited long enough for 2954 * all writes that were in transaction to be flushed) - adding an 2955 * extra delay for a recent interrupt is pointless. Hence, we do 2956 * not need an engine->irq_seqno_barrier() before the seqno reads. 2957 */ 2958 list_for_each_entry(request, &engine->timeline->requests, link) { 2959 if (__i915_gem_request_completed(request)) 2960 continue; 2961 2962 return request; 2963 } 2964 2965 return NULL; 2966 } 2967 2968 static void reset_request(struct drm_i915_gem_request *request) 2969 { 2970 void *vaddr = request->ring->vaddr; 2971 u32 head; 2972 2973 /* As this request likely depends on state from the lost 2974 * context, clear out all the user operations leaving the 2975 * breadcrumb at the end (so we get the fence notifications). 2976 */ 2977 head = request->head; 2978 if (request->postfix < head) { 2979 memset(vaddr + head, 0, request->ring->size - head); 2980 head = 0; 2981 } 2982 memset(vaddr + head, 0, request->postfix - head); 2983 } 2984 2985 static void i915_gem_reset_engine(struct intel_engine_cs *engine) 2986 { 2987 struct drm_i915_gem_request *request; 2988 struct i915_gem_context *incomplete_ctx; 2989 struct intel_timeline *timeline; 2990 unsigned long flags; 2991 bool ring_hung; 2992 2993 if (engine->irq_seqno_barrier) 2994 engine->irq_seqno_barrier(engine); 2995 2996 request = i915_gem_find_active_request(engine); 2997 if (!request) 2998 return; 2999 3000 ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG; 3001 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) 3002 ring_hung = false; 3003 3004 i915_set_reset_status(request->ctx, ring_hung); 3005 if (!ring_hung) 3006 return; 3007 3008 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n", 3009 engine->name, request->global_seqno); 3010 3011 /* Setup the CS to resume from the breadcrumb of the hung request */ 3012 engine->reset_hw(engine, request); 3013 3014 /* Users of the default context do not rely on logical state 3015 * preserved between batches. They have to emit full state on 3016 * every batch and so it is safe to execute queued requests following 3017 * the hang. 3018 * 3019 * Other contexts preserve state, now corrupt. We want to skip all 3020 * queued requests that reference the corrupt context. 3021 */ 3022 incomplete_ctx = request->ctx; 3023 if (i915_gem_context_is_default(incomplete_ctx)) 3024 return; 3025 3026 timeline = i915_gem_context_lookup_timeline(incomplete_ctx, engine); 3027 3028 spin_lock_irqsave(&engine->timeline->lock, flags); 3029 lockmgr(&timeline->lock, LK_EXCLUSIVE); 3030 3031 list_for_each_entry_continue(request, &engine->timeline->requests, link) 3032 if (request->ctx == incomplete_ctx) 3033 reset_request(request); 3034 3035 list_for_each_entry(request, &timeline->requests, link) 3036 reset_request(request); 3037 3038 lockmgr(&timeline->lock, LK_RELEASE); 3039 spin_unlock_irqrestore(&engine->timeline->lock, flags); 3040 } 3041 3042 void i915_gem_reset(struct drm_i915_private *dev_priv) 3043 { 3044 struct intel_engine_cs *engine; 3045 enum intel_engine_id id; 3046 3047 lockdep_assert_held(&dev_priv->drm.struct_mutex); 3048 3049 i915_gem_retire_requests(dev_priv); 3050 3051 for_each_engine(engine, dev_priv, id) 3052 i915_gem_reset_engine(engine); 3053 3054 i915_gem_restore_fences(dev_priv); 3055 3056 if (dev_priv->gt.awake) { 3057 intel_sanitize_gt_powersave(dev_priv); 3058 intel_enable_gt_powersave(dev_priv); 3059 if (INTEL_GEN(dev_priv) >= 6) 3060 gen6_rps_busy(dev_priv); 3061 } 3062 } 3063 3064 static void nop_submit_request(struct drm_i915_gem_request *request) 3065 { 3066 i915_gem_request_submit(request); 3067 intel_engine_init_global_seqno(request->engine, request->global_seqno); 3068 } 3069 3070 static void i915_gem_cleanup_engine(struct intel_engine_cs *engine) 3071 { 3072 engine->submit_request = nop_submit_request; 3073 3074 /* Mark all pending requests as complete so that any concurrent 3075 * (lockless) lookup doesn't try and wait upon the request as we 3076 * reset it. 3077 */ 3078 intel_engine_init_global_seqno(engine, 3079 intel_engine_last_submit(engine)); 3080 3081 /* 3082 * Clear the execlists queue up before freeing the requests, as those 3083 * are the ones that keep the context and ringbuffer backing objects 3084 * pinned in place. 3085 */ 3086 3087 if (i915.enable_execlists) { 3088 unsigned long flags; 3089 3090 spin_lock_irqsave(&engine->timeline->lock, flags); 3091 3092 i915_gem_request_put(engine->execlist_port[0].request); 3093 i915_gem_request_put(engine->execlist_port[1].request); 3094 memset(engine->execlist_port, 0, sizeof(engine->execlist_port)); 3095 engine->execlist_queue = LINUX_RB_ROOT; 3096 engine->execlist_first = NULL; 3097 3098 spin_unlock_irqrestore(&engine->timeline->lock, flags); 3099 } 3100 } 3101 3102 void i915_gem_set_wedged(struct drm_i915_private *dev_priv) 3103 { 3104 struct intel_engine_cs *engine; 3105 enum intel_engine_id id; 3106 3107 lockdep_assert_held(&dev_priv->drm.struct_mutex); 3108 set_bit(I915_WEDGED, &dev_priv->gpu_error.flags); 3109 3110 i915_gem_context_lost(dev_priv); 3111 for_each_engine(engine, dev_priv, id) 3112 i915_gem_cleanup_engine(engine); 3113 mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0); 3114 3115 i915_gem_retire_requests(dev_priv); 3116 } 3117 3118 static void 3119 i915_gem_retire_work_handler(struct work_struct *work) 3120 { 3121 struct drm_i915_private *dev_priv = 3122 container_of(work, typeof(*dev_priv), gt.retire_work.work); 3123 struct drm_device *dev = &dev_priv->drm; 3124 3125 /* Come back later if the device is busy... */ 3126 if (mutex_trylock(&dev->struct_mutex)) { 3127 i915_gem_retire_requests(dev_priv); 3128 mutex_unlock(&dev->struct_mutex); 3129 } 3130 3131 /* Keep the retire handler running until we are finally idle. 3132 * We do not need to do this test under locking as in the worst-case 3133 * we queue the retire worker once too often. 3134 */ 3135 if (READ_ONCE(dev_priv->gt.awake)) { 3136 i915_queue_hangcheck(dev_priv); 3137 queue_delayed_work(dev_priv->wq, 3138 &dev_priv->gt.retire_work, 3139 round_jiffies_up_relative(HZ)); 3140 } 3141 } 3142 3143 static void 3144 i915_gem_idle_work_handler(struct work_struct *work) 3145 { 3146 struct drm_i915_private *dev_priv = 3147 container_of(work, typeof(*dev_priv), gt.idle_work.work); 3148 struct drm_device *dev = &dev_priv->drm; 3149 struct intel_engine_cs *engine; 3150 enum intel_engine_id id; 3151 bool rearm_hangcheck; 3152 3153 if (!READ_ONCE(dev_priv->gt.awake)) 3154 return; 3155 3156 /* 3157 * Wait for last execlists context complete, but bail out in case a 3158 * new request is submitted. 3159 */ 3160 wait_for(READ_ONCE(dev_priv->gt.active_requests) || 3161 intel_execlists_idle(dev_priv), 10); 3162 3163 if (READ_ONCE(dev_priv->gt.active_requests)) 3164 return; 3165 3166 rearm_hangcheck = 3167 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); 3168 3169 if (!mutex_trylock(&dev->struct_mutex)) { 3170 /* Currently busy, come back later */ 3171 mod_delayed_work(dev_priv->wq, 3172 &dev_priv->gt.idle_work, 3173 msecs_to_jiffies(50)); 3174 goto out_rearm; 3175 } 3176 3177 /* 3178 * New request retired after this work handler started, extend active 3179 * period until next instance of the work. 3180 */ 3181 if (work_pending(work)) 3182 goto out_unlock; 3183 3184 if (dev_priv->gt.active_requests) 3185 goto out_unlock; 3186 3187 if (wait_for(intel_execlists_idle(dev_priv), 10)) 3188 DRM_ERROR("Timeout waiting for engines to idle\n"); 3189 3190 for_each_engine(engine, dev_priv, id) 3191 i915_gem_batch_pool_fini(&engine->batch_pool); 3192 3193 GEM_BUG_ON(!dev_priv->gt.awake); 3194 dev_priv->gt.awake = false; 3195 rearm_hangcheck = false; 3196 3197 if (INTEL_GEN(dev_priv) >= 6) 3198 gen6_rps_idle(dev_priv); 3199 intel_runtime_pm_put(dev_priv); 3200 out_unlock: 3201 mutex_unlock(&dev->struct_mutex); 3202 3203 out_rearm: 3204 if (rearm_hangcheck) { 3205 GEM_BUG_ON(!dev_priv->gt.awake); 3206 i915_queue_hangcheck(dev_priv); 3207 } 3208 } 3209 3210 void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) 3211 { 3212 struct drm_i915_gem_object *obj = to_intel_bo(gem); 3213 struct drm_i915_file_private *fpriv = file->driver_priv; 3214 struct i915_vma *vma, *vn; 3215 3216 mutex_lock(&obj->base.dev->struct_mutex); 3217 list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link) 3218 if (vma->vm->file == fpriv) 3219 i915_vma_close(vma); 3220 3221 if (i915_gem_object_is_active(obj) && 3222 !i915_gem_object_has_active_reference(obj)) { 3223 i915_gem_object_set_active_reference(obj); 3224 i915_gem_object_get(obj); 3225 } 3226 mutex_unlock(&obj->base.dev->struct_mutex); 3227 } 3228 3229 static unsigned long to_wait_timeout(s64 timeout_ns) 3230 { 3231 if (timeout_ns < 0) 3232 return MAX_SCHEDULE_TIMEOUT; 3233 3234 if (timeout_ns == 0) 3235 return 0; 3236 3237 return nsecs_to_jiffies_timeout(timeout_ns); 3238 } 3239 3240 /** 3241 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT 3242 * @dev: drm device pointer 3243 * @data: ioctl data blob 3244 * @file: drm file pointer 3245 * 3246 * Returns 0 if successful, else an error is returned with the remaining time in 3247 * the timeout parameter. 3248 * -ETIME: object is still busy after timeout 3249 * -ERESTARTSYS: signal interrupted the wait 3250 * -ENONENT: object doesn't exist 3251 * Also possible, but rare: 3252 * -EAGAIN: GPU wedged 3253 * -ENOMEM: damn 3254 * -ENODEV: Internal IRQ fail 3255 * -E?: The add request failed 3256 * 3257 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any 3258 * non-zero timeout parameter the wait ioctl will wait for the given number of 3259 * nanoseconds on an object becoming unbusy. Since the wait itself does so 3260 * without holding struct_mutex the object may become re-busied before this 3261 * function completes. A similar but shorter * race condition exists in the busy 3262 * ioctl 3263 */ 3264 int 3265 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) 3266 { 3267 struct drm_i915_gem_wait *args = data; 3268 struct drm_i915_gem_object *obj; 3269 ktime_t start; 3270 long ret; 3271 3272 if (args->flags != 0) 3273 return -EINVAL; 3274 3275 obj = i915_gem_object_lookup(file, args->bo_handle); 3276 if (!obj) 3277 return -ENOENT; 3278 3279 start = ktime_get(); 3280 3281 ret = i915_gem_object_wait(obj, 3282 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, 3283 to_wait_timeout(args->timeout_ns), 3284 to_rps_client(file)); 3285 3286 if (args->timeout_ns > 0) { 3287 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); 3288 if (args->timeout_ns < 0) 3289 args->timeout_ns = 0; 3290 3291 /* 3292 * Apparently ktime isn't accurate enough and occasionally has a 3293 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch 3294 * things up to make the test happy. We allow up to 1 jiffy. 3295 * 3296 * This is a regression from the timespec->ktime conversion. 3297 */ 3298 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) 3299 args->timeout_ns = 0; 3300 } 3301 3302 i915_gem_object_put(obj); 3303 return ret; 3304 } 3305 3306 static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags) 3307 { 3308 int ret, i; 3309 3310 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { 3311 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags); 3312 if (ret) 3313 return ret; 3314 } 3315 3316 return 0; 3317 } 3318 3319 int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags) 3320 { 3321 int ret; 3322 3323 if (flags & I915_WAIT_LOCKED) { 3324 struct i915_gem_timeline *tl; 3325 3326 lockdep_assert_held(&i915->drm.struct_mutex); 3327 3328 list_for_each_entry(tl, &i915->gt.timelines, link) { 3329 ret = wait_for_timeline(tl, flags); 3330 if (ret) 3331 return ret; 3332 } 3333 } else { 3334 ret = wait_for_timeline(&i915->gt.global_timeline, flags); 3335 if (ret) 3336 return ret; 3337 } 3338 3339 return 0; 3340 } 3341 3342 void i915_gem_clflush_object(struct drm_i915_gem_object *obj, 3343 bool force) 3344 { 3345 /* If we don't have a page list set up, then we're not pinned 3346 * to GPU, and we can ignore the cache flush because it'll happen 3347 * again at bind time. 3348 */ 3349 if (!obj->mm.pages) 3350 return; 3351 3352 /* 3353 * Stolen memory is always coherent with the GPU as it is explicitly 3354 * marked as wc by the system, or the system is cache-coherent. 3355 */ 3356 if (obj->stolen || obj->phys_handle) 3357 return; 3358 3359 /* If the GPU is snooping the contents of the CPU cache, 3360 * we do not need to manually clear the CPU cache lines. However, 3361 * the caches are only snooped when the render cache is 3362 * flushed/invalidated. As we always have to emit invalidations 3363 * and flushes when moving into and out of the RENDER domain, correct 3364 * snooping behaviour occurs naturally as the result of our domain 3365 * tracking. 3366 */ 3367 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) { 3368 obj->cache_dirty = true; 3369 return; 3370 } 3371 3372 trace_i915_gem_object_clflush(obj); 3373 drm_clflush_sg(obj->mm.pages); 3374 obj->cache_dirty = false; 3375 } 3376 3377 /** Flushes the GTT write domain for the object if it's dirty. */ 3378 static void 3379 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj) 3380 { 3381 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 3382 3383 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT) 3384 return; 3385 3386 /* No actual flushing is required for the GTT write domain. Writes 3387 * to it "immediately" go to main memory as far as we know, so there's 3388 * no chipset flush. It also doesn't land in render cache. 3389 * 3390 * However, we do have to enforce the order so that all writes through 3391 * the GTT land before any writes to the device, such as updates to 3392 * the GATT itself. 3393 * 3394 * We also have to wait a bit for the writes to land from the GTT. 3395 * An uncached read (i.e. mmio) seems to be ideal for the round-trip 3396 * timing. This issue has only been observed when switching quickly 3397 * between GTT writes and CPU reads from inside the kernel on recent hw, 3398 * and it appears to only affect discrete GTT blocks (i.e. on LLC 3399 * system agents we cannot reproduce this behaviour). 3400 */ 3401 wmb(); 3402 if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv)) 3403 POSTING_READ(RING_ACTHD(dev_priv->engine[RCS]->mmio_base)); 3404 3405 intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT)); 3406 3407 obj->base.write_domain = 0; 3408 trace_i915_gem_object_change_domain(obj, 3409 obj->base.read_domains, 3410 I915_GEM_DOMAIN_GTT); 3411 } 3412 3413 /** Flushes the CPU write domain for the object if it's dirty. */ 3414 static void 3415 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj) 3416 { 3417 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) 3418 return; 3419 3420 i915_gem_clflush_object(obj, obj->pin_display); 3421 intel_fb_obj_flush(obj, false, ORIGIN_CPU); 3422 3423 obj->base.write_domain = 0; 3424 trace_i915_gem_object_change_domain(obj, 3425 obj->base.read_domains, 3426 I915_GEM_DOMAIN_CPU); 3427 } 3428 3429 /** 3430 * Moves a single object to the GTT read, and possibly write domain. 3431 * @obj: object to act on 3432 * @write: ask for write access or read only 3433 * 3434 * This function returns when the move is complete, including waiting on 3435 * flushes to occur. 3436 */ 3437 int 3438 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) 3439 { 3440 uint32_t old_write_domain, old_read_domains; 3441 int ret; 3442 3443 lockdep_assert_held(&obj->base.dev->struct_mutex); 3444 3445 ret = i915_gem_object_wait(obj, 3446 I915_WAIT_INTERRUPTIBLE | 3447 I915_WAIT_LOCKED | 3448 (write ? I915_WAIT_ALL : 0), 3449 MAX_SCHEDULE_TIMEOUT, 3450 NULL); 3451 if (ret) 3452 return ret; 3453 3454 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) 3455 return 0; 3456 3457 /* Flush and acquire obj->pages so that we are coherent through 3458 * direct access in memory with previous cached writes through 3459 * shmemfs and that our cache domain tracking remains valid. 3460 * For example, if the obj->filp was moved to swap without us 3461 * being notified and releasing the pages, we would mistakenly 3462 * continue to assume that the obj remained out of the CPU cached 3463 * domain. 3464 */ 3465 ret = i915_gem_object_pin_pages(obj); 3466 if (ret) 3467 return ret; 3468 3469 i915_gem_object_flush_cpu_write_domain(obj); 3470 3471 /* Serialise direct access to this object with the barriers for 3472 * coherent writes from the GPU, by effectively invalidating the 3473 * GTT domain upon first access. 3474 */ 3475 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) 3476 mb(); 3477 3478 old_write_domain = obj->base.write_domain; 3479 old_read_domains = obj->base.read_domains; 3480 3481 /* It should now be out of any other write domains, and we can update 3482 * the domain values for our changes. 3483 */ 3484 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); 3485 obj->base.read_domains |= I915_GEM_DOMAIN_GTT; 3486 if (write) { 3487 obj->base.read_domains = I915_GEM_DOMAIN_GTT; 3488 obj->base.write_domain = I915_GEM_DOMAIN_GTT; 3489 obj->mm.dirty = true; 3490 } 3491 3492 trace_i915_gem_object_change_domain(obj, 3493 old_read_domains, 3494 old_write_domain); 3495 3496 i915_gem_object_unpin_pages(obj); 3497 return 0; 3498 } 3499 3500 /** 3501 * Changes the cache-level of an object across all VMA. 3502 * @obj: object to act on 3503 * @cache_level: new cache level to set for the object 3504 * 3505 * After this function returns, the object will be in the new cache-level 3506 * across all GTT and the contents of the backing storage will be coherent, 3507 * with respect to the new cache-level. In order to keep the backing storage 3508 * coherent for all users, we only allow a single cache level to be set 3509 * globally on the object and prevent it from being changed whilst the 3510 * hardware is reading from the object. That is if the object is currently 3511 * on the scanout it will be set to uncached (or equivalent display 3512 * cache coherency) and all non-MOCS GPU access will also be uncached so 3513 * that all direct access to the scanout remains coherent. 3514 */ 3515 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, 3516 enum i915_cache_level cache_level) 3517 { 3518 struct i915_vma *vma; 3519 int ret; 3520 3521 lockdep_assert_held(&obj->base.dev->struct_mutex); 3522 3523 if (obj->cache_level == cache_level) 3524 return 0; 3525 3526 /* Inspect the list of currently bound VMA and unbind any that would 3527 * be invalid given the new cache-level. This is principally to 3528 * catch the issue of the CS prefetch crossing page boundaries and 3529 * reading an invalid PTE on older architectures. 3530 */ 3531 restart: 3532 list_for_each_entry(vma, &obj->vma_list, obj_link) { 3533 if (!drm_mm_node_allocated(&vma->node)) 3534 continue; 3535 3536 if (i915_vma_is_pinned(vma)) { 3537 DRM_DEBUG("can not change the cache level of pinned objects\n"); 3538 return -EBUSY; 3539 } 3540 3541 if (i915_gem_valid_gtt_space(vma, cache_level)) 3542 continue; 3543 3544 ret = i915_vma_unbind(vma); 3545 if (ret) 3546 return ret; 3547 3548 /* As unbinding may affect other elements in the 3549 * obj->vma_list (due to side-effects from retiring 3550 * an active vma), play safe and restart the iterator. 3551 */ 3552 goto restart; 3553 } 3554 3555 /* We can reuse the existing drm_mm nodes but need to change the 3556 * cache-level on the PTE. We could simply unbind them all and 3557 * rebind with the correct cache-level on next use. However since 3558 * we already have a valid slot, dma mapping, pages etc, we may as 3559 * rewrite the PTE in the belief that doing so tramples upon less 3560 * state and so involves less work. 3561 */ 3562 if (obj->bind_count) { 3563 /* Before we change the PTE, the GPU must not be accessing it. 3564 * If we wait upon the object, we know that all the bound 3565 * VMA are no longer active. 3566 */ 3567 ret = i915_gem_object_wait(obj, 3568 I915_WAIT_INTERRUPTIBLE | 3569 I915_WAIT_LOCKED | 3570 I915_WAIT_ALL, 3571 MAX_SCHEDULE_TIMEOUT, 3572 NULL); 3573 if (ret) 3574 return ret; 3575 3576 if (!HAS_LLC(to_i915(obj->base.dev)) && 3577 cache_level != I915_CACHE_NONE) { 3578 /* Access to snoopable pages through the GTT is 3579 * incoherent and on some machines causes a hard 3580 * lockup. Relinquish the CPU mmaping to force 3581 * userspace to refault in the pages and we can 3582 * then double check if the GTT mapping is still 3583 * valid for that pointer access. 3584 */ 3585 i915_gem_release_mmap(obj); 3586 3587 /* As we no longer need a fence for GTT access, 3588 * we can relinquish it now (and so prevent having 3589 * to steal a fence from someone else on the next 3590 * fence request). Note GPU activity would have 3591 * dropped the fence as all snoopable access is 3592 * supposed to be linear. 3593 */ 3594 list_for_each_entry(vma, &obj->vma_list, obj_link) { 3595 ret = i915_vma_put_fence(vma); 3596 if (ret) 3597 return ret; 3598 } 3599 } else { 3600 /* We either have incoherent backing store and 3601 * so no GTT access or the architecture is fully 3602 * coherent. In such cases, existing GTT mmaps 3603 * ignore the cache bit in the PTE and we can 3604 * rewrite it without confusing the GPU or having 3605 * to force userspace to fault back in its mmaps. 3606 */ 3607 } 3608 3609 list_for_each_entry(vma, &obj->vma_list, obj_link) { 3610 if (!drm_mm_node_allocated(&vma->node)) 3611 continue; 3612 3613 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); 3614 if (ret) 3615 return ret; 3616 } 3617 } 3618 3619 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU && 3620 cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) 3621 obj->cache_dirty = true; 3622 3623 list_for_each_entry(vma, &obj->vma_list, obj_link) 3624 vma->node.color = cache_level; 3625 obj->cache_level = cache_level; 3626 3627 return 0; 3628 } 3629 3630 int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, 3631 struct drm_file *file) 3632 { 3633 struct drm_i915_gem_caching *args = data; 3634 struct drm_i915_gem_object *obj; 3635 int err = 0; 3636 3637 rcu_read_lock(); 3638 obj = i915_gem_object_lookup_rcu(file, args->handle); 3639 if (!obj) { 3640 err = -ENOENT; 3641 goto out; 3642 } 3643 3644 switch (obj->cache_level) { 3645 case I915_CACHE_LLC: 3646 case I915_CACHE_L3_LLC: 3647 args->caching = I915_CACHING_CACHED; 3648 break; 3649 3650 case I915_CACHE_WT: 3651 args->caching = I915_CACHING_DISPLAY; 3652 break; 3653 3654 default: 3655 args->caching = I915_CACHING_NONE; 3656 break; 3657 } 3658 out: 3659 rcu_read_unlock(); 3660 return err; 3661 } 3662 3663 int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, 3664 struct drm_file *file) 3665 { 3666 struct drm_i915_private *i915 = to_i915(dev); 3667 struct drm_i915_gem_caching *args = data; 3668 struct drm_i915_gem_object *obj; 3669 enum i915_cache_level level; 3670 int ret; 3671 3672 switch (args->caching) { 3673 case I915_CACHING_NONE: 3674 level = I915_CACHE_NONE; 3675 break; 3676 case I915_CACHING_CACHED: 3677 /* 3678 * Due to a HW issue on BXT A stepping, GPU stores via a 3679 * snooped mapping may leave stale data in a corresponding CPU 3680 * cacheline, whereas normally such cachelines would get 3681 * invalidated. 3682 */ 3683 if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) 3684 return -ENODEV; 3685 3686 level = I915_CACHE_LLC; 3687 break; 3688 case I915_CACHING_DISPLAY: 3689 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; 3690 break; 3691 default: 3692 return -EINVAL; 3693 } 3694 3695 ret = i915_mutex_lock_interruptible(dev); 3696 if (ret) 3697 return ret; 3698 3699 obj = i915_gem_object_lookup(file, args->handle); 3700 if (!obj) { 3701 ret = -ENOENT; 3702 goto unlock; 3703 } 3704 3705 ret = i915_gem_object_set_cache_level(obj, level); 3706 i915_gem_object_put(obj); 3707 unlock: 3708 mutex_unlock(&dev->struct_mutex); 3709 return ret; 3710 } 3711 3712 /* 3713 * Prepare buffer for display plane (scanout, cursors, etc). 3714 * Can be called from an uninterruptible phase (modesetting) and allows 3715 * any flushes to be pipelined (for pageflips). 3716 */ 3717 struct i915_vma * 3718 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, 3719 u32 alignment, 3720 const struct i915_ggtt_view *view) 3721 { 3722 struct i915_vma *vma; 3723 u32 old_read_domains, old_write_domain; 3724 int ret; 3725 3726 lockdep_assert_held(&obj->base.dev->struct_mutex); 3727 3728 /* Mark the pin_display early so that we account for the 3729 * display coherency whilst setting up the cache domains. 3730 */ 3731 obj->pin_display++; 3732 3733 /* The display engine is not coherent with the LLC cache on gen6. As 3734 * a result, we make sure that the pinning that is about to occur is 3735 * done with uncached PTEs. This is lowest common denominator for all 3736 * chipsets. 3737 * 3738 * However for gen6+, we could do better by using the GFDT bit instead 3739 * of uncaching, which would allow us to flush all the LLC-cached data 3740 * with that bit in the PTE to main memory with just one PIPE_CONTROL. 3741 */ 3742 ret = i915_gem_object_set_cache_level(obj, 3743 HAS_WT(to_i915(obj->base.dev)) ? 3744 I915_CACHE_WT : I915_CACHE_NONE); 3745 if (ret) { 3746 vma = ERR_PTR(ret); 3747 goto err_unpin_display; 3748 } 3749 3750 /* As the user may map the buffer once pinned in the display plane 3751 * (e.g. libkms for the bootup splash), we have to ensure that we 3752 * always use map_and_fenceable for all scanout buffers. However, 3753 * it may simply be too big to fit into mappable, in which case 3754 * put it anyway and hope that userspace can cope (but always first 3755 * try to preserve the existing ABI). 3756 */ 3757 vma = ERR_PTR(-ENOSPC); 3758 if (view->type == I915_GGTT_VIEW_NORMAL) 3759 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, 3760 PIN_MAPPABLE | PIN_NONBLOCK); 3761 if (IS_ERR(vma)) { 3762 struct drm_i915_private *i915 = to_i915(obj->base.dev); 3763 unsigned int flags; 3764 3765 /* Valleyview is definitely limited to scanning out the first 3766 * 512MiB. Lets presume this behaviour was inherited from the 3767 * g4x display engine and that all earlier gen are similarly 3768 * limited. Testing suggests that it is a little more 3769 * complicated than this. For example, Cherryview appears quite 3770 * happy to scanout from anywhere within its global aperture. 3771 */ 3772 flags = 0; 3773 if (HAS_GMCH_DISPLAY(i915)) 3774 flags = PIN_MAPPABLE; 3775 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); 3776 } 3777 if (IS_ERR(vma)) 3778 goto err_unpin_display; 3779 3780 vma->display_alignment = max_t(u64, vma->display_alignment, alignment); 3781 3782 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */ 3783 if (obj->cache_dirty || obj->base.write_domain == I915_GEM_DOMAIN_CPU) { 3784 i915_gem_clflush_object(obj, true); 3785 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB); 3786 } 3787 3788 old_write_domain = obj->base.write_domain; 3789 old_read_domains = obj->base.read_domains; 3790 3791 /* It should now be out of any other write domains, and we can update 3792 * the domain values for our changes. 3793 */ 3794 obj->base.write_domain = 0; 3795 obj->base.read_domains |= I915_GEM_DOMAIN_GTT; 3796 3797 trace_i915_gem_object_change_domain(obj, 3798 old_read_domains, 3799 old_write_domain); 3800 3801 return vma; 3802 3803 err_unpin_display: 3804 obj->pin_display--; 3805 return vma; 3806 } 3807 3808 void 3809 i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) 3810 { 3811 lockdep_assert_held(&vma->vm->dev->struct_mutex); 3812 3813 if (WARN_ON(vma->obj->pin_display == 0)) 3814 return; 3815 3816 if (--vma->obj->pin_display == 0) 3817 vma->display_alignment = 0; 3818 3819 /* Bump the LRU to try and avoid premature eviction whilst flipping */ 3820 if (!i915_vma_is_active(vma)) 3821 list_move_tail(&vma->vm_link, &vma->vm->inactive_list); 3822 3823 i915_vma_unpin(vma); 3824 } 3825 3826 /** 3827 * Moves a single object to the CPU read, and possibly write domain. 3828 * @obj: object to act on 3829 * @write: requesting write or read-only access 3830 * 3831 * This function returns when the move is complete, including waiting on 3832 * flushes to occur. 3833 */ 3834 int 3835 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) 3836 { 3837 uint32_t old_write_domain, old_read_domains; 3838 int ret; 3839 3840 lockdep_assert_held(&obj->base.dev->struct_mutex); 3841 3842 ret = i915_gem_object_wait(obj, 3843 I915_WAIT_INTERRUPTIBLE | 3844 I915_WAIT_LOCKED | 3845 (write ? I915_WAIT_ALL : 0), 3846 MAX_SCHEDULE_TIMEOUT, 3847 NULL); 3848 if (ret) 3849 return ret; 3850 3851 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) 3852 return 0; 3853 3854 i915_gem_object_flush_gtt_write_domain(obj); 3855 3856 old_write_domain = obj->base.write_domain; 3857 old_read_domains = obj->base.read_domains; 3858 3859 /* Flush the CPU cache if it's still invalid. */ 3860 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { 3861 i915_gem_clflush_object(obj, false); 3862 3863 obj->base.read_domains |= I915_GEM_DOMAIN_CPU; 3864 } 3865 3866 /* It should now be out of any other write domains, and we can update 3867 * the domain values for our changes. 3868 */ 3869 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); 3870 3871 /* If we're writing through the CPU, then the GPU read domains will 3872 * need to be invalidated at next use. 3873 */ 3874 if (write) { 3875 obj->base.read_domains = I915_GEM_DOMAIN_CPU; 3876 obj->base.write_domain = I915_GEM_DOMAIN_CPU; 3877 } 3878 3879 trace_i915_gem_object_change_domain(obj, 3880 old_read_domains, 3881 old_write_domain); 3882 3883 return 0; 3884 } 3885 3886 /* Throttle our rendering by waiting until the ring has completed our requests 3887 * emitted over 20 msec ago. 3888 * 3889 * Note that if we were to use the current jiffies each time around the loop, 3890 * we wouldn't escape the function with any frames outstanding if the time to 3891 * render a frame was over 20ms. 3892 * 3893 * This should get us reasonable parallelism between CPU and GPU but also 3894 * relatively low latency when blocking on a particular request to finish. 3895 */ 3896 static int 3897 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) 3898 { 3899 struct drm_i915_private *dev_priv = to_i915(dev); 3900 struct drm_i915_file_private *file_priv = file->driver_priv; 3901 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; 3902 struct drm_i915_gem_request *request, *target = NULL; 3903 long ret; 3904 3905 /* ABI: return -EIO if already wedged */ 3906 if (i915_terminally_wedged(&dev_priv->gpu_error)) 3907 return -EIO; 3908 3909 lockmgr(&file_priv->mm.lock, LK_EXCLUSIVE); 3910 list_for_each_entry(request, &file_priv->mm.request_list, client_list) { 3911 if (time_after_eq(request->emitted_jiffies, recent_enough)) 3912 break; 3913 3914 /* 3915 * Note that the request might not have been submitted yet. 3916 * In which case emitted_jiffies will be zero. 3917 */ 3918 if (!request->emitted_jiffies) 3919 continue; 3920 3921 target = request; 3922 } 3923 if (target) 3924 i915_gem_request_get(target); 3925 lockmgr(&file_priv->mm.lock, LK_RELEASE); 3926 3927 if (target == NULL) 3928 return 0; 3929 3930 ret = i915_wait_request(target, 3931 I915_WAIT_INTERRUPTIBLE, 3932 MAX_SCHEDULE_TIMEOUT); 3933 i915_gem_request_put(target); 3934 3935 return ret < 0 ? ret : 0; 3936 } 3937 3938 struct i915_vma * 3939 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, 3940 const struct i915_ggtt_view *view, 3941 u64 size, 3942 u64 alignment, 3943 u64 flags) 3944 { 3945 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 3946 struct i915_address_space *vm = &dev_priv->ggtt.base; 3947 struct i915_vma *vma; 3948 int ret; 3949 3950 lockdep_assert_held(&obj->base.dev->struct_mutex); 3951 3952 vma = i915_gem_obj_lookup_or_create_vma(obj, vm, view); 3953 if (IS_ERR(vma)) 3954 return vma; 3955 3956 if (i915_vma_misplaced(vma, size, alignment, flags)) { 3957 if (flags & PIN_NONBLOCK && 3958 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))) 3959 return ERR_PTR(-ENOSPC); 3960 3961 if (flags & PIN_MAPPABLE) { 3962 u32 fence_size; 3963 3964 fence_size = i915_gem_get_ggtt_size(dev_priv, vma->size, 3965 i915_gem_object_get_tiling(obj)); 3966 /* If the required space is larger than the available 3967 * aperture, we will not able to find a slot for the 3968 * object and unbinding the object now will be in 3969 * vain. Worse, doing so may cause us to ping-pong 3970 * the object in and out of the Global GTT and 3971 * waste a lot of cycles under the mutex. 3972 */ 3973 if (fence_size > dev_priv->ggtt.mappable_end) 3974 return ERR_PTR(-E2BIG); 3975 3976 /* If NONBLOCK is set the caller is optimistically 3977 * trying to cache the full object within the mappable 3978 * aperture, and *must* have a fallback in place for 3979 * situations where we cannot bind the object. We 3980 * can be a little more lax here and use the fallback 3981 * more often to avoid costly migrations of ourselves 3982 * and other objects within the aperture. 3983 * 3984 * Half-the-aperture is used as a simple heuristic. 3985 * More interesting would to do search for a free 3986 * block prior to making the commitment to unbind. 3987 * That caters for the self-harm case, and with a 3988 * little more heuristics (e.g. NOFAULT, NOEVICT) 3989 * we could try to minimise harm to others. 3990 */ 3991 if (flags & PIN_NONBLOCK && 3992 fence_size > dev_priv->ggtt.mappable_end / 2) 3993 return ERR_PTR(-ENOSPC); 3994 } 3995 3996 WARN(i915_vma_is_pinned(vma), 3997 "bo is already pinned in ggtt with incorrect alignment:" 3998 " offset=%08x, req.alignment=%llx," 3999 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", 4000 i915_ggtt_offset(vma), alignment, 4001 !!(flags & PIN_MAPPABLE), 4002 i915_vma_is_map_and_fenceable(vma)); 4003 ret = i915_vma_unbind(vma); 4004 if (ret) 4005 return ERR_PTR(ret); 4006 } 4007 4008 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); 4009 if (ret) 4010 return ERR_PTR(ret); 4011 4012 return vma; 4013 } 4014 4015 static __always_inline unsigned int __busy_read_flag(unsigned int id) 4016 { 4017 /* Note that we could alias engines in the execbuf API, but 4018 * that would be very unwise as it prevents userspace from 4019 * fine control over engine selection. Ahem. 4020 * 4021 * This should be something like EXEC_MAX_ENGINE instead of 4022 * I915_NUM_ENGINES. 4023 */ 4024 BUILD_BUG_ON(I915_NUM_ENGINES > 16); 4025 return 0x10000 << id; 4026 } 4027 4028 static __always_inline unsigned int __busy_write_id(unsigned int id) 4029 { 4030 /* The uABI guarantees an active writer is also amongst the read 4031 * engines. This would be true if we accessed the activity tracking 4032 * under the lock, but as we perform the lookup of the object and 4033 * its activity locklessly we can not guarantee that the last_write 4034 * being active implies that we have set the same engine flag from 4035 * last_read - hence we always set both read and write busy for 4036 * last_write. 4037 */ 4038 return id | __busy_read_flag(id); 4039 } 4040 4041 static __always_inline unsigned int 4042 __busy_set_if_active(const struct dma_fence *fence, 4043 unsigned int (*flag)(unsigned int id)) 4044 { 4045 struct drm_i915_gem_request *rq; 4046 4047 /* We have to check the current hw status of the fence as the uABI 4048 * guarantees forward progress. We could rely on the idle worker 4049 * to eventually flush us, but to minimise latency just ask the 4050 * hardware. 4051 * 4052 * Note we only report on the status of native fences. 4053 */ 4054 if (!dma_fence_is_i915(fence)) 4055 return 0; 4056 4057 /* opencode to_request() in order to avoid const warnings */ 4058 rq = container_of(fence, struct drm_i915_gem_request, fence); 4059 if (i915_gem_request_completed(rq)) 4060 return 0; 4061 4062 return flag(rq->engine->exec_id); 4063 } 4064 4065 static __always_inline unsigned int 4066 busy_check_reader(const struct dma_fence *fence) 4067 { 4068 return __busy_set_if_active(fence, __busy_read_flag); 4069 } 4070 4071 static __always_inline unsigned int 4072 busy_check_writer(const struct dma_fence *fence) 4073 { 4074 if (!fence) 4075 return 0; 4076 4077 return __busy_set_if_active(fence, __busy_write_id); 4078 } 4079 4080 int 4081 i915_gem_busy_ioctl(struct drm_device *dev, void *data, 4082 struct drm_file *file) 4083 { 4084 struct drm_i915_gem_busy *args = data; 4085 struct drm_i915_gem_object *obj; 4086 struct reservation_object_list *list; 4087 unsigned int seq; 4088 int err; 4089 4090 err = -ENOENT; 4091 rcu_read_lock(); 4092 obj = i915_gem_object_lookup_rcu(file, args->handle); 4093 if (!obj) 4094 goto out; 4095 4096 /* A discrepancy here is that we do not report the status of 4097 * non-i915 fences, i.e. even though we may report the object as idle, 4098 * a call to set-domain may still stall waiting for foreign rendering. 4099 * This also means that wait-ioctl may report an object as busy, 4100 * where busy-ioctl considers it idle. 4101 * 4102 * We trade the ability to warn of foreign fences to report on which 4103 * i915 engines are active for the object. 4104 * 4105 * Alternatively, we can trade that extra information on read/write 4106 * activity with 4107 * args->busy = 4108 * !reservation_object_test_signaled_rcu(obj->resv, true); 4109 * to report the overall busyness. This is what the wait-ioctl does. 4110 * 4111 */ 4112 retry: 4113 seq = raw_read_seqcount(&obj->resv->seq); 4114 4115 /* Translate the exclusive fence to the READ *and* WRITE engine */ 4116 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); 4117 4118 /* Translate shared fences to READ set of engines */ 4119 list = rcu_dereference(obj->resv->fence); 4120 if (list) { 4121 unsigned int shared_count = list->shared_count, i; 4122 4123 for (i = 0; i < shared_count; ++i) { 4124 struct dma_fence *fence = 4125 rcu_dereference(list->shared[i]); 4126 4127 args->busy |= busy_check_reader(fence); 4128 } 4129 } 4130 4131 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) 4132 goto retry; 4133 4134 err = 0; 4135 out: 4136 rcu_read_unlock(); 4137 return err; 4138 } 4139 4140 int 4141 i915_gem_throttle_ioctl(struct drm_device *dev, void *data, 4142 struct drm_file *file_priv) 4143 { 4144 return i915_gem_ring_throttle(dev, file_priv); 4145 } 4146 4147 int 4148 i915_gem_madvise_ioctl(struct drm_device *dev, void *data, 4149 struct drm_file *file_priv) 4150 { 4151 struct drm_i915_private *dev_priv = to_i915(dev); 4152 struct drm_i915_gem_madvise *args = data; 4153 struct drm_i915_gem_object *obj; 4154 int err; 4155 4156 switch (args->madv) { 4157 case I915_MADV_DONTNEED: 4158 case I915_MADV_WILLNEED: 4159 break; 4160 default: 4161 return -EINVAL; 4162 } 4163 4164 obj = i915_gem_object_lookup(file_priv, args->handle); 4165 if (!obj) 4166 return -ENOENT; 4167 4168 err = mutex_lock_interruptible(&obj->mm.lock); 4169 if (err) 4170 goto out; 4171 4172 if (obj->mm.pages && 4173 i915_gem_object_is_tiled(obj) && 4174 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { 4175 if (obj->mm.madv == I915_MADV_WILLNEED) { 4176 GEM_BUG_ON(!obj->mm.quirked); 4177 __i915_gem_object_unpin_pages(obj); 4178 obj->mm.quirked = false; 4179 } 4180 if (args->madv == I915_MADV_WILLNEED) { 4181 GEM_BUG_ON(obj->mm.quirked); 4182 __i915_gem_object_pin_pages(obj); 4183 obj->mm.quirked = true; 4184 } 4185 } 4186 4187 if (obj->mm.madv != __I915_MADV_PURGED) 4188 obj->mm.madv = args->madv; 4189 4190 /* if the object is no longer attached, discard its backing storage */ 4191 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages) 4192 i915_gem_object_truncate(obj); 4193 4194 args->retained = obj->mm.madv != __I915_MADV_PURGED; 4195 mutex_unlock(&obj->mm.lock); 4196 4197 out: 4198 i915_gem_object_put(obj); 4199 return err; 4200 } 4201 4202 static void 4203 frontbuffer_retire(struct i915_gem_active *active, 4204 struct drm_i915_gem_request *request) 4205 { 4206 struct drm_i915_gem_object *obj = 4207 container_of(active, typeof(*obj), frontbuffer_write); 4208 4209 intel_fb_obj_flush(obj, true, ORIGIN_CS); 4210 } 4211 4212 void i915_gem_object_init(struct drm_i915_gem_object *obj, 4213 const struct drm_i915_gem_object_ops *ops) 4214 { 4215 lockinit(&obj->mm.lock, "i9goml", 0, LK_CANRECURSE); 4216 4217 INIT_LIST_HEAD(&obj->global_link); 4218 INIT_LIST_HEAD(&obj->userfault_link); 4219 INIT_LIST_HEAD(&obj->obj_exec_link); 4220 INIT_LIST_HEAD(&obj->vma_list); 4221 INIT_LIST_HEAD(&obj->batch_pool_link); 4222 4223 obj->ops = ops; 4224 4225 reservation_object_init(&obj->__builtin_resv); 4226 obj->resv = &obj->__builtin_resv; 4227 4228 obj->frontbuffer_ggtt_origin = ORIGIN_GTT; 4229 init_request_active(&obj->frontbuffer_write, frontbuffer_retire); 4230 4231 obj->mm.madv = I915_MADV_WILLNEED; 4232 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); 4233 lockinit(&obj->mm.get_page.lock, "i915ogpl", 0, LK_CANRECURSE); 4234 4235 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size); 4236 } 4237 4238 static const struct drm_i915_gem_object_ops i915_gem_object_ops = { 4239 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | 4240 I915_GEM_OBJECT_IS_SHRINKABLE, 4241 .get_pages = i915_gem_object_get_pages_gtt, 4242 .put_pages = i915_gem_object_put_pages_gtt, 4243 }; 4244 4245 /* Note we don't consider signbits :| */ 4246 #define overflows_type(x, T) \ 4247 (sizeof(x) > sizeof(T) && (x) >> (sizeof(T) * BITS_PER_BYTE)) 4248 4249 struct drm_i915_gem_object * 4250 i915_gem_object_create(struct drm_device *dev, u64 size) 4251 { 4252 struct drm_i915_private *dev_priv = to_i915(dev); 4253 struct drm_i915_gem_object *obj; 4254 #if 0 4255 struct address_space *mapping; 4256 #endif 4257 gfp_t mask; 4258 int ret; 4259 4260 /* There is a prevalence of the assumption that we fit the object's 4261 * page count inside a 32bit _signed_ variable. Let's document this and 4262 * catch if we ever need to fix it. In the meantime, if you do spot 4263 * such a local variable, please consider fixing! 4264 */ 4265 if (WARN_ON(size >> PAGE_SHIFT > INT_MAX)) 4266 return ERR_PTR(-E2BIG); 4267 4268 if (overflows_type(size, obj->base.size)) 4269 return ERR_PTR(-E2BIG); 4270 4271 obj = i915_gem_object_alloc(dev); 4272 if (obj == NULL) 4273 return ERR_PTR(-ENOMEM); 4274 4275 ret = drm_gem_object_init(dev, &obj->base, size); 4276 if (ret) 4277 goto fail; 4278 4279 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; 4280 if (IS_CRESTLINE(dev_priv) || IS_BROADWATER(dev_priv)) { 4281 /* 965gm cannot relocate objects above 4GiB. */ 4282 mask &= ~__GFP_HIGHMEM; 4283 mask |= __GFP_DMA32; 4284 } 4285 4286 #if 0 4287 mapping = obj->base.filp->f_mapping; 4288 mapping_set_gfp_mask(mapping, mask); 4289 #endif 4290 4291 i915_gem_object_init(obj, &i915_gem_object_ops); 4292 4293 obj->base.write_domain = I915_GEM_DOMAIN_CPU; 4294 obj->base.read_domains = I915_GEM_DOMAIN_CPU; 4295 4296 if (HAS_LLC(dev_priv)) { 4297 /* On some devices, we can have the GPU use the LLC (the CPU 4298 * cache) for about a 10% performance improvement 4299 * compared to uncached. Graphics requests other than 4300 * display scanout are coherent with the CPU in 4301 * accessing this cache. This means in this mode we 4302 * don't need to clflush on the CPU side, and on the 4303 * GPU side we only need to flush internal caches to 4304 * get data visible to the CPU. 4305 * 4306 * However, we maintain the display planes as UC, and so 4307 * need to rebind when first used as such. 4308 */ 4309 obj->cache_level = I915_CACHE_LLC; 4310 } else 4311 obj->cache_level = I915_CACHE_NONE; 4312 4313 trace_i915_gem_object_create(obj); 4314 4315 return obj; 4316 4317 fail: 4318 i915_gem_object_free(obj); 4319 return ERR_PTR(ret); 4320 } 4321 4322 static bool discard_backing_storage(struct drm_i915_gem_object *obj) 4323 { 4324 /* If we are the last user of the backing storage (be it shmemfs 4325 * pages or stolen etc), we know that the pages are going to be 4326 * immediately released. In this case, we can then skip copying 4327 * back the contents from the GPU. 4328 */ 4329 4330 if (obj->mm.madv != I915_MADV_WILLNEED) 4331 return false; 4332 4333 if (obj->base.filp == NULL) 4334 return true; 4335 4336 /* At first glance, this looks racy, but then again so would be 4337 * userspace racing mmap against close. However, the first external 4338 * reference to the filp can only be obtained through the 4339 * i915_gem_mmap_ioctl() which safeguards us against the user 4340 * acquiring such a reference whilst we are in the middle of 4341 * freeing the object. 4342 */ 4343 #if 0 4344 return atomic_long_read(&obj->base.filp->f_count) == 1; 4345 #else 4346 return false; 4347 #endif 4348 } 4349 4350 static void __i915_gem_free_objects(struct drm_i915_private *i915, 4351 struct llist_node *freed) 4352 { 4353 struct drm_i915_gem_object *obj, *on; 4354 4355 mutex_lock(&i915->drm.struct_mutex); 4356 intel_runtime_pm_get(i915); 4357 llist_for_each_entry_safe(obj, on, freed, freed) { 4358 struct i915_vma *vma, *vn; 4359 4360 trace_i915_gem_object_destroy(obj); 4361 4362 GEM_BUG_ON(i915_gem_object_is_active(obj)); 4363 list_for_each_entry_safe(vma, vn, 4364 &obj->vma_list, obj_link) { 4365 GEM_BUG_ON(!i915_vma_is_ggtt(vma)); 4366 GEM_BUG_ON(i915_vma_is_active(vma)); 4367 vma->flags &= ~I915_VMA_PIN_MASK; 4368 i915_vma_close(vma); 4369 } 4370 GEM_BUG_ON(!list_empty(&obj->vma_list)); 4371 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); 4372 4373 list_del(&obj->global_link); 4374 } 4375 intel_runtime_pm_put(i915); 4376 mutex_unlock(&i915->drm.struct_mutex); 4377 4378 llist_for_each_entry_safe(obj, on, freed, freed) { 4379 GEM_BUG_ON(obj->bind_count); 4380 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); 4381 4382 if (obj->ops->release) 4383 obj->ops->release(obj); 4384 4385 #if 0 4386 if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) 4387 #else 4388 if (i915_gem_object_has_pinned_pages(obj)) 4389 #endif 4390 atomic_set(&obj->mm.pages_pin_count, 0); 4391 __i915_gem_object_put_pages(obj, I915_MM_NORMAL); 4392 GEM_BUG_ON(obj->mm.pages); 4393 4394 #if 0 4395 if (obj->base.import_attach) 4396 drm_prime_gem_destroy(&obj->base, NULL); 4397 #endif 4398 4399 reservation_object_fini(&obj->__builtin_resv); 4400 drm_gem_object_release(&obj->base); 4401 i915_gem_info_remove_obj(i915, obj->base.size); 4402 4403 kfree(obj->bit_17); 4404 i915_gem_object_free(obj); 4405 } 4406 } 4407 4408 static void i915_gem_flush_free_objects(struct drm_i915_private *i915) 4409 { 4410 struct llist_node *freed; 4411 4412 freed = llist_del_all(&i915->mm.free_list); 4413 if (unlikely(freed)) 4414 __i915_gem_free_objects(i915, freed); 4415 } 4416 4417 static void __i915_gem_free_work(struct work_struct *work) 4418 { 4419 struct drm_i915_private *i915 = 4420 container_of(work, struct drm_i915_private, mm.free_work); 4421 struct llist_node *freed; 4422 4423 /* All file-owned VMA should have been released by this point through 4424 * i915_gem_close_object(), or earlier by i915_gem_context_close(). 4425 * However, the object may also be bound into the global GTT (e.g. 4426 * older GPUs without per-process support, or for direct access through 4427 * the GTT either for the user or for scanout). Those VMA still need to 4428 * unbound now. 4429 */ 4430 4431 while ((freed = llist_del_all(&i915->mm.free_list))) 4432 __i915_gem_free_objects(i915, freed); 4433 } 4434 4435 static void __i915_gem_free_object_rcu(struct rcu_head *head) 4436 { 4437 struct drm_i915_gem_object *obj = 4438 container_of(head, typeof(*obj), rcu); 4439 struct drm_i915_private *i915 = to_i915(obj->base.dev); 4440 4441 /* We can't simply use call_rcu() from i915_gem_free_object() 4442 * as we need to block whilst unbinding, and the call_rcu 4443 * task may be called from softirq context. So we take a 4444 * detour through a worker. 4445 */ 4446 if (llist_add(&obj->freed, &i915->mm.free_list)) 4447 schedule_work(&i915->mm.free_work); 4448 } 4449 4450 void i915_gem_free_object(struct drm_gem_object *gem_obj) 4451 { 4452 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); 4453 4454 if (obj->mm.quirked) 4455 __i915_gem_object_unpin_pages(obj); 4456 4457 if (discard_backing_storage(obj)) 4458 obj->mm.madv = I915_MADV_DONTNEED; 4459 4460 /* Before we free the object, make sure any pure RCU-only 4461 * read-side critical sections are complete, e.g. 4462 * i915_gem_busy_ioctl(). For the corresponding synchronized 4463 * lookup see i915_gem_object_lookup_rcu(). 4464 */ 4465 call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 4466 } 4467 4468 void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) 4469 { 4470 lockdep_assert_held(&obj->base.dev->struct_mutex); 4471 4472 GEM_BUG_ON(i915_gem_object_has_active_reference(obj)); 4473 if (i915_gem_object_is_active(obj)) 4474 i915_gem_object_set_active_reference(obj); 4475 else 4476 i915_gem_object_put(obj); 4477 } 4478 4479 static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv) 4480 { 4481 struct intel_engine_cs *engine; 4482 enum intel_engine_id id; 4483 4484 for_each_engine(engine, dev_priv, id) 4485 GEM_BUG_ON(engine->last_context != dev_priv->kernel_context); 4486 } 4487 4488 int i915_gem_suspend(struct drm_device *dev) 4489 { 4490 struct drm_i915_private *dev_priv = to_i915(dev); 4491 int ret; 4492 4493 intel_suspend_gt_powersave(dev_priv); 4494 4495 mutex_lock(&dev->struct_mutex); 4496 4497 /* We have to flush all the executing contexts to main memory so 4498 * that they can saved in the hibernation image. To ensure the last 4499 * context image is coherent, we have to switch away from it. That 4500 * leaves the dev_priv->kernel_context still active when 4501 * we actually suspend, and its image in memory may not match the GPU 4502 * state. Fortunately, the kernel_context is disposable and we do 4503 * not rely on its state. 4504 */ 4505 ret = i915_gem_switch_to_kernel_context(dev_priv); 4506 if (ret) 4507 goto err; 4508 4509 ret = i915_gem_wait_for_idle(dev_priv, 4510 I915_WAIT_INTERRUPTIBLE | 4511 I915_WAIT_LOCKED); 4512 if (ret) 4513 goto err; 4514 4515 i915_gem_retire_requests(dev_priv); 4516 GEM_BUG_ON(dev_priv->gt.active_requests); 4517 4518 assert_kernel_context_is_current(dev_priv); 4519 i915_gem_context_lost(dev_priv); 4520 mutex_unlock(&dev->struct_mutex); 4521 4522 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); 4523 cancel_delayed_work_sync(&dev_priv->gt.retire_work); 4524 flush_delayed_work(&dev_priv->gt.idle_work); 4525 flush_work(&dev_priv->mm.free_work); 4526 4527 /* Assert that we sucessfully flushed all the work and 4528 * reset the GPU back to its idle, low power state. 4529 */ 4530 WARN_ON(dev_priv->gt.awake); 4531 WARN_ON(!intel_execlists_idle(dev_priv)); 4532 4533 /* 4534 * Neither the BIOS, ourselves or any other kernel 4535 * expects the system to be in execlists mode on startup, 4536 * so we need to reset the GPU back to legacy mode. And the only 4537 * known way to disable logical contexts is through a GPU reset. 4538 * 4539 * So in order to leave the system in a known default configuration, 4540 * always reset the GPU upon unload and suspend. Afterwards we then 4541 * clean up the GEM state tracking, flushing off the requests and 4542 * leaving the system in a known idle state. 4543 * 4544 * Note that is of the upmost importance that the GPU is idle and 4545 * all stray writes are flushed *before* we dismantle the backing 4546 * storage for the pinned objects. 4547 * 4548 * However, since we are uncertain that resetting the GPU on older 4549 * machines is a good idea, we don't - just in case it leaves the 4550 * machine in an unusable condition. 4551 */ 4552 if (HAS_HW_CONTEXTS(dev_priv)) { 4553 int reset = intel_gpu_reset(dev_priv, ALL_ENGINES); 4554 WARN_ON(reset && reset != -ENODEV); 4555 } 4556 4557 return 0; 4558 4559 err: 4560 mutex_unlock(&dev->struct_mutex); 4561 return ret; 4562 } 4563 4564 void i915_gem_resume(struct drm_device *dev) 4565 { 4566 struct drm_i915_private *dev_priv = to_i915(dev); 4567 4568 WARN_ON(dev_priv->gt.awake); 4569 4570 mutex_lock(&dev->struct_mutex); 4571 i915_gem_restore_gtt_mappings(dev_priv); 4572 4573 /* As we didn't flush the kernel context before suspend, we cannot 4574 * guarantee that the context image is complete. So let's just reset 4575 * it and start again. 4576 */ 4577 dev_priv->gt.resume(dev_priv); 4578 4579 mutex_unlock(&dev->struct_mutex); 4580 } 4581 4582 void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) 4583 { 4584 if (INTEL_GEN(dev_priv) < 5 || 4585 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) 4586 return; 4587 4588 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | 4589 DISP_TILE_SURFACE_SWIZZLING); 4590 4591 if (IS_GEN5(dev_priv)) 4592 return; 4593 4594 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); 4595 if (IS_GEN6(dev_priv)) 4596 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); 4597 else if (IS_GEN7(dev_priv)) 4598 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); 4599 else if (IS_GEN8(dev_priv)) 4600 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); 4601 else 4602 BUG(); 4603 } 4604 4605 static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base) 4606 { 4607 I915_WRITE(RING_CTL(base), 0); 4608 I915_WRITE(RING_HEAD(base), 0); 4609 I915_WRITE(RING_TAIL(base), 0); 4610 I915_WRITE(RING_START(base), 0); 4611 } 4612 4613 static void init_unused_rings(struct drm_i915_private *dev_priv) 4614 { 4615 if (IS_I830(dev_priv)) { 4616 init_unused_ring(dev_priv, PRB1_BASE); 4617 init_unused_ring(dev_priv, SRB0_BASE); 4618 init_unused_ring(dev_priv, SRB1_BASE); 4619 init_unused_ring(dev_priv, SRB2_BASE); 4620 init_unused_ring(dev_priv, SRB3_BASE); 4621 } else if (IS_GEN2(dev_priv)) { 4622 init_unused_ring(dev_priv, SRB0_BASE); 4623 init_unused_ring(dev_priv, SRB1_BASE); 4624 } else if (IS_GEN3(dev_priv)) { 4625 init_unused_ring(dev_priv, PRB1_BASE); 4626 init_unused_ring(dev_priv, PRB2_BASE); 4627 } 4628 } 4629 4630 int 4631 i915_gem_init_hw(struct drm_device *dev) 4632 { 4633 struct drm_i915_private *dev_priv = to_i915(dev); 4634 struct intel_engine_cs *engine; 4635 enum intel_engine_id id; 4636 int ret; 4637 4638 dev_priv->gt.last_init_time = ktime_get(); 4639 4640 /* Double layer security blanket, see i915_gem_init() */ 4641 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); 4642 4643 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) 4644 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); 4645 4646 if (IS_HASWELL(dev_priv)) 4647 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? 4648 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); 4649 4650 if (HAS_PCH_NOP(dev_priv)) { 4651 if (IS_IVYBRIDGE(dev_priv)) { 4652 u32 temp = I915_READ(GEN7_MSG_CTL); 4653 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); 4654 I915_WRITE(GEN7_MSG_CTL, temp); 4655 } else if (INTEL_GEN(dev_priv) >= 7) { 4656 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); 4657 temp &= ~RESET_PCH_HANDSHAKE_ENABLE; 4658 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); 4659 } 4660 } 4661 4662 i915_gem_init_swizzling(dev_priv); 4663 4664 /* 4665 * At least 830 can leave some of the unused rings 4666 * "active" (ie. head != tail) after resume which 4667 * will prevent c3 entry. Makes sure all unused rings 4668 * are totally idle. 4669 */ 4670 init_unused_rings(dev_priv); 4671 4672 BUG_ON(!dev_priv->kernel_context); 4673 4674 ret = i915_ppgtt_init_hw(dev_priv); 4675 if (ret) { 4676 DRM_ERROR("PPGTT enable HW failed %d\n", ret); 4677 goto out; 4678 } 4679 4680 /* Need to do basic initialisation of all rings first: */ 4681 for_each_engine(engine, dev_priv, id) { 4682 ret = engine->init_hw(engine); 4683 if (ret) 4684 goto out; 4685 } 4686 4687 intel_mocs_init_l3cc_table(dev); 4688 4689 /* We can't enable contexts until all firmware is loaded */ 4690 ret = intel_guc_setup(dev); 4691 if (ret) 4692 goto out; 4693 4694 out: 4695 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); 4696 return ret; 4697 } 4698 4699 bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value) 4700 { 4701 if (INTEL_INFO(dev_priv)->gen < 6) 4702 return false; 4703 4704 /* TODO: make semaphores and Execlists play nicely together */ 4705 if (i915.enable_execlists) 4706 return false; 4707 4708 if (value >= 0) 4709 return value; 4710 4711 #ifdef CONFIG_INTEL_IOMMU 4712 /* Enable semaphores on SNB when IO remapping is off */ 4713 if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped) 4714 return false; 4715 #endif 4716 4717 return true; 4718 } 4719 4720 int i915_gem_init(struct drm_device *dev) 4721 { 4722 struct drm_i915_private *dev_priv = to_i915(dev); 4723 int ret; 4724 4725 mutex_lock(&dev->struct_mutex); 4726 4727 if (!i915.enable_execlists) { 4728 dev_priv->gt.resume = intel_legacy_submission_resume; 4729 dev_priv->gt.cleanup_engine = intel_engine_cleanup; 4730 } else { 4731 dev_priv->gt.resume = intel_lr_context_resume; 4732 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; 4733 } 4734 4735 /* This is just a security blanket to placate dragons. 4736 * On some systems, we very sporadically observe that the first TLBs 4737 * used by the CS may be stale, despite us poking the TLB reset. If 4738 * we hold the forcewake during initialisation these problems 4739 * just magically go away. 4740 */ 4741 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); 4742 4743 i915_gem_init_userptr(dev_priv); 4744 4745 ret = i915_gem_init_ggtt(dev_priv); 4746 if (ret) 4747 goto out_unlock; 4748 4749 ret = i915_gem_context_init(dev); 4750 if (ret) 4751 goto out_unlock; 4752 4753 ret = intel_engines_init(dev); 4754 if (ret) 4755 goto out_unlock; 4756 4757 ret = i915_gem_init_hw(dev); 4758 if (ret == -EIO) { 4759 /* Allow engine initialisation to fail by marking the GPU as 4760 * wedged. But we only want to do this where the GPU is angry, 4761 * for all other failure, such as an allocation failure, bail. 4762 */ 4763 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); 4764 i915_gem_set_wedged(dev_priv); 4765 ret = 0; 4766 } 4767 4768 out_unlock: 4769 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); 4770 mutex_unlock(&dev->struct_mutex); 4771 4772 return ret; 4773 } 4774 4775 void 4776 i915_gem_cleanup_engines(struct drm_device *dev) 4777 { 4778 struct drm_i915_private *dev_priv = to_i915(dev); 4779 struct intel_engine_cs *engine; 4780 enum intel_engine_id id; 4781 4782 for_each_engine(engine, dev_priv, id) 4783 dev_priv->gt.cleanup_engine(engine); 4784 } 4785 4786 void 4787 i915_gem_load_init_fences(struct drm_i915_private *dev_priv) 4788 { 4789 int i; 4790 4791 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) && 4792 !IS_CHERRYVIEW(dev_priv)) 4793 dev_priv->num_fence_regs = 32; 4794 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) || 4795 IS_I945GM(dev_priv) || IS_G33(dev_priv)) 4796 dev_priv->num_fence_regs = 16; 4797 else 4798 dev_priv->num_fence_regs = 8; 4799 4800 if (intel_vgpu_active(dev_priv)) 4801 dev_priv->num_fence_regs = 4802 I915_READ(vgtif_reg(avail_rs.fence_num)); 4803 4804 /* Initialize fence registers to zero */ 4805 for (i = 0; i < dev_priv->num_fence_regs; i++) { 4806 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; 4807 4808 fence->i915 = dev_priv; 4809 fence->id = i; 4810 list_add_tail(&fence->link, &dev_priv->mm.fence_list); 4811 } 4812 i915_gem_restore_fences(dev_priv); 4813 4814 i915_gem_detect_bit_6_swizzle(dev_priv); 4815 } 4816 4817 int 4818 i915_gem_load_init(struct drm_device *dev) 4819 { 4820 struct drm_i915_private *dev_priv = to_i915(dev); 4821 int err; 4822 4823 dev_priv->objects = 4824 kmem_cache_create("i915_gem_object", 4825 sizeof(struct drm_i915_gem_object), 0, 4826 SLAB_HWCACHE_ALIGN, 4827 NULL); 4828 if (!dev_priv->objects) { 4829 err = -ENOMEM; 4830 goto err_out; 4831 } 4832 4833 dev_priv->vmas = 4834 kmem_cache_create("i915_gem_vma", 4835 sizeof(struct i915_vma), 0, 4836 SLAB_HWCACHE_ALIGN, 4837 NULL); 4838 if (!dev_priv->vmas) { 4839 err = -ENOMEM; 4840 goto err_objects; 4841 } 4842 4843 dev_priv->requests = 4844 kmem_cache_create("i915_gem_request", 4845 sizeof(struct drm_i915_gem_request), 0, 4846 SLAB_HWCACHE_ALIGN | 4847 SLAB_RECLAIM_ACCOUNT | 4848 SLAB_DESTROY_BY_RCU, 4849 NULL); 4850 if (!dev_priv->requests) { 4851 err = -ENOMEM; 4852 goto err_vmas; 4853 } 4854 4855 dev_priv->dependencies = KMEM_CACHE(i915_dependency, 4856 SLAB_HWCACHE_ALIGN | 4857 SLAB_RECLAIM_ACCOUNT); 4858 if (!dev_priv->dependencies) 4859 goto err_requests; 4860 4861 mutex_lock(&dev_priv->drm.struct_mutex); 4862 INIT_LIST_HEAD(&dev_priv->gt.timelines); 4863 err = i915_gem_timeline_init__global(dev_priv); 4864 mutex_unlock(&dev_priv->drm.struct_mutex); 4865 if (err) 4866 goto err_dependencies; 4867 4868 INIT_LIST_HEAD(&dev_priv->context_list); 4869 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work); 4870 init_llist_head(&dev_priv->mm.free_list); 4871 INIT_LIST_HEAD(&dev_priv->mm.unbound_list); 4872 INIT_LIST_HEAD(&dev_priv->mm.bound_list); 4873 INIT_LIST_HEAD(&dev_priv->mm.fence_list); 4874 INIT_LIST_HEAD(&dev_priv->mm.userfault_list); 4875 INIT_DELAYED_WORK(&dev_priv->gt.retire_work, 4876 i915_gem_retire_work_handler); 4877 INIT_DELAYED_WORK(&dev_priv->gt.idle_work, 4878 i915_gem_idle_work_handler); 4879 init_waitqueue_head(&dev_priv->gpu_error.wait_queue); 4880 init_waitqueue_head(&dev_priv->gpu_error.reset_queue); 4881 4882 init_waitqueue_head(&dev_priv->pending_flip_queue); 4883 4884 dev_priv->mm.interruptible = true; 4885 4886 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); 4887 4888 lockinit(&dev_priv->fb_tracking.lock, "drmftl", 0, 0); 4889 4890 return 0; 4891 4892 err_dependencies: 4893 kmem_cache_destroy(dev_priv->dependencies); 4894 err_requests: 4895 kmem_cache_destroy(dev_priv->requests); 4896 err_vmas: 4897 kmem_cache_destroy(dev_priv->vmas); 4898 err_objects: 4899 kmem_cache_destroy(dev_priv->objects); 4900 err_out: 4901 return err; 4902 } 4903 4904 void i915_gem_load_cleanup(struct drm_device *dev) 4905 { 4906 struct drm_i915_private *dev_priv = to_i915(dev); 4907 4908 WARN_ON(!llist_empty(&dev_priv->mm.free_list)); 4909 4910 mutex_lock(&dev_priv->drm.struct_mutex); 4911 i915_gem_timeline_fini(&dev_priv->gt.global_timeline); 4912 WARN_ON(!list_empty(&dev_priv->gt.timelines)); 4913 mutex_unlock(&dev_priv->drm.struct_mutex); 4914 4915 kmem_cache_destroy(dev_priv->dependencies); 4916 kmem_cache_destroy(dev_priv->requests); 4917 kmem_cache_destroy(dev_priv->vmas); 4918 kmem_cache_destroy(dev_priv->objects); 4919 4920 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ 4921 rcu_barrier(); 4922 } 4923 4924 int i915_gem_freeze(struct drm_i915_private *dev_priv) 4925 { 4926 intel_runtime_pm_get(dev_priv); 4927 4928 mutex_lock(&dev_priv->drm.struct_mutex); 4929 i915_gem_shrink_all(dev_priv); 4930 mutex_unlock(&dev_priv->drm.struct_mutex); 4931 4932 intel_runtime_pm_put(dev_priv); 4933 4934 return 0; 4935 } 4936 4937 int i915_gem_freeze_late(struct drm_i915_private *dev_priv) 4938 { 4939 struct drm_i915_gem_object *obj; 4940 struct list_head *phases[] = { 4941 &dev_priv->mm.unbound_list, 4942 &dev_priv->mm.bound_list, 4943 NULL 4944 }, **p; 4945 4946 /* Called just before we write the hibernation image. 4947 * 4948 * We need to update the domain tracking to reflect that the CPU 4949 * will be accessing all the pages to create and restore from the 4950 * hibernation, and so upon restoration those pages will be in the 4951 * CPU domain. 4952 * 4953 * To make sure the hibernation image contains the latest state, 4954 * we update that state just before writing out the image. 4955 * 4956 * To try and reduce the hibernation image, we manually shrink 4957 * the objects as well. 4958 */ 4959 4960 mutex_lock(&dev_priv->drm.struct_mutex); 4961 i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND); 4962 4963 for (p = phases; *p; p++) { 4964 list_for_each_entry(obj, *p, global_link) { 4965 obj->base.read_domains = I915_GEM_DOMAIN_CPU; 4966 obj->base.write_domain = I915_GEM_DOMAIN_CPU; 4967 } 4968 } 4969 mutex_unlock(&dev_priv->drm.struct_mutex); 4970 4971 return 0; 4972 } 4973 4974 void i915_gem_release(struct drm_device *dev, struct drm_file *file) 4975 { 4976 struct drm_i915_file_private *file_priv = file->driver_priv; 4977 struct drm_i915_gem_request *request; 4978 4979 /* Clean up our request list when the client is going away, so that 4980 * later retire_requests won't dereference our soon-to-be-gone 4981 * file_priv. 4982 */ 4983 lockmgr(&file_priv->mm.lock, LK_EXCLUSIVE); 4984 list_for_each_entry(request, &file_priv->mm.request_list, client_list) 4985 request->file_priv = NULL; 4986 lockmgr(&file_priv->mm.lock, LK_RELEASE); 4987 4988 if (!list_empty(&file_priv->rps.link)) { 4989 lockmgr(&to_i915(dev)->rps.client_lock, LK_EXCLUSIVE); 4990 list_del(&file_priv->rps.link); 4991 lockmgr(&to_i915(dev)->rps.client_lock, LK_RELEASE); 4992 } 4993 } 4994 4995 int 4996 i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 4997 vm_ooffset_t foff, struct ucred *cred, u_short *color) 4998 { 4999 *color = 0; /* XXXKIB */ 5000 return (0); 5001 } 5002 5003 void 5004 i915_gem_pager_dtor(void *handle) 5005 { 5006 struct drm_gem_object *obj = handle; 5007 struct drm_device *dev = obj->dev; 5008 5009 mutex_lock(&dev->struct_mutex); 5010 drm_gem_free_mmap_offset(obj); 5011 i915_gem_release_mmap(to_intel_bo(obj)); 5012 drm_gem_object_unreference(obj); 5013 mutex_unlock(&dev->struct_mutex); 5014 } 5015 5016 int i915_gem_open(struct drm_device *dev, struct drm_file *file) 5017 { 5018 struct drm_i915_file_private *file_priv; 5019 int ret; 5020 5021 DRM_DEBUG("\n"); 5022 5023 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); 5024 if (!file_priv) 5025 return -ENOMEM; 5026 5027 file->driver_priv = file_priv; 5028 file_priv->dev_priv = to_i915(dev); 5029 file_priv->file = file; 5030 INIT_LIST_HEAD(&file_priv->rps.link); 5031 5032 lockinit(&file_priv->mm.lock, "i915_priv", 0, 0); 5033 INIT_LIST_HEAD(&file_priv->mm.request_list); 5034 5035 file_priv->bsd_engine = -1; 5036 5037 ret = i915_gem_context_open(dev, file); 5038 if (ret) 5039 kfree(file_priv); 5040 5041 return ret; 5042 } 5043 5044 /** 5045 * i915_gem_track_fb - update frontbuffer tracking 5046 * @old: current GEM buffer for the frontbuffer slots 5047 * @new: new GEM buffer for the frontbuffer slots 5048 * @frontbuffer_bits: bitmask of frontbuffer slots 5049 * 5050 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them 5051 * from @old and setting them in @new. Both @old and @new can be NULL. 5052 */ 5053 void i915_gem_track_fb(struct drm_i915_gem_object *old, 5054 struct drm_i915_gem_object *new, 5055 unsigned frontbuffer_bits) 5056 { 5057 /* Control of individual bits within the mask are guarded by 5058 * the owning plane->mutex, i.e. we can never see concurrent 5059 * manipulation of individual bits. But since the bitfield as a whole 5060 * is updated using RMW, we need to use atomics in order to update 5061 * the bits. 5062 */ 5063 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > 5064 sizeof(atomic_t) * BITS_PER_BYTE); 5065 5066 if (old) { 5067 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); 5068 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); 5069 } 5070 5071 if (new) { 5072 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); 5073 atomic_or(frontbuffer_bits, &new->frontbuffer_bits); 5074 } 5075 } 5076 5077 /* Allocate a new GEM object and fill it with the supplied data */ 5078 struct drm_i915_gem_object * 5079 i915_gem_object_create_from_data(struct drm_device *dev, 5080 const void *data, size_t size) 5081 { 5082 struct drm_i915_gem_object *obj; 5083 struct sg_table *sg; 5084 size_t bytes; 5085 int ret; 5086 5087 obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE)); 5088 if (IS_ERR(obj)) 5089 return obj; 5090 5091 ret = i915_gem_object_set_to_cpu_domain(obj, true); 5092 if (ret) 5093 goto fail; 5094 5095 ret = i915_gem_object_pin_pages(obj); 5096 if (ret) 5097 goto fail; 5098 5099 sg = obj->mm.pages; 5100 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size); 5101 obj->mm.dirty = true; /* Backing store is now out of date */ 5102 i915_gem_object_unpin_pages(obj); 5103 5104 if (WARN_ON(bytes != size)) { 5105 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size); 5106 ret = -EFAULT; 5107 goto fail; 5108 } 5109 5110 return obj; 5111 5112 fail: 5113 i915_gem_object_put(obj); 5114 return ERR_PTR(ret); 5115 } 5116 5117 struct scatterlist * 5118 i915_gem_object_get_sg(struct drm_i915_gem_object *obj, 5119 unsigned int n, 5120 unsigned int *offset) 5121 { 5122 struct i915_gem_object_page_iter *iter = &obj->mm.get_page; 5123 struct scatterlist *sg; 5124 unsigned int idx, count; 5125 5126 might_sleep(); 5127 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); 5128 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 5129 5130 /* As we iterate forward through the sg, we record each entry in a 5131 * radixtree for quick repeated (backwards) lookups. If we have seen 5132 * this index previously, we will have an entry for it. 5133 * 5134 * Initial lookup is O(N), but this is amortized to O(1) for 5135 * sequential page access (where each new request is consecutive 5136 * to the previous one). Repeated lookups are O(lg(obj->base.size)), 5137 * i.e. O(1) with a large constant! 5138 */ 5139 if (n < READ_ONCE(iter->sg_idx)) 5140 goto lookup; 5141 5142 mutex_lock(&iter->lock); 5143 5144 /* We prefer to reuse the last sg so that repeated lookup of this 5145 * (or the subsequent) sg are fast - comparing against the last 5146 * sg is faster than going through the radixtree. 5147 */ 5148 5149 sg = iter->sg_pos; 5150 idx = iter->sg_idx; 5151 count = __sg_page_count(sg); 5152 5153 while (idx + count <= n) { 5154 unsigned long exception, i; 5155 int ret; 5156 5157 /* If we cannot allocate and insert this entry, or the 5158 * individual pages from this range, cancel updating the 5159 * sg_idx so that on this lookup we are forced to linearly 5160 * scan onwards, but on future lookups we will try the 5161 * insertion again (in which case we need to be careful of 5162 * the error return reporting that we have already inserted 5163 * this index). 5164 */ 5165 ret = radix_tree_insert(&iter->radix, idx, sg); 5166 if (ret && ret != -EEXIST) 5167 goto scan; 5168 5169 exception = 5170 RADIX_TREE_EXCEPTIONAL_ENTRY | 5171 idx << RADIX_TREE_EXCEPTIONAL_SHIFT; 5172 for (i = 1; i < count; i++) { 5173 ret = radix_tree_insert(&iter->radix, idx + i, 5174 (void *)exception); 5175 if (ret && ret != -EEXIST) 5176 goto scan; 5177 } 5178 5179 idx += count; 5180 sg = ____sg_next(sg); 5181 count = __sg_page_count(sg); 5182 } 5183 5184 scan: 5185 iter->sg_pos = sg; 5186 iter->sg_idx = idx; 5187 5188 mutex_unlock(&iter->lock); 5189 5190 if (unlikely(n < idx)) /* insertion completed by another thread */ 5191 goto lookup; 5192 5193 /* In case we failed to insert the entry into the radixtree, we need 5194 * to look beyond the current sg. 5195 */ 5196 while (idx + count <= n) { 5197 idx += count; 5198 sg = ____sg_next(sg); 5199 count = __sg_page_count(sg); 5200 } 5201 5202 *offset = n - idx; 5203 return sg; 5204 5205 lookup: 5206 rcu_read_lock(); 5207 5208 sg = radix_tree_lookup(&iter->radix, n); 5209 GEM_BUG_ON(!sg); 5210 5211 /* If this index is in the middle of multi-page sg entry, 5212 * the radixtree will contain an exceptional entry that points 5213 * to the start of that range. We will return the pointer to 5214 * the base page and the offset of this page within the 5215 * sg entry's range. 5216 */ 5217 *offset = 0; 5218 if (unlikely(radix_tree_exception(sg))) { 5219 unsigned long base = 5220 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; 5221 5222 sg = radix_tree_lookup(&iter->radix, base); 5223 GEM_BUG_ON(!sg); 5224 5225 *offset = n - base; 5226 } 5227 5228 rcu_read_unlock(); 5229 5230 return sg; 5231 } 5232 5233 struct page * 5234 i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) 5235 { 5236 struct scatterlist *sg; 5237 unsigned int offset; 5238 5239 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); 5240 5241 sg = i915_gem_object_get_sg(obj, n, &offset); 5242 return nth_page(sg_page(sg), offset); 5243 } 5244 5245 /* Like i915_gem_object_get_page(), but mark the returned page dirty */ 5246 struct page * 5247 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, 5248 unsigned int n) 5249 { 5250 struct page *page; 5251 5252 page = i915_gem_object_get_page(obj, n); 5253 if (!obj->mm.dirty) 5254 set_page_dirty(page); 5255 5256 return page; 5257 } 5258 5259 dma_addr_t 5260 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, 5261 unsigned long n) 5262 { 5263 struct scatterlist *sg; 5264 unsigned int offset; 5265 5266 sg = i915_gem_object_get_sg(obj, n, &offset); 5267 return sg_dma_address(sg) + (offset << PAGE_SHIFT); 5268 } 5269