Home | History | Annotate | Line # | Download | only in pci
mcpcia_dma.c revision 1.12
      1 /* $NetBSD: mcpcia_dma.c,v 1.12 1999/04/15 23:47:52 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.12 1999/04/15 23:47:52 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 #include <vm/vm.h>
     50 
     51 #define _ALPHA_BUS_DMA_PRIVATE
     52 #include <machine/bus.h>
     53 
     54 #include <dev/pci/pcireg.h>
     55 #include <dev/pci/pcivar.h>
     56 #include <alpha/pci/mcpciareg.h>
     57 #include <alpha/pci/mcpciavar.h>
     58 #include <alpha/pci/pci_kn300.h>
     59 
     60 bus_dma_tag_t mcpcia_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
     61 
     62 int	mcpcia_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     63 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     64 
     65 void	mcpcia_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     66 
     67 int	mcpcia_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     68 	    bus_size_t, struct proc *, int));
     69 
     70 int	mcpcia_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     71 	    struct mbuf *, int));
     72 
     73 int	mcpcia_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     74 	    struct uio *, int));
     75 
     76 int	mcpcia_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     77 	    bus_dma_segment_t *, int, bus_size_t, int));
     78 
     79 void	mcpcia_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     80 
     81 /*
     82  * Direct-mapped window: 2G at 2G
     83  */
     84 #define	MCPCIA_DIRECT_MAPPED_BASE	(2UL*1024UL*1024UL*1024UL)
     85 #define	MCPCIA_DIRECT_MAPPED_SIZE	(2UL*1024UL*1024UL*1024UL)
     86 
     87 /*
     88  * SGMAP window for PCI: 1G at 1G
     89  */
     90 #define	MCPCIA_PCI_SG_MAPPED_BASE	(1UL*1024UL*1024UL*1024UL)
     91 #define	MCPCIA_PCI_SG_MAPPED_SIZE	(1UL*1024UL*1024UL*1024UL)
     92 
     93 /*
     94  * SGMAP window for ISA: 8M at 8M
     95  */
     96 #define	MCPCIA_ISA_SG_MAPPED_BASE	(8*1024*1024)
     97 #define	MCPCIA_ISA_SG_MAPPED_SIZE	(8*1024*1024)
     98 
     99 #define	MCPCIA_SGTLB_INVALIDATE(ccp)					\
    100 do {									\
    101 	alpha_mb();							\
    102 	REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef;			\
    103 	alpha_mb();							\
    104 } while (0)
    105 
    106 void
    107 mcpcia_dma_init(ccp)
    108 	struct mcpcia_config *ccp;
    109 {
    110 	bus_dma_tag_t t;
    111 
    112 	/*
    113 	 * Initialize the DMA tag used for direct-mapped DMA.
    114 	 */
    115 	t = &ccp->cc_dmat_direct;
    116 	t->_cookie = ccp;
    117 	t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
    118 	t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE;
    119 	t->_next_window = &ccp->cc_dmat_pci_sgmap;
    120 	t->_boundary = 0;
    121 	t->_sgmap = NULL;
    122 	t->_get_tag = mcpcia_dma_get_tag;
    123 	t->_dmamap_create = _bus_dmamap_create;
    124 	t->_dmamap_destroy = _bus_dmamap_destroy;
    125 	t->_dmamap_load = _bus_dmamap_load_direct;
    126 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    127 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    128 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    129 	t->_dmamap_unload = _bus_dmamap_unload;
    130 	t->_dmamap_sync = _bus_dmamap_sync;
    131 
    132 	t->_dmamem_alloc = _bus_dmamem_alloc;
    133 	t->_dmamem_free = _bus_dmamem_free;
    134 	t->_dmamem_map = _bus_dmamem_map;
    135 	t->_dmamem_unmap = _bus_dmamem_unmap;
    136 	t->_dmamem_mmap = _bus_dmamem_mmap;
    137 
    138 	/*
    139 	 * Initialize the DMA tag used for sgmap-mapped PCI DMA.
    140 	 */
    141 	t = &ccp->cc_dmat_pci_sgmap;
    142 	t->_cookie = ccp;
    143 	t->_wbase = MCPCIA_PCI_SG_MAPPED_BASE;
    144 	t->_wsize = MCPCIA_PCI_SG_MAPPED_SIZE;
    145 	t->_next_window = NULL;
    146 	t->_boundary = 0;
    147 	t->_sgmap = &ccp->cc_pci_sgmap;
    148 	t->_get_tag = mcpcia_dma_get_tag;
    149 	t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
    150 	t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
    151 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    152 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    153 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    154 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    155 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    156 	t->_dmamap_sync = _bus_dmamap_sync;
    157 
    158 	t->_dmamem_alloc = _bus_dmamem_alloc;
    159 	t->_dmamem_free = _bus_dmamem_free;
    160 	t->_dmamem_map = _bus_dmamem_map;
    161 	t->_dmamem_unmap = _bus_dmamem_unmap;
    162 	t->_dmamem_mmap = _bus_dmamem_mmap;
    163 
    164 	/*
    165 	 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
    166 	 */
    167 	t = &ccp->cc_dmat_isa_sgmap;
    168 	t->_cookie = ccp;
    169 	t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
    170 	t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
    171 	t->_next_window = NULL;
    172 	t->_boundary = 0;
    173 	t->_sgmap = &ccp->cc_isa_sgmap;
    174 	t->_get_tag = mcpcia_dma_get_tag;
    175 	t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
    176 	t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
    177 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    178 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    179 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    180 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    181 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    182 	t->_dmamap_sync = _bus_dmamap_sync;
    183 
    184 	t->_dmamem_alloc = _bus_dmamem_alloc;
    185 	t->_dmamem_free = _bus_dmamem_free;
    186 	t->_dmamem_map = _bus_dmamem_map;
    187 	t->_dmamem_unmap = _bus_dmamem_unmap;
    188 	t->_dmamem_mmap = _bus_dmamem_mmap;
    189 
    190 	/*
    191 	 * Initialize the SGMAPs.
    192 	 */
    193 	alpha_sgmap_init(&ccp->cc_dmat_pci_sgmap, &ccp->cc_pci_sgmap,
    194 	    "mcpcia pci sgmap",
    195 	    MCPCIA_PCI_SG_MAPPED_BASE, 0, MCPCIA_PCI_SG_MAPPED_SIZE,
    196 	    sizeof(u_int64_t), NULL, 0);
    197 
    198 	alpha_sgmap_init(&ccp->cc_dmat_isa_sgmap, &ccp->cc_isa_sgmap,
    199 	    "mcpcia isa sgmap",
    200 	    MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
    201 	    sizeof(u_int64_t), NULL, 0);
    202 
    203 	/*
    204 	 * Disable windows first.
    205 	 */
    206 
    207 	REGVAL(MCPCIA_W0_BASE(ccp)) = 0;
    208 	REGVAL(MCPCIA_W1_BASE(ccp)) = 0;
    209 	REGVAL(MCPCIA_W2_BASE(ccp)) = 0;
    210 	REGVAL(MCPCIA_W3_BASE(ccp)) = 0;
    211 	REGVAL(MCPCIA_T0_BASE(ccp)) = 0;
    212 	REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
    213 	REGVAL(MCPCIA_T2_BASE(ccp)) = 0;
    214 	REGVAL(MCPCIA_T3_BASE(ccp)) = 0;
    215 	alpha_mb();
    216 
    217 	/*
    218 	 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
    219 	 */
    220 	REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_8M;
    221 	REGVAL(MCPCIA_T0_BASE(ccp)) =
    222 		ccp->cc_isa_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    223 	alpha_mb();
    224 	REGVAL(MCPCIA_W0_BASE(ccp)) =
    225 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
    226 	alpha_mb();
    227 
    228 	MCPCIA_SGTLB_INVALIDATE(ccp);
    229 
    230 	/*
    231 	 * Set up window 1 as a 2 GB Direct-mapped window starting at 2GB.
    232 	 */
    233 
    234 	REGVAL(MCPCIA_W1_MASK(ccp)) = MCPCIA_WMASK_2G;
    235 	REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
    236 	alpha_mb();
    237 	REGVAL(MCPCIA_W1_BASE(ccp)) =
    238 		MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
    239 	alpha_mb();
    240 
    241 	/*
    242 	 * Set up window 2 as a 1G SGMAP-mapped window starting at 1G.
    243 	 */
    244 
    245 	REGVAL(MCPCIA_W2_MASK(ccp)) = MCPCIA_WMASK_1G;
    246 	REGVAL(MCPCIA_T2_BASE(ccp)) =
    247 		ccp->cc_pci_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    248 	alpha_mb();
    249 	REGVAL(MCPCIA_W2_BASE(ccp)) =
    250 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_PCI_SG_MAPPED_BASE;
    251 	alpha_mb();
    252 
    253 	/* XXX XXX BEGIN XXX XXX */
    254 	{							/* XXX */
    255 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
    256 		alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
    257 	}							/* XXX */
    258 	/* XXX XXX END XXX XXX */
    259 }
    260 
    261 /*
    262  * Return the bus dma tag to be used for the specified bus type.
    263  * INTERNAL USE ONLY!
    264  */
    265 bus_dma_tag_t
    266 mcpcia_dma_get_tag(t, bustype)
    267 	bus_dma_tag_t t;
    268 	alpha_bus_t bustype;
    269 {
    270 	struct mcpcia_config *ccp = t->_cookie;
    271 
    272 	switch (bustype) {
    273 	case ALPHA_BUS_PCI:
    274 	case ALPHA_BUS_EISA:
    275 		/*
    276 		 * Start off using the direct-mapped window.  We will
    277 		 * automatically fall backed onto the chained PCI SGMAP
    278 		 * window if necessary.
    279 		 */
    280 		return (&ccp->cc_dmat_direct);
    281 
    282 	case ALPHA_BUS_ISA:
    283 		/*
    284 		 * ISA doesn't have enough address bits to use
    285 		 * the direct-mapped DMA window, so we must use
    286 		 * SGMAPs.
    287 		 */
    288 		return (&ccp->cc_dmat_isa_sgmap);
    289 
    290 	default:
    291 		panic("mcpcia_dma_get_tag: shouldn't be here, really...");
    292 	}
    293 }
    294 
    295 /*
    296  * Create a MCPCIA SGMAP-mapped DMA map.
    297  */
    298 int
    299 mcpcia_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    300     flags, dmamp)
    301 	bus_dma_tag_t t;
    302 	bus_size_t size;
    303 	int nsegments;
    304 	bus_size_t maxsegsz;
    305 	bus_size_t boundary;
    306 	int flags;
    307 	bus_dmamap_t *dmamp;
    308 {
    309 	bus_dmamap_t map;
    310 	int error;
    311 
    312 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    313 	    boundary, flags, dmamp);
    314 	if (error)
    315 		return (error);
    316 
    317 	map = *dmamp;
    318 
    319 	if (flags & BUS_DMA_ALLOCNOW) {
    320 		error = alpha_sgmap_alloc(map, round_page(size),
    321 		    t->_sgmap, flags);
    322 		if (error)
    323 			mcpcia_bus_dmamap_destroy_sgmap(t, map);
    324 	}
    325 
    326 	return (error);
    327 }
    328 
    329 /*
    330  * Destroy a MCPCIA SGMAP-mapped DMA map.
    331  */
    332 void
    333 mcpcia_bus_dmamap_destroy_sgmap(t, map)
    334 	bus_dma_tag_t t;
    335 	bus_dmamap_t map;
    336 {
    337 
    338 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    339 		alpha_sgmap_free(map, t->_sgmap);
    340 
    341 	_bus_dmamap_destroy(t, map);
    342 }
    343 
    344 /*
    345  * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
    346  */
    347 int
    348 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    349 	bus_dma_tag_t t;
    350 	bus_dmamap_t map;
    351 	void *buf;
    352 	bus_size_t buflen;
    353 	struct proc *p;
    354 	int flags;
    355 {
    356 	int error;
    357 	struct mcpcia_config *ccp = t->_cookie;
    358 
    359 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    360 	    t->_sgmap);
    361 	if (error == 0)
    362 		MCPCIA_SGTLB_INVALIDATE(ccp);
    363 	return (error);
    364 }
    365 
    366 /*
    367  * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
    368  */
    369 int
    370 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    371 	bus_dma_tag_t t;
    372 	bus_dmamap_t map;
    373 	struct mbuf *m;
    374 	int flags;
    375 {
    376 	int error;
    377 	struct mcpcia_config *ccp = t->_cookie;
    378 
    379 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    380 	if (error == 0)
    381 		MCPCIA_SGTLB_INVALIDATE(ccp);
    382 	return (error);
    383 }
    384 
    385 /*
    386  * Load a MCPCIA SGMAP-mapped DMA map with a uio.
    387  */
    388 int
    389 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    390 	bus_dma_tag_t t;
    391 	bus_dmamap_t map;
    392 	struct uio *uio;
    393 	int flags;
    394 {
    395 	int error;
    396 	struct mcpcia_config *ccp = t->_cookie;
    397 
    398 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    399 	if (error == 0)
    400 		MCPCIA_SGTLB_INVALIDATE(ccp);
    401 	return (error);
    402 }
    403 
    404 /*
    405  * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
    406  */
    407 int
    408 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    409 	bus_dma_tag_t t;
    410 	bus_dmamap_t map;
    411 	bus_dma_segment_t *segs;
    412 	int nsegs;
    413 	bus_size_t size;
    414 	int flags;
    415 {
    416 	int error;
    417 	struct mcpcia_config *ccp = t->_cookie;
    418 
    419 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    420 	    t->_sgmap);
    421 	if (error == 0)
    422 		MCPCIA_SGTLB_INVALIDATE(ccp);
    423 	return (error);
    424 }
    425 
    426 /*
    427  * Unload a MCPCIA DMA map.
    428  */
    429 void
    430 mcpcia_bus_dmamap_unload_sgmap(t, map)
    431 	bus_dma_tag_t t;
    432 	bus_dmamap_t map;
    433 {
    434 	struct mcpcia_config *ccp = t->_cookie;
    435 
    436 	/*
    437 	 * Invalidate any SGMAP page table entries used by this mapping.
    438 	 */
    439 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    440 	MCPCIA_SGTLB_INVALIDATE(ccp);
    441 
    442 	/*
    443 	 * Do the generic bits of the unload.
    444 	 */
    445 	_bus_dmamap_unload(t, map);
    446 }
    447