1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2016 Intel Corporation 5 */ 6 7 #ifndef __I915_GEM_OBJECT_H__ 8 #define __I915_GEM_OBJECT_H__ 9 10 #include <drm/drm_gem.h> 11 #include <drm/drm_file.h> 12 #include <drm/drm_device.h> 13 14 #include "display/intel_frontbuffer.h" 15 #include "intel_memory_region.h" 16 #include "i915_gem_object_types.h" 17 #include "i915_gem_gtt.h" 18 #include "i915_gem_ww.h" 19 #include "i915_vma_types.h" 20 21 enum intel_region_id; 22 23 /* 24 * XXX: There is a prevalence of the assumption that we fit the 25 * object's page count inside a 32bit _signed_ variable. Let's document 26 * this and catch if we ever need to fix it. In the meantime, if you do 27 * spot such a local variable, please consider fixing! 28 * 29 * Aside from our own locals (for which we have no excuse!): 30 * - sg_table embeds unsigned int for num_pages 31 * - get_user_pages*() mixed ints with longs 32 */ 33 #define GEM_CHECK_SIZE_OVERFLOW(sz) \ 34 GEM_WARN_ON((sz) >> PAGE_SHIFT > INT_MAX) 35 36 static inline bool i915_gem_object_size_2big(u64 size) 37 { 38 struct drm_i915_gem_object *obj; 39 40 if (GEM_CHECK_SIZE_OVERFLOW(size)) 41 return true; 42 43 if (overflows_type(size, obj->base.size)) 44 return true; 45 46 return false; 47 } 48 49 void i915_gem_init__objects(struct drm_i915_private *i915); 50 51 void i915_objects_module_exit(void); 52 int i915_objects_module_init(void); 53 54 struct drm_i915_gem_object *i915_gem_object_alloc(void); 55 void i915_gem_object_free(struct drm_i915_gem_object *obj); 56 57 void i915_gem_object_init(struct drm_i915_gem_object *obj, 58 const struct drm_i915_gem_object_ops *ops, 59 struct lock_class_key *key, 60 unsigned alloc_flags); 61 62 void __i915_gem_object_fini(struct drm_i915_gem_object *obj); 63 64 struct drm_i915_gem_object * 65 i915_gem_object_create_shmem(struct drm_i915_private *i915, 66 resource_size_t size); 67 struct drm_i915_gem_object * 68 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915, 69 const void *data, resource_size_t size); 70 struct drm_i915_gem_object * 71 __i915_gem_object_create_user(struct drm_i915_private *i915, u64 size, 72 struct intel_memory_region **placements, 73 unsigned int n_placements); 74 75 extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops; 76 77 void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, 78 struct sg_table *pages, 79 bool needs_clflush); 80 81 int i915_gem_object_pwrite_phys(struct drm_i915_gem_object *obj, 82 const struct drm_i915_gem_pwrite *args); 83 int i915_gem_object_pread_phys(struct drm_i915_gem_object *obj, 84 const struct drm_i915_gem_pread *args); 85 86 int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align); 87 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, 88 struct sg_table *pages); 89 void i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, 90 struct sg_table *pages); 91 92 void i915_gem_flush_free_objects(struct drm_i915_private *i915); 93 94 struct sg_table * 95 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj); 96 void i915_gem_object_truncate(struct drm_i915_gem_object *obj); 97 98 /** 99 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle 100 * @filp: DRM file private date 101 * @handle: userspace handle 102 * 103 * Returns: 104 * 105 * A pointer to the object named by the handle if such exists on @filp, NULL 106 * otherwise. This object is only valid whilst under the RCU read lock, and 107 * note carefully the object may be in the process of being destroyed. 108 */ 109 static inline struct drm_i915_gem_object * 110 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle) 111 { 112 #ifdef CONFIG_LOCKDEP 113 WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map)); 114 #endif 115 return idr_find(&file->object_idr, handle); 116 } 117 118 static inline struct drm_i915_gem_object * 119 i915_gem_object_get_rcu(struct drm_i915_gem_object *obj) 120 { 121 if (obj && !kref_get_unless_zero(&obj->base.refcount)) 122 obj = NULL; 123 124 return obj; 125 } 126 127 static inline struct drm_i915_gem_object * 128 i915_gem_object_lookup(struct drm_file *file, u32 handle) 129 { 130 struct drm_i915_gem_object *obj; 131 132 rcu_read_lock(); 133 obj = i915_gem_object_lookup_rcu(file, handle); 134 obj = i915_gem_object_get_rcu(obj); 135 rcu_read_unlock(); 136 137 return obj; 138 } 139 140 __deprecated 141 struct drm_gem_object * 142 drm_gem_object_lookup(struct drm_file *file, u32 handle); 143 144 __attribute__((nonnull)) 145 static inline struct drm_i915_gem_object * 146 i915_gem_object_get(struct drm_i915_gem_object *obj) 147 { 148 drm_gem_object_get(&obj->base); 149 return obj; 150 } 151 152 __attribute__((nonnull)) 153 static inline void 154 i915_gem_object_put(struct drm_i915_gem_object *obj) 155 { 156 __drm_gem_object_put(&obj->base); 157 } 158 159 #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv) 160 161 /* 162 * If more than one potential simultaneous locker, assert held. 163 */ 164 static inline void assert_object_held_shared(const struct drm_i915_gem_object *obj) 165 { 166 /* 167 * Note mm list lookup is protected by 168 * kref_get_unless_zero(). 169 */ 170 if (IS_ENABLED(CONFIG_LOCKDEP) && 171 kref_read(&obj->base.refcount) > 0) 172 assert_object_held(obj); 173 } 174 175 static inline int __i915_gem_object_lock(struct drm_i915_gem_object *obj, 176 struct i915_gem_ww_ctx *ww, 177 bool intr) 178 { 179 int ret; 180 181 if (intr) 182 ret = dma_resv_lock_interruptible(obj->base.resv, ww ? &ww->ctx : NULL); 183 else 184 ret = dma_resv_lock(obj->base.resv, ww ? &ww->ctx : NULL); 185 186 if (!ret && ww) { 187 i915_gem_object_get(obj); 188 list_add_tail(&obj->obj_link, &ww->obj_list); 189 } 190 if (ret == -EALREADY) 191 ret = 0; 192 193 if (ret == -EDEADLK) { 194 i915_gem_object_get(obj); 195 ww->contended = obj; 196 } 197 198 return ret; 199 } 200 201 static inline int i915_gem_object_lock(struct drm_i915_gem_object *obj, 202 struct i915_gem_ww_ctx *ww) 203 { 204 return __i915_gem_object_lock(obj, ww, ww && ww->intr); 205 } 206 207 static inline int i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj, 208 struct i915_gem_ww_ctx *ww) 209 { 210 WARN_ON(ww && !ww->intr); 211 return __i915_gem_object_lock(obj, ww, true); 212 } 213 214 static inline bool i915_gem_object_trylock(struct drm_i915_gem_object *obj) 215 { 216 return dma_resv_trylock(obj->base.resv); 217 } 218 219 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj) 220 { 221 if (obj->ops->adjust_lru) 222 obj->ops->adjust_lru(obj); 223 224 dma_resv_unlock(obj->base.resv); 225 } 226 227 static inline void 228 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj) 229 { 230 obj->flags |= I915_BO_READONLY; 231 } 232 233 static inline bool 234 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj) 235 { 236 return obj->flags & I915_BO_READONLY; 237 } 238 239 static inline bool 240 i915_gem_object_is_contiguous(const struct drm_i915_gem_object *obj) 241 { 242 return obj->flags & I915_BO_ALLOC_CONTIGUOUS; 243 } 244 245 static inline bool 246 i915_gem_object_is_volatile(const struct drm_i915_gem_object *obj) 247 { 248 return obj->flags & I915_BO_ALLOC_VOLATILE; 249 } 250 251 static inline void 252 i915_gem_object_set_volatile(struct drm_i915_gem_object *obj) 253 { 254 obj->flags |= I915_BO_ALLOC_VOLATILE; 255 } 256 257 static inline bool 258 i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object *obj) 259 { 260 return test_bit(I915_TILING_QUIRK_BIT, &obj->flags); 261 } 262 263 static inline void 264 i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object *obj) 265 { 266 set_bit(I915_TILING_QUIRK_BIT, &obj->flags); 267 } 268 269 static inline void 270 i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object *obj) 271 { 272 clear_bit(I915_TILING_QUIRK_BIT, &obj->flags); 273 } 274 275 static inline bool 276 i915_gem_object_is_protected(const struct drm_i915_gem_object *obj) 277 { 278 return obj->flags & I915_BO_PROTECTED; 279 } 280 281 static inline bool 282 i915_gem_object_type_has(const struct drm_i915_gem_object *obj, 283 unsigned long flags) 284 { 285 return obj->ops->flags & flags; 286 } 287 288 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj); 289 290 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj); 291 292 static inline bool 293 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj) 294 { 295 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_SHRINKABLE); 296 } 297 298 static inline bool 299 i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj) 300 { 301 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_PROXY); 302 } 303 304 static inline bool 305 i915_gem_object_never_mmap(const struct drm_i915_gem_object *obj) 306 { 307 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_NO_MMAP); 308 } 309 310 static inline bool 311 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj) 312 { 313 return READ_ONCE(obj->frontbuffer); 314 } 315 316 static inline unsigned int 317 i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj) 318 { 319 return obj->tiling_and_stride & TILING_MASK; 320 } 321 322 static inline bool 323 i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj) 324 { 325 return i915_gem_object_get_tiling(obj) != I915_TILING_NONE; 326 } 327 328 static inline unsigned int 329 i915_gem_object_get_stride(const struct drm_i915_gem_object *obj) 330 { 331 return obj->tiling_and_stride & STRIDE_MASK; 332 } 333 334 static inline unsigned int 335 i915_gem_tile_height(unsigned int tiling) 336 { 337 GEM_BUG_ON(!tiling); 338 return tiling == I915_TILING_Y ? 32 : 8; 339 } 340 341 static inline unsigned int 342 i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj) 343 { 344 return i915_gem_tile_height(i915_gem_object_get_tiling(obj)); 345 } 346 347 static inline unsigned int 348 i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj) 349 { 350 return (i915_gem_object_get_stride(obj) * 351 i915_gem_object_get_tile_height(obj)); 352 } 353 354 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj, 355 unsigned int tiling, unsigned int stride); 356 357 struct scatterlist * 358 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj, 359 struct i915_gem_object_page_iter *iter, 360 unsigned int n, 361 unsigned int *offset, bool dma); 362 363 static inline struct scatterlist * 364 i915_gem_object_get_sg(struct drm_i915_gem_object *obj, 365 unsigned int n, 366 unsigned int *offset) 367 { 368 return __i915_gem_object_get_sg(obj, &obj->mm.get_page, n, offset, false); 369 } 370 371 static inline struct scatterlist * 372 i915_gem_object_get_sg_dma(struct drm_i915_gem_object *obj, 373 unsigned int n, 374 unsigned int *offset) 375 { 376 return __i915_gem_object_get_sg(obj, &obj->mm.get_dma_page, n, offset, true); 377 } 378 379 struct page * 380 i915_gem_object_get_page(struct drm_i915_gem_object *obj, 381 unsigned int n); 382 383 struct page * 384 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, 385 unsigned int n); 386 387 dma_addr_t 388 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj, 389 unsigned long n, 390 unsigned int *len); 391 392 dma_addr_t 393 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, 394 unsigned long n); 395 396 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, 397 struct sg_table *pages, 398 unsigned int sg_page_sizes); 399 400 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj); 401 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj); 402 403 static inline int __must_check 404 i915_gem_object_pin_pages(struct drm_i915_gem_object *obj) 405 { 406 assert_object_held(obj); 407 408 if (atomic_inc_not_zero(&obj->mm.pages_pin_count)) 409 return 0; 410 411 return __i915_gem_object_get_pages(obj); 412 } 413 414 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj); 415 416 static inline bool 417 i915_gem_object_has_pages(struct drm_i915_gem_object *obj) 418 { 419 return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages)); 420 } 421 422 static inline void 423 __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj) 424 { 425 GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 426 427 atomic_inc(&obj->mm.pages_pin_count); 428 } 429 430 static inline bool 431 i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj) 432 { 433 return atomic_read(&obj->mm.pages_pin_count); 434 } 435 436 static inline void 437 __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj) 438 { 439 GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 440 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 441 442 atomic_dec(&obj->mm.pages_pin_count); 443 } 444 445 static inline void 446 i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj) 447 { 448 __i915_gem_object_unpin_pages(obj); 449 } 450 451 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj); 452 void i915_gem_object_truncate(struct drm_i915_gem_object *obj); 453 void i915_gem_object_writeback(struct drm_i915_gem_object *obj); 454 455 /** 456 * i915_gem_object_pin_map - return a contiguous mapping of the entire object 457 * @obj: the object to map into kernel address space 458 * @type: the type of mapping, used to select pgprot_t 459 * 460 * Calls i915_gem_object_pin_pages() to prevent reaping of the object's 461 * pages and then returns a contiguous mapping of the backing storage into 462 * the kernel address space. Based on the @type of mapping, the PTE will be 463 * set to either WriteBack or WriteCombine (via pgprot_t). 464 * 465 * The caller is responsible for calling i915_gem_object_unpin_map() when the 466 * mapping is no longer required. 467 * 468 * Returns the pointer through which to access the mapped object, or an 469 * ERR_PTR() on error. 470 */ 471 void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj, 472 enum i915_map_type type); 473 474 void *__must_check i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj, 475 enum i915_map_type type); 476 477 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj, 478 unsigned long offset, 479 unsigned long size); 480 static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj) 481 { 482 __i915_gem_object_flush_map(obj, 0, obj->base.size); 483 } 484 485 /** 486 * i915_gem_object_unpin_map - releases an earlier mapping 487 * @obj: the object to unmap 488 * 489 * After pinning the object and mapping its pages, once you are finished 490 * with your access, call i915_gem_object_unpin_map() to release the pin 491 * upon the mapping. Once the pin count reaches zero, that mapping may be 492 * removed. 493 */ 494 static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj) 495 { 496 i915_gem_object_unpin_pages(obj); 497 } 498 499 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj); 500 501 int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj, 502 unsigned int *needs_clflush); 503 int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj, 504 unsigned int *needs_clflush); 505 #define CLFLUSH_BEFORE BIT(0) 506 #define CLFLUSH_AFTER BIT(1) 507 #define CLFLUSH_FLAGS (CLFLUSH_BEFORE | CLFLUSH_AFTER) 508 509 static inline void 510 i915_gem_object_finish_access(struct drm_i915_gem_object *obj) 511 { 512 i915_gem_object_unpin_pages(obj); 513 } 514 515 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj, 516 unsigned int cache_level); 517 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj); 518 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj); 519 void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj); 520 521 int __must_check 522 i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write); 523 int __must_check 524 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write); 525 int __must_check 526 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write); 527 struct i915_vma * __must_check 528 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, 529 struct i915_gem_ww_ctx *ww, 530 u32 alignment, 531 const struct i915_ggtt_view *view, 532 unsigned int flags); 533 534 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj); 535 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj); 536 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj); 537 538 static inline bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) 539 { 540 if (obj->cache_dirty) 541 return false; 542 543 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) 544 return true; 545 546 /* Currently in use by HW (display engine)? Keep flushed. */ 547 return i915_gem_object_is_framebuffer(obj); 548 } 549 550 static inline void __start_cpu_write(struct drm_i915_gem_object *obj) 551 { 552 obj->read_domains = I915_GEM_DOMAIN_CPU; 553 obj->write_domain = I915_GEM_DOMAIN_CPU; 554 if (cpu_write_needs_clflush(obj)) 555 obj->cache_dirty = true; 556 } 557 558 void i915_gem_fence_wait_priority(struct dma_fence *fence, 559 const struct i915_sched_attr *attr); 560 561 int i915_gem_object_wait(struct drm_i915_gem_object *obj, 562 unsigned int flags, 563 long timeout); 564 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, 565 unsigned int flags, 566 const struct i915_sched_attr *attr); 567 568 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj, 569 enum fb_op_origin origin); 570 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, 571 enum fb_op_origin origin); 572 573 static inline void 574 i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj, 575 enum fb_op_origin origin) 576 { 577 if (unlikely(rcu_access_pointer(obj->frontbuffer))) 578 __i915_gem_object_flush_frontbuffer(obj, origin); 579 } 580 581 static inline void 582 i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, 583 enum fb_op_origin origin) 584 { 585 if (unlikely(rcu_access_pointer(obj->frontbuffer))) 586 __i915_gem_object_invalidate_frontbuffer(obj, origin); 587 } 588 589 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size); 590 591 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj); 592 593 void __i915_gem_free_object_rcu(struct rcu_head *head); 594 595 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj); 596 597 void __i915_gem_free_object(struct drm_i915_gem_object *obj); 598 599 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj); 600 601 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj); 602 603 int i915_gem_object_migrate(struct drm_i915_gem_object *obj, 604 struct i915_gem_ww_ctx *ww, 605 enum intel_region_id id); 606 607 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj, 608 enum intel_region_id id); 609 610 int i915_gem_object_wait_migration(struct drm_i915_gem_object *obj, 611 unsigned int flags); 612 613 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj, 614 enum intel_memory_type type); 615 616 #ifdef CONFIG_MMU_NOTIFIER 617 static inline bool 618 i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) 619 { 620 return obj->userptr.notifier.mm; 621 } 622 623 int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj); 624 int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj); 625 int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj); 626 #else 627 static inline bool i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) { return false; } 628 629 static inline int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; } 630 static inline int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; } 631 static inline int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; } 632 633 #endif 634 635 #endif 636