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 \
58 #define IOAPICADDR(irq_num) \
59 CNAME(ioapic_irqs) + IOAPIC_IRQI_SIZE * (irq_num) + IOAPIC_IRQI_ADDR
60 #define REDIRIDX(irq_num) \
61 CNAME(ioapic_irqs) + IOAPIC_IRQI_SIZE * (irq_num) + IOAPIC_IRQI_IDX
62 #define IOAPICFLAGS(irq_num) \
63 CNAME(ioapic_irqs) + IOAPIC_IRQI_SIZE * (irq_num) + IOAPIC_IRQI_FLAGS
65 #define MASK_IRQ(irq_num) \
66 IOAPIC_IMASK_LOCK ; /* into critical reg */ \
67 testl $IOAPIC_IRQI_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
68 jne 7f ; /* masked, don't mask */ \
69 orl $IOAPIC_IRQI_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
70 /* set the mask bit */ \
71 movq IOAPICADDR(irq_num), %rcx ; /* ioapic addr */ \
72 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
73 movl %eax, (%rcx) ; /* write the index */ \
74 orl $IOART_INTMASK,IOAPIC_WINDOW(%rcx) ;/* set the mask */ \
75 7: ; /* already masked */ \
76 IOAPIC_IMASK_UNLOCK ; \
79 * Test to see whether we are handling an edge or level triggered INT.
80 * Level-triggered INTs must still be masked as we don't clear the source,
81 * and the EOI cycle would cause redundant INTs to occur.
83 #define MASK_LEVEL_IRQ(irq_num) \
84 testl $IOAPIC_IRQI_FLAG_LEVEL, IOAPICFLAGS(irq_num) ; \
85 jz 9f ; /* edge, don't mask */ \
90 * Test to see if the source is currntly masked, clear if so.
92 #define UNMASK_IRQ(irq_num) \
95 IOAPIC_IMASK_LOCK ; /* into critical reg */ \
96 testl $IOAPIC_IRQI_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
97 je 7f ; /* bit clear, not masked */ \
98 andl $~IOAPIC_IRQI_FLAG_MASKED, IOAPICFLAGS(irq_num) ; \
99 /* clear mask bit */ \
100 movq IOAPICADDR(irq_num),%rcx ; /* ioapic addr */ \
101 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
102 movl %eax,(%rcx) ; /* write the index */ \
103 andl $~IOART_INTMASK,IOAPIC_WINDOW(%rcx) ;/* clear the mask */ \
105 IOAPIC_IMASK_UNLOCK ; \
109 * Interrupt call handlers run in the following sequence:
111 * - Push the trap frame required by doreti
112 * - Mask the interrupt and reenable its source
113 * - If we cannot take the interrupt set its ipending bit and
115 * - If we can take the interrupt clear its ipending bit,
116 * call the handler, then unmask and doreti.
118 * YYY can cache gd base opitner instead of using hidden %fs prefixes.
121 #define INTR_HANDLER(irq_num) \
124 IDTVEC(ioapic_intr##irq_num) ; \
126 FAKE_MCOUNT(TF_RIP(%rsp)) ; \
127 MASK_LEVEL_IRQ(irq_num) ; \
129 movl $0, LA_EOI(%rax) ; \
130 movq PCPU(curthread),%rbx ; \
131 testl $-1,TD_NEST_COUNT(%rbx) ; \
133 testl $-1,TD_CRITCOUNT(%rbx) ; \
136 /* in critical section, make interrupt pending */ \
137 /* set the pending bit and return, leave interrupt masked */ \
139 shlq $IRQ_SBITS(irq_num),%rcx ; \
140 movq $IRQ_LIDX(irq_num),%rdx ; \
141 orq %rcx,PCPU_E8(ipending,%rdx) ; \
142 orl $RQF_INTPEND,PCPU(reqflags) ; \
145 /* clear pending bit, run handler */ \
147 shlq $IRQ_SBITS(irq_num),%rcx ; \
149 movq $IRQ_LIDX(irq_num),%rdx ; \
150 andq %rcx,PCPU_E8(ipending,%rdx) ; \
151 pushq $irq_num ; /* trapframe -> intrframe */ \
152 movq %rsp, %rdi ; /* pass frame by reference */ \
153 incl TD_CRITCOUNT(%rbx) ; \
155 call ithread_fast_handler ; /* returns 0 to unmask */ \
156 decl TD_CRITCOUNT(%rbx) ; \
157 addq $8, %rsp ; /* intrframe -> trapframe */ \
158 UNMASK_IRQ(irq_num) ; \
164 * Handle "spurious INTerrupts".
166 * NOTE: This is different than the "spurious INTerrupt" generated by an
167 * 8259 PIC for missing INTs. See the APIC documentation for details.
168 * This routine should NOT do an 'EOI' cycle.
170 * NOTE: Even though we don't do anything here we must still swapgs if
171 * coming from a user frame in case the iretq faults... just use
172 * the nominal APIC_PUSH_FRAME sequence to get it done.
179 /* No EOI cycle used here */
180 FAKE_MCOUNT(TF_RIP(%rsp))
188 * Handle TLB shootdowns.
190 * NOTE: interrupts are left disabled.
198 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
199 FAKE_MCOUNT(TF_RIP(%rsp))
200 subq $8,%rsp /* make same as interrupt frame */
201 movq %rsp,%rdi /* pass frame by reference */
202 call smp_invltlb_intr
203 addq $8,%rsp /* turn into trapframe */
209 * Executed by a CPU when it receives an Xcpustop IPI from another CPU,
211 * - We cannot call doreti
212 * - Signals its receipt.
213 * - Waits for permission to restart.
214 * - Processing pending IPIQ events while waiting.
215 * - Signals its restart.
224 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
226 movl PCPU(cpuid), %eax
227 imull $PCB_SIZE, %eax
228 leaq CNAME(stoppcbs), %rdi
230 call CNAME(savectx) /* Save process context */
232 movslq PCPU(cpuid), %rax
235 * Indicate that we have stopped and loop waiting for permission
236 * to start again. We must still process IPI events while in a
239 * Interrupts must remain enabled for non-IPI'd per-cpu interrupts
240 * (e.g. Xtimer, Xinvltlb).
243 btsq %rax, stopped_cpus /* stopped_cpus |= (1<<id) */
246 andl $~RQF_IPIQ,PCPU(reqflags)
248 call lwkt_smp_stopped
251 btq %rax, started_cpus /* while (!(started_cpus & (1<<id))) */
255 btrq %rax, started_cpus /* started_cpus &= ~(1<<id) */
257 btrq %rax, stopped_cpus /* stopped_cpus &= ~(1<<id) */
262 movq CNAME(cpustop_restartfunc), %rax
265 movq $0, CNAME(cpustop_restartfunc) /* One-shot */
274 * For now just have one ipiq IPI, but what we really want is
275 * to have one for each source cpu to the APICs don't get stalled
276 * backlogging the requests.
284 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
285 FAKE_MCOUNT(TF_RIP(%rsp))
287 incl PCPU(cnt) + V_IPI
288 movq PCPU(curthread),%rbx
289 testl $-1,TD_CRITCOUNT(%rbx)
291 subq $8,%rsp /* make same as interrupt frame */
292 movq %rsp,%rdi /* pass frame by reference */
293 incl PCPU(intr_nesting_level)
294 incl TD_CRITCOUNT(%rbx)
296 call lwkt_process_ipiq_frame
297 decl TD_CRITCOUNT(%rbx)
298 decl PCPU(intr_nesting_level)
299 addq $8,%rsp /* turn into trapframe */
303 orl $RQF_IPIQ,PCPU(reqflags)
316 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
317 FAKE_MCOUNT(TF_RIP(%rsp))
319 subq $8,%rsp /* make same as interrupt frame */
320 movq %rsp,%rdi /* pass frame by reference */
321 call lapic_timer_always
322 addq $8,%rsp /* turn into trapframe */
324 incl PCPU(cnt) + V_TIMER
325 movq PCPU(curthread),%rbx
326 testl $-1,TD_CRITCOUNT(%rbx)
328 testl $-1,TD_NEST_COUNT(%rbx)
330 subq $8,%rsp /* make same as interrupt frame */
331 movq %rsp,%rdi /* pass frame by reference */
332 incl PCPU(intr_nesting_level)
333 incl TD_CRITCOUNT(%rbx)
335 call lapic_timer_process_frame
336 decl TD_CRITCOUNT(%rbx)
337 decl PCPU(intr_nesting_level)
338 addq $8,%rsp /* turn into trapframe */
342 orl $RQF_TIMER,PCPU(reqflags)
544 /* variables used by stop_cpus()/restart_cpus()/Xcpustop */
545 .globl stopped_cpus, started_cpus
551 .globl CNAME(cpustop_restartfunc)
552 CNAME(cpustop_restartfunc):