dma-buf synchronization patches
Subsystems (16)
cover dma-buf synchronization patches
The following series implements fence and converts dma-buf and android sync to use it. Patch 6 and 7 add support for polling to dma-buf, blocking until all fences are signaled.
I've dropped the extra patch to copy an export from the core,
and instead use the public version of it. I've had to fix
some fallout from the rebase, hopefully everything's clean now,
and ready for -next.
---
Maarten Lankhorst (6):
fence: dma-buf cross-device synchronization (v17)
seqno-fence: Hardware dma-buf implementation of fencing (v4)
dma-buf: use reservation objects
android: convert sync to fence api, v3
reservation: add support for fences to enable cross-device synchronisation
dma-buf: add poll support, v2
Documentation/DocBook/device-drivers.tmpl | 3 drivers/base/Kconfig | 9 drivers/base/Makefile | 2 drivers/base/dma-buf.c | 123 +++ drivers/base/fence.c | 465 +++++++++++++ drivers/gpu/drm/drm_prime.c | 8 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 drivers/gpu/drm/i915/i915_gem_dmabuf.c | 2 drivers/gpu/drm/nouveau/nouveau_drm.c | 1 drivers/gpu/drm/nouveau/nouveau_gem.h | 1 drivers/gpu/drm/nouveau/nouveau_prime.c | 7 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c | 2 drivers/gpu/drm/radeon/radeon_drv.c | 2 drivers/gpu/drm/radeon/radeon_prime.c | 8 drivers/gpu/drm/ttm/ttm_object.c | 2 drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 drivers/staging/android/Kconfig | 1 drivers/staging/android/Makefile | 2 drivers/staging/android/sw_sync.c | 4 drivers/staging/android/sync.c | 892 ++++++++---------------- drivers/staging/android/sync.h | 80 +- drivers/staging/android/sync_debug.c | 245 +++++++ drivers/staging/android/trace/sync.h | 12 include/drm/drmP.h | 2 include/linux/dma-buf.h | 21 - include/linux/fence.h | 329 +++++++++ include/linux/reservation.h | 18 include/linux/seqno-fence.h | 109 +++ include/trace/events/fence.h | 125 +++ 29 files changed, 1822 insertions(+), 657 deletions(-) create mode 100644 drivers/base/fence.c create mode 100644 drivers/staging/android/sync_debug.c create mode 100644 include/linux/fence.h create mode 100644 include/linux/seqno-fence.h create mode 100644 include/trace/events/fence.h
Signature · 1 line
Signature
PATCH 1/6 fence: dma-buf cross-device synchronization (v17)
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 page_flip 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:
+ fence_context_alloc()
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:
+ fence_signal()
To have a rough approximation whether a fence is fired, call:
+ fence_is_signaled()
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:
+ fence_add_callback()
The callback can optionally be cancelled with:
+ fence_remove_callback()
To wait synchronously, optionally with a timeout:
+ fence_wait()
+ fence_wait_timeout()
When emitting a fence, call:
+ trace_fence_emit()
To annotate that a fence is blocking on another fence, call:
+ trace_fence_annotate_wait_on(fence, on_fence)
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 = custom_get_fence(...);
if ((seqno_fence = to_seqno_fence(fence)) != NULL) {
dma_buf *fence_buf = seqno_fence->sync_buf;
get_dma_buf(fence_buf); ... tell the hw the memory location to wait ... custom_wait_on(fence_buf, seqno_fence->seqno_ofs, fence->seqno);
} else {
/* fall-back to sw sync * /
fence_add_callback(fence, my_cb);}
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.
enable_signaling 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, add_callback, cancel_callback, and wait ops, and replace with a simple enable_signaling() 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 attach_fence() and get_fence()
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 list_head in dma-fence struct would be problematic.
v5: [ Maarten Lankhorst ] Updated for dma-bikeshed-fence and dma-buf-manager.
v6: [ Maarten Lankhorst ] I removed dma_fence_cancel_callback and some comments
about checking if fence fired or not. This is broken by design. waitqueue_active during destruction is now fatal, since the signaller should be holding a reference in enable_signalling until it signalled the fence. Pass the original dma_fence_cb along, and call __remove_wait in the dma_fence_callback 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 __dma_fence_init 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
enable_signaling to bool. Rework dma_fence_wait. Added dma_fence_is_signaled and dma_fence_wait_timeout.
s/dma// and change exports to non GPL. Added fence_is_signaled and fence_enable_sw_signaling calls, add ability to override default wait operation.
v10: remove event_queue, use a custom list, export try_to_wake_up 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. enable_signaling is called with this lock held.
v11:
Use atomic ops for flags, lifting the need for some spin_lock_irqsaves. However I kept the guarantee that after fence_signal returns, it is guaranteed that enable_signaling 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 FENCE_TRACE, FENCE_WARN, and FENCE_ERR. This makes debugging easier. An CONFIG_DEBUG_FENCE will be added to turn off the FENCE_TRACE spam, and another runtime option can turn it off at runtime.
v12:
Add CONFIG_FENCE_TRACE. Add missing documentation for the fence->context
and fence->seqno members.v13:
Fixup CONFIG_FENCE_TRACE kconfig description. Move fence_context_alloc to fence.
Simplify fence_later.
Kill priv member to fence_cb.
v14:
Remove priv argument from fence_add_callback, oops!
v15:
Remove priv from documentation.
Explicitly include linux/atomic.h.
v16:
Add trace events.
Import changes required by android syncpoints.
v17:
Use wake_up_state instead of try_to_wake_up. (Colin Cross) Fix up commit description for seqno_fence. (Rob Clark)
--- 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
Documentation/DocBook/device-drivers.tmpl +2 -0
@@ -129,6 +129,8 @@ X!Edrivers/base/interface.c </sect1> <sect1><title>Device Drivers DMA Management</title> !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
drivers/base/Kconfig +9 -0
@@ -200,6 +200,15 @@ config DMA_SHARED_BUFFER APIs extension; the file's descriptor can then be passed on to other driver. +config FENCE_TRACE + bool "Enable verbose FENCE_TRACE messages" + depends on DMA_SHARED_BUFFER + help + Enable the FENCE_TRACE 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 DMA_CMA bool "DMA Contiguous Memory Allocator" depends on HAVE_DMA_CONTIGUOUS && CMA
drivers/base/Makefile +1 -1
@@ -10,7 +10,7 @@ obj-$(CONFIG_DMA_CMA) += dma-contiguous.o obj-y += power/ obj-$(CONFIG_HAS_DMA) += dma-mapping.o obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += dma-coherent.o -obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o reservation.o +obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o fence.o reservation.o obj-$(CONFIG_ISA) += isa.o obj-$(CONFIG_FW_LOADER) += firmware_class.o obj-$(CONFIG_NUMA) += node.o
drivers/base/fence.c new file +415 -0
415 lines not on this page · raw message
include/linux/fence.h new file +329 -0
329 lines not on this page · raw message
include/trace/events/fence.h new file +125 -0
125 lines not on this page · raw message
Re: [PATCH 1/6] fence: dma-buf cross-device synchronization (v17)
Quoted 145 lines · Maarten Lankhorst
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
page_flipioctl 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:
+fence_context_alloc()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:
+fence_signal()To have a rough approximation whether a fence is fired, call:
+fence_is_signaled()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:
+fence_add_callback()The callback can optionally be cancelled with:
+fence_remove_callback()To wait synchronously, optionally with a timeout:
+fence_wait()
+fence_wait_timeout()When emitting a fence, call:
+trace_fence_emit()To annotate that a fence is blocking on another fence, call:
+trace_fence_annotate_wait_on(fence, on_fence)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 = custom_get_fence(...); if ((seqno_fence = to_seqno_fence(fence)) != NULL) { dma_buf *fence_buf = seqno_fence->sync_buf; get_dma_buf(fence_buf);... tell the hw the memory location to wait ...
custom_wait_on(fence_buf, seqno_fence->seqno_ofs, fence->seqno);} else { /* fall-back to sw sync * / fence_add_callback(fence, my_cb);}
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.
enable_signalingcallback 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 thesw->hwsignaling path (it can be handled same assw->swcase), and therefore thefence->opscan be simplified and more handled in the core. So remove the signal,add_callback,cancel_callback, and wait ops, and replace with a simpleenable_signaling()op which can be used to inform a fence supportinghw->hwsignaling 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 inattach_fence()andget_fence()
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 thelist_headin dma-fence struct would be problematic.
v5: [ Maarten Lankhorst ] Updated for dma-bikeshed-fence and dma-buf-manager.
v6: [ Maarten Lankhorst ] I removeddma_fence_cancel_callbackand some comments
about checking if fence fired or not. This is broken by design.waitqueue_activeduring destruction is now fatal, since the signaller should be holding a reference inenable_signallinguntil it signalled the fence. Pass the originaldma_fence_cbalong, and call__remove_waitin thedma_fence_callbackhandler, so that no cleanup needs to be performed.
v7: [ Maarten Lankhorst ] Setcb->funcand 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__dma_fence_initto
header and fixed include mess.dma-fence.hnow includesdma-buf.hAll 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
enable_signalingto bool. Reworkdma_fence_wait. Addeddma_fence_is_signaledanddma_fence_wait_timeout.
s/dma// and change exports to non GPL. Addedfence_is_signaledandfence_enable_sw_signalingcalls, add ability to override default wait operation.
v10: removeevent_queue, use a custom list, exporttry_to_wake_upfrom
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.enable_signalingis called with this lock held.
v11:
Use atomic ops for flags, lifting the need for somespin_lock_irqsaves. However I kept the guarantee that afterfence_signalreturns, it is guaranteed thatenable_signalinghas 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
FENCE_TRACE,FENCE_WARN, andFENCE_ERR. This makes debugging easier. AnCONFIG_DEBUG_FENCEwill be added to turn off theFENCE_TRACEspam, and another runtime option can turn it off at runtime.
v12:Add CONFIG_FENCE_TRACE. Add missing documentation for the fence->context and fence->seqno members.v13:
FixupCONFIG_FENCE_TRACEkconfig description. Movefence_context_allocto fence.
Simplifyfence_later.
Kill priv member tofence_cb.
v14:
Remove priv argument fromfence_add_callback, oops!
v15:
Remove priv from documentation.
Explicitly includelinux/atomic.h.
v16:
Add trace events.
Import changes required by android syncpoints.
v17:
Usewake_up_stateinstead oftry_to_wake_up. (Colin Cross) Fix up commit description forseqno_fence. (Rob Clark)
Quoted 1 line
Signed-off-by Maarten Lankhorst <[email protected]>
Quoted 946 lines
--- 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.hDocumentation/DocBook/device-drivers.tmpl +2
-0@@ -129,6 +129,8 @@ X!Edrivers/base/interface.c </sect1> <sect1><title>Device Drivers DMA Management</title> !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.cdrivers/base/Kconfig +9
-0@@ -200,6 +200,15 @@ config DMA_SHARED_BUFFER APIs extension; the file's descriptor can then be passed on to other driver. +config FENCE_TRACE + bool "Enable verbose FENCE_TRACE messages" + depends on DMA_SHARED_BUFFER + help + Enable the FENCE_TRACE 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 DMA_CMA bool "DMA Contiguous Memory Allocator" depends on HAVE_DMA_CONTIGUOUS && CMAdrivers/base/Makefile +1
-1@@ -10,7 +10,7 @@ obj-$(CONFIG_DMA_CMA) += dma-contiguous.o obj-y += power/ obj-$(CONFIG_HAS_DMA) += dma-mapping.o obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += dma-coherent.o -obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o reservation.o +obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf.o fence.o reservation.o obj-$(CONFIG_ISA) += isa.o obj-$(CONFIG_FW_LOADER) += firmware_class.o obj-$(CONFIG_NUMA) += node.odrivers/base/fence.c new file +415
-0@@ -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 <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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 CREATE_TRACE_POINTS +#include <trace/events/fence.h> + +EXPORT_TRACEPOINT_SYMBOL (fence_annotate_wait_on); +EXPORT_TRACEPOINT_SYMBOL (fence_emit); + +/** +* 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 atomic_t fence_context_counter =ATOMIC_INIT (0 ); + +/** +* fence_context_alloc - 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 fence_context_alloc (unsigned num) +{ +BUG_ON (!num); +return atomic_add_return (num, &fence_context_counter) - num; +} +EXPORT_SYMBOL (fence_context_alloc); + +int __fence_signal (struct fence *fence) +{ +struct fence_cb *cur, *tmp; +int ret =0 ; + +if (WARN_ON (!fence)) +return -EINVAL ; + +if (!ktime_to_ns (fence->timestamp)) { + fence->timestamp =ktime_get (); +smp_mb__before_clear_bit (); + } + +if (test_and_set_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) { + ret = -EINVAL ; + +/* +* we might have raced with the unlocked fence_signal, +* still run through all callbacks +*/ + }else +trace_fence_signaled (fence); + +list_for_each_entry_safe (cur, tmp, &fence->cb_list, node) { +list_del_init (&cur->node); + cur->func (fence, cur); + } +return ret; +} +EXPORT_SYMBOL (__fence_signal); + +/** +* fence_signal - signal completion of a fence +* @fence: the fence to signal +* +* Signal completion for software callbacks on a fence, this will unblock +* fence_wait() calls and run all the callbacks added with +* fence_add_callback(). 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 fence_signal (struct fence *fence) +{ +unsigned long flags; + +if (!fence) +return -EINVAL ; + +if (!ktime_to_ns (fence->timestamp)) { + fence->timestamp =ktime_get (); +smp_mb__before_clear_bit (); + } + +if (test_and_set_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) +return -EINVAL ; + +trace_fence_signaled (fence); + +if (test_bit (FENCE_FLAG_ENABLE_SIGNAL_BIT , &fence->flags)) { +struct fence_cb *cur, *tmp; + +spin_lock_irqsave (fence->lock, flags); +list_for_each_entry_safe (cur, tmp, &fence->cb_list, node) { +list_del_init (&cur->node); + cur->func (fence, cur); + } +spin_unlock_irqrestore (fence->lock, flags); + } +return 0 ; +} +EXPORT_SYMBOL (fence_signal); + +/** +* fence_wait_timeout - 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 MAX_SCHEDULE_TIMEOUT +* +* 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 +fence_wait_timeout (struct fence *fence,bool intr,signed long timeout) +{ +unsigned long ret; + +if (WARN_ON (timeout <0 )) +return -EINVAL ; + +trace_fence_wait_start (fence); + ret = fence->ops->wait (fence, intr, timeout); +trace_fence_wait_end (fence); +return ret; +} +EXPORT_SYMBOL (fence_wait_timeout); + +void release_fence (struct kref *kref) +{ +struct fence *fence = + container_of(kref, struct fence, refcount); + +trace_fence_destroy (fence); + +BUG_ON (!list_empty (&fence->cb_list)); + +if (fence->ops->release) + fence->ops->release (fence); +else +kfree (fence); +} +EXPORT_SYMBOL (release_fence); + +/** +* fence_enable_sw_signaling - 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 fence_enable_sw_signaling (struct fence *fence) +{ +unsigned long flags; + +if (!test_and_set_bit (FENCE_FLAG_ENABLE_SIGNAL_BIT , &fence->flags) && + !test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) { +trace_fence_enable_signal (fence); + +spin_lock_irqsave (fence->lock, flags); + +if (!fence->ops->enable_signaling (fence)) +__fence_signal (fence); + +spin_unlock_irqrestore (fence->lock, flags); + } +} +EXPORT_SYMBOL (fence_enable_sw_signaling); + +/** +* fence_add_callback - 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 fence_add_callback, 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 fence_wait, 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 fence_signal. The callback itself can be called +* from irq context. +* +*/ +int fence_add_callback (struct fence *fence,struct fence_cb *cb, +fence_func_t func) +{ +unsigned long flags; +int ret =0 ; +bool was_set; + +if (WARN_ON (!fence || !func)) +return -EINVAL ; + +if (test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) { +INIT_LIST_HEAD (&cb->node); +return -ENOENT ; + } + +spin_lock_irqsave (fence->lock, flags); + + was_set =test_and_set_bit (FENCE_FLAG_ENABLE_SIGNAL_BIT , &fence->flags); + +if (test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) + ret = -ENOENT ; +else if (!was_set) { +trace_fence_enable_signal (fence); + +if (!fence->ops->enable_signaling (fence)) { +__fence_signal (fence); + ret = -ENOENT ; + } + } + +if (!ret) { + cb->func = func; +list_add_tail (&cb->node, &fence->cb_list); + }else +INIT_LIST_HEAD (&cb->node); +spin_unlock_irqrestore (fence->lock, flags); + +return ret; +} +EXPORT_SYMBOL (fence_add_callback); + +/** +* fence_remove_callback - 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 +fence_remove_callback (struct fence *fence,struct fence_cb *cb) +{ +unsigned long flags; +bool ret; + +spin_lock_irqsave (fence->lock, flags); + + ret = !list_empty (&cb->node); +if (ret) +list_del_init (&cb->node); + +spin_unlock_irqrestore (fence->lock, flags); + +return ret; +} +EXPORT_SYMBOL (fence_remove_callback); + +struct default_wait_cb { +struct fence_cb base; +struct task_struct *task; +}; + +static void +fence_default_wait_cb (struct fence *fence,struct fence_cb *cb) +{ +struct default_wait_cb *wait = + container_of(cb, struct default_wait_cb, base); + +wake_up_state (wait->task,TASK_NORMAL ); +} + +/** +* fence_default_wait - 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 MAX_SCHEDULE_TIMEOUT +* +* Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the +* remaining timeout in jiffies on success. +*/ +long +fence_default_wait (struct fence *fence,bool intr,signed long timeout) +{ +struct default_wait_cb cb; +unsigned long flags; +long ret = timeout; +bool was_set; + +if (test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) +return timeout; + +spin_lock_irqsave (fence->lock, flags); + +if (intr &&signal_pending (current)) { + ret = -ERESTARTSYS ; + goto out; + } + + was_set =test_and_set_bit (FENCE_FLAG_ENABLE_SIGNAL_BIT , &fence->flags); + +if (test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) + goto out; + +if (!was_set) { +trace_fence_enable_signal (fence); + +if (!fence->ops->enable_signaling (fence)) { +__fence_signal (fence); + goto out; + } + } + + cb.base.func = fence_default_wait_cb; + cb.task = current; +list_add (&cb.base.node, &fence->cb_list); + +while (!test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags) && ret >0 ) { +if (intr) +__set_current_state (TASK_INTERRUPTIBLE ); +else +__set_current_state (TASK_UNINTERRUPTIBLE ); +spin_unlock_irqrestore (fence->lock, flags); + + ret =schedule_timeout (ret); + +spin_lock_irqsave (fence->lock, flags); +if (ret >0 && intr &&signal_pending (current)) + ret = -ERESTARTSYS ; + } + +if (!list_empty (&cb.base.node)) +list_del (&cb.base.node); +__set_current_state (TASK_RUNNING ); + +out: +spin_unlock_irqrestore (fence->lock, flags); +return ret; +} +EXPORT_SYMBOL (fence_default_wait); + +/** +* __fence_init - Initialize a custom fence. +* @fence: [in] the fence to initialize +* @ops: [in] the fence_ops 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 fence_ops.enable_signaling 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 fence_later. +*/ +void +__fence_init (struct fence *fence,const struct fence_ops *ops, +spinlock_t *lock,unsigned context,unsigned seqno) +{ +BUG_ON (!lock); +BUG_ON (!ops || !ops->wait || !ops->enable_signaling || !ops->get_driver_name || !ops->get_timeline_name); + +kref_init (&fence->refcount); + fence->ops = ops; +INIT_LIST_HEAD (&fence->cb_list); + fence->lock = lock; + fence->context = context; + fence->seqno = seqno; + fence->flags =0UL ; + +trace_fence_init (fence); +} +EXPORT_SYMBOL (__fence_init);include/linux/fence.h new file +329
-0@@ -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 <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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 __LINUX_FENCE_H +#define __LINUX_FENCE_H + +#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 fence_ops ; +struct fence_cb ; + +/** +* struct fence - software synchronization primitive +* @refcount: refcount for this fence +* @ops: fence_ops associated with this fence +* @cb_list: list of all callbacks to call +* @lock: spin_lock_irqsave used for locking +* @context: execution context this fence belongs to, returned by +* fence_context_alloc() +* @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 FENCE_FLAG_* defined below +* @timestamp: Timestamp when the fence was signaled. +* @status: Optional, only valid if < 0, must be set before calling +* fence_signal, 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. +* +* FENCE_FLAG_SIGNALED_BIT - fence is already signaled +* FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called* +* FENCE_FLAG_USER_BITS - 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 fence_signal was called right +* before this bit was set, it would have been able to set the +* FENCE_FLAG_SIGNALED_BIT, before enable_signaling was called. +* Adding a check for FENCE_FLAG_SIGNALED_BIT after setting +* FENCE_FLAG_ENABLE_SIGNAL_BIT closes this race, and makes sure that +* after fence_signal was called, any enable_signaling call will have either +* been completed, or never called at all. +*/ +struct fence { +struct kref refcount; +const struct fence_ops *ops; +struct list_head cb_list; +spinlock_t *lock; +unsigned context, seqno; +unsigned long flags; +ktime_t timestamp; +int status; +}; + +enum fence_flag_bits { +FENCE_FLAG_SIGNALED_BIT , +FENCE_FLAG_ENABLE_SIGNAL_BIT , +FENCE_FLAG_USER_BITS ,/* must always be last member */ +}; + +typedef void (*fence_func_t )(struct fence *fence,struct fence_cb *cb); + +/** +* struct fence_cb - callback for fence_add_callback +* @node: used by fence_add_callback to append this struct to fence::cb_list +* @func: fence_func_t to call +* +* This struct will be initialized by fence_add_callback, additional +* data can be passed along by embedding fence_cb in another struct. +*/ +struct fence_cb { +struct list_head node; +fence_func_t func; +}; + +/** +* struct fence_ops - operations implemented for fence +* @get_driver_name: returns the driver name. +* @get_timeline_name: return the name of the context this fence belongs to. +* @enable_signaling: enable software signaling of fence. +* @signaled: [optional] peek whether the fence is signaled, can be null. +* @wait: custom wait implementation, or fence_default_wait. +* @release: [optional] called on destruction of fence, can be null +* @fill_driver_data: [optional] callback to fill in free-form debug info +* Returns amount of bytes filled, or -errno. +* @fence_value_str: [optional] fills in the value of the fence as a string +* @timeline_value_str: [optional] fills in the current value of the timeline as a string +* +* Notes on enable_signaling: +* 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 add_callback() 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 enable_signaling, but only when false is returned. +* +* Calling fence_signal before enable_signaling is called allows +* for a tiny race window in which enable_signaling is called during, +* before, or after fence_signal. To fight this, it is recommended +* that before enable_signaling returns true an extra reference is +* taken on the fence, to be released when the fence is signaled. +* This will mean fence_signal 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 fence_default_wait for default implementation. +* the fence_default_wait implementation should work for any fence, as long +* as enable_signaling 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 fence_ops { +const char *(*get_driver_name)(struct fence *fence); +const char *(*get_timeline_name)(struct fence *fence); +bool (*enable_signaling)(struct fence *fence); +bool (*signaled)(struct fence *fence); +long (*wait)(struct fence *fence,bool intr,signed long timeout); +void (*release)(struct fence *fence); + +int (*fill_driver_data)(struct fence *fence,void *data,int size); +void (*fence_value_str)(struct fence *fence,char *str,int size); +void (*timeline_value_str)(struct fence *fence,char *str,int size); +}; + +void __fence_init (struct fence *fence,const struct fence_ops *ops, +spinlock_t *lock,unsigned context,unsigned seqno); + +/** +* fence_get - increases refcount of the fence +* @fence: [in] fence to increase refcount of +*/ +static inline void fence_get (struct fence *fence) +{ +if (WARN_ON (!fence)) +return ; +kref_get (&fence->refcount); +} + +extern void release_fence (struct kref *kref); + +/** +* fence_put - decreases refcount of the fence +* @fence: [in] fence to reduce refcount of +*/ +static inline void fence_put (struct fence *fence) +{ +if (WARN_ON (!fence)) +return ; +kref_put (&fence->refcount, release_fence); +} + +int fence_signal (struct fence *fence); +int __fence_signal (struct fence *fence); +long fence_default_wait (struct fence *fence,bool intr,signed long timeout); +int fence_add_callback (struct fence *fence,struct fence_cb *cb, +fence_func_t func); +bool fence_remove_callback (struct fence *fence,struct fence_cb *cb); +void fence_enable_sw_signaling (struct fence *fence); + +static inline bool +__fence_is_signaled (struct fence *fence) +{ +if (test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) +return true; + +if (fence->ops->signaled && fence->ops->signaled (fence)) { +__fence_signal (fence); +return true; + } + +return false; +} + +/** +* fence_is_signaled - 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 fence_add_callback, fence_wait or fence_enable_sw_signaling +* haven't been called before. +* +* It's recommended for seqno fences to call fence_signal 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 +fence_is_signaled (struct fence *fence) +{ +if (test_bit (FENCE_FLAG_SIGNALED_BIT , &fence->flags)) +return true; + +if (fence->ops->signaled && fence->ops->signaled (fence)) { +fence_signal (fence); +return true; + } + +return false; +} + +/** +* fence_later - 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 *fence_later (struct fence *f1,struct fence *f2) +{ +BUG_ON (f1->context != f2->context); + +/* +* can't check just FENCE_FLAG_SIGNALED_BIT here, it may never have been +* set called if enable_signaling wasn't, and enabling that here is +* overkill. +*/ +if (f2->seqno - f1->seqno <=INT_MAX ) +return fence_is_signaled (f2) ?NULL : f2; +else +return fence_is_signaled (f1) ?NULL : f1; +} + +long fence_wait_timeout (struct fence *fence,bool intr,signed long timeout); + + +/** +* fence_wait - 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 fence_wait (struct fence *fence,bool intr) +{ +long ret; + +/* Since fence_wait_timeout cannot timeout with +* MAX_SCHEDULE_TIMEOUT, only valid return values are +* -ERESTARTSYS and MAX_SCHEDULE_TIMEOUT. +*/ + ret =fence_wait_timeout (fence, intr,MAX_SCHEDULE_TIMEOUT ); + +return ret <0 ? ret :0 ; +} + +unsigned fence_context_alloc (unsigned num); + +#define FENCE_TRACE(f, fmt, args...) \ + do { \ + struct fence *__ff = (f); \ + if (config_enabled(CONFIG_FENCE_TRACE)) \ + pr_info("f %u#%u: " fmt, \ + __ff->context, __ff->seqno, ##args); \ + } while (0) + +#define FENCE_WARN(f, fmt, args...) \ + do { \ + struct fence *__ff = (f); \ + pr_warn("f %u#%u: " fmt, __ff->context, __ff->seqno, ##args); \ + } while (0) + +#define FENCE_ERR(f, fmt, args...) \ + do { \ + struct fence *__ff = (f); \ + pr_err("f %u#%u: " fmt, __ff->context, __ff->seqno, ##args); \ + } while (0) + +#endif /* __LINUX_FENCE_H */ include/trace/events/fence.h new file +125
-0@@ -0,0 +1,125 @@ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM fence + +#if !defined(_TRACE_FENCE_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_FENCE_H + +#include <linux/tracepoint.h> + +struct fence ; + +TRACE_EVENT (fence_annotate_wait_on, + +/* fence: the fence waiting on f1, f1: the fence to be waited on. */ + TP_PROTO(struct fence *fence, struct fence *f1), + +TP_ARGS (fence, f1), + +TP_STRUCT__entry ( + __string(driver, fence->ops->get_driver_name(fence)) + __string(timeline, fence->ops->get_driver_name(fence)) + __field(unsigned int, context) + __field(unsigned int, seqno) + + __string(waiting_driver, f1->ops->get_driver_name(f1)) + __string(waiting_timeline, f1->ops->get_timeline_name(f1)) + __field(unsigned int, waiting_context) + __field(unsigned int, waiting_seqno) + ), + +TP_fast_assign ( + __assign_str(driver, fence->ops->get_driver_name(fence)) + __assign_str(timeline, fence->ops->get_timeline_name(fence)) + __entry->context = fence->context; + __entry->seqno = fence->seqno; + + __assign_str(waiting_driver, f1->ops->get_driver_name(f1)) + __assign_str(waiting_timeline, f1->ops->get_timeline_name(f1)) + __entry->waiting_context = f1->context; + __entry->waiting_seqno = f1->seqno; + + ), + +TP_printk ("driver=%s timeline=%s context=%u seqno=%u " +"waits on driver=%s timeline=%s context=%u seqno=%u" , +__get_str (driver),__get_str (timeline), __entry->context, __entry->seqno, +__get_str (waiting_driver),__get_str (waiting_timeline), __entry->waiting_context, __entry->waiting_seqno) +); + +DECLARE_EVENT_CLASS (fence, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence), + +TP_STRUCT__entry ( + __string(driver, fence->ops->get_driver_name(fence)) + __string(timeline, fence->ops->get_timeline_name(fence)) + __field(unsigned int, context) + __field(unsigned int, seqno) + ), + +TP_fast_assign ( + __assign_str(driver, fence->ops->get_driver_name(fence)) + __assign_str(timeline, fence->ops->get_timeline_name(fence)) + __entry->context = fence->context; + __entry->seqno = fence->seqno; + ), + +TP_printk ("driver=%s timeline=%s context=%u seqno=%u" , +__get_str (driver),__get_str (timeline), __entry->context, __entry->seqno) +); + +DEFINE_EVENT (fence, fence_emit, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence) +); + +DEFINE_EVENT (fence, fence_init, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence) +); + +DEFINE_EVENT (fence, fence_destroy, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence) +); + +DEFINE_EVENT (fence, fence_enable_signal, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence) +); + +DEFINE_EVENT (fence, fence_signaled, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence) +); + +DEFINE_EVENT (fence, fence_wait_start, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence) +); + +DEFINE_EVENT (fence, fence_wait_end, + + TP_PROTO(struct fence *fence), + +TP_ARGS (fence) +); + +#endif /* _TRACE_FENCE_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h>
PATCH 2/6 seqno-fence: Hardware dma-buf implementation of fencing (v4)
In reply to Maarten LankhorstThis type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose this for graphics cards that have an op to support this.
Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
v1: Original
v2: Renamed from bikeshed to seqno, moved into dma-fence.c since
not much was left of the file. Lots of documentation added.
v3: Use fence_ops instead of custom callbacks. Moved to own file
to avoid circular dependency between dma-buf.h and fence.h
v4: Add spinlock pointer to seqno_fence_init
--- Documentation/DocBook/device-drivers.tmpl | 1 drivers/base/fence.c | 50 +++++++++++++ include/linux/seqno-fence.h | 109 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 include/linux/seqno-fence.h
Documentation/DocBook/device-drivers.tmpl +1 -0
@@ -131,6 +131,7 @@ X!Edrivers/base/interface.c !Edrivers/base/dma-buf.c !Edrivers/base/fence.c !Iinclude/linux/fence.h +!Iinclude/linux/seqno-fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.c
drivers/base/fence.c +50 -0
59 lines not on this page · raw message
include/linux/seqno-fence.h new file +109 -0
109 lines not on this page · raw message
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
Quoted 13 lines · Maarten Lankhorst
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose this for graphics cards that have an op to support this.
Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
Quoted 8 lines
v1: Original
v2: Renamed from bikeshed to seqno, moved intodma-fence.csince
not much was left of the file. Lots of documentation added.
v3: Usefence_opsinstead of custom callbacks. Moved to own file
to avoid circular dependency betweendma-buf.handfence.h
v4: Add spinlock pointer toseqno_fence_initSigned-off-by Maarten Lankhorst <[email protected]>
Quoted 200 lines
--- Documentation/DocBook/device-drivers.tmpl | 1 drivers/base/fence.c | 50 +++++++++++++ include/linux/seqno-fence.h | 109 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 include/linux/seqno-fence.hDocumentation/DocBook/device-drivers.tmpl +1
-0@@ -131,6 +131,7 @@ X!Edrivers/base/interface.c !Edrivers/base/dma-buf.c !Edrivers/base/fence.c !Iinclude/linux/fence.h +!Iinclude/linux/seqno-fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.cdrivers/base/fence.c +50
-0@@ -25,6 +25,7 @@#include <linux/export.h> #include <linux/atomic.h> #include <linux/fence.h> +#include <linux/seqno-fence.h> #define CREATE_TRACE_POINTS #include <trace/events/fence.h> @@ -413,3 +414,52 @@ __fence_init(struct fence *fence, const struct fence_ops *ops,trace_fence_init (fence); }EXPORT_SYMBOL (__fence_init); + +static const char *seqno_fence_get_driver_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_driver_name (fence); +} + +static const char *seqno_fence_get_timeline_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_timeline_name (fence); +} + +static bool seqno_enable_signaling (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->enable_signaling (fence); +} + +static bool seqno_signaled (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->signaled && seqno_fence->ops->signaled (fence); +} + +static void seqno_release (struct fence *fence) +{ +struct seqno_fence *f =to_seqno_fence (fence); + +dma_buf_put (f->sync_buf); +if (f->ops->release) + f->ops->release (fence); +else +kfree (f); +} + +static long seqno_wait (struct fence *fence,bool intr,signed long timeout) +{ +struct seqno_fence *f =to_seqno_fence (fence); +return f->ops->wait (fence, intr, timeout); +} + +const struct fence_ops seqno_fence_ops = { + .get_driver_name = seqno_fence_get_driver_name, + .get_timeline_name = seqno_fence_get_timeline_name, + .enable_signaling = seqno_enable_signaling, + .signaled = seqno_signaled, + .wait = seqno_wait, + .release = seqno_release, +}; +EXPORT_SYMBOL (seqno_fence_ops);include/linux/seqno-fence.h new file +109
-0@@ -0,0 +1,109 @@ +/* +* seqno-fence, using a dma-buf to synchronize fencing +* +* Copyright (C) 2012 Texas Instruments +* Copyright (C) 2012 Canonical Ltd +* Authors: +* Rob Clark <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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 __LINUX_SEQNO_FENCE_H +#define __LINUX_SEQNO_FENCE_H + +#include <linux/fence.h> +#include <linux/dma-buf.h> + +struct seqno_fence { +struct fence base; + +const struct fence_ops *ops; +struct dma_buf *sync_buf; +uint32_t seqno_ofs; +}; + +extern const struct fence_ops seqno_fence_ops; + +/** +* to_seqno_fence - cast a fence to a seqno_fence +* @fence: fence to cast to a seqno_fence +* +* Returns NULL if the fence is not a seqno_fence, +* or the seqno_fence otherwise. +*/ +static inline struct seqno_fence * +to_seqno_fence (struct fence *fence) +{ +if (fence->ops != &seqno_fence_ops) +return NULL ; + return container_of(fence, struct seqno_fence, base); +} + +/** +* seqno_fence_init - initialize a seqno fence +* @fence: seqno_fence to initialize +* @lock: pointer to spinlock to use for fence +* @sync_buf: buffer containing the memory location to signal on +* @context: the execution context this fence is a part of +* @seqno_ofs: the offset within @sync_buf +* @seqno: the sequence # to signal on +* @ops: the fence_ops for operations on this seqno fence +* +* This function initializes a struct seqno_fence with passed parameters, +* and takes a reference on sync_buf which is released on fence destruction. +* +* A seqno_fence is a dma_fence which can complete in software when +* enable_signaling is called, but it also completes when +* (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true +* +* The seqno_fence will take a refcount on the sync_buf until it's +* destroyed, but actual lifetime of sync_buf may be longer if one of the +* callers take a reference to it. +* +* Certain hardware have instructions to insert this type of wait condition +* in the command stream, so no intervention from software would be needed. +* This type of fence can be destroyed before completed, however a reference +* on the sync_buf dma-buf can be taken. It is encouraged to re-use the same +* dma-buf for sync_buf, since mapping or unmapping the sync_buf to the +* device's vm can be expensive. +* +* It is recommended for creators of seqno_fence to call fence_signal +* before destruction. This will prevent possible issues from wraparound at +* time of issue vs time of check, since users can check fence_is_signaled +* before submitting instructions for the hardware to wait on the fence. +* However, when ops.enable_signaling is not called, it doesn't have to be +* done as soon as possible, just before there's any real danger of seqno +* wraparound. +*/ +static inline void +seqno_fence_init (struct seqno_fence *fence,spinlock_t *lock, +struct dma_buf *sync_buf,uint32_t context,uint32_t seqno_ofs, +uint32_t seqno,const struct fence_ops *ops) +{ +BUG_ON (!fence || !sync_buf || !ops); +BUG_ON (!ops->wait || !ops->enable_signaling || !ops->get_driver_name || !ops->get_timeline_name); + +/* +* ops is used in __fence_init for get_driver_name, so needs to be +* initialized first +*/ + fence->ops = ops; +__fence_init (&fence->base, &seqno_fence_ops, lock, context, seqno); +get_dma_buf (sync_buf); + fence->sync_buf = sync_buf; + fence->seqno_ofs = seqno_ofs; +} + +#endif /* __LINUX_SEQNO_FENCE_H */
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
In reply to Maarten LankhorstAm 17.02.2014 16:56, schrieb Maarten Lankhorst:
Quoted 3 lines
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
Can't we make that just "dma_buf[offset] != 0" instead? As far as I know this way it would match the definition M$ uses in their WDDM specification and so make it much more likely that hardware supports it.
Apart from that I still don't like the idea of leaking a drivers IRQ context outside of the driver, but without a proper GPU scheduler there probably isn't much alternative.
Christian.
Quoted 222 lines
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose this for graphics cards that have an op to support this.
Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
v1: Original
v2: Renamed from bikeshed to seqno, moved intodma-fence.csince
not much was left of the file. Lots of documentation added.
v3: Usefence_opsinstead of custom callbacks. Moved to own file
to avoid circular dependency betweendma-buf.handfence.h
v4: Add spinlock pointer toseqno_fence_initSigned-off-by Maarten Lankhorst <[email protected]>--- Documentation/DocBook/device-drivers.tmpl | 1 drivers/base/fence.c | 50 +++++++++++++ include/linux/seqno-fence.h | 109 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+)create mode 100644
include/linux/seqno-fence.hDocumentation/DocBook/device-drivers.tmpl +1
-0@@ -131,6 +131,7 @@ X!Edrivers/base/interface.c !Edrivers/base/dma-buf.c !Edrivers/base/fence.c !Iinclude/linux/fence.h +!Iinclude/linux/seqno-fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.cdrivers/base/fence.c +50
-0@@ -25,6 +25,7 @@#include <linux/export.h> #include <linux/atomic.h> #include <linux/fence.h> +#include <linux/seqno-fence.h> #define CREATE_TRACE_POINTS #include <trace/events/fence.h> @@ -413,3 +414,52 @@ __fence_init(struct fence *fence, const struct fence_ops *ops,trace_fence_init (fence); }EXPORT_SYMBOL (__fence_init); + +static const char *seqno_fence_get_driver_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_driver_name (fence); +} + +static const char *seqno_fence_get_timeline_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_timeline_name (fence); +} + +static bool seqno_enable_signaling (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->enable_signaling (fence); +} + +static bool seqno_signaled (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->signaled && seqno_fence->ops->signaled (fence); +} + +static void seqno_release (struct fence *fence) +{ +struct seqno_fence *f =to_seqno_fence (fence); + +dma_buf_put (f->sync_buf); +if (f->ops->release) + f->ops->release (fence); +else +kfree (f); +} + +static long seqno_wait (struct fence *fence,bool intr,signed long timeout) +{ +struct seqno_fence *f =to_seqno_fence (fence); +return f->ops->wait (fence, intr, timeout); +} + +const struct fence_ops seqno_fence_ops = { + .get_driver_name = seqno_fence_get_driver_name, + .get_timeline_name = seqno_fence_get_timeline_name, + .enable_signaling = seqno_enable_signaling, + .signaled = seqno_signaled, + .wait = seqno_wait, + .release = seqno_release, +}; +EXPORT_SYMBOL (seqno_fence_ops);include/linux/seqno-fence.h new file +109
-0@@ -0,0 +1,109 @@ +/* +* seqno-fence, using a dma-buf to synchronize fencing +* +* Copyright (C) 2012 Texas Instruments +* Copyright (C) 2012 Canonical Ltd +* Authors: +* Rob Clark <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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 __LINUX_SEQNO_FENCE_H +#define __LINUX_SEQNO_FENCE_H + +#include <linux/fence.h> +#include <linux/dma-buf.h> + +struct seqno_fence { +struct fence base; + +const struct fence_ops *ops; +struct dma_buf *sync_buf; +uint32_t seqno_ofs; +}; + +extern const struct fence_ops seqno_fence_ops; + +/** +* to_seqno_fence - cast a fence to a seqno_fence +* @fence: fence to cast to a seqno_fence +* +* Returns NULL if the fence is not a seqno_fence, +* or the seqno_fence otherwise. +*/ +static inline struct seqno_fence * +to_seqno_fence (struct fence *fence) +{ +if (fence->ops != &seqno_fence_ops) +return NULL ; + return container_of(fence, struct seqno_fence, base); +} + +/** +* seqno_fence_init - initialize a seqno fence +* @fence: seqno_fence to initialize +* @lock: pointer to spinlock to use for fence +* @sync_buf: buffer containing the memory location to signal on +* @context: the execution context this fence is a part of +* @seqno_ofs: the offset within @sync_buf +* @seqno: the sequence # to signal on +* @ops: the fence_ops for operations on this seqno fence +* +* This function initializes a struct seqno_fence with passed parameters, +* and takes a reference on sync_buf which is released on fence destruction. +* +* A seqno_fence is a dma_fence which can complete in software when +* enable_signaling is called, but it also completes when +* (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true +* +* The seqno_fence will take a refcount on the sync_buf until it's +* destroyed, but actual lifetime of sync_buf may be longer if one of the +* callers take a reference to it. +* +* Certain hardware have instructions to insert this type of wait condition +* in the command stream, so no intervention from software would be needed. +* This type of fence can be destroyed before completed, however a reference +* on the sync_buf dma-buf can be taken. It is encouraged to re-use the same +* dma-buf for sync_buf, since mapping or unmapping the sync_buf to the +* device's vm can be expensive. +* +* It is recommended for creators of seqno_fence to call fence_signal +* before destruction. This will prevent possible issues from wraparound at +* time of issue vs time of check, since users can check fence_is_signaled +* before submitting instructions for the hardware to wait on the fence. +* However, when ops.enable_signaling is not called, it doesn't have to be +* done as soon as possible, just before there's any real danger of seqno +* wraparound. +*/ +static inline void +seqno_fence_init (struct seqno_fence *fence,spinlock_t *lock, +struct dma_buf *sync_buf,uint32_t context,uint32_t seqno_ofs, +uint32_t seqno,const struct fence_ops *ops) +{ +BUG_ON (!fence || !sync_buf || !ops); +BUG_ON (!ops->wait || !ops->enable_signaling || !ops->get_driver_name || !ops->get_timeline_name); + +/* +* ops is used in __fence_init for get_driver_name, so needs to be +* initialized first +*/ + fence->ops = ops; +__fence_init (&fence->base, &seqno_fence_ops, lock, context, seqno); +get_dma_buf (sync_buf); + fence->sync_buf = sync_buf; + fence->seqno_ofs = seqno_ofs; +} + +#endif /* __LINUX_SEQNO_FENCE_H */ _______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
Quoted 10 lines · Christian König
Am 17.02.2014 16:56, schrieb Maarten Lankhorst:
Quoted 3 lines
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
Can't we make that just "dma_buf[offset] != 0" instead? As far as I know this way it would match the definition M$ uses in their WDDM specification and so make it much more likely that hardware supports it.
well 'buf[offset] >= value' at least means the same slot can be used for multiple operations (with increasing values of 'value').. not sure if that is something people care about.
Quoted 6 lines
=value seems to be possible with adreno and radeon. I'm not really sure about others (although I presume it as least supported for nv desktop stuff). For hw that cannot do >=value, we can either have a different fence implementation which uses the !=0 approach. Or change seqno-fence implementation later if needed. But if someone has hw that can do !=0 but not >=value, speak up now ;-)
Apart from that I still don't like the idea of leaking a drivers IRQ context outside of the driver, but without a proper GPU scheduler there probably isn't much alternative.
I guess it will be not uncommon scenario for gpu device to just need to kick display device to write a few registers for a page flip.. probably best not to schedule a worker just for this (unless the signalled device otherwise needs to). I think it is better in this case to give the signalee some rope to hang themselves, and make it the responsibility of the callback to kick things off to a worker if needed.
BR,
-R
Quoted 250 lines
Christian.
Quoted 238 lines
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose this for graphics cards that have an op to support this.
Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
v1: Original
v2: Renamed from bikeshed to seqno, moved intodma-fence.csince
not much was left of the file. Lots of documentation added.
v3: Usefence_opsinstead of custom callbacks. Moved to own file
to avoid circular dependency betweendma-buf.handfence.h
v4: Add spinlock pointer toseqno_fence_initSigned-off-by Maarten Lankhorst <[email protected]>--- Documentation/DocBook/device-drivers.tmpl | 1 drivers/base/fence.c | 50 +++++++++++++ include/linux/seqno-fence.h | 109 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+)create mode 100644
include/linux/seqno-fence.hDocumentation/DocBook/device-drivers.tmpl +1
-0@@ -131,6 +131,7 @@ X!Edrivers/base/interface.c !Edrivers/base/dma-buf.c !Edrivers/base/fence.c !Iinclude/linux/fence.h +!Iinclude/linux/seqno-fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.cdrivers/base/fence.c +22
-0@@ -25,6 +25,7 @@#include <linux/export.h> #include <linux/atomic.h> #include <linux/fence.h> +#include <linux/seqno-fence.h> #define CREATE_TRACE_POINTS #include <trace/events/fence.h> @@ -413,3 +414,52 @@ __fence_init(struct fence *fence, const structfence_ops *ops,trace_fence_init (fence ); }EXPORT_SYMBOL (__fence_init); + +static const char *seqno_fence_get_driver_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_driver_name (fence); +} + +static const char *seqno_fence_get_timeline_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_timeline_name (fence); +} + +static bool seqno_enable_signaling (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->enable_signaling (fence); +} + +static bool seqno_signaled (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); + return seqno_fence->ops->signaled &&
seqno_fence->ops->signaled(fence);drivers/base/fence.c +14
-0+} + +static void seqno_release (struct fence *fence) +{ +struct seqno_fence *f =to_seqno_fence (fence); + +dma_buf_put (f->sync_buf); +if (f->ops->release) + f->ops->release (fence); +else +kfree (f); +} + +static long seqno_wait(struct fence *fence, bool intr, signed longtimeout)
drivers/base/fence.c +14
-0+{ +struct seqno_fence *f =to_seqno_fence (fence); +return f->ops->wait (fence, intr, timeout); +} + +const struct fence_ops seqno_fence_ops = { + .get_driver_name = seqno_fence_get_driver_name, + .get_timeline_name = seqno_fence_get_timeline_name, + .enable_signaling = seqno_enable_signaling, + .signaled = seqno_signaled, + .wait = seqno_wait, + .release = seqno_release, +}; +EXPORT_SYMBOL (seqno_fence_ops);include/linux/seqno-fence.h new file +10
-0@@ -0,0 +1,109 @@ +/* +* seqno-fence, using a dma-buf to synchronize fencing +* +* Copyright (C) 2012 Texas Instruments +* Copyright (C) 2012 Canonical Ltd +* Authors: +* Rob Clark <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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/seqno-fence.h new file +44
-0+ +#ifndef __LINUX_SEQNO_FENCE_H +#define __LINUX_SEQNO_FENCE_H + +#include <linux/fence.h> +#include <linux/dma-buf.h> + +struct seqno_fence { +struct fence base; + +const struct fence_ops *ops; +struct dma_buf *sync_buf; +uint32_t seqno_ofs; +}; + +extern const struct fence_ops seqno_fence_ops; + +/** +* to_seqno_fence - cast a fence to a seqno_fence +* @fence: fence to cast to a seqno_fence +* +* Returns NULL if the fence is not a seqno_fence, +* or the seqno_fence otherwise. +*/ +static inline struct seqno_fence * +to_seqno_fence (struct fence *fence) +{ +if (fence->ops != &seqno_fence_ops) +return NULL ; + return container_of(fence, struct seqno_fence, base); +} + +/** +* seqno_fence_init - initialize a seqno fence +* @fence: seqno_fence to initialize +* @lock: pointer to spinlock to use for fence +* @sync_buf: buffer containing the memory location to signal on +* @context: the execution context this fence is a part of +* @seqno_ofs: the offset within @sync_buf +* @seqno: the sequence # to signal on +* @ops: the fence_ops for operations on this seqno fence +* +* This function initializes a struct seqno_fence with passed parameters, +* and takes a reference on sync_buf which is released on fence destruction.
+ *
+ * Aseqno_fenceis adma_fencewhich can complete in software when
+ *enable_signalingis called, but it also completes when
+ * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
+ *
+ * Theseqno_fencewill take a refcount on thesync_bufuntil it's
+ * destroyed, but actual lifetime ofsync_bufmay be longer if one of the
+ * callers take a reference to it.
+ *
+ * Certain hardware have instructions to insert this type of wait condition
+ * in the command stream, so no intervention from software would be needed.
+ * This type of fence can be destroyed before completed, however a reference
+ * on thesync_bufdma-buf can be taken. It is encouraged to re-use the same
+ * dma-buf forsync_buf, since mapping or unmapping thesync_bufto the
+ * device's vm can be expensive.
+ *
+ * It is recommended for creators ofseqno_fenceto callfence_signal
+ * before destruction. This will prevent possible issues from wraparound at
+ * time of issue vs time of check, since users can checkfence_is_signaled
+ * before submitting instructions for the hardware to wait on the fence.
+ * However, when ops.enable_signaling is not called, it doesn't have to be
+ * done as soon as possible, just before there's any real danger of seqno
+ * wraparound.
+ */include/linux/seqno-fence.h new file +3
-0+static inline void +seqno_fence_init (struct seqno_fence *fence,spinlock_t *lock, + struct dma_buf *sync_buf, uint32_t context, uint32_t
seqno_ofs,include/linux/seqno-fence.h new file +4
-0+ uint32_t seqno, const struct fence_ops *ops) +{ + BUG_ON(!fence || !sync_buf || !ops); + BUG_ON(!ops->wait || !ops->enable_signaling ||!ops->get_driver_name || !ops->get_timeline_name);
include/linux/seqno-fence.h new file +7
-0+ +/* +* ops is used in __fence_init for get_driver_name, so needs to be +* initialized first +*/ + fence->ops = ops; + __fence_init(&fence->base, &seqno_fence_ops, lock, context,seqno);
include/linux/seqno-fence.h new file +6
-0+get_dma_buf (sync_buf); + fence->sync_buf = sync_buf; + fence->seqno_ofs = seqno_ofs; +} + +#endif /* __LINUX_SEQNO_FENCE_H */ Quoted 4 lines
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
Am 17.02.2014 18:27, schrieb Rob Clark:
Quoted 8 lines
Quoted 6 lines · Christian König
Am 17.02.2014 16:56, schrieb Maarten Lankhorst:
Quoted 3 lines
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
Quoted 8 lines
Quoted 3 lines
Can't we make that just "dma_buf[offset] != 0" instead? As far as I know this way it would match the definition M$ uses in their WDDM specification and so make it much more likely that hardware supports it.
well 'buf[offset] >= value' at least means the same slot can be used for multiple operations (with increasing values of 'value').. not sure if that is something people care about.
Quoted 1 line
=value seems to be possible with adreno and radeon. I'm not really sure about others (although I presume it as least supported for nv desktop stuff). For hw that cannot do >=value, we can either have a different fence implementation which uses the !=0 approach. Or change seqno-fence implementation later if needed. But if someone has hw that can do !=0 but not >=value, speak up now ;-)
Here! Radeon can only do >=value on the DMA and 3D engine, but not with UVD or VCE. And for the 3D engine it means draining the pipe, which isn't really a good idea.
Christian.
Quoted 263 lines
Quoted 4 lines
Apart from that I still don't like the idea of leaking a drivers IRQ context outside of the driver, but without a proper GPU scheduler there probably isn't much alternative.
I guess it will be not uncommon scenario for gpu device to just need to kick display device to write a few registers for a page flip.. probably best not to schedule a worker just for this (unless the signalled device otherwise needs to). I think it is better in this case to give the signalee some rope to hang themselves, and make it the responsibility of the callback to kick things off to a worker if needed.
BR,
-RQuoted 248 lines
Christian.
Quoted 237 lines
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose this for graphics cards that have an op to support this.
Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
v1: Original
v2: Renamed from bikeshed to seqno, moved intodma-fence.csince
not much was left of the file. Lots of documentation added.
v3: Usefence_opsinstead of custom callbacks. Moved to own file
to avoid circular dependency betweendma-buf.handfence.h
v4: Add spinlock pointer toseqno_fence_initSigned-off-by Maarten Lankhorst <[email protected]>--- Documentation/DocBook/device-drivers.tmpl | 1 drivers/base/fence.c | 50 +++++++++++++ include/linux/seqno-fence.h | 109 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+)create mode 100644
include/linux/seqno-fence.hDocumentation/DocBook/device-drivers.tmpl +1
-0@@ -131,6 +131,7 @@ X!Edrivers/base/interface.c !Edrivers/base/dma-buf.c !Edrivers/base/fence.c !Iinclude/linux/fence.h +!Iinclude/linux/seqno-fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.cdrivers/base/fence.c +22
-0@@ -25,6 +25,7 @@#include <linux/export.h> #include <linux/atomic.h> #include <linux/fence.h> +#include <linux/seqno-fence.h> #define CREATE_TRACE_POINTS #include <trace/events/fence.h> @@ -413,3 +414,52 @@ __fence_init(struct fence *fence, const structfence_ops *ops,trace_fence_init (fence ); }EXPORT_SYMBOL (__fence_init); + +static const char *seqno_fence_get_driver_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_driver_name (fence); +} + +static const char *seqno_fence_get_timeline_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_timeline_name (fence); +} + +static bool seqno_enable_signaling (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->enable_signaling (fence); +} + +static bool seqno_signaled (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); + return seqno_fence->ops->signaled &&
seqno_fence->ops->signaled(fence);drivers/base/fence.c +14
-0+} + +static void seqno_release (struct fence *fence) +{ +struct seqno_fence *f =to_seqno_fence (fence); + +dma_buf_put (f->sync_buf); +if (f->ops->release) + f->ops->release (fence); +else +kfree (f); +} + +static long seqno_wait(struct fence *fence, bool intr, signed longtimeout)
drivers/base/fence.c +14
-0+{ +struct seqno_fence *f =to_seqno_fence (fence); +return f->ops->wait (fence, intr, timeout); +} + +const struct fence_ops seqno_fence_ops = { + .get_driver_name = seqno_fence_get_driver_name, + .get_timeline_name = seqno_fence_get_timeline_name, + .enable_signaling = seqno_enable_signaling, + .signaled = seqno_signaled, + .wait = seqno_wait, + .release = seqno_release, +}; +EXPORT_SYMBOL (seqno_fence_ops);include/linux/seqno-fence.h new file +10
-0@@ -0,0 +1,109 @@ +/* +* seqno-fence, using a dma-buf to synchronize fencing +* +* Copyright (C) 2012 Texas Instruments +* Copyright (C) 2012 Canonical Ltd +* Authors: +* Rob Clark <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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/seqno-fence.h new file +44
-0+ +#ifndef __LINUX_SEQNO_FENCE_H +#define __LINUX_SEQNO_FENCE_H + +#include <linux/fence.h> +#include <linux/dma-buf.h> + +struct seqno_fence { +struct fence base; + +const struct fence_ops *ops; +struct dma_buf *sync_buf; +uint32_t seqno_ofs; +}; + +extern const struct fence_ops seqno_fence_ops; + +/** +* to_seqno_fence - cast a fence to a seqno_fence +* @fence: fence to cast to a seqno_fence +* +* Returns NULL if the fence is not a seqno_fence, +* or the seqno_fence otherwise. +*/ +static inline struct seqno_fence * +to_seqno_fence (struct fence *fence) +{ +if (fence->ops != &seqno_fence_ops) +return NULL ; + return container_of(fence, struct seqno_fence, base); +} + +/** +* seqno_fence_init - initialize a seqno fence +* @fence: seqno_fence to initialize +* @lock: pointer to spinlock to use for fence +* @sync_buf: buffer containing the memory location to signal on +* @context: the execution context this fence is a part of +* @seqno_ofs: the offset within @sync_buf +* @seqno: the sequence # to signal on +* @ops: the fence_ops for operations on this seqno fence +* +* This function initializes a struct seqno_fence with passed parameters, +* and takes a reference on sync_buf which is released on fence destruction.
+ *
+ * Aseqno_fenceis adma_fencewhich can complete in software when
+ *enable_signalingis called, but it also completes when
+ * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
+ *
+ * Theseqno_fencewill take a refcount on thesync_bufuntil it's
+ * destroyed, but actual lifetime ofsync_bufmay be longer if one of the
+ * callers take a reference to it.
+ *
+ * Certain hardware have instructions to insert this type of wait condition
+ * in the command stream, so no intervention from software would be needed.
+ * This type of fence can be destroyed before completed, however a reference
+ * on thesync_bufdma-buf can be taken. It is encouraged to re-use the same
+ * dma-buf forsync_buf, since mapping or unmapping thesync_bufto the
+ * device's vm can be expensive.
+ *
+ * It is recommended for creators ofseqno_fenceto callfence_signal
+ * before destruction. This will prevent possible issues from wraparound at
+ * time of issue vs time of check, since users can checkfence_is_signaled
+ * before submitting instructions for the hardware to wait on the fence.
+ * However, when ops.enable_signaling is not called, it doesn't have to be
+ * done as soon as possible, just before there's any real danger of seqno
+ * wraparound.
+ */include/linux/seqno-fence.h new file +3
-0+static inline void +seqno_fence_init (struct seqno_fence *fence,spinlock_t *lock, + struct dma_buf *sync_buf, uint32_t context, uint32_t
seqno_ofs,include/linux/seqno-fence.h new file +4
-0+ uint32_t seqno, const struct fence_ops *ops) +{ + BUG_ON(!fence || !sync_buf || !ops); + BUG_ON(!ops->wait || !ops->enable_signaling ||!ops->get_driver_name || !ops->get_timeline_name);
include/linux/seqno-fence.h new file +7
-0+ +/* +* ops is used in __fence_init for get_driver_name, so needs to be +* initialized first +*/ + fence->ops = ops; + __fence_init(&fence->base, &seqno_fence_ops, lock, context,seqno);
include/linux/seqno-fence.h new file +6
-0+get_dma_buf (sync_buf); + fence->sync_buf = sync_buf; + fence->seqno_ofs = seqno_ofs; +} + +#endif /* __LINUX_SEQNO_FENCE_H */ Quoted 4 lines
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
Quoted 29 lines · Christian König
Am 17.02.2014 18:27, schrieb Rob Clark:
Quoted 10 lines
Quoted 8 lines · Christian König
Am 17.02.2014 16:56, schrieb Maarten Lankhorst:
Quoted 3 lines
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
Quoted 15 lines
Quoted 4 lines
Can't we make that just "dma_buf[offset] != 0" instead? As far as I know this way it would match the definition M$ uses in their WDDM specification
and so make it much more likely that hardware supports it.well 'buf[offset] >= value' at least means the same slot can be used for multiple operations (with increasing values of 'value').. not sure if that is something people care about.
Quoted 6 lines
=value seems to be possible with adreno and radeon. I'm not really sure about others (although I presume it as least supported for nv desktop stuff). For hw that cannot do >=value, we can either have a different fence implementation which uses the !=0 approach. Or change seqno-fence implementation later if needed. But if someone has hw that can do !=0 but not >=value, speak up now ;-)
Quoted 3 lines
Here! Radeon can only do >=value on the DMA and 3D engine, but not with UVD or VCE. And for the 3D engine it means draining the pipe, which isn't really a good idea.
hmm, ok.. forgot you have a few extra rings compared to me. Is UVD re-ordering from decode-order to display-order for you in hw? If not, I guess you need sw intervention anyways when a frame is done for frame re-ordering, so maybe hw->hw sync doesn't really matter as much as compared to gpu/3d->display. For dma<->3d interactions, seems like you would care more about hw<->hw sync, but I guess you aren't likely to use GPU A to do a resolve blit for GPU B..
For 3D ring, I assume you probably want a CP_WAIT_FOR_IDLE before a CP_MEM_WRITE to update fence value in memory (for the one signalling the fence). But why would you need that before a CP_WAIT_REG_MEM (for the one waiting for the fence)? I don't exactly have documentation for adreno version of CP_WAIT_REG_{MEM,EQ,GTE}.. but PFP and ME appear to be same instruction set as r600, so I'm pretty sure they should have similar capabilities.. CP_WAIT_REG_MEM appears to be same but with 32bit gpu addresses vs 64b.
BR,
-R
Quoted 280 lines
Christian.
Quoted 275 lines
Quoted 5 lines
Apart from that I still don't like the idea of leaking a drivers IRQ context
outside of the driver, but without a proper GPU scheduler there probably isn't much alternative.I guess it will be not uncommon scenario for gpu device to just need to kick display device to write a few registers for a page flip.. probably best not to schedule a worker just for this (unless the signalled device otherwise needs to). I think it is better in this case to give the signalee some rope to hang themselves, and make it the responsibility of the callback to kick things off to a worker if needed.
BR,
-RQuoted 258 lines
Christian.
Quoted 246 lines
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose
this for graphics cards that have an op to support this.Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
v1: Original
v2: Renamed from bikeshed to seqno, moved intodma-fence.csince
not much was left of the file. Lots of documentation added.
v3: Usefence_opsinstead of custom callbacks. Moved to own file
to avoid circular dependency betweendma-buf.handfence.h
v4: Add spinlock pointer toseqno_fence_initSigned-off-by Maarten Lankhorst <[email protected]>--- Documentation/DocBook/device-drivers.tmpl | 1 drivers/base/fence.c | 50 +++++++++++++ include/linux/seqno-fence.h | 109 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+)create mode 100644
include/linux/seqno-fence.hDocumentation/DocBook/device-drivers.tmpl +1
-0@@ -131,6 +131,7 @@ X!Edrivers/base/interface.c !Edrivers/base/dma-buf.c !Edrivers/base/fence.c !Iinclude/linux/fence.h +!Iinclude/linux/seqno-fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.cdrivers/base/fence.c +22
-0@@ -25,6 +25,7 @@#include <linux/export.h> #include <linux/atomic.h> #include <linux/fence.h> +#include <linux/seqno-fence.h> #define CREATE_TRACE_POINTS #include <trace/events/fence.h> @@ -413,3 +414,52 @@ __fence_init(struct fence *fence, const structfence_ops *ops,trace_fence_init (fence ); }EXPORT_SYMBOL (__fence_init); + +static const char *seqno_fence_get_driver_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_driver_name (fence); +} + +static const char *seqno_fence_get_timeline_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_timeline_name (fence); +} + +static bool seqno_enable_signaling (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->enable_signaling (fence); +} + +static bool seqno_signaled (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); + return seqno_fence->ops->signaled &&
seqno_fence->ops->signaled(fence);drivers/base/fence.c +14
-0+} + +static void seqno_release (struct fence *fence) +{ +struct seqno_fence *f =to_seqno_fence (fence); + +dma_buf_put (f->sync_buf); +if (f->ops->release) + f->ops->release (fence); +else +kfree (f); +} + +static long seqno_wait(struct fence *fence, bool intr, signed longtimeout)
drivers/base/fence.c +14
-0+{ +struct seqno_fence *f =to_seqno_fence (fence); +return f->ops->wait (fence, intr, timeout); +} + +const struct fence_ops seqno_fence_ops = { + .get_driver_name = seqno_fence_get_driver_name, + .get_timeline_name = seqno_fence_get_timeline_name, + .enable_signaling = seqno_enable_signaling, + .signaled = seqno_signaled, + .wait = seqno_wait, + .release = seqno_release, +}; +EXPORT_SYMBOL (seqno_fence_ops);include/linux/seqno-fence.h new file +10
-0@@ -0,0 +1,109 @@ +/* +* seqno-fence, using a dma-buf to synchronize fencing +* +* Copyright (C) 2012 Texas Instruments +* Copyright (C) 2012 Canonical Ltd +* Authors: +* Rob Clark <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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/seqno-fence.h new file +43
-0+ +#ifndef __LINUX_SEQNO_FENCE_H +#define __LINUX_SEQNO_FENCE_H + +#include <linux/fence.h> +#include <linux/dma-buf.h> + +struct seqno_fence { +struct fence base; + +const struct fence_ops *ops; +struct dma_buf *sync_buf; +uint32_t seqno_ofs; +}; + +extern const struct fence_ops seqno_fence_ops; + +/** +* to_seqno_fence - cast a fence to a seqno_fence +* @fence: fence to cast to a seqno_fence +* +* Returns NULL if the fence is not a seqno_fence, +* or the seqno_fence otherwise. +*/ +static inline struct seqno_fence * +to_seqno_fence (struct fence *fence) +{ +if (fence->ops != &seqno_fence_ops) +return NULL ; + return container_of(fence, struct seqno_fence, base); +} + +/** +* seqno_fence_init - initialize a seqno fence +* @fence: seqno_fence to initialize +* @lock: pointer to spinlock to use for fence +* @sync_buf: buffer containing the memory location to signal on +* @context: the execution context this fence is a part of +* @seqno_ofs: the offset within @sync_buf +* @seqno: the sequence # to signal on +* @ops: the fence_ops for operations on this seqno fence +* +* This function initializes a struct seqno_fence with passed parameters,
+ * and takes a reference onsync_bufwhich is released on fence destruction.
+ *
+ * Aseqno_fenceis adma_fencewhich can complete in software when
+ *enable_signalingis called, but it also completes when
+ * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
+ *
+ * Theseqno_fencewill take a refcount on thesync_bufuntil it's
+ * destroyed, but actual lifetime ofsync_bufmay be longer if one of the
+ * callers take a reference to it.
+ *
+ * Certain hardware have instructions to insert this type of wait condition
+ * in the command stream, so no intervention from software would be needed.
+ * This type of fence can be destroyed before completed, however a reference
+ * on thesync_bufdma-buf can be taken. It is encouraged to re-use the same
+ * dma-buf forsync_buf, since mapping or unmapping thesync_bufto the
+ * device's vm can be expensive.
+ *
+ * It is recommended for creators ofseqno_fenceto callfence_signal
+ * before destruction. This will prevent possible issues from wraparound
at
+ * time of issue vs time of check, since users can checkfence_is_signaled
+ * before submitting instructions for the hardware to wait on the fence.
+ * However, when ops.enable_signaling is not called, it doesn't have to be
+ * done as soon as possible, just before there's any real danger of seqno
+ * wraparound.
+ */include/linux/seqno-fence.h new file +3
-0+static inline void +seqno_fence_init (struct seqno_fence *fence,spinlock_t *lock, + struct dma_buf *sync_buf, uint32_t context, uint32_t
seqno_ofs,include/linux/seqno-fence.h new file +4
-0+ uint32_t seqno, const struct fence_ops *ops) +{ + BUG_ON(!fence || !sync_buf || !ops); + BUG_ON(!ops->wait || !ops->enable_signaling ||!ops->get_driver_name || !ops->get_timeline_name);
include/linux/seqno-fence.h new file +3
-0+ +/* +* ops is used in __fence_init for get_driver_name, so needs to be
include/linux/seqno-fence.h new file +4
-0+* initialized first +*/ + fence->ops = ops; + __fence_init(&fence->base, &seqno_fence_ops, lock, context,seqno);
include/linux/seqno-fence.h new file +6
-0+get_dma_buf (sync_buf); + fence->sync_buf = sync_buf; + fence->seqno_ofs = seqno_ofs; +} + +#endif /* __LINUX_SEQNO_FENCE_H */ Quoted 4 lines
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
Am 17.02.2014 19:24, schrieb Rob Clark:
Quoted 30 lines
Quoted 25 lines · Christian König
Am 17.02.2014 18:27, schrieb Rob Clark:
Quoted 8 lines
Quoted 6 lines · Christian König
Am 17.02.2014 16:56, schrieb Maarten Lankhorst:
Quoted 3 lines
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
Quoted 14 lines
Quoted 4 lines
Can't we make that just "dma_buf[offset] != 0" instead? As far as I know this way it would match the definition M$ uses in their WDDM specification
and so make it much more likely that hardware supports it.well 'buf[offset] >= value' at least means the same slot can be used for multiple operations (with increasing values of 'value').. not sure if that is something people care about.
Quoted 6 lines
=value seems to be possible with adreno and radeon. I'm not really sure about others (although I presume it as least supported for nv desktop stuff). For hw that cannot do >=value, we can either have a different fence implementation which uses the !=0 approach. Or change seqno-fence implementation later if needed. But if someone has hw that can do !=0 but not >=value, speak up now ;-)
Quoted 3 lines
Here! Radeon can only do >=value on the DMA and 3D engine, but not with UVD or VCE. And for the 3D engine it means draining the pipe, which isn't really a good idea.
Quoted 7 lines
hmm, ok.. forgot you have a few extra rings compared to me. Is UVD re-ordering from decode-order to display-order for you in hw? If not, I guess you need sw intervention anyways when a frame is done for frame re-ordering, so maybe
hw->hwsync doesn't really matter as much as compared to gpu/3d->display. For dma<->3d interactions, seems like you would care more about hw<->hw sync, but I guess you aren't likely to use GPU A to do a resolve blit for GPU B..
No UVD isn't reordering, but since frame reordering is predictable you usually end up with pipelining everything to the hardware. E.g. you send the decode commands in decode order to the UVD block and if you have overlay active one of the frames are going to be the first to display and then you want to wait for it on the display side.
Quoted 8 lines
For 3D ring, I assume you probably want a
CP_WAIT_FOR_IDLEbefore aCP_MEM_WRITEto update fence value in memory (for the one signalling the fence). But why would you need that before aCP_WAIT_REG_MEM(for the one waiting for the fence)? I don't exactly have documentation for adreno version of CP_WAIT_REG_{MEM,EQ,GTE}.. but PFP and ME appear to be same instruction set as r600, so I'm pretty sure they should have similar capabilities..CP_WAIT_REG_MEMappears to be same but with 32bit gpu addresses vs 64b.
You shouldn't use any of the CP commands for engine synchronization (neither for wait nor for signal). The PFP and ME are just the top of a quite deep pipeline and when you use any of the CP_WAIT functions you block them for something and that's draining the pipeline.
With the semaphore and fence commands the values are just attached as prerequisite to the draw command, e.g. the CP setups the draw environment and issues the command, but the actual execution of it is delayed until the "!= 0" condition hits. And in the meantime the CP already prepares the next draw operation.
But at least for compute queues wait semaphore aren't the perfect solution either. What you need then is a GPU scheduler that uses a kernel task for setting up the command submission for you when all prerequisites are meet.
Christian.
Quoted 279 lines
BR,
-RQuoted 276 lines
Christian.
Quoted 272 lines
Quoted 4 lines
Apart from that I still don't like the idea of leaking a drivers IRQ context
outside of the driver, but without a proper GPU scheduler there probably isn't much alternative.I guess it will be not uncommon scenario for gpu device to just need to kick display device to write a few registers for a page flip.. probably best not to schedule a worker just for this (unless the signalled device otherwise needs to). I think it is better in this case to give the signalee some rope to hang themselves, and make it the responsibility of the callback to kick things off to a worker if needed.
BR,
-RQuoted 257 lines
Christian.
Quoted 246 lines
A software fallback still has to be provided in case the fence is used with a device that doesn't support this mechanism. It is useful to expose
this for graphics cards that have an op to support this.Some cards like i915 can export those, but don't have an option to wait, so they need the software fallback.
I extended the original patch by Rob Clark.
v1: Original
v2: Renamed from bikeshed to seqno, moved intodma-fence.csince
not much was left of the file. Lots of documentation added.
v3: Usefence_opsinstead of custom callbacks. Moved to own file
to avoid circular dependency betweendma-buf.handfence.h
v4: Add spinlock pointer toseqno_fence_initSigned-off-by Maarten Lankhorst <[email protected]>--- Documentation/DocBook/device-drivers.tmpl | 1 drivers/base/fence.c | 50 +++++++++++++ include/linux/seqno-fence.h | 109 +++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+)create mode 100644
include/linux/seqno-fence.hDocumentation/DocBook/device-drivers.tmpl +1
-0@@ -131,6 +131,7 @@ X!Edrivers/base/interface.c !Edrivers/base/dma-buf.c !Edrivers/base/fence.c !Iinclude/linux/fence.h +!Iinclude/linux/seqno-fence.h !Edrivers/base/reservation.c !Iinclude/linux/reservation.h !Edrivers/base/dma-coherent.cdrivers/base/fence.c +22
-0@@ -25,6 +25,7 @@#include <linux/export.h> #include <linux/atomic.h> #include <linux/fence.h> +#include <linux/seqno-fence.h> #define CREATE_TRACE_POINTS #include <trace/events/fence.h> @@ -413,3 +414,52 @@ __fence_init(struct fence *fence, const structfence_ops *ops,trace_fence_init (fence ); }EXPORT_SYMBOL (__fence_init); + +static const char *seqno_fence_get_driver_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_driver_name (fence); +} + +static const char *seqno_fence_get_timeline_name (struct fence *fence) { +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->get_timeline_name (fence); +} + +static bool seqno_enable_signaling (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); +return seqno_fence->ops->enable_signaling (fence); +} + +static bool seqno_signaled (struct fence *fence) +{ +struct seqno_fence *seqno_fence =to_seqno_fence (fence); + return seqno_fence->ops->signaled &&
seqno_fence->ops->signaled(fence);drivers/base/fence.c +14
-0+} + +static void seqno_release (struct fence *fence) +{ +struct seqno_fence *f =to_seqno_fence (fence); + +dma_buf_put (f->sync_buf); +if (f->ops->release) + f->ops->release (fence); +else +kfree (f); +} + +static long seqno_wait(struct fence *fence, bool intr, signed longtimeout)
drivers/base/fence.c +14
-0+{ +struct seqno_fence *f =to_seqno_fence (fence); +return f->ops->wait (fence, intr, timeout); +} + +const struct fence_ops seqno_fence_ops = { + .get_driver_name = seqno_fence_get_driver_name, + .get_timeline_name = seqno_fence_get_timeline_name, + .enable_signaling = seqno_enable_signaling, + .signaled = seqno_signaled, + .wait = seqno_wait, + .release = seqno_release, +}; +EXPORT_SYMBOL (seqno_fence_ops);include/linux/seqno-fence.h new file +10
-0@@ -0,0 +1,109 @@ +/* +* seqno-fence, using a dma-buf to synchronize fencing +* +* Copyright (C) 2012 Texas Instruments +* Copyright (C) 2012 Canonical Ltd +* Authors: +* Rob Clark <[email protected]> +* Maarten Lankhorst <[email protected]> +* +* 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/seqno-fence.h new file +43
-0+ +#ifndef __LINUX_SEQNO_FENCE_H +#define __LINUX_SEQNO_FENCE_H + +#include <linux/fence.h> +#include <linux/dma-buf.h> + +struct seqno_fence { +struct fence base; + +const struct fence_ops *ops; +struct dma_buf *sync_buf; +uint32_t seqno_ofs; +}; + +extern const struct fence_ops seqno_fence_ops; + +/** +* to_seqno_fence - cast a fence to a seqno_fence +* @fence: fence to cast to a seqno_fence +* +* Returns NULL if the fence is not a seqno_fence, +* or the seqno_fence otherwise. +*/ +static inline struct seqno_fence * +to_seqno_fence (struct fence *fence) +{ +if (fence->ops != &seqno_fence_ops) +return NULL ; + return container_of(fence, struct seqno_fence, base); +} + +/** +* seqno_fence_init - initialize a seqno fence +* @fence: seqno_fence to initialize +* @lock: pointer to spinlock to use for fence +* @sync_buf: buffer containing the memory location to signal on +* @context: the execution context this fence is a part of +* @seqno_ofs: the offset within @sync_buf +* @seqno: the sequence # to signal on +* @ops: the fence_ops for operations on this seqno fence +* +* This function initializes a struct seqno_fence with passed parameters,
+ * and takes a reference onsync_bufwhich is released on fence destruction.
+ *
+ * Aseqno_fenceis adma_fencewhich can complete in software when
+ *enable_signalingis called, but it also completes when
+ * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
+ *
+ * Theseqno_fencewill take a refcount on thesync_bufuntil it's
+ * destroyed, but actual lifetime ofsync_bufmay be longer if one of the
+ * callers take a reference to it.
+ *
+ * Certain hardware have instructions to insert this type of wait condition
+ * in the command stream, so no intervention from software would be needed.
+ * This type of fence can be destroyed before completed, however a reference
+ * on thesync_bufdma-buf can be taken. It is encouraged to re-use the same
+ * dma-buf forsync_buf, since mapping or unmapping thesync_bufto the
+ * device's vm can be expensive.
+ *
+ * It is recommended for creators ofseqno_fenceto callfence_signal
+ * before destruction. This will prevent possible issues from wraparound
at
+ * time of issue vs time of check, since users can checkfence_is_signaled
+ * before submitting instructions for the hardware to wait on the fence.
+ * However, when ops.enable_signaling is not called, it doesn't have to be
+ * done as soon as possible, just before there's any real danger of seqno
+ * wraparound.
+ */include/linux/seqno-fence.h new file +3
-0+static inline void +seqno_fence_init (struct seqno_fence *fence,spinlock_t *lock, + struct dma_buf *sync_buf, uint32_t context, uint32_t
seqno_ofs,include/linux/seqno-fence.h new file +4
-0+ uint32_t seqno, const struct fence_ops *ops) +{ + BUG_ON(!fence || !sync_buf || !ops); + BUG_ON(!ops->wait || !ops->enable_signaling ||!ops->get_driver_name || !ops->get_timeline_name);
include/linux/seqno-fence.h new file +3
-0+ +/* +* ops is used in __fence_init for get_driver_name, so needs to be
include/linux/seqno-fence.h new file +4
-0+* initialized first +*/ + fence->ops = ops; + __fence_init(&fence->base, &seqno_fence_ops, lock, context,seqno);
include/linux/seqno-fence.h new file +6
-0+get_dma_buf (sync_buf); + fence->sync_buf = sync_buf; + fence->seqno_ofs = seqno_ofs; +} + +#endif /* __LINUX_SEQNO_FENCE_H */ Quoted 4 lines
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
op 17-02-14 19:41, Christian König schreef:
Quoted 50 lines
Am 17.02.2014 19:24, schrieb Rob Clark:
Quoted 30 lines
Quoted 25 lines · Christian König
Am 17.02.2014 18:27, schrieb Rob Clark:
Quoted 8 lines
Quoted 6 lines · Christian König
Am 17.02.2014 16:56, schrieb Maarten Lankhorst:
Quoted 3 lines
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
Quoted 14 lines
Quoted 4 lines
Can't we make that just "dma_buf[offset] != 0" instead? As far as I know this way it would match the definition M$ uses in their WDDM specification
and so make it much more likely that hardware supports it.well 'buf[offset] >= value' at least means the same slot can be used for multiple operations (with increasing values of 'value').. not sure if that is something people care about.
Quoted 6 lines
=value seems to be possible with adreno and radeon. I'm not really sure about others (although I presume it as least supported for nv desktop stuff). For hw that cannot do >=value, we can either have a different fence implementation which uses the !=0 approach. Or change seqno-fence implementation later if needed. But if someone has hw that can do !=0 but not >=value, speak up now ;-)
Quoted 3 lines
Here! Radeon can only do >=value on the DMA and 3D engine, but not with UVD or VCE. And for the 3D engine it means draining the pipe, which isn't really a good idea.
Quoted 7 lines
hmm, ok.. forgot you have a few extra rings compared to me. Is UVD re-ordering from decode-order to display-order for you in hw? If not, I guess you need sw intervention anyways when a frame is done for frame re-ordering, so maybe
hw->hwsync doesn't really matter as much as compared to gpu/3d->display. For dma<->3d interactions, seems like you would care more about hw<->hw sync, but I guess you aren't likely to use GPU A to do a resolve blit for GPU B..No UVD isn't reordering, but since frame reordering is predictable you usually end up with pipelining everything to the hardware. E.g. you send the decode commands in decode order to the UVD block and if you have overlay active one of the frames are going to be the first to display and then you want to wait for it on the display side.
Quoted 8 lines
For 3D ring, I assume you probably want a
CP_WAIT_FOR_IDLEbefore aCP_MEM_WRITEto update fence value in memory (for the one signalling the fence). But why would you need that before aCP_WAIT_REG_MEM(for the one waiting for the fence)? I don't exactly have documentation for adreno version of CP_WAIT_REG_{MEM,EQ,GTE}.. but PFP and ME appear to be same instruction set as r600, so I'm pretty sure they should have similar capabilities..CP_WAIT_REG_MEMappears to be same but with 32bit gpu addresses vs 64b.
Quoted 5 lines
You shouldn't use any of the CP commands for engine synchronization (neither for wait nor for signal). The PFP and ME are just the top of a quite deep pipeline and when you use any of the
CP_WAITfunctions you block them for something and that's draining the pipeline.With the semaphore and fence commands the values are just attached as prerequisite to the draw command, e.g. the CP setups the draw environment and issues the command, but the actual execution of it is delayed until the "!= 0" condition hits. And in the meantime the CP already prepares the next draw operation.
But at least for compute queues wait semaphore aren't the perfect solution either. What you need then is a GPU scheduler that uses a kernel task for setting up the command submission for you when all prerequisites are meet.
nouveau has sort of a scheduler in hardware. It can yield when waiting on a semaphore. And each process gets their own context and the timeslices can be adjusted. ;-) But I don't mind changing this patch when an actual user pops up. Nouveau can do a wait for (*sema & mask) != 0 only on nvc0 and newer, where mask can be chosen. But it can do == somevalue and >= somevalue on older relevant optimus hardware, so if we know that it was zero before and we know the sign of the new value that could work too.
Adding ops and a separate mask later on when users pop up is fine with me, the original design here was chosen so I could map the intel status page read-only into the process specific nvidia vm.
~Maarten
Re: [PATCH 2/6] seqno-fence: Hardware dma-buf implementation of fencing (v4)
Quoted 65 lines · Maarten Lankhorst
op 17-02-14 19:41, Christian König schreef:
Quoted 50 lines
Am 17.02.2014 19:24, schrieb Rob Clark:
Quoted 30 lines
Quoted 25 lines · Christian König
Am 17.02.2014 18:27, schrieb Rob Clark:
Quoted 8 lines
Quoted 6 lines · Christian König
Am 17.02.2014 16:56, schrieb Maarten Lankhorst:
Quoted 3 lines
This type of fence can be used with hardware synchronization for simple hardware that can block execution until the condition (dma_buf[offset] - value) >= 0 has been met.
Quoted 14 lines
Quoted 4 lines
Can't we make that just "dma_buf[offset] != 0" instead? As far as I know this way it would match the definition M$ uses in their WDDM specification
and so make it much more likely that hardware supports it.well 'buf[offset] >= value' at least means the same slot can be used for multiple operations (with increasing values of 'value').. not sure if that is something people care about.
Quoted 6 lines
=value seems to be possible with adreno and radeon. I'm not really sure about others (although I presume it as least supported for nv desktop stuff). For hw that cannot do >=value, we can either have a different fence implementation which uses the !=0 approach. Or change seqno-fence implementation later if needed. But if someone has hw that can do !=0 but not >=value, speak up now ;-)
Quoted 3 lines
Here! Radeon can only do >=value on the DMA and 3D engine, but not with UVD or VCE. And for the 3D engine it means draining the pipe, which isn't really a good idea.
Quoted 7 lines
hmm, ok.. forgot you have a few extra rings compared to me. Is UVD re-ordering from decode-order to display-order for you in hw? If not, I guess you need sw intervention anyways when a frame is done for frame re-ordering, so maybe
hw->hwsync doesn't really matter as much as compared to gpu/3d->display. For dma<->3d interactions, seems like you would care more about hw<->hw sync, but I guess you aren't likely to use GPU A to do a resolve blit for GPU B..No UVD isn't reordering, but since frame reordering is predictable you usually end up with pipelining everything to the hardware. E.g. you send the decode commands in decode order to the UVD block and if you have overlay active one of the frames are going to be the first to display and then you want to wait for it on the display side.
Quoted 8 lines
For 3D ring, I assume you probably want a
CP_WAIT_FOR_IDLEbefore aCP_MEM_WRITEto update fence value in memory (for the one signalling the fence). But why would you need that before aCP_WAIT_REG_MEM(for the one waiting for the fence)? I don't exactly have documentation for adreno version of CP_WAIT_REG_{MEM,EQ,GTE}.. but PFP and ME appear to be same instruction set as r600, so I'm pretty sure they should have similar capabilities..CP_WAIT_REG_MEMappears to be same but with 32bit gpu addresses vs 64b.Quoted 5 lines
You shouldn't use any of the CP commands for engine synchronization (neither for wait nor for signal). The PFP and ME are just the top of a quite deep pipeline and when you use any of the
CP_WAITfunctions you block them for something and that's draining the pipeline.With the semaphore and fence commands the values are just attached as prerequisite to the draw command, e.g. the CP setups the draw environment and issues the command, but the actual execution of it is delayed until the "!= 0" condition hits. And in the meantime the CP already prepares the next draw operation.
But at least for compute queues wait semaphore aren't the perfect solution either. What you need then is a GPU scheduler that uses a kernel task for setting up the command submission for you when all prerequisites are meet.
nouveau has sort of a scheduler in hardware. It can yield when waiting on a semaphore. And each process gets their own context and the timeslices can be adjusted. ;-) But I don't mind changing this patch when an actual user pops up. Nouveau can do a wait for (*sema & mask) != 0 only on nvc0 and newer, where mask can be chosen. But it can do == somevalue and >= somevalue on older relevant optimus hardware, so if we know that it was zero before and we know the sign of the new value that could work too.
Quoted 3 lines
Adding ops and a separate mask later on when users pop up is fine with me, the original design here was chosen so I could map the intel status page read-only into the process specific nvidia vm.
Yeah, I guess in the end we might end up having a pile of different memory-based (shared through dma-bufs) based fences. But imo getting this thing of the ground is more important, and you can always do crazy per-platform/generation/whatever hacks and match on that specific fence type in the interim. That'll cut it for the SoC madness, which also seems to be what google does with the android syncpoint stuff. -Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
PATCH 3/6 dma-buf: use reservation objects
In reply to Maarten LankhorstThis allows reservation objects to be used in dma-buf. it's required for implementing polling support on the fences that belong to a dma-buf.
--- drivers/base/dma-buf.c | 22 ++++++++++++++++++++-- drivers/gpu/drm/drm_prime.c | 8 +++++++- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/gpu/drm/i915/i915_gem_dmabuf.c | 2 +- drivers/gpu/drm/nouveau/nouveau_drm.c | 1 + drivers/gpu/drm/nouveau/nouveau_gem.h | 1 + drivers/gpu/drm/nouveau/nouveau_prime.c | 7 +++++++ drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c | 2 +- drivers/gpu/drm/radeon/radeon_drv.c | 2 ++ drivers/gpu/drm/radeon/radeon_prime.c | 8 ++++++++ drivers/gpu/drm/ttm/ttm_object.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- include/drm/drmP.h | 2 ++ include/linux/dma-buf.h | 9 ++++++--- 14 files changed, 59 insertions(+), 11 deletions(-)
drivers/base/dma-buf.c +20 -2
@@ -25,10 +25,12 @@#include <linux/fs.h> #include <linux/slab.h> #include <linux/dma-buf.h> +#include <linux/fence.h> #include <linux/anon_inodes.h> #include <linux/export.h> #include <linux/debugfs.h> #include <linux/seq_file.h> +#include <linux/reservation.h> static inline int is_dma_buf_file (struct file *);
@@ -56,6 +58,9 @@ static int dma_buf_release(struct inode *inode, struct file *file)list_del (&dmabuf->list_node);mutex_unlock (&db_list.lock); +if (dmabuf->resv == (struct reservation_object *)&dmabuf[1 ]) +reservation_object_fini (dmabuf->resv); +kfree (dmabuf);return 0 ; }
@@ -128,6 +133,7 @@ static inline int is_dma_buf_file(struct file *file)* @size: [in] Size of the buffer * @flags: [in] mode flags for the file. * @exp_name: [in] name of the exporting module - useful for debugging. +* @resv: [in] reservation-object, NULL to allocate default one. * * Returns, on success, a newly created dma_buf object, which wraps the * supplied private data and operations for dma_buf_ops. On either missing
@@ -135,10 +141,17 @@ static inline int is_dma_buf_file(struct file *file)* */ struct dma_buf *dma_buf_export_named (void *priv,const struct dma_buf_ops *ops, -size_t size,int flags,const char *exp_name) +size_t size,int flags,const char *exp_name, +struct reservation_object *resv) {struct dma_buf *dmabuf;struct file *file; +size_t alloc_size =sizeof (struct dma_buf ); +if (!resv) + alloc_size +=sizeof (struct reservation_object ); +else +/* prevent &dma_buf[1] == dma_buf->resv */ + alloc_size +=1 ; if (WARN_ON(!priv || !ops || !ops->map_dma_buf
@@ -150,7 +163,7 @@ struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops,return ERR_PTR (-EINVAL ); } - dmabuf =kzalloc (sizeof (struct dma_buf ),GFP_KERNEL ); + dmabuf =kzalloc (alloc_size,GFP_KERNEL );if (dmabuf ==NULL )return ERR_PTR (-ENOMEM );
@@ -158,6 +171,11 @@ struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops, dmabuf->ops = ops; dmabuf->size = size; dmabuf->exp_name = exp_name; +if (!resv) { + resv = (struct reservation_object *)&dmabuf[1 ]; +reservation_object_init (resv); + } + dmabuf->resv = resv; file =anon_inode_getfile ("dmabuf" , &dma_buf_fops, dmabuf, flags);if (IS_ERR (file)) {
drivers/gpu/drm/drm_prime.c +7 -1
@@ -318,7 +318,13 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {struct dma_buf *drm_gem_prime_export (struct drm_device *dev,struct drm_gem_object *obj,int flags) { -return dma_buf_export (obj, &drm_gem_prime_dmabuf_ops, obj->size, flags); +struct reservation_object *robj =NULL ; + +if (dev->driver->gem_prime_res_obj) + robj = dev->driver->gem_prime_res_obj (obj); + +return dma_buf_export (obj, &drm_gem_prime_dmabuf_ops, obj->size, + flags, robj); }EXPORT_SYMBOL (drm_gem_prime_export);
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1 -1
@@ -187,7 +187,7 @@ struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,struct exynos_drm_gem_obj *exynos_gem_obj =to_exynos_gem_obj (obj);return dma_buf_export (obj, &exynos_dmabuf_ops, - exynos_gem_obj->base.size, flags); + exynos_gem_obj->base.size, flags,NULL ); } struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
drivers/gpu/drm/i915/i915_gem_dmabuf.c +1 -1
@@ -233,7 +233,7 @@ static const struct dma_buf_ops i915_dmabuf_ops = {struct dma_buf *i915_gem_prime_export (struct drm_device *dev,struct drm_gem_object *gem_obj,int flags) { -return dma_buf_export (gem_obj, &i915_dmabuf_ops, gem_obj->size, flags); +return dma_buf_export (gem_obj, &i915_dmabuf_ops, gem_obj->size, flags,NULL ); }static int i915_gem_object_get_pages_dmabuf (struct drm_i915_gem_object *obj)
drivers/gpu/drm/nouveau/nouveau_drm.c +1 -0
@@ -816,6 +816,7 @@ driver = { .gem_prime_export = drm_gem_prime_export, .gem_prime_import = drm_gem_prime_import, .gem_prime_pin = nouveau_gem_prime_pin, + .gem_prime_res_obj = nouveau_gem_prime_res_obj, .gem_prime_unpin = nouveau_gem_prime_unpin, .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table, .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
drivers/gpu/drm/nouveau/nouveau_gem.h +1 -0
@@ -35,6 +35,7 @@ extern int nouveau_gem_ioctl_info(struct drm_device *, void *, struct drm_file *);extern int nouveau_gem_prime_pin (struct drm_gem_object *); +struct reservation_object *nouveau_gem_prime_res_obj (struct drm_gem_object *);extern void nouveau_gem_prime_unpin (struct drm_gem_object *);extern struct sg_table *nouveau_gem_prime_get_sg_table (struct drm_gem_object *); extern struct drm_gem_object *nouveau_gem_prime_import_sg_table(
drivers/gpu/drm/nouveau/nouveau_prime.c +7 -0
@@ -102,3 +102,10 @@ void nouveau_gem_prime_unpin(struct drm_gem_object *obj)nouveau_bo_unpin (nvbo); } + +struct reservation_object *nouveau_gem_prime_res_obj (struct drm_gem_object *obj) +{ +struct nouveau_bo *nvbo =nouveau_gem_object (obj); + +return nvbo->bo.resv; +}
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +1 -1
@@ -171,7 +171,7 @@ static struct dma_buf_ops omap_dmabuf_ops = {struct dma_buf *omap_gem_prime_export (struct drm_device *dev,struct drm_gem_object *obj,int flags) { -return dma_buf_export (obj, &omap_dmabuf_ops, obj->size, flags); +return dma_buf_export (obj, &omap_dmabuf_ops, obj->size, flags,NULL ); } struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
drivers/gpu/drm/radeon/radeon_drv.c +2 -0
@@ -128,6 +128,7 @@ struct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev, struct sg_table *sg);int radeon_gem_prime_pin (struct drm_gem_object *obj);void radeon_gem_prime_unpin (struct drm_gem_object *obj); +struct reservation_object *radeon_gem_prime_res_obj (struct drm_gem_object *);void *radeon_gem_prime_vmap (struct drm_gem_object *obj);void radeon_gem_prime_vunmap (struct drm_gem_object *obj,void *vaddr); extern long radeon_kms_compat_ioctl(struct file *filp, unsigned int cmd,
@@ -561,6 +562,7 @@ static struct drm_driver kms_driver = { .gem_prime_import = drm_gem_prime_import, .gem_prime_pin = radeon_gem_prime_pin, .gem_prime_unpin = radeon_gem_prime_unpin, + .gem_prime_res_obj = radeon_gem_prime_res_obj, .gem_prime_get_sg_table = radeon_gem_prime_get_sg_table, .gem_prime_import_sg_table = radeon_gem_prime_import_sg_table, .gem_prime_vmap = radeon_gem_prime_vmap,
drivers/gpu/drm/radeon/radeon_prime.c +8 -0
@@ -103,3 +103,11 @@ void radeon_gem_prime_unpin(struct drm_gem_object *obj)radeon_bo_unpin (bo);radeon_bo_unreserve (bo); } + + +struct reservation_object *radeon_gem_prime_res_obj (struct drm_gem_object *obj) +{ +struct radeon_bo *bo =gem_to_radeon_bo (obj); + +return bo->tbo.resv; +}
drivers/gpu/drm/ttm/ttm_object.c +1 -1
@@ -649,7 +649,7 @@ int ttm_prime_handle_to_fd(struct ttm_object_file *tfile, } dma_buf =dma_buf_export (prime, &tdev->ops, - prime->size, flags); + prime->size, flags,NULL );if (IS_ERR (dma_buf)) { ret =PTR_ERR (dma_buf); ttm_mem_global_free(tdev->mem_glob,
drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -1
@@ -404,7 +404,7 @@ static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)if (WARN_ON (!buf->sgt_base))return NULL ; - dbuf =dma_buf_export (buf, &vb2_dc_dmabuf_ops, buf->size, flags); + dbuf =dma_buf_export (buf, &vb2_dc_dmabuf_ops, buf->size, flags,NULL );if (IS_ERR (dbuf))return NULL ;
include/drm/drmP.h +2 -0
@@ -82,6 +82,7 @@ struct drm_device;struct device_node ;struct videomode ; +struct reservation_object ;#include <drm/drm_os_linux.h> #include <drm/drm_hashtab.h>
@@ -959,6 +960,7 @@ struct drm_driver {/* low-level interface used by drm_gem_prime_{import,export} */ int (*gem_prime_pin)(struct drm_gem_object *obj);void (*gem_prime_unpin)(struct drm_gem_object *obj); +struct reservation_object *(*gem_prime_res_obj)(struct drm_gem_object *);struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj); struct drm_gem_object *(*gem_prime_import_sg_table)( struct drm_device *dev, size_t size,
include/linux/dma-buf.h +6 -3
@@ -115,6 +115,7 @@ struct dma_buf_ops {* @exp_name: name of the exporter; useful for debugging. * @list_node: node for dma_buf accounting and debugging. * @priv: exporter specific private data for this buffer object. +* @resv: reservation object linked to this dma-buf */ struct dma_buf {size_t size;
@@ -128,6 +129,7 @@ struct dma_buf {const char *exp_name;struct list_head list_node;void *priv; +struct reservation_object *resv; };/**
@@ -168,10 +170,11 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *dmabuf_attach);struct dma_buf *dma_buf_export_named (void *priv,const struct dma_buf_ops *ops, -size_t size,int flags,const char *); +size_t size,int flags,const char *, +struct reservation_object *); -#define dma_buf_export (priv, ops, size, flags) \ - dma_buf_export_named(priv, ops, size, flags, __FILE__) +#define dma_buf_export (priv, ops, size, flags, resv) \ + dma_buf_export_named(priv, ops, size, flags, __FILE__, resv)int dma_buf_fd (struct dma_buf *dmabuf,int flags);struct dma_buf *dma_buf_get (int fd);
Re: [PATCH 3/6] dma-buf: use reservation objects
Quoted 102 lines · Maarten Lankhorst
This allows reservation objects to be used in dma-buf. it's required for implementing polling support on the fences that belong to a dma-buf.
Signed-off-by Maarten Lankhorst <[email protected]>--- drivers/base/dma-buf.c | 22 ++++++++++++++++++++-- drivers/gpu/drm/drm_prime.c | 8 +++++++- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/gpu/drm/i915/i915_gem_dmabuf.c | 2 +- drivers/gpu/drm/nouveau/nouveau_drm.c | 1 + drivers/gpu/drm/nouveau/nouveau_gem.h | 1 + drivers/gpu/drm/nouveau/nouveau_prime.c | 7 +++++++ drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c | 2 +- drivers/gpu/drm/radeon/radeon_drv.c | 2 ++ drivers/gpu/drm/radeon/radeon_prime.c | 8 ++++++++ drivers/gpu/drm/ttm/ttm_object.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- include/drm/drmP.h | 2 ++ include/linux/dma-buf.h | 9 ++++++--- 14 files changed, 59 insertions(+), 11 deletions(-)drivers/base/dma-buf.c +20
-2@@ -25,10 +25,12 @@#include <linux/fs.h> #include <linux/slab.h> #include <linux/dma-buf.h> +#include <linux/fence.h> #include <linux/anon_inodes.h> #include <linux/export.h> #include <linux/debugfs.h> #include <linux/seq_file.h> +#include <linux/reservation.h> static inline int is_dma_buf_file (struct file *);@@ -56,6 +58,9 @@ static int dma_buf_release(struct inode *inode, struct file *file)list_del (&dmabuf->list_node);mutex_unlock (&db_list.lock); +if (dmabuf->resv == (struct reservation_object *)&dmabuf[1 ]) +reservation_object_fini (dmabuf->resv); +kfree (dmabuf);return 0 ; }@@ -128,6 +133,7 @@ static inline int is_dma_buf_file(struct file *file)* @size: [in] Size of the buffer * @flags: [in] mode flags for the file. * @exp_name: [in] name of the exporting module - useful for debugging. +* @resv: [in] reservation-object, NULL to allocate default one. * * Returns, on success, a newly created dma_buf object, which wraps the * supplied private data and operations for dma_buf_ops. On either missing @@ -135,10 +141,17 @@ static inline int is_dma_buf_file(struct file *file)* */ struct dma_buf *dma_buf_export_named (void *priv,const struct dma_buf_ops *ops, -size_t size,int flags,const char *exp_name) +size_t size,int flags,const char *exp_name, +struct reservation_object *resv) {struct dma_buf *dmabuf;struct file *file; +size_t alloc_size =sizeof (struct dma_buf ); +if (!resv) + alloc_size +=sizeof (struct reservation_object ); +else +/* prevent &dma_buf[1] == dma_buf->resv */ + alloc_size +=1 ; if (WARN_ON(!priv || !ops || !ops->map_dma_buf@@ -150,7 +163,7 @@ struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops,return ERR_PTR (-EINVAL ); } - dmabuf =kzalloc (sizeof (struct dma_buf ),GFP_KERNEL ); + dmabuf =kzalloc (alloc_size,GFP_KERNEL );if (dmabuf ==NULL )return ERR_PTR (-ENOMEM );@@ -158,6 +171,11 @@ struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops, dmabuf->ops = ops; dmabuf->size = size; dmabuf->exp_name = exp_name; +if (!resv) { + resv = (struct reservation_object *)&dmabuf[1 ]; +reservation_object_init (resv); + } + dmabuf->resv = resv; file =anon_inode_getfile ("dmabuf" , &dma_buf_fops, dmabuf, flags);if (IS_ERR (file)) {drivers/gpu/drm/drm_prime.c +0
-0
drivers/gpu/drm/drm_prime.c static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = { · line 324
Quoted 8 lines
drivers/gpu/drm/drm_prime.c +4
-1@@ -318,7 +318,13 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {struct dma_buf *drm_gem_prime_export (struct drm_device *dev,struct drm_gem_object *obj,int flags) { -return dma_buf_export (obj, &drm_gem_prime_dmabuf_ops, obj->size, flags); +struct reservation_object *robj =NULL ; + +if (dev->driver->gem_prime_res_obj) + robj = dev->driver->gem_prime_res_obj (obj);
well, you could hook up msm_gem_prime_res_obj too (since I already have a resv obj in 'struct msm_gem_object' ;-)
That said, I wonder if maybe we just want to promote the 'struct reservation_object' ptr into 'struct drm_gem_object' so we can have a common get_prime_res_obj fxn for everyone using GEM?
Anyways, that only matters within drivers/gpu/drm so easy enough to change it later.. so for the drm/fence/reservation/dmabuf bits:
Quoted 207 lines
drivers/gpu/drm/drm_prime.c +3
-0+ +return dma_buf_export (obj, &drm_gem_prime_dmabuf_ops, obj->size, + flags, robj); }EXPORT_SYMBOL (drm_gem_prime_export);drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1
-1@@ -187,7 +187,7 @@ struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,struct exynos_drm_gem_obj *exynos_gem_obj =to_exynos_gem_obj (obj);return dma_buf_export (obj, &exynos_dmabuf_ops, - exynos_gem_obj->base.size, flags); + exynos_gem_obj->base.size, flags,NULL ); } struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,drivers/gpu/drm/i915/i915_gem_dmabuf.c +1
-1@@ -233,7 +233,7 @@ static const struct dma_buf_ops i915_dmabuf_ops = {struct dma_buf *i915_gem_prime_export (struct drm_device *dev,struct drm_gem_object *gem_obj,int flags) { -return dma_buf_export (gem_obj, &i915_dmabuf_ops, gem_obj->size, flags); +return dma_buf_export (gem_obj, &i915_dmabuf_ops, gem_obj->size, flags,NULL ); }static int i915_gem_object_get_pages_dmabuf (struct drm_i915_gem_object *obj)drivers/gpu/drm/nouveau/nouveau_drm.c +1
-0@@ -816,6 +816,7 @@ driver = { .gem_prime_export = drm_gem_prime_export, .gem_prime_import = drm_gem_prime_import, .gem_prime_pin = nouveau_gem_prime_pin, + .gem_prime_res_obj = nouveau_gem_prime_res_obj, .gem_prime_unpin = nouveau_gem_prime_unpin, .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table, .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,drivers/gpu/drm/nouveau/nouveau_gem.h +1
-0@@ -35,6 +35,7 @@ extern int nouveau_gem_ioctl_info(struct drm_device *, void *, struct drm_file *);extern int nouveau_gem_prime_pin (struct drm_gem_object *); +struct reservation_object *nouveau_gem_prime_res_obj (struct drm_gem_object *);extern void nouveau_gem_prime_unpin (struct drm_gem_object *);extern struct sg_table *nouveau_gem_prime_get_sg_table (struct drm_gem_object *); extern struct drm_gem_object *nouveau_gem_prime_import_sg_table(drivers/gpu/drm/nouveau/nouveau_prime.c +7
-0@@ -102,3 +102,10 @@ void nouveau_gem_prime_unpin(struct drm_gem_object *obj)nouveau_bo_unpin (nvbo); } + +struct reservation_object *nouveau_gem_prime_res_obj (struct drm_gem_object *obj) +{ +struct nouveau_bo *nvbo =nouveau_gem_object (obj); + +return nvbo->bo.resv; +}drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +1
-1@@ -171,7 +171,7 @@ static struct dma_buf_ops omap_dmabuf_ops = {struct dma_buf *omap_gem_prime_export (struct drm_device *dev,struct drm_gem_object *obj,int flags) { -return dma_buf_export (obj, &omap_dmabuf_ops, obj->size, flags); +return dma_buf_export (obj, &omap_dmabuf_ops, obj->size, flags,NULL ); } struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,drivers/gpu/drm/radeon/radeon_drv.c +2
-0@@ -128,6 +128,7 @@ struct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev, struct sg_table *sg);int radeon_gem_prime_pin (struct drm_gem_object *obj);void radeon_gem_prime_unpin (struct drm_gem_object *obj); +struct reservation_object *radeon_gem_prime_res_obj (struct drm_gem_object *);void *radeon_gem_prime_vmap (struct drm_gem_object *obj);void radeon_gem_prime_vunmap (struct drm_gem_object *obj,void *vaddr); extern long radeon_kms_compat_ioctl(struct file *filp, unsigned int cmd,@@ -561,6 +562,7 @@ static struct drm_driver kms_driver = { .gem_prime_import = drm_gem_prime_import, .gem_prime_pin = radeon_gem_prime_pin, .gem_prime_unpin = radeon_gem_prime_unpin, + .gem_prime_res_obj = radeon_gem_prime_res_obj, .gem_prime_get_sg_table = radeon_gem_prime_get_sg_table, .gem_prime_import_sg_table = radeon_gem_prime_import_sg_table, .gem_prime_vmap = radeon_gem_prime_vmap,drivers/gpu/drm/radeon/radeon_prime.c +8
-0@@ -103,3 +103,11 @@ void radeon_gem_prime_unpin(struct drm_gem_object *obj)radeon_bo_unpin (bo);radeon_bo_unreserve (bo); } + + +struct reservation_object *radeon_gem_prime_res_obj (struct drm_gem_object *obj) +{ +struct radeon_bo *bo =gem_to_radeon_bo (obj); + +return bo->tbo.resv; +}drivers/gpu/drm/ttm/ttm_object.c +1
-1@@ -649,7 +649,7 @@ int ttm_prime_handle_to_fd(struct ttm_object_file *tfile, } dma_buf =dma_buf_export (prime, &tdev->ops, - prime->size, flags); + prime->size, flags,NULL );if (IS_ERR (dma_buf)) { ret =PTR_ERR (dma_buf); ttm_mem_global_free(tdev->mem_glob,drivers/media/v4l2-core/videobuf2-dma-contig.c +1
-1@@ -404,7 +404,7 @@ static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)if (WARN_ON (!buf->sgt_base))return NULL ; - dbuf =dma_buf_export (buf, &vb2_dc_dmabuf_ops, buf->size, flags); + dbuf =dma_buf_export (buf, &vb2_dc_dmabuf_ops, buf->size, flags,NULL );if (IS_ERR (dbuf))return NULL ;include/drm/drmP.h +2
-0@@ -82,6 +82,7 @@ struct drm_device;struct device_node ;struct videomode ; +struct reservation_object ;#include <drm/drm_os_linux.h> #include <drm/drm_hashtab.h> @@ -959,6 +960,7 @@ struct drm_driver {/* low-level interface used by drm_gem_prime_{import,export} */ int (*gem_prime_pin)(struct drm_gem_object *obj);void (*gem_prime_unpin)(struct drm_gem_object *obj); +struct reservation_object *(*gem_prime_res_obj)(struct drm_gem_object *);struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj); struct drm_gem_object *(*gem_prime_import_sg_table)( struct drm_device *dev, size_t size,include/linux/dma-buf.h +6
-4@@ -115,6 +115,7 @@ struct dma_buf_ops {* @exp_name: name of the exporter; useful for debugging. * @list_node: node for dma_buf accounting and debugging. * @priv: exporter specific private data for this buffer object. +* @resv: reservation object linked to this dma-buf */ struct dma_buf {size_t size;@@ -128,6 +129,7 @@ struct dma_buf {const char *exp_name;struct list_head list_node;void *priv; +struct reservation_object *resv; };/** @@ -168,10 +170,11 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *dmabuf_attach);struct dma_buf *dma_buf_export_named (void *priv,const struct dma_buf_ops *ops, -size_t size,int flags,const char *); +size_t size,int flags,const char *, +struct reservation_object *); -#define dma_buf_export (priv, ops, size, flags) \ - dma_buf_export_named(priv, ops, size, flags, __FILE__) +#define dma_buf_export (priv, ops, size, flags, resv) \ + dma_buf_export_named(priv, ops, size, flags, __FILE__, resv)int dma_buf_fd (struct dma_buf *dmabuf,int flags);struct dma_buf *dma_buf_get (int fd); --To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 3/6] dma-buf: use reservation objects
In reply to Maarten LankhorstQuoted 5 lines · Maarten Lankhorst
This allows reservation objects to be used in dma-buf. it's required for implementing polling support on the fences that belong to a dma-buf.
For the TTM part:
PATCH 4/6 android: convert sync to fence api, v4
In reply to Maarten LankhorstAndroid syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Call fence_remove_callback in sync_fence_free if not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.
--- drivers/staging/android/Kconfig | 1 drivers/staging/android/Makefile | 2 drivers/staging/android/sw_sync.c | 4 drivers/staging/android/sync.c | 892 +++++++++++----------------------- drivers/staging/android/sync.h | 80 ++- drivers/staging/android/sync_debug.c | 245 +++++++++ drivers/staging/android/trace/sync.h | 12 7 files changed, 592 insertions(+), 644 deletions(-) create mode 100644 drivers/staging/android/sync_debug.c
drivers/staging/android/Kconfig +1 -0
@@ -77,6 +77,7 @@ config SYNC bool "Synchronization framework" default n select ANON_INODES + select DMA_SHARED_BUFFER ---help--- This option enables the framework for synchronization between multiple drivers. Sync implementations can take advantage of hardware
drivers/staging/android/Makefile +1 -1
@@ -9,5 +9,5 @@ obj-$(CONFIG_ANDROID_TIMED_OUTPUT) += timed_output.o obj-$(CONFIG_ANDROID_TIMED_GPIO) += timed_gpio.o obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER) += lowmemorykiller.o obj-$(CONFIG_ANDROID_INTF_ALARM_DEV) += alarm-dev.o -obj-$(CONFIG_SYNC) += sync.o +obj-$(CONFIG_SYNC) += sync.o sync_debug.o obj-$(CONFIG_SW_SYNC) += sw_sync.o
drivers/staging/android/sw_sync.c +2 -2
@@ -50,7 +50,7 @@ static struct sync_pt *sw_sync_pt_dup(struct sync_pt *sync_pt) {struct sw_sync_pt *pt = (struct sw_sync_pt *) sync_pt;struct sw_sync_timeline *obj = - (struct sw_sync_timeline *)sync_pt->parent; + (struct sw_sync_timeline *)sync_pt_parent (sync_pt);return (struct sync_pt *)sw_sync_pt_create (obj, pt->value); }
@@ -59,7 +59,7 @@ static int sw_sync_pt_has_signaled(struct sync_pt *sync_pt) {struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;struct sw_sync_timeline *obj = - (struct sw_sync_timeline *)sync_pt->parent; + (struct sw_sync_timeline *)sync_pt_parent (sync_pt);return sw_sync_cmp (obj->value, pt->value) >=0 ; }
drivers/staging/android/sync.c +291 -601
1,125 lines not on this page · raw message
drivers/staging/android/sync.h +46 -34
159 lines not on this page · raw message
drivers/staging/android/sync_debug.c new file +245 -0
245 lines not on this page · raw message
drivers/staging/android/trace/sync.h +6 -6
@@ -45,7 +45,7 @@ TRACE_EVENT(sync_wait,TP_fast_assign ( __assign_str(name, fence->name); - __entry->status = fence->status; + __entry->status =atomic_read (&fence->status); __entry->begin = begin; ),
@@ -54,19 +54,19 @@ TRACE_EVENT(sync_wait, ); TRACE_EVENT(sync_pt, - TP_PROTO(struct sync_pt *pt), + TP_PROTO(struct fence *pt), TP_ARGS(pt), TP_STRUCT__entry( - __string(timeline, pt->parent->name) + __string(timeline, pt->ops->get_timeline_name(pt)) __array(char, value, 32) ), TP_fast_assign( - __assign_str(timeline, pt->parent->name); - if (pt->parent->ops->pt_value_str) { - pt->parent->ops->pt_value_str(pt, __entry->value, + __assign_str(timeline, pt->ops->get_timeline_name(pt)); + if (pt->ops->fence_value_str) { + pt->ops->fence_value_str(pt, __entry->value, sizeof(__entry->value)); } else { __entry->value[0] = '\0';
Re: [PATCH 4/6] android: convert sync to fence api, v4
Quoted 348 lines · Maarten Lankhorst
Android syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Callfence_remove_callbackinsync_fence_freeif not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.Signed-off-by Maarten Lankhorst <[email protected]>--- drivers/staging/android/Kconfig | 1 drivers/staging/android/Makefile | 2 drivers/staging/android/sw_sync.c | 4 drivers/staging/android/sync.c | 892 +++++++++++----------------------- drivers/staging/android/sync.h | 80 ++- drivers/staging/android/sync_debug.c | 245 +++++++++ drivers/staging/android/trace/sync.h | 12 7 files changed, 592 insertions(+), 644 deletions(-) create mode 100644 drivers/staging/android/sync_debug.cdrivers/staging/android/Kconfig +1
-0@@ -77,6 +77,7 @@ config SYNC bool "Synchronization framework" default n select ANON_INODES + select DMA_SHARED_BUFFER ---help--- This option enables the framework for synchronization between multiple drivers. Sync implementations can take advantage of hardwaredrivers/staging/android/Makefile +1
-1@@ -9,5 +9,5 @@ obj-$(CONFIG_ANDROID_TIMED_OUTPUT) += timed_output.o obj-$(CONFIG_ANDROID_TIMED_GPIO) += timed_gpio.o obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER) += lowmemorykiller.o obj-$(CONFIG_ANDROID_INTF_ALARM_DEV) += alarm-dev.o -obj-$(CONFIG_SYNC) += sync.o +obj-$(CONFIG_SYNC) += sync.o sync_debug.o obj-$(CONFIG_SW_SYNC) += sw_sync.odrivers/staging/android/sw_sync.c +2
-2@@ -50,7 +50,7 @@ static struct sync_pt *sw_sync_pt_dup(struct sync_pt *sync_pt) {struct sw_sync_pt *pt = (struct sw_sync_pt *) sync_pt;struct sw_sync_timeline *obj = - (struct sw_sync_timeline *)sync_pt->parent; + (struct sw_sync_timeline *)sync_pt_parent (sync_pt);return (struct sync_pt *)sw_sync_pt_create (obj, pt->value); }@@ -59,7 +59,7 @@ static int sw_sync_pt_has_signaled(struct sync_pt *sync_pt) {struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;struct sw_sync_timeline *obj = - (struct sw_sync_timeline *)sync_pt->parent; + (struct sw_sync_timeline *)sync_pt_parent (sync_pt);return sw_sync_cmp (obj->value, pt->value) >=0 ; }drivers/staging/android/sync.c +33
-151@@ -31,22 +31,13 @@#define CREATE_TRACE_POINTS #include "trace/sync.h" -static void sync_fence_signal_pt (struct sync_pt *pt); -static int _sync_pt_has_signaled (struct sync_pt *pt); -static void sync_fence_free (struct kref *kref); -static void sync_dump (void ); - -static LIST_HEAD (sync_timeline_list_head); -static DEFINE_SPINLOCK (sync_timeline_list_lock); - -static LIST_HEAD (sync_fence_list_head); -static DEFINE_SPINLOCK (sync_fence_list_lock); +static const struct fence_ops android_fence_ops; +static const struct file_operations sync_fence_fops;struct sync_timeline *sync_timeline_create (const struct sync_timeline_ops *ops,int size,const char *name) {struct sync_timeline *obj; -unsigned long flags;if (size <sizeof (struct sync_timeline ))return NULL ;@@ -57,17 +48,14 @@ struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops,kref_init (&obj->kref); obj->ops = ops; + obj->context =fence_context_alloc (1 );strlcpy (obj->name, name,sizeof (obj->name));INIT_LIST_HEAD (&obj->child_list_head); -spin_lock_init (&obj->child_list_lock); -INIT_LIST_HEAD (&obj->active_list_head); -spin_lock_init (&obj->active_list_lock); +spin_lock_init (&obj->child_list_lock); -spin_lock_irqsave (&sync_timeline_list_lock, flags); -list_add_tail (&obj->sync_timeline_list, &sync_timeline_list_head); -spin_unlock_irqrestore (&sync_timeline_list_lock, flags); +sync_timeline_debug_add (obj);return obj; }@@ -77,11 +65,8 @@ static void sync_timeline_free(struct kref *kref) {struct sync_timeline *obj = container_of(kref, struct sync_timeline, kref); -unsigned long flags; -spin_lock_irqsave (&sync_timeline_list_lock, flags); -list_del (&obj->sync_timeline_list); -spin_unlock_irqrestore (&sync_timeline_list_lock, flags); +sync_timeline_debug_remove (obj);if (obj->ops->release_obj) obj->ops->release_obj (obj);@@ -89,6 +74,16 @@ static void sync_timeline_free(struct kref *kref)kfree (obj); } +static void sync_timeline_get (struct sync_timeline *obj) +{ +kref_get (&obj->kref); +} + +static void sync_timeline_put (struct sync_timeline *obj) +{ +kref_put (&obj->kref, sync_timeline_free); +} +void sync_timeline_destroy (struct sync_timeline *obj) { obj->destroyed = true;@@ -98,75 +93,30 @@ void sync_timeline_destroy(struct sync_timeline *obj)* signal any children that their parent is going away. */ sync_timeline_signal (obj); - -kref_put (&obj->kref, sync_timeline_free); +sync_timeline_put (obj); }EXPORT_SYMBOL (sync_timeline_destroy); -static void sync_timeline_add_pt (struct sync_timeline *obj,struct sync_pt *pt) -{ -unsigned long flags; - - pt->parent = obj; - -spin_lock_irqsave (&obj->child_list_lock, flags); -list_add_tail (&pt->child_list, &obj->child_list_head); -spin_unlock_irqrestore (&obj->child_list_lock, flags); -} - -static void sync_timeline_remove_pt (struct sync_pt *pt) -{ -struct sync_timeline *obj = pt->parent; -unsigned long flags; - -spin_lock_irqsave (&obj->active_list_lock, flags); -if (!list_empty (&pt->active_list)) -list_del_init (&pt->active_list); -spin_unlock_irqrestore (&obj->active_list_lock, flags); - -spin_lock_irqsave (&obj->child_list_lock, flags); -if (!list_empty (&pt->child_list)) -list_del_init (&pt->child_list); - -spin_unlock_irqrestore (&obj->child_list_lock, flags); -} -void sync_timeline_signal (struct sync_timeline *obj) {unsigned long flags;LIST_HEAD (signaled_pts); -struct list_head *pos, *n; +struct sync_pt *pt, *next;trace_sync_timeline (obj); -spin_lock_irqsave (&obj->active_list_lock, flags); - -list_for_each_safe (pos, n, &obj->active_list_head) { -struct sync_pt *pt = - container_of(pos, struct sync_pt, active_list); - -if (_sync_pt_has_signaled (pt)) { -list_del_init (pos); -list_add (&pt->signaled_list, &signaled_pts); -kref_get (&pt->fence->kref); - } - } - -spin_unlock_irqrestore (&obj->active_list_lock, flags); - -list_for_each_safe (pos, n, &signaled_pts) { -struct sync_pt *pt = - container_of(pos, struct sync_pt, signaled_list); - -list_del_init (pos); -sync_fence_signal_pt (pt); -kref_put (&pt->fence->kref, sync_fence_free); +spin_lock_irqsave (&obj->child_list_lock, flags); +list_for_each_entry_safe (pt, next, &obj->active_list_head, active_list) { +if (__fence_is_signaled (&pt->base)) +list_del (&pt->active_list); } +spin_unlock_irqrestore (&obj->child_list_lock, flags); }EXPORT_SYMBOL (sync_timeline_signal); -struct sync_pt *sync_pt_create (struct sync_timeline *parent,int size) +struct sync_pt *sync_pt_create (struct sync_timeline *obj,int size) { +unsigned long flags;struct sync_pt *pt;if (size <sizeof (struct sync_pt ))@@ -176,87 +126,27 @@ struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size)if (pt ==NULL )return NULL ; +spin_lock_irqsave (&obj->child_list_lock, flags); +sync_timeline_get (obj); +__fence_init (&pt->base, &android_fence_ops, &obj->child_list_lock, obj->context, ++obj->value); +list_add_tail (&pt->child_list, &obj->child_list_head);INIT_LIST_HEAD (&pt->active_list); -kref_get (&parent->kref); -sync_timeline_add_pt (parent, pt); - +spin_unlock_irqrestore (&obj->child_list_lock, flags);return pt; }EXPORT_SYMBOL (sync_pt_create);void sync_pt_free (struct sync_pt *pt) { -if (pt->parent->ops->free_pt) - pt->parent->ops->free_pt (pt); - -sync_timeline_remove_pt (pt); - -kref_put (&pt->parent->kref, sync_timeline_free); - -kfree (pt); +fence_put (&pt->base); }EXPORT_SYMBOL (sync_pt_free); -/* call with pt->parent->active_list_lock held */ -static int _sync_pt_has_signaled (struct sync_pt *pt) -{ -int old_status = pt->status; - -if (!pt->status) - pt->status = pt->parent->ops->has_signaled (pt); - -if (!pt->status && pt->parent->destroyed) - pt->status = -ENOENT ; - -if (pt->status != old_status) - pt->timestamp =ktime_get (); - -return pt->status; -} - -static struct sync_pt *sync_pt_dup (struct sync_pt *pt) -{ -return pt->parent->ops->dup (pt); -} - -/* Adds a sync pt to the active queue. Called when added to a fence */ -static void sync_pt_activate (struct sync_pt *pt) -{ -struct sync_timeline *obj = pt->parent; -unsigned long flags; -int err; - -spin_lock_irqsave (&obj->active_list_lock, flags); - - err =_sync_pt_has_signaled (pt); -if (err !=0 ) - goto out; - -list_add_tail (&pt->active_list, &obj->active_list_head); - -out: -spin_unlock_irqrestore (&obj->active_list_lock, flags); -} - -static int sync_fence_release (struct inode *inode,struct file *file); -static unsigned int sync_fence_poll (struct file *file,poll_table *wait); -static long sync_fence_ioctl (struct file *file,unsigned int cmd, -unsigned long arg); - - -static const struct file_operations sync_fence_fops = { - .release = sync_fence_release, - .poll = sync_fence_poll, - .unlocked_ioctl = sync_fence_ioctl, - .compat_ioctl = sync_fence_ioctl, -}; - -static struct sync_fence *sync_fence_alloc (const char *name) +static struct sync_fence *sync_fence_alloc (int size,const char *name) {struct sync_fence *fence; -unsigned long flags; - fence =kzalloc (sizeof (struct sync_fence ),GFP_KERNEL ); + fence =kzalloc (size,GFP_KERNEL );if (fence ==NULL )return NULL ;@@ -268,16 +158,8 @@ static struct sync_fence *sync_fence_alloc(const char *name)kref_init (&fence->kref);strlcpy (fence->name, name,sizeof (fence->name)); -INIT_LIST_HEAD (&fence->pt_list_head); -INIT_LIST_HEAD (&fence->waiter_list_head); -spin_lock_init (&fence->waiter_list_lock); -init_waitqueue_head (&fence->wq); -spin_lock_irqsave (&sync_fence_list_lock, flags); -list_add_tail (&fence->sync_fence_list, &sync_fence_list_head); -spin_unlock_irqrestore (&sync_fence_list_lock, flags); -return fence; err:
drivers/staging/android/sync.c err: · line 175
Quoted 9 lines
drivers/staging/android/sync.c +6
-0@@ -285,119 +167,40 @@ err:return NULL ; } +static void fence_check_cb_func (struct fence *f,struct fence_cb *cb) +{ + struct sync_fence_cb *check = container_of(cb, struct sync_fence_cb, cb); +struct sync_fence *fence = check->fence; + +// TODO: Add a fence->status member and check it
Hmm, C++ / C99 style comments makes checkpatch.pl complain. Did you run this series through checkpatch?
/Thomas
Re: [PATCH 4/6] android: convert sync to fence api, v4
op 19-02-14 14:56, Thomas Hellstrom schreef:
Quoted 11 lines
Quoted 6 lines
drivers/staging/android/sync.c err: · line 175
Quoted 6 lines
drivers/staging/android/sync.c +6
-0+static void fence_check_cb_func (struct fence *f,struct fence_cb *cb) +{ + struct sync_fence_cb *check = container_of(cb, struct sync_fence_cb, cb); +struct sync_fence *fence = check->fence; + +// TODO: Add a fence->status member and check it Hmm, C++ / C99 style comments makes checkpatch.pl complain. Did you run this series through checkpatch?
/Thomas
Actually I used c99 here because it shouldn't have been in the sent patch. ;-)
Right below that comment I use fence->status, so the right thing to do was to zap the comment.
Thanks for catching it!
~Maarten\
Re: [PATCH 4/6] android: convert sync to fence api, v4
In reply to Maarten LankhorstQuoted 12 lines · Maarten Lankhorst
Android syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Callfence_remove_callbackinsync_fence_freeif not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.
Quoted 2 lines
Signed-off-by Maarten Lankhorst <[email protected]>---
Snipped everything but headers - Ian Lister from our android team is signed up to have a more in-depth look at proper integration with android syncpoints. Adding him to cc.
Quoted 57 lines
drivers/staging/android/sync.h +5
-11@@ -21,6 +21,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/wait.h> +#include <linux/fence.h> struct sync_timeline ;struct sync_pt ;@@ -40,8 +41,6 @@ struct sync_fence;* -1 if a will signal before b * @free_pt: called before sync_pt is freed * @release_obj: called before sync_timeline is freed -* @print_obj: deprecated -* @print_pt: deprecated * @fill_driver_data: write implementation specific driver data to data. * should return an error if there is not enough room * as specified by size. This information is returned @@ -67,13 +66,6 @@ struct sync_timeline_ops {/* optional */ void (*release_obj)(struct sync_timeline *sync_timeline); -/* deprecated */ -void (*print_obj)(struct seq_file *s, -struct sync_timeline *sync_timeline); - -/* deprecated */ -void (*print_pt)(struct seq_file *s,struct sync_pt *sync_pt); -/* optional */ int (*fill_driver_data)(struct sync_pt *syncpt,void *data,int size);@@ -104,42 +96,48 @@ struct sync_timeline {/* protected by child_list_lock */ bool destroyed; +int context, value;struct list_head child_list_head;spinlock_t child_list_lock;struct list_head active_list_head; -spinlock_t active_list_lock; +#ifdef CONFIG_DEBUG_FS struct list_head sync_timeline_list; +#endif };/** * struct sync_pt - sync point -* @parent: sync_timeline to which this sync_pt belongs +* @fence: base fence class * @child_list: membership in sync_timeline.child_list_head
drivers/staging/android/sync.h struct sync_timeline { · line 124
Quoted 10 lines
drivers/staging/android/sync.h +3
-0* @active_list: membership in sync_timeline.active_list_head +<<<<<<< current * @signaled_list: membership in temporary signaled_list on stack * @fence: sync_fence to which the sync_pt belongs * @pt_list: membership in sync_fence.pt_list_head * @status: 1: signaled, 0:active, <0: error * @timestamp: time which sync_pt status transitioned from active to * signaled or error. +======= +>>>>>>> patched
Conflict markers ...
drivers/staging/android/sync.h struct sync_timeline { · line 127
Quoted 5 lines
drivers/staging/android/sync.h +1
-2*/ struct sync_pt { -struct sync_timeline *parent; -struct list_head child_list; +struct fence base;
Hm, embedding feels wrong, since that still means that I'll need to implement two kinds of fences in i915 - one using the seqno fence to make dma-buf sync work, and one to implmenent sync_pt to make the android folks happy.
If I can dream I think we should have a pointer to an underlying fence here, i.e. a struct sync_pt would just be a userspace interface wrapper to do explicit syncing using native fences, instead of implicit syncing like with dma-bufs. But this is all drive-by comments from a very cursory high-level look. I might be full of myself again ;-)
-Daniel
Quoted 99 lines
drivers/staging/android/sync.h +37
-21+struct list_head child_list;struct list_head active_list; -struct list_head signaled_list; - -struct sync_fence *fence; -struct list_head pt_list; +}; -/* protected by parent->active_list_lock */ -int status; +static inline struct sync_timeline *sync_pt_parent (struct sync_pt *pt) { + return container_of(pt->base.lock, struct sync_timeline, child_list_lock); +} -ktime_t timestamp; +struct sync_fence_cb { +struct fence_cb cb; +struct fence *sync_pt; +struct sync_fence *fence; };/** @@ -149,9 +147,7 @@ struct sync_pt {* @name: name of sync_fence. Useful for debugging * @pt_list_head: list of sync_pts in the fence. immutable once fence * is created -* @waiter_list_head: list of asynchronous waiters on this fence -* @waiter_list_lock: lock protecting @waiter_list_head and @status -* @status: 1: signaled, 0:active, <0: error +* @status: 0: signaled, >0:active, <0: error * * @wq: wait queue for fence signaling * @sync_fence_list: membership in global fence list @@ -160,17 +156,15 @@ struct sync_fence {struct file *file;struct kref kref;char name[32 ]; - -/* this list is immutable once the fence is created */ -struct list_head pt_list_head; - -struct list_head waiter_list_head; -spinlock_t waiter_list_lock;/* also protects status */ -int status; +#ifdef CONFIG_DEBUG_FS +struct list_head sync_fence_list; +#endif +int num_fences;wait_queue_head_t wq; +atomic_t status; -struct list_head sync_fence_list; +struct sync_fence_cb cbs[]; };struct sync_fence_waiter ;@@ -184,14 +178,14 @@ typedef void (*sync_callback_t)(struct sync_fence *fence,* @callback_data: pointer to pass to @callback */ struct sync_fence_waiter { -struct list_head waiter_list; - -sync_callback_t callback; +wait_queue_t work; +sync_callback_t callback; };static inline void sync_fence_waiter_init (struct sync_fence_waiter *waiter,sync_callback_t callback) { +INIT_LIST_HEAD (&waiter->work.task_list); waiter->callback = callback; }@@ -423,4 +417,22 @@ struct sync_fence_info_data {#define SYNC_IOC_FENCE_INFO _IOWR(SYNC_IOC_MAGIC, 2,\ struct sync_fence_info_data) +#ifdef CONFIG_DEBUG_FS + +extern void sync_timeline_debug_add (struct sync_timeline *obj); +extern void sync_timeline_debug_remove (struct sync_timeline *obj); +extern void sync_fence_debug_add (struct sync_fence *fence); +extern void sync_fence_debug_remove (struct sync_fence *fence); +extern void sync_dump (void ); + +#else +# define sync_timeline_debug_add (obj) +# define sync_timeline_debug_remove (obj) +# define sync_fence_debug_add (fence) +# define sync_fence_debug_remove (fence) +# define sync_dump () +#endif +int sync_fence_wake_up_wq (wait_queue_t *curr,unsigned mode, +int wake_flags,void *key); +#endif /* _LINUX_SYNC_H */
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 4/6] android: convert sync to fence api, v4
op 03-03-14 22:11, Daniel Vetter schreef:
Quoted 71 lines · Maarten Lankhorst
Quoted 12 lines
Android syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Callfence_remove_callbackinsync_fence_freeif not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.Quoted 2 lines
Signed-off-by Maarten Lankhorst <[email protected]>---
Snipped everything but headers - Ian Lister from our android team is signed up to have a more in-depth look at proper integration with android syncpoints. Adding him to cc.
Quoted 52 lines
drivers/staging/android/sync.h +4
-10@@ -21,6 +21,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/wait.h> +#include <linux/fence.h> struct sync_timeline ;struct sync_pt ;@@ -40,8 +41,6 @@ struct sync_fence;* -1 if a will signal before b * @free_pt: called before sync_pt is freed * @release_obj: called before sync_timeline is freed -* @print_obj: deprecated -* @print_pt: deprecated * @fill_driver_data: write implementation specific driver data to data. * should return an error if there is not enough room * as specified by size. This information is returned @@ -67,13 +66,6 @@ struct sync_timeline_ops {/* optional */ void (*release_obj)(struct sync_timeline *sync_timeline); -/* deprecated */ -void (*print_obj)(struct seq_file *s, -struct sync_timeline *sync_timeline); - -/* deprecated */ -void (*print_pt)(struct seq_file *s,struct sync_pt *sync_pt); -/* optional */ int (*fill_driver_data)(struct sync_pt *syncpt,void *data,int size);@@ -104,42 +96,48 @@ struct sync_timeline {/* protected by child_list_lock */ bool destroyed; +int context, value;struct list_head child_list_head;spinlock_t child_list_lock;struct list_head active_list_head; -spinlock_t active_list_lock; +#ifdef CONFIG_DEBUG_FS struct list_head sync_timeline_list; +#endif };
Quoted 16 lines
Quoted 5 lines
drivers/staging/android/sync.h +1
-1/** * struct sync_pt - sync point -* @parent: sync_timeline to which this sync_pt belongs +* @fence: base fence class * @child_list: membership in sync_timeline.child_list_head drivers/staging/android/sync.h struct sync_timeline { · line 124
Quoted 10 lines
drivers/staging/android/sync.h +3
-0* @active_list: membership in sync_timeline.active_list_head +<<<<<<< current * @signaled_list: membership in temporary signaled_list on stack * @fence: sync_fence to which the sync_pt belongs * @pt_list: membership in sync_fence.pt_list_head * @status: 1: signaled, 0:active, <0: error * @timestamp: time which sync_pt status transitioned from active to * signaled or error. +======= +>>>>>>> patchedConflict markers ...
Oops.
Quoted 10 lines
drivers/staging/android/sync.h struct sync_timeline { · line 127
Quoted 5 lines
drivers/staging/android/sync.h +1
-2*/ struct sync_pt { -struct sync_timeline *parent; -struct list_head child_list; +struct fence base;Hm, embedding feels wrong, since that still means that I'll need to implement two kinds of fences in i915 - one using the seqno fence to make dma-buf sync work, and one to implmenent
sync_ptto make the android folks happy.
Quoted 7 lines
If I can dream I think we should have a pointer to an underlying fence here, i.e. a struct
sync_ptwould just be a userspace interface wrapper to do explicit syncing using native fences, instead of implicit syncing like with dma-bufs. But this is all drive-by comments from a very cursory high-level look. I might be full of myself again ;-)
-Daniel
No, the idea is that because android syncpoint is simply another type of dma-fence, that if you deal with normal fences then android can automatically be handled too. The userspace fence api android exposes could be very easily made to work for dma-fence, just pass a dma-fence to sync_fence_create. So exposing dma-fence would probably work for android too.
~Maarten
Re: [PATCH 4/6] android: convert sync to fence api, v4
Quoted 106 lines · Maarten Lankhorst
op 03-03-14 22:11, Daniel Vetter schreef:
Quoted 71 lines · Maarten Lankhorst
Quoted 12 lines
Android syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Callfence_remove_callbackinsync_fence_freeif not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.Quoted 2 lines
Signed-off-by Maarten Lankhorst <[email protected]>---
Snipped everything but headers - Ian Lister from our android team is signed up to have a more in-depth look at proper integration with android syncpoints. Adding him to cc.
Quoted 52 lines
drivers/staging/android/sync.h +4
-10@@ -21,6 +21,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/wait.h> +#include <linux/fence.h> struct sync_timeline ;struct sync_pt ;@@ -40,8 +41,6 @@ struct sync_fence;* -1 if a will signal before b * @free_pt: called before sync_pt is freed * @release_obj: called before sync_timeline is freed -* @print_obj: deprecated -* @print_pt: deprecated * @fill_driver_data: write implementation specific driver data to data. * should return an error if there is not enough room * as specified by size. This information is returned @@ -67,13 +66,6 @@ struct sync_timeline_ops {/* optional */ void (*release_obj)(struct sync_timeline *sync_timeline); -/* deprecated */ -void (*print_obj)(struct seq_file *s, -struct sync_timeline *sync_timeline); - -/* deprecated */ -void (*print_pt)(struct seq_file *s,struct sync_pt *sync_pt); -/* optional */ int (*fill_driver_data)(struct sync_pt *syncpt,void *data,int size);@@ -104,42 +96,48 @@ struct sync_timeline {/* protected by child_list_lock */ bool destroyed; +int context, value;struct list_head child_list_head;spinlock_t child_list_lock;struct list_head active_list_head; -spinlock_t active_list_lock; +#ifdef CONFIG_DEBUG_FS struct list_head sync_timeline_list; +#endif };Quoted 16 lines
Quoted 5 lines
drivers/staging/android/sync.h +1
-1/** * struct sync_pt - sync point -* @parent: sync_timeline to which this sync_pt belongs +* @fence: base fence class * @child_list: membership in sync_timeline.child_list_head drivers/staging/android/sync.h struct sync_timeline { · line 124
Quoted 10 lines
drivers/staging/android/sync.h +3
-0* @active_list: membership in sync_timeline.active_list_head +<<<<<<< current * @signaled_list: membership in temporary signaled_list on stack * @fence: sync_fence to which the sync_pt belongs * @pt_list: membership in sync_fence.pt_list_head * @status: 1: signaled, 0:active, <0: error * @timestamp: time which sync_pt status transitioned from active to * signaled or error. +======= +>>>>>>> patchedConflict markers ...
Oops.
Quoted 10 lines
drivers/staging/android/sync.h struct sync_timeline { · line 127
Quoted 5 lines
drivers/staging/android/sync.h +1
-2*/ struct sync_pt { -struct sync_timeline *parent; -struct list_head child_list; +struct fence base;Hm, embedding feels wrong, since that still means that I'll need to implement two kinds of fences in i915 - one using the seqno fence to make dma-buf sync work, and one to implmenent
sync_ptto make the android folks happy.Quoted 7 lines
If I can dream I think we should have a pointer to an underlying fence here, i.e. a struct
sync_ptwould just be a userspace interface wrapper to do explicit syncing using native fences, instead of implicit syncing like with dma-bufs. But this is all drive-by comments from a very cursory high-level look. I might be full of myself again ;-)
-Daniel
Quoted 6 lines
No, the idea is that because android syncpoint is simply another type of dma-fence, that if you deal with normal fences then android can automatically be handled too. The userspace fence api android exposes could be very easily made to work for dma-fence, just pass a dma-fence to
sync_fence_create.
So exposing dma-fence would probably work for android too.
Hm, then why do we still have struct sync_pt around? Since it's just the internal bit, with the userspace facing object being struct sync_fence, I'd opt to shuffle any useful features into the core struct fence. -Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 4/6] android: convert sync to fence api, v4
op 04-03-14 09:14, Daniel Vetter schreef:
Quoted 113 lines · Maarten Lankhorst
Quoted 106 lines
op 03-03-14 22:11, Daniel Vetter schreef:
Quoted 71 lines · Maarten Lankhorst
Quoted 12 lines
Android syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Callfence_remove_callbackinsync_fence_freeif not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.Quoted 2 lines
Signed-off-by Maarten Lankhorst <[email protected]>---
Snipped everything but headers - Ian Lister from our android team is signed up to have a more in-depth look at proper integration with android syncpoints. Adding him to cc.
Quoted 52 lines
drivers/staging/android/sync.h +4
-10@@ -21,6 +21,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/wait.h> +#include <linux/fence.h> struct sync_timeline ;struct sync_pt ;@@ -40,8 +41,6 @@ struct sync_fence;* -1 if a will signal before b * @free_pt: called before sync_pt is freed * @release_obj: called before sync_timeline is freed -* @print_obj: deprecated -* @print_pt: deprecated * @fill_driver_data: write implementation specific driver data to data. * should return an error if there is not enough room * as specified by size. This information is returned @@ -67,13 +66,6 @@ struct sync_timeline_ops {/* optional */ void (*release_obj)(struct sync_timeline *sync_timeline); -/* deprecated */ -void (*print_obj)(struct seq_file *s, -struct sync_timeline *sync_timeline); - -/* deprecated */ -void (*print_pt)(struct seq_file *s,struct sync_pt *sync_pt); -/* optional */ int (*fill_driver_data)(struct sync_pt *syncpt,void *data,int size);@@ -104,42 +96,48 @@ struct sync_timeline {/* protected by child_list_lock */ bool destroyed; +int context, value;struct list_head child_list_head;spinlock_t child_list_lock;struct list_head active_list_head; -spinlock_t active_list_lock; +#ifdef CONFIG_DEBUG_FS struct list_head sync_timeline_list; +#endif };Quoted 16 lines
Quoted 5 lines
drivers/staging/android/sync.h +1
-1/** * struct sync_pt - sync point -* @parent: sync_timeline to which this sync_pt belongs +* @fence: base fence class * @child_list: membership in sync_timeline.child_list_head drivers/staging/android/sync.h struct sync_timeline { · line 124
Quoted 10 lines
drivers/staging/android/sync.h +3
-0* @active_list: membership in sync_timeline.active_list_head +<<<<<<< current * @signaled_list: membership in temporary signaled_list on stack * @fence: sync_fence to which the sync_pt belongs * @pt_list: membership in sync_fence.pt_list_head * @status: 1: signaled, 0:active, <0: error * @timestamp: time which sync_pt status transitioned from active to * signaled or error. +======= +>>>>>>> patchedConflict markers ...
Oops.
Quoted 10 lines
drivers/staging/android/sync.h struct sync_timeline { · line 127
Quoted 5 lines
drivers/staging/android/sync.h +1
-2*/ struct sync_pt { -struct sync_timeline *parent; -struct list_head child_list; +struct fence base;Hm, embedding feels wrong, since that still means that I'll need to implement two kinds of fences in i915 - one using the seqno fence to make dma-buf sync work, and one to implmenent
sync_ptto make the android folks happy.Quoted 7 lines
If I can dream I think we should have a pointer to an underlying fence here, i.e. a struct
sync_ptwould just be a userspace interface wrapper to do explicit syncing using native fences, instead of implicit syncing like with dma-bufs. But this is all drive-by comments from a very cursory high-level look. I might be full of myself again ;-)
-DanielQuoted 6 lines
No, the idea is that because android syncpoint is simply another type of dma-fence, that if you deal with normal fences then android can automatically be handled too. The userspace fence api android exposes could be very easily made to work for dma-fence, just pass a dma-fence to
sync_fence_create.
So exposing dma-fence would probably work for android too.
Quoted 4 lines
Hm, then why do we still have struct
sync_ptaround? Since it's just the internal bit, with the userspace facing object being structsync_fence, I'd opt to shuffle any useful features into the core struct fence. -Daniel
To keep compatibility with the android api. I think that gradually converting them is going to be more useful than to force all drivers to use a new api all at once. They could keep android syncpoint api for exporting, as long as they accept dma-fence for importing/waiting.
~Maarten
Re: [PATCH 4/6] android: convert sync to fence api, v4
Quoted 114 lines · Maarten Lankhorst
op 04-03-14 09:14, Daniel Vetter schreef:
Quoted 113 lines · Maarten Lankhorst
Quoted 112 lines
op 03-03-14 22:11, Daniel Vetter schreef:
Quoted 71 lines · Maarten Lankhorst
Quoted 12 lines
Android syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Callfence_remove_callbackinsync_fence_freeif not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.Quoted 2 lines
Signed-off-by Maarten Lankhorst <[email protected]>---
Snipped everything but headers - Ian Lister from our android team is signed up to have a more in-depth look at proper integration with android syncpoints. Adding him to cc.
Quoted 52 lines
drivers/staging/android/sync.h +4
-10@@ -21,6 +21,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/wait.h> +#include <linux/fence.h> struct sync_timeline ;struct sync_pt ;@@ -40,8 +41,6 @@ struct sync_fence;* -1 if a will signal before b * @free_pt: called before sync_pt is freed * @release_obj: called before sync_timeline is freed -* @print_obj: deprecated -* @print_pt: deprecated * @fill_driver_data: write implementation specific driver data to data. * should return an error if there is not enough room * as specified by size. This information is returned @@ -67,13 +66,6 @@ struct sync_timeline_ops {/* optional */ void (*release_obj)(struct sync_timeline *sync_timeline); -/* deprecated */ -void (*print_obj)(struct seq_file *s, -struct sync_timeline *sync_timeline); - -/* deprecated */ -void (*print_pt)(struct seq_file *s,struct sync_pt *sync_pt); -/* optional */ int (*fill_driver_data)(struct sync_pt *syncpt,void *data,int size);@@ -104,42 +96,48 @@ struct sync_timeline {/* protected by child_list_lock */ bool destroyed; +int context, value;struct list_head child_list_head;spinlock_t child_list_lock;struct list_head active_list_head; -spinlock_t active_list_lock; +#ifdef CONFIG_DEBUG_FS struct list_head sync_timeline_list; +#endif };Quoted 16 lines
Quoted 5 lines
drivers/staging/android/sync.h +1
-1/** * struct sync_pt - sync point -* @parent: sync_timeline to which this sync_pt belongs +* @fence: base fence class * @child_list: membership in sync_timeline.child_list_head drivers/staging/android/sync.h struct sync_timeline { · line 124
Quoted 10 lines
drivers/staging/android/sync.h +3
-0* @active_list: membership in sync_timeline.active_list_head +<<<<<<< current * @signaled_list: membership in temporary signaled_list on stack * @fence: sync_fence to which the sync_pt belongs * @pt_list: membership in sync_fence.pt_list_head * @status: 1: signaled, 0:active, <0: error * @timestamp: time which sync_pt status transitioned from active to * signaled or error. +======= +>>>>>>> patchedConflict markers ...
Oops.
Quoted 10 lines
drivers/staging/android/sync.h struct sync_timeline { · line 127
Quoted 5 lines
drivers/staging/android/sync.h +1
-2*/ struct sync_pt { -struct sync_timeline *parent; -struct list_head child_list; +struct fence base;Hm, embedding feels wrong, since that still means that I'll need to implement two kinds of fences in i915 - one using the seqno fence to make dma-buf sync work, and one to implmenent
sync_ptto make the android folks happy.Quoted 7 lines
If I can dream I think we should have a pointer to an underlying fence here, i.e. a struct
sync_ptwould just be a userspace interface wrapper to do explicit syncing using native fences, instead of implicit syncing like with dma-bufs. But this is all drive-by comments from a very cursory high-level look. I might be full of myself again ;-)
-DanielNo, the idea is that because android syncpoint is simply another type of dma-fence, that if you deal with normal fences then android can automatically be handled too. The userspace fence api android exposes could be very easily made to work for dma-fence, just pass a dma-fence to
sync_fence_create.
So exposing dma-fence would probably work for android too.
Quoted 7 lines
Quoted 4 lines
Hm, then why do we still have struct
sync_ptaround? Since it's just the internal bit, with the userspace facing object being structsync_fence, I'd opt to shuffle any useful features into the core struct fence. -DanielTo keep compatibility with the android api. I think that gradually converting them is going to be more useful than to force all drivers to use a new api all at once. They could keep android syncpoint api for exporting, as long as they accept dma-fence for importing/waiting.
We don't have any users of the android sync_pt stuff (outside of the framework itself). So any considerations for existing drivers for upstreaming are imo moot. At least for the in-kernel interfaces used. For the actual userspace interface I guess keeping the android syncpt ioctls as-is has value, at least if we conclude that their not badly broken. In which case we need to fix that before moving it out of staging.
Or are we just talking past each another here?
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 4/6] android: convert sync to fence api, v4
op 04-03-14 11:00, Daniel Vetter schreef:
Quoted 122 lines · Maarten Lankhorst
Quoted 114 lines
op 04-03-14 09:14, Daniel Vetter schreef:
Quoted 113 lines · Maarten Lankhorst
Quoted 112 lines
op 03-03-14 22:11, Daniel Vetter schreef:
Quoted 71 lines · Maarten Lankhorst
Quoted 12 lines
Android syncpoints can be mapped to a timeline. This removes the need to maintain a separate api for synchronization. I've left the android trace events in place, but the core fence events should already be sufficient for debugging.
v2:
- Callfence_remove_callbackinsync_fence_freeif not all fences have fired. v3:
- Merge Colin Cross' bugfixes, and the android fence merge optimization. v4:
- Merge with the upstream fixes.Quoted 2 lines
Signed-off-by Maarten Lankhorst <[email protected]>---
Snipped everything but headers - Ian Lister from our android team is signed up to have a more in-depth look at proper integration with android syncpoints. Adding him to cc.
Quoted 52 lines
drivers/staging/android/sync.h +4
-10@@ -21,6 +21,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/wait.h> +#include <linux/fence.h> struct sync_timeline ;struct sync_pt ;@@ -40,8 +41,6 @@ struct sync_fence;* -1 if a will signal before b * @free_pt: called before sync_pt is freed * @release_obj: called before sync_timeline is freed -* @print_obj: deprecated -* @print_pt: deprecated * @fill_driver_data: write implementation specific driver data to data. * should return an error if there is not enough room * as specified by size. This information is returned @@ -67,13 +66,6 @@ struct sync_timeline_ops {/* optional */ void (*release_obj)(struct sync_timeline *sync_timeline); -/* deprecated */ -void (*print_obj)(struct seq_file *s, -struct sync_timeline *sync_timeline); - -/* deprecated */ -void (*print_pt)(struct seq_file *s,struct sync_pt *sync_pt); -/* optional */ int (*fill_driver_data)(struct sync_pt *syncpt,void *data,int size);@@ -104,42 +96,48 @@ struct sync_timeline {/* protected by child_list_lock */ bool destroyed; +int context, value;struct list_head child_list_head;spinlock_t child_list_lock;struct list_head active_list_head; -spinlock_t active_list_lock; +#ifdef CONFIG_DEBUG_FS struct list_head sync_timeline_list; +#endif };Quoted 16 lines
Quoted 5 lines
drivers/staging/android/sync.h +1
-1/** * struct sync_pt - sync point -* @parent: sync_timeline to which this sync_pt belongs +* @fence: base fence class * @child_list: membership in sync_timeline.child_list_head drivers/staging/android/sync.h struct sync_timeline { · line 124
Quoted 10 lines
drivers/staging/android/sync.h +3
-0* @active_list: membership in sync_timeline.active_list_head +<<<<<<< current * @signaled_list: membership in temporary signaled_list on stack * @fence: sync_fence to which the sync_pt belongs * @pt_list: membership in sync_fence.pt_list_head * @status: 1: signaled, 0:active, <0: error * @timestamp: time which sync_pt status transitioned from active to * signaled or error. +======= +>>>>>>> patchedConflict markers ...
Oops.
Quoted 10 lines
drivers/staging/android/sync.h struct sync_timeline { · line 127
Quoted 5 lines
drivers/staging/android/sync.h +1
-2*/ struct sync_pt { -struct sync_timeline *parent; -struct list_head child_list; +struct fence base;Hm, embedding feels wrong, since that still means that I'll need to implement two kinds of fences in i915 - one using the seqno fence to make dma-buf sync work, and one to implmenent
sync_ptto make the android folks happy.Quoted 7 lines
If I can dream I think we should have a pointer to an underlying fence here, i.e. a struct
sync_ptwould just be a userspace interface wrapper to do explicit syncing using native fences, instead of implicit syncing like with dma-bufs. But this is all drive-by comments from a very cursory high-level look. I might be full of myself again ;-)
-DanielNo, the idea is that because android syncpoint is simply another type of dma-fence, that if you deal with normal fences then android can automatically be handled too. The userspace fence api android exposes could be very easily made to work for dma-fence, just pass a dma-fence to
sync_fence_create.
So exposing dma-fence would probably work for android too.Quoted 7 lines
Quoted 4 lines
Hm, then why do we still have struct
sync_ptaround? Since it's just the internal bit, with the userspace facing object being structsync_fence, I'd opt to shuffle any useful features into the core struct fence. -DanielTo keep compatibility with the android api. I think that gradually converting them is going to be more useful than to force all drivers to use a new api all at once. They could keep android syncpoint api for exporting, as long as they accept dma-fence for importing/waiting.
Quoted 6 lines
We don't have any users of the android
sync_ptstuff (outside of the framework itself). So any considerations for existing drivers for upstreaming are imo moot. At least for the in-kernel interfaces used. For the actual userspace interface I guess keeping the android syncpt ioctls as-is has value, at least if we conclude that their not badly broken. In which case we need to fix that before moving it out of staging.
Any driver has to implement sync_timeline_ops though, which interacts with sync_pt?
And creating a userspace fence requires a sync_pt, although it could very trivially be changed to require a fence too.
~Maarten
PATCH 5/6 reservation: add support for fences to enable cross-device synchronisation
In reply to Maarten Lankhorst--- include/linux/reservation.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
include/linux/reservation.h +17 -1
@@ -6,7 +6,7 @@* Copyright (C) 2012 Texas Instruments * * Authors: -* Rob Clark <[email protected]> +* Rob Clark <[email protected]> * Maarten Lankhorst <[email protected]> * Thomas Hellstrom <thellstrom-at-vmware-dot-com> *
@@ -40,22 +40,38 @@#define _LINUX_RESERVATION_H#include <linux/ww_mutex.h> +#include <linux/fence.h> extern struct ww_class reservation_ww_class;struct reservation_object {struct ww_mutex lock; + +struct fence *fence_excl; +struct fence **fence_shared; +u32 fence_shared_count, fence_shared_max; };static inline void reservation_object_init (struct reservation_object *obj) {ww_mutex_init (&obj->lock, &reservation_ww_class); + + obj->fence_shared_count = obj->fence_shared_max =0 ; + obj->fence_shared =NULL ; + obj->fence_excl =NULL ; }static inline void reservation_object_fini (struct reservation_object *obj) { +int i; + +if (obj->fence_excl) +fence_put (obj->fence_excl); +for (i =0 ; i < obj->fence_shared_count; ++i) +fence_put (obj->fence_shared[i]); +ww_mutex_destroy (&obj->lock); }
Re: [PATCH 5/6] reservation: add support for fences to enable cross-device synchronisation
Quoted 1 line · Maarten Lankhorst
Signed-off-by Maarten Lankhorst <[email protected]>
Quoted 57 lines
--- include/linux/reservation.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)include/linux/reservation.h +17
-1@@ -6,7 +6,7 @@* Copyright (C) 2012 Texas Instruments * * Authors: -* Rob Clark <[email protected]> +* Rob Clark <[email protected]> * Maarten Lankhorst <[email protected]> * Thomas Hellstrom <thellstrom-at-vmware-dot-com> * @@ -40,22 +40,38 @@#define _LINUX_RESERVATION_H#include <linux/ww_mutex.h> +#include <linux/fence.h> extern struct ww_class reservation_ww_class;struct reservation_object {struct ww_mutex lock; + +struct fence *fence_excl; +struct fence **fence_shared; +u32 fence_shared_count, fence_shared_max; };static inline void reservation_object_init (struct reservation_object *obj) {ww_mutex_init (&obj->lock, &reservation_ww_class); + + obj->fence_shared_count = obj->fence_shared_max =0 ; + obj->fence_shared =NULL ; + obj->fence_excl =NULL ; }static inline void reservation_object_fini (struct reservation_object *obj) { +int i; + +if (obj->fence_excl) +fence_put (obj->fence_excl); +for (i =0 ; i < obj->fence_shared_count; ++i) +fence_put (obj->fence_shared[i]); +ww_mutex_destroy (&obj->lock); }
PATCH 6/6 dma-buf: add poll support, v2
In reply to Maarten LankhorstThanks to Fengguang Wu for spotting a missing static cast.
v2:
- Kill unused variable need_shared.
--- drivers/base/dma-buf.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 12 ++++++ 2 files changed, 113 insertions(+)
drivers/base/dma-buf.c +101 -0
@@ -30,6 +30,7 @@#include <linux/export.h> #include <linux/debugfs.h> #include <linux/seq_file.h> +#include <linux/poll.h> #include <linux/reservation.h> static inline int is_dma_buf_file (struct file *);
@@ -52,6 +53,13 @@ static int dma_buf_release(struct inode *inode, struct file *file)BUG_ON (dmabuf->vmapping_counter); +/* +* Any fences that a dma-buf poll can wait on should be signaled +* before releasing dma-buf. This is the responsibility of each +* driver that uses the reservation objects. +*/ +BUG_ON (dmabuf->cb_shared.active || dmabuf->cb_excl.active); + dmabuf->ops->release (dmabuf);mutex_lock (&db_list.lock);
@@ -108,10 +116,99 @@ static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)return base + offset; } +static void dma_buf_poll_cb (struct fence *fence,struct fence_cb *cb) +{ +struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *) cb; +unsigned long flags; + +spin_lock_irqsave (&dcb->poll->lock, flags); +wake_up_locked_poll (dcb->poll, dcb->active); + dcb->active =0 ; +spin_unlock_irqrestore (&dcb->poll->lock, flags); +} + +static unsigned int dma_buf_poll (struct file *file,poll_table *poll) +{ +struct dma_buf *dmabuf; +struct reservation_object *resv; +unsigned long events; + + dmabuf = file->private_data; +if (!dmabuf || !dmabuf->resv) +return POLLERR ; + + resv = dmabuf->resv; + +poll_wait (file, &dmabuf->poll, poll); + + events =poll_requested_events (poll) & (POLLIN |POLLOUT ); +if (!events) +return 0 ; + +ww_mutex_lock (&resv->lock,NULL ); + +if (resv->fence_excl && (!(events &POLLOUT ) || resv->fence_shared_count ==0 )) { +struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl; +unsigned long pevents =POLLIN ; + +if (resv->fence_shared_count ==0 ) + pevents |=POLLOUT ; + +spin_lock_irq (&dmabuf->poll.lock); +if (dcb->active) { + dcb->active |= pevents; + events &= ~pevents; + }else + dcb->active = pevents; +spin_unlock_irq (&dmabuf->poll.lock); + +if (events & pevents) { +if (!fence_add_callback (resv->fence_excl, + &dcb->cb, dma_buf_poll_cb)) + events &= ~pevents; +else +// No callback queued, wake up any additional waiters. +dma_buf_poll_cb (NULL , &dcb->cb); + } + } + +if ((events &POLLOUT ) && resv->fence_shared_count >0 ) { +struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared; +int i; + +/* Only queue a new callback if no event has fired yet */ +spin_lock_irq (&dmabuf->poll.lock); +if (dcb->active) + events &= ~POLLOUT ; +else + dcb->active =POLLOUT ; +spin_unlock_irq (&dmabuf->poll.lock); + +if (!(events &POLLOUT )) + goto out; + +for (i =0 ; i < resv->fence_shared_count; ++i) +if (!fence_add_callback (resv->fence_shared[i], + &dcb->cb, dma_buf_poll_cb)) { + events &= ~POLLOUT ; +break ; + } + +// No callback queued, wake up any additional waiters. +if (i == resv->fence_shared_count) +dma_buf_poll_cb (NULL , &dcb->cb); + } + +out: +ww_mutex_unlock (&resv->lock); +return events; +} +static const struct file_operations dma_buf_fops = { .release = dma_buf_release, .mmap = dma_buf_mmap_internal, .llseek = dma_buf_llseek, + .poll = dma_buf_poll, };/*
@@ -171,6 +268,10 @@ struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops, dmabuf->ops = ops; dmabuf->size = size; dmabuf->exp_name = exp_name; +init_waitqueue_head (&dmabuf->poll); + dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll; + dmabuf->cb_excl.active = dmabuf->cb_shared.active =0 ; +if (!resv) { resv = (struct reservation_object *)&dmabuf[1 ];reservation_object_init (resv);
include/linux/dma-buf.h +12 -0
@@ -30,6 +30,8 @@#include <linux/list.h> #include <linux/dma-mapping.h> #include <linux/fs.h> +#include <linux/fence.h> +#include <linux/wait.h> struct device ;struct dma_buf ;
@@ -130,6 +132,16 @@ struct dma_buf {struct list_head list_node;void *priv;struct reservation_object *resv; + +/* poll support */ +wait_queue_head_t poll; + +struct dma_buf_poll_cb_t { +struct fence_cb cb; +wait_queue_head_t *poll; + +unsigned long active; + } cb_excl, cb_shared; };/**
Re: [PATCH 6/6] dma-buf: add poll support, v2
Quoted 24 lines · Maarten Lankhorst
Thanks to Fengguang Wu for spotting a missing static cast.
v2:
- Kill unused variableneed_shared.Signed-off-by Maarten Lankhorst <[email protected]>--- drivers/base/dma-buf.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 12 ++++++ 2 files changed, 113 insertions(+)drivers/base/dma-buf.c +1
-0@@ -30,6 +30,7 @@#include <linux/export.h> #include <linux/debugfs.h> #include <linux/seq_file.h> +#include <linux/poll.h> #include <linux/reservation.h> static inline int is_dma_buf_file (struct file *);
drivers/base/dma-buf.c static int dma_buf_release(struct inode *inode, struct file *file) · line 59
Quoted 7 lines
drivers/base/dma-buf.c +4
-0@@ -52,6 +53,13 @@ static int dma_buf_release(struct inode *inode, struct file *file)BUG_ON (dmabuf->vmapping_counter); +/* +* Any fences that a dma-buf poll can wait on should be signaled +* before releasing dma-buf. This is the responsibility of each +* driver that uses the reservation objects.
minor nit.. but wouldn't hurt to mention in the comment that if you hit this BUG_ON it is because someone isn't holding a ref to the dmabuf while there is a pending fence
Quoted 52 lines
drivers/base/dma-buf.c +45
-0+*/ +BUG_ON (dmabuf->cb_shared.active || dmabuf->cb_excl.active); + dmabuf->ops->release (dmabuf);mutex_lock (&db_list.lock);@@ -108,10 +116,99 @@ static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)return base + offset; } +static void dma_buf_poll_cb (struct fence *fence,struct fence_cb *cb) +{ +struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *) cb; +unsigned long flags; + +spin_lock_irqsave (&dcb->poll->lock, flags); +wake_up_locked_poll (dcb->poll, dcb->active); + dcb->active =0 ; +spin_unlock_irqrestore (&dcb->poll->lock, flags); +} + +static unsigned int dma_buf_poll (struct file *file,poll_table *poll) +{ +struct dma_buf *dmabuf; +struct reservation_object *resv; +unsigned long events; + + dmabuf = file->private_data; +if (!dmabuf || !dmabuf->resv) +return POLLERR ; + + resv = dmabuf->resv; + +poll_wait (file, &dmabuf->poll, poll); + + events =poll_requested_events (poll) & (POLLIN |POLLOUT ); +if (!events) +return 0 ; + +ww_mutex_lock (&resv->lock,NULL ); + +if (resv->fence_excl && (!(events &POLLOUT ) || resv->fence_shared_count ==0 )) { +struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl; +unsigned long pevents =POLLIN ; + +if (resv->fence_shared_count ==0 ) + pevents |=POLLOUT ; + +spin_lock_irq (&dmabuf->poll.lock); +if (dcb->active) { + dcb->active |= pevents; + events &= ~pevents;
drivers/base/dma-buf.c static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) · line 170
Quoted 10 lines
drivers/base/dma-buf.c +10
-0+ }else + dcb->active = pevents; +spin_unlock_irq (&dmabuf->poll.lock); + +if (events & pevents) { +if (!fence_add_callback (resv->fence_excl, + &dcb->cb, dma_buf_poll_cb)) + events &= ~pevents; +else +// No callback queued, wake up any additional waiters.
couple spots w/ c++ comments, which I assume you didn't mean to leave?
Anyways, other than those minor things,
Quoted 86 lines
drivers/base/dma-buf.c +41
-0+dma_buf_poll_cb (NULL , &dcb->cb); + } + } + +if ((events &POLLOUT ) && resv->fence_shared_count >0 ) { +struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared; +int i; + +/* Only queue a new callback if no event has fired yet */ +spin_lock_irq (&dmabuf->poll.lock); +if (dcb->active) + events &= ~POLLOUT ; +else + dcb->active =POLLOUT ; +spin_unlock_irq (&dmabuf->poll.lock); + +if (!(events &POLLOUT )) + goto out; + +for (i =0 ; i < resv->fence_shared_count; ++i) +if (!fence_add_callback (resv->fence_shared[i], + &dcb->cb, dma_buf_poll_cb)) { + events &= ~POLLOUT ; +break ; + } + +// No callback queued, wake up any additional waiters. +if (i == resv->fence_shared_count) +dma_buf_poll_cb (NULL , &dcb->cb); + } + +out: +ww_mutex_unlock (&resv->lock); +return events; +} +static const struct file_operations dma_buf_fops = { .release = dma_buf_release, .mmap = dma_buf_mmap_internal, .llseek = dma_buf_llseek, + .poll = dma_buf_poll, };/* @@ -171,6 +268,10 @@ struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops, dmabuf->ops = ops; dmabuf->size = size; dmabuf->exp_name = exp_name; +init_waitqueue_head (&dmabuf->poll); + dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll; + dmabuf->cb_excl.active = dmabuf->cb_shared.active =0 ; +if (!resv) { resv = (struct reservation_object *)&dmabuf[1 ];reservation_object_init (resv);include/linux/dma-buf.h +12
-0@@ -30,6 +30,8 @@#include <linux/list.h> #include <linux/dma-mapping.h> #include <linux/fs.h> +#include <linux/fence.h> +#include <linux/wait.h> struct device ;struct dma_buf ;@@ -130,6 +132,16 @@ struct dma_buf {struct list_head list_node;void *priv;struct reservation_object *resv; + +/* poll support */ +wait_queue_head_t poll; + +struct dma_buf_poll_cb_t { +struct fence_cb cb; +wait_queue_head_t *poll; + +unsigned long active; + } cb_excl, cb_shared; };/**