1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 #include "vmwgfx_drv.h" 29 #include <drm/ttm/ttm_bo_driver.h> 30 #include <drm/ttm/ttm_placement.h> 31 32 static const struct ttm_place vram_placement_flags = { 33 .fpfn = 0, 34 .lpfn = 0, 35 .mem_type = TTM_PL_VRAM, 36 .flags = 0 37 }; 38 39 static const struct ttm_place sys_placement_flags = { 40 .fpfn = 0, 41 .lpfn = 0, 42 .mem_type = TTM_PL_SYSTEM, 43 .flags = 0 44 }; 45 46 static const struct ttm_place gmr_placement_flags = { 47 .fpfn = 0, 48 .lpfn = 0, 49 .mem_type = VMW_PL_GMR, 50 .flags = 0 51 }; 52 53 static const struct ttm_place mob_placement_flags = { 54 .fpfn = 0, 55 .lpfn = 0, 56 .mem_type = VMW_PL_MOB, 57 .flags = 0 58 }; 59 60 struct ttm_placement vmw_vram_placement = { 61 .num_placement = 1, 62 .placement = &vram_placement_flags, 63 .num_busy_placement = 1, 64 .busy_placement = &vram_placement_flags 65 }; 66 67 static const struct ttm_place vram_gmr_placement_flags[] = { 68 { 69 .fpfn = 0, 70 .lpfn = 0, 71 .mem_type = TTM_PL_VRAM, 72 .flags = 0 73 }, { 74 .fpfn = 0, 75 .lpfn = 0, 76 .mem_type = VMW_PL_GMR, 77 .flags = 0 78 } 79 }; 80 81 static const struct ttm_place gmr_vram_placement_flags[] = { 82 { 83 .fpfn = 0, 84 .lpfn = 0, 85 .mem_type = VMW_PL_GMR, 86 .flags = 0 87 }, { 88 .fpfn = 0, 89 .lpfn = 0, 90 .mem_type = TTM_PL_VRAM, 91 .flags = 0 92 } 93 }; 94 95 struct ttm_placement vmw_vram_gmr_placement = { 96 .num_placement = 2, 97 .placement = vram_gmr_placement_flags, 98 .num_busy_placement = 1, 99 .busy_placement = &gmr_placement_flags 100 }; 101 102 struct ttm_placement vmw_vram_sys_placement = { 103 .num_placement = 1, 104 .placement = &vram_placement_flags, 105 .num_busy_placement = 1, 106 .busy_placement = &sys_placement_flags 107 }; 108 109 struct ttm_placement vmw_sys_placement = { 110 .num_placement = 1, 111 .placement = &sys_placement_flags, 112 .num_busy_placement = 1, 113 .busy_placement = &sys_placement_flags 114 }; 115 116 static const struct ttm_place evictable_placement_flags[] = { 117 { 118 .fpfn = 0, 119 .lpfn = 0, 120 .mem_type = TTM_PL_SYSTEM, 121 .flags = 0 122 }, { 123 .fpfn = 0, 124 .lpfn = 0, 125 .mem_type = TTM_PL_VRAM, 126 .flags = 0 127 }, { 128 .fpfn = 0, 129 .lpfn = 0, 130 .mem_type = VMW_PL_GMR, 131 .flags = 0 132 }, { 133 .fpfn = 0, 134 .lpfn = 0, 135 .mem_type = VMW_PL_MOB, 136 .flags = 0 137 } 138 }; 139 140 static const struct ttm_place nonfixed_placement_flags[] = { 141 { 142 .fpfn = 0, 143 .lpfn = 0, 144 .mem_type = TTM_PL_SYSTEM, 145 .flags = 0 146 }, { 147 .fpfn = 0, 148 .lpfn = 0, 149 .mem_type = VMW_PL_GMR, 150 .flags = 0 151 }, { 152 .fpfn = 0, 153 .lpfn = 0, 154 .mem_type = VMW_PL_MOB, 155 .flags = 0 156 } 157 }; 158 159 struct ttm_placement vmw_evictable_placement = { 160 .num_placement = 4, 161 .placement = evictable_placement_flags, 162 .num_busy_placement = 1, 163 .busy_placement = &sys_placement_flags 164 }; 165 166 struct ttm_placement vmw_srf_placement = { 167 .num_placement = 1, 168 .num_busy_placement = 2, 169 .placement = &gmr_placement_flags, 170 .busy_placement = gmr_vram_placement_flags 171 }; 172 173 struct ttm_placement vmw_mob_placement = { 174 .num_placement = 1, 175 .num_busy_placement = 1, 176 .placement = &mob_placement_flags, 177 .busy_placement = &mob_placement_flags 178 }; 179 180 struct ttm_placement vmw_nonfixed_placement = { 181 .num_placement = 3, 182 .placement = nonfixed_placement_flags, 183 .num_busy_placement = 1, 184 .busy_placement = &sys_placement_flags 185 }; 186 187 struct vmw_ttm_tt { 188 struct ttm_tt dma_ttm; 189 struct vmw_private *dev_priv; 190 int gmr_id; 191 struct vmw_mob *mob; 192 int mem_type; 193 struct sg_table sgt; 194 struct vmw_sg_table vsgt; 195 uint64_t sg_alloc_size; 196 bool mapped; 197 bool bound; 198 }; 199 200 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt); 201 202 /** 203 * __vmw_piter_non_sg_next: Helper functions to advance 204 * a struct vmw_piter iterator. 205 * 206 * @viter: Pointer to the iterator. 207 * 208 * These functions return false if past the end of the list, 209 * true otherwise. Functions are selected depending on the current 210 * DMA mapping mode. 211 */ 212 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter) 213 { 214 return ++(viter->i) < viter->num_pages; 215 } 216 217 static bool __vmw_piter_sg_next(struct vmw_piter *viter) 218 { 219 bool ret = __vmw_piter_non_sg_next(viter); 220 221 return __sg_page_iter_dma_next(&viter->iter) && ret; 222 } 223 224 225 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter) 226 { 227 return viter->addrs[viter->i]; 228 } 229 230 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter) 231 { 232 return sg_page_iter_dma_address(&viter->iter); 233 } 234 235 236 /** 237 * vmw_piter_start - Initialize a struct vmw_piter. 238 * 239 * @viter: Pointer to the iterator to initialize 240 * @vsgt: Pointer to a struct vmw_sg_table to initialize from 241 * @p_offset: Pointer offset used to update current array position 242 * 243 * Note that we're following the convention of __sg_page_iter_start, so that 244 * the iterator doesn't point to a valid page after initialization; it has 245 * to be advanced one step first. 246 */ 247 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt, 248 unsigned long p_offset) 249 { 250 viter->i = p_offset - 1; 251 viter->num_pages = vsgt->num_pages; 252 viter->pages = vsgt->pages; 253 switch (vsgt->mode) { 254 case vmw_dma_alloc_coherent: 255 viter->next = &__vmw_piter_non_sg_next; 256 viter->dma_address = &__vmw_piter_dma_addr; 257 viter->addrs = vsgt->addrs; 258 break; 259 case vmw_dma_map_populate: 260 case vmw_dma_map_bind: 261 viter->next = &__vmw_piter_sg_next; 262 viter->dma_address = &__vmw_piter_sg_addr; 263 __sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl, 264 vsgt->sgt->orig_nents, p_offset); 265 break; 266 default: 267 BUG(); 268 } 269 } 270 271 /** 272 * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for 273 * TTM pages 274 * 275 * @vmw_tt: Pointer to a struct vmw_ttm_backend 276 * 277 * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma. 278 */ 279 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt) 280 { 281 struct device *dev = vmw_tt->dev_priv->drm.dev; 282 283 dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0); 284 vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents; 285 } 286 287 /** 288 * vmw_ttm_map_for_dma - map TTM pages to get device addresses 289 * 290 * @vmw_tt: Pointer to a struct vmw_ttm_backend 291 * 292 * This function is used to get device addresses from the kernel DMA layer. 293 * However, it's violating the DMA API in that when this operation has been 294 * performed, it's illegal for the CPU to write to the pages without first 295 * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is 296 * therefore only legal to call this function if we know that the function 297 * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most 298 * a CPU write buffer flush. 299 */ 300 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt) 301 { 302 struct device *dev = vmw_tt->dev_priv->drm.dev; 303 304 return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0); 305 } 306 307 /** 308 * vmw_ttm_map_dma - Make sure TTM pages are visible to the device 309 * 310 * @vmw_tt: Pointer to a struct vmw_ttm_tt 311 * 312 * Select the correct function for and make sure the TTM pages are 313 * visible to the device. Allocate storage for the device mappings. 314 * If a mapping has already been performed, indicated by the storage 315 * pointer being non NULL, the function returns success. 316 */ 317 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt) 318 { 319 struct vmw_private *dev_priv = vmw_tt->dev_priv; 320 struct ttm_mem_global *glob = vmw_mem_glob(dev_priv); 321 struct vmw_sg_table *vsgt = &vmw_tt->vsgt; 322 struct ttm_operation_ctx ctx = { 323 .interruptible = true, 324 .no_wait_gpu = false 325 }; 326 struct vmw_piter iter; 327 dma_addr_t old; 328 int ret = 0; 329 static size_t sgl_size; 330 static size_t sgt_size; 331 struct scatterlist *sg; 332 333 if (vmw_tt->mapped) 334 return 0; 335 336 vsgt->mode = dev_priv->map_mode; 337 vsgt->pages = vmw_tt->dma_ttm.pages; 338 vsgt->num_pages = vmw_tt->dma_ttm.num_pages; 339 vsgt->addrs = vmw_tt->dma_ttm.dma_address; 340 vsgt->sgt = &vmw_tt->sgt; 341 342 switch (dev_priv->map_mode) { 343 case vmw_dma_map_bind: 344 case vmw_dma_map_populate: 345 if (unlikely(!sgl_size)) { 346 sgl_size = ttm_round_pot(sizeof(struct scatterlist)); 347 sgt_size = ttm_round_pot(sizeof(struct sg_table)); 348 } 349 vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages; 350 ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, &ctx); 351 if (unlikely(ret != 0)) 352 return ret; 353 354 sg = __sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages, 355 vsgt->num_pages, 0, 356 (unsigned long) vsgt->num_pages << PAGE_SHIFT, 357 dma_get_max_seg_size(dev_priv->drm.dev), 358 NULL, 0, GFP_KERNEL); 359 if (IS_ERR(sg)) { 360 ret = PTR_ERR(sg); 361 goto out_sg_alloc_fail; 362 } 363 364 if (vsgt->num_pages > vmw_tt->sgt.orig_nents) { 365 uint64_t over_alloc = 366 sgl_size * (vsgt->num_pages - 367 vmw_tt->sgt.orig_nents); 368 369 ttm_mem_global_free(glob, over_alloc); 370 vmw_tt->sg_alloc_size -= over_alloc; 371 } 372 373 ret = vmw_ttm_map_for_dma(vmw_tt); 374 if (unlikely(ret != 0)) 375 goto out_map_fail; 376 377 break; 378 default: 379 break; 380 } 381 382 old = ~((dma_addr_t) 0); 383 vmw_tt->vsgt.num_regions = 0; 384 for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) { 385 dma_addr_t cur = vmw_piter_dma_addr(&iter); 386 387 if (cur != old + PAGE_SIZE) 388 vmw_tt->vsgt.num_regions++; 389 old = cur; 390 } 391 392 vmw_tt->mapped = true; 393 return 0; 394 395 out_map_fail: 396 sg_free_table(vmw_tt->vsgt.sgt); 397 vmw_tt->vsgt.sgt = NULL; 398 out_sg_alloc_fail: 399 ttm_mem_global_free(glob, vmw_tt->sg_alloc_size); 400 return ret; 401 } 402 403 /** 404 * vmw_ttm_unmap_dma - Tear down any TTM page device mappings 405 * 406 * @vmw_tt: Pointer to a struct vmw_ttm_tt 407 * 408 * Tear down any previously set up device DMA mappings and free 409 * any storage space allocated for them. If there are no mappings set up, 410 * this function is a NOP. 411 */ 412 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt) 413 { 414 struct vmw_private *dev_priv = vmw_tt->dev_priv; 415 416 if (!vmw_tt->vsgt.sgt) 417 return; 418 419 switch (dev_priv->map_mode) { 420 case vmw_dma_map_bind: 421 case vmw_dma_map_populate: 422 vmw_ttm_unmap_from_dma(vmw_tt); 423 sg_free_table(vmw_tt->vsgt.sgt); 424 vmw_tt->vsgt.sgt = NULL; 425 ttm_mem_global_free(vmw_mem_glob(dev_priv), 426 vmw_tt->sg_alloc_size); 427 break; 428 default: 429 break; 430 } 431 vmw_tt->mapped = false; 432 } 433 434 /** 435 * vmw_bo_sg_table - Return a struct vmw_sg_table object for a 436 * TTM buffer object 437 * 438 * @bo: Pointer to a struct ttm_buffer_object 439 * 440 * Returns a pointer to a struct vmw_sg_table object. The object should 441 * not be freed after use. 442 * Note that for the device addresses to be valid, the buffer object must 443 * either be reserved or pinned. 444 */ 445 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo) 446 { 447 struct vmw_ttm_tt *vmw_tt = 448 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm); 449 450 return &vmw_tt->vsgt; 451 } 452 453 454 static int vmw_ttm_bind(struct ttm_device *bdev, 455 struct ttm_tt *ttm, struct ttm_resource *bo_mem) 456 { 457 struct vmw_ttm_tt *vmw_be = 458 container_of(ttm, struct vmw_ttm_tt, dma_ttm); 459 int ret = 0; 460 461 if (!bo_mem) 462 return -EINVAL; 463 464 if (vmw_be->bound) 465 return 0; 466 467 ret = vmw_ttm_map_dma(vmw_be); 468 if (unlikely(ret != 0)) 469 return ret; 470 471 vmw_be->gmr_id = bo_mem->start; 472 vmw_be->mem_type = bo_mem->mem_type; 473 474 switch (bo_mem->mem_type) { 475 case VMW_PL_GMR: 476 ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt, 477 ttm->num_pages, vmw_be->gmr_id); 478 break; 479 case VMW_PL_MOB: 480 if (unlikely(vmw_be->mob == NULL)) { 481 vmw_be->mob = 482 vmw_mob_create(ttm->num_pages); 483 if (unlikely(vmw_be->mob == NULL)) 484 return -ENOMEM; 485 } 486 487 ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob, 488 &vmw_be->vsgt, ttm->num_pages, 489 vmw_be->gmr_id); 490 break; 491 default: 492 BUG(); 493 } 494 vmw_be->bound = true; 495 return ret; 496 } 497 498 static void vmw_ttm_unbind(struct ttm_device *bdev, 499 struct ttm_tt *ttm) 500 { 501 struct vmw_ttm_tt *vmw_be = 502 container_of(ttm, struct vmw_ttm_tt, dma_ttm); 503 504 if (!vmw_be->bound) 505 return; 506 507 switch (vmw_be->mem_type) { 508 case VMW_PL_GMR: 509 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id); 510 break; 511 case VMW_PL_MOB: 512 vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob); 513 break; 514 default: 515 BUG(); 516 } 517 518 if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind) 519 vmw_ttm_unmap_dma(vmw_be); 520 vmw_be->bound = false; 521 } 522 523 524 static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm) 525 { 526 struct vmw_ttm_tt *vmw_be = 527 container_of(ttm, struct vmw_ttm_tt, dma_ttm); 528 529 vmw_ttm_unbind(bdev, ttm); 530 ttm_tt_destroy_common(bdev, ttm); 531 vmw_ttm_unmap_dma(vmw_be); 532 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent) 533 ttm_tt_fini(&vmw_be->dma_ttm); 534 else 535 ttm_tt_fini(ttm); 536 537 if (vmw_be->mob) 538 vmw_mob_destroy(vmw_be->mob); 539 540 kfree(vmw_be); 541 } 542 543 544 static int vmw_ttm_populate(struct ttm_device *bdev, 545 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) 546 { 547 unsigned int i; 548 int ret; 549 550 /* TODO: maybe completely drop this ? */ 551 if (ttm_tt_is_populated(ttm)) 552 return 0; 553 554 ret = ttm_pool_alloc(&bdev->pool, ttm, ctx); 555 if (ret) 556 return ret; 557 558 for (i = 0; i < ttm->num_pages; ++i) { 559 ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i], 560 PAGE_SIZE, ctx); 561 if (ret) 562 goto error; 563 } 564 return 0; 565 566 error: 567 while (i--) 568 ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i], 569 PAGE_SIZE); 570 ttm_pool_free(&bdev->pool, ttm); 571 return ret; 572 } 573 574 static void vmw_ttm_unpopulate(struct ttm_device *bdev, 575 struct ttm_tt *ttm) 576 { 577 struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt, 578 dma_ttm); 579 unsigned int i; 580 581 if (vmw_tt->mob) { 582 vmw_mob_destroy(vmw_tt->mob); 583 vmw_tt->mob = NULL; 584 } 585 586 vmw_ttm_unmap_dma(vmw_tt); 587 588 for (i = 0; i < ttm->num_pages; ++i) 589 ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i], 590 PAGE_SIZE); 591 592 ttm_pool_free(&bdev->pool, ttm); 593 } 594 595 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo, 596 uint32_t page_flags) 597 { 598 struct vmw_ttm_tt *vmw_be; 599 int ret; 600 601 vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL); 602 if (!vmw_be) 603 return NULL; 604 605 vmw_be->dev_priv = container_of(bo->bdev, struct vmw_private, bdev); 606 vmw_be->mob = NULL; 607 608 if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent) 609 ret = ttm_sg_tt_init(&vmw_be->dma_ttm, bo, page_flags, 610 ttm_cached); 611 else 612 ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags, 613 ttm_cached); 614 if (unlikely(ret != 0)) 615 goto out_no_init; 616 617 return &vmw_be->dma_ttm; 618 out_no_init: 619 kfree(vmw_be); 620 return NULL; 621 } 622 623 static void vmw_evict_flags(struct ttm_buffer_object *bo, 624 struct ttm_placement *placement) 625 { 626 *placement = vmw_sys_placement; 627 } 628 629 static int vmw_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem) 630 { 631 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev); 632 633 switch (mem->mem_type) { 634 case TTM_PL_SYSTEM: 635 case VMW_PL_GMR: 636 case VMW_PL_MOB: 637 return 0; 638 case TTM_PL_VRAM: 639 mem->bus.offset = (mem->start << PAGE_SHIFT) + 640 dev_priv->vram_start; 641 mem->bus.is_iomem = true; 642 mem->bus.caching = ttm_cached; 643 break; 644 default: 645 return -EINVAL; 646 } 647 return 0; 648 } 649 650 /** 651 * vmw_move_notify - TTM move_notify_callback 652 * 653 * @bo: The TTM buffer object about to move. 654 * @old_mem: The old memory where we move from 655 * @new_mem: The struct ttm_resource indicating to what memory 656 * region the move is taking place. 657 * 658 * Calls move_notify for all subsystems needing it. 659 * (currently only resources). 660 */ 661 static void vmw_move_notify(struct ttm_buffer_object *bo, 662 struct ttm_resource *old_mem, 663 struct ttm_resource *new_mem) 664 { 665 vmw_bo_move_notify(bo, new_mem); 666 vmw_query_move_notify(bo, old_mem, new_mem); 667 } 668 669 670 /** 671 * vmw_swap_notify - TTM move_notify_callback 672 * 673 * @bo: The TTM buffer object about to be swapped out. 674 */ 675 static void vmw_swap_notify(struct ttm_buffer_object *bo) 676 { 677 vmw_bo_swap_notify(bo); 678 (void) ttm_bo_wait(bo, false, false); 679 } 680 681 static int vmw_move(struct ttm_buffer_object *bo, 682 bool evict, 683 struct ttm_operation_ctx *ctx, 684 struct ttm_resource *new_mem, 685 struct ttm_place *hop) 686 { 687 struct ttm_resource_manager *old_man = ttm_manager_type(bo->bdev, bo->resource->mem_type); 688 struct ttm_resource_manager *new_man = ttm_manager_type(bo->bdev, new_mem->mem_type); 689 int ret; 690 691 if (new_man->use_tt && new_mem->mem_type != TTM_PL_SYSTEM) { 692 ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem); 693 if (ret) 694 return ret; 695 } 696 697 vmw_move_notify(bo, bo->resource, new_mem); 698 699 if (old_man->use_tt && new_man->use_tt) { 700 if (bo->resource->mem_type == TTM_PL_SYSTEM) { 701 ttm_bo_move_null(bo, new_mem); 702 return 0; 703 } 704 ret = ttm_bo_wait_ctx(bo, ctx); 705 if (ret) 706 goto fail; 707 708 vmw_ttm_unbind(bo->bdev, bo->ttm); 709 ttm_resource_free(bo, &bo->resource); 710 ttm_bo_assign_mem(bo, new_mem); 711 return 0; 712 } else { 713 ret = ttm_bo_move_memcpy(bo, ctx, new_mem); 714 if (ret) 715 goto fail; 716 } 717 return 0; 718 fail: 719 vmw_move_notify(bo, new_mem, bo->resource); 720 return ret; 721 } 722 723 struct ttm_device_funcs vmw_bo_driver = { 724 .ttm_tt_create = &vmw_ttm_tt_create, 725 .ttm_tt_populate = &vmw_ttm_populate, 726 .ttm_tt_unpopulate = &vmw_ttm_unpopulate, 727 .ttm_tt_destroy = &vmw_ttm_destroy, 728 .eviction_valuable = ttm_bo_eviction_valuable, 729 .evict_flags = vmw_evict_flags, 730 .move = vmw_move, 731 .swap_notify = vmw_swap_notify, 732 .io_mem_reserve = &vmw_ttm_io_mem_reserve, 733 }; 734 735 int vmw_bo_create_and_populate(struct vmw_private *dev_priv, 736 unsigned long bo_size, 737 struct ttm_buffer_object **bo_p) 738 { 739 struct ttm_operation_ctx ctx = { 740 .interruptible = false, 741 .no_wait_gpu = false 742 }; 743 struct ttm_buffer_object *bo; 744 int ret; 745 746 ret = vmw_bo_create_kernel(dev_priv, bo_size, 747 &vmw_sys_placement, 748 &bo); 749 if (unlikely(ret != 0)) 750 return ret; 751 752 ret = ttm_bo_reserve(bo, false, true, NULL); 753 BUG_ON(ret != 0); 754 ret = vmw_ttm_populate(bo->bdev, bo->ttm, &ctx); 755 if (likely(ret == 0)) { 756 struct vmw_ttm_tt *vmw_tt = 757 container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm); 758 ret = vmw_ttm_map_dma(vmw_tt); 759 } 760 761 ttm_bo_unreserve(bo); 762 763 if (likely(ret == 0)) 764 *bo_p = bo; 765 return ret; 766 } 767