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