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