Home | History | Annotate | Line # | Download | only in include
intr.h revision 1.31
      1 /*	$NetBSD: intr.h,v 1.31 2002/11/22 21:21:14 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 _I386_INTR_H_
     40 #define _I386_INTR_H_
     41 
     42 #include <machine/intrdefs.h>
     43 
     44 #ifndef _LOCORE
     45 #include <machine/cpu.h>
     46 #include <machine/pic.h>
     47 
     48 /*
     49  * Struct describing an interrupt source for a CPU. struct cpu_info
     50  * has an array of MAX_INTR_SOURCES of these. The index in the array
     51  * is equal to the stub number of the stubcode as present in vector.s
     52  *
     53  * The primary CPU's array of interrupt sources has its first 16
     54  * entries reserved for legacy ISA irq handlers. This means that
     55  * they have a 1:1 mapping for arrayindex:irq_num. This is not
     56  * true for interrupts that come in through IO APICs, to find
     57  * their source, go through ci->ci_isources[index].is_pic
     58  *
     59  * It's possible to always maintain a 1:1 mapping, but that means
     60  * limiting the total number of interrupt sources to MAX_INTR_SOURCES
     61  * (32), instead of 32 per CPU. It also would mean that having multiple
     62  * IO APICs which deliver interrupts from an equal pin number would
     63  * overlap if they were to be sent to the same CPU.
     64  */
     65 
     66 struct intrstub {
     67 	void *ist_entry;
     68 	void *ist_recurse;
     69 	void *ist_resume;
     70 };
     71 
     72 struct intrsource {
     73 	int is_maxlevel;		/* max. IPL for this source */
     74 	int is_pin;			/* IRQ for legacy; pin for IO APIC */
     75 	struct intrhand *is_handlers;	/* handler chain */
     76 	struct pic *is_pic;		/* originating PIC */
     77 	void *is_recurse;		/* entry for spllower */
     78 	void *is_resume;		/* entry for doreti */
     79 	struct evcnt is_evcnt;		/* interrupt counter */
     80 	char is_evname[32];		/* event counter name */
     81 	int is_flags;			/* see below */
     82 	int is_type;			/* level, edge */
     83 	int is_idtvec;
     84 	int is_minlevel;
     85 };
     86 
     87 #define IS_LEGACY	0x0001		/* legacy ISA irq source */
     88 #define IS_IPI		0x0002
     89 #define IS_LOG		0x0004
     90 
     91 
     92 /*
     93  * Interrupt handler chains.  *_intr_establish() insert a handler into
     94  * the list.  The handler is called with its (single) argument.
     95  */
     96 
     97 struct intrhand {
     98 	int	(*ih_fun)(void *);
     99 	void	*ih_arg;
    100 	int	ih_level;
    101 	struct	intrhand *ih_next;
    102 	int	ih_pin;
    103 	int	ih_slot;
    104 	struct cpu_info *ih_cpu;
    105 };
    106 
    107 #define IMASK(ci,level) (ci)->ci_imask[(level)]
    108 #define IUNMASK(ci,level) (ci)->ci_iunmask[(level)]
    109 
    110 extern void Xspllower __P((void));
    111 
    112 static __inline int splraise __P((int));
    113 static __inline void spllower __P((int));
    114 static __inline void softintr __P((int));
    115 
    116 /*
    117  * Convert spl level to local APIC level
    118  */
    119 #define APIC_LEVEL(l)   ((l) << 4)
    120 
    121 /*
    122  * compiler barrier: prevent reordering of instructions.
    123  * XXX something similar will move to <sys/cdefs.h>
    124  * or thereabouts.
    125  * This prevents the compiler from reordering code around
    126  * this "instruction", acting as a sequence point for code generation.
    127  */
    128 
    129 #define	__splbarrier() __asm __volatile("":::"memory")
    130 
    131 /*
    132  * Add a mask to cpl, and return the old value of cpl.
    133  */
    134 static __inline int
    135 splraise(int nlevel)
    136 {
    137 	int olevel;
    138 	struct cpu_info *ci = curcpu();
    139 
    140 	olevel = ci->ci_ilevel;
    141 	if (nlevel > olevel)
    142 		ci->ci_ilevel = nlevel;
    143 	__splbarrier();
    144 	return (olevel);
    145 }
    146 
    147 void    cpu_Debugger(void);
    148 void    printf(const char *, ...)
    149     __attribute__((__format__(__printf__,1,2)));
    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 	ci->ci_ilevel = nlevel;
    162 	/*
    163 	 * Since this should only lower the interrupt level,
    164 	 * the XOR below should only show interrupts that
    165 	 * are being unmasked.
    166 	 */
    167 	if (ci->ci_ipending & IUNMASK(ci,nlevel))
    168 		Xspllower();
    169 }
    170 
    171 /*
    172  * Hardware interrupt masks
    173  */
    174 #define	splbio()	splraise(IPL_BIO)
    175 #define	splnet()	splraise(IPL_NET)
    176 #define	spltty()	splraise(IPL_TTY)
    177 #define	splaudio()	splraise(IPL_AUDIO)
    178 #define	splclock()	splraise(IPL_CLOCK)
    179 #define	splstatclock()	splclock()
    180 #define	splserial()	splraise(IPL_SERIAL)
    181 #define splipi()	splraise(IPL_IPI)
    182 
    183 #define spllpt()	spltty()
    184 
    185 #define SPL_ASSERT_BELOW(x) KDASSERT(curcpu()->ci_ilevel < (x))
    186 #define	spllpt()	spltty()
    187 
    188 /*
    189  * Software interrupt masks
    190  *
    191  * NOTE: splsoftclock() is used by hardclock() to lower the priority from
    192  * clock to softclock before it calls softclock().
    193  */
    194 #define	spllowersoftclock() spllower(IPL_SOFTCLOCK)
    195 
    196 #define	splsoftclock()	splraise(IPL_SOFTCLOCK)
    197 #define	splsoftnet()	splraise(IPL_SOFTNET)
    198 #define	splsoftserial()	splraise(IPL_SOFTSERIAL)
    199 
    200 /*
    201  * Miscellaneous
    202  */
    203 #define	splvm()		splraise(IPL_IMP)
    204 #define	splhigh()	splraise(IPL_HIGH)
    205 #define	spl0()		spllower(IPL_NONE)
    206 #define	splsched()	splraise(IPL_SCHED)
    207 #define spllock() 	splhigh()
    208 #define	splx(x)		spllower(x)
    209 
    210 /*
    211  * Software interrupt registration
    212  *
    213  * We hand-code this to ensure that it's atomic.
    214  *
    215  * XXX always scheduled on the current CPU.
    216  */
    217 static __inline void
    218 softintr(int sir)
    219 {
    220 	struct cpu_info *ci = curcpu();
    221 
    222 	__asm __volatile("lock ; orl %1, %0" :
    223 	    "=m"(ci->ci_ipending) : "ir" (1 << sir));
    224 }
    225 
    226 /*
    227  * XXX
    228  */
    229 #define	setsoftnet()	softintr(SIR_NET)
    230 
    231 /*
    232  * Stub declarations.
    233  */
    234 
    235 extern void Xsoftclock(void);
    236 extern void Xsoftnet(void);
    237 extern void Xsoftserial(void);
    238 
    239 extern struct intrstub i8259_stubs[];
    240 extern struct intrstub ioapic_stubs[];
    241 
    242 struct cpu_info;
    243 
    244 #include "ioapic.h"
    245 
    246 extern char idt_allocmap[];
    247 
    248 void intr_default_setup(void);
    249 int i386_nmi(void);
    250 void intr_calculatemasks(struct cpu_info *);
    251 int intr_allocate_slot_cpu(struct cpu_info *, struct pic *, int, int *);
    252 int intr_allocate_slot(struct pic *, int, int, int, struct cpu_info **, int *,
    253 		       int *);
    254 void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), void *);
    255 void intr_disestablish(struct intrhand *);
    256 void cpu_intr_init(struct cpu_info *);
    257 #ifdef INTRDEBUG
    258 void intr_printconfig(void);
    259 #endif
    260 
    261 #if NIOAPIC > 0
    262 int intr_find_mpmapping(int bus, int pin, int *handle);
    263 #endif
    264 
    265 #ifdef MULTIPROCESSOR
    266 int i386_send_ipi(struct cpu_info *, int);
    267 void i386_broadcast_ipi(int);
    268 void i386_multicast_ipi(int, int);
    269 void i386_ipi_handler(void);
    270 void i386_intlock(void);
    271 void i386_intunlock(void);
    272 #endif
    273 
    274 #endif /* !_LOCORE */
    275 
    276 /*
    277  * Generic software interrupt support.
    278  */
    279 
    280 #define	I386_SOFTINTR_SOFTCLOCK		0
    281 #define	I386_SOFTINTR_SOFTNET		1
    282 #define	I386_SOFTINTR_SOFTSERIAL	2
    283 #define	I386_NSOFTINTR			3
    284 
    285 #ifndef _LOCORE
    286 #include <sys/queue.h>
    287 
    288 struct i386_soft_intrhand {
    289 	TAILQ_ENTRY(i386_soft_intrhand)
    290 		sih_q;
    291 	struct i386_soft_intr *sih_intrhead;
    292 	void	(*sih_fn)(void *);
    293 	void	*sih_arg;
    294 	int	sih_pending;
    295 };
    296 
    297 struct i386_soft_intr {
    298 	TAILQ_HEAD(, i386_soft_intrhand)
    299 		softintr_q;
    300 	int softintr_ssir;
    301 	struct simplelock softintr_slock;
    302 };
    303 
    304 #define	i386_softintr_lock(si, s)					\
    305 do {									\
    306 	(s) = splhigh();						\
    307 	simple_lock(&si->softintr_slock);				\
    308 } while (/*CONSTCOND*/ 0)
    309 
    310 #define	i386_softintr_unlock(si, s)					\
    311 do {									\
    312 	simple_unlock(&si->softintr_slock);				\
    313 	splx((s));							\
    314 } while (/*CONSTCOND*/ 0)
    315 
    316 void	*softintr_establish(int, void (*)(void *), void *);
    317 void	softintr_disestablish(void *);
    318 void	softintr_init(void);
    319 void	softintr_dispatch(int);
    320 
    321 #define	softintr_schedule(arg)						\
    322 do {									\
    323 	struct i386_soft_intrhand *__sih = (arg);			\
    324 	struct i386_soft_intr *__si = __sih->sih_intrhead;		\
    325 	int __s;							\
    326 									\
    327 	i386_softintr_lock(__si, __s);					\
    328 	if (__sih->sih_pending == 0) {					\
    329 		TAILQ_INSERT_TAIL(&__si->softintr_q, __sih, sih_q);	\
    330 		__sih->sih_pending = 1;					\
    331 		softintr(__si->softintr_ssir);				\
    332 	}								\
    333 	i386_softintr_unlock(__si, __s);				\
    334 } while (/*CONSTCOND*/ 0)
    335 #endif /* _LOCORE */
    336 
    337 #endif /* !_I386_INTR_H_ */
    338