intr.c revision 331017
1129198Scognet/*	$NetBSD: intr.c,v 1.12 2003/07/15 00:24:41 lukem Exp $	*/
2129198Scognet
3139735Simp/*-
4330897Seadler * SPDX-License-Identifier: BSD-4-Clause
5330897Seadler *
6129198Scognet * Copyright (c) 2004 Olivier Houchard.
7129198Scognet * Copyright (c) 1994-1998 Mark Brinicombe.
8129198Scognet * All rights reserved.
9129198Scognet *
10129198Scognet * Redistribution and use in source and binary forms, with or without
11129198Scognet * modification, are permitted provided that the following conditions
12129198Scognet * are met:
13129198Scognet * 1. Redistributions of source code must retain the above copyright
14129198Scognet *    notice, this list of conditions and the following disclaimer.
15129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
16129198Scognet *    notice, this list of conditions and the following disclaimer in the
17129198Scognet *    documentation and/or other materials provided with the distribution.
18129198Scognet * 3. All advertising materials mentioning features or use of this software
19129198Scognet *    must display the following acknowledgement:
20129198Scognet *	This product includes software developed by Mark Brinicombe
21129198Scognet *	for the NetBSD Project.
22129198Scognet * 4. The name of the company nor the name of the author may be used to
23129198Scognet *    endorse or promote products derived from this software without specific
24129198Scognet *    prior written permission.
25129198Scognet *
26129198Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27129198Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28129198Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29129198Scognet * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30129198Scognet * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31129198Scognet * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32129198Scognet * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36129198Scognet * SUCH DAMAGE.
37129198Scognet *
38129198Scognet * Soft interrupt and other generic interrupt functions.
39129198Scognet */
40129198Scognet
41276032Sandrew#include "opt_platform.h"
42277835Sbr#include "opt_hwpmc_hooks.h"
43276032Sandrew
44129198Scognet#include <sys/cdefs.h>
45129198Scognet__FBSDID("$FreeBSD: stable/11/sys/arm/arm/intr.c 331017 2018-03-15 19:08:33Z kevans $");
46276032Sandrew
47129198Scognet#include <sys/param.h>
48129198Scognet#include <sys/systm.h>
49236991Simp#include <sys/syslog.h>
50269646Sian#include <sys/kernel.h>
51129198Scognet#include <sys/malloc.h>
52135650Scognet#include <sys/proc.h>
53129198Scognet#include <sys/bus.h>
54129198Scognet#include <sys/interrupt.h>
55129198Scognet#include <sys/conf.h>
56277835Sbr#include <sys/pmc.h>
57277835Sbr#include <sys/pmckern.h>
58331017Skevans#include <sys/vmmeter.h>
59276032Sandrew
60129198Scognet#include <machine/atomic.h>
61281089Sandrew#include <machine/bus.h>
62129198Scognet#include <machine/intr.h>
63129198Scognet#include <machine/cpu.h>
64129198Scognet
65276032Sandrew#ifdef FDT
66276032Sandrew#include <dev/fdt/fdt_common.h>
67276032Sandrew#include <machine/fdt.h>
68276032Sandrew#endif
69276032Sandrew
70245637Sian#define	INTRNAME_LEN	(MAXCOMLEN + 1)
71245637Sian
72177325Sjhbtypedef void (*mask_fn)(void *);
73177325Sjhb
74151658Sjhbstatic struct intr_event *intr_events[NIRQ];
75129198Scognet
76292426Sadrianvoid	intr_irq_handler(struct trapframe *);
77135650Scognet
78178366Scognetvoid (*arm_post_filter)(void *) = NULL;
79260161Szbbint (*arm_config_irq)(int irq, enum intr_trigger trig,
80260161Szbb    enum intr_polarity pol) = NULL;
81178366Scognet
82262979Sian/* Data for statistics reporting. */
83262979Sianu_long intrcnt[NIRQ];
84282829Slooschar intrnames[(NIRQ * INTRNAME_LEN) + 1];
85262979Siansize_t sintrcnt = sizeof(intrcnt);
86262979Siansize_t sintrnames = sizeof(intrnames);
87262979Sian
88245637Sian/*
89245637Sian * Pre-format intrnames into an array of fixed-size strings containing spaces.
90245637Sian * This allows us to avoid the need for an intermediate table of indices into
91245637Sian * the names and counts arrays, while still meeting the requirements and
92245637Sian * assumptions of vmstat(8) and the kdb "show intrcnt" command, the two
93245637Sian * consumers of this data.
94245637Sian */
95269646Sianstatic void
96269646Sianintr_init(void *unused)
97245637Sian{
98245637Sian	int i;
99245637Sian
100246000Sian	for (i = 0; i < NIRQ; ++i) {
101245948Sian		snprintf(&intrnames[i * INTRNAME_LEN], INTRNAME_LEN, "%-*s",
102245948Sian		    INTRNAME_LEN - 1, "");
103246000Sian	}
104245637Sian}
105245637Sian
106269646SianSYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
107269646Sian
108276032Sandrew#ifdef FDT
109276032Sandrewint
110292426Sadrianintr_fdt_map_irq(phandle_t iparent, pcell_t *intr, int icells)
111276032Sandrew{
112276032Sandrew	fdt_pic_decode_t intr_decode;
113276032Sandrew	phandle_t intr_parent;
114276032Sandrew	int i, rv, interrupt, trig, pol;
115276032Sandrew
116276032Sandrew	intr_parent = OF_node_from_xref(iparent);
117276032Sandrew	for (i = 0; i < icells; i++)
118276032Sandrew		intr[i] = cpu_to_fdt32(intr[i]);
119276032Sandrew
120276032Sandrew	for (i = 0; fdt_pic_table[i] != NULL; i++) {
121276032Sandrew		intr_decode = fdt_pic_table[i];
122276032Sandrew		rv = intr_decode(intr_parent, intr, &interrupt, &trig, &pol);
123276032Sandrew
124276032Sandrew		if (rv == 0) {
125276032Sandrew			/* This was recognized as our PIC and decoded. */
126276032Sandrew			interrupt = FDT_MAP_IRQ(intr_parent, interrupt);
127276032Sandrew			return (interrupt);
128276032Sandrew		}
129276032Sandrew	}
130276032Sandrew
131276032Sandrew	/* Not in table, so guess */
132276032Sandrew	interrupt = FDT_MAP_IRQ(intr_parent, fdt32_to_cpu(intr[0]));
133276032Sandrew
134276032Sandrew	return (interrupt);
135276032Sandrew}
136276032Sandrew#endif
137276032Sandrew
138245637Sianvoid
139236991Simparm_setup_irqhandler(const char *name, driver_filter_t *filt,
140166901Spiso    void (*hand)(void*), void *arg, int irq, int flags, void **cookiep)
141129198Scognet{
142151658Sjhb	struct intr_event *event;
143129198Scognet	int error;
144129198Scognet
145129198Scognet	if (irq < 0 || irq >= NIRQ)
146129198Scognet		return;
147151658Sjhb	event = intr_events[irq];
148151658Sjhb	if (event == NULL) {
149178092Sjeff		error = intr_event_create(&event, (void *)irq, 0, irq,
150177325Sjhb		    (mask_fn)arm_mask_irq, (mask_fn)arm_unmask_irq,
151178366Scognet		    arm_post_filter, NULL, "intr%d:", irq);
152129198Scognet		if (error)
153129198Scognet			return;
154151658Sjhb		intr_events[irq] = event;
155283366Sandrew		snprintf(&intrnames[irq * INTRNAME_LEN], INTRNAME_LEN,
156246000Sian		    "irq%d: %-*s", irq, INTRNAME_LEN - 1, name);
157129198Scognet	}
158166901Spiso	intr_event_add_handler(event, name, filt, hand, arg,
159151658Sjhb	    intr_priority(flags), flags, cookiep);
160129198Scognet}
161129198Scognet
162147166Scognetint
163182933Srajarm_remove_irqhandler(int irq, void *cookie)
164147166Scognet{
165182933Sraj	struct intr_event *event;
166182933Sraj	int error;
167182933Sraj
168182933Sraj	event = intr_events[irq];
169182933Sraj	arm_mask_irq(irq);
170283366Sandrew
171182933Sraj	error = intr_event_remove_handler(cookie);
172182933Sraj
173182933Sraj	if (!TAILQ_EMPTY(&event->ie_handlers))
174182933Sraj		arm_unmask_irq(irq);
175182933Sraj	return (error);
176147166Scognet}
177147166Scognet
178129198Scognetvoid dosoftints(void);
179129198Scognetvoid
180129198Scognetdosoftints(void)
181129198Scognet{
182129198Scognet}
183129198Scognet
184129198Scognetvoid
185292426Sadrianintr_irq_handler(struct trapframe *frame)
186129198Scognet{
187151658Sjhb	struct intr_event *event;
188171616Scognet	int i;
189129198Scognet
190170291Sattilio	PCPU_INC(cnt.v_intr);
191193847Smarcel	i = -1;
192193847Smarcel	while ((i = arm_get_next_irq(i)) != -1) {
193245637Sian		intrcnt[i]++;
194151658Sjhb		event = intr_events[i];
195177940Sjhb		if (intr_event_handle(event, frame) != 0) {
196177940Sjhb			/* XXX: Log stray IRQs */
197171616Scognet			arm_mask_irq(i);
198171616Scognet		}
199135650Scognet	}
200277835Sbr#ifdef HWPMC_HOOKS
201277835Sbr	if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
202277835Sbr		pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, frame);
203277835Sbr#endif
204129198Scognet}
205