Home | History | Annotate | Line # | Download | only in pci
mcpcia_dma.c revision 1.5
      1 /* $NetBSD: mcpcia_dma.c,v 1.5 1998/06/03 18:25:54 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.5 1998/06/03 18:25:54 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->_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 ISA DMA.
    136 	 */
    137 	t = &ccp->cc_dmat_sgmap;
    138 	t->_cookie = ccp;
    139 	t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
    140 	t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
    141 	t->_next_window = NULL;
    142 	t->_boundary = 0;
    143 	t->_sgmap = &ccp->cc_sgmap;
    144 	t->_get_tag = mcpcia_dma_get_tag;
    145 	t->_dmamap_create = mcpcia_bus_dmamap_create_sgmap;
    146 	t->_dmamap_destroy = mcpcia_bus_dmamap_destroy_sgmap;
    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 	 * The SRM console is supposed to set up an 8MB direct mapped window
    162 	 * at 8MB (window 0) and a 1MB direct mapped window at 1MB. Useless.
    163 	 *
    164 	 * The DU && OpenVMS convention is to have an 8MB S/G window
    165 	 * at 8MB for window 0, a 1GB direct mapped window at 2GB
    166 	 * for window 1, a 1 GB S/G window at window 2 and a 512 MB
    167 	 * S/G window at window 3. This seems overkill.
    168 	 *
    169 	 * We'll just go with the 8MB S/G window and one 1GB direct window.
    170 	 * XXX See comment above.
    171 	 */
    172 
    173 	/*
    174 	 * Initialize the SGMAP.
    175 	 *
    176 	 * Must align page table to its size.
    177 	 */
    178 	alpha_sgmap_init(t, &ccp->cc_sgmap, "mcpcia_sgmap",
    179 	    MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
    180 	    sizeof(u_int64_t), NULL, 0);
    181 
    182 	if (ccp->cc_sgmap.aps_ptpa & (1024-1)) {
    183 		panic("mcpcia_dma_init: bad page table address %x",
    184 			ccp->cc_sgmap.aps_ptpa);
    185 	}
    186 
    187 	/*
    188 	 * Disable windows first.
    189 	 */
    190 
    191 	REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) = 0;
    192 	REGVAL(MCPCIA_W1_BASE(ccp->cc_sc)) = 0;
    193 	REGVAL(MCPCIA_W2_BASE(ccp->cc_sc)) = 0;
    194 	REGVAL(MCPCIA_W3_BASE(ccp->cc_sc)) = 0;
    195 	REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) = 0;
    196 	REGVAL(MCPCIA_T1_BASE(ccp->cc_sc)) = 0;
    197 	REGVAL(MCPCIA_T2_BASE(ccp->cc_sc)) = 0;
    198 	REGVAL(MCPCIA_T3_BASE(ccp->cc_sc)) = 0;
    199 	alpha_mb();
    200 
    201 	/*
    202 	 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
    203 	 */
    204 	REGVAL(MCPCIA_W0_MASK(ccp->cc_sc)) = MCPCIA_WMASK_8M;
    205 	REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) =
    206 		ccp->cc_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    207 	REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) =
    208 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
    209 	alpha_mb();
    210 
    211 	MCPCIA_SGTLB_INVALIDATE(ccp->cc_sc);
    212 
    213 	/*
    214 	 * Set up window 1 as a 1 GB Direct-mapped window starting at 2GB.
    215 	 */
    216 
    217 	REGVAL(MCPCIA_W0_MASK(ccp->cc_sc)) = MCPCIA_WMASK_1G;
    218 	REGVAL(MCPCIA_T0_BASE(ccp->cc_sc)) = 0;
    219 	REGVAL(MCPCIA_W0_BASE(ccp->cc_sc)) =
    220 		MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
    221 	alpha_mb();
    222 
    223 	/* XXX XXX BEGIN XXX XXX */
    224 	{							/* XXX */
    225 		extern vm_offset_t alpha_XXX_dmamap_or;		/* XXX */
    226 		alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */
    227 	}							/* XXX */
    228 	/* XXX XXX END XXX XXX */
    229 }
    230 
    231 /*
    232  * Return the bus dma tag to be used for the specified bus type.
    233  * INTERNAL USE ONLY!
    234  */
    235 bus_dma_tag_t
    236 mcpcia_dma_get_tag(t, bustype)
    237 	bus_dma_tag_t t;
    238 	alpha_bus_t bustype;
    239 {
    240 	struct mcpcia_config *ccp = t->_cookie;
    241 	extern int physmem;
    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->cc_sc);
    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->cc_sc);
    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->cc_sc);
    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->cc_sc);
    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->cc_sc);
    411 
    412 	/*
    413 	 * Do the generic bits of the unload.
    414 	 */
    415 	_bus_dmamap_unload(t, map);
    416 }
    417