ioapic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "apic.h"
  15. #include "io.h"
  16. #include "acpi.h"
  17. typedef struct Rbus Rbus;
  18. typedef struct Rdt Rdt;
  19. /* this cross-dependency from acpi to ioapic is from akaros, and
  20. * kind of breaks the clean model we had before, where table
  21. * parsing and hardware were completely separate. We'll try to
  22. * clean it up later.
  23. */
  24. extern Atable *apics; /* APIC info */
  25. int mpisabusno = -1;
  26. /* Rbus chains, one for each device bus: each rbus matches a device to an rdt */
  27. struct Rbus {
  28. Rbus *next;
  29. int devno;
  30. int fno;
  31. Rdt *rdt;
  32. };
  33. /* Each rdt describes an ioapic input pin (intin, from the bus/device) */
  34. struct Rdt {
  35. Apic *apic;
  36. int intin;
  37. uint32_t lo;
  38. uint32_t hi;
  39. int ref; /* could map to multiple busses */
  40. int enabled; /* times enabled */
  41. };
  42. enum { /* IOAPIC registers */
  43. Ioregsel = 0x00, /* indirect register address */
  44. Iowin = 0x04, /* indirect register data */
  45. Ioipa = 0x08, /* IRQ Pin Assertion */
  46. Ioeoi = 0x10, /* EOI */
  47. Ioapicid = 0x00, /* Identification */
  48. Ioapicver = 0x01, /* Version */
  49. Ioapicarb = 0x02, /* Arbitration */
  50. Ioabcfg = 0x03, /* Boot Coniguration */
  51. Ioredtbl = 0x10, /* Redirection Table */
  52. };
  53. static Rdt rdtarray[Nrdt];
  54. static int nrdtarray;
  55. static int gsib;
  56. static Rbus* rdtbus[Nbus];
  57. static Rdt* rdtvecno[IdtMAX+1];
  58. static Lock idtnolock;
  59. static int idtno = IdtIOAPIC;
  60. Apic xioapic[Napic];
  61. static int map_polarity[4] = {
  62. -1, IPhigh, -1, IPlow
  63. };
  64. static int map_edge_level[4] = {
  65. -1, TMedge, -1, TMlevel
  66. };
  67. /* TODO: use the slice library for this. */
  68. typedef struct {
  69. Vctl v;
  70. uint32_t lo;
  71. int valid;
  72. } Vinfo;
  73. static Vinfo todo[1<<13];
  74. static int todoidx = 0;
  75. /* this is a guess. */
  76. static char todostring[1024];
  77. char *readtodo(void)
  78. {
  79. int bus, dev, fn, i;
  80. char *p = todostring;
  81. char *e = p + sizeof(todostring);
  82. for(i = 0; i < todoidx; i++) {
  83. if (!todo[i].valid)
  84. continue;
  85. bus = BUSBNO(todo[i].v.Vkey.tbdf);
  86. dev = BUSDNO(todo[i].v.Vkey.tbdf);
  87. fn = BUSFNO(todo[i].v.Vkey.tbdf);
  88. p = seprint(p, e, "0x%x 0x%x 0x%x\n", bus, dev, fn);
  89. }
  90. return todostring;
  91. }
  92. static uint32_t ioapicread(Apic*apic, int reg)
  93. {
  94. volatile uint32_t *sel = apic->Ioapic.addr+Ioregsel;
  95. volatile uint32_t *data = apic->Ioapic.addr+Iowin;
  96. *sel = reg;
  97. return *data;
  98. }
  99. static void ioapicwrite(Apic*apic, int reg, uint32_t val)
  100. {
  101. volatile uint32_t *sel = apic->Ioapic.addr+Ioregsel;
  102. volatile uint32_t *data = apic->Ioapic.addr+Iowin;
  103. *sel = reg;
  104. *data = val;
  105. }
  106. static void
  107. rtblget(Apic* apic, int sel, uint32_t* hi, uint32_t* lo)
  108. {
  109. sel = Ioredtbl + 2*sel;
  110. *hi = ioapicread(apic, sel+1);
  111. *lo = ioapicread(apic, sel);
  112. }
  113. static void
  114. rtblput(Apic* apic, int sel, uint32_t hi, uint32_t lo)
  115. {
  116. sel = Ioredtbl + 2*sel;
  117. ioapicwrite(apic, sel+1, hi);
  118. ioapicwrite(apic, sel, lo);
  119. }
  120. static Rdt*
  121. rdtlookup(Apic *apic, int intin)
  122. {
  123. int i;
  124. Rdt *r;
  125. for(i = 0; i < nrdtarray; i++){
  126. r = rdtarray + i;
  127. if(apic == r->apic && intin == r->intin)
  128. return r;
  129. }
  130. return nil;
  131. }
  132. static int
  133. compatible(uint32_t new, uint32_t old)
  134. {
  135. uint32_t newtop = new & ~0xff;
  136. uint32_t oldtop = old & ~0xff;
  137. uint32_t newvno = new & 0xff;
  138. print("compatible: new 0x%x, old 0x%x\n", new, old);
  139. if (new == old)
  140. return 1;
  141. print("not the same\n");
  142. if ((newvno == 0) && (newtop == oldtop))
  143. return 1;
  144. print("REALLY not the same\n");
  145. return 0;
  146. }
  147. static void
  148. ioapicintrinit(int busno, int apicno, int intin, int devno, int fno, uint32_t lo)
  149. {
  150. Rbus *rbus;
  151. Rdt *rdt;
  152. Apic *apic;
  153. if(busno >= Nbus){
  154. print("ioapicintrinit: botch: Busno %d >= Nbus %d\n", busno, Nbus);
  155. return;
  156. }
  157. if (apicno >= Napic) {
  158. print("ioapicintrinit: botch: acpicno %d >= Napic %d\n", apicno, Napic);
  159. return;
  160. }
  161. if (nrdtarray >= Nrdt){
  162. print("ioapicintrinit: botch: nrdtarray %d >= Nrdt %d\n", nrdtarray, Nrdt);
  163. return;
  164. }
  165. apic = &xioapic[apicno];
  166. if(!apic->useable) {
  167. print("ioapicintrinit: botch: apic %d not marked usable\n", apicno);
  168. return;
  169. }
  170. if (intin >= apic->Ioapic.nrdt){
  171. print("ioapicintrinit: botch: initin %d >= apic->Ioapic.nrdt %d\n", intin, apic->Ioapic.nrdt);
  172. return;
  173. }
  174. rdt = rdtlookup(apic, intin);
  175. if (rdt == nil) {
  176. rdt = &rdtarray[nrdtarray++];
  177. rdt->apic = apic;
  178. rdt->intin = intin;
  179. rdt->lo = lo;
  180. } else {
  181. if (!compatible(lo, rdt->lo)) {
  182. print("ioapicintrinit: multiple irq botch bus %d %d/%d/%d lo %d vs %d\n",
  183. busno, apicno, intin, devno, lo, rdt->lo);
  184. return;
  185. }
  186. print("ioapicintrinit: dup rdt %d %d %d %d %.8x\n", busno, apicno, intin, devno, lo);
  187. }
  188. rdt->ref++;
  189. rbus = malloc(sizeof *rbus);
  190. rbus->rdt = rdt;
  191. rbus->devno = devno;
  192. rbus->fno = fno;
  193. rbus->next = rdtbus[busno];
  194. rdtbus[busno] = rbus;
  195. print("%s: success\n", __func__);
  196. }
  197. static int
  198. acpi_irq2ioapic(int irq)
  199. {
  200. int ioapic_idx = 0;
  201. Apic *apic;
  202. /* with acpi, the ioapics map a global interrupt space. each covers a
  203. * window of the space from [ibase, ibase + nrdt). */
  204. for (apic = xioapic; apic < &xioapic[Napic]; apic++, ioapic_idx++) {
  205. /* addr check is just for sanity */
  206. if (!apic->useable || !apic->Ioapic.addr)
  207. continue;
  208. if ((apic->Ioapic.gsib <= irq) && (irq < apic->Ioapic.gsib + apic->Ioapic.nrdt))
  209. return ioapic_idx;
  210. }
  211. return -1;
  212. }
  213. /* Build an RDT route, like we would have had from the MP tables had they been
  214. * parsed, via ACPI.
  215. *
  216. * This only really deals with the ISA IRQs and maybe PCI ones that happen to
  217. * have an override. FWIW, on qemu the PCI NIC shows up as an ACPI intovr.
  218. *
  219. * From Brendan http://f.osdev.org/viewtopic.php?f=1&t=25951:
  220. *
  221. * Before parsing the MADT you should begin by assuming that redirection
  222. * entries 0 to 15 are used for ISA IRQs 0 to 15. The MADT's "Interrupt
  223. * Source Override Structures" will tell you when this initial/default
  224. * assumption is wrong. For example, the MADT might tell you that ISA IRQ 9
  225. * is connected to IO APIC 44 and is level triggered; and (in this case)
  226. * it'd be silly to assume that ISA IRQ 9 is also connected to IO APIC
  227. * input 9 just because IO APIC input 9 is not listed.
  228. *
  229. * For PCI IRQs, the MADT tells you nothing and you can't assume anything
  230. * at all. Sadly, you have to interpret the ACPI AML to determine how PCI
  231. * IRQs are connected to IO APIC inputs (or find some other work-around;
  232. * like implementing a motherboard driver for each different motherboard,
  233. * or some complex auto-detection scheme, or just configure PCI devices to
  234. * use MSI instead). */
  235. static int
  236. acpi_make_rdt(Vctl *v, int irq, int bustype, int busno, int devno, int fno)
  237. {
  238. Atable *at;
  239. Apicst *st, *lst;
  240. uint32_t lo = 0;
  241. int pol, edge_level, ioapic_nr, gsi_irq;
  242. //print("acpi_make_rdt(0x%x %d %d 0x%x)\n", tbdf, irq, busno, devno);
  243. //die("acpi.make.rdt)\n");
  244. at = apics;
  245. st = nil;
  246. for (int i = 0; i < at->nchildren; i++) {
  247. lst = at->children[i]->tbl;
  248. if (lst->type == ASintovr) {
  249. if (lst->intovr.irq == irq) {
  250. st = lst;
  251. break;
  252. }
  253. }
  254. }
  255. if (st) {
  256. pol = map_polarity[st->intovr.flags & AFpmask];
  257. if (pol < 0) {
  258. print("ACPI override had bad polarity\n");
  259. return -1;
  260. }
  261. edge_level = map_edge_level[(st->intovr.flags & AFlevel) >> 2];
  262. if (edge_level < 0) {
  263. print("ACPI override had bad edge/level\n");
  264. return -1;
  265. }
  266. lo = pol | edge_level;
  267. gsi_irq = st->intovr.intr;
  268. } else {
  269. if (bustype == BusISA) {
  270. lo = IPhigh | TMedge;
  271. gsi_irq = irq;
  272. } else {
  273. /* Need to query ACPI at some point to handle this */
  274. print("Non-ISA IRQ %d not found in MADT, aborting\n", irq);
  275. todo[todoidx].v = *v;
  276. todo[todoidx].lo = lo | TMlevel | IPlow | Im;
  277. todo[todoidx].valid = 1;
  278. print("Set todo[0x%x] to valid\n", todoidx);
  279. todoidx++;
  280. return -1;
  281. }
  282. }
  283. ioapic_nr = acpi_irq2ioapic(gsi_irq);
  284. if (ioapic_nr < 0) {
  285. print("Could not find an IOAPIC for global irq %d!\n", gsi_irq);
  286. return -1;
  287. }
  288. ioapicintrinit(busno, ioapic_nr, gsi_irq - xioapic[ioapic_nr].Ioapic.gsib,
  289. devno, fno, lo);
  290. return 0;
  291. }
  292. void
  293. ioapicinit(int id, int ibase, uintptr_t pa)
  294. {
  295. Apic *apic;
  296. /*
  297. * Mark the IOAPIC useable if it has a good ID
  298. * and the registers can be mapped.
  299. */
  300. if(id >= Napic) {
  301. print("NOT setting ioapic %d useable; id must be < %d\n", id, Napic);
  302. return;
  303. }
  304. apic = &xioapic[id];
  305. if(apic->useable || (apic->Ioapic.addr = vmap(pa, 1024)) == nil)
  306. return;
  307. apic->useable = 1;
  308. apic->Ioapic.paddr = pa;
  309. /*
  310. * Initialise the I/O APIC.
  311. * The MultiProcessor Specification says it is the
  312. * responsibility of the O/S to set the APIC ID.
  313. */
  314. lock(&apic->Ioapic.l);
  315. apic->Ioapic.nrdt = ((ioapicread(apic, Ioapicver)>>16) & 0xff) + 1;
  316. if (ibase == -1) {
  317. apic->Ioapic.gsib = gsib;
  318. gsib += apic->Ioapic.nrdt;
  319. } else {
  320. apic->Ioapic.gsib = ibase;
  321. }
  322. ioapicwrite(apic, Ioapicid, id<<24);
  323. unlock(&apic->Ioapic.l);
  324. }
  325. void
  326. ioapicdump(void)
  327. {
  328. int i, n;
  329. Rbus *rbus;
  330. Rdt *rdt;
  331. Apic *apic;
  332. uint32_t hi, lo;
  333. if(!DBGFLG)
  334. return;
  335. for(i = 0; i < Napic; i++){
  336. apic = &xioapic[i];
  337. if(!apic->useable || apic->Ioapic.addr == 0)
  338. continue;
  339. print("ioapic %d addr %#p nrdt %d gsib %d\n",
  340. i, apic->Ioapic.addr, apic->Ioapic.nrdt, apic->Ioapic.gsib);
  341. for(n = 0; n < apic->Ioapic.nrdt; n++){
  342. lock(&apic->Ioapic.l);
  343. rtblget(apic, n, &hi, &lo);
  344. unlock(&apic->Ioapic.l);
  345. print(" rdt %2.2d %#8.8x %#8.8x\n", n, hi, lo);
  346. }
  347. }
  348. for(i = 0; i < Nbus; i++){
  349. if((rbus = rdtbus[i]) == nil)
  350. continue;
  351. print("iointr bus %d:\n", i);
  352. for(; rbus != nil; rbus = rbus->next){
  353. rdt = rbus->rdt;
  354. print(" apic %ld devno %#x (%d %d) fno %d intin %d lo %#x ref %d\n",
  355. rdt->apic-xioapic, rbus->devno, rbus->devno>>2,
  356. rbus->devno & 0x03, rbus->fno, rdt->intin, rdt->lo, rdt->ref);
  357. }
  358. }
  359. }
  360. void
  361. ioapiconline(void)
  362. {
  363. int i;
  364. Apic *apic;
  365. for(apic = xioapic; apic < &xioapic[Napic]; apic++){
  366. if(!apic->useable || apic->Ioapic.addr == nil)
  367. continue;
  368. for(i = 0; i < apic->Ioapic.nrdt; i++){
  369. lock(&apic->Ioapic.l);
  370. rtblput(apic, i, 0, Im);
  371. unlock(&apic->Ioapic.l);
  372. }
  373. }
  374. ioapicdump();
  375. }
  376. void
  377. irqenable(void)
  378. {
  379. int i;
  380. Apic *apic;
  381. uint32_t hi, lo;
  382. for(apic = xioapic; apic < &xioapic[Napic]; apic++){
  383. if(!apic->useable || apic->Ioapic.addr == nil)
  384. continue;
  385. for(i = 0; i < apic->Ioapic.nrdt; i++){
  386. lock(&apic->Ioapic.l);
  387. rtblget(apic, i, &hi, &lo);
  388. /* if something is set in the vector, enable the
  389. * rdtentry */
  390. if ((lo&0xff) != 0)
  391. rtblput(apic, i, hi, lo & ~Im);
  392. unlock(&apic->Ioapic.l);
  393. }
  394. }
  395. ioapicdump();
  396. }
  397. static int dfpolicy = 0;
  398. static void
  399. ioapicintrdd(uint32_t* hi, uint32_t* lo)
  400. {
  401. int i;
  402. static int df;
  403. static Lock dflock;
  404. /*
  405. * Set delivery mode (lo) and destination field (hi),
  406. * according to interrupt routing policy.
  407. */
  408. /*
  409. * The bulk of this code was written ~1995, when there was
  410. * one architecture and one generation of hardware, the number
  411. * of CPUs was up to 4(8) and the choices for interrupt routing
  412. * were physical, or flat logical (optionally with lowest
  413. * priority interrupt). Logical mode hasn't scaled well with
  414. * the increasing number of packages/cores/threads, so the
  415. * fall-back is to physical mode, which works across all processor
  416. * generations, both AMD and Intel, using the APIC and xAPIC.
  417. *
  418. * Interrupt routing policy can be set here.
  419. */
  420. switch(dfpolicy){
  421. default: /* noise core 0 */
  422. *hi = sys->machptr[0]->apicno<<24;
  423. break;
  424. case 1: /* round-robin */
  425. /*
  426. * Assign each interrupt to a different CPU on a round-robin
  427. * Some idea of the packages/cores/thread topology would be
  428. * useful here, e.g. to not assign interrupts to more than one
  429. * thread in a core. But, as usual, Intel make that an onerous
  430. * task.
  431. */
  432. lock(&dflock);
  433. for(;;){
  434. i = df++;
  435. if(df >= sys->nmach+1)
  436. df = 0;
  437. if(sys->machptr[i] == nil || !sys->machptr[i]->online)
  438. continue;
  439. i = sys->machptr[i]->apicno;
  440. if(xlapic[i].useable && xlapic[i].Ioapic.addr == 0)
  441. break;
  442. }
  443. unlock(&dflock);
  444. *hi = i<<24;
  445. break;
  446. }
  447. *lo |= Pm|MTf;
  448. }
  449. static int
  450. nextvec(void)
  451. {
  452. uint vecno;
  453. lock(&idtnolock);
  454. vecno = idtno;
  455. idtno = (idtno+8) % IdtMAX;
  456. if(idtno < IdtIOAPIC)
  457. idtno += IdtIOAPIC;
  458. unlock(&idtnolock);
  459. return vecno;
  460. }
  461. static int
  462. msimask(Vkey *v, int mask)
  463. {
  464. Pcidev *p;
  465. p = pcimatchtbdf(v->tbdf);
  466. if(p == nil)
  467. return -1;
  468. return pcimsimask(p, mask);
  469. }
  470. static int
  471. intrenablemsi(Vctl* v, Pcidev *p)
  472. {
  473. uint vno, lo, hi;
  474. uint64_t msivec;
  475. vno = nextvec();
  476. lo = IPlow | TMedge | vno;
  477. ioapicintrdd(&hi, &lo);
  478. if(lo & Lm)
  479. lo |= MTlp;
  480. msivec = (uint64_t)hi<<32 | lo;
  481. if(pcimsienable(p, msivec) == -1)
  482. return -1;
  483. v->isr = apicisr;
  484. v->eoi = apiceoi;
  485. v->vno = vno;
  486. v->type = "msi";
  487. v->mask = msimask;
  488. DBG("msiirq: %T: enabling %.16llx %s irq %d vno %d\n", p->tbdf, msivec, v->name, v->Vkey.irq, vno);
  489. return vno;
  490. }
  491. int
  492. disablemsi(Vctl* v, Pcidev *p)
  493. {
  494. if(p == nil)
  495. return -1;
  496. return pcimsimask(p, 1);
  497. }
  498. int
  499. ioapicintrdisable(int vecno)
  500. {
  501. Rdt *rdt;
  502. /*
  503. * FOV. Oh dear. This isn't very good.
  504. * Fortunately rdtvecno[vecno] is static
  505. * once assigned.
  506. * Must do better.
  507. *
  508. * What about any pending interrupts?
  509. */
  510. if(vecno < 0 || vecno > MaxVectorAPIC){
  511. panic("ioapicintrdisable: vecno %d out of range", vecno);
  512. return -1;
  513. }
  514. if((rdt = rdtvecno[vecno]) == nil){
  515. panic("ioapicintrdisable: vecno %d has no rdt", vecno);
  516. return -1;
  517. }
  518. lock(&rdt->apic->Ioapic.l);
  519. rdt->enabled--;
  520. if(rdt->enabled == 0)
  521. rtblput(rdt->apic, rdt->intin, 0, rdt->lo);
  522. unlock(&rdt->apic->Ioapic.l);
  523. return 0;
  524. }
  525. /* From Akaros, not sure we want this but for now ... */
  526. static int
  527. ioapic_exists(void)
  528. {
  529. /* not foolproof, if we called this before parsing */
  530. for (int i = 0; i < Napic; i++)
  531. if (xioapic[i].useable)
  532. return 1;
  533. return 0;
  534. }
  535. static Rdt *
  536. rbus_get_rdt(int busno, int devno, int fno)
  537. {
  538. Rbus *rbus;
  539. for (rbus = rdtbus[busno]; rbus != nil; rbus = rbus->next) {
  540. if (rbus->devno == devno && rbus->fno == fno) {
  541. return rbus->rdt;
  542. }
  543. }
  544. return 0;
  545. }
  546. /* Attempts to init a bus interrupt, initializes Vctl, and returns the IDT
  547. * vector to use (-1 on error). If routable, the IRQ will route to core 0. The
  548. * IRQ will be masked, if possible. Call Vctl->unmask() when you're ready.
  549. *
  550. * This will determine the type of bus the device is on (LAPIC, IOAPIC, PIC,
  551. * etc), and set the appropriate fields in isr_h. If applicable, it'll also
  552. * allocate an IDT vector, such as for an IOAPIC, and route the IOAPIC entries
  553. * appropriately.
  554. *
  555. * Callers init Vctl->dev_irq and ->tbdf. tbdf encodes the bus type and the
  556. * classic PCI bus:dev:func. dev_irq may be ignored based on the bus type (e.g.
  557. * PCI, esp MSI).
  558. *
  559. * In plan9, this was ioapicintrenable(), which also unmasked. We don't have a
  560. * deinit/disable method that would tear down the route yet. All the plan9 one
  561. * did was dec enabled and mask the entry. */
  562. int
  563. bus_irq_setup(Vctl *v)
  564. {
  565. //Rbus *rbus;
  566. Rdt *rdt;
  567. int bustype = BUSTYPE(v->Vkey.tbdf);
  568. int busno = -1, devno = -1, fno = 0, vno;
  569. Pcidev *p;
  570. if (!ioapic_exists()) {
  571. panic("%s: no ioapics?", __func__);
  572. switch (bustype) {
  573. //case BusLAPIC:
  574. //case BusIPI:
  575. //break;
  576. default:
  577. //irq_h->check_spurious = pic_check_spurious;
  578. //v->eoi = pic_send_eoi;
  579. //irq_h->mask = pic_mask_irq;
  580. //irq_h->unmask = pic_unmask_irq;
  581. //irq_h->route_irq = 0;
  582. //irq_h->type = "pic";
  583. /* PIC devices have vector = irq + 32 */
  584. return -1; //irq_h->dev_irq + IdtPIC;
  585. }
  586. }
  587. switch (bustype) {
  588. case BusLAPIC:
  589. /* nxm used to set the initial 'isr' method (i think equiv to our
  590. * check_spurious) to apiceoi for non-spurious lapic vectors. in
  591. * effect, i think they were sending the EOI early, and their eoi
  592. * method was 0. we're not doing that (unless we have to). */
  593. //v->check_spurious = lapic_check_spurious;
  594. v->eoi = nil; //apiceoi;
  595. v->isr = apiceoi;
  596. //v->mask = lapic_mask_irq;
  597. //v->unmask = lapic_unmask_irq;
  598. //v->route_irq = 0;
  599. v->type = "lapic";
  600. /* For the LAPIC, irq == vector */
  601. return v->Vkey.irq;
  602. case BusIPI:
  603. /* similar to LAPIC, but we don't actually have LVT entries */
  604. //v->check_spurious = lapic_check_spurious;
  605. v->eoi = apiceoi;
  606. //v->mask = 0;
  607. //v->unmask = 0;
  608. //v->route_irq = 0;
  609. v->type = "IPI";
  610. return v->Vkey.irq;
  611. case BusISA:
  612. if (mpisabusno == -1)
  613. panic("No ISA bus allocated");
  614. busno = mpisabusno;
  615. /* need to track the irq in devno in PCI interrupt assignment entry
  616. * format (see mp.c or MP spec D.3). */
  617. devno = v->Vkey.irq << 2;
  618. break;
  619. case BusPCI:
  620. p = pcimatchtbdf(v->Vkey.tbdf);
  621. if (!p) {
  622. print("No PCI dev for tbdf %p!", v->Vkey.tbdf);
  623. return -1;
  624. }
  625. if ((vno = intrenablemsi(v, p))!= -1)
  626. return vno;
  627. busno = BUSBNO(v->Vkey.tbdf);
  628. devno = pcicfgr8(p, PciINTP);
  629. /* this might not be a big deal - some PCI devices have no INTP. if
  630. * so, change our devno - 1 below. */
  631. if (devno == 0)
  632. panic("no INTP for tbdf %p", v->Vkey.tbdf);
  633. /* remember, devno is the device shifted with irq pin in bits 0-1.
  634. * we subtract 1, since the PCI intp maps 1 -> INTA, 2 -> INTB, etc,
  635. * and the MP spec uses 0 -> INTA, 1 -> INTB, etc. */
  636. devno = BUSDNO(v->Vkey.tbdf) << 2 | (devno - 1);
  637. fno = BUSFNO(v->Vkey.tbdf);
  638. break;
  639. default:
  640. panic("Unknown bus type, TBDF %p", v->Vkey.tbdf);
  641. }
  642. /* busno and devno are set, regardless of the bustype, enough to find rdt.
  643. * these may differ from the values in tbdf. */
  644. rdt = rbus_get_rdt(busno, devno, fno);
  645. if (!rdt) {
  646. /* second chance. if we didn't find the item the first time, then (if
  647. * it exists at all), it wasn't in the MP tables (or we had no tables).
  648. * So maybe we can figure it out via ACPI. */
  649. acpi_make_rdt(v, v->Vkey.irq, bustype, busno, devno, fno);
  650. rdt = rbus_get_rdt(busno, devno, fno);
  651. }
  652. if (!rdt) {
  653. print("Unable to build IOAPIC route for irq %d\n", v->Vkey.irq);
  654. return -1;
  655. }
  656. /*
  657. * what to do about devices that intrenable/intrdisable frequently?
  658. * 1) there is no ioapicdisable yet;
  659. * 2) it would be good to reuse freed vectors.
  660. * Oh bugger.
  661. * brho: plus the diff btw mask/unmask and enable/disable is unclear
  662. */
  663. /*
  664. * This is a low-frequency event so just lock
  665. * the whole IOAPIC to initialise the RDT entry
  666. * rather than putting a Lock in each entry.
  667. */
  668. lock(&rdt->apic->Ioapic.l);
  669. /* if a destination has already been picked, we store it in the lo. this
  670. * stays around regardless of enabled/disabled, since we don't reap vectors
  671. * yet. nor do we really mess with enabled... */
  672. if ((rdt->lo & 0xff) == 0) {
  673. vno = nextvec();
  674. rdt->lo |= vno;
  675. rdtvecno[vno] = rdt;
  676. } else {
  677. print("bus_irq_setup: %p: multiple irq bus %d dev %d\n", v->Vkey.tbdf, busno, devno);
  678. }
  679. rdt->enabled++;
  680. rdt->hi = 0; /* route to 0 by default */
  681. rdt->lo |= Pm | MTf;
  682. rtblput(rdt->apic, rdt->intin, rdt->hi, rdt->lo);
  683. vno = rdt->lo & 0xff;
  684. unlock(&rdt->apic->Ioapic.l);
  685. v->type = "ioapic";
  686. v->eoi = apiceoi;
  687. v->vno = vno;
  688. v->mask = msimask;
  689. return vno;
  690. }
  691. int
  692. acpiirq(uint32_t tbdf, int gsi)
  693. {
  694. Proc *up = externup();
  695. int ioapic_nr;
  696. int busno = BUSBNO(tbdf);
  697. int pin, devno;
  698. Pcidev *pcidev;
  699. Vctl *v;
  700. Vinfo *vinfotodo = nil;
  701. int acpiintrenable(Vctl *v);
  702. /* for now we know it's PCI, just ignore what they told us. */
  703. tbdf = MKBUS(BusPCI, busno, BUSDNO(tbdf), BUSFNO(tbdf));
  704. if((pcidev = pcimatchtbdf(tbdf)) == nil) {
  705. error("No such device (any more?)");
  706. }
  707. if((pin = pcicfgr8(pcidev, PciINTP)) == 0)
  708. error("no INTP for that device, which is impossible");
  709. // pcicfgw8(pcidev, PciINTL, gsi);
  710. for (int i = 0; i < todoidx; i++) {
  711. if (todo[i].v.Vkey.tbdf == tbdf) {
  712. vinfotodo = &todo[i];
  713. break;
  714. }
  715. }
  716. print("acpiirq: writing b:%d d:%d f:%d gsi:%d\n", BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf), gsi);
  717. if (vinfotodo == nil)
  718. error("Unknown tbdf");
  719. if (!vinfotodo->valid)
  720. error("Invalid tbdf");
  721. v = malloc(sizeof(*v));
  722. if (waserror()) {
  723. print("well, that went badly\n");
  724. free(v);
  725. nexterror();
  726. }
  727. *v = vinfotodo->v;
  728. v->Vkey.irq = gsi;
  729. devno = BUSDNO(v->Vkey.tbdf)<<2|(pin-1);
  730. if (DBGFLG)
  731. print("acpiirq: tbdf %#8.8x busno %d devno %d\n",
  732. v->Vkey.tbdf, busno, devno);
  733. ioapic_nr = acpi_irq2ioapic(gsi);
  734. if (DBGFLG)
  735. print("ioapic_nr for gsi %d is %d\n", gsi, ioapic_nr);
  736. if (ioapic_nr < 0) {
  737. error("Could not find an IOAPIC for global irq!\n");
  738. }
  739. //ioapicdump();
  740. ioapicintrinit(busno, ioapic_nr, gsi - xioapic[ioapic_nr].Ioapic.gsib,
  741. devno, BUSFNO(tbdf), vinfotodo->lo);
  742. if (DBGFLG)
  743. print("ioapicinrinit seems to have worked\n");
  744. poperror();
  745. vinfotodo->valid = 0;
  746. //ioapicdump();
  747. acpiintrenable(v);
  748. //ioapicdump();
  749. return 0;
  750. }