Home | History | Annotate | Line # | Download | only in pci
cia_dma.c revision 1.31.6.2
      1  1.31.6.2   thorpej /* $NetBSD: cia_dma.c,v 1.31.6.2 2021/08/01 22:42:02 thorpej Exp $ */
      2       1.2   thorpej 
      3       1.2   thorpej /*-
      4       1.5   thorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5       1.2   thorpej  * All rights reserved.
      6       1.2   thorpej  *
      7       1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.2   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9       1.2   thorpej  * NASA Ames Research Center.
     10       1.2   thorpej  *
     11       1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.2   thorpej  * modification, are permitted provided that the following conditions
     13       1.2   thorpej  * are met:
     14       1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.2   thorpej  *
     20       1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31       1.2   thorpej  */
     32       1.2   thorpej 
     33       1.2   thorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34       1.2   thorpej 
     35  1.31.6.2   thorpej __KERNEL_RCSID(0, "$NetBSD: cia_dma.c,v 1.31.6.2 2021/08/01 22:42:02 thorpej Exp $");
     36       1.2   thorpej 
     37       1.2   thorpej #include <sys/param.h>
     38       1.2   thorpej #include <sys/systm.h>
     39       1.2   thorpej #include <sys/kernel.h>
     40       1.2   thorpej #include <sys/device.h>
     41      1.16       mrg 
     42       1.2   thorpej #define _ALPHA_BUS_DMA_PRIVATE
     43      1.27    dyoung #include <sys/bus.h>
     44       1.2   thorpej 
     45  1.31.6.2   thorpej #include <uvm/uvm_extern.h>
     46  1.31.6.2   thorpej 
     47       1.2   thorpej #include <dev/pci/pcireg.h>
     48       1.2   thorpej #include <dev/pci/pcivar.h>
     49       1.2   thorpej #include <alpha/pci/ciareg.h>
     50       1.2   thorpej #include <alpha/pci/ciavar.h>
     51       1.2   thorpej 
     52  1.31.6.1   thorpej static bus_dma_tag_t cia_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     53       1.2   thorpej 
     54  1.31.6.1   thorpej static int	cia_bus_dmamap_create_direct(bus_dma_tag_t, bus_size_t, int,
     55  1.31.6.1   thorpej 		    bus_size_t, bus_size_t, int, bus_dmamap_t *);
     56      1.15   thorpej 
     57  1.31.6.1   thorpej static int	cia_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     58  1.31.6.1   thorpej 		    bus_size_t, struct proc *, int);
     59       1.2   thorpej 
     60  1.31.6.1   thorpej static int	cia_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     61  1.31.6.1   thorpej 		    struct mbuf *, int);
     62       1.2   thorpej 
     63  1.31.6.1   thorpej static int	cia_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     64  1.31.6.1   thorpej 		    struct uio *, int);
     65       1.2   thorpej 
     66  1.31.6.1   thorpej static int	cia_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
     67  1.31.6.1   thorpej 		    bus_dma_segment_t *, int, bus_size_t, int);
     68       1.2   thorpej 
     69  1.31.6.1   thorpej static void	cia_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
     70       1.2   thorpej 
     71       1.2   thorpej /*
     72       1.8   thorpej  * Direct-mapped window: 1G at 1G
     73       1.2   thorpej  */
     74  1.31.6.2   thorpej #define	CIA_DIRECT_MAPPED_BASE	(1UL*1024*1024*1024)
     75  1.31.6.2   thorpej #define	CIA_DIRECT_MAPPED_SIZE	(1UL*1024*1024*1024)
     76  1.31.6.2   thorpej 
     77  1.31.6.2   thorpej /*
     78  1.31.6.2   thorpej  * SGMAP window for ISA: 8M at 8M
     79  1.31.6.2   thorpej  */
     80  1.31.6.2   thorpej #define	CIA_SGMAP_MAPPED_LO_BASE (8UL*1024*1024)
     81  1.31.6.2   thorpej #define	CIA_SGMAP_MAPPED_LO_SIZE (8UL*1024*1024)
     82       1.2   thorpej 
     83       1.2   thorpej /*
     84  1.31.6.2   thorpej  * SGMAP window for PCI: 1G at 3G
     85       1.2   thorpej  */
     86  1.31.6.2   thorpej #define	CIA_SGMAP_MAPPED_HI_BASE (3UL*1024*1024*1024)
     87  1.31.6.2   thorpej #define	CIA_SGMAP_MAPPED_HI_SIZE (1UL*1024*1024*1024)
     88       1.2   thorpej 
     89      1.18   thorpej /* ALCOR/ALGOR2/PYXIS have a 256-byte out-bound DMA prefetch threshold. */
     90      1.18   thorpej #define	CIA_SGMAP_PFTHRESH	256
     91      1.18   thorpej 
     92  1.31.6.1   thorpej static void	cia_tlb_invalidate(void);
     93  1.31.6.1   thorpej static void	cia_broken_pyxis_tlb_invalidate(void);
     94      1.10   thorpej 
     95  1.31.6.1   thorpej static void	(*cia_tlb_invalidate_fn)(void);
     96      1.10   thorpej 
     97      1.10   thorpej #define	CIA_TLB_INVALIDATE()	(*cia_tlb_invalidate_fn)()
     98      1.10   thorpej 
     99      1.10   thorpej struct alpha_sgmap cia_pyxis_bug_sgmap;
    100  1.31.6.2   thorpej #define	CIA_PYXIS_BUG_BASE	(128UL*1024*1024)
    101  1.31.6.2   thorpej #define	CIA_PYXIS_BUG_SIZE	(2UL*1024*1024)
    102  1.31.6.2   thorpej 
    103  1.31.6.2   thorpej static void
    104  1.31.6.2   thorpej cia_dma_shutdown(void *arg)
    105  1.31.6.2   thorpej {
    106  1.31.6.2   thorpej 	struct cia_config *ccp = arg;
    107  1.31.6.2   thorpej 	int i;
    108  1.31.6.2   thorpej 
    109  1.31.6.2   thorpej 	/*
    110  1.31.6.2   thorpej 	 * Restore the original values, to make the firmware happy.
    111  1.31.6.2   thorpej 	 */
    112  1.31.6.2   thorpej 	for (i = 0; i < 4; i++) {
    113  1.31.6.2   thorpej 		REGVAL(CIA_PCI_W0BASE + (i * 0x100)) =
    114  1.31.6.2   thorpej 		    ccp->cc_saved_windows.wbase[i];
    115  1.31.6.2   thorpej 		alpha_mb();
    116  1.31.6.2   thorpej 		REGVAL(CIA_PCI_W0MASK + (i * 0x100)) =
    117  1.31.6.2   thorpej 		    ccp->cc_saved_windows.wmask[i];
    118  1.31.6.2   thorpej 		alpha_mb();
    119  1.31.6.2   thorpej 		REGVAL(CIA_PCI_T0BASE + (i * 0x100)) =
    120  1.31.6.2   thorpej 		    ccp->cc_saved_windows.tbase[i];
    121  1.31.6.2   thorpej 		alpha_mb();
    122  1.31.6.2   thorpej 	}
    123  1.31.6.2   thorpej }
    124       1.2   thorpej 
    125       1.2   thorpej void
    126      1.24       dsl cia_dma_init(struct cia_config *ccp)
    127       1.2   thorpej {
    128       1.2   thorpej 	bus_addr_t tbase;
    129       1.2   thorpej 	bus_dma_tag_t t;
    130  1.31.6.2   thorpej 	bus_dma_tag_t t_sg_hi = NULL;
    131  1.31.6.2   thorpej 	int i;
    132  1.31.6.2   thorpej 
    133  1.31.6.2   thorpej 	/*
    134  1.31.6.2   thorpej 	 * Save our configuration to restore at shutdown, just
    135  1.31.6.2   thorpej 	 * in case the firmware would get cranky with us.
    136  1.31.6.2   thorpej 	 */
    137  1.31.6.2   thorpej 	for (i = 0; i < 4; i++) {
    138  1.31.6.2   thorpej 		ccp->cc_saved_windows.wbase[i] =
    139  1.31.6.2   thorpej 		    REGVAL(CIA_PCI_W0BASE + (i * 0x100));
    140  1.31.6.2   thorpej 		ccp->cc_saved_windows.wmask[i] =
    141  1.31.6.2   thorpej 		    REGVAL(CIA_PCI_W0MASK + (i * 0x100));
    142  1.31.6.2   thorpej 		ccp->cc_saved_windows.tbase[i] =
    143  1.31.6.2   thorpej 		    REGVAL(CIA_PCI_T0BASE + (i * 0x100));
    144  1.31.6.2   thorpej 	}
    145  1.31.6.2   thorpej 	shutdownhook_establish(cia_dma_shutdown, ccp);
    146  1.31.6.2   thorpej 
    147  1.31.6.2   thorpej 	/*
    148  1.31.6.2   thorpej 	 * If we have more than 1GB of RAM, then set up an sgmap-mapped
    149  1.31.6.2   thorpej 	 * DMA window for PCI.  This is better than using the ISA window,
    150  1.31.6.2   thorpej 	 * which is pretty small and PCI devices could starve it.
    151  1.31.6.2   thorpej 	 *
    152  1.31.6.2   thorpej 	 * N.B. avail_end is "last-usable PFN + 1".
    153  1.31.6.2   thorpej 	 */
    154  1.31.6.2   thorpej 	if (uvm_physseg_get_avail_end(uvm_physseg_get_last()) >
    155  1.31.6.2   thorpej 	    atop(CIA_DIRECT_MAPPED_SIZE)) {
    156  1.31.6.2   thorpej 		t = t_sg_hi = &ccp->cc_dmat_sgmap_hi;
    157  1.31.6.2   thorpej 		t->_cookie = ccp;
    158  1.31.6.2   thorpej 		t->_wbase = CIA_SGMAP_MAPPED_HI_BASE;
    159  1.31.6.2   thorpej 		t->_wsize = CIA_SGMAP_MAPPED_HI_SIZE;
    160  1.31.6.2   thorpej 		t->_next_window = NULL;
    161  1.31.6.2   thorpej 		t->_boundary = 0;
    162  1.31.6.2   thorpej 		t->_sgmap = &ccp->cc_sgmap_hi;
    163  1.31.6.2   thorpej 		t->_pfthresh = CIA_SGMAP_PFTHRESH;
    164  1.31.6.2   thorpej 		t->_get_tag = cia_dma_get_tag;
    165  1.31.6.2   thorpej 		t->_dmamap_create = alpha_sgmap_dmamap_create;
    166  1.31.6.2   thorpej 		t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    167  1.31.6.2   thorpej 		t->_dmamap_load = cia_bus_dmamap_load_sgmap;
    168  1.31.6.2   thorpej 		t->_dmamap_load_mbuf = cia_bus_dmamap_load_mbuf_sgmap;
    169  1.31.6.2   thorpej 		t->_dmamap_load_uio = cia_bus_dmamap_load_uio_sgmap;
    170  1.31.6.2   thorpej 		t->_dmamap_load_raw = cia_bus_dmamap_load_raw_sgmap;
    171  1.31.6.2   thorpej 		t->_dmamap_unload = cia_bus_dmamap_unload_sgmap;
    172  1.31.6.2   thorpej 		t->_dmamap_sync = _bus_dmamap_sync;
    173  1.31.6.2   thorpej 
    174  1.31.6.2   thorpej 		t->_dmamem_alloc = _bus_dmamem_alloc;
    175  1.31.6.2   thorpej 		t->_dmamem_free = _bus_dmamem_free;
    176  1.31.6.2   thorpej 		t->_dmamem_map = _bus_dmamem_map;
    177  1.31.6.2   thorpej 		t->_dmamem_unmap = _bus_dmamem_unmap;
    178  1.31.6.2   thorpej 		t->_dmamem_mmap = _bus_dmamem_mmap;
    179  1.31.6.2   thorpej 	}
    180       1.2   thorpej 
    181       1.2   thorpej 	/*
    182       1.2   thorpej 	 * Initialize the DMA tag used for direct-mapped DMA.
    183       1.2   thorpej 	 */
    184       1.2   thorpej 	t = &ccp->cc_dmat_direct;
    185       1.2   thorpej 	t->_cookie = ccp;
    186       1.7   thorpej 	t->_wbase = CIA_DIRECT_MAPPED_BASE;
    187       1.8   thorpej 	t->_wsize = CIA_DIRECT_MAPPED_SIZE;
    188  1.31.6.2   thorpej 	t->_next_window = t_sg_hi;
    189       1.9   thorpej 	t->_boundary = 0;
    190       1.8   thorpej 	t->_sgmap = NULL;
    191       1.2   thorpej 	t->_get_tag = cia_dma_get_tag;
    192      1.15   thorpej 	t->_dmamap_create = cia_bus_dmamap_create_direct;
    193       1.2   thorpej 	t->_dmamap_destroy = _bus_dmamap_destroy;
    194       1.7   thorpej 	t->_dmamap_load = _bus_dmamap_load_direct;
    195       1.7   thorpej 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    196       1.7   thorpej 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    197       1.7   thorpej 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    198       1.2   thorpej 	t->_dmamap_unload = _bus_dmamap_unload;
    199       1.6   thorpej 	t->_dmamap_sync = _bus_dmamap_sync;
    200       1.2   thorpej 
    201       1.2   thorpej 	t->_dmamem_alloc = _bus_dmamem_alloc;
    202       1.2   thorpej 	t->_dmamem_free = _bus_dmamem_free;
    203       1.2   thorpej 	t->_dmamem_map = _bus_dmamem_map;
    204       1.2   thorpej 	t->_dmamem_unmap = _bus_dmamem_unmap;
    205       1.2   thorpej 	t->_dmamem_mmap = _bus_dmamem_mmap;
    206       1.2   thorpej 
    207       1.2   thorpej 	/*
    208  1.31.6.2   thorpej 	 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
    209       1.2   thorpej 	 */
    210  1.31.6.2   thorpej 	t = &ccp->cc_dmat_sgmap_lo;
    211       1.2   thorpej 	t->_cookie = ccp;
    212  1.31.6.2   thorpej 	t->_wbase = CIA_SGMAP_MAPPED_LO_BASE;
    213  1.31.6.2   thorpej 	t->_wsize = CIA_SGMAP_MAPPED_LO_SIZE;
    214       1.8   thorpej 	t->_next_window = NULL;
    215       1.9   thorpej 	t->_boundary = 0;
    216  1.31.6.2   thorpej 	t->_sgmap = &ccp->cc_sgmap_lo;
    217      1.18   thorpej 	t->_pfthresh = CIA_SGMAP_PFTHRESH;
    218       1.2   thorpej 	t->_get_tag = cia_dma_get_tag;
    219      1.17   thorpej 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    220      1.17   thorpej 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    221       1.2   thorpej 	t->_dmamap_load = cia_bus_dmamap_load_sgmap;
    222       1.2   thorpej 	t->_dmamap_load_mbuf = cia_bus_dmamap_load_mbuf_sgmap;
    223       1.2   thorpej 	t->_dmamap_load_uio = cia_bus_dmamap_load_uio_sgmap;
    224       1.2   thorpej 	t->_dmamap_load_raw = cia_bus_dmamap_load_raw_sgmap;
    225       1.2   thorpej 	t->_dmamap_unload = cia_bus_dmamap_unload_sgmap;
    226       1.6   thorpej 	t->_dmamap_sync = _bus_dmamap_sync;
    227       1.2   thorpej 
    228       1.2   thorpej 	t->_dmamem_alloc = _bus_dmamem_alloc;
    229       1.2   thorpej 	t->_dmamem_free = _bus_dmamem_free;
    230       1.2   thorpej 	t->_dmamem_map = _bus_dmamem_map;
    231       1.2   thorpej 	t->_dmamem_unmap = _bus_dmamem_unmap;
    232       1.2   thorpej 	t->_dmamem_mmap = _bus_dmamem_mmap;
    233       1.2   thorpej 
    234       1.2   thorpej 	/*
    235  1.31.6.2   thorpej 	 * The firmware will have set up window 1 as a 1G dirct-mapped
    236  1.31.6.2   thorpej 	 * DMA window beginning at 1G.  While it's pretty safe to assume
    237  1.31.6.2   thorpej 	 * this is the case, we'll go ahead and program the registers
    238  1.31.6.2   thorpej 	 * as we expect as a belt-and-suspenders measure.
    239       1.2   thorpej 	 */
    240  1.31.6.2   thorpej 	REGVAL(CIA_PCI_W1BASE) = CIA_DIRECT_MAPPED_BASE | CIA_PCI_WnBASE_W_EN;
    241  1.31.6.2   thorpej 	alpha_mb();
    242  1.31.6.2   thorpej 	REGVAL(CIA_PCI_W1MASK) = CIA_PCI_WnMASK_1G;
    243  1.31.6.2   thorpej 	alpha_mb();
    244  1.31.6.2   thorpej 	REGVAL(CIA_PCI_T1BASE) = 0;
    245  1.31.6.2   thorpej 	alpha_mb();
    246       1.2   thorpej 
    247       1.2   thorpej 	/*
    248  1.31.6.2   thorpej 	 * Initialize the SGMAP(s).  Must align page table to at least 32k
    249      1.11   thorpej 	 * (hardware bug?).
    250       1.2   thorpej 	 */
    251  1.31.6.2   thorpej 	alpha_sgmap_init(t, &ccp->cc_sgmap_lo, "cia_sgmap_lo",
    252  1.31.6.2   thorpej 	    CIA_SGMAP_MAPPED_LO_BASE, 0, CIA_SGMAP_MAPPED_LO_SIZE,
    253      1.28      matt 	    sizeof(uint64_t), NULL, (32*1024));
    254  1.31.6.2   thorpej 	if (t_sg_hi != NULL) {
    255  1.31.6.2   thorpej 		alpha_sgmap_init(t_sg_hi, &ccp->cc_sgmap_hi, "cia_sgmap_hi",
    256  1.31.6.2   thorpej 		    CIA_SGMAP_MAPPED_HI_BASE, 0, CIA_SGMAP_MAPPED_HI_SIZE,
    257  1.31.6.2   thorpej 		    sizeof(uint64_t), NULL, (32*1024));
    258  1.31.6.2   thorpej 	}
    259       1.2   thorpej 
    260      1.11   thorpej 	/*
    261      1.11   thorpej 	 * Set up window 0 as an 8MB SGMAP-mapped window
    262      1.11   thorpej 	 * starting at 8MB.
    263      1.11   thorpej 	 */
    264  1.31.6.2   thorpej 	REGVAL(CIA_PCI_W0BASE) = CIA_SGMAP_MAPPED_LO_BASE |
    265      1.11   thorpej 	    CIA_PCI_WnBASE_SG_EN | CIA_PCI_WnBASE_W_EN;
    266      1.11   thorpej 	alpha_mb();
    267      1.11   thorpej 
    268      1.11   thorpej 	REGVAL(CIA_PCI_W0MASK) = CIA_PCI_WnMASK_8M;
    269      1.11   thorpej 	alpha_mb();
    270      1.11   thorpej 
    271  1.31.6.2   thorpej 	tbase = ccp->cc_sgmap_lo.aps_ptpa >> CIA_PCI_TnBASE_SHIFT;
    272      1.11   thorpej 	if ((tbase & CIA_PCI_TnBASE_MASK) != tbase)
    273      1.11   thorpej 		panic("cia_dma_init: bad page table address");
    274      1.11   thorpej 	REGVAL(CIA_PCI_T0BASE) = tbase;
    275      1.11   thorpej 	alpha_mb();
    276      1.11   thorpej 
    277      1.11   thorpej 	/*
    278  1.31.6.2   thorpej 	 * (Maybe) set up window 3 as a 1G SGMAP-mapped window starting
    279  1.31.6.2   thorpej 	 * at 3G.
    280  1.31.6.2   thorpej 	 */
    281  1.31.6.2   thorpej 	if (t_sg_hi != NULL) {
    282  1.31.6.2   thorpej 		REGVAL(CIA_PCI_W3BASE) = CIA_SGMAP_MAPPED_HI_BASE |
    283  1.31.6.2   thorpej 		    CIA_PCI_WnBASE_SG_EN | CIA_PCI_WnBASE_W_EN;
    284  1.31.6.2   thorpej 		alpha_mb();
    285  1.31.6.2   thorpej 
    286  1.31.6.2   thorpej 		REGVAL(CIA_PCI_W3MASK) = CIA_PCI_WnMASK_1G;
    287  1.31.6.2   thorpej 		alpha_mb();
    288  1.31.6.2   thorpej 
    289  1.31.6.2   thorpej 		tbase = ccp->cc_sgmap_hi.aps_ptpa >> CIA_PCI_TnBASE_SHIFT;
    290  1.31.6.2   thorpej 		if ((tbase & CIA_PCI_TnBASE_MASK) != tbase)
    291  1.31.6.2   thorpej 			panic("cia_dma_init: bad page table address");
    292  1.31.6.2   thorpej 		REGVAL(CIA_PCI_T3BASE) = tbase;
    293  1.31.6.2   thorpej 		alpha_mb();
    294  1.31.6.2   thorpej 	} else {
    295  1.31.6.2   thorpej 		REGVAL(CIA_PCI_W3BASE) = 0;
    296  1.31.6.2   thorpej 		alpha_mb();
    297  1.31.6.2   thorpej 	}
    298  1.31.6.2   thorpej 
    299  1.31.6.2   thorpej 	/*
    300      1.11   thorpej 	 * Pass 1 and 2 (i.e. revision <= 1) of the Pyxis have a
    301      1.11   thorpej 	 * broken scatter/gather TLB; it cannot be invalidated.  To
    302      1.11   thorpej 	 * work around this problem, we configure window 2 as an SG
    303      1.11   thorpej 	 * 2M window at 128M, which we use in DMA loopback mode to
    304      1.11   thorpej 	 * read a spill page.  This works by causing TLB misses,
    305      1.11   thorpej 	 * causing the old entries to be purged to make room for
    306      1.11   thorpej 	 * the new entries coming in for the spill page.
    307      1.11   thorpej 	 */
    308      1.11   thorpej 	if ((ccp->cc_flags & CCF_ISPYXIS) != 0 && ccp->cc_rev <= 1) {
    309      1.28      matt 		uint64_t *page_table;
    310      1.11   thorpej 
    311      1.11   thorpej 		cia_tlb_invalidate_fn =
    312      1.11   thorpej 		    cia_broken_pyxis_tlb_invalidate;
    313      1.11   thorpej 
    314      1.11   thorpej 		alpha_sgmap_init(t, &cia_pyxis_bug_sgmap,
    315      1.11   thorpej 		    "pyxis_bug_sgmap", CIA_PYXIS_BUG_BASE, 0,
    316      1.28      matt 		    CIA_PYXIS_BUG_SIZE, sizeof(uint64_t), NULL,
    317      1.12   thorpej 		    (32*1024));
    318      1.11   thorpej 
    319      1.11   thorpej 		REGVAL(CIA_PCI_W2BASE) = CIA_PYXIS_BUG_BASE |
    320       1.4   thorpej 		    CIA_PCI_WnBASE_SG_EN | CIA_PCI_WnBASE_W_EN;
    321       1.2   thorpej 		alpha_mb();
    322       1.2   thorpej 
    323      1.11   thorpej 		REGVAL(CIA_PCI_W2MASK) = CIA_PCI_WnMASK_2M;
    324       1.2   thorpej 		alpha_mb();
    325       1.2   thorpej 
    326      1.11   thorpej 		tbase = cia_pyxis_bug_sgmap.aps_ptpa >>
    327      1.11   thorpej 		    CIA_PCI_TnBASE_SHIFT;
    328       1.2   thorpej 		if ((tbase & CIA_PCI_TnBASE_MASK) != tbase)
    329       1.2   thorpej 			panic("cia_dma_init: bad page table address");
    330      1.11   thorpej 		REGVAL(CIA_PCI_T2BASE) = tbase;
    331       1.2   thorpej 		alpha_mb();
    332       1.2   thorpej 
    333      1.10   thorpej 		/*
    334      1.11   thorpej 		 * Initialize the page table to point at the spill
    335      1.11   thorpej 		 * page.  Leave the last entry invalid.
    336      1.10   thorpej 		 */
    337      1.11   thorpej 		pci_sgmap_pte64_init_spill_page_pte();
    338      1.11   thorpej 		for (i = 0, page_table = cia_pyxis_bug_sgmap.aps_pt;
    339      1.11   thorpej 		     i < (CIA_PYXIS_BUG_SIZE / PAGE_SIZE) - 1; i++) {
    340      1.11   thorpej 			page_table[i] =
    341      1.11   thorpej 			    pci_sgmap_pte64_prefetch_spill_page_pte;
    342      1.11   thorpej 		}
    343      1.11   thorpej 		alpha_mb();
    344  1.31.6.2   thorpej 	} else {
    345  1.31.6.2   thorpej 		REGVAL(CIA_PCI_W2BASE) = 0;
    346  1.31.6.2   thorpej 		alpha_mb();
    347  1.31.6.2   thorpej 
    348      1.11   thorpej 		cia_tlb_invalidate_fn = cia_tlb_invalidate;
    349  1.31.6.2   thorpej 	}
    350      1.10   thorpej 
    351      1.11   thorpej 	CIA_TLB_INVALIDATE();
    352       1.2   thorpej }
    353       1.2   thorpej 
    354       1.2   thorpej /*
    355       1.2   thorpej  * Return the bus dma tag to be used for the specified bus type.
    356       1.2   thorpej  * INTERNAL USE ONLY!
    357       1.2   thorpej  */
    358  1.31.6.1   thorpej static bus_dma_tag_t
    359      1.24       dsl cia_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
    360       1.2   thorpej {
    361       1.2   thorpej 	struct cia_config *ccp = t->_cookie;
    362       1.2   thorpej 
    363       1.2   thorpej 	switch (bustype) {
    364       1.2   thorpej 	case ALPHA_BUS_PCI:
    365       1.2   thorpej 	case ALPHA_BUS_EISA:
    366       1.2   thorpej 		/*
    367  1.31.6.2   thorpej 		 * Regardless if how much memory is installed,
    368  1.31.6.2   thorpej 		 * start with the direct-mapped window.  It will
    369  1.31.6.2   thorpej 		 * fall back to the SGMAP window if we encounter a
    370  1.31.6.2   thorpej 		 * page that is out of range.
    371       1.2   thorpej 		 */
    372       1.2   thorpej 		return (&ccp->cc_dmat_direct);
    373       1.2   thorpej 
    374       1.2   thorpej 	case ALPHA_BUS_ISA:
    375       1.2   thorpej 		/*
    376       1.2   thorpej 		 * ISA doesn't have enough address bits to use
    377       1.2   thorpej 		 * the direct-mapped DMA window, so we must use
    378       1.2   thorpej 		 * SGMAPs.
    379       1.2   thorpej 		 */
    380  1.31.6.2   thorpej 		return (&ccp->cc_dmat_sgmap_lo);
    381       1.2   thorpej 
    382       1.2   thorpej 	default:
    383       1.2   thorpej 		panic("cia_dma_get_tag: shouldn't be here, really...");
    384       1.2   thorpej 	}
    385      1.15   thorpej }
    386      1.15   thorpej 
    387      1.15   thorpej /*
    388      1.15   thorpej  * Create a CIA direct-mapped DMA map.
    389      1.15   thorpej  */
    390  1.31.6.1   thorpej static int
    391      1.28      matt cia_bus_dmamap_create_direct(
    392      1.28      matt 	bus_dma_tag_t t,
    393      1.28      matt 	bus_size_t size,
    394      1.28      matt 	int nsegments,
    395      1.28      matt 	bus_size_t maxsegsz,
    396      1.28      matt 	bus_size_t boundary,
    397      1.28      matt 	int flags,
    398      1.28      matt 	bus_dmamap_t *dmamp)
    399      1.15   thorpej {
    400      1.15   thorpej 	struct cia_config *ccp = t->_cookie;
    401      1.15   thorpej 	bus_dmamap_t map;
    402      1.15   thorpej 	int error;
    403      1.15   thorpej 
    404      1.15   thorpej 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    405      1.15   thorpej 	    boundary, flags, dmamp);
    406      1.15   thorpej 	if (error)
    407      1.15   thorpej 		return (error);
    408      1.15   thorpej 
    409      1.15   thorpej 	map = *dmamp;
    410      1.15   thorpej 
    411      1.15   thorpej 	if ((ccp->cc_flags & CCF_PYXISBUG) != 0 &&
    412      1.15   thorpej 	    map->_dm_segcnt > 1) {
    413      1.15   thorpej 		/*
    414      1.15   thorpej 		 * We have a Pyxis with the DMA page crossing bug, make
    415      1.15   thorpej 		 * sure we don't coalesce adjacent DMA segments.
    416      1.15   thorpej 		 *
    417      1.15   thorpej 		 * NOTE: We can only do this if the max segment count
    418      1.15   thorpej 		 * is greater than 1.  This is because many network
    419      1.15   thorpej 		 * drivers allocate large contiguous blocks of memory
    420      1.15   thorpej 		 * for control data structures, even though they won't
    421      1.15   thorpej 		 * do any single DMA that crosses a page coundary.
    422      1.19    keihan 		 *	-- thorpej (at) NetBSD.org, 2/5/2000
    423      1.15   thorpej 		 */
    424      1.15   thorpej 		map->_dm_flags |= DMAMAP_NO_COALESCE;
    425      1.15   thorpej 	}
    426      1.15   thorpej 
    427      1.15   thorpej 	return (0);
    428       1.2   thorpej }
    429       1.2   thorpej 
    430       1.2   thorpej /*
    431       1.2   thorpej  * Load a CIA SGMAP-mapped DMA map with a linear buffer.
    432       1.2   thorpej  */
    433  1.31.6.1   thorpej static int
    434  1.31.6.2   thorpej cia_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    435  1.31.6.2   thorpej     bus_size_t buflen, struct proc *p, int flags)
    436       1.2   thorpej {
    437       1.2   thorpej 	int error;
    438       1.2   thorpej 
    439       1.2   thorpej 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    440       1.8   thorpej 	    t->_sgmap);
    441       1.2   thorpej 	if (error == 0)
    442       1.2   thorpej 		CIA_TLB_INVALIDATE();
    443       1.2   thorpej 
    444       1.2   thorpej 	return (error);
    445       1.2   thorpej }
    446       1.2   thorpej 
    447       1.2   thorpej /*
    448       1.2   thorpej  * Load a CIA SGMAP-mapped DMA map with an mbuf chain.
    449       1.2   thorpej  */
    450  1.31.6.1   thorpej static int
    451  1.31.6.2   thorpej cia_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    452  1.31.6.2   thorpej     struct mbuf *m, int flags)
    453       1.2   thorpej {
    454       1.2   thorpej 	int error;
    455       1.2   thorpej 
    456       1.8   thorpej 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    457       1.2   thorpej 	if (error == 0)
    458       1.2   thorpej 		CIA_TLB_INVALIDATE();
    459       1.2   thorpej 
    460       1.2   thorpej 	return (error);
    461       1.2   thorpej }
    462       1.2   thorpej 
    463       1.2   thorpej /*
    464       1.2   thorpej  * Load a CIA SGMAP-mapped DMA map with a uio.
    465       1.2   thorpej  */
    466  1.31.6.1   thorpej static int
    467  1.31.6.2   thorpej cia_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    468  1.31.6.2   thorpej     struct uio *uio, int flags)
    469       1.2   thorpej {
    470       1.2   thorpej 	int error;
    471       1.2   thorpej 
    472       1.8   thorpej 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    473       1.2   thorpej 	if (error == 0)
    474       1.2   thorpej 		CIA_TLB_INVALIDATE();
    475       1.2   thorpej 
    476       1.2   thorpej 	return (error);
    477       1.2   thorpej }
    478       1.2   thorpej 
    479       1.2   thorpej /*
    480       1.2   thorpej  * Load a CIA SGMAP-mapped DMA map with raw memory.
    481       1.2   thorpej  */
    482  1.31.6.1   thorpej static int
    483  1.31.6.2   thorpej cia_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    484  1.31.6.2   thorpej     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    485       1.2   thorpej {
    486       1.2   thorpej 	int error;
    487       1.2   thorpej 
    488       1.2   thorpej 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    489       1.8   thorpej 	    t->_sgmap);
    490       1.2   thorpej 	if (error == 0)
    491       1.2   thorpej 		CIA_TLB_INVALIDATE();
    492       1.2   thorpej 
    493       1.2   thorpej 	return (error);
    494       1.2   thorpej }
    495       1.2   thorpej 
    496       1.2   thorpej /*
    497       1.2   thorpej  * Unload a CIA DMA map.
    498       1.2   thorpej  */
    499  1.31.6.1   thorpej static void
    500      1.24       dsl cia_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
    501       1.2   thorpej {
    502       1.2   thorpej 
    503       1.2   thorpej 	/*
    504       1.2   thorpej 	 * Invalidate any SGMAP page table entries used by this
    505       1.2   thorpej 	 * mapping.
    506       1.2   thorpej 	 */
    507       1.8   thorpej 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    508       1.2   thorpej 	CIA_TLB_INVALIDATE();
    509       1.2   thorpej 
    510       1.2   thorpej 	/*
    511       1.2   thorpej 	 * Do the generic bits of the unload.
    512       1.2   thorpej 	 */
    513      1.31   thorpej 	_bus_dmamap_unload_common(t, map);
    514      1.10   thorpej }
    515      1.10   thorpej 
    516      1.10   thorpej /*
    517      1.10   thorpej  * Flush the CIA scatter/gather TLB.
    518      1.10   thorpej  */
    519  1.31.6.1   thorpej static void
    520      1.25    cegger cia_tlb_invalidate(void)
    521      1.10   thorpej {
    522      1.10   thorpej 
    523      1.10   thorpej 	alpha_mb();
    524      1.10   thorpej 	REGVAL(CIA_PCI_TBIA) = CIA_PCI_TBIA_ALL;
    525      1.10   thorpej 	alpha_mb();
    526      1.10   thorpej }
    527      1.10   thorpej 
    528      1.10   thorpej /*
    529      1.10   thorpej  * Flush the scatter/gather TLB on broken Pyxis chips.
    530      1.10   thorpej  */
    531  1.31.6.1   thorpej static void
    532      1.25    cegger cia_broken_pyxis_tlb_invalidate(void)
    533      1.10   thorpej {
    534      1.28      matt 	uint32_t ctrl;
    535      1.10   thorpej 	int i, s;
    536      1.10   thorpej 
    537      1.10   thorpej 	s = splhigh();
    538      1.10   thorpej 
    539      1.10   thorpej 	/*
    540      1.10   thorpej 	 * Put the Pyxis into PCI loopback mode.
    541      1.10   thorpej 	 */
    542      1.10   thorpej 	alpha_mb();
    543      1.10   thorpej 	ctrl = REGVAL(CIA_CSR_CTRL);
    544      1.10   thorpej 	REGVAL(CIA_CSR_CTRL) = ctrl | CTRL_PCI_LOOP_EN;
    545      1.10   thorpej 	alpha_mb();
    546      1.10   thorpej 
    547      1.10   thorpej 	/*
    548      1.10   thorpej 	 * Now, read from PCI dense memory space at offset 128M (our
    549      1.10   thorpej 	 * target window base), skipping 64k on each read.  This forces
    550      1.10   thorpej 	 * S/G TLB misses.
    551      1.10   thorpej 	 *
    552      1.10   thorpej 	 * XXX Looks like the TLB entries are `not quite LRU'.  We need
    553      1.10   thorpej 	 * XXX to read more times than there are actual tags!
    554      1.10   thorpej 	 */
    555      1.10   thorpej 	for (i = 0; i < CIA_TLB_NTAGS + 4; i++) {
    556      1.29  christos 		volatile uint64_t dummy;
    557      1.28      matt 		dummy = *((volatile uint64_t *)
    558      1.10   thorpej 		    ALPHA_PHYS_TO_K0SEG(CIA_PCI_DENSE + CIA_PYXIS_BUG_BASE +
    559      1.10   thorpej 		    (i * 65536)));
    560      1.29  christos 		__USE(dummy);
    561      1.10   thorpej 	}
    562      1.10   thorpej 
    563      1.10   thorpej 	/*
    564      1.10   thorpej 	 * Restore normal PCI operation.
    565      1.10   thorpej 	 */
    566      1.10   thorpej 	alpha_mb();
    567      1.10   thorpej 	REGVAL(CIA_CSR_CTRL) = ctrl;
    568      1.10   thorpej 	alpha_mb();
    569      1.10   thorpej 
    570      1.10   thorpej 	splx(s);
    571       1.2   thorpej }
    572