1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2014-2016 Intel Corporation 5 */ 6 7 #include <linux/anon_inodes.h> 8 #include <linux/mman.h> 9 #include <linux/pfn_t.h> 10 #include <linux/sizes.h> 11 12 #include "gt/intel_gt.h" 13 #include "gt/intel_gt_requests.h" 14 15 #include "i915_drv.h" 16 #include "i915_gem_gtt.h" 17 #include "i915_gem_ioctls.h" 18 #include "i915_gem_object.h" 19 #include "i915_gem_mman.h" 20 #include "i915_trace.h" 21 #include "i915_user_extensions.h" 22 #include "i915_vma.h" 23 24 static inline bool 25 __vma_matches(struct vm_area_struct *vma, struct file *filp, 26 unsigned long addr, unsigned long size) 27 { 28 if (vma->vm_file != filp) 29 return false; 30 31 return vma->vm_start == addr && 32 (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size); 33 } 34 35 /** 36 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address 37 * it is mapped to. 38 * @dev: drm device 39 * @data: ioctl data blob 40 * @file: drm file 41 * 42 * While the mapping holds a reference on the contents of the object, it doesn't 43 * imply a ref on the object itself. 44 * 45 * IMPORTANT: 46 * 47 * DRM driver writers who look a this function as an example for how to do GEM 48 * mmap support, please don't implement mmap support like here. The modern way 49 * to implement DRM mmap support is with an mmap offset ioctl (like 50 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. 51 * That way debug tooling like valgrind will understand what's going on, hiding 52 * the mmap call in a driver private ioctl will break that. The i915 driver only 53 * does cpu mmaps this way because we didn't know better. 54 */ 55 int 56 i915_gem_mmap_ioctl(struct drm_device *dev, void *data, 57 struct drm_file *file) 58 { 59 struct drm_i915_private *i915 = to_i915(dev); 60 struct drm_i915_gem_mmap *args = data; 61 struct drm_i915_gem_object *obj; 62 unsigned long addr; 63 64 /* mmap ioctl is disallowed for all platforms after TGL-LP. This also 65 * covers all platforms with local memory. 66 */ 67 if (GRAPHICS_VER(i915) >= 12 && !IS_TIGERLAKE(i915)) 68 return -EOPNOTSUPP; 69 70 if (args->flags & ~(I915_MMAP_WC)) 71 return -EINVAL; 72 73 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) 74 return -ENODEV; 75 76 obj = i915_gem_object_lookup(file, args->handle); 77 if (!obj) 78 return -ENOENT; 79 80 /* prime objects have no backing filp to GEM mmap 81 * pages from. 82 */ 83 if (!obj->base.filp) { 84 addr = -ENXIO; 85 goto err; 86 } 87 88 if (range_overflows(args->offset, args->size, (u64)obj->base.size)) { 89 addr = -EINVAL; 90 goto err; 91 } 92 93 addr = vm_mmap(obj->base.filp, 0, args->size, 94 PROT_READ | PROT_WRITE, MAP_SHARED, 95 args->offset); 96 if (IS_ERR_VALUE(addr)) 97 goto err; 98 99 if (args->flags & I915_MMAP_WC) { 100 struct mm_struct *mm = current->mm; 101 struct vm_area_struct *vma; 102 103 if (mmap_write_lock_killable(mm)) { 104 addr = -EINTR; 105 goto err; 106 } 107 vma = find_vma(mm, addr); 108 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) 109 vma->vm_page_prot = 110 pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 111 else 112 addr = -ENOMEM; 113 mmap_write_unlock(mm); 114 if (IS_ERR_VALUE(addr)) 115 goto err; 116 } 117 i915_gem_object_put(obj); 118 119 args->addr_ptr = (u64)addr; 120 return 0; 121 122 err: 123 i915_gem_object_put(obj); 124 return addr; 125 } 126 127 static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj) 128 { 129 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; 130 } 131 132 /** 133 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps 134 * 135 * A history of the GTT mmap interface: 136 * 137 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to 138 * aligned and suitable for fencing, and still fit into the available 139 * mappable space left by the pinned display objects. A classic problem 140 * we called the page-fault-of-doom where we would ping-pong between 141 * two objects that could not fit inside the GTT and so the memcpy 142 * would page one object in at the expense of the other between every 143 * single byte. 144 * 145 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none 146 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the 147 * object is too large for the available space (or simply too large 148 * for the mappable aperture!), a view is created instead and faulted 149 * into userspace. (This view is aligned and sized appropriately for 150 * fenced access.) 151 * 152 * 2 - Recognise WC as a separate cache domain so that we can flush the 153 * delayed writes via GTT before performing direct access via WC. 154 * 155 * 3 - Remove implicit set-domain(GTT) and synchronisation on initial 156 * pagefault; swapin remains transparent. 157 * 158 * 4 - Support multiple fault handlers per object depending on object's 159 * backing storage (a.k.a. MMAP_OFFSET). 160 * 161 * Restrictions: 162 * 163 * * snoopable objects cannot be accessed via the GTT. It can cause machine 164 * hangs on some architectures, corruption on others. An attempt to service 165 * a GTT page fault from a snoopable object will generate a SIGBUS. 166 * 167 * * the object must be able to fit into RAM (physical memory, though no 168 * limited to the mappable aperture). 169 * 170 * 171 * Caveats: 172 * 173 * * a new GTT page fault will synchronize rendering from the GPU and flush 174 * all data to system memory. Subsequent access will not be synchronized. 175 * 176 * * all mappings are revoked on runtime device suspend. 177 * 178 * * there are only 8, 16 or 32 fence registers to share between all users 179 * (older machines require fence register for display and blitter access 180 * as well). Contention of the fence registers will cause the previous users 181 * to be unmapped and any new access will generate new page faults. 182 * 183 * * running out of memory while servicing a fault may generate a SIGBUS, 184 * rather than the expected SIGSEGV. 185 */ 186 int i915_gem_mmap_gtt_version(void) 187 { 188 return 4; 189 } 190 191 static inline struct i915_ggtt_view 192 compute_partial_view(const struct drm_i915_gem_object *obj, 193 pgoff_t page_offset, 194 unsigned int chunk) 195 { 196 struct i915_ggtt_view view; 197 198 if (i915_gem_object_is_tiled(obj)) 199 chunk = roundup(chunk, tile_row_pages(obj) ?: 1); 200 201 view.type = I915_GGTT_VIEW_PARTIAL; 202 view.partial.offset = rounddown(page_offset, chunk); 203 view.partial.size = 204 min_t(unsigned int, chunk, 205 (obj->base.size >> PAGE_SHIFT) - view.partial.offset); 206 207 /* If the partial covers the entire object, just create a normal VMA. */ 208 if (chunk >= obj->base.size >> PAGE_SHIFT) 209 view.type = I915_GGTT_VIEW_NORMAL; 210 211 return view; 212 } 213 214 static vm_fault_t i915_error_to_vmf_fault(int err) 215 { 216 switch (err) { 217 default: 218 WARN_ONCE(err, "unhandled error in %s: %i\n", __func__, err); 219 fallthrough; 220 case -EIO: /* shmemfs failure from swap device */ 221 case -EFAULT: /* purged object */ 222 case -ENODEV: /* bad object, how did you get here! */ 223 case -ENXIO: /* unable to access backing store (on device) */ 224 return VM_FAULT_SIGBUS; 225 226 case -ENOMEM: /* our allocation failure */ 227 return VM_FAULT_OOM; 228 229 case 0: 230 case -EAGAIN: 231 case -ENOSPC: /* transient failure to evict? */ 232 case -ERESTARTSYS: 233 case -EINTR: 234 case -EBUSY: 235 /* 236 * EBUSY is ok: this just means that another thread 237 * already did the job. 238 */ 239 return VM_FAULT_NOPAGE; 240 } 241 } 242 243 static vm_fault_t vm_fault_cpu(struct vm_fault *vmf) 244 { 245 struct vm_area_struct *area = vmf->vma; 246 struct i915_mmap_offset *mmo = area->vm_private_data; 247 struct drm_i915_gem_object *obj = mmo->obj; 248 resource_size_t iomap; 249 int err; 250 251 /* Sanity check that we allow writing into this object */ 252 if (unlikely(i915_gem_object_is_readonly(obj) && 253 area->vm_flags & VM_WRITE)) 254 return VM_FAULT_SIGBUS; 255 256 if (i915_gem_object_lock_interruptible(obj, NULL)) 257 return VM_FAULT_NOPAGE; 258 259 err = i915_gem_object_pin_pages(obj); 260 if (err) 261 goto out; 262 263 iomap = -1; 264 if (!i915_gem_object_has_struct_page(obj)) { 265 iomap = obj->mm.region->iomap.base; 266 iomap -= obj->mm.region->region.start; 267 } 268 269 /* PTEs are revoked in obj->ops->put_pages() */ 270 err = remap_io_sg(area, 271 area->vm_start, area->vm_end - area->vm_start, 272 obj->mm.pages->sgl, iomap); 273 274 if (area->vm_flags & VM_WRITE) { 275 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 276 obj->mm.dirty = true; 277 } 278 279 i915_gem_object_unpin_pages(obj); 280 281 out: 282 i915_gem_object_unlock(obj); 283 return i915_error_to_vmf_fault(err); 284 } 285 286 static vm_fault_t vm_fault_gtt(struct vm_fault *vmf) 287 { 288 #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) 289 struct vm_area_struct *area = vmf->vma; 290 struct i915_mmap_offset *mmo = area->vm_private_data; 291 struct drm_i915_gem_object *obj = mmo->obj; 292 struct drm_device *dev = obj->base.dev; 293 struct drm_i915_private *i915 = to_i915(dev); 294 struct intel_runtime_pm *rpm = &i915->runtime_pm; 295 struct i915_ggtt *ggtt = &i915->ggtt; 296 bool write = area->vm_flags & VM_WRITE; 297 struct i915_gem_ww_ctx ww; 298 intel_wakeref_t wakeref; 299 struct i915_vma *vma; 300 pgoff_t page_offset; 301 int srcu; 302 int ret; 303 304 /* We don't use vmf->pgoff since that has the fake offset */ 305 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; 306 307 trace_i915_gem_object_fault(obj, page_offset, true, write); 308 309 wakeref = intel_runtime_pm_get(rpm); 310 311 i915_gem_ww_ctx_init(&ww, true); 312 retry: 313 ret = i915_gem_object_lock(obj, &ww); 314 if (ret) 315 goto err_rpm; 316 317 /* Sanity check that we allow writing into this object */ 318 if (i915_gem_object_is_readonly(obj) && write) { 319 ret = -EFAULT; 320 goto err_rpm; 321 } 322 323 ret = i915_gem_object_pin_pages(obj); 324 if (ret) 325 goto err_rpm; 326 327 ret = intel_gt_reset_trylock(ggtt->vm.gt, &srcu); 328 if (ret) 329 goto err_pages; 330 331 /* Now pin it into the GTT as needed */ 332 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, NULL, 0, 0, 333 PIN_MAPPABLE | 334 PIN_NONBLOCK /* NOWARN */ | 335 PIN_NOEVICT); 336 if (IS_ERR(vma) && vma != ERR_PTR(-EDEADLK)) { 337 /* Use a partial view if it is bigger than available space */ 338 struct i915_ggtt_view view = 339 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); 340 unsigned int flags; 341 342 flags = PIN_MAPPABLE | PIN_NOSEARCH; 343 if (view.type == I915_GGTT_VIEW_NORMAL) 344 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ 345 346 /* 347 * Userspace is now writing through an untracked VMA, abandon 348 * all hope that the hardware is able to track future writes. 349 */ 350 351 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, &view, 0, 0, flags); 352 if (IS_ERR(vma) && vma != ERR_PTR(-EDEADLK)) { 353 flags = PIN_MAPPABLE; 354 view.type = I915_GGTT_VIEW_PARTIAL; 355 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, &view, 0, 0, flags); 356 } 357 358 /* The entire mappable GGTT is pinned? Unexpected! */ 359 GEM_BUG_ON(vma == ERR_PTR(-ENOSPC)); 360 } 361 if (IS_ERR(vma)) { 362 ret = PTR_ERR(vma); 363 goto err_reset; 364 } 365 366 /* Access to snoopable pages through the GTT is incoherent. */ 367 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(i915)) { 368 ret = -EFAULT; 369 goto err_unpin; 370 } 371 372 ret = i915_vma_pin_fence(vma); 373 if (ret) 374 goto err_unpin; 375 376 /* Finally, remap it using the new GTT offset */ 377 ret = remap_io_mapping(area, 378 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), 379 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, 380 min_t(u64, vma->size, area->vm_end - area->vm_start), 381 &ggtt->iomap); 382 if (ret) 383 goto err_fence; 384 385 assert_rpm_wakelock_held(rpm); 386 387 /* Mark as being mmapped into userspace for later revocation */ 388 mutex_lock(&i915->ggtt.vm.mutex); 389 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) 390 list_add(&obj->userfault_link, &i915->ggtt.userfault_list); 391 mutex_unlock(&i915->ggtt.vm.mutex); 392 393 /* Track the mmo associated with the fenced vma */ 394 vma->mmo = mmo; 395 396 if (IS_ACTIVE(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)) 397 intel_wakeref_auto(&i915->ggtt.userfault_wakeref, 398 msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)); 399 400 if (write) { 401 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 402 i915_vma_set_ggtt_write(vma); 403 obj->mm.dirty = true; 404 } 405 406 err_fence: 407 i915_vma_unpin_fence(vma); 408 err_unpin: 409 __i915_vma_unpin(vma); 410 err_reset: 411 intel_gt_reset_unlock(ggtt->vm.gt, srcu); 412 err_pages: 413 i915_gem_object_unpin_pages(obj); 414 err_rpm: 415 if (ret == -EDEADLK) { 416 ret = i915_gem_ww_ctx_backoff(&ww); 417 if (!ret) 418 goto retry; 419 } 420 i915_gem_ww_ctx_fini(&ww); 421 intel_runtime_pm_put(rpm, wakeref); 422 return i915_error_to_vmf_fault(ret); 423 } 424 425 static int 426 vm_access(struct vm_area_struct *area, unsigned long addr, 427 void *buf, int len, int write) 428 { 429 struct i915_mmap_offset *mmo = area->vm_private_data; 430 struct drm_i915_gem_object *obj = mmo->obj; 431 struct i915_gem_ww_ctx ww; 432 void *vaddr; 433 int err = 0; 434 435 if (i915_gem_object_is_readonly(obj) && write) 436 return -EACCES; 437 438 addr -= area->vm_start; 439 if (addr >= obj->base.size) 440 return -EINVAL; 441 442 i915_gem_ww_ctx_init(&ww, true); 443 retry: 444 err = i915_gem_object_lock(obj, &ww); 445 if (err) 446 goto out; 447 448 /* As this is primarily for debugging, let's focus on simplicity */ 449 vaddr = i915_gem_object_pin_map(obj, I915_MAP_FORCE_WC); 450 if (IS_ERR(vaddr)) { 451 err = PTR_ERR(vaddr); 452 goto out; 453 } 454 455 if (write) { 456 memcpy(vaddr + addr, buf, len); 457 __i915_gem_object_flush_map(obj, addr, len); 458 } else { 459 memcpy(buf, vaddr + addr, len); 460 } 461 462 i915_gem_object_unpin_map(obj); 463 out: 464 if (err == -EDEADLK) { 465 err = i915_gem_ww_ctx_backoff(&ww); 466 if (!err) 467 goto retry; 468 } 469 i915_gem_ww_ctx_fini(&ww); 470 471 if (err) 472 return err; 473 474 return len; 475 } 476 477 void __i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj) 478 { 479 struct i915_vma *vma; 480 481 GEM_BUG_ON(!obj->userfault_count); 482 483 for_each_ggtt_vma(vma, obj) 484 i915_vma_revoke_mmap(vma); 485 486 GEM_BUG_ON(obj->userfault_count); 487 } 488 489 /* 490 * It is vital that we remove the page mapping if we have mapped a tiled 491 * object through the GTT and then lose the fence register due to 492 * resource pressure. Similarly if the object has been moved out of the 493 * aperture, than pages mapped into userspace must be revoked. Removing the 494 * mapping will then trigger a page fault on the next user access, allowing 495 * fixup by vm_fault_gtt(). 496 */ 497 void i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj) 498 { 499 struct drm_i915_private *i915 = to_i915(obj->base.dev); 500 intel_wakeref_t wakeref; 501 502 /* 503 * Serialisation between user GTT access and our code depends upon 504 * revoking the CPU's PTE whilst the mutex is held. The next user 505 * pagefault then has to wait until we release the mutex. 506 * 507 * Note that RPM complicates somewhat by adding an additional 508 * requirement that operations to the GGTT be made holding the RPM 509 * wakeref. 510 */ 511 wakeref = intel_runtime_pm_get(&i915->runtime_pm); 512 mutex_lock(&i915->ggtt.vm.mutex); 513 514 if (!obj->userfault_count) 515 goto out; 516 517 __i915_gem_object_release_mmap_gtt(obj); 518 519 /* 520 * Ensure that the CPU's PTE are revoked and there are not outstanding 521 * memory transactions from userspace before we return. The TLB 522 * flushing implied above by changing the PTE above *should* be 523 * sufficient, an extra barrier here just provides us with a bit 524 * of paranoid documentation about our requirement to serialise 525 * memory writes before touching registers / GSM. 526 */ 527 wmb(); 528 529 out: 530 mutex_unlock(&i915->ggtt.vm.mutex); 531 intel_runtime_pm_put(&i915->runtime_pm, wakeref); 532 } 533 534 void i915_gem_object_release_mmap_offset(struct drm_i915_gem_object *obj) 535 { 536 struct i915_mmap_offset *mmo, *mn; 537 538 spin_lock(&obj->mmo.lock); 539 rbtree_postorder_for_each_entry_safe(mmo, mn, 540 &obj->mmo.offsets, offset) { 541 /* 542 * vma_node_unmap for GTT mmaps handled already in 543 * __i915_gem_object_release_mmap_gtt 544 */ 545 if (mmo->mmap_type == I915_MMAP_TYPE_GTT) 546 continue; 547 548 spin_unlock(&obj->mmo.lock); 549 drm_vma_node_unmap(&mmo->vma_node, 550 obj->base.dev->anon_inode->i_mapping); 551 spin_lock(&obj->mmo.lock); 552 } 553 spin_unlock(&obj->mmo.lock); 554 } 555 556 static struct i915_mmap_offset * 557 lookup_mmo(struct drm_i915_gem_object *obj, 558 enum i915_mmap_type mmap_type) 559 { 560 struct rb_node *rb; 561 562 spin_lock(&obj->mmo.lock); 563 rb = obj->mmo.offsets.rb_node; 564 while (rb) { 565 struct i915_mmap_offset *mmo = 566 rb_entry(rb, typeof(*mmo), offset); 567 568 if (mmo->mmap_type == mmap_type) { 569 spin_unlock(&obj->mmo.lock); 570 return mmo; 571 } 572 573 if (mmo->mmap_type < mmap_type) 574 rb = rb->rb_right; 575 else 576 rb = rb->rb_left; 577 } 578 spin_unlock(&obj->mmo.lock); 579 580 return NULL; 581 } 582 583 static struct i915_mmap_offset * 584 insert_mmo(struct drm_i915_gem_object *obj, struct i915_mmap_offset *mmo) 585 { 586 struct rb_node *rb, **p; 587 588 spin_lock(&obj->mmo.lock); 589 rb = NULL; 590 p = &obj->mmo.offsets.rb_node; 591 while (*p) { 592 struct i915_mmap_offset *pos; 593 594 rb = *p; 595 pos = rb_entry(rb, typeof(*pos), offset); 596 597 if (pos->mmap_type == mmo->mmap_type) { 598 spin_unlock(&obj->mmo.lock); 599 drm_vma_offset_remove(obj->base.dev->vma_offset_manager, 600 &mmo->vma_node); 601 kfree(mmo); 602 return pos; 603 } 604 605 if (pos->mmap_type < mmo->mmap_type) 606 p = &rb->rb_right; 607 else 608 p = &rb->rb_left; 609 } 610 rb_link_node(&mmo->offset, rb, p); 611 rb_insert_color(&mmo->offset, &obj->mmo.offsets); 612 spin_unlock(&obj->mmo.lock); 613 614 return mmo; 615 } 616 617 static struct i915_mmap_offset * 618 mmap_offset_attach(struct drm_i915_gem_object *obj, 619 enum i915_mmap_type mmap_type, 620 struct drm_file *file) 621 { 622 struct drm_i915_private *i915 = to_i915(obj->base.dev); 623 struct i915_mmap_offset *mmo; 624 int err; 625 626 mmo = lookup_mmo(obj, mmap_type); 627 if (mmo) 628 goto out; 629 630 mmo = kmalloc(sizeof(*mmo), GFP_KERNEL); 631 if (!mmo) 632 return ERR_PTR(-ENOMEM); 633 634 mmo->obj = obj; 635 mmo->mmap_type = mmap_type; 636 drm_vma_node_reset(&mmo->vma_node); 637 638 err = drm_vma_offset_add(obj->base.dev->vma_offset_manager, 639 &mmo->vma_node, obj->base.size / PAGE_SIZE); 640 if (likely(!err)) 641 goto insert; 642 643 /* Attempt to reap some mmap space from dead objects */ 644 err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT); 645 if (err) 646 goto err; 647 648 i915_gem_drain_freed_objects(i915); 649 err = drm_vma_offset_add(obj->base.dev->vma_offset_manager, 650 &mmo->vma_node, obj->base.size / PAGE_SIZE); 651 if (err) 652 goto err; 653 654 insert: 655 mmo = insert_mmo(obj, mmo); 656 GEM_BUG_ON(lookup_mmo(obj, mmap_type) != mmo); 657 out: 658 if (file) 659 drm_vma_node_allow(&mmo->vma_node, file); 660 return mmo; 661 662 err: 663 kfree(mmo); 664 return ERR_PTR(err); 665 } 666 667 static int 668 __assign_mmap_offset(struct drm_file *file, 669 u32 handle, 670 enum i915_mmap_type mmap_type, 671 u64 *offset) 672 { 673 struct drm_i915_gem_object *obj; 674 struct i915_mmap_offset *mmo; 675 int err; 676 677 obj = i915_gem_object_lookup(file, handle); 678 if (!obj) 679 return -ENOENT; 680 681 if (i915_gem_object_never_mmap(obj)) { 682 err = -ENODEV; 683 goto out; 684 } 685 686 if (mmap_type != I915_MMAP_TYPE_GTT && 687 !i915_gem_object_has_struct_page(obj) && 688 !i915_gem_object_type_has(obj, I915_GEM_OBJECT_HAS_IOMEM)) { 689 err = -ENODEV; 690 goto out; 691 } 692 693 mmo = mmap_offset_attach(obj, mmap_type, file); 694 if (IS_ERR(mmo)) { 695 err = PTR_ERR(mmo); 696 goto out; 697 } 698 699 *offset = drm_vma_node_offset_addr(&mmo->vma_node); 700 err = 0; 701 out: 702 i915_gem_object_put(obj); 703 return err; 704 } 705 706 int 707 i915_gem_dumb_mmap_offset(struct drm_file *file, 708 struct drm_device *dev, 709 u32 handle, 710 u64 *offset) 711 { 712 enum i915_mmap_type mmap_type; 713 714 if (boot_cpu_has(X86_FEATURE_PAT)) 715 mmap_type = I915_MMAP_TYPE_WC; 716 else if (!i915_ggtt_has_aperture(&to_i915(dev)->ggtt)) 717 return -ENODEV; 718 else 719 mmap_type = I915_MMAP_TYPE_GTT; 720 721 return __assign_mmap_offset(file, handle, mmap_type, offset); 722 } 723 724 /** 725 * i915_gem_mmap_offset_ioctl - prepare an object for GTT mmap'ing 726 * @dev: DRM device 727 * @data: GTT mapping ioctl data 728 * @file: GEM object info 729 * 730 * Simply returns the fake offset to userspace so it can mmap it. 731 * The mmap call will end up in drm_gem_mmap(), which will set things 732 * up so we can get faults in the handler above. 733 * 734 * The fault handler will take care of binding the object into the GTT 735 * (since it may have been evicted to make room for something), allocating 736 * a fence register, and mapping the appropriate aperture address into 737 * userspace. 738 */ 739 int 740 i915_gem_mmap_offset_ioctl(struct drm_device *dev, void *data, 741 struct drm_file *file) 742 { 743 struct drm_i915_private *i915 = to_i915(dev); 744 struct drm_i915_gem_mmap_offset *args = data; 745 enum i915_mmap_type type; 746 int err; 747 748 /* 749 * Historically we failed to check args.pad and args.offset 750 * and so we cannot use those fields for user input and we cannot 751 * add -EINVAL for them as the ABI is fixed, i.e. old userspace 752 * may be feeding in garbage in those fields. 753 * 754 * if (args->pad) return -EINVAL; is verbotten! 755 */ 756 757 err = i915_user_extensions(u64_to_user_ptr(args->extensions), 758 NULL, 0, NULL); 759 if (err) 760 return err; 761 762 switch (args->flags) { 763 case I915_MMAP_OFFSET_GTT: 764 if (!i915_ggtt_has_aperture(&i915->ggtt)) 765 return -ENODEV; 766 type = I915_MMAP_TYPE_GTT; 767 break; 768 769 case I915_MMAP_OFFSET_WC: 770 if (!boot_cpu_has(X86_FEATURE_PAT)) 771 return -ENODEV; 772 type = I915_MMAP_TYPE_WC; 773 break; 774 775 case I915_MMAP_OFFSET_WB: 776 type = I915_MMAP_TYPE_WB; 777 break; 778 779 case I915_MMAP_OFFSET_UC: 780 if (!boot_cpu_has(X86_FEATURE_PAT)) 781 return -ENODEV; 782 type = I915_MMAP_TYPE_UC; 783 break; 784 785 default: 786 return -EINVAL; 787 } 788 789 return __assign_mmap_offset(file, args->handle, type, &args->offset); 790 } 791 792 static void vm_open(struct vm_area_struct *vma) 793 { 794 struct i915_mmap_offset *mmo = vma->vm_private_data; 795 struct drm_i915_gem_object *obj = mmo->obj; 796 797 GEM_BUG_ON(!obj); 798 i915_gem_object_get(obj); 799 } 800 801 static void vm_close(struct vm_area_struct *vma) 802 { 803 struct i915_mmap_offset *mmo = vma->vm_private_data; 804 struct drm_i915_gem_object *obj = mmo->obj; 805 806 GEM_BUG_ON(!obj); 807 i915_gem_object_put(obj); 808 } 809 810 static const struct vm_operations_struct vm_ops_gtt = { 811 .fault = vm_fault_gtt, 812 .access = vm_access, 813 .open = vm_open, 814 .close = vm_close, 815 }; 816 817 static const struct vm_operations_struct vm_ops_cpu = { 818 .fault = vm_fault_cpu, 819 .access = vm_access, 820 .open = vm_open, 821 .close = vm_close, 822 }; 823 824 static int singleton_release(struct inode *inode, struct file *file) 825 { 826 struct drm_i915_private *i915 = file->private_data; 827 828 cmpxchg(&i915->gem.mmap_singleton, file, NULL); 829 drm_dev_put(&i915->drm); 830 831 return 0; 832 } 833 834 static const struct file_operations singleton_fops = { 835 .owner = THIS_MODULE, 836 .release = singleton_release, 837 }; 838 839 static struct file *mmap_singleton(struct drm_i915_private *i915) 840 { 841 struct file *file; 842 843 rcu_read_lock(); 844 file = READ_ONCE(i915->gem.mmap_singleton); 845 if (file && !get_file_rcu(file)) 846 file = NULL; 847 rcu_read_unlock(); 848 if (file) 849 return file; 850 851 file = anon_inode_getfile("i915.gem", &singleton_fops, i915, O_RDWR); 852 if (IS_ERR(file)) 853 return file; 854 855 /* Everyone shares a single global address space */ 856 file->f_mapping = i915->drm.anon_inode->i_mapping; 857 858 smp_store_mb(i915->gem.mmap_singleton, file); 859 drm_dev_get(&i915->drm); 860 861 return file; 862 } 863 864 /* 865 * This overcomes the limitation in drm_gem_mmap's assignment of a 866 * drm_gem_object as the vma->vm_private_data. Since we need to 867 * be able to resolve multiple mmap offsets which could be tied 868 * to a single gem object. 869 */ 870 int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma) 871 { 872 struct drm_vma_offset_node *node; 873 struct drm_file *priv = filp->private_data; 874 struct drm_device *dev = priv->minor->dev; 875 struct drm_i915_gem_object *obj = NULL; 876 struct i915_mmap_offset *mmo = NULL; 877 struct file *anon; 878 879 if (drm_dev_is_unplugged(dev)) 880 return -ENODEV; 881 882 rcu_read_lock(); 883 drm_vma_offset_lock_lookup(dev->vma_offset_manager); 884 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager, 885 vma->vm_pgoff, 886 vma_pages(vma)); 887 if (node && drm_vma_node_is_allowed(node, priv)) { 888 /* 889 * Skip 0-refcnted objects as it is in the process of being 890 * destroyed and will be invalid when the vma manager lock 891 * is released. 892 */ 893 mmo = container_of(node, struct i915_mmap_offset, vma_node); 894 obj = i915_gem_object_get_rcu(mmo->obj); 895 } 896 drm_vma_offset_unlock_lookup(dev->vma_offset_manager); 897 rcu_read_unlock(); 898 if (!obj) 899 return node ? -EACCES : -EINVAL; 900 901 if (i915_gem_object_is_readonly(obj)) { 902 if (vma->vm_flags & VM_WRITE) { 903 i915_gem_object_put(obj); 904 return -EINVAL; 905 } 906 vma->vm_flags &= ~VM_MAYWRITE; 907 } 908 909 anon = mmap_singleton(to_i915(dev)); 910 if (IS_ERR(anon)) { 911 i915_gem_object_put(obj); 912 return PTR_ERR(anon); 913 } 914 915 vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 916 vma->vm_private_data = mmo; 917 918 /* 919 * We keep the ref on mmo->obj, not vm_file, but we require 920 * vma->vm_file->f_mapping, see vma_link(), for later revocation. 921 * Our userspace is accustomed to having per-file resource cleanup 922 * (i.e. contexts, objects and requests) on their close(fd), which 923 * requires avoiding extraneous references to their filp, hence why 924 * we prefer to use an anonymous file for their mmaps. 925 */ 926 vma_set_file(vma, anon); 927 /* Drop the initial creation reference, the vma is now holding one. */ 928 fput(anon); 929 930 switch (mmo->mmap_type) { 931 case I915_MMAP_TYPE_WC: 932 vma->vm_page_prot = 933 pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 934 vma->vm_ops = &vm_ops_cpu; 935 break; 936 937 case I915_MMAP_TYPE_WB: 938 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 939 vma->vm_ops = &vm_ops_cpu; 940 break; 941 942 case I915_MMAP_TYPE_UC: 943 vma->vm_page_prot = 944 pgprot_noncached(vm_get_page_prot(vma->vm_flags)); 945 vma->vm_ops = &vm_ops_cpu; 946 break; 947 948 case I915_MMAP_TYPE_GTT: 949 vma->vm_page_prot = 950 pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 951 vma->vm_ops = &vm_ops_gtt; 952 break; 953 } 954 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); 955 956 return 0; 957 } 958 959 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 960 #include "selftests/i915_gem_mman.c" 961 #endif 962