pci.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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, 0x24d0, pIIxget, pIIxset }, // Intel 82801EB
  543. { 0x1106, 0x0586, viaget, viaset }, // Viatech 82C586
  544. { 0x1106, 0x0596, viaget, viaset }, // Viatech 82C596
  545. { 0x1106, 0x0686, viaget, viaset }, // Viatech 82C686
  546. { 0x1045, 0xc700, optiget, optiset }, // Opti 82C700
  547. { 0x10b9, 0x1533, aliget, aliset }, // Al M1533
  548. { 0x1039, 0x0008, pIIxget, pIIxset }, // SI 503
  549. { 0x1039, 0x0496, pIIxget, pIIxset }, // SI 496
  550. { 0x1078, 0x0100, cyrixget, cyrixset } // Cyrix 5530 Legacy
  551. };
  552. typedef struct Slot Slot;
  553. struct Slot {
  554. uchar bus; // Pci bus number
  555. uchar dev; // Pci device number
  556. uchar maps[12]; // Avoid structs! Link and mask.
  557. uchar slot; // Add-in/built-in slot
  558. uchar reserved;
  559. };
  560. typedef struct Router Router;
  561. struct Router {
  562. uchar signature[4]; // Routing table signature
  563. uchar version[2]; // Version number
  564. uchar size[2]; // Total table size
  565. uchar bus; // Interrupt router bus number
  566. uchar devfn; // Router's devfunc
  567. uchar pciirqs[2]; // Exclusive PCI irqs
  568. uchar compat[4]; // Compatible PCI interrupt router
  569. uchar miniport[4]; // Miniport data
  570. uchar reserved[11];
  571. uchar checksum;
  572. };
  573. static ushort pciirqs; // Exclusive PCI irqs
  574. static Bridge *southbridge; // Which southbridge to use.
  575. static void
  576. pcirouting(void)
  577. {
  578. Slot *e;
  579. Router *r;
  580. int size, i, fn, tbdf;
  581. Pcidev *sbpci, *pci;
  582. uchar *p, *m, pin, irq, link;
  583. // Search for PCI interrupt routing table in BIOS
  584. for(p = (uchar *)KADDR(0xf0000); p < (uchar *)KADDR(0xfffff); p += 16)
  585. if(p[0] == '$' && p[1] == 'P' && p[2] == 'I' && p[3] == 'R')
  586. break;
  587. if(p >= (uchar *)KADDR(0xfffff))
  588. return;
  589. r = (Router *)p;
  590. // print("PCI interrupt routing table version %d.%d at %.6uX\n",
  591. // r->version[0], r->version[1], (ulong)r & 0xfffff);
  592. tbdf = (BusPCI << 24)|(r->bus << 16)|(r->devfn << 8);
  593. sbpci = pcimatchtbdf(tbdf);
  594. if(sbpci == nil) {
  595. print("pcirouting: Cannot find south bridge %T\n", tbdf);
  596. return;
  597. }
  598. for(i = 0; i != nelem(southbridges); i++)
  599. if(sbpci->vid == southbridges[i].vid && sbpci->did == southbridges[i].did)
  600. break;
  601. if(i == nelem(southbridges)) {
  602. print("pcirouting: ignoring south bridge %T %.4uX/%.4uX\n", tbdf, sbpci->vid, sbpci->did);
  603. return;
  604. }
  605. southbridge = &southbridges[i];
  606. pciirqs = (r->pciirqs[1] << 8)|r->pciirqs[0];
  607. size = (r->size[1] << 8)|r->size[0];
  608. for(e = (Slot *)&r[1]; (uchar *)e < p + size; e++) {
  609. // print("%.2uX/%.2uX %.2uX: ", e->bus, e->dev, e->slot);
  610. // for (i = 0; i != 4; i++) {
  611. // uchar *m = &e->maps[i * 3];
  612. // print("[%d] %.2uX %.4uX ",
  613. // i, m[0], (m[2] << 8)|m[1]);
  614. // }
  615. // print("\n");
  616. for(fn = 0; fn != 8; fn++) {
  617. tbdf = (BusPCI << 24)|(e->bus << 16)|((e->dev | fn) << 8);
  618. pci = pcimatchtbdf(tbdf);
  619. if(pci == nil)
  620. continue;
  621. pin = pcicfgr8(pci, PciINTP);
  622. if(pin == 0 || pin == 0xff)
  623. continue;
  624. m = &e->maps[(pin - 1) * 3];
  625. link = m[0];
  626. irq = southbridge->get(sbpci, link);
  627. if(irq == 0 || irq == pci->intl)
  628. continue;
  629. if(pci->intl != 0 && pci->intl != 0xFF) {
  630. print("pcirouting: BIOS workaround: %T at pin %d link %d irq %d -> %d\n",
  631. tbdf, pin, link, irq, pci->intl);
  632. southbridge->set(sbpci, link, pci->intl);
  633. continue;
  634. }
  635. print("pcirouting: %T at pin %d link %d irq %d\n", tbdf, pin, link, irq);
  636. pcicfgw8(pci, PciINTL, irq);
  637. pci->intl = irq;
  638. }
  639. }
  640. }
  641. static void
  642. pcicfginit(void)
  643. {
  644. char *p;
  645. Pcidev **list;
  646. ulong mema, ioa;
  647. int bno, n, pcibios;
  648. lock(&pcicfginitlock);
  649. if(pcicfgmode != -1)
  650. goto out;
  651. pcibios = 0;
  652. if(getconf("*nobios"))
  653. nobios = 1;
  654. else if(getconf("*pcibios"))
  655. pcibios = 1;
  656. if(getconf("*nopcirouting"))
  657. nopcirouting = 1;
  658. /*
  659. * Try to determine which PCI configuration mode is implemented.
  660. * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
  661. * a DWORD at 0xCF8 and another at 0xCFC and will pass through
  662. * any non-DWORD accesses as normal I/O cycles. There shouldn't be
  663. * a device behind these addresses so if Mode1 accesses fail try
  664. * for Mode2 (Mode2 is deprecated).
  665. */
  666. if(!pcibios){
  667. /*
  668. * Bits [30:24] of PciADDR must be 0,
  669. * according to the spec.
  670. */
  671. n = inl(PciADDR);
  672. if(!(n & 0x7FF00000)){
  673. outl(PciADDR, 0x80000000);
  674. outb(PciADDR+3, 0);
  675. if(inl(PciADDR) & 0x80000000){
  676. pcicfgmode = 1;
  677. pcimaxdno = 31;
  678. }
  679. }
  680. outl(PciADDR, n);
  681. if(pcicfgmode < 0){
  682. /*
  683. * The 'key' part of PciCSE should be 0.
  684. */
  685. n = inb(PciCSE);
  686. if(!(n & 0xF0)){
  687. outb(PciCSE, 0x0E);
  688. if(inb(PciCSE) == 0x0E){
  689. pcicfgmode = 2;
  690. pcimaxdno = 15;
  691. }
  692. }
  693. outb(PciCSE, n);
  694. }
  695. }
  696. if(pcicfgmode < 0)
  697. goto out;
  698. fmtinstall('T', tbdffmt);
  699. if(p = getconf("*pcimaxbno")){
  700. n = strtoul(p, 0, 0);
  701. if(n < pcimaxbno)
  702. pcimaxbno = n;
  703. }
  704. if(p = getconf("*pcimaxdno")){
  705. n = strtoul(p, 0, 0);
  706. if(n < pcimaxdno)
  707. pcimaxdno = n;
  708. }
  709. list = &pciroot;
  710. for(bno = 0; bno <= pcimaxbno; bno++) {
  711. int sbno = bno;
  712. bno = pcilscan(bno, list);
  713. while(*list)
  714. list = &(*list)->link;
  715. if (sbno == 0) {
  716. Pcidev *pci;
  717. /*
  718. * If we have found a PCI-to-Cardbus bridge, make sure
  719. * it has no valid mappings anymore.
  720. */
  721. pci = pciroot;
  722. while (pci) {
  723. if (pci->ccrb == 6 && pci->ccru == 7) {
  724. ushort bcr;
  725. /* reset the cardbus */
  726. bcr = pcicfgr16(pci, PciBCR);
  727. pcicfgw16(pci, PciBCR, 0x40 | bcr);
  728. delay(50);
  729. }
  730. pci = pci->link;
  731. }
  732. }
  733. }
  734. if(pciroot == nil)
  735. goto out;
  736. if(nobios) {
  737. /*
  738. * Work out how big the top bus is
  739. */
  740. mema = 0;
  741. ioa = 0;
  742. pcibusmap(pciroot, &mema, &ioa, 0);
  743. DBG("Sizes: mem=%8.8lux size=%8.8lux io=%8.8lux\n",
  744. mema, pcimask(mema), ioa);
  745. /*
  746. * Align the windows and map it
  747. */
  748. ioa = 0x1000;
  749. mema = 0x90000000;
  750. pcilog("Mask sizes: mem=%lux io=%lux\n", mema, ioa);
  751. pcibusmap(pciroot, &mema, &ioa, 1);
  752. DBG("Sizes2: mem=%lux io=%lux\n", mema, ioa);
  753. unlock(&pcicfginitlock);
  754. return;
  755. }
  756. if (!nopcirouting)
  757. pcirouting();
  758. out:
  759. unlock(&pcicfginitlock);
  760. if(getconf("*pcihinv"))
  761. pcihinv(nil);
  762. }
  763. static int
  764. pcicfgrw8(int tbdf, int rno, int data, int read)
  765. {
  766. int o, type, x;
  767. if(pcicfgmode == -1)
  768. pcicfginit();
  769. if(BUSBNO(tbdf))
  770. type = 0x01;
  771. else
  772. type = 0x00;
  773. x = -1;
  774. if(BUSDNO(tbdf) > pcimaxdno)
  775. return x;
  776. lock(&pcicfglock);
  777. switch(pcicfgmode){
  778. case 1:
  779. o = rno & 0x03;
  780. rno &= ~0x03;
  781. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  782. if(read)
  783. x = inb(PciDATA+o);
  784. else
  785. outb(PciDATA+o, data);
  786. outl(PciADDR, 0);
  787. break;
  788. case 2:
  789. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  790. outb(PciFORWARD, BUSBNO(tbdf));
  791. if(read)
  792. x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  793. else
  794. outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  795. outb(PciCSE, 0);
  796. break;
  797. }
  798. unlock(&pcicfglock);
  799. return x;
  800. }
  801. int
  802. pcicfgr8(Pcidev* pcidev, int rno)
  803. {
  804. return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
  805. }
  806. void
  807. pcicfgw8(Pcidev* pcidev, int rno, int data)
  808. {
  809. pcicfgrw8(pcidev->tbdf, rno, data, 0);
  810. }
  811. static int
  812. pcicfgrw16(int tbdf, int rno, int data, int read)
  813. {
  814. int o, type, x;
  815. if(pcicfgmode == -1)
  816. pcicfginit();
  817. if(BUSBNO(tbdf))
  818. type = 0x01;
  819. else
  820. type = 0x00;
  821. x = -1;
  822. if(BUSDNO(tbdf) > pcimaxdno)
  823. return x;
  824. lock(&pcicfglock);
  825. switch(pcicfgmode){
  826. case 1:
  827. o = rno & 0x02;
  828. rno &= ~0x03;
  829. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  830. if(read)
  831. x = ins(PciDATA+o);
  832. else
  833. outs(PciDATA+o, data);
  834. outl(PciADDR, 0);
  835. break;
  836. case 2:
  837. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  838. outb(PciFORWARD, BUSBNO(tbdf));
  839. if(read)
  840. x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  841. else
  842. outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  843. outb(PciCSE, 0);
  844. break;
  845. }
  846. unlock(&pcicfglock);
  847. return x;
  848. }
  849. int
  850. pcicfgr16(Pcidev* pcidev, int rno)
  851. {
  852. return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
  853. }
  854. void
  855. pcicfgw16(Pcidev* pcidev, int rno, int data)
  856. {
  857. pcicfgrw16(pcidev->tbdf, rno, data, 0);
  858. }
  859. static int
  860. pcicfgrw32(int tbdf, int rno, int data, int read)
  861. {
  862. int type, x;
  863. if(pcicfgmode == -1)
  864. pcicfginit();
  865. if(BUSBNO(tbdf))
  866. type = 0x01;
  867. else
  868. type = 0x00;
  869. x = -1;
  870. if(BUSDNO(tbdf) > pcimaxdno)
  871. return x;
  872. lock(&pcicfglock);
  873. switch(pcicfgmode){
  874. case 1:
  875. rno &= ~0x03;
  876. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  877. if(read)
  878. x = inl(PciDATA);
  879. else
  880. outl(PciDATA, data);
  881. outl(PciADDR, 0);
  882. break;
  883. case 2:
  884. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  885. outb(PciFORWARD, BUSBNO(tbdf));
  886. if(read)
  887. x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  888. else
  889. outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  890. outb(PciCSE, 0);
  891. break;
  892. }
  893. unlock(&pcicfglock);
  894. return x;
  895. }
  896. int
  897. pcicfgr32(Pcidev* pcidev, int rno)
  898. {
  899. return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
  900. }
  901. void
  902. pcicfgw32(Pcidev* pcidev, int rno, int data)
  903. {
  904. pcicfgrw32(pcidev->tbdf, rno, data, 0);
  905. }
  906. Pcidev*
  907. pcimatch(Pcidev* prev, int vid, int did)
  908. {
  909. if(pcicfgmode == -1)
  910. pcicfginit();
  911. if(prev == nil)
  912. prev = pcilist;
  913. else
  914. prev = prev->list;
  915. while(prev != nil){
  916. if((vid == 0 || prev->vid == vid)
  917. && (did == 0 || prev->did == did))
  918. break;
  919. prev = prev->list;
  920. }
  921. return prev;
  922. }
  923. Pcidev*
  924. pcimatchtbdf(int tbdf)
  925. {
  926. Pcidev *pcidev;
  927. if(pcicfgmode == -1)
  928. pcicfginit();
  929. for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
  930. if(pcidev->tbdf == tbdf)
  931. break;
  932. }
  933. return pcidev;
  934. }
  935. uchar
  936. pciipin(Pcidev *pci, uchar pin)
  937. {
  938. if (pci == nil)
  939. pci = pcilist;
  940. while (pci) {
  941. uchar intl;
  942. if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
  943. return pci->intl;
  944. if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
  945. return intl;
  946. pci = pci->list;
  947. }
  948. return 0;
  949. }
  950. static void
  951. pcilhinv(Pcidev* p)
  952. {
  953. int i;
  954. Pcidev *t;
  955. if(p == nil) {
  956. putstrn(PCICONS.output, PCICONS.ptr);
  957. p = pciroot;
  958. print("bus dev type vid did intl memory\n");
  959. }
  960. for(t = p; t != nil; t = t->link) {
  961. print("%d %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d ",
  962. BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
  963. t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
  964. for(i = 0; i < nelem(p->mem); i++) {
  965. if(t->mem[i].size == 0)
  966. continue;
  967. print("%d:%.8lux %d ", i,
  968. t->mem[i].bar, t->mem[i].size);
  969. }
  970. if(t->ioa.bar || t->ioa.size)
  971. print("ioa:%.8lux %d ", t->ioa.bar, t->ioa.size);
  972. if(t->mema.bar || t->mema.size)
  973. print("mema:%.8lux %d ", t->mema.bar, t->mema.size);
  974. if(t->bridge)
  975. print("->%d", BUSBNO(t->bridge->tbdf));
  976. print("\n");
  977. }
  978. while(p != nil) {
  979. if(p->bridge != nil)
  980. pcilhinv(p->bridge);
  981. p = p->link;
  982. }
  983. }
  984. void
  985. pcihinv(Pcidev* p)
  986. {
  987. if(pcicfgmode == -1)
  988. pcicfginit();
  989. lock(&pcicfginitlock);
  990. pcilhinv(p);
  991. unlock(&pcicfginitlock);
  992. }
  993. void
  994. pcireset(void)
  995. {
  996. Pcidev *p;
  997. if(pcicfgmode == -1)
  998. pcicfginit();
  999. for(p = pcilist; p != nil; p = p->list) {
  1000. /* don't mess with the bridges */
  1001. if(p->ccrb == 0x06)
  1002. continue;
  1003. pciclrbme(p);
  1004. }
  1005. }
  1006. void
  1007. pcisetioe(Pcidev* p)
  1008. {
  1009. p->pcr |= IOen;
  1010. pcicfgw16(p, PciPCR, p->pcr);
  1011. }
  1012. void
  1013. pciclrioe(Pcidev* p)
  1014. {
  1015. p->pcr &= ~IOen;
  1016. pcicfgw16(p, PciPCR, p->pcr);
  1017. }
  1018. void
  1019. pcisetbme(Pcidev* p)
  1020. {
  1021. p->pcr |= MASen;
  1022. pcicfgw16(p, PciPCR, p->pcr);
  1023. }
  1024. void
  1025. pciclrbme(Pcidev* p)
  1026. {
  1027. p->pcr &= ~MASen;
  1028. pcicfgw16(p, PciPCR, p->pcr);
  1029. }
  1030. static int
  1031. pcigetpmrb(Pcidev* p)
  1032. {
  1033. int ptr;
  1034. if(p->pmrb != 0)
  1035. return p->pmrb;
  1036. p->pmrb = -1;
  1037. /*
  1038. * If there are no extended capabilities implemented,
  1039. * (bit 4 in the status register) assume there's no standard
  1040. * power management method.
  1041. * Find the capabilities pointer based on PCI header type.
  1042. */
  1043. if(!(p->pcr & 0x0010))
  1044. return -1;
  1045. switch(pcicfgr8(p, PciHDT)){
  1046. default:
  1047. return -1;
  1048. case 0: /* all other */
  1049. case 1: /* PCI to PCI bridge */
  1050. ptr = 0x34;
  1051. break;
  1052. case 2: /* CardBus bridge */
  1053. ptr = 0x14;
  1054. break;
  1055. }
  1056. ptr = pcicfgr32(p, ptr);
  1057. while(ptr != 0){
  1058. /*
  1059. * Check for validity.
  1060. * Can't be in standard header and must be double
  1061. * word aligned.
  1062. */
  1063. if(ptr < 0x40 || (ptr & ~0xFC))
  1064. return -1;
  1065. if(pcicfgr8(p, ptr) == 0x01){
  1066. p->pmrb = ptr;
  1067. return ptr;
  1068. }
  1069. ptr = pcicfgr8(p, ptr+1);
  1070. }
  1071. return -1;
  1072. }
  1073. int
  1074. pcigetpms(Pcidev* p)
  1075. {
  1076. int pmcsr, ptr;
  1077. if((ptr = pcigetpmrb(p)) == -1)
  1078. return -1;
  1079. /*
  1080. * Power Management Register Block:
  1081. * offset 0: Capability ID
  1082. * 1: next item pointer
  1083. * 2: capabilities
  1084. * 4: control/status
  1085. * 6: bridge support extensions
  1086. * 7: data
  1087. */
  1088. pmcsr = pcicfgr16(p, ptr+4);
  1089. return pmcsr & 0x0003;
  1090. }
  1091. int
  1092. pcisetpms(Pcidev* p, int state)
  1093. {
  1094. int ostate, pmc, pmcsr, ptr;
  1095. if((ptr = pcigetpmrb(p)) == -1)
  1096. return -1;
  1097. pmc = pcicfgr16(p, ptr+2);
  1098. pmcsr = pcicfgr16(p, ptr+4);
  1099. ostate = pmcsr & 0x0003;
  1100. pmcsr &= ~0x0003;
  1101. switch(state){
  1102. default:
  1103. return -1;
  1104. case 0:
  1105. break;
  1106. case 1:
  1107. if(!(pmc & 0x0200))
  1108. return -1;
  1109. break;
  1110. case 2:
  1111. if(!(pmc & 0x0400))
  1112. return -1;
  1113. break;
  1114. case 3:
  1115. break;
  1116. }
  1117. pmcsr |= state;
  1118. pcicfgw16(p, ptr+4, pmcsr);
  1119. return ostate;
  1120. }