pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. * Needs a massive rewrite.
  12. */
  13. #include "u.h"
  14. #include "../port/lib.h"
  15. #include "mem.h"
  16. #include "dat.h"
  17. #include "fns.h"
  18. #include "io.h"
  19. void virtiosetup();
  20. int
  21. pcicapoff(Pcidev *p);
  22. enum {
  23. PciADDR = 0xCF8, /* CONFIG_ADDRESS */
  24. PciDATA = 0xCFC, /* CONFIG_DATA */
  25. Maxfn = 7,
  26. Maxdev = 31,
  27. Maxbus = 255,
  28. /* command register */
  29. IOen = (1 << 0),
  30. MEMen = (1 << 1),
  31. MASen = (1 << 2),
  32. MemWrInv = (1 << 4),
  33. PErrEn = (1 << 6),
  34. SErrEn = (1 << 8),
  35. Write,
  36. Read,
  37. };
  38. static Lock pcicfglock;
  39. static Lock pcicfginitlock;
  40. static int pcicfgmode = -1;
  41. static Pcidev *pciroot;
  42. static Pcidev *pcilist;
  43. static Pcidev *pcitail;
  44. static char *bustypes[] = {
  45. "CBUSI",
  46. "CBUSII",
  47. "EISA",
  48. "FUTURE",
  49. "INTERN",
  50. "ISA",
  51. "MBI",
  52. "MBII",
  53. "MCA",
  54. "MPI",
  55. "MPSA",
  56. "NUBUS",
  57. "PCI",
  58. "PCMCIA",
  59. "TC",
  60. "VL",
  61. "VME",
  62. "XPRESS",
  63. };
  64. static int
  65. pcicfgrw(int, int, int, int, int);
  66. static int
  67. tbdffmt(Fmt *fmt)
  68. {
  69. char *p;
  70. int l, r;
  71. u32 type, tbdf;
  72. if((p = malloc(READSTR)) == nil)
  73. return fmtstrcpy(fmt, "(tbdfconv)");
  74. switch(fmt->r){
  75. case 'T':
  76. tbdf = va_arg(fmt->args, u32);
  77. type = BUSTYPE(tbdf);
  78. if(type < nelem(bustypes))
  79. l = snprint(p, READSTR, bustypes[type]);
  80. else
  81. l = snprint(p, READSTR, "%d", type);
  82. snprint(p + l, READSTR - l, ".%d.%d.%d",
  83. BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
  84. break;
  85. default:
  86. snprint(p, READSTR, "(tbdfconv)");
  87. break;
  88. }
  89. r = fmtstrcpy(fmt, p);
  90. free(p);
  91. return r;
  92. }
  93. static u32
  94. pcibarsize(Pcidev *p, int rno)
  95. {
  96. u32 v, size;
  97. v = pcicfgr32(p, rno);
  98. pcicfgw32(p, rno, 0xFFFFFFF0);
  99. size = pcicfgr32(p, rno);
  100. if(v & 1)
  101. size |= 0xFFFF0000;
  102. pcicfgw32(p, rno, v);
  103. return -(size & ~0x0F);
  104. }
  105. static int
  106. pcilscan(int bno, char *path, Pcidev **list)
  107. {
  108. Pcidev *p, *head, *tail;
  109. int dno, fno, i, hdt, l, maxfno, maxubn, sbn, tbdf, ubn, capoff;
  110. maxubn = bno;
  111. head = nil;
  112. tail = nil;
  113. for(dno = 0; dno <= Maxdev; dno++){
  114. maxfno = 0;
  115. for(fno = 0; fno <= maxfno; fno++){
  116. /*
  117. * For this possible device, form the
  118. * bus+device+function triplet needed to address it
  119. * and try to read the vendor and device ID.
  120. * If successful, allocate a device struct and
  121. * start to fill it in with some useful information
  122. * from the device's configuration space.
  123. */
  124. tbdf = MKBUS(BusPCI, bno, dno, fno);
  125. l = pcicfgrw(tbdf, PciVID, 0, Read, 4);
  126. if(l == 0xFFFFFFFF || l == 0)
  127. continue;
  128. p = malloc(sizeof(*p));
  129. p->caplist = nil;
  130. p->capidx = nil;
  131. p->capcnt = 0;
  132. p->tbdf = tbdf;
  133. p->vid = l;
  134. p->did = l >> 16;
  135. p->path = path;
  136. if(pcilist != nil)
  137. pcitail->list = p;
  138. else
  139. pcilist = p;
  140. pcitail = p;
  141. p->pcr = pcicfgr16(p, PciPCR);
  142. p->rid = pcicfgr8(p, PciRID);
  143. p->ccrp = pcicfgr8(p, PciCCRp);
  144. p->ccru = pcicfgr8(p, PciCCRu);
  145. p->ccrb = pcicfgr8(p, PciCCRb);
  146. p->cls = pcicfgr8(p, PciCLS);
  147. p->ltr = pcicfgr8(p, PciLTR);
  148. p->intl = pcicfgr8(p, PciINTL);
  149. p->intp = pcicfgr8(p, PciINTP);
  150. /*
  151. * If the device is a multi-function device adjust the
  152. * loop count so all possible functions are checked.
  153. */
  154. hdt = pcicfgr8(p, PciHDT);
  155. if(hdt & 0x80)
  156. maxfno = Maxfn;
  157. /*
  158. * If appropriate, read the base address registers
  159. * and work out the sizes.
  160. */
  161. switch(p->ccrb){
  162. default:
  163. if((hdt & 0x7F) != 0)
  164. break;
  165. for(i = 0; i < nelem(p->mem); i++){
  166. p->mem[i].bar = pcicfgr32(p, PciBAR0 + 4 * i);
  167. p->mem[i].size = pcibarsize(p, PciBAR0 + 4 * i);
  168. }
  169. break;
  170. /*
  171. * Some virtio-pci devices have ccrb == 0x00, their BARs and
  172. * sizes also should be picked here.
  173. */
  174. case 0x05: /* memory controller */
  175. case 0x06: /* bridge device */
  176. break;
  177. }
  178. /*
  179. * Try to gather PCI capabilities. If the offset of capabilities
  180. * in the config area cannot be found, skip this step. For simplicity,
  181. * capabilities will be linked in a LIFO so we don't deal with list
  182. * heads and tails.
  183. */
  184. capoff = pcicapoff(p);
  185. int off = capoff;
  186. while(capoff != -1){
  187. off = pcicfgr8(p, off);
  188. if((off < 0x40) || (off & 3))
  189. break;
  190. off &= ~3;
  191. Pcicap *c = malloc(sizeof(*c));
  192. c->dev = p;
  193. c->link = p->caplist;
  194. p->caplist = c;
  195. p->capcnt++;
  196. c->vndr = pcicfgr8(p, off + PciCapVndr);
  197. c->caplen = pcicfgr8(p, off + PciCapLen);
  198. c->type = pcicfgr8(p, off + PciCapType);
  199. c->bar = pcicfgr8(p, off + PciCapBar);
  200. c->offset = pcicfgr32(p, off + PciCapOff);
  201. c->length = pcicfgr32(p, off + PciCapLength);
  202. off++;
  203. }
  204. if(p->capcnt > 0){
  205. p->capidx = malloc(p->capcnt * sizeof(Pcicap *));
  206. Pcicap *pcp = p->caplist;
  207. for(int pix = 0;; pix++){
  208. p->capidx[pix] = pcp;
  209. pcp = pcp->link;
  210. if(pcp == nil)
  211. break;
  212. }
  213. }
  214. if(head != nil)
  215. tail->link = p;
  216. else
  217. head = p;
  218. tail = p;
  219. }
  220. }
  221. *list = head;
  222. for(p = head; p != nil; p = p->link){
  223. /*
  224. * Find PCI-PCI bridges and recursively descend the tree.
  225. */
  226. if(p->ccrb != 0x06 || p->ccru != 0x04)
  227. continue;
  228. /*
  229. * If the secondary or subordinate bus number is not
  230. * initialised try to do what the PCI BIOS should have
  231. * done and fill in the numbers as the tree is descended.
  232. * On the way down the subordinate bus number is set to
  233. * the maximum as it's not known how many buses are behind
  234. * this one; the final value is set on the way back up.
  235. */
  236. sbn = pcicfgr8(p, PciSBN);
  237. ubn = pcicfgr8(p, PciUBN);
  238. if(sbn == 0 || ubn == 0){
  239. print("%T: unconfigured bridge\n", p->tbdf);
  240. sbn = maxubn + 1;
  241. /*
  242. * Make sure memory, I/O and master enables are
  243. * off, set the primary, secondary and subordinate
  244. * bus numbers and clear the secondary status before
  245. * attempting to scan the secondary bus.
  246. *
  247. * Initialisation of the bridge should be done here.
  248. */
  249. pcicfgw32(p, PciPCR, 0xFFFF0000);
  250. pcicfgw32(p, PciPBN, Maxbus << 16 | sbn << 8 | bno);
  251. pcicfgw16(p, PciSPSR, 0xFFFF);
  252. char *bus = mallocz(256, 1);
  253. snprint(bus, 256, "%s/%d.%d.0", path, BUSBNO(p->tbdf), BUSDNO(p->tbdf));
  254. maxubn = pcilscan(sbn, bus, &p->bridge);
  255. pcicfgw32(p, PciPBN, maxubn << 16 | sbn << 8 | bno);
  256. } else {
  257. /*
  258. * You can't go back.
  259. * This shouldn't be possible, but the
  260. * Iwill DK8-HTX seems to have subordinate
  261. * bus numbers which get smaller on the
  262. * way down. Need to look more closely at
  263. * this.
  264. */
  265. if(ubn > maxubn){
  266. maxubn = ubn;
  267. }
  268. char *bus = mallocz(256, 1);
  269. snprint(bus, 256, "%s/%d.%d.0", path, BUSBNO(p->tbdf), BUSDNO(p->tbdf));
  270. pcilscan(sbn, bus, &p->bridge);
  271. }
  272. }
  273. return maxubn;
  274. }
  275. typedef struct Bridge Bridge;
  276. struct Bridge {
  277. u16 vid;
  278. u16 did;
  279. u8 (*get)(Pcidev *, u8);
  280. void (*set)(Pcidev *, u8, u8);
  281. };
  282. typedef struct Slot Slot;
  283. struct Slot {
  284. u8 bus; // Pci bus number
  285. u8 dev; // Pci device number
  286. u8 maps[12]; // Avoid structs! Link and mask.
  287. u8 slot; // Add-in/built-in slot
  288. u8 reserved;
  289. };
  290. typedef struct Router Router;
  291. struct Router {
  292. u8 signature[4]; // Routing table signature
  293. u8 version[2]; // Version number
  294. u8 size[2]; // Total table size
  295. u8 bus; // Interrupt router bus number
  296. u8 devfn; // Router's devfunc
  297. u8 pciirqs[2]; // Exclusive PCI irqs
  298. u8 compat[4]; // Compatible PCI interrupt router
  299. u8 miniport[4]; // Miniport data
  300. u8 reserved[11];
  301. u8 checksum;
  302. };
  303. static void
  304. pcireservemem(void)
  305. {
  306. int i;
  307. Pcidev *p;
  308. for(p = nil; (p = pcimatch(p, 0, 0)) != nil;)
  309. for(i = 0; i < nelem(p->mem); i++)
  310. if(p->mem[i].bar && (p->mem[i].bar & 1) == 0)
  311. pamapinsert(p->mem[i].bar & ~0x0F, p->mem[i].size, PamDEV);
  312. }
  313. static void
  314. pcicfginit(void)
  315. {
  316. int sbno, bno, n;
  317. Pcidev **list, *p;
  318. if(pcicfgmode != -1)
  319. return;
  320. lock(&pcicfginitlock);
  321. if(pcicfgmode != -1){
  322. unlock(&pcicfginitlock);
  323. return;
  324. }
  325. fmtinstall('T', tbdffmt);
  326. /*
  327. * Try to determine if PCI Mode1 configuration implemented.
  328. * (Bits [30:24] of PciADDR must be 0, according to the spec.)
  329. * Mode2 won't appear in 64-bit machines.
  330. */
  331. n = inl(PciADDR);
  332. if(!(n & 0x7F000000)){
  333. outl(PciADDR, 0x80000000);
  334. outb(PciADDR + 3, 0);
  335. if(inl(PciADDR) & 0x80000000)
  336. pcicfgmode = 1;
  337. }
  338. outl(PciADDR, n);
  339. if(pcicfgmode < 0){
  340. unlock(&pcicfginitlock);
  341. return;
  342. }
  343. list = &pciroot;
  344. for(bno = 0; bno <= Maxbus; bno++){
  345. sbno = bno;
  346. bno = pcilscan(bno, "0.0.0", list);
  347. while(*list)
  348. list = &(*list)->link;
  349. if(sbno != 0)
  350. continue;
  351. /*
  352. * If we have found a PCI-to-Cardbus bridge, make sure
  353. * it has no valid mappings anymore.
  354. */
  355. for(p = pciroot; p != nil; p = p->link){
  356. if(p->ccrb == 6 && p->ccru == 7){
  357. /* reset the cardbus */
  358. pcicfgw16(p, PciBCR, 0x40 | pcicfgr16(p, PciBCR));
  359. delay(50);
  360. }
  361. }
  362. }
  363. pcireservemem();
  364. unlock(&pcicfginitlock);
  365. // Bring the virtio devices live.
  366. virtiosetup();
  367. //if(getconf("*pcihinv"))
  368. pcihinv(nil);
  369. }
  370. static int
  371. pcicfgrw(int tbdf, int r, int data, int rw, int w)
  372. {
  373. int o, x, er;
  374. pcicfginit();
  375. if(pcicfgmode != 1)
  376. return -1;
  377. if(BUSDNO(tbdf) > Maxdev)
  378. return -1;
  379. lock(&pcicfglock);
  380. o = r & (4 - w);
  381. er = (r & 0xfc) | ((r & 0xf00) << 16);
  382. outl(PciADDR, 0x80000000 | BUSBDF(tbdf) | er);
  383. if(rw == Read){
  384. x = -1;
  385. switch(w){
  386. case 1:
  387. x = inb(PciDATA + o);
  388. break;
  389. case 2:
  390. x = ins(PciDATA + o);
  391. break;
  392. case 4:
  393. x = inl(PciDATA + o);
  394. break;
  395. }
  396. } else {
  397. x = 0;
  398. switch(w){
  399. case 1:
  400. outb(PciDATA + o, data);
  401. break;
  402. case 2:
  403. outs(PciDATA + o, data);
  404. break;
  405. case 4:
  406. outl(PciDATA + o, data);
  407. break;
  408. }
  409. }
  410. // outl(PciADDR, 0);
  411. unlock(&pcicfglock);
  412. return x;
  413. }
  414. int
  415. pcicfgr8(Pcidev *p, int rno)
  416. {
  417. return pcicfgrw(p->tbdf, rno, 0, Read, 1);
  418. }
  419. void
  420. pcicfgw8(Pcidev *p, int rno, int data)
  421. {
  422. pcicfgrw(p->tbdf, rno, data, Write, 1);
  423. }
  424. int
  425. pcicfgr16(Pcidev *p, int rno)
  426. {
  427. return pcicfgrw(p->tbdf, rno, 0, Read, 2);
  428. }
  429. void
  430. pcicfgw16(Pcidev *p, int rno, int data)
  431. {
  432. pcicfgrw(p->tbdf, rno, data, Write, 2);
  433. }
  434. int
  435. pcicfgr32(Pcidev *p, int rno)
  436. {
  437. return pcicfgrw(p->tbdf, rno, 0, Read, 4);
  438. }
  439. void
  440. pcicfgw32(Pcidev *p, int rno, int data)
  441. {
  442. pcicfgrw(p->tbdf, rno, data, Write, 4);
  443. }
  444. Pcidev *
  445. pcimatch(Pcidev *prev, int vid, int did)
  446. {
  447. pcicfginit();
  448. prev = prev ? prev->list : pcilist;
  449. for(; prev != nil; prev = prev->list){
  450. if((vid == 0 || prev->vid == vid) && (did == 0 || prev->did == did))
  451. break;
  452. }
  453. return prev;
  454. }
  455. Pcidev *
  456. pcimatchtbdf(int tbdf)
  457. {
  458. Pcidev *p;
  459. for(p = nil; (p = pcimatch(p, 0, 0)) != nil;)
  460. if(p->tbdf == tbdf)
  461. break;
  462. return p;
  463. }
  464. void
  465. pcishowdev(Pcidev *t)
  466. {
  467. int i;
  468. char intpin = 'x';
  469. /* intpin numbers can range from 1 to 8. */
  470. intpin = "xABCDEFGHxxxxxxx"[t->intp & 0xf];
  471. print("%d %2d/%d %.2x %.2x %.2x %.4x %.4x %c %3d ",
  472. BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
  473. t->ccrb, t->ccru, t->ccrp, t->vid, t->did, intpin, t->intl);
  474. for(i = 0; i < nelem(t->mem); i++){
  475. if(t->mem[i].size == 0)
  476. continue;
  477. print("%d:%.8llx %d ", i, t->mem[i].bar, t->mem[i].size);
  478. }
  479. if(t->ioa.bar || t->ioa.size)
  480. print("ioa:%.8llx %d ", t->ioa.bar, t->ioa.size);
  481. if(t->mema.bar || t->mema.size)
  482. if(t->bridge)
  483. print("->%d", BUSBNO(t->bridge->tbdf));
  484. print("\n");
  485. }
  486. static void
  487. pcilhinv(Pcidev *p)
  488. {
  489. Pcidev *t;
  490. for(t = p; t != nil; t = t->link)
  491. pcishowdev(t);
  492. for(; p != nil; p = p->link)
  493. if(p->bridge != nil)
  494. pcilhinv(p->bridge);
  495. }
  496. void
  497. pcihinv(Pcidev *p)
  498. {
  499. pcicfginit();
  500. lock(&pcicfginitlock);
  501. if(p == nil){
  502. p = pciroot;
  503. print("bus dev type vid did intl memory\n");
  504. }
  505. pcilhinv(p);
  506. unlock(&pcicfginitlock);
  507. }
  508. void
  509. pcireset(void)
  510. {
  511. Pcidev *p;
  512. for(p = nil; (p = pcimatch(p, 0, 0)) != nil;)
  513. /* don't mess with the bridges */
  514. if(p->ccrb != 0x06)
  515. pciclrbme(p);
  516. }
  517. void
  518. pcisetbme(Pcidev *p)
  519. {
  520. p->pcr |= MASen;
  521. pcicfgw16(p, PciPCR, p->pcr);
  522. }
  523. void
  524. pciclrbme(Pcidev *p)
  525. {
  526. p->pcr &= ~MASen;
  527. pcicfgw16(p, PciPCR, p->pcr);
  528. }
  529. void
  530. pcisetmwi(Pcidev *p)
  531. {
  532. p->pcr |= MemWrInv;
  533. pcicfgw16(p, PciPCR, p->pcr);
  534. }
  535. void
  536. pciclrmwi(Pcidev *p)
  537. {
  538. p->pcr &= ~MemWrInv;
  539. pcicfgw16(p, PciPCR, p->pcr);
  540. }
  541. // Find the capability offset in a PCI device configuration space.
  542. // It depends of whether a device is a bridge, or a regular PCI device.
  543. // Return a positive number (offset) if capabilities are present, or -1
  544. // if the device does not have capabilities.
  545. int
  546. pcicapoff(Pcidev *p)
  547. {
  548. int off;
  549. /* status register bit 4 has capabilities */
  550. if((pcicfgr16(p, PciPSR) & 1 << 4) == 0)
  551. return -1;
  552. switch(pcicfgr8(p, PciHDT) & 0x7f){
  553. default:
  554. return -1;
  555. case 0: /* etc */
  556. case 1: /* pci to pci bridge */
  557. off = 0x34;
  558. break;
  559. case 2: /* cardbus bridge */
  560. off = 0x14;
  561. break;
  562. }
  563. return off;
  564. }
  565. // Obtain the offset to the needed capability (by its cap_vndr value)
  566. // in the device configuration space. Return a positive number (offset)
  567. // if the capability exists, or -1 otherwise.
  568. int
  569. pcicap(Pcidev *p, int cap)
  570. {
  571. int i, c, off;
  572. off = pcicapoff(p);
  573. if(off == -1)
  574. return -1;
  575. for(i = 48; i--;){
  576. off = pcicfgr8(p, off);
  577. if(off < 0x40 || (off & 3))
  578. break;
  579. off &= ~3;
  580. c = pcicfgr8(p, off);
  581. if(c == 0xff)
  582. break;
  583. if(c == cap)
  584. return off;
  585. off++;
  586. }
  587. return -1;
  588. }
  589. enum {
  590. Pmgcap = 2, /* capabilities; 2 bytes*/
  591. Pmgctl = 4, /* ctl/status; 2 bytes */
  592. Pmgbrg = 6, /* bridge support */
  593. Pmgdata = 7,
  594. };
  595. int
  596. pcigetpms(Pcidev *p)
  597. {
  598. int ptr;
  599. if((ptr = pcicap(p, PciCapPMG)) == -1)
  600. return -1;
  601. return pcicfgr16(p, ptr + Pmgctl) & 0x0003;
  602. }
  603. int
  604. pcisetpms(Pcidev *p, int state)
  605. {
  606. int pmc, pmcsr, ptr;
  607. if((ptr = pcicap(p, PciCapPMG)) == -1)
  608. return -1;
  609. pmc = pcicfgr16(p, ptr + Pmgcap);
  610. pmcsr = pcicfgr16(p, ptr + Pmgctl);
  611. switch(state){
  612. default:
  613. return -1;
  614. case 0:
  615. break;
  616. case 1:
  617. if(!(pmc & 0x0200))
  618. return -1;
  619. break;
  620. case 2:
  621. if(!(pmc & 0x0400))
  622. return -1;
  623. break;
  624. case 3:
  625. break;
  626. }
  627. pcicfgw16(p, ptr + 4, (pmcsr & ~3) | state);
  628. return pmcsr & 3;
  629. }