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