1 1.46 rillig /* $NetBSD: dvma.c,v 1.46 2024/09/08 09:36:49 rillig Exp $ */ 2 1.1 gwr 3 1.1 gwr /*- 4 1.1 gwr * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 1.1 gwr * All rights reserved. 6 1.1 gwr * 7 1.1 gwr * This code is derived from software contributed to The NetBSD Foundation 8 1.1 gwr * by Gordon W. Ross and Jeremy Cooper. 9 1.1 gwr * 10 1.1 gwr * Redistribution and use in source and binary forms, with or without 11 1.1 gwr * modification, are permitted provided that the following conditions 12 1.1 gwr * are met: 13 1.1 gwr * 1. Redistributions of source code must retain the above copyright 14 1.1 gwr * notice, this list of conditions and the following disclaimer. 15 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 gwr * notice, this list of conditions and the following disclaimer in the 17 1.1 gwr * documentation and/or other materials provided with the distribution. 18 1.1 gwr * 19 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 gwr * POSSIBILITY OF SUCH DAMAGE. 30 1.1 gwr */ 31 1.1 gwr 32 1.1 gwr /* 33 1.1 gwr * DVMA (Direct Virtual Memory Access - like DMA) 34 1.1 gwr * 35 1.1 gwr * In the Sun3 architecture, memory cycles initiated by secondary bus 36 1.1 gwr * masters (DVMA devices) passed through the same MMU that governed CPU 37 1.1 gwr * accesses. All DVMA devices were wired in such a way so that an offset 38 1.1 gwr * was added to the addresses they issued, causing them to access virtual 39 1.1 gwr * memory starting at address 0x0FF00000 - the offset. The task of 40 1.1 gwr * enabling a DVMA device to access main memory only involved creating 41 1.1 gwr * valid mapping in the MMU that translated these high addresses into the 42 1.1 gwr * appropriate physical addresses. 43 1.1 gwr * 44 1.1 gwr * The Sun3x presents a challenge to programming DVMA because the MMU is no 45 1.1 gwr * longer shared by both secondary bus masters and the CPU. The MC68030's 46 1.1 gwr * built-in MMU serves only to manage virtual memory accesses initiated by 47 1.1 gwr * the CPU. Secondary bus master bus accesses pass through a different MMU, 48 1.1 gwr * aptly named the 'I/O Mapper'. To enable every device driver that uses 49 1.1 gwr * DVMA to understand that these two address spaces are disconnected would 50 1.1 gwr * require a tremendous amount of code re-writing. To avoid this, we will 51 1.1 gwr * ensure that the I/O Mapper and the MC68030 MMU are programmed together, 52 1.1 gwr * so that DVMA mappings are consistent in both the CPU virtual address 53 1.1 gwr * space and secondary bus master address space - creating an environment 54 1.1 gwr * just like the Sun3 system. 55 1.1 gwr * 56 1.1 gwr * The maximum address space that any DVMA device in the Sun3x architecture 57 1.1 gwr * is capable of addressing is 24 bits wide (16 Megabytes.) We can alias 58 1.1 gwr * all of the mappings that exist in the I/O mapper by duplicating them in 59 1.1 gwr * a specially reserved section of the CPU's virtual address space, 16 60 1.1 gwr * Megabytes in size. Whenever a DVMA buffer is allocated, the allocation 61 1.1 gwr * code will enter in a mapping both in the MC68030 MMU page tables and the 62 1.1 gwr * I/O mapper. 63 1.1 gwr * 64 1.1 gwr * The address returned by the allocation routine is a virtual address that 65 1.1 gwr * the requesting driver must use to access the buffer. It is up to the 66 1.1 gwr * device driver to convert this virtual address into the appropriate slave 67 1.5 jeremy * address that its device should issue to access the buffer. (There will be 68 1.5 jeremy * routines that assist the driver in doing so.) 69 1.1 gwr */ 70 1.27 lukem 71 1.27 lukem #include <sys/cdefs.h> 72 1.46 rillig __KERNEL_RCSID(0, "$NetBSD: dvma.c,v 1.46 2024/09/08 09:36:49 rillig Exp $"); 73 1.10 gwr 74 1.1 gwr #include <sys/param.h> 75 1.1 gwr #include <sys/systm.h> 76 1.1 gwr #include <sys/device.h> 77 1.1 gwr #include <sys/proc.h> 78 1.44 thorpej #include <sys/vmem.h> 79 1.1 gwr #include <sys/buf.h> 80 1.1 gwr #include <sys/vnode.h> 81 1.1 gwr #include <sys/core.h> 82 1.1 gwr #include <sys/exec.h> 83 1.1 gwr 84 1.10 gwr #include <uvm/uvm_extern.h> 85 1.10 gwr 86 1.31 tsutsui #define _SUN68K_BUS_DMA_PRIVATE 87 1.1 gwr #include <machine/autoconf.h> 88 1.31 tsutsui #include <machine/bus.h> 89 1.1 gwr #include <machine/cpu.h> 90 1.9 gwr #include <machine/dvma.h> 91 1.1 gwr #include <machine/pmap.h> 92 1.9 gwr 93 1.9 gwr #include <sun3/sun3/machdep.h> 94 1.1 gwr 95 1.9 gwr #include <sun3/sun3x/enable.h> 96 1.9 gwr #include <sun3/sun3x/iommu.h> 97 1.1 gwr 98 1.1 gwr /* 99 1.46 rillig * Use a vmem arena to manage DVMA scratch-memory pages. 100 1.9 gwr * Note: SunOS says last three pages are reserved (PROM?) 101 1.9 gwr * Note: need a separate map (sub-map?) for last 1MB for 102 1.9 gwr * use by VME slave interface. 103 1.1 gwr */ 104 1.44 thorpej vmem_t *dvma_arena; 105 1.1 gwr 106 1.42 tsutsui void 107 1.28 chs dvma_init(void) 108 1.1 gwr { 109 1.1 gwr 110 1.1 gwr /* 111 1.44 thorpej * Create the vmem arena for DVMA pages. 112 1.1 gwr */ 113 1.44 thorpej dvma_arena = vmem_create("dvma", DVMA_MAP_BASE, DVMA_MAP_AVAIL, 114 1.44 thorpej PAGE_SIZE, /* quantum */ 115 1.44 thorpej NULL, /* importfn */ 116 1.44 thorpej NULL, /* releasefn */ 117 1.44 thorpej NULL, /* source */ 118 1.44 thorpej 0, /* qcache_max */ 119 1.44 thorpej VM_SLEEP, 120 1.44 thorpej IPL_VM); 121 1.1 gwr 122 1.1 gwr /* 123 1.1 gwr * Enable DVMA in the System Enable register. 124 1.1 gwr * Note: This is only necessary for VME slave accesses. 125 1.1 gwr * On-board devices are always capable of DVMA. 126 1.1 gwr */ 127 1.8 gwr *enable_reg |= ENA_SDVMA; 128 1.1 gwr } 129 1.1 gwr 130 1.1 gwr 131 1.1 gwr /* 132 1.1 gwr * Given a DVMA address, return the physical address that 133 1.1 gwr * would be used by some OTHER bus-master besides the CPU. 134 1.1 gwr * (Examples: on-board ie/le, VME xy board). 135 1.1 gwr */ 136 1.42 tsutsui u_long 137 1.28 chs dvma_kvtopa(void *kva, int bustype) 138 1.1 gwr { 139 1.1 gwr u_long addr, mask; 140 1.1 gwr 141 1.1 gwr addr = (u_long)kva; 142 1.8 gwr if ((addr & DVMA_MAP_BASE) != DVMA_MAP_BASE) 143 1.25 provos panic("dvma_kvtopa: bad dmva addr=0x%lx", addr); 144 1.1 gwr 145 1.6 gwr switch (bustype) { 146 1.6 gwr case BUS_OBIO: 147 1.6 gwr case BUS_OBMEM: 148 1.8 gwr mask = DVMA_OBIO_SLAVE_MASK; 149 1.8 gwr break; 150 1.8 gwr default: /* VME bus device. */ 151 1.8 gwr mask = DVMA_VME_SLAVE_MASK; 152 1.6 gwr break; 153 1.6 gwr } 154 1.1 gwr 155 1.32 tsutsui return addr & mask; 156 1.1 gwr } 157 1.1 gwr 158 1.1 gwr 159 1.1 gwr /* 160 1.1 gwr * Map a range [va, va+len] of wired virtual addresses in the given map 161 1.1 gwr * to a kernel address in DVMA space. 162 1.1 gwr */ 163 1.1 gwr void * 164 1.28 chs dvma_mapin(void *kmem_va, int len, int canwait) 165 1.1 gwr { 166 1.37 christos void *dvma_addr; 167 1.44 thorpej vaddr_t kva; 168 1.44 thorpej vmem_addr_t tva; 169 1.44 thorpej int npf, error; 170 1.13 thorpej paddr_t pa; 171 1.24 thorpej long off; 172 1.43 christos bool rv __debugused; 173 1.1 gwr 174 1.21 tsutsui kva = (vaddr_t)kmem_va; 175 1.44 thorpej KASSERT(kva >= VM_MIN_KERNEL_ADDRESS); 176 1.1 gwr 177 1.3 jeremy /* 178 1.3 jeremy * Calculate the offset of the data buffer from a page boundary. 179 1.3 jeremy */ 180 1.21 tsutsui off = kva & PGOFSET; 181 1.3 jeremy kva -= off; /* Truncate starting address to nearest page. */ 182 1.3 jeremy len = round_page(len + off); /* Round the buffer length to pages. */ 183 1.3 jeremy npf = btoc(len); /* Determine the number of pages to be mapped. */ 184 1.1 gwr 185 1.24 thorpej /* 186 1.24 thorpej * Try to allocate DVMA space of the appropriate size 187 1.24 thorpej * in which to do a transfer. 188 1.24 thorpej */ 189 1.44 thorpej const vm_flag_t vmflags = VM_INSTANTFIT | 190 1.44 thorpej (canwait ? VM_SLEEP : VM_NOSLEEP); 191 1.44 thorpej 192 1.44 thorpej error = vmem_xalloc(dvma_arena, len, 193 1.44 thorpej 0, /* alignment */ 194 1.44 thorpej 0, /* phase */ 195 1.44 thorpej 0, /* nocross */ 196 1.44 thorpej VMEM_ADDR_MIN, /* minaddr */ 197 1.44 thorpej VMEM_ADDR_MAX, /* maxaddr */ 198 1.44 thorpej vmflags, 199 1.44 thorpej &tva); 200 1.24 thorpej if (error) 201 1.32 tsutsui return NULL; 202 1.42 tsutsui 203 1.42 tsutsui /* 204 1.3 jeremy * Tva is the starting page to which the data buffer will be double 205 1.3 jeremy * mapped. Dvma_addr is the starting address of the buffer within 206 1.3 jeremy * that page and is the return value of the function. 207 1.3 jeremy */ 208 1.32 tsutsui dvma_addr = (void *)(tva + off); 209 1.1 gwr 210 1.32 tsutsui for (; npf--; kva += PAGE_SIZE, tva += PAGE_SIZE) { 211 1.3 jeremy /* 212 1.3 jeremy * Retrieve the physical address of each page in the buffer 213 1.3 jeremy * and enter mappings into the I/O MMU so they may be seen 214 1.3 jeremy * by external bus masters and into the special DVMA space 215 1.3 jeremy * in the MC68030 MMU so they may be seen by the CPU. 216 1.3 jeremy */ 217 1.13 thorpej rv = pmap_extract(pmap_kernel(), kva, &pa); 218 1.3 jeremy #ifdef DEBUG 219 1.36 thorpej if (rv == false) 220 1.1 gwr panic("dvma_mapin: null page frame"); 221 1.20 simonb #endif /* DEBUG */ 222 1.1 gwr 223 1.7 gwr iommu_enter((tva & IOMMU_VA_MASK), pa); 224 1.39 he pmap_kenter_pa(tva, 225 1.39 he pa | PMAP_NC, VM_PROT_READ | VM_PROT_WRITE, 0); 226 1.1 gwr } 227 1.22 chris pmap_update(pmap_kernel()); 228 1.1 gwr 229 1.32 tsutsui return dvma_addr; 230 1.1 gwr } 231 1.1 gwr 232 1.1 gwr /* 233 1.1 gwr * Remove double map of `va' in DVMA space at `kva'. 234 1.3 jeremy * 235 1.3 jeremy * TODO - This function might be the perfect place to handle the 236 1.3 jeremy * synchronization between the DVMA cache and central RAM 237 1.3 jeremy * on the 3/470. 238 1.1 gwr */ 239 1.42 tsutsui void 240 1.28 chs dvma_mapout(void *dvma_addr, int len) 241 1.1 gwr { 242 1.1 gwr u_long kva; 243 1.44 thorpej int off; 244 1.1 gwr 245 1.1 gwr kva = (u_long)dvma_addr; 246 1.1 gwr off = (int)kva & PGOFSET; 247 1.1 gwr kva -= off; 248 1.1 gwr len = round_page(len + off); 249 1.1 gwr 250 1.7 gwr iommu_remove((kva & IOMMU_VA_MASK), len); 251 1.23 chs pmap_kremove(kva, len); 252 1.22 chris pmap_update(pmap_kernel()); 253 1.1 gwr 254 1.44 thorpej vmem_xfree(dvma_arena, kva, len); 255 1.4 gwr } 256 1.4 gwr 257 1.4 gwr /* 258 1.4 gwr * Allocate actual memory pages in DVMA space. 259 1.4 gwr * (For sun3 compatibility - the ie driver.) 260 1.4 gwr */ 261 1.4 gwr void * 262 1.28 chs dvma_malloc(size_t bytes) 263 1.4 gwr { 264 1.4 gwr void *new_mem, *dvma_mem; 265 1.21 tsutsui vsize_t new_size; 266 1.4 gwr 267 1.32 tsutsui if (bytes == 0) 268 1.4 gwr return NULL; 269 1.4 gwr new_size = m68k_round_page(bytes); 270 1.32 tsutsui new_mem = (void *)uvm_km_alloc(kernel_map, new_size, 0, UVM_KMF_WIRED); 271 1.32 tsutsui if (new_mem == 0) 272 1.4 gwr return NULL; 273 1.4 gwr dvma_mem = dvma_mapin(new_mem, new_size, 1); 274 1.32 tsutsui return dvma_mem; 275 1.9 gwr } 276 1.9 gwr 277 1.9 gwr /* 278 1.9 gwr * Free pages from dvma_malloc() 279 1.9 gwr */ 280 1.42 tsutsui void 281 1.28 chs dvma_free(void *addr, size_t size) 282 1.9 gwr { 283 1.21 tsutsui vsize_t sz = m68k_round_page(size); 284 1.9 gwr 285 1.9 gwr dvma_mapout(addr, sz); 286 1.9 gwr /* XXX: need kmem address to free it... 287 1.9 gwr Oh well, we never call this anyway. */ 288 1.1 gwr } 289 1.31 tsutsui 290 1.42 tsutsui int 291 1.31 tsutsui _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs, 292 1.31 tsutsui int nsegs, bus_size_t size, int flags) 293 1.31 tsutsui { 294 1.31 tsutsui 295 1.31 tsutsui panic("_bus_dmamap_load_raw(): not implemented yet."); 296 1.31 tsutsui } 297 1.31 tsutsui 298 1.31 tsutsui int 299 1.31 tsutsui _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 300 1.31 tsutsui bus_size_t buflen, struct proc *p, int flags) 301 1.31 tsutsui { 302 1.44 thorpej vaddr_t kva; 303 1.44 thorpej vmem_addr_t dva; 304 1.33 tsutsui vsize_t off, sgsize; 305 1.33 tsutsui paddr_t pa; 306 1.33 tsutsui pmap_t pmap; 307 1.44 thorpej int error, rv __diagused; 308 1.33 tsutsui 309 1.33 tsutsui /* 310 1.33 tsutsui * Make sure that on error condition we return "no valid mappings". 311 1.33 tsutsui */ 312 1.33 tsutsui map->dm_nsegs = 0; 313 1.33 tsutsui map->dm_mapsize = 0; 314 1.33 tsutsui 315 1.33 tsutsui if (buflen > map->_dm_size) 316 1.33 tsutsui return EINVAL; 317 1.33 tsutsui 318 1.33 tsutsui kva = (vaddr_t)buf; 319 1.33 tsutsui off = kva & PGOFSET; 320 1.33 tsutsui sgsize = round_page(off + buflen); 321 1.31 tsutsui 322 1.33 tsutsui /* Try to allocate DVMA space. */ 323 1.44 thorpej const vm_flag_t vmflags = VM_INSTANTFIT | 324 1.44 thorpej ((flags & BUS_DMA_NOWAIT) ? VM_NOSLEEP : VM_SLEEP); 325 1.44 thorpej 326 1.44 thorpej error = vmem_xalloc(dvma_arena, sgsize, 327 1.44 thorpej 0, /* alignment */ 328 1.44 thorpej 0, /* phase */ 329 1.44 thorpej 0, /* nocross */ 330 1.44 thorpej VMEM_ADDR_MIN, /* minaddr */ 331 1.44 thorpej VMEM_ADDR_MAX, /* maxaddr */ 332 1.44 thorpej vmflags, 333 1.44 thorpej &dva); 334 1.33 tsutsui if (error) 335 1.33 tsutsui return ENOMEM; 336 1.33 tsutsui 337 1.33 tsutsui /* Fill in the segment. */ 338 1.33 tsutsui map->dm_segs[0].ds_addr = dva + off; 339 1.33 tsutsui map->dm_segs[0].ds_len = buflen; 340 1.33 tsutsui map->dm_segs[0]._ds_va = dva; 341 1.33 tsutsui map->dm_segs[0]._ds_sgsize = sgsize; 342 1.33 tsutsui 343 1.33 tsutsui /* 344 1.33 tsutsui * Now map the DVMA addresses we allocated to point to the 345 1.33 tsutsui * pages of the caller's buffer. 346 1.33 tsutsui */ 347 1.33 tsutsui if (p != NULL) 348 1.33 tsutsui pmap = p->p_vmspace->vm_map.pmap; 349 1.33 tsutsui else 350 1.33 tsutsui pmap = pmap_kernel(); 351 1.33 tsutsui 352 1.33 tsutsui while (sgsize > 0) { 353 1.33 tsutsui rv = pmap_extract(pmap, kva, &pa); 354 1.33 tsutsui #ifdef DIAGNOSTIC 355 1.36 thorpej if (rv == false) 356 1.33 tsutsui panic("%s: unmapped VA", __func__); 357 1.33 tsutsui #endif 358 1.33 tsutsui iommu_enter((dva & IOMMU_VA_MASK), pa); 359 1.39 he pmap_kenter_pa(dva, 360 1.39 he pa | PMAP_NC, VM_PROT_READ | VM_PROT_WRITE, 0); 361 1.33 tsutsui kva += PAGE_SIZE; 362 1.33 tsutsui dva += PAGE_SIZE; 363 1.33 tsutsui sgsize -= PAGE_SIZE; 364 1.33 tsutsui } 365 1.33 tsutsui 366 1.33 tsutsui map->dm_nsegs = 1; 367 1.33 tsutsui map->dm_mapsize = map->dm_segs[0].ds_len; 368 1.33 tsutsui 369 1.33 tsutsui return 0; 370 1.31 tsutsui } 371 1.31 tsutsui 372 1.42 tsutsui void 373 1.31 tsutsui _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 374 1.31 tsutsui { 375 1.33 tsutsui bus_dma_segment_t *segs; 376 1.33 tsutsui vaddr_t dva; 377 1.33 tsutsui vsize_t sgsize; 378 1.33 tsutsui 379 1.33 tsutsui #ifdef DIAGNOSTIC 380 1.33 tsutsui if (map->dm_nsegs != 1) 381 1.34 tsutsui panic("%s: invalid nsegs = %d", __func__, map->dm_nsegs); 382 1.33 tsutsui #endif 383 1.33 tsutsui 384 1.33 tsutsui segs = map->dm_segs; 385 1.33 tsutsui dva = segs[0]._ds_va & ~PGOFSET; 386 1.33 tsutsui sgsize = segs[0]._ds_sgsize; 387 1.33 tsutsui 388 1.33 tsutsui /* Unmap the DVMA addresses. */ 389 1.33 tsutsui iommu_remove((dva & IOMMU_VA_MASK), sgsize); 390 1.33 tsutsui pmap_kremove(dva, sgsize); 391 1.33 tsutsui pmap_update(pmap_kernel()); 392 1.33 tsutsui 393 1.33 tsutsui /* Free the DVMA addresses. */ 394 1.44 thorpej vmem_xfree(dvma_arena, dva, sgsize); 395 1.31 tsutsui 396 1.33 tsutsui /* Mark the mappings as invalid. */ 397 1.33 tsutsui map->dm_mapsize = 0; 398 1.33 tsutsui map->dm_nsegs = 0; 399 1.31 tsutsui } 400