Home | History | Annotate | Line # | Download | only in pci
dwlpx_dma.c revision 1.5
      1 /* $NetBSD: dwlpx_dma.c,v 1.5 1998/01/17 21:53:58 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.
     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.5 1998/01/17 21:53:58 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/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 int	dwlpx_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     73 	    bus_size_t, struct proc *, int));
     74 
     75 int	dwlpx_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
     76 	    struct mbuf *, int));
     77 int	dwlpx_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     78 	    struct mbuf *, int));
     79 
     80 int	dwlpx_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
     81 	    struct uio *, int));
     82 int	dwlpx_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     83 	    struct uio *, int));
     84 
     85 int	dwlpx_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
     86 	    bus_dma_segment_t *, int, bus_size_t, int));
     87 int	dwlpx_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     88 	    bus_dma_segment_t *, int, bus_size_t, int));
     89 
     90 void	dwlpx_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     91 
     92 /*
     93  * The direct-mapped DMA window begins at this PCI address.
     94  */
     95 #define	DWLPx_DIRECT_MAPPED_BASE 0x40000000
     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;
    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 = NULL;		/* Nothing to do. */
    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 = NULL;		/* Nothing to do. */
    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 wither 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, we follow the lead of
    166 	 * the workstation chipsets; the first window is an 8M
    167 	 * window SGMAP-mapped mapped at 8M, and the second window
    168 	 * is a 1G window direct-mapped mapped at 1G.
    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 	for (i = 0; i < (32*1024); i++)
    177 		page_table[i] = 0;
    178 	alpha_mb();
    179 
    180 	/*
    181 	 * Initialize the SGMAP for window A:
    182 	 *
    183 	 *	Size: 8M
    184 	 *	Window base: 8M
    185 	 *	SGVA base: 0
    186 	 */
    187 	exname = malloc(16, M_DEVBUF, M_NOWAIT);
    188 	if (exname == NULL)
    189 		panic("dwlpx_dma_init");
    190 	sprintf(exname, "%s_sgmap_a", ccp->cc_sc->dwlpx_dev.dv_xname);
    191 	alpha_sgmap_init(t, &ccp->cc_sgmap, exname,
    192 	    (8*1024*1024), 0, (8*1024*1024), sizeof(u_int32_t),
    193 	    (void *)page_table, 0);
    194 
    195 	/*
    196 	 * Set up DMA windows for this DWLPx.
    197 	 *
    198 	 * Do this even for all HPCs- even for the nonexistent
    199 	 * one on hose zero of a KFTIA.
    200 	 */
    201 	for (i = 0; i < NHPC; i++) {
    202 		REGVAL(PCIA_WMASK_A(i) + ccp->cc_sysbase) = PCIA_WMASK_8M;
    203 		REGVAL(PCIA_TBASE_A(i) + ccp->cc_sysbase) = 0;
    204 		alpha_mb();
    205 		REGVAL(PCIA_WBASE_A(i) + ccp->cc_sysbase) =
    206 		    (8*1024*1024) | PCIA_WBASE_W_EN | PCIA_WBASE_SG_EN;
    207 		alpha_mb();
    208 
    209 		REGVAL(PCIA_WMASK_B(i) + ccp->cc_sysbase) = PCIA_WMASK_1G;
    210 		REGVAL(PCIA_TBASE_B(i) + ccp->cc_sysbase) = 0;
    211 		alpha_mb();
    212 		REGVAL(PCIA_WBASE_B(i) + ccp->cc_sysbase) =
    213 		    DWLPx_DIRECT_MAPPED_BASE | PCIA_WBASE_W_EN;
    214 		alpha_mb();
    215 
    216 		REGVAL(PCIA_WMASK_C(i) + ccp->cc_sysbase) = 0;
    217 		REGVAL(PCIA_TBASE_C(i) + ccp->cc_sysbase) = 0;
    218 		alpha_mb();
    219 		REGVAL(PCIA_WBASE_C(i) + ccp->cc_sysbase) = 0;
    220 		alpha_mb();
    221 	}
    222 
    223 	/* XXX XXX BEGIN XXX XXX */
    224 	{							/* XXX */
    225 		extern vm_offset_t alpha_XXX_dmamap_or;		/* XXX */
    226 		alpha_XXX_dmamap_or = DWLPx_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 dwlpx_dma_get_tag(t, bustype)
    237 	bus_dma_tag_t t;
    238 	alpha_bus_t bustype;
    239 {
    240 	struct dwlpx_config *ccp = t->_cookie;
    241 
    242 	switch (bustype) {
    243 	case ALPHA_BUS_PCI:
    244 	case ALPHA_BUS_EISA:
    245 		/*
    246 		 * XXX FIXME!
    247 		 * XXX If the system has more than 1G of RAM,
    248 		 * XXX we need to use SGMAPs, or some combination
    249 		 * XXX of direct-mapped and SGMAP-mapped DMA.
    250 		 */
    251 		return (&ccp->cc_dmat_direct);
    252 
    253 	case ALPHA_BUS_ISA:
    254 		/*
    255 		 * ISA doesn't have enough address bits to use
    256 		 * the direct-mapped DMA window, so we must use
    257 		 * SGMAPs.
    258 		 */
    259 		return (&ccp->cc_dmat_sgmap);
    260 
    261 	default:
    262 		panic("dwlpx_dma_get_tag: shouldn't be here, really...");
    263 	}
    264 }
    265 
    266 /*
    267  * Create a DWLPx SGMAP-mapped DMA map.
    268  */
    269 int
    270 dwlpx_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    271     flags, dmamp)
    272 	bus_dma_tag_t t;
    273 	bus_size_t size;
    274 	int nsegments;
    275 	bus_size_t maxsegsz;
    276 	bus_size_t boundary;
    277 	int flags;
    278 	bus_dmamap_t *dmamp;
    279 {
    280 	struct dwlpx_config *ccp = t->_cookie;
    281 	bus_dmamap_t map;
    282 	int error;
    283 
    284 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    285 	    boundary, flags, dmamp);
    286 	if (error)
    287 		return (error);
    288 
    289 	map = *dmamp;
    290 
    291 	if (flags & BUS_DMA_ALLOCNOW) {
    292 		error = alpha_sgmap_alloc(map, round_page(size),
    293 		    &ccp->cc_sgmap, flags);
    294 		if (error)
    295 			dwlpx_bus_dmamap_destroy_sgmap(t, map);
    296 	}
    297 
    298 	return (error);
    299 }
    300 
    301 /*
    302  * Destroy a DWLPx SGMAP-mapped DMA map.
    303  */
    304 void
    305 dwlpx_bus_dmamap_destroy_sgmap(t, map)
    306 	bus_dma_tag_t t;
    307 	bus_dmamap_t map;
    308 {
    309 	struct dwlpx_config *ccp = t->_cookie;
    310 
    311 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    312 		alpha_sgmap_free(map, &ccp->cc_sgmap);
    313 
    314 	_bus_dmamap_destroy(t, map);
    315 }
    316 
    317 /*
    318  * Load a DWLPx direct-mapped DMA map with a linear buffer.
    319  */
    320 int
    321 dwlpx_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
    322 	bus_dma_tag_t t;
    323 	bus_dmamap_t map;
    324 	void *buf;
    325 	bus_size_t buflen;
    326 	struct proc *p;
    327 	int flags;
    328 {
    329 
    330 	return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
    331 	    flags, DWLPx_DIRECT_MAPPED_BASE));
    332 }
    333 
    334 /*
    335  * Load a DWLPx SGMAP-mapped DMA map with a linear buffer.
    336  */
    337 int
    338 dwlpx_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    339 	bus_dma_tag_t t;
    340 	bus_dmamap_t map;
    341 	void *buf;
    342 	bus_size_t buflen;
    343 	struct proc *p;
    344 	int flags;
    345 {
    346 	struct dwlpx_config *ccp = t->_cookie;
    347 
    348 	return (pci_sgmap_pte32_load(t, map, buf, buflen, p, flags,
    349 	    &ccp->cc_sgmap));
    350 }
    351 
    352 /*
    353  * Load a DWLPx direct-mapped DMA map with an mbuf chain.
    354  */
    355 int
    356 dwlpx_bus_dmamap_load_mbuf_direct(t, map, m, flags)
    357 	bus_dma_tag_t t;
    358 	bus_dmamap_t map;
    359 	struct mbuf *m;
    360 	int flags;
    361 {
    362 
    363 	return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
    364 	    flags, DWLPx_DIRECT_MAPPED_BASE));
    365 }
    366 
    367 /*
    368  * Load a DWLPx SGMAP-mapped DMA map with an mbuf chain.
    369  */
    370 int
    371 dwlpx_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    372 	bus_dma_tag_t t;
    373 	bus_dmamap_t map;
    374 	struct mbuf *m;
    375 	int flags;
    376 {
    377 	struct dwlpx_config *ccp = t->_cookie;
    378 
    379 	return (pci_sgmap_pte32_load_mbuf(t, map, m, flags, &ccp->cc_sgmap));
    380 }
    381 
    382 /*
    383  * Load a DWLPx direct-mapped DMA map with a uio.
    384  */
    385 int
    386 dwlpx_bus_dmamap_load_uio_direct(t, map, uio, flags)
    387 	bus_dma_tag_t t;
    388 	bus_dmamap_t map;
    389 	struct uio *uio;
    390 	int flags;
    391 {
    392 
    393 	return (_bus_dmamap_load_uio_direct_common(t, map, uio,
    394 	    flags, DWLPx_DIRECT_MAPPED_BASE));
    395 }
    396 
    397 /*
    398  * Load a DWLPx SGMAP-mapped DMA map with a uio.
    399  */
    400 int
    401 dwlpx_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    402 	bus_dma_tag_t t;
    403 	bus_dmamap_t map;
    404 	struct uio *uio;
    405 	int flags;
    406 {
    407 	struct dwlpx_config *ccp = t->_cookie;
    408 
    409 	return (pci_sgmap_pte32_load_uio(t, map, uio, flags, &ccp->cc_sgmap));
    410 }
    411 
    412 /*
    413  * Load a DWLPx direct-mapped DMA map with raw memory.
    414  */
    415 int
    416 dwlpx_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
    417 	bus_dma_tag_t t;
    418 	bus_dmamap_t map;
    419 	bus_dma_segment_t *segs;
    420 	int nsegs;
    421 	bus_size_t size;
    422 	int flags;
    423 {
    424 
    425 	return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
    426 	    size, flags, DWLPx_DIRECT_MAPPED_BASE));
    427 }
    428 
    429 /*
    430  * Load a DWLPx SGMAP-mapped DMA map with raw memory.
    431  */
    432 int
    433 dwlpx_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    434 	bus_dma_tag_t t;
    435 	bus_dmamap_t map;
    436 	bus_dma_segment_t *segs;
    437 	int nsegs;
    438 	bus_size_t size;
    439 	int flags;
    440 {
    441 	struct dwlpx_config *ccp = t->_cookie;
    442 
    443 	return (pci_sgmap_pte32_load_raw(t, map, segs, nsegs, size, flags,
    444 	    &ccp->cc_sgmap));
    445 }
    446 
    447 /*
    448  * Unload a DWLPx DMA map.
    449  */
    450 void
    451 dwlpx_bus_dmamap_unload_sgmap(t, map)
    452 	bus_dma_tag_t t;
    453 	bus_dmamap_t map;
    454 {
    455 	struct dwlpx_config *ccp = t->_cookie;
    456 
    457 	/*
    458 	 * Invalidate any SGMAP page table entries used by this
    459 	 * mapping.
    460 	 */
    461 	pci_sgmap_pte32_unload(t, map, &ccp->cc_sgmap);
    462 
    463 	/*
    464 	 * Do the generic bits of the unload.
    465 	 */
    466 	_bus_dmamap_unload(t, map);
    467 }
    468