pci.c 26 KB

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