xref: /dragonfly/sys/dev/drm/ttm/ttm_bo_vm.c (revision 11050bbc)
15718399fSFrançois Tigeot /**************************************************************************
25718399fSFrançois Tigeot  *
35718399fSFrançois Tigeot  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
45718399fSFrançois Tigeot  * All Rights Reserved.
55718399fSFrançois Tigeot  *
65718399fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
75718399fSFrançois Tigeot  * copy of this software and associated documentation files (the
85718399fSFrançois Tigeot  * "Software"), to deal in the Software without restriction, including
95718399fSFrançois Tigeot  * without limitation the rights to use, copy, modify, merge, publish,
105718399fSFrançois Tigeot  * distribute, sub license, and/or sell copies of the Software, and to
115718399fSFrançois Tigeot  * permit persons to whom the Software is furnished to do so, subject to
125718399fSFrançois Tigeot  * the following conditions:
135718399fSFrançois Tigeot  *
145718399fSFrançois Tigeot  * The above copyright notice and this permission notice (including the
155718399fSFrançois Tigeot  * next paragraph) shall be included in all copies or substantial portions
165718399fSFrançois Tigeot  * of the Software.
175718399fSFrançois Tigeot  *
185718399fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
195718399fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
205718399fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
215718399fSFrançois Tigeot  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
225718399fSFrançois Tigeot  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
235718399fSFrançois Tigeot  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
245718399fSFrançois Tigeot  * USE OR OTHER DEALINGS IN THE SOFTWARE.
255718399fSFrançois Tigeot  *
265718399fSFrançois Tigeot  **************************************************************************/
275718399fSFrançois Tigeot /*
285718399fSFrançois Tigeot  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
295718399fSFrançois Tigeot  */
305718399fSFrançois Tigeot 
31c66857ebSFrançois Tigeot #define pr_fmt(fmt) "[TTM] " fmt
32c66857ebSFrançois Tigeot 
33932d855eSSergey Zigachev #include <drm/ttm/ttm_module.h>
34932d855eSSergey Zigachev #include <drm/ttm/ttm_bo_driver.h>
35932d855eSSergey Zigachev #include <drm/ttm/ttm_placement.h>
36a34b4168SMatthew Dillon #include <drm/drm_vma_manager.h>
37a34b4168SMatthew Dillon #include <linux/mm.h>
38d78d3a22SFrançois Tigeot #include <linux/pfn_t.h>
39a34b4168SMatthew Dillon #include <linux/rbtree.h>
40a34b4168SMatthew Dillon #include <linux/module.h>
41a34b4168SMatthew Dillon #include <linux/uaccess.h>
423f2dd94aSFrançois Tigeot #include <linux/mem_encrypt.h>
43a34b4168SMatthew Dillon 
44a34b4168SMatthew Dillon #include <sys/sysctl.h>
455718399fSFrançois Tigeot #include <vm/vm.h>
465718399fSFrançois Tigeot #include <vm/vm_page.h>
4762bba8f6SFrançois Tigeot #include <vm/vm_page2.h>
4862bba8f6SFrançois Tigeot 
495718399fSFrançois Tigeot #define TTM_BO_VM_NUM_PREFAULT 16
505718399fSFrançois Tigeot 
ttm_bo_vm_fault_idle(struct ttm_buffer_object * bo,struct vm_area_struct * vma,struct vm_fault * vmf)5143e748b9SFrançois Tigeot static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
5243e748b9SFrançois Tigeot 				struct vm_area_struct *vma,
5343e748b9SFrançois Tigeot 				struct vm_fault *vmf)
5443e748b9SFrançois Tigeot {
5543e748b9SFrançois Tigeot 	int ret = 0;
5643e748b9SFrançois Tigeot 
571dedbd3bSFrançois Tigeot 	if (likely(!bo->moving))
5843e748b9SFrançois Tigeot 		goto out_unlock;
5943e748b9SFrançois Tigeot 
6043e748b9SFrançois Tigeot 	/*
6143e748b9SFrançois Tigeot 	 * Quick non-stalling check for idle.
6243e748b9SFrançois Tigeot 	 */
636559babbSFrançois Tigeot 	if (dma_fence_is_signaled(bo->moving))
641dedbd3bSFrançois Tigeot 		goto out_clear;
6543e748b9SFrançois Tigeot 
6643e748b9SFrançois Tigeot 	/*
6743e748b9SFrançois Tigeot 	 * If possible, avoid waiting for GPU with mmap_sem
6843e748b9SFrançois Tigeot 	 * held.
6943e748b9SFrançois Tigeot 	 */
7043e748b9SFrançois Tigeot 	if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
7143e748b9SFrançois Tigeot 		ret = VM_FAULT_RETRY;
7243e748b9SFrançois Tigeot 		if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
7343e748b9SFrançois Tigeot 			goto out_unlock;
7443e748b9SFrançois Tigeot 
75932d855eSSergey Zigachev 		ttm_bo_get(bo);
76*68bfabadSSergey Zigachev 		up_read(&vmf->vma->vm_mm->mmap_sem);		/* release */
776559babbSFrançois Tigeot 		(void) dma_fence_wait(bo->moving, true);
784be47400SFrançois Tigeot 		ttm_bo_unreserve(bo);
79932d855eSSergey Zigachev 		ttm_bo_put(bo);
80*68bfabadSSergey Zigachev 		down_read(&vmf->vma->vm_mm->mmap_sem);		/* acquire */
8143e748b9SFrançois Tigeot 		goto out_unlock;
8243e748b9SFrançois Tigeot 	}
8343e748b9SFrançois Tigeot 
8443e748b9SFrançois Tigeot 	/*
8543e748b9SFrançois Tigeot 	 * Ordinary wait.
8643e748b9SFrançois Tigeot 	 */
876559babbSFrançois Tigeot 	ret = dma_fence_wait(bo->moving, true);
881dedbd3bSFrançois Tigeot 	if (unlikely(ret != 0)) {
8943e748b9SFrançois Tigeot 		ret = (ret != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
9043e748b9SFrançois Tigeot 			VM_FAULT_NOPAGE;
911dedbd3bSFrançois Tigeot 		goto out_unlock;
921dedbd3bSFrançois Tigeot 	}
931dedbd3bSFrançois Tigeot 
941dedbd3bSFrançois Tigeot out_clear:
956559babbSFrançois Tigeot 	dma_fence_put(bo->moving);
961dedbd3bSFrançois Tigeot 	bo->moving = NULL;
9743e748b9SFrançois Tigeot 
9843e748b9SFrançois Tigeot out_unlock:
9943e748b9SFrançois Tigeot 	return ret;
10043e748b9SFrançois Tigeot }
10143e748b9SFrançois Tigeot 
102a34b4168SMatthew Dillon /*
103a34b4168SMatthew Dillon  * Always unstall on unexpected vm_page alias, fatal bus fault.
104a34b4168SMatthew Dillon  * Set to 0 to stall, set to positive count to unstall N times,
105a34b4168SMatthew Dillon  * then stall again.
106a34b4168SMatthew Dillon  */
107a34b4168SMatthew Dillon static int drm_unstall = -1;
108a34b4168SMatthew Dillon SYSCTL_INT(_debug, OID_AUTO, unstall, CTLFLAG_RW, &drm_unstall, 0, "");
109a34b4168SMatthew Dillon 
ttm_bo_vm_fault(struct vm_area_struct * vma,struct vm_fault * vmf)110a34b4168SMatthew Dillon static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
111ddea3d8dSzrj {
112a34b4168SMatthew Dillon 	/* see ttm_bo_mmap_single() at end of this file */
113a34b4168SMatthew Dillon 	/* ttm_bo_vm_ops not currently used, no entry should occur */
114a34b4168SMatthew Dillon 	panic("ttm_bo_vm_fault");
115a34b4168SMatthew Dillon #if 0
116a34b4168SMatthew Dillon 	struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
117a34b4168SMatthew Dillon 	    vma->vm_private_data;
1185718399fSFrançois Tigeot 	struct ttm_bo_device *bdev = bo->bdev;
119a34b4168SMatthew Dillon 	unsigned long page_offset;
120a34b4168SMatthew Dillon 	unsigned long page_last;
121a34b4168SMatthew Dillon 	unsigned long pfn;
1225718399fSFrançois Tigeot 	struct ttm_tt *ttm = NULL;
123a34b4168SMatthew Dillon 	struct page *page;
1245718399fSFrançois Tigeot 	int ret;
125a34b4168SMatthew Dillon 	int i;
1264be47400SFrançois Tigeot 	unsigned long address = vmf->address;
127a34b4168SMatthew Dillon 	int retval = VM_FAULT_NOPAGE;
1285718399fSFrançois Tigeot 	struct ttm_mem_type_manager *man =
1295718399fSFrançois Tigeot 		&bdev->man[bo->mem.mem_type];
13043e748b9SFrançois Tigeot 	struct vm_area_struct cvma;
1315718399fSFrançois Tigeot 
132a34b4168SMatthew Dillon 	/*
133a34b4168SMatthew Dillon 	 * Work around locking order reversal in fault / nopfn
134a34b4168SMatthew Dillon 	 * between mmap_sem and bo_reserve: Perform a trylock operation
13543e748b9SFrançois Tigeot 	 * for reserve, and if it fails, retry the fault after waiting
13643e748b9SFrançois Tigeot 	 * for the buffer to become unreserved.
137a34b4168SMatthew Dillon 	 */
138d78d3a22SFrançois Tigeot 	ret = ttm_bo_reserve(bo, true, true, NULL);
1395718399fSFrançois Tigeot 	if (unlikely(ret != 0)) {
14043e748b9SFrançois Tigeot 		if (ret != -EBUSY)
141a34b4168SMatthew Dillon 			return VM_FAULT_NOPAGE;
14243e748b9SFrançois Tigeot 
14343e748b9SFrançois Tigeot 		if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
14443e748b9SFrançois Tigeot 			if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
1454be47400SFrançois Tigeot 				ttm_bo_reference(bo);
14643e748b9SFrançois Tigeot 				up_read(&vma->vm_mm->mmap_sem);
14743e748b9SFrançois Tigeot 				(void) ttm_bo_wait_unreserved(bo);
1484be47400SFrançois Tigeot 				ttm_bo_unref(&bo);
14943e748b9SFrançois Tigeot 			}
15043e748b9SFrançois Tigeot 
15143e748b9SFrançois Tigeot 			return VM_FAULT_RETRY;
15243e748b9SFrançois Tigeot 		}
15343e748b9SFrançois Tigeot 
15443e748b9SFrançois Tigeot 		/*
15543e748b9SFrançois Tigeot 		 * If we'd want to change locking order to
15643e748b9SFrançois Tigeot 		 * mmap_sem -> bo::reserve, we'd use a blocking reserve here
15743e748b9SFrançois Tigeot 		 * instead of retrying the fault...
15843e748b9SFrançois Tigeot 		 */
15943e748b9SFrançois Tigeot 		return VM_FAULT_NOPAGE;
16043e748b9SFrançois Tigeot 	}
16143e748b9SFrançois Tigeot 
16243e748b9SFrançois Tigeot 	/*
16343e748b9SFrançois Tigeot 	 * Refuse to fault imported pages. This should be handled
16443e748b9SFrançois Tigeot 	 * (if at all) by redirecting mmap to the exporter.
16543e748b9SFrançois Tigeot 	 */
16643e748b9SFrançois Tigeot 	if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
16743e748b9SFrançois Tigeot 		retval = VM_FAULT_SIGBUS;
16843e748b9SFrançois Tigeot 		goto out_unlock;
1695718399fSFrançois Tigeot 	}
1705718399fSFrançois Tigeot 
1715718399fSFrançois Tigeot 	if (bdev->driver->fault_reserve_notify) {
1725718399fSFrançois Tigeot 		ret = bdev->driver->fault_reserve_notify(bo);
1735718399fSFrançois Tigeot 		switch (ret) {
1745718399fSFrançois Tigeot 		case 0:
1755718399fSFrançois Tigeot 			break;
1765718399fSFrançois Tigeot 		case -EBUSY:
177a34b4168SMatthew Dillon 		case -ERESTARTSYS:
178a34b4168SMatthew Dillon 			retval = VM_FAULT_NOPAGE;
179a34b4168SMatthew Dillon 			goto out_unlock;
180a34b4168SMatthew Dillon 		default:
181a34b4168SMatthew Dillon 			retval = VM_FAULT_SIGBUS;
182a34b4168SMatthew Dillon 			goto out_unlock;
183a34b4168SMatthew Dillon 		}
184a34b4168SMatthew Dillon 	}
185a34b4168SMatthew Dillon 
186a34b4168SMatthew Dillon 	/*
187a34b4168SMatthew Dillon 	 * Wait for buffer data in transit, due to a pipelined
188a34b4168SMatthew Dillon 	 * move.
189a34b4168SMatthew Dillon 	 */
190a34b4168SMatthew Dillon 
19143e748b9SFrançois Tigeot 	ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
192a34b4168SMatthew Dillon 	if (unlikely(ret != 0)) {
19343e748b9SFrançois Tigeot 		retval = ret;
1944be47400SFrançois Tigeot 
1954be47400SFrançois Tigeot 		if (retval == VM_FAULT_RETRY &&
1964be47400SFrançois Tigeot 		    !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
1974be47400SFrançois Tigeot 			/* The BO has already been unreserved. */
1984be47400SFrançois Tigeot 			return retval;
1994be47400SFrançois Tigeot 		}
2004be47400SFrançois Tigeot 
201a34b4168SMatthew Dillon 		goto out_unlock;
202a34b4168SMatthew Dillon 	}
203a34b4168SMatthew Dillon 
204a34b4168SMatthew Dillon 	ret = ttm_mem_io_lock(man, true);
205a34b4168SMatthew Dillon 	if (unlikely(ret != 0)) {
206a34b4168SMatthew Dillon 		retval = VM_FAULT_NOPAGE;
207a34b4168SMatthew Dillon 		goto out_unlock;
208a34b4168SMatthew Dillon 	}
209a34b4168SMatthew Dillon 	ret = ttm_mem_io_reserve_vm(bo);
210a34b4168SMatthew Dillon 	if (unlikely(ret != 0)) {
211a34b4168SMatthew Dillon 		retval = VM_FAULT_SIGBUS;
212a34b4168SMatthew Dillon 		goto out_io_unlock;
213a34b4168SMatthew Dillon 	}
214a34b4168SMatthew Dillon 
215a34b4168SMatthew Dillon 	page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
21643e748b9SFrançois Tigeot 		vma->vm_pgoff - drm_vma_node_start(&bo->vma_node);
21743e748b9SFrançois Tigeot 	page_last = vma_pages(vma) + vma->vm_pgoff -
21843e748b9SFrançois Tigeot 		drm_vma_node_start(&bo->vma_node);
219a34b4168SMatthew Dillon 
220a34b4168SMatthew Dillon 	if (unlikely(page_offset >= bo->num_pages)) {
221a34b4168SMatthew Dillon 		retval = VM_FAULT_SIGBUS;
222a34b4168SMatthew Dillon 		goto out_io_unlock;
223a34b4168SMatthew Dillon 	}
224a34b4168SMatthew Dillon 
225a34b4168SMatthew Dillon 	/*
22643e748b9SFrançois Tigeot 	 * Make a local vma copy to modify the page_prot member
22743e748b9SFrançois Tigeot 	 * and vm_flags if necessary. The vma parameter is protected
22843e748b9SFrançois Tigeot 	 * by mmap_sem in write mode.
229a34b4168SMatthew Dillon 	 */
23043e748b9SFrançois Tigeot 	cvma = *vma;
23143e748b9SFrançois Tigeot 	cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
23243e748b9SFrançois Tigeot 
233a34b4168SMatthew Dillon 	if (bo->mem.bus.is_iomem) {
23443e748b9SFrançois Tigeot 		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
23543e748b9SFrançois Tigeot 						cvma.vm_page_prot);
236a34b4168SMatthew Dillon 	} else {
237932d855eSSergey Zigachev 		struct ttm_operation_ctx ctx = {
238932d855eSSergey Zigachev 			.interruptible = false,
239932d855eSSergey Zigachev 			.no_wait_gpu = false,
240932d855eSSergey Zigachev 			.flags = TTM_OPT_FLAG_FORCE_ALLOC
241932d855eSSergey Zigachev 
242932d855eSSergey Zigachev 		};
243932d855eSSergey Zigachev 
244a34b4168SMatthew Dillon 		ttm = bo->ttm;
24543e748b9SFrançois Tigeot 		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
24643e748b9SFrançois Tigeot 						cvma.vm_page_prot);
247a34b4168SMatthew Dillon 
248a34b4168SMatthew Dillon 		/* Allocate all page at once, most common usage */
249932d855eSSergey Zigachev 		if (ttm_tt_populate(ttm, &ctx)) {
250a34b4168SMatthew Dillon 			retval = VM_FAULT_OOM;
251a34b4168SMatthew Dillon 			goto out_io_unlock;
252a34b4168SMatthew Dillon 		}
253a34b4168SMatthew Dillon 	}
254a34b4168SMatthew Dillon 
255a34b4168SMatthew Dillon 	/*
256a34b4168SMatthew Dillon 	 * Speculatively prefault a number of pages. Only error on
257a34b4168SMatthew Dillon 	 * first page.
258a34b4168SMatthew Dillon 	 */
259a34b4168SMatthew Dillon 	for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
260a34b4168SMatthew Dillon 		if (bo->mem.bus.is_iomem)
261a34b4168SMatthew Dillon 			pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
262a34b4168SMatthew Dillon 		else {
263a34b4168SMatthew Dillon 			page = ttm->pages[page_offset];
264a34b4168SMatthew Dillon 			if (unlikely(!page && i == 0)) {
265a34b4168SMatthew Dillon 				retval = VM_FAULT_OOM;
266a34b4168SMatthew Dillon 				goto out_io_unlock;
267a34b4168SMatthew Dillon 			} else if (unlikely(!page)) {
268a34b4168SMatthew Dillon 				break;
269a34b4168SMatthew Dillon 			}
27043e748b9SFrançois Tigeot 			page->mapping = vma->vm_file->f_mapping;
27143e748b9SFrançois Tigeot 			page->index = drm_vma_node_start(&bo->vma_node) +
27243e748b9SFrançois Tigeot 				page_offset;
273a34b4168SMatthew Dillon 			pfn = page_to_pfn(page);
274a34b4168SMatthew Dillon 		}
275a34b4168SMatthew Dillon 
27643e748b9SFrançois Tigeot 		if (vma->vm_flags & VM_MIXEDMAP)
277d78d3a22SFrançois Tigeot 			ret = vm_insert_mixed(&cvma, address,
278d78d3a22SFrançois Tigeot 					__pfn_to_pfn_t(pfn, PFN_DEV));
27943e748b9SFrançois Tigeot 		else
28043e748b9SFrançois Tigeot 			ret = vm_insert_pfn(&cvma, address, pfn);
28143e748b9SFrançois Tigeot 
282a34b4168SMatthew Dillon 		/*
283a34b4168SMatthew Dillon 		 * Somebody beat us to this PTE or prefaulting to
284a34b4168SMatthew Dillon 		 * an already populated PTE, or prefaulting error.
285a34b4168SMatthew Dillon 		 */
286a34b4168SMatthew Dillon 
287a34b4168SMatthew Dillon 		if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
288a34b4168SMatthew Dillon 			break;
289a34b4168SMatthew Dillon 		else if (unlikely(ret != 0)) {
290a34b4168SMatthew Dillon 			retval =
291a34b4168SMatthew Dillon 			    (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
292a34b4168SMatthew Dillon 			goto out_io_unlock;
293a34b4168SMatthew Dillon 		}
294a34b4168SMatthew Dillon 
295a34b4168SMatthew Dillon 		address += PAGE_SIZE;
296a34b4168SMatthew Dillon 		if (unlikely(++page_offset >= page_last))
297a34b4168SMatthew Dillon 			break;
298a34b4168SMatthew Dillon 	}
299a34b4168SMatthew Dillon out_io_unlock:
300a34b4168SMatthew Dillon 	ttm_mem_io_unlock(man);
301a34b4168SMatthew Dillon out_unlock:
302a34b4168SMatthew Dillon 	ttm_bo_unreserve(bo);
303a34b4168SMatthew Dillon 	return retval;
304a34b4168SMatthew Dillon #endif
305a34b4168SMatthew Dillon }
306a34b4168SMatthew Dillon 
307a34b4168SMatthew Dillon /* ttm_bo_vm_ops not currently used, no entry should occur */
ttm_bo_vm_open(struct vm_area_struct * vma)308a34b4168SMatthew Dillon static void ttm_bo_vm_open(struct vm_area_struct *vma)
309a34b4168SMatthew Dillon {
310a34b4168SMatthew Dillon 	struct ttm_buffer_object *bo =
311a34b4168SMatthew Dillon 	    (struct ttm_buffer_object *)vma->vm_private_data;
312a34b4168SMatthew Dillon 
31343e748b9SFrançois Tigeot #if 0
31443e748b9SFrançois Tigeot 	WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
31543e748b9SFrançois Tigeot #endif
31643e748b9SFrançois Tigeot 
317932d855eSSergey Zigachev 	ttm_bo_get(bo);
318a34b4168SMatthew Dillon }
319a34b4168SMatthew Dillon 
320a34b4168SMatthew Dillon /* ttm_bo_vm_ops not currently used, no entry should occur */
ttm_bo_vm_close(struct vm_area_struct * vma)321a34b4168SMatthew Dillon static void ttm_bo_vm_close(struct vm_area_struct *vma)
322a34b4168SMatthew Dillon {
323a34b4168SMatthew Dillon 	struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
324a34b4168SMatthew Dillon 
325932d855eSSergey Zigachev 	ttm_bo_put(bo);
326a34b4168SMatthew Dillon 	vma->vm_private_data = NULL;
327a34b4168SMatthew Dillon }
328a34b4168SMatthew Dillon 
ttm_bo_vm_access_kmap(struct ttm_buffer_object * bo,unsigned long offset,uint8_t * buf,int len,int write)3293f2dd94aSFrançois Tigeot static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo,
3303f2dd94aSFrançois Tigeot 				 unsigned long offset,
3313f2dd94aSFrançois Tigeot 				 uint8_t *buf, int len, int write)
3323f2dd94aSFrançois Tigeot {
3333f2dd94aSFrançois Tigeot 	unsigned long page = offset >> PAGE_SHIFT;
3343f2dd94aSFrançois Tigeot 	unsigned long bytes_left = len;
3353f2dd94aSFrançois Tigeot 	int ret;
3363f2dd94aSFrançois Tigeot 
3373f2dd94aSFrançois Tigeot 	/* Copy a page at a time, that way no extra virtual address
3383f2dd94aSFrançois Tigeot 	 * mapping is needed
3393f2dd94aSFrançois Tigeot 	 */
3403f2dd94aSFrançois Tigeot 	offset -= page << PAGE_SHIFT;
3413f2dd94aSFrançois Tigeot 	do {
3423f2dd94aSFrançois Tigeot 		unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
3433f2dd94aSFrançois Tigeot 		struct ttm_bo_kmap_obj map;
3443f2dd94aSFrançois Tigeot 		void *ptr;
3453f2dd94aSFrançois Tigeot 		bool is_iomem;
3463f2dd94aSFrançois Tigeot 
3473f2dd94aSFrançois Tigeot 		ret = ttm_bo_kmap(bo, page, 1, &map);
3483f2dd94aSFrançois Tigeot 		if (ret)
3493f2dd94aSFrançois Tigeot 			return ret;
3503f2dd94aSFrançois Tigeot 
3513f2dd94aSFrançois Tigeot 		ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset;
3523f2dd94aSFrançois Tigeot 		WARN_ON_ONCE(is_iomem);
3533f2dd94aSFrançois Tigeot 		if (write)
3543f2dd94aSFrançois Tigeot 			memcpy(ptr, buf, bytes);
3553f2dd94aSFrançois Tigeot 		else
3563f2dd94aSFrançois Tigeot 			memcpy(buf, ptr, bytes);
3573f2dd94aSFrançois Tigeot 		ttm_bo_kunmap(&map);
3583f2dd94aSFrançois Tigeot 
3593f2dd94aSFrançois Tigeot 		page++;
3603f2dd94aSFrançois Tigeot 		buf += bytes;
3613f2dd94aSFrançois Tigeot 		bytes_left -= bytes;
3623f2dd94aSFrançois Tigeot 		offset = 0;
3633f2dd94aSFrançois Tigeot 	} while (bytes_left);
3643f2dd94aSFrançois Tigeot 
3653f2dd94aSFrançois Tigeot 	return len;
3663f2dd94aSFrançois Tigeot }
3673f2dd94aSFrançois Tigeot 
ttm_bo_vm_access(struct vm_area_struct * vma,unsigned long addr,void * buf,int len,int write)3683f2dd94aSFrançois Tigeot static int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
3693f2dd94aSFrançois Tigeot 			    void *buf, int len, int write)
3703f2dd94aSFrançois Tigeot {
3713f2dd94aSFrançois Tigeot 	unsigned long offset = (addr) - vma->vm_start;
3723f2dd94aSFrançois Tigeot 	struct ttm_buffer_object *bo = vma->vm_private_data;
3733f2dd94aSFrançois Tigeot 	int ret;
3743f2dd94aSFrançois Tigeot 
3753f2dd94aSFrançois Tigeot 	if (len < 1 || (offset + len) >> PAGE_SHIFT > bo->num_pages)
3763f2dd94aSFrançois Tigeot 		return -EIO;
3773f2dd94aSFrançois Tigeot 
3783f2dd94aSFrançois Tigeot 	ret = ttm_bo_reserve(bo, true, false, NULL);
3793f2dd94aSFrançois Tigeot 	if (ret)
3803f2dd94aSFrançois Tigeot 		return ret;
3813f2dd94aSFrançois Tigeot 
3823f2dd94aSFrançois Tigeot 	switch (bo->mem.mem_type) {
3833f2dd94aSFrançois Tigeot 	case TTM_PL_SYSTEM:
3843f2dd94aSFrançois Tigeot 		if (unlikely(bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
3853f2dd94aSFrançois Tigeot 			ret = ttm_tt_swapin(bo->ttm);
3863f2dd94aSFrançois Tigeot 			if (unlikely(ret != 0))
3873f2dd94aSFrançois Tigeot 				return ret;
3883f2dd94aSFrançois Tigeot 		}
3893f2dd94aSFrançois Tigeot 		/* fall through */
3903f2dd94aSFrançois Tigeot 	case TTM_PL_TT:
3913f2dd94aSFrançois Tigeot 		ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write);
3923f2dd94aSFrançois Tigeot 		break;
3933f2dd94aSFrançois Tigeot 	default:
3943f2dd94aSFrançois Tigeot 		if (bo->bdev->driver->access_memory)
3953f2dd94aSFrançois Tigeot 			ret = bo->bdev->driver->access_memory(
3963f2dd94aSFrançois Tigeot 				bo, offset, buf, len, write);
3973f2dd94aSFrançois Tigeot 		else
3983f2dd94aSFrançois Tigeot 			ret = -EIO;
3993f2dd94aSFrançois Tigeot 	}
4003f2dd94aSFrançois Tigeot 
4013f2dd94aSFrançois Tigeot 	ttm_bo_unreserve(bo);
4023f2dd94aSFrançois Tigeot 
4033f2dd94aSFrançois Tigeot 	return ret;
4043f2dd94aSFrançois Tigeot }
4053f2dd94aSFrançois Tigeot 
406a34b4168SMatthew Dillon static const struct vm_operations_struct ttm_bo_vm_ops = {
407a34b4168SMatthew Dillon 	.fault = ttm_bo_vm_fault,
408a34b4168SMatthew Dillon 	.open = ttm_bo_vm_open,
4093f2dd94aSFrançois Tigeot 	.close = ttm_bo_vm_close,
4103f2dd94aSFrançois Tigeot 	.access = ttm_bo_vm_access
411a34b4168SMatthew Dillon };
412a34b4168SMatthew Dillon 
ttm_bo_vm_lookup(struct ttm_bo_device * bdev,unsigned long offset,unsigned long pages)413a34b4168SMatthew Dillon static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
414a34b4168SMatthew Dillon 						  unsigned long offset,
415a34b4168SMatthew Dillon 						  unsigned long pages)
416a34b4168SMatthew Dillon {
417a34b4168SMatthew Dillon 	struct drm_vma_offset_node *node;
418a34b4168SMatthew Dillon 	struct ttm_buffer_object *bo = NULL;
419a34b4168SMatthew Dillon 
420a34b4168SMatthew Dillon 	drm_vma_offset_lock_lookup(&bdev->vma_manager);
421a34b4168SMatthew Dillon 
422a34b4168SMatthew Dillon 	node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
423a34b4168SMatthew Dillon 	if (likely(node)) {
424a34b4168SMatthew Dillon 		bo = container_of(node, struct ttm_buffer_object, vma_node);
425a34b4168SMatthew Dillon 		if (!kref_get_unless_zero(&bo->kref))
426a34b4168SMatthew Dillon 			bo = NULL;
427a34b4168SMatthew Dillon 	}
428a34b4168SMatthew Dillon 
429a34b4168SMatthew Dillon 	drm_vma_offset_unlock_lookup(&bdev->vma_manager);
430a34b4168SMatthew Dillon 
431a34b4168SMatthew Dillon 	if (!bo)
432a34b4168SMatthew Dillon 		pr_err("Could not find buffer object to map\n");
433a34b4168SMatthew Dillon 
434a34b4168SMatthew Dillon 	return bo;
435a34b4168SMatthew Dillon }
436a34b4168SMatthew Dillon 
ttm_bo_mmap(struct file * filp,struct vm_area_struct * vma,struct ttm_bo_device * bdev)437a34b4168SMatthew Dillon int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
438a34b4168SMatthew Dillon 		struct ttm_bo_device *bdev)
439a34b4168SMatthew Dillon {
440a34b4168SMatthew Dillon 	struct ttm_bo_driver *driver;
441a34b4168SMatthew Dillon 	struct ttm_buffer_object *bo;
442a34b4168SMatthew Dillon 	int ret;
443a34b4168SMatthew Dillon 
444a34b4168SMatthew Dillon 	bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
445a34b4168SMatthew Dillon 	if (unlikely(!bo))
446a34b4168SMatthew Dillon 		return -EINVAL;
447a34b4168SMatthew Dillon 
448a34b4168SMatthew Dillon 	driver = bo->bdev->driver;
449a34b4168SMatthew Dillon 	if (unlikely(!driver->verify_access)) {
450a34b4168SMatthew Dillon 		ret = -EPERM;
451a34b4168SMatthew Dillon 		goto out_unref;
452a34b4168SMatthew Dillon 	}
453a34b4168SMatthew Dillon 	ret = driver->verify_access(bo, filp);
454a34b4168SMatthew Dillon 	if (unlikely(ret != 0))
455a34b4168SMatthew Dillon 		goto out_unref;
456a34b4168SMatthew Dillon 
457a34b4168SMatthew Dillon 	vma->vm_ops = &ttm_bo_vm_ops;
458a34b4168SMatthew Dillon 
459a34b4168SMatthew Dillon 	/*
460a34b4168SMatthew Dillon 	 * Note: We're transferring the bo reference to
461a34b4168SMatthew Dillon 	 * vma->vm_private_data here.
462a34b4168SMatthew Dillon 	 */
463a34b4168SMatthew Dillon 
464a34b4168SMatthew Dillon 	vma->vm_private_data = bo;
46543e748b9SFrançois Tigeot 
46643e748b9SFrançois Tigeot 	/*
46743e748b9SFrançois Tigeot 	 * We'd like to use VM_PFNMAP on shared mappings, where
46843e748b9SFrançois Tigeot 	 * (vma->vm_flags & VM_SHARED) != 0, for performance reasons,
46943e748b9SFrançois Tigeot 	 * but for some reason VM_PFNMAP + x86 PAT + write-combine is very
47043e748b9SFrançois Tigeot 	 * bad for performance. Until that has been sorted out, use
47143e748b9SFrançois Tigeot 	 * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719
47243e748b9SFrançois Tigeot 	 */
47343e748b9SFrançois Tigeot 	vma->vm_flags |= VM_MIXEDMAP;
47443e748b9SFrançois Tigeot 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
475a34b4168SMatthew Dillon 	return 0;
476a34b4168SMatthew Dillon out_unref:
477932d855eSSergey Zigachev 	ttm_bo_put(bo);
478a34b4168SMatthew Dillon 	return ret;
479a34b4168SMatthew Dillon }
480a34b4168SMatthew Dillon EXPORT_SYMBOL(ttm_bo_mmap);
481a34b4168SMatthew Dillon 
ttm_fbdev_mmap(struct vm_area_struct * vma,struct ttm_buffer_object * bo)482a34b4168SMatthew Dillon int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
483a34b4168SMatthew Dillon {
484a34b4168SMatthew Dillon 	if (vma->vm_pgoff != 0)
485a34b4168SMatthew Dillon 		return -EACCES;
486a34b4168SMatthew Dillon 
487932d855eSSergey Zigachev 	ttm_bo_get(bo);
488932d855eSSergey Zigachev 
489a34b4168SMatthew Dillon 	vma->vm_ops = &ttm_bo_vm_ops;
490932d855eSSergey Zigachev 	vma->vm_private_data = bo;
49143e748b9SFrançois Tigeot 	vma->vm_flags |= VM_MIXEDMAP;
49243e748b9SFrançois Tigeot 	vma->vm_flags |= VM_IO | VM_DONTEXPAND;
493a34b4168SMatthew Dillon 	return 0;
494a34b4168SMatthew Dillon }
495a34b4168SMatthew Dillon EXPORT_SYMBOL(ttm_fbdev_mmap);
496a34b4168SMatthew Dillon 
497a34b4168SMatthew Dillon /*
498a34b4168SMatthew Dillon  * DragonFlyBSD Interface
499a34b4168SMatthew Dillon  */
500a34b4168SMatthew Dillon 
50107cb5299SMatthew Dillon /*
50207cb5299SMatthew Dillon  * NOTE: This code is fragile.  This code can only be entered with *mres
50307cb5299SMatthew Dillon  *	 not NULL when *mres is a placeholder page allocated by the kernel.
50407cb5299SMatthew Dillon  */
505a34b4168SMatthew Dillon static int
ttm_bo_vm_fault_dfly(vm_object_t vm_obj,vm_ooffset_t offset,int prot,vm_page_t * mres)506a34b4168SMatthew Dillon ttm_bo_vm_fault_dfly(vm_object_t vm_obj, vm_ooffset_t offset,
507a34b4168SMatthew Dillon 		     int prot, vm_page_t *mres)
508a34b4168SMatthew Dillon {
509a34b4168SMatthew Dillon 	struct ttm_buffer_object *bo = vm_obj->handle;
510a34b4168SMatthew Dillon 	struct ttm_bo_device *bdev = bo->bdev;
511a34b4168SMatthew Dillon 	struct ttm_tt *ttm = NULL;
5122854a88cSMatthew Dillon 	vm_page_t m;
513a34b4168SMatthew Dillon 	int ret;
514a34b4168SMatthew Dillon 	int retval = VM_PAGER_OK;
5155f38e86dSFrançois Tigeot 	struct ttm_mem_type_manager *man =
5165f38e86dSFrançois Tigeot 		&bdev->man[bo->mem.mem_type];
5175f38e86dSFrançois Tigeot 	struct vm_area_struct cvma;
518a34b4168SMatthew Dillon 
5195f38e86dSFrançois Tigeot /*
5205f38e86dSFrançois Tigeot    The Linux code expects to receive these arguments:
5215f38e86dSFrançois Tigeot    - struct vm_area_struct *vma
5225f38e86dSFrançois Tigeot    - struct vm_fault *vmf
5235f38e86dSFrançois Tigeot */
5245f38e86dSFrançois Tigeot #ifdef __DragonFly__
5255f38e86dSFrançois Tigeot 	struct vm_area_struct vmas;
5265f38e86dSFrançois Tigeot 	struct vm_area_struct *vma = &vmas;
5275f38e86dSFrançois Tigeot 	struct vm_fault vmfs;
5285f38e86dSFrançois Tigeot 	struct vm_fault *vmf = &vmfs;
5295f38e86dSFrançois Tigeot 
5305f38e86dSFrançois Tigeot 	memset(vma, 0, sizeof(*vma));
5315f38e86dSFrançois Tigeot 	memset(vmf, 0, sizeof(*vmf));
5323f2dd94aSFrançois Tigeot 
5333f2dd94aSFrançois Tigeot 	vma->vm_mm = current->mm;
5343f2dd94aSFrançois Tigeot 	vmf->vma = vma;
535*68bfabadSSergey Zigachev 
536*68bfabadSSergey Zigachev 	int retry_count = 0;
5375f38e86dSFrançois Tigeot #endif
5385f38e86dSFrançois Tigeot 
539a34b4168SMatthew Dillon 	vm_object_pip_add(vm_obj, 1);
54007cb5299SMatthew Dillon 
54107cb5299SMatthew Dillon 	/*
5422854a88cSMatthew Dillon 	 * OBJT_MGTDEVICE does not pre-allocate the page.
54307cb5299SMatthew Dillon 	 */
5442854a88cSMatthew Dillon 	KKASSERT(*mres == NULL);
545a34b4168SMatthew Dillon 
546a34b4168SMatthew Dillon retry:
5473f2dd94aSFrançois Tigeot 	/* The Linux page fault handler acquires mmap_sem */
5483f2dd94aSFrançois Tigeot 	down_read(&vma->vm_mm->mmap_sem);
5493f2dd94aSFrançois Tigeot 
550a34b4168SMatthew Dillon 	m = NULL;
551a34b4168SMatthew Dillon 
552a34b4168SMatthew Dillon 	/*
5535f38e86dSFrançois Tigeot 	 * Work around locking order reversal in fault / nopfn
5545f38e86dSFrançois Tigeot 	 * between mmap_sem and bo_reserve: Perform a trylock operation
5555f38e86dSFrançois Tigeot 	 * for reserve, and if it fails, retry the fault after waiting
5565f38e86dSFrançois Tigeot 	 * for the buffer to become unreserved.
557a34b4168SMatthew Dillon 	 */
5585f38e86dSFrançois Tigeot 	ret = ttm_bo_reserve(bo, true, true, NULL);
559a34b4168SMatthew Dillon 	if (unlikely(ret != 0)) {
5605f38e86dSFrançois Tigeot 		if (ret != -EBUSY) {
561a34b4168SMatthew Dillon 			retval = VM_PAGER_ERROR;
562a34b4168SMatthew Dillon 			goto out_unlock2;
563a34b4168SMatthew Dillon 		}
564a34b4168SMatthew Dillon 
5653f2dd94aSFrançois Tigeot 		if (vmf->flags & FAULT_FLAG_ALLOW_RETRY || 1) {
5665f38e86dSFrançois Tigeot 			if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
5675f38e86dSFrançois Tigeot 				up_read(&vma->vm_mm->mmap_sem);
5685f38e86dSFrançois Tigeot 				(void) ttm_bo_wait_unreserved(bo);
569*68bfabadSSergey Zigachev 				down_read(&vma->vm_mm->mmap_sem);
5705f38e86dSFrançois Tigeot 			}
5715f38e86dSFrançois Tigeot 
5723f2dd94aSFrançois Tigeot #ifndef __DragonFly__
5733f2dd94aSFrançois Tigeot 			return VM_FAULT_RETRY;
5743f2dd94aSFrançois Tigeot #else
5753f2dd94aSFrançois Tigeot 			up_read(&vma->vm_mm->mmap_sem);
5765f38e86dSFrançois Tigeot 			lwkt_yield();
5775f38e86dSFrançois Tigeot 			goto retry;
5783f2dd94aSFrançois Tigeot #endif
5795f38e86dSFrançois Tigeot 		}
5805f38e86dSFrançois Tigeot 
5815f38e86dSFrançois Tigeot 		/*
5825f38e86dSFrançois Tigeot 		 * If we'd want to change locking order to
5835f38e86dSFrançois Tigeot 		 * mmap_sem -> bo::reserve, we'd use a blocking reserve here
5845f38e86dSFrançois Tigeot 		 * instead of retrying the fault...
5855f38e86dSFrançois Tigeot 		 */
5865f38e86dSFrançois Tigeot 		retval = VM_PAGER_ERROR;
5875f38e86dSFrançois Tigeot 		goto out_unlock2;
5885f38e86dSFrançois Tigeot 	}
5895f38e86dSFrançois Tigeot 
590a34b4168SMatthew Dillon 	if (bdev->driver->fault_reserve_notify) {
591a34b4168SMatthew Dillon 		ret = bdev->driver->fault_reserve_notify(bo);
592a34b4168SMatthew Dillon 		switch (ret) {
593a34b4168SMatthew Dillon 		case 0:
594a34b4168SMatthew Dillon 			break;
595a34b4168SMatthew Dillon 		case -EBUSY:
596a34b4168SMatthew Dillon 			lwkt_yield();
597a34b4168SMatthew Dillon 			/* fall through */
598797013cfSFrançois Tigeot 		case -ERESTARTSYS:
5995718399fSFrançois Tigeot 		case -EINTR:
600a34b4168SMatthew Dillon 			retval = VM_PAGER_ERROR;
6012854a88cSMatthew Dillon 			goto out_unlock1;
6025718399fSFrançois Tigeot 		default:
6035718399fSFrançois Tigeot 			retval = VM_PAGER_ERROR;
6042854a88cSMatthew Dillon 			goto out_unlock1;
6055718399fSFrançois Tigeot 		}
6065718399fSFrançois Tigeot 	}
6075718399fSFrançois Tigeot 
6085718399fSFrançois Tigeot 	/*
6095718399fSFrançois Tigeot 	 * Wait for buffer data in transit, due to a pipelined
6105718399fSFrançois Tigeot 	 * move.
6115718399fSFrançois Tigeot 	 */
6125f38e86dSFrançois Tigeot 	ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
6135718399fSFrançois Tigeot 	if (unlikely(ret != 0)) {
614*68bfabadSSergey Zigachev 		if (retry_count >= 100) {
6155718399fSFrançois Tigeot 			retval = VM_PAGER_ERROR;
6162854a88cSMatthew Dillon 			goto out_unlock1;
617*68bfabadSSergey Zigachev 		} else {
618*68bfabadSSergey Zigachev 			retry_count++;
619*68bfabadSSergey Zigachev 			ttm_bo_unreserve(bo);
620*68bfabadSSergey Zigachev 			up_read(&vma->vm_mm->mmap_sem);
621*68bfabadSSergey Zigachev 			int dummy;
622*68bfabadSSergey Zigachev 			tsleep(&dummy, 0, "blah", 1);
623*68bfabadSSergey Zigachev 
624*68bfabadSSergey Zigachev 			goto retry;
625*68bfabadSSergey Zigachev 		}
6265718399fSFrançois Tigeot 	}
6275718399fSFrançois Tigeot 
6285718399fSFrançois Tigeot 	ret = ttm_mem_io_lock(man, true);
6295718399fSFrançois Tigeot 	if (unlikely(ret != 0)) {
6305718399fSFrançois Tigeot 		retval = VM_PAGER_ERROR;
6312854a88cSMatthew Dillon 		goto out_unlock1;
6325718399fSFrançois Tigeot 	}
6335718399fSFrançois Tigeot 	ret = ttm_mem_io_reserve_vm(bo);
6345718399fSFrançois Tigeot 	if (unlikely(ret != 0)) {
6355718399fSFrançois Tigeot 		retval = VM_PAGER_ERROR;
6362854a88cSMatthew Dillon 		goto out_io_unlock1;
6375718399fSFrançois Tigeot 	}
638a34b4168SMatthew Dillon 	if (unlikely(OFF_TO_IDX(offset) >= bo->num_pages)) {
639a34b4168SMatthew Dillon 		retval = VM_PAGER_ERROR;
6402854a88cSMatthew Dillon 		goto out_io_unlock1;
641a34b4168SMatthew Dillon 	}
6425718399fSFrançois Tigeot 
6435718399fSFrançois Tigeot 	/*
64407cb5299SMatthew Dillon 	 * Lookup the real page.
64507cb5299SMatthew Dillon 	 *
6465718399fSFrançois Tigeot 	 * Strictly, we're not allowed to modify vma->vm_page_prot here,
6475718399fSFrançois Tigeot 	 * since the mmap_sem is only held in read mode. However, we
6485718399fSFrançois Tigeot 	 * modify only the caching bits of vma->vm_page_prot and
6495718399fSFrançois Tigeot 	 * consider those bits protected by
6505718399fSFrançois Tigeot 	 * the bo->mutex, as we should be the only writers.
6515718399fSFrançois Tigeot 	 * There shouldn't really be any readers of these bits except
6525718399fSFrançois Tigeot 	 * within vm_insert_mixed()? fork?
6535718399fSFrançois Tigeot 	 *
6545718399fSFrançois Tigeot 	 * TODO: Add a list of vmas to the bo, and change the
6555718399fSFrançois Tigeot 	 * vma->vm_page_prot when the object changes caching policy, with
6565718399fSFrançois Tigeot 	 * the correct locks held.
6575718399fSFrançois Tigeot 	 */
6585f38e86dSFrançois Tigeot 
6595f38e86dSFrançois Tigeot 	/*
6605f38e86dSFrançois Tigeot 	 * Make a local vma copy to modify the page_prot member
6615f38e86dSFrançois Tigeot 	 * and vm_flags if necessary. The vma parameter is protected
6625f38e86dSFrançois Tigeot 	 * by mmap_sem in write mode.
6635f38e86dSFrançois Tigeot 	 */
6645f38e86dSFrançois Tigeot 	cvma = *vma;
6655f38e86dSFrançois Tigeot #if 0
6665f38e86dSFrançois Tigeot 	cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
6675f38e86dSFrançois Tigeot #else
6685f38e86dSFrançois Tigeot 	cvma.vm_page_prot = 0;
6695f38e86dSFrançois Tigeot #endif
6705f38e86dSFrançois Tigeot 
6715718399fSFrançois Tigeot 	if (bo->mem.bus.is_iomem) {
6725f38e86dSFrançois Tigeot #ifdef __DragonFly__
6735718399fSFrançois Tigeot 		m = vm_phys_fictitious_to_vm_page(bo->mem.bus.base +
6745718399fSFrançois Tigeot 						  bo->mem.bus.offset + offset);
6754f125aeaSFrançois Tigeot 		pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement, 0));
6765f38e86dSFrançois Tigeot #endif
6775f38e86dSFrançois Tigeot 		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
6785f38e86dSFrançois Tigeot 						cvma.vm_page_prot);
6795718399fSFrançois Tigeot 	} else {
6805718399fSFrançois Tigeot 		ttm = bo->ttm;
6815f38e86dSFrançois Tigeot 		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
6825f38e86dSFrançois Tigeot 						cvma.vm_page_prot);
6835f38e86dSFrançois Tigeot 
6845f38e86dSFrançois Tigeot 		/* Allocate all page at once, most common usage */
685932d855eSSergey Zigachev 		struct ttm_operation_ctx ctx = {
686932d855eSSergey Zigachev 			.interruptible = false,
687932d855eSSergey Zigachev 			.no_wait_gpu = false,
688932d855eSSergey Zigachev 			.flags = TTM_OPT_FLAG_FORCE_ALLOC
689932d855eSSergey Zigachev 
690932d855eSSergey Zigachev 		};
691932d855eSSergey Zigachev 		if (ttm_tt_populate(ttm, &ctx)) {
692a34b4168SMatthew Dillon 			retval = VM_PAGER_ERROR;
6932854a88cSMatthew Dillon 			goto out_io_unlock1;
694a34b4168SMatthew Dillon 		}
695a34b4168SMatthew Dillon 
696f0bba3d1SFrançois Tigeot 		m = (struct vm_page *)ttm->pages[OFF_TO_IDX(offset)];
6975718399fSFrançois Tigeot 		if (unlikely(!m)) {
6985718399fSFrançois Tigeot 			retval = VM_PAGER_ERROR;
6992854a88cSMatthew Dillon 			goto out_io_unlock1;
7005718399fSFrançois Tigeot 		}
7015718399fSFrançois Tigeot 		pmap_page_set_memattr(m,
7025718399fSFrançois Tigeot 		    (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
7034f125aeaSFrançois Tigeot 		    VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement, 0));
7045718399fSFrançois Tigeot 	}
7055718399fSFrançois Tigeot 
706a34b4168SMatthew Dillon 	if (vm_page_busy_try(m, FALSE)) {
707a34b4168SMatthew Dillon 		kprintf("r");
708a34b4168SMatthew Dillon 		vm_page_sleep_busy(m, FALSE, "ttmvmf");
7095718399fSFrançois Tigeot 		ttm_mem_io_unlock(man);
7105718399fSFrançois Tigeot 		ttm_bo_unreserve(bo);
7113f2dd94aSFrançois Tigeot 		up_read(&vma->vm_mm->mmap_sem);
7125718399fSFrançois Tigeot 		goto retry;
7135718399fSFrançois Tigeot 	}
714a34b4168SMatthew Dillon 
715a34b4168SMatthew Dillon 	/*
7162854a88cSMatthew Dillon 	 * Return our fake page BUSYd.  Do not index it into the VM object.
7172854a88cSMatthew Dillon 	 * The caller will enter it into the pmap.
718a34b4168SMatthew Dillon 	 */
7195718399fSFrançois Tigeot 	m->valid = VM_PAGE_BITS_ALL;
7205718399fSFrançois Tigeot 	*mres = m;
72107cb5299SMatthew Dillon 
7225718399fSFrançois Tigeot out_io_unlock1:
7235718399fSFrançois Tigeot 	ttm_mem_io_unlock(man);
7245718399fSFrançois Tigeot out_unlock1:
7255718399fSFrançois Tigeot 	ttm_bo_unreserve(bo);
726a34b4168SMatthew Dillon out_unlock2:
7275718399fSFrançois Tigeot 	vm_object_pip_wakeup(vm_obj);
728*68bfabadSSergey Zigachev 	up_read(&vma->vm_mm->mmap_sem);
729*68bfabadSSergey Zigachev 
7305718399fSFrançois Tigeot 	return (retval);
7315718399fSFrançois Tigeot }
7325718399fSFrançois Tigeot 
7335718399fSFrançois Tigeot static int
ttm_bo_vm_ctor(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred,u_short * color)7345718399fSFrançois Tigeot ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
7355718399fSFrançois Tigeot 	       vm_ooffset_t foff, struct ucred *cred, u_short *color)
7365718399fSFrançois Tigeot {
7375718399fSFrançois Tigeot 
7385718399fSFrançois Tigeot 	/*
7396f486c69SFrançois Tigeot 	 * On Linux, a reference to the buffer object is acquired here.
7406f486c69SFrançois Tigeot 	 * The reason is that this function is not called when the
7416f486c69SFrançois Tigeot 	 * mmap() is initialized, but only when a process forks for
7426f486c69SFrançois Tigeot 	 * instance. Therefore on Linux, the reference on the bo is
7436f486c69SFrançois Tigeot 	 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's
7446f486c69SFrançois Tigeot 	 * then released in ttm_bo_vm_close().
7456f486c69SFrançois Tigeot 	 *
7466f486c69SFrançois Tigeot 	 * Here, this function is called during mmap() intialization.
7476f486c69SFrançois Tigeot 	 * Thus, the reference acquired in ttm_bo_mmap_single() is
7486f486c69SFrançois Tigeot 	 * sufficient.
7495718399fSFrançois Tigeot 	 */
7505718399fSFrançois Tigeot 	*color = 0;
7515718399fSFrançois Tigeot 	return (0);
7525718399fSFrançois Tigeot }
7535718399fSFrançois Tigeot 
7545718399fSFrançois Tigeot static void
ttm_bo_vm_dtor(void * handle)7555718399fSFrançois Tigeot ttm_bo_vm_dtor(void *handle)
7565718399fSFrançois Tigeot {
7575718399fSFrançois Tigeot 	struct ttm_buffer_object *bo = handle;
7585718399fSFrançois Tigeot 
7595718399fSFrançois Tigeot 	ttm_bo_unref(&bo);
7605718399fSFrançois Tigeot }
7615718399fSFrançois Tigeot 
7625718399fSFrançois Tigeot static struct cdev_pager_ops ttm_pager_ops = {
763a34b4168SMatthew Dillon 	.cdev_pg_fault = ttm_bo_vm_fault_dfly,
7645718399fSFrançois Tigeot 	.cdev_pg_ctor = ttm_bo_vm_ctor,
7655718399fSFrançois Tigeot 	.cdev_pg_dtor = ttm_bo_vm_dtor
7665718399fSFrançois Tigeot };
7675718399fSFrançois Tigeot 
768a34b4168SMatthew Dillon /*
769a34b4168SMatthew Dillon  * Called from drm_drv.c
770a34b4168SMatthew Dillon  *
771a34b4168SMatthew Dillon  * *offset - object offset in bytes
772a34b4168SMatthew Dillon  * size	   - map size in bytes
773a34b4168SMatthew Dillon  *
774a34b4168SMatthew Dillon  * We setup a dummy vma (for now) and call ttm_bo_mmap().  Then we setup
775a34b4168SMatthew Dillon  * our own VM object and dfly ops.  Note that the ops supplied by
776a34b4168SMatthew Dillon  * ttm_bo_mmap() are not currently used.
777a34b4168SMatthew Dillon  */
7785718399fSFrançois Tigeot int
ttm_bo_mmap_single(struct file * fp,struct drm_device * dev,vm_ooffset_t * offset,vm_size_t size,struct vm_object ** obj_res,int nprot)779932d855eSSergey Zigachev ttm_bo_mmap_single(struct file *fp, struct drm_device *dev,
780932d855eSSergey Zigachev 		   vm_ooffset_t *offset, vm_size_t size,
781932d855eSSergey Zigachev 		   struct vm_object **obj_res, int nprot)
7825718399fSFrançois Tigeot {
783a34b4168SMatthew Dillon 	struct ttm_bo_device *bdev = dev->drm_ttm_bdev;
7845718399fSFrançois Tigeot 	struct ttm_buffer_object *bo;
7855718399fSFrançois Tigeot 	struct vm_object *vm_obj;
786a34b4168SMatthew Dillon 	struct vm_area_struct vma;
7875718399fSFrançois Tigeot 	int ret;
7885718399fSFrançois Tigeot 
789f6201ebfSMatthew Dillon 	*obj_res = NULL;
790f6201ebfSMatthew Dillon 
791a34b4168SMatthew Dillon 	bzero(&vma, sizeof(vma));
792a34b4168SMatthew Dillon 	vma.vm_start = *offset;		/* bdev-relative offset */
793a34b4168SMatthew Dillon 	vma.vm_end = vma.vm_start + size;
794a34b4168SMatthew Dillon 	vma.vm_pgoff = vma.vm_start >> PAGE_SHIFT;
795a34b4168SMatthew Dillon 	/* vma.vm_page_prot */
796a34b4168SMatthew Dillon 	/* vma.vm_flags */
7975718399fSFrançois Tigeot 
7985718399fSFrançois Tigeot 	/*
799a34b4168SMatthew Dillon 	 * Call the linux-ported code to do the work, and on success just
800a34b4168SMatthew Dillon 	 * setup our own VM object and ignore what the linux code did other
801a34b4168SMatthew Dillon 	 * then supplying us the 'bo'.
8025718399fSFrançois Tigeot 	 */
803932d855eSSergey Zigachev 	ret = ttm_bo_mmap(fp, &vma, bdev);
804a34b4168SMatthew Dillon 
805a34b4168SMatthew Dillon 	if (ret == 0) {
806a34b4168SMatthew Dillon 		bo = vma.vm_private_data;
807a34b4168SMatthew Dillon 		vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE,
808a34b4168SMatthew Dillon 					     &ttm_pager_ops,
809a34b4168SMatthew Dillon 					     size, nprot, 0,
810a34b4168SMatthew Dillon 					     curthread->td_ucred);
811a34b4168SMatthew Dillon 		if (vm_obj) {
8125718399fSFrançois Tigeot 			*obj_res = vm_obj;
813a34b4168SMatthew Dillon 			*offset = 0;		/* object-relative offset */
814a34b4168SMatthew Dillon 		} else {
8155718399fSFrançois Tigeot 			ttm_bo_unref(&bo);
816a34b4168SMatthew Dillon 			ret = EINVAL;
817a34b4168SMatthew Dillon 		}
818a34b4168SMatthew Dillon 	}
8195718399fSFrançois Tigeot 	return ret;
8205718399fSFrançois Tigeot }
821a34b4168SMatthew Dillon EXPORT_SYMBOL(ttm_bo_mmap_single);
8225718399fSFrançois Tigeot 
82343e748b9SFrançois Tigeot #ifdef __DragonFly__
82443e748b9SFrançois Tigeot void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
82543e748b9SFrançois Tigeot 
8266f486c69SFrançois Tigeot void
ttm_bo_release_mmap(struct ttm_buffer_object * bo)8276f486c69SFrançois Tigeot ttm_bo_release_mmap(struct ttm_buffer_object *bo)
8286f486c69SFrançois Tigeot {
8296f486c69SFrançois Tigeot 	vm_object_t vm_obj;
8306f486c69SFrançois Tigeot 
8316f486c69SFrançois Tigeot 	vm_obj = cdev_pager_lookup(bo);
8326f486c69SFrançois Tigeot 	if (vm_obj == NULL)
8336f486c69SFrançois Tigeot 		return;
8346f486c69SFrançois Tigeot 
83504b45e6fSzrj 	VM_OBJECT_LOCK(vm_obj);
8363f2dd94aSFrançois Tigeot #if 1
8374bd76d06SMatthew Dillon 	vm_object_page_remove(vm_obj, 0, 0, false);
8383f2dd94aSFrançois Tigeot #else
8393f2dd94aSFrançois Tigeot 	/*
8403f2dd94aSFrançois Tigeot 	 * XXX REMOVED
8413f2dd94aSFrançois Tigeot 	 *
8423f2dd94aSFrançois Tigeot 	 * We no longer manage the vm pages inside the MGTDEVICE
8433f2dd94aSFrançois Tigeot 	 * objects.
8443f2dd94aSFrançois Tigeot 	 */
8453f2dd94aSFrançois Tigeot 	vm_page_t m;
8463f2dd94aSFrançois Tigeot 	int i;
8473f2dd94aSFrançois Tigeot 
8483f2dd94aSFrançois Tigeot 	for (i = 0; i < bo->num_pages; i++) {
8493f2dd94aSFrançois Tigeot 		m = vm_page_lookup_busy_wait(vm_obj, i, TRUE, "ttm_unm");
8503f2dd94aSFrançois Tigeot 		if (m == NULL)
8513f2dd94aSFrançois Tigeot 			continue;
8523f2dd94aSFrançois Tigeot 		cdev_pager_free_page(vm_obj, m);
8533f2dd94aSFrançois Tigeot 	}
8543f2dd94aSFrançois Tigeot #endif
85504b45e6fSzrj 	VM_OBJECT_UNLOCK(vm_obj);
8566f486c69SFrançois Tigeot 
8576f486c69SFrançois Tigeot 	vm_object_deallocate(vm_obj);
8586f486c69SFrançois Tigeot }
85943e748b9SFrançois Tigeot #endif
8606f486c69SFrançois Tigeot 
8615718399fSFrançois Tigeot #if 0
8625718399fSFrançois Tigeot int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
8635718399fSFrançois Tigeot {
8645718399fSFrançois Tigeot 	if (vma->vm_pgoff != 0)
8655718399fSFrançois Tigeot 		return -EACCES;
8665718399fSFrançois Tigeot 
8675718399fSFrançois Tigeot 	vma->vm_ops = &ttm_bo_vm_ops;
8685718399fSFrançois Tigeot 	vma->vm_private_data = ttm_bo_reference(bo);
8695718399fSFrançois Tigeot 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
8705718399fSFrançois Tigeot 	return 0;
8715718399fSFrançois Tigeot }
872c66857ebSFrançois Tigeot EXPORT_SYMBOL(ttm_fbdev_mmap);
8735718399fSFrançois Tigeot #endif
874