Module Name: src
Committed By: riastradh
Date: Thu Aug 23 01:06:51 UTC 2018
Modified Files:
src/sys/external/bsd/drm2/dist/drm/nouveau: nouveau_fence.c
nouveau_fence.h nouveau_nv84_fence.c
Log Message:
Rewrite nouveau_fence in an attempt to make it make sense.
PR kern/53441
XXX pullup-7
XXX pullup-8
To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 \
src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.c
cvs rdiff -u -r1.2 -r1.3 \
src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.h \
src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_nv84_fence.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.c
diff -u src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.c:1.4 src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.c:1.5
--- src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.c:1.4 Wed Apr 13 07:57:15 2016
+++ src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.c Thu Aug 23 01:06:50 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: nouveau_fence.c,v 1.4 2016/04/13 07:57:15 riastradh Exp $ */
+/* $NetBSD: nouveau_fence.c,v 1.5 2018/08/23 01:06:50 riastradh Exp $ */
/*
* Copyright (C) 2007 Ben Skeggs.
@@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nouveau_fence.c,v 1.4 2016/04/13 07:57:15 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nouveau_fence.c,v 1.5 2018/08/23 01:06:50 riastradh Exp $");
#include <drm/drmP.h>
@@ -41,6 +41,12 @@ __KERNEL_RCSID(0, "$NetBSD: nouveau_fenc
#include <engine/fifo.h>
+/*
+ * struct fence_work
+ *
+ * State for a work action scheduled when a fence is completed.
+ * Will call func(data) at some point after that happens.
+ */
struct fence_work {
struct work_struct base;
struct list_head head;
@@ -48,77 +54,119 @@ struct fence_work {
void *data;
};
+/*
+ * nouveau_fence_signal(fence)
+ *
+ * Schedule all the work for fence's completion, and mark it done.
+ *
+ * Caller must hold fence's channel's fence lock.
+ */
static void
nouveau_fence_signal(struct nouveau_fence *fence)
{
+ struct nouveau_channel *chan __diagused = fence->channel;
+ struct nouveau_fence_chan *fctx __diagused = chan->fence;
struct fence_work *work, *temp;
+ BUG_ON(!spin_is_locked(&fctx->lock));
+ BUG_ON(fence->done);
+
+ /* Schedule all the work for this fence. */
list_for_each_entry_safe(work, temp, &fence->work, head) {
schedule_work(&work->base);
list_del(&work->head);
}
- fence->channel = NULL;
+ /* Note that the fence is done. */
+ fence->done = true;
list_del(&fence->head);
}
+/*
+ * nouveau_fence_context_del(fctx)
+ *
+ * Artificially complete all fences in fctx, wait for their work
+ * to drain, and destroy the memory associated with fctx.
+ */
void
nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
{
struct nouveau_fence *fence, *fnext;
+
+ /* Signal all the fences in fctx. */
spin_lock(&fctx->lock);
list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
nouveau_fence_signal(fence);
+ /* XXX Doesn't this leak fence? */
}
spin_unlock(&fctx->lock);
+
+ /* Wait for the workqueue to drain. */
+ flush_scheduled_work();
+
+ /* Destroy the fence context. */
+ DRM_DESTROY_WAITQUEUE(&fctx->waitqueue);
spin_lock_destroy(&fctx->lock);
}
+/*
+ * nouveau_fence_context_new(fctx)
+ *
+ * Initialize the state fctx for all fences on a channel.
+ */
void
nouveau_fence_context_new(struct nouveau_fence_chan *fctx)
{
+
INIT_LIST_HEAD(&fctx->flip);
INIT_LIST_HEAD(&fctx->pending);
spin_lock_init(&fctx->lock);
+ DRM_INIT_WAITQUEUE(&fctx->waitqueue, "nvfnchan");
}
+/*
+ * nouveau_fence_work_handler(kwork)
+ *
+ * Work handler for nouveau_fence_work.
+ */
static void
nouveau_fence_work_handler(struct work_struct *kwork)
{
struct fence_work *work = container_of(kwork, typeof(*work), base);
+
work->func(work->data);
kfree(work);
}
+/*
+ * nouveau_fence_work(fence, func, data)
+ *
+ * Arrange to call func(data) after fence is completed. If fence
+ * is already completed, call it immediately. If memory is
+ * scarce, synchronously wait for the fence and call it.
+ */
void
nouveau_fence_work(struct nouveau_fence *fence,
void (*func)(void *), void *data)
{
struct nouveau_channel *chan = fence->channel;
- struct nouveau_fence_chan *fctx;
+ struct nouveau_fence_chan *fctx = chan->fence;
struct fence_work *work = NULL;
- if (nouveau_fence_done(fence)) {
- func(data);
- return;
- }
-
- fctx = chan->fence;
work = kmalloc(sizeof(*work), GFP_KERNEL);
- if (!work) {
+ if (work == NULL) {
WARN_ON(nouveau_fence_wait(fence, false, false));
func(data);
return;
}
spin_lock(&fctx->lock);
- if (!fence->channel) {
+ if (fence->done) {
spin_unlock(&fctx->lock);
kfree(work);
func(data);
return;
}
-
INIT_WORK(&work->base, nouveau_fence_work_handler);
work->func = func;
work->data = data;
@@ -126,23 +174,39 @@ nouveau_fence_work(struct nouveau_fence
spin_unlock(&fctx->lock);
}
+/*
+ * nouveau_fence_update(chan)
+ *
+ * Test all fences on chan for completion. For any that are
+ * completed, mark them as such and schedule work for them.
+ *
+ * Caller must hold chan's fence lock.
+ */
static void
nouveau_fence_update(struct nouveau_channel *chan)
{
struct nouveau_fence_chan *fctx = chan->fence;
struct nouveau_fence *fence, *fnext;
- spin_lock(&fctx->lock);
+ BUG_ON(!spin_is_locked(&fctx->lock));
list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
if (fctx->read(chan) < fence->sequence)
break;
-
nouveau_fence_signal(fence);
nouveau_fence_unref(&fence);
}
- spin_unlock(&fctx->lock);
+ BUG_ON(!spin_is_locked(&fctx->lock));
}
+/*
+ * nouveau_fence_emit(fence, chan)
+ *
+ * - Initialize fence.
+ * - Set its timeout to 15 sec from now.
+ * - Assign it the next sequence number on channel.
+ * - Submit it to the device with the device-specific emit routine.
+ * - If that succeeds, add it to the list of pending fences on chan.
+ */
int
nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
{
@@ -151,7 +215,9 @@ nouveau_fence_emit(struct nouveau_fence
fence->channel = chan;
fence->timeout = jiffies + (15 * HZ);
+ spin_lock(&fctx->lock);
fence->sequence = ++fctx->sequence;
+ spin_unlock(&fctx->lock);
ret = fctx->emit(fence);
if (!ret) {
@@ -164,42 +230,90 @@ nouveau_fence_emit(struct nouveau_fence
return ret;
}
+/*
+ * nouveau_fence_done_locked(fence, chan)
+ *
+ * Test whether fence, which must be on chan, is done. If it is
+ * not marked as done, poll all fences on chan first.
+ *
+ * Caller must hold chan's fence lock.
+ */
+static bool
+nouveau_fence_done_locked(struct nouveau_fence *fence,
+ struct nouveau_channel *chan)
+{
+ struct nouveau_fence_chan *fctx __diagused = chan->fence;
+
+ BUG_ON(!spin_is_locked(&fctx->lock));
+ BUG_ON(fence->channel != chan);
+
+ /* If it's not done, poll it for changes. */
+ if (!fence->done)
+ nouveau_fence_update(chan);
+
+ /* Check, possibly again, whether it is done now. */
+ return fence->done;
+}
+
+/*
+ * nouveau_fence_done(fence)
+ *
+ * Test whether fence is done. If it is not marked as done, poll
+ * all fences on its channel first. Caller MUST NOT hold the
+ * fence lock.
+ */
bool
nouveau_fence_done(struct nouveau_fence *fence)
{
- if (fence->channel)
- nouveau_fence_update(fence->channel);
- return !fence->channel;
+ struct nouveau_channel *chan = fence->channel;
+ struct nouveau_fence_chan *fctx = chan->fence;
+ bool done;
+
+ spin_lock(&fctx->lock);
+ done = nouveau_fence_done_locked(fence, chan);
+ spin_unlock(&fctx->lock);
+
+ return done;
}
+/*
+ * nouveau_fence_wait_uevent_handler(data, index)
+ *
+ * Nouveau uevent handler for fence completion. data is a
+ * nouveau_fence_chan pointer. Simply wake up all threads waiting
+ * for completion of any fences on the channel. Does not mark
+ * fences as completed -- threads must poll fences for completion.
+ */
static int
nouveau_fence_wait_uevent_handler(void *data, int index)
{
- struct nouveau_fence_priv *priv = data;
-#ifdef __NetBSD__
- spin_lock(&priv->waitlock);
- /* XXX Set a flag... */
- DRM_SPIN_WAKEUP_ALL(&priv->waitqueue, &priv->waitlock);
- spin_unlock(&priv->waitlock);
-#else
- wake_up_all(&priv->waiting);
-#endif
+ struct nouveau_fence_chan *fctx = data;
+
+ spin_lock(&fctx->lock);
+ DRM_SPIN_WAKEUP_ALL(&fctx->waitqueue, &fctx->lock);
+ spin_unlock(&fctx->lock);
+
return NVKM_EVENT_KEEP;
}
+/*
+ * nouveau_fence_wait_uevent(fence, intr)
+ *
+ * Wait using a nouveau event for completion of fence. Wait
+ * interruptibly iff intr is true.
+ */
static int
nouveau_fence_wait_uevent(struct nouveau_fence *fence, bool intr)
-
{
struct nouveau_channel *chan = fence->channel;
struct nouveau_fifo *pfifo = nouveau_fifo(chan->drm->device);
- struct nouveau_fence_priv *priv = chan->drm->fence;
+ struct nouveau_fence_chan *fctx = chan->fence;
struct nouveau_eventh *handler;
int ret = 0;
ret = nouveau_event_new(pfifo->uevent, 0,
nouveau_fence_wait_uevent_handler,
- priv, &handler);
+ fctx, &handler);
if (ret)
return ret;
@@ -209,32 +323,19 @@ nouveau_fence_wait_uevent(struct nouveau
unsigned long timeout = fence->timeout - jiffies;
if (time_before(jiffies, fence->timeout)) {
-#ifdef __NetBSD__
- spin_lock(&priv->waitlock);
+ spin_lock(&fctx->lock);
if (intr) {
DRM_SPIN_TIMED_WAIT_UNTIL(ret,
- &priv->waitqueue, &priv->waitlock,
+ &fctx->waitqueue, &fctx->lock,
timeout,
- nouveau_fence_done(fence));
+ nouveau_fence_done_locked(fence, chan));
} else {
DRM_SPIN_TIMED_WAIT_NOINTR_UNTIL(ret,
- &priv->waitqueue, &priv->waitlock,
+ &fctx->waitqueue, &fctx->lock,
timeout,
- nouveau_fence_done(fence));
- }
- spin_unlock(&priv->waitlock);
-#else
- if (intr) {
- ret = wait_event_interruptible_timeout(
- priv->waiting,
- nouveau_fence_done(fence),
- timeout);
- } else {
- ret = wait_event_timeout(priv->waiting,
- nouveau_fence_done(fence),
- timeout);
+ nouveau_fence_done_locked(fence, chan));
}
-#endif
+ spin_unlock(&fctx->lock);
}
if (ret >= 0) {
@@ -243,26 +344,17 @@ nouveau_fence_wait_uevent(struct nouveau
ret = -EBUSY;
}
} else {
-#ifdef __NetBSD__
- spin_lock(&priv->waitlock);
- if (intr) {
- DRM_SPIN_WAIT_UNTIL(ret, &priv->waitqueue,
- &priv->waitlock,
- nouveau_fence_done(fence));
- } else {
- DRM_SPIN_WAIT_NOINTR_UNTIL(ret, &priv->waitqueue,
- &priv->waitlock,
- nouveau_fence_done(fence));
- }
- spin_unlock(&priv->waitlock);
-#else
+ spin_lock(&fctx->lock);
if (intr) {
- ret = wait_event_interruptible(priv->waiting,
- nouveau_fence_done(fence));
+ DRM_SPIN_WAIT_UNTIL(ret, &fctx->waitqueue,
+ &fctx->lock,
+ nouveau_fence_done_locked(fence, chan));
} else {
- wait_event(priv->waiting, nouveau_fence_done(fence));
+ DRM_SPIN_WAIT_NOINTR_UNTIL(ret, &fctx->waitqueue,
+ &fctx->lock,
+ nouveau_fence_done_locked(fence, chan));
}
-#endif
+ spin_unlock(&fctx->lock);
}
nouveau_event_ref(NULL, &handler);
@@ -272,15 +364,19 @@ nouveau_fence_wait_uevent(struct nouveau
return 0;
}
+/*
+ * nouveau_fence_wait(fence, lazy, intr)
+ *
+ * Wait for fence to complete. Wait interruptibly iff intr is
+ * true. If lazy is true, may sleep, either for a single tick or
+ * for an interrupt; otherwise will busy-wait.
+ */
int
nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
{
struct nouveau_channel *chan = fence->channel;
- struct nouveau_fence_priv *priv = chan ? chan->drm->fence : NULL;
-#ifndef __NetBSD__
- unsigned long sleep_time = NSEC_PER_MSEC / 1000;
- ktime_t t;
-#endif
+ struct nouveau_fence_priv *priv = chan->drm->fence;
+ unsigned long delay_usec = 1;
int ret = 0;
while (priv && priv->uevent && lazy && !nouveau_fence_done(fence)) {
@@ -295,32 +391,17 @@ nouveau_fence_wait(struct nouveau_fence
break;
}
-#ifdef __NetBSD__
- if (lazy)
- kpause("nvfencep", intr, 1, NULL);
- else
- DELAY(1);
-#else
- __set_current_state(intr ? TASK_INTERRUPTIBLE :
- TASK_UNINTERRUPTIBLE);
- if (lazy) {
- t = ktime_set(0, sleep_time);
- schedule_hrtimeout(&t, HRTIMER_MODE_REL);
- sleep_time *= 2;
- if (sleep_time > NSEC_PER_MSEC)
- sleep_time = NSEC_PER_MSEC;
- }
-
- if (intr && signal_pending(current)) {
- ret = -ERESTARTSYS;
- break;
+ if (lazy && delay_usec >= 1000*hztoms(1)) {
+ /* XXX errno NetBSD->Linux */
+ ret = -kpause("nvfencew", intr, 1, NULL);
+ if (ret != -EWOULDBLOCK)
+ break;
+ } else {
+ DELAY(delay_usec);
+ delay_usec *= 2;
}
-#endif
}
-#ifndef __NetBSD__
- __set_current_state(TASK_RUNNING);
-#endif
return ret;
}
@@ -347,12 +428,14 @@ static void
nouveau_fence_del(struct kref *kref)
{
struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
+
kfree(fence);
}
void
nouveau_fence_unref(struct nouveau_fence **pfence)
{
+
if (*pfence)
kref_put(&(*pfence)->kref, nouveau_fence_del);
*pfence = NULL;
@@ -361,6 +444,7 @@ nouveau_fence_unref(struct nouveau_fence
struct nouveau_fence *
nouveau_fence_ref(struct nouveau_fence *fence)
{
+
if (fence)
kref_get(&fence->kref);
return fence;
@@ -382,6 +466,7 @@ nouveau_fence_new(struct nouveau_channel
INIT_LIST_HEAD(&fence->work);
fence->sysmem = sysmem;
+ fence->done = false;
kref_init(&fence->kref);
ret = nouveau_fence_emit(fence, chan);
Index: src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.h
diff -u src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.h:1.2 src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.h:1.3
--- src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.h:1.2 Wed Aug 6 13:35:13 2014
+++ src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_fence.h Thu Aug 23 01:06:50 2018
@@ -9,6 +9,7 @@ struct nouveau_fence {
struct kref kref;
bool sysmem;
+ bool done;
struct nouveau_channel *channel;
unsigned long timeout;
@@ -27,6 +28,11 @@ void nouveau_fence_work(struct nouveau_f
int nouveau_fence_wait(struct nouveau_fence *, bool lazy, bool intr);
int nouveau_fence_sync(struct nouveau_fence *, struct nouveau_channel *);
+/*
+ * struct nouveau_fence_chan:
+ *
+ * State common to all fences in a single nouveau_channel.
+ */
struct nouveau_fence_chan {
struct list_head pending;
struct list_head flip;
@@ -39,9 +45,15 @@ struct nouveau_fence_chan {
int (*sync32)(struct nouveau_channel *, u64, u32);
spinlock_t lock;
+ drm_waitqueue_t waitqueue;
u32 sequence;
};
+/*
+ * struct nouveau_fence_priv:
+ *
+ * Device-specific operations on fences.
+ */
struct nouveau_fence_priv {
void (*dtor)(struct nouveau_drm *);
bool (*suspend)(struct nouveau_drm *);
@@ -49,12 +61,6 @@ struct nouveau_fence_priv {
int (*context_new)(struct nouveau_channel *);
void (*context_del)(struct nouveau_channel *);
-#ifdef __NetBSD__
- spinlock_t waitlock;
- drm_waitqueue_t waitqueue;
-#else
- wait_queue_head_t waiting;
-#endif
bool uevent;
};
Index: src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_nv84_fence.c
diff -u src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_nv84_fence.c:1.2 src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_nv84_fence.c:1.3
--- src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_nv84_fence.c:1.2 Wed Feb 25 14:57:04 2015
+++ src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_nv84_fence.c Thu Aug 23 01:06:50 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: nouveau_nv84_fence.c,v 1.2 2015/02/25 14:57:04 riastradh Exp $ */
+/* $NetBSD: nouveau_nv84_fence.c,v 1.3 2018/08/23 01:06:50 riastradh Exp $ */
/*
* Copyright 2012 Red Hat Inc.
@@ -25,7 +25,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nouveau_nv84_fence.c,v 1.2 2015/02/25 14:57:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nouveau_nv84_fence.c,v 1.3 2018/08/23 01:06:50 riastradh Exp $");
#include <core/object.h>
#include <core/client.h>
@@ -216,11 +216,6 @@ nv84_fence_destroy(struct nouveau_drm *d
{
struct nv84_fence_priv *priv = drm->fence;
-#ifdef __NetBSD__
- spin_lock_destroy(&priv->base.waitlock);
- DRM_DESTROY_WAITQUEUE(&priv->base.waitqueue);
-#endif
-
nouveau_bo_unmap(priv->bo_gart);
if (priv->bo_gart)
nouveau_bo_unpin(priv->bo_gart);
@@ -250,12 +245,6 @@ nv84_fence_create(struct nouveau_drm *dr
priv->base.context_new = nv84_fence_context_new;
priv->base.context_del = nv84_fence_context_del;
-#ifdef __NetBSD__
- spin_lock_init(&priv->base.waitlock);
- DRM_INIT_WAITQUEUE(&priv->base.waitqueue, "nvfenceq");
-#else
- init_waitqueue_head(&priv->base.waiting);
-#endif
priv->base.uevent = true;
ret = nouveau_bo_new(drm->dev, 16 * (pfifo->max + 1), 0,