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