Home | History | Annotate | Line # | Download | only in pci
mcpcia_dma.c revision 1.17
      1 /* $NetBSD: mcpcia_dma.c,v 1.17 2009/03/14 14:45:53 dsl Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Matthew Jacob of the Numerical Aerospace Simulation
      9  * Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34 
     35 __KERNEL_RCSID(0, "$NetBSD: mcpcia_dma.c,v 1.17 2009/03/14 14:45:53 dsl Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #define _ALPHA_BUS_DMA_PRIVATE
     46 #include <machine/bus.h>
     47 
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcivar.h>
     50 #include <alpha/pci/mcpciareg.h>
     51 #include <alpha/pci/mcpciavar.h>
     52 #include <alpha/pci/pci_kn300.h>
     53 
     54 bus_dma_tag_t mcpcia_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     55 
     56 int	mcpcia_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     57 	    bus_size_t, struct proc *, int);
     58 
     59 int	mcpcia_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     60 	    struct mbuf *, int);
     61 
     62 int	mcpcia_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     63 	    struct uio *, int);
     64 
     65 int	mcpcia_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
     66 	    bus_dma_segment_t *, int, bus_size_t, int);
     67 
     68 void	mcpcia_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
     69 
     70 /*
     71  * Direct-mapped window: 2G at 2G
     72  */
     73 #define	MCPCIA_DIRECT_MAPPED_BASE	(2UL*1024UL*1024UL*1024UL)
     74 #define	MCPCIA_DIRECT_MAPPED_SIZE	(2UL*1024UL*1024UL*1024UL)
     75 
     76 /*
     77  * SGMAP window for PCI: 1G at 1G
     78  */
     79 #define	MCPCIA_PCI_SG_MAPPED_BASE	(1UL*1024UL*1024UL*1024UL)
     80 #define	MCPCIA_PCI_SG_MAPPED_SIZE	(1UL*1024UL*1024UL*1024UL)
     81 
     82 /*
     83  * SGMAP window for ISA: 8M at 8M
     84  */
     85 #define	MCPCIA_ISA_SG_MAPPED_BASE	(8*1024*1024)
     86 #define	MCPCIA_ISA_SG_MAPPED_SIZE	(8*1024*1024)
     87 
     88 /* MCPCIA has a 256-byte out-bound DMA prefetch threshold. */
     89 #define	MCPCIA_SG_MAPPED_PFTHRESH	256
     90 
     91 #define	MCPCIA_SGTLB_INVALIDATE(ccp)					\
     92 do {									\
     93 	alpha_mb();							\
     94 	REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef;			\
     95 	alpha_mb();							\
     96 } while (0)
     97 
     98 void
     99 mcpcia_dma_init(ccp)
    100 	struct mcpcia_config *ccp;
    101 {
    102 	bus_dma_tag_t t;
    103 
    104 	/*
    105 	 * Initialize the DMA tag used for direct-mapped DMA.
    106 	 */
    107 	t = &ccp->cc_dmat_direct;
    108 	t->_cookie = ccp;
    109 	t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
    110 	t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE;
    111 	t->_next_window = &ccp->cc_dmat_pci_sgmap;
    112 	t->_boundary = 0;
    113 	t->_sgmap = NULL;
    114 	t->_get_tag = mcpcia_dma_get_tag;
    115 	t->_dmamap_create = _bus_dmamap_create;
    116 	t->_dmamap_destroy = _bus_dmamap_destroy;
    117 	t->_dmamap_load = _bus_dmamap_load_direct;
    118 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    119 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    120 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    121 	t->_dmamap_unload = _bus_dmamap_unload;
    122 	t->_dmamap_sync = _bus_dmamap_sync;
    123 
    124 	t->_dmamem_alloc = _bus_dmamem_alloc;
    125 	t->_dmamem_free = _bus_dmamem_free;
    126 	t->_dmamem_map = _bus_dmamem_map;
    127 	t->_dmamem_unmap = _bus_dmamem_unmap;
    128 	t->_dmamem_mmap = _bus_dmamem_mmap;
    129 
    130 	/*
    131 	 * Initialize the DMA tag used for sgmap-mapped PCI DMA.
    132 	 */
    133 	t = &ccp->cc_dmat_pci_sgmap;
    134 	t->_cookie = ccp;
    135 	t->_wbase = MCPCIA_PCI_SG_MAPPED_BASE;
    136 	t->_wsize = MCPCIA_PCI_SG_MAPPED_SIZE;
    137 	t->_next_window = NULL;
    138 	t->_boundary = 0;
    139 	t->_sgmap = &ccp->cc_pci_sgmap;
    140 	t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH;
    141 	t->_get_tag = mcpcia_dma_get_tag;
    142 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    143 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    144 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    145 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    146 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    147 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    148 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    149 	t->_dmamap_sync = _bus_dmamap_sync;
    150 
    151 	t->_dmamem_alloc = _bus_dmamem_alloc;
    152 	t->_dmamem_free = _bus_dmamem_free;
    153 	t->_dmamem_map = _bus_dmamem_map;
    154 	t->_dmamem_unmap = _bus_dmamem_unmap;
    155 	t->_dmamem_mmap = _bus_dmamem_mmap;
    156 
    157 	/*
    158 	 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
    159 	 */
    160 	t = &ccp->cc_dmat_isa_sgmap;
    161 	t->_cookie = ccp;
    162 	t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
    163 	t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
    164 	t->_next_window = NULL;
    165 	t->_boundary = 0;
    166 	t->_sgmap = &ccp->cc_isa_sgmap;
    167 	t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH;
    168 	t->_get_tag = mcpcia_dma_get_tag;
    169 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    170 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    171 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    172 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    173 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    174 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    175 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    176 	t->_dmamap_sync = _bus_dmamap_sync;
    177 
    178 	t->_dmamem_alloc = _bus_dmamem_alloc;
    179 	t->_dmamem_free = _bus_dmamem_free;
    180 	t->_dmamem_map = _bus_dmamem_map;
    181 	t->_dmamem_unmap = _bus_dmamem_unmap;
    182 	t->_dmamem_mmap = _bus_dmamem_mmap;
    183 
    184 	/*
    185 	 * Initialize the SGMAPs.
    186 	 */
    187 	alpha_sgmap_init(&ccp->cc_dmat_pci_sgmap, &ccp->cc_pci_sgmap,
    188 	    "mcpcia pci sgmap",
    189 	    MCPCIA_PCI_SG_MAPPED_BASE, 0, MCPCIA_PCI_SG_MAPPED_SIZE,
    190 	    sizeof(u_int64_t), NULL, 0);
    191 
    192 	alpha_sgmap_init(&ccp->cc_dmat_isa_sgmap, &ccp->cc_isa_sgmap,
    193 	    "mcpcia isa sgmap",
    194 	    MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
    195 	    sizeof(u_int64_t), NULL, 0);
    196 
    197 	/*
    198 	 * Disable windows first.
    199 	 */
    200 
    201 	REGVAL(MCPCIA_W0_BASE(ccp)) = 0;
    202 	REGVAL(MCPCIA_W1_BASE(ccp)) = 0;
    203 	REGVAL(MCPCIA_W2_BASE(ccp)) = 0;
    204 	REGVAL(MCPCIA_W3_BASE(ccp)) = 0;
    205 	REGVAL(MCPCIA_T0_BASE(ccp)) = 0;
    206 	REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
    207 	REGVAL(MCPCIA_T2_BASE(ccp)) = 0;
    208 	REGVAL(MCPCIA_T3_BASE(ccp)) = 0;
    209 	alpha_mb();
    210 
    211 	/*
    212 	 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
    213 	 */
    214 	REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_8M;
    215 	REGVAL(MCPCIA_T0_BASE(ccp)) =
    216 		ccp->cc_isa_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    217 	alpha_mb();
    218 	REGVAL(MCPCIA_W0_BASE(ccp)) =
    219 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
    220 	alpha_mb();
    221 
    222 	MCPCIA_SGTLB_INVALIDATE(ccp);
    223 
    224 	/*
    225 	 * Set up window 1 as a 2 GB Direct-mapped window starting at 2GB.
    226 	 */
    227 
    228 	REGVAL(MCPCIA_W1_MASK(ccp)) = MCPCIA_WMASK_2G;
    229 	REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
    230 	alpha_mb();
    231 	REGVAL(MCPCIA_W1_BASE(ccp)) =
    232 		MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
    233 	alpha_mb();
    234 
    235 	/*
    236 	 * Set up window 2 as a 1G SGMAP-mapped window starting at 1G.
    237 	 */
    238 
    239 	REGVAL(MCPCIA_W2_MASK(ccp)) = MCPCIA_WMASK_1G;
    240 	REGVAL(MCPCIA_T2_BASE(ccp)) =
    241 		ccp->cc_pci_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    242 	alpha_mb();
    243 	REGVAL(MCPCIA_W2_BASE(ccp)) =
    244 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_PCI_SG_MAPPED_BASE;
    245 	alpha_mb();
    246 
    247 	/* XXX XXX BEGIN XXX XXX */
    248 	{							/* XXX */
    249 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
    250 		alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
    251 	}							/* XXX */
    252 	/* XXX XXX END XXX XXX */
    253 }
    254 
    255 /*
    256  * Return the bus dma tag to be used for the specified bus type.
    257  * INTERNAL USE ONLY!
    258  */
    259 bus_dma_tag_t
    260 mcpcia_dma_get_tag(t, bustype)
    261 	bus_dma_tag_t t;
    262 	alpha_bus_t bustype;
    263 {
    264 	struct mcpcia_config *ccp = t->_cookie;
    265 
    266 	switch (bustype) {
    267 	case ALPHA_BUS_PCI:
    268 	case ALPHA_BUS_EISA:
    269 		/*
    270 		 * Start off using the direct-mapped window.  We will
    271 		 * automatically fall backed onto the chained PCI SGMAP
    272 		 * window if necessary.
    273 		 */
    274 		return (&ccp->cc_dmat_direct);
    275 
    276 	case ALPHA_BUS_ISA:
    277 		/*
    278 		 * ISA doesn't have enough address bits to use
    279 		 * the direct-mapped DMA window, so we must use
    280 		 * SGMAPs.
    281 		 */
    282 		return (&ccp->cc_dmat_isa_sgmap);
    283 
    284 	default:
    285 		panic("mcpcia_dma_get_tag: shouldn't be here, really...");
    286 	}
    287 }
    288 
    289 /*
    290  * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
    291  */
    292 int
    293 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    294 	bus_dma_tag_t t;
    295 	bus_dmamap_t map;
    296 	void *buf;
    297 	bus_size_t buflen;
    298 	struct proc *p;
    299 	int flags;
    300 {
    301 	int error;
    302 	struct mcpcia_config *ccp = t->_cookie;
    303 
    304 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    305 	    t->_sgmap);
    306 	if (error == 0)
    307 		MCPCIA_SGTLB_INVALIDATE(ccp);
    308 	return (error);
    309 }
    310 
    311 /*
    312  * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
    313  */
    314 int
    315 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    316 	bus_dma_tag_t t;
    317 	bus_dmamap_t map;
    318 	struct mbuf *m;
    319 	int flags;
    320 {
    321 	int error;
    322 	struct mcpcia_config *ccp = t->_cookie;
    323 
    324 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    325 	if (error == 0)
    326 		MCPCIA_SGTLB_INVALIDATE(ccp);
    327 	return (error);
    328 }
    329 
    330 /*
    331  * Load a MCPCIA SGMAP-mapped DMA map with a uio.
    332  */
    333 int
    334 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    335 	bus_dma_tag_t t;
    336 	bus_dmamap_t map;
    337 	struct uio *uio;
    338 	int flags;
    339 {
    340 	int error;
    341 	struct mcpcia_config *ccp = t->_cookie;
    342 
    343 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    344 	if (error == 0)
    345 		MCPCIA_SGTLB_INVALIDATE(ccp);
    346 	return (error);
    347 }
    348 
    349 /*
    350  * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
    351  */
    352 int
    353 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    354 	bus_dma_tag_t t;
    355 	bus_dmamap_t map;
    356 	bus_dma_segment_t *segs;
    357 	int nsegs;
    358 	bus_size_t size;
    359 	int flags;
    360 {
    361 	int error;
    362 	struct mcpcia_config *ccp = t->_cookie;
    363 
    364 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    365 	    t->_sgmap);
    366 	if (error == 0)
    367 		MCPCIA_SGTLB_INVALIDATE(ccp);
    368 	return (error);
    369 }
    370 
    371 /*
    372  * Unload a MCPCIA DMA map.
    373  */
    374 void
    375 mcpcia_bus_dmamap_unload_sgmap(t, map)
    376 	bus_dma_tag_t t;
    377 	bus_dmamap_t map;
    378 {
    379 	struct mcpcia_config *ccp = t->_cookie;
    380 
    381 	/*
    382 	 * Invalidate any SGMAP page table entries used by this mapping.
    383 	 */
    384 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    385 	MCPCIA_SGTLB_INVALIDATE(ccp);
    386 
    387 	/*
    388 	 * Do the generic bits of the unload.
    389 	 */
    390 	_bus_dmamap_unload(t, map);
    391 }
    392