Home | History | Annotate | Line # | Download | only in pci
dwlpx_dma.c revision 1.16.124.1
      1 /* $NetBSD: dwlpx_dma.c,v 1.16.124.1 2008/06/02 13:21:47 mjf 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34 
     35 __KERNEL_RCSID(0, "$NetBSD: dwlpx_dma.c,v 1.16.124.1 2008/06/02 13:21:47 mjf Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #define _ALPHA_BUS_DMA_PRIVATE
     46 #include <machine/bus.h>
     47 
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcivar.h>
     50 #include <alpha/tlsb/tlsbreg.h>
     51 #include <alpha/tlsb/kftxxvar.h>
     52 #include <alpha/tlsb/kftxxreg.h>
     53 #include <alpha/pci/dwlpxreg.h>
     54 #include <alpha/pci/dwlpxvar.h>
     55 #include <alpha/pci/pci_kn8ae.h>
     56 
     57 bus_dma_tag_t dwlpx_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
     58 
     59 int	dwlpx_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     60 	    bus_size_t, struct proc *, int));
     61 
     62 int	dwlpx_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     63 	    struct mbuf *, int));
     64 
     65 int	dwlpx_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     66 	    struct uio *, int));
     67 
     68 int	dwlpx_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     69 	    bus_dma_segment_t *, int, bus_size_t, int));
     70 
     71 void	dwlpx_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     72 
     73 /*
     74  * Direct-mapped window: 2G at 2G
     75  */
     76 #define	DWLPx_DIRECT_MAPPED_BASE	(2UL*1024UL*1024UL*1024UL)
     77 #define	DWLPx_DIRECT_MAPPED_SIZE	(2UL*1024UL*1024UL*1024UL)
     78 
     79 /*
     80  * SGMAP window A: 256M or 1G at 1G
     81  */
     82 #define	DWLPx_SG_MAPPED_BASE		(1*1024*1024*1024)
     83 #define	DWLPx_SG_MAPPED_SIZE(x)		((x) * PAGE_SIZE)
     84 
     85 /* DWLPx has a 256-byte out-bound DMA prefetch threshold. */
     86 #define	DWLPx_SG_MAPPED_PFTHRESH	256
     87 
     88 /*
     89  * Set this to always use SGMAPs.
     90  */
     91 #ifdef DWLPX_ALWAYS_USE_SGMAP
     92 int	dwlpx_always_use_sgmap = 1;
     93 #else
     94 int	dwlpx_always_use_sgmap = 0;
     95 #endif
     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 	 * Determine size of Window C based on the amount of SGMAP
    108 	 * page table SRAM available.
    109 	 */
    110 	if (ccp->cc_sc->dwlpx_sgmapsz == DWLPX_SG128K) {
    111 		lim = 128 * 1024;
    112 		wmask = PCIA_WMASK_1G;
    113 	} else {
    114 		lim = 32 * 1024;
    115 		wmask = PCIA_WMASK_256M;
    116 	}
    117 
    118 	/*
    119 	 * Initialize the DMA tag used for direct-mapped DMA.  Chain
    120 	 * this window to the SGMAP window, because we might have
    121 	 * a lot of memory in the machine.
    122 	 */
    123 	t = &ccp->cc_dmat_direct;
    124 	t->_cookie = ccp;
    125 	t->_wbase = DWLPx_DIRECT_MAPPED_BASE;
    126 	t->_wsize = DWLPx_DIRECT_MAPPED_SIZE;
    127 	t->_next_window = &ccp->cc_dmat_sgmap;
    128 	t->_boundary = 0;
    129 	t->_sgmap = NULL;
    130 	t->_get_tag = dwlpx_dma_get_tag;
    131 	t->_dmamap_create = _bus_dmamap_create;
    132 	t->_dmamap_destroy = _bus_dmamap_destroy;
    133 	t->_dmamap_load = _bus_dmamap_load_direct;
    134 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    135 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    136 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    137 	t->_dmamap_unload = _bus_dmamap_unload;
    138 	t->_dmamap_sync = _bus_dmamap_sync;
    139 
    140 	t->_dmamem_alloc = _bus_dmamem_alloc;
    141 	t->_dmamem_free = _bus_dmamem_free;
    142 	t->_dmamem_map = _bus_dmamem_map;
    143 	t->_dmamem_unmap = _bus_dmamem_unmap;
    144 	t->_dmamem_mmap = _bus_dmamem_mmap;
    145 
    146 	/*
    147 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    148 	 */
    149 	t = &ccp->cc_dmat_sgmap;
    150 	t->_cookie = ccp;
    151 	t->_wbase = DWLPx_SG_MAPPED_BASE;
    152 	t->_wsize = DWLPx_SG_MAPPED_SIZE(lim);
    153 	t->_next_window = NULL;
    154 	t->_boundary = 0;
    155 	t->_sgmap = &ccp->cc_sgmap;
    156 	t->_pfthresh = DWLPx_SG_MAPPED_PFTHRESH;
    157 	t->_get_tag = dwlpx_dma_get_tag;
    158 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    159 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    160 	t->_dmamap_load = dwlpx_bus_dmamap_load_sgmap;
    161 	t->_dmamap_load_mbuf = dwlpx_bus_dmamap_load_mbuf_sgmap;
    162 	t->_dmamap_load_uio = dwlpx_bus_dmamap_load_uio_sgmap;
    163 	t->_dmamap_load_raw = dwlpx_bus_dmamap_load_raw_sgmap;
    164 	t->_dmamap_unload = dwlpx_bus_dmamap_unload_sgmap;
    165 	t->_dmamap_sync = _bus_dmamap_sync;
    166 
    167 	t->_dmamem_alloc = _bus_dmamem_alloc;
    168 	t->_dmamem_free = _bus_dmamem_free;
    169 	t->_dmamem_map = _bus_dmamem_map;
    170 	t->_dmamem_unmap = _bus_dmamem_unmap;
    171 	t->_dmamem_mmap = _bus_dmamem_mmap;
    172 
    173 	/*
    174 	 * A few notes about SGMAP-mapped DMA on the DWLPx:
    175 	 *
    176 	 * The DWLPx has PCIA-resident SRAM that is used for
    177 	 * the SGMAP page table; there is no TLB.  The DWLPA
    178 	 * has room for 32K entries, yielding a total of 256M
    179 	 * of sgva space.  The DWLPB has 32K entries or 128K
    180 	 * entries, depending on TBIT, yielding either 256M or
    181 	 * 1G of sgva space.
    182 	 *
    183 	 * This sgva space must be shared across all windows
    184 	 * that wish to use SGMAP-mapped DMA; make sure to
    185 	 * adjust the "sgvabase" argument to alpha_sgmap_init()
    186 	 * accordingly if you create more than one SGMAP-mapped
    187 	 * window.  Note that sgvabase != window base.  The former
    188 	 * is used to compute indexes into the page table only.
    189 	 *
    190 	 * In the current implementation the first window is a
    191 	 * 2G window direct-mapped mapped at 2G and the second
    192 	 * window is disabled and the third window is a 256M
    193 	 * or 1GB scatter/gather map at 1GB.
    194 	 *
    195 	 * We just punt on trying to support ISA DMA.  If you want
    196 	 * try that on a TurboLaser, well, you just lose.
    197 	 */
    198 
    199 	/*
    200 	 * Initialize the page table.
    201 	 */
    202 	page_table =
    203 	    (u_int32_t *)ALPHA_PHYS_TO_K0SEG(PCIA_SGMAP_PT + ccp->cc_sysbase);
    204 	for (i = 0; i < lim; i++)
    205 		page_table[i * SGMAP_PTE_SPACING] = 0;
    206 	alpha_mb();
    207 
    208 	/*
    209 	 * Initialize the SGMAP for window C:
    210 	 *
    211 	 *	Size: 256M or 1GB
    212 	 *	Window base: 1GB
    213 	 *	SGVA base: 0
    214 	 */
    215 	exname = malloc(16, M_DEVBUF, M_NOWAIT);
    216 	if (exname == NULL)
    217 		panic("dwlpx_dma_init");
    218 	sprintf(exname, "%s_sgmap_a", ccp->cc_sc->dwlpx_dev.dv_xname);
    219 	alpha_sgmap_init(t, &ccp->cc_sgmap, exname, DWLPx_SG_MAPPED_BASE,
    220 	    0, DWLPx_SG_MAPPED_SIZE(lim), sizeof(u_int32_t),
    221 	    (void *)page_table, 0);
    222 
    223 	/*
    224 	 * Set up DMA windows for this DWLPx.
    225 	 *
    226 	 * Do this even for all HPCs- even for the nonexistent
    227 	 * one on hose zero of a KFTIA.
    228 	 */
    229 	for (i = 0; i < NHPC; i++) {
    230 		REGVAL(PCIA_WMASK_A(i) + ccp->cc_sysbase) = PCIA_WMASK_2G;
    231 		REGVAL(PCIA_TBASE_A(i) + ccp->cc_sysbase) = 0;
    232 		REGVAL(PCIA_WBASE_A(i) + ccp->cc_sysbase) =
    233 		    DWLPx_DIRECT_MAPPED_BASE | PCIA_WBASE_W_EN;
    234 
    235 		REGVAL(PCIA_WMASK_B(i) + ccp->cc_sysbase) = 0;
    236 		REGVAL(PCIA_TBASE_B(i) + ccp->cc_sysbase) = 0;
    237 		REGVAL(PCIA_WBASE_B(i) + ccp->cc_sysbase) = 0;
    238 
    239 		REGVAL(PCIA_WMASK_C(i) + ccp->cc_sysbase) = wmask;
    240 		REGVAL(PCIA_TBASE_C(i) + ccp->cc_sysbase) = 0;
    241 		REGVAL(PCIA_WBASE_C(i) + ccp->cc_sysbase) =
    242 		    DWLPx_SG_MAPPED_BASE | PCIA_WBASE_W_EN | PCIA_WBASE_SG_EN;
    243 		alpha_mb();
    244 	}
    245 
    246 	/* XXX XXX BEGIN XXX XXX */
    247 	{							/* XXX */
    248 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
    249 		alpha_XXX_dmamap_or = DWLPx_DIRECT_MAPPED_BASE;	/* XXX */
    250 	}							/* XXX */
    251 	/* XXX XXX END XXX XXX */
    252 }
    253 
    254 /*
    255  * Return the bus dma tag to be used for the specified bus type.
    256  * INTERNAL USE ONLY!
    257  */
    258 bus_dma_tag_t
    259 dwlpx_dma_get_tag(t, bustype)
    260 	bus_dma_tag_t t;
    261 	alpha_bus_t bustype;
    262 {
    263 	struct dwlpx_config *ccp = t->_cookie;
    264 
    265 	switch (bustype) {
    266 	case ALPHA_BUS_PCI:
    267 	case ALPHA_BUS_EISA:
    268 		if (dwlpx_always_use_sgmap) {
    269 			/*
    270 			 * Always use SGMAPs (for testing purposes?)
    271 			 */
    272 			return (&ccp->cc_dmat_sgmap);
    273 		}
    274 		/*
    275 		 * Give these busses the direct-mapped window; if the
    276 		 * address crosses the threshold, it will fall back
    277 		 * onto the SGMAP window.
    278 		 */
    279 		return (&ccp->cc_dmat_direct);
    280 
    281 	case ALPHA_BUS_ISA:
    282 		/*
    283 		 * Don't even bother; it's not easy, given how the
    284 		 * page table is arranged, and it's not really worth
    285 		 * the trouble.
    286 		 */
    287 		panic("dwlpx_dma_get_tag: ISA DMA?  Forget it...");
    288 		/* NOTREACHED */
    289 
    290 	default:
    291 		panic("dwlpx_dma_get_tag: shouldn't be here, really...");
    292 	}
    293 }
    294 
    295 /*
    296  * Load a DWLPx SGMAP-mapped DMA map with a linear buffer.
    297  */
    298 int
    299 dwlpx_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    300 	bus_dma_tag_t t;
    301 	bus_dmamap_t map;
    302 	void *buf;
    303 	bus_size_t buflen;
    304 	struct proc *p;
    305 	int flags;
    306 {
    307 
    308 	return (pci_sgmap_pte32_load(t, map, buf, buflen, p, flags,
    309 	    t->_sgmap));
    310 }
    311 
    312 /*
    313  * Load a DWLPx SGMAP-mapped DMA map with an mbuf chain.
    314  */
    315 int
    316 dwlpx_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    317 	bus_dma_tag_t t;
    318 	bus_dmamap_t map;
    319 	struct mbuf *m;
    320 	int flags;
    321 {
    322 
    323 	return (pci_sgmap_pte32_load_mbuf(t, map, m, flags, t->_sgmap));
    324 }
    325 
    326 /*
    327  * Load a DWLPx SGMAP-mapped DMA map with a uio.
    328  */
    329 int
    330 dwlpx_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    331 	bus_dma_tag_t t;
    332 	bus_dmamap_t map;
    333 	struct uio *uio;
    334 	int flags;
    335 {
    336 
    337 	return (pci_sgmap_pte32_load_uio(t, map, uio, flags, t->_sgmap));
    338 }
    339 
    340 /*
    341  * Load a DWLPx SGMAP-mapped DMA map with raw memory.
    342  */
    343 int
    344 dwlpx_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    345 	bus_dma_tag_t t;
    346 	bus_dmamap_t map;
    347 	bus_dma_segment_t *segs;
    348 	int nsegs;
    349 	bus_size_t size;
    350 	int flags;
    351 {
    352 
    353 	return (pci_sgmap_pte32_load_raw(t, map, segs, nsegs, size, flags,
    354 	    t->_sgmap));
    355 }
    356 
    357 /*
    358  * Unload a DWLPx DMA map.
    359  */
    360 void
    361 dwlpx_bus_dmamap_unload_sgmap(t, map)
    362 	bus_dma_tag_t t;
    363 	bus_dmamap_t map;
    364 {
    365 
    366 	/*
    367 	 * Invalidate any SGMAP page table entries used by this
    368 	 * mapping.
    369 	 */
    370 	pci_sgmap_pte32_unload(t, map, t->_sgmap);
    371 
    372 	/*
    373 	 * Do the generic bits of the unload.
    374 	 */
    375 	_bus_dmamap_unload(t, map);
    376 }
    377