]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/mp.c
b392c6a46b233dcd98432f0d452db8f4ac32be1f
[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 static 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 & 0x1000))
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 static void
176 squidboy(Apic* apic)
177 {
178 //      iprint("Hello Squidboy\n");
179
180         machinit();
181         mmuinit();
182
183         cpuidentify();
184         cpuidprint();
185         checkmtrr();
186
187         apic->online = 1;
188
189         lapicinit(apic);
190         lapiconline();
191         syncclock();
192         timersinit();
193
194         fpoff();
195
196         lock(&active);
197         active.machs |= 1<<m->machno;
198         unlock(&active);
199
200         while(!active.thunderbirdsarego)
201                 microdelay(100);
202
203         schedinit();
204 }
205
206 static void
207 mpstartap(Apic* apic)
208 {
209         ulong *apbootp, *pdb, *pte;
210         Mach *mach, *mach0;
211         int i, machno;
212         uchar *p;
213
214         mach0 = MACHP(0);
215
216         /*
217          * Initialise the AP page-tables and Mach structure. The page-tables
218          * are the same as for the bootstrap processor with the exception of
219          * the PTE for the Mach structure.
220          * Xspanalloc will panic if an allocation can't be made.
221          */
222         p = xspanalloc(4*BY2PG, BY2PG, 0);
223         pdb = (ulong*)p;
224         memmove(pdb, mach0->pdb, BY2PG);
225         p += BY2PG;
226
227         if((pte = mmuwalk(pdb, MACHADDR, 1, 0)) == nil)
228                 return;
229         memmove(p, KADDR(PPN(*pte)), BY2PG);
230         *pte = PADDR(p)|PTEWRITE|PTEVALID;
231         if(mach0->havepge)
232                 *pte |= PTEGLOBAL;
233         p += BY2PG;
234
235         mach = (Mach*)p;
236         if((pte = mmuwalk(pdb, MACHADDR, 2, 0)) == nil)
237                 return;
238         *pte = PADDR(mach)|PTEWRITE|PTEVALID;
239         if(mach0->havepge)
240                 *pte |= PTEGLOBAL;
241         p += BY2PG;
242
243         machno = apic->machno;
244         MACHP(machno) = mach;
245         mach->machno = machno;
246         mach->pdb = pdb;
247         mach->gdt = (Segdesc*)p;        /* filled by mmuinit */
248
249         /*
250          * Tell the AP where its kernel vector and pdb are.
251          * The offsets are known in the AP bootstrap code.
252          */
253         apbootp = (ulong*)(APBOOTSTRAP+0x08);
254         *apbootp++ = (ulong)squidboy;
255         *apbootp++ = PADDR(pdb);
256         *apbootp = (ulong)apic;
257
258         /*
259          * Universal Startup Algorithm.
260          */
261         p = KADDR(0x467);
262         *p++ = PADDR(APBOOTSTRAP);
263         *p++ = PADDR(APBOOTSTRAP)>>8;
264         i = (PADDR(APBOOTSTRAP) & ~0xFFFF)/16;
265         /* code assumes i==0 */
266         if(i != 0)
267                 print("mp: bad APBOOTSTRAP\n");
268         *p++ = i;
269         *p = i>>8;
270
271         nvramwrite(0x0F, 0x0A);
272         lapicstartap(apic, PADDR(APBOOTSTRAP));
273         for(i = 0; i < 1000; i++){
274                 if(apic->online)
275                         break;
276                 delay(10);
277         }
278         nvramwrite(0x0F, 0x00);
279 }
280
281 void
282 mpinit(void)
283 {
284         int ncpu, i;
285         Apic *apic;
286         char *cp;
287
288         i8259init();
289         syncclock();
290
291         if(getconf("*apicdebug")){
292                 Bus *b;
293                 Aintr *ai;
294                 PCMPintr *pi;
295
296                 for(i=0; i<=MaxAPICNO; i++){
297                         if(apic = mpapic[i])
298                                 print("LAPIC%d: pa=%lux va=%lux flags=%x\n",
299                                         i, apic->paddr, (ulong)apic->addr, apic->flags);
300                         if(apic = mpioapic[i])
301                                 print("IOAPIC%d: pa=%lux va=%lux flags=%x gsibase=%d mre=%d\n",
302                                         i, apic->paddr, (ulong)apic->addr, apic->flags, apic->gsibase, apic->mre);
303                 }
304                 for(b = mpbus; b; b = b->next){
305                         print("BUS%d type=%d flags=%x\n", b->busno, b->type, b->po|b->el);
306                         for(ai = b->aintr; ai; ai = ai->next){
307                                 if(pi = ai->intr)
308                                         print("\ttype=%d irq=%d (%d [%c]) apic=%d intin=%d flags=%x\n",
309                                                 pi->type, pi->irq, pi->irq>>2, "ABCD"[pi->irq&3],
310                                                 pi->apicno, pi->intin, pi->flags);
311                         }
312                 }
313         }
314
315         apic = nil;
316         for(i=0; i<=MaxAPICNO; i++){
317                 if(mpapic[i] == nil)
318                         continue;
319                 if(mpapic[i]->flags & PcmpBP){
320                         apic = mpapic[i];
321                         break;
322                 }
323         }
324
325         if(apic == nil){
326                 panic("mpinit: no bootstrap processor");
327                 return;
328         }
329         apic->online = 1;
330         lapicinit(apic);
331
332         /*
333          * These interrupts are local to the processor
334          * and do not appear in the I/O APIC so it is OK
335          * to set them now.
336          */
337         intrenable(IrqTIMER, lapicclock, 0, BUSUNKNOWN, "clock");
338         intrenable(IrqERROR, lapicerror, 0, BUSUNKNOWN, "lapicerror");
339         intrenable(IrqSPURIOUS, lapicspurious, 0, BUSUNKNOWN, "lapicspurious");
340         lapiconline();
341
342         checkmtrr();
343
344         /*
345          * Initialise the application processors.
346          */
347         if(cp = getconf("*ncpu")){
348                 ncpu = strtol(cp, 0, 0);
349                 if(ncpu < 1)
350                         ncpu = 1;
351                 else if(ncpu > MAXMACH)
352                         ncpu = MAXMACH;
353         }
354         else
355                 ncpu = MAXMACH;
356         memmove((void*)APBOOTSTRAP, apbootstrap, sizeof(apbootstrap));
357         for(i=0; i<nelem(mpapic); i++){
358                 if((apic = mpapic[i]) == nil)
359                         continue;
360                 if(ncpu <= 1)
361                         break;
362                 if((apic->flags & (PcmpBP|PcmpEN)) == PcmpEN){
363                         mpstartap(apic);
364                         conf.nmach++;
365                         ncpu--;
366                 }
367         }
368
369         /*
370          *  we don't really know the number of processors till
371          *  here.
372          *
373          *  set conf.copymode here if nmach > 1.
374          *  Should look for an ExtINT line and enable it.
375          */
376         if(X86FAMILY(m->cpuidax) == 3 || conf.nmach > 1)
377                 conf.copymode = 1;
378 }
379
380 static int
381 mpintrcpu(void)
382 {
383         static Lock physidlock;
384         static int physid;
385         int i;
386
387         /*
388          * The bulk of this code was written ~1995, when there was
389          * one architecture and one generation of hardware, the number
390          * of CPUs was up to 4(8) and the choices for interrupt routing
391          * were physical, or flat logical (optionally with lowest
392          * priority interrupt). Logical mode hasn't scaled well with
393          * the increasing number of packages/cores/threads, so the
394          * fall-back is to physical mode, which works across all processor
395          * generations, both AMD and Intel, using the APIC and xAPIC.
396          *
397          * Interrupt routing policy can be set here.
398          * Currently, just assign each interrupt to a different CPU on
399          * a round-robin basis. Some idea of the packages/cores/thread
400          * topology would be useful here, e.g. to not assign interrupts
401          * to more than one thread in a core, or to use a "noise" core.
402          * But, as usual, Intel make that an onerous task. 
403          */
404         lock(&physidlock);
405         for(;;){
406                 i = physid++;
407                 if(physid >= nelem(mpapic))
408                         physid = 0;
409                 if(mpapic[i] == nil)
410                         continue;
411                 if(mpapic[i]->online)
412                         break;
413         }
414         unlock(&physidlock);
415
416         return mpapic[i]->apicno;
417 }
418
419 /*
420  * With the APIC a unique vector can be assigned to each
421  * request to enable an interrupt. There are two reasons this
422  * is a good idea:
423  * 1) to prevent lost interrupts, no more than 2 interrupts
424  *    should be assigned per block of 16 vectors (there is an
425  *    in-service entry and a holding entry for each priority
426  *    level and there is one priority level per block of 16
427  *    interrupts).
428  * 2) each input pin on the IOAPIC will receive a different
429  *    vector regardless of whether the devices on that pin use
430  *    the same IRQ as devices on another pin.
431  */
432 static int
433 allocvector(void)
434 {
435         static int round = 0, num = 0;
436         static Lock l;
437         int vno;
438         
439         lock(&l);
440         vno = VectorAPIC + num;
441         if(vno < MaxVectorAPIC-7)
442                 num += 8;
443         else
444                 num = ++round % 8;
445         unlock(&l);
446         return vno;
447 }
448
449 static int
450 mpintrenablex(Vctl* v, int tbdf)
451 {
452         Bus *bus;
453         Aintr *aintr;
454         Apic *apic;
455         Pcidev *pcidev;
456         int bno, dno, pin, hi, irq, lo, n, type, vno;
457
458         type = BUSTYPE(tbdf);
459         bno = BUSBNO(tbdf);
460         dno = BUSDNO(tbdf);
461
462         pin = 0;
463         pcidev = nil;
464         if(type == BusPCI){
465                 if(pcidev = pcimatchtbdf(tbdf))
466                         pin = pcicfgr8(pcidev, PciINTP);
467         } else if(type == BusISA)
468                 bno = mpisabus;
469
470 Findbus:
471         for(bus = mpbus; bus != nil; bus = bus->next){
472                 if(bus->type != type)
473                         continue;
474                 if(bus->busno == bno)
475                         break;
476         }
477
478         if(bus == nil){
479                 /*
480                  * if the PCI device is behind a PCI-PCI bridge thats not described
481                  * by the MP or ACPI tables then walk up the bus translating interrupt
482                  * pin to parent bus.
483                  */
484                 if(pcidev && pcidev->parent && pin > 0){
485                         pin = ((dno+(pin-1))%4)+1;
486                         pcidev = pcidev->parent;
487                         bno = BUSBNO(pcidev->tbdf);
488                         dno = BUSDNO(pcidev->tbdf);
489                         goto Findbus;
490                 }
491                 print("mpintrenable: can't find bus type %d, number %d\n", type, bno);
492                 return -1;
493         }
494
495         /*
496          * For PCI devices the interrupt pin (INT[ABCD]) and device
497          * number are encoded into the entry irq field, so create something
498          * to match on.
499          */
500         if(bus->type == BusPCI){
501                 if(pin > 0)
502                         irq = (dno<<2)|(pin-1);
503                 else
504                         irq = -1;
505         }
506         else
507                 irq = v->irq;
508
509         /*
510          * Find a matching interrupt entry from the list of interrupts
511          * attached to this bus.
512          */
513         for(aintr = bus->aintr; aintr; aintr = aintr->next){
514                 if(aintr->intr->irq != irq)
515                         continue;
516                 if(0){
517                         PCMPintr* p = aintr->intr;
518                         print("mpintrenablex: bus %d intin %d irq %d\n",
519                                 p->busno, p->intin, p->irq);
520                 }
521                 /*
522                  * Check if already enabled. Multifunction devices may share
523                  * INT[A-D]# so, if already enabled, check the polarity matches
524                  * and the trigger is level.
525                  *
526                  * Should check the devices differ only in the function number,
527                  * but that can wait for the planned enable/disable rewrite.
528                  * The RDT read here is safe for now as currently interrupts
529                  * are never disabled once enabled.
530                  */
531                 apic = aintr->apic;
532                 ioapicrdtr(apic, aintr->intr->intin, 0, &lo);
533                 if(!(lo & ApicIMASK)){
534                         vno = lo & 0xFF;
535                         if(0) print("%s vector %d (!imask)\n", v->name, vno);
536                         n = mpintrinit(bus, aintr->intr, vno, v->irq);
537                         n |= ApicPHYSICAL;              /* no-op */
538                         lo &= ~(ApicRemoteIRR|ApicDELIVS);
539                         if(n != lo){
540                                 print("mpintrenable: multiple botch irq %d, tbdf %uX, lo %8.8uX, n %8.8uX\n",
541                                         v->irq, tbdf, lo, n);
542                                 return -1;
543                         }
544                         v->isr = lapicisr;
545                         v->eoi = lapiceoi;
546                         return vno;
547                 }
548
549                 vno = allocvector();
550                 hi = mpintrcpu()<<24;
551                 lo = mpintrinit(bus, aintr->intr, vno, v->irq);
552                 lo |= ApicPHYSICAL;                     /* no-op */
553                 if(lo & ApicIMASK){
554                         print("mpintrenable: disabled irq %d, tbdf %uX, lo %8.8uX, hi %8.8uX\n",
555                                 v->irq, tbdf, lo, hi);
556                         return -1;
557                 }
558                 if((apic->flags & PcmpEN) && apic->type == PcmpIOAPIC)
559                         ioapicrdtw(apic, aintr->intr->intin, hi, lo);
560
561                 v->isr = lapicisr;
562                 v->eoi = lapiceoi;
563                 return vno;
564         }
565
566         return -1;
567 }
568
569 enum {
570         MSICtrl = 0x02, /* message control register (16 bit) */
571         MSIAddr = 0x04, /* message address register (64 bit) */
572         MSIData32 = 0x08, /* message data register for 32 bit MSI (16 bit) */
573         MSIData64 = 0x0C, /* message data register for 64 bit MSI (16 bit) */
574 };
575
576 static int
577 msiintrenable(Vctl *v)
578 {
579         int tbdf, vno, cap, cpu, ok64;
580         Pcidev *pci;
581
582         if(getconf("*msi") == nil)
583                 return -1;
584         tbdf = v->tbdf;
585         if(tbdf == BUSUNKNOWN || BUSTYPE(tbdf) != BusPCI)
586                 return -1;
587         pci = pcimatchtbdf(tbdf);
588         if(pci == nil) {
589                 print("msiintrenable: could not find Pcidev for tbdf %uX\n", tbdf);
590                 return -1;
591         }
592         cap = pcicap(pci, PciCapMSI);
593         if(cap < 0)
594                 return -1;
595         vno = allocvector();
596         cpu = mpintrcpu();
597         ok64 = (pcicfgr16(pci, cap + MSICtrl) & (1<<7)) != 0;
598         pcicfgw32(pci, cap + MSIAddr, (0xFEE << 20) | (cpu << 12));
599         if(ok64) pcicfgw32(pci, cap + MSIAddr + 4, 0);
600         pcicfgw16(pci, cap + (ok64 ? MSIData64 : MSIData32), vno | (1<<14));
601         pcicfgw16(pci, cap + MSICtrl, 1);
602         v->isr = lapicisr;
603         v->eoi = lapiceoi;
604         return vno;
605 }
606
607 int
608 mpintrenable(Vctl* v)
609 {
610         int irq, tbdf, vno;
611
612         vno = msiintrenable(v);
613         if(vno != -1)
614                 return vno;
615
616         /*
617          * If the bus is known, try it.
618          * BUSUNKNOWN is given both by [E]ISA devices and by
619          * interrupts local to the processor (local APIC, coprocessor
620          * breakpoint and page-fault).
621          */
622         tbdf = v->tbdf;
623         if(tbdf != BUSUNKNOWN && (vno = mpintrenablex(v, tbdf)) != -1)
624                 return vno;
625
626         irq = v->irq;
627         if(irq >= IrqLINT0 && irq <= MaxIrqLAPIC){
628                 if(irq != IrqSPURIOUS)
629                         v->isr = lapiceoi;
630                 return VectorPIC+irq;
631         }
632         if(irq < 0 || irq > MaxIrqPIC){
633                 print("mpintrenable: irq %d out of range\n", irq);
634                 return -1;
635         }
636
637         /*
638          * Either didn't find it or have to try the default buses
639          * (ISA and EISA). This hack is due to either over-zealousness 
640          * or laziness on the part of some manufacturers.
641          *
642          * The MP configuration table on some older systems
643          * (e.g. ASUS PCI/E-P54NP4) has an entry for the EISA bus
644          * but none for ISA. It also has the interrupt type and
645          * polarity set to 'default for this bus' which wouldn't
646          * be compatible with ISA.
647          */
648         if(mpeisabus != -1){
649                 vno = mpintrenablex(v, MKBUS(BusEISA, 0, 0, 0));
650                 if(vno != -1)
651                         return vno;
652         }
653         if(mpisabus != -1){
654                 vno = mpintrenablex(v, MKBUS(BusISA, 0, 0, 0));
655                 if(vno != -1)
656                         return vno;
657         }
658         print("mpintrenable: out of choices eisa %d isa %d tbdf %uX irq %d\n",
659                 mpeisabus, mpisabus, v->tbdf, v->irq);
660         return -1;
661 }
662
663
664 void
665 mpshutdown(void)
666 {
667         static Lock shutdownlock;
668
669         /*
670          * To be done...
671          */
672         if(!canlock(&shutdownlock)){
673                 /*
674                  * If this processor received the CTRL-ALT-DEL from
675                  * the keyboard, acknowledge it. Send an INIT to self.
676                  */
677 #ifdef FIXTHIS
678                 if(lapicisr(VectorKBD))
679                         lapiceoi(VectorKBD);
680 #endif /* FIX THIS */
681                 arch->introff();
682                 idle();
683         }
684
685         print("apshutdown: active = %#8.8ux\n", active.machs);
686         delay(1000);
687         splhi();
688
689         /*
690          * INIT all excluding self.
691          */
692         lapicicrw(0, 0x000C0000|ApicINIT);
693
694         pcireset();
695         i8042reset();
696
697         /*
698          * Often the BIOS hangs during restart if a conventional 8042
699          * warm-boot sequence is tried. The following is Intel specific and
700          * seems to perform a cold-boot, but at least it comes back.
701          * And sometimes there is no keyboard...
702          *
703          * The reset register (0xcf9) is usually in one of the bridge
704          * chips. The actual location and sequence could be extracted from
705          * ACPI but why bother, this is the end of the line anyway.
706          */
707         print("no kbd; trying bios warm boot...");
708         *(ushort*)KADDR(0x472) = 0x1234;        /* BIOS warm-boot flag */
709         outb(0xCF9, 0x02);
710         outb(0xCF9, 0x06);
711
712         print("can't reset\n");
713         for(;;)
714                 idle();
715 }