Home | History | Annotate | Line # | Download | only in pci
mcpcia_dma.c revision 1.11
      1 /* $NetBSD: mcpcia_dma.c,v 1.11 1999/04/15 22:32:21 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 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.11 1999/04/15 22:32:21 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: 1G at 2G
     83  *
     84  * XXX Should consider making this 2G at 2G, and creating a 1G at 1G
     85  * XXX SGMAP window for fallback if we have more than 2G of RAM.
     86  */
     87 #define	MCPCIA_DIRECT_MAPPED_BASE	(2UL*1024UL*1024UL*1024UL)
     88 #define	MCPCIA_DIRECT_MAPPED_SIZE	(1UL*1024UL*1024UL*1024UL)
     89 
     90 /*
     91  * SGMAP window for ISA: 8M at 8M
     92  */
     93 #define	MCPCIA_ISA_SG_MAPPED_BASE	(8*1024*1024)
     94 #define	MCPCIA_ISA_SG_MAPPED_SIZE	(8*1024*1024)
     95 
     96 #define	MCPCIA_SGTLB_INVALIDATE(ccp)					\
     97 do {									\
     98 	alpha_mb();							\
     99 	REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef;			\
    100 	alpha_mb();							\
    101 } while (0)
    102 
    103 void
    104 mcpcia_dma_init(ccp)
    105 	struct mcpcia_config *ccp;
    106 {
    107 	bus_dma_tag_t t;
    108 
    109 	/*
    110 	 * Initialize the DMA tag used for direct-mapped DMA.
    111 	 */
    112 	t = &ccp->cc_dmat_direct;
    113 	t->_cookie = ccp;
    114 	t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
    115 	t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE;
    116 	t->_next_window = NULL;			/* XXX See above. */
    117 	t->_boundary = 0;
    118 	t->_sgmap = NULL;
    119 	t->_get_tag = mcpcia_dma_get_tag;
    120 	t->_dmamap_create = _bus_dmamap_create;
    121 	t->_dmamap_destroy = _bus_dmamap_destroy;
    122 	t->_dmamap_load = _bus_dmamap_load_direct;
    123 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    124 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    125 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    126 	t->_dmamap_unload = _bus_dmamap_unload;
    127 	t->_dmamap_sync = _bus_dmamap_sync;
    128 
    129 	t->_dmamem_alloc = _bus_dmamem_alloc;
    130 	t->_dmamem_free = _bus_dmamem_free;
    131 	t->_dmamem_map = _bus_dmamem_map;
    132 	t->_dmamem_unmap = _bus_dmamem_unmap;
    133 	t->_dmamem_mmap = _bus_dmamem_mmap;
    134 
    135 	/*
    136 	 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
    137 	 */
    138 	t = &ccp->cc_dmat_sgmap;
    139 	t->_cookie = ccp;
    140 	t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
    141 	t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
    142 	t->_next_window = NULL;
    143 	t->_boundary = 0;
    144 	t->_sgmap = &ccp->cc_sgmap;
    145 	t->_get_tag = mcpcia_dma_get_tag;
    146 	t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
    147 	t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
    148 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    149 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    150 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    151 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    152 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    153 	t->_dmamap_sync = _bus_dmamap_sync;
    154 
    155 	t->_dmamem_alloc = _bus_dmamem_alloc;
    156 	t->_dmamem_free = _bus_dmamem_free;
    157 	t->_dmamem_map = _bus_dmamem_map;
    158 	t->_dmamem_unmap = _bus_dmamem_unmap;
    159 	t->_dmamem_mmap = _bus_dmamem_mmap;
    160 
    161 	/*
    162 	 * The SRM console is supposed to set up an 8MB direct mapped window
    163 	 * at 8MB (window 0) and a 1MB direct mapped window at 1MB. Useless.
    164 	 *
    165 	 * The DU && OpenVMS convention is to have an 8MB S/G window
    166 	 * at 8MB for window 0, a 1GB direct mapped window at 2GB
    167 	 * for window 1, a 1 GB S/G window at window 2 and a 512 MB
    168 	 * S/G window at window 3. This seems overkill.
    169 	 *
    170 	 * We'll just go with the 8MB S/G window and one 1GB direct window.
    171 	 * XXX See comment above.
    172 	 */
    173 
    174 	/*
    175 	 * Initialize the SGMAP.
    176 	 *
    177 	 * Must align page table to its size.
    178 	 */
    179 	alpha_sgmap_init(t, &ccp->cc_sgmap, "mcpcia_sgmap",
    180 	    MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
    181 	    sizeof(u_int64_t), NULL, 0);
    182 
    183 	if (ccp->cc_sgmap.aps_ptpa & (1024-1)) {
    184 		panic("mcpcia_dma_init: bad page table address 0x%lx",
    185 			ccp->cc_sgmap.aps_ptpa);
    186 	}
    187 
    188 	/*
    189 	 * Disable windows first.
    190 	 */
    191 
    192 	REGVAL(MCPCIA_W0_BASE(ccp)) = 0;
    193 	REGVAL(MCPCIA_W1_BASE(ccp)) = 0;
    194 	REGVAL(MCPCIA_W2_BASE(ccp)) = 0;
    195 	REGVAL(MCPCIA_W3_BASE(ccp)) = 0;
    196 	REGVAL(MCPCIA_T0_BASE(ccp)) = 0;
    197 	REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
    198 	REGVAL(MCPCIA_T2_BASE(ccp)) = 0;
    199 	REGVAL(MCPCIA_T3_BASE(ccp)) = 0;
    200 	alpha_mb();
    201 
    202 	/*
    203 	 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
    204 	 */
    205 	REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_8M;
    206 	REGVAL(MCPCIA_T0_BASE(ccp)) =
    207 		ccp->cc_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    208 	REGVAL(MCPCIA_W0_BASE(ccp)) =
    209 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
    210 	alpha_mb();
    211 
    212 	MCPCIA_SGTLB_INVALIDATE(ccp);
    213 
    214 	/*
    215 	 * Set up window 1 as a 1 GB Direct-mapped window starting at 2GB.
    216 	 */
    217 
    218 	REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_1G;
    219 	REGVAL(MCPCIA_T0_BASE(ccp)) = 0;
    220 	REGVAL(MCPCIA_W0_BASE(ccp)) =
    221 		MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
    222 	alpha_mb();
    223 
    224 	/* XXX XXX BEGIN XXX XXX */
    225 	{							/* XXX */
    226 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
    227 		alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
    228 	}							/* XXX */
    229 	/* XXX XXX END XXX XXX */
    230 }
    231 
    232 /*
    233  * Return the bus dma tag to be used for the specified bus type.
    234  * INTERNAL USE ONLY!
    235  */
    236 bus_dma_tag_t
    237 mcpcia_dma_get_tag(t, bustype)
    238 	bus_dma_tag_t t;
    239 	alpha_bus_t bustype;
    240 {
    241 	struct mcpcia_config *ccp = t->_cookie;
    242 
    243 	switch (bustype) {
    244 	case ALPHA_BUS_PCI:
    245 	case ALPHA_BUS_EISA:
    246 		/*
    247 		 * Just always use direct-mapped for now; see comment
    248 		 * above about chaining with SGMAPs.
    249 		 */
    250 		return (&ccp->cc_dmat_direct);
    251 
    252 	case ALPHA_BUS_ISA:
    253 		/*
    254 		 * ISA doesn't have enough address bits to use
    255 		 * the direct-mapped DMA window, so we must use
    256 		 * SGMAPs.
    257 		 */
    258 		return (&ccp->cc_dmat_sgmap);
    259 
    260 	default:
    261 		panic("mcpcia_dma_get_tag: shouldn't be here, really...");
    262 	}
    263 }
    264 
    265 /*
    266  * Create a MCPCIA SGMAP-mapped DMA map.
    267  */
    268 int
    269 mcpcia_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    270     flags, dmamp)
    271 	bus_dma_tag_t t;
    272 	bus_size_t size;
    273 	int nsegments;
    274 	bus_size_t maxsegsz;
    275 	bus_size_t boundary;
    276 	int flags;
    277 	bus_dmamap_t *dmamp;
    278 {
    279 	bus_dmamap_t map;
    280 	int error;
    281 
    282 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    283 	    boundary, flags, dmamp);
    284 	if (error)
    285 		return (error);
    286 
    287 	map = *dmamp;
    288 
    289 	if (flags & BUS_DMA_ALLOCNOW) {
    290 		error = alpha_sgmap_alloc(map, round_page(size),
    291 		    t->_sgmap, flags);
    292 		if (error)
    293 			mcpcia_bus_dmamap_destroy_sgmap(t, map);
    294 	}
    295 
    296 	return (error);
    297 }
    298 
    299 /*
    300  * Destroy a MCPCIA SGMAP-mapped DMA map.
    301  */
    302 void
    303 mcpcia_bus_dmamap_destroy_sgmap(t, map)
    304 	bus_dma_tag_t t;
    305 	bus_dmamap_t map;
    306 {
    307 
    308 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    309 		alpha_sgmap_free(map, t->_sgmap);
    310 
    311 	_bus_dmamap_destroy(t, map);
    312 }
    313 
    314 /*
    315  * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
    316  */
    317 int
    318 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    319 	bus_dma_tag_t t;
    320 	bus_dmamap_t map;
    321 	void *buf;
    322 	bus_size_t buflen;
    323 	struct proc *p;
    324 	int flags;
    325 {
    326 	int error;
    327 	struct mcpcia_config *ccp = t->_cookie;
    328 
    329 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    330 	    t->_sgmap);
    331 	if (error == 0)
    332 		MCPCIA_SGTLB_INVALIDATE(ccp);
    333 	return (error);
    334 }
    335 
    336 /*
    337  * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
    338  */
    339 int
    340 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    341 	bus_dma_tag_t t;
    342 	bus_dmamap_t map;
    343 	struct mbuf *m;
    344 	int flags;
    345 {
    346 	int error;
    347 	struct mcpcia_config *ccp = t->_cookie;
    348 
    349 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    350 	if (error == 0)
    351 		MCPCIA_SGTLB_INVALIDATE(ccp);
    352 	return (error);
    353 }
    354 
    355 /*
    356  * Load a MCPCIA SGMAP-mapped DMA map with a uio.
    357  */
    358 int
    359 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    360 	bus_dma_tag_t t;
    361 	bus_dmamap_t map;
    362 	struct uio *uio;
    363 	int flags;
    364 {
    365 	int error;
    366 	struct mcpcia_config *ccp = t->_cookie;
    367 
    368 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    369 	if (error == 0)
    370 		MCPCIA_SGTLB_INVALIDATE(ccp);
    371 	return (error);
    372 }
    373 
    374 /*
    375  * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
    376  */
    377 int
    378 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    379 	bus_dma_tag_t t;
    380 	bus_dmamap_t map;
    381 	bus_dma_segment_t *segs;
    382 	int nsegs;
    383 	bus_size_t size;
    384 	int flags;
    385 {
    386 	int error;
    387 	struct mcpcia_config *ccp = t->_cookie;
    388 
    389 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    390 	    t->_sgmap);
    391 	if (error == 0)
    392 		MCPCIA_SGTLB_INVALIDATE(ccp);
    393 	return (error);
    394 }
    395 
    396 /*
    397  * Unload a MCPCIA DMA map.
    398  */
    399 void
    400 mcpcia_bus_dmamap_unload_sgmap(t, map)
    401 	bus_dma_tag_t t;
    402 	bus_dmamap_t map;
    403 {
    404 	struct mcpcia_config *ccp = t->_cookie;
    405 
    406 	/*
    407 	 * Invalidate any SGMAP page table entries used by this mapping.
    408 	 */
    409 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    410 	MCPCIA_SGTLB_INVALIDATE(ccp);
    411 
    412 	/*
    413 	 * Do the generic bits of the unload.
    414 	 */
    415 	_bus_dmamap_unload(t, map);
    416 }
    417