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