PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/netbsd/src/sys/arch/cobalt/cobalt/interrupt.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 467 lines | 276 code | 74 blank | 117 comment | 48 complexity | 0d76b042fb1fd49b4a7617c4a76f6787 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* $NetBSD: interrupt.c,v 1.7 2011/07/01 20:36:42 dyoung Exp $ */
  2. /*-
  3. * Copyright (c) 2006 Izumi Tsutsui. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*-
  26. * Copyright (c) 2001 The NetBSD Foundation, Inc.
  27. * All rights reserved.
  28. *
  29. * This code is derived from software contributed to The NetBSD Foundation
  30. * by Jason R. Thorpe.
  31. *
  32. * Redistribution and use in source and binary forms, with or without
  33. * modification, are permitted provided that the following conditions
  34. * are met:
  35. * 1. Redistributions of source code must retain the above copyright
  36. * notice, this list of conditions and the following disclaimer.
  37. * 2. Redistributions in binary form must reproduce the above copyright
  38. * notice, this list of conditions and the following disclaimer in the
  39. * documentation and/or other materials provided with the distribution.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  42. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  43. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  44. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  45. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  46. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  47. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  49. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  51. * POSSIBILITY OF SUCH DAMAGE.
  52. */
  53. /*
  54. * Copyright (c) 2000 Soren S. Jorvang. All rights reserved.
  55. *
  56. * Redistribution and use in source and binary forms, with or without
  57. * modification, are permitted provided that the following conditions
  58. * are met:
  59. * 1. Redistributions of source code must retain the above copyright
  60. * notice, this list of conditions, and the following disclaimer.
  61. * 2. Redistributions in binary form must reproduce the above copyright
  62. * notice, this list of conditions and the following disclaimer in the
  63. * documentation and/or other materials provided with the distribution.
  64. *
  65. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  66. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  69. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  70. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  71. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  72. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  75. * SUCH DAMAGE.
  76. */
  77. #include <sys/cdefs.h>
  78. __KERNEL_RCSID(0, "$NetBSD: interrupt.c,v 1.7 2011/07/01 20:36:42 dyoung Exp $");
  79. #define __INTR_PRIVATE
  80. #include <sys/param.h>
  81. #include <sys/malloc.h>
  82. #include <sys/cpu.h>
  83. #include <sys/intr.h>
  84. #include <mips/mips3_clock.h>
  85. #include <sys/bus.h>
  86. #include <dev/ic/i8259reg.h>
  87. #include <dev/isa/isareg.h>
  88. #include <cobalt/dev/gtreg.h>
  89. #define ICU_LEVEL 4
  90. #define IRQ_SLAVE 2
  91. #define IO_ELCR 0x4d0
  92. #define IO_ELCRSIZE 2
  93. #define ELCR0 0
  94. #define ELCR1 1
  95. #define ICU1_READ(reg) \
  96. bus_space_read_1(icu_bst, icu1_bsh, (reg))
  97. #define ICU1_WRITE(reg, val) \
  98. bus_space_write_1(icu_bst, icu1_bsh, (reg), (val))
  99. #define ICU2_READ(reg) \
  100. bus_space_read_1(icu_bst, icu2_bsh, (reg))
  101. #define ICU2_WRITE(reg, val) \
  102. bus_space_write_1(icu_bst, icu2_bsh, (reg), (val))
  103. #define ELCR_READ(reg) \
  104. bus_space_read_1(icu_bst, elcr_bsh, (reg))
  105. #define ELCR_WRITE(reg, val) \
  106. bus_space_write_1(icu_bst, elcr_bsh, (reg), (val))
  107. static u_int icu_imask, icu_elcr;
  108. static bus_space_tag_t icu_bst;
  109. static bus_space_handle_t icu1_bsh, icu2_bsh, elcr_bsh;
  110. struct icu_intrhead {
  111. LIST_HEAD(, cobalt_intrhand) intr_q;
  112. int intr_type;
  113. struct evcnt intr_evcnt;
  114. char intr_evname[32];
  115. };
  116. static struct icu_intrhead icu_intrtab[NICU_INT];
  117. struct cpu_intrhead {
  118. struct cobalt_intrhand intr_ih;
  119. struct evcnt intr_evcnt;
  120. char intr_evname[32];
  121. };
  122. static struct cpu_intrhead cpu_intrtab[NCPU_INT];
  123. static int icu_intr(void *);
  124. static void icu_set(void);
  125. static const struct ipl_sr_map cobalt_ipl_sr_map = {
  126. .sr_bits = {
  127. [IPL_NONE] = MIPS_INT_MASK_0,
  128. [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0 | MIPS_INT_MASK_0,
  129. [IPL_SOFTBIO] = MIPS_SOFT_INT_MASK_0 | MIPS_INT_MASK_0,
  130. [IPL_SOFTNET] = MIPS_SOFT_INT_MASK | MIPS_INT_MASK_0,
  131. [IPL_SOFTSERIAL] = MIPS_SOFT_INT_MASK | MIPS_INT_MASK_0,
  132. [IPL_VM] = MIPS_INT_MASK ^ MIPS_INT_MASK_5,
  133. [IPL_SCHED] = MIPS_INT_MASK,
  134. [IPL_DDB] = MIPS_INT_MASK,
  135. [IPL_HIGH] = MIPS_INT_MASK,
  136. },
  137. };
  138. void
  139. intr_init(void)
  140. {
  141. int i;
  142. ipl_sr_map = cobalt_ipl_sr_map;
  143. /*
  144. * Initialize CPU interrupts.
  145. */
  146. for (i = 0; i < NCPU_INT; i++) {
  147. snprintf(cpu_intrtab[i].intr_evname,
  148. sizeof(cpu_intrtab[i].intr_evname), "int %d", i);
  149. evcnt_attach_dynamic(&cpu_intrtab[i].intr_evcnt,
  150. EVCNT_TYPE_INTR, NULL, "mips", cpu_intrtab[i].intr_evname);
  151. }
  152. /*
  153. * Initialize ICU interrupts.
  154. */
  155. icu_bst = 0; /* XXX unused on cobalt */
  156. bus_space_map(icu_bst, PCIB_BASE + IO_ICU1, IO_ICUSIZE, 0, &icu1_bsh);
  157. bus_space_map(icu_bst, PCIB_BASE + IO_ICU2, IO_ICUSIZE, 0, &icu2_bsh);
  158. bus_space_map(icu_bst, PCIB_BASE + IO_ELCR, IO_ELCRSIZE, 0, &elcr_bsh);
  159. /* All interrupts default to "masked off". */
  160. icu_imask = 0xffff;
  161. /* All interrupts default to edge-triggered. */
  162. icu_elcr = 0;
  163. /* Initialize master PIC */
  164. /* reset; program device, four bytes */
  165. ICU1_WRITE(PIC_ICW1, ICW1_SELECT | ICW1_IC4);
  166. /* starting at this vector index */
  167. ICU1_WRITE(PIC_ICW2, 0); /* XXX */
  168. /* slave on line 2 */
  169. ICU1_WRITE(PIC_ICW3, ICW3_CASCADE(IRQ_SLAVE));
  170. /* special fully nested mode, 8086 mode */
  171. ICU1_WRITE(PIC_ICW4, ICW4_SFNM | ICW4_8086);
  172. /* mask all interrupts */
  173. ICU1_WRITE(PIC_OCW1, icu_imask & 0xff);
  174. /* special mask mode */
  175. ICU1_WRITE(PIC_OCW3, OCW3_SELECT | OCW3_SSMM | OCW3_SMM);
  176. /* read IRR by default */
  177. ICU1_WRITE(PIC_OCW3, OCW3_SELECT | OCW3_RR);
  178. /* Initialize slave PIC */
  179. /* reset; program device, four bytes */
  180. ICU2_WRITE(PIC_ICW1, ICW1_SELECT | ICW1_IC4);
  181. /* starting at this vector index */
  182. ICU2_WRITE(PIC_ICW2, 8); /* XXX */
  183. /* slave connected to line 2 of master */
  184. ICU2_WRITE(PIC_ICW3, ICW3_SIC(IRQ_SLAVE));
  185. /* special fully nested mode, 8086 mode */
  186. ICU2_WRITE(PIC_ICW4, ICW4_SFNM | ICW4_8086);
  187. /* mask all interrupts */
  188. ICU1_WRITE(PIC_OCW1, (icu_imask >> 8) & 0xff);
  189. /* special mask mode */
  190. ICU2_WRITE(PIC_OCW3, OCW3_SELECT | OCW3_SSMM | OCW3_SMM);
  191. /* read IRR by default */
  192. ICU2_WRITE(PIC_OCW3, OCW3_SELECT | OCW3_RR);
  193. /* default to edge-triggered */
  194. ELCR_WRITE(ELCR0, icu_elcr & 0xff);
  195. ELCR_WRITE(ELCR1, (icu_elcr >> 8) & 0xff);
  196. wbflush();
  197. /* Initialize our interrupt table. */
  198. for (i = 0; i < NICU_INT; i++) {
  199. LIST_INIT(&icu_intrtab[i].intr_q);
  200. snprintf(icu_intrtab[i].intr_evname,
  201. sizeof(icu_intrtab[i].intr_evname), "irq %d", i);
  202. evcnt_attach_dynamic(&icu_intrtab[i].intr_evcnt,
  203. EVCNT_TYPE_INTR, &cpu_intrtab[ICU_LEVEL].intr_evcnt,
  204. "icu", icu_intrtab[i].intr_evname);
  205. icu_intrtab[i].intr_type = IST_NONE;
  206. }
  207. cpu_intr_establish(ICU_LEVEL, IPL_NONE, icu_intr, NULL);
  208. }
  209. void *
  210. icu_intr_establish(int irq, int type, int ipl, int (*func)(void *), void *arg)
  211. {
  212. struct cobalt_intrhand *ih;
  213. int s;
  214. if (irq >= NICU_INT || irq == IRQ_SLAVE || type == IST_NONE)
  215. panic("%s: bad irq or type", __func__);
  216. switch (icu_intrtab[irq].intr_type) {
  217. case IST_NONE:
  218. icu_intrtab[irq].intr_type = type;
  219. break;
  220. case IST_EDGE:
  221. case IST_LEVEL:
  222. if (type == icu_intrtab[irq].intr_type)
  223. break;
  224. /* FALLTHROUGH */
  225. case IST_PULSE:
  226. /*
  227. * We can't share interrupts in this case.
  228. */
  229. return NULL;
  230. }
  231. ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
  232. if (ih == NULL)
  233. return NULL;
  234. ih->ih_func = func;
  235. ih->ih_arg = arg;
  236. ih->ih_irq = irq;
  237. ih->ih_cookie_type = COBALT_COOKIE_TYPE_ICU;
  238. s = splhigh();
  239. /* Insert the handler into the table. */
  240. LIST_INSERT_HEAD(&icu_intrtab[irq].intr_q, ih, ih_q);
  241. /* Enable it, set trigger mode. */
  242. icu_imask &= ~(1U << irq);
  243. if (icu_intrtab[irq].intr_type == IST_LEVEL)
  244. icu_elcr |= (1U << irq);
  245. else
  246. icu_elcr &= ~(1U << irq);
  247. icu_set();
  248. splx(s);
  249. return ih;
  250. }
  251. void
  252. icu_intr_disestablish(void *cookie)
  253. {
  254. struct cobalt_intrhand *ih = cookie;
  255. int s;
  256. if (ih->ih_cookie_type == COBALT_COOKIE_TYPE_ICU) {
  257. s = splhigh();
  258. LIST_REMOVE(ih, ih_q);
  259. if (LIST_FIRST(&icu_intrtab[ih->ih_irq].intr_q) == NULL) {
  260. icu_imask |= (1U << ih->ih_irq);
  261. icu_set();
  262. }
  263. splx(s);
  264. free(ih, M_DEVBUF);
  265. }
  266. }
  267. void
  268. icu_set(void)
  269. {
  270. if ((icu_imask & 0xff00) != 0xff00)
  271. icu_imask &= ~(1U << IRQ_SLAVE);
  272. else
  273. icu_imask |= (1U << IRQ_SLAVE);
  274. ICU1_WRITE(PIC_OCW1, icu_imask);
  275. ICU2_WRITE(PIC_OCW1, icu_imask >> 8);
  276. ELCR_WRITE(ELCR0, icu_elcr);
  277. ELCR_WRITE(ELCR1, icu_elcr >> 8);
  278. }
  279. int
  280. icu_intr(void *arg)
  281. {
  282. struct cobalt_intrhand *ih;
  283. int irq, handled;
  284. handled = 0;
  285. for (;;) {
  286. /* check requested irq */
  287. ICU1_WRITE(PIC_OCW3, OCW3_SELECT | OCW3_POLL);
  288. irq = ICU1_READ(PIC_OCW3);
  289. if ((irq & OCW3_POLL_PENDING) == 0)
  290. return handled;
  291. irq = OCW3_POLL_IRQ(irq);
  292. if (irq == IRQ_SLAVE) {
  293. ICU2_WRITE(PIC_OCW3, OCW3_SELECT | OCW3_POLL);
  294. irq = OCW3_POLL_IRQ(ICU2_READ(PIC_OCW3)) + 8;
  295. }
  296. icu_intrtab[irq].intr_evcnt.ev_count++;
  297. LIST_FOREACH(ih, &icu_intrtab[irq].intr_q, ih_q) {
  298. if (__predict_false(ih->ih_func == NULL))
  299. printf("%s: spurious interrupt (irq = %d)\n",
  300. __func__, irq);
  301. else if (__predict_true((*ih->ih_func)(ih->ih_arg))) {
  302. handled = 1;
  303. }
  304. }
  305. /* issue EOI to ack */
  306. if (irq >= 8) {
  307. ICU2_WRITE(PIC_OCW2,
  308. OCW2_SELECT | OCW2_SL | OCW2_EOI |
  309. OCW2_ILS(irq - 8));
  310. irq = IRQ_SLAVE;
  311. }
  312. ICU1_WRITE(PIC_OCW2,
  313. OCW2_SELECT | OCW2_SL | OCW2_EOI | OCW2_ILS(irq));
  314. }
  315. }
  316. void *
  317. cpu_intr_establish(int level, int ipl, int (*func)(void *), void *arg)
  318. {
  319. struct cobalt_intrhand *ih;
  320. if (level < 0 || level >= NCPU_INT)
  321. panic("invalid interrupt level");
  322. ih = &cpu_intrtab[level].intr_ih;
  323. if (ih->ih_func != NULL)
  324. panic("cannot share CPU interrupts");
  325. ih->ih_cookie_type = COBALT_COOKIE_TYPE_CPU;
  326. ih->ih_func = func;
  327. ih->ih_arg = arg;
  328. ih->ih_irq = NICU_INT + level;
  329. return ih;
  330. }
  331. void
  332. cpu_intr_disestablish(void *cookie)
  333. {
  334. struct cobalt_intrhand *ih = cookie;
  335. if (ih->ih_cookie_type == COBALT_COOKIE_TYPE_CPU) {
  336. ih->ih_func = NULL;
  337. ih->ih_arg = NULL;
  338. ih->ih_cookie_type = 0;
  339. }
  340. }
  341. static void inline
  342. intr_handle(struct cpu_intrhead *intr)
  343. {
  344. struct cobalt_intrhand * const ih = &intr->intr_ih;
  345. if (__predict_true(ih->ih_func != NULL)
  346. && __predict_true((*ih->ih_func)(ih->ih_arg))) {
  347. intr->intr_evcnt.ev_count++;
  348. }
  349. }
  350. void
  351. cpu_intr(int ppl, vaddr_t pc, uint32_t status)
  352. {
  353. uint32_t pending;
  354. int ipl;
  355. curcpu()->ci_data.cpu_nintr++;
  356. while (ppl < (ipl = splintr(&pending))) {
  357. splx(ipl);
  358. if (pending & MIPS_INT_MASK_5) {
  359. struct clockframe cf;
  360. /* call the common MIPS3 clock interrupt handler */
  361. cf.pc = pc;
  362. cf.sr = status;
  363. cf.intr = (curcpu()->ci_idepth > 1);
  364. mips3_clockintr(&cf);
  365. }
  366. if (__predict_false(pending & MIPS_INT_MASK_0)) {
  367. /* GT64x11 timer0 */
  368. volatile uint32_t *irq_src =
  369. (uint32_t *)MIPS_PHYS_TO_KSEG1(GT_BASE + GT_INTR_CAUSE);
  370. if (__predict_true((*irq_src & T0EXP) != 0)) {
  371. /* GT64x11 timer is no longer used for hardclock(9) */
  372. *irq_src = 0;
  373. }
  374. }
  375. if (pending & MIPS_INT_MASK_3) {
  376. /* 16650 serial */
  377. intr_handle(&cpu_intrtab[3]);
  378. }
  379. if (pending & MIPS_INT_MASK_1) {
  380. /* tulip primary */
  381. intr_handle(&cpu_intrtab[1]);
  382. }
  383. if (pending & MIPS_INT_MASK_2) {
  384. /* tulip secondary */
  385. intr_handle(&cpu_intrtab[2]);
  386. }
  387. if (pending & MIPS_INT_MASK_4) {
  388. /* ICU interrupts */
  389. intr_handle(&cpu_intrtab[4]);
  390. }
  391. (void)splhigh();
  392. }
  393. }