Home | History | Annotate | Line # | Download | only in ttm
ttm_bo_vm.c revision 1.2.4.1
      1 /*	$NetBSD: ttm_bo_vm.c,v 1.2.4.1 2014/08/18 07:49:10 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ttm_bo_vm.c,v 1.2.4.1 2014/08/18 07:49:10 martin Exp $");
     34 
     35 #include <sys/types.h>
     36 
     37 #include <uvm/uvm.h>
     38 #include <uvm/uvm_extern.h>
     39 #include <uvm/uvm_fault.h>
     40 
     41 #include <linux/bitops.h>
     42 
     43 #include <drm/drm_vma_manager.h>
     44 
     45 #include <ttm/ttm_bo_driver.h>
     46 
     47 static int	ttm_bo_uvm_fault_idle(struct ttm_buffer_object *,
     48 		    struct uvm_faultinfo *, struct uvm_object *);
     49 static int	ttm_bo_uvm_lookup(struct ttm_bo_device *, unsigned long,
     50 		    unsigned long, struct ttm_buffer_object **);
     51 
     52 void
     53 ttm_bo_uvm_reference(struct uvm_object *uobj)
     54 {
     55 	struct ttm_buffer_object *const bo = container_of(uobj,
     56 	    struct ttm_buffer_object, uvmobj);
     57 
     58 	(void)ttm_bo_reference(bo);
     59 }
     60 
     61 void
     62 ttm_bo_uvm_detach(struct uvm_object *uobj)
     63 {
     64 	struct ttm_buffer_object *bo = container_of(uobj,
     65 	    struct ttm_buffer_object, uvmobj);
     66 
     67 	ttm_bo_unref(&bo);
     68 	KASSERT(bo == NULL);
     69 }
     70 
     71 int
     72 ttm_bo_uvm_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr,
     73     struct vm_page **pps, int npages, int centeridx, vm_prot_t access_type,
     74     int flags)
     75 {
     76 	struct uvm_object *const uobj = ufi->entry->object.uvm_obj;
     77 	struct ttm_buffer_object *const bo = container_of(uobj,
     78 	    struct ttm_buffer_object, uvmobj);
     79 	struct ttm_bo_device *const bdev = bo->bdev;
     80 	struct ttm_mem_type_manager *man =
     81 	    &bdev->man[bo->mem.mem_type];
     82 	union {
     83 		bus_addr_t base;
     84 		struct ttm_tt *ttm;
     85 	} u;
     86 	size_t size __diagused;
     87 	voff_t uoffset;		/* offset in bytes into bo */
     88 	unsigned startpage;	/* offset in pages into bo */
     89 	unsigned i;
     90 	vm_prot_t vm_prot;	/* VM_PROT_* */
     91 	pgprot_t pgprot;	/* VM_PROT_* | PMAP_* cacheability flags */
     92 	unsigned mmapflags;
     93 	int ret;
     94 
     95 	/* Copy-on-write mappings make no sense for the graphics aperture.  */
     96 	if (UVM_ET_ISCOPYONWRITE(ufi->entry)) {
     97 		ret = -EIO;
     98 		goto out0;
     99 	}
    100 
    101 	/* Try to lock the buffer.  */
    102 	ret = ttm_bo_reserve(bo, true, true, false, NULL);
    103 	if (ret) {
    104 		if (ret != -EBUSY)
    105 			goto out0;
    106 		/*
    107 		 * It's currently locked.  Unlock the fault, wait for
    108 		 * it, and start over.
    109 		 */
    110 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
    111 		(void)ttm_bo_wait_unreserved(bo);
    112 		return -ERESTART;
    113 	}
    114 
    115 	/* drm prime buffers are not mappable.  XXX Catch this earlier?  */
    116 	if (bo->ttm && ISSET(bo->ttm->page_flags, TTM_PAGE_FLAG_SG)) {
    117 		ret = -EINVAL;
    118 		goto out0;
    119 	}
    120 
    121 	/* Notify the driver of a fault if it wants.  */
    122 	if (bdev->driver->fault_reserve_notify) {
    123 		ret = (*bdev->driver->fault_reserve_notify)(bo);
    124 		if (ret) {
    125 			if (ret == -ERESTART)
    126 				ret = -EIO;
    127 			goto out0;
    128 		}
    129 	}
    130 
    131 	ret = ttm_bo_uvm_fault_idle(bo, ufi, uobj);
    132 	if (ret) {
    133 		/* Unlocks if it restarts.  */
    134 		KASSERT(ret == -ERESTART);
    135 		/* XXX errno Linux->NetBSD */
    136 		return -ret;
    137 	}
    138 
    139 	ret = ttm_mem_io_lock(man, true);
    140 	if (ret) {
    141 		ret = -EIO;
    142 		goto out1;
    143 	}
    144 	ret = ttm_mem_io_reserve_vm(bo);
    145 	if (ret) {
    146 		ret = -EIO;
    147 		goto out2;
    148 	}
    149 
    150 	vm_prot = ufi->entry->protection;
    151 	if (bo->mem.bus.is_iomem) {
    152 		u.base = (bo->mem.bus.base + bo->mem.bus.offset);
    153 		size = bo->mem.bus.size;
    154 		pgprot = ttm_io_prot(bo->mem.placement, vm_prot);
    155 	} else {
    156 		u.ttm = bo->ttm;
    157 		size = (bo->ttm->num_pages << PAGE_SHIFT);
    158 		if (ISSET(bo->mem.placement, TTM_PL_FLAG_CACHED))
    159 			pgprot = vm_prot;
    160 		else
    161 			pgprot = ttm_io_prot(bo->mem.placement, vm_prot);
    162 		if ((*u.ttm->bdev->driver->ttm_tt_populate)(u.ttm)) {
    163 			ret = -ENOMEM;
    164 			goto out2;
    165 		}
    166 	}
    167 
    168 	KASSERT(ufi->entry->start <= vaddr);
    169 	KASSERT((ufi->entry->offset & (PAGE_SIZE - 1)) == 0);
    170 	KASSERT(ufi->entry->offset <= size);
    171 	KASSERT((vaddr - ufi->entry->start) <= (size - ufi->entry->offset));
    172 	KASSERT(npages <= ((size - ufi->entry->offset) -
    173 		(vaddr - ufi->entry->start)));
    174 	uoffset = (ufi->entry->offset + (vaddr - ufi->entry->start));
    175 	startpage = (uoffset >> PAGE_SHIFT);
    176 	for (i = 0; i < npages; i++) {
    177 		paddr_t paddr;
    178 
    179 		/* XXX PGO_ALLPAGES?  */
    180 		if (pps[i] == PGO_DONTCARE)
    181 			continue;
    182 		if (bo->mem.bus.is_iomem) {
    183 			const paddr_t cookie = bus_space_mmap(bdev->memt,
    184 			    u.base, ((startpage + i) << PAGE_SHIFT), vm_prot,
    185 			    0);
    186 
    187 			paddr = pmap_phys_address(cookie);
    188 			mmapflags = pmap_mmap_flags(cookie);
    189 		} else {
    190 			paddr = page_to_phys(u.ttm->pages[startpage + i]);
    191 			mmapflags = 0;
    192 		}
    193 		ret = -pmap_enter(ufi->orig_map->pmap, vaddr + i*PAGE_SIZE,
    194 		    paddr, vm_prot, (PMAP_CANFAIL | pgprot | mmapflags));
    195 		if (ret)
    196 			goto out3;
    197 	}
    198 
    199 out3:	pmap_update(ufi->orig_map->pmap);
    200 out2:	ttm_mem_io_unlock(man);
    201 out1:	ttm_bo_unreserve(bo);
    202 out0:	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
    203 	/* XXX errno Linux->NetBSD */
    204 	return -ret;
    205 }
    206 
    207 static int
    208 ttm_bo_uvm_fault_idle(struct ttm_buffer_object *bo, struct uvm_faultinfo *ufi,
    209     struct uvm_object *uobj)
    210 {
    211 	struct ttm_bo_device *const bdev = bo->bdev;
    212 	int ret = 0;
    213 
    214 	spin_lock(&bdev->fence_lock);
    215 	if (__predict_true(!test_bit(TTM_BO_PRIV_FLAG_MOVING,
    216 		    &bo->priv_flags)))
    217 		goto out;
    218 	if (ttm_bo_wait(bo, false, false, true) == 0)
    219 		goto out;
    220 
    221 	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
    222 	(void)ttm_bo_wait(bo, false, true, false);
    223 	ret = -ERESTART;
    224 
    225 out:	spin_unlock(&bdev->fence_lock);
    226 	return ret;
    227 }
    228 
    229 int
    230 ttm_bo_mmap_object(struct ttm_bo_device *bdev, off_t offset, size_t size,
    231     vm_prot_t prot, struct uvm_object **uobjp, voff_t *uoffsetp,
    232     struct file *file)
    233 {
    234 	const unsigned long startpage = (offset >> PAGE_SHIFT);
    235 	const unsigned long npages = (size >> PAGE_SHIFT);
    236 	struct ttm_buffer_object *bo;
    237 	int ret;
    238 
    239 	KASSERT(0 == (offset & (PAGE_SIZE - 1)));
    240 	KASSERT(0 == (size & (PAGE_SIZE - 1)));
    241 
    242 	ret = ttm_bo_uvm_lookup(bdev, startpage, npages, &bo);
    243 	if (ret)
    244 		goto fail0;
    245 	KASSERT(drm_vma_node_start(&bo->vma_node) <= offset);
    246 	/* XXX Just assert this?  */
    247 	if (__predict_false(bdev->driver->verify_access == NULL)) {
    248 		ret = -EPERM;
    249 		goto fail1;
    250 	}
    251 	ret = (*bdev->driver->verify_access)(bo, file);
    252 	if (ret)
    253 		goto fail1;
    254 
    255 	/* Success!  */
    256 	*uobjp = &bo->uvmobj;
    257 	*uoffsetp = (offset -
    258 	    (drm_vma_node_start(&bo->vma_node) << PAGE_SHIFT));
    259 	return 0;
    260 
    261 fail1:	ttm_bo_unref(&bo);
    262 fail0:	KASSERT(ret);
    263 	return ret;
    264 }
    265 
    266 static int
    267 ttm_bo_uvm_lookup(struct ttm_bo_device *bdev, unsigned long startpage,
    268     unsigned long npages, struct ttm_buffer_object **bop)
    269 {
    270 	struct ttm_buffer_object *bo = NULL;
    271 	struct drm_vma_offset_node *node;
    272 
    273 	drm_vma_offset_lock_lookup(&bdev->vma_manager);
    274 	node = drm_vma_offset_lookup_locked(&bdev->vma_manager, startpage,
    275 	    npages);
    276 	if (node != NULL) {
    277 		bo = container_of(node, struct ttm_buffer_object, vma_node);
    278 		if (!kref_get_unless_zero(&bo->kref))
    279 			bo = NULL;
    280 	}
    281 	drm_vma_offset_unlock_lookup(&bdev->vma_manager);
    282 
    283 	if (bo == NULL)
    284 		return -ENOENT;
    285 
    286 	*bop = bo;
    287 	return 0;
    288 }
    289