2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2016 Intel Corporation
7 #include <linux/anon_inodes.h>
8 #include <linux/mman.h>
9 #include <linux/pfn_t.h>
10 #include <linux/sizes.h>
12 #include <drm/drm_cache.h>
14 #include "gt/intel_gt.h"
15 #include "gt/intel_gt_requests.h"
18 #include "i915_gem_evict.h"
19 #include "i915_gem_gtt.h"
20 #include "i915_gem_ioctls.h"
21 #include "i915_gem_object.h"
22 #include "i915_gem_mman.h"
24 #include "i915_trace.h"
25 #include "i915_user_extensions.h"
26 #include "i915_gem_ttm.h"
30 __vma_matches(struct vm_area_struct *vma, struct file *filp,
31 unsigned long addr, unsigned long size)
33 if (vma->vm_file != filp)
36 return vma->vm_start == addr &&
37 (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size);
41 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
44 * @data: ioctl data blob
47 * While the mapping holds a reference on the contents of the object, it doesn't
48 * imply a ref on the object itself.
52 * DRM driver writers who look a this function as an example for how to do GEM
53 * mmap support, please don't implement mmap support like here. The modern way
54 * to implement DRM mmap support is with an mmap offset ioctl (like
55 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
56 * That way debug tooling like valgrind will understand what's going on, hiding
57 * the mmap call in a driver private ioctl will break that. The i915 driver only
58 * does cpu mmaps this way because we didn't know better.
61 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
62 struct drm_file *file)
64 struct drm_i915_private *i915 = to_i915(dev);
65 struct drm_i915_gem_mmap *args = data;
66 struct drm_i915_gem_object *obj;
70 * mmap ioctl is disallowed for all discrete platforms,
71 * and for all platforms with GRAPHICS_VER > 12.
73 if (IS_DGFX(i915) || GRAPHICS_VER_FULL(i915) > IP_VER(12, 0))
76 if (args->flags & ~(I915_MMAP_WC))
79 if (args->flags & I915_MMAP_WC && !pat_enabled())
82 obj = i915_gem_object_lookup(file, args->handle);
86 /* prime objects have no backing filp to GEM mmap
89 if (!obj->base.filp) {
94 if (range_overflows(args->offset, args->size, (u64)obj->base.size)) {
99 addr = vm_mmap(obj->base.filp, 0, args->size,
100 PROT_READ | PROT_WRITE, MAP_SHARED,
102 if (IS_ERR_VALUE(addr))
105 if (args->flags & I915_MMAP_WC) {
106 struct mm_struct *mm = current->mm;
107 struct vm_area_struct *vma;
109 if (mmap_write_lock_killable(mm)) {
113 vma = find_vma(mm, addr);
114 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
116 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
119 mmap_write_unlock(mm);
120 if (IS_ERR_VALUE(addr))
123 i915_gem_object_put(obj);
125 args->addr_ptr = (u64)addr;
129 i915_gem_object_put(obj);
133 static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
135 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
139 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
141 * A history of the GTT mmap interface:
143 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
144 * aligned and suitable for fencing, and still fit into the available
145 * mappable space left by the pinned display objects. A classic problem
146 * we called the page-fault-of-doom where we would ping-pong between
147 * two objects that could not fit inside the GTT and so the memcpy
148 * would page one object in at the expense of the other between every
151 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
152 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
153 * object is too large for the available space (or simply too large
154 * for the mappable aperture!), a view is created instead and faulted
155 * into userspace. (This view is aligned and sized appropriately for
158 * 2 - Recognise WC as a separate cache domain so that we can flush the
159 * delayed writes via GTT before performing direct access via WC.
161 * 3 - Remove implicit set-domain(GTT) and synchronisation on initial
162 * pagefault; swapin remains transparent.
164 * 4 - Support multiple fault handlers per object depending on object's
165 * backing storage (a.k.a. MMAP_OFFSET).
169 * * snoopable objects cannot be accessed via the GTT. It can cause machine
170 * hangs on some architectures, corruption on others. An attempt to service
171 * a GTT page fault from a snoopable object will generate a SIGBUS.
173 * * the object must be able to fit into RAM (physical memory, though no
174 * limited to the mappable aperture).
179 * * a new GTT page fault will synchronize rendering from the GPU and flush
180 * all data to system memory. Subsequent access will not be synchronized.
182 * * all mappings are revoked on runtime device suspend.
184 * * there are only 8, 16 or 32 fence registers to share between all users
185 * (older machines require fence register for display and blitter access
186 * as well). Contention of the fence registers will cause the previous users
187 * to be unmapped and any new access will generate new page faults.
189 * * running out of memory while servicing a fault may generate a SIGBUS,
190 * rather than the expected SIGSEGV.
192 int i915_gem_mmap_gtt_version(void)
197 static inline struct i915_gtt_view
198 compute_partial_view(const struct drm_i915_gem_object *obj,
202 struct i915_gtt_view view;
204 if (i915_gem_object_is_tiled(obj))
205 chunk = roundup(chunk, tile_row_pages(obj) ?: 1);
207 view.type = I915_GTT_VIEW_PARTIAL;
208 view.partial.offset = rounddown(page_offset, chunk);
210 min_t(unsigned int, chunk,
211 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
213 /* If the partial covers the entire object, just create a normal VMA. */
214 if (chunk >= obj->base.size >> PAGE_SHIFT)
215 view.type = I915_GTT_VIEW_NORMAL;
220 static vm_fault_t i915_error_to_vmf_fault(int err)
224 WARN_ONCE(err, "unhandled error in %s: %i\n", __func__, err);
226 case -EIO: /* shmemfs failure from swap device */
227 case -EFAULT: /* purged object */
228 case -ENODEV: /* bad object, how did you get here! */
229 case -ENXIO: /* unable to access backing store (on device) */
230 return VM_FAULT_SIGBUS;
232 case -ENOMEM: /* our allocation failure */
237 case -ENOSPC: /* transient failure to evict? */
238 case -ENOBUFS: /* temporarily out of fences? */
243 * EBUSY is ok: this just means that another thread
244 * already did the job.
246 return VM_FAULT_NOPAGE;
250 static vm_fault_t vm_fault_cpu(struct vm_fault *vmf)
252 struct vm_area_struct *area = vmf->vma;
253 struct i915_mmap_offset *mmo = area->vm_private_data;
254 struct drm_i915_gem_object *obj = mmo->obj;
255 resource_size_t iomap;
258 /* Sanity check that we allow writing into this object */
259 if (unlikely(i915_gem_object_is_readonly(obj) &&
260 area->vm_flags & VM_WRITE))
261 return VM_FAULT_SIGBUS;
263 if (i915_gem_object_lock_interruptible(obj, NULL))
264 return VM_FAULT_NOPAGE;
266 err = i915_gem_object_pin_pages(obj);
271 if (!i915_gem_object_has_struct_page(obj)) {
272 iomap = obj->mm.region->iomap.base;
273 iomap -= obj->mm.region->region.start;
276 /* PTEs are revoked in obj->ops->put_pages() */
277 err = remap_io_sg(area,
278 area->vm_start, area->vm_end - area->vm_start,
279 obj->mm.pages->sgl, iomap);
281 if (area->vm_flags & VM_WRITE) {
282 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
283 obj->mm.dirty = true;
286 i915_gem_object_unpin_pages(obj);
289 i915_gem_object_unlock(obj);
290 return i915_error_to_vmf_fault(err);
293 static vm_fault_t vm_fault_gtt(struct vm_fault *vmf)
295 #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
296 struct vm_area_struct *area = vmf->vma;
297 struct i915_mmap_offset *mmo = area->vm_private_data;
298 struct drm_i915_gem_object *obj = mmo->obj;
299 struct drm_device *dev = obj->base.dev;
300 struct drm_i915_private *i915 = to_i915(dev);
301 struct intel_runtime_pm *rpm = &i915->runtime_pm;
302 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
303 bool write = area->vm_flags & VM_WRITE;
304 struct i915_gem_ww_ctx ww;
305 intel_wakeref_t wakeref;
306 struct i915_vma *vma;
311 /* We don't use vmf->pgoff since that has the fake offset */
312 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
314 trace_i915_gem_object_fault(obj, page_offset, true, write);
316 wakeref = intel_runtime_pm_get(rpm);
318 i915_gem_ww_ctx_init(&ww, true);
320 ret = i915_gem_object_lock(obj, &ww);
324 /* Sanity check that we allow writing into this object */
325 if (i915_gem_object_is_readonly(obj) && write) {
330 ret = i915_gem_object_pin_pages(obj);
334 ret = intel_gt_reset_lock_interruptible(ggtt->vm.gt, &srcu);
338 /* Now pin it into the GTT as needed */
339 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, NULL, 0, 0,
341 PIN_NONBLOCK /* NOWARN */ |
343 if (IS_ERR(vma) && vma != ERR_PTR(-EDEADLK)) {
344 /* Use a partial view if it is bigger than available space */
345 struct i915_gtt_view view =
346 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
349 flags = PIN_MAPPABLE | PIN_NOSEARCH;
350 if (view.type == I915_GTT_VIEW_NORMAL)
351 flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
354 * Userspace is now writing through an untracked VMA, abandon
355 * all hope that the hardware is able to track future writes.
358 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, &view, 0, 0, flags);
359 if (IS_ERR(vma) && vma != ERR_PTR(-EDEADLK)) {
360 flags = PIN_MAPPABLE;
361 view.type = I915_GTT_VIEW_PARTIAL;
362 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, &view, 0, 0, flags);
366 * The entire mappable GGTT is pinned? Unexpected!
367 * Try to evict the object we locked too, as normally we skip it
368 * due to lack of short term pinning inside execbuf.
370 if (vma == ERR_PTR(-ENOSPC)) {
371 ret = mutex_lock_interruptible(&ggtt->vm.mutex);
373 ret = i915_gem_evict_vm(&ggtt->vm, &ww, NULL);
374 mutex_unlock(&ggtt->vm.mutex);
378 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, &view, 0, 0, flags);
386 /* Access to snoopable pages through the GTT is incoherent. */
388 * For objects created by userspace through GEM_CREATE with pat_index
389 * set by set_pat extension, coherency is managed by userspace, make
390 * sure we don't fail handling the vm fault by calling
391 * i915_gem_object_has_cache_level() which always return true for such
392 * objects. Otherwise this helper function would fall back to checking
393 * whether the object is un-cached.
395 if (!(i915_gem_object_has_cache_level(obj, I915_CACHE_NONE) ||
401 ret = i915_vma_pin_fence(vma);
405 /* Finally, remap it using the new GTT offset */
406 ret = remap_io_mapping(area,
407 area->vm_start + (vma->gtt_view.partial.offset << PAGE_SHIFT),
408 (ggtt->gmadr.start + i915_ggtt_offset(vma)) >> PAGE_SHIFT,
409 min_t(u64, vma->size, area->vm_end - area->vm_start),
414 assert_rpm_wakelock_held(rpm);
416 /* Mark as being mmapped into userspace for later revocation */
417 mutex_lock(&to_gt(i915)->ggtt->vm.mutex);
418 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
419 list_add(&obj->userfault_link, &to_gt(i915)->ggtt->userfault_list);
420 mutex_unlock(&to_gt(i915)->ggtt->vm.mutex);
422 /* Track the mmo associated with the fenced vma */
425 if (CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)
426 intel_wakeref_auto(&i915->runtime_pm.userfault_wakeref,
427 msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND));
430 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
431 i915_vma_set_ggtt_write(vma);
432 obj->mm.dirty = true;
436 i915_vma_unpin_fence(vma);
438 __i915_vma_unpin(vma);
440 intel_gt_reset_unlock(ggtt->vm.gt, srcu);
442 i915_gem_object_unpin_pages(obj);
444 if (ret == -EDEADLK) {
445 ret = i915_gem_ww_ctx_backoff(&ww);
449 i915_gem_ww_ctx_fini(&ww);
450 intel_runtime_pm_put(rpm, wakeref);
451 return i915_error_to_vmf_fault(ret);
455 vm_access(struct vm_area_struct *area, unsigned long addr,
456 void *buf, int len, int write)
458 struct i915_mmap_offset *mmo = area->vm_private_data;
459 struct drm_i915_gem_object *obj = mmo->obj;
460 struct i915_gem_ww_ctx ww;
464 if (i915_gem_object_is_readonly(obj) && write)
467 addr -= area->vm_start;
468 if (range_overflows_t(u64, addr, len, obj->base.size))
471 i915_gem_ww_ctx_init(&ww, true);
473 err = i915_gem_object_lock(obj, &ww);
477 /* As this is primarily for debugging, let's focus on simplicity */
478 vaddr = i915_gem_object_pin_map(obj, I915_MAP_FORCE_WC);
480 err = PTR_ERR(vaddr);
485 memcpy(vaddr + addr, buf, len);
486 __i915_gem_object_flush_map(obj, addr, len);
488 memcpy(buf, vaddr + addr, len);
491 i915_gem_object_unpin_map(obj);
493 if (err == -EDEADLK) {
494 err = i915_gem_ww_ctx_backoff(&ww);
498 i915_gem_ww_ctx_fini(&ww);
506 void __i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj)
508 struct i915_vma *vma;
510 GEM_BUG_ON(!obj->userfault_count);
512 for_each_ggtt_vma(vma, obj)
513 i915_vma_revoke_mmap(vma);
515 GEM_BUG_ON(obj->userfault_count);
519 * It is vital that we remove the page mapping if we have mapped a tiled
520 * object through the GTT and then lose the fence register due to
521 * resource pressure. Similarly if the object has been moved out of the
522 * aperture, than pages mapped into userspace must be revoked. Removing the
523 * mapping will then trigger a page fault on the next user access, allowing
524 * fixup by vm_fault_gtt().
526 void i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj)
528 struct drm_i915_private *i915 = to_i915(obj->base.dev);
529 intel_wakeref_t wakeref;
532 * Serialisation between user GTT access and our code depends upon
533 * revoking the CPU's PTE whilst the mutex is held. The next user
534 * pagefault then has to wait until we release the mutex.
536 * Note that RPM complicates somewhat by adding an additional
537 * requirement that operations to the GGTT be made holding the RPM
540 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
541 mutex_lock(&to_gt(i915)->ggtt->vm.mutex);
543 if (!obj->userfault_count)
546 __i915_gem_object_release_mmap_gtt(obj);
549 * Ensure that the CPU's PTE are revoked and there are not outstanding
550 * memory transactions from userspace before we return. The TLB
551 * flushing implied above by changing the PTE above *should* be
552 * sufficient, an extra barrier here just provides us with a bit
553 * of paranoid documentation about our requirement to serialise
554 * memory writes before touching registers / GSM.
559 mutex_unlock(&to_gt(i915)->ggtt->vm.mutex);
560 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
563 void i915_gem_object_runtime_pm_release_mmap_offset(struct drm_i915_gem_object *obj)
565 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
566 struct ttm_device *bdev = bo->bdev;
568 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
571 * We have exclusive access here via runtime suspend. All other callers
572 * must first grab the rpm wakeref.
574 GEM_BUG_ON(!obj->userfault_count);
575 list_del(&obj->userfault_link);
576 obj->userfault_count = 0;
579 void i915_gem_object_release_mmap_offset(struct drm_i915_gem_object *obj)
581 struct i915_mmap_offset *mmo, *mn;
583 if (obj->ops->unmap_virtual)
584 obj->ops->unmap_virtual(obj);
586 spin_lock(&obj->mmo.lock);
587 rbtree_postorder_for_each_entry_safe(mmo, mn,
588 &obj->mmo.offsets, offset) {
590 * vma_node_unmap for GTT mmaps handled already in
591 * __i915_gem_object_release_mmap_gtt
593 if (mmo->mmap_type == I915_MMAP_TYPE_GTT)
596 spin_unlock(&obj->mmo.lock);
597 drm_vma_node_unmap(&mmo->vma_node,
598 obj->base.dev->anon_inode->i_mapping);
599 spin_lock(&obj->mmo.lock);
601 spin_unlock(&obj->mmo.lock);
604 static struct i915_mmap_offset *
605 lookup_mmo(struct drm_i915_gem_object *obj,
606 enum i915_mmap_type mmap_type)
610 spin_lock(&obj->mmo.lock);
611 rb = obj->mmo.offsets.rb_node;
613 struct i915_mmap_offset *mmo =
614 rb_entry(rb, typeof(*mmo), offset);
616 if (mmo->mmap_type == mmap_type) {
617 spin_unlock(&obj->mmo.lock);
621 if (mmo->mmap_type < mmap_type)
626 spin_unlock(&obj->mmo.lock);
631 static struct i915_mmap_offset *
632 insert_mmo(struct drm_i915_gem_object *obj, struct i915_mmap_offset *mmo)
634 struct rb_node *rb, **p;
636 spin_lock(&obj->mmo.lock);
638 p = &obj->mmo.offsets.rb_node;
640 struct i915_mmap_offset *pos;
643 pos = rb_entry(rb, typeof(*pos), offset);
645 if (pos->mmap_type == mmo->mmap_type) {
646 spin_unlock(&obj->mmo.lock);
647 drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
653 if (pos->mmap_type < mmo->mmap_type)
658 rb_link_node(&mmo->offset, rb, p);
659 rb_insert_color(&mmo->offset, &obj->mmo.offsets);
660 spin_unlock(&obj->mmo.lock);
665 static struct i915_mmap_offset *
666 mmap_offset_attach(struct drm_i915_gem_object *obj,
667 enum i915_mmap_type mmap_type,
668 struct drm_file *file)
670 struct drm_i915_private *i915 = to_i915(obj->base.dev);
671 struct i915_mmap_offset *mmo;
674 GEM_BUG_ON(obj->ops->mmap_offset || obj->ops->mmap_ops);
676 mmo = lookup_mmo(obj, mmap_type);
680 mmo = kmalloc(sizeof(*mmo), GFP_KERNEL);
682 return ERR_PTR(-ENOMEM);
685 mmo->mmap_type = mmap_type;
686 drm_vma_node_reset(&mmo->vma_node);
688 err = drm_vma_offset_add(obj->base.dev->vma_offset_manager,
689 &mmo->vma_node, obj->base.size / PAGE_SIZE);
693 /* Attempt to reap some mmap space from dead objects */
694 err = intel_gt_retire_requests_timeout(to_gt(i915), MAX_SCHEDULE_TIMEOUT,
699 i915_gem_drain_freed_objects(i915);
700 err = drm_vma_offset_add(obj->base.dev->vma_offset_manager,
701 &mmo->vma_node, obj->base.size / PAGE_SIZE);
706 mmo = insert_mmo(obj, mmo);
707 GEM_BUG_ON(lookup_mmo(obj, mmap_type) != mmo);
710 drm_vma_node_allow_once(&mmo->vma_node, file);
719 __assign_mmap_offset(struct drm_i915_gem_object *obj,
720 enum i915_mmap_type mmap_type,
721 u64 *offset, struct drm_file *file)
723 struct i915_mmap_offset *mmo;
725 if (i915_gem_object_never_mmap(obj))
728 if (obj->ops->mmap_offset) {
729 if (mmap_type != I915_MMAP_TYPE_FIXED)
732 *offset = obj->ops->mmap_offset(obj);
736 if (mmap_type == I915_MMAP_TYPE_FIXED)
739 if (mmap_type != I915_MMAP_TYPE_GTT &&
740 !i915_gem_object_has_struct_page(obj) &&
741 !i915_gem_object_has_iomem(obj))
744 mmo = mmap_offset_attach(obj, mmap_type, file);
748 *offset = drm_vma_node_offset_addr(&mmo->vma_node);
753 __assign_mmap_offset_handle(struct drm_file *file,
755 enum i915_mmap_type mmap_type,
758 struct drm_i915_gem_object *obj;
761 obj = i915_gem_object_lookup(file, handle);
765 err = i915_gem_object_lock_interruptible(obj, NULL);
768 err = __assign_mmap_offset(obj, mmap_type, offset, file);
769 i915_gem_object_unlock(obj);
771 i915_gem_object_put(obj);
776 i915_gem_dumb_mmap_offset(struct drm_file *file,
777 struct drm_device *dev,
781 struct drm_i915_private *i915 = to_i915(dev);
782 enum i915_mmap_type mmap_type;
784 if (HAS_LMEM(to_i915(dev)))
785 mmap_type = I915_MMAP_TYPE_FIXED;
786 else if (pat_enabled())
787 mmap_type = I915_MMAP_TYPE_WC;
788 else if (!i915_ggtt_has_aperture(to_gt(i915)->ggtt))
791 mmap_type = I915_MMAP_TYPE_GTT;
793 return __assign_mmap_offset_handle(file, handle, mmap_type, offset);
797 * i915_gem_mmap_offset_ioctl - prepare an object for GTT mmap'ing
799 * @data: GTT mapping ioctl data
800 * @file: GEM object info
802 * Simply returns the fake offset to userspace so it can mmap it.
803 * The mmap call will end up in drm_gem_mmap(), which will set things
804 * up so we can get faults in the handler above.
806 * The fault handler will take care of binding the object into the GTT
807 * (since it may have been evicted to make room for something), allocating
808 * a fence register, and mapping the appropriate aperture address into
812 i915_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
813 struct drm_file *file)
815 struct drm_i915_private *i915 = to_i915(dev);
816 struct drm_i915_gem_mmap_offset *args = data;
817 enum i915_mmap_type type;
821 * Historically we failed to check args.pad and args.offset
822 * and so we cannot use those fields for user input and we cannot
823 * add -EINVAL for them as the ABI is fixed, i.e. old userspace
824 * may be feeding in garbage in those fields.
826 * if (args->pad) return -EINVAL; is verbotten!
829 err = i915_user_extensions(u64_to_user_ptr(args->extensions),
834 switch (args->flags) {
835 case I915_MMAP_OFFSET_GTT:
836 if (!i915_ggtt_has_aperture(to_gt(i915)->ggtt))
838 type = I915_MMAP_TYPE_GTT;
841 case I915_MMAP_OFFSET_WC:
844 type = I915_MMAP_TYPE_WC;
847 case I915_MMAP_OFFSET_WB:
848 type = I915_MMAP_TYPE_WB;
851 case I915_MMAP_OFFSET_UC:
854 type = I915_MMAP_TYPE_UC;
857 case I915_MMAP_OFFSET_FIXED:
858 type = I915_MMAP_TYPE_FIXED;
865 return __assign_mmap_offset_handle(file, args->handle, type, &args->offset);
868 static void vm_open(struct vm_area_struct *vma)
870 struct i915_mmap_offset *mmo = vma->vm_private_data;
871 struct drm_i915_gem_object *obj = mmo->obj;
874 i915_gem_object_get(obj);
877 static void vm_close(struct vm_area_struct *vma)
879 struct i915_mmap_offset *mmo = vma->vm_private_data;
880 struct drm_i915_gem_object *obj = mmo->obj;
883 i915_gem_object_put(obj);
886 static const struct vm_operations_struct vm_ops_gtt = {
887 .fault = vm_fault_gtt,
893 static const struct vm_operations_struct vm_ops_cpu = {
894 .fault = vm_fault_cpu,
900 static int singleton_release(struct inode *inode, struct file *file)
902 struct drm_i915_private *i915 = file->private_data;
904 cmpxchg(&i915->gem.mmap_singleton, file, NULL);
905 drm_dev_put(&i915->drm);
910 static const struct file_operations singleton_fops = {
911 .owner = THIS_MODULE,
912 .release = singleton_release,
915 static struct file *mmap_singleton(struct drm_i915_private *i915)
919 file = get_file_active(&i915->gem.mmap_singleton);
923 file = anon_inode_getfile("i915.gem", &singleton_fops, i915, O_RDWR);
927 /* Everyone shares a single global address space */
928 file->f_mapping = i915->drm.anon_inode->i_mapping;
930 smp_store_mb(i915->gem.mmap_singleton, file);
931 drm_dev_get(&i915->drm);
937 i915_gem_object_mmap(struct drm_i915_gem_object *obj,
938 struct i915_mmap_offset *mmo,
939 struct vm_area_struct *vma)
941 struct drm_i915_private *i915 = to_i915(obj->base.dev);
942 struct drm_device *dev = &i915->drm;
945 if (i915_gem_object_is_readonly(obj)) {
946 if (vma->vm_flags & VM_WRITE) {
947 i915_gem_object_put(obj);
950 vm_flags_clear(vma, VM_MAYWRITE);
953 anon = mmap_singleton(to_i915(dev));
955 i915_gem_object_put(obj);
956 return PTR_ERR(anon);
959 vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO);
962 * We keep the ref on mmo->obj, not vm_file, but we require
963 * vma->vm_file->f_mapping, see vma_link(), for later revocation.
964 * Our userspace is accustomed to having per-file resource cleanup
965 * (i.e. contexts, objects and requests) on their close(fd), which
966 * requires avoiding extraneous references to their filp, hence why
967 * we prefer to use an anonymous file for their mmaps.
969 vma_set_file(vma, anon);
970 /* Drop the initial creation reference, the vma is now holding one. */
973 if (obj->ops->mmap_ops) {
974 vma->vm_page_prot = pgprot_decrypted(vm_get_page_prot(vma->vm_flags));
975 vma->vm_ops = obj->ops->mmap_ops;
976 vma->vm_private_data = obj->base.vma_node.driver_private;
980 vma->vm_private_data = mmo;
982 switch (mmo->mmap_type) {
983 case I915_MMAP_TYPE_WC:
985 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
986 vma->vm_ops = &vm_ops_cpu;
989 case I915_MMAP_TYPE_FIXED:
992 case I915_MMAP_TYPE_WB:
993 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
994 vma->vm_ops = &vm_ops_cpu;
997 case I915_MMAP_TYPE_UC:
999 pgprot_noncached(vm_get_page_prot(vma->vm_flags));
1000 vma->vm_ops = &vm_ops_cpu;
1003 case I915_MMAP_TYPE_GTT:
1005 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1006 vma->vm_ops = &vm_ops_gtt;
1009 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1015 * This overcomes the limitation in drm_gem_mmap's assignment of a
1016 * drm_gem_object as the vma->vm_private_data. Since we need to
1017 * be able to resolve multiple mmap offsets which could be tied
1018 * to a single gem object.
1020 int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
1022 struct drm_vma_offset_node *node;
1023 struct drm_file *priv = filp->private_data;
1024 struct drm_device *dev = priv->minor->dev;
1025 struct drm_i915_gem_object *obj = NULL;
1026 struct i915_mmap_offset *mmo = NULL;
1028 if (drm_dev_is_unplugged(dev))
1032 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
1033 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1036 if (node && drm_vma_node_is_allowed(node, priv)) {
1038 * Skip 0-refcnted objects as it is in the process of being
1039 * destroyed and will be invalid when the vma manager lock
1042 if (!node->driver_private) {
1043 mmo = container_of(node, struct i915_mmap_offset, vma_node);
1044 obj = i915_gem_object_get_rcu(mmo->obj);
1046 GEM_BUG_ON(obj && obj->ops->mmap_ops);
1048 obj = i915_gem_object_get_rcu
1049 (container_of(node, struct drm_i915_gem_object,
1052 GEM_BUG_ON(obj && !obj->ops->mmap_ops);
1055 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
1058 return node ? -EACCES : -EINVAL;
1060 return i915_gem_object_mmap(obj, mmo, vma);
1063 int i915_gem_fb_mmap(struct drm_i915_gem_object *obj, struct vm_area_struct *vma)
1065 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1066 struct drm_device *dev = &i915->drm;
1067 struct i915_mmap_offset *mmo = NULL;
1068 enum i915_mmap_type mmap_type;
1069 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
1071 if (drm_dev_is_unplugged(dev))
1074 /* handle ttm object */
1075 if (obj->ops->mmap_ops) {
1077 * ttm fault handler, ttm_bo_vm_fault_reserved() uses fake offset
1078 * to calculate page offset so set that up.
1080 vma->vm_pgoff += drm_vma_node_start(&obj->base.vma_node);
1082 /* handle stolen and smem objects */
1083 mmap_type = i915_ggtt_has_aperture(ggtt) ? I915_MMAP_TYPE_GTT : I915_MMAP_TYPE_WC;
1084 mmo = mmap_offset_attach(obj, mmap_type, NULL);
1086 return PTR_ERR(mmo);
1090 * When we install vm_ops for mmap we are too late for
1091 * the vm_ops->open() which increases the ref_count of
1092 * this obj and then it gets decreased by the vm_ops->close().
1093 * To balance this increase the obj ref_count here.
1095 obj = i915_gem_object_get(obj);
1096 return i915_gem_object_mmap(obj, mmo, vma);
1099 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1100 #include "selftests/i915_gem_mman.c"