bus_dma.c revision 1.46
1/* $NetBSD: bus_dma.c,v 1.46 2001/05/26 21:27:03 chs 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 41 42__KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.46 2001/05/26 21:27:03 chs Exp $"); 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/kernel.h> 47#include <sys/device.h> 48#include <sys/malloc.h> 49#include <sys/proc.h> 50#include <sys/mbuf.h> 51 52#include <uvm/uvm_extern.h> 53 54#define _ALPHA_BUS_DMA_PRIVATE 55#include <machine/bus.h> 56#include <machine/intr.h> 57 58int _bus_dmamap_load_buffer_direct(bus_dma_tag_t, 59 bus_dmamap_t, void *, bus_size_t, struct proc *, int, 60 paddr_t *, int *, int); 61 62extern paddr_t avail_start, avail_end; /* from pmap.c */ 63 64/* 65 * Common function for DMA map creation. May be called by bus-specific 66 * DMA map creation functions. 67 */ 68int 69_bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 70 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp) 71{ 72 struct alpha_bus_dmamap *map; 73 void *mapstore; 74 size_t mapsize; 75 76 /* 77 * Allocate and initialize the DMA map. The end of the map 78 * is a variable-sized array of segments, so we allocate enough 79 * room for them in one shot. 80 * 81 * Note we don't preserve the WAITOK or NOWAIT flags. Preservation 82 * of ALLOCNOW notifes others that we've reserved these resources, 83 * and they are not to be freed. 84 * 85 * The bus_dmamap_t includes one bus_dma_segment_t, hence 86 * the (nsegments - 1). 87 */ 88 mapsize = sizeof(struct alpha_bus_dmamap) + 89 (sizeof(bus_dma_segment_t) * (nsegments - 1)); 90 if ((mapstore = malloc(mapsize, M_DMAMAP, 91 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) 92 return (ENOMEM); 93 94 bzero(mapstore, mapsize); 95 map = (struct alpha_bus_dmamap *)mapstore; 96 map->_dm_size = size; 97 map->_dm_segcnt = nsegments; 98 map->_dm_maxsegsz = maxsegsz; 99 if (t->_boundary != 0 && t->_boundary < boundary) 100 map->_dm_boundary = t->_boundary; 101 else 102 map->_dm_boundary = boundary; 103 map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT); 104 map->dm_mapsize = 0; /* no valid mappings */ 105 map->dm_nsegs = 0; 106 107 *dmamp = map; 108 return (0); 109} 110 111/* 112 * Common function for DMA map destruction. May be called by bus-specific 113 * DMA map destruction functions. 114 */ 115void 116_bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 117{ 118 119 free(map, M_DMAMAP); 120} 121 122/* 123 * Utility function to load a linear buffer. lastaddrp holds state 124 * between invocations (for multiple-buffer loads). segp contains 125 * the starting segment on entrance, and the ending segment on exit. 126 * first indicates if this is the first invocation of this function. 127 */ 128int 129_bus_dmamap_load_buffer_direct(bus_dma_tag_t t, bus_dmamap_t map, 130 void *buf, size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp, 131 int *segp, int first) 132{ 133 bus_size_t sgsize; 134 bus_addr_t curaddr, lastaddr, baddr, bmask; 135 vaddr_t vaddr = (vaddr_t)buf; 136 int seg; 137 138 lastaddr = *lastaddrp; 139 bmask = ~(map->_dm_boundary - 1); 140 141 for (seg = *segp; buflen > 0 ; ) { 142 /* 143 * Get the physical address for this segment. 144 */ 145 if (p != NULL) 146 (void) pmap_extract(p->p_vmspace->vm_map.pmap, 147 vaddr, &curaddr); 148 else 149 curaddr = vtophys(vaddr); 150 151 /* 152 * If we're beyond the current DMA window, indicate 153 * that and try to fall back into SGMAPs. 154 */ 155 if (t->_wsize != 0 && curaddr >= t->_wsize) 156 return (EINVAL); 157 158 curaddr |= t->_wbase; 159 160 /* 161 * Compute the segment size, and adjust counts. 162 */ 163 sgsize = NBPG - ((u_long)vaddr & PGOFSET); 164 if (buflen < sgsize) 165 sgsize = buflen; 166 if (map->_dm_maxsegsz < sgsize) 167 sgsize = map->_dm_maxsegsz; 168 169 /* 170 * Make sure we don't cross any boundaries. 171 */ 172 if (map->_dm_boundary > 0) { 173 baddr = (curaddr + map->_dm_boundary) & bmask; 174 if (sgsize > (baddr - curaddr)) 175 sgsize = (baddr - curaddr); 176 } 177 178 /* 179 * Insert chunk into a segment, coalescing with 180 * the previous segment if possible. 181 */ 182 if (first) { 183 map->dm_segs[seg].ds_addr = curaddr; 184 map->dm_segs[seg].ds_len = sgsize; 185 first = 0; 186 } else { 187 if ((map->_dm_flags & DMAMAP_NO_COALESCE) == 0 && 188 curaddr == lastaddr && 189 (map->dm_segs[seg].ds_len + sgsize) <= 190 map->_dm_maxsegsz && 191 (map->_dm_boundary == 0 || 192 (map->dm_segs[seg].ds_addr & bmask) == 193 (curaddr & bmask))) 194 map->dm_segs[seg].ds_len += sgsize; 195 else { 196 if (++seg >= map->_dm_segcnt) 197 break; 198 map->dm_segs[seg].ds_addr = curaddr; 199 map->dm_segs[seg].ds_len = sgsize; 200 } 201 } 202 203 lastaddr = curaddr + sgsize; 204 vaddr += sgsize; 205 buflen -= sgsize; 206 } 207 208 *segp = seg; 209 *lastaddrp = lastaddr; 210 211 /* 212 * Did we fit? 213 */ 214 if (buflen != 0) { 215 /* 216 * If there is a chained window, we will automatically 217 * fall back to it. 218 */ 219 return (EFBIG); /* XXX better return value here? */ 220 } 221 222 return (0); 223} 224 225/* 226 * Common function for loading a direct-mapped DMA map with a linear 227 * buffer. Called by bus-specific DMA map load functions with the 228 * OR value appropriate for indicating "direct-mapped" for that 229 * chipset. 230 */ 231int 232_bus_dmamap_load_direct(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 233 bus_size_t buflen, struct proc *p, int flags) 234{ 235 paddr_t lastaddr; 236 int seg, error; 237 238 /* 239 * Make sure that on error condition we return "no valid mappings". 240 */ 241 map->dm_mapsize = 0; 242 map->dm_nsegs = 0; 243 244 if (buflen > map->_dm_size) 245 return (EINVAL); 246 247 seg = 0; 248 error = _bus_dmamap_load_buffer_direct(t, map, buf, buflen, 249 p, flags, &lastaddr, &seg, 1); 250 if (error == 0) { 251 map->dm_mapsize = buflen; 252 map->dm_nsegs = seg + 1; 253 } else if (t->_next_window != NULL) { 254 /* 255 * Give the next window a chance. 256 */ 257 error = bus_dmamap_load(t->_next_window, map, buf, buflen, 258 p, flags); 259 } 260 return (error); 261} 262 263/* 264 * Like _bus_dmamap_load_direct(), but for mbufs. 265 */ 266int 267_bus_dmamap_load_mbuf_direct(bus_dma_tag_t t, bus_dmamap_t map, 268 struct mbuf *m0, int flags) 269{ 270 paddr_t lastaddr; 271 int seg, error, first; 272 struct mbuf *m; 273 274 /* 275 * Make sure that on error condition we return "no valid mappings." 276 */ 277 map->dm_mapsize = 0; 278 map->dm_nsegs = 0; 279 280#ifdef DIAGNOSTIC 281 if ((m0->m_flags & M_PKTHDR) == 0) 282 panic("_bus_dmamap_load_mbuf_direct: no packet header"); 283#endif 284 285 if (m0->m_pkthdr.len > map->_dm_size) 286 return (EINVAL); 287 288 first = 1; 289 seg = 0; 290 error = 0; 291 for (m = m0; m != NULL && error == 0; m = m->m_next) { 292 error = _bus_dmamap_load_buffer_direct(t, map, 293 m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first); 294 first = 0; 295 } 296 if (error == 0) { 297 map->dm_mapsize = m0->m_pkthdr.len; 298 map->dm_nsegs = seg + 1; 299 } else if (t->_next_window != NULL) { 300 /* 301 * Give the next window a chance. 302 */ 303 error = bus_dmamap_load_mbuf(t->_next_window, map, m0, flags); 304 } 305 return (error); 306} 307 308/* 309 * Like _bus_dmamap_load_direct(), but for uios. 310 */ 311int 312_bus_dmamap_load_uio_direct(bus_dma_tag_t t, bus_dmamap_t map, 313 struct uio *uio, int flags) 314{ 315 paddr_t lastaddr; 316 int seg, i, error, first; 317 bus_size_t minlen, resid; 318 struct proc *p = NULL; 319 struct iovec *iov; 320 caddr_t addr; 321 322 /* 323 * Make sure that on error condition we return "no valid mappings." 324 */ 325 map->dm_mapsize = 0; 326 map->dm_nsegs = 0; 327 328 resid = uio->uio_resid; 329 iov = uio->uio_iov; 330 331 if (uio->uio_segflg == UIO_USERSPACE) { 332 p = uio->uio_procp; 333#ifdef DIAGNOSTIC 334 if (p == NULL) 335 panic("_bus_dmamap_load_uio_direct: " 336 "USERSPACE but no proc"); 337#endif 338 } 339 340 first = 1; 341 seg = 0; 342 error = 0; 343 for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) { 344 /* 345 * Now at the first iovec to load. Load each iovec 346 * until we have exhausted the residual count. 347 */ 348 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len; 349 addr = (caddr_t)iov[i].iov_base; 350 351 error = _bus_dmamap_load_buffer_direct(t, map, 352 addr, minlen, p, flags, &lastaddr, &seg, first); 353 first = 0; 354 355 resid -= minlen; 356 } 357 if (error == 0) { 358 map->dm_mapsize = uio->uio_resid; 359 map->dm_nsegs = seg + 1; 360 } else if (t->_next_window != NULL) { 361 /* 362 * Give the next window a chance. 363 */ 364 error = bus_dmamap_load_uio(t->_next_window, map, uio, flags); 365 } 366 return (error); 367} 368 369/* 370 * Like _bus_dmamap_load_direct(), but for raw memory. 371 */ 372int 373_bus_dmamap_load_raw_direct(bus_dma_tag_t t, bus_dmamap_t map, 374 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 375{ 376 377 panic("_bus_dmamap_load_raw_direct: not implemented"); 378} 379 380/* 381 * Common function for unloading a DMA map. May be called by 382 * chipset-specific DMA map unload functions. 383 */ 384void 385_bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 386{ 387 388 /* 389 * No resources to free; just mark the mappings as 390 * invalid. 391 */ 392 map->dm_mapsize = 0; 393 map->dm_nsegs = 0; 394} 395 396/* 397 * Common function for DMA map synchronization. May be called 398 * by chipset-specific DMA map synchronization functions. 399 */ 400void 401_bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 402 bus_size_t len, int ops) 403{ 404 405 /* 406 * Flush the store buffer. 407 */ 408 alpha_mb(); 409} 410 411/* 412 * Common function for DMA-safe memory allocation. May be called 413 * by bus-specific DMA memory allocation functions. 414 */ 415int 416_bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment, 417 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs, 418 int flags) 419{ 420 421 return (_bus_dmamem_alloc_range(t, size, alignment, boundary, 422 segs, nsegs, rsegs, flags, 0, trunc_page(avail_end))); 423} 424 425/* 426 * Allocate physical memory from the given physical address range. 427 * Called by DMA-safe memory allocation methods. 428 */ 429int 430_bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment, 431 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs, 432 int flags, paddr_t low, paddr_t high) 433{ 434 paddr_t curaddr, lastaddr; 435 struct vm_page *m; 436 struct pglist mlist; 437 int curseg, error; 438 439 /* Always round the size. */ 440 size = round_page(size); 441 442 high = avail_end - PAGE_SIZE; 443 444 /* 445 * Allocate pages from the VM system. 446 */ 447 TAILQ_INIT(&mlist); 448 error = uvm_pglistalloc(size, low, high, alignment, boundary, 449 &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0); 450 if (error) 451 return (error); 452 453 /* 454 * Compute the location, size, and number of segments actually 455 * returned by the VM code. 456 */ 457 m = mlist.tqh_first; 458 curseg = 0; 459 lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m); 460 segs[curseg].ds_len = PAGE_SIZE; 461 m = m->pageq.tqe_next; 462 463 for (; m != NULL; m = m->pageq.tqe_next) { 464 curaddr = VM_PAGE_TO_PHYS(m); 465#ifdef DIAGNOSTIC 466 if (curaddr < avail_start || curaddr >= high) { 467 printf("uvm_pglistalloc returned non-sensical" 468 " address 0x%lx\n", curaddr); 469 panic("_bus_dmamem_alloc"); 470 } 471#endif 472 if (curaddr == (lastaddr + PAGE_SIZE)) 473 segs[curseg].ds_len += PAGE_SIZE; 474 else { 475 curseg++; 476 segs[curseg].ds_addr = curaddr; 477 segs[curseg].ds_len = PAGE_SIZE; 478 } 479 lastaddr = curaddr; 480 } 481 482 *rsegs = curseg + 1; 483 484 return (0); 485} 486 487/* 488 * Common function for freeing DMA-safe memory. May be called by 489 * bus-specific DMA memory free functions. 490 */ 491void 492_bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs) 493{ 494 struct vm_page *m; 495 bus_addr_t addr; 496 struct pglist mlist; 497 int curseg; 498 499 /* 500 * Build a list of pages to free back to the VM system. 501 */ 502 TAILQ_INIT(&mlist); 503 for (curseg = 0; curseg < nsegs; curseg++) { 504 for (addr = segs[curseg].ds_addr; 505 addr < (segs[curseg].ds_addr + segs[curseg].ds_len); 506 addr += PAGE_SIZE) { 507 m = PHYS_TO_VM_PAGE(addr); 508 TAILQ_INSERT_TAIL(&mlist, m, pageq); 509 } 510 } 511 512 uvm_pglistfree(&mlist); 513} 514 515/* 516 * Common function for mapping DMA-safe memory. May be called by 517 * bus-specific DMA memory map functions. 518 */ 519int 520_bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, 521 size_t size, caddr_t *kvap, int flags) 522{ 523 vaddr_t va; 524 bus_addr_t addr; 525 int curseg; 526 527 /* 528 * If we're only mapping 1 segment, use K0SEG, to avoid 529 * TLB thrashing. 530 */ 531 if (nsegs == 1) { 532 *kvap = (caddr_t)ALPHA_PHYS_TO_K0SEG(segs[0].ds_addr); 533 return (0); 534 } 535 536 size = round_page(size); 537 538 va = uvm_km_valloc(kernel_map, size); 539 540 if (va == 0) 541 return (ENOMEM); 542 543 *kvap = (caddr_t)va; 544 545 for (curseg = 0; curseg < nsegs; curseg++) { 546 for (addr = segs[curseg].ds_addr; 547 addr < (segs[curseg].ds_addr + segs[curseg].ds_len); 548 addr += NBPG, va += NBPG, size -= NBPG) { 549 if (size == 0) 550 panic("_bus_dmamem_map: size botch"); 551 pmap_enter(pmap_kernel(), va, addr, 552 VM_PROT_READ | VM_PROT_WRITE, 553 PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE); 554 } 555 } 556 pmap_update(); 557 558 return (0); 559} 560 561/* 562 * Common function for unmapping DMA-safe memory. May be called by 563 * bus-specific DMA memory unmapping functions. 564 */ 565void 566_bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size) 567{ 568 569#ifdef DIAGNOSTIC 570 if ((u_long)kva & PGOFSET) 571 panic("_bus_dmamem_unmap"); 572#endif 573 574 /* 575 * Nothing to do if we mapped it with K0SEG. 576 */ 577 if (kva >= (caddr_t)ALPHA_K0SEG_BASE && 578 kva <= (caddr_t)ALPHA_K0SEG_END) 579 return; 580 581 size = round_page(size); 582 uvm_km_free(kernel_map, (vaddr_t)kva, size); 583} 584 585/* 586 * Common functin for mmap(2)'ing DMA-safe memory. May be called by 587 * bus-specific DMA mmap(2)'ing functions. 588 */ 589paddr_t 590_bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, 591 off_t off, int prot, int flags) 592{ 593 int i; 594 595 for (i = 0; i < nsegs; i++) { 596#ifdef DIAGNOSTIC 597 if (off & PGOFSET) 598 panic("_bus_dmamem_mmap: offset unaligned"); 599 if (segs[i].ds_addr & PGOFSET) 600 panic("_bus_dmamem_mmap: segment unaligned"); 601 if (segs[i].ds_len & PGOFSET) 602 panic("_bus_dmamem_mmap: segment size not multiple" 603 " of page size"); 604#endif 605 if (off >= segs[i].ds_len) { 606 off -= segs[i].ds_len; 607 continue; 608 } 609 610 return (alpha_btop((caddr_t)segs[i].ds_addr + off)); 611 } 612 613 /* Page not found. */ 614 return (-1); 615} 616