MachIntrABI: Remove unused setvar/getvar interfaces
[dragonfly.git] / sys / platform / pc64 / icu / icu_abi.c
1 /*
2  * Copyright (c) 1991 The Regents of the University of California.
3  * Copyright (c) 2005,2008 The DragonFly Project.
4  * All rights reserved.
5  * 
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * This code is derived from software contributed to Berkeley by
10  * William Jolitz.
11  * 
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  * 3. Neither the name of The DragonFly Project nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific, prior written permission.
25  * 
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
30  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
32  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  * 
39  * $DragonFly: src/sys/platform/pc64/icu/icu_abi.c,v 1.1 2008/08/29 17:07:16 dillon Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/machintr.h>
46 #include <sys/interrupt.h>
47 #include <sys/bus.h>
48
49 #include <machine/segments.h>
50 #include <machine/md_var.h>
51 #include <machine/intr_machdep.h>
52 #include <machine/globaldata.h>
53 #include <machine/smp.h>
54
55 #include <sys/thread2.h>
56
57 #include <machine_base/icu/elcr_var.h>
58
59 #include <machine_base/icu/icu.h>
60 #include <machine_base/icu/icu_ipl.h>
61 #include <machine_base/apic/ioapic.h>
62
63 extern inthand_t
64         IDTVEC(icu_intr0),      IDTVEC(icu_intr1),
65         IDTVEC(icu_intr2),      IDTVEC(icu_intr3),
66         IDTVEC(icu_intr4),      IDTVEC(icu_intr5),
67         IDTVEC(icu_intr6),      IDTVEC(icu_intr7),
68         IDTVEC(icu_intr8),      IDTVEC(icu_intr9),
69         IDTVEC(icu_intr10),     IDTVEC(icu_intr11),
70         IDTVEC(icu_intr12),     IDTVEC(icu_intr13),
71         IDTVEC(icu_intr14),     IDTVEC(icu_intr15);
72
73 static inthand_t *icu_intr[ICU_HWI_VECTORS] = {
74         &IDTVEC(icu_intr0),     &IDTVEC(icu_intr1),
75         &IDTVEC(icu_intr2),     &IDTVEC(icu_intr3),
76         &IDTVEC(icu_intr4),     &IDTVEC(icu_intr5),
77         &IDTVEC(icu_intr6),     &IDTVEC(icu_intr7),
78         &IDTVEC(icu_intr8),     &IDTVEC(icu_intr9),
79         &IDTVEC(icu_intr10),    &IDTVEC(icu_intr11),
80         &IDTVEC(icu_intr12),    &IDTVEC(icu_intr13),
81         &IDTVEC(icu_intr14),    &IDTVEC(icu_intr15)
82 };
83
84 static struct icu_irqmap {
85         int                     im_type;        /* ICU_IMT_ */
86         enum intr_trigger       im_trig;
87 } icu_irqmaps[IDT_HWI_VECTORS];
88
89 #define ICU_IMT_UNUSED          0       /* KEEP THIS */
90 #define ICU_IMT_RESERVED        1
91 #define ICU_IMT_LINE            2
92 #define ICU_IMT_SYSCALL         3
93
94 extern void     ICU_INTREN(int);
95 extern void     ICU_INTRDIS(int);
96
97 extern int      imcr_present;
98
99 static int      icu_vectorctl(int, int, int);
100 static void     icu_finalize(void);
101 static void     icu_cleanup(void);
102 static void     icu_setdefault(void);
103 static void     icu_stabilize(void);
104 static void     icu_initmap(void);
105 static void     icu_intr_config(int, enum intr_trigger, enum intr_polarity);
106
107 struct machintr_abi MachIntrABI_ICU = {
108         MACHINTR_ICU,
109         .intrdis        = ICU_INTRDIS,
110         .intren         = ICU_INTREN,
111         .vectorctl      = icu_vectorctl,
112         .finalize       = icu_finalize,
113         .cleanup        = icu_cleanup,
114         .setdefault     = icu_setdefault,
115         .stabilize      = icu_stabilize,
116         .initmap        = icu_initmap,
117         .intr_config    = icu_intr_config
118 };
119
120 /*
121  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
122  */
123
124 /*
125  * Called before interrupts are physically enabled
126  */
127 static void
128 icu_stabilize(void)
129 {
130         int intr;
131
132         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
133                 machintr_intrdis(intr);
134         machintr_intren(ICU_IRQ_SLAVE);
135 }
136
137 /*
138  * Called after interrupts physically enabled but before the
139  * critical section is released.
140  */
141 static void
142 icu_cleanup(void)
143 {
144         bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
145 }
146
147 /*
148  * Called after stablize and cleanup; critical section is not
149  * held and interrupts are not physically disabled.
150  */
151 static void
152 icu_finalize(void)
153 {
154         KKASSERT(MachIntrABI.type == MACHINTR_ICU);
155         KKASSERT(!ioapic_enable);
156
157         /*
158          * If an IMCR is present, programming bit 0 disconnects the 8259
159          * from the BSP.  The 8259 may still be connected to LINT0 on the
160          * BSP's LAPIC.
161          *
162          * If we are running SMP the LAPIC is active, try to use virtual
163          * wire mode so we can use other interrupt sources within the LAPIC
164          * in addition to the 8259.
165          */
166         if (imcr_present) {
167                 outb(0x22, 0x70);
168                 outb(0x23, 0x01);
169         }
170 }
171
172 static int
173 icu_vectorctl(int op, int intr, int flags)
174 {
175         int error;
176         register_t ef;
177
178         if (intr < 0 || intr >= ICU_HWI_VECTORS || intr == ICU_IRQ_SLAVE)
179                 return EINVAL;
180
181         ef = read_rflags();
182         cpu_disable_intr();
183         error = 0;
184
185         switch(op) {
186         case MACHINTR_VECTOR_SETUP:
187                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYSIGT,
188                        SEL_KPL, 0);
189                 machintr_intren(intr);
190                 break;
191
192         case MACHINTR_VECTOR_TEARDOWN:
193                 machintr_intrdis(intr);
194                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYSIGT,
195                        SEL_KPL, 0);
196                 break;
197
198         default:
199                 error = EOPNOTSUPP;
200                 break;
201         }
202         write_rflags(ef);
203         return error;
204 }
205
206 static void
207 icu_setdefault(void)
208 {
209         int intr;
210
211         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
212                 if (intr == ICU_IRQ_SLAVE)
213                         continue;
214                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYSIGT,
215                        SEL_KPL, 0);
216         }
217 }
218
219 static void
220 icu_initmap(void)
221 {
222         int i;
223
224         for (i = 0; i < ICU_HWI_VECTORS; ++i)
225                 icu_irqmaps[i].im_type = ICU_IMT_LINE;
226         icu_irqmaps[ICU_IRQ_SLAVE].im_type = ICU_IMT_RESERVED;
227
228         if (elcr_found) {
229                 for (i = 0; i < ICU_HWI_VECTORS; ++i)
230                         icu_irqmaps[i].im_trig = elcr_read_trigger(i);
231         } else {
232                 /*
233                  * NOTE: Trigger mode does not matter at all
234                  */
235                 for (i = 0; i < ICU_HWI_VECTORS; ++i)
236                         icu_irqmaps[i].im_trig = INTR_TRIGGER_EDGE;
237         }
238         icu_irqmaps[IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type = ICU_IMT_SYSCALL;
239 }
240
241 static void
242 icu_intr_config(int irq, enum intr_trigger trig,
243     enum intr_polarity pola __unused)
244 {
245         struct icu_irqmap *map;
246
247         KKASSERT(trig == INTR_TRIGGER_EDGE || trig == INTR_TRIGGER_LEVEL);
248
249         KKASSERT(irq >= 0 && irq < IDT_HWI_VECTORS);
250         map = &icu_irqmaps[irq];
251
252         KKASSERT(map->im_type == ICU_IMT_LINE);
253
254         /* TODO: Check whether it is configured or not */
255
256         if (trig == map->im_trig)
257                 return;
258
259         if (bootverbose) {
260                 kprintf("ICU: irq %d, %s -> %s\n", irq,
261                         intr_str_trigger(map->im_trig),
262                         intr_str_trigger(trig));
263         }
264         map->im_trig = trig;
265
266         if (!elcr_found) {
267                 if (bootverbose)
268                         kprintf("ICU: no ELCR, skip irq %d config\n", irq);
269                 return;
270         }
271         elcr_write_trigger(irq, map->im_trig);
272 }