mp.c 17 KB

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