1 1.9 riastrad /* $NetBSD: i915_vma.h,v 1.9 2021/12/19 12:40:43 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2016 Intel Corporation 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice (including the next 14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 15 1.1 riastrad * Software. 16 1.1 riastrad * 17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 1.1 riastrad * IN THE SOFTWARE. 24 1.1 riastrad * 25 1.1 riastrad */ 26 1.1 riastrad 27 1.1 riastrad #ifndef __I915_VMA_H__ 28 1.1 riastrad #define __I915_VMA_H__ 29 1.1 riastrad 30 1.1 riastrad #include <linux/io-mapping.h> 31 1.1 riastrad #include <linux/rbtree.h> 32 1.1 riastrad 33 1.1 riastrad #include <drm/drm_mm.h> 34 1.1 riastrad 35 1.1 riastrad #include "gem/i915_gem_object.h" 36 1.1 riastrad 37 1.1 riastrad #include "i915_gem_gtt.h" 38 1.1 riastrad #include "i915_gem_fence_reg.h" 39 1.1 riastrad 40 1.1 riastrad #include "i915_active.h" 41 1.1 riastrad #include "i915_request.h" 42 1.1 riastrad #include "i915_vma_types.h" 43 1.1 riastrad 44 1.7 riastrad void i915_vma_tree_init(struct drm_i915_gem_object *); 45 1.7 riastrad 46 1.1 riastrad struct i915_vma * 47 1.1 riastrad i915_vma_instance(struct drm_i915_gem_object *obj, 48 1.1 riastrad struct i915_address_space *vm, 49 1.1 riastrad const struct i915_ggtt_view *view); 50 1.1 riastrad 51 1.1 riastrad void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags); 52 1.1 riastrad #define I915_VMA_RELEASE_MAP BIT(0) 53 1.1 riastrad 54 1.4 riastrad static inline bool i915_vma_is_active(struct i915_vma *vma) 55 1.1 riastrad { 56 1.1 riastrad return !i915_active_is_idle(&vma->active); 57 1.1 riastrad } 58 1.1 riastrad 59 1.1 riastrad int __must_check __i915_vma_move_to_active(struct i915_vma *vma, 60 1.1 riastrad struct i915_request *rq); 61 1.1 riastrad int __must_check i915_vma_move_to_active(struct i915_vma *vma, 62 1.1 riastrad struct i915_request *rq, 63 1.1 riastrad unsigned int flags); 64 1.1 riastrad 65 1.9 riastrad #ifdef __NetBSD__ 66 1.9 riastrad #define __i915_vma_flags(v) ((unsigned long *)&(v)->flags) 67 1.9 riastrad #define __i915_vma_flags_const(v) ((const unsigned long *)&(v)->flags) 68 1.9 riastrad #else 69 1.1 riastrad #define __i915_vma_flags(v) ((unsigned long *)&(v)->flags.counter) 70 1.6 riastrad #define __i915_vma_flags_const(v) ((const unsigned long *)&(v)->flags.counter) 71 1.4 riastrad #endif 72 1.4 riastrad 73 1.1 riastrad 74 1.6 riastrad static inline bool i915_vma_is_ggtt(const struct i915_vma *vma) 75 1.1 riastrad { 76 1.6 riastrad return test_bit(I915_VMA_GGTT_BIT, __i915_vma_flags_const(vma)); 77 1.1 riastrad } 78 1.1 riastrad 79 1.6 riastrad static inline bool i915_vma_has_ggtt_write(const struct i915_vma *vma) 80 1.1 riastrad { 81 1.6 riastrad return test_bit(I915_VMA_GGTT_WRITE_BIT, __i915_vma_flags_const(vma)); 82 1.1 riastrad } 83 1.1 riastrad 84 1.1 riastrad static inline void i915_vma_set_ggtt_write(struct i915_vma *vma) 85 1.1 riastrad { 86 1.1 riastrad GEM_BUG_ON(!i915_vma_is_ggtt(vma)); 87 1.1 riastrad set_bit(I915_VMA_GGTT_WRITE_BIT, __i915_vma_flags(vma)); 88 1.1 riastrad } 89 1.1 riastrad 90 1.1 riastrad static inline bool i915_vma_unset_ggtt_write(struct i915_vma *vma) 91 1.1 riastrad { 92 1.1 riastrad return test_and_clear_bit(I915_VMA_GGTT_WRITE_BIT, 93 1.1 riastrad __i915_vma_flags(vma)); 94 1.1 riastrad } 95 1.1 riastrad 96 1.1 riastrad void i915_vma_flush_writes(struct i915_vma *vma); 97 1.1 riastrad 98 1.6 riastrad static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma) 99 1.1 riastrad { 100 1.6 riastrad return test_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags_const(vma)); 101 1.1 riastrad } 102 1.1 riastrad 103 1.1 riastrad static inline bool i915_vma_set_userfault(struct i915_vma *vma) 104 1.1 riastrad { 105 1.1 riastrad GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma)); 106 1.1 riastrad return test_and_set_bit(I915_VMA_USERFAULT_BIT, __i915_vma_flags(vma)); 107 1.1 riastrad } 108 1.1 riastrad 109 1.1 riastrad static inline void i915_vma_unset_userfault(struct i915_vma *vma) 110 1.1 riastrad { 111 1.1 riastrad return clear_bit(I915_VMA_USERFAULT_BIT, __i915_vma_flags(vma)); 112 1.1 riastrad } 113 1.1 riastrad 114 1.6 riastrad static inline bool i915_vma_has_userfault(const struct i915_vma *vma) 115 1.1 riastrad { 116 1.6 riastrad return test_bit(I915_VMA_USERFAULT_BIT, __i915_vma_flags_const(vma)); 117 1.1 riastrad } 118 1.1 riastrad 119 1.6 riastrad static inline bool i915_vma_is_closed(const struct i915_vma *vma) 120 1.1 riastrad { 121 1.1 riastrad return !list_empty(&vma->closed_link); 122 1.1 riastrad } 123 1.1 riastrad 124 1.6 riastrad static inline u32 i915_ggtt_offset(const struct i915_vma *vma) 125 1.1 riastrad { 126 1.1 riastrad GEM_BUG_ON(!i915_vma_is_ggtt(vma)); 127 1.1 riastrad GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); 128 1.1 riastrad GEM_BUG_ON(upper_32_bits(vma->node.start)); 129 1.1 riastrad GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1)); 130 1.1 riastrad return lower_32_bits(vma->node.start); 131 1.1 riastrad } 132 1.1 riastrad 133 1.6 riastrad static inline u32 i915_ggtt_pin_bias(const struct i915_vma *vma) 134 1.1 riastrad { 135 1.1 riastrad return i915_vm_to_ggtt(vma->vm)->pin_bias; 136 1.1 riastrad } 137 1.1 riastrad 138 1.1 riastrad static inline struct i915_vma *i915_vma_get(struct i915_vma *vma) 139 1.1 riastrad { 140 1.1 riastrad i915_gem_object_get(vma->obj); 141 1.1 riastrad return vma; 142 1.1 riastrad } 143 1.1 riastrad 144 1.1 riastrad static inline struct i915_vma *i915_vma_tryget(struct i915_vma *vma) 145 1.1 riastrad { 146 1.1 riastrad if (likely(kref_get_unless_zero(&vma->obj->base.refcount))) 147 1.1 riastrad return vma; 148 1.1 riastrad 149 1.1 riastrad return NULL; 150 1.1 riastrad } 151 1.1 riastrad 152 1.1 riastrad static inline void i915_vma_put(struct i915_vma *vma) 153 1.1 riastrad { 154 1.1 riastrad i915_gem_object_put(vma->obj); 155 1.1 riastrad } 156 1.1 riastrad 157 1.1 riastrad static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b) 158 1.1 riastrad { 159 1.8 riastrad return a - b; 160 1.1 riastrad } 161 1.1 riastrad 162 1.1 riastrad static inline long 163 1.1 riastrad i915_vma_compare(struct i915_vma *vma, 164 1.1 riastrad struct i915_address_space *vm, 165 1.1 riastrad const struct i915_ggtt_view *view) 166 1.1 riastrad { 167 1.1 riastrad ptrdiff_t cmp; 168 1.1 riastrad 169 1.1 riastrad GEM_BUG_ON(view && !i915_is_ggtt(vm)); 170 1.1 riastrad 171 1.1 riastrad cmp = ptrdiff(vma->vm, vm); 172 1.1 riastrad if (cmp) 173 1.1 riastrad return cmp; 174 1.1 riastrad 175 1.1 riastrad BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0); 176 1.1 riastrad cmp = vma->ggtt_view.type; 177 1.1 riastrad if (!view) 178 1.1 riastrad return cmp; 179 1.1 riastrad 180 1.1 riastrad cmp -= view->type; 181 1.1 riastrad if (cmp) 182 1.1 riastrad return cmp; 183 1.1 riastrad 184 1.1 riastrad assert_i915_gem_gtt_types(); 185 1.1 riastrad 186 1.1 riastrad /* ggtt_view.type also encodes its size so that we both distinguish 187 1.1 riastrad * different views using it as a "type" and also use a compact (no 188 1.1 riastrad * accessing of uninitialised padding bytes) memcmp without storing 189 1.1 riastrad * an extra parameter or adding more code. 190 1.1 riastrad * 191 1.1 riastrad * To ensure that the memcmp is valid for all branches of the union, 192 1.1 riastrad * even though the code looks like it is just comparing one branch, 193 1.1 riastrad * we assert above that all branches have the same address, and that 194 1.1 riastrad * each branch has a unique type/size. 195 1.1 riastrad */ 196 1.1 riastrad BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL); 197 1.1 riastrad BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED); 198 1.1 riastrad BUILD_BUG_ON(I915_GGTT_VIEW_ROTATED >= I915_GGTT_VIEW_REMAPPED); 199 1.1 riastrad BUILD_BUG_ON(offsetof(typeof(*view), rotated) != 200 1.1 riastrad offsetof(typeof(*view), partial)); 201 1.1 riastrad BUILD_BUG_ON(offsetof(typeof(*view), rotated) != 202 1.1 riastrad offsetof(typeof(*view), remapped)); 203 1.1 riastrad return memcmp(&vma->ggtt_view.partial, &view->partial, view->type); 204 1.1 riastrad } 205 1.1 riastrad 206 1.1 riastrad struct i915_vma_work *i915_vma_work(void); 207 1.1 riastrad int i915_vma_bind(struct i915_vma *vma, 208 1.1 riastrad enum i915_cache_level cache_level, 209 1.1 riastrad u32 flags, 210 1.1 riastrad struct i915_vma_work *work); 211 1.1 riastrad 212 1.1 riastrad bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color); 213 1.7 riastrad bool i915_vma_misplaced(const struct i915_vma *vma, 214 1.1 riastrad u64 size, u64 alignment, u64 flags); 215 1.1 riastrad void __i915_vma_set_map_and_fenceable(struct i915_vma *vma); 216 1.1 riastrad void i915_vma_revoke_mmap(struct i915_vma *vma); 217 1.1 riastrad int __i915_vma_unbind(struct i915_vma *vma); 218 1.1 riastrad int __must_check i915_vma_unbind(struct i915_vma *vma); 219 1.1 riastrad void i915_vma_unlink_ctx(struct i915_vma *vma); 220 1.1 riastrad void i915_vma_close(struct i915_vma *vma); 221 1.1 riastrad void i915_vma_reopen(struct i915_vma *vma); 222 1.1 riastrad 223 1.1 riastrad static inline struct i915_vma *__i915_vma_get(struct i915_vma *vma) 224 1.1 riastrad { 225 1.1 riastrad if (kref_get_unless_zero(&vma->ref)) 226 1.1 riastrad return vma; 227 1.1 riastrad 228 1.1 riastrad return NULL; 229 1.1 riastrad } 230 1.1 riastrad 231 1.1 riastrad void i915_vma_release(struct kref *ref); 232 1.1 riastrad static inline void __i915_vma_put(struct i915_vma *vma) 233 1.1 riastrad { 234 1.1 riastrad kref_put(&vma->ref, i915_vma_release); 235 1.1 riastrad } 236 1.1 riastrad 237 1.1 riastrad #define assert_vma_held(vma) dma_resv_assert_held((vma)->resv) 238 1.1 riastrad 239 1.1 riastrad static inline void i915_vma_lock(struct i915_vma *vma) 240 1.1 riastrad { 241 1.1 riastrad dma_resv_lock(vma->resv, NULL); 242 1.1 riastrad } 243 1.1 riastrad 244 1.1 riastrad static inline void i915_vma_unlock(struct i915_vma *vma) 245 1.1 riastrad { 246 1.1 riastrad dma_resv_unlock(vma->resv); 247 1.1 riastrad } 248 1.1 riastrad 249 1.1 riastrad int __must_check 250 1.1 riastrad i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags); 251 1.1 riastrad int i915_ggtt_pin(struct i915_vma *vma, u32 align, unsigned int flags); 252 1.1 riastrad 253 1.4 riastrad static inline int i915_vma_pin_count(struct i915_vma *vma) 254 1.1 riastrad { 255 1.1 riastrad return atomic_read(&vma->flags) & I915_VMA_PIN_MASK; 256 1.1 riastrad } 257 1.1 riastrad 258 1.4 riastrad static inline bool i915_vma_is_pinned(struct i915_vma *vma) 259 1.1 riastrad { 260 1.1 riastrad return i915_vma_pin_count(vma); 261 1.1 riastrad } 262 1.1 riastrad 263 1.1 riastrad static inline void __i915_vma_pin(struct i915_vma *vma) 264 1.1 riastrad { 265 1.1 riastrad atomic_inc(&vma->flags); 266 1.1 riastrad GEM_BUG_ON(!i915_vma_is_pinned(vma)); 267 1.1 riastrad } 268 1.1 riastrad 269 1.1 riastrad static inline void __i915_vma_unpin(struct i915_vma *vma) 270 1.1 riastrad { 271 1.1 riastrad GEM_BUG_ON(!i915_vma_is_pinned(vma)); 272 1.1 riastrad atomic_dec(&vma->flags); 273 1.1 riastrad } 274 1.1 riastrad 275 1.1 riastrad static inline void i915_vma_unpin(struct i915_vma *vma) 276 1.1 riastrad { 277 1.1 riastrad GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); 278 1.1 riastrad __i915_vma_unpin(vma); 279 1.1 riastrad } 280 1.1 riastrad 281 1.4 riastrad static inline bool i915_vma_is_bound(struct i915_vma *vma, 282 1.1 riastrad unsigned int where) 283 1.1 riastrad { 284 1.1 riastrad return atomic_read(&vma->flags) & where; 285 1.1 riastrad } 286 1.1 riastrad 287 1.1 riastrad static inline bool i915_node_color_differs(const struct drm_mm_node *node, 288 1.1 riastrad unsigned long color) 289 1.1 riastrad { 290 1.1 riastrad return drm_mm_node_allocated(node) && node->color != color; 291 1.1 riastrad } 292 1.1 riastrad 293 1.1 riastrad /** 294 1.1 riastrad * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture 295 1.1 riastrad * @vma: VMA to iomap 296 1.1 riastrad * 297 1.1 riastrad * The passed in VMA has to be pinned in the global GTT mappable region. 298 1.1 riastrad * An extra pinning of the VMA is acquired for the return iomapping, 299 1.1 riastrad * the caller must call i915_vma_unpin_iomap to relinquish the pinning 300 1.1 riastrad * after the iomapping is no longer required. 301 1.1 riastrad * 302 1.1 riastrad * Returns a valid iomapped pointer or ERR_PTR. 303 1.1 riastrad */ 304 1.5 riastrad #ifdef __NetBSD__ 305 1.5 riastrad # define __iomem __i915_vma_iomem 306 1.5 riastrad #endif 307 1.1 riastrad void __iomem *i915_vma_pin_iomap(struct i915_vma *vma); 308 1.5 riastrad #ifdef __NetBSD__ 309 1.5 riastrad # undef __iomem 310 1.5 riastrad #endif 311 1.5 riastrad 312 1.1 riastrad #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x)) 313 1.1 riastrad 314 1.1 riastrad /** 315 1.1 riastrad * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap 316 1.1 riastrad * @vma: VMA to unpin 317 1.1 riastrad * 318 1.1 riastrad * Unpins the previously iomapped VMA from i915_vma_pin_iomap(). 319 1.1 riastrad * 320 1.1 riastrad * This function is only valid to be called on a VMA previously 321 1.1 riastrad * iomapped by the caller with i915_vma_pin_iomap(). 322 1.1 riastrad */ 323 1.1 riastrad void i915_vma_unpin_iomap(struct i915_vma *vma); 324 1.1 riastrad 325 1.1 riastrad static inline struct page *i915_vma_first_page(struct i915_vma *vma) 326 1.1 riastrad { 327 1.1 riastrad GEM_BUG_ON(!vma->pages); 328 1.3 riastrad #ifdef __NetBSD__ 329 1.6 riastrad return vma->pages->sgl->sg_pgs[0]; 330 1.3 riastrad #else 331 1.1 riastrad return sg_page(vma->pages->sgl); 332 1.3 riastrad #endif 333 1.1 riastrad } 334 1.1 riastrad 335 1.1 riastrad /** 336 1.1 riastrad * i915_vma_pin_fence - pin fencing state 337 1.1 riastrad * @vma: vma to pin fencing for 338 1.1 riastrad * 339 1.1 riastrad * This pins the fencing state (whether tiled or untiled) to make sure the 340 1.1 riastrad * vma (and its object) is ready to be used as a scanout target. Fencing 341 1.1 riastrad * status must be synchronize first by calling i915_vma_get_fence(): 342 1.1 riastrad * 343 1.1 riastrad * The resulting fence pin reference must be released again with 344 1.1 riastrad * i915_vma_unpin_fence(). 345 1.1 riastrad * 346 1.1 riastrad * Returns: 347 1.1 riastrad * 348 1.1 riastrad * True if the vma has a fence, false otherwise. 349 1.1 riastrad */ 350 1.1 riastrad int __must_check i915_vma_pin_fence(struct i915_vma *vma); 351 1.1 riastrad int __must_check i915_vma_revoke_fence(struct i915_vma *vma); 352 1.1 riastrad 353 1.1 riastrad int __i915_vma_pin_fence(struct i915_vma *vma); 354 1.1 riastrad 355 1.1 riastrad static inline void __i915_vma_unpin_fence(struct i915_vma *vma) 356 1.1 riastrad { 357 1.1 riastrad GEM_BUG_ON(atomic_read(&vma->fence->pin_count) <= 0); 358 1.1 riastrad atomic_dec(&vma->fence->pin_count); 359 1.1 riastrad } 360 1.1 riastrad 361 1.1 riastrad /** 362 1.1 riastrad * i915_vma_unpin_fence - unpin fencing state 363 1.1 riastrad * @vma: vma to unpin fencing for 364 1.1 riastrad * 365 1.1 riastrad * This releases the fence pin reference acquired through 366 1.1 riastrad * i915_vma_pin_fence. It will handle both objects with and without an 367 1.1 riastrad * attached fence correctly, callers do not need to distinguish this. 368 1.1 riastrad */ 369 1.1 riastrad static inline void 370 1.1 riastrad i915_vma_unpin_fence(struct i915_vma *vma) 371 1.1 riastrad { 372 1.1 riastrad if (vma->fence) 373 1.1 riastrad __i915_vma_unpin_fence(vma); 374 1.1 riastrad } 375 1.1 riastrad 376 1.1 riastrad void i915_vma_parked(struct intel_gt *gt); 377 1.1 riastrad 378 1.1 riastrad #define for_each_until(cond) if (cond) break; else 379 1.1 riastrad 380 1.1 riastrad /** 381 1.1 riastrad * for_each_ggtt_vma - Iterate over the GGTT VMA belonging to an object. 382 1.1 riastrad * @V: the #i915_vma iterator 383 1.1 riastrad * @OBJ: the #drm_i915_gem_object 384 1.1 riastrad * 385 1.1 riastrad * GGTT VMA are placed at the being of the object's vma_list, see 386 1.1 riastrad * vma_create(), so we can stop our walk as soon as we see a ppgtt VMA, 387 1.1 riastrad * or the list is empty ofc. 388 1.1 riastrad */ 389 1.1 riastrad #define for_each_ggtt_vma(V, OBJ) \ 390 1.1 riastrad list_for_each_entry(V, &(OBJ)->vma.list, obj_link) \ 391 1.1 riastrad for_each_until(!i915_vma_is_ggtt(V)) 392 1.1 riastrad 393 1.1 riastrad struct i915_vma *i915_vma_alloc(void); 394 1.1 riastrad void i915_vma_free(struct i915_vma *vma); 395 1.1 riastrad 396 1.1 riastrad struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma); 397 1.1 riastrad void i915_vma_make_shrinkable(struct i915_vma *vma); 398 1.1 riastrad void i915_vma_make_purgeable(struct i915_vma *vma); 399 1.1 riastrad 400 1.1 riastrad static inline int i915_vma_sync(struct i915_vma *vma) 401 1.1 riastrad { 402 1.1 riastrad /* Wait for the asynchronous bindings and pending GPU reads */ 403 1.1 riastrad return i915_active_wait(&vma->active); 404 1.1 riastrad } 405 1.1 riastrad 406 1.1 riastrad #endif 407