1 1.25 thorpej /* $NetBSD: apecs_dma.c,v 1.25 2021/07/04 22:42:36 thorpej Exp $ */ 2 1.2 thorpej 3 1.2 thorpej /*- 4 1.5 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 1.2 thorpej * All rights reserved. 6 1.2 thorpej * 7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 1.2 thorpej * NASA Ames Research Center. 10 1.2 thorpej * 11 1.2 thorpej * Redistribution and use in source and binary forms, with or without 12 1.2 thorpej * modification, are permitted provided that the following conditions 13 1.2 thorpej * are met: 14 1.2 thorpej * 1. Redistributions of source code must retain the above copyright 15 1.2 thorpej * notice, this list of conditions and the following disclaimer. 16 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright 17 1.2 thorpej * notice, this list of conditions and the following disclaimer in the 18 1.2 thorpej * documentation and/or other materials provided with the distribution. 19 1.2 thorpej * 20 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE. 31 1.2 thorpej */ 32 1.2 thorpej 33 1.2 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 34 1.2 thorpej 35 1.25 thorpej __KERNEL_RCSID(0, "$NetBSD: apecs_dma.c,v 1.25 2021/07/04 22:42:36 thorpej Exp $"); 36 1.2 thorpej 37 1.2 thorpej #include <sys/param.h> 38 1.2 thorpej #include <sys/systm.h> 39 1.2 thorpej #include <sys/kernel.h> 40 1.2 thorpej #include <sys/device.h> 41 1.13 mrg 42 1.2 thorpej #define _ALPHA_BUS_DMA_PRIVATE 43 1.20 dyoung #include <sys/bus.h> 44 1.2 thorpej 45 1.2 thorpej #include <dev/pci/pcireg.h> 46 1.2 thorpej #include <dev/pci/pcivar.h> 47 1.2 thorpej #include <alpha/pci/apecsreg.h> 48 1.2 thorpej #include <alpha/pci/apecsvar.h> 49 1.2 thorpej 50 1.24 thorpej static bus_dma_tag_t apecs_dma_get_tag(bus_dma_tag_t, alpha_bus_t); 51 1.2 thorpej 52 1.24 thorpej static int apecs_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *, 53 1.24 thorpej bus_size_t, struct proc *, int); 54 1.2 thorpej 55 1.24 thorpej static int apecs_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t, 56 1.24 thorpej struct mbuf *, int); 57 1.2 thorpej 58 1.24 thorpej static int apecs_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t, 59 1.24 thorpej struct uio *, int); 60 1.2 thorpej 61 1.24 thorpej static int apecs_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t, 62 1.24 thorpej bus_dma_segment_t *, int, bus_size_t, int); 63 1.2 thorpej 64 1.24 thorpej static void apecs_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t); 65 1.2 thorpej 66 1.2 thorpej /* 67 1.9 thorpej * Direct-mapped window: 1G at 1G 68 1.2 thorpej */ 69 1.9 thorpej #define APECS_DIRECT_MAPPED_BASE (1*1024*1024*1024) 70 1.9 thorpej #define APECS_DIRECT_MAPPED_SIZE (1*1024*1024*1024) 71 1.2 thorpej 72 1.2 thorpej /* 73 1.9 thorpej * SGMAP window: 8M at 8M 74 1.2 thorpej */ 75 1.2 thorpej #define APECS_SGMAP_MAPPED_BASE (8*1024*1024) 76 1.9 thorpej #define APECS_SGMAP_MAPPED_SIZE (8*1024*1024) 77 1.2 thorpej 78 1.15 thorpej /* APECS has a 256-byte out-bound DMA prefetch threshold. */ 79 1.15 thorpej #define APECS_SGMAP_PFTHRESH 256 80 1.15 thorpej 81 1.2 thorpej /* 82 1.2 thorpej * Macro to flush APECS scatter/gather TLB. 83 1.2 thorpej */ 84 1.2 thorpej #define APECS_TLB_INVALIDATE() \ 85 1.2 thorpej do { \ 86 1.2 thorpej alpha_mb(); \ 87 1.2 thorpej REGVAL(EPIC_TBIA) = 0; \ 88 1.2 thorpej alpha_mb(); \ 89 1.2 thorpej } while (0) 90 1.2 thorpej 91 1.2 thorpej void 92 1.18 dsl apecs_dma_init(struct apecs_config *acp) 93 1.2 thorpej { 94 1.2 thorpej bus_addr_t tbase; 95 1.2 thorpej bus_dma_tag_t t; 96 1.2 thorpej 97 1.2 thorpej /* 98 1.2 thorpej * Initialize the DMA tag used for direct-mapped DMA. 99 1.2 thorpej */ 100 1.2 thorpej t = &acp->ac_dmat_direct; 101 1.2 thorpej t->_cookie = acp; 102 1.8 thorpej t->_wbase = APECS_DIRECT_MAPPED_BASE; 103 1.9 thorpej t->_wsize = APECS_DIRECT_MAPPED_SIZE; 104 1.9 thorpej t->_next_window = NULL; 105 1.10 thorpej t->_boundary = 0; 106 1.9 thorpej t->_sgmap = NULL; 107 1.2 thorpej t->_get_tag = apecs_dma_get_tag; 108 1.2 thorpej t->_dmamap_create = _bus_dmamap_create; 109 1.2 thorpej t->_dmamap_destroy = _bus_dmamap_destroy; 110 1.8 thorpej t->_dmamap_load = _bus_dmamap_load_direct; 111 1.8 thorpej t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct; 112 1.8 thorpej t->_dmamap_load_uio = _bus_dmamap_load_uio_direct; 113 1.8 thorpej t->_dmamap_load_raw = _bus_dmamap_load_raw_direct; 114 1.2 thorpej t->_dmamap_unload = _bus_dmamap_unload; 115 1.7 thorpej t->_dmamap_sync = _bus_dmamap_sync; 116 1.2 thorpej 117 1.2 thorpej t->_dmamem_alloc = _bus_dmamem_alloc; 118 1.2 thorpej t->_dmamem_free = _bus_dmamem_free; 119 1.2 thorpej t->_dmamem_map = _bus_dmamem_map; 120 1.2 thorpej t->_dmamem_unmap = _bus_dmamem_unmap; 121 1.2 thorpej t->_dmamem_mmap = _bus_dmamem_mmap; 122 1.2 thorpej 123 1.2 thorpej /* 124 1.2 thorpej * Initialize the DMA tag used for sgmap-mapped DMA. 125 1.2 thorpej */ 126 1.2 thorpej t = &acp->ac_dmat_sgmap; 127 1.2 thorpej t->_cookie = acp; 128 1.8 thorpej t->_wbase = APECS_SGMAP_MAPPED_BASE; 129 1.9 thorpej t->_wsize = APECS_SGMAP_MAPPED_SIZE; 130 1.9 thorpej t->_next_window = NULL; 131 1.10 thorpej t->_boundary = 0; 132 1.9 thorpej t->_sgmap = &acp->ac_sgmap; 133 1.15 thorpej t->_pfthresh = APECS_SGMAP_PFTHRESH; 134 1.2 thorpej t->_get_tag = apecs_dma_get_tag; 135 1.14 thorpej t->_dmamap_create = alpha_sgmap_dmamap_create; 136 1.14 thorpej t->_dmamap_destroy = alpha_sgmap_dmamap_destroy; 137 1.2 thorpej t->_dmamap_load = apecs_bus_dmamap_load_sgmap; 138 1.2 thorpej t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap; 139 1.2 thorpej t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap; 140 1.2 thorpej t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap; 141 1.2 thorpej t->_dmamap_unload = apecs_bus_dmamap_unload_sgmap; 142 1.7 thorpej t->_dmamap_sync = _bus_dmamap_sync; 143 1.2 thorpej 144 1.2 thorpej t->_dmamem_alloc = _bus_dmamem_alloc; 145 1.2 thorpej t->_dmamem_free = _bus_dmamem_free; 146 1.2 thorpej t->_dmamem_map = _bus_dmamem_map; 147 1.2 thorpej t->_dmamem_unmap = _bus_dmamem_unmap; 148 1.2 thorpej t->_dmamem_mmap = _bus_dmamem_mmap; 149 1.2 thorpej 150 1.2 thorpej /* 151 1.2 thorpej * The firmware has set up window 2 as a 1G direct-mapped DMA 152 1.2 thorpej * window beginning at 1G. We leave it alone. Disable 153 1.2 thorpej * window 1. 154 1.2 thorpej */ 155 1.2 thorpej REGVAL(EPIC_PCI_BASE_1) = 0; 156 1.2 thorpej alpha_mb(); 157 1.2 thorpej 158 1.2 thorpej /* 159 1.11 thorpej * Initialize the SGMAP. 160 1.2 thorpej */ 161 1.11 thorpej alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap", 162 1.11 thorpej APECS_SGMAP_MAPPED_BASE, 0, APECS_SGMAP_MAPPED_SIZE, 163 1.21 matt sizeof(uint64_t), NULL, 0); 164 1.2 thorpej 165 1.11 thorpej /* 166 1.11 thorpej * Set up window 1 as an 8MB SGMAP-mapped window 167 1.11 thorpej * starting at 8MB. 168 1.11 thorpej */ 169 1.11 thorpej REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE | 170 1.11 thorpej EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB; 171 1.11 thorpej alpha_mb(); 172 1.11 thorpej 173 1.11 thorpej REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M; 174 1.11 thorpej alpha_mb(); 175 1.11 thorpej 176 1.11 thorpej tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT; 177 1.11 thorpej if ((tbase & EPIC_TBASE_T_BASE) != tbase) 178 1.11 thorpej panic("apecs_dma_init: bad page table address"); 179 1.11 thorpej REGVAL(EPIC_TBASE_1) = tbase; 180 1.11 thorpej alpha_mb(); 181 1.11 thorpej 182 1.11 thorpej APECS_TLB_INVALIDATE(); 183 1.2 thorpej } 184 1.2 thorpej 185 1.2 thorpej /* 186 1.2 thorpej * Return the bus dma tag to be used for the specified bus type. 187 1.2 thorpej * INTERNAL USE ONLY! 188 1.2 thorpej */ 189 1.24 thorpej static bus_dma_tag_t 190 1.18 dsl apecs_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype) 191 1.2 thorpej { 192 1.2 thorpej struct apecs_config *acp = t->_cookie; 193 1.2 thorpej 194 1.2 thorpej switch (bustype) { 195 1.2 thorpej case ALPHA_BUS_PCI: 196 1.2 thorpej case ALPHA_BUS_EISA: 197 1.2 thorpej /* 198 1.2 thorpej * Systems with an APECS can only support 1G 199 1.2 thorpej * of memory, so we use the direct-mapped window 200 1.2 thorpej * on busses that have 32-bit DMA. 201 1.2 thorpej */ 202 1.2 thorpej return (&acp->ac_dmat_direct); 203 1.2 thorpej 204 1.2 thorpej case ALPHA_BUS_ISA: 205 1.2 thorpej /* 206 1.2 thorpej * ISA doesn't have enough address bits to use 207 1.2 thorpej * the direct-mapped DMA window, so we must use 208 1.2 thorpej * SGMAPs. 209 1.2 thorpej */ 210 1.2 thorpej return (&acp->ac_dmat_sgmap); 211 1.2 thorpej 212 1.2 thorpej default: 213 1.2 thorpej panic("apecs_dma_get_tag: shouldn't be here, really..."); 214 1.2 thorpej } 215 1.2 thorpej } 216 1.2 thorpej 217 1.2 thorpej /* 218 1.2 thorpej * Load an APECS SGMAP-mapped DMA map with a linear buffer. 219 1.2 thorpej */ 220 1.24 thorpej static int 221 1.18 dsl apecs_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags) 222 1.2 thorpej { 223 1.2 thorpej int error; 224 1.2 thorpej 225 1.2 thorpej error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags, 226 1.9 thorpej t->_sgmap); 227 1.2 thorpej if (error == 0) 228 1.2 thorpej APECS_TLB_INVALIDATE(); 229 1.2 thorpej 230 1.2 thorpej return (error); 231 1.2 thorpej } 232 1.2 thorpej 233 1.2 thorpej /* 234 1.2 thorpej * Load an APECS SGMAP-mapped DMA map with an mbuf chain. 235 1.2 thorpej */ 236 1.24 thorpej static int 237 1.18 dsl apecs_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags) 238 1.2 thorpej { 239 1.2 thorpej int error; 240 1.2 thorpej 241 1.9 thorpej error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap); 242 1.2 thorpej if (error == 0) 243 1.2 thorpej APECS_TLB_INVALIDATE(); 244 1.2 thorpej 245 1.2 thorpej return (error); 246 1.2 thorpej } 247 1.2 thorpej 248 1.2 thorpej /* 249 1.2 thorpej * Load an APECS SGMAP-mapped DMA map with a uio. 250 1.2 thorpej */ 251 1.24 thorpej static int 252 1.18 dsl apecs_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags) 253 1.2 thorpej { 254 1.2 thorpej int error; 255 1.2 thorpej 256 1.9 thorpej error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap); 257 1.2 thorpej if (error == 0) 258 1.2 thorpej APECS_TLB_INVALIDATE(); 259 1.2 thorpej 260 1.2 thorpej return (error); 261 1.2 thorpej } 262 1.2 thorpej 263 1.2 thorpej /* 264 1.2 thorpej * Load an APECS SGMAP-mapped DMA map with raw memory. 265 1.2 thorpej */ 266 1.24 thorpej static int 267 1.18 dsl apecs_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) 268 1.2 thorpej { 269 1.2 thorpej int error; 270 1.2 thorpej 271 1.2 thorpej error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags, 272 1.9 thorpej t->_sgmap); 273 1.2 thorpej if (error == 0) 274 1.2 thorpej APECS_TLB_INVALIDATE(); 275 1.2 thorpej 276 1.2 thorpej return (error); 277 1.2 thorpej } 278 1.2 thorpej 279 1.2 thorpej /* 280 1.2 thorpej * Unload an APECS DMA map. 281 1.2 thorpej */ 282 1.24 thorpej static void 283 1.18 dsl apecs_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map) 284 1.2 thorpej { 285 1.2 thorpej 286 1.2 thorpej /* 287 1.2 thorpej * Invalidate any SGMAP page table entries used by this 288 1.2 thorpej * mapping. 289 1.2 thorpej */ 290 1.9 thorpej pci_sgmap_pte64_unload(t, map, t->_sgmap); 291 1.2 thorpej APECS_TLB_INVALIDATE(); 292 1.2 thorpej 293 1.2 thorpej /* 294 1.2 thorpej * Do the generic bits of the unload. 295 1.2 thorpej */ 296 1.23 thorpej _bus_dmamap_unload_common(t, map); 297 1.2 thorpej } 298