pci.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * PCI support code.
  3. * To do:
  4. * initialise bridge mappings if the PCI BIOS didn't.
  5. */
  6. #include "u.h"
  7. #include "lib.h"
  8. #include "mem.h"
  9. #include "dat.h"
  10. #include "fns.h"
  11. #include "io.h"
  12. #include "error.h"
  13. enum { /* configuration mechanism #1 */
  14. PciADDR = 0xCF8, /* CONFIG_ADDRESS */
  15. PciDATA = 0xCFC, /* CONFIG_DATA */
  16. /* configuration mechanism #2 */
  17. PciCSE = 0xCF8, /* configuration space enable */
  18. PciFORWARD = 0xCFA, /* which bus */
  19. MaxFNO = 7,
  20. MaxUBN = 255,
  21. };
  22. enum
  23. { /* command register */
  24. IOen = (1<<0),
  25. MEMen = (1<<1),
  26. MASen = (1<<2),
  27. MemWrInv = (1<<4),
  28. PErrEn = (1<<6),
  29. SErrEn = (1<<8),
  30. };
  31. static Lock pcicfglock;
  32. static Lock pcicfginitlock;
  33. static int pcicfgmode = -1;
  34. static int pcimaxbno = 7;
  35. static int pcimaxdno;
  36. static Pcidev* pciroot;
  37. static Pcidev* pcilist;
  38. static Pcidev* pcitail;
  39. static int pcicfgrw32(int, int, int, int);
  40. static int pcicfgrw8(int, int, int, int);
  41. ulong
  42. pcibarsize(Pcidev *p, int rno)
  43. {
  44. ulong v, size;
  45. v = pcicfgrw32(p->tbdf, rno, 0, 1);
  46. pcicfgrw32(p->tbdf, rno, 0xFFFFFFF0, 0);
  47. size = pcicfgrw32(p->tbdf, rno, 0, 1);
  48. if(v & 1)
  49. size |= 0xFFFF0000;
  50. pcicfgrw32(p->tbdf, rno, v, 0);
  51. return -(size & ~0x0F);
  52. }
  53. int
  54. pciscan(int bno, Pcidev** list)
  55. {
  56. Pcidev *p, *head, *tail;
  57. int dno, fno, i, hdt, l, maxfno, maxubn, rno, sbn, tbdf, ubn;
  58. maxubn = bno;
  59. head = nil;
  60. tail = nil;
  61. for(dno = 0; dno <= pcimaxdno; dno++){
  62. maxfno = 0;
  63. for(fno = 0; fno <= maxfno; fno++){
  64. /*
  65. * For this possible device, form the
  66. * bus+device+function triplet needed to address it
  67. * and try to read the vendor and device ID.
  68. * If successful, allocate a device struct and
  69. * start to fill it in with some useful information
  70. * from the device's configuration space.
  71. */
  72. tbdf = MKBUS(BusPCI, bno, dno, fno);
  73. l = pcicfgrw32(tbdf, PciVID, 0, 1);
  74. if(l == 0xFFFFFFFF || l == 0)
  75. continue;
  76. p = malloc(sizeof(*p));
  77. p->tbdf = tbdf;
  78. p->vid = l;
  79. p->did = l>>16;
  80. if(pcilist != nil)
  81. pcitail->list = p;
  82. else
  83. pcilist = p;
  84. pcitail = p;
  85. p->rid = pcicfgr8(p, PciRID);
  86. p->ccrp = pcicfgr8(p, PciCCRp);
  87. p->ccru = pcicfgr8(p, PciCCRu);
  88. p->ccrb = pcicfgr8(p, PciCCRb);
  89. p->pcr = pcicfgr32(p, PciPCR);
  90. p->intl = pcicfgr8(p, PciINTL);
  91. /*
  92. * If the device is a multi-function device adjust the
  93. * loop count so all possible functions are checked.
  94. */
  95. hdt = pcicfgr8(p, PciHDT);
  96. if(hdt & 0x80)
  97. maxfno = MaxFNO;
  98. /*
  99. * If appropriate, read the base address registers
  100. * and work out the sizes.
  101. */
  102. switch(p->ccrb){
  103. case 0x01: /* mass storage controller */
  104. case 0x02: /* network controller */
  105. case 0x03: /* display controller */
  106. case 0x04: /* multimedia device */
  107. case 0x07: /* simple comm. controllers */
  108. case 0x08: /* base system peripherals */
  109. case 0x09: /* input devices */
  110. case 0x0A: /* docking stations */
  111. case 0x0B: /* processors */
  112. case 0x0C: /* serial bus controllers */
  113. if((hdt & 0x7F) != 0)
  114. break;
  115. rno = PciBAR0 - 4;
  116. for(i = 0; i < nelem(p->mem); i++){
  117. rno += 4;
  118. p->mem[i].bar = pcicfgr32(p, rno);
  119. p->mem[i].size = pcibarsize(p, rno);
  120. }
  121. break;
  122. case 0x00:
  123. case 0x05: /* memory controller */
  124. case 0x06: /* bridge device */
  125. default:
  126. break;
  127. }
  128. if(head != nil)
  129. tail->link = p;
  130. else
  131. head = p;
  132. tail = p;
  133. }
  134. }
  135. *list = head;
  136. for(p = head; p != nil; p = p->link){
  137. /*
  138. * Find PCI-PCI and PCI-Cardbus bridges
  139. * and recursively descend the tree.
  140. */
  141. if(p->ccrb != 0x06 || p->ccru != 0x04)
  142. continue;
  143. /*
  144. * If the secondary or subordinate bus number is not
  145. * initialised try to do what the PCI BIOS should have
  146. * done and fill in the numbers as the tree is descended.
  147. * On the way down the subordinate bus number is set to
  148. * the maximum as it's not known how many buses are behind
  149. * this one; the final value is set on the way back up.
  150. */
  151. ubn = pcicfgr8(p, PciUBN);
  152. sbn = pcicfgr8(p, PciSBN);
  153. if(sbn == 0 || ubn == 0){
  154. sbn = maxubn+1;
  155. /*
  156. * Make sure memory, I/O and master enables are
  157. * off, set the primary, secondary and subordinate
  158. * bus numbers and clear the secondary status before
  159. * attempting to scan the secondary bus.
  160. *
  161. * Initialisation of the bridge should be done here.
  162. */
  163. pcicfgw32(p, PciPCR, 0xFFFF0000);
  164. l = (MaxUBN<<16)|(sbn<<8)|bno;
  165. pcicfgw32(p, PciPBN, l);
  166. pcicfgw16(p, PciSPSR, 0xFFFF);
  167. maxubn = pciscan(sbn, &p->bridge);
  168. l = (maxubn<<16)|(sbn<<8)|bno;
  169. pcicfgw32(p, PciPBN, l);
  170. }
  171. else{
  172. /*
  173. * You can't go back.
  174. * This shouldn't be possible, but the
  175. * Iwill DK8-HTX seems to have subordinate
  176. * bus numbers which get smaller on the
  177. * way down. Need to look more closely at
  178. * this.
  179. */
  180. if(ubn > maxubn)
  181. maxubn = ubn;
  182. pciscan(sbn, &p->bridge);
  183. }
  184. }
  185. return maxubn;
  186. }
  187. static uchar
  188. null_link(Pcidev *, uchar )
  189. {
  190. return 0;
  191. }
  192. static void
  193. null_init(Pcidev *, uchar , uchar )
  194. {
  195. }
  196. static uchar
  197. pIIx_link(Pcidev *router, uchar link)
  198. {
  199. uchar pirq;
  200. /* link should be 0x60, 0x61, 0x62, 0x63 */
  201. pirq = pcicfgr8(router, link);
  202. return (pirq < 16)? pirq: 0;
  203. }
  204. static void
  205. pIIx_init(Pcidev *router, uchar link, uchar irq)
  206. {
  207. pcicfgw8(router, link, irq);
  208. }
  209. static uchar
  210. via_link(Pcidev *router, uchar link)
  211. {
  212. uchar pirq;
  213. /* link should be 1, 2, 3, 5 */
  214. pirq = (link < 6)? pcicfgr8(router, 0x55 + (link>>1)): 0;
  215. return (link & 1)? (pirq >> 4): (pirq & 15);
  216. }
  217. static void
  218. via_init(Pcidev *router, uchar link, uchar irq)
  219. {
  220. uchar pirq;
  221. pirq = pcicfgr8(router, 0x55 + (link >> 1));
  222. pirq &= (link & 1)? 0x0f: 0xf0;
  223. pirq |= (link & 1)? (irq << 4): (irq & 15);
  224. pcicfgw8(router, 0x55 + (link>>1), pirq);
  225. }
  226. static uchar
  227. opti_link(Pcidev *router, uchar link)
  228. {
  229. uchar pirq = 0;
  230. /* link should be 0x02, 0x12, 0x22, 0x32 */
  231. if ((link & 0xcf) == 0x02)
  232. pirq = pcicfgr8(router, 0xb8 + (link >> 5));
  233. return (link & 0x10)? (pirq >> 4): (pirq & 15);
  234. }
  235. static void
  236. opti_init(Pcidev *router, uchar link, uchar irq)
  237. {
  238. uchar pirq;
  239. pirq = pcicfgr8(router, 0xb8 + (link >> 5));
  240. pirq &= (link & 0x10)? 0x0f : 0xf0;
  241. pirq |= (link & 0x10)? (irq << 4): (irq & 15);
  242. pcicfgw8(router, 0xb8 + (link >> 5), pirq);
  243. }
  244. static uchar
  245. ali_link(Pcidev *router, uchar link)
  246. {
  247. /* No, you're not dreaming */
  248. static const uchar map[] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
  249. uchar pirq;
  250. /* link should be 0x01..0x08 */
  251. pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
  252. return (link & 1)? map[pirq&15]: map[pirq>>4];
  253. }
  254. static void
  255. ali_init(Pcidev *router, uchar link, uchar irq)
  256. {
  257. /* Inverse of map in ali_link */
  258. static const uchar map[] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
  259. uchar pirq;
  260. pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
  261. pirq &= (link & 1)? 0x0f: 0xf0;
  262. pirq |= (link & 1)? (map[irq] << 4): (map[irq] & 15);
  263. pcicfgw8(router, 0x48 + ((link-1)>>1), pirq);
  264. }
  265. static uchar
  266. cyrix_link(Pcidev *router, uchar link)
  267. {
  268. uchar pirq;
  269. /* link should be 1, 2, 3, 4 */
  270. pirq = pcicfgr8(router, 0x5c + ((link-1)>>1));
  271. return ((link & 1)? pirq >> 4: pirq & 15);
  272. }
  273. static void
  274. cyrix_init(Pcidev *router, uchar link, uchar irq)
  275. {
  276. uchar pirq;
  277. pirq = pcicfgr8(router, 0x5c + (link>>1));
  278. pirq &= (link & 1)? 0x0f: 0xf0;
  279. pirq |= (link & 1)? (irq << 4): (irq & 15);
  280. pcicfgw8(router, 0x5c + (link>>1), pirq);
  281. }
  282. typedef struct {
  283. ushort sb_vid, sb_did;
  284. uchar (*sb_translate)(Pcidev *, uchar);
  285. void (*sb_initialize)(Pcidev *, uchar, uchar);
  286. } bridge_t;
  287. static bridge_t southbridges[] = {
  288. { 0x8086, 0x122e, pIIx_link, pIIx_init }, // Intel 82371FB
  289. { 0x8086, 0x1234, pIIx_link, pIIx_init }, // Intel 82371MX
  290. { 0x8086, 0x7000, pIIx_link, pIIx_init }, // Intel 82371SB
  291. { 0x8086, 0x7110, pIIx_link, pIIx_init }, // Intel 82371AB
  292. { 0x8086, 0x7198, pIIx_link, pIIx_init }, // Intel 82443MX (fn 1)
  293. { 0x8086, 0x2410, pIIx_link, pIIx_init }, // Intel 82801AA
  294. { 0x8086, 0x2420, pIIx_link, pIIx_init }, // Intel 82801AB
  295. { 0x8086, 0x2440, pIIx_link, pIIx_init }, // Intel 82801BA
  296. { 0x8086, 0x244c, pIIx_link, pIIx_init }, // Intel 82801BAM
  297. { 0x8086, 0x2480, pIIx_link, pIIx_init }, // Intel 82801CA
  298. { 0x8086, 0x248c, pIIx_link, pIIx_init }, // Intel 82801CAM
  299. { 0x8086, 0x24c0, pIIx_link, pIIx_init }, // Intel 82801DBL
  300. { 0x8086, 0x24cc, pIIx_link, pIIx_init }, // Intel 82801DBM
  301. { 0x8086, 0x24d0, pIIx_link, pIIx_init }, // Intel 82801EB
  302. { 0x8086, 0x2640, pIIx_link, pIIx_init }, // Intel 82801FB
  303. { 0x8086, 0x27b8, pIIx_link, pIIx_init }, // Intel 82801GB
  304. { 0x8086, 0x27b9, pIIx_link, pIIx_init }, // Intel 82801GBM
  305. { 0x1106, 0x0586, via_link, via_init }, // Viatech 82C586
  306. { 0x1106, 0x0596, via_link, via_init }, // Viatech 82C596
  307. { 0x1106, 0x0686, via_link, via_init }, // Viatech 82C686
  308. { 0x1106, 0x3227, via_link, via_init }, // Viatech VT8237
  309. { 0x1045, 0xc700, opti_link, opti_init }, // Opti 82C700
  310. { 0x10b9, 0x1533, ali_link, ali_init }, // Al M1533
  311. { 0x1039, 0x0008, pIIx_link, pIIx_init }, // SI 503
  312. { 0x1039, 0x0496, pIIx_link, pIIx_init }, // SI 496
  313. { 0x1078, 0x0100, cyrix_link, cyrix_init }, // Cyrix 5530 Legacy
  314. { 0x1002, 0x4377, nil, nil }, // ATI Radeon Xpress 200M
  315. { 0x1002, 0x4372, nil, nil }, // ATI SB400
  316. { 0x1022, 0x746B, nil, nil }, // AMD 8111
  317. { 0x10DE, 0x00D1, nil, nil }, // NVIDIA nForce 3
  318. { 0x1166, 0x0200, nil, nil }, // ServerWorks ServerSet III LE
  319. };
  320. typedef struct {
  321. uchar e_bus; // Pci bus number
  322. uchar e_dev; // Pci device number
  323. uchar e_maps[12]; // Avoid structs! Link and mask.
  324. uchar e_slot; // Add-in/built-in slot
  325. uchar e_reserved;
  326. } slot_t;
  327. typedef struct {
  328. uchar rt_signature[4]; // Routing table signature
  329. uchar rt_version[2]; // Version number
  330. uchar rt_size[2]; // Total table size
  331. uchar rt_bus; // Interrupt router bus number
  332. uchar rt_devfn; // Router's devfunc
  333. uchar rt_pciirqs[2]; // Exclusive PCI irqs
  334. uchar rt_compat[4]; // Compatible PCI interrupt router
  335. uchar rt_miniport[4]; // Miniport data
  336. uchar rt_reserved[11];
  337. uchar rt_checksum;
  338. } router_t;
  339. static ushort pciirqs; // Exclusive PCI irqs
  340. static bridge_t *southbridge; // Which southbridge to use.
  341. static void
  342. pcirouting(void)
  343. {
  344. uchar *p, pin, irq;
  345. ulong tbdf, vdid;
  346. ushort vid, did;
  347. router_t *r;
  348. slot_t *e;
  349. int size, i, fn;
  350. Pcidev *sbpci, *pci;
  351. // Peek in the BIOS
  352. for (p = (uchar *)KADDR(0xf0000); p < (uchar *)KADDR(0xfffff); p += 16)
  353. if (p[0] == '$' && p[1] == 'P' && p[2] == 'I' && p[3] == 'R')
  354. break;
  355. if (p >= (uchar *)KADDR(0xfffff))
  356. return;
  357. r = (router_t *)p;
  358. // print("PCI interrupt routing table version %d.%d at %.6uX\n",
  359. // r->rt_version[0], r->rt_version[1], (ulong)r & 0xfffff);
  360. tbdf = (BusPCI << 24)|(r->rt_bus << 16)|(r->rt_devfn << 8);
  361. vdid = pcicfgrw32(tbdf, PciVID, 0, 1);
  362. vid = vdid;
  363. did = vdid >> 16;
  364. for (i = 0; i != nelem(southbridges); i++)
  365. if (vid == southbridges[i].sb_vid && did == southbridges[i].sb_did)
  366. break;
  367. if (i == nelem(southbridges)) {
  368. print("pcirouting: South bridge %.4uX, %.4uX not found\n", vid, did);
  369. return;
  370. }
  371. southbridge = &southbridges[i];
  372. if ((sbpci = pcimatch(nil, vid, did)) == nil) {
  373. print("pcirouting: Cannot match south bridge %.4uX, %.4uX\n",
  374. vid, did);
  375. return;
  376. }
  377. pciirqs = (r->rt_pciirqs[1] << 8)|r->rt_pciirqs[0];
  378. size = (r->rt_size[1] << 8)|r->rt_size[0];
  379. for (e = (slot_t *)&r[1]; (uchar *)e < p + size; e++) {
  380. // print("%.2uX/%.2uX %.2uX: ", e->e_bus, e->e_dev, e->e_slot);
  381. // for (i = 0; i != 4; i++) {
  382. // uchar *m = &e->e_maps[i * 3];
  383. // print("[%d] %.2uX %.4uX ",
  384. // i, m[0], (m[2] << 8)|m[1]);
  385. // }
  386. // print("\n");
  387. for (fn = 0; fn != 8; fn++) {
  388. uchar *m;
  389. // Retrieve the did and vid through the devfn before
  390. // obtaining the Pcidev structure.
  391. tbdf = (BusPCI << 24)|(e->e_bus << 16)|((e->e_dev | fn) << 8);
  392. vdid = pcicfgrw32(tbdf, PciVID, 0, 1);
  393. if (vdid == 0xFFFFFFFF || vdid == 0)
  394. continue;
  395. vid = vdid;
  396. did = vdid >> 16;
  397. pci = nil;
  398. while ((pci = pcimatch(pci, vid, did)) != nil) {
  399. if (pci->intl != 0 && pci->intl != 0xFF)
  400. continue;
  401. pin = pcicfgr8(pci, PciINTP);
  402. if (pin == 0 || pin == 0xff)
  403. continue;
  404. m = &e->e_maps[(pin - 1) * 3];
  405. irq = southbridge->sb_translate(sbpci, m[0]);
  406. if (irq) {
  407. print("pcirouting: %.4uX/%.4uX at pin %d irq %d\n",
  408. vid, did, pin, irq);
  409. pcicfgw8(pci, PciINTL, irq);
  410. pci->intl = irq;
  411. }
  412. }
  413. }
  414. }
  415. }
  416. static void
  417. pcicfginit(void)
  418. {
  419. char *p;
  420. int bno, n;
  421. Pcidev **list;
  422. lock(&pcicfginitlock);
  423. if(pcicfgmode != -1)
  424. goto out;
  425. /*
  426. * Try to determine which PCI configuration mode is implemented.
  427. * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
  428. * a DWORD at 0xCF8 and another at 0xCFC and will pass through
  429. * any non-DWORD accesses as normal I/O cycles. There shouldn't be
  430. * a device behind these addresses so if Mode1 accesses fail try
  431. * for Mode2 (Mode2 is deprecated).
  432. */
  433. /*
  434. * Bits [30:24] of PciADDR must be 0,
  435. * according to the spec.
  436. */
  437. n = inl(PciADDR);
  438. if(!(n & 0x7FF00000)){
  439. outl(PciADDR, 0x80000000);
  440. outb(PciADDR+3, 0);
  441. if(inl(PciADDR) & 0x80000000){
  442. pcicfgmode = 1;
  443. pcimaxdno = 31;
  444. }
  445. }
  446. outl(PciADDR, n);
  447. if(pcicfgmode < 0){
  448. /*
  449. * The 'key' part of PciCSE should be 0.
  450. */
  451. n = inb(PciCSE);
  452. if(!(n & 0xF0)){
  453. outb(PciCSE, 0x0E);
  454. if(inb(PciCSE) == 0x0E){
  455. pcicfgmode = 2;
  456. pcimaxdno = 15;
  457. }
  458. }
  459. outb(PciCSE, n);
  460. }
  461. if(pcicfgmode < 0)
  462. goto out;
  463. if(p = getconf("*pcimaxbno"))
  464. pcimaxbno = strtoul(p, 0, 0);
  465. if(p = getconf("*pcimaxdno"))
  466. pcimaxdno = strtoul(p, 0, 0);
  467. list = &pciroot;
  468. for(bno = 0; bno <= pcimaxbno; bno++) {
  469. bno = pciscan(bno, list);
  470. while(*list)
  471. list = &(*list)->link;
  472. }
  473. pcirouting();
  474. out:
  475. unlock(&pcicfginitlock);
  476. if(getconf("*pcihinv"))
  477. pcihinv(nil);
  478. }
  479. static int
  480. pcicfgrw8(int tbdf, int rno, int data, int read)
  481. {
  482. int o, type, x;
  483. if(pcicfgmode == -1)
  484. pcicfginit();
  485. if(BUSBNO(tbdf))
  486. type = 0x01;
  487. else
  488. type = 0x00;
  489. x = -1;
  490. if(BUSDNO(tbdf) > pcimaxdno)
  491. return x;
  492. lock(&pcicfglock);
  493. switch(pcicfgmode){
  494. case 1:
  495. o = rno & 0x03;
  496. rno &= ~0x03;
  497. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  498. if(read)
  499. x = inb(PciDATA+o);
  500. else
  501. outb(PciDATA+o, data);
  502. outl(PciADDR, 0);
  503. break;
  504. case 2:
  505. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  506. outb(PciFORWARD, BUSBNO(tbdf));
  507. if(read)
  508. x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  509. else
  510. outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  511. outb(PciCSE, 0);
  512. break;
  513. }
  514. unlock(&pcicfglock);
  515. return x;
  516. }
  517. int
  518. pcicfgr8(Pcidev* pcidev, int rno)
  519. {
  520. return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
  521. }
  522. void
  523. pcicfgw8(Pcidev* pcidev, int rno, int data)
  524. {
  525. pcicfgrw8(pcidev->tbdf, rno, data, 0);
  526. }
  527. static int
  528. pcicfgrw16(int tbdf, int rno, int data, int read)
  529. {
  530. int o, type, x;
  531. if(pcicfgmode == -1)
  532. pcicfginit();
  533. if(BUSBNO(tbdf))
  534. type = 0x01;
  535. else
  536. type = 0x00;
  537. x = -1;
  538. if(BUSDNO(tbdf) > pcimaxdno)
  539. return x;
  540. lock(&pcicfglock);
  541. switch(pcicfgmode){
  542. case 1:
  543. o = rno & 0x02;
  544. rno &= ~0x03;
  545. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  546. if(read)
  547. x = ins(PciDATA+o);
  548. else
  549. outs(PciDATA+o, data);
  550. outl(PciADDR, 0);
  551. break;
  552. case 2:
  553. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  554. outb(PciFORWARD, BUSBNO(tbdf));
  555. if(read)
  556. x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  557. else
  558. outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  559. outb(PciCSE, 0);
  560. break;
  561. }
  562. unlock(&pcicfglock);
  563. return x;
  564. }
  565. int
  566. pcicfgr16(Pcidev* pcidev, int rno)
  567. {
  568. return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
  569. }
  570. void
  571. pcicfgw16(Pcidev* pcidev, int rno, int data)
  572. {
  573. pcicfgrw16(pcidev->tbdf, rno, data, 0);
  574. }
  575. static int
  576. pcicfgrw32(int tbdf, int rno, int data, int read)
  577. {
  578. int type, x;
  579. if(pcicfgmode == -1)
  580. pcicfginit();
  581. if(BUSBNO(tbdf))
  582. type = 0x01;
  583. else
  584. type = 0x00;
  585. x = -1;
  586. if(BUSDNO(tbdf) > pcimaxdno)
  587. return x;
  588. lock(&pcicfglock);
  589. switch(pcicfgmode){
  590. case 1:
  591. rno &= ~0x03;
  592. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  593. if(read)
  594. x = inl(PciDATA);
  595. else
  596. outl(PciDATA, data);
  597. outl(PciADDR, 0);
  598. break;
  599. case 2:
  600. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  601. outb(PciFORWARD, BUSBNO(tbdf));
  602. if(read)
  603. x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  604. else
  605. outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  606. outb(PciCSE, 0);
  607. break;
  608. }
  609. unlock(&pcicfglock);
  610. return x;
  611. }
  612. int
  613. pcicfgr32(Pcidev* pcidev, int rno)
  614. {
  615. return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
  616. }
  617. void
  618. pcicfgw32(Pcidev* pcidev, int rno, int data)
  619. {
  620. pcicfgrw32(pcidev->tbdf, rno, data, 0);
  621. }
  622. Pcidev*
  623. pcimatch(Pcidev* prev, int vid, int did)
  624. {
  625. if(pcicfgmode == -1)
  626. pcicfginit();
  627. if(prev == nil)
  628. prev = pcilist;
  629. else
  630. prev = prev->list;
  631. while(prev != nil) {
  632. if((vid == 0 || prev->vid == vid)
  633. && (did == 0 || prev->did == did))
  634. break;
  635. prev = prev->list;
  636. }
  637. return prev;
  638. }
  639. uchar
  640. pciipin(Pcidev *pci, uchar pin)
  641. {
  642. if (pci == nil)
  643. pci = pcilist;
  644. while (pci) {
  645. uchar intl;
  646. if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
  647. return pci->intl;
  648. if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
  649. return intl;
  650. pci = pci->list;
  651. }
  652. return 0;
  653. }
  654. static ushort
  655. pciimask(Pcidev *pci)
  656. {
  657. ushort imask;
  658. imask = 0;
  659. while (pci) {
  660. if (pcicfgr8(pci, PciINTP) && pci->intl < 16)
  661. imask |= 1 << pci->intl;
  662. if (pci->bridge)
  663. imask |= pciimask(pci->bridge);
  664. pci = pci->list;
  665. }
  666. return imask;
  667. }
  668. uchar
  669. pciintl(Pcidev *pci)
  670. {
  671. ushort imask;
  672. int i;
  673. if (pci == nil)
  674. pci = pcilist;
  675. imask = pciimask(pci) | 1;
  676. for (i = 0; i != 16; i++)
  677. if ((imask & (1 << i)) == 0)
  678. return i;
  679. return 0;
  680. }
  681. void
  682. pcihinv(Pcidev* p)
  683. {
  684. int i;
  685. Pcidev *t;
  686. if(pcicfgmode == -1)
  687. pcicfginit();
  688. if(p == nil) {
  689. p = pciroot;
  690. print("bus dev type vid did intl memory\n");
  691. }
  692. for(t = p; t != nil; t = t->link) {
  693. print("%d %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d ",
  694. BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
  695. t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
  696. for(i = 0; i < nelem(p->mem); i++) {
  697. if(t->mem[i].size == 0)
  698. continue;
  699. print("%d:%.8lux %d ", i,
  700. t->mem[i].bar, t->mem[i].size);
  701. }
  702. print("\n");
  703. }
  704. while(p != nil) {
  705. if(p->bridge != nil)
  706. pcihinv(p->bridge);
  707. p = p->link;
  708. }
  709. }
  710. void
  711. pcireset(void)
  712. {
  713. Pcidev *p;
  714. int pcr;
  715. if(pcicfgmode == -1)
  716. pcicfginit();
  717. for(p = pcilist; p != nil; p = p->list){
  718. pcr = pcicfgr16(p, PciPSR);
  719. pcicfgw16(p, PciPSR, pcr & ~0x04);
  720. }
  721. }
  722. void
  723. pcisetioe(Pcidev* p)
  724. {
  725. p->pcr |= IOen;
  726. pcicfgw16(p, PciPCR, p->pcr);
  727. }
  728. void
  729. pciclrioe(Pcidev* p)
  730. {
  731. p->pcr &= ~IOen;
  732. pcicfgw16(p, PciPCR, p->pcr);
  733. }
  734. void
  735. pcisetbme(Pcidev* p)
  736. {
  737. p->pcr |= MASen;
  738. pcicfgw16(p, PciPCR, p->pcr);
  739. }
  740. void
  741. pciclrbme(Pcidev* p)
  742. {
  743. p->pcr &= ~MASen;
  744. pcicfgw16(p, PciPCR, p->pcr);
  745. }
  746. void
  747. pcisetmwi(Pcidev* p)
  748. {
  749. p->pcr |= MemWrInv;
  750. pcicfgw16(p, PciPCR, p->pcr);
  751. }
  752. void
  753. pciclrmwi(Pcidev* p)
  754. {
  755. p->pcr &= ~MemWrInv;
  756. pcicfgw16(p, PciPCR, p->pcr);
  757. }
  758. static int
  759. pcigetpmrb(Pcidev* p)
  760. {
  761. int ptr;
  762. if(p->pmrb != 0)
  763. return p->pmrb;
  764. p->pmrb = -1;
  765. /*
  766. * If there are no extended capabilities implemented,
  767. * (bit 4 in the status register) assume there's no standard
  768. * power management method.
  769. * Find the capabilities pointer based on PCI header type.
  770. */
  771. if(!(pcicfgr16(p, PciPSR) & 0x0010))
  772. return -1;
  773. switch(pcicfgr8(p, PciHDT)){
  774. default:
  775. return -1;
  776. case 0: /* all other */
  777. case 1: /* PCI to PCI bridge */
  778. ptr = 0x34;
  779. break;
  780. case 2: /* CardBus bridge */
  781. ptr = 0x14;
  782. break;
  783. }
  784. ptr = pcicfgr32(p, ptr);
  785. while(ptr != 0){
  786. /*
  787. * Check for validity.
  788. * Can't be in standard header and must be double
  789. * word aligned.
  790. */
  791. if(ptr < 0x40 || (ptr & ~0xFC))
  792. return -1;
  793. if(pcicfgr8(p, ptr) == 0x01){
  794. p->pmrb = ptr;
  795. return ptr;
  796. }
  797. ptr = pcicfgr8(p, ptr+1);
  798. }
  799. return -1;
  800. }
  801. int
  802. pcigetpms(Pcidev* p)
  803. {
  804. int pmcsr, ptr;
  805. if((ptr = pcigetpmrb(p)) == -1)
  806. return -1;
  807. /*
  808. * Power Management Register Block:
  809. * offset 0: Capability ID
  810. * 1: next item pointer
  811. * 2: capabilities
  812. * 4: control/status
  813. * 6: bridge support extensions
  814. * 7: data
  815. */
  816. pmcsr = pcicfgr16(p, ptr+4);
  817. return pmcsr & 0x0003;
  818. }
  819. int
  820. pcisetpms(Pcidev* p, int state)
  821. {
  822. int ostate, pmc, pmcsr, ptr;
  823. if((ptr = pcigetpmrb(p)) == -1)
  824. return -1;
  825. pmc = pcicfgr16(p, ptr+2);
  826. pmcsr = pcicfgr16(p, ptr+4);
  827. ostate = pmcsr & 0x0003;
  828. pmcsr &= ~0x0003;
  829. switch(state){
  830. default:
  831. return -1;
  832. case 0:
  833. break;
  834. case 1:
  835. if(!(pmc & 0x0200))
  836. return -1;
  837. break;
  838. case 2:
  839. if(!(pmc & 0x0400))
  840. return -1;
  841. break;
  842. case 3:
  843. break;
  844. }
  845. pmcsr |= state;
  846. pcicfgw16(p, ptr+4, pmcsr);
  847. return ostate;
  848. }