xref: /dragonfly/sys/dev/drm/ttm/ttm_bo_vm.c (revision c9c5aa9e)
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;
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 	 * OBJT_MGTDEVICE does not pre-allocate the page.
460 	 */
461 	KKASSERT(*mres == NULL);
462 
463 retry:
464 	m = NULL;
465 
466 	/*
467 	 * Work around locking order reversal in fault / nopfn
468 	 * between mmap_sem and bo_reserve: Perform a trylock operation
469 	 * for reserve, and if it fails, retry the fault after waiting
470 	 * for the buffer to become unreserved.
471 	 */
472 	ret = ttm_bo_reserve(bo, true, true, NULL);
473 	if (unlikely(ret != 0)) {
474 		if (ret != -EBUSY) {
475 			retval = VM_PAGER_ERROR;
476 			goto out_unlock2;
477 		}
478 
479 		if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) || 1) {
480 			if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
481 				up_read(&vma->vm_mm->mmap_sem);
482 				(void) ttm_bo_wait_unreserved(bo);
483 			}
484 
485 			lwkt_yield();
486 			goto retry;
487 		}
488 
489 		/*
490 		 * If we'd want to change locking order to
491 		 * mmap_sem -> bo::reserve, we'd use a blocking reserve here
492 		 * instead of retrying the fault...
493 		 */
494 		retval = VM_PAGER_ERROR;
495 		goto out_unlock2;
496 	}
497 
498 	if (bdev->driver->fault_reserve_notify) {
499 		ret = bdev->driver->fault_reserve_notify(bo);
500 		switch (ret) {
501 		case 0:
502 			break;
503 		case -EBUSY:
504 			lwkt_yield();
505 			/* fall through */
506 		case -ERESTARTSYS:
507 		case -EINTR:
508 			retval = VM_PAGER_ERROR;
509 			goto out_unlock1;
510 		default:
511 			retval = VM_PAGER_ERROR;
512 			goto out_unlock1;
513 		}
514 	}
515 
516 	/*
517 	 * Wait for buffer data in transit, due to a pipelined
518 	 * move.
519 	 */
520 	ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
521 	if (unlikely(ret != 0)) {
522 		retval = VM_PAGER_ERROR;
523 		goto out_unlock1;
524 	}
525 
526 	ret = ttm_mem_io_lock(man, true);
527 	if (unlikely(ret != 0)) {
528 		retval = VM_PAGER_ERROR;
529 		goto out_unlock1;
530 	}
531 	ret = ttm_mem_io_reserve_vm(bo);
532 	if (unlikely(ret != 0)) {
533 		retval = VM_PAGER_ERROR;
534 		goto out_io_unlock1;
535 	}
536 	if (unlikely(OFF_TO_IDX(offset) >= bo->num_pages)) {
537 		retval = VM_PAGER_ERROR;
538 		goto out_io_unlock1;
539 	}
540 
541 	/*
542 	 * Lookup the real page.
543 	 *
544 	 * Strictly, we're not allowed to modify vma->vm_page_prot here,
545 	 * since the mmap_sem is only held in read mode. However, we
546 	 * modify only the caching bits of vma->vm_page_prot and
547 	 * consider those bits protected by
548 	 * the bo->mutex, as we should be the only writers.
549 	 * There shouldn't really be any readers of these bits except
550 	 * within vm_insert_mixed()? fork?
551 	 *
552 	 * TODO: Add a list of vmas to the bo, and change the
553 	 * vma->vm_page_prot when the object changes caching policy, with
554 	 * the correct locks held.
555 	 */
556 
557 	/*
558 	 * Make a local vma copy to modify the page_prot member
559 	 * and vm_flags if necessary. The vma parameter is protected
560 	 * by mmap_sem in write mode.
561 	 */
562 	cvma = *vma;
563 #if 0
564 	cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
565 #else
566 	cvma.vm_page_prot = 0;
567 #endif
568 
569 	if (bo->mem.bus.is_iomem) {
570 #ifdef __DragonFly__
571 		m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base +
572 						  bo->mem.bus.offset + offset);
573 		pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement, 0));
574 #endif
575 		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
576 						cvma.vm_page_prot);
577 	} else {
578 		ttm = bo->ttm;
579 		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
580 						cvma.vm_page_prot);
581 
582 		/* Allocate all page at once, most common usage */
583 		if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
584 			retval = VM_PAGER_ERROR;
585 			goto out_io_unlock1;
586 		}
587 
588 		m = (struct vm_page *)ttm->pages[OFF_TO_IDX(offset)];
589 		if (unlikely(!m)) {
590 			retval = VM_PAGER_ERROR;
591 			goto out_io_unlock1;
592 		}
593 		pmap_page_set_memattr(m,
594 		    (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
595 		    VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement, 0));
596 	}
597 
598 	if (vm_page_busy_try(m, FALSE)) {
599 		kprintf("r");
600 		vm_page_sleep_busy(m, FALSE, "ttmvmf");
601 		ttm_mem_io_unlock(man);
602 		ttm_bo_unreserve(bo);
603 		goto retry;
604 	}
605 
606 	/*
607 	 * Return our fake page BUSYd.  Do not index it into the VM object.
608 	 * The caller will enter it into the pmap.
609 	 */
610 	m->valid = VM_PAGE_BITS_ALL;
611 	*mres = m;
612 
613 out_io_unlock1:
614 	ttm_mem_io_unlock(man);
615 out_unlock1:
616 	ttm_bo_unreserve(bo);
617 out_unlock2:
618 	vm_object_pip_wakeup(vm_obj);
619 	return (retval);
620 }
621 
622 static int
623 ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
624 	       vm_ooffset_t foff, struct ucred *cred, u_short *color)
625 {
626 
627 	/*
628 	 * On Linux, a reference to the buffer object is acquired here.
629 	 * The reason is that this function is not called when the
630 	 * mmap() is initialized, but only when a process forks for
631 	 * instance. Therefore on Linux, the reference on the bo is
632 	 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's
633 	 * then released in ttm_bo_vm_close().
634 	 *
635 	 * Here, this function is called during mmap() intialization.
636 	 * Thus, the reference acquired in ttm_bo_mmap_single() is
637 	 * sufficient.
638 	 */
639 	*color = 0;
640 	return (0);
641 }
642 
643 static void
644 ttm_bo_vm_dtor(void *handle)
645 {
646 	struct ttm_buffer_object *bo = handle;
647 
648 	ttm_bo_unref(&bo);
649 }
650 
651 static struct cdev_pager_ops ttm_pager_ops = {
652 	.cdev_pg_fault = ttm_bo_vm_fault_dfly,
653 	.cdev_pg_ctor = ttm_bo_vm_ctor,
654 	.cdev_pg_dtor = ttm_bo_vm_dtor
655 };
656 
657 /*
658  * Called from drm_drv.c
659  *
660  * *offset - object offset in bytes
661  * size	   - map size in bytes
662  *
663  * We setup a dummy vma (for now) and call ttm_bo_mmap().  Then we setup
664  * our own VM object and dfly ops.  Note that the ops supplied by
665  * ttm_bo_mmap() are not currently used.
666  */
667 int
668 ttm_bo_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
669 		   vm_size_t size, struct vm_object **obj_res, int nprot)
670 {
671 	struct ttm_bo_device *bdev = dev->drm_ttm_bdev;
672 	struct ttm_buffer_object *bo;
673 	struct vm_object *vm_obj;
674 	struct vm_area_struct vma;
675 	int ret;
676 
677 	*obj_res = NULL;
678 
679 	bzero(&vma, sizeof(vma));
680 	vma.vm_start = *offset;		/* bdev-relative offset */
681 	vma.vm_end = vma.vm_start + size;
682 	vma.vm_pgoff = vma.vm_start >> PAGE_SHIFT;
683 	/* vma.vm_page_prot */
684 	/* vma.vm_flags */
685 
686 	/*
687 	 * Call the linux-ported code to do the work, and on success just
688 	 * setup our own VM object and ignore what the linux code did other
689 	 * then supplying us the 'bo'.
690 	 */
691 	ret = ttm_bo_mmap(NULL, &vma, bdev);
692 
693 	if (ret == 0) {
694 		bo = vma.vm_private_data;
695 		vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE,
696 					     &ttm_pager_ops,
697 					     size, nprot, 0,
698 					     curthread->td_ucred);
699 		if (vm_obj) {
700 			*obj_res = vm_obj;
701 			*offset = 0;		/* object-relative offset */
702 		} else {
703 			ttm_bo_unref(&bo);
704 			ret = EINVAL;
705 		}
706 	}
707 	return ret;
708 }
709 EXPORT_SYMBOL(ttm_bo_mmap_single);
710 
711 #ifdef __DragonFly__
712 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
713 
714 void
715 ttm_bo_release_mmap(struct ttm_buffer_object *bo)
716 {
717 	vm_object_t vm_obj;
718 
719 	vm_obj = cdev_pager_lookup(bo);
720 	if (vm_obj == NULL)
721 		return;
722 
723 	VM_OBJECT_LOCK(vm_obj);
724 	vm_object_page_remove(vm_obj, 0, 0, false);
725 	VM_OBJECT_UNLOCK(vm_obj);
726 
727 	vm_object_deallocate(vm_obj);
728 }
729 #endif
730 
731 #if 0
732 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
733 {
734 	if (vma->vm_pgoff != 0)
735 		return -EACCES;
736 
737 	vma->vm_ops = &ttm_bo_vm_ops;
738 	vma->vm_private_data = ttm_bo_reference(bo);
739 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
740 	return 0;
741 }
742 EXPORT_SYMBOL(ttm_fbdev_mmap);
743 #endif
744