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