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