devpci.c 5.0 KB

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