1 /************************************************************************** 2 * 3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 4 * All Rights Reserved. 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 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 29 */ 30 31 #define pr_fmt(fmt) "[TTM] " fmt 32 33 #include <drm/ttm/ttm_module.h> 34 #include <drm/ttm/ttm_bo_driver.h> 35 #include <drm/ttm/ttm_placement.h> 36 #include <drm/drm_vma_manager.h> 37 #include <linux/mm.h> 38 #include <linux/pfn_t.h> 39 #include <linux/rbtree.h> 40 #include <linux/module.h> 41 #include <linux/uaccess.h> 42 #include <linux/mem_encrypt.h> 43 44 #include <sys/sysctl.h> 45 #include <vm/vm.h> 46 #include <vm/vm_page.h> 47 #include <vm/vm_page2.h> 48 49 #define TTM_BO_VM_NUM_PREFAULT 16 50 51 static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo, 52 struct vm_area_struct *vma, 53 struct vm_fault *vmf) 54 { 55 int ret = 0; 56 57 if (likely(!bo->moving)) 58 goto out_unlock; 59 60 /* 61 * Quick non-stalling check for idle. 62 */ 63 if (dma_fence_is_signaled(bo->moving)) 64 goto out_clear; 65 66 /* 67 * If possible, avoid waiting for GPU with mmap_sem 68 * held. 69 */ 70 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) { 71 ret = VM_FAULT_RETRY; 72 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) 73 goto out_unlock; 74 75 ttm_bo_get(bo); 76 up_read(&vmf->vma->vm_mm->mmap_sem); /* release */ 77 (void) dma_fence_wait(bo->moving, true); 78 ttm_bo_unreserve(bo); 79 ttm_bo_put(bo); 80 down_read(&vmf->vma->vm_mm->mmap_sem); /* acquire */ 81 goto out_unlock; 82 } 83 84 /* 85 * Ordinary wait. 86 */ 87 ret = dma_fence_wait(bo->moving, true); 88 if (unlikely(ret != 0)) { 89 ret = (ret != -ERESTARTSYS) ? VM_FAULT_SIGBUS : 90 VM_FAULT_NOPAGE; 91 goto out_unlock; 92 } 93 94 out_clear: 95 dma_fence_put(bo->moving); 96 bo->moving = NULL; 97 98 out_unlock: 99 return ret; 100 } 101 102 /* 103 * Always unstall on unexpected vm_page alias, fatal bus fault. 104 * Set to 0 to stall, set to positive count to unstall N times, 105 * then stall again. 106 */ 107 static int drm_unstall = -1; 108 SYSCTL_INT(_debug, OID_AUTO, unstall, CTLFLAG_RW, &drm_unstall, 0, ""); 109 110 static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 111 { 112 /* see ttm_bo_mmap_single() at end of this file */ 113 /* ttm_bo_vm_ops not currently used, no entry should occur */ 114 panic("ttm_bo_vm_fault"); 115 #if 0 116 struct ttm_buffer_object *bo = (struct ttm_buffer_object *) 117 vma->vm_private_data; 118 struct ttm_bo_device *bdev = bo->bdev; 119 unsigned long page_offset; 120 unsigned long page_last; 121 unsigned long pfn; 122 struct ttm_tt *ttm = NULL; 123 struct page *page; 124 int ret; 125 int i; 126 unsigned long address = vmf->address; 127 int retval = VM_FAULT_NOPAGE; 128 struct ttm_mem_type_manager *man = 129 &bdev->man[bo->mem.mem_type]; 130 struct vm_area_struct cvma; 131 132 /* 133 * Work around locking order reversal in fault / nopfn 134 * between mmap_sem and bo_reserve: Perform a trylock operation 135 * for reserve, and if it fails, retry the fault after waiting 136 * for the buffer to become unreserved. 137 */ 138 ret = ttm_bo_reserve(bo, true, true, NULL); 139 if (unlikely(ret != 0)) { 140 if (ret != -EBUSY) 141 return VM_FAULT_NOPAGE; 142 143 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) { 144 if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 145 ttm_bo_reference(bo); 146 up_read(&vma->vm_mm->mmap_sem); 147 (void) ttm_bo_wait_unreserved(bo); 148 ttm_bo_unref(&bo); 149 } 150 151 return VM_FAULT_RETRY; 152 } 153 154 /* 155 * If we'd want to change locking order to 156 * mmap_sem -> bo::reserve, we'd use a blocking reserve here 157 * instead of retrying the fault... 158 */ 159 return VM_FAULT_NOPAGE; 160 } 161 162 /* 163 * Refuse to fault imported pages. This should be handled 164 * (if at all) by redirecting mmap to the exporter. 165 */ 166 if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) { 167 retval = VM_FAULT_SIGBUS; 168 goto out_unlock; 169 } 170 171 if (bdev->driver->fault_reserve_notify) { 172 ret = bdev->driver->fault_reserve_notify(bo); 173 switch (ret) { 174 case 0: 175 break; 176 case -EBUSY: 177 case -ERESTARTSYS: 178 retval = VM_FAULT_NOPAGE; 179 goto out_unlock; 180 default: 181 retval = VM_FAULT_SIGBUS; 182 goto out_unlock; 183 } 184 } 185 186 /* 187 * Wait for buffer data in transit, due to a pipelined 188 * move. 189 */ 190 191 ret = ttm_bo_vm_fault_idle(bo, vma, vmf); 192 if (unlikely(ret != 0)) { 193 retval = ret; 194 195 if (retval == VM_FAULT_RETRY && 196 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 197 /* The BO has already been unreserved. */ 198 return retval; 199 } 200 201 goto out_unlock; 202 } 203 204 ret = ttm_mem_io_lock(man, true); 205 if (unlikely(ret != 0)) { 206 retval = VM_FAULT_NOPAGE; 207 goto out_unlock; 208 } 209 ret = ttm_mem_io_reserve_vm(bo); 210 if (unlikely(ret != 0)) { 211 retval = VM_FAULT_SIGBUS; 212 goto out_io_unlock; 213 } 214 215 page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) + 216 vma->vm_pgoff - drm_vma_node_start(&bo->vma_node); 217 page_last = vma_pages(vma) + vma->vm_pgoff - 218 drm_vma_node_start(&bo->vma_node); 219 220 if (unlikely(page_offset >= bo->num_pages)) { 221 retval = VM_FAULT_SIGBUS; 222 goto out_io_unlock; 223 } 224 225 /* 226 * Make a local vma copy to modify the page_prot member 227 * and vm_flags if necessary. The vma parameter is protected 228 * by mmap_sem in write mode. 229 */ 230 cvma = *vma; 231 cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags); 232 233 if (bo->mem.bus.is_iomem) { 234 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, 235 cvma.vm_page_prot); 236 } else { 237 struct ttm_operation_ctx ctx = { 238 .interruptible = false, 239 .no_wait_gpu = false, 240 .flags = TTM_OPT_FLAG_FORCE_ALLOC 241 242 }; 243 244 ttm = bo->ttm; 245 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, 246 cvma.vm_page_prot); 247 248 /* Allocate all page at once, most common usage */ 249 if (ttm_tt_populate(ttm, &ctx)) { 250 retval = VM_FAULT_OOM; 251 goto out_io_unlock; 252 } 253 } 254 255 /* 256 * Speculatively prefault a number of pages. Only error on 257 * first page. 258 */ 259 for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) { 260 if (bo->mem.bus.is_iomem) 261 pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset; 262 else { 263 page = ttm->pages[page_offset]; 264 if (unlikely(!page && i == 0)) { 265 retval = VM_FAULT_OOM; 266 goto out_io_unlock; 267 } else if (unlikely(!page)) { 268 break; 269 } 270 page->mapping = vma->vm_file->f_mapping; 271 page->index = drm_vma_node_start(&bo->vma_node) + 272 page_offset; 273 pfn = page_to_pfn(page); 274 } 275 276 if (vma->vm_flags & VM_MIXEDMAP) 277 ret = vm_insert_mixed(&cvma, address, 278 __pfn_to_pfn_t(pfn, PFN_DEV)); 279 else 280 ret = vm_insert_pfn(&cvma, address, pfn); 281 282 /* 283 * Somebody beat us to this PTE or prefaulting to 284 * an already populated PTE, or prefaulting error. 285 */ 286 287 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0))) 288 break; 289 else if (unlikely(ret != 0)) { 290 retval = 291 (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; 292 goto out_io_unlock; 293 } 294 295 address += PAGE_SIZE; 296 if (unlikely(++page_offset >= page_last)) 297 break; 298 } 299 out_io_unlock: 300 ttm_mem_io_unlock(man); 301 out_unlock: 302 ttm_bo_unreserve(bo); 303 return retval; 304 #endif 305 } 306 307 /* ttm_bo_vm_ops not currently used, no entry should occur */ 308 static void ttm_bo_vm_open(struct vm_area_struct *vma) 309 { 310 struct ttm_buffer_object *bo = 311 (struct ttm_buffer_object *)vma->vm_private_data; 312 313 #if 0 314 WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping); 315 #endif 316 317 ttm_bo_get(bo); 318 } 319 320 /* ttm_bo_vm_ops not currently used, no entry should occur */ 321 static void ttm_bo_vm_close(struct vm_area_struct *vma) 322 { 323 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data; 324 325 ttm_bo_put(bo); 326 vma->vm_private_data = NULL; 327 } 328 329 static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo, 330 unsigned long offset, 331 uint8_t *buf, int len, int write) 332 { 333 unsigned long page = offset >> PAGE_SHIFT; 334 unsigned long bytes_left = len; 335 int ret; 336 337 /* Copy a page at a time, that way no extra virtual address 338 * mapping is needed 339 */ 340 offset -= page << PAGE_SHIFT; 341 do { 342 unsigned long bytes = min(bytes_left, PAGE_SIZE - offset); 343 struct ttm_bo_kmap_obj map; 344 void *ptr; 345 bool is_iomem; 346 347 ret = ttm_bo_kmap(bo, page, 1, &map); 348 if (ret) 349 return ret; 350 351 ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset; 352 WARN_ON_ONCE(is_iomem); 353 if (write) 354 memcpy(ptr, buf, bytes); 355 else 356 memcpy(buf, ptr, bytes); 357 ttm_bo_kunmap(&map); 358 359 page++; 360 buf += bytes; 361 bytes_left -= bytes; 362 offset = 0; 363 } while (bytes_left); 364 365 return len; 366 } 367 368 static int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, 369 void *buf, int len, int write) 370 { 371 unsigned long offset = (addr) - vma->vm_start; 372 struct ttm_buffer_object *bo = vma->vm_private_data; 373 int ret; 374 375 if (len < 1 || (offset + len) >> PAGE_SHIFT > bo->num_pages) 376 return -EIO; 377 378 ret = ttm_bo_reserve(bo, true, false, NULL); 379 if (ret) 380 return ret; 381 382 switch (bo->mem.mem_type) { 383 case TTM_PL_SYSTEM: 384 if (unlikely(bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) { 385 ret = ttm_tt_swapin(bo->ttm); 386 if (unlikely(ret != 0)) 387 return ret; 388 } 389 /* fall through */ 390 case TTM_PL_TT: 391 ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write); 392 break; 393 default: 394 if (bo->bdev->driver->access_memory) 395 ret = bo->bdev->driver->access_memory( 396 bo, offset, buf, len, write); 397 else 398 ret = -EIO; 399 } 400 401 ttm_bo_unreserve(bo); 402 403 return ret; 404 } 405 406 static const struct vm_operations_struct ttm_bo_vm_ops = { 407 .fault = ttm_bo_vm_fault, 408 .open = ttm_bo_vm_open, 409 .close = ttm_bo_vm_close, 410 .access = ttm_bo_vm_access 411 }; 412 413 static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev, 414 unsigned long offset, 415 unsigned long pages) 416 { 417 struct drm_vma_offset_node *node; 418 struct ttm_buffer_object *bo = NULL; 419 420 drm_vma_offset_lock_lookup(&bdev->vma_manager); 421 422 node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages); 423 if (likely(node)) { 424 bo = container_of(node, struct ttm_buffer_object, vma_node); 425 if (!kref_get_unless_zero(&bo->kref)) 426 bo = NULL; 427 } 428 429 drm_vma_offset_unlock_lookup(&bdev->vma_manager); 430 431 if (!bo) 432 pr_err("Could not find buffer object to map\n"); 433 434 return bo; 435 } 436 437 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, 438 struct ttm_bo_device *bdev) 439 { 440 struct ttm_bo_driver *driver; 441 struct ttm_buffer_object *bo; 442 int ret; 443 444 bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma)); 445 if (unlikely(!bo)) 446 return -EINVAL; 447 448 driver = bo->bdev->driver; 449 if (unlikely(!driver->verify_access)) { 450 ret = -EPERM; 451 goto out_unref; 452 } 453 ret = driver->verify_access(bo, filp); 454 if (unlikely(ret != 0)) 455 goto out_unref; 456 457 vma->vm_ops = &ttm_bo_vm_ops; 458 459 /* 460 * Note: We're transferring the bo reference to 461 * vma->vm_private_data here. 462 */ 463 464 vma->vm_private_data = bo; 465 466 /* 467 * We'd like to use VM_PFNMAP on shared mappings, where 468 * (vma->vm_flags & VM_SHARED) != 0, for performance reasons, 469 * but for some reason VM_PFNMAP + x86 PAT + write-combine is very 470 * bad for performance. Until that has been sorted out, use 471 * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719 472 */ 473 vma->vm_flags |= VM_MIXEDMAP; 474 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; 475 return 0; 476 out_unref: 477 ttm_bo_put(bo); 478 return ret; 479 } 480 EXPORT_SYMBOL(ttm_bo_mmap); 481 482 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo) 483 { 484 if (vma->vm_pgoff != 0) 485 return -EACCES; 486 487 ttm_bo_get(bo); 488 489 vma->vm_ops = &ttm_bo_vm_ops; 490 vma->vm_private_data = bo; 491 vma->vm_flags |= VM_MIXEDMAP; 492 vma->vm_flags |= VM_IO | VM_DONTEXPAND; 493 return 0; 494 } 495 EXPORT_SYMBOL(ttm_fbdev_mmap); 496 497 /* 498 * DragonFlyBSD Interface 499 */ 500 501 #include "opt_vm.h" 502 503 /* 504 * NOTE: This code is fragile. This code can only be entered with *mres 505 * not NULL when *mres is a placeholder page allocated by the kernel. 506 */ 507 static int 508 ttm_bo_vm_fault_dfly(vm_object_t vm_obj, vm_ooffset_t offset, 509 int prot, vm_page_t *mres) 510 { 511 struct ttm_buffer_object *bo = vm_obj->handle; 512 struct ttm_bo_device *bdev = bo->bdev; 513 struct ttm_tt *ttm = NULL; 514 vm_page_t m; 515 int ret; 516 int retval = VM_PAGER_OK; 517 struct ttm_mem_type_manager *man = 518 &bdev->man[bo->mem.mem_type]; 519 struct vm_area_struct cvma; 520 521 /* 522 The Linux code expects to receive these arguments: 523 - struct vm_area_struct *vma 524 - struct vm_fault *vmf 525 */ 526 #ifdef __DragonFly__ 527 struct vm_area_struct vmas; 528 struct vm_area_struct *vma = &vmas; 529 struct vm_fault vmfs; 530 struct vm_fault *vmf = &vmfs; 531 532 memset(vma, 0, sizeof(*vma)); 533 memset(vmf, 0, sizeof(*vmf)); 534 535 vma->vm_mm = current->mm; 536 vmf->vma = vma; 537 538 int retry_count = 0; 539 #endif 540 541 vm_object_pip_add(vm_obj, 1); 542 543 /* 544 * OBJT_MGTDEVICE does not pre-allocate the page. 545 */ 546 KKASSERT(*mres == NULL); 547 548 retry: 549 /* The Linux page fault handler acquires mmap_sem */ 550 down_read(&vma->vm_mm->mmap_sem); 551 552 m = NULL; 553 554 /* 555 * Work around locking order reversal in fault / nopfn 556 * between mmap_sem and bo_reserve: Perform a trylock operation 557 * for reserve, and if it fails, retry the fault after waiting 558 * for the buffer to become unreserved. 559 */ 560 ret = ttm_bo_reserve(bo, true, true, NULL); 561 if (unlikely(ret != 0)) { 562 if (ret != -EBUSY) { 563 retval = VM_PAGER_ERROR; 564 goto out_unlock2; 565 } 566 567 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY || 1) { 568 if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 569 up_read(&vma->vm_mm->mmap_sem); 570 (void) ttm_bo_wait_unreserved(bo); 571 down_read(&vma->vm_mm->mmap_sem); 572 } 573 574 #ifndef __DragonFly__ 575 return VM_FAULT_RETRY; 576 #else 577 up_read(&vma->vm_mm->mmap_sem); 578 lwkt_yield(); 579 goto retry; 580 #endif 581 } 582 583 /* 584 * If we'd want to change locking order to 585 * mmap_sem -> bo::reserve, we'd use a blocking reserve here 586 * instead of retrying the fault... 587 */ 588 retval = VM_PAGER_ERROR; 589 goto out_unlock2; 590 } 591 592 if (bdev->driver->fault_reserve_notify) { 593 ret = bdev->driver->fault_reserve_notify(bo); 594 switch (ret) { 595 case 0: 596 break; 597 case -EBUSY: 598 lwkt_yield(); 599 /* fall through */ 600 case -ERESTARTSYS: 601 case -EINTR: 602 retval = VM_PAGER_ERROR; 603 goto out_unlock1; 604 default: 605 retval = VM_PAGER_ERROR; 606 goto out_unlock1; 607 } 608 } 609 610 /* 611 * Wait for buffer data in transit, due to a pipelined 612 * move. 613 */ 614 ret = ttm_bo_vm_fault_idle(bo, vma, vmf); 615 if (unlikely(ret != 0)) { 616 if (retry_count >= 100) { 617 retval = VM_PAGER_ERROR; 618 goto out_unlock1; 619 } else { 620 retry_count++; 621 ttm_bo_unreserve(bo); 622 up_read(&vma->vm_mm->mmap_sem); 623 int dummy; 624 tsleep(&dummy, 0, "blah", 1); 625 626 goto retry; 627 } 628 } 629 630 ret = ttm_mem_io_lock(man, true); 631 if (unlikely(ret != 0)) { 632 retval = VM_PAGER_ERROR; 633 goto out_unlock1; 634 } 635 ret = ttm_mem_io_reserve_vm(bo); 636 if (unlikely(ret != 0)) { 637 retval = VM_PAGER_ERROR; 638 goto out_io_unlock1; 639 } 640 if (unlikely(OFF_TO_IDX(offset) >= bo->num_pages)) { 641 retval = VM_PAGER_ERROR; 642 goto out_io_unlock1; 643 } 644 645 /* 646 * Lookup the real page. 647 * 648 * Strictly, we're not allowed to modify vma->vm_page_prot here, 649 * since the mmap_sem is only held in read mode. However, we 650 * modify only the caching bits of vma->vm_page_prot and 651 * consider those bits protected by 652 * the bo->mutex, as we should be the only writers. 653 * There shouldn't really be any readers of these bits except 654 * within vm_insert_mixed()? fork? 655 * 656 * TODO: Add a list of vmas to the bo, and change the 657 * vma->vm_page_prot when the object changes caching policy, with 658 * the correct locks held. 659 */ 660 661 /* 662 * Make a local vma copy to modify the page_prot member 663 * and vm_flags if necessary. The vma parameter is protected 664 * by mmap_sem in write mode. 665 */ 666 cvma = *vma; 667 #if 0 668 cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags); 669 #else 670 cvma.vm_page_prot = 0; 671 #endif 672 673 if (bo->mem.bus.is_iomem) { 674 #ifdef __DragonFly__ 675 m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base + 676 bo->mem.bus.offset + offset); 677 pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement, 0)); 678 #endif 679 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, 680 cvma.vm_page_prot); 681 } else { 682 ttm = bo->ttm; 683 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, 684 cvma.vm_page_prot); 685 686 /* Allocate all page at once, most common usage */ 687 struct ttm_operation_ctx ctx = { 688 .interruptible = false, 689 .no_wait_gpu = false, 690 .flags = TTM_OPT_FLAG_FORCE_ALLOC 691 692 }; 693 if (ttm_tt_populate(ttm, &ctx)) { 694 retval = VM_PAGER_ERROR; 695 goto out_io_unlock1; 696 } 697 698 m = (struct vm_page *)ttm->pages[OFF_TO_IDX(offset)]; 699 if (unlikely(!m)) { 700 retval = VM_PAGER_ERROR; 701 goto out_io_unlock1; 702 } 703 pmap_page_set_memattr(m, 704 (bo->mem.placement & TTM_PL_FLAG_CACHED) ? 705 VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement, 0)); 706 } 707 708 if (vm_page_busy_try(m, FALSE)) { 709 kprintf("r"); 710 vm_page_sleep_busy(m, FALSE, "ttmvmf"); 711 ttm_mem_io_unlock(man); 712 ttm_bo_unreserve(bo); 713 up_read(&vma->vm_mm->mmap_sem); 714 goto retry; 715 } 716 717 /* 718 * Return our fake page BUSYd. Do not index it into the VM object. 719 * The caller will enter it into the pmap. 720 */ 721 m->valid = VM_PAGE_BITS_ALL; 722 *mres = m; 723 724 out_io_unlock1: 725 ttm_mem_io_unlock(man); 726 out_unlock1: 727 ttm_bo_unreserve(bo); 728 out_unlock2: 729 vm_object_pip_wakeup(vm_obj); 730 up_read(&vma->vm_mm->mmap_sem); 731 732 return (retval); 733 } 734 735 static int 736 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 737 vm_ooffset_t foff, struct ucred *cred, u_short *color) 738 { 739 740 /* 741 * On Linux, a reference to the buffer object is acquired here. 742 * The reason is that this function is not called when the 743 * mmap() is initialized, but only when a process forks for 744 * instance. Therefore on Linux, the reference on the bo is 745 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's 746 * then released in ttm_bo_vm_close(). 747 * 748 * Here, this function is called during mmap() intialization. 749 * Thus, the reference acquired in ttm_bo_mmap_single() is 750 * sufficient. 751 */ 752 *color = 0; 753 return (0); 754 } 755 756 static void 757 ttm_bo_vm_dtor(void *handle) 758 { 759 struct ttm_buffer_object *bo = handle; 760 761 ttm_bo_unref(&bo); 762 } 763 764 static struct cdev_pager_ops ttm_pager_ops = { 765 .cdev_pg_fault = ttm_bo_vm_fault_dfly, 766 .cdev_pg_ctor = ttm_bo_vm_ctor, 767 .cdev_pg_dtor = ttm_bo_vm_dtor 768 }; 769 770 /* 771 * Called from drm_drv.c 772 * 773 * *offset - object offset in bytes 774 * size - map size in bytes 775 * 776 * We setup a dummy vma (for now) and call ttm_bo_mmap(). Then we setup 777 * our own VM object and dfly ops. Note that the ops supplied by 778 * ttm_bo_mmap() are not currently used. 779 */ 780 int 781 ttm_bo_mmap_single(struct file *fp, struct drm_device *dev, 782 vm_ooffset_t *offset, vm_size_t size, 783 struct vm_object **obj_res, int nprot) 784 { 785 struct ttm_bo_device *bdev = dev->drm_ttm_bdev; 786 struct ttm_buffer_object *bo; 787 struct vm_object *vm_obj; 788 struct vm_area_struct vma; 789 int ret; 790 791 *obj_res = NULL; 792 793 bzero(&vma, sizeof(vma)); 794 vma.vm_start = *offset; /* bdev-relative offset */ 795 vma.vm_end = vma.vm_start + size; 796 vma.vm_pgoff = vma.vm_start >> PAGE_SHIFT; 797 /* vma.vm_page_prot */ 798 /* vma.vm_flags */ 799 800 /* 801 * Call the linux-ported code to do the work, and on success just 802 * setup our own VM object and ignore what the linux code did other 803 * then supplying us the 'bo'. 804 */ 805 ret = ttm_bo_mmap(fp, &vma, bdev); 806 807 if (ret == 0) { 808 bo = vma.vm_private_data; 809 vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, 810 &ttm_pager_ops, 811 size, nprot, 0, 812 curthread->td_ucred); 813 if (vm_obj) { 814 *obj_res = vm_obj; 815 *offset = 0; /* object-relative offset */ 816 } else { 817 ttm_bo_unref(&bo); 818 ret = EINVAL; 819 } 820 } 821 return ret; 822 } 823 EXPORT_SYMBOL(ttm_bo_mmap_single); 824 825 #ifdef __DragonFly__ 826 void ttm_bo_release_mmap(struct ttm_buffer_object *bo); 827 828 void 829 ttm_bo_release_mmap(struct ttm_buffer_object *bo) 830 { 831 vm_object_t vm_obj; 832 833 vm_obj = cdev_pager_lookup(bo); 834 if (vm_obj == NULL) 835 return; 836 837 VM_OBJECT_LOCK(vm_obj); 838 #if 1 839 vm_object_page_remove(vm_obj, 0, 0, false); 840 #else 841 /* 842 * XXX REMOVED 843 * 844 * We no longer manage the vm pages inside the MGTDEVICE 845 * objects. 846 */ 847 vm_page_t m; 848 int i; 849 850 for (i = 0; i < bo->num_pages; i++) { 851 m = vm_page_lookup_busy_wait(vm_obj, i, TRUE, "ttm_unm"); 852 if (m == NULL) 853 continue; 854 cdev_pager_free_page(vm_obj, m); 855 } 856 #endif 857 VM_OBJECT_UNLOCK(vm_obj); 858 859 vm_object_deallocate(vm_obj); 860 } 861 #endif 862 863 #if 0 864 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo) 865 { 866 if (vma->vm_pgoff != 0) 867 return -EACCES; 868 869 vma->vm_ops = &ttm_bo_vm_ops; 870 vma->vm_private_data = ttm_bo_reference(bo); 871 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND; 872 return 0; 873 } 874 EXPORT_SYMBOL(ttm_fbdev_mmap); 875 #endif 876