pci.c 30 KB

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