Home | History | Annotate | Line # | Download | only in pci
tsp_dma.c revision 1.15.6.2
      1 /* $NetBSD: tsp_dma.c,v 1.15.6.2 2021/06/17 04:46:17 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2021 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 /*-
     34  * Copyright (c) 1999 by Ross Harvey.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by Ross Harvey.
     47  * 4. The name of Ross Harvey may not be used to endorse or promote products
     48  *    derived from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
     51  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY BE LIABLE FOR ANY
     54  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: tsp_dma.c,v 1.15.6.2 2021/06/17 04:46:17 thorpej Exp $");
     65 
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/kernel.h>
     69 #include <sys/device.h>
     70 #include <sys/malloc.h>
     71 
     72 #include <machine/autoconf.h>
     73 #define _ALPHA_BUS_DMA_PRIVATE
     74 #include <sys/bus.h>
     75 #include <machine/rpb.h>
     76 
     77 #include <dev/pci/pcireg.h>
     78 #include <dev/pci/pcivar.h>
     79 #include <alpha/pci/tsreg.h>
     80 #include <alpha/pci/tsvar.h>
     81 
     82 #define tsp_dma() { Generate ctags(1) key. }
     83 
     84 #define	EDIFF(a, b) (((a) | WSBA_ENA | WSBA_SG)	!= ((b) | WSBA_ENA | WSBA_SG))
     85 
     86 static bus_dma_tag_t tsp_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     87 
     88 static int	tsp_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     89 		    bus_size_t, struct proc *, int);
     90 
     91 static int	tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     92 		    struct mbuf *, int);
     93 
     94 static int	tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     95 		    struct uio *, int);
     96 
     97 static int	tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
     98 		    bus_dma_segment_t *, int, bus_size_t, int);
     99 
    100 static void	tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
    101 
    102 static void	tsp_tlb_invalidate(struct tsp_config *);
    103 
    104 /*
    105  * XXX Need to figure out what this is, if any.  Initialize it to
    106  * XXX something that should be safe.
    107  */
    108 #define	TSP_SGMAP_PFTHRESH	256
    109 
    110 /*
    111  * Quoting the 21272 programmer's reference manual:
    112  *
    113  * <quote>
    114  * 10.1.4.4 Monster Window DMA Address Translation
    115  *
    116  * In case of a PCI dual-address cycle command, the high-order PCI address
    117  * bits <63:40> are compared to the constant value 0x0000_01 (that is, bit
    118  * <40> = 1; all other bits = 0). If these bits match, a monster window hit
    119  * has occurred and the low-order PCI address bits <34:0> are used unchanged
    120  * as the system address bits <34:0>. PCI address bits <39:35> are ignored.
    121  * The high-order 32 PCI address bits are available on b_ad<31:0> in the
    122  * second cycle of a DAC, and also on b_ad<63:32> in the first cycle of a
    123  * DAC if b_req64_l is asserted.
    124  * </quote>
    125  *
    126  * This means that we can address up to 32GB of RAM using a direct-mapped
    127  * 64-bit DMA tag.  This leaves us possibly having to fall back on SGMAP
    128  * DMA on a Titan system (those support up to 64GB of RAM), and we may have
    129  * to address that with an additional large SGMAP DAC window at another
    130  * time.
    131  */
    132 #define	TSP_MONSTER_DMA_WINDOW_BASE	0x100##00000000UL
    133 #define	TSP_MONSTER_DMA_WINDOW_SIZE	0x008##00000000UL
    134 
    135 void
    136 tsp_dma_init(struct tsp_config *pcp)
    137 {
    138 	int i;
    139 	bus_dma_tag_t t;
    140 	struct ts_pchip *pccsr = pcp->pc_csr;
    141 	bus_addr_t dwbase, dwlen, sgwbase, sgwlen, tbase;
    142 	static struct map_expected {
    143 		uint32_t base, mask, enables;
    144 	} premap[4] = {
    145 		{ 0x00800000, 0x00700000, WSBA_ENA | WSBA_SG },
    146 		{ 0x80000000, 0x3ff00000, WSBA_ENA           },
    147 		{ 0, 0, 0 },
    148 		{ 0, 0, 0 }
    149 	};
    150 
    151 	alpha_mb();
    152 	for(i = 0; i < 4; ++i) {
    153 		if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
    154 		    EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask))
    155 			printf("tsp%d: window %d: %lx/base %lx/mask %lx"
    156 			    " reinitialized\n",
    157 			    pcp->pc_pslot, i,
    158 			    pccsr->tsp_wsba[i].tsg_r,
    159 			    pccsr->tsp_wsm[i].tsg_r,
    160 			    pccsr->tsp_tba[i].tsg_r);
    161 		pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
    162 		pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
    163 	}
    164 
    165 	/* Ensure the Monster Window is enabled. */
    166 	pccsr->tsp_pctl.tsg_r |= PCTL_MWIN;
    167 	alpha_mb();
    168 
    169 	/*
    170 	 * Initialize the DMA tag used for direct-mapped 64-bit DMA.
    171 	 */
    172 	t = &pcp->pc_dmat64_direct;
    173 	t->_cookie = pcp;
    174 	t->_wbase = TSP_MONSTER_DMA_WINDOW_BASE;
    175 	t->_wsize = TSP_MONSTER_DMA_WINDOW_SIZE;
    176 	t->_next_window = &pcp->pc_dmat_sgmap;
    177 	t->_boundary = 0;
    178 	t->_sgmap = NULL;
    179 	t->_get_tag = tsp_dma_get_tag;
    180 	t->_dmamap_create = _bus_dmamap_create;
    181 	t->_dmamap_destroy = _bus_dmamap_destroy;
    182 	t->_dmamap_load = _bus_dmamap_load_direct;
    183 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    184 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    185 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    186 	t->_dmamap_unload = _bus_dmamap_unload;
    187 	t->_dmamap_sync = _bus_dmamap_sync;
    188 
    189 	t->_dmamem_alloc = _bus_dmamem_alloc;
    190 	t->_dmamem_free = _bus_dmamem_free;
    191 	t->_dmamem_map = _bus_dmamem_map;
    192 	t->_dmamem_unmap = _bus_dmamem_unmap;
    193 	t->_dmamem_mmap = _bus_dmamem_mmap;
    194 
    195 	/*
    196 	 * Initialize the DMA tag used for direct-mapped DMA.
    197 	 */
    198 	t = &pcp->pc_dmat_direct;
    199 	t->_cookie = pcp;
    200 	t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
    201 	t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
    202 	t->_next_window = &pcp->pc_dmat_sgmap;
    203 	t->_boundary = 0;
    204 	t->_sgmap = NULL;
    205 	t->_get_tag = tsp_dma_get_tag;
    206 	t->_dmamap_create = _bus_dmamap_create;
    207 	t->_dmamap_destroy = _bus_dmamap_destroy;
    208 	t->_dmamap_load = _bus_dmamap_load_direct;
    209 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    210 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    211 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    212 	t->_dmamap_unload = _bus_dmamap_unload;
    213 	t->_dmamap_sync = _bus_dmamap_sync;
    214 
    215 	t->_dmamem_alloc = _bus_dmamem_alloc;
    216 	t->_dmamem_free = _bus_dmamem_free;
    217 	t->_dmamem_map = _bus_dmamem_map;
    218 	t->_dmamem_unmap = _bus_dmamem_unmap;
    219 	t->_dmamem_mmap = _bus_dmamem_mmap;
    220 
    221 	/*
    222 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    223 	 */
    224 	t = &pcp->pc_dmat_sgmap;
    225 	t->_cookie = pcp;
    226 	t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
    227 	t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
    228 	t->_next_window = NULL;
    229 	t->_boundary = 0;
    230 	t->_sgmap = &pcp->pc_sgmap;
    231 	t->_pfthresh = TSP_SGMAP_PFTHRESH;
    232 	t->_get_tag = tsp_dma_get_tag;
    233 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    234 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    235 	t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
    236 	t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
    237 	t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
    238 	t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
    239 	t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
    240 	t->_dmamap_sync = _bus_dmamap_sync;
    241 
    242 	t->_dmamem_alloc = _bus_dmamem_alloc;
    243 	t->_dmamem_free = _bus_dmamem_free;
    244 	t->_dmamem_map = _bus_dmamem_map;
    245 	t->_dmamem_unmap = _bus_dmamem_unmap;
    246 	t->_dmamem_mmap = _bus_dmamem_mmap;
    247 
    248 	/*
    249 	 * Initialize the SGMAP.  Align page table to 32k in case
    250 	 * window is somewhat larger than expected.
    251 	 */
    252 	alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
    253 	    sgwbase, 0, sgwlen, sizeof(uint64_t), NULL, (32*1024));
    254 
    255 	/*
    256 	 * Enable window 0 and enable SG PTE mapping.
    257 	 */
    258 	alpha_mb();
    259 	pccsr->tsp_wsba[0].tsg_r |= WSBA_SG | WSBA_ENA;
    260 	alpha_mb();
    261 
    262 	/*
    263 	 * Enable window 1 in direct mode.
    264 	 */
    265 	alpha_mb();
    266 	pccsr->tsp_wsba[1].tsg_r =
    267 	    (pccsr->tsp_wsba[1].tsg_r & ~WSBA_SG) | WSBA_ENA;
    268 	alpha_mb();
    269 
    270 	/*
    271 	 * Check windows for sanity, especially if we later decide to
    272 	 * use the firmware's initialization in some cases.
    273 	 */
    274 	if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
    275 	    (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
    276 		panic("tsp_dma_init: overlap");
    277 
    278 	tbase = pcp->pc_sgmap.aps_ptpa;
    279 	if (tbase & ~0x7fffffc00UL)
    280 		panic("tsp_dma_init: bad page table address");
    281 	alpha_mb();
    282 	pccsr->tsp_tba[0].tsg_r = tbase;
    283 	alpha_mb();
    284 
    285 	tsp_tlb_invalidate(pcp);
    286 	alpha_mb();
    287 }
    288 
    289 /*
    290  * Return the bus dma tag to be used for the specified bus type.
    291  * INTERNAL USE ONLY!
    292  */
    293 static bus_dma_tag_t
    294 tsp_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
    295 {
    296 	struct tsp_config *pcp = t->_cookie;
    297 
    298 	switch (bustype) {
    299 	case ALPHA_BUS_PCI:
    300 	case ALPHA_BUS_EISA:
    301 		/*
    302 		 * The direct mapped window will work for most systems,
    303 		 * most of the time. When it doesn't, we chain to the sgmap
    304 		 * window automatically.
    305 		 */
    306 		return (&pcp->pc_dmat_direct);
    307 
    308 	case ALPHA_BUS_ISA:
    309 		/*
    310 		 * ISA doesn't have enough address bits to use
    311 		 * the direct-mapped DMA window, so we must use
    312 		 * SGMAPs.
    313 		 */
    314 		return (&pcp->pc_dmat_sgmap);
    315 
    316 	default:
    317 		panic("tsp_dma_get_tag: shouldn't be here, really...");
    318 	}
    319 }
    320 
    321 /*
    322  * Load a TSP SGMAP-mapped DMA map with a linear buffer.
    323  */
    324 static int
    325 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)
    326 {
    327 	int error;
    328 
    329 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    330 	    t->_sgmap);
    331 	if (error == 0)
    332 		tsp_tlb_invalidate(t->_cookie);
    333 
    334 	return (error);
    335 }
    336 
    337 /*
    338  * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
    339  */
    340 static int
    341 tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
    342 {
    343 	int error;
    344 
    345 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    346 	if (error == 0)
    347 		tsp_tlb_invalidate(t->_cookie);
    348 
    349 	return (error);
    350 }
    351 
    352 /*
    353  * Load a TSP SGMAP-mapped DMA map with a uio.
    354  */
    355 static int
    356 tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
    357 {
    358 	int error;
    359 
    360 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    361 	if (error == 0)
    362 		tsp_tlb_invalidate(t->_cookie);
    363 
    364 	return (error);
    365 }
    366 
    367 /*
    368  * Load a TSP SGMAP-mapped DMA map with raw memory.
    369  */
    370 static int
    371 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)
    372 {
    373 	int error;
    374 
    375 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    376 	    t->_sgmap);
    377 	if (error == 0)
    378 		tsp_tlb_invalidate(t->_cookie);
    379 
    380 	return (error);
    381 }
    382 
    383 /*
    384  * Unload a TSP DMA map.
    385  */
    386 static void
    387 tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
    388 {
    389 
    390 	/*
    391 	 * Invalidate any SGMAP page table entries used by this
    392 	 * mapping.
    393 	 */
    394 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    395 	tsp_tlb_invalidate(t->_cookie);
    396 
    397 	/*
    398 	 * Do the generic bits of the unload.
    399 	 */
    400 	_bus_dmamap_unload_common(t, map);
    401 }
    402 
    403 /*
    404  * Flush the TSP scatter/gather TLB.
    405  */
    406 static void
    407 tsp_tlb_invalidate(struct tsp_config *pcp)
    408 {
    409 
    410 	alpha_mb();
    411 	*pcp->pc_tlbia = 0;
    412 	alpha_mb();
    413 }
    414