source: mainline/arch/ia32/src/smp/apic.c@ e20de55

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e20de55 was e20de55, checked in by Jakub Jermar <jakub@…>, 19 years ago

Fix programming of local APIC timer to interrupt the processor
in the proper relation to HZ. (Was hardcoded to be each 1ms,
but should have been each 10ms).

  • Property mode set to 100644
File size: 13.3 KB
RevLine 
[f761f1eb]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>
[397c77f]30#include <arch/smp/apic.h>
31#include <arch/smp/ap.h>
[ed0dd65]32#include <arch/smp/mps.h>
[66def8d]33#include <arch/boot/boot.h>
[f761f1eb]34#include <mm/page.h>
35#include <time/delay.h>
[fcfac420]36#include <interrupt.h>
[f761f1eb]37#include <arch/interrupt.h>
38#include <print.h>
39#include <arch/asm.h>
40#include <arch.h>
41
[5f85c91]42#ifdef CONFIG_SMP
[8262010]43
[f761f1eb]44/*
[a83a802]45 * Advanced Programmable Interrupt Controller for SMP systems.
[f761f1eb]46 * Tested on:
[d0780b4c]47 * Bochs 2.0.2 - Bochs 2.2.6 with 2-8 CPUs
[880de6e]48 * Simics 2.0.28 - Simics 2.2.19 2-15 CPUs
[78c32b4]49 * VMware Workstation 5.5 with 2 CPUs
[8b3eebb]50 * QEMU 0.8.0 with 2-15 CPUs
[f761f1eb]51 * ASUS P/I-P65UP5 + ASUS C-P55T2D REV. 1.41 with 2x 200Mhz Pentium CPUs
[2c457e8]52 * ASUS PCH-DL with 2x 3000Mhz Pentium 4 Xeon (HT) CPUs
53 * MSI K7D Master-L with 2x 2100MHz Athlon MP CPUs
[f761f1eb]54 */
55
56/*
57 * These variables either stay configured as initilalized, or are changed by
58 * the MP configuration code.
59 *
60 * Pay special attention to the volatile keyword. Without it, gcc -O2 would
61 * optimize the code too much and accesses to l_apic and io_apic, that must
62 * always be 32-bit, would use byte oriented instructions.
63 */
64volatile __u32 *l_apic = (__u32 *) 0xfee00000;
65volatile __u32 *io_apic = (__u32 *) 0xfec00000;
66
67__u32 apic_id_mask = 0;
68
[f701b236]69static int apic_poll_errors(void);
70
[9149135]71#ifdef LAPIC_VERBOSE
[f701b236]72static char *delmod_str[] = {
73 "Fixed",
74 "Lowest Priority",
75 "SMI",
76 "Reserved",
77 "NMI",
78 "INIT",
79 "STARTUP",
80 "ExtInt"
81};
82
83static char *destmod_str[] = {
84 "Physical",
85 "Logical"
86};
87
88static char *trigmod_str[] = {
89 "Edge",
90 "Level"
91};
92
93static char *mask_str[] = {
94 "Unmasked",
95 "Masked"
96};
97
98static char *delivs_str[] = {
99 "Idle",
100 "Send Pending"
101};
102
103static char *tm_mode_str[] = {
104 "One-shot",
105 "Periodic"
106};
107
108static char *intpol_str[] = {
109 "Polarity High",
110 "Polarity Low"
111};
[9149135]112#endif /* LAPIC_VERBOSE */
[f761f1eb]113
[fcfac420]114
[25d7709]115static void apic_spurious(int n, istate_t *istate);
116static void l_apic_timer_interrupt(int n, istate_t *istate);
[fcfac420]117
[8418c7d]118/** Initialize APIC on BSP. */
[f761f1eb]119void apic_init(void)
120{
[9149135]121 io_apic_id_t idreg;
122 int i;
[f761f1eb]123
[25d7709]124 exc_register(VECTOR_APIC_SPUR, "apic_spurious", (iroutine) apic_spurious);
[f761f1eb]125
126 enable_irqs_function = io_apic_enable_irqs;
127 disable_irqs_function = io_apic_disable_irqs;
128 eoi_function = l_apic_eoi;
129
130 /*
131 * Configure interrupt routing.
132 * IRQ 0 remains masked as the time signal is generated by l_apic's themselves.
133 * Other interrupts will be forwarded to the lowest priority CPU.
134 */
135 io_apic_disable_irqs(0xffff);
[25d7709]136 exc_register(VECTOR_CLK, "l_apic_timer", (iroutine) l_apic_timer_interrupt);
[9149135]137 for (i = 0; i < IRQ_COUNT; i++) {
[f761f1eb]138 int pin;
139
[a83a802]140 if ((pin = smp_irq_to_pin(i)) != -1) {
[9149135]141 io_apic_change_ioredtbl(pin, DEST_ALL, IVT_IRQBASE+i, LOPRI);
[a83a802]142 }
[f761f1eb]143 }
144
145 /*
146 * Ensure that io_apic has unique ID.
147 */
[9149135]148 idreg.value = io_apic_read(IOAPICID);
149 if ((1<<idreg.apic_id) & apic_id_mask) { /* see if IO APIC ID is used already */
150 for (i = 0; i < APIC_ID_COUNT; i++) {
[f761f1eb]151 if (!((1<<i) & apic_id_mask)) {
[9149135]152 idreg.apic_id = i;
153 io_apic_write(IOAPICID, idreg.value);
[f761f1eb]154 break;
155 }
156 }
157 }
158
159 /*
160 * Configure the BSP's lapic.
161 */
162 l_apic_init();
[9149135]163
[f761f1eb]164 l_apic_debug();
165}
166
[f701b236]167/** APIC spurious interrupt handler.
168 *
169 * @param n Interrupt vector.
170 * @param stack Interrupted stack.
171 */
[25d7709]172void apic_spurious(int n, istate_t *istate)
[f761f1eb]173{
[43114c5]174 printf("cpu%d: APIC spurious interrupt\n", CPU->id);
[f761f1eb]175}
176
[f701b236]177/** Poll for APIC errors.
178 *
179 * Examine Error Status Register and report all errors found.
180 *
181 * @return 0 on error, 1 on success.
182 */
[f761f1eb]183int apic_poll_errors(void)
184{
[f701b236]185 esr_t esr;
[f761f1eb]186
[f701b236]187 esr.value = l_apic[ESR];
[f761f1eb]188
[f701b236]189 if (esr.send_checksum_error)
[9149135]190 printf("Send Checksum Error\n");
[f701b236]191 if (esr.receive_checksum_error)
[9149135]192 printf("Receive Checksum Error\n");
[f701b236]193 if (esr.send_accept_error)
[f761f1eb]194 printf("Send Accept Error\n");
[f701b236]195 if (esr.receive_accept_error)
[f761f1eb]196 printf("Receive Accept Error\n");
[f701b236]197 if (esr.send_illegal_vector)
[f761f1eb]198 printf("Send Illegal Vector\n");
[f701b236]199 if (esr.received_illegal_vector)
[f761f1eb]200 printf("Received Illegal Vector\n");
[f701b236]201 if (esr.illegal_register_address)
[f761f1eb]202 printf("Illegal Register Address\n");
[76cec1e]203
[f701b236]204 return !esr.err_bitmap;
[f761f1eb]205}
206
[f701b236]207/** Send all CPUs excluding CPU IPI vector.
208 *
209 * @param vector Interrupt vector to be sent.
210 *
211 * @return 0 on failure, 1 on success.
[169587a]212 */
213int l_apic_broadcast_custom_ipi(__u8 vector)
214{
[8418c7d]215 icr_t icr;
[169587a]216
[8418c7d]217 icr.lo = l_apic[ICRlo];
218 icr.delmod = DELMOD_FIXED;
219 icr.destmod = DESTMOD_LOGIC;
220 icr.level = LEVEL_ASSERT;
221 icr.shorthand = SHORTHAND_ALL_EXCL;
222 icr.trigger_mode = TRIGMOD_LEVEL;
223 icr.vector = vector;
[169587a]224
[8418c7d]225 l_apic[ICRlo] = icr.lo;
[169587a]226
[8418c7d]227 icr.lo = l_apic[ICRlo];
[9149135]228 if (icr.delivs == DELIVS_PENDING)
[169587a]229 printf("IPI is pending.\n");
230
231 return apic_poll_errors();
232}
233
[f701b236]234/** Universal Start-up Algorithm for bringing up the AP processors.
235 *
236 * @param apicid APIC ID of the processor to be brought up.
237 *
238 * @return 0 on failure, 1 on success.
[f761f1eb]239 */
240int l_apic_send_init_ipi(__u8 apicid)
241{
[8418c7d]242 icr_t icr;
[f761f1eb]243 int i;
244
245 /*
246 * Read the ICR register in and zero all non-reserved fields.
247 */
[8418c7d]248 icr.lo = l_apic[ICRlo];
249 icr.hi = l_apic[ICRhi];
[f761f1eb]250
[8418c7d]251 icr.delmod = DELMOD_INIT;
252 icr.destmod = DESTMOD_PHYS;
253 icr.level = LEVEL_ASSERT;
254 icr.trigger_mode = TRIGMOD_LEVEL;
255 icr.shorthand = SHORTHAND_NONE;
256 icr.vector = 0;
257 icr.dest = apicid;
[f761f1eb]258
[8418c7d]259 l_apic[ICRhi] = icr.hi;
260 l_apic[ICRlo] = icr.lo;
[c9b8c5c]261
[f761f1eb]262 /*
263 * According to MP Specification, 20us should be enough to
264 * deliver the IPI.
265 */
266 delay(20);
267
268 if (!apic_poll_errors()) return 0;
269
[8418c7d]270 icr.lo = l_apic[ICRlo];
[9149135]271 if (icr.delivs == DELIVS_PENDING)
[f761f1eb]272 printf("IPI is pending.\n");
[c9b8c5c]273
[8418c7d]274 icr.delmod = DELMOD_INIT;
275 icr.destmod = DESTMOD_PHYS;
276 icr.level = LEVEL_DEASSERT;
277 icr.shorthand = SHORTHAND_NONE;
278 icr.trigger_mode = TRIGMOD_LEVEL;
279 icr.vector = 0;
280 l_apic[ICRlo] = icr.lo;
[f761f1eb]281
282 /*
283 * Wait 10ms as MP Specification specifies.
284 */
285 delay(10000);
286
[c9b8c5c]287 if (!is_82489DX_apic(l_apic[LAVR])) {
288 /*
289 * If this is not 82489DX-based l_apic we must send two STARTUP IPI's.
290 */
291 for (i = 0; i<2; i++) {
[8418c7d]292 icr.lo = l_apic[ICRlo];
293 icr.vector = ((__address) ap_boot) / 4096; /* calculate the reset vector */
294 icr.delmod = DELMOD_STARTUP;
295 icr.destmod = DESTMOD_PHYS;
296 icr.level = LEVEL_ASSERT;
297 icr.shorthand = SHORTHAND_NONE;
298 icr.trigger_mode = TRIGMOD_LEVEL;
299 l_apic[ICRlo] = icr.lo;
[c9b8c5c]300 delay(200);
301 }
[f761f1eb]302 }
303
304 return apic_poll_errors();
305}
306
[f701b236]307/** Initialize Local APIC. */
[f761f1eb]308void l_apic_init(void)
309{
[8418c7d]310 lvt_error_t error;
311 lvt_lint_t lint;
[d0780b4c]312 tpr_t tpr;
[8418c7d]313 svr_t svr;
314 icr_t icr;
[f701b236]315 tdcr_t tdcr;
316 lvt_tm_t tm;
[93e90c7]317 ldr_t ldr;
318 dfr_t dfr;
[8418c7d]319 __u32 t1, t2;
320
321 /* Initialize LVT Error register. */
322 error.value = l_apic[LVT_Err];
323 error.masked = true;
324 l_apic[LVT_Err] = error.value;
325
326 /* Initialize LVT LINT0 register. */
327 lint.value = l_apic[LVT_LINT0];
328 lint.masked = true;
329 l_apic[LVT_LINT0] = lint.value;
330
331 /* Initialize LVT LINT1 register. */
332 lint.value = l_apic[LVT_LINT1];
333 lint.masked = true;
334 l_apic[LVT_LINT1] = lint.value;
[d0780b4c]335
336 /* Task Priority Register initialization. */
337 tpr.value = l_apic[TPR];
338 tpr.pri_sc = 0;
339 tpr.pri = 0;
340 l_apic[TPR] = tpr.value;
[8418c7d]341
342 /* Spurious-Interrupt Vector Register initialization. */
343 svr.value = l_apic[SVR];
344 svr.vector = VECTOR_APIC_SPUR;
345 svr.lapic_enabled = true;
[d0780b4c]346 svr.focus_checking = true;
[8418c7d]347 l_apic[SVR] = svr.value;
[f761f1eb]348
[434f700]349 if (CPU->arch.family >= 6)
350 enable_l_apic_in_msr();
[f761f1eb]351
[8418c7d]352 /* Interrupt Command Register initialization. */
353 icr.lo = l_apic[ICRlo];
354 icr.delmod = DELMOD_INIT;
355 icr.destmod = DESTMOD_PHYS;
356 icr.level = LEVEL_DEASSERT;
357 icr.shorthand = SHORTHAND_ALL_INCL;
358 icr.trigger_mode = TRIGMOD_LEVEL;
359 l_apic[ICRlo] = icr.lo;
[f761f1eb]360
[f701b236]361 /* Timer Divide Configuration Register initialization. */
362 tdcr.value = l_apic[TDCR];
363 tdcr.div_value = DIVIDE_1;
364 l_apic[TDCR] = tdcr.value;
[8418c7d]365
[f701b236]366 /* Program local timer. */
[8418c7d]367 tm.value = l_apic[LVT_Tm];
368 tm.vector = VECTOR_CLK;
369 tm.mode = TIMER_PERIODIC;
370 tm.masked = false;
371 l_apic[LVT_Tm] = tm.value;
[f761f1eb]372
[e20de55]373 /*
374 * Measure and configure the timer to generate timer
375 * interrupt with period 1s/HZ seconds.
376 */
[f761f1eb]377 t1 = l_apic[CCRT];
378 l_apic[ICRT] = 0xffffffff;
379
380 while (l_apic[CCRT] == t1)
381 ;
382
383 t1 = l_apic[CCRT];
[e20de55]384 delay(1000000/HZ);
[f761f1eb]385 t2 = l_apic[CCRT];
386
387 l_apic[ICRT] = t1-t2;
[93e90c7]388
389 /* Program Logical Destination Register. */
390 ldr.value = l_apic[LDR];
391 if (CPU->id < sizeof(CPU->id)*8) /* size in bits */
392 ldr.id = (1<<CPU->id);
393 l_apic[LDR] = ldr.value;
394
395 /* Program Destination Format Register for Flat mode. */
396 dfr.value = l_apic[DFR];
397 dfr.model = MODEL_FLAT;
398 l_apic[DFR] = dfr.value;
[f761f1eb]399}
400
[f701b236]401/** Local APIC End of Interrupt. */
[f761f1eb]402void l_apic_eoi(void)
403{
404 l_apic[EOI] = 0;
405}
406
[f701b236]407/** Dump content of Local APIC registers. */
[f761f1eb]408void l_apic_debug(void)
409{
410#ifdef LAPIC_VERBOSE
[f701b236]411 lvt_tm_t tm;
412 lvt_lint_t lint;
413 lvt_error_t error;
[f761f1eb]414
[f701b236]415 printf("LVT on cpu%d, LAPIC ID: %d\n", CPU->id, l_apic_id());
[f761f1eb]416
[f701b236]417 tm.value = l_apic[LVT_Tm];
[280a27e]418 printf("LVT Tm: vector=%hhd, %s, %s, %s\n", tm.vector, delivs_str[tm.delivs], mask_str[tm.masked], tm_mode_str[tm.mode]);
[f701b236]419 lint.value = l_apic[LVT_LINT0];
[280a27e]420 printf("LVT LINT0: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]);
[f701b236]421 lint.value = l_apic[LVT_LINT1];
[280a27e]422 printf("LVT LINT1: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]);
[f701b236]423 error.value = l_apic[LVT_Err];
[280a27e]424 printf("LVT Err: vector=%hhd, %s, %s\n", error.vector, delivs_str[error.delivs], mask_str[error.masked]);
[f761f1eb]425#endif
426}
427
[f701b236]428/** Local APIC Timer Interrupt.
429 *
430 * @param n Interrupt vector number.
431 * @param stack Interrupted stack.
432 */
[25d7709]433void l_apic_timer_interrupt(int n, istate_t *istate)
[f761f1eb]434{
435 l_apic_eoi();
436 clock();
437}
438
[f701b236]439/** Get Local APIC ID.
440 *
441 * @return Local APIC ID.
442 */
[7f1bfce]443__u8 l_apic_id(void)
[8262010]444{
[9149135]445 l_apic_id_t idreg;
[f701b236]446
[9149135]447 idreg.value = l_apic[L_APIC_ID];
448 return idreg.apic_id;
[8262010]449}
450
[f701b236]451/** Read from IO APIC register.
452 *
453 * @param address IO APIC register address.
454 *
455 * @return Content of the addressed IO APIC register.
456 */
[f761f1eb]457__u32 io_apic_read(__u8 address)
458{
[f701b236]459 io_regsel_t regsel;
[f761f1eb]460
[f701b236]461 regsel.value = io_apic[IOREGSEL];
462 regsel.reg_addr = address;
463 io_apic[IOREGSEL] = regsel.value;
[f761f1eb]464 return io_apic[IOWIN];
465}
466
[f701b236]467/** Write to IO APIC register.
468 *
469 * @param address IO APIC register address.
470 * @param Content to be written to the addressed IO APIC register.
471 */
[f761f1eb]472void io_apic_write(__u8 address, __u32 x)
473{
[f701b236]474 io_regsel_t regsel;
475
476 regsel.value = io_apic[IOREGSEL];
477 regsel.reg_addr = address;
478 io_apic[IOREGSEL] = regsel.value;
[f761f1eb]479 io_apic[IOWIN] = x;
480}
481
[f701b236]482/** Change some attributes of one item in I/O Redirection Table.
483 *
484 * @param pin IO APIC pin number.
485 * @param dest Interrupt destination address.
486 * @param v Interrupt vector to trigger.
487 * @param flags Flags.
488 */
489void io_apic_change_ioredtbl(int pin, int dest, __u8 v, int flags)
[f761f1eb]490{
[a83a802]491 io_redirection_reg_t reg;
[f701b236]492 int dlvr = DELMOD_FIXED;
[f761f1eb]493
494 if (flags & LOPRI)
[a83a802]495 dlvr = DELMOD_LOWPRI;
496
[f701b236]497 reg.lo = io_apic_read(IOREDTBL + pin*2);
498 reg.hi = io_apic_read(IOREDTBL + pin*2 + 1);
[f761f1eb]499
[93e90c7]500 reg.dest = dest;
[a83a802]501 reg.destmod = DESTMOD_LOGIC;
502 reg.trigger_mode = TRIGMOD_EDGE;
503 reg.intpol = POLARITY_HIGH;
504 reg.delmod = dlvr;
505 reg.intvec = v;
506
[f701b236]507 io_apic_write(IOREDTBL + pin*2, reg.lo);
508 io_apic_write(IOREDTBL + pin*2 + 1, reg.hi);
[f761f1eb]509}
510
[f701b236]511/** Mask IRQs in IO APIC.
512 *
513 * @param irqmask Bitmask of IRQs to be masked (0 = do not mask, 1 = mask).
514 */
[f761f1eb]515void io_apic_disable_irqs(__u16 irqmask)
516{
[a83a802]517 io_redirection_reg_t reg;
518 int i, pin;
[f761f1eb]519
520 for (i=0;i<16;i++) {
[9149135]521 if (irqmask & (1<<i)) {
[f761f1eb]522 /*
523 * Mask the signal input in IO APIC if there is a
524 * mapping for the respective IRQ number.
525 */
[a83a802]526 pin = smp_irq_to_pin(i);
[f761f1eb]527 if (pin != -1) {
[a83a802]528 reg.lo = io_apic_read(IOREDTBL + pin*2);
529 reg.masked = true;
530 io_apic_write(IOREDTBL + pin*2, reg.lo);
[f761f1eb]531 }
532
533 }
534 }
535}
536
[f701b236]537/** Unmask IRQs in IO APIC.
538 *
539 * @param irqmask Bitmask of IRQs to be unmasked (0 = do not unmask, 1 = unmask).
540 */
[f761f1eb]541void io_apic_enable_irqs(__u16 irqmask)
542{
[a83a802]543 int i, pin;
544 io_redirection_reg_t reg;
[f761f1eb]545
546 for (i=0;i<16;i++) {
[9149135]547 if (irqmask & (1<<i)) {
[f761f1eb]548 /*
549 * Unmask the signal input in IO APIC if there is a
550 * mapping for the respective IRQ number.
551 */
[a83a802]552 pin = smp_irq_to_pin(i);
[f761f1eb]553 if (pin != -1) {
[a83a802]554 reg.lo = io_apic_read(IOREDTBL + pin*2);
555 reg.masked = false;
556 io_apic_write(IOREDTBL + pin*2, reg.lo);
[f761f1eb]557 }
558
559 }
560 }
561}
562
[5f85c91]563#endif /* CONFIG_SMP */
Note: See TracBrowser for help on using the repository browser.