1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 /************************************************************************** 3 * 4 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 /* 29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 30 */ 31 32 #include <drm/ttm/ttm_bo_driver.h> 33 #include <drm/ttm/ttm_placement.h> 34 #include <drm/drm_vma_manager.h> 35 #include <linux/dma-buf-map.h> 36 #include <linux/io.h> 37 #include <linux/highmem.h> 38 #include <linux/wait.h> 39 #include <linux/slab.h> 40 #include <linux/vmalloc.h> 41 #include <linux/module.h> 42 #include <linux/dma-resv.h> 43 44 struct ttm_transfer_obj { 45 struct ttm_buffer_object base; 46 struct ttm_buffer_object *bo; 47 }; 48 49 int ttm_mem_io_reserve(struct ttm_device *bdev, 50 struct ttm_resource *mem) 51 { 52 if (mem->bus.offset || mem->bus.addr) 53 return 0; 54 55 mem->bus.is_iomem = false; 56 if (!bdev->funcs->io_mem_reserve) 57 return 0; 58 59 return bdev->funcs->io_mem_reserve(bdev, mem); 60 } 61 62 void ttm_mem_io_free(struct ttm_device *bdev, 63 struct ttm_resource *mem) 64 { 65 if (!mem->bus.offset && !mem->bus.addr) 66 return; 67 68 if (bdev->funcs->io_mem_free) 69 bdev->funcs->io_mem_free(bdev, mem); 70 71 mem->bus.offset = 0; 72 mem->bus.addr = NULL; 73 } 74 75 static int ttm_resource_ioremap(struct ttm_device *bdev, 76 struct ttm_resource *mem, 77 void **virtual) 78 { 79 int ret; 80 void *addr; 81 82 *virtual = NULL; 83 ret = ttm_mem_io_reserve(bdev, mem); 84 if (ret || !mem->bus.is_iomem) 85 return ret; 86 87 if (mem->bus.addr) { 88 addr = mem->bus.addr; 89 } else { 90 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT; 91 92 if (mem->bus.caching == ttm_write_combined) 93 addr = ioremap_wc(mem->bus.offset, bus_size); 94 #ifdef CONFIG_X86 95 else if (mem->bus.caching == ttm_cached) 96 addr = ioremap_cache(mem->bus.offset, bus_size); 97 #endif 98 else 99 addr = ioremap(mem->bus.offset, bus_size); 100 if (!addr) { 101 ttm_mem_io_free(bdev, mem); 102 return -ENOMEM; 103 } 104 } 105 *virtual = addr; 106 return 0; 107 } 108 109 static void ttm_resource_iounmap(struct ttm_device *bdev, 110 struct ttm_resource *mem, 111 void *virtual) 112 { 113 if (virtual && mem->bus.addr == NULL) 114 iounmap(virtual); 115 ttm_mem_io_free(bdev, mem); 116 } 117 118 static int ttm_copy_io_page(void *dst, void *src, unsigned long page) 119 { 120 uint32_t *dstP = 121 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT)); 122 uint32_t *srcP = 123 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT)); 124 125 int i; 126 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i) 127 iowrite32(ioread32(srcP++), dstP++); 128 return 0; 129 } 130 131 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src, 132 unsigned long page, 133 pgprot_t prot) 134 { 135 struct page *d = ttm->pages[page]; 136 void *dst; 137 138 if (!d) 139 return -ENOMEM; 140 141 src = (void *)((unsigned long)src + (page << PAGE_SHIFT)); 142 dst = kmap_atomic_prot(d, prot); 143 if (!dst) 144 return -ENOMEM; 145 146 memcpy_fromio(dst, src, PAGE_SIZE); 147 148 kunmap_atomic(dst); 149 150 return 0; 151 } 152 153 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst, 154 unsigned long page, 155 pgprot_t prot) 156 { 157 struct page *s = ttm->pages[page]; 158 void *src; 159 160 if (!s) 161 return -ENOMEM; 162 163 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT)); 164 src = kmap_atomic_prot(s, prot); 165 if (!src) 166 return -ENOMEM; 167 168 memcpy_toio(dst, src, PAGE_SIZE); 169 170 kunmap_atomic(src); 171 172 return 0; 173 } 174 175 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo, 176 struct ttm_operation_ctx *ctx, 177 struct ttm_resource *new_mem) 178 { 179 struct ttm_device *bdev = bo->bdev; 180 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type); 181 struct ttm_tt *ttm = bo->ttm; 182 struct ttm_resource *old_mem = bo->resource; 183 struct ttm_resource old_copy = *old_mem; 184 void *old_iomap; 185 void *new_iomap; 186 int ret; 187 unsigned long i; 188 189 ret = ttm_bo_wait_ctx(bo, ctx); 190 if (ret) 191 return ret; 192 193 ret = ttm_resource_ioremap(bdev, old_mem, &old_iomap); 194 if (ret) 195 return ret; 196 ret = ttm_resource_ioremap(bdev, new_mem, &new_iomap); 197 if (ret) 198 goto out; 199 200 /* 201 * Single TTM move. NOP. 202 */ 203 if (old_iomap == NULL && new_iomap == NULL) 204 goto out2; 205 206 /* 207 * Don't move nonexistent data. Clear destination instead. 208 */ 209 if (old_iomap == NULL && 210 (ttm == NULL || (!ttm_tt_is_populated(ttm) && 211 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) { 212 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE); 213 goto out2; 214 } 215 216 /* 217 * TTM might be null for moves within the same region. 218 */ 219 if (ttm) { 220 ret = ttm_tt_populate(bdev, ttm, ctx); 221 if (ret) 222 goto out1; 223 } 224 225 for (i = 0; i < new_mem->num_pages; ++i) { 226 if (old_iomap == NULL) { 227 pgprot_t prot = ttm_io_prot(bo, old_mem, PAGE_KERNEL); 228 ret = ttm_copy_ttm_io_page(ttm, new_iomap, i, 229 prot); 230 } else if (new_iomap == NULL) { 231 pgprot_t prot = ttm_io_prot(bo, new_mem, PAGE_KERNEL); 232 ret = ttm_copy_io_ttm_page(ttm, old_iomap, i, 233 prot); 234 } else { 235 ret = ttm_copy_io_page(new_iomap, old_iomap, i); 236 } 237 if (ret) 238 goto out1; 239 } 240 mb(); 241 out2: 242 old_copy = *old_mem; 243 244 ttm_bo_assign_mem(bo, new_mem); 245 246 if (!man->use_tt) 247 ttm_bo_tt_destroy(bo); 248 249 out1: 250 ttm_resource_iounmap(bdev, old_mem, new_iomap); 251 out: 252 ttm_resource_iounmap(bdev, &old_copy, old_iomap); 253 254 /* 255 * On error, keep the mm node! 256 */ 257 if (!ret) 258 ttm_resource_free(bo, &old_copy); 259 return ret; 260 } 261 EXPORT_SYMBOL(ttm_bo_move_memcpy); 262 263 static void ttm_transfered_destroy(struct ttm_buffer_object *bo) 264 { 265 struct ttm_transfer_obj *fbo; 266 267 fbo = container_of(bo, struct ttm_transfer_obj, base); 268 ttm_bo_put(fbo->bo); 269 kfree(fbo); 270 } 271 272 /** 273 * ttm_buffer_object_transfer 274 * 275 * @bo: A pointer to a struct ttm_buffer_object. 276 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object, 277 * holding the data of @bo with the old placement. 278 * 279 * This is a utility function that may be called after an accelerated move 280 * has been scheduled. A new buffer object is created as a placeholder for 281 * the old data while it's being copied. When that buffer object is idle, 282 * it can be destroyed, releasing the space of the old placement. 283 * Returns: 284 * !0: Failure. 285 */ 286 287 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo, 288 struct ttm_buffer_object **new_obj) 289 { 290 struct ttm_transfer_obj *fbo; 291 int ret; 292 293 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL); 294 if (!fbo) 295 return -ENOMEM; 296 297 fbo->base = *bo; 298 299 ttm_bo_get(bo); 300 fbo->bo = bo; 301 302 /** 303 * Fix up members that we shouldn't copy directly: 304 * TODO: Explicit member copy would probably be better here. 305 */ 306 307 atomic_inc(&ttm_glob.bo_count); 308 INIT_LIST_HEAD(&fbo->base.ddestroy); 309 INIT_LIST_HEAD(&fbo->base.lru); 310 fbo->base.moving = NULL; 311 drm_vma_node_reset(&fbo->base.base.vma_node); 312 313 kref_init(&fbo->base.kref); 314 fbo->base.destroy = &ttm_transfered_destroy; 315 fbo->base.pin_count = 0; 316 if (bo->type != ttm_bo_type_sg) 317 fbo->base.base.resv = &fbo->base.base._resv; 318 319 dma_resv_init(&fbo->base.base._resv); 320 fbo->base.base.dev = NULL; 321 ret = dma_resv_trylock(&fbo->base.base._resv); 322 WARN_ON(!ret); 323 324 ttm_bo_move_to_lru_tail_unlocked(&fbo->base); 325 326 *new_obj = &fbo->base; 327 return 0; 328 } 329 330 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res, 331 pgprot_t tmp) 332 { 333 struct ttm_resource_manager *man; 334 enum ttm_caching caching; 335 336 man = ttm_manager_type(bo->bdev, res->mem_type); 337 caching = man->use_tt ? bo->ttm->caching : res->bus.caching; 338 339 /* Cached mappings need no adjustment */ 340 if (caching == ttm_cached) 341 return tmp; 342 343 #if defined(__i386__) || defined(__x86_64__) 344 if (caching == ttm_write_combined) 345 tmp = pgprot_writecombine(tmp); 346 else if (boot_cpu_data.x86 > 3) 347 tmp = pgprot_noncached(tmp); 348 #endif 349 #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \ 350 defined(__powerpc__) || defined(__mips__) 351 if (caching == ttm_write_combined) 352 tmp = pgprot_writecombine(tmp); 353 else 354 tmp = pgprot_noncached(tmp); 355 #endif 356 #if defined(__sparc__) 357 tmp = pgprot_noncached(tmp); 358 #endif 359 return tmp; 360 } 361 EXPORT_SYMBOL(ttm_io_prot); 362 363 static int ttm_bo_ioremap(struct ttm_buffer_object *bo, 364 unsigned long offset, 365 unsigned long size, 366 struct ttm_bo_kmap_obj *map) 367 { 368 struct ttm_resource *mem = bo->resource; 369 370 if (bo->resource->bus.addr) { 371 map->bo_kmap_type = ttm_bo_map_premapped; 372 map->virtual = ((u8 *)bo->resource->bus.addr) + offset; 373 } else { 374 resource_size_t res = bo->resource->bus.offset + offset; 375 376 map->bo_kmap_type = ttm_bo_map_iomap; 377 if (mem->bus.caching == ttm_write_combined) 378 map->virtual = ioremap_wc(res, size); 379 #ifdef CONFIG_X86 380 else if (mem->bus.caching == ttm_cached) 381 map->virtual = ioremap_cache(res, size); 382 #endif 383 else 384 map->virtual = ioremap(res, size); 385 } 386 return (!map->virtual) ? -ENOMEM : 0; 387 } 388 389 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo, 390 unsigned long start_page, 391 unsigned long num_pages, 392 struct ttm_bo_kmap_obj *map) 393 { 394 struct ttm_resource *mem = bo->resource; 395 struct ttm_operation_ctx ctx = { 396 .interruptible = false, 397 .no_wait_gpu = false 398 }; 399 struct ttm_tt *ttm = bo->ttm; 400 pgprot_t prot; 401 int ret; 402 403 BUG_ON(!ttm); 404 405 ret = ttm_tt_populate(bo->bdev, ttm, &ctx); 406 if (ret) 407 return ret; 408 409 if (num_pages == 1 && ttm->caching == ttm_cached) { 410 /* 411 * We're mapping a single page, and the desired 412 * page protection is consistent with the bo. 413 */ 414 415 map->bo_kmap_type = ttm_bo_map_kmap; 416 map->page = ttm->pages[start_page]; 417 map->virtual = kmap(map->page); 418 } else { 419 /* 420 * We need to use vmap to get the desired page protection 421 * or to make the buffer object look contiguous. 422 */ 423 prot = ttm_io_prot(bo, mem, PAGE_KERNEL); 424 map->bo_kmap_type = ttm_bo_map_vmap; 425 map->virtual = vmap(ttm->pages + start_page, num_pages, 426 0, prot); 427 } 428 return (!map->virtual) ? -ENOMEM : 0; 429 } 430 431 int ttm_bo_kmap(struct ttm_buffer_object *bo, 432 unsigned long start_page, unsigned long num_pages, 433 struct ttm_bo_kmap_obj *map) 434 { 435 unsigned long offset, size; 436 int ret; 437 438 map->virtual = NULL; 439 map->bo = bo; 440 if (num_pages > bo->resource->num_pages) 441 return -EINVAL; 442 if ((start_page + num_pages) > bo->resource->num_pages) 443 return -EINVAL; 444 445 ret = ttm_mem_io_reserve(bo->bdev, bo->resource); 446 if (ret) 447 return ret; 448 if (!bo->resource->bus.is_iomem) { 449 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map); 450 } else { 451 offset = start_page << PAGE_SHIFT; 452 size = num_pages << PAGE_SHIFT; 453 return ttm_bo_ioremap(bo, offset, size, map); 454 } 455 } 456 EXPORT_SYMBOL(ttm_bo_kmap); 457 458 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map) 459 { 460 if (!map->virtual) 461 return; 462 switch (map->bo_kmap_type) { 463 case ttm_bo_map_iomap: 464 iounmap(map->virtual); 465 break; 466 case ttm_bo_map_vmap: 467 vunmap(map->virtual); 468 break; 469 case ttm_bo_map_kmap: 470 kunmap(map->page); 471 break; 472 case ttm_bo_map_premapped: 473 break; 474 default: 475 BUG(); 476 } 477 ttm_mem_io_free(map->bo->bdev, map->bo->resource); 478 map->virtual = NULL; 479 map->page = NULL; 480 } 481 EXPORT_SYMBOL(ttm_bo_kunmap); 482 483 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map) 484 { 485 struct ttm_resource *mem = bo->resource; 486 int ret; 487 488 ret = ttm_mem_io_reserve(bo->bdev, mem); 489 if (ret) 490 return ret; 491 492 if (mem->bus.is_iomem) { 493 void __iomem *vaddr_iomem; 494 495 if (mem->bus.addr) 496 vaddr_iomem = (void __iomem *)mem->bus.addr; 497 else if (mem->bus.caching == ttm_write_combined) 498 vaddr_iomem = ioremap_wc(mem->bus.offset, 499 bo->base.size); 500 #ifdef CONFIG_X86 501 else if (mem->bus.caching == ttm_cached) 502 vaddr_iomem = ioremap_cache(mem->bus.offset, 503 bo->base.size); 504 #endif 505 else 506 vaddr_iomem = ioremap(mem->bus.offset, bo->base.size); 507 508 if (!vaddr_iomem) 509 return -ENOMEM; 510 511 dma_buf_map_set_vaddr_iomem(map, vaddr_iomem); 512 513 } else { 514 struct ttm_operation_ctx ctx = { 515 .interruptible = false, 516 .no_wait_gpu = false 517 }; 518 struct ttm_tt *ttm = bo->ttm; 519 pgprot_t prot; 520 void *vaddr; 521 522 ret = ttm_tt_populate(bo->bdev, ttm, &ctx); 523 if (ret) 524 return ret; 525 526 /* 527 * We need to use vmap to get the desired page protection 528 * or to make the buffer object look contiguous. 529 */ 530 prot = ttm_io_prot(bo, mem, PAGE_KERNEL); 531 vaddr = vmap(ttm->pages, ttm->num_pages, 0, prot); 532 if (!vaddr) 533 return -ENOMEM; 534 535 dma_buf_map_set_vaddr(map, vaddr); 536 } 537 538 return 0; 539 } 540 EXPORT_SYMBOL(ttm_bo_vmap); 541 542 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map) 543 { 544 struct ttm_resource *mem = bo->resource; 545 546 if (dma_buf_map_is_null(map)) 547 return; 548 549 if (!map->is_iomem) 550 vunmap(map->vaddr); 551 else if (!mem->bus.addr) 552 iounmap(map->vaddr_iomem); 553 dma_buf_map_clear(map); 554 555 ttm_mem_io_free(bo->bdev, bo->resource); 556 } 557 EXPORT_SYMBOL(ttm_bo_vunmap); 558 559 static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo, 560 bool dst_use_tt) 561 { 562 int ret; 563 ret = ttm_bo_wait(bo, false, false); 564 if (ret) 565 return ret; 566 567 if (!dst_use_tt) 568 ttm_bo_tt_destroy(bo); 569 ttm_resource_free(bo, bo->resource); 570 return 0; 571 } 572 573 static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo, 574 struct dma_fence *fence, 575 bool dst_use_tt) 576 { 577 struct ttm_buffer_object *ghost_obj; 578 int ret; 579 580 /** 581 * This should help pipeline ordinary buffer moves. 582 * 583 * Hang old buffer memory on a new buffer object, 584 * and leave it to be released when the GPU 585 * operation has completed. 586 */ 587 588 dma_fence_put(bo->moving); 589 bo->moving = dma_fence_get(fence); 590 591 ret = ttm_buffer_object_transfer(bo, &ghost_obj); 592 if (ret) 593 return ret; 594 595 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence); 596 597 /** 598 * If we're not moving to fixed memory, the TTM object 599 * needs to stay alive. Otherwhise hang it on the ghost 600 * bo to be unbound and destroyed. 601 */ 602 603 if (dst_use_tt) 604 ghost_obj->ttm = NULL; 605 else 606 bo->ttm = NULL; 607 608 dma_resv_unlock(&ghost_obj->base._resv); 609 ttm_bo_put(ghost_obj); 610 return 0; 611 } 612 613 static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo, 614 struct dma_fence *fence) 615 { 616 struct ttm_device *bdev = bo->bdev; 617 struct ttm_resource_manager *from; 618 619 from = ttm_manager_type(bdev, bo->resource->mem_type); 620 621 /** 622 * BO doesn't have a TTM we need to bind/unbind. Just remember 623 * this eviction and free up the allocation 624 */ 625 spin_lock(&from->move_lock); 626 if (!from->move || dma_fence_is_later(fence, from->move)) { 627 dma_fence_put(from->move); 628 from->move = dma_fence_get(fence); 629 } 630 spin_unlock(&from->move_lock); 631 632 ttm_resource_free(bo, bo->resource); 633 634 dma_fence_put(bo->moving); 635 bo->moving = dma_fence_get(fence); 636 } 637 638 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo, 639 struct dma_fence *fence, 640 bool evict, 641 bool pipeline, 642 struct ttm_resource *new_mem) 643 { 644 struct ttm_device *bdev = bo->bdev; 645 struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->resource->mem_type); 646 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type); 647 int ret = 0; 648 649 dma_resv_add_excl_fence(bo->base.resv, fence); 650 if (!evict) 651 ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt); 652 else if (!from->use_tt && pipeline) 653 ttm_bo_move_pipeline_evict(bo, fence); 654 else 655 ret = ttm_bo_wait_free_node(bo, man->use_tt); 656 657 if (ret) 658 return ret; 659 660 ttm_bo_assign_mem(bo, new_mem); 661 662 return 0; 663 } 664 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup); 665 666 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo) 667 { 668 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM }; 669 struct ttm_buffer_object *ghost; 670 int ret; 671 672 ret = ttm_buffer_object_transfer(bo, &ghost); 673 if (ret) 674 return ret; 675 676 ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv); 677 /* Last resort, wait for the BO to be idle when we are OOM */ 678 if (ret) 679 ttm_bo_wait(bo, false, false); 680 681 ttm_resource_alloc(bo, &sys_mem, bo->resource); 682 bo->ttm = NULL; 683 684 dma_resv_unlock(&ghost->base._resv); 685 ttm_bo_put(ghost); 686 687 return 0; 688 } 689