pci.c 15 KB

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