blob: e5973809b69aea3e46b75541263a8e8d27509401 [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"
199#include "gem/i915_gem_pm.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +0100200#include "gt/intel_lrc_reg.h"
201
Robert Braggeec688e2016-11-07 19:49:47 +0000202#include "i915_drv.h"
Jani Nikuladb94e9f2019-08-08 16:42:44 +0300203#include "i915_perf.h"
Michal Wajdeczko5ed7a0c2019-06-26 12:38:26 +0000204#include "oa/i915_oa_hsw.h"
205#include "oa/i915_oa_bdw.h"
206#include "oa/i915_oa_chv.h"
207#include "oa/i915_oa_sklgt2.h"
208#include "oa/i915_oa_sklgt3.h"
209#include "oa/i915_oa_sklgt4.h"
210#include "oa/i915_oa_bxt.h"
211#include "oa/i915_oa_kblgt2.h"
212#include "oa/i915_oa_kblgt3.h"
213#include "oa/i915_oa_glk.h"
214#include "oa/i915_oa_cflgt2.h"
215#include "oa/i915_oa_cflgt3.h"
216#include "oa/i915_oa_cnl.h"
217#include "oa/i915_oa_icl.h"
Robert Braggd7965152016-11-07 19:49:52 +0000218
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200219/* HW requires this to be a power of two, between 128k and 16M, though driver
220 * is currently generally designed assuming the largest 16M size is used such
221 * that the overflow cases are unlikely in normal operation.
222 */
223#define OA_BUFFER_SIZE SZ_16M
224
225#define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
Robert Braggd7965152016-11-07 19:49:52 +0000226
Robert Bragg0dd860c2017-05-11 16:43:28 +0100227/**
228 * DOC: OA Tail Pointer Race
229 *
230 * There's a HW race condition between OA unit tail pointer register updates and
Robert Braggd7965152016-11-07 19:49:52 +0000231 * writes to memory whereby the tail pointer can sometimes get ahead of what's
Robert Bragg0dd860c2017-05-11 16:43:28 +0100232 * been written out to the OA buffer so far (in terms of what's visible to the
233 * CPU).
Robert Braggd7965152016-11-07 19:49:52 +0000234 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100235 * Although this can be observed explicitly while copying reports to userspace
236 * by checking for a zeroed report-id field in tail reports, we want to account
Robert Bragg19f81df2017-06-13 12:23:03 +0100237 * for this earlier, as part of the oa_buffer_check to avoid lots of redundant
Robert Bragg0dd860c2017-05-11 16:43:28 +0100238 * read() attempts.
Robert Braggd7965152016-11-07 19:49:52 +0000239 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100240 * In effect we define a tail pointer for reading that lags the real tail
241 * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough
242 * time for the corresponding reports to become visible to the CPU.
Robert Braggd7965152016-11-07 19:49:52 +0000243 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100244 * To manage this we actually track two tail pointers:
245 * 1) An 'aging' tail with an associated timestamp that is tracked until we
246 * can trust the corresponding data is visible to the CPU; at which point
247 * it is considered 'aged'.
248 * 2) An 'aged' tail that can be used for read()ing.
249 *
250 * The two separate pointers let us decouple read()s from tail pointer aging.
251 *
252 * The tail pointers are checked and updated at a limited rate within a hrtimer
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800253 * callback (the same callback that is used for delivering EPOLLIN events)
Robert Bragg0dd860c2017-05-11 16:43:28 +0100254 *
255 * Initially the tails are marked invalid with %INVALID_TAIL_PTR which
256 * indicates that an updated tail pointer is needed.
257 *
258 * Most of the implementation details for this workaround are in
Robert Bragg19f81df2017-06-13 12:23:03 +0100259 * oa_buffer_check_unlocked() and _append_oa_reports()
Robert Bragg0dd860c2017-05-11 16:43:28 +0100260 *
261 * Note for posterity: previously the driver used to define an effective tail
262 * pointer that lagged the real pointer by a 'tail margin' measured in bytes
263 * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
264 * This was flawed considering that the OA unit may also automatically generate
265 * non-periodic reports (such as on context switch) or the OA unit may be
266 * enabled without any periodic sampling.
Robert Braggd7965152016-11-07 19:49:52 +0000267 */
268#define OA_TAIL_MARGIN_NSEC 100000ULL
Robert Bragg0dd860c2017-05-11 16:43:28 +0100269#define INVALID_TAIL_PTR 0xffffffff
Robert Braggd7965152016-11-07 19:49:52 +0000270
271/* frequency for checking whether the OA unit has written new reports to the
272 * circular OA buffer...
273 */
274#define POLL_FREQUENCY 200
275#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
276
Robert Braggccdf6342016-11-07 19:49:54 +0000277/* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
Robert Braggccdf6342016-11-07 19:49:54 +0000278static u32 i915_perf_stream_paranoid = true;
279
Robert Braggd7965152016-11-07 19:49:52 +0000280/* The maximum exponent the hardware accepts is 63 (essentially it selects one
281 * of the 64bit timestamp bits to trigger reports from) but there's currently
282 * no known use case for sampling as infrequently as once per 47 thousand years.
283 *
284 * Since the timestamps included in OA reports are only 32bits it seems
285 * reasonable to limit the OA exponent where it's still possible to account for
286 * overflow in OA report timestamps.
287 */
288#define OA_EXPONENT_MAX 31
289
290#define INVALID_CTX_ID 0xffffffff
291
Robert Bragg19f81df2017-06-13 12:23:03 +0100292/* On Gen8+ automatically triggered OA reports include a 'reason' field... */
293#define OAREPORT_REASON_MASK 0x3f
294#define OAREPORT_REASON_SHIFT 19
295#define OAREPORT_REASON_TIMER (1<<0)
296#define OAREPORT_REASON_CTX_SWITCH (1<<3)
297#define OAREPORT_REASON_CLK_RATIO (1<<5)
298
Robert Braggd7965152016-11-07 19:49:52 +0000299
Robert Bragg00319ba2016-11-07 19:49:55 +0000300/* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
301 *
Robert Bragg155e9412017-06-13 12:23:05 +0100302 * The highest sampling frequency we can theoretically program the OA unit
303 * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
304 *
305 * Initialized just before we register the sysctl parameter.
Robert Bragg00319ba2016-11-07 19:49:55 +0000306 */
Robert Bragg155e9412017-06-13 12:23:05 +0100307static int oa_sample_rate_hard_limit;
Robert Bragg00319ba2016-11-07 19:49:55 +0000308
309/* Theoretically we can program the OA unit to sample every 160ns but don't
310 * allow that by default unless root...
311 *
312 * The default threshold of 100000Hz is based on perf's similar
313 * kernel.perf_event_max_sample_rate sysctl parameter.
314 */
315static u32 i915_oa_max_sample_rate = 100000;
316
Robert Braggd7965152016-11-07 19:49:52 +0000317/* XXX: beware if future OA HW adds new report formats that the current
318 * code assumes all reports have a power-of-two size and ~(size - 1) can
319 * be used as a mask to align the OA tail pointer.
320 */
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300321static const struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Braggd7965152016-11-07 19:49:52 +0000322 [I915_OA_FORMAT_A13] = { 0, 64 },
323 [I915_OA_FORMAT_A29] = { 1, 128 },
324 [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
325 /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
326 [I915_OA_FORMAT_B4_C8] = { 4, 64 },
327 [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
328 [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
329 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
330};
331
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300332static const struct i915_oa_format gen8_plus_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Bragg19f81df2017-06-13 12:23:03 +0100333 [I915_OA_FORMAT_A12] = { 0, 64 },
334 [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
335 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
336 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
337};
338
Robert Braggd7965152016-11-07 19:49:52 +0000339#define SAMPLE_OA_REPORT (1<<0)
Robert Braggeec688e2016-11-07 19:49:47 +0000340
Robert Bragg16d98b32016-12-07 21:40:33 +0000341/**
342 * struct perf_open_properties - for validated properties given to open a stream
343 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
344 * @single_context: Whether a single or all gpu contexts should be monitored
345 * @ctx_handle: A gem ctx handle for use with @single_context
346 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
347 * @oa_format: An OA unit HW report format
348 * @oa_periodic: Whether to enable periodic OA unit sampling
349 * @oa_period_exponent: The OA unit sampling period is derived from this
350 *
351 * As read_properties_unlocked() enumerates and validates the properties given
352 * to open a stream of metrics the configuration is built up in the structure
353 * which starts out zero initialized.
354 */
Robert Braggeec688e2016-11-07 19:49:47 +0000355struct perf_open_properties {
356 u32 sample_flags;
357
358 u64 single_context:1;
359 u64 ctx_handle;
Robert Braggd7965152016-11-07 19:49:52 +0000360
361 /* OA sampling state */
362 int metrics_set;
363 int oa_format;
364 bool oa_periodic;
365 int oa_period_exponent;
Robert Braggeec688e2016-11-07 19:49:47 +0000366};
367
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700368static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer);
369
Chris Wilson8f8b1172019-10-07 22:09:41 +0100370static void free_oa_config(struct i915_oa_config *oa_config)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100371{
372 if (!PTR_ERR(oa_config->flex_regs))
373 kfree(oa_config->flex_regs);
374 if (!PTR_ERR(oa_config->b_counter_regs))
375 kfree(oa_config->b_counter_regs);
376 if (!PTR_ERR(oa_config->mux_regs))
377 kfree(oa_config->mux_regs);
378 kfree(oa_config);
379}
380
Chris Wilson8f8b1172019-10-07 22:09:41 +0100381static void put_oa_config(struct i915_oa_config *oa_config)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100382{
383 if (!atomic_dec_and_test(&oa_config->ref_count))
384 return;
385
Chris Wilson8f8b1172019-10-07 22:09:41 +0100386 free_oa_config(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100387}
388
Chris Wilson8f8b1172019-10-07 22:09:41 +0100389static int get_oa_config(struct i915_perf *perf,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100390 int metrics_set,
391 struct i915_oa_config **out_config)
392{
393 int ret;
394
395 if (metrics_set == 1) {
Chris Wilson8f8b1172019-10-07 22:09:41 +0100396 *out_config = &perf->test_config;
397 atomic_inc(&perf->test_config.ref_count);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100398 return 0;
399 }
400
Chris Wilson8f8b1172019-10-07 22:09:41 +0100401 ret = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100402 if (ret)
403 return ret;
404
Chris Wilson8f8b1172019-10-07 22:09:41 +0100405 *out_config = idr_find(&perf->metrics_idr, metrics_set);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100406 if (!*out_config)
407 ret = -EINVAL;
408 else
409 atomic_inc(&(*out_config)->ref_count);
410
Chris Wilson8f8b1172019-10-07 22:09:41 +0100411 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100412
413 return ret;
414}
415
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700416static u32 gen8_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100417{
Chris Wilson8f8b1172019-10-07 22:09:41 +0100418 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700419
Chris Wilson8f8b1172019-10-07 22:09:41 +0100420 return intel_uncore_read(uncore, GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
Robert Bragg19f81df2017-06-13 12:23:03 +0100421}
422
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700423static u32 gen7_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100424{
Chris Wilson8f8b1172019-10-07 22:09:41 +0100425 struct intel_uncore *uncore = stream->gt->uncore;
426 u32 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100427
428 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
429}
430
Robert Bragg0dd860c2017-05-11 16:43:28 +0100431/**
Robert Bragg19f81df2017-06-13 12:23:03 +0100432 * oa_buffer_check_unlocked - check for data and update tail ptr state
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700433 * @stream: i915 stream instance
Robert Braggd7965152016-11-07 19:49:52 +0000434 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100435 * This is either called via fops (for blocking reads in user ctx) or the poll
436 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
437 * if there is data available for userspace to read.
Robert Braggd7965152016-11-07 19:49:52 +0000438 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100439 * This function is central to providing a workaround for the OA unit tail
440 * pointer having a race with respect to what data is visible to the CPU.
441 * It is responsible for reading tail pointers from the hardware and giving
442 * the pointers time to 'age' before they are made available for reading.
443 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
444 *
445 * Besides returning true when there is data available to read() this function
446 * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp
447 * and .aged_tail_idx state used for reading.
448 *
449 * Note: It's safe to read OA config state here unlocked, assuming that this is
450 * only called while the stream is enabled, while the global OA configuration
451 * can't be modified.
452 *
453 * Returns: %true if the OA buffer contains data, else %false
Robert Braggd7965152016-11-07 19:49:52 +0000454 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700455static bool oa_buffer_check_unlocked(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +0000456{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700457 int report_size = stream->oa_buffer.format_size;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100458 unsigned long flags;
459 unsigned int aged_idx;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100460 u32 head, hw_tail, aged_tail, aging_tail;
461 u64 now;
Robert Braggd7965152016-11-07 19:49:52 +0000462
Robert Bragg0dd860c2017-05-11 16:43:28 +0100463 /* We have to consider the (unlikely) possibility that read() errors
464 * could result in an OA buffer reset which might reset the head,
465 * tails[] and aged_tail state.
466 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700467 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100468
469 /* NB: The head we observe here might effectively be a little out of
470 * date (between head and tails[aged_idx].offset if there is currently
471 * a read() in progress.
472 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700473 head = stream->oa_buffer.head;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100474
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700475 aged_idx = stream->oa_buffer.aged_tail_idx;
476 aged_tail = stream->oa_buffer.tails[aged_idx].offset;
477 aging_tail = stream->oa_buffer.tails[!aged_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100478
Chris Wilson8f8b1172019-10-07 22:09:41 +0100479 hw_tail = stream->perf->ops.oa_hw_tail_read(stream);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100480
481 /* The tail pointer increases in 64 byte increments,
482 * not in report_size steps...
483 */
484 hw_tail &= ~(report_size - 1);
485
486 now = ktime_get_mono_fast_ns();
487
Robert Bragg4117ebc2017-05-11 16:43:30 +0100488 /* Update the aged tail
489 *
490 * Flip the tail pointer available for read()s once the aging tail is
491 * old enough to trust that the corresponding data will be visible to
492 * the CPU...
493 *
494 * Do this before updating the aging pointer in case we may be able to
495 * immediately start aging a new pointer too (if new data has become
496 * available) without needing to wait for a later hrtimer callback.
497 */
498 if (aging_tail != INVALID_TAIL_PTR &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700499 ((now - stream->oa_buffer.aging_timestamp) >
Robert Bragg4117ebc2017-05-11 16:43:30 +0100500 OA_TAIL_MARGIN_NSEC)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100501
Robert Bragg4117ebc2017-05-11 16:43:30 +0100502 aged_idx ^= 1;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700503 stream->oa_buffer.aged_tail_idx = aged_idx;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100504
505 aged_tail = aging_tail;
506
507 /* Mark that we need a new pointer to start aging... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700508 stream->oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100509 aging_tail = INVALID_TAIL_PTR;
510 }
511
Robert Bragg0dd860c2017-05-11 16:43:28 +0100512 /* Update the aging tail
513 *
514 * We throttle aging tail updates until we have a new tail that
515 * represents >= one report more data than is already available for
516 * reading. This ensures there will be enough data for a successful
517 * read once this new pointer has aged and ensures we will give the new
518 * pointer time to age.
519 */
520 if (aging_tail == INVALID_TAIL_PTR &&
521 (aged_tail == INVALID_TAIL_PTR ||
522 OA_TAKEN(hw_tail, aged_tail) >= report_size)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700523 struct i915_vma *vma = stream->oa_buffer.vma;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100524 u32 gtt_offset = i915_ggtt_offset(vma);
525
526 /* Be paranoid and do a bounds check on the pointer read back
527 * from hardware, just in case some spurious hardware condition
528 * could put the tail out of bounds...
529 */
530 if (hw_tail >= gtt_offset &&
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200531 hw_tail < (gtt_offset + OA_BUFFER_SIZE)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700532 stream->oa_buffer.tails[!aged_idx].offset =
Robert Bragg0dd860c2017-05-11 16:43:28 +0100533 aging_tail = hw_tail;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700534 stream->oa_buffer.aging_timestamp = now;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100535 } else {
536 DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %u\n",
537 hw_tail);
538 }
539 }
540
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700541 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100542
543 return aged_tail == INVALID_TAIL_PTR ?
544 false : OA_TAKEN(aged_tail, head) >= report_size;
Robert Braggd7965152016-11-07 19:49:52 +0000545}
546
547/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000548 * append_oa_status - Appends a status record to a userspace read() buffer.
549 * @stream: An i915-perf stream opened for OA metrics
550 * @buf: destination buffer given by userspace
551 * @count: the number of bytes userspace wants to read
552 * @offset: (inout): the current position for writing into @buf
553 * @type: The kind of status to report to userspace
554 *
555 * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
556 * into the userspace read() buffer.
557 *
558 * The @buf @offset will only be updated on success.
559 *
560 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000561 */
562static int append_oa_status(struct i915_perf_stream *stream,
563 char __user *buf,
564 size_t count,
565 size_t *offset,
566 enum drm_i915_perf_record_type type)
567{
568 struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
569
570 if ((count - *offset) < header.size)
571 return -ENOSPC;
572
573 if (copy_to_user(buf + *offset, &header, sizeof(header)))
574 return -EFAULT;
575
576 (*offset) += header.size;
577
578 return 0;
579}
580
581/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000582 * append_oa_sample - Copies single OA report into userspace read() buffer.
583 * @stream: An i915-perf stream opened for OA metrics
584 * @buf: destination buffer given by userspace
585 * @count: the number of bytes userspace wants to read
586 * @offset: (inout): the current position for writing into @buf
587 * @report: A single OA report to (optionally) include as part of the sample
588 *
589 * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
590 * properties when opening a stream, tracked as `stream->sample_flags`. This
591 * function copies the requested components of a single sample to the given
592 * read() @buf.
593 *
594 * The @buf @offset will only be updated on success.
595 *
596 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000597 */
598static int append_oa_sample(struct i915_perf_stream *stream,
599 char __user *buf,
600 size_t count,
601 size_t *offset,
602 const u8 *report)
603{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700604 int report_size = stream->oa_buffer.format_size;
Robert Braggd7965152016-11-07 19:49:52 +0000605 struct drm_i915_perf_record_header header;
606 u32 sample_flags = stream->sample_flags;
607
608 header.type = DRM_I915_PERF_RECORD_SAMPLE;
609 header.pad = 0;
610 header.size = stream->sample_size;
611
612 if ((count - *offset) < header.size)
613 return -ENOSPC;
614
615 buf += *offset;
616 if (copy_to_user(buf, &header, sizeof(header)))
617 return -EFAULT;
618 buf += sizeof(header);
619
620 if (sample_flags & SAMPLE_OA_REPORT) {
621 if (copy_to_user(buf, report, report_size))
622 return -EFAULT;
623 }
624
625 (*offset) += header.size;
626
627 return 0;
628}
629
630/**
631 * Copies all buffered OA reports into userspace read() buffer.
632 * @stream: An i915-perf stream opened for OA metrics
633 * @buf: destination buffer given by userspace
634 * @count: the number of bytes userspace wants to read
635 * @offset: (inout): the current position for writing into @buf
Robert Braggd7965152016-11-07 19:49:52 +0000636 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000637 * Notably any error condition resulting in a short read (-%ENOSPC or
638 * -%EFAULT) will be returned even though one or more records may
Robert Braggd7965152016-11-07 19:49:52 +0000639 * have been successfully copied. In this case it's up to the caller
640 * to decide if the error should be squashed before returning to
641 * userspace.
642 *
643 * Note: reports are consumed from the head, and appended to the
Robert Bragge81b3a52017-05-11 16:43:24 +0100644 * tail, so the tail chases the head?... If you think that's mad
Robert Braggd7965152016-11-07 19:49:52 +0000645 * and back-to-front you're not alone, but this follows the
646 * Gen PRM naming convention.
Robert Bragg16d98b32016-12-07 21:40:33 +0000647 *
648 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000649 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100650static int gen8_append_oa_reports(struct i915_perf_stream *stream,
651 char __user *buf,
652 size_t count,
653 size_t *offset)
654{
Chris Wilson8f8b1172019-10-07 22:09:41 +0100655 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700656 int report_size = stream->oa_buffer.format_size;
657 u8 *oa_buf_base = stream->oa_buffer.vaddr;
658 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200659 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100660 size_t start_offset = *offset;
661 unsigned long flags;
662 unsigned int aged_tail_idx;
663 u32 head, tail;
664 u32 taken;
665 int ret = 0;
666
667 if (WARN_ON(!stream->enabled))
668 return -EIO;
669
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700670 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100671
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700672 head = stream->oa_buffer.head;
673 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
674 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg19f81df2017-06-13 12:23:03 +0100675
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700676 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100677
678 /*
679 * An invalid tail pointer here means we're still waiting for the poll
680 * hrtimer callback to give us a pointer
681 */
682 if (tail == INVALID_TAIL_PTR)
683 return -EAGAIN;
684
685 /*
686 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
687 * while indexing relative to oa_buf_base.
688 */
689 head -= gtt_offset;
690 tail -= gtt_offset;
691
692 /*
693 * An out of bounds or misaligned head or tail pointer implies a driver
694 * bug since we validate + align the tail pointers we read from the
695 * hardware and we are in full control of the head pointer which should
696 * only be incremented by multiples of the report size (notably also
697 * all a power of two).
698 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200699 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
700 tail > OA_BUFFER_SIZE || tail % report_size,
Robert Bragg19f81df2017-06-13 12:23:03 +0100701 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
702 head, tail))
703 return -EIO;
704
705
706 for (/* none */;
707 (taken = OA_TAKEN(tail, head));
708 head = (head + report_size) & mask) {
709 u8 *report = oa_buf_base + head;
710 u32 *report32 = (void *)report;
711 u32 ctx_id;
712 u32 reason;
713
714 /*
715 * All the report sizes factor neatly into the buffer
716 * size so we never expect to see a report split
717 * between the beginning and end of the buffer.
718 *
719 * Given the initial alignment check a misalignment
720 * here would imply a driver bug that would result
721 * in an overrun.
722 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200723 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100724 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
725 break;
726 }
727
728 /*
729 * The reason field includes flags identifying what
730 * triggered this specific report (mostly timer
731 * triggered or e.g. due to a context switch).
732 *
733 * This field is never expected to be zero so we can
734 * check that the report isn't invalid before copying
735 * it to userspace...
736 */
737 reason = ((report32[0] >> OAREPORT_REASON_SHIFT) &
738 OAREPORT_REASON_MASK);
739 if (reason == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +0100740 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg19f81df2017-06-13 12:23:03 +0100741 DRM_NOTE("Skipping spurious, invalid OA report\n");
742 continue;
743 }
744
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700745 ctx_id = report32[2] & stream->specific_ctx_id_mask;
Robert Bragg19f81df2017-06-13 12:23:03 +0100746
747 /*
748 * Squash whatever is in the CTX_ID field if it's marked as
749 * invalid to be sure we avoid false-positive, single-context
750 * filtering below...
751 *
752 * Note: that we don't clear the valid_ctx_bit so userspace can
753 * understand that the ID has been squashed by the kernel.
754 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100755 if (!(report32[0] & stream->perf->gen8_valid_ctx_bit))
Robert Bragg19f81df2017-06-13 12:23:03 +0100756 ctx_id = report32[2] = INVALID_CTX_ID;
757
758 /*
759 * NB: For Gen 8 the OA unit no longer supports clock gating
760 * off for a specific context and the kernel can't securely
761 * stop the counters from updating as system-wide / global
762 * values.
763 *
764 * Automatic reports now include a context ID so reports can be
765 * filtered on the cpu but it's not worth trying to
766 * automatically subtract/hide counter progress for other
767 * contexts while filtering since we can't stop userspace
768 * issuing MI_REPORT_PERF_COUNT commands which would still
769 * provide a side-band view of the real values.
770 *
771 * To allow userspace (such as Mesa/GL_INTEL_performance_query)
772 * to normalize counters for a single filtered context then it
773 * needs be forwarded bookend context-switch reports so that it
774 * can track switches in between MI_REPORT_PERF_COUNT commands
775 * and can itself subtract/ignore the progress of counters
776 * associated with other contexts. Note that the hardware
777 * automatically triggers reports when switching to a new
778 * context which are tagged with the ID of the newly active
779 * context. To avoid the complexity (and likely fragility) of
780 * reading ahead while parsing reports to try and minimize
781 * forwarding redundant context switch reports (i.e. between
782 * other, unrelated contexts) we simply elect to forward them
783 * all.
784 *
785 * We don't rely solely on the reason field to identify context
786 * switches since it's not-uncommon for periodic samples to
787 * identify a switch before any 'context switch' report.
788 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100789 if (!stream->perf->exclusive_stream->ctx ||
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700790 stream->specific_ctx_id == ctx_id ||
791 stream->oa_buffer.last_ctx_id == stream->specific_ctx_id ||
Robert Bragg19f81df2017-06-13 12:23:03 +0100792 reason & OAREPORT_REASON_CTX_SWITCH) {
793
794 /*
795 * While filtering for a single context we avoid
796 * leaking the IDs of other contexts.
797 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100798 if (stream->perf->exclusive_stream->ctx &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700799 stream->specific_ctx_id != ctx_id) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100800 report32[2] = INVALID_CTX_ID;
801 }
802
803 ret = append_oa_sample(stream, buf, count, offset,
804 report);
805 if (ret)
806 break;
807
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700808 stream->oa_buffer.last_ctx_id = ctx_id;
Robert Bragg19f81df2017-06-13 12:23:03 +0100809 }
810
811 /*
812 * The above reason field sanity check is based on
813 * the assumption that the OA buffer is initially
814 * zeroed and we reset the field after copying so the
815 * check is still meaningful once old reports start
816 * being overwritten.
817 */
818 report32[0] = 0;
819 }
820
821 if (start_offset != *offset) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700822 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100823
824 /*
825 * We removed the gtt_offset for the copy loop above, indexing
826 * relative to oa_buf_base so put back here...
827 */
828 head += gtt_offset;
829
Chris Wilson8f8b1172019-10-07 22:09:41 +0100830 intel_uncore_write(uncore, GEN8_OAHEADPTR,
831 head & GEN8_OAHEADPTR_MASK);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700832 stream->oa_buffer.head = head;
Robert Bragg19f81df2017-06-13 12:23:03 +0100833
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700834 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100835 }
836
837 return ret;
838}
839
840/**
841 * gen8_oa_read - copy status records then buffered OA reports
842 * @stream: An i915-perf stream opened for OA metrics
843 * @buf: destination buffer given by userspace
844 * @count: the number of bytes userspace wants to read
845 * @offset: (inout): the current position for writing into @buf
846 *
847 * Checks OA unit status registers and if necessary appends corresponding
848 * status records for userspace (such as for a buffer full condition) and then
849 * initiate appending any buffered OA reports.
850 *
851 * Updates @offset according to the number of bytes successfully copied into
852 * the userspace buffer.
853 *
854 * NB: some data may be successfully copied to the userspace buffer
855 * even if an error is returned, and this is reflected in the
856 * updated @offset.
857 *
858 * Returns: zero on success or a negative error code
859 */
860static int gen8_oa_read(struct i915_perf_stream *stream,
861 char __user *buf,
862 size_t count,
863 size_t *offset)
864{
Chris Wilson8f8b1172019-10-07 22:09:41 +0100865 struct intel_uncore *uncore = stream->gt->uncore;
Robert Bragg19f81df2017-06-13 12:23:03 +0100866 u32 oastatus;
867 int ret;
868
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700869 if (WARN_ON(!stream->oa_buffer.vaddr))
Robert Bragg19f81df2017-06-13 12:23:03 +0100870 return -EIO;
871
Chris Wilson8f8b1172019-10-07 22:09:41 +0100872 oastatus = intel_uncore_read(uncore, GEN8_OASTATUS);
Robert Bragg19f81df2017-06-13 12:23:03 +0100873
874 /*
875 * We treat OABUFFER_OVERFLOW as a significant error:
876 *
877 * Although theoretically we could handle this more gracefully
878 * sometimes, some Gens don't correctly suppress certain
879 * automatically triggered reports in this condition and so we
880 * have to assume that old reports are now being trampled
881 * over.
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200882 *
883 * Considering how we don't currently give userspace control
884 * over the OA buffer size and always configure a large 16MB
885 * buffer, then a buffer overflow does anyway likely indicate
886 * that something has gone quite badly wrong.
Robert Bragg19f81df2017-06-13 12:23:03 +0100887 */
888 if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
889 ret = append_oa_status(stream, buf, count, offset,
890 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
891 if (ret)
892 return ret;
893
894 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700895 stream->period_exponent);
Robert Bragg19f81df2017-06-13 12:23:03 +0100896
Chris Wilson8f8b1172019-10-07 22:09:41 +0100897 stream->perf->ops.oa_disable(stream);
898 stream->perf->ops.oa_enable(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +0100899
900 /*
901 * Note: .oa_enable() is expected to re-init the oabuffer and
902 * reset GEN8_OASTATUS for us
903 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100904 oastatus = intel_uncore_read(uncore, GEN8_OASTATUS);
Robert Bragg19f81df2017-06-13 12:23:03 +0100905 }
906
907 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
908 ret = append_oa_status(stream, buf, count, offset,
909 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
910 if (ret)
911 return ret;
Chris Wilson8f8b1172019-10-07 22:09:41 +0100912 intel_uncore_write(uncore, GEN8_OASTATUS,
913 oastatus & ~GEN8_OASTATUS_REPORT_LOST);
Robert Bragg19f81df2017-06-13 12:23:03 +0100914 }
915
916 return gen8_append_oa_reports(stream, buf, count, offset);
917}
918
919/**
920 * Copies all buffered OA reports into userspace read() buffer.
921 * @stream: An i915-perf stream opened for OA metrics
922 * @buf: destination buffer given by userspace
923 * @count: the number of bytes userspace wants to read
924 * @offset: (inout): the current position for writing into @buf
925 *
926 * Notably any error condition resulting in a short read (-%ENOSPC or
927 * -%EFAULT) will be returned even though one or more records may
928 * have been successfully copied. In this case it's up to the caller
929 * to decide if the error should be squashed before returning to
930 * userspace.
931 *
932 * Note: reports are consumed from the head, and appended to the
933 * tail, so the tail chases the head?... If you think that's mad
934 * and back-to-front you're not alone, but this follows the
935 * Gen PRM naming convention.
936 *
937 * Returns: 0 on success, negative error code on failure.
938 */
Robert Braggd7965152016-11-07 19:49:52 +0000939static int gen7_append_oa_reports(struct i915_perf_stream *stream,
940 char __user *buf,
941 size_t count,
Robert Bragg3bb335c2017-05-11 16:43:27 +0100942 size_t *offset)
Robert Braggd7965152016-11-07 19:49:52 +0000943{
Chris Wilson8f8b1172019-10-07 22:09:41 +0100944 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700945 int report_size = stream->oa_buffer.format_size;
946 u8 *oa_buf_base = stream->oa_buffer.vaddr;
947 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200948 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100949 size_t start_offset = *offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100950 unsigned long flags;
951 unsigned int aged_tail_idx;
952 u32 head, tail;
Robert Braggd7965152016-11-07 19:49:52 +0000953 u32 taken;
954 int ret = 0;
955
956 if (WARN_ON(!stream->enabled))
957 return -EIO;
958
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700959 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggf2790202017-05-11 16:43:26 +0100960
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700961 head = stream->oa_buffer.head;
962 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
963 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100964
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700965 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100966
967 /* An invalid tail pointer here means we're still waiting for the poll
968 * hrtimer callback to give us a pointer
Robert Braggf2790202017-05-11 16:43:26 +0100969 */
Robert Bragg0dd860c2017-05-11 16:43:28 +0100970 if (tail == INVALID_TAIL_PTR)
Robert Braggd7965152016-11-07 19:49:52 +0000971 return -EAGAIN;
972
Robert Bragg0dd860c2017-05-11 16:43:28 +0100973 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
974 * while indexing relative to oa_buf_base.
975 */
976 head -= gtt_offset;
977 tail -= gtt_offset;
978
979 /* An out of bounds or misaligned head or tail pointer implies a driver
980 * bug since we validate + align the tail pointers we read from the
981 * hardware and we are in full control of the head pointer which should
982 * only be incremented by multiples of the report size (notably also
983 * all a power of two).
984 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200985 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
986 tail > OA_BUFFER_SIZE || tail % report_size,
Robert Bragg0dd860c2017-05-11 16:43:28 +0100987 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
988 head, tail))
989 return -EIO;
990
Robert Braggd7965152016-11-07 19:49:52 +0000991
992 for (/* none */;
993 (taken = OA_TAKEN(tail, head));
994 head = (head + report_size) & mask) {
995 u8 *report = oa_buf_base + head;
996 u32 *report32 = (void *)report;
997
998 /* All the report sizes factor neatly into the buffer
999 * size so we never expect to see a report split
1000 * between the beginning and end of the buffer.
1001 *
1002 * Given the initial alignment check a misalignment
1003 * here would imply a driver bug that would result
1004 * in an overrun.
1005 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001006 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
Robert Braggd7965152016-11-07 19:49:52 +00001007 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
1008 break;
1009 }
1010
1011 /* The report-ID field for periodic samples includes
1012 * some undocumented flags related to what triggered
1013 * the report and is never expected to be zero so we
1014 * can check that the report isn't invalid before
1015 * copying it to userspace...
1016 */
1017 if (report32[0] == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01001018 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg712122e2017-05-11 16:43:31 +01001019 DRM_NOTE("Skipping spurious, invalid OA report\n");
Robert Braggd7965152016-11-07 19:49:52 +00001020 continue;
1021 }
1022
1023 ret = append_oa_sample(stream, buf, count, offset, report);
1024 if (ret)
1025 break;
1026
1027 /* The above report-id field sanity check is based on
1028 * the assumption that the OA buffer is initially
1029 * zeroed and we reset the field after copying so the
1030 * check is still meaningful once old reports start
1031 * being overwritten.
1032 */
1033 report32[0] = 0;
1034 }
1035
Robert Bragg3bb335c2017-05-11 16:43:27 +01001036 if (start_offset != *offset) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001037 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001038
Robert Bragg3bb335c2017-05-11 16:43:27 +01001039 /* We removed the gtt_offset for the copy loop above, indexing
1040 * relative to oa_buf_base so put back here...
1041 */
1042 head += gtt_offset;
1043
Chris Wilson8f8b1172019-10-07 22:09:41 +01001044 intel_uncore_write(uncore, GEN7_OASTATUS2,
1045 (head & GEN7_OASTATUS2_HEAD_MASK) |
1046 GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001047 stream->oa_buffer.head = head;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001048
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001049 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg3bb335c2017-05-11 16:43:27 +01001050 }
Robert Braggd7965152016-11-07 19:49:52 +00001051
1052 return ret;
1053}
1054
Robert Bragg16d98b32016-12-07 21:40:33 +00001055/**
1056 * gen7_oa_read - copy status records then buffered OA reports
1057 * @stream: An i915-perf stream opened for OA metrics
1058 * @buf: destination buffer given by userspace
1059 * @count: the number of bytes userspace wants to read
1060 * @offset: (inout): the current position for writing into @buf
1061 *
1062 * Checks Gen 7 specific OA unit status registers and if necessary appends
1063 * corresponding status records for userspace (such as for a buffer full
1064 * condition) and then initiate appending any buffered OA reports.
1065 *
1066 * Updates @offset according to the number of bytes successfully copied into
1067 * the userspace buffer.
1068 *
1069 * Returns: zero on success or a negative error code
1070 */
Robert Braggd7965152016-11-07 19:49:52 +00001071static int gen7_oa_read(struct i915_perf_stream *stream,
1072 char __user *buf,
1073 size_t count,
1074 size_t *offset)
1075{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001076 struct intel_uncore *uncore = stream->gt->uncore;
Robert Braggd7965152016-11-07 19:49:52 +00001077 u32 oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001078 int ret;
1079
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001080 if (WARN_ON(!stream->oa_buffer.vaddr))
Robert Braggd7965152016-11-07 19:49:52 +00001081 return -EIO;
1082
Chris Wilson8f8b1172019-10-07 22:09:41 +01001083 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001084
Robert Braggd7965152016-11-07 19:49:52 +00001085 /* XXX: On Haswell we don't have a safe way to clear oastatus1
1086 * bits while the OA unit is enabled (while the tail pointer
1087 * may be updated asynchronously) so we ignore status bits
1088 * that have already been reported to userspace.
1089 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001090 oastatus1 &= ~stream->perf->gen7_latched_oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001091
1092 /* We treat OABUFFER_OVERFLOW as a significant error:
1093 *
1094 * - The status can be interpreted to mean that the buffer is
1095 * currently full (with a higher precedence than OA_TAKEN()
1096 * which will start to report a near-empty buffer after an
1097 * overflow) but it's awkward that we can't clear the status
1098 * on Haswell, so without a reset we won't be able to catch
1099 * the state again.
1100 *
1101 * - Since it also implies the HW has started overwriting old
1102 * reports it may also affect our sanity checks for invalid
1103 * reports when copying to userspace that assume new reports
1104 * are being written to cleared memory.
1105 *
1106 * - In the future we may want to introduce a flight recorder
1107 * mode where the driver will automatically maintain a safe
1108 * guard band between head/tail, avoiding this overflow
1109 * condition, but we avoid the added driver complexity for
1110 * now.
1111 */
1112 if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
1113 ret = append_oa_status(stream, buf, count, offset,
1114 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
1115 if (ret)
1116 return ret;
1117
Robert Bragg19f81df2017-06-13 12:23:03 +01001118 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001119 stream->period_exponent);
Robert Braggd7965152016-11-07 19:49:52 +00001120
Chris Wilson8f8b1172019-10-07 22:09:41 +01001121 stream->perf->ops.oa_disable(stream);
1122 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001123
Chris Wilson8f8b1172019-10-07 22:09:41 +01001124 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001125 }
1126
1127 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
1128 ret = append_oa_status(stream, buf, count, offset,
1129 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1130 if (ret)
1131 return ret;
Chris Wilson8f8b1172019-10-07 22:09:41 +01001132 stream->perf->gen7_latched_oastatus1 |=
Robert Braggd7965152016-11-07 19:49:52 +00001133 GEN7_OASTATUS1_REPORT_LOST;
1134 }
1135
Robert Bragg3bb335c2017-05-11 16:43:27 +01001136 return gen7_append_oa_reports(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001137}
1138
Robert Bragg16d98b32016-12-07 21:40:33 +00001139/**
1140 * i915_oa_wait_unlocked - handles blocking IO until OA data available
1141 * @stream: An i915-perf stream opened for OA metrics
1142 *
1143 * Called when userspace tries to read() from a blocking stream FD opened
1144 * for OA metrics. It waits until the hrtimer callback finds a non-empty
1145 * OA buffer and wakes us.
1146 *
1147 * Note: it's acceptable to have this return with some false positives
1148 * since any subsequent read handling will return -EAGAIN if there isn't
1149 * really data ready for userspace yet.
1150 *
1151 * Returns: zero on success or a negative error code
1152 */
Robert Braggd7965152016-11-07 19:49:52 +00001153static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
1154{
Robert Braggd7965152016-11-07 19:49:52 +00001155 /* We would wait indefinitely if periodic sampling is not enabled */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001156 if (!stream->periodic)
Robert Braggd7965152016-11-07 19:49:52 +00001157 return -EIO;
1158
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001159 return wait_event_interruptible(stream->poll_wq,
1160 oa_buffer_check_unlocked(stream));
Robert Braggd7965152016-11-07 19:49:52 +00001161}
1162
Robert Bragg16d98b32016-12-07 21:40:33 +00001163/**
1164 * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
1165 * @stream: An i915-perf stream opened for OA metrics
1166 * @file: An i915 perf stream file
1167 * @wait: poll() state table
1168 *
1169 * For handling userspace polling on an i915 perf stream opened for OA metrics,
1170 * this starts a poll_wait with the wait queue that our hrtimer callback wakes
1171 * when it sees data ready to read in the circular OA buffer.
1172 */
Robert Braggd7965152016-11-07 19:49:52 +00001173static void i915_oa_poll_wait(struct i915_perf_stream *stream,
1174 struct file *file,
1175 poll_table *wait)
1176{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001177 poll_wait(file, &stream->poll_wq, wait);
Robert Braggd7965152016-11-07 19:49:52 +00001178}
1179
Robert Bragg16d98b32016-12-07 21:40:33 +00001180/**
1181 * i915_oa_read - just calls through to &i915_oa_ops->read
1182 * @stream: An i915-perf stream opened for OA metrics
1183 * @buf: destination buffer given by userspace
1184 * @count: the number of bytes userspace wants to read
1185 * @offset: (inout): the current position for writing into @buf
1186 *
1187 * Updates @offset according to the number of bytes successfully copied into
1188 * the userspace buffer.
1189 *
1190 * Returns: zero on success or a negative error code
1191 */
Robert Braggd7965152016-11-07 19:49:52 +00001192static int i915_oa_read(struct i915_perf_stream *stream,
1193 char __user *buf,
1194 size_t count,
1195 size_t *offset)
1196{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001197 return stream->perf->ops.read(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001198}
1199
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001200static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001201{
Chris Wilson5e2a0412019-04-26 17:33:34 +01001202 struct i915_gem_engines_iter it;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001203 struct i915_gem_context *ctx = stream->ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001204 struct intel_context *ce;
Chris Wilsonfa9f6682019-04-26 17:33:29 +01001205 int err;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001206
Chris Wilson5e2a0412019-04-26 17:33:34 +01001207 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1208 if (ce->engine->class != RENDER_CLASS)
1209 continue;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001210
Chris Wilson5e2a0412019-04-26 17:33:34 +01001211 /*
1212 * As the ID is the gtt offset of the context's vma we
1213 * pin the vma to ensure the ID remains fixed.
1214 */
1215 err = intel_context_pin(ce);
1216 if (err == 0) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001217 stream->pinned_ctx = ce;
Chris Wilson5e2a0412019-04-26 17:33:34 +01001218 break;
1219 }
1220 }
1221 i915_gem_context_unlock_engines(ctx);
1222
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001223 return stream->pinned_ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001224}
1225
Robert Bragg16d98b32016-12-07 21:40:33 +00001226/**
1227 * oa_get_render_ctx_id - determine and hold ctx hw id
1228 * @stream: An i915-perf stream opened for OA metrics
1229 *
1230 * Determine the render context hw id, and ensure it remains fixed for the
Robert Braggd7965152016-11-07 19:49:52 +00001231 * lifetime of the stream. This ensures that we don't have to worry about
1232 * updating the context ID in OACONTROL on the fly.
Robert Bragg16d98b32016-12-07 21:40:33 +00001233 *
1234 * Returns: zero on success or a negative error code
Robert Braggd7965152016-11-07 19:49:52 +00001235 */
1236static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
1237{
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001238 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001239
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001240 ce = oa_pin_context(stream);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001241 if (IS_ERR(ce))
1242 return PTR_ERR(ce);
Robert Braggd7965152016-11-07 19:49:52 +00001243
Chris Wilson8f8b1172019-10-07 22:09:41 +01001244 switch (INTEL_GEN(ce->engine->i915)) {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001245 case 7: {
Robert Bragg19f81df2017-06-13 12:23:03 +01001246 /*
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001247 * On Haswell we don't do any post processing of the reports
1248 * and don't need to use the mask.
Robert Bragg19f81df2017-06-13 12:23:03 +01001249 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001250 stream->specific_ctx_id = i915_ggtt_offset(ce->state);
1251 stream->specific_ctx_id_mask = 0;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001252 break;
Robert Bragg19f81df2017-06-13 12:23:03 +01001253 }
Robert Braggd7965152016-11-07 19:49:52 +00001254
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001255 case 8:
1256 case 9:
1257 case 10:
Chris Wilson8f8b1172019-10-07 22:09:41 +01001258 if (USES_GUC_SUBMISSION(ce->engine->i915)) {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001259 /*
1260 * When using GuC, the context descriptor we write in
1261 * i915 is read by GuC and rewritten before it's
1262 * actually written into the hardware. The LRCA is
1263 * what is put into the context id field of the
1264 * context descriptor by GuC. Because it's aligned to
1265 * a page, the lower 12bits are always at 0 and
1266 * dropped by GuC. They won't be part of the context
1267 * ID in the OA reports, so squash those lower bits.
1268 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001269 stream->specific_ctx_id =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001270 lower_32_bits(ce->lrc_desc) >> 12;
1271
1272 /*
1273 * GuC uses the top bit to signal proxy submission, so
1274 * ignore that bit.
1275 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001276 stream->specific_ctx_id_mask =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001277 (1U << (GEN8_CTX_ID_WIDTH - 1)) - 1;
1278 } else {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001279 stream->specific_ctx_id_mask =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001280 (1U << GEN8_CTX_ID_WIDTH) - 1;
Chris Wilson2935ed52019-10-04 14:40:08 +01001281 stream->specific_ctx_id = stream->specific_ctx_id_mask;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001282 }
1283 break;
1284
Michel Thierry45e9c822019-08-23 01:20:50 -07001285 case 11:
1286 case 12: {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001287 stream->specific_ctx_id_mask =
Chris Wilson2935ed52019-10-04 14:40:08 +01001288 ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) << (GEN11_SW_CTX_ID_SHIFT - 32);
1289 stream->specific_ctx_id = stream->specific_ctx_id_mask;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001290 break;
1291 }
1292
1293 default:
Chris Wilson8f8b1172019-10-07 22:09:41 +01001294 MISSING_CASE(INTEL_GEN(ce->engine->i915));
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001295 }
1296
Chris Wilson2935ed52019-10-04 14:40:08 +01001297 ce->tag = stream->specific_ctx_id_mask;
1298
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001299 DRM_DEBUG_DRIVER("filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001300 stream->specific_ctx_id,
1301 stream->specific_ctx_id_mask);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001302
Chris Wilson266a2402017-05-04 10:33:08 +01001303 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001304}
1305
Robert Bragg16d98b32016-12-07 21:40:33 +00001306/**
1307 * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
1308 * @stream: An i915-perf stream opened for OA metrics
1309 *
1310 * In case anything needed doing to ensure the context HW ID would remain valid
1311 * for the lifetime of the stream, then that can be undone here.
1312 */
Robert Braggd7965152016-11-07 19:49:52 +00001313static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
1314{
Chris Wilson1fc44d92018-05-17 22:26:32 +01001315 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001316
Chris Wilson2935ed52019-10-04 14:40:08 +01001317 ce = fetch_and_zero(&stream->pinned_ctx);
1318 if (ce) {
1319 ce->tag = 0; /* recomputed on next submission after parking */
1320 intel_context_unpin(ce);
1321 }
1322
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001323 stream->specific_ctx_id = INVALID_CTX_ID;
1324 stream->specific_ctx_id_mask = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001325}
1326
1327static void
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001328free_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001329{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001330 i915_vma_unpin_and_release(&stream->oa_buffer.vma,
Chris Wilson6a2f59e2018-07-21 13:50:37 +01001331 I915_VMA_RELEASE_MAP);
Robert Braggd7965152016-11-07 19:49:52 +00001332
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001333 stream->oa_buffer.vaddr = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001334}
1335
1336static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
1337{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001338 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00001339
Chris Wilson8f8b1172019-10-07 22:09:41 +01001340 BUG_ON(stream != perf->exclusive_stream);
Robert Braggd7965152016-11-07 19:49:52 +00001341
Robert Bragg19f81df2017-06-13 12:23:03 +01001342 /*
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001343 * Unset exclusive_stream first, it will be checked while disabling
1344 * the metric set on gen8+.
Robert Bragg19f81df2017-06-13 12:23:03 +01001345 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001346 perf->exclusive_stream = NULL;
1347 perf->ops.disable_metric_set(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001348
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001349 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001350
Chris Wilson8f8b1172019-10-07 22:09:41 +01001351 intel_uncore_forcewake_put(stream->gt->uncore, FORCEWAKE_ALL);
1352 intel_runtime_pm_put(stream->gt->uncore->rpm, stream->wakeref);
Robert Braggd7965152016-11-07 19:49:52 +00001353
1354 if (stream->ctx)
1355 oa_put_render_ctx_id(stream);
1356
Chris Wilson8f8b1172019-10-07 22:09:41 +01001357 put_oa_config(stream->oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001358
Chris Wilson8f8b1172019-10-07 22:09:41 +01001359 if (perf->spurious_report_rs.missed) {
Robert Bragg712122e2017-05-11 16:43:31 +01001360 DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n",
Chris Wilson8f8b1172019-10-07 22:09:41 +01001361 perf->spurious_report_rs.missed);
Robert Bragg712122e2017-05-11 16:43:31 +01001362 }
Robert Braggd7965152016-11-07 19:49:52 +00001363}
1364
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001365static void gen7_init_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001366{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001367 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001368 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001369 unsigned long flags;
1370
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001371 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggd7965152016-11-07 19:49:52 +00001372
1373 /* Pre-DevBDW: OABUFFER must be set with counters off,
1374 * before OASTATUS1, but after OASTATUS2
1375 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001376 intel_uncore_write(uncore, GEN7_OASTATUS2, /* head */
1377 gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001378 stream->oa_buffer.head = gtt_offset;
Robert Braggf2790202017-05-11 16:43:26 +01001379
Chris Wilson8f8b1172019-10-07 22:09:41 +01001380 intel_uncore_write(uncore, GEN7_OABUFFER, gtt_offset);
Robert Braggf2790202017-05-11 16:43:26 +01001381
Chris Wilson8f8b1172019-10-07 22:09:41 +01001382 intel_uncore_write(uncore, GEN7_OASTATUS1, /* tail */
1383 gtt_offset | OABUFFER_SIZE_16M);
Robert Braggd7965152016-11-07 19:49:52 +00001384
Robert Bragg0dd860c2017-05-11 16:43:28 +01001385 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001386 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1387 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001388
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001389 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001390
Robert Braggd7965152016-11-07 19:49:52 +00001391 /* On Haswell we have to track which OASTATUS1 flags we've
1392 * already seen since they can't be cleared while periodic
1393 * sampling is enabled.
1394 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001395 stream->perf->gen7_latched_oastatus1 = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001396
1397 /* NB: although the OA buffer will initially be allocated
1398 * zeroed via shmfs (and so this memset is redundant when
1399 * first allocating), we may re-init the OA buffer, either
1400 * when re-enabling a stream or in error/reset paths.
1401 *
1402 * The reason we clear the buffer for each re-init is for the
1403 * sanity check in gen7_append_oa_reports() that looks at the
1404 * report-id field to make sure it's non-zero which relies on
1405 * the assumption that new reports are being written to zeroed
1406 * memory...
1407 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001408 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001409
1410 /* Maybe make ->pollin per-stream state if we support multiple
1411 * concurrent streams in the future.
1412 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001413 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00001414}
1415
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001416static void gen8_init_oa_buffer(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001417{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001418 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001419 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg19f81df2017-06-13 12:23:03 +01001420 unsigned long flags;
1421
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001422 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001423
Chris Wilson8f8b1172019-10-07 22:09:41 +01001424 intel_uncore_write(uncore, GEN8_OASTATUS, 0);
1425 intel_uncore_write(uncore, GEN8_OAHEADPTR, gtt_offset);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001426 stream->oa_buffer.head = gtt_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01001427
Chris Wilson8f8b1172019-10-07 22:09:41 +01001428 intel_uncore_write(uncore, GEN8_OABUFFER_UDW, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01001429
1430 /*
1431 * PRM says:
1432 *
1433 * "This MMIO must be set before the OATAILPTR
1434 * register and after the OAHEADPTR register. This is
1435 * to enable proper functionality of the overflow
1436 * bit."
1437 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001438 intel_uncore_write(uncore, GEN8_OABUFFER, gtt_offset |
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001439 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
Chris Wilson8f8b1172019-10-07 22:09:41 +01001440 intel_uncore_write(uncore, GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
Robert Bragg19f81df2017-06-13 12:23:03 +01001441
1442 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001443 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1444 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg19f81df2017-06-13 12:23:03 +01001445
1446 /*
1447 * Reset state used to recognise context switches, affecting which
1448 * reports we will forward to userspace while filtering for a single
1449 * context.
1450 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001451 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
Robert Bragg19f81df2017-06-13 12:23:03 +01001452
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001453 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001454
1455 /*
1456 * NB: although the OA buffer will initially be allocated
1457 * zeroed via shmfs (and so this memset is redundant when
1458 * first allocating), we may re-init the OA buffer, either
1459 * when re-enabling a stream or in error/reset paths.
1460 *
1461 * The reason we clear the buffer for each re-init is for the
1462 * sanity check in gen8_append_oa_reports() that looks at the
1463 * reason field to make sure it's non-zero which relies on
1464 * the assumption that new reports are being written to zeroed
1465 * memory...
1466 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001467 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Bragg19f81df2017-06-13 12:23:03 +01001468
1469 /*
1470 * Maybe make ->pollin per-stream state if we support multiple
1471 * concurrent streams in the future.
1472 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001473 stream->pollin = false;
Robert Bragg19f81df2017-06-13 12:23:03 +01001474}
1475
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001476static int alloc_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001477{
1478 struct drm_i915_gem_object *bo;
1479 struct i915_vma *vma;
1480 int ret;
1481
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001482 if (WARN_ON(stream->oa_buffer.vma))
Robert Braggd7965152016-11-07 19:49:52 +00001483 return -ENODEV;
1484
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001485 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
1486 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
1487
Chris Wilson8f8b1172019-10-07 22:09:41 +01001488 bo = i915_gem_object_create_shmem(stream->perf->i915, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001489 if (IS_ERR(bo)) {
1490 DRM_ERROR("Failed to allocate OA buffer\n");
Chris Wilson28507482019-10-04 14:39:58 +01001491 return PTR_ERR(bo);
Robert Braggd7965152016-11-07 19:49:52 +00001492 }
1493
Chris Wilsona679f582019-03-21 16:19:07 +00001494 i915_gem_object_set_cache_coherency(bo, I915_CACHE_LLC);
Robert Braggd7965152016-11-07 19:49:52 +00001495
1496 /* PreHSW required 512K alignment, HSW requires 16M */
1497 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0);
1498 if (IS_ERR(vma)) {
1499 ret = PTR_ERR(vma);
1500 goto err_unref;
1501 }
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001502 stream->oa_buffer.vma = vma;
Robert Braggd7965152016-11-07 19:49:52 +00001503
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001504 stream->oa_buffer.vaddr =
Robert Braggd7965152016-11-07 19:49:52 +00001505 i915_gem_object_pin_map(bo, I915_MAP_WB);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001506 if (IS_ERR(stream->oa_buffer.vaddr)) {
1507 ret = PTR_ERR(stream->oa_buffer.vaddr);
Robert Braggd7965152016-11-07 19:49:52 +00001508 goto err_unpin;
1509 }
1510
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001511 DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001512 i915_ggtt_offset(stream->oa_buffer.vma),
1513 stream->oa_buffer.vaddr);
Robert Braggd7965152016-11-07 19:49:52 +00001514
Chris Wilson28507482019-10-04 14:39:58 +01001515 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001516
1517err_unpin:
1518 __i915_vma_unpin(vma);
1519
1520err_unref:
1521 i915_gem_object_put(bo);
1522
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001523 stream->oa_buffer.vaddr = NULL;
1524 stream->oa_buffer.vma = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001525
Robert Braggd7965152016-11-07 19:49:52 +00001526 return ret;
1527}
1528
Chris Wilson8f8b1172019-10-07 22:09:41 +01001529static void config_oa_regs(struct intel_uncore *uncore,
Robert Braggd7965152016-11-07 19:49:52 +00001530 const struct i915_oa_reg *regs,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001531 u32 n_regs)
Robert Braggd7965152016-11-07 19:49:52 +00001532{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001533 u32 i;
Robert Braggd7965152016-11-07 19:49:52 +00001534
1535 for (i = 0; i < n_regs; i++) {
1536 const struct i915_oa_reg *reg = regs + i;
1537
Chris Wilson8f8b1172019-10-07 22:09:41 +01001538 intel_uncore_write(uncore, reg->addr, reg->value);
Robert Braggd7965152016-11-07 19:49:52 +00001539 }
1540}
1541
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001542static void delay_after_mux(void)
Robert Braggd7965152016-11-07 19:49:52 +00001543{
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001544 /*
1545 * It apparently takes a fairly long time for a new MUX
Robert Braggd7965152016-11-07 19:49:52 +00001546 * configuration to be be applied after these register writes.
1547 * This delay duration was derived empirically based on the
1548 * render_basic config but hopefully it covers the maximum
1549 * configuration latency.
1550 *
1551 * As a fallback, the checks in _append_oa_reports() to skip
1552 * invalid OA reports do also seem to work to discard reports
1553 * generated before this config has completed - albeit not
1554 * silently.
1555 *
1556 * Unfortunately this is essentially a magic number, since we
1557 * don't currently know of a reliable mechanism for predicting
1558 * how long the MUX config will take to apply and besides
1559 * seeing invalid reports we don't know of a reliable way to
1560 * explicitly check that the MUX config has landed.
1561 *
1562 * It's even possible we've miss characterized the underlying
1563 * problem - it just seems like the simplest explanation why
1564 * a delay at this location would mitigate any invalid reports.
1565 */
1566 usleep_range(15000, 20000);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001567}
1568
1569static int hsw_enable_metric_set(struct i915_perf_stream *stream)
1570{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001571 struct intel_uncore *uncore = stream->gt->uncore;
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001572 const struct i915_oa_config *oa_config = stream->oa_config;
1573
1574 /*
1575 * PRM:
1576 *
1577 * OA unit is using “crclk” for its functionality. When trunk
1578 * level clock gating takes place, OA clock would be gated,
1579 * unable to count the events from non-render clock domain.
1580 * Render clock gating must be disabled when OA is enabled to
1581 * count the events from non-render domain. Unit level clock
1582 * gating for RCS should also be disabled.
1583 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001584 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
1585 GEN7_DOP_CLOCK_GATE_ENABLE, 0);
1586 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
1587 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001588
Chris Wilson8f8b1172019-10-07 22:09:41 +01001589 config_oa_regs(uncore, oa_config->mux_regs, oa_config->mux_regs_len);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001590 delay_after_mux();
Robert Braggd7965152016-11-07 19:49:52 +00001591
Chris Wilson8f8b1172019-10-07 22:09:41 +01001592 config_oa_regs(uncore, oa_config->b_counter_regs,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001593 oa_config->b_counter_regs_len);
Robert Braggd7965152016-11-07 19:49:52 +00001594
1595 return 0;
1596}
1597
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001598static void hsw_disable_metric_set(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001599{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001600 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001601
Chris Wilson8f8b1172019-10-07 22:09:41 +01001602 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
1603 GEN6_CSUNIT_CLOCK_GATE_DISABLE, 0);
1604 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
1605 0, GEN7_DOP_CLOCK_GATE_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00001606
Chris Wilson8f8b1172019-10-07 22:09:41 +01001607 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Braggd7965152016-11-07 19:49:52 +00001608}
1609
Chris Wilsona9877da2019-07-16 22:34:43 +01001610static u32 oa_config_flex_reg(const struct i915_oa_config *oa_config,
1611 i915_reg_t reg)
1612{
1613 u32 mmio = i915_mmio_reg_offset(reg);
1614 int i;
1615
1616 /*
1617 * This arbitrary default will select the 'EU FPU0 Pipeline
1618 * Active' event. In the future it's anticipated that there
1619 * will be an explicit 'No Event' we can select, but not yet...
1620 */
1621 if (!oa_config)
1622 return 0;
1623
1624 for (i = 0; i < oa_config->flex_regs_len; i++) {
1625 if (i915_mmio_reg_offset(oa_config->flex_regs[i].addr) == mmio)
1626 return oa_config->flex_regs[i].value;
1627 }
1628
1629 return 0;
1630}
Robert Bragg19f81df2017-06-13 12:23:03 +01001631/*
1632 * NB: It must always remain pointer safe to run this even if the OA unit
1633 * has been disabled.
1634 *
1635 * It's fine to put out-of-date values into these per-context registers
1636 * in the case that the OA unit has been disabled.
1637 */
Chris Wilsonb146e5e2019-03-06 08:47:04 +00001638static void
Chris Wilson7dc56af2019-09-24 15:59:50 +01001639gen8_update_reg_state_unlocked(const struct intel_context *ce,
1640 const struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001641{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001642 u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
1643 u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01001644 /* The MMIO offsets for Flex EU registers aren't contiguous */
Lionel Landwerlin35ab4fd2018-08-13 09:02:18 +01001645 i915_reg_t flex_regs[] = {
1646 EU_PERF_CNTL0,
1647 EU_PERF_CNTL1,
1648 EU_PERF_CNTL2,
1649 EU_PERF_CNTL3,
1650 EU_PERF_CNTL4,
1651 EU_PERF_CNTL5,
1652 EU_PERF_CNTL6,
Robert Bragg19f81df2017-06-13 12:23:03 +01001653 };
Chris Wilson7dc56af2019-09-24 15:59:50 +01001654 u32 *reg_state = ce->lrc_reg_state;
Robert Bragg19f81df2017-06-13 12:23:03 +01001655 int i;
1656
Chris Wilson7dc56af2019-09-24 15:59:50 +01001657 reg_state[ctx_oactxctrl + 1] =
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001658 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
1659 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
Chris Wilson7dc56af2019-09-24 15:59:50 +01001660 GEN8_OA_COUNTER_RESUME;
Robert Bragg19f81df2017-06-13 12:23:03 +01001661
Chris Wilson7dc56af2019-09-24 15:59:50 +01001662 for (i = 0; i < ARRAY_SIZE(flex_regs); i++)
1663 reg_state[ctx_flexeu0 + i * 2 + 1] =
1664 oa_config_flex_reg(stream->oa_config, flex_regs[i]);
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00001665
Chris Wilson8f8b1172019-10-07 22:09:41 +01001666 reg_state[CTX_R_PWR_CLK_STATE] =
1667 intel_sseu_make_rpcs(ce->engine->i915, &ce->sseu);
Robert Bragg19f81df2017-06-13 12:23:03 +01001668}
1669
Chris Wilsona9877da2019-07-16 22:34:43 +01001670struct flex {
1671 i915_reg_t reg;
1672 u32 offset;
1673 u32 value;
1674};
1675
1676static int
1677gen8_store_flex(struct i915_request *rq,
1678 struct intel_context *ce,
1679 const struct flex *flex, unsigned int count)
1680{
1681 u32 offset;
1682 u32 *cs;
1683
1684 cs = intel_ring_begin(rq, 4 * count);
1685 if (IS_ERR(cs))
1686 return PTR_ERR(cs);
1687
1688 offset = i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
1689 do {
1690 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
Chris Wilson7dc56af2019-09-24 15:59:50 +01001691 *cs++ = offset + flex->offset * sizeof(u32);
Chris Wilsona9877da2019-07-16 22:34:43 +01001692 *cs++ = 0;
1693 *cs++ = flex->value;
1694 } while (flex++, --count);
1695
1696 intel_ring_advance(rq, cs);
1697
1698 return 0;
1699}
1700
1701static int
1702gen8_load_flex(struct i915_request *rq,
1703 struct intel_context *ce,
1704 const struct flex *flex, unsigned int count)
1705{
1706 u32 *cs;
1707
1708 GEM_BUG_ON(!count || count > 63);
1709
1710 cs = intel_ring_begin(rq, 2 * count + 2);
1711 if (IS_ERR(cs))
1712 return PTR_ERR(cs);
1713
1714 *cs++ = MI_LOAD_REGISTER_IMM(count);
1715 do {
1716 *cs++ = i915_mmio_reg_offset(flex->reg);
1717 *cs++ = flex->value;
1718 } while (flex++, --count);
1719 *cs++ = MI_NOOP;
1720
1721 intel_ring_advance(rq, cs);
1722
1723 return 0;
1724}
1725
1726static int gen8_modify_context(struct intel_context *ce,
1727 const struct flex *flex, unsigned int count)
1728{
1729 struct i915_request *rq;
1730 int err;
1731
1732 lockdep_assert_held(&ce->pin_mutex);
1733
1734 rq = i915_request_create(ce->engine->kernel_context);
1735 if (IS_ERR(rq))
1736 return PTR_ERR(rq);
1737
1738 /* Serialise with the remote context */
1739 err = intel_context_prepare_remote_request(ce, rq);
1740 if (err == 0)
1741 err = gen8_store_flex(rq, ce, flex, count);
1742
1743 i915_request_add(rq);
1744 return err;
1745}
1746
1747static int gen8_modify_self(struct intel_context *ce,
1748 const struct flex *flex, unsigned int count)
1749{
1750 struct i915_request *rq;
1751 int err;
1752
1753 rq = i915_request_create(ce);
1754 if (IS_ERR(rq))
1755 return PTR_ERR(rq);
1756
1757 err = gen8_load_flex(rq, ce, flex, count);
1758
1759 i915_request_add(rq);
1760 return err;
1761}
1762
Chris Wilson5cca5032019-07-26 14:14:58 +01001763static int gen8_configure_context(struct i915_gem_context *ctx,
1764 struct flex *flex, unsigned int count)
1765{
1766 struct i915_gem_engines_iter it;
1767 struct intel_context *ce;
1768 int err = 0;
1769
1770 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1771 GEM_BUG_ON(ce == ce->engine->kernel_context);
1772
1773 if (ce->engine->class != RENDER_CLASS)
1774 continue;
1775
1776 err = intel_context_lock_pinned(ce);
1777 if (err)
1778 break;
1779
1780 flex->value = intel_sseu_make_rpcs(ctx->i915, &ce->sseu);
1781
1782 /* Otherwise OA settings will be set upon first use */
1783 if (intel_context_is_pinned(ce))
1784 err = gen8_modify_context(ce, flex, count);
1785
1786 intel_context_unlock_pinned(ce);
1787 if (err)
1788 break;
1789 }
1790 i915_gem_context_unlock_engines(ctx);
1791
1792 return err;
1793}
1794
Robert Bragg19f81df2017-06-13 12:23:03 +01001795/*
Robert Bragg19f81df2017-06-13 12:23:03 +01001796 * Manages updating the per-context aspects of the OA stream
1797 * configuration across all contexts.
1798 *
1799 * The awkward consideration here is that OACTXCONTROL controls the
1800 * exponent for periodic sampling which is primarily used for system
1801 * wide profiling where we'd like a consistent sampling period even in
1802 * the face of context switches.
1803 *
1804 * Our approach of updating the register state context (as opposed to
1805 * say using a workaround batch buffer) ensures that the hardware
1806 * won't automatically reload an out-of-date timer exponent even
1807 * transiently before a WA BB could be parsed.
1808 *
1809 * This function needs to:
1810 * - Ensure the currently running context's per-context OA state is
1811 * updated
1812 * - Ensure that all existing contexts will have the correct per-context
1813 * OA state if they are scheduled for use.
1814 * - Ensure any new contexts will be initialized with the correct
1815 * per-context OA state.
1816 *
1817 * Note: it's only the RCS/Render context that has any OA state.
1818 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001819static int gen8_configure_all_contexts(struct i915_perf_stream *stream,
Lionel Landwerlin41d3fdc2018-03-01 11:06:13 +00001820 const struct i915_oa_config *oa_config)
Robert Bragg19f81df2017-06-13 12:23:03 +01001821{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001822 struct drm_i915_private *i915 = stream->perf->i915;
Chris Wilsona9877da2019-07-16 22:34:43 +01001823 /* The MMIO offsets for Flex EU registers aren't contiguous */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001824 const u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
Chris Wilson7dc56af2019-09-24 15:59:50 +01001825#define ctx_flexeuN(N) (ctx_flexeu0 + 2 * (N) + 1)
Chris Wilsona9877da2019-07-16 22:34:43 +01001826 struct flex regs[] = {
1827 {
1828 GEN8_R_PWR_CLK_STATE,
1829 CTX_R_PWR_CLK_STATE,
1830 },
1831 {
1832 GEN8_OACTXCONTROL,
Chris Wilson8f8b1172019-10-07 22:09:41 +01001833 stream->perf->ctx_oactxctrl_offset + 1,
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001834 ((stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
1835 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
Chris Wilsona9877da2019-07-16 22:34:43 +01001836 GEN8_OA_COUNTER_RESUME)
1837 },
1838 { EU_PERF_CNTL0, ctx_flexeuN(0) },
1839 { EU_PERF_CNTL1, ctx_flexeuN(1) },
1840 { EU_PERF_CNTL2, ctx_flexeuN(2) },
1841 { EU_PERF_CNTL3, ctx_flexeuN(3) },
1842 { EU_PERF_CNTL4, ctx_flexeuN(4) },
1843 { EU_PERF_CNTL5, ctx_flexeuN(5) },
1844 { EU_PERF_CNTL6, ctx_flexeuN(6) },
1845 };
1846#undef ctx_flexeuN
1847 struct intel_engine_cs *engine;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001848 struct i915_gem_context *ctx, *cn;
1849 int i, err;
Robert Bragg19f81df2017-06-13 12:23:03 +01001850
Chris Wilsona9877da2019-07-16 22:34:43 +01001851 for (i = 2; i < ARRAY_SIZE(regs); i++)
1852 regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
1853
Chris Wilsona4c969d2019-10-07 22:09:42 +01001854 lockdep_assert_held(&stream->perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01001855
Robert Bragg19f81df2017-06-13 12:23:03 +01001856 /*
1857 * The OA register config is setup through the context image. This image
1858 * might be written to by the GPU on context switch (in particular on
1859 * lite-restore). This means we can't safely update a context's image,
1860 * if this context is scheduled/submitted to run on the GPU.
1861 *
1862 * We could emit the OA register config through the batch buffer but
1863 * this might leave small interval of time where the OA unit is
1864 * configured at an invalid sampling period.
1865 *
Chris Wilsona9877da2019-07-16 22:34:43 +01001866 * Note that since we emit all requests from a single ring, there
1867 * is still an implicit global barrier here that may cause a high
1868 * priority context to wait for an otherwise independent low priority
1869 * context. Contexts idle at the time of reconfiguration are not
1870 * trapped behind the barrier.
Robert Bragg19f81df2017-06-13 12:23:03 +01001871 */
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001872 spin_lock(&i915->gem.contexts.lock);
1873 list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
Chris Wilsona9877da2019-07-16 22:34:43 +01001874 if (ctx == i915->kernel_context)
1875 continue;
1876
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001877 if (!kref_get_unless_zero(&ctx->ref))
1878 continue;
1879
1880 spin_unlock(&i915->gem.contexts.lock);
1881
Chris Wilson5cca5032019-07-26 14:14:58 +01001882 err = gen8_configure_context(ctx, regs, ARRAY_SIZE(regs));
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001883 if (err) {
1884 i915_gem_context_put(ctx);
Chris Wilsona9877da2019-07-16 22:34:43 +01001885 return err;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001886 }
1887
1888 spin_lock(&i915->gem.contexts.lock);
1889 list_safe_reset_next(ctx, cn, link);
1890 i915_gem_context_put(ctx);
Robert Bragg19f81df2017-06-13 12:23:03 +01001891 }
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001892 spin_unlock(&i915->gem.contexts.lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01001893
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001894 /*
Chris Wilsona9877da2019-07-16 22:34:43 +01001895 * After updating all other contexts, we need to modify ourselves.
1896 * If we don't modify the kernel_context, we do not get events while
1897 * idle.
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001898 */
Chris Wilson750e76b2019-08-06 13:43:00 +01001899 for_each_uabi_engine(engine, i915) {
Chris Wilsona9877da2019-07-16 22:34:43 +01001900 struct intel_context *ce = engine->kernel_context;
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001901
Chris Wilsona9877da2019-07-16 22:34:43 +01001902 if (engine->class != RENDER_CLASS)
1903 continue;
1904
1905 regs[0].value = intel_sseu_make_rpcs(i915, &ce->sseu);
1906
1907 err = gen8_modify_self(ce, regs, ARRAY_SIZE(regs));
1908 if (err)
1909 return err;
1910 }
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001911
1912 return 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01001913}
1914
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001915static int gen8_enable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001916{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001917 struct intel_uncore *uncore = stream->gt->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001918 const struct i915_oa_config *oa_config = stream->oa_config;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001919 int ret;
Robert Bragg19f81df2017-06-13 12:23:03 +01001920
1921 /*
1922 * We disable slice/unslice clock ratio change reports on SKL since
1923 * they are too noisy. The HW generates a lot of redundant reports
1924 * where the ratio hasn't really changed causing a lot of redundant
1925 * work to processes and increasing the chances we'll hit buffer
1926 * overruns.
1927 *
1928 * Although we don't currently use the 'disable overrun' OABUFFER
1929 * feature it's worth noting that clock ratio reports have to be
1930 * disabled before considering to use that feature since the HW doesn't
1931 * correctly block these reports.
1932 *
1933 * Currently none of the high-level metrics we have depend on knowing
1934 * this ratio to normalize.
1935 *
1936 * Note: This register is not power context saved and restored, but
1937 * that's OK considering that we disable RC6 while the OA unit is
1938 * enabled.
1939 *
1940 * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
1941 * be read back from automatically triggered reports, as part of the
1942 * RPT_ID field.
1943 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001944 if (IS_GEN_RANGE(stream->perf->i915, 9, 11)) {
1945 intel_uncore_write(uncore, GEN8_OA_DEBUG,
1946 _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
1947 GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
Robert Bragg19f81df2017-06-13 12:23:03 +01001948 }
1949
1950 /*
1951 * Update all contexts prior writing the mux configurations as we need
1952 * to make sure all slices/subslices are ON before writing to NOA
1953 * registers.
1954 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001955 ret = gen8_configure_all_contexts(stream, oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01001956 if (ret)
1957 return ret;
1958
Chris Wilson8f8b1172019-10-07 22:09:41 +01001959 config_oa_regs(uncore, oa_config->mux_regs, oa_config->mux_regs_len);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001960 delay_after_mux();
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001961
Chris Wilson8f8b1172019-10-07 22:09:41 +01001962 config_oa_regs(uncore, oa_config->b_counter_regs,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001963 oa_config->b_counter_regs_len);
Robert Bragg19f81df2017-06-13 12:23:03 +01001964
1965 return 0;
1966}
1967
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001968static void gen8_disable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001969{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001970 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001971
Robert Bragg19f81df2017-06-13 12:23:03 +01001972 /* Reset all contexts' slices/subslices configurations. */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001973 gen8_configure_all_contexts(stream, NULL);
Lionel Landwerlin28964cf2017-08-03 17:58:10 +01001974
Chris Wilson8f8b1172019-10-07 22:09:41 +01001975 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01001976}
1977
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001978static void gen10_disable_metric_set(struct i915_perf_stream *stream)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001979{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001980 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001981
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001982 /* Reset all contexts' slices/subslices configurations. */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001983 gen8_configure_all_contexts(stream, NULL);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001984
1985 /* Make sure we disable noa to save power. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001986 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001987}
1988
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001989static void gen7_oa_enable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001990{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001991 struct intel_uncore *uncore = stream->gt->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001992 struct i915_gem_context *ctx = stream->ctx;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001993 u32 ctx_id = stream->specific_ctx_id;
1994 bool periodic = stream->periodic;
1995 u32 period_exponent = stream->period_exponent;
1996 u32 report_format = stream->oa_buffer.format;
Lionel Landwerlin11051302018-03-26 10:08:23 +01001997
Robert Bragg1bef3402017-06-13 12:23:06 +01001998 /*
1999 * Reset buf pointers so we don't forward reports from before now.
2000 *
2001 * Think carefully if considering trying to avoid this, since it
2002 * also ensures status flags and the buffer itself are cleared
2003 * in error paths, and we have checks for invalid reports based
2004 * on the assumption that certain fields are written to zeroed
2005 * memory which this helps maintains.
2006 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002007 gen7_init_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002008
Chris Wilson8f8b1172019-10-07 22:09:41 +01002009 intel_uncore_write(uncore, GEN7_OACONTROL,
2010 (ctx_id & GEN7_OACONTROL_CTX_MASK) |
2011 (period_exponent <<
2012 GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
2013 (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
2014 (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
2015 (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
2016 GEN7_OACONTROL_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00002017}
2018
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002019static void gen8_oa_enable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002020{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002021 struct intel_uncore *uncore = stream->gt->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002022 u32 report_format = stream->oa_buffer.format;
Robert Bragg19f81df2017-06-13 12:23:03 +01002023
2024 /*
2025 * Reset buf pointers so we don't forward reports from before now.
2026 *
2027 * Think carefully if considering trying to avoid this, since it
2028 * also ensures status flags and the buffer itself are cleared
2029 * in error paths, and we have checks for invalid reports based
2030 * on the assumption that certain fields are written to zeroed
2031 * memory which this helps maintains.
2032 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002033 gen8_init_oa_buffer(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002034
2035 /*
2036 * Note: we don't rely on the hardware to perform single context
2037 * filtering and instead filter on the cpu based on the context-id
2038 * field of reports
2039 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002040 intel_uncore_write(uncore, GEN8_OACONTROL,
2041 (report_format << GEN8_OA_REPORT_FORMAT_SHIFT) |
2042 GEN8_OA_COUNTER_ENABLE);
Robert Bragg19f81df2017-06-13 12:23:03 +01002043}
2044
Robert Bragg16d98b32016-12-07 21:40:33 +00002045/**
2046 * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
2047 * @stream: An i915 perf stream opened for OA metrics
2048 *
2049 * [Re]enables hardware periodic sampling according to the period configured
2050 * when opening the stream. This also starts a hrtimer that will periodically
2051 * check for data in the circular OA buffer for notifying userspace (e.g.
2052 * during a read() or poll()).
2053 */
Robert Braggd7965152016-11-07 19:49:52 +00002054static void i915_oa_stream_enable(struct i915_perf_stream *stream)
2055{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002056 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002057
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002058 if (stream->periodic)
2059 hrtimer_start(&stream->poll_check_timer,
Robert Braggd7965152016-11-07 19:49:52 +00002060 ns_to_ktime(POLL_PERIOD),
2061 HRTIMER_MODE_REL_PINNED);
2062}
2063
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002064static void gen7_oa_disable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002065{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002066 struct intel_uncore *uncore = stream->gt->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002067
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002068 intel_uncore_write(uncore, GEN7_OACONTROL, 0);
2069 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002070 GEN7_OACONTROL, GEN7_OACONTROL_ENABLE, 0,
2071 50))
2072 DRM_ERROR("wait for OA to be disabled timed out\n");
Robert Braggd7965152016-11-07 19:49:52 +00002073}
2074
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002075static void gen8_oa_disable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002076{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002077 struct intel_uncore *uncore = stream->gt->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002078
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002079 intel_uncore_write(uncore, GEN8_OACONTROL, 0);
2080 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002081 GEN8_OACONTROL, GEN8_OA_COUNTER_ENABLE, 0,
2082 50))
2083 DRM_ERROR("wait for OA to be disabled timed out\n");
Robert Bragg19f81df2017-06-13 12:23:03 +01002084}
2085
Robert Bragg16d98b32016-12-07 21:40:33 +00002086/**
2087 * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
2088 * @stream: An i915 perf stream opened for OA metrics
2089 *
2090 * Stops the OA unit from periodically writing counter reports into the
2091 * circular OA buffer. This also stops the hrtimer that periodically checks for
2092 * data in the circular OA buffer, for notifying userspace.
2093 */
Robert Braggd7965152016-11-07 19:49:52 +00002094static void i915_oa_stream_disable(struct i915_perf_stream *stream)
2095{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002096 stream->perf->ops.oa_disable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002097
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002098 if (stream->periodic)
2099 hrtimer_cancel(&stream->poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00002100}
2101
Robert Braggd7965152016-11-07 19:49:52 +00002102static const struct i915_perf_stream_ops i915_oa_stream_ops = {
2103 .destroy = i915_oa_stream_destroy,
2104 .enable = i915_oa_stream_enable,
2105 .disable = i915_oa_stream_disable,
2106 .wait_unlocked = i915_oa_wait_unlocked,
2107 .poll_wait = i915_oa_poll_wait,
2108 .read = i915_oa_read,
2109};
2110
Robert Bragg16d98b32016-12-07 21:40:33 +00002111/**
2112 * i915_oa_stream_init - validate combined props for OA stream and init
2113 * @stream: An i915 perf stream
2114 * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
2115 * @props: The property state that configures stream (individually validated)
2116 *
2117 * While read_properties_unlocked() validates properties in isolation it
2118 * doesn't ensure that the combination necessarily makes sense.
2119 *
2120 * At this point it has been determined that userspace wants a stream of
2121 * OA metrics, but still we need to further validate the combined
2122 * properties are OK.
2123 *
2124 * If the configuration makes sense then we can allocate memory for
2125 * a circular OA buffer and apply the requested metric set configuration.
2126 *
2127 * Returns: zero on success or a negative error code.
2128 */
Robert Braggd7965152016-11-07 19:49:52 +00002129static int i915_oa_stream_init(struct i915_perf_stream *stream,
2130 struct drm_i915_perf_open_param *param,
2131 struct perf_open_properties *props)
2132{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002133 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00002134 int format_size;
2135 int ret;
2136
Robert Bragg442b8c02016-11-07 19:49:53 +00002137 /* If the sysfs metrics/ directory wasn't registered for some
2138 * reason then don't let userspace try their luck with config
2139 * IDs
2140 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002141 if (!perf->metrics_kobj) {
Robert Bragg77085502016-12-01 17:21:52 +00002142 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
Robert Bragg442b8c02016-11-07 19:49:53 +00002143 return -EINVAL;
2144 }
2145
Robert Braggd7965152016-11-07 19:49:52 +00002146 if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
Robert Bragg77085502016-12-01 17:21:52 +00002147 DRM_DEBUG("Only OA report sampling supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002148 return -EINVAL;
2149 }
2150
Chris Wilson8f8b1172019-10-07 22:09:41 +01002151 if (!perf->ops.enable_metric_set) {
Robert Bragg77085502016-12-01 17:21:52 +00002152 DRM_DEBUG("OA unit not supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002153 return -ENODEV;
2154 }
2155
2156 /* To avoid the complexity of having to accurately filter
2157 * counter reports and marshal to the appropriate client
2158 * we currently only allow exclusive access
2159 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002160 if (perf->exclusive_stream) {
Robert Bragg77085502016-12-01 17:21:52 +00002161 DRM_DEBUG("OA unit already in use\n");
Robert Braggd7965152016-11-07 19:49:52 +00002162 return -EBUSY;
2163 }
2164
Robert Braggd7965152016-11-07 19:49:52 +00002165 if (!props->oa_format) {
Robert Bragg77085502016-12-01 17:21:52 +00002166 DRM_DEBUG("OA report format not specified\n");
Robert Braggd7965152016-11-07 19:49:52 +00002167 return -EINVAL;
2168 }
2169
2170 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
2171
Chris Wilson8f8b1172019-10-07 22:09:41 +01002172 format_size = perf->oa_formats[props->oa_format].size;
Robert Braggd7965152016-11-07 19:49:52 +00002173
2174 stream->sample_flags |= SAMPLE_OA_REPORT;
2175 stream->sample_size += format_size;
2176
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002177 stream->oa_buffer.format_size = format_size;
2178 if (WARN_ON(stream->oa_buffer.format_size == 0))
Robert Braggd7965152016-11-07 19:49:52 +00002179 return -EINVAL;
2180
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002181 stream->oa_buffer.format =
Chris Wilson8f8b1172019-10-07 22:09:41 +01002182 perf->oa_formats[props->oa_format].format;
Robert Braggd7965152016-11-07 19:49:52 +00002183
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002184 stream->periodic = props->oa_periodic;
2185 if (stream->periodic)
2186 stream->period_exponent = props->oa_period_exponent;
Robert Braggd7965152016-11-07 19:49:52 +00002187
Robert Braggd7965152016-11-07 19:49:52 +00002188 if (stream->ctx) {
2189 ret = oa_get_render_ctx_id(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002190 if (ret) {
2191 DRM_DEBUG("Invalid context id to filter with\n");
Robert Braggd7965152016-11-07 19:49:52 +00002192 return ret;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002193 }
Robert Braggd7965152016-11-07 19:49:52 +00002194 }
2195
Chris Wilson8f8b1172019-10-07 22:09:41 +01002196 ret = get_oa_config(perf, props->metrics_set, &stream->oa_config);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002197 if (ret) {
2198 DRM_DEBUG("Invalid OA config id=%i\n", props->metrics_set);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002199 goto err_config;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002200 }
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002201
Robert Braggd7965152016-11-07 19:49:52 +00002202 /* PRM - observability performance counters:
2203 *
2204 * OACONTROL, performance counter enable, note:
2205 *
2206 * "When this bit is set, in order to have coherent counts,
2207 * RC6 power state and trunk clock gating must be disabled.
2208 * This can be achieved by programming MMIO registers as
2209 * 0xA094=0 and 0xA090[31]=1"
2210 *
2211 * In our case we are expecting that taking pm + FORCEWAKE
2212 * references will effectively disable RC6.
2213 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002214 stream->wakeref = intel_runtime_pm_get(stream->gt->uncore->rpm);
2215 intel_uncore_forcewake_get(stream->gt->uncore, FORCEWAKE_ALL);
Robert Braggd7965152016-11-07 19:49:52 +00002216
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002217 ret = alloc_oa_buffer(stream);
sagar.a.kamble@intel.com987f8c42017-06-27 23:09:41 +05302218 if (ret)
2219 goto err_oa_buf_alloc;
2220
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002221 stream->ops = &i915_oa_stream_ops;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002222 perf->exclusive_stream = stream;
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002223
Chris Wilson8f8b1172019-10-07 22:09:41 +01002224 ret = perf->ops.enable_metric_set(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002225 if (ret) {
2226 DRM_DEBUG("Unable to enable metric set\n");
Robert Braggd7965152016-11-07 19:49:52 +00002227 goto err_enable;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002228 }
Robert Braggd7965152016-11-07 19:49:52 +00002229
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002230 hrtimer_init(&stream->poll_check_timer,
2231 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2232 stream->poll_check_timer.function = oa_poll_check_timer_cb;
2233 init_waitqueue_head(&stream->poll_wq);
2234 spin_lock_init(&stream->oa_buffer.ptr_lock);
2235
Robert Braggd7965152016-11-07 19:49:52 +00002236 return 0;
2237
2238err_enable:
Chris Wilson8f8b1172019-10-07 22:09:41 +01002239 perf->exclusive_stream = NULL;
2240 perf->ops.disable_metric_set(stream);
Lionel Landwerlin41d3fdc2018-03-01 11:06:13 +00002241
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002242 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002243
2244err_oa_buf_alloc:
Chris Wilson8f8b1172019-10-07 22:09:41 +01002245 put_oa_config(stream->oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002246
Chris Wilson8f8b1172019-10-07 22:09:41 +01002247 intel_uncore_forcewake_put(stream->gt->uncore, FORCEWAKE_ALL);
2248 intel_runtime_pm_put(stream->gt->uncore->rpm, stream->wakeref);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002249
2250err_config:
Robert Braggd7965152016-11-07 19:49:52 +00002251 if (stream->ctx)
2252 oa_put_render_ctx_id(stream);
2253
2254 return ret;
2255}
2256
Chris Wilson7dc56af2019-09-24 15:59:50 +01002257void i915_oa_init_reg_state(const struct intel_context *ce,
2258 const struct intel_engine_cs *engine)
Robert Bragg19f81df2017-06-13 12:23:03 +01002259{
Chris Wilson28b6cb02017-08-10 18:57:43 +01002260 struct i915_perf_stream *stream;
Robert Bragg19f81df2017-06-13 12:23:03 +01002261
Chris Wilsondffa8fe2019-08-30 19:19:29 +01002262 /* perf.exclusive_stream serialised by gen8_configure_all_contexts() */
2263 lockdep_assert_held(&ce->pin_mutex);
2264
Chris Wilson8a68d462019-03-05 18:03:30 +00002265 if (engine->class != RENDER_CLASS)
Robert Bragg19f81df2017-06-13 12:23:03 +01002266 return;
2267
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002268 stream = engine->i915->perf.exclusive_stream;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002269 if (stream)
Chris Wilson7dc56af2019-09-24 15:59:50 +01002270 gen8_update_reg_state_unlocked(ce, stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002271}
2272
Robert Bragg16d98b32016-12-07 21:40:33 +00002273/**
2274 * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation
2275 * @stream: An i915 perf stream
2276 * @file: An i915 perf stream file
2277 * @buf: destination buffer given by userspace
2278 * @count: the number of bytes userspace wants to read
2279 * @ppos: (inout) file seek position (unused)
2280 *
2281 * Besides wrapping &i915_perf_stream_ops->read this provides a common place to
2282 * ensure that if we've successfully copied any data then reporting that takes
2283 * precedence over any internal error status, so the data isn't lost.
2284 *
2285 * For example ret will be -ENOSPC whenever there is more buffered data than
2286 * can be copied to userspace, but that's only interesting if we weren't able
2287 * to copy some data because it implies the userspace buffer is too small to
2288 * receive a single record (and we never split records).
2289 *
2290 * Another case with ret == -EFAULT is more of a grey area since it would seem
2291 * like bad form for userspace to ask us to overrun its buffer, but the user
2292 * knows best:
2293 *
2294 * http://yarchive.net/comp/linux/partial_reads_writes.html
2295 *
2296 * Returns: The number of bytes copied or a negative error code on failure.
2297 */
Robert Braggeec688e2016-11-07 19:49:47 +00002298static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
2299 struct file *file,
2300 char __user *buf,
2301 size_t count,
2302 loff_t *ppos)
2303{
2304 /* Note we keep the offset (aka bytes read) separate from any
2305 * error status so that the final check for whether we return
2306 * the bytes read with a higher precedence than any error (see
2307 * comment below) doesn't need to be handled/duplicated in
2308 * stream->ops->read() implementations.
2309 */
2310 size_t offset = 0;
2311 int ret = stream->ops->read(stream, buf, count, &offset);
2312
Robert Braggeec688e2016-11-07 19:49:47 +00002313 return offset ?: (ret ?: -EAGAIN);
2314}
2315
Robert Bragg16d98b32016-12-07 21:40:33 +00002316/**
2317 * i915_perf_read - handles read() FOP for i915 perf stream FDs
2318 * @file: An i915 perf stream file
2319 * @buf: destination buffer given by userspace
2320 * @count: the number of bytes userspace wants to read
2321 * @ppos: (inout) file seek position (unused)
2322 *
2323 * The entry point for handling a read() on a stream file descriptor from
2324 * userspace. Most of the work is left to the i915_perf_read_locked() and
2325 * &i915_perf_stream_ops->read but to save having stream implementations (of
2326 * which we might have multiple later) we handle blocking read here.
2327 *
2328 * We can also consistently treat trying to read from a disabled stream
2329 * as an IO error so implementations can assume the stream is enabled
2330 * while reading.
2331 *
2332 * Returns: The number of bytes copied or a negative error code on failure.
2333 */
Robert Braggeec688e2016-11-07 19:49:47 +00002334static ssize_t i915_perf_read(struct file *file,
2335 char __user *buf,
2336 size_t count,
2337 loff_t *ppos)
2338{
2339 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002340 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002341 ssize_t ret;
2342
Robert Braggd7965152016-11-07 19:49:52 +00002343 /* To ensure it's handled consistently we simply treat all reads of a
2344 * disabled stream as an error. In particular it might otherwise lead
2345 * to a deadlock for blocking file descriptors...
2346 */
2347 if (!stream->enabled)
2348 return -EIO;
2349
Robert Braggeec688e2016-11-07 19:49:47 +00002350 if (!(file->f_flags & O_NONBLOCK)) {
Robert Braggd7965152016-11-07 19:49:52 +00002351 /* There's the small chance of false positives from
2352 * stream->ops->wait_unlocked.
2353 *
2354 * E.g. with single context filtering since we only wait until
2355 * oabuffer has >= 1 report we don't immediately know whether
2356 * any reports really belong to the current context
Robert Braggeec688e2016-11-07 19:49:47 +00002357 */
2358 do {
2359 ret = stream->ops->wait_unlocked(stream);
2360 if (ret)
2361 return ret;
2362
Chris Wilson8f8b1172019-10-07 22:09:41 +01002363 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002364 ret = i915_perf_read_locked(stream, file,
2365 buf, count, ppos);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002366 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002367 } while (ret == -EAGAIN);
2368 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01002369 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002370 ret = i915_perf_read_locked(stream, file, buf, count, ppos);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002371 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002372 }
2373
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002374 /* We allow the poll checking to sometimes report false positive EPOLLIN
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002375 * events where we might actually report EAGAIN on read() if there's
2376 * not really any data available. In this situation though we don't
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002377 * want to enter a busy loop between poll() reporting a EPOLLIN event
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002378 * and read() returning -EAGAIN. Clearing the oa.pollin state here
2379 * effectively ensures we back off until the next hrtimer callback
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002380 * before reporting another EPOLLIN event.
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002381 */
2382 if (ret >= 0 || ret == -EAGAIN) {
Robert Braggd7965152016-11-07 19:49:52 +00002383 /* Maybe make ->pollin per-stream state if we support multiple
2384 * concurrent streams in the future.
2385 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002386 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00002387 }
2388
Robert Braggeec688e2016-11-07 19:49:47 +00002389 return ret;
2390}
2391
Robert Braggd7965152016-11-07 19:49:52 +00002392static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
2393{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002394 struct i915_perf_stream *stream =
2395 container_of(hrtimer, typeof(*stream), poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00002396
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002397 if (oa_buffer_check_unlocked(stream)) {
2398 stream->pollin = true;
2399 wake_up(&stream->poll_wq);
Robert Braggd7965152016-11-07 19:49:52 +00002400 }
2401
2402 hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
2403
2404 return HRTIMER_RESTART;
2405}
2406
Robert Bragg16d98b32016-12-07 21:40:33 +00002407/**
2408 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
Robert Bragg16d98b32016-12-07 21:40:33 +00002409 * @stream: An i915 perf stream
2410 * @file: An i915 perf stream file
2411 * @wait: poll() state table
2412 *
2413 * For handling userspace polling on an i915 perf stream, this calls through to
2414 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
2415 * will be woken for new stream data.
2416 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01002417 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00002418 * with any non-file-operation driver hooks.
2419 *
2420 * Returns: any poll events that are ready without sleeping
2421 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002422static __poll_t i915_perf_poll_locked(struct i915_perf_stream *stream,
2423 struct file *file,
2424 poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00002425{
Al Viroafc9a422017-07-03 06:39:46 -04002426 __poll_t events = 0;
Robert Braggeec688e2016-11-07 19:49:47 +00002427
2428 stream->ops->poll_wait(stream, file, wait);
2429
Robert Braggd7965152016-11-07 19:49:52 +00002430 /* Note: we don't explicitly check whether there's something to read
2431 * here since this path may be very hot depending on what else
2432 * userspace is polling, or on the timeout in use. We rely solely on
2433 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
2434 * samples to read.
2435 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002436 if (stream->pollin)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002437 events |= EPOLLIN;
Robert Braggeec688e2016-11-07 19:49:47 +00002438
Robert Braggd7965152016-11-07 19:49:52 +00002439 return events;
Robert Braggeec688e2016-11-07 19:49:47 +00002440}
2441
Robert Bragg16d98b32016-12-07 21:40:33 +00002442/**
2443 * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
2444 * @file: An i915 perf stream file
2445 * @wait: poll() state table
2446 *
2447 * For handling userspace polling on an i915 perf stream, this ensures
2448 * poll_wait() gets called with a wait queue that will be woken for new stream
2449 * data.
2450 *
2451 * Note: Implementation deferred to i915_perf_poll_locked()
2452 *
2453 * Returns: any poll events that are ready without sleeping
2454 */
Al Viroafc9a422017-07-03 06:39:46 -04002455static __poll_t i915_perf_poll(struct file *file, poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00002456{
2457 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002458 struct i915_perf *perf = stream->perf;
Al Viroafc9a422017-07-03 06:39:46 -04002459 __poll_t ret;
Robert Braggeec688e2016-11-07 19:49:47 +00002460
Chris Wilson8f8b1172019-10-07 22:09:41 +01002461 mutex_lock(&perf->lock);
2462 ret = i915_perf_poll_locked(stream, file, wait);
2463 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002464
2465 return ret;
2466}
2467
Robert Bragg16d98b32016-12-07 21:40:33 +00002468/**
2469 * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
2470 * @stream: A disabled i915 perf stream
2471 *
2472 * [Re]enables the associated capture of data for this stream.
2473 *
2474 * If a stream was previously enabled then there's currently no intention
2475 * to provide userspace any guarantee about the preservation of previously
2476 * buffered data.
2477 */
Robert Braggeec688e2016-11-07 19:49:47 +00002478static void i915_perf_enable_locked(struct i915_perf_stream *stream)
2479{
2480 if (stream->enabled)
2481 return;
2482
2483 /* Allow stream->ops->enable() to refer to this */
2484 stream->enabled = true;
2485
2486 if (stream->ops->enable)
2487 stream->ops->enable(stream);
2488}
2489
Robert Bragg16d98b32016-12-07 21:40:33 +00002490/**
2491 * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
2492 * @stream: An enabled i915 perf stream
2493 *
2494 * Disables the associated capture of data for this stream.
2495 *
2496 * The intention is that disabling an re-enabling a stream will ideally be
2497 * cheaper than destroying and re-opening a stream with the same configuration,
2498 * though there are no formal guarantees about what state or buffered data
2499 * must be retained between disabling and re-enabling a stream.
2500 *
2501 * Note: while a stream is disabled it's considered an error for userspace
2502 * to attempt to read from the stream (-EIO).
2503 */
Robert Braggeec688e2016-11-07 19:49:47 +00002504static void i915_perf_disable_locked(struct i915_perf_stream *stream)
2505{
2506 if (!stream->enabled)
2507 return;
2508
2509 /* Allow stream->ops->disable() to refer to this */
2510 stream->enabled = false;
2511
2512 if (stream->ops->disable)
2513 stream->ops->disable(stream);
2514}
2515
Robert Bragg16d98b32016-12-07 21:40:33 +00002516/**
2517 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2518 * @stream: An i915 perf stream
2519 * @cmd: the ioctl request
2520 * @arg: the ioctl data
2521 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01002522 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00002523 * with any non-file-operation driver hooks.
2524 *
2525 * Returns: zero on success or a negative error code. Returns -EINVAL for
2526 * an unknown ioctl request.
2527 */
Robert Braggeec688e2016-11-07 19:49:47 +00002528static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
2529 unsigned int cmd,
2530 unsigned long arg)
2531{
2532 switch (cmd) {
2533 case I915_PERF_IOCTL_ENABLE:
2534 i915_perf_enable_locked(stream);
2535 return 0;
2536 case I915_PERF_IOCTL_DISABLE:
2537 i915_perf_disable_locked(stream);
2538 return 0;
2539 }
2540
2541 return -EINVAL;
2542}
2543
Robert Bragg16d98b32016-12-07 21:40:33 +00002544/**
2545 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2546 * @file: An i915 perf stream file
2547 * @cmd: the ioctl request
2548 * @arg: the ioctl data
2549 *
2550 * Implementation deferred to i915_perf_ioctl_locked().
2551 *
2552 * Returns: zero on success or a negative error code. Returns -EINVAL for
2553 * an unknown ioctl request.
2554 */
Robert Braggeec688e2016-11-07 19:49:47 +00002555static long i915_perf_ioctl(struct file *file,
2556 unsigned int cmd,
2557 unsigned long arg)
2558{
2559 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002560 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002561 long ret;
2562
Chris Wilson8f8b1172019-10-07 22:09:41 +01002563 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002564 ret = i915_perf_ioctl_locked(stream, cmd, arg);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002565 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002566
2567 return ret;
2568}
2569
Robert Bragg16d98b32016-12-07 21:40:33 +00002570/**
2571 * i915_perf_destroy_locked - destroy an i915 perf stream
2572 * @stream: An i915 perf stream
2573 *
2574 * Frees all resources associated with the given i915 perf @stream, disabling
2575 * any associated data capture in the process.
2576 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01002577 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00002578 * with any non-file-operation driver hooks.
2579 */
Robert Braggeec688e2016-11-07 19:49:47 +00002580static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
2581{
Robert Braggeec688e2016-11-07 19:49:47 +00002582 if (stream->enabled)
2583 i915_perf_disable_locked(stream);
2584
2585 if (stream->ops->destroy)
2586 stream->ops->destroy(stream);
2587
2588 list_del(&stream->link);
2589
Chris Wilson69df05e2016-12-18 15:37:21 +00002590 if (stream->ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01002591 i915_gem_context_put(stream->ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002592
2593 kfree(stream);
2594}
2595
Robert Bragg16d98b32016-12-07 21:40:33 +00002596/**
2597 * i915_perf_release - handles userspace close() of a stream file
2598 * @inode: anonymous inode associated with file
2599 * @file: An i915 perf stream file
2600 *
2601 * Cleans up any resources associated with an open i915 perf stream file.
2602 *
2603 * NB: close() can't really fail from the userspace point of view.
2604 *
2605 * Returns: zero on success or a negative error code.
2606 */
Robert Braggeec688e2016-11-07 19:49:47 +00002607static int i915_perf_release(struct inode *inode, struct file *file)
2608{
2609 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002610 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002611
Chris Wilson8f8b1172019-10-07 22:09:41 +01002612 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002613 i915_perf_destroy_locked(stream);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002614 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002615
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002616 /* Release the reference the perf stream kept on the driver. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002617 drm_dev_put(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002618
Robert Braggeec688e2016-11-07 19:49:47 +00002619 return 0;
2620}
2621
2622
2623static const struct file_operations fops = {
2624 .owner = THIS_MODULE,
2625 .llseek = no_llseek,
2626 .release = i915_perf_release,
2627 .poll = i915_perf_poll,
2628 .read = i915_perf_read,
2629 .unlocked_ioctl = i915_perf_ioctl,
Lionel Landwerlin191f8962017-10-24 16:27:28 +01002630 /* Our ioctl have no arguments, so it's safe to use the same function
2631 * to handle 32bits compatibility.
2632 */
2633 .compat_ioctl = i915_perf_ioctl,
Robert Braggeec688e2016-11-07 19:49:47 +00002634};
2635
2636
Robert Bragg16d98b32016-12-07 21:40:33 +00002637/**
2638 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
Chris Wilson8f8b1172019-10-07 22:09:41 +01002639 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00002640 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
2641 * @props: individually validated u64 property value pairs
2642 * @file: drm file
2643 *
2644 * See i915_perf_ioctl_open() for interface details.
2645 *
2646 * Implements further stream config validation and stream initialization on
Chris Wilson8f8b1172019-10-07 22:09:41 +01002647 * behalf of i915_perf_open_ioctl() with the &perf->lock mutex
Robert Bragg16d98b32016-12-07 21:40:33 +00002648 * taken to serialize with any non-file-operation driver hooks.
2649 *
2650 * Note: at this point the @props have only been validated in isolation and
2651 * it's still necessary to validate that the combination of properties makes
2652 * sense.
2653 *
2654 * In the case where userspace is interested in OA unit metrics then further
2655 * config validation and stream initialization details will be handled by
2656 * i915_oa_stream_init(). The code here should only validate config state that
2657 * will be relevant to all stream types / backends.
2658 *
2659 * Returns: zero on success or a negative error code.
2660 */
Robert Braggeec688e2016-11-07 19:49:47 +00002661static int
Chris Wilson8f8b1172019-10-07 22:09:41 +01002662i915_perf_open_ioctl_locked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00002663 struct drm_i915_perf_open_param *param,
2664 struct perf_open_properties *props,
2665 struct drm_file *file)
2666{
2667 struct i915_gem_context *specific_ctx = NULL;
2668 struct i915_perf_stream *stream = NULL;
2669 unsigned long f_flags = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01002670 bool privileged_op = true;
Robert Braggeec688e2016-11-07 19:49:47 +00002671 int stream_fd;
2672 int ret;
2673
2674 if (props->single_context) {
2675 u32 ctx_handle = props->ctx_handle;
2676 struct drm_i915_file_private *file_priv = file->driver_priv;
2677
Imre Deak635f56c2017-07-14 18:12:41 +03002678 specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle);
2679 if (!specific_ctx) {
2680 DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n",
2681 ctx_handle);
2682 ret = -ENOENT;
Robert Braggeec688e2016-11-07 19:49:47 +00002683 goto err;
2684 }
2685 }
2686
Robert Bragg19f81df2017-06-13 12:23:03 +01002687 /*
2688 * On Haswell the OA unit supports clock gating off for a specific
2689 * context and in this mode there's no visibility of metrics for the
2690 * rest of the system, which we consider acceptable for a
2691 * non-privileged client.
2692 *
2693 * For Gen8+ the OA unit no longer supports clock gating off for a
2694 * specific context and the kernel can't securely stop the counters
2695 * from updating as system-wide / global values. Even though we can
2696 * filter reports based on the included context ID we can't block
2697 * clients from seeing the raw / global counter values via
2698 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
2699 * enable the OA unit by default.
2700 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002701 if (IS_HASWELL(perf->i915) && specific_ctx)
Robert Bragg19f81df2017-06-13 12:23:03 +01002702 privileged_op = false;
2703
Robert Braggccdf6342016-11-07 19:49:54 +00002704 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
2705 * we check a dev.i915.perf_stream_paranoid sysctl option
2706 * to determine if it's ok to access system wide OA counters
2707 * without CAP_SYS_ADMIN privileges.
2708 */
Robert Bragg19f81df2017-06-13 12:23:03 +01002709 if (privileged_op &&
Robert Braggccdf6342016-11-07 19:49:54 +00002710 i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002711 DRM_DEBUG("Insufficient privileges to open system-wide i915 perf stream\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002712 ret = -EACCES;
2713 goto err_ctx;
2714 }
2715
2716 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2717 if (!stream) {
2718 ret = -ENOMEM;
2719 goto err_ctx;
2720 }
2721
Chris Wilson8f8b1172019-10-07 22:09:41 +01002722 stream->perf = perf;
2723 stream->gt = &perf->i915->gt;
Robert Braggeec688e2016-11-07 19:49:47 +00002724 stream->ctx = specific_ctx;
2725
Robert Braggd7965152016-11-07 19:49:52 +00002726 ret = i915_oa_stream_init(stream, param, props);
2727 if (ret)
2728 goto err_alloc;
2729
2730 /* we avoid simply assigning stream->sample_flags = props->sample_flags
2731 * to have _stream_init check the combination of sample flags more
2732 * thoroughly, but still this is the expected result at this point.
Robert Braggeec688e2016-11-07 19:49:47 +00002733 */
Robert Braggd7965152016-11-07 19:49:52 +00002734 if (WARN_ON(stream->sample_flags != props->sample_flags)) {
2735 ret = -ENODEV;
Matthew Auld22f880c2017-03-27 21:34:59 +01002736 goto err_flags;
Robert Braggd7965152016-11-07 19:49:52 +00002737 }
Robert Braggeec688e2016-11-07 19:49:47 +00002738
Chris Wilson8f8b1172019-10-07 22:09:41 +01002739 list_add(&stream->link, &perf->streams);
Robert Braggeec688e2016-11-07 19:49:47 +00002740
2741 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
2742 f_flags |= O_CLOEXEC;
2743 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
2744 f_flags |= O_NONBLOCK;
2745
2746 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
2747 if (stream_fd < 0) {
2748 ret = stream_fd;
2749 goto err_open;
2750 }
2751
2752 if (!(param->flags & I915_PERF_FLAG_DISABLED))
2753 i915_perf_enable_locked(stream);
2754
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002755 /* Take a reference on the driver that will be kept with stream_fd
2756 * until its release.
2757 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002758 drm_dev_get(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002759
Robert Braggeec688e2016-11-07 19:49:47 +00002760 return stream_fd;
2761
2762err_open:
2763 list_del(&stream->link);
Matthew Auld22f880c2017-03-27 21:34:59 +01002764err_flags:
Robert Braggeec688e2016-11-07 19:49:47 +00002765 if (stream->ops->destroy)
2766 stream->ops->destroy(stream);
2767err_alloc:
2768 kfree(stream);
2769err_ctx:
Chris Wilson69df05e2016-12-18 15:37:21 +00002770 if (specific_ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01002771 i915_gem_context_put(specific_ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002772err:
2773 return ret;
2774}
2775
Chris Wilson8f8b1172019-10-07 22:09:41 +01002776static u64 oa_exponent_to_ns(struct i915_perf *perf, int exponent)
Robert Bragg155e9412017-06-13 12:23:05 +01002777{
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01002778 return div64_u64(1000000000ULL * (2ULL << exponent),
Chris Wilson8f8b1172019-10-07 22:09:41 +01002779 1000ULL * RUNTIME_INFO(perf->i915)->cs_timestamp_frequency_khz);
Robert Bragg155e9412017-06-13 12:23:05 +01002780}
2781
Robert Bragg16d98b32016-12-07 21:40:33 +00002782/**
2783 * read_properties_unlocked - validate + copy userspace stream open properties
Chris Wilson8f8b1172019-10-07 22:09:41 +01002784 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00002785 * @uprops: The array of u64 key value pairs given by userspace
2786 * @n_props: The number of key value pairs expected in @uprops
2787 * @props: The stream configuration built up while validating properties
Robert Braggeec688e2016-11-07 19:49:47 +00002788 *
2789 * Note this function only validates properties in isolation it doesn't
2790 * validate that the combination of properties makes sense or that all
2791 * properties necessary for a particular kind of stream have been set.
Robert Bragg16d98b32016-12-07 21:40:33 +00002792 *
2793 * Note that there currently aren't any ordering requirements for properties so
2794 * we shouldn't validate or assume anything about ordering here. This doesn't
2795 * rule out defining new properties with ordering requirements in the future.
Robert Braggeec688e2016-11-07 19:49:47 +00002796 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002797static int read_properties_unlocked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00002798 u64 __user *uprops,
2799 u32 n_props,
2800 struct perf_open_properties *props)
2801{
2802 u64 __user *uprop = uprops;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002803 u32 i;
Robert Braggeec688e2016-11-07 19:49:47 +00002804
2805 memset(props, 0, sizeof(struct perf_open_properties));
2806
2807 if (!n_props) {
Robert Bragg77085502016-12-01 17:21:52 +00002808 DRM_DEBUG("No i915 perf properties given\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002809 return -EINVAL;
2810 }
2811
2812 /* Considering that ID = 0 is reserved and assuming that we don't
2813 * (currently) expect any configurations to ever specify duplicate
2814 * values for a particular property ID then the last _PROP_MAX value is
2815 * one greater than the maximum number of properties we expect to get
2816 * from userspace.
2817 */
2818 if (n_props >= DRM_I915_PERF_PROP_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002819 DRM_DEBUG("More i915 perf properties specified than exist\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002820 return -EINVAL;
2821 }
2822
2823 for (i = 0; i < n_props; i++) {
Robert Bragg00319ba2016-11-07 19:49:55 +00002824 u64 oa_period, oa_freq_hz;
Robert Braggeec688e2016-11-07 19:49:47 +00002825 u64 id, value;
2826 int ret;
2827
2828 ret = get_user(id, uprop);
2829 if (ret)
2830 return ret;
2831
2832 ret = get_user(value, uprop + 1);
2833 if (ret)
2834 return ret;
2835
Matthew Auld0a309f92017-03-27 21:32:36 +01002836 if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
2837 DRM_DEBUG("Unknown i915 perf property ID\n");
2838 return -EINVAL;
2839 }
2840
Robert Braggeec688e2016-11-07 19:49:47 +00002841 switch ((enum drm_i915_perf_property_id)id) {
2842 case DRM_I915_PERF_PROP_CTX_HANDLE:
2843 props->single_context = 1;
2844 props->ctx_handle = value;
2845 break;
Robert Braggd7965152016-11-07 19:49:52 +00002846 case DRM_I915_PERF_PROP_SAMPLE_OA:
Lionel Landwerlinb6dd47b2018-03-26 10:08:22 +01002847 if (value)
2848 props->sample_flags |= SAMPLE_OA_REPORT;
Robert Braggd7965152016-11-07 19:49:52 +00002849 break;
2850 case DRM_I915_PERF_PROP_OA_METRICS_SET:
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002851 if (value == 0) {
Robert Bragg77085502016-12-01 17:21:52 +00002852 DRM_DEBUG("Unknown OA metric set ID\n");
Robert Braggd7965152016-11-07 19:49:52 +00002853 return -EINVAL;
2854 }
2855 props->metrics_set = value;
2856 break;
2857 case DRM_I915_PERF_PROP_OA_FORMAT:
2858 if (value == 0 || value >= I915_OA_FORMAT_MAX) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002859 DRM_DEBUG("Out-of-range OA report format %llu\n",
2860 value);
Robert Braggd7965152016-11-07 19:49:52 +00002861 return -EINVAL;
2862 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01002863 if (!perf->oa_formats[value].size) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002864 DRM_DEBUG("Unsupported OA report format %llu\n",
2865 value);
Robert Braggd7965152016-11-07 19:49:52 +00002866 return -EINVAL;
2867 }
2868 props->oa_format = value;
2869 break;
2870 case DRM_I915_PERF_PROP_OA_EXPONENT:
2871 if (value > OA_EXPONENT_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002872 DRM_DEBUG("OA timer exponent too high (> %u)\n",
2873 OA_EXPONENT_MAX);
Robert Braggd7965152016-11-07 19:49:52 +00002874 return -EINVAL;
2875 }
2876
Robert Bragg00319ba2016-11-07 19:49:55 +00002877 /* Theoretically we can program the OA unit to sample
Robert Bragg155e9412017-06-13 12:23:05 +01002878 * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
2879 * for BXT. We don't allow such high sampling
2880 * frequencies by default unless root.
Robert Braggd7965152016-11-07 19:49:52 +00002881 */
Robert Bragg155e9412017-06-13 12:23:05 +01002882
Robert Bragg00319ba2016-11-07 19:49:55 +00002883 BUILD_BUG_ON(sizeof(oa_period) != 8);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002884 oa_period = oa_exponent_to_ns(perf, value);
Robert Bragg00319ba2016-11-07 19:49:55 +00002885
2886 /* This check is primarily to ensure that oa_period <=
2887 * UINT32_MAX (before passing to do_div which only
2888 * accepts a u32 denominator), but we can also skip
2889 * checking anything < 1Hz which implicitly can't be
2890 * limited via an integer oa_max_sample_rate.
2891 */
2892 if (oa_period <= NSEC_PER_SEC) {
2893 u64 tmp = NSEC_PER_SEC;
2894 do_div(tmp, oa_period);
2895 oa_freq_hz = tmp;
2896 } else
2897 oa_freq_hz = 0;
2898
2899 if (oa_freq_hz > i915_oa_max_sample_rate &&
2900 !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002901 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 +00002902 i915_oa_max_sample_rate);
Robert Braggd7965152016-11-07 19:49:52 +00002903 return -EACCES;
2904 }
2905
2906 props->oa_periodic = true;
2907 props->oa_period_exponent = value;
2908 break;
Matthew Auld0a309f92017-03-27 21:32:36 +01002909 case DRM_I915_PERF_PROP_MAX:
Robert Braggeec688e2016-11-07 19:49:47 +00002910 MISSING_CASE(id);
Robert Braggeec688e2016-11-07 19:49:47 +00002911 return -EINVAL;
2912 }
2913
2914 uprop += 2;
2915 }
2916
2917 return 0;
2918}
2919
Robert Bragg16d98b32016-12-07 21:40:33 +00002920/**
2921 * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
2922 * @dev: drm device
2923 * @data: ioctl data copied from userspace (unvalidated)
2924 * @file: drm file
2925 *
2926 * Validates the stream open parameters given by userspace including flags
2927 * and an array of u64 key, value pair properties.
2928 *
2929 * Very little is assumed up front about the nature of the stream being
2930 * opened (for instance we don't assume it's for periodic OA unit metrics). An
2931 * i915-perf stream is expected to be a suitable interface for other forms of
2932 * buffered data written by the GPU besides periodic OA metrics.
2933 *
2934 * Note we copy the properties from userspace outside of the i915 perf
2935 * mutex to avoid an awkward lockdep with mmap_sem.
2936 *
2937 * Most of the implementation details are handled by
Chris Wilson8f8b1172019-10-07 22:09:41 +01002938 * i915_perf_open_ioctl_locked() after taking the &perf->lock
Robert Bragg16d98b32016-12-07 21:40:33 +00002939 * mutex for serializing with any non-file-operation driver hooks.
2940 *
2941 * Return: A newly opened i915 Perf stream file descriptor or negative
2942 * error code on failure.
2943 */
Robert Braggeec688e2016-11-07 19:49:47 +00002944int i915_perf_open_ioctl(struct drm_device *dev, void *data,
2945 struct drm_file *file)
2946{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002947 struct i915_perf *perf = &to_i915(dev)->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002948 struct drm_i915_perf_open_param *param = data;
2949 struct perf_open_properties props;
2950 u32 known_open_flags;
2951 int ret;
2952
Chris Wilson8f8b1172019-10-07 22:09:41 +01002953 if (!perf->i915) {
Robert Bragg77085502016-12-01 17:21:52 +00002954 DRM_DEBUG("i915 perf interface not available for this system\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002955 return -ENOTSUPP;
2956 }
2957
2958 known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
2959 I915_PERF_FLAG_FD_NONBLOCK |
2960 I915_PERF_FLAG_DISABLED;
2961 if (param->flags & ~known_open_flags) {
Robert Bragg77085502016-12-01 17:21:52 +00002962 DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002963 return -EINVAL;
2964 }
2965
Chris Wilson8f8b1172019-10-07 22:09:41 +01002966 ret = read_properties_unlocked(perf,
Robert Braggeec688e2016-11-07 19:49:47 +00002967 u64_to_user_ptr(param->properties_ptr),
2968 param->num_properties,
2969 &props);
2970 if (ret)
2971 return ret;
2972
Chris Wilson8f8b1172019-10-07 22:09:41 +01002973 mutex_lock(&perf->lock);
2974 ret = i915_perf_open_ioctl_locked(perf, param, &props, file);
2975 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002976
2977 return ret;
2978}
2979
Robert Bragg16d98b32016-12-07 21:40:33 +00002980/**
2981 * i915_perf_register - exposes i915-perf to userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01002982 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00002983 *
2984 * In particular OA metric sets are advertised under a sysfs metrics/
2985 * directory allowing userspace to enumerate valid IDs that can be
2986 * used to open an i915-perf stream.
2987 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002988void i915_perf_register(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00002989{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002990 struct i915_perf *perf = &i915->perf;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002991 int ret;
2992
Chris Wilson8f8b1172019-10-07 22:09:41 +01002993 if (!perf->i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00002994 return;
2995
2996 /* To be sure we're synchronized with an attempted
2997 * i915_perf_open_ioctl(); considering that we register after
2998 * being exposed to userspace.
2999 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003000 mutex_lock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003001
Chris Wilson8f8b1172019-10-07 22:09:41 +01003002 perf->metrics_kobj =
Robert Bragg442b8c02016-11-07 19:49:53 +00003003 kobject_create_and_add("metrics",
Chris Wilson8f8b1172019-10-07 22:09:41 +01003004 &i915->drm.primary->kdev->kobj);
3005 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003006 goto exit;
3007
Chris Wilson8f8b1172019-10-07 22:09:41 +01003008 sysfs_attr_init(&perf->test_config.sysfs_metric_id.attr);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003009
Chris Wilson8f8b1172019-10-07 22:09:41 +01003010 if (INTEL_GEN(i915) >= 11) {
3011 i915_perf_load_test_config_icl(i915);
3012 } else if (IS_CANNONLAKE(i915)) {
3013 i915_perf_load_test_config_cnl(i915);
3014 } else if (IS_COFFEELAKE(i915)) {
3015 if (IS_CFL_GT2(i915))
3016 i915_perf_load_test_config_cflgt2(i915);
3017 if (IS_CFL_GT3(i915))
3018 i915_perf_load_test_config_cflgt3(i915);
3019 } else if (IS_GEMINILAKE(i915)) {
3020 i915_perf_load_test_config_glk(i915);
3021 } else if (IS_KABYLAKE(i915)) {
3022 if (IS_KBL_GT2(i915))
3023 i915_perf_load_test_config_kblgt2(i915);
3024 else if (IS_KBL_GT3(i915))
3025 i915_perf_load_test_config_kblgt3(i915);
3026 } else if (IS_BROXTON(i915)) {
3027 i915_perf_load_test_config_bxt(i915);
3028 } else if (IS_SKYLAKE(i915)) {
3029 if (IS_SKL_GT2(i915))
3030 i915_perf_load_test_config_sklgt2(i915);
3031 else if (IS_SKL_GT3(i915))
3032 i915_perf_load_test_config_sklgt3(i915);
3033 else if (IS_SKL_GT4(i915))
3034 i915_perf_load_test_config_sklgt4(i915);
3035 } else if (IS_CHERRYVIEW(i915)) {
3036 i915_perf_load_test_config_chv(i915);
3037 } else if (IS_BROADWELL(i915)) {
3038 i915_perf_load_test_config_bdw(i915);
3039 } else if (IS_HASWELL(i915)) {
3040 i915_perf_load_test_config_hsw(i915);
3041 }
Robert Bragg442b8c02016-11-07 19:49:53 +00003042
Chris Wilson8f8b1172019-10-07 22:09:41 +01003043 if (perf->test_config.id == 0)
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003044 goto sysfs_error;
3045
Chris Wilson8f8b1172019-10-07 22:09:41 +01003046 ret = sysfs_create_group(perf->metrics_kobj,
3047 &perf->test_config.sysfs_metric);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003048 if (ret)
3049 goto sysfs_error;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003050
Chris Wilson8f8b1172019-10-07 22:09:41 +01003051 atomic_set(&perf->test_config.ref_count, 1);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003052
Robert Bragg19f81df2017-06-13 12:23:03 +01003053 goto exit;
3054
3055sysfs_error:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003056 kobject_put(perf->metrics_kobj);
3057 perf->metrics_kobj = NULL;
Robert Bragg19f81df2017-06-13 12:23:03 +01003058
Robert Bragg442b8c02016-11-07 19:49:53 +00003059exit:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003060 mutex_unlock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003061}
3062
Robert Bragg16d98b32016-12-07 21:40:33 +00003063/**
3064 * i915_perf_unregister - hide i915-perf from userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01003065 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003066 *
3067 * i915-perf state cleanup is split up into an 'unregister' and
3068 * 'deinit' phase where the interface is first hidden from
3069 * userspace by i915_perf_unregister() before cleaning up
3070 * remaining state in i915_perf_fini().
3071 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003072void i915_perf_unregister(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003073{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003074 struct i915_perf *perf = &i915->perf;
3075
3076 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003077 return;
3078
Chris Wilson8f8b1172019-10-07 22:09:41 +01003079 sysfs_remove_group(perf->metrics_kobj,
3080 &perf->test_config.sysfs_metric);
Robert Bragg442b8c02016-11-07 19:49:53 +00003081
Chris Wilson8f8b1172019-10-07 22:09:41 +01003082 kobject_put(perf->metrics_kobj);
3083 perf->metrics_kobj = NULL;
Robert Bragg442b8c02016-11-07 19:49:53 +00003084}
3085
Chris Wilson8f8b1172019-10-07 22:09:41 +01003086static bool gen8_is_valid_flex_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003087{
3088 static const i915_reg_t flex_eu_regs[] = {
3089 EU_PERF_CNTL0,
3090 EU_PERF_CNTL1,
3091 EU_PERF_CNTL2,
3092 EU_PERF_CNTL3,
3093 EU_PERF_CNTL4,
3094 EU_PERF_CNTL5,
3095 EU_PERF_CNTL6,
3096 };
3097 int i;
3098
3099 for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) {
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003100 if (i915_mmio_reg_offset(flex_eu_regs[i]) == addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003101 return true;
3102 }
3103 return false;
3104}
3105
Chris Wilson8f8b1172019-10-07 22:09:41 +01003106static bool gen7_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003107{
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003108 return (addr >= i915_mmio_reg_offset(OASTARTTRIG1) &&
3109 addr <= i915_mmio_reg_offset(OASTARTTRIG8)) ||
3110 (addr >= i915_mmio_reg_offset(OAREPORTTRIG1) &&
3111 addr <= i915_mmio_reg_offset(OAREPORTTRIG8)) ||
3112 (addr >= i915_mmio_reg_offset(OACEC0_0) &&
3113 addr <= i915_mmio_reg_offset(OACEC7_1));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003114}
3115
Chris Wilson8f8b1172019-10-07 22:09:41 +01003116static bool gen7_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003117{
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003118 return addr == i915_mmio_reg_offset(HALF_SLICE_CHICKEN2) ||
3119 (addr >= i915_mmio_reg_offset(MICRO_BP0_0) &&
3120 addr <= i915_mmio_reg_offset(NOA_WRITE)) ||
3121 (addr >= i915_mmio_reg_offset(OA_PERFCNT1_LO) &&
3122 addr <= i915_mmio_reg_offset(OA_PERFCNT2_HI)) ||
3123 (addr >= i915_mmio_reg_offset(OA_PERFMATRIX_LO) &&
3124 addr <= i915_mmio_reg_offset(OA_PERFMATRIX_HI));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003125}
3126
Chris Wilson8f8b1172019-10-07 22:09:41 +01003127static bool gen8_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003128{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003129 return gen7_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003130 addr == i915_mmio_reg_offset(WAIT_FOR_RC6_EXIT) ||
3131 (addr >= i915_mmio_reg_offset(RPM_CONFIG0) &&
3132 addr <= i915_mmio_reg_offset(NOA_CONFIG(8)));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003133}
3134
Chris Wilson8f8b1172019-10-07 22:09:41 +01003135static bool gen10_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003136{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003137 return gen8_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlinbf210f62019-06-02 01:58:45 +03003138 addr == i915_mmio_reg_offset(GEN10_NOA_WRITE_HIGH) ||
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003139 (addr >= i915_mmio_reg_offset(OA_PERFCNT3_LO) &&
3140 addr <= i915_mmio_reg_offset(OA_PERFCNT4_HI));
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003141}
3142
Chris Wilson8f8b1172019-10-07 22:09:41 +01003143static bool hsw_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003144{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003145 return gen7_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003146 (addr >= 0x25100 && addr <= 0x2FF90) ||
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003147 (addr >= i915_mmio_reg_offset(HSW_MBVID2_NOA0) &&
3148 addr <= i915_mmio_reg_offset(HSW_MBVID2_NOA9)) ||
3149 addr == i915_mmio_reg_offset(HSW_MBVID2_MISR0);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003150}
3151
Chris Wilson8f8b1172019-10-07 22:09:41 +01003152static bool chv_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003153{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003154 return gen7_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003155 (addr >= 0x182300 && addr <= 0x1823A4);
3156}
3157
Jani Nikula739f3ab2019-01-16 11:15:19 +02003158static u32 mask_reg_value(u32 reg, u32 val)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003159{
3160 /* HALF_SLICE_CHICKEN2 is programmed with a the
3161 * WaDisableSTUnitPowerOptimization workaround. Make sure the value
3162 * programmed by userspace doesn't change this.
3163 */
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003164 if (i915_mmio_reg_offset(HALF_SLICE_CHICKEN2) == reg)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003165 val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE);
3166
3167 /* WAIT_FOR_RC6_EXIT has only one bit fullfilling the function
3168 * indicated by its name and a bunch of selection fields used by OA
3169 * configs.
3170 */
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003171 if (i915_mmio_reg_offset(WAIT_FOR_RC6_EXIT) == reg)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003172 val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE);
3173
3174 return val;
3175}
3176
Chris Wilson8f8b1172019-10-07 22:09:41 +01003177static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,
3178 bool (*is_valid)(struct i915_perf *perf, u32 addr),
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003179 u32 __user *regs,
3180 u32 n_regs)
3181{
3182 struct i915_oa_reg *oa_regs;
3183 int err;
3184 u32 i;
3185
3186 if (!n_regs)
3187 return NULL;
3188
Linus Torvalds96d4f262019-01-03 18:57:57 -08003189 if (!access_ok(regs, n_regs * sizeof(u32) * 2))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003190 return ERR_PTR(-EFAULT);
3191
3192 /* No is_valid function means we're not allowing any register to be programmed. */
3193 GEM_BUG_ON(!is_valid);
3194 if (!is_valid)
3195 return ERR_PTR(-EINVAL);
3196
3197 oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL);
3198 if (!oa_regs)
3199 return ERR_PTR(-ENOMEM);
3200
3201 for (i = 0; i < n_regs; i++) {
3202 u32 addr, value;
3203
3204 err = get_user(addr, regs);
3205 if (err)
3206 goto addr_err;
3207
Chris Wilson8f8b1172019-10-07 22:09:41 +01003208 if (!is_valid(perf, addr)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003209 DRM_DEBUG("Invalid oa_reg address: %X\n", addr);
3210 err = -EINVAL;
3211 goto addr_err;
3212 }
3213
3214 err = get_user(value, regs + 1);
3215 if (err)
3216 goto addr_err;
3217
3218 oa_regs[i].addr = _MMIO(addr);
3219 oa_regs[i].value = mask_reg_value(addr, value);
3220
3221 regs += 2;
3222 }
3223
3224 return oa_regs;
3225
3226addr_err:
3227 kfree(oa_regs);
3228 return ERR_PTR(err);
3229}
3230
3231static ssize_t show_dynamic_id(struct device *dev,
3232 struct device_attribute *attr,
3233 char *buf)
3234{
3235 struct i915_oa_config *oa_config =
3236 container_of(attr, typeof(*oa_config), sysfs_metric_id);
3237
3238 return sprintf(buf, "%d\n", oa_config->id);
3239}
3240
Chris Wilson8f8b1172019-10-07 22:09:41 +01003241static int create_dynamic_oa_sysfs_entry(struct i915_perf *perf,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003242 struct i915_oa_config *oa_config)
3243{
Chris Wilson28152a22017-08-03 23:37:00 +01003244 sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003245 oa_config->sysfs_metric_id.attr.name = "id";
3246 oa_config->sysfs_metric_id.attr.mode = S_IRUGO;
3247 oa_config->sysfs_metric_id.show = show_dynamic_id;
3248 oa_config->sysfs_metric_id.store = NULL;
3249
3250 oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr;
3251 oa_config->attrs[1] = NULL;
3252
3253 oa_config->sysfs_metric.name = oa_config->uuid;
3254 oa_config->sysfs_metric.attrs = oa_config->attrs;
3255
Chris Wilson8f8b1172019-10-07 22:09:41 +01003256 return sysfs_create_group(perf->metrics_kobj,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003257 &oa_config->sysfs_metric);
3258}
3259
3260/**
3261 * i915_perf_add_config_ioctl - DRM ioctl() for userspace to add a new OA config
3262 * @dev: drm device
3263 * @data: ioctl data (pointer to struct drm_i915_perf_oa_config) copied from
3264 * userspace (unvalidated)
3265 * @file: drm file
3266 *
3267 * Validates the submitted OA register to be saved into a new OA config that
3268 * can then be used for programming the OA unit and its NOA network.
3269 *
3270 * Returns: A new allocated config number to be used with the perf open ioctl
3271 * or a negative error code on failure.
3272 */
3273int i915_perf_add_config_ioctl(struct drm_device *dev, void *data,
3274 struct drm_file *file)
3275{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003276 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003277 struct drm_i915_perf_oa_config *args = data;
3278 struct i915_oa_config *oa_config, *tmp;
3279 int err, id;
3280
Chris Wilson8f8b1172019-10-07 22:09:41 +01003281 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003282 DRM_DEBUG("i915 perf interface not available for this system\n");
3283 return -ENOTSUPP;
3284 }
3285
Chris Wilson8f8b1172019-10-07 22:09:41 +01003286 if (!perf->metrics_kobj) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003287 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
3288 return -EINVAL;
3289 }
3290
3291 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
3292 DRM_DEBUG("Insufficient privileges to add i915 OA config\n");
3293 return -EACCES;
3294 }
3295
3296 if ((!args->mux_regs_ptr || !args->n_mux_regs) &&
3297 (!args->boolean_regs_ptr || !args->n_boolean_regs) &&
3298 (!args->flex_regs_ptr || !args->n_flex_regs)) {
3299 DRM_DEBUG("No OA registers given\n");
3300 return -EINVAL;
3301 }
3302
3303 oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL);
3304 if (!oa_config) {
3305 DRM_DEBUG("Failed to allocate memory for the OA config\n");
3306 return -ENOMEM;
3307 }
3308
3309 atomic_set(&oa_config->ref_count, 1);
3310
3311 if (!uuid_is_valid(args->uuid)) {
3312 DRM_DEBUG("Invalid uuid format for OA config\n");
3313 err = -EINVAL;
3314 goto reg_err;
3315 }
3316
3317 /* Last character in oa_config->uuid will be 0 because oa_config is
3318 * kzalloc.
3319 */
3320 memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid));
3321
3322 oa_config->mux_regs_len = args->n_mux_regs;
3323 oa_config->mux_regs =
Chris Wilson8f8b1172019-10-07 22:09:41 +01003324 alloc_oa_regs(perf,
3325 perf->ops.is_valid_mux_reg,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003326 u64_to_user_ptr(args->mux_regs_ptr),
3327 args->n_mux_regs);
3328
3329 if (IS_ERR(oa_config->mux_regs)) {
3330 DRM_DEBUG("Failed to create OA config for mux_regs\n");
3331 err = PTR_ERR(oa_config->mux_regs);
3332 goto reg_err;
3333 }
3334
3335 oa_config->b_counter_regs_len = args->n_boolean_regs;
3336 oa_config->b_counter_regs =
Chris Wilson8f8b1172019-10-07 22:09:41 +01003337 alloc_oa_regs(perf,
3338 perf->ops.is_valid_b_counter_reg,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003339 u64_to_user_ptr(args->boolean_regs_ptr),
3340 args->n_boolean_regs);
3341
3342 if (IS_ERR(oa_config->b_counter_regs)) {
3343 DRM_DEBUG("Failed to create OA config for b_counter_regs\n");
3344 err = PTR_ERR(oa_config->b_counter_regs);
3345 goto reg_err;
3346 }
3347
Chris Wilson8f8b1172019-10-07 22:09:41 +01003348 if (INTEL_GEN(perf->i915) < 8) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003349 if (args->n_flex_regs != 0) {
3350 err = -EINVAL;
3351 goto reg_err;
3352 }
3353 } else {
3354 oa_config->flex_regs_len = args->n_flex_regs;
3355 oa_config->flex_regs =
Chris Wilson8f8b1172019-10-07 22:09:41 +01003356 alloc_oa_regs(perf,
3357 perf->ops.is_valid_flex_reg,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003358 u64_to_user_ptr(args->flex_regs_ptr),
3359 args->n_flex_regs);
3360
3361 if (IS_ERR(oa_config->flex_regs)) {
3362 DRM_DEBUG("Failed to create OA config for flex_regs\n");
3363 err = PTR_ERR(oa_config->flex_regs);
3364 goto reg_err;
3365 }
3366 }
3367
Chris Wilson8f8b1172019-10-07 22:09:41 +01003368 err = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003369 if (err)
3370 goto reg_err;
3371
3372 /* We shouldn't have too many configs, so this iteration shouldn't be
3373 * too costly.
3374 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003375 idr_for_each_entry(&perf->metrics_idr, tmp, id) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003376 if (!strcmp(tmp->uuid, oa_config->uuid)) {
3377 DRM_DEBUG("OA config already exists with this uuid\n");
3378 err = -EADDRINUSE;
3379 goto sysfs_err;
3380 }
3381 }
3382
Chris Wilson8f8b1172019-10-07 22:09:41 +01003383 err = create_dynamic_oa_sysfs_entry(perf, oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003384 if (err) {
3385 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
3386 goto sysfs_err;
3387 }
3388
3389 /* Config id 0 is invalid, id 1 for kernel stored test config. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003390 oa_config->id = idr_alloc(&perf->metrics_idr,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003391 oa_config, 2,
3392 0, GFP_KERNEL);
3393 if (oa_config->id < 0) {
3394 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
3395 err = oa_config->id;
3396 goto sysfs_err;
3397 }
3398
Chris Wilson8f8b1172019-10-07 22:09:41 +01003399 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003400
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01003401 DRM_DEBUG("Added config %s id=%i\n", oa_config->uuid, oa_config->id);
3402
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003403 return oa_config->id;
3404
3405sysfs_err:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003406 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003407reg_err:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003408 put_oa_config(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003409 DRM_DEBUG("Failed to add new OA config\n");
3410 return err;
3411}
3412
3413/**
3414 * i915_perf_remove_config_ioctl - DRM ioctl() for userspace to remove an OA config
3415 * @dev: drm device
3416 * @data: ioctl data (pointer to u64 integer) copied from userspace
3417 * @file: drm file
3418 *
3419 * Configs can be removed while being used, the will stop appearing in sysfs
3420 * and their content will be freed when the stream using the config is closed.
3421 *
3422 * Returns: 0 on success or a negative error code on failure.
3423 */
3424int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data,
3425 struct drm_file *file)
3426{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003427 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003428 u64 *arg = data;
3429 struct i915_oa_config *oa_config;
3430 int ret;
3431
Chris Wilson8f8b1172019-10-07 22:09:41 +01003432 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003433 DRM_DEBUG("i915 perf interface not available for this system\n");
3434 return -ENOTSUPP;
3435 }
3436
3437 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
3438 DRM_DEBUG("Insufficient privileges to remove i915 OA config\n");
3439 return -EACCES;
3440 }
3441
Chris Wilson8f8b1172019-10-07 22:09:41 +01003442 ret = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003443 if (ret)
3444 goto lock_err;
3445
Chris Wilson8f8b1172019-10-07 22:09:41 +01003446 oa_config = idr_find(&perf->metrics_idr, *arg);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003447 if (!oa_config) {
3448 DRM_DEBUG("Failed to remove unknown OA config\n");
3449 ret = -ENOENT;
3450 goto config_err;
3451 }
3452
3453 GEM_BUG_ON(*arg != oa_config->id);
3454
Chris Wilson8f8b1172019-10-07 22:09:41 +01003455 sysfs_remove_group(perf->metrics_kobj,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003456 &oa_config->sysfs_metric);
3457
Chris Wilson8f8b1172019-10-07 22:09:41 +01003458 idr_remove(&perf->metrics_idr, *arg);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01003459
3460 DRM_DEBUG("Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
3461
Chris Wilson8f8b1172019-10-07 22:09:41 +01003462 put_oa_config(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003463
3464config_err:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003465 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003466lock_err:
3467 return ret;
3468}
3469
Robert Braggccdf6342016-11-07 19:49:54 +00003470static struct ctl_table oa_table[] = {
3471 {
3472 .procname = "perf_stream_paranoid",
3473 .data = &i915_perf_stream_paranoid,
3474 .maxlen = sizeof(i915_perf_stream_paranoid),
3475 .mode = 0644,
3476 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07003477 .extra1 = SYSCTL_ZERO,
3478 .extra2 = SYSCTL_ONE,
Robert Braggccdf6342016-11-07 19:49:54 +00003479 },
Robert Bragg00319ba2016-11-07 19:49:55 +00003480 {
3481 .procname = "oa_max_sample_rate",
3482 .data = &i915_oa_max_sample_rate,
3483 .maxlen = sizeof(i915_oa_max_sample_rate),
3484 .mode = 0644,
3485 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07003486 .extra1 = SYSCTL_ZERO,
Robert Bragg00319ba2016-11-07 19:49:55 +00003487 .extra2 = &oa_sample_rate_hard_limit,
3488 },
Robert Braggccdf6342016-11-07 19:49:54 +00003489 {}
3490};
3491
3492static struct ctl_table i915_root[] = {
3493 {
3494 .procname = "i915",
3495 .maxlen = 0,
3496 .mode = 0555,
3497 .child = oa_table,
3498 },
3499 {}
3500};
3501
3502static struct ctl_table dev_root[] = {
3503 {
3504 .procname = "dev",
3505 .maxlen = 0,
3506 .mode = 0555,
3507 .child = i915_root,
3508 },
3509 {}
3510};
3511
Robert Bragg16d98b32016-12-07 21:40:33 +00003512/**
3513 * i915_perf_init - initialize i915-perf state on module load
Chris Wilson8f8b1172019-10-07 22:09:41 +01003514 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003515 *
3516 * Initializes i915-perf state without exposing anything to userspace.
3517 *
3518 * Note: i915-perf initialization is split into an 'init' and 'register'
3519 * phase with the i915_perf_register() exposing state to userspace.
3520 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003521void i915_perf_init(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00003522{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003523 struct i915_perf *perf = &i915->perf;
Robert Braggd7965152016-11-07 19:49:52 +00003524
Chris Wilson8f8b1172019-10-07 22:09:41 +01003525 /* XXX const struct i915_perf_ops! */
3526
3527 if (IS_HASWELL(i915)) {
3528 perf->ops.is_valid_b_counter_reg = gen7_is_valid_b_counter_addr;
3529 perf->ops.is_valid_mux_reg = hsw_is_valid_mux_addr;
3530 perf->ops.is_valid_flex_reg = NULL;
3531 perf->ops.enable_metric_set = hsw_enable_metric_set;
3532 perf->ops.disable_metric_set = hsw_disable_metric_set;
3533 perf->ops.oa_enable = gen7_oa_enable;
3534 perf->ops.oa_disable = gen7_oa_disable;
3535 perf->ops.read = gen7_oa_read;
3536 perf->ops.oa_hw_tail_read = gen7_oa_hw_tail_read;
3537
3538 perf->oa_formats = hsw_oa_formats;
3539 } else if (HAS_LOGICAL_RING_CONTEXTS(i915)) {
Robert Bragg19f81df2017-06-13 12:23:03 +01003540 /* Note: that although we could theoretically also support the
3541 * legacy ringbuffer mode on BDW (and earlier iterations of
3542 * this driver, before upstreaming did this) it didn't seem
3543 * worth the complexity to maintain now that BDW+ enable
3544 * execlist mode by default.
3545 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003546 perf->oa_formats = gen8_plus_oa_formats;
Robert Braggd7965152016-11-07 19:49:52 +00003547
Chris Wilson8f8b1172019-10-07 22:09:41 +01003548 perf->ops.oa_enable = gen8_oa_enable;
3549 perf->ops.oa_disable = gen8_oa_disable;
3550 perf->ops.read = gen8_oa_read;
3551 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003552
Chris Wilson8f8b1172019-10-07 22:09:41 +01003553 if (IS_GEN_RANGE(i915, 8, 9)) {
3554 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003555 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003556 perf->ops.is_valid_mux_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003557 gen8_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003558 perf->ops.is_valid_flex_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003559 gen8_is_valid_flex_addr;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003560
Chris Wilson8f8b1172019-10-07 22:09:41 +01003561 if (IS_CHERRYVIEW(i915)) {
3562 perf->ops.is_valid_mux_reg =
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003563 chv_is_valid_mux_addr;
3564 }
Robert Bragg155e9412017-06-13 12:23:05 +01003565
Chris Wilson8f8b1172019-10-07 22:09:41 +01003566 perf->ops.enable_metric_set = gen8_enable_metric_set;
3567 perf->ops.disable_metric_set = gen8_disable_metric_set;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003568
Chris Wilson8f8b1172019-10-07 22:09:41 +01003569 if (IS_GEN(i915, 8)) {
3570 perf->ctx_oactxctrl_offset = 0x120;
3571 perf->ctx_flexeu0_offset = 0x2ce;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003572
Chris Wilson8f8b1172019-10-07 22:09:41 +01003573 perf->gen8_valid_ctx_bit = BIT(25);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003574 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01003575 perf->ctx_oactxctrl_offset = 0x128;
3576 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003577
Chris Wilson8f8b1172019-10-07 22:09:41 +01003578 perf->gen8_valid_ctx_bit = BIT(16);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003579 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01003580 } else if (IS_GEN_RANGE(i915, 10, 11)) {
3581 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003582 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003583 perf->ops.is_valid_mux_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003584 gen10_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003585 perf->ops.is_valid_flex_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003586 gen8_is_valid_flex_addr;
3587
Chris Wilson8f8b1172019-10-07 22:09:41 +01003588 perf->ops.enable_metric_set = gen8_enable_metric_set;
3589 perf->ops.disable_metric_set = gen10_disable_metric_set;
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003590
Chris Wilson8f8b1172019-10-07 22:09:41 +01003591 if (IS_GEN(i915, 10)) {
3592 perf->ctx_oactxctrl_offset = 0x128;
3593 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03003594 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01003595 perf->ctx_oactxctrl_offset = 0x124;
3596 perf->ctx_flexeu0_offset = 0x78e;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03003597 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01003598 perf->gen8_valid_ctx_bit = BIT(16);
Robert Bragg19f81df2017-06-13 12:23:03 +01003599 }
Robert Bragg19f81df2017-06-13 12:23:03 +01003600 }
3601
Chris Wilson8f8b1172019-10-07 22:09:41 +01003602 if (perf->ops.enable_metric_set) {
3603 INIT_LIST_HEAD(&perf->streams);
3604 mutex_init(&perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01003605
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01003606 oa_sample_rate_hard_limit = 1000 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003607 (RUNTIME_INFO(i915)->cs_timestamp_frequency_khz / 2);
3608 perf->sysctl_header = register_sysctl_table(dev_root);
Robert Bragg19f81df2017-06-13 12:23:03 +01003609
Chris Wilson8f8b1172019-10-07 22:09:41 +01003610 mutex_init(&perf->metrics_lock);
3611 idr_init(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003612
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003613 /* We set up some ratelimit state to potentially throttle any
3614 * _NOTES about spurious, invalid OA reports which we don't
3615 * forward to userspace.
3616 *
3617 * We print a _NOTE about any throttling when closing the
3618 * stream instead of waiting until driver _fini which no one
3619 * would ever see.
3620 *
3621 * Using the same limiting factors as printk_ratelimit()
3622 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003623 ratelimit_state_init(&perf->spurious_report_rs, 5 * HZ, 10);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003624 /* Since we use a DRM_NOTE for spurious reports it would be
3625 * inconsistent to let __ratelimit() automatically print a
3626 * warning for throttling.
3627 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003628 ratelimit_set_flags(&perf->spurious_report_rs,
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003629 RATELIMIT_MSG_ON_RELEASE);
3630
Chris Wilson8f8b1172019-10-07 22:09:41 +01003631 perf->i915 = i915;
Robert Bragg19f81df2017-06-13 12:23:03 +01003632 }
Robert Braggeec688e2016-11-07 19:49:47 +00003633}
3634
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003635static int destroy_config(int id, void *p, void *data)
3636{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003637 put_oa_config(p);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003638 return 0;
3639}
3640
Robert Bragg16d98b32016-12-07 21:40:33 +00003641/**
3642 * i915_perf_fini - Counter part to i915_perf_init()
Chris Wilson8f8b1172019-10-07 22:09:41 +01003643 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003644 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003645void i915_perf_fini(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00003646{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003647 struct i915_perf *perf = &i915->perf;
3648
3649 if (!perf->i915)
Robert Braggeec688e2016-11-07 19:49:47 +00003650 return;
3651
Chris Wilson8f8b1172019-10-07 22:09:41 +01003652 idr_for_each(&perf->metrics_idr, destroy_config, perf);
3653 idr_destroy(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003654
Chris Wilson8f8b1172019-10-07 22:09:41 +01003655 unregister_sysctl_table(perf->sysctl_header);
Robert Braggccdf6342016-11-07 19:49:54 +00003656
Chris Wilson8f8b1172019-10-07 22:09:41 +01003657 memset(&perf->ops, 0, sizeof(perf->ops));
3658 perf->i915 = NULL;
Robert Braggeec688e2016-11-07 19:49:47 +00003659}