v3d_perfmon.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 Raspberry Pi
  4. */
  5. #include "v3d_drv.h"
  6. #include "v3d_regs.h"
  7. #define V3D_PERFMONID_MIN 1
  8. #define V3D_PERFMONID_MAX U32_MAX
  9. static const struct v3d_perf_counter_desc v3d_v42_performance_counters[] = {
  10. {"FEP", "FEP-valid-primitives-no-rendered-pixels", "[FEP] Valid primitives that result in no rendered pixels, for all rendered tiles"},
  11. {"FEP", "FEP-valid-primitives-rendered-pixels", "[FEP] Valid primitives for all rendered tiles (primitives may be counted in more than one tile)"},
  12. {"FEP", "FEP-clipped-quads", "[FEP] Early-Z/Near/Far clipped quads"},
  13. {"FEP", "FEP-valid-quads", "[FEP] Valid quads"},
  14. {"TLB", "TLB-quads-not-passing-stencil-test", "[TLB] Quads with no pixels passing the stencil test"},
  15. {"TLB", "TLB-quads-not-passing-z-and-stencil-test", "[TLB] Quads with no pixels passing the Z and stencil tests"},
  16. {"TLB", "TLB-quads-passing-z-and-stencil-test", "[TLB] Quads with any pixels passing the Z and stencil tests"},
  17. {"TLB", "TLB-quads-with-zero-coverage", "[TLB] Quads with all pixels having zero coverage"},
  18. {"TLB", "TLB-quads-with-non-zero-coverage", "[TLB] Quads with any pixels having non-zero coverage"},
  19. {"TLB", "TLB-quads-written-to-color-buffer", "[TLB] Quads with valid pixels written to colour buffer"},
  20. {"PTB", "PTB-primitives-discarded-outside-viewport", "[PTB] Primitives discarded by being outside the viewport"},
  21. {"PTB", "PTB-primitives-need-clipping", "[PTB] Primitives that need clipping"},
  22. {"PTB", "PTB-primitives-discarded-reversed", "[PTB] Primitives that are discarded because they are reversed"},
  23. {"QPU", "QPU-total-idle-clk-cycles", "[QPU] Total idle clock cycles for all QPUs"},
  24. {"QPU", "QPU-total-active-clk-cycles-vertex-coord-shading", "[QPU] Total active clock cycles for all QPUs doing vertex/coordinate/user shading (counts only when QPU is not stalled)"},
  25. {"QPU", "QPU-total-active-clk-cycles-fragment-shading", "[QPU] Total active clock cycles for all QPUs doing fragment shading (counts only when QPU is not stalled)"},
  26. {"QPU", "QPU-total-clk-cycles-executing-valid-instr", "[QPU] Total clock cycles for all QPUs executing valid instructions"},
  27. {"QPU", "QPU-total-clk-cycles-waiting-TMU", "[QPU] Total clock cycles for all QPUs stalled waiting for TMUs only (counter won't increment if QPU also stalling for another reason)"},
  28. {"QPU", "QPU-total-clk-cycles-waiting-scoreboard", "[QPU] Total clock cycles for all QPUs stalled waiting for Scoreboard only (counter won't increment if QPU also stalling for another reason)"},
  29. {"QPU", "QPU-total-clk-cycles-waiting-varyings", "[QPU] Total clock cycles for all QPUs stalled waiting for Varyings only (counter won't increment if QPU also stalling for another reason)"},
  30. {"QPU", "QPU-total-instr-cache-hit", "[QPU] Total instruction cache hits for all slices"},
  31. {"QPU", "QPU-total-instr-cache-miss", "[QPU] Total instruction cache misses for all slices"},
  32. {"QPU", "QPU-total-uniform-cache-hit", "[QPU] Total uniforms cache hits for all slices"},
  33. {"QPU", "QPU-total-uniform-cache-miss", "[QPU] Total uniforms cache misses for all slices"},
  34. {"TMU", "TMU-total-text-quads-access", "[TMU] Total texture cache accesses"},
  35. {"TMU", "TMU-total-text-cache-miss", "[TMU] Total texture cache misses (number of fetches from memory/L2cache)"},
  36. {"VPM", "VPM-total-clk-cycles-VDW-stalled", "[VPM] Total clock cycles VDW is stalled waiting for VPM access"},
  37. {"VPM", "VPM-total-clk-cycles-VCD-stalled", "[VPM] Total clock cycles VCD is stalled waiting for VPM access"},
  38. {"CLE", "CLE-bin-thread-active-cycles", "[CLE] Bin thread active cycles"},
  39. {"CLE", "CLE-render-thread-active-cycles", "[CLE] Render thread active cycles"},
  40. {"L2T", "L2T-total-cache-hit", "[L2T] Total Level 2 cache hits"},
  41. {"L2T", "L2T-total-cache-miss", "[L2T] Total Level 2 cache misses"},
  42. {"CORE", "cycle-count", "[CORE] Cycle counter"},
  43. {"QPU", "QPU-total-clk-cycles-waiting-vertex-coord-shading", "[QPU] Total stalled clock cycles for all QPUs doing vertex/coordinate/user shading"},
  44. {"QPU", "QPU-total-clk-cycles-waiting-fragment-shading", "[QPU] Total stalled clock cycles for all QPUs doing fragment shading"},
  45. {"PTB", "PTB-primitives-binned", "[PTB] Total primitives binned"},
  46. {"AXI", "AXI-writes-seen-watch-0", "[AXI] Writes seen by watch 0"},
  47. {"AXI", "AXI-reads-seen-watch-0", "[AXI] Reads seen by watch 0"},
  48. {"AXI", "AXI-writes-stalled-seen-watch-0", "[AXI] Write stalls seen by watch 0"},
  49. {"AXI", "AXI-reads-stalled-seen-watch-0", "[AXI] Read stalls seen by watch 0"},
  50. {"AXI", "AXI-write-bytes-seen-watch-0", "[AXI] Total bytes written seen by watch 0"},
  51. {"AXI", "AXI-read-bytes-seen-watch-0", "[AXI] Total bytes read seen by watch 0"},
  52. {"AXI", "AXI-writes-seen-watch-1", "[AXI] Writes seen by watch 1"},
  53. {"AXI", "AXI-reads-seen-watch-1", "[AXI] Reads seen by watch 1"},
  54. {"AXI", "AXI-writes-stalled-seen-watch-1", "[AXI] Write stalls seen by watch 1"},
  55. {"AXI", "AXI-reads-stalled-seen-watch-1", "[AXI] Read stalls seen by watch 1"},
  56. {"AXI", "AXI-write-bytes-seen-watch-1", "[AXI] Total bytes written seen by watch 1"},
  57. {"AXI", "AXI-read-bytes-seen-watch-1", "[AXI] Total bytes read seen by watch 1"},
  58. {"TLB", "TLB-partial-quads-written-to-color-buffer", "[TLB] Partial quads written to the colour buffer"},
  59. {"TMU", "TMU-total-config-access", "[TMU] Total config accesses"},
  60. {"L2T", "L2T-no-id-stalled", "[L2T] No ID stall"},
  61. {"L2T", "L2T-command-queue-stalled", "[L2T] Command queue full stall"},
  62. {"L2T", "L2T-TMU-writes", "[L2T] TMU write accesses"},
  63. {"TMU", "TMU-active-cycles", "[TMU] Active cycles"},
  64. {"TMU", "TMU-stalled-cycles", "[TMU] Stalled cycles"},
  65. {"CLE", "CLE-thread-active-cycles", "[CLE] Bin or render thread active cycles"},
  66. {"L2T", "L2T-TMU-reads", "[L2T] TMU read accesses"},
  67. {"L2T", "L2T-CLE-reads", "[L2T] CLE read accesses"},
  68. {"L2T", "L2T-VCD-reads", "[L2T] VCD read accesses"},
  69. {"L2T", "L2T-TMU-config-reads", "[L2T] TMU CFG read accesses"},
  70. {"L2T", "L2T-SLC0-reads", "[L2T] SLC0 read accesses"},
  71. {"L2T", "L2T-SLC1-reads", "[L2T] SLC1 read accesses"},
  72. {"L2T", "L2T-SLC2-reads", "[L2T] SLC2 read accesses"},
  73. {"L2T", "L2T-TMU-write-miss", "[L2T] TMU write misses"},
  74. {"L2T", "L2T-TMU-read-miss", "[L2T] TMU read misses"},
  75. {"L2T", "L2T-CLE-read-miss", "[L2T] CLE read misses"},
  76. {"L2T", "L2T-VCD-read-miss", "[L2T] VCD read misses"},
  77. {"L2T", "L2T-TMU-config-read-miss", "[L2T] TMU CFG read misses"},
  78. {"L2T", "L2T-SLC0-read-miss", "[L2T] SLC0 read misses"},
  79. {"L2T", "L2T-SLC1-read-miss", "[L2T] SLC1 read misses"},
  80. {"L2T", "L2T-SLC2-read-miss", "[L2T] SLC2 read misses"},
  81. {"CORE", "core-memory-writes", "[CORE] Total memory writes"},
  82. {"L2T", "L2T-memory-writes", "[L2T] Total memory writes"},
  83. {"PTB", "PTB-memory-writes", "[PTB] Total memory writes"},
  84. {"TLB", "TLB-memory-writes", "[TLB] Total memory writes"},
  85. {"CORE", "core-memory-reads", "[CORE] Total memory reads"},
  86. {"L2T", "L2T-memory-reads", "[L2T] Total memory reads"},
  87. {"PTB", "PTB-memory-reads", "[PTB] Total memory reads"},
  88. {"PSE", "PSE-memory-reads", "[PSE] Total memory reads"},
  89. {"TLB", "TLB-memory-reads", "[TLB] Total memory reads"},
  90. {"GMP", "GMP-memory-reads", "[GMP] Total memory reads"},
  91. {"PTB", "PTB-memory-words-writes", "[PTB] Total memory words written"},
  92. {"TLB", "TLB-memory-words-writes", "[TLB] Total memory words written"},
  93. {"PSE", "PSE-memory-words-reads", "[PSE] Total memory words read"},
  94. {"TLB", "TLB-memory-words-reads", "[TLB] Total memory words read"},
  95. {"TMU", "TMU-MRU-hits", "[TMU] Total MRU hits"},
  96. {"CORE", "compute-active-cycles", "[CORE] Compute active cycles"},
  97. };
  98. static const struct v3d_perf_counter_desc v3d_v71_performance_counters[] = {
  99. {"CORE", "cycle-count", "[CORE] Cycle counter"},
  100. {"CORE", "core-active", "[CORE] Bin/Render/Compute active cycles"},
  101. {"CLE", "CLE-bin-thread-active-cycles", "[CLE] Bin thread active cycles"},
  102. {"CLE", "CLE-render-thread-active-cycles", "[CLE] Render thread active cycles"},
  103. {"CORE", "compute-active-cycles", "[CORE] Compute active cycles"},
  104. {"FEP", "FEP-valid-primitives-no-rendered-pixels", "[FEP] Valid primitives that result in no rendered pixels, for all rendered tiles"},
  105. {"FEP", "FEP-valid-primitives-rendered-pixels", "[FEP] Valid primitives for all rendered tiles (primitives may be counted in more than one tile)"},
  106. {"FEP", "FEP-clipped-quads", "[FEP] Early-Z/Near/Far clipped quads"},
  107. {"FEP", "FEP-valid-quads", "[FEP] Valid quads"},
  108. {"TLB", "TLB-quads-not-passing-stencil-test", "[TLB] Quads with no pixels passing the stencil test"},
  109. {"TLB", "TLB-quads-not-passing-z-and-stencil-test", "[TLB] Quads with no pixels passing the Z and stencil tests"},
  110. {"TLB", "TLB-quads-passing-z-and-stencil-test", "[TLB] Quads with any pixels passing the Z and stencil tests"},
  111. {"TLB", "TLB-quads-written-to-color-buffer", "[TLB] Quads with valid pixels written to colour buffer"},
  112. {"TLB", "TLB-partial-quads-written-to-color-buffer", "[TLB] Partial quads written to the colour buffer"},
  113. {"PTB", "PTB-primitives-need-clipping", "[PTB] Primitives that need clipping"},
  114. {"PTB", "PTB-primitives-discarded-outside-viewport", "[PTB] Primitives discarded by being outside the viewport"},
  115. {"PTB", "PTB-primitives-binned", "[PTB] Total primitives binned"},
  116. {"PTB", "PTB-primitives-discarded-reversed", "[PTB] Primitives that are discarded because they are reversed"},
  117. {"QPU", "QPU-total-instr-cache-hit", "[QPU] Total instruction cache hits for all slices"},
  118. {"QPU", "QPU-total-instr-cache-miss", "[QPU] Total instruction cache misses for all slices"},
  119. {"QPU", "QPU-total-uniform-cache-hit", "[QPU] Total uniforms cache hits for all slices"},
  120. {"QPU", "QPU-total-uniform-cache-miss", "[QPU] Total uniforms cache misses for all slices"},
  121. {"TMU", "TMU-active-cycles", "[TMU] Active cycles"},
  122. {"TMU", "TMU-stalled-cycles", "[TMU] Stalled cycles"},
  123. {"TMU", "TMU-total-text-quads-access", "[TMU] Total texture cache accesses"},
  124. {"TMU", "TMU-cache-x4-active-cycles", "[TMU] Cache active cycles for x4 access"},
  125. {"TMU", "TMU-cache-x4-stalled-cycles", "[TMU] Cache stalled cycles for x4 access"},
  126. {"TMU", "TMU-total-text-quads-x4-access", "[TMU] Total texture cache x4 access"},
  127. {"L2T", "L2T-total-cache-hit", "[L2T] Total Level 2 cache hits"},
  128. {"L2T", "L2T-total-cache-miss", "[L2T] Total Level 2 cache misses"},
  129. {"L2T", "L2T-local", "[L2T] Local mode access"},
  130. {"L2T", "L2T-writeback", "[L2T] Writeback"},
  131. {"L2T", "L2T-zero", "[L2T] Zero"},
  132. {"L2T", "L2T-merge", "[L2T] Merge"},
  133. {"L2T", "L2T-fill", "[L2T] Fill"},
  134. {"L2T", "L2T-stalls-no-wid", "[L2T] Stalls because no WID available"},
  135. {"L2T", "L2T-stalls-no-rid", "[L2T] Stalls because no RID available"},
  136. {"L2T", "L2T-stalls-queue-full", "[L2T] Stalls because internal queue full"},
  137. {"L2T", "L2T-stalls-wrightback", "[L2T] Stalls because writeback in flight"},
  138. {"L2T", "L2T-stalls-mem", "[L2T] Stalls because AXI blocks read"},
  139. {"L2T", "L2T-stalls-fill", "[L2T] Stalls because fill pending for victim cache-line"},
  140. {"L2T", "L2T-hitq", "[L2T] Sent request via hit queue"},
  141. {"L2T", "L2T-hitq-full", "[L2T] Sent request via main queue because hit queue is full"},
  142. {"L2T", "L2T-stalls-read-data", "[L2T] Stalls because waiting for data from SDRAM"},
  143. {"L2T", "L2T-TMU-read-hits", "[L2T] TMU read hits"},
  144. {"L2T", "L2T-TMU-read-miss", "[L2T] TMU read misses"},
  145. {"L2T", "L2T-VCD-read-hits", "[L2T] VCD read hits"},
  146. {"L2T", "L2T-VCD-read-miss", "[L2T] VCD read misses"},
  147. {"L2T", "L2T-SLC-read-hits", "[L2T] SLC read hits (all slices)"},
  148. {"L2T", "L2T-SLC-read-miss", "[L2T] SLC read misses (all slices)"},
  149. {"AXI", "AXI-writes-seen-watch-0", "[AXI] Writes seen by watch 0"},
  150. {"AXI", "AXI-reads-seen-watch-0", "[AXI] Reads seen by watch 0"},
  151. {"AXI", "AXI-writes-stalled-seen-watch-0", "[AXI] Write stalls seen by watch 0"},
  152. {"AXI", "AXI-reads-stalled-seen-watch-0", "[AXI] Read stalls seen by watch 0"},
  153. {"AXI", "AXI-write-bytes-seen-watch-0", "[AXI] Total bytes written seen by watch 0"},
  154. {"AXI", "AXI-read-bytes-seen-watch-0", "[AXI] Total bytes read seen by watch 0"},
  155. {"AXI", "AXI-writes-seen-watch-1", "[AXI] Writes seen by watch 1"},
  156. {"AXI", "AXI-reads-seen-watch-1", "[AXI] Reads seen by watch 1"},
  157. {"AXI", "AXI-writes-stalled-seen-watch-1", "[AXI] Write stalls seen by watch 1"},
  158. {"AXI", "AXI-reads-stalled-seen-watch-1", "[AXI] Read stalls seen by watch 1"},
  159. {"AXI", "AXI-write-bytes-seen-watch-1", "[AXI] Total bytes written seen by watch 1"},
  160. {"AXI", "AXI-read-bytes-seen-watch-1", "[AXI] Total bytes read seen by watch 1"},
  161. {"CORE", "core-memory-writes", "[CORE] Total memory writes"},
  162. {"L2T", "L2T-memory-writes", "[L2T] Total memory writes"},
  163. {"PTB", "PTB-memory-writes", "[PTB] Total memory writes"},
  164. {"TLB", "TLB-memory-writes", "[TLB] Total memory writes"},
  165. {"CORE", "core-memory-reads", "[CORE] Total memory reads"},
  166. {"L2T", "L2T-memory-reads", "[L2T] Total memory reads"},
  167. {"PTB", "PTB-memory-reads", "[PTB] Total memory reads"},
  168. {"PSE", "PSE-memory-reads", "[PSE] Total memory reads"},
  169. {"TLB", "TLB-memory-reads", "[TLB] Total memory reads"},
  170. {"PTB", "PTB-memory-words-writes", "[PTB] Total memory words written"},
  171. {"TLB", "TLB-memory-words-writes", "[TLB] Total memory words written"},
  172. {"PSE", "PSE-memory-words-reads", "[PSE] Total memory words read"},
  173. {"TLB", "TLB-memory-words-reads", "[TLB] Total memory words read"},
  174. {"AXI", "AXI-read-trans", "[AXI] Read transaction count"},
  175. {"AXI", "AXI-write-trans", "[AXI] Write transaction count"},
  176. {"AXI", "AXI-read-wait-cycles", "[AXI] Read total wait cycles"},
  177. {"AXI", "AXI-write-wait-cycles", "[AXI] Write total wait cycles"},
  178. {"AXI", "AXI-max-outstanding-reads", "[AXI] Maximum outstanding read transactions"},
  179. {"AXI", "AXI-max-outstanding-writes", "[AXI] Maximum outstanding write transactions"},
  180. {"QPU", "QPU-wait-bubble", "[QPU] Pipeline bubble in qcycles due all threads waiting"},
  181. {"QPU", "QPU-ic-miss-bubble", "[QPU] Pipeline bubble in qcycles due instruction-cache miss"},
  182. {"QPU", "QPU-active", "[QPU] Executed shader instruction"},
  183. {"QPU", "QPU-total-active-clk-cycles-fragment-shading", "[QPU] Total active clock cycles for all QPUs doing fragment shading (counts only when QPU is not stalled)"},
  184. {"QPU", "QPU-stalls", "[QPU] Stalled qcycles executing shader instruction"},
  185. {"QPU", "QPU-total-clk-cycles-waiting-fragment-shading", "[QPU] Total stalled clock cycles for all QPUs doing fragment shading"},
  186. {"QPU", "QPU-stalls-TMU", "[QPU] Stalled qcycles waiting for TMU"},
  187. {"QPU", "QPU-stalls-TLB", "[QPU] Stalled qcycles waiting for TLB"},
  188. {"QPU", "QPU-stalls-VPM", "[QPU] Stalled qcycles waiting for VPM"},
  189. {"QPU", "QPU-stalls-uniforms", "[QPU] Stalled qcycles waiting for uniforms"},
  190. {"QPU", "QPU-stalls-SFU", "[QPU] Stalled qcycles waiting for SFU"},
  191. {"QPU", "QPU-stalls-other", "[QPU] Stalled qcycles waiting for any other reason (vary/W/Z)"},
  192. };
  193. void v3d_perfmon_init(struct v3d_dev *v3d)
  194. {
  195. const struct v3d_perf_counter_desc *counters = NULL;
  196. unsigned int max = 0;
  197. if (v3d->ver >= 71) {
  198. counters = v3d_v71_performance_counters;
  199. max = ARRAY_SIZE(v3d_v71_performance_counters);
  200. } else if (v3d->ver >= 42) {
  201. counters = v3d_v42_performance_counters;
  202. max = ARRAY_SIZE(v3d_v42_performance_counters);
  203. }
  204. v3d->perfmon_info.max_counters = max;
  205. v3d->perfmon_info.counters = counters;
  206. }
  207. void v3d_perfmon_get(struct v3d_perfmon *perfmon)
  208. {
  209. if (perfmon)
  210. refcount_inc(&perfmon->refcnt);
  211. }
  212. void v3d_perfmon_put(struct v3d_perfmon *perfmon)
  213. {
  214. if (perfmon && refcount_dec_and_test(&perfmon->refcnt)) {
  215. mutex_destroy(&perfmon->lock);
  216. kfree(perfmon);
  217. }
  218. }
  219. void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon)
  220. {
  221. unsigned int i;
  222. u32 mask;
  223. u8 ncounters;
  224. if (WARN_ON_ONCE(!perfmon || v3d->active_perfmon))
  225. return;
  226. ncounters = perfmon->ncounters;
  227. mask = GENMASK(ncounters - 1, 0);
  228. for (i = 0; i < ncounters; i++) {
  229. u32 source = i / 4;
  230. u32 channel = V3D_SET_FIELD_VER(perfmon->counters[i], V3D_PCTR_S0,
  231. v3d->ver);
  232. i++;
  233. channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0,
  234. V3D_PCTR_S1, v3d->ver);
  235. i++;
  236. channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0,
  237. V3D_PCTR_S2, v3d->ver);
  238. i++;
  239. channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0,
  240. V3D_PCTR_S3, v3d->ver);
  241. V3D_CORE_WRITE(0, V3D_V4_PCTR_0_SRC_X(source), channel);
  242. }
  243. V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, mask);
  244. V3D_CORE_WRITE(0, V3D_V4_PCTR_0_CLR, mask);
  245. V3D_CORE_WRITE(0, V3D_PCTR_0_OVERFLOW, mask);
  246. v3d->active_perfmon = perfmon;
  247. }
  248. void v3d_perfmon_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon,
  249. bool capture)
  250. {
  251. unsigned int i;
  252. if (!perfmon || !v3d->active_perfmon)
  253. return;
  254. mutex_lock(&perfmon->lock);
  255. if (perfmon != v3d->active_perfmon) {
  256. mutex_unlock(&perfmon->lock);
  257. return;
  258. }
  259. if (capture)
  260. for (i = 0; i < perfmon->ncounters; i++)
  261. perfmon->values[i] += V3D_CORE_READ(0, V3D_PCTR_0_PCTRX(i));
  262. V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, 0);
  263. v3d->active_perfmon = NULL;
  264. mutex_unlock(&perfmon->lock);
  265. }
  266. struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id)
  267. {
  268. struct v3d_perfmon *perfmon;
  269. mutex_lock(&v3d_priv->perfmon.lock);
  270. perfmon = idr_find(&v3d_priv->perfmon.idr, id);
  271. v3d_perfmon_get(perfmon);
  272. mutex_unlock(&v3d_priv->perfmon.lock);
  273. return perfmon;
  274. }
  275. void v3d_perfmon_open_file(struct v3d_file_priv *v3d_priv)
  276. {
  277. mutex_init(&v3d_priv->perfmon.lock);
  278. idr_init_base(&v3d_priv->perfmon.idr, 1);
  279. }
  280. static int v3d_perfmon_idr_del(int id, void *elem, void *data)
  281. {
  282. struct v3d_perfmon *perfmon = elem;
  283. struct v3d_dev *v3d = (struct v3d_dev *)data;
  284. /* If the active perfmon is being destroyed, stop it first */
  285. if (perfmon == v3d->active_perfmon)
  286. v3d_perfmon_stop(v3d, perfmon, false);
  287. v3d_perfmon_put(perfmon);
  288. return 0;
  289. }
  290. void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv)
  291. {
  292. struct v3d_dev *v3d = v3d_priv->v3d;
  293. mutex_lock(&v3d_priv->perfmon.lock);
  294. idr_for_each(&v3d_priv->perfmon.idr, v3d_perfmon_idr_del, v3d);
  295. idr_destroy(&v3d_priv->perfmon.idr);
  296. mutex_unlock(&v3d_priv->perfmon.lock);
  297. mutex_destroy(&v3d_priv->perfmon.lock);
  298. }
  299. int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,
  300. struct drm_file *file_priv)
  301. {
  302. struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
  303. struct drm_v3d_perfmon_create *req = data;
  304. struct v3d_dev *v3d = v3d_priv->v3d;
  305. struct v3d_perfmon *perfmon;
  306. unsigned int i;
  307. int ret;
  308. /* Number of monitored counters cannot exceed HW limits. */
  309. if (req->ncounters > DRM_V3D_MAX_PERF_COUNTERS ||
  310. !req->ncounters)
  311. return -EINVAL;
  312. /* Make sure all counters are valid. */
  313. for (i = 0; i < req->ncounters; i++) {
  314. if (req->counters[i] >= v3d->perfmon_info.max_counters)
  315. return -EINVAL;
  316. }
  317. perfmon = kzalloc(struct_size(perfmon, values, req->ncounters),
  318. GFP_KERNEL);
  319. if (!perfmon)
  320. return -ENOMEM;
  321. for (i = 0; i < req->ncounters; i++)
  322. perfmon->counters[i] = req->counters[i];
  323. perfmon->ncounters = req->ncounters;
  324. refcount_set(&perfmon->refcnt, 1);
  325. mutex_init(&perfmon->lock);
  326. mutex_lock(&v3d_priv->perfmon.lock);
  327. ret = idr_alloc(&v3d_priv->perfmon.idr, perfmon, V3D_PERFMONID_MIN,
  328. V3D_PERFMONID_MAX, GFP_KERNEL);
  329. mutex_unlock(&v3d_priv->perfmon.lock);
  330. if (ret < 0) {
  331. mutex_destroy(&perfmon->lock);
  332. kfree(perfmon);
  333. return ret;
  334. }
  335. req->id = ret;
  336. return 0;
  337. }
  338. int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
  339. struct drm_file *file_priv)
  340. {
  341. struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
  342. struct drm_v3d_perfmon_destroy *req = data;
  343. struct v3d_dev *v3d = v3d_priv->v3d;
  344. struct v3d_perfmon *perfmon;
  345. mutex_lock(&v3d_priv->perfmon.lock);
  346. perfmon = idr_remove(&v3d_priv->perfmon.idr, req->id);
  347. mutex_unlock(&v3d_priv->perfmon.lock);
  348. if (!perfmon)
  349. return -EINVAL;
  350. /* If the active perfmon is being destroyed, stop it first */
  351. if (perfmon == v3d->active_perfmon)
  352. v3d_perfmon_stop(v3d, perfmon, false);
  353. v3d_perfmon_put(perfmon);
  354. return 0;
  355. }
  356. int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
  357. struct drm_file *file_priv)
  358. {
  359. struct v3d_dev *v3d = to_v3d_dev(dev);
  360. struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
  361. struct drm_v3d_perfmon_get_values *req = data;
  362. struct v3d_perfmon *perfmon;
  363. int ret = 0;
  364. if (req->pad != 0)
  365. return -EINVAL;
  366. mutex_lock(&v3d_priv->perfmon.lock);
  367. perfmon = idr_find(&v3d_priv->perfmon.idr, req->id);
  368. v3d_perfmon_get(perfmon);
  369. mutex_unlock(&v3d_priv->perfmon.lock);
  370. if (!perfmon)
  371. return -EINVAL;
  372. v3d_perfmon_stop(v3d, perfmon, true);
  373. if (copy_to_user(u64_to_user_ptr(req->values_ptr), perfmon->values,
  374. perfmon->ncounters * sizeof(u64)))
  375. ret = -EFAULT;
  376. v3d_perfmon_put(perfmon);
  377. return ret;
  378. }
  379. int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data,
  380. struct drm_file *file_priv)
  381. {
  382. struct drm_v3d_perfmon_get_counter *req = data;
  383. struct v3d_dev *v3d = to_v3d_dev(dev);
  384. const struct v3d_perf_counter_desc *counter;
  385. for (int i = 0; i < ARRAY_SIZE(req->reserved); i++) {
  386. if (req->reserved[i] != 0)
  387. return -EINVAL;
  388. }
  389. if (!v3d->perfmon_info.max_counters)
  390. return -EOPNOTSUPP;
  391. /* Make sure that the counter ID is valid */
  392. if (req->counter >= v3d->perfmon_info.max_counters)
  393. return -EINVAL;
  394. counter = &v3d->perfmon_info.counters[req->counter];
  395. strscpy(req->name, counter->name, sizeof(req->name));
  396. strscpy(req->category, counter->category, sizeof(req->category));
  397. strscpy(req->description, counter->description, sizeof(req->description));
  398. return 0;
  399. }