pci.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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, 0x27b9, pIIx_link, pIIx_init }, // Intel 82801GBM
  304. { 0x1106, 0x0586, via_link, via_init }, // Viatech 82C586
  305. { 0x1106, 0x0596, via_link, via_init }, // Viatech 82C596
  306. { 0x1106, 0x0686, via_link, via_init }, // Viatech 82C686
  307. { 0x1106, 0x3227, via_link, via_init }, // Viatech VT8237
  308. { 0x1045, 0xc700, opti_link, opti_init }, // Opti 82C700
  309. { 0x10b9, 0x1533, ali_link, ali_init }, // Al M1533
  310. { 0x1039, 0x0008, pIIx_link, pIIx_init }, // SI 503
  311. { 0x1039, 0x0496, pIIx_link, pIIx_init }, // SI 496
  312. { 0x1078, 0x0100, cyrix_link, cyrix_init }, // Cyrix 5530 Legacy
  313. { 0x1002, 0x4377, nil, nil }, // ATI Radeon Xpress 200M
  314. { 0x1002, 0x4372, nil, nil }, // ATI SB400
  315. { 0x1022, 0x746B, nil, nil }, // AMD 8111
  316. { 0x10DE, 0x00D1, nil, nil }, // NVIDIA nForce 3
  317. { 0x1166, 0x0200, nil, nil }, // ServerWorks ServerSet III LE
  318. };
  319. typedef struct {
  320. uchar e_bus; // Pci bus number
  321. uchar e_dev; // Pci device number
  322. uchar e_maps[12]; // Avoid structs! Link and mask.
  323. uchar e_slot; // Add-in/built-in slot
  324. uchar e_reserved;
  325. } slot_t;
  326. typedef struct {
  327. uchar rt_signature[4]; // Routing table signature
  328. uchar rt_version[2]; // Version number
  329. uchar rt_size[2]; // Total table size
  330. uchar rt_bus; // Interrupt router bus number
  331. uchar rt_devfn; // Router's devfunc
  332. uchar rt_pciirqs[2]; // Exclusive PCI irqs
  333. uchar rt_compat[4]; // Compatible PCI interrupt router
  334. uchar rt_miniport[4]; // Miniport data
  335. uchar rt_reserved[11];
  336. uchar rt_checksum;
  337. } router_t;
  338. static ushort pciirqs; // Exclusive PCI irqs
  339. static bridge_t *southbridge; // Which southbridge to use.
  340. static void
  341. pcirouting(void)
  342. {
  343. uchar *p, pin, irq;
  344. ulong tbdf, vdid;
  345. ushort vid, did;
  346. router_t *r;
  347. slot_t *e;
  348. int size, i, fn;
  349. Pcidev *sbpci, *pci;
  350. // Peek in the BIOS
  351. for (p = (uchar *)KADDR(0xf0000); p < (uchar *)KADDR(0xfffff); p += 16)
  352. if (p[0] == '$' && p[1] == 'P' && p[2] == 'I' && p[3] == 'R')
  353. break;
  354. if (p >= (uchar *)KADDR(0xfffff))
  355. return;
  356. r = (router_t *)p;
  357. // print("PCI interrupt routing table version %d.%d at %.6uX\n",
  358. // r->rt_version[0], r->rt_version[1], (ulong)r & 0xfffff);
  359. tbdf = (BusPCI << 24)|(r->rt_bus << 16)|(r->rt_devfn << 8);
  360. vdid = pcicfgrw32(tbdf, PciVID, 0, 1);
  361. vid = vdid;
  362. did = vdid >> 16;
  363. for (i = 0; i != nelem(southbridges); i++)
  364. if (vid == southbridges[i].sb_vid && did == southbridges[i].sb_did)
  365. break;
  366. if (i == nelem(southbridges)) {
  367. print("pcirouting: South bridge %.4uX, %.4uX not found\n", vid, did);
  368. return;
  369. }
  370. southbridge = &southbridges[i];
  371. if ((sbpci = pcimatch(nil, vid, did)) == nil) {
  372. print("pcirouting: Cannot match south bridge %.4uX, %.4uX\n",
  373. vid, did);
  374. return;
  375. }
  376. pciirqs = (r->rt_pciirqs[1] << 8)|r->rt_pciirqs[0];
  377. size = (r->rt_size[1] << 8)|r->rt_size[0];
  378. for (e = (slot_t *)&r[1]; (uchar *)e < p + size; e++) {
  379. // print("%.2uX/%.2uX %.2uX: ", e->e_bus, e->e_dev, e->e_slot);
  380. // for (i = 0; i != 4; i++) {
  381. // uchar *m = &e->e_maps[i * 3];
  382. // print("[%d] %.2uX %.4uX ",
  383. // i, m[0], (m[2] << 8)|m[1]);
  384. // }
  385. // print("\n");
  386. for (fn = 0; fn != 8; fn++) {
  387. uchar *m;
  388. // Retrieve the did and vid through the devfn before
  389. // obtaining the Pcidev structure.
  390. tbdf = (BusPCI << 24)|(e->e_bus << 16)|((e->e_dev | fn) << 8);
  391. vdid = pcicfgrw32(tbdf, PciVID, 0, 1);
  392. if (vdid == 0xFFFFFFFF || vdid == 0)
  393. continue;
  394. vid = vdid;
  395. did = vdid >> 16;
  396. pci = nil;
  397. while ((pci = pcimatch(pci, vid, did)) != nil) {
  398. if (pci->intl != 0 && pci->intl != 0xFF)
  399. continue;
  400. pin = pcicfgr8(pci, PciINTP);
  401. if (pin == 0 || pin == 0xff)
  402. continue;
  403. m = &e->e_maps[(pin - 1) * 3];
  404. irq = southbridge->sb_translate(sbpci, m[0]);
  405. if (irq) {
  406. print("pcirouting: %.4uX/%.4uX at pin %d irq %d\n",
  407. vid, did, pin, irq);
  408. pcicfgw8(pci, PciINTL, irq);
  409. pci->intl = irq;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. static void
  416. pcicfginit(void)
  417. {
  418. char *p;
  419. int bno, n;
  420. Pcidev **list;
  421. lock(&pcicfginitlock);
  422. if(pcicfgmode != -1)
  423. goto out;
  424. /*
  425. * Try to determine which PCI configuration mode is implemented.
  426. * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
  427. * a DWORD at 0xCF8 and another at 0xCFC and will pass through
  428. * any non-DWORD accesses as normal I/O cycles. There shouldn't be
  429. * a device behind these addresses so if Mode1 accesses fail try
  430. * for Mode2 (Mode2 is deprecated).
  431. */
  432. /*
  433. * Bits [30:24] of PciADDR must be 0,
  434. * according to the spec.
  435. */
  436. n = inl(PciADDR);
  437. if(!(n & 0x7FF00000)){
  438. outl(PciADDR, 0x80000000);
  439. outb(PciADDR+3, 0);
  440. if(inl(PciADDR) & 0x80000000){
  441. pcicfgmode = 1;
  442. pcimaxdno = 31;
  443. }
  444. }
  445. outl(PciADDR, n);
  446. if(pcicfgmode < 0){
  447. /*
  448. * The 'key' part of PciCSE should be 0.
  449. */
  450. n = inb(PciCSE);
  451. if(!(n & 0xF0)){
  452. outb(PciCSE, 0x0E);
  453. if(inb(PciCSE) == 0x0E){
  454. pcicfgmode = 2;
  455. pcimaxdno = 15;
  456. }
  457. }
  458. outb(PciCSE, n);
  459. }
  460. if(pcicfgmode < 0)
  461. goto out;
  462. if(p = getconf("*pcimaxbno"))
  463. pcimaxbno = strtoul(p, 0, 0);
  464. if(p = getconf("*pcimaxdno"))
  465. pcimaxdno = strtoul(p, 0, 0);
  466. list = &pciroot;
  467. for(bno = 0; bno <= pcimaxbno; bno++) {
  468. bno = pciscan(bno, list);
  469. while(*list)
  470. list = &(*list)->link;
  471. }
  472. pcirouting();
  473. out:
  474. unlock(&pcicfginitlock);
  475. if(getconf("*pcihinv"))
  476. pcihinv(nil);
  477. }
  478. static int
  479. pcicfgrw8(int tbdf, int rno, int data, int read)
  480. {
  481. int o, type, x;
  482. if(pcicfgmode == -1)
  483. pcicfginit();
  484. if(BUSBNO(tbdf))
  485. type = 0x01;
  486. else
  487. type = 0x00;
  488. x = -1;
  489. if(BUSDNO(tbdf) > pcimaxdno)
  490. return x;
  491. lock(&pcicfglock);
  492. switch(pcicfgmode){
  493. case 1:
  494. o = rno & 0x03;
  495. rno &= ~0x03;
  496. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  497. if(read)
  498. x = inb(PciDATA+o);
  499. else
  500. outb(PciDATA+o, data);
  501. outl(PciADDR, 0);
  502. break;
  503. case 2:
  504. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  505. outb(PciFORWARD, BUSBNO(tbdf));
  506. if(read)
  507. x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  508. else
  509. outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  510. outb(PciCSE, 0);
  511. break;
  512. }
  513. unlock(&pcicfglock);
  514. return x;
  515. }
  516. int
  517. pcicfgr8(Pcidev* pcidev, int rno)
  518. {
  519. return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
  520. }
  521. void
  522. pcicfgw8(Pcidev* pcidev, int rno, int data)
  523. {
  524. pcicfgrw8(pcidev->tbdf, rno, data, 0);
  525. }
  526. static int
  527. pcicfgrw16(int tbdf, int rno, int data, int read)
  528. {
  529. int o, type, x;
  530. if(pcicfgmode == -1)
  531. pcicfginit();
  532. if(BUSBNO(tbdf))
  533. type = 0x01;
  534. else
  535. type = 0x00;
  536. x = -1;
  537. if(BUSDNO(tbdf) > pcimaxdno)
  538. return x;
  539. lock(&pcicfglock);
  540. switch(pcicfgmode){
  541. case 1:
  542. o = rno & 0x02;
  543. rno &= ~0x03;
  544. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  545. if(read)
  546. x = ins(PciDATA+o);
  547. else
  548. outs(PciDATA+o, data);
  549. outl(PciADDR, 0);
  550. break;
  551. case 2:
  552. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  553. outb(PciFORWARD, BUSBNO(tbdf));
  554. if(read)
  555. x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  556. else
  557. outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  558. outb(PciCSE, 0);
  559. break;
  560. }
  561. unlock(&pcicfglock);
  562. return x;
  563. }
  564. int
  565. pcicfgr16(Pcidev* pcidev, int rno)
  566. {
  567. return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
  568. }
  569. void
  570. pcicfgw16(Pcidev* pcidev, int rno, int data)
  571. {
  572. pcicfgrw16(pcidev->tbdf, rno, data, 0);
  573. }
  574. static int
  575. pcicfgrw32(int tbdf, int rno, int data, int read)
  576. {
  577. int type, x;
  578. if(pcicfgmode == -1)
  579. pcicfginit();
  580. if(BUSBNO(tbdf))
  581. type = 0x01;
  582. else
  583. type = 0x00;
  584. x = -1;
  585. if(BUSDNO(tbdf) > pcimaxdno)
  586. return x;
  587. lock(&pcicfglock);
  588. switch(pcicfgmode){
  589. case 1:
  590. rno &= ~0x03;
  591. outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
  592. if(read)
  593. x = inl(PciDATA);
  594. else
  595. outl(PciDATA, data);
  596. outl(PciADDR, 0);
  597. break;
  598. case 2:
  599. outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
  600. outb(PciFORWARD, BUSBNO(tbdf));
  601. if(read)
  602. x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
  603. else
  604. outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
  605. outb(PciCSE, 0);
  606. break;
  607. }
  608. unlock(&pcicfglock);
  609. return x;
  610. }
  611. int
  612. pcicfgr32(Pcidev* pcidev, int rno)
  613. {
  614. return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
  615. }
  616. void
  617. pcicfgw32(Pcidev* pcidev, int rno, int data)
  618. {
  619. pcicfgrw32(pcidev->tbdf, rno, data, 0);
  620. }
  621. Pcidev*
  622. pcimatch(Pcidev* prev, int vid, int did)
  623. {
  624. if(pcicfgmode == -1)
  625. pcicfginit();
  626. if(prev == nil)
  627. prev = pcilist;
  628. else
  629. prev = prev->list;
  630. while(prev != nil) {
  631. if((vid == 0 || prev->vid == vid)
  632. && (did == 0 || prev->did == did))
  633. break;
  634. prev = prev->list;
  635. }
  636. return prev;
  637. }
  638. uchar
  639. pciipin(Pcidev *pci, uchar pin)
  640. {
  641. if (pci == nil)
  642. pci = pcilist;
  643. while (pci) {
  644. uchar intl;
  645. if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
  646. return pci->intl;
  647. if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
  648. return intl;
  649. pci = pci->list;
  650. }
  651. return 0;
  652. }
  653. static ushort
  654. pciimask(Pcidev *pci)
  655. {
  656. ushort imask;
  657. imask = 0;
  658. while (pci) {
  659. if (pcicfgr8(pci, PciINTP) && pci->intl < 16)
  660. imask |= 1 << pci->intl;
  661. if (pci->bridge)
  662. imask |= pciimask(pci->bridge);
  663. pci = pci->list;
  664. }
  665. return imask;
  666. }
  667. uchar
  668. pciintl(Pcidev *pci)
  669. {
  670. ushort imask;
  671. int i;
  672. if (pci == nil)
  673. pci = pcilist;
  674. imask = pciimask(pci) | 1;
  675. for (i = 0; i != 16; i++)
  676. if ((imask & (1 << i)) == 0)
  677. return i;
  678. return 0;
  679. }
  680. void
  681. pcihinv(Pcidev* p)
  682. {
  683. int i;
  684. Pcidev *t;
  685. if(pcicfgmode == -1)
  686. pcicfginit();
  687. if(p == nil) {
  688. p = pciroot;
  689. print("bus dev type vid did intl memory\n");
  690. }
  691. for(t = p; t != nil; t = t->link) {
  692. print("%d %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d ",
  693. BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
  694. t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
  695. for(i = 0; i < nelem(p->mem); i++) {
  696. if(t->mem[i].size == 0)
  697. continue;
  698. print("%d:%.8lux %d ", i,
  699. t->mem[i].bar, t->mem[i].size);
  700. }
  701. print("\n");
  702. }
  703. while(p != nil) {
  704. if(p->bridge != nil)
  705. pcihinv(p->bridge);
  706. p = p->link;
  707. }
  708. }
  709. void
  710. pcireset(void)
  711. {
  712. Pcidev *p;
  713. int pcr;
  714. if(pcicfgmode == -1)
  715. pcicfginit();
  716. for(p = pcilist; p != nil; p = p->list){
  717. pcr = pcicfgr16(p, PciPSR);
  718. pcicfgw16(p, PciPSR, pcr & ~0x04);
  719. }
  720. }
  721. void
  722. pcisetioe(Pcidev* p)
  723. {
  724. p->pcr |= IOen;
  725. pcicfgw16(p, PciPCR, p->pcr);
  726. }
  727. void
  728. pciclrioe(Pcidev* p)
  729. {
  730. p->pcr &= ~IOen;
  731. pcicfgw16(p, PciPCR, p->pcr);
  732. }
  733. void
  734. pcisetbme(Pcidev* p)
  735. {
  736. p->pcr |= MASen;
  737. pcicfgw16(p, PciPCR, p->pcr);
  738. }
  739. void
  740. pciclrbme(Pcidev* p)
  741. {
  742. p->pcr &= ~MASen;
  743. pcicfgw16(p, PciPCR, p->pcr);
  744. }
  745. void
  746. pcisetmwi(Pcidev* p)
  747. {
  748. p->pcr |= MemWrInv;
  749. pcicfgw16(p, PciPCR, p->pcr);
  750. }
  751. void
  752. pciclrmwi(Pcidev* p)
  753. {
  754. p->pcr &= ~MemWrInv;
  755. pcicfgw16(p, PciPCR, p->pcr);
  756. }
  757. static int
  758. pcigetpmrb(Pcidev* p)
  759. {
  760. int ptr;
  761. if(p->pmrb != 0)
  762. return p->pmrb;
  763. p->pmrb = -1;
  764. /*
  765. * If there are no extended capabilities implemented,
  766. * (bit 4 in the status register) assume there's no standard
  767. * power management method.
  768. * Find the capabilities pointer based on PCI header type.
  769. */
  770. if(!(pcicfgr16(p, PciPSR) & 0x0010))
  771. return -1;
  772. switch(pcicfgr8(p, PciHDT)){
  773. default:
  774. return -1;
  775. case 0: /* all other */
  776. case 1: /* PCI to PCI bridge */
  777. ptr = 0x34;
  778. break;
  779. case 2: /* CardBus bridge */
  780. ptr = 0x14;
  781. break;
  782. }
  783. ptr = pcicfgr32(p, ptr);
  784. while(ptr != 0){
  785. /*
  786. * Check for validity.
  787. * Can't be in standard header and must be double
  788. * word aligned.
  789. */
  790. if(ptr < 0x40 || (ptr & ~0xFC))
  791. return -1;
  792. if(pcicfgr8(p, ptr) == 0x01){
  793. p->pmrb = ptr;
  794. return ptr;
  795. }
  796. ptr = pcicfgr8(p, ptr+1);
  797. }
  798. return -1;
  799. }
  800. int
  801. pcigetpms(Pcidev* p)
  802. {
  803. int pmcsr, ptr;
  804. if((ptr = pcigetpmrb(p)) == -1)
  805. return -1;
  806. /*
  807. * Power Management Register Block:
  808. * offset 0: Capability ID
  809. * 1: next item pointer
  810. * 2: capabilities
  811. * 4: control/status
  812. * 6: bridge support extensions
  813. * 7: data
  814. */
  815. pmcsr = pcicfgr16(p, ptr+4);
  816. return pmcsr & 0x0003;
  817. }
  818. int
  819. pcisetpms(Pcidev* p, int state)
  820. {
  821. int ostate, pmc, pmcsr, ptr;
  822. if((ptr = pcigetpmrb(p)) == -1)
  823. return -1;
  824. pmc = pcicfgr16(p, ptr+2);
  825. pmcsr = pcicfgr16(p, ptr+4);
  826. ostate = pmcsr & 0x0003;
  827. pmcsr &= ~0x0003;
  828. switch(state){
  829. default:
  830. return -1;
  831. case 0:
  832. break;
  833. case 1:
  834. if(!(pmc & 0x0200))
  835. return -1;
  836. break;
  837. case 2:
  838. if(!(pmc & 0x0400))
  839. return -1;
  840. break;
  841. case 3:
  842. break;
  843. }
  844. pmcsr |= state;
  845. pcicfgw16(p, ptr+4, pmcsr);
  846. return ostate;
  847. }