Fix typos.
[dragonfly.git] / sys / i386 / icu / icu_vector.s
1 /*
2  *      from: vector.s, 386BSD 0.1 unknown origin
3  * $FreeBSD: src/sys/i386/isa/icu_vector.s,v 1.14.2.2 2000/07/18 21:12:42 dfr Exp $
4  * $DragonFly: src/sys/i386/icu/Attic/icu_vector.s,v 1.25 2005/11/04 21:16:59 dillon Exp $
5  */
6 /*
7  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
8  */
9
10 #include "use_npx.h"
11 #include "opt_auto_eoi.h"
12
13 #include <machine/asmacros.h>
14 #include <machine/ipl.h>
15 #include <machine/lock.h>
16 #include <machine/psl.h>
17 #include <machine/trap.h>
18
19 #include <i386/icu/icu.h>
20 #include <bus/isa/i386/isa.h>
21
22 #include "assym.s"
23
24 #ifndef APIC_IO
25
26 #define ICU_IMR_OFFSET          1       /* IO_ICU{1,2} + 1 */
27
28 #define ICU_EOI                 0x20    /* XXX - define elsewhere */
29
30 #define IRQ_LBIT(irq_num)       (1 << (irq_num))
31 #define IRQ_BIT(irq_num)        (1 << ((irq_num) % 8))
32 #define IRQ_BYTE(irq_num)       ((irq_num) >> 3)
33
34 #ifdef AUTO_EOI_1
35 #define ENABLE_ICU1             /* use auto-EOI to reduce i/o */
36 #define OUTB_ICU1
37 #else
38 #define ENABLE_ICU1                                                     \
39         movb    $ICU_EOI,%al ;  /* as soon as possible send EOI ... */  \
40         OUTB_ICU1 ;             /* ... to clear in service bit */       \
41
42 #define OUTB_ICU1                                                       \
43         outb    %al,$IO_ICU1 ;                                          \
44
45 #endif
46
47 #ifdef AUTO_EOI_2
48 /*
49  * The data sheet says no auto-EOI on slave, but it sometimes works.
50  */
51 #define ENABLE_ICU1_AND_2       ENABLE_ICU1
52 #else
53 #define ENABLE_ICU1_AND_2                                               \
54         movb    $ICU_EOI,%al ;  /* as above */                          \
55         outb    %al,$IO_ICU2 ;  /* but do second icu first ... */       \
56         OUTB_ICU1 ;     /* ... then first icu (if !AUTO_EOI_1) */       \
57
58 #endif
59
60 /*
61  * Macro helpers
62  */
63 #define PUSH_FRAME                                                      \
64         pushl   $0 ;            /* dummy error code */                  \
65         pushl   $0 ;            /* dummy trap type */                   \
66         pushal ;                /* 8 registers */                       \
67         pushl   %ds ;                                                   \
68         pushl   %es ;                                                   \
69         pushl   %fs ;                                                   \
70         mov     $KDSEL,%ax ;                                            \
71         mov     %ax,%ds ;                                               \
72         mov     %ax,%es ;                                               \
73         mov     $KPSEL,%ax ;                                            \
74         mov     %ax,%fs ;                                               \
75
76 #define PUSH_DUMMY                                                      \
77         pushfl ;                /* phys int frame / flags */            \
78         pushl %cs ;             /* phys int frame / cs */               \
79         pushl   12(%esp) ;      /* original caller eip */               \
80         pushl   $0 ;            /* dummy error code */                  \
81         pushl   $0 ;            /* dummy trap type */                   \
82         subl    $12*4,%esp ;    /* pushal + 3 seg regs (dummy) + CPL */ \
83
84 /*
85  * Warning: POP_FRAME can only be used if there is no chance of a
86  * segment register being changed (e.g. by procfs), which is why syscalls
87  * have to use doreti.
88  */
89 #define POP_FRAME                                                       \
90         popl    %fs ;                                                   \
91         popl    %es ;                                                   \
92         popl    %ds ;                                                   \
93         popal ;                                                         \
94         addl    $2*4,%esp ;     /* dummy trap & error codes */          \
95
96 #define POP_DUMMY                                                       \
97         addl    $17*4,%esp ;                                            \
98
99 #define MASK_IRQ(icu, irq_num)                                          \
100         ICU_IMASK_LOCK ;                                                \
101         movb    icu_imen + IRQ_BYTE(irq_num),%al ;                      \
102         orb     $IRQ_BIT(irq_num),%al ;                                 \
103         movb    %al,icu_imen + IRQ_BYTE(irq_num) ;                      \
104         outb    %al,$icu+ICU_IMR_OFFSET ;                               \
105         ICU_IMASK_UNLOCK ;                                              \
106
107 #define UNMASK_IRQ(icu, irq_num)                                        \
108         cmpl    $0,%eax ;                                               \
109         jnz     8f ;                                                    \
110         ICU_IMASK_LOCK ;                                                \
111         movb    icu_imen + IRQ_BYTE(irq_num),%al ;                      \
112         andb    $~IRQ_BIT(irq_num),%al ;                                \
113         movb    %al,icu_imen + IRQ_BYTE(irq_num) ;                      \
114         outb    %al,$icu+ICU_IMR_OFFSET ;                               \
115         ICU_IMASK_UNLOCK ;                                              \
116 8: ;                                                                    \
117         
118 /*
119  * Fast interrupt call handlers run in the following sequence:
120  *
121  *      - Push the trap frame required by doreti.
122  *      - Mask the interrupt and reenable its source.
123  *      - If we cannot take the interrupt set its fpending bit and
124  *        doreti.
125  *      - If we can take the interrupt clear its fpending bit,
126  *        call the handler, then unmask the interrupt and doreti.
127  *
128  *      YYY can cache gd base pointer instead of using hidden %fs
129  *      prefixes.
130  */
131
132 #define FAST_INTR(irq_num, vec_name, icu, enable_icus)                   \
133         .text ;                                                         \
134         SUPERALIGN_TEXT ;                                               \
135 IDTVEC(vec_name) ;                                                      \
136         PUSH_FRAME ;                                                    \
137         FAKE_MCOUNT(13*4(%esp)) ;                                       \
138         MASK_IRQ(icu, irq_num) ;                                        \
139         enable_icus ;                                                   \
140         movl    PCPU(curthread),%ebx ;                                  \
141         pushl   $0 ;                    /* DUMMY CPL FOR DORETI */      \
142         cmpl    $TDPRI_CRIT,TD_PRI(%ebx) ;                              \
143         jl      2f ;                                                    \
144 1: ;                                                                    \
145         /* set pending bit and return, leave interrupt masked */        \
146         orl     $IRQ_LBIT(irq_num),PCPU(fpending) ;                     \
147         orl     $RQF_INTPEND, PCPU(reqflags) ;                          \
148         jmp     5f ;                                                    \
149 2: ;                                                                    \
150         /* clear pending bit, run handler */                            \
151         andl    $~IRQ_LBIT(irq_num),PCPU(fpending) ;                    \
152         pushl   $irq_num ;                                              \
153         call    ithread_fast_handler ;  /* returns 0 to unmask int */   \
154         addl    $4,%esp ;                                               \
155         UNMASK_IRQ(icu, irq_num) ;                                      \
156 5: ;                                                                    \
157         MEXITCOUNT ;                                                    \
158         jmp     doreti ;                                                \
159
160 /*
161  * Slow interrupt call handlers run in the following sequence:
162  *
163  *      - Push the trap frame required by doreti.
164  *      - Mask the interrupt and reenable its source.
165  *      - If we cannot take the interrupt set its ipending bit and
166  *        doreti.  In addition to checking for a critical section
167  *        and cpl mask we also check to see if the thread is still
168  *        running.
169  *      - If we can take the interrupt clear its ipending bit
170  *        and schedule its thread.  Leave interrupts masked and doreti.
171  *
172  *      sched_ithd() is called with interrupts enabled and outside of a
173  *      critical section (so it can preempt us).
174  *
175  *      YYY sched_ithd may preempt us synchronously (fix interrupt stacking)
176  *
177  *      Note that intr_nesting_level is not bumped during sched_ithd because
178  *      blocking allocations are allowed in the preemption case.
179  *
180  *      YYY can cache gd base pointer instead of using hidden %fs
181  *      prefixes.
182  */
183
184 #define SLOW_INTR(irq_num, vec_name, icu, enable_icus)                   \
185         .text ;                                                         \
186         SUPERALIGN_TEXT ;                                               \
187 IDTVEC(vec_name) ;                                                      \
188         PUSH_FRAME ;                                                    \
189         FAKE_MCOUNT(13*4(%esp)) ;                                       \
190         MASK_IRQ(icu, irq_num) ;                                        \
191         incl    PCPU(cnt) + V_INTR ;                                    \
192         enable_icus ;                                                   \
193         movl    PCPU(curthread),%ebx ;                                  \
194         pushl   $0 ;                    /* DUMMY CPL FOR DORETI */      \
195         cmpl    $TDPRI_CRIT,TD_PRI(%ebx) ;                              \
196         jl      2f ;                                                    \
197 1: ;                                                                    \
198         /* set the pending bit and return, leave interrupt masked */    \
199         orl     $IRQ_LBIT(irq_num), PCPU(ipending) ;                    \
200         orl     $RQF_INTPEND, PCPU(reqflags) ;                          \
201         jmp     5f ;                                                    \
202 2: ;                                                                    \
203         /* set running bit, clear pending bit, run handler */           \
204         andl    $~IRQ_LBIT(irq_num), PCPU(ipending) ;                   \
205         sti ;                                                           \
206         pushl   $irq_num ;                                              \
207         call    sched_ithd ;                                            \
208         addl    $4,%esp ;                                               \
209 5: ;                                                                    \
210         MEXITCOUNT ;                                                    \
211         jmp     doreti ;                                                \
212
213 /*
214  * Unmask a slow interrupt.  This function is used by interrupt threads
215  * after they have descheduled themselves to reenable interrupts and
216  * possibly cause a reschedule to occur.
217  */
218
219 #define INTR_UNMASK(irq_num, vec_name, icu)                             \
220         .text ;                                                         \
221         SUPERALIGN_TEXT ;                                               \
222 IDTVEC(vec_name) ;                                                      \
223         pushl %ebp ;     /* frame for ddb backtrace */                  \
224         movl    %esp, %ebp ;                                            \
225         subl    %eax, %eax ;                                            \
226         UNMASK_IRQ(icu, irq_num) ;                                      \
227         popl %ebp ;                                                     \
228         ret ;                                                           \
229
230 MCOUNT_LABEL(bintr)
231         FAST_INTR(0,icu_fastintr0, IO_ICU1, ENABLE_ICU1)
232         FAST_INTR(1,icu_fastintr1, IO_ICU1, ENABLE_ICU1)
233         FAST_INTR(2,icu_fastintr2, IO_ICU1, ENABLE_ICU1)
234         FAST_INTR(3,icu_fastintr3, IO_ICU1, ENABLE_ICU1)
235         FAST_INTR(4,icu_fastintr4, IO_ICU1, ENABLE_ICU1)
236         FAST_INTR(5,icu_fastintr5, IO_ICU1, ENABLE_ICU1)
237         FAST_INTR(6,icu_fastintr6, IO_ICU1, ENABLE_ICU1)
238         FAST_INTR(7,icu_fastintr7, IO_ICU1, ENABLE_ICU1)
239         FAST_INTR(8,icu_fastintr8, IO_ICU2, ENABLE_ICU1_AND_2)
240         FAST_INTR(9,icu_fastintr9, IO_ICU2, ENABLE_ICU1_AND_2)
241         FAST_INTR(10,icu_fastintr10, IO_ICU2, ENABLE_ICU1_AND_2)
242         FAST_INTR(11,icu_fastintr11, IO_ICU2, ENABLE_ICU1_AND_2)
243         FAST_INTR(12,icu_fastintr12, IO_ICU2, ENABLE_ICU1_AND_2)
244         FAST_INTR(13,icu_fastintr13, IO_ICU2, ENABLE_ICU1_AND_2)
245         FAST_INTR(14,icu_fastintr14, IO_ICU2, ENABLE_ICU1_AND_2)
246         FAST_INTR(15,icu_fastintr15, IO_ICU2, ENABLE_ICU1_AND_2)
247
248         SLOW_INTR(0,icu_slowintr0, IO_ICU1, ENABLE_ICU1)
249         SLOW_INTR(1,icu_slowintr1, IO_ICU1, ENABLE_ICU1)
250         SLOW_INTR(2,icu_slowintr2, IO_ICU1, ENABLE_ICU1)
251         SLOW_INTR(3,icu_slowintr3, IO_ICU1, ENABLE_ICU1)
252         SLOW_INTR(4,icu_slowintr4, IO_ICU1, ENABLE_ICU1)
253         SLOW_INTR(5,icu_slowintr5, IO_ICU1, ENABLE_ICU1)
254         SLOW_INTR(6,icu_slowintr6, IO_ICU1, ENABLE_ICU1)
255         SLOW_INTR(7,icu_slowintr7, IO_ICU1, ENABLE_ICU1)
256         SLOW_INTR(8,icu_slowintr8, IO_ICU2, ENABLE_ICU1_AND_2)
257         SLOW_INTR(9,icu_slowintr9, IO_ICU2, ENABLE_ICU1_AND_2)
258         SLOW_INTR(10,icu_slowintr10, IO_ICU2, ENABLE_ICU1_AND_2)
259         SLOW_INTR(11,icu_slowintr11, IO_ICU2, ENABLE_ICU1_AND_2)
260         SLOW_INTR(12,icu_slowintr12, IO_ICU2, ENABLE_ICU1_AND_2)
261         SLOW_INTR(13,icu_slowintr13, IO_ICU2, ENABLE_ICU1_AND_2)
262         SLOW_INTR(14,icu_slowintr14, IO_ICU2, ENABLE_ICU1_AND_2)
263         SLOW_INTR(15,icu_slowintr15, IO_ICU2, ENABLE_ICU1_AND_2)
264
265 MCOUNT_LABEL(eintr)
266
267         .data
268
269         .text
270
271 #endif