1 1.22 thorpej /* $NetBSD: tsp_dma.c,v 1.22 2021/07/19 01:06:14 thorpej Exp $ */ 2 1.17 thorpej 3 1.17 thorpej /*- 4 1.17 thorpej * Copyright (c) 1997, 1998, 2021 The NetBSD Foundation, Inc. 5 1.17 thorpej * All rights reserved. 6 1.17 thorpej * 7 1.17 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.17 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 1.17 thorpej * NASA Ames Research Center. 10 1.17 thorpej * 11 1.17 thorpej * Redistribution and use in source and binary forms, with or without 12 1.17 thorpej * modification, are permitted provided that the following conditions 13 1.17 thorpej * are met: 14 1.17 thorpej * 1. Redistributions of source code must retain the above copyright 15 1.17 thorpej * notice, this list of conditions and the following disclaimer. 16 1.17 thorpej * 2. Redistributions in binary form must reproduce the above copyright 17 1.17 thorpej * notice, this list of conditions and the following disclaimer in the 18 1.17 thorpej * documentation and/or other materials provided with the distribution. 19 1.17 thorpej * 20 1.17 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.17 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.17 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.17 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.17 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.17 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.17 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.17 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.17 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.17 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.17 thorpej * POSSIBILITY OF SUCH DAMAGE. 31 1.17 thorpej */ 32 1.1 ross 33 1.1 ross /*- 34 1.1 ross * Copyright (c) 1999 by Ross Harvey. All rights reserved. 35 1.1 ross * 36 1.1 ross * Redistribution and use in source and binary forms, with or without 37 1.1 ross * modification, are permitted provided that the following conditions 38 1.1 ross * are met: 39 1.1 ross * 1. Redistributions of source code must retain the above copyright 40 1.1 ross * notice, this list of conditions and the following disclaimer. 41 1.1 ross * 2. Redistributions in binary form must reproduce the above copyright 42 1.1 ross * notice, this list of conditions and the following disclaimer in the 43 1.1 ross * documentation and/or other materials provided with the distribution. 44 1.1 ross * 3. All advertising materials mentioning features or use of this software 45 1.1 ross * must display the following acknowledgement: 46 1.1 ross * This product includes software developed by Ross Harvey. 47 1.1 ross * 4. The name of Ross Harvey may not be used to endorse or promote products 48 1.1 ross * derived from this software without specific prior written permission. 49 1.1 ross * 50 1.1 ross * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS 51 1.1 ross * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 52 1.1 ross * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE 53 1.1 ross * ARE DISCLAIMED. IN NO EVENT SHALL ROSS HARVEY BE LIABLE FOR ANY 54 1.1 ross * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 1.1 ross * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 1.1 ross * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 1.1 ross * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 1.1 ross * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 1.1 ross * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 1.1 ross * SUCH DAMAGE. 61 1.1 ross */ 62 1.5 lukem 63 1.5 lukem #include <sys/cdefs.h> 64 1.22 thorpej __KERNEL_RCSID(0, "$NetBSD: tsp_dma.c,v 1.22 2021/07/19 01:06:14 thorpej Exp $"); 65 1.1 ross 66 1.1 ross #include <sys/param.h> 67 1.1 ross #include <sys/systm.h> 68 1.1 ross #include <sys/kernel.h> 69 1.1 ross #include <sys/device.h> 70 1.2 mrg 71 1.19 thorpej #include <uvm/uvm_extern.h> 72 1.19 thorpej 73 1.1 ross #include <machine/autoconf.h> 74 1.1 ross #define _ALPHA_BUS_DMA_PRIVATE 75 1.11 dyoung #include <sys/bus.h> 76 1.1 ross #include <machine/rpb.h> 77 1.1 ross 78 1.1 ross #include <dev/pci/pcireg.h> 79 1.1 ross #include <dev/pci/pcivar.h> 80 1.1 ross #include <alpha/pci/tsreg.h> 81 1.1 ross #include <alpha/pci/tsvar.h> 82 1.1 ross 83 1.1 ross #define tsp_dma() { Generate ctags(1) key. } 84 1.1 ross 85 1.16 thorpej static bus_dma_tag_t tsp_dma_get_tag(bus_dma_tag_t, alpha_bus_t); 86 1.1 ross 87 1.16 thorpej static int tsp_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *, 88 1.16 thorpej bus_size_t, struct proc *, int); 89 1.1 ross 90 1.16 thorpej static int tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t, 91 1.16 thorpej struct mbuf *, int); 92 1.1 ross 93 1.16 thorpej static int tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t, 94 1.16 thorpej struct uio *, int); 95 1.1 ross 96 1.16 thorpej static int tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t, 97 1.16 thorpej bus_dma_segment_t *, int, bus_size_t, int); 98 1.1 ross 99 1.16 thorpej static void tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t); 100 1.1 ross 101 1.16 thorpej static void tsp_tlb_invalidate(struct tsp_config *); 102 1.1 ross 103 1.4 thorpej /* 104 1.4 thorpej * XXX Need to figure out what this is, if any. Initialize it to 105 1.4 thorpej * XXX something that should be safe. 106 1.4 thorpej */ 107 1.4 thorpej #define TSP_SGMAP_PFTHRESH 256 108 1.4 thorpej 109 1.17 thorpej /* 110 1.17 thorpej * Quoting the 21272 programmer's reference manual: 111 1.17 thorpej * 112 1.17 thorpej * <quote> 113 1.17 thorpej * 10.1.4.4 Monster Window DMA Address Translation 114 1.17 thorpej * 115 1.17 thorpej * In case of a PCI dual-address cycle command, the high-order PCI address 116 1.17 thorpej * bits <63:40> are compared to the constant value 0x0000_01 (that is, bit 117 1.17 thorpej * <40> = 1; all other bits = 0). If these bits match, a monster window hit 118 1.17 thorpej * has occurred and the low-order PCI address bits <34:0> are used unchanged 119 1.17 thorpej * as the system address bits <34:0>. PCI address bits <39:35> are ignored. 120 1.17 thorpej * The high-order 32 PCI address bits are available on b_ad<31:0> in the 121 1.17 thorpej * second cycle of a DAC, and also on b_ad<63:32> in the first cycle of a 122 1.17 thorpej * DAC if b_req64_l is asserted. 123 1.17 thorpej * </quote> 124 1.17 thorpej * 125 1.17 thorpej * This means that we can address up to 32GB of RAM using a direct-mapped 126 1.17 thorpej * 64-bit DMA tag. This leaves us possibly having to fall back on SGMAP 127 1.17 thorpej * DMA on a Titan system (those support up to 64GB of RAM), and we may have 128 1.17 thorpej * to address that with an additional large SGMAP DAC window at another 129 1.19 thorpej * time. XXX Does the Titan Monster Window support the extra bit? 130 1.17 thorpej */ 131 1.17 thorpej #define TSP_MONSTER_DMA_WINDOW_BASE 0x100##00000000UL 132 1.17 thorpej #define TSP_MONSTER_DMA_WINDOW_SIZE 0x008##00000000UL 133 1.17 thorpej 134 1.19 thorpej /* 135 1.19 thorpej * Basic 24-bit ISA DMA window is 8MB @ 8MB. The firmware will 136 1.19 thorpej * have set this up in Window 0. 137 1.19 thorpej */ 138 1.19 thorpej #define TSP_SGMAP_MAPPED_LO_BASE (8UL * 1024 * 1024) 139 1.19 thorpej #define TSP_SGMAP_MAPPED_LO_SIZE (8UL * 1024 * 1024) 140 1.19 thorpej 141 1.19 thorpej /* 142 1.19 thorpej * Basic 32-bit PCI DMA window is 1GB @ 2GB. The firmware will 143 1.19 thorpej * have set this up in Window 1. 144 1.19 thorpej */ 145 1.19 thorpej #define TSP_DIRECT_MAPPED_BASE (2UL * 1024 * 1024 * 1024) 146 1.19 thorpej #define TSP_DIRECT_MAPPED_SIZE (1UL * 1024 * 1024 * 1024) 147 1.19 thorpej 148 1.19 thorpej /* 149 1.19 thorpej * For systems that have > 1GB of RAM, but PCI devices that don't 150 1.19 thorpej * support dual-address cycle, we will also set up an additional 151 1.19 thorpej * SGMAP DMA window 1GB @ 3GB. We will use Window 2 for this purpose. 152 1.19 thorpej */ 153 1.19 thorpej #define TSP_SGMAP_MAPPED_HI_BASE (3UL * 1024 * 1024 * 1024) 154 1.19 thorpej #define TSP_SGMAP_MAPPED_HI_SIZE (1UL * 1024 * 1024 * 1024) 155 1.19 thorpej 156 1.19 thorpej /* 157 1.19 thorpej * Window 3 is still available for use in the future. Window 3 supports 158 1.19 thorpej * dual address cycle. 159 1.19 thorpej */ 160 1.19 thorpej 161 1.22 thorpej static void 162 1.22 thorpej tsp_dma_shutdown(void *arg) 163 1.22 thorpej { 164 1.22 thorpej struct tsp_config *pcp = arg; 165 1.22 thorpej struct ts_pchip *pccsr = pcp->pc_csr; 166 1.22 thorpej int i; 167 1.22 thorpej 168 1.22 thorpej /* 169 1.22 thorpej * Restore the initial values, to make the firmware happy. 170 1.22 thorpej */ 171 1.22 thorpej for (i = 0; i < 4; i++) { 172 1.22 thorpej pccsr->tsp_wsba[i].tsg_r = pcp->pc_saved_windows.wsba[i]; 173 1.22 thorpej pccsr->tsp_wsm[i].tsg_r = pcp->pc_saved_windows.wsm[i]; 174 1.22 thorpej pccsr->tsp_tba[i].tsg_r = pcp->pc_saved_windows.tba[i]; 175 1.22 thorpej alpha_mb(); 176 1.22 thorpej } 177 1.22 thorpej pccsr->tsp_pctl.tsg_r = pcp->pc_saved_pctl; 178 1.22 thorpej alpha_mb(); 179 1.22 thorpej } 180 1.22 thorpej 181 1.1 ross void 182 1.9 dsl tsp_dma_init(struct tsp_config *pcp) 183 1.1 ross { 184 1.1 ross bus_dma_tag_t t; 185 1.19 thorpej bus_dma_tag_t t_sg_hi = NULL; 186 1.1 ross struct ts_pchip *pccsr = pcp->pc_csr; 187 1.19 thorpej bus_addr_t tbase; 188 1.22 thorpej int i; 189 1.22 thorpej 190 1.22 thorpej /* 191 1.22 thorpej * Save our configuration to restore at shutdown, just 192 1.22 thorpej * in case the firmware would get cranky with us. 193 1.22 thorpej */ 194 1.22 thorpej for (i = 0; i < 4; i++) { 195 1.22 thorpej pcp->pc_saved_windows.wsba[i] = pccsr->tsp_wsba[i].tsg_r; 196 1.22 thorpej pcp->pc_saved_windows.wsm[i] = pccsr->tsp_wsm[i].tsg_r; 197 1.22 thorpej pcp->pc_saved_windows.tba[i] = pccsr->tsp_tba[i].tsg_r; 198 1.22 thorpej } 199 1.22 thorpej pcp->pc_saved_pctl = pccsr->tsp_pctl.tsg_r; 200 1.22 thorpej shutdownhook_establish(tsp_dma_shutdown, pcp); 201 1.17 thorpej 202 1.17 thorpej /* Ensure the Monster Window is enabled. */ 203 1.19 thorpej alpha_mb(); 204 1.17 thorpej pccsr->tsp_pctl.tsg_r |= PCTL_MWIN; 205 1.1 ross alpha_mb(); 206 1.1 ross 207 1.1 ross /* 208 1.19 thorpej * If we have more than 1GB of RAM, then set up an sgmap-mapped 209 1.19 thorpej * DMA window for non-DAC PCI. This is better than using the ISA 210 1.19 thorpej * window, which is pretty small and PCI devices could starve it. 211 1.19 thorpej * 212 1.19 thorpej * N.B. avail_end is "last-usable PFN + 1". 213 1.19 thorpej */ 214 1.19 thorpej if (uvm_physseg_get_avail_end(uvm_physseg_get_last()) > 215 1.19 thorpej atop(TSP_DIRECT_MAPPED_SIZE)) { 216 1.19 thorpej t = t_sg_hi = &pcp->pc_dmat_sgmap_hi; 217 1.19 thorpej t->_cookie = pcp; 218 1.19 thorpej t->_wbase = TSP_SGMAP_MAPPED_HI_BASE; 219 1.19 thorpej t->_wsize = TSP_SGMAP_MAPPED_HI_SIZE; 220 1.19 thorpej t->_next_window = NULL; 221 1.19 thorpej t->_boundary = 0; 222 1.19 thorpej t->_sgmap = &pcp->pc_sgmap_hi; 223 1.21 thorpej /* 224 1.21 thorpej * According to section 8.1.2.2 of the Tsunami/Typhoon 225 1.21 thorpej * hardware reference manual (DS-0025A-TE), the SGMAP 226 1.21 thorpej * TLB is arranged as 168 locations of 4 consecutive 227 1.21 thorpej * quadwords. It seems that on some revisions of the 228 1.21 thorpej * Pchip, SGMAP translation is not perfectly reliable 229 1.21 thorpej * unless we align the DMA segments to the TLBs natural 230 1.21 thorpej * boundaries (observed on the API CS20). 231 1.21 thorpej * 232 1.21 thorpej * N.B. the Titan (as observed on a Compaq DS25) does not 233 1.21 thorpej * seem to have this problem, but we'll play it safe and 234 1.21 thorpej * run this way on both variants. 235 1.21 thorpej */ 236 1.21 thorpej t->_sgmap_minalign = PAGE_SIZE * 4; 237 1.19 thorpej t->_pfthresh = TSP_SGMAP_PFTHRESH; 238 1.19 thorpej t->_get_tag = tsp_dma_get_tag; 239 1.19 thorpej t->_dmamap_create = alpha_sgmap_dmamap_create; 240 1.19 thorpej t->_dmamap_destroy = alpha_sgmap_dmamap_destroy; 241 1.19 thorpej t->_dmamap_load = tsp_bus_dmamap_load_sgmap; 242 1.19 thorpej t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap; 243 1.19 thorpej t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap; 244 1.19 thorpej t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap; 245 1.19 thorpej t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap; 246 1.19 thorpej t->_dmamap_sync = _bus_dmamap_sync; 247 1.19 thorpej 248 1.19 thorpej t->_dmamem_alloc = _bus_dmamem_alloc; 249 1.19 thorpej t->_dmamem_free = _bus_dmamem_free; 250 1.19 thorpej t->_dmamem_map = _bus_dmamem_map; 251 1.19 thorpej t->_dmamem_unmap = _bus_dmamem_unmap; 252 1.19 thorpej t->_dmamem_mmap = _bus_dmamem_mmap; 253 1.19 thorpej } 254 1.19 thorpej 255 1.19 thorpej /* 256 1.17 thorpej * Initialize the DMA tag used for direct-mapped 64-bit DMA. 257 1.17 thorpej */ 258 1.17 thorpej t = &pcp->pc_dmat64_direct; 259 1.17 thorpej t->_cookie = pcp; 260 1.17 thorpej t->_wbase = TSP_MONSTER_DMA_WINDOW_BASE; 261 1.17 thorpej t->_wsize = TSP_MONSTER_DMA_WINDOW_SIZE; 262 1.19 thorpej t->_next_window = t_sg_hi; 263 1.17 thorpej t->_boundary = 0; 264 1.17 thorpej t->_sgmap = NULL; 265 1.17 thorpej t->_get_tag = tsp_dma_get_tag; 266 1.17 thorpej t->_dmamap_create = _bus_dmamap_create; 267 1.17 thorpej t->_dmamap_destroy = _bus_dmamap_destroy; 268 1.17 thorpej t->_dmamap_load = _bus_dmamap_load_direct; 269 1.17 thorpej t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct; 270 1.17 thorpej t->_dmamap_load_uio = _bus_dmamap_load_uio_direct; 271 1.17 thorpej t->_dmamap_load_raw = _bus_dmamap_load_raw_direct; 272 1.17 thorpej t->_dmamap_unload = _bus_dmamap_unload; 273 1.17 thorpej t->_dmamap_sync = _bus_dmamap_sync; 274 1.17 thorpej 275 1.17 thorpej t->_dmamem_alloc = _bus_dmamem_alloc; 276 1.17 thorpej t->_dmamem_free = _bus_dmamem_free; 277 1.17 thorpej t->_dmamem_map = _bus_dmamem_map; 278 1.17 thorpej t->_dmamem_unmap = _bus_dmamem_unmap; 279 1.17 thorpej t->_dmamem_mmap = _bus_dmamem_mmap; 280 1.17 thorpej 281 1.17 thorpej /* 282 1.1 ross * Initialize the DMA tag used for direct-mapped DMA. 283 1.1 ross */ 284 1.1 ross t = &pcp->pc_dmat_direct; 285 1.1 ross t->_cookie = pcp; 286 1.19 thorpej t->_wbase = TSP_DIRECT_MAPPED_BASE; 287 1.19 thorpej t->_wsize = TSP_DIRECT_MAPPED_SIZE; 288 1.19 thorpej t->_next_window = t_sg_hi; 289 1.1 ross t->_boundary = 0; 290 1.1 ross t->_sgmap = NULL; 291 1.1 ross t->_get_tag = tsp_dma_get_tag; 292 1.1 ross t->_dmamap_create = _bus_dmamap_create; 293 1.1 ross t->_dmamap_destroy = _bus_dmamap_destroy; 294 1.1 ross t->_dmamap_load = _bus_dmamap_load_direct; 295 1.1 ross t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct; 296 1.1 ross t->_dmamap_load_uio = _bus_dmamap_load_uio_direct; 297 1.1 ross t->_dmamap_load_raw = _bus_dmamap_load_raw_direct; 298 1.1 ross t->_dmamap_unload = _bus_dmamap_unload; 299 1.1 ross t->_dmamap_sync = _bus_dmamap_sync; 300 1.1 ross 301 1.1 ross t->_dmamem_alloc = _bus_dmamem_alloc; 302 1.1 ross t->_dmamem_free = _bus_dmamem_free; 303 1.1 ross t->_dmamem_map = _bus_dmamem_map; 304 1.1 ross t->_dmamem_unmap = _bus_dmamem_unmap; 305 1.1 ross t->_dmamem_mmap = _bus_dmamem_mmap; 306 1.1 ross 307 1.1 ross /* 308 1.19 thorpej * Initialize the DMA tag used for ISA sgmap-mapped DMA. 309 1.1 ross */ 310 1.19 thorpej t = &pcp->pc_dmat_sgmap_lo; 311 1.1 ross t->_cookie = pcp; 312 1.19 thorpej t->_wbase = TSP_SGMAP_MAPPED_LO_BASE; 313 1.19 thorpej t->_wsize = TSP_SGMAP_MAPPED_LO_SIZE; 314 1.1 ross t->_next_window = NULL; 315 1.1 ross t->_boundary = 0; 316 1.19 thorpej t->_sgmap = &pcp->pc_sgmap_lo; 317 1.21 thorpej /* 318 1.21 thorpej * This appears to be needed to make DMA on the ALI southbridge 319 1.21 thorpej * that's present in some systems happy. ??? 320 1.21 thorpej */ 321 1.21 thorpej t->_sgmap_minalign = (64UL * 1024); 322 1.4 thorpej t->_pfthresh = TSP_SGMAP_PFTHRESH; 323 1.1 ross t->_get_tag = tsp_dma_get_tag; 324 1.3 thorpej t->_dmamap_create = alpha_sgmap_dmamap_create; 325 1.3 thorpej t->_dmamap_destroy = alpha_sgmap_dmamap_destroy; 326 1.1 ross t->_dmamap_load = tsp_bus_dmamap_load_sgmap; 327 1.1 ross t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap; 328 1.1 ross t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap; 329 1.1 ross t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap; 330 1.1 ross t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap; 331 1.1 ross t->_dmamap_sync = _bus_dmamap_sync; 332 1.1 ross 333 1.1 ross t->_dmamem_alloc = _bus_dmamem_alloc; 334 1.1 ross t->_dmamem_free = _bus_dmamem_free; 335 1.1 ross t->_dmamem_map = _bus_dmamem_map; 336 1.1 ross t->_dmamem_unmap = _bus_dmamem_unmap; 337 1.1 ross t->_dmamem_mmap = _bus_dmamem_mmap; 338 1.1 ross 339 1.1 ross /* 340 1.19 thorpej * Initialize the SGMAPs. Align page tables to 32k in case 341 1.1 ross * window is somewhat larger than expected. 342 1.1 ross */ 343 1.19 thorpej alpha_sgmap_init(t, &pcp->pc_sgmap_lo, "tsp_sgmap_lo", 344 1.19 thorpej TSP_SGMAP_MAPPED_LO_BASE, 0, TSP_SGMAP_MAPPED_LO_SIZE, 345 1.19 thorpej sizeof(uint64_t), NULL, (32*1024)); 346 1.19 thorpej if (t_sg_hi != NULL) { 347 1.20 thorpej alpha_sgmap_init(t_sg_hi, &pcp->pc_sgmap_hi, "tsp_sgmap_hi", 348 1.19 thorpej TSP_SGMAP_MAPPED_HI_BASE, 0, TSP_SGMAP_MAPPED_HI_SIZE, 349 1.19 thorpej sizeof(uint64_t), NULL, (32*1024)); 350 1.19 thorpej } 351 1.1 ross 352 1.1 ross /* 353 1.1 ross * Enable window 0 and enable SG PTE mapping. 354 1.1 ross */ 355 1.1 ross alpha_mb(); 356 1.19 thorpej pccsr->tsp_wsba[0].tsg_r = 357 1.19 thorpej TSP_SGMAP_MAPPED_LO_BASE | WSBA_SG | WSBA_ENA; 358 1.19 thorpej alpha_mb(); 359 1.19 thorpej pccsr->tsp_wsm[0].tsg_r = WSM_8MB; 360 1.19 thorpej alpha_mb(); 361 1.19 thorpej tbase = pcp->pc_sgmap_lo.aps_ptpa; 362 1.19 thorpej if (tbase & ~0x7fffffc00UL) 363 1.19 thorpej panic("tsp_dma_init: bad page table address"); 364 1.19 thorpej pccsr->tsp_tba[0].tsg_r = tbase; 365 1.1 ross alpha_mb(); 366 1.1 ross 367 1.1 ross /* 368 1.13 tsutsui * Enable window 1 in direct mode. 369 1.13 tsutsui */ 370 1.13 tsutsui alpha_mb(); 371 1.19 thorpej pccsr->tsp_wsba[1].tsg_r = TSP_DIRECT_MAPPED_BASE | WSBA_ENA; 372 1.19 thorpej alpha_mb(); 373 1.19 thorpej pccsr->tsp_wsm[1].tsg_r = WSM_1GB; 374 1.19 thorpej alpha_mb(); 375 1.19 thorpej pccsr->tsp_tba[1].tsg_r = 0; 376 1.13 tsutsui alpha_mb(); 377 1.13 tsutsui 378 1.19 thorpej if (t_sg_hi != NULL) { 379 1.19 thorpej /* 380 1.19 thorpej * Enable window 2 and enable SG PTE mapping. 381 1.19 thorpej */ 382 1.19 thorpej alpha_mb(); 383 1.19 thorpej pccsr->tsp_wsba[2].tsg_r = 384 1.19 thorpej TSP_SGMAP_MAPPED_HI_BASE | WSBA_SG | WSBA_ENA; 385 1.19 thorpej alpha_mb(); 386 1.19 thorpej pccsr->tsp_wsm[2].tsg_r = WSM_1GB; 387 1.19 thorpej alpha_mb(); 388 1.19 thorpej tbase = pcp->pc_sgmap_hi.aps_ptpa; 389 1.19 thorpej if (tbase & ~0x7fffffc00UL) 390 1.19 thorpej panic("tsp_dma_init: bad page table address"); 391 1.19 thorpej pccsr->tsp_tba[2].tsg_r = tbase; 392 1.19 thorpej alpha_mb(); 393 1.19 thorpej } 394 1.19 thorpej 395 1.13 tsutsui /* 396 1.19 thorpej * Disable window 3. 397 1.1 ross */ 398 1.1 ross alpha_mb(); 399 1.19 thorpej pccsr->tsp_wsba[3].tsg_r = 0; 400 1.19 thorpej alpha_mb(); 401 1.19 thorpej pccsr->tsp_wsm[3].tsg_r = 0; 402 1.19 thorpej alpha_mb(); 403 1.19 thorpej pccsr->tsp_tba[3].tsg_r = 0; 404 1.1 ross alpha_mb(); 405 1.1 ross 406 1.1 ross tsp_tlb_invalidate(pcp); 407 1.1 ross alpha_mb(); 408 1.1 ross } 409 1.1 ross 410 1.1 ross /* 411 1.1 ross * Return the bus dma tag to be used for the specified bus type. 412 1.1 ross * INTERNAL USE ONLY! 413 1.1 ross */ 414 1.16 thorpej static bus_dma_tag_t 415 1.9 dsl tsp_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype) 416 1.1 ross { 417 1.1 ross struct tsp_config *pcp = t->_cookie; 418 1.1 ross 419 1.1 ross switch (bustype) { 420 1.1 ross case ALPHA_BUS_PCI: 421 1.1 ross case ALPHA_BUS_EISA: 422 1.1 ross /* 423 1.1 ross * The direct mapped window will work for most systems, 424 1.1 ross * most of the time. When it doesn't, we chain to the sgmap 425 1.1 ross * window automatically. 426 1.1 ross */ 427 1.1 ross return (&pcp->pc_dmat_direct); 428 1.1 ross 429 1.1 ross case ALPHA_BUS_ISA: 430 1.1 ross /* 431 1.1 ross * ISA doesn't have enough address bits to use 432 1.1 ross * the direct-mapped DMA window, so we must use 433 1.1 ross * SGMAPs. 434 1.1 ross */ 435 1.19 thorpej return (&pcp->pc_dmat_sgmap_lo); 436 1.1 ross 437 1.1 ross default: 438 1.1 ross panic("tsp_dma_get_tag: shouldn't be here, really..."); 439 1.1 ross } 440 1.1 ross } 441 1.1 ross 442 1.1 ross /* 443 1.1 ross * Load a TSP SGMAP-mapped DMA map with a linear buffer. 444 1.1 ross */ 445 1.16 thorpej static int 446 1.9 dsl tsp_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, bus_size_t buflen, struct proc *p, int flags) 447 1.1 ross { 448 1.1 ross int error; 449 1.1 ross 450 1.1 ross error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags, 451 1.1 ross t->_sgmap); 452 1.1 ross if (error == 0) 453 1.1 ross tsp_tlb_invalidate(t->_cookie); 454 1.1 ross 455 1.1 ross return (error); 456 1.1 ross } 457 1.1 ross 458 1.1 ross /* 459 1.1 ross * Load a TSP SGMAP-mapped DMA map with an mbuf chain. 460 1.1 ross */ 461 1.16 thorpej static int 462 1.9 dsl tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags) 463 1.1 ross { 464 1.1 ross int error; 465 1.1 ross 466 1.1 ross error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap); 467 1.1 ross if (error == 0) 468 1.1 ross tsp_tlb_invalidate(t->_cookie); 469 1.1 ross 470 1.1 ross return (error); 471 1.1 ross } 472 1.1 ross 473 1.1 ross /* 474 1.1 ross * Load a TSP SGMAP-mapped DMA map with a uio. 475 1.1 ross */ 476 1.16 thorpej static int 477 1.9 dsl tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags) 478 1.1 ross { 479 1.1 ross int error; 480 1.1 ross 481 1.1 ross error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap); 482 1.1 ross if (error == 0) 483 1.1 ross tsp_tlb_invalidate(t->_cookie); 484 1.1 ross 485 1.1 ross return (error); 486 1.1 ross } 487 1.1 ross 488 1.1 ross /* 489 1.1 ross * Load a TSP SGMAP-mapped DMA map with raw memory. 490 1.1 ross */ 491 1.16 thorpej static int 492 1.9 dsl tsp_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) 493 1.1 ross { 494 1.1 ross int error; 495 1.1 ross 496 1.1 ross error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags, 497 1.1 ross t->_sgmap); 498 1.1 ross if (error == 0) 499 1.1 ross tsp_tlb_invalidate(t->_cookie); 500 1.1 ross 501 1.1 ross return (error); 502 1.1 ross } 503 1.1 ross 504 1.1 ross /* 505 1.1 ross * Unload a TSP DMA map. 506 1.1 ross */ 507 1.16 thorpej static void 508 1.9 dsl tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map) 509 1.1 ross { 510 1.1 ross 511 1.1 ross /* 512 1.1 ross * Invalidate any SGMAP page table entries used by this 513 1.1 ross * mapping. 514 1.1 ross */ 515 1.1 ross pci_sgmap_pte64_unload(t, map, t->_sgmap); 516 1.1 ross tsp_tlb_invalidate(t->_cookie); 517 1.1 ross 518 1.1 ross /* 519 1.1 ross * Do the generic bits of the unload. 520 1.1 ross */ 521 1.15 thorpej _bus_dmamap_unload_common(t, map); 522 1.1 ross } 523 1.1 ross 524 1.1 ross /* 525 1.1 ross * Flush the TSP scatter/gather TLB. 526 1.1 ross */ 527 1.16 thorpej static void 528 1.9 dsl tsp_tlb_invalidate(struct tsp_config *pcp) 529 1.1 ross { 530 1.1 ross 531 1.1 ross alpha_mb(); 532 1.13 tsutsui *pcp->pc_tlbia = 0; 533 1.1 ross alpha_mb(); 534 1.1 ross } 535