pci.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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, 0x248c, pIIxget, pIIxset }, // Intel 82801CAM
  539. { 0x8086, 0x24c0, pIIxget, pIIxset }, // Intel 82801DBL
  540. { 0x8086, 0x24cc, pIIxget, pIIxset }, // Intel 82801DBM
  541. { 0x8086, 0x24d0, pIIxget, pIIxset }, // Intel 82801EB
  542. { 0x8086, 0x2640, pIIxget, pIIxset }, // Intel 82801FB
  543. { 0x8086, 0x27b9, pIIxget, pIIxset }, // Intel 82801GBM
  544. { 0x1106, 0x0586, viaget, viaset }, // Viatech 82C586
  545. { 0x1106, 0x0596, viaget, viaset }, // Viatech 82C596
  546. { 0x1106, 0x0686, viaget, viaset }, // Viatech 82C686
  547. { 0x1106, 0x3227, viaget, viaset }, // Viatech VT8237
  548. { 0x1045, 0xc700, optiget, optiset }, // Opti 82C700
  549. { 0x10b9, 0x1533, aliget, aliset }, // Al M1533
  550. { 0x1039, 0x0008, pIIxget, pIIxset }, // SI 503
  551. { 0x1039, 0x0496, pIIxget, pIIxset }, // SI 496
  552. { 0x1078, 0x0100, cyrixget, cyrixset }, // Cyrix 5530 Legacy
  553. { 0x1022, 0x746B, nil, nil }, // AMD 8111
  554. { 0x10DE, 0x00D1, nil, nil }, // NVIDIA nForce 3
  555. { 0x1166, 0x0200, nil, nil }, // ServerWorks ServerSet III LE
  556. { 0x1002, 0x4377, nil, nil }, // ATI Radeon Xpress 200M
  557. };
  558. typedef struct Slot Slot;
  559. struct Slot {
  560. uchar bus; // Pci bus number
  561. uchar dev; // Pci device number
  562. uchar maps[12]; // Avoid structs! Link and mask.
  563. uchar slot; // Add-in/built-in slot
  564. uchar reserved;
  565. };
  566. typedef struct Router Router;
  567. struct Router {
  568. uchar signature[4]; // Routing table signature
  569. uchar version[2]; // Version number
  570. uchar size[2]; // Total table size
  571. uchar bus; // Interrupt router bus number
  572. uchar devfn; // Router's devfunc
  573. uchar pciirqs[2]; // Exclusive PCI irqs
  574. uchar compat[4]; // Compatible PCI interrupt router
  575. uchar miniport[4]; // Miniport data
  576. uchar reserved[11];
  577. uchar checksum;
  578. };
  579. static ushort pciirqs; // Exclusive PCI irqs
  580. static Bridge *southbridge; // Which southbridge to use.
  581. static void
  582. pcirouting(void)
  583. {
  584. Slot *e;
  585. Router *r;
  586. int size, i, fn, tbdf;
  587. Pcidev *sbpci, *pci;
  588. uchar *p, pin, irq, link, *map;
  589. // Search for PCI interrupt routing table in BIOS
  590. for(p = (uchar *)KADDR(0xf0000); p < (uchar *)KADDR(0xfffff); p += 16)
  591. if(p[0] == '$' && p[1] == 'P' && p[2] == 'I' && p[3] == 'R')
  592. break;
  593. if(p >= (uchar *)KADDR(0xfffff))
  594. return;
  595. r = (Router *)p;
  596. // print("PCI interrupt routing table version %d.%d at %.6uX\n",
  597. // r->version[0], r->version[1], (ulong)r & 0xfffff);
  598. tbdf = (BusPCI << 24)|(r->bus << 16)|(r->devfn << 8);
  599. sbpci = pcimatchtbdf(tbdf);
  600. if(sbpci == nil) {
  601. print("pcirouting: Cannot find south bridge %T\n", tbdf);
  602. return;
  603. }
  604. for(i = 0; i != nelem(southbridges); i++)
  605. if(sbpci->vid == southbridges[i].vid && sbpci->did == southbridges[i].did)
  606. break;
  607. if(i == nelem(southbridges)) {
  608. print("pcirouting: ignoring south bridge %T %.4uX/%.4uX\n", tbdf, sbpci->vid, sbpci->did);
  609. return;
  610. }
  611. southbridge = &southbridges[i];
  612. if(southbridge->get == nil || southbridge->set == nil)
  613. return;
  614. pciirqs = (r->pciirqs[1] << 8)|r->pciirqs[0];
  615. size = (r->size[1] << 8)|r->size[0];
  616. for(e = (Slot *)&r[1]; (uchar *)e < p + size; e++) {
  617. // print("%.2uX/%.2uX %.2uX: ", e->bus, e->dev, e->slot);
  618. // for (i = 0; i != 4; i++) {
  619. // uchar *m = &e->maps[i * 3];
  620. // print("[%d] %.2uX %.4uX ",
  621. // i, m[0], (m[2] << 8)|m[1]);
  622. // }
  623. // print("\n");
  624. for(fn = 0; fn != 8; fn++) {
  625. tbdf = (BusPCI << 24)|(e->bus << 16)|((e->dev | fn) << 8);
  626. pci = pcimatchtbdf(tbdf);
  627. if(pci == nil)
  628. continue;
  629. pin = pcicfgr8(pci, PciINTP);
  630. if(pin == 0 || pin == 0xff)
  631. continue;
  632. map = &e->maps[(pin - 1) * 3];
  633. link = map[0];
  634. irq = southbridge->get(sbpci, link);
  635. if(irq == 0 || irq == pci->intl)
  636. continue;
  637. if(pci->intl != 0 && pci->intl != 0xFF) {
  638. print("pcirouting: BIOS workaround: %T at pin %d link %d irq %d -> %d\n",
  639. tbdf, pin, link, irq, pci->intl);
  640. southbridge->set(sbpci, link, pci->intl);
  641. continue;
  642. }
  643. print("pcirouting: %T at pin %d link %d irq %d\n", tbdf, pin, link, irq);
  644. pcicfgw8(pci, PciINTL, irq);
  645. pci->intl = irq;
  646. }
  647. }
  648. }
  649. static void pcireservemem(void);
  650. void
  651. pcibussize(Pcidev *root, ulong *msize, ulong *iosize)
  652. {
  653. *msize = 0;
  654. *iosize = 0;
  655. pcibusmap(root, msize, iosize, 0);
  656. }
  657. static void
  658. pcicfginit(void)
  659. {
  660. char *p;
  661. Pcidev **list;
  662. ulong mema, ioa;
  663. int bno, n, pcibios;
  664. lock(&pcicfginitlock);
  665. if(pcicfgmode != -1)
  666. goto out;
  667. pcibios = 0;
  668. if(getconf("*nobios"))
  669. nobios = 1;
  670. else if(getconf("*pcibios"))
  671. pcibios = 1;
  672. if(getconf("*nopcirouting"))
  673. nopcirouting = 1;
  674. /*
  675. * Try to determine which PCI configuration mode is implemented.
  676. * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
  677. * a DWORD at 0xCF8 and another at 0xCFC and will pass through
  678. * any non-DWORD accesses as normal I/O cycles. There shouldn't be
  679. * a device behind these addresses so if Mode1 accesses fail try
  680. * for Mode2 (Mode2 is deprecated).
  681. */
  682. if(!pcibios){
  683. /*
  684. * Bits [30:24] of PciADDR must be 0,
  685. * according to the spec.
  686. */
  687. n = inl(PciADDR);
  688. if(!(n & 0x7FF00000)){
  689. outl(PciADDR, 0x80000000);
  690. outb(PciADDR+3, 0);
  691. if(inl(PciADDR) & 0x80000000){
  692. pcicfgmode = 1;
  693. pcimaxdno = 31;
  694. }
  695. }
  696. outl(PciADDR, n);
  697. if(pcicfgmode < 0){
  698. /*
  699. * The 'key' part of PciCSE should be 0.
  700. */
  701. n = inb(PciCSE);
  702. if(!(n & 0xF0)){
  703. outb(PciCSE, 0x0E);
  704. if(inb(PciCSE) == 0x0E){
  705. pcicfgmode = 2;
  706. pcimaxdno = 15;
  707. }
  708. }
  709. outb(PciCSE, n);
  710. }
  711. }
  712. if(pcicfgmode < 0)
  713. goto out;
  714. fmtinstall('T', tbdffmt);
  715. if(p = getconf("*pcimaxbno")){
  716. n = strtoul(p, 0, 0);
  717. if(n < pcimaxbno)
  718. pcimaxbno = n;
  719. }
  720. if(p = getconf("*pcimaxdno")){
  721. n = strtoul(p, 0, 0);
  722. if(n < pcimaxdno)
  723. pcimaxdno = n;
  724. }
  725. list = &pciroot;
  726. for(bno = 0; bno <= pcimaxbno; bno++) {
  727. int sbno = bno;
  728. bno = pcilscan(bno, list);
  729. while(*list)
  730. list = &(*list)->link;
  731. if (sbno == 0) {
  732. Pcidev *pci;
  733. /*
  734. * If we have found a PCI-to-Cardbus bridge, make sure
  735. * it has no valid mappings anymore.
  736. */
  737. for(pci = pciroot; pci != nil; pci = pci->link){
  738. if (pci->ccrb == 6 && pci->ccru == 7) {
  739. ushort bcr;
  740. /* reset the cardbus */
  741. bcr = pcicfgr16(pci, PciBCR);
  742. pcicfgw16(pci, PciBCR, 0x40 | bcr);
  743. delay(50);
  744. }
  745. }
  746. }
  747. }
  748. if(pciroot == nil)
  749. goto out;
  750. if(nobios) {
  751. /*
  752. * Work out how big the top bus is
  753. */
  754. pcibussize(pciroot, &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(!(pcicfgr16(p, PciPSR) & 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. }