2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Oleksandr Tymoshenko
5 * Copyright (c) 2002-2004 Juli Mallett <jmallett@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_hwpmc_hooks.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/interrupt.h>
41 #include <sys/pmckern.h>
43 #include <machine/clock.h>
44 #include <machine/cpu.h>
45 #include <machine/cpufunc.h>
46 #include <machine/cpuinfo.h>
47 #include <machine/cpuregs.h>
48 #include <machine/frame.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/md_var.h>
51 #include <machine/trap.h>
53 static struct intr_event *hardintr_events[NHARD_IRQS];
54 static struct intr_event *softintr_events[NSOFT_IRQS];
55 static mips_intrcnt_t mips_intr_counters[NSOFT_IRQS + NHARD_IRQS];
57 static int intrcnt_index;
59 static cpu_intr_mask_t hardintr_mask_func;
60 static cpu_intr_unmask_t hardintr_unmask_func;
63 mips_intrcnt_create(const char* name)
65 mips_intrcnt_t counter = &intrcnt[intrcnt_index++];
67 mips_intrcnt_setname(counter, name);
72 mips_intrcnt_setname(mips_intrcnt_t counter, const char *name)
74 int idx = counter - intrcnt;
76 KASSERT(counter != NULL, ("mips_intrcnt_setname: NULL counter"));
78 snprintf(intrnames + (MAXCOMLEN + 1) * idx,
79 MAXCOMLEN + 1, "%-*s", MAXCOMLEN, name);
83 mips_mask_hard_irq(void *source)
85 uintptr_t irq = (uintptr_t)source;
87 mips_wr_status(mips_rd_status() & ~(((1 << irq) << 8) << 2));
91 mips_unmask_hard_irq(void *source)
93 uintptr_t irq = (uintptr_t)source;
95 mips_wr_status(mips_rd_status() | (((1 << irq) << 8) << 2));
99 mips_mask_soft_irq(void *source)
101 uintptr_t irq = (uintptr_t)source;
103 mips_wr_status(mips_rd_status() & ~((1 << irq) << 8));
107 mips_unmask_soft_irq(void *source)
109 uintptr_t irq = (uintptr_t)source;
111 mips_wr_status(mips_rd_status() | ((1 << irq) << 8));
115 * Perform initialization of interrupts prior to setting
119 cpu_init_interrupts()
122 char name[MAXCOMLEN + 1];
125 * Initialize all available vectors so spare IRQ
126 * would show up in systat output
128 for (i = 0; i < NSOFT_IRQS; i++) {
129 snprintf(name, MAXCOMLEN + 1, "sint%d:", i);
130 mips_intr_counters[i] = mips_intrcnt_create(name);
133 for (i = 0; i < NHARD_IRQS; i++) {
134 snprintf(name, MAXCOMLEN + 1, "int%d:", i);
135 mips_intr_counters[NSOFT_IRQS + i] = mips_intrcnt_create(name);
140 cpu_set_hardintr_mask_func(cpu_intr_mask_t func)
143 hardintr_mask_func = func;
147 cpu_set_hardintr_unmask_func(cpu_intr_unmask_t func)
150 hardintr_unmask_func = func;
154 cpu_establish_hardintr(const char *name, driver_filter_t *filt,
155 void (*handler)(void*), void *arg, int irq, int flags, void **cookiep)
157 struct intr_event *event;
161 * We have 6 levels, but thats 0 - 5 (not including 6)
163 if (irq < 0 || irq >= NHARD_IRQS)
164 panic("%s called for unknown hard intr %d", __func__, irq);
166 if (hardintr_mask_func == NULL)
167 hardintr_mask_func = mips_mask_hard_irq;
169 if (hardintr_unmask_func == NULL)
170 hardintr_unmask_func = mips_unmask_hard_irq;
172 event = hardintr_events[irq];
174 error = intr_event_create(&event, (void *)(uintptr_t)irq, 0,
175 irq, hardintr_mask_func, hardintr_unmask_func,
176 NULL, NULL, "int%d", irq);
179 hardintr_events[irq] = event;
180 mips_unmask_hard_irq((void*)(uintptr_t)irq);
183 intr_event_add_handler(event, name, filt, handler, arg,
184 intr_priority(flags), flags, cookiep);
186 mips_intrcnt_setname(mips_intr_counters[NSOFT_IRQS + irq],
191 cpu_establish_softintr(const char *name, driver_filter_t *filt,
192 void (*handler)(void*), void *arg, int irq, int flags,
195 struct intr_event *event;
199 printf("Establish SOFT IRQ %d: filt %p handler %p arg %p\n",
200 irq, filt, handler, arg);
202 if (irq < 0 || irq > NSOFT_IRQS)
203 panic("%s called for unknown hard intr %d", __func__, irq);
205 event = softintr_events[irq];
207 error = intr_event_create(&event, (void *)(uintptr_t)irq, 0,
208 irq, mips_mask_soft_irq, mips_unmask_soft_irq,
209 NULL, NULL, "sint%d:", irq);
212 softintr_events[irq] = event;
213 mips_unmask_soft_irq((void*)(uintptr_t)irq);
216 intr_event_add_handler(event, name, filt, handler, arg,
217 intr_priority(flags), flags, cookiep);
219 mips_intrcnt_setname(mips_intr_counters[irq], event->ie_fullname);
223 cpu_intr(struct trapframe *tf)
225 struct intr_event *event;
226 register_t cause, status;
231 cause = mips_rd_cause();
232 status = mips_rd_status();
233 intr = (cause & MIPS_INT_MASK) >> 8;
235 * Do not handle masked interrupts. They were masked by
236 * pre_ithread function (mips_mask_XXX_intr) and will be
237 * unmasked once ithread is through with handler
239 intr &= (status & MIPS_INT_MASK) >> 8;
240 while ((i = fls(intr)) != 0) {
241 intr &= ~(1 << (i - 1));
244 /* Software interrupt. */
245 i--; /* Get a 0-offset interrupt. */
247 event = softintr_events[i];
248 mips_intrcnt_inc(mips_intr_counters[i]);
251 /* Hardware interrupt. */
252 i -= 2; /* Trim software interrupt bits. */
253 i--; /* Get a 0-offset interrupt. */
255 event = hardintr_events[i];
256 mips_intrcnt_inc(mips_intr_counters[NSOFT_IRQS + i]);
260 if (!event || CK_SLIST_EMPTY(&event->ie_handlers)) {
261 printf("stray %s interrupt %d\n",
262 hard ? "hard" : "soft", i);
266 if (intr_event_handle(event, tf) != 0) {
267 printf("stray %s interrupt %d\n",
268 hard ? "hard" : "soft", i);
272 KASSERT(i == 0, ("all interrupts handled"));
277 if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
278 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);