blob: 50f2f972020df31071a17d6d29c8e7bc5f3f3159 [file] [log] [blame]
Robert Braggeec688e2016-11-07 19:49:47 +00001/*
2 * Copyright © 2015-2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Robert Bragg <robert@sixbynine.org>
25 */
26
Robert Bragg7abbd8d2016-11-07 19:49:57 +000027
28/**
Robert Bragg16d98b32016-12-07 21:40:33 +000029 * DOC: i915 Perf Overview
Robert Bragg7abbd8d2016-11-07 19:49:57 +000030 *
31 * Gen graphics supports a large number of performance counters that can help
32 * driver and application developers understand and optimize their use of the
33 * GPU.
34 *
35 * This i915 perf interface enables userspace to configure and open a file
36 * descriptor representing a stream of GPU metrics which can then be read() as
37 * a stream of sample records.
38 *
39 * The interface is particularly suited to exposing buffered metrics that are
40 * captured by DMA from the GPU, unsynchronized with and unrelated to the CPU.
41 *
42 * Streams representing a single context are accessible to applications with a
43 * corresponding drm file descriptor, such that OpenGL can use the interface
44 * without special privileges. Access to system-wide metrics requires root
45 * privileges by default, unless changed via the dev.i915.perf_event_paranoid
46 * sysctl option.
47 *
Robert Bragg16d98b32016-12-07 21:40:33 +000048 */
49
50/**
51 * DOC: i915 Perf History and Comparison with Core Perf
Robert Bragg7abbd8d2016-11-07 19:49:57 +000052 *
53 * The interface was initially inspired by the core Perf infrastructure but
54 * some notable differences are:
55 *
56 * i915 perf file descriptors represent a "stream" instead of an "event"; where
57 * a perf event primarily corresponds to a single 64bit value, while a stream
58 * might sample sets of tightly-coupled counters, depending on the
59 * configuration. For example the Gen OA unit isn't designed to support
60 * orthogonal configurations of individual counters; it's configured for a set
61 * of related counters. Samples for an i915 perf stream capturing OA metrics
62 * will include a set of counter values packed in a compact HW specific format.
63 * The OA unit supports a number of different packing formats which can be
64 * selected by the user opening the stream. Perf has support for grouping
65 * events, but each event in the group is configured, validated and
66 * authenticated individually with separate system calls.
67 *
68 * i915 perf stream configurations are provided as an array of u64 (key,value)
69 * pairs, instead of a fixed struct with multiple miscellaneous config members,
70 * interleaved with event-type specific members.
71 *
72 * i915 perf doesn't support exposing metrics via an mmap'd circular buffer.
73 * The supported metrics are being written to memory by the GPU unsynchronized
74 * with the CPU, using HW specific packing formats for counter sets. Sometimes
75 * the constraints on HW configuration require reports to be filtered before it
76 * would be acceptable to expose them to unprivileged applications - to hide
77 * the metrics of other processes/contexts. For these use cases a read() based
78 * interface is a good fit, and provides an opportunity to filter data as it
79 * gets copied from the GPU mapped buffers to userspace buffers.
80 *
81 *
Robert Bragg16d98b32016-12-07 21:40:33 +000082 * Issues hit with first prototype based on Core Perf
83 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Robert Bragg7abbd8d2016-11-07 19:49:57 +000084 *
85 * The first prototype of this driver was based on the core perf
86 * infrastructure, and while we did make that mostly work, with some changes to
87 * perf, we found we were breaking or working around too many assumptions baked
88 * into perf's currently cpu centric design.
89 *
90 * In the end we didn't see a clear benefit to making perf's implementation and
91 * interface more complex by changing design assumptions while we knew we still
92 * wouldn't be able to use any existing perf based userspace tools.
93 *
94 * Also considering the Gen specific nature of the Observability hardware and
95 * how userspace will sometimes need to combine i915 perf OA metrics with
96 * side-band OA data captured via MI_REPORT_PERF_COUNT commands; we're
97 * expecting the interface to be used by a platform specific userspace such as
98 * OpenGL or tools. This is to say; we aren't inherently missing out on having
99 * a standard vendor/architecture agnostic interface by not using perf.
100 *
101 *
102 * For posterity, in case we might re-visit trying to adapt core perf to be
103 * better suited to exposing i915 metrics these were the main pain points we
104 * hit:
105 *
106 * - The perf based OA PMU driver broke some significant design assumptions:
107 *
108 * Existing perf pmus are used for profiling work on a cpu and we were
109 * introducing the idea of _IS_DEVICE pmus with different security
110 * implications, the need to fake cpu-related data (such as user/kernel
111 * registers) to fit with perf's current design, and adding _DEVICE records
112 * as a way to forward device-specific status records.
113 *
114 * The OA unit writes reports of counters into a circular buffer, without
115 * involvement from the CPU, making our PMU driver the first of a kind.
116 *
117 * Given the way we were periodically forward data from the GPU-mapped, OA
118 * buffer to perf's buffer, those bursts of sample writes looked to perf like
119 * we were sampling too fast and so we had to subvert its throttling checks.
120 *
121 * Perf supports groups of counters and allows those to be read via
122 * transactions internally but transactions currently seem designed to be
123 * explicitly initiated from the cpu (say in response to a userspace read())
124 * and while we could pull a report out of the OA buffer we can't
125 * trigger a report from the cpu on demand.
126 *
127 * Related to being report based; the OA counters are configured in HW as a
128 * set while perf generally expects counter configurations to be orthogonal.
129 * Although counters can be associated with a group leader as they are
130 * opened, there's no clear precedent for being able to provide group-wide
131 * configuration attributes (for example we want to let userspace choose the
132 * OA unit report format used to capture all counters in a set, or specify a
133 * GPU context to filter metrics on). We avoided using perf's grouping
134 * feature and forwarded OA reports to userspace via perf's 'raw' sample
135 * field. This suited our userspace well considering how coupled the counters
136 * are when dealing with normalizing. It would be inconvenient to split
137 * counters up into separate events, only to require userspace to recombine
138 * them. For Mesa it's also convenient to be forwarded raw, periodic reports
139 * for combining with the side-band raw reports it captures using
140 * MI_REPORT_PERF_COUNT commands.
141 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000142 * - As a side note on perf's grouping feature; there was also some concern
Robert Bragg7abbd8d2016-11-07 19:49:57 +0000143 * that using PERF_FORMAT_GROUP as a way to pack together counter values
144 * would quite drastically inflate our sample sizes, which would likely
145 * lower the effective sampling resolutions we could use when the available
146 * memory bandwidth is limited.
147 *
148 * With the OA unit's report formats, counters are packed together as 32
149 * or 40bit values, with the largest report size being 256 bytes.
150 *
151 * PERF_FORMAT_GROUP values are 64bit, but there doesn't appear to be a
152 * documented ordering to the values, implying PERF_FORMAT_ID must also be
153 * used to add a 64bit ID before each value; giving 16 bytes per counter.
154 *
155 * Related to counter orthogonality; we can't time share the OA unit, while
156 * event scheduling is a central design idea within perf for allowing
157 * userspace to open + enable more events than can be configured in HW at any
158 * one time. The OA unit is not designed to allow re-configuration while in
159 * use. We can't reconfigure the OA unit without losing internal OA unit
160 * state which we can't access explicitly to save and restore. Reconfiguring
161 * the OA unit is also relatively slow, involving ~100 register writes. From
162 * userspace Mesa also depends on a stable OA configuration when emitting
163 * MI_REPORT_PERF_COUNT commands and importantly the OA unit can't be
164 * disabled while there are outstanding MI_RPC commands lest we hang the
165 * command streamer.
166 *
167 * The contents of sample records aren't extensible by device drivers (i.e.
168 * the sample_type bits). As an example; Sourab Gupta had been looking to
169 * attach GPU timestamps to our OA samples. We were shoehorning OA reports
170 * into sample records by using the 'raw' field, but it's tricky to pack more
171 * than one thing into this field because events/core.c currently only lets a
172 * pmu give a single raw data pointer plus len which will be copied into the
173 * ring buffer. To include more than the OA report we'd have to copy the
174 * report into an intermediate larger buffer. I'd been considering allowing a
175 * vector of data+len values to be specified for copying the raw data, but
176 * it felt like a kludge to being using the raw field for this purpose.
177 *
178 * - It felt like our perf based PMU was making some technical compromises
179 * just for the sake of using perf:
180 *
181 * perf_event_open() requires events to either relate to a pid or a specific
182 * cpu core, while our device pmu related to neither. Events opened with a
183 * pid will be automatically enabled/disabled according to the scheduling of
184 * that process - so not appropriate for us. When an event is related to a
185 * cpu id, perf ensures pmu methods will be invoked via an inter process
186 * interrupt on that core. To avoid invasive changes our userspace opened OA
187 * perf events for a specific cpu. This was workable but it meant the
188 * majority of the OA driver ran in atomic context, including all OA report
189 * forwarding, which wasn't really necessary in our case and seems to make
190 * our locking requirements somewhat complex as we handled the interaction
191 * with the rest of the i915 driver.
192 */
193
Robert Braggeec688e2016-11-07 19:49:47 +0000194#include <linux/anon_inodes.h>
Robert Braggd7965152016-11-07 19:49:52 +0000195#include <linux/sizes.h>
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100196#include <linux/uuid.h>
Robert Braggeec688e2016-11-07 19:49:47 +0000197
Chris Wilson10be98a2019-05-28 10:29:49 +0100198#include "gem/i915_gem_context.h"
Chris Wilsona5efcde2019-10-11 20:03:17 +0100199#include "gt/intel_engine_pm.h"
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100200#include "gt/intel_engine_user.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +0100201#include "gt/intel_lrc_reg.h"
202
Robert Braggeec688e2016-11-07 19:49:47 +0000203#include "i915_drv.h"
Jani Nikuladb94e9f2019-08-08 16:42:44 +0300204#include "i915_perf.h"
Michal Wajdeczko5ed7a0c2019-06-26 12:38:26 +0000205#include "oa/i915_oa_hsw.h"
206#include "oa/i915_oa_bdw.h"
207#include "oa/i915_oa_chv.h"
208#include "oa/i915_oa_sklgt2.h"
209#include "oa/i915_oa_sklgt3.h"
210#include "oa/i915_oa_sklgt4.h"
211#include "oa/i915_oa_bxt.h"
212#include "oa/i915_oa_kblgt2.h"
213#include "oa/i915_oa_kblgt3.h"
214#include "oa/i915_oa_glk.h"
215#include "oa/i915_oa_cflgt2.h"
216#include "oa/i915_oa_cflgt3.h"
217#include "oa/i915_oa_cnl.h"
218#include "oa/i915_oa_icl.h"
Robert Braggd7965152016-11-07 19:49:52 +0000219
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200220/* HW requires this to be a power of two, between 128k and 16M, though driver
221 * is currently generally designed assuming the largest 16M size is used such
222 * that the overflow cases are unlikely in normal operation.
223 */
224#define OA_BUFFER_SIZE SZ_16M
225
226#define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
Robert Braggd7965152016-11-07 19:49:52 +0000227
Robert Bragg0dd860c2017-05-11 16:43:28 +0100228/**
229 * DOC: OA Tail Pointer Race
230 *
231 * There's a HW race condition between OA unit tail pointer register updates and
Robert Braggd7965152016-11-07 19:49:52 +0000232 * writes to memory whereby the tail pointer can sometimes get ahead of what's
Robert Bragg0dd860c2017-05-11 16:43:28 +0100233 * been written out to the OA buffer so far (in terms of what's visible to the
234 * CPU).
Robert Braggd7965152016-11-07 19:49:52 +0000235 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100236 * Although this can be observed explicitly while copying reports to userspace
237 * by checking for a zeroed report-id field in tail reports, we want to account
Robert Bragg19f81df2017-06-13 12:23:03 +0100238 * for this earlier, as part of the oa_buffer_check to avoid lots of redundant
Robert Bragg0dd860c2017-05-11 16:43:28 +0100239 * read() attempts.
Robert Braggd7965152016-11-07 19:49:52 +0000240 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100241 * In effect we define a tail pointer for reading that lags the real tail
242 * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough
243 * time for the corresponding reports to become visible to the CPU.
Robert Braggd7965152016-11-07 19:49:52 +0000244 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100245 * To manage this we actually track two tail pointers:
246 * 1) An 'aging' tail with an associated timestamp that is tracked until we
247 * can trust the corresponding data is visible to the CPU; at which point
248 * it is considered 'aged'.
249 * 2) An 'aged' tail that can be used for read()ing.
250 *
251 * The two separate pointers let us decouple read()s from tail pointer aging.
252 *
253 * The tail pointers are checked and updated at a limited rate within a hrtimer
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800254 * callback (the same callback that is used for delivering EPOLLIN events)
Robert Bragg0dd860c2017-05-11 16:43:28 +0100255 *
256 * Initially the tails are marked invalid with %INVALID_TAIL_PTR which
257 * indicates that an updated tail pointer is needed.
258 *
259 * Most of the implementation details for this workaround are in
Robert Bragg19f81df2017-06-13 12:23:03 +0100260 * oa_buffer_check_unlocked() and _append_oa_reports()
Robert Bragg0dd860c2017-05-11 16:43:28 +0100261 *
262 * Note for posterity: previously the driver used to define an effective tail
263 * pointer that lagged the real pointer by a 'tail margin' measured in bytes
264 * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
265 * This was flawed considering that the OA unit may also automatically generate
266 * non-periodic reports (such as on context switch) or the OA unit may be
267 * enabled without any periodic sampling.
Robert Braggd7965152016-11-07 19:49:52 +0000268 */
269#define OA_TAIL_MARGIN_NSEC 100000ULL
Robert Bragg0dd860c2017-05-11 16:43:28 +0100270#define INVALID_TAIL_PTR 0xffffffff
Robert Braggd7965152016-11-07 19:49:52 +0000271
272/* frequency for checking whether the OA unit has written new reports to the
273 * circular OA buffer...
274 */
275#define POLL_FREQUENCY 200
276#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
277
Robert Braggccdf6342016-11-07 19:49:54 +0000278/* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
Robert Braggccdf6342016-11-07 19:49:54 +0000279static u32 i915_perf_stream_paranoid = true;
280
Robert Braggd7965152016-11-07 19:49:52 +0000281/* The maximum exponent the hardware accepts is 63 (essentially it selects one
282 * of the 64bit timestamp bits to trigger reports from) but there's currently
283 * no known use case for sampling as infrequently as once per 47 thousand years.
284 *
285 * Since the timestamps included in OA reports are only 32bits it seems
286 * reasonable to limit the OA exponent where it's still possible to account for
287 * overflow in OA report timestamps.
288 */
289#define OA_EXPONENT_MAX 31
290
291#define INVALID_CTX_ID 0xffffffff
292
Robert Bragg19f81df2017-06-13 12:23:03 +0100293/* On Gen8+ automatically triggered OA reports include a 'reason' field... */
294#define OAREPORT_REASON_MASK 0x3f
295#define OAREPORT_REASON_SHIFT 19
296#define OAREPORT_REASON_TIMER (1<<0)
297#define OAREPORT_REASON_CTX_SWITCH (1<<3)
298#define OAREPORT_REASON_CLK_RATIO (1<<5)
299
Robert Braggd7965152016-11-07 19:49:52 +0000300
Robert Bragg00319ba2016-11-07 19:49:55 +0000301/* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
302 *
Robert Bragg155e9412017-06-13 12:23:05 +0100303 * The highest sampling frequency we can theoretically program the OA unit
304 * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
305 *
306 * Initialized just before we register the sysctl parameter.
Robert Bragg00319ba2016-11-07 19:49:55 +0000307 */
Robert Bragg155e9412017-06-13 12:23:05 +0100308static int oa_sample_rate_hard_limit;
Robert Bragg00319ba2016-11-07 19:49:55 +0000309
310/* Theoretically we can program the OA unit to sample every 160ns but don't
311 * allow that by default unless root...
312 *
313 * The default threshold of 100000Hz is based on perf's similar
314 * kernel.perf_event_max_sample_rate sysctl parameter.
315 */
316static u32 i915_oa_max_sample_rate = 100000;
317
Robert Braggd7965152016-11-07 19:49:52 +0000318/* XXX: beware if future OA HW adds new report formats that the current
319 * code assumes all reports have a power-of-two size and ~(size - 1) can
320 * be used as a mask to align the OA tail pointer.
321 */
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300322static const struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Braggd7965152016-11-07 19:49:52 +0000323 [I915_OA_FORMAT_A13] = { 0, 64 },
324 [I915_OA_FORMAT_A29] = { 1, 128 },
325 [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
326 /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
327 [I915_OA_FORMAT_B4_C8] = { 4, 64 },
328 [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
329 [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
330 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
331};
332
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300333static const struct i915_oa_format gen8_plus_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Bragg19f81df2017-06-13 12:23:03 +0100334 [I915_OA_FORMAT_A12] = { 0, 64 },
335 [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
336 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
337 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
338};
339
Robert Braggd7965152016-11-07 19:49:52 +0000340#define SAMPLE_OA_REPORT (1<<0)
Robert Braggeec688e2016-11-07 19:49:47 +0000341
Robert Bragg16d98b32016-12-07 21:40:33 +0000342/**
343 * struct perf_open_properties - for validated properties given to open a stream
344 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
345 * @single_context: Whether a single or all gpu contexts should be monitored
346 * @ctx_handle: A gem ctx handle for use with @single_context
347 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
348 * @oa_format: An OA unit HW report format
349 * @oa_periodic: Whether to enable periodic OA unit sampling
350 * @oa_period_exponent: The OA unit sampling period is derived from this
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100351 * @engine: The engine (typically rcs0) being monitored by the OA unit
Robert Bragg16d98b32016-12-07 21:40:33 +0000352 *
353 * As read_properties_unlocked() enumerates and validates the properties given
354 * to open a stream of metrics the configuration is built up in the structure
355 * which starts out zero initialized.
356 */
Robert Braggeec688e2016-11-07 19:49:47 +0000357struct perf_open_properties {
358 u32 sample_flags;
359
360 u64 single_context:1;
361 u64 ctx_handle;
Robert Braggd7965152016-11-07 19:49:52 +0000362
363 /* OA sampling state */
364 int metrics_set;
365 int oa_format;
366 bool oa_periodic;
367 int oa_period_exponent;
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100368
369 struct intel_engine_cs *engine;
Robert Braggeec688e2016-11-07 19:49:47 +0000370};
371
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100372struct i915_oa_config_bo {
373 struct llist_node node;
374
375 struct i915_oa_config *oa_config;
376 struct i915_vma *vma;
377};
378
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700379static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer);
380
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100381void i915_oa_config_release(struct kref *ref)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100382{
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100383 struct i915_oa_config *oa_config =
384 container_of(ref, typeof(*oa_config), ref);
385
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100386 if (!PTR_ERR(oa_config->flex_regs))
387 kfree(oa_config->flex_regs);
388 if (!PTR_ERR(oa_config->b_counter_regs))
389 kfree(oa_config->b_counter_regs);
390 if (!PTR_ERR(oa_config->mux_regs))
391 kfree(oa_config->mux_regs);
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100392
393 kfree_rcu(oa_config, rcu);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100394}
395
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100396struct i915_oa_config *
397i915_perf_get_oa_config(struct i915_perf *perf, int metrics_set)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100398{
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100399 struct i915_oa_config *oa_config;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100400
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100401 rcu_read_lock();
402 if (metrics_set == 1)
403 oa_config = &perf->test_config;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100404 else
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100405 oa_config = idr_find(&perf->metrics_idr, metrics_set);
406 if (oa_config)
407 oa_config = i915_oa_config_get(oa_config);
408 rcu_read_unlock();
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100409
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100410 return oa_config;
411}
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100412
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100413static void free_oa_config_bo(struct i915_oa_config_bo *oa_bo)
414{
415 i915_oa_config_put(oa_bo->oa_config);
416 i915_vma_put(oa_bo->vma);
417 kfree(oa_bo);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100418}
419
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700420static u32 gen8_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100421{
Chris Wilson52111c42019-10-10 16:05:20 +0100422 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700423
Chris Wilson8f8b1172019-10-07 22:09:41 +0100424 return intel_uncore_read(uncore, GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
Robert Bragg19f81df2017-06-13 12:23:03 +0100425}
426
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700427static u32 gen7_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100428{
Chris Wilson52111c42019-10-10 16:05:20 +0100429 struct intel_uncore *uncore = stream->uncore;
Chris Wilson8f8b1172019-10-07 22:09:41 +0100430 u32 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100431
432 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
433}
434
Robert Bragg0dd860c2017-05-11 16:43:28 +0100435/**
Robert Bragg19f81df2017-06-13 12:23:03 +0100436 * oa_buffer_check_unlocked - check for data and update tail ptr state
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700437 * @stream: i915 stream instance
Robert Braggd7965152016-11-07 19:49:52 +0000438 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100439 * This is either called via fops (for blocking reads in user ctx) or the poll
440 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
441 * if there is data available for userspace to read.
Robert Braggd7965152016-11-07 19:49:52 +0000442 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100443 * This function is central to providing a workaround for the OA unit tail
444 * pointer having a race with respect to what data is visible to the CPU.
445 * It is responsible for reading tail pointers from the hardware and giving
446 * the pointers time to 'age' before they are made available for reading.
447 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
448 *
449 * Besides returning true when there is data available to read() this function
450 * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp
451 * and .aged_tail_idx state used for reading.
452 *
453 * Note: It's safe to read OA config state here unlocked, assuming that this is
454 * only called while the stream is enabled, while the global OA configuration
455 * can't be modified.
456 *
457 * Returns: %true if the OA buffer contains data, else %false
Robert Braggd7965152016-11-07 19:49:52 +0000458 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700459static bool oa_buffer_check_unlocked(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +0000460{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700461 int report_size = stream->oa_buffer.format_size;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100462 unsigned long flags;
463 unsigned int aged_idx;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100464 u32 head, hw_tail, aged_tail, aging_tail;
465 u64 now;
Robert Braggd7965152016-11-07 19:49:52 +0000466
Robert Bragg0dd860c2017-05-11 16:43:28 +0100467 /* We have to consider the (unlikely) possibility that read() errors
468 * could result in an OA buffer reset which might reset the head,
469 * tails[] and aged_tail state.
470 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700471 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100472
473 /* NB: The head we observe here might effectively be a little out of
474 * date (between head and tails[aged_idx].offset if there is currently
475 * a read() in progress.
476 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700477 head = stream->oa_buffer.head;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100478
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700479 aged_idx = stream->oa_buffer.aged_tail_idx;
480 aged_tail = stream->oa_buffer.tails[aged_idx].offset;
481 aging_tail = stream->oa_buffer.tails[!aged_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100482
Chris Wilson8f8b1172019-10-07 22:09:41 +0100483 hw_tail = stream->perf->ops.oa_hw_tail_read(stream);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100484
485 /* The tail pointer increases in 64 byte increments,
486 * not in report_size steps...
487 */
488 hw_tail &= ~(report_size - 1);
489
490 now = ktime_get_mono_fast_ns();
491
Robert Bragg4117ebc2017-05-11 16:43:30 +0100492 /* Update the aged tail
493 *
494 * Flip the tail pointer available for read()s once the aging tail is
495 * old enough to trust that the corresponding data will be visible to
496 * the CPU...
497 *
498 * Do this before updating the aging pointer in case we may be able to
499 * immediately start aging a new pointer too (if new data has become
500 * available) without needing to wait for a later hrtimer callback.
501 */
502 if (aging_tail != INVALID_TAIL_PTR &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700503 ((now - stream->oa_buffer.aging_timestamp) >
Robert Bragg4117ebc2017-05-11 16:43:30 +0100504 OA_TAIL_MARGIN_NSEC)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100505
Robert Bragg4117ebc2017-05-11 16:43:30 +0100506 aged_idx ^= 1;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700507 stream->oa_buffer.aged_tail_idx = aged_idx;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100508
509 aged_tail = aging_tail;
510
511 /* Mark that we need a new pointer to start aging... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700512 stream->oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100513 aging_tail = INVALID_TAIL_PTR;
514 }
515
Robert Bragg0dd860c2017-05-11 16:43:28 +0100516 /* Update the aging tail
517 *
518 * We throttle aging tail updates until we have a new tail that
519 * represents >= one report more data than is already available for
520 * reading. This ensures there will be enough data for a successful
521 * read once this new pointer has aged and ensures we will give the new
522 * pointer time to age.
523 */
524 if (aging_tail == INVALID_TAIL_PTR &&
525 (aged_tail == INVALID_TAIL_PTR ||
526 OA_TAKEN(hw_tail, aged_tail) >= report_size)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700527 struct i915_vma *vma = stream->oa_buffer.vma;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100528 u32 gtt_offset = i915_ggtt_offset(vma);
529
530 /* Be paranoid and do a bounds check on the pointer read back
531 * from hardware, just in case some spurious hardware condition
532 * could put the tail out of bounds...
533 */
534 if (hw_tail >= gtt_offset &&
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200535 hw_tail < (gtt_offset + OA_BUFFER_SIZE)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700536 stream->oa_buffer.tails[!aged_idx].offset =
Robert Bragg0dd860c2017-05-11 16:43:28 +0100537 aging_tail = hw_tail;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700538 stream->oa_buffer.aging_timestamp = now;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100539 } else {
540 DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %u\n",
541 hw_tail);
542 }
543 }
544
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700545 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100546
547 return aged_tail == INVALID_TAIL_PTR ?
548 false : OA_TAKEN(aged_tail, head) >= report_size;
Robert Braggd7965152016-11-07 19:49:52 +0000549}
550
551/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000552 * append_oa_status - Appends a status record to a userspace read() buffer.
553 * @stream: An i915-perf stream opened for OA metrics
554 * @buf: destination buffer given by userspace
555 * @count: the number of bytes userspace wants to read
556 * @offset: (inout): the current position for writing into @buf
557 * @type: The kind of status to report to userspace
558 *
559 * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
560 * into the userspace read() buffer.
561 *
562 * The @buf @offset will only be updated on success.
563 *
564 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000565 */
566static int append_oa_status(struct i915_perf_stream *stream,
567 char __user *buf,
568 size_t count,
569 size_t *offset,
570 enum drm_i915_perf_record_type type)
571{
572 struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
573
574 if ((count - *offset) < header.size)
575 return -ENOSPC;
576
577 if (copy_to_user(buf + *offset, &header, sizeof(header)))
578 return -EFAULT;
579
580 (*offset) += header.size;
581
582 return 0;
583}
584
585/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000586 * append_oa_sample - Copies single OA report into userspace read() buffer.
587 * @stream: An i915-perf stream opened for OA metrics
588 * @buf: destination buffer given by userspace
589 * @count: the number of bytes userspace wants to read
590 * @offset: (inout): the current position for writing into @buf
591 * @report: A single OA report to (optionally) include as part of the sample
592 *
593 * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
594 * properties when opening a stream, tracked as `stream->sample_flags`. This
595 * function copies the requested components of a single sample to the given
596 * read() @buf.
597 *
598 * The @buf @offset will only be updated on success.
599 *
600 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000601 */
602static int append_oa_sample(struct i915_perf_stream *stream,
603 char __user *buf,
604 size_t count,
605 size_t *offset,
606 const u8 *report)
607{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700608 int report_size = stream->oa_buffer.format_size;
Robert Braggd7965152016-11-07 19:49:52 +0000609 struct drm_i915_perf_record_header header;
610 u32 sample_flags = stream->sample_flags;
611
612 header.type = DRM_I915_PERF_RECORD_SAMPLE;
613 header.pad = 0;
614 header.size = stream->sample_size;
615
616 if ((count - *offset) < header.size)
617 return -ENOSPC;
618
619 buf += *offset;
620 if (copy_to_user(buf, &header, sizeof(header)))
621 return -EFAULT;
622 buf += sizeof(header);
623
624 if (sample_flags & SAMPLE_OA_REPORT) {
625 if (copy_to_user(buf, report, report_size))
626 return -EFAULT;
627 }
628
629 (*offset) += header.size;
630
631 return 0;
632}
633
634/**
635 * Copies all buffered OA reports into userspace read() buffer.
636 * @stream: An i915-perf stream opened for OA metrics
637 * @buf: destination buffer given by userspace
638 * @count: the number of bytes userspace wants to read
639 * @offset: (inout): the current position for writing into @buf
Robert Braggd7965152016-11-07 19:49:52 +0000640 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000641 * Notably any error condition resulting in a short read (-%ENOSPC or
642 * -%EFAULT) will be returned even though one or more records may
Robert Braggd7965152016-11-07 19:49:52 +0000643 * have been successfully copied. In this case it's up to the caller
644 * to decide if the error should be squashed before returning to
645 * userspace.
646 *
647 * Note: reports are consumed from the head, and appended to the
Robert Bragge81b3a52017-05-11 16:43:24 +0100648 * tail, so the tail chases the head?... If you think that's mad
Robert Braggd7965152016-11-07 19:49:52 +0000649 * and back-to-front you're not alone, but this follows the
650 * Gen PRM naming convention.
Robert Bragg16d98b32016-12-07 21:40:33 +0000651 *
652 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000653 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100654static int gen8_append_oa_reports(struct i915_perf_stream *stream,
655 char __user *buf,
656 size_t count,
657 size_t *offset)
658{
Chris Wilson52111c42019-10-10 16:05:20 +0100659 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700660 int report_size = stream->oa_buffer.format_size;
661 u8 *oa_buf_base = stream->oa_buffer.vaddr;
662 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200663 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100664 size_t start_offset = *offset;
665 unsigned long flags;
666 unsigned int aged_tail_idx;
667 u32 head, tail;
668 u32 taken;
669 int ret = 0;
670
671 if (WARN_ON(!stream->enabled))
672 return -EIO;
673
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700674 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100675
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700676 head = stream->oa_buffer.head;
677 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
678 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg19f81df2017-06-13 12:23:03 +0100679
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700680 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100681
682 /*
683 * An invalid tail pointer here means we're still waiting for the poll
684 * hrtimer callback to give us a pointer
685 */
686 if (tail == INVALID_TAIL_PTR)
687 return -EAGAIN;
688
689 /*
690 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
691 * while indexing relative to oa_buf_base.
692 */
693 head -= gtt_offset;
694 tail -= gtt_offset;
695
696 /*
697 * An out of bounds or misaligned head or tail pointer implies a driver
698 * bug since we validate + align the tail pointers we read from the
699 * hardware and we are in full control of the head pointer which should
700 * only be incremented by multiples of the report size (notably also
701 * all a power of two).
702 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200703 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
704 tail > OA_BUFFER_SIZE || tail % report_size,
Robert Bragg19f81df2017-06-13 12:23:03 +0100705 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
706 head, tail))
707 return -EIO;
708
709
710 for (/* none */;
711 (taken = OA_TAKEN(tail, head));
712 head = (head + report_size) & mask) {
713 u8 *report = oa_buf_base + head;
714 u32 *report32 = (void *)report;
715 u32 ctx_id;
716 u32 reason;
717
718 /*
719 * All the report sizes factor neatly into the buffer
720 * size so we never expect to see a report split
721 * between the beginning and end of the buffer.
722 *
723 * Given the initial alignment check a misalignment
724 * here would imply a driver bug that would result
725 * in an overrun.
726 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200727 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100728 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
729 break;
730 }
731
732 /*
733 * The reason field includes flags identifying what
734 * triggered this specific report (mostly timer
735 * triggered or e.g. due to a context switch).
736 *
737 * This field is never expected to be zero so we can
738 * check that the report isn't invalid before copying
739 * it to userspace...
740 */
741 reason = ((report32[0] >> OAREPORT_REASON_SHIFT) &
742 OAREPORT_REASON_MASK);
743 if (reason == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +0100744 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg19f81df2017-06-13 12:23:03 +0100745 DRM_NOTE("Skipping spurious, invalid OA report\n");
746 continue;
747 }
748
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700749 ctx_id = report32[2] & stream->specific_ctx_id_mask;
Robert Bragg19f81df2017-06-13 12:23:03 +0100750
751 /*
752 * Squash whatever is in the CTX_ID field if it's marked as
753 * invalid to be sure we avoid false-positive, single-context
754 * filtering below...
755 *
756 * Note: that we don't clear the valid_ctx_bit so userspace can
757 * understand that the ID has been squashed by the kernel.
758 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100759 if (!(report32[0] & stream->perf->gen8_valid_ctx_bit))
Robert Bragg19f81df2017-06-13 12:23:03 +0100760 ctx_id = report32[2] = INVALID_CTX_ID;
761
762 /*
763 * NB: For Gen 8 the OA unit no longer supports clock gating
764 * off for a specific context and the kernel can't securely
765 * stop the counters from updating as system-wide / global
766 * values.
767 *
768 * Automatic reports now include a context ID so reports can be
769 * filtered on the cpu but it's not worth trying to
770 * automatically subtract/hide counter progress for other
771 * contexts while filtering since we can't stop userspace
772 * issuing MI_REPORT_PERF_COUNT commands which would still
773 * provide a side-band view of the real values.
774 *
775 * To allow userspace (such as Mesa/GL_INTEL_performance_query)
776 * to normalize counters for a single filtered context then it
777 * needs be forwarded bookend context-switch reports so that it
778 * can track switches in between MI_REPORT_PERF_COUNT commands
779 * and can itself subtract/ignore the progress of counters
780 * associated with other contexts. Note that the hardware
781 * automatically triggers reports when switching to a new
782 * context which are tagged with the ID of the newly active
783 * context. To avoid the complexity (and likely fragility) of
784 * reading ahead while parsing reports to try and minimize
785 * forwarding redundant context switch reports (i.e. between
786 * other, unrelated contexts) we simply elect to forward them
787 * all.
788 *
789 * We don't rely solely on the reason field to identify context
790 * switches since it's not-uncommon for periodic samples to
791 * identify a switch before any 'context switch' report.
792 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100793 if (!stream->perf->exclusive_stream->ctx ||
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700794 stream->specific_ctx_id == ctx_id ||
795 stream->oa_buffer.last_ctx_id == stream->specific_ctx_id ||
Robert Bragg19f81df2017-06-13 12:23:03 +0100796 reason & OAREPORT_REASON_CTX_SWITCH) {
797
798 /*
799 * While filtering for a single context we avoid
800 * leaking the IDs of other contexts.
801 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100802 if (stream->perf->exclusive_stream->ctx &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700803 stream->specific_ctx_id != ctx_id) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100804 report32[2] = INVALID_CTX_ID;
805 }
806
807 ret = append_oa_sample(stream, buf, count, offset,
808 report);
809 if (ret)
810 break;
811
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700812 stream->oa_buffer.last_ctx_id = ctx_id;
Robert Bragg19f81df2017-06-13 12:23:03 +0100813 }
814
815 /*
816 * The above reason field sanity check is based on
817 * the assumption that the OA buffer is initially
818 * zeroed and we reset the field after copying so the
819 * check is still meaningful once old reports start
820 * being overwritten.
821 */
822 report32[0] = 0;
823 }
824
825 if (start_offset != *offset) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700826 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100827
828 /*
829 * We removed the gtt_offset for the copy loop above, indexing
830 * relative to oa_buf_base so put back here...
831 */
832 head += gtt_offset;
833
Chris Wilson8f8b1172019-10-07 22:09:41 +0100834 intel_uncore_write(uncore, GEN8_OAHEADPTR,
835 head & GEN8_OAHEADPTR_MASK);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700836 stream->oa_buffer.head = head;
Robert Bragg19f81df2017-06-13 12:23:03 +0100837
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700838 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100839 }
840
841 return ret;
842}
843
844/**
845 * gen8_oa_read - copy status records then buffered OA reports
846 * @stream: An i915-perf stream opened for OA metrics
847 * @buf: destination buffer given by userspace
848 * @count: the number of bytes userspace wants to read
849 * @offset: (inout): the current position for writing into @buf
850 *
851 * Checks OA unit status registers and if necessary appends corresponding
852 * status records for userspace (such as for a buffer full condition) and then
853 * initiate appending any buffered OA reports.
854 *
855 * Updates @offset according to the number of bytes successfully copied into
856 * the userspace buffer.
857 *
858 * NB: some data may be successfully copied to the userspace buffer
859 * even if an error is returned, and this is reflected in the
860 * updated @offset.
861 *
862 * Returns: zero on success or a negative error code
863 */
864static int gen8_oa_read(struct i915_perf_stream *stream,
865 char __user *buf,
866 size_t count,
867 size_t *offset)
868{
Chris Wilson52111c42019-10-10 16:05:20 +0100869 struct intel_uncore *uncore = stream->uncore;
Robert Bragg19f81df2017-06-13 12:23:03 +0100870 u32 oastatus;
871 int ret;
872
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700873 if (WARN_ON(!stream->oa_buffer.vaddr))
Robert Bragg19f81df2017-06-13 12:23:03 +0100874 return -EIO;
875
Chris Wilson8f8b1172019-10-07 22:09:41 +0100876 oastatus = intel_uncore_read(uncore, GEN8_OASTATUS);
Robert Bragg19f81df2017-06-13 12:23:03 +0100877
878 /*
879 * We treat OABUFFER_OVERFLOW as a significant error:
880 *
881 * Although theoretically we could handle this more gracefully
882 * sometimes, some Gens don't correctly suppress certain
883 * automatically triggered reports in this condition and so we
884 * have to assume that old reports are now being trampled
885 * over.
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200886 *
887 * Considering how we don't currently give userspace control
888 * over the OA buffer size and always configure a large 16MB
889 * buffer, then a buffer overflow does anyway likely indicate
890 * that something has gone quite badly wrong.
Robert Bragg19f81df2017-06-13 12:23:03 +0100891 */
892 if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
893 ret = append_oa_status(stream, buf, count, offset,
894 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
895 if (ret)
896 return ret;
897
898 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700899 stream->period_exponent);
Robert Bragg19f81df2017-06-13 12:23:03 +0100900
Chris Wilson8f8b1172019-10-07 22:09:41 +0100901 stream->perf->ops.oa_disable(stream);
902 stream->perf->ops.oa_enable(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +0100903
904 /*
905 * Note: .oa_enable() is expected to re-init the oabuffer and
906 * reset GEN8_OASTATUS for us
907 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100908 oastatus = intel_uncore_read(uncore, GEN8_OASTATUS);
Robert Bragg19f81df2017-06-13 12:23:03 +0100909 }
910
911 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
912 ret = append_oa_status(stream, buf, count, offset,
913 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
914 if (ret)
915 return ret;
Chris Wilson8f8b1172019-10-07 22:09:41 +0100916 intel_uncore_write(uncore, GEN8_OASTATUS,
917 oastatus & ~GEN8_OASTATUS_REPORT_LOST);
Robert Bragg19f81df2017-06-13 12:23:03 +0100918 }
919
920 return gen8_append_oa_reports(stream, buf, count, offset);
921}
922
923/**
924 * Copies all buffered OA reports into userspace read() buffer.
925 * @stream: An i915-perf stream opened for OA metrics
926 * @buf: destination buffer given by userspace
927 * @count: the number of bytes userspace wants to read
928 * @offset: (inout): the current position for writing into @buf
929 *
930 * Notably any error condition resulting in a short read (-%ENOSPC or
931 * -%EFAULT) will be returned even though one or more records may
932 * have been successfully copied. In this case it's up to the caller
933 * to decide if the error should be squashed before returning to
934 * userspace.
935 *
936 * Note: reports are consumed from the head, and appended to the
937 * tail, so the tail chases the head?... If you think that's mad
938 * and back-to-front you're not alone, but this follows the
939 * Gen PRM naming convention.
940 *
941 * Returns: 0 on success, negative error code on failure.
942 */
Robert Braggd7965152016-11-07 19:49:52 +0000943static int gen7_append_oa_reports(struct i915_perf_stream *stream,
944 char __user *buf,
945 size_t count,
Robert Bragg3bb335c2017-05-11 16:43:27 +0100946 size_t *offset)
Robert Braggd7965152016-11-07 19:49:52 +0000947{
Chris Wilson52111c42019-10-10 16:05:20 +0100948 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700949 int report_size = stream->oa_buffer.format_size;
950 u8 *oa_buf_base = stream->oa_buffer.vaddr;
951 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200952 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100953 size_t start_offset = *offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100954 unsigned long flags;
955 unsigned int aged_tail_idx;
956 u32 head, tail;
Robert Braggd7965152016-11-07 19:49:52 +0000957 u32 taken;
958 int ret = 0;
959
960 if (WARN_ON(!stream->enabled))
961 return -EIO;
962
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700963 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggf2790202017-05-11 16:43:26 +0100964
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700965 head = stream->oa_buffer.head;
966 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
967 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100968
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700969 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100970
971 /* An invalid tail pointer here means we're still waiting for the poll
972 * hrtimer callback to give us a pointer
Robert Braggf2790202017-05-11 16:43:26 +0100973 */
Robert Bragg0dd860c2017-05-11 16:43:28 +0100974 if (tail == INVALID_TAIL_PTR)
Robert Braggd7965152016-11-07 19:49:52 +0000975 return -EAGAIN;
976
Robert Bragg0dd860c2017-05-11 16:43:28 +0100977 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
978 * while indexing relative to oa_buf_base.
979 */
980 head -= gtt_offset;
981 tail -= gtt_offset;
982
983 /* An out of bounds or misaligned head or tail pointer implies a driver
984 * bug since we validate + align the tail pointers we read from the
985 * hardware and we are in full control of the head pointer which should
986 * only be incremented by multiples of the report size (notably also
987 * all a power of two).
988 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200989 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
990 tail > OA_BUFFER_SIZE || tail % report_size,
Robert Bragg0dd860c2017-05-11 16:43:28 +0100991 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
992 head, tail))
993 return -EIO;
994
Robert Braggd7965152016-11-07 19:49:52 +0000995
996 for (/* none */;
997 (taken = OA_TAKEN(tail, head));
998 head = (head + report_size) & mask) {
999 u8 *report = oa_buf_base + head;
1000 u32 *report32 = (void *)report;
1001
1002 /* All the report sizes factor neatly into the buffer
1003 * size so we never expect to see a report split
1004 * between the beginning and end of the buffer.
1005 *
1006 * Given the initial alignment check a misalignment
1007 * here would imply a driver bug that would result
1008 * in an overrun.
1009 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001010 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
Robert Braggd7965152016-11-07 19:49:52 +00001011 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
1012 break;
1013 }
1014
1015 /* The report-ID field for periodic samples includes
1016 * some undocumented flags related to what triggered
1017 * the report and is never expected to be zero so we
1018 * can check that the report isn't invalid before
1019 * copying it to userspace...
1020 */
1021 if (report32[0] == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01001022 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg712122e2017-05-11 16:43:31 +01001023 DRM_NOTE("Skipping spurious, invalid OA report\n");
Robert Braggd7965152016-11-07 19:49:52 +00001024 continue;
1025 }
1026
1027 ret = append_oa_sample(stream, buf, count, offset, report);
1028 if (ret)
1029 break;
1030
1031 /* The above report-id field sanity check is based on
1032 * the assumption that the OA buffer is initially
1033 * zeroed and we reset the field after copying so the
1034 * check is still meaningful once old reports start
1035 * being overwritten.
1036 */
1037 report32[0] = 0;
1038 }
1039
Robert Bragg3bb335c2017-05-11 16:43:27 +01001040 if (start_offset != *offset) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001041 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001042
Robert Bragg3bb335c2017-05-11 16:43:27 +01001043 /* We removed the gtt_offset for the copy loop above, indexing
1044 * relative to oa_buf_base so put back here...
1045 */
1046 head += gtt_offset;
1047
Chris Wilson8f8b1172019-10-07 22:09:41 +01001048 intel_uncore_write(uncore, GEN7_OASTATUS2,
1049 (head & GEN7_OASTATUS2_HEAD_MASK) |
1050 GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001051 stream->oa_buffer.head = head;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001052
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001053 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg3bb335c2017-05-11 16:43:27 +01001054 }
Robert Braggd7965152016-11-07 19:49:52 +00001055
1056 return ret;
1057}
1058
Robert Bragg16d98b32016-12-07 21:40:33 +00001059/**
1060 * gen7_oa_read - copy status records then buffered OA reports
1061 * @stream: An i915-perf stream opened for OA metrics
1062 * @buf: destination buffer given by userspace
1063 * @count: the number of bytes userspace wants to read
1064 * @offset: (inout): the current position for writing into @buf
1065 *
1066 * Checks Gen 7 specific OA unit status registers and if necessary appends
1067 * corresponding status records for userspace (such as for a buffer full
1068 * condition) and then initiate appending any buffered OA reports.
1069 *
1070 * Updates @offset according to the number of bytes successfully copied into
1071 * the userspace buffer.
1072 *
1073 * Returns: zero on success or a negative error code
1074 */
Robert Braggd7965152016-11-07 19:49:52 +00001075static int gen7_oa_read(struct i915_perf_stream *stream,
1076 char __user *buf,
1077 size_t count,
1078 size_t *offset)
1079{
Chris Wilson52111c42019-10-10 16:05:20 +01001080 struct intel_uncore *uncore = stream->uncore;
Robert Braggd7965152016-11-07 19:49:52 +00001081 u32 oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001082 int ret;
1083
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001084 if (WARN_ON(!stream->oa_buffer.vaddr))
Robert Braggd7965152016-11-07 19:49:52 +00001085 return -EIO;
1086
Chris Wilson8f8b1172019-10-07 22:09:41 +01001087 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001088
Robert Braggd7965152016-11-07 19:49:52 +00001089 /* XXX: On Haswell we don't have a safe way to clear oastatus1
1090 * bits while the OA unit is enabled (while the tail pointer
1091 * may be updated asynchronously) so we ignore status bits
1092 * that have already been reported to userspace.
1093 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001094 oastatus1 &= ~stream->perf->gen7_latched_oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001095
1096 /* We treat OABUFFER_OVERFLOW as a significant error:
1097 *
1098 * - The status can be interpreted to mean that the buffer is
1099 * currently full (with a higher precedence than OA_TAKEN()
1100 * which will start to report a near-empty buffer after an
1101 * overflow) but it's awkward that we can't clear the status
1102 * on Haswell, so without a reset we won't be able to catch
1103 * the state again.
1104 *
1105 * - Since it also implies the HW has started overwriting old
1106 * reports it may also affect our sanity checks for invalid
1107 * reports when copying to userspace that assume new reports
1108 * are being written to cleared memory.
1109 *
1110 * - In the future we may want to introduce a flight recorder
1111 * mode where the driver will automatically maintain a safe
1112 * guard band between head/tail, avoiding this overflow
1113 * condition, but we avoid the added driver complexity for
1114 * now.
1115 */
1116 if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
1117 ret = append_oa_status(stream, buf, count, offset,
1118 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
1119 if (ret)
1120 return ret;
1121
Robert Bragg19f81df2017-06-13 12:23:03 +01001122 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001123 stream->period_exponent);
Robert Braggd7965152016-11-07 19:49:52 +00001124
Chris Wilson8f8b1172019-10-07 22:09:41 +01001125 stream->perf->ops.oa_disable(stream);
1126 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001127
Chris Wilson8f8b1172019-10-07 22:09:41 +01001128 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001129 }
1130
1131 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
1132 ret = append_oa_status(stream, buf, count, offset,
1133 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1134 if (ret)
1135 return ret;
Chris Wilson8f8b1172019-10-07 22:09:41 +01001136 stream->perf->gen7_latched_oastatus1 |=
Robert Braggd7965152016-11-07 19:49:52 +00001137 GEN7_OASTATUS1_REPORT_LOST;
1138 }
1139
Robert Bragg3bb335c2017-05-11 16:43:27 +01001140 return gen7_append_oa_reports(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001141}
1142
Robert Bragg16d98b32016-12-07 21:40:33 +00001143/**
1144 * i915_oa_wait_unlocked - handles blocking IO until OA data available
1145 * @stream: An i915-perf stream opened for OA metrics
1146 *
1147 * Called when userspace tries to read() from a blocking stream FD opened
1148 * for OA metrics. It waits until the hrtimer callback finds a non-empty
1149 * OA buffer and wakes us.
1150 *
1151 * Note: it's acceptable to have this return with some false positives
1152 * since any subsequent read handling will return -EAGAIN if there isn't
1153 * really data ready for userspace yet.
1154 *
1155 * Returns: zero on success or a negative error code
1156 */
Robert Braggd7965152016-11-07 19:49:52 +00001157static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
1158{
Robert Braggd7965152016-11-07 19:49:52 +00001159 /* We would wait indefinitely if periodic sampling is not enabled */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001160 if (!stream->periodic)
Robert Braggd7965152016-11-07 19:49:52 +00001161 return -EIO;
1162
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001163 return wait_event_interruptible(stream->poll_wq,
1164 oa_buffer_check_unlocked(stream));
Robert Braggd7965152016-11-07 19:49:52 +00001165}
1166
Robert Bragg16d98b32016-12-07 21:40:33 +00001167/**
1168 * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
1169 * @stream: An i915-perf stream opened for OA metrics
1170 * @file: An i915 perf stream file
1171 * @wait: poll() state table
1172 *
1173 * For handling userspace polling on an i915 perf stream opened for OA metrics,
1174 * this starts a poll_wait with the wait queue that our hrtimer callback wakes
1175 * when it sees data ready to read in the circular OA buffer.
1176 */
Robert Braggd7965152016-11-07 19:49:52 +00001177static void i915_oa_poll_wait(struct i915_perf_stream *stream,
1178 struct file *file,
1179 poll_table *wait)
1180{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001181 poll_wait(file, &stream->poll_wq, wait);
Robert Braggd7965152016-11-07 19:49:52 +00001182}
1183
Robert Bragg16d98b32016-12-07 21:40:33 +00001184/**
1185 * i915_oa_read - just calls through to &i915_oa_ops->read
1186 * @stream: An i915-perf stream opened for OA metrics
1187 * @buf: destination buffer given by userspace
1188 * @count: the number of bytes userspace wants to read
1189 * @offset: (inout): the current position for writing into @buf
1190 *
1191 * Updates @offset according to the number of bytes successfully copied into
1192 * the userspace buffer.
1193 *
1194 * Returns: zero on success or a negative error code
1195 */
Robert Braggd7965152016-11-07 19:49:52 +00001196static int i915_oa_read(struct i915_perf_stream *stream,
1197 char __user *buf,
1198 size_t count,
1199 size_t *offset)
1200{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001201 return stream->perf->ops.read(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001202}
1203
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001204static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001205{
Chris Wilson5e2a0412019-04-26 17:33:34 +01001206 struct i915_gem_engines_iter it;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001207 struct i915_gem_context *ctx = stream->ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001208 struct intel_context *ce;
Chris Wilsonfa9f6682019-04-26 17:33:29 +01001209 int err;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001210
Chris Wilson5e2a0412019-04-26 17:33:34 +01001211 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
Lionel Landwerlin9a613632019-10-10 16:05:19 +01001212 if (ce->engine != stream->engine) /* first match! */
Chris Wilson5e2a0412019-04-26 17:33:34 +01001213 continue;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001214
Chris Wilson5e2a0412019-04-26 17:33:34 +01001215 /*
1216 * As the ID is the gtt offset of the context's vma we
1217 * pin the vma to ensure the ID remains fixed.
1218 */
1219 err = intel_context_pin(ce);
1220 if (err == 0) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001221 stream->pinned_ctx = ce;
Chris Wilson5e2a0412019-04-26 17:33:34 +01001222 break;
1223 }
1224 }
1225 i915_gem_context_unlock_engines(ctx);
1226
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001227 return stream->pinned_ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001228}
1229
Robert Bragg16d98b32016-12-07 21:40:33 +00001230/**
1231 * oa_get_render_ctx_id - determine and hold ctx hw id
1232 * @stream: An i915-perf stream opened for OA metrics
1233 *
1234 * Determine the render context hw id, and ensure it remains fixed for the
Robert Braggd7965152016-11-07 19:49:52 +00001235 * lifetime of the stream. This ensures that we don't have to worry about
1236 * updating the context ID in OACONTROL on the fly.
Robert Bragg16d98b32016-12-07 21:40:33 +00001237 *
1238 * Returns: zero on success or a negative error code
Robert Braggd7965152016-11-07 19:49:52 +00001239 */
1240static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
1241{
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001242 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001243
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001244 ce = oa_pin_context(stream);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001245 if (IS_ERR(ce))
1246 return PTR_ERR(ce);
Robert Braggd7965152016-11-07 19:49:52 +00001247
Chris Wilson8f8b1172019-10-07 22:09:41 +01001248 switch (INTEL_GEN(ce->engine->i915)) {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001249 case 7: {
Robert Bragg19f81df2017-06-13 12:23:03 +01001250 /*
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001251 * On Haswell we don't do any post processing of the reports
1252 * and don't need to use the mask.
Robert Bragg19f81df2017-06-13 12:23:03 +01001253 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001254 stream->specific_ctx_id = i915_ggtt_offset(ce->state);
1255 stream->specific_ctx_id_mask = 0;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001256 break;
Robert Bragg19f81df2017-06-13 12:23:03 +01001257 }
Robert Braggd7965152016-11-07 19:49:52 +00001258
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001259 case 8:
1260 case 9:
1261 case 10:
Chris Wilson8f8b1172019-10-07 22:09:41 +01001262 if (USES_GUC_SUBMISSION(ce->engine->i915)) {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001263 /*
1264 * When using GuC, the context descriptor we write in
1265 * i915 is read by GuC and rewritten before it's
1266 * actually written into the hardware. The LRCA is
1267 * what is put into the context id field of the
1268 * context descriptor by GuC. Because it's aligned to
1269 * a page, the lower 12bits are always at 0 and
1270 * dropped by GuC. They won't be part of the context
1271 * ID in the OA reports, so squash those lower bits.
1272 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001273 stream->specific_ctx_id =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001274 lower_32_bits(ce->lrc_desc) >> 12;
1275
1276 /*
1277 * GuC uses the top bit to signal proxy submission, so
1278 * ignore that bit.
1279 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001280 stream->specific_ctx_id_mask =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001281 (1U << (GEN8_CTX_ID_WIDTH - 1)) - 1;
1282 } else {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001283 stream->specific_ctx_id_mask =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001284 (1U << GEN8_CTX_ID_WIDTH) - 1;
Chris Wilson2935ed52019-10-04 14:40:08 +01001285 stream->specific_ctx_id = stream->specific_ctx_id_mask;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001286 }
1287 break;
1288
Michel Thierry45e9c822019-08-23 01:20:50 -07001289 case 11:
1290 case 12: {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001291 stream->specific_ctx_id_mask =
Chris Wilson2935ed52019-10-04 14:40:08 +01001292 ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) << (GEN11_SW_CTX_ID_SHIFT - 32);
1293 stream->specific_ctx_id = stream->specific_ctx_id_mask;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001294 break;
1295 }
1296
1297 default:
Chris Wilson8f8b1172019-10-07 22:09:41 +01001298 MISSING_CASE(INTEL_GEN(ce->engine->i915));
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001299 }
1300
Chris Wilson2935ed52019-10-04 14:40:08 +01001301 ce->tag = stream->specific_ctx_id_mask;
1302
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001303 DRM_DEBUG_DRIVER("filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001304 stream->specific_ctx_id,
1305 stream->specific_ctx_id_mask);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001306
Chris Wilson266a2402017-05-04 10:33:08 +01001307 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001308}
1309
Robert Bragg16d98b32016-12-07 21:40:33 +00001310/**
1311 * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
1312 * @stream: An i915-perf stream opened for OA metrics
1313 *
1314 * In case anything needed doing to ensure the context HW ID would remain valid
1315 * for the lifetime of the stream, then that can be undone here.
1316 */
Robert Braggd7965152016-11-07 19:49:52 +00001317static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
1318{
Chris Wilson1fc44d92018-05-17 22:26:32 +01001319 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001320
Chris Wilson2935ed52019-10-04 14:40:08 +01001321 ce = fetch_and_zero(&stream->pinned_ctx);
1322 if (ce) {
1323 ce->tag = 0; /* recomputed on next submission after parking */
1324 intel_context_unpin(ce);
1325 }
1326
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001327 stream->specific_ctx_id = INVALID_CTX_ID;
1328 stream->specific_ctx_id_mask = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001329}
1330
1331static void
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001332free_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001333{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001334 i915_vma_unpin_and_release(&stream->oa_buffer.vma,
Chris Wilson6a2f59e2018-07-21 13:50:37 +01001335 I915_VMA_RELEASE_MAP);
Robert Braggd7965152016-11-07 19:49:52 +00001336
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001337 stream->oa_buffer.vaddr = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001338}
1339
Lionel Landwerlin6a450082019-10-12 08:23:06 +01001340static void
1341free_oa_configs(struct i915_perf_stream *stream)
1342{
1343 struct i915_oa_config_bo *oa_bo, *tmp;
1344
1345 i915_oa_config_put(stream->oa_config);
1346 llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node)
1347 free_oa_config_bo(oa_bo);
1348}
1349
Robert Braggd7965152016-11-07 19:49:52 +00001350static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
1351{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001352 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00001353
Chris Wilson8f8b1172019-10-07 22:09:41 +01001354 BUG_ON(stream != perf->exclusive_stream);
Robert Braggd7965152016-11-07 19:49:52 +00001355
Robert Bragg19f81df2017-06-13 12:23:03 +01001356 /*
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001357 * Unset exclusive_stream first, it will be checked while disabling
1358 * the metric set on gen8+.
Robert Bragg19f81df2017-06-13 12:23:03 +01001359 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001360 perf->exclusive_stream = NULL;
1361 perf->ops.disable_metric_set(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001362
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001363 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001364
Chris Wilson52111c42019-10-10 16:05:20 +01001365 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
Chris Wilsona5efcde2019-10-11 20:03:17 +01001366 intel_engine_pm_put(stream->engine);
Robert Braggd7965152016-11-07 19:49:52 +00001367
1368 if (stream->ctx)
1369 oa_put_render_ctx_id(stream);
1370
Lionel Landwerlin6a450082019-10-12 08:23:06 +01001371 free_oa_configs(stream);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001372
Chris Wilson8f8b1172019-10-07 22:09:41 +01001373 if (perf->spurious_report_rs.missed) {
Robert Bragg712122e2017-05-11 16:43:31 +01001374 DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n",
Chris Wilson8f8b1172019-10-07 22:09:41 +01001375 perf->spurious_report_rs.missed);
Robert Bragg712122e2017-05-11 16:43:31 +01001376 }
Robert Braggd7965152016-11-07 19:49:52 +00001377}
1378
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001379static void gen7_init_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001380{
Chris Wilson52111c42019-10-10 16:05:20 +01001381 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001382 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001383 unsigned long flags;
1384
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001385 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggd7965152016-11-07 19:49:52 +00001386
1387 /* Pre-DevBDW: OABUFFER must be set with counters off,
1388 * before OASTATUS1, but after OASTATUS2
1389 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001390 intel_uncore_write(uncore, GEN7_OASTATUS2, /* head */
1391 gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001392 stream->oa_buffer.head = gtt_offset;
Robert Braggf2790202017-05-11 16:43:26 +01001393
Chris Wilson8f8b1172019-10-07 22:09:41 +01001394 intel_uncore_write(uncore, GEN7_OABUFFER, gtt_offset);
Robert Braggf2790202017-05-11 16:43:26 +01001395
Chris Wilson8f8b1172019-10-07 22:09:41 +01001396 intel_uncore_write(uncore, GEN7_OASTATUS1, /* tail */
1397 gtt_offset | OABUFFER_SIZE_16M);
Robert Braggd7965152016-11-07 19:49:52 +00001398
Robert Bragg0dd860c2017-05-11 16:43:28 +01001399 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001400 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1401 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001402
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001403 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001404
Robert Braggd7965152016-11-07 19:49:52 +00001405 /* On Haswell we have to track which OASTATUS1 flags we've
1406 * already seen since they can't be cleared while periodic
1407 * sampling is enabled.
1408 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001409 stream->perf->gen7_latched_oastatus1 = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001410
1411 /* NB: although the OA buffer will initially be allocated
1412 * zeroed via shmfs (and so this memset is redundant when
1413 * first allocating), we may re-init the OA buffer, either
1414 * when re-enabling a stream or in error/reset paths.
1415 *
1416 * The reason we clear the buffer for each re-init is for the
1417 * sanity check in gen7_append_oa_reports() that looks at the
1418 * report-id field to make sure it's non-zero which relies on
1419 * the assumption that new reports are being written to zeroed
1420 * memory...
1421 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001422 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001423
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001424 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00001425}
1426
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001427static void gen8_init_oa_buffer(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001428{
Chris Wilson52111c42019-10-10 16:05:20 +01001429 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001430 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg19f81df2017-06-13 12:23:03 +01001431 unsigned long flags;
1432
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001433 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001434
Chris Wilson8f8b1172019-10-07 22:09:41 +01001435 intel_uncore_write(uncore, GEN8_OASTATUS, 0);
1436 intel_uncore_write(uncore, GEN8_OAHEADPTR, gtt_offset);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001437 stream->oa_buffer.head = gtt_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01001438
Chris Wilson8f8b1172019-10-07 22:09:41 +01001439 intel_uncore_write(uncore, GEN8_OABUFFER_UDW, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01001440
1441 /*
1442 * PRM says:
1443 *
1444 * "This MMIO must be set before the OATAILPTR
1445 * register and after the OAHEADPTR register. This is
1446 * to enable proper functionality of the overflow
1447 * bit."
1448 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001449 intel_uncore_write(uncore, GEN8_OABUFFER, gtt_offset |
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001450 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
Chris Wilson8f8b1172019-10-07 22:09:41 +01001451 intel_uncore_write(uncore, GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
Robert Bragg19f81df2017-06-13 12:23:03 +01001452
1453 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001454 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1455 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg19f81df2017-06-13 12:23:03 +01001456
1457 /*
1458 * Reset state used to recognise context switches, affecting which
1459 * reports we will forward to userspace while filtering for a single
1460 * context.
1461 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001462 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
Robert Bragg19f81df2017-06-13 12:23:03 +01001463
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001464 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001465
1466 /*
1467 * NB: although the OA buffer will initially be allocated
1468 * zeroed via shmfs (and so this memset is redundant when
1469 * first allocating), we may re-init the OA buffer, either
1470 * when re-enabling a stream or in error/reset paths.
1471 *
1472 * The reason we clear the buffer for each re-init is for the
1473 * sanity check in gen8_append_oa_reports() that looks at the
1474 * reason field to make sure it's non-zero which relies on
1475 * the assumption that new reports are being written to zeroed
1476 * memory...
1477 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001478 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Bragg19f81df2017-06-13 12:23:03 +01001479
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001480 stream->pollin = false;
Robert Bragg19f81df2017-06-13 12:23:03 +01001481}
1482
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001483static int alloc_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001484{
1485 struct drm_i915_gem_object *bo;
1486 struct i915_vma *vma;
1487 int ret;
1488
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001489 if (WARN_ON(stream->oa_buffer.vma))
Robert Braggd7965152016-11-07 19:49:52 +00001490 return -ENODEV;
1491
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001492 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
1493 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
1494
Chris Wilson8f8b1172019-10-07 22:09:41 +01001495 bo = i915_gem_object_create_shmem(stream->perf->i915, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001496 if (IS_ERR(bo)) {
1497 DRM_ERROR("Failed to allocate OA buffer\n");
Chris Wilson28507482019-10-04 14:39:58 +01001498 return PTR_ERR(bo);
Robert Braggd7965152016-11-07 19:49:52 +00001499 }
1500
Chris Wilsona679f582019-03-21 16:19:07 +00001501 i915_gem_object_set_cache_coherency(bo, I915_CACHE_LLC);
Robert Braggd7965152016-11-07 19:49:52 +00001502
1503 /* PreHSW required 512K alignment, HSW requires 16M */
1504 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0);
1505 if (IS_ERR(vma)) {
1506 ret = PTR_ERR(vma);
1507 goto err_unref;
1508 }
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001509 stream->oa_buffer.vma = vma;
Robert Braggd7965152016-11-07 19:49:52 +00001510
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001511 stream->oa_buffer.vaddr =
Robert Braggd7965152016-11-07 19:49:52 +00001512 i915_gem_object_pin_map(bo, I915_MAP_WB);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001513 if (IS_ERR(stream->oa_buffer.vaddr)) {
1514 ret = PTR_ERR(stream->oa_buffer.vaddr);
Robert Braggd7965152016-11-07 19:49:52 +00001515 goto err_unpin;
1516 }
1517
Chris Wilson28507482019-10-04 14:39:58 +01001518 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001519
1520err_unpin:
1521 __i915_vma_unpin(vma);
1522
1523err_unref:
1524 i915_gem_object_put(bo);
1525
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001526 stream->oa_buffer.vaddr = NULL;
1527 stream->oa_buffer.vma = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001528
Robert Braggd7965152016-11-07 19:49:52 +00001529 return ret;
1530}
1531
Chris Wilson8f8b1172019-10-07 22:09:41 +01001532static void config_oa_regs(struct intel_uncore *uncore,
Robert Braggd7965152016-11-07 19:49:52 +00001533 const struct i915_oa_reg *regs,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001534 u32 n_regs)
Robert Braggd7965152016-11-07 19:49:52 +00001535{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001536 u32 i;
Robert Braggd7965152016-11-07 19:49:52 +00001537
1538 for (i = 0; i < n_regs; i++) {
1539 const struct i915_oa_reg *reg = regs + i;
1540
Chris Wilson8f8b1172019-10-07 22:09:41 +01001541 intel_uncore_write(uncore, reg->addr, reg->value);
Robert Braggd7965152016-11-07 19:49:52 +00001542 }
1543}
1544
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001545static void delay_after_mux(void)
Robert Braggd7965152016-11-07 19:49:52 +00001546{
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001547 /*
1548 * It apparently takes a fairly long time for a new MUX
Robert Braggd7965152016-11-07 19:49:52 +00001549 * configuration to be be applied after these register writes.
1550 * This delay duration was derived empirically based on the
1551 * render_basic config but hopefully it covers the maximum
1552 * configuration latency.
1553 *
1554 * As a fallback, the checks in _append_oa_reports() to skip
1555 * invalid OA reports do also seem to work to discard reports
1556 * generated before this config has completed - albeit not
1557 * silently.
1558 *
1559 * Unfortunately this is essentially a magic number, since we
1560 * don't currently know of a reliable mechanism for predicting
1561 * how long the MUX config will take to apply and besides
1562 * seeing invalid reports we don't know of a reliable way to
1563 * explicitly check that the MUX config has landed.
1564 *
1565 * It's even possible we've miss characterized the underlying
1566 * problem - it just seems like the simplest explanation why
1567 * a delay at this location would mitigate any invalid reports.
1568 */
1569 usleep_range(15000, 20000);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001570}
1571
1572static int hsw_enable_metric_set(struct i915_perf_stream *stream)
1573{
Chris Wilson52111c42019-10-10 16:05:20 +01001574 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001575 const struct i915_oa_config *oa_config = stream->oa_config;
1576
1577 /*
1578 * PRM:
1579 *
1580 * OA unit is using “crclk” for its functionality. When trunk
1581 * level clock gating takes place, OA clock would be gated,
1582 * unable to count the events from non-render clock domain.
1583 * Render clock gating must be disabled when OA is enabled to
1584 * count the events from non-render domain. Unit level clock
1585 * gating for RCS should also be disabled.
1586 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001587 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
1588 GEN7_DOP_CLOCK_GATE_ENABLE, 0);
1589 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
1590 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001591
Chris Wilson8f8b1172019-10-07 22:09:41 +01001592 config_oa_regs(uncore, oa_config->mux_regs, oa_config->mux_regs_len);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001593 delay_after_mux();
Robert Braggd7965152016-11-07 19:49:52 +00001594
Chris Wilson8f8b1172019-10-07 22:09:41 +01001595 config_oa_regs(uncore, oa_config->b_counter_regs,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001596 oa_config->b_counter_regs_len);
Robert Braggd7965152016-11-07 19:49:52 +00001597
1598 return 0;
1599}
1600
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001601static void hsw_disable_metric_set(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001602{
Chris Wilson52111c42019-10-10 16:05:20 +01001603 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001604
Chris Wilson8f8b1172019-10-07 22:09:41 +01001605 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
1606 GEN6_CSUNIT_CLOCK_GATE_DISABLE, 0);
1607 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
1608 0, GEN7_DOP_CLOCK_GATE_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00001609
Chris Wilson8f8b1172019-10-07 22:09:41 +01001610 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Braggd7965152016-11-07 19:49:52 +00001611}
1612
Chris Wilsona9877da2019-07-16 22:34:43 +01001613static u32 oa_config_flex_reg(const struct i915_oa_config *oa_config,
1614 i915_reg_t reg)
1615{
1616 u32 mmio = i915_mmio_reg_offset(reg);
1617 int i;
1618
1619 /*
1620 * This arbitrary default will select the 'EU FPU0 Pipeline
1621 * Active' event. In the future it's anticipated that there
1622 * will be an explicit 'No Event' we can select, but not yet...
1623 */
1624 if (!oa_config)
1625 return 0;
1626
1627 for (i = 0; i < oa_config->flex_regs_len; i++) {
1628 if (i915_mmio_reg_offset(oa_config->flex_regs[i].addr) == mmio)
1629 return oa_config->flex_regs[i].value;
1630 }
1631
1632 return 0;
1633}
Robert Bragg19f81df2017-06-13 12:23:03 +01001634/*
1635 * NB: It must always remain pointer safe to run this even if the OA unit
1636 * has been disabled.
1637 *
1638 * It's fine to put out-of-date values into these per-context registers
1639 * in the case that the OA unit has been disabled.
1640 */
Chris Wilsonb146e5e2019-03-06 08:47:04 +00001641static void
Chris Wilson7dc56af2019-09-24 15:59:50 +01001642gen8_update_reg_state_unlocked(const struct intel_context *ce,
1643 const struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001644{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001645 u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
1646 u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01001647 /* The MMIO offsets for Flex EU registers aren't contiguous */
Lionel Landwerlin35ab4fd2018-08-13 09:02:18 +01001648 i915_reg_t flex_regs[] = {
1649 EU_PERF_CNTL0,
1650 EU_PERF_CNTL1,
1651 EU_PERF_CNTL2,
1652 EU_PERF_CNTL3,
1653 EU_PERF_CNTL4,
1654 EU_PERF_CNTL5,
1655 EU_PERF_CNTL6,
Robert Bragg19f81df2017-06-13 12:23:03 +01001656 };
Chris Wilson7dc56af2019-09-24 15:59:50 +01001657 u32 *reg_state = ce->lrc_reg_state;
Robert Bragg19f81df2017-06-13 12:23:03 +01001658 int i;
1659
Chris Wilson7dc56af2019-09-24 15:59:50 +01001660 reg_state[ctx_oactxctrl + 1] =
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001661 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
1662 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
Chris Wilson7dc56af2019-09-24 15:59:50 +01001663 GEN8_OA_COUNTER_RESUME;
Robert Bragg19f81df2017-06-13 12:23:03 +01001664
Chris Wilson7dc56af2019-09-24 15:59:50 +01001665 for (i = 0; i < ARRAY_SIZE(flex_regs); i++)
1666 reg_state[ctx_flexeu0 + i * 2 + 1] =
1667 oa_config_flex_reg(stream->oa_config, flex_regs[i]);
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00001668
Chris Wilson8f8b1172019-10-07 22:09:41 +01001669 reg_state[CTX_R_PWR_CLK_STATE] =
1670 intel_sseu_make_rpcs(ce->engine->i915, &ce->sseu);
Robert Bragg19f81df2017-06-13 12:23:03 +01001671}
1672
Chris Wilsona9877da2019-07-16 22:34:43 +01001673struct flex {
1674 i915_reg_t reg;
1675 u32 offset;
1676 u32 value;
1677};
1678
1679static int
1680gen8_store_flex(struct i915_request *rq,
1681 struct intel_context *ce,
1682 const struct flex *flex, unsigned int count)
1683{
1684 u32 offset;
1685 u32 *cs;
1686
1687 cs = intel_ring_begin(rq, 4 * count);
1688 if (IS_ERR(cs))
1689 return PTR_ERR(cs);
1690
1691 offset = i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
1692 do {
1693 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
Chris Wilson7dc56af2019-09-24 15:59:50 +01001694 *cs++ = offset + flex->offset * sizeof(u32);
Chris Wilsona9877da2019-07-16 22:34:43 +01001695 *cs++ = 0;
1696 *cs++ = flex->value;
1697 } while (flex++, --count);
1698
1699 intel_ring_advance(rq, cs);
1700
1701 return 0;
1702}
1703
1704static int
1705gen8_load_flex(struct i915_request *rq,
1706 struct intel_context *ce,
1707 const struct flex *flex, unsigned int count)
1708{
1709 u32 *cs;
1710
1711 GEM_BUG_ON(!count || count > 63);
1712
1713 cs = intel_ring_begin(rq, 2 * count + 2);
1714 if (IS_ERR(cs))
1715 return PTR_ERR(cs);
1716
1717 *cs++ = MI_LOAD_REGISTER_IMM(count);
1718 do {
1719 *cs++ = i915_mmio_reg_offset(flex->reg);
1720 *cs++ = flex->value;
1721 } while (flex++, --count);
1722 *cs++ = MI_NOOP;
1723
1724 intel_ring_advance(rq, cs);
1725
1726 return 0;
1727}
1728
1729static int gen8_modify_context(struct intel_context *ce,
1730 const struct flex *flex, unsigned int count)
1731{
1732 struct i915_request *rq;
1733 int err;
1734
1735 lockdep_assert_held(&ce->pin_mutex);
1736
1737 rq = i915_request_create(ce->engine->kernel_context);
1738 if (IS_ERR(rq))
1739 return PTR_ERR(rq);
1740
1741 /* Serialise with the remote context */
1742 err = intel_context_prepare_remote_request(ce, rq);
1743 if (err == 0)
1744 err = gen8_store_flex(rq, ce, flex, count);
1745
1746 i915_request_add(rq);
1747 return err;
1748}
1749
1750static int gen8_modify_self(struct intel_context *ce,
1751 const struct flex *flex, unsigned int count)
1752{
1753 struct i915_request *rq;
1754 int err;
1755
1756 rq = i915_request_create(ce);
1757 if (IS_ERR(rq))
1758 return PTR_ERR(rq);
1759
1760 err = gen8_load_flex(rq, ce, flex, count);
1761
1762 i915_request_add(rq);
1763 return err;
1764}
1765
Chris Wilson5cca5032019-07-26 14:14:58 +01001766static int gen8_configure_context(struct i915_gem_context *ctx,
1767 struct flex *flex, unsigned int count)
1768{
1769 struct i915_gem_engines_iter it;
1770 struct intel_context *ce;
1771 int err = 0;
1772
1773 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1774 GEM_BUG_ON(ce == ce->engine->kernel_context);
1775
1776 if (ce->engine->class != RENDER_CLASS)
1777 continue;
1778
1779 err = intel_context_lock_pinned(ce);
1780 if (err)
1781 break;
1782
1783 flex->value = intel_sseu_make_rpcs(ctx->i915, &ce->sseu);
1784
1785 /* Otherwise OA settings will be set upon first use */
1786 if (intel_context_is_pinned(ce))
1787 err = gen8_modify_context(ce, flex, count);
1788
1789 intel_context_unlock_pinned(ce);
1790 if (err)
1791 break;
1792 }
1793 i915_gem_context_unlock_engines(ctx);
1794
1795 return err;
1796}
1797
Robert Bragg19f81df2017-06-13 12:23:03 +01001798/*
Robert Bragg19f81df2017-06-13 12:23:03 +01001799 * Manages updating the per-context aspects of the OA stream
1800 * configuration across all contexts.
1801 *
1802 * The awkward consideration here is that OACTXCONTROL controls the
1803 * exponent for periodic sampling which is primarily used for system
1804 * wide profiling where we'd like a consistent sampling period even in
1805 * the face of context switches.
1806 *
1807 * Our approach of updating the register state context (as opposed to
1808 * say using a workaround batch buffer) ensures that the hardware
1809 * won't automatically reload an out-of-date timer exponent even
1810 * transiently before a WA BB could be parsed.
1811 *
1812 * This function needs to:
1813 * - Ensure the currently running context's per-context OA state is
1814 * updated
1815 * - Ensure that all existing contexts will have the correct per-context
1816 * OA state if they are scheduled for use.
1817 * - Ensure any new contexts will be initialized with the correct
1818 * per-context OA state.
1819 *
1820 * Note: it's only the RCS/Render context that has any OA state.
1821 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001822static int gen8_configure_all_contexts(struct i915_perf_stream *stream,
Lionel Landwerlin41d3fdc2018-03-01 11:06:13 +00001823 const struct i915_oa_config *oa_config)
Robert Bragg19f81df2017-06-13 12:23:03 +01001824{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001825 struct drm_i915_private *i915 = stream->perf->i915;
Chris Wilsona9877da2019-07-16 22:34:43 +01001826 /* The MMIO offsets for Flex EU registers aren't contiguous */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001827 const u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
Chris Wilson7dc56af2019-09-24 15:59:50 +01001828#define ctx_flexeuN(N) (ctx_flexeu0 + 2 * (N) + 1)
Chris Wilsona9877da2019-07-16 22:34:43 +01001829 struct flex regs[] = {
1830 {
1831 GEN8_R_PWR_CLK_STATE,
1832 CTX_R_PWR_CLK_STATE,
1833 },
1834 {
1835 GEN8_OACTXCONTROL,
Chris Wilson8f8b1172019-10-07 22:09:41 +01001836 stream->perf->ctx_oactxctrl_offset + 1,
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001837 ((stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
1838 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
Chris Wilsona9877da2019-07-16 22:34:43 +01001839 GEN8_OA_COUNTER_RESUME)
1840 },
1841 { EU_PERF_CNTL0, ctx_flexeuN(0) },
1842 { EU_PERF_CNTL1, ctx_flexeuN(1) },
1843 { EU_PERF_CNTL2, ctx_flexeuN(2) },
1844 { EU_PERF_CNTL3, ctx_flexeuN(3) },
1845 { EU_PERF_CNTL4, ctx_flexeuN(4) },
1846 { EU_PERF_CNTL5, ctx_flexeuN(5) },
1847 { EU_PERF_CNTL6, ctx_flexeuN(6) },
1848 };
1849#undef ctx_flexeuN
1850 struct intel_engine_cs *engine;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001851 struct i915_gem_context *ctx, *cn;
1852 int i, err;
Robert Bragg19f81df2017-06-13 12:23:03 +01001853
Chris Wilsona9877da2019-07-16 22:34:43 +01001854 for (i = 2; i < ARRAY_SIZE(regs); i++)
1855 regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
1856
Chris Wilsona4c969d2019-10-07 22:09:42 +01001857 lockdep_assert_held(&stream->perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01001858
Robert Bragg19f81df2017-06-13 12:23:03 +01001859 /*
1860 * The OA register config is setup through the context image. This image
1861 * might be written to by the GPU on context switch (in particular on
1862 * lite-restore). This means we can't safely update a context's image,
1863 * if this context is scheduled/submitted to run on the GPU.
1864 *
1865 * We could emit the OA register config through the batch buffer but
1866 * this might leave small interval of time where the OA unit is
1867 * configured at an invalid sampling period.
1868 *
Chris Wilsona9877da2019-07-16 22:34:43 +01001869 * Note that since we emit all requests from a single ring, there
1870 * is still an implicit global barrier here that may cause a high
1871 * priority context to wait for an otherwise independent low priority
1872 * context. Contexts idle at the time of reconfiguration are not
1873 * trapped behind the barrier.
Robert Bragg19f81df2017-06-13 12:23:03 +01001874 */
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001875 spin_lock(&i915->gem.contexts.lock);
1876 list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
Chris Wilsona9877da2019-07-16 22:34:43 +01001877 if (ctx == i915->kernel_context)
1878 continue;
1879
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001880 if (!kref_get_unless_zero(&ctx->ref))
1881 continue;
1882
1883 spin_unlock(&i915->gem.contexts.lock);
1884
Chris Wilson5cca5032019-07-26 14:14:58 +01001885 err = gen8_configure_context(ctx, regs, ARRAY_SIZE(regs));
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001886 if (err) {
1887 i915_gem_context_put(ctx);
Chris Wilsona9877da2019-07-16 22:34:43 +01001888 return err;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001889 }
1890
1891 spin_lock(&i915->gem.contexts.lock);
1892 list_safe_reset_next(ctx, cn, link);
1893 i915_gem_context_put(ctx);
Robert Bragg19f81df2017-06-13 12:23:03 +01001894 }
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01001895 spin_unlock(&i915->gem.contexts.lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01001896
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001897 /*
Chris Wilsona9877da2019-07-16 22:34:43 +01001898 * After updating all other contexts, we need to modify ourselves.
1899 * If we don't modify the kernel_context, we do not get events while
1900 * idle.
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001901 */
Chris Wilson750e76b2019-08-06 13:43:00 +01001902 for_each_uabi_engine(engine, i915) {
Chris Wilsona9877da2019-07-16 22:34:43 +01001903 struct intel_context *ce = engine->kernel_context;
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001904
Chris Wilsona9877da2019-07-16 22:34:43 +01001905 if (engine->class != RENDER_CLASS)
1906 continue;
1907
1908 regs[0].value = intel_sseu_make_rpcs(i915, &ce->sseu);
1909
1910 err = gen8_modify_self(ce, regs, ARRAY_SIZE(regs));
1911 if (err)
1912 return err;
1913 }
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01001914
1915 return 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01001916}
1917
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001918static int gen8_enable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001919{
Chris Wilson52111c42019-10-10 16:05:20 +01001920 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001921 const struct i915_oa_config *oa_config = stream->oa_config;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001922 int ret;
Robert Bragg19f81df2017-06-13 12:23:03 +01001923
1924 /*
1925 * We disable slice/unslice clock ratio change reports on SKL since
1926 * they are too noisy. The HW generates a lot of redundant reports
1927 * where the ratio hasn't really changed causing a lot of redundant
1928 * work to processes and increasing the chances we'll hit buffer
1929 * overruns.
1930 *
1931 * Although we don't currently use the 'disable overrun' OABUFFER
1932 * feature it's worth noting that clock ratio reports have to be
1933 * disabled before considering to use that feature since the HW doesn't
1934 * correctly block these reports.
1935 *
1936 * Currently none of the high-level metrics we have depend on knowing
1937 * this ratio to normalize.
1938 *
1939 * Note: This register is not power context saved and restored, but
1940 * that's OK considering that we disable RC6 while the OA unit is
1941 * enabled.
1942 *
1943 * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
1944 * be read back from automatically triggered reports, as part of the
1945 * RPT_ID field.
1946 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001947 if (IS_GEN_RANGE(stream->perf->i915, 9, 11)) {
1948 intel_uncore_write(uncore, GEN8_OA_DEBUG,
1949 _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
1950 GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
Robert Bragg19f81df2017-06-13 12:23:03 +01001951 }
1952
1953 /*
1954 * Update all contexts prior writing the mux configurations as we need
1955 * to make sure all slices/subslices are ON before writing to NOA
1956 * registers.
1957 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001958 ret = gen8_configure_all_contexts(stream, oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01001959 if (ret)
1960 return ret;
1961
Chris Wilson8f8b1172019-10-07 22:09:41 +01001962 config_oa_regs(uncore, oa_config->mux_regs, oa_config->mux_regs_len);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001963 delay_after_mux();
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001964
Chris Wilson8f8b1172019-10-07 22:09:41 +01001965 config_oa_regs(uncore, oa_config->b_counter_regs,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001966 oa_config->b_counter_regs_len);
Robert Bragg19f81df2017-06-13 12:23:03 +01001967
1968 return 0;
1969}
1970
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001971static void gen8_disable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001972{
Chris Wilson52111c42019-10-10 16:05:20 +01001973 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001974
Robert Bragg19f81df2017-06-13 12:23:03 +01001975 /* Reset all contexts' slices/subslices configurations. */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001976 gen8_configure_all_contexts(stream, NULL);
Lionel Landwerlin28964cf2017-08-03 17:58:10 +01001977
Chris Wilson8f8b1172019-10-07 22:09:41 +01001978 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01001979}
1980
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001981static void gen10_disable_metric_set(struct i915_perf_stream *stream)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001982{
Chris Wilson52111c42019-10-10 16:05:20 +01001983 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001984
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001985 /* Reset all contexts' slices/subslices configurations. */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001986 gen8_configure_all_contexts(stream, NULL);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001987
1988 /* Make sure we disable noa to save power. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001989 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00001990}
1991
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001992static void gen7_oa_enable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001993{
Chris Wilson52111c42019-10-10 16:05:20 +01001994 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01001995 struct i915_gem_context *ctx = stream->ctx;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001996 u32 ctx_id = stream->specific_ctx_id;
1997 bool periodic = stream->periodic;
1998 u32 period_exponent = stream->period_exponent;
1999 u32 report_format = stream->oa_buffer.format;
Lionel Landwerlin11051302018-03-26 10:08:23 +01002000
Robert Bragg1bef3402017-06-13 12:23:06 +01002001 /*
2002 * Reset buf pointers so we don't forward reports from before now.
2003 *
2004 * Think carefully if considering trying to avoid this, since it
2005 * also ensures status flags and the buffer itself are cleared
2006 * in error paths, and we have checks for invalid reports based
2007 * on the assumption that certain fields are written to zeroed
2008 * memory which this helps maintains.
2009 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002010 gen7_init_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002011
Chris Wilson8f8b1172019-10-07 22:09:41 +01002012 intel_uncore_write(uncore, GEN7_OACONTROL,
2013 (ctx_id & GEN7_OACONTROL_CTX_MASK) |
2014 (period_exponent <<
2015 GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
2016 (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
2017 (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
2018 (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
2019 GEN7_OACONTROL_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00002020}
2021
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002022static void gen8_oa_enable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002023{
Chris Wilson52111c42019-10-10 16:05:20 +01002024 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002025 u32 report_format = stream->oa_buffer.format;
Robert Bragg19f81df2017-06-13 12:23:03 +01002026
2027 /*
2028 * Reset buf pointers so we don't forward reports from before now.
2029 *
2030 * Think carefully if considering trying to avoid this, since it
2031 * also ensures status flags and the buffer itself are cleared
2032 * in error paths, and we have checks for invalid reports based
2033 * on the assumption that certain fields are written to zeroed
2034 * memory which this helps maintains.
2035 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002036 gen8_init_oa_buffer(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002037
2038 /*
2039 * Note: we don't rely on the hardware to perform single context
2040 * filtering and instead filter on the cpu based on the context-id
2041 * field of reports
2042 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002043 intel_uncore_write(uncore, GEN8_OACONTROL,
2044 (report_format << GEN8_OA_REPORT_FORMAT_SHIFT) |
2045 GEN8_OA_COUNTER_ENABLE);
Robert Bragg19f81df2017-06-13 12:23:03 +01002046}
2047
Robert Bragg16d98b32016-12-07 21:40:33 +00002048/**
2049 * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
2050 * @stream: An i915 perf stream opened for OA metrics
2051 *
2052 * [Re]enables hardware periodic sampling according to the period configured
2053 * when opening the stream. This also starts a hrtimer that will periodically
2054 * check for data in the circular OA buffer for notifying userspace (e.g.
2055 * during a read() or poll()).
2056 */
Robert Braggd7965152016-11-07 19:49:52 +00002057static void i915_oa_stream_enable(struct i915_perf_stream *stream)
2058{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002059 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002060
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002061 if (stream->periodic)
2062 hrtimer_start(&stream->poll_check_timer,
Robert Braggd7965152016-11-07 19:49:52 +00002063 ns_to_ktime(POLL_PERIOD),
2064 HRTIMER_MODE_REL_PINNED);
2065}
2066
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002067static void gen7_oa_disable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002068{
Chris Wilson52111c42019-10-10 16:05:20 +01002069 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002070
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002071 intel_uncore_write(uncore, GEN7_OACONTROL, 0);
2072 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002073 GEN7_OACONTROL, GEN7_OACONTROL_ENABLE, 0,
2074 50))
2075 DRM_ERROR("wait for OA to be disabled timed out\n");
Robert Braggd7965152016-11-07 19:49:52 +00002076}
2077
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002078static void gen8_oa_disable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002079{
Chris Wilson52111c42019-10-10 16:05:20 +01002080 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002081
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002082 intel_uncore_write(uncore, GEN8_OACONTROL, 0);
2083 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002084 GEN8_OACONTROL, GEN8_OA_COUNTER_ENABLE, 0,
2085 50))
2086 DRM_ERROR("wait for OA to be disabled timed out\n");
Robert Bragg19f81df2017-06-13 12:23:03 +01002087}
2088
Robert Bragg16d98b32016-12-07 21:40:33 +00002089/**
2090 * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
2091 * @stream: An i915 perf stream opened for OA metrics
2092 *
2093 * Stops the OA unit from periodically writing counter reports into the
2094 * circular OA buffer. This also stops the hrtimer that periodically checks for
2095 * data in the circular OA buffer, for notifying userspace.
2096 */
Robert Braggd7965152016-11-07 19:49:52 +00002097static void i915_oa_stream_disable(struct i915_perf_stream *stream)
2098{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002099 stream->perf->ops.oa_disable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002100
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002101 if (stream->periodic)
2102 hrtimer_cancel(&stream->poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00002103}
2104
Robert Braggd7965152016-11-07 19:49:52 +00002105static const struct i915_perf_stream_ops i915_oa_stream_ops = {
2106 .destroy = i915_oa_stream_destroy,
2107 .enable = i915_oa_stream_enable,
2108 .disable = i915_oa_stream_disable,
2109 .wait_unlocked = i915_oa_wait_unlocked,
2110 .poll_wait = i915_oa_poll_wait,
2111 .read = i915_oa_read,
2112};
2113
Robert Bragg16d98b32016-12-07 21:40:33 +00002114/**
2115 * i915_oa_stream_init - validate combined props for OA stream and init
2116 * @stream: An i915 perf stream
2117 * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
2118 * @props: The property state that configures stream (individually validated)
2119 *
2120 * While read_properties_unlocked() validates properties in isolation it
2121 * doesn't ensure that the combination necessarily makes sense.
2122 *
2123 * At this point it has been determined that userspace wants a stream of
2124 * OA metrics, but still we need to further validate the combined
2125 * properties are OK.
2126 *
2127 * If the configuration makes sense then we can allocate memory for
2128 * a circular OA buffer and apply the requested metric set configuration.
2129 *
2130 * Returns: zero on success or a negative error code.
2131 */
Robert Braggd7965152016-11-07 19:49:52 +00002132static int i915_oa_stream_init(struct i915_perf_stream *stream,
2133 struct drm_i915_perf_open_param *param,
2134 struct perf_open_properties *props)
2135{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002136 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00002137 int format_size;
2138 int ret;
2139
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002140 if (!props->engine) {
2141 DRM_DEBUG("OA engine not specified\n");
2142 return -EINVAL;
2143 }
2144
2145 /*
2146 * If the sysfs metrics/ directory wasn't registered for some
Robert Bragg442b8c02016-11-07 19:49:53 +00002147 * reason then don't let userspace try their luck with config
2148 * IDs
2149 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002150 if (!perf->metrics_kobj) {
Robert Bragg77085502016-12-01 17:21:52 +00002151 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
Robert Bragg442b8c02016-11-07 19:49:53 +00002152 return -EINVAL;
2153 }
2154
Robert Braggd7965152016-11-07 19:49:52 +00002155 if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
Robert Bragg77085502016-12-01 17:21:52 +00002156 DRM_DEBUG("Only OA report sampling supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002157 return -EINVAL;
2158 }
2159
Chris Wilson8f8b1172019-10-07 22:09:41 +01002160 if (!perf->ops.enable_metric_set) {
Robert Bragg77085502016-12-01 17:21:52 +00002161 DRM_DEBUG("OA unit not supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002162 return -ENODEV;
2163 }
2164
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002165 /*
2166 * To avoid the complexity of having to accurately filter
Robert Braggd7965152016-11-07 19:49:52 +00002167 * counter reports and marshal to the appropriate client
2168 * we currently only allow exclusive access
2169 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002170 if (perf->exclusive_stream) {
Robert Bragg77085502016-12-01 17:21:52 +00002171 DRM_DEBUG("OA unit already in use\n");
Robert Braggd7965152016-11-07 19:49:52 +00002172 return -EBUSY;
2173 }
2174
Robert Braggd7965152016-11-07 19:49:52 +00002175 if (!props->oa_format) {
Robert Bragg77085502016-12-01 17:21:52 +00002176 DRM_DEBUG("OA report format not specified\n");
Robert Braggd7965152016-11-07 19:49:52 +00002177 return -EINVAL;
2178 }
2179
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002180 stream->engine = props->engine;
Chris Wilson52111c42019-10-10 16:05:20 +01002181 stream->uncore = stream->engine->gt->uncore;
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002182
Robert Braggd7965152016-11-07 19:49:52 +00002183 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
2184
Chris Wilson8f8b1172019-10-07 22:09:41 +01002185 format_size = perf->oa_formats[props->oa_format].size;
Robert Braggd7965152016-11-07 19:49:52 +00002186
2187 stream->sample_flags |= SAMPLE_OA_REPORT;
2188 stream->sample_size += format_size;
2189
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002190 stream->oa_buffer.format_size = format_size;
2191 if (WARN_ON(stream->oa_buffer.format_size == 0))
Robert Braggd7965152016-11-07 19:49:52 +00002192 return -EINVAL;
2193
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002194 stream->oa_buffer.format =
Chris Wilson8f8b1172019-10-07 22:09:41 +01002195 perf->oa_formats[props->oa_format].format;
Robert Braggd7965152016-11-07 19:49:52 +00002196
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002197 stream->periodic = props->oa_periodic;
2198 if (stream->periodic)
2199 stream->period_exponent = props->oa_period_exponent;
Robert Braggd7965152016-11-07 19:49:52 +00002200
Robert Braggd7965152016-11-07 19:49:52 +00002201 if (stream->ctx) {
2202 ret = oa_get_render_ctx_id(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002203 if (ret) {
2204 DRM_DEBUG("Invalid context id to filter with\n");
Robert Braggd7965152016-11-07 19:49:52 +00002205 return ret;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002206 }
Robert Braggd7965152016-11-07 19:49:52 +00002207 }
2208
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002209 stream->oa_config = i915_perf_get_oa_config(perf, props->metrics_set);
2210 if (!stream->oa_config) {
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002211 DRM_DEBUG("Invalid OA config id=%i\n", props->metrics_set);
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002212 ret = -EINVAL;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002213 goto err_config;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002214 }
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002215
Robert Braggd7965152016-11-07 19:49:52 +00002216 /* PRM - observability performance counters:
2217 *
2218 * OACONTROL, performance counter enable, note:
2219 *
2220 * "When this bit is set, in order to have coherent counts,
2221 * RC6 power state and trunk clock gating must be disabled.
2222 * This can be achieved by programming MMIO registers as
2223 * 0xA094=0 and 0xA090[31]=1"
2224 *
2225 * In our case we are expecting that taking pm + FORCEWAKE
2226 * references will effectively disable RC6.
2227 */
Chris Wilsona5efcde2019-10-11 20:03:17 +01002228 intel_engine_pm_get(stream->engine);
Chris Wilson52111c42019-10-10 16:05:20 +01002229 intel_uncore_forcewake_get(stream->uncore, FORCEWAKE_ALL);
Robert Braggd7965152016-11-07 19:49:52 +00002230
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002231 ret = alloc_oa_buffer(stream);
sagar.a.kamble@intel.com987f8c42017-06-27 23:09:41 +05302232 if (ret)
2233 goto err_oa_buf_alloc;
2234
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002235 stream->ops = &i915_oa_stream_ops;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002236 perf->exclusive_stream = stream;
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002237
Chris Wilson8f8b1172019-10-07 22:09:41 +01002238 ret = perf->ops.enable_metric_set(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002239 if (ret) {
2240 DRM_DEBUG("Unable to enable metric set\n");
Robert Braggd7965152016-11-07 19:49:52 +00002241 goto err_enable;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002242 }
Robert Braggd7965152016-11-07 19:49:52 +00002243
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002244 DRM_DEBUG("opening stream oa config uuid=%s\n",
2245 stream->oa_config->uuid);
2246
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002247 hrtimer_init(&stream->poll_check_timer,
2248 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2249 stream->poll_check_timer.function = oa_poll_check_timer_cb;
2250 init_waitqueue_head(&stream->poll_wq);
2251 spin_lock_init(&stream->oa_buffer.ptr_lock);
2252
Robert Braggd7965152016-11-07 19:49:52 +00002253 return 0;
2254
2255err_enable:
Chris Wilson8f8b1172019-10-07 22:09:41 +01002256 perf->exclusive_stream = NULL;
2257 perf->ops.disable_metric_set(stream);
Lionel Landwerlin41d3fdc2018-03-01 11:06:13 +00002258
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002259 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002260
2261err_oa_buf_alloc:
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002262 free_oa_configs(stream);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002263
Chris Wilson52111c42019-10-10 16:05:20 +01002264 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
Chris Wilsona5efcde2019-10-11 20:03:17 +01002265 intel_engine_pm_put(stream->engine);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002266
2267err_config:
Robert Braggd7965152016-11-07 19:49:52 +00002268 if (stream->ctx)
2269 oa_put_render_ctx_id(stream);
2270
2271 return ret;
2272}
2273
Chris Wilson7dc56af2019-09-24 15:59:50 +01002274void i915_oa_init_reg_state(const struct intel_context *ce,
2275 const struct intel_engine_cs *engine)
Robert Bragg19f81df2017-06-13 12:23:03 +01002276{
Chris Wilson28b6cb02017-08-10 18:57:43 +01002277 struct i915_perf_stream *stream;
Robert Bragg19f81df2017-06-13 12:23:03 +01002278
Chris Wilsondffa8fe2019-08-30 19:19:29 +01002279 /* perf.exclusive_stream serialised by gen8_configure_all_contexts() */
2280 lockdep_assert_held(&ce->pin_mutex);
2281
Chris Wilson8a68d462019-03-05 18:03:30 +00002282 if (engine->class != RENDER_CLASS)
Robert Bragg19f81df2017-06-13 12:23:03 +01002283 return;
2284
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002285 stream = engine->i915->perf.exclusive_stream;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002286 if (stream)
Chris Wilson7dc56af2019-09-24 15:59:50 +01002287 gen8_update_reg_state_unlocked(ce, stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002288}
2289
Robert Bragg16d98b32016-12-07 21:40:33 +00002290/**
2291 * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation
2292 * @stream: An i915 perf stream
2293 * @file: An i915 perf stream file
2294 * @buf: destination buffer given by userspace
2295 * @count: the number of bytes userspace wants to read
2296 * @ppos: (inout) file seek position (unused)
2297 *
2298 * Besides wrapping &i915_perf_stream_ops->read this provides a common place to
2299 * ensure that if we've successfully copied any data then reporting that takes
2300 * precedence over any internal error status, so the data isn't lost.
2301 *
2302 * For example ret will be -ENOSPC whenever there is more buffered data than
2303 * can be copied to userspace, but that's only interesting if we weren't able
2304 * to copy some data because it implies the userspace buffer is too small to
2305 * receive a single record (and we never split records).
2306 *
2307 * Another case with ret == -EFAULT is more of a grey area since it would seem
2308 * like bad form for userspace to ask us to overrun its buffer, but the user
2309 * knows best:
2310 *
2311 * http://yarchive.net/comp/linux/partial_reads_writes.html
2312 *
2313 * Returns: The number of bytes copied or a negative error code on failure.
2314 */
Robert Braggeec688e2016-11-07 19:49:47 +00002315static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
2316 struct file *file,
2317 char __user *buf,
2318 size_t count,
2319 loff_t *ppos)
2320{
2321 /* Note we keep the offset (aka bytes read) separate from any
2322 * error status so that the final check for whether we return
2323 * the bytes read with a higher precedence than any error (see
2324 * comment below) doesn't need to be handled/duplicated in
2325 * stream->ops->read() implementations.
2326 */
2327 size_t offset = 0;
2328 int ret = stream->ops->read(stream, buf, count, &offset);
2329
Robert Braggeec688e2016-11-07 19:49:47 +00002330 return offset ?: (ret ?: -EAGAIN);
2331}
2332
Robert Bragg16d98b32016-12-07 21:40:33 +00002333/**
2334 * i915_perf_read - handles read() FOP for i915 perf stream FDs
2335 * @file: An i915 perf stream file
2336 * @buf: destination buffer given by userspace
2337 * @count: the number of bytes userspace wants to read
2338 * @ppos: (inout) file seek position (unused)
2339 *
2340 * The entry point for handling a read() on a stream file descriptor from
2341 * userspace. Most of the work is left to the i915_perf_read_locked() and
2342 * &i915_perf_stream_ops->read but to save having stream implementations (of
2343 * which we might have multiple later) we handle blocking read here.
2344 *
2345 * We can also consistently treat trying to read from a disabled stream
2346 * as an IO error so implementations can assume the stream is enabled
2347 * while reading.
2348 *
2349 * Returns: The number of bytes copied or a negative error code on failure.
2350 */
Robert Braggeec688e2016-11-07 19:49:47 +00002351static ssize_t i915_perf_read(struct file *file,
2352 char __user *buf,
2353 size_t count,
2354 loff_t *ppos)
2355{
2356 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002357 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002358 ssize_t ret;
2359
Robert Braggd7965152016-11-07 19:49:52 +00002360 /* To ensure it's handled consistently we simply treat all reads of a
2361 * disabled stream as an error. In particular it might otherwise lead
2362 * to a deadlock for blocking file descriptors...
2363 */
2364 if (!stream->enabled)
2365 return -EIO;
2366
Robert Braggeec688e2016-11-07 19:49:47 +00002367 if (!(file->f_flags & O_NONBLOCK)) {
Robert Braggd7965152016-11-07 19:49:52 +00002368 /* There's the small chance of false positives from
2369 * stream->ops->wait_unlocked.
2370 *
2371 * E.g. with single context filtering since we only wait until
2372 * oabuffer has >= 1 report we don't immediately know whether
2373 * any reports really belong to the current context
Robert Braggeec688e2016-11-07 19:49:47 +00002374 */
2375 do {
2376 ret = stream->ops->wait_unlocked(stream);
2377 if (ret)
2378 return ret;
2379
Chris Wilson8f8b1172019-10-07 22:09:41 +01002380 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002381 ret = i915_perf_read_locked(stream, file,
2382 buf, count, ppos);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002383 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002384 } while (ret == -EAGAIN);
2385 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01002386 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002387 ret = i915_perf_read_locked(stream, file, buf, count, ppos);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002388 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002389 }
2390
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002391 /* We allow the poll checking to sometimes report false positive EPOLLIN
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002392 * events where we might actually report EAGAIN on read() if there's
2393 * not really any data available. In this situation though we don't
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002394 * want to enter a busy loop between poll() reporting a EPOLLIN event
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002395 * and read() returning -EAGAIN. Clearing the oa.pollin state here
2396 * effectively ensures we back off until the next hrtimer callback
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002397 * before reporting another EPOLLIN event.
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002398 */
2399 if (ret >= 0 || ret == -EAGAIN) {
Robert Braggd7965152016-11-07 19:49:52 +00002400 /* Maybe make ->pollin per-stream state if we support multiple
2401 * concurrent streams in the future.
2402 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002403 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00002404 }
2405
Robert Braggeec688e2016-11-07 19:49:47 +00002406 return ret;
2407}
2408
Robert Braggd7965152016-11-07 19:49:52 +00002409static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
2410{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002411 struct i915_perf_stream *stream =
2412 container_of(hrtimer, typeof(*stream), poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00002413
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002414 if (oa_buffer_check_unlocked(stream)) {
2415 stream->pollin = true;
2416 wake_up(&stream->poll_wq);
Robert Braggd7965152016-11-07 19:49:52 +00002417 }
2418
2419 hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
2420
2421 return HRTIMER_RESTART;
2422}
2423
Robert Bragg16d98b32016-12-07 21:40:33 +00002424/**
2425 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
Robert Bragg16d98b32016-12-07 21:40:33 +00002426 * @stream: An i915 perf stream
2427 * @file: An i915 perf stream file
2428 * @wait: poll() state table
2429 *
2430 * For handling userspace polling on an i915 perf stream, this calls through to
2431 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
2432 * will be woken for new stream data.
2433 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01002434 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00002435 * with any non-file-operation driver hooks.
2436 *
2437 * Returns: any poll events that are ready without sleeping
2438 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002439static __poll_t i915_perf_poll_locked(struct i915_perf_stream *stream,
2440 struct file *file,
2441 poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00002442{
Al Viroafc9a422017-07-03 06:39:46 -04002443 __poll_t events = 0;
Robert Braggeec688e2016-11-07 19:49:47 +00002444
2445 stream->ops->poll_wait(stream, file, wait);
2446
Robert Braggd7965152016-11-07 19:49:52 +00002447 /* Note: we don't explicitly check whether there's something to read
2448 * here since this path may be very hot depending on what else
2449 * userspace is polling, or on the timeout in use. We rely solely on
2450 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
2451 * samples to read.
2452 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002453 if (stream->pollin)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002454 events |= EPOLLIN;
Robert Braggeec688e2016-11-07 19:49:47 +00002455
Robert Braggd7965152016-11-07 19:49:52 +00002456 return events;
Robert Braggeec688e2016-11-07 19:49:47 +00002457}
2458
Robert Bragg16d98b32016-12-07 21:40:33 +00002459/**
2460 * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
2461 * @file: An i915 perf stream file
2462 * @wait: poll() state table
2463 *
2464 * For handling userspace polling on an i915 perf stream, this ensures
2465 * poll_wait() gets called with a wait queue that will be woken for new stream
2466 * data.
2467 *
2468 * Note: Implementation deferred to i915_perf_poll_locked()
2469 *
2470 * Returns: any poll events that are ready without sleeping
2471 */
Al Viroafc9a422017-07-03 06:39:46 -04002472static __poll_t i915_perf_poll(struct file *file, poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00002473{
2474 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002475 struct i915_perf *perf = stream->perf;
Al Viroafc9a422017-07-03 06:39:46 -04002476 __poll_t ret;
Robert Braggeec688e2016-11-07 19:49:47 +00002477
Chris Wilson8f8b1172019-10-07 22:09:41 +01002478 mutex_lock(&perf->lock);
2479 ret = i915_perf_poll_locked(stream, file, wait);
2480 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002481
2482 return ret;
2483}
2484
Robert Bragg16d98b32016-12-07 21:40:33 +00002485/**
2486 * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
2487 * @stream: A disabled i915 perf stream
2488 *
2489 * [Re]enables the associated capture of data for this stream.
2490 *
2491 * If a stream was previously enabled then there's currently no intention
2492 * to provide userspace any guarantee about the preservation of previously
2493 * buffered data.
2494 */
Robert Braggeec688e2016-11-07 19:49:47 +00002495static void i915_perf_enable_locked(struct i915_perf_stream *stream)
2496{
2497 if (stream->enabled)
2498 return;
2499
2500 /* Allow stream->ops->enable() to refer to this */
2501 stream->enabled = true;
2502
2503 if (stream->ops->enable)
2504 stream->ops->enable(stream);
2505}
2506
Robert Bragg16d98b32016-12-07 21:40:33 +00002507/**
2508 * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
2509 * @stream: An enabled i915 perf stream
2510 *
2511 * Disables the associated capture of data for this stream.
2512 *
2513 * The intention is that disabling an re-enabling a stream will ideally be
2514 * cheaper than destroying and re-opening a stream with the same configuration,
2515 * though there are no formal guarantees about what state or buffered data
2516 * must be retained between disabling and re-enabling a stream.
2517 *
2518 * Note: while a stream is disabled it's considered an error for userspace
2519 * to attempt to read from the stream (-EIO).
2520 */
Robert Braggeec688e2016-11-07 19:49:47 +00002521static void i915_perf_disable_locked(struct i915_perf_stream *stream)
2522{
2523 if (!stream->enabled)
2524 return;
2525
2526 /* Allow stream->ops->disable() to refer to this */
2527 stream->enabled = false;
2528
2529 if (stream->ops->disable)
2530 stream->ops->disable(stream);
2531}
2532
Robert Bragg16d98b32016-12-07 21:40:33 +00002533/**
2534 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2535 * @stream: An i915 perf stream
2536 * @cmd: the ioctl request
2537 * @arg: the ioctl data
2538 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01002539 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00002540 * with any non-file-operation driver hooks.
2541 *
2542 * Returns: zero on success or a negative error code. Returns -EINVAL for
2543 * an unknown ioctl request.
2544 */
Robert Braggeec688e2016-11-07 19:49:47 +00002545static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
2546 unsigned int cmd,
2547 unsigned long arg)
2548{
2549 switch (cmd) {
2550 case I915_PERF_IOCTL_ENABLE:
2551 i915_perf_enable_locked(stream);
2552 return 0;
2553 case I915_PERF_IOCTL_DISABLE:
2554 i915_perf_disable_locked(stream);
2555 return 0;
2556 }
2557
2558 return -EINVAL;
2559}
2560
Robert Bragg16d98b32016-12-07 21:40:33 +00002561/**
2562 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2563 * @file: An i915 perf stream file
2564 * @cmd: the ioctl request
2565 * @arg: the ioctl data
2566 *
2567 * Implementation deferred to i915_perf_ioctl_locked().
2568 *
2569 * Returns: zero on success or a negative error code. Returns -EINVAL for
2570 * an unknown ioctl request.
2571 */
Robert Braggeec688e2016-11-07 19:49:47 +00002572static long i915_perf_ioctl(struct file *file,
2573 unsigned int cmd,
2574 unsigned long arg)
2575{
2576 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002577 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002578 long ret;
2579
Chris Wilson8f8b1172019-10-07 22:09:41 +01002580 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002581 ret = i915_perf_ioctl_locked(stream, cmd, arg);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002582 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002583
2584 return ret;
2585}
2586
Robert Bragg16d98b32016-12-07 21:40:33 +00002587/**
2588 * i915_perf_destroy_locked - destroy an i915 perf stream
2589 * @stream: An i915 perf stream
2590 *
2591 * Frees all resources associated with the given i915 perf @stream, disabling
2592 * any associated data capture in the process.
2593 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01002594 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00002595 * with any non-file-operation driver hooks.
2596 */
Robert Braggeec688e2016-11-07 19:49:47 +00002597static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
2598{
Robert Braggeec688e2016-11-07 19:49:47 +00002599 if (stream->enabled)
2600 i915_perf_disable_locked(stream);
2601
2602 if (stream->ops->destroy)
2603 stream->ops->destroy(stream);
2604
Chris Wilson69df05e2016-12-18 15:37:21 +00002605 if (stream->ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01002606 i915_gem_context_put(stream->ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002607
2608 kfree(stream);
2609}
2610
Robert Bragg16d98b32016-12-07 21:40:33 +00002611/**
2612 * i915_perf_release - handles userspace close() of a stream file
2613 * @inode: anonymous inode associated with file
2614 * @file: An i915 perf stream file
2615 *
2616 * Cleans up any resources associated with an open i915 perf stream file.
2617 *
2618 * NB: close() can't really fail from the userspace point of view.
2619 *
2620 * Returns: zero on success or a negative error code.
2621 */
Robert Braggeec688e2016-11-07 19:49:47 +00002622static int i915_perf_release(struct inode *inode, struct file *file)
2623{
2624 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002625 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002626
Chris Wilson8f8b1172019-10-07 22:09:41 +01002627 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002628 i915_perf_destroy_locked(stream);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002629 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002630
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002631 /* Release the reference the perf stream kept on the driver. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002632 drm_dev_put(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002633
Robert Braggeec688e2016-11-07 19:49:47 +00002634 return 0;
2635}
2636
2637
2638static const struct file_operations fops = {
2639 .owner = THIS_MODULE,
2640 .llseek = no_llseek,
2641 .release = i915_perf_release,
2642 .poll = i915_perf_poll,
2643 .read = i915_perf_read,
2644 .unlocked_ioctl = i915_perf_ioctl,
Lionel Landwerlin191f8962017-10-24 16:27:28 +01002645 /* Our ioctl have no arguments, so it's safe to use the same function
2646 * to handle 32bits compatibility.
2647 */
2648 .compat_ioctl = i915_perf_ioctl,
Robert Braggeec688e2016-11-07 19:49:47 +00002649};
2650
2651
Robert Bragg16d98b32016-12-07 21:40:33 +00002652/**
2653 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
Chris Wilson8f8b1172019-10-07 22:09:41 +01002654 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00002655 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
2656 * @props: individually validated u64 property value pairs
2657 * @file: drm file
2658 *
2659 * See i915_perf_ioctl_open() for interface details.
2660 *
2661 * Implements further stream config validation and stream initialization on
Chris Wilson8f8b1172019-10-07 22:09:41 +01002662 * behalf of i915_perf_open_ioctl() with the &perf->lock mutex
Robert Bragg16d98b32016-12-07 21:40:33 +00002663 * taken to serialize with any non-file-operation driver hooks.
2664 *
2665 * Note: at this point the @props have only been validated in isolation and
2666 * it's still necessary to validate that the combination of properties makes
2667 * sense.
2668 *
2669 * In the case where userspace is interested in OA unit metrics then further
2670 * config validation and stream initialization details will be handled by
2671 * i915_oa_stream_init(). The code here should only validate config state that
2672 * will be relevant to all stream types / backends.
2673 *
2674 * Returns: zero on success or a negative error code.
2675 */
Robert Braggeec688e2016-11-07 19:49:47 +00002676static int
Chris Wilson8f8b1172019-10-07 22:09:41 +01002677i915_perf_open_ioctl_locked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00002678 struct drm_i915_perf_open_param *param,
2679 struct perf_open_properties *props,
2680 struct drm_file *file)
2681{
2682 struct i915_gem_context *specific_ctx = NULL;
2683 struct i915_perf_stream *stream = NULL;
2684 unsigned long f_flags = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01002685 bool privileged_op = true;
Robert Braggeec688e2016-11-07 19:49:47 +00002686 int stream_fd;
2687 int ret;
2688
2689 if (props->single_context) {
2690 u32 ctx_handle = props->ctx_handle;
2691 struct drm_i915_file_private *file_priv = file->driver_priv;
2692
Imre Deak635f56c2017-07-14 18:12:41 +03002693 specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle);
2694 if (!specific_ctx) {
2695 DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n",
2696 ctx_handle);
2697 ret = -ENOENT;
Robert Braggeec688e2016-11-07 19:49:47 +00002698 goto err;
2699 }
2700 }
2701
Robert Bragg19f81df2017-06-13 12:23:03 +01002702 /*
2703 * On Haswell the OA unit supports clock gating off for a specific
2704 * context and in this mode there's no visibility of metrics for the
2705 * rest of the system, which we consider acceptable for a
2706 * non-privileged client.
2707 *
2708 * For Gen8+ the OA unit no longer supports clock gating off for a
2709 * specific context and the kernel can't securely stop the counters
2710 * from updating as system-wide / global values. Even though we can
2711 * filter reports based on the included context ID we can't block
2712 * clients from seeing the raw / global counter values via
2713 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
2714 * enable the OA unit by default.
2715 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002716 if (IS_HASWELL(perf->i915) && specific_ctx)
Robert Bragg19f81df2017-06-13 12:23:03 +01002717 privileged_op = false;
2718
Robert Braggccdf6342016-11-07 19:49:54 +00002719 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
2720 * we check a dev.i915.perf_stream_paranoid sysctl option
2721 * to determine if it's ok to access system wide OA counters
2722 * without CAP_SYS_ADMIN privileges.
2723 */
Robert Bragg19f81df2017-06-13 12:23:03 +01002724 if (privileged_op &&
Robert Braggccdf6342016-11-07 19:49:54 +00002725 i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002726 DRM_DEBUG("Insufficient privileges to open system-wide i915 perf stream\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002727 ret = -EACCES;
2728 goto err_ctx;
2729 }
2730
2731 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2732 if (!stream) {
2733 ret = -ENOMEM;
2734 goto err_ctx;
2735 }
2736
Chris Wilson8f8b1172019-10-07 22:09:41 +01002737 stream->perf = perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002738 stream->ctx = specific_ctx;
2739
Robert Braggd7965152016-11-07 19:49:52 +00002740 ret = i915_oa_stream_init(stream, param, props);
2741 if (ret)
2742 goto err_alloc;
2743
2744 /* we avoid simply assigning stream->sample_flags = props->sample_flags
2745 * to have _stream_init check the combination of sample flags more
2746 * thoroughly, but still this is the expected result at this point.
Robert Braggeec688e2016-11-07 19:49:47 +00002747 */
Robert Braggd7965152016-11-07 19:49:52 +00002748 if (WARN_ON(stream->sample_flags != props->sample_flags)) {
2749 ret = -ENODEV;
Matthew Auld22f880c2017-03-27 21:34:59 +01002750 goto err_flags;
Robert Braggd7965152016-11-07 19:49:52 +00002751 }
Robert Braggeec688e2016-11-07 19:49:47 +00002752
Robert Braggeec688e2016-11-07 19:49:47 +00002753 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
2754 f_flags |= O_CLOEXEC;
2755 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
2756 f_flags |= O_NONBLOCK;
2757
2758 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
2759 if (stream_fd < 0) {
2760 ret = stream_fd;
Lionel Landwerlin23b9e412019-10-08 15:01:11 +01002761 goto err_flags;
Robert Braggeec688e2016-11-07 19:49:47 +00002762 }
2763
2764 if (!(param->flags & I915_PERF_FLAG_DISABLED))
2765 i915_perf_enable_locked(stream);
2766
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002767 /* Take a reference on the driver that will be kept with stream_fd
2768 * until its release.
2769 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002770 drm_dev_get(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03002771
Robert Braggeec688e2016-11-07 19:49:47 +00002772 return stream_fd;
2773
Matthew Auld22f880c2017-03-27 21:34:59 +01002774err_flags:
Robert Braggeec688e2016-11-07 19:49:47 +00002775 if (stream->ops->destroy)
2776 stream->ops->destroy(stream);
2777err_alloc:
2778 kfree(stream);
2779err_ctx:
Chris Wilson69df05e2016-12-18 15:37:21 +00002780 if (specific_ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01002781 i915_gem_context_put(specific_ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002782err:
2783 return ret;
2784}
2785
Chris Wilson8f8b1172019-10-07 22:09:41 +01002786static u64 oa_exponent_to_ns(struct i915_perf *perf, int exponent)
Robert Bragg155e9412017-06-13 12:23:05 +01002787{
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01002788 return div64_u64(1000000000ULL * (2ULL << exponent),
Chris Wilson8f8b1172019-10-07 22:09:41 +01002789 1000ULL * RUNTIME_INFO(perf->i915)->cs_timestamp_frequency_khz);
Robert Bragg155e9412017-06-13 12:23:05 +01002790}
2791
Robert Bragg16d98b32016-12-07 21:40:33 +00002792/**
2793 * read_properties_unlocked - validate + copy userspace stream open properties
Chris Wilson8f8b1172019-10-07 22:09:41 +01002794 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00002795 * @uprops: The array of u64 key value pairs given by userspace
2796 * @n_props: The number of key value pairs expected in @uprops
2797 * @props: The stream configuration built up while validating properties
Robert Braggeec688e2016-11-07 19:49:47 +00002798 *
2799 * Note this function only validates properties in isolation it doesn't
2800 * validate that the combination of properties makes sense or that all
2801 * properties necessary for a particular kind of stream have been set.
Robert Bragg16d98b32016-12-07 21:40:33 +00002802 *
2803 * Note that there currently aren't any ordering requirements for properties so
2804 * we shouldn't validate or assume anything about ordering here. This doesn't
2805 * rule out defining new properties with ordering requirements in the future.
Robert Braggeec688e2016-11-07 19:49:47 +00002806 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002807static int read_properties_unlocked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00002808 u64 __user *uprops,
2809 u32 n_props,
2810 struct perf_open_properties *props)
2811{
2812 u64 __user *uprop = uprops;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002813 u32 i;
Robert Braggeec688e2016-11-07 19:49:47 +00002814
2815 memset(props, 0, sizeof(struct perf_open_properties));
2816
2817 if (!n_props) {
Robert Bragg77085502016-12-01 17:21:52 +00002818 DRM_DEBUG("No i915 perf properties given\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002819 return -EINVAL;
2820 }
2821
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002822 /* At the moment we only support using i915-perf on the RCS. */
2823 props->engine = intel_engine_lookup_user(perf->i915,
2824 I915_ENGINE_CLASS_RENDER,
2825 0);
2826 if (!props->engine) {
2827 DRM_DEBUG("No RENDER-capable engines\n");
2828 return -EINVAL;
2829 }
2830
Robert Braggeec688e2016-11-07 19:49:47 +00002831 /* Considering that ID = 0 is reserved and assuming that we don't
2832 * (currently) expect any configurations to ever specify duplicate
2833 * values for a particular property ID then the last _PROP_MAX value is
2834 * one greater than the maximum number of properties we expect to get
2835 * from userspace.
2836 */
2837 if (n_props >= DRM_I915_PERF_PROP_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002838 DRM_DEBUG("More i915 perf properties specified than exist\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002839 return -EINVAL;
2840 }
2841
2842 for (i = 0; i < n_props; i++) {
Robert Bragg00319ba2016-11-07 19:49:55 +00002843 u64 oa_period, oa_freq_hz;
Robert Braggeec688e2016-11-07 19:49:47 +00002844 u64 id, value;
2845 int ret;
2846
2847 ret = get_user(id, uprop);
2848 if (ret)
2849 return ret;
2850
2851 ret = get_user(value, uprop + 1);
2852 if (ret)
2853 return ret;
2854
Matthew Auld0a309f92017-03-27 21:32:36 +01002855 if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
2856 DRM_DEBUG("Unknown i915 perf property ID\n");
2857 return -EINVAL;
2858 }
2859
Robert Braggeec688e2016-11-07 19:49:47 +00002860 switch ((enum drm_i915_perf_property_id)id) {
2861 case DRM_I915_PERF_PROP_CTX_HANDLE:
2862 props->single_context = 1;
2863 props->ctx_handle = value;
2864 break;
Robert Braggd7965152016-11-07 19:49:52 +00002865 case DRM_I915_PERF_PROP_SAMPLE_OA:
Lionel Landwerlinb6dd47b2018-03-26 10:08:22 +01002866 if (value)
2867 props->sample_flags |= SAMPLE_OA_REPORT;
Robert Braggd7965152016-11-07 19:49:52 +00002868 break;
2869 case DRM_I915_PERF_PROP_OA_METRICS_SET:
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002870 if (value == 0) {
Robert Bragg77085502016-12-01 17:21:52 +00002871 DRM_DEBUG("Unknown OA metric set ID\n");
Robert Braggd7965152016-11-07 19:49:52 +00002872 return -EINVAL;
2873 }
2874 props->metrics_set = value;
2875 break;
2876 case DRM_I915_PERF_PROP_OA_FORMAT:
2877 if (value == 0 || value >= I915_OA_FORMAT_MAX) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002878 DRM_DEBUG("Out-of-range OA report format %llu\n",
2879 value);
Robert Braggd7965152016-11-07 19:49:52 +00002880 return -EINVAL;
2881 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01002882 if (!perf->oa_formats[value].size) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002883 DRM_DEBUG("Unsupported OA report format %llu\n",
2884 value);
Robert Braggd7965152016-11-07 19:49:52 +00002885 return -EINVAL;
2886 }
2887 props->oa_format = value;
2888 break;
2889 case DRM_I915_PERF_PROP_OA_EXPONENT:
2890 if (value > OA_EXPONENT_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002891 DRM_DEBUG("OA timer exponent too high (> %u)\n",
2892 OA_EXPONENT_MAX);
Robert Braggd7965152016-11-07 19:49:52 +00002893 return -EINVAL;
2894 }
2895
Robert Bragg00319ba2016-11-07 19:49:55 +00002896 /* Theoretically we can program the OA unit to sample
Robert Bragg155e9412017-06-13 12:23:05 +01002897 * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
2898 * for BXT. We don't allow such high sampling
2899 * frequencies by default unless root.
Robert Braggd7965152016-11-07 19:49:52 +00002900 */
Robert Bragg155e9412017-06-13 12:23:05 +01002901
Robert Bragg00319ba2016-11-07 19:49:55 +00002902 BUILD_BUG_ON(sizeof(oa_period) != 8);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002903 oa_period = oa_exponent_to_ns(perf, value);
Robert Bragg00319ba2016-11-07 19:49:55 +00002904
2905 /* This check is primarily to ensure that oa_period <=
2906 * UINT32_MAX (before passing to do_div which only
2907 * accepts a u32 denominator), but we can also skip
2908 * checking anything < 1Hz which implicitly can't be
2909 * limited via an integer oa_max_sample_rate.
2910 */
2911 if (oa_period <= NSEC_PER_SEC) {
2912 u64 tmp = NSEC_PER_SEC;
2913 do_div(tmp, oa_period);
2914 oa_freq_hz = tmp;
2915 } else
2916 oa_freq_hz = 0;
2917
2918 if (oa_freq_hz > i915_oa_max_sample_rate &&
2919 !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002920 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 +00002921 i915_oa_max_sample_rate);
Robert Braggd7965152016-11-07 19:49:52 +00002922 return -EACCES;
2923 }
2924
2925 props->oa_periodic = true;
2926 props->oa_period_exponent = value;
2927 break;
Matthew Auld0a309f92017-03-27 21:32:36 +01002928 case DRM_I915_PERF_PROP_MAX:
Robert Braggeec688e2016-11-07 19:49:47 +00002929 MISSING_CASE(id);
Robert Braggeec688e2016-11-07 19:49:47 +00002930 return -EINVAL;
2931 }
2932
2933 uprop += 2;
2934 }
2935
2936 return 0;
2937}
2938
Robert Bragg16d98b32016-12-07 21:40:33 +00002939/**
2940 * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
2941 * @dev: drm device
2942 * @data: ioctl data copied from userspace (unvalidated)
2943 * @file: drm file
2944 *
2945 * Validates the stream open parameters given by userspace including flags
2946 * and an array of u64 key, value pair properties.
2947 *
2948 * Very little is assumed up front about the nature of the stream being
2949 * opened (for instance we don't assume it's for periodic OA unit metrics). An
2950 * i915-perf stream is expected to be a suitable interface for other forms of
2951 * buffered data written by the GPU besides periodic OA metrics.
2952 *
2953 * Note we copy the properties from userspace outside of the i915 perf
2954 * mutex to avoid an awkward lockdep with mmap_sem.
2955 *
2956 * Most of the implementation details are handled by
Chris Wilson8f8b1172019-10-07 22:09:41 +01002957 * i915_perf_open_ioctl_locked() after taking the &perf->lock
Robert Bragg16d98b32016-12-07 21:40:33 +00002958 * mutex for serializing with any non-file-operation driver hooks.
2959 *
2960 * Return: A newly opened i915 Perf stream file descriptor or negative
2961 * error code on failure.
2962 */
Robert Braggeec688e2016-11-07 19:49:47 +00002963int i915_perf_open_ioctl(struct drm_device *dev, void *data,
2964 struct drm_file *file)
2965{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002966 struct i915_perf *perf = &to_i915(dev)->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002967 struct drm_i915_perf_open_param *param = data;
2968 struct perf_open_properties props;
2969 u32 known_open_flags;
2970 int ret;
2971
Chris Wilson8f8b1172019-10-07 22:09:41 +01002972 if (!perf->i915) {
Robert Bragg77085502016-12-01 17:21:52 +00002973 DRM_DEBUG("i915 perf interface not available for this system\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002974 return -ENOTSUPP;
2975 }
2976
2977 known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
2978 I915_PERF_FLAG_FD_NONBLOCK |
2979 I915_PERF_FLAG_DISABLED;
2980 if (param->flags & ~known_open_flags) {
Robert Bragg77085502016-12-01 17:21:52 +00002981 DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002982 return -EINVAL;
2983 }
2984
Chris Wilson8f8b1172019-10-07 22:09:41 +01002985 ret = read_properties_unlocked(perf,
Robert Braggeec688e2016-11-07 19:49:47 +00002986 u64_to_user_ptr(param->properties_ptr),
2987 param->num_properties,
2988 &props);
2989 if (ret)
2990 return ret;
2991
Chris Wilson8f8b1172019-10-07 22:09:41 +01002992 mutex_lock(&perf->lock);
2993 ret = i915_perf_open_ioctl_locked(perf, param, &props, file);
2994 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002995
2996 return ret;
2997}
2998
Robert Bragg16d98b32016-12-07 21:40:33 +00002999/**
3000 * i915_perf_register - exposes i915-perf to userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01003001 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003002 *
3003 * In particular OA metric sets are advertised under a sysfs metrics/
3004 * directory allowing userspace to enumerate valid IDs that can be
3005 * used to open an i915-perf stream.
3006 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003007void i915_perf_register(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003008{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003009 struct i915_perf *perf = &i915->perf;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003010 int ret;
3011
Chris Wilson8f8b1172019-10-07 22:09:41 +01003012 if (!perf->i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003013 return;
3014
3015 /* To be sure we're synchronized with an attempted
3016 * i915_perf_open_ioctl(); considering that we register after
3017 * being exposed to userspace.
3018 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003019 mutex_lock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003020
Chris Wilson8f8b1172019-10-07 22:09:41 +01003021 perf->metrics_kobj =
Robert Bragg442b8c02016-11-07 19:49:53 +00003022 kobject_create_and_add("metrics",
Chris Wilson8f8b1172019-10-07 22:09:41 +01003023 &i915->drm.primary->kdev->kobj);
3024 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003025 goto exit;
3026
Chris Wilson8f8b1172019-10-07 22:09:41 +01003027 sysfs_attr_init(&perf->test_config.sysfs_metric_id.attr);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003028
Chris Wilson8f8b1172019-10-07 22:09:41 +01003029 if (INTEL_GEN(i915) >= 11) {
3030 i915_perf_load_test_config_icl(i915);
3031 } else if (IS_CANNONLAKE(i915)) {
3032 i915_perf_load_test_config_cnl(i915);
3033 } else if (IS_COFFEELAKE(i915)) {
3034 if (IS_CFL_GT2(i915))
3035 i915_perf_load_test_config_cflgt2(i915);
3036 if (IS_CFL_GT3(i915))
3037 i915_perf_load_test_config_cflgt3(i915);
3038 } else if (IS_GEMINILAKE(i915)) {
3039 i915_perf_load_test_config_glk(i915);
3040 } else if (IS_KABYLAKE(i915)) {
3041 if (IS_KBL_GT2(i915))
3042 i915_perf_load_test_config_kblgt2(i915);
3043 else if (IS_KBL_GT3(i915))
3044 i915_perf_load_test_config_kblgt3(i915);
3045 } else if (IS_BROXTON(i915)) {
3046 i915_perf_load_test_config_bxt(i915);
3047 } else if (IS_SKYLAKE(i915)) {
3048 if (IS_SKL_GT2(i915))
3049 i915_perf_load_test_config_sklgt2(i915);
3050 else if (IS_SKL_GT3(i915))
3051 i915_perf_load_test_config_sklgt3(i915);
3052 else if (IS_SKL_GT4(i915))
3053 i915_perf_load_test_config_sklgt4(i915);
3054 } else if (IS_CHERRYVIEW(i915)) {
3055 i915_perf_load_test_config_chv(i915);
3056 } else if (IS_BROADWELL(i915)) {
3057 i915_perf_load_test_config_bdw(i915);
3058 } else if (IS_HASWELL(i915)) {
3059 i915_perf_load_test_config_hsw(i915);
3060 }
Robert Bragg442b8c02016-11-07 19:49:53 +00003061
Chris Wilson8f8b1172019-10-07 22:09:41 +01003062 if (perf->test_config.id == 0)
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003063 goto sysfs_error;
3064
Chris Wilson8f8b1172019-10-07 22:09:41 +01003065 ret = sysfs_create_group(perf->metrics_kobj,
3066 &perf->test_config.sysfs_metric);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003067 if (ret)
3068 goto sysfs_error;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003069
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003070 perf->test_config.perf = perf;
3071 kref_init(&perf->test_config.ref);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003072
Robert Bragg19f81df2017-06-13 12:23:03 +01003073 goto exit;
3074
3075sysfs_error:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003076 kobject_put(perf->metrics_kobj);
3077 perf->metrics_kobj = NULL;
Robert Bragg19f81df2017-06-13 12:23:03 +01003078
Robert Bragg442b8c02016-11-07 19:49:53 +00003079exit:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003080 mutex_unlock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003081}
3082
Robert Bragg16d98b32016-12-07 21:40:33 +00003083/**
3084 * i915_perf_unregister - hide i915-perf from userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01003085 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003086 *
3087 * i915-perf state cleanup is split up into an 'unregister' and
3088 * 'deinit' phase where the interface is first hidden from
3089 * userspace by i915_perf_unregister() before cleaning up
3090 * remaining state in i915_perf_fini().
3091 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003092void i915_perf_unregister(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003093{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003094 struct i915_perf *perf = &i915->perf;
3095
3096 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003097 return;
3098
Chris Wilson8f8b1172019-10-07 22:09:41 +01003099 sysfs_remove_group(perf->metrics_kobj,
3100 &perf->test_config.sysfs_metric);
Robert Bragg442b8c02016-11-07 19:49:53 +00003101
Chris Wilson8f8b1172019-10-07 22:09:41 +01003102 kobject_put(perf->metrics_kobj);
3103 perf->metrics_kobj = NULL;
Robert Bragg442b8c02016-11-07 19:49:53 +00003104}
3105
Chris Wilson8f8b1172019-10-07 22:09:41 +01003106static bool gen8_is_valid_flex_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003107{
3108 static const i915_reg_t flex_eu_regs[] = {
3109 EU_PERF_CNTL0,
3110 EU_PERF_CNTL1,
3111 EU_PERF_CNTL2,
3112 EU_PERF_CNTL3,
3113 EU_PERF_CNTL4,
3114 EU_PERF_CNTL5,
3115 EU_PERF_CNTL6,
3116 };
3117 int i;
3118
3119 for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) {
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003120 if (i915_mmio_reg_offset(flex_eu_regs[i]) == addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003121 return true;
3122 }
3123 return false;
3124}
3125
Chris Wilson8f8b1172019-10-07 22:09:41 +01003126static bool gen7_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003127{
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003128 return (addr >= i915_mmio_reg_offset(OASTARTTRIG1) &&
3129 addr <= i915_mmio_reg_offset(OASTARTTRIG8)) ||
3130 (addr >= i915_mmio_reg_offset(OAREPORTTRIG1) &&
3131 addr <= i915_mmio_reg_offset(OAREPORTTRIG8)) ||
3132 (addr >= i915_mmio_reg_offset(OACEC0_0) &&
3133 addr <= i915_mmio_reg_offset(OACEC7_1));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003134}
3135
Chris Wilson8f8b1172019-10-07 22:09:41 +01003136static bool gen7_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003137{
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003138 return addr == i915_mmio_reg_offset(HALF_SLICE_CHICKEN2) ||
3139 (addr >= i915_mmio_reg_offset(MICRO_BP0_0) &&
3140 addr <= i915_mmio_reg_offset(NOA_WRITE)) ||
3141 (addr >= i915_mmio_reg_offset(OA_PERFCNT1_LO) &&
3142 addr <= i915_mmio_reg_offset(OA_PERFCNT2_HI)) ||
3143 (addr >= i915_mmio_reg_offset(OA_PERFMATRIX_LO) &&
3144 addr <= i915_mmio_reg_offset(OA_PERFMATRIX_HI));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003145}
3146
Chris Wilson8f8b1172019-10-07 22:09:41 +01003147static bool gen8_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003148{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003149 return gen7_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003150 addr == i915_mmio_reg_offset(WAIT_FOR_RC6_EXIT) ||
3151 (addr >= i915_mmio_reg_offset(RPM_CONFIG0) &&
3152 addr <= i915_mmio_reg_offset(NOA_CONFIG(8)));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003153}
3154
Chris Wilson8f8b1172019-10-07 22:09:41 +01003155static bool gen10_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003156{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003157 return gen8_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlinbf210f62019-06-02 01:58:45 +03003158 addr == i915_mmio_reg_offset(GEN10_NOA_WRITE_HIGH) ||
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003159 (addr >= i915_mmio_reg_offset(OA_PERFCNT3_LO) &&
3160 addr <= i915_mmio_reg_offset(OA_PERFCNT4_HI));
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003161}
3162
Chris Wilson8f8b1172019-10-07 22:09:41 +01003163static bool hsw_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003164{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003165 return gen7_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003166 (addr >= 0x25100 && addr <= 0x2FF90) ||
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003167 (addr >= i915_mmio_reg_offset(HSW_MBVID2_NOA0) &&
3168 addr <= i915_mmio_reg_offset(HSW_MBVID2_NOA9)) ||
3169 addr == i915_mmio_reg_offset(HSW_MBVID2_MISR0);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003170}
3171
Chris Wilson8f8b1172019-10-07 22:09:41 +01003172static bool chv_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003173{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003174 return gen7_is_valid_mux_addr(perf, addr) ||
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003175 (addr >= 0x182300 && addr <= 0x1823A4);
3176}
3177
Jani Nikula739f3ab2019-01-16 11:15:19 +02003178static u32 mask_reg_value(u32 reg, u32 val)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003179{
3180 /* HALF_SLICE_CHICKEN2 is programmed with a the
3181 * WaDisableSTUnitPowerOptimization workaround. Make sure the value
3182 * programmed by userspace doesn't change this.
3183 */
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003184 if (i915_mmio_reg_offset(HALF_SLICE_CHICKEN2) == reg)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003185 val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE);
3186
3187 /* WAIT_FOR_RC6_EXIT has only one bit fullfilling the function
3188 * indicated by its name and a bunch of selection fields used by OA
3189 * configs.
3190 */
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003191 if (i915_mmio_reg_offset(WAIT_FOR_RC6_EXIT) == reg)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003192 val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE);
3193
3194 return val;
3195}
3196
Chris Wilson8f8b1172019-10-07 22:09:41 +01003197static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,
3198 bool (*is_valid)(struct i915_perf *perf, u32 addr),
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003199 u32 __user *regs,
3200 u32 n_regs)
3201{
3202 struct i915_oa_reg *oa_regs;
3203 int err;
3204 u32 i;
3205
3206 if (!n_regs)
3207 return NULL;
3208
Linus Torvalds96d4f262019-01-03 18:57:57 -08003209 if (!access_ok(regs, n_regs * sizeof(u32) * 2))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003210 return ERR_PTR(-EFAULT);
3211
3212 /* No is_valid function means we're not allowing any register to be programmed. */
3213 GEM_BUG_ON(!is_valid);
3214 if (!is_valid)
3215 return ERR_PTR(-EINVAL);
3216
3217 oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL);
3218 if (!oa_regs)
3219 return ERR_PTR(-ENOMEM);
3220
3221 for (i = 0; i < n_regs; i++) {
3222 u32 addr, value;
3223
3224 err = get_user(addr, regs);
3225 if (err)
3226 goto addr_err;
3227
Chris Wilson8f8b1172019-10-07 22:09:41 +01003228 if (!is_valid(perf, addr)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003229 DRM_DEBUG("Invalid oa_reg address: %X\n", addr);
3230 err = -EINVAL;
3231 goto addr_err;
3232 }
3233
3234 err = get_user(value, regs + 1);
3235 if (err)
3236 goto addr_err;
3237
3238 oa_regs[i].addr = _MMIO(addr);
3239 oa_regs[i].value = mask_reg_value(addr, value);
3240
3241 regs += 2;
3242 }
3243
3244 return oa_regs;
3245
3246addr_err:
3247 kfree(oa_regs);
3248 return ERR_PTR(err);
3249}
3250
3251static ssize_t show_dynamic_id(struct device *dev,
3252 struct device_attribute *attr,
3253 char *buf)
3254{
3255 struct i915_oa_config *oa_config =
3256 container_of(attr, typeof(*oa_config), sysfs_metric_id);
3257
3258 return sprintf(buf, "%d\n", oa_config->id);
3259}
3260
Chris Wilson8f8b1172019-10-07 22:09:41 +01003261static int create_dynamic_oa_sysfs_entry(struct i915_perf *perf,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003262 struct i915_oa_config *oa_config)
3263{
Chris Wilson28152a22017-08-03 23:37:00 +01003264 sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003265 oa_config->sysfs_metric_id.attr.name = "id";
3266 oa_config->sysfs_metric_id.attr.mode = S_IRUGO;
3267 oa_config->sysfs_metric_id.show = show_dynamic_id;
3268 oa_config->sysfs_metric_id.store = NULL;
3269
3270 oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr;
3271 oa_config->attrs[1] = NULL;
3272
3273 oa_config->sysfs_metric.name = oa_config->uuid;
3274 oa_config->sysfs_metric.attrs = oa_config->attrs;
3275
Chris Wilson8f8b1172019-10-07 22:09:41 +01003276 return sysfs_create_group(perf->metrics_kobj,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003277 &oa_config->sysfs_metric);
3278}
3279
3280/**
3281 * i915_perf_add_config_ioctl - DRM ioctl() for userspace to add a new OA config
3282 * @dev: drm device
3283 * @data: ioctl data (pointer to struct drm_i915_perf_oa_config) copied from
3284 * userspace (unvalidated)
3285 * @file: drm file
3286 *
3287 * Validates the submitted OA register to be saved into a new OA config that
3288 * can then be used for programming the OA unit and its NOA network.
3289 *
3290 * Returns: A new allocated config number to be used with the perf open ioctl
3291 * or a negative error code on failure.
3292 */
3293int i915_perf_add_config_ioctl(struct drm_device *dev, void *data,
3294 struct drm_file *file)
3295{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003296 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003297 struct drm_i915_perf_oa_config *args = data;
3298 struct i915_oa_config *oa_config, *tmp;
3299 int err, id;
3300
Chris Wilson8f8b1172019-10-07 22:09:41 +01003301 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003302 DRM_DEBUG("i915 perf interface not available for this system\n");
3303 return -ENOTSUPP;
3304 }
3305
Chris Wilson8f8b1172019-10-07 22:09:41 +01003306 if (!perf->metrics_kobj) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003307 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
3308 return -EINVAL;
3309 }
3310
3311 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
3312 DRM_DEBUG("Insufficient privileges to add i915 OA config\n");
3313 return -EACCES;
3314 }
3315
3316 if ((!args->mux_regs_ptr || !args->n_mux_regs) &&
3317 (!args->boolean_regs_ptr || !args->n_boolean_regs) &&
3318 (!args->flex_regs_ptr || !args->n_flex_regs)) {
3319 DRM_DEBUG("No OA registers given\n");
3320 return -EINVAL;
3321 }
3322
3323 oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL);
3324 if (!oa_config) {
3325 DRM_DEBUG("Failed to allocate memory for the OA config\n");
3326 return -ENOMEM;
3327 }
3328
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003329 oa_config->perf = perf;
3330 kref_init(&oa_config->ref);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003331
3332 if (!uuid_is_valid(args->uuid)) {
3333 DRM_DEBUG("Invalid uuid format for OA config\n");
3334 err = -EINVAL;
3335 goto reg_err;
3336 }
3337
3338 /* Last character in oa_config->uuid will be 0 because oa_config is
3339 * kzalloc.
3340 */
3341 memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid));
3342
3343 oa_config->mux_regs_len = args->n_mux_regs;
3344 oa_config->mux_regs =
Chris Wilson8f8b1172019-10-07 22:09:41 +01003345 alloc_oa_regs(perf,
3346 perf->ops.is_valid_mux_reg,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003347 u64_to_user_ptr(args->mux_regs_ptr),
3348 args->n_mux_regs);
3349
3350 if (IS_ERR(oa_config->mux_regs)) {
3351 DRM_DEBUG("Failed to create OA config for mux_regs\n");
3352 err = PTR_ERR(oa_config->mux_regs);
3353 goto reg_err;
3354 }
3355
3356 oa_config->b_counter_regs_len = args->n_boolean_regs;
3357 oa_config->b_counter_regs =
Chris Wilson8f8b1172019-10-07 22:09:41 +01003358 alloc_oa_regs(perf,
3359 perf->ops.is_valid_b_counter_reg,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003360 u64_to_user_ptr(args->boolean_regs_ptr),
3361 args->n_boolean_regs);
3362
3363 if (IS_ERR(oa_config->b_counter_regs)) {
3364 DRM_DEBUG("Failed to create OA config for b_counter_regs\n");
3365 err = PTR_ERR(oa_config->b_counter_regs);
3366 goto reg_err;
3367 }
3368
Chris Wilson8f8b1172019-10-07 22:09:41 +01003369 if (INTEL_GEN(perf->i915) < 8) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003370 if (args->n_flex_regs != 0) {
3371 err = -EINVAL;
3372 goto reg_err;
3373 }
3374 } else {
3375 oa_config->flex_regs_len = args->n_flex_regs;
3376 oa_config->flex_regs =
Chris Wilson8f8b1172019-10-07 22:09:41 +01003377 alloc_oa_regs(perf,
3378 perf->ops.is_valid_flex_reg,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003379 u64_to_user_ptr(args->flex_regs_ptr),
3380 args->n_flex_regs);
3381
3382 if (IS_ERR(oa_config->flex_regs)) {
3383 DRM_DEBUG("Failed to create OA config for flex_regs\n");
3384 err = PTR_ERR(oa_config->flex_regs);
3385 goto reg_err;
3386 }
3387 }
3388
Chris Wilson8f8b1172019-10-07 22:09:41 +01003389 err = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003390 if (err)
3391 goto reg_err;
3392
3393 /* We shouldn't have too many configs, so this iteration shouldn't be
3394 * too costly.
3395 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003396 idr_for_each_entry(&perf->metrics_idr, tmp, id) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003397 if (!strcmp(tmp->uuid, oa_config->uuid)) {
3398 DRM_DEBUG("OA config already exists with this uuid\n");
3399 err = -EADDRINUSE;
3400 goto sysfs_err;
3401 }
3402 }
3403
Chris Wilson8f8b1172019-10-07 22:09:41 +01003404 err = create_dynamic_oa_sysfs_entry(perf, oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003405 if (err) {
3406 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
3407 goto sysfs_err;
3408 }
3409
3410 /* Config id 0 is invalid, id 1 for kernel stored test config. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003411 oa_config->id = idr_alloc(&perf->metrics_idr,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003412 oa_config, 2,
3413 0, GFP_KERNEL);
3414 if (oa_config->id < 0) {
3415 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
3416 err = oa_config->id;
3417 goto sysfs_err;
3418 }
3419
Chris Wilson8f8b1172019-10-07 22:09:41 +01003420 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003421
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01003422 DRM_DEBUG("Added config %s id=%i\n", oa_config->uuid, oa_config->id);
3423
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003424 return oa_config->id;
3425
3426sysfs_err:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003427 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003428reg_err:
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003429 i915_oa_config_put(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003430 DRM_DEBUG("Failed to add new OA config\n");
3431 return err;
3432}
3433
3434/**
3435 * i915_perf_remove_config_ioctl - DRM ioctl() for userspace to remove an OA config
3436 * @dev: drm device
3437 * @data: ioctl data (pointer to u64 integer) copied from userspace
3438 * @file: drm file
3439 *
3440 * Configs can be removed while being used, the will stop appearing in sysfs
3441 * and their content will be freed when the stream using the config is closed.
3442 *
3443 * Returns: 0 on success or a negative error code on failure.
3444 */
3445int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data,
3446 struct drm_file *file)
3447{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003448 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003449 u64 *arg = data;
3450 struct i915_oa_config *oa_config;
3451 int ret;
3452
Chris Wilson8f8b1172019-10-07 22:09:41 +01003453 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003454 DRM_DEBUG("i915 perf interface not available for this system\n");
3455 return -ENOTSUPP;
3456 }
3457
3458 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
3459 DRM_DEBUG("Insufficient privileges to remove i915 OA config\n");
3460 return -EACCES;
3461 }
3462
Chris Wilson8f8b1172019-10-07 22:09:41 +01003463 ret = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003464 if (ret)
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003465 return ret;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003466
Chris Wilson8f8b1172019-10-07 22:09:41 +01003467 oa_config = idr_find(&perf->metrics_idr, *arg);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003468 if (!oa_config) {
3469 DRM_DEBUG("Failed to remove unknown OA config\n");
3470 ret = -ENOENT;
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003471 goto err_unlock;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003472 }
3473
3474 GEM_BUG_ON(*arg != oa_config->id);
3475
Chris Wilson8f8b1172019-10-07 22:09:41 +01003476 sysfs_remove_group(perf->metrics_kobj,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003477 &oa_config->sysfs_metric);
3478
Chris Wilson8f8b1172019-10-07 22:09:41 +01003479 idr_remove(&perf->metrics_idr, *arg);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01003480
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003481 mutex_unlock(&perf->metrics_lock);
3482
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01003483 DRM_DEBUG("Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
3484
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003485 i915_oa_config_put(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003486
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003487 return 0;
3488
3489err_unlock:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003490 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003491 return ret;
3492}
3493
Robert Braggccdf6342016-11-07 19:49:54 +00003494static struct ctl_table oa_table[] = {
3495 {
3496 .procname = "perf_stream_paranoid",
3497 .data = &i915_perf_stream_paranoid,
3498 .maxlen = sizeof(i915_perf_stream_paranoid),
3499 .mode = 0644,
3500 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07003501 .extra1 = SYSCTL_ZERO,
3502 .extra2 = SYSCTL_ONE,
Robert Braggccdf6342016-11-07 19:49:54 +00003503 },
Robert Bragg00319ba2016-11-07 19:49:55 +00003504 {
3505 .procname = "oa_max_sample_rate",
3506 .data = &i915_oa_max_sample_rate,
3507 .maxlen = sizeof(i915_oa_max_sample_rate),
3508 .mode = 0644,
3509 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07003510 .extra1 = SYSCTL_ZERO,
Robert Bragg00319ba2016-11-07 19:49:55 +00003511 .extra2 = &oa_sample_rate_hard_limit,
3512 },
Robert Braggccdf6342016-11-07 19:49:54 +00003513 {}
3514};
3515
3516static struct ctl_table i915_root[] = {
3517 {
3518 .procname = "i915",
3519 .maxlen = 0,
3520 .mode = 0555,
3521 .child = oa_table,
3522 },
3523 {}
3524};
3525
3526static struct ctl_table dev_root[] = {
3527 {
3528 .procname = "dev",
3529 .maxlen = 0,
3530 .mode = 0555,
3531 .child = i915_root,
3532 },
3533 {}
3534};
3535
Robert Bragg16d98b32016-12-07 21:40:33 +00003536/**
3537 * i915_perf_init - initialize i915-perf state on module load
Chris Wilson8f8b1172019-10-07 22:09:41 +01003538 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003539 *
3540 * Initializes i915-perf state without exposing anything to userspace.
3541 *
3542 * Note: i915-perf initialization is split into an 'init' and 'register'
3543 * phase with the i915_perf_register() exposing state to userspace.
3544 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003545void i915_perf_init(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00003546{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003547 struct i915_perf *perf = &i915->perf;
Robert Braggd7965152016-11-07 19:49:52 +00003548
Chris Wilson8f8b1172019-10-07 22:09:41 +01003549 /* XXX const struct i915_perf_ops! */
3550
3551 if (IS_HASWELL(i915)) {
3552 perf->ops.is_valid_b_counter_reg = gen7_is_valid_b_counter_addr;
3553 perf->ops.is_valid_mux_reg = hsw_is_valid_mux_addr;
3554 perf->ops.is_valid_flex_reg = NULL;
3555 perf->ops.enable_metric_set = hsw_enable_metric_set;
3556 perf->ops.disable_metric_set = hsw_disable_metric_set;
3557 perf->ops.oa_enable = gen7_oa_enable;
3558 perf->ops.oa_disable = gen7_oa_disable;
3559 perf->ops.read = gen7_oa_read;
3560 perf->ops.oa_hw_tail_read = gen7_oa_hw_tail_read;
3561
3562 perf->oa_formats = hsw_oa_formats;
3563 } else if (HAS_LOGICAL_RING_CONTEXTS(i915)) {
Robert Bragg19f81df2017-06-13 12:23:03 +01003564 /* Note: that although we could theoretically also support the
3565 * legacy ringbuffer mode on BDW (and earlier iterations of
3566 * this driver, before upstreaming did this) it didn't seem
3567 * worth the complexity to maintain now that BDW+ enable
3568 * execlist mode by default.
3569 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003570 perf->oa_formats = gen8_plus_oa_formats;
Robert Braggd7965152016-11-07 19:49:52 +00003571
Chris Wilson8f8b1172019-10-07 22:09:41 +01003572 perf->ops.oa_enable = gen8_oa_enable;
3573 perf->ops.oa_disable = gen8_oa_disable;
3574 perf->ops.read = gen8_oa_read;
3575 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003576
Chris Wilson8f8b1172019-10-07 22:09:41 +01003577 if (IS_GEN_RANGE(i915, 8, 9)) {
3578 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003579 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003580 perf->ops.is_valid_mux_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003581 gen8_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003582 perf->ops.is_valid_flex_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003583 gen8_is_valid_flex_addr;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003584
Chris Wilson8f8b1172019-10-07 22:09:41 +01003585 if (IS_CHERRYVIEW(i915)) {
3586 perf->ops.is_valid_mux_reg =
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003587 chv_is_valid_mux_addr;
3588 }
Robert Bragg155e9412017-06-13 12:23:05 +01003589
Chris Wilson8f8b1172019-10-07 22:09:41 +01003590 perf->ops.enable_metric_set = gen8_enable_metric_set;
3591 perf->ops.disable_metric_set = gen8_disable_metric_set;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003592
Chris Wilson8f8b1172019-10-07 22:09:41 +01003593 if (IS_GEN(i915, 8)) {
3594 perf->ctx_oactxctrl_offset = 0x120;
3595 perf->ctx_flexeu0_offset = 0x2ce;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003596
Chris Wilson8f8b1172019-10-07 22:09:41 +01003597 perf->gen8_valid_ctx_bit = BIT(25);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003598 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01003599 perf->ctx_oactxctrl_offset = 0x128;
3600 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003601
Chris Wilson8f8b1172019-10-07 22:09:41 +01003602 perf->gen8_valid_ctx_bit = BIT(16);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00003603 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01003604 } else if (IS_GEN_RANGE(i915, 10, 11)) {
3605 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003606 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003607 perf->ops.is_valid_mux_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003608 gen10_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003609 perf->ops.is_valid_flex_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003610 gen8_is_valid_flex_addr;
3611
Chris Wilson8f8b1172019-10-07 22:09:41 +01003612 perf->ops.enable_metric_set = gen8_enable_metric_set;
3613 perf->ops.disable_metric_set = gen10_disable_metric_set;
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003614
Chris Wilson8f8b1172019-10-07 22:09:41 +01003615 if (IS_GEN(i915, 10)) {
3616 perf->ctx_oactxctrl_offset = 0x128;
3617 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03003618 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01003619 perf->ctx_oactxctrl_offset = 0x124;
3620 perf->ctx_flexeu0_offset = 0x78e;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03003621 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01003622 perf->gen8_valid_ctx_bit = BIT(16);
Robert Bragg19f81df2017-06-13 12:23:03 +01003623 }
Robert Bragg19f81df2017-06-13 12:23:03 +01003624 }
3625
Chris Wilson8f8b1172019-10-07 22:09:41 +01003626 if (perf->ops.enable_metric_set) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01003627 mutex_init(&perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01003628
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01003629 oa_sample_rate_hard_limit = 1000 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003630 (RUNTIME_INFO(i915)->cs_timestamp_frequency_khz / 2);
3631 perf->sysctl_header = register_sysctl_table(dev_root);
Robert Bragg19f81df2017-06-13 12:23:03 +01003632
Chris Wilson8f8b1172019-10-07 22:09:41 +01003633 mutex_init(&perf->metrics_lock);
3634 idr_init(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003635
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003636 /* We set up some ratelimit state to potentially throttle any
3637 * _NOTES about spurious, invalid OA reports which we don't
3638 * forward to userspace.
3639 *
3640 * We print a _NOTE about any throttling when closing the
3641 * stream instead of waiting until driver _fini which no one
3642 * would ever see.
3643 *
3644 * Using the same limiting factors as printk_ratelimit()
3645 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003646 ratelimit_state_init(&perf->spurious_report_rs, 5 * HZ, 10);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003647 /* Since we use a DRM_NOTE for spurious reports it would be
3648 * inconsistent to let __ratelimit() automatically print a
3649 * warning for throttling.
3650 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003651 ratelimit_set_flags(&perf->spurious_report_rs,
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003652 RATELIMIT_MSG_ON_RELEASE);
3653
Chris Wilson8f8b1172019-10-07 22:09:41 +01003654 perf->i915 = i915;
Robert Bragg19f81df2017-06-13 12:23:03 +01003655 }
Robert Braggeec688e2016-11-07 19:49:47 +00003656}
3657
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003658static int destroy_config(int id, void *p, void *data)
3659{
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003660 i915_oa_config_put(p);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003661 return 0;
3662}
3663
Robert Bragg16d98b32016-12-07 21:40:33 +00003664/**
3665 * i915_perf_fini - Counter part to i915_perf_init()
Chris Wilson8f8b1172019-10-07 22:09:41 +01003666 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003667 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003668void i915_perf_fini(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00003669{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003670 struct i915_perf *perf = &i915->perf;
3671
3672 if (!perf->i915)
Robert Braggeec688e2016-11-07 19:49:47 +00003673 return;
3674
Chris Wilson8f8b1172019-10-07 22:09:41 +01003675 idr_for_each(&perf->metrics_idr, destroy_config, perf);
3676 idr_destroy(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003677
Chris Wilson8f8b1172019-10-07 22:09:41 +01003678 unregister_sysctl_table(perf->sysctl_header);
Robert Braggccdf6342016-11-07 19:49:54 +00003679
Chris Wilson8f8b1172019-10-07 22:09:41 +01003680 memset(&perf->ops, 0, sizeof(perf->ops));
3681 perf->i915 = NULL;
Robert Braggeec688e2016-11-07 19:49:47 +00003682}