Home | History | Annotate | Line # | Download | only in pci
lca_dma.c revision 1.26
      1 /* $NetBSD: lca_dma.c,v 1.26 2021/05/05 02:15:18 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 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 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34 
     35 __KERNEL_RCSID(0, "$NetBSD: lca_dma.c,v 1.26 2021/05/05 02:15:18 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/lcareg.h>
     49 #include <alpha/pci/lcavar.h>
     50 
     51 static bus_dma_tag_t lca_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     52 
     53 static int	lca_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     54 		    bus_size_t, struct proc *, int);
     55 
     56 static int	lca_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     57 		    struct mbuf *, int);
     58 
     59 static int	lca_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     60 		    struct uio *, int);
     61 
     62 static int	lca_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	lca_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
     66 
     67 /*
     68  * Direct-mapped window: 1G at 1G
     69  */
     70 #define	LCA_DIRECT_MAPPED_BASE	(1*1024*1024*1024)
     71 #define	LCA_DIRECT_MAPPED_SIZE	(1*1024*1024*1024)
     72 
     73 /*
     74  * SGMAP window: 8M at 8M
     75  */
     76 #define	LCA_SGMAP_MAPPED_BASE	(8*1024*1024)
     77 #define	LCA_SGMAP_MAPPED_SIZE	(8*1024*1024)
     78 
     79 /*
     80  * The LCA doesn't have a DMA prefetch threshold.  However, it is known
     81  * to lose if we don't allocate a spill page.  So initialize it to 256.
     82  */
     83 #define	LCA_SGMAP_PFTHRESH	256
     84 
     85 /*
     86  * Macro to flush LCA scatter/gather TLB.
     87  */
     88 #define	LCA_TLB_INVALIDATE() \
     89 do { \
     90 	alpha_mb(); \
     91 	REGVAL64(LCA_IOC_TBIA) = 0; \
     92 	alpha_mb(); \
     93 } while (0)
     94 
     95 void
     96 lca_dma_init(struct lca_config *lcp)
     97 {
     98 	bus_dma_tag_t t;
     99 
    100 	/*
    101 	 * Initialize the DMA tag used for direct-mapped DMA.
    102 	 */
    103 	t = &lcp->lc_dmat_direct;
    104 	t->_cookie = lcp;
    105 	t->_wbase = LCA_DIRECT_MAPPED_BASE;
    106 	t->_wsize = LCA_DIRECT_MAPPED_SIZE;
    107 	t->_next_window = NULL;
    108 	t->_boundary = 0;
    109 	t->_sgmap = NULL;
    110 	t->_get_tag = lca_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 DMA.
    128 	 */
    129 	t = &lcp->lc_dmat_sgmap;
    130 	t->_cookie = lcp;
    131 	t->_wbase = LCA_SGMAP_MAPPED_BASE;
    132 	t->_wsize = LCA_SGMAP_MAPPED_SIZE;
    133 	t->_next_window = NULL;
    134 	t->_boundary = 0;
    135 	t->_sgmap = &lcp->lc_sgmap;
    136 	t->_pfthresh = LCA_SGMAP_PFTHRESH;
    137 	t->_get_tag = lca_dma_get_tag;
    138 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    139 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    140 	t->_dmamap_load = lca_bus_dmamap_load_sgmap;
    141 	t->_dmamap_load_mbuf = lca_bus_dmamap_load_mbuf_sgmap;
    142 	t->_dmamap_load_uio = lca_bus_dmamap_load_uio_sgmap;
    143 	t->_dmamap_load_raw = lca_bus_dmamap_load_raw_sgmap;
    144 	t->_dmamap_unload = lca_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 	/* Initialize window 1 as a direct-mapped window. */
    154 	REGVAL64(LCA_IOC_W_BASE1) = LCA_DIRECT_MAPPED_BASE | IOC_W_BASE_WEN;
    155 	REGVAL64(LCA_IOC_W_MASK1) = IOC_W_MASK_1G;
    156 	alpha_mb();
    157 
    158 	/* Disable window 0 while we set it up. */
    159 	REGVAL64(LCA_IOC_W_BASE0) = 0;
    160 	alpha_mb();
    161 
    162 	/*
    163 	 * Initialize the SGMAP.
    164 	 */
    165 	alpha_sgmap_init(t, &lcp->lc_sgmap, "lca_sgmap",
    166 	    LCA_SGMAP_MAPPED_BASE, 0, LCA_SGMAP_MAPPED_SIZE,
    167 	    sizeof(uint64_t), NULL, 0);
    168 
    169 	/*
    170 	 * Set up window 0 as an 8MB SGMAP-mapped window
    171 	 * starting at 8MB.
    172 	 */
    173 	REGVAL64(LCA_IOC_W_BASE0) = LCA_SGMAP_MAPPED_BASE |
    174 	    IOC_W_BASE_SG | IOC_W_BASE_WEN;
    175 	REGVAL64(LCA_IOC_W_MASK0) = IOC_W_MASK_8M;
    176 	alpha_mb();
    177 
    178 	if ((lcp->lc_sgmap.aps_ptpa & IOC_W_T_BASE) !=
    179 	    lcp->lc_sgmap.aps_ptpa)
    180 		panic("lca_dma_init: bad page table address");
    181 	REGVAL64(LCA_IOC_W_T_BASE0) = lcp->lc_sgmap.aps_ptpa;
    182 	alpha_mb();
    183 
    184 	/* Enble the scatter/gather TLB. */
    185 	REGVAL64(LCA_IOC_TB_ENA) = IOC_TB_ENA_TEN;
    186 	alpha_mb();
    187 
    188 	LCA_TLB_INVALIDATE();
    189 }
    190 
    191 /*
    192  * Return the bus dma tag to be used for the specified bus type.
    193  * INTERNAL USE ONLY!
    194  */
    195 static bus_dma_tag_t
    196 lca_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
    197 {
    198 	struct lca_config *lcp = t->_cookie;
    199 
    200 	switch (bustype) {
    201 	case ALPHA_BUS_PCI:
    202 	case ALPHA_BUS_EISA:
    203 		/*
    204 		 * Systems with an LCA can only support 1G
    205 		 * of memory, so we use the direct-mapped window
    206 		 * on busses that have 32-bit DMA.
    207 		 */
    208 		return (&lcp->lc_dmat_direct);
    209 
    210 	case ALPHA_BUS_ISA:
    211 		/*
    212 		 * ISA doesn't have enough address bits to use
    213 		 * the direct-mapped DMA window, so we must use
    214 		 * SGMAPs.
    215 		 */
    216 		return (&lcp->lc_dmat_sgmap);
    217 
    218 	default:
    219 		panic("lca_dma_get_tag: shouldn't be here, really...");
    220 	}
    221 }
    222 
    223 /*
    224  * Load an LCA SGMAP-mapped DMA map with a linear buffer.
    225  */
    226 static int
    227 lca_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags)
    228 {
    229 	int error;
    230 
    231 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    232 	    t->_sgmap);
    233 	if (error == 0)
    234 		LCA_TLB_INVALIDATE();
    235 
    236 	return (error);
    237 }
    238 
    239 /*
    240  * Load an LCA SGMAP-mapped DMA map with an mbuf chain.
    241  */
    242 static int
    243 lca_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags)
    244 {
    245 	int error;
    246 
    247 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    248 	if (error == 0)
    249 		LCA_TLB_INVALIDATE();
    250 
    251 	return (error);
    252 }
    253 
    254 /*
    255  * Load an LCA SGMAP-mapped DMA map with a uio.
    256  */
    257 static int
    258 lca_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags)
    259 {
    260 	int error;
    261 
    262 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    263 	if (error == 0)
    264 		LCA_TLB_INVALIDATE();
    265 
    266 	return (error);
    267 }
    268 
    269 /*
    270  * Load an LCA SGMAP-mapped DMA map with raw memory.
    271  */
    272 static int
    273 lca_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)
    274 {
    275 	int error;
    276 
    277 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    278 	    t->_sgmap);
    279 	if (error == 0)
    280 		LCA_TLB_INVALIDATE();
    281 
    282 	return (error);
    283 }
    284 
    285 /*
    286  * Unload an LCA DMA map.
    287  */
    288 static void
    289 lca_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
    290 {
    291 
    292 	/*
    293 	 * Invalidate any SGMAP page table entries used by this
    294 	 * mapping.
    295 	 */
    296 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    297 	LCA_TLB_INVALIDATE();
    298 
    299 	/*
    300 	 * Do the generic bits of the unload.
    301 	 */
    302 	_bus_dmamap_unload_common(t, map);
    303 }
    304