Home | History | Annotate | Line # | Download | only in include
intr.h revision 1.29
      1 /*	$NetBSD: intr.h,v 1.29 2002/11/01 01:12:44 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 /*
     43  * Interrupt priority levels.
     44  *
     45  * There are tty, network and disk drivers that use free() at interrupt
     46  * time, so imp > (tty | net | bio).
     47  *
     48  * Since run queues may be manipulated by both the statclock and tty,
     49  * network, and disk drivers, clock > imp.
     50  *
     51  * IPL_HIGH must block everything that can manipulate a run queue.
     52  *
     53  * We need serial drivers to run at the absolute highest priority to
     54  * avoid overruns, so serial > high.
     55  */
     56 #define	IPL_NONE	0x00	/* nothing */
     57 #define	IPL_SOFTCLOCK	0x40	/* timeouts */
     58 #define	IPL_SOFTNET	0x50	/* protocol stacks */
     59 #define	IPL_BIO		0x60	/* block I/O */
     60 #define	IPL_NET		0x70	/* network */
     61 #define	IPL_SOFTSERIAL	0x80	/* serial */
     62 #define	IPL_TTY		0x90	/* terminal */
     63 #define	IPL_IMP		0xa0	/* memory allocation */
     64 #define	IPL_AUDIO	0xb0	/* audio */
     65 #define	IPL_CLOCK	0xc0	/* clock */
     66 #define IPL_SCHED	IPL_CLOCK
     67 #define	IPL_HIGH	0xd0	/* everything */
     68 #define	IPL_SERIAL	0xd0	/* serial */
     69 #define IPL_IPI		0xe0	/* inter-processor interrupts */
     70 #define	NIPL		16
     71 
     72 /* Interrupt sharing types. */
     73 #define	IST_NONE	0	/* none */
     74 #define	IST_PULSE	1	/* pulsed */
     75 #define	IST_EDGE	2	/* edge-triggered */
     76 #define	IST_LEVEL	3	/* level-triggered */
     77 
     78 /* Soft interrupt masks. */
     79 #define	SIR_CLOCK	31
     80 #define	SIR_NET		30
     81 #define	SIR_SERIAL	29
     82 
     83 /* Hack for CLKF_INTR(). */
     84 #define	IPL_TAGINTR	28
     85 
     86 #ifndef _LOCORE
     87 
     88 extern volatile u_int32_t lapic_tpr;
     89 extern volatile u_int32_t ipending;
     90 
     91 extern int imasks[NIPL];
     92 extern int iunmask[NIPL];
     93 
     94 #define CPSHIFT 4
     95 #define IMASK(level) imasks[(level)>>CPSHIFT]
     96 #define IUNMASK(level) iunmask[(level)>>CPSHIFT]
     97 
     98 extern void Xspllower __P((void));
     99 
    100 static __inline int splraise __P((int));
    101 static __inline void spllower __P((int));
    102 static __inline void softintr __P((int));
    103 
    104 /*
    105  * compiler barrier: prevent reordering of instructions.
    106  * XXX something similar will move to <sys/cdefs.h>
    107  * or thereabouts.
    108  * This prevents the compiler from reordering code around
    109  * this "instruction", acting as a sequence point for code generation.
    110  */
    111 
    112 #define	__splbarrier() __asm __volatile("" : : : "memory")
    113 
    114 /*
    115  * Add a mask to cpl, and return the old value of cpl.
    116  */
    117 static __inline int
    118 splraise(int ncpl)
    119 {
    120 	register int ocpl = lapic_tpr;
    121 
    122 	if (ncpl > ocpl)
    123 		lapic_tpr = ncpl;
    124 	__splbarrier();
    125 	return (ocpl);
    126 }
    127 
    128 /*
    129  * Restore a value to cpl (unmasking interrupts).  If any unmasked
    130  * interrupts are pending, call Xspllower() to process them.
    131  */
    132 static __inline void
    133 spllower(int ncpl)
    134 {
    135 	register int cmask;
    136 
    137 	__splbarrier();
    138 	lapic_tpr = ncpl;
    139 	cmask = IUNMASK(ncpl);
    140 	if (ipending & cmask)
    141 		Xspllower();
    142 }
    143 
    144 /*
    145  * Hardware interrupt masks
    146  */
    147 #define	splbio()	splraise(IPL_BIO)
    148 #define	splnet()	splraise(IPL_NET)
    149 #define	spltty()	splraise(IPL_TTY)
    150 #define	splaudio()	splraise(IPL_AUDIO)
    151 #define	splclock()	splraise(IPL_CLOCK)
    152 #define	splstatclock()	splclock()
    153 #define	splserial()	splraise(IPL_SERIAL)
    154 #define splipi()	splraise(IPL_IPI)
    155 
    156 #define spllpt()	spltty()
    157 
    158 #define SPL_ASSERT_ATMOST(x) KDASSERT(lapic_tpr <= (x))
    159 #define	spllpt()	spltty()
    160 
    161 /*
    162  * Software interrupt masks
    163  *
    164  * NOTE: splsoftclock() is used by hardclock() to lower the priority from
    165  * clock to softclock before it calls softclock().
    166  */
    167 #define	spllowersoftclock() spllower(IPL_SOFTCLOCK)
    168 
    169 #define	splsoftclock()	splraise(IPL_SOFTCLOCK)
    170 #define	splsoftnet()	splraise(IPL_SOFTNET)
    171 #define	splsoftserial()	splraise(IPL_SOFTSERIAL)
    172 
    173 /*
    174  * Miscellaneous
    175  */
    176 #define	splvm()		splraise(IPL_IMP)
    177 #define	splhigh()	splraise(IPL_HIGH)
    178 #define	spl0()		spllower(IPL_NONE)
    179 #define	splsched()	splraise(IPL_SCHED)
    180 #define spllock() 	splhigh()
    181 #define	splx(x)		spllower(x)
    182 
    183 /*
    184  * Software interrupt registration
    185  *
    186  * We hand-code this to ensure that it's atomic.
    187  */
    188 static __inline void
    189 softintr(register int sir)
    190 {
    191 	__asm __volatile("lock ; orl %1, %0" : "=m"(ipending) : "ir" (1 << sir));
    192 }
    193 
    194 #define	setsoftnet()	softintr(SIR_NET)
    195 
    196 /* XXX does ipi goo belong here, or elsewhere? */
    197 
    198 #define I386_IPI_HALT			0x00000001
    199 #define I386_IPI_MICROSET		0x00000002
    200 #define I386_IPI_FLUSH_FPU		0x00000004
    201 #define I386_IPI_SYNCH_FPU		0x00000008
    202 #define I386_IPI_TLB			0x00000010
    203 #define I386_IPI_MTRR			0x00000020
    204 #define I386_IPI_GDT			0x00000040
    205 
    206 #define I386_NIPI		7
    207 
    208 #ifdef MULTIPROCESSOR
    209 struct cpu_info;
    210 
    211 int i386_send_ipi (struct cpu_info *, int);
    212 void i386_broadcast_ipi (int);
    213 void i386_multicast_ipi (int, int);
    214 void i386_ipi_handler (void);
    215 #endif
    216 
    217 #endif /* !_LOCORE */
    218 
    219 /*
    220  * Generic software interrupt support.
    221  */
    222 
    223 #define	I386_SOFTINTR_SOFTCLOCK		0
    224 #define	I386_SOFTINTR_SOFTNET		1
    225 #define	I386_SOFTINTR_SOFTSERIAL	2
    226 #define	I386_NSOFTINTR			3
    227 
    228 #ifndef _LOCORE
    229 #include <sys/queue.h>
    230 
    231 struct i386_soft_intrhand {
    232 	TAILQ_ENTRY(i386_soft_intrhand)
    233 		sih_q;
    234 	struct i386_soft_intr *sih_intrhead;
    235 	void	(*sih_fn)(void *);
    236 	void	*sih_arg;
    237 	int	sih_pending;
    238 };
    239 
    240 struct i386_soft_intr {
    241 	TAILQ_HEAD(, i386_soft_intrhand)
    242 		softintr_q;
    243 	int softintr_ssir;
    244 	struct simplelock softintr_slock;
    245 };
    246 
    247 #define	i386_softintr_lock(si, s)					\
    248 do {									\
    249 	/* XXX splhigh braindamage on i386 */				\
    250 	(s) = splserial();						\
    251 	simple_lock(&si->softintr_slock);				\
    252 } while (/*CONSTCOND*/ 0)
    253 
    254 #define	i386_softintr_unlock(si, s)					\
    255 do {									\
    256 	simple_unlock(&si->softintr_slock);				\
    257 	splx((s));							\
    258 } while (/*CONSTCOND*/ 0)
    259 
    260 void	*softintr_establish(int, void (*)(void *), void *);
    261 void	softintr_disestablish(void *);
    262 void	softintr_init(void);
    263 void	softintr_dispatch(int);
    264 
    265 #define	softintr_schedule(arg)						\
    266 do {									\
    267 	struct i386_soft_intrhand *__sih = (arg);			\
    268 	struct i386_soft_intr *__si = __sih->sih_intrhead;		\
    269 	int __s;							\
    270 									\
    271 	i386_softintr_lock(__si, __s);					\
    272 	if (__sih->sih_pending == 0) {					\
    273 		TAILQ_INSERT_TAIL(&__si->softintr_q, __sih, sih_q);	\
    274 		__sih->sih_pending = 1;					\
    275 		softintr(__si->softintr_ssir);				\
    276 	}								\
    277 	i386_softintr_unlock(__si, __s);				\
    278 } while (/*CONSTCOND*/ 0)
    279 #endif /* _LOCORE */
    280 
    281 #endif /* !_I386_INTR_H_ */
    282