pci.c 30 KB

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