Home | History | Annotate | Line # | Download | only in pci
mcpcia_dma.c revision 1.4
      1 /* $NetBSD: mcpcia_dma.c,v 1.4 1998/05/21 23:38:04 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 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 of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center. This code happens to have been written
     10  * by Matthew Jacob for NASA/Ames.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the NetBSD
     23  *	Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPEMCPCIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     42 
     43 __KERNEL_RCSID(0, "$NetBSD: mcpcia_dma.c,v 1.4 1998/05/21 23:38:04 thorpej Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 #include <vm/vm.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_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     64 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     65 
     66 void	mcpcia_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     67 
     68 int	mcpcia_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     69 	    bus_size_t, struct proc *, int));
     70 
     71 int	mcpcia_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     72 	    struct mbuf *, int));
     73 
     74 int	mcpcia_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     75 	    struct uio *, int));
     76 
     77 int	mcpcia_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     78 	    bus_dma_segment_t *, int, bus_size_t, int));
     79 
     80 void	mcpcia_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     81 
     82 /*
     83  * Direct-mapped window: 1G at 2G
     84  *
     85  * XXX Should consider making this 2G at 2G, and creating a 1G at 1G
     86  * XXX SGMAP window for fallback if we have more than 2G of RAM.
     87  */
     88 #define	MCPCIA_DIRECT_MAPPED_BASE	(2UL*1024UL*1024UL*1024UL)
     89 #define	MCPCIA_DIRECT_MAPPED_SIZE	(1UL*1024UL*1024UL*1024UL)
     90 
     91 /*
     92  * SGMAP window for ISA: 8M at 8M
     93  */
     94 #define	MCPCIA_ISA_SG_MAPPED_BASE	(8*1024*1024)
     95 #define	MCPCIA_ISA_SG_MAPPED_SIZE	(8*1024*1024)
     96 
     97 #define	MCPCIA_SGTLB_INVALIDATE(mcp)	\
     98 	alpha_mb(),	\
     99 	REGVAL(MCPCIA_SG_TBIA(mcp)) = 0xdeadbeef, \
    100 	alpha_mb()
    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 = NULL;			/* XXX See above. */
    116 	t->_sgmap = NULL;
    117 	t->_get_tag = mcpcia_dma_get_tag;
    118 	t->_dmamap_create = _bus_dmamap_create;
    119 	t->_dmamap_destroy = _bus_dmamap_destroy;
    120 	t->_dmamap_load = _bus_dmamap_load_direct;
    121 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    122 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    123 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    124 	t->_dmamap_unload = _bus_dmamap_unload;
    125 	t->_dmamap_sync = _bus_dmamap_sync;
    126 
    127 	t->_dmamem_alloc = _bus_dmamem_alloc;
    128 	t->_dmamem_free = _bus_dmamem_free;
    129 	t->_dmamem_map = _bus_dmamem_map;
    130 	t->_dmamem_unmap = _bus_dmamem_unmap;
    131 	t->_dmamem_mmap = _bus_dmamem_mmap;
    132 
    133 	/*
    134 	 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
    135 	 */
    136 	t = &ccp->cc_dmat_sgmap;
    137 	t->_cookie = ccp;
    138 	t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
    139 	t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
    140 	t->_next_window = NULL;
    141 	t->_sgmap = &ccp->cc_sgmap;
    142 	t->_get_tag = mcpcia_dma_get_tag;
    143 	t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
    144 	t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
    145 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    146 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    147 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    148 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    149 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    150 	t->_dmamap_sync = _bus_dmamap_sync;
    151 
    152 	t->_dmamem_alloc = _bus_dmamem_alloc;
    153 	t->_dmamem_free = _bus_dmamem_free;
    154 	t->_dmamem_map = _bus_dmamem_map;
    155 	t->_dmamem_unmap = _bus_dmamem_unmap;
    156 	t->_dmamem_mmap = _bus_dmamem_mmap;
    157 
    158 	/*
    159 	 * The SRM console is supposed to set up an 8MB direct mapped window
    160 	 * at 8MB (window 0) and a 1MB direct mapped window at 1MB. Useless.
    161 	 *
    162 	 * The DU && OpenVMS convention is to have an 8MB S/G window
    163 	 * at 8MB for window 0, a 1GB direct mapped window at 2GB
    164 	 * for window 1, a 1 GB S/G window at window 2 and a 512 MB
    165 	 * S/G window at window 3. This seems overkill.
    166 	 *
    167 	 * We'll just go with the 8MB S/G window and one 1GB direct window.
    168 	 * XXX See comment above.
    169 	 */
    170 
    171 	/*
    172 	 * Initialize the SGMAP.
    173 	 *
    174 	 * Must align page table to its size.
    175 	 */
    176 	alpha_sgmap_init(t, &ccp->cc_sgmap, "mcpcia_sgmap",
    177 	    MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
    178 	    sizeof(u_int64_t), NULL, 0);
    179 
    180 	if (ccp->cc_sgmap.aps_ptpa & (1024-1)) {
    181 		panic("mcpcia_dma_init: bad page table address %x",
    182 			ccp->cc_sgmap.aps_ptpa);
    183 	}
    184 
    185 	/*
    186 	 * Disable windows first.
    187 	 */
    188 
    189 	REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) = 0;
    190 	REGVAL(MCPCIA_W1_BASE(ccp->cc_sc)) = 0;
    191 	REGVAL(MCPCIA_W2_BASE(ccp->cc_sc)) = 0;
    192 	REGVAL(MCPCIA_W3_BASE(ccp->cc_sc)) = 0;
    193 	REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) = 0;
    194 	REGVAL(MCPCIA_T1_BASE(ccp->cc_sc)) = 0;
    195 	REGVAL(MCPCIA_T2_BASE(ccp->cc_sc)) = 0;
    196 	REGVAL(MCPCIA_T3_BASE(ccp->cc_sc)) = 0;
    197 	alpha_mb();
    198 
    199 	/*
    200 	 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
    201 	 */
    202 	REGVAL(MCPCIA_W0_MASK(ccp->cc_sc)) = MCPCIA_WMASK_8M;
    203 	REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) =
    204 		ccp->cc_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    205 	REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) =
    206 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
    207 	alpha_mb();
    208 
    209 	MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
    210 
    211 	/*
    212 	 * Set up window 1 as a 1 GB Direct-mapped window starting at 2GB.
    213 	 */
    214 
    215 	REGVAL(MCPCIA_W0_MASK(ccp->cc_sc)) = MCPCIA_WMASK_1G;
    216 	REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) = 0;
    217 	REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) =
    218 		MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
    219 	alpha_mb();
    220 
    221 	/* XXX XXX BEGIN XXX XXX */
    222 	{							/* XXX */
    223 		extern vm_offset_t alpha_XXX_dmamap_or;		/* XXX */
    224 		alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
    225 	}							/* XXX */
    226 	/* XXX XXX END XXX XXX */
    227 }
    228 
    229 /*
    230  * Return the bus dma tag to be used for the specified bus type.
    231  * INTERNAL USE ONLY!
    232  */
    233 bus_dma_tag_t
    234 mcpcia_dma_get_tag(t, bustype)
    235 	bus_dma_tag_t t;
    236 	alpha_bus_t bustype;
    237 {
    238 	struct mcpcia_config *ccp = t->_cookie;
    239 	extern int physmem;
    240 
    241 	switch (bustype) {
    242 	case ALPHA_BUS_PCI:
    243 	case ALPHA_BUS_EISA:
    244 		/*
    245 		 * Just always use direct-mapped for now; see comment
    246 		 * above about chaining with SGMAPs.
    247 		 */
    248 		return (&ccp->cc_dmat_direct);
    249 
    250 	case ALPHA_BUS_ISA:
    251 		/*
    252 		 * ISA doesn't have enough address bits to use
    253 		 * the direct-mapped DMA window, so we must use
    254 		 * SGMAPs.
    255 		 */
    256 		return (&ccp->cc_dmat_sgmap);
    257 
    258 	default:
    259 		panic("mcpcia_dma_get_tag: shouldn't be here, really...");
    260 	}
    261 }
    262 
    263 /*
    264  * Create a MCPCIA SGMAP-mapped DMA map.
    265  */
    266 int
    267 mcpcia_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    268     flags, dmamp)
    269 	bus_dma_tag_t t;
    270 	bus_size_t size;
    271 	int nsegments;
    272 	bus_size_t maxsegsz;
    273 	bus_size_t boundary;
    274 	int flags;
    275 	bus_dmamap_t *dmamp;
    276 {
    277 	bus_dmamap_t map;
    278 	int error;
    279 
    280 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    281 	    boundary, flags, dmamp);
    282 	if (error)
    283 		return (error);
    284 
    285 	map = *dmamp;
    286 
    287 	if (flags & BUS_DMA_ALLOCNOW) {
    288 		error = alpha_sgmap_alloc(map, round_page(size),
    289 		    t->_sgmap, flags);
    290 		if (error)
    291 			mcpcia_bus_dmamap_destroy_sgmap(t, map);
    292 	}
    293 
    294 	return (error);
    295 }
    296 
    297 /*
    298  * Destroy a MCPCIA SGMAP-mapped DMA map.
    299  */
    300 void
    301 mcpcia_bus_dmamap_destroy_sgmap(t, map)
    302 	bus_dma_tag_t t;
    303 	bus_dmamap_t map;
    304 {
    305 
    306 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    307 		alpha_sgmap_free(map, t->_sgmap);
    308 
    309 	_bus_dmamap_destroy(t, map);
    310 }
    311 
    312 /*
    313  * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
    314  */
    315 int
    316 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    317 	bus_dma_tag_t t;
    318 	bus_dmamap_t map;
    319 	void *buf;
    320 	bus_size_t buflen;
    321 	struct proc *p;
    322 	int flags;
    323 {
    324 	int error;
    325 	struct mcpcia_config *ccp = t->_cookie;
    326 
    327 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    328 	    t->_sgmap);
    329 	if (error == 0)
    330 		MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
    331 	return (error);
    332 }
    333 
    334 /*
    335  * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
    336  */
    337 int
    338 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    339 	bus_dma_tag_t t;
    340 	bus_dmamap_t map;
    341 	struct mbuf *m;
    342 	int flags;
    343 {
    344 	int error;
    345 	struct mcpcia_config *ccp = t->_cookie;
    346 
    347 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    348 	if (error == 0)
    349 		MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
    350 	return (error);
    351 }
    352 
    353 /*
    354  * Load a MCPCIA SGMAP-mapped DMA map with a uio.
    355  */
    356 int
    357 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    358 	bus_dma_tag_t t;
    359 	bus_dmamap_t map;
    360 	struct uio *uio;
    361 	int flags;
    362 {
    363 	int error;
    364 	struct mcpcia_config *ccp = t->_cookie;
    365 
    366 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    367 	if (error == 0)
    368 		MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
    369 	return (error);
    370 }
    371 
    372 /*
    373  * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
    374  */
    375 int
    376 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    377 	bus_dma_tag_t t;
    378 	bus_dmamap_t map;
    379 	bus_dma_segment_t *segs;
    380 	int nsegs;
    381 	bus_size_t size;
    382 	int flags;
    383 {
    384 	int error;
    385 	struct mcpcia_config *ccp = t->_cookie;
    386 
    387 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    388 	    t->_sgmap);
    389 	if (error == 0)
    390 		MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
    391 	return (error);
    392 }
    393 
    394 /*
    395  * Unload a MCPCIA DMA map.
    396  */
    397 void
    398 mcpcia_bus_dmamap_unload_sgmap(t, map)
    399 	bus_dma_tag_t t;
    400 	bus_dmamap_t map;
    401 {
    402 	struct mcpcia_config *ccp = t->_cookie;
    403 
    404 	/*
    405 	 * Invalidate any SGMAP page table entries used by this mapping.
    406 	 */
    407 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    408 	MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
    409 
    410 	/*
    411 	 * Do the generic bits of the unload.
    412 	 */
    413 	_bus_dmamap_unload(t, map);
    414 }
    415