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