uperf_sbus.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* $OpenBSD: uperf_sbus.c,v 1.8 2008/12/15 22:35:06 kettenis Exp $ */
  2. /*
  3. * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * Effort sponsored in part by the Defense Advanced Research Projects
  28. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  29. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  30. *
  31. */
  32. #include <sys/types.h>
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/kernel.h>
  36. #include <sys/errno.h>
  37. #include <sys/device.h>
  38. #include <sys/malloc.h>
  39. #include <machine/bus.h>
  40. #include <machine/intr.h>
  41. #include <machine/autoconf.h>
  42. #ifdef DDB
  43. #include <machine/db_machdep.h>
  44. #endif
  45. #include <arch/sparc64/dev/uperfvar.h>
  46. #include <dev/sun/uperfio.h>
  47. #include <dev/sbus/sbusvar.h>
  48. #include <dev/sbus/uperf_sbusreg.h>
  49. int uperf_sbus_match(struct device *, void *, void *);
  50. void uperf_sbus_attach(struct device *, struct device *, void *);
  51. struct uperf_sbus_softc {
  52. struct uperf_softc sc_usc;
  53. bus_space_tag_t sc_bus_t; /* direct register tag */
  54. bus_space_handle_t sc_bus_h; /* direct register handle */
  55. };
  56. struct cfattach uperf_sbus_ca = {
  57. sizeof(struct uperf_sbus_softc), uperf_sbus_match, uperf_sbus_attach
  58. };
  59. u_int32_t uperf_sbus_read_reg(struct uperf_sbus_softc *, bus_size_t);
  60. void uperf_sbus_write_reg(struct uperf_sbus_softc *,
  61. bus_size_t, u_int32_t);
  62. int uperf_sbus_getcnt(void *, int, u_int32_t *, u_int32_t *);
  63. int uperf_sbus_clrcnt(void *, int);
  64. int uperf_sbus_getcntsrc(void *, int, u_int *, u_int *);
  65. int uperf_sbus_setcntsrc(void *, int, u_int, u_int);
  66. #ifdef DDB
  67. void uperf_sbus_xir(void *, int);
  68. #endif
  69. struct uperf_src uperf_sbus_srcs[] = {
  70. { UPERFSRC_SYSCK, UPERF_CNT0|UPERF_CNT1, SEL0_SYSCK },
  71. { UPERFSRC_PRALL, UPERF_CNT0|UPERF_CNT1, SEL0_PRALL },
  72. { UPERFSRC_PRP0, UPERF_CNT0|UPERF_CNT1, SEL0_PRP0 },
  73. { UPERFSRC_PRU2S, UPERF_CNT0|UPERF_CNT1, SEL0_PRUS },
  74. { UPERFSRC_UPA128, UPERF_CNT0, SEL0_128BUSY },
  75. { UPERFSRC_RP0, UPERF_CNT1, SEL1_RDP0 },
  76. { UPERFSRC_UPA64, UPERF_CNT0, SEL0_64BUSY },
  77. { UPERFSRC_P0CRMR, UPERF_CNT1, SEL1_CRMP0 },
  78. { UPERFSRC_PIOS, UPERF_CNT0, SEL0_PIOSTALL },
  79. { UPERFSRC_P0PIO, UPERF_CNT1, SEL1_PIOP0 },
  80. { UPERFSRC_MEMRI, UPERF_CNT0|UPERF_CNT0, SEL0_MEMREQ },
  81. { UPERFSRC_MCBUSY, UPERF_CNT0, SEL0_MCBUSY },
  82. { UPERFSRC_MEMRC, UPERF_CNT1, SEL1_MRC},
  83. { UPERFSRC_PXSH, UPERF_CNT0, SEL0_PENDSTALL },
  84. { UPERFSRC_RDP0, UPERF_CNT0, SEL1_RDP1 },
  85. { UPERFSRC_P0CWMR, UPERF_CNT0, SEL0_CWMRP0 },
  86. { UPERFSRC_CRMP1, UPERF_CNT1, SEL1_CRMP1 },
  87. { UPERFSRC_P1CWMR, UPERF_CNT0, SEL0_CWMRP1 },
  88. { UPERFSRC_PIOP1, UPERF_CNT1, SEL1_PIOP1 },
  89. { UPERFSRC_CIT, UPERF_CNT0, SEL0_CIT },
  90. { UPERFSRC_CWXI, UPERF_CNT1, SEL1_CWXI },
  91. { UPERFSRC_U2SDAT, UPERF_CNT0|UPERF_CNT1, SEL0_DACT },
  92. { UPERFSRC_CRXI, UPERF_CNT0, SEL0_CRXI },
  93. { -1, -1, 0 }
  94. };
  95. int
  96. uperf_sbus_match(struct device *parent, void *vcf, void *aux)
  97. {
  98. struct sbus_attach_args *sa = aux;
  99. return (strcmp(sa->sa_name, "sc") == 0);
  100. }
  101. void
  102. uperf_sbus_attach(struct device *parent, struct device *self, void *aux)
  103. {
  104. struct sbus_attach_args *sa = aux;
  105. struct uperf_sbus_softc *sc = (struct uperf_sbus_softc *)self;
  106. char *model;
  107. u_int32_t id;
  108. sc->sc_bus_t = sa->sa_bustag;
  109. sc->sc_usc.usc_cookie = sc;
  110. sc->sc_usc.usc_getcntsrc = uperf_sbus_getcntsrc;
  111. sc->sc_usc.usc_setcntsrc = uperf_sbus_setcntsrc;
  112. sc->sc_usc.usc_clrcnt = uperf_sbus_clrcnt;
  113. sc->sc_usc.usc_getcnt = uperf_sbus_getcnt;
  114. sc->sc_usc.usc_srcs = uperf_sbus_srcs;
  115. if (sa->sa_nreg != 1) {
  116. printf(": expected 1 register, got %d\n", sa->sa_nreg);
  117. return;
  118. }
  119. if (sbus_bus_map(sc->sc_bus_t, sa->sa_reg[0].sbr_slot,
  120. sa->sa_reg[0].sbr_offset, sa->sa_reg[0].sbr_size, 0, 0,
  121. &sc->sc_bus_h) != 0) {
  122. printf(": couldn't map registers\n");
  123. return;
  124. }
  125. id = uperf_sbus_read_reg(sc, USC_ID);
  126. model = getpropstring(sa->sa_node, "model");
  127. if (model == NULL || strlen(model) == 0)
  128. model = "unknown";
  129. printf(": model %s (%x/%x) ports %d\n", model,
  130. (id & USC_ID_IMPL_M) >> USC_ID_IMPL_S,
  131. (id & USC_ID_VERS_M) >> USC_ID_VERS_S,
  132. (id & USC_ID_UPANUM_M) >> USC_ID_UPANUM_S);
  133. #ifdef DDB
  134. db_register_xir(uperf_sbus_xir, sc);
  135. #endif
  136. }
  137. /*
  138. * Read from an indirect register
  139. */
  140. u_int32_t
  141. uperf_sbus_read_reg(struct uperf_sbus_softc *sc, bus_size_t r)
  142. {
  143. u_int32_t v;
  144. int s;
  145. s = splhigh();
  146. bus_space_write_1(sc->sc_bus_t, sc->sc_bus_h, USC_ADDR, r);
  147. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_ADDR, 1,
  148. BUS_SPACE_BARRIER_WRITE);
  149. /* Can't use multi reads because we have to gaurantee order */
  150. v = bus_space_read_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 0);
  151. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 0, 1,
  152. BUS_SPACE_BARRIER_READ);
  153. v <<= 8;
  154. v |= bus_space_read_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 1);
  155. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 1, 1,
  156. BUS_SPACE_BARRIER_READ);
  157. v <<= 8;
  158. v |= bus_space_read_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 2);
  159. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 2, 1,
  160. BUS_SPACE_BARRIER_READ);
  161. v <<= 8;
  162. v |= bus_space_read_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 3);
  163. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 3, 1,
  164. BUS_SPACE_BARRIER_READ);
  165. splx(s);
  166. return (v);
  167. }
  168. /*
  169. * Write to an indirect register
  170. */
  171. void
  172. uperf_sbus_write_reg(struct uperf_sbus_softc *sc, bus_size_t r, u_int32_t v)
  173. {
  174. int s;
  175. s = splhigh();
  176. bus_space_write_1(sc->sc_bus_t, sc->sc_bus_h, USC_ADDR, r);
  177. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_ADDR, 1,
  178. BUS_SPACE_BARRIER_WRITE);
  179. /* Can't use multi writes because we have to gaurantee order */
  180. bus_space_write_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 0,
  181. (v >> 24) & 0xff);
  182. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 0, 1,
  183. BUS_SPACE_BARRIER_WRITE);
  184. bus_space_write_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 1,
  185. (v >> 16) & 0xff);
  186. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 1, 1,
  187. BUS_SPACE_BARRIER_WRITE);
  188. bus_space_write_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 2,
  189. (v >> 8) & 0xff);
  190. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 2, 1,
  191. BUS_SPACE_BARRIER_WRITE);
  192. bus_space_write_1(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 3,
  193. (v >> 0) & 0xff);
  194. bus_space_barrier(sc->sc_bus_t, sc->sc_bus_h, USC_DATA + 3, 1,
  195. BUS_SPACE_BARRIER_WRITE);
  196. splx(s);
  197. }
  198. int
  199. uperf_sbus_clrcnt(void *vsc, int flags)
  200. {
  201. struct uperf_sbus_softc *sc = vsc;
  202. u_int32_t clr = 0, oldsrc;
  203. if (flags & UPERF_CNT0)
  204. clr |= USC_PCTRL_CLR0;
  205. if (flags & UPERF_CNT1)
  206. clr |= USC_PCTRL_CLR1;
  207. if (clr) {
  208. oldsrc = uperf_sbus_read_reg(sc, USC_PERFCTRL);
  209. uperf_sbus_write_reg(sc, USC_PERFCTRL, clr | oldsrc);
  210. }
  211. return (0);
  212. }
  213. int
  214. uperf_sbus_setcntsrc(void *vsc, int flags, u_int src0, u_int src1)
  215. {
  216. struct uperf_sbus_softc *sc = vsc;
  217. u_int32_t src;
  218. src = uperf_sbus_read_reg(sc, USC_PERFCTRL);
  219. if (flags & UPERF_CNT0) {
  220. src &= ~USC_PCTRL_SEL0;
  221. src |= ((src0 << 0) & USC_PCTRL_SEL0) | USC_PCTRL_CLR0;
  222. }
  223. if (flags & UPERF_CNT1) {
  224. src &= ~USC_PCTRL_SEL1;
  225. src |= ((src1 << 8) & USC_PCTRL_SEL1) | USC_PCTRL_CLR1;
  226. }
  227. uperf_sbus_write_reg(sc, USC_PERFCTRL, src);
  228. return (0);
  229. }
  230. int
  231. uperf_sbus_getcntsrc(void *vsc, int flags, u_int *srcp0, u_int *srcp1)
  232. {
  233. struct uperf_sbus_softc *sc = vsc;
  234. u_int32_t src;
  235. src = uperf_sbus_read_reg(sc, USC_PERFCTRL);
  236. if (flags & UPERF_CNT0)
  237. *srcp0 = (src & USC_PCTRL_SEL0) >> 0;
  238. if (flags & UPERF_CNT1)
  239. *srcp1 = (src & USC_PCTRL_SEL1) >> 8;
  240. return (0);
  241. }
  242. int
  243. uperf_sbus_getcnt(void *vsc, int flags, u_int32_t *cntp0, u_int32_t *cntp1)
  244. {
  245. struct uperf_sbus_softc *sc = vsc;
  246. u_int32_t c0, c1;
  247. c0 = uperf_sbus_read_reg(sc, USC_PERF0);
  248. c1 = uperf_sbus_read_reg(sc, USC_PERFSHAD);
  249. if (flags & UPERF_CNT0)
  250. *cntp0 = c0;
  251. if (flags & UPERF_CNT1)
  252. *cntp1 = c1;
  253. return (0);
  254. }
  255. #ifdef DDB
  256. void
  257. uperf_sbus_xir(void *arg, int cpu)
  258. {
  259. struct uperf_sbus_softc *sc = arg;
  260. uperf_sbus_write_reg(sc, USC_CTRL, USC_CTRL_XIR);
  261. }
  262. #endif