Skip to content

Commit

Permalink
drm/i915: Initial introduction of vma resources
Browse files Browse the repository at this point in the history
Introduce vma resources, sort of similar to TTM resources,  needed for
asynchronous bind management. Initially we will use them to hold
completion of unbinding when we capture data from a vma, but they will
be used extensively in upcoming patches for asynchronous vma unbinding.

v6:
- Some documentation updates

Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20220110172219.107131-2-thomas.hellstrom@linux.intel.com
  • Loading branch information
Thomas Hellström committed Jan 11, 2022
1 parent ab4911b commit e1a4bbb
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 63 deletions.
1 change: 1 addition & 0 deletions drivers/gpu/drm/i915/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ i915-y += \
i915_trace_points.o \
i915_ttm_buddy_manager.o \
i915_vma.o \
i915_vma_resource.o \
i915_vma_snapshot.o \
intel_wopcm.o

Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ eb_relocate_entry(struct i915_execbuffer *eb,
mutex_lock(&vma->vm->mutex);
err = i915_vma_bind(target->vma,
target->vma->obj->cache_level,
PIN_GLOBAL, NULL);
PIN_GLOBAL, NULL, NULL);
mutex_unlock(&vma->vm->mutex);
reloc_cache_remap(&eb->reloc_cache, ev->vma->obj);
if (err)
Expand Down
55 changes: 49 additions & 6 deletions drivers/gpu/drm/i915/i915_vma.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "i915_sw_fence_work.h"
#include "i915_trace.h"
#include "i915_vma.h"
#include "i915_vma_resource.h"

static struct kmem_cache *slab_vmas;

Expand Down Expand Up @@ -380,6 +381,8 @@ static int i915_vma_verify_bind_complete(struct i915_vma *vma)
* @cache_level: mapping cache level
* @flags: flags like global or local mapping
* @work: preallocated worker for allocating and binding the PTE
* @vma_res: pointer to a preallocated vma resource. The resource is either
* consumed or freed.
*
* DMA addresses are taken from the scatter-gather table of this object (or of
* this VMA in case of non-default GGTT views) and PTE entries set up.
Expand All @@ -388,7 +391,8 @@ static int i915_vma_verify_bind_complete(struct i915_vma *vma)
int i915_vma_bind(struct i915_vma *vma,
enum i915_cache_level cache_level,
u32 flags,
struct i915_vma_work *work)
struct i915_vma_work *work,
struct i915_vma_resource *vma_res)
{
u32 bind_flags;
u32 vma_flags;
Expand All @@ -399,11 +403,15 @@ int i915_vma_bind(struct i915_vma *vma,

if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
vma->node.size,
vma->vm->total)))
vma->vm->total))) {
kfree(vma_res);
return -ENODEV;
}

if (GEM_DEBUG_WARN_ON(!flags))
if (GEM_DEBUG_WARN_ON(!flags)) {
kfree(vma_res);
return -EINVAL;
}

bind_flags = flags;
bind_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Expand All @@ -412,11 +420,21 @@ int i915_vma_bind(struct i915_vma *vma,
vma_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;

bind_flags &= ~vma_flags;
if (bind_flags == 0)
if (bind_flags == 0) {
kfree(vma_res);
return 0;
}

GEM_BUG_ON(!atomic_read(&vma->pages_count));

if (vma->resource || !vma_res) {
/* Rebinding with an additional I915_VMA_*_BIND */
GEM_WARN_ON(!vma_flags);
kfree(vma_res);
} else {
i915_vma_resource_init(vma_res);
vma->resource = vma_res;
}
trace_i915_vma_bind(vma, bind_flags);
if (work && bind_flags & vma->vm->bind_async_flags) {
struct dma_fence *prev;
Expand Down Expand Up @@ -1224,6 +1242,7 @@ int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
{
struct i915_vma_work *work = NULL;
struct dma_fence *moving = NULL;
struct i915_vma_resource *vma_res = NULL;
intel_wakeref_t wakeref = 0;
unsigned int bound;
int err;
Expand Down Expand Up @@ -1278,6 +1297,12 @@ int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
}
}

vma_res = i915_vma_resource_alloc();
if (IS_ERR(vma_res)) {
err = PTR_ERR(vma_res);
goto err_fence;
}

/*
* Differentiate between user/kernel vma inside the aliasing-ppgtt.
*
Expand All @@ -1298,7 +1323,7 @@ int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
err = mutex_lock_interruptible_nested(&vma->vm->mutex,
!(flags & PIN_GLOBAL));
if (err)
goto err_fence;
goto err_vma_res;

/* No more allocations allowed now we hold vm->mutex */

Expand Down Expand Up @@ -1339,7 +1364,8 @@ int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
GEM_BUG_ON(!vma->pages);
err = i915_vma_bind(vma,
vma->obj->cache_level,
flags, work);
flags, work, vma_res);
vma_res = NULL;
if (err)
goto err_remove;

Expand All @@ -1362,6 +1388,8 @@ int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
i915_active_release(&vma->active);
err_unlock:
mutex_unlock(&vma->vm->mutex);
err_vma_res:
kfree(vma_res);
err_fence:
if (work)
dma_fence_work_commit_imm(&work->base);
Expand Down Expand Up @@ -1512,6 +1540,7 @@ void i915_vma_release(struct kref *ref)
i915_vm_put(vma->vm);

i915_active_fini(&vma->active);
GEM_WARN_ON(vma->resource);
i915_vma_free(vma);
}

Expand Down Expand Up @@ -1660,6 +1689,8 @@ int _i915_vma_move_to_active(struct i915_vma *vma,

void __i915_vma_evict(struct i915_vma *vma)
{
struct dma_fence *unbind_fence;

GEM_BUG_ON(i915_vma_is_pinned(vma));

if (i915_vma_is_map_and_fenceable(vma)) {
Expand Down Expand Up @@ -1697,8 +1728,20 @@ void __i915_vma_evict(struct i915_vma *vma)
atomic_and(~(I915_VMA_BIND_MASK | I915_VMA_ERROR | I915_VMA_GGTT_WRITE),
&vma->flags);

unbind_fence = i915_vma_resource_unbind(vma->resource);
i915_vma_resource_put(vma->resource);
vma->resource = NULL;

i915_vma_detach(vma);
vma_unbind_pages(vma);

/*
* This uninterruptible wait under the vm mutex is currently
* only ever blocking while the vma is being captured from.
* With async unbinding, this wait here will be removed.
*/
dma_fence_wait(unbind_fence, false);
dma_fence_put(unbind_fence);
}

int __i915_vma_unbind(struct i915_vma *vma)
Expand Down
19 changes: 18 additions & 1 deletion drivers/gpu/drm/i915/i915_vma.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "i915_active.h"
#include "i915_request.h"
#include "i915_vma_resource.h"
#include "i915_vma_types.h"

struct i915_vma *
Expand Down Expand Up @@ -204,7 +205,8 @@ struct i915_vma_work *i915_vma_work(void);
int i915_vma_bind(struct i915_vma *vma,
enum i915_cache_level cache_level,
u32 flags,
struct i915_vma_work *work);
struct i915_vma_work *work,
struct i915_vma_resource *vma_res);

bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color);
bool i915_vma_misplaced(const struct i915_vma *vma,
Expand Down Expand Up @@ -428,6 +430,21 @@ static inline int i915_vma_sync(struct i915_vma *vma)
return i915_active_wait(&vma->active);
}

/**
* i915_vma_get_current_resource - Get the current resource of the vma
* @vma: The vma to get the current resource from.
*
* It's illegal to call this function if the vma is not bound.
*
* Return: A refcounted pointer to the current vma resource
* of the vma, assuming the vma is bound.
*/
static inline struct i915_vma_resource *
i915_vma_get_current_resource(struct i915_vma *vma)
{
return i915_vma_resource_get(vma->resource);
}

void i915_vma_module_exit(void);
int i915_vma_module_init(void);

Expand Down
124 changes: 124 additions & 0 deletions drivers/gpu/drm/i915/i915_vma_resource.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// SPDX-License-Identifier: MIT
/*
* Copyright © 2021 Intel Corporation
*/
#include <linux/slab.h>

#include "i915_vma_resource.h"

/* Callbacks for the unbind dma-fence. */
static const char *get_driver_name(struct dma_fence *fence)
{
return "vma unbind fence";
}

static const char *get_timeline_name(struct dma_fence *fence)
{
return "unbound";
}

static struct dma_fence_ops unbind_fence_ops = {
.get_driver_name = get_driver_name,
.get_timeline_name = get_timeline_name,
};

/**
* i915_vma_resource_init - Initialize a vma resource.
* @vma_res: The vma resource to initialize
*
* Initializes a vma resource allocated using i915_vma_resource_alloc().
* The reason for having separate allocate and initialize function is that
* initialization may need to be performed from under a lock where
* allocation is not allowed.
*/
void i915_vma_resource_init(struct i915_vma_resource *vma_res)
{
spin_lock_init(&vma_res->lock);
dma_fence_init(&vma_res->unbind_fence, &unbind_fence_ops,
&vma_res->lock, 0, 0);
refcount_set(&vma_res->hold_count, 1);
}

/**
* i915_vma_resource_alloc - Allocate a vma resource
*
* Return: A pointer to a cleared struct i915_vma_resource or
* a -ENOMEM error pointer if allocation fails.
*/
struct i915_vma_resource *i915_vma_resource_alloc(void)
{
struct i915_vma_resource *vma_res =
kzalloc(sizeof(*vma_res), GFP_KERNEL);

return vma_res ? vma_res : ERR_PTR(-ENOMEM);
}

static void __i915_vma_resource_unhold(struct i915_vma_resource *vma_res)
{
if (refcount_dec_and_test(&vma_res->hold_count))
dma_fence_signal(&vma_res->unbind_fence);
}

/**
* i915_vma_resource_unhold - Unhold the signaling of the vma resource unbind
* fence.
* @vma_res: The vma resource.
* @lockdep_cookie: The lockdep cookie returned from i915_vma_resource_hold.
*
* The function may leave a dma_fence critical section.
*/
void i915_vma_resource_unhold(struct i915_vma_resource *vma_res,
bool lockdep_cookie)
{
dma_fence_end_signalling(lockdep_cookie);

if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
unsigned long irq_flags;

/* Inefficient open-coded might_lock_irqsave() */
spin_lock_irqsave(&vma_res->lock, irq_flags);
spin_unlock_irqrestore(&vma_res->lock, irq_flags);
}

__i915_vma_resource_unhold(vma_res);
}

/**
* i915_vma_resource_hold - Hold the signaling of the vma resource unbind fence.
* @vma_res: The vma resource.
* @lockdep_cookie: Pointer to a bool serving as a lockdep cooke that should
* be given as an argument to the pairing i915_vma_resource_unhold.
*
* If returning true, the function enters a dma_fence signalling critical
* section if not in one already.
*
* Return: true if holding successful, false if not.
*/
bool i915_vma_resource_hold(struct i915_vma_resource *vma_res,
bool *lockdep_cookie)
{
bool held = refcount_inc_not_zero(&vma_res->hold_count);

if (held)
*lockdep_cookie = dma_fence_begin_signalling();

return held;
}

/**
* i915_vma_resource_unbind - Unbind a vma resource
* @vma_res: The vma resource to unbind.
*
* At this point this function does little more than publish a fence that
* signals immediately unless signaling is held back.
*
* Return: A refcounted pointer to a dma-fence that signals when unbinding is
* complete.
*/
struct dma_fence *
i915_vma_resource_unbind(struct i915_vma_resource *vma_res)
{
__i915_vma_resource_unhold(vma_res);
dma_fence_get(&vma_res->unbind_fence);
return &vma_res->unbind_fence;
}
Loading

0 comments on commit e1a4bbb

Please sign in to comment.