1 /* $NetBSD: mcpcia_dma.c,v 1.15 2001/07/19 18:55:40 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 41 42 __KERNEL_RCSID(0, "$NetBSD: mcpcia_dma.c,v 1.15 2001/07/19 18:55:40 thorpej Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/device.h> 48 #include <sys/malloc.h> 49 50 #include <uvm/uvm_extern.h> 51 52 #define _ALPHA_BUS_DMA_PRIVATE 53 #include <machine/bus.h> 54 55 #include <dev/pci/pcireg.h> 56 #include <dev/pci/pcivar.h> 57 #include <alpha/pci/mcpciareg.h> 58 #include <alpha/pci/mcpciavar.h> 59 #include <alpha/pci/pci_kn300.h> 60 61 bus_dma_tag_t mcpcia_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t)); 62 63 int mcpcia_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *, 64 bus_size_t, struct proc *, int)); 65 66 int mcpcia_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t, 67 struct mbuf *, int)); 68 69 int mcpcia_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t, 70 struct uio *, int)); 71 72 int mcpcia_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t, 73 bus_dma_segment_t *, int, bus_size_t, int)); 74 75 void mcpcia_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t)); 76 77 /* 78 * Direct-mapped window: 2G at 2G 79 */ 80 #define MCPCIA_DIRECT_MAPPED_BASE (2UL*1024UL*1024UL*1024UL) 81 #define MCPCIA_DIRECT_MAPPED_SIZE (2UL*1024UL*1024UL*1024UL) 82 83 /* 84 * SGMAP window for PCI: 1G at 1G 85 */ 86 #define MCPCIA_PCI_SG_MAPPED_BASE (1UL*1024UL*1024UL*1024UL) 87 #define MCPCIA_PCI_SG_MAPPED_SIZE (1UL*1024UL*1024UL*1024UL) 88 89 /* 90 * SGMAP window for ISA: 8M at 8M 91 */ 92 #define MCPCIA_ISA_SG_MAPPED_BASE (8*1024*1024) 93 #define MCPCIA_ISA_SG_MAPPED_SIZE (8*1024*1024) 94 95 /* MCPCIA has a 256-byte out-bound DMA prefetch threshold. */ 96 #define MCPCIA_SG_MAPPED_PFTHRESH 256 97 98 #define MCPCIA_SGTLB_INVALIDATE(ccp) \ 99 do { \ 100 alpha_mb(); \ 101 REGVAL(MCPCIA_SG_TBIA(ccp)) = 0xdeadbeef; \ 102 alpha_mb(); \ 103 } while (0) 104 105 void 106 mcpcia_dma_init(ccp) 107 struct mcpcia_config *ccp; 108 { 109 bus_dma_tag_t t; 110 111 /* 112 * Initialize the DMA tag used for direct-mapped DMA. 113 */ 114 t = &ccp->cc_dmat_direct; 115 t->_cookie = ccp; 116 t->_wbase = MCPCIA_DIRECT_MAPPED_BASE; 117 t->_wsize = MCPCIA_DIRECT_MAPPED_SIZE; 118 t->_next_window = &ccp->cc_dmat_pci_sgmap; 119 t->_boundary = 0; 120 t->_sgmap = NULL; 121 t->_get_tag = mcpcia_dma_get_tag; 122 t->_dmamap_create = _bus_dmamap_create; 123 t->_dmamap_destroy = _bus_dmamap_destroy; 124 t->_dmamap_load = _bus_dmamap_load_direct; 125 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct; 126 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct; 127 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct; 128 t->_dmamap_unload = _bus_dmamap_unload; 129 t->_dmamap_sync = _bus_dmamap_sync; 130 131 t->_dmamem_alloc = _bus_dmamem_alloc; 132 t->_dmamem_free = _bus_dmamem_free; 133 t->_dmamem_map = _bus_dmamem_map; 134 t->_dmamem_unmap = _bus_dmamem_unmap; 135 t->_dmamem_mmap = _bus_dmamem_mmap; 136 137 /* 138 * Initialize the DMA tag used for sgmap-mapped PCI DMA. 139 */ 140 t = &ccp->cc_dmat_pci_sgmap; 141 t->_cookie = ccp; 142 t->_wbase = MCPCIA_PCI_SG_MAPPED_BASE; 143 t->_wsize = MCPCIA_PCI_SG_MAPPED_SIZE; 144 t->_next_window = NULL; 145 t->_boundary = 0; 146 t->_sgmap = &ccp->cc_pci_sgmap; 147 t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH; 148 t->_get_tag = mcpcia_dma_get_tag; 149 t->_dmamap_create = alpha_sgmap_dmamap_create; 150 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy; 151 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap; 152 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap; 153 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap; 154 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap; 155 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap; 156 t->_dmamap_sync = _bus_dmamap_sync; 157 158 t->_dmamem_alloc = _bus_dmamem_alloc; 159 t->_dmamem_free = _bus_dmamem_free; 160 t->_dmamem_map = _bus_dmamem_map; 161 t->_dmamem_unmap = _bus_dmamem_unmap; 162 t->_dmamem_mmap = _bus_dmamem_mmap; 163 164 /* 165 * Initialize the DMA tag used for sgmap-mapped ISA DMA. 166 */ 167 t = &ccp->cc_dmat_isa_sgmap; 168 t->_cookie = ccp; 169 t->_wbase = MCPCIA_ISA_SG_MAPPED_BASE; 170 t->_wsize = MCPCIA_ISA_SG_MAPPED_SIZE; 171 t->_next_window = NULL; 172 t->_boundary = 0; 173 t->_sgmap = &ccp->cc_isa_sgmap; 174 t->_pfthresh = MCPCIA_SG_MAPPED_PFTHRESH; 175 t->_get_tag = mcpcia_dma_get_tag; 176 t->_dmamap_create = alpha_sgmap_dmamap_create; 177 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy; 178 t->_dmamap_load = mcpcia_bus_dmamap_load_sgmap; 179 t->_dmamap_load_mbuf = mcpcia_bus_dmamap_load_mbuf_sgmap; 180 t->_dmamap_load_uio = mcpcia_bus_dmamap_load_uio_sgmap; 181 t->_dmamap_load_raw = mcpcia_bus_dmamap_load_raw_sgmap; 182 t->_dmamap_unload = mcpcia_bus_dmamap_unload_sgmap; 183 t->_dmamap_sync = _bus_dmamap_sync; 184 185 t->_dmamem_alloc = _bus_dmamem_alloc; 186 t->_dmamem_free = _bus_dmamem_free; 187 t->_dmamem_map = _bus_dmamem_map; 188 t->_dmamem_unmap = _bus_dmamem_unmap; 189 t->_dmamem_mmap = _bus_dmamem_mmap; 190 191 /* 192 * Initialize the SGMAPs. 193 */ 194 alpha_sgmap_init(&ccp->cc_dmat_pci_sgmap, &ccp->cc_pci_sgmap, 195 "mcpcia pci sgmap", 196 MCPCIA_PCI_SG_MAPPED_BASE, 0, MCPCIA_PCI_SG_MAPPED_SIZE, 197 sizeof(u_int64_t), NULL, 0); 198 199 alpha_sgmap_init(&ccp->cc_dmat_isa_sgmap, &ccp->cc_isa_sgmap, 200 "mcpcia isa sgmap", 201 MCPCIA_ISA_SG_MAPPED_BASE, 0, MCPCIA_ISA_SG_MAPPED_SIZE, 202 sizeof(u_int64_t), NULL, 0); 203 204 /* 205 * Disable windows first. 206 */ 207 208 REGVAL(MCPCIA_W0_BASE(ccp)) = 0; 209 REGVAL(MCPCIA_W1_BASE(ccp)) = 0; 210 REGVAL(MCPCIA_W2_BASE(ccp)) = 0; 211 REGVAL(MCPCIA_W3_BASE(ccp)) = 0; 212 REGVAL(MCPCIA_T0_BASE(ccp)) = 0; 213 REGVAL(MCPCIA_T1_BASE(ccp)) = 0; 214 REGVAL(MCPCIA_T2_BASE(ccp)) = 0; 215 REGVAL(MCPCIA_T3_BASE(ccp)) = 0; 216 alpha_mb(); 217 218 /* 219 * Set up window 0 as an 8MB SGMAP-mapped window starting at 8MB. 220 */ 221 REGVAL(MCPCIA_W0_MASK(ccp)) = MCPCIA_WMASK_8M; 222 REGVAL(MCPCIA_T0_BASE(ccp)) = 223 ccp->cc_isa_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT; 224 alpha_mb(); 225 REGVAL(MCPCIA_W0_BASE(ccp)) = 226 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_ISA_SG_MAPPED_BASE; 227 alpha_mb(); 228 229 MCPCIA_SGTLB_INVALIDATE(ccp); 230 231 /* 232 * Set up window 1 as a 2 GB Direct-mapped window starting at 2GB. 233 */ 234 235 REGVAL(MCPCIA_W1_MASK(ccp)) = MCPCIA_WMASK_2G; 236 REGVAL(MCPCIA_T1_BASE(ccp)) = 0; 237 alpha_mb(); 238 REGVAL(MCPCIA_W1_BASE(ccp)) = 239 MCPCIA_DIRECT_MAPPED_BASE | MCPCIA_WBASE_EN; 240 alpha_mb(); 241 242 /* 243 * Set up window 2 as a 1G SGMAP-mapped window starting at 1G. 244 */ 245 246 REGVAL(MCPCIA_W2_MASK(ccp)) = MCPCIA_WMASK_1G; 247 REGVAL(MCPCIA_T2_BASE(ccp)) = 248 ccp->cc_pci_sgmap.aps_ptpa >> MCPCIA_TBASEX_SHIFT; 249 alpha_mb(); 250 REGVAL(MCPCIA_W2_BASE(ccp)) = 251 MCPCIA_WBASE_EN | MCPCIA_WBASE_SG | MCPCIA_PCI_SG_MAPPED_BASE; 252 alpha_mb(); 253 254 /* XXX XXX BEGIN XXX XXX */ 255 { /* XXX */ 256 extern paddr_t alpha_XXX_dmamap_or; /* XXX */ 257 alpha_XXX_dmamap_or = MCPCIA_DIRECT_MAPPED_BASE;/* XXX */ 258 } /* XXX */ 259 /* XXX XXX END XXX XXX */ 260 } 261 262 /* 263 * Return the bus dma tag to be used for the specified bus type. 264 * INTERNAL USE ONLY! 265 */ 266 bus_dma_tag_t 267 mcpcia_dma_get_tag(t, bustype) 268 bus_dma_tag_t t; 269 alpha_bus_t bustype; 270 { 271 struct mcpcia_config *ccp = t->_cookie; 272 273 switch (bustype) { 274 case ALPHA_BUS_PCI: 275 case ALPHA_BUS_EISA: 276 /* 277 * Start off using the direct-mapped window. We will 278 * automatically fall backed onto the chained PCI SGMAP 279 * window if necessary. 280 */ 281 return (&ccp->cc_dmat_direct); 282 283 case ALPHA_BUS_ISA: 284 /* 285 * ISA doesn't have enough address bits to use 286 * the direct-mapped DMA window, so we must use 287 * SGMAPs. 288 */ 289 return (&ccp->cc_dmat_isa_sgmap); 290 291 default: 292 panic("mcpcia_dma_get_tag: shouldn't be here, really..."); 293 } 294 } 295 296 /* 297 * Load a MCPCIA SGMAP-mapped DMA map with a linear buffer. 298 */ 299 int 300 mcpcia_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags) 301 bus_dma_tag_t t; 302 bus_dmamap_t map; 303 void *buf; 304 bus_size_t buflen; 305 struct proc *p; 306 int flags; 307 { 308 int error; 309 struct mcpcia_config *ccp = t->_cookie; 310 311 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags, 312 t->_sgmap); 313 if (error == 0) 314 MCPCIA_SGTLB_INVALIDATE(ccp); 315 return (error); 316 } 317 318 /* 319 * Load a MCPCIA SGMAP-mapped DMA map with an mbuf chain. 320 */ 321 int 322 mcpcia_bus_dmamap_load_mbuf_sgmap(t, map, m, flags) 323 bus_dma_tag_t t; 324 bus_dmamap_t map; 325 struct mbuf *m; 326 int flags; 327 { 328 int error; 329 struct mcpcia_config *ccp = t->_cookie; 330 331 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap); 332 if (error == 0) 333 MCPCIA_SGTLB_INVALIDATE(ccp); 334 return (error); 335 } 336 337 /* 338 * Load a MCPCIA SGMAP-mapped DMA map with a uio. 339 */ 340 int 341 mcpcia_bus_dmamap_load_uio_sgmap(t, map, uio, flags) 342 bus_dma_tag_t t; 343 bus_dmamap_t map; 344 struct uio *uio; 345 int flags; 346 { 347 int error; 348 struct mcpcia_config *ccp = t->_cookie; 349 350 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap); 351 if (error == 0) 352 MCPCIA_SGTLB_INVALIDATE(ccp); 353 return (error); 354 } 355 356 /* 357 * Load a MCPCIA SGMAP-mapped DMA map with raw memory. 358 */ 359 int 360 mcpcia_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags) 361 bus_dma_tag_t t; 362 bus_dmamap_t map; 363 bus_dma_segment_t *segs; 364 int nsegs; 365 bus_size_t size; 366 int flags; 367 { 368 int error; 369 struct mcpcia_config *ccp = t->_cookie; 370 371 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags, 372 t->_sgmap); 373 if (error == 0) 374 MCPCIA_SGTLB_INVALIDATE(ccp); 375 return (error); 376 } 377 378 /* 379 * Unload a MCPCIA DMA map. 380 */ 381 void 382 mcpcia_bus_dmamap_unload_sgmap(t, map) 383 bus_dma_tag_t t; 384 bus_dmamap_t map; 385 { 386 struct mcpcia_config *ccp = t->_cookie; 387 388 /* 389 * Invalidate any SGMAP page table entries used by this mapping. 390 */ 391 pci_sgmap_pte64_unload(t, map, t->_sgmap); 392 MCPCIA_SGTLB_INVALIDATE(ccp); 393 394 /* 395 * Do the generic bits of the unload. 396 */ 397 _bus_dmamap_unload(t, map); 398 } 399