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