1/* $NetBSD: isadma_bounce.c,v 1.12 2011/02/20 07:52:42 matt Exp $ */ 2/* NetBSD: isadma_bounce.c,v 1.2 2000/06/01 05:49:36 thorpej Exp */ 3 4/*- 5 * Copyright (c) 1996, 1997, 1998, 2000 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 35 36__KERNEL_RCSID(0, "$NetBSD: isadma_bounce.c,v 1.12 2011/02/20 07:52:42 matt Exp $"); 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/syslog.h> 41#include <sys/device.h> 42#include <sys/malloc.h> 43#include <sys/proc.h> 44#include <sys/mbuf.h> 45 46#define _ARC_BUS_DMA_PRIVATE 47#include <sys/bus.h> 48 49#include <dev/isa/isareg.h> 50#include <dev/isa/isavar.h> 51 52#include <uvm/uvm_extern.h> 53 54/* 55 * Cookie used by bouncing ISA DMA. A pointer to one of these is stashed 56 * in the DMA map. 57 */ 58struct isadma_bounce_cookie { 59 int id_flags; /* flags; see below */ 60 61 /* 62 * Information about the original buffer used during 63 * DMA map syncs. Note that origbuflen is only used 64 * for ID_BUFTYPE_LINEAR. 65 */ 66 void *id_origbuf; /* pointer to orig buffer if 67 bouncing */ 68 bus_size_t id_origbuflen; /* ...and size */ 69 int id_buftype; /* type of buffer */ 70 71 void *id_bouncebuf; /* pointer to the bounce buffer */ 72 bus_size_t id_bouncebuflen; /* ...and size */ 73 int id_nbouncesegs; /* number of valid bounce segs */ 74 bus_dma_segment_t id_bouncesegs[1]; /* array of bounce buffer 75 physical memory segments */ 76}; 77 78/* id_flags */ 79#define ID_MIGHT_NEED_BOUNCE 0x01 /* map could need bounce buffers */ 80#define ID_HAS_BOUNCE 0x02 /* map currently has bounce buffers */ 81#define ID_IS_BOUNCING 0x04 /* map is bouncing current xfer */ 82 83/* id_buftype */ 84#define ID_BUFTYPE_INVALID 0 85#define ID_BUFTYPE_LINEAR 1 86#define ID_BUFTYPE_MBUF 2 87#define ID_BUFTYPE_UIO 3 88#define ID_BUFTYPE_RAW 4 89 90int isadma_bounce_dmamap_create(bus_dma_tag_t, bus_size_t, int, 91 bus_size_t, bus_size_t, int, bus_dmamap_t *); 92void isadma_bounce_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t); 93int isadma_bounce_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, 94 bus_size_t, struct proc *, int); 95int isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t, 96 struct mbuf *, int); 97int isadma_bounce_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t, 98 struct uio *, int); 99int isadma_bounce_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t, 100 bus_dma_segment_t *, int, bus_size_t, int); 101void isadma_bounce_dmamap_unload(bus_dma_tag_t, bus_dmamap_t); 102void isadma_bounce_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, 103 bus_addr_t, bus_size_t, int); 104int isadma_bounce_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t, 105 bus_size_t, bus_dma_segment_t *, int, int *, int); 106 107static int isadma_bounce_alloc_bouncebuf(bus_dma_tag_t, bus_dmamap_t, 108 bus_size_t, int); 109static void isadma_bounce_free_bouncebuf(bus_dma_tag_t, bus_dmamap_t); 110 111void 112isadma_bounce_tag_init(bus_dma_tag_t t) 113{ 114 /* 115 * Initialize the DMA tag used for ISA DMA. 116 */ 117 118 _bus_dma_tag_init(t); 119 120 t->_dmamap_create = isadma_bounce_dmamap_create; 121 t->_dmamap_destroy = isadma_bounce_dmamap_destroy; 122 t->_dmamap_load = isadma_bounce_dmamap_load; 123 t->_dmamap_load_mbuf = isadma_bounce_dmamap_load_mbuf; 124 t->_dmamap_load_uio = isadma_bounce_dmamap_load_uio; 125 t->_dmamap_load_raw = isadma_bounce_dmamap_load_raw; 126 t->_dmamap_unload = isadma_bounce_dmamap_unload; 127 t->_dmamap_sync = isadma_bounce_dmamap_sync; 128 t->_dmamem_alloc = isadma_bounce_dmamem_alloc; 129} 130 131/* 132 * Create an ISA DMA map. 133 */ 134int 135isadma_bounce_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 136 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp) 137{ 138 struct isadma_bounce_cookie *cookie; 139 bus_dmamap_t map; 140 int error, cookieflags; 141 void *cookiestore; 142 size_t cookiesize; 143 144 /* Call common function to create the basic map. */ 145 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, 146 flags, dmamp); 147 if (error) 148 return error; 149 150 map = *dmamp; 151 map->_dm_cookie = NULL; 152 153 cookiesize = sizeof(*cookie); 154 155 /* 156 * ISA only has 24-bits of address space. This means 157 * we can't DMA to pages over 16M. In order to DMA to 158 * arbitrary buffers, we use "bounce buffers" - pages 159 * in memory below the 16M boundary. On DMA reads, 160 * DMA happens to the bounce buffers, and is copied into 161 * the caller's buffer. On writes, data is copied into 162 * but bounce buffer, and the DMA happens from those 163 * pages. To software using the DMA mapping interface, 164 * this looks simply like a data cache. 165 * 166 * If we have more than 16M of RAM in the system, we may 167 * need bounce buffers. We check and remember that here. 168 * 169 * ...or, there is an opposite case. The most segments 170 * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If 171 * the caller can't handle that many segments (e.g. the 172 * ISA DMA controller), we may have to bounce it as well. 173 */ 174 cookieflags = 0; 175 if (mips_avail_end > ISA_DMA_BOUNCE_THRESHOLD || 176 ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) { 177 cookieflags |= ID_MIGHT_NEED_BOUNCE; 178 cookiesize += (sizeof(bus_dma_segment_t) * 179 (map->_dm_segcnt - 1)); 180 } 181 182 /* 183 * Allocate our cookie. 184 */ 185 if ((cookiestore = malloc(cookiesize, M_DMAMAP, 186 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) { 187 error = ENOMEM; 188 goto out; 189 } 190 memset(cookiestore, 0, cookiesize); 191 cookie = (struct isadma_bounce_cookie *)cookiestore; 192 cookie->id_flags = cookieflags; 193 map->_dm_cookie = cookie; 194 195 if (cookieflags & ID_MIGHT_NEED_BOUNCE) { 196 /* 197 * Allocate the bounce pages now if the caller 198 * wishes us to do so. 199 */ 200 if ((flags & BUS_DMA_ALLOCNOW) == 0) 201 goto out; 202 203 error = isadma_bounce_alloc_bouncebuf(t, map, size, flags); 204 } 205 206 out: 207 if (error) { 208 if (map->_dm_cookie != NULL) 209 free(map->_dm_cookie, M_DMAMAP); 210 _bus_dmamap_destroy(t, map); 211 } 212 return error; 213} 214 215/* 216 * Destroy an ISA DMA map. 217 */ 218void 219isadma_bounce_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 220{ 221 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 222 223 /* 224 * Free any bounce pages this map might hold. 225 */ 226 if (cookie->id_flags & ID_HAS_BOUNCE) 227 isadma_bounce_free_bouncebuf(t, map); 228 229 free(cookie, M_DMAMAP); 230 _bus_dmamap_destroy(t, map); 231} 232 233/* 234 * Load an ISA DMA map with a linear buffer. 235 */ 236int 237isadma_bounce_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 238 bus_size_t buflen, struct proc *p, int flags) 239{ 240 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 241 int error; 242 243 /* 244 * Make sure that on error condition we return "no valid mappings." 245 */ 246 map->dm_mapsize = 0; 247 map->dm_nsegs = 0; 248 249 /* 250 * Try to load the map the normal way. If this errors out, 251 * and we can bounce, we will. 252 */ 253 error = _bus_dmamap_load(t, map, buf, buflen, p, flags); 254 if (error == 0 || 255 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0)) 256 return error; 257 258 /* 259 * First attempt failed; bounce it. 260 */ 261 262 /* 263 * Allocate bounce pages, if necessary. 264 */ 265 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) { 266 error = isadma_bounce_alloc_bouncebuf(t, map, buflen, flags); 267 if (error) 268 return error; 269 } 270 271 /* 272 * Cache a pointer to the caller's buffer and load the DMA map 273 * with the bounce buffer. 274 */ 275 cookie->id_origbuf = buf; 276 cookie->id_origbuflen = buflen; 277 cookie->id_buftype = ID_BUFTYPE_LINEAR; 278 error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen, 279 p, flags); 280 if (error) { 281 /* 282 * Free the bounce pages, unless our resources 283 * are reserved for our exclusive use. 284 */ 285 if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 286 isadma_bounce_free_bouncebuf(t, map); 287 return error; 288 } 289 290 /* ...so isadma_bounce_dmamap_sync() knows we're bouncing */ 291 cookie->id_flags |= ID_IS_BOUNCING; 292 return 0; 293} 294 295/* 296 * Like isadma_bounce_dmamap_load(), but for mbufs. 297 */ 298int 299isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, 300 struct mbuf *m0, int flags) 301{ 302 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 303 int error; 304 305 /* 306 * Make sure on error condition we return "no valid mappings." 307 */ 308 map->dm_mapsize = 0; 309 map->dm_nsegs = 0; 310 311#ifdef DIAGNOSTIC 312 if ((m0->m_flags & M_PKTHDR) == 0) 313 panic("isadma_bounce_dmamap_load_mbuf: no packet header"); 314#endif 315 316 if (m0->m_pkthdr.len > map->_dm_size) 317 return EINVAL; 318 319 /* 320 * Try to load the map the normal way. If this errors out, 321 * and we can bounce, we will. 322 */ 323 error = _bus_dmamap_load_mbuf(t, map, m0, flags); 324 if (error == 0 || 325 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0)) 326 return error; 327 328 /* 329 * First attempt failed; bounce it. 330 */ 331 332 /* 333 * Allocate bounce pages, if necessary. 334 */ 335 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) { 336 error = isadma_bounce_alloc_bouncebuf(t, map, m0->m_pkthdr.len, 337 flags); 338 if (error) 339 return error; 340 } 341 342 /* 343 * Cache a pointer to the caller's buffer and load the DMA map 344 * with the bounce buffer. 345 */ 346 cookie->id_origbuf = m0; 347 cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */ 348 cookie->id_buftype = ID_BUFTYPE_MBUF; 349 error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, 350 m0->m_pkthdr.len, NULL, flags); 351 if (error) { 352 /* 353 * Free the bounce pages, unless our resources 354 * are reserved for our exclusive use. 355 */ 356 if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 357 isadma_bounce_free_bouncebuf(t, map); 358 return error; 359 } 360 361 /* ...so isadma_bounce_dmamap_sync() knows we're bouncing */ 362 cookie->id_flags |= ID_IS_BOUNCING; 363 return 0; 364} 365 366/* 367 * Like isadma_bounce_dmamap_load(), but for uios. 368 */ 369int 370isadma_bounce_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, 371 struct uio *uio, int flags) 372{ 373 374 panic("isadma_bounce_dmamap_load_uio: not implemented"); 375} 376 377/* 378 * Like isadma_bounce_dmamap_load(), but for raw memory allocated with 379 * bus_dmamem_alloc(). 380 */ 381int 382isadma_bounce_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, 383 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 384{ 385 386 panic("isadma_bounce_dmamap_load_raw: not implemented"); 387} 388 389/* 390 * Unload an ISA DMA map. 391 */ 392void 393isadma_bounce_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 394{ 395 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 396 397 /* 398 * If we have bounce pages, free them, unless they're 399 * reserved for our exclusive use. 400 */ 401 if ((cookie->id_flags & ID_HAS_BOUNCE) && 402 (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 403 isadma_bounce_free_bouncebuf(t, map); 404 405 cookie->id_flags &= ~ID_IS_BOUNCING; 406 cookie->id_buftype = ID_BUFTYPE_INVALID; 407 408 /* 409 * Do the generic bits of the unload. 410 */ 411 _bus_dmamap_unload(t, map); 412} 413 414/* 415 * Synchronize an ISA DMA map. 416 */ 417void 418isadma_bounce_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 419 bus_size_t len, int ops) 420{ 421 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 422 void (*sync)(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, bus_size_t, int); 423 424 sync = _bus_dmamap_sync; 425 426 /* 427 * Mixing PRE and POST operations is not allowed. 428 */ 429 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 && 430 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0) 431 panic("isadma_bounce_dmamap_sync: mix PRE and POST"); 432 433#ifdef DIAGNOSTIC 434 if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) { 435 if (offset >= map->dm_mapsize) 436 panic("isadma_bounce_dmamap_sync: bad offset"); 437 if (len == 0 || (offset + len) > map->dm_mapsize) 438 panic("isadma_bounce_dmamap_sync: bad length"); 439 } 440#endif 441 442 /* 443 * If we're not bouncing, just drain the write buffer 444 * and flush cache. 445 */ 446 if ((cookie->id_flags & ID_IS_BOUNCING) == 0) { 447 ((*sync)(t, map, offset, len, ops)); 448 return; 449 } 450 451 /* 452 * XXX 453 * This should be needed in BUS_DMASYNC_POSTREAD case only, 454 * if _mips3_bus_dmamap_sync() used "Hit_Invalidate on POSTREAD", 455 * rather than "Hit_Write_Back_Invalidate on PREREAD". 456 */ 457 if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_POSTREAD)) 458 ((*sync)(t, map, offset, len, ops)); 459 460 switch (cookie->id_buftype) { 461 case ID_BUFTYPE_LINEAR: 462 /* 463 * Nothing to do for pre-read. 464 */ 465 466 if (ops & BUS_DMASYNC_PREWRITE) { 467 /* 468 * Copy the caller's buffer to the bounce buffer. 469 */ 470 memcpy((char *)cookie->id_bouncebuf + offset, 471 (char *)cookie->id_origbuf + offset, len); 472 } 473 474 if (ops & BUS_DMASYNC_POSTREAD) { 475 /* 476 * Copy the bounce buffer to the caller's buffer. 477 */ 478 memcpy((char *)cookie->id_origbuf + offset, 479 (char *)cookie->id_bouncebuf + offset, len); 480 } 481 482 /* 483 * Nothing to do for post-write. 484 */ 485 break; 486 487 case ID_BUFTYPE_MBUF: 488 { 489 struct mbuf *m, *m0 = cookie->id_origbuf; 490 bus_size_t minlen, moff; 491 492 /* 493 * Nothing to do for pre-read. 494 */ 495 496 if (ops & BUS_DMASYNC_PREWRITE) { 497 /* 498 * Copy the caller's buffer to the bounce buffer. 499 */ 500 m_copydata(m0, offset, len, 501 (char *)cookie->id_bouncebuf + offset); 502 } 503 504 if (ops & BUS_DMASYNC_POSTREAD) { 505 /* 506 * Copy the bounce buffer to the caller's buffer. 507 */ 508 for (moff = offset, m = m0; m != NULL && len != 0; 509 m = m->m_next) { 510 /* Find the beginning mbuf. */ 511 if (moff >= m->m_len) { 512 moff -= m->m_len; 513 continue; 514 } 515 516 /* 517 * Now at the first mbuf to sync; nail 518 * each one until we have exhausted the 519 * length. 520 */ 521 minlen = len < m->m_len - moff ? 522 len : m->m_len - moff; 523 524 memcpy(mtod(m, char *) + moff, 525 (char *)cookie->id_bouncebuf + offset, 526 minlen); 527 528 moff = 0; 529 len -= minlen; 530 offset += minlen; 531 } 532 } 533 534 /* 535 * Nothing to do for post-write. 536 */ 537 break; 538 } 539 540 case ID_BUFTYPE_UIO: 541 panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_UIO"); 542 break; 543 544 case ID_BUFTYPE_RAW: 545 panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_RAW"); 546 break; 547 548 case ID_BUFTYPE_INVALID: 549 panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_INVALID"); 550 break; 551 552 default: 553 printf("unknown buffer type %d\n", cookie->id_buftype); 554 panic("isadma_bounce_dmamap_sync"); 555 } 556 557 if (ops & BUS_DMASYNC_PREWRITE) 558 ((*sync)(t, map, offset, len, ops)); 559} 560 561/* 562 * Allocate memory safe for ISA DMA. 563 */ 564int 565isadma_bounce_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, 566 bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs, 567 int nsegs, int *rsegs, int flags) 568{ 569 paddr_t high; 570 571 if (mips_avail_end > ISA_DMA_BOUNCE_THRESHOLD) 572 high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD); 573 else 574 high = trunc_page(mips_avail_end); 575 576 return _bus_dmamem_alloc_range(t, size, alignment, boundary, 577 segs, nsegs, rsegs, flags, 0, high); 578} 579 580/********************************************************************** 581 * ISA DMA utility functions 582 **********************************************************************/ 583 584static int 585isadma_bounce_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, 586 bus_size_t size, int flags) 587{ 588 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 589 int error = 0; 590 591 cookie->id_bouncebuflen = round_page(size); 592 error = isadma_bounce_dmamem_alloc(t, cookie->id_bouncebuflen, 593 PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs, 594 map->_dm_segcnt, &cookie->id_nbouncesegs, flags); 595 if (error) 596 goto out; 597 error = _bus_dmamem_map(t, cookie->id_bouncesegs, 598 cookie->id_nbouncesegs, cookie->id_bouncebuflen, 599 (void **)&cookie->id_bouncebuf, flags); 600 601 out: 602 if (error) { 603 _bus_dmamem_free(t, cookie->id_bouncesegs, 604 cookie->id_nbouncesegs); 605 cookie->id_bouncebuflen = 0; 606 cookie->id_nbouncesegs = 0; 607 } else 608 cookie->id_flags |= ID_HAS_BOUNCE; 609 610 return error; 611} 612 613static void 614isadma_bounce_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map) 615{ 616 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 617 618 _bus_dmamem_unmap(t, cookie->id_bouncebuf, 619 cookie->id_bouncebuflen); 620 _bus_dmamem_free(t, cookie->id_bouncesegs, 621 cookie->id_nbouncesegs); 622 cookie->id_bouncebuflen = 0; 623 cookie->id_nbouncesegs = 0; 624 cookie->id_flags &= ~ID_HAS_BOUNCE; 625} 626