x86_64 intr_abi: Reindent
[dragonfly.git] / sys / platform / pc64 / apic / apic_abi.c
1 /*
2  * Copyright (c) 1991 The Regents of the University of California.
3  * Copyright (c) 1996, by Steve Passe.  All rights reserved.
4  * Copyright (c) 2005,2008 The DragonFly Project.  All rights reserved.
5  * All rights reserved.
6  * 
7  * This code is derived from software contributed to The DragonFly Project
8  * by Matthew Dillon <dillon@backplane.com>
9  *
10  * This code is derived from software contributed to Berkeley by
11  * William Jolitz.
12  * 
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  * 3. Neither the name of The DragonFly Project nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific, prior written permission.
26  * 
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
31  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
35  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * $DragonFly: src/sys/platform/pc64/apic/apic_abi.c,v 1.1 2008/08/29 17:07:12 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/machintr.h>
47 #include <sys/interrupt.h>
48 #include <sys/bus.h>
49
50 #include <machine/smp.h>
51 #include <machine/segments.h>
52 #include <machine/md_var.h>
53 #include <machine/clock.h>      /* apic_8254_intr */
54 #include <machine_base/isa/intr_machdep.h>
55 #include <machine_base/icu/icu.h>
56 #include <machine/globaldata.h>
57
58 #include <sys/thread2.h>
59
60 #include "apic_ipl.h"
61
62 #ifdef SMP /* APIC-IO */
63
64 extern void     APIC_INTREN(int);
65 extern void     APIC_INTRDIS(int);
66
67 extern inthand_t
68         IDTVEC(apic_fastintr0), IDTVEC(apic_fastintr1),
69         IDTVEC(apic_fastintr2), IDTVEC(apic_fastintr3),
70         IDTVEC(apic_fastintr4), IDTVEC(apic_fastintr5),
71         IDTVEC(apic_fastintr6), IDTVEC(apic_fastintr7),
72         IDTVEC(apic_fastintr8), IDTVEC(apic_fastintr9),
73         IDTVEC(apic_fastintr10), IDTVEC(apic_fastintr11),
74         IDTVEC(apic_fastintr12), IDTVEC(apic_fastintr13),
75         IDTVEC(apic_fastintr14), IDTVEC(apic_fastintr15),
76         IDTVEC(apic_fastintr16), IDTVEC(apic_fastintr17),
77         IDTVEC(apic_fastintr18), IDTVEC(apic_fastintr19),
78         IDTVEC(apic_fastintr20), IDTVEC(apic_fastintr21),
79         IDTVEC(apic_fastintr22), IDTVEC(apic_fastintr23);
80
81 static int      apic_setvar(int, const void *);
82 static int      apic_getvar(int, void *);
83 static int      apic_vectorctl(int, int, int);
84 static void     apic_finalize(void);
85 static void     apic_cleanup(void);
86
87 static inthand_t *apic_fastintr[APIC_HWI_VECTORS] = {
88         &IDTVEC(apic_fastintr0), &IDTVEC(apic_fastintr1),
89         &IDTVEC(apic_fastintr2), &IDTVEC(apic_fastintr3),
90         &IDTVEC(apic_fastintr4), &IDTVEC(apic_fastintr5),
91         &IDTVEC(apic_fastintr6), &IDTVEC(apic_fastintr7),
92         &IDTVEC(apic_fastintr8), &IDTVEC(apic_fastintr9),
93         &IDTVEC(apic_fastintr10), &IDTVEC(apic_fastintr11),
94         &IDTVEC(apic_fastintr12), &IDTVEC(apic_fastintr13),
95         &IDTVEC(apic_fastintr14), &IDTVEC(apic_fastintr15),
96         &IDTVEC(apic_fastintr16), &IDTVEC(apic_fastintr17),
97         &IDTVEC(apic_fastintr18), &IDTVEC(apic_fastintr19),
98         &IDTVEC(apic_fastintr20), &IDTVEC(apic_fastintr21),
99         &IDTVEC(apic_fastintr22), &IDTVEC(apic_fastintr23)
100 };
101
102 static int      apic_imcr_present;
103
104 struct machintr_abi MachIntrABI_APIC = {
105         MACHINTR_APIC,
106         .intrdis        = APIC_INTRDIS,
107         .intren         = APIC_INTREN,
108         .vectorctl      = apic_vectorctl,
109         .setvar         = apic_setvar,
110         .getvar         = apic_getvar,
111         .finalize       = apic_finalize,
112         .cleanup        = apic_cleanup
113 };
114
115 static int
116 apic_setvar(int varid, const void *buf)
117 {
118         int error = 0;
119
120         switch(varid) {
121         case MACHINTR_VAR_IMCR_PRESENT:
122                 apic_imcr_present = *(const int *)buf;
123                 break;
124
125         default:
126                 error = ENOENT;
127                 break;
128         }
129         return error;
130 }
131
132 static int
133 apic_getvar(int varid, void *buf)
134 {
135         int error = 0;
136
137         switch(varid) {
138         case MACHINTR_VAR_IMCR_PRESENT:
139                 *(int *)buf = apic_imcr_present;
140                 break;
141
142         default:
143                 error = ENOENT;
144                 break;
145         }
146         return error;
147 }
148
149 /*
150  * Called before interrupts are physically enabled, this routine does the
151  * final configuration of the BSP's local APIC:
152  *
153  *  - disable 'pic mode'.
154  *  - disable 'virtual wire mode'.
155  *  - enable NMI.
156  */
157 static void
158 apic_finalize(void)
159 {
160         uint32_t temp;
161
162         /*
163          * If an IMCR is present, program bit 0 to disconnect the 8259
164          * from the BSP.  The 8259 may still be connected to LINT0 on
165          * the BSP's LAPIC.
166          */
167         if (apic_imcr_present) {
168                 outb(0x22, 0x70);       /* select IMCR */
169                 outb(0x23, 0x01);       /* disconnect 8259 */
170         }
171
172         /*
173          * Setup lint0 (the 8259 'virtual wire' connection).  We
174          * mask the interrupt, completing the disconnection of the
175          * 8259.
176          */
177         temp = lapic->lvt_lint0;
178         temp |= APIC_LVT_MASKED;
179         lapic->lvt_lint0 = temp;
180
181         /*
182          * Setup lint1 to handle an NMI 
183          */
184         temp = lapic->lvt_lint1;
185         temp &= ~APIC_LVT_MASKED;
186         lapic->lvt_lint1 = temp;
187
188         if (bootverbose)
189                 apic_dump("bsp_apic_configure()");
190 }
191
192 /*
193  * This routine is called after physical interrupts are enabled but before
194  * the critical section is released.  We need to clean out any interrupts
195  * that had already been posted to the cpu.
196  */
197 static void
198 apic_cleanup(void)
199 {
200         mdcpu->gd_fpending = 0;
201 }
202
203 static int
204 apic_vectorctl(int op, int intr, int flags)
205 {
206         int error;
207         int vector;
208         int select;
209         uint32_t value;
210         u_long ef;
211
212         if (intr < 0 || intr >= APIC_HWI_VECTORS)
213                 return EINVAL;
214
215         ef = read_rflags();
216         cpu_disable_intr();
217         error = 0;
218
219         switch(op) {
220         case MACHINTR_VECTOR_SETUP:
221                 vector = IDT_OFFSET + intr;
222                 setidt(vector, apic_fastintr[intr], SDT_SYSIGT, SEL_KPL, 0);
223
224                 /*
225                  * Now reprogram the vector in the IO APIC.  In order to avoid
226                  * losing an EOI for a level interrupt, which is vector based,
227                  * make sure that the IO APIC is programmed for edge-triggering
228                  * first, then reprogrammed with the new vector.  This should
229                  * clear the IRR bit.
230                  */
231                 if (int_to_apicintpin[intr].ioapic >= 0) {
232                         imen_lock();
233
234                         select = int_to_apicintpin[intr].redirindex;
235                         value = io_apic_read(int_to_apicintpin[intr].ioapic,
236                                              select);
237                         value |= IOART_INTMSET;
238
239                         io_apic_write(int_to_apicintpin[intr].ioapic,
240                                       select, (value & ~APIC_TRIGMOD_MASK));
241                         io_apic_write(int_to_apicintpin[intr].ioapic,
242                                       select, (value & ~IOART_INTVEC) | vector);
243
244                         imen_unlock();
245                 }
246
247                 machintr_intren(intr);
248                 break;
249
250         case MACHINTR_VECTOR_TEARDOWN:
251                 /*
252                  * Teardown an interrupt vector.  The vector should already be
253                  * installed in the cpu's IDT, but make sure.
254                  */
255                 machintr_intrdis(intr);
256
257                 vector = IDT_OFFSET + intr;
258                 setidt(vector, apic_fastintr[intr], SDT_SYSIGT, SEL_KPL, 0);
259
260                 /*
261                  * And then reprogram the IO APIC to point to the SLOW vector
262                  * (it may have previously been pointed to the FAST version of
263                  * the vector).  This will allow us to keep track of spurious
264                  * interrupts.
265                  *
266                  * In order to avoid losing an EOI for a level interrupt, which
267                  * is vector based, make sure that the IO APIC is programmed for
268                  * edge-triggering first, then reprogrammed with the new vector.
269                  * This should clear the IRR bit.
270                  */
271                 if (int_to_apicintpin[intr].ioapic >= 0) {
272                         imen_lock();
273
274                         select = int_to_apicintpin[intr].redirindex;
275                         value = io_apic_read(int_to_apicintpin[intr].ioapic,
276                                              select);
277
278                         io_apic_write(int_to_apicintpin[intr].ioapic,
279                                       select, (value & ~APIC_TRIGMOD_MASK));
280                         io_apic_write(int_to_apicintpin[intr].ioapic,
281                                       select, (value & ~IOART_INTVEC) | vector);
282
283                         imen_unlock();
284                 }
285                 break;
286
287         case MACHINTR_VECTOR_SETDEFAULT:
288                 /*
289                  * This is a just-in-case an int pin is running through the 8259
290                  * when we don't expect it to, or an IO APIC pin somehow wound
291                  * up getting enabled without us specifically programming it in
292                  * this ABI.  Note that IO APIC pins are by default programmed
293                  * to IDT_OFFSET + intr.
294                  */
295                 vector = IDT_OFFSET + intr;
296                 setidt(vector, apic_fastintr[intr], SDT_SYSIGT, SEL_KPL, 0);
297                 break;
298
299         default:
300                 error = EOPNOTSUPP;
301                 break;
302         }
303
304         write_rflags(ef);
305         return error;
306 }
307
308 #endif  /* SMP */