devpci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. #include "../port/error.h"
  16. enum {
  17. Qtopdir = 0,
  18. Qpcidir,
  19. Qpcictl,
  20. Qpciraw,
  21. };
  22. #define TYPE(q) ((ulong)(q).path & 0x0F)
  23. #define QID(c, t) (((c)<<4)|(t))
  24. static Dirtab topdir[] = {
  25. ".", { Qtopdir, 0, QTDIR }, 0, 0555,
  26. "pci", { Qpcidir, 0, QTDIR }, 0, 0555,
  27. };
  28. extern Dev pcidevtab;
  29. static int
  30. pcidirgen(Chan *c, int t, int tbdf, Dir *dp)
  31. {
  32. Qid q;
  33. q = (Qid){BUSBDF(tbdf)|t, 0, 0};
  34. switch(t) {
  35. case Qpcictl:
  36. snprint(up->genbuf, sizeof up->genbuf, "%d.%d.%dctl",
  37. BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
  38. devdir(c, q, up->genbuf, 0, eve, 0444, dp);
  39. return 1;
  40. case Qpciraw:
  41. snprint(up->genbuf, sizeof up->genbuf, "%d.%d.%draw",
  42. BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
  43. devdir(c, q, up->genbuf, 128, eve, 0664, dp);
  44. return 1;
  45. }
  46. return -1;
  47. }
  48. static int
  49. pcigen(Chan *c, char *, Dirtab*, int, int s, Dir *dp)
  50. {
  51. int tbdf;
  52. Pcidev *p;
  53. Qid q;
  54. switch(TYPE(c->qid)){
  55. case Qtopdir:
  56. if(s == DEVDOTDOT){
  57. q = (Qid){QID(0, Qtopdir), 0, QTDIR};
  58. snprint(up->genbuf, sizeof up->genbuf, "#%C", pcidevtab.dc);
  59. devdir(c, q, up->genbuf, 0, eve, 0555, dp);
  60. return 1;
  61. }
  62. return devgen(c, nil, topdir, nelem(topdir), s, dp);
  63. case Qpcidir:
  64. if(s == DEVDOTDOT){
  65. q = (Qid){QID(0, Qtopdir), 0, QTDIR};
  66. snprint(up->genbuf, sizeof up->genbuf, "#%C", pcidevtab.dc);
  67. devdir(c, q, up->genbuf, 0, eve, 0555, dp);
  68. return 1;
  69. }
  70. p = pcimatch(nil, 0, 0);
  71. while(s >= 2 && p != nil) {
  72. p = pcimatch(p, 0, 0);
  73. s -= 2;
  74. }
  75. if(p == nil)
  76. return -1;
  77. return pcidirgen(c, s+Qpcictl, p->tbdf, dp);
  78. case Qpcictl:
  79. case Qpciraw:
  80. tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((uint32_t)c->qid.path);
  81. p = pcimatchtbdf(tbdf);
  82. if(p == nil)
  83. return -1;
  84. return pcidirgen(c, TYPE(c->qid), tbdf, dp);
  85. default:
  86. break;
  87. }
  88. return -1;
  89. }
  90. static Chan*
  91. pciattach(char *spec)
  92. {
  93. return devattach(pcidevtab.dc, spec);
  94. }
  95. Walkqid*
  96. pciwalk(Chan* c, Chan *nc, char** name, int nname)
  97. {
  98. return devwalk(c, nc, name, nname, (Dirtab *)0, 0, pcigen);
  99. }
  100. static int32_t
  101. pcistat(Chan* c, uint8_t* dp, int32_t n)
  102. {
  103. return devstat(c, dp, n, (Dirtab *)0, 0L, pcigen);
  104. }
  105. static Chan*
  106. pciopen(Chan *c, int omode)
  107. {
  108. c = devopen(c, omode, (Dirtab*)0, 0, pcigen);
  109. switch(TYPE(c->qid)){
  110. default:
  111. break;
  112. }
  113. return c;
  114. }
  115. static void
  116. pciclose(Chan*)
  117. {
  118. }
  119. static int32_t
  120. pciread(Chan *c, void *va, int32_t n, int64_t offset)
  121. {
  122. char buf[256], *ebuf, *w, *a;
  123. int i, tbdf, r;
  124. uint32_t x;
  125. Pcidev *p;
  126. a = va;
  127. switch(TYPE(c->qid)){
  128. case Qtopdir:
  129. case Qpcidir:
  130. return devdirread(c, a, n, (Dirtab *)0, 0L, pcigen);
  131. case Qpcictl:
  132. tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((uint32_t)c->qid.path);
  133. p = pcimatchtbdf(tbdf);
  134. if(p == nil)
  135. error(Egreg);
  136. ebuf = buf+sizeof buf-1; /* -1 for newline */
  137. w = seprint(buf, ebuf, "%.2x.%.2x.%.2x %.4x/%.4x %3d",
  138. p->ccrb, p->ccru, p->ccrp, p->vid, p->did, p->intl);
  139. for(i=0; i<nelem(p->mem); i++){
  140. if(p->mem[i].size == 0)
  141. continue;
  142. w = seprint(w, ebuf, " %d:%.8lux %d", i, p->mem[i].bar, p->mem[i].size);
  143. }
  144. *w++ = '\n';
  145. *w = '\0';
  146. return readstr(offset, a, n, buf);
  147. case Qpciraw:
  148. tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((uint32_t)c->qid.path);
  149. p = pcimatchtbdf(tbdf);
  150. if(p == nil)
  151. error(Egreg);
  152. if(n+offset > 256)
  153. n = 256-offset;
  154. if(n < 0)
  155. return 0;
  156. r = offset;
  157. if(!(r & 3) && n == 4){
  158. x = pcicfgr32(p, r);
  159. PBIT32(a, x);
  160. return 4;
  161. }
  162. if(!(r & 1) && n == 2){
  163. x = pcicfgr16(p, r);
  164. PBIT16(a, x);
  165. return 2;
  166. }
  167. for(i = 0; i < n; i++){
  168. x = pcicfgr8(p, r);
  169. PBIT8(a, x);
  170. a++;
  171. r++;
  172. }
  173. return i;
  174. default:
  175. error(Egreg);
  176. }
  177. return n;
  178. }
  179. static int32_t
  180. pciwrite(Chan *c, void *va, int32_t n, int64_t offset)
  181. {
  182. char buf[256];
  183. uint8_t *a;
  184. int i, r, tbdf;
  185. uint32_t x;
  186. Pcidev *p;
  187. if(n >= sizeof(buf))
  188. n = sizeof(buf)-1;
  189. a = va;
  190. strncpy(buf, (char*)a, n);
  191. buf[n] = 0;
  192. switch(TYPE(c->qid)){
  193. case Qpciraw:
  194. tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((uint32_t)c->qid.path);
  195. p = pcimatchtbdf(tbdf);
  196. if(p == nil)
  197. error(Egreg);
  198. if(offset > 256)
  199. return 0;
  200. if(n+offset > 256)
  201. n = 256-offset;
  202. r = offset;
  203. if(!(r & 3) && n == 4){
  204. x = GBIT32(a);
  205. pcicfgw32(p, r, x);
  206. return 4;
  207. }
  208. if(!(r & 1) && n == 2){
  209. x = GBIT16(a);
  210. pcicfgw16(p, r, x);
  211. return 2;
  212. }
  213. for(i = 0; i < n; i++){
  214. x = GBIT8(a);
  215. pcicfgw8(p, r, x);
  216. a++;
  217. r++;
  218. }
  219. return i;
  220. default:
  221. error(Egreg);
  222. }
  223. return n;
  224. }
  225. Dev pcidevtab = {
  226. '$',
  227. "pci",
  228. devreset,
  229. devinit,
  230. devshutdown,
  231. pciattach,
  232. pciwalk,
  233. pcistat,
  234. pciopen,
  235. devcreate,
  236. pciclose,
  237. pciread,
  238. devbread,
  239. pciwrite,
  240. devbwrite,
  241. devremove,
  242. devwstat,
  243. };