Home | History | Annotate | Line # | Download | only in drm
drm_memory.c revision 1.6.4.2
      1  1.6.4.2  tls /*	$NetBSD: drm_memory.c,v 1.6.4.2 2014/08/20 00:04:20 tls Exp $	*/
      2  1.6.4.2  tls 
      3  1.6.4.2  tls /*-
      4  1.6.4.2  tls  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.6.4.2  tls  * All rights reserved.
      6  1.6.4.2  tls  *
      7  1.6.4.2  tls  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6.4.2  tls  * by Taylor R. Campbell.
      9  1.6.4.2  tls  *
     10  1.6.4.2  tls  * Redistribution and use in source and binary forms, with or without
     11  1.6.4.2  tls  * modification, are permitted provided that the following conditions
     12  1.6.4.2  tls  * are met:
     13  1.6.4.2  tls  * 1. Redistributions of source code must retain the above copyright
     14  1.6.4.2  tls  *    notice, this list of conditions and the following disclaimer.
     15  1.6.4.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.6.4.2  tls  *    notice, this list of conditions and the following disclaimer in the
     17  1.6.4.2  tls  *    documentation and/or other materials provided with the distribution.
     18  1.6.4.2  tls  *
     19  1.6.4.2  tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.6.4.2  tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.6.4.2  tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.6.4.2  tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.6.4.2  tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.6.4.2  tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.6.4.2  tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.6.4.2  tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.6.4.2  tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.6.4.2  tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.6.4.2  tls  * POSSIBILITY OF SUCH DAMAGE.
     30  1.6.4.2  tls  */
     31  1.6.4.2  tls 
     32  1.6.4.2  tls #include <sys/cdefs.h>
     33  1.6.4.2  tls __KERNEL_RCSID(0, "$NetBSD: drm_memory.c,v 1.6.4.2 2014/08/20 00:04:20 tls Exp $");
     34  1.6.4.2  tls 
     35  1.6.4.2  tls #ifdef _KERNEL_OPT
     36  1.6.4.2  tls #include "agp_i810.h"
     37  1.6.4.2  tls #include "genfb.h"
     38  1.6.4.2  tls #else
     39  1.6.4.2  tls #define	NAGP_I810	1	/* XXX WTF?  */
     40  1.6.4.2  tls #define	NGENFB		0	/* XXX WTF?  */
     41  1.6.4.2  tls #endif
     42  1.6.4.2  tls 
     43  1.6.4.2  tls #include <sys/bus.h>
     44  1.6.4.2  tls 
     45  1.6.4.2  tls #if NAGP_I810 > 0
     46  1.6.4.2  tls /* XXX include order botch -- shouldn't need to include pcivar.h */
     47  1.6.4.2  tls #include <dev/pci/pcivar.h>
     48  1.6.4.2  tls #include <dev/pci/agpvar.h>
     49  1.6.4.2  tls #endif
     50  1.6.4.2  tls 
     51  1.6.4.2  tls #if NGENFB > 0
     52  1.6.4.2  tls #include <dev/wsfb/genfbvar.h>
     53  1.6.4.2  tls #endif
     54  1.6.4.2  tls 
     55  1.6.4.2  tls #include <drm/drmP.h>
     56  1.6.4.2  tls 
     57  1.6.4.2  tls /*
     58  1.6.4.2  tls  * XXX drm_bus_borrow is a horrible kludge!
     59  1.6.4.2  tls  */
     60  1.6.4.2  tls static bool
     61  1.6.4.2  tls drm_bus_borrow(bus_addr_t base, bus_size_t size, bus_space_handle_t *handlep)
     62  1.6.4.2  tls {
     63  1.6.4.2  tls 
     64  1.6.4.2  tls #if NAGP_I810 > 0
     65  1.6.4.2  tls 	if (agp_i810_borrow(base, size, handlep))
     66  1.6.4.2  tls 		return true;
     67  1.6.4.2  tls #endif
     68  1.6.4.2  tls 
     69  1.6.4.2  tls #if NGENFB > 0
     70  1.6.4.2  tls 	if (genfb_borrow(base, handlep))
     71  1.6.4.2  tls 		return true;
     72  1.6.4.2  tls #endif
     73  1.6.4.2  tls 
     74  1.6.4.2  tls 	return false;
     75  1.6.4.2  tls }
     76  1.6.4.2  tls 
     77  1.6.4.2  tls void *
     78  1.6.4.2  tls drm_ioremap(struct drm_device *dev, struct drm_local_map *map)
     79  1.6.4.2  tls {
     80  1.6.4.2  tls 	const bus_space_tag_t bst = dev->bst;
     81  1.6.4.2  tls 	unsigned int unit;
     82  1.6.4.2  tls 
     83  1.6.4.2  tls 	/*
     84  1.6.4.2  tls 	 * Search dev's bus maps for a match.
     85  1.6.4.2  tls 	 */
     86  1.6.4.2  tls 	for (unit = 0; unit < dev->bus_nmaps; unit++) {
     87  1.6.4.2  tls 		struct drm_bus_map *const bm = &dev->bus_maps[unit];
     88  1.6.4.2  tls 		int flags = bm->bm_flags;
     89  1.6.4.2  tls 
     90  1.6.4.2  tls 		/* Reject maps starting after the request.  */
     91  1.6.4.2  tls 		if (map->offset < bm->bm_base)
     92  1.6.4.2  tls 			continue;
     93  1.6.4.2  tls 
     94  1.6.4.2  tls 		/* Reject maps smaller than the request.  */
     95  1.6.4.2  tls 		if (bm->bm_size < map->size)
     96  1.6.4.2  tls 			continue;
     97  1.6.4.2  tls 
     98  1.6.4.2  tls 		/* Reject maps that the request doesn't fit in.  */
     99  1.6.4.2  tls 		if ((bm->bm_size - map->size) <
    100  1.6.4.2  tls 		    (map->offset - bm->bm_base))
    101  1.6.4.2  tls 			continue;
    102  1.6.4.2  tls 
    103  1.6.4.2  tls 		/* Ensure we can map the space into virtual memory.  */
    104  1.6.4.2  tls 		if (!ISSET(flags, BUS_SPACE_MAP_LINEAR))
    105  1.6.4.2  tls 			continue;
    106  1.6.4.2  tls 
    107  1.6.4.2  tls 		/* Reflect requested flags in the bus_space map.  */
    108  1.6.4.2  tls 		if (ISSET(map->flags, _DRM_WRITE_COMBINING))
    109  1.6.4.2  tls 			flags |= BUS_SPACE_MAP_PREFETCHABLE;
    110  1.6.4.2  tls 
    111  1.6.4.2  tls 		/* Map it.  */
    112  1.6.4.2  tls 		if (bus_space_map(bst, map->offset, map->size, flags,
    113  1.6.4.2  tls 			&map->lm_data.bus_space.bsh))
    114  1.6.4.2  tls 			break;
    115  1.6.4.2  tls 
    116  1.6.4.2  tls 		map->lm_data.bus_space.bus_map = bm;
    117  1.6.4.2  tls 		goto win;
    118  1.6.4.2  tls 	}
    119  1.6.4.2  tls 
    120  1.6.4.2  tls 	/* Couldn't map it.  Try borrowing from someone else.  */
    121  1.6.4.2  tls 	if (drm_bus_borrow(map->offset, map->size,
    122  1.6.4.2  tls 		&map->lm_data.bus_space.bsh)) {
    123  1.6.4.2  tls 		map->lm_data.bus_space.bus_map = NULL;
    124  1.6.4.2  tls 		goto win;
    125  1.6.4.2  tls 	}
    126  1.6.4.2  tls 
    127  1.6.4.2  tls 	/* Failure!  */
    128  1.6.4.2  tls 	return NULL;
    129  1.6.4.2  tls 
    130  1.6.4.2  tls win:	map->lm_data.bus_space.bst = bst;
    131  1.6.4.2  tls 	return bus_space_vaddr(bst, map->lm_data.bus_space.bsh);
    132  1.6.4.2  tls }
    133  1.6.4.2  tls 
    134  1.6.4.2  tls void
    135  1.6.4.2  tls drm_iounmap(struct drm_device *dev, struct drm_local_map *map)
    136  1.6.4.2  tls {
    137  1.6.4.2  tls 	if (map->lm_data.bus_space.bus_map != NULL) {
    138  1.6.4.2  tls 		bus_space_unmap(map->lm_data.bus_space.bst,
    139  1.6.4.2  tls 		    map->lm_data.bus_space.bsh, map->size);
    140  1.6.4.2  tls 		map->lm_data.bus_space.bus_map = NULL;
    141  1.6.4.2  tls 	}
    142  1.6.4.2  tls }
    143  1.6.4.2  tls 
    144  1.6.4.2  tls /*
    145  1.6.4.2  tls  * Allocate a drm dma handle, allocate memory fit for DMA, and map it.
    146  1.6.4.2  tls  *
    147  1.6.4.2  tls  * XXX This is called drm_pci_alloc for hysterical raisins; it is not
    148  1.6.4.2  tls  * specific to PCI.
    149  1.6.4.2  tls  *
    150  1.6.4.2  tls  * XXX For now, we use non-blocking allocations because this is called
    151  1.6.4.2  tls  * by ioctls with the drm global mutex held.
    152  1.6.4.2  tls  *
    153  1.6.4.2  tls  * XXX Error information is lost because this returns NULL on failure,
    154  1.6.4.2  tls  * not even an error embedded in a pointer.
    155  1.6.4.2  tls  */
    156  1.6.4.2  tls struct drm_dma_handle *
    157  1.6.4.2  tls drm_pci_alloc(struct drm_device *dev, size_t size, size_t align)
    158  1.6.4.2  tls {
    159  1.6.4.2  tls 	int nsegs;
    160  1.6.4.2  tls 	int error;
    161  1.6.4.2  tls 
    162  1.6.4.2  tls 	/*
    163  1.6.4.2  tls 	 * Allocate a drm_dma_handle record.
    164  1.6.4.2  tls 	 */
    165  1.6.4.2  tls 	struct drm_dma_handle *const dmah = kmem_alloc(sizeof(*dmah),
    166  1.6.4.2  tls 	    KM_NOSLEEP);
    167  1.6.4.2  tls 	if (dmah == NULL) {
    168  1.6.4.2  tls 		error = -ENOMEM;
    169  1.6.4.2  tls 		goto out;
    170  1.6.4.2  tls 	}
    171  1.6.4.2  tls 	dmah->dmah_tag = dev->dmat;
    172  1.6.4.2  tls 
    173  1.6.4.2  tls 	/*
    174  1.6.4.2  tls 	 * Allocate the requested amount of DMA-safe memory.
    175  1.6.4.2  tls 	 */
    176  1.6.4.2  tls 	/* XXX errno NetBSD->Linux */
    177  1.6.4.2  tls 	error = -bus_dmamem_alloc(dmah->dmah_tag, size, align, 0,
    178  1.6.4.2  tls 	    &dmah->dmah_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    179  1.6.4.2  tls 	if (error)
    180  1.6.4.2  tls 		goto fail0;
    181  1.6.4.2  tls 	KASSERT(nsegs == 1);
    182  1.6.4.2  tls 
    183  1.6.4.2  tls 	/*
    184  1.6.4.2  tls 	 * Map the DMA-safe memory into kernel virtual address space.
    185  1.6.4.2  tls 	 */
    186  1.6.4.2  tls 	/* XXX errno NetBSD->Linux */
    187  1.6.4.2  tls 	error = -bus_dmamem_map(dmah->dmah_tag, &dmah->dmah_seg, 1, size,
    188  1.6.4.2  tls 	    &dmah->vaddr,
    189  1.6.4.2  tls 	    (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
    190  1.6.4.2  tls 	if (error)
    191  1.6.4.2  tls 		goto fail1;
    192  1.6.4.2  tls 	dmah->size = size;
    193  1.6.4.2  tls 
    194  1.6.4.2  tls 	/*
    195  1.6.4.2  tls 	 * Create a map for DMA transfers.
    196  1.6.4.2  tls 	 */
    197  1.6.4.2  tls 	/* XXX errno NetBSD->Linux */
    198  1.6.4.2  tls 	error = -bus_dmamap_create(dmah->dmah_tag, size, 1, size, 0,
    199  1.6.4.2  tls 	    BUS_DMA_NOWAIT, &dmah->dmah_map);
    200  1.6.4.2  tls 	if (error)
    201  1.6.4.2  tls 		goto fail2;
    202  1.6.4.2  tls 
    203  1.6.4.2  tls 	/*
    204  1.6.4.2  tls 	 * Load the kva buffer into the map for DMA transfers.
    205  1.6.4.2  tls 	 */
    206  1.6.4.2  tls 	/* XXX errno NetBSD->Linux */
    207  1.6.4.2  tls 	error = -bus_dmamap_load(dmah->dmah_tag, dmah->dmah_map, dmah->vaddr,
    208  1.6.4.2  tls 	    size, NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
    209  1.6.4.2  tls 	if (error)
    210  1.6.4.2  tls 		goto fail3;
    211  1.6.4.2  tls 
    212  1.6.4.2  tls 	/* Record the bus address for convenient reference.  */
    213  1.6.4.2  tls 	dmah->busaddr = dmah->dmah_map->dm_segs[0].ds_addr;
    214  1.6.4.2  tls 
    215  1.6.4.2  tls 	/* Zero the DMA buffer.  XXX Yikes!  Is this necessary?  */
    216  1.6.4.2  tls 	memset(dmah->vaddr, 0, size);
    217  1.6.4.2  tls 
    218  1.6.4.2  tls 	/* Success!  */
    219  1.6.4.2  tls 	return dmah;
    220  1.6.4.2  tls 
    221  1.6.4.2  tls fail3:	bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
    222  1.6.4.2  tls fail2:	bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
    223  1.6.4.2  tls fail1:	bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
    224  1.6.4.2  tls fail0:	dmah->dmah_tag = NULL;	/* XXX paranoia */
    225  1.6.4.2  tls 	kmem_free(dmah, sizeof(*dmah));
    226  1.6.4.2  tls out:	DRM_DEBUG("drm_pci_alloc failed: %d\n", error);
    227  1.6.4.2  tls 	return NULL;
    228  1.6.4.2  tls }
    229  1.6.4.2  tls 
    230  1.6.4.2  tls /*
    231  1.6.4.2  tls  * Release the bus DMA mappings and memory in dmah, and deallocate it.
    232  1.6.4.2  tls  */
    233  1.6.4.2  tls void
    234  1.6.4.2  tls drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah)
    235  1.6.4.2  tls {
    236  1.6.4.2  tls 
    237  1.6.4.2  tls 	bus_dmamap_unload(dmah->dmah_tag, dmah->dmah_map);
    238  1.6.4.2  tls 	bus_dmamap_destroy(dmah->dmah_tag, dmah->dmah_map);
    239  1.6.4.2  tls 	bus_dmamem_unmap(dmah->dmah_tag, dmah->vaddr, dmah->size);
    240  1.6.4.2  tls 	bus_dmamem_free(dmah->dmah_tag, &dmah->dmah_seg, 1);
    241  1.6.4.2  tls 	dmah->dmah_tag = NULL;	/* XXX paranoia */
    242  1.6.4.2  tls 	kmem_free(dmah, sizeof(*dmah));
    243  1.6.4.2  tls }
    244  1.6.4.2  tls 
    245  1.6.4.2  tls /*
    246  1.6.4.2  tls  * Make sure the DMA-safe memory allocated for dev lies between
    247  1.6.4.2  tls  * min_addr and max_addr.  Can be used multiple times to restrict the
    248  1.6.4.2  tls  * bounds further, but never to expand the bounds again.
    249  1.6.4.2  tls  *
    250  1.6.4.2  tls  * XXX Caller must guarantee nobody has used the tag yet,
    251  1.6.4.2  tls  * i.e. allocated any DMA memory.
    252  1.6.4.2  tls  */
    253  1.6.4.2  tls int
    254  1.6.4.2  tls drm_limit_dma_space(struct drm_device *dev, resource_size_t min_addr,
    255  1.6.4.2  tls     resource_size_t max_addr)
    256  1.6.4.2  tls {
    257  1.6.4.2  tls 	int ret;
    258  1.6.4.2  tls 
    259  1.6.4.2  tls 	KASSERT(min_addr <= max_addr);
    260  1.6.4.2  tls 
    261  1.6.4.2  tls 	/*
    262  1.6.4.2  tls 	 * Limit it further if we have already limited it, and destroy
    263  1.6.4.2  tls 	 * the old subregion DMA tag.
    264  1.6.4.2  tls 	 */
    265  1.6.4.2  tls 	if (dev->dmat_subregion_p) {
    266  1.6.4.2  tls 		min_addr = MAX(min_addr, dev->dmat_subregion_min);
    267  1.6.4.2  tls 		max_addr = MIN(max_addr, dev->dmat_subregion_max);
    268  1.6.4.2  tls 		bus_dmatag_destroy(dev->dmat);
    269  1.6.4.2  tls 	}
    270  1.6.4.2  tls 
    271  1.6.4.2  tls 	/*
    272  1.6.4.2  tls 	 * Create a DMA tag for a subregion from the bus's DMA tag.  If
    273  1.6.4.2  tls 	 * that fails, restore dev->dmat to the whole region so that we
    274  1.6.4.2  tls 	 * need not worry about dev->dmat being uninitialized (not that
    275  1.6.4.2  tls 	 * the caller should try to allocate DMA-safe memory on failure
    276  1.6.4.2  tls 	 * anyway, but...paranoia).
    277  1.6.4.2  tls 	 */
    278  1.6.4.2  tls 	/* XXX errno NetBSD->Linux */
    279  1.6.4.2  tls 	ret = -bus_dmatag_subregion(dev->bus_dmat, min_addr, max_addr,
    280  1.6.4.2  tls 	    &dev->dmat, BUS_DMA_WAITOK);
    281  1.6.4.2  tls 	if (ret) {
    282  1.6.4.2  tls 		dev->dmat = dev->bus_dmat;
    283  1.6.4.2  tls 		dev->dmat_subregion_p = false;
    284  1.6.4.2  tls 		return ret;
    285  1.6.4.2  tls 	}
    286  1.6.4.2  tls 
    287  1.6.4.2  tls 	/*
    288  1.6.4.2  tls 	 * Remember that we have a subregion tag so that we know to
    289  1.6.4.2  tls 	 * destroy it later, and record the bounds in case we need to
    290  1.6.4.2  tls 	 * limit them again.
    291  1.6.4.2  tls 	 */
    292  1.6.4.2  tls 	dev->dmat_subregion_p = true;
    293  1.6.4.2  tls 	dev->dmat_subregion_min = min_addr;
    294  1.6.4.2  tls 	dev->dmat_subregion_max = max_addr;
    295  1.6.4.2  tls 
    296  1.6.4.2  tls 	/* Success!  */
    297  1.6.4.2  tls 	return 0;
    298  1.6.4.2  tls }
    299