[PATCH 1/6] fence: dma-buf cross-device synchronization (v17) (original) (raw)

Rob Clark robdclark at gmail.com
Mon Feb 17 08:56:39 PST 2014


On Mon, Feb 17, 2014 at 10:55 AM, Maarten Lankhorst <maarten.lankhorst at canonical.com> wrote:

A fence can be attached to a buffer which is being filled or consumed by hw, to allow userspace to pass the buffer without waiting to another device. For example, userspace can call pageflip ioctl to display the next frame of graphics after kicking the GPU but while the GPU is still rendering. The display device sharing the buffer with the GPU would attach a callback to get notified when the GPU's rendering-complete IRQ fires, to update the scan-out address of the display, without having to wake up userspace.

A driver must allocate a fence context for each execution ring that can run in parallel. The function for this takes an argument with how many contexts to allocate: + fencecontextalloc() A fence is transient, one-shot deal. It is allocated and attached to one or more dma-buf's. When the one that attached it is done, with the pending operation, it can signal the fence: + fencesignal() To have a rough approximation whether a fence is fired, call: + fenceissignaled() The dma-buf-mgr handles tracking, and waiting on, the fences associated with a dma-buf. The one pending on the fence can add an async callback: + fenceaddcallback() The callback can optionally be cancelled with: + fenceremovecallback() To wait synchronously, optionally with a timeout: + fencewait() + fencewaittimeout() When emitting a fence, call: + tracefenceemit() To annotate that a fence is blocking on another fence, call: + tracefenceannotatewaiton(fence, onfence) A default software-only implementation is provided, which can be used by drivers attaching a fence to a buffer when they have no other means for hw sync. But a memory backed fence is also envisioned, because it is common that GPU's can write to, or poll on some memory location for synchronization. For example: fence = customgetfence(...); if ((seqnofence = toseqnofence(fence)) != NULL) { dmabuf *fencebuf = seqnofence->syncbuf; getdmabuf(fencebuf); ... tell the hw the memory location to wait ... customwaiton(fencebuf, seqnofence->seqnoofs, fence->seqno); } else { /* fall-back to sw sync * / fenceaddcallback(fence, mycb); } On SoC platforms, if some other hw mechanism is provided for synchronizing between IP blocks, it could be supported as an alternate implementation with it's own fence ops in a similar way. enablesignaling callback is used to provide sw signaling in case a cpu waiter is requested or no compatible hardware signaling could be used. The intention is to provide a userspace interface (presumably via eventfd) later, to be used in conjunction with dma-buf's mmap support for sw access to buffers (or for userspace apps that would prefer to do their own synchronization). v1: Original v2: After discussion w/ danvet and mlankhorst on #dri-devel, we decided that dma-fence didn't need to care about the sw->hw signaling path (it can be handled same as sw->sw case), and therefore the fence->ops can be simplified and more handled in the core. So remove the signal, addcallback, cancelcallback, and wait ops, and replace with a simple enablesignaling() op which can be used to inform a fence supporting hw->hw signaling that one or more devices which do not support hw signaling are waiting (and therefore it should enable an irq or do whatever is necessary in order that the CPU is notified when the fence is passed). v3: Fix locking fail in attachfence() and getfence() v4: Remove tie-in w/ dma-buf.. after discussion w/ danvet and mlankorst we decided that we need to be able to attach one fence to N dma-buf's, so using the listhead in dma-fence struct would be problematic. v5: [ Maarten Lankhorst ] Updated for dma-bikeshed-fence and dma-buf-manager. v6: [ Maarten Lankhorst ] I removed dmafencecancelcallback and some comments about checking if fence fired or not. This is broken by design. waitqueueactive during destruction is now fatal, since the signaller should be holding a reference in enablesignalling until it signalled _the fence. Pass the original dmafencecb along, and call removewait in the dmafencecallback handler, so that no cleanup needs to be performed. v7: [ Maarten Lankhorst ] Set cb->func and only enable sw signaling if fence wasn't signaled yet, for example for hardware fences that may choose to signal blindly. _v8: [ Maarten Lankhorst ] Tons of tiny fixes, moved dmafenceinit to header and fixed include mess. dma-fence.h now includes dma-buf.h All members are now initialized, so kmalloc can be used for allocating a dma-fence. More documentation added. v9: Change compiler bitfields to flags, change return type of enablesignaling to bool. Rework dmafencewait. Added dmafenceissignaled and dmafencewaittimeout. s/dma// and change exports to non GPL. Added fenceissignaled and fenceenableswsignaling calls, add ability to override default wait operation. v10: remove eventqueue, use a custom list, export trytowakeup from scheduler. Remove fence lock and use a global spinlock instead, this should hopefully remove all the locking headaches I was having on trying to implement this. enablesignaling is called with this lock held. v11: Use atomic ops for flags, lifting the need for some spinlockirqsaves. However I kept the guarantee that after fencesignal returns, it is guaranteed that enablesignaling has either been called to completion, or will not be called any more. Add contexts and seqno to base fence implementation. This allows you to wait for less fences, by testing for seqno + signaled, and then only wait on the later fence. Add FENCETRACE, FENCEWARN, and FENCEERR. This makes debugging easier. An CONFIGDEBUGFENCE will be added to turn off the FENCETRACE spam, and another runtime option can turn it off at runtime. v12: Add CONFIGFENCETRACE. Add missing documentation for the fence->context and fence->seqno members. v13: Fixup CONFIGFENCETRACE kconfig description. Move fencecontextalloc to fence. Simplify fencelater. Kill priv member to fencecb. v14: Remove priv argument from fenceaddcallback, oops! v15: Remove priv from documentation. Explicitly include linux/atomic.h. v16: Add trace events. Import changes required by android syncpoints. v17: Use wakeupstate instead of trytowakeup. (Colin Cross) Fix up commit description for seqnofence. (Rob Clark) Signed-off-by: Maarten Lankhorst <maarten.lankhorst at canonical.com>

Reviewed-by: Rob Clark <robdclark at gmail.com>

--- Documentation/DocBook/device-drivers.tmpl | 2 drivers/base/Kconfig | 9 + drivers/base/Makefile | 2 drivers/base/fence.c | 415 +++++++++++++++++++++++++++++ include/linux/fence.h | 329 +++++++++++++++++++++++ include/trace/events/fence.h | 125 +++++++++ 6 files changed, 881 insertions(+), 1 deletion(-) create mode 100644 drivers/base/fence.c create mode 100644 include/linux/fence.h create mode 100644 include/trace/events/fence.h

diff --git a/Documentation/DocBook/device-drivers.tmpl b/Documentation/DocBook/device-drivers.tmpl index f5170082bdb3..7a0c9ddb4818 100644 --- a/Documentation/DocBook/device-drivers.tmpl +++ b/Documentation/DocBook/device-drivers.tmpl @@ -129,6 +129,8 @@ X!Edrivers/base/interface.c Device Drivers DMA Management !Edrivers/base/dma-buf.c +!Edrivers/base/fence.c +!Iinclude/linux/fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.c diff --git a/drivers/base/Kconfig b/drivers/base/Kconfig index ec36e7772e57..b50ad30151ae 100644 --- a/drivers/base/Kconfig +++ b/drivers/base/Kconfig @@ -200,6 +200,15 @@ config DMASHAREDBUFFER APIs extension; the file's descriptor can then be passed on to other driver. +config FENCETRACE + bool "Enable verbose FENCETRACE messages" + depends on DMASHAREDBUFFER + help + Enable the FENCETRACE printks. This will add extra + spam to the console log, but will make it easier to diagnose + lockup related problems for dma-buffers shared across multiple + devices. + config DMACMA bool "DMA Contiguous Memory Allocator" depends on HAVEDMACONTIGUOUS && CMA diff --git a/drivers/base/Makefile b/drivers/base/Makefile index 04b314e0fa51..eb4864aee073 100644 --- a/drivers/base/Makefile +++ b/drivers/base/Makefile @@ -10,7 +10,7 @@ obj-$(CONFIGDMACMA) += dma-contiguous.o obj-y += power/ obj-$(CONFIGHASDMA) += dma-mapping.o obj-$(CONFIGHAVEGENERICDMACOHERENT) += dma-coherent.o -obj-$(CONFIGDMASHAREDBUFFER) += dma-buf.o reservation.o +obj-$(CONFIGDMASHAREDBUFFER) += dma-buf.o fence.o reservation.o obj-$(CONFIGISA) += isa.o obj-$(CONFIGFWLOADER) += firmwareclass.o obj-$(CONFIGNUMA) += node.o diff --git a/drivers/base/fence.c b/drivers/base/fence.c new file mode 100644 index 000000000000..12df2bf62034 --- /dev/null +++ b/drivers/base/fence.c @@ -0,0 +1,415 @@ +/* + * Fence mechanism for dma-buf and to allow for asynchronous dma access + * + * Copyright (C) 2012 Canonical Ltd + * Copyright (C) 2012 Texas Instruments + * + * Authors: + * Rob Clark <robdclark at gmail.com> + * Maarten Lankhorst <maarten.lankhorst at canonical.com> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <linux/slab.h> +#include <linux/export.h> +#include <linux/atomic.h> +#include <linux/fence.h> + +#define CREATETRACEPOINTS +#include <trace/events/fence.h> + +EXPORTTRACEPOINTSYMBOL(fenceannotatewaiton); +EXPORTTRACEPOINTSYMBOL(fenceemit); + +/** + * fence context counter: each execution context should have its own + * fence context, this allows checking if fences belong to the same + * context or not. One device can have multiple separate contexts, + * and they're used if some engine can run independently of another. + */ +static atomict fencecontextcounter = ATOMICINIT(0); + +/** + * fencecontextalloc - allocate an array of fence contexts + * @num: [in] amount of contexts to allocate + * + * This function will return the first index of the number of fences allocated. + * The fence context is used for setting fence->context to a unique number. + */ +unsigned fencecontextalloc(unsigned num) +{ + BUGON(!num); + return atomicaddreturn(num, &fencecontextcounter) - num; +} +EXPORTSYMBOL(fencecontextalloc); + _+int fencesignal(struct fence *fence) +{ + struct fencecb *cur, *tmp; + int ret = 0; + + if (WARNON(!fence)) + return -EINVAL; + + if (!ktimetons(fence->timestamp)) { + fence->timestamp = ktimeget(); + smpmb_beforeclearbit(); + } + + if (testandsetbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) { + ret = -EINVAL; + + /* + * we might have raced with the unlocked fencesignal, + * still run through all callbacks + */ + } else + tracefencesignaled(fence); + + listforeachentrysafe(cur, tmp, &fence->cblist, node) { + listdelinit(&cur->node); + cur->func(fence, cur); + } + return ret; +} _+EXPORTSYMBOL(fencesignal); + +/** + * fencesignal - signal completion of a fence + * @fence: the fence to signal + * + * Signal completion for software callbacks on a fence, this will unblock + * fencewait() calls and run all the callbacks added with + * fenceaddcallback(). Can be called multiple times, but since a fence + * can only go from unsignaled to signaled state, it will only be effective + * the first time. + */ +int fencesignal(struct fence *fence) +{ + unsigned long flags; + + if (!fence) + return -EINVAL; + + if (!ktimetons(fence->timestamp)) { + fence->timestamp = ktimeget(); + smpmb_beforeclearbit(); + } + + if (testandsetbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) + return -EINVAL; + + tracefencesignaled(fence); + + if (testbit(FENCEFLAGENABLESIGNALBIT, &fence->flags)) { + struct fencecb *cur, *tmp; + + spinlockirqsave(fence->lock, flags); + listforeachentrysafe(cur, tmp, &fence->cblist, node) { + listdelinit(&cur->node); + cur->func(fence, cur); + } + spinunlockirqrestore(fence->lock, flags); + } + return 0; +} +EXPORTSYMBOL(fencesignal); + +/** + * fencewaittimeout - sleep until the fence gets signaled + * or until timeout elapses + * @fence: [in] the fence to wait on + * @intr: [in] if true, do an interruptible wait + * @timeout: [in] timeout value in jiffies, or MAXSCHEDULETIMEOUT + * + * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the + * remaining timeout in jiffies on success. Other error values may be + * returned on custom implementations. + * + * Performs a synchronous wait on this fence. It is assumed the caller + * directly or indirectly (buf-mgr between reservation and committing) + * holds a reference to the fence, otherwise the fence might be + * freed before return, resulting in undefined behavior. + */ +long +fencewaittimeout(struct fence *fence, bool intr, signed long timeout) +{ + unsigned long ret; + + if (WARNON(timeout < 0))_ _+ return -EINVAL;_ _+_ _+ tracefencewaitstart(fence);_ _+ ret = fence->ops->wait(fence, intr, timeout); + tracefencewaitend(fence); + return ret; +} +EXPORTSYMBOL(fencewaittimeout); + +void releasefence(struct kref *kref) +{ + struct fence *fence = + containerof(kref, struct fence, refcount); + + tracefencedestroy(fence); + + BUGON(!listempty(&fence->cblist)); + + if (fence->ops->release) + fence->ops->release(fence); + else + kfree(fence); +} +EXPORTSYMBOL(releasefence); + +/** + * fenceenableswsignaling - enable signaling on fence + * @fence: [in] the fence to enable + * + * this will request for sw signaling to be enabled, to make the fence + * complete as soon as possible + */ +void fenceenableswsignaling(struct fence *fence) +{ + unsigned long flags; + + if (!testandsetbit(FENCEFLAGENABLESIGNALBIT, &fence->flags) && + !testbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) { + tracefenceenablesignal(fence); + + spinlockirqsave(fence->lock, flags); + + if (!fence->ops->enablesignaling(fence)) _+ fencesignal(fence); + + spinunlockirqrestore(fence->lock, flags); + } +} +EXPORTSYMBOL(fenceenableswsignaling); + +/** + * fenceaddcallback - add a callback to be called when the fence + * is signaled + * @fence: [in] the fence to wait on + * @cb: [in] the callback to register + * @func: [in] the function to call + * + * cb will be initialized by fenceaddcallback, no initialization + * by the caller is required. Any number of callbacks can be registered + * to a fence, but a callback can only be registered to one fence at a time. + * + * Note that the callback can be called from an atomic context. If + * fence is already signaled, this function will return -ENOENT (and + * not call the callback) + * + * Add a software callback to the fence. Same restrictions apply to + * refcount as it does to fencewait, however the caller doesn't need to + * keep a refcount to fence afterwards: when software access is enabled, + * the creator of the fence is required to keep the fence alive until + * after it signals with fencesignal. The callback itself can be called + * from irq context. + * + */ +int fenceaddcallback(struct fence *fence, struct fencecb *cb, + fencefunct func) +{ + unsigned long flags; + int ret = 0; + bool wasset; + + if (WARNON(!fence || !func)) + return -EINVAL; + + if (testbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) { + INITLISTHEAD(&cb->node); + return -ENOENT; + } + + spinlockirqsave(fence->lock, flags); + + wasset = testandsetbit(FENCEFLAGENABLESIGNALBIT, &fence->flags); + + if (testbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) + ret = -ENOENT; + else if (!wasset) { + tracefenceenablesignal(fence); + + if (!fence->ops->enablesignaling(fence)) { _+ fencesignal(fence); + ret = -ENOENT; + } + } + + if (!ret) { + cb->func = func; + listaddtail(&cb->node, &fence->cblist); + } else + INITLISTHEAD(&cb->node); + spinunlockirqrestore(fence->lock, flags); + + return ret; +} +EXPORTSYMBOL(fenceaddcallback); + +/** + * fenceremovecallback - remove a callback from the signaling list + * @fence: [in] the fence to wait on + * @cb: [in] the callback to remove + * + * Remove a previously queued callback from the fence. This function returns + * true if the callback is succesfully removed, or false if the fence has + * already been signaled. + * + * WARNING: + * Cancelling a callback should only be done if you really know what you're + * doing, since deadlocks and race conditions could occur all too easily. For + * this reason, it should only ever be done on hardware lockup recovery, + * with a reference held to the fence. + */ +bool +fenceremovecallback(struct fence *fence, struct fencecb *cb) +{ + unsigned long flags; + bool ret; + + spinlockirqsave(fence->lock, flags); + + ret = !listempty(&cb->node); + if (ret) + listdelinit(&cb->node); + + spinunlockirqrestore(fence->lock, flags); + + return ret; +} +EXPORTSYMBOL(fenceremovecallback); + +struct defaultwaitcb { + struct fencecb base; + struct taskstruct *task; +}; + +static void +fencedefaultwaitcb(struct fence *fence, struct fencecb *cb) +{ + struct defaultwaitcb *wait = + containerof(cb, struct defaultwaitcb, base); + + wakeupstate(wait->task, TASKNORMAL); +} + +/** + * fencedefaultwait - default sleep until the fence gets signaled + * or until timeout elapses + * @fence: [in] the fence to wait on + * @intr: [in] if true, do an interruptible wait + * @timeout: [in] timeout value in jiffies, or MAXSCHEDULETIMEOUT + * + * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the + * remaining timeout in jiffies on success. + */ +long +fencedefaultwait(struct fence *fence, bool intr, signed long timeout) +{ + struct defaultwaitcb cb; + unsigned long flags; + long ret = timeout; + bool wasset; + + if (testbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) + return timeout; + + spinlockirqsave(fence->lock, flags); + + if (intr && signalpending(current)) { + ret = -ERESTARTSYS; + goto out; + } + + wasset = testandsetbit(FENCEFLAGENABLESIGNALBIT, &fence->flags); + + if (testbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) + goto out; + + if (!wasset) { + tracefenceenablesignal(fence); + + if (!fence->ops->enablesignaling(fence)) { _+ fencesignal(fence); + goto out; + } + } + + cb.base.func = fencedefaultwaitcb; + cb.task = current; + listadd(&cb.base.node, &fence->cblist); + + while (!testbit(FENCEFLAGSIGNALEDBIT, &fence->flags) && ret > 0) { + if (intr) _+ setcurrentstate(TASKINTERRUPTIBLE); + else _+ setcurrentstate(TASKUNINTERRUPTIBLE); + spinunlockirqrestore(fence->lock, flags); + + ret = scheduletimeout(ret); + + spinlockirqsave(fence->lock, flags); + if (ret > 0 && intr && signalpending(current)) + ret = -ERESTARTSYS; + } + + if (!listempty(&cb.base.node)) + listdel(&cb.base.node); _+ setcurrentstate(TASKRUNNING); + +out: + spinunlockirqrestore(fence->lock, flags); + return ret; +} +EXPORTSYMBOL(fencedefaultwait); + +/** _+ * fenceinit - Initialize a custom fence. + * @fence: [in] the fence to initialize + * @ops: [in] the fenceops for operations on this fence + * @lock: [in] the irqsafe spinlock to use for locking this fence + * @context: [in] the execution context this fence is run on + * @seqno: [in] a linear increasing sequence number for this context + * + * Initializes an allocated fence, the caller doesn't have to keep its + * refcount after committing with this fence, but it will need to hold a + * refcount again if fenceops.enablesignaling gets called. This can + * be used for other implementing other types of fence. + * + * context and seqno are used for easy comparison between fences, allowing + * to check which fence is later by simply using fencelater. + */ +void _+fenceinit(struct fence *fence, const struct fenceops *ops, + spinlockt *lock, unsigned context, unsigned seqno) +{ + BUGON(!lock); + BUGON(!ops || !ops->wait || !ops->enablesignaling || !ops->getdrivername || !ops->gettimelinename); + + krefinit(&fence->refcount); + fence->ops = ops; + INITLISTHEAD(&fence->cblist); + fence->lock = lock; + fence->context = context; + fence->seqno = seqno; + fence->flags = 0UL; + + tracefenceinit(fence); +} _+EXPORTSYMBOL(fenceinit); diff --git a/include/linux/fence.h b/include/linux/fence.h new file mode 100644 index 000000000000..a868eceb7cf5 --- /dev/null +++ b/include/linux/fence.h @@ -0,0 +1,329 @@ +/* + * Fence mechanism for dma-buf to allow for asynchronous dma access + * + * Copyright (C) 2012 Canonical Ltd + * Copyright (C) 2012 Texas Instruments + * + * Authors: + * Rob Clark <robdclark at gmail.com> + * Maarten Lankhorst <maarten.lankhorst at canonical.com> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + _+#ifndef LINUXFENCEH _+#define LINUXFENCEH + +#include <linux/err.h> +#include <linux/wait.h> +#include <linux/list.h> +#include <linux/bitops.h> +#include <linux/kref.h> +#include <linux/sched.h> +#include <linux/printk.h> + +struct fence; +struct fenceops; +struct fencecb; + +/** + * struct fence - software synchronization primitive + * @refcount: refcount for this fence + * @ops: fenceops associated with this fence + * @cblist: list of all callbacks to call + * @lock: spinlockirqsave used for locking + * @context: execution context this fence belongs to, returned by + * fencecontextalloc() + * @seqno: the sequence number of this fence inside the execution context, + * can be compared to decide which fence would be signaled later. + * @flags: A mask of FENCEFLAG* defined below + * @timestamp: Timestamp when the fence was signaled. + * @status: Optional, only valid if < 0, must be set before calling_ _+ * fencesignal, indicates that the fence has completed with an error._ _+ *_ _+ * the flags member must be manipulated and read using the appropriate_ _+ * atomic ops (bit*), so taking the spinlock will not be needed most_ _+ * of the time._ _+ *_ _+ * FENCEFLAGSIGNALEDBIT - fence is already signaled_ _+ * FENCEFLAGENABLESIGNALBIT - enablesignaling might have been called*_ _+ * FENCEFLAGUSERBITS - start of the unused bits, can be used by the_ _+ * implementer of the fence for its own purposes. Can be used in different_ _+ * ways by different fence implementers, so do not rely on this._ _+ *_ _+ * *) Since atomic bitops are used, this is not guaranteed to be the case._ _+ * Particularly, if the bit was set, but fencesignal was called right_ _+ * before this bit was set, it would have been able to set the_ _+ * FENCEFLAGSIGNALEDBIT, before enablesignaling was called._ _+ * Adding a check for FENCEFLAGSIGNALEDBIT after setting_ _+ * FENCEFLAGENABLESIGNALBIT closes this race, and makes sure that_ _+ * after fencesignal was called, any enablesignaling call will have either_ _+ * been completed, or never called at all._ _+ */_ _+struct fence {_ _+ struct kref refcount;_ _+ const struct fenceops *ops;_ _+ struct listhead cblist;_ _+ spinlockt *lock;_ _+ unsigned context, seqno;_ _+ unsigned long flags;_ _+ ktimet timestamp;_ _+ int status;_ _+};_ _+_ _+enum fenceflagbits {_ _+ FENCEFLAGSIGNALEDBIT,_ _+ FENCEFLAGENABLESIGNALBIT,_ _+ FENCEFLAGUSERBITS, /* must always be last member */_ _+};_ _+_ _+typedef void (*fencefunct)(struct fence *fence, struct fencecb *cb);_ _+_ _+/**_ _+ * struct fencecb - callback for fenceaddcallback_ _+ * @node: used by fenceaddcallback to append this struct to fence::cblist_ _+ * @func: fencefunct to call_ _+ *_ _+ * This struct will be initialized by fenceaddcallback, additional_ _+ * data can be passed along by embedding fencecb in another struct._ _+ */_ _+struct fencecb {_ _+ struct listhead node;_ _+ fencefunct func;_ _+};_ _+_ _+/**_ _+ * struct fenceops - operations implemented for fence_ _+ * @getdrivername: returns the driver name._ _+ * @gettimelinename: return the name of the context this fence belongs to._ _+ * @enablesignaling: enable software signaling of fence._ _+ * @signaled: [optional] peek whether the fence is signaled, can be null._ _+ * @wait: custom wait implementation, or fencedefaultwait._ _+ * @release: [optional] called on destruction of fence, can be null_ _+ * @filldriverdata: [optional] callback to fill in free-form debug info_ _+ * Returns amount of bytes filled, or -errno._ _+ * @fencevaluestr: [optional] fills in the value of the fence as a string_ _+ * @timelinevaluestr: [optional] fills in the current value of the timeline as a string_ _+ *_ _+ * Notes on enablesignaling:_ _+ * For fence implementations that have the capability for hw->hw + * signaling, they can implement this op to enable the necessary + * irqs, or insert commands into cmdstream, etc. This is called + * in the first wait() or addcallback() path to let the fence + * implementation know that there is another driver waiting on + * the signal (ie. hw->sw case). + * + * This function can be called called from atomic context, but not + * from irq context, so normal spinlocks can be used. + * + * A return value of false indicates the fence already passed, + * or some failure occured that made it impossible to enable + * signaling. True indicates succesful enabling. + * + * fence->status may be set in enablesignaling, but only when false is returned. + * + * Calling fencesignal before enablesignaling is called allows + * for a tiny race window in which enablesignaling is called during, + * before, or after fencesignal. To fight this, it is recommended + * that before enablesignaling returns true an extra reference is + * taken on the fence, to be released when the fence is signaled. + * This will mean fencesignal will still be called twice, but + * the second time will be a noop since it was already signaled. + * + * Notes on signaled: + * May set fence->status if returning true. + * + * Notes on wait: + * Must not be NULL, set to fencedefaultwait for default implementation. + * the fencedefaultwait implementation should work for any fence, as long + * as enablesignaling works correctly. + * + * Must return -ERESTARTSYS if the wait is intr = true and the wait was + * interrupted, and remaining jiffies if fence has signaled, or 0 if wait + * timed out. Can also return other error values on custom implementations, + * which should be treated as if the fence is signaled. For example a hardware + * lockup could be reported like that. + * + * Notes on release: + * Can be NULL, this function allows additional commands to run on + * destruction of the fence. Can be called from irq context. + * If pointer is set to NULL, kfree will get called instead. + */ + +struct fenceops { + const char *(*getdrivername)(struct fence *fence); + const char *(*gettimelinename)(struct fence *fence); + bool (*enablesignaling)(struct fence *fence); + bool (*signaled)(struct fence *fence); + long (*wait)(struct fence *fence, bool intr, signed long timeout); + void (*release)(struct fence *fence); + + int (*filldriverdata)(struct fence *fence, void *data, int size); + void (*fencevaluestr)(struct fence *fence, char *str, int size); + void (*timelinevaluestr)(struct fence *fence, char *str, int size); +}; + _+void fenceinit(struct fence *fence, const struct fenceops *ops, + spinlockt *lock, unsigned context, unsigned seqno); + +/** + * fenceget - increases refcount of the fence + * @fence: [in] fence to increase refcount of + */ +static inline void fenceget(struct fence *fence) +{ + if (WARNON(!fence)) + return; + krefget(&fence->refcount); +} + +extern void releasefence(struct kref *kref); + +/** + * fenceput - decreases refcount of the fence + * @fence: [in] fence to reduce refcount of + */ +static inline void fenceput(struct fence *fence) +{ + if (WARNON(!fence)) + return; + krefput(&fence->refcount, releasefence); +} + +int fencesignal(struct fence *fence); _+int fencesignal(struct fence *fence); +long fencedefaultwait(struct fence *fence, bool intr, signed long timeout); +int fenceaddcallback(struct fence *fence, struct fencecb *cb, + fencefunct func); +bool fenceremovecallback(struct fence *fence, struct fencecb *cb); +void fenceenableswsignaling(struct fence *fence); + +static inline bool _+fenceissignaled(struct fence *fence) +{ + if (testbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) + return true; + + if (fence->ops->signaled && fence->ops->signaled(fence)) { _+ fencesignal(fence); + return true; + } + + return false; +} + +/** + * fenceissignaled - Return an indication if the fence is signaled yet. + * @fence: [in] the fence to check + * + * Returns true if the fence was already signaled, false if not. Since this + * function doesn't enable signaling, it is not guaranteed to ever return true + * If fenceaddcallback, fencewait or fenceenableswsignaling + * haven't been called before. + * + * It's recommended for seqno fences to call fencesignal when the + * operation is complete, it makes it possible to prevent issues from + * wraparound between time of issue and time of use by checking the return + * value of this function before calling hardware-specific wait instructions. + */ +static inline bool +fenceissignaled(struct fence *fence) +{ + if (testbit(FENCEFLAGSIGNALEDBIT, &fence->flags)) + return true; + + if (fence->ops->signaled && fence->ops->signaled(fence)) { + fencesignal(fence); + return true; + } + + return false; +} + +/** + * fencelater - return the chronologically later fence + * @f1: [in] the first fence from the same context + * @f2: [in] the second fence from the same context + * + * Returns NULL if both fences are signaled, otherwise the fence that would be + * signaled last. Both fences must be from the same context, since a seqno is + * not re-used across contexts. + */ +static inline struct fence *fencelater(struct fence *f1, struct fence *f2) +{ + BUGON(f1->context != f2->context); + + /* + * can't check just FENCEFLAGSIGNALEDBIT here, it may never have been + * set called if enablesignaling wasn't, and enabling that here is + * overkill. + */ _+ if (f2->seqno - f1->seqno <= INTMAX)_ _+ return fenceissignaled(f2) ? NULL : f2;_ _+ else_ _+ return fenceissignaled(f1) ? NULL : f1;_ _+}_ _+_ _+long fencewaittimeout(struct fence *fence, bool intr, signed long timeout);_ _+_ _+_ _+/**_ _+ * fencewait - sleep until the fence gets signaled_ _+ * @fence: [in] the fence to wait on_ _+ * @intr: [in] if true, do an interruptible wait_ _+ *_ _+ * This function will return -ERESTARTSYS if interrupted by a signal,_ _+ * or 0 if the fence was signaled. Other error values may be_ _+ * returned on custom implementations._ _+ *_ _+ * Performs a synchronous wait on this fence. It is assumed the caller_ _+ * directly or indirectly (buf-mgr between reservation and committing)_ _+ * holds a reference to the fence, otherwise the fence might be_ _+ * freed before return, resulting in undefined behavior._ _+ */_ _+static inline long fencewait(struct fence *fence, bool intr)_ _+{_ _+ long ret;_ _+_ _+ /* Since fencewaittimeout cannot timeout with_ _+ * MAXSCHEDULETIMEOUT, only valid return values are_ _+ * -ERESTARTSYS and MAXSCHEDULETIMEOUT._ _+ */_ _+ ret = fencewaittimeout(fence, intr, MAXSCHEDULETIMEOUT);_ _+_ _+ return ret < 0 ? ret : 0;_ _+}_ _+_ _+unsigned fencecontextalloc(unsigned num);_ _+_ _+#define FENCETRACE(f, fmt, args...) _ _+ do { _ _+ struct fence *_ff = (f); _ _+ if (configenabled(CONFIGFENCETRACE)) _ _+ prinfo("f %u#%u: " fmt, _ _+ _ff->context, _ff->seqno, ##args); _ + } while (0) + _+#define FENCEWARN(f, fmt, args...) _ _+ do { _ _+ struct fence *_ff = (f); _ _+ prwarn("f %u#%u: " fmt, _ff->context, _ff->seqno, ##args); _ + } while (0) + _+#define FENCEERR(f, fmt, args...) _ _+ do { _ _+ struct fence _ff = (f); _ _+ prerr("f %u#%u: " fmt, _ff->context, _ff->seqno, ##args); _ + } while (0) + _+#endif / LINUXFENCEH */ diff --git a/include/trace/events/fence.h b/include/trace/events/fence.h new file mode 100644 index 000000000000..ce3d83230cff --- /dev/null +++ b/include/trace/events/fence.h @@ -0,0 +1,125 @@ +#undef TRACESYSTEM +#define TRACESYSTEM fence + +#if !defined(TRACEFENCEH) || defined(TRACEHEADERMULTIREAD) +#define TRACEFENCEH + +#include <linux/tracepoint.h> + +struct fence; + +TRACEEVENT(fenceannotatewaiton, + + /* fence: the fence waiting on f1, f1: the fence to be waited on. */ + TPPROTO(struct fence *fence, struct fence *f1), + + TPARGS(fence, f1), + + TPSTRUCT_entry( _+ string(driver, fence->ops->getdrivername(fence)) _+ string(timeline, fence->ops->getdrivername(fence)) _+ field(unsigned int, context) _+ field(unsigned int, seqno) + _+ string(waitingdriver, f1->ops->getdrivername(f1)) _+ string(waitingtimeline, f1->ops->gettimelinename(f1)) _+ field(unsigned int, waitingcontext) _+ field(unsigned int, waitingseqno) + ), + + TPfastassign( _+ assignstr(driver, fence->ops->getdrivername(fence)) _+ assignstr(timeline, fence->ops->gettimelinename(fence)) _+ entry->context = fence->context; _+ entry->seqno = fence->seqno; + _+ assignstr(waitingdriver, f1->ops->getdrivername(f1)) _+ assignstr(waitingtimeline, f1->ops->gettimelinename(f1)) _+ entry->waitingcontext = f1->context; _+ entry->waitingseqno = f1->seqno; + + ), + + TPprintk("driver=%s timeline=%s context=%u seqno=%u " + "waits on driver=%s timeline=%s context=%u seqno=%u", _+ _getstr(driver), _getstr(timeline), _entry->context, entry->seqno, _+ _getstr(waitingdriver), _getstr(waitingtimeline), _entry->waitingcontext, entry->waitingseqno) +); + +DECLAREEVENTCLASS(fence, + + TPPROTO(struct fence *fence), + + TPARGS(fence), + + TPSTRUCT_entry( _+ string(driver, fence->ops->getdrivername(fence)) _+ string(timeline, fence->ops->gettimelinename(fence)) _+ field(unsigned int, context) _+ field(unsigned int, seqno) + ), + + TPfastassign( _+ assignstr(driver, fence->ops->getdrivername(fence)) _+ assignstr(timeline, fence->ops->gettimelinename(fence)) _+ entry->context = fence->context; _+ entry->seqno = fence->seqno; + ), + + TPprintk("driver=%s timeline=%s context=%u seqno=%u", _+ _getstr(driver), _getstr(timeline), _entry->context, entry->seqno) +); + +DEFINEEVENT(fence, fenceemit, + + TPPROTO(struct fence *fence), + + TPARGS(fence) +); + +DEFINEEVENT(fence, fenceinit, + + TPPROTO(struct fence *fence), + + TPARGS(fence) +); + +DEFINEEVENT(fence, fencedestroy, + + TPPROTO(struct fence *fence), + + TPARGS(fence) +); + +DEFINEEVENT(fence, fenceenablesignal, + + TPPROTO(struct fence *fence), + + TPARGS(fence) +); + +DEFINEEVENT(fence, fencesignaled, + + TPPROTO(struct fence *fence), + + TPARGS(fence) +); + +DEFINEEVENT(fence, fencewaitstart, + + TPPROTO(struct fence *fence), + + TPARGS(fence) +); + +DEFINEEVENT(fence, fencewaitend, + + TPPROTO(struct fence *fence), + + TPARGS(fence) +); + +#endif /* TRACEFENCEH */ + +/* This part must be outside protection */ +#include <trace/definetrace.h>



More information about the dri-devel mailing list