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 $
7 #include "opt_auto_eoi.h"
10 #include <machine/asmacros.h>
11 #include <machine/lock.h>
12 #include <machine/psl.h>
13 #include <machine/trap.h>
14 #include <machine/segments.h>
16 #include <machine_base/icu/icu.h>
17 #include <bus/isa/isa.h>
22 #include <machine_base/apic/ioapic_ipl.h>
23 #include <machine/intr_machdep.h>
26 /* convert an absolute IRQ# into bitmask */
27 #define IRQ_LBIT(irq_num) (1UL << (irq_num & 0x3f))
30 #define IRQ_SBITS(irq_num) ((irq_num) & 0x3f)
32 /* convert an absolute IRQ# into gd_ipending index */
33 #define IRQ_LIDX(irq_num) ((irq_num) >> 6)
36 #define MPLOCKED lock ;
41 #define APIC_PUSH_FRAME \
42 PUSH_FRAME ; /* 15 regs + space for 5 extras */ \
43 movq $0,TF_XFLAGS(%rsp) ; \
44 movq $0,TF_TRAPNO(%rsp) ; \
45 movq $0,TF_ADDR(%rsp) ; \
46 movq $0,TF_FLAGS(%rsp) ; \
47 movq $0,TF_ERR(%rsp) ; \
51 * JG stale? Warning: POP_FRAME can only be used if there is no chance of a
52 * segment register being changed (e.g. by procfs), which is why syscalls
55 #define APIC_POP_FRAME POP_FRAME
57 #define IOAPICADDR(irq_num) \
58 CNAME(int_to_apicintpin) + IOAPIC_IM_SIZE * (irq_num) + IOAPIC_IM_ADDR
59 #define REDIRIDX(irq_num) \
60 CNAME(int_to_apicintpin) + IOAPIC_IM_SIZE * (irq_num) + IOAPIC_IM_ENTIDX
61 #define IOAPICFLAGS(irq_num) \
62 CNAME(int_to_apicintpin) + IOAPIC_IM_SIZE * (irq_num) + IOAPIC_IM_FLAGS
64 #define MASK_IRQ(irq_num) \
65 IOAPIC_IMASK_LOCK ; /* into critical reg */ \
66 testl $IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
67 jne 7f ; /* masked, don't mask */ \
68 orl $IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
69 /* set the mask bit */ \
70 movq IOAPICADDR(irq_num), %rcx ; /* ioapic addr */ \
71 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
72 movl %eax, (%rcx) ; /* write the index */ \
73 orl $IOART_INTMASK,IOAPIC_WINDOW(%rcx) ;/* set the mask */ \
74 7: ; /* already masked */ \
75 IOAPIC_IMASK_UNLOCK ; \
78 * Test to see whether we are handling an edge or level triggered INT.
79 * Level-triggered INTs must still be masked as we don't clear the source,
80 * and the EOI cycle would cause redundant INTs to occur.
82 #define MASK_LEVEL_IRQ(irq_num) \
83 testl $IOAPIC_IM_FLAG_LEVEL, IOAPICFLAGS(irq_num) ; \
84 jz 9f ; /* edge, don't mask */ \
89 * Test to see if the source is currntly masked, clear if so.
91 #define UNMASK_IRQ(irq_num) \
94 IOAPIC_IMASK_LOCK ; /* into critical reg */ \
95 testl $IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
96 je 7f ; /* bit clear, not masked */ \
97 andl $~IOAPIC_IM_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
98 /* clear mask bit */ \
99 movq IOAPICADDR(irq_num),%rcx ; /* ioapic addr */ \
100 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
101 movl %eax,(%rcx) ; /* write the index */ \
102 andl $~IOART_INTMASK,IOAPIC_WINDOW(%rcx) ;/* clear the mask */ \
104 IOAPIC_IMASK_UNLOCK ; \
107 #ifdef SMP /* APIC-IO */
110 * Interrupt call handlers run in the following sequence:
112 * - Push the trap frame required by doreti
113 * - Mask the interrupt and reenable its source
114 * - If we cannot take the interrupt set its ipending bit and
116 * - If we can take the interrupt clear its ipending bit,
117 * call the handler, then unmask and doreti.
119 * YYY can cache gd base opitner instead of using hidden %fs prefixes.
122 #define INTR_HANDLER(irq_num) \
125 IDTVEC(ioapic_intr##irq_num) ; \
127 FAKE_MCOUNT(TF_RIP(%rsp)) ; \
128 MASK_LEVEL_IRQ(irq_num) ; \
130 movl $0, LA_EOI(%rax) ; \
131 movq PCPU(curthread),%rbx ; \
132 testl $-1,TD_NEST_COUNT(%rbx) ; \
134 testl $-1,TD_CRITCOUNT(%rbx) ; \
137 /* in critical section, make interrupt pending */ \
138 /* set the pending bit and return, leave interrupt masked */ \
140 shlq $IRQ_SBITS(irq_num),%rcx ; \
141 movq $IRQ_LIDX(irq_num),%rdx ; \
142 orq %rcx,PCPU_E8(ipending,%rdx) ; \
143 orl $RQF_INTPEND,PCPU(reqflags) ; \
146 /* clear pending bit, run handler */ \
148 shlq $IRQ_SBITS(irq_num),%rcx ; \
150 movq $IRQ_LIDX(irq_num),%rdx ; \
151 andq %rcx,PCPU_E8(ipending,%rdx) ; \
152 pushq $irq_num ; /* trapframe -> intrframe */ \
153 movq %rsp, %rdi ; /* pass frame by reference */ \
154 incl TD_CRITCOUNT(%rbx) ; \
156 call ithread_fast_handler ; /* returns 0 to unmask */ \
157 decl TD_CRITCOUNT(%rbx) ; \
158 addq $8, %rsp ; /* intrframe -> trapframe */ \
159 UNMASK_IRQ(irq_num) ; \
167 * Handle "spurious INTerrupts".
169 * This is different than the "spurious INTerrupt" generated by an
170 * 8259 PIC for missing INTs. See the APIC documentation for details.
171 * This routine should NOT do an 'EOI' cycle.
178 /* No EOI cycle used here */
184 * Handle TLB shootdowns.
186 * NOTE: interrupts are left disabled.
194 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
195 FAKE_MCOUNT(TF_RIP(%rsp))
196 subq $8,%rsp /* make same as interrupt frame */
197 movq %rsp,%rdi /* pass frame by reference */
198 call smp_invltlb_intr
199 addq $8,%rsp /* turn into trapframe */
205 * Executed by a CPU when it receives an Xcpustop IPI from another CPU,
207 * - We cannot call doreti
208 * - Signals its receipt.
209 * - Waits for permission to restart.
210 * - Processing pending IPIQ events while waiting.
211 * - Signals its restart.
220 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
222 movl PCPU(cpuid), %eax
223 imull $PCB_SIZE, %eax
224 leaq CNAME(stoppcbs), %rdi
226 call CNAME(savectx) /* Save process context */
228 movslq PCPU(cpuid), %rax
231 * Indicate that we have stopped and loop waiting for permission
232 * to start again. We must still process IPI events while in a
235 * Interrupts must remain enabled for non-IPI'd per-cpu interrupts
236 * (e.g. Xtimer, Xinvltlb).
239 btsq %rax, stopped_cpus /* stopped_cpus |= (1<<id) */
242 andl $~RQF_IPIQ,PCPU(reqflags)
244 call lwkt_smp_stopped
247 btq %rax, started_cpus /* while (!(started_cpus & (1<<id))) */
251 btrq %rax, started_cpus /* started_cpus &= ~(1<<id) */
253 btrq %rax, stopped_cpus /* stopped_cpus &= ~(1<<id) */
258 movq CNAME(cpustop_restartfunc), %rax
261 movq $0, CNAME(cpustop_restartfunc) /* One-shot */
270 * For now just have one ipiq IPI, but what we really want is
271 * to have one for each source cpu to the APICs don't get stalled
272 * backlogging the requests.
280 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
281 FAKE_MCOUNT(TF_RIP(%rsp))
283 incl PCPU(cnt) + V_IPI
284 movq PCPU(curthread),%rbx
285 testl $-1,TD_CRITCOUNT(%rbx)
287 subq $8,%rsp /* make same as interrupt frame */
288 movq %rsp,%rdi /* pass frame by reference */
289 incl PCPU(intr_nesting_level)
290 incl TD_CRITCOUNT(%rbx)
292 call lwkt_process_ipiq_frame
293 decl TD_CRITCOUNT(%rbx)
294 decl PCPU(intr_nesting_level)
295 addq $8,%rsp /* turn into trapframe */
299 orl $RQF_IPIQ,PCPU(reqflags)
310 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
311 FAKE_MCOUNT(TF_RIP(%rsp))
313 subq $8,%rsp /* make same as interrupt frame */
314 movq %rsp,%rdi /* pass frame by reference */
315 call lapic_timer_always
316 addq $8,%rsp /* turn into trapframe */
318 incl PCPU(cnt) + V_TIMER
319 movq PCPU(curthread),%rbx
320 testl $-1,TD_CRITCOUNT(%rbx)
322 testl $-1,TD_NEST_COUNT(%rbx)
324 subq $8,%rsp /* make same as interrupt frame */
325 movq %rsp,%rdi /* pass frame by reference */
326 incl PCPU(intr_nesting_level)
327 incl TD_CRITCOUNT(%rbx)
329 call lapic_timer_process_frame
330 decl TD_CRITCOUNT(%rbx)
331 decl PCPU(intr_nesting_level)
332 addq $8,%rsp /* turn into trapframe */
336 orl $RQF_TIMER,PCPU(reqflags)
341 #ifdef SMP /* APIC-IO */
542 /* variables used by stop_cpus()/restart_cpus()/Xcpustop */
543 .globl stopped_cpus, started_cpus
549 .globl CNAME(cpustop_restartfunc)
550 CNAME(cpustop_restartfunc):