]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/mp.c
pc kernel: split mpstartap() and squidboy into separate file... stuff for amd64
[plan9front.git] / sys / src / 9 / pc / mp.c
1 #include "u.h"
2 #include "../port/lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "io.h"
7 #include "ureg.h"
8
9 #include "mp.h"
10 #include "apbootstrap.h"
11
12 /* filled in by pcmpinit or acpiinit */
13 Bus* mpbus;
14 Bus* mpbuslast;
15 int mpisabus = -1;
16 int mpeisabus = -1;
17 Apic *mpioapic[MaxAPICNO+1];
18 Apic *mpapic[MaxAPICNO+1];
19
20 int
21 mpintrinit(Bus* bus, PCMPintr* intr, int vno, int /*irq*/)
22 {
23         int el, po, v;
24
25         /*
26          * Parse an I/O or Local APIC interrupt table entry and
27          * return the encoded vector.
28          */
29         v = vno;
30
31         po = intr->flags & PcmpPOMASK;
32         el = intr->flags & PcmpELMASK;
33
34         switch(intr->intr){
35         default:                                /* PcmpINT */
36                 v |= ApicFIXED;                 /* no-op */
37                 break;
38
39         case PcmpNMI:
40                 v |= ApicNMI;
41                 po = PcmpHIGH;
42                 el = PcmpEDGE;
43                 break;
44
45         case PcmpSMI:
46                 v |= ApicSMI;
47                 break;
48
49         case PcmpExtINT:
50                 v |= ApicExtINT;
51                 /*
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.
60                  */
61                 po = PcmpHIGH;
62                 el = PcmpEDGE;
63                 break;
64         }
65
66         /*
67          */
68         if(bus->type == BusEISA && !po && !el /*&& !(i8259elcr & (1<<irq))*/){
69                 po = PcmpHIGH;
70                 el = PcmpEDGE;
71         }
72         if(!po)
73                 po = bus->po;
74         if(po == PcmpLOW)
75                 v |= ApicLOW;
76         else if(po != PcmpHIGH){
77                 print("mpintrinit: bad polarity 0x%uX\n", po);
78                 return ApicIMASK;
79         }
80
81         if(!el)
82                 el = bus->el;
83         if(el == PcmpLEVEL)
84                 v |= ApicLEVEL;
85         else if(el != PcmpEDGE){
86                 print("mpintrinit: bad trigger 0x%uX\n", el);
87                 return ApicIMASK;
88         }
89
90         return v;
91 }
92
93 void
94 checkmtrr(void)
95 {
96         int i, vcnt;
97         Mach *mach0;
98
99         /*
100          * If there are MTRR registers, snarf them for validation.
101          */
102         if(!(m->cpuiddx & Mtrr))
103                 return;
104
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)]);
113         }
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]);
119
120         /*
121          * If not the bootstrap processor, compare.
122          */
123         if(m->machno == 0)
124                 return;
125
126         mach0 = MACHP(0);
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]);
137         }
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]);
142         }
143 }
144
145 uvlong
146 tscticks(uvlong *hz)
147 {
148         if(hz != nil)
149                 *hz = m->cpuhz;
150
151         cycles(&m->tscticks);   /* Uses the rdtsc instruction */
152         return m->tscticks;
153 }
154
155 void
156 syncclock(void)
157 {
158         uvlong x;
159
160         if(arch->fastclock != tscticks)
161                 return;
162
163         if(m->machno == 0){
164                 wrmsr(0x10, 0);
165                 m->tscticks = 0;
166         } else {
167                 x = MACHP(0)->tscticks;
168                 while(x == MACHP(0)->tscticks)
169                         ;
170                 wrmsr(0x10, MACHP(0)->tscticks);
171                 cycles(&m->tscticks);
172         }
173 }
174
175 void
176 mpinit(void)
177 {
178         int ncpu, i;
179         Apic *apic;
180         char *cp;
181
182         i8259init();
183         syncclock();
184
185         if(getconf("*apicdebug")){
186                 Bus *b;
187                 Aintr *ai;
188                 PCMPintr *pi;
189
190                 for(i=0; i<=MaxAPICNO; i++){
191                         if(apic = mpapic[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);
197                 }
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){
201                                 if(pi = ai->intr)
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);
205                         }
206                 }
207         }
208
209         apic = nil;
210         for(i=0; i<=MaxAPICNO; i++){
211                 if(mpapic[i] == nil)
212                         continue;
213                 if(mpapic[i]->flags & PcmpBP){
214                         apic = mpapic[i];
215                         break;
216                 }
217         }
218
219         if(apic == nil){
220                 panic("mpinit: no bootstrap processor");
221                 return;
222         }
223         apic->online = 1;
224
225         lapicinit(apic);
226
227         /*
228          * These interrupts are local to the processor
229          * and do not appear in the I/O APIC so it is OK
230          * to set them now.
231          */
232         intrenable(IrqTIMER, lapicclock, 0, BUSUNKNOWN, "clock");
233         intrenable(IrqERROR, lapicerror, 0, BUSUNKNOWN, "lapicerror");
234         intrenable(IrqSPURIOUS, lapicspurious, 0, BUSUNKNOWN, "lapicspurious");
235         lapiconline();
236
237         checkmtrr();
238
239         /*
240          * Initialise the application processors.
241          */
242         if(cp = getconf("*ncpu")){
243                 ncpu = strtol(cp, 0, 0);
244                 if(ncpu < 1)
245                         ncpu = 1;
246                 else if(ncpu > MAXMACH)
247                         ncpu = MAXMACH;
248         }
249         else
250                 ncpu = MAXMACH;
251         memmove((void*)APBOOTSTRAP, apbootstrap, sizeof(apbootstrap));
252         for(i=0; i<nelem(mpapic); i++){
253                 if((apic = mpapic[i]) == nil)
254                         continue;
255                 if(ncpu <= 1)
256                         break;
257                 if((apic->flags & (PcmpBP|PcmpEN)) == PcmpEN){
258                         mpstartap(apic);
259                         conf.nmach++;
260                         ncpu--;
261                 }
262         }
263
264         /*
265          *  we don't really know the number of processors till
266          *  here.
267          *
268          *  set conf.copymode here if nmach > 1.
269          *  Should look for an ExtINT line and enable it.
270          */
271         if(X86FAMILY(m->cpuidax) == 3 || conf.nmach > 1)
272                 conf.copymode = 1;
273 }
274
275 static int
276 mpintrcpu(void)
277 {
278         static Lock physidlock;
279         static int physid;
280         int i;
281
282         /*
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.
291          *
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. 
298          */
299         lock(&physidlock);
300         for(;;){
301                 i = physid++;
302                 if(physid >= nelem(mpapic))
303                         physid = 0;
304                 if(mpapic[i] == nil)
305                         continue;
306                 if(mpapic[i]->online)
307                         break;
308         }
309         unlock(&physidlock);
310
311         return mpapic[i]->apicno;
312 }
313
314 /*
315  * With the APIC a unique vector can be assigned to each
316  * request to enable an interrupt. There are two reasons this
317  * is a good idea:
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
322  *    interrupts).
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.
326  */
327 static int
328 allocvector(void)
329 {
330         static int round = 0, num = 0;
331         static Lock l;
332         int vno;
333         
334         lock(&l);
335         vno = VectorAPIC + num;
336         if(vno < MaxVectorAPIC-7)
337                 num += 8;
338         else
339                 num = ++round % 8;
340         unlock(&l);
341         return vno;
342 }
343
344 static int
345 mpintrenablex(Vctl* v, int tbdf)
346 {
347         Bus *bus;
348         Aintr *aintr;
349         Apic *apic;
350         Pcidev *pcidev;
351         int bno, dno, pin, hi, irq, lo, n, type, vno;
352
353         type = BUSTYPE(tbdf);
354         bno = BUSBNO(tbdf);
355         dno = BUSDNO(tbdf);
356
357         pin = 0;
358         pcidev = nil;
359         if(type == BusPCI){
360                 if(pcidev = pcimatchtbdf(tbdf))
361                         pin = pcicfgr8(pcidev, PciINTP);
362         } else if(type == BusISA)
363                 bno = mpisabus;
364
365 Findbus:
366         for(bus = mpbus; bus != nil; bus = bus->next){
367                 if(bus->type != type)
368                         continue;
369                 if(bus->busno == bno)
370                         break;
371         }
372
373         if(bus == nil){
374                 /*
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
377                  * pin to parent bus.
378                  */
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);
384                         goto Findbus;
385                 }
386                 print("mpintrenable: can't find bus type %d, number %d\n", type, bno);
387                 return -1;
388         }
389
390         /*
391          * For PCI devices the interrupt pin (INT[ABCD]) and device
392          * number are encoded into the entry irq field, so create something
393          * to match on.
394          */
395         if(bus->type == BusPCI){
396                 if(pin > 0)
397                         irq = (dno<<2)|(pin-1);
398                 else
399                         irq = -1;
400         }
401         else
402                 irq = v->irq;
403
404         /*
405          * Find a matching interrupt entry from the list of interrupts
406          * attached to this bus.
407          */
408         for(aintr = bus->aintr; aintr; aintr = aintr->next){
409                 if(aintr->intr->irq != irq)
410                         continue;
411                 if(0){
412                         PCMPintr* p = aintr->intr;
413                         print("mpintrenablex: bus %d intin %d irq %d\n",
414                                 p->busno, p->intin, p->irq);
415                 }
416                 /*
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.
420                  *
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.
425                  */
426                 apic = aintr->apic;
427                 ioapicrdtr(apic, aintr->intr->intin, 0, &lo);
428                 if(!(lo & ApicIMASK)){
429                         vno = lo & 0xFF;
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);
434                         if(n != lo){
435                                 print("mpintrenable: multiple botch irq %d, tbdf %uX, lo %8.8uX, n %8.8uX\n",
436                                         v->irq, tbdf, lo, n);
437                                 return -1;
438                         }
439                         v->isr = lapicisr;
440                         v->eoi = lapiceoi;
441                         return vno;
442                 }
443
444                 vno = allocvector();
445                 hi = mpintrcpu()<<24;
446                 lo = mpintrinit(bus, aintr->intr, vno, v->irq);
447                 lo |= ApicPHYSICAL;                     /* no-op */
448                 if(lo & ApicIMASK){
449                         print("mpintrenable: disabled irq %d, tbdf %uX, lo %8.8uX, hi %8.8uX\n",
450                                 v->irq, tbdf, lo, hi);
451                         return -1;
452                 }
453                 if((apic->flags & PcmpEN) && apic->type == PcmpIOAPIC)
454                         ioapicrdtw(apic, aintr->intr->intin, hi, lo);
455
456                 v->isr = lapicisr;
457                 v->eoi = lapiceoi;
458                 return vno;
459         }
460
461         return -1;
462 }
463
464 enum {
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) */
469 };
470
471 enum {
472         HTMSIMapping    = 0xA8,
473         HTMSIFlags      = 0x02,
474         HTMSIFlagsEn    = 0x01,
475 };
476
477 static int
478 htmsicapenable(Pcidev *p)
479 {
480         int cap, flags;
481
482         if((cap = pcihtcap(p, HTMSIMapping)) <= 0)
483                 return -1;
484         flags = pcicfgr8(p, cap + HTMSIFlags);
485         if((flags & HTMSIFlagsEn) == 0)
486                 pcicfgw8(p, cap + HTMSIFlags, flags | HTMSIFlagsEn);
487         return 0;
488 }
489
490 static int
491 htmsienable(Pcidev *pdev)
492 {
493         Pcidev *p;
494
495         p = nil;
496         while((p = pcimatch(p, 0x1022, 0)) != nil)
497                 if(p->did == 0x1103 || p->did == 0x1203)
498                         break;
499
500         if(p == nil)
501                 return 0;       /* not hypertransport platform */
502
503         p = nil;
504         while((p = pcimatch(p, 0x10de, 0)) != nil){
505                 switch(p->did){
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 */
515                         htmsicapenable(p);
516                         break;
517                 }
518         }
519
520         if(htmsicapenable(pdev) == 0)
521                 return 0;
522
523         for(p = pdev->parent; p != nil; p = p->parent)
524                 if(htmsicapenable(p) == 0)
525                         return 0;
526
527         return -1;
528 }
529
530 static int
531 msiintrenable(Vctl *v)
532 {
533         int tbdf, vno, cap, cpu, ok64;
534         Pcidev *pci;
535
536         if(getconf("*nomsi") != nil)
537                 return -1;
538         tbdf = v->tbdf;
539         if(tbdf == BUSUNKNOWN || BUSTYPE(tbdf) != BusPCI)
540                 return -1;
541         pci = pcimatchtbdf(tbdf);
542         if(pci == nil) {
543                 print("msiintrenable: could not find Pcidev for tbdf %uX\n", tbdf);
544                 return -1;
545         }
546         if(htmsienable(pci) < 0)
547                 return -1;
548         cap = pcicap(pci, PciCapMSI);
549         if(cap < 0)
550                 return -1;
551         vno = allocvector();
552         cpu = mpintrcpu();
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);
558         v->isr = lapicisr;
559         v->eoi = lapiceoi;
560         return vno;
561 }
562
563 int
564 mpintrenable(Vctl* v)
565 {
566         int irq, tbdf, vno;
567
568         vno = msiintrenable(v);
569         if(vno != -1)
570                 return vno;
571
572         /*
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).
577          */
578         tbdf = v->tbdf;
579         if(tbdf != BUSUNKNOWN && (vno = mpintrenablex(v, tbdf)) != -1)
580                 return vno;
581
582         irq = v->irq;
583         if(irq >= IrqLINT0 && irq <= MaxIrqLAPIC){
584                 if(irq != IrqSPURIOUS)
585                         v->isr = lapiceoi;
586                 return VectorPIC+irq;
587         }
588         if(irq < 0 || irq > MaxIrqPIC){
589                 print("mpintrenable: irq %d out of range\n", irq);
590                 return -1;
591         }
592
593         /*
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.
597          *
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.
603          */
604         if(mpeisabus != -1){
605                 vno = mpintrenablex(v, MKBUS(BusEISA, 0, 0, 0));
606                 if(vno != -1)
607                         return vno;
608         }
609         if(mpisabus != -1){
610                 vno = mpintrenablex(v, MKBUS(BusISA, 0, 0, 0));
611                 if(vno != -1)
612                         return vno;
613         }
614         print("mpintrenable: out of choices eisa %d isa %d tbdf %uX irq %d\n",
615                 mpeisabus, mpisabus, v->tbdf, v->irq);
616         return -1;
617 }
618
619
620 void
621 mpshutdown(void)
622 {
623         static Lock shutdownlock;
624
625         /*
626          * To be done...
627          */
628         if(!canlock(&shutdownlock)){
629                 /*
630                  * If this processor received the CTRL-ALT-DEL from
631                  * the keyboard, acknowledge it. Send an INIT to self.
632                  */
633 #ifdef FIXTHIS
634                 if(lapicisr(VectorKBD))
635                         lapiceoi(VectorKBD);
636 #endif /* FIX THIS */
637                 arch->introff();
638                 idle();
639         }
640
641         print("apshutdown: active = %#8.8ux\n", active.machs);
642         delay(1000);
643         splhi();
644
645         /*
646          * INIT all excluding self.
647          */
648         lapicicrw(0, 0x000C0000|ApicINIT);
649
650         pcireset();
651         i8042reset();
652
653         /*
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...
658          *
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.
662          */
663         print("no kbd; trying bios warm boot...");
664         *(ushort*)KADDR(0x472) = 0x1234;        /* BIOS warm-boot flag */
665         outb(0xCF9, 0x02);
666         outb(0xCF9, 0x06);
667
668         print("can't reset\n");
669         for(;;)
670                 idle();
671 }