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