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 if (!pm_runtime_get_if_active(v3d->drm.dev)) 236 return; 237 238 ncounters = perfmon->ncounters; 239 mask = GENMASK(ncounters - 1, 0); 240 241 for (i = 0; i < ncounters; i++) { 242 u32 source = i / 4; 243 u32 channel = V3D_SET_FIELD_VER(perfmon->counters[i], V3D_PCTR_S0, 244 v3d->ver); 245 246 i++; 247 channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0, 248 V3D_PCTR_S1, v3d->ver); 249 i++; 250 channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0, 251 V3D_PCTR_S2, v3d->ver); 252 i++; 253 channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0, 254 V3D_PCTR_S3, v3d->ver); 255 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_SRC_X(source), channel); 256 } 257 258 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, mask); 259 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_CLR, mask); 260 V3D_CORE_WRITE(0, V3D_PCTR_0_OVERFLOW, mask); 261 262 v3d->active_perfmon = perfmon; 263 264 v3d_pm_runtime_put(v3d); 265 } 266 267 void v3d_perfmon_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon, 268 bool capture) 269 { 270 unsigned int i; 271 272 if (!perfmon || !v3d->active_perfmon) 273 return; 274 275 mutex_lock(&perfmon->lock); 276 if (perfmon != v3d->active_perfmon) 277 goto out; 278 279 if (!pm_runtime_get_if_active(v3d->drm.dev)) 280 goto out_clear; 281 282 if (capture) 283 for (i = 0; i < perfmon->ncounters; i++) 284 perfmon->values[i] += V3D_CORE_READ(0, V3D_PCTR_0_PCTRX(i)); 285 286 V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, 0); 287 288 v3d_pm_runtime_put(v3d); 289 290 out_clear: 291 v3d->active_perfmon = NULL; 292 out: 293 mutex_unlock(&perfmon->lock); 294 } 295 296 struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id) 297 { 298 struct v3d_perfmon *perfmon; 299 300 xa_lock(&v3d_priv->perfmons); 301 perfmon = xa_load(&v3d_priv->perfmons, id); 302 v3d_perfmon_get(perfmon); 303 xa_unlock(&v3d_priv->perfmons); 304 305 return perfmon; 306 } 307 308 void v3d_perfmon_open_file(struct v3d_file_priv *v3d_priv) 309 { 310 xa_init_flags(&v3d_priv->perfmons, XA_FLAGS_ALLOC1); 311 } 312 313 static void v3d_perfmon_delete(struct v3d_file_priv *v3d_priv, 314 struct v3d_perfmon *perfmon) 315 { 316 struct v3d_dev *v3d = v3d_priv->v3d; 317 318 /* If the active perfmon is being destroyed, stop it first */ 319 if (perfmon == v3d->active_perfmon) 320 v3d_perfmon_stop(v3d, perfmon, false); 321 322 /* If the global perfmon is being destroyed, clean it and release 323 * the reference stashed in v3d_perfmon_set_global_ioctl(). 324 */ 325 if (cmpxchg(&v3d->global_perfmon, perfmon, NULL) == perfmon) 326 v3d_perfmon_put(perfmon); 327 328 v3d_perfmon_put(perfmon); 329 } 330 331 void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv) 332 { 333 struct v3d_perfmon *perfmon; 334 unsigned long id; 335 336 xa_for_each(&v3d_priv->perfmons, id, perfmon) 337 v3d_perfmon_delete(v3d_priv, perfmon); 338 339 xa_destroy(&v3d_priv->perfmons); 340 } 341 342 int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data, 343 struct drm_file *file_priv) 344 { 345 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 346 struct drm_v3d_perfmon_create *req = data; 347 struct v3d_dev *v3d = v3d_priv->v3d; 348 struct v3d_perfmon *perfmon; 349 unsigned int i; 350 int ret; 351 u32 id; 352 353 /* Number of monitored counters cannot exceed HW limits. */ 354 if (req->ncounters > DRM_V3D_MAX_PERF_COUNTERS || 355 !req->ncounters) 356 return -EINVAL; 357 358 /* Make sure all counters are valid. */ 359 for (i = 0; i < req->ncounters; i++) { 360 if (req->counters[i] >= v3d->perfmon_info.max_counters) 361 return -EINVAL; 362 } 363 364 perfmon = kzalloc_flex(*perfmon, values, req->ncounters); 365 if (!perfmon) 366 return -ENOMEM; 367 368 for (i = 0; i < req->ncounters; i++) 369 perfmon->counters[i] = req->counters[i]; 370 371 perfmon->ncounters = req->ncounters; 372 373 refcount_set(&perfmon->refcnt, 1); 374 mutex_init(&perfmon->lock); 375 376 ret = xa_alloc(&v3d_priv->perfmons, &id, perfmon, xa_limit_32b, 377 GFP_KERNEL); 378 if (ret < 0) { 379 mutex_destroy(&perfmon->lock); 380 kfree(perfmon); 381 return ret; 382 } 383 384 req->id = id; 385 386 return 0; 387 } 388 389 int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data, 390 struct drm_file *file_priv) 391 { 392 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 393 struct drm_v3d_perfmon_destroy *req = data; 394 struct v3d_perfmon *perfmon; 395 396 perfmon = xa_erase(&v3d_priv->perfmons, req->id); 397 if (!perfmon) 398 return -EINVAL; 399 400 v3d_perfmon_delete(v3d_priv, perfmon); 401 402 return 0; 403 } 404 405 int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data, 406 struct drm_file *file_priv) 407 { 408 struct v3d_dev *v3d = to_v3d_dev(dev); 409 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 410 struct drm_v3d_perfmon_get_values *req = data; 411 struct v3d_perfmon *perfmon; 412 int ret = 0; 413 414 if (req->pad != 0) 415 return -EINVAL; 416 417 perfmon = v3d_perfmon_find(v3d_priv, req->id); 418 if (!perfmon) 419 return -EINVAL; 420 421 v3d_perfmon_stop(v3d, perfmon, true); 422 423 if (copy_to_user(u64_to_user_ptr(req->values_ptr), perfmon->values, 424 perfmon->ncounters * sizeof(u64))) 425 ret = -EFAULT; 426 427 v3d_perfmon_put(perfmon); 428 429 return ret; 430 } 431 432 int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data, 433 struct drm_file *file_priv) 434 { 435 struct drm_v3d_perfmon_get_counter *req = data; 436 struct v3d_dev *v3d = to_v3d_dev(dev); 437 const struct v3d_perf_counter_desc *counter; 438 439 for (int i = 0; i < ARRAY_SIZE(req->reserved); i++) { 440 if (req->reserved[i] != 0) 441 return -EINVAL; 442 } 443 444 if (!v3d->perfmon_info.max_counters) 445 return -EOPNOTSUPP; 446 447 /* Make sure that the counter ID is valid */ 448 if (req->counter >= v3d->perfmon_info.max_counters) 449 return -EINVAL; 450 451 counter = &v3d->perfmon_info.counters[req->counter]; 452 453 strscpy(req->name, counter->name, sizeof(req->name)); 454 strscpy(req->category, counter->category, sizeof(req->category)); 455 strscpy(req->description, counter->description, sizeof(req->description)); 456 457 return 0; 458 } 459 460 int v3d_perfmon_set_global_ioctl(struct drm_device *dev, void *data, 461 struct drm_file *file_priv) 462 { 463 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 464 struct drm_v3d_perfmon_set_global *req = data; 465 struct v3d_dev *v3d = to_v3d_dev(dev); 466 struct v3d_perfmon *perfmon; 467 468 if (req->flags & ~DRM_V3D_PERFMON_CLEAR_GLOBAL) 469 return -EINVAL; 470 471 perfmon = v3d_perfmon_find(v3d_priv, req->id); 472 if (!perfmon) 473 return -EINVAL; 474 475 /* If the request is to clear the global performance monitor */ 476 if (req->flags & DRM_V3D_PERFMON_CLEAR_GLOBAL) { 477 struct v3d_perfmon *old; 478 479 /* DRM_V3D_PERFMON_CLEAR_GLOBAL doesn't check if 480 * v3d->global_perfmon == perfmon. Therefore, there 481 * is no need to keep perfmon's reference. 482 */ 483 v3d_perfmon_put(perfmon); 484 485 old = xchg(&v3d->global_perfmon, NULL); 486 if (!old) 487 return -EINVAL; 488 489 v3d_perfmon_put(old); 490 491 return 0; 492 } 493 494 if (cmpxchg(&v3d->global_perfmon, NULL, perfmon)) { 495 v3d_perfmon_put(perfmon); 496 return -EBUSY; 497 } 498 499 return 0; 500 } 501