00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00035 #include <arch/types.h>
00036 #include <arch/smp/apic.h>
00037 #include <arch/smp/ap.h>
00038 #include <arch/smp/mps.h>
00039 #include <arch/boot/boot.h>
00040 #include <mm/page.h>
00041 #include <time/delay.h>
00042 #include <interrupt.h>
00043 #include <arch/interrupt.h>
00044 #include <print.h>
00045 #include <arch/asm.h>
00046 #include <arch.h>
00047
00048 #ifdef CONFIG_SMP
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 volatile __u32 *l_apic = (__u32 *) 0xfee00000;
00071 volatile __u32 *io_apic = (__u32 *) 0xfec00000;
00072
00073 __u32 apic_id_mask = 0;
00074
00075 static int apic_poll_errors(void);
00076
00077 #ifdef LAPIC_VERBOSE
00078 static char *delmod_str[] = {
00079 "Fixed",
00080 "Lowest Priority",
00081 "SMI",
00082 "Reserved",
00083 "NMI",
00084 "INIT",
00085 "STARTUP",
00086 "ExtInt"
00087 };
00088
00089 static char *destmod_str[] = {
00090 "Physical",
00091 "Logical"
00092 };
00093
00094 static char *trigmod_str[] = {
00095 "Edge",
00096 "Level"
00097 };
00098
00099 static char *mask_str[] = {
00100 "Unmasked",
00101 "Masked"
00102 };
00103
00104 static char *delivs_str[] = {
00105 "Idle",
00106 "Send Pending"
00107 };
00108
00109 static char *tm_mode_str[] = {
00110 "One-shot",
00111 "Periodic"
00112 };
00113
00114 static char *intpol_str[] = {
00115 "Polarity High",
00116 "Polarity Low"
00117 };
00118 #endif
00119
00120
00121 static void apic_spurious(int n, istate_t *istate);
00122 static void l_apic_timer_interrupt(int n, istate_t *istate);
00123
00125 void apic_init(void)
00126 {
00127 io_apic_id_t idreg;
00128 int i;
00129
00130 exc_register(VECTOR_APIC_SPUR, "apic_spurious", (iroutine) apic_spurious);
00131
00132 enable_irqs_function = io_apic_enable_irqs;
00133 disable_irqs_function = io_apic_disable_irqs;
00134 eoi_function = l_apic_eoi;
00135
00136
00137
00138
00139
00140
00141 io_apic_disable_irqs(0xffff);
00142 exc_register(VECTOR_CLK, "l_apic_timer", (iroutine) l_apic_timer_interrupt);
00143 for (i = 0; i < IRQ_COUNT; i++) {
00144 int pin;
00145
00146 if ((pin = smp_irq_to_pin(i)) != -1) {
00147 io_apic_change_ioredtbl(pin, DEST_ALL, IVT_IRQBASE+i, LOPRI);
00148 }
00149 }
00150
00151
00152
00153
00154 idreg.value = io_apic_read(IOAPICID);
00155 if ((1<<idreg.apic_id) & apic_id_mask) {
00156 for (i = 0; i < APIC_ID_COUNT; i++) {
00157 if (!((1<<i) & apic_id_mask)) {
00158 idreg.apic_id = i;
00159 io_apic_write(IOAPICID, idreg.value);
00160 break;
00161 }
00162 }
00163 }
00164
00165
00166
00167
00168 l_apic_init();
00169
00170 l_apic_debug();
00171 }
00172
00178 void apic_spurious(int n, istate_t *istate)
00179 {
00180 #ifdef CONFIG_DEBUG
00181 printf("cpu%d: APIC spurious interrupt\n", CPU->id);
00182 #endif
00183 }
00184
00191 int apic_poll_errors(void)
00192 {
00193 esr_t esr;
00194
00195 esr.value = l_apic[ESR];
00196
00197 if (esr.send_checksum_error)
00198 printf("Send Checksum Error\n");
00199 if (esr.receive_checksum_error)
00200 printf("Receive Checksum Error\n");
00201 if (esr.send_accept_error)
00202 printf("Send Accept Error\n");
00203 if (esr.receive_accept_error)
00204 printf("Receive Accept Error\n");
00205 if (esr.send_illegal_vector)
00206 printf("Send Illegal Vector\n");
00207 if (esr.received_illegal_vector)
00208 printf("Received Illegal Vector\n");
00209 if (esr.illegal_register_address)
00210 printf("Illegal Register Address\n");
00211
00212 return !esr.err_bitmap;
00213 }
00214
00221 int l_apic_broadcast_custom_ipi(__u8 vector)
00222 {
00223 icr_t icr;
00224
00225 icr.lo = l_apic[ICRlo];
00226 icr.delmod = DELMOD_FIXED;
00227 icr.destmod = DESTMOD_LOGIC;
00228 icr.level = LEVEL_ASSERT;
00229 icr.shorthand = SHORTHAND_ALL_EXCL;
00230 icr.trigger_mode = TRIGMOD_LEVEL;
00231 icr.vector = vector;
00232
00233 l_apic[ICRlo] = icr.lo;
00234
00235 icr.lo = l_apic[ICRlo];
00236 if (icr.delivs == DELIVS_PENDING) {
00237 #ifdef CONFIG_DEBUG
00238 printf("IPI is pending.\n");
00239 #endif
00240 }
00241
00242 return apic_poll_errors();
00243 }
00244
00251 int l_apic_send_init_ipi(__u8 apicid)
00252 {
00253 icr_t icr;
00254 int i;
00255
00256
00257
00258
00259 icr.lo = l_apic[ICRlo];
00260 icr.hi = l_apic[ICRhi];
00261
00262 icr.delmod = DELMOD_INIT;
00263 icr.destmod = DESTMOD_PHYS;
00264 icr.level = LEVEL_ASSERT;
00265 icr.trigger_mode = TRIGMOD_LEVEL;
00266 icr.shorthand = SHORTHAND_NONE;
00267 icr.vector = 0;
00268 icr.dest = apicid;
00269
00270 l_apic[ICRhi] = icr.hi;
00271 l_apic[ICRlo] = icr.lo;
00272
00273
00274
00275
00276
00277 delay(20);
00278
00279 if (!apic_poll_errors())
00280 return 0;
00281
00282 icr.lo = l_apic[ICRlo];
00283 if (icr.delivs == DELIVS_PENDING) {
00284 #ifdef CONFIG_DEBUG
00285 printf("IPI is pending.\n");
00286 #endif
00287 }
00288
00289 icr.delmod = DELMOD_INIT;
00290 icr.destmod = DESTMOD_PHYS;
00291 icr.level = LEVEL_DEASSERT;
00292 icr.shorthand = SHORTHAND_NONE;
00293 icr.trigger_mode = TRIGMOD_LEVEL;
00294 icr.vector = 0;
00295 l_apic[ICRlo] = icr.lo;
00296
00297
00298
00299
00300 delay(10000);
00301
00302 if (!is_82489DX_apic(l_apic[LAVR])) {
00303
00304
00305
00306 for (i = 0; i<2; i++) {
00307 icr.lo = l_apic[ICRlo];
00308 icr.vector = ((__address) ap_boot) / 4096;
00309 icr.delmod = DELMOD_STARTUP;
00310 icr.destmod = DESTMOD_PHYS;
00311 icr.level = LEVEL_ASSERT;
00312 icr.shorthand = SHORTHAND_NONE;
00313 icr.trigger_mode = TRIGMOD_LEVEL;
00314 l_apic[ICRlo] = icr.lo;
00315 delay(200);
00316 }
00317 }
00318
00319 return apic_poll_errors();
00320 }
00321
00323 void l_apic_init(void)
00324 {
00325 lvt_error_t error;
00326 lvt_lint_t lint;
00327 tpr_t tpr;
00328 svr_t svr;
00329 icr_t icr;
00330 tdcr_t tdcr;
00331 lvt_tm_t tm;
00332 ldr_t ldr;
00333 dfr_t dfr;
00334 __u32 t1, t2;
00335
00336
00337 error.value = l_apic[LVT_Err];
00338 error.masked = true;
00339 l_apic[LVT_Err] = error.value;
00340
00341
00342 lint.value = l_apic[LVT_LINT0];
00343 lint.masked = true;
00344 l_apic[LVT_LINT0] = lint.value;
00345
00346
00347 lint.value = l_apic[LVT_LINT1];
00348 lint.masked = true;
00349 l_apic[LVT_LINT1] = lint.value;
00350
00351
00352 tpr.value = l_apic[TPR];
00353 tpr.pri_sc = 0;
00354 tpr.pri = 0;
00355 l_apic[TPR] = tpr.value;
00356
00357
00358 svr.value = l_apic[SVR];
00359 svr.vector = VECTOR_APIC_SPUR;
00360 svr.lapic_enabled = true;
00361 svr.focus_checking = true;
00362 l_apic[SVR] = svr.value;
00363
00364 if (CPU->arch.family >= 6)
00365 enable_l_apic_in_msr();
00366
00367
00368 icr.lo = l_apic[ICRlo];
00369 icr.delmod = DELMOD_INIT;
00370 icr.destmod = DESTMOD_PHYS;
00371 icr.level = LEVEL_DEASSERT;
00372 icr.shorthand = SHORTHAND_ALL_INCL;
00373 icr.trigger_mode = TRIGMOD_LEVEL;
00374 l_apic[ICRlo] = icr.lo;
00375
00376
00377 tdcr.value = l_apic[TDCR];
00378 tdcr.div_value = DIVIDE_1;
00379 l_apic[TDCR] = tdcr.value;
00380
00381
00382 tm.value = l_apic[LVT_Tm];
00383 tm.vector = VECTOR_CLK;
00384 tm.mode = TIMER_PERIODIC;
00385 tm.masked = false;
00386 l_apic[LVT_Tm] = tm.value;
00387
00388
00389
00390
00391
00392 t1 = l_apic[CCRT];
00393 l_apic[ICRT] = 0xffffffff;
00394
00395 while (l_apic[CCRT] == t1)
00396 ;
00397
00398 t1 = l_apic[CCRT];
00399 delay(1000000/HZ);
00400 t2 = l_apic[CCRT];
00401
00402 l_apic[ICRT] = t1-t2;
00403
00404
00405 ldr.value = l_apic[LDR];
00406 if (CPU->id < sizeof(CPU->id)*8)
00407 ldr.id = (1<<CPU->id);
00408 l_apic[LDR] = ldr.value;
00409
00410
00411 dfr.value = l_apic[DFR];
00412 dfr.model = MODEL_FLAT;
00413 l_apic[DFR] = dfr.value;
00414 }
00415
00417 void l_apic_eoi(void)
00418 {
00419 l_apic[EOI] = 0;
00420 }
00421
00423 void l_apic_debug(void)
00424 {
00425 #ifdef LAPIC_VERBOSE
00426 lvt_tm_t tm;
00427 lvt_lint_t lint;
00428 lvt_error_t error;
00429
00430 printf("LVT on cpu%d, LAPIC ID: %d\n", CPU->id, l_apic_id());
00431
00432 tm.value = l_apic[LVT_Tm];
00433 printf("LVT Tm: vector=%hhd, %s, %s, %s\n", tm.vector, delivs_str[tm.delivs], mask_str[tm.masked], tm_mode_str[tm.mode]);
00434 lint.value = l_apic[LVT_LINT0];
00435 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]);
00436 lint.value = l_apic[LVT_LINT1];
00437 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]);
00438 error.value = l_apic[LVT_Err];
00439 printf("LVT Err: vector=%hhd, %s, %s\n", error.vector, delivs_str[error.delivs], mask_str[error.masked]);
00440 #endif
00441 }
00442
00448 void l_apic_timer_interrupt(int n, istate_t *istate)
00449 {
00450 l_apic_eoi();
00451 clock();
00452 }
00453
00458 __u8 l_apic_id(void)
00459 {
00460 l_apic_id_t idreg;
00461
00462 idreg.value = l_apic[L_APIC_ID];
00463 return idreg.apic_id;
00464 }
00465
00472 __u32 io_apic_read(__u8 address)
00473 {
00474 io_regsel_t regsel;
00475
00476 regsel.value = io_apic[IOREGSEL];
00477 regsel.reg_addr = address;
00478 io_apic[IOREGSEL] = regsel.value;
00479 return io_apic[IOWIN];
00480 }
00481
00487 void io_apic_write(__u8 address, __u32 x)
00488 {
00489 io_regsel_t regsel;
00490
00491 regsel.value = io_apic[IOREGSEL];
00492 regsel.reg_addr = address;
00493 io_apic[IOREGSEL] = regsel.value;
00494 io_apic[IOWIN] = x;
00495 }
00496
00504 void io_apic_change_ioredtbl(int pin, int dest, __u8 v, int flags)
00505 {
00506 io_redirection_reg_t reg;
00507 int dlvr = DELMOD_FIXED;
00508
00509 if (flags & LOPRI)
00510 dlvr = DELMOD_LOWPRI;
00511
00512 reg.lo = io_apic_read(IOREDTBL + pin*2);
00513 reg.hi = io_apic_read(IOREDTBL + pin*2 + 1);
00514
00515 reg.dest = dest;
00516 reg.destmod = DESTMOD_LOGIC;
00517 reg.trigger_mode = TRIGMOD_EDGE;
00518 reg.intpol = POLARITY_HIGH;
00519 reg.delmod = dlvr;
00520 reg.intvec = v;
00521
00522 io_apic_write(IOREDTBL + pin*2, reg.lo);
00523 io_apic_write(IOREDTBL + pin*2 + 1, reg.hi);
00524 }
00525
00530 void io_apic_disable_irqs(__u16 irqmask)
00531 {
00532 io_redirection_reg_t reg;
00533 int i, pin;
00534
00535 for (i=0;i<16;i++) {
00536 if (irqmask & (1<<i)) {
00537
00538
00539
00540
00541 pin = smp_irq_to_pin(i);
00542 if (pin != -1) {
00543 reg.lo = io_apic_read(IOREDTBL + pin*2);
00544 reg.masked = true;
00545 io_apic_write(IOREDTBL + pin*2, reg.lo);
00546 }
00547
00548 }
00549 }
00550 }
00551
00556 void io_apic_enable_irqs(__u16 irqmask)
00557 {
00558 int i, pin;
00559 io_redirection_reg_t reg;
00560
00561 for (i=0;i<16;i++) {
00562 if (irqmask & (1<<i)) {
00563
00564
00565
00566
00567 pin = smp_irq_to_pin(i);
00568 if (pin != -1) {
00569 reg.lo = io_apic_read(IOREDTBL + pin*2);
00570 reg.masked = false;
00571 io_apic_write(IOREDTBL + pin*2, reg.lo);
00572 }
00573
00574 }
00575 }
00576 }
00577
00578 #endif
00579