isa.c revision 15147
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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91 37 * $Id: isa.c,v 1.66 1996/04/07 17:32:13 bde Exp $ 38 */ 39 40/* 41 * code to manage AT bus 42 * 43 * 92/08/18 Frank P. MacLachlan (fpm@crash.cts.com): 44 * Fixed uninitialized variable problem and added code to deal 45 * with DMA page boundaries in isa_dmarangecheck(). Fixed word 46 * mode DMA count compution and reorganized DMA setup code in 47 * isa_dmastart() 48 */ 49 50#include <sys/param.h> 51#include <sys/systm.h> 52#include <sys/sysctl.h> 53#include <sys/buf.h> 54#include <sys/syslog.h> 55#include <sys/malloc.h> 56#include <machine/segments.h> 57#include <vm/vm.h> 58#include <vm/vm_param.h> 59#include <vm/pmap.h> 60#include <i386/isa/isa_device.h> 61#include <i386/isa/isa.h> 62#include <i386/isa/icu.h> 63#include <i386/isa/ic/i8237.h> 64#include <sys/devconf.h> 65#include "vector.h" 66 67/* 68** Register definitions for DMA controller 1 (channels 0..3): 69*/ 70#define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */ 71#define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */ 72#define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */ 73#define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */ 74 75/* 76** Register definitions for DMA controller 2 (channels 4..7): 77*/ 78#define DMA2_CHN(c) (IO_DMA2 + 2*(2*(c))) /* addr reg for channel c */ 79#define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */ 80#define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */ 81#define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */ 82 83u_long *intr_countp[ICU_LEN]; 84inthand2_t *intr_handler[ICU_LEN]; 85u_int intr_mask[ICU_LEN]; 86u_int* intr_mptr[ICU_LEN]; 87int intr_unit[ICU_LEN]; 88 89extern struct kern_devconf kdc_cpu0; 90 91struct kern_devconf kdc_isa0 = { 92 0, 0, 0, /* filled in by dev_attach */ 93 "isa", 0, { MDDT_BUS, 0 }, 94 0, 0, 0, BUS_EXTERNALLEN, 95 &kdc_cpu0, /* parent is the CPU */ 96 0, /* no parentdata */ 97 DC_BUSY, /* busses are always busy */ 98 "ISA or EISA bus", 99 DC_CLS_BUS /* class */ 100}; 101 102static inthand_t *fastintr[ICU_LEN] = { 103 &IDTVEC(fastintr0), &IDTVEC(fastintr1), 104 &IDTVEC(fastintr2), &IDTVEC(fastintr3), 105 &IDTVEC(fastintr4), &IDTVEC(fastintr5), 106 &IDTVEC(fastintr6), &IDTVEC(fastintr7), 107 &IDTVEC(fastintr8), &IDTVEC(fastintr9), 108 &IDTVEC(fastintr10), &IDTVEC(fastintr11), 109 &IDTVEC(fastintr12), &IDTVEC(fastintr13), 110 &IDTVEC(fastintr14), &IDTVEC(fastintr15) 111}; 112 113static inthand_t *slowintr[ICU_LEN] = { 114 &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3), 115 &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7), 116 &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11), 117 &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) 118}; 119 120static void config_isadev __P((struct isa_device *isdp, u_int *mp)); 121static void config_isadev_c __P((struct isa_device *isdp, u_int *mp, 122 int reconfig)); 123static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp, 124 int item, char const *whatnot, char const *reason, 125 char const *format)); 126static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp, 127 u_int checkbits)); 128static int isa_dmarangecheck __P((caddr_t va, u_int length, int chan)); 129static inthand2_t isa_strayintr; 130static void register_imask __P((struct isa_device *dvp, u_int mask)); 131 132/* 133 * print a conflict message 134 */ 135static void 136conflict(dvp, tmpdvp, item, whatnot, reason, format) 137 struct isa_device *dvp; 138 struct isa_device *tmpdvp; 139 int item; 140 char const *whatnot; 141 char const *reason; 142 char const *format; 143{ 144 printf("%s%d not %sed due to %s conflict with %s%d at ", 145 dvp->id_driver->name, dvp->id_unit, whatnot, reason, 146 tmpdvp->id_driver->name, tmpdvp->id_unit); 147 printf(format, item); 148 printf("\n"); 149} 150 151/* 152 * Check to see if things are already in use, like IRQ's, I/O addresses 153 * and Memory addresses. 154 */ 155static int 156haveseen(dvp, tmpdvp, checkbits) 157 struct isa_device *dvp; 158 struct isa_device *tmpdvp; 159 u_int checkbits; 160{ 161 /* 162 * Only check against devices that have already been found and are not 163 * unilaterally allowed to conflict anyway. 164 */ 165 if (tmpdvp->id_alive && !dvp->id_conflicts) { 166 char const *whatnot; 167 168 whatnot = checkbits & CC_ATTACH ? "attach" : "prob"; 169 /* 170 * Check for I/O address conflict. We can only check the 171 * starting address of the device against the range of the 172 * device that has already been probed since we do not 173 * know how many I/O addresses this device uses. 174 */ 175 if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) { 176 if ((dvp->id_iobase >= tmpdvp->id_iobase) && 177 (dvp->id_iobase <= 178 (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) { 179 conflict(dvp, tmpdvp, dvp->id_iobase, whatnot, 180 "I/O address", "0x%x"); 181 return 1; 182 } 183 } 184 /* 185 * Check for Memory address conflict. We can check for 186 * range overlap, but it will not catch all cases since the 187 * driver may adjust the msize paramater during probe, for 188 * now we just check that the starting address does not 189 * fall within any allocated region. 190 * XXX could add a second check after the probe for overlap, 191 * since at that time we would know the full range. 192 * XXX KERNBASE is a hack, we should have vaddr in the table! 193 */ 194 if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) { 195 if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) && 196 (KERNBASE + dvp->id_maddr <= 197 (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) { 198 conflict(dvp, tmpdvp, (int)dvp->id_maddr, 199 whatnot, "maddr", "0x%x"); 200 return 1; 201 } 202 } 203 /* 204 * Check for IRQ conflicts. 205 */ 206 if (checkbits & CC_IRQ && tmpdvp->id_irq) { 207 if (tmpdvp->id_irq == dvp->id_irq) { 208 conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1, 209 whatnot, "irq", "%d"); 210 return 1; 211 } 212 } 213 /* 214 * Check for DRQ conflicts. 215 */ 216 if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) { 217 if (tmpdvp->id_drq == dvp->id_drq) { 218 conflict(dvp, tmpdvp, dvp->id_drq, whatnot, 219 "drq", "%d"); 220 return 1; 221 } 222 } 223 } 224 return 0; 225} 226 227/* 228 * Search through all the isa_devtab_* tables looking for anything that 229 * conflicts with the current device. 230 */ 231int 232haveseen_isadev(dvp, checkbits) 233 struct isa_device *dvp; 234 u_int checkbits; 235{ 236 struct isa_device *tmpdvp; 237 int status = 0; 238 239 for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) { 240 status |= haveseen(dvp, tmpdvp, checkbits); 241 if (status) 242 return status; 243 } 244 for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) { 245 status |= haveseen(dvp, tmpdvp, checkbits); 246 if (status) 247 return status; 248 } 249 for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) { 250 status |= haveseen(dvp, tmpdvp, checkbits); 251 if (status) 252 return status; 253 } 254 for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) { 255 status |= haveseen(dvp, tmpdvp, checkbits); 256 if (status) 257 return status; 258 } 259 return(status); 260} 261 262/* 263 * Configure all ISA devices 264 */ 265void 266isa_configure() { 267 struct isa_device *dvp; 268 269 dev_attach(&kdc_isa0); 270 271 splhigh(); 272 printf("Probing for devices on the ISA bus:\n"); 273 /* First probe all the sensitive probes */ 274 for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 275 if (dvp->id_driver->sensitive_hw) 276 config_isadev(dvp, &tty_imask); 277 for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 278 if (dvp->id_driver->sensitive_hw) 279 config_isadev(dvp, &bio_imask); 280 for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 281 if (dvp->id_driver->sensitive_hw) 282 config_isadev(dvp, &net_imask); 283 for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 284 if (dvp->id_driver->sensitive_hw) 285 config_isadev(dvp, (u_int *)NULL); 286 287 /* Then all the bad ones */ 288 for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 289 if (!dvp->id_driver->sensitive_hw) 290 config_isadev(dvp, &tty_imask); 291 for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 292 if (!dvp->id_driver->sensitive_hw) 293 config_isadev(dvp, &bio_imask); 294 for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 295 if (!dvp->id_driver->sensitive_hw) 296 config_isadev(dvp, &net_imask); 297 for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 298 if (!dvp->id_driver->sensitive_hw) 299 config_isadev(dvp, (u_int *)NULL); 300 301 bio_imask |= SWI_CLOCK_MASK; 302 net_imask |= SWI_NET_MASK; 303 tty_imask |= SWI_TTY_MASK; 304 305/* 306 * XXX we should really add the tty device to net_imask when the line is 307 * switched to SLIPDISC, and then remove it when it is switched away from 308 * SLIPDISC. No need to block out ALL ttys during a splimp when only one 309 * of them is running slip. 310 * 311 * XXX actually, blocking all ttys during a splimp doesn't matter so much 312 * with sio because the serial interrupt layer doesn't use tty_imask. Only 313 * non-serial ttys suffer. It's more stupid that ALL 'net's are blocked 314 * during spltty. 315 */ 316#include "sl.h" 317#if NSL > 0 318 net_imask |= tty_imask; 319 tty_imask = net_imask; 320#endif 321 322 /* bio_imask |= tty_imask ; can some tty devices use buffers? */ 323 324 if (bootverbose) 325 printf("imasks: bio %x, tty %x, net %x\n", 326 bio_imask, tty_imask, net_imask); 327 328 /* 329 * Finish initializing intr_mask[]. Note that the partly 330 * constructed masks aren't actually used since we're at splhigh. 331 * For fully dynamic initialization, register_intr() and 332 * unregister_intr() will have to adjust the masks for _all_ 333 * interrupts and for tty_imask, etc. 334 */ 335 for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 336 register_imask(dvp, tty_imask); 337 for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 338 register_imask(dvp, bio_imask); 339 for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 340 register_imask(dvp, net_imask); 341 for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 342 register_imask(dvp, SWI_CLOCK_MASK); 343 spl0(); 344} 345 346/* 347 * Configure an ISA device. 348 */ 349 350 351static void 352config_isadev(isdp, mp) 353 struct isa_device *isdp; 354 u_int *mp; 355{ 356 config_isadev_c(isdp, mp, 0); 357} 358 359void 360reconfig_isadev(isdp, mp) 361 struct isa_device *isdp; 362 u_int *mp; 363{ 364 config_isadev_c(isdp, mp, 1); 365} 366 367static void 368config_isadev_c(isdp, mp, reconfig) 369 struct isa_device *isdp; 370 u_int *mp; 371 int reconfig; 372{ 373 u_int checkbits; 374 int id_alive; 375 int last_alive; 376 struct isa_driver *dp = isdp->id_driver; 377 378 if (!isdp->id_enabled) { 379 printf("%s%d: disabled, not probed.\n", 380 dp->name, isdp->id_unit); 381 return; 382 } 383 checkbits = CC_DRQ | CC_IOADDR | CC_MEMADDR; 384 if (!reconfig && haveseen_isadev(isdp, checkbits)) 385 return; 386 if (!reconfig && isdp->id_maddr) { 387 isdp->id_maddr -= 0xa0000; /* XXX should be a define */ 388 isdp->id_maddr += atdevbase; 389 } 390 if (reconfig) { 391 last_alive = isdp->id_alive; 392 isdp->id_reconfig = 1; 393 } 394 else { 395 last_alive = 0; 396 isdp->id_reconfig = 0; 397 } 398 id_alive = (*dp->probe)(isdp); 399 if (id_alive) { 400 /* 401 * Only print the I/O address range if id_alive != -1 402 * Right now this is a temporary fix just for the new 403 * NPX code so that if it finds a 486 that can use trap 404 * 16 it will not report I/O addresses. 405 * Rod Grimes 04/26/94 406 */ 407 if (!isdp->id_reconfig) { 408 printf("%s%d", dp->name, isdp->id_unit); 409 if (id_alive != -1) { 410 printf(" at 0x%x", isdp->id_iobase); 411 if (isdp->id_iobase + id_alive - 1 != 412 isdp->id_iobase) { 413 printf("-0x%x", 414 isdp->id_iobase + id_alive - 1); 415 } 416 } 417 if (isdp->id_irq) 418 printf(" irq %d", ffs(isdp->id_irq) - 1); 419 if (isdp->id_drq != -1) 420 printf(" drq %d", isdp->id_drq); 421 if (isdp->id_maddr) 422 printf(" maddr 0x%lx", kvtop(isdp->id_maddr)); 423 if (isdp->id_msize) 424 printf(" msize %d", isdp->id_msize); 425 if (isdp->id_flags) 426 printf(" flags 0x%x", isdp->id_flags); 427 if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) { 428 printf(" on motherboard"); 429 } else if (isdp->id_iobase >= 0x1000 && 430 !(isdp->id_iobase & 0x300)) { 431 printf (" on eisa slot %d", 432 isdp->id_iobase >> 12); 433 } else { 434 printf (" on isa"); 435 } 436 printf("\n"); 437 /* 438 * Check for conflicts again. The driver may have 439 * changed *dvp. We should weaken the early check 440 * since the driver may have been able to change 441 * *dvp to avoid conflicts if given a chance. We 442 * already skip the early check for IRQs and force 443 * a check for IRQs in the next group of checks. 444 */ 445 checkbits |= CC_IRQ; 446 if (haveseen_isadev(isdp, checkbits)) 447 return; 448 isdp->id_alive = id_alive; 449 } 450 (*dp->attach)(isdp); 451 if (isdp->id_irq) { 452 if (mp) 453 INTRMASK(*mp, isdp->id_irq); 454 register_intr(ffs(isdp->id_irq) - 1, isdp->id_id, 455 isdp->id_ri_flags, isdp->id_intr, 456 mp, isdp->id_unit); 457 INTREN(isdp->id_irq); 458 } 459 } else { 460 if (isdp->id_reconfig) { 461 (*dp->attach)(isdp); /* reconfiguration attach */ 462 } 463 if (!last_alive) { 464 if (!isdp->id_reconfig) { 465 printf("%s%d not found", 466 dp->name, isdp->id_unit); 467 if (isdp->id_iobase) { 468 printf(" at 0x%x", isdp->id_iobase); 469 } 470 printf("\n"); 471 } 472 } 473 else { 474 /* This code has not been tested.... */ 475 if (isdp->id_irq) { 476 INTRDIS(isdp->id_irq); 477 unregister_intr(ffs(isdp->id_irq) - 1, 478 isdp->id_intr); 479 if (mp) 480 INTRUNMASK(*mp, isdp->id_irq); 481 } 482 } 483 } 484} 485 486/* 487 * Provide ISA-specific device information to user programs using the 488 * hw.devconf interface. 489 */ 490int 491isa_externalize(struct isa_device *id, struct sysctl_req *req) 492{ 493 return (SYSCTL_OUT(req, id, sizeof *id)); 494} 495 496/* 497 * This is used to forcibly reconfigure an ISA device. It currently just 498 * returns an error 'cos you can't do that yet. It is here to demonstrate 499 * what the `internalize' routine is supposed to do. 500 */ 501int 502isa_internalize(struct isa_device *id, struct sysctl_req *req) 503{ 504 struct isa_device myid; 505 int rv; 506 507 rv = SYSCTL_IN(req, &myid, sizeof *id); 508 if(rv) 509 return rv; 510 511 rv = EOPNOTSUPP; 512 /* code would go here to validate the configuration request */ 513 /* code would go here to actually perform the reconfiguration */ 514 return rv; 515} 516 517int 518isa_generic_externalize(struct kern_devconf *kdc, struct sysctl_req *req) 519{ 520 return isa_externalize(kdc->kdc_isa, req); 521} 522 523/* 524 * Fill in default interrupt table (in case of spuruious interrupt 525 * during configuration of kernel, setup interrupt control unit 526 */ 527void 528isa_defaultirq() 529{ 530 int i; 531 532 /* icu vectors */ 533 for (i = 0; i < ICU_LEN; i++) 534 unregister_intr(i, (inthand2_t *)NULL); 535 536 /* initialize 8259's */ 537 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */ 538 outb(IO_ICU1+1, NRSVIDT); /* starting at this vector index */ 539 outb(IO_ICU1+1, 1<<2); /* slave on line 2 */ 540#ifdef AUTO_EOI_1 541 outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */ 542#else 543 outb(IO_ICU1+1, 1); /* 8086 mode */ 544#endif 545 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */ 546 outb(IO_ICU1, 0x0a); /* default to IRR on read */ 547 outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */ 548 549 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */ 550 outb(IO_ICU2+1, NRSVIDT+8); /* staring at this vector index */ 551 outb(IO_ICU2+1,2); /* my slave id is 2 */ 552#ifdef AUTO_EOI_2 553 outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */ 554#else 555 outb(IO_ICU2+1,1); /* 8086 mode */ 556#endif 557 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */ 558 outb(IO_ICU2, 0x0a); /* default to IRR on read */ 559} 560 561static caddr_t dma_bouncebuf[8]; 562static u_int dma_bouncebufsize[8]; 563static u_int8_t dma_bounced = 0; 564static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */ 565static u_int8_t dma_inuse = 0; /* User for acquire/release */ 566 567#define VALID_DMA_MASK (7) 568 569/* high byte of address is stored in this port for i-th dma channel */ 570static short dmapageport[8] = 571 { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a }; 572 573/* 574 * Setup a DMA channel's bounce buffer. 575 */ 576void 577isa_dmainit(chan, bouncebufsize) 578 int chan; 579 u_int bouncebufsize; 580{ 581 void *buf; 582 583#ifdef DIAGNOSTIC 584 if (chan & ~VALID_DMA_MASK) 585 panic("isa_dmainit: channel out of range"); 586 587 if (dma_bouncebuf[chan] != NULL) 588 panic("isa_dmainit: impossible request"); 589#endif 590 591 dma_bouncebufsize[chan] = bouncebufsize; 592 593 /* Try malloc() first. It works better if it works. */ 594 buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT); 595 if (buf != NULL) { 596 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) { 597 dma_bouncebuf[chan] = buf; 598 return; 599 } 600 free(buf, M_DEVBUF); 601 } 602 buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful, 603 1ul, chan & 4 ? 0x20000ul : 0x10000ul); 604 if (buf == NULL) 605 printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize); 606 else 607 dma_bouncebuf[chan] = buf; 608} 609 610/* 611 * Register a DMA channel's usage. Usually called from a device driver 612 * in open() or during it's initialization. 613 */ 614int 615isa_dma_acquire(chan) 616 int chan; 617{ 618#ifdef DIAGNOSTIC 619 if (chan & ~VALID_DMA_MASK) 620 panic("isa_dma_acquire: channel out of range"); 621#endif 622 623 if (dma_inuse & (1 << chan)) { 624 printf("isa_dma_acquire: channel %d already in use\n", chan); 625 return (EBUSY); 626 } 627 dma_inuse |= (1 << chan); 628 629 return (0); 630} 631 632/* 633 * Unregister a DMA channel's usage. Usually called from a device driver 634 * during close() or during it's shutdown. 635 */ 636void 637isa_dma_release(chan) 638 int chan; 639{ 640#ifdef DIAGNOSTIC 641 if (chan & ~VALID_DMA_MASK) 642 panic("isa_dma_release: channel out of range"); 643 644 if (dma_inuse & (1 << chan) == 0) 645 printf("isa_dma_release: channel %d not in use\n", chan); 646#endif 647 648 if (dma_busy & (1 << chan)) { 649 dma_busy &= ~(1 << chan); 650 /* 651 * XXX We should also do "dma_bounced &= (1 << chan);" 652 * because we are acting on behalf of isa_dmadone() which 653 * was not called to end the last DMA operation. This does 654 * not matter now, but it may in the future. 655 */ 656 } 657 658 dma_inuse &= ~(1 << chan); 659} 660 661/* 662 * isa_dmacascade(): program 8237 DMA controller channel to accept 663 * external dma control by a board. 664 */ 665void isa_dmacascade(chan) 666 int chan; 667{ 668#ifdef DIAGNOSTIC 669 if (chan & ~VALID_DMA_MASK) 670 panic("isa_dmacascade: channel out of range"); 671#endif 672 673 /* set dma channel mode, and set dma channel mode */ 674 if ((chan & 4) == 0) { 675 outb(DMA1_MODE, DMA37MD_CASCADE | chan); 676 outb(DMA1_SMSK, chan); 677 } else { 678 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3)); 679 outb(DMA2_SMSK, chan & 3); 680 } 681} 682 683/* 684 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment 685 * problems by using a bounce buffer. 686 */ 687void isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan) 688{ 689 vm_offset_t phys; 690 int waport; 691 caddr_t newaddr; 692 693#ifdef DIAGNOSTIC 694 if (chan & ~VALID_DMA_MASK) 695 panic("isa_dmastart: channel out of range"); 696 697 if ((chan < 4 && nbytes > (1<<16)) 698 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1))) 699 panic("isa_dmastart: impossible request"); 700 701 if (dma_inuse & (1 << chan) == 0) 702 printf("isa_dmastart: channel %d not acquired\n", chan); 703#endif 704 705 if (dma_busy & (1 << chan)) 706 printf("isa_dmastart: channel %d busy\n", chan); 707 708 dma_busy |= (1 << chan); 709 710 if (isa_dmarangecheck(addr, nbytes, chan)) { 711 if (dma_bouncebuf[chan] == NULL 712 || dma_bouncebufsize[chan] < nbytes) 713 panic("isa_dmastart: bad bounce buffer"); 714 dma_bounced |= (1 << chan); 715 newaddr = dma_bouncebuf[chan]; 716 717 /* copy bounce buffer on write */ 718 if (!(flags & B_READ)) 719 bcopy(addr, newaddr, nbytes); 720 addr = newaddr; 721 } 722 723 /* translate to physical */ 724 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr); 725 726 if ((chan & 4) == 0) { 727 /* 728 * Program one of DMA channels 0..3. These are 729 * byte mode channels. 730 */ 731 /* set dma channel mode, and reset address ff */ 732 733 /* If B_RAW flag is set, then use autoinitialise mode */ 734 if (flags & B_RAW) { 735 if (flags & B_READ) 736 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan); 737 else 738 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan); 739 } 740 else 741 if (flags & B_READ) 742 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan); 743 else 744 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan); 745 outb(DMA1_FFC, 0); 746 747 /* send start address */ 748 waport = DMA1_CHN(chan); 749 outb(waport, phys); 750 outb(waport, phys>>8); 751 outb(dmapageport[chan], phys>>16); 752 753 /* send count */ 754 outb(waport + 1, --nbytes); 755 outb(waport + 1, nbytes>>8); 756 757 /* unmask channel */ 758 outb(DMA1_SMSK, chan); 759 } else { 760 /* 761 * Program one of DMA channels 4..7. These are 762 * word mode channels. 763 */ 764 /* set dma channel mode, and reset address ff */ 765 766 /* If B_RAW flag is set, then use autoinitialise mode */ 767 if (flags & B_RAW) { 768 if (flags & B_READ) 769 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3)); 770 else 771 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3)); 772 } 773 else 774 if (flags & B_READ) 775 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3)); 776 else 777 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3)); 778 outb(DMA2_FFC, 0); 779 780 /* send start address */ 781 waport = DMA2_CHN(chan - 4); 782 outb(waport, phys>>1); 783 outb(waport, phys>>9); 784 outb(dmapageport[chan], phys>>16); 785 786 /* send count */ 787 nbytes >>= 1; 788 outb(waport + 2, --nbytes); 789 outb(waport + 2, nbytes>>8); 790 791 /* unmask channel */ 792 outb(DMA2_SMSK, chan & 3); 793 } 794} 795 796void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan) 797{ 798#ifdef DIAGNOSTIC 799 if (chan & ~VALID_DMA_MASK) 800 panic("isa_dmadone: channel out of range"); 801 802 if (dma_inuse & (1 << chan) == 0) 803 printf("isa_dmadone: channel %d not acquired\n", chan); 804#endif 805 806#if 0 807 /* 808 * XXX This should be checked, but drivers like ad1848 only call 809 * isa_dmastart() once because they use Auto DMA mode. If we 810 * leave this in, drivers that do this will print this continuously. 811 */ 812 if (dma_busy & (1 << chan) == 0) 813 printf("isa_dmadone: channel %d not busy\n", chan); 814#endif 815 816 if (dma_bounced & (1 << chan)) { 817 /* copy bounce buffer on read */ 818 if (flags & B_READ) 819 bcopy(dma_bouncebuf[chan], addr, nbytes); 820 821 dma_bounced &= ~(1 << chan); 822 } 823 dma_busy &= ~(1 << chan); 824} 825 826/* 827 * Check for problems with the address range of a DMA transfer 828 * (non-contiguous physical pages, outside of bus address space, 829 * crossing DMA page boundaries). 830 * Return true if special handling needed. 831 */ 832 833static int 834isa_dmarangecheck(caddr_t va, u_int length, int chan) { 835 vm_offset_t phys, priorpage = 0, endva; 836 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1); 837 838 endva = (vm_offset_t)round_page(va + length); 839 for (; va < (caddr_t) endva ; va += NBPG) { 840 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va)); 841#define ISARAM_END RAM_END 842 if (phys == 0) 843 panic("isa_dmacheck: no physical page present"); 844 if (phys >= ISARAM_END) 845 return (1); 846 if (priorpage) { 847 if (priorpage + NBPG != phys) 848 return (1); 849 /* check if crossing a DMA page boundary */ 850 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk) 851 return (1); 852 } 853 priorpage = phys; 854 } 855 return (0); 856} 857 858#define NMI_PARITY (1 << 7) 859#define NMI_IOCHAN (1 << 6) 860#define ENMI_WATCHDOG (1 << 7) 861#define ENMI_BUSTIMER (1 << 6) 862#define ENMI_IOSTATUS (1 << 5) 863 864/* 865 * Handle a NMI, possibly a machine check. 866 * return true to panic system, false to ignore. 867 */ 868int 869isa_nmi(cd) 870 int cd; 871{ 872 int isa_port = inb(0x61); 873 int eisa_port = inb(0x461); 874 if(isa_port & NMI_PARITY) { 875 panic("RAM parity error, likely hardware failure."); 876 } else if(isa_port & NMI_IOCHAN) { 877 panic("I/O channel check, likely hardware failure."); 878 } else if(eisa_port & ENMI_WATCHDOG) { 879 panic("EISA watchdog timer expired, likely hardware failure."); 880 } else if(eisa_port & ENMI_BUSTIMER) { 881 panic("EISA bus timeout, likely hardware failure."); 882 } else if(eisa_port & ENMI_IOSTATUS) { 883 panic("EISA I/O port status error."); 884 } else { 885 printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port); 886 return(0); 887 } 888} 889 890/* 891 * Caught a stray interrupt, notify 892 */ 893static void 894isa_strayintr(d) 895 int d; 896{ 897 898 /* DON'T BOTHER FOR NOW! */ 899 /* for some reason, we get bursts of intr #7, even if not enabled! */ 900 /* 901 * Well the reason you got bursts of intr #7 is because someone 902 * raised an interrupt line and dropped it before the 8259 could 903 * prioritize it. This is documented in the intel data book. This 904 * means you have BAD hardware! I have changed this so that only 905 * the first 5 get logged, then it quits logging them, and puts 906 * out a special message. rgrimes 3/25/1993 907 */ 908 /* 909 * XXX TODO print a different message for #7 if it is for a 910 * glitch. Glitches can be distinguished from real #7's by 911 * testing that the in-service bit is _not_ set. The test 912 * must be done before sending an EOI so it can't be done if 913 * we are using AUTO_EOI_1. 914 */ 915 if (intrcnt[NR_DEVICES + d] <= 5) 916 log(LOG_ERR, "stray irq %d\n", d); 917 if (intrcnt[NR_DEVICES + d] == 5) 918 log(LOG_CRIT, 919 "too many stray irq %d's; not logging any more\n", d); 920} 921 922/* 923 * Find the highest priority enabled display device. Since we can't 924 * distinguish display devices from ttys, depend on display devices 925 * being sensitive and before sensitive non-display devices (if any) 926 * in isa_devtab_tty. 927 * 928 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES. 929 */ 930struct isa_device * 931find_display() 932{ 933 struct isa_device *dvp; 934 935 for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++) 936 if (dvp->id_driver->sensitive_hw && dvp->id_enabled) 937 return (dvp); 938 return (NULL); 939} 940 941/* 942 * find an ISA device in a given isa_devtab_* table, given 943 * the table to search, the expected id_driver entry, and the unit number. 944 * 945 * this function is defined in isa_device.h, and this location is debatable; 946 * i put it there because it's useless w/o, and directly operates on 947 * the other stuff in that file. 948 * 949 */ 950 951struct isa_device *find_isadev(table, driverp, unit) 952 struct isa_device *table; 953 struct isa_driver *driverp; 954 int unit; 955{ 956 if (driverp == NULL) /* sanity check */ 957 return NULL; 958 959 while ((table->id_driver != driverp) || (table->id_unit != unit)) { 960 if (table->id_driver == 0) 961 return NULL; 962 963 table++; 964 } 965 966 return table; 967} 968 969/* 970 * Return nonzero if a (masked) irq is pending for a given device. 971 */ 972int 973isa_irq_pending(dvp) 974 struct isa_device *dvp; 975{ 976 unsigned id_irq; 977 978 id_irq = dvp->id_irq; 979 if (id_irq & 0xff) 980 return (inb(IO_ICU1) & id_irq); 981 return (inb(IO_ICU2) & (id_irq >> 8)); 982} 983 984int 985update_intr_masks(void) 986{ 987 int intr, n=0; 988 u_int mask,*maskptr; 989 990 for (intr=0; intr < ICU_LEN; intr ++) { 991 if (intr==2) continue; 992 maskptr = intr_mptr[intr]; 993 if (!maskptr) continue; 994 *maskptr |= 1 << intr; 995 mask = *maskptr; 996 if (mask != intr_mask[intr]) { 997#if 0 998 printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n", 999 intr, intr_mask[intr], mask, maskptr); 1000#endif 1001 intr_mask[intr]=mask; 1002 n++; 1003 } 1004 1005 } 1006 return (n); 1007} 1008 1009int 1010register_intr(intr, device_id, flags, handler, maskptr, unit) 1011 int intr; 1012 int device_id; 1013 u_int flags; 1014 inthand2_t *handler; 1015 u_int *maskptr; 1016 int unit; 1017{ 1018 char *cp; 1019 u_long ef; 1020 int id; 1021 u_int mask = (maskptr ? *maskptr : 0); 1022 1023 if ((u_int)intr >= ICU_LEN || intr == 2 1024 || (u_int)device_id >= NR_DEVICES) 1025 return (EINVAL); 1026 if (intr_handler[intr] != isa_strayintr) 1027 return (EBUSY); 1028 ef = read_eflags(); 1029 disable_intr(); 1030 intr_countp[intr] = &intrcnt[device_id]; 1031 intr_handler[intr] = handler; 1032 intr_mptr[intr] = maskptr; 1033 intr_mask[intr] = mask | (1 << intr); 1034 intr_unit[intr] = unit; 1035 setidt(ICU_OFFSET + intr, 1036 flags & RI_FAST ? fastintr[intr] : slowintr[intr], 1037 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1038 write_eflags(ef); 1039 for (cp = intrnames, id = 0; id <= device_id; id++) 1040 while (*cp++ != '\0') 1041 ; 1042 if (cp > eintrnames) 1043 return (0); 1044 if (intr < 10) { 1045 cp[-3] = intr + '0'; 1046 cp[-2] = ' '; 1047 } else { 1048 cp[-3] = '1'; 1049 cp[-2] = intr - 10 + '0'; 1050 } 1051 return (0); 1052} 1053 1054static void 1055register_imask(dvp, mask) 1056 struct isa_device *dvp; 1057 u_int mask; 1058{ 1059 if (dvp->id_alive && dvp->id_irq) { 1060 int intr; 1061 1062 intr = ffs(dvp->id_irq) - 1; 1063 intr_mask[intr] = mask | (1 <<intr); 1064 } 1065 (void) update_intr_masks(); 1066} 1067 1068int 1069unregister_intr(intr, handler) 1070 int intr; 1071 inthand2_t *handler; 1072{ 1073 u_long ef; 1074 1075 if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr]) 1076 return (EINVAL); 1077 ef = read_eflags(); 1078 disable_intr(); 1079 intr_countp[intr] = &intrcnt[NR_DEVICES + intr]; 1080 intr_handler[intr] = isa_strayintr; 1081 intr_mptr[intr] = NULL; 1082 intr_mask[intr] = HWI_MASK | SWI_MASK; 1083 intr_unit[intr] = intr; 1084 setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL, 1085 GSEL(GCODE_SEL, SEL_KPL)); 1086 write_eflags(ef); 1087 return (0); 1088} 1089