blob: b5030192be3ef6b9db29708530221e33d9874ce4 [file] [log] [blame]
Robert Braggeec688e2016-11-07 19:49:47 +00001/*
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
Robert Bragg7abbd8d2016-11-07 19:49:57 +000027
28/**
Robert Bragg16d98b32016-12-07 21:40:33 +000029 * DOC: i915 Perf Overview
Robert Bragg7abbd8d2016-11-07 19:49:57 +000030 *
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 *
Robert Bragg16d98b32016-12-07 21:40:33 +000048 */
49
50/**
51 * DOC: i915 Perf History and Comparison with Core Perf
Robert Bragg7abbd8d2016-11-07 19:49:57 +000052 *
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 *
Robert Bragg16d98b32016-12-07 21:40:33 +000082 * Issues hit with first prototype based on Core Perf
83 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Robert Bragg7abbd8d2016-11-07 19:49:57 +000084 *
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 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000142 * - As a side note on perf's grouping feature; there was also some concern
Robert Bragg7abbd8d2016-11-07 19:49:57 +0000143 * 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
Robert Braggeec688e2016-11-07 19:49:47 +0000194#include <linux/anon_inodes.h>
Robert Braggd7965152016-11-07 19:49:52 +0000195#include <linux/sizes.h>
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100196#include <linux/uuid.h>
Robert Braggeec688e2016-11-07 19:49:47 +0000197
Chris Wilson10be98a2019-05-28 10:29:49 +0100198#include "gem/i915_gem_context.h"
Chris Wilsona5efcde2019-10-11 20:03:17 +0100199#include "gt/intel_engine_pm.h"
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100200#include "gt/intel_engine_user.h"
Lionel Landwerlindaed3e42019-10-12 08:23:07 +0100201#include "gt/intel_gt.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +0100202#include "gt/intel_lrc_reg.h"
Chris Wilson2871ea82019-10-24 11:03:44 +0100203#include "gt/intel_ring.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +0100204
Robert Braggeec688e2016-11-07 19:49:47 +0000205#include "i915_drv.h"
Jani Nikuladb94e9f2019-08-08 16:42:44 +0300206#include "i915_perf.h"
Michal Wajdeczko5ed7a0c2019-06-26 12:38:26 +0000207#include "oa/i915_oa_hsw.h"
208#include "oa/i915_oa_bdw.h"
209#include "oa/i915_oa_chv.h"
210#include "oa/i915_oa_sklgt2.h"
211#include "oa/i915_oa_sklgt3.h"
212#include "oa/i915_oa_sklgt4.h"
213#include "oa/i915_oa_bxt.h"
214#include "oa/i915_oa_kblgt2.h"
215#include "oa/i915_oa_kblgt3.h"
216#include "oa/i915_oa_glk.h"
217#include "oa/i915_oa_cflgt2.h"
218#include "oa/i915_oa_cflgt3.h"
219#include "oa/i915_oa_cnl.h"
220#include "oa/i915_oa_icl.h"
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700221#include "oa/i915_oa_tgl.h"
Robert Braggd7965152016-11-07 19:49:52 +0000222
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200223/* HW requires this to be a power of two, between 128k and 16M, though driver
224 * is currently generally designed assuming the largest 16M size is used such
225 * that the overflow cases are unlikely in normal operation.
226 */
227#define OA_BUFFER_SIZE SZ_16M
228
229#define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
Robert Braggd7965152016-11-07 19:49:52 +0000230
Robert Bragg0dd860c2017-05-11 16:43:28 +0100231/**
232 * DOC: OA Tail Pointer Race
233 *
234 * There's a HW race condition between OA unit tail pointer register updates and
Robert Braggd7965152016-11-07 19:49:52 +0000235 * writes to memory whereby the tail pointer can sometimes get ahead of what's
Robert Bragg0dd860c2017-05-11 16:43:28 +0100236 * been written out to the OA buffer so far (in terms of what's visible to the
237 * CPU).
Robert Braggd7965152016-11-07 19:49:52 +0000238 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100239 * Although this can be observed explicitly while copying reports to userspace
240 * by checking for a zeroed report-id field in tail reports, we want to account
Robert Bragg19f81df2017-06-13 12:23:03 +0100241 * for this earlier, as part of the oa_buffer_check to avoid lots of redundant
Robert Bragg0dd860c2017-05-11 16:43:28 +0100242 * read() attempts.
Robert Braggd7965152016-11-07 19:49:52 +0000243 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100244 * In effect we define a tail pointer for reading that lags the real tail
245 * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough
246 * time for the corresponding reports to become visible to the CPU.
Robert Braggd7965152016-11-07 19:49:52 +0000247 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100248 * To manage this we actually track two tail pointers:
249 * 1) An 'aging' tail with an associated timestamp that is tracked until we
250 * can trust the corresponding data is visible to the CPU; at which point
251 * it is considered 'aged'.
252 * 2) An 'aged' tail that can be used for read()ing.
253 *
254 * The two separate pointers let us decouple read()s from tail pointer aging.
255 *
256 * The tail pointers are checked and updated at a limited rate within a hrtimer
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800257 * callback (the same callback that is used for delivering EPOLLIN events)
Robert Bragg0dd860c2017-05-11 16:43:28 +0100258 *
259 * Initially the tails are marked invalid with %INVALID_TAIL_PTR which
260 * indicates that an updated tail pointer is needed.
261 *
262 * Most of the implementation details for this workaround are in
Robert Bragg19f81df2017-06-13 12:23:03 +0100263 * oa_buffer_check_unlocked() and _append_oa_reports()
Robert Bragg0dd860c2017-05-11 16:43:28 +0100264 *
265 * Note for posterity: previously the driver used to define an effective tail
266 * pointer that lagged the real pointer by a 'tail margin' measured in bytes
267 * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
268 * This was flawed considering that the OA unit may also automatically generate
269 * non-periodic reports (such as on context switch) or the OA unit may be
270 * enabled without any periodic sampling.
Robert Braggd7965152016-11-07 19:49:52 +0000271 */
272#define OA_TAIL_MARGIN_NSEC 100000ULL
Robert Bragg0dd860c2017-05-11 16:43:28 +0100273#define INVALID_TAIL_PTR 0xffffffff
Robert Braggd7965152016-11-07 19:49:52 +0000274
275/* frequency for checking whether the OA unit has written new reports to the
276 * circular OA buffer...
277 */
278#define POLL_FREQUENCY 200
279#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
280
Robert Braggccdf6342016-11-07 19:49:54 +0000281/* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
Robert Braggccdf6342016-11-07 19:49:54 +0000282static u32 i915_perf_stream_paranoid = true;
283
Robert Braggd7965152016-11-07 19:49:52 +0000284/* The maximum exponent the hardware accepts is 63 (essentially it selects one
285 * of the 64bit timestamp bits to trigger reports from) but there's currently
286 * no known use case for sampling as infrequently as once per 47 thousand years.
287 *
288 * Since the timestamps included in OA reports are only 32bits it seems
289 * reasonable to limit the OA exponent where it's still possible to account for
290 * overflow in OA report timestamps.
291 */
292#define OA_EXPONENT_MAX 31
293
294#define INVALID_CTX_ID 0xffffffff
295
Robert Bragg19f81df2017-06-13 12:23:03 +0100296/* On Gen8+ automatically triggered OA reports include a 'reason' field... */
297#define OAREPORT_REASON_MASK 0x3f
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700298#define OAREPORT_REASON_MASK_EXTENDED 0x7f
Robert Bragg19f81df2017-06-13 12:23:03 +0100299#define OAREPORT_REASON_SHIFT 19
300#define OAREPORT_REASON_TIMER (1<<0)
301#define OAREPORT_REASON_CTX_SWITCH (1<<3)
302#define OAREPORT_REASON_CLK_RATIO (1<<5)
303
Robert Braggd7965152016-11-07 19:49:52 +0000304
Robert Bragg00319ba2016-11-07 19:49:55 +0000305/* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
306 *
Robert Bragg155e9412017-06-13 12:23:05 +0100307 * The highest sampling frequency we can theoretically program the OA unit
308 * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
309 *
310 * Initialized just before we register the sysctl parameter.
Robert Bragg00319ba2016-11-07 19:49:55 +0000311 */
Robert Bragg155e9412017-06-13 12:23:05 +0100312static int oa_sample_rate_hard_limit;
Robert Bragg00319ba2016-11-07 19:49:55 +0000313
314/* Theoretically we can program the OA unit to sample every 160ns but don't
315 * allow that by default unless root...
316 *
317 * The default threshold of 100000Hz is based on perf's similar
318 * kernel.perf_event_max_sample_rate sysctl parameter.
319 */
320static u32 i915_oa_max_sample_rate = 100000;
321
Robert Braggd7965152016-11-07 19:49:52 +0000322/* XXX: beware if future OA HW adds new report formats that the current
323 * code assumes all reports have a power-of-two size and ~(size - 1) can
324 * be used as a mask to align the OA tail pointer.
325 */
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300326static const struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Braggd7965152016-11-07 19:49:52 +0000327 [I915_OA_FORMAT_A13] = { 0, 64 },
328 [I915_OA_FORMAT_A29] = { 1, 128 },
329 [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
330 /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
331 [I915_OA_FORMAT_B4_C8] = { 4, 64 },
332 [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
333 [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
334 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
335};
336
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300337static const struct i915_oa_format gen8_plus_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Bragg19f81df2017-06-13 12:23:03 +0100338 [I915_OA_FORMAT_A12] = { 0, 64 },
339 [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
340 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
341 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
342};
343
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700344static const struct i915_oa_format gen12_oa_formats[I915_OA_FORMAT_MAX] = {
345 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
346};
347
Robert Braggd7965152016-11-07 19:49:52 +0000348#define SAMPLE_OA_REPORT (1<<0)
Robert Braggeec688e2016-11-07 19:49:47 +0000349
Robert Bragg16d98b32016-12-07 21:40:33 +0000350/**
351 * struct perf_open_properties - for validated properties given to open a stream
352 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
353 * @single_context: Whether a single or all gpu contexts should be monitored
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +0100354 * @hold_preemption: Whether the preemption is disabled for the filtered
355 * context
Robert Bragg16d98b32016-12-07 21:40:33 +0000356 * @ctx_handle: A gem ctx handle for use with @single_context
357 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
358 * @oa_format: An OA unit HW report format
359 * @oa_periodic: Whether to enable periodic OA unit sampling
360 * @oa_period_exponent: The OA unit sampling period is derived from this
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100361 * @engine: The engine (typically rcs0) being monitored by the OA unit
Robert Bragg16d98b32016-12-07 21:40:33 +0000362 *
363 * As read_properties_unlocked() enumerates and validates the properties given
364 * to open a stream of metrics the configuration is built up in the structure
365 * which starts out zero initialized.
366 */
Robert Braggeec688e2016-11-07 19:49:47 +0000367struct perf_open_properties {
368 u32 sample_flags;
369
370 u64 single_context:1;
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +0100371 u64 hold_preemption:1;
Robert Braggeec688e2016-11-07 19:49:47 +0000372 u64 ctx_handle;
Robert Braggd7965152016-11-07 19:49:52 +0000373
374 /* OA sampling state */
375 int metrics_set;
376 int oa_format;
377 bool oa_periodic;
378 int oa_period_exponent;
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100379
380 struct intel_engine_cs *engine;
Robert Braggeec688e2016-11-07 19:49:47 +0000381};
382
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100383struct i915_oa_config_bo {
384 struct llist_node node;
385
386 struct i915_oa_config *oa_config;
387 struct i915_vma *vma;
388};
389
Venkata Sandeep Dhanalakota3dc716fd2019-12-13 07:51:51 -0800390static struct ctl_table_header *sysctl_header;
391
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700392static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer);
393
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100394void i915_oa_config_release(struct kref *ref)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100395{
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100396 struct i915_oa_config *oa_config =
397 container_of(ref, typeof(*oa_config), ref);
398
Chris Wilsonc2fba932019-10-13 10:52:11 +0100399 kfree(oa_config->flex_regs);
400 kfree(oa_config->b_counter_regs);
401 kfree(oa_config->mux_regs);
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100402
403 kfree_rcu(oa_config, rcu);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100404}
405
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100406struct i915_oa_config *
407i915_perf_get_oa_config(struct i915_perf *perf, int metrics_set)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100408{
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100409 struct i915_oa_config *oa_config;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100410
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100411 rcu_read_lock();
412 if (metrics_set == 1)
413 oa_config = &perf->test_config;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100414 else
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100415 oa_config = idr_find(&perf->metrics_idr, metrics_set);
416 if (oa_config)
417 oa_config = i915_oa_config_get(oa_config);
418 rcu_read_unlock();
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100419
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100420 return oa_config;
421}
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100422
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100423static void free_oa_config_bo(struct i915_oa_config_bo *oa_bo)
424{
425 i915_oa_config_put(oa_bo->oa_config);
426 i915_vma_put(oa_bo->vma);
427 kfree(oa_bo);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100428}
429
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700430static u32 gen12_oa_hw_tail_read(struct i915_perf_stream *stream)
431{
432 struct intel_uncore *uncore = stream->uncore;
433
434 return intel_uncore_read(uncore, GEN12_OAG_OATAILPTR) &
435 GEN12_OAG_OATAILPTR_MASK;
436}
437
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700438static u32 gen8_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100439{
Chris Wilson52111c42019-10-10 16:05:20 +0100440 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700441
Chris Wilson8f8b1172019-10-07 22:09:41 +0100442 return intel_uncore_read(uncore, GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
Robert Bragg19f81df2017-06-13 12:23:03 +0100443}
444
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700445static u32 gen7_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100446{
Chris Wilson52111c42019-10-10 16:05:20 +0100447 struct intel_uncore *uncore = stream->uncore;
Chris Wilson8f8b1172019-10-07 22:09:41 +0100448 u32 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100449
450 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
451}
452
Robert Bragg0dd860c2017-05-11 16:43:28 +0100453/**
Robert Bragg19f81df2017-06-13 12:23:03 +0100454 * oa_buffer_check_unlocked - check for data and update tail ptr state
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700455 * @stream: i915 stream instance
Robert Braggd7965152016-11-07 19:49:52 +0000456 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100457 * This is either called via fops (for blocking reads in user ctx) or the poll
458 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
459 * if there is data available for userspace to read.
Robert Braggd7965152016-11-07 19:49:52 +0000460 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100461 * This function is central to providing a workaround for the OA unit tail
462 * pointer having a race with respect to what data is visible to the CPU.
463 * It is responsible for reading tail pointers from the hardware and giving
464 * the pointers time to 'age' before they are made available for reading.
465 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
466 *
467 * Besides returning true when there is data available to read() this function
468 * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp
469 * and .aged_tail_idx state used for reading.
470 *
471 * Note: It's safe to read OA config state here unlocked, assuming that this is
472 * only called while the stream is enabled, while the global OA configuration
473 * can't be modified.
474 *
475 * Returns: %true if the OA buffer contains data, else %false
Robert Braggd7965152016-11-07 19:49:52 +0000476 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700477static bool oa_buffer_check_unlocked(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +0000478{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700479 int report_size = stream->oa_buffer.format_size;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100480 unsigned long flags;
481 unsigned int aged_idx;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100482 u32 head, hw_tail, aged_tail, aging_tail;
483 u64 now;
Robert Braggd7965152016-11-07 19:49:52 +0000484
Robert Bragg0dd860c2017-05-11 16:43:28 +0100485 /* We have to consider the (unlikely) possibility that read() errors
486 * could result in an OA buffer reset which might reset the head,
487 * tails[] and aged_tail state.
488 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700489 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100490
491 /* NB: The head we observe here might effectively be a little out of
492 * date (between head and tails[aged_idx].offset if there is currently
493 * a read() in progress.
494 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700495 head = stream->oa_buffer.head;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100496
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700497 aged_idx = stream->oa_buffer.aged_tail_idx;
498 aged_tail = stream->oa_buffer.tails[aged_idx].offset;
499 aging_tail = stream->oa_buffer.tails[!aged_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100500
Chris Wilson8f8b1172019-10-07 22:09:41 +0100501 hw_tail = stream->perf->ops.oa_hw_tail_read(stream);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100502
503 /* The tail pointer increases in 64 byte increments,
504 * not in report_size steps...
505 */
506 hw_tail &= ~(report_size - 1);
507
508 now = ktime_get_mono_fast_ns();
509
Robert Bragg4117ebc2017-05-11 16:43:30 +0100510 /* Update the aged tail
511 *
512 * Flip the tail pointer available for read()s once the aging tail is
513 * old enough to trust that the corresponding data will be visible to
514 * the CPU...
515 *
516 * Do this before updating the aging pointer in case we may be able to
517 * immediately start aging a new pointer too (if new data has become
518 * available) without needing to wait for a later hrtimer callback.
519 */
520 if (aging_tail != INVALID_TAIL_PTR &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700521 ((now - stream->oa_buffer.aging_timestamp) >
Robert Bragg4117ebc2017-05-11 16:43:30 +0100522 OA_TAIL_MARGIN_NSEC)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100523
Robert Bragg4117ebc2017-05-11 16:43:30 +0100524 aged_idx ^= 1;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700525 stream->oa_buffer.aged_tail_idx = aged_idx;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100526
527 aged_tail = aging_tail;
528
529 /* Mark that we need a new pointer to start aging... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700530 stream->oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100531 aging_tail = INVALID_TAIL_PTR;
532 }
533
Robert Bragg0dd860c2017-05-11 16:43:28 +0100534 /* Update the aging tail
535 *
536 * We throttle aging tail updates until we have a new tail that
537 * represents >= one report more data than is already available for
538 * reading. This ensures there will be enough data for a successful
539 * read once this new pointer has aged and ensures we will give the new
540 * pointer time to age.
541 */
542 if (aging_tail == INVALID_TAIL_PTR &&
543 (aged_tail == INVALID_TAIL_PTR ||
544 OA_TAKEN(hw_tail, aged_tail) >= report_size)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700545 struct i915_vma *vma = stream->oa_buffer.vma;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100546 u32 gtt_offset = i915_ggtt_offset(vma);
547
548 /* Be paranoid and do a bounds check on the pointer read back
549 * from hardware, just in case some spurious hardware condition
550 * could put the tail out of bounds...
551 */
552 if (hw_tail >= gtt_offset &&
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200553 hw_tail < (gtt_offset + OA_BUFFER_SIZE)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700554 stream->oa_buffer.tails[!aged_idx].offset =
Robert Bragg0dd860c2017-05-11 16:43:28 +0100555 aging_tail = hw_tail;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700556 stream->oa_buffer.aging_timestamp = now;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100557 } else {
Wambui Karuga0bf85732020-02-18 20:39:36 +0300558 drm_err(&stream->perf->i915->drm,
559 "Ignoring spurious out of range OA buffer tail pointer = %x\n",
560 hw_tail);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100561 }
562 }
563
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700564 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100565
566 return aged_tail == INVALID_TAIL_PTR ?
567 false : OA_TAKEN(aged_tail, head) >= report_size;
Robert Braggd7965152016-11-07 19:49:52 +0000568}
569
570/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000571 * append_oa_status - Appends a status record to a userspace read() buffer.
572 * @stream: An i915-perf stream opened for OA metrics
573 * @buf: destination buffer given by userspace
574 * @count: the number of bytes userspace wants to read
575 * @offset: (inout): the current position for writing into @buf
576 * @type: The kind of status to report to userspace
577 *
578 * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
579 * into the userspace read() buffer.
580 *
581 * The @buf @offset will only be updated on success.
582 *
583 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000584 */
585static int append_oa_status(struct i915_perf_stream *stream,
586 char __user *buf,
587 size_t count,
588 size_t *offset,
589 enum drm_i915_perf_record_type type)
590{
591 struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
592
593 if ((count - *offset) < header.size)
594 return -ENOSPC;
595
596 if (copy_to_user(buf + *offset, &header, sizeof(header)))
597 return -EFAULT;
598
599 (*offset) += header.size;
600
601 return 0;
602}
603
604/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000605 * append_oa_sample - Copies single OA report into userspace read() buffer.
606 * @stream: An i915-perf stream opened for OA metrics
607 * @buf: destination buffer given by userspace
608 * @count: the number of bytes userspace wants to read
609 * @offset: (inout): the current position for writing into @buf
610 * @report: A single OA report to (optionally) include as part of the sample
611 *
612 * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
613 * properties when opening a stream, tracked as `stream->sample_flags`. This
614 * function copies the requested components of a single sample to the given
615 * read() @buf.
616 *
617 * The @buf @offset will only be updated on success.
618 *
619 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000620 */
621static int append_oa_sample(struct i915_perf_stream *stream,
622 char __user *buf,
623 size_t count,
624 size_t *offset,
625 const u8 *report)
626{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700627 int report_size = stream->oa_buffer.format_size;
Robert Braggd7965152016-11-07 19:49:52 +0000628 struct drm_i915_perf_record_header header;
629 u32 sample_flags = stream->sample_flags;
630
631 header.type = DRM_I915_PERF_RECORD_SAMPLE;
632 header.pad = 0;
633 header.size = stream->sample_size;
634
635 if ((count - *offset) < header.size)
636 return -ENOSPC;
637
638 buf += *offset;
639 if (copy_to_user(buf, &header, sizeof(header)))
640 return -EFAULT;
641 buf += sizeof(header);
642
643 if (sample_flags & SAMPLE_OA_REPORT) {
644 if (copy_to_user(buf, report, report_size))
645 return -EFAULT;
646 }
647
648 (*offset) += header.size;
649
650 return 0;
651}
652
653/**
654 * Copies all buffered OA reports into userspace read() buffer.
655 * @stream: An i915-perf stream opened for OA metrics
656 * @buf: destination buffer given by userspace
657 * @count: the number of bytes userspace wants to read
658 * @offset: (inout): the current position for writing into @buf
Robert Braggd7965152016-11-07 19:49:52 +0000659 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000660 * Notably any error condition resulting in a short read (-%ENOSPC or
661 * -%EFAULT) will be returned even though one or more records may
Robert Braggd7965152016-11-07 19:49:52 +0000662 * have been successfully copied. In this case it's up to the caller
663 * to decide if the error should be squashed before returning to
664 * userspace.
665 *
666 * Note: reports are consumed from the head, and appended to the
Robert Bragge81b3a52017-05-11 16:43:24 +0100667 * tail, so the tail chases the head?... If you think that's mad
Robert Braggd7965152016-11-07 19:49:52 +0000668 * and back-to-front you're not alone, but this follows the
669 * Gen PRM naming convention.
Robert Bragg16d98b32016-12-07 21:40:33 +0000670 *
671 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000672 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100673static int gen8_append_oa_reports(struct i915_perf_stream *stream,
674 char __user *buf,
675 size_t count,
676 size_t *offset)
677{
Chris Wilson52111c42019-10-10 16:05:20 +0100678 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700679 int report_size = stream->oa_buffer.format_size;
680 u8 *oa_buf_base = stream->oa_buffer.vaddr;
681 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200682 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100683 size_t start_offset = *offset;
684 unsigned long flags;
685 unsigned int aged_tail_idx;
686 u32 head, tail;
687 u32 taken;
688 int ret = 0;
689
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +0530690 if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
Robert Bragg19f81df2017-06-13 12:23:03 +0100691 return -EIO;
692
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700693 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100694
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700695 head = stream->oa_buffer.head;
696 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
697 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg19f81df2017-06-13 12:23:03 +0100698
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700699 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100700
701 /*
702 * An invalid tail pointer here means we're still waiting for the poll
703 * hrtimer callback to give us a pointer
704 */
705 if (tail == INVALID_TAIL_PTR)
706 return -EAGAIN;
707
708 /*
709 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
710 * while indexing relative to oa_buf_base.
711 */
712 head -= gtt_offset;
713 tail -= gtt_offset;
714
715 /*
716 * An out of bounds or misaligned head or tail pointer implies a driver
717 * bug since we validate + align the tail pointers we read from the
718 * hardware and we are in full control of the head pointer which should
719 * only be incremented by multiples of the report size (notably also
720 * all a power of two).
721 */
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +0530722 if (drm_WARN_ONCE(&uncore->i915->drm,
723 head > OA_BUFFER_SIZE || head % report_size ||
724 tail > OA_BUFFER_SIZE || tail % report_size,
725 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
726 head, tail))
Robert Bragg19f81df2017-06-13 12:23:03 +0100727 return -EIO;
728
729
730 for (/* none */;
731 (taken = OA_TAKEN(tail, head));
732 head = (head + report_size) & mask) {
733 u8 *report = oa_buf_base + head;
734 u32 *report32 = (void *)report;
735 u32 ctx_id;
736 u32 reason;
737
738 /*
739 * All the report sizes factor neatly into the buffer
740 * size so we never expect to see a report split
741 * between the beginning and end of the buffer.
742 *
743 * Given the initial alignment check a misalignment
744 * here would imply a driver bug that would result
745 * in an overrun.
746 */
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +0530747 if (drm_WARN_ON(&uncore->i915->drm,
748 (OA_BUFFER_SIZE - head) < report_size)) {
Wambui Karuga0bf85732020-02-18 20:39:36 +0300749 drm_err(&uncore->i915->drm,
750 "Spurious OA head ptr: non-integral report offset\n");
Robert Bragg19f81df2017-06-13 12:23:03 +0100751 break;
752 }
753
754 /*
755 * The reason field includes flags identifying what
756 * triggered this specific report (mostly timer
757 * triggered or e.g. due to a context switch).
758 *
759 * This field is never expected to be zero so we can
760 * check that the report isn't invalid before copying
761 * it to userspace...
762 */
763 reason = ((report32[0] >> OAREPORT_REASON_SHIFT) &
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700764 (IS_GEN(stream->perf->i915, 12) ?
765 OAREPORT_REASON_MASK_EXTENDED :
766 OAREPORT_REASON_MASK));
Robert Bragg19f81df2017-06-13 12:23:03 +0100767 if (reason == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +0100768 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg19f81df2017-06-13 12:23:03 +0100769 DRM_NOTE("Skipping spurious, invalid OA report\n");
770 continue;
771 }
772
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700773 ctx_id = report32[2] & stream->specific_ctx_id_mask;
Robert Bragg19f81df2017-06-13 12:23:03 +0100774
775 /*
776 * Squash whatever is in the CTX_ID field if it's marked as
777 * invalid to be sure we avoid false-positive, single-context
778 * filtering below...
779 *
780 * Note: that we don't clear the valid_ctx_bit so userspace can
781 * understand that the ID has been squashed by the kernel.
782 */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700783 if (!(report32[0] & stream->perf->gen8_valid_ctx_bit) &&
784 INTEL_GEN(stream->perf->i915) <= 11)
Robert Bragg19f81df2017-06-13 12:23:03 +0100785 ctx_id = report32[2] = INVALID_CTX_ID;
786
787 /*
788 * NB: For Gen 8 the OA unit no longer supports clock gating
789 * off for a specific context and the kernel can't securely
790 * stop the counters from updating as system-wide / global
791 * values.
792 *
793 * Automatic reports now include a context ID so reports can be
794 * filtered on the cpu but it's not worth trying to
795 * automatically subtract/hide counter progress for other
796 * contexts while filtering since we can't stop userspace
797 * issuing MI_REPORT_PERF_COUNT commands which would still
798 * provide a side-band view of the real values.
799 *
800 * To allow userspace (such as Mesa/GL_INTEL_performance_query)
801 * to normalize counters for a single filtered context then it
802 * needs be forwarded bookend context-switch reports so that it
803 * can track switches in between MI_REPORT_PERF_COUNT commands
804 * and can itself subtract/ignore the progress of counters
805 * associated with other contexts. Note that the hardware
806 * automatically triggers reports when switching to a new
807 * context which are tagged with the ID of the newly active
808 * context. To avoid the complexity (and likely fragility) of
809 * reading ahead while parsing reports to try and minimize
810 * forwarding redundant context switch reports (i.e. between
811 * other, unrelated contexts) we simply elect to forward them
812 * all.
813 *
814 * We don't rely solely on the reason field to identify context
815 * switches since it's not-uncommon for periodic samples to
816 * identify a switch before any 'context switch' report.
817 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100818 if (!stream->perf->exclusive_stream->ctx ||
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700819 stream->specific_ctx_id == ctx_id ||
820 stream->oa_buffer.last_ctx_id == stream->specific_ctx_id ||
Robert Bragg19f81df2017-06-13 12:23:03 +0100821 reason & OAREPORT_REASON_CTX_SWITCH) {
822
823 /*
824 * While filtering for a single context we avoid
825 * leaking the IDs of other contexts.
826 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100827 if (stream->perf->exclusive_stream->ctx &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700828 stream->specific_ctx_id != ctx_id) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100829 report32[2] = INVALID_CTX_ID;
830 }
831
832 ret = append_oa_sample(stream, buf, count, offset,
833 report);
834 if (ret)
835 break;
836
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700837 stream->oa_buffer.last_ctx_id = ctx_id;
Robert Bragg19f81df2017-06-13 12:23:03 +0100838 }
839
840 /*
841 * The above reason field sanity check is based on
842 * the assumption that the OA buffer is initially
843 * zeroed and we reset the field after copying so the
844 * check is still meaningful once old reports start
845 * being overwritten.
846 */
847 report32[0] = 0;
848 }
849
850 if (start_offset != *offset) {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700851 i915_reg_t oaheadptr;
852
853 oaheadptr = IS_GEN(stream->perf->i915, 12) ?
854 GEN12_OAG_OAHEADPTR : GEN8_OAHEADPTR;
855
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700856 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100857
858 /*
859 * We removed the gtt_offset for the copy loop above, indexing
860 * relative to oa_buf_base so put back here...
861 */
862 head += gtt_offset;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700863 intel_uncore_write(uncore, oaheadptr,
864 head & GEN12_OAG_OAHEADPTR_MASK);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700865 stream->oa_buffer.head = head;
Robert Bragg19f81df2017-06-13 12:23:03 +0100866
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700867 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100868 }
869
870 return ret;
871}
872
873/**
874 * gen8_oa_read - copy status records then buffered OA reports
875 * @stream: An i915-perf stream opened for OA metrics
876 * @buf: destination buffer given by userspace
877 * @count: the number of bytes userspace wants to read
878 * @offset: (inout): the current position for writing into @buf
879 *
880 * Checks OA unit status registers and if necessary appends corresponding
881 * status records for userspace (such as for a buffer full condition) and then
882 * initiate appending any buffered OA reports.
883 *
884 * Updates @offset according to the number of bytes successfully copied into
885 * the userspace buffer.
886 *
887 * NB: some data may be successfully copied to the userspace buffer
888 * even if an error is returned, and this is reflected in the
889 * updated @offset.
890 *
891 * Returns: zero on success or a negative error code
892 */
893static int gen8_oa_read(struct i915_perf_stream *stream,
894 char __user *buf,
895 size_t count,
896 size_t *offset)
897{
Chris Wilson52111c42019-10-10 16:05:20 +0100898 struct intel_uncore *uncore = stream->uncore;
Robert Bragg19f81df2017-06-13 12:23:03 +0100899 u32 oastatus;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700900 i915_reg_t oastatus_reg;
Robert Bragg19f81df2017-06-13 12:23:03 +0100901 int ret;
902
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +0530903 if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
Robert Bragg19f81df2017-06-13 12:23:03 +0100904 return -EIO;
905
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700906 oastatus_reg = IS_GEN(stream->perf->i915, 12) ?
907 GEN12_OAG_OASTATUS : GEN8_OASTATUS;
908
909 oastatus = intel_uncore_read(uncore, oastatus_reg);
Robert Bragg19f81df2017-06-13 12:23:03 +0100910
911 /*
912 * We treat OABUFFER_OVERFLOW as a significant error:
913 *
914 * Although theoretically we could handle this more gracefully
915 * sometimes, some Gens don't correctly suppress certain
916 * automatically triggered reports in this condition and so we
917 * have to assume that old reports are now being trampled
918 * over.
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200919 *
920 * Considering how we don't currently give userspace control
921 * over the OA buffer size and always configure a large 16MB
922 * buffer, then a buffer overflow does anyway likely indicate
923 * that something has gone quite badly wrong.
Robert Bragg19f81df2017-06-13 12:23:03 +0100924 */
925 if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
926 ret = append_oa_status(stream, buf, count, offset,
927 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
928 if (ret)
929 return ret;
930
931 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700932 stream->period_exponent);
Robert Bragg19f81df2017-06-13 12:23:03 +0100933
Chris Wilson8f8b1172019-10-07 22:09:41 +0100934 stream->perf->ops.oa_disable(stream);
935 stream->perf->ops.oa_enable(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +0100936
937 /*
938 * Note: .oa_enable() is expected to re-init the oabuffer and
939 * reset GEN8_OASTATUS for us
940 */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700941 oastatus = intel_uncore_read(uncore, oastatus_reg);
Robert Bragg19f81df2017-06-13 12:23:03 +0100942 }
943
944 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
945 ret = append_oa_status(stream, buf, count, offset,
946 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
947 if (ret)
948 return ret;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700949 intel_uncore_write(uncore, oastatus_reg,
Chris Wilson8f8b1172019-10-07 22:09:41 +0100950 oastatus & ~GEN8_OASTATUS_REPORT_LOST);
Robert Bragg19f81df2017-06-13 12:23:03 +0100951 }
952
953 return gen8_append_oa_reports(stream, buf, count, offset);
954}
955
956/**
957 * Copies all buffered OA reports into userspace read() buffer.
958 * @stream: An i915-perf stream opened for OA metrics
959 * @buf: destination buffer given by userspace
960 * @count: the number of bytes userspace wants to read
961 * @offset: (inout): the current position for writing into @buf
962 *
963 * Notably any error condition resulting in a short read (-%ENOSPC or
964 * -%EFAULT) will be returned even though one or more records may
965 * have been successfully copied. In this case it's up to the caller
966 * to decide if the error should be squashed before returning to
967 * userspace.
968 *
969 * Note: reports are consumed from the head, and appended to the
970 * tail, so the tail chases the head?... If you think that's mad
971 * and back-to-front you're not alone, but this follows the
972 * Gen PRM naming convention.
973 *
974 * Returns: 0 on success, negative error code on failure.
975 */
Robert Braggd7965152016-11-07 19:49:52 +0000976static int gen7_append_oa_reports(struct i915_perf_stream *stream,
977 char __user *buf,
978 size_t count,
Robert Bragg3bb335c2017-05-11 16:43:27 +0100979 size_t *offset)
Robert Braggd7965152016-11-07 19:49:52 +0000980{
Chris Wilson52111c42019-10-10 16:05:20 +0100981 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700982 int report_size = stream->oa_buffer.format_size;
983 u8 *oa_buf_base = stream->oa_buffer.vaddr;
984 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200985 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100986 size_t start_offset = *offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100987 unsigned long flags;
988 unsigned int aged_tail_idx;
989 u32 head, tail;
Robert Braggd7965152016-11-07 19:49:52 +0000990 u32 taken;
991 int ret = 0;
992
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +0530993 if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
Robert Braggd7965152016-11-07 19:49:52 +0000994 return -EIO;
995
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700996 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggf2790202017-05-11 16:43:26 +0100997
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700998 head = stream->oa_buffer.head;
999 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
1000 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001001
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001002 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001003
1004 /* An invalid tail pointer here means we're still waiting for the poll
1005 * hrtimer callback to give us a pointer
Robert Braggf2790202017-05-11 16:43:26 +01001006 */
Robert Bragg0dd860c2017-05-11 16:43:28 +01001007 if (tail == INVALID_TAIL_PTR)
Robert Braggd7965152016-11-07 19:49:52 +00001008 return -EAGAIN;
1009
Robert Bragg0dd860c2017-05-11 16:43:28 +01001010 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
1011 * while indexing relative to oa_buf_base.
1012 */
1013 head -= gtt_offset;
1014 tail -= gtt_offset;
1015
1016 /* An out of bounds or misaligned head or tail pointer implies a driver
1017 * bug since we validate + align the tail pointers we read from the
1018 * hardware and we are in full control of the head pointer which should
1019 * only be incremented by multiples of the report size (notably also
1020 * all a power of two).
1021 */
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +05301022 if (drm_WARN_ONCE(&uncore->i915->drm,
1023 head > OA_BUFFER_SIZE || head % report_size ||
1024 tail > OA_BUFFER_SIZE || tail % report_size,
1025 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
1026 head, tail))
Robert Bragg0dd860c2017-05-11 16:43:28 +01001027 return -EIO;
1028
Robert Braggd7965152016-11-07 19:49:52 +00001029
1030 for (/* none */;
1031 (taken = OA_TAKEN(tail, head));
1032 head = (head + report_size) & mask) {
1033 u8 *report = oa_buf_base + head;
1034 u32 *report32 = (void *)report;
1035
1036 /* All the report sizes factor neatly into the buffer
1037 * size so we never expect to see a report split
1038 * between the beginning and end of the buffer.
1039 *
1040 * Given the initial alignment check a misalignment
1041 * here would imply a driver bug that would result
1042 * in an overrun.
1043 */
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +05301044 if (drm_WARN_ON(&uncore->i915->drm,
1045 (OA_BUFFER_SIZE - head) < report_size)) {
Wambui Karuga0bf85732020-02-18 20:39:36 +03001046 drm_err(&uncore->i915->drm,
1047 "Spurious OA head ptr: non-integral report offset\n");
Robert Braggd7965152016-11-07 19:49:52 +00001048 break;
1049 }
1050
1051 /* The report-ID field for periodic samples includes
1052 * some undocumented flags related to what triggered
1053 * the report and is never expected to be zero so we
1054 * can check that the report isn't invalid before
1055 * copying it to userspace...
1056 */
1057 if (report32[0] == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01001058 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg712122e2017-05-11 16:43:31 +01001059 DRM_NOTE("Skipping spurious, invalid OA report\n");
Robert Braggd7965152016-11-07 19:49:52 +00001060 continue;
1061 }
1062
1063 ret = append_oa_sample(stream, buf, count, offset, report);
1064 if (ret)
1065 break;
1066
1067 /* The above report-id field sanity check is based on
1068 * the assumption that the OA buffer is initially
1069 * zeroed and we reset the field after copying so the
1070 * check is still meaningful once old reports start
1071 * being overwritten.
1072 */
1073 report32[0] = 0;
1074 }
1075
Robert Bragg3bb335c2017-05-11 16:43:27 +01001076 if (start_offset != *offset) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001077 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001078
Robert Bragg3bb335c2017-05-11 16:43:27 +01001079 /* We removed the gtt_offset for the copy loop above, indexing
1080 * relative to oa_buf_base so put back here...
1081 */
1082 head += gtt_offset;
1083
Chris Wilson8f8b1172019-10-07 22:09:41 +01001084 intel_uncore_write(uncore, GEN7_OASTATUS2,
1085 (head & GEN7_OASTATUS2_HEAD_MASK) |
1086 GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001087 stream->oa_buffer.head = head;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001088
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001089 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg3bb335c2017-05-11 16:43:27 +01001090 }
Robert Braggd7965152016-11-07 19:49:52 +00001091
1092 return ret;
1093}
1094
Robert Bragg16d98b32016-12-07 21:40:33 +00001095/**
1096 * gen7_oa_read - copy status records then buffered OA reports
1097 * @stream: An i915-perf stream opened for OA metrics
1098 * @buf: destination buffer given by userspace
1099 * @count: the number of bytes userspace wants to read
1100 * @offset: (inout): the current position for writing into @buf
1101 *
1102 * Checks Gen 7 specific OA unit status registers and if necessary appends
1103 * corresponding status records for userspace (such as for a buffer full
1104 * condition) and then initiate appending any buffered OA reports.
1105 *
1106 * Updates @offset according to the number of bytes successfully copied into
1107 * the userspace buffer.
1108 *
1109 * Returns: zero on success or a negative error code
1110 */
Robert Braggd7965152016-11-07 19:49:52 +00001111static int gen7_oa_read(struct i915_perf_stream *stream,
1112 char __user *buf,
1113 size_t count,
1114 size_t *offset)
1115{
Chris Wilson52111c42019-10-10 16:05:20 +01001116 struct intel_uncore *uncore = stream->uncore;
Robert Braggd7965152016-11-07 19:49:52 +00001117 u32 oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001118 int ret;
1119
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +05301120 if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
Robert Braggd7965152016-11-07 19:49:52 +00001121 return -EIO;
1122
Chris Wilson8f8b1172019-10-07 22:09:41 +01001123 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001124
Robert Braggd7965152016-11-07 19:49:52 +00001125 /* XXX: On Haswell we don't have a safe way to clear oastatus1
1126 * bits while the OA unit is enabled (while the tail pointer
1127 * may be updated asynchronously) so we ignore status bits
1128 * that have already been reported to userspace.
1129 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001130 oastatus1 &= ~stream->perf->gen7_latched_oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001131
1132 /* We treat OABUFFER_OVERFLOW as a significant error:
1133 *
1134 * - The status can be interpreted to mean that the buffer is
1135 * currently full (with a higher precedence than OA_TAKEN()
1136 * which will start to report a near-empty buffer after an
1137 * overflow) but it's awkward that we can't clear the status
1138 * on Haswell, so without a reset we won't be able to catch
1139 * the state again.
1140 *
1141 * - Since it also implies the HW has started overwriting old
1142 * reports it may also affect our sanity checks for invalid
1143 * reports when copying to userspace that assume new reports
1144 * are being written to cleared memory.
1145 *
1146 * - In the future we may want to introduce a flight recorder
1147 * mode where the driver will automatically maintain a safe
1148 * guard band between head/tail, avoiding this overflow
1149 * condition, but we avoid the added driver complexity for
1150 * now.
1151 */
1152 if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
1153 ret = append_oa_status(stream, buf, count, offset,
1154 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
1155 if (ret)
1156 return ret;
1157
Robert Bragg19f81df2017-06-13 12:23:03 +01001158 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001159 stream->period_exponent);
Robert Braggd7965152016-11-07 19:49:52 +00001160
Chris Wilson8f8b1172019-10-07 22:09:41 +01001161 stream->perf->ops.oa_disable(stream);
1162 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001163
Chris Wilson8f8b1172019-10-07 22:09:41 +01001164 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001165 }
1166
1167 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
1168 ret = append_oa_status(stream, buf, count, offset,
1169 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1170 if (ret)
1171 return ret;
Chris Wilson8f8b1172019-10-07 22:09:41 +01001172 stream->perf->gen7_latched_oastatus1 |=
Robert Braggd7965152016-11-07 19:49:52 +00001173 GEN7_OASTATUS1_REPORT_LOST;
1174 }
1175
Robert Bragg3bb335c2017-05-11 16:43:27 +01001176 return gen7_append_oa_reports(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001177}
1178
Robert Bragg16d98b32016-12-07 21:40:33 +00001179/**
1180 * i915_oa_wait_unlocked - handles blocking IO until OA data available
1181 * @stream: An i915-perf stream opened for OA metrics
1182 *
1183 * Called when userspace tries to read() from a blocking stream FD opened
1184 * for OA metrics. It waits until the hrtimer callback finds a non-empty
1185 * OA buffer and wakes us.
1186 *
1187 * Note: it's acceptable to have this return with some false positives
1188 * since any subsequent read handling will return -EAGAIN if there isn't
1189 * really data ready for userspace yet.
1190 *
1191 * Returns: zero on success or a negative error code
1192 */
Robert Braggd7965152016-11-07 19:49:52 +00001193static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
1194{
Robert Braggd7965152016-11-07 19:49:52 +00001195 /* We would wait indefinitely if periodic sampling is not enabled */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001196 if (!stream->periodic)
Robert Braggd7965152016-11-07 19:49:52 +00001197 return -EIO;
1198
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001199 return wait_event_interruptible(stream->poll_wq,
1200 oa_buffer_check_unlocked(stream));
Robert Braggd7965152016-11-07 19:49:52 +00001201}
1202
Robert Bragg16d98b32016-12-07 21:40:33 +00001203/**
1204 * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
1205 * @stream: An i915-perf stream opened for OA metrics
1206 * @file: An i915 perf stream file
1207 * @wait: poll() state table
1208 *
1209 * For handling userspace polling on an i915 perf stream opened for OA metrics,
1210 * this starts a poll_wait with the wait queue that our hrtimer callback wakes
1211 * when it sees data ready to read in the circular OA buffer.
1212 */
Robert Braggd7965152016-11-07 19:49:52 +00001213static void i915_oa_poll_wait(struct i915_perf_stream *stream,
1214 struct file *file,
1215 poll_table *wait)
1216{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001217 poll_wait(file, &stream->poll_wq, wait);
Robert Braggd7965152016-11-07 19:49:52 +00001218}
1219
Robert Bragg16d98b32016-12-07 21:40:33 +00001220/**
1221 * i915_oa_read - just calls through to &i915_oa_ops->read
1222 * @stream: An i915-perf stream opened for OA metrics
1223 * @buf: destination buffer given by userspace
1224 * @count: the number of bytes userspace wants to read
1225 * @offset: (inout): the current position for writing into @buf
1226 *
1227 * Updates @offset according to the number of bytes successfully copied into
1228 * the userspace buffer.
1229 *
1230 * Returns: zero on success or a negative error code
1231 */
Robert Braggd7965152016-11-07 19:49:52 +00001232static int i915_oa_read(struct i915_perf_stream *stream,
1233 char __user *buf,
1234 size_t count,
1235 size_t *offset)
1236{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001237 return stream->perf->ops.read(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001238}
1239
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001240static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001241{
Chris Wilson5e2a0412019-04-26 17:33:34 +01001242 struct i915_gem_engines_iter it;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001243 struct i915_gem_context *ctx = stream->ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001244 struct intel_context *ce;
Chris Wilsonfa9f6682019-04-26 17:33:29 +01001245 int err;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001246
Chris Wilson5e2a0412019-04-26 17:33:34 +01001247 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
Lionel Landwerlin9a613632019-10-10 16:05:19 +01001248 if (ce->engine != stream->engine) /* first match! */
Chris Wilson5e2a0412019-04-26 17:33:34 +01001249 continue;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001250
Chris Wilson5e2a0412019-04-26 17:33:34 +01001251 /*
1252 * As the ID is the gtt offset of the context's vma we
1253 * pin the vma to ensure the ID remains fixed.
1254 */
1255 err = intel_context_pin(ce);
1256 if (err == 0) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001257 stream->pinned_ctx = ce;
Chris Wilson5e2a0412019-04-26 17:33:34 +01001258 break;
1259 }
1260 }
1261 i915_gem_context_unlock_engines(ctx);
1262
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001263 return stream->pinned_ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001264}
1265
Robert Bragg16d98b32016-12-07 21:40:33 +00001266/**
1267 * oa_get_render_ctx_id - determine and hold ctx hw id
1268 * @stream: An i915-perf stream opened for OA metrics
1269 *
1270 * Determine the render context hw id, and ensure it remains fixed for the
Robert Braggd7965152016-11-07 19:49:52 +00001271 * lifetime of the stream. This ensures that we don't have to worry about
1272 * updating the context ID in OACONTROL on the fly.
Robert Bragg16d98b32016-12-07 21:40:33 +00001273 *
1274 * Returns: zero on success or a negative error code
Robert Braggd7965152016-11-07 19:49:52 +00001275 */
1276static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
1277{
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001278 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001279
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001280 ce = oa_pin_context(stream);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001281 if (IS_ERR(ce))
1282 return PTR_ERR(ce);
Robert Braggd7965152016-11-07 19:49:52 +00001283
Chris Wilson8f8b1172019-10-07 22:09:41 +01001284 switch (INTEL_GEN(ce->engine->i915)) {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001285 case 7: {
Robert Bragg19f81df2017-06-13 12:23:03 +01001286 /*
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001287 * On Haswell we don't do any post processing of the reports
1288 * and don't need to use the mask.
Robert Bragg19f81df2017-06-13 12:23:03 +01001289 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001290 stream->specific_ctx_id = i915_ggtt_offset(ce->state);
1291 stream->specific_ctx_id_mask = 0;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001292 break;
Robert Bragg19f81df2017-06-13 12:23:03 +01001293 }
Robert Braggd7965152016-11-07 19:49:52 +00001294
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001295 case 8:
1296 case 9:
1297 case 10:
Michal Wajdeczko19c17b72019-10-28 16:45:20 +00001298 if (intel_engine_in_execlists_submission_mode(ce->engine)) {
1299 stream->specific_ctx_id_mask =
1300 (1U << GEN8_CTX_ID_WIDTH) - 1;
1301 stream->specific_ctx_id = stream->specific_ctx_id_mask;
1302 } else {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001303 /*
1304 * When using GuC, the context descriptor we write in
1305 * i915 is read by GuC and rewritten before it's
1306 * actually written into the hardware. The LRCA is
1307 * what is put into the context id field of the
1308 * context descriptor by GuC. Because it's aligned to
1309 * a page, the lower 12bits are always at 0 and
1310 * dropped by GuC. They won't be part of the context
1311 * ID in the OA reports, so squash those lower bits.
1312 */
Chris Wilson53b26222020-04-28 19:47:49 +01001313 stream->specific_ctx_id = ce->lrc.lrca >> 12;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001314
1315 /*
1316 * GuC uses the top bit to signal proxy submission, so
1317 * ignore that bit.
1318 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001319 stream->specific_ctx_id_mask =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001320 (1U << (GEN8_CTX_ID_WIDTH - 1)) - 1;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001321 }
1322 break;
1323
Michel Thierry45e9c822019-08-23 01:20:50 -07001324 case 11:
1325 case 12: {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001326 stream->specific_ctx_id_mask =
Chris Wilson2935ed52019-10-04 14:40:08 +01001327 ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) << (GEN11_SW_CTX_ID_SHIFT - 32);
Umesh Nerlige Ramappa6f280b12020-01-23 17:37:01 -08001328 /*
1329 * Pick an unused context id
1330 * 0 - (NUM_CONTEXT_TAG - 1) are used by other contexts
1331 * GEN12_MAX_CONTEXT_HW_ID (0x7ff) is used by idle context
1332 */
1333 stream->specific_ctx_id = (GEN12_MAX_CONTEXT_HW_ID - 1) << (GEN11_SW_CTX_ID_SHIFT - 32);
1334 BUILD_BUG_ON((GEN12_MAX_CONTEXT_HW_ID - 1) < NUM_CONTEXT_TAG);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001335 break;
1336 }
1337
1338 default:
Chris Wilson8f8b1172019-10-07 22:09:41 +01001339 MISSING_CASE(INTEL_GEN(ce->engine->i915));
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001340 }
1341
Umesh Nerlige Ramappa6f280b12020-01-23 17:37:01 -08001342 ce->tag = stream->specific_ctx_id;
Chris Wilson2935ed52019-10-04 14:40:08 +01001343
Wambui Karuga0bf85732020-02-18 20:39:36 +03001344 drm_dbg(&stream->perf->i915->drm,
1345 "filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
1346 stream->specific_ctx_id,
1347 stream->specific_ctx_id_mask);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001348
Chris Wilson266a2402017-05-04 10:33:08 +01001349 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001350}
1351
Robert Bragg16d98b32016-12-07 21:40:33 +00001352/**
1353 * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
1354 * @stream: An i915-perf stream opened for OA metrics
1355 *
1356 * In case anything needed doing to ensure the context HW ID would remain valid
1357 * for the lifetime of the stream, then that can be undone here.
1358 */
Robert Braggd7965152016-11-07 19:49:52 +00001359static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
1360{
Chris Wilson1fc44d92018-05-17 22:26:32 +01001361 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001362
Chris Wilson2935ed52019-10-04 14:40:08 +01001363 ce = fetch_and_zero(&stream->pinned_ctx);
1364 if (ce) {
1365 ce->tag = 0; /* recomputed on next submission after parking */
1366 intel_context_unpin(ce);
1367 }
1368
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001369 stream->specific_ctx_id = INVALID_CTX_ID;
1370 stream->specific_ctx_id_mask = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001371}
1372
1373static void
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001374free_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001375{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001376 i915_vma_unpin_and_release(&stream->oa_buffer.vma,
Chris Wilson6a2f59e2018-07-21 13:50:37 +01001377 I915_VMA_RELEASE_MAP);
Robert Braggd7965152016-11-07 19:49:52 +00001378
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001379 stream->oa_buffer.vaddr = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001380}
1381
Lionel Landwerlin6a450082019-10-12 08:23:06 +01001382static void
1383free_oa_configs(struct i915_perf_stream *stream)
1384{
1385 struct i915_oa_config_bo *oa_bo, *tmp;
1386
1387 i915_oa_config_put(stream->oa_config);
1388 llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node)
1389 free_oa_config_bo(oa_bo);
1390}
1391
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001392static void
1393free_noa_wait(struct i915_perf_stream *stream)
1394{
1395 i915_vma_unpin_and_release(&stream->noa_wait, 0);
1396}
1397
Robert Braggd7965152016-11-07 19:49:52 +00001398static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
1399{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001400 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00001401
Chris Wilson8f8b1172019-10-07 22:09:41 +01001402 BUG_ON(stream != perf->exclusive_stream);
Robert Braggd7965152016-11-07 19:49:52 +00001403
Robert Bragg19f81df2017-06-13 12:23:03 +01001404 /*
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001405 * Unset exclusive_stream first, it will be checked while disabling
1406 * the metric set on gen8+.
Chris Wilsona5af0812020-02-27 08:57:05 +00001407 *
1408 * See i915_oa_init_reg_state() and lrc_configure_all_contexts()
Robert Bragg19f81df2017-06-13 12:23:03 +01001409 */
Chris Wilsona5af0812020-02-27 08:57:05 +00001410 WRITE_ONCE(perf->exclusive_stream, NULL);
Chris Wilson8f8b1172019-10-07 22:09:41 +01001411 perf->ops.disable_metric_set(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001412
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001413 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001414
Chris Wilson52111c42019-10-10 16:05:20 +01001415 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
Chris Wilsona5efcde2019-10-11 20:03:17 +01001416 intel_engine_pm_put(stream->engine);
Robert Braggd7965152016-11-07 19:49:52 +00001417
1418 if (stream->ctx)
1419 oa_put_render_ctx_id(stream);
1420
Lionel Landwerlin6a450082019-10-12 08:23:06 +01001421 free_oa_configs(stream);
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001422 free_noa_wait(stream);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001423
Chris Wilson8f8b1172019-10-07 22:09:41 +01001424 if (perf->spurious_report_rs.missed) {
Robert Bragg712122e2017-05-11 16:43:31 +01001425 DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n",
Chris Wilson8f8b1172019-10-07 22:09:41 +01001426 perf->spurious_report_rs.missed);
Robert Bragg712122e2017-05-11 16:43:31 +01001427 }
Robert Braggd7965152016-11-07 19:49:52 +00001428}
1429
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001430static void gen7_init_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001431{
Chris Wilson52111c42019-10-10 16:05:20 +01001432 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001433 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001434 unsigned long flags;
1435
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001436 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggd7965152016-11-07 19:49:52 +00001437
1438 /* Pre-DevBDW: OABUFFER must be set with counters off,
1439 * before OASTATUS1, but after OASTATUS2
1440 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001441 intel_uncore_write(uncore, GEN7_OASTATUS2, /* head */
1442 gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001443 stream->oa_buffer.head = gtt_offset;
Robert Braggf2790202017-05-11 16:43:26 +01001444
Chris Wilson8f8b1172019-10-07 22:09:41 +01001445 intel_uncore_write(uncore, GEN7_OABUFFER, gtt_offset);
Robert Braggf2790202017-05-11 16:43:26 +01001446
Chris Wilson8f8b1172019-10-07 22:09:41 +01001447 intel_uncore_write(uncore, GEN7_OASTATUS1, /* tail */
1448 gtt_offset | OABUFFER_SIZE_16M);
Robert Braggd7965152016-11-07 19:49:52 +00001449
Robert Bragg0dd860c2017-05-11 16:43:28 +01001450 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001451 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1452 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001453
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001454 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001455
Robert Braggd7965152016-11-07 19:49:52 +00001456 /* On Haswell we have to track which OASTATUS1 flags we've
1457 * already seen since they can't be cleared while periodic
1458 * sampling is enabled.
1459 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001460 stream->perf->gen7_latched_oastatus1 = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001461
1462 /* NB: although the OA buffer will initially be allocated
1463 * zeroed via shmfs (and so this memset is redundant when
1464 * first allocating), we may re-init the OA buffer, either
1465 * when re-enabling a stream or in error/reset paths.
1466 *
1467 * The reason we clear the buffer for each re-init is for the
1468 * sanity check in gen7_append_oa_reports() that looks at the
1469 * report-id field to make sure it's non-zero which relies on
1470 * the assumption that new reports are being written to zeroed
1471 * memory...
1472 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001473 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001474
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001475 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00001476}
1477
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001478static void gen8_init_oa_buffer(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001479{
Chris Wilson52111c42019-10-10 16:05:20 +01001480 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001481 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg19f81df2017-06-13 12:23:03 +01001482 unsigned long flags;
1483
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001484 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001485
Chris Wilson8f8b1172019-10-07 22:09:41 +01001486 intel_uncore_write(uncore, GEN8_OASTATUS, 0);
1487 intel_uncore_write(uncore, GEN8_OAHEADPTR, gtt_offset);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001488 stream->oa_buffer.head = gtt_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01001489
Chris Wilson8f8b1172019-10-07 22:09:41 +01001490 intel_uncore_write(uncore, GEN8_OABUFFER_UDW, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01001491
1492 /*
1493 * PRM says:
1494 *
1495 * "This MMIO must be set before the OATAILPTR
1496 * register and after the OAHEADPTR register. This is
1497 * to enable proper functionality of the overflow
1498 * bit."
1499 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001500 intel_uncore_write(uncore, GEN8_OABUFFER, gtt_offset |
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001501 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
Chris Wilson8f8b1172019-10-07 22:09:41 +01001502 intel_uncore_write(uncore, GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
Robert Bragg19f81df2017-06-13 12:23:03 +01001503
1504 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001505 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1506 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg19f81df2017-06-13 12:23:03 +01001507
1508 /*
1509 * Reset state used to recognise context switches, affecting which
1510 * reports we will forward to userspace while filtering for a single
1511 * context.
1512 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001513 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
Robert Bragg19f81df2017-06-13 12:23:03 +01001514
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001515 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001516
1517 /*
1518 * NB: although the OA buffer will initially be allocated
1519 * zeroed via shmfs (and so this memset is redundant when
1520 * first allocating), we may re-init the OA buffer, either
1521 * when re-enabling a stream or in error/reset paths.
1522 *
1523 * The reason we clear the buffer for each re-init is for the
1524 * sanity check in gen8_append_oa_reports() that looks at the
1525 * reason field to make sure it's non-zero which relies on
1526 * the assumption that new reports are being written to zeroed
1527 * memory...
1528 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001529 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Bragg19f81df2017-06-13 12:23:03 +01001530
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001531 stream->pollin = false;
Robert Bragg19f81df2017-06-13 12:23:03 +01001532}
1533
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07001534static void gen12_init_oa_buffer(struct i915_perf_stream *stream)
1535{
1536 struct intel_uncore *uncore = stream->uncore;
1537 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
1538 unsigned long flags;
1539
1540 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1541
1542 intel_uncore_write(uncore, GEN12_OAG_OASTATUS, 0);
1543 intel_uncore_write(uncore, GEN12_OAG_OAHEADPTR,
1544 gtt_offset & GEN12_OAG_OAHEADPTR_MASK);
1545 stream->oa_buffer.head = gtt_offset;
1546
1547 /*
1548 * PRM says:
1549 *
1550 * "This MMIO must be set before the OATAILPTR
1551 * register and after the OAHEADPTR register. This is
1552 * to enable proper functionality of the overflow
1553 * bit."
1554 */
1555 intel_uncore_write(uncore, GEN12_OAG_OABUFFER, gtt_offset |
1556 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
1557 intel_uncore_write(uncore, GEN12_OAG_OATAILPTR,
1558 gtt_offset & GEN12_OAG_OATAILPTR_MASK);
1559
1560 /* Mark that we need updated tail pointers to read from... */
1561 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1562 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1563
1564 /*
1565 * Reset state used to recognise context switches, affecting which
1566 * reports we will forward to userspace while filtering for a single
1567 * context.
1568 */
1569 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
1570
1571 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1572
1573 /*
1574 * NB: although the OA buffer will initially be allocated
1575 * zeroed via shmfs (and so this memset is redundant when
1576 * first allocating), we may re-init the OA buffer, either
1577 * when re-enabling a stream or in error/reset paths.
1578 *
1579 * The reason we clear the buffer for each re-init is for the
1580 * sanity check in gen8_append_oa_reports() that looks at the
1581 * reason field to make sure it's non-zero which relies on
1582 * the assumption that new reports are being written to zeroed
1583 * memory...
1584 */
1585 memset(stream->oa_buffer.vaddr, 0,
1586 stream->oa_buffer.vma->size);
1587
1588 stream->pollin = false;
1589}
1590
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001591static int alloc_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001592{
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +05301593 struct drm_i915_private *i915 = stream->perf->i915;
Robert Braggd7965152016-11-07 19:49:52 +00001594 struct drm_i915_gem_object *bo;
1595 struct i915_vma *vma;
1596 int ret;
1597
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +05301598 if (drm_WARN_ON(&i915->drm, stream->oa_buffer.vma))
Robert Braggd7965152016-11-07 19:49:52 +00001599 return -ENODEV;
1600
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001601 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
1602 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
1603
Chris Wilson8f8b1172019-10-07 22:09:41 +01001604 bo = i915_gem_object_create_shmem(stream->perf->i915, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001605 if (IS_ERR(bo)) {
Wambui Karuga00376cc2020-01-31 12:34:12 +03001606 drm_err(&i915->drm, "Failed to allocate OA buffer\n");
Chris Wilson28507482019-10-04 14:39:58 +01001607 return PTR_ERR(bo);
Robert Braggd7965152016-11-07 19:49:52 +00001608 }
1609
Chris Wilsona679f582019-03-21 16:19:07 +00001610 i915_gem_object_set_cache_coherency(bo, I915_CACHE_LLC);
Robert Braggd7965152016-11-07 19:49:52 +00001611
1612 /* PreHSW required 512K alignment, HSW requires 16M */
1613 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0);
1614 if (IS_ERR(vma)) {
1615 ret = PTR_ERR(vma);
1616 goto err_unref;
1617 }
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001618 stream->oa_buffer.vma = vma;
Robert Braggd7965152016-11-07 19:49:52 +00001619
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001620 stream->oa_buffer.vaddr =
Robert Braggd7965152016-11-07 19:49:52 +00001621 i915_gem_object_pin_map(bo, I915_MAP_WB);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001622 if (IS_ERR(stream->oa_buffer.vaddr)) {
1623 ret = PTR_ERR(stream->oa_buffer.vaddr);
Robert Braggd7965152016-11-07 19:49:52 +00001624 goto err_unpin;
1625 }
1626
Chris Wilson28507482019-10-04 14:39:58 +01001627 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001628
1629err_unpin:
1630 __i915_vma_unpin(vma);
1631
1632err_unref:
1633 i915_gem_object_put(bo);
1634
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001635 stream->oa_buffer.vaddr = NULL;
1636 stream->oa_buffer.vma = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001637
Robert Braggd7965152016-11-07 19:49:52 +00001638 return ret;
1639}
1640
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001641static u32 *save_restore_register(struct i915_perf_stream *stream, u32 *cs,
1642 bool save, i915_reg_t reg, u32 offset,
1643 u32 dword_count)
1644{
1645 u32 cmd;
1646 u32 d;
1647
1648 cmd = save ? MI_STORE_REGISTER_MEM : MI_LOAD_REGISTER_MEM;
1649 if (INTEL_GEN(stream->perf->i915) >= 8)
1650 cmd++;
1651
1652 for (d = 0; d < dword_count; d++) {
1653 *cs++ = cmd;
1654 *cs++ = i915_mmio_reg_offset(reg) + 4 * d;
1655 *cs++ = intel_gt_scratch_offset(stream->engine->gt,
1656 offset) + 4 * d;
1657 *cs++ = 0;
1658 }
1659
1660 return cs;
1661}
1662
1663static int alloc_noa_wait(struct i915_perf_stream *stream)
1664{
1665 struct drm_i915_private *i915 = stream->perf->i915;
1666 struct drm_i915_gem_object *bo;
1667 struct i915_vma *vma;
1668 const u64 delay_ticks = 0xffffffffffffffff -
1669 DIV64_U64_ROUND_UP(
1670 atomic64_read(&stream->perf->noa_programming_delay) *
1671 RUNTIME_INFO(i915)->cs_timestamp_frequency_khz,
1672 1000000ull);
1673 const u32 base = stream->engine->mmio_base;
1674#define CS_GPR(x) GEN8_RING_CS_GPR(base, x)
1675 u32 *batch, *ts0, *cs, *jump;
1676 int ret, i;
1677 enum {
1678 START_TS,
1679 NOW_TS,
1680 DELTA_TS,
1681 JUMP_PREDICATE,
1682 DELTA_TARGET,
1683 N_CS_GPR
1684 };
1685
1686 bo = i915_gem_object_create_internal(i915, 4096);
1687 if (IS_ERR(bo)) {
Wambui Karuga00376cc2020-01-31 12:34:12 +03001688 drm_err(&i915->drm,
1689 "Failed to allocate NOA wait batchbuffer\n");
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001690 return PTR_ERR(bo);
1691 }
1692
1693 /*
1694 * We pin in GGTT because we jump into this buffer now because
1695 * multiple OA config BOs will have a jump to this address and it
1696 * needs to be fixed during the lifetime of the i915/perf stream.
1697 */
1698 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, 0, PIN_HIGH);
1699 if (IS_ERR(vma)) {
1700 ret = PTR_ERR(vma);
1701 goto err_unref;
1702 }
1703
1704 batch = cs = i915_gem_object_pin_map(bo, I915_MAP_WB);
1705 if (IS_ERR(batch)) {
1706 ret = PTR_ERR(batch);
1707 goto err_unpin;
1708 }
1709
1710 /* Save registers. */
1711 for (i = 0; i < N_CS_GPR; i++)
1712 cs = save_restore_register(
1713 stream, cs, true /* save */, CS_GPR(i),
1714 INTEL_GT_SCRATCH_FIELD_PERF_CS_GPR + 8 * i, 2);
1715 cs = save_restore_register(
1716 stream, cs, true /* save */, MI_PREDICATE_RESULT_1,
1717 INTEL_GT_SCRATCH_FIELD_PERF_PREDICATE_RESULT_1, 1);
1718
1719 /* First timestamp snapshot location. */
1720 ts0 = cs;
1721
1722 /*
1723 * Initial snapshot of the timestamp register to implement the wait.
1724 * We work with 32b values, so clear out the top 32b bits of the
1725 * register because the ALU works 64bits.
1726 */
1727 *cs++ = MI_LOAD_REGISTER_IMM(1);
1728 *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS)) + 4;
1729 *cs++ = 0;
1730 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1731 *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
1732 *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS));
1733
1734 /*
1735 * This is the location we're going to jump back into until the
1736 * required amount of time has passed.
1737 */
1738 jump = cs;
1739
1740 /*
1741 * Take another snapshot of the timestamp register. Take care to clear
1742 * up the top 32bits of CS_GPR(1) as we're using it for other
1743 * operations below.
1744 */
1745 *cs++ = MI_LOAD_REGISTER_IMM(1);
1746 *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS)) + 4;
1747 *cs++ = 0;
1748 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1749 *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
1750 *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS));
1751
1752 /*
1753 * Do a diff between the 2 timestamps and store the result back into
1754 * CS_GPR(1).
1755 */
1756 *cs++ = MI_MATH(5);
1757 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(NOW_TS));
1758 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(START_TS));
1759 *cs++ = MI_MATH_SUB;
1760 *cs++ = MI_MATH_STORE(MI_MATH_REG(DELTA_TS), MI_MATH_REG_ACCU);
1761 *cs++ = MI_MATH_STORE(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
1762
1763 /*
1764 * Transfer the carry flag (set to 1 if ts1 < ts0, meaning the
1765 * timestamp have rolled over the 32bits) into the predicate register
1766 * to be used for the predicated jump.
1767 */
1768 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1769 *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
1770 *cs++ = i915_mmio_reg_offset(MI_PREDICATE_RESULT_1);
1771
1772 /* Restart from the beginning if we had timestamps roll over. */
1773 *cs++ = (INTEL_GEN(i915) < 8 ?
1774 MI_BATCH_BUFFER_START :
1775 MI_BATCH_BUFFER_START_GEN8) |
1776 MI_BATCH_PREDICATE;
1777 *cs++ = i915_ggtt_offset(vma) + (ts0 - batch) * 4;
1778 *cs++ = 0;
1779
1780 /*
1781 * Now add the diff between to previous timestamps and add it to :
1782 * (((1 * << 64) - 1) - delay_ns)
1783 *
1784 * When the Carry Flag contains 1 this means the elapsed time is
1785 * longer than the expected delay, and we can exit the wait loop.
1786 */
1787 *cs++ = MI_LOAD_REGISTER_IMM(2);
1788 *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET));
1789 *cs++ = lower_32_bits(delay_ticks);
1790 *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET)) + 4;
1791 *cs++ = upper_32_bits(delay_ticks);
1792
1793 *cs++ = MI_MATH(4);
1794 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(DELTA_TS));
1795 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(DELTA_TARGET));
1796 *cs++ = MI_MATH_ADD;
1797 *cs++ = MI_MATH_STOREINV(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
1798
Lionel Landwerlindd590f62019-11-14 16:02:24 +02001799 *cs++ = MI_ARB_CHECK;
1800
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001801 /*
1802 * Transfer the result into the predicate register to be used for the
1803 * predicated jump.
1804 */
1805 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1806 *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
1807 *cs++ = i915_mmio_reg_offset(MI_PREDICATE_RESULT_1);
1808
1809 /* Predicate the jump. */
1810 *cs++ = (INTEL_GEN(i915) < 8 ?
1811 MI_BATCH_BUFFER_START :
1812 MI_BATCH_BUFFER_START_GEN8) |
1813 MI_BATCH_PREDICATE;
1814 *cs++ = i915_ggtt_offset(vma) + (jump - batch) * 4;
1815 *cs++ = 0;
1816
1817 /* Restore registers. */
1818 for (i = 0; i < N_CS_GPR; i++)
1819 cs = save_restore_register(
1820 stream, cs, false /* restore */, CS_GPR(i),
1821 INTEL_GT_SCRATCH_FIELD_PERF_CS_GPR + 8 * i, 2);
1822 cs = save_restore_register(
1823 stream, cs, false /* restore */, MI_PREDICATE_RESULT_1,
1824 INTEL_GT_SCRATCH_FIELD_PERF_PREDICATE_RESULT_1, 1);
1825
1826 /* And return to the ring. */
1827 *cs++ = MI_BATCH_BUFFER_END;
1828
1829 GEM_BUG_ON(cs - batch > PAGE_SIZE / sizeof(*batch));
1830
1831 i915_gem_object_flush_map(bo);
1832 i915_gem_object_unpin_map(bo);
1833
1834 stream->noa_wait = vma;
1835 return 0;
1836
1837err_unpin:
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001838 i915_vma_unpin_and_release(&vma, 0);
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001839err_unref:
1840 i915_gem_object_put(bo);
1841 return ret;
1842}
1843
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001844static u32 *write_cs_mi_lri(u32 *cs,
1845 const struct i915_oa_reg *reg_data,
1846 u32 n_regs)
Robert Braggd7965152016-11-07 19:49:52 +00001847{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001848 u32 i;
Robert Braggd7965152016-11-07 19:49:52 +00001849
1850 for (i = 0; i < n_regs; i++) {
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001851 if ((i % MI_LOAD_REGISTER_IMM_MAX_REGS) == 0) {
1852 u32 n_lri = min_t(u32,
1853 n_regs - i,
1854 MI_LOAD_REGISTER_IMM_MAX_REGS);
Robert Braggd7965152016-11-07 19:49:52 +00001855
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001856 *cs++ = MI_LOAD_REGISTER_IMM(n_lri);
1857 }
1858 *cs++ = i915_mmio_reg_offset(reg_data[i].addr);
1859 *cs++ = reg_data[i].value;
Robert Braggd7965152016-11-07 19:49:52 +00001860 }
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001861
1862 return cs;
Robert Braggd7965152016-11-07 19:49:52 +00001863}
1864
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001865static int num_lri_dwords(int num_regs)
Robert Braggd7965152016-11-07 19:49:52 +00001866{
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001867 int count = 0;
1868
1869 if (num_regs > 0) {
1870 count += DIV_ROUND_UP(num_regs, MI_LOAD_REGISTER_IMM_MAX_REGS);
1871 count += num_regs * 2;
1872 }
1873
1874 return count;
1875}
1876
1877static struct i915_oa_config_bo *
1878alloc_oa_config_buffer(struct i915_perf_stream *stream,
1879 struct i915_oa_config *oa_config)
1880{
1881 struct drm_i915_gem_object *obj;
1882 struct i915_oa_config_bo *oa_bo;
1883 size_t config_length = 0;
1884 u32 *cs;
1885 int err;
1886
1887 oa_bo = kzalloc(sizeof(*oa_bo), GFP_KERNEL);
1888 if (!oa_bo)
1889 return ERR_PTR(-ENOMEM);
1890
1891 config_length += num_lri_dwords(oa_config->mux_regs_len);
1892 config_length += num_lri_dwords(oa_config->b_counter_regs_len);
1893 config_length += num_lri_dwords(oa_config->flex_regs_len);
Lionel Landwerlin93937652019-11-13 17:46:39 +02001894 config_length += 3; /* MI_BATCH_BUFFER_START */
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001895 config_length = ALIGN(sizeof(u32) * config_length, I915_GTT_PAGE_SIZE);
1896
1897 obj = i915_gem_object_create_shmem(stream->perf->i915, config_length);
1898 if (IS_ERR(obj)) {
1899 err = PTR_ERR(obj);
1900 goto err_free;
1901 }
1902
1903 cs = i915_gem_object_pin_map(obj, I915_MAP_WB);
1904 if (IS_ERR(cs)) {
1905 err = PTR_ERR(cs);
1906 goto err_oa_bo;
1907 }
1908
1909 cs = write_cs_mi_lri(cs,
1910 oa_config->mux_regs,
1911 oa_config->mux_regs_len);
1912 cs = write_cs_mi_lri(cs,
1913 oa_config->b_counter_regs,
1914 oa_config->b_counter_regs_len);
1915 cs = write_cs_mi_lri(cs,
1916 oa_config->flex_regs,
1917 oa_config->flex_regs_len);
1918
Lionel Landwerlin93937652019-11-13 17:46:39 +02001919 /* Jump into the active wait. */
1920 *cs++ = (INTEL_GEN(stream->perf->i915) < 8 ?
1921 MI_BATCH_BUFFER_START :
1922 MI_BATCH_BUFFER_START_GEN8);
1923 *cs++ = i915_ggtt_offset(stream->noa_wait);
1924 *cs++ = 0;
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001925
1926 i915_gem_object_flush_map(obj);
1927 i915_gem_object_unpin_map(obj);
1928
1929 oa_bo->vma = i915_vma_instance(obj,
1930 &stream->engine->gt->ggtt->vm,
1931 NULL);
1932 if (IS_ERR(oa_bo->vma)) {
1933 err = PTR_ERR(oa_bo->vma);
1934 goto err_oa_bo;
1935 }
1936
1937 oa_bo->oa_config = i915_oa_config_get(oa_config);
1938 llist_add(&oa_bo->node, &stream->oa_config_bos);
1939
1940 return oa_bo;
1941
1942err_oa_bo:
1943 i915_gem_object_put(obj);
1944err_free:
1945 kfree(oa_bo);
1946 return ERR_PTR(err);
1947}
1948
1949static struct i915_vma *
1950get_oa_vma(struct i915_perf_stream *stream, struct i915_oa_config *oa_config)
1951{
1952 struct i915_oa_config_bo *oa_bo;
1953
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001954 /*
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001955 * Look for the buffer in the already allocated BOs attached
1956 * to the stream.
Robert Braggd7965152016-11-07 19:49:52 +00001957 */
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001958 llist_for_each_entry(oa_bo, stream->oa_config_bos.first, node) {
1959 if (oa_bo->oa_config == oa_config &&
1960 memcmp(oa_bo->oa_config->uuid,
1961 oa_config->uuid,
1962 sizeof(oa_config->uuid)) == 0)
1963 goto out;
1964 }
1965
1966 oa_bo = alloc_oa_config_buffer(stream, oa_config);
1967 if (IS_ERR(oa_bo))
1968 return ERR_CAST(oa_bo);
1969
1970out:
1971 return i915_vma_get(oa_bo->vma);
1972}
1973
Chris Wilson4b4e9732020-03-02 08:57:57 +00001974static struct i915_request *
1975emit_oa_config(struct i915_perf_stream *stream,
1976 struct i915_oa_config *oa_config,
1977 struct intel_context *ce)
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001978{
1979 struct i915_request *rq;
1980 struct i915_vma *vma;
1981 int err;
1982
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03001983 vma = get_oa_vma(stream, oa_config);
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001984 if (IS_ERR(vma))
Chris Wilson4b4e9732020-03-02 08:57:57 +00001985 return ERR_CAST(vma);
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001986
1987 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1988 if (err)
1989 goto err_vma_put;
1990
Chris Wilsonde5825b2019-11-25 10:58:56 +00001991 intel_engine_pm_get(ce->engine);
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001992 rq = i915_request_create(ce);
Chris Wilsonde5825b2019-11-25 10:58:56 +00001993 intel_engine_pm_put(ce->engine);
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001994 if (IS_ERR(rq)) {
1995 err = PTR_ERR(rq);
1996 goto err_vma_unpin;
1997 }
1998
1999 i915_vma_lock(vma);
2000 err = i915_request_await_object(rq, vma->obj, 0);
2001 if (!err)
2002 err = i915_vma_move_to_active(vma, rq, 0);
2003 i915_vma_unlock(vma);
2004 if (err)
2005 goto err_add_request;
2006
2007 err = rq->engine->emit_bb_start(rq,
2008 vma->node.start, 0,
2009 I915_DISPATCH_SECURE);
Chris Wilson4b4e9732020-03-02 08:57:57 +00002010 if (err)
2011 goto err_add_request;
2012
2013 i915_request_get(rq);
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01002014err_add_request:
2015 i915_request_add(rq);
2016err_vma_unpin:
2017 i915_vma_unpin(vma);
2018err_vma_put:
2019 i915_vma_put(vma);
Chris Wilson4b4e9732020-03-02 08:57:57 +00002020 return err ? ERR_PTR(err) : rq;
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01002021}
2022
Chris Wilson5f5c3822019-10-12 10:10:56 +01002023static struct intel_context *oa_context(struct i915_perf_stream *stream)
2024{
2025 return stream->pinned_ctx ?: stream->engine->kernel_context;
2026}
2027
Chris Wilson4b4e9732020-03-02 08:57:57 +00002028static struct i915_request *
2029hsw_enable_metric_set(struct i915_perf_stream *stream)
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01002030{
Chris Wilson52111c42019-10-10 16:05:20 +01002031 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01002032
2033 /*
2034 * PRM:
2035 *
2036 * OA unit is using “crclk” for its functionality. When trunk
2037 * level clock gating takes place, OA clock would be gated,
2038 * unable to count the events from non-render clock domain.
2039 * Render clock gating must be disabled when OA is enabled to
2040 * count the events from non-render domain. Unit level clock
2041 * gating for RCS should also be disabled.
2042 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002043 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2044 GEN7_DOP_CLOCK_GATE_ENABLE, 0);
2045 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2046 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01002047
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03002048 return emit_oa_config(stream, stream->oa_config, oa_context(stream));
Robert Braggd7965152016-11-07 19:49:52 +00002049}
2050
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002051static void hsw_disable_metric_set(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002052{
Chris Wilson52111c42019-10-10 16:05:20 +01002053 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002054
Chris Wilson8f8b1172019-10-07 22:09:41 +01002055 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2056 GEN6_CSUNIT_CLOCK_GATE_DISABLE, 0);
2057 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2058 0, GEN7_DOP_CLOCK_GATE_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00002059
Chris Wilson8f8b1172019-10-07 22:09:41 +01002060 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Braggd7965152016-11-07 19:49:52 +00002061}
2062
Chris Wilsona9877da2019-07-16 22:34:43 +01002063static u32 oa_config_flex_reg(const struct i915_oa_config *oa_config,
2064 i915_reg_t reg)
2065{
2066 u32 mmio = i915_mmio_reg_offset(reg);
2067 int i;
2068
2069 /*
2070 * This arbitrary default will select the 'EU FPU0 Pipeline
2071 * Active' event. In the future it's anticipated that there
2072 * will be an explicit 'No Event' we can select, but not yet...
2073 */
2074 if (!oa_config)
2075 return 0;
2076
2077 for (i = 0; i < oa_config->flex_regs_len; i++) {
2078 if (i915_mmio_reg_offset(oa_config->flex_regs[i].addr) == mmio)
2079 return oa_config->flex_regs[i].value;
2080 }
2081
2082 return 0;
2083}
Robert Bragg19f81df2017-06-13 12:23:03 +01002084/*
2085 * NB: It must always remain pointer safe to run this even if the OA unit
2086 * has been disabled.
2087 *
2088 * It's fine to put out-of-date values into these per-context registers
2089 * in the case that the OA unit has been disabled.
2090 */
Chris Wilsonb146e5e2019-03-06 08:47:04 +00002091static void
Chris Wilson7dc56af2019-09-24 15:59:50 +01002092gen8_update_reg_state_unlocked(const struct intel_context *ce,
2093 const struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002094{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002095 u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
2096 u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01002097 /* The MMIO offsets for Flex EU registers aren't contiguous */
Lionel Landwerlin35ab4fd2018-08-13 09:02:18 +01002098 i915_reg_t flex_regs[] = {
2099 EU_PERF_CNTL0,
2100 EU_PERF_CNTL1,
2101 EU_PERF_CNTL2,
2102 EU_PERF_CNTL3,
2103 EU_PERF_CNTL4,
2104 EU_PERF_CNTL5,
2105 EU_PERF_CNTL6,
Robert Bragg19f81df2017-06-13 12:23:03 +01002106 };
Chris Wilson7dc56af2019-09-24 15:59:50 +01002107 u32 *reg_state = ce->lrc_reg_state;
Robert Bragg19f81df2017-06-13 12:23:03 +01002108 int i;
2109
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002110 reg_state[ctx_oactxctrl + 1] =
2111 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2112 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2113 GEN8_OA_COUNTER_RESUME;
Robert Bragg19f81df2017-06-13 12:23:03 +01002114
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002115 for (i = 0; i < ARRAY_SIZE(flex_regs); i++)
Chris Wilson7dc56af2019-09-24 15:59:50 +01002116 reg_state[ctx_flexeu0 + i * 2 + 1] =
2117 oa_config_flex_reg(stream->oa_config, flex_regs[i]);
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002118
Chris Wilson8f8b1172019-10-07 22:09:41 +01002119 reg_state[CTX_R_PWR_CLK_STATE] =
2120 intel_sseu_make_rpcs(ce->engine->i915, &ce->sseu);
Robert Bragg19f81df2017-06-13 12:23:03 +01002121}
2122
Chris Wilsona9877da2019-07-16 22:34:43 +01002123struct flex {
2124 i915_reg_t reg;
2125 u32 offset;
2126 u32 value;
2127};
2128
2129static int
2130gen8_store_flex(struct i915_request *rq,
2131 struct intel_context *ce,
2132 const struct flex *flex, unsigned int count)
2133{
2134 u32 offset;
2135 u32 *cs;
2136
2137 cs = intel_ring_begin(rq, 4 * count);
2138 if (IS_ERR(cs))
2139 return PTR_ERR(cs);
2140
2141 offset = i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
2142 do {
2143 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
Chris Wilson7dc56af2019-09-24 15:59:50 +01002144 *cs++ = offset + flex->offset * sizeof(u32);
Chris Wilsona9877da2019-07-16 22:34:43 +01002145 *cs++ = 0;
2146 *cs++ = flex->value;
2147 } while (flex++, --count);
2148
2149 intel_ring_advance(rq, cs);
2150
2151 return 0;
2152}
2153
2154static int
2155gen8_load_flex(struct i915_request *rq,
2156 struct intel_context *ce,
2157 const struct flex *flex, unsigned int count)
2158{
2159 u32 *cs;
2160
2161 GEM_BUG_ON(!count || count > 63);
2162
2163 cs = intel_ring_begin(rq, 2 * count + 2);
2164 if (IS_ERR(cs))
2165 return PTR_ERR(cs);
2166
2167 *cs++ = MI_LOAD_REGISTER_IMM(count);
2168 do {
2169 *cs++ = i915_mmio_reg_offset(flex->reg);
2170 *cs++ = flex->value;
2171 } while (flex++, --count);
2172 *cs++ = MI_NOOP;
2173
2174 intel_ring_advance(rq, cs);
2175
2176 return 0;
2177}
2178
2179static int gen8_modify_context(struct intel_context *ce,
2180 const struct flex *flex, unsigned int count)
2181{
2182 struct i915_request *rq;
2183 int err;
2184
Chris Wilsonde5825b2019-11-25 10:58:56 +00002185 rq = intel_engine_create_kernel_request(ce->engine);
Chris Wilsona9877da2019-07-16 22:34:43 +01002186 if (IS_ERR(rq))
2187 return PTR_ERR(rq);
2188
2189 /* Serialise with the remote context */
2190 err = intel_context_prepare_remote_request(ce, rq);
2191 if (err == 0)
2192 err = gen8_store_flex(rq, ce, flex, count);
2193
2194 i915_request_add(rq);
2195 return err;
2196}
2197
2198static int gen8_modify_self(struct intel_context *ce,
2199 const struct flex *flex, unsigned int count)
2200{
2201 struct i915_request *rq;
2202 int err;
2203
Chris Wilsond236e2a2020-02-27 08:57:06 +00002204 intel_engine_pm_get(ce->engine);
Chris Wilsona9877da2019-07-16 22:34:43 +01002205 rq = i915_request_create(ce);
Chris Wilsond236e2a2020-02-27 08:57:06 +00002206 intel_engine_pm_put(ce->engine);
Chris Wilsona9877da2019-07-16 22:34:43 +01002207 if (IS_ERR(rq))
2208 return PTR_ERR(rq);
2209
2210 err = gen8_load_flex(rq, ce, flex, count);
2211
2212 i915_request_add(rq);
2213 return err;
2214}
2215
Chris Wilson5cca5032019-07-26 14:14:58 +01002216static int gen8_configure_context(struct i915_gem_context *ctx,
2217 struct flex *flex, unsigned int count)
2218{
2219 struct i915_gem_engines_iter it;
2220 struct intel_context *ce;
2221 int err = 0;
2222
2223 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
2224 GEM_BUG_ON(ce == ce->engine->kernel_context);
2225
2226 if (ce->engine->class != RENDER_CLASS)
2227 continue;
2228
Chris Wilsonfeed5c72020-01-09 08:51:42 +00002229 /* Otherwise OA settings will be set upon first use */
2230 if (!intel_context_pin_if_active(ce))
2231 continue;
Chris Wilson5cca5032019-07-26 14:14:58 +01002232
2233 flex->value = intel_sseu_make_rpcs(ctx->i915, &ce->sseu);
Chris Wilsonfeed5c72020-01-09 08:51:42 +00002234 err = gen8_modify_context(ce, flex, count);
Chris Wilson5cca5032019-07-26 14:14:58 +01002235
Chris Wilsonfeed5c72020-01-09 08:51:42 +00002236 intel_context_unpin(ce);
Chris Wilson5cca5032019-07-26 14:14:58 +01002237 if (err)
2238 break;
2239 }
2240 i915_gem_context_unlock_engines(ctx);
2241
2242 return err;
2243}
2244
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002245static int gen12_configure_oar_context(struct i915_perf_stream *stream, bool enable)
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002246{
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002247 int err;
2248 struct intel_context *ce = stream->pinned_ctx;
2249 u32 format = stream->oa_buffer.format;
2250 struct flex regs_context[] = {
2251 {
2252 GEN8_OACTXCONTROL,
2253 stream->perf->ctx_oactxctrl_offset + 1,
2254 enable ? GEN8_OA_COUNTER_RESUME : 0,
2255 },
2256 };
2257 /* Offsets in regs_lri are not used since this configuration is only
2258 * applied using LRI. Initialize the correct offsets for posterity.
2259 */
2260#define GEN12_OAR_OACONTROL_OFFSET 0x5B0
2261 struct flex regs_lri[] = {
2262 {
2263 GEN12_OAR_OACONTROL,
2264 GEN12_OAR_OACONTROL_OFFSET + 1,
2265 (format << GEN12_OAR_OACONTROL_COUNTER_FORMAT_SHIFT) |
2266 (enable ? GEN12_OAR_OACONTROL_COUNTER_ENABLE : 0)
2267 },
2268 {
2269 RING_CONTEXT_CONTROL(ce->engine->mmio_base),
2270 CTX_CONTEXT_CONTROL,
2271 _MASKED_FIELD(GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE,
2272 enable ?
2273 GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE :
2274 0)
2275 },
2276 };
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002277
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002278 /* Modify the context image of pinned context with regs_context*/
2279 err = intel_context_lock_pinned(ce);
2280 if (err)
2281 return err;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002282
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002283 err = gen8_modify_context(ce, regs_context, ARRAY_SIZE(regs_context));
2284 intel_context_unlock_pinned(ce);
2285 if (err)
2286 return err;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002287
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002288 /* Apply regs_lri using LRI with pinned context */
2289 return gen8_modify_self(ce, regs_lri, ARRAY_SIZE(regs_lri));
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002290}
2291
Robert Bragg19f81df2017-06-13 12:23:03 +01002292/*
Robert Bragg19f81df2017-06-13 12:23:03 +01002293 * Manages updating the per-context aspects of the OA stream
2294 * configuration across all contexts.
2295 *
2296 * The awkward consideration here is that OACTXCONTROL controls the
2297 * exponent for periodic sampling which is primarily used for system
2298 * wide profiling where we'd like a consistent sampling period even in
2299 * the face of context switches.
2300 *
2301 * Our approach of updating the register state context (as opposed to
2302 * say using a workaround batch buffer) ensures that the hardware
2303 * won't automatically reload an out-of-date timer exponent even
2304 * transiently before a WA BB could be parsed.
2305 *
2306 * This function needs to:
2307 * - Ensure the currently running context's per-context OA state is
2308 * updated
2309 * - Ensure that all existing contexts will have the correct per-context
2310 * OA state if they are scheduled for use.
2311 * - Ensure any new contexts will be initialized with the correct
2312 * per-context OA state.
2313 *
2314 * Note: it's only the RCS/Render context that has any OA state.
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002315 * Note: the first flex register passed must always be R_PWR_CLK_STATE
Robert Bragg19f81df2017-06-13 12:23:03 +01002316 */
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002317static int oa_configure_all_contexts(struct i915_perf_stream *stream,
2318 struct flex *regs,
2319 size_t num_regs)
Robert Bragg19f81df2017-06-13 12:23:03 +01002320{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002321 struct drm_i915_private *i915 = stream->perf->i915;
Chris Wilsona9877da2019-07-16 22:34:43 +01002322 struct intel_engine_cs *engine;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002323 struct i915_gem_context *ctx, *cn;
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002324 int err;
Chris Wilsona9877da2019-07-16 22:34:43 +01002325
Chris Wilsona4c969d2019-10-07 22:09:42 +01002326 lockdep_assert_held(&stream->perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01002327
Robert Bragg19f81df2017-06-13 12:23:03 +01002328 /*
2329 * The OA register config is setup through the context image. This image
2330 * might be written to by the GPU on context switch (in particular on
2331 * lite-restore). This means we can't safely update a context's image,
2332 * if this context is scheduled/submitted to run on the GPU.
2333 *
2334 * We could emit the OA register config through the batch buffer but
2335 * this might leave small interval of time where the OA unit is
2336 * configured at an invalid sampling period.
2337 *
Chris Wilsona9877da2019-07-16 22:34:43 +01002338 * Note that since we emit all requests from a single ring, there
2339 * is still an implicit global barrier here that may cause a high
2340 * priority context to wait for an otherwise independent low priority
2341 * context. Contexts idle at the time of reconfiguration are not
2342 * trapped behind the barrier.
Robert Bragg19f81df2017-06-13 12:23:03 +01002343 */
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002344 spin_lock(&i915->gem.contexts.lock);
2345 list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002346 if (!kref_get_unless_zero(&ctx->ref))
2347 continue;
2348
2349 spin_unlock(&i915->gem.contexts.lock);
2350
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002351 err = gen8_configure_context(ctx, regs, num_regs);
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002352 if (err) {
2353 i915_gem_context_put(ctx);
Chris Wilsona9877da2019-07-16 22:34:43 +01002354 return err;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002355 }
2356
2357 spin_lock(&i915->gem.contexts.lock);
2358 list_safe_reset_next(ctx, cn, link);
2359 i915_gem_context_put(ctx);
Robert Bragg19f81df2017-06-13 12:23:03 +01002360 }
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002361 spin_unlock(&i915->gem.contexts.lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01002362
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002363 /*
Chris Wilsona9877da2019-07-16 22:34:43 +01002364 * After updating all other contexts, we need to modify ourselves.
2365 * If we don't modify the kernel_context, we do not get events while
2366 * idle.
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002367 */
Chris Wilson750e76b2019-08-06 13:43:00 +01002368 for_each_uabi_engine(engine, i915) {
Chris Wilsona9877da2019-07-16 22:34:43 +01002369 struct intel_context *ce = engine->kernel_context;
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002370
Chris Wilsona9877da2019-07-16 22:34:43 +01002371 if (engine->class != RENDER_CLASS)
2372 continue;
2373
2374 regs[0].value = intel_sseu_make_rpcs(i915, &ce->sseu);
2375
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002376 err = gen8_modify_self(ce, regs, num_regs);
Chris Wilsona9877da2019-07-16 22:34:43 +01002377 if (err)
2378 return err;
2379 }
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002380
2381 return 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01002382}
2383
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002384static int gen12_configure_all_contexts(struct i915_perf_stream *stream,
2385 const struct i915_oa_config *oa_config)
2386{
2387 struct flex regs[] = {
2388 {
2389 GEN8_R_PWR_CLK_STATE,
2390 CTX_R_PWR_CLK_STATE,
2391 },
2392 };
2393
2394 return oa_configure_all_contexts(stream, regs, ARRAY_SIZE(regs));
2395}
2396
2397static int lrc_configure_all_contexts(struct i915_perf_stream *stream,
2398 const struct i915_oa_config *oa_config)
2399{
2400 /* The MMIO offsets for Flex EU registers aren't contiguous */
2401 const u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
2402#define ctx_flexeuN(N) (ctx_flexeu0 + 2 * (N) + 1)
2403 struct flex regs[] = {
2404 {
2405 GEN8_R_PWR_CLK_STATE,
2406 CTX_R_PWR_CLK_STATE,
2407 },
2408 {
2409 GEN8_OACTXCONTROL,
2410 stream->perf->ctx_oactxctrl_offset + 1,
2411 },
2412 { EU_PERF_CNTL0, ctx_flexeuN(0) },
2413 { EU_PERF_CNTL1, ctx_flexeuN(1) },
2414 { EU_PERF_CNTL2, ctx_flexeuN(2) },
2415 { EU_PERF_CNTL3, ctx_flexeuN(3) },
2416 { EU_PERF_CNTL4, ctx_flexeuN(4) },
2417 { EU_PERF_CNTL5, ctx_flexeuN(5) },
2418 { EU_PERF_CNTL6, ctx_flexeuN(6) },
2419 };
2420#undef ctx_flexeuN
2421 int i;
2422
2423 regs[1].value =
2424 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2425 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2426 GEN8_OA_COUNTER_RESUME;
2427
2428 for (i = 2; i < ARRAY_SIZE(regs); i++)
2429 regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
2430
2431 return oa_configure_all_contexts(stream, regs, ARRAY_SIZE(regs));
2432}
2433
Chris Wilson4b4e9732020-03-02 08:57:57 +00002434static struct i915_request *
2435gen8_enable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002436{
Chris Wilson52111c42019-10-10 16:05:20 +01002437 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03002438 struct i915_oa_config *oa_config = stream->oa_config;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002439 int ret;
Robert Bragg19f81df2017-06-13 12:23:03 +01002440
2441 /*
2442 * We disable slice/unslice clock ratio change reports on SKL since
2443 * they are too noisy. The HW generates a lot of redundant reports
2444 * where the ratio hasn't really changed causing a lot of redundant
2445 * work to processes and increasing the chances we'll hit buffer
2446 * overruns.
2447 *
2448 * Although we don't currently use the 'disable overrun' OABUFFER
2449 * feature it's worth noting that clock ratio reports have to be
2450 * disabled before considering to use that feature since the HW doesn't
2451 * correctly block these reports.
2452 *
2453 * Currently none of the high-level metrics we have depend on knowing
2454 * this ratio to normalize.
2455 *
2456 * Note: This register is not power context saved and restored, but
2457 * that's OK considering that we disable RC6 while the OA unit is
2458 * enabled.
2459 *
2460 * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
2461 * be read back from automatically triggered reports, as part of the
2462 * RPT_ID field.
2463 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002464 if (IS_GEN_RANGE(stream->perf->i915, 9, 11)) {
2465 intel_uncore_write(uncore, GEN8_OA_DEBUG,
2466 _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2467 GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
Robert Bragg19f81df2017-06-13 12:23:03 +01002468 }
2469
2470 /*
2471 * Update all contexts prior writing the mux configurations as we need
2472 * to make sure all slices/subslices are ON before writing to NOA
2473 * registers.
2474 */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002475 ret = lrc_configure_all_contexts(stream, oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01002476 if (ret)
Chris Wilson4b4e9732020-03-02 08:57:57 +00002477 return ERR_PTR(ret);
Robert Bragg19f81df2017-06-13 12:23:03 +01002478
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03002479 return emit_oa_config(stream, oa_config, oa_context(stream));
Robert Bragg19f81df2017-06-13 12:23:03 +01002480}
2481
Chris Wilson9278bbb2019-11-01 19:21:16 +00002482static u32 oag_report_ctx_switches(const struct i915_perf_stream *stream)
2483{
2484 return _MASKED_FIELD(GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS,
2485 (stream->sample_flags & SAMPLE_OA_REPORT) ?
2486 0 : GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS);
2487}
2488
Chris Wilson4b4e9732020-03-02 08:57:57 +00002489static struct i915_request *
2490gen12_enable_metric_set(struct i915_perf_stream *stream)
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002491{
2492 struct intel_uncore *uncore = stream->uncore;
2493 struct i915_oa_config *oa_config = stream->oa_config;
2494 bool periodic = stream->periodic;
2495 u32 period_exponent = stream->period_exponent;
2496 int ret;
2497
2498 intel_uncore_write(uncore, GEN12_OAG_OA_DEBUG,
2499 /* Disable clk ratio reports, like previous Gens. */
2500 _MASKED_BIT_ENABLE(GEN12_OAG_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2501 GEN12_OAG_OA_DEBUG_INCLUDE_CLK_RATIO) |
2502 /*
Chris Wilson9278bbb2019-11-01 19:21:16 +00002503 * If the user didn't require OA reports, instruct
2504 * the hardware not to emit ctx switch reports.
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002505 */
Chris Wilson9278bbb2019-11-01 19:21:16 +00002506 oag_report_ctx_switches(stream));
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002507
2508 intel_uncore_write(uncore, GEN12_OAG_OAGLBCTXCTRL, periodic ?
2509 (GEN12_OAG_OAGLBCTXCTRL_COUNTER_RESUME |
2510 GEN12_OAG_OAGLBCTXCTRL_TIMER_ENABLE |
2511 (period_exponent << GEN12_OAG_OAGLBCTXCTRL_TIMER_PERIOD_SHIFT))
2512 : 0);
2513
2514 /*
2515 * Update all contexts prior writing the mux configurations as we need
2516 * to make sure all slices/subslices are ON before writing to NOA
2517 * registers.
2518 */
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002519 ret = gen12_configure_all_contexts(stream, oa_config);
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002520 if (ret)
Chris Wilson4b4e9732020-03-02 08:57:57 +00002521 return ERR_PTR(ret);
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002522
2523 /*
2524 * For Gen12, performance counters are context
2525 * saved/restored. Only enable it for the context that
2526 * requested this.
2527 */
2528 if (stream->ctx) {
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002529 ret = gen12_configure_oar_context(stream, true);
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002530 if (ret)
Chris Wilson4b4e9732020-03-02 08:57:57 +00002531 return ERR_PTR(ret);
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002532 }
2533
2534 return emit_oa_config(stream, oa_config, oa_context(stream));
2535}
2536
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002537static void gen8_disable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002538{
Chris Wilson52111c42019-10-10 16:05:20 +01002539 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002540
Robert Bragg19f81df2017-06-13 12:23:03 +01002541 /* Reset all contexts' slices/subslices configurations. */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002542 lrc_configure_all_contexts(stream, NULL);
Lionel Landwerlin28964cf2017-08-03 17:58:10 +01002543
Chris Wilson8f8b1172019-10-07 22:09:41 +01002544 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01002545}
2546
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002547static void gen10_disable_metric_set(struct i915_perf_stream *stream)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002548{
Chris Wilson52111c42019-10-10 16:05:20 +01002549 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002550
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002551 /* Reset all contexts' slices/subslices configurations. */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002552 lrc_configure_all_contexts(stream, NULL);
2553
2554 /* Make sure we disable noa to save power. */
2555 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
2556}
2557
2558static void gen12_disable_metric_set(struct i915_perf_stream *stream)
2559{
2560 struct intel_uncore *uncore = stream->uncore;
2561
2562 /* Reset all contexts' slices/subslices configurations. */
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002563 gen12_configure_all_contexts(stream, NULL);
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002564
2565 /* disable the context save/restore or OAR counters */
2566 if (stream->ctx)
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002567 gen12_configure_oar_context(stream, false);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002568
2569 /* Make sure we disable noa to save power. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002570 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002571}
2572
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002573static void gen7_oa_enable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002574{
Chris Wilson52111c42019-10-10 16:05:20 +01002575 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002576 struct i915_gem_context *ctx = stream->ctx;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002577 u32 ctx_id = stream->specific_ctx_id;
2578 bool periodic = stream->periodic;
2579 u32 period_exponent = stream->period_exponent;
2580 u32 report_format = stream->oa_buffer.format;
Lionel Landwerlin11051302018-03-26 10:08:23 +01002581
Robert Bragg1bef3402017-06-13 12:23:06 +01002582 /*
2583 * Reset buf pointers so we don't forward reports from before now.
2584 *
2585 * Think carefully if considering trying to avoid this, since it
2586 * also ensures status flags and the buffer itself are cleared
2587 * in error paths, and we have checks for invalid reports based
2588 * on the assumption that certain fields are written to zeroed
2589 * memory which this helps maintains.
2590 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002591 gen7_init_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002592
Chris Wilson8f8b1172019-10-07 22:09:41 +01002593 intel_uncore_write(uncore, GEN7_OACONTROL,
2594 (ctx_id & GEN7_OACONTROL_CTX_MASK) |
2595 (period_exponent <<
2596 GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
2597 (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
2598 (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
2599 (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
2600 GEN7_OACONTROL_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00002601}
2602
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002603static void gen8_oa_enable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002604{
Chris Wilson52111c42019-10-10 16:05:20 +01002605 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002606 u32 report_format = stream->oa_buffer.format;
Robert Bragg19f81df2017-06-13 12:23:03 +01002607
2608 /*
2609 * Reset buf pointers so we don't forward reports from before now.
2610 *
2611 * Think carefully if considering trying to avoid this, since it
2612 * also ensures status flags and the buffer itself are cleared
2613 * in error paths, and we have checks for invalid reports based
2614 * on the assumption that certain fields are written to zeroed
2615 * memory which this helps maintains.
2616 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002617 gen8_init_oa_buffer(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002618
2619 /*
2620 * Note: we don't rely on the hardware to perform single context
2621 * filtering and instead filter on the cpu based on the context-id
2622 * field of reports
2623 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002624 intel_uncore_write(uncore, GEN8_OACONTROL,
2625 (report_format << GEN8_OA_REPORT_FORMAT_SHIFT) |
2626 GEN8_OA_COUNTER_ENABLE);
Robert Bragg19f81df2017-06-13 12:23:03 +01002627}
2628
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002629static void gen12_oa_enable(struct i915_perf_stream *stream)
2630{
2631 struct intel_uncore *uncore = stream->uncore;
2632 u32 report_format = stream->oa_buffer.format;
2633
2634 /*
2635 * If we don't want OA reports from the OA buffer, then we don't even
2636 * need to program the OAG unit.
2637 */
2638 if (!(stream->sample_flags & SAMPLE_OA_REPORT))
2639 return;
2640
2641 gen12_init_oa_buffer(stream);
2642
2643 intel_uncore_write(uncore, GEN12_OAG_OACONTROL,
2644 (report_format << GEN12_OAG_OACONTROL_OA_COUNTER_FORMAT_SHIFT) |
2645 GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE);
2646}
2647
Robert Bragg16d98b32016-12-07 21:40:33 +00002648/**
2649 * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
2650 * @stream: An i915 perf stream opened for OA metrics
2651 *
2652 * [Re]enables hardware periodic sampling according to the period configured
2653 * when opening the stream. This also starts a hrtimer that will periodically
2654 * check for data in the circular OA buffer for notifying userspace (e.g.
2655 * during a read() or poll()).
2656 */
Robert Braggd7965152016-11-07 19:49:52 +00002657static void i915_oa_stream_enable(struct i915_perf_stream *stream)
2658{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002659 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002660
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002661 if (stream->periodic)
2662 hrtimer_start(&stream->poll_check_timer,
Robert Braggd7965152016-11-07 19:49:52 +00002663 ns_to_ktime(POLL_PERIOD),
2664 HRTIMER_MODE_REL_PINNED);
2665}
2666
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002667static void gen7_oa_disable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002668{
Chris Wilson52111c42019-10-10 16:05:20 +01002669 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002670
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002671 intel_uncore_write(uncore, GEN7_OACONTROL, 0);
2672 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002673 GEN7_OACONTROL, GEN7_OACONTROL_ENABLE, 0,
2674 50))
Wambui Karuga0bf85732020-02-18 20:39:36 +03002675 drm_err(&stream->perf->i915->drm,
2676 "wait for OA to be disabled timed out\n");
Robert Braggd7965152016-11-07 19:49:52 +00002677}
2678
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002679static void gen8_oa_disable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002680{
Chris Wilson52111c42019-10-10 16:05:20 +01002681 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002682
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002683 intel_uncore_write(uncore, GEN8_OACONTROL, 0);
2684 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002685 GEN8_OACONTROL, GEN8_OA_COUNTER_ENABLE, 0,
2686 50))
Wambui Karuga0bf85732020-02-18 20:39:36 +03002687 drm_err(&stream->perf->i915->drm,
2688 "wait for OA to be disabled timed out\n");
Robert Bragg19f81df2017-06-13 12:23:03 +01002689}
2690
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002691static void gen12_oa_disable(struct i915_perf_stream *stream)
2692{
2693 struct intel_uncore *uncore = stream->uncore;
2694
2695 intel_uncore_write(uncore, GEN12_OAG_OACONTROL, 0);
2696 if (intel_wait_for_register(uncore,
2697 GEN12_OAG_OACONTROL,
2698 GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE, 0,
2699 50))
Wambui Karuga0bf85732020-02-18 20:39:36 +03002700 drm_err(&stream->perf->i915->drm,
2701 "wait for OA to be disabled timed out\n");
Umesh Nerlige Ramappac06aa1b2020-03-09 14:10:57 -07002702
2703 intel_uncore_write(uncore, GEN12_OA_TLB_INV_CR, 1);
2704 if (intel_wait_for_register(uncore,
2705 GEN12_OA_TLB_INV_CR,
2706 1, 0,
2707 50))
2708 drm_err(&stream->perf->i915->drm,
2709 "wait for OA tlb invalidate timed out\n");
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002710}
2711
Robert Bragg16d98b32016-12-07 21:40:33 +00002712/**
2713 * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
2714 * @stream: An i915 perf stream opened for OA metrics
2715 *
2716 * Stops the OA unit from periodically writing counter reports into the
2717 * circular OA buffer. This also stops the hrtimer that periodically checks for
2718 * data in the circular OA buffer, for notifying userspace.
2719 */
Robert Braggd7965152016-11-07 19:49:52 +00002720static void i915_oa_stream_disable(struct i915_perf_stream *stream)
2721{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002722 stream->perf->ops.oa_disable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002723
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002724 if (stream->periodic)
2725 hrtimer_cancel(&stream->poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00002726}
2727
Robert Braggd7965152016-11-07 19:49:52 +00002728static const struct i915_perf_stream_ops i915_oa_stream_ops = {
2729 .destroy = i915_oa_stream_destroy,
2730 .enable = i915_oa_stream_enable,
2731 .disable = i915_oa_stream_disable,
2732 .wait_unlocked = i915_oa_wait_unlocked,
2733 .poll_wait = i915_oa_poll_wait,
2734 .read = i915_oa_read,
2735};
2736
Chris Wilson4b4e9732020-03-02 08:57:57 +00002737static int i915_perf_stream_enable_sync(struct i915_perf_stream *stream)
2738{
2739 struct i915_request *rq;
2740
2741 rq = stream->perf->ops.enable_metric_set(stream);
2742 if (IS_ERR(rq))
2743 return PTR_ERR(rq);
2744
2745 i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT);
2746 i915_request_put(rq);
2747
2748 return 0;
2749}
2750
Robert Bragg16d98b32016-12-07 21:40:33 +00002751/**
2752 * i915_oa_stream_init - validate combined props for OA stream and init
2753 * @stream: An i915 perf stream
2754 * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
2755 * @props: The property state that configures stream (individually validated)
2756 *
2757 * While read_properties_unlocked() validates properties in isolation it
2758 * doesn't ensure that the combination necessarily makes sense.
2759 *
2760 * At this point it has been determined that userspace wants a stream of
2761 * OA metrics, but still we need to further validate the combined
2762 * properties are OK.
2763 *
2764 * If the configuration makes sense then we can allocate memory for
2765 * a circular OA buffer and apply the requested metric set configuration.
2766 *
2767 * Returns: zero on success or a negative error code.
2768 */
Robert Braggd7965152016-11-07 19:49:52 +00002769static int i915_oa_stream_init(struct i915_perf_stream *stream,
2770 struct drm_i915_perf_open_param *param,
2771 struct perf_open_properties *props)
2772{
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +05302773 struct drm_i915_private *i915 = stream->perf->i915;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002774 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00002775 int format_size;
2776 int ret;
2777
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002778 if (!props->engine) {
2779 DRM_DEBUG("OA engine not specified\n");
2780 return -EINVAL;
2781 }
2782
2783 /*
2784 * If the sysfs metrics/ directory wasn't registered for some
Robert Bragg442b8c02016-11-07 19:49:53 +00002785 * reason then don't let userspace try their luck with config
2786 * IDs
2787 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002788 if (!perf->metrics_kobj) {
Robert Bragg77085502016-12-01 17:21:52 +00002789 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
Robert Bragg442b8c02016-11-07 19:49:53 +00002790 return -EINVAL;
2791 }
2792
Umesh Nerlige Ramappa322d56a2019-12-06 11:43:38 -08002793 if (!(props->sample_flags & SAMPLE_OA_REPORT) &&
2794 (INTEL_GEN(perf->i915) < 12 || !stream->ctx)) {
Robert Bragg77085502016-12-01 17:21:52 +00002795 DRM_DEBUG("Only OA report sampling supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002796 return -EINVAL;
2797 }
2798
Chris Wilson8f8b1172019-10-07 22:09:41 +01002799 if (!perf->ops.enable_metric_set) {
Robert Bragg77085502016-12-01 17:21:52 +00002800 DRM_DEBUG("OA unit not supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002801 return -ENODEV;
2802 }
2803
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002804 /*
2805 * To avoid the complexity of having to accurately filter
Robert Braggd7965152016-11-07 19:49:52 +00002806 * counter reports and marshal to the appropriate client
2807 * we currently only allow exclusive access
2808 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002809 if (perf->exclusive_stream) {
Robert Bragg77085502016-12-01 17:21:52 +00002810 DRM_DEBUG("OA unit already in use\n");
Robert Braggd7965152016-11-07 19:49:52 +00002811 return -EBUSY;
2812 }
2813
Robert Braggd7965152016-11-07 19:49:52 +00002814 if (!props->oa_format) {
Robert Bragg77085502016-12-01 17:21:52 +00002815 DRM_DEBUG("OA report format not specified\n");
Robert Braggd7965152016-11-07 19:49:52 +00002816 return -EINVAL;
2817 }
2818
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002819 stream->engine = props->engine;
Chris Wilson52111c42019-10-10 16:05:20 +01002820 stream->uncore = stream->engine->gt->uncore;
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002821
Robert Braggd7965152016-11-07 19:49:52 +00002822 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
2823
Chris Wilson8f8b1172019-10-07 22:09:41 +01002824 format_size = perf->oa_formats[props->oa_format].size;
Robert Braggd7965152016-11-07 19:49:52 +00002825
Umesh Nerlige Ramappa322d56a2019-12-06 11:43:38 -08002826 stream->sample_flags = props->sample_flags;
Robert Braggd7965152016-11-07 19:49:52 +00002827 stream->sample_size += format_size;
2828
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002829 stream->oa_buffer.format_size = format_size;
Pankaj Bharadiyaa9f236d2020-01-15 09:14:54 +05302830 if (drm_WARN_ON(&i915->drm, stream->oa_buffer.format_size == 0))
Robert Braggd7965152016-11-07 19:49:52 +00002831 return -EINVAL;
2832
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01002833 stream->hold_preemption = props->hold_preemption;
2834
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002835 stream->oa_buffer.format =
Chris Wilson8f8b1172019-10-07 22:09:41 +01002836 perf->oa_formats[props->oa_format].format;
Robert Braggd7965152016-11-07 19:49:52 +00002837
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002838 stream->periodic = props->oa_periodic;
2839 if (stream->periodic)
2840 stream->period_exponent = props->oa_period_exponent;
Robert Braggd7965152016-11-07 19:49:52 +00002841
Robert Braggd7965152016-11-07 19:49:52 +00002842 if (stream->ctx) {
2843 ret = oa_get_render_ctx_id(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002844 if (ret) {
2845 DRM_DEBUG("Invalid context id to filter with\n");
Robert Braggd7965152016-11-07 19:49:52 +00002846 return ret;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002847 }
Robert Braggd7965152016-11-07 19:49:52 +00002848 }
2849
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01002850 ret = alloc_noa_wait(stream);
2851 if (ret) {
2852 DRM_DEBUG("Unable to allocate NOA wait batch buffer\n");
2853 goto err_noa_wait_alloc;
2854 }
2855
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002856 stream->oa_config = i915_perf_get_oa_config(perf, props->metrics_set);
2857 if (!stream->oa_config) {
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002858 DRM_DEBUG("Invalid OA config id=%i\n", props->metrics_set);
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002859 ret = -EINVAL;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002860 goto err_config;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002861 }
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002862
Robert Braggd7965152016-11-07 19:49:52 +00002863 /* PRM - observability performance counters:
2864 *
2865 * OACONTROL, performance counter enable, note:
2866 *
2867 * "When this bit is set, in order to have coherent counts,
2868 * RC6 power state and trunk clock gating must be disabled.
2869 * This can be achieved by programming MMIO registers as
2870 * 0xA094=0 and 0xA090[31]=1"
2871 *
2872 * In our case we are expecting that taking pm + FORCEWAKE
2873 * references will effectively disable RC6.
2874 */
Chris Wilsona5efcde2019-10-11 20:03:17 +01002875 intel_engine_pm_get(stream->engine);
Chris Wilson52111c42019-10-10 16:05:20 +01002876 intel_uncore_forcewake_get(stream->uncore, FORCEWAKE_ALL);
Robert Braggd7965152016-11-07 19:49:52 +00002877
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002878 ret = alloc_oa_buffer(stream);
sagar.a.kamble@intel.com987f8c42017-06-27 23:09:41 +05302879 if (ret)
2880 goto err_oa_buf_alloc;
2881
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002882 stream->ops = &i915_oa_stream_ops;
Chris Wilsona5af0812020-02-27 08:57:05 +00002883 WRITE_ONCE(perf->exclusive_stream, stream);
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002884
Chris Wilson4b4e9732020-03-02 08:57:57 +00002885 ret = i915_perf_stream_enable_sync(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002886 if (ret) {
2887 DRM_DEBUG("Unable to enable metric set\n");
Robert Braggd7965152016-11-07 19:49:52 +00002888 goto err_enable;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002889 }
Robert Braggd7965152016-11-07 19:49:52 +00002890
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002891 DRM_DEBUG("opening stream oa config uuid=%s\n",
2892 stream->oa_config->uuid);
2893
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002894 hrtimer_init(&stream->poll_check_timer,
2895 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2896 stream->poll_check_timer.function = oa_poll_check_timer_cb;
2897 init_waitqueue_head(&stream->poll_wq);
2898 spin_lock_init(&stream->oa_buffer.ptr_lock);
2899
Robert Braggd7965152016-11-07 19:49:52 +00002900 return 0;
2901
2902err_enable:
Chris Wilsona5af0812020-02-27 08:57:05 +00002903 WRITE_ONCE(perf->exclusive_stream, NULL);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002904 perf->ops.disable_metric_set(stream);
Lionel Landwerlin41d3fdc2018-03-01 11:06:13 +00002905
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002906 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002907
2908err_oa_buf_alloc:
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002909 free_oa_configs(stream);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002910
Chris Wilson52111c42019-10-10 16:05:20 +01002911 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
Chris Wilsona5efcde2019-10-11 20:03:17 +01002912 intel_engine_pm_put(stream->engine);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002913
2914err_config:
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01002915 free_noa_wait(stream);
2916
2917err_noa_wait_alloc:
Robert Braggd7965152016-11-07 19:49:52 +00002918 if (stream->ctx)
2919 oa_put_render_ctx_id(stream);
2920
2921 return ret;
2922}
2923
Chris Wilson7dc56af2019-09-24 15:59:50 +01002924void i915_oa_init_reg_state(const struct intel_context *ce,
2925 const struct intel_engine_cs *engine)
Robert Bragg19f81df2017-06-13 12:23:03 +01002926{
Chris Wilson28b6cb02017-08-10 18:57:43 +01002927 struct i915_perf_stream *stream;
Robert Bragg19f81df2017-06-13 12:23:03 +01002928
Chris Wilson8a68d462019-03-05 18:03:30 +00002929 if (engine->class != RENDER_CLASS)
Robert Bragg19f81df2017-06-13 12:23:03 +01002930 return;
2931
Chris Wilsona5af0812020-02-27 08:57:05 +00002932 /* perf.exclusive_stream serialised by lrc_configure_all_contexts() */
2933 stream = READ_ONCE(engine->i915->perf.exclusive_stream);
Umesh Nerlige Ramappaccdeed42019-12-06 11:43:39 -08002934 /*
2935 * For gen12, only CTX_R_PWR_CLK_STATE needs update, but the caller
2936 * is already doing that, so nothing to be done for gen12 here.
2937 */
2938 if (stream && INTEL_GEN(stream->perf->i915) < 12)
Chris Wilson7dc56af2019-09-24 15:59:50 +01002939 gen8_update_reg_state_unlocked(ce, stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002940}
2941
Robert Bragg16d98b32016-12-07 21:40:33 +00002942/**
Robert Bragg16d98b32016-12-07 21:40:33 +00002943 * i915_perf_read - handles read() FOP for i915 perf stream FDs
2944 * @file: An i915 perf stream file
2945 * @buf: destination buffer given by userspace
2946 * @count: the number of bytes userspace wants to read
2947 * @ppos: (inout) file seek position (unused)
2948 *
2949 * The entry point for handling a read() on a stream file descriptor from
2950 * userspace. Most of the work is left to the i915_perf_read_locked() and
2951 * &i915_perf_stream_ops->read but to save having stream implementations (of
2952 * which we might have multiple later) we handle blocking read here.
2953 *
2954 * We can also consistently treat trying to read from a disabled stream
2955 * as an IO error so implementations can assume the stream is enabled
2956 * while reading.
2957 *
2958 * Returns: The number of bytes copied or a negative error code on failure.
2959 */
Robert Braggeec688e2016-11-07 19:49:47 +00002960static ssize_t i915_perf_read(struct file *file,
2961 char __user *buf,
2962 size_t count,
2963 loff_t *ppos)
2964{
2965 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002966 struct i915_perf *perf = stream->perf;
Ashutosh Dixitbcad5882020-04-08 16:42:01 -07002967 size_t offset = 0;
2968 int ret;
Robert Braggeec688e2016-11-07 19:49:47 +00002969
Robert Braggd7965152016-11-07 19:49:52 +00002970 /* To ensure it's handled consistently we simply treat all reads of a
2971 * disabled stream as an error. In particular it might otherwise lead
2972 * to a deadlock for blocking file descriptors...
2973 */
2974 if (!stream->enabled)
2975 return -EIO;
2976
Robert Braggeec688e2016-11-07 19:49:47 +00002977 if (!(file->f_flags & O_NONBLOCK)) {
Robert Braggd7965152016-11-07 19:49:52 +00002978 /* There's the small chance of false positives from
2979 * stream->ops->wait_unlocked.
2980 *
2981 * E.g. with single context filtering since we only wait until
2982 * oabuffer has >= 1 report we don't immediately know whether
2983 * any reports really belong to the current context
Robert Braggeec688e2016-11-07 19:49:47 +00002984 */
2985 do {
2986 ret = stream->ops->wait_unlocked(stream);
2987 if (ret)
2988 return ret;
2989
Chris Wilson8f8b1172019-10-07 22:09:41 +01002990 mutex_lock(&perf->lock);
Ashutosh Dixitbcad5882020-04-08 16:42:01 -07002991 ret = stream->ops->read(stream, buf, count, &offset);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002992 mutex_unlock(&perf->lock);
Ashutosh Dixitbcad5882020-04-08 16:42:01 -07002993 } while (!offset && !ret);
Robert Braggeec688e2016-11-07 19:49:47 +00002994 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01002995 mutex_lock(&perf->lock);
Ashutosh Dixitbcad5882020-04-08 16:42:01 -07002996 ret = stream->ops->read(stream, buf, count, &offset);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002997 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002998 }
2999
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003000 /* We allow the poll checking to sometimes report false positive EPOLLIN
Robert Bragg26ebd9c2017-05-11 16:43:25 +01003001 * events where we might actually report EAGAIN on read() if there's
3002 * not really any data available. In this situation though we don't
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003003 * want to enter a busy loop between poll() reporting a EPOLLIN event
Robert Bragg26ebd9c2017-05-11 16:43:25 +01003004 * and read() returning -EAGAIN. Clearing the oa.pollin state here
3005 * effectively ensures we back off until the next hrtimer callback
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003006 * before reporting another EPOLLIN event.
Ashutosh Dixitbcad5882020-04-08 16:42:01 -07003007 * The exception to this is if ops->read() returned -ENOSPC which means
3008 * that more OA data is available than could fit in the user provided
3009 * buffer. In this case we want the next poll() call to not block.
Robert Bragg26ebd9c2017-05-11 16:43:25 +01003010 */
Ashutosh Dixitbcad5882020-04-08 16:42:01 -07003011 if (ret != -ENOSPC)
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003012 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00003013
Ashutosh Dixitbcad5882020-04-08 16:42:01 -07003014 /* Possible values for ret are 0, -EFAULT, -ENOSPC, -EIO, ... */
3015 return offset ?: (ret ?: -EAGAIN);
Robert Braggeec688e2016-11-07 19:49:47 +00003016}
3017
Robert Braggd7965152016-11-07 19:49:52 +00003018static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
3019{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003020 struct i915_perf_stream *stream =
3021 container_of(hrtimer, typeof(*stream), poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00003022
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003023 if (oa_buffer_check_unlocked(stream)) {
3024 stream->pollin = true;
3025 wake_up(&stream->poll_wq);
Robert Braggd7965152016-11-07 19:49:52 +00003026 }
3027
3028 hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
3029
3030 return HRTIMER_RESTART;
3031}
3032
Robert Bragg16d98b32016-12-07 21:40:33 +00003033/**
3034 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
Robert Bragg16d98b32016-12-07 21:40:33 +00003035 * @stream: An i915 perf stream
3036 * @file: An i915 perf stream file
3037 * @wait: poll() state table
3038 *
3039 * For handling userspace polling on an i915 perf stream, this calls through to
3040 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
3041 * will be woken for new stream data.
3042 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003043 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00003044 * with any non-file-operation driver hooks.
3045 *
3046 * Returns: any poll events that are ready without sleeping
3047 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003048static __poll_t i915_perf_poll_locked(struct i915_perf_stream *stream,
3049 struct file *file,
3050 poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00003051{
Al Viroafc9a422017-07-03 06:39:46 -04003052 __poll_t events = 0;
Robert Braggeec688e2016-11-07 19:49:47 +00003053
3054 stream->ops->poll_wait(stream, file, wait);
3055
Robert Braggd7965152016-11-07 19:49:52 +00003056 /* Note: we don't explicitly check whether there's something to read
3057 * here since this path may be very hot depending on what else
3058 * userspace is polling, or on the timeout in use. We rely solely on
3059 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
3060 * samples to read.
3061 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003062 if (stream->pollin)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003063 events |= EPOLLIN;
Robert Braggeec688e2016-11-07 19:49:47 +00003064
Robert Braggd7965152016-11-07 19:49:52 +00003065 return events;
Robert Braggeec688e2016-11-07 19:49:47 +00003066}
3067
Robert Bragg16d98b32016-12-07 21:40:33 +00003068/**
3069 * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
3070 * @file: An i915 perf stream file
3071 * @wait: poll() state table
3072 *
3073 * For handling userspace polling on an i915 perf stream, this ensures
3074 * poll_wait() gets called with a wait queue that will be woken for new stream
3075 * data.
3076 *
3077 * Note: Implementation deferred to i915_perf_poll_locked()
3078 *
3079 * Returns: any poll events that are ready without sleeping
3080 */
Al Viroafc9a422017-07-03 06:39:46 -04003081static __poll_t i915_perf_poll(struct file *file, poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00003082{
3083 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003084 struct i915_perf *perf = stream->perf;
Al Viroafc9a422017-07-03 06:39:46 -04003085 __poll_t ret;
Robert Braggeec688e2016-11-07 19:49:47 +00003086
Chris Wilson8f8b1172019-10-07 22:09:41 +01003087 mutex_lock(&perf->lock);
3088 ret = i915_perf_poll_locked(stream, file, wait);
3089 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003090
3091 return ret;
3092}
3093
Robert Bragg16d98b32016-12-07 21:40:33 +00003094/**
3095 * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
3096 * @stream: A disabled i915 perf stream
3097 *
3098 * [Re]enables the associated capture of data for this stream.
3099 *
3100 * If a stream was previously enabled then there's currently no intention
3101 * to provide userspace any guarantee about the preservation of previously
3102 * buffered data.
3103 */
Robert Braggeec688e2016-11-07 19:49:47 +00003104static void i915_perf_enable_locked(struct i915_perf_stream *stream)
3105{
3106 if (stream->enabled)
3107 return;
3108
3109 /* Allow stream->ops->enable() to refer to this */
3110 stream->enabled = true;
3111
3112 if (stream->ops->enable)
3113 stream->ops->enable(stream);
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003114
3115 if (stream->hold_preemption)
Chris Wilson9f3ccd42019-12-20 10:12:29 +00003116 intel_context_set_nopreempt(stream->pinned_ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00003117}
3118
Robert Bragg16d98b32016-12-07 21:40:33 +00003119/**
3120 * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
3121 * @stream: An enabled i915 perf stream
3122 *
3123 * Disables the associated capture of data for this stream.
3124 *
3125 * The intention is that disabling an re-enabling a stream will ideally be
3126 * cheaper than destroying and re-opening a stream with the same configuration,
3127 * though there are no formal guarantees about what state or buffered data
3128 * must be retained between disabling and re-enabling a stream.
3129 *
3130 * Note: while a stream is disabled it's considered an error for userspace
3131 * to attempt to read from the stream (-EIO).
3132 */
Robert Braggeec688e2016-11-07 19:49:47 +00003133static void i915_perf_disable_locked(struct i915_perf_stream *stream)
3134{
3135 if (!stream->enabled)
3136 return;
3137
3138 /* Allow stream->ops->disable() to refer to this */
3139 stream->enabled = false;
3140
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003141 if (stream->hold_preemption)
Chris Wilson9f3ccd42019-12-20 10:12:29 +00003142 intel_context_clear_nopreempt(stream->pinned_ctx);
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003143
Robert Braggeec688e2016-11-07 19:49:47 +00003144 if (stream->ops->disable)
3145 stream->ops->disable(stream);
3146}
3147
Chris Wilson7831e9a2019-10-14 21:14:03 +01003148static long i915_perf_config_locked(struct i915_perf_stream *stream,
3149 unsigned long metrics_set)
3150{
3151 struct i915_oa_config *config;
3152 long ret = stream->oa_config->id;
3153
3154 config = i915_perf_get_oa_config(stream->perf, metrics_set);
3155 if (!config)
3156 return -EINVAL;
3157
3158 if (config != stream->oa_config) {
Chris Wilson4b4e9732020-03-02 08:57:57 +00003159 struct i915_request *rq;
Chris Wilson7831e9a2019-10-14 21:14:03 +01003160
3161 /*
3162 * If OA is bound to a specific context, emit the
3163 * reconfiguration inline from that context. The update
3164 * will then be ordered with respect to submission on that
3165 * context.
3166 *
3167 * When set globally, we use a low priority kernel context,
3168 * so it will effectively take effect when idle.
3169 */
Chris Wilson4b4e9732020-03-02 08:57:57 +00003170 rq = emit_oa_config(stream, config, oa_context(stream));
3171 if (!IS_ERR(rq)) {
Chris Wilson7831e9a2019-10-14 21:14:03 +01003172 config = xchg(&stream->oa_config, config);
Chris Wilson4b4e9732020-03-02 08:57:57 +00003173 i915_request_put(rq);
3174 } else {
3175 ret = PTR_ERR(rq);
3176 }
Chris Wilson7831e9a2019-10-14 21:14:03 +01003177 }
3178
3179 i915_oa_config_put(config);
3180
3181 return ret;
3182}
3183
Robert Bragg16d98b32016-12-07 21:40:33 +00003184/**
3185 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
3186 * @stream: An i915 perf stream
3187 * @cmd: the ioctl request
3188 * @arg: the ioctl data
3189 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003190 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00003191 * with any non-file-operation driver hooks.
3192 *
3193 * Returns: zero on success or a negative error code. Returns -EINVAL for
3194 * an unknown ioctl request.
3195 */
Robert Braggeec688e2016-11-07 19:49:47 +00003196static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
3197 unsigned int cmd,
3198 unsigned long arg)
3199{
3200 switch (cmd) {
3201 case I915_PERF_IOCTL_ENABLE:
3202 i915_perf_enable_locked(stream);
3203 return 0;
3204 case I915_PERF_IOCTL_DISABLE:
3205 i915_perf_disable_locked(stream);
3206 return 0;
Chris Wilson7831e9a2019-10-14 21:14:03 +01003207 case I915_PERF_IOCTL_CONFIG:
3208 return i915_perf_config_locked(stream, arg);
Robert Braggeec688e2016-11-07 19:49:47 +00003209 }
3210
3211 return -EINVAL;
3212}
3213
Robert Bragg16d98b32016-12-07 21:40:33 +00003214/**
3215 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
3216 * @file: An i915 perf stream file
3217 * @cmd: the ioctl request
3218 * @arg: the ioctl data
3219 *
3220 * Implementation deferred to i915_perf_ioctl_locked().
3221 *
3222 * Returns: zero on success or a negative error code. Returns -EINVAL for
3223 * an unknown ioctl request.
3224 */
Robert Braggeec688e2016-11-07 19:49:47 +00003225static long i915_perf_ioctl(struct file *file,
3226 unsigned int cmd,
3227 unsigned long arg)
3228{
3229 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003230 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003231 long ret;
3232
Chris Wilson8f8b1172019-10-07 22:09:41 +01003233 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003234 ret = i915_perf_ioctl_locked(stream, cmd, arg);
Chris Wilson8f8b1172019-10-07 22:09:41 +01003235 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003236
3237 return ret;
3238}
3239
Robert Bragg16d98b32016-12-07 21:40:33 +00003240/**
3241 * i915_perf_destroy_locked - destroy an i915 perf stream
3242 * @stream: An i915 perf stream
3243 *
3244 * Frees all resources associated with the given i915 perf @stream, disabling
3245 * any associated data capture in the process.
3246 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003247 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00003248 * with any non-file-operation driver hooks.
3249 */
Robert Braggeec688e2016-11-07 19:49:47 +00003250static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
3251{
Robert Braggeec688e2016-11-07 19:49:47 +00003252 if (stream->enabled)
3253 i915_perf_disable_locked(stream);
3254
3255 if (stream->ops->destroy)
3256 stream->ops->destroy(stream);
3257
Chris Wilson69df05e2016-12-18 15:37:21 +00003258 if (stream->ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01003259 i915_gem_context_put(stream->ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00003260
3261 kfree(stream);
3262}
3263
Robert Bragg16d98b32016-12-07 21:40:33 +00003264/**
3265 * i915_perf_release - handles userspace close() of a stream file
3266 * @inode: anonymous inode associated with file
3267 * @file: An i915 perf stream file
3268 *
3269 * Cleans up any resources associated with an open i915 perf stream file.
3270 *
3271 * NB: close() can't really fail from the userspace point of view.
3272 *
3273 * Returns: zero on success or a negative error code.
3274 */
Robert Braggeec688e2016-11-07 19:49:47 +00003275static int i915_perf_release(struct inode *inode, struct file *file)
3276{
3277 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003278 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003279
Chris Wilson8f8b1172019-10-07 22:09:41 +01003280 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003281 i915_perf_destroy_locked(stream);
Chris Wilson8f8b1172019-10-07 22:09:41 +01003282 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003283
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003284 /* Release the reference the perf stream kept on the driver. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003285 drm_dev_put(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003286
Robert Braggeec688e2016-11-07 19:49:47 +00003287 return 0;
3288}
3289
3290
3291static const struct file_operations fops = {
3292 .owner = THIS_MODULE,
3293 .llseek = no_llseek,
3294 .release = i915_perf_release,
3295 .poll = i915_perf_poll,
3296 .read = i915_perf_read,
3297 .unlocked_ioctl = i915_perf_ioctl,
Lionel Landwerlin191f8962017-10-24 16:27:28 +01003298 /* Our ioctl have no arguments, so it's safe to use the same function
3299 * to handle 32bits compatibility.
3300 */
3301 .compat_ioctl = i915_perf_ioctl,
Robert Braggeec688e2016-11-07 19:49:47 +00003302};
3303
3304
Robert Bragg16d98b32016-12-07 21:40:33 +00003305/**
3306 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
Chris Wilson8f8b1172019-10-07 22:09:41 +01003307 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003308 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
3309 * @props: individually validated u64 property value pairs
3310 * @file: drm file
3311 *
3312 * See i915_perf_ioctl_open() for interface details.
3313 *
3314 * Implements further stream config validation and stream initialization on
Chris Wilson8f8b1172019-10-07 22:09:41 +01003315 * behalf of i915_perf_open_ioctl() with the &perf->lock mutex
Robert Bragg16d98b32016-12-07 21:40:33 +00003316 * taken to serialize with any non-file-operation driver hooks.
3317 *
3318 * Note: at this point the @props have only been validated in isolation and
3319 * it's still necessary to validate that the combination of properties makes
3320 * sense.
3321 *
3322 * In the case where userspace is interested in OA unit metrics then further
3323 * config validation and stream initialization details will be handled by
3324 * i915_oa_stream_init(). The code here should only validate config state that
3325 * will be relevant to all stream types / backends.
3326 *
3327 * Returns: zero on success or a negative error code.
3328 */
Robert Braggeec688e2016-11-07 19:49:47 +00003329static int
Chris Wilson8f8b1172019-10-07 22:09:41 +01003330i915_perf_open_ioctl_locked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00003331 struct drm_i915_perf_open_param *param,
3332 struct perf_open_properties *props,
3333 struct drm_file *file)
3334{
3335 struct i915_gem_context *specific_ctx = NULL;
3336 struct i915_perf_stream *stream = NULL;
3337 unsigned long f_flags = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01003338 bool privileged_op = true;
Robert Braggeec688e2016-11-07 19:49:47 +00003339 int stream_fd;
3340 int ret;
3341
3342 if (props->single_context) {
3343 u32 ctx_handle = props->ctx_handle;
3344 struct drm_i915_file_private *file_priv = file->driver_priv;
3345
Imre Deak635f56c2017-07-14 18:12:41 +03003346 specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle);
3347 if (!specific_ctx) {
3348 DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n",
3349 ctx_handle);
3350 ret = -ENOENT;
Robert Braggeec688e2016-11-07 19:49:47 +00003351 goto err;
3352 }
3353 }
3354
Robert Bragg19f81df2017-06-13 12:23:03 +01003355 /*
3356 * On Haswell the OA unit supports clock gating off for a specific
3357 * context and in this mode there's no visibility of metrics for the
3358 * rest of the system, which we consider acceptable for a
3359 * non-privileged client.
3360 *
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003361 * For Gen8->11 the OA unit no longer supports clock gating off for a
Robert Bragg19f81df2017-06-13 12:23:03 +01003362 * specific context and the kernel can't securely stop the counters
3363 * from updating as system-wide / global values. Even though we can
3364 * filter reports based on the included context ID we can't block
3365 * clients from seeing the raw / global counter values via
3366 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
3367 * enable the OA unit by default.
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003368 *
3369 * For Gen12+ we gain a new OAR unit that only monitors the RCS on a
3370 * per context basis. So we can relax requirements there if the user
3371 * doesn't request global stream access (i.e. query based sampling
3372 * using MI_RECORD_PERF_COUNT.
Robert Bragg19f81df2017-06-13 12:23:03 +01003373 */
Lionel Landwerlin0b0120d2019-11-11 11:53:08 +02003374 if (IS_HASWELL(perf->i915) && specific_ctx)
Robert Bragg19f81df2017-06-13 12:23:03 +01003375 privileged_op = false;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003376 else if (IS_GEN(perf->i915, 12) && specific_ctx &&
3377 (props->sample_flags & SAMPLE_OA_REPORT) == 0)
3378 privileged_op = false;
Robert Bragg19f81df2017-06-13 12:23:03 +01003379
Lionel Landwerlin0b0120d2019-11-11 11:53:08 +02003380 if (props->hold_preemption) {
3381 if (!props->single_context) {
3382 DRM_DEBUG("preemption disable with no context\n");
3383 ret = -EINVAL;
3384 goto err;
3385 }
3386 privileged_op = true;
3387 }
3388
Robert Braggccdf6342016-11-07 19:49:54 +00003389 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
3390 * we check a dev.i915.perf_stream_paranoid sysctl option
3391 * to determine if it's ok to access system wide OA counters
3392 * without CAP_SYS_ADMIN privileges.
3393 */
Robert Bragg19f81df2017-06-13 12:23:03 +01003394 if (privileged_op &&
Robert Braggccdf6342016-11-07 19:49:54 +00003395 i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003396 DRM_DEBUG("Insufficient privileges to open i915 perf stream\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003397 ret = -EACCES;
3398 goto err_ctx;
3399 }
3400
3401 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
3402 if (!stream) {
3403 ret = -ENOMEM;
3404 goto err_ctx;
3405 }
3406
Chris Wilson8f8b1172019-10-07 22:09:41 +01003407 stream->perf = perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003408 stream->ctx = specific_ctx;
3409
Robert Braggd7965152016-11-07 19:49:52 +00003410 ret = i915_oa_stream_init(stream, param, props);
3411 if (ret)
3412 goto err_alloc;
3413
3414 /* we avoid simply assigning stream->sample_flags = props->sample_flags
3415 * to have _stream_init check the combination of sample flags more
3416 * thoroughly, but still this is the expected result at this point.
Robert Braggeec688e2016-11-07 19:49:47 +00003417 */
Robert Braggd7965152016-11-07 19:49:52 +00003418 if (WARN_ON(stream->sample_flags != props->sample_flags)) {
3419 ret = -ENODEV;
Matthew Auld22f880c2017-03-27 21:34:59 +01003420 goto err_flags;
Robert Braggd7965152016-11-07 19:49:52 +00003421 }
Robert Braggeec688e2016-11-07 19:49:47 +00003422
Robert Braggeec688e2016-11-07 19:49:47 +00003423 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
3424 f_flags |= O_CLOEXEC;
3425 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
3426 f_flags |= O_NONBLOCK;
3427
3428 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
3429 if (stream_fd < 0) {
3430 ret = stream_fd;
Lionel Landwerlin23b9e412019-10-08 15:01:11 +01003431 goto err_flags;
Robert Braggeec688e2016-11-07 19:49:47 +00003432 }
3433
3434 if (!(param->flags & I915_PERF_FLAG_DISABLED))
3435 i915_perf_enable_locked(stream);
3436
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003437 /* Take a reference on the driver that will be kept with stream_fd
3438 * until its release.
3439 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003440 drm_dev_get(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003441
Robert Braggeec688e2016-11-07 19:49:47 +00003442 return stream_fd;
3443
Matthew Auld22f880c2017-03-27 21:34:59 +01003444err_flags:
Robert Braggeec688e2016-11-07 19:49:47 +00003445 if (stream->ops->destroy)
3446 stream->ops->destroy(stream);
3447err_alloc:
3448 kfree(stream);
3449err_ctx:
Chris Wilson69df05e2016-12-18 15:37:21 +00003450 if (specific_ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01003451 i915_gem_context_put(specific_ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00003452err:
3453 return ret;
3454}
3455
Chris Wilson8f8b1172019-10-07 22:09:41 +01003456static u64 oa_exponent_to_ns(struct i915_perf *perf, int exponent)
Robert Bragg155e9412017-06-13 12:23:05 +01003457{
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01003458 return div64_u64(1000000000ULL * (2ULL << exponent),
Chris Wilson8f8b1172019-10-07 22:09:41 +01003459 1000ULL * RUNTIME_INFO(perf->i915)->cs_timestamp_frequency_khz);
Robert Bragg155e9412017-06-13 12:23:05 +01003460}
3461
Robert Bragg16d98b32016-12-07 21:40:33 +00003462/**
3463 * read_properties_unlocked - validate + copy userspace stream open properties
Chris Wilson8f8b1172019-10-07 22:09:41 +01003464 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003465 * @uprops: The array of u64 key value pairs given by userspace
3466 * @n_props: The number of key value pairs expected in @uprops
3467 * @props: The stream configuration built up while validating properties
Robert Braggeec688e2016-11-07 19:49:47 +00003468 *
3469 * Note this function only validates properties in isolation it doesn't
3470 * validate that the combination of properties makes sense or that all
3471 * properties necessary for a particular kind of stream have been set.
Robert Bragg16d98b32016-12-07 21:40:33 +00003472 *
3473 * Note that there currently aren't any ordering requirements for properties so
3474 * we shouldn't validate or assume anything about ordering here. This doesn't
3475 * rule out defining new properties with ordering requirements in the future.
Robert Braggeec688e2016-11-07 19:49:47 +00003476 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003477static int read_properties_unlocked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00003478 u64 __user *uprops,
3479 u32 n_props,
3480 struct perf_open_properties *props)
3481{
3482 u64 __user *uprop = uprops;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003483 u32 i;
Robert Braggeec688e2016-11-07 19:49:47 +00003484
3485 memset(props, 0, sizeof(struct perf_open_properties));
3486
3487 if (!n_props) {
Robert Bragg77085502016-12-01 17:21:52 +00003488 DRM_DEBUG("No i915 perf properties given\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003489 return -EINVAL;
3490 }
3491
Lionel Landwerlin9a613632019-10-10 16:05:19 +01003492 /* At the moment we only support using i915-perf on the RCS. */
3493 props->engine = intel_engine_lookup_user(perf->i915,
3494 I915_ENGINE_CLASS_RENDER,
3495 0);
3496 if (!props->engine) {
3497 DRM_DEBUG("No RENDER-capable engines\n");
3498 return -EINVAL;
3499 }
3500
Robert Braggeec688e2016-11-07 19:49:47 +00003501 /* Considering that ID = 0 is reserved and assuming that we don't
3502 * (currently) expect any configurations to ever specify duplicate
3503 * values for a particular property ID then the last _PROP_MAX value is
3504 * one greater than the maximum number of properties we expect to get
3505 * from userspace.
3506 */
3507 if (n_props >= DRM_I915_PERF_PROP_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00003508 DRM_DEBUG("More i915 perf properties specified than exist\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003509 return -EINVAL;
3510 }
3511
3512 for (i = 0; i < n_props; i++) {
Robert Bragg00319ba2016-11-07 19:49:55 +00003513 u64 oa_period, oa_freq_hz;
Robert Braggeec688e2016-11-07 19:49:47 +00003514 u64 id, value;
3515 int ret;
3516
3517 ret = get_user(id, uprop);
3518 if (ret)
3519 return ret;
3520
3521 ret = get_user(value, uprop + 1);
3522 if (ret)
3523 return ret;
3524
Matthew Auld0a309f92017-03-27 21:32:36 +01003525 if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
3526 DRM_DEBUG("Unknown i915 perf property ID\n");
3527 return -EINVAL;
3528 }
3529
Robert Braggeec688e2016-11-07 19:49:47 +00003530 switch ((enum drm_i915_perf_property_id)id) {
3531 case DRM_I915_PERF_PROP_CTX_HANDLE:
3532 props->single_context = 1;
3533 props->ctx_handle = value;
3534 break;
Robert Braggd7965152016-11-07 19:49:52 +00003535 case DRM_I915_PERF_PROP_SAMPLE_OA:
Lionel Landwerlinb6dd47b2018-03-26 10:08:22 +01003536 if (value)
3537 props->sample_flags |= SAMPLE_OA_REPORT;
Robert Braggd7965152016-11-07 19:49:52 +00003538 break;
3539 case DRM_I915_PERF_PROP_OA_METRICS_SET:
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003540 if (value == 0) {
Robert Bragg77085502016-12-01 17:21:52 +00003541 DRM_DEBUG("Unknown OA metric set ID\n");
Robert Braggd7965152016-11-07 19:49:52 +00003542 return -EINVAL;
3543 }
3544 props->metrics_set = value;
3545 break;
3546 case DRM_I915_PERF_PROP_OA_FORMAT:
3547 if (value == 0 || value >= I915_OA_FORMAT_MAX) {
Robert Bragg52c57c22017-05-11 16:43:29 +01003548 DRM_DEBUG("Out-of-range OA report format %llu\n",
3549 value);
Robert Braggd7965152016-11-07 19:49:52 +00003550 return -EINVAL;
3551 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01003552 if (!perf->oa_formats[value].size) {
Robert Bragg52c57c22017-05-11 16:43:29 +01003553 DRM_DEBUG("Unsupported OA report format %llu\n",
3554 value);
Robert Braggd7965152016-11-07 19:49:52 +00003555 return -EINVAL;
3556 }
3557 props->oa_format = value;
3558 break;
3559 case DRM_I915_PERF_PROP_OA_EXPONENT:
3560 if (value > OA_EXPONENT_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00003561 DRM_DEBUG("OA timer exponent too high (> %u)\n",
3562 OA_EXPONENT_MAX);
Robert Braggd7965152016-11-07 19:49:52 +00003563 return -EINVAL;
3564 }
3565
Robert Bragg00319ba2016-11-07 19:49:55 +00003566 /* Theoretically we can program the OA unit to sample
Robert Bragg155e9412017-06-13 12:23:05 +01003567 * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
3568 * for BXT. We don't allow such high sampling
3569 * frequencies by default unless root.
Robert Braggd7965152016-11-07 19:49:52 +00003570 */
Robert Bragg155e9412017-06-13 12:23:05 +01003571
Robert Bragg00319ba2016-11-07 19:49:55 +00003572 BUILD_BUG_ON(sizeof(oa_period) != 8);
Chris Wilson8f8b1172019-10-07 22:09:41 +01003573 oa_period = oa_exponent_to_ns(perf, value);
Robert Bragg00319ba2016-11-07 19:49:55 +00003574
3575 /* This check is primarily to ensure that oa_period <=
3576 * UINT32_MAX (before passing to do_div which only
3577 * accepts a u32 denominator), but we can also skip
3578 * checking anything < 1Hz which implicitly can't be
3579 * limited via an integer oa_max_sample_rate.
3580 */
3581 if (oa_period <= NSEC_PER_SEC) {
3582 u64 tmp = NSEC_PER_SEC;
3583 do_div(tmp, oa_period);
3584 oa_freq_hz = tmp;
3585 } else
3586 oa_freq_hz = 0;
3587
3588 if (oa_freq_hz > i915_oa_max_sample_rate &&
3589 !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00003590 DRM_DEBUG("OA exponent would exceed the max sampling frequency (sysctl dev.i915.oa_max_sample_rate) %uHz without root privileges\n",
Robert Bragg00319ba2016-11-07 19:49:55 +00003591 i915_oa_max_sample_rate);
Robert Braggd7965152016-11-07 19:49:52 +00003592 return -EACCES;
3593 }
3594
3595 props->oa_periodic = true;
3596 props->oa_period_exponent = value;
3597 break;
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003598 case DRM_I915_PERF_PROP_HOLD_PREEMPTION:
3599 props->hold_preemption = !!value;
3600 break;
Matthew Auld0a309f92017-03-27 21:32:36 +01003601 case DRM_I915_PERF_PROP_MAX:
Robert Braggeec688e2016-11-07 19:49:47 +00003602 MISSING_CASE(id);
Robert Braggeec688e2016-11-07 19:49:47 +00003603 return -EINVAL;
3604 }
3605
3606 uprop += 2;
3607 }
3608
3609 return 0;
3610}
3611
Robert Bragg16d98b32016-12-07 21:40:33 +00003612/**
3613 * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
3614 * @dev: drm device
3615 * @data: ioctl data copied from userspace (unvalidated)
3616 * @file: drm file
3617 *
3618 * Validates the stream open parameters given by userspace including flags
3619 * and an array of u64 key, value pair properties.
3620 *
3621 * Very little is assumed up front about the nature of the stream being
3622 * opened (for instance we don't assume it's for periodic OA unit metrics). An
3623 * i915-perf stream is expected to be a suitable interface for other forms of
3624 * buffered data written by the GPU besides periodic OA metrics.
3625 *
3626 * Note we copy the properties from userspace outside of the i915 perf
3627 * mutex to avoid an awkward lockdep with mmap_sem.
3628 *
3629 * Most of the implementation details are handled by
Chris Wilson8f8b1172019-10-07 22:09:41 +01003630 * i915_perf_open_ioctl_locked() after taking the &perf->lock
Robert Bragg16d98b32016-12-07 21:40:33 +00003631 * mutex for serializing with any non-file-operation driver hooks.
3632 *
3633 * Return: A newly opened i915 Perf stream file descriptor or negative
3634 * error code on failure.
3635 */
Robert Braggeec688e2016-11-07 19:49:47 +00003636int i915_perf_open_ioctl(struct drm_device *dev, void *data,
3637 struct drm_file *file)
3638{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003639 struct i915_perf *perf = &to_i915(dev)->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003640 struct drm_i915_perf_open_param *param = data;
3641 struct perf_open_properties props;
3642 u32 known_open_flags;
3643 int ret;
3644
Chris Wilson8f8b1172019-10-07 22:09:41 +01003645 if (!perf->i915) {
Robert Bragg77085502016-12-01 17:21:52 +00003646 DRM_DEBUG("i915 perf interface not available for this system\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003647 return -ENOTSUPP;
3648 }
3649
3650 known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
3651 I915_PERF_FLAG_FD_NONBLOCK |
3652 I915_PERF_FLAG_DISABLED;
3653 if (param->flags & ~known_open_flags) {
Robert Bragg77085502016-12-01 17:21:52 +00003654 DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003655 return -EINVAL;
3656 }
3657
Chris Wilson8f8b1172019-10-07 22:09:41 +01003658 ret = read_properties_unlocked(perf,
Robert Braggeec688e2016-11-07 19:49:47 +00003659 u64_to_user_ptr(param->properties_ptr),
3660 param->num_properties,
3661 &props);
3662 if (ret)
3663 return ret;
3664
Chris Wilson8f8b1172019-10-07 22:09:41 +01003665 mutex_lock(&perf->lock);
3666 ret = i915_perf_open_ioctl_locked(perf, param, &props, file);
3667 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003668
3669 return ret;
3670}
3671
Robert Bragg16d98b32016-12-07 21:40:33 +00003672/**
3673 * i915_perf_register - exposes i915-perf to userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01003674 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003675 *
3676 * In particular OA metric sets are advertised under a sysfs metrics/
3677 * directory allowing userspace to enumerate valid IDs that can be
3678 * used to open an i915-perf stream.
3679 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003680void i915_perf_register(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003681{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003682 struct i915_perf *perf = &i915->perf;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003683 int ret;
3684
Chris Wilson8f8b1172019-10-07 22:09:41 +01003685 if (!perf->i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003686 return;
3687
3688 /* To be sure we're synchronized with an attempted
3689 * i915_perf_open_ioctl(); considering that we register after
3690 * being exposed to userspace.
3691 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003692 mutex_lock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003693
Chris Wilson8f8b1172019-10-07 22:09:41 +01003694 perf->metrics_kobj =
Robert Bragg442b8c02016-11-07 19:49:53 +00003695 kobject_create_and_add("metrics",
Chris Wilson8f8b1172019-10-07 22:09:41 +01003696 &i915->drm.primary->kdev->kobj);
3697 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003698 goto exit;
3699
Chris Wilson8f8b1172019-10-07 22:09:41 +01003700 sysfs_attr_init(&perf->test_config.sysfs_metric_id.attr);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003701
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003702 if (IS_TIGERLAKE(i915)) {
3703 i915_perf_load_test_config_tgl(i915);
3704 } else if (INTEL_GEN(i915) >= 11) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01003705 i915_perf_load_test_config_icl(i915);
3706 } else if (IS_CANNONLAKE(i915)) {
3707 i915_perf_load_test_config_cnl(i915);
3708 } else if (IS_COFFEELAKE(i915)) {
3709 if (IS_CFL_GT2(i915))
3710 i915_perf_load_test_config_cflgt2(i915);
3711 if (IS_CFL_GT3(i915))
3712 i915_perf_load_test_config_cflgt3(i915);
3713 } else if (IS_GEMINILAKE(i915)) {
3714 i915_perf_load_test_config_glk(i915);
3715 } else if (IS_KABYLAKE(i915)) {
3716 if (IS_KBL_GT2(i915))
3717 i915_perf_load_test_config_kblgt2(i915);
3718 else if (IS_KBL_GT3(i915))
3719 i915_perf_load_test_config_kblgt3(i915);
3720 } else if (IS_BROXTON(i915)) {
3721 i915_perf_load_test_config_bxt(i915);
3722 } else if (IS_SKYLAKE(i915)) {
3723 if (IS_SKL_GT2(i915))
3724 i915_perf_load_test_config_sklgt2(i915);
3725 else if (IS_SKL_GT3(i915))
3726 i915_perf_load_test_config_sklgt3(i915);
3727 else if (IS_SKL_GT4(i915))
3728 i915_perf_load_test_config_sklgt4(i915);
3729 } else if (IS_CHERRYVIEW(i915)) {
3730 i915_perf_load_test_config_chv(i915);
3731 } else if (IS_BROADWELL(i915)) {
3732 i915_perf_load_test_config_bdw(i915);
3733 } else if (IS_HASWELL(i915)) {
3734 i915_perf_load_test_config_hsw(i915);
3735 }
Robert Bragg442b8c02016-11-07 19:49:53 +00003736
Chris Wilson8f8b1172019-10-07 22:09:41 +01003737 if (perf->test_config.id == 0)
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003738 goto sysfs_error;
3739
Chris Wilson8f8b1172019-10-07 22:09:41 +01003740 ret = sysfs_create_group(perf->metrics_kobj,
3741 &perf->test_config.sysfs_metric);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003742 if (ret)
3743 goto sysfs_error;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003744
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003745 perf->test_config.perf = perf;
3746 kref_init(&perf->test_config.ref);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003747
Robert Bragg19f81df2017-06-13 12:23:03 +01003748 goto exit;
3749
3750sysfs_error:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003751 kobject_put(perf->metrics_kobj);
3752 perf->metrics_kobj = NULL;
Robert Bragg19f81df2017-06-13 12:23:03 +01003753
Robert Bragg442b8c02016-11-07 19:49:53 +00003754exit:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003755 mutex_unlock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003756}
3757
Robert Bragg16d98b32016-12-07 21:40:33 +00003758/**
3759 * i915_perf_unregister - hide i915-perf from userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01003760 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003761 *
3762 * i915-perf state cleanup is split up into an 'unregister' and
3763 * 'deinit' phase where the interface is first hidden from
3764 * userspace by i915_perf_unregister() before cleaning up
3765 * remaining state in i915_perf_fini().
3766 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003767void i915_perf_unregister(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003768{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003769 struct i915_perf *perf = &i915->perf;
3770
3771 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003772 return;
3773
Chris Wilson8f8b1172019-10-07 22:09:41 +01003774 sysfs_remove_group(perf->metrics_kobj,
3775 &perf->test_config.sysfs_metric);
Robert Bragg442b8c02016-11-07 19:49:53 +00003776
Chris Wilson8f8b1172019-10-07 22:09:41 +01003777 kobject_put(perf->metrics_kobj);
3778 perf->metrics_kobj = NULL;
Robert Bragg442b8c02016-11-07 19:49:53 +00003779}
3780
Chris Wilson8f8b1172019-10-07 22:09:41 +01003781static bool gen8_is_valid_flex_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003782{
3783 static const i915_reg_t flex_eu_regs[] = {
3784 EU_PERF_CNTL0,
3785 EU_PERF_CNTL1,
3786 EU_PERF_CNTL2,
3787 EU_PERF_CNTL3,
3788 EU_PERF_CNTL4,
3789 EU_PERF_CNTL5,
3790 EU_PERF_CNTL6,
3791 };
3792 int i;
3793
3794 for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) {
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003795 if (i915_mmio_reg_offset(flex_eu_regs[i]) == addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003796 return true;
3797 }
3798 return false;
3799}
3800
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003801#define ADDR_IN_RANGE(addr, start, end) \
3802 ((addr) >= (start) && \
3803 (addr) <= (end))
3804
3805#define REG_IN_RANGE(addr, start, end) \
3806 ((addr) >= i915_mmio_reg_offset(start) && \
3807 (addr) <= i915_mmio_reg_offset(end))
3808
3809#define REG_EQUAL(addr, mmio) \
3810 ((addr) == i915_mmio_reg_offset(mmio))
3811
Chris Wilson8f8b1172019-10-07 22:09:41 +01003812static bool gen7_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003813{
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003814 return REG_IN_RANGE(addr, OASTARTTRIG1, OASTARTTRIG8) ||
3815 REG_IN_RANGE(addr, OAREPORTTRIG1, OAREPORTTRIG8) ||
3816 REG_IN_RANGE(addr, OACEC0_0, OACEC7_1);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003817}
3818
Chris Wilson8f8b1172019-10-07 22:09:41 +01003819static bool gen7_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003820{
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003821 return REG_EQUAL(addr, HALF_SLICE_CHICKEN2) ||
3822 REG_IN_RANGE(addr, MICRO_BP0_0, NOA_WRITE) ||
3823 REG_IN_RANGE(addr, OA_PERFCNT1_LO, OA_PERFCNT2_HI) ||
3824 REG_IN_RANGE(addr, OA_PERFMATRIX_LO, OA_PERFMATRIX_HI);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003825}
3826
Chris Wilson8f8b1172019-10-07 22:09:41 +01003827static bool gen8_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003828{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003829 return gen7_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003830 REG_EQUAL(addr, WAIT_FOR_RC6_EXIT) ||
3831 REG_IN_RANGE(addr, RPM_CONFIG0, NOA_CONFIG(8));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003832}
3833
Chris Wilson8f8b1172019-10-07 22:09:41 +01003834static bool gen10_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003835{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003836 return gen8_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003837 REG_EQUAL(addr, GEN10_NOA_WRITE_HIGH) ||
3838 REG_IN_RANGE(addr, OA_PERFCNT3_LO, OA_PERFCNT4_HI);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003839}
3840
Chris Wilson8f8b1172019-10-07 22:09:41 +01003841static bool hsw_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003842{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003843 return gen7_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003844 ADDR_IN_RANGE(addr, 0x25100, 0x2FF90) ||
3845 REG_IN_RANGE(addr, HSW_MBVID2_NOA0, HSW_MBVID2_NOA9) ||
3846 REG_EQUAL(addr, HSW_MBVID2_MISR0);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003847}
3848
Chris Wilson8f8b1172019-10-07 22:09:41 +01003849static bool chv_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003850{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003851 return gen7_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003852 ADDR_IN_RANGE(addr, 0x182300, 0x1823A4);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003853}
3854
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003855static bool gen12_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
3856{
3857 return REG_IN_RANGE(addr, GEN12_OAG_OASTARTTRIG1, GEN12_OAG_OASTARTTRIG8) ||
3858 REG_IN_RANGE(addr, GEN12_OAG_OAREPORTTRIG1, GEN12_OAG_OAREPORTTRIG8) ||
3859 REG_IN_RANGE(addr, GEN12_OAG_CEC0_0, GEN12_OAG_CEC7_1) ||
3860 REG_IN_RANGE(addr, GEN12_OAG_SCEC0_0, GEN12_OAG_SCEC7_1) ||
3861 REG_EQUAL(addr, GEN12_OAA_DBG_REG) ||
3862 REG_EQUAL(addr, GEN12_OAG_OA_PESS) ||
3863 REG_EQUAL(addr, GEN12_OAG_SPCTR_CNF);
3864}
3865
3866static bool gen12_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
3867{
3868 return REG_EQUAL(addr, NOA_WRITE) ||
3869 REG_EQUAL(addr, GEN10_NOA_WRITE_HIGH) ||
3870 REG_EQUAL(addr, GDT_CHICKEN_BITS) ||
3871 REG_EQUAL(addr, WAIT_FOR_RC6_EXIT) ||
3872 REG_EQUAL(addr, RPM_CONFIG0) ||
3873 REG_EQUAL(addr, RPM_CONFIG1) ||
3874 REG_IN_RANGE(addr, NOA_CONFIG(0), NOA_CONFIG(8));
3875}
3876
Jani Nikula739f3ab2019-01-16 11:15:19 +02003877static u32 mask_reg_value(u32 reg, u32 val)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003878{
3879 /* HALF_SLICE_CHICKEN2 is programmed with a the
3880 * WaDisableSTUnitPowerOptimization workaround. Make sure the value
3881 * programmed by userspace doesn't change this.
3882 */
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003883 if (REG_EQUAL(reg, HALF_SLICE_CHICKEN2))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003884 val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE);
3885
3886 /* WAIT_FOR_RC6_EXIT has only one bit fullfilling the function
3887 * indicated by its name and a bunch of selection fields used by OA
3888 * configs.
3889 */
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003890 if (REG_EQUAL(reg, WAIT_FOR_RC6_EXIT))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003891 val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE);
3892
3893 return val;
3894}
3895
Chris Wilson8f8b1172019-10-07 22:09:41 +01003896static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,
3897 bool (*is_valid)(struct i915_perf *perf, u32 addr),
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003898 u32 __user *regs,
3899 u32 n_regs)
3900{
3901 struct i915_oa_reg *oa_regs;
3902 int err;
3903 u32 i;
3904
3905 if (!n_regs)
3906 return NULL;
3907
Linus Torvalds96d4f262019-01-03 18:57:57 -08003908 if (!access_ok(regs, n_regs * sizeof(u32) * 2))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003909 return ERR_PTR(-EFAULT);
3910
3911 /* No is_valid function means we're not allowing any register to be programmed. */
3912 GEM_BUG_ON(!is_valid);
3913 if (!is_valid)
3914 return ERR_PTR(-EINVAL);
3915
3916 oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL);
3917 if (!oa_regs)
3918 return ERR_PTR(-ENOMEM);
3919
3920 for (i = 0; i < n_regs; i++) {
3921 u32 addr, value;
3922
3923 err = get_user(addr, regs);
3924 if (err)
3925 goto addr_err;
3926
Chris Wilson8f8b1172019-10-07 22:09:41 +01003927 if (!is_valid(perf, addr)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003928 DRM_DEBUG("Invalid oa_reg address: %X\n", addr);
3929 err = -EINVAL;
3930 goto addr_err;
3931 }
3932
3933 err = get_user(value, regs + 1);
3934 if (err)
3935 goto addr_err;
3936
3937 oa_regs[i].addr = _MMIO(addr);
3938 oa_regs[i].value = mask_reg_value(addr, value);
3939
3940 regs += 2;
3941 }
3942
3943 return oa_regs;
3944
3945addr_err:
3946 kfree(oa_regs);
3947 return ERR_PTR(err);
3948}
3949
3950static ssize_t show_dynamic_id(struct device *dev,
3951 struct device_attribute *attr,
3952 char *buf)
3953{
3954 struct i915_oa_config *oa_config =
3955 container_of(attr, typeof(*oa_config), sysfs_metric_id);
3956
3957 return sprintf(buf, "%d\n", oa_config->id);
3958}
3959
Chris Wilson8f8b1172019-10-07 22:09:41 +01003960static int create_dynamic_oa_sysfs_entry(struct i915_perf *perf,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003961 struct i915_oa_config *oa_config)
3962{
Chris Wilson28152a22017-08-03 23:37:00 +01003963 sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003964 oa_config->sysfs_metric_id.attr.name = "id";
3965 oa_config->sysfs_metric_id.attr.mode = S_IRUGO;
3966 oa_config->sysfs_metric_id.show = show_dynamic_id;
3967 oa_config->sysfs_metric_id.store = NULL;
3968
3969 oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr;
3970 oa_config->attrs[1] = NULL;
3971
3972 oa_config->sysfs_metric.name = oa_config->uuid;
3973 oa_config->sysfs_metric.attrs = oa_config->attrs;
3974
Chris Wilson8f8b1172019-10-07 22:09:41 +01003975 return sysfs_create_group(perf->metrics_kobj,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003976 &oa_config->sysfs_metric);
3977}
3978
3979/**
3980 * i915_perf_add_config_ioctl - DRM ioctl() for userspace to add a new OA config
3981 * @dev: drm device
3982 * @data: ioctl data (pointer to struct drm_i915_perf_oa_config) copied from
3983 * userspace (unvalidated)
3984 * @file: drm file
3985 *
3986 * Validates the submitted OA register to be saved into a new OA config that
3987 * can then be used for programming the OA unit and its NOA network.
3988 *
3989 * Returns: A new allocated config number to be used with the perf open ioctl
3990 * or a negative error code on failure.
3991 */
3992int i915_perf_add_config_ioctl(struct drm_device *dev, void *data,
3993 struct drm_file *file)
3994{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003995 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003996 struct drm_i915_perf_oa_config *args = data;
3997 struct i915_oa_config *oa_config, *tmp;
Mao Wenanc415ef22019-12-04 09:01:54 +08003998 struct i915_oa_reg *regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003999 int err, id;
4000
Chris Wilson8f8b1172019-10-07 22:09:41 +01004001 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004002 DRM_DEBUG("i915 perf interface not available for this system\n");
4003 return -ENOTSUPP;
4004 }
4005
Chris Wilson8f8b1172019-10-07 22:09:41 +01004006 if (!perf->metrics_kobj) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004007 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
4008 return -EINVAL;
4009 }
4010
4011 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
4012 DRM_DEBUG("Insufficient privileges to add i915 OA config\n");
4013 return -EACCES;
4014 }
4015
4016 if ((!args->mux_regs_ptr || !args->n_mux_regs) &&
4017 (!args->boolean_regs_ptr || !args->n_boolean_regs) &&
4018 (!args->flex_regs_ptr || !args->n_flex_regs)) {
4019 DRM_DEBUG("No OA registers given\n");
4020 return -EINVAL;
4021 }
4022
4023 oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL);
4024 if (!oa_config) {
4025 DRM_DEBUG("Failed to allocate memory for the OA config\n");
4026 return -ENOMEM;
4027 }
4028
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004029 oa_config->perf = perf;
4030 kref_init(&oa_config->ref);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004031
4032 if (!uuid_is_valid(args->uuid)) {
4033 DRM_DEBUG("Invalid uuid format for OA config\n");
4034 err = -EINVAL;
4035 goto reg_err;
4036 }
4037
4038 /* Last character in oa_config->uuid will be 0 because oa_config is
4039 * kzalloc.
4040 */
4041 memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid));
4042
4043 oa_config->mux_regs_len = args->n_mux_regs;
Chris Wilsonc2fba932019-10-13 10:52:11 +01004044 regs = alloc_oa_regs(perf,
4045 perf->ops.is_valid_mux_reg,
4046 u64_to_user_ptr(args->mux_regs_ptr),
4047 args->n_mux_regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004048
Chris Wilsonc2fba932019-10-13 10:52:11 +01004049 if (IS_ERR(regs)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004050 DRM_DEBUG("Failed to create OA config for mux_regs\n");
Chris Wilsonc2fba932019-10-13 10:52:11 +01004051 err = PTR_ERR(regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004052 goto reg_err;
4053 }
Chris Wilsonc2fba932019-10-13 10:52:11 +01004054 oa_config->mux_regs = regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004055
4056 oa_config->b_counter_regs_len = args->n_boolean_regs;
Chris Wilsonc2fba932019-10-13 10:52:11 +01004057 regs = alloc_oa_regs(perf,
4058 perf->ops.is_valid_b_counter_reg,
4059 u64_to_user_ptr(args->boolean_regs_ptr),
4060 args->n_boolean_regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004061
Chris Wilsonc2fba932019-10-13 10:52:11 +01004062 if (IS_ERR(regs)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004063 DRM_DEBUG("Failed to create OA config for b_counter_regs\n");
Chris Wilsonc2fba932019-10-13 10:52:11 +01004064 err = PTR_ERR(regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004065 goto reg_err;
4066 }
Chris Wilsonc2fba932019-10-13 10:52:11 +01004067 oa_config->b_counter_regs = regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004068
Chris Wilson8f8b1172019-10-07 22:09:41 +01004069 if (INTEL_GEN(perf->i915) < 8) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004070 if (args->n_flex_regs != 0) {
4071 err = -EINVAL;
4072 goto reg_err;
4073 }
4074 } else {
4075 oa_config->flex_regs_len = args->n_flex_regs;
Chris Wilsonc2fba932019-10-13 10:52:11 +01004076 regs = alloc_oa_regs(perf,
4077 perf->ops.is_valid_flex_reg,
4078 u64_to_user_ptr(args->flex_regs_ptr),
4079 args->n_flex_regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004080
Chris Wilsonc2fba932019-10-13 10:52:11 +01004081 if (IS_ERR(regs)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004082 DRM_DEBUG("Failed to create OA config for flex_regs\n");
Chris Wilsonc2fba932019-10-13 10:52:11 +01004083 err = PTR_ERR(regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004084 goto reg_err;
4085 }
Chris Wilsonc2fba932019-10-13 10:52:11 +01004086 oa_config->flex_regs = regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004087 }
4088
Chris Wilson8f8b1172019-10-07 22:09:41 +01004089 err = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004090 if (err)
4091 goto reg_err;
4092
4093 /* We shouldn't have too many configs, so this iteration shouldn't be
4094 * too costly.
4095 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004096 idr_for_each_entry(&perf->metrics_idr, tmp, id) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004097 if (!strcmp(tmp->uuid, oa_config->uuid)) {
4098 DRM_DEBUG("OA config already exists with this uuid\n");
4099 err = -EADDRINUSE;
4100 goto sysfs_err;
4101 }
4102 }
4103
Chris Wilson8f8b1172019-10-07 22:09:41 +01004104 err = create_dynamic_oa_sysfs_entry(perf, oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004105 if (err) {
4106 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
4107 goto sysfs_err;
4108 }
4109
4110 /* Config id 0 is invalid, id 1 for kernel stored test config. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004111 oa_config->id = idr_alloc(&perf->metrics_idr,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004112 oa_config, 2,
4113 0, GFP_KERNEL);
4114 if (oa_config->id < 0) {
4115 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
4116 err = oa_config->id;
4117 goto sysfs_err;
4118 }
4119
Chris Wilson8f8b1172019-10-07 22:09:41 +01004120 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004121
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01004122 DRM_DEBUG("Added config %s id=%i\n", oa_config->uuid, oa_config->id);
4123
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004124 return oa_config->id;
4125
4126sysfs_err:
Chris Wilson8f8b1172019-10-07 22:09:41 +01004127 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004128reg_err:
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004129 i915_oa_config_put(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004130 DRM_DEBUG("Failed to add new OA config\n");
4131 return err;
4132}
4133
4134/**
4135 * i915_perf_remove_config_ioctl - DRM ioctl() for userspace to remove an OA config
4136 * @dev: drm device
4137 * @data: ioctl data (pointer to u64 integer) copied from userspace
4138 * @file: drm file
4139 *
4140 * Configs can be removed while being used, the will stop appearing in sysfs
4141 * and their content will be freed when the stream using the config is closed.
4142 *
4143 * Returns: 0 on success or a negative error code on failure.
4144 */
4145int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data,
4146 struct drm_file *file)
4147{
Chris Wilson8f8b1172019-10-07 22:09:41 +01004148 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004149 u64 *arg = data;
4150 struct i915_oa_config *oa_config;
4151 int ret;
4152
Chris Wilson8f8b1172019-10-07 22:09:41 +01004153 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004154 DRM_DEBUG("i915 perf interface not available for this system\n");
4155 return -ENOTSUPP;
4156 }
4157
4158 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
4159 DRM_DEBUG("Insufficient privileges to remove i915 OA config\n");
4160 return -EACCES;
4161 }
4162
Chris Wilson8f8b1172019-10-07 22:09:41 +01004163 ret = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004164 if (ret)
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004165 return ret;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004166
Chris Wilson8f8b1172019-10-07 22:09:41 +01004167 oa_config = idr_find(&perf->metrics_idr, *arg);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004168 if (!oa_config) {
4169 DRM_DEBUG("Failed to remove unknown OA config\n");
4170 ret = -ENOENT;
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004171 goto err_unlock;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004172 }
4173
4174 GEM_BUG_ON(*arg != oa_config->id);
4175
Lionel Landwerlin4f6ccc72019-10-14 21:14:02 +01004176 sysfs_remove_group(perf->metrics_kobj, &oa_config->sysfs_metric);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004177
Chris Wilson8f8b1172019-10-07 22:09:41 +01004178 idr_remove(&perf->metrics_idr, *arg);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01004179
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004180 mutex_unlock(&perf->metrics_lock);
4181
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01004182 DRM_DEBUG("Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
4183
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004184 i915_oa_config_put(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004185
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004186 return 0;
4187
4188err_unlock:
Chris Wilson8f8b1172019-10-07 22:09:41 +01004189 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004190 return ret;
4191}
4192
Robert Braggccdf6342016-11-07 19:49:54 +00004193static struct ctl_table oa_table[] = {
4194 {
4195 .procname = "perf_stream_paranoid",
4196 .data = &i915_perf_stream_paranoid,
4197 .maxlen = sizeof(i915_perf_stream_paranoid),
4198 .mode = 0644,
4199 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07004200 .extra1 = SYSCTL_ZERO,
4201 .extra2 = SYSCTL_ONE,
Robert Braggccdf6342016-11-07 19:49:54 +00004202 },
Robert Bragg00319ba2016-11-07 19:49:55 +00004203 {
4204 .procname = "oa_max_sample_rate",
4205 .data = &i915_oa_max_sample_rate,
4206 .maxlen = sizeof(i915_oa_max_sample_rate),
4207 .mode = 0644,
4208 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07004209 .extra1 = SYSCTL_ZERO,
Robert Bragg00319ba2016-11-07 19:49:55 +00004210 .extra2 = &oa_sample_rate_hard_limit,
4211 },
Robert Braggccdf6342016-11-07 19:49:54 +00004212 {}
4213};
4214
4215static struct ctl_table i915_root[] = {
4216 {
4217 .procname = "i915",
4218 .maxlen = 0,
4219 .mode = 0555,
4220 .child = oa_table,
4221 },
4222 {}
4223};
4224
4225static struct ctl_table dev_root[] = {
4226 {
4227 .procname = "dev",
4228 .maxlen = 0,
4229 .mode = 0555,
4230 .child = i915_root,
4231 },
4232 {}
4233};
4234
Robert Bragg16d98b32016-12-07 21:40:33 +00004235/**
Venkata Sandeep Dhanalakota3dc716fd2019-12-13 07:51:51 -08004236 * i915_perf_init - initialize i915-perf state on module bind
Chris Wilson8f8b1172019-10-07 22:09:41 +01004237 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00004238 *
4239 * Initializes i915-perf state without exposing anything to userspace.
4240 *
4241 * Note: i915-perf initialization is split into an 'init' and 'register'
4242 * phase with the i915_perf_register() exposing state to userspace.
4243 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004244void i915_perf_init(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00004245{
Chris Wilson8f8b1172019-10-07 22:09:41 +01004246 struct i915_perf *perf = &i915->perf;
Robert Braggd7965152016-11-07 19:49:52 +00004247
Chris Wilson8f8b1172019-10-07 22:09:41 +01004248 /* XXX const struct i915_perf_ops! */
4249
4250 if (IS_HASWELL(i915)) {
4251 perf->ops.is_valid_b_counter_reg = gen7_is_valid_b_counter_addr;
4252 perf->ops.is_valid_mux_reg = hsw_is_valid_mux_addr;
4253 perf->ops.is_valid_flex_reg = NULL;
4254 perf->ops.enable_metric_set = hsw_enable_metric_set;
4255 perf->ops.disable_metric_set = hsw_disable_metric_set;
4256 perf->ops.oa_enable = gen7_oa_enable;
4257 perf->ops.oa_disable = gen7_oa_disable;
4258 perf->ops.read = gen7_oa_read;
4259 perf->ops.oa_hw_tail_read = gen7_oa_hw_tail_read;
4260
4261 perf->oa_formats = hsw_oa_formats;
4262 } else if (HAS_LOGICAL_RING_CONTEXTS(i915)) {
Robert Bragg19f81df2017-06-13 12:23:03 +01004263 /* Note: that although we could theoretically also support the
4264 * legacy ringbuffer mode on BDW (and earlier iterations of
4265 * this driver, before upstreaming did this) it didn't seem
4266 * worth the complexity to maintain now that BDW+ enable
4267 * execlist mode by default.
4268 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004269 perf->ops.read = gen8_oa_read;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01004270
Chris Wilson8f8b1172019-10-07 22:09:41 +01004271 if (IS_GEN_RANGE(i915, 8, 9)) {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004272 perf->oa_formats = gen8_plus_oa_formats;
4273
Chris Wilson8f8b1172019-10-07 22:09:41 +01004274 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004275 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004276 perf->ops.is_valid_mux_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004277 gen8_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004278 perf->ops.is_valid_flex_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004279 gen8_is_valid_flex_addr;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01004280
Chris Wilson8f8b1172019-10-07 22:09:41 +01004281 if (IS_CHERRYVIEW(i915)) {
4282 perf->ops.is_valid_mux_reg =
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004283 chv_is_valid_mux_addr;
4284 }
Robert Bragg155e9412017-06-13 12:23:05 +01004285
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004286 perf->ops.oa_enable = gen8_oa_enable;
4287 perf->ops.oa_disable = gen8_oa_disable;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004288 perf->ops.enable_metric_set = gen8_enable_metric_set;
4289 perf->ops.disable_metric_set = gen8_disable_metric_set;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004290 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004291
Chris Wilson8f8b1172019-10-07 22:09:41 +01004292 if (IS_GEN(i915, 8)) {
4293 perf->ctx_oactxctrl_offset = 0x120;
4294 perf->ctx_flexeu0_offset = 0x2ce;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004295
Chris Wilson8f8b1172019-10-07 22:09:41 +01004296 perf->gen8_valid_ctx_bit = BIT(25);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004297 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01004298 perf->ctx_oactxctrl_offset = 0x128;
4299 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004300
Chris Wilson8f8b1172019-10-07 22:09:41 +01004301 perf->gen8_valid_ctx_bit = BIT(16);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004302 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01004303 } else if (IS_GEN_RANGE(i915, 10, 11)) {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004304 perf->oa_formats = gen8_plus_oa_formats;
4305
Chris Wilson8f8b1172019-10-07 22:09:41 +01004306 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004307 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004308 perf->ops.is_valid_mux_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004309 gen10_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004310 perf->ops.is_valid_flex_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004311 gen8_is_valid_flex_addr;
4312
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004313 perf->ops.oa_enable = gen8_oa_enable;
4314 perf->ops.oa_disable = gen8_oa_disable;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004315 perf->ops.enable_metric_set = gen8_enable_metric_set;
4316 perf->ops.disable_metric_set = gen10_disable_metric_set;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004317 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004318
Chris Wilson8f8b1172019-10-07 22:09:41 +01004319 if (IS_GEN(i915, 10)) {
4320 perf->ctx_oactxctrl_offset = 0x128;
4321 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03004322 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01004323 perf->ctx_oactxctrl_offset = 0x124;
4324 perf->ctx_flexeu0_offset = 0x78e;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03004325 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01004326 perf->gen8_valid_ctx_bit = BIT(16);
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004327 } else if (IS_GEN(i915, 12)) {
4328 perf->oa_formats = gen12_oa_formats;
4329
4330 perf->ops.is_valid_b_counter_reg =
4331 gen12_is_valid_b_counter_addr;
4332 perf->ops.is_valid_mux_reg =
4333 gen12_is_valid_mux_addr;
4334 perf->ops.is_valid_flex_reg =
4335 gen8_is_valid_flex_addr;
4336
4337 perf->ops.oa_enable = gen12_oa_enable;
4338 perf->ops.oa_disable = gen12_oa_disable;
4339 perf->ops.enable_metric_set = gen12_enable_metric_set;
4340 perf->ops.disable_metric_set = gen12_disable_metric_set;
4341 perf->ops.oa_hw_tail_read = gen12_oa_hw_tail_read;
4342
4343 perf->ctx_flexeu0_offset = 0;
4344 perf->ctx_oactxctrl_offset = 0x144;
Robert Bragg19f81df2017-06-13 12:23:03 +01004345 }
Robert Bragg19f81df2017-06-13 12:23:03 +01004346 }
4347
Chris Wilson8f8b1172019-10-07 22:09:41 +01004348 if (perf->ops.enable_metric_set) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01004349 mutex_init(&perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01004350
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01004351 oa_sample_rate_hard_limit = 1000 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01004352 (RUNTIME_INFO(i915)->cs_timestamp_frequency_khz / 2);
Robert Bragg19f81df2017-06-13 12:23:03 +01004353
Chris Wilson8f8b1172019-10-07 22:09:41 +01004354 mutex_init(&perf->metrics_lock);
4355 idr_init(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004356
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07004357 /* We set up some ratelimit state to potentially throttle any
4358 * _NOTES about spurious, invalid OA reports which we don't
4359 * forward to userspace.
4360 *
4361 * We print a _NOTE about any throttling when closing the
4362 * stream instead of waiting until driver _fini which no one
4363 * would ever see.
4364 *
4365 * Using the same limiting factors as printk_ratelimit()
4366 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004367 ratelimit_state_init(&perf->spurious_report_rs, 5 * HZ, 10);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07004368 /* Since we use a DRM_NOTE for spurious reports it would be
4369 * inconsistent to let __ratelimit() automatically print a
4370 * warning for throttling.
4371 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004372 ratelimit_set_flags(&perf->spurious_report_rs,
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07004373 RATELIMIT_MSG_ON_RELEASE);
4374
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01004375 atomic64_set(&perf->noa_programming_delay,
4376 500 * 1000 /* 500us */);
4377
Chris Wilson8f8b1172019-10-07 22:09:41 +01004378 perf->i915 = i915;
Robert Bragg19f81df2017-06-13 12:23:03 +01004379 }
Robert Braggeec688e2016-11-07 19:49:47 +00004380}
4381
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004382static int destroy_config(int id, void *p, void *data)
4383{
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004384 i915_oa_config_put(p);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004385 return 0;
4386}
4387
Venkata Sandeep Dhanalakota3dc716fd2019-12-13 07:51:51 -08004388void i915_perf_sysctl_register(void)
4389{
4390 sysctl_header = register_sysctl_table(dev_root);
4391}
4392
4393void i915_perf_sysctl_unregister(void)
4394{
4395 unregister_sysctl_table(sysctl_header);
4396}
4397
Robert Bragg16d98b32016-12-07 21:40:33 +00004398/**
4399 * i915_perf_fini - Counter part to i915_perf_init()
Chris Wilson8f8b1172019-10-07 22:09:41 +01004400 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00004401 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004402void i915_perf_fini(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00004403{
Chris Wilson8f8b1172019-10-07 22:09:41 +01004404 struct i915_perf *perf = &i915->perf;
4405
4406 if (!perf->i915)
Robert Braggeec688e2016-11-07 19:49:47 +00004407 return;
4408
Chris Wilson8f8b1172019-10-07 22:09:41 +01004409 idr_for_each(&perf->metrics_idr, destroy_config, perf);
4410 idr_destroy(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004411
Chris Wilson8f8b1172019-10-07 22:09:41 +01004412 memset(&perf->ops, 0, sizeof(perf->ops));
4413 perf->i915 = NULL;
Robert Braggeec688e2016-11-07 19:49:47 +00004414}
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01004415
Lionel Landwerlinb8d49f22019-10-14 21:14:01 +01004416/**
4417 * i915_perf_ioctl_version - Version of the i915-perf subsystem
4418 *
4419 * This version number is used by userspace to detect available features.
4420 */
4421int i915_perf_ioctl_version(void)
4422{
Chris Wilson7831e9a2019-10-14 21:14:03 +01004423 /*
4424 * 1: Initial version
4425 * I915_PERF_IOCTL_ENABLE
4426 * I915_PERF_IOCTL_DISABLE
4427 *
4428 * 2: Added runtime modification of OA config.
4429 * I915_PERF_IOCTL_CONFIG
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01004430 *
4431 * 3: Add DRM_I915_PERF_PROP_HOLD_PREEMPTION parameter to hold
4432 * preemption on a particular context so that performance data is
4433 * accessible from a delta of MI_RPC reports without looking at the
4434 * OA buffer.
Chris Wilson7831e9a2019-10-14 21:14:03 +01004435 */
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01004436 return 3;
Lionel Landwerlinb8d49f22019-10-14 21:14:01 +01004437}
4438
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01004439#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
4440#include "selftests/i915_perf.c"
4441#endif