1/* $NetBSD: apecs_dma.c,v 1.25 2021/07/04 22:42:36 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 1997, 1998 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 of the Numerical Aerospace Simulation Facility, 9 * 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: apecs_dma.c,v 1.25 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/apecsreg.h> 48#include <alpha/pci/apecsvar.h> 49 50static bus_dma_tag_t apecs_dma_get_tag(bus_dma_tag_t, alpha_bus_t); 51 52static int apecs_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *, 53 bus_size_t, struct proc *, int); 54 55static int apecs_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t, 56 struct mbuf *, int); 57 58static int apecs_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t, 59 struct uio *, int); 60 61static int apecs_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 apecs_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t); 65 66/* 67 * Direct-mapped window: 1G at 1G 68 */ 69#define APECS_DIRECT_MAPPED_BASE (1*1024*1024*1024) 70#define APECS_DIRECT_MAPPED_SIZE (1*1024*1024*1024) 71 72/* 73 * SGMAP window: 8M at 8M 74 */ 75#define APECS_SGMAP_MAPPED_BASE (8*1024*1024) 76#define APECS_SGMAP_MAPPED_SIZE (8*1024*1024) 77 78/* APECS has a 256-byte out-bound DMA prefetch threshold. */ 79#define APECS_SGMAP_PFTHRESH 256 80 81/* 82 * Macro to flush APECS scatter/gather TLB. 83 */ 84#define APECS_TLB_INVALIDATE() \ 85do { \ 86 alpha_mb(); \ 87 REGVAL(EPIC_TBIA) = 0; \ 88 alpha_mb(); \ 89} while (0) 90 91void 92apecs_dma_init(struct apecs_config *acp) 93{ 94 bus_addr_t tbase; 95 bus_dma_tag_t t; 96 97 /* 98 * Initialize the DMA tag used for direct-mapped DMA. 99 */ 100 t = &acp->ac_dmat_direct; 101 t->_cookie = acp; 102 t->_wbase = APECS_DIRECT_MAPPED_BASE; 103 t->_wsize = APECS_DIRECT_MAPPED_SIZE; 104 t->_next_window = NULL; 105 t->_boundary = 0; 106 t->_sgmap = NULL; 107 t->_get_tag = apecs_dma_get_tag; 108 t->_dmamap_create = _bus_dmamap_create; 109 t->_dmamap_destroy = _bus_dmamap_destroy; 110 t->_dmamap_load = _bus_dmamap_load_direct; 111 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct; 112 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct; 113 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct; 114 t->_dmamap_unload = _bus_dmamap_unload; 115 t->_dmamap_sync = _bus_dmamap_sync; 116 117 t->_dmamem_alloc = _bus_dmamem_alloc; 118 t->_dmamem_free = _bus_dmamem_free; 119 t->_dmamem_map = _bus_dmamem_map; 120 t->_dmamem_unmap = _bus_dmamem_unmap; 121 t->_dmamem_mmap = _bus_dmamem_mmap; 122 123 /* 124 * Initialize the DMA tag used for sgmap-mapped DMA. 125 */ 126 t = &acp->ac_dmat_sgmap; 127 t->_cookie = acp; 128 t->_wbase = APECS_SGMAP_MAPPED_BASE; 129 t->_wsize = APECS_SGMAP_MAPPED_SIZE; 130 t->_next_window = NULL; 131 t->_boundary = 0; 132 t->_sgmap = &acp->ac_sgmap; 133 t->_pfthresh = APECS_SGMAP_PFTHRESH; 134 t->_get_tag = apecs_dma_get_tag; 135 t->_dmamap_create = alpha_sgmap_dmamap_create; 136 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy; 137 t->_dmamap_load = apecs_bus_dmamap_load_sgmap; 138 t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap; 139 t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap; 140 t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap; 141 t->_dmamap_unload = apecs_bus_dmamap_unload_sgmap; 142 t->_dmamap_sync = _bus_dmamap_sync; 143 144 t->_dmamem_alloc = _bus_dmamem_alloc; 145 t->_dmamem_free = _bus_dmamem_free; 146 t->_dmamem_map = _bus_dmamem_map; 147 t->_dmamem_unmap = _bus_dmamem_unmap; 148 t->_dmamem_mmap = _bus_dmamem_mmap; 149 150 /* 151 * The firmware has set up window 2 as a 1G direct-mapped DMA 152 * window beginning at 1G. We leave it alone. Disable 153 * window 1. 154 */ 155 REGVAL(EPIC_PCI_BASE_1) = 0; 156 alpha_mb(); 157 158 /* 159 * Initialize the SGMAP. 160 */ 161 alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap", 162 APECS_SGMAP_MAPPED_BASE, 0, APECS_SGMAP_MAPPED_SIZE, 163 sizeof(uint64_t), NULL, 0); 164 165 /* 166 * Set up window 1 as an 8MB SGMAP-mapped window 167 * starting at 8MB. 168 */ 169 REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE | 170 EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB; 171 alpha_mb(); 172 173 REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M; 174 alpha_mb(); 175 176 tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT; 177 if ((tbase & EPIC_TBASE_T_BASE) != tbase) 178 panic("apecs_dma_init: bad page table address"); 179 REGVAL(EPIC_TBASE_1) = tbase; 180 alpha_mb(); 181 182 APECS_TLB_INVALIDATE(); 183} 184 185/* 186 * Return the bus dma tag to be used for the specified bus type. 187 * INTERNAL USE ONLY! 188 */ 189static bus_dma_tag_t 190apecs_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype) 191{ 192 struct apecs_config *acp = t->_cookie; 193 194 switch (bustype) { 195 case ALPHA_BUS_PCI: 196 case ALPHA_BUS_EISA: 197 /* 198 * Systems with an APECS can only support 1G 199 * of memory, so we use the direct-mapped window 200 * on busses that have 32-bit DMA. 201 */ 202 return (&acp->ac_dmat_direct); 203 204 case ALPHA_BUS_ISA: 205 /* 206 * ISA doesn't have enough address bits to use 207 * the direct-mapped DMA window, so we must use 208 * SGMAPs. 209 */ 210 return (&acp->ac_dmat_sgmap); 211 212 default: 213 panic("apecs_dma_get_tag: shouldn't be here, really..."); 214 } 215} 216 217/* 218 * Load an APECS SGMAP-mapped DMA map with a linear buffer. 219 */ 220static int 221apecs_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{ 223 int error; 224 225 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags, 226 t->_sgmap); 227 if (error == 0) 228 APECS_TLB_INVALIDATE(); 229 230 return (error); 231} 232 233/* 234 * Load an APECS SGMAP-mapped DMA map with an mbuf chain. 235 */ 236static int 237apecs_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m, int flags) 238{ 239 int error; 240 241 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap); 242 if (error == 0) 243 APECS_TLB_INVALIDATE(); 244 245 return (error); 246} 247 248/* 249 * Load an APECS SGMAP-mapped DMA map with a uio. 250 */ 251static int 252apecs_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, int flags) 253{ 254 int error; 255 256 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap); 257 if (error == 0) 258 APECS_TLB_INVALIDATE(); 259 260 return (error); 261} 262 263/* 264 * Load an APECS SGMAP-mapped DMA map with raw memory. 265 */ 266static int 267apecs_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{ 269 int error; 270 271 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags, 272 t->_sgmap); 273 if (error == 0) 274 APECS_TLB_INVALIDATE(); 275 276 return (error); 277} 278 279/* 280 * Unload an APECS DMA map. 281 */ 282static void 283apecs_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map) 284{ 285 286 /* 287 * Invalidate any SGMAP page table entries used by this 288 * mapping. 289 */ 290 pci_sgmap_pte64_unload(t, map, t->_sgmap); 291 APECS_TLB_INVALIDATE(); 292 293 /* 294 * Do the generic bits of the unload. 295 */ 296 _bus_dmamap_unload_common(t, map); 297} 298