mp.c 18 KB

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