1 /*
2 * Copyright © 2015-2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Robert Bragg <robert@sixbynine.org>
25 */
26
27
28 /**
29 * DOC: i915 Perf Overview
30 *
31 * Gen graphics supports a large number of performance counters that can help
32 * driver and application developers understand and optimize their use of the
33 * GPU.
34 *
35 * This i915 perf interface enables userspace to configure and open a file
36 * descriptor representing a stream of GPU metrics which can then be read() as
37 * a stream of sample records.
38 *
39 * The interface is particularly suited to exposing buffered metrics that are
40 * captured by DMA from the GPU, unsynchronized with and unrelated to the CPU.
41 *
42 * Streams representing a single context are accessible to applications with a
43 * corresponding drm file descriptor, such that OpenGL can use the interface
44 * without special privileges. Access to system-wide metrics requires root
45 * privileges by default, unless changed via the dev.i915.perf_event_paranoid
46 * sysctl option.
47 *
48 */
49
50 /**
51 * DOC: i915 Perf History and Comparison with Core Perf
52 *
53 * The interface was initially inspired by the core Perf infrastructure but
54 * some notable differences are:
55 *
56 * i915 perf file descriptors represent a "stream" instead of an "event"; where
57 * a perf event primarily corresponds to a single 64bit value, while a stream
58 * might sample sets of tightly-coupled counters, depending on the
59 * configuration. For example the Gen OA unit isn't designed to support
60 * orthogonal configurations of individual counters; it's configured for a set
61 * of related counters. Samples for an i915 perf stream capturing OA metrics
62 * will include a set of counter values packed in a compact HW specific format.
63 * The OA unit supports a number of different packing formats which can be
64 * selected by the user opening the stream. Perf has support for grouping
65 * events, but each event in the group is configured, validated and
66 * authenticated individually with separate system calls.
67 *
68 * i915 perf stream configurations are provided as an array of u64 (key,value)
69 * pairs, instead of a fixed struct with multiple miscellaneous config members,
70 * interleaved with event-type specific members.
71 *
72 * i915 perf doesn't support exposing metrics via an mmap'd circular buffer.
73 * The supported metrics are being written to memory by the GPU unsynchronized
74 * with the CPU, using HW specific packing formats for counter sets. Sometimes
75 * the constraints on HW configuration require reports to be filtered before it
76 * would be acceptable to expose them to unprivileged applications - to hide
77 * the metrics of other processes/contexts. For these use cases a read() based
78 * interface is a good fit, and provides an opportunity to filter data as it
79 * gets copied from the GPU mapped buffers to userspace buffers.
80 *
81 *
82 * Issues hit with first prototype based on Core Perf
83 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84 *
85 * The first prototype of this driver was based on the core perf
86 * infrastructure, and while we did make that mostly work, with some changes to
87 * perf, we found we were breaking or working around too many assumptions baked
88 * into perf's currently cpu centric design.
89 *
90 * In the end we didn't see a clear benefit to making perf's implementation and
91 * interface more complex by changing design assumptions while we knew we still
92 * wouldn't be able to use any existing perf based userspace tools.
93 *
94 * Also considering the Gen specific nature of the Observability hardware and
95 * how userspace will sometimes need to combine i915 perf OA metrics with
96 * side-band OA data captured via MI_REPORT_PERF_COUNT commands; we're
97 * expecting the interface to be used by a platform specific userspace such as
98 * OpenGL or tools. This is to say; we aren't inherently missing out on having
99 * a standard vendor/architecture agnostic interface by not using perf.
100 *
101 *
102 * For posterity, in case we might re-visit trying to adapt core perf to be
103 * better suited to exposing i915 metrics these were the main pain points we
104 * hit:
105 *
106 * - The perf based OA PMU driver broke some significant design assumptions:
107 *
108 * Existing perf pmus are used for profiling work on a cpu and we were
109 * introducing the idea of _IS_DEVICE pmus with different security
110 * implications, the need to fake cpu-related data (such as user/kernel
111 * registers) to fit with perf's current design, and adding _DEVICE records
112 * as a way to forward device-specific status records.
113 *
114 * The OA unit writes reports of counters into a circular buffer, without
115 * involvement from the CPU, making our PMU driver the first of a kind.
116 *
117 * Given the way we were periodically forward data from the GPU-mapped, OA
118 * buffer to perf's buffer, those bursts of sample writes looked to perf like
119 * we were sampling too fast and so we had to subvert its throttling checks.
120 *
121 * Perf supports groups of counters and allows those to be read via
122 * transactions internally but transactions currently seem designed to be
123 * explicitly initiated from the cpu (say in response to a userspace read())
124 * and while we could pull a report out of the OA buffer we can't
125 * trigger a report from the cpu on demand.
126 *
127 * Related to being report based; the OA counters are configured in HW as a
128 * set while perf generally expects counter configurations to be orthogonal.
129 * Although counters can be associated with a group leader as they are
130 * opened, there's no clear precedent for being able to provide group-wide
131 * configuration attributes (for example we want to let userspace choose the
132 * OA unit report format used to capture all counters in a set, or specify a
133 * GPU context to filter metrics on). We avoided using perf's grouping
134 * feature and forwarded OA reports to userspace via perf's 'raw' sample
135 * field. This suited our userspace well considering how coupled the counters
136 * are when dealing with normalizing. It would be inconvenient to split
137 * counters up into separate events, only to require userspace to recombine
138 * them. For Mesa it's also convenient to be forwarded raw, periodic reports
139 * for combining with the side-band raw reports it captures using
140 * MI_REPORT_PERF_COUNT commands.
141 *
142 * - As a side note on perf's grouping feature; there was also some concern
143 * that using PERF_FORMAT_GROUP as a way to pack together counter values
144 * would quite drastically inflate our sample sizes, which would likely
145 * lower the effective sampling resolutions we could use when the available
146 * memory bandwidth is limited.
147 *
148 * With the OA unit's report formats, counters are packed together as 32
149 * or 40bit values, with the largest report size being 256 bytes.
150 *
151 * PERF_FORMAT_GROUP values are 64bit, but there doesn't appear to be a
152 * documented ordering to the values, implying PERF_FORMAT_ID must also be
153 * used to add a 64bit ID before each value; giving 16 bytes per counter.
154 *
155 * Related to counter orthogonality; we can't time share the OA unit, while
156 * event scheduling is a central design idea within perf for allowing
157 * userspace to open + enable more events than can be configured in HW at any
158 * one time. The OA unit is not designed to allow re-configuration while in
159 * use. We can't reconfigure the OA unit without losing internal OA unit
160 * state which we can't access explicitly to save and restore. Reconfiguring
161 * the OA unit is also relatively slow, involving ~100 register writes. From
162 * userspace Mesa also depends on a stable OA configuration when emitting
163 * MI_REPORT_PERF_COUNT commands and importantly the OA unit can't be
164 * disabled while there are outstanding MI_RPC commands lest we hang the
165 * command streamer.
166 *
167 * The contents of sample records aren't extensible by device drivers (i.e.
168 * the sample_type bits). As an example; Sourab Gupta had been looking to
169 * attach GPU timestamps to our OA samples. We were shoehorning OA reports
170 * into sample records by using the 'raw' field, but it's tricky to pack more
171 * than one thing into this field because events/core.c currently only lets a
172 * pmu give a single raw data pointer plus len which will be copied into the
173 * ring buffer. To include more than the OA report we'd have to copy the
174 * report into an intermediate larger buffer. I'd been considering allowing a
175 * vector of data+len values to be specified for copying the raw data, but
176 * it felt like a kludge to being using the raw field for this purpose.
177 *
178 * - It felt like our perf based PMU was making some technical compromises
179 * just for the sake of using perf:
180 *
181 * perf_event_open() requires events to either relate to a pid or a specific
182 * cpu core, while our device pmu related to neither. Events opened with a
183 * pid will be automatically enabled/disabled according to the scheduling of
184 * that process - so not appropriate for us. When an event is related to a
185 * cpu id, perf ensures pmu methods will be invoked via an inter process
186 * interrupt on that core. To avoid invasive changes our userspace opened OA
187 * perf events for a specific cpu. This was workable but it meant the
188 * majority of the OA driver ran in atomic context, including all OA report
189 * forwarding, which wasn't really necessary in our case and seems to make
190 * our locking requirements somewhat complex as we handled the interaction
191 * with the rest of the i915 driver.
192 */
193
194 #include <linux/anon_inodes.h>
195 #include <linux/nospec.h>
196 #include <linux/sizes.h>
197 #include <linux/uuid.h>
198
199 #include "gem/i915_gem_context.h"
200 #include "gem/i915_gem_internal.h"
201 #include "gt/intel_engine_pm.h"
202 #include "gt/intel_engine_regs.h"
203 #include "gt/intel_engine_user.h"
204 #include "gt/intel_execlists_submission.h"
205 #include "gt/intel_gpu_commands.h"
206 #include "gt/intel_gt.h"
207 #include "gt/intel_gt_clock_utils.h"
208 #include "gt/intel_gt_mcr.h"
209 #include "gt/intel_gt_regs.h"
210 #include "gt/intel_lrc.h"
211 #include "gt/intel_lrc_reg.h"
212 #include "gt/intel_rc6.h"
213 #include "gt/intel_ring.h"
214 #include "gt/uc/intel_guc_slpc.h"
215
216 #include "i915_drv.h"
217 #include "i915_file_private.h"
218 #include "i915_perf.h"
219 #include "i915_perf_oa_regs.h"
220 #include "i915_reg.h"
221
222 /* HW requires this to be a power of two, between 128k and 16M, though driver
223 * is currently generally designed assuming the largest 16M size is used such
224 * that the overflow cases are unlikely in normal operation.
225 */
226 #define OA_BUFFER_SIZE SZ_16M
227
228 #define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
229
230 /**
231 * DOC: OA Tail Pointer Race
232 *
233 * There's a HW race condition between OA unit tail pointer register updates and
234 * writes to memory whereby the tail pointer can sometimes get ahead of what's
235 * been written out to the OA buffer so far (in terms of what's visible to the
236 * CPU).
237 *
238 * Although this can be observed explicitly while copying reports to userspace
239 * by checking for a zeroed report-id field in tail reports, we want to account
240 * for this earlier, as part of the oa_buffer_check_unlocked to avoid lots of
241 * redundant read() attempts.
242 *
243 * We workaround this issue in oa_buffer_check_unlocked() by reading the reports
244 * in the OA buffer, starting from the tail reported by the HW until we find a
245 * report with its first 2 dwords not 0 meaning its previous report is
246 * completely in memory and ready to be read. Those dwords are also set to 0
247 * once read and the whole buffer is cleared upon OA buffer initialization. The
248 * first dword is the reason for this report while the second is the timestamp,
249 * making the chances of having those 2 fields at 0 fairly unlikely. A more
250 * detailed explanation is available in oa_buffer_check_unlocked().
251 *
252 * Most of the implementation details for this workaround are in
253 * oa_buffer_check_unlocked() and _append_oa_reports()
254 *
255 * Note for posterity: previously the driver used to define an effective tail
256 * pointer that lagged the real pointer by a 'tail margin' measured in bytes
257 * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
258 * This was flawed considering that the OA unit may also automatically generate
259 * non-periodic reports (such as on context switch) or the OA unit may be
260 * enabled without any periodic sampling.
261 */
262 #define OA_TAIL_MARGIN_NSEC 100000ULL
263 #define INVALID_TAIL_PTR 0xffffffff
264
265 /* The default frequency for checking whether the OA unit has written new
266 * reports to the circular OA buffer...
267 */
268 #define DEFAULT_POLL_FREQUENCY_HZ 200
269 #define DEFAULT_POLL_PERIOD_NS (NSEC_PER_SEC / DEFAULT_POLL_FREQUENCY_HZ)
270
271 /* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
272 static u32 i915_perf_stream_paranoid = true;
273
274 /* The maximum exponent the hardware accepts is 63 (essentially it selects one
275 * of the 64bit timestamp bits to trigger reports from) but there's currently
276 * no known use case for sampling as infrequently as once per 47 thousand years.
277 *
278 * Since the timestamps included in OA reports are only 32bits it seems
279 * reasonable to limit the OA exponent where it's still possible to account for
280 * overflow in OA report timestamps.
281 */
282 #define OA_EXPONENT_MAX 31
283
284 #define INVALID_CTX_ID 0xffffffff
285
286 /* On Gen8+ automatically triggered OA reports include a 'reason' field... */
287 #define OAREPORT_REASON_MASK 0x3f
288 #define OAREPORT_REASON_MASK_EXTENDED 0x7f
289 #define OAREPORT_REASON_SHIFT 19
290 #define OAREPORT_REASON_TIMER (1<<0)
291 #define OAREPORT_REASON_CTX_SWITCH (1<<3)
292 #define OAREPORT_REASON_CLK_RATIO (1<<5)
293
294 #define HAS_MI_SET_PREDICATE(i915) (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50))
295
296 /* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
297 *
298 * The highest sampling frequency we can theoretically program the OA unit
299 * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
300 *
301 * Initialized just before we register the sysctl parameter.
302 */
303 static int oa_sample_rate_hard_limit;
304
305 /* Theoretically we can program the OA unit to sample every 160ns but don't
306 * allow that by default unless root...
307 *
308 * The default threshold of 100000Hz is based on perf's similar
309 * kernel.perf_event_max_sample_rate sysctl parameter.
310 */
311 static u32 i915_oa_max_sample_rate = 100000;
312
313 /* XXX: beware if future OA HW adds new report formats that the current
314 * code assumes all reports have a power-of-two size and ~(size - 1) can
315 * be used as a mask to align the OA tail pointer.
316 */
317 static const struct i915_oa_format oa_formats[I915_OA_FORMAT_MAX] = {
318 [I915_OA_FORMAT_A13] = { 0, 64 },
319 [I915_OA_FORMAT_A29] = { 1, 128 },
320 [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
321 /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
322 [I915_OA_FORMAT_B4_C8] = { 4, 64 },
323 [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
324 [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
325 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
326 [I915_OA_FORMAT_A12] = { 0, 64 },
327 [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
328 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
329 [I915_OAR_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
330 [I915_OA_FORMAT_A24u40_A14u32_B8_C8] = { 5, 256 },
331 [I915_OAM_FORMAT_MPEC8u64_B8_C8] = { 1, 192, TYPE_OAM, HDR_64_BIT },
332 [I915_OAM_FORMAT_MPEC8u32_B8_C8] = { 2, 128, TYPE_OAM, HDR_64_BIT },
333 };
334
335 static const u32 mtl_oa_base[] = {
336 [PERF_GROUP_OAM_SAMEDIA_0] = 0x393000,
337 };
338
339 #define SAMPLE_OA_REPORT (1<<0)
340
341 /**
342 * struct perf_open_properties - for validated properties given to open a stream
343 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
344 * @single_context: Whether a single or all gpu contexts should be monitored
345 * @hold_preemption: Whether the preemption is disabled for the filtered
346 * context
347 * @ctx_handle: A gem ctx handle for use with @single_context
348 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
349 * @oa_format: An OA unit HW report format
350 * @oa_periodic: Whether to enable periodic OA unit sampling
351 * @oa_period_exponent: The OA unit sampling period is derived from this
352 * @engine: The engine (typically rcs0) being monitored by the OA unit
353 * @has_sseu: Whether @sseu was specified by userspace
354 * @sseu: internal SSEU configuration computed either from the userspace
355 * specified configuration in the opening parameters or a default value
356 * (see get_default_sseu_config())
357 * @poll_oa_period: The period in nanoseconds at which the CPU will check for OA
358 * data availability
359 *
360 * As read_properties_unlocked() enumerates and validates the properties given
361 * to open a stream of metrics the configuration is built up in the structure
362 * which starts out zero initialized.
363 */
364 struct perf_open_properties {
365 u32 sample_flags;
366
367 u64 single_context:1;
368 u64 hold_preemption:1;
369 u64 ctx_handle;
370
371 /* OA sampling state */
372 int metrics_set;
373 int oa_format;
374 bool oa_periodic;
375 int oa_period_exponent;
376
377 struct intel_engine_cs *engine;
378
379 bool has_sseu;
380 struct intel_sseu sseu;
381
382 u64 poll_oa_period;
383 };
384
385 struct i915_oa_config_bo {
386 struct llist_node node;
387
388 struct i915_oa_config *oa_config;
389 struct i915_vma *vma;
390 };
391
392 static struct ctl_table_header *sysctl_header;
393
394 static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer);
395
i915_oa_config_release(struct kref * ref)396 void i915_oa_config_release(struct kref *ref)
397 {
398 struct i915_oa_config *oa_config =
399 container_of(ref, typeof(*oa_config), ref);
400
401 kfree(oa_config->flex_regs);
402 kfree(oa_config->b_counter_regs);
403 kfree(oa_config->mux_regs);
404
405 kfree_rcu(oa_config, rcu);
406 }
407
408 struct i915_oa_config *
i915_perf_get_oa_config(struct i915_perf * perf,int metrics_set)409 i915_perf_get_oa_config(struct i915_perf *perf, int metrics_set)
410 {
411 struct i915_oa_config *oa_config;
412
413 rcu_read_lock();
414 oa_config = idr_find(&perf->metrics_idr, metrics_set);
415 if (oa_config)
416 oa_config = i915_oa_config_get(oa_config);
417 rcu_read_unlock();
418
419 return oa_config;
420 }
421
free_oa_config_bo(struct i915_oa_config_bo * oa_bo)422 static void free_oa_config_bo(struct i915_oa_config_bo *oa_bo)
423 {
424 i915_oa_config_put(oa_bo->oa_config);
425 i915_vma_put(oa_bo->vma);
426 kfree(oa_bo);
427 }
428
429 static inline const
__oa_regs(struct i915_perf_stream * stream)430 struct i915_perf_regs *__oa_regs(struct i915_perf_stream *stream)
431 {
432 return &stream->engine->oa_group->regs;
433 }
434
gen12_oa_hw_tail_read(struct i915_perf_stream * stream)435 static u32 gen12_oa_hw_tail_read(struct i915_perf_stream *stream)
436 {
437 struct intel_uncore *uncore = stream->uncore;
438
439 return intel_uncore_read(uncore, __oa_regs(stream)->oa_tail_ptr) &
440 GEN12_OAG_OATAILPTR_MASK;
441 }
442
gen8_oa_hw_tail_read(struct i915_perf_stream * stream)443 static u32 gen8_oa_hw_tail_read(struct i915_perf_stream *stream)
444 {
445 struct intel_uncore *uncore = stream->uncore;
446
447 return intel_uncore_read(uncore, GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
448 }
449
gen7_oa_hw_tail_read(struct i915_perf_stream * stream)450 static u32 gen7_oa_hw_tail_read(struct i915_perf_stream *stream)
451 {
452 struct intel_uncore *uncore = stream->uncore;
453 u32 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
454
455 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
456 }
457
458 #define oa_report_header_64bit(__s) \
459 ((__s)->oa_buffer.format->header == HDR_64_BIT)
460
oa_report_id(struct i915_perf_stream * stream,void * report)461 static u64 oa_report_id(struct i915_perf_stream *stream, void *report)
462 {
463 return oa_report_header_64bit(stream) ? *(u64 *)report : *(u32 *)report;
464 }
465
oa_report_reason(struct i915_perf_stream * stream,void * report)466 static u64 oa_report_reason(struct i915_perf_stream *stream, void *report)
467 {
468 return (oa_report_id(stream, report) >> OAREPORT_REASON_SHIFT) &
469 (GRAPHICS_VER(stream->perf->i915) == 12 ?
470 OAREPORT_REASON_MASK_EXTENDED :
471 OAREPORT_REASON_MASK);
472 }
473
oa_report_id_clear(struct i915_perf_stream * stream,u32 * report)474 static void oa_report_id_clear(struct i915_perf_stream *stream, u32 *report)
475 {
476 if (oa_report_header_64bit(stream))
477 *(u64 *)report = 0;
478 else
479 *report = 0;
480 }
481
oa_report_ctx_invalid(struct i915_perf_stream * stream,void * report)482 static bool oa_report_ctx_invalid(struct i915_perf_stream *stream, void *report)
483 {
484 return !(oa_report_id(stream, report) &
485 stream->perf->gen8_valid_ctx_bit);
486 }
487
oa_timestamp(struct i915_perf_stream * stream,void * report)488 static u64 oa_timestamp(struct i915_perf_stream *stream, void *report)
489 {
490 return oa_report_header_64bit(stream) ?
491 *((u64 *)report + 1) :
492 *((u32 *)report + 1);
493 }
494
oa_timestamp_clear(struct i915_perf_stream * stream,u32 * report)495 static void oa_timestamp_clear(struct i915_perf_stream *stream, u32 *report)
496 {
497 if (oa_report_header_64bit(stream))
498 *(u64 *)&report[2] = 0;
499 else
500 report[1] = 0;
501 }
502
oa_context_id(struct i915_perf_stream * stream,u32 * report)503 static u32 oa_context_id(struct i915_perf_stream *stream, u32 *report)
504 {
505 u32 ctx_id = oa_report_header_64bit(stream) ? report[4] : report[2];
506
507 return ctx_id & stream->specific_ctx_id_mask;
508 }
509
oa_context_id_squash(struct i915_perf_stream * stream,u32 * report)510 static void oa_context_id_squash(struct i915_perf_stream *stream, u32 *report)
511 {
512 if (oa_report_header_64bit(stream))
513 report[4] = INVALID_CTX_ID;
514 else
515 report[2] = INVALID_CTX_ID;
516 }
517
518 /**
519 * oa_buffer_check_unlocked - check for data and update tail ptr state
520 * @stream: i915 stream instance
521 *
522 * This is either called via fops (for blocking reads in user ctx) or the poll
523 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
524 * if there is data available for userspace to read.
525 *
526 * This function is central to providing a workaround for the OA unit tail
527 * pointer having a race with respect to what data is visible to the CPU.
528 * It is responsible for reading tail pointers from the hardware and giving
529 * the pointers time to 'age' before they are made available for reading.
530 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
531 *
532 * Besides returning true when there is data available to read() this function
533 * also updates the tail in the oa_buffer object.
534 *
535 * Note: It's safe to read OA config state here unlocked, assuming that this is
536 * only called while the stream is enabled, while the global OA configuration
537 * can't be modified.
538 *
539 * Returns: %true if the OA buffer contains data, else %false
540 */
oa_buffer_check_unlocked(struct i915_perf_stream * stream)541 static bool oa_buffer_check_unlocked(struct i915_perf_stream *stream)
542 {
543 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
544 int report_size = stream->oa_buffer.format->size;
545 u32 head, tail, read_tail;
546 unsigned long flags;
547 bool pollin;
548 u32 hw_tail;
549 u32 partial_report_size;
550
551 /* We have to consider the (unlikely) possibility that read() errors
552 * could result in an OA buffer reset which might reset the head and
553 * tail state.
554 */
555 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
556
557 hw_tail = stream->perf->ops.oa_hw_tail_read(stream);
558
559 /* The tail pointer increases in 64 byte increments, not in report_size
560 * steps. Also the report size may not be a power of 2. Compute
561 * potentially partially landed report in the OA buffer
562 */
563 partial_report_size = OA_TAKEN(hw_tail, stream->oa_buffer.tail);
564 partial_report_size %= report_size;
565
566 /* Subtract partial amount off the tail */
567 hw_tail = OA_TAKEN(hw_tail, partial_report_size);
568
569 /* NB: The head we observe here might effectively be a little
570 * out of date. If a read() is in progress, the head could be
571 * anywhere between this head and stream->oa_buffer.tail.
572 */
573 head = stream->oa_buffer.head - gtt_offset;
574 read_tail = stream->oa_buffer.tail - gtt_offset;
575
576 tail = hw_tail;
577
578 /* Walk the stream backward until we find a report with report
579 * id and timestmap not at 0. Since the circular buffer pointers
580 * progress by increments of 64 bytes and that reports can be up
581 * to 256 bytes long, we can't tell whether a report has fully
582 * landed in memory before the report id and timestamp of the
583 * following report have effectively landed.
584 *
585 * This is assuming that the writes of the OA unit land in
586 * memory in the order they were written to.
587 * If not : (╯°□°)╯︵ ┻━┻
588 */
589 while (OA_TAKEN(tail, read_tail) >= report_size) {
590 void *report = stream->oa_buffer.vaddr + tail;
591
592 if (oa_report_id(stream, report) ||
593 oa_timestamp(stream, report))
594 break;
595
596 tail = (tail - report_size) & (OA_BUFFER_SIZE - 1);
597 }
598
599 if (OA_TAKEN(hw_tail, tail) > report_size &&
600 __ratelimit(&stream->perf->tail_pointer_race))
601 drm_notice(&stream->uncore->i915->drm,
602 "unlanded report(s) head=0x%x tail=0x%x hw_tail=0x%x\n",
603 head, tail, hw_tail);
604
605 stream->oa_buffer.tail = gtt_offset + tail;
606
607 pollin = OA_TAKEN(stream->oa_buffer.tail,
608 stream->oa_buffer.head) >= report_size;
609
610 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
611
612 return pollin;
613 }
614
615 /**
616 * append_oa_status - Appends a status record to a userspace read() buffer.
617 * @stream: An i915-perf stream opened for OA metrics
618 * @buf: destination buffer given by userspace
619 * @count: the number of bytes userspace wants to read
620 * @offset: (inout): the current position for writing into @buf
621 * @type: The kind of status to report to userspace
622 *
623 * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
624 * into the userspace read() buffer.
625 *
626 * The @buf @offset will only be updated on success.
627 *
628 * Returns: 0 on success, negative error code on failure.
629 */
append_oa_status(struct i915_perf_stream * stream,char __user * buf,size_t count,size_t * offset,enum drm_i915_perf_record_type type)630 static int append_oa_status(struct i915_perf_stream *stream,
631 char __user *buf,
632 size_t count,
633 size_t *offset,
634 enum drm_i915_perf_record_type type)
635 {
636 struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
637
638 if ((count - *offset) < header.size)
639 return -ENOSPC;
640
641 if (copy_to_user(buf + *offset, &header, sizeof(header)))
642 return -EFAULT;
643
644 (*offset) += header.size;
645
646 return 0;
647 }
648
649 /**
650 * append_oa_sample - Copies single OA report into userspace read() buffer.
651 * @stream: An i915-perf stream opened for OA metrics
652 * @buf: destination buffer given by userspace
653 * @count: the number of bytes userspace wants to read
654 * @offset: (inout): the current position for writing into @buf
655 * @report: A single OA report to (optionally) include as part of the sample
656 *
657 * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
658 * properties when opening a stream, tracked as `stream->sample_flags`. This
659 * function copies the requested components of a single sample to the given
660 * read() @buf.
661 *
662 * The @buf @offset will only be updated on success.
663 *
664 * Returns: 0 on success, negative error code on failure.
665 */
append_oa_sample(struct i915_perf_stream * stream,char __user * buf,size_t count,size_t * offset,const u8 * report)666 static int append_oa_sample(struct i915_perf_stream *stream,
667 char __user *buf,
668 size_t count,
669 size_t *offset,
670 const u8 *report)
671 {
672 int report_size = stream->oa_buffer.format->size;
673 struct drm_i915_perf_record_header header;
674 int report_size_partial;
675 u8 *oa_buf_end;
676
677 header.type = DRM_I915_PERF_RECORD_SAMPLE;
678 header.pad = 0;
679 header.size = stream->sample_size;
680
681 if ((count - *offset) < header.size)
682 return -ENOSPC;
683
684 buf += *offset;
685 if (copy_to_user(buf, &header, sizeof(header)))
686 return -EFAULT;
687 buf += sizeof(header);
688
689 oa_buf_end = stream->oa_buffer.vaddr + OA_BUFFER_SIZE;
690 report_size_partial = oa_buf_end - report;
691
692 if (report_size_partial < report_size) {
693 if (copy_to_user(buf, report, report_size_partial))
694 return -EFAULT;
695 buf += report_size_partial;
696
697 if (copy_to_user(buf, stream->oa_buffer.vaddr,
698 report_size - report_size_partial))
699 return -EFAULT;
700 } else if (copy_to_user(buf, report, report_size)) {
701 return -EFAULT;
702 }
703
704 (*offset) += header.size;
705
706 return 0;
707 }
708
709 /**
710 * gen8_append_oa_reports - Copies all buffered OA reports into
711 * userspace read() buffer.
712 * @stream: An i915-perf stream opened for OA metrics
713 * @buf: destination buffer given by userspace
714 * @count: the number of bytes userspace wants to read
715 * @offset: (inout): the current position for writing into @buf
716 *
717 * Notably any error condition resulting in a short read (-%ENOSPC or
718 * -%EFAULT) will be returned even though one or more records may
719 * have been successfully copied. In this case it's up to the caller
720 * to decide if the error should be squashed before returning to
721 * userspace.
722 *
723 * Note: reports are consumed from the head, and appended to the
724 * tail, so the tail chases the head?... If you think that's mad
725 * and back-to-front you're not alone, but this follows the
726 * Gen PRM naming convention.
727 *
728 * Returns: 0 on success, negative error code on failure.
729 */
gen8_append_oa_reports(struct i915_perf_stream * stream,char __user * buf,size_t count,size_t * offset)730 static int gen8_append_oa_reports(struct i915_perf_stream *stream,
731 char __user *buf,
732 size_t count,
733 size_t *offset)
734 {
735 struct intel_uncore *uncore = stream->uncore;
736 int report_size = stream->oa_buffer.format->size;
737 u8 *oa_buf_base = stream->oa_buffer.vaddr;
738 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
739 u32 mask = (OA_BUFFER_SIZE - 1);
740 size_t start_offset = *offset;
741 unsigned long flags;
742 u32 head, tail;
743 int ret = 0;
744
745 if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
746 return -EIO;
747
748 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
749
750 head = stream->oa_buffer.head;
751 tail = stream->oa_buffer.tail;
752
753 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
754
755 /*
756 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
757 * while indexing relative to oa_buf_base.
758 */
759 head -= gtt_offset;
760 tail -= gtt_offset;
761
762 /*
763 * An out of bounds or misaligned head or tail pointer implies a driver
764 * bug since we validate + align the tail pointers we read from the
765 * hardware and we are in full control of the head pointer which should
766 * only be incremented by multiples of the report size.
767 */
768 if (drm_WARN_ONCE(&uncore->i915->drm,
769 head > OA_BUFFER_SIZE ||
770 tail > OA_BUFFER_SIZE,
771 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
772 head, tail))
773 return -EIO;
774
775
776 for (/* none */;
777 OA_TAKEN(tail, head);
778 head = (head + report_size) & mask) {
779 u8 *report = oa_buf_base + head;
780 u32 *report32 = (void *)report;
781 u32 ctx_id;
782 u64 reason;
783
784 /*
785 * The reason field includes flags identifying what
786 * triggered this specific report (mostly timer
787 * triggered or e.g. due to a context switch).
788 */
789 reason = oa_report_reason(stream, report);
790 ctx_id = oa_context_id(stream, report32);
791
792 /*
793 * Squash whatever is in the CTX_ID field if it's marked as
794 * invalid to be sure we avoid false-positive, single-context
795 * filtering below...
796 *
797 * Note: that we don't clear the valid_ctx_bit so userspace can
798 * understand that the ID has been squashed by the kernel.
799 *
800 * Update:
801 *
802 * On XEHP platforms the behavior of context id valid bit has
803 * changed compared to prior platforms. To describe this, we
804 * define a few terms:
805 *
806 * context-switch-report: This is a report with the reason type
807 * being context-switch. It is generated when a context switches
808 * out.
809 *
810 * context-valid-bit: A bit that is set in the report ID field
811 * to indicate that a valid context has been loaded.
812 *
813 * gpu-idle: A condition characterized by a
814 * context-switch-report with context-valid-bit set to 0.
815 *
816 * On prior platforms, context-id-valid bit is set to 0 only
817 * when GPU goes idle. In all other reports, it is set to 1.
818 *
819 * On XEHP platforms, context-valid-bit is set to 1 in a context
820 * switch report if a new context switched in. For all other
821 * reports it is set to 0.
822 *
823 * This change in behavior causes an issue with MMIO triggered
824 * reports. MMIO triggered reports have the markers in the
825 * context ID field and the context-valid-bit is 0. The logic
826 * below to squash the context ID would render the report
827 * useless since the user will not be able to find it in the OA
828 * buffer. Since MMIO triggered reports exist only on XEHP,
829 * we should avoid squashing these for XEHP platforms.
830 */
831
832 if (oa_report_ctx_invalid(stream, report) &&
833 GRAPHICS_VER_FULL(stream->engine->i915) < IP_VER(12, 50)) {
834 ctx_id = INVALID_CTX_ID;
835 oa_context_id_squash(stream, report32);
836 }
837
838 /*
839 * NB: For Gen 8 the OA unit no longer supports clock gating
840 * off for a specific context and the kernel can't securely
841 * stop the counters from updating as system-wide / global
842 * values.
843 *
844 * Automatic reports now include a context ID so reports can be
845 * filtered on the cpu but it's not worth trying to
846 * automatically subtract/hide counter progress for other
847 * contexts while filtering since we can't stop userspace
848 * issuing MI_REPORT_PERF_COUNT commands which would still
849 * provide a side-band view of the real values.
850 *
851 * To allow userspace (such as Mesa/GL_INTEL_performance_query)
852 * to normalize counters for a single filtered context then it
853 * needs be forwarded bookend context-switch reports so that it
854 * can track switches in between MI_REPORT_PERF_COUNT commands
855 * and can itself subtract/ignore the progress of counters
856 * associated with other contexts. Note that the hardware
857 * automatically triggers reports when switching to a new
858 * context which are tagged with the ID of the newly active
859 * context. To avoid the complexity (and likely fragility) of
860 * reading ahead while parsing reports to try and minimize
861 * forwarding redundant context switch reports (i.e. between
862 * other, unrelated contexts) we simply elect to forward them
863 * all.
864 *
865 * We don't rely solely on the reason field to identify context
866 * switches since it's not-uncommon for periodic samples to
867 * identify a switch before any 'context switch' report.
868 */
869 if (!stream->ctx ||
870 stream->specific_ctx_id == ctx_id ||
871 stream->oa_buffer.last_ctx_id == stream->specific_ctx_id ||
872 reason & OAREPORT_REASON_CTX_SWITCH) {
873
874 /*
875 * While filtering for a single context we avoid
876 * leaking the IDs of other contexts.
877 */
878 if (stream->ctx &&
879 stream->specific_ctx_id != ctx_id) {
880 oa_context_id_squash(stream, report32);
881 }
882
883 ret = append_oa_sample(stream, buf, count, offset,
884 report);
885 if (ret)
886 break;
887
888 stream->oa_buffer.last_ctx_id = ctx_id;
889 }
890
891 if (is_power_of_2(report_size)) {
892 /*
893 * Clear out the report id and timestamp as a means
894 * to detect unlanded reports.
895 */
896 oa_report_id_clear(stream, report32);
897 oa_timestamp_clear(stream, report32);
898 } else {
899 u8 *oa_buf_end = stream->oa_buffer.vaddr +
900 OA_BUFFER_SIZE;
901 u32 part = oa_buf_end - (u8 *)report32;
902
903 /* Zero out the entire report */
904 if (report_size <= part) {
905 memset(report32, 0, report_size);
906 } else {
907 memset(report32, 0, part);
908 memset(oa_buf_base, 0, report_size - part);
909 }
910 }
911 }
912
913 if (start_offset != *offset) {
914 i915_reg_t oaheadptr;
915
916 oaheadptr = GRAPHICS_VER(stream->perf->i915) == 12 ?
917 __oa_regs(stream)->oa_head_ptr :
918 GEN8_OAHEADPTR;
919
920 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
921
922 /*
923 * We removed the gtt_offset for the copy loop above, indexing
924 * relative to oa_buf_base so put back here...
925 */
926 head += gtt_offset;
927 intel_uncore_write(uncore, oaheadptr,
928 head & GEN12_OAG_OAHEADPTR_MASK);
929 stream->oa_buffer.head = head;
930
931 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
932 }
933
934 return ret;
935 }
936
937 /**
938 * gen8_oa_read - copy status records then buffered OA reports
939 * @stream: An i915-perf stream opened for OA metrics
940 * @buf: destination buffer given by userspace
941 * @count: the number of bytes userspace wants to read
942 * @offset: (inout): the current position for writing into @buf
943 *
944 * Checks OA unit status registers and if necessary appends corresponding
945 * status records for userspace (such as for a buffer full condition) and then
946 * initiate appending any buffered OA reports.
947 *
948 * Updates @offset according to the number of bytes successfully copied into
949 * the userspace buffer.
950 *
951 * NB: some data may be successfully copied to the userspace buffer
952 * even if an error is returned, and this is reflected in the
953 * updated @offset.
954 *
955 * Returns: zero on success or a negative error code
956 */
gen8_oa_read(struct i915_perf_stream * stream,char __user * buf,size_t count,size_t * offset)957 static int gen8_oa_read(struct i915_perf_stream *stream,
958 char __user *buf,
959 size_t count,
960 size_t *offset)
961 {
962 struct intel_uncore *uncore = stream->uncore;
963 u32 oastatus;
964 i915_reg_t oastatus_reg;
965 int ret;
966
967 if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
968 return -EIO;
969
970 oastatus_reg = GRAPHICS_VER(stream->perf->i915) == 12 ?
971 __oa_regs(stream)->oa_status :
972 GEN8_OASTATUS;
973
974 oastatus = intel_uncore_read(uncore, oastatus_reg);
975
976 /*
977 * We treat OABUFFER_OVERFLOW as a significant error:
978 *
979 * Although theoretically we could handle this more gracefully
980 * sometimes, some Gens don't correctly suppress certain
981 * automatically triggered reports in this condition and so we
982 * have to assume that old reports are now being trampled
983 * over.
984 *
985 * Considering how we don't currently give userspace control
986 * over the OA buffer size and always configure a large 16MB
987 * buffer, then a buffer overflow does anyway likely indicate
988 * that something has gone quite badly wrong.
989 */
990 if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
991 ret = append_oa_status(stream, buf, count, offset,
992 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
993 if (ret)
994 return ret;
995
996 drm_dbg(&stream->perf->i915->drm,
997 "OA buffer overflow (exponent = %d): force restart\n",
998 stream->period_exponent);
999
1000 stream->perf->ops.oa_disable(stream);
1001 stream->perf->ops.oa_enable(stream);
1002
1003 /*
1004 * Note: .oa_enable() is expected to re-init the oabuffer and
1005 * reset GEN8_OASTATUS for us
1006 */
1007 oastatus = intel_uncore_read(uncore, oastatus_reg);
1008 }
1009
1010 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
1011 ret = append_oa_status(stream, buf, count, offset,
1012 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1013 if (ret)
1014 return ret;
1015
1016 intel_uncore_rmw(uncore, oastatus_reg,
1017 GEN8_OASTATUS_COUNTER_OVERFLOW |
1018 GEN8_OASTATUS_REPORT_LOST,
1019 IS_GRAPHICS_VER(uncore->i915, 8, 11) ?
1020 (GEN8_OASTATUS_HEAD_POINTER_WRAP |
1021 GEN8_OASTATUS_TAIL_POINTER_WRAP) : 0);
1022 }
1023
1024 return gen8_append_oa_reports(stream, buf, count, offset);
1025 }
1026
1027 /**
1028 * gen7_append_oa_reports - Copies all buffered OA reports into
1029 * userspace read() buffer.
1030 * @stream: An i915-perf stream opened for OA metrics
1031 * @buf: destination buffer given by userspace
1032 * @count: the number of bytes userspace wants to read
1033 * @offset: (inout): the current position for writing into @buf
1034 *
1035 * Notably any error condition resulting in a short read (-%ENOSPC or
1036 * -%EFAULT) will be returned even though one or more records may
1037 * have been successfully copied. In this case it's up to the caller
1038 * to decide if the error should be squashed before returning to
1039 * userspace.
1040 *
1041 * Note: reports are consumed from the head, and appended to the
1042 * tail, so the tail chases the head?... If you think that's mad
1043 * and back-to-front you're not alone, but this follows the
1044 * Gen PRM naming convention.
1045 *
1046 * Returns: 0 on success, negative error code on failure.
1047 */
gen7_append_oa_reports(struct i915_perf_stream * stream,char __user * buf,size_t count,size_t * offset)1048 static int gen7_append_oa_reports(struct i915_perf_stream *stream,
1049 char __user *buf,
1050 size_t count,
1051 size_t *offset)
1052 {
1053 struct intel_uncore *uncore = stream->uncore;
1054 int report_size = stream->oa_buffer.format->size;
1055 u8 *oa_buf_base = stream->oa_buffer.vaddr;
1056 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
1057 u32 mask = (OA_BUFFER_SIZE - 1);
1058 size_t start_offset = *offset;
1059 unsigned long flags;
1060 u32 head, tail;
1061 int ret = 0;
1062
1063 if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
1064 return -EIO;
1065
1066 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1067
1068 head = stream->oa_buffer.head;
1069 tail = stream->oa_buffer.tail;
1070
1071 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1072
1073 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
1074 * while indexing relative to oa_buf_base.
1075 */
1076 head -= gtt_offset;
1077 tail -= gtt_offset;
1078
1079 /* An out of bounds or misaligned head or tail pointer implies a driver
1080 * bug since we validate + align the tail pointers we read from the
1081 * hardware and we are in full control of the head pointer which should
1082 * only be incremented by multiples of the report size (notably also
1083 * all a power of two).
1084 */
1085 if (drm_WARN_ONCE(&uncore->i915->drm,
1086 head > OA_BUFFER_SIZE || head % report_size ||
1087 tail > OA_BUFFER_SIZE || tail % report_size,
1088 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
1089 head, tail))
1090 return -EIO;
1091
1092
1093 for (/* none */;
1094 OA_TAKEN(tail, head);
1095 head = (head + report_size) & mask) {
1096 u8 *report = oa_buf_base + head;
1097 u32 *report32 = (void *)report;
1098
1099 /* All the report sizes factor neatly into the buffer
1100 * size so we never expect to see a report split
1101 * between the beginning and end of the buffer.
1102 *
1103 * Given the initial alignment check a misalignment
1104 * here would imply a driver bug that would result
1105 * in an overrun.
1106 */
1107 if (drm_WARN_ON(&uncore->i915->drm,
1108 (OA_BUFFER_SIZE - head) < report_size)) {
1109 drm_err(&uncore->i915->drm,
1110 "Spurious OA head ptr: non-integral report offset\n");
1111 break;
1112 }
1113
1114 /* The report-ID field for periodic samples includes
1115 * some undocumented flags related to what triggered
1116 * the report and is never expected to be zero so we
1117 * can check that the report isn't invalid before
1118 * copying it to userspace...
1119 */
1120 if (report32[0] == 0) {
1121 if (__ratelimit(&stream->perf->spurious_report_rs))
1122 drm_notice(&uncore->i915->drm,
1123 "Skipping spurious, invalid OA report\n");
1124 continue;
1125 }
1126
1127 ret = append_oa_sample(stream, buf, count, offset, report);
1128 if (ret)
1129 break;
1130
1131 /* Clear out the first 2 dwords as a mean to detect unlanded
1132 * reports.
1133 */
1134 report32[0] = 0;
1135 report32[1] = 0;
1136 }
1137
1138 if (start_offset != *offset) {
1139 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1140
1141 /* We removed the gtt_offset for the copy loop above, indexing
1142 * relative to oa_buf_base so put back here...
1143 */
1144 head += gtt_offset;
1145
1146 intel_uncore_write(uncore, GEN7_OASTATUS2,
1147 (head & GEN7_OASTATUS2_HEAD_MASK) |
1148 GEN7_OASTATUS2_MEM_SELECT_GGTT);
1149 stream->oa_buffer.head = head;
1150
1151 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1152 }
1153
1154 return ret;
1155 }
1156
1157 /**
1158 * gen7_oa_read - copy status records then buffered OA reports
1159 * @stream: An i915-perf stream opened for OA metrics
1160 * @buf: destination buffer given by userspace
1161 * @count: the number of bytes userspace wants to read
1162 * @offset: (inout): the current position for writing into @buf
1163 *
1164 * Checks Gen 7 specific OA unit status registers and if necessary appends
1165 * corresponding status records for userspace (such as for a buffer full
1166 * condition) and then initiate appending any buffered OA reports.
1167 *
1168 * Updates @offset according to the number of bytes successfully copied into
1169 * the userspace buffer.
1170 *
1171 * Returns: zero on success or a negative error code
1172 */
gen7_oa_read(struct i915_perf_stream * stream,char __user * buf,size_t count,size_t * offset)1173 static int gen7_oa_read(struct i915_perf_stream *stream,
1174 char __user *buf,
1175 size_t count,
1176 size_t *offset)
1177 {
1178 struct intel_uncore *uncore = stream->uncore;
1179 u32 oastatus1;
1180 int ret;
1181
1182 if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
1183 return -EIO;
1184
1185 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
1186
1187 /* XXX: On Haswell we don't have a safe way to clear oastatus1
1188 * bits while the OA unit is enabled (while the tail pointer
1189 * may be updated asynchronously) so we ignore status bits
1190 * that have already been reported to userspace.
1191 */
1192 oastatus1 &= ~stream->perf->gen7_latched_oastatus1;
1193
1194 /* We treat OABUFFER_OVERFLOW as a significant error:
1195 *
1196 * - The status can be interpreted to mean that the buffer is
1197 * currently full (with a higher precedence than OA_TAKEN()
1198 * which will start to report a near-empty buffer after an
1199 * overflow) but it's awkward that we can't clear the status
1200 * on Haswell, so without a reset we won't be able to catch
1201 * the state again.
1202 *
1203 * - Since it also implies the HW has started overwriting old
1204 * reports it may also affect our sanity checks for invalid
1205 * reports when copying to userspace that assume new reports
1206 * are being written to cleared memory.
1207 *
1208 * - In the future we may want to introduce a flight recorder
1209 * mode where the driver will automatically maintain a safe
1210 * guard band between head/tail, avoiding this overflow
1211 * condition, but we avoid the added driver complexity for
1212 * now.
1213 */
1214 if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
1215 ret = append_oa_status(stream, buf, count, offset,
1216 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
1217 if (ret)
1218 return ret;
1219
1220 drm_dbg(&stream->perf->i915->drm,
1221 "OA buffer overflow (exponent = %d): force restart\n",
1222 stream->period_exponent);
1223
1224 stream->perf->ops.oa_disable(stream);
1225 stream->perf->ops.oa_enable(stream);
1226
1227 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
1228 }
1229
1230 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
1231 ret = append_oa_status(stream, buf, count, offset,
1232 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1233 if (ret)
1234 return ret;
1235 stream->perf->gen7_latched_oastatus1 |=
1236 GEN7_OASTATUS1_REPORT_LOST;
1237 }
1238
1239 return gen7_append_oa_reports(stream, buf, count, offset);
1240 }
1241
1242 /**
1243 * i915_oa_wait_unlocked - handles blocking IO until OA data available
1244 * @stream: An i915-perf stream opened for OA metrics
1245 *
1246 * Called when userspace tries to read() from a blocking stream FD opened
1247 * for OA metrics. It waits until the hrtimer callback finds a non-empty
1248 * OA buffer and wakes us.
1249 *
1250 * Note: it's acceptable to have this return with some false positives
1251 * since any subsequent read handling will return -EAGAIN if there isn't
1252 * really data ready for userspace yet.
1253 *
1254 * Returns: zero on success or a negative error code
1255 */
i915_oa_wait_unlocked(struct i915_perf_stream * stream)1256 static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
1257 {
1258 /* We would wait indefinitely if periodic sampling is not enabled */
1259 if (!stream->periodic)
1260 return -EIO;
1261
1262 return wait_event_interruptible(stream->poll_wq,
1263 oa_buffer_check_unlocked(stream));
1264 }
1265
1266 /**
1267 * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
1268 * @stream: An i915-perf stream opened for OA metrics
1269 * @file: An i915 perf stream file
1270 * @wait: poll() state table
1271 *
1272 * For handling userspace polling on an i915 perf stream opened for OA metrics,
1273 * this starts a poll_wait with the wait queue that our hrtimer callback wakes
1274 * when it sees data ready to read in the circular OA buffer.
1275 */
i915_oa_poll_wait(struct i915_perf_stream * stream,struct file * file,poll_table * wait)1276 static void i915_oa_poll_wait(struct i915_perf_stream *stream,
1277 struct file *file,
1278 poll_table *wait)
1279 {
1280 poll_wait(file, &stream->poll_wq, wait);
1281 }
1282
1283 /**
1284 * i915_oa_read - just calls through to &i915_oa_ops->read
1285 * @stream: An i915-perf stream opened for OA metrics
1286 * @buf: destination buffer given by userspace
1287 * @count: the number of bytes userspace wants to read
1288 * @offset: (inout): the current position for writing into @buf
1289 *
1290 * Updates @offset according to the number of bytes successfully copied into
1291 * the userspace buffer.
1292 *
1293 * Returns: zero on success or a negative error code
1294 */
i915_oa_read(struct i915_perf_stream * stream,char __user * buf,size_t count,size_t * offset)1295 static int i915_oa_read(struct i915_perf_stream *stream,
1296 char __user *buf,
1297 size_t count,
1298 size_t *offset)
1299 {
1300 return stream->perf->ops.read(stream, buf, count, offset);
1301 }
1302
oa_pin_context(struct i915_perf_stream * stream)1303 static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)
1304 {
1305 struct i915_gem_engines_iter it;
1306 struct i915_gem_context *ctx = stream->ctx;
1307 struct intel_context *ce;
1308 struct i915_gem_ww_ctx ww;
1309 int err = -ENODEV;
1310
1311 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1312 if (ce->engine != stream->engine) /* first match! */
1313 continue;
1314
1315 err = 0;
1316 break;
1317 }
1318 i915_gem_context_unlock_engines(ctx);
1319
1320 if (err)
1321 return ERR_PTR(err);
1322
1323 i915_gem_ww_ctx_init(&ww, true);
1324 retry:
1325 /*
1326 * As the ID is the gtt offset of the context's vma we
1327 * pin the vma to ensure the ID remains fixed.
1328 */
1329 err = intel_context_pin_ww(ce, &ww);
1330 if (err == -EDEADLK) {
1331 err = i915_gem_ww_ctx_backoff(&ww);
1332 if (!err)
1333 goto retry;
1334 }
1335 i915_gem_ww_ctx_fini(&ww);
1336
1337 if (err)
1338 return ERR_PTR(err);
1339
1340 stream->pinned_ctx = ce;
1341 return stream->pinned_ctx;
1342 }
1343
1344 static int
__store_reg_to_mem(struct i915_request * rq,i915_reg_t reg,u32 ggtt_offset)1345 __store_reg_to_mem(struct i915_request *rq, i915_reg_t reg, u32 ggtt_offset)
1346 {
1347 u32 *cs, cmd;
1348
1349 cmd = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
1350 if (GRAPHICS_VER(rq->i915) >= 8)
1351 cmd++;
1352
1353 cs = intel_ring_begin(rq, 4);
1354 if (IS_ERR(cs))
1355 return PTR_ERR(cs);
1356
1357 *cs++ = cmd;
1358 *cs++ = i915_mmio_reg_offset(reg);
1359 *cs++ = ggtt_offset;
1360 *cs++ = 0;
1361
1362 intel_ring_advance(rq, cs);
1363
1364 return 0;
1365 }
1366
1367 static int
__read_reg(struct intel_context * ce,i915_reg_t reg,u32 ggtt_offset)1368 __read_reg(struct intel_context *ce, i915_reg_t reg, u32 ggtt_offset)
1369 {
1370 struct i915_request *rq;
1371 int err;
1372
1373 rq = i915_request_create(ce);
1374 if (IS_ERR(rq))
1375 return PTR_ERR(rq);
1376
1377 i915_request_get(rq);
1378
1379 err = __store_reg_to_mem(rq, reg, ggtt_offset);
1380
1381 i915_request_add(rq);
1382 if (!err && i915_request_wait(rq, 0, HZ / 2) < 0)
1383 err = -ETIME;
1384
1385 i915_request_put(rq);
1386
1387 return err;
1388 }
1389
1390 static int
gen12_guc_sw_ctx_id(struct intel_context * ce,u32 * ctx_id)1391 gen12_guc_sw_ctx_id(struct intel_context *ce, u32 *ctx_id)
1392 {
1393 struct i915_vma *scratch;
1394 u32 *val;
1395 int err;
1396
1397 scratch = __vm_create_scratch_for_read_pinned(&ce->engine->gt->ggtt->vm, 4);
1398 if (IS_ERR(scratch))
1399 return PTR_ERR(scratch);
1400
1401 err = i915_vma_sync(scratch);
1402 if (err)
1403 goto err_scratch;
1404
1405 err = __read_reg(ce, RING_EXECLIST_STATUS_HI(ce->engine->mmio_base),
1406 i915_ggtt_offset(scratch));
1407 if (err)
1408 goto err_scratch;
1409
1410 val = i915_gem_object_pin_map_unlocked(scratch->obj, I915_MAP_WB);
1411 if (IS_ERR(val)) {
1412 err = PTR_ERR(val);
1413 goto err_scratch;
1414 }
1415
1416 *ctx_id = *val;
1417 i915_gem_object_unpin_map(scratch->obj);
1418
1419 err_scratch:
1420 i915_vma_unpin_and_release(&scratch, 0);
1421 return err;
1422 }
1423
1424 /*
1425 * For execlist mode of submission, pick an unused context id
1426 * 0 - (NUM_CONTEXT_TAG -1) are used by other contexts
1427 * XXX_MAX_CONTEXT_HW_ID is used by idle context
1428 *
1429 * For GuC mode of submission read context id from the upper dword of the
1430 * EXECLIST_STATUS register. Note that we read this value only once and expect
1431 * that the value stays fixed for the entire OA use case. There are cases where
1432 * GuC KMD implementation may deregister a context to reuse it's context id, but
1433 * we prevent that from happening to the OA context by pinning it.
1434 */
gen12_get_render_context_id(struct i915_perf_stream * stream)1435 static int gen12_get_render_context_id(struct i915_perf_stream *stream)
1436 {
1437 u32 ctx_id, mask;
1438 int ret;
1439
1440 if (intel_engine_uses_guc(stream->engine)) {
1441 ret = gen12_guc_sw_ctx_id(stream->pinned_ctx, &ctx_id);
1442 if (ret)
1443 return ret;
1444
1445 mask = ((1U << GEN12_GUC_SW_CTX_ID_WIDTH) - 1) <<
1446 (GEN12_GUC_SW_CTX_ID_SHIFT - 32);
1447 } else if (GRAPHICS_VER_FULL(stream->engine->i915) >= IP_VER(12, 50)) {
1448 ctx_id = (XEHP_MAX_CONTEXT_HW_ID - 1) <<
1449 (XEHP_SW_CTX_ID_SHIFT - 32);
1450
1451 mask = ((1U << XEHP_SW_CTX_ID_WIDTH) - 1) <<
1452 (XEHP_SW_CTX_ID_SHIFT - 32);
1453 } else {
1454 ctx_id = (GEN12_MAX_CONTEXT_HW_ID - 1) <<
1455 (GEN11_SW_CTX_ID_SHIFT - 32);
1456
1457 mask = ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) <<
1458 (GEN11_SW_CTX_ID_SHIFT - 32);
1459 }
1460 stream->specific_ctx_id = ctx_id & mask;
1461 stream->specific_ctx_id_mask = mask;
1462
1463 return 0;
1464 }
1465
oa_find_reg_in_lri(u32 * state,u32 reg,u32 * offset,u32 end)1466 static bool oa_find_reg_in_lri(u32 *state, u32 reg, u32 *offset, u32 end)
1467 {
1468 u32 idx = *offset;
1469 u32 len = min(MI_LRI_LEN(state[idx]) + idx, end);
1470 bool found = false;
1471
1472 idx++;
1473 for (; idx < len; idx += 2) {
1474 if (state[idx] == reg) {
1475 found = true;
1476 break;
1477 }
1478 }
1479
1480 *offset = idx;
1481 return found;
1482 }
1483
oa_context_image_offset(struct intel_context * ce,u32 reg)1484 static u32 oa_context_image_offset(struct intel_context *ce, u32 reg)
1485 {
1486 u32 offset, len = (ce->engine->context_size - PAGE_SIZE) / 4;
1487 u32 *state = ce->lrc_reg_state;
1488
1489 if (drm_WARN_ON(&ce->engine->i915->drm, !state))
1490 return U32_MAX;
1491
1492 for (offset = 0; offset < len; ) {
1493 if (IS_MI_LRI_CMD(state[offset])) {
1494 /*
1495 * We expect reg-value pairs in MI_LRI command, so
1496 * MI_LRI_LEN() should be even, if not, issue a warning.
1497 */
1498 drm_WARN_ON(&ce->engine->i915->drm,
1499 MI_LRI_LEN(state[offset]) & 0x1);
1500
1501 if (oa_find_reg_in_lri(state, reg, &offset, len))
1502 break;
1503 } else {
1504 offset++;
1505 }
1506 }
1507
1508 return offset < len ? offset : U32_MAX;
1509 }
1510
set_oa_ctx_ctrl_offset(struct intel_context * ce)1511 static int set_oa_ctx_ctrl_offset(struct intel_context *ce)
1512 {
1513 i915_reg_t reg = GEN12_OACTXCONTROL(ce->engine->mmio_base);
1514 struct i915_perf *perf = &ce->engine->i915->perf;
1515 u32 offset = perf->ctx_oactxctrl_offset;
1516
1517 /* Do this only once. Failure is stored as offset of U32_MAX */
1518 if (offset)
1519 goto exit;
1520
1521 offset = oa_context_image_offset(ce, i915_mmio_reg_offset(reg));
1522 perf->ctx_oactxctrl_offset = offset;
1523
1524 drm_dbg(&ce->engine->i915->drm,
1525 "%s oa ctx control at 0x%08x dword offset\n",
1526 ce->engine->name, offset);
1527
1528 exit:
1529 return offset && offset != U32_MAX ? 0 : -ENODEV;
1530 }
1531
engine_supports_mi_query(struct intel_engine_cs * engine)1532 static bool engine_supports_mi_query(struct intel_engine_cs *engine)
1533 {
1534 return engine->class == RENDER_CLASS;
1535 }
1536
1537 /**
1538 * oa_get_render_ctx_id - determine and hold ctx hw id
1539 * @stream: An i915-perf stream opened for OA metrics
1540 *
1541 * Determine the render context hw id, and ensure it remains fixed for the
1542 * lifetime of the stream. This ensures that we don't have to worry about
1543 * updating the context ID in OACONTROL on the fly.
1544 *
1545 * Returns: zero on success or a negative error code
1546 */
oa_get_render_ctx_id(struct i915_perf_stream * stream)1547 static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
1548 {
1549 struct intel_context *ce;
1550 int ret = 0;
1551
1552 ce = oa_pin_context(stream);
1553 if (IS_ERR(ce))
1554 return PTR_ERR(ce);
1555
1556 if (engine_supports_mi_query(stream->engine) &&
1557 HAS_LOGICAL_RING_CONTEXTS(stream->perf->i915)) {
1558 /*
1559 * We are enabling perf query here. If we don't find the context
1560 * offset here, just return an error.
1561 */
1562 ret = set_oa_ctx_ctrl_offset(ce);
1563 if (ret) {
1564 intel_context_unpin(ce);
1565 drm_err(&stream->perf->i915->drm,
1566 "Enabling perf query failed for %s\n",
1567 stream->engine->name);
1568 return ret;
1569 }
1570 }
1571
1572 switch (GRAPHICS_VER(ce->engine->i915)) {
1573 case 7: {
1574 /*
1575 * On Haswell we don't do any post processing of the reports
1576 * and don't need to use the mask.
1577 */
1578 stream->specific_ctx_id = i915_ggtt_offset(ce->state);
1579 stream->specific_ctx_id_mask = 0;
1580 break;
1581 }
1582
1583 case 8:
1584 case 9:
1585 if (intel_engine_uses_guc(ce->engine)) {
1586 /*
1587 * When using GuC, the context descriptor we write in
1588 * i915 is read by GuC and rewritten before it's
1589 * actually written into the hardware. The LRCA is
1590 * what is put into the context id field of the
1591 * context descriptor by GuC. Because it's aligned to
1592 * a page, the lower 12bits are always at 0 and
1593 * dropped by GuC. They won't be part of the context
1594 * ID in the OA reports, so squash those lower bits.
1595 */
1596 stream->specific_ctx_id = ce->lrc.lrca >> 12;
1597
1598 /*
1599 * GuC uses the top bit to signal proxy submission, so
1600 * ignore that bit.
1601 */
1602 stream->specific_ctx_id_mask =
1603 (1U << (GEN8_CTX_ID_WIDTH - 1)) - 1;
1604 } else {
1605 stream->specific_ctx_id_mask =
1606 (1U << GEN8_CTX_ID_WIDTH) - 1;
1607 stream->specific_ctx_id = stream->specific_ctx_id_mask;
1608 }
1609 break;
1610
1611 case 11:
1612 case 12:
1613 ret = gen12_get_render_context_id(stream);
1614 break;
1615
1616 default:
1617 MISSING_CASE(GRAPHICS_VER(ce->engine->i915));
1618 }
1619
1620 ce->tag = stream->specific_ctx_id;
1621
1622 drm_dbg(&stream->perf->i915->drm,
1623 "filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
1624 stream->specific_ctx_id,
1625 stream->specific_ctx_id_mask);
1626
1627 return ret;
1628 }
1629
1630 /**
1631 * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
1632 * @stream: An i915-perf stream opened for OA metrics
1633 *
1634 * In case anything needed doing to ensure the context HW ID would remain valid
1635 * for the lifetime of the stream, then that can be undone here.
1636 */
oa_put_render_ctx_id(struct i915_perf_stream * stream)1637 static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
1638 {
1639 struct intel_context *ce;
1640
1641 ce = fetch_and_zero(&stream->pinned_ctx);
1642 if (ce) {
1643 ce->tag = 0; /* recomputed on next submission after parking */
1644 intel_context_unpin(ce);
1645 }
1646
1647 stream->specific_ctx_id = INVALID_CTX_ID;
1648 stream->specific_ctx_id_mask = 0;
1649 }
1650
1651 static void
free_oa_buffer(struct i915_perf_stream * stream)1652 free_oa_buffer(struct i915_perf_stream *stream)
1653 {
1654 i915_vma_unpin_and_release(&stream->oa_buffer.vma,
1655 I915_VMA_RELEASE_MAP);
1656
1657 stream->oa_buffer.vaddr = NULL;
1658 }
1659
1660 static void
free_oa_configs(struct i915_perf_stream * stream)1661 free_oa_configs(struct i915_perf_stream *stream)
1662 {
1663 struct i915_oa_config_bo *oa_bo, *tmp;
1664
1665 i915_oa_config_put(stream->oa_config);
1666 llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node)
1667 free_oa_config_bo(oa_bo);
1668 }
1669
1670 static void
free_noa_wait(struct i915_perf_stream * stream)1671 free_noa_wait(struct i915_perf_stream *stream)
1672 {
1673 i915_vma_unpin_and_release(&stream->noa_wait, 0);
1674 }
1675
engine_supports_oa(const struct intel_engine_cs * engine)1676 static bool engine_supports_oa(const struct intel_engine_cs *engine)
1677 {
1678 return engine->oa_group;
1679 }
1680
engine_supports_oa_format(struct intel_engine_cs * engine,int type)1681 static bool engine_supports_oa_format(struct intel_engine_cs *engine, int type)
1682 {
1683 return engine->oa_group && engine->oa_group->type == type;
1684 }
1685
i915_oa_stream_destroy(struct i915_perf_stream * stream)1686 static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
1687 {
1688 struct i915_perf *perf = stream->perf;
1689 struct intel_gt *gt = stream->engine->gt;
1690 struct i915_perf_group *g = stream->engine->oa_group;
1691
1692 if (WARN_ON(stream != g->exclusive_stream))
1693 return;
1694
1695 /*
1696 * Unset exclusive_stream first, it will be checked while disabling
1697 * the metric set on gen8+.
1698 *
1699 * See i915_oa_init_reg_state() and lrc_configure_all_contexts()
1700 */
1701 WRITE_ONCE(g->exclusive_stream, NULL);
1702 perf->ops.disable_metric_set(stream);
1703
1704 free_oa_buffer(stream);
1705
1706 /*
1707 * Wa_16011777198:dg2: Unset the override of GUCRC mode to enable rc6.
1708 */
1709 if (stream->override_gucrc)
1710 drm_WARN_ON(>->i915->drm,
1711 intel_guc_slpc_unset_gucrc_mode(>->uc.guc.slpc));
1712
1713 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
1714 intel_engine_pm_put(stream->engine);
1715
1716 if (stream->ctx)
1717 oa_put_render_ctx_id(stream);
1718
1719 free_oa_configs(stream);
1720 free_noa_wait(stream);
1721
1722 if (perf->spurious_report_rs.missed) {
1723 drm_notice(>->i915->drm,
1724 "%d spurious OA report notices suppressed due to ratelimiting\n",
1725 perf->spurious_report_rs.missed);
1726 }
1727 }
1728
gen7_init_oa_buffer(struct i915_perf_stream * stream)1729 static void gen7_init_oa_buffer(struct i915_perf_stream *stream)
1730 {
1731 struct intel_uncore *uncore = stream->uncore;
1732 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
1733 unsigned long flags;
1734
1735 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1736
1737 /* Pre-DevBDW: OABUFFER must be set with counters off,
1738 * before OASTATUS1, but after OASTATUS2
1739 */
1740 intel_uncore_write(uncore, GEN7_OASTATUS2, /* head */
1741 gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT);
1742 stream->oa_buffer.head = gtt_offset;
1743
1744 intel_uncore_write(uncore, GEN7_OABUFFER, gtt_offset);
1745
1746 intel_uncore_write(uncore, GEN7_OASTATUS1, /* tail */
1747 gtt_offset | OABUFFER_SIZE_16M);
1748
1749 /* Mark that we need updated tail pointers to read from... */
1750 stream->oa_buffer.tail = gtt_offset;
1751
1752 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1753
1754 /* On Haswell we have to track which OASTATUS1 flags we've
1755 * already seen since they can't be cleared while periodic
1756 * sampling is enabled.
1757 */
1758 stream->perf->gen7_latched_oastatus1 = 0;
1759
1760 /* NB: although the OA buffer will initially be allocated
1761 * zeroed via shmfs (and so this memset is redundant when
1762 * first allocating), we may re-init the OA buffer, either
1763 * when re-enabling a stream or in error/reset paths.
1764 *
1765 * The reason we clear the buffer for each re-init is for the
1766 * sanity check in gen7_append_oa_reports() that looks at the
1767 * report-id field to make sure it's non-zero which relies on
1768 * the assumption that new reports are being written to zeroed
1769 * memory...
1770 */
1771 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1772 }
1773
gen8_init_oa_buffer(struct i915_perf_stream * stream)1774 static void gen8_init_oa_buffer(struct i915_perf_stream *stream)
1775 {
1776 struct intel_uncore *uncore = stream->uncore;
1777 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
1778 unsigned long flags;
1779
1780 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1781
1782 intel_uncore_write(uncore, GEN8_OASTATUS, 0);
1783 intel_uncore_write(uncore, GEN8_OAHEADPTR, gtt_offset);
1784 stream->oa_buffer.head = gtt_offset;
1785
1786 intel_uncore_write(uncore, GEN8_OABUFFER_UDW, 0);
1787
1788 /*
1789 * PRM says:
1790 *
1791 * "This MMIO must be set before the OATAILPTR
1792 * register and after the OAHEADPTR register. This is
1793 * to enable proper functionality of the overflow
1794 * bit."
1795 */
1796 intel_uncore_write(uncore, GEN8_OABUFFER, gtt_offset |
1797 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
1798 intel_uncore_write(uncore, GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
1799
1800 /* Mark that we need updated tail pointers to read from... */
1801 stream->oa_buffer.tail = gtt_offset;
1802
1803 /*
1804 * Reset state used to recognise context switches, affecting which
1805 * reports we will forward to userspace while filtering for a single
1806 * context.
1807 */
1808 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
1809
1810 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1811
1812 /*
1813 * NB: although the OA buffer will initially be allocated
1814 * zeroed via shmfs (and so this memset is redundant when
1815 * first allocating), we may re-init the OA buffer, either
1816 * when re-enabling a stream or in error/reset paths.
1817 *
1818 * The reason we clear the buffer for each re-init is for the
1819 * sanity check in gen8_append_oa_reports() that looks at the
1820 * reason field to make sure it's non-zero which relies on
1821 * the assumption that new reports are being written to zeroed
1822 * memory...
1823 */
1824 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1825 }
1826
gen12_init_oa_buffer(struct i915_perf_stream * stream)1827 static void gen12_init_oa_buffer(struct i915_perf_stream *stream)
1828 {
1829 struct intel_uncore *uncore = stream->uncore;
1830 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
1831 unsigned long flags;
1832
1833 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1834
1835 intel_uncore_write(uncore, __oa_regs(stream)->oa_status, 0);
1836 intel_uncore_write(uncore, __oa_regs(stream)->oa_head_ptr,
1837 gtt_offset & GEN12_OAG_OAHEADPTR_MASK);
1838 stream->oa_buffer.head = gtt_offset;
1839
1840 /*
1841 * PRM says:
1842 *
1843 * "This MMIO must be set before the OATAILPTR
1844 * register and after the OAHEADPTR register. This is
1845 * to enable proper functionality of the overflow
1846 * bit."
1847 */
1848 intel_uncore_write(uncore, __oa_regs(stream)->oa_buffer, gtt_offset |
1849 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
1850 intel_uncore_write(uncore, __oa_regs(stream)->oa_tail_ptr,
1851 gtt_offset & GEN12_OAG_OATAILPTR_MASK);
1852
1853 /* Mark that we need updated tail pointers to read from... */
1854 stream->oa_buffer.tail = gtt_offset;
1855
1856 /*
1857 * Reset state used to recognise context switches, affecting which
1858 * reports we will forward to userspace while filtering for a single
1859 * context.
1860 */
1861 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
1862
1863 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1864
1865 /*
1866 * NB: although the OA buffer will initially be allocated
1867 * zeroed via shmfs (and so this memset is redundant when
1868 * first allocating), we may re-init the OA buffer, either
1869 * when re-enabling a stream or in error/reset paths.
1870 *
1871 * The reason we clear the buffer for each re-init is for the
1872 * sanity check in gen8_append_oa_reports() that looks at the
1873 * reason field to make sure it's non-zero which relies on
1874 * the assumption that new reports are being written to zeroed
1875 * memory...
1876 */
1877 memset(stream->oa_buffer.vaddr, 0,
1878 stream->oa_buffer.vma->size);
1879 }
1880
alloc_oa_buffer(struct i915_perf_stream * stream)1881 static int alloc_oa_buffer(struct i915_perf_stream *stream)
1882 {
1883 struct drm_i915_private *i915 = stream->perf->i915;
1884 struct intel_gt *gt = stream->engine->gt;
1885 struct drm_i915_gem_object *bo;
1886 struct i915_vma *vma;
1887 int ret;
1888
1889 if (drm_WARN_ON(&i915->drm, stream->oa_buffer.vma))
1890 return -ENODEV;
1891
1892 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
1893 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
1894
1895 bo = i915_gem_object_create_shmem(stream->perf->i915, OA_BUFFER_SIZE);
1896 if (IS_ERR(bo)) {
1897 drm_err(&i915->drm, "Failed to allocate OA buffer\n");
1898 return PTR_ERR(bo);
1899 }
1900
1901 i915_gem_object_set_cache_coherency(bo, I915_CACHE_LLC);
1902
1903 /* PreHSW required 512K alignment, HSW requires 16M */
1904 vma = i915_vma_instance(bo, >->ggtt->vm, NULL);
1905 if (IS_ERR(vma)) {
1906 ret = PTR_ERR(vma);
1907 goto err_unref;
1908 }
1909
1910 /*
1911 * PreHSW required 512K alignment.
1912 * HSW and onwards, align to requested size of OA buffer.
1913 */
1914 ret = i915_vma_pin(vma, 0, SZ_16M, PIN_GLOBAL | PIN_HIGH);
1915 if (ret) {
1916 drm_err(>->i915->drm, "Failed to pin OA buffer %d\n", ret);
1917 goto err_unref;
1918 }
1919
1920 stream->oa_buffer.vma = vma;
1921
1922 stream->oa_buffer.vaddr =
1923 i915_gem_object_pin_map_unlocked(bo, I915_MAP_WB);
1924 if (IS_ERR(stream->oa_buffer.vaddr)) {
1925 ret = PTR_ERR(stream->oa_buffer.vaddr);
1926 goto err_unpin;
1927 }
1928
1929 return 0;
1930
1931 err_unpin:
1932 __i915_vma_unpin(vma);
1933
1934 err_unref:
1935 i915_gem_object_put(bo);
1936
1937 stream->oa_buffer.vaddr = NULL;
1938 stream->oa_buffer.vma = NULL;
1939
1940 return ret;
1941 }
1942
save_restore_register(struct i915_perf_stream * stream,u32 * cs,bool save,i915_reg_t reg,u32 offset,u32 dword_count)1943 static u32 *save_restore_register(struct i915_perf_stream *stream, u32 *cs,
1944 bool save, i915_reg_t reg, u32 offset,
1945 u32 dword_count)
1946 {
1947 u32 cmd;
1948 u32 d;
1949
1950 cmd = save ? MI_STORE_REGISTER_MEM : MI_LOAD_REGISTER_MEM;
1951 cmd |= MI_SRM_LRM_GLOBAL_GTT;
1952 if (GRAPHICS_VER(stream->perf->i915) >= 8)
1953 cmd++;
1954
1955 for (d = 0; d < dword_count; d++) {
1956 *cs++ = cmd;
1957 *cs++ = i915_mmio_reg_offset(reg) + 4 * d;
1958 *cs++ = i915_ggtt_offset(stream->noa_wait) + offset + 4 * d;
1959 *cs++ = 0;
1960 }
1961
1962 return cs;
1963 }
1964
alloc_noa_wait(struct i915_perf_stream * stream)1965 static int alloc_noa_wait(struct i915_perf_stream *stream)
1966 {
1967 struct drm_i915_private *i915 = stream->perf->i915;
1968 struct intel_gt *gt = stream->engine->gt;
1969 struct drm_i915_gem_object *bo;
1970 struct i915_vma *vma;
1971 const u64 delay_ticks = 0xffffffffffffffff -
1972 intel_gt_ns_to_clock_interval(to_gt(stream->perf->i915),
1973 atomic64_read(&stream->perf->noa_programming_delay));
1974 const u32 base = stream->engine->mmio_base;
1975 #define CS_GPR(x) GEN8_RING_CS_GPR(base, x)
1976 u32 *batch, *ts0, *cs, *jump;
1977 struct i915_gem_ww_ctx ww;
1978 int ret, i;
1979 enum {
1980 START_TS,
1981 NOW_TS,
1982 DELTA_TS,
1983 JUMP_PREDICATE,
1984 DELTA_TARGET,
1985 N_CS_GPR
1986 };
1987 i915_reg_t mi_predicate_result = HAS_MI_SET_PREDICATE(i915) ?
1988 MI_PREDICATE_RESULT_2_ENGINE(base) :
1989 MI_PREDICATE_RESULT_1(RENDER_RING_BASE);
1990
1991 /*
1992 * gt->scratch was being used to save/restore the GPR registers, but on
1993 * MTL the scratch uses stolen lmem. An MI_SRM to this memory region
1994 * causes an engine hang. Instead allocate an additional page here to
1995 * save/restore GPR registers
1996 */
1997 bo = i915_gem_object_create_internal(i915, 8192);
1998 if (IS_ERR(bo)) {
1999 drm_err(&i915->drm,
2000 "Failed to allocate NOA wait batchbuffer\n");
2001 return PTR_ERR(bo);
2002 }
2003
2004 i915_gem_ww_ctx_init(&ww, true);
2005 retry:
2006 ret = i915_gem_object_lock(bo, &ww);
2007 if (ret)
2008 goto out_ww;
2009
2010 /*
2011 * We pin in GGTT because we jump into this buffer now because
2012 * multiple OA config BOs will have a jump to this address and it
2013 * needs to be fixed during the lifetime of the i915/perf stream.
2014 */
2015 vma = i915_vma_instance(bo, >->ggtt->vm, NULL);
2016 if (IS_ERR(vma)) {
2017 ret = PTR_ERR(vma);
2018 goto out_ww;
2019 }
2020
2021 ret = i915_vma_pin_ww(vma, &ww, 0, 0, PIN_GLOBAL | PIN_HIGH);
2022 if (ret)
2023 goto out_ww;
2024
2025 batch = cs = i915_gem_object_pin_map(bo, I915_MAP_WB);
2026 if (IS_ERR(batch)) {
2027 ret = PTR_ERR(batch);
2028 goto err_unpin;
2029 }
2030
2031 stream->noa_wait = vma;
2032
2033 #define GPR_SAVE_OFFSET 4096
2034 #define PREDICATE_SAVE_OFFSET 4160
2035
2036 /* Save registers. */
2037 for (i = 0; i < N_CS_GPR; i++)
2038 cs = save_restore_register(
2039 stream, cs, true /* save */, CS_GPR(i),
2040 GPR_SAVE_OFFSET + 8 * i, 2);
2041 cs = save_restore_register(
2042 stream, cs, true /* save */, mi_predicate_result,
2043 PREDICATE_SAVE_OFFSET, 1);
2044
2045 /* First timestamp snapshot location. */
2046 ts0 = cs;
2047
2048 /*
2049 * Initial snapshot of the timestamp register to implement the wait.
2050 * We work with 32b values, so clear out the top 32b bits of the
2051 * register because the ALU works 64bits.
2052 */
2053 *cs++ = MI_LOAD_REGISTER_IMM(1);
2054 *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS)) + 4;
2055 *cs++ = 0;
2056 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
2057 *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
2058 *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS));
2059
2060 /*
2061 * This is the location we're going to jump back into until the
2062 * required amount of time has passed.
2063 */
2064 jump = cs;
2065
2066 /*
2067 * Take another snapshot of the timestamp register. Take care to clear
2068 * up the top 32bits of CS_GPR(1) as we're using it for other
2069 * operations below.
2070 */
2071 *cs++ = MI_LOAD_REGISTER_IMM(1);
2072 *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS)) + 4;
2073 *cs++ = 0;
2074 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
2075 *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
2076 *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS));
2077
2078 /*
2079 * Do a diff between the 2 timestamps and store the result back into
2080 * CS_GPR(1).
2081 */
2082 *cs++ = MI_MATH(5);
2083 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(NOW_TS));
2084 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(START_TS));
2085 *cs++ = MI_MATH_SUB;
2086 *cs++ = MI_MATH_STORE(MI_MATH_REG(DELTA_TS), MI_MATH_REG_ACCU);
2087 *cs++ = MI_MATH_STORE(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
2088
2089 /*
2090 * Transfer the carry flag (set to 1 if ts1 < ts0, meaning the
2091 * timestamp have rolled over the 32bits) into the predicate register
2092 * to be used for the predicated jump.
2093 */
2094 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
2095 *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
2096 *cs++ = i915_mmio_reg_offset(mi_predicate_result);
2097
2098 if (HAS_MI_SET_PREDICATE(i915))
2099 *cs++ = MI_SET_PREDICATE | 1;
2100
2101 /* Restart from the beginning if we had timestamps roll over. */
2102 *cs++ = (GRAPHICS_VER(i915) < 8 ?
2103 MI_BATCH_BUFFER_START :
2104 MI_BATCH_BUFFER_START_GEN8) |
2105 MI_BATCH_PREDICATE;
2106 *cs++ = i915_ggtt_offset(vma) + (ts0 - batch) * 4;
2107 *cs++ = 0;
2108
2109 if (HAS_MI_SET_PREDICATE(i915))
2110 *cs++ = MI_SET_PREDICATE;
2111
2112 /*
2113 * Now add the diff between to previous timestamps and add it to :
2114 * (((1 * << 64) - 1) - delay_ns)
2115 *
2116 * When the Carry Flag contains 1 this means the elapsed time is
2117 * longer than the expected delay, and we can exit the wait loop.
2118 */
2119 *cs++ = MI_LOAD_REGISTER_IMM(2);
2120 *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET));
2121 *cs++ = lower_32_bits(delay_ticks);
2122 *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET)) + 4;
2123 *cs++ = upper_32_bits(delay_ticks);
2124
2125 *cs++ = MI_MATH(4);
2126 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(DELTA_TS));
2127 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(DELTA_TARGET));
2128 *cs++ = MI_MATH_ADD;
2129 *cs++ = MI_MATH_STOREINV(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
2130
2131 *cs++ = MI_ARB_CHECK;
2132
2133 /*
2134 * Transfer the result into the predicate register to be used for the
2135 * predicated jump.
2136 */
2137 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
2138 *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
2139 *cs++ = i915_mmio_reg_offset(mi_predicate_result);
2140
2141 if (HAS_MI_SET_PREDICATE(i915))
2142 *cs++ = MI_SET_PREDICATE | 1;
2143
2144 /* Predicate the jump. */
2145 *cs++ = (GRAPHICS_VER(i915) < 8 ?
2146 MI_BATCH_BUFFER_START :
2147 MI_BATCH_BUFFER_START_GEN8) |
2148 MI_BATCH_PREDICATE;
2149 *cs++ = i915_ggtt_offset(vma) + (jump - batch) * 4;
2150 *cs++ = 0;
2151
2152 if (HAS_MI_SET_PREDICATE(i915))
2153 *cs++ = MI_SET_PREDICATE;
2154
2155 /* Restore registers. */
2156 for (i = 0; i < N_CS_GPR; i++)
2157 cs = save_restore_register(
2158 stream, cs, false /* restore */, CS_GPR(i),
2159 GPR_SAVE_OFFSET + 8 * i, 2);
2160 cs = save_restore_register(
2161 stream, cs, false /* restore */, mi_predicate_result,
2162 PREDICATE_SAVE_OFFSET, 1);
2163
2164 /* And return to the ring. */
2165 *cs++ = MI_BATCH_BUFFER_END;
2166
2167 GEM_BUG_ON(cs - batch > PAGE_SIZE / sizeof(*batch));
2168
2169 i915_gem_object_flush_map(bo);
2170 __i915_gem_object_release_map(bo);
2171
2172 goto out_ww;
2173
2174 err_unpin:
2175 i915_vma_unpin_and_release(&vma, 0);
2176 out_ww:
2177 if (ret == -EDEADLK) {
2178 ret = i915_gem_ww_ctx_backoff(&ww);
2179 if (!ret)
2180 goto retry;
2181 }
2182 i915_gem_ww_ctx_fini(&ww);
2183 if (ret)
2184 i915_gem_object_put(bo);
2185 return ret;
2186 }
2187
write_cs_mi_lri(u32 * cs,const struct i915_oa_reg * reg_data,u32 n_regs)2188 static u32 *write_cs_mi_lri(u32 *cs,
2189 const struct i915_oa_reg *reg_data,
2190 u32 n_regs)
2191 {
2192 u32 i;
2193
2194 for (i = 0; i < n_regs; i++) {
2195 if ((i % MI_LOAD_REGISTER_IMM_MAX_REGS) == 0) {
2196 u32 n_lri = min_t(u32,
2197 n_regs - i,
2198 MI_LOAD_REGISTER_IMM_MAX_REGS);
2199
2200 *cs++ = MI_LOAD_REGISTER_IMM(n_lri);
2201 }
2202 *cs++ = i915_mmio_reg_offset(reg_data[i].addr);
2203 *cs++ = reg_data[i].value;
2204 }
2205
2206 return cs;
2207 }
2208
num_lri_dwords(int num_regs)2209 static int num_lri_dwords(int num_regs)
2210 {
2211 int count = 0;
2212
2213 if (num_regs > 0) {
2214 count += DIV_ROUND_UP(num_regs, MI_LOAD_REGISTER_IMM_MAX_REGS);
2215 count += num_regs * 2;
2216 }
2217
2218 return count;
2219 }
2220
2221 static struct i915_oa_config_bo *
alloc_oa_config_buffer(struct i915_perf_stream * stream,struct i915_oa_config * oa_config)2222 alloc_oa_config_buffer(struct i915_perf_stream *stream,
2223 struct i915_oa_config *oa_config)
2224 {
2225 struct drm_i915_gem_object *obj;
2226 struct i915_oa_config_bo *oa_bo;
2227 struct i915_gem_ww_ctx ww;
2228 size_t config_length = 0;
2229 u32 *cs;
2230 int err;
2231
2232 oa_bo = kzalloc(sizeof(*oa_bo), GFP_KERNEL);
2233 if (!oa_bo)
2234 return ERR_PTR(-ENOMEM);
2235
2236 config_length += num_lri_dwords(oa_config->mux_regs_len);
2237 config_length += num_lri_dwords(oa_config->b_counter_regs_len);
2238 config_length += num_lri_dwords(oa_config->flex_regs_len);
2239 config_length += 3; /* MI_BATCH_BUFFER_START */
2240 config_length = ALIGN(sizeof(u32) * config_length, I915_GTT_PAGE_SIZE);
2241
2242 obj = i915_gem_object_create_shmem(stream->perf->i915, config_length);
2243 if (IS_ERR(obj)) {
2244 err = PTR_ERR(obj);
2245 goto err_free;
2246 }
2247
2248 i915_gem_ww_ctx_init(&ww, true);
2249 retry:
2250 err = i915_gem_object_lock(obj, &ww);
2251 if (err)
2252 goto out_ww;
2253
2254 cs = i915_gem_object_pin_map(obj, I915_MAP_WB);
2255 if (IS_ERR(cs)) {
2256 err = PTR_ERR(cs);
2257 goto out_ww;
2258 }
2259
2260 cs = write_cs_mi_lri(cs,
2261 oa_config->mux_regs,
2262 oa_config->mux_regs_len);
2263 cs = write_cs_mi_lri(cs,
2264 oa_config->b_counter_regs,
2265 oa_config->b_counter_regs_len);
2266 cs = write_cs_mi_lri(cs,
2267 oa_config->flex_regs,
2268 oa_config->flex_regs_len);
2269
2270 /* Jump into the active wait. */
2271 *cs++ = (GRAPHICS_VER(stream->perf->i915) < 8 ?
2272 MI_BATCH_BUFFER_START :
2273 MI_BATCH_BUFFER_START_GEN8);
2274 *cs++ = i915_ggtt_offset(stream->noa_wait);
2275 *cs++ = 0;
2276
2277 i915_gem_object_flush_map(obj);
2278 __i915_gem_object_release_map(obj);
2279
2280 oa_bo->vma = i915_vma_instance(obj,
2281 &stream->engine->gt->ggtt->vm,
2282 NULL);
2283 if (IS_ERR(oa_bo->vma)) {
2284 err = PTR_ERR(oa_bo->vma);
2285 goto out_ww;
2286 }
2287
2288 oa_bo->oa_config = i915_oa_config_get(oa_config);
2289 llist_add(&oa_bo->node, &stream->oa_config_bos);
2290
2291 out_ww:
2292 if (err == -EDEADLK) {
2293 err = i915_gem_ww_ctx_backoff(&ww);
2294 if (!err)
2295 goto retry;
2296 }
2297 i915_gem_ww_ctx_fini(&ww);
2298
2299 if (err)
2300 i915_gem_object_put(obj);
2301 err_free:
2302 if (err) {
2303 kfree(oa_bo);
2304 return ERR_PTR(err);
2305 }
2306 return oa_bo;
2307 }
2308
2309 static struct i915_vma *
get_oa_vma(struct i915_perf_stream * stream,struct i915_oa_config * oa_config)2310 get_oa_vma(struct i915_perf_stream *stream, struct i915_oa_config *oa_config)
2311 {
2312 struct i915_oa_config_bo *oa_bo;
2313
2314 /*
2315 * Look for the buffer in the already allocated BOs attached
2316 * to the stream.
2317 */
2318 llist_for_each_entry(oa_bo, stream->oa_config_bos.first, node) {
2319 if (oa_bo->oa_config == oa_config &&
2320 memcmp(oa_bo->oa_config->uuid,
2321 oa_config->uuid,
2322 sizeof(oa_config->uuid)) == 0)
2323 goto out;
2324 }
2325
2326 oa_bo = alloc_oa_config_buffer(stream, oa_config);
2327 if (IS_ERR(oa_bo))
2328 return ERR_CAST(oa_bo);
2329
2330 out:
2331 return i915_vma_get(oa_bo->vma);
2332 }
2333
2334 static int
emit_oa_config(struct i915_perf_stream * stream,struct i915_oa_config * oa_config,struct intel_context * ce,struct i915_active * active)2335 emit_oa_config(struct i915_perf_stream *stream,
2336 struct i915_oa_config *oa_config,
2337 struct intel_context *ce,
2338 struct i915_active *active)
2339 {
2340 struct i915_request *rq;
2341 struct i915_vma *vma;
2342 struct i915_gem_ww_ctx ww;
2343 int err;
2344
2345 vma = get_oa_vma(stream, oa_config);
2346 if (IS_ERR(vma))
2347 return PTR_ERR(vma);
2348
2349 i915_gem_ww_ctx_init(&ww, true);
2350 retry:
2351 err = i915_gem_object_lock(vma->obj, &ww);
2352 if (err)
2353 goto err;
2354
2355 err = i915_vma_pin_ww(vma, &ww, 0, 0, PIN_GLOBAL | PIN_HIGH);
2356 if (err)
2357 goto err;
2358
2359 intel_engine_pm_get(ce->engine);
2360 rq = i915_request_create(ce);
2361 intel_engine_pm_put(ce->engine);
2362 if (IS_ERR(rq)) {
2363 err = PTR_ERR(rq);
2364 goto err_vma_unpin;
2365 }
2366
2367 if (!IS_ERR_OR_NULL(active)) {
2368 /* After all individual context modifications */
2369 err = i915_request_await_active(rq, active,
2370 I915_ACTIVE_AWAIT_ACTIVE);
2371 if (err)
2372 goto err_add_request;
2373
2374 err = i915_active_add_request(active, rq);
2375 if (err)
2376 goto err_add_request;
2377 }
2378
2379 err = i915_vma_move_to_active(vma, rq, 0);
2380 if (err)
2381 goto err_add_request;
2382
2383 err = rq->engine->emit_bb_start(rq,
2384 i915_vma_offset(vma), 0,
2385 I915_DISPATCH_SECURE);
2386 if (err)
2387 goto err_add_request;
2388
2389 err_add_request:
2390 i915_request_add(rq);
2391 err_vma_unpin:
2392 i915_vma_unpin(vma);
2393 err:
2394 if (err == -EDEADLK) {
2395 err = i915_gem_ww_ctx_backoff(&ww);
2396 if (!err)
2397 goto retry;
2398 }
2399
2400 i915_gem_ww_ctx_fini(&ww);
2401 i915_vma_put(vma);
2402 return err;
2403 }
2404
oa_context(struct i915_perf_stream * stream)2405 static struct intel_context *oa_context(struct i915_perf_stream *stream)
2406 {
2407 return stream->pinned_ctx ?: stream->engine->kernel_context;
2408 }
2409
2410 static int
hsw_enable_metric_set(struct i915_perf_stream * stream,struct i915_active * active)2411 hsw_enable_metric_set(struct i915_perf_stream *stream,
2412 struct i915_active *active)
2413 {
2414 struct intel_uncore *uncore = stream->uncore;
2415
2416 /*
2417 * PRM:
2418 *
2419 * OA unit is using “crclk” for its functionality. When trunk
2420 * level clock gating takes place, OA clock would be gated,
2421 * unable to count the events from non-render clock domain.
2422 * Render clock gating must be disabled when OA is enabled to
2423 * count the events from non-render domain. Unit level clock
2424 * gating for RCS should also be disabled.
2425 */
2426 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2427 GEN7_DOP_CLOCK_GATE_ENABLE, 0);
2428 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2429 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
2430
2431 return emit_oa_config(stream,
2432 stream->oa_config, oa_context(stream),
2433 active);
2434 }
2435
hsw_disable_metric_set(struct i915_perf_stream * stream)2436 static void hsw_disable_metric_set(struct i915_perf_stream *stream)
2437 {
2438 struct intel_uncore *uncore = stream->uncore;
2439
2440 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2441 GEN6_CSUNIT_CLOCK_GATE_DISABLE, 0);
2442 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2443 0, GEN7_DOP_CLOCK_GATE_ENABLE);
2444
2445 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
2446 }
2447
oa_config_flex_reg(const struct i915_oa_config * oa_config,i915_reg_t reg)2448 static u32 oa_config_flex_reg(const struct i915_oa_config *oa_config,
2449 i915_reg_t reg)
2450 {
2451 u32 mmio = i915_mmio_reg_offset(reg);
2452 int i;
2453
2454 /*
2455 * This arbitrary default will select the 'EU FPU0 Pipeline
2456 * Active' event. In the future it's anticipated that there
2457 * will be an explicit 'No Event' we can select, but not yet...
2458 */
2459 if (!oa_config)
2460 return 0;
2461
2462 for (i = 0; i < oa_config->flex_regs_len; i++) {
2463 if (i915_mmio_reg_offset(oa_config->flex_regs[i].addr) == mmio)
2464 return oa_config->flex_regs[i].value;
2465 }
2466
2467 return 0;
2468 }
2469 /*
2470 * NB: It must always remain pointer safe to run this even if the OA unit
2471 * has been disabled.
2472 *
2473 * It's fine to put out-of-date values into these per-context registers
2474 * in the case that the OA unit has been disabled.
2475 */
2476 static void
gen8_update_reg_state_unlocked(const struct intel_context * ce,const struct i915_perf_stream * stream)2477 gen8_update_reg_state_unlocked(const struct intel_context *ce,
2478 const struct i915_perf_stream *stream)
2479 {
2480 u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
2481 u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
2482 /* The MMIO offsets for Flex EU registers aren't contiguous */
2483 static const i915_reg_t flex_regs[] = {
2484 EU_PERF_CNTL0,
2485 EU_PERF_CNTL1,
2486 EU_PERF_CNTL2,
2487 EU_PERF_CNTL3,
2488 EU_PERF_CNTL4,
2489 EU_PERF_CNTL5,
2490 EU_PERF_CNTL6,
2491 };
2492 u32 *reg_state = ce->lrc_reg_state;
2493 int i;
2494
2495 reg_state[ctx_oactxctrl + 1] =
2496 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2497 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2498 GEN8_OA_COUNTER_RESUME;
2499
2500 for (i = 0; i < ARRAY_SIZE(flex_regs); i++)
2501 reg_state[ctx_flexeu0 + i * 2 + 1] =
2502 oa_config_flex_reg(stream->oa_config, flex_regs[i]);
2503 }
2504
2505 struct flex {
2506 i915_reg_t reg;
2507 u32 offset;
2508 u32 value;
2509 };
2510
2511 static int
gen8_store_flex(struct i915_request * rq,struct intel_context * ce,const struct flex * flex,unsigned int count)2512 gen8_store_flex(struct i915_request *rq,
2513 struct intel_context *ce,
2514 const struct flex *flex, unsigned int count)
2515 {
2516 u32 offset;
2517 u32 *cs;
2518
2519 cs = intel_ring_begin(rq, 4 * count);
2520 if (IS_ERR(cs))
2521 return PTR_ERR(cs);
2522
2523 offset = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET;
2524 do {
2525 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
2526 *cs++ = offset + flex->offset * sizeof(u32);
2527 *cs++ = 0;
2528 *cs++ = flex->value;
2529 } while (flex++, --count);
2530
2531 intel_ring_advance(rq, cs);
2532
2533 return 0;
2534 }
2535
2536 static int
gen8_load_flex(struct i915_request * rq,struct intel_context * ce,const struct flex * flex,unsigned int count)2537 gen8_load_flex(struct i915_request *rq,
2538 struct intel_context *ce,
2539 const struct flex *flex, unsigned int count)
2540 {
2541 u32 *cs;
2542
2543 GEM_BUG_ON(!count || count > 63);
2544
2545 cs = intel_ring_begin(rq, 2 * count + 2);
2546 if (IS_ERR(cs))
2547 return PTR_ERR(cs);
2548
2549 *cs++ = MI_LOAD_REGISTER_IMM(count);
2550 do {
2551 *cs++ = i915_mmio_reg_offset(flex->reg);
2552 *cs++ = flex->value;
2553 } while (flex++, --count);
2554 *cs++ = MI_NOOP;
2555
2556 intel_ring_advance(rq, cs);
2557
2558 return 0;
2559 }
2560
gen8_modify_context(struct intel_context * ce,const struct flex * flex,unsigned int count)2561 static int gen8_modify_context(struct intel_context *ce,
2562 const struct flex *flex, unsigned int count)
2563 {
2564 struct i915_request *rq;
2565 int err;
2566
2567 rq = intel_engine_create_kernel_request(ce->engine);
2568 if (IS_ERR(rq))
2569 return PTR_ERR(rq);
2570
2571 /* Serialise with the remote context */
2572 err = intel_context_prepare_remote_request(ce, rq);
2573 if (err == 0)
2574 err = gen8_store_flex(rq, ce, flex, count);
2575
2576 i915_request_add(rq);
2577 return err;
2578 }
2579
2580 static int
gen8_modify_self(struct intel_context * ce,const struct flex * flex,unsigned int count,struct i915_active * active)2581 gen8_modify_self(struct intel_context *ce,
2582 const struct flex *flex, unsigned int count,
2583 struct i915_active *active)
2584 {
2585 struct i915_request *rq;
2586 int err;
2587
2588 intel_engine_pm_get(ce->engine);
2589 rq = i915_request_create(ce);
2590 intel_engine_pm_put(ce->engine);
2591 if (IS_ERR(rq))
2592 return PTR_ERR(rq);
2593
2594 if (!IS_ERR_OR_NULL(active)) {
2595 err = i915_active_add_request(active, rq);
2596 if (err)
2597 goto err_add_request;
2598 }
2599
2600 err = gen8_load_flex(rq, ce, flex, count);
2601 if (err)
2602 goto err_add_request;
2603
2604 err_add_request:
2605 i915_request_add(rq);
2606 return err;
2607 }
2608
gen8_configure_context(struct i915_perf_stream * stream,struct i915_gem_context * ctx,struct flex * flex,unsigned int count)2609 static int gen8_configure_context(struct i915_perf_stream *stream,
2610 struct i915_gem_context *ctx,
2611 struct flex *flex, unsigned int count)
2612 {
2613 struct i915_gem_engines_iter it;
2614 struct intel_context *ce;
2615 int err = 0;
2616
2617 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
2618 GEM_BUG_ON(ce == ce->engine->kernel_context);
2619
2620 if (ce->engine->class != RENDER_CLASS)
2621 continue;
2622
2623 /* Otherwise OA settings will be set upon first use */
2624 if (!intel_context_pin_if_active(ce))
2625 continue;
2626
2627 flex->value = intel_sseu_make_rpcs(ce->engine->gt, &ce->sseu);
2628 err = gen8_modify_context(ce, flex, count);
2629
2630 intel_context_unpin(ce);
2631 if (err)
2632 break;
2633 }
2634 i915_gem_context_unlock_engines(ctx);
2635
2636 return err;
2637 }
2638
gen12_configure_oar_context(struct i915_perf_stream * stream,struct i915_active * active)2639 static int gen12_configure_oar_context(struct i915_perf_stream *stream,
2640 struct i915_active *active)
2641 {
2642 int err;
2643 struct intel_context *ce = stream->pinned_ctx;
2644 u32 format = stream->oa_buffer.format->format;
2645 u32 offset = stream->perf->ctx_oactxctrl_offset;
2646 struct flex regs_context[] = {
2647 {
2648 GEN8_OACTXCONTROL,
2649 offset + 1,
2650 active ? GEN8_OA_COUNTER_RESUME : 0,
2651 },
2652 };
2653 /* Offsets in regs_lri are not used since this configuration is only
2654 * applied using LRI. Initialize the correct offsets for posterity.
2655 */
2656 #define GEN12_OAR_OACONTROL_OFFSET 0x5B0
2657 struct flex regs_lri[] = {
2658 {
2659 GEN12_OAR_OACONTROL,
2660 GEN12_OAR_OACONTROL_OFFSET + 1,
2661 (format << GEN12_OAR_OACONTROL_COUNTER_FORMAT_SHIFT) |
2662 (active ? GEN12_OAR_OACONTROL_COUNTER_ENABLE : 0)
2663 },
2664 {
2665 RING_CONTEXT_CONTROL(ce->engine->mmio_base),
2666 CTX_CONTEXT_CONTROL,
2667 _MASKED_FIELD(GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE,
2668 active ?
2669 GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE :
2670 0)
2671 },
2672 };
2673
2674 /* Modify the context image of pinned context with regs_context */
2675 err = intel_context_lock_pinned(ce);
2676 if (err)
2677 return err;
2678
2679 err = gen8_modify_context(ce, regs_context,
2680 ARRAY_SIZE(regs_context));
2681 intel_context_unlock_pinned(ce);
2682 if (err)
2683 return err;
2684
2685 /* Apply regs_lri using LRI with pinned context */
2686 return gen8_modify_self(ce, regs_lri, ARRAY_SIZE(regs_lri), active);
2687 }
2688
2689 /*
2690 * Manages updating the per-context aspects of the OA stream
2691 * configuration across all contexts.
2692 *
2693 * The awkward consideration here is that OACTXCONTROL controls the
2694 * exponent for periodic sampling which is primarily used for system
2695 * wide profiling where we'd like a consistent sampling period even in
2696 * the face of context switches.
2697 *
2698 * Our approach of updating the register state context (as opposed to
2699 * say using a workaround batch buffer) ensures that the hardware
2700 * won't automatically reload an out-of-date timer exponent even
2701 * transiently before a WA BB could be parsed.
2702 *
2703 * This function needs to:
2704 * - Ensure the currently running context's per-context OA state is
2705 * updated
2706 * - Ensure that all existing contexts will have the correct per-context
2707 * OA state if they are scheduled for use.
2708 * - Ensure any new contexts will be initialized with the correct
2709 * per-context OA state.
2710 *
2711 * Note: it's only the RCS/Render context that has any OA state.
2712 * Note: the first flex register passed must always be R_PWR_CLK_STATE
2713 */
2714 static int
oa_configure_all_contexts(struct i915_perf_stream * stream,struct flex * regs,size_t num_regs,struct i915_active * active)2715 oa_configure_all_contexts(struct i915_perf_stream *stream,
2716 struct flex *regs,
2717 size_t num_regs,
2718 struct i915_active *active)
2719 {
2720 struct drm_i915_private *i915 = stream->perf->i915;
2721 struct intel_engine_cs *engine;
2722 struct intel_gt *gt = stream->engine->gt;
2723 struct i915_gem_context *ctx, *cn;
2724 int err;
2725
2726 lockdep_assert_held(>->perf.lock);
2727
2728 /*
2729 * The OA register config is setup through the context image. This image
2730 * might be written to by the GPU on context switch (in particular on
2731 * lite-restore). This means we can't safely update a context's image,
2732 * if this context is scheduled/submitted to run on the GPU.
2733 *
2734 * We could emit the OA register config through the batch buffer but
2735 * this might leave small interval of time where the OA unit is
2736 * configured at an invalid sampling period.
2737 *
2738 * Note that since we emit all requests from a single ring, there
2739 * is still an implicit global barrier here that may cause a high
2740 * priority context to wait for an otherwise independent low priority
2741 * context. Contexts idle at the time of reconfiguration are not
2742 * trapped behind the barrier.
2743 */
2744 spin_lock(&i915->gem.contexts.lock);
2745 list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
2746 if (!kref_get_unless_zero(&ctx->ref))
2747 continue;
2748
2749 spin_unlock(&i915->gem.contexts.lock);
2750
2751 err = gen8_configure_context(stream, ctx, regs, num_regs);
2752 if (err) {
2753 i915_gem_context_put(ctx);
2754 return err;
2755 }
2756
2757 spin_lock(&i915->gem.contexts.lock);
2758 list_safe_reset_next(ctx, cn, link);
2759 i915_gem_context_put(ctx);
2760 }
2761 spin_unlock(&i915->gem.contexts.lock);
2762
2763 /*
2764 * After updating all other contexts, we need to modify ourselves.
2765 * If we don't modify the kernel_context, we do not get events while
2766 * idle.
2767 */
2768 for_each_uabi_engine(engine, i915) {
2769 struct intel_context *ce = engine->kernel_context;
2770
2771 if (engine->class != RENDER_CLASS)
2772 continue;
2773
2774 regs[0].value = intel_sseu_make_rpcs(engine->gt, &ce->sseu);
2775
2776 err = gen8_modify_self(ce, regs, num_regs, active);
2777 if (err)
2778 return err;
2779 }
2780
2781 return 0;
2782 }
2783
2784 static int
lrc_configure_all_contexts(struct i915_perf_stream * stream,const struct i915_oa_config * oa_config,struct i915_active * active)2785 lrc_configure_all_contexts(struct i915_perf_stream *stream,
2786 const struct i915_oa_config *oa_config,
2787 struct i915_active *active)
2788 {
2789 u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
2790 /* The MMIO offsets for Flex EU registers aren't contiguous */
2791 const u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
2792 #define ctx_flexeuN(N) (ctx_flexeu0 + 2 * (N) + 1)
2793 struct flex regs[] = {
2794 {
2795 GEN8_R_PWR_CLK_STATE(RENDER_RING_BASE),
2796 CTX_R_PWR_CLK_STATE,
2797 },
2798 {
2799 GEN8_OACTXCONTROL,
2800 ctx_oactxctrl + 1,
2801 },
2802 { EU_PERF_CNTL0, ctx_flexeuN(0) },
2803 { EU_PERF_CNTL1, ctx_flexeuN(1) },
2804 { EU_PERF_CNTL2, ctx_flexeuN(2) },
2805 { EU_PERF_CNTL3, ctx_flexeuN(3) },
2806 { EU_PERF_CNTL4, ctx_flexeuN(4) },
2807 { EU_PERF_CNTL5, ctx_flexeuN(5) },
2808 { EU_PERF_CNTL6, ctx_flexeuN(6) },
2809 };
2810 #undef ctx_flexeuN
2811 int i;
2812
2813 regs[1].value =
2814 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2815 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2816 GEN8_OA_COUNTER_RESUME;
2817
2818 for (i = 2; i < ARRAY_SIZE(regs); i++)
2819 regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
2820
2821 return oa_configure_all_contexts(stream,
2822 regs, ARRAY_SIZE(regs),
2823 active);
2824 }
2825
2826 static int
gen8_enable_metric_set(struct i915_perf_stream * stream,struct i915_active * active)2827 gen8_enable_metric_set(struct i915_perf_stream *stream,
2828 struct i915_active *active)
2829 {
2830 struct intel_uncore *uncore = stream->uncore;
2831 struct i915_oa_config *oa_config = stream->oa_config;
2832 int ret;
2833
2834 /*
2835 * We disable slice/unslice clock ratio change reports on SKL since
2836 * they are too noisy. The HW generates a lot of redundant reports
2837 * where the ratio hasn't really changed causing a lot of redundant
2838 * work to processes and increasing the chances we'll hit buffer
2839 * overruns.
2840 *
2841 * Although we don't currently use the 'disable overrun' OABUFFER
2842 * feature it's worth noting that clock ratio reports have to be
2843 * disabled before considering to use that feature since the HW doesn't
2844 * correctly block these reports.
2845 *
2846 * Currently none of the high-level metrics we have depend on knowing
2847 * this ratio to normalize.
2848 *
2849 * Note: This register is not power context saved and restored, but
2850 * that's OK considering that we disable RC6 while the OA unit is
2851 * enabled.
2852 *
2853 * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
2854 * be read back from automatically triggered reports, as part of the
2855 * RPT_ID field.
2856 */
2857 if (IS_GRAPHICS_VER(stream->perf->i915, 9, 11)) {
2858 intel_uncore_write(uncore, GEN8_OA_DEBUG,
2859 _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2860 GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
2861 }
2862
2863 /*
2864 * Update all contexts prior writing the mux configurations as we need
2865 * to make sure all slices/subslices are ON before writing to NOA
2866 * registers.
2867 */
2868 ret = lrc_configure_all_contexts(stream, oa_config, active);
2869 if (ret)
2870 return ret;
2871
2872 return emit_oa_config(stream,
2873 stream->oa_config, oa_context(stream),
2874 active);
2875 }
2876
oag_report_ctx_switches(const struct i915_perf_stream * stream)2877 static u32 oag_report_ctx_switches(const struct i915_perf_stream *stream)
2878 {
2879 return _MASKED_FIELD(GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS,
2880 (stream->sample_flags & SAMPLE_OA_REPORT) ?
2881 0 : GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS);
2882 }
2883
2884 static int
gen12_enable_metric_set(struct i915_perf_stream * stream,struct i915_active * active)2885 gen12_enable_metric_set(struct i915_perf_stream *stream,
2886 struct i915_active *active)
2887 {
2888 struct drm_i915_private *i915 = stream->perf->i915;
2889 struct intel_uncore *uncore = stream->uncore;
2890 bool periodic = stream->periodic;
2891 u32 period_exponent = stream->period_exponent;
2892 u32 sqcnt1;
2893 int ret;
2894
2895 /*
2896 * Wa_1508761755:xehpsdv, dg2
2897 * EU NOA signals behave incorrectly if EU clock gating is enabled.
2898 * Disable thread stall DOP gating and EU DOP gating.
2899 */
2900 if (IS_XEHPSDV(i915) || IS_DG2(i915)) {
2901 intel_gt_mcr_multicast_write(uncore->gt, GEN8_ROW_CHICKEN,
2902 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
2903 intel_uncore_write(uncore, GEN7_ROW_CHICKEN2,
2904 _MASKED_BIT_ENABLE(GEN12_DISABLE_DOP_GATING));
2905 }
2906
2907 intel_uncore_write(uncore, __oa_regs(stream)->oa_debug,
2908 /* Disable clk ratio reports, like previous Gens. */
2909 _MASKED_BIT_ENABLE(GEN12_OAG_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2910 GEN12_OAG_OA_DEBUG_INCLUDE_CLK_RATIO) |
2911 /*
2912 * If the user didn't require OA reports, instruct
2913 * the hardware not to emit ctx switch reports.
2914 */
2915 oag_report_ctx_switches(stream));
2916
2917 intel_uncore_write(uncore, __oa_regs(stream)->oa_ctx_ctrl, periodic ?
2918 (GEN12_OAG_OAGLBCTXCTRL_COUNTER_RESUME |
2919 GEN12_OAG_OAGLBCTXCTRL_TIMER_ENABLE |
2920 (period_exponent << GEN12_OAG_OAGLBCTXCTRL_TIMER_PERIOD_SHIFT))
2921 : 0);
2922
2923 /*
2924 * Initialize Super Queue Internal Cnt Register
2925 * Set PMON Enable in order to collect valid metrics.
2926 * Enable byets per clock reporting in OA for XEHPSDV onward.
2927 */
2928 sqcnt1 = GEN12_SQCNT1_PMON_ENABLE |
2929 (HAS_OA_BPC_REPORTING(i915) ? GEN12_SQCNT1_OABPC : 0);
2930
2931 intel_uncore_rmw(uncore, GEN12_SQCNT1, 0, sqcnt1);
2932
2933 /*
2934 * For Gen12, performance counters are context
2935 * saved/restored. Only enable it for the context that
2936 * requested this.
2937 */
2938 if (stream->ctx) {
2939 ret = gen12_configure_oar_context(stream, active);
2940 if (ret)
2941 return ret;
2942 }
2943
2944 return emit_oa_config(stream,
2945 stream->oa_config, oa_context(stream),
2946 active);
2947 }
2948
gen8_disable_metric_set(struct i915_perf_stream * stream)2949 static void gen8_disable_metric_set(struct i915_perf_stream *stream)
2950 {
2951 struct intel_uncore *uncore = stream->uncore;
2952
2953 /* Reset all contexts' slices/subslices configurations. */
2954 lrc_configure_all_contexts(stream, NULL, NULL);
2955
2956 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
2957 }
2958
gen11_disable_metric_set(struct i915_perf_stream * stream)2959 static void gen11_disable_metric_set(struct i915_perf_stream *stream)
2960 {
2961 struct intel_uncore *uncore = stream->uncore;
2962
2963 /* Reset all contexts' slices/subslices configurations. */
2964 lrc_configure_all_contexts(stream, NULL, NULL);
2965
2966 /* Make sure we disable noa to save power. */
2967 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
2968 }
2969
gen12_disable_metric_set(struct i915_perf_stream * stream)2970 static void gen12_disable_metric_set(struct i915_perf_stream *stream)
2971 {
2972 struct intel_uncore *uncore = stream->uncore;
2973 struct drm_i915_private *i915 = stream->perf->i915;
2974 u32 sqcnt1;
2975
2976 /*
2977 * Wa_1508761755:xehpsdv, dg2
2978 * Enable thread stall DOP gating and EU DOP gating.
2979 */
2980 if (IS_XEHPSDV(i915) || IS_DG2(i915)) {
2981 intel_gt_mcr_multicast_write(uncore->gt, GEN8_ROW_CHICKEN,
2982 _MASKED_BIT_DISABLE(STALL_DOP_GATING_DISABLE));
2983 intel_uncore_write(uncore, GEN7_ROW_CHICKEN2,
2984 _MASKED_BIT_DISABLE(GEN12_DISABLE_DOP_GATING));
2985 }
2986
2987 /* disable the context save/restore or OAR counters */
2988 if (stream->ctx)
2989 gen12_configure_oar_context(stream, NULL);
2990
2991 /* Make sure we disable noa to save power. */
2992 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
2993
2994 sqcnt1 = GEN12_SQCNT1_PMON_ENABLE |
2995 (HAS_OA_BPC_REPORTING(i915) ? GEN12_SQCNT1_OABPC : 0);
2996
2997 /* Reset PMON Enable to save power. */
2998 intel_uncore_rmw(uncore, GEN12_SQCNT1, sqcnt1, 0);
2999 }
3000
gen7_oa_enable(struct i915_perf_stream * stream)3001 static void gen7_oa_enable(struct i915_perf_stream *stream)
3002 {
3003 struct intel_uncore *uncore = stream->uncore;
3004 struct i915_gem_context *ctx = stream->ctx;
3005 u32 ctx_id = stream->specific_ctx_id;
3006 bool periodic = stream->periodic;
3007 u32 period_exponent = stream->period_exponent;
3008 u32 report_format = stream->oa_buffer.format->format;
3009
3010 /*
3011 * Reset buf pointers so we don't forward reports from before now.
3012 *
3013 * Think carefully if considering trying to avoid this, since it
3014 * also ensures status flags and the buffer itself are cleared
3015 * in error paths, and we have checks for invalid reports based
3016 * on the assumption that certain fields are written to zeroed
3017 * memory which this helps maintains.
3018 */
3019 gen7_init_oa_buffer(stream);
3020
3021 intel_uncore_write(uncore, GEN7_OACONTROL,
3022 (ctx_id & GEN7_OACONTROL_CTX_MASK) |
3023 (period_exponent <<
3024 GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
3025 (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
3026 (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
3027 (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
3028 GEN7_OACONTROL_ENABLE);
3029 }
3030
gen8_oa_enable(struct i915_perf_stream * stream)3031 static void gen8_oa_enable(struct i915_perf_stream *stream)
3032 {
3033 struct intel_uncore *uncore = stream->uncore;
3034 u32 report_format = stream->oa_buffer.format->format;
3035
3036 /*
3037 * Reset buf pointers so we don't forward reports from before now.
3038 *
3039 * Think carefully if considering trying to avoid this, since it
3040 * also ensures status flags and the buffer itself are cleared
3041 * in error paths, and we have checks for invalid reports based
3042 * on the assumption that certain fields are written to zeroed
3043 * memory which this helps maintains.
3044 */
3045 gen8_init_oa_buffer(stream);
3046
3047 /*
3048 * Note: we don't rely on the hardware to perform single context
3049 * filtering and instead filter on the cpu based on the context-id
3050 * field of reports
3051 */
3052 intel_uncore_write(uncore, GEN8_OACONTROL,
3053 (report_format << GEN8_OA_REPORT_FORMAT_SHIFT) |
3054 GEN8_OA_COUNTER_ENABLE);
3055 }
3056
gen12_oa_enable(struct i915_perf_stream * stream)3057 static void gen12_oa_enable(struct i915_perf_stream *stream)
3058 {
3059 const struct i915_perf_regs *regs;
3060 u32 val;
3061
3062 /*
3063 * If we don't want OA reports from the OA buffer, then we don't even
3064 * need to program the OAG unit.
3065 */
3066 if (!(stream->sample_flags & SAMPLE_OA_REPORT))
3067 return;
3068
3069 gen12_init_oa_buffer(stream);
3070
3071 regs = __oa_regs(stream);
3072 val = (stream->oa_buffer.format->format << regs->oa_ctrl_counter_format_shift) |
3073 GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE;
3074
3075 intel_uncore_write(stream->uncore, regs->oa_ctrl, val);
3076 }
3077
3078 /**
3079 * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
3080 * @stream: An i915 perf stream opened for OA metrics
3081 *
3082 * [Re]enables hardware periodic sampling according to the period configured
3083 * when opening the stream. This also starts a hrtimer that will periodically
3084 * check for data in the circular OA buffer for notifying userspace (e.g.
3085 * during a read() or poll()).
3086 */
i915_oa_stream_enable(struct i915_perf_stream * stream)3087 static void i915_oa_stream_enable(struct i915_perf_stream *stream)
3088 {
3089 stream->pollin = false;
3090
3091 stream->perf->ops.oa_enable(stream);
3092
3093 if (stream->sample_flags & SAMPLE_OA_REPORT)
3094 hrtimer_start(&stream->poll_check_timer,
3095 ns_to_ktime(stream->poll_oa_period),
3096 HRTIMER_MODE_REL_PINNED);
3097 }
3098
gen7_oa_disable(struct i915_perf_stream * stream)3099 static void gen7_oa_disable(struct i915_perf_stream *stream)
3100 {
3101 struct intel_uncore *uncore = stream->uncore;
3102
3103 intel_uncore_write(uncore, GEN7_OACONTROL, 0);
3104 if (intel_wait_for_register(uncore,
3105 GEN7_OACONTROL, GEN7_OACONTROL_ENABLE, 0,
3106 50))
3107 drm_err(&stream->perf->i915->drm,
3108 "wait for OA to be disabled timed out\n");
3109 }
3110
gen8_oa_disable(struct i915_perf_stream * stream)3111 static void gen8_oa_disable(struct i915_perf_stream *stream)
3112 {
3113 struct intel_uncore *uncore = stream->uncore;
3114
3115 intel_uncore_write(uncore, GEN8_OACONTROL, 0);
3116 if (intel_wait_for_register(uncore,
3117 GEN8_OACONTROL, GEN8_OA_COUNTER_ENABLE, 0,
3118 50))
3119 drm_err(&stream->perf->i915->drm,
3120 "wait for OA to be disabled timed out\n");
3121 }
3122
gen12_oa_disable(struct i915_perf_stream * stream)3123 static void gen12_oa_disable(struct i915_perf_stream *stream)
3124 {
3125 struct intel_uncore *uncore = stream->uncore;
3126
3127 intel_uncore_write(uncore, __oa_regs(stream)->oa_ctrl, 0);
3128 if (intel_wait_for_register(uncore,
3129 __oa_regs(stream)->oa_ctrl,
3130 GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE, 0,
3131 50))
3132 drm_err(&stream->perf->i915->drm,
3133 "wait for OA to be disabled timed out\n");
3134
3135 intel_uncore_write(uncore, GEN12_OA_TLB_INV_CR, 1);
3136 if (intel_wait_for_register(uncore,
3137 GEN12_OA_TLB_INV_CR,
3138 1, 0,
3139 50))
3140 drm_err(&stream->perf->i915->drm,
3141 "wait for OA tlb invalidate timed out\n");
3142 }
3143
3144 /**
3145 * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
3146 * @stream: An i915 perf stream opened for OA metrics
3147 *
3148 * Stops the OA unit from periodically writing counter reports into the
3149 * circular OA buffer. This also stops the hrtimer that periodically checks for
3150 * data in the circular OA buffer, for notifying userspace.
3151 */
i915_oa_stream_disable(struct i915_perf_stream * stream)3152 static void i915_oa_stream_disable(struct i915_perf_stream *stream)
3153 {
3154 stream->perf->ops.oa_disable(stream);
3155
3156 if (stream->sample_flags & SAMPLE_OA_REPORT)
3157 hrtimer_cancel(&stream->poll_check_timer);
3158 }
3159
3160 static const struct i915_perf_stream_ops i915_oa_stream_ops = {
3161 .destroy = i915_oa_stream_destroy,
3162 .enable = i915_oa_stream_enable,
3163 .disable = i915_oa_stream_disable,
3164 .wait_unlocked = i915_oa_wait_unlocked,
3165 .poll_wait = i915_oa_poll_wait,
3166 .read = i915_oa_read,
3167 };
3168
i915_perf_stream_enable_sync(struct i915_perf_stream * stream)3169 static int i915_perf_stream_enable_sync(struct i915_perf_stream *stream)
3170 {
3171 struct i915_active *active;
3172 int err;
3173
3174 active = i915_active_create();
3175 if (!active)
3176 return -ENOMEM;
3177
3178 err = stream->perf->ops.enable_metric_set(stream, active);
3179 if (err == 0)
3180 __i915_active_wait(active, TASK_UNINTERRUPTIBLE);
3181
3182 i915_active_put(active);
3183 return err;
3184 }
3185
3186 static void
get_default_sseu_config(struct intel_sseu * out_sseu,struct intel_engine_cs * engine)3187 get_default_sseu_config(struct intel_sseu *out_sseu,
3188 struct intel_engine_cs *engine)
3189 {
3190 const struct sseu_dev_info *devinfo_sseu = &engine->gt->info.sseu;
3191
3192 *out_sseu = intel_sseu_from_device_info(devinfo_sseu);
3193
3194 if (GRAPHICS_VER(engine->i915) == 11) {
3195 /*
3196 * We only need subslice count so it doesn't matter which ones
3197 * we select - just turn off low bits in the amount of half of
3198 * all available subslices per slice.
3199 */
3200 out_sseu->subslice_mask =
3201 ~(~0 << (hweight8(out_sseu->subslice_mask) / 2));
3202 out_sseu->slice_mask = 0x1;
3203 }
3204 }
3205
3206 static int
get_sseu_config(struct intel_sseu * out_sseu,struct intel_engine_cs * engine,const struct drm_i915_gem_context_param_sseu * drm_sseu)3207 get_sseu_config(struct intel_sseu *out_sseu,
3208 struct intel_engine_cs *engine,
3209 const struct drm_i915_gem_context_param_sseu *drm_sseu)
3210 {
3211 if (drm_sseu->engine.engine_class != engine->uabi_class ||
3212 drm_sseu->engine.engine_instance != engine->uabi_instance)
3213 return -EINVAL;
3214
3215 return i915_gem_user_to_context_sseu(engine->gt, drm_sseu, out_sseu);
3216 }
3217
3218 /*
3219 * OA timestamp frequency = CS timestamp frequency in most platforms. On some
3220 * platforms OA unit ignores the CTC_SHIFT and the 2 timestamps differ. In such
3221 * cases, return the adjusted CS timestamp frequency to the user.
3222 */
i915_perf_oa_timestamp_frequency(struct drm_i915_private * i915)3223 u32 i915_perf_oa_timestamp_frequency(struct drm_i915_private *i915)
3224 {
3225 struct intel_gt *gt = to_gt(i915);
3226
3227 /* Wa_18013179988 */
3228 if (IS_DG2(i915) || IS_GFX_GT_IP_RANGE(gt, IP_VER(12, 70), IP_VER(12, 74))) {
3229 intel_wakeref_t wakeref;
3230 u32 reg, shift;
3231
3232 with_intel_runtime_pm(to_gt(i915)->uncore->rpm, wakeref)
3233 reg = intel_uncore_read(to_gt(i915)->uncore, RPM_CONFIG0);
3234
3235 shift = REG_FIELD_GET(GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK,
3236 reg);
3237
3238 return to_gt(i915)->clock_frequency << (3 - shift);
3239 }
3240
3241 return to_gt(i915)->clock_frequency;
3242 }
3243
3244 /**
3245 * i915_oa_stream_init - validate combined props for OA stream and init
3246 * @stream: An i915 perf stream
3247 * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
3248 * @props: The property state that configures stream (individually validated)
3249 *
3250 * While read_properties_unlocked() validates properties in isolation it
3251 * doesn't ensure that the combination necessarily makes sense.
3252 *
3253 * At this point it has been determined that userspace wants a stream of
3254 * OA metrics, but still we need to further validate the combined
3255 * properties are OK.
3256 *
3257 * If the configuration makes sense then we can allocate memory for
3258 * a circular OA buffer and apply the requested metric set configuration.
3259 *
3260 * Returns: zero on success or a negative error code.
3261 */
i915_oa_stream_init(struct i915_perf_stream * stream,struct drm_i915_perf_open_param * param,struct perf_open_properties * props)3262 static int i915_oa_stream_init(struct i915_perf_stream *stream,
3263 struct drm_i915_perf_open_param *param,
3264 struct perf_open_properties *props)
3265 {
3266 struct drm_i915_private *i915 = stream->perf->i915;
3267 struct i915_perf *perf = stream->perf;
3268 struct i915_perf_group *g;
3269 struct intel_gt *gt;
3270 int ret;
3271
3272 if (!props->engine) {
3273 drm_dbg(&stream->perf->i915->drm,
3274 "OA engine not specified\n");
3275 return -EINVAL;
3276 }
3277 gt = props->engine->gt;
3278 g = props->engine->oa_group;
3279
3280 /*
3281 * If the sysfs metrics/ directory wasn't registered for some
3282 * reason then don't let userspace try their luck with config
3283 * IDs
3284 */
3285 if (!perf->metrics_kobj) {
3286 drm_dbg(&stream->perf->i915->drm,
3287 "OA metrics weren't advertised via sysfs\n");
3288 return -EINVAL;
3289 }
3290
3291 if (!(props->sample_flags & SAMPLE_OA_REPORT) &&
3292 (GRAPHICS_VER(perf->i915) < 12 || !stream->ctx)) {
3293 drm_dbg(&stream->perf->i915->drm,
3294 "Only OA report sampling supported\n");
3295 return -EINVAL;
3296 }
3297
3298 if (!perf->ops.enable_metric_set) {
3299 drm_dbg(&stream->perf->i915->drm,
3300 "OA unit not supported\n");
3301 return -ENODEV;
3302 }
3303
3304 /*
3305 * To avoid the complexity of having to accurately filter
3306 * counter reports and marshal to the appropriate client
3307 * we currently only allow exclusive access
3308 */
3309 if (g->exclusive_stream) {
3310 drm_dbg(&stream->perf->i915->drm,
3311 "OA unit already in use\n");
3312 return -EBUSY;
3313 }
3314
3315 if (!props->oa_format) {
3316 drm_dbg(&stream->perf->i915->drm,
3317 "OA report format not specified\n");
3318 return -EINVAL;
3319 }
3320
3321 stream->engine = props->engine;
3322 stream->uncore = stream->engine->gt->uncore;
3323
3324 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
3325
3326 stream->oa_buffer.format = &perf->oa_formats[props->oa_format];
3327 if (drm_WARN_ON(&i915->drm, stream->oa_buffer.format->size == 0))
3328 return -EINVAL;
3329
3330 stream->sample_flags = props->sample_flags;
3331 stream->sample_size += stream->oa_buffer.format->size;
3332
3333 stream->hold_preemption = props->hold_preemption;
3334
3335 stream->periodic = props->oa_periodic;
3336 if (stream->periodic)
3337 stream->period_exponent = props->oa_period_exponent;
3338
3339 if (stream->ctx) {
3340 ret = oa_get_render_ctx_id(stream);
3341 if (ret) {
3342 drm_dbg(&stream->perf->i915->drm,
3343 "Invalid context id to filter with\n");
3344 return ret;
3345 }
3346 }
3347
3348 ret = alloc_noa_wait(stream);
3349 if (ret) {
3350 drm_dbg(&stream->perf->i915->drm,
3351 "Unable to allocate NOA wait batch buffer\n");
3352 goto err_noa_wait_alloc;
3353 }
3354
3355 stream->oa_config = i915_perf_get_oa_config(perf, props->metrics_set);
3356 if (!stream->oa_config) {
3357 drm_dbg(&stream->perf->i915->drm,
3358 "Invalid OA config id=%i\n", props->metrics_set);
3359 ret = -EINVAL;
3360 goto err_config;
3361 }
3362
3363 /* PRM - observability performance counters:
3364 *
3365 * OACONTROL, performance counter enable, note:
3366 *
3367 * "When this bit is set, in order to have coherent counts,
3368 * RC6 power state and trunk clock gating must be disabled.
3369 * This can be achieved by programming MMIO registers as
3370 * 0xA094=0 and 0xA090[31]=1"
3371 *
3372 * In our case we are expecting that taking pm + FORCEWAKE
3373 * references will effectively disable RC6.
3374 */
3375 intel_engine_pm_get(stream->engine);
3376 intel_uncore_forcewake_get(stream->uncore, FORCEWAKE_ALL);
3377
3378 /*
3379 * Wa_16011777198:dg2: GuC resets render as part of the Wa. This causes
3380 * OA to lose the configuration state. Prevent this by overriding GUCRC
3381 * mode.
3382 */
3383 if (intel_uc_uses_guc_rc(>->uc) &&
3384 (IS_DG2_GRAPHICS_STEP(gt->i915, G10, STEP_A0, STEP_C0) ||
3385 IS_DG2_GRAPHICS_STEP(gt->i915, G11, STEP_A0, STEP_B0))) {
3386 ret = intel_guc_slpc_override_gucrc_mode(>->uc.guc.slpc,
3387 SLPC_GUCRC_MODE_GUCRC_NO_RC6);
3388 if (ret) {
3389 drm_dbg(&stream->perf->i915->drm,
3390 "Unable to override gucrc mode\n");
3391 goto err_gucrc;
3392 }
3393
3394 stream->override_gucrc = true;
3395 }
3396
3397 ret = alloc_oa_buffer(stream);
3398 if (ret)
3399 goto err_oa_buf_alloc;
3400
3401 stream->ops = &i915_oa_stream_ops;
3402
3403 stream->engine->gt->perf.sseu = props->sseu;
3404 WRITE_ONCE(g->exclusive_stream, stream);
3405
3406 ret = i915_perf_stream_enable_sync(stream);
3407 if (ret) {
3408 drm_dbg(&stream->perf->i915->drm,
3409 "Unable to enable metric set\n");
3410 goto err_enable;
3411 }
3412
3413 drm_dbg(&stream->perf->i915->drm,
3414 "opening stream oa config uuid=%s\n",
3415 stream->oa_config->uuid);
3416
3417 hrtimer_init(&stream->poll_check_timer,
3418 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3419 stream->poll_check_timer.function = oa_poll_check_timer_cb;
3420 init_waitqueue_head(&stream->poll_wq);
3421 spin_lock_init(&stream->oa_buffer.ptr_lock);
3422 mutex_init(&stream->lock);
3423
3424 return 0;
3425
3426 err_enable:
3427 WRITE_ONCE(g->exclusive_stream, NULL);
3428 perf->ops.disable_metric_set(stream);
3429
3430 free_oa_buffer(stream);
3431
3432 err_oa_buf_alloc:
3433 if (stream->override_gucrc)
3434 intel_guc_slpc_unset_gucrc_mode(>->uc.guc.slpc);
3435
3436 err_gucrc:
3437 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
3438 intel_engine_pm_put(stream->engine);
3439
3440 free_oa_configs(stream);
3441
3442 err_config:
3443 free_noa_wait(stream);
3444
3445 err_noa_wait_alloc:
3446 if (stream->ctx)
3447 oa_put_render_ctx_id(stream);
3448
3449 return ret;
3450 }
3451
i915_oa_init_reg_state(const struct intel_context * ce,const struct intel_engine_cs * engine)3452 void i915_oa_init_reg_state(const struct intel_context *ce,
3453 const struct intel_engine_cs *engine)
3454 {
3455 struct i915_perf_stream *stream;
3456
3457 if (engine->class != RENDER_CLASS)
3458 return;
3459
3460 /* perf.exclusive_stream serialised by lrc_configure_all_contexts() */
3461 stream = READ_ONCE(engine->oa_group->exclusive_stream);
3462 if (stream && GRAPHICS_VER(stream->perf->i915) < 12)
3463 gen8_update_reg_state_unlocked(ce, stream);
3464 }
3465
3466 /**
3467 * i915_perf_read - handles read() FOP for i915 perf stream FDs
3468 * @file: An i915 perf stream file
3469 * @buf: destination buffer given by userspace
3470 * @count: the number of bytes userspace wants to read
3471 * @ppos: (inout) file seek position (unused)
3472 *
3473 * The entry point for handling a read() on a stream file descriptor from
3474 * userspace. Most of the work is left to the i915_perf_read_locked() and
3475 * &i915_perf_stream_ops->read but to save having stream implementations (of
3476 * which we might have multiple later) we handle blocking read here.
3477 *
3478 * We can also consistently treat trying to read from a disabled stream
3479 * as an IO error so implementations can assume the stream is enabled
3480 * while reading.
3481 *
3482 * Returns: The number of bytes copied or a negative error code on failure.
3483 */
i915_perf_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)3484 static ssize_t i915_perf_read(struct file *file,
3485 char __user *buf,
3486 size_t count,
3487 loff_t *ppos)
3488 {
3489 struct i915_perf_stream *stream = file->private_data;
3490 size_t offset = 0;
3491 int ret;
3492
3493 /* To ensure it's handled consistently we simply treat all reads of a
3494 * disabled stream as an error. In particular it might otherwise lead
3495 * to a deadlock for blocking file descriptors...
3496 */
3497 if (!stream->enabled || !(stream->sample_flags & SAMPLE_OA_REPORT))
3498 return -EIO;
3499
3500 if (!(file->f_flags & O_NONBLOCK)) {
3501 /* There's the small chance of false positives from
3502 * stream->ops->wait_unlocked.
3503 *
3504 * E.g. with single context filtering since we only wait until
3505 * oabuffer has >= 1 report we don't immediately know whether
3506 * any reports really belong to the current context
3507 */
3508 do {
3509 ret = stream->ops->wait_unlocked(stream);
3510 if (ret)
3511 return ret;
3512
3513 mutex_lock(&stream->lock);
3514 ret = stream->ops->read(stream, buf, count, &offset);
3515 mutex_unlock(&stream->lock);
3516 } while (!offset && !ret);
3517 } else {
3518 mutex_lock(&stream->lock);
3519 ret = stream->ops->read(stream, buf, count, &offset);
3520 mutex_unlock(&stream->lock);
3521 }
3522
3523 /* We allow the poll checking to sometimes report false positive EPOLLIN
3524 * events where we might actually report EAGAIN on read() if there's
3525 * not really any data available. In this situation though we don't
3526 * want to enter a busy loop between poll() reporting a EPOLLIN event
3527 * and read() returning -EAGAIN. Clearing the oa.pollin state here
3528 * effectively ensures we back off until the next hrtimer callback
3529 * before reporting another EPOLLIN event.
3530 * The exception to this is if ops->read() returned -ENOSPC which means
3531 * that more OA data is available than could fit in the user provided
3532 * buffer. In this case we want the next poll() call to not block.
3533 */
3534 if (ret != -ENOSPC)
3535 stream->pollin = false;
3536
3537 /* Possible values for ret are 0, -EFAULT, -ENOSPC, -EIO, ... */
3538 return offset ?: (ret ?: -EAGAIN);
3539 }
3540
oa_poll_check_timer_cb(struct hrtimer * hrtimer)3541 static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
3542 {
3543 struct i915_perf_stream *stream =
3544 container_of(hrtimer, typeof(*stream), poll_check_timer);
3545
3546 if (oa_buffer_check_unlocked(stream)) {
3547 stream->pollin = true;
3548 wake_up(&stream->poll_wq);
3549 }
3550
3551 hrtimer_forward_now(hrtimer,
3552 ns_to_ktime(stream->poll_oa_period));
3553
3554 return HRTIMER_RESTART;
3555 }
3556
3557 /**
3558 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
3559 * @stream: An i915 perf stream
3560 * @file: An i915 perf stream file
3561 * @wait: poll() state table
3562 *
3563 * For handling userspace polling on an i915 perf stream, this calls through to
3564 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
3565 * will be woken for new stream data.
3566 *
3567 * Returns: any poll events that are ready without sleeping
3568 */
i915_perf_poll_locked(struct i915_perf_stream * stream,struct file * file,poll_table * wait)3569 static __poll_t i915_perf_poll_locked(struct i915_perf_stream *stream,
3570 struct file *file,
3571 poll_table *wait)
3572 {
3573 __poll_t events = 0;
3574
3575 stream->ops->poll_wait(stream, file, wait);
3576
3577 /* Note: we don't explicitly check whether there's something to read
3578 * here since this path may be very hot depending on what else
3579 * userspace is polling, or on the timeout in use. We rely solely on
3580 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
3581 * samples to read.
3582 */
3583 if (stream->pollin)
3584 events |= EPOLLIN;
3585
3586 return events;
3587 }
3588
3589 /**
3590 * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
3591 * @file: An i915 perf stream file
3592 * @wait: poll() state table
3593 *
3594 * For handling userspace polling on an i915 perf stream, this ensures
3595 * poll_wait() gets called with a wait queue that will be woken for new stream
3596 * data.
3597 *
3598 * Note: Implementation deferred to i915_perf_poll_locked()
3599 *
3600 * Returns: any poll events that are ready without sleeping
3601 */
i915_perf_poll(struct file * file,poll_table * wait)3602 static __poll_t i915_perf_poll(struct file *file, poll_table *wait)
3603 {
3604 struct i915_perf_stream *stream = file->private_data;
3605 __poll_t ret;
3606
3607 mutex_lock(&stream->lock);
3608 ret = i915_perf_poll_locked(stream, file, wait);
3609 mutex_unlock(&stream->lock);
3610
3611 return ret;
3612 }
3613
3614 /**
3615 * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
3616 * @stream: A disabled i915 perf stream
3617 *
3618 * [Re]enables the associated capture of data for this stream.
3619 *
3620 * If a stream was previously enabled then there's currently no intention
3621 * to provide userspace any guarantee about the preservation of previously
3622 * buffered data.
3623 */
i915_perf_enable_locked(struct i915_perf_stream * stream)3624 static void i915_perf_enable_locked(struct i915_perf_stream *stream)
3625 {
3626 if (stream->enabled)
3627 return;
3628
3629 /* Allow stream->ops->enable() to refer to this */
3630 stream->enabled = true;
3631
3632 if (stream->ops->enable)
3633 stream->ops->enable(stream);
3634
3635 if (stream->hold_preemption)
3636 intel_context_set_nopreempt(stream->pinned_ctx);
3637 }
3638
3639 /**
3640 * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
3641 * @stream: An enabled i915 perf stream
3642 *
3643 * Disables the associated capture of data for this stream.
3644 *
3645 * The intention is that disabling an re-enabling a stream will ideally be
3646 * cheaper than destroying and re-opening a stream with the same configuration,
3647 * though there are no formal guarantees about what state or buffered data
3648 * must be retained between disabling and re-enabling a stream.
3649 *
3650 * Note: while a stream is disabled it's considered an error for userspace
3651 * to attempt to read from the stream (-EIO).
3652 */
i915_perf_disable_locked(struct i915_perf_stream * stream)3653 static void i915_perf_disable_locked(struct i915_perf_stream *stream)
3654 {
3655 if (!stream->enabled)
3656 return;
3657
3658 /* Allow stream->ops->disable() to refer to this */
3659 stream->enabled = false;
3660
3661 if (stream->hold_preemption)
3662 intel_context_clear_nopreempt(stream->pinned_ctx);
3663
3664 if (stream->ops->disable)
3665 stream->ops->disable(stream);
3666 }
3667
i915_perf_config_locked(struct i915_perf_stream * stream,unsigned long metrics_set)3668 static long i915_perf_config_locked(struct i915_perf_stream *stream,
3669 unsigned long metrics_set)
3670 {
3671 struct i915_oa_config *config;
3672 long ret = stream->oa_config->id;
3673
3674 config = i915_perf_get_oa_config(stream->perf, metrics_set);
3675 if (!config)
3676 return -EINVAL;
3677
3678 if (config != stream->oa_config) {
3679 int err;
3680
3681 /*
3682 * If OA is bound to a specific context, emit the
3683 * reconfiguration inline from that context. The update
3684 * will then be ordered with respect to submission on that
3685 * context.
3686 *
3687 * When set globally, we use a low priority kernel context,
3688 * so it will effectively take effect when idle.
3689 */
3690 err = emit_oa_config(stream, config, oa_context(stream), NULL);
3691 if (!err)
3692 config = xchg(&stream->oa_config, config);
3693 else
3694 ret = err;
3695 }
3696
3697 i915_oa_config_put(config);
3698
3699 return ret;
3700 }
3701
3702 /**
3703 * i915_perf_ioctl_locked - support ioctl() usage with i915 perf stream FDs
3704 * @stream: An i915 perf stream
3705 * @cmd: the ioctl request
3706 * @arg: the ioctl data
3707 *
3708 * Returns: zero on success or a negative error code. Returns -EINVAL for
3709 * an unknown ioctl request.
3710 */
i915_perf_ioctl_locked(struct i915_perf_stream * stream,unsigned int cmd,unsigned long arg)3711 static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
3712 unsigned int cmd,
3713 unsigned long arg)
3714 {
3715 switch (cmd) {
3716 case I915_PERF_IOCTL_ENABLE:
3717 i915_perf_enable_locked(stream);
3718 return 0;
3719 case I915_PERF_IOCTL_DISABLE:
3720 i915_perf_disable_locked(stream);
3721 return 0;
3722 case I915_PERF_IOCTL_CONFIG:
3723 return i915_perf_config_locked(stream, arg);
3724 }
3725
3726 return -EINVAL;
3727 }
3728
3729 /**
3730 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
3731 * @file: An i915 perf stream file
3732 * @cmd: the ioctl request
3733 * @arg: the ioctl data
3734 *
3735 * Implementation deferred to i915_perf_ioctl_locked().
3736 *
3737 * Returns: zero on success or a negative error code. Returns -EINVAL for
3738 * an unknown ioctl request.
3739 */
i915_perf_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3740 static long i915_perf_ioctl(struct file *file,
3741 unsigned int cmd,
3742 unsigned long arg)
3743 {
3744 struct i915_perf_stream *stream = file->private_data;
3745 long ret;
3746
3747 mutex_lock(&stream->lock);
3748 ret = i915_perf_ioctl_locked(stream, cmd, arg);
3749 mutex_unlock(&stream->lock);
3750
3751 return ret;
3752 }
3753
3754 /**
3755 * i915_perf_destroy_locked - destroy an i915 perf stream
3756 * @stream: An i915 perf stream
3757 *
3758 * Frees all resources associated with the given i915 perf @stream, disabling
3759 * any associated data capture in the process.
3760 *
3761 * Note: The >->perf.lock mutex has been taken to serialize
3762 * with any non-file-operation driver hooks.
3763 */
i915_perf_destroy_locked(struct i915_perf_stream * stream)3764 static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
3765 {
3766 if (stream->enabled)
3767 i915_perf_disable_locked(stream);
3768
3769 if (stream->ops->destroy)
3770 stream->ops->destroy(stream);
3771
3772 if (stream->ctx)
3773 i915_gem_context_put(stream->ctx);
3774
3775 kfree(stream);
3776 }
3777
3778 /**
3779 * i915_perf_release - handles userspace close() of a stream file
3780 * @inode: anonymous inode associated with file
3781 * @file: An i915 perf stream file
3782 *
3783 * Cleans up any resources associated with an open i915 perf stream file.
3784 *
3785 * NB: close() can't really fail from the userspace point of view.
3786 *
3787 * Returns: zero on success or a negative error code.
3788 */
i915_perf_release(struct inode * inode,struct file * file)3789 static int i915_perf_release(struct inode *inode, struct file *file)
3790 {
3791 struct i915_perf_stream *stream = file->private_data;
3792 struct i915_perf *perf = stream->perf;
3793 struct intel_gt *gt = stream->engine->gt;
3794
3795 /*
3796 * Within this call, we know that the fd is being closed and we have no
3797 * other user of stream->lock. Use the perf lock to destroy the stream
3798 * here.
3799 */
3800 mutex_lock(>->perf.lock);
3801 i915_perf_destroy_locked(stream);
3802 mutex_unlock(>->perf.lock);
3803
3804 /* Release the reference the perf stream kept on the driver. */
3805 drm_dev_put(&perf->i915->drm);
3806
3807 return 0;
3808 }
3809
3810
3811 static const struct file_operations fops = {
3812 .owner = THIS_MODULE,
3813 .llseek = no_llseek,
3814 .release = i915_perf_release,
3815 .poll = i915_perf_poll,
3816 .read = i915_perf_read,
3817 .unlocked_ioctl = i915_perf_ioctl,
3818 /* Our ioctl have no arguments, so it's safe to use the same function
3819 * to handle 32bits compatibility.
3820 */
3821 .compat_ioctl = i915_perf_ioctl,
3822 };
3823
3824
3825 /**
3826 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
3827 * @perf: i915 perf instance
3828 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
3829 * @props: individually validated u64 property value pairs
3830 * @file: drm file
3831 *
3832 * See i915_perf_ioctl_open() for interface details.
3833 *
3834 * Implements further stream config validation and stream initialization on
3835 * behalf of i915_perf_open_ioctl() with the >->perf.lock mutex
3836 * taken to serialize with any non-file-operation driver hooks.
3837 *
3838 * Note: at this point the @props have only been validated in isolation and
3839 * it's still necessary to validate that the combination of properties makes
3840 * sense.
3841 *
3842 * In the case where userspace is interested in OA unit metrics then further
3843 * config validation and stream initialization details will be handled by
3844 * i915_oa_stream_init(). The code here should only validate config state that
3845 * will be relevant to all stream types / backends.
3846 *
3847 * Returns: zero on success or a negative error code.
3848 */
3849 static int
i915_perf_open_ioctl_locked(struct i915_perf * perf,struct drm_i915_perf_open_param * param,struct perf_open_properties * props,struct drm_file * file)3850 i915_perf_open_ioctl_locked(struct i915_perf *perf,
3851 struct drm_i915_perf_open_param *param,
3852 struct perf_open_properties *props,
3853 struct drm_file *file)
3854 {
3855 struct i915_gem_context *specific_ctx = NULL;
3856 struct i915_perf_stream *stream = NULL;
3857 unsigned long f_flags = 0;
3858 bool privileged_op = true;
3859 int stream_fd;
3860 int ret;
3861
3862 if (props->single_context) {
3863 u32 ctx_handle = props->ctx_handle;
3864 struct drm_i915_file_private *file_priv = file->driver_priv;
3865
3866 specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle);
3867 if (IS_ERR(specific_ctx)) {
3868 drm_dbg(&perf->i915->drm,
3869 "Failed to look up context with ID %u for opening perf stream\n",
3870 ctx_handle);
3871 ret = PTR_ERR(specific_ctx);
3872 goto err;
3873 }
3874 }
3875
3876 /*
3877 * On Haswell the OA unit supports clock gating off for a specific
3878 * context and in this mode there's no visibility of metrics for the
3879 * rest of the system, which we consider acceptable for a
3880 * non-privileged client.
3881 *
3882 * For Gen8->11 the OA unit no longer supports clock gating off for a
3883 * specific context and the kernel can't securely stop the counters
3884 * from updating as system-wide / global values. Even though we can
3885 * filter reports based on the included context ID we can't block
3886 * clients from seeing the raw / global counter values via
3887 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
3888 * enable the OA unit by default.
3889 *
3890 * For Gen12+ we gain a new OAR unit that only monitors the RCS on a
3891 * per context basis. So we can relax requirements there if the user
3892 * doesn't request global stream access (i.e. query based sampling
3893 * using MI_RECORD_PERF_COUNT.
3894 */
3895 if (IS_HASWELL(perf->i915) && specific_ctx)
3896 privileged_op = false;
3897 else if (GRAPHICS_VER(perf->i915) == 12 && specific_ctx &&
3898 (props->sample_flags & SAMPLE_OA_REPORT) == 0)
3899 privileged_op = false;
3900
3901 if (props->hold_preemption) {
3902 if (!props->single_context) {
3903 drm_dbg(&perf->i915->drm,
3904 "preemption disable with no context\n");
3905 ret = -EINVAL;
3906 goto err;
3907 }
3908 privileged_op = true;
3909 }
3910
3911 /*
3912 * Asking for SSEU configuration is a priviliged operation.
3913 */
3914 if (props->has_sseu)
3915 privileged_op = true;
3916 else
3917 get_default_sseu_config(&props->sseu, props->engine);
3918
3919 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
3920 * we check a dev.i915.perf_stream_paranoid sysctl option
3921 * to determine if it's ok to access system wide OA counters
3922 * without CAP_PERFMON or CAP_SYS_ADMIN privileges.
3923 */
3924 if (privileged_op &&
3925 i915_perf_stream_paranoid && !perfmon_capable()) {
3926 drm_dbg(&perf->i915->drm,
3927 "Insufficient privileges to open i915 perf stream\n");
3928 ret = -EACCES;
3929 goto err_ctx;
3930 }
3931
3932 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
3933 if (!stream) {
3934 ret = -ENOMEM;
3935 goto err_ctx;
3936 }
3937
3938 stream->perf = perf;
3939 stream->ctx = specific_ctx;
3940 stream->poll_oa_period = props->poll_oa_period;
3941
3942 ret = i915_oa_stream_init(stream, param, props);
3943 if (ret)
3944 goto err_alloc;
3945
3946 /* we avoid simply assigning stream->sample_flags = props->sample_flags
3947 * to have _stream_init check the combination of sample flags more
3948 * thoroughly, but still this is the expected result at this point.
3949 */
3950 if (WARN_ON(stream->sample_flags != props->sample_flags)) {
3951 ret = -ENODEV;
3952 goto err_flags;
3953 }
3954
3955 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
3956 f_flags |= O_CLOEXEC;
3957 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
3958 f_flags |= O_NONBLOCK;
3959
3960 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
3961 if (stream_fd < 0) {
3962 ret = stream_fd;
3963 goto err_flags;
3964 }
3965
3966 if (!(param->flags & I915_PERF_FLAG_DISABLED))
3967 i915_perf_enable_locked(stream);
3968
3969 /* Take a reference on the driver that will be kept with stream_fd
3970 * until its release.
3971 */
3972 drm_dev_get(&perf->i915->drm);
3973
3974 return stream_fd;
3975
3976 err_flags:
3977 if (stream->ops->destroy)
3978 stream->ops->destroy(stream);
3979 err_alloc:
3980 kfree(stream);
3981 err_ctx:
3982 if (specific_ctx)
3983 i915_gem_context_put(specific_ctx);
3984 err:
3985 return ret;
3986 }
3987
oa_exponent_to_ns(struct i915_perf * perf,int exponent)3988 static u64 oa_exponent_to_ns(struct i915_perf *perf, int exponent)
3989 {
3990 u64 nom = (2ULL << exponent) * NSEC_PER_SEC;
3991 u32 den = i915_perf_oa_timestamp_frequency(perf->i915);
3992
3993 return div_u64(nom + den - 1, den);
3994 }
3995
3996 static __always_inline bool
oa_format_valid(struct i915_perf * perf,enum drm_i915_oa_format format)3997 oa_format_valid(struct i915_perf *perf, enum drm_i915_oa_format format)
3998 {
3999 return test_bit(format, perf->format_mask);
4000 }
4001
4002 static __always_inline void
oa_format_add(struct i915_perf * perf,enum drm_i915_oa_format format)4003 oa_format_add(struct i915_perf *perf, enum drm_i915_oa_format format)
4004 {
4005 __set_bit(format, perf->format_mask);
4006 }
4007
4008 /**
4009 * read_properties_unlocked - validate + copy userspace stream open properties
4010 * @perf: i915 perf instance
4011 * @uprops: The array of u64 key value pairs given by userspace
4012 * @n_props: The number of key value pairs expected in @uprops
4013 * @props: The stream configuration built up while validating properties
4014 *
4015 * Note this function only validates properties in isolation it doesn't
4016 * validate that the combination of properties makes sense or that all
4017 * properties necessary for a particular kind of stream have been set.
4018 *
4019 * Note that there currently aren't any ordering requirements for properties so
4020 * we shouldn't validate or assume anything about ordering here. This doesn't
4021 * rule out defining new properties with ordering requirements in the future.
4022 */
read_properties_unlocked(struct i915_perf * perf,u64 __user * uprops,u32 n_props,struct perf_open_properties * props)4023 static int read_properties_unlocked(struct i915_perf *perf,
4024 u64 __user *uprops,
4025 u32 n_props,
4026 struct perf_open_properties *props)
4027 {
4028 struct drm_i915_gem_context_param_sseu user_sseu;
4029 const struct i915_oa_format *f;
4030 u64 __user *uprop = uprops;
4031 bool config_instance = false;
4032 bool config_class = false;
4033 bool config_sseu = false;
4034 u8 class, instance;
4035 u32 i;
4036 int ret;
4037
4038 memset(props, 0, sizeof(struct perf_open_properties));
4039 props->poll_oa_period = DEFAULT_POLL_PERIOD_NS;
4040
4041 /* Considering that ID = 0 is reserved and assuming that we don't
4042 * (currently) expect any configurations to ever specify duplicate
4043 * values for a particular property ID then the last _PROP_MAX value is
4044 * one greater than the maximum number of properties we expect to get
4045 * from userspace.
4046 */
4047 if (!n_props || n_props >= DRM_I915_PERF_PROP_MAX) {
4048 drm_dbg(&perf->i915->drm,
4049 "Invalid number of i915 perf properties given\n");
4050 return -EINVAL;
4051 }
4052
4053 /* Defaults when class:instance is not passed */
4054 class = I915_ENGINE_CLASS_RENDER;
4055 instance = 0;
4056
4057 for (i = 0; i < n_props; i++) {
4058 u64 oa_period, oa_freq_hz;
4059 u64 id, value;
4060
4061 ret = get_user(id, uprop);
4062 if (ret)
4063 return ret;
4064
4065 ret = get_user(value, uprop + 1);
4066 if (ret)
4067 return ret;
4068
4069 if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
4070 drm_dbg(&perf->i915->drm,
4071 "Unknown i915 perf property ID\n");
4072 return -EINVAL;
4073 }
4074
4075 switch ((enum drm_i915_perf_property_id)id) {
4076 case DRM_I915_PERF_PROP_CTX_HANDLE:
4077 props->single_context = 1;
4078 props->ctx_handle = value;
4079 break;
4080 case DRM_I915_PERF_PROP_SAMPLE_OA:
4081 if (value)
4082 props->sample_flags |= SAMPLE_OA_REPORT;
4083 break;
4084 case DRM_I915_PERF_PROP_OA_METRICS_SET:
4085 if (value == 0) {
4086 drm_dbg(&perf->i915->drm,
4087 "Unknown OA metric set ID\n");
4088 return -EINVAL;
4089 }
4090 props->metrics_set = value;
4091 break;
4092 case DRM_I915_PERF_PROP_OA_FORMAT:
4093 if (value == 0 || value >= I915_OA_FORMAT_MAX) {
4094 drm_dbg(&perf->i915->drm,
4095 "Out-of-range OA report format %llu\n",
4096 value);
4097 return -EINVAL;
4098 }
4099 if (!oa_format_valid(perf, value)) {
4100 drm_dbg(&perf->i915->drm,
4101 "Unsupported OA report format %llu\n",
4102 value);
4103 return -EINVAL;
4104 }
4105 props->oa_format = value;
4106 break;
4107 case DRM_I915_PERF_PROP_OA_EXPONENT:
4108 if (value > OA_EXPONENT_MAX) {
4109 drm_dbg(&perf->i915->drm,
4110 "OA timer exponent too high (> %u)\n",
4111 OA_EXPONENT_MAX);
4112 return -EINVAL;
4113 }
4114
4115 /* Theoretically we can program the OA unit to sample
4116 * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
4117 * for BXT. We don't allow such high sampling
4118 * frequencies by default unless root.
4119 */
4120
4121 BUILD_BUG_ON(sizeof(oa_period) != 8);
4122 oa_period = oa_exponent_to_ns(perf, value);
4123
4124 /* This check is primarily to ensure that oa_period <=
4125 * UINT32_MAX (before passing to do_div which only
4126 * accepts a u32 denominator), but we can also skip
4127 * checking anything < 1Hz which implicitly can't be
4128 * limited via an integer oa_max_sample_rate.
4129 */
4130 if (oa_period <= NSEC_PER_SEC) {
4131 u64 tmp = NSEC_PER_SEC;
4132 do_div(tmp, oa_period);
4133 oa_freq_hz = tmp;
4134 } else
4135 oa_freq_hz = 0;
4136
4137 if (oa_freq_hz > i915_oa_max_sample_rate && !perfmon_capable()) {
4138 drm_dbg(&perf->i915->drm,
4139 "OA exponent would exceed the max sampling frequency (sysctl dev.i915.oa_max_sample_rate) %uHz without CAP_PERFMON or CAP_SYS_ADMIN privileges\n",
4140 i915_oa_max_sample_rate);
4141 return -EACCES;
4142 }
4143
4144 props->oa_periodic = true;
4145 props->oa_period_exponent = value;
4146 break;
4147 case DRM_I915_PERF_PROP_HOLD_PREEMPTION:
4148 props->hold_preemption = !!value;
4149 break;
4150 case DRM_I915_PERF_PROP_GLOBAL_SSEU: {
4151 if (GRAPHICS_VER_FULL(perf->i915) >= IP_VER(12, 50)) {
4152 drm_dbg(&perf->i915->drm,
4153 "SSEU config not supported on gfx %x\n",
4154 GRAPHICS_VER_FULL(perf->i915));
4155 return -ENODEV;
4156 }
4157
4158 if (copy_from_user(&user_sseu,
4159 u64_to_user_ptr(value),
4160 sizeof(user_sseu))) {
4161 drm_dbg(&perf->i915->drm,
4162 "Unable to copy global sseu parameter\n");
4163 return -EFAULT;
4164 }
4165 config_sseu = true;
4166 break;
4167 }
4168 case DRM_I915_PERF_PROP_POLL_OA_PERIOD:
4169 if (value < 100000 /* 100us */) {
4170 drm_dbg(&perf->i915->drm,
4171 "OA availability timer too small (%lluns < 100us)\n",
4172 value);
4173 return -EINVAL;
4174 }
4175 props->poll_oa_period = value;
4176 break;
4177 case DRM_I915_PERF_PROP_OA_ENGINE_CLASS:
4178 class = (u8)value;
4179 config_class = true;
4180 break;
4181 case DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE:
4182 instance = (u8)value;
4183 config_instance = true;
4184 break;
4185 default:
4186 MISSING_CASE(id);
4187 return -EINVAL;
4188 }
4189
4190 uprop += 2;
4191 }
4192
4193 if ((config_class && !config_instance) ||
4194 (config_instance && !config_class)) {
4195 drm_dbg(&perf->i915->drm,
4196 "OA engine-class and engine-instance parameters must be passed together\n");
4197 return -EINVAL;
4198 }
4199
4200 props->engine = intel_engine_lookup_user(perf->i915, class, instance);
4201 if (!props->engine) {
4202 drm_dbg(&perf->i915->drm,
4203 "OA engine class and instance invalid %d:%d\n",
4204 class, instance);
4205 return -EINVAL;
4206 }
4207
4208 if (!engine_supports_oa(props->engine)) {
4209 drm_dbg(&perf->i915->drm,
4210 "Engine not supported by OA %d:%d\n",
4211 class, instance);
4212 return -EINVAL;
4213 }
4214
4215 /*
4216 * Wa_14017512683: mtl[a0..c0): Use of OAM must be preceded with Media
4217 * C6 disable in BIOS. Fail if Media C6 is enabled on steppings where OAM
4218 * does not work as expected.
4219 */
4220 if (IS_MTL_MEDIA_STEP(props->engine->i915, STEP_A0, STEP_C0) &&
4221 props->engine->oa_group->type == TYPE_OAM &&
4222 intel_check_bios_c6_setup(&props->engine->gt->rc6)) {
4223 drm_dbg(&perf->i915->drm,
4224 "OAM requires media C6 to be disabled in BIOS\n");
4225 return -EINVAL;
4226 }
4227
4228 i = array_index_nospec(props->oa_format, I915_OA_FORMAT_MAX);
4229 f = &perf->oa_formats[i];
4230 if (!engine_supports_oa_format(props->engine, f->type)) {
4231 drm_dbg(&perf->i915->drm,
4232 "Invalid OA format %d for class %d\n",
4233 f->type, props->engine->class);
4234 return -EINVAL;
4235 }
4236
4237 if (config_sseu) {
4238 ret = get_sseu_config(&props->sseu, props->engine, &user_sseu);
4239 if (ret) {
4240 drm_dbg(&perf->i915->drm,
4241 "Invalid SSEU configuration\n");
4242 return ret;
4243 }
4244 props->has_sseu = true;
4245 }
4246
4247 return 0;
4248 }
4249
4250 /**
4251 * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
4252 * @dev: drm device
4253 * @data: ioctl data copied from userspace (unvalidated)
4254 * @file: drm file
4255 *
4256 * Validates the stream open parameters given by userspace including flags
4257 * and an array of u64 key, value pair properties.
4258 *
4259 * Very little is assumed up front about the nature of the stream being
4260 * opened (for instance we don't assume it's for periodic OA unit metrics). An
4261 * i915-perf stream is expected to be a suitable interface for other forms of
4262 * buffered data written by the GPU besides periodic OA metrics.
4263 *
4264 * Note we copy the properties from userspace outside of the i915 perf
4265 * mutex to avoid an awkward lockdep with mmap_lock.
4266 *
4267 * Most of the implementation details are handled by
4268 * i915_perf_open_ioctl_locked() after taking the >->perf.lock
4269 * mutex for serializing with any non-file-operation driver hooks.
4270 *
4271 * Return: A newly opened i915 Perf stream file descriptor or negative
4272 * error code on failure.
4273 */
i915_perf_open_ioctl(struct drm_device * dev,void * data,struct drm_file * file)4274 int i915_perf_open_ioctl(struct drm_device *dev, void *data,
4275 struct drm_file *file)
4276 {
4277 struct i915_perf *perf = &to_i915(dev)->perf;
4278 struct drm_i915_perf_open_param *param = data;
4279 struct intel_gt *gt;
4280 struct perf_open_properties props;
4281 u32 known_open_flags;
4282 int ret;
4283
4284 if (!perf->i915)
4285 return -ENOTSUPP;
4286
4287 known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
4288 I915_PERF_FLAG_FD_NONBLOCK |
4289 I915_PERF_FLAG_DISABLED;
4290 if (param->flags & ~known_open_flags) {
4291 drm_dbg(&perf->i915->drm,
4292 "Unknown drm_i915_perf_open_param flag\n");
4293 return -EINVAL;
4294 }
4295
4296 ret = read_properties_unlocked(perf,
4297 u64_to_user_ptr(param->properties_ptr),
4298 param->num_properties,
4299 &props);
4300 if (ret)
4301 return ret;
4302
4303 gt = props.engine->gt;
4304
4305 mutex_lock(>->perf.lock);
4306 ret = i915_perf_open_ioctl_locked(perf, param, &props, file);
4307 mutex_unlock(>->perf.lock);
4308
4309 return ret;
4310 }
4311
4312 /**
4313 * i915_perf_register - exposes i915-perf to userspace
4314 * @i915: i915 device instance
4315 *
4316 * In particular OA metric sets are advertised under a sysfs metrics/
4317 * directory allowing userspace to enumerate valid IDs that can be
4318 * used to open an i915-perf stream.
4319 */
i915_perf_register(struct drm_i915_private * i915)4320 void i915_perf_register(struct drm_i915_private *i915)
4321 {
4322 struct i915_perf *perf = &i915->perf;
4323 struct intel_gt *gt = to_gt(i915);
4324
4325 if (!perf->i915)
4326 return;
4327
4328 /* To be sure we're synchronized with an attempted
4329 * i915_perf_open_ioctl(); considering that we register after
4330 * being exposed to userspace.
4331 */
4332 mutex_lock(>->perf.lock);
4333
4334 perf->metrics_kobj =
4335 kobject_create_and_add("metrics",
4336 &i915->drm.primary->kdev->kobj);
4337
4338 mutex_unlock(>->perf.lock);
4339 }
4340
4341 /**
4342 * i915_perf_unregister - hide i915-perf from userspace
4343 * @i915: i915 device instance
4344 *
4345 * i915-perf state cleanup is split up into an 'unregister' and
4346 * 'deinit' phase where the interface is first hidden from
4347 * userspace by i915_perf_unregister() before cleaning up
4348 * remaining state in i915_perf_fini().
4349 */
i915_perf_unregister(struct drm_i915_private * i915)4350 void i915_perf_unregister(struct drm_i915_private *i915)
4351 {
4352 struct i915_perf *perf = &i915->perf;
4353
4354 if (!perf->metrics_kobj)
4355 return;
4356
4357 kobject_put(perf->metrics_kobj);
4358 perf->metrics_kobj = NULL;
4359 }
4360
gen8_is_valid_flex_addr(struct i915_perf * perf,u32 addr)4361 static bool gen8_is_valid_flex_addr(struct i915_perf *perf, u32 addr)
4362 {
4363 static const i915_reg_t flex_eu_regs[] = {
4364 EU_PERF_CNTL0,
4365 EU_PERF_CNTL1,
4366 EU_PERF_CNTL2,
4367 EU_PERF_CNTL3,
4368 EU_PERF_CNTL4,
4369 EU_PERF_CNTL5,
4370 EU_PERF_CNTL6,
4371 };
4372 int i;
4373
4374 for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) {
4375 if (i915_mmio_reg_offset(flex_eu_regs[i]) == addr)
4376 return true;
4377 }
4378 return false;
4379 }
4380
reg_in_range_table(u32 addr,const struct i915_range * table)4381 static bool reg_in_range_table(u32 addr, const struct i915_range *table)
4382 {
4383 while (table->start || table->end) {
4384 if (addr >= table->start && addr <= table->end)
4385 return true;
4386
4387 table++;
4388 }
4389
4390 return false;
4391 }
4392
4393 #define REG_EQUAL(addr, mmio) \
4394 ((addr) == i915_mmio_reg_offset(mmio))
4395
4396 static const struct i915_range gen7_oa_b_counters[] = {
4397 { .start = 0x2710, .end = 0x272c }, /* OASTARTTRIG[1-8] */
4398 { .start = 0x2740, .end = 0x275c }, /* OAREPORTTRIG[1-8] */
4399 { .start = 0x2770, .end = 0x27ac }, /* OACEC[0-7][0-1] */
4400 {}
4401 };
4402
4403 static const struct i915_range gen12_oa_b_counters[] = {
4404 { .start = 0x2b2c, .end = 0x2b2c }, /* GEN12_OAG_OA_PESS */
4405 { .start = 0xd900, .end = 0xd91c }, /* GEN12_OAG_OASTARTTRIG[1-8] */
4406 { .start = 0xd920, .end = 0xd93c }, /* GEN12_OAG_OAREPORTTRIG1[1-8] */
4407 { .start = 0xd940, .end = 0xd97c }, /* GEN12_OAG_CEC[0-7][0-1] */
4408 { .start = 0xdc00, .end = 0xdc3c }, /* GEN12_OAG_SCEC[0-7][0-1] */
4409 { .start = 0xdc40, .end = 0xdc40 }, /* GEN12_OAG_SPCTR_CNF */
4410 { .start = 0xdc44, .end = 0xdc44 }, /* GEN12_OAA_DBG_REG */
4411 {}
4412 };
4413
4414 static const struct i915_range mtl_oam_b_counters[] = {
4415 { .start = 0x393000, .end = 0x39301c }, /* GEN12_OAM_STARTTRIG1[1-8] */
4416 { .start = 0x393020, .end = 0x39303c }, /* GEN12_OAM_REPORTTRIG1[1-8] */
4417 { .start = 0x393040, .end = 0x39307c }, /* GEN12_OAM_CEC[0-7][0-1] */
4418 { .start = 0x393200, .end = 0x39323C }, /* MPES[0-7] */
4419 {}
4420 };
4421
4422 static const struct i915_range xehp_oa_b_counters[] = {
4423 { .start = 0xdc48, .end = 0xdc48 }, /* OAA_ENABLE_REG */
4424 { .start = 0xdd00, .end = 0xdd48 }, /* OAG_LCE0_0 - OAA_LENABLE_REG */
4425 {}
4426 };
4427
4428 static const struct i915_range gen7_oa_mux_regs[] = {
4429 { .start = 0x91b8, .end = 0x91cc }, /* OA_PERFCNT[1-2], OA_PERFMATRIX */
4430 { .start = 0x9800, .end = 0x9888 }, /* MICRO_BP0_0 - NOA_WRITE */
4431 { .start = 0xe180, .end = 0xe180 }, /* HALF_SLICE_CHICKEN2 */
4432 {}
4433 };
4434
4435 static const struct i915_range hsw_oa_mux_regs[] = {
4436 { .start = 0x09e80, .end = 0x09ea4 }, /* HSW_MBVID2_NOA[0-9] */
4437 { .start = 0x09ec0, .end = 0x09ec0 }, /* HSW_MBVID2_MISR0 */
4438 { .start = 0x25100, .end = 0x2ff90 },
4439 {}
4440 };
4441
4442 static const struct i915_range chv_oa_mux_regs[] = {
4443 { .start = 0x182300, .end = 0x1823a4 },
4444 {}
4445 };
4446
4447 static const struct i915_range gen8_oa_mux_regs[] = {
4448 { .start = 0x0d00, .end = 0x0d2c }, /* RPM_CONFIG[0-1], NOA_CONFIG[0-8] */
4449 { .start = 0x20cc, .end = 0x20cc }, /* WAIT_FOR_RC6_EXIT */
4450 {}
4451 };
4452
4453 static const struct i915_range gen11_oa_mux_regs[] = {
4454 { .start = 0x91c8, .end = 0x91dc }, /* OA_PERFCNT[3-4] */
4455 {}
4456 };
4457
4458 static const struct i915_range gen12_oa_mux_regs[] = {
4459 { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */
4460 { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */
4461 { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */
4462 { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */
4463 { .start = 0x20cc, .end = 0x20cc }, /* WAIT_FOR_RC6_EXIT */
4464 {}
4465 };
4466
4467 /*
4468 * Ref: 14010536224:
4469 * 0x20cc is repurposed on MTL, so use a separate array for MTL.
4470 */
4471 static const struct i915_range mtl_oa_mux_regs[] = {
4472 { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */
4473 { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */
4474 { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */
4475 { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */
4476 { .start = 0x38d100, .end = 0x38d114}, /* VISACTL */
4477 {}
4478 };
4479
gen7_is_valid_b_counter_addr(struct i915_perf * perf,u32 addr)4480 static bool gen7_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
4481 {
4482 return reg_in_range_table(addr, gen7_oa_b_counters);
4483 }
4484
gen8_is_valid_mux_addr(struct i915_perf * perf,u32 addr)4485 static bool gen8_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
4486 {
4487 return reg_in_range_table(addr, gen7_oa_mux_regs) ||
4488 reg_in_range_table(addr, gen8_oa_mux_regs);
4489 }
4490
gen11_is_valid_mux_addr(struct i915_perf * perf,u32 addr)4491 static bool gen11_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
4492 {
4493 return reg_in_range_table(addr, gen7_oa_mux_regs) ||
4494 reg_in_range_table(addr, gen8_oa_mux_regs) ||
4495 reg_in_range_table(addr, gen11_oa_mux_regs);
4496 }
4497
hsw_is_valid_mux_addr(struct i915_perf * perf,u32 addr)4498 static bool hsw_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
4499 {
4500 return reg_in_range_table(addr, gen7_oa_mux_regs) ||
4501 reg_in_range_table(addr, hsw_oa_mux_regs);
4502 }
4503
chv_is_valid_mux_addr(struct i915_perf * perf,u32 addr)4504 static bool chv_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
4505 {
4506 return reg_in_range_table(addr, gen7_oa_mux_regs) ||
4507 reg_in_range_table(addr, chv_oa_mux_regs);
4508 }
4509
gen12_is_valid_b_counter_addr(struct i915_perf * perf,u32 addr)4510 static bool gen12_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
4511 {
4512 return reg_in_range_table(addr, gen12_oa_b_counters);
4513 }
4514
mtl_is_valid_oam_b_counter_addr(struct i915_perf * perf,u32 addr)4515 static bool mtl_is_valid_oam_b_counter_addr(struct i915_perf *perf, u32 addr)
4516 {
4517 if (HAS_OAM(perf->i915) &&
4518 GRAPHICS_VER_FULL(perf->i915) >= IP_VER(12, 70))
4519 return reg_in_range_table(addr, mtl_oam_b_counters);
4520
4521 return false;
4522 }
4523
xehp_is_valid_b_counter_addr(struct i915_perf * perf,u32 addr)4524 static bool xehp_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
4525 {
4526 return reg_in_range_table(addr, xehp_oa_b_counters) ||
4527 reg_in_range_table(addr, gen12_oa_b_counters) ||
4528 mtl_is_valid_oam_b_counter_addr(perf, addr);
4529 }
4530
gen12_is_valid_mux_addr(struct i915_perf * perf,u32 addr)4531 static bool gen12_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
4532 {
4533 if (GRAPHICS_VER_FULL(perf->i915) >= IP_VER(12, 70))
4534 return reg_in_range_table(addr, mtl_oa_mux_regs);
4535 else
4536 return reg_in_range_table(addr, gen12_oa_mux_regs);
4537 }
4538
mask_reg_value(u32 reg,u32 val)4539 static u32 mask_reg_value(u32 reg, u32 val)
4540 {
4541 /* HALF_SLICE_CHICKEN2 is programmed with a the
4542 * WaDisableSTUnitPowerOptimization workaround. Make sure the value
4543 * programmed by userspace doesn't change this.
4544 */
4545 if (REG_EQUAL(reg, HALF_SLICE_CHICKEN2))
4546 val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE);
4547
4548 /* WAIT_FOR_RC6_EXIT has only one bit fullfilling the function
4549 * indicated by its name and a bunch of selection fields used by OA
4550 * configs.
4551 */
4552 if (REG_EQUAL(reg, WAIT_FOR_RC6_EXIT))
4553 val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE);
4554
4555 return val;
4556 }
4557
alloc_oa_regs(struct i915_perf * perf,bool (* is_valid)(struct i915_perf * perf,u32 addr),u32 __user * regs,u32 n_regs)4558 static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,
4559 bool (*is_valid)(struct i915_perf *perf, u32 addr),
4560 u32 __user *regs,
4561 u32 n_regs)
4562 {
4563 struct i915_oa_reg *oa_regs;
4564 int err;
4565 u32 i;
4566
4567 if (!n_regs)
4568 return NULL;
4569
4570 /* No is_valid function means we're not allowing any register to be programmed. */
4571 GEM_BUG_ON(!is_valid);
4572 if (!is_valid)
4573 return ERR_PTR(-EINVAL);
4574
4575 oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL);
4576 if (!oa_regs)
4577 return ERR_PTR(-ENOMEM);
4578
4579 for (i = 0; i < n_regs; i++) {
4580 u32 addr, value;
4581
4582 err = get_user(addr, regs);
4583 if (err)
4584 goto addr_err;
4585
4586 if (!is_valid(perf, addr)) {
4587 drm_dbg(&perf->i915->drm,
4588 "Invalid oa_reg address: %X\n", addr);
4589 err = -EINVAL;
4590 goto addr_err;
4591 }
4592
4593 err = get_user(value, regs + 1);
4594 if (err)
4595 goto addr_err;
4596
4597 oa_regs[i].addr = _MMIO(addr);
4598 oa_regs[i].value = mask_reg_value(addr, value);
4599
4600 regs += 2;
4601 }
4602
4603 return oa_regs;
4604
4605 addr_err:
4606 kfree(oa_regs);
4607 return ERR_PTR(err);
4608 }
4609
show_dynamic_id(struct kobject * kobj,struct kobj_attribute * attr,char * buf)4610 static ssize_t show_dynamic_id(struct kobject *kobj,
4611 struct kobj_attribute *attr,
4612 char *buf)
4613 {
4614 struct i915_oa_config *oa_config =
4615 container_of(attr, typeof(*oa_config), sysfs_metric_id);
4616
4617 return sprintf(buf, "%d\n", oa_config->id);
4618 }
4619
create_dynamic_oa_sysfs_entry(struct i915_perf * perf,struct i915_oa_config * oa_config)4620 static int create_dynamic_oa_sysfs_entry(struct i915_perf *perf,
4621 struct i915_oa_config *oa_config)
4622 {
4623 sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
4624 oa_config->sysfs_metric_id.attr.name = "id";
4625 oa_config->sysfs_metric_id.attr.mode = S_IRUGO;
4626 oa_config->sysfs_metric_id.show = show_dynamic_id;
4627 oa_config->sysfs_metric_id.store = NULL;
4628
4629 oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr;
4630 oa_config->attrs[1] = NULL;
4631
4632 oa_config->sysfs_metric.name = oa_config->uuid;
4633 oa_config->sysfs_metric.attrs = oa_config->attrs;
4634
4635 return sysfs_create_group(perf->metrics_kobj,
4636 &oa_config->sysfs_metric);
4637 }
4638
4639 /**
4640 * i915_perf_add_config_ioctl - DRM ioctl() for userspace to add a new OA config
4641 * @dev: drm device
4642 * @data: ioctl data (pointer to struct drm_i915_perf_oa_config) copied from
4643 * userspace (unvalidated)
4644 * @file: drm file
4645 *
4646 * Validates the submitted OA register to be saved into a new OA config that
4647 * can then be used for programming the OA unit and its NOA network.
4648 *
4649 * Returns: A new allocated config number to be used with the perf open ioctl
4650 * or a negative error code on failure.
4651 */
i915_perf_add_config_ioctl(struct drm_device * dev,void * data,struct drm_file * file)4652 int i915_perf_add_config_ioctl(struct drm_device *dev, void *data,
4653 struct drm_file *file)
4654 {
4655 struct i915_perf *perf = &to_i915(dev)->perf;
4656 struct drm_i915_perf_oa_config *args = data;
4657 struct i915_oa_config *oa_config, *tmp;
4658 struct i915_oa_reg *regs;
4659 int err, id;
4660
4661 if (!perf->i915)
4662 return -ENOTSUPP;
4663
4664 if (!perf->metrics_kobj) {
4665 drm_dbg(&perf->i915->drm,
4666 "OA metrics weren't advertised via sysfs\n");
4667 return -EINVAL;
4668 }
4669
4670 if (i915_perf_stream_paranoid && !perfmon_capable()) {
4671 drm_dbg(&perf->i915->drm,
4672 "Insufficient privileges to add i915 OA config\n");
4673 return -EACCES;
4674 }
4675
4676 if ((!args->mux_regs_ptr || !args->n_mux_regs) &&
4677 (!args->boolean_regs_ptr || !args->n_boolean_regs) &&
4678 (!args->flex_regs_ptr || !args->n_flex_regs)) {
4679 drm_dbg(&perf->i915->drm,
4680 "No OA registers given\n");
4681 return -EINVAL;
4682 }
4683
4684 oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL);
4685 if (!oa_config) {
4686 drm_dbg(&perf->i915->drm,
4687 "Failed to allocate memory for the OA config\n");
4688 return -ENOMEM;
4689 }
4690
4691 oa_config->perf = perf;
4692 kref_init(&oa_config->ref);
4693
4694 if (!uuid_is_valid(args->uuid)) {
4695 drm_dbg(&perf->i915->drm,
4696 "Invalid uuid format for OA config\n");
4697 err = -EINVAL;
4698 goto reg_err;
4699 }
4700
4701 /* Last character in oa_config->uuid will be 0 because oa_config is
4702 * kzalloc.
4703 */
4704 memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid));
4705
4706 oa_config->mux_regs_len = args->n_mux_regs;
4707 regs = alloc_oa_regs(perf,
4708 perf->ops.is_valid_mux_reg,
4709 u64_to_user_ptr(args->mux_regs_ptr),
4710 args->n_mux_regs);
4711
4712 if (IS_ERR(regs)) {
4713 drm_dbg(&perf->i915->drm,
4714 "Failed to create OA config for mux_regs\n");
4715 err = PTR_ERR(regs);
4716 goto reg_err;
4717 }
4718 oa_config->mux_regs = regs;
4719
4720 oa_config->b_counter_regs_len = args->n_boolean_regs;
4721 regs = alloc_oa_regs(perf,
4722 perf->ops.is_valid_b_counter_reg,
4723 u64_to_user_ptr(args->boolean_regs_ptr),
4724 args->n_boolean_regs);
4725
4726 if (IS_ERR(regs)) {
4727 drm_dbg(&perf->i915->drm,
4728 "Failed to create OA config for b_counter_regs\n");
4729 err = PTR_ERR(regs);
4730 goto reg_err;
4731 }
4732 oa_config->b_counter_regs = regs;
4733
4734 if (GRAPHICS_VER(perf->i915) < 8) {
4735 if (args->n_flex_regs != 0) {
4736 err = -EINVAL;
4737 goto reg_err;
4738 }
4739 } else {
4740 oa_config->flex_regs_len = args->n_flex_regs;
4741 regs = alloc_oa_regs(perf,
4742 perf->ops.is_valid_flex_reg,
4743 u64_to_user_ptr(args->flex_regs_ptr),
4744 args->n_flex_regs);
4745
4746 if (IS_ERR(regs)) {
4747 drm_dbg(&perf->i915->drm,
4748 "Failed to create OA config for flex_regs\n");
4749 err = PTR_ERR(regs);
4750 goto reg_err;
4751 }
4752 oa_config->flex_regs = regs;
4753 }
4754
4755 err = mutex_lock_interruptible(&perf->metrics_lock);
4756 if (err)
4757 goto reg_err;
4758
4759 /* We shouldn't have too many configs, so this iteration shouldn't be
4760 * too costly.
4761 */
4762 idr_for_each_entry(&perf->metrics_idr, tmp, id) {
4763 if (!strcmp(tmp->uuid, oa_config->uuid)) {
4764 drm_dbg(&perf->i915->drm,
4765 "OA config already exists with this uuid\n");
4766 err = -EADDRINUSE;
4767 goto sysfs_err;
4768 }
4769 }
4770
4771 err = create_dynamic_oa_sysfs_entry(perf, oa_config);
4772 if (err) {
4773 drm_dbg(&perf->i915->drm,
4774 "Failed to create sysfs entry for OA config\n");
4775 goto sysfs_err;
4776 }
4777
4778 /* Config id 0 is invalid, id 1 for kernel stored test config. */
4779 oa_config->id = idr_alloc(&perf->metrics_idr,
4780 oa_config, 2,
4781 0, GFP_KERNEL);
4782 if (oa_config->id < 0) {
4783 drm_dbg(&perf->i915->drm,
4784 "Failed to create sysfs entry for OA config\n");
4785 err = oa_config->id;
4786 goto sysfs_err;
4787 }
4788 id = oa_config->id;
4789
4790 drm_dbg(&perf->i915->drm,
4791 "Added config %s id=%i\n", oa_config->uuid, oa_config->id);
4792 mutex_unlock(&perf->metrics_lock);
4793
4794 return id;
4795
4796 sysfs_err:
4797 mutex_unlock(&perf->metrics_lock);
4798 reg_err:
4799 i915_oa_config_put(oa_config);
4800 drm_dbg(&perf->i915->drm,
4801 "Failed to add new OA config\n");
4802 return err;
4803 }
4804
4805 /**
4806 * i915_perf_remove_config_ioctl - DRM ioctl() for userspace to remove an OA config
4807 * @dev: drm device
4808 * @data: ioctl data (pointer to u64 integer) copied from userspace
4809 * @file: drm file
4810 *
4811 * Configs can be removed while being used, the will stop appearing in sysfs
4812 * and their content will be freed when the stream using the config is closed.
4813 *
4814 * Returns: 0 on success or a negative error code on failure.
4815 */
i915_perf_remove_config_ioctl(struct drm_device * dev,void * data,struct drm_file * file)4816 int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data,
4817 struct drm_file *file)
4818 {
4819 struct i915_perf *perf = &to_i915(dev)->perf;
4820 u64 *arg = data;
4821 struct i915_oa_config *oa_config;
4822 int ret;
4823
4824 if (!perf->i915)
4825 return -ENOTSUPP;
4826
4827 if (i915_perf_stream_paranoid && !perfmon_capable()) {
4828 drm_dbg(&perf->i915->drm,
4829 "Insufficient privileges to remove i915 OA config\n");
4830 return -EACCES;
4831 }
4832
4833 ret = mutex_lock_interruptible(&perf->metrics_lock);
4834 if (ret)
4835 return ret;
4836
4837 oa_config = idr_find(&perf->metrics_idr, *arg);
4838 if (!oa_config) {
4839 drm_dbg(&perf->i915->drm,
4840 "Failed to remove unknown OA config\n");
4841 ret = -ENOENT;
4842 goto err_unlock;
4843 }
4844
4845 GEM_BUG_ON(*arg != oa_config->id);
4846
4847 sysfs_remove_group(perf->metrics_kobj, &oa_config->sysfs_metric);
4848
4849 idr_remove(&perf->metrics_idr, *arg);
4850
4851 mutex_unlock(&perf->metrics_lock);
4852
4853 drm_dbg(&perf->i915->drm,
4854 "Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
4855
4856 i915_oa_config_put(oa_config);
4857
4858 return 0;
4859
4860 err_unlock:
4861 mutex_unlock(&perf->metrics_lock);
4862 return ret;
4863 }
4864
4865 static struct ctl_table oa_table[] = {
4866 {
4867 .procname = "perf_stream_paranoid",
4868 .data = &i915_perf_stream_paranoid,
4869 .maxlen = sizeof(i915_perf_stream_paranoid),
4870 .mode = 0644,
4871 .proc_handler = proc_dointvec_minmax,
4872 .extra1 = SYSCTL_ZERO,
4873 .extra2 = SYSCTL_ONE,
4874 },
4875 {
4876 .procname = "oa_max_sample_rate",
4877 .data = &i915_oa_max_sample_rate,
4878 .maxlen = sizeof(i915_oa_max_sample_rate),
4879 .mode = 0644,
4880 .proc_handler = proc_dointvec_minmax,
4881 .extra1 = SYSCTL_ZERO,
4882 .extra2 = &oa_sample_rate_hard_limit,
4883 },
4884 {}
4885 };
4886
num_perf_groups_per_gt(struct intel_gt * gt)4887 static u32 num_perf_groups_per_gt(struct intel_gt *gt)
4888 {
4889 return 1;
4890 }
4891
__oam_engine_group(struct intel_engine_cs * engine)4892 static u32 __oam_engine_group(struct intel_engine_cs *engine)
4893 {
4894 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 70)) {
4895 /*
4896 * There's 1 SAMEDIA gt and 1 OAM per SAMEDIA gt. All media slices
4897 * within the gt use the same OAM. All MTL SKUs list 1 SA MEDIA.
4898 */
4899 drm_WARN_ON(&engine->i915->drm,
4900 engine->gt->type != GT_MEDIA);
4901
4902 return PERF_GROUP_OAM_SAMEDIA_0;
4903 }
4904
4905 return PERF_GROUP_INVALID;
4906 }
4907
__oa_engine_group(struct intel_engine_cs * engine)4908 static u32 __oa_engine_group(struct intel_engine_cs *engine)
4909 {
4910 switch (engine->class) {
4911 case RENDER_CLASS:
4912 return PERF_GROUP_OAG;
4913
4914 case VIDEO_DECODE_CLASS:
4915 case VIDEO_ENHANCEMENT_CLASS:
4916 return __oam_engine_group(engine);
4917
4918 default:
4919 return PERF_GROUP_INVALID;
4920 }
4921 }
4922
__oam_regs(u32 base)4923 static struct i915_perf_regs __oam_regs(u32 base)
4924 {
4925 return (struct i915_perf_regs) {
4926 base,
4927 GEN12_OAM_HEAD_POINTER(base),
4928 GEN12_OAM_TAIL_POINTER(base),
4929 GEN12_OAM_BUFFER(base),
4930 GEN12_OAM_CONTEXT_CONTROL(base),
4931 GEN12_OAM_CONTROL(base),
4932 GEN12_OAM_DEBUG(base),
4933 GEN12_OAM_STATUS(base),
4934 GEN12_OAM_CONTROL_COUNTER_FORMAT_SHIFT,
4935 };
4936 }
4937
__oag_regs(void)4938 static struct i915_perf_regs __oag_regs(void)
4939 {
4940 return (struct i915_perf_regs) {
4941 0,
4942 GEN12_OAG_OAHEADPTR,
4943 GEN12_OAG_OATAILPTR,
4944 GEN12_OAG_OABUFFER,
4945 GEN12_OAG_OAGLBCTXCTRL,
4946 GEN12_OAG_OACONTROL,
4947 GEN12_OAG_OA_DEBUG,
4948 GEN12_OAG_OASTATUS,
4949 GEN12_OAG_OACONTROL_OA_COUNTER_FORMAT_SHIFT,
4950 };
4951 }
4952
oa_init_groups(struct intel_gt * gt)4953 static void oa_init_groups(struct intel_gt *gt)
4954 {
4955 int i, num_groups = gt->perf.num_perf_groups;
4956
4957 for (i = 0; i < num_groups; i++) {
4958 struct i915_perf_group *g = >->perf.group[i];
4959
4960 /* Fused off engines can result in a group with num_engines == 0 */
4961 if (g->num_engines == 0)
4962 continue;
4963
4964 if (i == PERF_GROUP_OAG && gt->type != GT_MEDIA) {
4965 g->regs = __oag_regs();
4966 g->type = TYPE_OAG;
4967 } else if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70)) {
4968 g->regs = __oam_regs(mtl_oa_base[i]);
4969 g->type = TYPE_OAM;
4970 }
4971 }
4972 }
4973
oa_init_gt(struct intel_gt * gt)4974 static int oa_init_gt(struct intel_gt *gt)
4975 {
4976 u32 num_groups = num_perf_groups_per_gt(gt);
4977 struct intel_engine_cs *engine;
4978 struct i915_perf_group *g;
4979 intel_engine_mask_t tmp;
4980
4981 g = kcalloc(num_groups, sizeof(*g), GFP_KERNEL);
4982 if (!g)
4983 return -ENOMEM;
4984
4985 for_each_engine_masked(engine, gt, ALL_ENGINES, tmp) {
4986 u32 index = __oa_engine_group(engine);
4987
4988 engine->oa_group = NULL;
4989 if (index < num_groups) {
4990 g[index].num_engines++;
4991 engine->oa_group = &g[index];
4992 }
4993 }
4994
4995 gt->perf.num_perf_groups = num_groups;
4996 gt->perf.group = g;
4997
4998 oa_init_groups(gt);
4999
5000 return 0;
5001 }
5002
oa_init_engine_groups(struct i915_perf * perf)5003 static int oa_init_engine_groups(struct i915_perf *perf)
5004 {
5005 struct intel_gt *gt;
5006 int i, ret;
5007
5008 for_each_gt(gt, perf->i915, i) {
5009 ret = oa_init_gt(gt);
5010 if (ret)
5011 return ret;
5012 }
5013
5014 return 0;
5015 }
5016
oa_init_supported_formats(struct i915_perf * perf)5017 static void oa_init_supported_formats(struct i915_perf *perf)
5018 {
5019 struct drm_i915_private *i915 = perf->i915;
5020 enum intel_platform platform = INTEL_INFO(i915)->platform;
5021
5022 switch (platform) {
5023 case INTEL_HASWELL:
5024 oa_format_add(perf, I915_OA_FORMAT_A13);
5025 oa_format_add(perf, I915_OA_FORMAT_A13);
5026 oa_format_add(perf, I915_OA_FORMAT_A29);
5027 oa_format_add(perf, I915_OA_FORMAT_A13_B8_C8);
5028 oa_format_add(perf, I915_OA_FORMAT_B4_C8);
5029 oa_format_add(perf, I915_OA_FORMAT_A45_B8_C8);
5030 oa_format_add(perf, I915_OA_FORMAT_B4_C8_A16);
5031 oa_format_add(perf, I915_OA_FORMAT_C4_B8);
5032 break;
5033
5034 case INTEL_BROADWELL:
5035 case INTEL_CHERRYVIEW:
5036 case INTEL_SKYLAKE:
5037 case INTEL_BROXTON:
5038 case INTEL_KABYLAKE:
5039 case INTEL_GEMINILAKE:
5040 case INTEL_COFFEELAKE:
5041 case INTEL_COMETLAKE:
5042 case INTEL_ICELAKE:
5043 case INTEL_ELKHARTLAKE:
5044 case INTEL_JASPERLAKE:
5045 case INTEL_TIGERLAKE:
5046 case INTEL_ROCKETLAKE:
5047 case INTEL_DG1:
5048 case INTEL_ALDERLAKE_S:
5049 case INTEL_ALDERLAKE_P:
5050 oa_format_add(perf, I915_OA_FORMAT_A12);
5051 oa_format_add(perf, I915_OA_FORMAT_A12_B8_C8);
5052 oa_format_add(perf, I915_OA_FORMAT_A32u40_A4u32_B8_C8);
5053 oa_format_add(perf, I915_OA_FORMAT_C4_B8);
5054 break;
5055
5056 case INTEL_DG2:
5057 oa_format_add(perf, I915_OAR_FORMAT_A32u40_A4u32_B8_C8);
5058 oa_format_add(perf, I915_OA_FORMAT_A24u40_A14u32_B8_C8);
5059 break;
5060
5061 case INTEL_METEORLAKE:
5062 oa_format_add(perf, I915_OAR_FORMAT_A32u40_A4u32_B8_C8);
5063 oa_format_add(perf, I915_OA_FORMAT_A24u40_A14u32_B8_C8);
5064 oa_format_add(perf, I915_OAM_FORMAT_MPEC8u64_B8_C8);
5065 oa_format_add(perf, I915_OAM_FORMAT_MPEC8u32_B8_C8);
5066 break;
5067
5068 default:
5069 MISSING_CASE(platform);
5070 }
5071 }
5072
i915_perf_init_info(struct drm_i915_private * i915)5073 static void i915_perf_init_info(struct drm_i915_private *i915)
5074 {
5075 struct i915_perf *perf = &i915->perf;
5076
5077 switch (GRAPHICS_VER(i915)) {
5078 case 8:
5079 perf->ctx_oactxctrl_offset = 0x120;
5080 perf->ctx_flexeu0_offset = 0x2ce;
5081 perf->gen8_valid_ctx_bit = BIT(25);
5082 break;
5083 case 9:
5084 perf->ctx_oactxctrl_offset = 0x128;
5085 perf->ctx_flexeu0_offset = 0x3de;
5086 perf->gen8_valid_ctx_bit = BIT(16);
5087 break;
5088 case 11:
5089 perf->ctx_oactxctrl_offset = 0x124;
5090 perf->ctx_flexeu0_offset = 0x78e;
5091 perf->gen8_valid_ctx_bit = BIT(16);
5092 break;
5093 case 12:
5094 perf->gen8_valid_ctx_bit = BIT(16);
5095 /*
5096 * Calculate offset at runtime in oa_pin_context for gen12 and
5097 * cache the value in perf->ctx_oactxctrl_offset.
5098 */
5099 break;
5100 default:
5101 MISSING_CASE(GRAPHICS_VER(i915));
5102 }
5103 }
5104
5105 /**
5106 * i915_perf_init - initialize i915-perf state on module bind
5107 * @i915: i915 device instance
5108 *
5109 * Initializes i915-perf state without exposing anything to userspace.
5110 *
5111 * Note: i915-perf initialization is split into an 'init' and 'register'
5112 * phase with the i915_perf_register() exposing state to userspace.
5113 */
i915_perf_init(struct drm_i915_private * i915)5114 int i915_perf_init(struct drm_i915_private *i915)
5115 {
5116 struct i915_perf *perf = &i915->perf;
5117
5118 perf->oa_formats = oa_formats;
5119 if (IS_HASWELL(i915)) {
5120 perf->ops.is_valid_b_counter_reg = gen7_is_valid_b_counter_addr;
5121 perf->ops.is_valid_mux_reg = hsw_is_valid_mux_addr;
5122 perf->ops.is_valid_flex_reg = NULL;
5123 perf->ops.enable_metric_set = hsw_enable_metric_set;
5124 perf->ops.disable_metric_set = hsw_disable_metric_set;
5125 perf->ops.oa_enable = gen7_oa_enable;
5126 perf->ops.oa_disable = gen7_oa_disable;
5127 perf->ops.read = gen7_oa_read;
5128 perf->ops.oa_hw_tail_read = gen7_oa_hw_tail_read;
5129 } else if (HAS_LOGICAL_RING_CONTEXTS(i915)) {
5130 /* Note: that although we could theoretically also support the
5131 * legacy ringbuffer mode on BDW (and earlier iterations of
5132 * this driver, before upstreaming did this) it didn't seem
5133 * worth the complexity to maintain now that BDW+ enable
5134 * execlist mode by default.
5135 */
5136 perf->ops.read = gen8_oa_read;
5137 i915_perf_init_info(i915);
5138
5139 if (IS_GRAPHICS_VER(i915, 8, 9)) {
5140 perf->ops.is_valid_b_counter_reg =
5141 gen7_is_valid_b_counter_addr;
5142 perf->ops.is_valid_mux_reg =
5143 gen8_is_valid_mux_addr;
5144 perf->ops.is_valid_flex_reg =
5145 gen8_is_valid_flex_addr;
5146
5147 if (IS_CHERRYVIEW(i915)) {
5148 perf->ops.is_valid_mux_reg =
5149 chv_is_valid_mux_addr;
5150 }
5151
5152 perf->ops.oa_enable = gen8_oa_enable;
5153 perf->ops.oa_disable = gen8_oa_disable;
5154 perf->ops.enable_metric_set = gen8_enable_metric_set;
5155 perf->ops.disable_metric_set = gen8_disable_metric_set;
5156 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
5157 } else if (GRAPHICS_VER(i915) == 11) {
5158 perf->ops.is_valid_b_counter_reg =
5159 gen7_is_valid_b_counter_addr;
5160 perf->ops.is_valid_mux_reg =
5161 gen11_is_valid_mux_addr;
5162 perf->ops.is_valid_flex_reg =
5163 gen8_is_valid_flex_addr;
5164
5165 perf->ops.oa_enable = gen8_oa_enable;
5166 perf->ops.oa_disable = gen8_oa_disable;
5167 perf->ops.enable_metric_set = gen8_enable_metric_set;
5168 perf->ops.disable_metric_set = gen11_disable_metric_set;
5169 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
5170 } else if (GRAPHICS_VER(i915) == 12) {
5171 perf->ops.is_valid_b_counter_reg =
5172 HAS_OA_SLICE_CONTRIB_LIMITS(i915) ?
5173 xehp_is_valid_b_counter_addr :
5174 gen12_is_valid_b_counter_addr;
5175 perf->ops.is_valid_mux_reg =
5176 gen12_is_valid_mux_addr;
5177 perf->ops.is_valid_flex_reg =
5178 gen8_is_valid_flex_addr;
5179
5180 perf->ops.oa_enable = gen12_oa_enable;
5181 perf->ops.oa_disable = gen12_oa_disable;
5182 perf->ops.enable_metric_set = gen12_enable_metric_set;
5183 perf->ops.disable_metric_set = gen12_disable_metric_set;
5184 perf->ops.oa_hw_tail_read = gen12_oa_hw_tail_read;
5185 }
5186 }
5187
5188 if (perf->ops.enable_metric_set) {
5189 struct intel_gt *gt;
5190 int i, ret;
5191
5192 for_each_gt(gt, i915, i)
5193 mutex_init(>->perf.lock);
5194
5195 /* Choose a representative limit */
5196 oa_sample_rate_hard_limit = to_gt(i915)->clock_frequency / 2;
5197
5198 mutex_init(&perf->metrics_lock);
5199 idr_init_base(&perf->metrics_idr, 1);
5200
5201 /* We set up some ratelimit state to potentially throttle any
5202 * _NOTES about spurious, invalid OA reports which we don't
5203 * forward to userspace.
5204 *
5205 * We print a _NOTE about any throttling when closing the
5206 * stream instead of waiting until driver _fini which no one
5207 * would ever see.
5208 *
5209 * Using the same limiting factors as printk_ratelimit()
5210 */
5211 ratelimit_state_init(&perf->spurious_report_rs, 5 * HZ, 10);
5212 /* Since we use a DRM_NOTE for spurious reports it would be
5213 * inconsistent to let __ratelimit() automatically print a
5214 * warning for throttling.
5215 */
5216 ratelimit_set_flags(&perf->spurious_report_rs,
5217 RATELIMIT_MSG_ON_RELEASE);
5218
5219 ratelimit_state_init(&perf->tail_pointer_race,
5220 5 * HZ, 10);
5221 ratelimit_set_flags(&perf->tail_pointer_race,
5222 RATELIMIT_MSG_ON_RELEASE);
5223
5224 atomic64_set(&perf->noa_programming_delay,
5225 500 * 1000 /* 500us */);
5226
5227 perf->i915 = i915;
5228
5229 ret = oa_init_engine_groups(perf);
5230 if (ret) {
5231 drm_err(&i915->drm,
5232 "OA initialization failed %d\n", ret);
5233 return ret;
5234 }
5235
5236 oa_init_supported_formats(perf);
5237 }
5238
5239 return 0;
5240 }
5241
destroy_config(int id,void * p,void * data)5242 static int destroy_config(int id, void *p, void *data)
5243 {
5244 i915_oa_config_put(p);
5245 return 0;
5246 }
5247
i915_perf_sysctl_register(void)5248 int i915_perf_sysctl_register(void)
5249 {
5250 sysctl_header = register_sysctl("dev/i915", oa_table);
5251 return 0;
5252 }
5253
i915_perf_sysctl_unregister(void)5254 void i915_perf_sysctl_unregister(void)
5255 {
5256 unregister_sysctl_table(sysctl_header);
5257 }
5258
5259 /**
5260 * i915_perf_fini - Counter part to i915_perf_init()
5261 * @i915: i915 device instance
5262 */
i915_perf_fini(struct drm_i915_private * i915)5263 void i915_perf_fini(struct drm_i915_private *i915)
5264 {
5265 struct i915_perf *perf = &i915->perf;
5266 struct intel_gt *gt;
5267 int i;
5268
5269 if (!perf->i915)
5270 return;
5271
5272 for_each_gt(gt, perf->i915, i)
5273 kfree(gt->perf.group);
5274
5275 idr_for_each(&perf->metrics_idr, destroy_config, perf);
5276 idr_destroy(&perf->metrics_idr);
5277
5278 memset(&perf->ops, 0, sizeof(perf->ops));
5279 perf->i915 = NULL;
5280 }
5281
5282 /**
5283 * i915_perf_ioctl_version - Version of the i915-perf subsystem
5284 * @i915: The i915 device
5285 *
5286 * This version number is used by userspace to detect available features.
5287 */
i915_perf_ioctl_version(struct drm_i915_private * i915)5288 int i915_perf_ioctl_version(struct drm_i915_private *i915)
5289 {
5290 /*
5291 * 1: Initial version
5292 * I915_PERF_IOCTL_ENABLE
5293 * I915_PERF_IOCTL_DISABLE
5294 *
5295 * 2: Added runtime modification of OA config.
5296 * I915_PERF_IOCTL_CONFIG
5297 *
5298 * 3: Add DRM_I915_PERF_PROP_HOLD_PREEMPTION parameter to hold
5299 * preemption on a particular context so that performance data is
5300 * accessible from a delta of MI_RPC reports without looking at the
5301 * OA buffer.
5302 *
5303 * 4: Add DRM_I915_PERF_PROP_ALLOWED_SSEU to limit what contexts can
5304 * be run for the duration of the performance recording based on
5305 * their SSEU configuration.
5306 *
5307 * 5: Add DRM_I915_PERF_PROP_POLL_OA_PERIOD parameter that controls the
5308 * interval for the hrtimer used to check for OA data.
5309 *
5310 * 6: Add DRM_I915_PERF_PROP_OA_ENGINE_CLASS and
5311 * DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE
5312 *
5313 * 7: Add support for video decode and enhancement classes.
5314 */
5315
5316 /*
5317 * Wa_14017512683: mtl[a0..c0): Use of OAM must be preceded with Media
5318 * C6 disable in BIOS. If Media C6 is enabled in BIOS, return version 6
5319 * to indicate that OA media is not supported.
5320 */
5321 if (IS_MTL_MEDIA_STEP(i915, STEP_A0, STEP_C0)) {
5322 struct intel_gt *gt;
5323 int i;
5324
5325 for_each_gt(gt, i915, i) {
5326 if (gt->type == GT_MEDIA &&
5327 intel_check_bios_c6_setup(>->rc6))
5328 return 6;
5329 }
5330 }
5331
5332 return 7;
5333 }
5334
5335 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5336 #include "selftests/i915_perf.c"
5337 #endif
5338