Home | History | Annotate | Line # | Download | only in pci
cia_dma.c revision 1.11
      1 /* $NetBSD: cia_dma.c,v 1.11 1998/06/06 01:33:23 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, SPECIAL, 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: cia_dma.c,v 1.11 1998/06/06 01:33:23 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/pci/ciareg.h>
     57 #include <alpha/pci/ciavar.h>
     58 
     59 bus_dma_tag_t cia_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
     60 
     61 int	cia_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     62 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     63 
     64 void	cia_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     65 
     66 int	cia_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     67 	    bus_size_t, struct proc *, int));
     68 
     69 int	cia_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     70 	    struct mbuf *, int));
     71 
     72 int	cia_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     73 	    struct uio *, int));
     74 
     75 int	cia_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     76 	    bus_dma_segment_t *, int, bus_size_t, int));
     77 
     78 void	cia_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     79 
     80 /*
     81  * Direct-mapped window: 1G at 1G
     82  */
     83 #define	CIA_DIRECT_MAPPED_BASE	(1*1024*1024*1024)
     84 #define	CIA_DIRECT_MAPPED_SIZE	(1*1024*1024*1024)
     85 
     86 /*
     87  * SGMAP window: 8M at 8M
     88  */
     89 #define	CIA_SGMAP_MAPPED_BASE	(8*1024*1024)
     90 #define	CIA_SGMAP_MAPPED_SIZE	(8*1024*1024)
     91 
     92 void	cia_tlb_invalidate __P((void));
     93 void	cia_broken_pyxis_tlb_invalidate __P((void));
     94 
     95 void	(*cia_tlb_invalidate_fn) __P((void));
     96 
     97 #define	CIA_TLB_INVALIDATE()	(*cia_tlb_invalidate_fn)()
     98 
     99 struct alpha_sgmap cia_pyxis_bug_sgmap;
    100 #define	CIA_PYXIS_BUG_BASE	(1*128*1024)
    101 #define	CIA_PYXIS_BUG_SIZE	(2*1024*1024)
    102 
    103 void
    104 cia_dma_init(ccp)
    105 	struct cia_config *ccp;
    106 {
    107 	bus_addr_t tbase;
    108 	bus_dma_tag_t t;
    109 
    110 	/*
    111 	 * Initialize the DMA tag used for direct-mapped DMA.
    112 	 */
    113 	t = &ccp->cc_dmat_direct;
    114 	t->_cookie = ccp;
    115 	t->_wbase = CIA_DIRECT_MAPPED_BASE;
    116 	t->_wsize = CIA_DIRECT_MAPPED_SIZE;
    117 	t->_next_window = NULL;
    118 	t->_boundary = 0;
    119 	t->_sgmap = NULL;
    120 	t->_get_tag = cia_dma_get_tag;
    121 	t->_dmamap_create = _bus_dmamap_create;
    122 	t->_dmamap_destroy = _bus_dmamap_destroy;
    123 	t->_dmamap_load = _bus_dmamap_load_direct;
    124 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    125 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    126 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    127 	t->_dmamap_unload = _bus_dmamap_unload;
    128 	t->_dmamap_sync = _bus_dmamap_sync;
    129 
    130 	t->_dmamem_alloc = _bus_dmamem_alloc;
    131 	t->_dmamem_free = _bus_dmamem_free;
    132 	t->_dmamem_map = _bus_dmamem_map;
    133 	t->_dmamem_unmap = _bus_dmamem_unmap;
    134 	t->_dmamem_mmap = _bus_dmamem_mmap;
    135 
    136 	/*
    137 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    138 	 */
    139 	t = &ccp->cc_dmat_sgmap;
    140 	t->_cookie = ccp;
    141 	t->_wbase = CIA_SGMAP_MAPPED_BASE;
    142 	t->_wsize = CIA_SGMAP_MAPPED_SIZE;
    143 	t->_next_window = NULL;
    144 	t->_boundary = 0;
    145 	t->_sgmap = &ccp->cc_sgmap;
    146 	t->_get_tag = cia_dma_get_tag;
    147 	t->_dmamap_create = cia_bus_dmamap_create_sgmap;
    148 	t->_dmamap_destroy = cia_bus_dmamap_destroy_sgmap;
    149 	t->_dmamap_load = cia_bus_dmamap_load_sgmap;
    150 	t->_dmamap_load_mbuf = cia_bus_dmamap_load_mbuf_sgmap;
    151 	t->_dmamap_load_uio = cia_bus_dmamap_load_uio_sgmap;
    152 	t->_dmamap_load_raw = cia_bus_dmamap_load_raw_sgmap;
    153 	t->_dmamap_unload = cia_bus_dmamap_unload_sgmap;
    154 	t->_dmamap_sync = _bus_dmamap_sync;
    155 
    156 	t->_dmamem_alloc = _bus_dmamem_alloc;
    157 	t->_dmamem_free = _bus_dmamem_free;
    158 	t->_dmamem_map = _bus_dmamem_map;
    159 	t->_dmamem_unmap = _bus_dmamem_unmap;
    160 	t->_dmamem_mmap = _bus_dmamem_mmap;
    161 
    162 	/*
    163 	 * The firmware has set up window 1 as a 1G direct-mapped DMA
    164 	 * window beginning at 1G.  We leave it alone.  Leave window
    165 	 * 0 alone until we reconfigure it for SGMAP-mapped DMA.
    166 	 * Windows 2 and 3 are already disabled.
    167 	 */
    168 
    169 	/*
    170 	 * Initialize the SGMAP.  Must align page table to 32k
    171 	 * (hardware bug?).
    172 	 */
    173 	alpha_sgmap_init(t, &ccp->cc_sgmap, "cia_sgmap",
    174 	    CIA_SGMAP_MAPPED_BASE, 0, CIA_SGMAP_MAPPED_SIZE,
    175 	    sizeof(u_int64_t), NULL, (32*1024*1024));
    176 
    177 	/*
    178 	 * Set up window 0 as an 8MB SGMAP-mapped window
    179 	 * starting at 8MB.
    180 	 */
    181 	REGVAL(CIA_PCI_W0BASE) = CIA_SGMAP_MAPPED_BASE |
    182 	    CIA_PCI_WnBASE_SG_EN | CIA_PCI_WnBASE_W_EN;
    183 	alpha_mb();
    184 
    185 	REGVAL(CIA_PCI_W0MASK) = CIA_PCI_WnMASK_8M;
    186 	alpha_mb();
    187 
    188 	tbase = ccp->cc_sgmap.aps_ptpa >> CIA_PCI_TnBASE_SHIFT;
    189 	if ((tbase & CIA_PCI_TnBASE_MASK) != tbase)
    190 		panic("cia_dma_init: bad page table address");
    191 	REGVAL(CIA_PCI_T0BASE) = tbase;
    192 	alpha_mb();
    193 
    194 	/*
    195 	 * Pass 1 and 2 (i.e. revision <= 1) of the Pyxis have a
    196 	 * broken scatter/gather TLB; it cannot be invalidated.  To
    197 	 * work around this problem, we configure window 2 as an SG
    198 	 * 2M window at 128M, which we use in DMA loopback mode to
    199 	 * read a spill page.  This works by causing TLB misses,
    200 	 * causing the old entries to be purged to make room for
    201 	 * the new entries coming in for the spill page.
    202 	 */
    203 	if ((ccp->cc_flags & CCF_ISPYXIS) != 0 && ccp->cc_rev <= 1) {
    204 		u_int64_t *page_table;
    205 		int i;
    206 
    207 		cia_tlb_invalidate_fn =
    208 		    cia_broken_pyxis_tlb_invalidate;
    209 
    210 		alpha_sgmap_init(t, &cia_pyxis_bug_sgmap,
    211 		    "pyxis_bug_sgmap", CIA_PYXIS_BUG_BASE, 0,
    212 		    CIA_PYXIS_BUG_SIZE, sizeof(u_int64_t), NULL,
    213 		    (32*1024*1024));
    214 
    215 		REGVAL(CIA_PCI_W2BASE) = CIA_PYXIS_BUG_BASE |
    216 		    CIA_PCI_WnBASE_SG_EN | CIA_PCI_WnBASE_W_EN;
    217 		alpha_mb();
    218 
    219 		REGVAL(CIA_PCI_W2MASK) = CIA_PCI_WnMASK_2M;
    220 		alpha_mb();
    221 
    222 		tbase = cia_pyxis_bug_sgmap.aps_ptpa >>
    223 		    CIA_PCI_TnBASE_SHIFT;
    224 		if ((tbase & CIA_PCI_TnBASE_MASK) != tbase)
    225 			panic("cia_dma_init: bad page table address");
    226 		REGVAL(CIA_PCI_T2BASE) = tbase;
    227 		alpha_mb();
    228 
    229 		/*
    230 		 * Initialize the page table to point at the spill
    231 		 * page.  Leave the last entry invalid.
    232 		 */
    233 		pci_sgmap_pte64_init_spill_page_pte();
    234 		for (i = 0, page_table = cia_pyxis_bug_sgmap.aps_pt;
    235 		     i < (CIA_PYXIS_BUG_SIZE / PAGE_SIZE) - 1; i++) {
    236 			page_table[i] =
    237 			    pci_sgmap_pte64_prefetch_spill_page_pte;
    238 		}
    239 		alpha_mb();
    240 	} else
    241 		cia_tlb_invalidate_fn = cia_tlb_invalidate;
    242 
    243 	CIA_TLB_INVALIDATE();
    244 
    245 	/* XXX XXX BEGIN XXX XXX */
    246 	{							/* XXX */
    247 		extern vm_offset_t alpha_XXX_dmamap_or;		/* XXX */
    248 		alpha_XXX_dmamap_or = CIA_DIRECT_MAPPED_BASE;	/* XXX */
    249 	}							/* XXX */
    250 	/* XXX XXX END XXX XXX */
    251 }
    252 
    253 /*
    254  * Return the bus dma tag to be used for the specified bus type.
    255  * INTERNAL USE ONLY!
    256  */
    257 bus_dma_tag_t
    258 cia_dma_get_tag(t, bustype)
    259 	bus_dma_tag_t t;
    260 	alpha_bus_t bustype;
    261 {
    262 	struct cia_config *ccp = t->_cookie;
    263 
    264 	switch (bustype) {
    265 	case ALPHA_BUS_PCI:
    266 	case ALPHA_BUS_EISA:
    267 		/*
    268 		 * Systems with a CIA can only support 1G
    269 		 * of memory, so we use the direct-mapped window
    270 		 * on busses that have 32-bit DMA.
    271 		 */
    272 		return (&ccp->cc_dmat_direct);
    273 
    274 	case ALPHA_BUS_ISA:
    275 		/*
    276 		 * ISA doesn't have enough address bits to use
    277 		 * the direct-mapped DMA window, so we must use
    278 		 * SGMAPs.
    279 		 */
    280 		return (&ccp->cc_dmat_sgmap);
    281 
    282 	default:
    283 		panic("cia_dma_get_tag: shouldn't be here, really...");
    284 	}
    285 }
    286 
    287 /*
    288  * Create a CIA SGMAP-mapped DMA map.
    289  */
    290 int
    291 cia_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    292     flags, dmamp)
    293 	bus_dma_tag_t t;
    294 	bus_size_t size;
    295 	int nsegments;
    296 	bus_size_t maxsegsz;
    297 	bus_size_t boundary;
    298 	int flags;
    299 	bus_dmamap_t *dmamp;
    300 {
    301 	bus_dmamap_t map;
    302 	int error;
    303 
    304 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    305 	    boundary, flags, dmamp);
    306 	if (error)
    307 		return (error);
    308 
    309 	map = *dmamp;
    310 
    311 	if (flags & BUS_DMA_ALLOCNOW) {
    312 		error = alpha_sgmap_alloc(map, round_page(size),
    313 		    t->_sgmap, flags);
    314 		if (error)
    315 			cia_bus_dmamap_destroy_sgmap(t, map);
    316 	}
    317 
    318 	return (error);
    319 }
    320 
    321 /*
    322  * Destroy a CIA SGMAP-mapped DMA map.
    323  */
    324 void
    325 cia_bus_dmamap_destroy_sgmap(t, map)
    326 	bus_dma_tag_t t;
    327 	bus_dmamap_t map;
    328 {
    329 
    330 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    331 		alpha_sgmap_free(map, t->_sgmap);
    332 
    333 	_bus_dmamap_destroy(t, map);
    334 }
    335 
    336 /*
    337  * Load a CIA SGMAP-mapped DMA map with a linear buffer.
    338  */
    339 int
    340 cia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    341 	bus_dma_tag_t t;
    342 	bus_dmamap_t map;
    343 	void *buf;
    344 	bus_size_t buflen;
    345 	struct proc *p;
    346 	int flags;
    347 {
    348 	int error;
    349 
    350 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    351 	    t->_sgmap);
    352 	if (error == 0)
    353 		CIA_TLB_INVALIDATE();
    354 
    355 	return (error);
    356 }
    357 
    358 /*
    359  * Load a CIA SGMAP-mapped DMA map with an mbuf chain.
    360  */
    361 int
    362 cia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    363 	bus_dma_tag_t t;
    364 	bus_dmamap_t map;
    365 	struct mbuf *m;
    366 	int flags;
    367 {
    368 	int error;
    369 
    370 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    371 	if (error == 0)
    372 		CIA_TLB_INVALIDATE();
    373 
    374 	return (error);
    375 }
    376 
    377 /*
    378  * Load a CIA SGMAP-mapped DMA map with a uio.
    379  */
    380 int
    381 cia_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    382 	bus_dma_tag_t t;
    383 	bus_dmamap_t map;
    384 	struct uio *uio;
    385 	int flags;
    386 {
    387 	int error;
    388 
    389 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    390 	if (error == 0)
    391 		CIA_TLB_INVALIDATE();
    392 
    393 	return (error);
    394 }
    395 
    396 /*
    397  * Load a CIA SGMAP-mapped DMA map with raw memory.
    398  */
    399 int
    400 cia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    401 	bus_dma_tag_t t;
    402 	bus_dmamap_t map;
    403 	bus_dma_segment_t *segs;
    404 	int nsegs;
    405 	bus_size_t size;
    406 	int flags;
    407 {
    408 	int error;
    409 
    410 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    411 	    t->_sgmap);
    412 	if (error == 0)
    413 		CIA_TLB_INVALIDATE();
    414 
    415 	return (error);
    416 }
    417 
    418 /*
    419  * Unload a CIA DMA map.
    420  */
    421 void
    422 cia_bus_dmamap_unload_sgmap(t, map)
    423 	bus_dma_tag_t t;
    424 	bus_dmamap_t map;
    425 {
    426 
    427 	/*
    428 	 * Invalidate any SGMAP page table entries used by this
    429 	 * mapping.
    430 	 */
    431 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    432 	CIA_TLB_INVALIDATE();
    433 
    434 	/*
    435 	 * Do the generic bits of the unload.
    436 	 */
    437 	_bus_dmamap_unload(t, map);
    438 }
    439 
    440 /*
    441  * Flush the CIA scatter/gather TLB.
    442  */
    443 void
    444 cia_tlb_invalidate()
    445 {
    446 
    447 	alpha_mb();
    448 	REGVAL(CIA_PCI_TBIA) = CIA_PCI_TBIA_ALL;
    449 	alpha_mb();
    450 }
    451 
    452 /*
    453  * Flush the scatter/gather TLB on broken Pyxis chips.
    454  */
    455 void
    456 cia_broken_pyxis_tlb_invalidate()
    457 {
    458 	volatile u_int64_t dummy;
    459 	u_int32_t ctrl;
    460 	int i, s;
    461 
    462 	s = splhigh();
    463 
    464 	/*
    465 	 * Put the Pyxis into PCI loopback mode.
    466 	 */
    467 	alpha_mb();
    468 	ctrl = REGVAL(CIA_CSR_CTRL);
    469 	REGVAL(CIA_CSR_CTRL) = ctrl | CTRL_PCI_LOOP_EN;
    470 	alpha_mb();
    471 
    472 	/*
    473 	 * Now, read from PCI dense memory space at offset 128M (our
    474 	 * target window base), skipping 64k on each read.  This forces
    475 	 * S/G TLB misses.
    476 	 *
    477 	 * XXX Looks like the TLB entries are `not quite LRU'.  We need
    478 	 * XXX to read more times than there are actual tags!
    479 	 */
    480 	for (i = 0; i < CIA_TLB_NTAGS + 4; i++) {
    481 		dummy = *((volatile u_int64_t *)
    482 		    ALPHA_PHYS_TO_K0SEG(CIA_PCI_DENSE + CIA_PYXIS_BUG_BASE +
    483 		    (i * 65536)));
    484 	}
    485 
    486 	/*
    487 	 * Restore normal PCI operation.
    488 	 */
    489 	alpha_mb();
    490 	REGVAL(CIA_CSR_CTRL) = ctrl;
    491 	alpha_mb();
    492 
    493 	splx(s);
    494 }
    495