Home | History | Annotate | Line # | Download | only in drm
drm_vm.c revision 1.5.4.3
      1  1.5.4.2       tls /*	$NetBSD: drm_vm.c,v 1.5.4.3 2017/12/03 11:37:58 jdolecek Exp $	*/
      2  1.5.4.2       tls 
      3  1.5.4.2       tls /*-
      4  1.5.4.2       tls  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.5.4.2       tls  * All rights reserved.
      6  1.5.4.2       tls  *
      7  1.5.4.2       tls  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5.4.2       tls  * by Taylor R. Campbell.
      9  1.5.4.2       tls  *
     10  1.5.4.2       tls  * Redistribution and use in source and binary forms, with or without
     11  1.5.4.2       tls  * modification, are permitted provided that the following conditions
     12  1.5.4.2       tls  * are met:
     13  1.5.4.2       tls  * 1. Redistributions of source code must retain the above copyright
     14  1.5.4.2       tls  *    notice, this list of conditions and the following disclaimer.
     15  1.5.4.2       tls  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5.4.2       tls  *    notice, this list of conditions and the following disclaimer in the
     17  1.5.4.2       tls  *    documentation and/or other materials provided with the distribution.
     18  1.5.4.2       tls  *
     19  1.5.4.2       tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.5.4.2       tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.5.4.2       tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.5.4.2       tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.5.4.2       tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.5.4.2       tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.5.4.2       tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.5.4.2       tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.5.4.2       tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.5.4.2       tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.5.4.2       tls  * POSSIBILITY OF SUCH DAMAGE.
     30  1.5.4.2       tls  */
     31  1.5.4.2       tls 
     32  1.5.4.2       tls #include <sys/cdefs.h>
     33  1.5.4.2       tls __KERNEL_RCSID(0, "$NetBSD: drm_vm.c,v 1.5.4.3 2017/12/03 11:37:58 jdolecek Exp $");
     34  1.5.4.2       tls 
     35  1.5.4.2       tls #include <sys/types.h>
     36  1.5.4.2       tls #include <sys/conf.h>
     37  1.5.4.2       tls 
     38  1.5.4.2       tls #include <uvm/uvm_extern.h>
     39  1.5.4.2       tls #include <uvm/uvm_device.h>
     40  1.5.4.2       tls 
     41  1.5.4.2       tls #include <drm/drmP.h>
     42  1.5.4.2       tls 
     43  1.5.4.2       tls static paddr_t	drm_mmap_paddr_locked(struct drm_device *, off_t, int);
     44  1.5.4.2       tls static paddr_t	drm_mmap_dma_paddr(struct drm_device *, off_t, int);
     45  1.5.4.2       tls static paddr_t	drm_mmap_map_paddr(struct drm_device *, struct drm_local_map *,
     46  1.5.4.2       tls 		    off_t, int);
     47  1.5.4.2       tls 
     48  1.5.4.2       tls int
     49  1.5.4.2       tls drm_mmap_object(struct drm_device *dev, off_t offset, size_t size, int prot,
     50  1.5.4.2       tls     struct uvm_object **uobjp, voff_t *uoffsetp, struct file *file __unused)
     51  1.5.4.2       tls {
     52  1.5.4.3  jdolecek 	devmajor_t maj = cdevsw_lookup_major(&drm_cdevsw);
     53  1.5.4.3  jdolecek 	dev_t devno = makedev(maj, dev->primary->index);
     54  1.5.4.2       tls 	struct uvm_object *uobj;
     55  1.5.4.2       tls 
     56  1.5.4.2       tls 	KASSERT(offset == (offset & ~(PAGE_SIZE-1)));
     57  1.5.4.2       tls 
     58  1.5.4.2       tls 	/*
     59  1.5.4.2       tls 	 * Attach the device.  The size and offset are used only for
     60  1.5.4.2       tls 	 * access checks; offset does not become a base address for the
     61  1.5.4.2       tls 	 * subsequent uvm_map, hence we set *uoffsetp to offset, not 0.
     62  1.5.4.2       tls 	 */
     63  1.5.4.3  jdolecek 	uobj = udv_attach(devno, prot, offset, size);
     64  1.5.4.2       tls 	if (uobj == NULL)
     65  1.5.4.2       tls 		return -EINVAL;
     66  1.5.4.2       tls 
     67  1.5.4.2       tls 	*uobjp = uobj;
     68  1.5.4.2       tls 	*uoffsetp = offset;
     69  1.5.4.2       tls 	return 0;
     70  1.5.4.2       tls }
     71  1.5.4.2       tls 
     72  1.5.4.2       tls paddr_t
     73  1.5.4.2       tls drm_mmap_paddr(struct drm_device *dev, off_t byte_offset, int prot)
     74  1.5.4.2       tls {
     75  1.5.4.2       tls 	paddr_t paddr;
     76  1.5.4.2       tls 
     77  1.5.4.2       tls 	if (byte_offset != (byte_offset & ~(PAGE_SIZE-1)))
     78  1.5.4.2       tls 		return -1;
     79  1.5.4.2       tls 
     80  1.5.4.2       tls 	mutex_lock(&dev->struct_mutex);
     81  1.5.4.2       tls 	paddr = drm_mmap_paddr_locked(dev, byte_offset, prot);
     82  1.5.4.2       tls 	mutex_unlock(&dev->struct_mutex);
     83  1.5.4.2       tls 
     84  1.5.4.2       tls 	return paddr;
     85  1.5.4.2       tls }
     86  1.5.4.2       tls 
     87  1.5.4.2       tls static paddr_t
     88  1.5.4.2       tls drm_mmap_paddr_locked(struct drm_device *dev, off_t byte_offset, int prot)
     89  1.5.4.2       tls {
     90  1.5.4.2       tls 	const off_t page_offset = (byte_offset >> PAGE_SHIFT);
     91  1.5.4.2       tls 	struct drm_hash_item *hash;
     92  1.5.4.2       tls 
     93  1.5.4.2       tls 	KASSERT(mutex_is_locked(&dev->struct_mutex));
     94  1.5.4.2       tls 	KASSERT(byte_offset == (byte_offset & ~(PAGE_SIZE-1)));
     95  1.5.4.2       tls 
     96  1.5.4.2       tls 	if ((dev->dma != NULL) &&
     97  1.5.4.2       tls 	    (0 <= byte_offset) &&
     98  1.5.4.2       tls 	    (page_offset <= dev->dma->page_count))
     99  1.5.4.2       tls 		return drm_mmap_dma_paddr(dev, byte_offset, prot);
    100  1.5.4.2       tls 
    101  1.5.4.2       tls 	if (drm_ht_find_item(&dev->map_hash, page_offset, &hash))
    102  1.5.4.2       tls 		return -1;
    103  1.5.4.2       tls 
    104  1.5.4.2       tls 	struct drm_local_map *const map = drm_hash_entry(hash,
    105  1.5.4.2       tls 	    struct drm_map_list, hash)->map;
    106  1.5.4.2       tls 	if (map == NULL)
    107  1.5.4.2       tls 		return -1;
    108  1.5.4.2       tls 
    109  1.5.4.2       tls 	/*
    110  1.5.4.2       tls 	 * XXX FreeBSD drops the mutex at this point, which would be
    111  1.5.4.2       tls 	 * nice, to allow sleeping in bus_dma cruft, but I don't know
    112  1.5.4.2       tls 	 * what guarantees the map will continue to exist.
    113  1.5.4.2       tls 	 */
    114  1.5.4.2       tls 
    115  1.5.4.2       tls 	if (ISSET(map->flags, _DRM_RESTRICTED) && !DRM_SUSER())
    116  1.5.4.2       tls 		return -1;
    117  1.5.4.2       tls 
    118  1.5.4.2       tls 	if (!(map->offset <= byte_offset))
    119  1.5.4.2       tls 		return -1;
    120  1.5.4.2       tls 	if (map->size < (map->offset - byte_offset))
    121  1.5.4.2       tls 		return -1;
    122  1.5.4.2       tls 
    123  1.5.4.2       tls 	return drm_mmap_map_paddr(dev, map, (byte_offset - map->offset), prot);
    124  1.5.4.2       tls }
    125  1.5.4.2       tls 
    126  1.5.4.2       tls static paddr_t
    127  1.5.4.2       tls drm_mmap_dma_paddr(struct drm_device *dev, off_t byte_offset, int prot)
    128  1.5.4.2       tls {
    129  1.5.4.2       tls 	const off_t page_offset = (byte_offset >> PAGE_SHIFT);
    130  1.5.4.2       tls 
    131  1.5.4.2       tls 	KASSERT(mutex_is_locked(&dev->struct_mutex));
    132  1.5.4.2       tls 	KASSERT(byte_offset == (byte_offset & ~(PAGE_SIZE-1)));
    133  1.5.4.2       tls 	KASSERT(page_offset <= dev->dma->page_count);
    134  1.5.4.2       tls 
    135  1.5.4.2       tls 	if (dev->dma->pagelist == NULL)
    136  1.5.4.2       tls 		return (paddr_t)-1;
    137  1.5.4.2       tls 
    138  1.5.4.2       tls 	return dev->dma->pagelist[page_offset];
    139  1.5.4.2       tls }
    140  1.5.4.2       tls 
    141  1.5.4.2       tls static paddr_t
    142  1.5.4.2       tls drm_mmap_map_paddr(struct drm_device *dev, struct drm_local_map *map,
    143  1.5.4.2       tls     off_t byte_offset, int prot)
    144  1.5.4.2       tls {
    145  1.5.4.2       tls 	int flags = 0;
    146  1.5.4.2       tls 
    147  1.5.4.2       tls 	KASSERT(byte_offset <= map->size);
    148  1.5.4.2       tls 
    149  1.5.4.2       tls 	switch (map->type) {
    150  1.5.4.2       tls 	case _DRM_FRAME_BUFFER:
    151  1.5.4.2       tls 	case _DRM_AGP:
    152  1.5.4.2       tls 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    153  1.5.4.2       tls 		/* Fall through.  */
    154  1.5.4.2       tls 
    155  1.5.4.2       tls 	case _DRM_REGISTERS:
    156  1.5.4.2       tls 		flags |= BUS_SPACE_MAP_LINEAR; /* XXX Why?  */
    157  1.5.4.2       tls 
    158  1.5.4.2       tls 		return bus_space_mmap(map->lm_data.bus_space.bst, map->offset,
    159  1.5.4.2       tls 		    byte_offset, prot, flags);
    160  1.5.4.2       tls 
    161  1.5.4.2       tls 	case _DRM_CONSISTENT: {
    162  1.5.4.2       tls 		struct drm_dma_handle *const dmah = map->lm_data.dmah;
    163  1.5.4.2       tls 
    164  1.5.4.2       tls 		return bus_dmamem_mmap(dev->dmat, &dmah->dmah_seg, 1,
    165  1.5.4.2       tls 		    byte_offset, prot,
    166  1.5.4.2       tls 		    /* XXX BUS_DMA_WAITOK?  We're holding a mutex...  */
    167  1.5.4.2       tls 		    /* XXX What else?  BUS_DMA_COHERENT?  */
    168  1.5.4.2       tls 		    (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
    169  1.5.4.2       tls 	}
    170  1.5.4.2       tls 
    171  1.5.4.2       tls 	case _DRM_SCATTER_GATHER: {
    172  1.5.4.2       tls 		struct drm_sg_mem *const sg = dev->sg;
    173  1.5.4.2       tls 
    174  1.5.4.2       tls #if 0				/* XXX */
    175  1.5.4.2       tls 		KASSERT(sg == map->lm_data.sg);
    176  1.5.4.2       tls #endif
    177  1.5.4.2       tls 
    178  1.5.4.2       tls 		return bus_dmamem_mmap(dev->dmat, sg->sg_segs, sg->sg_nsegs,
    179  1.5.4.2       tls 		    byte_offset, prot,
    180  1.5.4.2       tls 		    /* XXX BUS_DMA_WAITOK?  We're holding a mutex...  */
    181  1.5.4.2       tls 		    /* XXX What else?  BUS_DMA_COHERENT?  */
    182  1.5.4.2       tls 		    (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
    183  1.5.4.2       tls 	}
    184  1.5.4.2       tls 
    185  1.5.4.2       tls 	case _DRM_SHM:
    186  1.5.4.2       tls 	default:
    187  1.5.4.2       tls 		return (paddr_t)-1;
    188  1.5.4.2       tls 	}
    189  1.5.4.2       tls }
    190