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