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/io.h> 36 #include <linux/highmem.h> 37 #include <linux/wait.h> 38 #include <linux/slab.h> 39 #include <linux/vmalloc.h> 40 #include <linux/module.h> 41 #include <linux/dma-resv.h> 42 43 struct ttm_transfer_obj { 44 struct ttm_buffer_object base; 45 struct ttm_buffer_object *bo; 46 }; 47 48 void ttm_bo_free_old_node(struct ttm_buffer_object *bo) 49 { 50 ttm_bo_mem_put(bo, &bo->mem); 51 } 52 53 int ttm_bo_move_ttm(struct ttm_buffer_object *bo, 54 struct ttm_operation_ctx *ctx, 55 struct ttm_mem_reg *new_mem) 56 { 57 struct ttm_tt *ttm = bo->ttm; 58 struct ttm_mem_reg *old_mem = &bo->mem; 59 int ret; 60 61 if (old_mem->mem_type != TTM_PL_SYSTEM) { 62 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu); 63 64 if (unlikely(ret != 0)) { 65 if (ret != -ERESTARTSYS) 66 pr_err("Failed to expire sync object before unbinding TTM\n"); 67 return ret; 68 } 69 70 ttm_tt_unbind(ttm); 71 ttm_bo_free_old_node(bo); 72 ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM, 73 TTM_PL_MASK_MEM); 74 old_mem->mem_type = TTM_PL_SYSTEM; 75 } 76 77 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement); 78 if (unlikely(ret != 0)) 79 return ret; 80 81 if (new_mem->mem_type != TTM_PL_SYSTEM) { 82 ret = ttm_tt_bind(ttm, new_mem, ctx); 83 if (unlikely(ret != 0)) 84 return ret; 85 } 86 87 *old_mem = *new_mem; 88 new_mem->mm_node = NULL; 89 90 return 0; 91 } 92 EXPORT_SYMBOL(ttm_bo_move_ttm); 93 94 int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible) 95 { 96 if (likely(man->io_reserve_fastpath)) 97 return 0; 98 99 if (interruptible) 100 return mutex_lock_interruptible(&man->io_reserve_mutex); 101 102 mutex_lock(&man->io_reserve_mutex); 103 return 0; 104 } 105 106 void ttm_mem_io_unlock(struct ttm_mem_type_manager *man) 107 { 108 if (likely(man->io_reserve_fastpath)) 109 return; 110 111 mutex_unlock(&man->io_reserve_mutex); 112 } 113 114 static int ttm_mem_io_evict(struct ttm_mem_type_manager *man) 115 { 116 struct ttm_buffer_object *bo; 117 118 if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru)) 119 return -EAGAIN; 120 121 bo = list_first_entry(&man->io_reserve_lru, 122 struct ttm_buffer_object, 123 io_reserve_lru); 124 list_del_init(&bo->io_reserve_lru); 125 ttm_bo_unmap_virtual_locked(bo); 126 127 return 0; 128 } 129 130 131 int ttm_mem_io_reserve(struct ttm_bo_device *bdev, 132 struct ttm_mem_reg *mem) 133 { 134 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 135 int ret = 0; 136 137 if (!bdev->driver->io_mem_reserve) 138 return 0; 139 if (likely(man->io_reserve_fastpath)) 140 return bdev->driver->io_mem_reserve(bdev, mem); 141 142 if (bdev->driver->io_mem_reserve && 143 mem->bus.io_reserved_count++ == 0) { 144 retry: 145 ret = bdev->driver->io_mem_reserve(bdev, mem); 146 if (ret == -EAGAIN) { 147 ret = ttm_mem_io_evict(man); 148 if (ret == 0) 149 goto retry; 150 } 151 } 152 return ret; 153 } 154 155 void ttm_mem_io_free(struct ttm_bo_device *bdev, 156 struct ttm_mem_reg *mem) 157 { 158 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 159 160 if (likely(man->io_reserve_fastpath)) 161 return; 162 163 if (bdev->driver->io_mem_reserve && 164 --mem->bus.io_reserved_count == 0 && 165 bdev->driver->io_mem_free) 166 bdev->driver->io_mem_free(bdev, mem); 167 168 } 169 170 int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo) 171 { 172 struct ttm_mem_reg *mem = &bo->mem; 173 int ret; 174 175 if (!mem->bus.io_reserved_vm) { 176 struct ttm_mem_type_manager *man = 177 &bo->bdev->man[mem->mem_type]; 178 179 ret = ttm_mem_io_reserve(bo->bdev, mem); 180 if (unlikely(ret != 0)) 181 return ret; 182 mem->bus.io_reserved_vm = true; 183 if (man->use_io_reserve_lru) 184 list_add_tail(&bo->io_reserve_lru, 185 &man->io_reserve_lru); 186 } 187 return 0; 188 } 189 190 void ttm_mem_io_free_vm(struct ttm_buffer_object *bo) 191 { 192 struct ttm_mem_reg *mem = &bo->mem; 193 194 if (mem->bus.io_reserved_vm) { 195 mem->bus.io_reserved_vm = false; 196 list_del_init(&bo->io_reserve_lru); 197 ttm_mem_io_free(bo->bdev, mem); 198 } 199 } 200 201 static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem, 202 void **virtual) 203 { 204 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 205 int ret; 206 void *addr; 207 int flags; 208 209 *virtual = NULL; 210 (void) ttm_mem_io_lock(man, false); 211 ret = ttm_mem_io_reserve(bdev, mem); 212 ttm_mem_io_unlock(man); 213 if (ret || !mem->bus.is_iomem) 214 return ret; 215 216 if (mem->bus.addr) { 217 addr = mem->bus.addr; 218 } else { 219 if (mem->placement & TTM_PL_FLAG_WC) 220 flags = BUS_SPACE_MAP_PREFETCHABLE; 221 else 222 flags = 0; 223 224 if (bus_space_map(bdev->memt, mem->bus.base + mem->bus.offset, 225 mem->bus.size, BUS_SPACE_MAP_LINEAR | flags, 226 &mem->bus.bsh)) { 227 printf("%s bus_space_map failed\n", __func__); 228 return -ENOMEM; 229 } 230 231 addr = bus_space_vaddr(bdev->memt, mem->bus.bsh); 232 233 if (!addr) { 234 (void) ttm_mem_io_lock(man, false); 235 ttm_mem_io_free(bdev, mem); 236 ttm_mem_io_unlock(man); 237 return -ENOMEM; 238 } 239 } 240 *virtual = addr; 241 return 0; 242 } 243 244 static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem, 245 void *virtual) 246 { 247 struct ttm_mem_type_manager *man; 248 249 man = &bdev->man[mem->mem_type]; 250 251 if (virtual && mem->bus.addr == NULL) 252 bus_space_unmap(bdev->memt, mem->bus.bsh, mem->bus.size); 253 (void) ttm_mem_io_lock(man, false); 254 ttm_mem_io_free(bdev, mem); 255 ttm_mem_io_unlock(man); 256 } 257 258 static int ttm_copy_io_page(void *dst, void *src, unsigned long page) 259 { 260 uint32_t *dstP = 261 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT)); 262 uint32_t *srcP = 263 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT)); 264 265 int i; 266 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i) 267 iowrite32(ioread32(srcP++), dstP++); 268 return 0; 269 } 270 271 #if defined(CONFIG_X86) && defined(__linux__) 272 #define __ttm_kmap_atomic_prot(__page, __prot) kmap_atomic_prot(__page, __prot) 273 #define __ttm_kunmap_atomic(__addr) kunmap_atomic(__addr) 274 #else 275 #define __ttm_kmap_atomic_prot(__page, __prot) vmap(&__page, 1, 0, __prot) 276 #define __ttm_kunmap_atomic(__addr) vunmap(__addr, PAGE_SIZE) 277 #endif 278 279 280 /** 281 * ttm_kmap_atomic_prot - Efficient kernel map of a single page with 282 * specified page protection. 283 * 284 * @page: The page to map. 285 * @prot: The page protection. 286 * 287 * This function maps a TTM page using the kmap_atomic api if available, 288 * otherwise falls back to vmap. The user must make sure that the 289 * specified page does not have an aliased mapping with a different caching 290 * policy unless the architecture explicitly allows it. Also mapping and 291 * unmapping using this api must be correctly nested. Unmapping should 292 * occur in the reverse order of mapping. 293 */ 294 void *ttm_kmap_atomic_prot(struct vm_page *page, pgprot_t prot) 295 { 296 #if defined(__amd64__) || defined(__i386__) 297 if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) 298 return kmap_atomic(page); 299 else 300 #endif 301 return __ttm_kmap_atomic_prot(page, prot); 302 } 303 EXPORT_SYMBOL(ttm_kmap_atomic_prot); 304 305 /** 306 * ttm_kunmap_atomic_prot - Unmap a page that was mapped using 307 * ttm_kmap_atomic_prot. 308 * 309 * @addr: The virtual address from the map. 310 * @prot: The page protection. 311 */ 312 void ttm_kunmap_atomic_prot(void *addr, pgprot_t prot) 313 { 314 #if defined(__amd64__) || defined(__i386__) 315 if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) 316 kunmap_atomic(addr); 317 else 318 #endif 319 __ttm_kunmap_atomic(addr); 320 } 321 EXPORT_SYMBOL(ttm_kunmap_atomic_prot); 322 323 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src, 324 unsigned long page, 325 pgprot_t prot) 326 { 327 struct vm_page *d = ttm->pages[page]; 328 void *dst; 329 330 if (!d) 331 return -ENOMEM; 332 333 src = (void *)((unsigned long)src + (page << PAGE_SHIFT)); 334 dst = ttm_kmap_atomic_prot(d, prot); 335 if (!dst) 336 return -ENOMEM; 337 338 memcpy_fromio(dst, src, PAGE_SIZE); 339 340 ttm_kunmap_atomic_prot(dst, prot); 341 342 return 0; 343 } 344 345 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst, 346 unsigned long page, 347 pgprot_t prot) 348 { 349 struct vm_page *s = ttm->pages[page]; 350 void *src; 351 352 if (!s) 353 return -ENOMEM; 354 355 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT)); 356 src = ttm_kmap_atomic_prot(s, prot); 357 if (!src) 358 return -ENOMEM; 359 360 memcpy_toio(dst, src, PAGE_SIZE); 361 362 ttm_kunmap_atomic_prot(src, prot); 363 364 return 0; 365 } 366 367 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo, 368 struct ttm_operation_ctx *ctx, 369 struct ttm_mem_reg *new_mem) 370 { 371 struct ttm_bo_device *bdev = bo->bdev; 372 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type]; 373 struct ttm_tt *ttm = bo->ttm; 374 struct ttm_mem_reg *old_mem = &bo->mem; 375 struct ttm_mem_reg old_copy = *old_mem; 376 void *old_iomap; 377 void *new_iomap; 378 int ret; 379 unsigned long i; 380 unsigned long page; 381 unsigned long add = 0; 382 int dir; 383 384 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu); 385 if (ret) 386 return ret; 387 388 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap); 389 if (ret) 390 return ret; 391 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap); 392 if (ret) 393 goto out; 394 395 /* 396 * Single TTM move. NOP. 397 */ 398 if (old_iomap == NULL && new_iomap == NULL) 399 goto out2; 400 401 /* 402 * Don't move nonexistent data. Clear destination instead. 403 */ 404 if (old_iomap == NULL && 405 (ttm == NULL || (ttm->state == tt_unpopulated && 406 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) { 407 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE); 408 goto out2; 409 } 410 411 /* 412 * TTM might be null for moves within the same region. 413 */ 414 if (ttm) { 415 ret = ttm_tt_populate(ttm, ctx); 416 if (ret) 417 goto out1; 418 } 419 420 add = 0; 421 dir = 1; 422 423 if ((old_mem->mem_type == new_mem->mem_type) && 424 (new_mem->start < old_mem->start + old_mem->size)) { 425 dir = -1; 426 add = new_mem->num_pages - 1; 427 } 428 429 for (i = 0; i < new_mem->num_pages; ++i) { 430 page = i * dir + add; 431 if (old_iomap == NULL) { 432 pgprot_t prot = ttm_io_prot(old_mem->placement, 433 PAGE_KERNEL); 434 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page, 435 prot); 436 } else if (new_iomap == NULL) { 437 pgprot_t prot = ttm_io_prot(new_mem->placement, 438 PAGE_KERNEL); 439 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page, 440 prot); 441 } else { 442 ret = ttm_copy_io_page(new_iomap, old_iomap, page); 443 } 444 if (ret) 445 goto out1; 446 } 447 mb(); 448 out2: 449 old_copy = *old_mem; 450 *old_mem = *new_mem; 451 new_mem->mm_node = NULL; 452 453 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) { 454 ttm_tt_destroy(ttm); 455 bo->ttm = NULL; 456 } 457 458 out1: 459 ttm_mem_reg_iounmap(bdev, old_mem, new_iomap); 460 out: 461 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap); 462 463 /* 464 * On error, keep the mm node! 465 */ 466 if (!ret) 467 ttm_bo_mem_put(bo, &old_copy); 468 return ret; 469 } 470 EXPORT_SYMBOL(ttm_bo_move_memcpy); 471 472 static void ttm_transfered_destroy(struct ttm_buffer_object *bo) 473 { 474 struct ttm_transfer_obj *fbo; 475 476 fbo = container_of(bo, struct ttm_transfer_obj, base); 477 ttm_bo_put(fbo->bo); 478 kfree(fbo); 479 } 480 481 /** 482 * ttm_buffer_object_transfer 483 * 484 * @bo: A pointer to a struct ttm_buffer_object. 485 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object, 486 * holding the data of @bo with the old placement. 487 * 488 * This is a utility function that may be called after an accelerated move 489 * has been scheduled. A new buffer object is created as a placeholder for 490 * the old data while it's being copied. When that buffer object is idle, 491 * it can be destroyed, releasing the space of the old placement. 492 * Returns: 493 * !0: Failure. 494 */ 495 496 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo, 497 struct ttm_buffer_object **new_obj) 498 { 499 struct ttm_transfer_obj *fbo; 500 int ret; 501 502 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL); 503 if (!fbo) 504 return -ENOMEM; 505 506 fbo->base = *bo; 507 fbo->base.mem.placement |= TTM_PL_FLAG_NO_EVICT; 508 509 ttm_bo_get(bo); 510 fbo->bo = bo; 511 512 /** 513 * Fix up members that we shouldn't copy directly: 514 * TODO: Explicit member copy would probably be better here. 515 */ 516 517 atomic_inc(&ttm_bo_glob.bo_count); 518 INIT_LIST_HEAD(&fbo->base.ddestroy); 519 INIT_LIST_HEAD(&fbo->base.lru); 520 INIT_LIST_HEAD(&fbo->base.swap); 521 INIT_LIST_HEAD(&fbo->base.io_reserve_lru); 522 fbo->base.moving = NULL; 523 drm_vma_node_reset(&fbo->base.base.vma_node); 524 525 kref_init(&fbo->base.kref); 526 fbo->base.destroy = &ttm_transfered_destroy; 527 fbo->base.acc_size = 0; 528 if (bo->type != ttm_bo_type_sg) 529 fbo->base.base.resv = &fbo->base.base._resv; 530 531 dma_resv_init(&fbo->base.base._resv); 532 fbo->base.base.dev = NULL; 533 ret = dma_resv_trylock(&fbo->base.base._resv); 534 WARN_ON(!ret); 535 536 *new_obj = &fbo->base; 537 return 0; 538 } 539 540 #ifdef __linux__ 541 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp) 542 { 543 /* Cached mappings need no adjustment */ 544 if (caching_flags & TTM_PL_FLAG_CACHED) 545 return tmp; 546 547 #if defined(__i386__) || defined(__x86_64__) 548 if (caching_flags & TTM_PL_FLAG_WC) 549 tmp = pgprot_writecombine(tmp); 550 else if (boot_cpu_data.x86 > 3) 551 tmp = pgprot_noncached(tmp); 552 #endif 553 #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \ 554 defined(__powerpc__) || defined(__mips__) 555 if (caching_flags & TTM_PL_FLAG_WC) 556 tmp = pgprot_writecombine(tmp); 557 else 558 tmp = pgprot_noncached(tmp); 559 #endif 560 #if defined(__sparc__) 561 tmp = pgprot_noncached(tmp); 562 #endif 563 return tmp; 564 } 565 EXPORT_SYMBOL(ttm_io_prot); 566 #endif 567 568 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp) 569 { 570 /* Cached mappings need no adjustment */ 571 if (caching_flags & TTM_PL_FLAG_CACHED) 572 return tmp; 573 574 if (caching_flags & TTM_PL_FLAG_WC) 575 tmp = pgprot_writecombine(tmp); 576 else 577 tmp = pgprot_noncached(tmp); 578 579 return tmp; 580 } 581 582 static int ttm_bo_ioremap(struct ttm_buffer_object *bo, 583 unsigned long offset, 584 unsigned long size, 585 struct ttm_bo_kmap_obj *map) 586 { 587 int flags; 588 struct ttm_mem_reg *mem = &bo->mem; 589 590 if (bo->mem.bus.addr) { 591 map->bo_kmap_type = ttm_bo_map_premapped; 592 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset); 593 } else { 594 map->bo_kmap_type = ttm_bo_map_iomap; 595 if (mem->placement & TTM_PL_FLAG_WC) 596 flags = BUS_SPACE_MAP_PREFETCHABLE; 597 else 598 flags = 0; 599 600 if (bus_space_map(bo->bdev->memt, 601 mem->bus.base + bo->mem.bus.offset + offset, 602 size, BUS_SPACE_MAP_LINEAR | flags, 603 &bo->mem.bus.bsh)) { 604 printf("%s bus_space_map failed\n", __func__); 605 map->virtual = 0; 606 } else 607 map->virtual = bus_space_vaddr(bo->bdev->memt, 608 bo->mem.bus.bsh); 609 } 610 return (!map->virtual) ? -ENOMEM : 0; 611 } 612 613 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo, 614 unsigned long start_page, 615 unsigned long num_pages, 616 struct ttm_bo_kmap_obj *map) 617 { 618 struct ttm_mem_reg *mem = &bo->mem; 619 struct ttm_operation_ctx ctx = { 620 .interruptible = false, 621 .no_wait_gpu = false 622 }; 623 struct ttm_tt *ttm = bo->ttm; 624 pgprot_t prot; 625 int ret; 626 627 BUG_ON(!ttm); 628 629 ret = ttm_tt_populate(ttm, &ctx); 630 if (ret) 631 return ret; 632 633 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) { 634 /* 635 * We're mapping a single page, and the desired 636 * page protection is consistent with the bo. 637 */ 638 639 map->bo_kmap_type = ttm_bo_map_kmap; 640 map->page = ttm->pages[start_page]; 641 map->virtual = kmap(map->page); 642 } else { 643 /* 644 * We need to use vmap to get the desired page protection 645 * or to make the buffer object look contiguous. 646 */ 647 prot = ttm_io_prot(mem->placement, PAGE_KERNEL); 648 map->bo_kmap_type = ttm_bo_map_vmap; 649 map->virtual = vmap(ttm->pages + start_page, num_pages, 650 0, prot); 651 } 652 return (!map->virtual) ? -ENOMEM : 0; 653 } 654 655 int ttm_bo_kmap(struct ttm_buffer_object *bo, 656 unsigned long start_page, unsigned long num_pages, 657 struct ttm_bo_kmap_obj *map) 658 { 659 struct ttm_mem_type_manager *man = 660 &bo->bdev->man[bo->mem.mem_type]; 661 unsigned long offset, size; 662 int ret; 663 664 map->virtual = NULL; 665 map->bo = bo; 666 if (num_pages > bo->num_pages) 667 return -EINVAL; 668 if (start_page > bo->num_pages) 669 return -EINVAL; 670 671 (void) ttm_mem_io_lock(man, false); 672 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem); 673 ttm_mem_io_unlock(man); 674 if (ret) 675 return ret; 676 if (!bo->mem.bus.is_iomem) { 677 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map); 678 } else { 679 offset = start_page << PAGE_SHIFT; 680 size = num_pages << PAGE_SHIFT; 681 return ttm_bo_ioremap(bo, offset, size, map); 682 } 683 } 684 EXPORT_SYMBOL(ttm_bo_kmap); 685 686 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map) 687 { 688 struct ttm_buffer_object *bo = map->bo; 689 struct ttm_mem_type_manager *man = 690 &bo->bdev->man[bo->mem.mem_type]; 691 692 if (!map->virtual) 693 return; 694 switch (map->bo_kmap_type) { 695 case ttm_bo_map_iomap: 696 bus_space_unmap(bo->bdev->memt, bo->mem.bus.bsh, 697 bo->mem.bus.size); 698 break; 699 case ttm_bo_map_vmap: 700 vunmap(map->virtual, bo->mem.bus.size); 701 break; 702 case ttm_bo_map_kmap: 703 kunmap_va(map->virtual); 704 break; 705 case ttm_bo_map_premapped: 706 break; 707 default: 708 BUG(); 709 } 710 (void) ttm_mem_io_lock(man, false); 711 ttm_mem_io_free(map->bo->bdev, &map->bo->mem); 712 ttm_mem_io_unlock(man); 713 map->virtual = NULL; 714 map->page = NULL; 715 } 716 EXPORT_SYMBOL(ttm_bo_kunmap); 717 718 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo, 719 struct dma_fence *fence, 720 bool evict, 721 struct ttm_mem_reg *new_mem) 722 { 723 struct ttm_bo_device *bdev = bo->bdev; 724 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type]; 725 struct ttm_mem_reg *old_mem = &bo->mem; 726 int ret; 727 struct ttm_buffer_object *ghost_obj; 728 729 dma_resv_add_excl_fence(bo->base.resv, fence); 730 if (evict) { 731 ret = ttm_bo_wait(bo, false, false); 732 if (ret) 733 return ret; 734 735 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) { 736 ttm_tt_destroy(bo->ttm); 737 bo->ttm = NULL; 738 } 739 ttm_bo_free_old_node(bo); 740 } else { 741 /** 742 * This should help pipeline ordinary buffer moves. 743 * 744 * Hang old buffer memory on a new buffer object, 745 * and leave it to be released when the GPU 746 * operation has completed. 747 */ 748 749 dma_fence_put(bo->moving); 750 bo->moving = dma_fence_get(fence); 751 752 ret = ttm_buffer_object_transfer(bo, &ghost_obj); 753 if (ret) 754 return ret; 755 756 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence); 757 758 /** 759 * If we're not moving to fixed memory, the TTM object 760 * needs to stay alive. Otherwhise hang it on the ghost 761 * bo to be unbound and destroyed. 762 */ 763 764 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) 765 ghost_obj->ttm = NULL; 766 else 767 bo->ttm = NULL; 768 769 dma_resv_unlock(&ghost_obj->base._resv); 770 ttm_bo_put(ghost_obj); 771 } 772 773 *old_mem = *new_mem; 774 new_mem->mm_node = NULL; 775 776 return 0; 777 } 778 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup); 779 780 int ttm_bo_pipeline_move(struct ttm_buffer_object *bo, 781 struct dma_fence *fence, bool evict, 782 struct ttm_mem_reg *new_mem) 783 { 784 struct ttm_bo_device *bdev = bo->bdev; 785 struct ttm_mem_reg *old_mem = &bo->mem; 786 787 struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type]; 788 struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type]; 789 790 int ret; 791 792 dma_resv_add_excl_fence(bo->base.resv, fence); 793 794 if (!evict) { 795 struct ttm_buffer_object *ghost_obj; 796 797 /** 798 * This should help pipeline ordinary buffer moves. 799 * 800 * Hang old buffer memory on a new buffer object, 801 * and leave it to be released when the GPU 802 * operation has completed. 803 */ 804 805 dma_fence_put(bo->moving); 806 bo->moving = dma_fence_get(fence); 807 808 ret = ttm_buffer_object_transfer(bo, &ghost_obj); 809 if (ret) 810 return ret; 811 812 dma_resv_add_excl_fence(&ghost_obj->base._resv, fence); 813 814 /** 815 * If we're not moving to fixed memory, the TTM object 816 * needs to stay alive. Otherwhise hang it on the ghost 817 * bo to be unbound and destroyed. 818 */ 819 820 if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED)) 821 ghost_obj->ttm = NULL; 822 else 823 bo->ttm = NULL; 824 825 dma_resv_unlock(&ghost_obj->base._resv); 826 ttm_bo_put(ghost_obj); 827 828 } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) { 829 830 /** 831 * BO doesn't have a TTM we need to bind/unbind. Just remember 832 * this eviction and free up the allocation 833 */ 834 835 spin_lock(&from->move_lock); 836 if (!from->move || dma_fence_is_later(fence, from->move)) { 837 dma_fence_put(from->move); 838 from->move = dma_fence_get(fence); 839 } 840 spin_unlock(&from->move_lock); 841 842 ttm_bo_free_old_node(bo); 843 844 dma_fence_put(bo->moving); 845 bo->moving = dma_fence_get(fence); 846 847 } else { 848 /** 849 * Last resort, wait for the move to be completed. 850 * 851 * Should never happen in pratice. 852 */ 853 854 ret = ttm_bo_wait(bo, false, false); 855 if (ret) 856 return ret; 857 858 if (to->flags & TTM_MEMTYPE_FLAG_FIXED) { 859 ttm_tt_destroy(bo->ttm); 860 bo->ttm = NULL; 861 } 862 ttm_bo_free_old_node(bo); 863 } 864 865 *old_mem = *new_mem; 866 new_mem->mm_node = NULL; 867 868 return 0; 869 } 870 EXPORT_SYMBOL(ttm_bo_pipeline_move); 871 872 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo) 873 { 874 struct ttm_buffer_object *ghost; 875 int ret; 876 877 ret = ttm_buffer_object_transfer(bo, &ghost); 878 if (ret) 879 return ret; 880 881 ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv); 882 /* Last resort, wait for the BO to be idle when we are OOM */ 883 if (ret) 884 ttm_bo_wait(bo, false, false); 885 886 memset(&bo->mem, 0, sizeof(bo->mem)); 887 bo->mem.mem_type = TTM_PL_SYSTEM; 888 bo->ttm = NULL; 889 890 dma_resv_unlock(&ghost->base._resv); 891 ttm_bo_put(ghost); 892 893 return 0; 894 } 895