pci.c 29 KB

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