xref: /netbsd-src/sys/external/bsd/drm2/drm/drm_memory.c (revision e67b1c183ca65e42dcf3c592ea6e7e2035fa0b04)
1*e67b1c18Sriastradh /*	$NetBSD: drm_memory.c,v 1.17 2021/12/19 10:47:13 riastradh Exp $	*/
26cb10275Sriastradh 
36cb10275Sriastradh /*-
46cb10275Sriastradh  * Copyright (c) 2013 The NetBSD Foundation, Inc.
56cb10275Sriastradh  * All rights reserved.
66cb10275Sriastradh  *
76cb10275Sriastradh  * This code is derived from software contributed to The NetBSD Foundation
86cb10275Sriastradh  * by Taylor R. Campbell.
96cb10275Sriastradh  *
106cb10275Sriastradh  * Redistribution and use in source and binary forms, with or without
116cb10275Sriastradh  * modification, are permitted provided that the following conditions
126cb10275Sriastradh  * are met:
136cb10275Sriastradh  * 1. Redistributions of source code must retain the above copyright
146cb10275Sriastradh  *    notice, this list of conditions and the following disclaimer.
156cb10275Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
166cb10275Sriastradh  *    notice, this list of conditions and the following disclaimer in the
176cb10275Sriastradh  *    documentation and/or other materials provided with the distribution.
186cb10275Sriastradh  *
196cb10275Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206cb10275Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216cb10275Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226cb10275Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236cb10275Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246cb10275Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256cb10275Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266cb10275Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276cb10275Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286cb10275Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296cb10275Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
306cb10275Sriastradh  */
316cb10275Sriastradh 
326cb10275Sriastradh #include <sys/cdefs.h>
33*e67b1c18Sriastradh __KERNEL_RCSID(0, "$NetBSD: drm_memory.c,v 1.17 2021/12/19 10:47:13 riastradh Exp $");
340a23657fSjmcneill 
350a23657fSjmcneill #if defined(__i386__) || defined(__x86_64__)
366cb10275Sriastradh 
376cb10275Sriastradh # ifdef _KERNEL_OPT
38bf0eee82Smlelstv #  include "agp.h"
39d44fe573Smlelstv #  if NAGP > 0
406cb10275Sriastradh #   include "agp_i810.h"
410a23657fSjmcneill #  else
420a23657fSjmcneill #   define NAGP_I810	0
430a23657fSjmcneill #  endif
446cb10275Sriastradh #  include "genfb.h"
456cb10275Sriastradh # else
46d44fe573Smlelstv #  define NAGP_I810	1
47d44fe573Smlelstv #  define NGENFB	0
48d44fe573Smlelstv # endif
49d44fe573Smlelstv 
50d44fe573Smlelstv #else
51d44fe573Smlelstv 
52d44fe573Smlelstv # ifdef _KERNEL_OPT
53d44fe573Smlelstv #  define NAGP_I810	0
54d44fe573Smlelstv #  include "genfb.h"
55d44fe573Smlelstv # else
56d44fe573Smlelstv #  define NAGP_I810	0
57d44fe573Smlelstv #  define NGENFB	0
58d44fe573Smlelstv # endif
59d44fe573Smlelstv 
606cb10275Sriastradh #endif
616cb10275Sriastradh 
626cb10275Sriastradh #include <sys/bus.h>
636cb10275Sriastradh 
646cb10275Sriastradh #if NAGP_I810 > 0
656cb10275Sriastradh /* XXX include order botch -- shouldn't need to include pcivar.h */
666cb10275Sriastradh #include <dev/pci/pcivar.h>
676cb10275Sriastradh #include <dev/pci/agpvar.h>
686cb10275Sriastradh #endif
696cb10275Sriastradh 
706cb10275Sriastradh #if NGENFB > 0
716cb10275Sriastradh #include <dev/wsfb/genfbvar.h>
726cb10275Sriastradh #endif
736cb10275Sriastradh 
74f21b21b0Sriastradh #include <drm/drm_device.h>
758fe3f4a7Sriastradh #include <drm/drm_drv.h>
76*e67b1c18Sriastradh #include <drm/drm_cache.h>
7794c04678Sriastradh #include <drm/drm_legacy.h>
788fe3f4a7Sriastradh #include <drm/drm_pci.h>
799a59cb4fSriastradh #include <drm/drm_print.h>
806cb10275Sriastradh 
816cb10275Sriastradh /*
826cb10275Sriastradh  * XXX drm_bus_borrow is a horrible kludge!
836cb10275Sriastradh  */
846cb10275Sriastradh static bool
drm_bus_borrow(bus_addr_t base,bus_size_t size,bus_space_handle_t * handlep)857df9e83cSriastradh drm_bus_borrow(bus_addr_t base, bus_size_t size, bus_space_handle_t *handlep)
866cb10275Sriastradh {
876cb10275Sriastradh 
886cb10275Sriastradh #if NAGP_I810 > 0
897df9e83cSriastradh 	if (agp_i810_borrow(base, size, handlep))
906cb10275Sriastradh 		return true;
916cb10275Sriastradh #endif
926cb10275Sriastradh 
936cb10275Sriastradh #if NGENFB > 0
946cb10275Sriastradh 	if (genfb_borrow(base, handlep))
956cb10275Sriastradh 		return true;
966cb10275Sriastradh #endif
976cb10275Sriastradh 
986cb10275Sriastradh 	return false;
996cb10275Sriastradh }
1006cb10275Sriastradh 
1019f0b3ec0Sriastradh void
drm_legacy_ioremap(struct drm_local_map * map,struct drm_device * dev)102231b70a7Sriastradh drm_legacy_ioremap(struct drm_local_map *map, struct drm_device *dev)
1036cb10275Sriastradh {
1046cb10275Sriastradh 	const bus_space_tag_t bst = dev->bst;
1056cb10275Sriastradh 	unsigned int unit;
1066cb10275Sriastradh 
1076cb10275Sriastradh 	/*
1086cb10275Sriastradh 	 * Search dev's bus maps for a match.
1096cb10275Sriastradh 	 */
1106cb10275Sriastradh 	for (unit = 0; unit < dev->bus_nmaps; unit++) {
1116cb10275Sriastradh 		struct drm_bus_map *const bm = &dev->bus_maps[unit];
1122ef3036bSriastradh 		int flags = bm->bm_flags;
1136cb10275Sriastradh 
1146cb10275Sriastradh 		/* Reject maps starting after the request.  */
1156cb10275Sriastradh 		if (map->offset < bm->bm_base)
1166cb10275Sriastradh 			continue;
1176cb10275Sriastradh 
1186cb10275Sriastradh 		/* Reject maps smaller than the request.  */
1196cb10275Sriastradh 		if (bm->bm_size < map->size)
1206cb10275Sriastradh 			continue;
1216cb10275Sriastradh 
1226cb10275Sriastradh 		/* Reject maps that the request doesn't fit in.  */
1236cb10275Sriastradh 		if ((bm->bm_size - map->size) <
1246cb10275Sriastradh 		    (map->offset - bm->bm_base))
1256cb10275Sriastradh 			continue;
1266cb10275Sriastradh 
1276cb10275Sriastradh 		/* Ensure we can map the space into virtual memory.  */
1282ef3036bSriastradh 		if (!ISSET(flags, BUS_SPACE_MAP_LINEAR))
1296cb10275Sriastradh 			continue;
1306cb10275Sriastradh 
1312ef3036bSriastradh 		/* Reflect requested flags in the bus_space map.  */
1322ef3036bSriastradh 		if (ISSET(map->flags, _DRM_WRITE_COMBINING))
1332ef3036bSriastradh 			flags |= BUS_SPACE_MAP_PREFETCHABLE;
1342ef3036bSriastradh 
1356cb10275Sriastradh 		/* Map it.  */
1362ef3036bSriastradh 		if (bus_space_map(bst, map->offset, map->size, flags,
1372ef3036bSriastradh 			&map->lm_data.bus_space.bsh))
1386cb10275Sriastradh 			break;
1396cb10275Sriastradh 
1406cb10275Sriastradh 		map->lm_data.bus_space.bus_map = bm;
1416cb10275Sriastradh 		goto win;
1426cb10275Sriastradh 	}
1436cb10275Sriastradh 
1446cb10275Sriastradh 	/* Couldn't map it.  Try borrowing from someone else.  */
1457df9e83cSriastradh 	if (drm_bus_borrow(map->offset, map->size,
1467df9e83cSriastradh 		&map->lm_data.bus_space.bsh)) {
1476cb10275Sriastradh 		map->lm_data.bus_space.bus_map = NULL;
1486cb10275Sriastradh 		goto win;
1496cb10275Sriastradh 	}
1506cb10275Sriastradh 
1516cb10275Sriastradh 	/* Failure!  */
1529f0b3ec0Sriastradh 	return;
1536cb10275Sriastradh 
1546cb10275Sriastradh win:	map->lm_data.bus_space.bst = bst;
1559f0b3ec0Sriastradh 	map->handle = bus_space_vaddr(bst, map->lm_data.bus_space.bsh);
1566cb10275Sriastradh }
1576cb10275Sriastradh 
1586cb10275Sriastradh void
drm_legacy_ioremapfree(struct drm_local_map * map,struct drm_device * dev)159231b70a7Sriastradh drm_legacy_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1606cb10275Sriastradh {
1616cb10275Sriastradh 	if (map->lm_data.bus_space.bus_map != NULL) {
1626cb10275Sriastradh 		bus_space_unmap(map->lm_data.bus_space.bst,
1636cb10275Sriastradh 		    map->lm_data.bus_space.bsh, map->size);
1646cb10275Sriastradh 		map->lm_data.bus_space.bus_map = NULL;
1659f0b3ec0Sriastradh 		map->handle = NULL;
1666cb10275Sriastradh 	}
1676cb10275Sriastradh }
1686cb10275Sriastradh 
1696cb10275Sriastradh /*
1706cb10275Sriastradh  * Allocate a drm dma handle, allocate memory fit for DMA, and map it.
1716cb10275Sriastradh  *
1726cb10275Sriastradh  * XXX This is called drm_pci_alloc for hysterical raisins; it is not
1736cb10275Sriastradh  * specific to PCI.
1746cb10275Sriastradh  *
1756cb10275Sriastradh  * XXX For now, we use non-blocking allocations because this is called
1766cb10275Sriastradh  * by ioctls with the drm global mutex held.
1776cb10275Sriastradh  *
1786cb10275Sriastradh  * XXX Error information is lost because this returns NULL on failure,
1796cb10275Sriastradh  * not even an error embedded in a pointer.
1806cb10275Sriastradh  */
1816cb10275Sriastradh struct drm_dma_handle *
drm_pci_alloc(struct drm_device * dev,size_t size,size_t align)1826cb10275Sriastradh drm_pci_alloc(struct drm_device *dev, size_t size, size_t align)
1836cb10275Sriastradh {
1846cb10275Sriastradh 	int nsegs;
1856cb10275Sriastradh 	int error;
1866cb10275Sriastradh 
1876cb10275Sriastradh 	/*
1886cb10275Sriastradh 	 * Allocate a drm_dma_handle record.
1896cb10275Sriastradh 	 */
1906cb10275Sriastradh 	struct drm_dma_handle *const dmah = kmem_alloc(sizeof(*dmah),
1916cb10275Sriastradh 	    KM_NOSLEEP);
1926cb10275Sriastradh 	if (dmah == NULL) {
1936cb10275Sriastradh 		error = -ENOMEM;
1946cb10275Sriastradh 		goto out;
1956cb10275Sriastradh 	}
1966cb10275Sriastradh 	dmah->dmah_tag = dev->dmat;
1976cb10275Sriastradh 
1986cb10275Sriastradh 	/*
1996cb10275Sriastradh 	 * Allocate the requested amount of DMA-safe memory.
2006cb10275Sriastradh 	 */
2016cb10275Sriastradh 	/* XXX errno NetBSD->Linux */
2026cb10275Sriastradh 	error = -bus_dmamem_alloc(dmah->dmah_tag, size, align, 0,
2036cb10275Sriastradh 	    &dmah->dmah_seg, 1, &nsegs, BUS_DMA_NOWAIT);
2046cb10275Sriastradh 	if (error)
2056cb10275Sriastradh 		goto fail0;
2066cb10275Sriastradh 	KASSERT(nsegs == 1);
2076cb10275Sriastradh 
2086cb10275Sriastradh 	/*
2096cb10275Sriastradh 	 * Map the DMA-safe memory into kernel virtual address space.
2106cb10275Sriastradh 	 */
2116cb10275Sriastradh 	/* XXX errno NetBSD->Linux */
2126cb10275Sriastradh 	error = -bus_dmamem_map(dmah->dmah_tag, &dmah->dmah_seg, 1, size,
2136cb10275Sriastradh 	    &dmah->vaddr,
2146cb10275Sriastradh 	    (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
2156cb10275Sriastradh 	if (error)
2166cb10275Sriastradh 		goto fail1;
2176cb10275Sriastradh 	dmah->size = size;
2186cb10275Sriastradh 
2196cb10275Sriastradh 	/*
2206cb10275Sriastradh 	 * Create a map for DMA transfers.
2216cb10275Sriastradh 	 */
2226cb10275Sriastradh 	/* XXX errno NetBSD->Linux */
2236cb10275Sriastradh 	error = -bus_dmamap_create(dmah->dmah_tag, size, 1, size, 0,
2246cb10275Sriastradh 	    BUS_DMA_NOWAIT, &dmah->dmah_map);
2256cb10275Sriastradh 	if (error)
2266cb10275Sriastradh 		goto fail2;
2276cb10275Sriastradh 
2286cb10275Sriastradh 	/*
2296cb10275Sriastradh 	 * Load the kva buffer into the map for DMA transfers.
2306cb10275Sriastradh 	 */
2316cb10275Sriastradh 	/* XXX errno NetBSD->Linux */
2326cb10275Sriastradh 	error = -bus_dmamap_load(dmah->dmah_tag, dmah->dmah_map, dmah->vaddr,
2336cb10275Sriastradh 	    size, NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
2346cb10275Sriastradh 	if (error)
2356cb10275Sriastradh 		goto fail3;
2366cb10275Sriastradh 
2376cb10275Sriastradh 	/* Record the bus address for convenient reference.  */
2386cb10275Sriastradh 	dmah->busaddr = dmah->dmah_map->dm_segs[0].ds_addr;
2396cb10275Sriastradh 
2406cb10275Sriastradh 	/* Zero the DMA buffer.  XXX Yikes!  Is this necessary?  */
2416cb10275Sriastradh 	memset(dmah->vaddr, 0, size);
2426cb10275Sriastradh 
2436cb10275Sriastradh 	/* Success!  */
2446cb10275Sriastradh 	return dmah;
2456cb10275Sriastradh 
2466cb10275Sriastradh fail3:	bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
2476cb10275Sriastradh fail2:	bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
2486cb10275Sriastradh fail1:	bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
2496cb10275Sriastradh fail0:	dmah->dmah_tag = NULL;	/* XXX paranoia */
2506cb10275Sriastradh 	kmem_free(dmah, sizeof(*dmah));
2516cb10275Sriastradh out:	DRM_DEBUG("drm_pci_alloc failed: %d\n", error);
2526cb10275Sriastradh 	return NULL;
2536cb10275Sriastradh }
2546cb10275Sriastradh 
2556cb10275Sriastradh /*
2566cb10275Sriastradh  * Release the bus DMA mappings and memory in dmah, and deallocate it.
2576cb10275Sriastradh  */
2586cb10275Sriastradh void
drm_pci_free(struct drm_device * dev,struct drm_dma_handle * dmah)2596cb10275Sriastradh drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah)
2606cb10275Sriastradh {
2616cb10275Sriastradh 
2626cb10275Sriastradh 	bus_dmamap_unload(dmah->dmah_tag, dmah->dmah_map);
2636cb10275Sriastradh 	bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
2646cb10275Sriastradh 	bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
2656cb10275Sriastradh 	bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
2666cb10275Sriastradh 	dmah->dmah_tag = NULL;	/* XXX paranoia */
2676cb10275Sriastradh 	kmem_free(dmah, sizeof(*dmah));
2686cb10275Sriastradh }
2696cb10275Sriastradh 
2706cb10275Sriastradh /*
2716cb10275Sriastradh  * Make sure the DMA-safe memory allocated for dev lies between
2726cb10275Sriastradh  * min_addr and max_addr.  Can be used multiple times to restrict the
2736cb10275Sriastradh  * bounds further, but never to expand the bounds again.
2746cb10275Sriastradh  *
2756cb10275Sriastradh  * XXX Caller must guarantee nobody has used the tag yet,
2766cb10275Sriastradh  * i.e. allocated any DMA memory.
2776cb10275Sriastradh  */
2786cb10275Sriastradh int
drm_limit_dma_space(struct drm_device * dev,resource_size_t min_addr,resource_size_t max_addr)2796cb10275Sriastradh drm_limit_dma_space(struct drm_device *dev, resource_size_t min_addr,
2806cb10275Sriastradh     resource_size_t max_addr)
2816cb10275Sriastradh {
282d2e4193eSriastradh 	int ret;
2836cb10275Sriastradh 
2846cb10275Sriastradh 	KASSERT(min_addr <= max_addr);
2856cb10275Sriastradh 
2866cb10275Sriastradh 	/*
2876cb10275Sriastradh 	 * Limit it further if we have already limited it, and destroy
2886cb10275Sriastradh 	 * the old subregion DMA tag.
2896cb10275Sriastradh 	 */
2906cb10275Sriastradh 	if (dev->dmat_subregion_p) {
2916cb10275Sriastradh 		min_addr = MAX(min_addr, dev->dmat_subregion_min);
2926cb10275Sriastradh 		max_addr = MIN(max_addr, dev->dmat_subregion_max);
2936cb10275Sriastradh 		bus_dmatag_destroy(dev->dmat);
2946cb10275Sriastradh 	}
2956cb10275Sriastradh 
2966cb10275Sriastradh 	/*
2976d235455Sriastradh 	 * If our limit contains the 32-bit space but for some reason
2986d235455Sriastradh 	 * we can't use a subregion, either because the bus doesn't
2996d235455Sriastradh 	 * support >32-bit DMA or because bus_dma(9) on this platform
3006d235455Sriastradh 	 * lacks bus_dmatag_subregion, just use the 32-bit space.
3016d235455Sriastradh 	 */
3026d235455Sriastradh 	if (min_addr == 0 && max_addr >= UINT32_C(0xffffffff) &&
3036d235455Sriastradh 	    dev->bus_dmat == dev->bus_dmat32) {
3046d235455Sriastradh dma32:		dev->dmat = dev->bus_dmat32;
3056d235455Sriastradh 		dev->dmat_subregion_p = false;
3066d235455Sriastradh 		dev->dmat_subregion_min = 0;
3076d235455Sriastradh 		dev->dmat_subregion_max = UINT32_C(0xffffffff);
3086d235455Sriastradh 		return 0;
3096d235455Sriastradh 	}
3106d235455Sriastradh 
3116d235455Sriastradh 	/*
3126cb10275Sriastradh 	 * Create a DMA tag for a subregion from the bus's DMA tag.  If
3136cb10275Sriastradh 	 * that fails, restore dev->dmat to the whole region so that we
3146cb10275Sriastradh 	 * need not worry about dev->dmat being uninitialized (not that
3156cb10275Sriastradh 	 * the caller should try to allocate DMA-safe memory on failure
3166cb10275Sriastradh 	 * anyway, but...paranoia).
3176cb10275Sriastradh 	 */
318d2e4193eSriastradh 	/* XXX errno NetBSD->Linux */
319d2e4193eSriastradh 	ret = -bus_dmatag_subregion(dev->bus_dmat, min_addr, max_addr,
3206cb10275Sriastradh 	    &dev->dmat, BUS_DMA_WAITOK);
321d2e4193eSriastradh 	if (ret) {
3226d235455Sriastradh 		/*
3236d235455Sriastradh 		 * bus_dmatag_subregion may fail.  If so, and if the
3246d235455Sriastradh 		 * subregion contains the 32-bit space, just use the
3256d235455Sriastradh 		 * 32-bit DMA tag.
3266d235455Sriastradh 		 */
3276d235455Sriastradh 		if (ret == -EOPNOTSUPP && dev->bus_dmat32 &&
3286d235455Sriastradh 		    min_addr == 0 && max_addr >= UINT32_C(0xffffffff))
3296d235455Sriastradh 			goto dma32;
3306d235455Sriastradh 		/* XXX Back out?  */
3316cb10275Sriastradh 		dev->dmat = dev->bus_dmat;
332d2e4193eSriastradh 		dev->dmat_subregion_p = false;
3336d235455Sriastradh 		dev->dmat_subregion_min = 0;
3346d235455Sriastradh 		dev->dmat_subregion_max = __type_max(bus_addr_t);
335d2e4193eSriastradh 		return ret;
3366cb10275Sriastradh 	}
3376cb10275Sriastradh 
3386cb10275Sriastradh 	/*
3396cb10275Sriastradh 	 * Remember that we have a subregion tag so that we know to
3406cb10275Sriastradh 	 * destroy it later, and record the bounds in case we need to
3416cb10275Sriastradh 	 * limit them again.
3426cb10275Sriastradh 	 */
3436cb10275Sriastradh 	dev->dmat_subregion_p = true;
3446cb10275Sriastradh 	dev->dmat_subregion_min = min_addr;
3456cb10275Sriastradh 	dev->dmat_subregion_max = max_addr;
3466cb10275Sriastradh 
3476cb10275Sriastradh 	/* Success!  */
3486cb10275Sriastradh 	return 0;
3496cb10275Sriastradh }
350*e67b1c18Sriastradh 
351*e67b1c18Sriastradh bool
drm_need_swiotlb(int dma_bits)352*e67b1c18Sriastradh drm_need_swiotlb(int dma_bits)
353*e67b1c18Sriastradh {
354*e67b1c18Sriastradh 
355*e67b1c18Sriastradh 	return false;
356*e67b1c18Sriastradh }
357