// SPDX-License-Identifier: GPL-2.0-only1/*2* Fence mechanism for dma-buf and to allow for asynchronous dma access3*4* Copyright (C) 2012 Canonical Ltd5* Copyright (C) 2012 Texas Instruments6*7* Authors:8* Rob Clark <[email protected]>9* Maarten Lankhorst <[email protected]>10*/1112#include <linux/slab.h>13#include <linux/export.h>14#include <linux/atomic.h>15#include <linux/dma-fence.h>16#include <linux/sched/signal.h>17#include <linux/seq_file.h>1819#define CREATE_TRACE_POINTS20#include <trace/events/dma_fence.h>2122EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);23EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);24EXPORT_TRACEPOINT_SYMBOL(dma_fence_signaled);2526static DEFINE_SPINLOCK(dma_fence_stub_lock);27static struct dma_fence dma_fence_stub;2829/*30* fence context counter: each execution context should have its own31* fence context, this allows checking if fences belong to the same32* context or not. One device can have multiple separate contexts,33* and they're used if some engine can run independently of another.34*/35static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1);3637/**38* DOC: DMA fences overview39*40* DMA fences, represented by &struct dma_fence, are the kernel internal41* synchronization primitive for DMA operations like GPU rendering, video42* encoding/decoding, or displaying buffers on a screen.43*44* A fence is initialized using dma_fence_init() and completed using45* dma_fence_signal(). Fences are associated with a context, allocated through46* dma_fence_context_alloc(), and all fences on the same context are47* fully ordered.48*49* Since the purposes of fences is to facilitate cross-device and50* cross-application synchronization, there's multiple ways to use one:51*52* - Individual fences can be exposed as a &sync_file, accessed as a file53* descriptor from userspace, created by calling sync_file_create(). This is54* called explicit fencing, since userspace passes around explicit55* synchronization points.56*57* - Some subsystems also have their own explicit fencing primitives, like58* &drm_syncobj. Compared to &sync_file, a &drm_syncobj allows the underlying59* fence to be updated.60*61* - Then there's also implicit fencing, where the synchronization points are62* implicitly passed around as part of shared &dma_buf instances. Such63* implicit fences are stored in &struct dma_resv through the64* &dma_buf.resv pointer.65*/6667/**68* DOC: fence cross-driver contract69*70* Since &dma_fence provide a cross driver contract, all drivers must follow the71* same rules:72*73* * Fences must complete in a reasonable time. Fences which represent kernels74* and shaders submitted by userspace, which could run forever, must be backed75* up by timeout and gpu hang recovery code. Minimally that code must prevent76* further command submission and force complete all in-flight fences, e.g.77* when the driver or hardware do not support gpu reset, or if the gpu reset78* failed for some reason. Ideally the driver supports gpu recovery which only79* affects the offending userspace context, and no other userspace80* submissions.81*82* * Drivers may have different ideas of what completion within a reasonable83* time means. Some hang recovery code uses a fixed timeout, others a mix84* between observing forward progress and increasingly strict timeouts.85* Drivers should not try to second guess timeout handling of fences from86* other drivers.87*88* * To ensure there's no deadlocks of dma_fence_wait() against other locks89* drivers should annotate all code required to reach dma_fence_signal(),90* which completes the fences, with dma_fence_begin_signalling() and91* dma_fence_end_signalling().92*93* * Drivers are allowed to call dma_fence_wait() while holding dma_resv_lock().94* This means any code required for fence completion cannot acquire a95* &dma_resv lock. Note that this also pulls in the entire established96* locking hierarchy around dma_resv_lock() and dma_resv_unlock().97*98* * Drivers are allowed to call dma_fence_wait() from their &shrinker99* callbacks. This means any code required for fence completion cannot100* allocate memory with GFP_KERNEL.101*102* * Drivers are allowed to call dma_fence_wait() from their &mmu_notifier103* respectively &mmu_interval_notifier callbacks. This means any code required104* for fence completion cannot allocate memory with GFP_NOFS or GFP_NOIO.105* Only GFP_ATOMIC is permissible, which might fail.106*107* Note that only GPU drivers have a reasonable excuse for both requiring108* &mmu_interval_notifier and &shrinker callbacks at the same time as having to109* track asynchronous compute work using &dma_fence. No driver outside of110* drivers/gpu should ever call dma_fence_wait() in such contexts.111*/112113static const char *dma_fence_stub_get_name(struct dma_fence *fence)114{115return "stub";116}117118static const struct dma_fence_ops dma_fence_stub_ops = {119.get_driver_name = dma_fence_stub_get_name,120.get_timeline_name = dma_fence_stub_get_name,121};122123static int __init dma_fence_init_stub(void)124{125dma_fence_init(&dma_fence_stub, &dma_fence_stub_ops,126&dma_fence_stub_lock, 0, 0);127128set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,129&dma_fence_stub.flags);130131dma_fence_signal(&dma_fence_stub);132return 0;133}134subsys_initcall(dma_fence_init_stub);135136/**137* dma_fence_get_stub - return a signaled fence138*139* Return a stub fence which is already signaled. The fence's timestamp140* corresponds to the initialisation time of the linux kernel.141*/142struct dma_fence *dma_fence_get_stub(void)143{144return dma_fence_get(&dma_fence_stub);145}146EXPORT_SYMBOL(dma_fence_get_stub);147148/**149* dma_fence_allocate_private_stub - return a private, signaled fence150* @timestamp: timestamp when the fence was signaled151*152* Return a newly allocated and signaled stub fence.153*/154struct dma_fence *dma_fence_allocate_private_stub(ktime_t timestamp)155{156struct dma_fence *fence;157158fence = kzalloc(sizeof(*fence), GFP_KERNEL);159if (fence == NULL)160return NULL;161162dma_fence_init(fence,163&dma_fence_stub_ops,164&dma_fence_stub_lock,1650, 0);166167set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,168&fence->flags);169170dma_fence_signal_timestamp(fence, timestamp);171172return fence;173}174EXPORT_SYMBOL(dma_fence_allocate_private_stub);175176/**177* dma_fence_context_alloc - allocate an array of fence contexts178* @num: amount of contexts to allocate179*180* This function will return the first index of the number of fence contexts181* allocated. The fence context is used for setting &dma_fence.context to a182* unique number by passing the context to dma_fence_init().183*/184u64 dma_fence_context_alloc(unsigned num)185{186WARN_ON(!num);187return atomic64_fetch_add(num, &dma_fence_context_counter);188}189EXPORT_SYMBOL(dma_fence_context_alloc);190191/**192* DOC: fence signalling annotation193*194* Proving correctness of all the kernel code around &dma_fence through code195* review and testing is tricky for a few reasons:196*197* * It is a cross-driver contract, and therefore all drivers must follow the198* same rules for lock nesting order, calling contexts for various functions199* and anything else significant for in-kernel interfaces. But it is also200* impossible to test all drivers in a single machine, hence brute-force N vs.201* N testing of all combinations is impossible. Even just limiting to the202* possible combinations is infeasible.203*204* * There is an enormous amount of driver code involved. For render drivers205* there's the tail of command submission, after fences are published,206* scheduler code, interrupt and workers to process job completion,207* and timeout, gpu reset and gpu hang recovery code. Plus for integration208* with core mm with have &mmu_notifier, respectively &mmu_interval_notifier,209* and &shrinker. For modesetting drivers there's the commit tail functions210* between when fences for an atomic modeset are published, and when the211* corresponding vblank completes, including any interrupt processing and212* related workers. Auditing all that code, across all drivers, is not213* feasible.214*215* * Due to how many other subsystems are involved and the locking hierarchies216* this pulls in there is extremely thin wiggle-room for driver-specific217* differences. &dma_fence interacts with almost all of the core memory218* handling through page fault handlers via &dma_resv, dma_resv_lock() and219* dma_resv_unlock(). On the other side it also interacts through all220* allocation sites through &mmu_notifier and &shrinker.221*222* Furthermore lockdep does not handle cross-release dependencies, which means223* any deadlocks between dma_fence_wait() and dma_fence_signal() can't be caught224* at runtime with some quick testing. The simplest example is one thread225* waiting on a &dma_fence while holding a lock::226*227* lock(A);228* dma_fence_wait(B);229* unlock(A);230*231* while the other thread is stuck trying to acquire the same lock, which232* prevents it from signalling the fence the previous thread is stuck waiting233* on::234*235* lock(A);236* unlock(A);237* dma_fence_signal(B);238*239* By manually annotating all code relevant to signalling a &dma_fence we can240* teach lockdep about these dependencies, which also helps with the validation241* headache since now lockdep can check all the rules for us::242*243* cookie = dma_fence_begin_signalling();244* lock(A);245* unlock(A);246* dma_fence_signal(B);247* dma_fence_end_signalling(cookie);248*249* For using dma_fence_begin_signalling() and dma_fence_end_signalling() to250* annotate critical sections the following rules need to be observed:251*252* * All code necessary to complete a &dma_fence must be annotated, from the253* point where a fence is accessible to other threads, to the point where254* dma_fence_signal() is called. Un-annotated code can contain deadlock issues,255* and due to the very strict rules and many corner cases it is infeasible to256* catch these just with review or normal stress testing.257*258* * &struct dma_resv deserves a special note, since the readers are only259* protected by rcu. This means the signalling critical section starts as soon260* as the new fences are installed, even before dma_resv_unlock() is called.261*262* * The only exception are fast paths and opportunistic signalling code, which263* calls dma_fence_signal() purely as an optimization, but is not required to264* guarantee completion of a &dma_fence. The usual example is a wait IOCTL265* which calls dma_fence_signal(), while the mandatory completion path goes266* through a hardware interrupt and possible job completion worker.267*268* * To aid composability of code, the annotations can be freely nested, as long269* as the overall locking hierarchy is consistent. The annotations also work270* both in interrupt and process context. Due to implementation details this271* requires that callers pass an opaque cookie from272* dma_fence_begin_signalling() to dma_fence_end_signalling().273*274* * Validation against the cross driver contract is implemented by priming275* lockdep with the relevant hierarchy at boot-up. This means even just276* testing with a single device is enough to validate a driver, at least as277* far as deadlocks with dma_fence_wait() against dma_fence_signal() are278* concerned.279*/280#ifdef CONFIG_LOCKDEP281static struct lockdep_map dma_fence_lockdep_map = {282.name = "dma_fence_map"283};284285/**286* dma_fence_begin_signalling - begin a critical DMA fence signalling section287*288* Drivers should use this to annotate the beginning of any code section289* required to eventually complete &dma_fence by calling dma_fence_signal().290*291* The end of these critical sections are annotated with292* dma_fence_end_signalling().293*294* Returns:295*296* Opaque cookie needed by the implementation, which needs to be passed to297* dma_fence_end_signalling().298*/299bool dma_fence_begin_signalling(void)300{301/* explicitly nesting ... */302if (lock_is_held_type(&dma_fence_lockdep_map, 1))303return true;304305/* rely on might_sleep check for soft/hardirq locks */306if (in_atomic())307return true;308309/* ... and non-recursive successful read_trylock */310lock_acquire(&dma_fence_lockdep_map, 0, 1, 1, 1, NULL, _RET_IP_);311312return false;313}314EXPORT_SYMBOL(dma_fence_begin_signalling);315316/**317* dma_fence_end_signalling - end a critical DMA fence signalling section318* @cookie: opaque cookie from dma_fence_begin_signalling()319*320* Closes a critical section annotation opened by dma_fence_begin_signalling().321*/322void dma_fence_end_signalling(bool cookie)323{324if (cookie)325return;326327lock_release(&dma_fence_lockdep_map, _RET_IP_);328}329EXPORT_SYMBOL(dma_fence_end_signalling);330331void __dma_fence_might_wait(void)332{333bool tmp;334335tmp = lock_is_held_type(&dma_fence_lockdep_map, 1);336if (tmp)337lock_release(&dma_fence_lockdep_map, _THIS_IP_);338lock_map_acquire(&dma_fence_lockdep_map);339lock_map_release(&dma_fence_lockdep_map);340if (tmp)341lock_acquire(&dma_fence_lockdep_map, 0, 1, 1, 1, NULL, _THIS_IP_);342}343#endif344345346/**347* dma_fence_signal_timestamp_locked - signal completion of a fence348* @fence: the fence to signal349* @timestamp: fence signal timestamp in kernel's CLOCK_MONOTONIC time domain350*351* Signal completion for software callbacks on a fence, this will unblock352* dma_fence_wait() calls and run all the callbacks added with353* dma_fence_add_callback(). Can be called multiple times, but since a fence354* can only go from the unsignaled to the signaled state and not back, it will355* only be effective the first time. Set the timestamp provided as the fence356* signal timestamp.357*358* Unlike dma_fence_signal_timestamp(), this function must be called with359* &dma_fence.lock held.360*/361void dma_fence_signal_timestamp_locked(struct dma_fence *fence,362ktime_t timestamp)363{364struct dma_fence_cb *cur, *tmp;365struct list_head cb_list;366367lockdep_assert_held(fence->lock);368369if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,370&fence->flags)))371return;372373/* Stash the cb_list before replacing it with the timestamp */374list_replace(&fence->cb_list, &cb_list);375376fence->timestamp = timestamp;377set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);378trace_dma_fence_signaled(fence);379380list_for_each_entry_safe(cur, tmp, &cb_list, node) {381INIT_LIST_HEAD(&cur->node);382cur->func(fence, cur);383}384}385EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);386387/**388* dma_fence_signal_timestamp - signal completion of a fence389* @fence: the fence to signal390* @timestamp: fence signal timestamp in kernel's CLOCK_MONOTONIC time domain391*392* Signal completion for software callbacks on a fence, this will unblock393* dma_fence_wait() calls and run all the callbacks added with394* dma_fence_add_callback(). Can be called multiple times, but since a fence395* can only go from the unsignaled to the signaled state and not back, it will396* only be effective the first time. Set the timestamp provided as the fence397* signal timestamp.398*/399void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)400{401unsigned long flags;402403if (WARN_ON(!fence))404return;405406spin_lock_irqsave(fence->lock, flags);407dma_fence_signal_timestamp_locked(fence, timestamp);408spin_unlock_irqrestore(fence->lock, flags);409}410EXPORT_SYMBOL(dma_fence_signal_timestamp);411412/**413* dma_fence_signal_locked - signal completion of a fence414* @fence: the fence to signal415*416* Signal completion for software callbacks on a fence, this will unblock417* dma_fence_wait() calls and run all the callbacks added with418* dma_fence_add_callback(). Can be called multiple times, but since a fence419* can only go from the unsignaled to the signaled state and not back, it will420* only be effective the first time.421*422* Unlike dma_fence_signal(), this function must be called with &dma_fence.lock423* held.424*/425void dma_fence_signal_locked(struct dma_fence *fence)426{427dma_fence_signal_timestamp_locked(fence, ktime_get());428}429EXPORT_SYMBOL(dma_fence_signal_locked);430431/**432* dma_fence_check_and_signal_locked - signal the fence if it's not yet signaled433* @fence: the fence to check and signal434*435* Checks whether a fence was signaled and signals it if it was not yet signaled.436*437* Unlike dma_fence_check_and_signal(), this function must be called with438* &struct dma_fence.lock being held.439*440* Return: true if fence has been signaled already, false otherwise.441*/442bool dma_fence_check_and_signal_locked(struct dma_fence *fence)443{444bool ret;445446ret = dma_fence_test_signaled_flag(fence);447dma_fence_signal_locked(fence);448449return ret;450}451EXPORT_SYMBOL(dma_fence_check_and_signal_locked);452453/**454* dma_fence_check_and_signal - signal the fence if it's not yet signaled455* @fence: the fence to check and signal456*457* Checks whether a fence was signaled and signals it if it was not yet signaled.458* All this is done in a race-free manner.459*460* Return: true if fence has been signaled already, false otherwise.461*/462bool dma_fence_check_and_signal(struct dma_fence *fence)463{464unsigned long flags;465bool ret;466467spin_lock_irqsave(fence->lock, flags);468ret = dma_fence_check_and_signal_locked(fence);469spin_unlock_irqrestore(fence->lock, flags);470471return ret;472}473EXPORT_SYMBOL(dma_fence_check_and_signal);474475/**476* dma_fence_signal - signal completion of a fence477* @fence: the fence to signal478*479* Signal completion for software callbacks on a fence, this will unblock480* dma_fence_wait() calls and run all the callbacks added with481* dma_fence_add_callback(). Can be called multiple times, but since a fence482* can only go from the unsignaled to the signaled state and not back, it will483* only be effective the first time.484*/485void dma_fence_signal(struct dma_fence *fence)486{487unsigned long flags;488bool tmp;489490if (WARN_ON(!fence))491return;492493tmp = dma_fence_begin_signalling();494495spin_lock_irqsave(fence->lock, flags);496dma_fence_signal_timestamp_locked(fence, ktime_get());497spin_unlock_irqrestore(fence->lock, flags);498499dma_fence_end_signalling(tmp);500}501EXPORT_SYMBOL(dma_fence_signal);502503/**504* dma_fence_wait_timeout - sleep until the fence gets signaled505* or until timeout elapses506* @fence: the fence to wait on507* @intr: if true, do an interruptible wait508* @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT509*510* Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the511* remaining timeout in jiffies on success. Other error values may be512* returned on custom implementations.513*514* Performs a synchronous wait on this fence. It is assumed the caller515* directly or indirectly (buf-mgr between reservation and committing)516* holds a reference to the fence, otherwise the fence might be517* freed before return, resulting in undefined behavior.518*519* See also dma_fence_wait() and dma_fence_wait_any_timeout().520*/521signed long522dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)523{524signed long ret;525526if (WARN_ON(timeout < 0))527return -EINVAL;528529might_sleep();530531__dma_fence_might_wait();532533dma_fence_enable_sw_signaling(fence);534535if (trace_dma_fence_wait_start_enabled()) {536rcu_read_lock();537trace_dma_fence_wait_start(fence);538rcu_read_unlock();539}540if (fence->ops->wait)541ret = fence->ops->wait(fence, intr, timeout);542else543ret = dma_fence_default_wait(fence, intr, timeout);544if (trace_dma_fence_wait_end_enabled()) {545rcu_read_lock();546trace_dma_fence_wait_end(fence);547rcu_read_unlock();548}549return ret;550}551EXPORT_SYMBOL(dma_fence_wait_timeout);552553/**554* dma_fence_release - default release function for fences555* @kref: &dma_fence.recfount556*557* This is the default release functions for &dma_fence. Drivers shouldn't call558* this directly, but instead call dma_fence_put().559*/560void dma_fence_release(struct kref *kref)561{562struct dma_fence *fence =563container_of(kref, struct dma_fence, refcount);564565rcu_read_lock();566trace_dma_fence_destroy(fence);567568if (!list_empty(&fence->cb_list) &&569!dma_fence_test_signaled_flag(fence)) {570const char __rcu *timeline;571const char __rcu *driver;572unsigned long flags;573574driver = dma_fence_driver_name(fence);575timeline = dma_fence_timeline_name(fence);576577WARN(1,578"Fence %s:%s:%llx:%llx released with pending signals!\n",579rcu_dereference(driver), rcu_dereference(timeline),580fence->context, fence->seqno);581582/*583* Failed to signal before release, likely a refcounting issue.584*585* This should never happen, but if it does make sure that we586* don't leave chains dangling. We set the error flag first587* so that the callbacks know this signal is due to an error.588*/589spin_lock_irqsave(fence->lock, flags);590fence->error = -EDEADLK;591dma_fence_signal_locked(fence);592spin_unlock_irqrestore(fence->lock, flags);593}594595rcu_read_unlock();596597if (fence->ops->release)598fence->ops->release(fence);599else600dma_fence_free(fence);601}602EXPORT_SYMBOL(dma_fence_release);603604/**605* dma_fence_free - default release function for &dma_fence.606* @fence: fence to release607*608* This is the default implementation for &dma_fence_ops.release. It calls609* kfree_rcu() on @fence.610*/611void dma_fence_free(struct dma_fence *fence)612{613kfree_rcu(fence, rcu);614}615EXPORT_SYMBOL(dma_fence_free);616617static bool __dma_fence_enable_signaling(struct dma_fence *fence)618{619bool was_set;620621lockdep_assert_held(fence->lock);622623was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,624&fence->flags);625626if (dma_fence_test_signaled_flag(fence))627return false;628629if (!was_set && fence->ops->enable_signaling) {630trace_dma_fence_enable_signal(fence);631632if (!fence->ops->enable_signaling(fence)) {633dma_fence_signal_locked(fence);634return false;635}636}637638return true;639}640641/**642* dma_fence_enable_sw_signaling - enable signaling on fence643* @fence: the fence to enable644*645* This will request for sw signaling to be enabled, to make the fence646* complete as soon as possible. This calls &dma_fence_ops.enable_signaling647* internally.648*/649void dma_fence_enable_sw_signaling(struct dma_fence *fence)650{651unsigned long flags;652653spin_lock_irqsave(fence->lock, flags);654__dma_fence_enable_signaling(fence);655spin_unlock_irqrestore(fence->lock, flags);656}657EXPORT_SYMBOL(dma_fence_enable_sw_signaling);658659/**660* dma_fence_add_callback - add a callback to be called when the fence661* is signaled662* @fence: the fence to wait on663* @cb: the callback to register664* @func: the function to call665*666* Add a software callback to the fence. The caller should keep a reference to667* the fence.668*669* @cb will be initialized by dma_fence_add_callback(), no initialization670* by the caller is required. Any number of callbacks can be registered671* to a fence, but a callback can only be registered to one fence at a time.672*673* If fence is already signaled, this function will return -ENOENT (and674* *not* call the callback).675*676* Note that the callback can be called from an atomic context or irq context.677*678* Returns 0 in case of success, -ENOENT if the fence is already signaled679* and -EINVAL in case of error.680*/681int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,682dma_fence_func_t func)683{684unsigned long flags;685int ret = 0;686687if (WARN_ON(!fence || !func))688return -EINVAL;689690if (dma_fence_test_signaled_flag(fence)) {691INIT_LIST_HEAD(&cb->node);692return -ENOENT;693}694695spin_lock_irqsave(fence->lock, flags);696697if (__dma_fence_enable_signaling(fence)) {698cb->func = func;699list_add_tail(&cb->node, &fence->cb_list);700} else {701INIT_LIST_HEAD(&cb->node);702ret = -ENOENT;703}704705spin_unlock_irqrestore(fence->lock, flags);706707return ret;708}709EXPORT_SYMBOL(dma_fence_add_callback);710711/**712* dma_fence_get_status - returns the status upon completion713* @fence: the dma_fence to query714*715* This wraps dma_fence_get_status_locked() to return the error status716* condition on a signaled fence. See dma_fence_get_status_locked() for more717* details.718*719* Returns 0 if the fence has not yet been signaled, 1 if the fence has720* been signaled without an error condition, or a negative error code721* if the fence has been completed in err.722*/723int dma_fence_get_status(struct dma_fence *fence)724{725unsigned long flags;726int status;727728spin_lock_irqsave(fence->lock, flags);729status = dma_fence_get_status_locked(fence);730spin_unlock_irqrestore(fence->lock, flags);731732return status;733}734EXPORT_SYMBOL(dma_fence_get_status);735736/**737* dma_fence_remove_callback - remove a callback from the signaling list738* @fence: the fence to wait on739* @cb: the callback to remove740*741* Remove a previously queued callback from the fence. This function returns742* true if the callback is successfully removed, or false if the fence has743* already been signaled.744*745* *WARNING*:746* Cancelling a callback should only be done if you really know what you're747* doing, since deadlocks and race conditions could occur all too easily. For748* this reason, it should only ever be done on hardware lockup recovery,749* with a reference held to the fence.750*751* Behaviour is undefined if @cb has not been added to @fence using752* dma_fence_add_callback() beforehand.753*/754bool755dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)756{757unsigned long flags;758bool ret;759760spin_lock_irqsave(fence->lock, flags);761762ret = !list_empty(&cb->node);763if (ret)764list_del_init(&cb->node);765766spin_unlock_irqrestore(fence->lock, flags);767768return ret;769}770EXPORT_SYMBOL(dma_fence_remove_callback);771772struct default_wait_cb {773struct dma_fence_cb base;774struct task_struct *task;775};776777static void778dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)779{780struct default_wait_cb *wait =781container_of(cb, struct default_wait_cb, base);782783wake_up_state(wait->task, TASK_NORMAL);784}785786/**787* dma_fence_default_wait - default sleep until the fence gets signaled788* or until timeout elapses789* @fence: the fence to wait on790* @intr: if true, do an interruptible wait791* @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT792*793* Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the794* remaining timeout in jiffies on success. If timeout is zero the value one is795* returned if the fence is already signaled for consistency with other796* functions taking a jiffies timeout.797*/798signed long799dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)800{801struct default_wait_cb cb;802unsigned long flags;803signed long ret = timeout ? timeout : 1;804805spin_lock_irqsave(fence->lock, flags);806807if (dma_fence_test_signaled_flag(fence))808goto out;809810if (intr && signal_pending(current)) {811ret = -ERESTARTSYS;812goto out;813}814815if (!timeout) {816ret = 0;817goto out;818}819820cb.base.func = dma_fence_default_wait_cb;821cb.task = current;822list_add(&cb.base.node, &fence->cb_list);823824while (!dma_fence_test_signaled_flag(fence) && ret > 0) {825if (intr)826__set_current_state(TASK_INTERRUPTIBLE);827else828__set_current_state(TASK_UNINTERRUPTIBLE);829spin_unlock_irqrestore(fence->lock, flags);830831ret = schedule_timeout(ret);832833spin_lock_irqsave(fence->lock, flags);834if (ret > 0 && intr && signal_pending(current))835ret = -ERESTARTSYS;836}837838if (!list_empty(&cb.base.node))839list_del(&cb.base.node);840__set_current_state(TASK_RUNNING);841842out:843spin_unlock_irqrestore(fence->lock, flags);844return ret;845}846EXPORT_SYMBOL(dma_fence_default_wait);847848static bool849dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,850uint32_t *idx)851{852int i;853854for (i = 0; i < count; ++i) {855struct dma_fence *fence = fences[i];856if (dma_fence_test_signaled_flag(fence)) {857if (idx)858*idx = i;859return true;860}861}862return false;863}864865/**866* dma_fence_wait_any_timeout - sleep until any fence gets signaled867* or until timeout elapses868* @fences: array of fences to wait on869* @count: number of fences to wait on870* @intr: if true, do an interruptible wait871* @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT872* @idx: used to store the first signaled fence index, meaningful only on873* positive return874*875* Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if876* interrupted, 0 if the wait timed out, or the remaining timeout in jiffies877* on success.878*879* Synchronous waits for the first fence in the array to be signaled. The880* caller needs to hold a reference to all fences in the array, otherwise a881* fence might be freed before return, resulting in undefined behavior.882*883* See also dma_fence_wait() and dma_fence_wait_timeout().884*/885signed long886dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,887bool intr, signed long timeout, uint32_t *idx)888{889struct default_wait_cb *cb;890signed long ret = timeout;891unsigned i;892893if (WARN_ON(!fences || !count || timeout < 0))894return -EINVAL;895896if (timeout == 0) {897for (i = 0; i < count; ++i)898if (dma_fence_is_signaled(fences[i])) {899if (idx)900*idx = i;901return 1;902}903904return 0;905}906907cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);908if (cb == NULL) {909ret = -ENOMEM;910goto err_free_cb;911}912913for (i = 0; i < count; ++i) {914struct dma_fence *fence = fences[i];915916cb[i].task = current;917if (dma_fence_add_callback(fence, &cb[i].base,918dma_fence_default_wait_cb)) {919/* This fence is already signaled */920if (idx)921*idx = i;922goto fence_rm_cb;923}924}925926while (ret > 0) {927if (intr)928set_current_state(TASK_INTERRUPTIBLE);929else930set_current_state(TASK_UNINTERRUPTIBLE);931932if (dma_fence_test_signaled_any(fences, count, idx))933break;934935ret = schedule_timeout(ret);936937if (ret > 0 && intr && signal_pending(current))938ret = -ERESTARTSYS;939}940941__set_current_state(TASK_RUNNING);942943fence_rm_cb:944while (i-- > 0)945dma_fence_remove_callback(fences[i], &cb[i].base);946947err_free_cb:948kfree(cb);949950return ret;951}952EXPORT_SYMBOL(dma_fence_wait_any_timeout);953954/**955* DOC: deadline hints956*957* In an ideal world, it would be possible to pipeline a workload sufficiently958* that a utilization based device frequency governor could arrive at a minimum959* frequency that meets the requirements of the use-case, in order to minimize960* power consumption. But in the real world there are many workloads which961* defy this ideal. For example, but not limited to:962*963* * Workloads that ping-pong between device and CPU, with alternating periods964* of CPU waiting for device, and device waiting on CPU. This can result in965* devfreq and cpufreq seeing idle time in their respective domains and in966* result reduce frequency.967*968* * Workloads that interact with a periodic time based deadline, such as double969* buffered GPU rendering vs vblank sync'd page flipping. In this scenario,970* missing a vblank deadline results in an *increase* in idle time on the GPU971* (since it has to wait an additional vblank period), sending a signal to972* the GPU's devfreq to reduce frequency, when in fact the opposite is what is973* needed.974*975* To this end, deadline hint(s) can be set on a &dma_fence via &dma_fence_set_deadline976* (or indirectly via userspace facing ioctls like &sync_set_deadline).977* The deadline hint provides a way for the waiting driver, or userspace, to978* convey an appropriate sense of urgency to the signaling driver.979*980* A deadline hint is given in absolute ktime (CLOCK_MONOTONIC for userspace981* facing APIs). The time could either be some point in the future (such as982* the vblank based deadline for page-flipping, or the start of a compositor's983* composition cycle), or the current time to indicate an immediate deadline984* hint (Ie. forward progress cannot be made until this fence is signaled).985*986* Multiple deadlines may be set on a given fence, even in parallel. See the987* documentation for &dma_fence_ops.set_deadline.988*989* The deadline hint is just that, a hint. The driver that created the fence990* may react by increasing frequency, making different scheduling choices, etc.991* Or doing nothing at all.992*/993994/**995* dma_fence_set_deadline - set desired fence-wait deadline hint996* @fence: the fence that is to be waited on997* @deadline: the time by which the waiter hopes for the fence to be998* signaled999*1000* Give the fence signaler a hint about an upcoming deadline, such as1001* vblank, by which point the waiter would prefer the fence to be1002* signaled by. This is intended to give feedback to the fence signaler1003* to aid in power management decisions, such as boosting GPU frequency1004* if a periodic vblank deadline is approaching but the fence is not1005* yet signaled..1006*/1007void dma_fence_set_deadline(struct dma_fence *fence, ktime_t deadline)1008{1009if (fence->ops->set_deadline && !dma_fence_is_signaled(fence))1010fence->ops->set_deadline(fence, deadline);1011}1012EXPORT_SYMBOL(dma_fence_set_deadline);10131014/**1015* dma_fence_describe - Dump fence description into seq_file1016* @fence: the fence to describe1017* @seq: the seq_file to put the textual description into1018*1019* Dump a textual description of the fence and it's state into the seq_file.1020*/1021void dma_fence_describe(struct dma_fence *fence, struct seq_file *seq)1022{1023const char __rcu *timeline = "";1024const char __rcu *driver = "";1025const char *signaled = "";10261027rcu_read_lock();10281029if (!dma_fence_is_signaled(fence)) {1030timeline = dma_fence_timeline_name(fence);1031driver = dma_fence_driver_name(fence);1032signaled = "un";1033}10341035seq_printf(seq, "%llu:%llu %s %s %ssignalled\n",1036fence->context, fence->seqno, timeline, driver,1037signaled);10381039rcu_read_unlock();1040}1041EXPORT_SYMBOL(dma_fence_describe);10421043static void1044__dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,1045spinlock_t *lock, u64 context, u64 seqno, unsigned long flags)1046{1047BUG_ON(!lock);1048BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name);10491050kref_init(&fence->refcount);1051fence->ops = ops;1052INIT_LIST_HEAD(&fence->cb_list);1053fence->lock = lock;1054fence->context = context;1055fence->seqno = seqno;1056fence->flags = flags;1057fence->error = 0;10581059trace_dma_fence_init(fence);1060}10611062/**1063* dma_fence_init - Initialize a custom fence.1064* @fence: the fence to initialize1065* @ops: the dma_fence_ops for operations on this fence1066* @lock: the irqsafe spinlock to use for locking this fence1067* @context: the execution context this fence is run on1068* @seqno: a linear increasing sequence number for this context1069*1070* Initializes an allocated fence, the caller doesn't have to keep its1071* refcount after committing with this fence, but it will need to hold a1072* refcount again if &dma_fence_ops.enable_signaling gets called.1073*1074* context and seqno are used for easy comparison between fences, allowing1075* to check which fence is later by simply using dma_fence_later().1076*/1077void1078dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,1079spinlock_t *lock, u64 context, u64 seqno)1080{1081__dma_fence_init(fence, ops, lock, context, seqno, 0UL);1082}1083EXPORT_SYMBOL(dma_fence_init);10841085/**1086* dma_fence_init64 - Initialize a custom fence with 64-bit seqno support.1087* @fence: the fence to initialize1088* @ops: the dma_fence_ops for operations on this fence1089* @lock: the irqsafe spinlock to use for locking this fence1090* @context: the execution context this fence is run on1091* @seqno: a linear increasing sequence number for this context1092*1093* Initializes an allocated fence, the caller doesn't have to keep its1094* refcount after committing with this fence, but it will need to hold a1095* refcount again if &dma_fence_ops.enable_signaling gets called.1096*1097* Context and seqno are used for easy comparison between fences, allowing1098* to check which fence is later by simply using dma_fence_later().1099*/1100void1101dma_fence_init64(struct dma_fence *fence, const struct dma_fence_ops *ops,1102spinlock_t *lock, u64 context, u64 seqno)1103{1104__dma_fence_init(fence, ops, lock, context, seqno,1105BIT(DMA_FENCE_FLAG_SEQNO64_BIT));1106}1107EXPORT_SYMBOL(dma_fence_init64);11081109/**1110* dma_fence_driver_name - Access the driver name1111* @fence: the fence to query1112*1113* Returns a driver name backing the dma-fence implementation.1114*1115* IMPORTANT CONSIDERATION:1116* Dma-fence contract stipulates that access to driver provided data (data not1117* directly embedded into the object itself), such as the &dma_fence.lock and1118* memory potentially accessed by the &dma_fence.ops functions, is forbidden1119* after the fence has been signalled. Drivers are allowed to free that data,1120* and some do.1121*1122* To allow safe access drivers are mandated to guarantee a RCU grace period1123* between signalling the fence and freeing said data.1124*1125* As such access to the driver name is only valid inside a RCU locked section.1126* The pointer MUST be both queried and USED ONLY WITHIN a SINGLE block guarded1127* by the &rcu_read_lock and &rcu_read_unlock pair.1128*/1129const char __rcu *dma_fence_driver_name(struct dma_fence *fence)1130{1131RCU_LOCKDEP_WARN(!rcu_read_lock_held(),1132"RCU protection is required for safe access to returned string");11331134if (!dma_fence_test_signaled_flag(fence))1135return fence->ops->get_driver_name(fence);1136else1137return "detached-driver";1138}1139EXPORT_SYMBOL(dma_fence_driver_name);11401141/**1142* dma_fence_timeline_name - Access the timeline name1143* @fence: the fence to query1144*1145* Returns a timeline name provided by the dma-fence implementation.1146*1147* IMPORTANT CONSIDERATION:1148* Dma-fence contract stipulates that access to driver provided data (data not1149* directly embedded into the object itself), such as the &dma_fence.lock and1150* memory potentially accessed by the &dma_fence.ops functions, is forbidden1151* after the fence has been signalled. Drivers are allowed to free that data,1152* and some do.1153*1154* To allow safe access drivers are mandated to guarantee a RCU grace period1155* between signalling the fence and freeing said data.1156*1157* As such access to the driver name is only valid inside a RCU locked section.1158* The pointer MUST be both queried and USED ONLY WITHIN a SINGLE block guarded1159* by the &rcu_read_lock and &rcu_read_unlock pair.1160*/1161const char __rcu *dma_fence_timeline_name(struct dma_fence *fence)1162{1163RCU_LOCKDEP_WARN(!rcu_read_lock_held(),1164"RCU protection is required for safe access to returned string");11651166if (!dma_fence_test_signaled_flag(fence))1167return fence->ops->get_timeline_name(fence);1168else1169return "signaled-timeline";1170}1171EXPORT_SYMBOL(dma_fence_timeline_name);117211731174