Home | History | Annotate | Line # | Download | only in pci
mcpcia_dma.c revision 1.25
      1 /* $NetBSD: mcpcia_dma.c,v 1.25 2021/06/19 16:36:36 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999 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 and Matthew Jacob of the Numerical Aerospace Simulation
      9  * Facility, 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: mcpcia_dma.c,v 1.25 2021/06/19 16:36:36 thorpej 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 #define _ALPHA_BUS_DMA_PRIVATE
     44 #include <sys/bus.h>
     45 
     46 #include <dev/pci/pcireg.h>
     47 #include <dev/pci/pcivar.h>
     48 #include <alpha/pci/mcpciareg.h>
     49 #include <alpha/pci/mcpciavar.h>
     50 
     51 static bus_dma_tag_t mcpcia_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     52 
     53 static int	mcpcia_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t,
     54 		    void *, bus_size_t, struct proc *, int);
     55 
     56 static int	mcpcia_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     57 		    struct mbuf *, int);
     58 
     59 static int	mcpcia_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     60 		    struct uio *, int);
     61 
     62 static int	mcpcia_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
     63 		    bus_dma_segment_t *, int, bus_size_t, int);
     64 
     65 static void	mcpcia_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
     66 
     67 /*
     68  * Direct-mapped window: 2G at 2G
     69  */
     70 #define	MCPCIA_DIRECT_MAPPED_BASE	(2UL*1024UL*1024UL*1024UL)
     71 #define	MCPCIA_DIRECT_MAPPED_SIZE	(2UL*1024UL*1024UL*1024UL)
     72 
     73 /*
     74  * SGMAP window for PCI: 1G at 1G
     75  */
     76 #define	MCPCIA_PCI_SG_MAPPED_BASE	(1UL*1024UL*1024UL*1024UL)
     77 #define	MCPCIA_PCI_SG_MAPPED_SIZE	(1UL*1024UL*1024UL*1024UL)
     78 
     79 /*
     80  * SGMAP window for ISA: 8M at 8M
     81  */
     82 #define	MCPCIA_ISA_SG_MAPPED_BASE	(8*1024*1024)
     83 #define	MCPCIA_ISA_SG_MAPPED_SIZE	(8*1024*1024)
     84 
     85 /* MCPCIA has a 256-byte out-bound DMA prefetch threshold. */
     86 #define	MCPCIA_SG_MAPPED_PFTHRESH	256
     87 
     88 #define	MCPCIA_SGTLB_INVALIDATE(ccp)					\
     89 do {									\
     90 	alpha_mb();							\
     91 	REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef;			\
     92 	alpha_mb();							\
     93 } while (0)
     94 
     95 void
     96 mcpcia_dma_init(struct mcpcia_config *ccp)
     97 {
     98 	bus_dma_tag_t t;
     99 
    100 	/*
    101 	 * Initialize the DMA tag used for direct-mapped DMA.
    102 	 */
    103 	t = &ccp->cc_dmat_direct;
    104 	t->_cookie = ccp;
    105 	t->_wbase = MCPCIA_DIRECT_MAPPED_BASE;
    106 	t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE;
    107 	t->_next_window = &ccp->cc_dmat_pci_sgmap;
    108 	t->_boundary = 0;
    109 	t->_sgmap = NULL;
    110 	t->_get_tag = mcpcia_dma_get_tag;
    111 	t->_dmamap_create = _bus_dmamap_create;
    112 	t->_dmamap_destroy = _bus_dmamap_destroy;
    113 	t->_dmamap_load = _bus_dmamap_load_direct;
    114 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    115 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    116 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    117 	t->_dmamap_unload = _bus_dmamap_unload;
    118 	t->_dmamap_sync = _bus_dmamap_sync;
    119 
    120 	t->_dmamem_alloc = _bus_dmamem_alloc;
    121 	t->_dmamem_free = _bus_dmamem_free;
    122 	t->_dmamem_map = _bus_dmamem_map;
    123 	t->_dmamem_unmap = _bus_dmamem_unmap;
    124 	t->_dmamem_mmap = _bus_dmamem_mmap;
    125 
    126 	/*
    127 	 * Initialize the DMA tag used for sgmap-mapped PCI DMA.
    128 	 */
    129 	t = &ccp->cc_dmat_pci_sgmap;
    130 	t->_cookie = ccp;
    131 	t->_wbase = MCPCIA_PCI_SG_MAPPED_BASE;
    132 	t->_wsize = MCPCIA_PCI_SG_MAPPED_SIZE;
    133 	t->_next_window = NULL;
    134 	t->_boundary = 0;
    135 	t->_sgmap = &ccp->cc_pci_sgmap;
    136 	t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH;
    137 	t->_get_tag = mcpcia_dma_get_tag;
    138 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    139 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    140 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    141 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    142 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    143 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    144 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    145 	t->_dmamap_sync = _bus_dmamap_sync;
    146 
    147 	t->_dmamem_alloc = _bus_dmamem_alloc;
    148 	t->_dmamem_free = _bus_dmamem_free;
    149 	t->_dmamem_map = _bus_dmamem_map;
    150 	t->_dmamem_unmap = _bus_dmamem_unmap;
    151 	t->_dmamem_mmap = _bus_dmamem_mmap;
    152 
    153 	/*
    154 	 * Initialize the DMA tag used for sgmap-mapped ISA DMA.
    155 	 */
    156 	t = &ccp->cc_dmat_isa_sgmap;
    157 	t->_cookie = ccp;
    158 	t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE;
    159 	t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE;
    160 	t->_next_window = NULL;
    161 	t->_boundary = 0;
    162 	t->_sgmap = &ccp->cc_isa_sgmap;
    163 	t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH;
    164 	t->_get_tag = mcpcia_dma_get_tag;
    165 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    166 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    167 	t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap;
    168 	t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap;
    169 	t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap;
    170 	t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap;
    171 	t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap;
    172 	t->_dmamap_sync = _bus_dmamap_sync;
    173 
    174 	t->_dmamem_alloc = _bus_dmamem_alloc;
    175 	t->_dmamem_free = _bus_dmamem_free;
    176 	t->_dmamem_map = _bus_dmamem_map;
    177 	t->_dmamem_unmap = _bus_dmamem_unmap;
    178 	t->_dmamem_mmap = _bus_dmamem_mmap;
    179 
    180 	/*
    181 	 * Initialize the SGMAPs.
    182 	 */
    183 	alpha_sgmap_init(&ccp->cc_dmat_pci_sgmap, &ccp->cc_pci_sgmap,
    184 	    "mcpcia pci sgmap",
    185 	    MCPCIA_PCI_SG_MAPPED_BASE, 0, MCPCIA_PCI_SG_MAPPED_SIZE,
    186 	    sizeof(uint64_t), NULL, 0);
    187 
    188 	alpha_sgmap_init(&ccp->cc_dmat_isa_sgmap, &ccp->cc_isa_sgmap,
    189 	    "mcpcia isa sgmap",
    190 	    MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE,
    191 	    sizeof(uint64_t), NULL, 0);
    192 
    193 	/*
    194 	 * Disable windows first.
    195 	 */
    196 
    197 	REGVAL(MCPCIA_W0_BASE(ccp)) = 0;
    198 	REGVAL(MCPCIA_W1_BASE(ccp)) = 0;
    199 	REGVAL(MCPCIA_W2_BASE(ccp)) = 0;
    200 	REGVAL(MCPCIA_W3_BASE(ccp)) = 0;
    201 	REGVAL(MCPCIA_T0_BASE(ccp)) = 0;
    202 	REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
    203 	REGVAL(MCPCIA_T2_BASE(ccp)) = 0;
    204 	REGVAL(MCPCIA_T3_BASE(ccp)) = 0;
    205 	alpha_mb();
    206 
    207 	/*
    208 	 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB.
    209 	 */
    210 	REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_8M;
    211 	REGVAL(MCPCIA_T0_BASE(ccp)) =
    212 		ccp->cc_isa_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    213 	alpha_mb();
    214 	REGVAL(MCPCIA_W0_BASE(ccp)) =
    215 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE;
    216 	alpha_mb();
    217 
    218 	MCPCIA_SGTLB_INVALIDATE(ccp);
    219 
    220 	/*
    221 	 * Set up window 1 as a 2 GB Direct-mapped window starting at 2GB.
    222 	 */
    223 
    224 	REGVAL(MCPCIA_W1_MASK(ccp)) = MCPCIA_WMASK_2G;
    225 	REGVAL(MCPCIA_T1_BASE(ccp)) = 0;
    226 	alpha_mb();
    227 	REGVAL(MCPCIA_W1_BASE(ccp)) =
    228 		MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN;
    229 	alpha_mb();
    230 
    231 	/*
    232 	 * Set up window 2 as a 1G SGMAP-mapped window starting at 1G.
    233 	 */
    234 
    235 	REGVAL(MCPCIA_W2_MASK(ccp)) = MCPCIA_WMASK_1G;
    236 	REGVAL(MCPCIA_T2_BASE(ccp)) =
    237 		ccp->cc_pci_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT;
    238 	alpha_mb();
    239 	REGVAL(MCPCIA_W2_BASE(ccp)) =
    240 		MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_PCI_SG_MAPPED_BASE;
    241 	alpha_mb();
    242 }
    243 
    244 /*
    245  * Return the bus dma tag to be used for the specified bus type.
    246  * INTERNAL USE ONLY!
    247  */
    248 static bus_dma_tag_t
    249 mcpcia_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
    250 {
    251 	struct mcpcia_config *ccp = t->_cookie;
    252 
    253 	switch (bustype) {
    254 	case ALPHA_BUS_PCI:
    255 	case ALPHA_BUS_EISA:
    256 		/*
    257 		 * Start off using the direct-mapped window.  We will
    258 		 * automatically fall backed onto the chained PCI SGMAP
    259 		 * window if necessary.
    260 		 */
    261 		return (&ccp->cc_dmat_direct);
    262 
    263 	case ALPHA_BUS_ISA:
    264 		/*
    265 		 * ISA doesn't have enough address bits to use
    266 		 * the direct-mapped DMA window, so we must use
    267 		 * SGMAPs.
    268 		 */
    269 		return (&ccp->cc_dmat_isa_sgmap);
    270 
    271 	default:
    272 		panic("mcpcia_dma_get_tag: shouldn't be here, really...");
    273 	}
    274 }
    275 
    276 /*
    277  * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer.
    278  */
    279 static int
    280 mcpcia_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
    281 {
    282 	int error;
    283 	struct mcpcia_config *ccp = t->_cookie;
    284 
    285 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    286 	    t->_sgmap);
    287 	if (error == 0)
    288 		MCPCIA_SGTLB_INVALIDATE(ccp);
    289 	return (error);
    290 }
    291 
    292 /*
    293  * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain.
    294  */
    295 static int
    296 mcpcia_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
    297 {
    298 	int error;
    299 	struct mcpcia_config *ccp = t->_cookie;
    300 
    301 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    302 	if (error == 0)
    303 		MCPCIA_SGTLB_INVALIDATE(ccp);
    304 	return (error);
    305 }
    306 
    307 /*
    308  * Load a MCPCIA SGMAP-mapped DMA map with a uio.
    309  */
    310 static int
    311 mcpcia_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
    312 {
    313 	int error;
    314 	struct mcpcia_config *ccp = t->_cookie;
    315 
    316 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    317 	if (error == 0)
    318 		MCPCIA_SGTLB_INVALIDATE(ccp);
    319 	return (error);
    320 }
    321 
    322 /*
    323  * Load a MCPCIA SGMAP-mapped DMA map with raw memory.
    324  */
    325 static int
    326 mcpcia_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)
    327 {
    328 	int error;
    329 	struct mcpcia_config *ccp = t->_cookie;
    330 
    331 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    332 	    t->_sgmap);
    333 	if (error == 0)
    334 		MCPCIA_SGTLB_INVALIDATE(ccp);
    335 	return (error);
    336 }
    337 
    338 /*
    339  * Unload a MCPCIA DMA map.
    340  */
    341 static void
    342 mcpcia_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
    343 {
    344 	struct mcpcia_config *ccp = t->_cookie;
    345 
    346 	/*
    347 	 * Invalidate any SGMAP page table entries used by this mapping.
    348 	 */
    349 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    350 	MCPCIA_SGTLB_INVALIDATE(ccp);
    351 
    352 	/*
    353 	 * Do the generic bits of the unload.
    354 	 */
    355 	_bus_dmamap_unload_common(t, map);
    356 }
    357