xref: /linux/drivers/gpu/drm/v3d/v3d_perfmon.c (revision aa8a8c7e4641ebcef02dd6fa387ca334857367f4)
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 		atomic_dec(&perfmon->v3d->perfmon_state.nperfmons);
222 		kfree(perfmon);
223 	}
224 }
225 
226 static void v3d_perfmon_hw_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon)
227 {
228 	u8 ncounters = perfmon->ncounters;
229 	u32 mask = GENMASK(ncounters - 1, 0);
230 	unsigned int i;
231 
232 	for (i = 0; i < ncounters; i++) {
233 		u32 source = i / 4;
234 		u32 channel = V3D_SET_FIELD_VER(perfmon->counters[i], V3D_PCTR_S0,
235 						v3d->ver);
236 
237 		i++;
238 		channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0,
239 					     V3D_PCTR_S1, v3d->ver);
240 		i++;
241 		channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0,
242 					     V3D_PCTR_S2, v3d->ver);
243 		i++;
244 		channel |= V3D_SET_FIELD_VER(i < ncounters ? perfmon->counters[i] : 0,
245 					     V3D_PCTR_S3, v3d->ver);
246 		V3D_CORE_WRITE(0, V3D_V4_PCTR_0_SRC_X(source), channel);
247 	}
248 
249 	V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, mask);
250 	V3D_CORE_WRITE(0, V3D_V4_PCTR_0_CLR, mask);
251 	V3D_CORE_WRITE(0, V3D_PCTR_0_OVERFLOW, mask);
252 }
253 
254 static void v3d_perfmon_hw_capture(struct v3d_dev *v3d, struct v3d_perfmon *perfmon)
255 {
256 	u32 mask = GENMASK(perfmon->ncounters - 1, 0);
257 
258 	for (int i = 0; i < perfmon->ncounters; i++)
259 		perfmon->values[i] += V3D_CORE_READ(0, V3D_PCTR_0_PCTRX(i));
260 
261 	V3D_CORE_WRITE(0, V3D_V4_PCTR_0_CLR, mask);
262 }
263 
264 static void v3d_perfmon_hw_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon,
265 				bool capture)
266 {
267 	if (capture)
268 		v3d_perfmon_hw_capture(v3d, perfmon);
269 
270 	V3D_CORE_WRITE(0, V3D_V4_PCTR_0_EN, 0);
271 }
272 
273 void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon)
274 {
275 	guard(spinlock_irqsave)(&v3d->perfmon_state.lock);
276 
277 	if (!perfmon || v3d->global_perfmon)
278 		return;
279 
280 	/* Cross-queue serialization should have drained any previous perfmon
281 	 * job before this one runs.
282 	 */
283 	if (WARN_ON_ONCE(v3d->perfmon_state.active))
284 		return;
285 
286 	if (!pm_runtime_get_if_active(v3d->drm.dev))
287 		return;
288 
289 	v3d_perfmon_hw_start(v3d, perfmon);
290 	v3d->perfmon_state.active = perfmon;
291 
292 	v3d_pm_runtime_put(v3d);
293 }
294 
295 static void v3d_perfmon_capture_locked(struct v3d_dev *v3d,
296 				       struct v3d_perfmon *perfmon)
297 {
298 	lockdep_assert_held(&v3d->perfmon_state.lock);
299 
300 	if (!perfmon || perfmon != v3d->perfmon_state.active)
301 		return;
302 
303 	if (!pm_runtime_get_if_active(v3d->drm.dev))
304 		return;
305 
306 	v3d_perfmon_hw_capture(v3d, perfmon);
307 	v3d_pm_runtime_put(v3d);
308 }
309 
310 static void
311 v3d_perfmon_stop_locked(struct v3d_dev *v3d, struct v3d_perfmon *perfmon,
312 			bool capture)
313 {
314 	lockdep_assert_held(&v3d->perfmon_state.lock);
315 
316 	if (!perfmon || perfmon != v3d->perfmon_state.active)
317 		return;
318 
319 	v3d->perfmon_state.active = NULL;
320 
321 	/* If the device is suspended, the HW has already stopped counting. */
322 	if (!pm_runtime_get_if_active(v3d->drm.dev))
323 		return;
324 
325 	v3d_perfmon_hw_stop(v3d, perfmon, capture);
326 	v3d_pm_runtime_put(v3d);
327 }
328 
329 void v3d_perfmon_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon,
330 		      bool capture)
331 {
332 	if (!perfmon)
333 		return;
334 
335 	guard(spinlock_irqsave)(&v3d->perfmon_state.lock);
336 	v3d_perfmon_stop_locked(v3d, perfmon, capture);
337 }
338 
339 void
340 v3d_perfmon_suspend(struct v3d_dev *v3d)
341 {
342 	guard(spinlock_irqsave)(&v3d->perfmon_state.lock);
343 
344 	if (!v3d->perfmon_state.active)
345 		return;
346 
347 	v3d_perfmon_hw_stop(v3d, v3d->perfmon_state.active, true);
348 }
349 
350 void
351 v3d_perfmon_resume(struct v3d_dev *v3d)
352 {
353 	guard(spinlock_irqsave)(&v3d->perfmon_state.lock);
354 
355 	if (!v3d->perfmon_state.active)
356 		return;
357 
358 	v3d_perfmon_hw_start(v3d, v3d->perfmon_state.active);
359 }
360 
361 struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id)
362 {
363 	struct v3d_perfmon *perfmon;
364 
365 	xa_lock(&v3d_priv->perfmons);
366 	perfmon = xa_load(&v3d_priv->perfmons, id);
367 	v3d_perfmon_get(perfmon);
368 	xa_unlock(&v3d_priv->perfmons);
369 
370 	return perfmon;
371 }
372 
373 void v3d_perfmon_open_file(struct v3d_file_priv *v3d_priv)
374 {
375 	xa_init_flags(&v3d_priv->perfmons, XA_FLAGS_ALLOC1);
376 }
377 
378 static void v3d_perfmon_delete(struct v3d_file_priv *v3d_priv,
379 			       struct v3d_perfmon *perfmon)
380 {
381 	struct v3d_dev *v3d = v3d_priv->v3d;
382 
383 	/* If the active perfmon is being destroyed, stop it first */
384 	scoped_guard(spinlock_irqsave, &v3d->perfmon_state.lock) {
385 		v3d_perfmon_stop_locked(v3d, perfmon, false);
386 
387 		/* If the global perfmon is being destroyed, clean it and release
388 		 * the reference stashed in v3d_perfmon_set_global_ioctl().
389 		 */
390 		if (v3d->global_perfmon == perfmon) {
391 			v3d_perfmon_put(v3d->global_perfmon);
392 			v3d->global_perfmon = NULL;
393 		}
394 	}
395 
396 	v3d_perfmon_put(perfmon);
397 }
398 
399 void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv)
400 {
401 	struct v3d_perfmon *perfmon;
402 	unsigned long id;
403 
404 	xa_for_each(&v3d_priv->perfmons, id, perfmon)
405 		v3d_perfmon_delete(v3d_priv, perfmon);
406 
407 	xa_destroy(&v3d_priv->perfmons);
408 }
409 
410 int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,
411 			     struct drm_file *file_priv)
412 {
413 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
414 	struct drm_v3d_perfmon_create *req = data;
415 	struct v3d_dev *v3d = v3d_priv->v3d;
416 	struct v3d_perfmon *perfmon;
417 	unsigned int i;
418 	int ret;
419 	u32 id;
420 
421 	/* Number of monitored counters cannot exceed HW limits. */
422 	if (req->ncounters > DRM_V3D_MAX_PERF_COUNTERS ||
423 	    !req->ncounters)
424 		return -EINVAL;
425 
426 	/* Make sure all counters are valid. */
427 	for (i = 0; i < req->ncounters; i++) {
428 		if (req->counters[i] >= v3d->perfmon_info.max_counters)
429 			return -EINVAL;
430 	}
431 
432 	perfmon = kzalloc_flex(*perfmon, values, req->ncounters);
433 	if (!perfmon)
434 		return -ENOMEM;
435 
436 	for (i = 0; i < req->ncounters; i++)
437 		perfmon->counters[i] = req->counters[i];
438 
439 	perfmon->ncounters = req->ncounters;
440 	perfmon->v3d = v3d;
441 
442 	refcount_set(&perfmon->refcnt, 1);
443 	atomic_inc(&v3d->perfmon_state.nperfmons);
444 
445 	ret = xa_alloc(&v3d_priv->perfmons, &id, perfmon, xa_limit_32b,
446 		       GFP_KERNEL);
447 	if (ret < 0) {
448 		v3d_perfmon_put(perfmon);
449 		return ret;
450 	}
451 
452 	req->id = id;
453 
454 	return 0;
455 }
456 
457 int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
458 			      struct drm_file *file_priv)
459 {
460 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
461 	struct drm_v3d_perfmon_destroy *req = data;
462 	struct v3d_perfmon *perfmon;
463 
464 	perfmon = xa_erase(&v3d_priv->perfmons, req->id);
465 	if (!perfmon)
466 		return -EINVAL;
467 
468 	v3d_perfmon_delete(v3d_priv, perfmon);
469 
470 	return 0;
471 }
472 
473 int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
474 				 struct drm_file *file_priv)
475 {
476 	struct v3d_dev *v3d = to_v3d_dev(dev);
477 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
478 	struct drm_v3d_perfmon_get_values *req = data;
479 	u64 values[DRM_V3D_MAX_PERF_COUNTERS];
480 	struct v3d_perfmon *perfmon;
481 	size_t size;
482 	int ret = 0;
483 
484 	if (req->pad != 0)
485 		return -EINVAL;
486 
487 	perfmon = v3d_perfmon_find(v3d_priv, req->id);
488 	if (!perfmon)
489 		return -EINVAL;
490 
491 	size = perfmon->ncounters * sizeof(u64);
492 
493 	scoped_guard(spinlock_irqsave, &v3d->perfmon_state.lock) {
494 		v3d_perfmon_capture_locked(v3d, perfmon);
495 		memcpy(values, perfmon->values, size);
496 	}
497 
498 	if (copy_to_user(u64_to_user_ptr(req->values_ptr), values, size))
499 		ret = -EFAULT;
500 
501 	v3d_perfmon_put(perfmon);
502 
503 	return ret;
504 }
505 
506 int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data,
507 				  struct drm_file *file_priv)
508 {
509 	struct drm_v3d_perfmon_get_counter *req = data;
510 	struct v3d_dev *v3d = to_v3d_dev(dev);
511 	const struct v3d_perf_counter_desc *counter;
512 
513 	for (int i = 0; i < ARRAY_SIZE(req->reserved); i++) {
514 		if (req->reserved[i] != 0)
515 			return -EINVAL;
516 	}
517 
518 	if (!v3d->perfmon_info.max_counters)
519 		return -EOPNOTSUPP;
520 
521 	/* Make sure that the counter ID is valid */
522 	if (req->counter >= v3d->perfmon_info.max_counters)
523 		return -EINVAL;
524 
525 	counter = &v3d->perfmon_info.counters[req->counter];
526 
527 	strscpy(req->name, counter->name, sizeof(req->name));
528 	strscpy(req->category, counter->category, sizeof(req->category));
529 	strscpy(req->description, counter->description, sizeof(req->description));
530 
531 	return 0;
532 }
533 
534 int v3d_perfmon_set_global_ioctl(struct drm_device *dev, void *data,
535 				 struct drm_file *file_priv)
536 {
537 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
538 	struct drm_v3d_perfmon_set_global *req = data;
539 	struct v3d_dev *v3d = to_v3d_dev(dev);
540 	struct v3d_perfmon *perfmon;
541 
542 	if (req->flags & ~DRM_V3D_PERFMON_CLEAR_GLOBAL)
543 		return -EINVAL;
544 
545 	perfmon = v3d_perfmon_find(v3d_priv, req->id);
546 	if (!perfmon)
547 		return -EINVAL;
548 
549 	/* If the request is to clear the global performance monitor */
550 	if (req->flags & DRM_V3D_PERFMON_CLEAR_GLOBAL) {
551 		struct v3d_perfmon *old;
552 
553 		/* DRM_V3D_PERFMON_CLEAR_GLOBAL doesn't check if
554 		 * v3d->global_perfmon == perfmon. Therefore, there
555 		 * is no need to keep perfmon's reference.
556 		 */
557 		v3d_perfmon_put(perfmon);
558 
559 		scoped_guard(spinlock_irqsave, &v3d->perfmon_state.lock) {
560 			old = v3d->global_perfmon;
561 			if (!old)
562 				return -EINVAL;
563 
564 			v3d_perfmon_stop_locked(v3d, old, true);
565 			v3d->global_perfmon = NULL;
566 		}
567 
568 		v3d_perfmon_put(old);
569 
570 		return 0;
571 	}
572 
573 	scoped_guard(spinlock_irqsave, &v3d->perfmon_state.lock) {
574 		if (v3d->perfmon_state.active || v3d->global_perfmon) {
575 			v3d_perfmon_put(perfmon);
576 			return -EBUSY;
577 		}
578 
579 		v3d->global_perfmon = perfmon;
580 		v3d->perfmon_state.active = perfmon;
581 
582 		/* If the device is suspended, v3d_perfmon_resume() will
583 		 * program the HW on the next resume.
584 		 */
585 		if (pm_runtime_get_if_active(v3d->drm.dev)) {
586 			v3d_perfmon_hw_start(v3d, perfmon);
587 			v3d_pm_runtime_put(v3d);
588 		}
589 	}
590 
591 	return 0;
592 }
593