2 #include "../port/lib.h"
10 #include "apbootstrap.h"
12 /* filled in by pcmpinit or acpiinit */
17 Apic *mpioapic[MaxAPICNO+1];
18 Apic *mpapic[MaxAPICNO+1];
21 mpintrinit(Bus* bus, PCMPintr* intr, int vno, int /*irq*/)
26 * Parse an I/O or Local APIC interrupt table entry and
27 * return the encoded vector.
31 po = intr->flags & PcmpPOMASK;
32 el = intr->flags & PcmpELMASK;
35 default: /* PcmpINT */
36 v |= ApicFIXED; /* no-op */
52 * The AMI Goliath doesn't boot successfully with it's LINTR0
53 * entry which decodes to low+level. The PPro manual says ExtINT
54 * should be level, whereas the Pentium is edge. Setting the
55 * Goliath to edge+high seems to cure the problem. Other PPro
56 * MP tables (e.g. ASUS P/I-P65UP5 have a entry which decodes
57 * to edge+high, so who knows.
58 * Perhaps it would be best just to not set an ExtINT entry at
59 * all, it shouldn't be needed for SMP mode.
68 if(bus->type == BusEISA && !po && !el /*&& !(i8259elcr & (1<<irq))*/){
76 else if(po != PcmpHIGH){
77 print("mpintrinit: bad polarity 0x%uX\n", po);
85 else if(el != PcmpEDGE){
86 print("mpintrinit: bad trigger 0x%uX\n", el);
100 * If there are MTRR registers, snarf them for validation.
102 if(!(m->cpuiddx & Mtrr))
105 rdmsr(0x0FE, &m->mtrrcap);
106 rdmsr(0x2FF, &m->mtrrdef);
107 if(m->mtrrcap & 0x0100){
108 rdmsr(0x250, &m->mtrrfix[0]);
109 rdmsr(0x258, &m->mtrrfix[1]);
110 rdmsr(0x259, &m->mtrrfix[2]);
111 for(i = 0; i < 8; i++)
112 rdmsr(0x268+i, &m->mtrrfix[(i+3)]);
114 vcnt = m->mtrrcap & 0x00FF;
115 if(vcnt > nelem(m->mtrrvar))
116 vcnt = nelem(m->mtrrvar);
117 for(i = 0; i < vcnt; i++)
118 rdmsr(0x200+i, &m->mtrrvar[i]);
121 * If not the bootstrap processor, compare.
127 if(mach0->mtrrcap != m->mtrrcap)
128 print("mtrrcap%d: %lluX %lluX\n",
129 m->machno, mach0->mtrrcap, m->mtrrcap);
130 if(mach0->mtrrdef != m->mtrrdef)
131 print("mtrrdef%d: %lluX %lluX\n",
132 m->machno, mach0->mtrrdef, m->mtrrdef);
133 for(i = 0; i < 11; i++){
134 if(mach0->mtrrfix[i] != m->mtrrfix[i])
135 print("mtrrfix%d: i%d: %lluX %lluX\n",
136 m->machno, i, mach0->mtrrfix[i], m->mtrrfix[i]);
138 for(i = 0; i < vcnt; i++){
139 if(mach0->mtrrvar[i] != m->mtrrvar[i])
140 print("mtrrvar%d: i%d: %lluX %lluX\n",
141 m->machno, i, mach0->mtrrvar[i], m->mtrrvar[i]);
151 cycles(&m->tscticks); /* Uses the rdtsc instruction */
160 if(arch->fastclock != tscticks)
167 x = MACHP(0)->tscticks;
168 while(x == MACHP(0)->tscticks)
170 wrmsr(0x10, MACHP(0)->tscticks);
171 cycles(&m->tscticks);
185 if(getconf("*apicdebug")){
190 for(i=0; i<=MaxAPICNO; i++){
192 print("LAPIC%d: pa=%lux va=%#p flags=%x\n",
193 i, apic->paddr, apic->addr, apic->flags);
194 if(apic = mpioapic[i])
195 print("IOAPIC%d: pa=%lux va=%#p flags=%x gsibase=%d mre=%d\n",
196 i, apic->paddr, apic->addr, apic->flags, apic->gsibase, apic->mre);
198 for(b = mpbus; b; b = b->next){
199 print("BUS%d type=%d flags=%x\n", b->busno, b->type, b->po|b->el);
200 for(ai = b->aintr; ai; ai = ai->next){
202 print("\ttype=%d irq=%d (%d [%c]) apic=%d intin=%d flags=%x\n",
203 pi->type, pi->irq, pi->irq>>2, "ABCD"[pi->irq&3],
204 pi->apicno, pi->intin, pi->flags);
210 for(i=0; i<=MaxAPICNO; i++){
213 if(mpapic[i]->flags & PcmpBP){
220 panic("mpinit: no bootstrap processor");
228 * These interrupts are local to the processor
229 * and do not appear in the I/O APIC so it is OK
232 intrenable(IrqTIMER, lapicclock, 0, BUSUNKNOWN, "clock");
233 intrenable(IrqERROR, lapicerror, 0, BUSUNKNOWN, "lapicerror");
234 intrenable(IrqSPURIOUS, lapicspurious, 0, BUSUNKNOWN, "lapicspurious");
240 * Initialise the application processors.
242 if(cp = getconf("*ncpu")){
243 ncpu = strtol(cp, 0, 0);
246 else if(ncpu > MAXMACH)
251 memmove((void*)APBOOTSTRAP, apbootstrap, sizeof(apbootstrap));
252 for(i=0; i<nelem(mpapic); i++){
253 if((apic = mpapic[i]) == nil)
257 if((apic->flags & (PcmpBP|PcmpEN)) == PcmpEN){
265 * we don't really know the number of processors till
268 * set conf.copymode here if nmach > 1.
269 * Should look for an ExtINT line and enable it.
271 if(X86FAMILY(m->cpuidax) == 3 || conf.nmach > 1)
278 static Lock physidlock;
283 * The bulk of this code was written ~1995, when there was
284 * one architecture and one generation of hardware, the number
285 * of CPUs was up to 4(8) and the choices for interrupt routing
286 * were physical, or flat logical (optionally with lowest
287 * priority interrupt). Logical mode hasn't scaled well with
288 * the increasing number of packages/cores/threads, so the
289 * fall-back is to physical mode, which works across all processor
290 * generations, both AMD and Intel, using the APIC and xAPIC.
292 * Interrupt routing policy can be set here.
293 * Currently, just assign each interrupt to a different CPU on
294 * a round-robin basis. Some idea of the packages/cores/thread
295 * topology would be useful here, e.g. to not assign interrupts
296 * to more than one thread in a core, or to use a "noise" core.
297 * But, as usual, Intel make that an onerous task.
302 if(physid >= nelem(mpapic))
306 if(mpapic[i]->online)
311 return mpapic[i]->apicno;
315 * With the APIC a unique vector can be assigned to each
316 * request to enable an interrupt. There are two reasons this
318 * 1) to prevent lost interrupts, no more than 2 interrupts
319 * should be assigned per block of 16 vectors (there is an
320 * in-service entry and a holding entry for each priority
321 * level and there is one priority level per block of 16
323 * 2) each input pin on the IOAPIC will receive a different
324 * vector regardless of whether the devices on that pin use
325 * the same IRQ as devices on another pin.
330 static int round = 0, num = 0;
335 vno = VectorAPIC + num;
336 if(vno < MaxVectorAPIC-7)
345 mpintrenablex(Vctl* v, int tbdf)
351 int bno, dno, pin, hi, irq, lo, n, type, vno;
353 type = BUSTYPE(tbdf);
360 if(pcidev = pcimatchtbdf(tbdf))
361 pin = pcicfgr8(pcidev, PciINTP);
362 } else if(type == BusISA)
366 for(bus = mpbus; bus != nil; bus = bus->next){
367 if(bus->type != type)
369 if(bus->busno == bno)
375 * if the PCI device is behind a PCI-PCI bridge thats not described
376 * by the MP or ACPI tables then walk up the bus translating interrupt
379 if(pcidev && pcidev->parent && pin > 0){
380 pin = ((dno+(pin-1))%4)+1;
381 pcidev = pcidev->parent;
382 bno = BUSBNO(pcidev->tbdf);
383 dno = BUSDNO(pcidev->tbdf);
386 print("mpintrenable: can't find bus type %d, number %d\n", type, bno);
391 * For PCI devices the interrupt pin (INT[ABCD]) and device
392 * number are encoded into the entry irq field, so create something
395 if(bus->type == BusPCI){
397 irq = (dno<<2)|(pin-1);
405 * Find a matching interrupt entry from the list of interrupts
406 * attached to this bus.
408 for(aintr = bus->aintr; aintr; aintr = aintr->next){
409 if(aintr->intr->irq != irq)
412 PCMPintr* p = aintr->intr;
413 print("mpintrenablex: bus %d intin %d irq %d\n",
414 p->busno, p->intin, p->irq);
417 * Check if already enabled. Multifunction devices may share
418 * INT[A-D]# so, if already enabled, check the polarity matches
419 * and the trigger is level.
421 * Should check the devices differ only in the function number,
422 * but that can wait for the planned enable/disable rewrite.
423 * The RDT read here is safe for now as currently interrupts
424 * are never disabled once enabled.
427 ioapicrdtr(apic, aintr->intr->intin, 0, &lo);
428 if(!(lo & ApicIMASK)){
430 if(0) print("%s vector %d (!imask)\n", v->name, vno);
431 n = mpintrinit(bus, aintr->intr, vno, v->irq);
432 n |= ApicPHYSICAL; /* no-op */
433 lo &= ~(ApicRemoteIRR|ApicDELIVS);
435 print("mpintrenable: multiple botch irq %d, tbdf %uX, lo %8.8uX, n %8.8uX\n",
436 v->irq, tbdf, lo, n);
445 hi = mpintrcpu()<<24;
446 lo = mpintrinit(bus, aintr->intr, vno, v->irq);
447 lo |= ApicPHYSICAL; /* no-op */
449 print("mpintrenable: disabled irq %d, tbdf %uX, lo %8.8uX, hi %8.8uX\n",
450 v->irq, tbdf, lo, hi);
453 if((apic->flags & PcmpEN) && apic->type == PcmpIOAPIC)
454 ioapicrdtw(apic, aintr->intr->intin, hi, lo);
465 MSICtrl = 0x02, /* message control register (16 bit) */
466 MSIAddr = 0x04, /* message address register (64 bit) */
467 MSIData32 = 0x08, /* message data register for 32 bit MSI (16 bit) */
468 MSIData64 = 0x0C, /* message data register for 64 bit MSI (16 bit) */
478 htmsicapenable(Pcidev *p)
482 if((cap = pcihtcap(p, HTMSIMapping)) <= 0)
484 flags = pcicfgr8(p, cap + HTMSIFlags);
485 if((flags & HTMSIFlagsEn) == 0)
486 pcicfgw8(p, cap + HTMSIFlags, flags | HTMSIFlagsEn);
491 htmsienable(Pcidev *pdev)
496 while((p = pcimatch(p, 0x1022, 0)) != nil)
497 if(p->did == 0x1103 || p->did == 0x1203)
501 return 0; /* not hypertransport platform */
504 while((p = pcimatch(p, 0x10de, 0)) != nil){
506 case 0x02f0: /* NVIDIA NFORCE C51 MEMC0 */
507 case 0x02f1: /* NVIDIA NFORCE C51 MEMC1 */
508 case 0x02f2: /* NVIDIA NFORCE C51 MEMC2 */
509 case 0x02f3: /* NVIDIA NFORCE C51 MEMC3 */
510 case 0x02f4: /* NVIDIA NFORCE C51 MEMC4 */
511 case 0x02f5: /* NVIDIA NFORCE C51 MEMC5 */
512 case 0x02f6: /* NVIDIA NFORCE C51 MEMC6 */
513 case 0x02f7: /* NVIDIA NFORCE C51 MEMC7 */
514 case 0x0369: /* NVIDIA NFORCE MCP55 MEMC */
520 if(htmsicapenable(pdev) == 0)
523 for(p = pdev->parent; p != nil; p = p->parent)
524 if(htmsicapenable(p) == 0)
531 msiintrenable(Vctl *v)
533 int tbdf, vno, cap, cpu, ok64;
536 if(getconf("*nomsi") != nil)
539 if(tbdf == BUSUNKNOWN || BUSTYPE(tbdf) != BusPCI)
541 pci = pcimatchtbdf(tbdf);
543 print("msiintrenable: could not find Pcidev for tbdf %uX\n", tbdf);
546 if(htmsienable(pci) < 0)
548 cap = pcicap(pci, PciCapMSI);
553 ok64 = (pcicfgr16(pci, cap + MSICtrl) & (1<<7)) != 0;
554 pcicfgw32(pci, cap + MSIAddr, (0xFEE << 20) | (cpu << 12));
555 if(ok64) pcicfgw32(pci, cap + MSIAddr + 4, 0);
556 pcicfgw16(pci, cap + (ok64 ? MSIData64 : MSIData32), vno | (1<<14));
557 pcicfgw16(pci, cap + MSICtrl, 1);
564 mpintrenable(Vctl* v)
568 vno = msiintrenable(v);
573 * If the bus is known, try it.
574 * BUSUNKNOWN is given both by [E]ISA devices and by
575 * interrupts local to the processor (local APIC, coprocessor
576 * breakpoint and page-fault).
579 if(tbdf != BUSUNKNOWN && (vno = mpintrenablex(v, tbdf)) != -1)
583 if(irq >= IrqLINT0 && irq <= MaxIrqLAPIC){
584 if(irq != IrqSPURIOUS)
586 return VectorPIC+irq;
588 if(irq < 0 || irq > MaxIrqPIC){
589 print("mpintrenable: irq %d out of range\n", irq);
594 * Either didn't find it or have to try the default buses
595 * (ISA and EISA). This hack is due to either over-zealousness
596 * or laziness on the part of some manufacturers.
598 * The MP configuration table on some older systems
599 * (e.g. ASUS PCI/E-P54NP4) has an entry for the EISA bus
600 * but none for ISA. It also has the interrupt type and
601 * polarity set to 'default for this bus' which wouldn't
602 * be compatible with ISA.
605 vno = mpintrenablex(v, MKBUS(BusEISA, 0, 0, 0));
610 vno = mpintrenablex(v, MKBUS(BusISA, 0, 0, 0));
614 print("mpintrenable: out of choices eisa %d isa %d tbdf %uX irq %d\n",
615 mpeisabus, mpisabus, v->tbdf, v->irq);
623 static Lock shutdownlock;
628 if(!canlock(&shutdownlock)){
630 * If this processor received the CTRL-ALT-DEL from
631 * the keyboard, acknowledge it. Send an INIT to self.
634 if(lapicisr(VectorKBD))
636 #endif /* FIX THIS */
641 print("apshutdown: active = %#8.8ux\n", active.machs);
646 * INIT all excluding self.
648 lapicicrw(0, 0x000C0000|ApicINIT);
654 * Often the BIOS hangs during restart if a conventional 8042
655 * warm-boot sequence is tried. The following is Intel specific and
656 * seems to perform a cold-boot, but at least it comes back.
657 * And sometimes there is no keyboard...
659 * The reset register (0xcf9) is usually in one of the bridge
660 * chips. The actual location and sequence could be extracted from
661 * ACPI but why bother, this is the end of the line anyway.
663 print("no kbd; trying bios warm boot...");
664 *(ushort*)KADDR(0x472) = 0x1234; /* BIOS warm-boot flag */
668 print("can't reset\n");