2 * Copyright (c) 2010-2011 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "opt_xtrace.h"
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/vmmeter.h>
39 #include <sys/interrupt.h>
40 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/sched.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
49 #include <machine/cpu.h>
50 #include <machine/fpu.h>
51 #include <machine/frame.h>
52 #include <machine/intr.h>
53 #include <machine/intrcnt.h>
54 #include <machine/md_var.h>
55 #include <machine/pcb.h>
56 #include <machine/reg.h>
57 #include <machine/smp.h>
64 struct intr_event *event; /* interrupt event */
65 volatile long *cntp; /* interrupt counter */
70 ia64_ihtype *ia64_handler[IA64_NXIVS];
72 static enum ia64_xiv_use ia64_xiv[IA64_NXIVS];
73 static struct ia64_intr *ia64_intrs[IA64_NXIVS];
75 static ia64_ihtype ia64_ih_invalid;
76 static ia64_ihtype ia64_ih_irq;
83 for (xiv = 0; xiv < IA64_NXIVS; xiv++) {
84 ia64_handler[xiv] = ia64_ih_invalid;
85 ia64_xiv[xiv] = IA64_XIV_FREE;
86 ia64_intrs[xiv] = NULL;
88 (void)ia64_xiv_reserve(15, IA64_XIV_ARCH, NULL);
92 ia64_xiv_free(u_int xiv, enum ia64_xiv_use what)
95 if (xiv >= IA64_NXIVS)
97 if (what == IA64_XIV_FREE || what == IA64_XIV_ARCH)
99 if (ia64_xiv[xiv] != what)
101 ia64_xiv[xiv] = IA64_XIV_FREE;
102 ia64_handler[xiv] = ia64_ih_invalid;
107 ia64_xiv_reserve(u_int xiv, enum ia64_xiv_use what, ia64_ihtype ih)
110 if (xiv >= IA64_NXIVS)
112 if (what == IA64_XIV_FREE)
114 if (ia64_xiv[xiv] != IA64_XIV_FREE)
116 ia64_xiv[xiv] = what;
117 ia64_handler[xiv] = (ih == NULL) ? ia64_ih_invalid: ih;
119 printf("XIV %u: use=%u, IH=%p\n", xiv, what, ih);
124 ia64_xiv_alloc(u_int prio, enum ia64_xiv_use what, ia64_ihtype ih)
130 if (hwprio > IA64_MAX_HWPRIO)
131 hwprio = IA64_MAX_HWPRIO;
133 xiv0 = IA64_NXIVS - (hwprio + 1) * 16;
135 KASSERT(xiv0 >= IA64_MIN_XIV, ("%s: min XIV", __func__));
136 KASSERT(xiv0 < IA64_NXIVS, ("%s: max XIV", __func__));
139 while (xiv < IA64_NXIVS && ia64_xiv_reserve(xiv, what, ih))
142 if (xiv < IA64_NXIVS)
146 while (xiv >= IA64_MIN_XIV && ia64_xiv_reserve(xiv, what, ih))
149 return ((xiv >= IA64_MIN_XIV) ? xiv : 0);
153 ia64_intr_eoi(void *arg)
155 u_int xiv = (uintptr_t)arg;
159 KASSERT(i != NULL, ("%s", __func__));
160 sapic_eoi(i->sapic, xiv);
164 ia64_intr_mask(void *arg)
166 u_int xiv = (uintptr_t)arg;
170 KASSERT(i != NULL, ("%s", __func__));
171 sapic_mask(i->sapic, i->irq);
172 sapic_eoi(i->sapic, xiv);
176 ia64_intr_unmask(void *arg)
178 u_int xiv = (uintptr_t)arg;
182 KASSERT(i != NULL, ("%s", __func__));
183 sapic_unmask(i->sapic, i->irq);
187 ia64_setup_intr(const char *name, int irq, driver_filter_t filter,
188 driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
196 prio = intr_priority(flags);
197 if (prio > PRI_MAX_ITHD)
202 /* Get the I/O SAPIC and XIV that corresponds to the IRQ. */
203 sa = sapic_lookup(irq, &xiv);
211 i = malloc(sizeof(struct ia64_intr), M_DEVBUF,
214 sa = sapic_lookup(irq, &xiv);
215 KASSERT(sa != NULL, ("sapic_lookup"));
221 * If the IRQ has no XIV assigned to it yet, assign one based
225 xiv = ia64_xiv_alloc(prio, IA64_XIV_IRQ, ia64_ih_irq);
232 error = intr_event_create(&i->event, (void *)(uintptr_t)xiv,
233 0, irq, ia64_intr_mask, ia64_intr_unmask, ia64_intr_eoi,
234 NULL, "irq%u:", irq);
236 ia64_xiv_free(xiv, IA64_XIV_IRQ);
244 i->cntp = intrcnt + xiv;
249 sapic_enable(sa, irq, xiv);
251 if (name != NULL && *name != '\0') {
252 /* XXX needs abstraction. Too error prone. */
253 intrname = intrnames + xiv * INTRNAME_LEN;
254 memset(intrname, ' ', INTRNAME_LEN - 1);
255 bcopy(name, intrname, strlen(name));
262 KASSERT(i != NULL, ("XIV mapping bug"));
264 error = intr_event_add_handler(i->event, name, filter, handler, arg,
265 prio, flags, cookiep);
270 ia64_teardown_intr(void *cookie)
273 return (intr_event_remove_handler(cookie));
285 for (xiv = IA64_NXIVS - 1; xiv >= IA64_MIN_XIV; xiv--) {
286 if (ia64_xiv[xiv] != IA64_XIV_IRQ)
290 cpu = (cpu == 0) ? MAXCPU - 1 : cpu - 1;
291 pc = cpuid_to_pcpu[cpu];
292 } while (pc == NULL || !pc->pc_md.awake);
293 sapic_bind_intr(i->irq, pc);
298 * Interrupt handlers.
302 ia64_handle_intr(struct trapframe *tf)
308 ia64_set_fpsr(IA64_FPSR_DEFAULT);
314 PCPU_INC(cnt.v_intr);
316 xiv = ia64_get_ivr();
319 PCPU_INC(md.stats.pcs_nstrays);
325 CTR3(KTR_INTR, "INTR: XIV=%u, #%u: frame=%p", xiv,
326 PCPU_GET(cnt.v_intr), tf);
327 if (!(ia64_handler[xiv])(td, xiv, tf)) {
331 xiv = ia64_get_ivr();
337 if (TRAPF_USERMODE(tf)) {
338 while (td->td_flags & (TDF_ASTPENDING|TDF_NEEDRESCHED)) {
347 ia64_ih_invalid(struct thread *td, u_int xiv, struct trapframe *tf)
350 panic("invalid XIV: %u", xiv);
355 ia64_ih_irq(struct thread *td, u_int xiv, struct trapframe *tf)
358 struct intr_event *ie; /* our interrupt event */
360 PCPU_INC(md.stats.pcs_nhwints);
362 /* Find the interrupt thread for this XIV. */
364 KASSERT(i != NULL, ("%s: unassigned XIV", __func__));
369 KASSERT(ie != NULL, ("%s: interrupt without event", __func__));
371 if (intr_event_handle(ie, tf) != 0) {
372 ia64_intr_mask((void *)(uintptr_t)xiv);
373 log(LOG_ERR, "stray irq%u\n", i->irq);
382 db_print_xiv(u_int xiv, int always)
388 db_printf("XIV %u (%p): ", xiv, i);
389 sapic_print(i->sapic, i->irq);
391 db_printf("XIV %u: unassigned\n", xiv);
394 DB_SHOW_COMMAND(xiv, db_show_xiv)
399 xiv = ((addr >> 4) % 16) * 10 + (addr % 16);
400 if (xiv >= IA64_NXIVS)
401 db_printf("error: XIV %u not in range [0..%u]\n",
402 xiv, IA64_NXIVS - 1);
404 db_print_xiv(xiv, 1);
406 for (xiv = 0; xiv < IA64_NXIVS; xiv++)
407 db_print_xiv(xiv, 0);