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