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