1 1.11 thorpej /* $NetBSD: ttwoga_dma.c,v 1.11 2021/05/08 00:08:43 thorpej Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.1 thorpej * by Jason R. Thorpe. 9 1.1 thorpej * 10 1.1 thorpej * Redistribution and use in source and binary forms, with or without 11 1.1 thorpej * modification, are permitted provided that the following conditions 12 1.1 thorpej * are met: 13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 14 1.1 thorpej * notice, this list of conditions and the following disclaimer. 15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 17 1.1 thorpej * documentation and/or other materials provided with the distribution. 18 1.1 thorpej * 19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 30 1.1 thorpej */ 31 1.1 thorpej 32 1.1 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 33 1.1 thorpej 34 1.11 thorpej __KERNEL_RCSID(0, "$NetBSD: ttwoga_dma.c,v 1.11 2021/05/08 00:08:43 thorpej Exp $"); 35 1.1 thorpej 36 1.1 thorpej #include <sys/param.h> 37 1.1 thorpej #include <sys/systm.h> 38 1.1 thorpej #include <sys/kernel.h> 39 1.7 matt #include <sys/device.h> 40 1.1 thorpej 41 1.1 thorpej #define _ALPHA_BUS_DMA_PRIVATE 42 1.6 dyoung #include <sys/bus.h> 43 1.1 thorpej 44 1.7 matt #include <dev/pci/pcireg.h> 45 1.1 thorpej #include <dev/pci/pcivar.h> 46 1.1 thorpej 47 1.1 thorpej #include <alpha/pci/ttwogareg.h> 48 1.1 thorpej #include <alpha/pci/ttwogavar.h> 49 1.1 thorpej 50 1.10 thorpej static bus_dma_tag_t ttwoga_dma_get_tag(bus_dma_tag_t, alpha_bus_t); 51 1.1 thorpej 52 1.10 thorpej static int ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, 53 1.10 thorpej void *, bus_size_t, struct proc *, int); 54 1.1 thorpej 55 1.10 thorpej static int ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t, 56 1.10 thorpej struct mbuf *, int); 57 1.1 thorpej 58 1.10 thorpej static int ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t, 59 1.10 thorpej struct uio *, int); 60 1.1 thorpej 61 1.10 thorpej static int ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t, 62 1.10 thorpej bus_dma_segment_t *, int, bus_size_t, int); 63 1.1 thorpej 64 1.10 thorpej static void ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t); 65 1.1 thorpej 66 1.1 thorpej /* 67 1.1 thorpej * Direct-mapped window: 1G at 1G 68 1.1 thorpej */ 69 1.1 thorpej #define TTWOGA_DIRECT_MAPPED_BASE (1UL*1024UL*1024UL*1024UL) 70 1.1 thorpej #define TTWOGA_DIRECT_MAPPED_SIZE (1UL*1024UL*1024UL*1024UL) 71 1.1 thorpej 72 1.1 thorpej /* 73 1.1 thorpej * SGMAP window: 8M at 8M 74 1.1 thorpej */ 75 1.1 thorpej #define TTWOGA_SGMAP_MAPPED_BASE (8UL*1024UL*1024UL) 76 1.1 thorpej #define TTWOGA_SGMAP_MAPPED_SIZE (8UL*1024UL*1024UL) 77 1.1 thorpej 78 1.3 thorpej /* T2 has a 256-byte out-bound DMA prefetch threshold. */ 79 1.3 thorpej #define TTWOGA_SGMAP_PFTHRESH 256 80 1.3 thorpej 81 1.1 thorpej /* 82 1.1 thorpej * Macro to flush the T2 Gate Array scatter/gather TLB. 83 1.1 thorpej */ 84 1.1 thorpej #define TTWOGA_TLB_INVALIDATE(tcp) \ 85 1.1 thorpej do { \ 86 1.7 matt uint64_t temp; \ 87 1.1 thorpej \ 88 1.1 thorpej alpha_mb(); \ 89 1.1 thorpej temp = T2GA((tcp), T2_IOCSR); \ 90 1.1 thorpej T2GA((tcp), T2_IOCSR) = temp | IOCSR_FTLB; \ 91 1.1 thorpej alpha_mb(); \ 92 1.1 thorpej alpha_mb(); /* MAGIC */ \ 93 1.1 thorpej T2GA((tcp), T2_IOCSR) = temp; \ 94 1.1 thorpej alpha_mb(); \ 95 1.1 thorpej alpha_mb(); /* MAGIC */ \ 96 1.1 thorpej } while (0) 97 1.1 thorpej 98 1.1 thorpej void 99 1.1 thorpej ttwoga_dma_init(struct ttwoga_config *tcp) 100 1.1 thorpej { 101 1.1 thorpej bus_dma_tag_t t; 102 1.1 thorpej 103 1.1 thorpej /* 104 1.1 thorpej * Initialize the DMA tag used for direct-mapped DMA. 105 1.1 thorpej */ 106 1.1 thorpej t = &tcp->tc_dmat_direct; 107 1.1 thorpej t->_cookie = tcp; 108 1.1 thorpej t->_wbase = TTWOGA_DIRECT_MAPPED_BASE; 109 1.1 thorpej t->_wsize = TTWOGA_DIRECT_MAPPED_SIZE; 110 1.1 thorpej t->_next_window = NULL; 111 1.1 thorpej t->_boundary = 0; 112 1.1 thorpej t->_sgmap = NULL; 113 1.1 thorpej t->_get_tag = ttwoga_dma_get_tag; 114 1.1 thorpej t->_dmamap_create = _bus_dmamap_create; 115 1.1 thorpej t->_dmamap_destroy = _bus_dmamap_destroy; 116 1.1 thorpej t->_dmamap_load = _bus_dmamap_load_direct; 117 1.1 thorpej t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct; 118 1.1 thorpej t->_dmamap_load_uio = _bus_dmamap_load_uio_direct; 119 1.1 thorpej t->_dmamap_load_raw = _bus_dmamap_load_raw_direct; 120 1.1 thorpej t->_dmamap_unload = _bus_dmamap_unload; 121 1.1 thorpej t->_dmamap_sync = _bus_dmamap_sync; 122 1.1 thorpej 123 1.1 thorpej t->_dmamem_alloc = _bus_dmamem_alloc; 124 1.1 thorpej t->_dmamem_free = _bus_dmamem_free; 125 1.1 thorpej t->_dmamem_map = _bus_dmamem_map; 126 1.1 thorpej t->_dmamem_unmap = _bus_dmamem_unmap; 127 1.1 thorpej t->_dmamem_mmap = _bus_dmamem_mmap; 128 1.1 thorpej 129 1.1 thorpej /* 130 1.1 thorpej * Initialize the DMA tag used for sgmap-mapped DMA. 131 1.1 thorpej */ 132 1.1 thorpej t = &tcp->tc_dmat_sgmap; 133 1.1 thorpej t->_cookie = tcp; 134 1.1 thorpej t->_wbase = TTWOGA_SGMAP_MAPPED_BASE; 135 1.1 thorpej t->_wsize = TTWOGA_SGMAP_MAPPED_SIZE; 136 1.1 thorpej t->_next_window = NULL; 137 1.1 thorpej t->_boundary = 0; 138 1.1 thorpej t->_sgmap = &tcp->tc_sgmap; 139 1.3 thorpej t->_pfthresh = TTWOGA_SGMAP_PFTHRESH; 140 1.1 thorpej t->_get_tag = ttwoga_dma_get_tag; 141 1.2 thorpej t->_dmamap_create = alpha_sgmap_dmamap_create; 142 1.2 thorpej t->_dmamap_destroy = alpha_sgmap_dmamap_destroy; 143 1.1 thorpej t->_dmamap_load = ttwoga_bus_dmamap_load_sgmap; 144 1.1 thorpej t->_dmamap_load_mbuf = ttwoga_bus_dmamap_load_mbuf_sgmap; 145 1.1 thorpej t->_dmamap_load_uio = ttwoga_bus_dmamap_load_uio_sgmap; 146 1.1 thorpej t->_dmamap_load_raw = ttwoga_bus_dmamap_load_raw_sgmap; 147 1.1 thorpej t->_dmamap_unload = ttwoga_bus_dmamap_unload_sgmap; 148 1.1 thorpej t->_dmamap_sync = _bus_dmamap_sync; 149 1.1 thorpej 150 1.1 thorpej t->_dmamem_alloc = _bus_dmamem_alloc; 151 1.1 thorpej t->_dmamem_free = _bus_dmamem_free; 152 1.1 thorpej t->_dmamem_map = _bus_dmamem_map; 153 1.1 thorpej t->_dmamem_unmap = _bus_dmamem_unmap; 154 1.1 thorpej t->_dmamem_mmap = _bus_dmamem_mmap; 155 1.1 thorpej 156 1.1 thorpej /* 157 1.1 thorpej * Disable the SGMAP TLB, and flush it. We reenable it if 158 1.1 thorpej * we have a Sable or a Gamma with T3 or T4; Gamma with T2 159 1.1 thorpej * has a TLB bug apparently severe enough to require disabling 160 1.1 thorpej * it. 161 1.1 thorpej */ 162 1.1 thorpej alpha_mb(); 163 1.1 thorpej T2GA(tcp, T2_IOCSR) &= ~IOCSR_ETLB; 164 1.1 thorpej alpha_mb(); 165 1.1 thorpej alpha_mb(); /* MAGIC */ 166 1.1 thorpej 167 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp); 168 1.1 thorpej 169 1.1 thorpej /* 170 1.1 thorpej * XXX We might want to make sure our DMA windows don't 171 1.1 thorpej * XXX overlap with PCI memory space! 172 1.1 thorpej */ 173 1.1 thorpej 174 1.1 thorpej /* 175 1.1 thorpej * Set up window 1 as a 1G direct-mapped window 176 1.1 thorpej * starting at 1G. 177 1.1 thorpej */ 178 1.1 thorpej T2GA(tcp, T2_WBASE1) = 0; 179 1.1 thorpej alpha_mb(); 180 1.1 thorpej 181 1.1 thorpej T2GA(tcp, T2_WMASK1) = (TTWOGA_DIRECT_MAPPED_SIZE - 1) & WMASKx_PWM; 182 1.1 thorpej alpha_mb(); 183 1.1 thorpej 184 1.1 thorpej T2GA(tcp, T2_TBASE1) = 0; 185 1.1 thorpej alpha_mb(); 186 1.1 thorpej 187 1.1 thorpej T2GA(tcp, T2_WBASE1) = TTWOGA_DIRECT_MAPPED_BASE | 188 1.1 thorpej ((TTWOGA_DIRECT_MAPPED_BASE + (TTWOGA_DIRECT_MAPPED_SIZE - 1)) >> 189 1.1 thorpej WBASEx_PWxA_SHIFT) | WBASEx_PWE; 190 1.1 thorpej alpha_mb(); 191 1.1 thorpej 192 1.1 thorpej /* 193 1.1 thorpej * Initialize the SGMAP. 194 1.1 thorpej */ 195 1.1 thorpej alpha_sgmap_init(t, &tcp->tc_sgmap, "ttwoga_sgmap", 196 1.1 thorpej TTWOGA_SGMAP_MAPPED_BASE, 0, TTWOGA_SGMAP_MAPPED_SIZE, 197 1.7 matt sizeof(uint64_t), NULL, 0); 198 1.1 thorpej 199 1.1 thorpej /* 200 1.1 thorpej * Set up window 2 as an 8MB SGMAP-mapped window 201 1.1 thorpej * starting at 8MB. 202 1.1 thorpej */ 203 1.1 thorpej #ifdef DIAGNOSTIC 204 1.1 thorpej if ((TTWOGA_SGMAP_MAPPED_BASE & WBASEx_PWSA) != 205 1.1 thorpej TTWOGA_SGMAP_MAPPED_BASE) 206 1.1 thorpej panic("ttwoga_dma_init: SGMAP base inconsistency"); 207 1.1 thorpej #endif 208 1.1 thorpej T2GA(tcp, T2_WBASE2) = 0; 209 1.1 thorpej alpha_mb(); 210 1.1 thorpej 211 1.1 thorpej T2GA(tcp, T2_WMASK2) = (TTWOGA_SGMAP_MAPPED_SIZE - 1) & WMASKx_PWM; 212 1.1 thorpej alpha_mb(); 213 1.1 thorpej 214 1.1 thorpej T2GA(tcp, T2_TBASE2) = tcp->tc_sgmap.aps_ptpa >> 1; 215 1.1 thorpej alpha_mb(); 216 1.1 thorpej 217 1.1 thorpej T2GA(tcp, T2_WBASE2) = TTWOGA_SGMAP_MAPPED_BASE | 218 1.1 thorpej ((TTWOGA_SGMAP_MAPPED_BASE + (TTWOGA_SGMAP_MAPPED_SIZE - 1)) >> 219 1.1 thorpej WBASEx_PWxA_SHIFT) | WBASEx_SGE | WBASEx_PWE; 220 1.1 thorpej alpha_mb(); 221 1.1 thorpej 222 1.1 thorpej /* 223 1.1 thorpej * Enable SGMAP TLB on Sable or Gamma with T3 or T4; see above. 224 1.1 thorpej */ 225 1.1 thorpej if (alpha_implver() == ALPHA_IMPLVER_EV4 || 226 1.1 thorpej tcp->tc_rev >= TRN_T3) { 227 1.1 thorpej alpha_mb(); 228 1.1 thorpej T2GA(tcp, T2_IOCSR) |= IOCSR_ETLB; 229 1.1 thorpej alpha_mb(); 230 1.1 thorpej alpha_mb(); /* MAGIC */ 231 1.1 thorpej tcp->tc_use_tlb = 1; 232 1.1 thorpej } 233 1.1 thorpej } 234 1.1 thorpej 235 1.1 thorpej /* 236 1.1 thorpej * Return the bus dma tag to be used for the specified bus type. 237 1.1 thorpej * INTERNAL USE ONLY! 238 1.1 thorpej */ 239 1.10 thorpej static bus_dma_tag_t 240 1.1 thorpej ttwoga_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype) 241 1.1 thorpej { 242 1.1 thorpej struct ttwoga_config *tcp = t->_cookie; 243 1.1 thorpej 244 1.1 thorpej switch (bustype) { 245 1.1 thorpej case ALPHA_BUS_PCI: 246 1.1 thorpej case ALPHA_BUS_EISA: 247 1.1 thorpej /* 248 1.1 thorpej * Systems with a T2 Gate Array can have 2G 249 1.1 thorpej * of memory, but we only get a direct-mapped 250 1.1 thorpej * window of 1G! 251 1.1 thorpej * 252 1.1 thorpej * XXX FIX THIS SOMEDAY! 253 1.1 thorpej */ 254 1.1 thorpej return (&tcp->tc_dmat_direct); 255 1.1 thorpej 256 1.1 thorpej case ALPHA_BUS_ISA: 257 1.1 thorpej /* 258 1.1 thorpej * ISA doesn't have enough address bits to use 259 1.1 thorpej * the direct-mapped DMA window, so we must use 260 1.1 thorpej * SGMAPs. 261 1.1 thorpej */ 262 1.1 thorpej return (&tcp->tc_dmat_sgmap); 263 1.1 thorpej 264 1.1 thorpej default: 265 1.1 thorpej panic("ttwoga_dma_get_tag: shouldn't be here, really..."); 266 1.1 thorpej } 267 1.1 thorpej } 268 1.1 thorpej 269 1.1 thorpej /* 270 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with a liner buffer. 271 1.1 thorpej */ 272 1.10 thorpej static int 273 1.1 thorpej ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 274 1.1 thorpej bus_size_t buflen, struct proc *p, int flags) 275 1.1 thorpej { 276 1.1 thorpej struct ttwoga_config *tcp = t->_cookie; 277 1.1 thorpej int error; 278 1.1 thorpej 279 1.1 thorpej error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags, 280 1.1 thorpej t->_sgmap); 281 1.1 thorpej if (error == 0 && tcp->tc_use_tlb) 282 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp); 283 1.1 thorpej 284 1.1 thorpej return (error); 285 1.1 thorpej } 286 1.1 thorpej 287 1.1 thorpej /* 288 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with an mbuf chain. 289 1.1 thorpej */ 290 1.10 thorpej static int 291 1.1 thorpej ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, 292 1.1 thorpej struct mbuf *m, int flags) 293 1.1 thorpej { 294 1.1 thorpej struct ttwoga_config *tcp = t->_cookie; 295 1.1 thorpej int error; 296 1.1 thorpej 297 1.1 thorpej error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap); 298 1.1 thorpej if (error == 0 && tcp->tc_use_tlb) 299 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp); 300 1.1 thorpej 301 1.1 thorpej return (error); 302 1.1 thorpej } 303 1.1 thorpej 304 1.1 thorpej /* 305 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with a uio. 306 1.1 thorpej */ 307 1.10 thorpej static int 308 1.1 thorpej ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, 309 1.1 thorpej struct uio *uio, int flags) 310 1.1 thorpej { 311 1.1 thorpej struct ttwoga_config *tcp = t->_cookie; 312 1.1 thorpej int error; 313 1.1 thorpej 314 1.1 thorpej error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap); 315 1.1 thorpej if (error == 0 && tcp->tc_use_tlb) 316 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp); 317 1.1 thorpej 318 1.1 thorpej return (error); 319 1.1 thorpej } 320 1.1 thorpej 321 1.1 thorpej /* 322 1.1 thorpej * Load a T2 SGMAP-mapped DMA map with raw memory. 323 1.1 thorpej */ 324 1.10 thorpej static int 325 1.1 thorpej ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map, 326 1.1 thorpej bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 327 1.1 thorpej { 328 1.1 thorpej struct ttwoga_config *tcp = t->_cookie; 329 1.1 thorpej int error; 330 1.1 thorpej 331 1.1 thorpej error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags, 332 1.1 thorpej t->_sgmap); 333 1.1 thorpej if (error == 0 && tcp->tc_use_tlb) 334 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp); 335 1.1 thorpej 336 1.1 thorpej return (error); 337 1.1 thorpej } 338 1.1 thorpej 339 1.1 thorpej /* 340 1.1 thorpej * Unload an T2 DMA map. 341 1.1 thorpej */ 342 1.11 thorpej static void 343 1.1 thorpej ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map) 344 1.1 thorpej { 345 1.1 thorpej struct ttwoga_config *tcp = t->_cookie; 346 1.1 thorpej 347 1.1 thorpej /* 348 1.1 thorpej * Invalidate any SGMAP page table entries used by this 349 1.1 thorpej * mapping. 350 1.1 thorpej */ 351 1.1 thorpej pci_sgmap_pte64_unload(t, map, t->_sgmap); 352 1.1 thorpej if (tcp->tc_use_tlb) 353 1.1 thorpej TTWOGA_TLB_INVALIDATE(tcp); 354 1.1 thorpej 355 1.1 thorpej /* 356 1.1 thorpej * Do the generic bits of the unload. 357 1.1 thorpej */ 358 1.9 thorpej _bus_dmamap_unload_common(t, map); 359 1.1 thorpej } 360