751daced9f42d53eb2661981c23adcc427137213
[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 "icu.h"
58 #include "icu_ipl.h"
59
60 extern struct machintr_abi MachIntrABI_APIC;
61
62 extern inthand_t
63         IDTVEC(icu_intr0),      IDTVEC(icu_intr1),
64         IDTVEC(icu_intr2),      IDTVEC(icu_intr3),
65         IDTVEC(icu_intr4),      IDTVEC(icu_intr5),
66         IDTVEC(icu_intr6),      IDTVEC(icu_intr7),
67         IDTVEC(icu_intr8),      IDTVEC(icu_intr9),
68         IDTVEC(icu_intr10),     IDTVEC(icu_intr11),
69         IDTVEC(icu_intr12),     IDTVEC(icu_intr13),
70         IDTVEC(icu_intr14),     IDTVEC(icu_intr15);
71
72 static inthand_t *icu_intr[ICU_HWI_VECTORS] = {
73         &IDTVEC(icu_intr0),     &IDTVEC(icu_intr1),
74         &IDTVEC(icu_intr2),     &IDTVEC(icu_intr3),
75         &IDTVEC(icu_intr4),     &IDTVEC(icu_intr5),
76         &IDTVEC(icu_intr6),     &IDTVEC(icu_intr7),
77         &IDTVEC(icu_intr8),     &IDTVEC(icu_intr9),
78         &IDTVEC(icu_intr10),    &IDTVEC(icu_intr11),
79         &IDTVEC(icu_intr12),    &IDTVEC(icu_intr13),
80         &IDTVEC(icu_intr14),    &IDTVEC(icu_intr15)
81 };
82
83 extern void     ICU_INTREN(int);
84 extern void     ICU_INTRDIS(int);
85
86 static int      icu_vectorctl(int, int, int);
87 static int      icu_setvar(int, const void *);
88 static int      icu_getvar(int, void *);
89 static void     icu_finalize(void);
90 static void     icu_cleanup(void);
91 static void     icu_setdefault(void);
92
93 struct machintr_abi MachIntrABI_ICU = {
94         MACHINTR_ICU,
95         .intrdis        = ICU_INTRDIS,
96         .intren         = ICU_INTREN,
97         .vectorctl      = icu_vectorctl,
98         .setvar         = icu_setvar,
99         .getvar         = icu_getvar,
100         .finalize       = icu_finalize,
101         .cleanup        = icu_cleanup,
102         .setdefault     = icu_setdefault
103 };
104
105 static int      icu_imcr_present;
106
107 /*
108  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
109  */
110 static int
111 icu_setvar(int varid, const void *buf)
112 {
113         int error = 0;
114         
115         switch(varid) {
116         case MACHINTR_VAR_IMCR_PRESENT:
117                 icu_imcr_present = *(const int *)buf;
118                 break;
119
120         default:
121                 error = ENOENT;
122                 break;
123         }
124         return error;
125 }
126
127 static int
128 icu_getvar(int varid, void *buf)
129 {
130         int error = 0;
131         
132         switch(varid) {
133         case MACHINTR_VAR_IMCR_PRESENT:
134                 *(int *)buf = icu_imcr_present;
135                 break;
136
137         default:
138                 error = ENOENT;
139                 break;
140         }
141         return error;
142 }
143
144 /*
145  * Called before interrupts are physically enabled
146  */
147 static void
148 icu_finalize(void)
149 {
150         int intr;
151
152 #ifdef SMP
153         if (apic_io_enable) {
154                 KKASSERT(MachIntrABI.type == MACHINTR_ICU);
155                 MachIntrABI_APIC.setvar(MACHINTR_VAR_IMCR_PRESENT,
156                                         &icu_imcr_present);
157                 MachIntrABI_APIC.finalize();
158                 return;
159         }
160 #endif
161
162         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
163                 machintr_intrdis(intr);
164         machintr_intren(ICU_IRQ_SLAVE);
165
166 #if defined(SMP)
167         /*
168          * If an IMCR is present, programming bit 0 disconnects the 8259
169          * from the BSP.  The 8259 may still be connected to LINT0 on the
170          * BSP's LAPIC.
171          *
172          * If we are running SMP the LAPIC is active, try to use virtual
173          * wire mode so we can use other interrupt sources within the LAPIC
174          * in addition to the 8259.
175          */
176         if (icu_imcr_present) {
177                 outb(0x22, 0x70);
178                 outb(0x23, 0x01);
179         }
180 #endif
181 }
182
183 /*
184  * Called after interrupts physically enabled but before the
185  * critical section is released.
186  */
187 static void
188 icu_cleanup(void)
189 {
190         bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
191 }
192
193 static int
194 icu_vectorctl(int op, int intr, int flags)
195 {
196         int error;
197         register_t ef;
198
199         if (intr < 0 || intr >= ICU_HWI_VECTORS || intr == ICU_IRQ_SLAVE)
200                 return EINVAL;
201
202         ef = read_rflags();
203         cpu_disable_intr();
204         error = 0;
205
206         switch(op) {
207         case MACHINTR_VECTOR_SETUP:
208                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYSIGT,
209                        SEL_KPL, 0);
210                 machintr_intren(intr);
211                 break;
212
213         case MACHINTR_VECTOR_TEARDOWN:
214                 machintr_intrdis(intr);
215                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYSIGT,
216                        SEL_KPL, 0);
217                 break;
218
219         default:
220                 error = EOPNOTSUPP;
221                 break;
222         }
223         write_rflags(ef);
224         return error;
225 }
226
227 static void
228 icu_setdefault(void)
229 {
230         int intr;
231
232         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
233                 if (intr == ICU_IRQ_SLAVE)
234                         continue;
235                 setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYSIGT,
236                        SEL_KPL, 0);
237         }
238 }