blob: a8c2318d3d5edfd2611a9f516511ee25b808dbc3 [file] [log] [blame]
Robert Braggeec688e2016-11-07 19:49:47 +00001/*
2 * Copyright © 2015-2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Robert Bragg <robert@sixbynine.org>
25 */
26
Robert Bragg7abbd8d2016-11-07 19:49:57 +000027
28/**
Robert Bragg16d98b32016-12-07 21:40:33 +000029 * DOC: i915 Perf Overview
Robert Bragg7abbd8d2016-11-07 19:49:57 +000030 *
31 * Gen graphics supports a large number of performance counters that can help
32 * driver and application developers understand and optimize their use of the
33 * GPU.
34 *
35 * This i915 perf interface enables userspace to configure and open a file
36 * descriptor representing a stream of GPU metrics which can then be read() as
37 * a stream of sample records.
38 *
39 * The interface is particularly suited to exposing buffered metrics that are
40 * captured by DMA from the GPU, unsynchronized with and unrelated to the CPU.
41 *
42 * Streams representing a single context are accessible to applications with a
43 * corresponding drm file descriptor, such that OpenGL can use the interface
44 * without special privileges. Access to system-wide metrics requires root
45 * privileges by default, unless changed via the dev.i915.perf_event_paranoid
46 * sysctl option.
47 *
Robert Bragg16d98b32016-12-07 21:40:33 +000048 */
49
50/**
51 * DOC: i915 Perf History and Comparison with Core Perf
Robert Bragg7abbd8d2016-11-07 19:49:57 +000052 *
53 * The interface was initially inspired by the core Perf infrastructure but
54 * some notable differences are:
55 *
56 * i915 perf file descriptors represent a "stream" instead of an "event"; where
57 * a perf event primarily corresponds to a single 64bit value, while a stream
58 * might sample sets of tightly-coupled counters, depending on the
59 * configuration. For example the Gen OA unit isn't designed to support
60 * orthogonal configurations of individual counters; it's configured for a set
61 * of related counters. Samples for an i915 perf stream capturing OA metrics
62 * will include a set of counter values packed in a compact HW specific format.
63 * The OA unit supports a number of different packing formats which can be
64 * selected by the user opening the stream. Perf has support for grouping
65 * events, but each event in the group is configured, validated and
66 * authenticated individually with separate system calls.
67 *
68 * i915 perf stream configurations are provided as an array of u64 (key,value)
69 * pairs, instead of a fixed struct with multiple miscellaneous config members,
70 * interleaved with event-type specific members.
71 *
72 * i915 perf doesn't support exposing metrics via an mmap'd circular buffer.
73 * The supported metrics are being written to memory by the GPU unsynchronized
74 * with the CPU, using HW specific packing formats for counter sets. Sometimes
75 * the constraints on HW configuration require reports to be filtered before it
76 * would be acceptable to expose them to unprivileged applications - to hide
77 * the metrics of other processes/contexts. For these use cases a read() based
78 * interface is a good fit, and provides an opportunity to filter data as it
79 * gets copied from the GPU mapped buffers to userspace buffers.
80 *
81 *
Robert Bragg16d98b32016-12-07 21:40:33 +000082 * Issues hit with first prototype based on Core Perf
83 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Robert Bragg7abbd8d2016-11-07 19:49:57 +000084 *
85 * The first prototype of this driver was based on the core perf
86 * infrastructure, and while we did make that mostly work, with some changes to
87 * perf, we found we were breaking or working around too many assumptions baked
88 * into perf's currently cpu centric design.
89 *
90 * In the end we didn't see a clear benefit to making perf's implementation and
91 * interface more complex by changing design assumptions while we knew we still
92 * wouldn't be able to use any existing perf based userspace tools.
93 *
94 * Also considering the Gen specific nature of the Observability hardware and
95 * how userspace will sometimes need to combine i915 perf OA metrics with
96 * side-band OA data captured via MI_REPORT_PERF_COUNT commands; we're
97 * expecting the interface to be used by a platform specific userspace such as
98 * OpenGL or tools. This is to say; we aren't inherently missing out on having
99 * a standard vendor/architecture agnostic interface by not using perf.
100 *
101 *
102 * For posterity, in case we might re-visit trying to adapt core perf to be
103 * better suited to exposing i915 metrics these were the main pain points we
104 * hit:
105 *
106 * - The perf based OA PMU driver broke some significant design assumptions:
107 *
108 * Existing perf pmus are used for profiling work on a cpu and we were
109 * introducing the idea of _IS_DEVICE pmus with different security
110 * implications, the need to fake cpu-related data (such as user/kernel
111 * registers) to fit with perf's current design, and adding _DEVICE records
112 * as a way to forward device-specific status records.
113 *
114 * The OA unit writes reports of counters into a circular buffer, without
115 * involvement from the CPU, making our PMU driver the first of a kind.
116 *
117 * Given the way we were periodically forward data from the GPU-mapped, OA
118 * buffer to perf's buffer, those bursts of sample writes looked to perf like
119 * we were sampling too fast and so we had to subvert its throttling checks.
120 *
121 * Perf supports groups of counters and allows those to be read via
122 * transactions internally but transactions currently seem designed to be
123 * explicitly initiated from the cpu (say in response to a userspace read())
124 * and while we could pull a report out of the OA buffer we can't
125 * trigger a report from the cpu on demand.
126 *
127 * Related to being report based; the OA counters are configured in HW as a
128 * set while perf generally expects counter configurations to be orthogonal.
129 * Although counters can be associated with a group leader as they are
130 * opened, there's no clear precedent for being able to provide group-wide
131 * configuration attributes (for example we want to let userspace choose the
132 * OA unit report format used to capture all counters in a set, or specify a
133 * GPU context to filter metrics on). We avoided using perf's grouping
134 * feature and forwarded OA reports to userspace via perf's 'raw' sample
135 * field. This suited our userspace well considering how coupled the counters
136 * are when dealing with normalizing. It would be inconvenient to split
137 * counters up into separate events, only to require userspace to recombine
138 * them. For Mesa it's also convenient to be forwarded raw, periodic reports
139 * for combining with the side-band raw reports it captures using
140 * MI_REPORT_PERF_COUNT commands.
141 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000142 * - As a side note on perf's grouping feature; there was also some concern
Robert Bragg7abbd8d2016-11-07 19:49:57 +0000143 * that using PERF_FORMAT_GROUP as a way to pack together counter values
144 * would quite drastically inflate our sample sizes, which would likely
145 * lower the effective sampling resolutions we could use when the available
146 * memory bandwidth is limited.
147 *
148 * With the OA unit's report formats, counters are packed together as 32
149 * or 40bit values, with the largest report size being 256 bytes.
150 *
151 * PERF_FORMAT_GROUP values are 64bit, but there doesn't appear to be a
152 * documented ordering to the values, implying PERF_FORMAT_ID must also be
153 * used to add a 64bit ID before each value; giving 16 bytes per counter.
154 *
155 * Related to counter orthogonality; we can't time share the OA unit, while
156 * event scheduling is a central design idea within perf for allowing
157 * userspace to open + enable more events than can be configured in HW at any
158 * one time. The OA unit is not designed to allow re-configuration while in
159 * use. We can't reconfigure the OA unit without losing internal OA unit
160 * state which we can't access explicitly to save and restore. Reconfiguring
161 * the OA unit is also relatively slow, involving ~100 register writes. From
162 * userspace Mesa also depends on a stable OA configuration when emitting
163 * MI_REPORT_PERF_COUNT commands and importantly the OA unit can't be
164 * disabled while there are outstanding MI_RPC commands lest we hang the
165 * command streamer.
166 *
167 * The contents of sample records aren't extensible by device drivers (i.e.
168 * the sample_type bits). As an example; Sourab Gupta had been looking to
169 * attach GPU timestamps to our OA samples. We were shoehorning OA reports
170 * into sample records by using the 'raw' field, but it's tricky to pack more
171 * than one thing into this field because events/core.c currently only lets a
172 * pmu give a single raw data pointer plus len which will be copied into the
173 * ring buffer. To include more than the OA report we'd have to copy the
174 * report into an intermediate larger buffer. I'd been considering allowing a
175 * vector of data+len values to be specified for copying the raw data, but
176 * it felt like a kludge to being using the raw field for this purpose.
177 *
178 * - It felt like our perf based PMU was making some technical compromises
179 * just for the sake of using perf:
180 *
181 * perf_event_open() requires events to either relate to a pid or a specific
182 * cpu core, while our device pmu related to neither. Events opened with a
183 * pid will be automatically enabled/disabled according to the scheduling of
184 * that process - so not appropriate for us. When an event is related to a
185 * cpu id, perf ensures pmu methods will be invoked via an inter process
186 * interrupt on that core. To avoid invasive changes our userspace opened OA
187 * perf events for a specific cpu. This was workable but it meant the
188 * majority of the OA driver ran in atomic context, including all OA report
189 * forwarding, which wasn't really necessary in our case and seems to make
190 * our locking requirements somewhat complex as we handled the interaction
191 * with the rest of the i915 driver.
192 */
193
Robert Braggeec688e2016-11-07 19:49:47 +0000194#include <linux/anon_inodes.h>
Robert Braggd7965152016-11-07 19:49:52 +0000195#include <linux/sizes.h>
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100196#include <linux/uuid.h>
Robert Braggeec688e2016-11-07 19:49:47 +0000197
Chris Wilson10be98a2019-05-28 10:29:49 +0100198#include "gem/i915_gem_context.h"
Chris Wilsona5efcde2019-10-11 20:03:17 +0100199#include "gt/intel_engine_pm.h"
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100200#include "gt/intel_engine_user.h"
Lionel Landwerlindaed3e42019-10-12 08:23:07 +0100201#include "gt/intel_gt.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +0100202#include "gt/intel_lrc_reg.h"
Chris Wilson2871ea82019-10-24 11:03:44 +0100203#include "gt/intel_ring.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +0100204
Robert Braggeec688e2016-11-07 19:49:47 +0000205#include "i915_drv.h"
Jani Nikuladb94e9f2019-08-08 16:42:44 +0300206#include "i915_perf.h"
Michal Wajdeczko5ed7a0c2019-06-26 12:38:26 +0000207#include "oa/i915_oa_hsw.h"
208#include "oa/i915_oa_bdw.h"
209#include "oa/i915_oa_chv.h"
210#include "oa/i915_oa_sklgt2.h"
211#include "oa/i915_oa_sklgt3.h"
212#include "oa/i915_oa_sklgt4.h"
213#include "oa/i915_oa_bxt.h"
214#include "oa/i915_oa_kblgt2.h"
215#include "oa/i915_oa_kblgt3.h"
216#include "oa/i915_oa_glk.h"
217#include "oa/i915_oa_cflgt2.h"
218#include "oa/i915_oa_cflgt3.h"
219#include "oa/i915_oa_cnl.h"
220#include "oa/i915_oa_icl.h"
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700221#include "oa/i915_oa_tgl.h"
Robert Braggd7965152016-11-07 19:49:52 +0000222
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200223/* HW requires this to be a power of two, between 128k and 16M, though driver
224 * is currently generally designed assuming the largest 16M size is used such
225 * that the overflow cases are unlikely in normal operation.
226 */
227#define OA_BUFFER_SIZE SZ_16M
228
229#define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
Robert Braggd7965152016-11-07 19:49:52 +0000230
Robert Bragg0dd860c2017-05-11 16:43:28 +0100231/**
232 * DOC: OA Tail Pointer Race
233 *
234 * There's a HW race condition between OA unit tail pointer register updates and
Robert Braggd7965152016-11-07 19:49:52 +0000235 * writes to memory whereby the tail pointer can sometimes get ahead of what's
Robert Bragg0dd860c2017-05-11 16:43:28 +0100236 * been written out to the OA buffer so far (in terms of what's visible to the
237 * CPU).
Robert Braggd7965152016-11-07 19:49:52 +0000238 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100239 * Although this can be observed explicitly while copying reports to userspace
240 * by checking for a zeroed report-id field in tail reports, we want to account
Robert Bragg19f81df2017-06-13 12:23:03 +0100241 * for this earlier, as part of the oa_buffer_check to avoid lots of redundant
Robert Bragg0dd860c2017-05-11 16:43:28 +0100242 * read() attempts.
Robert Braggd7965152016-11-07 19:49:52 +0000243 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100244 * In effect we define a tail pointer for reading that lags the real tail
245 * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough
246 * time for the corresponding reports to become visible to the CPU.
Robert Braggd7965152016-11-07 19:49:52 +0000247 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100248 * To manage this we actually track two tail pointers:
249 * 1) An 'aging' tail with an associated timestamp that is tracked until we
250 * can trust the corresponding data is visible to the CPU; at which point
251 * it is considered 'aged'.
252 * 2) An 'aged' tail that can be used for read()ing.
253 *
254 * The two separate pointers let us decouple read()s from tail pointer aging.
255 *
256 * The tail pointers are checked and updated at a limited rate within a hrtimer
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800257 * callback (the same callback that is used for delivering EPOLLIN events)
Robert Bragg0dd860c2017-05-11 16:43:28 +0100258 *
259 * Initially the tails are marked invalid with %INVALID_TAIL_PTR which
260 * indicates that an updated tail pointer is needed.
261 *
262 * Most of the implementation details for this workaround are in
Robert Bragg19f81df2017-06-13 12:23:03 +0100263 * oa_buffer_check_unlocked() and _append_oa_reports()
Robert Bragg0dd860c2017-05-11 16:43:28 +0100264 *
265 * Note for posterity: previously the driver used to define an effective tail
266 * pointer that lagged the real pointer by a 'tail margin' measured in bytes
267 * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
268 * This was flawed considering that the OA unit may also automatically generate
269 * non-periodic reports (such as on context switch) or the OA unit may be
270 * enabled without any periodic sampling.
Robert Braggd7965152016-11-07 19:49:52 +0000271 */
272#define OA_TAIL_MARGIN_NSEC 100000ULL
Robert Bragg0dd860c2017-05-11 16:43:28 +0100273#define INVALID_TAIL_PTR 0xffffffff
Robert Braggd7965152016-11-07 19:49:52 +0000274
275/* frequency for checking whether the OA unit has written new reports to the
276 * circular OA buffer...
277 */
278#define POLL_FREQUENCY 200
279#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
280
Robert Braggccdf6342016-11-07 19:49:54 +0000281/* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
Robert Braggccdf6342016-11-07 19:49:54 +0000282static u32 i915_perf_stream_paranoid = true;
283
Robert Braggd7965152016-11-07 19:49:52 +0000284/* The maximum exponent the hardware accepts is 63 (essentially it selects one
285 * of the 64bit timestamp bits to trigger reports from) but there's currently
286 * no known use case for sampling as infrequently as once per 47 thousand years.
287 *
288 * Since the timestamps included in OA reports are only 32bits it seems
289 * reasonable to limit the OA exponent where it's still possible to account for
290 * overflow in OA report timestamps.
291 */
292#define OA_EXPONENT_MAX 31
293
294#define INVALID_CTX_ID 0xffffffff
295
Robert Bragg19f81df2017-06-13 12:23:03 +0100296/* On Gen8+ automatically triggered OA reports include a 'reason' field... */
297#define OAREPORT_REASON_MASK 0x3f
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700298#define OAREPORT_REASON_MASK_EXTENDED 0x7f
Robert Bragg19f81df2017-06-13 12:23:03 +0100299#define OAREPORT_REASON_SHIFT 19
300#define OAREPORT_REASON_TIMER (1<<0)
301#define OAREPORT_REASON_CTX_SWITCH (1<<3)
302#define OAREPORT_REASON_CLK_RATIO (1<<5)
303
Robert Braggd7965152016-11-07 19:49:52 +0000304
Robert Bragg00319ba2016-11-07 19:49:55 +0000305/* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
306 *
Robert Bragg155e9412017-06-13 12:23:05 +0100307 * The highest sampling frequency we can theoretically program the OA unit
308 * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
309 *
310 * Initialized just before we register the sysctl parameter.
Robert Bragg00319ba2016-11-07 19:49:55 +0000311 */
Robert Bragg155e9412017-06-13 12:23:05 +0100312static int oa_sample_rate_hard_limit;
Robert Bragg00319ba2016-11-07 19:49:55 +0000313
314/* Theoretically we can program the OA unit to sample every 160ns but don't
315 * allow that by default unless root...
316 *
317 * The default threshold of 100000Hz is based on perf's similar
318 * kernel.perf_event_max_sample_rate sysctl parameter.
319 */
320static u32 i915_oa_max_sample_rate = 100000;
321
Robert Braggd7965152016-11-07 19:49:52 +0000322/* XXX: beware if future OA HW adds new report formats that the current
323 * code assumes all reports have a power-of-two size and ~(size - 1) can
324 * be used as a mask to align the OA tail pointer.
325 */
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300326static const struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Braggd7965152016-11-07 19:49:52 +0000327 [I915_OA_FORMAT_A13] = { 0, 64 },
328 [I915_OA_FORMAT_A29] = { 1, 128 },
329 [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
330 /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
331 [I915_OA_FORMAT_B4_C8] = { 4, 64 },
332 [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
333 [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
334 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
335};
336
Jani Nikula6ebb6d82018-06-13 14:49:29 +0300337static const struct i915_oa_format gen8_plus_oa_formats[I915_OA_FORMAT_MAX] = {
Robert Bragg19f81df2017-06-13 12:23:03 +0100338 [I915_OA_FORMAT_A12] = { 0, 64 },
339 [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
340 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
341 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
342};
343
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700344static const struct i915_oa_format gen12_oa_formats[I915_OA_FORMAT_MAX] = {
345 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
346};
347
Robert Braggd7965152016-11-07 19:49:52 +0000348#define SAMPLE_OA_REPORT (1<<0)
Robert Braggeec688e2016-11-07 19:49:47 +0000349
Robert Bragg16d98b32016-12-07 21:40:33 +0000350/**
351 * struct perf_open_properties - for validated properties given to open a stream
352 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
353 * @single_context: Whether a single or all gpu contexts should be monitored
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +0100354 * @hold_preemption: Whether the preemption is disabled for the filtered
355 * context
Robert Bragg16d98b32016-12-07 21:40:33 +0000356 * @ctx_handle: A gem ctx handle for use with @single_context
357 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
358 * @oa_format: An OA unit HW report format
359 * @oa_periodic: Whether to enable periodic OA unit sampling
360 * @oa_period_exponent: The OA unit sampling period is derived from this
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100361 * @engine: The engine (typically rcs0) being monitored by the OA unit
Robert Bragg16d98b32016-12-07 21:40:33 +0000362 *
363 * As read_properties_unlocked() enumerates and validates the properties given
364 * to open a stream of metrics the configuration is built up in the structure
365 * which starts out zero initialized.
366 */
Robert Braggeec688e2016-11-07 19:49:47 +0000367struct perf_open_properties {
368 u32 sample_flags;
369
370 u64 single_context:1;
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +0100371 u64 hold_preemption:1;
Robert Braggeec688e2016-11-07 19:49:47 +0000372 u64 ctx_handle;
Robert Braggd7965152016-11-07 19:49:52 +0000373
374 /* OA sampling state */
375 int metrics_set;
376 int oa_format;
377 bool oa_periodic;
378 int oa_period_exponent;
Lionel Landwerlin9a613632019-10-10 16:05:19 +0100379
380 struct intel_engine_cs *engine;
Robert Braggeec688e2016-11-07 19:49:47 +0000381};
382
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100383struct i915_oa_config_bo {
384 struct llist_node node;
385
386 struct i915_oa_config *oa_config;
387 struct i915_vma *vma;
388};
389
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700390static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer);
391
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100392void i915_oa_config_release(struct kref *ref)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100393{
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100394 struct i915_oa_config *oa_config =
395 container_of(ref, typeof(*oa_config), ref);
396
Chris Wilsonc2fba932019-10-13 10:52:11 +0100397 kfree(oa_config->flex_regs);
398 kfree(oa_config->b_counter_regs);
399 kfree(oa_config->mux_regs);
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100400
401 kfree_rcu(oa_config, rcu);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100402}
403
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100404struct i915_oa_config *
405i915_perf_get_oa_config(struct i915_perf *perf, int metrics_set)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100406{
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100407 struct i915_oa_config *oa_config;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100408
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100409 rcu_read_lock();
410 if (metrics_set == 1)
411 oa_config = &perf->test_config;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100412 else
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100413 oa_config = idr_find(&perf->metrics_idr, metrics_set);
414 if (oa_config)
415 oa_config = i915_oa_config_get(oa_config);
416 rcu_read_unlock();
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100417
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100418 return oa_config;
419}
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100420
Lionel Landwerlin6a450082019-10-12 08:23:06 +0100421static void free_oa_config_bo(struct i915_oa_config_bo *oa_bo)
422{
423 i915_oa_config_put(oa_bo->oa_config);
424 i915_vma_put(oa_bo->vma);
425 kfree(oa_bo);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +0100426}
427
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700428static u32 gen12_oa_hw_tail_read(struct i915_perf_stream *stream)
429{
430 struct intel_uncore *uncore = stream->uncore;
431
432 return intel_uncore_read(uncore, GEN12_OAG_OATAILPTR) &
433 GEN12_OAG_OATAILPTR_MASK;
434}
435
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700436static u32 gen8_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100437{
Chris Wilson52111c42019-10-10 16:05:20 +0100438 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700439
Chris Wilson8f8b1172019-10-07 22:09:41 +0100440 return intel_uncore_read(uncore, GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
Robert Bragg19f81df2017-06-13 12:23:03 +0100441}
442
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700443static u32 gen7_oa_hw_tail_read(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +0100444{
Chris Wilson52111c42019-10-10 16:05:20 +0100445 struct intel_uncore *uncore = stream->uncore;
Chris Wilson8f8b1172019-10-07 22:09:41 +0100446 u32 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100447
448 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
449}
450
Robert Bragg0dd860c2017-05-11 16:43:28 +0100451/**
Robert Bragg19f81df2017-06-13 12:23:03 +0100452 * oa_buffer_check_unlocked - check for data and update tail ptr state
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700453 * @stream: i915 stream instance
Robert Braggd7965152016-11-07 19:49:52 +0000454 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100455 * This is either called via fops (for blocking reads in user ctx) or the poll
456 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
457 * if there is data available for userspace to read.
Robert Braggd7965152016-11-07 19:49:52 +0000458 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100459 * This function is central to providing a workaround for the OA unit tail
460 * pointer having a race with respect to what data is visible to the CPU.
461 * It is responsible for reading tail pointers from the hardware and giving
462 * the pointers time to 'age' before they are made available for reading.
463 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
464 *
465 * Besides returning true when there is data available to read() this function
466 * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp
467 * and .aged_tail_idx state used for reading.
468 *
469 * Note: It's safe to read OA config state here unlocked, assuming that this is
470 * only called while the stream is enabled, while the global OA configuration
471 * can't be modified.
472 *
473 * Returns: %true if the OA buffer contains data, else %false
Robert Braggd7965152016-11-07 19:49:52 +0000474 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700475static bool oa_buffer_check_unlocked(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +0000476{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700477 int report_size = stream->oa_buffer.format_size;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100478 unsigned long flags;
479 unsigned int aged_idx;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100480 u32 head, hw_tail, aged_tail, aging_tail;
481 u64 now;
Robert Braggd7965152016-11-07 19:49:52 +0000482
Robert Bragg0dd860c2017-05-11 16:43:28 +0100483 /* We have to consider the (unlikely) possibility that read() errors
484 * could result in an OA buffer reset which might reset the head,
485 * tails[] and aged_tail state.
486 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700487 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100488
489 /* NB: The head we observe here might effectively be a little out of
490 * date (between head and tails[aged_idx].offset if there is currently
491 * a read() in progress.
492 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700493 head = stream->oa_buffer.head;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100494
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700495 aged_idx = stream->oa_buffer.aged_tail_idx;
496 aged_tail = stream->oa_buffer.tails[aged_idx].offset;
497 aging_tail = stream->oa_buffer.tails[!aged_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100498
Chris Wilson8f8b1172019-10-07 22:09:41 +0100499 hw_tail = stream->perf->ops.oa_hw_tail_read(stream);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100500
501 /* The tail pointer increases in 64 byte increments,
502 * not in report_size steps...
503 */
504 hw_tail &= ~(report_size - 1);
505
506 now = ktime_get_mono_fast_ns();
507
Robert Bragg4117ebc2017-05-11 16:43:30 +0100508 /* Update the aged tail
509 *
510 * Flip the tail pointer available for read()s once the aging tail is
511 * old enough to trust that the corresponding data will be visible to
512 * the CPU...
513 *
514 * Do this before updating the aging pointer in case we may be able to
515 * immediately start aging a new pointer too (if new data has become
516 * available) without needing to wait for a later hrtimer callback.
517 */
518 if (aging_tail != INVALID_TAIL_PTR &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700519 ((now - stream->oa_buffer.aging_timestamp) >
Robert Bragg4117ebc2017-05-11 16:43:30 +0100520 OA_TAIL_MARGIN_NSEC)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100521
Robert Bragg4117ebc2017-05-11 16:43:30 +0100522 aged_idx ^= 1;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700523 stream->oa_buffer.aged_tail_idx = aged_idx;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100524
525 aged_tail = aging_tail;
526
527 /* Mark that we need a new pointer to start aging... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700528 stream->oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR;
Robert Bragg4117ebc2017-05-11 16:43:30 +0100529 aging_tail = INVALID_TAIL_PTR;
530 }
531
Robert Bragg0dd860c2017-05-11 16:43:28 +0100532 /* Update the aging tail
533 *
534 * We throttle aging tail updates until we have a new tail that
535 * represents >= one report more data than is already available for
536 * reading. This ensures there will be enough data for a successful
537 * read once this new pointer has aged and ensures we will give the new
538 * pointer time to age.
539 */
540 if (aging_tail == INVALID_TAIL_PTR &&
541 (aged_tail == INVALID_TAIL_PTR ||
542 OA_TAKEN(hw_tail, aged_tail) >= report_size)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700543 struct i915_vma *vma = stream->oa_buffer.vma;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100544 u32 gtt_offset = i915_ggtt_offset(vma);
545
546 /* Be paranoid and do a bounds check on the pointer read back
547 * from hardware, just in case some spurious hardware condition
548 * could put the tail out of bounds...
549 */
550 if (hw_tail >= gtt_offset &&
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200551 hw_tail < (gtt_offset + OA_BUFFER_SIZE)) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700552 stream->oa_buffer.tails[!aged_idx].offset =
Robert Bragg0dd860c2017-05-11 16:43:28 +0100553 aging_tail = hw_tail;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700554 stream->oa_buffer.aging_timestamp = now;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100555 } else {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700556 DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %x\n",
Robert Bragg0dd860c2017-05-11 16:43:28 +0100557 hw_tail);
558 }
559 }
560
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700561 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100562
563 return aged_tail == INVALID_TAIL_PTR ?
564 false : OA_TAKEN(aged_tail, head) >= report_size;
Robert Braggd7965152016-11-07 19:49:52 +0000565}
566
567/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000568 * append_oa_status - Appends a status record to a userspace read() buffer.
569 * @stream: An i915-perf stream opened for OA metrics
570 * @buf: destination buffer given by userspace
571 * @count: the number of bytes userspace wants to read
572 * @offset: (inout): the current position for writing into @buf
573 * @type: The kind of status to report to userspace
574 *
575 * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
576 * into the userspace read() buffer.
577 *
578 * The @buf @offset will only be updated on success.
579 *
580 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000581 */
582static int append_oa_status(struct i915_perf_stream *stream,
583 char __user *buf,
584 size_t count,
585 size_t *offset,
586 enum drm_i915_perf_record_type type)
587{
588 struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
589
590 if ((count - *offset) < header.size)
591 return -ENOSPC;
592
593 if (copy_to_user(buf + *offset, &header, sizeof(header)))
594 return -EFAULT;
595
596 (*offset) += header.size;
597
598 return 0;
599}
600
601/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000602 * append_oa_sample - Copies single OA report into userspace read() buffer.
603 * @stream: An i915-perf stream opened for OA metrics
604 * @buf: destination buffer given by userspace
605 * @count: the number of bytes userspace wants to read
606 * @offset: (inout): the current position for writing into @buf
607 * @report: A single OA report to (optionally) include as part of the sample
608 *
609 * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
610 * properties when opening a stream, tracked as `stream->sample_flags`. This
611 * function copies the requested components of a single sample to the given
612 * read() @buf.
613 *
614 * The @buf @offset will only be updated on success.
615 *
616 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000617 */
618static int append_oa_sample(struct i915_perf_stream *stream,
619 char __user *buf,
620 size_t count,
621 size_t *offset,
622 const u8 *report)
623{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700624 int report_size = stream->oa_buffer.format_size;
Robert Braggd7965152016-11-07 19:49:52 +0000625 struct drm_i915_perf_record_header header;
626 u32 sample_flags = stream->sample_flags;
627
628 header.type = DRM_I915_PERF_RECORD_SAMPLE;
629 header.pad = 0;
630 header.size = stream->sample_size;
631
632 if ((count - *offset) < header.size)
633 return -ENOSPC;
634
635 buf += *offset;
636 if (copy_to_user(buf, &header, sizeof(header)))
637 return -EFAULT;
638 buf += sizeof(header);
639
640 if (sample_flags & SAMPLE_OA_REPORT) {
641 if (copy_to_user(buf, report, report_size))
642 return -EFAULT;
643 }
644
645 (*offset) += header.size;
646
647 return 0;
648}
649
650/**
651 * Copies all buffered OA reports into userspace read() buffer.
652 * @stream: An i915-perf stream opened for OA metrics
653 * @buf: destination buffer given by userspace
654 * @count: the number of bytes userspace wants to read
655 * @offset: (inout): the current position for writing into @buf
Robert Braggd7965152016-11-07 19:49:52 +0000656 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000657 * Notably any error condition resulting in a short read (-%ENOSPC or
658 * -%EFAULT) will be returned even though one or more records may
Robert Braggd7965152016-11-07 19:49:52 +0000659 * have been successfully copied. In this case it's up to the caller
660 * to decide if the error should be squashed before returning to
661 * userspace.
662 *
663 * Note: reports are consumed from the head, and appended to the
Robert Bragge81b3a52017-05-11 16:43:24 +0100664 * tail, so the tail chases the head?... If you think that's mad
Robert Braggd7965152016-11-07 19:49:52 +0000665 * and back-to-front you're not alone, but this follows the
666 * Gen PRM naming convention.
Robert Bragg16d98b32016-12-07 21:40:33 +0000667 *
668 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000669 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100670static int gen8_append_oa_reports(struct i915_perf_stream *stream,
671 char __user *buf,
672 size_t count,
673 size_t *offset)
674{
Chris Wilson52111c42019-10-10 16:05:20 +0100675 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700676 int report_size = stream->oa_buffer.format_size;
677 u8 *oa_buf_base = stream->oa_buffer.vaddr;
678 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200679 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg19f81df2017-06-13 12:23:03 +0100680 size_t start_offset = *offset;
681 unsigned long flags;
682 unsigned int aged_tail_idx;
683 u32 head, tail;
684 u32 taken;
685 int ret = 0;
686
687 if (WARN_ON(!stream->enabled))
688 return -EIO;
689
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700690 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100691
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700692 head = stream->oa_buffer.head;
693 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
694 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg19f81df2017-06-13 12:23:03 +0100695
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700696 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100697
698 /*
699 * An invalid tail pointer here means we're still waiting for the poll
700 * hrtimer callback to give us a pointer
701 */
702 if (tail == INVALID_TAIL_PTR)
703 return -EAGAIN;
704
705 /*
706 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
707 * while indexing relative to oa_buf_base.
708 */
709 head -= gtt_offset;
710 tail -= gtt_offset;
711
712 /*
713 * An out of bounds or misaligned head or tail pointer implies a driver
714 * bug since we validate + align the tail pointers we read from the
715 * hardware and we are in full control of the head pointer which should
716 * only be incremented by multiples of the report size (notably also
717 * all a power of two).
718 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200719 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
720 tail > OA_BUFFER_SIZE || tail % report_size,
Robert Bragg19f81df2017-06-13 12:23:03 +0100721 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
722 head, tail))
723 return -EIO;
724
725
726 for (/* none */;
727 (taken = OA_TAKEN(tail, head));
728 head = (head + report_size) & mask) {
729 u8 *report = oa_buf_base + head;
730 u32 *report32 = (void *)report;
731 u32 ctx_id;
732 u32 reason;
733
734 /*
735 * All the report sizes factor neatly into the buffer
736 * size so we never expect to see a report split
737 * between the beginning and end of the buffer.
738 *
739 * Given the initial alignment check a misalignment
740 * here would imply a driver bug that would result
741 * in an overrun.
742 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200743 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100744 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
745 break;
746 }
747
748 /*
749 * The reason field includes flags identifying what
750 * triggered this specific report (mostly timer
751 * triggered or e.g. due to a context switch).
752 *
753 * This field is never expected to be zero so we can
754 * check that the report isn't invalid before copying
755 * it to userspace...
756 */
757 reason = ((report32[0] >> OAREPORT_REASON_SHIFT) &
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700758 (IS_GEN(stream->perf->i915, 12) ?
759 OAREPORT_REASON_MASK_EXTENDED :
760 OAREPORT_REASON_MASK));
Robert Bragg19f81df2017-06-13 12:23:03 +0100761 if (reason == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +0100762 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg19f81df2017-06-13 12:23:03 +0100763 DRM_NOTE("Skipping spurious, invalid OA report\n");
764 continue;
765 }
766
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700767 ctx_id = report32[2] & stream->specific_ctx_id_mask;
Robert Bragg19f81df2017-06-13 12:23:03 +0100768
769 /*
770 * Squash whatever is in the CTX_ID field if it's marked as
771 * invalid to be sure we avoid false-positive, single-context
772 * filtering below...
773 *
774 * Note: that we don't clear the valid_ctx_bit so userspace can
775 * understand that the ID has been squashed by the kernel.
776 */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700777 if (!(report32[0] & stream->perf->gen8_valid_ctx_bit) &&
778 INTEL_GEN(stream->perf->i915) <= 11)
Robert Bragg19f81df2017-06-13 12:23:03 +0100779 ctx_id = report32[2] = INVALID_CTX_ID;
780
781 /*
782 * NB: For Gen 8 the OA unit no longer supports clock gating
783 * off for a specific context and the kernel can't securely
784 * stop the counters from updating as system-wide / global
785 * values.
786 *
787 * Automatic reports now include a context ID so reports can be
788 * filtered on the cpu but it's not worth trying to
789 * automatically subtract/hide counter progress for other
790 * contexts while filtering since we can't stop userspace
791 * issuing MI_REPORT_PERF_COUNT commands which would still
792 * provide a side-band view of the real values.
793 *
794 * To allow userspace (such as Mesa/GL_INTEL_performance_query)
795 * to normalize counters for a single filtered context then it
796 * needs be forwarded bookend context-switch reports so that it
797 * can track switches in between MI_REPORT_PERF_COUNT commands
798 * and can itself subtract/ignore the progress of counters
799 * associated with other contexts. Note that the hardware
800 * automatically triggers reports when switching to a new
801 * context which are tagged with the ID of the newly active
802 * context. To avoid the complexity (and likely fragility) of
803 * reading ahead while parsing reports to try and minimize
804 * forwarding redundant context switch reports (i.e. between
805 * other, unrelated contexts) we simply elect to forward them
806 * all.
807 *
808 * We don't rely solely on the reason field to identify context
809 * switches since it's not-uncommon for periodic samples to
810 * identify a switch before any 'context switch' report.
811 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100812 if (!stream->perf->exclusive_stream->ctx ||
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700813 stream->specific_ctx_id == ctx_id ||
814 stream->oa_buffer.last_ctx_id == stream->specific_ctx_id ||
Robert Bragg19f81df2017-06-13 12:23:03 +0100815 reason & OAREPORT_REASON_CTX_SWITCH) {
816
817 /*
818 * While filtering for a single context we avoid
819 * leaking the IDs of other contexts.
820 */
Chris Wilson8f8b1172019-10-07 22:09:41 +0100821 if (stream->perf->exclusive_stream->ctx &&
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700822 stream->specific_ctx_id != ctx_id) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100823 report32[2] = INVALID_CTX_ID;
824 }
825
826 ret = append_oa_sample(stream, buf, count, offset,
827 report);
828 if (ret)
829 break;
830
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700831 stream->oa_buffer.last_ctx_id = ctx_id;
Robert Bragg19f81df2017-06-13 12:23:03 +0100832 }
833
834 /*
835 * The above reason field sanity check is based on
836 * the assumption that the OA buffer is initially
837 * zeroed and we reset the field after copying so the
838 * check is still meaningful once old reports start
839 * being overwritten.
840 */
841 report32[0] = 0;
842 }
843
844 if (start_offset != *offset) {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700845 i915_reg_t oaheadptr;
846
847 oaheadptr = IS_GEN(stream->perf->i915, 12) ?
848 GEN12_OAG_OAHEADPTR : GEN8_OAHEADPTR;
849
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700850 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100851
852 /*
853 * We removed the gtt_offset for the copy loop above, indexing
854 * relative to oa_buf_base so put back here...
855 */
856 head += gtt_offset;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700857 intel_uncore_write(uncore, oaheadptr,
858 head & GEN12_OAG_OAHEADPTR_MASK);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700859 stream->oa_buffer.head = head;
Robert Bragg19f81df2017-06-13 12:23:03 +0100860
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700861 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +0100862 }
863
864 return ret;
865}
866
867/**
868 * gen8_oa_read - copy status records then buffered OA reports
869 * @stream: An i915-perf stream opened for OA metrics
870 * @buf: destination buffer given by userspace
871 * @count: the number of bytes userspace wants to read
872 * @offset: (inout): the current position for writing into @buf
873 *
874 * Checks OA unit status registers and if necessary appends corresponding
875 * status records for userspace (such as for a buffer full condition) and then
876 * initiate appending any buffered OA reports.
877 *
878 * Updates @offset according to the number of bytes successfully copied into
879 * the userspace buffer.
880 *
881 * NB: some data may be successfully copied to the userspace buffer
882 * even if an error is returned, and this is reflected in the
883 * updated @offset.
884 *
885 * Returns: zero on success or a negative error code
886 */
887static int gen8_oa_read(struct i915_perf_stream *stream,
888 char __user *buf,
889 size_t count,
890 size_t *offset)
891{
Chris Wilson52111c42019-10-10 16:05:20 +0100892 struct intel_uncore *uncore = stream->uncore;
Robert Bragg19f81df2017-06-13 12:23:03 +0100893 u32 oastatus;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700894 i915_reg_t oastatus_reg;
Robert Bragg19f81df2017-06-13 12:23:03 +0100895 int ret;
896
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700897 if (WARN_ON(!stream->oa_buffer.vaddr))
Robert Bragg19f81df2017-06-13 12:23:03 +0100898 return -EIO;
899
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700900 oastatus_reg = IS_GEN(stream->perf->i915, 12) ?
901 GEN12_OAG_OASTATUS : GEN8_OASTATUS;
902
903 oastatus = intel_uncore_read(uncore, oastatus_reg);
Robert Bragg19f81df2017-06-13 12:23:03 +0100904
905 /*
906 * We treat OABUFFER_OVERFLOW as a significant error:
907 *
908 * Although theoretically we could handle this more gracefully
909 * sometimes, some Gens don't correctly suppress certain
910 * automatically triggered reports in this condition and so we
911 * have to assume that old reports are now being trampled
912 * over.
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200913 *
914 * Considering how we don't currently give userspace control
915 * over the OA buffer size and always configure a large 16MB
916 * buffer, then a buffer overflow does anyway likely indicate
917 * that something has gone quite badly wrong.
Robert Bragg19f81df2017-06-13 12:23:03 +0100918 */
919 if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
920 ret = append_oa_status(stream, buf, count, offset,
921 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
922 if (ret)
923 return ret;
924
925 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700926 stream->period_exponent);
Robert Bragg19f81df2017-06-13 12:23:03 +0100927
Chris Wilson8f8b1172019-10-07 22:09:41 +0100928 stream->perf->ops.oa_disable(stream);
929 stream->perf->ops.oa_enable(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +0100930
931 /*
932 * Note: .oa_enable() is expected to re-init the oabuffer and
933 * reset GEN8_OASTATUS for us
934 */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700935 oastatus = intel_uncore_read(uncore, oastatus_reg);
Robert Bragg19f81df2017-06-13 12:23:03 +0100936 }
937
938 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
939 ret = append_oa_status(stream, buf, count, offset,
940 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
941 if (ret)
942 return ret;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -0700943 intel_uncore_write(uncore, oastatus_reg,
Chris Wilson8f8b1172019-10-07 22:09:41 +0100944 oastatus & ~GEN8_OASTATUS_REPORT_LOST);
Robert Bragg19f81df2017-06-13 12:23:03 +0100945 }
946
947 return gen8_append_oa_reports(stream, buf, count, offset);
948}
949
950/**
951 * Copies all buffered OA reports into userspace read() buffer.
952 * @stream: An i915-perf stream opened for OA metrics
953 * @buf: destination buffer given by userspace
954 * @count: the number of bytes userspace wants to read
955 * @offset: (inout): the current position for writing into @buf
956 *
957 * Notably any error condition resulting in a short read (-%ENOSPC or
958 * -%EFAULT) will be returned even though one or more records may
959 * have been successfully copied. In this case it's up to the caller
960 * to decide if the error should be squashed before returning to
961 * userspace.
962 *
963 * Note: reports are consumed from the head, and appended to the
964 * tail, so the tail chases the head?... If you think that's mad
965 * and back-to-front you're not alone, but this follows the
966 * Gen PRM naming convention.
967 *
968 * Returns: 0 on success, negative error code on failure.
969 */
Robert Braggd7965152016-11-07 19:49:52 +0000970static int gen7_append_oa_reports(struct i915_perf_stream *stream,
971 char __user *buf,
972 size_t count,
Robert Bragg3bb335c2017-05-11 16:43:27 +0100973 size_t *offset)
Robert Braggd7965152016-11-07 19:49:52 +0000974{
Chris Wilson52111c42019-10-10 16:05:20 +0100975 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700976 int report_size = stream->oa_buffer.format_size;
977 u8 *oa_buf_base = stream->oa_buffer.vaddr;
978 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Joonas Lahtinenfe841682018-11-16 15:55:09 +0200979 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100980 size_t start_offset = *offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100981 unsigned long flags;
982 unsigned int aged_tail_idx;
983 u32 head, tail;
Robert Braggd7965152016-11-07 19:49:52 +0000984 u32 taken;
985 int ret = 0;
986
987 if (WARN_ON(!stream->enabled))
988 return -EIO;
989
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700990 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggf2790202017-05-11 16:43:26 +0100991
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700992 head = stream->oa_buffer.head;
993 aged_tail_idx = stream->oa_buffer.aged_tail_idx;
994 tail = stream->oa_buffer.tails[aged_tail_idx].offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100995
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -0700996 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100997
998 /* An invalid tail pointer here means we're still waiting for the poll
999 * hrtimer callback to give us a pointer
Robert Braggf2790202017-05-11 16:43:26 +01001000 */
Robert Bragg0dd860c2017-05-11 16:43:28 +01001001 if (tail == INVALID_TAIL_PTR)
Robert Braggd7965152016-11-07 19:49:52 +00001002 return -EAGAIN;
1003
Robert Bragg0dd860c2017-05-11 16:43:28 +01001004 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
1005 * while indexing relative to oa_buf_base.
1006 */
1007 head -= gtt_offset;
1008 tail -= gtt_offset;
1009
1010 /* An out of bounds or misaligned head or tail pointer implies a driver
1011 * bug since we validate + align the tail pointers we read from the
1012 * hardware and we are in full control of the head pointer which should
1013 * only be incremented by multiples of the report size (notably also
1014 * all a power of two).
1015 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001016 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
1017 tail > OA_BUFFER_SIZE || tail % report_size,
Robert Bragg0dd860c2017-05-11 16:43:28 +01001018 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
1019 head, tail))
1020 return -EIO;
1021
Robert Braggd7965152016-11-07 19:49:52 +00001022
1023 for (/* none */;
1024 (taken = OA_TAKEN(tail, head));
1025 head = (head + report_size) & mask) {
1026 u8 *report = oa_buf_base + head;
1027 u32 *report32 = (void *)report;
1028
1029 /* All the report sizes factor neatly into the buffer
1030 * size so we never expect to see a report split
1031 * between the beginning and end of the buffer.
1032 *
1033 * Given the initial alignment check a misalignment
1034 * here would imply a driver bug that would result
1035 * in an overrun.
1036 */
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001037 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
Robert Braggd7965152016-11-07 19:49:52 +00001038 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
1039 break;
1040 }
1041
1042 /* The report-ID field for periodic samples includes
1043 * some undocumented flags related to what triggered
1044 * the report and is never expected to be zero so we
1045 * can check that the report isn't invalid before
1046 * copying it to userspace...
1047 */
1048 if (report32[0] == 0) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01001049 if (__ratelimit(&stream->perf->spurious_report_rs))
Robert Bragg712122e2017-05-11 16:43:31 +01001050 DRM_NOTE("Skipping spurious, invalid OA report\n");
Robert Braggd7965152016-11-07 19:49:52 +00001051 continue;
1052 }
1053
1054 ret = append_oa_sample(stream, buf, count, offset, report);
1055 if (ret)
1056 break;
1057
1058 /* The above report-id field sanity check is based on
1059 * the assumption that the OA buffer is initially
1060 * zeroed and we reset the field after copying so the
1061 * check is still meaningful once old reports start
1062 * being overwritten.
1063 */
1064 report32[0] = 0;
1065 }
1066
Robert Bragg3bb335c2017-05-11 16:43:27 +01001067 if (start_offset != *offset) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001068 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001069
Robert Bragg3bb335c2017-05-11 16:43:27 +01001070 /* We removed the gtt_offset for the copy loop above, indexing
1071 * relative to oa_buf_base so put back here...
1072 */
1073 head += gtt_offset;
1074
Chris Wilson8f8b1172019-10-07 22:09:41 +01001075 intel_uncore_write(uncore, GEN7_OASTATUS2,
1076 (head & GEN7_OASTATUS2_HEAD_MASK) |
1077 GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001078 stream->oa_buffer.head = head;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001079
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001080 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg3bb335c2017-05-11 16:43:27 +01001081 }
Robert Braggd7965152016-11-07 19:49:52 +00001082
1083 return ret;
1084}
1085
Robert Bragg16d98b32016-12-07 21:40:33 +00001086/**
1087 * gen7_oa_read - copy status records then buffered OA reports
1088 * @stream: An i915-perf stream opened for OA metrics
1089 * @buf: destination buffer given by userspace
1090 * @count: the number of bytes userspace wants to read
1091 * @offset: (inout): the current position for writing into @buf
1092 *
1093 * Checks Gen 7 specific OA unit status registers and if necessary appends
1094 * corresponding status records for userspace (such as for a buffer full
1095 * condition) and then initiate appending any buffered OA reports.
1096 *
1097 * Updates @offset according to the number of bytes successfully copied into
1098 * the userspace buffer.
1099 *
1100 * Returns: zero on success or a negative error code
1101 */
Robert Braggd7965152016-11-07 19:49:52 +00001102static int gen7_oa_read(struct i915_perf_stream *stream,
1103 char __user *buf,
1104 size_t count,
1105 size_t *offset)
1106{
Chris Wilson52111c42019-10-10 16:05:20 +01001107 struct intel_uncore *uncore = stream->uncore;
Robert Braggd7965152016-11-07 19:49:52 +00001108 u32 oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001109 int ret;
1110
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001111 if (WARN_ON(!stream->oa_buffer.vaddr))
Robert Braggd7965152016-11-07 19:49:52 +00001112 return -EIO;
1113
Chris Wilson8f8b1172019-10-07 22:09:41 +01001114 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001115
Robert Braggd7965152016-11-07 19:49:52 +00001116 /* XXX: On Haswell we don't have a safe way to clear oastatus1
1117 * bits while the OA unit is enabled (while the tail pointer
1118 * may be updated asynchronously) so we ignore status bits
1119 * that have already been reported to userspace.
1120 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001121 oastatus1 &= ~stream->perf->gen7_latched_oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001122
1123 /* We treat OABUFFER_OVERFLOW as a significant error:
1124 *
1125 * - The status can be interpreted to mean that the buffer is
1126 * currently full (with a higher precedence than OA_TAKEN()
1127 * which will start to report a near-empty buffer after an
1128 * overflow) but it's awkward that we can't clear the status
1129 * on Haswell, so without a reset we won't be able to catch
1130 * the state again.
1131 *
1132 * - Since it also implies the HW has started overwriting old
1133 * reports it may also affect our sanity checks for invalid
1134 * reports when copying to userspace that assume new reports
1135 * are being written to cleared memory.
1136 *
1137 * - In the future we may want to introduce a flight recorder
1138 * mode where the driver will automatically maintain a safe
1139 * guard band between head/tail, avoiding this overflow
1140 * condition, but we avoid the added driver complexity for
1141 * now.
1142 */
1143 if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
1144 ret = append_oa_status(stream, buf, count, offset,
1145 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
1146 if (ret)
1147 return ret;
1148
Robert Bragg19f81df2017-06-13 12:23:03 +01001149 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001150 stream->period_exponent);
Robert Braggd7965152016-11-07 19:49:52 +00001151
Chris Wilson8f8b1172019-10-07 22:09:41 +01001152 stream->perf->ops.oa_disable(stream);
1153 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001154
Chris Wilson8f8b1172019-10-07 22:09:41 +01001155 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001156 }
1157
1158 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
1159 ret = append_oa_status(stream, buf, count, offset,
1160 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1161 if (ret)
1162 return ret;
Chris Wilson8f8b1172019-10-07 22:09:41 +01001163 stream->perf->gen7_latched_oastatus1 |=
Robert Braggd7965152016-11-07 19:49:52 +00001164 GEN7_OASTATUS1_REPORT_LOST;
1165 }
1166
Robert Bragg3bb335c2017-05-11 16:43:27 +01001167 return gen7_append_oa_reports(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001168}
1169
Robert Bragg16d98b32016-12-07 21:40:33 +00001170/**
1171 * i915_oa_wait_unlocked - handles blocking IO until OA data available
1172 * @stream: An i915-perf stream opened for OA metrics
1173 *
1174 * Called when userspace tries to read() from a blocking stream FD opened
1175 * for OA metrics. It waits until the hrtimer callback finds a non-empty
1176 * OA buffer and wakes us.
1177 *
1178 * Note: it's acceptable to have this return with some false positives
1179 * since any subsequent read handling will return -EAGAIN if there isn't
1180 * really data ready for userspace yet.
1181 *
1182 * Returns: zero on success or a negative error code
1183 */
Robert Braggd7965152016-11-07 19:49:52 +00001184static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
1185{
Robert Braggd7965152016-11-07 19:49:52 +00001186 /* We would wait indefinitely if periodic sampling is not enabled */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001187 if (!stream->periodic)
Robert Braggd7965152016-11-07 19:49:52 +00001188 return -EIO;
1189
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001190 return wait_event_interruptible(stream->poll_wq,
1191 oa_buffer_check_unlocked(stream));
Robert Braggd7965152016-11-07 19:49:52 +00001192}
1193
Robert Bragg16d98b32016-12-07 21:40:33 +00001194/**
1195 * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
1196 * @stream: An i915-perf stream opened for OA metrics
1197 * @file: An i915 perf stream file
1198 * @wait: poll() state table
1199 *
1200 * For handling userspace polling on an i915 perf stream opened for OA metrics,
1201 * this starts a poll_wait with the wait queue that our hrtimer callback wakes
1202 * when it sees data ready to read in the circular OA buffer.
1203 */
Robert Braggd7965152016-11-07 19:49:52 +00001204static void i915_oa_poll_wait(struct i915_perf_stream *stream,
1205 struct file *file,
1206 poll_table *wait)
1207{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001208 poll_wait(file, &stream->poll_wq, wait);
Robert Braggd7965152016-11-07 19:49:52 +00001209}
1210
Robert Bragg16d98b32016-12-07 21:40:33 +00001211/**
1212 * i915_oa_read - just calls through to &i915_oa_ops->read
1213 * @stream: An i915-perf stream opened for OA metrics
1214 * @buf: destination buffer given by userspace
1215 * @count: the number of bytes userspace wants to read
1216 * @offset: (inout): the current position for writing into @buf
1217 *
1218 * Updates @offset according to the number of bytes successfully copied into
1219 * the userspace buffer.
1220 *
1221 * Returns: zero on success or a negative error code
1222 */
Robert Braggd7965152016-11-07 19:49:52 +00001223static int i915_oa_read(struct i915_perf_stream *stream,
1224 char __user *buf,
1225 size_t count,
1226 size_t *offset)
1227{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001228 return stream->perf->ops.read(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001229}
1230
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001231static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001232{
Chris Wilson5e2a0412019-04-26 17:33:34 +01001233 struct i915_gem_engines_iter it;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001234 struct i915_gem_context *ctx = stream->ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001235 struct intel_context *ce;
Chris Wilsonfa9f6682019-04-26 17:33:29 +01001236 int err;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001237
Chris Wilson5e2a0412019-04-26 17:33:34 +01001238 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
Lionel Landwerlin9a613632019-10-10 16:05:19 +01001239 if (ce->engine != stream->engine) /* first match! */
Chris Wilson5e2a0412019-04-26 17:33:34 +01001240 continue;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001241
Chris Wilson5e2a0412019-04-26 17:33:34 +01001242 /*
1243 * As the ID is the gtt offset of the context's vma we
1244 * pin the vma to ensure the ID remains fixed.
1245 */
1246 err = intel_context_pin(ce);
1247 if (err == 0) {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001248 stream->pinned_ctx = ce;
Chris Wilson5e2a0412019-04-26 17:33:34 +01001249 break;
1250 }
1251 }
1252 i915_gem_context_unlock_engines(ctx);
1253
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001254 return stream->pinned_ctx;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001255}
1256
Robert Bragg16d98b32016-12-07 21:40:33 +00001257/**
1258 * oa_get_render_ctx_id - determine and hold ctx hw id
1259 * @stream: An i915-perf stream opened for OA metrics
1260 *
1261 * Determine the render context hw id, and ensure it remains fixed for the
Robert Braggd7965152016-11-07 19:49:52 +00001262 * lifetime of the stream. This ensures that we don't have to worry about
1263 * updating the context ID in OACONTROL on the fly.
Robert Bragg16d98b32016-12-07 21:40:33 +00001264 *
1265 * Returns: zero on success or a negative error code
Robert Braggd7965152016-11-07 19:49:52 +00001266 */
1267static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
1268{
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001269 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001270
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001271 ce = oa_pin_context(stream);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001272 if (IS_ERR(ce))
1273 return PTR_ERR(ce);
Robert Braggd7965152016-11-07 19:49:52 +00001274
Chris Wilson8f8b1172019-10-07 22:09:41 +01001275 switch (INTEL_GEN(ce->engine->i915)) {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001276 case 7: {
Robert Bragg19f81df2017-06-13 12:23:03 +01001277 /*
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001278 * On Haswell we don't do any post processing of the reports
1279 * and don't need to use the mask.
Robert Bragg19f81df2017-06-13 12:23:03 +01001280 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001281 stream->specific_ctx_id = i915_ggtt_offset(ce->state);
1282 stream->specific_ctx_id_mask = 0;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001283 break;
Robert Bragg19f81df2017-06-13 12:23:03 +01001284 }
Robert Braggd7965152016-11-07 19:49:52 +00001285
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001286 case 8:
1287 case 9:
1288 case 10:
Michal Wajdeczko19c17b72019-10-28 16:45:20 +00001289 if (intel_engine_in_execlists_submission_mode(ce->engine)) {
1290 stream->specific_ctx_id_mask =
1291 (1U << GEN8_CTX_ID_WIDTH) - 1;
1292 stream->specific_ctx_id = stream->specific_ctx_id_mask;
1293 } else {
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001294 /*
1295 * When using GuC, the context descriptor we write in
1296 * i915 is read by GuC and rewritten before it's
1297 * actually written into the hardware. The LRCA is
1298 * what is put into the context id field of the
1299 * context descriptor by GuC. Because it's aligned to
1300 * a page, the lower 12bits are always at 0 and
1301 * dropped by GuC. They won't be part of the context
1302 * ID in the OA reports, so squash those lower bits.
1303 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001304 stream->specific_ctx_id =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001305 lower_32_bits(ce->lrc_desc) >> 12;
1306
1307 /*
1308 * GuC uses the top bit to signal proxy submission, so
1309 * ignore that bit.
1310 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001311 stream->specific_ctx_id_mask =
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001312 (1U << (GEN8_CTX_ID_WIDTH - 1)) - 1;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001313 }
1314 break;
1315
Michel Thierry45e9c822019-08-23 01:20:50 -07001316 case 11:
1317 case 12: {
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001318 stream->specific_ctx_id_mask =
Chris Wilson2935ed52019-10-04 14:40:08 +01001319 ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) << (GEN11_SW_CTX_ID_SHIFT - 32);
1320 stream->specific_ctx_id = stream->specific_ctx_id_mask;
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001321 break;
1322 }
1323
1324 default:
Chris Wilson8f8b1172019-10-07 22:09:41 +01001325 MISSING_CASE(INTEL_GEN(ce->engine->i915));
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001326 }
1327
Chris Wilson2935ed52019-10-04 14:40:08 +01001328 ce->tag = stream->specific_ctx_id_mask;
1329
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001330 DRM_DEBUG_DRIVER("filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001331 stream->specific_ctx_id,
1332 stream->specific_ctx_id_mask);
Lionel Landwerlin61d56762018-06-02 12:29:46 +01001333
Chris Wilson266a2402017-05-04 10:33:08 +01001334 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001335}
1336
Robert Bragg16d98b32016-12-07 21:40:33 +00001337/**
1338 * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
1339 * @stream: An i915-perf stream opened for OA metrics
1340 *
1341 * In case anything needed doing to ensure the context HW ID would remain valid
1342 * for the lifetime of the stream, then that can be undone here.
1343 */
Robert Braggd7965152016-11-07 19:49:52 +00001344static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
1345{
Chris Wilson1fc44d92018-05-17 22:26:32 +01001346 struct intel_context *ce;
Robert Braggd7965152016-11-07 19:49:52 +00001347
Chris Wilson2935ed52019-10-04 14:40:08 +01001348 ce = fetch_and_zero(&stream->pinned_ctx);
1349 if (ce) {
1350 ce->tag = 0; /* recomputed on next submission after parking */
1351 intel_context_unpin(ce);
1352 }
1353
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001354 stream->specific_ctx_id = INVALID_CTX_ID;
1355 stream->specific_ctx_id_mask = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001356}
1357
1358static void
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001359free_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001360{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001361 i915_vma_unpin_and_release(&stream->oa_buffer.vma,
Chris Wilson6a2f59e2018-07-21 13:50:37 +01001362 I915_VMA_RELEASE_MAP);
Robert Braggd7965152016-11-07 19:49:52 +00001363
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001364 stream->oa_buffer.vaddr = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001365}
1366
Lionel Landwerlin6a450082019-10-12 08:23:06 +01001367static void
1368free_oa_configs(struct i915_perf_stream *stream)
1369{
1370 struct i915_oa_config_bo *oa_bo, *tmp;
1371
1372 i915_oa_config_put(stream->oa_config);
1373 llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node)
1374 free_oa_config_bo(oa_bo);
1375}
1376
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001377static void
1378free_noa_wait(struct i915_perf_stream *stream)
1379{
1380 i915_vma_unpin_and_release(&stream->noa_wait, 0);
1381}
1382
Robert Braggd7965152016-11-07 19:49:52 +00001383static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
1384{
Chris Wilson8f8b1172019-10-07 22:09:41 +01001385 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00001386
Chris Wilson8f8b1172019-10-07 22:09:41 +01001387 BUG_ON(stream != perf->exclusive_stream);
Robert Braggd7965152016-11-07 19:49:52 +00001388
Robert Bragg19f81df2017-06-13 12:23:03 +01001389 /*
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001390 * Unset exclusive_stream first, it will be checked while disabling
1391 * the metric set on gen8+.
Robert Bragg19f81df2017-06-13 12:23:03 +01001392 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001393 perf->exclusive_stream = NULL;
1394 perf->ops.disable_metric_set(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001395
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001396 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00001397
Chris Wilson52111c42019-10-10 16:05:20 +01001398 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
Chris Wilsona5efcde2019-10-11 20:03:17 +01001399 intel_engine_pm_put(stream->engine);
Robert Braggd7965152016-11-07 19:49:52 +00001400
1401 if (stream->ctx)
1402 oa_put_render_ctx_id(stream);
1403
Lionel Landwerlin6a450082019-10-12 08:23:06 +01001404 free_oa_configs(stream);
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001405 free_noa_wait(stream);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01001406
Chris Wilson8f8b1172019-10-07 22:09:41 +01001407 if (perf->spurious_report_rs.missed) {
Robert Bragg712122e2017-05-11 16:43:31 +01001408 DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n",
Chris Wilson8f8b1172019-10-07 22:09:41 +01001409 perf->spurious_report_rs.missed);
Robert Bragg712122e2017-05-11 16:43:31 +01001410 }
Robert Braggd7965152016-11-07 19:49:52 +00001411}
1412
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001413static void gen7_init_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001414{
Chris Wilson52111c42019-10-10 16:05:20 +01001415 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001416 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001417 unsigned long flags;
1418
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001419 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Braggd7965152016-11-07 19:49:52 +00001420
1421 /* Pre-DevBDW: OABUFFER must be set with counters off,
1422 * before OASTATUS1, but after OASTATUS2
1423 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001424 intel_uncore_write(uncore, GEN7_OASTATUS2, /* head */
1425 gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001426 stream->oa_buffer.head = gtt_offset;
Robert Braggf2790202017-05-11 16:43:26 +01001427
Chris Wilson8f8b1172019-10-07 22:09:41 +01001428 intel_uncore_write(uncore, GEN7_OABUFFER, gtt_offset);
Robert Braggf2790202017-05-11 16:43:26 +01001429
Chris Wilson8f8b1172019-10-07 22:09:41 +01001430 intel_uncore_write(uncore, GEN7_OASTATUS1, /* tail */
1431 gtt_offset | OABUFFER_SIZE_16M);
Robert Braggd7965152016-11-07 19:49:52 +00001432
Robert Bragg0dd860c2017-05-11 16:43:28 +01001433 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001434 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1435 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg0dd860c2017-05-11 16:43:28 +01001436
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001437 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001438
Robert Braggd7965152016-11-07 19:49:52 +00001439 /* On Haswell we have to track which OASTATUS1 flags we've
1440 * already seen since they can't be cleared while periodic
1441 * sampling is enabled.
1442 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001443 stream->perf->gen7_latched_oastatus1 = 0;
Robert Braggd7965152016-11-07 19:49:52 +00001444
1445 /* NB: although the OA buffer will initially be allocated
1446 * zeroed via shmfs (and so this memset is redundant when
1447 * first allocating), we may re-init the OA buffer, either
1448 * when re-enabling a stream or in error/reset paths.
1449 *
1450 * The reason we clear the buffer for each re-init is for the
1451 * sanity check in gen7_append_oa_reports() that looks at the
1452 * report-id field to make sure it's non-zero which relies on
1453 * the assumption that new reports are being written to zeroed
1454 * memory...
1455 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001456 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001457
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001458 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00001459}
1460
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001461static void gen8_init_oa_buffer(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01001462{
Chris Wilson52111c42019-10-10 16:05:20 +01001463 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001464 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
Robert Bragg19f81df2017-06-13 12:23:03 +01001465 unsigned long flags;
1466
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001467 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001468
Chris Wilson8f8b1172019-10-07 22:09:41 +01001469 intel_uncore_write(uncore, GEN8_OASTATUS, 0);
1470 intel_uncore_write(uncore, GEN8_OAHEADPTR, gtt_offset);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001471 stream->oa_buffer.head = gtt_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01001472
Chris Wilson8f8b1172019-10-07 22:09:41 +01001473 intel_uncore_write(uncore, GEN8_OABUFFER_UDW, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01001474
1475 /*
1476 * PRM says:
1477 *
1478 * "This MMIO must be set before the OATAILPTR
1479 * register and after the OAHEADPTR register. This is
1480 * to enable proper functionality of the overflow
1481 * bit."
1482 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01001483 intel_uncore_write(uncore, GEN8_OABUFFER, gtt_offset |
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001484 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
Chris Wilson8f8b1172019-10-07 22:09:41 +01001485 intel_uncore_write(uncore, GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
Robert Bragg19f81df2017-06-13 12:23:03 +01001486
1487 /* Mark that we need updated tail pointers to read from... */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001488 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1489 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
Robert Bragg19f81df2017-06-13 12:23:03 +01001490
1491 /*
1492 * Reset state used to recognise context switches, affecting which
1493 * reports we will forward to userspace while filtering for a single
1494 * context.
1495 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001496 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
Robert Bragg19f81df2017-06-13 12:23:03 +01001497
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001498 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
Robert Bragg19f81df2017-06-13 12:23:03 +01001499
1500 /*
1501 * NB: although the OA buffer will initially be allocated
1502 * zeroed via shmfs (and so this memset is redundant when
1503 * first allocating), we may re-init the OA buffer, either
1504 * when re-enabling a stream or in error/reset paths.
1505 *
1506 * The reason we clear the buffer for each re-init is for the
1507 * sanity check in gen8_append_oa_reports() that looks at the
1508 * reason field to make sure it's non-zero which relies on
1509 * the assumption that new reports are being written to zeroed
1510 * memory...
1511 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001512 memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
Robert Bragg19f81df2017-06-13 12:23:03 +01001513
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001514 stream->pollin = false;
Robert Bragg19f81df2017-06-13 12:23:03 +01001515}
1516
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07001517static void gen12_init_oa_buffer(struct i915_perf_stream *stream)
1518{
1519 struct intel_uncore *uncore = stream->uncore;
1520 u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
1521 unsigned long flags;
1522
1523 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1524
1525 intel_uncore_write(uncore, GEN12_OAG_OASTATUS, 0);
1526 intel_uncore_write(uncore, GEN12_OAG_OAHEADPTR,
1527 gtt_offset & GEN12_OAG_OAHEADPTR_MASK);
1528 stream->oa_buffer.head = gtt_offset;
1529
1530 /*
1531 * PRM says:
1532 *
1533 * "This MMIO must be set before the OATAILPTR
1534 * register and after the OAHEADPTR register. This is
1535 * to enable proper functionality of the overflow
1536 * bit."
1537 */
1538 intel_uncore_write(uncore, GEN12_OAG_OABUFFER, gtt_offset |
1539 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
1540 intel_uncore_write(uncore, GEN12_OAG_OATAILPTR,
1541 gtt_offset & GEN12_OAG_OATAILPTR_MASK);
1542
1543 /* Mark that we need updated tail pointers to read from... */
1544 stream->oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1545 stream->oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1546
1547 /*
1548 * Reset state used to recognise context switches, affecting which
1549 * reports we will forward to userspace while filtering for a single
1550 * context.
1551 */
1552 stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
1553
1554 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1555
1556 /*
1557 * NB: although the OA buffer will initially be allocated
1558 * zeroed via shmfs (and so this memset is redundant when
1559 * first allocating), we may re-init the OA buffer, either
1560 * when re-enabling a stream or in error/reset paths.
1561 *
1562 * The reason we clear the buffer for each re-init is for the
1563 * sanity check in gen8_append_oa_reports() that looks at the
1564 * reason field to make sure it's non-zero which relies on
1565 * the assumption that new reports are being written to zeroed
1566 * memory...
1567 */
1568 memset(stream->oa_buffer.vaddr, 0,
1569 stream->oa_buffer.vma->size);
1570
1571 stream->pollin = false;
1572}
1573
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001574static int alloc_oa_buffer(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00001575{
1576 struct drm_i915_gem_object *bo;
1577 struct i915_vma *vma;
1578 int ret;
1579
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001580 if (WARN_ON(stream->oa_buffer.vma))
Robert Braggd7965152016-11-07 19:49:52 +00001581 return -ENODEV;
1582
Joonas Lahtinenfe841682018-11-16 15:55:09 +02001583 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
1584 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
1585
Chris Wilson8f8b1172019-10-07 22:09:41 +01001586 bo = i915_gem_object_create_shmem(stream->perf->i915, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001587 if (IS_ERR(bo)) {
1588 DRM_ERROR("Failed to allocate OA buffer\n");
Chris Wilson28507482019-10-04 14:39:58 +01001589 return PTR_ERR(bo);
Robert Braggd7965152016-11-07 19:49:52 +00001590 }
1591
Chris Wilsona679f582019-03-21 16:19:07 +00001592 i915_gem_object_set_cache_coherency(bo, I915_CACHE_LLC);
Robert Braggd7965152016-11-07 19:49:52 +00001593
1594 /* PreHSW required 512K alignment, HSW requires 16M */
1595 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0);
1596 if (IS_ERR(vma)) {
1597 ret = PTR_ERR(vma);
1598 goto err_unref;
1599 }
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001600 stream->oa_buffer.vma = vma;
Robert Braggd7965152016-11-07 19:49:52 +00001601
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001602 stream->oa_buffer.vaddr =
Robert Braggd7965152016-11-07 19:49:52 +00001603 i915_gem_object_pin_map(bo, I915_MAP_WB);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001604 if (IS_ERR(stream->oa_buffer.vaddr)) {
1605 ret = PTR_ERR(stream->oa_buffer.vaddr);
Robert Braggd7965152016-11-07 19:49:52 +00001606 goto err_unpin;
1607 }
1608
Chris Wilson28507482019-10-04 14:39:58 +01001609 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001610
1611err_unpin:
1612 __i915_vma_unpin(vma);
1613
1614err_unref:
1615 i915_gem_object_put(bo);
1616
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07001617 stream->oa_buffer.vaddr = NULL;
1618 stream->oa_buffer.vma = NULL;
Robert Braggd7965152016-11-07 19:49:52 +00001619
Robert Braggd7965152016-11-07 19:49:52 +00001620 return ret;
1621}
1622
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001623static u32 *save_restore_register(struct i915_perf_stream *stream, u32 *cs,
1624 bool save, i915_reg_t reg, u32 offset,
1625 u32 dword_count)
1626{
1627 u32 cmd;
1628 u32 d;
1629
1630 cmd = save ? MI_STORE_REGISTER_MEM : MI_LOAD_REGISTER_MEM;
1631 if (INTEL_GEN(stream->perf->i915) >= 8)
1632 cmd++;
1633
1634 for (d = 0; d < dword_count; d++) {
1635 *cs++ = cmd;
1636 *cs++ = i915_mmio_reg_offset(reg) + 4 * d;
1637 *cs++ = intel_gt_scratch_offset(stream->engine->gt,
1638 offset) + 4 * d;
1639 *cs++ = 0;
1640 }
1641
1642 return cs;
1643}
1644
1645static int alloc_noa_wait(struct i915_perf_stream *stream)
1646{
1647 struct drm_i915_private *i915 = stream->perf->i915;
1648 struct drm_i915_gem_object *bo;
1649 struct i915_vma *vma;
1650 const u64 delay_ticks = 0xffffffffffffffff -
1651 DIV64_U64_ROUND_UP(
1652 atomic64_read(&stream->perf->noa_programming_delay) *
1653 RUNTIME_INFO(i915)->cs_timestamp_frequency_khz,
1654 1000000ull);
1655 const u32 base = stream->engine->mmio_base;
1656#define CS_GPR(x) GEN8_RING_CS_GPR(base, x)
1657 u32 *batch, *ts0, *cs, *jump;
1658 int ret, i;
1659 enum {
1660 START_TS,
1661 NOW_TS,
1662 DELTA_TS,
1663 JUMP_PREDICATE,
1664 DELTA_TARGET,
1665 N_CS_GPR
1666 };
1667
1668 bo = i915_gem_object_create_internal(i915, 4096);
1669 if (IS_ERR(bo)) {
1670 DRM_ERROR("Failed to allocate NOA wait batchbuffer\n");
1671 return PTR_ERR(bo);
1672 }
1673
1674 /*
1675 * We pin in GGTT because we jump into this buffer now because
1676 * multiple OA config BOs will have a jump to this address and it
1677 * needs to be fixed during the lifetime of the i915/perf stream.
1678 */
1679 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, 0, PIN_HIGH);
1680 if (IS_ERR(vma)) {
1681 ret = PTR_ERR(vma);
1682 goto err_unref;
1683 }
1684
1685 batch = cs = i915_gem_object_pin_map(bo, I915_MAP_WB);
1686 if (IS_ERR(batch)) {
1687 ret = PTR_ERR(batch);
1688 goto err_unpin;
1689 }
1690
1691 /* Save registers. */
1692 for (i = 0; i < N_CS_GPR; i++)
1693 cs = save_restore_register(
1694 stream, cs, true /* save */, CS_GPR(i),
1695 INTEL_GT_SCRATCH_FIELD_PERF_CS_GPR + 8 * i, 2);
1696 cs = save_restore_register(
1697 stream, cs, true /* save */, MI_PREDICATE_RESULT_1,
1698 INTEL_GT_SCRATCH_FIELD_PERF_PREDICATE_RESULT_1, 1);
1699
1700 /* First timestamp snapshot location. */
1701 ts0 = cs;
1702
1703 /*
1704 * Initial snapshot of the timestamp register to implement the wait.
1705 * We work with 32b values, so clear out the top 32b bits of the
1706 * register because the ALU works 64bits.
1707 */
1708 *cs++ = MI_LOAD_REGISTER_IMM(1);
1709 *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS)) + 4;
1710 *cs++ = 0;
1711 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1712 *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
1713 *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS));
1714
1715 /*
1716 * This is the location we're going to jump back into until the
1717 * required amount of time has passed.
1718 */
1719 jump = cs;
1720
1721 /*
1722 * Take another snapshot of the timestamp register. Take care to clear
1723 * up the top 32bits of CS_GPR(1) as we're using it for other
1724 * operations below.
1725 */
1726 *cs++ = MI_LOAD_REGISTER_IMM(1);
1727 *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS)) + 4;
1728 *cs++ = 0;
1729 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1730 *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
1731 *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS));
1732
1733 /*
1734 * Do a diff between the 2 timestamps and store the result back into
1735 * CS_GPR(1).
1736 */
1737 *cs++ = MI_MATH(5);
1738 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(NOW_TS));
1739 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(START_TS));
1740 *cs++ = MI_MATH_SUB;
1741 *cs++ = MI_MATH_STORE(MI_MATH_REG(DELTA_TS), MI_MATH_REG_ACCU);
1742 *cs++ = MI_MATH_STORE(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
1743
1744 /*
1745 * Transfer the carry flag (set to 1 if ts1 < ts0, meaning the
1746 * timestamp have rolled over the 32bits) into the predicate register
1747 * to be used for the predicated jump.
1748 */
1749 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1750 *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
1751 *cs++ = i915_mmio_reg_offset(MI_PREDICATE_RESULT_1);
1752
1753 /* Restart from the beginning if we had timestamps roll over. */
1754 *cs++ = (INTEL_GEN(i915) < 8 ?
1755 MI_BATCH_BUFFER_START :
1756 MI_BATCH_BUFFER_START_GEN8) |
1757 MI_BATCH_PREDICATE;
1758 *cs++ = i915_ggtt_offset(vma) + (ts0 - batch) * 4;
1759 *cs++ = 0;
1760
1761 /*
1762 * Now add the diff between to previous timestamps and add it to :
1763 * (((1 * << 64) - 1) - delay_ns)
1764 *
1765 * When the Carry Flag contains 1 this means the elapsed time is
1766 * longer than the expected delay, and we can exit the wait loop.
1767 */
1768 *cs++ = MI_LOAD_REGISTER_IMM(2);
1769 *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET));
1770 *cs++ = lower_32_bits(delay_ticks);
1771 *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET)) + 4;
1772 *cs++ = upper_32_bits(delay_ticks);
1773
1774 *cs++ = MI_MATH(4);
1775 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(DELTA_TS));
1776 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(DELTA_TARGET));
1777 *cs++ = MI_MATH_ADD;
1778 *cs++ = MI_MATH_STOREINV(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
1779
1780 /*
1781 * Transfer the result into the predicate register to be used for the
1782 * predicated jump.
1783 */
1784 *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1785 *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
1786 *cs++ = i915_mmio_reg_offset(MI_PREDICATE_RESULT_1);
1787
1788 /* Predicate the jump. */
1789 *cs++ = (INTEL_GEN(i915) < 8 ?
1790 MI_BATCH_BUFFER_START :
1791 MI_BATCH_BUFFER_START_GEN8) |
1792 MI_BATCH_PREDICATE;
1793 *cs++ = i915_ggtt_offset(vma) + (jump - batch) * 4;
1794 *cs++ = 0;
1795
1796 /* Restore registers. */
1797 for (i = 0; i < N_CS_GPR; i++)
1798 cs = save_restore_register(
1799 stream, cs, false /* restore */, CS_GPR(i),
1800 INTEL_GT_SCRATCH_FIELD_PERF_CS_GPR + 8 * i, 2);
1801 cs = save_restore_register(
1802 stream, cs, false /* restore */, MI_PREDICATE_RESULT_1,
1803 INTEL_GT_SCRATCH_FIELD_PERF_PREDICATE_RESULT_1, 1);
1804
1805 /* And return to the ring. */
1806 *cs++ = MI_BATCH_BUFFER_END;
1807
1808 GEM_BUG_ON(cs - batch > PAGE_SIZE / sizeof(*batch));
1809
1810 i915_gem_object_flush_map(bo);
1811 i915_gem_object_unpin_map(bo);
1812
1813 stream->noa_wait = vma;
1814 return 0;
1815
1816err_unpin:
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001817 i915_vma_unpin_and_release(&vma, 0);
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01001818err_unref:
1819 i915_gem_object_put(bo);
1820 return ret;
1821}
1822
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001823static u32 *write_cs_mi_lri(u32 *cs,
1824 const struct i915_oa_reg *reg_data,
1825 u32 n_regs)
Robert Braggd7965152016-11-07 19:49:52 +00001826{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001827 u32 i;
Robert Braggd7965152016-11-07 19:49:52 +00001828
1829 for (i = 0; i < n_regs; i++) {
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001830 if ((i % MI_LOAD_REGISTER_IMM_MAX_REGS) == 0) {
1831 u32 n_lri = min_t(u32,
1832 n_regs - i,
1833 MI_LOAD_REGISTER_IMM_MAX_REGS);
Robert Braggd7965152016-11-07 19:49:52 +00001834
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001835 *cs++ = MI_LOAD_REGISTER_IMM(n_lri);
1836 }
1837 *cs++ = i915_mmio_reg_offset(reg_data[i].addr);
1838 *cs++ = reg_data[i].value;
Robert Braggd7965152016-11-07 19:49:52 +00001839 }
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001840
1841 return cs;
Robert Braggd7965152016-11-07 19:49:52 +00001842}
1843
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001844static int num_lri_dwords(int num_regs)
Robert Braggd7965152016-11-07 19:49:52 +00001845{
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001846 int count = 0;
1847
1848 if (num_regs > 0) {
1849 count += DIV_ROUND_UP(num_regs, MI_LOAD_REGISTER_IMM_MAX_REGS);
1850 count += num_regs * 2;
1851 }
1852
1853 return count;
1854}
1855
1856static struct i915_oa_config_bo *
1857alloc_oa_config_buffer(struct i915_perf_stream *stream,
1858 struct i915_oa_config *oa_config)
1859{
1860 struct drm_i915_gem_object *obj;
1861 struct i915_oa_config_bo *oa_bo;
1862 size_t config_length = 0;
1863 u32 *cs;
1864 int err;
1865
1866 oa_bo = kzalloc(sizeof(*oa_bo), GFP_KERNEL);
1867 if (!oa_bo)
1868 return ERR_PTR(-ENOMEM);
1869
1870 config_length += num_lri_dwords(oa_config->mux_regs_len);
1871 config_length += num_lri_dwords(oa_config->b_counter_regs_len);
1872 config_length += num_lri_dwords(oa_config->flex_regs_len);
1873 config_length++; /* MI_BATCH_BUFFER_END */
1874 config_length = ALIGN(sizeof(u32) * config_length, I915_GTT_PAGE_SIZE);
1875
1876 obj = i915_gem_object_create_shmem(stream->perf->i915, config_length);
1877 if (IS_ERR(obj)) {
1878 err = PTR_ERR(obj);
1879 goto err_free;
1880 }
1881
1882 cs = i915_gem_object_pin_map(obj, I915_MAP_WB);
1883 if (IS_ERR(cs)) {
1884 err = PTR_ERR(cs);
1885 goto err_oa_bo;
1886 }
1887
1888 cs = write_cs_mi_lri(cs,
1889 oa_config->mux_regs,
1890 oa_config->mux_regs_len);
1891 cs = write_cs_mi_lri(cs,
1892 oa_config->b_counter_regs,
1893 oa_config->b_counter_regs_len);
1894 cs = write_cs_mi_lri(cs,
1895 oa_config->flex_regs,
1896 oa_config->flex_regs_len);
1897
1898 *cs++ = MI_BATCH_BUFFER_END;
1899
1900 i915_gem_object_flush_map(obj);
1901 i915_gem_object_unpin_map(obj);
1902
1903 oa_bo->vma = i915_vma_instance(obj,
1904 &stream->engine->gt->ggtt->vm,
1905 NULL);
1906 if (IS_ERR(oa_bo->vma)) {
1907 err = PTR_ERR(oa_bo->vma);
1908 goto err_oa_bo;
1909 }
1910
1911 oa_bo->oa_config = i915_oa_config_get(oa_config);
1912 llist_add(&oa_bo->node, &stream->oa_config_bos);
1913
1914 return oa_bo;
1915
1916err_oa_bo:
1917 i915_gem_object_put(obj);
1918err_free:
1919 kfree(oa_bo);
1920 return ERR_PTR(err);
1921}
1922
1923static struct i915_vma *
1924get_oa_vma(struct i915_perf_stream *stream, struct i915_oa_config *oa_config)
1925{
1926 struct i915_oa_config_bo *oa_bo;
1927
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001928 /*
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001929 * Look for the buffer in the already allocated BOs attached
1930 * to the stream.
Robert Braggd7965152016-11-07 19:49:52 +00001931 */
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001932 llist_for_each_entry(oa_bo, stream->oa_config_bos.first, node) {
1933 if (oa_bo->oa_config == oa_config &&
1934 memcmp(oa_bo->oa_config->uuid,
1935 oa_config->uuid,
1936 sizeof(oa_config->uuid)) == 0)
1937 goto out;
1938 }
1939
1940 oa_bo = alloc_oa_config_buffer(stream, oa_config);
1941 if (IS_ERR(oa_bo))
1942 return ERR_CAST(oa_bo);
1943
1944out:
1945 return i915_vma_get(oa_bo->vma);
1946}
1947
1948static int emit_oa_config(struct i915_perf_stream *stream,
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03001949 struct i915_oa_config *oa_config,
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001950 struct intel_context *ce)
1951{
1952 struct i915_request *rq;
1953 struct i915_vma *vma;
1954 int err;
1955
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03001956 vma = get_oa_vma(stream, oa_config);
Lionel Landwerlin15d0ace2019-10-12 08:23:08 +01001957 if (IS_ERR(vma))
1958 return PTR_ERR(vma);
1959
1960 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1961 if (err)
1962 goto err_vma_put;
1963
1964 rq = i915_request_create(ce);
1965 if (IS_ERR(rq)) {
1966 err = PTR_ERR(rq);
1967 goto err_vma_unpin;
1968 }
1969
1970 i915_vma_lock(vma);
1971 err = i915_request_await_object(rq, vma->obj, 0);
1972 if (!err)
1973 err = i915_vma_move_to_active(vma, rq, 0);
1974 i915_vma_unlock(vma);
1975 if (err)
1976 goto err_add_request;
1977
1978 err = rq->engine->emit_bb_start(rq,
1979 vma->node.start, 0,
1980 I915_DISPATCH_SECURE);
1981err_add_request:
1982 i915_request_add(rq);
1983err_vma_unpin:
1984 i915_vma_unpin(vma);
1985err_vma_put:
1986 i915_vma_put(vma);
1987 return err;
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001988}
1989
Chris Wilson5f5c3822019-10-12 10:10:56 +01001990static struct intel_context *oa_context(struct i915_perf_stream *stream)
1991{
1992 return stream->pinned_ctx ?: stream->engine->kernel_context;
1993}
1994
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001995static int hsw_enable_metric_set(struct i915_perf_stream *stream)
1996{
Chris Wilson52111c42019-10-10 16:05:20 +01001997 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01001998
1999 /*
2000 * PRM:
2001 *
2002 * OA unit is using “crclk” for its functionality. When trunk
2003 * level clock gating takes place, OA clock would be gated,
2004 * unable to count the events from non-render clock domain.
2005 * Render clock gating must be disabled when OA is enabled to
2006 * count the events from non-render domain. Unit level clock
2007 * gating for RCS should also be disabled.
2008 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002009 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2010 GEN7_DOP_CLOCK_GATE_ENABLE, 0);
2011 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2012 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Lionel Landwerlin14bfcd32019-07-10 11:55:24 +01002013
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03002014 return emit_oa_config(stream, stream->oa_config, oa_context(stream));
Robert Braggd7965152016-11-07 19:49:52 +00002015}
2016
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002017static void hsw_disable_metric_set(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002018{
Chris Wilson52111c42019-10-10 16:05:20 +01002019 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002020
Chris Wilson8f8b1172019-10-07 22:09:41 +01002021 intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2022 GEN6_CSUNIT_CLOCK_GATE_DISABLE, 0);
2023 intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2024 0, GEN7_DOP_CLOCK_GATE_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00002025
Chris Wilson8f8b1172019-10-07 22:09:41 +01002026 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Braggd7965152016-11-07 19:49:52 +00002027}
2028
Chris Wilsona9877da2019-07-16 22:34:43 +01002029static u32 oa_config_flex_reg(const struct i915_oa_config *oa_config,
2030 i915_reg_t reg)
2031{
2032 u32 mmio = i915_mmio_reg_offset(reg);
2033 int i;
2034
2035 /*
2036 * This arbitrary default will select the 'EU FPU0 Pipeline
2037 * Active' event. In the future it's anticipated that there
2038 * will be an explicit 'No Event' we can select, but not yet...
2039 */
2040 if (!oa_config)
2041 return 0;
2042
2043 for (i = 0; i < oa_config->flex_regs_len; i++) {
2044 if (i915_mmio_reg_offset(oa_config->flex_regs[i].addr) == mmio)
2045 return oa_config->flex_regs[i].value;
2046 }
2047
2048 return 0;
2049}
Robert Bragg19f81df2017-06-13 12:23:03 +01002050/*
2051 * NB: It must always remain pointer safe to run this even if the OA unit
2052 * has been disabled.
2053 *
2054 * It's fine to put out-of-date values into these per-context registers
2055 * in the case that the OA unit has been disabled.
2056 */
Chris Wilsonb146e5e2019-03-06 08:47:04 +00002057static void
Chris Wilson7dc56af2019-09-24 15:59:50 +01002058gen8_update_reg_state_unlocked(const struct intel_context *ce,
2059 const struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002060{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002061 u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
2062 u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
Robert Bragg19f81df2017-06-13 12:23:03 +01002063 /* The MMIO offsets for Flex EU registers aren't contiguous */
Lionel Landwerlin35ab4fd2018-08-13 09:02:18 +01002064 i915_reg_t flex_regs[] = {
2065 EU_PERF_CNTL0,
2066 EU_PERF_CNTL1,
2067 EU_PERF_CNTL2,
2068 EU_PERF_CNTL3,
2069 EU_PERF_CNTL4,
2070 EU_PERF_CNTL5,
2071 EU_PERF_CNTL6,
Robert Bragg19f81df2017-06-13 12:23:03 +01002072 };
Chris Wilson7dc56af2019-09-24 15:59:50 +01002073 u32 *reg_state = ce->lrc_reg_state;
Robert Bragg19f81df2017-06-13 12:23:03 +01002074 int i;
2075
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002076 if (IS_GEN(stream->perf->i915, 12)) {
2077 u32 format = stream->oa_buffer.format;
Robert Bragg19f81df2017-06-13 12:23:03 +01002078
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002079 reg_state[ctx_oactxctrl + 1] =
2080 (format << GEN12_OAR_OACONTROL_COUNTER_FORMAT_SHIFT) |
2081 (stream->oa_config ? GEN12_OAR_OACONTROL_COUNTER_ENABLE : 0);
2082 } else {
2083 reg_state[ctx_oactxctrl + 1] =
2084 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2085 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2086 GEN8_OA_COUNTER_RESUME;
2087 }
2088
2089 for (i = 0; !!ctx_flexeu0 && i < ARRAY_SIZE(flex_regs); i++)
Chris Wilson7dc56af2019-09-24 15:59:50 +01002090 reg_state[ctx_flexeu0 + i * 2 + 1] =
2091 oa_config_flex_reg(stream->oa_config, flex_regs[i]);
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002092
Chris Wilson8f8b1172019-10-07 22:09:41 +01002093 reg_state[CTX_R_PWR_CLK_STATE] =
2094 intel_sseu_make_rpcs(ce->engine->i915, &ce->sseu);
Robert Bragg19f81df2017-06-13 12:23:03 +01002095}
2096
Chris Wilsona9877da2019-07-16 22:34:43 +01002097struct flex {
2098 i915_reg_t reg;
2099 u32 offset;
2100 u32 value;
2101};
2102
2103static int
2104gen8_store_flex(struct i915_request *rq,
2105 struct intel_context *ce,
2106 const struct flex *flex, unsigned int count)
2107{
2108 u32 offset;
2109 u32 *cs;
2110
2111 cs = intel_ring_begin(rq, 4 * count);
2112 if (IS_ERR(cs))
2113 return PTR_ERR(cs);
2114
2115 offset = i915_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
2116 do {
2117 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
Chris Wilson7dc56af2019-09-24 15:59:50 +01002118 *cs++ = offset + flex->offset * sizeof(u32);
Chris Wilsona9877da2019-07-16 22:34:43 +01002119 *cs++ = 0;
2120 *cs++ = flex->value;
2121 } while (flex++, --count);
2122
2123 intel_ring_advance(rq, cs);
2124
2125 return 0;
2126}
2127
2128static int
2129gen8_load_flex(struct i915_request *rq,
2130 struct intel_context *ce,
2131 const struct flex *flex, unsigned int count)
2132{
2133 u32 *cs;
2134
2135 GEM_BUG_ON(!count || count > 63);
2136
2137 cs = intel_ring_begin(rq, 2 * count + 2);
2138 if (IS_ERR(cs))
2139 return PTR_ERR(cs);
2140
2141 *cs++ = MI_LOAD_REGISTER_IMM(count);
2142 do {
2143 *cs++ = i915_mmio_reg_offset(flex->reg);
2144 *cs++ = flex->value;
2145 } while (flex++, --count);
2146 *cs++ = MI_NOOP;
2147
2148 intel_ring_advance(rq, cs);
2149
2150 return 0;
2151}
2152
2153static int gen8_modify_context(struct intel_context *ce,
2154 const struct flex *flex, unsigned int count)
2155{
2156 struct i915_request *rq;
2157 int err;
2158
2159 lockdep_assert_held(&ce->pin_mutex);
2160
2161 rq = i915_request_create(ce->engine->kernel_context);
2162 if (IS_ERR(rq))
2163 return PTR_ERR(rq);
2164
2165 /* Serialise with the remote context */
2166 err = intel_context_prepare_remote_request(ce, rq);
2167 if (err == 0)
2168 err = gen8_store_flex(rq, ce, flex, count);
2169
2170 i915_request_add(rq);
2171 return err;
2172}
2173
2174static int gen8_modify_self(struct intel_context *ce,
2175 const struct flex *flex, unsigned int count)
2176{
2177 struct i915_request *rq;
2178 int err;
2179
2180 rq = i915_request_create(ce);
2181 if (IS_ERR(rq))
2182 return PTR_ERR(rq);
2183
2184 err = gen8_load_flex(rq, ce, flex, count);
2185
2186 i915_request_add(rq);
2187 return err;
2188}
2189
Chris Wilson5cca5032019-07-26 14:14:58 +01002190static int gen8_configure_context(struct i915_gem_context *ctx,
2191 struct flex *flex, unsigned int count)
2192{
2193 struct i915_gem_engines_iter it;
2194 struct intel_context *ce;
2195 int err = 0;
2196
2197 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
2198 GEM_BUG_ON(ce == ce->engine->kernel_context);
2199
2200 if (ce->engine->class != RENDER_CLASS)
2201 continue;
2202
2203 err = intel_context_lock_pinned(ce);
2204 if (err)
2205 break;
2206
2207 flex->value = intel_sseu_make_rpcs(ctx->i915, &ce->sseu);
2208
2209 /* Otherwise OA settings will be set upon first use */
2210 if (intel_context_is_pinned(ce))
2211 err = gen8_modify_context(ce, flex, count);
2212
2213 intel_context_unlock_pinned(ce);
2214 if (err)
2215 break;
2216 }
2217 i915_gem_context_unlock_engines(ctx);
2218
2219 return err;
2220}
2221
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002222static int gen12_emit_oar_config(struct intel_context *ce, bool enable)
2223{
2224 struct i915_request *rq;
2225 u32 *cs;
2226 int err = 0;
2227
2228 rq = i915_request_create(ce);
2229 if (IS_ERR(rq))
2230 return PTR_ERR(rq);
2231
2232 cs = intel_ring_begin(rq, 4);
2233 if (IS_ERR(cs)) {
2234 err = PTR_ERR(cs);
2235 goto out;
2236 }
2237
2238 *cs++ = MI_LOAD_REGISTER_IMM(1);
2239 *cs++ = i915_mmio_reg_offset(RING_CONTEXT_CONTROL(ce->engine->mmio_base));
2240 *cs++ = _MASKED_FIELD(GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE,
2241 enable ? GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE : 0);
2242 *cs++ = MI_NOOP;
2243
2244 intel_ring_advance(rq, cs);
2245
2246out:
2247 i915_request_add(rq);
2248
2249 return err;
2250}
2251
Robert Bragg19f81df2017-06-13 12:23:03 +01002252/*
Robert Bragg19f81df2017-06-13 12:23:03 +01002253 * Manages updating the per-context aspects of the OA stream
2254 * configuration across all contexts.
2255 *
2256 * The awkward consideration here is that OACTXCONTROL controls the
2257 * exponent for periodic sampling which is primarily used for system
2258 * wide profiling where we'd like a consistent sampling period even in
2259 * the face of context switches.
2260 *
2261 * Our approach of updating the register state context (as opposed to
2262 * say using a workaround batch buffer) ensures that the hardware
2263 * won't automatically reload an out-of-date timer exponent even
2264 * transiently before a WA BB could be parsed.
2265 *
2266 * This function needs to:
2267 * - Ensure the currently running context's per-context OA state is
2268 * updated
2269 * - Ensure that all existing contexts will have the correct per-context
2270 * OA state if they are scheduled for use.
2271 * - Ensure any new contexts will be initialized with the correct
2272 * per-context OA state.
2273 *
2274 * Note: it's only the RCS/Render context that has any OA state.
2275 */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002276static int lrc_configure_all_contexts(struct i915_perf_stream *stream,
2277 const struct i915_oa_config *oa_config)
Robert Bragg19f81df2017-06-13 12:23:03 +01002278{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002279 struct drm_i915_private *i915 = stream->perf->i915;
Chris Wilsona9877da2019-07-16 22:34:43 +01002280 /* The MMIO offsets for Flex EU registers aren't contiguous */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002281 const u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
Chris Wilson7dc56af2019-09-24 15:59:50 +01002282#define ctx_flexeuN(N) (ctx_flexeu0 + 2 * (N) + 1)
Chris Wilsona9877da2019-07-16 22:34:43 +01002283 struct flex regs[] = {
2284 {
2285 GEN8_R_PWR_CLK_STATE,
2286 CTX_R_PWR_CLK_STATE,
2287 },
2288 {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002289 IS_GEN(i915, 12) ?
2290 GEN12_OAR_OACONTROL : GEN8_OACTXCONTROL,
Chris Wilson8f8b1172019-10-07 22:09:41 +01002291 stream->perf->ctx_oactxctrl_offset + 1,
Chris Wilsona9877da2019-07-16 22:34:43 +01002292 },
2293 { EU_PERF_CNTL0, ctx_flexeuN(0) },
2294 { EU_PERF_CNTL1, ctx_flexeuN(1) },
2295 { EU_PERF_CNTL2, ctx_flexeuN(2) },
2296 { EU_PERF_CNTL3, ctx_flexeuN(3) },
2297 { EU_PERF_CNTL4, ctx_flexeuN(4) },
2298 { EU_PERF_CNTL5, ctx_flexeuN(5) },
2299 { EU_PERF_CNTL6, ctx_flexeuN(6) },
2300 };
2301#undef ctx_flexeuN
2302 struct intel_engine_cs *engine;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002303 struct i915_gem_context *ctx, *cn;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002304 size_t array_size = IS_GEN(i915, 12) ? 2 : ARRAY_SIZE(regs);
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002305 int i, err;
Robert Bragg19f81df2017-06-13 12:23:03 +01002306
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002307 if (IS_GEN(i915, 12)) {
2308 u32 format = stream->oa_buffer.format;
2309
2310 regs[1].value =
2311 (format << GEN12_OAR_OACONTROL_COUNTER_FORMAT_SHIFT) |
2312 (oa_config ? GEN12_OAR_OACONTROL_COUNTER_ENABLE : 0);
2313 } else {
2314 regs[1].value =
2315 (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2316 (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2317 GEN8_OA_COUNTER_RESUME;
2318 }
2319
2320 for (i = 2; !!ctx_flexeu0 && i < array_size; i++)
Chris Wilsona9877da2019-07-16 22:34:43 +01002321 regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
2322
Chris Wilsona4c969d2019-10-07 22:09:42 +01002323 lockdep_assert_held(&stream->perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01002324
Robert Bragg19f81df2017-06-13 12:23:03 +01002325 /*
2326 * The OA register config is setup through the context image. This image
2327 * might be written to by the GPU on context switch (in particular on
2328 * lite-restore). This means we can't safely update a context's image,
2329 * if this context is scheduled/submitted to run on the GPU.
2330 *
2331 * We could emit the OA register config through the batch buffer but
2332 * this might leave small interval of time where the OA unit is
2333 * configured at an invalid sampling period.
2334 *
Chris Wilsona9877da2019-07-16 22:34:43 +01002335 * Note that since we emit all requests from a single ring, there
2336 * is still an implicit global barrier here that may cause a high
2337 * priority context to wait for an otherwise independent low priority
2338 * context. Contexts idle at the time of reconfiguration are not
2339 * trapped behind the barrier.
Robert Bragg19f81df2017-06-13 12:23:03 +01002340 */
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002341 spin_lock(&i915->gem.contexts.lock);
2342 list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
Chris Wilsona9877da2019-07-16 22:34:43 +01002343 if (ctx == i915->kernel_context)
2344 continue;
2345
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002346 if (!kref_get_unless_zero(&ctx->ref))
2347 continue;
2348
2349 spin_unlock(&i915->gem.contexts.lock);
2350
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002351 err = gen8_configure_context(ctx, regs, array_size);
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002352 if (err) {
2353 i915_gem_context_put(ctx);
Chris Wilsona9877da2019-07-16 22:34:43 +01002354 return err;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002355 }
2356
2357 spin_lock(&i915->gem.contexts.lock);
2358 list_safe_reset_next(ctx, cn, link);
2359 i915_gem_context_put(ctx);
Robert Bragg19f81df2017-06-13 12:23:03 +01002360 }
Chris Wilsona4e7ccd2019-10-04 14:40:09 +01002361 spin_unlock(&i915->gem.contexts.lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01002362
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002363 /*
Chris Wilsona9877da2019-07-16 22:34:43 +01002364 * After updating all other contexts, we need to modify ourselves.
2365 * If we don't modify the kernel_context, we do not get events while
2366 * idle.
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002367 */
Chris Wilson750e76b2019-08-06 13:43:00 +01002368 for_each_uabi_engine(engine, i915) {
Chris Wilsona9877da2019-07-16 22:34:43 +01002369 struct intel_context *ce = engine->kernel_context;
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002370
Chris Wilsona9877da2019-07-16 22:34:43 +01002371 if (engine->class != RENDER_CLASS)
2372 continue;
2373
2374 regs[0].value = intel_sseu_make_rpcs(i915, &ce->sseu);
2375
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002376 err = gen8_modify_self(ce, regs, array_size);
Chris Wilsona9877da2019-07-16 22:34:43 +01002377 if (err)
2378 return err;
2379 }
Tvrtko Ursulin722f3de2018-09-12 16:29:30 +01002380
2381 return 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01002382}
2383
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002384static int gen8_enable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002385{
Chris Wilson52111c42019-10-10 16:05:20 +01002386 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03002387 struct i915_oa_config *oa_config = stream->oa_config;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002388 int ret;
Robert Bragg19f81df2017-06-13 12:23:03 +01002389
2390 /*
2391 * We disable slice/unslice clock ratio change reports on SKL since
2392 * they are too noisy. The HW generates a lot of redundant reports
2393 * where the ratio hasn't really changed causing a lot of redundant
2394 * work to processes and increasing the chances we'll hit buffer
2395 * overruns.
2396 *
2397 * Although we don't currently use the 'disable overrun' OABUFFER
2398 * feature it's worth noting that clock ratio reports have to be
2399 * disabled before considering to use that feature since the HW doesn't
2400 * correctly block these reports.
2401 *
2402 * Currently none of the high-level metrics we have depend on knowing
2403 * this ratio to normalize.
2404 *
2405 * Note: This register is not power context saved and restored, but
2406 * that's OK considering that we disable RC6 while the OA unit is
2407 * enabled.
2408 *
2409 * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
2410 * be read back from automatically triggered reports, as part of the
2411 * RPT_ID field.
2412 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002413 if (IS_GEN_RANGE(stream->perf->i915, 9, 11)) {
2414 intel_uncore_write(uncore, GEN8_OA_DEBUG,
2415 _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2416 GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
Robert Bragg19f81df2017-06-13 12:23:03 +01002417 }
2418
2419 /*
2420 * Update all contexts prior writing the mux configurations as we need
2421 * to make sure all slices/subslices are ON before writing to NOA
2422 * registers.
2423 */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002424 ret = lrc_configure_all_contexts(stream, oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01002425 if (ret)
2426 return ret;
2427
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03002428 return emit_oa_config(stream, oa_config, oa_context(stream));
Robert Bragg19f81df2017-06-13 12:23:03 +01002429}
2430
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002431static int gen12_enable_metric_set(struct i915_perf_stream *stream)
2432{
2433 struct intel_uncore *uncore = stream->uncore;
2434 struct i915_oa_config *oa_config = stream->oa_config;
2435 bool periodic = stream->periodic;
2436 u32 period_exponent = stream->period_exponent;
2437 int ret;
2438
2439 intel_uncore_write(uncore, GEN12_OAG_OA_DEBUG,
2440 /* Disable clk ratio reports, like previous Gens. */
2441 _MASKED_BIT_ENABLE(GEN12_OAG_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2442 GEN12_OAG_OA_DEBUG_INCLUDE_CLK_RATIO) |
2443 /*
2444 * If the user didn't require OA reports, instruct the
2445 * hardware not to emit ctx switch reports.
2446 */
2447 !(stream->sample_flags & SAMPLE_OA_REPORT) ?
2448 _MASKED_BIT_ENABLE(GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS) :
2449 _MASKED_BIT_DISABLE(GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS));
2450
2451 intel_uncore_write(uncore, GEN12_OAG_OAGLBCTXCTRL, periodic ?
2452 (GEN12_OAG_OAGLBCTXCTRL_COUNTER_RESUME |
2453 GEN12_OAG_OAGLBCTXCTRL_TIMER_ENABLE |
2454 (period_exponent << GEN12_OAG_OAGLBCTXCTRL_TIMER_PERIOD_SHIFT))
2455 : 0);
2456
2457 /*
2458 * Update all contexts prior writing the mux configurations as we need
2459 * to make sure all slices/subslices are ON before writing to NOA
2460 * registers.
2461 */
2462 ret = lrc_configure_all_contexts(stream, oa_config);
2463 if (ret)
2464 return ret;
2465
2466 /*
2467 * For Gen12, performance counters are context
2468 * saved/restored. Only enable it for the context that
2469 * requested this.
2470 */
2471 if (stream->ctx) {
2472 ret = gen12_emit_oar_config(stream->pinned_ctx,
2473 oa_config != NULL);
2474 if (ret)
2475 return ret;
2476 }
2477
2478 return emit_oa_config(stream, oa_config, oa_context(stream));
2479}
2480
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002481static void gen8_disable_metric_set(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002482{
Chris Wilson52111c42019-10-10 16:05:20 +01002483 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002484
Robert Bragg19f81df2017-06-13 12:23:03 +01002485 /* Reset all contexts' slices/subslices configurations. */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002486 lrc_configure_all_contexts(stream, NULL);
Lionel Landwerlin28964cf2017-08-03 17:58:10 +01002487
Chris Wilson8f8b1172019-10-07 22:09:41 +01002488 intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
Robert Bragg19f81df2017-06-13 12:23:03 +01002489}
2490
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002491static void gen10_disable_metric_set(struct i915_perf_stream *stream)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002492{
Chris Wilson52111c42019-10-10 16:05:20 +01002493 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002494
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002495 /* Reset all contexts' slices/subslices configurations. */
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002496 lrc_configure_all_contexts(stream, NULL);
2497
2498 /* Make sure we disable noa to save power. */
2499 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
2500}
2501
2502static void gen12_disable_metric_set(struct i915_perf_stream *stream)
2503{
2504 struct intel_uncore *uncore = stream->uncore;
2505
2506 /* Reset all contexts' slices/subslices configurations. */
2507 lrc_configure_all_contexts(stream, NULL);
2508
2509 /* disable the context save/restore or OAR counters */
2510 if (stream->ctx)
2511 gen12_emit_oar_config(stream->pinned_ctx, false);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002512
2513 /* Make sure we disable noa to save power. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002514 intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00002515}
2516
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002517static void gen7_oa_enable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002518{
Chris Wilson52111c42019-10-10 16:05:20 +01002519 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002520 struct i915_gem_context *ctx = stream->ctx;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002521 u32 ctx_id = stream->specific_ctx_id;
2522 bool periodic = stream->periodic;
2523 u32 period_exponent = stream->period_exponent;
2524 u32 report_format = stream->oa_buffer.format;
Lionel Landwerlin11051302018-03-26 10:08:23 +01002525
Robert Bragg1bef3402017-06-13 12:23:06 +01002526 /*
2527 * Reset buf pointers so we don't forward reports from before now.
2528 *
2529 * Think carefully if considering trying to avoid this, since it
2530 * also ensures status flags and the buffer itself are cleared
2531 * in error paths, and we have checks for invalid reports based
2532 * on the assumption that certain fields are written to zeroed
2533 * memory which this helps maintains.
2534 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002535 gen7_init_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002536
Chris Wilson8f8b1172019-10-07 22:09:41 +01002537 intel_uncore_write(uncore, GEN7_OACONTROL,
2538 (ctx_id & GEN7_OACONTROL_CTX_MASK) |
2539 (period_exponent <<
2540 GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
2541 (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
2542 (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
2543 (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
2544 GEN7_OACONTROL_ENABLE);
Robert Braggd7965152016-11-07 19:49:52 +00002545}
2546
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002547static void gen8_oa_enable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002548{
Chris Wilson52111c42019-10-10 16:05:20 +01002549 struct intel_uncore *uncore = stream->uncore;
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002550 u32 report_format = stream->oa_buffer.format;
Robert Bragg19f81df2017-06-13 12:23:03 +01002551
2552 /*
2553 * Reset buf pointers so we don't forward reports from before now.
2554 *
2555 * Think carefully if considering trying to avoid this, since it
2556 * also ensures status flags and the buffer itself are cleared
2557 * in error paths, and we have checks for invalid reports based
2558 * on the assumption that certain fields are written to zeroed
2559 * memory which this helps maintains.
2560 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002561 gen8_init_oa_buffer(stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002562
2563 /*
2564 * Note: we don't rely on the hardware to perform single context
2565 * filtering and instead filter on the cpu based on the context-id
2566 * field of reports
2567 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002568 intel_uncore_write(uncore, GEN8_OACONTROL,
2569 (report_format << GEN8_OA_REPORT_FORMAT_SHIFT) |
2570 GEN8_OA_COUNTER_ENABLE);
Robert Bragg19f81df2017-06-13 12:23:03 +01002571}
2572
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002573static void gen12_oa_enable(struct i915_perf_stream *stream)
2574{
2575 struct intel_uncore *uncore = stream->uncore;
2576 u32 report_format = stream->oa_buffer.format;
2577
2578 /*
2579 * If we don't want OA reports from the OA buffer, then we don't even
2580 * need to program the OAG unit.
2581 */
2582 if (!(stream->sample_flags & SAMPLE_OA_REPORT))
2583 return;
2584
2585 gen12_init_oa_buffer(stream);
2586
2587 intel_uncore_write(uncore, GEN12_OAG_OACONTROL,
2588 (report_format << GEN12_OAG_OACONTROL_OA_COUNTER_FORMAT_SHIFT) |
2589 GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE);
2590}
2591
Robert Bragg16d98b32016-12-07 21:40:33 +00002592/**
2593 * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
2594 * @stream: An i915 perf stream opened for OA metrics
2595 *
2596 * [Re]enables hardware periodic sampling according to the period configured
2597 * when opening the stream. This also starts a hrtimer that will periodically
2598 * check for data in the circular OA buffer for notifying userspace (e.g.
2599 * during a read() or poll()).
2600 */
Robert Braggd7965152016-11-07 19:49:52 +00002601static void i915_oa_stream_enable(struct i915_perf_stream *stream)
2602{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002603 stream->perf->ops.oa_enable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002604
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002605 if (stream->periodic)
2606 hrtimer_start(&stream->poll_check_timer,
Robert Braggd7965152016-11-07 19:49:52 +00002607 ns_to_ktime(POLL_PERIOD),
2608 HRTIMER_MODE_REL_PINNED);
2609}
2610
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002611static void gen7_oa_disable(struct i915_perf_stream *stream)
Robert Braggd7965152016-11-07 19:49:52 +00002612{
Chris Wilson52111c42019-10-10 16:05:20 +01002613 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002614
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002615 intel_uncore_write(uncore, GEN7_OACONTROL, 0);
2616 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002617 GEN7_OACONTROL, GEN7_OACONTROL_ENABLE, 0,
2618 50))
2619 DRM_ERROR("wait for OA to be disabled timed out\n");
Robert Braggd7965152016-11-07 19:49:52 +00002620}
2621
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002622static void gen8_oa_disable(struct i915_perf_stream *stream)
Robert Bragg19f81df2017-06-13 12:23:03 +01002623{
Chris Wilson52111c42019-10-10 16:05:20 +01002624 struct intel_uncore *uncore = stream->uncore;
Lionel Landwerlin5728de22018-10-23 11:07:06 +01002625
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002626 intel_uncore_write(uncore, GEN8_OACONTROL, 0);
2627 if (intel_wait_for_register(uncore,
Chris Wilsone896d292018-05-11 14:52:07 +01002628 GEN8_OACONTROL, GEN8_OA_COUNTER_ENABLE, 0,
2629 50))
2630 DRM_ERROR("wait for OA to be disabled timed out\n");
Robert Bragg19f81df2017-06-13 12:23:03 +01002631}
2632
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002633static void gen12_oa_disable(struct i915_perf_stream *stream)
2634{
2635 struct intel_uncore *uncore = stream->uncore;
2636
2637 intel_uncore_write(uncore, GEN12_OAG_OACONTROL, 0);
2638 if (intel_wait_for_register(uncore,
2639 GEN12_OAG_OACONTROL,
2640 GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE, 0,
2641 50))
2642 DRM_ERROR("wait for OA to be disabled timed out\n");
2643}
2644
Robert Bragg16d98b32016-12-07 21:40:33 +00002645/**
2646 * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
2647 * @stream: An i915 perf stream opened for OA metrics
2648 *
2649 * Stops the OA unit from periodically writing counter reports into the
2650 * circular OA buffer. This also stops the hrtimer that periodically checks for
2651 * data in the circular OA buffer, for notifying userspace.
2652 */
Robert Braggd7965152016-11-07 19:49:52 +00002653static void i915_oa_stream_disable(struct i915_perf_stream *stream)
2654{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002655 stream->perf->ops.oa_disable(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002656
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002657 if (stream->periodic)
2658 hrtimer_cancel(&stream->poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00002659}
2660
Robert Braggd7965152016-11-07 19:49:52 +00002661static const struct i915_perf_stream_ops i915_oa_stream_ops = {
2662 .destroy = i915_oa_stream_destroy,
2663 .enable = i915_oa_stream_enable,
2664 .disable = i915_oa_stream_disable,
2665 .wait_unlocked = i915_oa_wait_unlocked,
2666 .poll_wait = i915_oa_poll_wait,
2667 .read = i915_oa_read,
2668};
2669
Robert Bragg16d98b32016-12-07 21:40:33 +00002670/**
2671 * i915_oa_stream_init - validate combined props for OA stream and init
2672 * @stream: An i915 perf stream
2673 * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
2674 * @props: The property state that configures stream (individually validated)
2675 *
2676 * While read_properties_unlocked() validates properties in isolation it
2677 * doesn't ensure that the combination necessarily makes sense.
2678 *
2679 * At this point it has been determined that userspace wants a stream of
2680 * OA metrics, but still we need to further validate the combined
2681 * properties are OK.
2682 *
2683 * If the configuration makes sense then we can allocate memory for
2684 * a circular OA buffer and apply the requested metric set configuration.
2685 *
2686 * Returns: zero on success or a negative error code.
2687 */
Robert Braggd7965152016-11-07 19:49:52 +00002688static int i915_oa_stream_init(struct i915_perf_stream *stream,
2689 struct drm_i915_perf_open_param *param,
2690 struct perf_open_properties *props)
2691{
Chris Wilson8f8b1172019-10-07 22:09:41 +01002692 struct i915_perf *perf = stream->perf;
Robert Braggd7965152016-11-07 19:49:52 +00002693 int format_size;
2694 int ret;
2695
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002696 if (!props->engine) {
2697 DRM_DEBUG("OA engine not specified\n");
2698 return -EINVAL;
2699 }
2700
2701 /*
2702 * If the sysfs metrics/ directory wasn't registered for some
Robert Bragg442b8c02016-11-07 19:49:53 +00002703 * reason then don't let userspace try their luck with config
2704 * IDs
2705 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002706 if (!perf->metrics_kobj) {
Robert Bragg77085502016-12-01 17:21:52 +00002707 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
Robert Bragg442b8c02016-11-07 19:49:53 +00002708 return -EINVAL;
2709 }
2710
Robert Braggd7965152016-11-07 19:49:52 +00002711 if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
Robert Bragg77085502016-12-01 17:21:52 +00002712 DRM_DEBUG("Only OA report sampling supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002713 return -EINVAL;
2714 }
2715
Chris Wilson8f8b1172019-10-07 22:09:41 +01002716 if (!perf->ops.enable_metric_set) {
Robert Bragg77085502016-12-01 17:21:52 +00002717 DRM_DEBUG("OA unit not supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002718 return -ENODEV;
2719 }
2720
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002721 /*
2722 * To avoid the complexity of having to accurately filter
Robert Braggd7965152016-11-07 19:49:52 +00002723 * counter reports and marshal to the appropriate client
2724 * we currently only allow exclusive access
2725 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01002726 if (perf->exclusive_stream) {
Robert Bragg77085502016-12-01 17:21:52 +00002727 DRM_DEBUG("OA unit already in use\n");
Robert Braggd7965152016-11-07 19:49:52 +00002728 return -EBUSY;
2729 }
2730
Robert Braggd7965152016-11-07 19:49:52 +00002731 if (!props->oa_format) {
Robert Bragg77085502016-12-01 17:21:52 +00002732 DRM_DEBUG("OA report format not specified\n");
Robert Braggd7965152016-11-07 19:49:52 +00002733 return -EINVAL;
2734 }
2735
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002736 stream->engine = props->engine;
Chris Wilson52111c42019-10-10 16:05:20 +01002737 stream->uncore = stream->engine->gt->uncore;
Lionel Landwerlin9a613632019-10-10 16:05:19 +01002738
Robert Braggd7965152016-11-07 19:49:52 +00002739 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
2740
Chris Wilson8f8b1172019-10-07 22:09:41 +01002741 format_size = perf->oa_formats[props->oa_format].size;
Robert Braggd7965152016-11-07 19:49:52 +00002742
2743 stream->sample_flags |= SAMPLE_OA_REPORT;
2744 stream->sample_size += format_size;
2745
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002746 stream->oa_buffer.format_size = format_size;
2747 if (WARN_ON(stream->oa_buffer.format_size == 0))
Robert Braggd7965152016-11-07 19:49:52 +00002748 return -EINVAL;
2749
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01002750 stream->hold_preemption = props->hold_preemption;
2751
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002752 stream->oa_buffer.format =
Chris Wilson8f8b1172019-10-07 22:09:41 +01002753 perf->oa_formats[props->oa_format].format;
Robert Braggd7965152016-11-07 19:49:52 +00002754
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002755 stream->periodic = props->oa_periodic;
2756 if (stream->periodic)
2757 stream->period_exponent = props->oa_period_exponent;
Robert Braggd7965152016-11-07 19:49:52 +00002758
Robert Braggd7965152016-11-07 19:49:52 +00002759 if (stream->ctx) {
2760 ret = oa_get_render_ctx_id(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002761 if (ret) {
2762 DRM_DEBUG("Invalid context id to filter with\n");
Robert Braggd7965152016-11-07 19:49:52 +00002763 return ret;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002764 }
Robert Braggd7965152016-11-07 19:49:52 +00002765 }
2766
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01002767 ret = alloc_noa_wait(stream);
2768 if (ret) {
2769 DRM_DEBUG("Unable to allocate NOA wait batch buffer\n");
2770 goto err_noa_wait_alloc;
2771 }
2772
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002773 stream->oa_config = i915_perf_get_oa_config(perf, props->metrics_set);
2774 if (!stream->oa_config) {
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002775 DRM_DEBUG("Invalid OA config id=%i\n", props->metrics_set);
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002776 ret = -EINVAL;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002777 goto err_config;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002778 }
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002779
Robert Braggd7965152016-11-07 19:49:52 +00002780 /* PRM - observability performance counters:
2781 *
2782 * OACONTROL, performance counter enable, note:
2783 *
2784 * "When this bit is set, in order to have coherent counts,
2785 * RC6 power state and trunk clock gating must be disabled.
2786 * This can be achieved by programming MMIO registers as
2787 * 0xA094=0 and 0xA090[31]=1"
2788 *
2789 * In our case we are expecting that taking pm + FORCEWAKE
2790 * references will effectively disable RC6.
2791 */
Chris Wilsona5efcde2019-10-11 20:03:17 +01002792 intel_engine_pm_get(stream->engine);
Chris Wilson52111c42019-10-10 16:05:20 +01002793 intel_uncore_forcewake_get(stream->uncore, FORCEWAKE_ALL);
Robert Braggd7965152016-11-07 19:49:52 +00002794
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002795 ret = alloc_oa_buffer(stream);
sagar.a.kamble@intel.com987f8c42017-06-27 23:09:41 +05302796 if (ret)
2797 goto err_oa_buf_alloc;
2798
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002799 stream->ops = &i915_oa_stream_ops;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002800 perf->exclusive_stream = stream;
Lionel Landwerlinec431ea2019-02-05 09:50:29 +00002801
Chris Wilson8f8b1172019-10-07 22:09:41 +01002802 ret = perf->ops.enable_metric_set(stream);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002803 if (ret) {
2804 DRM_DEBUG("Unable to enable metric set\n");
Robert Braggd7965152016-11-07 19:49:52 +00002805 goto err_enable;
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01002806 }
Robert Braggd7965152016-11-07 19:49:52 +00002807
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002808 DRM_DEBUG("opening stream oa config uuid=%s\n",
2809 stream->oa_config->uuid);
2810
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002811 hrtimer_init(&stream->poll_check_timer,
2812 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2813 stream->poll_check_timer.function = oa_poll_check_timer_cb;
2814 init_waitqueue_head(&stream->poll_wq);
2815 spin_lock_init(&stream->oa_buffer.ptr_lock);
2816
Robert Braggd7965152016-11-07 19:49:52 +00002817 return 0;
2818
2819err_enable:
Chris Wilson8f8b1172019-10-07 22:09:41 +01002820 perf->exclusive_stream = NULL;
2821 perf->ops.disable_metric_set(stream);
Lionel Landwerlin41d3fdc2018-03-01 11:06:13 +00002822
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002823 free_oa_buffer(stream);
Robert Braggd7965152016-11-07 19:49:52 +00002824
2825err_oa_buf_alloc:
Lionel Landwerlin6a450082019-10-12 08:23:06 +01002826 free_oa_configs(stream);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002827
Chris Wilson52111c42019-10-10 16:05:20 +01002828 intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
Chris Wilsona5efcde2019-10-11 20:03:17 +01002829 intel_engine_pm_put(stream->engine);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002830
2831err_config:
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01002832 free_noa_wait(stream);
2833
2834err_noa_wait_alloc:
Robert Braggd7965152016-11-07 19:49:52 +00002835 if (stream->ctx)
2836 oa_put_render_ctx_id(stream);
2837
2838 return ret;
2839}
2840
Chris Wilson7dc56af2019-09-24 15:59:50 +01002841void i915_oa_init_reg_state(const struct intel_context *ce,
2842 const struct intel_engine_cs *engine)
Robert Bragg19f81df2017-06-13 12:23:03 +01002843{
Chris Wilson28b6cb02017-08-10 18:57:43 +01002844 struct i915_perf_stream *stream;
Robert Bragg19f81df2017-06-13 12:23:03 +01002845
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07002846 /* perf.exclusive_stream serialised by lrc_configure_all_contexts() */
Chris Wilsondffa8fe2019-08-30 19:19:29 +01002847
Chris Wilson8a68d462019-03-05 18:03:30 +00002848 if (engine->class != RENDER_CLASS)
Robert Bragg19f81df2017-06-13 12:23:03 +01002849 return;
2850
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002851 stream = engine->i915->perf.exclusive_stream;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002852 if (stream)
Chris Wilson7dc56af2019-09-24 15:59:50 +01002853 gen8_update_reg_state_unlocked(ce, stream);
Robert Bragg19f81df2017-06-13 12:23:03 +01002854}
2855
Robert Bragg16d98b32016-12-07 21:40:33 +00002856/**
2857 * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation
2858 * @stream: An i915 perf stream
2859 * @file: An i915 perf stream file
2860 * @buf: destination buffer given by userspace
2861 * @count: the number of bytes userspace wants to read
2862 * @ppos: (inout) file seek position (unused)
2863 *
2864 * Besides wrapping &i915_perf_stream_ops->read this provides a common place to
2865 * ensure that if we've successfully copied any data then reporting that takes
2866 * precedence over any internal error status, so the data isn't lost.
2867 *
2868 * For example ret will be -ENOSPC whenever there is more buffered data than
2869 * can be copied to userspace, but that's only interesting if we weren't able
2870 * to copy some data because it implies the userspace buffer is too small to
2871 * receive a single record (and we never split records).
2872 *
2873 * Another case with ret == -EFAULT is more of a grey area since it would seem
2874 * like bad form for userspace to ask us to overrun its buffer, but the user
2875 * knows best:
2876 *
2877 * http://yarchive.net/comp/linux/partial_reads_writes.html
2878 *
2879 * Returns: The number of bytes copied or a negative error code on failure.
2880 */
Robert Braggeec688e2016-11-07 19:49:47 +00002881static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
2882 struct file *file,
2883 char __user *buf,
2884 size_t count,
2885 loff_t *ppos)
2886{
2887 /* Note we keep the offset (aka bytes read) separate from any
2888 * error status so that the final check for whether we return
2889 * the bytes read with a higher precedence than any error (see
2890 * comment below) doesn't need to be handled/duplicated in
2891 * stream->ops->read() implementations.
2892 */
2893 size_t offset = 0;
2894 int ret = stream->ops->read(stream, buf, count, &offset);
2895
Robert Braggeec688e2016-11-07 19:49:47 +00002896 return offset ?: (ret ?: -EAGAIN);
2897}
2898
Robert Bragg16d98b32016-12-07 21:40:33 +00002899/**
2900 * i915_perf_read - handles read() FOP for i915 perf stream FDs
2901 * @file: An i915 perf stream file
2902 * @buf: destination buffer given by userspace
2903 * @count: the number of bytes userspace wants to read
2904 * @ppos: (inout) file seek position (unused)
2905 *
2906 * The entry point for handling a read() on a stream file descriptor from
2907 * userspace. Most of the work is left to the i915_perf_read_locked() and
2908 * &i915_perf_stream_ops->read but to save having stream implementations (of
2909 * which we might have multiple later) we handle blocking read here.
2910 *
2911 * We can also consistently treat trying to read from a disabled stream
2912 * as an IO error so implementations can assume the stream is enabled
2913 * while reading.
2914 *
2915 * Returns: The number of bytes copied or a negative error code on failure.
2916 */
Robert Braggeec688e2016-11-07 19:49:47 +00002917static ssize_t i915_perf_read(struct file *file,
2918 char __user *buf,
2919 size_t count,
2920 loff_t *ppos)
2921{
2922 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01002923 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00002924 ssize_t ret;
2925
Robert Braggd7965152016-11-07 19:49:52 +00002926 /* To ensure it's handled consistently we simply treat all reads of a
2927 * disabled stream as an error. In particular it might otherwise lead
2928 * to a deadlock for blocking file descriptors...
2929 */
2930 if (!stream->enabled)
2931 return -EIO;
2932
Robert Braggeec688e2016-11-07 19:49:47 +00002933 if (!(file->f_flags & O_NONBLOCK)) {
Robert Braggd7965152016-11-07 19:49:52 +00002934 /* There's the small chance of false positives from
2935 * stream->ops->wait_unlocked.
2936 *
2937 * E.g. with single context filtering since we only wait until
2938 * oabuffer has >= 1 report we don't immediately know whether
2939 * any reports really belong to the current context
Robert Braggeec688e2016-11-07 19:49:47 +00002940 */
2941 do {
2942 ret = stream->ops->wait_unlocked(stream);
2943 if (ret)
2944 return ret;
2945
Chris Wilson8f8b1172019-10-07 22:09:41 +01002946 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002947 ret = i915_perf_read_locked(stream, file,
2948 buf, count, ppos);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002949 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002950 } while (ret == -EAGAIN);
2951 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01002952 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002953 ret = i915_perf_read_locked(stream, file, buf, count, ppos);
Chris Wilson8f8b1172019-10-07 22:09:41 +01002954 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00002955 }
2956
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002957 /* We allow the poll checking to sometimes report false positive EPOLLIN
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002958 * events where we might actually report EAGAIN on read() if there's
2959 * not really any data available. In this situation though we don't
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002960 * want to enter a busy loop between poll() reporting a EPOLLIN event
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002961 * and read() returning -EAGAIN. Clearing the oa.pollin state here
2962 * effectively ensures we back off until the next hrtimer callback
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002963 * before reporting another EPOLLIN event.
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002964 */
2965 if (ret >= 0 || ret == -EAGAIN) {
Robert Braggd7965152016-11-07 19:49:52 +00002966 /* Maybe make ->pollin per-stream state if we support multiple
2967 * concurrent streams in the future.
2968 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002969 stream->pollin = false;
Robert Braggd7965152016-11-07 19:49:52 +00002970 }
2971
Robert Braggeec688e2016-11-07 19:49:47 +00002972 return ret;
2973}
2974
Robert Braggd7965152016-11-07 19:49:52 +00002975static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
2976{
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002977 struct i915_perf_stream *stream =
2978 container_of(hrtimer, typeof(*stream), poll_check_timer);
Robert Braggd7965152016-11-07 19:49:52 +00002979
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07002980 if (oa_buffer_check_unlocked(stream)) {
2981 stream->pollin = true;
2982 wake_up(&stream->poll_wq);
Robert Braggd7965152016-11-07 19:49:52 +00002983 }
2984
2985 hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
2986
2987 return HRTIMER_RESTART;
2988}
2989
Robert Bragg16d98b32016-12-07 21:40:33 +00002990/**
2991 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
Robert Bragg16d98b32016-12-07 21:40:33 +00002992 * @stream: An i915 perf stream
2993 * @file: An i915 perf stream file
2994 * @wait: poll() state table
2995 *
2996 * For handling userspace polling on an i915 perf stream, this calls through to
2997 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
2998 * will be woken for new stream data.
2999 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003000 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00003001 * with any non-file-operation driver hooks.
3002 *
3003 * Returns: any poll events that are ready without sleeping
3004 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003005static __poll_t i915_perf_poll_locked(struct i915_perf_stream *stream,
3006 struct file *file,
3007 poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00003008{
Al Viroafc9a422017-07-03 06:39:46 -04003009 __poll_t events = 0;
Robert Braggeec688e2016-11-07 19:49:47 +00003010
3011 stream->ops->poll_wait(stream, file, wait);
3012
Robert Braggd7965152016-11-07 19:49:52 +00003013 /* Note: we don't explicitly check whether there's something to read
3014 * here since this path may be very hot depending on what else
3015 * userspace is polling, or on the timeout in use. We rely solely on
3016 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
3017 * samples to read.
3018 */
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07003019 if (stream->pollin)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003020 events |= EPOLLIN;
Robert Braggeec688e2016-11-07 19:49:47 +00003021
Robert Braggd7965152016-11-07 19:49:52 +00003022 return events;
Robert Braggeec688e2016-11-07 19:49:47 +00003023}
3024
Robert Bragg16d98b32016-12-07 21:40:33 +00003025/**
3026 * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
3027 * @file: An i915 perf stream file
3028 * @wait: poll() state table
3029 *
3030 * For handling userspace polling on an i915 perf stream, this ensures
3031 * poll_wait() gets called with a wait queue that will be woken for new stream
3032 * data.
3033 *
3034 * Note: Implementation deferred to i915_perf_poll_locked()
3035 *
3036 * Returns: any poll events that are ready without sleeping
3037 */
Al Viroafc9a422017-07-03 06:39:46 -04003038static __poll_t i915_perf_poll(struct file *file, poll_table *wait)
Robert Braggeec688e2016-11-07 19:49:47 +00003039{
3040 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003041 struct i915_perf *perf = stream->perf;
Al Viroafc9a422017-07-03 06:39:46 -04003042 __poll_t ret;
Robert Braggeec688e2016-11-07 19:49:47 +00003043
Chris Wilson8f8b1172019-10-07 22:09:41 +01003044 mutex_lock(&perf->lock);
3045 ret = i915_perf_poll_locked(stream, file, wait);
3046 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003047
3048 return ret;
3049}
3050
Robert Bragg16d98b32016-12-07 21:40:33 +00003051/**
3052 * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
3053 * @stream: A disabled i915 perf stream
3054 *
3055 * [Re]enables the associated capture of data for this stream.
3056 *
3057 * If a stream was previously enabled then there's currently no intention
3058 * to provide userspace any guarantee about the preservation of previously
3059 * buffered data.
3060 */
Robert Braggeec688e2016-11-07 19:49:47 +00003061static void i915_perf_enable_locked(struct i915_perf_stream *stream)
3062{
3063 if (stream->enabled)
3064 return;
3065
3066 /* Allow stream->ops->enable() to refer to this */
3067 stream->enabled = true;
3068
3069 if (stream->ops->enable)
3070 stream->ops->enable(stream);
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003071
3072 if (stream->hold_preemption)
3073 i915_gem_context_set_nopreempt(stream->ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00003074}
3075
Robert Bragg16d98b32016-12-07 21:40:33 +00003076/**
3077 * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
3078 * @stream: An enabled i915 perf stream
3079 *
3080 * Disables the associated capture of data for this stream.
3081 *
3082 * The intention is that disabling an re-enabling a stream will ideally be
3083 * cheaper than destroying and re-opening a stream with the same configuration,
3084 * though there are no formal guarantees about what state or buffered data
3085 * must be retained between disabling and re-enabling a stream.
3086 *
3087 * Note: while a stream is disabled it's considered an error for userspace
3088 * to attempt to read from the stream (-EIO).
3089 */
Robert Braggeec688e2016-11-07 19:49:47 +00003090static void i915_perf_disable_locked(struct i915_perf_stream *stream)
3091{
3092 if (!stream->enabled)
3093 return;
3094
3095 /* Allow stream->ops->disable() to refer to this */
3096 stream->enabled = false;
3097
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003098 if (stream->hold_preemption)
3099 i915_gem_context_clear_nopreempt(stream->ctx);
3100
Robert Braggeec688e2016-11-07 19:49:47 +00003101 if (stream->ops->disable)
3102 stream->ops->disable(stream);
3103}
3104
Chris Wilson7831e9a2019-10-14 21:14:03 +01003105static long i915_perf_config_locked(struct i915_perf_stream *stream,
3106 unsigned long metrics_set)
3107{
3108 struct i915_oa_config *config;
3109 long ret = stream->oa_config->id;
3110
3111 config = i915_perf_get_oa_config(stream->perf, metrics_set);
3112 if (!config)
3113 return -EINVAL;
3114
3115 if (config != stream->oa_config) {
3116 int err;
3117
3118 /*
3119 * If OA is bound to a specific context, emit the
3120 * reconfiguration inline from that context. The update
3121 * will then be ordered with respect to submission on that
3122 * context.
3123 *
3124 * When set globally, we use a low priority kernel context,
3125 * so it will effectively take effect when idle.
3126 */
Lionel Landwerlin8814c6d2019-10-20 00:46:47 +03003127 err = emit_oa_config(stream, config, oa_context(stream));
Chris Wilson7831e9a2019-10-14 21:14:03 +01003128 if (err == 0)
3129 config = xchg(&stream->oa_config, config);
3130 else
3131 ret = err;
3132 }
3133
3134 i915_oa_config_put(config);
3135
3136 return ret;
3137}
3138
Robert Bragg16d98b32016-12-07 21:40:33 +00003139/**
3140 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
3141 * @stream: An i915 perf stream
3142 * @cmd: the ioctl request
3143 * @arg: the ioctl data
3144 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003145 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00003146 * with any non-file-operation driver hooks.
3147 *
3148 * Returns: zero on success or a negative error code. Returns -EINVAL for
3149 * an unknown ioctl request.
3150 */
Robert Braggeec688e2016-11-07 19:49:47 +00003151static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
3152 unsigned int cmd,
3153 unsigned long arg)
3154{
3155 switch (cmd) {
3156 case I915_PERF_IOCTL_ENABLE:
3157 i915_perf_enable_locked(stream);
3158 return 0;
3159 case I915_PERF_IOCTL_DISABLE:
3160 i915_perf_disable_locked(stream);
3161 return 0;
Chris Wilson7831e9a2019-10-14 21:14:03 +01003162 case I915_PERF_IOCTL_CONFIG:
3163 return i915_perf_config_locked(stream, arg);
Robert Braggeec688e2016-11-07 19:49:47 +00003164 }
3165
3166 return -EINVAL;
3167}
3168
Robert Bragg16d98b32016-12-07 21:40:33 +00003169/**
3170 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
3171 * @file: An i915 perf stream file
3172 * @cmd: the ioctl request
3173 * @arg: the ioctl data
3174 *
3175 * Implementation deferred to i915_perf_ioctl_locked().
3176 *
3177 * Returns: zero on success or a negative error code. Returns -EINVAL for
3178 * an unknown ioctl request.
3179 */
Robert Braggeec688e2016-11-07 19:49:47 +00003180static long i915_perf_ioctl(struct file *file,
3181 unsigned int cmd,
3182 unsigned long arg)
3183{
3184 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003185 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003186 long ret;
3187
Chris Wilson8f8b1172019-10-07 22:09:41 +01003188 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003189 ret = i915_perf_ioctl_locked(stream, cmd, arg);
Chris Wilson8f8b1172019-10-07 22:09:41 +01003190 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003191
3192 return ret;
3193}
3194
Robert Bragg16d98b32016-12-07 21:40:33 +00003195/**
3196 * i915_perf_destroy_locked - destroy an i915 perf stream
3197 * @stream: An i915 perf stream
3198 *
3199 * Frees all resources associated with the given i915 perf @stream, disabling
3200 * any associated data capture in the process.
3201 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01003202 * Note: The &perf->lock mutex has been taken to serialize
Robert Bragg16d98b32016-12-07 21:40:33 +00003203 * with any non-file-operation driver hooks.
3204 */
Robert Braggeec688e2016-11-07 19:49:47 +00003205static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
3206{
Robert Braggeec688e2016-11-07 19:49:47 +00003207 if (stream->enabled)
3208 i915_perf_disable_locked(stream);
3209
3210 if (stream->ops->destroy)
3211 stream->ops->destroy(stream);
3212
Chris Wilson69df05e2016-12-18 15:37:21 +00003213 if (stream->ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01003214 i915_gem_context_put(stream->ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00003215
3216 kfree(stream);
3217}
3218
Robert Bragg16d98b32016-12-07 21:40:33 +00003219/**
3220 * i915_perf_release - handles userspace close() of a stream file
3221 * @inode: anonymous inode associated with file
3222 * @file: An i915 perf stream file
3223 *
3224 * Cleans up any resources associated with an open i915 perf stream file.
3225 *
3226 * NB: close() can't really fail from the userspace point of view.
3227 *
3228 * Returns: zero on success or a negative error code.
3229 */
Robert Braggeec688e2016-11-07 19:49:47 +00003230static int i915_perf_release(struct inode *inode, struct file *file)
3231{
3232 struct i915_perf_stream *stream = file->private_data;
Chris Wilson8f8b1172019-10-07 22:09:41 +01003233 struct i915_perf *perf = stream->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003234
Chris Wilson8f8b1172019-10-07 22:09:41 +01003235 mutex_lock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003236 i915_perf_destroy_locked(stream);
Chris Wilson8f8b1172019-10-07 22:09:41 +01003237 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003238
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003239 /* Release the reference the perf stream kept on the driver. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003240 drm_dev_put(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003241
Robert Braggeec688e2016-11-07 19:49:47 +00003242 return 0;
3243}
3244
3245
3246static const struct file_operations fops = {
3247 .owner = THIS_MODULE,
3248 .llseek = no_llseek,
3249 .release = i915_perf_release,
3250 .poll = i915_perf_poll,
3251 .read = i915_perf_read,
3252 .unlocked_ioctl = i915_perf_ioctl,
Lionel Landwerlin191f8962017-10-24 16:27:28 +01003253 /* Our ioctl have no arguments, so it's safe to use the same function
3254 * to handle 32bits compatibility.
3255 */
3256 .compat_ioctl = i915_perf_ioctl,
Robert Braggeec688e2016-11-07 19:49:47 +00003257};
3258
3259
Robert Bragg16d98b32016-12-07 21:40:33 +00003260/**
3261 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
Chris Wilson8f8b1172019-10-07 22:09:41 +01003262 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003263 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
3264 * @props: individually validated u64 property value pairs
3265 * @file: drm file
3266 *
3267 * See i915_perf_ioctl_open() for interface details.
3268 *
3269 * Implements further stream config validation and stream initialization on
Chris Wilson8f8b1172019-10-07 22:09:41 +01003270 * behalf of i915_perf_open_ioctl() with the &perf->lock mutex
Robert Bragg16d98b32016-12-07 21:40:33 +00003271 * taken to serialize with any non-file-operation driver hooks.
3272 *
3273 * Note: at this point the @props have only been validated in isolation and
3274 * it's still necessary to validate that the combination of properties makes
3275 * sense.
3276 *
3277 * In the case where userspace is interested in OA unit metrics then further
3278 * config validation and stream initialization details will be handled by
3279 * i915_oa_stream_init(). The code here should only validate config state that
3280 * will be relevant to all stream types / backends.
3281 *
3282 * Returns: zero on success or a negative error code.
3283 */
Robert Braggeec688e2016-11-07 19:49:47 +00003284static int
Chris Wilson8f8b1172019-10-07 22:09:41 +01003285i915_perf_open_ioctl_locked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00003286 struct drm_i915_perf_open_param *param,
3287 struct perf_open_properties *props,
3288 struct drm_file *file)
3289{
3290 struct i915_gem_context *specific_ctx = NULL;
3291 struct i915_perf_stream *stream = NULL;
3292 unsigned long f_flags = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01003293 bool privileged_op = true;
Robert Braggeec688e2016-11-07 19:49:47 +00003294 int stream_fd;
3295 int ret;
3296
3297 if (props->single_context) {
3298 u32 ctx_handle = props->ctx_handle;
3299 struct drm_i915_file_private *file_priv = file->driver_priv;
3300
Imre Deak635f56c2017-07-14 18:12:41 +03003301 specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle);
3302 if (!specific_ctx) {
3303 DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n",
3304 ctx_handle);
3305 ret = -ENOENT;
Robert Braggeec688e2016-11-07 19:49:47 +00003306 goto err;
3307 }
3308 }
3309
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003310 if (props->hold_preemption) {
3311 if (!props->single_context) {
3312 DRM_DEBUG("preemption disable with no context\n");
3313 ret = -EINVAL;
3314 goto err;
3315 }
3316 privileged_op = true;
3317 }
3318
Robert Bragg19f81df2017-06-13 12:23:03 +01003319 /*
3320 * On Haswell the OA unit supports clock gating off for a specific
3321 * context and in this mode there's no visibility of metrics for the
3322 * rest of the system, which we consider acceptable for a
3323 * non-privileged client.
3324 *
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003325 * For Gen8->11 the OA unit no longer supports clock gating off for a
Robert Bragg19f81df2017-06-13 12:23:03 +01003326 * specific context and the kernel can't securely stop the counters
3327 * from updating as system-wide / global values. Even though we can
3328 * filter reports based on the included context ID we can't block
3329 * clients from seeing the raw / global counter values via
3330 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
3331 * enable the OA unit by default.
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003332 *
3333 * For Gen12+ we gain a new OAR unit that only monitors the RCS on a
3334 * per context basis. So we can relax requirements there if the user
3335 * doesn't request global stream access (i.e. query based sampling
3336 * using MI_RECORD_PERF_COUNT.
Robert Bragg19f81df2017-06-13 12:23:03 +01003337 */
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003338 if (IS_HASWELL(perf->i915) && specific_ctx && !props->hold_preemption)
Robert Bragg19f81df2017-06-13 12:23:03 +01003339 privileged_op = false;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003340 else if (IS_GEN(perf->i915, 12) && specific_ctx &&
3341 (props->sample_flags & SAMPLE_OA_REPORT) == 0)
3342 privileged_op = false;
Robert Bragg19f81df2017-06-13 12:23:03 +01003343
Robert Braggccdf6342016-11-07 19:49:54 +00003344 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
3345 * we check a dev.i915.perf_stream_paranoid sysctl option
3346 * to determine if it's ok to access system wide OA counters
3347 * without CAP_SYS_ADMIN privileges.
3348 */
Robert Bragg19f81df2017-06-13 12:23:03 +01003349 if (privileged_op &&
Robert Braggccdf6342016-11-07 19:49:54 +00003350 i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003351 DRM_DEBUG("Insufficient privileges to open i915 perf stream\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003352 ret = -EACCES;
3353 goto err_ctx;
3354 }
3355
3356 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
3357 if (!stream) {
3358 ret = -ENOMEM;
3359 goto err_ctx;
3360 }
3361
Chris Wilson8f8b1172019-10-07 22:09:41 +01003362 stream->perf = perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003363 stream->ctx = specific_ctx;
3364
Robert Braggd7965152016-11-07 19:49:52 +00003365 ret = i915_oa_stream_init(stream, param, props);
3366 if (ret)
3367 goto err_alloc;
3368
3369 /* we avoid simply assigning stream->sample_flags = props->sample_flags
3370 * to have _stream_init check the combination of sample flags more
3371 * thoroughly, but still this is the expected result at this point.
Robert Braggeec688e2016-11-07 19:49:47 +00003372 */
Robert Braggd7965152016-11-07 19:49:52 +00003373 if (WARN_ON(stream->sample_flags != props->sample_flags)) {
3374 ret = -ENODEV;
Matthew Auld22f880c2017-03-27 21:34:59 +01003375 goto err_flags;
Robert Braggd7965152016-11-07 19:49:52 +00003376 }
Robert Braggeec688e2016-11-07 19:49:47 +00003377
Robert Braggeec688e2016-11-07 19:49:47 +00003378 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
3379 f_flags |= O_CLOEXEC;
3380 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
3381 f_flags |= O_NONBLOCK;
3382
3383 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
3384 if (stream_fd < 0) {
3385 ret = stream_fd;
Lionel Landwerlin23b9e412019-10-08 15:01:11 +01003386 goto err_flags;
Robert Braggeec688e2016-11-07 19:49:47 +00003387 }
3388
3389 if (!(param->flags & I915_PERF_FLAG_DISABLED))
3390 i915_perf_enable_locked(stream);
3391
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003392 /* Take a reference on the driver that will be kept with stream_fd
3393 * until its release.
3394 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003395 drm_dev_get(&perf->i915->drm);
Lionel Landwerlina5af1df2019-07-09 15:33:39 +03003396
Robert Braggeec688e2016-11-07 19:49:47 +00003397 return stream_fd;
3398
Matthew Auld22f880c2017-03-27 21:34:59 +01003399err_flags:
Robert Braggeec688e2016-11-07 19:49:47 +00003400 if (stream->ops->destroy)
3401 stream->ops->destroy(stream);
3402err_alloc:
3403 kfree(stream);
3404err_ctx:
Chris Wilson69df05e2016-12-18 15:37:21 +00003405 if (specific_ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01003406 i915_gem_context_put(specific_ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00003407err:
3408 return ret;
3409}
3410
Chris Wilson8f8b1172019-10-07 22:09:41 +01003411static u64 oa_exponent_to_ns(struct i915_perf *perf, int exponent)
Robert Bragg155e9412017-06-13 12:23:05 +01003412{
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01003413 return div64_u64(1000000000ULL * (2ULL << exponent),
Chris Wilson8f8b1172019-10-07 22:09:41 +01003414 1000ULL * RUNTIME_INFO(perf->i915)->cs_timestamp_frequency_khz);
Robert Bragg155e9412017-06-13 12:23:05 +01003415}
3416
Robert Bragg16d98b32016-12-07 21:40:33 +00003417/**
3418 * read_properties_unlocked - validate + copy userspace stream open properties
Chris Wilson8f8b1172019-10-07 22:09:41 +01003419 * @perf: i915 perf instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003420 * @uprops: The array of u64 key value pairs given by userspace
3421 * @n_props: The number of key value pairs expected in @uprops
3422 * @props: The stream configuration built up while validating properties
Robert Braggeec688e2016-11-07 19:49:47 +00003423 *
3424 * Note this function only validates properties in isolation it doesn't
3425 * validate that the combination of properties makes sense or that all
3426 * properties necessary for a particular kind of stream have been set.
Robert Bragg16d98b32016-12-07 21:40:33 +00003427 *
3428 * Note that there currently aren't any ordering requirements for properties so
3429 * we shouldn't validate or assume anything about ordering here. This doesn't
3430 * rule out defining new properties with ordering requirements in the future.
Robert Braggeec688e2016-11-07 19:49:47 +00003431 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003432static int read_properties_unlocked(struct i915_perf *perf,
Robert Braggeec688e2016-11-07 19:49:47 +00003433 u64 __user *uprops,
3434 u32 n_props,
3435 struct perf_open_properties *props)
3436{
3437 u64 __user *uprop = uprops;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003438 u32 i;
Robert Braggeec688e2016-11-07 19:49:47 +00003439
3440 memset(props, 0, sizeof(struct perf_open_properties));
3441
3442 if (!n_props) {
Robert Bragg77085502016-12-01 17:21:52 +00003443 DRM_DEBUG("No i915 perf properties given\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003444 return -EINVAL;
3445 }
3446
Lionel Landwerlin9a613632019-10-10 16:05:19 +01003447 /* At the moment we only support using i915-perf on the RCS. */
3448 props->engine = intel_engine_lookup_user(perf->i915,
3449 I915_ENGINE_CLASS_RENDER,
3450 0);
3451 if (!props->engine) {
3452 DRM_DEBUG("No RENDER-capable engines\n");
3453 return -EINVAL;
3454 }
3455
Robert Braggeec688e2016-11-07 19:49:47 +00003456 /* Considering that ID = 0 is reserved and assuming that we don't
3457 * (currently) expect any configurations to ever specify duplicate
3458 * values for a particular property ID then the last _PROP_MAX value is
3459 * one greater than the maximum number of properties we expect to get
3460 * from userspace.
3461 */
3462 if (n_props >= DRM_I915_PERF_PROP_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00003463 DRM_DEBUG("More i915 perf properties specified than exist\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003464 return -EINVAL;
3465 }
3466
3467 for (i = 0; i < n_props; i++) {
Robert Bragg00319ba2016-11-07 19:49:55 +00003468 u64 oa_period, oa_freq_hz;
Robert Braggeec688e2016-11-07 19:49:47 +00003469 u64 id, value;
3470 int ret;
3471
3472 ret = get_user(id, uprop);
3473 if (ret)
3474 return ret;
3475
3476 ret = get_user(value, uprop + 1);
3477 if (ret)
3478 return ret;
3479
Matthew Auld0a309f92017-03-27 21:32:36 +01003480 if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
3481 DRM_DEBUG("Unknown i915 perf property ID\n");
3482 return -EINVAL;
3483 }
3484
Robert Braggeec688e2016-11-07 19:49:47 +00003485 switch ((enum drm_i915_perf_property_id)id) {
3486 case DRM_I915_PERF_PROP_CTX_HANDLE:
3487 props->single_context = 1;
3488 props->ctx_handle = value;
3489 break;
Robert Braggd7965152016-11-07 19:49:52 +00003490 case DRM_I915_PERF_PROP_SAMPLE_OA:
Lionel Landwerlinb6dd47b2018-03-26 10:08:22 +01003491 if (value)
3492 props->sample_flags |= SAMPLE_OA_REPORT;
Robert Braggd7965152016-11-07 19:49:52 +00003493 break;
3494 case DRM_I915_PERF_PROP_OA_METRICS_SET:
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003495 if (value == 0) {
Robert Bragg77085502016-12-01 17:21:52 +00003496 DRM_DEBUG("Unknown OA metric set ID\n");
Robert Braggd7965152016-11-07 19:49:52 +00003497 return -EINVAL;
3498 }
3499 props->metrics_set = value;
3500 break;
3501 case DRM_I915_PERF_PROP_OA_FORMAT:
3502 if (value == 0 || value >= I915_OA_FORMAT_MAX) {
Robert Bragg52c57c22017-05-11 16:43:29 +01003503 DRM_DEBUG("Out-of-range OA report format %llu\n",
3504 value);
Robert Braggd7965152016-11-07 19:49:52 +00003505 return -EINVAL;
3506 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01003507 if (!perf->oa_formats[value].size) {
Robert Bragg52c57c22017-05-11 16:43:29 +01003508 DRM_DEBUG("Unsupported OA report format %llu\n",
3509 value);
Robert Braggd7965152016-11-07 19:49:52 +00003510 return -EINVAL;
3511 }
3512 props->oa_format = value;
3513 break;
3514 case DRM_I915_PERF_PROP_OA_EXPONENT:
3515 if (value > OA_EXPONENT_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00003516 DRM_DEBUG("OA timer exponent too high (> %u)\n",
3517 OA_EXPONENT_MAX);
Robert Braggd7965152016-11-07 19:49:52 +00003518 return -EINVAL;
3519 }
3520
Robert Bragg00319ba2016-11-07 19:49:55 +00003521 /* Theoretically we can program the OA unit to sample
Robert Bragg155e9412017-06-13 12:23:05 +01003522 * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
3523 * for BXT. We don't allow such high sampling
3524 * frequencies by default unless root.
Robert Braggd7965152016-11-07 19:49:52 +00003525 */
Robert Bragg155e9412017-06-13 12:23:05 +01003526
Robert Bragg00319ba2016-11-07 19:49:55 +00003527 BUILD_BUG_ON(sizeof(oa_period) != 8);
Chris Wilson8f8b1172019-10-07 22:09:41 +01003528 oa_period = oa_exponent_to_ns(perf, value);
Robert Bragg00319ba2016-11-07 19:49:55 +00003529
3530 /* This check is primarily to ensure that oa_period <=
3531 * UINT32_MAX (before passing to do_div which only
3532 * accepts a u32 denominator), but we can also skip
3533 * checking anything < 1Hz which implicitly can't be
3534 * limited via an integer oa_max_sample_rate.
3535 */
3536 if (oa_period <= NSEC_PER_SEC) {
3537 u64 tmp = NSEC_PER_SEC;
3538 do_div(tmp, oa_period);
3539 oa_freq_hz = tmp;
3540 } else
3541 oa_freq_hz = 0;
3542
3543 if (oa_freq_hz > i915_oa_max_sample_rate &&
3544 !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00003545 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 +00003546 i915_oa_max_sample_rate);
Robert Braggd7965152016-11-07 19:49:52 +00003547 return -EACCES;
3548 }
3549
3550 props->oa_periodic = true;
3551 props->oa_period_exponent = value;
3552 break;
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01003553 case DRM_I915_PERF_PROP_HOLD_PREEMPTION:
3554 props->hold_preemption = !!value;
3555 break;
Matthew Auld0a309f92017-03-27 21:32:36 +01003556 case DRM_I915_PERF_PROP_MAX:
Robert Braggeec688e2016-11-07 19:49:47 +00003557 MISSING_CASE(id);
Robert Braggeec688e2016-11-07 19:49:47 +00003558 return -EINVAL;
3559 }
3560
3561 uprop += 2;
3562 }
3563
3564 return 0;
3565}
3566
Robert Bragg16d98b32016-12-07 21:40:33 +00003567/**
3568 * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
3569 * @dev: drm device
3570 * @data: ioctl data copied from userspace (unvalidated)
3571 * @file: drm file
3572 *
3573 * Validates the stream open parameters given by userspace including flags
3574 * and an array of u64 key, value pair properties.
3575 *
3576 * Very little is assumed up front about the nature of the stream being
3577 * opened (for instance we don't assume it's for periodic OA unit metrics). An
3578 * i915-perf stream is expected to be a suitable interface for other forms of
3579 * buffered data written by the GPU besides periodic OA metrics.
3580 *
3581 * Note we copy the properties from userspace outside of the i915 perf
3582 * mutex to avoid an awkward lockdep with mmap_sem.
3583 *
3584 * Most of the implementation details are handled by
Chris Wilson8f8b1172019-10-07 22:09:41 +01003585 * i915_perf_open_ioctl_locked() after taking the &perf->lock
Robert Bragg16d98b32016-12-07 21:40:33 +00003586 * mutex for serializing with any non-file-operation driver hooks.
3587 *
3588 * Return: A newly opened i915 Perf stream file descriptor or negative
3589 * error code on failure.
3590 */
Robert Braggeec688e2016-11-07 19:49:47 +00003591int i915_perf_open_ioctl(struct drm_device *dev, void *data,
3592 struct drm_file *file)
3593{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003594 struct i915_perf *perf = &to_i915(dev)->perf;
Robert Braggeec688e2016-11-07 19:49:47 +00003595 struct drm_i915_perf_open_param *param = data;
3596 struct perf_open_properties props;
3597 u32 known_open_flags;
3598 int ret;
3599
Chris Wilson8f8b1172019-10-07 22:09:41 +01003600 if (!perf->i915) {
Robert Bragg77085502016-12-01 17:21:52 +00003601 DRM_DEBUG("i915 perf interface not available for this system\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003602 return -ENOTSUPP;
3603 }
3604
3605 known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
3606 I915_PERF_FLAG_FD_NONBLOCK |
3607 I915_PERF_FLAG_DISABLED;
3608 if (param->flags & ~known_open_flags) {
Robert Bragg77085502016-12-01 17:21:52 +00003609 DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n");
Robert Braggeec688e2016-11-07 19:49:47 +00003610 return -EINVAL;
3611 }
3612
Chris Wilson8f8b1172019-10-07 22:09:41 +01003613 ret = read_properties_unlocked(perf,
Robert Braggeec688e2016-11-07 19:49:47 +00003614 u64_to_user_ptr(param->properties_ptr),
3615 param->num_properties,
3616 &props);
3617 if (ret)
3618 return ret;
3619
Chris Wilson8f8b1172019-10-07 22:09:41 +01003620 mutex_lock(&perf->lock);
3621 ret = i915_perf_open_ioctl_locked(perf, param, &props, file);
3622 mutex_unlock(&perf->lock);
Robert Braggeec688e2016-11-07 19:49:47 +00003623
3624 return ret;
3625}
3626
Robert Bragg16d98b32016-12-07 21:40:33 +00003627/**
3628 * i915_perf_register - exposes i915-perf to userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01003629 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003630 *
3631 * In particular OA metric sets are advertised under a sysfs metrics/
3632 * directory allowing userspace to enumerate valid IDs that can be
3633 * used to open an i915-perf stream.
3634 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003635void i915_perf_register(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003636{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003637 struct i915_perf *perf = &i915->perf;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003638 int ret;
3639
Chris Wilson8f8b1172019-10-07 22:09:41 +01003640 if (!perf->i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003641 return;
3642
3643 /* To be sure we're synchronized with an attempted
3644 * i915_perf_open_ioctl(); considering that we register after
3645 * being exposed to userspace.
3646 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003647 mutex_lock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003648
Chris Wilson8f8b1172019-10-07 22:09:41 +01003649 perf->metrics_kobj =
Robert Bragg442b8c02016-11-07 19:49:53 +00003650 kobject_create_and_add("metrics",
Chris Wilson8f8b1172019-10-07 22:09:41 +01003651 &i915->drm.primary->kdev->kobj);
3652 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003653 goto exit;
3654
Chris Wilson8f8b1172019-10-07 22:09:41 +01003655 sysfs_attr_init(&perf->test_config.sysfs_metric_id.attr);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003656
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003657 if (IS_TIGERLAKE(i915)) {
3658 i915_perf_load_test_config_tgl(i915);
3659 } else if (INTEL_GEN(i915) >= 11) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01003660 i915_perf_load_test_config_icl(i915);
3661 } else if (IS_CANNONLAKE(i915)) {
3662 i915_perf_load_test_config_cnl(i915);
3663 } else if (IS_COFFEELAKE(i915)) {
3664 if (IS_CFL_GT2(i915))
3665 i915_perf_load_test_config_cflgt2(i915);
3666 if (IS_CFL_GT3(i915))
3667 i915_perf_load_test_config_cflgt3(i915);
3668 } else if (IS_GEMINILAKE(i915)) {
3669 i915_perf_load_test_config_glk(i915);
3670 } else if (IS_KABYLAKE(i915)) {
3671 if (IS_KBL_GT2(i915))
3672 i915_perf_load_test_config_kblgt2(i915);
3673 else if (IS_KBL_GT3(i915))
3674 i915_perf_load_test_config_kblgt3(i915);
3675 } else if (IS_BROXTON(i915)) {
3676 i915_perf_load_test_config_bxt(i915);
3677 } else if (IS_SKYLAKE(i915)) {
3678 if (IS_SKL_GT2(i915))
3679 i915_perf_load_test_config_sklgt2(i915);
3680 else if (IS_SKL_GT3(i915))
3681 i915_perf_load_test_config_sklgt3(i915);
3682 else if (IS_SKL_GT4(i915))
3683 i915_perf_load_test_config_sklgt4(i915);
3684 } else if (IS_CHERRYVIEW(i915)) {
3685 i915_perf_load_test_config_chv(i915);
3686 } else if (IS_BROADWELL(i915)) {
3687 i915_perf_load_test_config_bdw(i915);
3688 } else if (IS_HASWELL(i915)) {
3689 i915_perf_load_test_config_hsw(i915);
3690 }
Robert Bragg442b8c02016-11-07 19:49:53 +00003691
Chris Wilson8f8b1172019-10-07 22:09:41 +01003692 if (perf->test_config.id == 0)
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003693 goto sysfs_error;
3694
Chris Wilson8f8b1172019-10-07 22:09:41 +01003695 ret = sysfs_create_group(perf->metrics_kobj,
3696 &perf->test_config.sysfs_metric);
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003697 if (ret)
3698 goto sysfs_error;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003699
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003700 perf->test_config.perf = perf;
3701 kref_init(&perf->test_config.ref);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003702
Robert Bragg19f81df2017-06-13 12:23:03 +01003703 goto exit;
3704
3705sysfs_error:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003706 kobject_put(perf->metrics_kobj);
3707 perf->metrics_kobj = NULL;
Robert Bragg19f81df2017-06-13 12:23:03 +01003708
Robert Bragg442b8c02016-11-07 19:49:53 +00003709exit:
Chris Wilson8f8b1172019-10-07 22:09:41 +01003710 mutex_unlock(&perf->lock);
Robert Bragg442b8c02016-11-07 19:49:53 +00003711}
3712
Robert Bragg16d98b32016-12-07 21:40:33 +00003713/**
3714 * i915_perf_unregister - hide i915-perf from userspace
Chris Wilson8f8b1172019-10-07 22:09:41 +01003715 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00003716 *
3717 * i915-perf state cleanup is split up into an 'unregister' and
3718 * 'deinit' phase where the interface is first hidden from
3719 * userspace by i915_perf_unregister() before cleaning up
3720 * remaining state in i915_perf_fini().
3721 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01003722void i915_perf_unregister(struct drm_i915_private *i915)
Robert Bragg442b8c02016-11-07 19:49:53 +00003723{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003724 struct i915_perf *perf = &i915->perf;
3725
3726 if (!perf->metrics_kobj)
Robert Bragg442b8c02016-11-07 19:49:53 +00003727 return;
3728
Chris Wilson8f8b1172019-10-07 22:09:41 +01003729 sysfs_remove_group(perf->metrics_kobj,
3730 &perf->test_config.sysfs_metric);
Robert Bragg442b8c02016-11-07 19:49:53 +00003731
Chris Wilson8f8b1172019-10-07 22:09:41 +01003732 kobject_put(perf->metrics_kobj);
3733 perf->metrics_kobj = NULL;
Robert Bragg442b8c02016-11-07 19:49:53 +00003734}
3735
Chris Wilson8f8b1172019-10-07 22:09:41 +01003736static bool gen8_is_valid_flex_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003737{
3738 static const i915_reg_t flex_eu_regs[] = {
3739 EU_PERF_CNTL0,
3740 EU_PERF_CNTL1,
3741 EU_PERF_CNTL2,
3742 EU_PERF_CNTL3,
3743 EU_PERF_CNTL4,
3744 EU_PERF_CNTL5,
3745 EU_PERF_CNTL6,
3746 };
3747 int i;
3748
3749 for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) {
Lionel Landwerlin7c52a222017-11-13 23:34:52 +00003750 if (i915_mmio_reg_offset(flex_eu_regs[i]) == addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003751 return true;
3752 }
3753 return false;
3754}
3755
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003756#define ADDR_IN_RANGE(addr, start, end) \
3757 ((addr) >= (start) && \
3758 (addr) <= (end))
3759
3760#define REG_IN_RANGE(addr, start, end) \
3761 ((addr) >= i915_mmio_reg_offset(start) && \
3762 (addr) <= i915_mmio_reg_offset(end))
3763
3764#define REG_EQUAL(addr, mmio) \
3765 ((addr) == i915_mmio_reg_offset(mmio))
3766
Chris Wilson8f8b1172019-10-07 22:09:41 +01003767static bool gen7_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003768{
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003769 return REG_IN_RANGE(addr, OASTARTTRIG1, OASTARTTRIG8) ||
3770 REG_IN_RANGE(addr, OAREPORTTRIG1, OAREPORTTRIG8) ||
3771 REG_IN_RANGE(addr, OACEC0_0, OACEC7_1);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003772}
3773
Chris Wilson8f8b1172019-10-07 22:09:41 +01003774static bool gen7_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003775{
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003776 return REG_EQUAL(addr, HALF_SLICE_CHICKEN2) ||
3777 REG_IN_RANGE(addr, MICRO_BP0_0, NOA_WRITE) ||
3778 REG_IN_RANGE(addr, OA_PERFCNT1_LO, OA_PERFCNT2_HI) ||
3779 REG_IN_RANGE(addr, OA_PERFMATRIX_LO, OA_PERFMATRIX_HI);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003780}
3781
Chris Wilson8f8b1172019-10-07 22:09:41 +01003782static bool gen8_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003783{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003784 return gen7_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003785 REG_EQUAL(addr, WAIT_FOR_RC6_EXIT) ||
3786 REG_IN_RANGE(addr, RPM_CONFIG0, NOA_CONFIG(8));
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003787}
3788
Chris Wilson8f8b1172019-10-07 22:09:41 +01003789static bool gen10_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003790{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003791 return gen8_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003792 REG_EQUAL(addr, GEN10_NOA_WRITE_HIGH) ||
3793 REG_IN_RANGE(addr, OA_PERFCNT3_LO, OA_PERFCNT4_HI);
Lionel Landwerlin95690a02017-11-10 19:08:43 +00003794}
3795
Chris Wilson8f8b1172019-10-07 22:09:41 +01003796static bool hsw_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003797{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003798 return gen7_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003799 ADDR_IN_RANGE(addr, 0x25100, 0x2FF90) ||
3800 REG_IN_RANGE(addr, HSW_MBVID2_NOA0, HSW_MBVID2_NOA9) ||
3801 REG_EQUAL(addr, HSW_MBVID2_MISR0);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003802}
3803
Chris Wilson8f8b1172019-10-07 22:09:41 +01003804static bool chv_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003805{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003806 return gen7_is_valid_mux_addr(perf, addr) ||
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003807 ADDR_IN_RANGE(addr, 0x182300, 0x1823A4);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003808}
3809
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07003810static bool gen12_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
3811{
3812 return REG_IN_RANGE(addr, GEN12_OAG_OASTARTTRIG1, GEN12_OAG_OASTARTTRIG8) ||
3813 REG_IN_RANGE(addr, GEN12_OAG_OAREPORTTRIG1, GEN12_OAG_OAREPORTTRIG8) ||
3814 REG_IN_RANGE(addr, GEN12_OAG_CEC0_0, GEN12_OAG_CEC7_1) ||
3815 REG_IN_RANGE(addr, GEN12_OAG_SCEC0_0, GEN12_OAG_SCEC7_1) ||
3816 REG_EQUAL(addr, GEN12_OAA_DBG_REG) ||
3817 REG_EQUAL(addr, GEN12_OAG_OA_PESS) ||
3818 REG_EQUAL(addr, GEN12_OAG_SPCTR_CNF);
3819}
3820
3821static bool gen12_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
3822{
3823 return REG_EQUAL(addr, NOA_WRITE) ||
3824 REG_EQUAL(addr, GEN10_NOA_WRITE_HIGH) ||
3825 REG_EQUAL(addr, GDT_CHICKEN_BITS) ||
3826 REG_EQUAL(addr, WAIT_FOR_RC6_EXIT) ||
3827 REG_EQUAL(addr, RPM_CONFIG0) ||
3828 REG_EQUAL(addr, RPM_CONFIG1) ||
3829 REG_IN_RANGE(addr, NOA_CONFIG(0), NOA_CONFIG(8));
3830}
3831
Jani Nikula739f3ab2019-01-16 11:15:19 +02003832static u32 mask_reg_value(u32 reg, u32 val)
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003833{
3834 /* HALF_SLICE_CHICKEN2 is programmed with a the
3835 * WaDisableSTUnitPowerOptimization workaround. Make sure the value
3836 * programmed by userspace doesn't change this.
3837 */
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003838 if (REG_EQUAL(reg, HALF_SLICE_CHICKEN2))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003839 val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE);
3840
3841 /* WAIT_FOR_RC6_EXIT has only one bit fullfilling the function
3842 * indicated by its name and a bunch of selection fields used by OA
3843 * configs.
3844 */
Umesh Nerlige Ramappafc215232019-10-25 12:37:45 -07003845 if (REG_EQUAL(reg, WAIT_FOR_RC6_EXIT))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003846 val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE);
3847
3848 return val;
3849}
3850
Chris Wilson8f8b1172019-10-07 22:09:41 +01003851static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,
3852 bool (*is_valid)(struct i915_perf *perf, u32 addr),
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003853 u32 __user *regs,
3854 u32 n_regs)
3855{
3856 struct i915_oa_reg *oa_regs;
3857 int err;
3858 u32 i;
3859
3860 if (!n_regs)
3861 return NULL;
3862
Linus Torvalds96d4f262019-01-03 18:57:57 -08003863 if (!access_ok(regs, n_regs * sizeof(u32) * 2))
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003864 return ERR_PTR(-EFAULT);
3865
3866 /* No is_valid function means we're not allowing any register to be programmed. */
3867 GEM_BUG_ON(!is_valid);
3868 if (!is_valid)
3869 return ERR_PTR(-EINVAL);
3870
3871 oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL);
3872 if (!oa_regs)
3873 return ERR_PTR(-ENOMEM);
3874
3875 for (i = 0; i < n_regs; i++) {
3876 u32 addr, value;
3877
3878 err = get_user(addr, regs);
3879 if (err)
3880 goto addr_err;
3881
Chris Wilson8f8b1172019-10-07 22:09:41 +01003882 if (!is_valid(perf, addr)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003883 DRM_DEBUG("Invalid oa_reg address: %X\n", addr);
3884 err = -EINVAL;
3885 goto addr_err;
3886 }
3887
3888 err = get_user(value, regs + 1);
3889 if (err)
3890 goto addr_err;
3891
3892 oa_regs[i].addr = _MMIO(addr);
3893 oa_regs[i].value = mask_reg_value(addr, value);
3894
3895 regs += 2;
3896 }
3897
3898 return oa_regs;
3899
3900addr_err:
3901 kfree(oa_regs);
3902 return ERR_PTR(err);
3903}
3904
3905static ssize_t show_dynamic_id(struct device *dev,
3906 struct device_attribute *attr,
3907 char *buf)
3908{
3909 struct i915_oa_config *oa_config =
3910 container_of(attr, typeof(*oa_config), sysfs_metric_id);
3911
3912 return sprintf(buf, "%d\n", oa_config->id);
3913}
3914
Chris Wilson8f8b1172019-10-07 22:09:41 +01003915static int create_dynamic_oa_sysfs_entry(struct i915_perf *perf,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003916 struct i915_oa_config *oa_config)
3917{
Chris Wilson28152a22017-08-03 23:37:00 +01003918 sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003919 oa_config->sysfs_metric_id.attr.name = "id";
3920 oa_config->sysfs_metric_id.attr.mode = S_IRUGO;
3921 oa_config->sysfs_metric_id.show = show_dynamic_id;
3922 oa_config->sysfs_metric_id.store = NULL;
3923
3924 oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr;
3925 oa_config->attrs[1] = NULL;
3926
3927 oa_config->sysfs_metric.name = oa_config->uuid;
3928 oa_config->sysfs_metric.attrs = oa_config->attrs;
3929
Chris Wilson8f8b1172019-10-07 22:09:41 +01003930 return sysfs_create_group(perf->metrics_kobj,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003931 &oa_config->sysfs_metric);
3932}
3933
3934/**
3935 * i915_perf_add_config_ioctl - DRM ioctl() for userspace to add a new OA config
3936 * @dev: drm device
3937 * @data: ioctl data (pointer to struct drm_i915_perf_oa_config) copied from
3938 * userspace (unvalidated)
3939 * @file: drm file
3940 *
3941 * Validates the submitted OA register to be saved into a new OA config that
3942 * can then be used for programming the OA unit and its NOA network.
3943 *
3944 * Returns: A new allocated config number to be used with the perf open ioctl
3945 * or a negative error code on failure.
3946 */
3947int i915_perf_add_config_ioctl(struct drm_device *dev, void *data,
3948 struct drm_file *file)
3949{
Chris Wilson8f8b1172019-10-07 22:09:41 +01003950 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003951 struct drm_i915_perf_oa_config *args = data;
3952 struct i915_oa_config *oa_config, *tmp;
Chris Wilsonc2fba932019-10-13 10:52:11 +01003953 static struct i915_oa_reg *regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003954 int err, id;
3955
Chris Wilson8f8b1172019-10-07 22:09:41 +01003956 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003957 DRM_DEBUG("i915 perf interface not available for this system\n");
3958 return -ENOTSUPP;
3959 }
3960
Chris Wilson8f8b1172019-10-07 22:09:41 +01003961 if (!perf->metrics_kobj) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003962 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
3963 return -EINVAL;
3964 }
3965
3966 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
3967 DRM_DEBUG("Insufficient privileges to add i915 OA config\n");
3968 return -EACCES;
3969 }
3970
3971 if ((!args->mux_regs_ptr || !args->n_mux_regs) &&
3972 (!args->boolean_regs_ptr || !args->n_boolean_regs) &&
3973 (!args->flex_regs_ptr || !args->n_flex_regs)) {
3974 DRM_DEBUG("No OA registers given\n");
3975 return -EINVAL;
3976 }
3977
3978 oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL);
3979 if (!oa_config) {
3980 DRM_DEBUG("Failed to allocate memory for the OA config\n");
3981 return -ENOMEM;
3982 }
3983
Lionel Landwerlin6a450082019-10-12 08:23:06 +01003984 oa_config->perf = perf;
3985 kref_init(&oa_config->ref);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01003986
3987 if (!uuid_is_valid(args->uuid)) {
3988 DRM_DEBUG("Invalid uuid format for OA config\n");
3989 err = -EINVAL;
3990 goto reg_err;
3991 }
3992
3993 /* Last character in oa_config->uuid will be 0 because oa_config is
3994 * kzalloc.
3995 */
3996 memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid));
3997
3998 oa_config->mux_regs_len = args->n_mux_regs;
Chris Wilsonc2fba932019-10-13 10:52:11 +01003999 regs = alloc_oa_regs(perf,
4000 perf->ops.is_valid_mux_reg,
4001 u64_to_user_ptr(args->mux_regs_ptr),
4002 args->n_mux_regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004003
Chris Wilsonc2fba932019-10-13 10:52:11 +01004004 if (IS_ERR(regs)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004005 DRM_DEBUG("Failed to create OA config for mux_regs\n");
Chris Wilsonc2fba932019-10-13 10:52:11 +01004006 err = PTR_ERR(regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004007 goto reg_err;
4008 }
Chris Wilsonc2fba932019-10-13 10:52:11 +01004009 oa_config->mux_regs = regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004010
4011 oa_config->b_counter_regs_len = args->n_boolean_regs;
Chris Wilsonc2fba932019-10-13 10:52:11 +01004012 regs = alloc_oa_regs(perf,
4013 perf->ops.is_valid_b_counter_reg,
4014 u64_to_user_ptr(args->boolean_regs_ptr),
4015 args->n_boolean_regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004016
Chris Wilsonc2fba932019-10-13 10:52:11 +01004017 if (IS_ERR(regs)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004018 DRM_DEBUG("Failed to create OA config for b_counter_regs\n");
Chris Wilsonc2fba932019-10-13 10:52:11 +01004019 err = PTR_ERR(regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004020 goto reg_err;
4021 }
Chris Wilsonc2fba932019-10-13 10:52:11 +01004022 oa_config->b_counter_regs = regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004023
Chris Wilson8f8b1172019-10-07 22:09:41 +01004024 if (INTEL_GEN(perf->i915) < 8) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004025 if (args->n_flex_regs != 0) {
4026 err = -EINVAL;
4027 goto reg_err;
4028 }
4029 } else {
4030 oa_config->flex_regs_len = args->n_flex_regs;
Chris Wilsonc2fba932019-10-13 10:52:11 +01004031 regs = alloc_oa_regs(perf,
4032 perf->ops.is_valid_flex_reg,
4033 u64_to_user_ptr(args->flex_regs_ptr),
4034 args->n_flex_regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004035
Chris Wilsonc2fba932019-10-13 10:52:11 +01004036 if (IS_ERR(regs)) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004037 DRM_DEBUG("Failed to create OA config for flex_regs\n");
Chris Wilsonc2fba932019-10-13 10:52:11 +01004038 err = PTR_ERR(regs);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004039 goto reg_err;
4040 }
Chris Wilsonc2fba932019-10-13 10:52:11 +01004041 oa_config->flex_regs = regs;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004042 }
4043
Chris Wilson8f8b1172019-10-07 22:09:41 +01004044 err = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004045 if (err)
4046 goto reg_err;
4047
4048 /* We shouldn't have too many configs, so this iteration shouldn't be
4049 * too costly.
4050 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004051 idr_for_each_entry(&perf->metrics_idr, tmp, id) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004052 if (!strcmp(tmp->uuid, oa_config->uuid)) {
4053 DRM_DEBUG("OA config already exists with this uuid\n");
4054 err = -EADDRINUSE;
4055 goto sysfs_err;
4056 }
4057 }
4058
Chris Wilson8f8b1172019-10-07 22:09:41 +01004059 err = create_dynamic_oa_sysfs_entry(perf, oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004060 if (err) {
4061 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
4062 goto sysfs_err;
4063 }
4064
4065 /* Config id 0 is invalid, id 1 for kernel stored test config. */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004066 oa_config->id = idr_alloc(&perf->metrics_idr,
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004067 oa_config, 2,
4068 0, GFP_KERNEL);
4069 if (oa_config->id < 0) {
4070 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
4071 err = oa_config->id;
4072 goto sysfs_err;
4073 }
4074
Chris Wilson8f8b1172019-10-07 22:09:41 +01004075 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004076
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01004077 DRM_DEBUG("Added config %s id=%i\n", oa_config->uuid, oa_config->id);
4078
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004079 return oa_config->id;
4080
4081sysfs_err:
Chris Wilson8f8b1172019-10-07 22:09:41 +01004082 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004083reg_err:
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004084 i915_oa_config_put(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004085 DRM_DEBUG("Failed to add new OA config\n");
4086 return err;
4087}
4088
4089/**
4090 * i915_perf_remove_config_ioctl - DRM ioctl() for userspace to remove an OA config
4091 * @dev: drm device
4092 * @data: ioctl data (pointer to u64 integer) copied from userspace
4093 * @file: drm file
4094 *
4095 * Configs can be removed while being used, the will stop appearing in sysfs
4096 * and their content will be freed when the stream using the config is closed.
4097 *
4098 * Returns: 0 on success or a negative error code on failure.
4099 */
4100int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data,
4101 struct drm_file *file)
4102{
Chris Wilson8f8b1172019-10-07 22:09:41 +01004103 struct i915_perf *perf = &to_i915(dev)->perf;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004104 u64 *arg = data;
4105 struct i915_oa_config *oa_config;
4106 int ret;
4107
Chris Wilson8f8b1172019-10-07 22:09:41 +01004108 if (!perf->i915) {
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004109 DRM_DEBUG("i915 perf interface not available for this system\n");
4110 return -ENOTSUPP;
4111 }
4112
4113 if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
4114 DRM_DEBUG("Insufficient privileges to remove i915 OA config\n");
4115 return -EACCES;
4116 }
4117
Chris Wilson8f8b1172019-10-07 22:09:41 +01004118 ret = mutex_lock_interruptible(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004119 if (ret)
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004120 return ret;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004121
Chris Wilson8f8b1172019-10-07 22:09:41 +01004122 oa_config = idr_find(&perf->metrics_idr, *arg);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004123 if (!oa_config) {
4124 DRM_DEBUG("Failed to remove unknown OA config\n");
4125 ret = -ENOENT;
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004126 goto err_unlock;
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004127 }
4128
4129 GEM_BUG_ON(*arg != oa_config->id);
4130
Lionel Landwerlin4f6ccc72019-10-14 21:14:02 +01004131 sysfs_remove_group(perf->metrics_kobj, &oa_config->sysfs_metric);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004132
Chris Wilson8f8b1172019-10-07 22:09:41 +01004133 idr_remove(&perf->metrics_idr, *arg);
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01004134
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004135 mutex_unlock(&perf->metrics_lock);
4136
Lionel Landwerlin9bd9be62018-03-26 10:08:28 +01004137 DRM_DEBUG("Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
4138
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004139 i915_oa_config_put(oa_config);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004140
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004141 return 0;
4142
4143err_unlock:
Chris Wilson8f8b1172019-10-07 22:09:41 +01004144 mutex_unlock(&perf->metrics_lock);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004145 return ret;
4146}
4147
Robert Braggccdf6342016-11-07 19:49:54 +00004148static struct ctl_table oa_table[] = {
4149 {
4150 .procname = "perf_stream_paranoid",
4151 .data = &i915_perf_stream_paranoid,
4152 .maxlen = sizeof(i915_perf_stream_paranoid),
4153 .mode = 0644,
4154 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07004155 .extra1 = SYSCTL_ZERO,
4156 .extra2 = SYSCTL_ONE,
Robert Braggccdf6342016-11-07 19:49:54 +00004157 },
Robert Bragg00319ba2016-11-07 19:49:55 +00004158 {
4159 .procname = "oa_max_sample_rate",
4160 .data = &i915_oa_max_sample_rate,
4161 .maxlen = sizeof(i915_oa_max_sample_rate),
4162 .mode = 0644,
4163 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07004164 .extra1 = SYSCTL_ZERO,
Robert Bragg00319ba2016-11-07 19:49:55 +00004165 .extra2 = &oa_sample_rate_hard_limit,
4166 },
Robert Braggccdf6342016-11-07 19:49:54 +00004167 {}
4168};
4169
4170static struct ctl_table i915_root[] = {
4171 {
4172 .procname = "i915",
4173 .maxlen = 0,
4174 .mode = 0555,
4175 .child = oa_table,
4176 },
4177 {}
4178};
4179
4180static struct ctl_table dev_root[] = {
4181 {
4182 .procname = "dev",
4183 .maxlen = 0,
4184 .mode = 0555,
4185 .child = i915_root,
4186 },
4187 {}
4188};
4189
Robert Bragg16d98b32016-12-07 21:40:33 +00004190/**
4191 * i915_perf_init - initialize i915-perf state on module load
Chris Wilson8f8b1172019-10-07 22:09:41 +01004192 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00004193 *
4194 * Initializes i915-perf state without exposing anything to userspace.
4195 *
4196 * Note: i915-perf initialization is split into an 'init' and 'register'
4197 * phase with the i915_perf_register() exposing state to userspace.
4198 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004199void i915_perf_init(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00004200{
Chris Wilson8f8b1172019-10-07 22:09:41 +01004201 struct i915_perf *perf = &i915->perf;
Robert Braggd7965152016-11-07 19:49:52 +00004202
Chris Wilson8f8b1172019-10-07 22:09:41 +01004203 /* XXX const struct i915_perf_ops! */
4204
4205 if (IS_HASWELL(i915)) {
4206 perf->ops.is_valid_b_counter_reg = gen7_is_valid_b_counter_addr;
4207 perf->ops.is_valid_mux_reg = hsw_is_valid_mux_addr;
4208 perf->ops.is_valid_flex_reg = NULL;
4209 perf->ops.enable_metric_set = hsw_enable_metric_set;
4210 perf->ops.disable_metric_set = hsw_disable_metric_set;
4211 perf->ops.oa_enable = gen7_oa_enable;
4212 perf->ops.oa_disable = gen7_oa_disable;
4213 perf->ops.read = gen7_oa_read;
4214 perf->ops.oa_hw_tail_read = gen7_oa_hw_tail_read;
4215
4216 perf->oa_formats = hsw_oa_formats;
4217 } else if (HAS_LOGICAL_RING_CONTEXTS(i915)) {
Robert Bragg19f81df2017-06-13 12:23:03 +01004218 /* Note: that although we could theoretically also support the
4219 * legacy ringbuffer mode on BDW (and earlier iterations of
4220 * this driver, before upstreaming did this) it didn't seem
4221 * worth the complexity to maintain now that BDW+ enable
4222 * execlist mode by default.
4223 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004224 perf->ops.read = gen8_oa_read;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01004225
Chris Wilson8f8b1172019-10-07 22:09:41 +01004226 if (IS_GEN_RANGE(i915, 8, 9)) {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004227 perf->oa_formats = gen8_plus_oa_formats;
4228
Chris Wilson8f8b1172019-10-07 22:09:41 +01004229 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004230 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004231 perf->ops.is_valid_mux_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004232 gen8_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004233 perf->ops.is_valid_flex_reg =
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004234 gen8_is_valid_flex_addr;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01004235
Chris Wilson8f8b1172019-10-07 22:09:41 +01004236 if (IS_CHERRYVIEW(i915)) {
4237 perf->ops.is_valid_mux_reg =
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004238 chv_is_valid_mux_addr;
4239 }
Robert Bragg155e9412017-06-13 12:23:05 +01004240
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004241 perf->ops.oa_enable = gen8_oa_enable;
4242 perf->ops.oa_disable = gen8_oa_disable;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004243 perf->ops.enable_metric_set = gen8_enable_metric_set;
4244 perf->ops.disable_metric_set = gen8_disable_metric_set;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004245 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004246
Chris Wilson8f8b1172019-10-07 22:09:41 +01004247 if (IS_GEN(i915, 8)) {
4248 perf->ctx_oactxctrl_offset = 0x120;
4249 perf->ctx_flexeu0_offset = 0x2ce;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004250
Chris Wilson8f8b1172019-10-07 22:09:41 +01004251 perf->gen8_valid_ctx_bit = BIT(25);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004252 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01004253 perf->ctx_oactxctrl_offset = 0x128;
4254 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004255
Chris Wilson8f8b1172019-10-07 22:09:41 +01004256 perf->gen8_valid_ctx_bit = BIT(16);
Lionel Landwerlinba6b7c12017-11-10 19:08:41 +00004257 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01004258 } else if (IS_GEN_RANGE(i915, 10, 11)) {
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004259 perf->oa_formats = gen8_plus_oa_formats;
4260
Chris Wilson8f8b1172019-10-07 22:09:41 +01004261 perf->ops.is_valid_b_counter_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004262 gen7_is_valid_b_counter_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004263 perf->ops.is_valid_mux_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004264 gen10_is_valid_mux_addr;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004265 perf->ops.is_valid_flex_reg =
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004266 gen8_is_valid_flex_addr;
4267
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004268 perf->ops.oa_enable = gen8_oa_enable;
4269 perf->ops.oa_disable = gen8_oa_disable;
Chris Wilson8f8b1172019-10-07 22:09:41 +01004270 perf->ops.enable_metric_set = gen8_enable_metric_set;
4271 perf->ops.disable_metric_set = gen10_disable_metric_set;
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004272 perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
Lionel Landwerlin95690a02017-11-10 19:08:43 +00004273
Chris Wilson8f8b1172019-10-07 22:09:41 +01004274 if (IS_GEN(i915, 10)) {
4275 perf->ctx_oactxctrl_offset = 0x128;
4276 perf->ctx_flexeu0_offset = 0x3de;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03004277 } else {
Chris Wilson8f8b1172019-10-07 22:09:41 +01004278 perf->ctx_oactxctrl_offset = 0x124;
4279 perf->ctx_flexeu0_offset = 0x78e;
Lionel Landwerlin8dcfdfb2019-06-10 11:19:14 +03004280 }
Chris Wilson8f8b1172019-10-07 22:09:41 +01004281 perf->gen8_valid_ctx_bit = BIT(16);
Lionel Landwerlin00a7f0d2019-10-25 12:37:46 -07004282 } else if (IS_GEN(i915, 12)) {
4283 perf->oa_formats = gen12_oa_formats;
4284
4285 perf->ops.is_valid_b_counter_reg =
4286 gen12_is_valid_b_counter_addr;
4287 perf->ops.is_valid_mux_reg =
4288 gen12_is_valid_mux_addr;
4289 perf->ops.is_valid_flex_reg =
4290 gen8_is_valid_flex_addr;
4291
4292 perf->ops.oa_enable = gen12_oa_enable;
4293 perf->ops.oa_disable = gen12_oa_disable;
4294 perf->ops.enable_metric_set = gen12_enable_metric_set;
4295 perf->ops.disable_metric_set = gen12_disable_metric_set;
4296 perf->ops.oa_hw_tail_read = gen12_oa_hw_tail_read;
4297
4298 perf->ctx_flexeu0_offset = 0;
4299 perf->ctx_oactxctrl_offset = 0x144;
Robert Bragg19f81df2017-06-13 12:23:03 +01004300 }
Robert Bragg19f81df2017-06-13 12:23:03 +01004301 }
4302
Chris Wilson8f8b1172019-10-07 22:09:41 +01004303 if (perf->ops.enable_metric_set) {
Chris Wilson8f8b1172019-10-07 22:09:41 +01004304 mutex_init(&perf->lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01004305
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01004306 oa_sample_rate_hard_limit = 1000 *
Chris Wilson8f8b1172019-10-07 22:09:41 +01004307 (RUNTIME_INFO(i915)->cs_timestamp_frequency_khz / 2);
4308 perf->sysctl_header = register_sysctl_table(dev_root);
Robert Bragg19f81df2017-06-13 12:23:03 +01004309
Chris Wilson8f8b1172019-10-07 22:09:41 +01004310 mutex_init(&perf->metrics_lock);
4311 idr_init(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004312
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07004313 /* We set up some ratelimit state to potentially throttle any
4314 * _NOTES about spurious, invalid OA reports which we don't
4315 * forward to userspace.
4316 *
4317 * We print a _NOTE about any throttling when closing the
4318 * stream instead of waiting until driver _fini which no one
4319 * would ever see.
4320 *
4321 * Using the same limiting factors as printk_ratelimit()
4322 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004323 ratelimit_state_init(&perf->spurious_report_rs, 5 * HZ, 10);
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07004324 /* Since we use a DRM_NOTE for spurious reports it would be
4325 * inconsistent to let __ratelimit() automatically print a
4326 * warning for throttling.
4327 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004328 ratelimit_set_flags(&perf->spurious_report_rs,
Umesh Nerlige Ramappaa37f08a2019-08-06 16:30:02 -07004329 RATELIMIT_MSG_ON_RELEASE);
4330
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01004331 atomic64_set(&perf->noa_programming_delay,
4332 500 * 1000 /* 500us */);
4333
Chris Wilson8f8b1172019-10-07 22:09:41 +01004334 perf->i915 = i915;
Robert Bragg19f81df2017-06-13 12:23:03 +01004335 }
Robert Braggeec688e2016-11-07 19:49:47 +00004336}
4337
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004338static int destroy_config(int id, void *p, void *data)
4339{
Lionel Landwerlin6a450082019-10-12 08:23:06 +01004340 i915_oa_config_put(p);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004341 return 0;
4342}
4343
Robert Bragg16d98b32016-12-07 21:40:33 +00004344/**
4345 * i915_perf_fini - Counter part to i915_perf_init()
Chris Wilson8f8b1172019-10-07 22:09:41 +01004346 * @i915: i915 device instance
Robert Bragg16d98b32016-12-07 21:40:33 +00004347 */
Chris Wilson8f8b1172019-10-07 22:09:41 +01004348void i915_perf_fini(struct drm_i915_private *i915)
Robert Braggeec688e2016-11-07 19:49:47 +00004349{
Chris Wilson8f8b1172019-10-07 22:09:41 +01004350 struct i915_perf *perf = &i915->perf;
4351
4352 if (!perf->i915)
Robert Braggeec688e2016-11-07 19:49:47 +00004353 return;
4354
Chris Wilson8f8b1172019-10-07 22:09:41 +01004355 idr_for_each(&perf->metrics_idr, destroy_config, perf);
4356 idr_destroy(&perf->metrics_idr);
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01004357
Chris Wilson8f8b1172019-10-07 22:09:41 +01004358 unregister_sysctl_table(perf->sysctl_header);
Robert Braggccdf6342016-11-07 19:49:54 +00004359
Chris Wilson8f8b1172019-10-07 22:09:41 +01004360 memset(&perf->ops, 0, sizeof(perf->ops));
4361 perf->i915 = NULL;
Robert Braggeec688e2016-11-07 19:49:47 +00004362}
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01004363
Lionel Landwerlinb8d49f22019-10-14 21:14:01 +01004364/**
4365 * i915_perf_ioctl_version - Version of the i915-perf subsystem
4366 *
4367 * This version number is used by userspace to detect available features.
4368 */
4369int i915_perf_ioctl_version(void)
4370{
Chris Wilson7831e9a2019-10-14 21:14:03 +01004371 /*
4372 * 1: Initial version
4373 * I915_PERF_IOCTL_ENABLE
4374 * I915_PERF_IOCTL_DISABLE
4375 *
4376 * 2: Added runtime modification of OA config.
4377 * I915_PERF_IOCTL_CONFIG
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01004378 *
4379 * 3: Add DRM_I915_PERF_PROP_HOLD_PREEMPTION parameter to hold
4380 * preemption on a particular context so that performance data is
4381 * accessible from a delta of MI_RPC reports without looking at the
4382 * OA buffer.
Chris Wilson7831e9a2019-10-14 21:14:03 +01004383 */
Lionel Landwerlin9cd20ef2019-10-14 21:14:04 +01004384 return 3;
Lionel Landwerlinb8d49f22019-10-14 21:14:01 +01004385}
4386
Lionel Landwerlindaed3e42019-10-12 08:23:07 +01004387#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
4388#include "selftests/i915_perf.c"
4389#endif