2 * Copyright (c) 2006 Oleksandr Tymoshenko
3 * Copyright (c) 2002-2004 Juli Mallett <jmallett@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/interrupt.h>
39 #include <sys/pmckern.h>
41 #include <machine/clock.h>
42 #include <machine/cpu.h>
43 #include <machine/cpufunc.h>
44 #include <machine/cpuinfo.h>
45 #include <machine/cpuregs.h>
46 #include <machine/frame.h>
47 #include <machine/intr_machdep.h>
48 #include <machine/md_var.h>
49 #include <machine/trap.h>
51 static struct intr_event *hardintr_events[NHARD_IRQS];
52 static struct intr_event *softintr_events[NSOFT_IRQS];
53 static mips_intrcnt_t mips_intr_counters[NSOFT_IRQS + NHARD_IRQS];
55 static int intrcnt_index;
57 static cpu_intr_mask_t hardintr_mask_func;
58 static cpu_intr_unmask_t hardintr_unmask_func;
61 mips_intrcnt_create(const char* name)
63 mips_intrcnt_t counter = &intrcnt[intrcnt_index++];
65 mips_intrcnt_setname(counter, name);
70 mips_intrcnt_setname(mips_intrcnt_t counter, const char *name)
72 int idx = counter - intrcnt;
74 KASSERT(counter != NULL, ("mips_intrcnt_setname: NULL counter"));
76 snprintf(intrnames + (MAXCOMLEN + 1) * idx,
77 MAXCOMLEN + 1, "%-*s", MAXCOMLEN, name);
81 mips_mask_hard_irq(void *source)
83 uintptr_t irq = (uintptr_t)source;
85 mips_wr_status(mips_rd_status() & ~(((1 << irq) << 8) << 2));
89 mips_unmask_hard_irq(void *source)
91 uintptr_t irq = (uintptr_t)source;
93 mips_wr_status(mips_rd_status() | (((1 << irq) << 8) << 2));
97 mips_mask_soft_irq(void *source)
99 uintptr_t irq = (uintptr_t)source;
101 mips_wr_status(mips_rd_status() & ~((1 << irq) << 8));
105 mips_unmask_soft_irq(void *source)
107 uintptr_t irq = (uintptr_t)source;
109 mips_wr_status(mips_rd_status() | ((1 << irq) << 8));
113 * Perform initialization of interrupts prior to setting
117 cpu_init_interrupts()
120 char name[MAXCOMLEN + 1];
123 * Initialize all available vectors so spare IRQ
124 * would show up in systat output
126 for (i = 0; i < NSOFT_IRQS; i++) {
127 snprintf(name, MAXCOMLEN + 1, "sint%d:", i);
128 mips_intr_counters[i] = mips_intrcnt_create(name);
131 for (i = 0; i < NHARD_IRQS; i++) {
132 snprintf(name, MAXCOMLEN + 1, "int%d:", i);
133 mips_intr_counters[NSOFT_IRQS + i] = mips_intrcnt_create(name);
138 cpu_set_hardintr_mask_func(cpu_intr_mask_t func)
141 hardintr_mask_func = func;
145 cpu_set_hardintr_unmask_func(cpu_intr_unmask_t func)
148 hardintr_unmask_func = func;
152 cpu_establish_hardintr(const char *name, driver_filter_t *filt,
153 void (*handler)(void*), void *arg, int irq, int flags, void **cookiep)
155 struct intr_event *event;
159 * We have 6 levels, but thats 0 - 5 (not including 6)
161 if (irq < 0 || irq >= NHARD_IRQS)
162 panic("%s called for unknown hard intr %d", __func__, irq);
164 if (hardintr_mask_func == NULL)
165 hardintr_mask_func = mips_mask_hard_irq;
167 if (hardintr_unmask_func == NULL)
168 hardintr_unmask_func = mips_unmask_hard_irq;
170 event = hardintr_events[irq];
172 error = intr_event_create(&event, (void *)(uintptr_t)irq, 0,
173 irq, hardintr_mask_func, hardintr_unmask_func,
174 NULL, NULL, "int%d", irq);
177 hardintr_events[irq] = event;
178 mips_unmask_hard_irq((void*)(uintptr_t)irq);
181 intr_event_add_handler(event, name, filt, handler, arg,
182 intr_priority(flags), flags, cookiep);
184 mips_intrcnt_setname(mips_intr_counters[NSOFT_IRQS + irq],
189 cpu_establish_softintr(const char *name, driver_filter_t *filt,
190 void (*handler)(void*), void *arg, int irq, int flags,
193 struct intr_event *event;
197 printf("Establish SOFT IRQ %d: filt %p handler %p arg %p\n",
198 irq, filt, handler, arg);
200 if (irq < 0 || irq > NSOFT_IRQS)
201 panic("%s called for unknown hard intr %d", __func__, irq);
203 event = softintr_events[irq];
205 error = intr_event_create(&event, (void *)(uintptr_t)irq, 0,
206 irq, mips_mask_soft_irq, mips_unmask_soft_irq,
207 NULL, NULL, "sint%d:", irq);
210 softintr_events[irq] = event;
211 mips_unmask_soft_irq((void*)(uintptr_t)irq);
214 intr_event_add_handler(event, name, filt, handler, arg,
215 intr_priority(flags), flags, cookiep);
217 mips_intrcnt_setname(mips_intr_counters[irq], event->ie_fullname);
221 cpu_intr(struct trapframe *tf)
223 struct intr_event *event;
224 register_t cause, status;
229 cause = mips_rd_cause();
230 status = mips_rd_status();
231 intr = (cause & MIPS_INT_MASK) >> 8;
233 * Do not handle masked interrupts. They were masked by
234 * pre_ithread function (mips_mask_XXX_intr) and will be
235 * unmasked once ithread is through with handler
237 intr &= (status & MIPS_INT_MASK) >> 8;
238 while ((i = fls(intr)) != 0) {
239 intr &= ~(1 << (i - 1));
242 /* Software interrupt. */
243 i--; /* Get a 0-offset interrupt. */
245 event = softintr_events[i];
246 mips_intrcnt_inc(mips_intr_counters[i]);
249 /* Hardware interrupt. */
250 i -= 2; /* Trim software interrupt bits. */
251 i--; /* Get a 0-offset interrupt. */
253 event = hardintr_events[i];
254 mips_intrcnt_inc(mips_intr_counters[NSOFT_IRQS + i]);
258 if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
259 printf("stray %s interrupt %d\n",
260 hard ? "hard" : "soft", i);
264 if (intr_event_handle(event, tf) != 0) {
265 printf("stray %s interrupt %d\n",
266 hard ? "hard" : "soft", i);
270 KASSERT(i == 0, ("all interrupts handled"));
275 if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
276 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);