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