1 /* $NetBSD: isadma_bounce.c,v 1.5 2003/05/05 12:55:42 fvdl Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997, 1998, 2000 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: isadma_bounce.c,v 1.5 2003/05/05 12:55:42 fvdl Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/syslog.h> 47 #include <sys/device.h> 48 #include <sys/malloc.h> 49 #include <sys/proc.h> 50 #include <sys/mbuf.h> 51 52 #define _ALPHA_BUS_DMA_PRIVATE 53 #include <machine/bus.h> 54 55 #include <dev/isa/isareg.h> 56 #include <dev/isa/isavar.h> 57 58 #include <uvm/uvm_extern.h> 59 60 extern paddr_t avail_end; 61 62 /* 63 * Cookie used by bouncing ISA DMA. A pointer to one of these is stashed 64 * in the DMA map. 65 */ 66 struct isadma_bounce_cookie { 67 int id_flags; /* flags; see below */ 68 69 /* 70 * Information about the original buffer used during 71 * DMA map syncs. Note that origbuflen is only used 72 * for ID_BUFTYPE_LINEAR. 73 */ 74 void *id_origbuf; /* pointer to orig buffer if 75 bouncing */ 76 bus_size_t id_origbuflen; /* ...and size */ 77 int id_buftype; /* type of buffer */ 78 79 void *id_bouncebuf; /* pointer to the bounce buffer */ 80 bus_size_t id_bouncebuflen; /* ...and size */ 81 int id_nbouncesegs; /* number of valid bounce segs */ 82 bus_dma_segment_t id_bouncesegs[1]; /* array of bounce buffer 83 physical memory segments */ 84 }; 85 86 /* id_flags */ 87 #define ID_MIGHT_NEED_BOUNCE 0x01 /* map could need bounce buffers */ 88 #define ID_HAS_BOUNCE 0x02 /* map currently has bounce buffers */ 89 #define ID_IS_BOUNCING 0x04 /* map is bouncing current xfer */ 90 91 /* id_buftype */ 92 #define ID_BUFTYPE_INVALID 0 93 #define ID_BUFTYPE_LINEAR 1 94 #define ID_BUFTYPE_MBUF 2 95 #define ID_BUFTYPE_UIO 3 96 #define ID_BUFTYPE_RAW 4 97 98 int isadma_bounce_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t, 99 bus_size_t, int)); 100 void isadma_bounce_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t)); 101 102 /* 103 * Create an ISA DMA map. 104 */ 105 int 106 isadma_bounce_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 107 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp) 108 { 109 struct isadma_bounce_cookie *cookie; 110 bus_dmamap_t map; 111 int error, cookieflags; 112 void *cookiestore; 113 size_t cookiesize; 114 115 /* Call common function to create the basic map. */ 116 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, 117 flags, dmamp); 118 if (error) 119 return (error); 120 121 map = *dmamp; 122 map->_dm_cookie = NULL; 123 124 cookiesize = sizeof(*cookie); 125 126 /* 127 * ISA only has 24-bits of address space. This means 128 * we can't DMA to pages over 16M. In order to DMA to 129 * arbitrary buffers, we use "bounce buffers" - pages 130 * in memory below the 16M boundary. On DMA reads, 131 * DMA happens to the bounce buffers, and is copied into 132 * the caller's buffer. On writes, data is copied into 133 * but bounce buffer, and the DMA happens from those 134 * pages. To software using the DMA mapping interface, 135 * this looks simply like a data cache. 136 * 137 * If we have more than 16M of RAM in the system, we may 138 * need bounce buffers. We check and remember that here. 139 * 140 * ...or, there is an opposite case. The most segments 141 * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If 142 * the caller can't handle that many segments (e.g. the 143 * ISA DMA controller), we may have to bounce it as well. 144 */ 145 cookieflags = 0; 146 if (avail_end > (t->_wbase + t->_wsize) || 147 ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) { 148 cookieflags |= ID_MIGHT_NEED_BOUNCE; 149 cookiesize += (sizeof(bus_dma_segment_t) * 150 (map->_dm_segcnt - 1)); 151 } 152 153 /* 154 * Allocate our cookie. 155 */ 156 if ((cookiestore = malloc(cookiesize, M_DMAMAP, 157 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) { 158 error = ENOMEM; 159 goto out; 160 } 161 memset(cookiestore, 0, cookiesize); 162 cookie = (struct isadma_bounce_cookie *)cookiestore; 163 cookie->id_flags = cookieflags; 164 map->_dm_cookie = cookie; 165 166 if (cookieflags & ID_MIGHT_NEED_BOUNCE) { 167 /* 168 * Allocate the bounce pages now if the caller 169 * wishes us to do so. 170 */ 171 if ((flags & BUS_DMA_ALLOCNOW) == 0) 172 goto out; 173 174 error = isadma_bounce_alloc_bouncebuf(t, map, size, flags); 175 } 176 177 out: 178 if (error) { 179 if (map->_dm_cookie != NULL) 180 free(map->_dm_cookie, M_DMAMAP); 181 _bus_dmamap_destroy(t, map); 182 } 183 return (error); 184 } 185 186 /* 187 * Destroy an ISA DMA map. 188 */ 189 void 190 isadma_bounce_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 191 { 192 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 193 194 /* 195 * Free any bounce pages this map might hold. 196 */ 197 if (cookie->id_flags & ID_HAS_BOUNCE) 198 isadma_bounce_free_bouncebuf(t, map); 199 200 free(cookie, M_DMAMAP); 201 _bus_dmamap_destroy(t, map); 202 } 203 204 /* 205 * Load an ISA DMA map with a linear buffer. 206 */ 207 int 208 isadma_bounce_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 209 size_t buflen, struct proc *p, int flags) 210 { 211 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 212 int error; 213 214 /* 215 * Make sure that on error condition we return "no valid mappings." 216 */ 217 map->dm_mapsize = 0; 218 map->dm_nsegs = 0; 219 220 /* 221 * Try to load the map the normal way. If this errors out, 222 * and we can bounce, we will. 223 */ 224 error = _bus_dmamap_load_direct(t, map, buf, buflen, p, flags); 225 if (error == 0 || 226 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0)) 227 return (error); 228 229 /* 230 * First attempt failed; bounce it. 231 */ 232 233 /* 234 * Allocate bounce pages, if necessary. 235 */ 236 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) { 237 error = isadma_bounce_alloc_bouncebuf(t, map, buflen, flags); 238 if (error) 239 return (error); 240 } 241 242 /* 243 * Cache a pointer to the caller's buffer and load the DMA map 244 * with the bounce buffer. 245 */ 246 cookie->id_origbuf = buf; 247 cookie->id_origbuflen = buflen; 248 cookie->id_buftype = ID_BUFTYPE_LINEAR; 249 error = _bus_dmamap_load_direct(t, map, cookie->id_bouncebuf, buflen, 250 p, flags); 251 if (error) { 252 /* 253 * Free the bounce pages, unless our resources 254 * are reserved for our exclusive use. 255 */ 256 if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 257 isadma_bounce_free_bouncebuf(t, map); 258 return (error); 259 } 260 261 /* ...so isadma_bounce_dmamap_sync() knows we're bouncing */ 262 cookie->id_flags |= ID_IS_BOUNCING; 263 map->_dm_window = t; 264 return (0); 265 } 266 267 /* 268 * Like isadma_bounce_dmamap_load(), but for mbufs. 269 */ 270 int 271 isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, 272 struct mbuf *m0, int flags) 273 { 274 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 275 int error; 276 277 /* 278 * Make sure on error condition we return "no valid mappings." 279 */ 280 map->dm_mapsize = 0; 281 map->dm_nsegs = 0; 282 283 #ifdef DIAGNOSTIC 284 if ((m0->m_flags & M_PKTHDR) == 0) 285 panic("isadma_bounce_dmamap_load_mbuf: no packet header"); 286 #endif 287 288 if (m0->m_pkthdr.len > map->_dm_size) 289 return (EINVAL); 290 291 /* 292 * Try to load the map the normal way. If this errors out, 293 * and we can bounce, we will. 294 */ 295 error = _bus_dmamap_load_mbuf_direct(t, map, m0, flags); 296 if (error == 0 || 297 (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0)) 298 return (error); 299 300 /* 301 * First attempt failed; bounce it. 302 */ 303 304 /* 305 * Allocate bounce pages, if necessary. 306 */ 307 if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) { 308 error = isadma_bounce_alloc_bouncebuf(t, map, m0->m_pkthdr.len, 309 flags); 310 if (error) 311 return (error); 312 } 313 314 /* 315 * Cache a pointer to the caller's buffer and load the DMA map 316 * with the bounce buffer. 317 */ 318 cookie->id_origbuf = m0; 319 cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */ 320 cookie->id_buftype = ID_BUFTYPE_MBUF; 321 error = _bus_dmamap_load_direct(t, map, cookie->id_bouncebuf, 322 m0->m_pkthdr.len, NULL, flags); 323 if (error) { 324 /* 325 * Free the bounce pages, unless our resources 326 * are reserved for our exclusive use. 327 */ 328 if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 329 isadma_bounce_free_bouncebuf(t, map); 330 return (error); 331 } 332 333 /* ...so isadma_bounce_dmamap_sync() knows we're bouncing */ 334 cookie->id_flags |= ID_IS_BOUNCING; 335 map->_dm_window = t; 336 return (0); 337 } 338 339 /* 340 * Like isadma_bounce_dmamap_load(), but for uios. 341 */ 342 int 343 isadma_bounce_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, 344 struct uio *uio, int flags) 345 { 346 347 panic("isadma_bounce_dmamap_load_uio: not implemented"); 348 } 349 350 /* 351 * Like isadma_bounce_dmamap_load(), but for raw memory allocated with 352 * bus_dmamem_alloc(). 353 */ 354 int 355 isadma_bounce_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, 356 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 357 { 358 359 panic("isadma_bounce_dmamap_load_raw: not implemented"); 360 } 361 362 /* 363 * Unload an ISA DMA map. 364 */ 365 void 366 isadma_bounce_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 367 { 368 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 369 370 /* 371 * If we have bounce pages, free them, unless they're 372 * reserved for our exclusive use. 373 */ 374 if ((cookie->id_flags & ID_HAS_BOUNCE) && 375 (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) 376 isadma_bounce_free_bouncebuf(t, map); 377 378 cookie->id_flags &= ~ID_IS_BOUNCING; 379 cookie->id_buftype = ID_BUFTYPE_INVALID; 380 381 /* 382 * Do the generic bits of the unload. 383 */ 384 _bus_dmamap_unload(t, map); 385 } 386 387 /* 388 * Synchronize an ISA DMA map. 389 */ 390 void 391 isadma_bounce_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset, 392 bus_size_t len, int ops) 393 { 394 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 395 396 /* 397 * Mixing PRE and POST operations is not allowed. 398 */ 399 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 && 400 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0) 401 panic("isadma_bounce_dmamap_sync: mix PRE and POST"); 402 403 #ifdef DIAGNOSTIC 404 if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) { 405 if (offset >= map->dm_mapsize) 406 panic("isadma_bounce_dmamap_sync: bad offset"); 407 if (len == 0 || (offset + len) > map->dm_mapsize) 408 panic("isadma_bounce_dmamap_sync: bad length"); 409 } 410 #endif 411 412 /* 413 * If we're not bouncing, just drain the write buffer 414 * and return. 415 */ 416 if ((cookie->id_flags & ID_IS_BOUNCING) == 0) { 417 alpha_mb(); 418 return; 419 } 420 421 switch (cookie->id_buftype) { 422 case ID_BUFTYPE_LINEAR: 423 /* 424 * Nothing to do for pre-read. 425 */ 426 427 if (ops & BUS_DMASYNC_PREWRITE) { 428 /* 429 * Copy the caller's buffer to the bounce buffer. 430 */ 431 memcpy((char *)cookie->id_bouncebuf + offset, 432 (char *)cookie->id_origbuf + offset, len); 433 } 434 435 if (ops & BUS_DMASYNC_POSTREAD) { 436 /* 437 * Copy the bounce buffer to the caller's buffer. 438 */ 439 memcpy((char *)cookie->id_origbuf + offset, 440 (char *)cookie->id_bouncebuf + offset, len); 441 } 442 443 /* 444 * Nothing to do for post-write. 445 */ 446 break; 447 448 case ID_BUFTYPE_MBUF: 449 { 450 struct mbuf *m, *m0 = cookie->id_origbuf; 451 bus_size_t minlen, moff; 452 453 /* 454 * Nothing to do for pre-read. 455 */ 456 457 if (ops & BUS_DMASYNC_PREWRITE) { 458 /* 459 * Copy the caller's buffer to the bounce buffer. 460 */ 461 m_copydata(m0, offset, len, 462 (char *)cookie->id_bouncebuf + offset); 463 } 464 465 if (ops & BUS_DMASYNC_POSTREAD) { 466 /* 467 * Copy the bounce buffer to the caller's buffer. 468 */ 469 for (moff = offset, m = m0; m != NULL && len != 0; 470 m = m->m_next) { 471 /* Find the beginning mbuf. */ 472 if (moff >= m->m_len) { 473 moff -= m->m_len; 474 continue; 475 } 476 477 /* 478 * Now at the first mbuf to sync; nail 479 * each one until we have exhausted the 480 * length. 481 */ 482 minlen = len < m->m_len - moff ? 483 len : m->m_len - moff; 484 485 memcpy(mtod(m, caddr_t) + moff, 486 (char *)cookie->id_bouncebuf + offset, 487 minlen); 488 489 moff = 0; 490 len -= minlen; 491 offset += minlen; 492 } 493 } 494 495 /* 496 * Nothing to do for post-write. 497 */ 498 break; 499 } 500 501 case ID_BUFTYPE_UIO: 502 panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_UIO"); 503 break; 504 505 case ID_BUFTYPE_RAW: 506 panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_RAW"); 507 break; 508 509 case ID_BUFTYPE_INVALID: 510 panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_INVALID"); 511 break; 512 513 default: 514 printf("unknown buffer type %d\n", cookie->id_buftype); 515 panic("isadma_bounce_dmamap_sync"); 516 } 517 518 /* Drain the write buffer. */ 519 alpha_mb(); 520 } 521 522 /* 523 * Allocate memory safe for ISA DMA. 524 */ 525 int 526 isadma_bounce_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, 527 bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs, 528 int nsegs, int *rsegs, int flags) 529 { 530 paddr_t high; 531 532 if (avail_end > ISA_DMA_BOUNCE_THRESHOLD) 533 high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD); 534 else 535 high = trunc_page(avail_end); 536 537 return (_bus_dmamem_alloc_range(t, size, alignment, boundary, 538 segs, nsegs, rsegs, flags, 0, high)); 539 } 540 541 /********************************************************************** 542 * ISA DMA utility functions 543 **********************************************************************/ 544 545 int 546 isadma_bounce_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, 547 bus_size_t size, int flags) 548 { 549 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 550 int error = 0; 551 552 cookie->id_bouncebuflen = round_page(size); 553 error = isadma_bounce_dmamem_alloc(t, cookie->id_bouncebuflen, 554 PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs, 555 map->_dm_segcnt, &cookie->id_nbouncesegs, flags); 556 if (error) 557 goto out; 558 error = _bus_dmamem_map(t, cookie->id_bouncesegs, 559 cookie->id_nbouncesegs, cookie->id_bouncebuflen, 560 (caddr_t *)&cookie->id_bouncebuf, flags); 561 562 out: 563 if (error) { 564 _bus_dmamem_free(t, cookie->id_bouncesegs, 565 cookie->id_nbouncesegs); 566 cookie->id_bouncebuflen = 0; 567 cookie->id_nbouncesegs = 0; 568 } else 569 cookie->id_flags |= ID_HAS_BOUNCE; 570 571 return (error); 572 } 573 574 void 575 isadma_bounce_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map) 576 { 577 struct isadma_bounce_cookie *cookie = map->_dm_cookie; 578 579 _bus_dmamem_unmap(t, cookie->id_bouncebuf, 580 cookie->id_bouncebuflen); 581 _bus_dmamem_free(t, cookie->id_bouncesegs, 582 cookie->id_nbouncesegs); 583 cookie->id_bouncebuflen = 0; 584 cookie->id_nbouncesegs = 0; 585 cookie->id_flags &= ~ID_HAS_BOUNCE; 586 } 587