pci.c 20 KB

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