mp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. #include "mp.h"
  9. #include "apbootstrap.h"
  10. #define dprint(...) if(mpdebug) print(__VA_ARGS__); else USED(mpdebug)
  11. /* from mpacpi.c */
  12. Apic *bootapic;
  13. int mpdebug;
  14. void (*mpacpifunc)(void);
  15. static PCMP* mppcmp;
  16. static Bus* mpbus;
  17. static Bus* mpbuslast;
  18. static int mpisabus = -1;
  19. static int mpeisabus = -1;
  20. extern int i8259elcr; /* mask of level-triggered interrupts */
  21. /* static */ Apic mpapic[MaxAPICNO+1];
  22. /* static */ int machno2apicno[MaxAPICNO+1]; /* inverse map: machno -> APIC ID */
  23. static Ref mpvnoref; /* unique vector assignment */
  24. static int mpmachno = 1;
  25. static Lock mpphysidlock;
  26. static int mpphysid;
  27. static char* buses[] = {
  28. "CBUSI ",
  29. "CBUSII",
  30. "EISA ",
  31. "FUTURE",
  32. "INTERN",
  33. "ISA ",
  34. "MBI ",
  35. "MBII ",
  36. "MCA ",
  37. "MPI ",
  38. "MPSA ",
  39. "NUBUS ",
  40. "PCI ",
  41. "PCMCIA",
  42. "TC ",
  43. "VL ",
  44. "VME ",
  45. "XPRESS",
  46. 0,
  47. };
  48. static Apic*
  49. mkprocessor(PCMPprocessor* p)
  50. {
  51. int apicno;
  52. Apic *apic;
  53. apicno = p->apicno;
  54. if(!(p->flags & PcmpEN) || apicno > MaxAPICNO)
  55. return 0;
  56. apic = &mpapic[apicno];
  57. apic->type = PcmpPROCESSOR;
  58. apic->apicno = apicno;
  59. apic->flags = p->flags;
  60. apic->lintr[0] = ApicIMASK;
  61. apic->lintr[1] = ApicIMASK;
  62. if(p->flags & PcmpBP){
  63. machno2apicno[0] = apicno;
  64. apic->machno = 0;
  65. }
  66. else{
  67. machno2apicno[mpmachno] = apicno;
  68. apic->machno = mpmachno;
  69. mpmachno++;
  70. }
  71. return apic;
  72. }
  73. static Bus*
  74. mkbus(PCMPbus* p)
  75. {
  76. Bus *bus;
  77. int i;
  78. for(i = 0; buses[i]; i++){
  79. if(strncmp(buses[i], p->string, sizeof(p->string)) == 0)
  80. break;
  81. }
  82. if(buses[i] == 0)
  83. return 0;
  84. bus = xalloc(sizeof(Bus));
  85. if(mpbus)
  86. mpbuslast->next = bus;
  87. else
  88. mpbus = bus;
  89. mpbuslast = bus;
  90. bus->type = i;
  91. bus->busno = p->busno;
  92. if(bus->type == BusEISA){
  93. bus->po = PcmpLOW;
  94. bus->el = PcmpLEVEL;
  95. if(mpeisabus != -1)
  96. print("mkbus: more than one EISA bus\n");
  97. mpeisabus = bus->busno;
  98. }
  99. else if(bus->type == BusPCI){
  100. bus->po = PcmpLOW;
  101. bus->el = PcmpLEVEL;
  102. }
  103. else if(bus->type == BusISA){
  104. bus->po = PcmpHIGH;
  105. bus->el = PcmpEDGE;
  106. if(mpisabus != -1)
  107. print("mkbus: more than one ISA bus\n");
  108. mpisabus = bus->busno;
  109. }
  110. else{
  111. bus->po = PcmpHIGH;
  112. bus->el = PcmpEDGE;
  113. }
  114. return bus;
  115. }
  116. static Bus*
  117. mpgetbus(int busno)
  118. {
  119. Bus *bus;
  120. for(bus = mpbus; bus; bus = bus->next){
  121. if(bus->busno == busno)
  122. return bus;
  123. }
  124. print("mpgetbus: can't find bus %d\n", busno);
  125. return 0;
  126. }
  127. static Apic*
  128. mkioapic(PCMPioapic* p)
  129. {
  130. void *va;
  131. int apicno;
  132. Apic *apic;
  133. apicno = p->apicno;
  134. if(!(p->flags & PcmpEN) || apicno > MaxAPICNO)
  135. return 0;
  136. /*
  137. * Map the I/O APIC.
  138. */
  139. if((va = vmap(p->addr, 1024)) == nil)
  140. return 0;
  141. apic = &mpapic[apicno];
  142. apic->type = PcmpIOAPIC;
  143. apic->apicno = apicno;
  144. apic->addr = va;
  145. apic->paddr = p->addr;
  146. apic->flags = p->flags;
  147. return apic;
  148. }
  149. static Aintr*
  150. mkiointr(PCMPintr* p)
  151. {
  152. Bus *bus;
  153. Aintr *aintr;
  154. PCMPintr* pcmpintr;
  155. /*
  156. * According to the MultiProcessor Specification, a destination
  157. * I/O APIC of 0xFF means the signal is routed to all I/O APICs.
  158. * It's unclear how that can possibly be correct so treat it as
  159. * an error for now.
  160. */
  161. if(p->apicno == 0xFF)
  162. return 0;
  163. if((bus = mpgetbus(p->busno)) == 0)
  164. return 0;
  165. aintr = xalloc(sizeof(Aintr));
  166. aintr->intr = p;
  167. if(0)
  168. dprint("mkiointr: type %d intr type %d flags %#o "
  169. "bus %d irq %d apicno %d intin %d\n",
  170. p->type, p->intr, p->flags,
  171. p->busno, p->irq, p->apicno, p->intin);
  172. /*
  173. * Hack for Intel SR1520ML motherboard, which BIOS describes
  174. * the i82575 dual ethernet controllers incorrectly.
  175. */
  176. if(mppcmp && memcmp(mppcmp->product, "INTEL X38MLST ", 20) == 0){
  177. if(p->busno == 1 && p->intin == 16 && p->irq == 1){
  178. pcmpintr = malloc(sizeof(PCMPintr));
  179. if(pcmpintr == nil)
  180. panic("mkiointr: no memory");
  181. memmove(pcmpintr, p, sizeof(PCMPintr));
  182. print("mkiointr: %20.20s bus %d intin %d irq %d\n",
  183. (char*)mppcmp->product,
  184. pcmpintr->busno, pcmpintr->intin,
  185. pcmpintr->irq);
  186. pcmpintr->intin = 17;
  187. aintr->intr = pcmpintr;
  188. }
  189. }
  190. if ((unsigned)p->apicno >= nelem(mpapic))
  191. panic("mkiointr: apic %d out of range", p->apicno);
  192. aintr->apic = &mpapic[p->apicno];
  193. aintr->next = bus->aintr;
  194. bus->aintr = aintr;
  195. return aintr;
  196. }
  197. static int
  198. mpintrinit(Bus* bus, PCMPintr* intr, int vno, int /*irq*/)
  199. {
  200. int el, po, v;
  201. /*
  202. * Parse an I/O or Local APIC interrupt table entry and
  203. * return the encoded vector.
  204. */
  205. v = vno;
  206. po = intr->flags & PcmpPOMASK;
  207. el = intr->flags & PcmpELMASK;
  208. switch(intr->intr){
  209. default: /* PcmpINT */
  210. v |= ApicFIXED; /* no-op */
  211. break;
  212. case PcmpNMI:
  213. v |= ApicNMI;
  214. po = PcmpHIGH;
  215. el = PcmpEDGE;
  216. break;
  217. case PcmpSMI:
  218. v |= ApicSMI;
  219. break;
  220. case PcmpExtINT:
  221. v |= ApicExtINT;
  222. /*
  223. * The AMI Goliath doesn't boot successfully with it's LINTR0
  224. * entry which decodes to low+level. The PPro manual says ExtINT
  225. * should be level, whereas the Pentium is edge. Setting the
  226. * Goliath to edge+high seems to cure the problem. Other PPro
  227. * MP tables (e.g. ASUS P/I-P65UP5 have a entry which decodes
  228. * to edge+high, so who knows.
  229. * Perhaps it would be best just to not set an ExtINT entry at
  230. * all, it shouldn't be needed for SMP mode.
  231. */
  232. po = PcmpHIGH;
  233. el = PcmpEDGE;
  234. break;
  235. }
  236. /*
  237. */
  238. if(bus->type == BusEISA && !po && !el /*&& !(i8259elcr & (1<<irq))*/){
  239. po = PcmpHIGH;
  240. el = PcmpEDGE;
  241. }
  242. if(!po)
  243. po = bus->po;
  244. if(po == PcmpLOW)
  245. v |= ApicLOW;
  246. else if(po != PcmpHIGH){
  247. print("mpintrinit: bad polarity 0x%uX\n", po);
  248. return ApicIMASK;
  249. }
  250. if(!el)
  251. el = bus->el;
  252. if(el == PcmpLEVEL)
  253. v |= ApicLEVEL;
  254. else if(el != PcmpEDGE){
  255. print("mpintrinit: bad trigger 0x%uX\n", el);
  256. return ApicIMASK;
  257. }
  258. return v;
  259. }
  260. static int
  261. mklintr(PCMPintr* p)
  262. {
  263. Apic *apic;
  264. Bus *bus;
  265. int intin, v;
  266. /*
  267. * The offsets of vectors for LINT[01] are known to be
  268. * 0 and 1 from the local APIC vector space at VectorLAPIC.
  269. */
  270. if((bus = mpgetbus(p->busno)) == 0)
  271. return 0;
  272. intin = p->intin;
  273. /*
  274. * Pentium Pros have problems if LINT[01] are set to ExtINT
  275. * so just bag it, SMP mode shouldn't need ExtINT anyway.
  276. */
  277. if(p->intr == PcmpExtINT || p->intr == PcmpNMI)
  278. v = ApicIMASK;
  279. else
  280. v = mpintrinit(bus, p, VectorLAPIC+intin, p->irq);
  281. if(p->apicno == 0xFF){
  282. for(apic = mpapic; apic <= &mpapic[MaxAPICNO]; apic++){
  283. if((apic->flags & PcmpEN)
  284. && apic->type == PcmpPROCESSOR)
  285. apic->lintr[intin] = v;
  286. }
  287. }
  288. else{
  289. if ((unsigned)p->apicno >= nelem(mpapic))
  290. panic("mklintr: ioapic %d out of range", p->apicno);
  291. apic = &mpapic[p->apicno];
  292. if((apic->flags & PcmpEN) && apic->type == PcmpPROCESSOR)
  293. apic->lintr[intin] = v;
  294. }
  295. return v;
  296. }
  297. static void
  298. checkmtrr(void)
  299. {
  300. int i, vcnt;
  301. Mach *mach0;
  302. /*
  303. * If there are MTRR registers, snarf them for validation.
  304. */
  305. if(!(m->cpuiddx & 0x1000))
  306. return;
  307. rdmsr(0x0FE, &m->mtrrcap);
  308. rdmsr(0x2FF, &m->mtrrdef);
  309. if(m->mtrrcap & 0x0100){
  310. rdmsr(0x250, &m->mtrrfix[0]);
  311. rdmsr(0x258, &m->mtrrfix[1]);
  312. rdmsr(0x259, &m->mtrrfix[2]);
  313. for(i = 0; i < 8; i++)
  314. rdmsr(0x268+i, &m->mtrrfix[(i+3)]);
  315. }
  316. vcnt = m->mtrrcap & 0x00FF;
  317. if(vcnt > nelem(m->mtrrvar))
  318. vcnt = nelem(m->mtrrvar);
  319. for(i = 0; i < vcnt; i++)
  320. rdmsr(0x200+i, &m->mtrrvar[i]);
  321. /*
  322. * If not the bootstrap processor, compare.
  323. */
  324. if(m->machno == 0)
  325. return;
  326. mach0 = MACHP(0);
  327. if(mach0->mtrrcap != m->mtrrcap)
  328. print("mtrrcap%d: %lluX %lluX\n",
  329. m->machno, mach0->mtrrcap, m->mtrrcap);
  330. if(mach0->mtrrdef != m->mtrrdef)
  331. print("mtrrdef%d: %lluX %lluX\n",
  332. m->machno, mach0->mtrrdef, m->mtrrdef);
  333. for(i = 0; i < 11; i++){
  334. if(mach0->mtrrfix[i] != m->mtrrfix[i])
  335. print("mtrrfix%d: i%d: %lluX %lluX\n",
  336. m->machno, i, mach0->mtrrfix[i], m->mtrrfix[i]);
  337. }
  338. for(i = 0; i < vcnt; i++){
  339. if(mach0->mtrrvar[i] != m->mtrrvar[i])
  340. print("mtrrvar%d: i%d: %lluX %lluX\n",
  341. m->machno, i, mach0->mtrrvar[i], m->mtrrvar[i]);
  342. }
  343. }
  344. static void
  345. squidboy(Apic* apic)
  346. {
  347. // iprint("Hello Squidboy\n");
  348. machinit();
  349. mmuinit();
  350. cpuidentify();
  351. cpuidprint();
  352. checkmtrr();
  353. apic->online = 1;
  354. lapicinit(apic);
  355. lapiconline();
  356. syncclock();
  357. timersinit();
  358. fpoff();
  359. lock(&active);
  360. active.machs |= 1<<m->machno;
  361. unlock(&active);
  362. while(!active.thunderbirdsarego)
  363. microdelay(100);
  364. schedinit();
  365. }
  366. static void
  367. mpstartap(Apic* apic)
  368. {
  369. ulong *apbootp, *pdb, *pte;
  370. Mach *mach, *mach0;
  371. int i, machno;
  372. uchar *p;
  373. mach0 = MACHP(0);
  374. /*
  375. * Initialise the AP page-tables and Mach structure. The page-tables
  376. * are the same as for the bootstrap processor with the exception of
  377. * the PTE for the Mach structure.
  378. * Xspanalloc will panic if an allocation can't be made.
  379. */
  380. p = xspanalloc(4*BY2PG, BY2PG, 0);
  381. pdb = (ulong*)p;
  382. memmove(pdb, mach0->pdb, BY2PG);
  383. p += BY2PG;
  384. if((pte = mmuwalk(pdb, MACHADDR, 1, 0)) == nil)
  385. return;
  386. memmove(p, KADDR(PPN(*pte)), BY2PG);
  387. *pte = PADDR(p)|PTEWRITE|PTEVALID;
  388. if(mach0->havepge)
  389. *pte |= PTEGLOBAL;
  390. p += BY2PG;
  391. mach = (Mach*)p;
  392. if((pte = mmuwalk(pdb, MACHADDR, 2, 0)) == nil)
  393. return;
  394. *pte = PADDR(mach)|PTEWRITE|PTEVALID;
  395. if(mach0->havepge)
  396. *pte |= PTEGLOBAL;
  397. p += BY2PG;
  398. machno = apic->machno;
  399. MACHP(machno) = mach;
  400. mach->machno = machno;
  401. mach->pdb = pdb;
  402. mach->gdt = (Segdesc*)p; /* filled by mmuinit */
  403. /*
  404. * Tell the AP where its kernel vector and pdb are.
  405. * The offsets are known in the AP bootstrap code.
  406. */
  407. apbootp = (ulong*)(APBOOTSTRAP+0x08);
  408. *apbootp++ = (ulong)squidboy;
  409. *apbootp++ = PADDR(pdb);
  410. *apbootp = (ulong)apic;
  411. /*
  412. * Universal Startup Algorithm.
  413. */
  414. p = KADDR(0x467);
  415. *p++ = PADDR(APBOOTSTRAP);
  416. *p++ = PADDR(APBOOTSTRAP)>>8;
  417. i = (PADDR(APBOOTSTRAP) & ~0xFFFF)/16;
  418. /* code assumes i==0 */
  419. if(i != 0)
  420. print("mp: bad APBOOTSTRAP\n");
  421. *p++ = i;
  422. *p = i>>8;
  423. nvramwrite(0x0F, 0x0A);
  424. lapicstartap(apic, PADDR(APBOOTSTRAP));
  425. for(i = 0; i < 1000; i++){
  426. if(apic->online)
  427. break;
  428. delay(10);
  429. }
  430. nvramwrite(0x0F, 0x00);
  431. }
  432. static void
  433. trympacpi(void)
  434. {
  435. if (mpacpifunc != nil) {
  436. print("mpinit: scanning acpi madt for extra cpus\n");
  437. (*mpacpifunc)();
  438. }
  439. }
  440. void
  441. mpinit(void)
  442. {
  443. int ncpu, cpuson;
  444. char *cp;
  445. PCMP *pcmp;
  446. uchar *e, *p;
  447. Apic *apic, *bpapic;
  448. void *va;
  449. mpdebug = getconf("*debugmp") != nil;
  450. i8259init();
  451. syncclock();
  452. bpapic = nil;
  453. cpuson = 0;
  454. if(_mp_ == 0) {
  455. /*
  456. * We can easily get processor info from ACPI, but
  457. * interrupt routing, etc. would require interpreting AML.
  458. */
  459. print("mpinit: no mp table found, assuming uniprocessor\n");
  460. archrevert();
  461. return;
  462. }
  463. pcmp = KADDR(_mp_->physaddr);
  464. /*
  465. * Map the local APIC.
  466. */
  467. if((va = vmap(pcmp->lapicbase, 1024)) == nil)
  468. return;
  469. mppcmp = pcmp;
  470. print("LAPIC: %#lux %#lux\n", pcmp->lapicbase, (ulong)va);
  471. /*
  472. * Run through the table saving information needed for starting
  473. * application processors and initialising any I/O APICs. The table
  474. * is guaranteed to be in order such that only one pass is necessary.
  475. */
  476. p = ((uchar*)pcmp)+sizeof(PCMP);
  477. e = ((uchar*)pcmp)+pcmp->length;
  478. while(p < e) switch(*p){
  479. default:
  480. print("mpinit: unknown PCMP type 0x%uX (e-p 0x%luX)\n",
  481. *p, e-p);
  482. while(p < e){
  483. print("%uX ", *p);
  484. p++;
  485. }
  486. break;
  487. case PcmpPROCESSOR:
  488. if(apic = mkprocessor((PCMPprocessor*)p)){
  489. /*
  490. * Must take a note of bootstrap processor APIC
  491. * now as it will be needed in order to start the
  492. * application processors later and there's no
  493. * guarantee that the bootstrap processor appears
  494. * first in the table before the others.
  495. */
  496. apic->addr = va;
  497. apic->paddr = pcmp->lapicbase;
  498. if(apic->flags & PcmpBP)
  499. bpapic = apic;
  500. cpuson++;
  501. }
  502. p += sizeof(PCMPprocessor);
  503. continue;
  504. case PcmpBUS:
  505. mkbus((PCMPbus*)p);
  506. p += sizeof(PCMPbus);
  507. continue;
  508. case PcmpIOAPIC:
  509. if(apic = mkioapic((PCMPioapic*)p))
  510. ioapicinit(apic, ((PCMPioapic*)p)->apicno);
  511. p += sizeof(PCMPioapic);
  512. continue;
  513. case PcmpIOINTR:
  514. mkiointr((PCMPintr*)p);
  515. p += sizeof(PCMPintr);
  516. continue;
  517. case PcmpLINTR:
  518. mklintr((PCMPintr*)p);
  519. p += sizeof(PCMPintr);
  520. continue;
  521. }
  522. dprint("mpinit: mp table describes %d cpus\n", cpuson);
  523. /* For now, always scan ACPI's MADT for processors that MP missed. */
  524. trympacpi();
  525. if (bpapic == nil)
  526. bpapic = bootapic;
  527. /*
  528. * No bootstrap processor, no need to go further.
  529. */
  530. if(bpapic == 0)
  531. return;
  532. bpapic->online = 1;
  533. lapicinit(bpapic);
  534. /*
  535. * These interrupts are local to the processor
  536. * and do not appear in the I/O APIC so it is OK
  537. * to set them now.
  538. */
  539. intrenable(IrqTIMER, lapicclock, 0, BUSUNKNOWN, "clock");
  540. intrenable(IrqERROR, lapicerror, 0, BUSUNKNOWN, "lapicerror");
  541. intrenable(IrqSPURIOUS, lapicspurious, 0, BUSUNKNOWN, "lapicspurious");
  542. lapiconline();
  543. checkmtrr();
  544. /*
  545. * Initialise the application processors.
  546. */
  547. if(cp = getconf("*ncpu")){
  548. ncpu = strtol(cp, 0, 0);
  549. if(ncpu < 1)
  550. ncpu = 1;
  551. else if(ncpu > MAXMACH)
  552. ncpu = MAXMACH;
  553. }
  554. else
  555. ncpu = MAXMACH;
  556. memmove((void*)APBOOTSTRAP, apbootstrap, sizeof(apbootstrap));
  557. for(apic = mpapic; apic <= &mpapic[MaxAPICNO]; apic++){
  558. if(ncpu <= 1)
  559. break;
  560. if((apic->flags & (PcmpBP|PcmpEN)) == PcmpEN
  561. && apic->type == PcmpPROCESSOR){
  562. mpstartap(apic);
  563. conf.nmach++;
  564. ncpu--;
  565. }
  566. }
  567. /*
  568. * we don't really know the number of processors till
  569. * here.
  570. *
  571. * set conf.copymode here if nmach > 1.
  572. * Should look for an ExtINT line and enable it.
  573. */
  574. if(X86FAMILY(m->cpuidax) == 3 || conf.nmach > 1)
  575. conf.copymode = 1;
  576. }
  577. static int
  578. mpintrcpu(void)
  579. {
  580. int i;
  581. /*
  582. * The bulk of this code was written ~1995, when there was
  583. * one architecture and one generation of hardware, the number
  584. * of CPUs was up to 4(8) and the choices for interrupt routing
  585. * were physical, or flat logical (optionally with lowest
  586. * priority interrupt). Logical mode hasn't scaled well with
  587. * the increasing number of packages/cores/threads, so the
  588. * fall-back is to physical mode, which works across all processor
  589. * generations, both AMD and Intel, using the APIC and xAPIC.
  590. *
  591. * Interrupt routing policy can be set here.
  592. * Currently, just assign each interrupt to a different CPU on
  593. * a round-robin basis. Some idea of the packages/cores/thread
  594. * topology would be useful here, e.g. to not assign interrupts
  595. * to more than one thread in a core, or to use a "noise" core.
  596. * But, as usual, Intel make that an onerous task.
  597. */
  598. /*
  599. * temporary workaround for many-core intel (non-amd) systems:
  600. * always use cpu 0. (TODO)
  601. */
  602. if(strncmp(m->cpuidid, "AuthenticAMD", 12) != 0 && conf.nmach > 8)
  603. return 0;
  604. lock(&mpphysidlock);
  605. for(;;){
  606. i = mpphysid++;
  607. if(mpphysid >= MaxAPICNO+1)
  608. mpphysid = 0;
  609. if(mpapic[i].online)
  610. break;
  611. }
  612. unlock(&mpphysidlock);
  613. return mpapic[i].apicno;
  614. }
  615. static int
  616. mpintrenablex(Vctl* v, int tbdf)
  617. {
  618. Bus *bus;
  619. Aintr *aintr;
  620. Apic *apic;
  621. Pcidev *pcidev;
  622. int bno, dno, hi, irq, lo, n, type, vno;
  623. char *typenm;
  624. /*
  625. * Find the bus.
  626. */
  627. type = BUSTYPE(tbdf);
  628. bno = BUSBNO(tbdf);
  629. dno = BUSDNO(tbdf);
  630. if(type == BusISA)
  631. bno = mpisabus;
  632. vno = -1;
  633. for(bus = mpbus; bus != nil; bus = bus->next){
  634. if(bus->type != type)
  635. continue;
  636. if(bus->busno == bno)
  637. break;
  638. }
  639. if(bus == nil){
  640. typenm = type < 0 || type >= nelem(buses)? "": buses[type];
  641. panic("mpintrenablex: can't find bus type %d (%s) for "
  642. "irq %d %s busno %d", type, typenm, v->irq, v->name, bno);
  643. return -1;
  644. }
  645. /*
  646. * For PCI devices the interrupt pin (INT[ABCD]) and device
  647. * number are encoded into the entry irq field, so create something
  648. * to match on. The interrupt pin used by the device has to be
  649. * obtained from the PCI config space.
  650. */
  651. if(bus->type == BusPCI){
  652. pcidev = pcimatchtbdf(tbdf);
  653. if(pcidev != nil && (n = pcicfgr8(pcidev, PciINTP)) != 0)
  654. irq = (dno<<2)|(n-1);
  655. else
  656. irq = -1;
  657. //print("pcidev %#uX: irq %#uX v->irq %#uX\n", tbdf, irq, v->irq);
  658. }
  659. else
  660. irq = v->irq;
  661. /*
  662. * Find a matching interrupt entry from the list of interrupts
  663. * attached to this bus.
  664. */
  665. for(aintr = bus->aintr; aintr; aintr = aintr->next){
  666. if(aintr->intr->irq != irq)
  667. continue;
  668. if (0) {
  669. PCMPintr* p = aintr->intr;
  670. print("mpintrenablex: bus %d intin %d irq %d\n",
  671. p->busno, p->intin, p->irq);
  672. }
  673. /*
  674. * Check if already enabled. Multifunction devices may share
  675. * INT[A-D]# so, if already enabled, check the polarity matches
  676. * and the trigger is level.
  677. *
  678. * Should check the devices differ only in the function number,
  679. * but that can wait for the planned enable/disable rewrite.
  680. * The RDT read here is safe for now as currently interrupts
  681. * are never disabled once enabled.
  682. */
  683. apic = aintr->apic;
  684. ioapicrdtr(apic, aintr->intr->intin, 0, &lo);
  685. if(!(lo & ApicIMASK)){
  686. vno = lo & 0xFF;
  687. //print("%s vector %d (!imask)\n", v->name, vno);
  688. n = mpintrinit(bus, aintr->intr, vno, v->irq);
  689. n |= ApicPHYSICAL; /* no-op */
  690. lo &= ~(ApicRemoteIRR|ApicDELIVS);
  691. if(n != lo || !(n & ApicLEVEL)){
  692. print("mpintrenable: multiple botch irq%d, tbdf %uX, lo %8.8uX, n %8.8uX\n",
  693. v->irq, tbdf, lo, n);
  694. return -1;
  695. }
  696. break;
  697. }
  698. /*
  699. * With the APIC a unique vector can be assigned to each
  700. * request to enable an interrupt. There are two reasons this
  701. * is a good idea:
  702. * 1) to prevent lost interrupts, no more than 2 interrupts
  703. * should be assigned per block of 16 vectors (there is an
  704. * in-service entry and a holding entry for each priority
  705. * level and there is one priority level per block of 16
  706. * interrupts).
  707. * 2) each input pin on the IOAPIC will receive a different
  708. * vector regardless of whether the devices on that pin use
  709. * the same IRQ as devices on another pin.
  710. */
  711. vno = VectorAPIC + (incref(&mpvnoref)-1)*8;
  712. //print("%s vector %d (imask)\n", v->name, vno);
  713. if(vno > MaxVectorAPIC){
  714. print("mpintrenable: vno %d, irq %d, tbdf %uX\n",
  715. vno, v->irq, tbdf);
  716. return -1;
  717. }
  718. hi = mpintrcpu()<<24;
  719. lo = mpintrinit(bus, aintr->intr, vno, v->irq);
  720. //print("lo 0x%uX: busno %d intr %d vno %d irq %d elcr 0x%uX\n",
  721. // lo, bus->busno, aintr->intr->irq, vno,
  722. // v->irq, i8259elcr);
  723. if(lo & ApicIMASK)
  724. return -1;
  725. lo |= ApicPHYSICAL; /* no-op */
  726. if((apic->flags & PcmpEN) && apic->type == PcmpIOAPIC)
  727. ioapicrdtw(apic, aintr->intr->intin, hi, lo);
  728. //else
  729. // print("lo not enabled 0x%uX %d\n",
  730. // apic->flags, apic->type);
  731. break;
  732. }
  733. if (aintr) {
  734. v->isr = lapicisr;
  735. v->eoi = lapiceoi;
  736. }
  737. return vno;
  738. }
  739. int
  740. mpintrenable(Vctl* v)
  741. {
  742. int irq, tbdf, vno;
  743. /*
  744. * If the bus is known, try it.
  745. * BUSUNKNOWN is given both by [E]ISA devices and by
  746. * interrupts local to the processor (local APIC, coprocessor
  747. * breakpoint and page-fault).
  748. */
  749. tbdf = v->tbdf;
  750. if(tbdf != BUSUNKNOWN && (vno = mpintrenablex(v, tbdf)) != -1)
  751. return vno;
  752. irq = v->irq;
  753. if(irq >= IrqLINT0 && irq <= MaxIrqLAPIC){
  754. if(irq != IrqSPURIOUS)
  755. v->isr = lapiceoi;
  756. return VectorPIC+irq;
  757. }
  758. if(irq < 0 || irq > MaxIrqPIC){
  759. print("mpintrenable: irq %d out of range\n", irq);
  760. return -1;
  761. }
  762. /*
  763. * Either didn't find it or have to try the default buses
  764. * (ISA and EISA). This hack is due to either over-zealousness
  765. * or laziness on the part of some manufacturers.
  766. *
  767. * The MP configuration table on some older systems
  768. * (e.g. ASUS PCI/E-P54NP4) has an entry for the EISA bus
  769. * but none for ISA. It also has the interrupt type and
  770. * polarity set to 'default for this bus' which wouldn't
  771. * be compatible with ISA.
  772. */
  773. if(mpeisabus != -1){
  774. vno = mpintrenablex(v, MKBUS(BusEISA, 0, 0, 0));
  775. if(vno != -1)
  776. return vno;
  777. }
  778. if(mpisabus != -1){
  779. vno = mpintrenablex(v, MKBUS(BusISA, 0, 0, 0));
  780. if(vno != -1)
  781. return vno;
  782. }
  783. print("mpintrenable: out of choices eisa %d isa %d tbdf %#ux irq %d\n",
  784. mpeisabus, mpisabus, v->tbdf, v->irq);
  785. return -1;
  786. }
  787. static Lock mpshutdownlock;
  788. void
  789. mpshutdown(void)
  790. {
  791. /*
  792. * To be done...
  793. */
  794. if(!canlock(&mpshutdownlock)){
  795. /*
  796. * If this processor received the CTRL-ALT-DEL from
  797. * the keyboard, acknowledge it. Send an INIT to self.
  798. */
  799. #ifdef FIXTHIS
  800. if(lapicisr(VectorKBD))
  801. lapiceoi(VectorKBD);
  802. #endif /* FIX THIS */
  803. arch->introff();
  804. idle();
  805. }
  806. if(active.rebooting)
  807. return;
  808. print("apshutdown: active = %#8.8ux\n", active.machs);
  809. delay(1000);
  810. splhi();
  811. arch->resetothers();
  812. pcireset();
  813. i8042reset();
  814. /*
  815. * Often the BIOS hangs during restart if a conventional 8042
  816. * warm-boot sequence is tried. The following is Intel specific and
  817. * seems to perform a cold-boot, but at least it comes back.
  818. * And sometimes there is no keyboard...
  819. *
  820. * The reset register (0xcf9) is usually in one of the bridge
  821. * chips. The actual location and sequence could be extracted from
  822. * ACPI but why bother, this is the end of the line anyway.
  823. */
  824. print("no kbd; trying bios warm boot...");
  825. *(ushort*)KADDR(0x472) = 0x1234; /* BIOS warm-boot flag */
  826. outb(0xCF9, 0x02);
  827. outb(0xCF9, 0x06);
  828. print("can't reset\n");
  829. for(;;)
  830. idle();
  831. }