18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Fence mechanism for dma-buf and to allow for asynchronous dma access 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Canonical Ltd 68c2ecf20Sopenharmony_ci * Copyright (C) 2012 Texas Instruments 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Authors: 98c2ecf20Sopenharmony_ci * Rob Clark <robdclark@gmail.com> 108c2ecf20Sopenharmony_ci * Maarten Lankhorst <maarten.lankhorst@canonical.com> 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/export.h> 158c2ecf20Sopenharmony_ci#include <linux/atomic.h> 168c2ecf20Sopenharmony_ci#include <linux/dma-fence.h> 178c2ecf20Sopenharmony_ci#include <linux/sched/signal.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define CREATE_TRACE_POINTS 208c2ecf20Sopenharmony_ci#include <trace/events/dma_fence.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ciEXPORT_TRACEPOINT_SYMBOL(dma_fence_emit); 238c2ecf20Sopenharmony_ciEXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal); 248c2ecf20Sopenharmony_ciEXPORT_TRACEPOINT_SYMBOL(dma_fence_signaled); 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(dma_fence_stub_lock); 278c2ecf20Sopenharmony_cistatic struct dma_fence dma_fence_stub; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* 308c2ecf20Sopenharmony_ci * fence context counter: each execution context should have its own 318c2ecf20Sopenharmony_ci * fence context, this allows checking if fences belong to the same 328c2ecf20Sopenharmony_ci * context or not. One device can have multiple separate contexts, 338c2ecf20Sopenharmony_ci * and they're used if some engine can run independently of another. 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_cistatic atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/** 388c2ecf20Sopenharmony_ci * DOC: DMA fences overview 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * DMA fences, represented by &struct dma_fence, are the kernel internal 418c2ecf20Sopenharmony_ci * synchronization primitive for DMA operations like GPU rendering, video 428c2ecf20Sopenharmony_ci * encoding/decoding, or displaying buffers on a screen. 438c2ecf20Sopenharmony_ci * 448c2ecf20Sopenharmony_ci * A fence is initialized using dma_fence_init() and completed using 458c2ecf20Sopenharmony_ci * dma_fence_signal(). Fences are associated with a context, allocated through 468c2ecf20Sopenharmony_ci * dma_fence_context_alloc(), and all fences on the same context are 478c2ecf20Sopenharmony_ci * fully ordered. 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * Since the purposes of fences is to facilitate cross-device and 508c2ecf20Sopenharmony_ci * cross-application synchronization, there's multiple ways to use one: 518c2ecf20Sopenharmony_ci * 528c2ecf20Sopenharmony_ci * - Individual fences can be exposed as a &sync_file, accessed as a file 538c2ecf20Sopenharmony_ci * descriptor from userspace, created by calling sync_file_create(). This is 548c2ecf20Sopenharmony_ci * called explicit fencing, since userspace passes around explicit 558c2ecf20Sopenharmony_ci * synchronization points. 568c2ecf20Sopenharmony_ci * 578c2ecf20Sopenharmony_ci * - Some subsystems also have their own explicit fencing primitives, like 588c2ecf20Sopenharmony_ci * &drm_syncobj. Compared to &sync_file, a &drm_syncobj allows the underlying 598c2ecf20Sopenharmony_ci * fence to be updated. 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * - Then there's also implicit fencing, where the synchronization points are 628c2ecf20Sopenharmony_ci * implicitly passed around as part of shared &dma_buf instances. Such 638c2ecf20Sopenharmony_ci * implicit fences are stored in &struct dma_resv through the 648c2ecf20Sopenharmony_ci * &dma_buf.resv pointer. 658c2ecf20Sopenharmony_ci */ 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/** 688c2ecf20Sopenharmony_ci * DOC: fence cross-driver contract 698c2ecf20Sopenharmony_ci * 708c2ecf20Sopenharmony_ci * Since &dma_fence provide a cross driver contract, all drivers must follow the 718c2ecf20Sopenharmony_ci * same rules: 728c2ecf20Sopenharmony_ci * 738c2ecf20Sopenharmony_ci * * Fences must complete in a reasonable time. Fences which represent kernels 748c2ecf20Sopenharmony_ci * and shaders submitted by userspace, which could run forever, must be backed 758c2ecf20Sopenharmony_ci * up by timeout and gpu hang recovery code. Minimally that code must prevent 768c2ecf20Sopenharmony_ci * further command submission and force complete all in-flight fences, e.g. 778c2ecf20Sopenharmony_ci * when the driver or hardware do not support gpu reset, or if the gpu reset 788c2ecf20Sopenharmony_ci * failed for some reason. Ideally the driver supports gpu recovery which only 798c2ecf20Sopenharmony_ci * affects the offending userspace context, and no other userspace 808c2ecf20Sopenharmony_ci * submissions. 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci * * Drivers may have different ideas of what completion within a reasonable 838c2ecf20Sopenharmony_ci * time means. Some hang recovery code uses a fixed timeout, others a mix 848c2ecf20Sopenharmony_ci * between observing forward progress and increasingly strict timeouts. 858c2ecf20Sopenharmony_ci * Drivers should not try to second guess timeout handling of fences from 868c2ecf20Sopenharmony_ci * other drivers. 878c2ecf20Sopenharmony_ci * 888c2ecf20Sopenharmony_ci * * To ensure there's no deadlocks of dma_fence_wait() against other locks 898c2ecf20Sopenharmony_ci * drivers should annotate all code required to reach dma_fence_signal(), 908c2ecf20Sopenharmony_ci * which completes the fences, with dma_fence_begin_signalling() and 918c2ecf20Sopenharmony_ci * dma_fence_end_signalling(). 928c2ecf20Sopenharmony_ci * 938c2ecf20Sopenharmony_ci * * Drivers are allowed to call dma_fence_wait() while holding dma_resv_lock(). 948c2ecf20Sopenharmony_ci * This means any code required for fence completion cannot acquire a 958c2ecf20Sopenharmony_ci * &dma_resv lock. Note that this also pulls in the entire established 968c2ecf20Sopenharmony_ci * locking hierarchy around dma_resv_lock() and dma_resv_unlock(). 978c2ecf20Sopenharmony_ci * 988c2ecf20Sopenharmony_ci * * Drivers are allowed to call dma_fence_wait() from their &shrinker 998c2ecf20Sopenharmony_ci * callbacks. This means any code required for fence completion cannot 1008c2ecf20Sopenharmony_ci * allocate memory with GFP_KERNEL. 1018c2ecf20Sopenharmony_ci * 1028c2ecf20Sopenharmony_ci * * Drivers are allowed to call dma_fence_wait() from their &mmu_notifier 1038c2ecf20Sopenharmony_ci * respectively &mmu_interval_notifier callbacks. This means any code required 1048c2ecf20Sopenharmony_ci * for fence completeion cannot allocate memory with GFP_NOFS or GFP_NOIO. 1058c2ecf20Sopenharmony_ci * Only GFP_ATOMIC is permissible, which might fail. 1068c2ecf20Sopenharmony_ci * 1078c2ecf20Sopenharmony_ci * Note that only GPU drivers have a reasonable excuse for both requiring 1088c2ecf20Sopenharmony_ci * &mmu_interval_notifier and &shrinker callbacks at the same time as having to 1098c2ecf20Sopenharmony_ci * track asynchronous compute work using &dma_fence. No driver outside of 1108c2ecf20Sopenharmony_ci * drivers/gpu should ever call dma_fence_wait() in such contexts. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic const char *dma_fence_stub_get_name(struct dma_fence *fence) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci return "stub"; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic const struct dma_fence_ops dma_fence_stub_ops = { 1198c2ecf20Sopenharmony_ci .get_driver_name = dma_fence_stub_get_name, 1208c2ecf20Sopenharmony_ci .get_timeline_name = dma_fence_stub_get_name, 1218c2ecf20Sopenharmony_ci}; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci/** 1248c2ecf20Sopenharmony_ci * dma_fence_get_stub - return a signaled fence 1258c2ecf20Sopenharmony_ci * 1268c2ecf20Sopenharmony_ci * Return a stub fence which is already signaled. 1278c2ecf20Sopenharmony_ci */ 1288c2ecf20Sopenharmony_cistruct dma_fence *dma_fence_get_stub(void) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci spin_lock(&dma_fence_stub_lock); 1318c2ecf20Sopenharmony_ci if (!dma_fence_stub.ops) { 1328c2ecf20Sopenharmony_ci dma_fence_init(&dma_fence_stub, 1338c2ecf20Sopenharmony_ci &dma_fence_stub_ops, 1348c2ecf20Sopenharmony_ci &dma_fence_stub_lock, 1358c2ecf20Sopenharmony_ci 0, 0); 1368c2ecf20Sopenharmony_ci dma_fence_signal_locked(&dma_fence_stub); 1378c2ecf20Sopenharmony_ci } 1388c2ecf20Sopenharmony_ci spin_unlock(&dma_fence_stub_lock); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return dma_fence_get(&dma_fence_stub); 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_get_stub); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci/** 1458c2ecf20Sopenharmony_ci * dma_fence_context_alloc - allocate an array of fence contexts 1468c2ecf20Sopenharmony_ci * @num: amount of contexts to allocate 1478c2ecf20Sopenharmony_ci * 1488c2ecf20Sopenharmony_ci * This function will return the first index of the number of fence contexts 1498c2ecf20Sopenharmony_ci * allocated. The fence context is used for setting &dma_fence.context to a 1508c2ecf20Sopenharmony_ci * unique number by passing the context to dma_fence_init(). 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_ciu64 dma_fence_context_alloc(unsigned num) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci WARN_ON(!num); 1558c2ecf20Sopenharmony_ci return atomic64_fetch_add(num, &dma_fence_context_counter); 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_context_alloc); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci/** 1608c2ecf20Sopenharmony_ci * DOC: fence signalling annotation 1618c2ecf20Sopenharmony_ci * 1628c2ecf20Sopenharmony_ci * Proving correctness of all the kernel code around &dma_fence through code 1638c2ecf20Sopenharmony_ci * review and testing is tricky for a few reasons: 1648c2ecf20Sopenharmony_ci * 1658c2ecf20Sopenharmony_ci * * It is a cross-driver contract, and therefore all drivers must follow the 1668c2ecf20Sopenharmony_ci * same rules for lock nesting order, calling contexts for various functions 1678c2ecf20Sopenharmony_ci * and anything else significant for in-kernel interfaces. But it is also 1688c2ecf20Sopenharmony_ci * impossible to test all drivers in a single machine, hence brute-force N vs. 1698c2ecf20Sopenharmony_ci * N testing of all combinations is impossible. Even just limiting to the 1708c2ecf20Sopenharmony_ci * possible combinations is infeasible. 1718c2ecf20Sopenharmony_ci * 1728c2ecf20Sopenharmony_ci * * There is an enormous amount of driver code involved. For render drivers 1738c2ecf20Sopenharmony_ci * there's the tail of command submission, after fences are published, 1748c2ecf20Sopenharmony_ci * scheduler code, interrupt and workers to process job completion, 1758c2ecf20Sopenharmony_ci * and timeout, gpu reset and gpu hang recovery code. Plus for integration 1768c2ecf20Sopenharmony_ci * with core mm with have &mmu_notifier, respectively &mmu_interval_notifier, 1778c2ecf20Sopenharmony_ci * and &shrinker. For modesetting drivers there's the commit tail functions 1788c2ecf20Sopenharmony_ci * between when fences for an atomic modeset are published, and when the 1798c2ecf20Sopenharmony_ci * corresponding vblank completes, including any interrupt processing and 1808c2ecf20Sopenharmony_ci * related workers. Auditing all that code, across all drivers, is not 1818c2ecf20Sopenharmony_ci * feasible. 1828c2ecf20Sopenharmony_ci * 1838c2ecf20Sopenharmony_ci * * Due to how many other subsystems are involved and the locking hierarchies 1848c2ecf20Sopenharmony_ci * this pulls in there is extremely thin wiggle-room for driver-specific 1858c2ecf20Sopenharmony_ci * differences. &dma_fence interacts with almost all of the core memory 1868c2ecf20Sopenharmony_ci * handling through page fault handlers via &dma_resv, dma_resv_lock() and 1878c2ecf20Sopenharmony_ci * dma_resv_unlock(). On the other side it also interacts through all 1888c2ecf20Sopenharmony_ci * allocation sites through &mmu_notifier and &shrinker. 1898c2ecf20Sopenharmony_ci * 1908c2ecf20Sopenharmony_ci * Furthermore lockdep does not handle cross-release dependencies, which means 1918c2ecf20Sopenharmony_ci * any deadlocks between dma_fence_wait() and dma_fence_signal() can't be caught 1928c2ecf20Sopenharmony_ci * at runtime with some quick testing. The simplest example is one thread 1938c2ecf20Sopenharmony_ci * waiting on a &dma_fence while holding a lock:: 1948c2ecf20Sopenharmony_ci * 1958c2ecf20Sopenharmony_ci * lock(A); 1968c2ecf20Sopenharmony_ci * dma_fence_wait(B); 1978c2ecf20Sopenharmony_ci * unlock(A); 1988c2ecf20Sopenharmony_ci * 1998c2ecf20Sopenharmony_ci * while the other thread is stuck trying to acquire the same lock, which 2008c2ecf20Sopenharmony_ci * prevents it from signalling the fence the previous thread is stuck waiting 2018c2ecf20Sopenharmony_ci * on:: 2028c2ecf20Sopenharmony_ci * 2038c2ecf20Sopenharmony_ci * lock(A); 2048c2ecf20Sopenharmony_ci * unlock(A); 2058c2ecf20Sopenharmony_ci * dma_fence_signal(B); 2068c2ecf20Sopenharmony_ci * 2078c2ecf20Sopenharmony_ci * By manually annotating all code relevant to signalling a &dma_fence we can 2088c2ecf20Sopenharmony_ci * teach lockdep about these dependencies, which also helps with the validation 2098c2ecf20Sopenharmony_ci * headache since now lockdep can check all the rules for us:: 2108c2ecf20Sopenharmony_ci * 2118c2ecf20Sopenharmony_ci * cookie = dma_fence_begin_signalling(); 2128c2ecf20Sopenharmony_ci * lock(A); 2138c2ecf20Sopenharmony_ci * unlock(A); 2148c2ecf20Sopenharmony_ci * dma_fence_signal(B); 2158c2ecf20Sopenharmony_ci * dma_fence_end_signalling(cookie); 2168c2ecf20Sopenharmony_ci * 2178c2ecf20Sopenharmony_ci * For using dma_fence_begin_signalling() and dma_fence_end_signalling() to 2188c2ecf20Sopenharmony_ci * annotate critical sections the following rules need to be observed: 2198c2ecf20Sopenharmony_ci * 2208c2ecf20Sopenharmony_ci * * All code necessary to complete a &dma_fence must be annotated, from the 2218c2ecf20Sopenharmony_ci * point where a fence is accessible to other threads, to the point where 2228c2ecf20Sopenharmony_ci * dma_fence_signal() is called. Un-annotated code can contain deadlock issues, 2238c2ecf20Sopenharmony_ci * and due to the very strict rules and many corner cases it is infeasible to 2248c2ecf20Sopenharmony_ci * catch these just with review or normal stress testing. 2258c2ecf20Sopenharmony_ci * 2268c2ecf20Sopenharmony_ci * * &struct dma_resv deserves a special note, since the readers are only 2278c2ecf20Sopenharmony_ci * protected by rcu. This means the signalling critical section starts as soon 2288c2ecf20Sopenharmony_ci * as the new fences are installed, even before dma_resv_unlock() is called. 2298c2ecf20Sopenharmony_ci * 2308c2ecf20Sopenharmony_ci * * The only exception are fast paths and opportunistic signalling code, which 2318c2ecf20Sopenharmony_ci * calls dma_fence_signal() purely as an optimization, but is not required to 2328c2ecf20Sopenharmony_ci * guarantee completion of a &dma_fence. The usual example is a wait IOCTL 2338c2ecf20Sopenharmony_ci * which calls dma_fence_signal(), while the mandatory completion path goes 2348c2ecf20Sopenharmony_ci * through a hardware interrupt and possible job completion worker. 2358c2ecf20Sopenharmony_ci * 2368c2ecf20Sopenharmony_ci * * To aid composability of code, the annotations can be freely nested, as long 2378c2ecf20Sopenharmony_ci * as the overall locking hierarchy is consistent. The annotations also work 2388c2ecf20Sopenharmony_ci * both in interrupt and process context. Due to implementation details this 2398c2ecf20Sopenharmony_ci * requires that callers pass an opaque cookie from 2408c2ecf20Sopenharmony_ci * dma_fence_begin_signalling() to dma_fence_end_signalling(). 2418c2ecf20Sopenharmony_ci * 2428c2ecf20Sopenharmony_ci * * Validation against the cross driver contract is implemented by priming 2438c2ecf20Sopenharmony_ci * lockdep with the relevant hierarchy at boot-up. This means even just 2448c2ecf20Sopenharmony_ci * testing with a single device is enough to validate a driver, at least as 2458c2ecf20Sopenharmony_ci * far as deadlocks with dma_fence_wait() against dma_fence_signal() are 2468c2ecf20Sopenharmony_ci * concerned. 2478c2ecf20Sopenharmony_ci */ 2488c2ecf20Sopenharmony_ci#ifdef CONFIG_LOCKDEP 2498c2ecf20Sopenharmony_cistatic struct lockdep_map dma_fence_lockdep_map = { 2508c2ecf20Sopenharmony_ci .name = "dma_fence_map" 2518c2ecf20Sopenharmony_ci}; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/** 2548c2ecf20Sopenharmony_ci * dma_fence_begin_signalling - begin a critical DMA fence signalling section 2558c2ecf20Sopenharmony_ci * 2568c2ecf20Sopenharmony_ci * Drivers should use this to annotate the beginning of any code section 2578c2ecf20Sopenharmony_ci * required to eventually complete &dma_fence by calling dma_fence_signal(). 2588c2ecf20Sopenharmony_ci * 2598c2ecf20Sopenharmony_ci * The end of these critical sections are annotated with 2608c2ecf20Sopenharmony_ci * dma_fence_end_signalling(). 2618c2ecf20Sopenharmony_ci * 2628c2ecf20Sopenharmony_ci * Returns: 2638c2ecf20Sopenharmony_ci * 2648c2ecf20Sopenharmony_ci * Opaque cookie needed by the implementation, which needs to be passed to 2658c2ecf20Sopenharmony_ci * dma_fence_end_signalling(). 2668c2ecf20Sopenharmony_ci */ 2678c2ecf20Sopenharmony_cibool dma_fence_begin_signalling(void) 2688c2ecf20Sopenharmony_ci{ 2698c2ecf20Sopenharmony_ci /* explicitly nesting ... */ 2708c2ecf20Sopenharmony_ci if (lock_is_held_type(&dma_fence_lockdep_map, 1)) 2718c2ecf20Sopenharmony_ci return true; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci /* rely on might_sleep check for soft/hardirq locks */ 2748c2ecf20Sopenharmony_ci if (in_atomic()) 2758c2ecf20Sopenharmony_ci return true; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci /* ... and non-recursive readlock */ 2788c2ecf20Sopenharmony_ci lock_acquire(&dma_fence_lockdep_map, 0, 0, 1, 1, NULL, _RET_IP_); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci return false; 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_begin_signalling); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci/** 2858c2ecf20Sopenharmony_ci * dma_fence_end_signalling - end a critical DMA fence signalling section 2868c2ecf20Sopenharmony_ci * @cookie: opaque cookie from dma_fence_begin_signalling() 2878c2ecf20Sopenharmony_ci * 2888c2ecf20Sopenharmony_ci * Closes a critical section annotation opened by dma_fence_begin_signalling(). 2898c2ecf20Sopenharmony_ci */ 2908c2ecf20Sopenharmony_civoid dma_fence_end_signalling(bool cookie) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci if (cookie) 2938c2ecf20Sopenharmony_ci return; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci lock_release(&dma_fence_lockdep_map, _RET_IP_); 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_end_signalling); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_civoid __dma_fence_might_wait(void) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci bool tmp; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci tmp = lock_is_held_type(&dma_fence_lockdep_map, 1); 3048c2ecf20Sopenharmony_ci if (tmp) 3058c2ecf20Sopenharmony_ci lock_release(&dma_fence_lockdep_map, _THIS_IP_); 3068c2ecf20Sopenharmony_ci lock_map_acquire(&dma_fence_lockdep_map); 3078c2ecf20Sopenharmony_ci lock_map_release(&dma_fence_lockdep_map); 3088c2ecf20Sopenharmony_ci if (tmp) 3098c2ecf20Sopenharmony_ci lock_acquire(&dma_fence_lockdep_map, 0, 0, 1, 1, NULL, _THIS_IP_); 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ci#endif 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci/** 3158c2ecf20Sopenharmony_ci * dma_fence_signal_locked - signal completion of a fence 3168c2ecf20Sopenharmony_ci * @fence: the fence to signal 3178c2ecf20Sopenharmony_ci * 3188c2ecf20Sopenharmony_ci * Signal completion for software callbacks on a fence, this will unblock 3198c2ecf20Sopenharmony_ci * dma_fence_wait() calls and run all the callbacks added with 3208c2ecf20Sopenharmony_ci * dma_fence_add_callback(). Can be called multiple times, but since a fence 3218c2ecf20Sopenharmony_ci * can only go from the unsignaled to the signaled state and not back, it will 3228c2ecf20Sopenharmony_ci * only be effective the first time. 3238c2ecf20Sopenharmony_ci * 3248c2ecf20Sopenharmony_ci * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock 3258c2ecf20Sopenharmony_ci * held. 3268c2ecf20Sopenharmony_ci * 3278c2ecf20Sopenharmony_ci * Returns 0 on success and a negative error value when @fence has been 3288c2ecf20Sopenharmony_ci * signalled already. 3298c2ecf20Sopenharmony_ci */ 3308c2ecf20Sopenharmony_ciint dma_fence_signal_locked(struct dma_fence *fence) 3318c2ecf20Sopenharmony_ci{ 3328c2ecf20Sopenharmony_ci struct dma_fence_cb *cur, *tmp; 3338c2ecf20Sopenharmony_ci struct list_head cb_list; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci lockdep_assert_held(fence->lock); 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, 3388c2ecf20Sopenharmony_ci &fence->flags))) 3398c2ecf20Sopenharmony_ci return -EINVAL; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci /* Stash the cb_list before replacing it with the timestamp */ 3428c2ecf20Sopenharmony_ci list_replace(&fence->cb_list, &cb_list); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci fence->timestamp = ktime_get(); 3458c2ecf20Sopenharmony_ci set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags); 3468c2ecf20Sopenharmony_ci trace_dma_fence_signaled(fence); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci list_for_each_entry_safe(cur, tmp, &cb_list, node) { 3498c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&cur->node); 3508c2ecf20Sopenharmony_ci cur->func(fence, cur); 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci return 0; 3548c2ecf20Sopenharmony_ci} 3558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_signal_locked); 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci/** 3588c2ecf20Sopenharmony_ci * dma_fence_signal - signal completion of a fence 3598c2ecf20Sopenharmony_ci * @fence: the fence to signal 3608c2ecf20Sopenharmony_ci * 3618c2ecf20Sopenharmony_ci * Signal completion for software callbacks on a fence, this will unblock 3628c2ecf20Sopenharmony_ci * dma_fence_wait() calls and run all the callbacks added with 3638c2ecf20Sopenharmony_ci * dma_fence_add_callback(). Can be called multiple times, but since a fence 3648c2ecf20Sopenharmony_ci * can only go from the unsignaled to the signaled state and not back, it will 3658c2ecf20Sopenharmony_ci * only be effective the first time. 3668c2ecf20Sopenharmony_ci * 3678c2ecf20Sopenharmony_ci * Returns 0 on success and a negative error value when @fence has been 3688c2ecf20Sopenharmony_ci * signalled already. 3698c2ecf20Sopenharmony_ci */ 3708c2ecf20Sopenharmony_ciint dma_fence_signal(struct dma_fence *fence) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci unsigned long flags; 3738c2ecf20Sopenharmony_ci int ret; 3748c2ecf20Sopenharmony_ci bool tmp; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci if (!fence) 3778c2ecf20Sopenharmony_ci return -EINVAL; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci tmp = dma_fence_begin_signalling(); 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 3828c2ecf20Sopenharmony_ci ret = dma_fence_signal_locked(fence); 3838c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci dma_fence_end_signalling(tmp); 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci return ret; 3888c2ecf20Sopenharmony_ci} 3898c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_signal); 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci/** 3928c2ecf20Sopenharmony_ci * dma_fence_wait_timeout - sleep until the fence gets signaled 3938c2ecf20Sopenharmony_ci * or until timeout elapses 3948c2ecf20Sopenharmony_ci * @fence: the fence to wait on 3958c2ecf20Sopenharmony_ci * @intr: if true, do an interruptible wait 3968c2ecf20Sopenharmony_ci * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT 3978c2ecf20Sopenharmony_ci * 3988c2ecf20Sopenharmony_ci * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the 3998c2ecf20Sopenharmony_ci * remaining timeout in jiffies on success. Other error values may be 4008c2ecf20Sopenharmony_ci * returned on custom implementations. 4018c2ecf20Sopenharmony_ci * 4028c2ecf20Sopenharmony_ci * Performs a synchronous wait on this fence. It is assumed the caller 4038c2ecf20Sopenharmony_ci * directly or indirectly (buf-mgr between reservation and committing) 4048c2ecf20Sopenharmony_ci * holds a reference to the fence, otherwise the fence might be 4058c2ecf20Sopenharmony_ci * freed before return, resulting in undefined behavior. 4068c2ecf20Sopenharmony_ci * 4078c2ecf20Sopenharmony_ci * See also dma_fence_wait() and dma_fence_wait_any_timeout(). 4088c2ecf20Sopenharmony_ci */ 4098c2ecf20Sopenharmony_cisigned long 4108c2ecf20Sopenharmony_cidma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout) 4118c2ecf20Sopenharmony_ci{ 4128c2ecf20Sopenharmony_ci signed long ret; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci if (WARN_ON(timeout < 0)) 4158c2ecf20Sopenharmony_ci return -EINVAL; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci might_sleep(); 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci __dma_fence_might_wait(); 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci trace_dma_fence_wait_start(fence); 4228c2ecf20Sopenharmony_ci if (fence->ops->wait) 4238c2ecf20Sopenharmony_ci ret = fence->ops->wait(fence, intr, timeout); 4248c2ecf20Sopenharmony_ci else 4258c2ecf20Sopenharmony_ci ret = dma_fence_default_wait(fence, intr, timeout); 4268c2ecf20Sopenharmony_ci trace_dma_fence_wait_end(fence); 4278c2ecf20Sopenharmony_ci return ret; 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_wait_timeout); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci/** 4328c2ecf20Sopenharmony_ci * dma_fence_release - default relese function for fences 4338c2ecf20Sopenharmony_ci * @kref: &dma_fence.recfount 4348c2ecf20Sopenharmony_ci * 4358c2ecf20Sopenharmony_ci * This is the default release functions for &dma_fence. Drivers shouldn't call 4368c2ecf20Sopenharmony_ci * this directly, but instead call dma_fence_put(). 4378c2ecf20Sopenharmony_ci */ 4388c2ecf20Sopenharmony_civoid dma_fence_release(struct kref *kref) 4398c2ecf20Sopenharmony_ci{ 4408c2ecf20Sopenharmony_ci struct dma_fence *fence = 4418c2ecf20Sopenharmony_ci container_of(kref, struct dma_fence, refcount); 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci trace_dma_fence_destroy(fence); 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci if (WARN(!list_empty(&fence->cb_list) && 4468c2ecf20Sopenharmony_ci !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags), 4478c2ecf20Sopenharmony_ci "Fence %s:%s:%llx:%llx released with pending signals!\n", 4488c2ecf20Sopenharmony_ci fence->ops->get_driver_name(fence), 4498c2ecf20Sopenharmony_ci fence->ops->get_timeline_name(fence), 4508c2ecf20Sopenharmony_ci fence->context, fence->seqno)) { 4518c2ecf20Sopenharmony_ci unsigned long flags; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci /* 4548c2ecf20Sopenharmony_ci * Failed to signal before release, likely a refcounting issue. 4558c2ecf20Sopenharmony_ci * 4568c2ecf20Sopenharmony_ci * This should never happen, but if it does make sure that we 4578c2ecf20Sopenharmony_ci * don't leave chains dangling. We set the error flag first 4588c2ecf20Sopenharmony_ci * so that the callbacks know this signal is due to an error. 4598c2ecf20Sopenharmony_ci */ 4608c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 4618c2ecf20Sopenharmony_ci fence->error = -EDEADLK; 4628c2ecf20Sopenharmony_ci dma_fence_signal_locked(fence); 4638c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci if (fence->ops->release) 4678c2ecf20Sopenharmony_ci fence->ops->release(fence); 4688c2ecf20Sopenharmony_ci else 4698c2ecf20Sopenharmony_ci dma_fence_free(fence); 4708c2ecf20Sopenharmony_ci} 4718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_release); 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci/** 4748c2ecf20Sopenharmony_ci * dma_fence_free - default release function for &dma_fence. 4758c2ecf20Sopenharmony_ci * @fence: fence to release 4768c2ecf20Sopenharmony_ci * 4778c2ecf20Sopenharmony_ci * This is the default implementation for &dma_fence_ops.release. It calls 4788c2ecf20Sopenharmony_ci * kfree_rcu() on @fence. 4798c2ecf20Sopenharmony_ci */ 4808c2ecf20Sopenharmony_civoid dma_fence_free(struct dma_fence *fence) 4818c2ecf20Sopenharmony_ci{ 4828c2ecf20Sopenharmony_ci kfree_rcu(fence, rcu); 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_free); 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_cistatic bool __dma_fence_enable_signaling(struct dma_fence *fence) 4878c2ecf20Sopenharmony_ci{ 4888c2ecf20Sopenharmony_ci bool was_set; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci lockdep_assert_held(fence->lock); 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, 4938c2ecf20Sopenharmony_ci &fence->flags); 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 4968c2ecf20Sopenharmony_ci return false; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci if (!was_set && fence->ops->enable_signaling) { 4998c2ecf20Sopenharmony_ci trace_dma_fence_enable_signal(fence); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci if (!fence->ops->enable_signaling(fence)) { 5028c2ecf20Sopenharmony_ci dma_fence_signal_locked(fence); 5038c2ecf20Sopenharmony_ci return false; 5048c2ecf20Sopenharmony_ci } 5058c2ecf20Sopenharmony_ci } 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci return true; 5088c2ecf20Sopenharmony_ci} 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci/** 5118c2ecf20Sopenharmony_ci * dma_fence_enable_sw_signaling - enable signaling on fence 5128c2ecf20Sopenharmony_ci * @fence: the fence to enable 5138c2ecf20Sopenharmony_ci * 5148c2ecf20Sopenharmony_ci * This will request for sw signaling to be enabled, to make the fence 5158c2ecf20Sopenharmony_ci * complete as soon as possible. This calls &dma_fence_ops.enable_signaling 5168c2ecf20Sopenharmony_ci * internally. 5178c2ecf20Sopenharmony_ci */ 5188c2ecf20Sopenharmony_civoid dma_fence_enable_sw_signaling(struct dma_fence *fence) 5198c2ecf20Sopenharmony_ci{ 5208c2ecf20Sopenharmony_ci unsigned long flags; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 5238c2ecf20Sopenharmony_ci return; 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 5268c2ecf20Sopenharmony_ci __dma_fence_enable_signaling(fence); 5278c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 5288c2ecf20Sopenharmony_ci} 5298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_enable_sw_signaling); 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci/** 5328c2ecf20Sopenharmony_ci * dma_fence_add_callback - add a callback to be called when the fence 5338c2ecf20Sopenharmony_ci * is signaled 5348c2ecf20Sopenharmony_ci * @fence: the fence to wait on 5358c2ecf20Sopenharmony_ci * @cb: the callback to register 5368c2ecf20Sopenharmony_ci * @func: the function to call 5378c2ecf20Sopenharmony_ci * 5388c2ecf20Sopenharmony_ci * @cb will be initialized by dma_fence_add_callback(), no initialization 5398c2ecf20Sopenharmony_ci * by the caller is required. Any number of callbacks can be registered 5408c2ecf20Sopenharmony_ci * to a fence, but a callback can only be registered to one fence at a time. 5418c2ecf20Sopenharmony_ci * 5428c2ecf20Sopenharmony_ci * Note that the callback can be called from an atomic context. If 5438c2ecf20Sopenharmony_ci * fence is already signaled, this function will return -ENOENT (and 5448c2ecf20Sopenharmony_ci * *not* call the callback). 5458c2ecf20Sopenharmony_ci * 5468c2ecf20Sopenharmony_ci * Add a software callback to the fence. Same restrictions apply to 5478c2ecf20Sopenharmony_ci * refcount as it does to dma_fence_wait(), however the caller doesn't need to 5488c2ecf20Sopenharmony_ci * keep a refcount to fence afterward dma_fence_add_callback() has returned: 5498c2ecf20Sopenharmony_ci * when software access is enabled, the creator of the fence is required to keep 5508c2ecf20Sopenharmony_ci * the fence alive until after it signals with dma_fence_signal(). The callback 5518c2ecf20Sopenharmony_ci * itself can be called from irq context. 5528c2ecf20Sopenharmony_ci * 5538c2ecf20Sopenharmony_ci * Returns 0 in case of success, -ENOENT if the fence is already signaled 5548c2ecf20Sopenharmony_ci * and -EINVAL in case of error. 5558c2ecf20Sopenharmony_ci */ 5568c2ecf20Sopenharmony_ciint dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb, 5578c2ecf20Sopenharmony_ci dma_fence_func_t func) 5588c2ecf20Sopenharmony_ci{ 5598c2ecf20Sopenharmony_ci unsigned long flags; 5608c2ecf20Sopenharmony_ci int ret = 0; 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci if (WARN_ON(!fence || !func)) 5638c2ecf20Sopenharmony_ci return -EINVAL; 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { 5668c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&cb->node); 5678c2ecf20Sopenharmony_ci return -ENOENT; 5688c2ecf20Sopenharmony_ci } 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci if (__dma_fence_enable_signaling(fence)) { 5738c2ecf20Sopenharmony_ci cb->func = func; 5748c2ecf20Sopenharmony_ci list_add_tail(&cb->node, &fence->cb_list); 5758c2ecf20Sopenharmony_ci } else { 5768c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&cb->node); 5778c2ecf20Sopenharmony_ci ret = -ENOENT; 5788c2ecf20Sopenharmony_ci } 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci return ret; 5838c2ecf20Sopenharmony_ci} 5848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_add_callback); 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci/** 5878c2ecf20Sopenharmony_ci * dma_fence_get_status - returns the status upon completion 5888c2ecf20Sopenharmony_ci * @fence: the dma_fence to query 5898c2ecf20Sopenharmony_ci * 5908c2ecf20Sopenharmony_ci * This wraps dma_fence_get_status_locked() to return the error status 5918c2ecf20Sopenharmony_ci * condition on a signaled fence. See dma_fence_get_status_locked() for more 5928c2ecf20Sopenharmony_ci * details. 5938c2ecf20Sopenharmony_ci * 5948c2ecf20Sopenharmony_ci * Returns 0 if the fence has not yet been signaled, 1 if the fence has 5958c2ecf20Sopenharmony_ci * been signaled without an error condition, or a negative error code 5968c2ecf20Sopenharmony_ci * if the fence has been completed in err. 5978c2ecf20Sopenharmony_ci */ 5988c2ecf20Sopenharmony_ciint dma_fence_get_status(struct dma_fence *fence) 5998c2ecf20Sopenharmony_ci{ 6008c2ecf20Sopenharmony_ci unsigned long flags; 6018c2ecf20Sopenharmony_ci int status; 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 6048c2ecf20Sopenharmony_ci status = dma_fence_get_status_locked(fence); 6058c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_ci return status; 6088c2ecf20Sopenharmony_ci} 6098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_get_status); 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci/** 6128c2ecf20Sopenharmony_ci * dma_fence_remove_callback - remove a callback from the signaling list 6138c2ecf20Sopenharmony_ci * @fence: the fence to wait on 6148c2ecf20Sopenharmony_ci * @cb: the callback to remove 6158c2ecf20Sopenharmony_ci * 6168c2ecf20Sopenharmony_ci * Remove a previously queued callback from the fence. This function returns 6178c2ecf20Sopenharmony_ci * true if the callback is successfully removed, or false if the fence has 6188c2ecf20Sopenharmony_ci * already been signaled. 6198c2ecf20Sopenharmony_ci * 6208c2ecf20Sopenharmony_ci * *WARNING*: 6218c2ecf20Sopenharmony_ci * Cancelling a callback should only be done if you really know what you're 6228c2ecf20Sopenharmony_ci * doing, since deadlocks and race conditions could occur all too easily. For 6238c2ecf20Sopenharmony_ci * this reason, it should only ever be done on hardware lockup recovery, 6248c2ecf20Sopenharmony_ci * with a reference held to the fence. 6258c2ecf20Sopenharmony_ci * 6268c2ecf20Sopenharmony_ci * Behaviour is undefined if @cb has not been added to @fence using 6278c2ecf20Sopenharmony_ci * dma_fence_add_callback() beforehand. 6288c2ecf20Sopenharmony_ci */ 6298c2ecf20Sopenharmony_cibool 6308c2ecf20Sopenharmony_cidma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb) 6318c2ecf20Sopenharmony_ci{ 6328c2ecf20Sopenharmony_ci unsigned long flags; 6338c2ecf20Sopenharmony_ci bool ret; 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci ret = !list_empty(&cb->node); 6388c2ecf20Sopenharmony_ci if (ret) 6398c2ecf20Sopenharmony_ci list_del_init(&cb->node); 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci return ret; 6448c2ecf20Sopenharmony_ci} 6458c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_remove_callback); 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_cistruct default_wait_cb { 6488c2ecf20Sopenharmony_ci struct dma_fence_cb base; 6498c2ecf20Sopenharmony_ci struct task_struct *task; 6508c2ecf20Sopenharmony_ci}; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_cistatic void 6538c2ecf20Sopenharmony_cidma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb) 6548c2ecf20Sopenharmony_ci{ 6558c2ecf20Sopenharmony_ci struct default_wait_cb *wait = 6568c2ecf20Sopenharmony_ci container_of(cb, struct default_wait_cb, base); 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_ci wake_up_state(wait->task, TASK_NORMAL); 6598c2ecf20Sopenharmony_ci} 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci/** 6628c2ecf20Sopenharmony_ci * dma_fence_default_wait - default sleep until the fence gets signaled 6638c2ecf20Sopenharmony_ci * or until timeout elapses 6648c2ecf20Sopenharmony_ci * @fence: the fence to wait on 6658c2ecf20Sopenharmony_ci * @intr: if true, do an interruptible wait 6668c2ecf20Sopenharmony_ci * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT 6678c2ecf20Sopenharmony_ci * 6688c2ecf20Sopenharmony_ci * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the 6698c2ecf20Sopenharmony_ci * remaining timeout in jiffies on success. If timeout is zero the value one is 6708c2ecf20Sopenharmony_ci * returned if the fence is already signaled for consistency with other 6718c2ecf20Sopenharmony_ci * functions taking a jiffies timeout. 6728c2ecf20Sopenharmony_ci */ 6738c2ecf20Sopenharmony_cisigned long 6748c2ecf20Sopenharmony_cidma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout) 6758c2ecf20Sopenharmony_ci{ 6768c2ecf20Sopenharmony_ci struct default_wait_cb cb; 6778c2ecf20Sopenharmony_ci unsigned long flags; 6788c2ecf20Sopenharmony_ci signed long ret = timeout ? timeout : 1; 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 6818c2ecf20Sopenharmony_ci return ret; 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci if (intr && signal_pending(current)) { 6868c2ecf20Sopenharmony_ci ret = -ERESTARTSYS; 6878c2ecf20Sopenharmony_ci goto out; 6888c2ecf20Sopenharmony_ci } 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci if (!__dma_fence_enable_signaling(fence)) 6918c2ecf20Sopenharmony_ci goto out; 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci if (!timeout) { 6948c2ecf20Sopenharmony_ci ret = 0; 6958c2ecf20Sopenharmony_ci goto out; 6968c2ecf20Sopenharmony_ci } 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci cb.base.func = dma_fence_default_wait_cb; 6998c2ecf20Sopenharmony_ci cb.task = current; 7008c2ecf20Sopenharmony_ci list_add(&cb.base.node, &fence->cb_list); 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) { 7038c2ecf20Sopenharmony_ci if (intr) 7048c2ecf20Sopenharmony_ci __set_current_state(TASK_INTERRUPTIBLE); 7058c2ecf20Sopenharmony_ci else 7068c2ecf20Sopenharmony_ci __set_current_state(TASK_UNINTERRUPTIBLE); 7078c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_ci ret = schedule_timeout(ret); 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci spin_lock_irqsave(fence->lock, flags); 7128c2ecf20Sopenharmony_ci if (ret > 0 && intr && signal_pending(current)) 7138c2ecf20Sopenharmony_ci ret = -ERESTARTSYS; 7148c2ecf20Sopenharmony_ci } 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci if (!list_empty(&cb.base.node)) 7178c2ecf20Sopenharmony_ci list_del(&cb.base.node); 7188c2ecf20Sopenharmony_ci __set_current_state(TASK_RUNNING); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ciout: 7218c2ecf20Sopenharmony_ci spin_unlock_irqrestore(fence->lock, flags); 7228c2ecf20Sopenharmony_ci return ret; 7238c2ecf20Sopenharmony_ci} 7248c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_default_wait); 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_cistatic bool 7278c2ecf20Sopenharmony_cidma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count, 7288c2ecf20Sopenharmony_ci uint32_t *idx) 7298c2ecf20Sopenharmony_ci{ 7308c2ecf20Sopenharmony_ci int i; 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci for (i = 0; i < count; ++i) { 7338c2ecf20Sopenharmony_ci struct dma_fence *fence = fences[i]; 7348c2ecf20Sopenharmony_ci if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { 7358c2ecf20Sopenharmony_ci if (idx) 7368c2ecf20Sopenharmony_ci *idx = i; 7378c2ecf20Sopenharmony_ci return true; 7388c2ecf20Sopenharmony_ci } 7398c2ecf20Sopenharmony_ci } 7408c2ecf20Sopenharmony_ci return false; 7418c2ecf20Sopenharmony_ci} 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci/** 7448c2ecf20Sopenharmony_ci * dma_fence_wait_any_timeout - sleep until any fence gets signaled 7458c2ecf20Sopenharmony_ci * or until timeout elapses 7468c2ecf20Sopenharmony_ci * @fences: array of fences to wait on 7478c2ecf20Sopenharmony_ci * @count: number of fences to wait on 7488c2ecf20Sopenharmony_ci * @intr: if true, do an interruptible wait 7498c2ecf20Sopenharmony_ci * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT 7508c2ecf20Sopenharmony_ci * @idx: used to store the first signaled fence index, meaningful only on 7518c2ecf20Sopenharmony_ci * positive return 7528c2ecf20Sopenharmony_ci * 7538c2ecf20Sopenharmony_ci * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if 7548c2ecf20Sopenharmony_ci * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies 7558c2ecf20Sopenharmony_ci * on success. 7568c2ecf20Sopenharmony_ci * 7578c2ecf20Sopenharmony_ci * Synchronous waits for the first fence in the array to be signaled. The 7588c2ecf20Sopenharmony_ci * caller needs to hold a reference to all fences in the array, otherwise a 7598c2ecf20Sopenharmony_ci * fence might be freed before return, resulting in undefined behavior. 7608c2ecf20Sopenharmony_ci * 7618c2ecf20Sopenharmony_ci * See also dma_fence_wait() and dma_fence_wait_timeout(). 7628c2ecf20Sopenharmony_ci */ 7638c2ecf20Sopenharmony_cisigned long 7648c2ecf20Sopenharmony_cidma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count, 7658c2ecf20Sopenharmony_ci bool intr, signed long timeout, uint32_t *idx) 7668c2ecf20Sopenharmony_ci{ 7678c2ecf20Sopenharmony_ci struct default_wait_cb *cb; 7688c2ecf20Sopenharmony_ci signed long ret = timeout; 7698c2ecf20Sopenharmony_ci unsigned i; 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci if (WARN_ON(!fences || !count || timeout < 0)) 7728c2ecf20Sopenharmony_ci return -EINVAL; 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci if (timeout == 0) { 7758c2ecf20Sopenharmony_ci for (i = 0; i < count; ++i) 7768c2ecf20Sopenharmony_ci if (dma_fence_is_signaled(fences[i])) { 7778c2ecf20Sopenharmony_ci if (idx) 7788c2ecf20Sopenharmony_ci *idx = i; 7798c2ecf20Sopenharmony_ci return 1; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci return 0; 7838c2ecf20Sopenharmony_ci } 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL); 7868c2ecf20Sopenharmony_ci if (cb == NULL) { 7878c2ecf20Sopenharmony_ci ret = -ENOMEM; 7888c2ecf20Sopenharmony_ci goto err_free_cb; 7898c2ecf20Sopenharmony_ci } 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci for (i = 0; i < count; ++i) { 7928c2ecf20Sopenharmony_ci struct dma_fence *fence = fences[i]; 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci cb[i].task = current; 7958c2ecf20Sopenharmony_ci if (dma_fence_add_callback(fence, &cb[i].base, 7968c2ecf20Sopenharmony_ci dma_fence_default_wait_cb)) { 7978c2ecf20Sopenharmony_ci /* This fence is already signaled */ 7988c2ecf20Sopenharmony_ci if (idx) 7998c2ecf20Sopenharmony_ci *idx = i; 8008c2ecf20Sopenharmony_ci goto fence_rm_cb; 8018c2ecf20Sopenharmony_ci } 8028c2ecf20Sopenharmony_ci } 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci while (ret > 0) { 8058c2ecf20Sopenharmony_ci if (intr) 8068c2ecf20Sopenharmony_ci set_current_state(TASK_INTERRUPTIBLE); 8078c2ecf20Sopenharmony_ci else 8088c2ecf20Sopenharmony_ci set_current_state(TASK_UNINTERRUPTIBLE); 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci if (dma_fence_test_signaled_any(fences, count, idx)) 8118c2ecf20Sopenharmony_ci break; 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci ret = schedule_timeout(ret); 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci if (ret > 0 && intr && signal_pending(current)) 8168c2ecf20Sopenharmony_ci ret = -ERESTARTSYS; 8178c2ecf20Sopenharmony_ci } 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci __set_current_state(TASK_RUNNING); 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_cifence_rm_cb: 8228c2ecf20Sopenharmony_ci while (i-- > 0) 8238c2ecf20Sopenharmony_ci dma_fence_remove_callback(fences[i], &cb[i].base); 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_cierr_free_cb: 8268c2ecf20Sopenharmony_ci kfree(cb); 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci return ret; 8298c2ecf20Sopenharmony_ci} 8308c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_wait_any_timeout); 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci/** 8338c2ecf20Sopenharmony_ci * dma_fence_init - Initialize a custom fence. 8348c2ecf20Sopenharmony_ci * @fence: the fence to initialize 8358c2ecf20Sopenharmony_ci * @ops: the dma_fence_ops for operations on this fence 8368c2ecf20Sopenharmony_ci * @lock: the irqsafe spinlock to use for locking this fence 8378c2ecf20Sopenharmony_ci * @context: the execution context this fence is run on 8388c2ecf20Sopenharmony_ci * @seqno: a linear increasing sequence number for this context 8398c2ecf20Sopenharmony_ci * 8408c2ecf20Sopenharmony_ci * Initializes an allocated fence, the caller doesn't have to keep its 8418c2ecf20Sopenharmony_ci * refcount after committing with this fence, but it will need to hold a 8428c2ecf20Sopenharmony_ci * refcount again if &dma_fence_ops.enable_signaling gets called. 8438c2ecf20Sopenharmony_ci * 8448c2ecf20Sopenharmony_ci * context and seqno are used for easy comparison between fences, allowing 8458c2ecf20Sopenharmony_ci * to check which fence is later by simply using dma_fence_later(). 8468c2ecf20Sopenharmony_ci */ 8478c2ecf20Sopenharmony_civoid 8488c2ecf20Sopenharmony_cidma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops, 8498c2ecf20Sopenharmony_ci spinlock_t *lock, u64 context, u64 seqno) 8508c2ecf20Sopenharmony_ci{ 8518c2ecf20Sopenharmony_ci BUG_ON(!lock); 8528c2ecf20Sopenharmony_ci BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name); 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci kref_init(&fence->refcount); 8558c2ecf20Sopenharmony_ci fence->ops = ops; 8568c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&fence->cb_list); 8578c2ecf20Sopenharmony_ci fence->lock = lock; 8588c2ecf20Sopenharmony_ci fence->context = context; 8598c2ecf20Sopenharmony_ci fence->seqno = seqno; 8608c2ecf20Sopenharmony_ci fence->flags = 0UL; 8618c2ecf20Sopenharmony_ci fence->error = 0; 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci trace_dma_fence_init(fence); 8648c2ecf20Sopenharmony_ci} 8658c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dma_fence_init); 866