pci.c 20 KB

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