ioapic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. print("Bustype: %d\n", bustype);
  276. print("todo[%d] b:%d d:%d f:%d\n", todoidx, busno, devno, fno);
  277. todo[todoidx].v = *v;
  278. todo[todoidx].lo = lo | TMlevel | IPlow | Im;
  279. todo[todoidx].valid = 1;
  280. print("Set todo[0x%x] to valid\n", todoidx);
  281. todoidx++;
  282. return -1;
  283. }
  284. }
  285. ioapic_nr = acpi_irq2ioapic(gsi_irq);
  286. if (ioapic_nr < 0) {
  287. print("Could not find an IOAPIC for global irq %d!\n", gsi_irq);
  288. return -1;
  289. }
  290. ioapicintrinit(busno, ioapic_nr, gsi_irq - xioapic[ioapic_nr].Ioapic.gsib,
  291. devno, fno, lo);
  292. return 0;
  293. }
  294. void
  295. ioapicinit(int id, int ibase, uintptr_t pa)
  296. {
  297. Apic *apic;
  298. /*
  299. * Mark the IOAPIC useable if it has a good ID
  300. * and the registers can be mapped.
  301. */
  302. if(id >= Napic) {
  303. print("NOT setting ioapic %d useable; id must be < %d\n", id, Napic);
  304. return;
  305. }
  306. apic = &xioapic[id];
  307. if(apic->useable || (apic->Ioapic.addr = vmap(pa, 1024)) == nil)
  308. return;
  309. apic->useable = 1;
  310. apic->Ioapic.paddr = pa;
  311. /*
  312. * Initialise the I/O APIC.
  313. * The MultiProcessor Specification says it is the
  314. * responsibility of the O/S to set the APIC ID.
  315. */
  316. lock(&apic->Ioapic.l);
  317. apic->Ioapic.nrdt = ((ioapicread(apic, Ioapicver)>>16) & 0xff) + 1;
  318. if (ibase == -1) {
  319. apic->Ioapic.gsib = gsib;
  320. gsib += apic->Ioapic.nrdt;
  321. } else {
  322. apic->Ioapic.gsib = ibase;
  323. }
  324. ioapicwrite(apic, Ioapicid, id<<24);
  325. unlock(&apic->Ioapic.l);
  326. }
  327. void
  328. ioapicdump(void)
  329. {
  330. int i, n;
  331. Rbus *rbus;
  332. Rdt *rdt;
  333. Apic *apic;
  334. uint32_t hi, lo;
  335. //if(!DBGFLG)
  336. return;
  337. for(i = 0; i < Napic; i++){
  338. apic = &xioapic[i];
  339. if(!apic->useable || apic->Ioapic.addr == 0)
  340. continue;
  341. print("ioapic %d addr %#p nrdt %d gsib %d\n",
  342. i, apic->Ioapic.addr, apic->Ioapic.nrdt, apic->Ioapic.gsib);
  343. for(n = 0; n < apic->Ioapic.nrdt; n++){
  344. lock(&apic->Ioapic.l);
  345. rtblget(apic, n, &hi, &lo);
  346. unlock(&apic->Ioapic.l);
  347. print(" rdt %2.2d %#8.8x %#8.8x\n", n, hi, lo);
  348. }
  349. }
  350. for(i = 0; i < Nbus; i++){
  351. if((rbus = rdtbus[i]) == nil)
  352. continue;
  353. print("iointr bus %d:\n", i);
  354. for(; rbus != nil; rbus = rbus->next){
  355. rdt = rbus->rdt;
  356. print(" apic %ld devno %#x (%d %d) fno %d intin %d lo %#x ref %d\n",
  357. rdt->apic-xioapic, rbus->devno, rbus->devno>>2,
  358. rbus->devno & 0x03, rbus->fno, rdt->intin, rdt->lo, rdt->ref);
  359. }
  360. }
  361. }
  362. void
  363. ioapiconline(void)
  364. {
  365. int i;
  366. Apic *apic;
  367. for(apic = xioapic; apic < &xioapic[Napic]; apic++){
  368. if(!apic->useable || apic->Ioapic.addr == nil)
  369. continue;
  370. for(i = 0; i < apic->Ioapic.nrdt; i++){
  371. lock(&apic->Ioapic.l);
  372. rtblput(apic, i, 0, Im);
  373. unlock(&apic->Ioapic.l);
  374. }
  375. }
  376. ioapicdump();
  377. }
  378. void
  379. irqenable(void)
  380. {
  381. int i;
  382. Apic *apic;
  383. uint32_t hi, lo;
  384. for(apic = xioapic; apic < &xioapic[Napic]; apic++){
  385. if(!apic->useable || apic->Ioapic.addr == nil)
  386. continue;
  387. for(i = 0; i < apic->Ioapic.nrdt; i++){
  388. lock(&apic->Ioapic.l);
  389. rtblget(apic, i, &hi, &lo);
  390. /* if something is set in the vector, enable the
  391. * rdtentry */
  392. if ((lo&0xff) != 0)
  393. rtblput(apic, i, hi, lo & ~Im);
  394. unlock(&apic->Ioapic.l);
  395. }
  396. }
  397. ioapicdump();
  398. }
  399. static int dfpolicy = 0;
  400. static void
  401. ioapicintrdd(uint32_t* hi, uint32_t* lo)
  402. {
  403. int i;
  404. static int df;
  405. static Lock dflock;
  406. /*
  407. * Set delivery mode (lo) and destination field (hi),
  408. * according to interrupt routing policy.
  409. */
  410. /*
  411. * The bulk of this code was written ~1995, when there was
  412. * one architecture and one generation of hardware, the number
  413. * of CPUs was up to 4(8) and the choices for interrupt routing
  414. * were physical, or flat logical (optionally with lowest
  415. * priority interrupt). Logical mode hasn't scaled well with
  416. * the increasing number of packages/cores/threads, so the
  417. * fall-back is to physical mode, which works across all processor
  418. * generations, both AMD and Intel, using the APIC and xAPIC.
  419. *
  420. * Interrupt routing policy can be set here.
  421. */
  422. switch(dfpolicy){
  423. default: /* noise core 0 */
  424. *hi = sys->machptr[0]->apicno<<24;
  425. break;
  426. case 1: /* round-robin */
  427. /*
  428. * Assign each interrupt to a different CPU on a round-robin
  429. * Some idea of the packages/cores/thread topology would be
  430. * useful here, e.g. to not assign interrupts to more than one
  431. * thread in a core. But, as usual, Intel make that an onerous
  432. * task.
  433. */
  434. lock(&dflock);
  435. for(;;){
  436. i = df++;
  437. if(df >= sys->nmach+1)
  438. df = 0;
  439. if(sys->machptr[i] == nil || !sys->machptr[i]->online)
  440. continue;
  441. i = sys->machptr[i]->apicno;
  442. if(xlapic[i].useable && xlapic[i].Ioapic.addr == 0)
  443. break;
  444. }
  445. unlock(&dflock);
  446. *hi = i<<24;
  447. break;
  448. }
  449. *lo |= Pm|MTf;
  450. }
  451. static int
  452. nextvec(void)
  453. {
  454. uint vecno;
  455. lock(&idtnolock);
  456. vecno = idtno;
  457. idtno = (idtno+8) % IdtMAX;
  458. if(idtno < IdtIOAPIC)
  459. idtno += IdtIOAPIC;
  460. unlock(&idtnolock);
  461. return vecno;
  462. }
  463. static int
  464. msimask(Vkey *v, int mask)
  465. {
  466. Pcidev *p;
  467. p = pcimatchtbdf(v->tbdf);
  468. if(p == nil)
  469. return -1;
  470. return pcimsimask(p, mask);
  471. }
  472. static int
  473. intrenablemsi(Vctl* v, Pcidev *p)
  474. {
  475. uint vno, lo, hi;
  476. uint64_t msivec;
  477. vno = nextvec();
  478. lo = IPlow | TMedge | vno;
  479. ioapicintrdd(&hi, &lo);
  480. if(lo & Lm)
  481. lo |= MTlp;
  482. msivec = (uint64_t)hi<<32 | lo;
  483. if(pcimsienable(p, msivec) == -1)
  484. return -1;
  485. v->isr = apicisr;
  486. v->eoi = apiceoi;
  487. v->vno = vno;
  488. v->type = "msi";
  489. v->mask = msimask;
  490. DBG("msiirq: %T: enabling %.16llx %s irq %d vno %d\n", p->tbdf, msivec, v->name, v->Vkey.irq, vno);
  491. return vno;
  492. }
  493. int
  494. disablemsi(Vctl* v, Pcidev *p)
  495. {
  496. if(p == nil)
  497. return -1;
  498. return pcimsimask(p, 1);
  499. }
  500. int
  501. ioapicintrdisable(int vecno)
  502. {
  503. Rdt *rdt;
  504. /*
  505. * FOV. Oh dear. This isn't very good.
  506. * Fortunately rdtvecno[vecno] is static
  507. * once assigned.
  508. * Must do better.
  509. *
  510. * What about any pending interrupts?
  511. */
  512. if(vecno < 0 || vecno > MaxVectorAPIC){
  513. panic("ioapicintrdisable: vecno %d out of range", vecno);
  514. return -1;
  515. }
  516. if((rdt = rdtvecno[vecno]) == nil){
  517. panic("ioapicintrdisable: vecno %d has no rdt", vecno);
  518. return -1;
  519. }
  520. lock(&rdt->apic->Ioapic.l);
  521. rdt->enabled--;
  522. if(rdt->enabled == 0)
  523. rtblput(rdt->apic, rdt->intin, 0, rdt->lo);
  524. unlock(&rdt->apic->Ioapic.l);
  525. return 0;
  526. }
  527. /* From Akaros, not sure we want this but for now ... */
  528. static int
  529. ioapic_exists(void)
  530. {
  531. /* not foolproof, if we called this before parsing */
  532. for (int i = 0; i < Napic; i++)
  533. if (xioapic[i].useable)
  534. return 1;
  535. return 0;
  536. }
  537. static Rdt *
  538. rbus_get_rdt(int busno, int devno, int fno)
  539. {
  540. Rbus *rbus;
  541. for (rbus = rdtbus[busno]; rbus != nil; rbus = rbus->next) {
  542. if (rbus->devno == devno && rbus->fno == fno) {
  543. return rbus->rdt;
  544. }
  545. }
  546. return 0;
  547. }
  548. /* Attempts to init a bus interrupt, initializes Vctl, and returns the IDT
  549. * vector to use (-1 on error). If routable, the IRQ will route to core 0. The
  550. * IRQ will be masked, if possible. Call Vctl->unmask() when you're ready.
  551. *
  552. * This will determine the type of bus the device is on (LAPIC, IOAPIC, PIC,
  553. * etc), and set the appropriate fields in isr_h. If applicable, it'll also
  554. * allocate an IDT vector, such as for an IOAPIC, and route the IOAPIC entries
  555. * appropriately.
  556. *
  557. * Callers init Vctl->dev_irq and ->tbdf. tbdf encodes the bus type and the
  558. * classic PCI bus:dev:func. dev_irq may be ignored based on the bus type (e.g.
  559. * PCI, esp MSI).
  560. *
  561. * In plan9, this was ioapicintrenable(), which also unmasked. We don't have a
  562. * deinit/disable method that would tear down the route yet. All the plan9 one
  563. * did was dec enabled and mask the entry. */
  564. int
  565. bus_irq_setup(Vctl *v)
  566. {
  567. //Rbus *rbus;
  568. Rdt *rdt;
  569. int bustype = BUSTYPE(v->Vkey.tbdf);
  570. int busno = -1, devno = -1, fno = 0, vno;
  571. Pcidev *p;
  572. if (!ioapic_exists()) {
  573. panic("%s: no ioapics?", __func__);
  574. switch (bustype) {
  575. //case BusLAPIC:
  576. //case BusIPI:
  577. //break;
  578. default:
  579. //irq_h->check_spurious = pic_check_spurious;
  580. //v->eoi = pic_send_eoi;
  581. //irq_h->mask = pic_mask_irq;
  582. //irq_h->unmask = pic_unmask_irq;
  583. //irq_h->route_irq = 0;
  584. //irq_h->type = "pic";
  585. /* PIC devices have vector = irq + 32 */
  586. return -1; //irq_h->dev_irq + IdtPIC;
  587. }
  588. }
  589. switch (bustype) {
  590. case BusLAPIC:
  591. /* nxm used to set the initial 'isr' method (i think equiv to our
  592. * check_spurious) to apiceoi for non-spurious lapic vectors. in
  593. * effect, i think they were sending the EOI early, and their eoi
  594. * method was 0. we're not doing that (unless we have to). */
  595. //v->check_spurious = lapic_check_spurious;
  596. v->eoi = nil; //apiceoi;
  597. v->isr = apiceoi;
  598. //v->mask = lapic_mask_irq;
  599. //v->unmask = lapic_unmask_irq;
  600. //v->route_irq = 0;
  601. v->type = "lapic";
  602. /* For the LAPIC, irq == vector */
  603. return v->Vkey.irq;
  604. case BusIPI:
  605. /* similar to LAPIC, but we don't actually have LVT entries */
  606. //v->check_spurious = lapic_check_spurious;
  607. v->eoi = apiceoi;
  608. //v->mask = 0;
  609. //v->unmask = 0;
  610. //v->route_irq = 0;
  611. v->type = "IPI";
  612. return v->Vkey.irq;
  613. case BusISA:
  614. if (mpisabusno == -1)
  615. panic("No ISA bus allocated");
  616. busno = mpisabusno;
  617. /* need to track the irq in devno in PCI interrupt assignment entry
  618. * format (see mp.c or MP spec D.3). */
  619. devno = v->Vkey.irq << 2;
  620. break;
  621. case BusPCI:
  622. p = pcimatchtbdf(v->Vkey.tbdf);
  623. if (!p) {
  624. print("No PCI dev for tbdf %p!", v->Vkey.tbdf);
  625. return -1;
  626. }
  627. if ((vno = intrenablemsi(v, p))!= -1)
  628. return vno;
  629. busno = BUSBNO(v->Vkey.tbdf);
  630. devno = pcicfgr8(p, PciINTP);
  631. /* this might not be a big deal - some PCI devices have no INTP. if
  632. * so, change our devno - 1 below. */
  633. if (devno == 0)
  634. panic("no INTP for tbdf %p", v->Vkey.tbdf);
  635. /* remember, devno is the device shifted with irq pin in bits 0-1.
  636. * we subtract 1, since the PCI intp maps 1 -> INTA, 2 -> INTB, etc,
  637. * and the MP spec uses 0 -> INTA, 1 -> INTB, etc. */
  638. devno = BUSDNO(v->Vkey.tbdf) << 2 | (devno - 1);
  639. fno = BUSFNO(v->Vkey.tbdf);
  640. break;
  641. default:
  642. panic("Unknown bus type, TBDF %p", v->Vkey.tbdf);
  643. }
  644. /* busno and devno are set, regardless of the bustype, enough to find rdt.
  645. * these may differ from the values in tbdf. */
  646. rdt = rbus_get_rdt(busno, devno, fno);
  647. if (!rdt) {
  648. /* second chance. if we didn't find the item the first time, then (if
  649. * it exists at all), it wasn't in the MP tables (or we had no tables).
  650. * So maybe we can figure it out via ACPI. */
  651. acpi_make_rdt(v, v->Vkey.irq, bustype, busno, devno, fno);
  652. rdt = rbus_get_rdt(busno, devno, fno);
  653. }
  654. if (!rdt) {
  655. print("Unable to build IOAPIC route for irq %d\n", v->Vkey.irq);
  656. return -1;
  657. }
  658. /*
  659. * what to do about devices that intrenable/intrdisable frequently?
  660. * 1) there is no ioapicdisable yet;
  661. * 2) it would be good to reuse freed vectors.
  662. * Oh bugger.
  663. * brho: plus the diff btw mask/unmask and enable/disable is unclear
  664. */
  665. /*
  666. * This is a low-frequency event so just lock
  667. * the whole IOAPIC to initialise the RDT entry
  668. * rather than putting a Lock in each entry.
  669. */
  670. lock(&rdt->apic->Ioapic.l);
  671. /* if a destination has already been picked, we store it in the lo. this
  672. * stays around regardless of enabled/disabled, since we don't reap vectors
  673. * yet. nor do we really mess with enabled... */
  674. if ((rdt->lo & 0xff) == 0) {
  675. vno = nextvec();
  676. rdt->lo |= vno;
  677. rdtvecno[vno] = rdt;
  678. } else {
  679. print("bus_irq_setup: %p: multiple irq bus %d dev %d\n", v->Vkey.tbdf, busno, devno);
  680. }
  681. rdt->enabled++;
  682. rdt->hi = 0; /* route to 0 by default */
  683. rdt->lo |= Pm | MTf;
  684. rtblput(rdt->apic, rdt->intin, rdt->hi, rdt->lo);
  685. vno = rdt->lo & 0xff;
  686. unlock(&rdt->apic->Ioapic.l);
  687. v->type = "ioapic";
  688. v->eoi = apiceoi;
  689. v->vno = vno;
  690. v->mask = msimask;
  691. return vno;
  692. }
  693. int
  694. acpiirq(uint32_t tbdf, int gsi)
  695. {
  696. Proc *up = externup();
  697. int ioapic_nr;
  698. int busno = BUSBNO(tbdf);
  699. int pin, devno;
  700. Pcidev *pcidev;
  701. Vctl *v;
  702. Vinfo *vinfotodo = nil;
  703. int acpiintrenable(Vctl *v);
  704. /* for now we know it's PCI, just ignore what they told us. */
  705. tbdf = MKBUS(BusPCI, busno, BUSDNO(tbdf), BUSFNO(tbdf));
  706. if((pcidev = pcimatchtbdf(tbdf)) == nil) {
  707. error("No such device (any more?)");
  708. }
  709. if((pin = pcicfgr8(pcidev, PciINTP)) == 0)
  710. error("no INTP for that device, which is impossible");
  711. // pcicfgw8(pcidev, PciINTL, gsi);
  712. for (int i = 0; i < todoidx; i++) {
  713. if (todo[i].v.Vkey.tbdf == tbdf) {
  714. vinfotodo = &todo[i];
  715. break;
  716. }
  717. }
  718. print("acpiirq: vinfotodo b:%d d:%d f:%d gsi:%d\n", BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf), gsi);
  719. if (vinfotodo == nil)
  720. error("Unknown tbdf");
  721. if (!vinfotodo->valid)
  722. error("Invalid tbdf");
  723. v = malloc(sizeof(*v));
  724. if (waserror()) {
  725. print("well, that went badly\n");
  726. free(v);
  727. nexterror();
  728. }
  729. *v = vinfotodo->v;
  730. v->Vkey.irq = gsi;
  731. devno = BUSDNO(v->Vkey.tbdf)<<2|(pin-1);
  732. //if (DBGFLG)
  733. print("acpiirq: tbdf %#8.8x busno %d devno %d\n",
  734. v->Vkey.tbdf, busno, devno);
  735. ioapic_nr = acpi_irq2ioapic(gsi);
  736. //if (DBGFLG)
  737. print("ioapic_nr for gsi %d is %d\n", gsi, ioapic_nr);
  738. if (ioapic_nr < 0) {
  739. error("Could not find an IOAPIC for global irq!\n");
  740. }
  741. //ioapicdump();
  742. ioapicintrinit(busno, ioapic_nr, gsi - xioapic[ioapic_nr].Ioapic.gsib,
  743. devno, BUSFNO(tbdf), vinfotodo->lo);
  744. //if (DBGFLG)
  745. print("ioapicinrinit seems to have worked\n");
  746. poperror();
  747. vinfotodo->valid = 0;
  748. //ioapicdump();
  749. acpiintrenable(v);
  750. //ioapicdump();
  751. return 0;
  752. }