pci.c 29 KB

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