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