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 * We can check for invalidly typed locals with typecheck(), see for example 30 * i915_gem_object_get_sg(). 31 */ 32 #define GEM_CHECK_SIZE_OVERFLOW(sz) \ 33 GEM_WARN_ON((sz) >> PAGE_SHIFT > INT_MAX) 34 35 static inline bool i915_gem_object_size_2big(u64 size) 36 { 37 struct drm_i915_gem_object *obj; 38 39 if (GEM_CHECK_SIZE_OVERFLOW(size)) 40 return true; 41 42 if (overflows_type(size, obj->base.size)) 43 return true; 44 45 return false; 46 } 47 48 void i915_gem_init__objects(struct drm_i915_private *i915); 49 50 void i915_objects_module_exit(void); 51 int i915_objects_module_init(void); 52 53 struct drm_i915_gem_object *i915_gem_object_alloc(void); 54 void i915_gem_object_free(struct drm_i915_gem_object *obj); 55 56 void i915_gem_object_init(struct drm_i915_gem_object *obj, 57 const struct drm_i915_gem_object_ops *ops, 58 struct lock_class_key *key, 59 unsigned alloc_flags); 60 61 void __i915_gem_object_fini(struct drm_i915_gem_object *obj); 62 63 struct drm_i915_gem_object * 64 i915_gem_object_create_shmem(struct drm_i915_private *i915, 65 resource_size_t size); 66 struct drm_i915_gem_object * 67 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915, 68 const void *data, resource_size_t size); 69 struct drm_i915_gem_object * 70 __i915_gem_object_create_user(struct drm_i915_private *i915, u64 size, 71 struct intel_memory_region **placements, 72 unsigned int n_placements); 73 74 extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops; 75 76 void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, 77 struct sg_table *pages, 78 bool needs_clflush); 79 80 int i915_gem_object_pwrite_phys(struct drm_i915_gem_object *obj, 81 const struct drm_i915_gem_pwrite *args); 82 int i915_gem_object_pread_phys(struct drm_i915_gem_object *obj, 83 const struct drm_i915_gem_pread *args); 84 85 int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align); 86 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, 87 struct sg_table *pages); 88 void i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, 89 struct sg_table *pages); 90 91 void i915_gem_flush_free_objects(struct drm_i915_private *i915); 92 93 struct sg_table * 94 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj); 95 96 /** 97 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle 98 * @filp: DRM file private date 99 * @handle: userspace handle 100 * 101 * Returns: 102 * 103 * A pointer to the object named by the handle if such exists on @filp, NULL 104 * otherwise. This object is only valid whilst under the RCU read lock, and 105 * note carefully the object may be in the process of being destroyed. 106 */ 107 static inline struct drm_i915_gem_object * 108 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle) 109 { 110 #ifdef CONFIG_LOCKDEP 111 WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map)); 112 #endif 113 return idr_find(&file->object_idr, handle); 114 } 115 116 static inline struct drm_i915_gem_object * 117 i915_gem_object_get_rcu(struct drm_i915_gem_object *obj) 118 { 119 if (obj && !kref_get_unless_zero(&obj->base.refcount)) 120 obj = NULL; 121 122 return obj; 123 } 124 125 static inline struct drm_i915_gem_object * 126 i915_gem_object_lookup(struct drm_file *file, u32 handle) 127 { 128 struct drm_i915_gem_object *obj; 129 130 rcu_read_lock(); 131 obj = i915_gem_object_lookup_rcu(file, handle); 132 obj = i915_gem_object_get_rcu(obj); 133 rcu_read_unlock(); 134 135 return obj; 136 } 137 138 __deprecated 139 struct drm_gem_object * 140 drm_gem_object_lookup(struct drm_file *file, u32 handle); 141 142 __attribute__((nonnull)) 143 static inline struct drm_i915_gem_object * 144 i915_gem_object_get(struct drm_i915_gem_object *obj) 145 { 146 drm_gem_object_get(&obj->base); 147 return obj; 148 } 149 150 __attribute__((nonnull)) 151 static inline void 152 i915_gem_object_put(struct drm_i915_gem_object *obj) 153 { 154 __drm_gem_object_put(&obj->base); 155 } 156 157 #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv) 158 159 /* 160 * If more than one potential simultaneous locker, assert held. 161 */ 162 static inline void assert_object_held_shared(const struct drm_i915_gem_object *obj) 163 { 164 /* 165 * Note mm list lookup is protected by 166 * kref_get_unless_zero(). 167 */ 168 if (IS_ENABLED(CONFIG_LOCKDEP) && 169 kref_read(&obj->base.refcount) > 0) 170 assert_object_held(obj); 171 } 172 173 static inline int __i915_gem_object_lock(struct drm_i915_gem_object *obj, 174 struct i915_gem_ww_ctx *ww, 175 bool intr) 176 { 177 int ret; 178 179 if (intr) 180 ret = dma_resv_lock_interruptible(obj->base.resv, ww ? &ww->ctx : NULL); 181 else 182 ret = dma_resv_lock(obj->base.resv, ww ? &ww->ctx : NULL); 183 184 if (!ret && ww) { 185 i915_gem_object_get(obj); 186 list_add_tail(&obj->obj_link, &ww->obj_list); 187 } 188 if (ret == -EALREADY) 189 ret = 0; 190 191 if (ret == -EDEADLK) { 192 i915_gem_object_get(obj); 193 ww->contended = obj; 194 } 195 196 return ret; 197 } 198 199 static inline int i915_gem_object_lock(struct drm_i915_gem_object *obj, 200 struct i915_gem_ww_ctx *ww) 201 { 202 return __i915_gem_object_lock(obj, ww, ww && ww->intr); 203 } 204 205 static inline int i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj, 206 struct i915_gem_ww_ctx *ww) 207 { 208 WARN_ON(ww && !ww->intr); 209 return __i915_gem_object_lock(obj, ww, true); 210 } 211 212 static inline bool i915_gem_object_trylock(struct drm_i915_gem_object *obj, 213 struct i915_gem_ww_ctx *ww) 214 { 215 if (!ww) 216 return dma_resv_trylock(obj->base.resv); 217 else 218 return ww_mutex_trylock(&obj->base.resv->lock, &ww->ctx); 219 } 220 221 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj) 222 { 223 if (obj->ops->adjust_lru) 224 obj->ops->adjust_lru(obj); 225 226 dma_resv_unlock(obj->base.resv); 227 } 228 229 static inline void 230 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj) 231 { 232 obj->flags |= I915_BO_READONLY; 233 } 234 235 static inline bool 236 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj) 237 { 238 return obj->flags & I915_BO_READONLY; 239 } 240 241 static inline bool 242 i915_gem_object_is_contiguous(const struct drm_i915_gem_object *obj) 243 { 244 return obj->flags & I915_BO_ALLOC_CONTIGUOUS; 245 } 246 247 static inline bool 248 i915_gem_object_is_volatile(const struct drm_i915_gem_object *obj) 249 { 250 return obj->flags & I915_BO_ALLOC_VOLATILE; 251 } 252 253 static inline void 254 i915_gem_object_set_volatile(struct drm_i915_gem_object *obj) 255 { 256 obj->flags |= I915_BO_ALLOC_VOLATILE; 257 } 258 259 static inline bool 260 i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object *obj) 261 { 262 return test_bit(I915_TILING_QUIRK_BIT, &obj->flags); 263 } 264 265 static inline void 266 i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object *obj) 267 { 268 set_bit(I915_TILING_QUIRK_BIT, &obj->flags); 269 } 270 271 static inline void 272 i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object *obj) 273 { 274 clear_bit(I915_TILING_QUIRK_BIT, &obj->flags); 275 } 276 277 static inline bool 278 i915_gem_object_is_protected(const struct drm_i915_gem_object *obj) 279 { 280 return obj->flags & I915_BO_PROTECTED; 281 } 282 283 static inline bool 284 i915_gem_object_type_has(const struct drm_i915_gem_object *obj, 285 unsigned long flags) 286 { 287 return obj->ops->flags & flags; 288 } 289 290 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj); 291 292 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj); 293 294 static inline bool 295 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj) 296 { 297 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_SHRINKABLE); 298 } 299 300 static inline bool 301 i915_gem_object_has_self_managed_shrink_list(const struct drm_i915_gem_object *obj) 302 { 303 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST); 304 } 305 306 static inline bool 307 i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj) 308 { 309 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_PROXY); 310 } 311 312 static inline bool 313 i915_gem_object_never_mmap(const struct drm_i915_gem_object *obj) 314 { 315 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_NO_MMAP); 316 } 317 318 static inline bool 319 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj) 320 { 321 return READ_ONCE(obj->frontbuffer); 322 } 323 324 static inline unsigned int 325 i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj) 326 { 327 return obj->tiling_and_stride & TILING_MASK; 328 } 329 330 static inline bool 331 i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj) 332 { 333 return i915_gem_object_get_tiling(obj) != I915_TILING_NONE; 334 } 335 336 static inline unsigned int 337 i915_gem_object_get_stride(const struct drm_i915_gem_object *obj) 338 { 339 return obj->tiling_and_stride & STRIDE_MASK; 340 } 341 342 static inline unsigned int 343 i915_gem_tile_height(unsigned int tiling) 344 { 345 GEM_BUG_ON(!tiling); 346 return tiling == I915_TILING_Y ? 32 : 8; 347 } 348 349 static inline unsigned int 350 i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj) 351 { 352 return i915_gem_tile_height(i915_gem_object_get_tiling(obj)); 353 } 354 355 static inline unsigned int 356 i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj) 357 { 358 return (i915_gem_object_get_stride(obj) * 359 i915_gem_object_get_tile_height(obj)); 360 } 361 362 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj, 363 unsigned int tiling, unsigned int stride); 364 365 /** 366 * __i915_gem_object_page_iter_get_sg - helper to find the target scatterlist 367 * pointer and the target page position using pgoff_t n input argument and 368 * i915_gem_object_page_iter 369 * @obj: i915 GEM buffer object 370 * @iter: i915 GEM buffer object page iterator 371 * @n: page offset 372 * @offset: searched physical offset, 373 * it will be used for returning physical page offset value 374 * 375 * Context: Takes and releases the mutex lock of the i915_gem_object_page_iter. 376 * Takes and releases the RCU lock to search the radix_tree of 377 * i915_gem_object_page_iter. 378 * 379 * Returns: 380 * The target scatterlist pointer and the target page position. 381 * 382 * Recommended to use wrapper macro: i915_gem_object_page_iter_get_sg() 383 */ 384 struct scatterlist * 385 __i915_gem_object_page_iter_get_sg(struct drm_i915_gem_object *obj, 386 struct i915_gem_object_page_iter *iter, 387 pgoff_t n, 388 unsigned int *offset); 389 390 /** 391 * i915_gem_object_page_iter_get_sg - wrapper macro for 392 * __i915_gem_object_page_iter_get_sg() 393 * @obj: i915 GEM buffer object 394 * @it: i915 GEM buffer object page iterator 395 * @n: page offset 396 * @offset: searched physical offset, 397 * it will be used for returning physical page offset value 398 * 399 * Context: Takes and releases the mutex lock of the i915_gem_object_page_iter. 400 * Takes and releases the RCU lock to search the radix_tree of 401 * i915_gem_object_page_iter. 402 * 403 * Returns: 404 * The target scatterlist pointer and the target page position. 405 * 406 * In order to avoid the truncation of the input parameter, it checks the page 407 * offset n's type from the input parameter before calling 408 * __i915_gem_object_page_iter_get_sg(). 409 */ 410 #define i915_gem_object_page_iter_get_sg(obj, it, n, offset) ({ \ 411 static_assert(castable_to_type(n, pgoff_t)); \ 412 __i915_gem_object_page_iter_get_sg(obj, it, n, offset); \ 413 }) 414 415 /** 416 * __i915_gem_object_get_sg - helper to find the target scatterlist 417 * pointer and the target page position using pgoff_t n input argument and 418 * drm_i915_gem_object. It uses an internal shmem scatterlist lookup function. 419 * @obj: i915 GEM buffer object 420 * @n: page offset 421 * @offset: searched physical offset, 422 * it will be used for returning physical page offset value 423 * 424 * It uses drm_i915_gem_object's internal shmem scatterlist lookup function as 425 * i915_gem_object_page_iter and calls __i915_gem_object_page_iter_get_sg(). 426 * 427 * Returns: 428 * The target scatterlist pointer and the target page position. 429 * 430 * Recommended to use wrapper macro: i915_gem_object_get_sg() 431 * See also __i915_gem_object_page_iter_get_sg() 432 */ 433 static inline struct scatterlist * 434 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj, pgoff_t n, 435 unsigned int *offset) 436 { 437 return __i915_gem_object_page_iter_get_sg(obj, &obj->mm.get_page, n, offset); 438 } 439 440 /** 441 * i915_gem_object_get_sg - wrapper macro for __i915_gem_object_get_sg() 442 * @obj: i915 GEM buffer object 443 * @n: page offset 444 * @offset: searched physical offset, 445 * it will be used for returning physical page offset value 446 * 447 * Returns: 448 * The target scatterlist pointer and the target page position. 449 * 450 * In order to avoid the truncation of the input parameter, it checks the page 451 * offset n's type from the input parameter before calling 452 * __i915_gem_object_get_sg(). 453 * See also __i915_gem_object_page_iter_get_sg() 454 */ 455 #define i915_gem_object_get_sg(obj, n, offset) ({ \ 456 static_assert(castable_to_type(n, pgoff_t)); \ 457 __i915_gem_object_get_sg(obj, n, offset); \ 458 }) 459 460 /** 461 * __i915_gem_object_get_sg_dma - helper to find the target scatterlist 462 * pointer and the target page position using pgoff_t n input argument and 463 * drm_i915_gem_object. It uses an internal DMA mapped scatterlist lookup function 464 * @obj: i915 GEM buffer object 465 * @n: page offset 466 * @offset: searched physical offset, 467 * it will be used for returning physical page offset value 468 * 469 * It uses drm_i915_gem_object's internal DMA mapped scatterlist lookup function 470 * as i915_gem_object_page_iter and calls __i915_gem_object_page_iter_get_sg(). 471 * 472 * Returns: 473 * The target scatterlist pointer and the target page position. 474 * 475 * Recommended to use wrapper macro: i915_gem_object_get_sg_dma() 476 * See also __i915_gem_object_page_iter_get_sg() 477 */ 478 static inline struct scatterlist * 479 __i915_gem_object_get_sg_dma(struct drm_i915_gem_object *obj, pgoff_t n, 480 unsigned int *offset) 481 { 482 return __i915_gem_object_page_iter_get_sg(obj, &obj->mm.get_dma_page, n, offset); 483 } 484 485 /** 486 * i915_gem_object_get_sg_dma - wrapper macro for __i915_gem_object_get_sg_dma() 487 * @obj: i915 GEM buffer object 488 * @n: page offset 489 * @offset: searched physical offset, 490 * it will be used for returning physical page offset value 491 * 492 * Returns: 493 * The target scatterlist pointer and the target page position. 494 * 495 * In order to avoid the truncation of the input parameter, it checks the page 496 * offset n's type from the input parameter before calling 497 * __i915_gem_object_get_sg_dma(). 498 * See also __i915_gem_object_page_iter_get_sg() 499 */ 500 #define i915_gem_object_get_sg_dma(obj, n, offset) ({ \ 501 static_assert(castable_to_type(n, pgoff_t)); \ 502 __i915_gem_object_get_sg_dma(obj, n, offset); \ 503 }) 504 505 /** 506 * __i915_gem_object_get_page - helper to find the target page with a page offset 507 * @obj: i915 GEM buffer object 508 * @n: page offset 509 * 510 * It uses drm_i915_gem_object's internal shmem scatterlist lookup function as 511 * i915_gem_object_page_iter and calls __i915_gem_object_page_iter_get_sg() 512 * internally. 513 * 514 * Returns: 515 * The target page pointer. 516 * 517 * Recommended to use wrapper macro: i915_gem_object_get_page() 518 * See also __i915_gem_object_page_iter_get_sg() 519 */ 520 struct page * 521 __i915_gem_object_get_page(struct drm_i915_gem_object *obj, pgoff_t n); 522 523 /** 524 * i915_gem_object_get_page - wrapper macro for __i915_gem_object_get_page 525 * @obj: i915 GEM buffer object 526 * @n: page offset 527 * 528 * Returns: 529 * The target page pointer. 530 * 531 * In order to avoid the truncation of the input parameter, it checks the page 532 * offset n's type from the input parameter before calling 533 * __i915_gem_object_get_page(). 534 * See also __i915_gem_object_page_iter_get_sg() 535 */ 536 #define i915_gem_object_get_page(obj, n) ({ \ 537 static_assert(castable_to_type(n, pgoff_t)); \ 538 __i915_gem_object_get_page(obj, n); \ 539 }) 540 541 /** 542 * __i915_gem_object_get_dirty_page - helper to find the target page with a page 543 * offset 544 * @obj: i915 GEM buffer object 545 * @n: page offset 546 * 547 * It works like i915_gem_object_get_page(), but it marks the returned page dirty. 548 * 549 * Returns: 550 * The target page pointer. 551 * 552 * Recommended to use wrapper macro: i915_gem_object_get_dirty_page() 553 * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_page() 554 */ 555 struct page * 556 __i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, pgoff_t n); 557 558 /** 559 * i915_gem_object_get_dirty_page - wrapper macro for __i915_gem_object_get_dirty_page 560 * @obj: i915 GEM buffer object 561 * @n: page offset 562 * 563 * Returns: 564 * The target page pointer. 565 * 566 * In order to avoid the truncation of the input parameter, it checks the page 567 * offset n's type from the input parameter before calling 568 * __i915_gem_object_get_dirty_page(). 569 * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_page() 570 */ 571 #define i915_gem_object_get_dirty_page(obj, n) ({ \ 572 static_assert(castable_to_type(n, pgoff_t)); \ 573 __i915_gem_object_get_dirty_page(obj, n); \ 574 }) 575 576 /** 577 * __i915_gem_object_get_dma_address_len - helper to get bus addresses of 578 * targeted DMA mapped scatterlist from i915 GEM buffer object and it's length 579 * @obj: i915 GEM buffer object 580 * @n: page offset 581 * @len: DMA mapped scatterlist's DMA bus addresses length to return 582 * 583 * Returns: 584 * Bus addresses of targeted DMA mapped scatterlist 585 * 586 * Recommended to use wrapper macro: i915_gem_object_get_dma_address_len() 587 * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_sg_dma() 588 */ 589 dma_addr_t 590 __i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj, pgoff_t n, 591 unsigned int *len); 592 593 /** 594 * i915_gem_object_get_dma_address_len - wrapper macro for 595 * __i915_gem_object_get_dma_address_len 596 * @obj: i915 GEM buffer object 597 * @n: page offset 598 * @len: DMA mapped scatterlist's DMA bus addresses length to return 599 * 600 * Returns: 601 * Bus addresses of targeted DMA mapped scatterlist 602 * 603 * In order to avoid the truncation of the input parameter, it checks the page 604 * offset n's type from the input parameter before calling 605 * __i915_gem_object_get_dma_address_len(). 606 * See also __i915_gem_object_page_iter_get_sg() and 607 * __i915_gem_object_get_dma_address_len() 608 */ 609 #define i915_gem_object_get_dma_address_len(obj, n, len) ({ \ 610 static_assert(castable_to_type(n, pgoff_t)); \ 611 __i915_gem_object_get_dma_address_len(obj, n, len); \ 612 }) 613 614 /** 615 * __i915_gem_object_get_dma_address - helper to get bus addresses of 616 * targeted DMA mapped scatterlist from i915 GEM buffer object 617 * @obj: i915 GEM buffer object 618 * @n: page offset 619 * 620 * Returns: 621 * Bus addresses of targeted DMA mapped scatterlis 622 * 623 * Recommended to use wrapper macro: i915_gem_object_get_dma_address() 624 * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_sg_dma() 625 */ 626 dma_addr_t 627 __i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, pgoff_t n); 628 629 /** 630 * i915_gem_object_get_dma_address - wrapper macro for 631 * __i915_gem_object_get_dma_address 632 * @obj: i915 GEM buffer object 633 * @n: page offset 634 * 635 * Returns: 636 * Bus addresses of targeted DMA mapped scatterlist 637 * 638 * In order to avoid the truncation of the input parameter, it checks the page 639 * offset n's type from the input parameter before calling 640 * __i915_gem_object_get_dma_address(). 641 * See also __i915_gem_object_page_iter_get_sg() and 642 * __i915_gem_object_get_dma_address() 643 */ 644 #define i915_gem_object_get_dma_address(obj, n) ({ \ 645 static_assert(castable_to_type(n, pgoff_t)); \ 646 __i915_gem_object_get_dma_address(obj, n); \ 647 }) 648 649 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, 650 struct sg_table *pages); 651 652 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj); 653 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj); 654 655 static inline int __must_check 656 i915_gem_object_pin_pages(struct drm_i915_gem_object *obj) 657 { 658 assert_object_held(obj); 659 660 if (atomic_inc_not_zero(&obj->mm.pages_pin_count)) 661 return 0; 662 663 return __i915_gem_object_get_pages(obj); 664 } 665 666 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj); 667 668 static inline bool 669 i915_gem_object_has_pages(struct drm_i915_gem_object *obj) 670 { 671 return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages)); 672 } 673 674 static inline void 675 __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj) 676 { 677 GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 678 679 atomic_inc(&obj->mm.pages_pin_count); 680 } 681 682 static inline bool 683 i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj) 684 { 685 return atomic_read(&obj->mm.pages_pin_count); 686 } 687 688 static inline void 689 __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj) 690 { 691 GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 692 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 693 694 atomic_dec(&obj->mm.pages_pin_count); 695 } 696 697 static inline void 698 i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj) 699 { 700 __i915_gem_object_unpin_pages(obj); 701 } 702 703 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj); 704 int i915_gem_object_truncate(struct drm_i915_gem_object *obj); 705 706 /** 707 * i915_gem_object_pin_map - return a contiguous mapping of the entire object 708 * @obj: the object to map into kernel address space 709 * @type: the type of mapping, used to select pgprot_t 710 * 711 * Calls i915_gem_object_pin_pages() to prevent reaping of the object's 712 * pages and then returns a contiguous mapping of the backing storage into 713 * the kernel address space. Based on the @type of mapping, the PTE will be 714 * set to either WriteBack or WriteCombine (via pgprot_t). 715 * 716 * The caller is responsible for calling i915_gem_object_unpin_map() when the 717 * mapping is no longer required. 718 * 719 * Returns the pointer through which to access the mapped object, or an 720 * ERR_PTR() on error. 721 */ 722 void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj, 723 enum i915_map_type type); 724 725 void *__must_check i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj, 726 enum i915_map_type type); 727 728 enum i915_map_type i915_coherent_map_type(struct drm_i915_private *i915, 729 struct drm_i915_gem_object *obj, 730 bool always_coherent); 731 732 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj, 733 unsigned long offset, 734 unsigned long size); 735 static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj) 736 { 737 __i915_gem_object_flush_map(obj, 0, obj->base.size); 738 } 739 740 /** 741 * i915_gem_object_unpin_map - releases an earlier mapping 742 * @obj: the object to unmap 743 * 744 * After pinning the object and mapping its pages, once you are finished 745 * with your access, call i915_gem_object_unpin_map() to release the pin 746 * upon the mapping. Once the pin count reaches zero, that mapping may be 747 * removed. 748 */ 749 static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj) 750 { 751 i915_gem_object_unpin_pages(obj); 752 } 753 754 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj); 755 756 int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj, 757 unsigned int *needs_clflush); 758 int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj, 759 unsigned int *needs_clflush); 760 #define CLFLUSH_BEFORE BIT(0) 761 #define CLFLUSH_AFTER BIT(1) 762 #define CLFLUSH_FLAGS (CLFLUSH_BEFORE | CLFLUSH_AFTER) 763 764 static inline void 765 i915_gem_object_finish_access(struct drm_i915_gem_object *obj) 766 { 767 i915_gem_object_unpin_pages(obj); 768 } 769 770 int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj, 771 struct dma_fence **fence); 772 int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj, 773 bool intr); 774 bool i915_gem_object_has_unknown_state(struct drm_i915_gem_object *obj); 775 776 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj, 777 unsigned int cache_level); 778 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj); 779 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj); 780 void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj); 781 bool i915_gem_cpu_write_needs_clflush(struct drm_i915_gem_object *obj); 782 783 int __must_check 784 i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write); 785 int __must_check 786 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write); 787 int __must_check 788 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write); 789 struct i915_vma * __must_check 790 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, 791 struct i915_gem_ww_ctx *ww, 792 u32 alignment, 793 const struct i915_gtt_view *view, 794 unsigned int flags); 795 796 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj); 797 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj); 798 void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj); 799 void __i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj); 800 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj); 801 802 static inline void __start_cpu_write(struct drm_i915_gem_object *obj) 803 { 804 obj->read_domains = I915_GEM_DOMAIN_CPU; 805 obj->write_domain = I915_GEM_DOMAIN_CPU; 806 if (i915_gem_cpu_write_needs_clflush(obj)) 807 obj->cache_dirty = true; 808 } 809 810 void i915_gem_fence_wait_priority(struct dma_fence *fence, 811 const struct i915_sched_attr *attr); 812 813 int i915_gem_object_wait(struct drm_i915_gem_object *obj, 814 unsigned int flags, 815 long timeout); 816 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, 817 unsigned int flags, 818 const struct i915_sched_attr *attr); 819 820 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj, 821 enum fb_op_origin origin); 822 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, 823 enum fb_op_origin origin); 824 825 static inline void 826 i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj, 827 enum fb_op_origin origin) 828 { 829 if (unlikely(rcu_access_pointer(obj->frontbuffer))) 830 __i915_gem_object_flush_frontbuffer(obj, origin); 831 } 832 833 static inline void 834 i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, 835 enum fb_op_origin origin) 836 { 837 if (unlikely(rcu_access_pointer(obj->frontbuffer))) 838 __i915_gem_object_invalidate_frontbuffer(obj, origin); 839 } 840 841 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size); 842 843 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj); 844 845 void __i915_gem_free_object_rcu(struct rcu_head *head); 846 847 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj); 848 849 void __i915_gem_free_object(struct drm_i915_gem_object *obj); 850 851 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj); 852 853 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj); 854 855 int i915_gem_object_migrate(struct drm_i915_gem_object *obj, 856 struct i915_gem_ww_ctx *ww, 857 enum intel_region_id id); 858 int __i915_gem_object_migrate(struct drm_i915_gem_object *obj, 859 struct i915_gem_ww_ctx *ww, 860 enum intel_region_id id, 861 unsigned int flags); 862 863 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj, 864 enum intel_region_id id); 865 866 int i915_gem_object_wait_migration(struct drm_i915_gem_object *obj, 867 unsigned int flags); 868 869 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj, 870 enum intel_memory_type type); 871 872 bool i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj); 873 874 int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st, 875 size_t size, struct intel_memory_region *mr, 876 struct address_space *mapping, 877 unsigned int max_segment); 878 void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping, 879 bool dirty, bool backup); 880 void __shmem_writeback(size_t size, struct address_space *mapping); 881 882 #ifdef CONFIG_MMU_NOTIFIER 883 static inline bool 884 i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) 885 { 886 return obj->userptr.notifier.mm; 887 } 888 889 int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj); 890 int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj); 891 int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj); 892 #else 893 static inline bool i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) { return false; } 894 895 static inline int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; } 896 static inline int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; } 897 static inline int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; } 898 899 #endif 900 901 #endif 902