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 $
4 * $DragonFly: src/sys/platform/pc32/apic/apic_vector.s,v 1.39 2008/08/02 01:14:43 dillon Exp $
9 #include "opt_auto_eoi.h"
12 #include <machine/asmacros.h>
13 #include <machine/lock.h>
14 #include <machine/psl.h>
15 #include <machine/trap.h>
17 #include <machine_base/icu/icu.h>
18 #include <bus/isa/isa.h>
24 #include <machine/smp.h>
25 #include <machine_base/isa/intr_machdep.h>
27 /* convert an absolute IRQ# into a bitmask */
28 #define IRQ_LBIT(irq_num) (1 << (irq_num))
30 /* make an index into the IO APIC from the IRQ# */
31 #define REDTBL_IDX(irq_num) (0x10 + ((irq_num) * 2))
34 #define MPLOCKED lock ;
39 #define APIC_PUSH_FRAME \
40 PUSH_FRAME ; /* 15 regs + space for 5 extras */ \
41 movq $0,TF_XFLAGS(%rsp) ; \
42 movq $0,TF_TRAPNO(%rsp) ; \
43 movq $0,TF_ADDR(%rsp) ; \
44 movq $0,TF_FLAGS(%rsp) ; \
45 movq $0,TF_ERR(%rsp) ; \
49 * JG stale? Warning: POP_FRAME can only be used if there is no chance of a
50 * segment register being changed (e.g. by procfs), which is why syscalls
53 #define APIC_POP_FRAME POP_FRAME
55 /* sizeof(struct apic_intmapinfo) == 24 */
56 #define IOAPICADDR(irq_num) CNAME(int_to_apicintpin) + 24 * (irq_num) + 8
57 #define REDIRIDX(irq_num) CNAME(int_to_apicintpin) + 24 * (irq_num) + 16
59 #define MASK_IRQ(irq_num) \
60 APIC_IMASK_LOCK ; /* into critical reg */ \
61 testl $IRQ_LBIT(irq_num), apic_imen ; \
62 jne 7f ; /* masked, don't mask */ \
63 orl $IRQ_LBIT(irq_num), apic_imen ; /* set the mask bit */ \
64 movq IOAPICADDR(irq_num), %rcx ; /* ioapic addr */ \
65 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
66 movl %eax, (%rcx) ; /* write the index */ \
67 movl IOAPIC_WINDOW(%rcx), %eax ; /* current value */ \
68 orl $IOART_INTMASK, %eax ; /* set the mask */ \
69 movl %eax, IOAPIC_WINDOW(%rcx) ; /* new value */ \
70 7: ; /* already masked */ \
74 * Test to see whether we are handling an edge or level triggered INT.
75 * Level-triggered INTs must still be masked as we don't clear the source,
76 * and the EOI cycle would cause redundant INTs to occur.
78 #define MASK_LEVEL_IRQ(irq_num) \
79 testl $IRQ_LBIT(irq_num), apic_pin_trigger ; \
80 jz 9f ; /* edge, don't mask */ \
85 * Test to see if the source is currntly masked, clear if so.
87 #define UNMASK_IRQ(irq_num) \
90 APIC_IMASK_LOCK ; /* into critical reg */ \
91 testl $IRQ_LBIT(irq_num), apic_imen ; \
92 je 7f ; /* bit clear, not masked */ \
93 andl $~IRQ_LBIT(irq_num), apic_imen ;/* clear mask bit */ \
94 movq IOAPICADDR(irq_num),%rcx ; /* ioapic addr */ \
95 movl REDIRIDX(irq_num), %eax ; /* get the index */ \
96 movl %eax,(%rcx) ; /* write the index */ \
97 movl IOAPIC_WINDOW(%rcx),%eax ; /* current value */ \
98 andl $~IOART_INTMASK,%eax ; /* clear the mask */ \
99 movl %eax,IOAPIC_WINDOW(%rcx) ; /* new value */ \
101 APIC_IMASK_UNLOCK ; \
107 * Fast interrupt call handlers run in the following sequence:
109 * - Push the trap frame required by doreti
110 * - Mask the interrupt and reenable its source
111 * - If we cannot take the interrupt set its fpending bit and
112 * doreti. Note that we cannot mess with mp_lock at all
113 * if we entered from a critical section!
114 * - If we can take the interrupt clear its fpending bit,
115 * call the handler, then unmask and doreti.
117 * YYY can cache gd base opitner instead of using hidden %fs prefixes.
120 #define FAST_INTR(irq_num, vec_name) \
125 FAKE_MCOUNT(15*4(%esp)) ; \
126 MASK_LEVEL_IRQ(irq_num) ; \
128 movl $0, LA_EOI(%rax) ; \
129 movq PCPU(curthread),%rbx ; \
130 testl $-1,TD_NEST_COUNT(%rbx) ; \
132 cmpl $TDPRI_CRIT,TD_PRI(%rbx) ; \
135 /* in critical section, make interrupt pending */ \
136 /* set the pending bit and return, leave interrupt masked */ \
137 orl $IRQ_LBIT(irq_num),PCPU(fpending) ; \
138 orl $RQF_INTPEND,PCPU(reqflags) ; \
141 /* clear pending bit, run handler */ \
142 andl $~IRQ_LBIT(irq_num),PCPU(fpending) ; \
143 pushq $irq_num ; /* trapframe -> intrframe */ \
144 movq %rsp, %rdi ; /* pass frame by reference */ \
145 addl $TDPRI_CRIT,TD_PRI(%ebx) ; \
146 call ithread_fast_handler ; /* returns 0 to unmask */ \
147 subl $TDPRI_CRIT,TD_PRI(%ebx) ; \
148 addq $8, %rsp ; /* intrframe -> trapframe */ \
149 UNMASK_IRQ(irq_num) ; \
157 * Handle "spurious INTerrupts".
159 * This is different than the "spurious INTerrupt" generated by an
160 * 8259 PIC for missing INTs. See the APIC documentation for details.
161 * This routine should NOT do an 'EOI' cycle.
168 /* No EOI cycle used here */
174 * Handle TLB shootdowns.
182 movq %cr3, %rax /* invalidate the TLB */
186 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
193 * Executed by a CPU when it receives an Xcpustop IPI from another CPU,
195 * - Signals its receipt.
196 * - Waits for permission to restart.
197 * - Processing pending IPIQ events while waiting.
198 * - Signals its restart.
207 /* We save registers that are not preserved across function calls. */
208 /* JG can be re-written with mov's */
220 /* JGXXX switch to kernel %gs? */
221 pushl %ds /* save current data segment */
225 mov %ax, %ds /* use KERNEL data segment */
231 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
234 movl PCPU(cpuid), %eax
235 imull $PCB_SIZE, %eax
236 leaq CNAME(stoppcbs), %rdi
238 call CNAME(savectx) /* Save process context */
241 movl PCPU(cpuid), %eax
244 * Indicate that we have stopped and loop waiting for permission
245 * to start again. We must still process IPI events while in a
249 btsl %eax, stopped_cpus /* stopped_cpus |= (1<<id) */
251 andl $~RQF_IPIQ,PCPU(reqflags)
253 call lwkt_smp_stopped
255 btl %eax, started_cpus /* while (!(started_cpus & (1<<id))) */
259 btrl %eax, started_cpus /* started_cpus &= ~(1<<id) */
261 btrl %eax, stopped_cpus /* stopped_cpus &= ~(1<<id) */
266 movq CNAME(cpustop_restartfunc), %rax
269 movq $0, CNAME(cpustop_restartfunc) /* One-shot */
285 popl %ds /* restore previous data segment */
292 * For now just have one ipiq IPI, but what we really want is
293 * to have one for each source cpu to the APICs don't get stalled
294 * backlogging the requests.
302 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
303 FAKE_MCOUNT(15*4(%esp))
305 incl PCPU(cnt) + V_IPI
306 movq PCPU(curthread),%rbx
307 cmpl $TDPRI_CRIT,TD_PRI(%rbx)
309 subq $8,%rsp /* make same as interrupt frame */
310 movq %rsp,%rdi /* pass frame by reference */
311 incl PCPU(intr_nesting_level)
312 addl $TDPRI_CRIT,TD_PRI(%rbx)
313 call lwkt_process_ipiq_frame
314 subl $TDPRI_CRIT,TD_PRI(%rbx)
315 decl PCPU(intr_nesting_level)
316 addq $8,%rsp /* turn into trapframe */
320 orl $RQF_IPIQ,PCPU(reqflags)
331 movl $0, LA_EOI(%rax) /* End Of Interrupt to APIC */
332 FAKE_MCOUNT(15*4(%esp))
334 incl PCPU(cnt) + V_TIMER
335 movq PCPU(curthread),%rbx
336 cmpl $TDPRI_CRIT,TD_PRI(%rbx)
338 testl $-1,TD_NEST_COUNT(%rbx)
340 subq $8,%rsp /* make same as interrupt frame */
341 movq %rsp,%rdi /* pass frame by reference */
342 incl PCPU(intr_nesting_level)
343 addl $TDPRI_CRIT,TD_PRI(%rbx)
344 call lapic_timer_process_frame
345 subl $TDPRI_CRIT,TD_PRI(%rbx)
346 decl PCPU(intr_nesting_level)
347 addq $8,%rsp /* turn into trapframe */
351 orl $RQF_TIMER,PCPU(reqflags)
359 FAST_INTR(0,apic_fastintr0)
360 FAST_INTR(1,apic_fastintr1)
361 FAST_INTR(2,apic_fastintr2)
362 FAST_INTR(3,apic_fastintr3)
363 FAST_INTR(4,apic_fastintr4)
364 FAST_INTR(5,apic_fastintr5)
365 FAST_INTR(6,apic_fastintr6)
366 FAST_INTR(7,apic_fastintr7)
367 FAST_INTR(8,apic_fastintr8)
368 FAST_INTR(9,apic_fastintr9)
369 FAST_INTR(10,apic_fastintr10)
370 FAST_INTR(11,apic_fastintr11)
371 FAST_INTR(12,apic_fastintr12)
372 FAST_INTR(13,apic_fastintr13)
373 FAST_INTR(14,apic_fastintr14)
374 FAST_INTR(15,apic_fastintr15)
375 FAST_INTR(16,apic_fastintr16)
376 FAST_INTR(17,apic_fastintr17)
377 FAST_INTR(18,apic_fastintr18)
378 FAST_INTR(19,apic_fastintr19)
379 FAST_INTR(20,apic_fastintr20)
380 FAST_INTR(21,apic_fastintr21)
381 FAST_INTR(22,apic_fastintr22)
382 FAST_INTR(23,apic_fastintr23)
389 /* variables used by stop_cpus()/restart_cpus()/Xcpustop */
390 .globl stopped_cpus, started_cpus
396 .globl CNAME(cpustop_restartfunc)
397 CNAME(cpustop_restartfunc):
400 .globl apic_pin_trigger