pci.c 29 KB

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