Rev 501 | Rev 513 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | #include <arch/types.h> |
||
11 | jermar | 30 | #include <arch/smp/apic.h> |
31 | #include <arch/smp/ap.h> |
||
34 | jermar | 32 | #include <arch/smp/mps.h> |
1 | jermar | 33 | #include <mm/page.h> |
34 | #include <time/delay.h> |
||
35 | #include <arch/interrupt.h> |
||
36 | #include <print.h> |
||
37 | #include <arch/asm.h> |
||
38 | #include <arch.h> |
||
39 | |||
458 | decky | 40 | #ifdef CONFIG_SMP |
16 | jermar | 41 | |
1 | jermar | 42 | /* |
512 | jermar | 43 | * Advanced Programmable Interrupt Controller for SMP systems. |
1 | jermar | 44 | * Tested on: |
112 | jermar | 45 | * Bochs 2.0.2 - Bochs 2.2 with 2-8 CPUs |
117 | jermar | 46 | * Simics 2.0.28 - Simics 2.2.14 2-4 CPUs |
1 | jermar | 47 | * ASUS P/I-P65UP5 + ASUS C-P55T2D REV. 1.41 with 2x 200Mhz Pentium CPUs |
437 | decky | 48 | * ASUS PCH-DL with 2x 3000Mhz Pentium 4 Xeon (HT) CPUs |
49 | * MSI K7D Master-L with 2x 2100MHz Athlon MP CPUs |
||
1 | jermar | 50 | */ |
51 | |||
52 | /* |
||
53 | * These variables either stay configured as initilalized, or are changed by |
||
54 | * the MP configuration code. |
||
55 | * |
||
56 | * Pay special attention to the volatile keyword. Without it, gcc -O2 would |
||
57 | * optimize the code too much and accesses to l_apic and io_apic, that must |
||
58 | * always be 32-bit, would use byte oriented instructions. |
||
59 | */ |
||
60 | volatile __u32 *l_apic = (__u32 *) 0xfee00000; |
||
61 | volatile __u32 *io_apic = (__u32 *) 0xfec00000; |
||
62 | |||
63 | __u32 apic_id_mask = 0; |
||
64 | |||
65 | int apic_poll_errors(void); |
||
66 | |||
67 | void apic_init(void) |
||
68 | { |
||
69 | __u32 tmp, id, i; |
||
70 | |||
71 | trap_register(VECTOR_APIC_SPUR, apic_spurious); |
||
72 | |||
73 | enable_irqs_function = io_apic_enable_irqs; |
||
74 | disable_irqs_function = io_apic_disable_irqs; |
||
75 | eoi_function = l_apic_eoi; |
||
76 | |||
77 | /* |
||
78 | * Configure interrupt routing. |
||
79 | * IRQ 0 remains masked as the time signal is generated by l_apic's themselves. |
||
80 | * Other interrupts will be forwarded to the lowest priority CPU. |
||
81 | */ |
||
82 | io_apic_disable_irqs(0xffff); |
||
83 | trap_register(VECTOR_CLK, l_apic_timer_interrupt); |
||
512 | jermar | 84 | for (i=0; i<16; i++) { |
1 | jermar | 85 | int pin; |
86 | |||
512 | jermar | 87 | if ((pin = smp_irq_to_pin(i)) != -1) { |
88 | io_apic_change_ioredtbl(pin,0xff,IVT_IRQBASE+i,LOPRI); |
||
89 | } |
||
1 | jermar | 90 | } |
91 | |||
92 | |||
93 | /* |
||
94 | * Ensure that io_apic has unique ID. |
||
95 | */ |
||
96 | tmp = io_apic_read(IOAPICID); |
||
97 | id = (tmp >> 24) & 0xf; |
||
98 | if ((1<<id) & apic_id_mask) { |
||
99 | int i; |
||
100 | |||
101 | for (i=0; i<15; i++) { |
||
102 | if (!((1<<i) & apic_id_mask)) { |
||
103 | io_apic_write(IOAPICID, (tmp & (~(0xf<<24))) | (i<<24)); |
||
104 | break; |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /* |
||
110 | * Configure the BSP's lapic. |
||
111 | */ |
||
112 | l_apic_init(); |
||
113 | l_apic_debug(); |
||
114 | } |
||
115 | |||
268 | palkovsky | 116 | void apic_spurious(__u8 n, __native stack[]) |
1 | jermar | 117 | { |
15 | jermar | 118 | printf("cpu%d: APIC spurious interrupt\n", CPU->id); |
1 | jermar | 119 | } |
120 | |||
121 | int apic_poll_errors(void) |
||
122 | { |
||
123 | __u32 esr; |
||
124 | |||
125 | esr = l_apic[ESR] & ~ESRClear; |
||
126 | |||
127 | if ((esr>>0) & 1) |
||
128 | printf("Send CS Error\n"); |
||
129 | if ((esr>>1) & 1) |
||
130 | printf("Receive CS Error\n"); |
||
131 | if ((esr>>2) & 1) |
||
132 | printf("Send Accept Error\n"); |
||
133 | if ((esr>>3) & 1) |
||
134 | printf("Receive Accept Error\n"); |
||
135 | if ((esr>>5) & 1) |
||
136 | printf("Send Illegal Vector\n"); |
||
137 | if ((esr>>6) & 1) |
||
138 | printf("Received Illegal Vector\n"); |
||
139 | if ((esr>>7) & 1) |
||
140 | printf("Illegal Register Address\n"); |
||
125 | jermar | 141 | |
1 | jermar | 142 | return !esr; |
143 | } |
||
144 | |||
145 | /* |
||
15 | jermar | 146 | * Send all CPUs excluding CPU IPI vector. |
5 | jermar | 147 | */ |
148 | int l_apic_broadcast_custom_ipi(__u8 vector) |
||
149 | { |
||
150 | __u32 lo; |
||
151 | |||
152 | /* |
||
153 | * Read the ICR register in and zero all non-reserved fields. |
||
154 | */ |
||
155 | lo = l_apic[ICRlo] & ICRloClear; |
||
156 | |||
157 | lo |= DLVRMODE_FIXED | DESTMODE_LOGIC | LEVEL_ASSERT | SHORTHAND_EXCL | TRGRMODE_LEVEL | vector; |
||
158 | |||
159 | l_apic[ICRlo] = lo; |
||
160 | |||
161 | lo = l_apic[ICRlo] & ICRloClear; |
||
162 | if (lo & SEND_PENDING) |
||
163 | printf("IPI is pending.\n"); |
||
164 | |||
165 | return apic_poll_errors(); |
||
166 | } |
||
167 | |||
168 | /* |
||
1 | jermar | 169 | * Universal Start-up Algorithm for bringing up the AP processors. |
170 | */ |
||
171 | int l_apic_send_init_ipi(__u8 apicid) |
||
172 | { |
||
173 | __u32 lo, hi; |
||
174 | int i; |
||
175 | |||
176 | /* |
||
177 | * Read the ICR register in and zero all non-reserved fields. |
||
178 | */ |
||
179 | lo = l_apic[ICRlo] & ICRloClear; |
||
180 | hi = l_apic[ICRhi] & ICRhiClear; |
||
181 | |||
182 | lo |= DLVRMODE_INIT | DESTMODE_PHYS | LEVEL_ASSERT | SHORTHAND_DEST | TRGRMODE_LEVEL; |
||
183 | hi |= apicid << 24; |
||
184 | |||
185 | l_apic[ICRhi] = hi; |
||
186 | l_apic[ICRlo] = lo; |
||
27 | jermar | 187 | |
1 | jermar | 188 | /* |
189 | * According to MP Specification, 20us should be enough to |
||
190 | * deliver the IPI. |
||
191 | */ |
||
192 | delay(20); |
||
193 | |||
194 | if (!apic_poll_errors()) return 0; |
||
195 | |||
196 | lo = l_apic[ICRlo] & ICRloClear; |
||
197 | if (lo & SEND_PENDING) |
||
198 | printf("IPI is pending.\n"); |
||
27 | jermar | 199 | |
1 | jermar | 200 | l_apic[ICRlo] = lo | DLVRMODE_INIT | DESTMODE_PHYS | LEVEL_DEASSERT | SHORTHAND_DEST | TRGRMODE_LEVEL; |
201 | |||
202 | /* |
||
203 | * Wait 10ms as MP Specification specifies. |
||
204 | */ |
||
205 | delay(10000); |
||
206 | |||
27 | jermar | 207 | if (!is_82489DX_apic(l_apic[LAVR])) { |
208 | /* |
||
209 | * If this is not 82489DX-based l_apic we must send two STARTUP IPI's. |
||
210 | */ |
||
211 | for (i = 0; i<2; i++) { |
||
212 | lo = l_apic[ICRlo] & ICRloClear; |
||
213 | lo |= ((__address) ap_boot) / 4096; /* calculate the reset vector */ |
||
110 | jermar | 214 | l_apic[ICRlo] = lo | DLVRMODE_STUP | DESTMODE_PHYS | LEVEL_ASSERT | SHORTHAND_DEST | TRGRMODE_LEVEL; |
27 | jermar | 215 | delay(200); |
216 | } |
||
1 | jermar | 217 | } |
218 | |||
27 | jermar | 219 | |
1 | jermar | 220 | return apic_poll_errors(); |
221 | } |
||
222 | |||
223 | void l_apic_init(void) |
||
224 | { |
||
225 | __u32 tmp, t1, t2; |
||
226 | |||
227 | l_apic[LVT_Err] |= (1<<16); |
||
228 | l_apic[LVT_LINT0] |= (1<<16); |
||
229 | l_apic[LVT_LINT1] |= (1<<16); |
||
230 | |||
231 | tmp = l_apic[SVR] & SVRClear; |
||
232 | l_apic[SVR] = tmp | (1<<8) | (VECTOR_APIC_SPUR); |
||
233 | |||
234 | l_apic[TPR] &= TPRClear; |
||
235 | |||
31 | jermar | 236 | if (CPU->arch.family >= 6) |
237 | enable_l_apic_in_msr(); |
||
1 | jermar | 238 | |
239 | tmp = l_apic[ICRlo] & ICRloClear; |
||
240 | l_apic[ICRlo] = tmp | DLVRMODE_INIT | DESTMODE_PHYS | LEVEL_DEASSERT | SHORTHAND_INCL | TRGRMODE_LEVEL; |
||
241 | |||
242 | /* |
||
243 | * Program the timer for periodic mode and respective vector. |
||
244 | */ |
||
245 | |||
246 | l_apic[TDCR] &= TDCRClear; |
||
247 | l_apic[TDCR] |= 0xb; |
||
248 | tmp = l_apic[LVT_Tm] | (1<<17) | (VECTOR_CLK); |
||
249 | l_apic[LVT_Tm] = tmp & ~(1<<16); |
||
250 | |||
251 | t1 = l_apic[CCRT]; |
||
252 | l_apic[ICRT] = 0xffffffff; |
||
253 | |||
254 | while (l_apic[CCRT] == t1) |
||
255 | ; |
||
256 | |||
257 | t1 = l_apic[CCRT]; |
||
258 | delay(1000); |
||
259 | t2 = l_apic[CCRT]; |
||
260 | |||
261 | l_apic[ICRT] = t1-t2; |
||
31 | jermar | 262 | |
1 | jermar | 263 | } |
264 | |||
265 | void l_apic_eoi(void) |
||
266 | { |
||
267 | l_apic[EOI] = 0; |
||
268 | } |
||
269 | |||
270 | void l_apic_debug(void) |
||
271 | { |
||
272 | #ifdef LAPIC_VERBOSE |
||
273 | int i, lint; |
||
274 | |||
16 | jermar | 275 | printf("LVT on cpu%d, LAPIC ID: %d\n", CPU->id, l_apic_id()); |
1 | jermar | 276 | |
277 | printf("LVT_Tm: "); |
||
278 | if (l_apic[LVT_Tm] & (1<<17)) printf("periodic"); else printf("one-shot"); putchar(','); |
||
279 | if (l_apic[LVT_Tm] & (1<<16)) printf("masked"); else printf("not masked"); putchar(','); |
||
280 | if (l_apic[LVT_Tm] & (1<<12)) printf("send pending"); else printf("idle"); putchar(','); |
||
281 | printf("%B\n", l_apic[LVT_Tm] & 0xff); |
||
282 | |||
283 | for (i=0; i<2; i++) { |
||
284 | lint = i ? LVT_LINT1 : LVT_LINT0; |
||
285 | printf("LVT_LINT%d: ", i); |
||
286 | if (l_apic[lint] & (1<<16)) printf("masked"); else printf("not masked"); putchar(','); |
||
287 | if (l_apic[lint] & (1<<15)) printf("level"); else printf("edge"); putchar(','); |
||
288 | printf("%d", l_apic[lint] & (1<<14)); putchar(','); |
||
289 | printf("%d", l_apic[lint] & (1<<13)); putchar(','); |
||
290 | if (l_apic[lint] & (1<<12)) printf("send pending"); else printf("idle"); putchar(','); |
||
291 | |||
292 | switch ((l_apic[lint]>>8)&7) { |
||
293 | case 0: printf("fixed"); break; |
||
294 | case 4: printf("NMI"); break; |
||
295 | case 7: printf("ExtINT"); break; |
||
296 | } |
||
297 | putchar(','); |
||
298 | printf("%B\n", l_apic[lint] & 0xff); |
||
299 | } |
||
300 | |||
301 | printf("LVT_Err: "); |
||
302 | if (l_apic[LVT_Err] & (1<<16)) printf("masked"); else printf("not masked"); putchar(','); |
||
303 | if (l_apic[LVT_Err] & (1<<12)) printf("send pending"); else printf("idle"); putchar(','); |
||
304 | printf("%B\n", l_apic[LVT_Err] & 0xff); |
||
305 | |||
306 | /* |
||
307 | * This register is supported only on P6 and higher. |
||
308 | */ |
||
16 | jermar | 309 | if (CPU->arch.family > 5) { |
1 | jermar | 310 | printf("LVT_PCINT: "); |
311 | if (l_apic[LVT_PCINT] & (1<<16)) printf("masked"); else printf("not masked"); putchar(','); |
||
312 | if (l_apic[LVT_PCINT] & (1<<12)) printf("send pending"); else printf("idle"); putchar(','); |
||
313 | switch ((l_apic[LVT_PCINT] >> 8)&7) { |
||
314 | case 0: printf("fixed"); break; |
||
315 | case 4: printf("NMI"); break; |
||
316 | case 7: printf("ExtINT"); break; |
||
317 | } |
||
318 | putchar(','); |
||
319 | printf("%B\n", l_apic[LVT_PCINT] & 0xff); |
||
320 | } |
||
321 | #endif |
||
322 | } |
||
323 | |||
268 | palkovsky | 324 | void l_apic_timer_interrupt(__u8 n, __native stack[]) |
1 | jermar | 325 | { |
326 | l_apic_eoi(); |
||
327 | clock(); |
||
328 | } |
||
329 | |||
81 | jermar | 330 | __u8 l_apic_id(void) |
16 | jermar | 331 | { |
332 | return (l_apic[L_APIC_ID] >> L_APIC_IDShift)&L_APIC_IDMask; |
||
333 | } |
||
334 | |||
1 | jermar | 335 | __u32 io_apic_read(__u8 address) |
336 | { |
||
337 | __u32 tmp; |
||
338 | |||
339 | tmp = io_apic[IOREGSEL] & ~0xf; |
||
340 | io_apic[IOREGSEL] = tmp | address; |
||
341 | return io_apic[IOWIN]; |
||
342 | } |
||
343 | |||
344 | void io_apic_write(__u8 address, __u32 x) |
||
345 | { |
||
346 | __u32 tmp; |
||
347 | |||
348 | tmp = io_apic[IOREGSEL] & ~0xf; |
||
349 | io_apic[IOREGSEL] = tmp | address; |
||
350 | io_apic[IOWIN] = x; |
||
351 | } |
||
352 | |||
353 | void io_apic_change_ioredtbl(int signal, int dest, __u8 v, int flags) |
||
354 | { |
||
512 | jermar | 355 | io_redirection_reg_t reg; |
1 | jermar | 356 | int dlvr = 0; |
357 | |||
358 | if (flags & LOPRI) |
||
512 | jermar | 359 | dlvr = DELMOD_LOWPRI; |
360 | |||
1 | jermar | 361 | |
512 | jermar | 362 | reg.lo = io_apic_read(IOREDTBL + signal*2); |
363 | reg.hi = io_apic_read(IOREDTBL + signal*2 + 1); |
||
1 | jermar | 364 | |
512 | jermar | 365 | reg.dest = dest; |
366 | reg.destmod = DESTMOD_LOGIC; |
||
367 | reg.trigger_mode = TRIGMOD_EDGE; |
||
368 | reg.intpol = POLARITY_HIGH; |
||
369 | reg.delmod = dlvr; |
||
370 | reg.intvec = v; |
||
1 | jermar | 371 | |
512 | jermar | 372 | io_apic_write(IOREDTBL + signal*2, reg.lo); |
373 | io_apic_write(IOREDTBL + signal*2 + 1, reg.hi); |
||
1 | jermar | 374 | } |
375 | |||
376 | void io_apic_disable_irqs(__u16 irqmask) |
||
377 | { |
||
512 | jermar | 378 | io_redirection_reg_t reg; |
379 | int i, pin; |
||
1 | jermar | 380 | |
381 | for (i=0;i<16;i++) { |
||
382 | if ((irqmask>>i) & 1) { |
||
383 | /* |
||
384 | * Mask the signal input in IO APIC if there is a |
||
385 | * mapping for the respective IRQ number. |
||
386 | */ |
||
512 | jermar | 387 | pin = smp_irq_to_pin(i); |
1 | jermar | 388 | if (pin != -1) { |
512 | jermar | 389 | reg.lo = io_apic_read(IOREDTBL + pin*2); |
390 | reg.masked = true; |
||
391 | io_apic_write(IOREDTBL + pin*2, reg.lo); |
||
1 | jermar | 392 | } |
393 | |||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
398 | void io_apic_enable_irqs(__u16 irqmask) |
||
399 | { |
||
512 | jermar | 400 | int i, pin; |
401 | io_redirection_reg_t reg; |
||
1 | jermar | 402 | |
403 | for (i=0;i<16;i++) { |
||
404 | if ((irqmask>>i) & 1) { |
||
405 | /* |
||
406 | * Unmask the signal input in IO APIC if there is a |
||
407 | * mapping for the respective IRQ number. |
||
408 | */ |
||
512 | jermar | 409 | pin = smp_irq_to_pin(i); |
1 | jermar | 410 | if (pin != -1) { |
512 | jermar | 411 | reg.lo = io_apic_read(IOREDTBL + pin*2); |
412 | reg.masked = false; |
||
413 | io_apic_write(IOREDTBL + pin*2, reg.lo); |
||
1 | jermar | 414 | } |
415 | |||
416 | } |
||
417 | } |
||
418 | |||
419 | } |
||
420 | |||
458 | decky | 421 | #endif /* CONFIG_SMP */ |