1 /* SPDX-License-Identifier: MIT */ 2 /* 3 * Copyright © 2021 Intel Corporation 4 */ 5 6 #ifndef __I915_VMA_RESOURCE_H__ 7 #define __I915_VMA_RESOURCE_H__ 8 9 #include <linux/dma-fence.h> 10 #include <linux/refcount.h> 11 12 #include "i915_gem.h" 13 #include "i915_scatterlist.h" 14 #include "i915_sw_fence.h" 15 #include "intel_runtime_pm.h" 16 17 struct intel_memory_region; 18 19 struct i915_page_sizes { 20 /** 21 * The sg mask of the pages sg_table. i.e the mask of 22 * the lengths for each sg entry. 23 */ 24 unsigned int phys; 25 26 /** 27 * The gtt page sizes we are allowed to use given the 28 * sg mask and the supported page sizes. This will 29 * express the smallest unit we can use for the whole 30 * object, as well as the larger sizes we may be able 31 * to use opportunistically. 32 */ 33 unsigned int sg; 34 }; 35 36 /** 37 * struct i915_vma_resource - Snapshotted unbind information. 38 * @unbind_fence: Fence to mark unbinding complete. Note that this fence 39 * is not considered published until unbind is scheduled, and as such it 40 * is illegal to access this fence before scheduled unbind other than 41 * for refcounting. 42 * @lock: The @unbind_fence lock. 43 * @hold_count: Number of holders blocking the fence from finishing. 44 * The vma itself is keeping a hold, which is released when unbind 45 * is scheduled. 46 * @work: Work struct for deferred unbind work. 47 * @chain: Pointer to struct i915_sw_fence used to await dependencies. 48 * @rb: Rb node for the vm's pending unbind interval tree. 49 * @__subtree_last: Interval tree private member. 50 * @vm: non-refcounted pointer to the vm. This is for internal use only and 51 * this member is cleared after vm_resource unbind. 52 * @mr: The memory region of the object pointed to by the vma. 53 * @ops: Pointer to the backend i915_vma_ops. 54 * @private: Bind backend private info. 55 * @start: Offset into the address space of bind range start. Note that 56 * this is after any padding that might have been allocated. 57 * @node_size: Size of the allocated range manager node with padding 58 * subtracted. 59 * @vma_size: Bind size. 60 * @page_sizes_gtt: Resulting page sizes from the bind operation. 61 * @bound_flags: Flags indicating binding status. 62 * @allocated: Backend private data. TODO: Should move into @private. 63 * @immediate_unbind: Unbind can be done immediately and doesn't need to be 64 * deferred to a work item awaiting unsignaled fences. This is a hack. 65 * (dma_fence_work uses a fence flag for this, but this seems slightly 66 * cleaner). 67 * @needs_wakeref: Whether a wakeref is needed during unbind. Since we can't 68 * take a wakeref in the dma-fence signalling critical path, it needs to be 69 * taken when the unbind is scheduled. 70 * @skip_pte_rewrite: During ggtt suspend and vm takedown pte rewriting 71 * needs to be skipped for unbind. 72 * @tlb: pointer for obj->mm.tlb, if async unbind. Otherwise, NULL 73 * 74 * The lifetime of a struct i915_vma_resource is from a binding request to 75 * the actual possible asynchronous unbind has completed. 76 */ 77 struct i915_vma_resource { 78 struct dma_fence unbind_fence; 79 /* See above for description of the lock. */ 80 spinlock_t lock; 81 refcount_t hold_count; 82 struct work_struct work; 83 struct i915_sw_fence chain; 84 struct rb_node rb; 85 u64 __subtree_last; 86 struct i915_address_space *vm; 87 intel_wakeref_t wakeref; 88 89 /** 90 * struct i915_vma_bindinfo - Information needed for async bind 91 * only but that can be dropped after the bind has taken place. 92 * Consider making this a separate argument to the bind_vma 93 * op, coalescing with other arguments like vm, stash, cache_level 94 * and flags 95 * @pages: The pages sg-table. 96 * @page_sizes: Page sizes of the pages. 97 * @pages_rsgt: Refcounted sg-table when delayed object destruction 98 * is supported. May be NULL. 99 * @readonly: Whether the vma should be bound read-only. 100 * @lmem: Whether the vma points to lmem. 101 */ 102 struct i915_vma_bindinfo { 103 struct sg_table *pages; 104 struct i915_page_sizes page_sizes; 105 struct i915_refct_sgt *pages_rsgt; 106 bool readonly:1; 107 bool lmem:1; 108 } bi; 109 110 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR) 111 struct intel_memory_region *mr; 112 #endif 113 const struct i915_vma_ops *ops; 114 void *private; 115 u64 start; 116 u64 node_size; 117 u64 vma_size; 118 u32 page_sizes_gtt; 119 120 u32 bound_flags; 121 bool allocated:1; 122 bool immediate_unbind:1; 123 bool needs_wakeref:1; 124 bool skip_pte_rewrite:1; 125 126 u32 *tlb; 127 }; 128 129 bool i915_vma_resource_hold(struct i915_vma_resource *vma_res, 130 bool *lockdep_cookie); 131 132 void i915_vma_resource_unhold(struct i915_vma_resource *vma_res, 133 bool lockdep_cookie); 134 135 struct i915_vma_resource *i915_vma_resource_alloc(void); 136 137 void i915_vma_resource_free(struct i915_vma_resource *vma_res); 138 139 struct dma_fence *i915_vma_resource_unbind(struct i915_vma_resource *vma_res, 140 u32 *tlb); 141 142 void __i915_vma_resource_init(struct i915_vma_resource *vma_res); 143 144 /** 145 * i915_vma_resource_get - Take a reference on a vma resource 146 * @vma_res: The vma resource on which to take a reference. 147 * 148 * Return: The @vma_res pointer 149 */ 150 static inline struct i915_vma_resource 151 *i915_vma_resource_get(struct i915_vma_resource *vma_res) 152 { 153 dma_fence_get(&vma_res->unbind_fence); 154 return vma_res; 155 } 156 157 /** 158 * i915_vma_resource_put - Release a reference to a struct i915_vma_resource 159 * @vma_res: The resource 160 */ 161 static inline void i915_vma_resource_put(struct i915_vma_resource *vma_res) 162 { 163 dma_fence_put(&vma_res->unbind_fence); 164 } 165 166 /** 167 * i915_vma_resource_init - Initialize a vma resource. 168 * @vma_res: The vma resource to initialize 169 * @vm: Pointer to the vm. 170 * @pages: The pages sg-table. 171 * @page_sizes: Page sizes of the pages. 172 * @pages_rsgt: Pointer to a struct i915_refct_sgt of an object with 173 * delayed destruction. 174 * @readonly: Whether the vma should be bound read-only. 175 * @lmem: Whether the vma points to lmem. 176 * @mr: The memory region of the object the vma points to. 177 * @ops: The backend ops. 178 * @private: Bind backend private info. 179 * @start: Offset into the address space of bind range start after padding. 180 * @node_size: Size of the allocated range manager node minus padding. 181 * @size: Bind size. 182 * 183 * Initializes a vma resource allocated using i915_vma_resource_alloc(). 184 * The reason for having separate allocate and initialize function is that 185 * initialization may need to be performed from under a lock where 186 * allocation is not allowed. 187 */ 188 static inline void i915_vma_resource_init(struct i915_vma_resource *vma_res, 189 struct i915_address_space *vm, 190 struct sg_table *pages, 191 const struct i915_page_sizes *page_sizes, 192 struct i915_refct_sgt *pages_rsgt, 193 bool readonly, 194 bool lmem, 195 struct intel_memory_region *mr, 196 const struct i915_vma_ops *ops, 197 void *private, 198 u64 start, 199 u64 node_size, 200 u64 size) 201 { 202 __i915_vma_resource_init(vma_res); 203 vma_res->vm = vm; 204 vma_res->bi.pages = pages; 205 vma_res->bi.page_sizes = *page_sizes; 206 if (pages_rsgt) 207 vma_res->bi.pages_rsgt = i915_refct_sgt_get(pages_rsgt); 208 vma_res->bi.readonly = readonly; 209 vma_res->bi.lmem = lmem; 210 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR) 211 vma_res->mr = mr; 212 #endif 213 vma_res->ops = ops; 214 vma_res->private = private; 215 vma_res->start = start; 216 vma_res->node_size = node_size; 217 vma_res->vma_size = size; 218 } 219 220 static inline void i915_vma_resource_fini(struct i915_vma_resource *vma_res) 221 { 222 GEM_BUG_ON(refcount_read(&vma_res->hold_count) != 1); 223 if (vma_res->bi.pages_rsgt) 224 i915_refct_sgt_put(vma_res->bi.pages_rsgt); 225 i915_sw_fence_fini(&vma_res->chain); 226 } 227 228 int i915_vma_resource_bind_dep_sync(struct i915_address_space *vm, 229 u64 first, 230 u64 last, 231 bool intr); 232 233 int i915_vma_resource_bind_dep_await(struct i915_address_space *vm, 234 struct i915_sw_fence *sw_fence, 235 u64 first, 236 u64 last, 237 bool intr, 238 gfp_t gfp); 239 240 void i915_vma_resource_bind_dep_sync_all(struct i915_address_space *vm); 241 242 void i915_vma_resource_module_exit(void); 243 244 int i915_vma_resource_module_init(void); 245 246 #endif 247