pci.c 26 KB

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