1290001Sglebius/* SPDX-License-Identifier: GPL-2.0-only */ 2290001Sglebius/* 3290001Sglebius * fence-chain: chain fences together in a timeline 4290001Sglebius * 5290001Sglebius * Copyright (C) 2018 Advanced Micro Devices, Inc. 6290001Sglebius * Authors: 7290001Sglebius * Christian K��nig <christian.koenig@amd.com> 8290001Sglebius */ 9290001Sglebius 10290001Sglebius#ifndef __LINUX_DMA_FENCE_CHAIN_H 11290001Sglebius#define __LINUX_DMA_FENCE_CHAIN_H 12290001Sglebius 13290001Sglebius#include <linux/dma-fence.h> 14290001Sglebius#include <linux/irq_work.h> 15290001Sglebius#include <linux/slab.h> 16290001Sglebius 17290001Sglebius/** 18290001Sglebius * struct dma_fence_chain - fence to represent an node of a fence chain 19290001Sglebius * @base: fence base class 20290001Sglebius * @prev: previous fence of the chain 21290001Sglebius * @prev_seqno: original previous seqno before garbage collection 22290001Sglebius * @fence: encapsulated fence 23290001Sglebius * @lock: spinlock for fence handling 24290001Sglebius */ 25290001Sglebiusstruct dma_fence_chain { 26290001Sglebius struct dma_fence base; 27290001Sglebius struct dma_fence __rcu *prev; 28290001Sglebius u64 prev_seqno; 29290001Sglebius struct dma_fence *fence; 30290001Sglebius union { 31290001Sglebius /** 32290001Sglebius * @cb: callback for signaling 33290001Sglebius * 34290001Sglebius * This is used to add the callback for signaling the 35290001Sglebius * complection of the fence chain. Never used at the same time 36290001Sglebius * as the irq work. 37290001Sglebius */ 38290001Sglebius struct dma_fence_cb cb; 39290001Sglebius 40290001Sglebius /** 41290001Sglebius * @work: irq work item for signaling 42290001Sglebius * 43290001Sglebius * Irq work structure to allow us to add the callback without 44290001Sglebius * running into lock inversion. Never used at the same time as 45290001Sglebius * the callback. 46290001Sglebius */ 47290001Sglebius struct irq_work work; 48290001Sglebius }; 49290001Sglebius spinlock_t lock; 50290001Sglebius}; 51290001Sglebius 52290001Sglebius 53290001Sglebius/** 54290001Sglebius * to_dma_fence_chain - cast a fence to a dma_fence_chain 55290001Sglebius * @fence: fence to cast to a dma_fence_array 56290001Sglebius * 57290001Sglebius * Returns NULL if the fence is not a dma_fence_chain, 58290001Sglebius * or the dma_fence_chain otherwise. 59290001Sglebius */ 60290001Sglebiusstatic inline struct dma_fence_chain * 61290001Sglebiusto_dma_fence_chain(struct dma_fence *fence) 62290001Sglebius{ 63290001Sglebius if (!fence || !dma_fence_is_chain(fence)) 64290001Sglebius return NULL; 65290001Sglebius 66290001Sglebius return container_of(fence, struct dma_fence_chain, base); 67290001Sglebius} 68290001Sglebius 69290001Sglebius/** 70290001Sglebius * dma_fence_chain_contained - return the contained fence 71290001Sglebius * @fence: the fence to test 72290001Sglebius * 73290001Sglebius * If the fence is a dma_fence_chain the function returns the fence contained 74290001Sglebius * inside the chain object, otherwise it returns the fence itself. 75290001Sglebius */ 76290001Sglebiusstatic inline struct dma_fence * 77290001Sglebiusdma_fence_chain_contained(struct dma_fence *fence) 78{ 79 struct dma_fence_chain *chain = to_dma_fence_chain(fence); 80 81 return chain ? chain->fence : fence; 82} 83 84/** 85 * dma_fence_chain_alloc 86 * 87 * Returns a new struct dma_fence_chain object or NULL on failure. 88 */ 89static inline struct dma_fence_chain *dma_fence_chain_alloc(void) 90{ 91 return kmalloc(sizeof(struct dma_fence_chain), GFP_KERNEL); 92}; 93 94/** 95 * dma_fence_chain_free 96 * @chain: chain node to free 97 * 98 * Frees up an allocated but not used struct dma_fence_chain object. This 99 * doesn't need an RCU grace period since the fence was never initialized nor 100 * published. After dma_fence_chain_init() has been called the fence must be 101 * released by calling dma_fence_put(), and not through this function. 102 */ 103static inline void dma_fence_chain_free(struct dma_fence_chain *chain) 104{ 105 kfree(chain); 106}; 107 108/** 109 * dma_fence_chain_for_each - iterate over all fences in chain 110 * @iter: current fence 111 * @head: starting point 112 * 113 * Iterate over all fences in the chain. We keep a reference to the current 114 * fence while inside the loop which must be dropped when breaking out. 115 * 116 * For a deep dive iterator see dma_fence_unwrap_for_each(). 117 */ 118#define dma_fence_chain_for_each(iter, head) \ 119 for (iter = dma_fence_get(head); iter; \ 120 iter = dma_fence_chain_walk(iter)) 121 122struct dma_fence *dma_fence_chain_walk(struct dma_fence *fence); 123int dma_fence_chain_find_seqno(struct dma_fence **pfence, uint64_t seqno); 124void dma_fence_chain_init(struct dma_fence_chain *chain, 125 struct dma_fence *prev, 126 struct dma_fence *fence, 127 uint64_t seqno); 128 129#endif /* __LINUX_DMA_FENCE_CHAIN_H */ 130