1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2021 Raspberry Pi 4 */ 5 6 #include "v3d_drv.h" 7 #include "v3d_regs.h" 8 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 99 static const struct v3d_perf_counter_desc v3d_v71_performance_counters[] = { 100 {"CORE", "cycle-count", "[CORE] Cycle counter"}, 101 {"CORE", "core-active", "[CORE] Bin/Render/Compute active cycles"}, 102 {"CLE", "CLE-bin-thread-active-cycles", "[CLE] Bin thread active cycles"}, 103 {"CLE", "CLE-render-thread-active-cycles", "[CLE] Render thread active cycles"}, 104 {"CORE", "compute-active-cycles", "[CORE] Compute active cycles"}, 105 {"FEP", "FEP-valid-primitives-no-rendered-pixels", "[FEP] Valid primitives that result in no rendered pixels, for all rendered tiles"}, 106 {"FEP", "FEP-valid-primitives-rendered-pixels", "[FEP] Valid primitives for all rendered tiles (primitives may be counted in more than one tile)"}, 107 {"FEP", "FEP-clipped-quads", "[FEP] Early-Z/Near/Far clipped quads"}, 108 {"FEP", "FEP-valid-quads", "[FEP] Valid quads"}, 109 {"TLB", "TLB-quads-not-passing-stencil-test", "[TLB] Quads with no pixels passing the stencil test"}, 110 {"TLB", "TLB-quads-not-passing-z-and-stencil-test", "[TLB] Quads with no pixels passing the Z and stencil tests"}, 111 {"TLB", "TLB-quads-passing-z-and-stencil-test", "[TLB] Quads with any pixels passing the Z and stencil tests"}, 112 {"TLB", "TLB-quads-written-to-color-buffer", "[TLB] Quads with valid pixels written to colour buffer"}, 113 {"TLB", "TLB-partial-quads-written-to-color-buffer", "[TLB] Partial quads written to the colour buffer"}, 114 {"PTB", "PTB-primitives-need-clipping", "[PTB] Primitives that need clipping"}, 115 {"PTB", "PTB-primitives-discarded-outside-viewport", "[PTB] Primitives discarded by being outside the viewport"}, 116 {"PTB", "PTB-primitives-binned", "[PTB] Total primitives binned"}, 117 {"PTB", "PTB-primitives-discarded-reversed", "[PTB] Primitives that are discarded because they are reversed"}, 118 {"QPU", "QPU-total-instr-cache-hit", "[QPU] Total instruction cache hits for all slices"}, 119 {"QPU", "QPU-total-instr-cache-miss", "[QPU] Total instruction cache misses for all slices"}, 120 {"QPU", "QPU-total-uniform-cache-hit", "[QPU] Total uniforms cache hits for all slices"}, 121 {"QPU", "QPU-total-uniform-cache-miss", "[QPU] Total uniforms cache misses for all slices"}, 122 {"TMU", "TMU-active-cycles", "[TMU] Active cycles"}, 123 {"TMU", "TMU-stalled-cycles", "[TMU] Stalled cycles"}, 124 {"TMU", "TMU-total-text-quads-access", "[TMU] Total texture cache accesses"}, 125 {"TMU", "TMU-cache-x4-active-cycles", "[TMU] Cache active cycles for x4 access"}, 126 {"TMU", "TMU-cache-x4-stalled-cycles", "[TMU] Cache stalled cycles for x4 access"}, 127 {"TMU", "TMU-total-text-quads-x4-access", "[TMU] Total texture cache x4 access"}, 128 {"L2T", "L2T-total-cache-hit", "[L2T] Total Level 2 cache hits"}, 129 {"L2T", "L2T-total-cache-miss", "[L2T] Total Level 2 cache misses"}, 130 {"L2T", "L2T-local", "[L2T] Local mode access"}, 131 {"L2T", "L2T-writeback", "[L2T] Writeback"}, 132 {"L2T", "L2T-zero", "[L2T] Zero"}, 133 {"L2T", "L2T-merge", "[L2T] Merge"}, 134 {"L2T", "L2T-fill", "[L2T] Fill"}, 135 {"L2T", "L2T-stalls-no-wid", "[L2T] Stalls because no WID available"}, 136 {"L2T", "L2T-stalls-no-rid", "[L2T] Stalls because no RID available"}, 137 {"L2T", "L2T-stalls-queue-full", "[L2T] Stalls because internal queue full"}, 138 {"L2T", "L2T-stalls-wrightback", "[L2T] Stalls because writeback in flight"}, 139 {"L2T", "L2T-stalls-mem", "[L2T] Stalls because AXI blocks read"}, 140 {"L2T", "L2T-stalls-fill", "[L2T] Stalls because fill pending for victim cache-line"}, 141 {"L2T", "L2T-hitq", "[L2T] Sent request via hit queue"}, 142 {"L2T", "L2T-hitq-full", "[L2T] Sent request via main queue because hit queue is full"}, 143 {"L2T", "L2T-stalls-read-data", "[L2T] Stalls because waiting for data from SDRAM"}, 144 {"L2T", "L2T-TMU-read-hits", "[L2T] TMU read hits"}, 145 {"L2T", "L2T-TMU-read-miss", "[L2T] TMU read misses"}, 146 {"L2T", "L2T-VCD-read-hits", "[L2T] VCD read hits"}, 147 {"L2T", "L2T-VCD-read-miss", "[L2T] VCD read misses"}, 148 {"L2T", "L2T-SLC-read-hits", "[L2T] SLC read hits (all slices)"}, 149 {"L2T", "L2T-SLC-read-miss", "[L2T] SLC read misses (all slices)"}, 150 {"AXI", "AXI-writes-seen-watch-0", "[AXI] Writes seen by watch 0"}, 151 {"AXI", "AXI-reads-seen-watch-0", "[AXI] Reads seen by watch 0"}, 152 {"AXI", "AXI-writes-stalled-seen-watch-0", "[AXI] Write stalls seen by watch 0"}, 153 {"AXI", "AXI-reads-stalled-seen-watch-0", "[AXI] Read stalls seen by watch 0"}, 154 {"AXI", "AXI-write-bytes-seen-watch-0", "[AXI] Total bytes written seen by watch 0"}, 155 {"AXI", "AXI-read-bytes-seen-watch-0", "[AXI] Total bytes read seen by watch 0"}, 156 {"AXI", "AXI-writes-seen-watch-1", "[AXI] Writes seen by watch 1"}, 157 {"AXI", "AXI-reads-seen-watch-1", "[AXI] Reads seen by watch 1"}, 158 {"AXI", "AXI-writes-stalled-seen-watch-1", "[AXI] Write stalls seen by watch 1"}, 159 {"AXI", "AXI-reads-stalled-seen-watch-1", "[AXI] Read stalls seen by watch 1"}, 160 {"AXI", "AXI-write-bytes-seen-watch-1", "[AXI] Total bytes written seen by watch 1"}, 161 {"AXI", "AXI-read-bytes-seen-watch-1", "[AXI] Total bytes read seen by watch 1"}, 162 {"CORE", "core-memory-writes", "[CORE] Total memory writes"}, 163 {"L2T", "L2T-memory-writes", "[L2T] Total memory writes"}, 164 {"PTB", "PTB-memory-writes", "[PTB] Total memory writes"}, 165 {"TLB", "TLB-memory-writes", "[TLB] Total memory writes"}, 166 {"CORE", "core-memory-reads", "[CORE] Total memory reads"}, 167 {"L2T", "L2T-memory-reads", "[L2T] Total memory reads"}, 168 {"PTB", "PTB-memory-reads", "[PTB] Total memory reads"}, 169 {"PSE", "PSE-memory-reads", "[PSE] Total memory reads"}, 170 {"TLB", "TLB-memory-reads", "[TLB] Total memory reads"}, 171 {"PTB", "PTB-memory-words-writes", "[PTB] Total memory words written"}, 172 {"TLB", "TLB-memory-words-writes", "[TLB] Total memory words written"}, 173 {"PSE", "PSE-memory-words-reads", "[PSE] Total memory words read"}, 174 {"TLB", "TLB-memory-words-reads", "[TLB] Total memory words read"}, 175 {"AXI", "AXI-read-trans", "[AXI] Read transaction count"}, 176 {"AXI", "AXI-write-trans", "[AXI] Write transaction count"}, 177 {"AXI", "AXI-read-wait-cycles", "[AXI] Read total wait cycles"}, 178 {"AXI", "AXI-write-wait-cycles", "[AXI] Write total wait cycles"}, 179 {"AXI", "AXI-max-outstanding-reads", "[AXI] Maximum outstanding read transactions"}, 180 {"AXI", "AXI-max-outstanding-writes", "[AXI] Maximum outstanding write transactions"}, 181 {"QPU", "QPU-wait-bubble", "[QPU] Pipeline bubble in qcycles due all threads waiting"}, 182 {"QPU", "QPU-ic-miss-bubble", "[QPU] Pipeline bubble in qcycles due instruction-cache miss"}, 183 {"QPU", "QPU-active", "[QPU] Executed shader instruction"}, 184 {"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)"}, 185 {"QPU", "QPU-stalls", "[QPU] Stalled qcycles executing shader instruction"}, 186 {"QPU", "QPU-total-clk-cycles-waiting-fragment-shading", "[QPU] Total stalled clock cycles for all QPUs doing fragment shading"}, 187 {"QPU", "QPU-stalls-TMU", "[QPU] Stalled qcycles waiting for TMU"}, 188 {"QPU", "QPU-stalls-TLB", "[QPU] Stalled qcycles waiting for TLB"}, 189 {"QPU", "QPU-stalls-VPM", "[QPU] Stalled qcycles waiting for VPM"}, 190 {"QPU", "QPU-stalls-uniforms", "[QPU] Stalled qcycles waiting for uniforms"}, 191 {"QPU", "QPU-stalls-SFU", "[QPU] Stalled qcycles waiting for SFU"}, 192 {"QPU", "QPU-stalls-other", "[QPU] Stalled qcycles waiting for any other reason (vary/W/Z)"}, 193 }; 194 195 void v3d_perfmon_init(struct v3d_dev *v3d) 196 { 197 const struct v3d_perf_counter_desc *counters = NULL; 198 unsigned int max = 0; 199 200 if (v3d->ver >= V3D_GEN_71) { 201 counters = v3d_v71_performance_counters; 202 max = ARRAY_SIZE(v3d_v71_performance_counters); 203 } else if (v3d->ver >= V3D_GEN_42) { 204 counters = v3d_v42_performance_counters; 205 max = ARRAY_SIZE(v3d_v42_performance_counters); 206 } 207 208 v3d->perfmon_info.max_counters = max; 209 v3d->perfmon_info.counters = counters; 210 } 211 212 void v3d_perfmon_get(struct v3d_perfmon *perfmon) 213 { 214 if (perfmon) 215 refcount_inc(&perfmon->refcnt); 216 } 217 218 void v3d_perfmon_put(struct v3d_perfmon *perfmon) 219 { 220 if (perfmon && refcount_dec_and_test(&perfmon->refcnt)) { 221 mutex_destroy(&perfmon->lock); 222 kfree(perfmon); 223 } 224 } 225 226 void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon) 227 { 228 unsigned int i; 229 u32 mask; 230 u8 ncounters; 231 232 if (WARN_ON_ONCE(!perfmon || v3d->active_perfmon)) 233 return; 234 235 ncounters = perfmon->ncounters; 236 mask = GENMASK(ncounters - 1, 0); 237 238 for (i = 0; i < ncounters; i++) { 239 u32 source = i / 4; 240 u32 channel = V3D_SET_FIELD_VER(perfmon->counters[i], V3D_PCTR_S0, 241 v3d->ver); 242 243 i++; 244 channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0, 245 V3D_PCTR_S1, v3d->ver); 246 i++; 247 channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0, 248 V3D_PCTR_S2, v3d->ver); 249 i++; 250 channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0, 251 V3D_PCTR_S3, v3d->ver); 252 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_SRC_X(source), channel); 253 } 254 255 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, mask); 256 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_CLR, mask); 257 V3D_CORE_WRITE(0, V3D_PCTR_0_OVERFLOW, mask); 258 259 v3d->active_perfmon = perfmon; 260 } 261 262 void v3d_perfmon_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon, 263 bool capture) 264 { 265 unsigned int i; 266 267 if (!perfmon || !v3d->active_perfmon) 268 return; 269 270 mutex_lock(&perfmon->lock); 271 if (perfmon != v3d->active_perfmon) { 272 mutex_unlock(&perfmon->lock); 273 return; 274 } 275 276 if (capture) 277 for (i = 0; i < perfmon->ncounters; i++) 278 perfmon->values[i] += V3D_CORE_READ(0, V3D_PCTR_0_PCTRX(i)); 279 280 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, 0); 281 282 v3d->active_perfmon = NULL; 283 mutex_unlock(&perfmon->lock); 284 } 285 286 struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id) 287 { 288 struct v3d_perfmon *perfmon; 289 290 xa_lock(&v3d_priv->perfmons); 291 perfmon = xa_load(&v3d_priv->perfmons, id); 292 v3d_perfmon_get(perfmon); 293 xa_unlock(&v3d_priv->perfmons); 294 295 return perfmon; 296 } 297 298 void v3d_perfmon_open_file(struct v3d_file_priv *v3d_priv) 299 { 300 xa_init_flags(&v3d_priv->perfmons, XA_FLAGS_ALLOC1); 301 } 302 303 static void v3d_perfmon_delete(struct v3d_file_priv *v3d_priv, 304 struct v3d_perfmon *perfmon) 305 { 306 struct v3d_dev *v3d = v3d_priv->v3d; 307 308 /* If the active perfmon is being destroyed, stop it first */ 309 if (perfmon == v3d->active_perfmon) 310 v3d_perfmon_stop(v3d, perfmon, false); 311 312 /* If the global perfmon is being destroyed, clean it and release 313 * the reference stashed in v3d_perfmon_set_global_ioctl(). 314 */ 315 if (cmpxchg(&v3d->global_perfmon, perfmon, NULL) == perfmon) 316 v3d_perfmon_put(perfmon); 317 318 v3d_perfmon_put(perfmon); 319 } 320 321 void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv) 322 { 323 struct v3d_perfmon *perfmon; 324 unsigned long id; 325 326 xa_for_each(&v3d_priv->perfmons, id, perfmon) 327 v3d_perfmon_delete(v3d_priv, perfmon); 328 329 xa_destroy(&v3d_priv->perfmons); 330 } 331 332 int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data, 333 struct drm_file *file_priv) 334 { 335 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 336 struct drm_v3d_perfmon_create *req = data; 337 struct v3d_dev *v3d = v3d_priv->v3d; 338 struct v3d_perfmon *perfmon; 339 unsigned int i; 340 int ret; 341 u32 id; 342 343 /* Number of monitored counters cannot exceed HW limits. */ 344 if (req->ncounters > DRM_V3D_MAX_PERF_COUNTERS || 345 !req->ncounters) 346 return -EINVAL; 347 348 /* Make sure all counters are valid. */ 349 for (i = 0; i < req->ncounters; i++) { 350 if (req->counters[i] >= v3d->perfmon_info.max_counters) 351 return -EINVAL; 352 } 353 354 perfmon = kzalloc_flex(*perfmon, values, req->ncounters); 355 if (!perfmon) 356 return -ENOMEM; 357 358 for (i = 0; i < req->ncounters; i++) 359 perfmon->counters[i] = req->counters[i]; 360 361 perfmon->ncounters = req->ncounters; 362 363 refcount_set(&perfmon->refcnt, 1); 364 mutex_init(&perfmon->lock); 365 366 ret = xa_alloc(&v3d_priv->perfmons, &id, perfmon, xa_limit_32b, 367 GFP_KERNEL); 368 if (ret < 0) { 369 mutex_destroy(&perfmon->lock); 370 kfree(perfmon); 371 return ret; 372 } 373 374 req->id = id; 375 376 return 0; 377 } 378 379 int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data, 380 struct drm_file *file_priv) 381 { 382 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 383 struct drm_v3d_perfmon_destroy *req = data; 384 struct v3d_perfmon *perfmon; 385 386 perfmon = xa_erase(&v3d_priv->perfmons, req->id); 387 if (!perfmon) 388 return -EINVAL; 389 390 v3d_perfmon_delete(v3d_priv, perfmon); 391 392 return 0; 393 } 394 395 int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data, 396 struct drm_file *file_priv) 397 { 398 struct v3d_dev *v3d = to_v3d_dev(dev); 399 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 400 struct drm_v3d_perfmon_get_values *req = data; 401 struct v3d_perfmon *perfmon; 402 int ret = 0; 403 404 if (req->pad != 0) 405 return -EINVAL; 406 407 perfmon = v3d_perfmon_find(v3d_priv, req->id); 408 if (!perfmon) 409 return -EINVAL; 410 411 v3d_perfmon_stop(v3d, perfmon, true); 412 413 if (copy_to_user(u64_to_user_ptr(req->values_ptr), perfmon->values, 414 perfmon->ncounters * sizeof(u64))) 415 ret = -EFAULT; 416 417 v3d_perfmon_put(perfmon); 418 419 return ret; 420 } 421 422 int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data, 423 struct drm_file *file_priv) 424 { 425 struct drm_v3d_perfmon_get_counter *req = data; 426 struct v3d_dev *v3d = to_v3d_dev(dev); 427 const struct v3d_perf_counter_desc *counter; 428 429 for (int i = 0; i < ARRAY_SIZE(req->reserved); i++) { 430 if (req->reserved[i] != 0) 431 return -EINVAL; 432 } 433 434 if (!v3d->perfmon_info.max_counters) 435 return -EOPNOTSUPP; 436 437 /* Make sure that the counter ID is valid */ 438 if (req->counter >= v3d->perfmon_info.max_counters) 439 return -EINVAL; 440 441 counter = &v3d->perfmon_info.counters[req->counter]; 442 443 strscpy(req->name, counter->name, sizeof(req->name)); 444 strscpy(req->category, counter->category, sizeof(req->category)); 445 strscpy(req->description, counter->description, sizeof(req->description)); 446 447 return 0; 448 } 449 450 int v3d_perfmon_set_global_ioctl(struct drm_device *dev, void *data, 451 struct drm_file *file_priv) 452 { 453 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 454 struct drm_v3d_perfmon_set_global *req = data; 455 struct v3d_dev *v3d = to_v3d_dev(dev); 456 struct v3d_perfmon *perfmon; 457 458 if (req->flags & ~DRM_V3D_PERFMON_CLEAR_GLOBAL) 459 return -EINVAL; 460 461 perfmon = v3d_perfmon_find(v3d_priv, req->id); 462 if (!perfmon) 463 return -EINVAL; 464 465 /* If the request is to clear the global performance monitor */ 466 if (req->flags & DRM_V3D_PERFMON_CLEAR_GLOBAL) { 467 struct v3d_perfmon *old; 468 469 /* DRM_V3D_PERFMON_CLEAR_GLOBAL doesn't check if 470 * v3d->global_perfmon == perfmon. Therefore, there 471 * is no need to keep perfmon's reference. 472 */ 473 v3d_perfmon_put(perfmon); 474 475 old = xchg(&v3d->global_perfmon, NULL); 476 if (!old) 477 return -EINVAL; 478 479 v3d_perfmon_put(old); 480 481 return 0; 482 } 483 484 if (cmpxchg(&v3d->global_perfmon, NULL, perfmon)) { 485 v3d_perfmon_put(perfmon); 486 return -EBUSY; 487 } 488 489 return 0; 490 } 491