]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/mp.c
pc/pc64: bring up ap's one after another, use idlehands() while waiting for thunderbi...
[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 uvlong
94 tscticks(uvlong *hz)
95 {
96         if(hz != nil)
97                 *hz = m->cpuhz;
98
99         cycles(&m->tscticks);   /* Uses the rdtsc instruction */
100         return m->tscticks;
101 }
102
103 void
104 syncclock(void)
105 {
106         uvlong x;
107
108         if(arch->fastclock != tscticks)
109                 return;
110
111         if(m->machno == 0){
112                 wrmsr(0x10, 0);
113                 m->tscticks = 0;
114         } else {
115                 x = MACHP(0)->tscticks;
116                 while(x == MACHP(0)->tscticks)
117                         ;
118                 wrmsr(0x10, MACHP(0)->tscticks);
119                 cycles(&m->tscticks);
120         }
121 }
122
123 void
124 mpinit(void)
125 {
126         int ncpu, i;
127         Apic *apic;
128         char *cp;
129
130         i8259init();
131         syncclock();
132
133         if(getconf("*apicdebug")){
134                 Bus *b;
135                 Aintr *ai;
136                 PCMPintr *pi;
137
138                 for(i=0; i<=MaxAPICNO; i++){
139                         if(apic = mpapic[i])
140                                 print("LAPIC%d: pa=%lux va=%#p flags=%x\n",
141                                         i, apic->paddr, apic->addr, apic->flags);
142                         if(apic = mpioapic[i])
143                                 print("IOAPIC%d: pa=%lux va=%#p flags=%x gsibase=%d mre=%d\n",
144                                         i, apic->paddr, apic->addr, apic->flags, apic->gsibase, apic->mre);
145                 }
146                 for(b = mpbus; b; b = b->next){
147                         print("BUS%d type=%d flags=%x\n", b->busno, b->type, b->po|b->el);
148                         for(ai = b->aintr; ai; ai = ai->next){
149                                 if(pi = ai->intr)
150                                         print("\ttype=%d irq=%d (%d [%c]) apic=%d intin=%d flags=%x\n",
151                                                 pi->type, pi->irq, pi->irq>>2, "ABCD"[pi->irq&3],
152                                                 pi->apicno, pi->intin, pi->flags);
153                         }
154                 }
155         }
156
157         apic = nil;
158         for(i=0; i<=MaxAPICNO; i++){
159                 if(mpapic[i] == nil)
160                         continue;
161                 if(mpapic[i]->flags & PcmpBP){
162                         apic = mpapic[i];
163                         break;
164                 }
165         }
166
167         if(apic == nil){
168                 panic("mpinit: no bootstrap processor");
169                 return;
170         }
171         apic->online = 1;
172
173         lapicinit(apic);
174
175         /*
176          * These interrupts are local to the processor
177          * and do not appear in the I/O APIC so it is OK
178          * to set them now.
179          */
180         intrenable(IrqTIMER, lapicclock, 0, BUSUNKNOWN, "clock");
181         intrenable(IrqERROR, lapicerror, 0, BUSUNKNOWN, "lapicerror");
182         intrenable(IrqSPURIOUS, lapicspurious, 0, BUSUNKNOWN, "lapicspurious");
183         lapiconline();
184
185         /*
186          * Initialise the application processors.
187          */
188         if(cp = getconf("*ncpu")){
189                 ncpu = strtol(cp, 0, 0);
190                 if(ncpu < 1)
191                         ncpu = 1;
192                 else if(ncpu > MAXMACH)
193                         ncpu = MAXMACH;
194         }
195         else
196                 ncpu = MAXMACH;
197         memmove((void*)APBOOTSTRAP, apbootstrap, sizeof(apbootstrap));
198         for(i=0; i<nelem(mpapic); i++){
199                 if((apic = mpapic[i]) == nil || apic->machno == 0 || apic->machno >= MAXMACH)
200                         continue;
201                 if(ncpu <= 1)
202                         break;
203                 if((apic->flags & (PcmpBP|PcmpEN)) == PcmpEN){
204                         mpstartap(apic);
205                         conf.nmach++;
206                         ncpu--;
207
208                         if(!apic->online){
209                                 print("LAPIC%d: cpu%d did not startup\n", i, apic->machno);
210                                 continue;
211                         }
212
213                         /* update tscticks for ap's syncclock() */
214                         while(!active.machs[apic->machno]){
215                                 if(arch->fastclock == tscticks)
216                                         cycles(&m->tscticks);
217                         }
218                 }
219         }
220
221         /*
222          *  we don't really know the number of processors till
223          *  here.
224          *
225          *  set conf.copymode here if nmach > 1.
226          *  Should look for an ExtINT line and enable it.
227          */
228         if(X86FAMILY(m->cpuidax) == 3 || conf.nmach > 1)
229                 conf.copymode = 1;
230 }
231
232 static int
233 mpintrcpu(void)
234 {
235         static Lock physidlock;
236         static int physid;
237         int i;
238
239         /*
240          * The bulk of this code was written ~1995, when there was
241          * one architecture and one generation of hardware, the number
242          * of CPUs was up to 4(8) and the choices for interrupt routing
243          * were physical, or flat logical (optionally with lowest
244          * priority interrupt). Logical mode hasn't scaled well with
245          * the increasing number of packages/cores/threads, so the
246          * fall-back is to physical mode, which works across all processor
247          * generations, both AMD and Intel, using the APIC and xAPIC.
248          *
249          * Interrupt routing policy can be set here.
250          * Currently, just assign each interrupt to a different CPU on
251          * a round-robin basis. Some idea of the packages/cores/thread
252          * topology would be useful here, e.g. to not assign interrupts
253          * to more than one thread in a core, or to use a "noise" core.
254          * But, as usual, Intel make that an onerous task. 
255          */
256         lock(&physidlock);
257         for(;;){
258                 i = physid++;
259                 if(physid >= nelem(mpapic))
260                         physid = 0;
261                 if(mpapic[i] == nil)
262                         continue;
263                 if(mpapic[i]->online)
264                         break;
265         }
266         unlock(&physidlock);
267
268         return mpapic[i]->apicno;
269 }
270
271 /*
272  * With the APIC a unique vector can be assigned to each
273  * request to enable an interrupt. There are two reasons this
274  * is a good idea:
275  * 1) to prevent lost interrupts, no more than 2 interrupts
276  *    should be assigned per block of 16 vectors (there is an
277  *    in-service entry and a holding entry for each priority
278  *    level and there is one priority level per block of 16
279  *    interrupts).
280  * 2) each input pin on the IOAPIC will receive a different
281  *    vector regardless of whether the devices on that pin use
282  *    the same IRQ as devices on another pin.
283  */
284 static int
285 allocvector(void)
286 {
287         static int round = 0, num = 0;
288         static Lock l;
289         int vno;
290         
291         lock(&l);
292         vno = VectorAPIC + num;
293         if(vno < MaxVectorAPIC-7)
294                 num += 8;
295         else
296                 num = ++round % 8;
297         unlock(&l);
298         return vno;
299 }
300
301 static int
302 mpintrenablex(Vctl* v, int tbdf)
303 {
304         Bus *bus;
305         Aintr *aintr;
306         Apic *apic;
307         Pcidev *pcidev;
308         int bno, dno, pin, hi, irq, lo, n, type, vno;
309
310         type = BUSTYPE(tbdf);
311         bno = BUSBNO(tbdf);
312         dno = BUSDNO(tbdf);
313
314         pin = 0;
315         pcidev = nil;
316         if(type == BusPCI){
317                 if(pcidev = pcimatchtbdf(tbdf))
318                         pin = pcicfgr8(pcidev, PciINTP);
319         } else if(type == BusISA)
320                 bno = mpisabus;
321
322 Findbus:
323         for(bus = mpbus; bus != nil; bus = bus->next){
324                 if(bus->type != type)
325                         continue;
326                 if(bus->busno == bno)
327                         break;
328         }
329
330         if(bus == nil){
331                 /*
332                  * if the PCI device is behind a PCI-PCI bridge thats not described
333                  * by the MP or ACPI tables then walk up the bus translating interrupt
334                  * pin to parent bus.
335                  */
336                 if(pcidev && pcidev->parent && pin > 0){
337                         pin = ((dno+(pin-1))%4)+1;
338                         pcidev = pcidev->parent;
339                         bno = BUSBNO(pcidev->tbdf);
340                         dno = BUSDNO(pcidev->tbdf);
341                         goto Findbus;
342                 }
343                 print("mpintrenable: can't find bus type %d, number %d\n", type, bno);
344                 return -1;
345         }
346
347         /*
348          * For PCI devices the interrupt pin (INT[ABCD]) and device
349          * number are encoded into the entry irq field, so create something
350          * to match on.
351          */
352         if(bus->type == BusPCI){
353                 if(pin > 0)
354                         irq = (dno<<2)|(pin-1);
355                 else
356                         irq = -1;
357         }
358         else
359                 irq = v->irq;
360
361         /*
362          * Find a matching interrupt entry from the list of interrupts
363          * attached to this bus.
364          */
365         for(aintr = bus->aintr; aintr; aintr = aintr->next){
366                 if(aintr->intr->irq != irq)
367                         continue;
368                 if(0){
369                         PCMPintr* p = aintr->intr;
370                         print("mpintrenablex: bus %d intin %d irq %d\n",
371                                 p->busno, p->intin, p->irq);
372                 }
373                 /*
374                  * Check if already enabled. Multifunction devices may share
375                  * INT[A-D]# so, if already enabled, check the polarity matches
376                  * and the trigger is level.
377                  *
378                  * Should check the devices differ only in the function number,
379                  * but that can wait for the planned enable/disable rewrite.
380                  * The RDT read here is safe for now as currently interrupts
381                  * are never disabled once enabled.
382                  */
383                 apic = aintr->apic;
384                 ioapicrdtr(apic, aintr->intr->intin, 0, &lo);
385                 if(!(lo & ApicIMASK)){
386                         vno = lo & 0xFF;
387                         if(0) print("%s vector %d (!imask)\n", v->name, vno);
388                         n = mpintrinit(bus, aintr->intr, vno, v->irq);
389                         n |= ApicPHYSICAL;              /* no-op */
390                         lo &= ~(ApicRemoteIRR|ApicDELIVS);
391                         if(n != lo){
392                                 print("mpintrenable: multiple botch irq %d, tbdf %uX, lo %8.8uX, n %8.8uX\n",
393                                         v->irq, tbdf, lo, n);
394                                 return -1;
395                         }
396                         v->isr = lapicisr;
397                         v->eoi = lapiceoi;
398                         return vno;
399                 }
400
401                 vno = allocvector();
402                 hi = mpintrcpu()<<24;
403                 lo = mpintrinit(bus, aintr->intr, vno, v->irq);
404                 lo |= ApicPHYSICAL;                     /* no-op */
405                 if(lo & ApicIMASK){
406                         print("mpintrenable: disabled irq %d, tbdf %uX, lo %8.8uX, hi %8.8uX\n",
407                                 v->irq, tbdf, lo, hi);
408                         return -1;
409                 }
410                 if((apic->flags & PcmpEN) && apic->type == PcmpIOAPIC)
411                         ioapicrdtw(apic, aintr->intr->intin, hi, lo);
412
413                 v->isr = lapicisr;
414                 v->eoi = lapiceoi;
415                 return vno;
416         }
417
418         return -1;
419 }
420
421 enum {
422         MSICtrl = 0x02, /* message control register (16 bit) */
423         MSIAddr = 0x04, /* message address register (64 bit) */
424         MSIData32 = 0x08, /* message data register for 32 bit MSI (16 bit) */
425         MSIData64 = 0x0C, /* message data register for 64 bit MSI (16 bit) */
426 };
427
428 enum {
429         HTMSIMapping    = 0xA8,
430         HTMSIFlags      = 0x02,
431         HTMSIFlagsEn    = 0x01,
432 };
433
434 static int
435 htmsicapenable(Pcidev *p)
436 {
437         int cap, flags;
438
439         if((cap = pcihtcap(p, HTMSIMapping)) <= 0)
440                 return -1;
441         flags = pcicfgr8(p, cap + HTMSIFlags);
442         if((flags & HTMSIFlagsEn) == 0)
443                 pcicfgw8(p, cap + HTMSIFlags, flags | HTMSIFlagsEn);
444         return 0;
445 }
446
447 static int
448 htmsienable(Pcidev *pdev)
449 {
450         Pcidev *p;
451
452         p = nil;
453         while((p = pcimatch(p, 0x1022, 0)) != nil)
454                 if(p->did == 0x1103 || p->did == 0x1203)
455                         break;
456
457         if(p == nil)
458                 return 0;       /* not hypertransport platform */
459
460         p = nil;
461         while((p = pcimatch(p, 0x10de, 0)) != nil){
462                 switch(p->did){
463                 case 0x02f0:    /* NVIDIA NFORCE C51 MEMC0 */
464                 case 0x02f1:    /* NVIDIA NFORCE C51 MEMC1 */
465                 case 0x02f2:    /* NVIDIA NFORCE C51 MEMC2 */
466                 case 0x02f3:    /* NVIDIA NFORCE C51 MEMC3 */
467                 case 0x02f4:    /* NVIDIA NFORCE C51 MEMC4 */
468                 case 0x02f5:    /* NVIDIA NFORCE C51 MEMC5 */
469                 case 0x02f6:    /* NVIDIA NFORCE C51 MEMC6 */
470                 case 0x02f7:    /* NVIDIA NFORCE C51 MEMC7 */
471                 case 0x0369:    /* NVIDIA NFORCE MCP55 MEMC */
472                         htmsicapenable(p);
473                         break;
474                 }
475         }
476
477         if(htmsicapenable(pdev) == 0)
478                 return 0;
479
480         for(p = pdev->parent; p != nil; p = p->parent)
481                 if(htmsicapenable(p) == 0)
482                         return 0;
483
484         return -1;
485 }
486
487 static int
488 msiintrenable(Vctl *v)
489 {
490         int tbdf, vno, cap, cpu, ok64;
491         Pcidev *pci;
492
493         if(getconf("*nomsi") != nil)
494                 return -1;
495         tbdf = v->tbdf;
496         if(tbdf == BUSUNKNOWN || BUSTYPE(tbdf) != BusPCI)
497                 return -1;
498         pci = pcimatchtbdf(tbdf);
499         if(pci == nil) {
500                 print("msiintrenable: could not find Pcidev for tbdf %uX\n", tbdf);
501                 return -1;
502         }
503         if(htmsienable(pci) < 0)
504                 return -1;
505         cap = pcicap(pci, PciCapMSI);
506         if(cap < 0)
507                 return -1;
508         vno = allocvector();
509         cpu = mpintrcpu();
510         ok64 = (pcicfgr16(pci, cap + MSICtrl) & (1<<7)) != 0;
511         pcicfgw32(pci, cap + MSIAddr, (0xFEE << 20) | (cpu << 12));
512         if(ok64) pcicfgw32(pci, cap + MSIAddr + 4, 0);
513         pcicfgw16(pci, cap + (ok64 ? MSIData64 : MSIData32), vno | (1<<14));
514         pcicfgw16(pci, cap + MSICtrl, 1);
515         v->isr = lapicisr;
516         v->eoi = lapiceoi;
517         return vno;
518 }
519
520 int
521 mpintrenable(Vctl* v)
522 {
523         int irq, tbdf, vno;
524
525         vno = msiintrenable(v);
526         if(vno != -1)
527                 return vno;
528
529         /*
530          * If the bus is known, try it.
531          * BUSUNKNOWN is given both by [E]ISA devices and by
532          * interrupts local to the processor (local APIC, coprocessor
533          * breakpoint and page-fault).
534          */
535         tbdf = v->tbdf;
536         if(tbdf != BUSUNKNOWN && (vno = mpintrenablex(v, tbdf)) != -1)
537                 return vno;
538
539         irq = v->irq;
540         if(irq >= IrqLINT0 && irq <= MaxIrqLAPIC){
541                 if(irq != IrqSPURIOUS)
542                         v->isr = lapiceoi;
543                 return VectorPIC+irq;
544         }
545         if(irq < 0 || irq > MaxIrqPIC){
546                 print("mpintrenable: irq %d out of range\n", irq);
547                 return -1;
548         }
549
550         /*
551          * Either didn't find it or have to try the default buses
552          * (ISA and EISA). This hack is due to either over-zealousness 
553          * or laziness on the part of some manufacturers.
554          *
555          * The MP configuration table on some older systems
556          * (e.g. ASUS PCI/E-P54NP4) has an entry for the EISA bus
557          * but none for ISA. It also has the interrupt type and
558          * polarity set to 'default for this bus' which wouldn't
559          * be compatible with ISA.
560          */
561         if(mpeisabus != -1){
562                 vno = mpintrenablex(v, MKBUS(BusEISA, 0, 0, 0));
563                 if(vno != -1)
564                         return vno;
565         }
566         if(mpisabus != -1){
567                 vno = mpintrenablex(v, MKBUS(BusISA, 0, 0, 0));
568                 if(vno != -1)
569                         return vno;
570         }
571         print("mpintrenable: out of choices eisa %d isa %d tbdf %uX irq %d\n",
572                 mpeisabus, mpisabus, v->tbdf, v->irq);
573         return -1;
574 }
575
576 void
577 mpshutdown(void)
578 {
579         /*
580          * Park application processors.
581          */
582         if(m->machno != 0){
583                 splhi();
584                 arch->introff();
585                 for(;;) idle();
586         }
587         delay(1000);
588         splhi();
589
590         /*
591          * INIT all excluding self.
592          */
593         lapicicrw(0, 0x000C0000|ApicINIT);
594
595         pcireset();
596 }