1 /* $NetBSD: bus_dma.c,v 1.19 2026/10/22 20:48:19 andvar Exp $ */ 2 3 /* 4 * Copyright (c) 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> 34 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.19 2026/10/22 20:48:19 andvar Exp $"); 35 36 #define _EWS4800MIPS_BUS_DMA_PRIVATE 37 /* #define BUS_DMA_DEBUG */ 38 39 #include <sys/param.h> 40 #include <sys/kmem.h> 41 #include <sys/bus.h> 42 #include <sys/mbuf.h> 43 #include <sys/proc.h> 44 45 #include <mips/locore.h> 46 #include <mips/cache.h> 47 48 #include <machine/sbdvar.h> 49 50 #include <dev/bus_dma/bus_dmamem_common.h> 51 52 #include <uvm/uvm_extern.h> 53 54 extern paddr_t kvtophys(vaddr_t); /* XXX */ 55 static int _bus_dmamap_load_buffer(bus_dmamap_t, void *, bus_size_t, 56 struct vmspace *, int, vaddr_t *, int *, int); 57 58 struct ews4800mips_bus_dma_tag ews4800mips_default_bus_dma_tag = { 59 _bus_dmamap_create, 60 _bus_dmamap_destroy, 61 _bus_dmamap_load, 62 _bus_dmamap_load_mbuf, 63 _bus_dmamap_load_uio, 64 _bus_dmamap_load_raw, 65 _bus_dmamap_unload, 66 _bus_dmamap_sync, 67 _bus_dmamem_alloc, 68 _bus_dmamem_free, 69 _bus_dmamem_map, 70 _bus_dmamem_unmap, 71 _bus_dmamem_mmap, 72 }; 73 74 static size_t 75 _bus_dmamap_mapsize(int const nsegments) 76 { 77 KASSERT(nsegments > 0); 78 return sizeof(struct ews4800mips_bus_dmamap) + 79 (sizeof(bus_dma_segment_t) * (nsegments - 1)); 80 } 81 82 /* 83 * Common function for DMA map creation. May be called by bus-specific 84 * DMA map creation functions. 85 */ 86 int 87 _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 88 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp) 89 { 90 struct ews4800mips_bus_dmamap *map; 91 void *mapstore; 92 93 /* 94 * Allocate and initialize the DMA map. The end of the map 95 * is a variable-sized array of segments, so we allocate enough 96 * room for them in one shot. 97 * 98 * Note we don't preserve the WAITOK or NOWAIT flags. Preservation 99 * of ALLOCNOW notifes others that we've reserved these resources, 100 * and they are not to be freed. 101 * 102 * The bus_dmamap_t includes one bus_dma_segment_t, hence 103 * the (nsegments - 1). 104 */ 105 if ((mapstore = kmem_zalloc(_bus_dmamap_mapsize(nsegments), 106 (flags & BUS_DMA_NOWAIT) ? KM_NOSLEEP : KM_SLEEP)) == NULL) 107 return ENOMEM; 108 109 map = (struct ews4800mips_bus_dmamap *)mapstore; 110 map->_dm_size = size; 111 map->_dm_segcnt = nsegments; 112 map->_dm_maxmaxsegsz = maxsegsz; 113 map->_dm_boundary = boundary; 114 map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT); 115 map->_dm_vmspace = NULL; 116 map->dm_maxsegsz = maxsegsz; 117 map->dm_mapsize = 0; /* no valid mappings */ 118 map->dm_nsegs = 0; 119 120 *dmamp = map; 121 return 0; 122 } 123 124 /* 125 * Common function for DMA map destruction. May be called by bus-specific 126 * DMA map destruction functions. 127 */ 128 void 129 _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 130 { 131 132 kmem_free(map, _bus_dmamap_mapsize(map->_dm_segcnt)); 133 } 134 135 136 /* 137 * Utility function to load a linear buffer. lastaddrp holds state 138 * between invocations (for multiple-buffer loads). segp contains 139 * the starting segment on entrance, and the ending segment on exit. 140 * first indicates if this is the first invocation of this function. 141 */ 142 int 143 _bus_dmamap_load_buffer(bus_dmamap_t map, void *buf, bus_size_t buflen, 144 struct vmspace *vm, int flags, vaddr_t *lastaddrp, int *segp, int first) 145 { 146 bus_size_t sgsize; 147 bus_addr_t curaddr, lastaddr, baddr, bmask; 148 vaddr_t vaddr = (vaddr_t)buf; 149 int seg; 150 151 lastaddr = *lastaddrp; 152 bmask = ~(map->_dm_boundary - 1); 153 154 for (seg = *segp; buflen > 0 ; ) { 155 /* 156 * Get the physical address for this segment. 157 */ 158 if (!VMSPACE_IS_KERNEL_P(vm)) 159 (void) pmap_extract(vm_map_pmap(&vm->vm_map), 160 vaddr, (paddr_t *)&curaddr); 161 else 162 curaddr = kvtophys(vaddr); 163 164 /* 165 * Compute the segment size, and adjust counts. 166 */ 167 sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET); 168 if (buflen < sgsize) 169 sgsize = buflen; 170 171 /* 172 * Make sure we don't cross any boundaries. 173 */ 174 if (map->_dm_boundary > 0) { 175 baddr = (curaddr + map->_dm_boundary) & bmask; 176 if (sgsize > (baddr - curaddr)) 177 sgsize = (baddr - curaddr); 178 } 179 180 /* 181 * Insert chunk into a segment, coalescing with 182 * the previous segment if possible. 183 */ 184 if (first) { 185 map->dm_segs[seg].ds_addr = curaddr; 186 map->dm_segs[seg].ds_len = sgsize; 187 map->dm_segs[seg]._ds_vaddr = vaddr; 188 first = 0; 189 } else { 190 if (curaddr == lastaddr && 191 (map->dm_segs[seg].ds_len + sgsize) <= 192 map->dm_maxsegsz && 193 (map->_dm_boundary == 0 || 194 (map->dm_segs[seg].ds_addr & bmask) == 195 (curaddr & bmask))) 196 map->dm_segs[seg].ds_len += sgsize; 197 else { 198 if (++seg >= map->_dm_segcnt) 199 break; 200 map->dm_segs[seg].ds_addr = curaddr; 201 map->dm_segs[seg].ds_len = sgsize; 202 map->dm_segs[seg]._ds_vaddr = vaddr; 203 } 204 } 205 206 lastaddr = curaddr + sgsize; 207 vaddr += sgsize; 208 buflen -= sgsize; 209 } 210 211 *segp = seg; 212 *lastaddrp = lastaddr; 213 214 /* 215 * Did we fit? 216 */ 217 if (buflen != 0) 218 return EFBIG; /* XXX Better return value here? */ 219 220 return 0; 221 } 222 223 /* 224 * Common function for loading a direct-mapped DMA map with a linear 225 * buffer. 226 */ 227 int 228 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 229 bus_size_t buflen, struct proc *p, int flags) 230 { 231 vaddr_t lastaddr; 232 int seg, error; 233 struct vmspace *vm; 234 235 /* 236 * Make sure that on error condition we return "no valid mappings". 237 */ 238 map->dm_mapsize = 0; 239 map->dm_nsegs = 0; 240 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz); 241 242 if (buflen > map->_dm_size) 243 return EINVAL; 244 245 if (p != NULL) { 246 vm = p->p_vmspace; 247 } else { 248 vm = vmspace_kernel(); 249 } 250 251 seg = 0; 252 error = _bus_dmamap_load_buffer(map, buf, buflen, 253 vm, flags, &lastaddr, &seg, 1); 254 if (error == 0) { 255 map->dm_mapsize = buflen; 256 map->dm_nsegs = seg + 1; 257 map->_dm_vmspace = vm; 258 259 /* 260 * For linear buffers, we support marking the mapping 261 * as COHERENT. 262 * 263 * XXX Check TLB entries for cache-inhibit bits? 264 */ 265 if (buf >= (void *)MIPS_KSEG1_START && 266 buf < (void *)MIPS_KSEG2_START) 267 map->_dm_flags |= EWS4800MIPS_DMAMAP_COHERENT; 268 } 269 return error; 270 } 271 272 /* 273 * Like _bus_dmamap_load(), but for mbufs. 274 */ 275 int 276 _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0, 277 int flags) 278 { 279 vaddr_t lastaddr; 280 int seg, error, first; 281 struct mbuf *m; 282 283 /* 284 * Make sure that on error condition we return "no valid mappings." 285 */ 286 map->dm_mapsize = 0; 287 map->dm_nsegs = 0; 288 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz); 289 290 #ifdef DIAGNOSTIC 291 if ((m0->m_flags & M_PKTHDR) == 0) 292 panic("_bus_dmamap_load_mbuf: no packet header"); 293 #endif 294 295 if (m0->m_pkthdr.len > map->_dm_size) 296 return EINVAL; 297 298 first = 1; 299 seg = 0; 300 error = 0; 301 for (m = m0; m != NULL && error == 0; m = m->m_next) { 302 error = _bus_dmamap_load_buffer(map, m->m_data, m->m_len, 303 vmspace_kernel(), flags, &lastaddr, &seg, first); 304 first = 0; 305 } 306 if (error == 0) { 307 map->dm_mapsize = m0->m_pkthdr.len; 308 map->dm_nsegs = seg + 1; 309 map->_dm_vmspace = vmspace_kernel(); /* always kernel */ 310 } 311 return error; 312 } 313 314 /* 315 * Like _bus_dmamap_load(), but for uios. 316 */ 317 int 318 _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio, 319 int flags) 320 { 321 vaddr_t lastaddr; 322 int seg, i, error, first; 323 bus_size_t minlen, resid; 324 struct iovec *iov; 325 void *addr; 326 327 /* 328 * Make sure that on error condition we return "no valid mappings." 329 */ 330 map->dm_mapsize = 0; 331 map->dm_nsegs = 0; 332 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz); 333 334 resid = uio->uio_resid; 335 iov = uio->uio_iov; 336 337 first = 1; 338 seg = 0; 339 error = 0; 340 for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) { 341 /* 342 * Now at the first iovec to load. Load each iovec 343 * until we have exhausted the residual count. 344 */ 345 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len; 346 addr = (void *)iov[i].iov_base; 347 348 error = _bus_dmamap_load_buffer(map, addr, minlen, 349 uio->uio_vmspace, flags, &lastaddr, &seg, first); 350 first = 0; 351 352 resid -= minlen; 353 } 354 if (error == 0) { 355 map->dm_mapsize = uio->uio_resid; 356 map->dm_nsegs = seg + 1; 357 map->_dm_vmspace = uio->uio_vmspace; 358 } 359 return error; 360 } 361 362 /* 363 * Like _bus_dmamap_load(), but for raw memory. 364 */ 365 int 366 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, 367 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 368 { 369 370 panic("_bus_dmamap_load_raw: not implemented"); 371 } 372 373 /* 374 * Common function for unloading a DMA map. May be called by 375 * chipset-specific DMA map unload functions. 376 */ 377 void 378 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 379 { 380 381 /* 382 * No resources to free; just mark the mappings as 383 * invalid. 384 */ 385 map->dm_maxsegsz = map->_dm_maxmaxsegsz; 386 map->dm_mapsize = 0; 387 map->dm_nsegs = 0; 388 map->_dm_flags &= ~EWS4800MIPS_DMAMAP_COHERENT; 389 } 390 391 /* 392 * Common function for DMA map synchronization. May be called 393 * by chipset-specific DMA map synchronization functions. 394 */ 395 void 396 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 397 bus_size_t len, int ops) 398 { 399 bus_size_t minlen; 400 vaddr_t vaddr, start, end, preboundary, firstboundary, lastboundary; 401 int i, useindex; 402 403 /* 404 * Mixing PRE and POST operations is not allowed. 405 */ 406 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 && 407 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0) 408 panic("_bus_dmamap_sync: mix PRE and POST"); 409 410 #ifdef DIAGNOSTIC 411 if (offset >= map->dm_mapsize) 412 panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)", 413 offset, map->dm_mapsize); 414 if (len == 0 || (offset + len) > map->dm_mapsize) 415 panic("_bus_dmamap_sync: bad length"); 416 #endif 417 418 /* 419 * Since we're dealing with a virtually-indexed, write-back 420 * cache, we need to do the following things: 421 * 422 * PREREAD -- Invalidate D-cache. Note we might have 423 * to also write-back here if we have to use an Index 424 * op, or if the buffer start/end is not cache-line aligned. 425 * 426 * PREWRITE -- Write-back the D-cache. If we have to use 427 * an Index op, we also have to invalidate. Note that if 428 * we are doing PREREAD|PREWRITE, we can collapse everything 429 * into a single op. 430 * 431 * POSTREAD -- Nothing. 432 * 433 * POSTWRITE -- Nothing. 434 */ 435 436 /* 437 * Flush the write buffer. 438 */ 439 wbflush(); 440 441 /* 442 * If the mapping is of COHERENT DMA-safe memory, no cache 443 * flush is necessary. 444 */ 445 if (map->_dm_flags & EWS4800MIPS_DMAMAP_COHERENT) 446 return; 447 448 /* 449 * No cache flushes are necessary if we're only doing 450 * POSTREAD or POSTWRITE (i.e. not doing PREREAD or PREWRITE). 451 */ 452 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) == 0) 453 return; 454 455 /* 456 * If the mapping belongs to the kernel, or it belongs 457 * to the currently-running process (XXX actually, vmspace), 458 * then we can use Hit ops. Otherwise, Index ops. 459 * 460 * This should be true the vast majority of the time. 461 */ 462 if (__predict_true(VMSPACE_IS_KERNEL_P(map->_dm_vmspace) || 463 map->_dm_vmspace == curproc->p_vmspace)) 464 useindex = 0; 465 else 466 useindex = 1; 467 468 for (i = 0; i < map->dm_nsegs && len != 0; i++) { 469 /* Find the beginning segment. */ 470 if (offset >= map->dm_segs[i].ds_len) { 471 offset -= map->dm_segs[i].ds_len; 472 continue; 473 } 474 475 /* 476 * Now at the first segment to sync; nail 477 * each segment until we have exhausted the 478 * length. 479 */ 480 minlen = len < map->dm_segs[i].ds_len - offset ? 481 len : map->dm_segs[i].ds_len - offset; 482 483 vaddr = map->dm_segs[i]._ds_vaddr; 484 485 #ifdef BUS_DMA_DEBUG 486 printf("bus_dmamap_sync: flushing segment %d " 487 "(0x%x+%lx, 0x%x+0x%lx) (olen = %ld)...", i, 488 vaddr, offset, vaddr, offset + minlen - 1, len); 489 #endif 490 491 /* 492 * If we are forced to use Index ops, it's always a 493 * Write-back,Invalidate, so just do one test. 494 */ 495 if (__predict_false(useindex)) { 496 mips_dcache_wbinv_range_index(vaddr + offset, minlen); 497 #ifdef BUS_DMA_DEBUG 498 printf("\n"); 499 #endif 500 offset = 0; 501 len -= minlen; 502 continue; 503 } 504 505 start = vaddr + offset; 506 switch (ops) { 507 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE: 508 mips_dcache_wbinv_range(start, minlen); 509 break; 510 511 case BUS_DMASYNC_PREREAD: { 512 const struct mips_cache_info * const mci = &mips_cache_info; 513 end = start + minlen; 514 preboundary = start & ~mci->mci_dcache_align_mask; 515 firstboundary = (start + mci->mci_dcache_align_mask) 516 & ~mci->mci_dcache_align_mask; 517 lastboundary = end & ~mci->mci_dcache_align_mask; 518 if (preboundary < start && preboundary < lastboundary) 519 mips_dcache_wbinv_range(preboundary, 520 mci->mci_dcache_align); 521 if (firstboundary < lastboundary) 522 mips_dcache_inv_range(firstboundary, 523 lastboundary - firstboundary); 524 if (lastboundary < end) 525 mips_dcache_wbinv_range(lastboundary, 526 mci->mci_dcache_align); 527 break; 528 } 529 530 case BUS_DMASYNC_PREWRITE: 531 mips_dcache_wb_range(start, minlen); 532 break; 533 } 534 #ifdef BUS_DMA_DEBUG 535 printf("\n"); 536 #endif 537 offset = 0; 538 len -= minlen; 539 } 540 } 541 542 /* 543 * Common function for DMA-safe memory allocation. May be called 544 * by bus-specific DMA memory allocation functions. 545 */ 546 int 547 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment, 548 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs, 549 int flags) 550 { 551 552 return _bus_dmamem_alloc_range_common(t, size, alignment, boundary, 553 segs, nsegs, rsegs, flags, 554 pmap_limits.avail_start /*low*/, 555 pmap_limits.avail_end - PAGE_SIZE /*high*/); 556 } 557 558 /* 559 * Common function for freeing DMA-safe memory. May be called by 560 * bus-specific DMA memory free functions. 561 */ 562 void 563 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs) 564 { 565 566 _bus_dmamem_free_common(t, segs, nsegs); 567 } 568 569 /* 570 * Common function for mapping DMA-safe memory. May be called by 571 * bus-specific DMA memory map functions. 572 */ 573 int 574 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, 575 size_t size, void **kvap, int flags) 576 { 577 578 /* 579 * If we're only mapping 1 segment, use KSEG0 or KSEG1, to avoid 580 * TLB thrashing. 581 */ 582 if (nsegs == 1) { 583 if (flags & BUS_DMA_COHERENT) 584 *kvap = (void *)MIPS_PHYS_TO_KSEG1(segs[0].ds_addr); 585 else 586 *kvap = (void *)MIPS_PHYS_TO_KSEG0(segs[0].ds_addr); 587 return 0; 588 } 589 590 /* XXX BUS_DMA_COHERENT */ 591 return (_bus_dmamem_map_common(t, segs, nsegs, size, kvap, flags, 0)); 592 } 593 594 /* 595 * Common function for unmapping DMA-safe memory. May be called by 596 * bus-specific DMA memory unmapping functions. 597 */ 598 void 599 _bus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size) 600 { 601 602 /* 603 * Nothing to do if we mapped it with KSEG0 or KSEG1 (i.e. 604 * not in KSEG2). 605 */ 606 if (kva >= (void *)MIPS_KSEG0_START && 607 kva < (void *)MIPS_KSEG2_START) 608 return; 609 610 _bus_dmamem_unmap_common(t, kva, size); 611 } 612 613 /* 614 * Common function for mmap(2)'ing DMA-safe memory. May be called by 615 * bus-specific DMA mmap(2)'ing functions. 616 */ 617 paddr_t 618 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, 619 off_t off, int prot, int flags) 620 { 621 bus_addr_t rv; 622 623 rv = _bus_dmamem_mmap_common(t, segs, nsegs, off, prot, flags); 624 if (rv == (bus_addr_t)-1) 625 return (-1); 626 627 return (mips_btop((char *)rv)); 628 } 629