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