1 /* $NetBSD: drm_vm.c,v 1.3 2014/05/01 15:19:16 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 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: drm_vm.c,v 1.3 2014/05/01 15:19:16 riastradh Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/conf.h> 37 38 #include <uvm/uvm_extern.h> 39 #include <uvm/uvm_device.h> 40 41 #include <drm/drmP.h> 42 43 static paddr_t drm_mmap_paddr_locked(struct drm_device *, off_t, int); 44 static paddr_t drm_mmap_dma_paddr(struct drm_device *, off_t, int); 45 static paddr_t drm_mmap_map_paddr(struct drm_device *, struct drm_local_map *, 46 off_t, int); 47 48 int 49 drm_mmap_object(struct drm_device *dev, off_t offset, size_t size, int prot, 50 struct uvm_object **uobjp, voff_t *uoffsetp) 51 { 52 dev_t devno = cdevsw_lookup_major(&drm_cdevsw); 53 struct uvm_object *uobj; 54 55 KASSERT(offset == (offset & ~(PAGE_SIZE-1))); 56 57 /* 58 * Attach the device. The size and offset are used only for 59 * access checks; offset does not become a base address for the 60 * subsequent uvm_map, hence we set *uoffsetp to offset, not 0. 61 */ 62 uobj = udv_attach(&devno, prot, offset, size); 63 if (uobj == NULL) 64 return -EINVAL; 65 66 *uobjp = uobj; 67 *uoffsetp = offset; 68 return 0; 69 } 70 71 paddr_t 72 drm_mmap_paddr(struct drm_device *dev, off_t byte_offset, int prot) 73 { 74 paddr_t paddr; 75 76 if (byte_offset != (byte_offset & ~(PAGE_SIZE-1))) 77 return -1; 78 79 mutex_lock(&dev->struct_mutex); 80 paddr = drm_mmap_paddr_locked(dev, byte_offset, prot); 81 mutex_unlock(&dev->struct_mutex); 82 83 return paddr; 84 } 85 86 static paddr_t 87 drm_mmap_paddr_locked(struct drm_device *dev, off_t byte_offset, int prot) 88 { 89 const off_t page_offset = (byte_offset >> PAGE_SHIFT); 90 struct drm_hash_item *hash; 91 92 KASSERT(mutex_is_locked(&dev->struct_mutex)); 93 KASSERT(byte_offset == (byte_offset & ~(PAGE_SIZE-1))); 94 95 if ((dev->dma != NULL) && 96 (0 <= byte_offset) && 97 (page_offset <= dev->dma->page_count)) 98 return drm_mmap_dma_paddr(dev, byte_offset, prot); 99 100 if (drm_ht_find_item(&dev->map_hash, page_offset, &hash)) 101 return -1; 102 103 struct drm_local_map *const map = drm_hash_entry(hash, 104 struct drm_map_list, hash)->map; 105 if (map == NULL) 106 return -1; 107 108 /* 109 * XXX FreeBSD drops the mutex at this point, which would be 110 * nice, to allow sleeping in bus_dma cruft, but I don't know 111 * what guarantees the map will continue to exist. 112 */ 113 114 if (ISSET(map->flags, _DRM_RESTRICTED) && !DRM_SUSER()) 115 return -1; 116 117 if (!(map->offset <= byte_offset)) 118 return -1; 119 if (map->size < (map->offset - byte_offset)) 120 return -1; 121 122 return drm_mmap_map_paddr(dev, map, (byte_offset - map->offset), prot); 123 } 124 125 static paddr_t 126 drm_mmap_dma_paddr(struct drm_device *dev, off_t byte_offset, int prot) 127 { 128 const off_t page_offset = (byte_offset >> PAGE_SHIFT); 129 130 KASSERT(mutex_is_locked(&dev->struct_mutex)); 131 KASSERT(byte_offset == (byte_offset & ~(PAGE_SIZE-1))); 132 KASSERT(page_offset <= dev->dma->page_count); 133 134 if (dev->dma->pagelist == NULL) 135 return (paddr_t)-1; 136 137 return dev->dma->pagelist[page_offset]; 138 } 139 140 static paddr_t 141 drm_mmap_map_paddr(struct drm_device *dev, struct drm_local_map *map, 142 off_t byte_offset, int prot) 143 { 144 int flags = 0; 145 146 KASSERT(byte_offset <= map->size); 147 148 switch (map->type) { 149 case _DRM_FRAME_BUFFER: 150 case _DRM_AGP: 151 flags |= BUS_SPACE_MAP_PREFETCHABLE; 152 /* Fall through. */ 153 154 case _DRM_REGISTERS: 155 flags |= BUS_SPACE_MAP_LINEAR; /* XXX Why? */ 156 157 return bus_space_mmap(dev->bst, map->offset, byte_offset, prot, 158 flags); 159 160 case _DRM_CONSISTENT: { 161 struct drm_dma_handle *const dmah = map->lm_data.dmah; 162 163 return bus_dmamem_mmap(dev->dmat, &dmah->dmah_seg, 1, 164 byte_offset, prot, 165 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */ 166 /* XXX What else? BUS_DMA_COHERENT? */ 167 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE)); 168 } 169 170 case _DRM_SCATTER_GATHER: { 171 struct drm_sg_mem *const sg = dev->sg; 172 173 #if 0 /* XXX */ 174 KASSERT(sg == map->lm_data.sg); 175 #endif 176 177 return bus_dmamem_mmap(dev->dmat, sg->sg_segs, sg->sg_nsegs, 178 byte_offset, prot, 179 /* XXX BUS_DMA_WAITOK? We're holding a mutex... */ 180 /* XXX What else? BUS_DMA_COHERENT? */ 181 (BUS_DMA_WAITOK | BUS_DMA_NOCACHE)); 182 } 183 184 case _DRM_SHM: 185 case _DRM_GEM: 186 default: 187 return (paddr_t)-1; 188 } 189 } 190