Home | History | Annotate | Line # | Download | only in drm
drm_vm.c revision 1.1.2.3
      1 /*	$NetBSD: drm_vm.c,v 1.1.2.3 2014/01/22 14:58:47 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.1.2.3 2014/01/22 14:58:47 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 
     37 #include <uvm/uvm_extern.h>
     38 
     39 #include <drm/drmP.h>
     40 
     41 static paddr_t	drm_mmap_paddr_locked(struct drm_device *, off_t, int);
     42 static paddr_t	drm_mmap_dma_paddr(struct drm_device *, off_t, int);
     43 static paddr_t	drm_mmap_map_paddr(struct drm_device *, struct drm_local_map *,
     44 		    off_t, int);
     45 
     46 paddr_t
     47 drm_mmap_paddr(struct drm_device *dev, off_t byte_offset, int prot)
     48 {
     49 	paddr_t paddr;
     50 
     51 	mutex_lock(&dev->struct_mutex);
     52 	paddr = drm_mmap_paddr_locked(dev, byte_offset, prot);
     53 	mutex_unlock(&dev->struct_mutex);
     54 
     55 	return paddr;
     56 }
     57 
     58 static paddr_t
     59 drm_mmap_paddr_locked(struct drm_device *dev, off_t byte_offset, int prot)
     60 {
     61 	const off_t page_offset = (byte_offset >> PAGE_SHIFT);
     62 	struct drm_hash_item *hash;
     63 
     64 	KASSERT(mutex_is_locked(&dev->struct_mutex));
     65 
     66 	if (byte_offset != (byte_offset & ~(PAGE_SIZE-1)))
     67 		return -1;
     68 
     69 	if ((dev->dma != NULL) &&
     70 	    (0 <= byte_offset) &&
     71 	    (byte_offset <= (dev->dma->page_count << PAGE_SHIFT)))
     72 		return drm_mmap_dma_paddr(dev, byte_offset, prot);
     73 
     74 	if (drm_ht_find_item(&dev->map_hash, page_offset, &hash))
     75 		return -1;
     76 
     77 	struct drm_local_map *const map = drm_hash_entry(hash,
     78 	    struct drm_map_list, hash)->map;
     79 	if (map == NULL)
     80 		return -1;
     81 
     82 	/*
     83 	 * XXX FreeBSD drops the mutex at this point, which would be
     84 	 * nice, to allow sleeping in bus_dma cruft, but I don't know
     85 	 * what guarantees the map will continue to exist.
     86 	 */
     87 
     88 	if (ISSET(map->flags, _DRM_RESTRICTED) && !DRM_SUSER())
     89 		return -1;
     90 
     91 	if (byte_offset < map->offset)
     92 		return -1;
     93 
     94 	return drm_mmap_map_paddr(dev, map, (map->offset - byte_offset),
     95 	    prot);
     96 }
     97 
     98 static paddr_t
     99 drm_mmap_dma_paddr(struct drm_device *dev, off_t byte_offset, int prot)
    100 {
    101 	const off_t page_offset = (byte_offset >> PAGE_SHIFT);
    102 
    103 	KASSERT(mutex_is_locked(&dev->struct_mutex));
    104 
    105 	if (dev->dma->pagelist == NULL)
    106 		return (paddr_t)-1;
    107 
    108 	if (page_offset >= dev->dma->page_count)
    109 		return (paddr_t)-1;
    110 
    111 	return dev->dma->pagelist[page_offset];
    112 }
    113 
    114 static paddr_t
    115 drm_mmap_map_paddr(struct drm_device *dev, struct drm_local_map *map,
    116     off_t byte_offset, int prot)
    117 {
    118 	int flags = 0;
    119 
    120 	switch (map->type) {
    121 	case _DRM_FRAME_BUFFER:
    122 	case _DRM_AGP:
    123 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    124 		/* Fall through.  */
    125 
    126 	case _DRM_REGISTERS:
    127 		flags |= BUS_SPACE_MAP_LINEAR; /* XXX Why?  */
    128 
    129 		return bus_space_mmap(dev->bst, map->offset, byte_offset, prot,
    130 		    flags);
    131 
    132 	case _DRM_CONSISTENT: {
    133 		struct drm_dma_handle *const dmah = map->lm_data.dmah;
    134 
    135 		return bus_dmamem_mmap(dev->dmat, &dmah->dmah_seg, 1,
    136 		    byte_offset, prot,
    137 		    /* XXX BUS_DMA_WAITOK?  We're holding a mutex...  */
    138 		    /* XXX What else?  BUS_DMA_COHERENT?  */
    139 		    (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
    140 	}
    141 
    142 	case _DRM_SCATTER_GATHER: {
    143 		struct drm_sg_mem *const sg = dev->sg;
    144 
    145 #if 0				/* XXX */
    146 		KASSERT(sg == map->lm_data.sg);
    147 #endif
    148 
    149 		return bus_dmamem_mmap(dev->dmat, sg->sg_segs, sg->sg_nsegs,
    150 		    byte_offset, prot,
    151 		    /* XXX BUS_DMA_WAITOK?  We're holding a mutex...  */
    152 		    /* XXX What else?  BUS_DMA_COHERENT?  */
    153 		    (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
    154 	}
    155 
    156 	case _DRM_SHM:
    157 	case _DRM_GEM:
    158 	default:
    159 		return (paddr_t)-1;
    160 	}
    161 }
    162