pci.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /*
  2. * PCI support code.
  3. */
  4. #include "u.h"
  5. #include "../port/lib.h"
  6. #include "mem.h"
  7. #include "dat.h"
  8. #include "fns.h"
  9. #include "io.h"
  10. #include "../port/error.h"
  11. #define DBG if(0) pcilog
  12. struct
  13. {
  14. char output[16384];
  15. int ptr;
  16. }PCICONS;
  17. int
  18. pcilog(char *fmt, ...)
  19. {
  20. int n;
  21. va_list arg;
  22. char buf[PRINTSIZE];
  23. va_start(arg, fmt);
  24. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  25. va_end(arg);
  26. memmove(PCICONS.output+PCICONS.ptr, buf, n);
  27. PCICONS.ptr += n;
  28. return n;
  29. }
  30. enum
  31. { /* configuration mechanism #1 */
  32. PciADDR = 0xCF8, /* CONFIG_ADDRESS */
  33. PciDATA = 0xCFC, /* CONFIG_DATA */
  34. /* configuration mechanism #2 */
  35. PciCSE = 0xCF8, /* configuration space enable */
  36. PciFORWARD = 0xCFA, /* which bus */
  37. MaxFNO = 7,
  38. MaxUBN = 255,
  39. };
  40. enum
  41. { /* command register */
  42. IOen = (1<<0),
  43. MEMen = (1<<1),
  44. MASen = (1<<2),
  45. MemWrInv = (1<<4),
  46. PErrEn = (1<<6),
  47. SErrEn = (1<<8),
  48. };
  49. static Lock pcicfglock;
  50. static Lock pcicfginitlock;
  51. static int pcicfgmode = -1;
  52. static int pcimaxbno = 7;
  53. static int pcimaxdno;
  54. static Pcidev* pciroot;
  55. static Pcidev* pcilist;
  56. static Pcidev* pcitail;
  57. static int nobios, nopcirouting;
  58. static int pcicfgrw32(int, int, int, int);
  59. static int pcicfgrw8(int, int, int, int);
  60. static char* bustypes[] = {
  61. "CBUSI",
  62. "CBUSII",
  63. "EISA",
  64. "FUTURE",
  65. "INTERN",
  66. "ISA",
  67. "MBI",
  68. "MBII",
  69. "MCA",
  70. "MPI",
  71. "MPSA",
  72. "NUBUS",
  73. "PCI",
  74. "PCMCIA",
  75. "TC",
  76. "VL",
  77. "VME",
  78. "XPRESS",
  79. };
  80. #pragma varargck type "T" int
  81. static int
  82. tbdffmt(Fmt* fmt)
  83. {
  84. char *p;
  85. int l, r, type, tbdf;
  86. if((p = malloc(READSTR)) == nil)
  87. return fmtstrcpy(fmt, "(tbdfconv)");
  88. switch(fmt->r){
  89. case 'T':
  90. tbdf = va_arg(fmt->args, int);
  91. type = BUSTYPE(tbdf);
  92. if(type < nelem(bustypes))
  93. l = snprint(p, READSTR, bustypes[type]);
  94. else
  95. l = snprint(p, READSTR, "%d", type);
  96. snprint(p+l, READSTR-l, ".%d.%d.%d",
  97. BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
  98. break;
  99. default:
  100. snprint(p, READSTR, "(tbdfconv)");
  101. break;
  102. }
  103. r = fmtstrcpy(fmt, p);
  104. free(p);
  105. return r;
  106. }
  107. ulong
  108. pcibarsize(Pcidev *p, int rno)
  109. {
  110. ulong v, size;
  111. v = pcicfgrw32(p->tbdf, rno, 0, 1);
  112. pcicfgrw32(p->tbdf, rno, 0xFFFFFFF0, 0);
  113. size = pcicfgrw32(p->tbdf, rno, 0, 1);
  114. if(v & 1)
  115. size |= 0xFFFF0000;
  116. pcicfgrw32(p->tbdf, rno, v, 0);
  117. return -(size & ~0x0F);
  118. }
  119. static int
  120. pcisizcmp(void *a, void *b)
  121. {
  122. Pcisiz *aa, *bb;
  123. aa = a;
  124. bb = b;
  125. return aa->siz - bb->siz;
  126. }
  127. static ulong
  128. pcimask(ulong v)
  129. {
  130. ulong m;
  131. m = BI2BY*sizeof(v);
  132. for(m = 1<<(m-1); m != 0; m >>= 1) {
  133. if(m & v)
  134. break;
  135. }
  136. m--;
  137. if((v & m) == 0)
  138. return v;
  139. v |= m;
  140. return v+1;
  141. }
  142. static void
  143. pcibusmap(Pcidev *root, ulong *pmema, ulong *pioa, int wrreg)
  144. {
  145. Pcidev *p;
  146. int ntb, i, size, rno, hole;
  147. ulong v, mema, ioa, sioa, smema, base, limit;
  148. Pcisiz *table, *tptr, *mtb, *itb;
  149. extern void qsort(void*, long, long, int (*)(void*, void*));
  150. if(!nobios)
  151. return;
  152. ioa = *pioa;
  153. mema = *pmema;
  154. DBG("pcibusmap wr=%d %T mem=%luX io=%luX\n",
  155. wrreg, root->tbdf, mema, ioa);
  156. ntb = 0;
  157. for(p = root; p != nil; p = p->link)
  158. ntb++;
  159. ntb *= (PciCIS-PciBAR0)/4;
  160. table = malloc(2*ntb*sizeof(Pcisiz));
  161. itb = table;
  162. mtb = table+ntb;
  163. /*
  164. * Build a table of sizes
  165. */
  166. for(p = root; p != nil; p = p->link) {
  167. if(p->ccrb == 0x06) {
  168. if(p->ccru != 0x04 || p->bridge == nil) {
  169. // DBG("pci: ignored bridge %T\n", p->tbdf);
  170. continue;
  171. }
  172. sioa = ioa;
  173. smema = mema;
  174. pcibusmap(p->bridge, &smema, &sioa, 0);
  175. hole = pcimask(smema-mema);
  176. if(hole < (1<<20))
  177. hole = 1<<20;
  178. p->mema.size = hole;
  179. hole = pcimask(sioa-ioa);
  180. if(hole < (1<<12))
  181. hole = 1<<12;
  182. p->ioa.size = hole;
  183. itb->dev = p;
  184. itb->bar = -1;
  185. itb->siz = p->ioa.size;
  186. itb++;
  187. mtb->dev = p;
  188. mtb->bar = -1;
  189. mtb->siz = p->mema.size;
  190. mtb++;
  191. continue;
  192. }
  193. for(i = 0; i <= 5; i++) {
  194. rno = PciBAR0 + i*4;
  195. v = pcicfgrw32(p->tbdf, rno, 0, 1);
  196. size = pcibarsize(p, rno);
  197. if(size == 0)
  198. continue;
  199. if(v & 1) {
  200. itb->dev = p;
  201. itb->bar = i;
  202. itb->siz = size;
  203. itb++;
  204. }
  205. else {
  206. mtb->dev = p;
  207. mtb->bar = i;
  208. mtb->siz = size;
  209. mtb++;
  210. }
  211. p->mem[i].size = size;
  212. }
  213. }
  214. /*
  215. * Sort both tables IO smallest first, Memory largest
  216. */
  217. qsort(table, itb-table, sizeof(Pcisiz), pcisizcmp);
  218. tptr = table+ntb;
  219. qsort(tptr, mtb-tptr, sizeof(Pcisiz), pcisizcmp);
  220. /*
  221. * Allocate IO address space on this bus
  222. */
  223. for(tptr = table; tptr < itb; tptr++) {
  224. hole = tptr->siz;
  225. if(tptr->bar == -1)
  226. hole = 1<<12;
  227. ioa = (ioa+hole-1) & ~(hole-1);
  228. p = tptr->dev;
  229. if(tptr->bar == -1)
  230. p->ioa.bar = ioa;
  231. else {
  232. p->pcr |= IOen;
  233. p->mem[tptr->bar].bar = ioa|1;
  234. if(wrreg)
  235. pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), ioa|1, 0);
  236. }
  237. ioa += tptr->siz;
  238. }
  239. /*
  240. * Allocate Memory address space on this bus
  241. */
  242. for(tptr = table+ntb; tptr < mtb; tptr++) {
  243. hole = tptr->siz;
  244. if(tptr->bar == -1)
  245. hole = 1<<20;
  246. mema = (mema+hole-1) & ~(hole-1);
  247. p = tptr->dev;
  248. if(tptr->bar == -1)
  249. p->mema.bar = mema;
  250. else {
  251. p->pcr |= MEMen;
  252. p->mem[tptr->bar].bar = mema;
  253. if(wrreg)
  254. pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), mema, 0);
  255. }
  256. mema += tptr->siz;
  257. }
  258. *pmema = mema;
  259. *pioa = ioa;
  260. free(table);
  261. if(wrreg == 0)
  262. return;
  263. /*
  264. * Finally set all the bridge addresses & registers
  265. */
  266. for(p = root; p != nil; p = p->link) {
  267. if(p->bridge == nil) {
  268. pcicfgrw8(p->tbdf, PciLTR, 64, 0);
  269. p->pcr |= MASen;
  270. pcicfgrw32(p->tbdf, PciPCR, p->pcr, 0);
  271. continue;
  272. }
  273. base = p->ioa.bar;
  274. limit = base+p->ioa.size-1;
  275. v = pcicfgrw32(p->tbdf, PciBAR3, 0, 1);
  276. v = (v&0xFFFF0000)|(limit & 0xF000)|((base & 0xF000)>>8);
  277. pcicfgrw32(p->tbdf, PciBAR3, v, 0);
  278. v = (limit & 0xFFFF0000)|(base>>16);
  279. pcicfgrw32(p->tbdf, 0x30, v, 0);
  280. base = p->mema.bar;
  281. limit = base+p->mema.size-1;
  282. v = (limit & 0xFFF00000)|((base & 0xFFF00000)>>16);
  283. pcicfgrw32(p->tbdf, PciBAR4, v, 0);
  284. /*
  285. * Disable memory prefetch
  286. */
  287. pcicfgrw32(p->tbdf, PciBAR5, 0x0000FFFF, 0);
  288. pcicfgrw8(p->tbdf, PciLTR, 64, 0);
  289. /*
  290. * Enable the bridge
  291. */
  292. v = 0xFFFF0000 | IOen | MEMen | MASen;
  293. pcicfgrw32(p->tbdf, PciPCR, v, 0);
  294. sioa = p->ioa.bar;
  295. smema = p->mema.bar;
  296. pcibusmap(p->bridge, &smema, &sioa, 1);
  297. }
  298. }
  299. static int
  300. pcilscan(int bno, Pcidev** list)
  301. {
  302. Pcidev *p, *head, *tail;
  303. int dno, fno, i, hdt, l, maxfno, maxubn, rno, sbn, tbdf, ubn;
  304. maxubn = bno;
  305. head = nil;
  306. tail = nil;
  307. for(dno = 0; dno <= pcimaxdno; dno++){
  308. maxfno = 0;
  309. for(fno = 0; fno <= maxfno; fno++){
  310. /*
  311. * For this possible device, form the
  312. * bus+device+function triplet needed to address it
  313. * and try to read the vendor and device ID.
  314. * If successful, allocate a device struct and
  315. * start to fill it in with some useful information
  316. * from the device's configuration space.
  317. */
  318. tbdf = MKBUS(BusPCI, bno, dno, fno);
  319. l = pcicfgrw32(tbdf, PciVID, 0, 1);
  320. if(l == 0xFFFFFFFF || l == 0)
  321. continue;
  322. p = malloc(sizeof(*p));
  323. p->tbdf = tbdf;
  324. p->vid = l;
  325. p->did = l>>16;
  326. if(pcilist != nil)
  327. pcitail->list = p;
  328. else
  329. pcilist = p;
  330. pcitail = p;
  331. p->rid = pcicfgr8(p, PciRID);
  332. p->ccrp = pcicfgr8(p, PciCCRp);
  333. p->ccru = pcicfgr8(p, PciCCRu);
  334. p->ccrb = pcicfgr8(p, PciCCRb);
  335. p->pcr = pcicfgr32(p, PciPCR);
  336. p->intl = pcicfgr8(p, PciINTL);
  337. /*
  338. * If the device is a multi-function device adjust the
  339. * loop count so all possible functions are checked.
  340. */
  341. hdt = pcicfgr8(p, PciHDT);
  342. if(hdt & 0x80)
  343. maxfno = MaxFNO;
  344. /*
  345. * If appropriate, read the base address registers
  346. * and work out the sizes.
  347. */
  348. switch(p->ccrb) {
  349. case 0x01: /* mass storage controller */
  350. case 0x02: /* network controller */
  351. case 0x03: /* display controller */
  352. case 0x04: /* multimedia device */
  353. case 0x07: /* simple comm. controllers */
  354. case 0x08: /* base system peripherals */
  355. case 0x09: /* input devices */
  356. case 0x0A: /* docking stations */
  357. case 0x0B: /* processors */
  358. case 0x0C: /* serial bus controllers */
  359. if((hdt & 0x7F) != 0)
  360. break;
  361. rno = PciBAR0 - 4;
  362. for(i = 0; i < nelem(p->mem); i++) {
  363. rno += 4;
  364. p->mem[i].bar = pcicfgr32(p, rno);
  365. p->mem[i].size = pcibarsize(p, rno);
  366. }
  367. break;
  368. case 0x00:
  369. case 0x05: /* memory controller */
  370. case 0x06: /* bridge device */
  371. default:
  372. break;
  373. }
  374. if(head != nil)
  375. tail->link = p;
  376. else
  377. head = p;
  378. tail = p;
  379. }
  380. }
  381. *list = head;
  382. for(p = head; p != nil; p = p->link){
  383. /*
  384. * Find PCI-PCI bridges and recursively descend the tree.
  385. */
  386. if(p->ccrb != 0x06 || p->ccru != 0x04)
  387. continue;
  388. /*
  389. * If the secondary or subordinate bus number is not
  390. * initialised try to do what the PCI BIOS should have
  391. * done and fill in the numbers as the tree is descended.
  392. * On the way down the subordinate bus number is set to
  393. * the maximum as it's not known how many buses are behind
  394. * this one; the final value is set on the way back up.
  395. */
  396. sbn = pcicfgr8(p, PciSBN);
  397. ubn = pcicfgr8(p, PciUBN);
  398. if(sbn == 0 || ubn == 0 || nobios) {
  399. sbn = maxubn+1;
  400. /*
  401. * Make sure memory, I/O and master enables are
  402. * off, set the primary, secondary and subordinate
  403. * bus numbers and clear the secondary status before
  404. * attempting to scan the secondary bus.
  405. *
  406. * Initialisation of the bridge should be done here.
  407. */
  408. pcicfgw32(p, PciPCR, 0xFFFF0000);
  409. l = (MaxUBN<<16)|(sbn<<8)|bno;
  410. pcicfgw32(p, PciPBN, l);
  411. pcicfgw16(p, PciSPSR, 0xFFFF);
  412. maxubn = pcilscan(sbn, &p->bridge);
  413. l = (maxubn<<16)|(sbn<<8)|bno;
  414. pcicfgw32(p, PciPBN, l);
  415. }
  416. else {
  417. maxubn = ubn;
  418. pcilscan(sbn, &p->bridge);
  419. }
  420. }
  421. return maxubn;
  422. }
  423. int
  424. pciscan(int bno, Pcidev **list)
  425. {
  426. int ubn;
  427. lock(&pcicfginitlock);
  428. ubn = pcilscan(bno, list);
  429. unlock(&pcicfginitlock);
  430. return ubn;
  431. }
  432. static uchar
  433. pIIx_get(Pcidev *router, uchar link)
  434. {
  435. uchar pirq;
  436. /* link should be 0x60, 0x61, 0x62, 0x63 */
  437. pirq = pcicfgr8(router, link);
  438. return (pirq < 16)? pirq: 0;
  439. }
  440. static void
  441. pIIx_set(Pcidev *router, uchar link, uchar irq)
  442. {
  443. pcicfgw8(router, link, irq);
  444. }
  445. static uchar
  446. via_get(Pcidev *router, uchar link)
  447. {
  448. uchar pirq;
  449. /* link should be 1, 2, 3, 5 */
  450. pirq = (link < 6)? pcicfgr8(router, 0x55 + (link>>1)): 0;
  451. return (link & 1)? (pirq >> 4): (pirq & 15);
  452. }
  453. static void
  454. via_set(Pcidev *router, uchar link, uchar irq)
  455. {
  456. uchar pirq;
  457. pirq = pcicfgr8(router, 0x55 + (link >> 1));
  458. pirq &= (link & 1)? 0x0f: 0xf0;
  459. pirq |= (link & 1)? (irq << 4): (irq & 15);
  460. pcicfgw8(router, 0x55 + (link>>1), pirq);
  461. }
  462. static uchar
  463. opti_get(Pcidev *router, uchar link)
  464. {
  465. uchar pirq = 0;
  466. /* link should be 0x02, 0x12, 0x22, 0x32 */
  467. if ((link & 0xcf) == 0x02)
  468. pirq = pcicfgr8(router, 0xb8 + (link >> 5));
  469. return (link & 0x10)? (pirq >> 4): (pirq & 15);
  470. }
  471. static void
  472. opti_set(Pcidev *router, uchar link, uchar irq)
  473. {
  474. uchar pirq;
  475. pirq = pcicfgr8(router, 0xb8 + (link >> 5));
  476. pirq &= (link & 0x10)? 0x0f : 0xf0;
  477. pirq |= (link & 0x10)? (irq << 4): (irq & 15);
  478. pcicfgw8(router, 0xb8 + (link >> 5), pirq);
  479. }
  480. static uchar
  481. ali_get(Pcidev *router, uchar link)
  482. {
  483. /* No, you're not dreaming */
  484. static const uchar map[] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
  485. uchar pirq;
  486. /* link should be 0x01..0x08 */
  487. pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
  488. return (link & 1)? map[pirq&15]: map[pirq>>4];
  489. }
  490. static void
  491. ali_set(Pcidev *router, uchar link, uchar irq)
  492. {
  493. /* Inverse of map in ali_get */
  494. static const uchar map[] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
  495. uchar pirq;
  496. pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
  497. pirq &= (link & 1)? 0x0f: 0xf0;
  498. pirq |= (link & 1)? (map[irq] << 4): (map[irq] & 15);
  499. pcicfgw8(router, 0x48 + ((link-1)>>1), pirq);
  500. }
  501. static uchar
  502. cyrix_get(Pcidev *router, uchar link)
  503. {
  504. uchar pirq;
  505. /* link should be 1, 2, 3, 4 */
  506. pirq = pcicfgr8(router, 0x5c + ((link-1)>>1));
  507. return ((link & 1)? pirq >> 4: pirq & 15);
  508. }
  509. static void
  510. cyrix_set(Pcidev *router, uchar link, uchar irq)
  511. {
  512. uchar pirq;
  513. pirq = pcicfgr8(router, 0x5c + (link>>1));
  514. pirq &= (link & 1)? 0x0f: 0xf0;
  515. pirq |= (link & 1)? (irq << 4): (irq & 15);
  516. pcicfgw8(router, 0x5c + (link>>1), pirq);
  517. }
  518. typedef struct
  519. {
  520. ushort vid;
  521. ushort did;
  522. uchar (*get)(Pcidev *, uchar);
  523. void (*set)(Pcidev *, uchar, uchar);
  524. } bridge_t;
  525. static bridge_t southbridges[] = {
  526. { 0x8086, 0x122e, pIIx_get, pIIx_set }, // Intel 82371FB
  527. { 0x8086, 0x1234, pIIx_get, pIIx_set }, // Intel 82371MX
  528. { 0x8086, 0x7000, pIIx_get, pIIx_set }, // Intel 82371SB
  529. { 0x8086, 0x7110, pIIx_get, pIIx_set }, // Intel 82371AB
  530. { 0x8086, 0x7198, pIIx_get, pIIx_set }, // Intel 82443MX (fn 1)
  531. { 0x8086, 0x2410, pIIx_get, pIIx_set }, // Intel 82801AA
  532. { 0x8086, 0x2420, pIIx_get, pIIx_set }, // Intel 82801AB
  533. { 0x8086, 0x2440, pIIx_get, pIIx_set }, // Intel 82801BA
  534. { 0x8086, 0x244c, pIIx_get, pIIx_set }, // Intel 82801BAM
  535. { 0x1106, 0x0586, via_get, via_set }, // Viatech 82C586
  536. { 0x1106, 0x0596, via_get, via_set }, // Viatech 82C596
  537. { 0x1106, 0x0686, via_get, via_set }, // Viatech 82C686
  538. { 0x1045, 0xc700, opti_get, opti_set }, // Opti 82C700
  539. { 0x10b9, 0x1533, ali_get, ali_set }, // Al M1533
  540. { 0x1039, 0x0008, pIIx_get, pIIx_set }, // SI 503
  541. { 0x1039, 0x0496, pIIx_get, pIIx_set }, // SI 496
  542. { 0x1078, 0x0100, cyrix_get, cyrix_set } // Cyrix 5530 Legacy
  543. };
  544. typedef struct {
  545. uchar e_bus; // Pci bus number
  546. uchar e_dev; // Pci device number
  547. uchar e_maps[12]; // Avoid structs! Link and mask.
  548. uchar e_slot; // Add-in/built-in slot
  549. uchar e_reserved;
  550. } slot_t;
  551. typedef struct {
  552. uchar rt_signature[4]; // Routing table signature
  553. uchar rt_version[2]; // Version number
  554. uchar rt_size[2]; // Total table size
  555. uchar rt_bus; // Interrupt router bus number
  556. uchar rt_devfn; // Router's devfunc
  557. uchar rt_pciirqs[2]; // Exclusive PCI irqs
  558. uchar rt_compat[4]; // Compatible PCI interrupt router
  559. uchar rt_miniport[4]; // Miniport data
  560. uchar rt_reserved[11];
  561. uchar rt_checksum;
  562. } router_t;
  563. static ushort pciirqs; // Exclusive PCI irqs
  564. static bridge_t *southbridge; // Which southbridge to use.
  565. static void
  566. pcirouting(void)
  567. {
  568. slot_t *e;
  569. router_t *r;
  570. int size, i, fn, tbdf;
  571. Pcidev *sbpci, *pci;
  572. uchar *p, *m, pin, irq, link;
  573. // Search for PCI interrupt routing table in BIOS
  574. for(p = (uchar *)KADDR(0xf0000); p < (uchar *)KADDR(0xfffff); p += 16)
  575. if(p[0] == '$' && p[1] == 'P' && p[2] == 'I' && p[3] == 'R')
  576. break;
  577. if(p >= (uchar *)KADDR(0xfffff))
  578. return;
  579. r = (router_t *)p;
  580. // print("PCI interrupt routing table version %d.%d at %.6uX\n",
  581. // r->rt_version[0], r->rt_version[1], (ulong)r & 0xfffff);
  582. tbdf = (BusPCI << 24)|(r->rt_bus << 16)|(r->rt_devfn << 8);
  583. sbpci = pcimatchtbdf(tbdf);
  584. if(sbpci == nil) {
  585. print("pcirouting: Cannot find south bridge %T\n", tbdf);
  586. return;
  587. }
  588. for(i = 0; i != nelem(southbridges); i++)
  589. if(sbpci->vid == southbridges[i].vid && sbpci->did == southbridges[i].did)
  590. break;
  591. if(i == nelem(southbridges)) {
  592. print("pcirouting: ignoring south bridge %T %.4uX/%.4uX\n", tbdf, sbpci->vid, sbpci->did);
  593. return;
  594. }
  595. southbridge = &southbridges[i];
  596. pciirqs = (r->rt_pciirqs[1] << 8)|r->rt_pciirqs[0];
  597. size = (r->rt_size[1] << 8)|r->rt_size[0];
  598. for(e = (slot_t *)&r[1]; (uchar *)e < p + size; e++) {
  599. // print("%.2uX/%.2uX %.2uX: ", e->e_bus, e->e_dev, e->e_slot);
  600. // for (i = 0; i != 4; i++) {
  601. // uchar *m = &e->e_maps[i * 3];
  602. // print("[%d] %.2uX %.4uX ",
  603. // i, m[0], (m[2] << 8)|m[1]);
  604. // }
  605. // print("\n");
  606. for(fn = 0; fn != 8; fn++) {
  607. tbdf = (BusPCI << 24)|(e->e_bus << 16)|((e->e_dev | fn) << 8);
  608. pci = pcimatchtbdf(tbdf);
  609. if(pci == nil)
  610. continue;
  611. pin = pcicfgr8(pci, PciINTP);
  612. if(pin == 0 || pin == 0xff)
  613. continue;
  614. m = &e->e_maps[(pin - 1) * 3];
  615. link = m[0];
  616. irq = southbridge->get(sbpci, link);
  617. if(irq == 0 || irq == pci->intl)
  618. continue;
  619. if(pci->intl != 0 && pci->intl != 0xFF) {
  620. print("pcirouting: BIOS workaround: %T at pin %d link %d irq %d -> %d\n",
  621. tbdf, pin, link, irq, pci->intl);
  622. southbridge->set(sbpci, link, pci->intl);
  623. continue;
  624. }
  625. print("pcirouting: %T at pin %d link %d irq %d\n", tbdf, pin, link, irq);
  626. pcicfgw8(pci, PciINTL, irq);
  627. pci->intl = irq;
  628. }
  629. }
  630. }
  631. static void
  632. pcicfginit(void)
  633. {
  634. char *p;
  635. Pcidev **list;
  636. ulong mema, ioa;
  637. int bno, n, pcibios;
  638. lock(&pcicfginitlock);
  639. if(pcicfgmode != -1)
  640. goto out;
  641. pcibios = 0;
  642. if(getconf("*nobios"))
  643. nobios = 1;
  644. else if(getconf("*pcibios"))
  645. pcibios = 1;
  646. if(getconf("*nopcirouting"))
  647. nopcirouting = 1;
  648. /*
  649. * Try to determine which PCI configuration mode is implemented.
  650. * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
  651. * a DWORD at 0xCF8 and another at 0xCFC and will pass through
  652. * any non-DWORD accesses as normal I/O cycles. There shouldn't be
  653. * a device behind these addresses so if Mode1 accesses fail try
  654. * for Mode2 (Mode2 is deprecated).
  655. */
  656. if(!pcibios){
  657. /*
  658. * Bits [30:24] of PciADDR must be 0,
  659. * according to the spec.
  660. */
  661. n = inl(PciADDR);
  662. if(!(n & 0x7FF00000)){
  663. outl(PciADDR, 0x80000000);
  664. outb(PciADDR+3, 0);
  665. if(inl(PciADDR) & 0x80000000){
  666. pcicfgmode = 1;
  667. pcimaxdno = 31;
  668. }
  669. }
  670. outl(PciADDR, n);
  671. if(pcicfgmode < 0){
  672. /*
  673. * The 'key' part of PciCSE should be 0.
  674. */
  675. n = inb(PciCSE);
  676. if(!(n & 0xF0)){
  677. outb(PciCSE, 0x0E);
  678. if(inb(PciCSE) == 0x0E){
  679. pcicfgmode = 2;
  680. pcimaxdno = 15;
  681. }
  682. }
  683. outb(PciCSE, n);
  684. }
  685. }
  686. if(pcicfgmode < 0)
  687. goto out;
  688. fmtinstall('T', tbdffmt);
  689. if(p = getconf("*pcimaxbno")){
  690. n = strtoul(p, 0, 0);
  691. if(n < pcimaxbno)
  692. pcimaxbno = n;
  693. }
  694. if(p = getconf("*pcimaxdno")){
  695. n = strtoul(p, 0, 0);
  696. if(n < pcimaxdno)
  697. pcimaxdno = n;
  698. }
  699. list = &pciroot;
  700. for(bno = 0; bno <= pcimaxbno; bno++) {
  701. int sbno = bno;
  702. bno = pcilscan(bno, list);
  703. while(*list)
  704. list = &(*list)->link;
  705. if (sbno == 0) {
  706. Pcidev *pci;
  707. /*
  708. * If we have found a PCI-to-Cardbus bridge, make sure
  709. * it has no valid mappings anymore.
  710. */
  711. pci = pciroot;
  712. while (pci) {
  713. if (pci->ccrb == 6 && pci->ccru == 7) {
  714. ushort bcr;
  715. /* reset the cardbus */
  716. bcr = pcicfgr16(pci, PciBCR);
  717. pcicfgw16(pci, PciBCR, 0x40 | bcr);
  718. delay(50);
  719. }
  720. pci = pci->link;
  721. }
  722. }
  723. }
  724. if(pciroot == nil)
  725. goto out;
  726. if(nobios) {
  727. /*
  728. * Work out how big the top bus is
  729. */
  730. mema = 0;
  731. ioa = 0;
  732. pcibusmap(pciroot, &mema, &ioa, 0);
  733. DBG("Sizes: mem=%8.8lux size=%8.8lux io=%8.8lux\n",
  734. mema, pcimask(mema), ioa);
  735. /*
  736. * Align the windows and map it
  737. */
  738. ioa = 0x1000;
  739. mema = 0x90000000;
  740. pcilog("Mask sizes: mem=%lux io=%lux\n", mema, ioa);
  741. pcibusmap(pciroot, &mema, &ioa, 1);
  742. DBG("Sizes2: mem=%lux io=%lux\n", mema, ioa);
  743. unlock(&pcicfginitlock);
  744. return;
  745. }
  746. if (!nopcirouting)
  747. pcirouting();
  748. out:
  749. unlock(&pcicfginitlock);
  750. if(getconf("*pcihinv"))
  751. pcihinv(nil);
  752. }
  753. static int
  754. pcicfgrw8(int tbdf, int rno, int data, int read)
  755. {
  756. int o, type, x;
  757. if(pcicfgmode == -1)
  758. pcicfginit();
  759. if(BUSBNO(tbdf))
  760. type = 0x01;
  761. else
  762. type = 0x00;
  763. x = -1;
  764. if(BUSDNO(tbdf) > pcimaxdno)
  765. return x;
  766. lock(&pcicfglock);
  767. switch(pcicfgmode){
  768. case 1:
  769. o = rno & 0x03;
  770. rno &= ~0x03;
  771. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  772. if(read)
  773. x = inb(PciDATA+o);
  774. else
  775. outb(PciDATA+o, data);
  776. outl(PciADDR, 0);
  777. break;
  778. case 2:
  779. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  780. outb(PciFORWARD, BUSBNO(tbdf));
  781. if(read)
  782. x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  783. else
  784. outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  785. outb(PciCSE, 0);
  786. break;
  787. }
  788. unlock(&pcicfglock);
  789. return x;
  790. }
  791. int
  792. pcicfgr8(Pcidev* pcidev, int rno)
  793. {
  794. return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
  795. }
  796. void
  797. pcicfgw8(Pcidev* pcidev, int rno, int data)
  798. {
  799. pcicfgrw8(pcidev->tbdf, rno, data, 0);
  800. }
  801. static int
  802. pcicfgrw16(int tbdf, int rno, int data, int read)
  803. {
  804. int o, type, x;
  805. if(pcicfgmode == -1)
  806. pcicfginit();
  807. if(BUSBNO(tbdf))
  808. type = 0x01;
  809. else
  810. type = 0x00;
  811. x = -1;
  812. if(BUSDNO(tbdf) > pcimaxdno)
  813. return x;
  814. lock(&pcicfglock);
  815. switch(pcicfgmode){
  816. case 1:
  817. o = rno & 0x02;
  818. rno &= ~0x03;
  819. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  820. if(read)
  821. x = ins(PciDATA+o);
  822. else
  823. outs(PciDATA+o, data);
  824. outl(PciADDR, 0);
  825. break;
  826. case 2:
  827. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  828. outb(PciFORWARD, BUSBNO(tbdf));
  829. if(read)
  830. x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  831. else
  832. outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  833. outb(PciCSE, 0);
  834. break;
  835. }
  836. unlock(&pcicfglock);
  837. return x;
  838. }
  839. int
  840. pcicfgr16(Pcidev* pcidev, int rno)
  841. {
  842. return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
  843. }
  844. void
  845. pcicfgw16(Pcidev* pcidev, int rno, int data)
  846. {
  847. pcicfgrw16(pcidev->tbdf, rno, data, 0);
  848. }
  849. static int
  850. pcicfgrw32(int tbdf, int rno, int data, int read)
  851. {
  852. int type, x;
  853. if(pcicfgmode == -1)
  854. pcicfginit();
  855. if(BUSBNO(tbdf))
  856. type = 0x01;
  857. else
  858. type = 0x00;
  859. x = -1;
  860. if(BUSDNO(tbdf) > pcimaxdno)
  861. return x;
  862. lock(&pcicfglock);
  863. switch(pcicfgmode){
  864. case 1:
  865. rno &= ~0x03;
  866. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  867. if(read)
  868. x = inl(PciDATA);
  869. else
  870. outl(PciDATA, data);
  871. outl(PciADDR, 0);
  872. break;
  873. case 2:
  874. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  875. outb(PciFORWARD, BUSBNO(tbdf));
  876. if(read)
  877. x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  878. else
  879. outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  880. outb(PciCSE, 0);
  881. break;
  882. }
  883. unlock(&pcicfglock);
  884. return x;
  885. }
  886. int
  887. pcicfgr32(Pcidev* pcidev, int rno)
  888. {
  889. return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
  890. }
  891. void
  892. pcicfgw32(Pcidev* pcidev, int rno, int data)
  893. {
  894. pcicfgrw32(pcidev->tbdf, rno, data, 0);
  895. }
  896. Pcidev*
  897. pcimatch(Pcidev* prev, int vid, int did)
  898. {
  899. if(pcicfgmode == -1)
  900. pcicfginit();
  901. if(prev == nil)
  902. prev = pcilist;
  903. else
  904. prev = prev->list;
  905. while(prev != nil){
  906. if((vid == 0 || prev->vid == vid)
  907. && (did == 0 || prev->did == did))
  908. break;
  909. prev = prev->list;
  910. }
  911. return prev;
  912. }
  913. Pcidev*
  914. pcimatchtbdf(int tbdf)
  915. {
  916. Pcidev *pcidev;
  917. if(pcicfgmode == -1)
  918. pcicfginit();
  919. for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
  920. if(pcidev->tbdf == tbdf)
  921. break;
  922. }
  923. return pcidev;
  924. }
  925. uchar
  926. pciipin(Pcidev *pci, uchar pin)
  927. {
  928. if (pci == nil)
  929. pci = pcilist;
  930. while (pci) {
  931. uchar intl;
  932. if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
  933. return pci->intl;
  934. if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
  935. return intl;
  936. pci = pci->list;
  937. }
  938. return 0;
  939. }
  940. static void
  941. pcilhinv(Pcidev* p)
  942. {
  943. int i;
  944. Pcidev *t;
  945. if(p == nil) {
  946. putstrn(PCICONS.output, PCICONS.ptr);
  947. p = pciroot;
  948. print("bus dev type vid did intl memory\n");
  949. }
  950. for(t = p; t != nil; t = t->link) {
  951. print("%d %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d ",
  952. BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
  953. t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
  954. for(i = 0; i < nelem(p->mem); i++) {
  955. if(t->mem[i].size == 0)
  956. continue;
  957. print("%d:%.8lux %d ", i,
  958. t->mem[i].bar, t->mem[i].size);
  959. }
  960. if(t->ioa.bar || t->ioa.size)
  961. print("ioa:%.8lux %d ", t->ioa.bar, t->ioa.size);
  962. if(t->mema.bar || t->mema.size)
  963. print("mema:%.8lux %d ", t->mema.bar, t->mema.size);
  964. if(t->bridge)
  965. print("->%d", BUSBNO(t->bridge->tbdf));
  966. print("\n");
  967. }
  968. while(p != nil) {
  969. if(p->bridge != nil)
  970. pcilhinv(p->bridge);
  971. p = p->link;
  972. }
  973. }
  974. void
  975. pcihinv(Pcidev* p)
  976. {
  977. if(pcicfgmode == -1)
  978. pcicfginit();
  979. lock(&pcicfginitlock);
  980. pcilhinv(p);
  981. unlock(&pcicfginitlock);
  982. }
  983. void
  984. pcireset(void)
  985. {
  986. Pcidev *p;
  987. if(pcicfgmode == -1)
  988. pcicfginit();
  989. for(p = pcilist; p != nil; p = p->list) {
  990. /* don't mess with the bridges */
  991. if(p->ccrb == 0x06)
  992. continue;
  993. pciclrbme(p);
  994. }
  995. }
  996. void
  997. pcisetioe(Pcidev* p)
  998. {
  999. int pcr;
  1000. pcr = pcicfgr16(p, PciPCR);
  1001. pcr |= IOen;
  1002. pcicfgw16(p, PciPCR, pcr);
  1003. }
  1004. void
  1005. pciclrioe(Pcidev* p)
  1006. {
  1007. int pcr;
  1008. pcr = pcicfgr16(p, PciPCR);
  1009. pcr &= ~IOen;
  1010. pcicfgw16(p, PciPCR, pcr);
  1011. }
  1012. void
  1013. pcisetbme(Pcidev* p)
  1014. {
  1015. int pcr;
  1016. pcr = pcicfgr16(p, PciPCR);
  1017. pcr |= MASen;
  1018. pcicfgw16(p, PciPCR, pcr);
  1019. }
  1020. void
  1021. pciclrbme(Pcidev* p)
  1022. {
  1023. int pcr;
  1024. pcr = pcicfgr16(p, PciPCR);
  1025. pcr &= ~MASen;
  1026. pcicfgw16(p, PciPCR, pcr);
  1027. }