1129198Scognet/* $NetBSD: intr.c,v 1.12 2003/07/15 00:24:41 lukem Exp $ */ 2129198Scognet 3139735Simp/*- 4129198Scognet * Copyright (c) 2004 Olivier Houchard. 5129198Scognet * Copyright (c) 1994-1998 Mark Brinicombe. 6129198Scognet * All rights reserved. 7129198Scognet * 8129198Scognet * Redistribution and use in source and binary forms, with or without 9129198Scognet * modification, are permitted provided that the following conditions 10129198Scognet * are met: 11129198Scognet * 1. Redistributions of source code must retain the above copyright 12129198Scognet * notice, this list of conditions and the following disclaimer. 13129198Scognet * 2. Redistributions in binary form must reproduce the above copyright 14129198Scognet * notice, this list of conditions and the following disclaimer in the 15129198Scognet * documentation and/or other materials provided with the distribution. 16129198Scognet * 3. All advertising materials mentioning features or use of this software 17129198Scognet * must display the following acknowledgement: 18129198Scognet * This product includes software developed by Mark Brinicombe 19129198Scognet * for the NetBSD Project. 20129198Scognet * 4. The name of the company nor the name of the author may be used to 21129198Scognet * endorse or promote products derived from this software without specific 22129198Scognet * prior written permission. 23129198Scognet * 24129198Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25129198Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26129198Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27129198Scognet * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28129198Scognet * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29129198Scognet * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30129198Scognet * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34129198Scognet * SUCH DAMAGE. 35129198Scognet * 36129198Scognet * Soft interrupt and other generic interrupt functions. 37129198Scognet */ 38129198Scognet 39276032Sandrew#include "opt_platform.h" 40277835Sbr#include "opt_hwpmc_hooks.h" 41276032Sandrew 42129198Scognet#include <sys/cdefs.h> 43129198Scognet__FBSDID("$FreeBSD: stable/11/sys/arm/arm/intr.c 331722 2018-03-29 02:50:57Z eadler $"); 44276032Sandrew 45129198Scognet#include <sys/param.h> 46129198Scognet#include <sys/systm.h> 47236991Simp#include <sys/syslog.h> 48269646Sian#include <sys/kernel.h> 49129198Scognet#include <sys/malloc.h> 50135650Scognet#include <sys/proc.h> 51129198Scognet#include <sys/bus.h> 52129198Scognet#include <sys/interrupt.h> 53129198Scognet#include <sys/conf.h> 54277835Sbr#include <sys/pmc.h> 55277835Sbr#include <sys/pmckern.h> 56331017Skevans#include <sys/vmmeter.h> 57276032Sandrew 58129198Scognet#include <machine/atomic.h> 59281089Sandrew#include <machine/bus.h> 60129198Scognet#include <machine/intr.h> 61129198Scognet#include <machine/cpu.h> 62129198Scognet 63276032Sandrew#ifdef FDT 64276032Sandrew#include <dev/fdt/fdt_common.h> 65276032Sandrew#include <machine/fdt.h> 66276032Sandrew#endif 67276032Sandrew 68245637Sian#define INTRNAME_LEN (MAXCOMLEN + 1) 69245637Sian 70177325Sjhbtypedef void (*mask_fn)(void *); 71177325Sjhb 72151658Sjhbstatic struct intr_event *intr_events[NIRQ]; 73129198Scognet 74292426Sadrianvoid intr_irq_handler(struct trapframe *); 75135650Scognet 76178366Scognetvoid (*arm_post_filter)(void *) = NULL; 77260161Szbbint (*arm_config_irq)(int irq, enum intr_trigger trig, 78260161Szbb enum intr_polarity pol) = NULL; 79178366Scognet 80262979Sian/* Data for statistics reporting. */ 81262979Sianu_long intrcnt[NIRQ]; 82282829Slooschar intrnames[(NIRQ * INTRNAME_LEN) + 1]; 83262979Siansize_t sintrcnt = sizeof(intrcnt); 84262979Siansize_t sintrnames = sizeof(intrnames); 85262979Sian 86245637Sian/* 87245637Sian * Pre-format intrnames into an array of fixed-size strings containing spaces. 88245637Sian * This allows us to avoid the need for an intermediate table of indices into 89245637Sian * the names and counts arrays, while still meeting the requirements and 90245637Sian * assumptions of vmstat(8) and the kdb "show intrcnt" command, the two 91245637Sian * consumers of this data. 92245637Sian */ 93269646Sianstatic void 94269646Sianintr_init(void *unused) 95245637Sian{ 96245637Sian int i; 97245637Sian 98246000Sian for (i = 0; i < NIRQ; ++i) { 99245948Sian snprintf(&intrnames[i * INTRNAME_LEN], INTRNAME_LEN, "%-*s", 100245948Sian INTRNAME_LEN - 1, ""); 101246000Sian } 102245637Sian} 103245637Sian 104269646SianSYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL); 105269646Sian 106276032Sandrew#ifdef FDT 107276032Sandrewint 108292426Sadrianintr_fdt_map_irq(phandle_t iparent, pcell_t *intr, int icells) 109276032Sandrew{ 110276032Sandrew fdt_pic_decode_t intr_decode; 111276032Sandrew phandle_t intr_parent; 112276032Sandrew int i, rv, interrupt, trig, pol; 113276032Sandrew 114276032Sandrew intr_parent = OF_node_from_xref(iparent); 115276032Sandrew for (i = 0; i < icells; i++) 116276032Sandrew intr[i] = cpu_to_fdt32(intr[i]); 117276032Sandrew 118276032Sandrew for (i = 0; fdt_pic_table[i] != NULL; i++) { 119276032Sandrew intr_decode = fdt_pic_table[i]; 120276032Sandrew rv = intr_decode(intr_parent, intr, &interrupt, &trig, &pol); 121276032Sandrew 122276032Sandrew if (rv == 0) { 123276032Sandrew /* This was recognized as our PIC and decoded. */ 124276032Sandrew interrupt = FDT_MAP_IRQ(intr_parent, interrupt); 125276032Sandrew return (interrupt); 126276032Sandrew } 127276032Sandrew } 128276032Sandrew 129276032Sandrew /* Not in table, so guess */ 130276032Sandrew interrupt = FDT_MAP_IRQ(intr_parent, fdt32_to_cpu(intr[0])); 131276032Sandrew 132276032Sandrew return (interrupt); 133276032Sandrew} 134276032Sandrew#endif 135276032Sandrew 136245637Sianvoid 137236991Simparm_setup_irqhandler(const char *name, driver_filter_t *filt, 138166901Spiso void (*hand)(void*), void *arg, int irq, int flags, void **cookiep) 139129198Scognet{ 140151658Sjhb struct intr_event *event; 141129198Scognet int error; 142129198Scognet 143129198Scognet if (irq < 0 || irq >= NIRQ) 144129198Scognet return; 145151658Sjhb event = intr_events[irq]; 146151658Sjhb if (event == NULL) { 147178092Sjeff error = intr_event_create(&event, (void *)irq, 0, irq, 148177325Sjhb (mask_fn)arm_mask_irq, (mask_fn)arm_unmask_irq, 149178366Scognet arm_post_filter, NULL, "intr%d:", irq); 150129198Scognet if (error) 151129198Scognet return; 152151658Sjhb intr_events[irq] = event; 153283366Sandrew snprintf(&intrnames[irq * INTRNAME_LEN], INTRNAME_LEN, 154246000Sian "irq%d: %-*s", irq, INTRNAME_LEN - 1, name); 155129198Scognet } 156166901Spiso intr_event_add_handler(event, name, filt, hand, arg, 157151658Sjhb intr_priority(flags), flags, cookiep); 158129198Scognet} 159129198Scognet 160147166Scognetint 161182933Srajarm_remove_irqhandler(int irq, void *cookie) 162147166Scognet{ 163182933Sraj struct intr_event *event; 164182933Sraj int error; 165182933Sraj 166182933Sraj event = intr_events[irq]; 167182933Sraj arm_mask_irq(irq); 168283366Sandrew 169182933Sraj error = intr_event_remove_handler(cookie); 170182933Sraj 171182933Sraj if (!TAILQ_EMPTY(&event->ie_handlers)) 172182933Sraj arm_unmask_irq(irq); 173182933Sraj return (error); 174147166Scognet} 175147166Scognet 176129198Scognetvoid dosoftints(void); 177129198Scognetvoid 178129198Scognetdosoftints(void) 179129198Scognet{ 180129198Scognet} 181129198Scognet 182129198Scognetvoid 183292426Sadrianintr_irq_handler(struct trapframe *frame) 184129198Scognet{ 185151658Sjhb struct intr_event *event; 186171616Scognet int i; 187129198Scognet 188170291Sattilio PCPU_INC(cnt.v_intr); 189193847Smarcel i = -1; 190193847Smarcel while ((i = arm_get_next_irq(i)) != -1) { 191245637Sian intrcnt[i]++; 192151658Sjhb event = intr_events[i]; 193177940Sjhb if (intr_event_handle(event, frame) != 0) { 194177940Sjhb /* XXX: Log stray IRQs */ 195171616Scognet arm_mask_irq(i); 196171616Scognet } 197135650Scognet } 198277835Sbr#ifdef HWPMC_HOOKS 199277835Sbr if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN)) 200277835Sbr pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, frame); 201277835Sbr#endif 202129198Scognet} 203