isa_machdep.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /* $OpenBSD: isa_machdep.c,v 1.80 2015/07/13 17:45:01 mikeb Exp $ */
  2. /* $NetBSD: isa_machdep.c,v 1.22 1997/06/12 23:57:32 thorpej Exp $ */
  3. /*-
  4. * Copyright (c) 1996, 1997 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. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*-
  33. * Copyright (c) 1993, 1994, 1996, 1997
  34. * Charles M. Hannum. All rights reserved.
  35. * Copyright (c) 1991 The Regents of the University of California.
  36. * All rights reserved.
  37. *
  38. * This code is derived from software contributed to Berkeley by
  39. * William Jolitz.
  40. *
  41. * Redistribution and use in source and binary forms, with or without
  42. * modification, are permitted provided that the following conditions
  43. * are met:
  44. * 1. Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in the
  48. * documentation and/or other materials provided with the distribution.
  49. * 3. Neither the name of the University nor the names of its contributors
  50. * may be used to endorse or promote products derived from this software
  51. * without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  54. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  57. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  58. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  59. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  60. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  61. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  62. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  63. * SUCH DAMAGE.
  64. *
  65. * @(#)isa.c 7.2 (Berkeley) 5/13/91
  66. */
  67. #include <sys/param.h>
  68. #include <sys/systm.h>
  69. #include <sys/syslog.h>
  70. #include <sys/device.h>
  71. #include <sys/malloc.h>
  72. #include <sys/proc.h>
  73. #include <uvm/uvm_extern.h>
  74. #include "ioapic.h"
  75. #if NIOAPIC > 0
  76. #include <machine/i82093var.h>
  77. #include <machine/mpbiosvar.h>
  78. #endif
  79. #include <machine/bus.h>
  80. #include <machine/intr.h>
  81. #include <machine/pio.h>
  82. #include <machine/cpufunc.h>
  83. #include <machine/i8259.h>
  84. #include <dev/isa/isareg.h>
  85. #include <dev/isa/isavar.h>
  86. #include <dev/isa/isadmavar.h>
  87. #include <i386/isa/isa_machdep.h>
  88. #include "isadma.h"
  89. extern paddr_t avail_end;
  90. #define IDTVEC(name) __CONCAT(X,name)
  91. /* default interrupt vector table entries */
  92. typedef int (*vector)(void);
  93. extern vector IDTVEC(intr)[];
  94. void isa_strayintr(int);
  95. void intr_calculatemasks(void);
  96. int fakeintr(void *);
  97. #if NISADMA > 0
  98. int _isa_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int,
  99. bus_size_t, bus_size_t, int, bus_dmamap_t *);
  100. void _isa_bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
  101. int _isa_bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
  102. bus_size_t, struct proc *, int);
  103. int _isa_bus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t,
  104. struct mbuf *, int);
  105. int _isa_bus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t,
  106. struct uio *, int);
  107. int _isa_bus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
  108. bus_dma_segment_t *, int, bus_size_t, int);
  109. void _isa_bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
  110. void _isa_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t,
  111. bus_addr_t, bus_size_t, int);
  112. int _isa_bus_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
  113. bus_size_t, bus_dma_segment_t *, int, int *, int);
  114. int _isa_dma_check_buffer(void *, bus_size_t, int, bus_size_t,
  115. struct proc *);
  116. int _isa_dma_alloc_bouncebuf(bus_dma_tag_t, bus_dmamap_t,
  117. bus_size_t, int);
  118. void _isa_dma_free_bouncebuf(bus_dma_tag_t, bus_dmamap_t);
  119. /*
  120. * Entry points for ISA DMA. These are mostly wrappers around
  121. * the generic functions that understand how to deal with bounce
  122. * buffers, if necessary.
  123. */
  124. struct bus_dma_tag isa_bus_dma_tag = {
  125. NULL, /* _cookie */
  126. _isa_bus_dmamap_create,
  127. _isa_bus_dmamap_destroy,
  128. _isa_bus_dmamap_load,
  129. _isa_bus_dmamap_load_mbuf,
  130. _isa_bus_dmamap_load_uio,
  131. _isa_bus_dmamap_load_raw,
  132. _isa_bus_dmamap_unload,
  133. _isa_bus_dmamap_sync,
  134. _isa_bus_dmamem_alloc,
  135. _bus_dmamem_alloc_range,
  136. _bus_dmamem_free,
  137. _bus_dmamem_map,
  138. _bus_dmamem_unmap,
  139. _bus_dmamem_mmap,
  140. };
  141. #endif /* NISADMA > 0 */
  142. /*
  143. * Fill in default interrupt table (in case of spurious interrupt
  144. * during configuration of kernel, setup interrupt control unit
  145. */
  146. void
  147. isa_defaultirq(void)
  148. {
  149. int i;
  150. /* icu vectors */
  151. for (i = 0; i < ICU_LEN; i++)
  152. setgate(&idt[ICU_OFFSET + i], IDTVEC(intr)[i], 0,
  153. SDT_SYS386IGT, SEL_KPL, GICODE_SEL);
  154. /* initialize 8259's */
  155. outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
  156. outb(IO_ICU1+1, ICU_OFFSET); /* starting at this vector index */
  157. outb(IO_ICU1+1, 1 << IRQ_SLAVE); /* slave on line 2 */
  158. #ifdef AUTO_EOI_1
  159. outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */
  160. #else
  161. outb(IO_ICU1+1, 1); /* 8086 mode */
  162. #endif
  163. outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
  164. outb(IO_ICU1, 0x68); /* special mask mode (if available) */
  165. outb(IO_ICU1, 0x0a); /* Read IRR by default. */
  166. #ifdef REORDER_IRQ
  167. outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */
  168. #endif
  169. outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
  170. outb(IO_ICU2+1, ICU_OFFSET+8); /* staring at this vector index */
  171. outb(IO_ICU2+1, IRQ_SLAVE);
  172. #ifdef AUTO_EOI_2
  173. outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */
  174. #else
  175. outb(IO_ICU2+1, 1); /* 8086 mode */
  176. #endif
  177. outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
  178. outb(IO_ICU2, 0x68); /* special mask mode (if available) */
  179. outb(IO_ICU2, 0x0a); /* Read IRR by default. */
  180. }
  181. /*
  182. * Handle a NMI, possibly a machine check.
  183. * return true to panic system, false to ignore.
  184. */
  185. int
  186. isa_nmi(void)
  187. {
  188. /* This is historic garbage; these ports are not readable */
  189. log(LOG_CRIT, "No-maskable interrupt, may be parity error\n");
  190. return(0);
  191. }
  192. u_long intrstray[ICU_LEN];
  193. /*
  194. * Caught a stray interrupt, notify
  195. */
  196. void
  197. isa_strayintr(int irq)
  198. {
  199. /*
  200. * Stray interrupts on irq 7 occur when an interrupt line is raised
  201. * and then lowered before the CPU acknowledges it. This generally
  202. * means either the device is screwed or something is cli'ing too
  203. * long and it's timing out.
  204. */
  205. if (++intrstray[irq] <= 5)
  206. log(LOG_ERR, "stray interrupt %d%s\n", irq,
  207. intrstray[irq] >= 5 ? "; stopped logging" : "");
  208. }
  209. int intrtype[ICU_LEN], intrmask[ICU_LEN], intrlevel[ICU_LEN];
  210. int iminlevel[ICU_LEN], imaxlevel[ICU_LEN];
  211. struct intrhand *intrhand[ICU_LEN];
  212. int imask[NIPL]; /* Bitmask telling what interrupts are blocked. */
  213. int iunmask[NIPL]; /* Bitmask telling what interrupts are accepted. */
  214. /*
  215. * Recalculate the interrupt masks from scratch.
  216. * We could code special registry and deregistry versions of this function that
  217. * would be faster, but the code would be nastier, and we don't expect this to
  218. * happen very much anyway.
  219. */
  220. void
  221. intr_calculatemasks(void)
  222. {
  223. int irq, level, unusedirqs;
  224. struct intrhand *q;
  225. /* First, figure out which levels each IRQ uses. */
  226. unusedirqs = 0xffff;
  227. for (irq = 0; irq < ICU_LEN; irq++) {
  228. int levels = 0;
  229. for (q = intrhand[irq]; q; q = q->ih_next)
  230. levels |= 1 << IPL(q->ih_level);
  231. intrlevel[irq] = levels;
  232. if (levels)
  233. unusedirqs &= ~(1 << irq);
  234. }
  235. /* Then figure out which IRQs use each level. */
  236. for (level = 0; level < NIPL; level++) {
  237. int irqs = 0;
  238. for (irq = 0; irq < ICU_LEN; irq++)
  239. if (intrlevel[irq] & (1 << level))
  240. irqs |= 1 << irq;
  241. imask[level] = irqs | unusedirqs;
  242. }
  243. /*
  244. * Initialize soft interrupt masks to block themselves.
  245. */
  246. IMASK(IPL_SOFTCLOCK) |= 1 << SIR_CLOCK;
  247. IMASK(IPL_SOFTNET) |= 1 << SIR_NET;
  248. IMASK(IPL_SOFTTTY) |= 1 << SIR_TTY;
  249. /*
  250. * Enforce a hierarchy that gives slow devices a better chance at not
  251. * dropping data.
  252. */
  253. for (level = 0; level < NIPL - 1; level++)
  254. imask[level + 1] |= imask[level];
  255. /* And eventually calculate the complete masks. */
  256. for (irq = 0; irq < ICU_LEN; irq++) {
  257. int irqs = 1 << irq;
  258. int minlevel = IPL_NONE;
  259. int maxlevel = IPL_NONE;
  260. if (intrhand[irq] == NULL) {
  261. maxlevel = IPL_HIGH;
  262. irqs = IMASK(IPL_HIGH);
  263. } else {
  264. for (q = intrhand[irq]; q; q = q->ih_next) {
  265. irqs |= IMASK(q->ih_level);
  266. if (minlevel == IPL_NONE ||
  267. q->ih_level < minlevel)
  268. minlevel = q->ih_level;
  269. if (q->ih_level > maxlevel)
  270. maxlevel = q->ih_level;
  271. }
  272. }
  273. if (irqs != IMASK(maxlevel))
  274. panic("irq %d level %x mask mismatch: %x vs %x", irq,
  275. maxlevel, irqs, IMASK(maxlevel));
  276. intrmask[irq] = irqs;
  277. iminlevel[irq] = minlevel;
  278. imaxlevel[irq] = maxlevel;
  279. #if 0
  280. printf("irq %d: level %x, mask 0x%x (%x)\n", irq,
  281. imaxlevel[irq], intrmask[irq], IMASK(imaxlevel[irq]));
  282. #endif
  283. }
  284. /* Lastly, determine which IRQs are actually in use. */
  285. {
  286. int irqs = 0;
  287. for (irq = 0; irq < ICU_LEN; irq++)
  288. if (intrhand[irq])
  289. irqs |= 1 << irq;
  290. if (irqs >= 0x100) /* any IRQs >= 8 in use */
  291. irqs |= 1 << IRQ_SLAVE;
  292. imen = ~irqs;
  293. SET_ICUS();
  294. }
  295. /* For speed of splx, provide the inverse of the interrupt masks. */
  296. for (irq = 0; irq < ICU_LEN; irq++)
  297. iunmask[irq] = ~imask[irq];
  298. }
  299. int
  300. fakeintr(arg)
  301. void *arg;
  302. {
  303. return 0;
  304. }
  305. #define LEGAL_IRQ(x) ((x) >= 0 && (x) < ICU_LEN && (x) != 2)
  306. int
  307. isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
  308. {
  309. int i, bestirq, count;
  310. int tmp;
  311. struct intrhand **p, *q;
  312. if (type == IST_NONE)
  313. panic("intr_alloc: bogus type");
  314. bestirq = -1;
  315. count = -1;
  316. /* some interrupts should never be dynamically allocated */
  317. mask &= 0xdef8;
  318. /*
  319. * XXX some interrupts will be used later (6 for fdc, 12 for pms).
  320. * the right answer is to do "breadth-first" searching of devices.
  321. */
  322. mask &= 0xefbf;
  323. for (i = 0; i < ICU_LEN; i++) {
  324. if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
  325. continue;
  326. switch(intrtype[i]) {
  327. case IST_NONE:
  328. /*
  329. * if nothing's using the irq, just return it
  330. */
  331. *irq = i;
  332. return (0);
  333. case IST_EDGE:
  334. case IST_LEVEL:
  335. if (type != intrtype[i])
  336. continue;
  337. /*
  338. * if the irq is shareable, count the number of other
  339. * handlers, and if it's smaller than the last irq like
  340. * this, remember it
  341. *
  342. * XXX We should probably also consider the
  343. * interrupt level and stick IPL_TTY with other
  344. * IPL_TTY, etc.
  345. */
  346. for (p = &intrhand[i], tmp = 0; (q = *p) != NULL;
  347. p = &q->ih_next, tmp++)
  348. ;
  349. if ((bestirq == -1) || (count > tmp)) {
  350. bestirq = i;
  351. count = tmp;
  352. }
  353. break;
  354. case IST_PULSE:
  355. /* this just isn't shareable */
  356. continue;
  357. }
  358. }
  359. if (bestirq == -1)
  360. return (1);
  361. *irq = bestirq;
  362. return (0);
  363. }
  364. /*
  365. * Just check to see if an IRQ is available/can be shared.
  366. * 0 = interrupt not available
  367. * 1 = interrupt shareable
  368. * 2 = interrupt all to ourself
  369. */
  370. int
  371. isa_intr_check(isa_chipset_tag_t ic, int irq, int type)
  372. {
  373. if (!LEGAL_IRQ(irq) || type == IST_NONE)
  374. return (0);
  375. switch (intrtype[irq]) {
  376. case IST_NONE:
  377. return (2);
  378. break;
  379. case IST_LEVEL:
  380. if (type != intrtype[irq])
  381. return (0);
  382. return (1);
  383. break;
  384. case IST_EDGE:
  385. case IST_PULSE:
  386. if (type != IST_NONE)
  387. return (0);
  388. }
  389. return (1);
  390. }
  391. /*
  392. * Set up an interrupt handler to start being called.
  393. * XXX PRONE TO RACE CONDITIONS, UGLY, 'INTERESTING' INSERTION ALGORITHM.
  394. */
  395. void *
  396. isa_intr_establish(isa_chipset_tag_t ic, int irq, int type, int level,
  397. int (*ih_fun)(void *), void *ih_arg, const char *ih_what)
  398. {
  399. struct intrhand **p, *q, *ih;
  400. static struct intrhand fakehand = {fakeintr};
  401. int flags;
  402. #if NIOAPIC > 0
  403. struct mp_intr_map *mip;
  404. if (mp_busses != NULL) {
  405. int mpspec_pin = irq;
  406. int airq;
  407. if (mp_isa_bus == NULL)
  408. panic("no isa bus");
  409. for (mip = mp_isa_bus->mb_intrs; mip != NULL;
  410. mip = mip->next) {
  411. if (mip->bus_pin == mpspec_pin) {
  412. airq = mip->ioapic_ih | irq;
  413. break;
  414. }
  415. }
  416. if (mip == NULL && mp_eisa_bus) {
  417. for (mip = mp_eisa_bus->mb_intrs; mip != NULL;
  418. mip = mip->next) {
  419. if (mip->bus_pin == mpspec_pin) {
  420. airq = mip->ioapic_ih | irq;
  421. break;
  422. }
  423. }
  424. }
  425. /* no MP mapping found -- invent! */
  426. if (mip == NULL)
  427. airq = mpbios_invent(irq, type, mp_isa_bus->mb_idx);
  428. return (apic_intr_establish(airq, type, level, ih_fun,
  429. ih_arg, ih_what));
  430. }
  431. #endif
  432. flags = level & IPL_MPSAFE;
  433. level &= ~IPL_MPSAFE;
  434. KASSERT(level <= IPL_TTY || level >= IPL_CLOCK || flags & IPL_MPSAFE);
  435. /* no point in sleeping unless someone can free memory. */
  436. ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
  437. if (ih == NULL) {
  438. printf("%s: isa_intr_establish: can't malloc handler info\n",
  439. ih_what);
  440. return (NULL);
  441. }
  442. if (!LEGAL_IRQ(irq) || type == IST_NONE) {
  443. printf("%s: isa_intr_establish: bogus irq or type\n", ih_what);
  444. free(ih, M_DEVBUF, 0);
  445. return (NULL);
  446. }
  447. switch (intrtype[irq]) {
  448. case IST_NONE:
  449. intrtype[irq] = type;
  450. break;
  451. case IST_EDGE:
  452. intr_shared_edge = 1;
  453. /* FALLTHROUGH */
  454. case IST_LEVEL:
  455. if (type == intrtype[irq])
  456. break;
  457. case IST_PULSE:
  458. if (type != IST_NONE) {
  459. /*printf("%s: intr_establish: can't share %s with %s, irq %d\n",
  460. ih_what, isa_intr_typename(intrtype[irq]),
  461. isa_intr_typename(type), irq);*/
  462. free(ih, M_DEVBUF, 0);
  463. return (NULL);
  464. }
  465. break;
  466. }
  467. /*
  468. * Figure out where to put the handler.
  469. * This is O(N^2), but we want to preserve the order, and N is
  470. * generally small.
  471. */
  472. for (p = &intrhand[irq]; (q = *p) != NULL; p = &q->ih_next)
  473. ;
  474. /*
  475. * Actually install a fake handler momentarily, since we might be doing
  476. * this with interrupts enabled and don't want the real routine called
  477. * until masking is set up.
  478. */
  479. fakehand.ih_level = level;
  480. *p = &fakehand;
  481. intr_calculatemasks();
  482. /*
  483. * Poke the real handler in now.
  484. */
  485. ih->ih_fun = ih_fun;
  486. ih->ih_arg = ih_arg;
  487. ih->ih_next = NULL;
  488. ih->ih_level = level;
  489. ih->ih_flags = flags;
  490. ih->ih_irq = irq;
  491. evcount_attach(&ih->ih_count, ih_what, &ih->ih_irq);
  492. *p = ih;
  493. return (ih);
  494. }
  495. /*
  496. * Deregister an interrupt handler.
  497. */
  498. void
  499. isa_intr_disestablish(isa_chipset_tag_t ic, void *arg)
  500. {
  501. struct intrhand *ih = arg;
  502. int irq = ih->ih_irq;
  503. struct intrhand **p, *q;
  504. #if NIOAPIC > 0
  505. if (irq & APIC_INT_VIA_APIC) {
  506. apic_intr_disestablish(arg);
  507. return;
  508. }
  509. #endif
  510. if (!LEGAL_IRQ(irq))
  511. panic("intr_disestablish: bogus irq %d", irq);
  512. /*
  513. * Remove the handler from the chain.
  514. * This is O(n^2), too.
  515. */
  516. for (p = &intrhand[irq]; (q = *p) != NULL && q != ih; p = &q->ih_next)
  517. ;
  518. if (q)
  519. *p = q->ih_next;
  520. else
  521. panic("intr_disestablish: handler not registered");
  522. evcount_detach(&ih->ih_count);
  523. free(ih, M_DEVBUF, 0);
  524. intr_calculatemasks();
  525. if (intrhand[irq] == NULL)
  526. intrtype[irq] = IST_NONE;
  527. }
  528. void
  529. isa_attach_hook(struct device *parent, struct device *self,
  530. struct isabus_attach_args *iba)
  531. {
  532. extern int isa_has_been_seen;
  533. /*
  534. * Notify others that might need to know that the ISA bus
  535. * has now been attached.
  536. */
  537. if (isa_has_been_seen)
  538. panic("isaattach: ISA bus already seen!");
  539. isa_has_been_seen = 1;
  540. }
  541. #if NISADMA > 0
  542. /**********************************************************************
  543. * bus.h dma interface entry points
  544. **********************************************************************/
  545. #ifdef ISA_DMA_STATS
  546. #define STAT_INCR(v) (v)++
  547. #define STAT_DECR(v) do { \
  548. if ((v) == 0) \
  549. printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
  550. else \
  551. (v)--; \
  552. } while (0)
  553. u_long isa_dma_stats_loads;
  554. u_long isa_dma_stats_bounces;
  555. u_long isa_dma_stats_nbouncebufs;
  556. #else
  557. #define STAT_INCR(v)
  558. #define STAT_DECR(v)
  559. #endif
  560. /*
  561. * Create an ISA DMA map.
  562. */
  563. int
  564. _isa_bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
  565. bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
  566. {
  567. struct isa_dma_cookie *cookie;
  568. bus_dmamap_t map;
  569. int error, cookieflags;
  570. void *cookiestore;
  571. size_t cookiesize;
  572. /* Call common function to create the basic map. */
  573. error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
  574. flags, dmamp);
  575. if (error)
  576. return (error);
  577. map = *dmamp;
  578. map->_dm_cookie = NULL;
  579. cookiesize = sizeof(struct isa_dma_cookie);
  580. /*
  581. * ISA only has 24-bits of address space. This means
  582. * we can't DMA to pages over 16M. In order to DMA to
  583. * arbitrary buffers, we use "bounce buffers" - pages
  584. * in memory below the 16M boundary. On DMA reads,
  585. * DMA happens to the bounce buffers, and is copied into
  586. * the caller's buffer. On writes, data is copied into
  587. * the bounce buffer, and the DMA happens from those
  588. * pages. To software using the DMA mapping interface,
  589. * this looks simply like a data cache.
  590. *
  591. * If we have more than 16M of RAM in the system, we may
  592. * need bounce buffers. We check and remember that here.
  593. *
  594. * There are exceptions, however. VLB devices can do
  595. * 32-bit DMA, and indicate that here.
  596. *
  597. * ...or, there is an opposite case. The most segments
  598. * a transfer will require is (maxxfer / NBPG) + 1. If
  599. * the caller can't handle that many segments (e.g. the
  600. * ISA DMA controller), we may have to bounce it as well.
  601. */
  602. cookieflags = 0;
  603. if ((avail_end > ISA_DMA_BOUNCE_THRESHOLD &&
  604. (flags & ISABUS_DMA_32BIT) == 0) ||
  605. ((map->_dm_size / NBPG) + 1) > map->_dm_segcnt) {
  606. cookieflags |= ID_MIGHT_NEED_BOUNCE;
  607. cookiesize += (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
  608. }
  609. /*
  610. * Allocate our cookie.
  611. */
  612. if ((cookiestore = malloc(cookiesize, M_DEVBUF,
  613. ((flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)|M_ZERO)) == NULL) {
  614. error = ENOMEM;
  615. goto out;
  616. }
  617. cookie = (struct isa_dma_cookie *)cookiestore;
  618. cookie->id_flags = cookieflags;
  619. map->_dm_cookie = cookie;
  620. if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
  621. /*
  622. * Allocate the bounce pages now if the caller
  623. * wishes us to do so.
  624. */
  625. if ((flags & BUS_DMA_ALLOCNOW) == 0)
  626. goto out;
  627. error = _isa_dma_alloc_bouncebuf(t, map, size, flags);
  628. }
  629. out:
  630. if (error) {
  631. if (map->_dm_cookie != NULL)
  632. free(map->_dm_cookie, M_DEVBUF, 0);
  633. _bus_dmamap_destroy(t, map);
  634. }
  635. return (error);
  636. }
  637. /*
  638. * Destroy an ISA DMA map.
  639. */
  640. void
  641. _isa_bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
  642. {
  643. struct isa_dma_cookie *cookie = map->_dm_cookie;
  644. /*
  645. * Free any bounce pages this map might hold.
  646. */
  647. if (cookie->id_flags & ID_HAS_BOUNCE)
  648. _isa_dma_free_bouncebuf(t, map);
  649. free(cookie, M_DEVBUF, 0);
  650. _bus_dmamap_destroy(t, map);
  651. }
  652. /*
  653. * Load an ISA DMA map with a linear buffer.
  654. */
  655. int
  656. _isa_bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
  657. bus_size_t buflen, struct proc *p, int flags)
  658. {
  659. struct isa_dma_cookie *cookie = map->_dm_cookie;
  660. int error;
  661. STAT_INCR(isa_dma_stats_loads);
  662. /*
  663. * Check to see if we might need to bounce the transfer.
  664. */
  665. if (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) {
  666. /*
  667. * Check if all pages are below the bounce
  668. * threshold. If they are, don't bother bouncing.
  669. */
  670. if (_isa_dma_check_buffer(buf, buflen,
  671. map->_dm_segcnt, map->_dm_boundary, p) == 0)
  672. return (_bus_dmamap_load(t, map, buf, buflen,
  673. p, flags));
  674. STAT_INCR(isa_dma_stats_bounces);
  675. /*
  676. * Allocate bounce pages, if necessary.
  677. */
  678. if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
  679. error = _isa_dma_alloc_bouncebuf(t, map, buflen,
  680. flags);
  681. if (error)
  682. return (error);
  683. }
  684. /*
  685. * Cache a pointer to the caller's buffer and
  686. * load the DMA map with the bounce buffer.
  687. */
  688. cookie->id_origbuf = buf;
  689. cookie->id_origbuflen = buflen;
  690. error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
  691. buflen, p, flags);
  692. if (error) {
  693. /*
  694. * Free the bounce pages, unless our resources
  695. * are reserved for our exclusive use.
  696. */
  697. if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
  698. _isa_dma_free_bouncebuf(t, map);
  699. }
  700. /* ...so _isa_bus_dmamap_sync() knows we're bouncing */
  701. cookie->id_flags |= ID_IS_BOUNCING;
  702. } else {
  703. /*
  704. * Just use the generic load function.
  705. */
  706. error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
  707. }
  708. return (error);
  709. }
  710. /*
  711. * Like _isa_bus_dmamap_load(), but for mbufs.
  712. */
  713. int
  714. _isa_bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m,
  715. int flags)
  716. {
  717. panic("_isa_bus_dmamap_load_mbuf: not implemented");
  718. }
  719. /*
  720. * Like _isa_bus_dmamap_load(), but for uios.
  721. */
  722. int
  723. _isa_bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
  724. int flags)
  725. {
  726. panic("_isa_bus_dmamap_load_uio: not implemented");
  727. }
  728. /*
  729. * Like _isa_bus_dmamap_load(), but for raw memory allocated with
  730. * bus_dmamem_alloc().
  731. */
  732. int
  733. _isa_bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
  734. bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
  735. {
  736. panic("_isa_bus_dmamap_load_raw: not implemented");
  737. }
  738. /*
  739. * Unload an ISA DMA map.
  740. */
  741. void
  742. _isa_bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
  743. {
  744. struct isa_dma_cookie *cookie = map->_dm_cookie;
  745. /*
  746. * If we have bounce pages, free them, unless they're
  747. * reserved for our exclusive use.
  748. */
  749. if ((cookie->id_flags & ID_HAS_BOUNCE) &&
  750. (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
  751. _isa_dma_free_bouncebuf(t, map);
  752. cookie->id_flags &= ~ID_IS_BOUNCING;
  753. /*
  754. * Do the generic bits of the unload.
  755. */
  756. _bus_dmamap_unload(t, map);
  757. }
  758. /*
  759. * Synchronize an ISA DMA map.
  760. */
  761. void
  762. _isa_bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
  763. bus_size_t len, int op)
  764. {
  765. struct isa_dma_cookie *cookie = map->_dm_cookie;
  766. #ifdef DEBUG
  767. if ((op & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
  768. if (offset >= map->dm_mapsize)
  769. panic("_isa_bus_dmamap_sync: bad offset");
  770. if (len == 0 || (offset + len) > map->dm_mapsize)
  771. panic("_isa_bus_dmamap_sync: bad length");
  772. }
  773. #endif
  774. #ifdef DIAGNOSTIC
  775. if ((op & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) != 0 &&
  776. (op & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE)) != 0)
  777. panic("_isa_bus_dmamap_sync: mix PRE and POST");
  778. #endif /* DIAGNOSTIC */
  779. /* PREREAD and POSTWRITE are no-ops */
  780. if (op & BUS_DMASYNC_PREWRITE) {
  781. /*
  782. * If we're bouncing this transfer, copy the
  783. * caller's buffer to the bounce buffer.
  784. */
  785. if (cookie->id_flags & ID_IS_BOUNCING)
  786. memcpy(cookie->id_bouncebuf + offset,
  787. (char *)cookie->id_origbuf + offset, len);
  788. }
  789. _bus_dmamap_sync(t, map, offset, len, op);
  790. if (op & BUS_DMASYNC_POSTREAD) {
  791. /*
  792. * If we're bouncing this transfer, copy the
  793. * bounce buffer to the caller's buffer.
  794. */
  795. if (cookie->id_flags & ID_IS_BOUNCING)
  796. memcpy(cookie->id_origbuf + offset,
  797. (char *)cookie->id_bouncebuf + offset, len);
  798. }
  799. }
  800. /*
  801. * Allocate memory safe for ISA DMA.
  802. */
  803. int
  804. _isa_bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
  805. bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
  806. int flags)
  807. {
  808. int error;
  809. /* Try in ISA addressable region first */
  810. error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
  811. segs, nsegs, rsegs, flags, 0, ISA_DMA_BOUNCE_THRESHOLD);
  812. if (!error)
  813. return (error);
  814. /* Otherwise try anywhere (we'll bounce later) */
  815. error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
  816. segs, nsegs, rsegs, flags, (bus_addr_t)0, (bus_addr_t)-1);
  817. return (error);
  818. }
  819. /**********************************************************************
  820. * ISA DMA utility functions
  821. **********************************************************************/
  822. /*
  823. * Return 0 if all pages in the passed buffer lie within the DMA'able
  824. * range RAM.
  825. */
  826. int
  827. _isa_dma_check_buffer(void *buf, bus_size_t buflen, int segcnt,
  828. bus_size_t boundary, struct proc *p)
  829. {
  830. vaddr_t vaddr = (vaddr_t)buf;
  831. vaddr_t endva;
  832. paddr_t pa, lastpa;
  833. u_long pagemask = ~(boundary - 1);
  834. pmap_t pmap;
  835. int nsegs;
  836. endva = round_page(vaddr + buflen);
  837. nsegs = 1;
  838. lastpa = 0;
  839. if (p != NULL)
  840. pmap = p->p_vmspace->vm_map.pmap;
  841. else
  842. pmap = pmap_kernel();
  843. for (; vaddr < endva; vaddr += NBPG) {
  844. /*
  845. * Get physical address for this segment.
  846. */
  847. pmap_extract(pmap, (vaddr_t)vaddr, &pa);
  848. pa = trunc_page(pa);
  849. /*
  850. * Is it below the DMA'able threshold?
  851. */
  852. if (pa > ISA_DMA_BOUNCE_THRESHOLD)
  853. return (EINVAL);
  854. if (lastpa) {
  855. /*
  856. * Check excessive segment count.
  857. */
  858. if (lastpa + NBPG != pa) {
  859. if (++nsegs > segcnt)
  860. return (EFBIG);
  861. }
  862. /*
  863. * Check boundary restriction.
  864. */
  865. if (boundary) {
  866. if ((lastpa ^ pa) & pagemask)
  867. return (EINVAL);
  868. }
  869. }
  870. lastpa = pa;
  871. }
  872. return (0);
  873. }
  874. int
  875. _isa_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, bus_size_t size, int flags)
  876. {
  877. struct isa_dma_cookie *cookie = map->_dm_cookie;
  878. int error = 0;
  879. cookie->id_bouncebuflen = round_page(size);
  880. error = _bus_dmamem_alloc_range(t, cookie->id_bouncebuflen,
  881. NBPG, map->_dm_boundary, cookie->id_bouncesegs,
  882. map->_dm_segcnt, &cookie->id_nbouncesegs, flags,
  883. 0, ISA_DMA_BOUNCE_THRESHOLD);
  884. if (error)
  885. goto out;
  886. error = _bus_dmamem_map(t, cookie->id_bouncesegs,
  887. cookie->id_nbouncesegs, cookie->id_bouncebuflen,
  888. (caddr_t *)&cookie->id_bouncebuf, flags);
  889. out:
  890. if (error) {
  891. _bus_dmamem_free(t, cookie->id_bouncesegs,
  892. cookie->id_nbouncesegs);
  893. cookie->id_bouncebuflen = 0;
  894. cookie->id_nbouncesegs = 0;
  895. } else {
  896. cookie->id_flags |= ID_HAS_BOUNCE;
  897. STAT_INCR(isa_dma_stats_nbouncebufs);
  898. }
  899. return (error);
  900. }
  901. void
  902. _isa_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
  903. {
  904. struct isa_dma_cookie *cookie = map->_dm_cookie;
  905. STAT_DECR(isa_dma_stats_nbouncebufs);
  906. _bus_dmamem_unmap(t, cookie->id_bouncebuf,
  907. cookie->id_bouncebuflen);
  908. _bus_dmamem_free(t, cookie->id_bouncesegs,
  909. cookie->id_nbouncesegs);
  910. cookie->id_bouncebuflen = 0;
  911. cookie->id_nbouncesegs = 0;
  912. cookie->id_flags &= ~ID_HAS_BOUNCE;
  913. }
  914. #endif /* NISADMA > 0 */