1 /* 2 * Copyright © 2017 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 #include <linux/highmem.h> 26 #include <linux/sched/mm.h> 27 28 #include <drm/drm_cache.h> 29 30 #include "display/intel_frontbuffer.h" 31 #include "pxp/intel_pxp.h" 32 33 #include "i915_drv.h" 34 #include "i915_file_private.h" 35 #include "i915_gem_clflush.h" 36 #include "i915_gem_context.h" 37 #include "i915_gem_dmabuf.h" 38 #include "i915_gem_mman.h" 39 #include "i915_gem_object.h" 40 #include "i915_gem_ttm.h" 41 #include "i915_memcpy.h" 42 #include "i915_trace.h" 43 44 static struct pool slab_objects; 45 46 static const struct drm_gem_object_funcs i915_gem_object_funcs; 47 48 struct drm_i915_gem_object *i915_gem_object_alloc(void) 49 { 50 struct drm_i915_gem_object *obj; 51 52 #ifdef __linux__ 53 obj = kmem_cache_zalloc(slab_objects, GFP_KERNEL); 54 #else 55 obj = pool_get(&slab_objects, PR_WAITOK | PR_ZERO); 56 #endif 57 if (!obj) 58 return NULL; 59 obj->base.funcs = &i915_gem_object_funcs; 60 61 return obj; 62 } 63 64 void i915_gem_object_free(struct drm_i915_gem_object *obj) 65 { 66 #ifdef __linux__ 67 return kmem_cache_free(slab_objects, obj); 68 #else 69 pool_put(&slab_objects, obj); 70 #endif 71 } 72 73 void i915_gem_object_init(struct drm_i915_gem_object *obj, 74 const struct drm_i915_gem_object_ops *ops, 75 struct lock_class_key *key, unsigned flags) 76 { 77 /* 78 * A gem object is embedded both in a struct ttm_buffer_object :/ and 79 * in a drm_i915_gem_object. Make sure they are aliased. 80 */ 81 BUILD_BUG_ON(offsetof(typeof(*obj), base) != 82 offsetof(typeof(*obj), __do_not_access.base)); 83 84 mtx_init(&obj->vma.lock, IPL_NONE); 85 INIT_LIST_HEAD(&obj->vma.list); 86 87 INIT_LIST_HEAD(&obj->mm.link); 88 89 INIT_LIST_HEAD(&obj->lut_list); 90 mtx_init(&obj->lut_lock, IPL_NONE); 91 92 mtx_init(&obj->mmo.lock, IPL_NONE); 93 obj->mmo.offsets = RB_ROOT; 94 95 init_rcu_head(&obj->rcu); 96 97 obj->ops = ops; 98 GEM_BUG_ON(flags & ~I915_BO_ALLOC_FLAGS); 99 obj->flags = flags; 100 101 obj->mm.madv = I915_MADV_WILLNEED; 102 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); 103 rw_init(&obj->mm.get_page.lock, "mmget"); 104 INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN); 105 rw_init(&obj->mm.get_dma_page.lock, "mmgetd"); 106 } 107 108 /** 109 * __i915_gem_object_fini - Clean up a GEM object initialization 110 * @obj: The gem object to cleanup 111 * 112 * This function cleans up gem object fields that are set up by 113 * drm_gem_private_object_init() and i915_gem_object_init(). 114 * It's primarily intended as a helper for backends that need to 115 * clean up the gem object in separate steps. 116 */ 117 void __i915_gem_object_fini(struct drm_i915_gem_object *obj) 118 { 119 mutex_destroy(&obj->mm.get_page.lock); 120 mutex_destroy(&obj->mm.get_dma_page.lock); 121 dma_resv_fini(&obj->base._resv); 122 } 123 124 /** 125 * i915_gem_object_set_cache_coherency - Mark up the object's coherency levels 126 * for a given cache_level 127 * @obj: #drm_i915_gem_object 128 * @cache_level: cache level 129 */ 130 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj, 131 unsigned int cache_level) 132 { 133 struct drm_i915_private *i915 = to_i915(obj->base.dev); 134 135 obj->cache_level = cache_level; 136 137 if (cache_level != I915_CACHE_NONE) 138 obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ | 139 I915_BO_CACHE_COHERENT_FOR_WRITE); 140 else if (HAS_LLC(i915)) 141 obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ; 142 else 143 obj->cache_coherent = 0; 144 145 obj->cache_dirty = 146 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) && 147 !IS_DGFX(i915); 148 } 149 150 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj) 151 { 152 struct drm_i915_private *i915 = to_i915(obj->base.dev); 153 154 /* 155 * This is purely from a security perspective, so we simply don't care 156 * about non-userspace objects being able to bypass the LLC. 157 */ 158 if (!(obj->flags & I915_BO_ALLOC_USER)) 159 return false; 160 161 /* 162 * EHL and JSL add the 'Bypass LLC' MOCS entry, which should make it 163 * possible for userspace to bypass the GTT caching bits set by the 164 * kernel, as per the given object cache_level. This is troublesome 165 * since the heavy flush we apply when first gathering the pages is 166 * skipped if the kernel thinks the object is coherent with the GPU. As 167 * a result it might be possible to bypass the cache and read the 168 * contents of the page directly, which could be stale data. If it's 169 * just a case of userspace shooting themselves in the foot then so be 170 * it, but since i915 takes the stance of always zeroing memory before 171 * handing it to userspace, we need to prevent this. 172 */ 173 return IS_JSL_EHL(i915); 174 } 175 176 static void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) 177 { 178 struct drm_i915_gem_object *obj = to_intel_bo(gem); 179 struct drm_i915_file_private *fpriv = file->driver_priv; 180 struct i915_lut_handle bookmark = {}; 181 struct i915_mmap_offset *mmo, *mn; 182 struct i915_lut_handle *lut, *ln; 183 DRM_LIST_HEAD(close); 184 185 spin_lock(&obj->lut_lock); 186 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { 187 struct i915_gem_context *ctx = lut->ctx; 188 189 if (ctx && ctx->file_priv == fpriv) { 190 i915_gem_context_get(ctx); 191 list_move(&lut->obj_link, &close); 192 } 193 194 /* Break long locks, and carefully continue on from this spot */ 195 if (&ln->obj_link != &obj->lut_list) { 196 list_add_tail(&bookmark.obj_link, &ln->obj_link); 197 if (cond_resched_lock(&obj->lut_lock)) 198 list_safe_reset_next(&bookmark, ln, obj_link); 199 __list_del_entry(&bookmark.obj_link); 200 } 201 } 202 spin_unlock(&obj->lut_lock); 203 204 spin_lock(&obj->mmo.lock); 205 rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset) 206 drm_vma_node_revoke(&mmo->vma_node, file); 207 spin_unlock(&obj->mmo.lock); 208 209 list_for_each_entry_safe(lut, ln, &close, obj_link) { 210 struct i915_gem_context *ctx = lut->ctx; 211 struct i915_vma *vma; 212 213 /* 214 * We allow the process to have multiple handles to the same 215 * vma, in the same fd namespace, by virtue of flink/open. 216 */ 217 218 mutex_lock(&ctx->lut_mutex); 219 vma = radix_tree_delete(&ctx->handles_vma, lut->handle); 220 if (vma) { 221 GEM_BUG_ON(vma->obj != obj); 222 GEM_BUG_ON(!atomic_read(&vma->open_count)); 223 i915_vma_close(vma); 224 } 225 mutex_unlock(&ctx->lut_mutex); 226 227 i915_gem_context_put(lut->ctx); 228 i915_lut_handle_free(lut); 229 i915_gem_object_put(obj); 230 } 231 } 232 233 void __i915_gem_free_object_rcu(struct rcu_head *head) 234 { 235 struct drm_i915_gem_object *obj = 236 container_of(head, typeof(*obj), rcu); 237 struct drm_i915_private *i915 = to_i915(obj->base.dev); 238 239 #ifdef __OpenBSD__ 240 if (obj->base.uao) 241 uao_detach(obj->base.uao); 242 #endif 243 244 i915_gem_object_free(obj); 245 246 GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); 247 atomic_dec(&i915->mm.free_count); 248 } 249 250 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj) 251 { 252 /* Skip serialisation and waking the device if known to be not used. */ 253 254 if (obj->userfault_count && !IS_DGFX(to_i915(obj->base.dev))) 255 i915_gem_object_release_mmap_gtt(obj); 256 257 if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) { 258 struct i915_mmap_offset *mmo, *mn; 259 260 i915_gem_object_release_mmap_offset(obj); 261 262 rbtree_postorder_for_each_entry_safe(mmo, mn, 263 &obj->mmo.offsets, 264 offset) { 265 drm_vma_offset_remove(obj->base.dev->vma_offset_manager, 266 &mmo->vma_node); 267 kfree(mmo); 268 } 269 obj->mmo.offsets = RB_ROOT; 270 } 271 } 272 273 /** 274 * __i915_gem_object_pages_fini - Clean up pages use of a gem object 275 * @obj: The gem object to clean up 276 * 277 * This function cleans up usage of the object mm.pages member. It 278 * is intended for backends that need to clean up a gem object in 279 * separate steps and needs to be called when the object is idle before 280 * the object's backing memory is freed. 281 */ 282 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj) 283 { 284 assert_object_held_shared(obj); 285 286 if (!list_empty(&obj->vma.list)) { 287 struct i915_vma *vma; 288 289 spin_lock(&obj->vma.lock); 290 while ((vma = list_first_entry_or_null(&obj->vma.list, 291 struct i915_vma, 292 obj_link))) { 293 GEM_BUG_ON(vma->obj != obj); 294 spin_unlock(&obj->vma.lock); 295 296 i915_vma_destroy(vma); 297 298 spin_lock(&obj->vma.lock); 299 } 300 spin_unlock(&obj->vma.lock); 301 } 302 303 __i915_gem_object_free_mmaps(obj); 304 305 atomic_set(&obj->mm.pages_pin_count, 0); 306 __i915_gem_object_put_pages(obj); 307 GEM_BUG_ON(i915_gem_object_has_pages(obj)); 308 } 309 310 void __i915_gem_free_object(struct drm_i915_gem_object *obj) 311 { 312 trace_i915_gem_object_destroy(obj); 313 314 GEM_BUG_ON(!list_empty(&obj->lut_list)); 315 316 bitmap_free(obj->bit_17); 317 318 if (obj->base.import_attach) 319 drm_prime_gem_destroy(&obj->base, NULL); 320 321 drm_gem_free_mmap_offset(&obj->base); 322 323 if (obj->ops->release) 324 obj->ops->release(obj); 325 326 if (obj->mm.n_placements > 1) 327 kfree(obj->mm.placements); 328 329 if (obj->shares_resv_from) 330 i915_vm_resv_put(obj->shares_resv_from); 331 332 __i915_gem_object_fini(obj); 333 } 334 335 static void __i915_gem_free_objects(struct drm_i915_private *i915, 336 struct llist_node *freed) 337 { 338 struct drm_i915_gem_object *obj, *on; 339 340 llist_for_each_entry_safe(obj, on, freed, freed) { 341 might_sleep(); 342 if (obj->ops->delayed_free) { 343 obj->ops->delayed_free(obj); 344 continue; 345 } 346 347 __i915_gem_object_pages_fini(obj); 348 __i915_gem_free_object(obj); 349 350 /* But keep the pointer alive for RCU-protected lookups */ 351 call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 352 cond_resched(); 353 } 354 } 355 356 void i915_gem_flush_free_objects(struct drm_i915_private *i915) 357 { 358 struct llist_node *freed = llist_del_all(&i915->mm.free_list); 359 360 if (unlikely(freed)) 361 __i915_gem_free_objects(i915, freed); 362 } 363 364 static void __i915_gem_free_work(struct work_struct *work) 365 { 366 struct drm_i915_private *i915 = 367 container_of(work, struct drm_i915_private, mm.free_work); 368 369 i915_gem_flush_free_objects(i915); 370 } 371 372 static void i915_gem_free_object(struct drm_gem_object *gem_obj) 373 { 374 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); 375 struct drm_i915_private *i915 = to_i915(obj->base.dev); 376 377 GEM_BUG_ON(i915_gem_object_is_framebuffer(obj)); 378 379 /* 380 * Before we free the object, make sure any pure RCU-only 381 * read-side critical sections are complete, e.g. 382 * i915_gem_busy_ioctl(). For the corresponding synchronized 383 * lookup see i915_gem_object_lookup_rcu(). 384 */ 385 atomic_inc(&i915->mm.free_count); 386 387 /* 388 * Since we require blocking on struct_mutex to unbind the freed 389 * object from the GPU before releasing resources back to the 390 * system, we can not do that directly from the RCU callback (which may 391 * be a softirq context), but must instead then defer that work onto a 392 * kthread. We use the RCU callback rather than move the freed object 393 * directly onto the work queue so that we can mix between using the 394 * worker and performing frees directly from subsequent allocations for 395 * crude but effective memory throttling. 396 */ 397 398 if (llist_add(&obj->freed, &i915->mm.free_list)) 399 queue_work(i915->wq, &i915->mm.free_work); 400 } 401 402 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj, 403 enum fb_op_origin origin) 404 { 405 struct intel_frontbuffer *front; 406 407 front = __intel_frontbuffer_get(obj); 408 if (front) { 409 intel_frontbuffer_flush(front, origin); 410 intel_frontbuffer_put(front); 411 } 412 } 413 414 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, 415 enum fb_op_origin origin) 416 { 417 struct intel_frontbuffer *front; 418 419 front = __intel_frontbuffer_get(obj); 420 if (front) { 421 intel_frontbuffer_invalidate(front, origin); 422 intel_frontbuffer_put(front); 423 } 424 } 425 426 static void 427 i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size) 428 { 429 void *src_map; 430 void *src_ptr; 431 432 src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT)); 433 434 src_ptr = src_map + offset_in_page(offset); 435 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) 436 drm_clflush_virt_range(src_ptr, size); 437 memcpy(dst, src_ptr, size); 438 439 kunmap_atomic(src_map); 440 } 441 442 static void 443 i915_gem_object_read_from_page_iomap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size) 444 { 445 STUB(); 446 #ifdef notyet 447 void __iomem *src_map; 448 void __iomem *src_ptr; 449 dma_addr_t dma = i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT); 450 451 src_map = io_mapping_map_wc(&obj->mm.region->iomap, 452 dma - obj->mm.region->region.start, 453 PAGE_SIZE); 454 455 src_ptr = src_map + offset_in_page(offset); 456 if (!i915_memcpy_from_wc(dst, (void __force *)src_ptr, size)) 457 memcpy_fromio(dst, src_ptr, size); 458 459 io_mapping_unmap(src_map); 460 #endif 461 } 462 463 /** 464 * i915_gem_object_read_from_page - read data from the page of a GEM object 465 * @obj: GEM object to read from 466 * @offset: offset within the object 467 * @dst: buffer to store the read data 468 * @size: size to read 469 * 470 * Reads data from @obj at the specified offset. The requested region to read 471 * from can't cross a page boundary. The caller must ensure that @obj pages 472 * are pinned and that @obj is synced wrt. any related writes. 473 * 474 * Return: %0 on success or -ENODEV if the type of @obj's backing store is 475 * unsupported. 476 */ 477 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size) 478 { 479 GEM_BUG_ON(offset >= obj->base.size); 480 GEM_BUG_ON(offset_in_page(offset) > PAGE_SIZE - size); 481 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 482 483 if (i915_gem_object_has_struct_page(obj)) 484 i915_gem_object_read_from_page_kmap(obj, offset, dst, size); 485 else if (i915_gem_object_has_iomem(obj)) 486 i915_gem_object_read_from_page_iomap(obj, offset, dst, size); 487 else 488 return -ENODEV; 489 490 return 0; 491 } 492 493 /** 494 * i915_gem_object_evictable - Whether object is likely evictable after unbind. 495 * @obj: The object to check 496 * 497 * This function checks whether the object is likely unvictable after unbind. 498 * If the object is not locked when checking, the result is only advisory. 499 * If the object is locked when checking, and the function returns true, 500 * then an eviction should indeed be possible. But since unlocked vma 501 * unpinning and unbinding is currently possible, the object can actually 502 * become evictable even if this function returns false. 503 * 504 * Return: true if the object may be evictable. False otherwise. 505 */ 506 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj) 507 { 508 struct i915_vma *vma; 509 int pin_count = atomic_read(&obj->mm.pages_pin_count); 510 511 if (!pin_count) 512 return true; 513 514 spin_lock(&obj->vma.lock); 515 list_for_each_entry(vma, &obj->vma.list, obj_link) { 516 if (i915_vma_is_pinned(vma)) { 517 spin_unlock(&obj->vma.lock); 518 return false; 519 } 520 if (atomic_read(&vma->pages_count)) 521 pin_count--; 522 } 523 spin_unlock(&obj->vma.lock); 524 GEM_WARN_ON(pin_count < 0); 525 526 return pin_count == 0; 527 } 528 529 /** 530 * i915_gem_object_migratable - Whether the object is migratable out of the 531 * current region. 532 * @obj: Pointer to the object. 533 * 534 * Return: Whether the object is allowed to be resident in other 535 * regions than the current while pages are present. 536 */ 537 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj) 538 { 539 struct intel_memory_region *mr = READ_ONCE(obj->mm.region); 540 541 if (!mr) 542 return false; 543 544 return obj->mm.n_placements > 1; 545 } 546 547 /** 548 * i915_gem_object_has_struct_page - Whether the object is page-backed 549 * @obj: The object to query. 550 * 551 * This function should only be called while the object is locked or pinned, 552 * otherwise the page backing may change under the caller. 553 * 554 * Return: True if page-backed, false otherwise. 555 */ 556 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj) 557 { 558 #ifdef CONFIG_LOCKDEP 559 if (IS_DGFX(to_i915(obj->base.dev)) && 560 i915_gem_object_evictable((void __force *)obj)) 561 assert_object_held_shared(obj); 562 #endif 563 return obj->mem_flags & I915_BO_FLAG_STRUCT_PAGE; 564 } 565 566 /** 567 * i915_gem_object_has_iomem - Whether the object is iomem-backed 568 * @obj: The object to query. 569 * 570 * This function should only be called while the object is locked or pinned, 571 * otherwise the iomem backing may change under the caller. 572 * 573 * Return: True if iomem-backed, false otherwise. 574 */ 575 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj) 576 { 577 #ifdef CONFIG_LOCKDEP 578 if (IS_DGFX(to_i915(obj->base.dev)) && 579 i915_gem_object_evictable((void __force *)obj)) 580 assert_object_held_shared(obj); 581 #endif 582 return obj->mem_flags & I915_BO_FLAG_IOMEM; 583 } 584 585 /** 586 * i915_gem_object_can_migrate - Whether an object likely can be migrated 587 * 588 * @obj: The object to migrate 589 * @id: The region intended to migrate to 590 * 591 * Check whether the object backend supports migration to the 592 * given region. Note that pinning may affect the ability to migrate as 593 * returned by this function. 594 * 595 * This function is primarily intended as a helper for checking the 596 * possibility to migrate objects and might be slightly less permissive 597 * than i915_gem_object_migrate() when it comes to objects with the 598 * I915_BO_ALLOC_USER flag set. 599 * 600 * Return: true if migration is possible, false otherwise. 601 */ 602 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj, 603 enum intel_region_id id) 604 { 605 struct drm_i915_private *i915 = to_i915(obj->base.dev); 606 unsigned int num_allowed = obj->mm.n_placements; 607 struct intel_memory_region *mr; 608 unsigned int i; 609 610 GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN); 611 GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED); 612 613 mr = i915->mm.regions[id]; 614 if (!mr) 615 return false; 616 617 if (!IS_ALIGNED(obj->base.size, mr->min_page_size)) 618 return false; 619 620 if (obj->mm.region == mr) 621 return true; 622 623 if (!i915_gem_object_evictable(obj)) 624 return false; 625 626 if (!obj->ops->migrate) 627 return false; 628 629 if (!(obj->flags & I915_BO_ALLOC_USER)) 630 return true; 631 632 if (num_allowed == 0) 633 return false; 634 635 for (i = 0; i < num_allowed; ++i) { 636 if (mr == obj->mm.placements[i]) 637 return true; 638 } 639 640 return false; 641 } 642 643 /** 644 * i915_gem_object_migrate - Migrate an object to the desired region id 645 * @obj: The object to migrate. 646 * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may 647 * not be successful in evicting other objects to make room for this object. 648 * @id: The region id to migrate to. 649 * 650 * Attempt to migrate the object to the desired memory region. The 651 * object backend must support migration and the object may not be 652 * pinned, (explicitly pinned pages or pinned vmas). The object must 653 * be locked. 654 * On successful completion, the object will have pages pointing to 655 * memory in the new region, but an async migration task may not have 656 * completed yet, and to accomplish that, i915_gem_object_wait_migration() 657 * must be called. 658 * 659 * Note: the @ww parameter is not used yet, but included to make sure 660 * callers put some effort into obtaining a valid ww ctx if one is 661 * available. 662 * 663 * Return: 0 on success. Negative error code on failure. In particular may 664 * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance 665 * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and 666 * -EBUSY if the object is pinned. 667 */ 668 int i915_gem_object_migrate(struct drm_i915_gem_object *obj, 669 struct i915_gem_ww_ctx *ww, 670 enum intel_region_id id) 671 { 672 return __i915_gem_object_migrate(obj, ww, id, obj->flags); 673 } 674 675 /** 676 * __i915_gem_object_migrate - Migrate an object to the desired region id, with 677 * control of the extra flags 678 * @obj: The object to migrate. 679 * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may 680 * not be successful in evicting other objects to make room for this object. 681 * @id: The region id to migrate to. 682 * @flags: The object flags. Normally just obj->flags. 683 * 684 * Attempt to migrate the object to the desired memory region. The 685 * object backend must support migration and the object may not be 686 * pinned, (explicitly pinned pages or pinned vmas). The object must 687 * be locked. 688 * On successful completion, the object will have pages pointing to 689 * memory in the new region, but an async migration task may not have 690 * completed yet, and to accomplish that, i915_gem_object_wait_migration() 691 * must be called. 692 * 693 * Note: the @ww parameter is not used yet, but included to make sure 694 * callers put some effort into obtaining a valid ww ctx if one is 695 * available. 696 * 697 * Return: 0 on success. Negative error code on failure. In particular may 698 * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance 699 * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and 700 * -EBUSY if the object is pinned. 701 */ 702 int __i915_gem_object_migrate(struct drm_i915_gem_object *obj, 703 struct i915_gem_ww_ctx *ww, 704 enum intel_region_id id, 705 unsigned int flags) 706 { 707 struct drm_i915_private *i915 = to_i915(obj->base.dev); 708 struct intel_memory_region *mr; 709 710 GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN); 711 GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED); 712 assert_object_held(obj); 713 714 mr = i915->mm.regions[id]; 715 GEM_BUG_ON(!mr); 716 717 if (!i915_gem_object_can_migrate(obj, id)) 718 return -EINVAL; 719 720 if (!obj->ops->migrate) { 721 if (GEM_WARN_ON(obj->mm.region != mr)) 722 return -EINVAL; 723 return 0; 724 } 725 726 return obj->ops->migrate(obj, mr, flags); 727 } 728 729 /** 730 * i915_gem_object_placement_possible - Check whether the object can be 731 * placed at certain memory type 732 * @obj: Pointer to the object 733 * @type: The memory type to check 734 * 735 * Return: True if the object can be placed in @type. False otherwise. 736 */ 737 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj, 738 enum intel_memory_type type) 739 { 740 unsigned int i; 741 742 if (!obj->mm.n_placements) { 743 switch (type) { 744 case INTEL_MEMORY_LOCAL: 745 return i915_gem_object_has_iomem(obj); 746 case INTEL_MEMORY_SYSTEM: 747 return i915_gem_object_has_pages(obj); 748 default: 749 /* Ignore stolen for now */ 750 GEM_BUG_ON(1); 751 return false; 752 } 753 } 754 755 for (i = 0; i < obj->mm.n_placements; i++) { 756 if (obj->mm.placements[i]->type == type) 757 return true; 758 } 759 760 return false; 761 } 762 763 /** 764 * i915_gem_object_needs_ccs_pages - Check whether the object requires extra 765 * pages when placed in system-memory, in order to save and later restore the 766 * flat-CCS aux state when the object is moved between local-memory and 767 * system-memory 768 * @obj: Pointer to the object 769 * 770 * Return: True if the object needs extra ccs pages. False otherwise. 771 */ 772 bool i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj) 773 { 774 bool lmem_placement = false; 775 int i; 776 777 if (!HAS_FLAT_CCS(to_i915(obj->base.dev))) 778 return false; 779 780 if (obj->flags & I915_BO_ALLOC_CCS_AUX) 781 return true; 782 783 for (i = 0; i < obj->mm.n_placements; i++) { 784 /* Compression is not allowed for the objects with smem placement */ 785 if (obj->mm.placements[i]->type == INTEL_MEMORY_SYSTEM) 786 return false; 787 if (!lmem_placement && 788 obj->mm.placements[i]->type == INTEL_MEMORY_LOCAL) 789 lmem_placement = true; 790 } 791 792 return lmem_placement; 793 } 794 795 void i915_gem_init__objects(struct drm_i915_private *i915) 796 { 797 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); 798 } 799 800 void i915_objects_module_exit(void) 801 { 802 #ifdef __linux__ 803 kmem_cache_destroy(slab_objects); 804 #else 805 pool_destroy(&slab_objects); 806 #endif 807 } 808 809 int __init i915_objects_module_init(void) 810 { 811 #ifdef __linux__ 812 slab_objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); 813 if (!slab_objects) 814 return -ENOMEM; 815 #else 816 pool_init(&slab_objects, sizeof(struct drm_i915_gem_object), 817 CACHELINESIZE, IPL_NONE, 0, "drmobj", NULL); 818 #endif 819 820 return 0; 821 } 822 823 static const struct drm_gem_object_funcs i915_gem_object_funcs = { 824 .free = i915_gem_free_object, 825 .close = i915_gem_close_object, 826 .export = i915_gem_prime_export, 827 }; 828 829 /** 830 * i915_gem_object_get_moving_fence - Get the object's moving fence if any 831 * @obj: The object whose moving fence to get. 832 * @fence: The resulting fence 833 * 834 * A non-signaled moving fence means that there is an async operation 835 * pending on the object that needs to be waited on before setting up 836 * any GPU- or CPU PTEs to the object's pages. 837 * 838 * Return: Negative error code or 0 for success. 839 */ 840 int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj, 841 struct dma_fence **fence) 842 { 843 return dma_resv_get_singleton(obj->base.resv, DMA_RESV_USAGE_KERNEL, 844 fence); 845 } 846 847 /** 848 * i915_gem_object_wait_moving_fence - Wait for the object's moving fence if any 849 * @obj: The object whose moving fence to wait for. 850 * @intr: Whether to wait interruptible. 851 * 852 * If the moving fence signaled without an error, it is detached from the 853 * object and put. 854 * 855 * Return: 0 if successful, -ERESTARTSYS if the wait was interrupted, 856 * negative error code if the async operation represented by the 857 * moving fence failed. 858 */ 859 int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj, 860 bool intr) 861 { 862 long ret; 863 864 assert_object_held(obj); 865 866 ret = dma_resv_wait_timeout(obj->base. resv, DMA_RESV_USAGE_KERNEL, 867 intr, MAX_SCHEDULE_TIMEOUT); 868 if (!ret) 869 ret = -ETIME; 870 else if (ret > 0 && i915_gem_object_has_unknown_state(obj)) 871 ret = -EIO; 872 873 return ret < 0 ? ret : 0; 874 } 875 876 /** 877 * i915_gem_object_has_unknown_state - Return true if the object backing pages are 878 * in an unknown_state. This means that userspace must NEVER be allowed to touch 879 * the pages, with either the GPU or CPU. 880 * 881 * ONLY valid to be called after ensuring that all kernel fences have signalled 882 * (in particular the fence for moving/clearing the object). 883 */ 884 bool i915_gem_object_has_unknown_state(struct drm_i915_gem_object *obj) 885 { 886 /* 887 * The below barrier pairs with the dma_fence_signal() in 888 * __memcpy_work(). We should only sample the unknown_state after all 889 * the kernel fences have signalled. 890 */ 891 smp_rmb(); 892 return obj->mm.unknown_state; 893 } 894 895 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 896 #include "selftests/huge_gem_object.c" 897 #include "selftests/huge_pages.c" 898 #include "selftests/i915_gem_migrate.c" 899 #include "selftests/i915_gem_object.c" 900 #include "selftests/i915_gem_coherency.c" 901 #endif 902