Home | History | Annotate | Line # | Download | only in include
intr.h revision 1.10
      1 /*	$NetBSD: intr.h,v 1.10 2003/10/30 21:19:54 fvdl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum, and by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #ifndef _X86_INTR_H_
     40 #define _X86_INTR_H_
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_multiprocessor.h"
     44 #endif
     45 
     46 #include <machine/intrdefs.h>
     47 
     48 #ifndef _LOCORE
     49 #include <machine/cpu.h>
     50 #include <machine/pic.h>
     51 
     52 /*
     53  * Struct describing an interrupt source for a CPU. struct cpu_info
     54  * has an array of MAX_INTR_SOURCES of these. The index in the array
     55  * is equal to the stub number of the stubcode as present in vector.s
     56  *
     57  * The primary CPU's array of interrupt sources has its first 16
     58  * entries reserved for legacy ISA irq handlers. This means that
     59  * they have a 1:1 mapping for arrayindex:irq_num. This is not
     60  * true for interrupts that come in through IO APICs, to find
     61  * their source, go through ci->ci_isources[index].is_pic
     62  *
     63  * It's possible to always maintain a 1:1 mapping, but that means
     64  * limiting the total number of interrupt sources to MAX_INTR_SOURCES
     65  * (32), instead of 32 per CPU. It also would mean that having multiple
     66  * IO APICs which deliver interrupts from an equal pin number would
     67  * overlap if they were to be sent to the same CPU.
     68  */
     69 
     70 struct intrstub {
     71 	void *ist_entry;
     72 	void *ist_recurse;
     73 	void *ist_resume;
     74 };
     75 
     76 struct intrsource {
     77 	int is_maxlevel;		/* max. IPL for this source */
     78 	int is_pin;			/* IRQ for legacy; pin for IO APIC */
     79 	struct intrhand *is_handlers;	/* handler chain */
     80 	struct pic *is_pic;		/* originating PIC */
     81 	void *is_recurse;		/* entry for spllower */
     82 	void *is_resume;		/* entry for doreti */
     83 	struct evcnt is_evcnt;		/* interrupt counter */
     84 	char is_evname[32];		/* event counter name */
     85 	int is_flags;			/* see below */
     86 	int is_type;			/* level, edge */
     87 	int is_idtvec;
     88 	int is_minlevel;
     89 };
     90 
     91 #define IS_LEGACY	0x0001		/* legacy ISA irq source */
     92 #define IS_IPI		0x0002
     93 #define IS_LOG		0x0004
     94 
     95 
     96 /*
     97  * Interrupt handler chains.  *_intr_establish() insert a handler into
     98  * the list.  The handler is called with its (single) argument.
     99  */
    100 
    101 struct intrhand {
    102 	int	(*ih_fun)(void *);
    103 	void	*ih_arg;
    104 	int	ih_level;
    105 	struct	intrhand *ih_next;
    106 	int	ih_pin;
    107 	int	ih_slot;
    108 	struct cpu_info *ih_cpu;
    109 };
    110 
    111 #define IMASK(ci,level) (ci)->ci_imask[(level)]
    112 #define IUNMASK(ci,level) (ci)->ci_iunmask[(level)]
    113 
    114 extern void Xspllower(int);
    115 
    116 static __inline int splraise(int);
    117 static __inline void spllower(int);
    118 static __inline void softintr(int);
    119 
    120 /*
    121  * Convert spl level to local APIC level
    122  */
    123 #define APIC_LEVEL(l)   ((l) << 4)
    124 
    125 /*
    126  * compiler barrier: prevent reordering of instructions.
    127  * XXX something similar will move to <sys/cdefs.h>
    128  * or thereabouts.
    129  * This prevents the compiler from reordering code around
    130  * this "instruction", acting as a sequence point for code generation.
    131  */
    132 
    133 #define	__splbarrier() __asm __volatile("":::"memory")
    134 
    135 /*
    136  * Add a mask to cpl, and return the old value of cpl.
    137  */
    138 static __inline int
    139 splraise(int nlevel)
    140 {
    141 	int olevel;
    142 	struct cpu_info *ci = curcpu();
    143 
    144 	olevel = ci->ci_ilevel;
    145 	if (nlevel > olevel)
    146 		ci->ci_ilevel = nlevel;
    147 	__splbarrier();
    148 	return (olevel);
    149 }
    150 
    151 /*
    152  * Restore a value to cpl (unmasking interrupts).  If any unmasked
    153  * interrupts are pending, call Xspllower() to process them.
    154  */
    155 static __inline void
    156 spllower(int nlevel)
    157 {
    158 	struct cpu_info *ci = curcpu();
    159 
    160 	__splbarrier();
    161 	/*
    162 	 * Since this should only lower the interrupt level,
    163 	 * the XOR below should only show interrupts that
    164 	 * are being unmasked.
    165 	 */
    166 	if (ci->ci_ipending & IUNMASK(ci,nlevel))
    167 		Xspllower(nlevel);
    168 	else
    169 		ci->ci_ilevel = nlevel;
    170 }
    171 
    172 /*
    173  * Hardware interrupt masks
    174  */
    175 #define	splbio()	splraise(IPL_BIO)
    176 #define	splnet()	splraise(IPL_NET)
    177 #define	spltty()	splraise(IPL_TTY)
    178 #define	splaudio()	splraise(IPL_AUDIO)
    179 #define	splclock()	splraise(IPL_CLOCK)
    180 #define	splstatclock()	splclock()
    181 #define	splserial()	splraise(IPL_SERIAL)
    182 #define splipi()	splraise(IPL_IPI)
    183 
    184 #define spllpt()	spltty()
    185 
    186 #define SPL_ASSERT_BELOW(x) KDASSERT(curcpu()->ci_ilevel < (x))
    187 #define	spllpt()	spltty()
    188 
    189 /*
    190  * Software interrupt masks
    191  *
    192  * NOTE: splsoftclock() is used by hardclock() to lower the priority from
    193  * clock to softclock before it calls softclock().
    194  */
    195 #define	spllowersoftclock() spllower(IPL_SOFTCLOCK)
    196 
    197 #define	splsoftclock()	splraise(IPL_SOFTCLOCK)
    198 #define	splsoftnet()	splraise(IPL_SOFTNET)
    199 #define	splsoftserial()	splraise(IPL_SOFTSERIAL)
    200 
    201 /*
    202  * Miscellaneous
    203  */
    204 #define	splvm()		splraise(IPL_VM)
    205 #define	splhigh()	splraise(IPL_HIGH)
    206 #define	spl0()		spllower(IPL_NONE)
    207 #define	splsched()	splraise(IPL_SCHED)
    208 #define spllock() 	splhigh()
    209 #define	splx(x)		spllower(x)
    210 
    211 /*
    212  * Software interrupt registration
    213  *
    214  * We hand-code this to ensure that it's atomic.
    215  *
    216  * XXX always scheduled on the current CPU.
    217  */
    218 static __inline void
    219 softintr(int sir)
    220 {
    221 	struct cpu_info *ci = curcpu();
    222 
    223 	__asm __volatile("lock ; orl %1, %0" :
    224 	    "=m"(ci->ci_ipending) : "ir" (1 << sir));
    225 }
    226 
    227 /*
    228  * XXX
    229  */
    230 #define	setsoftnet()	softintr(SIR_NET)
    231 
    232 /*
    233  * Stub declarations.
    234  */
    235 
    236 extern void Xsoftclock(void);
    237 extern void Xsoftnet(void);
    238 extern void Xsoftserial(void);
    239 
    240 extern struct intrstub i8259_stubs[];
    241 extern struct intrstub ioapic_edge_stubs[];
    242 extern struct intrstub ioapic_level_stubs[];
    243 
    244 struct cpu_info;
    245 
    246 extern char idt_allocmap[];
    247 
    248 struct pcibus_attach_args;
    249 
    250 void intr_default_setup(void);
    251 int x86_nmi(void);
    252 void intr_calculatemasks(struct cpu_info *);
    253 int intr_allocate_slot_cpu(struct cpu_info *, struct pic *, int, int *);
    254 int intr_allocate_slot(struct pic *, int, int, int, struct cpu_info **, int *,
    255 		       int *);
    256 void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), void *);
    257 void intr_disestablish(struct intrhand *);
    258 void intr_add_pcibus(struct pcibus_attach_args *);
    259 const char *intr_string(int);
    260 void cpu_intr_init(struct cpu_info *);
    261 int intr_find_mpmapping(int, int, int *);
    262 #ifdef INTRDEBUG
    263 void intr_printconfig(void);
    264 #endif
    265 
    266 #ifdef MULTIPROCESSOR
    267 int x86_send_ipi(struct cpu_info *, int);
    268 void x86_broadcast_ipi(int);
    269 void x86_multicast_ipi(int, int);
    270 void x86_ipi_handler(void);
    271 void x86_intlock(struct intrframe *);
    272 void x86_intunlock(struct intrframe *);
    273 void x86_softintlock(void);
    274 void x86_softintunlock(void);
    275 
    276 extern void (*ipifunc[X86_NIPI])(struct cpu_info *);
    277 #endif
    278 
    279 #endif /* !_LOCORE */
    280 
    281 /*
    282  * Generic software interrupt support.
    283  */
    284 
    285 #define	X86_SOFTINTR_SOFTCLOCK		0
    286 #define	X86_SOFTINTR_SOFTNET		1
    287 #define	X86_SOFTINTR_SOFTSERIAL	2
    288 #define	X86_NSOFTINTR			3
    289 
    290 #ifndef _LOCORE
    291 #include <sys/queue.h>
    292 
    293 struct x86_soft_intrhand {
    294 	TAILQ_ENTRY(x86_soft_intrhand)
    295 		sih_q;
    296 	struct x86_soft_intr *sih_intrhead;
    297 	void	(*sih_fn)(void *);
    298 	void	*sih_arg;
    299 	int	sih_pending;
    300 };
    301 
    302 struct x86_soft_intr {
    303 	TAILQ_HEAD(, x86_soft_intrhand)
    304 		softintr_q;
    305 	int softintr_ssir;
    306 	struct simplelock softintr_slock;
    307 };
    308 
    309 #define	x86_softintr_lock(si, s)					\
    310 do {									\
    311 	(s) = splhigh();						\
    312 	simple_lock(&si->softintr_slock);				\
    313 } while (/*CONSTCOND*/ 0)
    314 
    315 #define	x86_softintr_unlock(si, s)					\
    316 do {									\
    317 	simple_unlock(&si->softintr_slock);				\
    318 	splx((s));							\
    319 } while (/*CONSTCOND*/ 0)
    320 
    321 void	*softintr_establish(int, void (*)(void *), void *);
    322 void	softintr_disestablish(void *);
    323 void	softintr_init(void);
    324 void	softintr_dispatch(int);
    325 
    326 #define	softintr_schedule(arg)						\
    327 do {									\
    328 	struct x86_soft_intrhand *__sih = (arg);			\
    329 	struct x86_soft_intr *__si = __sih->sih_intrhead;		\
    330 	int __s;							\
    331 									\
    332 	x86_softintr_lock(__si, __s);					\
    333 	if (__sih->sih_pending == 0) {					\
    334 		TAILQ_INSERT_TAIL(&__si->softintr_q, __sih, sih_q);	\
    335 		__sih->sih_pending = 1;					\
    336 		softintr(__si->softintr_ssir);				\
    337 	}								\
    338 	x86_softintr_unlock(__si, __s);					\
    339 } while (/*CONSTCOND*/ 0)
    340 #endif /* _LOCORE */
    341 
    342 #endif /* !_X86_INTR_H_ */
    343