1*0a6a1f1dSLionel Sambuc /* $NetBSD: intr.h,v 1.48 2015/08/17 06:16:02 knakahara Exp $ */
21cd76c75SBen Gras
31cd76c75SBen Gras /*-
41cd76c75SBen Gras * Copyright (c) 1998, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
51cd76c75SBen Gras * All rights reserved.
61cd76c75SBen Gras *
71cd76c75SBen Gras * This code is derived from software contributed to The NetBSD Foundation
81cd76c75SBen Gras * by Charles M. Hannum, and by Jason R. Thorpe.
91cd76c75SBen Gras *
101cd76c75SBen Gras * Redistribution and use in source and binary forms, with or without
111cd76c75SBen Gras * modification, are permitted provided that the following conditions
121cd76c75SBen Gras * are met:
131cd76c75SBen Gras * 1. Redistributions of source code must retain the above copyright
141cd76c75SBen Gras * notice, this list of conditions and the following disclaimer.
151cd76c75SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
161cd76c75SBen Gras * notice, this list of conditions and the following disclaimer in the
171cd76c75SBen Gras * documentation and/or other materials provided with the distribution.
181cd76c75SBen Gras *
191cd76c75SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201cd76c75SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211cd76c75SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221cd76c75SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231cd76c75SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241cd76c75SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251cd76c75SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261cd76c75SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271cd76c75SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281cd76c75SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291cd76c75SBen Gras * POSSIBILITY OF SUCH DAMAGE.
301cd76c75SBen Gras */
311cd76c75SBen Gras
321cd76c75SBen Gras #ifndef _X86_INTR_H_
331cd76c75SBen Gras #define _X86_INTR_H_
341cd76c75SBen Gras
351cd76c75SBen Gras #define __HAVE_FAST_SOFTINTS
361cd76c75SBen Gras #define __HAVE_PREEMPTION
371cd76c75SBen Gras
381cd76c75SBen Gras #ifdef _KERNEL
391cd76c75SBen Gras #include <sys/types.h>
401cd76c75SBen Gras #else
411cd76c75SBen Gras #include <stdbool.h>
421cd76c75SBen Gras #endif
431cd76c75SBen Gras
441cd76c75SBen Gras #include <sys/evcnt.h>
45*0a6a1f1dSLionel Sambuc #include <sys/queue.h>
461cd76c75SBen Gras #include <machine/intrdefs.h>
471cd76c75SBen Gras
481cd76c75SBen Gras #ifndef _LOCORE
491cd76c75SBen Gras #include <machine/pic.h>
501cd76c75SBen Gras
511cd76c75SBen Gras /*
521cd76c75SBen Gras * Struct describing an interrupt source for a CPU. struct cpu_info
531cd76c75SBen Gras * has an array of MAX_INTR_SOURCES of these. The index in the array
541cd76c75SBen Gras * is equal to the stub number of the stubcode as present in vector.s
551cd76c75SBen Gras *
561cd76c75SBen Gras * The primary CPU's array of interrupt sources has its first 16
571cd76c75SBen Gras * entries reserved for legacy ISA irq handlers. This means that
581cd76c75SBen Gras * they have a 1:1 mapping for arrayindex:irq_num. This is not
591cd76c75SBen Gras * true for interrupts that come in through IO APICs, to find
601cd76c75SBen Gras * their source, go through ci->ci_isources[index].is_pic
611cd76c75SBen Gras *
621cd76c75SBen Gras * It's possible to always maintain a 1:1 mapping, but that means
631cd76c75SBen Gras * limiting the total number of interrupt sources to MAX_INTR_SOURCES
641cd76c75SBen Gras * (32), instead of 32 per CPU. It also would mean that having multiple
651cd76c75SBen Gras * IO APICs which deliver interrupts from an equal pin number would
661cd76c75SBen Gras * overlap if they were to be sent to the same CPU.
671cd76c75SBen Gras */
681cd76c75SBen Gras
691cd76c75SBen Gras struct intrstub {
701cd76c75SBen Gras void *ist_entry;
711cd76c75SBen Gras void *ist_recurse;
721cd76c75SBen Gras void *ist_resume;
731cd76c75SBen Gras };
741cd76c75SBen Gras
75*0a6a1f1dSLionel Sambuc struct percpu_evcnt {
76*0a6a1f1dSLionel Sambuc cpuid_t cpuid;
77*0a6a1f1dSLionel Sambuc uint64_t count;
78*0a6a1f1dSLionel Sambuc };
79*0a6a1f1dSLionel Sambuc
801cd76c75SBen Gras struct intrsource {
811cd76c75SBen Gras int is_maxlevel; /* max. IPL for this source */
821cd76c75SBen Gras int is_pin; /* IRQ for legacy; pin for IO APIC,
831cd76c75SBen Gras -1 for MSI */
841cd76c75SBen Gras struct intrhand *is_handlers; /* handler chain */
851cd76c75SBen Gras struct pic *is_pic; /* originating PIC */
861cd76c75SBen Gras void *is_recurse; /* entry for spllower */
871cd76c75SBen Gras void *is_resume; /* entry for doreti */
881cd76c75SBen Gras lwp_t *is_lwp; /* for soft interrupts */
89*0a6a1f1dSLionel Sambuc struct evcnt is_evcnt; /* interrupt counter per cpu */
901cd76c75SBen Gras int is_flags; /* see below */
911cd76c75SBen Gras int is_type; /* level, edge */
921cd76c75SBen Gras int is_idtvec;
931cd76c75SBen Gras int is_minlevel;
941cd76c75SBen Gras char is_evname[32]; /* event counter name */
95*0a6a1f1dSLionel Sambuc char is_intrid[INTRIDBUF]; /* intrid created by create_intrid() */
96*0a6a1f1dSLionel Sambuc char is_xname[INTRDEVNAMEBUF]; /* device names */
97*0a6a1f1dSLionel Sambuc cpuid_t is_active_cpu; /* active cpuid */
98*0a6a1f1dSLionel Sambuc struct percpu_evcnt *is_saved_evcnt; /* interrupt count of deactivated cpus */
99*0a6a1f1dSLionel Sambuc SIMPLEQ_ENTRY(intrsource) is_list; /* link of intrsources */
1001cd76c75SBen Gras };
1011cd76c75SBen Gras
1021cd76c75SBen Gras #define IS_LEGACY 0x0001 /* legacy ISA irq source */
1031cd76c75SBen Gras #define IS_IPI 0x0002
1041cd76c75SBen Gras #define IS_LOG 0x0004
1051cd76c75SBen Gras
1061cd76c75SBen Gras /*
1071cd76c75SBen Gras * Interrupt handler chains. *_intr_establish() insert a handler into
1081cd76c75SBen Gras * the list. The handler is called with its (single) argument.
1091cd76c75SBen Gras */
1101cd76c75SBen Gras
1111cd76c75SBen Gras struct intrhand {
1121cd76c75SBen Gras int (*ih_fun)(void *);
1131cd76c75SBen Gras void *ih_arg;
1141cd76c75SBen Gras int ih_level;
1151cd76c75SBen Gras int (*ih_realfun)(void *);
1161cd76c75SBen Gras void *ih_realarg;
1171cd76c75SBen Gras struct intrhand *ih_next;
1181cd76c75SBen Gras struct intrhand **ih_prevp;
1191cd76c75SBen Gras int ih_pin;
1201cd76c75SBen Gras int ih_slot;
1211cd76c75SBen Gras struct cpu_info *ih_cpu;
1221cd76c75SBen Gras };
1231cd76c75SBen Gras
1241cd76c75SBen Gras #define IMASK(ci,level) (ci)->ci_imask[(level)]
1251cd76c75SBen Gras #define IUNMASK(ci,level) (ci)->ci_iunmask[(level)]
1261cd76c75SBen Gras
1271cd76c75SBen Gras #ifdef _KERNEL
1281cd76c75SBen Gras
1291cd76c75SBen Gras void Xspllower(int);
1301cd76c75SBen Gras void spllower(int);
1311cd76c75SBen Gras int splraise(int);
1321cd76c75SBen Gras void softintr(int);
1331cd76c75SBen Gras
1341cd76c75SBen Gras /*
1351cd76c75SBen Gras * Convert spl level to local APIC level
1361cd76c75SBen Gras */
1371cd76c75SBen Gras
1381cd76c75SBen Gras #define APIC_LEVEL(l) ((l) << 4)
1391cd76c75SBen Gras
1401cd76c75SBen Gras /*
1411cd76c75SBen Gras * Miscellaneous
1421cd76c75SBen Gras */
1431cd76c75SBen Gras
1441cd76c75SBen Gras #define SPL_ASSERT_BELOW(x) KDASSERT(curcpu()->ci_ilevel < (x))
1451cd76c75SBen Gras #define spl0() spllower(IPL_NONE)
1461cd76c75SBen Gras #define splx(x) spllower(x)
1471cd76c75SBen Gras
1481cd76c75SBen Gras typedef uint8_t ipl_t;
1491cd76c75SBen Gras typedef struct {
1501cd76c75SBen Gras ipl_t _ipl;
1511cd76c75SBen Gras } ipl_cookie_t;
1521cd76c75SBen Gras
1531cd76c75SBen Gras static inline ipl_cookie_t
makeiplcookie(ipl_t ipl)1541cd76c75SBen Gras makeiplcookie(ipl_t ipl)
1551cd76c75SBen Gras {
1561cd76c75SBen Gras
1571cd76c75SBen Gras return (ipl_cookie_t){._ipl = ipl};
1581cd76c75SBen Gras }
1591cd76c75SBen Gras
1601cd76c75SBen Gras static inline int
splraiseipl(ipl_cookie_t icookie)1611cd76c75SBen Gras splraiseipl(ipl_cookie_t icookie)
1621cd76c75SBen Gras {
1631cd76c75SBen Gras
1641cd76c75SBen Gras return splraise(icookie._ipl);
1651cd76c75SBen Gras }
1661cd76c75SBen Gras
1671cd76c75SBen Gras #include <sys/spl.h>
1681cd76c75SBen Gras
1691cd76c75SBen Gras /*
1701cd76c75SBen Gras * Stub declarations.
1711cd76c75SBen Gras */
1721cd76c75SBen Gras
1731cd76c75SBen Gras void Xsoftintr(void);
1741cd76c75SBen Gras void Xpreemptrecurse(void);
1751cd76c75SBen Gras void Xpreemptresume(void);
1761cd76c75SBen Gras
1771cd76c75SBen Gras extern struct intrstub i8259_stubs[];
1781cd76c75SBen Gras extern struct intrstub ioapic_edge_stubs[];
1791cd76c75SBen Gras extern struct intrstub ioapic_level_stubs[];
1801cd76c75SBen Gras
1811cd76c75SBen Gras struct cpu_info;
1821cd76c75SBen Gras
1831cd76c75SBen Gras struct pcibus_attach_args;
1841cd76c75SBen Gras
185*0a6a1f1dSLionel Sambuc typedef uint64_t intr_handle_t;
186*0a6a1f1dSLionel Sambuc
1871cd76c75SBen Gras void intr_default_setup(void);
1881cd76c75SBen Gras void x86_nmi(void);
189*0a6a1f1dSLionel Sambuc void *intr_establish_xname(int, struct pic *, int, int, int, int (*)(void *),
190*0a6a1f1dSLionel Sambuc void *, bool, const char *);
1911cd76c75SBen Gras void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), void *, bool);
1921cd76c75SBen Gras void intr_disestablish(struct intrhand *);
1931cd76c75SBen Gras void intr_add_pcibus(struct pcibus_attach_args *);
194*0a6a1f1dSLionel Sambuc const char *intr_string(intr_handle_t, char *, size_t);
1951cd76c75SBen Gras void cpu_intr_init(struct cpu_info *);
196*0a6a1f1dSLionel Sambuc int intr_find_mpmapping(int, int, intr_handle_t *);
1971cd76c75SBen Gras struct pic *intr_findpic(int);
1981cd76c75SBen Gras void intr_printconfig(void);
1991cd76c75SBen Gras
200*0a6a1f1dSLionel Sambuc struct intrsource *intr_allocate_io_intrsource(const char *);
201*0a6a1f1dSLionel Sambuc void intr_free_io_intrsource(const char *);
202*0a6a1f1dSLionel Sambuc
2031cd76c75SBen Gras int x86_send_ipi(struct cpu_info *, int);
2041cd76c75SBen Gras void x86_broadcast_ipi(int);
2051cd76c75SBen Gras void x86_ipi_handler(void);
2061cd76c75SBen Gras
207*0a6a1f1dSLionel Sambuc extern void (* const ipifunc[X86_NIPI])(struct cpu_info *);
2081cd76c75SBen Gras
2091cd76c75SBen Gras #endif /* _KERNEL */
2101cd76c75SBen Gras
2111cd76c75SBen Gras #endif /* !_LOCORE */
2121cd76c75SBen Gras
2131cd76c75SBen Gras #endif /* !_X86_INTR_H_ */
214