1/*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: stable/11/sys/x86/isa/isa_dma.c 332304 2018-04-08 20:52:09Z emaste $"); 37 38/* 39 * code to manage AT bus 40 * 41 * 92/08/18 Frank P. MacLachlan (fpm@crash.cts.com): 42 * Fixed uninitialized variable problem and added code to deal 43 * with DMA page boundaries in isa_dmarangecheck(). Fixed word 44 * mode DMA count compution and reorganized DMA setup code in 45 * isa_dmastart() 46 */ 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/bus.h> 51#include <sys/kernel.h> 52#include <sys/malloc.h> 53#include <sys/lock.h> 54#include <sys/proc.h> 55#include <sys/mutex.h> 56#include <sys/module.h> 57#include <vm/vm.h> 58#include <vm/vm_param.h> 59#include <vm/pmap.h> 60#include <isa/isareg.h> 61#include <isa/isavar.h> 62#include <isa/isa_dmareg.h> 63 64#define ISARAM_END 0x1000000 65 66static int isa_dmarangecheck(caddr_t va, u_int length, int chan); 67 68static caddr_t dma_bouncebuf[8]; 69static u_int dma_bouncebufsize[8]; 70static u_int8_t dma_bounced = 0; 71static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */ 72static u_int8_t dma_inuse = 0; /* User for acquire/release */ 73static u_int8_t dma_auto_mode = 0; 74static struct mtx isa_dma_lock; 75MTX_SYSINIT(isa_dma_lock, &isa_dma_lock, "isa DMA lock", MTX_DEF); 76 77#define VALID_DMA_MASK (7) 78 79/* high byte of address is stored in this port for i-th dma channel */ 80static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a }; 81 82/* 83 * Setup a DMA channel's bounce buffer. 84 */ 85int 86isa_dma_init(int chan, u_int bouncebufsize, int flag) 87{ 88 void *buf; 89 int contig; 90 91#ifdef DIAGNOSTIC 92 if (chan & ~VALID_DMA_MASK) 93 panic("isa_dma_init: channel out of range"); 94#endif 95 96 97 /* Try malloc() first. It works better if it works. */ 98 buf = malloc(bouncebufsize, M_DEVBUF, flag); 99 if (buf != NULL) { 100 if (isa_dmarangecheck(buf, bouncebufsize, chan) != 0) { 101 free(buf, M_DEVBUF); 102 buf = NULL; 103 } 104 contig = 0; 105 } 106 107 if (buf == NULL) { 108 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful, 109 1ul, chan & 4 ? 0x20000ul : 0x10000ul); 110 contig = 1; 111 } 112 113 if (buf == NULL) 114 return (ENOMEM); 115 116 mtx_lock(&isa_dma_lock); 117 /* 118 * If a DMA channel is shared, both drivers have to call isa_dma_init 119 * since they don't know that the other driver will do it. 120 * Just return if we're already set up good. 121 * XXX: this only works if they agree on the bouncebuf size. This 122 * XXX: is typically the case since they are multiple instances of 123 * XXX: the same driver. 124 */ 125 if (dma_bouncebuf[chan] != NULL) { 126 if (contig) 127 contigfree(buf, bouncebufsize, M_DEVBUF); 128 else 129 free(buf, M_DEVBUF); 130 mtx_unlock(&isa_dma_lock); 131 return (0); 132 } 133 134 dma_bouncebufsize[chan] = bouncebufsize; 135 dma_bouncebuf[chan] = buf; 136 137 mtx_unlock(&isa_dma_lock); 138 139 return (0); 140} 141 142/* 143 * Register a DMA channel's usage. Usually called from a device driver 144 * in open() or during its initialization. 145 */ 146int 147isa_dma_acquire(int chan) 148{ 149#ifdef DIAGNOSTIC 150 if (chan & ~VALID_DMA_MASK) 151 panic("isa_dma_acquire: channel out of range"); 152#endif 153 154 mtx_lock(&isa_dma_lock); 155 if (dma_inuse & (1 << chan)) { 156 printf("isa_dma_acquire: channel %d already in use\n", chan); 157 mtx_unlock(&isa_dma_lock); 158 return (EBUSY); 159 } 160 dma_inuse |= (1 << chan); 161 dma_auto_mode &= ~(1 << chan); 162 mtx_unlock(&isa_dma_lock); 163 164 return (0); 165} 166 167/* 168 * Unregister a DMA channel's usage. Usually called from a device driver 169 * during close() or during its shutdown. 170 */ 171void 172isa_dma_release(int chan) 173{ 174#ifdef DIAGNOSTIC 175 if (chan & ~VALID_DMA_MASK) 176 panic("isa_dma_release: channel out of range"); 177 178 mtx_lock(&isa_dma_lock); 179 if ((dma_inuse & (1 << chan)) == 0) 180 printf("isa_dma_release: channel %d not in use\n", chan); 181#else 182 mtx_lock(&isa_dma_lock); 183#endif 184 185 if (dma_busy & (1 << chan)) { 186 dma_busy &= ~(1 << chan); 187 /* 188 * XXX We should also do "dma_bounced &= (1 << chan);" 189 * because we are acting on behalf of isa_dmadone() which 190 * was not called to end the last DMA operation. This does 191 * not matter now, but it may in the future. 192 */ 193 } 194 195 dma_inuse &= ~(1 << chan); 196 dma_auto_mode &= ~(1 << chan); 197 198 mtx_unlock(&isa_dma_lock); 199} 200 201/* 202 * isa_dmacascade(): program 8237 DMA controller channel to accept 203 * external dma control by a board. 204 */ 205void 206isa_dmacascade(int chan) 207{ 208#ifdef DIAGNOSTIC 209 if (chan & ~VALID_DMA_MASK) 210 panic("isa_dmacascade: channel out of range"); 211#endif 212 213 mtx_lock(&isa_dma_lock); 214 /* set dma channel mode, and set dma channel mode */ 215 if ((chan & 4) == 0) { 216 outb(DMA1_MODE, DMA37MD_CASCADE | chan); 217 outb(DMA1_SMSK, chan); 218 } else { 219 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3)); 220 outb(DMA2_SMSK, chan & 3); 221 } 222 mtx_unlock(&isa_dma_lock); 223} 224 225/* 226 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment 227 * problems by using a bounce buffer. 228 */ 229void 230isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan) 231{ 232 vm_paddr_t phys; 233 int waport; 234 caddr_t newaddr; 235 int dma_range_checked; 236 237 dma_range_checked = isa_dmarangecheck(addr, nbytes, chan); 238 239#ifdef DIAGNOSTIC 240 if (chan & ~VALID_DMA_MASK) 241 panic("isa_dmastart: channel out of range"); 242 243 if ((chan < 4 && nbytes > (1<<16)) 244 || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1))) 245 panic("isa_dmastart: impossible request"); 246 247 mtx_lock(&isa_dma_lock); 248 if ((dma_inuse & (1 << chan)) == 0) 249 printf("isa_dmastart: channel %d not acquired\n", chan); 250#else 251 mtx_lock(&isa_dma_lock); 252#endif 253 254#if 0 255 /* 256 * XXX This should be checked, but drivers like ad1848 only call 257 * isa_dmastart() once because they use Auto DMA mode. If we 258 * leave this in, drivers that do this will print this continuously. 259 */ 260 if (dma_busy & (1 << chan)) 261 printf("isa_dmastart: channel %d busy\n", chan); 262#endif 263 264 dma_busy |= (1 << chan); 265 266 if (dma_range_checked) { 267 if (dma_bouncebuf[chan] == NULL 268 || dma_bouncebufsize[chan] < nbytes) 269 panic("isa_dmastart: bad bounce buffer"); 270 dma_bounced |= (1 << chan); 271 newaddr = dma_bouncebuf[chan]; 272 273 /* copy bounce buffer on write */ 274 if (!(flags & ISADMA_READ)) 275 bcopy(addr, newaddr, nbytes); 276 addr = newaddr; 277 } 278 279 /* translate to physical */ 280 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr); 281 282 if (flags & ISADMA_RAW) { 283 dma_auto_mode |= (1 << chan); 284 } else { 285 dma_auto_mode &= ~(1 << chan); 286 } 287 288 if ((chan & 4) == 0) { 289 /* 290 * Program one of DMA channels 0..3. These are 291 * byte mode channels. 292 */ 293 /* set dma channel mode, and reset address ff */ 294 295 /* If ISADMA_RAW flag is set, then use autoinitialise mode */ 296 if (flags & ISADMA_RAW) { 297 if (flags & ISADMA_READ) 298 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan); 299 else 300 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan); 301 } 302 else 303 if (flags & ISADMA_READ) 304 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan); 305 else 306 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan); 307 outb(DMA1_FFC, 0); 308 309 /* send start address */ 310 waport = DMA1_CHN(chan); 311 outb(waport, phys); 312 outb(waport, phys>>8); 313 outb(dmapageport[chan], phys>>16); 314 315 /* send count */ 316 outb(waport + 1, --nbytes); 317 outb(waport + 1, nbytes>>8); 318 319 /* unmask channel */ 320 outb(DMA1_SMSK, chan); 321 } else { 322 /* 323 * Program one of DMA channels 4..7. These are 324 * word mode channels. 325 */ 326 /* set dma channel mode, and reset address ff */ 327 328 /* If ISADMA_RAW flag is set, then use autoinitialise mode */ 329 if (flags & ISADMA_RAW) { 330 if (flags & ISADMA_READ) 331 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3)); 332 else 333 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3)); 334 } 335 else 336 if (flags & ISADMA_READ) 337 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3)); 338 else 339 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3)); 340 outb(DMA2_FFC, 0); 341 342 /* send start address */ 343 waport = DMA2_CHN(chan - 4); 344 outb(waport, phys>>1); 345 outb(waport, phys>>9); 346 outb(dmapageport[chan], phys>>16); 347 348 /* send count */ 349 nbytes >>= 1; 350 outb(waport + 2, --nbytes); 351 outb(waport + 2, nbytes>>8); 352 353 /* unmask channel */ 354 outb(DMA2_SMSK, chan & 3); 355 } 356 mtx_unlock(&isa_dma_lock); 357} 358 359void 360isa_dmadone(int flags, caddr_t addr, int nbytes, int chan) 361{ 362#ifdef DIAGNOSTIC 363 if (chan & ~VALID_DMA_MASK) 364 panic("isa_dmadone: channel out of range"); 365 366 if ((dma_inuse & (1 << chan)) == 0) 367 printf("isa_dmadone: channel %d not acquired\n", chan); 368#endif 369 370 mtx_lock(&isa_dma_lock); 371 if (((dma_busy & (1 << chan)) == 0) && 372 (dma_auto_mode & (1 << chan)) == 0 ) 373 printf("isa_dmadone: channel %d not busy\n", chan); 374 375 if ((dma_auto_mode & (1 << chan)) == 0) 376 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4); 377 378 if (dma_bounced & (1 << chan)) { 379 /* copy bounce buffer on read */ 380 if (flags & ISADMA_READ) 381 bcopy(dma_bouncebuf[chan], addr, nbytes); 382 383 dma_bounced &= ~(1 << chan); 384 } 385 dma_busy &= ~(1 << chan); 386 mtx_unlock(&isa_dma_lock); 387} 388 389/* 390 * Check for problems with the address range of a DMA transfer 391 * (non-contiguous physical pages, outside of bus address space, 392 * crossing DMA page boundaries). 393 * Return true if special handling needed. 394 */ 395 396static int 397isa_dmarangecheck(caddr_t va, u_int length, int chan) 398{ 399 vm_paddr_t phys, priorpage = 0; 400 vm_offset_t endva; 401 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1); 402 403 endva = (vm_offset_t)round_page((vm_offset_t)va + length); 404 for (; va < (caddr_t) endva ; va += PAGE_SIZE) { 405 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va)); 406 if (phys == 0) 407 panic("isa_dmacheck: no physical page present"); 408 if (phys >= ISARAM_END) 409 return (1); 410 if (priorpage) { 411 if (priorpage + PAGE_SIZE != phys) 412 return (1); 413 /* check if crossing a DMA page boundary */ 414 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk) 415 return (1); 416 } 417 priorpage = phys; 418 } 419 return (0); 420} 421 422/* 423 * Query the progress of a transfer on a DMA channel. 424 * 425 * To avoid having to interrupt a transfer in progress, we sample 426 * each of the high and low databytes twice, and apply the following 427 * logic to determine the correct count. 428 * 429 * Reads are performed with interrupts disabled, thus it is to be 430 * expected that the time between reads is very small. At most 431 * one rollover in the low count byte can be expected within the 432 * four reads that are performed. 433 * 434 * There are three gaps in which a rollover can occur : 435 * 436 * - read low1 437 * gap1 438 * - read high1 439 * gap2 440 * - read low2 441 * gap3 442 * - read high2 443 * 444 * If a rollover occurs in gap1 or gap2, the low2 value will be 445 * greater than the low1 value. In this case, low2 and high2 are a 446 * corresponding pair. 447 * 448 * In any other case, low1 and high1 can be considered to be correct. 449 * 450 * The function returns the number of bytes remaining in the transfer, 451 * or -1 if the channel requested is not active. 452 * 453 */ 454static int 455isa_dmastatus_locked(int chan) 456{ 457 u_long cnt = 0; 458 int ffport, waport; 459 u_long low1, high1, low2, high2; 460 461 mtx_assert(&isa_dma_lock, MA_OWNED); 462 463 /* channel active? */ 464 if ((dma_inuse & (1 << chan)) == 0) { 465 printf("isa_dmastatus: channel %d not active\n", chan); 466 return(-1); 467 } 468 /* channel busy? */ 469 470 if (((dma_busy & (1 << chan)) == 0) && 471 (dma_auto_mode & (1 << chan)) == 0 ) { 472 printf("chan %d not busy\n", chan); 473 return -2 ; 474 } 475 if (chan < 4) { /* low DMA controller */ 476 ffport = DMA1_FFC; 477 waport = DMA1_CHN(chan) + 1; 478 } else { /* high DMA controller */ 479 ffport = DMA2_FFC; 480 waport = DMA2_CHN(chan - 4) + 2; 481 } 482 483 disable_intr(); /* no interrupts Mr Jones! */ 484 outb(ffport, 0); /* clear register LSB flipflop */ 485 low1 = inb(waport); 486 high1 = inb(waport); 487 outb(ffport, 0); /* clear again */ 488 low2 = inb(waport); 489 high2 = inb(waport); 490 enable_intr(); /* enable interrupts again */ 491 492 /* 493 * Now decide if a wrap has tried to skew our results. 494 * Note that after TC, the count will read 0xffff, while we want 495 * to return zero, so we add and then mask to compensate. 496 */ 497 if (low1 >= low2) { 498 cnt = (low1 + (high1 << 8) + 1) & 0xffff; 499 } else { 500 cnt = (low2 + (high2 << 8) + 1) & 0xffff; 501 } 502 503 if (chan >= 4) /* high channels move words */ 504 cnt *= 2; 505 return(cnt); 506} 507 508int 509isa_dmastatus(int chan) 510{ 511 int status; 512 513 mtx_lock(&isa_dma_lock); 514 status = isa_dmastatus_locked(chan); 515 mtx_unlock(&isa_dma_lock); 516 517 return (status); 518} 519 520/* 521 * Reached terminal count yet ? 522 */ 523int 524isa_dmatc(int chan) 525{ 526 527 if (chan < 4) 528 return(inb(DMA1_STATUS) & (1 << chan)); 529 else 530 return(inb(DMA2_STATUS) & (1 << (chan & 3))); 531} 532 533/* 534 * Stop a DMA transfer currently in progress. 535 */ 536int 537isa_dmastop(int chan) 538{ 539 int status; 540 541 mtx_lock(&isa_dma_lock); 542 if ((dma_inuse & (1 << chan)) == 0) 543 printf("isa_dmastop: channel %d not acquired\n", chan); 544 545 if (((dma_busy & (1 << chan)) == 0) && 546 ((dma_auto_mode & (1 << chan)) == 0)) { 547 printf("chan %d not busy\n", chan); 548 mtx_unlock(&isa_dma_lock); 549 return -2 ; 550 } 551 552 if ((chan & 4) == 0) { 553 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */); 554 } else { 555 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */); 556 } 557 558 status = isa_dmastatus_locked(chan); 559 560 mtx_unlock(&isa_dma_lock); 561 562 return (status); 563} 564 565/* 566 * Attach to the ISA PnP descriptor for the AT DMA controller 567 */ 568static struct isa_pnp_id atdma_ids[] = { 569 { 0x0002d041 /* PNP0200 */, "AT DMA controller" }, 570 { 0 } 571}; 572 573static int 574atdma_probe(device_t dev) 575{ 576 int result; 577 578 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0) 579 device_quiet(dev); 580 return(result); 581} 582 583static int 584atdma_attach(device_t dev) 585{ 586 return(0); 587} 588 589static device_method_t atdma_methods[] = { 590 /* Device interface */ 591 DEVMETHOD(device_probe, atdma_probe), 592 DEVMETHOD(device_attach, atdma_attach), 593 DEVMETHOD(device_detach, bus_generic_detach), 594 DEVMETHOD(device_shutdown, bus_generic_shutdown), 595 DEVMETHOD(device_suspend, bus_generic_suspend), 596 DEVMETHOD(device_resume, bus_generic_resume), 597 { 0, 0 } 598}; 599 600static driver_t atdma_driver = { 601 "atdma", 602 atdma_methods, 603 1, /* no softc */ 604}; 605 606static devclass_t atdma_devclass; 607 608DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0); 609DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0); 610