pci.c 29 KB

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