1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2014-2016 Intel Corporation 5 */ 6 7 #include "i915_drv.h" 8 #include "i915_gem_object.h" 9 #include "i915_scatterlist.h" 10 #include "i915_gem_lmem.h" 11 #include "i915_gem_mman.h" 12 13 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, 14 struct sg_table *pages, 15 unsigned int sg_page_sizes) 16 { 17 struct drm_i915_private *i915 = to_i915(obj->base.dev); 18 unsigned long supported = INTEL_INFO(i915)->page_sizes; 19 int i; 20 21 lockdep_assert_held(&obj->mm.lock); 22 23 if (i915_gem_object_is_volatile(obj)) 24 obj->mm.madv = I915_MADV_DONTNEED; 25 26 /* Make the pages coherent with the GPU (flushing any swapin). */ 27 if (obj->cache_dirty) { 28 obj->write_domain = 0; 29 if (i915_gem_object_has_struct_page(obj)) 30 drm_clflush_sg(pages); 31 obj->cache_dirty = false; 32 } 33 34 obj->mm.get_page.sg_pos = pages->sgl; 35 obj->mm.get_page.sg_idx = 0; 36 37 obj->mm.pages = pages; 38 39 if (i915_gem_object_is_tiled(obj) && 40 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { 41 GEM_BUG_ON(obj->mm.quirked); 42 __i915_gem_object_pin_pages(obj); 43 obj->mm.quirked = true; 44 } 45 46 GEM_BUG_ON(!sg_page_sizes); 47 obj->mm.page_sizes.phys = sg_page_sizes; 48 49 /* 50 * Calculate the supported page-sizes which fit into the given 51 * sg_page_sizes. This will give us the page-sizes which we may be able 52 * to use opportunistically when later inserting into the GTT. For 53 * example if phys=2G, then in theory we should be able to use 1G, 2M, 54 * 64K or 4K pages, although in practice this will depend on a number of 55 * other factors. 56 */ 57 obj->mm.page_sizes.sg = 0; 58 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { 59 if (obj->mm.page_sizes.phys & ~0u << i) 60 obj->mm.page_sizes.sg |= BIT(i); 61 } 62 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); 63 64 if (i915_gem_object_is_shrinkable(obj)) { 65 struct list_head *list; 66 unsigned long flags; 67 68 spin_lock_irqsave(&i915->mm.obj_lock, flags); 69 70 i915->mm.shrink_count++; 71 i915->mm.shrink_memory += obj->base.size; 72 73 if (obj->mm.madv != I915_MADV_WILLNEED) 74 list = &i915->mm.purge_list; 75 else 76 list = &i915->mm.shrink_list; 77 list_add_tail(&obj->mm.link, list); 78 79 atomic_set(&obj->mm.shrink_pin, 0); 80 spin_unlock_irqrestore(&i915->mm.obj_lock, flags); 81 } 82 } 83 84 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) 85 { 86 struct drm_i915_private *i915 = to_i915(obj->base.dev); 87 int err; 88 89 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { 90 drm_dbg(&i915->drm, 91 "Attempting to obtain a purgeable object\n"); 92 return -EFAULT; 93 } 94 95 err = obj->ops->get_pages(obj); 96 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); 97 98 return err; 99 } 100 101 /* Ensure that the associated pages are gathered from the backing storage 102 * and pinned into our object. i915_gem_object_pin_pages() may be called 103 * multiple times before they are released by a single call to 104 * i915_gem_object_unpin_pages() - once the pages are no longer referenced 105 * either as a result of memory pressure (reaping pages under the shrinker) 106 * or as the object is itself released. 107 */ 108 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) 109 { 110 int err; 111 112 err = mutex_lock_interruptible_nested(&obj->mm.lock, I915_MM_GET_PAGES); 113 if (err) 114 return err; 115 116 if (unlikely(!i915_gem_object_has_pages(obj))) { 117 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); 118 119 err = ____i915_gem_object_get_pages(obj); 120 if (err) 121 goto unlock; 122 123 smp_mb__before_atomic(); 124 } 125 atomic_inc(&obj->mm.pages_pin_count); 126 127 unlock: 128 mutex_unlock(&obj->mm.lock); 129 return err; 130 } 131 132 /* Immediately discard the backing storage */ 133 void i915_gem_object_truncate(struct drm_i915_gem_object *obj) 134 { 135 drm_gem_free_mmap_offset(&obj->base); 136 if (obj->ops->truncate) 137 obj->ops->truncate(obj); 138 } 139 140 /* Try to discard unwanted pages */ 141 void i915_gem_object_writeback(struct drm_i915_gem_object *obj) 142 { 143 lockdep_assert_held(&obj->mm.lock); 144 GEM_BUG_ON(i915_gem_object_has_pages(obj)); 145 146 if (obj->ops->writeback) 147 obj->ops->writeback(obj); 148 } 149 150 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) 151 { 152 struct radix_tree_iter iter; 153 void __rcu **slot; 154 155 rcu_read_lock(); 156 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) 157 radix_tree_delete(&obj->mm.get_page.radix, iter.index); 158 rcu_read_unlock(); 159 } 160 161 static void unmap_object(struct drm_i915_gem_object *obj, void *ptr) 162 { 163 if (is_vmalloc_addr(ptr)) 164 vunmap(ptr, obj->base.size); 165 } 166 167 struct sg_table * 168 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) 169 { 170 struct sg_table *pages; 171 172 pages = fetch_and_zero(&obj->mm.pages); 173 if (IS_ERR_OR_NULL(pages)) 174 return pages; 175 176 if (i915_gem_object_is_volatile(obj)) 177 obj->mm.madv = I915_MADV_WILLNEED; 178 179 i915_gem_object_make_unshrinkable(obj); 180 181 if (obj->mm.mapping) { 182 unmap_object(obj, page_mask_bits(obj->mm.mapping)); 183 obj->mm.mapping = NULL; 184 } 185 186 __i915_gem_object_reset_page_iter(obj); 187 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; 188 189 return pages; 190 } 191 192 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj) 193 { 194 struct sg_table *pages; 195 int err; 196 197 if (i915_gem_object_has_pinned_pages(obj)) 198 return -EBUSY; 199 200 /* May be called by shrinker from within get_pages() (on another bo) */ 201 mutex_lock(&obj->mm.lock); 202 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { 203 err = -EBUSY; 204 goto unlock; 205 } 206 207 i915_gem_object_release_mmap_offset(obj); 208 209 /* 210 * ->put_pages might need to allocate memory for the bit17 swizzle 211 * array, hence protect them from being reaped by removing them from gtt 212 * lists early. 213 */ 214 pages = __i915_gem_object_unset_pages(obj); 215 216 /* 217 * XXX Temporary hijinx to avoid updating all backends to handle 218 * NULL pages. In the future, when we have more asynchronous 219 * get_pages backends we should be better able to handle the 220 * cancellation of the async task in a more uniform manner. 221 */ 222 if (!pages && !i915_gem_object_needs_async_cancel(obj)) 223 pages = ERR_PTR(-EINVAL); 224 225 if (!IS_ERR(pages)) 226 obj->ops->put_pages(obj, pages); 227 228 err = 0; 229 unlock: 230 mutex_unlock(&obj->mm.lock); 231 232 return err; 233 } 234 235 /* The 'mapping' part of i915_gem_object_pin_map() below */ 236 static void *i915_gem_object_map_page(struct drm_i915_gem_object *obj, 237 enum i915_map_type type) 238 { 239 unsigned long n_pages = obj->base.size >> PAGE_SHIFT, i; 240 struct vm_page *stack[32], **pages = stack, *page; 241 struct sgt_iter iter; 242 pgprot_t pgprot; 243 void *vaddr; 244 245 switch (type) { 246 default: 247 MISSING_CASE(type); 248 fallthrough; /* to use PAGE_KERNEL anyway */ 249 case I915_MAP_WB: 250 /* 251 * On 32b, highmem using a finite set of indirect PTE (i.e. 252 * vmap) to provide virtual mappings of the high pages. 253 * As these are finite, map_new_virtual() must wait for some 254 * other kmap() to finish when it runs out. If we map a large 255 * number of objects, there is no method for it to tell us 256 * to release the mappings, and we deadlock. 257 * 258 * However, if we make an explicit vmap of the page, that 259 * uses a larger vmalloc arena, and also has the ability 260 * to tell us to release unwanted mappings. Most importantly, 261 * it will fail and propagate an error instead of waiting 262 * forever. 263 * 264 * So if the page is beyond the 32b boundary, make an explicit 265 * vmap. 266 */ 267 #ifdef notyet 268 if (n_pages == 1 && !PageHighMem(sg_page(obj->mm.pages->sgl))) 269 return page_address(sg_page(obj->mm.pages->sgl)); 270 #endif 271 pgprot = PAGE_KERNEL; 272 break; 273 case I915_MAP_WC: 274 pgprot = pgprot_writecombine(PAGE_KERNEL_IO); 275 break; 276 } 277 278 if (n_pages > ARRAY_SIZE(stack)) { 279 /* Too big for stack -- allocate temporary array instead */ 280 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); 281 if (!pages) 282 return NULL; 283 } 284 285 i = 0; 286 for_each_sgt_page(page, iter, obj->mm.pages) 287 pages[i++] = page; 288 vaddr = vmap(pages, n_pages, 0, pgprot); 289 if (pages != stack) 290 kvfree(pages); 291 return vaddr; 292 } 293 294 static void *i915_gem_object_map_pfn(struct drm_i915_gem_object *obj, 295 enum i915_map_type type) 296 { 297 STUB(); 298 return NULL; 299 #ifdef notyet 300 resource_size_t iomap = obj->mm.region->iomap.base - 301 obj->mm.region->region.start; 302 unsigned long n_pfn = obj->base.size >> PAGE_SHIFT; 303 unsigned long stack[32], *pfns = stack, i; 304 struct sgt_iter iter; 305 dma_addr_t addr; 306 void *vaddr; 307 308 if (type != I915_MAP_WC) 309 return NULL; 310 311 if (n_pfn > ARRAY_SIZE(stack)) { 312 /* Too big for stack -- allocate temporary array instead */ 313 pfns = kvmalloc_array(n_pfn, sizeof(*pfns), GFP_KERNEL); 314 if (!pfns) 315 return NULL; 316 } 317 318 i = 0; 319 for_each_sgt_daddr(addr, iter, obj->mm.pages) 320 pfns[i++] = (iomap + addr) >> PAGE_SHIFT; 321 vaddr = vmap_pfn(pfns, n_pfn, pgprot_writecombine(PAGE_KERNEL_IO)); 322 if (pfns != stack) 323 kvfree(pfns); 324 return vaddr; 325 #endif 326 } 327 328 static void *i915_gem_object_map(struct drm_i915_gem_object *obj, 329 enum i915_map_type type) 330 { 331 unsigned long n_pages = obj->base.size >> PAGE_SHIFT; 332 struct sg_table *sgt = obj->mm.pages; 333 struct vm_page *stack_pages[32]; 334 struct vm_page **pages = stack_pages; 335 struct vm_struct *area; 336 pgprot_t pgprot; 337 void *addr; 338 339 if (!i915_gem_object_has_struct_page(obj) && type != I915_MAP_WC) 340 return NULL; 341 342 #if 0 343 /* A single page can always be kmapped */ 344 if (n_pages == 1 && type == I915_MAP_WB) 345 return kmap(sg_page(sgt->sgl)); 346 #endif 347 348 if (n_pages > ARRAY_SIZE(stack_pages)) { 349 /* Too big for stack -- allocate temporary array instead */ 350 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); 351 if (!pages) 352 return NULL; 353 } 354 355 switch (type) { 356 default: 357 MISSING_CASE(type); 358 /* fallthrough - to use PAGE_KERNEL anyway */ 359 case I915_MAP_WB: 360 pgprot = PAGE_KERNEL; 361 break; 362 case I915_MAP_WC: 363 pgprot = pgprot_writecombine(PAGE_KERNEL_IO); 364 break; 365 } 366 367 if (i915_gem_object_has_struct_page(obj)) { 368 struct sgt_iter iter; 369 struct vm_page *page; 370 unsigned long i = 0; 371 372 for_each_sgt_page(page, iter, sgt) 373 pages[i++] = page; 374 } else { 375 STUB(); 376 #ifdef notyet 377 resource_size_t iomap; 378 struct sgt_iter iter; 379 pte_t **ptes = mem; 380 dma_addr_t addr; 381 382 iomap = obj->mm.region->iomap.base; 383 iomap -= obj->mm.region->region.start; 384 385 for_each_sgt_daddr(addr, iter, sgt) 386 **ptes++ = iomap_pte(iomap, addr, pgprot); 387 #endif 388 } 389 addr = vmap(pages, n_pages, 0, pgprot); 390 391 if (pages != stack_pages) 392 kvfree(pages); 393 394 return addr; 395 } 396 397 /* get, pin, and map the pages of the object into kernel space */ 398 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, 399 enum i915_map_type type) 400 { 401 enum i915_map_type has_type; 402 unsigned int flags; 403 bool pinned; 404 void *ptr; 405 int err; 406 407 flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | I915_GEM_OBJECT_HAS_IOMEM; 408 if (!i915_gem_object_type_has(obj, flags)) 409 return ERR_PTR(-ENXIO); 410 411 err = mutex_lock_interruptible_nested(&obj->mm.lock, I915_MM_GET_PAGES); 412 if (err) 413 return ERR_PTR(err); 414 415 pinned = !(type & I915_MAP_OVERRIDE); 416 type &= ~I915_MAP_OVERRIDE; 417 418 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { 419 if (unlikely(!i915_gem_object_has_pages(obj))) { 420 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); 421 422 err = ____i915_gem_object_get_pages(obj); 423 if (err) 424 goto err_unlock; 425 426 smp_mb__before_atomic(); 427 } 428 atomic_inc(&obj->mm.pages_pin_count); 429 pinned = false; 430 } 431 GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 432 433 ptr = page_unpack_bits(obj->mm.mapping, &has_type); 434 if (ptr && has_type != type) { 435 if (pinned) { 436 err = -EBUSY; 437 goto err_unpin; 438 } 439 440 unmap_object(obj, ptr); 441 442 ptr = obj->mm.mapping = NULL; 443 } 444 445 if (!ptr) { 446 if (GEM_WARN_ON(type == I915_MAP_WC && 447 !static_cpu_has(X86_FEATURE_PAT))) 448 ptr = NULL; 449 else if (i915_gem_object_has_struct_page(obj)) 450 ptr = i915_gem_object_map_page(obj, type); 451 else 452 ptr = i915_gem_object_map_pfn(obj, type); 453 if (!ptr) { 454 err = -ENOMEM; 455 goto err_unpin; 456 } 457 458 obj->mm.mapping = page_pack_bits(ptr, type); 459 } 460 461 out_unlock: 462 mutex_unlock(&obj->mm.lock); 463 return ptr; 464 465 err_unpin: 466 atomic_dec(&obj->mm.pages_pin_count); 467 err_unlock: 468 ptr = ERR_PTR(err); 469 goto out_unlock; 470 } 471 472 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj, 473 unsigned long offset, 474 unsigned long size) 475 { 476 enum i915_map_type has_type; 477 void *ptr; 478 479 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 480 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size), 481 offset, size, obj->base.size)); 482 483 wmb(); /* let all previous writes be visible to coherent partners */ 484 obj->mm.dirty = true; 485 486 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) 487 return; 488 489 ptr = page_unpack_bits(obj->mm.mapping, &has_type); 490 if (has_type == I915_MAP_WC) 491 return; 492 493 drm_clflush_virt_range(ptr + offset, size); 494 if (size == obj->base.size) { 495 obj->write_domain &= ~I915_GEM_DOMAIN_CPU; 496 obj->cache_dirty = false; 497 } 498 } 499 500 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj) 501 { 502 GEM_BUG_ON(!obj->mm.mapping); 503 504 /* 505 * We allow removing the mapping from underneath pinned pages! 506 * 507 * Furthermore, since this is an unsafe operation reserved only 508 * for construction time manipulation, we ignore locking prudence. 509 */ 510 unmap_object(obj, page_mask_bits(fetch_and_zero(&obj->mm.mapping))); 511 512 i915_gem_object_unpin_map(obj); 513 } 514 515 struct scatterlist * 516 i915_gem_object_get_sg(struct drm_i915_gem_object *obj, 517 unsigned int n, 518 unsigned int *offset) 519 { 520 struct i915_gem_object_page_iter *iter = &obj->mm.get_page; 521 struct scatterlist *sg; 522 unsigned int idx, count; 523 524 might_sleep(); 525 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); 526 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 527 528 /* As we iterate forward through the sg, we record each entry in a 529 * radixtree for quick repeated (backwards) lookups. If we have seen 530 * this index previously, we will have an entry for it. 531 * 532 * Initial lookup is O(N), but this is amortized to O(1) for 533 * sequential page access (where each new request is consecutive 534 * to the previous one). Repeated lookups are O(lg(obj->base.size)), 535 * i.e. O(1) with a large constant! 536 */ 537 if (n < READ_ONCE(iter->sg_idx)) 538 goto lookup; 539 540 mutex_lock(&iter->lock); 541 542 /* We prefer to reuse the last sg so that repeated lookup of this 543 * (or the subsequent) sg are fast - comparing against the last 544 * sg is faster than going through the radixtree. 545 */ 546 547 sg = iter->sg_pos; 548 idx = iter->sg_idx; 549 count = __sg_page_count(sg); 550 551 while (idx + count <= n) { 552 void *entry; 553 unsigned long i; 554 int ret; 555 556 /* If we cannot allocate and insert this entry, or the 557 * individual pages from this range, cancel updating the 558 * sg_idx so that on this lookup we are forced to linearly 559 * scan onwards, but on future lookups we will try the 560 * insertion again (in which case we need to be careful of 561 * the error return reporting that we have already inserted 562 * this index). 563 */ 564 ret = radix_tree_insert(&iter->radix, idx, sg); 565 if (ret && ret != -EEXIST) 566 goto scan; 567 568 entry = xa_mk_value(idx); 569 for (i = 1; i < count; i++) { 570 ret = radix_tree_insert(&iter->radix, idx + i, entry); 571 if (ret && ret != -EEXIST) 572 goto scan; 573 } 574 575 idx += count; 576 sg = ____sg_next(sg); 577 count = __sg_page_count(sg); 578 } 579 580 scan: 581 iter->sg_pos = sg; 582 iter->sg_idx = idx; 583 584 mutex_unlock(&iter->lock); 585 586 if (unlikely(n < idx)) /* insertion completed by another thread */ 587 goto lookup; 588 589 /* In case we failed to insert the entry into the radixtree, we need 590 * to look beyond the current sg. 591 */ 592 while (idx + count <= n) { 593 idx += count; 594 sg = ____sg_next(sg); 595 count = __sg_page_count(sg); 596 } 597 598 *offset = n - idx; 599 return sg; 600 601 lookup: 602 rcu_read_lock(); 603 604 sg = radix_tree_lookup(&iter->radix, n); 605 GEM_BUG_ON(!sg); 606 607 /* If this index is in the middle of multi-page sg entry, 608 * the radix tree will contain a value entry that points 609 * to the start of that range. We will return the pointer to 610 * the base page and the offset of this page within the 611 * sg entry's range. 612 */ 613 *offset = 0; 614 if (unlikely(xa_is_value(sg))) { 615 unsigned long base = xa_to_value(sg); 616 617 sg = radix_tree_lookup(&iter->radix, base); 618 GEM_BUG_ON(!sg); 619 620 *offset = n - base; 621 } 622 623 rcu_read_unlock(); 624 625 return sg; 626 } 627 628 struct vm_page * 629 i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) 630 { 631 struct scatterlist *sg; 632 unsigned int offset; 633 634 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); 635 636 sg = i915_gem_object_get_sg(obj, n, &offset); 637 return nth_page(sg_page(sg), offset); 638 } 639 640 /* Like i915_gem_object_get_page(), but mark the returned page dirty */ 641 struct vm_page * 642 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, 643 unsigned int n) 644 { 645 struct vm_page *page; 646 647 page = i915_gem_object_get_page(obj, n); 648 if (!obj->mm.dirty) 649 set_page_dirty(page); 650 651 return page; 652 } 653 654 dma_addr_t 655 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj, 656 unsigned long n, 657 unsigned int *len) 658 { 659 struct scatterlist *sg; 660 unsigned int offset; 661 662 sg = i915_gem_object_get_sg(obj, n, &offset); 663 664 if (len) 665 *len = sg_dma_len(sg) - (offset << PAGE_SHIFT); 666 667 return sg_dma_address(sg) + (offset << PAGE_SHIFT); 668 } 669 670 dma_addr_t 671 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, 672 unsigned long n) 673 { 674 return i915_gem_object_get_dma_address_len(obj, n, NULL); 675 } 676