2 * from: vector.s, 386BSD 0.1 unknown origin
3 * $FreeBSD: src/sys/i386/isa/apic_vector.s,v 1.47.2.5 2001/09/01 22:33:38 tegge Exp $
6 #include "opt_auto_eoi.h"
8 #include <machine/asmacros.h>
9 #include <machine/lock.h>
10 #include <machine/psl.h>
11 #include <machine/trap.h>
13 #include <machine_base/icu/icu.h>
14 #include <bus/isa/isa.h>
20 #include <machine/smp.h>
21 #include <machine/intr_machdep.h>
23 /* convert an absolute IRQ# into bitmask */
24 #define IRQ_LBIT(irq_num) (1 << ((irq_num) & 0x1f))
26 /* convert an absolute IRQ# into ipending index */
27 #define IRQ_LIDX(irq_num) ((irq_num) >> 5)
30 #define MPLOCKED lock ;
36 * Push an interrupt frame in a format acceptable to doreti, reload
37 * the segment registers for the kernel.
40 pushl $0 ; /* dummy error code */ \
41 pushl $0 ; /* dummy trap type */ \
42 pushl $0 ; /* dummy xflags type */ \
44 pushl %ds ; /* save data and extra segments ... */ \
57 * Warning: POP_FRAME can only be used if there is no chance of a
58 * segment register being changed (e.g. by procfs), which is why syscalls
67 addl $3*4,%esp ; /* dummy xflags, trap & error codes */ \
69 #define IOAPICADDR(irq_num) \
70 CNAME(int_to_apicintpin) + IOAPIC_IM_SIZE * (irq_num) + IOAPIC_IM_ADDR
71 #define REDIRIDX(irq_num) \
72 CNAME(int_to_apicintpin) + IOAPIC_IM_SIZE * (irq_num) + IOAPIC_IM_ENTIDX
73 #define IOAPICFLAGS(irq_num) \
74 CNAME(int_to_apicintpin) + IOAPIC_IM_SIZE * (irq_num) + IOAPIC_IM_FLAGS
76 #define MASK_IRQ(irq_num) \
77 IOAPIC_IMASK_LOCK ; /* into critical reg */ \
78 testl $IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
79 jne 7f ; /* masked, don't mask */ \
80 orl $IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
81 /* set the mask bit */ \
82 movl IOAPICADDR(irq_num), %ecx ; /* ioapic addr */ \
83 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
84 movl %eax, (%ecx) ; /* write the index */ \
85 orl $IOART_INTMASK,IOAPIC_WINDOW(%ecx) ;/* set the mask */ \
86 7: ; /* already masked */ \
87 IOAPIC_IMASK_UNLOCK ; \
90 * Test to see whether we are handling an edge or level triggered INT.
91 * Level-triggered INTs must still be masked as we don't clear the source,
92 * and the EOI cycle would cause redundant INTs to occur.
94 #define MASK_LEVEL_IRQ(irq_num) \
95 testl $IOAPIC_IM_FLAG_LEVEL, IOAPICFLAGS(irq_num) ; \
96 jz 9f ; /* edge, don't mask */ \
101 * Test to see if the source is currntly masked, clear if so.
103 #define UNMASK_IRQ(irq_num) \
106 IOAPIC_IMASK_LOCK ; /* into critical reg */ \
107 testl $IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
108 je 7f ; /* bit clear, not masked */ \
109 andl $~IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
110 /* clear mask bit */ \
111 movl IOAPICADDR(irq_num),%ecx ; /* ioapic addr */ \
112 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
113 movl %eax,(%ecx) ; /* write the index */ \
114 andl $~IOART_INTMASK,IOAPIC_WINDOW(%ecx) ;/* clear the mask */ \
116 IOAPIC_IMASK_UNLOCK ; \
119 #ifdef SMP /* APIC-IO */
122 * Interrupt call handlers run in the following sequence:
124 * - Push the trap frame required by doreti
125 * - Mask the interrupt and reenable its source
126 * - If we cannot take the interrupt set its ipending bit and
128 * - If we can take the interrupt clear its ipending bit,
129 * call the handler, then unmask and doreti.
131 * YYY can cache gd base opitner instead of using hidden %fs prefixes.
134 #define INTR_HANDLER(irq_num) \
137 IDTVEC(ioapic_intr##irq_num) ; \
139 FAKE_MCOUNT(15*4(%esp)) ; \
140 MASK_LEVEL_IRQ(irq_num) ; \
141 movl $0, lapic_eoi ; \
142 movl PCPU(curthread),%ebx ; \
143 movl $0,%eax ; /* CURRENT CPL IN FRAME (REMOVED) */ \
145 testl $-1,TD_NEST_COUNT(%ebx) ; \
147 testl $-1,TD_CRITCOUNT(%ebx) ; \
150 /* in critical section, make interrupt pending */ \
151 /* set the pending bit and return, leave interrupt masked */ \
152 movl $IRQ_LIDX(irq_num),%edx ; \
153 orl $IRQ_LBIT(irq_num),PCPU_E4(ipending,%edx) ; \
154 orl $RQF_INTPEND,PCPU(reqflags) ; \
157 /* clear pending bit, run handler */ \
158 movl $IRQ_LIDX(irq_num),%edx ; \
159 andl $~IRQ_LBIT(irq_num),PCPU_E4(ipending,%edx) ; \
161 pushl %esp ; /* pass frame by reference */ \
162 incl TD_CRITCOUNT(%ebx) ; \
164 call ithread_fast_handler ; /* returns 0 to unmask */ \
165 decl TD_CRITCOUNT(%ebx) ; \
167 UNMASK_IRQ(irq_num) ; \
175 * Handle "spurious INTerrupts".
177 * This is different than the "spurious INTerrupt" generated by an
178 * 8259 PIC for missing INTs. See the APIC documentation for details.
179 * This routine should NOT do an 'EOI' cycle.
186 /* No EOI cycle used here */
192 * Handle TLB shootdowns.
194 * NOTE: Interrupts remain disabled.
201 movl $0, lapic_eoi /* End Of Interrupt to APIC */
202 FAKE_MCOUNT(15*4(%esp))
204 subl $8,%esp /* make same as interrupt frame */
205 pushl %esp /* pass frame by reference */
206 call smp_invltlb_intr
210 jmp doreti_syscall_ret
213 * Executed by a CPU when it receives an Xcpustop IPI from another CPU,
215 * - Signals its receipt.
216 * - Waits for permission to restart.
217 * - Processing pending IPIQ events while waiting.
218 * - Signals its restart.
230 pushl %ds /* save current data segment */
234 mov %ax, %ds /* use KERNEL data segment */
238 movl $0, lapic_eoi /* End Of Interrupt to APIC */
240 movl PCPU(cpuid), %eax
241 imull $PCB_SIZE, %eax
242 leal CNAME(stoppcbs)(%eax), %eax
244 call CNAME(savectx) /* Save process context */
248 movl PCPU(cpuid), %eax
251 * Indicate that we have stopped and loop waiting for permission
252 * to start again. We must still process IPI events while in a
255 * Interrupts must remain enabled for non-IPI'd per-cpu interrupts
256 * (e.g. Xtimer, Xinvltlb).
259 btsl %eax, stopped_cpus /* stopped_cpus |= (1<<id) */
262 andl $~RQF_IPIQ,PCPU(reqflags)
264 call lwkt_smp_stopped
266 btl %eax, started_cpus /* while (!(started_cpus & (1<<id))) */
270 btrl %eax, started_cpus /* started_cpus &= ~(1<<id) */
272 btrl %eax, stopped_cpus /* stopped_cpus &= ~(1<<id) */
277 movl CNAME(cpustop_restartfunc), %eax
280 movl $0, CNAME(cpustop_restartfunc) /* One-shot */
285 popl %ds /* restore previous data segment */
294 * For now just have one ipiq IPI, but what we really want is
295 * to have one for each source cpu to the APICs don't get stalled
296 * backlogging the requests.
303 movl $0, lapic_eoi /* End Of Interrupt to APIC */
304 FAKE_MCOUNT(15*4(%esp))
306 incl PCPU(cnt) + V_IPI
307 movl PCPU(curthread),%ebx
308 testl $-1,TD_CRITCOUNT(%ebx)
310 subl $8,%esp /* make same as interrupt frame */
311 pushl %esp /* pass frame by reference */
312 incl PCPU(intr_nesting_level)
313 incl TD_CRITCOUNT(%ebx)
315 call lwkt_process_ipiq_frame
316 decl TD_CRITCOUNT(%ebx)
317 decl PCPU(intr_nesting_level)
319 pushl $0 /* CPL for frame (REMOVED) */
323 orl $RQF_IPIQ,PCPU(reqflags)
325 jmp doreti_syscall_ret
332 movl $0, lapic_eoi /* End Of Interrupt to APIC */
333 FAKE_MCOUNT(15*4(%esp))
335 incl PCPU(cnt) + V_TIMER
336 movl PCPU(curthread),%ebx
337 testl $-1,TD_CRITCOUNT(%ebx)
339 testl $-1,TD_NEST_COUNT(%ebx)
341 subl $8,%esp /* make same as interrupt frame */
342 pushl %esp /* pass frame by reference */
343 incl PCPU(intr_nesting_level)
344 incl TD_CRITCOUNT(%ebx)
346 call lapic_timer_process_frame
347 decl TD_CRITCOUNT(%ebx)
348 decl PCPU(intr_nesting_level)
350 pushl $0 /* CPL for frame (REMOVED) */
354 orl $RQF_TIMER,PCPU(reqflags)
356 jmp doreti_syscall_ret
358 #ifdef SMP /* APIC-IO */
559 /* variables used by stop_cpus()/restart_cpus()/Xcpustop */
560 .globl stopped_cpus, started_cpus
566 .globl CNAME(cpustop_restartfunc)
567 CNAME(cpustop_restartfunc):