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 unsigned long ttm_bo_default_io_mem_pfn(struct ttm_buffer_object *bo, 352 unsigned long page_offset) 353 { 354 return ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) 355 + page_offset; 356 } 357 EXPORT_SYMBOL(ttm_bo_default_io_mem_pfn); 358 359 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, 360 struct ttm_bo_device *bdev) 361 { 362 struct ttm_bo_driver *driver; 363 struct ttm_buffer_object *bo; 364 int ret; 365 366 bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma)); 367 if (unlikely(!bo)) 368 return -EINVAL; 369 370 driver = bo->bdev->driver; 371 if (unlikely(!driver->verify_access)) { 372 ret = -EPERM; 373 goto out_unref; 374 } 375 ret = driver->verify_access(bo, filp); 376 if (unlikely(ret != 0)) 377 goto out_unref; 378 379 vma->vm_ops = &ttm_bo_vm_ops; 380 381 /* 382 * Note: We're transferring the bo reference to 383 * vma->vm_private_data here. 384 */ 385 386 vma->vm_private_data = bo; 387 388 /* 389 * We'd like to use VM_PFNMAP on shared mappings, where 390 * (vma->vm_flags & VM_SHARED) != 0, for performance reasons, 391 * but for some reason VM_PFNMAP + x86 PAT + write-combine is very 392 * bad for performance. Until that has been sorted out, use 393 * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719 394 */ 395 vma->vm_flags |= VM_MIXEDMAP; 396 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; 397 return 0; 398 out_unref: 399 ttm_bo_unref(&bo); 400 return ret; 401 } 402 EXPORT_SYMBOL(ttm_bo_mmap); 403 404 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo) 405 { 406 if (vma->vm_pgoff != 0) 407 return -EACCES; 408 409 vma->vm_ops = &ttm_bo_vm_ops; 410 vma->vm_private_data = ttm_bo_reference(bo); 411 vma->vm_flags |= VM_MIXEDMAP; 412 vma->vm_flags |= VM_IO | VM_DONTEXPAND; 413 return 0; 414 } 415 EXPORT_SYMBOL(ttm_fbdev_mmap); 416 417 /* 418 * DragonFlyBSD Interface 419 */ 420 421 #include "opt_vm.h" 422 423 /* 424 * NOTE: This code is fragile. This code can only be entered with *mres 425 * not NULL when *mres is a placeholder page allocated by the kernel. 426 */ 427 static int 428 ttm_bo_vm_fault_dfly(vm_object_t vm_obj, vm_ooffset_t offset, 429 int prot, vm_page_t *mres) 430 { 431 struct ttm_buffer_object *bo = vm_obj->handle; 432 struct ttm_bo_device *bdev = bo->bdev; 433 struct ttm_tt *ttm = NULL; 434 vm_page_t m, mtmp; 435 int ret; 436 int retval = VM_PAGER_OK; 437 struct ttm_mem_type_manager *man = 438 &bdev->man[bo->mem.mem_type]; 439 struct vm_area_struct cvma; 440 441 /* 442 The Linux code expects to receive these arguments: 443 - struct vm_area_struct *vma 444 - struct vm_fault *vmf 445 */ 446 #ifdef __DragonFly__ 447 struct vm_area_struct vmas; 448 struct vm_area_struct *vma = &vmas; 449 struct vm_fault vmfs; 450 struct vm_fault *vmf = &vmfs; 451 452 memset(vma, 0, sizeof(*vma)); 453 memset(vmf, 0, sizeof(*vmf)); 454 #endif 455 456 vm_object_pip_add(vm_obj, 1); 457 458 /* 459 * We must atomically clean up any possible placeholder page to avoid 460 * the DRM subsystem attempting to use it. We can determine if this 461 * is a place holder page by checking m->valid. 462 * 463 * We have to do this before any potential fault_reserve_notify() 464 * which might try to free the map (and thus deadlock on our busy 465 * page). 466 */ 467 m = *mres; 468 *mres = NULL; 469 if (m) { 470 if (m->valid == VM_PAGE_BITS_ALL) { 471 /* actual page */ 472 vm_page_wakeup(m); 473 } else { 474 /* placeholder page */ 475 KKASSERT((m->flags & PG_FICTITIOUS) == 0); 476 vm_page_remove(m); 477 vm_page_free(m); 478 } 479 } 480 481 retry: 482 VM_OBJECT_UNLOCK(vm_obj); 483 m = NULL; 484 485 /* 486 * Work around locking order reversal in fault / nopfn 487 * between mmap_sem and bo_reserve: Perform a trylock operation 488 * for reserve, and if it fails, retry the fault after waiting 489 * for the buffer to become unreserved. 490 */ 491 ret = ttm_bo_reserve(bo, true, true, NULL); 492 if (unlikely(ret != 0)) { 493 if (ret != -EBUSY) { 494 retval = VM_PAGER_ERROR; 495 VM_OBJECT_LOCK(vm_obj); 496 goto out_unlock2; 497 } 498 499 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY || 1) { 500 if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 501 up_read(&vma->vm_mm->mmap_sem); 502 (void) ttm_bo_wait_unreserved(bo); 503 } 504 505 #ifndef __DragonFly__ 506 return VM_FAULT_RETRY; 507 #else 508 VM_OBJECT_LOCK(vm_obj); 509 lwkt_yield(); 510 goto retry; 511 #endif 512 } 513 514 /* 515 * If we'd want to change locking order to 516 * mmap_sem -> bo::reserve, we'd use a blocking reserve here 517 * instead of retrying the fault... 518 */ 519 #ifndef __DragonFly__ 520 return VM_FAULT_NOPAGE; 521 #else 522 retval = VM_PAGER_ERROR; 523 VM_OBJECT_LOCK(vm_obj); 524 goto out_unlock2; 525 #endif 526 } 527 528 if (bdev->driver->fault_reserve_notify) { 529 ret = bdev->driver->fault_reserve_notify(bo); 530 switch (ret) { 531 case 0: 532 break; 533 case -EBUSY: 534 lwkt_yield(); 535 /* fall through */ 536 case -ERESTARTSYS: 537 case -EINTR: 538 retval = VM_PAGER_ERROR; 539 goto out_unlock; 540 default: 541 retval = VM_PAGER_ERROR; 542 goto out_unlock; 543 } 544 } 545 546 /* 547 * Wait for buffer data in transit, due to a pipelined 548 * move. 549 */ 550 ret = ttm_bo_vm_fault_idle(bo, vma, vmf); 551 if (unlikely(ret != 0)) { 552 retval = ret; 553 #ifdef __DragonFly__ 554 retval = VM_PAGER_ERROR; 555 #endif 556 goto out_unlock; 557 } 558 559 ret = ttm_mem_io_lock(man, true); 560 if (unlikely(ret != 0)) { 561 retval = VM_PAGER_ERROR; 562 goto out_unlock; 563 } 564 ret = ttm_mem_io_reserve_vm(bo); 565 if (unlikely(ret != 0)) { 566 retval = VM_PAGER_ERROR; 567 goto out_io_unlock; 568 } 569 if (unlikely(OFF_TO_IDX(offset) >= bo->num_pages)) { 570 retval = VM_PAGER_ERROR; 571 goto out_io_unlock; 572 } 573 574 /* 575 * Lookup the real page. 576 * 577 * Strictly, we're not allowed to modify vma->vm_page_prot here, 578 * since the mmap_sem is only held in read mode. However, we 579 * modify only the caching bits of vma->vm_page_prot and 580 * consider those bits protected by 581 * the bo->mutex, as we should be the only writers. 582 * There shouldn't really be any readers of these bits except 583 * within vm_insert_mixed()? fork? 584 * 585 * TODO: Add a list of vmas to the bo, and change the 586 * vma->vm_page_prot when the object changes caching policy, with 587 * the correct locks held. 588 */ 589 590 /* 591 * Make a local vma copy to modify the page_prot member 592 * and vm_flags if necessary. The vma parameter is protected 593 * by mmap_sem in write mode. 594 */ 595 cvma = *vma; 596 #if 0 597 cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags); 598 #else 599 cvma.vm_page_prot = 0; 600 #endif 601 602 if (bo->mem.bus.is_iomem) { 603 #ifdef __DragonFly__ 604 m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base + 605 bo->mem.bus.offset + offset); 606 pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement, 0)); 607 #endif 608 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, 609 cvma.vm_page_prot); 610 } else { 611 ttm = bo->ttm; 612 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement, 613 cvma.vm_page_prot); 614 615 /* Allocate all page at once, most common usage */ 616 if (ttm->bdev->driver->ttm_tt_populate(ttm)) { 617 retval = VM_PAGER_ERROR; 618 goto out_io_unlock; 619 } 620 621 m = (struct vm_page *)ttm->pages[OFF_TO_IDX(offset)]; 622 if (unlikely(!m)) { 623 retval = VM_PAGER_ERROR; 624 goto out_io_unlock; 625 } 626 pmap_page_set_memattr(m, 627 (bo->mem.placement & TTM_PL_FLAG_CACHED) ? 628 VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement, 0)); 629 } 630 631 VM_OBJECT_LOCK(vm_obj); 632 633 if (vm_page_busy_try(m, FALSE)) { 634 kprintf("r"); 635 vm_page_sleep_busy(m, FALSE, "ttmvmf"); 636 ttm_mem_io_unlock(man); 637 ttm_bo_unreserve(bo); 638 goto retry; 639 } 640 641 /* 642 * We want our fake page in the VM object, not the page the OS 643 * allocatedd for us as a placeholder. 644 */ 645 m->valid = VM_PAGE_BITS_ALL; 646 *mres = m; 647 648 /* 649 * Insert the page into the object if not already inserted. 650 */ 651 if (m->object) { 652 if (m->object != vm_obj || m->pindex != OFF_TO_IDX(offset)) { 653 retval = VM_PAGER_ERROR; 654 kprintf("ttm_bo_vm_fault_dfly: m(%p) already inserted " 655 "in obj %p, attempt obj %p\n", 656 m, m->object, vm_obj); 657 while (drm_unstall == 0) { 658 tsleep(&retval, 0, "DEBUG", hz/10); 659 } 660 if (drm_unstall > 0) 661 --drm_unstall; 662 } 663 } else { 664 mtmp = vm_page_lookup(vm_obj, OFF_TO_IDX(offset)); 665 if (mtmp == NULL) { 666 vm_page_insert(m, vm_obj, OFF_TO_IDX(offset)); 667 } else { 668 panic("inconsistent insert bo %p m %p mtmp %p " 669 "offset %jx", 670 bo, m, mtmp, 671 (uintmax_t)offset); 672 } 673 } 674 675 out_io_unlock1: 676 ttm_mem_io_unlock(man); 677 out_unlock1: 678 ttm_bo_unreserve(bo); 679 out_unlock2: 680 vm_object_pip_wakeup(vm_obj); 681 return (retval); 682 683 out_io_unlock: 684 VM_OBJECT_LOCK(vm_obj); 685 goto out_io_unlock1; 686 687 out_unlock: 688 VM_OBJECT_LOCK(vm_obj); 689 goto out_unlock1; 690 } 691 692 static int 693 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 694 vm_ooffset_t foff, struct ucred *cred, u_short *color) 695 { 696 697 /* 698 * On Linux, a reference to the buffer object is acquired here. 699 * The reason is that this function is not called when the 700 * mmap() is initialized, but only when a process forks for 701 * instance. Therefore on Linux, the reference on the bo is 702 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's 703 * then released in ttm_bo_vm_close(). 704 * 705 * Here, this function is called during mmap() intialization. 706 * Thus, the reference acquired in ttm_bo_mmap_single() is 707 * sufficient. 708 */ 709 *color = 0; 710 return (0); 711 } 712 713 static void 714 ttm_bo_vm_dtor(void *handle) 715 { 716 struct ttm_buffer_object *bo = handle; 717 718 ttm_bo_unref(&bo); 719 } 720 721 static struct cdev_pager_ops ttm_pager_ops = { 722 .cdev_pg_fault = ttm_bo_vm_fault_dfly, 723 .cdev_pg_ctor = ttm_bo_vm_ctor, 724 .cdev_pg_dtor = ttm_bo_vm_dtor 725 }; 726 727 /* 728 * Called from drm_drv.c 729 * 730 * *offset - object offset in bytes 731 * size - map size in bytes 732 * 733 * We setup a dummy vma (for now) and call ttm_bo_mmap(). Then we setup 734 * our own VM object and dfly ops. Note that the ops supplied by 735 * ttm_bo_mmap() are not currently used. 736 */ 737 int 738 ttm_bo_mmap_single(struct drm_device *dev, vm_ooffset_t *offset, 739 vm_size_t size, struct vm_object **obj_res, int nprot) 740 { 741 struct ttm_bo_device *bdev = dev->drm_ttm_bdev; 742 struct ttm_buffer_object *bo; 743 struct vm_object *vm_obj; 744 struct vm_area_struct vma; 745 int ret; 746 747 *obj_res = NULL; 748 749 bzero(&vma, sizeof(vma)); 750 vma.vm_start = *offset; /* bdev-relative offset */ 751 vma.vm_end = vma.vm_start + size; 752 vma.vm_pgoff = vma.vm_start >> PAGE_SHIFT; 753 /* vma.vm_page_prot */ 754 /* vma.vm_flags */ 755 756 /* 757 * Call the linux-ported code to do the work, and on success just 758 * setup our own VM object and ignore what the linux code did other 759 * then supplying us the 'bo'. 760 */ 761 ret = ttm_bo_mmap(NULL, &vma, bdev); 762 763 if (ret == 0) { 764 bo = vma.vm_private_data; 765 vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, 766 &ttm_pager_ops, 767 size, nprot, 0, 768 curthread->td_ucred); 769 if (vm_obj) { 770 *obj_res = vm_obj; 771 *offset = 0; /* object-relative offset */ 772 } else { 773 ttm_bo_unref(&bo); 774 ret = EINVAL; 775 } 776 } 777 return ret; 778 } 779 EXPORT_SYMBOL(ttm_bo_mmap_single); 780 781 #ifdef __DragonFly__ 782 void ttm_bo_release_mmap(struct ttm_buffer_object *bo); 783 784 void 785 ttm_bo_release_mmap(struct ttm_buffer_object *bo) 786 { 787 vm_object_t vm_obj; 788 vm_page_t m; 789 int i; 790 791 vm_obj = cdev_pager_lookup(bo); 792 if (vm_obj == NULL) 793 return; 794 795 VM_OBJECT_LOCK(vm_obj); 796 for (i = 0; i < bo->num_pages; i++) { 797 m = vm_page_lookup_busy_wait(vm_obj, i, TRUE, "ttm_unm"); 798 if (m == NULL) 799 continue; 800 cdev_pager_free_page(vm_obj, m); 801 } 802 VM_OBJECT_UNLOCK(vm_obj); 803 804 vm_object_deallocate(vm_obj); 805 } 806 #endif 807 808 #if 0 809 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo) 810 { 811 if (vma->vm_pgoff != 0) 812 return -EACCES; 813 814 vma->vm_ops = &ttm_bo_vm_ops; 815 vma->vm_private_data = ttm_bo_reference(bo); 816 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND; 817 return 0; 818 } 819 EXPORT_SYMBOL(ttm_fbdev_mmap); 820 #endif 821