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