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