Home | History | Annotate | Line # | Download | only in pci
dwlpx_dma.c revision 1.7
      1 /* $NetBSD: dwlpx_dma.c,v 1.7 1998/03/23 07:42:40 mjacob 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.
     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, SPEDWLPxL, 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: dwlpx_dma.c,v 1.7 1998/03/23 07:42:40 mjacob 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/tlsb/tlsbreg.h>
     57 #include <alpha/tlsb/kftxxvar.h>
     58 #include <alpha/tlsb/kftxxreg.h>
     59 #include <alpha/pci/dwlpxreg.h>
     60 #include <alpha/pci/dwlpxvar.h>
     61 #include <alpha/pci/pci_kn8ae.h>
     62 
     63 bus_dma_tag_t dwlpx_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
     64 
     65 int	dwlpx_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     66 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     67 
     68 void	dwlpx_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     69 
     70 int	dwlpx_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
     71 	    bus_size_t, struct proc *, int));
     72 
     73 int	dwlpx_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     74 	    bus_size_t, struct proc *, int));
     75 
     76 int	dwlpx_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
     77 	    struct mbuf *, int));
     78 
     79 int	dwlpx_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     80 	    struct mbuf *, int));
     81 
     82 int	dwlpx_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
     83 	    struct uio *, int));
     84 int	dwlpx_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     85 	    struct uio *, int));
     86 
     87 int	dwlpx_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
     88 	    bus_dma_segment_t *, int, bus_size_t, int));
     89 int	dwlpx_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     90 	    bus_dma_segment_t *, int, bus_size_t, int));
     91 
     92 void	dwlpx_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     93 
     94 #define	DWLPx_DIRECT_MAPPED_BASE	0x80000000
     95 #define	DWLPx_SG_MAPPED_BASE		0x10000000
     96 
     97 void
     98 dwlpx_dma_init(ccp)
     99 	struct dwlpx_config *ccp;
    100 {
    101 	char *exname;
    102 	bus_dma_tag_t t;
    103 	u_int32_t *page_table;
    104 	int i, lim, wmask;
    105 
    106 	/*
    107 	 * Initialize the DMA tag used for direct-mapped DMA.
    108 	 */
    109 	t = &ccp->cc_dmat_direct;
    110 	t->_cookie = ccp;
    111 	t->_get_tag = dwlpx_dma_get_tag;
    112 	t->_dmamap_create = _bus_dmamap_create;
    113 	t->_dmamap_destroy = _bus_dmamap_destroy;
    114 	t->_dmamap_load = dwlpx_bus_dmamap_load_direct;
    115 	t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_direct;
    116 	t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_direct;
    117 	t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_direct;
    118 	t->_dmamap_unload = _bus_dmamap_unload;
    119 	t->_dmamap_sync = _bus_dmamap_sync;
    120 
    121 	t->_dmamem_alloc = _bus_dmamem_alloc;
    122 	t->_dmamem_free = _bus_dmamem_free;
    123 	t->_dmamem_map = _bus_dmamem_map;
    124 	t->_dmamem_unmap = _bus_dmamem_unmap;
    125 	t->_dmamem_mmap = _bus_dmamem_mmap;
    126 
    127 	/*
    128 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    129 	 */
    130 	t = &ccp->cc_dmat_sgmap;
    131 	t->_cookie = ccp;
    132 	t->_get_tag = dwlpx_dma_get_tag;
    133 	t->_dmamap_create = dwlpx_bus_dmamap_create_sgmap;
    134 	t->_dmamap_destroy = dwlpx_bus_dmamap_destroy_sgmap;
    135 	t->_dmamap_load = dwlpx_bus_dmamap_load_sgmap;
    136 	t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_sgmap;
    137 	t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_sgmap;
    138 	t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_sgmap;
    139 	t->_dmamap_unload = dwlpx_bus_dmamap_unload_sgmap;
    140 	t->_dmamap_sync = _bus_dmamap_sync;
    141 
    142 	t->_dmamem_alloc = _bus_dmamem_alloc;
    143 	t->_dmamem_free = _bus_dmamem_free;
    144 	t->_dmamem_map = _bus_dmamem_map;
    145 	t->_dmamem_unmap = _bus_dmamem_unmap;
    146 	t->_dmamem_mmap = _bus_dmamem_mmap;
    147 
    148 	/*
    149 	 * A few notes about SGMAP-mapped DMA on the DWLPx:
    150 	 *
    151 	 * The DWLPx has PCIA-resident SRAM that is used for
    152 	 * the SGMAP page table; there is no TLB.  The DWLPA
    153 	 * has room for 32K entries, yielding a total of 256M
    154 	 * of sgva space.  The DWLPB has 32K entries or 128K
    155 	 * entries, depending on TBIT, yielding either 256M or
    156 	 * 1G of sgva space.
    157 	 *
    158 	 * This sgva space must be shared across all windows
    159 	 * that wish to use SGMAP-mapped DMA; make sure to
    160 	 * adjust the "sgvabase" argument to alpha_sgmap_init()
    161 	 * accordingly if you create more than one SGMAP-mapped
    162 	 * window.  Note that sgvabase != window base.  The former
    163 	 * is used to compute indexes into the page table only.
    164 	 *
    165 	 * In the current implementation the first window is a
    166 	 * 2G window direct-mapped mapped at 2G and the second
    167 	 * window is disabled and the third window is a 256M
    168 	 * or 1GB scatter/gather map at 1GB.
    169 	 */
    170 
    171 	/*
    172 	 * Initialize the page table.
    173 	 */
    174 	page_table =
    175 	    (u_int32_t *)ALPHA_PHYS_TO_K0SEG(PCIA_SGMAP_PT + ccp->cc_sysbase);
    176 	if (ccp->cc_sc->dwlpx_sgmapsz == DWLPX_SG128K) {
    177 		lim = 128 * 1024;
    178 		wmask = PCIA_WMASK_1G;
    179 	} else {
    180 		lim = 32 * 1024;
    181 		wmask = PCIA_WMASK_256M;
    182 	}
    183 	for (i = 0; i < lim; i++)
    184 		page_table[i * SGMAP_PTE_SPACING] = 0;
    185 	alpha_mb();
    186 
    187 	/*
    188 	 * Initialize the SGMAP for window C:
    189 	 *
    190 	 *	Size: 256M or 1GB
    191 	 *	Window base: 1GB
    192 	 *	SGVA base: 0
    193 	 */
    194 	exname = malloc(16, M_DEVBUF, M_NOWAIT);
    195 	if (exname == NULL)
    196 		panic("dwlpx_dma_init");
    197 	sprintf(exname, "%s_sgmap_a", ccp->cc_sc->dwlpx_dev.dv_xname);
    198 	alpha_sgmap_init(t, &ccp->cc_sgmap, exname, DWLPx_SG_MAPPED_BASE,
    199 	    0, lim * NBPG, sizeof(u_int32_t), (void *)page_table, 0);
    200 
    201 	/*
    202 	 * Set up DMA windows for this DWLPx.
    203 	 *
    204 	 * Do this even for all HPCs- even for the nonexistent
    205 	 * one on hose zero of a KFTIA.
    206 	 */
    207 	for (i = 0; i < NHPC; i++) {
    208 		REGVAL(PCIA_WMASK_A(i) + ccp->cc_sysbase) = PCIA_WMASK_2G;
    209 		REGVAL(PCIA_TBASE_A(i) + ccp->cc_sysbase) = 0;
    210 		REGVAL(PCIA_WBASE_A(i) + ccp->cc_sysbase) =
    211 		    DWLPx_DIRECT_MAPPED_BASE | PCIA_WBASE_W_EN;
    212 
    213 		REGVAL(PCIA_WMASK_B(i) + ccp->cc_sysbase) = 0;
    214 		REGVAL(PCIA_TBASE_B(i) + ccp->cc_sysbase) = 0;
    215 		REGVAL(PCIA_WBASE_B(i) + ccp->cc_sysbase) = 0;
    216 
    217 		REGVAL(PCIA_WMASK_C(i) + ccp->cc_sysbase) = wmask;
    218 		REGVAL(PCIA_TBASE_C(i) + ccp->cc_sysbase) = 0;
    219 		REGVAL(PCIA_WBASE_C(i) + ccp->cc_sysbase) =
    220 		    DWLPx_SG_MAPPED_BASE | PCIA_WBASE_W_EN | PCIA_WBASE_SG_EN;
    221 		alpha_mb();
    222 	}
    223 
    224 	/* XXX XXX BEGIN XXX XXX */
    225 	{							/* XXX */
    226 		extern vm_offset_t alpha_XXX_dmamap_or;		/* XXX */
    227 		alpha_XXX_dmamap_or = DWLPx_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 dwlpx_dma_get_tag(t, bustype)
    238 	bus_dma_tag_t t;
    239 	alpha_bus_t bustype;
    240 {
    241 	struct dwlpx_config *ccp = t->_cookie;
    242 	extern int physmem;
    243 
    244 	switch (bustype) {
    245 	case ALPHA_BUS_PCI:
    246 	case ALPHA_BUS_EISA:
    247 		/*
    248 		 * As best as I can tell from the DU source, you can't
    249 		 * do DIRECT MAPPED and S/G at the same time.
    250 		 */
    251 #ifndef	MSS3_DEBUG_SG
    252 		if (physmem <= btoc(2048LL << 20LL))
    253 			return (&ccp->cc_dmat_direct);
    254 #endif
    255 		/* FALLTHROUGH */
    256 
    257 	case ALPHA_BUS_ISA:
    258 		/*
    259 		 * ISA doesn't have enough address bits to use
    260 		 * the direct-mapped DMA window, so we must use
    261 		 * SGMAPs.
    262 		 */
    263 		return (&ccp->cc_dmat_sgmap);
    264 
    265 	default:
    266 		panic("dwlpx_dma_get_tag: shouldn't be here, really...");
    267 	}
    268 }
    269 
    270 /*
    271  * Create a DWLPx SGMAP-mapped DMA map.
    272  */
    273 int
    274 dwlpx_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    275     flags, dmamp)
    276 	bus_dma_tag_t t;
    277 	bus_size_t size;
    278 	int nsegments;
    279 	bus_size_t maxsegsz;
    280 	bus_size_t boundary;
    281 	int flags;
    282 	bus_dmamap_t *dmamp;
    283 {
    284 	struct dwlpx_config *ccp = t->_cookie;
    285 	bus_dmamap_t map;
    286 	int error;
    287 
    288 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    289 	    boundary, flags, dmamp);
    290 	if (error)
    291 		return (error);
    292 
    293 	map = *dmamp;
    294 
    295 	if (flags & BUS_DMA_ALLOCNOW) {
    296 		error = alpha_sgmap_alloc(map, round_page(size),
    297 		    &ccp->cc_sgmap, flags);
    298 		if (error)
    299 			dwlpx_bus_dmamap_destroy_sgmap(t, map);
    300 	}
    301 
    302 	return (error);
    303 }
    304 
    305 /*
    306  * Destroy a DWLPx SGMAP-mapped DMA map.
    307  */
    308 void
    309 dwlpx_bus_dmamap_destroy_sgmap(t, map)
    310 	bus_dma_tag_t t;
    311 	bus_dmamap_t map;
    312 {
    313 	struct dwlpx_config *ccp = t->_cookie;
    314 
    315 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    316 		alpha_sgmap_free(map, &ccp->cc_sgmap);
    317 
    318 	_bus_dmamap_destroy(t, map);
    319 }
    320 
    321 /*
    322  * Load a DWLPx direct-mapped DMA map with a linear buffer.
    323  */
    324 int
    325 dwlpx_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
    326 	bus_dma_tag_t t;
    327 	bus_dmamap_t map;
    328 	void *buf;
    329 	bus_size_t buflen;
    330 	struct proc *p;
    331 	int flags;
    332 {
    333 	return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
    334 	    flags, DWLPx_DIRECT_MAPPED_BASE));
    335 }
    336 
    337 /*
    338  * Load a DWLPx SGMAP-mapped DMA map with a linear buffer.
    339  */
    340 int
    341 dwlpx_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    342 	bus_dma_tag_t t;
    343 	bus_dmamap_t map;
    344 	void *buf;
    345 	bus_size_t buflen;
    346 	struct proc *p;
    347 	int flags;
    348 {
    349 	struct dwlpx_config *ccp = t->_cookie;
    350 
    351 	return (pci_sgmap_pte32_load(t, map, buf, buflen, p, flags,
    352 	    &ccp->cc_sgmap));
    353 }
    354 
    355 /*
    356  * Load a DWLPx direct-mapped DMA map with an mbuf chain.
    357  */
    358 int
    359 dwlpx_bus_dmamap_load_mbuf_direct(t, map, m, flags)
    360 	bus_dma_tag_t t;
    361 	bus_dmamap_t map;
    362 	struct mbuf *m;
    363 	int flags;
    364 {
    365 
    366 	return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
    367 	    flags, DWLPx_DIRECT_MAPPED_BASE));
    368 }
    369 
    370 /*
    371  * Load a DWLPx SGMAP-mapped DMA map with an mbuf chain.
    372  */
    373 int
    374 dwlpx_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    375 	bus_dma_tag_t t;
    376 	bus_dmamap_t map;
    377 	struct mbuf *m;
    378 	int flags;
    379 {
    380 	struct dwlpx_config *ccp = t->_cookie;
    381 
    382 	return (pci_sgmap_pte32_load_mbuf(t, map, m, flags, &ccp->cc_sgmap));
    383 }
    384 
    385 /*
    386  * Load a DWLPx direct-mapped DMA map with a uio.
    387  */
    388 int
    389 dwlpx_bus_dmamap_load_uio_direct(t, map, uio, flags)
    390 	bus_dma_tag_t t;
    391 	bus_dmamap_t map;
    392 	struct uio *uio;
    393 	int flags;
    394 {
    395 
    396 	return (_bus_dmamap_load_uio_direct_common(t, map, uio,
    397 	    flags, DWLPx_DIRECT_MAPPED_BASE));
    398 }
    399 
    400 /*
    401  * Load a DWLPx SGMAP-mapped DMA map with a uio.
    402  */
    403 int
    404 dwlpx_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    405 	bus_dma_tag_t t;
    406 	bus_dmamap_t map;
    407 	struct uio *uio;
    408 	int flags;
    409 {
    410 	struct dwlpx_config *ccp = t->_cookie;
    411 
    412 	return (pci_sgmap_pte32_load_uio(t, map, uio, flags, &ccp->cc_sgmap));
    413 }
    414 
    415 /*
    416  * Load a DWLPx direct-mapped DMA map with raw memory.
    417  */
    418 int
    419 dwlpx_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
    420 	bus_dma_tag_t t;
    421 	bus_dmamap_t map;
    422 	bus_dma_segment_t *segs;
    423 	int nsegs;
    424 	bus_size_t size;
    425 	int flags;
    426 {
    427 
    428 	return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
    429 	    size, flags, DWLPx_DIRECT_MAPPED_BASE));
    430 }
    431 
    432 /*
    433  * Load a DWLPx SGMAP-mapped DMA map with raw memory.
    434  */
    435 int
    436 dwlpx_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    437 	bus_dma_tag_t t;
    438 	bus_dmamap_t map;
    439 	bus_dma_segment_t *segs;
    440 	int nsegs;
    441 	bus_size_t size;
    442 	int flags;
    443 {
    444 	struct dwlpx_config *ccp = t->_cookie;
    445 
    446 	return (pci_sgmap_pte32_load_raw(t, map, segs, nsegs, size, flags,
    447 	    &ccp->cc_sgmap));
    448 }
    449 
    450 /*
    451  * Unload a DWLPx DMA map.
    452  */
    453 void
    454 dwlpx_bus_dmamap_unload_sgmap(t, map)
    455 	bus_dma_tag_t t;
    456 	bus_dmamap_t map;
    457 {
    458 	struct dwlpx_config *ccp = t->_cookie;
    459 
    460 	/*
    461 	 * Invalidate any SGMAP page table entries used by this
    462 	 * mapping.
    463 	 */
    464 	pci_sgmap_pte32_unload(t, map, &ccp->cc_sgmap);
    465 
    466 	/*
    467 	 * Do the generic bits of the unload.
    468 	 */
    469 	_bus_dmamap_unload(t, map);
    470 }
    471