2 /* $NetBSD: interrupt.c,v 1.23 1998/02/24 07:38:01 thorpej Exp $ */
5 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
8 * Authors: Keith Bostic, Chris G. Demetriou
10 * Permission to use, copy, modify and distribute this software and
11 * its documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 * Carnegie Mellon requests users of this software to return to
22 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
23 * School of Computer Science
24 * Carnegie Mellon University
25 * Pittsburgh PA 15213-3890
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
31 * Additional Copyright (c) 1997 by Matthew Jacob for NASA/Ames Research Center.
32 * Redistribute and modify at will, leaving only this additional copyright
38 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/vmmeter.h>
46 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/sched.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
55 #include <machine/clock.h>
56 #include <machine/cpu.h>
57 #include <machine/fpu.h>
58 #include <machine/frame.h>
59 #include <machine/intr.h>
60 #include <machine/md_var.h>
61 #include <machine/pcb.h>
62 #include <machine/reg.h>
63 #include <machine/sapicvar.h>
64 #include <machine/smp.h>
67 struct evcnt clock_intr_evcnt; /* event counter for clock intrs. */
69 #include <sys/interrupt.h>
70 #include <machine/intrcnt.h>
78 extern int mp_ipi_test;
81 static void ia64_dispatch_intr(void *, u_int);
84 dummy_perf(unsigned long vector, struct trapframe *tf)
86 printf("performance interrupt!\n");
89 void (*perf_irq)(unsigned long, struct trapframe *) = dummy_perf;
91 static unsigned int ints[MAXCPU];
92 SYSCTL_OPAQUE(_debug, OID_AUTO, ints, CTLFLAG_RW, &ints, sizeof(ints), "IU",
95 static unsigned int clks[MAXCPU];
97 SYSCTL_OPAQUE(_debug, OID_AUTO, clks, CTLFLAG_RW, &clks, sizeof(clks), "IU",
100 SYSCTL_INT(_debug, OID_AUTO, clks, CTLFLAG_RW, clks, 0, "");
104 static unsigned int asts[MAXCPU];
105 SYSCTL_OPAQUE(_debug, OID_AUTO, asts, CTLFLAG_RW, &asts, sizeof(asts), "IU",
108 static unsigned int rdvs[MAXCPU];
109 SYSCTL_OPAQUE(_debug, OID_AUTO, rdvs, CTLFLAG_RW, &rdvs, sizeof(rdvs), "IU",
113 SYSCTL_NODE(_debug, OID_AUTO, clock, CTLFLAG_RW, 0, "clock statistics");
115 static int adjust_edges = 0;
116 SYSCTL_INT(_debug_clock, OID_AUTO, adjust_edges, CTLFLAG_RD,
117 &adjust_edges, 0, "Number of times ITC got more than 12.5% behind");
119 static int adjust_excess = 0;
120 SYSCTL_INT(_debug_clock, OID_AUTO, adjust_excess, CTLFLAG_RD,
121 &adjust_excess, 0, "Total number of ignored ITC interrupts");
123 static int adjust_lost = 0;
124 SYSCTL_INT(_debug_clock, OID_AUTO, adjust_lost, CTLFLAG_RD,
125 &adjust_lost, 0, "Total number of lost ITC interrupts");
127 static int adjust_ticks = 0;
128 SYSCTL_INT(_debug_clock, OID_AUTO, adjust_ticks, CTLFLAG_RD,
129 &adjust_ticks, 0, "Total number of ITC interrupts with adjustment");
132 interrupt(struct trapframe *tf)
135 volatile struct ia64_interrupt_block *ib = IA64_INTERRUPT_BLOCK;
136 uint64_t adj, clk, itc;
141 ia64_set_fpsr(IA64_FPSR_DEFAULT);
144 atomic_add_int(&td->td_intr_nesting_level, 1);
146 vector = tf->tf_special.ifa;
150 * Handle ExtINT interrupts by generating an INTA cycle to
155 printf("ExtINT interrupt: vector=%u\n", (int)inta);
157 __asm __volatile("mov cr.eoi = r0;; srlz.d");
161 } else if (vector == 15)
164 if (vector == CLOCK_VECTOR) {/* clock interrupt */
165 /* CTR0(KTR_INTR, "clock interrupt"); */
167 itc = ia64_get_itc();
169 PCPU_INC(cnt.v_intr);
170 #ifdef EVCNT_COUNTERS
171 clock_intr_evcnt.ev_count++;
173 intrcnt[INTRCNT_CLOCK]++;
175 clks[PCPU_GET(cpuid)]++;
179 adj = PCPU_GET(clockadj);
180 clk = PCPU_GET(clock);
183 while (delta >= ia64_clock_reload) {
184 /* Only the BSP runs the real clock */
185 if (PCPU_GET(cpuid) == 0)
186 hardclock(TRAPF_USERMODE(tf), TRAPF_PC(tf));
188 hardclock_cpu(TRAPF_USERMODE(tf));
190 profclock(TRAPF_USERMODE(tf), TRAPF_PC(tf));
191 statclock(TRAPF_USERMODE(tf));
192 delta -= ia64_clock_reload;
193 clk += ia64_clock_reload;
198 ia64_set_itm(itc + ia64_clock_reload - adj);
200 adjust_lost += count - 1;
201 if (delta > (ia64_clock_reload >> 3)) {
204 adj = ia64_clock_reload >> 4;
211 PCPU_SET(clock, clk);
212 PCPU_SET(clockadj, adj);
217 } else if (vector == ipi_vector[IPI_AST]) {
218 asts[PCPU_GET(cpuid)]++;
219 CTR1(KTR_SMP, "IPI_AST, cpuid=%d", PCPU_GET(cpuid));
220 } else if (vector == ipi_vector[IPI_HIGH_FP]) {
221 struct thread *thr = PCPU_GET(fpcurthread);
223 mtx_lock_spin(&thr->td_md.md_highfp_mtx);
224 save_high_fp(&thr->td_pcb->pcb_high_fp);
225 thr->td_pcb->pcb_fpcpu = NULL;
226 PCPU_SET(fpcurthread, NULL);
227 mtx_unlock_spin(&thr->td_md.md_highfp_mtx);
229 } else if (vector == ipi_vector[IPI_RENDEZVOUS]) {
230 rdvs[PCPU_GET(cpuid)]++;
231 CTR1(KTR_SMP, "IPI_RENDEZVOUS, cpuid=%d", PCPU_GET(cpuid));
232 smp_rendezvous_action();
233 } else if (vector == ipi_vector[IPI_STOP]) {
234 cpumask_t mybit = PCPU_GET(cpumask);
236 savectx(PCPU_PTR(pcb));
237 atomic_set_int(&stopped_cpus, mybit);
238 while ((started_cpus & mybit) == 0)
240 atomic_clear_int(&started_cpus, mybit);
241 atomic_clear_int(&stopped_cpus, mybit);
242 } else if (vector == ipi_vector[IPI_TEST]) {
243 CTR1(KTR_SMP, "IPI_TEST, cpuid=%d", PCPU_GET(cpuid));
245 } else if (vector == ipi_vector[IPI_PREEMPT]) {
246 CTR1(KTR_SMP, "IPI_PREEMPT, cpuid=%d", PCPU_GET(cpuid));
247 sched_preempt(curthread);
250 ints[PCPU_GET(cpuid)]++;
251 ia64_dispatch_intr(tf, vector);
254 __asm __volatile("mov cr.eoi = r0;; srlz.d");
255 vector = ia64_get_ivr();
260 atomic_subtract_int(&td->td_intr_nesting_level, 1);
262 if (TRAPF_USERMODE(tf)) {
265 mtx_assert(&Giant, MA_NOTOWNED);
271 * Hardware irqs have vectors starting at this offset.
273 #define IA64_HARDWARE_IRQ_BASE 0x20
276 struct intr_event *event; /* interrupt event */
277 volatile long *cntp; /* interrupt counter */
282 static struct ia64_intr *ia64_intrs[256];
285 ia64_intr_eoi(void *arg)
287 u_int vector = (uintptr_t)arg;
290 i = ia64_intrs[vector];
292 sapic_eoi(i->sapic, vector);
296 ia64_intr_mask(void *arg)
298 u_int vector = (uintptr_t)arg;
301 i = ia64_intrs[vector];
303 sapic_mask(i->sapic, i->irq);
304 sapic_eoi(i->sapic, vector);
309 ia64_intr_unmask(void *arg)
311 u_int vector = (uintptr_t)arg;
314 i = ia64_intrs[vector];
316 sapic_unmask(i->sapic, i->irq);
320 ia64_setup_intr(const char *name, int irq, driver_filter_t filter,
321 driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
329 /* Get the I/O SAPIC that corresponds to the IRQ. */
330 sa = sapic_lookup(irq);
335 * XXX - There's a priority implied by the choice of vector.
336 * We should therefore relate the vector to the interrupt type.
338 vector = irq + IA64_HARDWARE_IRQ_BASE;
340 i = ia64_intrs[vector];
342 i = malloc(sizeof(struct ia64_intr), M_DEVBUF, M_NOWAIT);
346 error = intr_event_create(&i->event, (void *)(uintptr_t)vector,
347 0, irq, ia64_intr_mask, ia64_intr_unmask, ia64_intr_eoi,
348 NULL, "irq%u:", irq);
354 if (!atomic_cmpset_ptr(&ia64_intrs[vector], NULL, i)) {
355 intr_event_destroy(i->event);
357 i = ia64_intrs[vector];
362 i->cntp = intrcnt + irq + INTRCNT_ISA_IRQ;
363 if (name != NULL && *name != '\0') {
364 /* XXX needs abstraction. Too error prone. */
365 intrname = intrnames +
366 (irq + INTRCNT_ISA_IRQ) * INTRNAME_LEN;
367 memset(intrname, ' ', INTRNAME_LEN - 1);
368 bcopy(name, intrname, strlen(name));
371 sapic_enable(i->sapic, irq, vector);
375 error = intr_event_add_handler(i->event, name, filter, handler, arg,
376 intr_priority(flags), flags, cookiep);
381 ia64_teardown_intr(void *cookie)
384 return (intr_event_remove_handler(cookie));
388 ia64_dispatch_intr(void *frame, u_int vector)
391 struct intr_event *ie; /* our interrupt event */
394 * Find the interrupt thread for this vector.
396 i = ia64_intrs[vector];
397 KASSERT(i != NULL, ("%s: unassigned vector", __func__));
402 KASSERT(ie != NULL, ("%s: interrupt without event", __func__));
404 if (intr_event_handle(ie, frame) != 0) {
406 * XXX: The pre-INTR_FILTER code didn't mask stray
409 ia64_intr_mask((void *)(uintptr_t)vector);
410 log(LOG_ERR, "stray irq%u\n", i->irq);
417 db_print_vector(u_int vector, int always)
421 i = ia64_intrs[vector];
423 db_printf("vector %u (%p): ", vector, i);
424 sapic_print(i->sapic, i->irq);
426 db_printf("vector %u: unassigned\n", vector);
429 DB_SHOW_COMMAND(vector, db_show_vector)
434 vector = ((addr >> 4) % 16) * 10 + (addr % 16);
436 db_printf("error: vector %u not in range [0..255]\n",
439 db_print_vector(vector, 1);
441 for (vector = 0; vector < 256; vector++)
442 db_print_vector(vector, 0);