Home | History | Annotate | Line # | Download | only in pci
tsp_dma.c revision 1.19
      1  1.19  thorpej /* $NetBSD: tsp_dma.c,v 1.19 2021/07/18 00:01:20 thorpej Exp $ */
      2  1.17  thorpej 
      3  1.17  thorpej /*-
      4  1.17  thorpej  * Copyright (c) 1997, 1998, 2021 The NetBSD Foundation, Inc.
      5  1.17  thorpej  * All rights reserved.
      6  1.17  thorpej  *
      7  1.17  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.17  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.17  thorpej  * NASA Ames Research Center.
     10  1.17  thorpej  *
     11  1.17  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.17  thorpej  * modification, are permitted provided that the following conditions
     13  1.17  thorpej  * are met:
     14  1.17  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.17  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.17  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.17  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.17  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.17  thorpej  *
     20  1.17  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.17  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.17  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.17  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.17  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.17  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.17  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.17  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.17  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.17  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.17  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31  1.17  thorpej  */
     32   1.1     ross 
     33   1.1     ross /*-
     34   1.1     ross  * Copyright (c) 1999 by Ross Harvey.  All rights reserved.
     35   1.1     ross  *
     36   1.1     ross  * Redistribution and use in source and binary forms, with or without
     37   1.1     ross  * modification, are permitted provided that the following conditions
     38   1.1     ross  * are met:
     39   1.1     ross  * 1. Redistributions of source code must retain the above copyright
     40   1.1     ross  *    notice, this list of conditions and the following disclaimer.
     41   1.1     ross  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1     ross  *    notice, this list of conditions and the following disclaimer in the
     43   1.1     ross  *    documentation and/or other materials provided with the distribution.
     44   1.1     ross  * 3. All advertising materials mentioning features or use of this software
     45   1.1     ross  *    must display the following acknowledgement:
     46   1.1     ross  *	This product includes software developed by Ross Harvey.
     47   1.1     ross  * 4. The name of Ross Harvey may not be used to endorse or promote products
     48   1.1     ross  *    derived from this software without specific prior written permission.
     49   1.1     ross  *
     50   1.1     ross  * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
     51   1.1     ross  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     52   1.1     ross  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
     53   1.1     ross  * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY BE LIABLE FOR ANY
     54   1.1     ross  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55   1.1     ross  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56   1.1     ross  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57   1.1     ross  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58   1.1     ross  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59   1.1     ross  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60   1.1     ross  * SUCH DAMAGE.
     61   1.1     ross  */
     62   1.5    lukem 
     63   1.5    lukem #include <sys/cdefs.h>
     64  1.19  thorpej __KERNEL_RCSID(0, "$NetBSD: tsp_dma.c,v 1.19 2021/07/18 00:01:20 thorpej Exp $");
     65   1.1     ross 
     66   1.1     ross #include <sys/param.h>
     67   1.1     ross #include <sys/systm.h>
     68   1.1     ross #include <sys/kernel.h>
     69   1.1     ross #include <sys/device.h>
     70   1.2      mrg 
     71  1.19  thorpej #include <uvm/uvm_extern.h>
     72  1.19  thorpej 
     73   1.1     ross #include <machine/autoconf.h>
     74   1.1     ross #define _ALPHA_BUS_DMA_PRIVATE
     75  1.11   dyoung #include <sys/bus.h>
     76   1.1     ross #include <machine/rpb.h>
     77   1.1     ross 
     78   1.1     ross #include <dev/pci/pcireg.h>
     79   1.1     ross #include <dev/pci/pcivar.h>
     80   1.1     ross #include <alpha/pci/tsreg.h>
     81   1.1     ross #include <alpha/pci/tsvar.h>
     82   1.1     ross 
     83   1.1     ross #define tsp_dma() { Generate ctags(1) key. }
     84   1.1     ross 
     85  1.16  thorpej static bus_dma_tag_t tsp_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     86   1.1     ross 
     87  1.16  thorpej static int	tsp_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     88  1.16  thorpej 		    bus_size_t, struct proc *, int);
     89   1.1     ross 
     90  1.16  thorpej static int	tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     91  1.16  thorpej 		    struct mbuf *, int);
     92   1.1     ross 
     93  1.16  thorpej static int	tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     94  1.16  thorpej 		    struct uio *, int);
     95   1.1     ross 
     96  1.16  thorpej static int	tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
     97  1.16  thorpej 		    bus_dma_segment_t *, int, bus_size_t, int);
     98   1.1     ross 
     99  1.16  thorpej static void	tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
    100   1.1     ross 
    101  1.16  thorpej static void	tsp_tlb_invalidate(struct tsp_config *);
    102   1.1     ross 
    103   1.4  thorpej /*
    104   1.4  thorpej  * XXX Need to figure out what this is, if any.  Initialize it to
    105   1.4  thorpej  * XXX something that should be safe.
    106   1.4  thorpej  */
    107   1.4  thorpej #define	TSP_SGMAP_PFTHRESH	256
    108   1.4  thorpej 
    109  1.17  thorpej /*
    110  1.17  thorpej  * Quoting the 21272 programmer's reference manual:
    111  1.17  thorpej  *
    112  1.17  thorpej  * <quote>
    113  1.17  thorpej  * 10.1.4.4 Monster Window DMA Address Translation
    114  1.17  thorpej  *
    115  1.17  thorpej  * In case of a PCI dual-address cycle command, the high-order PCI address
    116  1.17  thorpej  * bits <63:40> are compared to the constant value 0x0000_01 (that is, bit
    117  1.17  thorpej  * <40> = 1; all other bits = 0). If these bits match, a monster window hit
    118  1.17  thorpej  * has occurred and the low-order PCI address bits <34:0> are used unchanged
    119  1.17  thorpej  * as the system address bits <34:0>. PCI address bits <39:35> are ignored.
    120  1.17  thorpej  * The high-order 32 PCI address bits are available on b_ad<31:0> in the
    121  1.17  thorpej  * second cycle of a DAC, and also on b_ad<63:32> in the first cycle of a
    122  1.17  thorpej  * DAC if b_req64_l is asserted.
    123  1.17  thorpej  * </quote>
    124  1.17  thorpej  *
    125  1.17  thorpej  * This means that we can address up to 32GB of RAM using a direct-mapped
    126  1.17  thorpej  * 64-bit DMA tag.  This leaves us possibly having to fall back on SGMAP
    127  1.17  thorpej  * DMA on a Titan system (those support up to 64GB of RAM), and we may have
    128  1.17  thorpej  * to address that with an additional large SGMAP DAC window at another
    129  1.19  thorpej  * time.  XXX Does the Titan Monster Window support the extra bit?
    130  1.17  thorpej  */
    131  1.17  thorpej #define	TSP_MONSTER_DMA_WINDOW_BASE	0x100##00000000UL
    132  1.17  thorpej #define	TSP_MONSTER_DMA_WINDOW_SIZE	0x008##00000000UL
    133  1.17  thorpej 
    134  1.19  thorpej /*
    135  1.19  thorpej  * Basic 24-bit ISA DMA window is 8MB @ 8MB.  The firmware will
    136  1.19  thorpej  * have set this up in Window 0.
    137  1.19  thorpej  */
    138  1.19  thorpej #define	TSP_SGMAP_MAPPED_LO_BASE	(8UL * 1024 * 1024)
    139  1.19  thorpej #define	TSP_SGMAP_MAPPED_LO_SIZE	(8UL * 1024 * 1024)
    140  1.19  thorpej 
    141  1.19  thorpej /*
    142  1.19  thorpej  * Basic 32-bit PCI DMA window is 1GB @ 2GB.  The firmware will
    143  1.19  thorpej  * have set this up in Window 1.
    144  1.19  thorpej  */
    145  1.19  thorpej #define	TSP_DIRECT_MAPPED_BASE		(2UL * 1024 * 1024 * 1024)
    146  1.19  thorpej #define	TSP_DIRECT_MAPPED_SIZE		(1UL * 1024 * 1024 * 1024)
    147  1.19  thorpej 
    148  1.19  thorpej /*
    149  1.19  thorpej  * For systems that have > 1GB of RAM, but PCI devices that don't
    150  1.19  thorpej  * support dual-address cycle, we will also set up an additional
    151  1.19  thorpej  * SGMAP DMA window 1GB @ 3GB.  We will use Window 2 for this purpose.
    152  1.19  thorpej  */
    153  1.19  thorpej #define	TSP_SGMAP_MAPPED_HI_BASE	(3UL * 1024 * 1024 * 1024)
    154  1.19  thorpej #define	TSP_SGMAP_MAPPED_HI_SIZE	(1UL * 1024 * 1024 * 1024)
    155  1.19  thorpej 
    156  1.19  thorpej /*
    157  1.19  thorpej  * Window 3 is still available for use in the future.  Window 3 supports
    158  1.19  thorpej  * dual address cycle.
    159  1.19  thorpej  */
    160  1.19  thorpej 
    161   1.1     ross void
    162   1.9      dsl tsp_dma_init(struct tsp_config *pcp)
    163   1.1     ross {
    164   1.1     ross 	bus_dma_tag_t t;
    165  1.19  thorpej 	bus_dma_tag_t t_sg_hi = NULL;
    166   1.1     ross 	struct ts_pchip *pccsr = pcp->pc_csr;
    167  1.19  thorpej 	bus_addr_t tbase;
    168  1.17  thorpej 
    169  1.17  thorpej 	/* Ensure the Monster Window is enabled. */
    170  1.19  thorpej 	alpha_mb();
    171  1.17  thorpej 	pccsr->tsp_pctl.tsg_r |= PCTL_MWIN;
    172   1.1     ross 	alpha_mb();
    173   1.1     ross 
    174   1.1     ross 	/*
    175  1.19  thorpej 	 * If we have more than 1GB of RAM, then set up an sgmap-mapped
    176  1.19  thorpej 	 * DMA window for non-DAC PCI.  This is better than using the ISA
    177  1.19  thorpej 	 * window, which is pretty small and PCI devices could starve it.
    178  1.19  thorpej 	 *
    179  1.19  thorpej 	 * N.B. avail_end is "last-usable PFN + 1".
    180  1.19  thorpej 	 */
    181  1.19  thorpej 	if (uvm_physseg_get_avail_end(uvm_physseg_get_last()) >
    182  1.19  thorpej 	    atop(TSP_DIRECT_MAPPED_SIZE)) {
    183  1.19  thorpej 		t = t_sg_hi = &pcp->pc_dmat_sgmap_hi;
    184  1.19  thorpej 		t->_cookie = pcp;
    185  1.19  thorpej 		t->_wbase = TSP_SGMAP_MAPPED_HI_BASE;
    186  1.19  thorpej 		t->_wsize = TSP_SGMAP_MAPPED_HI_SIZE;
    187  1.19  thorpej 		t->_next_window = NULL;
    188  1.19  thorpej 		t->_boundary = 0;
    189  1.19  thorpej 		t->_sgmap = &pcp->pc_sgmap_hi;
    190  1.19  thorpej 		t->_pfthresh = TSP_SGMAP_PFTHRESH;
    191  1.19  thorpej 		t->_get_tag = tsp_dma_get_tag;
    192  1.19  thorpej 		t->_dmamap_create = alpha_sgmap_dmamap_create;
    193  1.19  thorpej 		t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    194  1.19  thorpej 		t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
    195  1.19  thorpej 		t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
    196  1.19  thorpej 		t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
    197  1.19  thorpej 		t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
    198  1.19  thorpej 		t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
    199  1.19  thorpej 		t->_dmamap_sync = _bus_dmamap_sync;
    200  1.19  thorpej 
    201  1.19  thorpej 		t->_dmamem_alloc = _bus_dmamem_alloc;
    202  1.19  thorpej 		t->_dmamem_free = _bus_dmamem_free;
    203  1.19  thorpej 		t->_dmamem_map = _bus_dmamem_map;
    204  1.19  thorpej 		t->_dmamem_unmap = _bus_dmamem_unmap;
    205  1.19  thorpej 		t->_dmamem_mmap = _bus_dmamem_mmap;
    206  1.19  thorpej 	}
    207  1.19  thorpej 
    208  1.19  thorpej 	/*
    209  1.17  thorpej 	 * Initialize the DMA tag used for direct-mapped 64-bit DMA.
    210  1.17  thorpej 	 */
    211  1.17  thorpej 	t = &pcp->pc_dmat64_direct;
    212  1.17  thorpej 	t->_cookie = pcp;
    213  1.17  thorpej 	t->_wbase = TSP_MONSTER_DMA_WINDOW_BASE;
    214  1.17  thorpej 	t->_wsize = TSP_MONSTER_DMA_WINDOW_SIZE;
    215  1.19  thorpej 	t->_next_window = t_sg_hi;
    216  1.17  thorpej 	t->_boundary = 0;
    217  1.17  thorpej 	t->_sgmap = NULL;
    218  1.17  thorpej 	t->_get_tag = tsp_dma_get_tag;
    219  1.17  thorpej 	t->_dmamap_create = _bus_dmamap_create;
    220  1.17  thorpej 	t->_dmamap_destroy = _bus_dmamap_destroy;
    221  1.17  thorpej 	t->_dmamap_load = _bus_dmamap_load_direct;
    222  1.17  thorpej 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    223  1.17  thorpej 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    224  1.17  thorpej 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    225  1.17  thorpej 	t->_dmamap_unload = _bus_dmamap_unload;
    226  1.17  thorpej 	t->_dmamap_sync = _bus_dmamap_sync;
    227  1.17  thorpej 
    228  1.17  thorpej 	t->_dmamem_alloc = _bus_dmamem_alloc;
    229  1.17  thorpej 	t->_dmamem_free = _bus_dmamem_free;
    230  1.17  thorpej 	t->_dmamem_map = _bus_dmamem_map;
    231  1.17  thorpej 	t->_dmamem_unmap = _bus_dmamem_unmap;
    232  1.17  thorpej 	t->_dmamem_mmap = _bus_dmamem_mmap;
    233  1.17  thorpej 
    234  1.17  thorpej 	/*
    235   1.1     ross 	 * Initialize the DMA tag used for direct-mapped DMA.
    236   1.1     ross 	 */
    237   1.1     ross 	t = &pcp->pc_dmat_direct;
    238   1.1     ross 	t->_cookie = pcp;
    239  1.19  thorpej 	t->_wbase = TSP_DIRECT_MAPPED_BASE;
    240  1.19  thorpej 	t->_wsize = TSP_DIRECT_MAPPED_SIZE;
    241  1.19  thorpej 	t->_next_window = t_sg_hi;
    242   1.1     ross 	t->_boundary = 0;
    243   1.1     ross 	t->_sgmap = NULL;
    244   1.1     ross 	t->_get_tag = tsp_dma_get_tag;
    245   1.1     ross 	t->_dmamap_create = _bus_dmamap_create;
    246   1.1     ross 	t->_dmamap_destroy = _bus_dmamap_destroy;
    247   1.1     ross 	t->_dmamap_load = _bus_dmamap_load_direct;
    248   1.1     ross 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    249   1.1     ross 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    250   1.1     ross 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    251   1.1     ross 	t->_dmamap_unload = _bus_dmamap_unload;
    252   1.1     ross 	t->_dmamap_sync = _bus_dmamap_sync;
    253   1.1     ross 
    254   1.1     ross 	t->_dmamem_alloc = _bus_dmamem_alloc;
    255   1.1     ross 	t->_dmamem_free = _bus_dmamem_free;
    256   1.1     ross 	t->_dmamem_map = _bus_dmamem_map;
    257   1.1     ross 	t->_dmamem_unmap = _bus_dmamem_unmap;
    258   1.1     ross 	t->_dmamem_mmap = _bus_dmamem_mmap;
    259   1.1     ross 
    260   1.1     ross 	/*
    261  1.19  thorpej 	 * Initialize the DMA tag used for ISA sgmap-mapped DMA.
    262   1.1     ross 	 */
    263  1.19  thorpej 	t = &pcp->pc_dmat_sgmap_lo;
    264   1.1     ross 	t->_cookie = pcp;
    265  1.19  thorpej 	t->_wbase = TSP_SGMAP_MAPPED_LO_BASE;
    266  1.19  thorpej 	t->_wsize = TSP_SGMAP_MAPPED_LO_SIZE;
    267   1.1     ross 	t->_next_window = NULL;
    268   1.1     ross 	t->_boundary = 0;
    269  1.19  thorpej 	t->_sgmap = &pcp->pc_sgmap_lo;
    270   1.4  thorpej 	t->_pfthresh = TSP_SGMAP_PFTHRESH;
    271   1.1     ross 	t->_get_tag = tsp_dma_get_tag;
    272   1.3  thorpej 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    273   1.3  thorpej 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    274   1.1     ross 	t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
    275   1.1     ross 	t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
    276   1.1     ross 	t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
    277   1.1     ross 	t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
    278   1.1     ross 	t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
    279   1.1     ross 	t->_dmamap_sync = _bus_dmamap_sync;
    280   1.1     ross 
    281   1.1     ross 	t->_dmamem_alloc = _bus_dmamem_alloc;
    282   1.1     ross 	t->_dmamem_free = _bus_dmamem_free;
    283   1.1     ross 	t->_dmamem_map = _bus_dmamem_map;
    284   1.1     ross 	t->_dmamem_unmap = _bus_dmamem_unmap;
    285   1.1     ross 	t->_dmamem_mmap = _bus_dmamem_mmap;
    286   1.1     ross 
    287   1.1     ross 	/*
    288  1.19  thorpej 	 * Initialize the SGMAPs.  Align page tables to 32k in case
    289   1.1     ross 	 * window is somewhat larger than expected.
    290   1.1     ross 	 */
    291  1.19  thorpej 	alpha_sgmap_init(t, &pcp->pc_sgmap_lo, "tsp_sgmap_lo",
    292  1.19  thorpej 	    TSP_SGMAP_MAPPED_LO_BASE, 0, TSP_SGMAP_MAPPED_LO_SIZE,
    293  1.19  thorpej 	    sizeof(uint64_t), NULL, (32*1024));
    294  1.19  thorpej 	if (t_sg_hi != NULL) {
    295  1.19  thorpej 		alpha_sgmap_init(t, &pcp->pc_sgmap_hi, "tsp_sgmap_hi",
    296  1.19  thorpej 		    TSP_SGMAP_MAPPED_HI_BASE, 0, TSP_SGMAP_MAPPED_HI_SIZE,
    297  1.19  thorpej 		    sizeof(uint64_t), NULL, (32*1024));
    298  1.19  thorpej 	}
    299   1.1     ross 
    300   1.1     ross 	/*
    301   1.1     ross 	 * Enable window 0 and enable SG PTE mapping.
    302   1.1     ross 	 */
    303   1.1     ross 	alpha_mb();
    304  1.19  thorpej 	pccsr->tsp_wsba[0].tsg_r =
    305  1.19  thorpej 	    TSP_SGMAP_MAPPED_LO_BASE | WSBA_SG | WSBA_ENA;
    306  1.19  thorpej 	alpha_mb();
    307  1.19  thorpej 	pccsr->tsp_wsm[0].tsg_r = WSM_8MB;
    308  1.19  thorpej 	alpha_mb();
    309  1.19  thorpej 	tbase = pcp->pc_sgmap_lo.aps_ptpa;
    310  1.19  thorpej 	if (tbase & ~0x7fffffc00UL)
    311  1.19  thorpej 		panic("tsp_dma_init: bad page table address");
    312  1.19  thorpej 	pccsr->tsp_tba[0].tsg_r = tbase;
    313   1.1     ross 	alpha_mb();
    314   1.1     ross 
    315   1.1     ross 	/*
    316  1.13  tsutsui 	 * Enable window 1 in direct mode.
    317  1.13  tsutsui 	 */
    318  1.13  tsutsui 	alpha_mb();
    319  1.19  thorpej 	pccsr->tsp_wsba[1].tsg_r = TSP_DIRECT_MAPPED_BASE | WSBA_ENA;
    320  1.19  thorpej 	alpha_mb();
    321  1.19  thorpej 	pccsr->tsp_wsm[1].tsg_r = WSM_1GB;
    322  1.19  thorpej 	alpha_mb();
    323  1.19  thorpej 	pccsr->tsp_tba[1].tsg_r = 0;
    324  1.13  tsutsui 	alpha_mb();
    325  1.13  tsutsui 
    326  1.19  thorpej 	if (t_sg_hi != NULL) {
    327  1.19  thorpej 		/*
    328  1.19  thorpej 		 * Enable window 2 and enable SG PTE mapping.
    329  1.19  thorpej 		 */
    330  1.19  thorpej 		alpha_mb();
    331  1.19  thorpej 		pccsr->tsp_wsba[2].tsg_r =
    332  1.19  thorpej 		    TSP_SGMAP_MAPPED_HI_BASE | WSBA_SG | WSBA_ENA;
    333  1.19  thorpej 		alpha_mb();
    334  1.19  thorpej 		pccsr->tsp_wsm[2].tsg_r = WSM_1GB;
    335  1.19  thorpej 		alpha_mb();
    336  1.19  thorpej 		tbase = pcp->pc_sgmap_hi.aps_ptpa;
    337  1.19  thorpej 		if (tbase & ~0x7fffffc00UL)
    338  1.19  thorpej 			panic("tsp_dma_init: bad page table address");
    339  1.19  thorpej 		pccsr->tsp_tba[2].tsg_r = tbase;
    340  1.19  thorpej 		alpha_mb();
    341  1.19  thorpej 	}
    342  1.19  thorpej 
    343  1.13  tsutsui 	/*
    344  1.19  thorpej 	 * Disable window 3.
    345   1.1     ross 	 */
    346   1.1     ross 	alpha_mb();
    347  1.19  thorpej 	pccsr->tsp_wsba[3].tsg_r = 0;
    348  1.19  thorpej 	alpha_mb();
    349  1.19  thorpej 	pccsr->tsp_wsm[3].tsg_r = 0;
    350  1.19  thorpej 	alpha_mb();
    351  1.19  thorpej 	pccsr->tsp_tba[3].tsg_r = 0;
    352   1.1     ross 	alpha_mb();
    353   1.1     ross 
    354   1.1     ross 	tsp_tlb_invalidate(pcp);
    355   1.1     ross 	alpha_mb();
    356   1.1     ross }
    357   1.1     ross 
    358   1.1     ross /*
    359   1.1     ross  * Return the bus dma tag to be used for the specified bus type.
    360   1.1     ross  * INTERNAL USE ONLY!
    361   1.1     ross  */
    362  1.16  thorpej static bus_dma_tag_t
    363   1.9      dsl tsp_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
    364   1.1     ross {
    365   1.1     ross 	struct tsp_config *pcp = t->_cookie;
    366   1.1     ross 
    367   1.1     ross 	switch (bustype) {
    368   1.1     ross 	case ALPHA_BUS_PCI:
    369   1.1     ross 	case ALPHA_BUS_EISA:
    370   1.1     ross 		/*
    371   1.1     ross 		 * The direct mapped window will work for most systems,
    372   1.1     ross 		 * most of the time. When it doesn't, we chain to the sgmap
    373   1.1     ross 		 * window automatically.
    374   1.1     ross 		 */
    375   1.1     ross 		return (&pcp->pc_dmat_direct);
    376   1.1     ross 
    377   1.1     ross 	case ALPHA_BUS_ISA:
    378   1.1     ross 		/*
    379   1.1     ross 		 * ISA doesn't have enough address bits to use
    380   1.1     ross 		 * the direct-mapped DMA window, so we must use
    381   1.1     ross 		 * SGMAPs.
    382   1.1     ross 		 */
    383  1.19  thorpej 		return (&pcp->pc_dmat_sgmap_lo);
    384   1.1     ross 
    385   1.1     ross 	default:
    386   1.1     ross 		panic("tsp_dma_get_tag: shouldn't be here, really...");
    387   1.1     ross 	}
    388   1.1     ross }
    389   1.1     ross 
    390   1.1     ross /*
    391   1.1     ross  * Load a TSP SGMAP-mapped DMA map with a linear buffer.
    392   1.1     ross  */
    393  1.16  thorpej static int
    394   1.9      dsl tsp_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
    395   1.1     ross {
    396   1.1     ross 	int error;
    397   1.1     ross 
    398   1.1     ross 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    399   1.1     ross 	    t->_sgmap);
    400   1.1     ross 	if (error == 0)
    401   1.1     ross 		tsp_tlb_invalidate(t->_cookie);
    402   1.1     ross 
    403   1.1     ross 	return (error);
    404   1.1     ross }
    405   1.1     ross 
    406   1.1     ross /*
    407   1.1     ross  * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
    408   1.1     ross  */
    409  1.16  thorpej static int
    410   1.9      dsl tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
    411   1.1     ross {
    412   1.1     ross 	int error;
    413   1.1     ross 
    414   1.1     ross 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    415   1.1     ross 	if (error == 0)
    416   1.1     ross 		tsp_tlb_invalidate(t->_cookie);
    417   1.1     ross 
    418   1.1     ross 	return (error);
    419   1.1     ross }
    420   1.1     ross 
    421   1.1     ross /*
    422   1.1     ross  * Load a TSP SGMAP-mapped DMA map with a uio.
    423   1.1     ross  */
    424  1.16  thorpej static int
    425   1.9      dsl tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
    426   1.1     ross {
    427   1.1     ross 	int error;
    428   1.1     ross 
    429   1.1     ross 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    430   1.1     ross 	if (error == 0)
    431   1.1     ross 		tsp_tlb_invalidate(t->_cookie);
    432   1.1     ross 
    433   1.1     ross 	return (error);
    434   1.1     ross }
    435   1.1     ross 
    436   1.1     ross /*
    437   1.1     ross  * Load a TSP SGMAP-mapped DMA map with raw memory.
    438   1.1     ross  */
    439  1.16  thorpej static int
    440   1.9      dsl tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    441   1.1     ross {
    442   1.1     ross 	int error;
    443   1.1     ross 
    444   1.1     ross 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    445   1.1     ross 	    t->_sgmap);
    446   1.1     ross 	if (error == 0)
    447   1.1     ross 		tsp_tlb_invalidate(t->_cookie);
    448   1.1     ross 
    449   1.1     ross 	return (error);
    450   1.1     ross }
    451   1.1     ross 
    452   1.1     ross /*
    453   1.1     ross  * Unload a TSP DMA map.
    454   1.1     ross  */
    455  1.16  thorpej static void
    456   1.9      dsl tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
    457   1.1     ross {
    458   1.1     ross 
    459   1.1     ross 	/*
    460   1.1     ross 	 * Invalidate any SGMAP page table entries used by this
    461   1.1     ross 	 * mapping.
    462   1.1     ross 	 */
    463   1.1     ross 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    464   1.1     ross 	tsp_tlb_invalidate(t->_cookie);
    465   1.1     ross 
    466   1.1     ross 	/*
    467   1.1     ross 	 * Do the generic bits of the unload.
    468   1.1     ross 	 */
    469  1.15  thorpej 	_bus_dmamap_unload_common(t, map);
    470   1.1     ross }
    471   1.1     ross 
    472   1.1     ross /*
    473   1.1     ross  * Flush the TSP scatter/gather TLB.
    474   1.1     ross  */
    475  1.16  thorpej static void
    476   1.9      dsl tsp_tlb_invalidate(struct tsp_config *pcp)
    477   1.1     ross {
    478   1.1     ross 
    479   1.1     ross 	alpha_mb();
    480  1.13  tsutsui 	*pcp->pc_tlbia = 0;
    481   1.1     ross 	alpha_mb();
    482   1.1     ross }
    483