Home | History | Annotate | Line # | Download | only in drm
drm_memory.c revision 1.14
      1 /*	$NetBSD: drm_memory.c,v 1.14 2021/12/19 09:52:00 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_memory.c,v 1.14 2021/12/19 09:52:00 riastradh Exp $");
     34 
     35 #if defined(__i386__) || defined(__x86_64__)
     36 
     37 # ifdef _KERNEL_OPT
     38 #  include "agp.h"
     39 #  if NAGP > 0
     40 #   include "agp_i810.h"
     41 #  else
     42 #   define NAGP_I810	0
     43 #  endif
     44 #  include "genfb.h"
     45 # else
     46 #  define NAGP_I810	1
     47 #  define NGENFB	0
     48 # endif
     49 
     50 #else
     51 
     52 # ifdef _KERNEL_OPT
     53 #  define NAGP_I810	0
     54 #  include "genfb.h"
     55 # else
     56 #  define NAGP_I810	0
     57 #  define NGENFB	0
     58 # endif
     59 
     60 #endif
     61 
     62 #include <sys/bus.h>
     63 
     64 #if NAGP_I810 > 0
     65 /* XXX include order botch -- shouldn't need to include pcivar.h */
     66 #include <dev/pci/pcivar.h>
     67 #include <dev/pci/agpvar.h>
     68 #endif
     69 
     70 #if NGENFB > 0
     71 #include <dev/wsfb/genfbvar.h>
     72 #endif
     73 
     74 #include <drm/drm_drv.h>
     75 #include <drm/drm_legacy.h>
     76 #include <drm/drm_pci.h>
     77 #include <drm/drmP.h>
     78 
     79 /*
     80  * XXX drm_bus_borrow is a horrible kludge!
     81  */
     82 static bool
     83 drm_bus_borrow(bus_addr_t base, bus_size_t size, bus_space_handle_t *handlep)
     84 {
     85 
     86 #if NAGP_I810 > 0
     87 	if (agp_i810_borrow(base, size, handlep))
     88 		return true;
     89 #endif
     90 
     91 #if NGENFB > 0
     92 	if (genfb_borrow(base, handlep))
     93 		return true;
     94 #endif
     95 
     96 	return false;
     97 }
     98 
     99 void
    100 drm_legacy_ioremap(struct drm_local_map *map, struct drm_device *dev)
    101 {
    102 	const bus_space_tag_t bst = dev->bst;
    103 	unsigned int unit;
    104 
    105 	/*
    106 	 * Search dev's bus maps for a match.
    107 	 */
    108 	for (unit = 0; unit < dev->bus_nmaps; unit++) {
    109 		struct drm_bus_map *const bm = &dev->bus_maps[unit];
    110 		int flags = bm->bm_flags;
    111 
    112 		/* Reject maps starting after the request.  */
    113 		if (map->offset < bm->bm_base)
    114 			continue;
    115 
    116 		/* Reject maps smaller than the request.  */
    117 		if (bm->bm_size < map->size)
    118 			continue;
    119 
    120 		/* Reject maps that the request doesn't fit in.  */
    121 		if ((bm->bm_size - map->size) <
    122 		    (map->offset - bm->bm_base))
    123 			continue;
    124 
    125 		/* Ensure we can map the space into virtual memory.  */
    126 		if (!ISSET(flags, BUS_SPACE_MAP_LINEAR))
    127 			continue;
    128 
    129 		/* Reflect requested flags in the bus_space map.  */
    130 		if (ISSET(map->flags, _DRM_WRITE_COMBINING))
    131 			flags |= BUS_SPACE_MAP_PREFETCHABLE;
    132 
    133 		/* Map it.  */
    134 		if (bus_space_map(bst, map->offset, map->size, flags,
    135 			&map->lm_data.bus_space.bsh))
    136 			break;
    137 
    138 		map->lm_data.bus_space.bus_map = bm;
    139 		goto win;
    140 	}
    141 
    142 	/* Couldn't map it.  Try borrowing from someone else.  */
    143 	if (drm_bus_borrow(map->offset, map->size,
    144 		&map->lm_data.bus_space.bsh)) {
    145 		map->lm_data.bus_space.bus_map = NULL;
    146 		goto win;
    147 	}
    148 
    149 	/* Failure!  */
    150 	return;
    151 
    152 win:	map->lm_data.bus_space.bst = bst;
    153 	map->handle = bus_space_vaddr(bst, map->lm_data.bus_space.bsh);
    154 }
    155 
    156 void
    157 drm_legacy_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
    158 {
    159 	if (map->lm_data.bus_space.bus_map != NULL) {
    160 		bus_space_unmap(map->lm_data.bus_space.bst,
    161 		    map->lm_data.bus_space.bsh, map->size);
    162 		map->lm_data.bus_space.bus_map = NULL;
    163 		map->handle = NULL;
    164 	}
    165 }
    166 
    167 /*
    168  * Allocate a drm dma handle, allocate memory fit for DMA, and map it.
    169  *
    170  * XXX This is called drm_pci_alloc for hysterical raisins; it is not
    171  * specific to PCI.
    172  *
    173  * XXX For now, we use non-blocking allocations because this is called
    174  * by ioctls with the drm global mutex held.
    175  *
    176  * XXX Error information is lost because this returns NULL on failure,
    177  * not even an error embedded in a pointer.
    178  */
    179 struct drm_dma_handle *
    180 drm_pci_alloc(struct drm_device *dev, size_t size, size_t align)
    181 {
    182 	int nsegs;
    183 	int error;
    184 
    185 	/*
    186 	 * Allocate a drm_dma_handle record.
    187 	 */
    188 	struct drm_dma_handle *const dmah = kmem_alloc(sizeof(*dmah),
    189 	    KM_NOSLEEP);
    190 	if (dmah == NULL) {
    191 		error = -ENOMEM;
    192 		goto out;
    193 	}
    194 	dmah->dmah_tag = dev->dmat;
    195 
    196 	/*
    197 	 * Allocate the requested amount of DMA-safe memory.
    198 	 */
    199 	/* XXX errno NetBSD->Linux */
    200 	error = -bus_dmamem_alloc(dmah->dmah_tag, size, align, 0,
    201 	    &dmah->dmah_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    202 	if (error)
    203 		goto fail0;
    204 	KASSERT(nsegs == 1);
    205 
    206 	/*
    207 	 * Map the DMA-safe memory into kernel virtual address space.
    208 	 */
    209 	/* XXX errno NetBSD->Linux */
    210 	error = -bus_dmamem_map(dmah->dmah_tag, &dmah->dmah_seg, 1, size,
    211 	    &dmah->vaddr,
    212 	    (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
    213 	if (error)
    214 		goto fail1;
    215 	dmah->size = size;
    216 
    217 	/*
    218 	 * Create a map for DMA transfers.
    219 	 */
    220 	/* XXX errno NetBSD->Linux */
    221 	error = -bus_dmamap_create(dmah->dmah_tag, size, 1, size, 0,
    222 	    BUS_DMA_NOWAIT, &dmah->dmah_map);
    223 	if (error)
    224 		goto fail2;
    225 
    226 	/*
    227 	 * Load the kva buffer into the map for DMA transfers.
    228 	 */
    229 	/* XXX errno NetBSD->Linux */
    230 	error = -bus_dmamap_load(dmah->dmah_tag, dmah->dmah_map, dmah->vaddr,
    231 	    size, NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
    232 	if (error)
    233 		goto fail3;
    234 
    235 	/* Record the bus address for convenient reference.  */
    236 	dmah->busaddr = dmah->dmah_map->dm_segs[0].ds_addr;
    237 
    238 	/* Zero the DMA buffer.  XXX Yikes!  Is this necessary?  */
    239 	memset(dmah->vaddr, 0, size);
    240 
    241 	/* Success!  */
    242 	return dmah;
    243 
    244 fail3:	bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
    245 fail2:	bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
    246 fail1:	bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
    247 fail0:	dmah->dmah_tag = NULL;	/* XXX paranoia */
    248 	kmem_free(dmah, sizeof(*dmah));
    249 out:	DRM_DEBUG("drm_pci_alloc failed: %d\n", error);
    250 	return NULL;
    251 }
    252 
    253 /*
    254  * Release the bus DMA mappings and memory in dmah, and deallocate it.
    255  */
    256 void
    257 drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah)
    258 {
    259 
    260 	bus_dmamap_unload(dmah->dmah_tag, dmah->dmah_map);
    261 	bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
    262 	bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
    263 	bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
    264 	dmah->dmah_tag = NULL;	/* XXX paranoia */
    265 	kmem_free(dmah, sizeof(*dmah));
    266 }
    267 
    268 /*
    269  * Make sure the DMA-safe memory allocated for dev lies between
    270  * min_addr and max_addr.  Can be used multiple times to restrict the
    271  * bounds further, but never to expand the bounds again.
    272  *
    273  * XXX Caller must guarantee nobody has used the tag yet,
    274  * i.e. allocated any DMA memory.
    275  */
    276 int
    277 drm_limit_dma_space(struct drm_device *dev, resource_size_t min_addr,
    278     resource_size_t max_addr)
    279 {
    280 	int ret;
    281 
    282 	KASSERT(min_addr <= max_addr);
    283 
    284 	/*
    285 	 * Limit it further if we have already limited it, and destroy
    286 	 * the old subregion DMA tag.
    287 	 */
    288 	if (dev->dmat_subregion_p) {
    289 		min_addr = MAX(min_addr, dev->dmat_subregion_min);
    290 		max_addr = MIN(max_addr, dev->dmat_subregion_max);
    291 		bus_dmatag_destroy(dev->dmat);
    292 	}
    293 
    294 	/*
    295 	 * If our limit contains the 32-bit space but for some reason
    296 	 * we can't use a subregion, either because the bus doesn't
    297 	 * support >32-bit DMA or because bus_dma(9) on this platform
    298 	 * lacks bus_dmatag_subregion, just use the 32-bit space.
    299 	 */
    300 	if (min_addr == 0 && max_addr >= UINT32_C(0xffffffff) &&
    301 	    dev->bus_dmat == dev->bus_dmat32) {
    302 dma32:		dev->dmat = dev->bus_dmat32;
    303 		dev->dmat_subregion_p = false;
    304 		dev->dmat_subregion_min = 0;
    305 		dev->dmat_subregion_max = UINT32_C(0xffffffff);
    306 		return 0;
    307 	}
    308 
    309 	/*
    310 	 * Create a DMA tag for a subregion from the bus's DMA tag.  If
    311 	 * that fails, restore dev->dmat to the whole region so that we
    312 	 * need not worry about dev->dmat being uninitialized (not that
    313 	 * the caller should try to allocate DMA-safe memory on failure
    314 	 * anyway, but...paranoia).
    315 	 */
    316 	/* XXX errno NetBSD->Linux */
    317 	ret = -bus_dmatag_subregion(dev->bus_dmat, min_addr, max_addr,
    318 	    &dev->dmat, BUS_DMA_WAITOK);
    319 	if (ret) {
    320 		/*
    321 		 * bus_dmatag_subregion may fail.  If so, and if the
    322 		 * subregion contains the 32-bit space, just use the
    323 		 * 32-bit DMA tag.
    324 		 */
    325 		if (ret == -EOPNOTSUPP && dev->bus_dmat32 &&
    326 		    min_addr == 0 && max_addr >= UINT32_C(0xffffffff))
    327 			goto dma32;
    328 		/* XXX Back out?  */
    329 		dev->dmat = dev->bus_dmat;
    330 		dev->dmat_subregion_p = false;
    331 		dev->dmat_subregion_min = 0;
    332 		dev->dmat_subregion_max = __type_max(bus_addr_t);
    333 		return ret;
    334 	}
    335 
    336 	/*
    337 	 * Remember that we have a subregion tag so that we know to
    338 	 * destroy it later, and record the bounds in case we need to
    339 	 * limit them again.
    340 	 */
    341 	dev->dmat_subregion_p = true;
    342 	dev->dmat_subregion_min = min_addr;
    343 	dev->dmat_subregion_max = max_addr;
    344 
    345 	/* Success!  */
    346 	return 0;
    347 }
    348