2019-05-28 10:29:47 +01:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* Copyright © 2014-2016 Intel Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mman.h>
|
|
|
|
#include <linux/sizes.h>
|
|
|
|
|
2019-07-12 20:29:53 +01:00
|
|
|
#include "gt/intel_gt.h"
|
2019-10-04 14:40:06 +01:00
|
|
|
#include "gt/intel_gt_requests.h"
|
2019-07-12 20:29:53 +01:00
|
|
|
|
2019-05-28 10:29:47 +01:00
|
|
|
#include "i915_drv.h"
|
|
|
|
#include "i915_gem_gtt.h"
|
|
|
|
#include "i915_gem_ioctls.h"
|
|
|
|
#include "i915_gem_object.h"
|
2019-08-06 13:07:28 +03:00
|
|
|
#include "i915_trace.h"
|
2019-05-28 10:29:47 +01:00
|
|
|
#include "i915_vma.h"
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
__vma_matches(struct vm_area_struct *vma, struct file *filp,
|
|
|
|
unsigned long addr, unsigned long size)
|
|
|
|
{
|
|
|
|
if (vma->vm_file != filp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return vma->vm_start == addr &&
|
|
|
|
(vma->vm_end - vma->vm_start) == PAGE_ALIGN(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
|
|
|
|
* it is mapped to.
|
|
|
|
* @dev: drm device
|
|
|
|
* @data: ioctl data blob
|
|
|
|
* @file: drm file
|
|
|
|
*
|
|
|
|
* While the mapping holds a reference on the contents of the object, it doesn't
|
|
|
|
* imply a ref on the object itself.
|
|
|
|
*
|
|
|
|
* IMPORTANT:
|
|
|
|
*
|
|
|
|
* DRM driver writers who look a this function as an example for how to do GEM
|
|
|
|
* mmap support, please don't implement mmap support like here. The modern way
|
|
|
|
* to implement DRM mmap support is with an mmap offset ioctl (like
|
|
|
|
* i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
|
|
|
|
* That way debug tooling like valgrind will understand what's going on, hiding
|
|
|
|
* the mmap call in a driver private ioctl will break that. The i915 driver only
|
|
|
|
* does cpu mmaps this way because we didn't know better.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
|
|
|
|
struct drm_file *file)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_mmap *args = data;
|
|
|
|
struct drm_i915_gem_object *obj;
|
|
|
|
unsigned long addr;
|
|
|
|
|
|
|
|
if (args->flags & ~(I915_MMAP_WC))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
obj = i915_gem_object_lookup(file, args->handle);
|
|
|
|
if (!obj)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
/* prime objects have no backing filp to GEM mmap
|
|
|
|
* pages from.
|
|
|
|
*/
|
|
|
|
if (!obj->base.filp) {
|
|
|
|
addr = -ENXIO;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range_overflows(args->offset, args->size, (u64)obj->base.size)) {
|
|
|
|
addr = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = vm_mmap(obj->base.filp, 0, args->size,
|
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
|
|
args->offset);
|
|
|
|
if (IS_ERR_VALUE(addr))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (args->flags & I915_MMAP_WC) {
|
|
|
|
struct mm_struct *mm = current->mm;
|
|
|
|
struct vm_area_struct *vma;
|
|
|
|
|
|
|
|
if (down_write_killable(&mm->mmap_sem)) {
|
|
|
|
addr = -EINTR;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
vma = find_vma(mm, addr);
|
|
|
|
if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
|
|
|
|
vma->vm_page_prot =
|
|
|
|
pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
|
|
|
|
else
|
|
|
|
addr = -ENOMEM;
|
|
|
|
up_write(&mm->mmap_sem);
|
|
|
|
if (IS_ERR_VALUE(addr))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
i915_gem_object_put(obj);
|
|
|
|
|
|
|
|
args->addr_ptr = (u64)addr;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
i915_gem_object_put(obj);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
|
|
|
|
{
|
|
|
|
return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
|
|
|
|
*
|
|
|
|
* A history of the GTT mmap interface:
|
|
|
|
*
|
|
|
|
* 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
|
|
|
|
* aligned and suitable for fencing, and still fit into the available
|
|
|
|
* mappable space left by the pinned display objects. A classic problem
|
|
|
|
* we called the page-fault-of-doom where we would ping-pong between
|
|
|
|
* two objects that could not fit inside the GTT and so the memcpy
|
|
|
|
* would page one object in at the expense of the other between every
|
|
|
|
* single byte.
|
|
|
|
*
|
|
|
|
* 1 - Objects can be any size, and have any compatible fencing (X Y, or none
|
|
|
|
* as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
|
|
|
|
* object is too large for the available space (or simply too large
|
|
|
|
* for the mappable aperture!), a view is created instead and faulted
|
|
|
|
* into userspace. (This view is aligned and sized appropriately for
|
|
|
|
* fenced access.)
|
|
|
|
*
|
|
|
|
* 2 - Recognise WC as a separate cache domain so that we can flush the
|
|
|
|
* delayed writes via GTT before performing direct access via WC.
|
|
|
|
*
|
|
|
|
* 3 - Remove implicit set-domain(GTT) and synchronisation on initial
|
|
|
|
* pagefault; swapin remains transparent.
|
|
|
|
*
|
|
|
|
* Restrictions:
|
|
|
|
*
|
|
|
|
* * snoopable objects cannot be accessed via the GTT. It can cause machine
|
|
|
|
* hangs on some architectures, corruption on others. An attempt to service
|
|
|
|
* a GTT page fault from a snoopable object will generate a SIGBUS.
|
|
|
|
*
|
|
|
|
* * the object must be able to fit into RAM (physical memory, though no
|
|
|
|
* limited to the mappable aperture).
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Caveats:
|
|
|
|
*
|
|
|
|
* * a new GTT page fault will synchronize rendering from the GPU and flush
|
|
|
|
* all data to system memory. Subsequent access will not be synchronized.
|
|
|
|
*
|
|
|
|
* * all mappings are revoked on runtime device suspend.
|
|
|
|
*
|
|
|
|
* * there are only 8, 16 or 32 fence registers to share between all users
|
|
|
|
* (older machines require fence register for display and blitter access
|
|
|
|
* as well). Contention of the fence registers will cause the previous users
|
|
|
|
* to be unmapped and any new access will generate new page faults.
|
|
|
|
*
|
|
|
|
* * running out of memory while servicing a fault may generate a SIGBUS,
|
|
|
|
* rather than the expected SIGSEGV.
|
|
|
|
*/
|
|
|
|
int i915_gem_mmap_gtt_version(void)
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct i915_ggtt_view
|
|
|
|
compute_partial_view(const struct drm_i915_gem_object *obj,
|
|
|
|
pgoff_t page_offset,
|
|
|
|
unsigned int chunk)
|
|
|
|
{
|
|
|
|
struct i915_ggtt_view view;
|
|
|
|
|
|
|
|
if (i915_gem_object_is_tiled(obj))
|
|
|
|
chunk = roundup(chunk, tile_row_pages(obj));
|
|
|
|
|
|
|
|
view.type = I915_GGTT_VIEW_PARTIAL;
|
|
|
|
view.partial.offset = rounddown(page_offset, chunk);
|
|
|
|
view.partial.size =
|
|
|
|
min_t(unsigned int, chunk,
|
|
|
|
(obj->base.size >> PAGE_SHIFT) - view.partial.offset);
|
|
|
|
|
|
|
|
/* If the partial covers the entire object, just create a normal VMA. */
|
|
|
|
if (chunk >= obj->base.size >> PAGE_SHIFT)
|
|
|
|
view.type = I915_GGTT_VIEW_NORMAL;
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i915_gem_fault - fault a page into the GTT
|
|
|
|
* @vmf: fault info
|
|
|
|
*
|
|
|
|
* The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
|
|
|
|
* from userspace. The fault handler takes care of binding the object to
|
|
|
|
* the GTT (if needed), allocating and programming a fence register (again,
|
|
|
|
* only if needed based on whether the old reg is still valid or the object
|
|
|
|
* is tiled) and inserting a new PTE into the faulting process.
|
|
|
|
*
|
|
|
|
* Note that the faulting process may involve evicting existing objects
|
|
|
|
* from the GTT and/or fence registers to make room. So performance may
|
|
|
|
* suffer if the GTT working set is large or there are few fence registers
|
|
|
|
* left.
|
|
|
|
*
|
|
|
|
* The current feature set supported by i915_gem_fault() and thus GTT mmaps
|
|
|
|
* is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
|
|
|
|
*/
|
|
|
|
vm_fault_t i915_gem_fault(struct vm_fault *vmf)
|
|
|
|
{
|
|
|
|
#define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
|
|
|
|
struct vm_area_struct *area = vmf->vma;
|
|
|
|
struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
|
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
struct drm_i915_private *i915 = to_i915(dev);
|
2019-06-13 16:21:54 -07:00
|
|
|
struct intel_runtime_pm *rpm = &i915->runtime_pm;
|
2019-05-28 10:29:47 +01:00
|
|
|
struct i915_ggtt *ggtt = &i915->ggtt;
|
|
|
|
bool write = area->vm_flags & VM_WRITE;
|
|
|
|
intel_wakeref_t wakeref;
|
|
|
|
struct i915_vma *vma;
|
|
|
|
pgoff_t page_offset;
|
|
|
|
int srcu;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check that we allow writing into this object */
|
|
|
|
if (i915_gem_object_is_readonly(obj) && write)
|
|
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
|
|
|
|
/* We don't use vmf->pgoff since that has the fake offset */
|
|
|
|
page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
|
|
|
|
|
|
|
|
trace_i915_gem_object_fault(obj, page_offset, true, write);
|
|
|
|
|
|
|
|
ret = i915_gem_object_pin_pages(obj);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2019-06-13 16:21:54 -07:00
|
|
|
wakeref = intel_runtime_pm_get(rpm);
|
2019-05-28 10:29:47 +01:00
|
|
|
|
2019-09-12 17:08:34 +01:00
|
|
|
ret = intel_gt_reset_trylock(ggtt->vm.gt, &srcu);
|
|
|
|
if (ret)
|
2019-05-28 10:29:47 +01:00
|
|
|
goto err_rpm;
|
|
|
|
|
|
|
|
/* Now pin it into the GTT as needed */
|
|
|
|
vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
|
|
|
|
PIN_MAPPABLE |
|
2019-08-21 13:32:34 +01:00
|
|
|
PIN_NONBLOCK /* NOWARN */ |
|
2019-08-26 14:07:50 +01:00
|
|
|
PIN_NOEVICT);
|
2019-05-28 10:29:47 +01:00
|
|
|
if (IS_ERR(vma)) {
|
|
|
|
/* Use a partial view if it is bigger than available space */
|
|
|
|
struct i915_ggtt_view view =
|
|
|
|
compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
|
|
|
|
unsigned int flags;
|
|
|
|
|
2019-08-21 13:32:34 +01:00
|
|
|
flags = PIN_MAPPABLE | PIN_NOSEARCH;
|
2019-05-28 10:29:47 +01:00
|
|
|
if (view.type == I915_GGTT_VIEW_NORMAL)
|
|
|
|
flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Userspace is now writing through an untracked VMA, abandon
|
|
|
|
* all hope that the hardware is able to track future writes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
|
2019-08-21 13:32:34 +01:00
|
|
|
if (IS_ERR(vma)) {
|
2019-05-28 10:29:47 +01:00
|
|
|
flags = PIN_MAPPABLE;
|
|
|
|
view.type = I915_GGTT_VIEW_PARTIAL;
|
|
|
|
vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
|
|
|
|
}
|
2019-09-02 05:02:46 +01:00
|
|
|
|
|
|
|
/* The entire mappable GGTT is pinned? Unexpected! */
|
|
|
|
GEM_BUG_ON(vma == ERR_PTR(-ENOSPC));
|
2019-05-28 10:29:47 +01:00
|
|
|
}
|
|
|
|
if (IS_ERR(vma)) {
|
|
|
|
ret = PTR_ERR(vma);
|
drm/i915: Pull i915_vma_pin under the vm->mutex
Replace the struct_mutex requirement for pinning the i915_vma with the
local vm->mutex instead. Note that the vm->mutex is tainted by the
shrinker (we require unbinding from inside fs-reclaim) and so we cannot
allocate while holding that mutex. Instead we have to preallocate
workers to do allocate and apply the PTE updates after we have we
reserved their slot in the drm_mm (using fences to order the PTE writes
with the GPU work and with later unbind).
In adding the asynchronous vma binding, one subtle requirement is to
avoid coupling the binding fence into the backing object->resv. That is
the asynchronous binding only applies to the vma timeline itself and not
to the pages as that is a more global timeline (the binding of one vma
does not need to be ordered with another vma, nor does the implicit GEM
fencing depend on a vma, only on writes to the backing store). Keeping
the vma binding distinct from the backing store timelines is verified by
a number of async gem_exec_fence and gem_exec_schedule tests. The way we
do this is quite simple, we keep the fence for the vma binding separate
and only wait on it as required, and never add it to the obj->resv
itself.
Another consequence in reducing the locking around the vma is the
destruction of the vma is no longer globally serialised by struct_mutex.
A natural solution would be to add a kref to i915_vma, but that requires
decoupling the reference cycles, possibly by introducing a new
i915_mm_pages object that is own by both obj->mm and vma->pages.
However, we have not taken that route due to the overshadowing lmem/ttm
discussions, and instead play a series of complicated games with
trylocks to (hopefully) ensure that only one destruction path is called!
v2: Add some commentary, and some helpers to reduce patch churn.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20191004134015.13204-4-chris@chris-wilson.co.uk
2019-10-04 14:39:58 +01:00
|
|
|
goto err_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Access to snoopable pages through the GTT is incoherent. */
|
|
|
|
if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(i915)) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto err_unpin;
|
2019-05-28 10:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = i915_vma_pin_fence(vma);
|
|
|
|
if (ret)
|
|
|
|
goto err_unpin;
|
|
|
|
|
|
|
|
/* Finally, remap it using the new GTT offset */
|
|
|
|
ret = remap_io_mapping(area,
|
|
|
|
area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
|
|
|
|
(ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
|
|
|
|
min_t(u64, vma->size, area->vm_end - area->vm_start),
|
|
|
|
&ggtt->iomap);
|
|
|
|
if (ret)
|
|
|
|
goto err_fence;
|
|
|
|
|
2019-06-13 16:21:54 -07:00
|
|
|
assert_rpm_wakelock_held(rpm);
|
2019-08-22 07:09:13 +01:00
|
|
|
|
|
|
|
/* Mark as being mmapped into userspace for later revocation */
|
|
|
|
mutex_lock(&i915->ggtt.vm.mutex);
|
2019-05-28 10:29:47 +01:00
|
|
|
if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
|
2019-06-13 08:32:54 +01:00
|
|
|
list_add(&obj->userfault_link, &i915->ggtt.userfault_list);
|
2019-08-22 07:09:13 +01:00
|
|
|
mutex_unlock(&i915->ggtt.vm.mutex);
|
|
|
|
|
2019-10-25 14:59:42 +01:00
|
|
|
if (IS_ACTIVE(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND))
|
2019-06-13 08:32:54 +01:00
|
|
|
intel_wakeref_auto(&i915->ggtt.userfault_wakeref,
|
2019-05-28 10:29:47 +01:00
|
|
|
msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND));
|
|
|
|
|
2019-09-20 13:18:21 +01:00
|
|
|
if (write) {
|
|
|
|
GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
|
|
|
|
i915_vma_set_ggtt_write(vma);
|
|
|
|
obj->mm.dirty = true;
|
|
|
|
}
|
2019-05-28 10:29:47 +01:00
|
|
|
|
|
|
|
err_fence:
|
|
|
|
i915_vma_unpin_fence(vma);
|
|
|
|
err_unpin:
|
|
|
|
__i915_vma_unpin(vma);
|
|
|
|
err_reset:
|
2019-07-12 20:29:53 +01:00
|
|
|
intel_gt_reset_unlock(ggtt->vm.gt, srcu);
|
2019-05-28 10:29:47 +01:00
|
|
|
err_rpm:
|
2019-06-13 16:21:54 -07:00
|
|
|
intel_runtime_pm_put(rpm, wakeref);
|
2019-05-28 10:29:47 +01:00
|
|
|
i915_gem_object_unpin_pages(obj);
|
|
|
|
err:
|
|
|
|
switch (ret) {
|
2019-09-02 05:02:46 +01:00
|
|
|
default:
|
|
|
|
WARN_ONCE(ret, "unhandled error in %s: %i\n", __func__, ret);
|
|
|
|
/* fallthrough */
|
|
|
|
case -EIO: /* shmemfs failure from swap device */
|
|
|
|
case -EFAULT: /* purged object */
|
2019-09-28 09:25:46 +01:00
|
|
|
case -ENODEV: /* bad object, how did you get here! */
|
2019-09-02 05:02:46 +01:00
|
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
|
|
|
|
case -ENOSPC: /* shmemfs allocation failure */
|
|
|
|
case -ENOMEM: /* our allocation failure */
|
|
|
|
return VM_FAULT_OOM;
|
|
|
|
|
2019-05-28 10:29:47 +01:00
|
|
|
case 0:
|
2019-09-02 05:02:46 +01:00
|
|
|
case -EAGAIN:
|
2019-05-28 10:29:47 +01:00
|
|
|
case -ERESTARTSYS:
|
|
|
|
case -EINTR:
|
|
|
|
case -EBUSY:
|
|
|
|
/*
|
|
|
|
* EBUSY is ok: this just means that another thread
|
|
|
|
* already did the job.
|
|
|
|
*/
|
|
|
|
return VM_FAULT_NOPAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct i915_vma *vma;
|
|
|
|
|
|
|
|
GEM_BUG_ON(!obj->userfault_count);
|
|
|
|
|
|
|
|
obj->userfault_count = 0;
|
|
|
|
list_del(&obj->userfault_link);
|
|
|
|
drm_vma_node_unmap(&obj->base.vma_node,
|
|
|
|
obj->base.dev->anon_inode->i_mapping);
|
|
|
|
|
|
|
|
for_each_ggtt_vma(vma, obj)
|
|
|
|
i915_vma_unset_userfault(vma);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i915_gem_object_release_mmap - remove physical page mappings
|
|
|
|
* @obj: obj in question
|
|
|
|
*
|
|
|
|
* Preserve the reservation of the mmapping with the DRM core code, but
|
|
|
|
* relinquish ownership of the pages back to the system.
|
|
|
|
*
|
|
|
|
* It is vital that we remove the page mapping if we have mapped a tiled
|
|
|
|
* object through the GTT and then lose the fence register due to
|
|
|
|
* resource pressure. Similarly if the object has been moved out of the
|
|
|
|
* aperture, than pages mapped into userspace must be revoked. Removing the
|
|
|
|
* mapping will then trigger a page fault on the next user access, allowing
|
|
|
|
* fixup by i915_gem_fault().
|
|
|
|
*/
|
|
|
|
void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct drm_i915_private *i915 = to_i915(obj->base.dev);
|
|
|
|
intel_wakeref_t wakeref;
|
|
|
|
|
|
|
|
/* Serialisation between user GTT access and our code depends upon
|
|
|
|
* revoking the CPU's PTE whilst the mutex is held. The next user
|
|
|
|
* pagefault then has to wait until we release the mutex.
|
|
|
|
*
|
|
|
|
* Note that RPM complicates somewhat by adding an additional
|
|
|
|
* requirement that operations to the GGTT be made holding the RPM
|
|
|
|
* wakeref.
|
|
|
|
*/
|
2019-06-13 16:21:54 -07:00
|
|
|
wakeref = intel_runtime_pm_get(&i915->runtime_pm);
|
2019-08-22 07:09:13 +01:00
|
|
|
mutex_lock(&i915->ggtt.vm.mutex);
|
2019-05-28 10:29:47 +01:00
|
|
|
|
|
|
|
if (!obj->userfault_count)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
__i915_gem_object_release_mmap(obj);
|
|
|
|
|
|
|
|
/* Ensure that the CPU's PTE are revoked and there are not outstanding
|
|
|
|
* memory transactions from userspace before we return. The TLB
|
|
|
|
* flushing implied above by changing the PTE above *should* be
|
|
|
|
* sufficient, an extra barrier here just provides us with a bit
|
|
|
|
* of paranoid documentation about our requirement to serialise
|
|
|
|
* memory writes before touching registers / GSM.
|
|
|
|
*/
|
|
|
|
wmb();
|
|
|
|
|
|
|
|
out:
|
2019-08-22 07:09:13 +01:00
|
|
|
mutex_unlock(&i915->ggtt.vm.mutex);
|
2019-06-13 16:21:54 -07:00
|
|
|
intel_runtime_pm_put(&i915->runtime_pm, wakeref);
|
2019-05-28 10:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int create_mmap_offset(struct drm_i915_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct drm_i915_private *i915 = to_i915(obj->base.dev);
|
2019-10-04 14:40:06 +01:00
|
|
|
struct intel_gt *gt = &i915->gt;
|
2019-05-28 10:29:47 +01:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = drm_gem_create_mmap_offset(&obj->base);
|
|
|
|
if (likely(!err))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Attempt to reap some mmap space from dead objects */
|
2019-10-04 14:40:06 +01:00
|
|
|
err = intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT);
|
2019-10-04 14:40:05 +01:00
|
|
|
if (err)
|
|
|
|
return err;
|
2019-05-28 10:29:47 +01:00
|
|
|
|
2019-10-04 14:40:05 +01:00
|
|
|
i915_gem_drain_freed_objects(i915);
|
|
|
|
return drm_gem_create_mmap_offset(&obj->base);
|
2019-05-28 10:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
i915_gem_mmap_gtt(struct drm_file *file,
|
|
|
|
struct drm_device *dev,
|
|
|
|
u32 handle,
|
|
|
|
u64 *offset)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
2019-11-05 14:53:05 +00:00
|
|
|
if (!i915_ggtt_has_aperture(&to_i915(dev)->ggtt))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2019-05-28 10:29:47 +01:00
|
|
|
obj = i915_gem_object_lookup(file, handle);
|
|
|
|
if (!obj)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2019-09-28 09:25:46 +01:00
|
|
|
if (i915_gem_object_never_bind_ggtt(obj)) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-05-28 10:29:47 +01:00
|
|
|
ret = create_mmap_offset(obj);
|
|
|
|
if (ret == 0)
|
|
|
|
*offset = drm_vma_node_offset_addr(&obj->base.vma_node);
|
|
|
|
|
2019-09-28 09:25:46 +01:00
|
|
|
out:
|
2019-05-28 10:29:47 +01:00
|
|
|
i915_gem_object_put(obj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
|
|
|
|
* @dev: DRM device
|
|
|
|
* @data: GTT mapping ioctl data
|
|
|
|
* @file: GEM object info
|
|
|
|
*
|
|
|
|
* Simply returns the fake offset to userspace so it can mmap it.
|
|
|
|
* The mmap call will end up in drm_gem_mmap(), which will set things
|
|
|
|
* up so we can get faults in the handler above.
|
|
|
|
*
|
|
|
|
* The fault handler will take care of binding the object into the GTT
|
|
|
|
* (since it may have been evicted to make room for something), allocating
|
|
|
|
* a fence register, and mapping the appropriate aperture address into
|
|
|
|
* userspace.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
|
|
|
|
struct drm_file *file)
|
|
|
|
{
|
|
|
|
struct drm_i915_gem_mmap_gtt *args = data;
|
|
|
|
|
|
|
|
return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
|
|
|
|
#include "selftests/i915_gem_mman.c"
|
|
|
|
#endif
|