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. ioa = *pioa;
  152. mema = *pmema;
  153. DBG("pcibusmap wr=%d %T mem=%luX io=%luX\n",
  154. wrreg, root->tbdf, mema, ioa);
  155. ntb = 0;
  156. for(p = root; p != nil; p = p->link)
  157. ntb++;
  158. ntb *= (PciCIS-PciBAR0)/4;
  159. table = malloc(2*ntb*sizeof(Pcisiz));
  160. itb = table;
  161. mtb = table+ntb;
  162. /*
  163. * Build a table of sizes
  164. */
  165. for(p = root; p != nil; p = p->link) {
  166. if(p->ccrb == 0x06) {
  167. if(p->ccru != 0x04 || p->bridge == nil) {
  168. // DBG("pci: ignored bridge %T\n", p->tbdf);
  169. continue;
  170. }
  171. sioa = ioa;
  172. smema = mema;
  173. pcibusmap(p->bridge, &smema, &sioa, 0);
  174. hole = pcimask(smema-mema);
  175. if(hole < (1<<20))
  176. hole = 1<<20;
  177. p->mema.size = hole;
  178. hole = pcimask(sioa-ioa);
  179. if(hole < (1<<12))
  180. hole = 1<<12;
  181. p->ioa.size = hole;
  182. itb->dev = p;
  183. itb->bar = -1;
  184. itb->siz = p->ioa.size;
  185. itb++;
  186. mtb->dev = p;
  187. mtb->bar = -1;
  188. mtb->siz = p->mema.size;
  189. mtb++;
  190. continue;
  191. }
  192. for(i = 0; i <= 5; i++) {
  193. rno = PciBAR0 + i*4;
  194. v = pcicfgrw32(p->tbdf, rno, 0, 1);
  195. size = pcibarsize(p, rno);
  196. if(size == 0)
  197. continue;
  198. if(v & 1) {
  199. itb->dev = p;
  200. itb->bar = i;
  201. itb->siz = size;
  202. itb++;
  203. }
  204. else {
  205. mtb->dev = p;
  206. mtb->bar = i;
  207. mtb->siz = size;
  208. mtb++;
  209. }
  210. p->mem[i].size = size;
  211. }
  212. }
  213. /*
  214. * Sort both tables IO smallest first, Memory largest
  215. */
  216. qsort(table, itb-table, sizeof(Pcisiz), pcisizcmp);
  217. tptr = table+ntb;
  218. qsort(tptr, mtb-tptr, sizeof(Pcisiz), pcisizcmp);
  219. /*
  220. * Allocate IO address space on this bus
  221. */
  222. for(tptr = table; tptr < itb; tptr++) {
  223. hole = tptr->siz;
  224. if(tptr->bar == -1)
  225. hole = 1<<12;
  226. ioa = (ioa+hole-1) & ~(hole-1);
  227. p = tptr->dev;
  228. if(tptr->bar == -1)
  229. p->ioa.bar = ioa;
  230. else {
  231. p->pcr |= IOen;
  232. p->mem[tptr->bar].bar = ioa|1;
  233. if(wrreg)
  234. pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), ioa|1, 0);
  235. }
  236. ioa += tptr->siz;
  237. }
  238. /*
  239. * Allocate Memory address space on this bus
  240. */
  241. for(tptr = table+ntb; tptr < mtb; tptr++) {
  242. hole = tptr->siz;
  243. if(tptr->bar == -1)
  244. hole = 1<<20;
  245. mema = (mema+hole-1) & ~(hole-1);
  246. p = tptr->dev;
  247. if(tptr->bar == -1)
  248. p->mema.bar = mema;
  249. else {
  250. p->pcr |= MEMen;
  251. p->mem[tptr->bar].bar = mema;
  252. if(wrreg)
  253. pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), mema, 0);
  254. }
  255. mema += tptr->siz;
  256. }
  257. *pmema = mema;
  258. *pioa = ioa;
  259. free(table);
  260. if(wrreg == 0)
  261. return;
  262. /*
  263. * Finally set all the bridge addresses & registers
  264. */
  265. for(p = root; p != nil; p = p->link) {
  266. if(p->bridge == nil) {
  267. pcicfgrw8(p->tbdf, PciLTR, 64, 0);
  268. p->pcr |= MASen;
  269. pcicfgrw16(p->tbdf, PciPCR, p->pcr, 0);
  270. continue;
  271. }
  272. base = p->ioa.bar;
  273. limit = base+p->ioa.size-1;
  274. v = pcicfgrw32(p->tbdf, PciIBR, 0, 1);
  275. v = (v&0xFFFF0000)|(limit & 0xF000)|((base & 0xF000)>>8);
  276. pcicfgrw32(p->tbdf, PciIBR, v, 0);
  277. v = (limit & 0xFFFF0000)|(base>>16);
  278. pcicfgrw32(p->tbdf, PciIUBR, v, 0);
  279. base = p->mema.bar;
  280. limit = base+p->mema.size-1;
  281. v = (limit & 0xFFF00000)|((base & 0xFFF00000)>>16);
  282. pcicfgrw32(p->tbdf, PciMBR, v, 0);
  283. /*
  284. * Disable memory prefetch
  285. */
  286. pcicfgrw32(p->tbdf, PciPMBR, 0x0000FFFF, 0);
  287. pcicfgrw8(p->tbdf, PciLTR, 64, 0);
  288. /*
  289. * Enable the bridge
  290. */
  291. p->pcr |= IOen|MEMen|MASen;
  292. pcicfgrw32(p->tbdf, PciPCR, 0xFFFF0000|p->pcr , 0);
  293. sioa = p->ioa.bar;
  294. smema = p->mema.bar;
  295. pcibusmap(p->bridge, &smema, &sioa, 1);
  296. }
  297. }
  298. static int
  299. pcilscan(int bno, Pcidev** list)
  300. {
  301. Pcidev *p, *head, *tail;
  302. int dno, fno, i, hdt, l, maxfno, maxubn, rno, sbn, tbdf, ubn;
  303. maxubn = bno;
  304. head = nil;
  305. tail = nil;
  306. for(dno = 0; dno <= pcimaxdno; dno++){
  307. maxfno = 0;
  308. for(fno = 0; fno <= maxfno; fno++){
  309. /*
  310. * For this possible device, form the
  311. * bus+device+function triplet needed to address it
  312. * and try to read the vendor and device ID.
  313. * If successful, allocate a device struct and
  314. * start to fill it in with some useful information
  315. * from the device's configuration space.
  316. */
  317. tbdf = MKBUS(BusPCI, bno, dno, fno);
  318. l = pcicfgrw32(tbdf, PciVID, 0, 1);
  319. if(l == 0xFFFFFFFF || l == 0)
  320. continue;
  321. p = malloc(sizeof(*p));
  322. p->tbdf = tbdf;
  323. p->vid = l;
  324. p->did = l>>16;
  325. if(pcilist != nil)
  326. pcitail->list = p;
  327. else
  328. pcilist = p;
  329. pcitail = p;
  330. p->pcr = pcicfgr16(p, PciPCR);
  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->cls = pcicfgr8(p, PciCLS);
  336. p->ltr = pcicfgr8(p, PciLTR);
  337. p->intl = pcicfgr8(p, PciINTL);
  338. /*
  339. * If the device is a multi-function device adjust the
  340. * loop count so all possible functions are checked.
  341. */
  342. hdt = pcicfgr8(p, PciHDT);
  343. if(hdt & 0x80)
  344. maxfno = MaxFNO;
  345. /*
  346. * If appropriate, read the base address registers
  347. * and work out the sizes.
  348. */
  349. switch(p->ccrb) {
  350. case 0x01: /* mass storage controller */
  351. case 0x02: /* network controller */
  352. case 0x03: /* display controller */
  353. case 0x04: /* multimedia device */
  354. case 0x07: /* simple comm. controllers */
  355. case 0x08: /* base system peripherals */
  356. case 0x09: /* input devices */
  357. case 0x0A: /* docking stations */
  358. case 0x0B: /* processors */
  359. case 0x0C: /* serial bus controllers */
  360. if((hdt & 0x7F) != 0)
  361. break;
  362. rno = PciBAR0 - 4;
  363. for(i = 0; i < nelem(p->mem); i++) {
  364. rno += 4;
  365. p->mem[i].bar = pcicfgr32(p, rno);
  366. p->mem[i].size = pcibarsize(p, rno);
  367. }
  368. break;
  369. case 0x00:
  370. case 0x05: /* memory controller */
  371. case 0x06: /* bridge device */
  372. default:
  373. break;
  374. }
  375. if(head != nil)
  376. tail->link = p;
  377. else
  378. head = p;
  379. tail = p;
  380. }
  381. }
  382. *list = head;
  383. for(p = head; p != nil; p = p->link){
  384. /*
  385. * Find PCI-PCI bridges and recursively descend the tree.
  386. */
  387. if(p->ccrb != 0x06 || p->ccru != 0x04)
  388. continue;
  389. /*
  390. * If the secondary or subordinate bus number is not
  391. * initialised try to do what the PCI BIOS should have
  392. * done and fill in the numbers as the tree is descended.
  393. * On the way down the subordinate bus number is set to
  394. * the maximum as it's not known how many buses are behind
  395. * this one; the final value is set on the way back up.
  396. */
  397. sbn = pcicfgr8(p, PciSBN);
  398. ubn = pcicfgr8(p, PciUBN);
  399. if(sbn == 0 || ubn == 0 || nobios) {
  400. sbn = maxubn+1;
  401. /*
  402. * Make sure memory, I/O and master enables are
  403. * off, set the primary, secondary and subordinate
  404. * bus numbers and clear the secondary status before
  405. * attempting to scan the secondary bus.
  406. *
  407. * Initialisation of the bridge should be done here.
  408. */
  409. pcicfgw32(p, PciPCR, 0xFFFF0000);
  410. l = (MaxUBN<<16)|(sbn<<8)|bno;
  411. pcicfgw32(p, PciPBN, l);
  412. pcicfgw16(p, PciSPSR, 0xFFFF);
  413. maxubn = pcilscan(sbn, &p->bridge);
  414. l = (maxubn<<16)|(sbn<<8)|bno;
  415. pcicfgw32(p, PciPBN, l);
  416. }
  417. else {
  418. if(ubn > maxubn)
  419. maxubn = ubn;
  420. pcilscan(sbn, &p->bridge);
  421. }
  422. }
  423. return maxubn;
  424. }
  425. int
  426. pciscan(int bno, Pcidev **list)
  427. {
  428. int ubn;
  429. lock(&pcicfginitlock);
  430. ubn = pcilscan(bno, list);
  431. unlock(&pcicfginitlock);
  432. return ubn;
  433. }
  434. static uchar
  435. pIIxget(Pcidev *router, uchar link)
  436. {
  437. uchar pirq;
  438. /* link should be 0x60, 0x61, 0x62, 0x63 */
  439. pirq = pcicfgr8(router, link);
  440. return (pirq < 16)? pirq: 0;
  441. }
  442. static void
  443. pIIxset(Pcidev *router, uchar link, uchar irq)
  444. {
  445. pcicfgw8(router, link, irq);
  446. }
  447. static uchar
  448. viaget(Pcidev *router, uchar link)
  449. {
  450. uchar pirq;
  451. /* link should be 1, 2, 3, 5 */
  452. pirq = (link < 6)? pcicfgr8(router, 0x55 + (link>>1)): 0;
  453. return (link & 1)? (pirq >> 4): (pirq & 15);
  454. }
  455. static void
  456. viaset(Pcidev *router, uchar link, uchar irq)
  457. {
  458. uchar pirq;
  459. pirq = pcicfgr8(router, 0x55 + (link >> 1));
  460. pirq &= (link & 1)? 0x0f: 0xf0;
  461. pirq |= (link & 1)? (irq << 4): (irq & 15);
  462. pcicfgw8(router, 0x55 + (link>>1), pirq);
  463. }
  464. static uchar
  465. optiget(Pcidev *router, uchar link)
  466. {
  467. uchar pirq = 0;
  468. /* link should be 0x02, 0x12, 0x22, 0x32 */
  469. if ((link & 0xcf) == 0x02)
  470. pirq = pcicfgr8(router, 0xb8 + (link >> 5));
  471. return (link & 0x10)? (pirq >> 4): (pirq & 15);
  472. }
  473. static void
  474. optiset(Pcidev *router, uchar link, uchar irq)
  475. {
  476. uchar pirq;
  477. pirq = pcicfgr8(router, 0xb8 + (link >> 5));
  478. pirq &= (link & 0x10)? 0x0f : 0xf0;
  479. pirq |= (link & 0x10)? (irq << 4): (irq & 15);
  480. pcicfgw8(router, 0xb8 + (link >> 5), pirq);
  481. }
  482. static uchar
  483. aliget(Pcidev *router, uchar link)
  484. {
  485. /* No, you're not dreaming */
  486. static const uchar map[] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
  487. uchar pirq;
  488. /* link should be 0x01..0x08 */
  489. pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
  490. return (link & 1)? map[pirq&15]: map[pirq>>4];
  491. }
  492. static void
  493. aliset(Pcidev *router, uchar link, uchar irq)
  494. {
  495. /* Inverse of map in aliget */
  496. static const uchar map[] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
  497. uchar pirq;
  498. pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
  499. pirq &= (link & 1)? 0x0f: 0xf0;
  500. pirq |= (link & 1)? (map[irq] << 4): (map[irq] & 15);
  501. pcicfgw8(router, 0x48 + ((link-1)>>1), pirq);
  502. }
  503. static uchar
  504. cyrixget(Pcidev *router, uchar link)
  505. {
  506. uchar pirq;
  507. /* link should be 1, 2, 3, 4 */
  508. pirq = pcicfgr8(router, 0x5c + ((link-1)>>1));
  509. return ((link & 1)? pirq >> 4: pirq & 15);
  510. }
  511. static void
  512. cyrixset(Pcidev *router, uchar link, uchar irq)
  513. {
  514. uchar pirq;
  515. pirq = pcicfgr8(router, 0x5c + (link>>1));
  516. pirq &= (link & 1)? 0x0f: 0xf0;
  517. pirq |= (link & 1)? (irq << 4): (irq & 15);
  518. pcicfgw8(router, 0x5c + (link>>1), pirq);
  519. }
  520. typedef struct Bridge Bridge;
  521. struct Bridge
  522. {
  523. ushort vid;
  524. ushort did;
  525. uchar (*get)(Pcidev *, uchar);
  526. void (*set)(Pcidev *, uchar, uchar);
  527. };
  528. static Bridge southbridges[] = {
  529. { 0x8086, 0x122e, pIIxget, pIIxset }, // Intel 82371FB
  530. { 0x8086, 0x1234, pIIxget, pIIxset }, // Intel 82371MX
  531. { 0x8086, 0x7000, pIIxget, pIIxset }, // Intel 82371SB
  532. { 0x8086, 0x7110, pIIxget, pIIxset }, // Intel 82371AB
  533. { 0x8086, 0x7198, pIIxget, pIIxset }, // Intel 82443MX (fn 1)
  534. { 0x8086, 0x2410, pIIxget, pIIxset }, // Intel 82801AA
  535. { 0x8086, 0x2420, pIIxget, pIIxset }, // Intel 82801AB
  536. { 0x8086, 0x2440, pIIxget, pIIxset }, // Intel 82801BA
  537. { 0x8086, 0x244c, pIIxget, pIIxset }, // Intel 82801BAM
  538. { 0x8086, 0x2480, pIIxget, pIIxset }, // Intel 82801CA
  539. { 0x8086, 0x248c, pIIxget, pIIxset }, // Intel 82801CAM
  540. { 0x8086, 0x24c0, pIIxget, pIIxset }, // Intel 82801DBL
  541. { 0x8086, 0x24cc, pIIxget, pIIxset }, // Intel 82801DBM
  542. { 0x8086, 0x24d0, pIIxget, pIIxset }, // Intel 82801EB
  543. { 0x8086, 0x2640, pIIxget, pIIxset }, // Intel 82801FB
  544. { 0x8086, 0x27b9, pIIxget, pIIxset }, // Intel 82801GBM
  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. void
  652. pcibussize(Pcidev *root, ulong *msize, ulong *iosize)
  653. {
  654. *msize = 0;
  655. *iosize = 0;
  656. pcibusmap(root, msize, iosize, 0);
  657. }
  658. static void
  659. pcicfginit(void)
  660. {
  661. char *p;
  662. Pcidev **list;
  663. ulong mema, ioa;
  664. int bno, n, pcibios;
  665. lock(&pcicfginitlock);
  666. if(pcicfgmode != -1)
  667. goto out;
  668. pcibios = 0;
  669. if(getconf("*nobios"))
  670. nobios = 1;
  671. else if(getconf("*pcibios"))
  672. pcibios = 1;
  673. if(getconf("*nopcirouting"))
  674. nopcirouting = 1;
  675. /*
  676. * Try to determine which PCI configuration mode is implemented.
  677. * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
  678. * a DWORD at 0xCF8 and another at 0xCFC and will pass through
  679. * any non-DWORD accesses as normal I/O cycles. There shouldn't be
  680. * a device behind these addresses so if Mode1 accesses fail try
  681. * for Mode2 (Mode2 is deprecated).
  682. */
  683. if(!pcibios){
  684. /*
  685. * Bits [30:24] of PciADDR must be 0,
  686. * according to the spec.
  687. */
  688. n = inl(PciADDR);
  689. if(!(n & 0x7FF00000)){
  690. outl(PciADDR, 0x80000000);
  691. outb(PciADDR+3, 0);
  692. if(inl(PciADDR) & 0x80000000){
  693. pcicfgmode = 1;
  694. pcimaxdno = 31;
  695. }
  696. }
  697. outl(PciADDR, n);
  698. if(pcicfgmode < 0){
  699. /*
  700. * The 'key' part of PciCSE should be 0.
  701. */
  702. n = inb(PciCSE);
  703. if(!(n & 0xF0)){
  704. outb(PciCSE, 0x0E);
  705. if(inb(PciCSE) == 0x0E){
  706. pcicfgmode = 2;
  707. pcimaxdno = 15;
  708. }
  709. }
  710. outb(PciCSE, n);
  711. }
  712. }
  713. if(pcicfgmode < 0)
  714. goto out;
  715. fmtinstall('T', tbdffmt);
  716. if(p = getconf("*pcimaxbno")){
  717. n = strtoul(p, 0, 0);
  718. if(n < pcimaxbno)
  719. pcimaxbno = n;
  720. }
  721. if(p = getconf("*pcimaxdno")){
  722. n = strtoul(p, 0, 0);
  723. if(n < pcimaxdno)
  724. pcimaxdno = n;
  725. }
  726. list = &pciroot;
  727. for(bno = 0; bno <= pcimaxbno; bno++) {
  728. int sbno = bno;
  729. bno = pcilscan(bno, list);
  730. while(*list)
  731. list = &(*list)->link;
  732. if (sbno == 0) {
  733. Pcidev *pci;
  734. /*
  735. * If we have found a PCI-to-Cardbus bridge, make sure
  736. * it has no valid mappings anymore.
  737. */
  738. for(pci = pciroot; pci != nil; pci = pci->link){
  739. if (pci->ccrb == 6 && pci->ccru == 7) {
  740. ushort bcr;
  741. /* reset the cardbus */
  742. bcr = pcicfgr16(pci, PciBCR);
  743. pcicfgw16(pci, PciBCR, 0x40 | bcr);
  744. delay(50);
  745. }
  746. }
  747. }
  748. }
  749. if(pciroot == nil)
  750. goto out;
  751. if(nobios) {
  752. /*
  753. * Work out how big the top bus is
  754. */
  755. pcibussize(pciroot, &mema, &ioa);
  756. /*
  757. * Align the windows and map it
  758. */
  759. ioa = 0x1000;
  760. mema = 0x90000000;
  761. pcilog("Mask sizes: mem=%lux io=%lux\n", mema, ioa);
  762. pcibusmap(pciroot, &mema, &ioa, 1);
  763. DBG("Sizes2: mem=%lux io=%lux\n", mema, ioa);
  764. unlock(&pcicfginitlock);
  765. return;
  766. }
  767. if (!nopcirouting)
  768. pcirouting();
  769. out:
  770. pcireservemem();
  771. unlock(&pcicfginitlock);
  772. if(getconf("*pcihinv"))
  773. pcihinv(nil);
  774. }
  775. static void
  776. pcireservemem(void)
  777. {
  778. int i;
  779. Pcidev *p;
  780. /*
  781. * mark all the physical address space claimed by pci devices
  782. * as in use, so that upaalloc doesn't give it out.
  783. */
  784. for(p=pciroot; p; p=p->list)
  785. for(i=0; i<nelem(p->mem); i++)
  786. if(p->mem[i].bar && (p->mem[i].bar&1) == 0)
  787. upareserve(p->mem[i].bar&~0x0F, p->mem[i].size);
  788. }
  789. static int
  790. pcicfgrw8(int tbdf, int rno, int data, int read)
  791. {
  792. int o, type, x;
  793. if(pcicfgmode == -1)
  794. pcicfginit();
  795. if(BUSBNO(tbdf))
  796. type = 0x01;
  797. else
  798. type = 0x00;
  799. x = -1;
  800. if(BUSDNO(tbdf) > pcimaxdno)
  801. return x;
  802. lock(&pcicfglock);
  803. switch(pcicfgmode){
  804. case 1:
  805. o = rno & 0x03;
  806. rno &= ~0x03;
  807. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  808. if(read)
  809. x = inb(PciDATA+o);
  810. else
  811. outb(PciDATA+o, data);
  812. outl(PciADDR, 0);
  813. break;
  814. case 2:
  815. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  816. outb(PciFORWARD, BUSBNO(tbdf));
  817. if(read)
  818. x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  819. else
  820. outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  821. outb(PciCSE, 0);
  822. break;
  823. }
  824. unlock(&pcicfglock);
  825. return x;
  826. }
  827. int
  828. pcicfgr8(Pcidev* pcidev, int rno)
  829. {
  830. return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
  831. }
  832. void
  833. pcicfgw8(Pcidev* pcidev, int rno, int data)
  834. {
  835. pcicfgrw8(pcidev->tbdf, rno, data, 0);
  836. }
  837. static int
  838. pcicfgrw16(int tbdf, int rno, int data, int read)
  839. {
  840. int o, type, x;
  841. if(pcicfgmode == -1)
  842. pcicfginit();
  843. if(BUSBNO(tbdf))
  844. type = 0x01;
  845. else
  846. type = 0x00;
  847. x = -1;
  848. if(BUSDNO(tbdf) > pcimaxdno)
  849. return x;
  850. lock(&pcicfglock);
  851. switch(pcicfgmode){
  852. case 1:
  853. o = rno & 0x02;
  854. rno &= ~0x03;
  855. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  856. if(read)
  857. x = ins(PciDATA+o);
  858. else
  859. outs(PciDATA+o, data);
  860. outl(PciADDR, 0);
  861. break;
  862. case 2:
  863. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  864. outb(PciFORWARD, BUSBNO(tbdf));
  865. if(read)
  866. x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  867. else
  868. outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  869. outb(PciCSE, 0);
  870. break;
  871. }
  872. unlock(&pcicfglock);
  873. return x;
  874. }
  875. int
  876. pcicfgr16(Pcidev* pcidev, int rno)
  877. {
  878. return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
  879. }
  880. void
  881. pcicfgw16(Pcidev* pcidev, int rno, int data)
  882. {
  883. pcicfgrw16(pcidev->tbdf, rno, data, 0);
  884. }
  885. static int
  886. pcicfgrw32(int tbdf, int rno, int data, int read)
  887. {
  888. int type, x;
  889. if(pcicfgmode == -1)
  890. pcicfginit();
  891. if(BUSBNO(tbdf))
  892. type = 0x01;
  893. else
  894. type = 0x00;
  895. x = -1;
  896. if(BUSDNO(tbdf) > pcimaxdno)
  897. return x;
  898. lock(&pcicfglock);
  899. switch(pcicfgmode){
  900. case 1:
  901. rno &= ~0x03;
  902. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  903. if(read)
  904. x = inl(PciDATA);
  905. else
  906. outl(PciDATA, data);
  907. outl(PciADDR, 0);
  908. break;
  909. case 2:
  910. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  911. outb(PciFORWARD, BUSBNO(tbdf));
  912. if(read)
  913. x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  914. else
  915. outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  916. outb(PciCSE, 0);
  917. break;
  918. }
  919. unlock(&pcicfglock);
  920. return x;
  921. }
  922. int
  923. pcicfgr32(Pcidev* pcidev, int rno)
  924. {
  925. return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
  926. }
  927. void
  928. pcicfgw32(Pcidev* pcidev, int rno, int data)
  929. {
  930. pcicfgrw32(pcidev->tbdf, rno, data, 0);
  931. }
  932. Pcidev*
  933. pcimatch(Pcidev* prev, int vid, int did)
  934. {
  935. if(pcicfgmode == -1)
  936. pcicfginit();
  937. if(prev == nil)
  938. prev = pcilist;
  939. else
  940. prev = prev->list;
  941. while(prev != nil){
  942. if((vid == 0 || prev->vid == vid)
  943. && (did == 0 || prev->did == did))
  944. break;
  945. prev = prev->list;
  946. }
  947. return prev;
  948. }
  949. Pcidev*
  950. pcimatchtbdf(int tbdf)
  951. {
  952. Pcidev *pcidev;
  953. if(pcicfgmode == -1)
  954. pcicfginit();
  955. for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
  956. if(pcidev->tbdf == tbdf)
  957. break;
  958. }
  959. return pcidev;
  960. }
  961. uchar
  962. pciipin(Pcidev *pci, uchar pin)
  963. {
  964. if (pci == nil)
  965. pci = pcilist;
  966. while (pci) {
  967. uchar intl;
  968. if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
  969. return pci->intl;
  970. if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
  971. return intl;
  972. pci = pci->list;
  973. }
  974. return 0;
  975. }
  976. static void
  977. pcilhinv(Pcidev* p)
  978. {
  979. int i;
  980. Pcidev *t;
  981. if(p == nil) {
  982. putstrn(PCICONS.output, PCICONS.ptr);
  983. p = pciroot;
  984. print("bus dev type vid did intl memory\n");
  985. }
  986. for(t = p; t != nil; t = t->link) {
  987. print("%d %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d ",
  988. BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
  989. t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
  990. for(i = 0; i < nelem(p->mem); i++) {
  991. if(t->mem[i].size == 0)
  992. continue;
  993. print("%d:%.8lux %d ", i,
  994. t->mem[i].bar, t->mem[i].size);
  995. }
  996. if(t->ioa.bar || t->ioa.size)
  997. print("ioa:%.8lux %d ", t->ioa.bar, t->ioa.size);
  998. if(t->mema.bar || t->mema.size)
  999. print("mema:%.8lux %d ", t->mema.bar, t->mema.size);
  1000. if(t->bridge)
  1001. print("->%d", BUSBNO(t->bridge->tbdf));
  1002. print("\n");
  1003. }
  1004. while(p != nil) {
  1005. if(p->bridge != nil)
  1006. pcilhinv(p->bridge);
  1007. p = p->link;
  1008. }
  1009. }
  1010. void
  1011. pcihinv(Pcidev* p)
  1012. {
  1013. if(pcicfgmode == -1)
  1014. pcicfginit();
  1015. lock(&pcicfginitlock);
  1016. pcilhinv(p);
  1017. unlock(&pcicfginitlock);
  1018. }
  1019. void
  1020. pcireset(void)
  1021. {
  1022. Pcidev *p;
  1023. if(pcicfgmode == -1)
  1024. pcicfginit();
  1025. for(p = pcilist; p != nil; p = p->list) {
  1026. /* don't mess with the bridges */
  1027. if(p->ccrb == 0x06)
  1028. continue;
  1029. pciclrbme(p);
  1030. }
  1031. }
  1032. void
  1033. pcisetioe(Pcidev* p)
  1034. {
  1035. p->pcr |= IOen;
  1036. pcicfgw16(p, PciPCR, p->pcr);
  1037. }
  1038. void
  1039. pciclrioe(Pcidev* p)
  1040. {
  1041. p->pcr &= ~IOen;
  1042. pcicfgw16(p, PciPCR, p->pcr);
  1043. }
  1044. void
  1045. pcisetbme(Pcidev* p)
  1046. {
  1047. p->pcr |= MASen;
  1048. pcicfgw16(p, PciPCR, p->pcr);
  1049. }
  1050. void
  1051. pciclrbme(Pcidev* p)
  1052. {
  1053. p->pcr &= ~MASen;
  1054. pcicfgw16(p, PciPCR, p->pcr);
  1055. }
  1056. void
  1057. pcisetmwi(Pcidev* p)
  1058. {
  1059. p->pcr |= MemWrInv;
  1060. pcicfgw16(p, PciPCR, p->pcr);
  1061. }
  1062. void
  1063. pciclrmwi(Pcidev* p)
  1064. {
  1065. p->pcr &= ~MemWrInv;
  1066. pcicfgw16(p, PciPCR, p->pcr);
  1067. }
  1068. static int
  1069. pcigetpmrb(Pcidev* p)
  1070. {
  1071. int ptr;
  1072. if(p->pmrb != 0)
  1073. return p->pmrb;
  1074. p->pmrb = -1;
  1075. /*
  1076. * If there are no extended capabilities implemented,
  1077. * (bit 4 in the status register) assume there's no standard
  1078. * power management method.
  1079. * Find the capabilities pointer based on PCI header type.
  1080. */
  1081. if(!(pcicfgr16(p, PciPSR) & 0x0010))
  1082. return -1;
  1083. switch(pcicfgr8(p, PciHDT)){
  1084. default:
  1085. return -1;
  1086. case 0: /* all other */
  1087. case 1: /* PCI to PCI bridge */
  1088. ptr = 0x34;
  1089. break;
  1090. case 2: /* CardBus bridge */
  1091. ptr = 0x14;
  1092. break;
  1093. }
  1094. ptr = pcicfgr32(p, ptr);
  1095. while(ptr != 0){
  1096. /*
  1097. * Check for validity.
  1098. * Can't be in standard header and must be double
  1099. * word aligned.
  1100. */
  1101. if(ptr < 0x40 || (ptr & ~0xFC))
  1102. return -1;
  1103. if(pcicfgr8(p, ptr) == 0x01){
  1104. p->pmrb = ptr;
  1105. return ptr;
  1106. }
  1107. ptr = pcicfgr8(p, ptr+1);
  1108. }
  1109. return -1;
  1110. }
  1111. int
  1112. pcigetpms(Pcidev* p)
  1113. {
  1114. int pmcsr, ptr;
  1115. if((ptr = pcigetpmrb(p)) == -1)
  1116. return -1;
  1117. /*
  1118. * Power Management Register Block:
  1119. * offset 0: Capability ID
  1120. * 1: next item pointer
  1121. * 2: capabilities
  1122. * 4: control/status
  1123. * 6: bridge support extensions
  1124. * 7: data
  1125. */
  1126. pmcsr = pcicfgr16(p, ptr+4);
  1127. return pmcsr & 0x0003;
  1128. }
  1129. int
  1130. pcisetpms(Pcidev* p, int state)
  1131. {
  1132. int ostate, pmc, pmcsr, ptr;
  1133. if((ptr = pcigetpmrb(p)) == -1)
  1134. return -1;
  1135. pmc = pcicfgr16(p, ptr+2);
  1136. pmcsr = pcicfgr16(p, ptr+4);
  1137. ostate = pmcsr & 0x0003;
  1138. pmcsr &= ~0x0003;
  1139. switch(state){
  1140. default:
  1141. return -1;
  1142. case 0:
  1143. break;
  1144. case 1:
  1145. if(!(pmc & 0x0200))
  1146. return -1;
  1147. break;
  1148. case 2:
  1149. if(!(pmc & 0x0400))
  1150. return -1;
  1151. break;
  1152. case 3:
  1153. break;
  1154. }
  1155. pmcsr |= state;
  1156. pcicfgw16(p, ptr+4, pmcsr);
  1157. return ostate;
  1158. }