blob: 4d79698685ea40d62eb349867c2458d2ac9383a0 [file] [log] [blame]
Robert Braggeec688e2016-11-07 19:49:47 +00001/*
2 * Copyright © 2015-2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Robert Bragg <robert@sixbynine.org>
25 */
26
Robert Bragg7abbd8d2016-11-07 19:49:57 +000027
28/**
Robert Bragg16d98b32016-12-07 21:40:33 +000029 * DOC: i915 Perf Overview
Robert Bragg7abbd8d2016-11-07 19:49:57 +000030 *
31 * Gen graphics supports a large number of performance counters that can help
32 * driver and application developers understand and optimize their use of the
33 * GPU.
34 *
35 * This i915 perf interface enables userspace to configure and open a file
36 * descriptor representing a stream of GPU metrics which can then be read() as
37 * a stream of sample records.
38 *
39 * The interface is particularly suited to exposing buffered metrics that are
40 * captured by DMA from the GPU, unsynchronized with and unrelated to the CPU.
41 *
42 * Streams representing a single context are accessible to applications with a
43 * corresponding drm file descriptor, such that OpenGL can use the interface
44 * without special privileges. Access to system-wide metrics requires root
45 * privileges by default, unless changed via the dev.i915.perf_event_paranoid
46 * sysctl option.
47 *
Robert Bragg16d98b32016-12-07 21:40:33 +000048 */
49
50/**
51 * DOC: i915 Perf History and Comparison with Core Perf
Robert Bragg7abbd8d2016-11-07 19:49:57 +000052 *
53 * The interface was initially inspired by the core Perf infrastructure but
54 * some notable differences are:
55 *
56 * i915 perf file descriptors represent a "stream" instead of an "event"; where
57 * a perf event primarily corresponds to a single 64bit value, while a stream
58 * might sample sets of tightly-coupled counters, depending on the
59 * configuration. For example the Gen OA unit isn't designed to support
60 * orthogonal configurations of individual counters; it's configured for a set
61 * of related counters. Samples for an i915 perf stream capturing OA metrics
62 * will include a set of counter values packed in a compact HW specific format.
63 * The OA unit supports a number of different packing formats which can be
64 * selected by the user opening the stream. Perf has support for grouping
65 * events, but each event in the group is configured, validated and
66 * authenticated individually with separate system calls.
67 *
68 * i915 perf stream configurations are provided as an array of u64 (key,value)
69 * pairs, instead of a fixed struct with multiple miscellaneous config members,
70 * interleaved with event-type specific members.
71 *
72 * i915 perf doesn't support exposing metrics via an mmap'd circular buffer.
73 * The supported metrics are being written to memory by the GPU unsynchronized
74 * with the CPU, using HW specific packing formats for counter sets. Sometimes
75 * the constraints on HW configuration require reports to be filtered before it
76 * would be acceptable to expose them to unprivileged applications - to hide
77 * the metrics of other processes/contexts. For these use cases a read() based
78 * interface is a good fit, and provides an opportunity to filter data as it
79 * gets copied from the GPU mapped buffers to userspace buffers.
80 *
81 *
Robert Bragg16d98b32016-12-07 21:40:33 +000082 * Issues hit with first prototype based on Core Perf
83 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Robert Bragg7abbd8d2016-11-07 19:49:57 +000084 *
85 * The first prototype of this driver was based on the core perf
86 * infrastructure, and while we did make that mostly work, with some changes to
87 * perf, we found we were breaking or working around too many assumptions baked
88 * into perf's currently cpu centric design.
89 *
90 * In the end we didn't see a clear benefit to making perf's implementation and
91 * interface more complex by changing design assumptions while we knew we still
92 * wouldn't be able to use any existing perf based userspace tools.
93 *
94 * Also considering the Gen specific nature of the Observability hardware and
95 * how userspace will sometimes need to combine i915 perf OA metrics with
96 * side-band OA data captured via MI_REPORT_PERF_COUNT commands; we're
97 * expecting the interface to be used by a platform specific userspace such as
98 * OpenGL or tools. This is to say; we aren't inherently missing out on having
99 * a standard vendor/architecture agnostic interface by not using perf.
100 *
101 *
102 * For posterity, in case we might re-visit trying to adapt core perf to be
103 * better suited to exposing i915 metrics these were the main pain points we
104 * hit:
105 *
106 * - The perf based OA PMU driver broke some significant design assumptions:
107 *
108 * Existing perf pmus are used for profiling work on a cpu and we were
109 * introducing the idea of _IS_DEVICE pmus with different security
110 * implications, the need to fake cpu-related data (such as user/kernel
111 * registers) to fit with perf's current design, and adding _DEVICE records
112 * as a way to forward device-specific status records.
113 *
114 * The OA unit writes reports of counters into a circular buffer, without
115 * involvement from the CPU, making our PMU driver the first of a kind.
116 *
117 * Given the way we were periodically forward data from the GPU-mapped, OA
118 * buffer to perf's buffer, those bursts of sample writes looked to perf like
119 * we were sampling too fast and so we had to subvert its throttling checks.
120 *
121 * Perf supports groups of counters and allows those to be read via
122 * transactions internally but transactions currently seem designed to be
123 * explicitly initiated from the cpu (say in response to a userspace read())
124 * and while we could pull a report out of the OA buffer we can't
125 * trigger a report from the cpu on demand.
126 *
127 * Related to being report based; the OA counters are configured in HW as a
128 * set while perf generally expects counter configurations to be orthogonal.
129 * Although counters can be associated with a group leader as they are
130 * opened, there's no clear precedent for being able to provide group-wide
131 * configuration attributes (for example we want to let userspace choose the
132 * OA unit report format used to capture all counters in a set, or specify a
133 * GPU context to filter metrics on). We avoided using perf's grouping
134 * feature and forwarded OA reports to userspace via perf's 'raw' sample
135 * field. This suited our userspace well considering how coupled the counters
136 * are when dealing with normalizing. It would be inconvenient to split
137 * counters up into separate events, only to require userspace to recombine
138 * them. For Mesa it's also convenient to be forwarded raw, periodic reports
139 * for combining with the side-band raw reports it captures using
140 * MI_REPORT_PERF_COUNT commands.
141 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000142 * - As a side note on perf's grouping feature; there was also some concern
Robert Bragg7abbd8d2016-11-07 19:49:57 +0000143 * that using PERF_FORMAT_GROUP as a way to pack together counter values
144 * would quite drastically inflate our sample sizes, which would likely
145 * lower the effective sampling resolutions we could use when the available
146 * memory bandwidth is limited.
147 *
148 * With the OA unit's report formats, counters are packed together as 32
149 * or 40bit values, with the largest report size being 256 bytes.
150 *
151 * PERF_FORMAT_GROUP values are 64bit, but there doesn't appear to be a
152 * documented ordering to the values, implying PERF_FORMAT_ID must also be
153 * used to add a 64bit ID before each value; giving 16 bytes per counter.
154 *
155 * Related to counter orthogonality; we can't time share the OA unit, while
156 * event scheduling is a central design idea within perf for allowing
157 * userspace to open + enable more events than can be configured in HW at any
158 * one time. The OA unit is not designed to allow re-configuration while in
159 * use. We can't reconfigure the OA unit without losing internal OA unit
160 * state which we can't access explicitly to save and restore. Reconfiguring
161 * the OA unit is also relatively slow, involving ~100 register writes. From
162 * userspace Mesa also depends on a stable OA configuration when emitting
163 * MI_REPORT_PERF_COUNT commands and importantly the OA unit can't be
164 * disabled while there are outstanding MI_RPC commands lest we hang the
165 * command streamer.
166 *
167 * The contents of sample records aren't extensible by device drivers (i.e.
168 * the sample_type bits). As an example; Sourab Gupta had been looking to
169 * attach GPU timestamps to our OA samples. We were shoehorning OA reports
170 * into sample records by using the 'raw' field, but it's tricky to pack more
171 * than one thing into this field because events/core.c currently only lets a
172 * pmu give a single raw data pointer plus len which will be copied into the
173 * ring buffer. To include more than the OA report we'd have to copy the
174 * report into an intermediate larger buffer. I'd been considering allowing a
175 * vector of data+len values to be specified for copying the raw data, but
176 * it felt like a kludge to being using the raw field for this purpose.
177 *
178 * - It felt like our perf based PMU was making some technical compromises
179 * just for the sake of using perf:
180 *
181 * perf_event_open() requires events to either relate to a pid or a specific
182 * cpu core, while our device pmu related to neither. Events opened with a
183 * pid will be automatically enabled/disabled according to the scheduling of
184 * that process - so not appropriate for us. When an event is related to a
185 * cpu id, perf ensures pmu methods will be invoked via an inter process
186 * interrupt on that core. To avoid invasive changes our userspace opened OA
187 * perf events for a specific cpu. This was workable but it meant the
188 * majority of the OA driver ran in atomic context, including all OA report
189 * forwarding, which wasn't really necessary in our case and seems to make
190 * our locking requirements somewhat complex as we handled the interaction
191 * with the rest of the i915 driver.
192 */
193
Robert Braggeec688e2016-11-07 19:49:47 +0000194#include <linux/anon_inodes.h>
Robert Braggd7965152016-11-07 19:49:52 +0000195#include <linux/sizes.h>
Robert Braggeec688e2016-11-07 19:49:47 +0000196
197#include "i915_drv.h"
Robert Braggd7965152016-11-07 19:49:52 +0000198#include "i915_oa_hsw.h"
Robert Bragg19f81df2017-06-13 12:23:03 +0100199#include "i915_oa_bdw.h"
200#include "i915_oa_chv.h"
201#include "i915_oa_sklgt2.h"
202#include "i915_oa_sklgt3.h"
203#include "i915_oa_sklgt4.h"
204#include "i915_oa_bxt.h"
Robert Braggd7965152016-11-07 19:49:52 +0000205
206/* HW requires this to be a power of two, between 128k and 16M, though driver
207 * is currently generally designed assuming the largest 16M size is used such
208 * that the overflow cases are unlikely in normal operation.
209 */
210#define OA_BUFFER_SIZE SZ_16M
211
212#define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
213
Robert Bragg0dd860c2017-05-11 16:43:28 +0100214/**
215 * DOC: OA Tail Pointer Race
216 *
217 * There's a HW race condition between OA unit tail pointer register updates and
Robert Braggd7965152016-11-07 19:49:52 +0000218 * writes to memory whereby the tail pointer can sometimes get ahead of what's
Robert Bragg0dd860c2017-05-11 16:43:28 +0100219 * been written out to the OA buffer so far (in terms of what's visible to the
220 * CPU).
Robert Braggd7965152016-11-07 19:49:52 +0000221 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100222 * Although this can be observed explicitly while copying reports to userspace
223 * by checking for a zeroed report-id field in tail reports, we want to account
Robert Bragg19f81df2017-06-13 12:23:03 +0100224 * for this earlier, as part of the oa_buffer_check to avoid lots of redundant
Robert Bragg0dd860c2017-05-11 16:43:28 +0100225 * read() attempts.
Robert Braggd7965152016-11-07 19:49:52 +0000226 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100227 * In effect we define a tail pointer for reading that lags the real tail
228 * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough
229 * time for the corresponding reports to become visible to the CPU.
Robert Braggd7965152016-11-07 19:49:52 +0000230 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100231 * To manage this we actually track two tail pointers:
232 * 1) An 'aging' tail with an associated timestamp that is tracked until we
233 * can trust the corresponding data is visible to the CPU; at which point
234 * it is considered 'aged'.
235 * 2) An 'aged' tail that can be used for read()ing.
236 *
237 * The two separate pointers let us decouple read()s from tail pointer aging.
238 *
239 * The tail pointers are checked and updated at a limited rate within a hrtimer
240 * callback (the same callback that is used for delivering POLLIN events)
241 *
242 * Initially the tails are marked invalid with %INVALID_TAIL_PTR which
243 * indicates that an updated tail pointer is needed.
244 *
245 * Most of the implementation details for this workaround are in
Robert Bragg19f81df2017-06-13 12:23:03 +0100246 * oa_buffer_check_unlocked() and _append_oa_reports()
Robert Bragg0dd860c2017-05-11 16:43:28 +0100247 *
248 * Note for posterity: previously the driver used to define an effective tail
249 * pointer that lagged the real pointer by a 'tail margin' measured in bytes
250 * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
251 * This was flawed considering that the OA unit may also automatically generate
252 * non-periodic reports (such as on context switch) or the OA unit may be
253 * enabled without any periodic sampling.
Robert Braggd7965152016-11-07 19:49:52 +0000254 */
255#define OA_TAIL_MARGIN_NSEC 100000ULL
Robert Bragg0dd860c2017-05-11 16:43:28 +0100256#define INVALID_TAIL_PTR 0xffffffff
Robert Braggd7965152016-11-07 19:49:52 +0000257
258/* frequency for checking whether the OA unit has written new reports to the
259 * circular OA buffer...
260 */
261#define POLL_FREQUENCY 200
262#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
263
Robert Braggccdf6342016-11-07 19:49:54 +0000264/* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
265static int zero;
266static int one = 1;
267static u32 i915_perf_stream_paranoid = true;
268
Robert Braggd7965152016-11-07 19:49:52 +0000269/* The maximum exponent the hardware accepts is 63 (essentially it selects one
270 * of the 64bit timestamp bits to trigger reports from) but there's currently
271 * no known use case for sampling as infrequently as once per 47 thousand years.
272 *
273 * Since the timestamps included in OA reports are only 32bits it seems
274 * reasonable to limit the OA exponent where it's still possible to account for
275 * overflow in OA report timestamps.
276 */
277#define OA_EXPONENT_MAX 31
278
279#define INVALID_CTX_ID 0xffffffff
280
Robert Bragg19f81df2017-06-13 12:23:03 +0100281/* On Gen8+ automatically triggered OA reports include a 'reason' field... */
282#define OAREPORT_REASON_MASK 0x3f
283#define OAREPORT_REASON_SHIFT 19
284#define OAREPORT_REASON_TIMER (1<<0)
285#define OAREPORT_REASON_CTX_SWITCH (1<<3)
286#define OAREPORT_REASON_CLK_RATIO (1<<5)
287
Robert Braggd7965152016-11-07 19:49:52 +0000288
Robert Bragg00319ba2016-11-07 19:49:55 +0000289/* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
290 *
Robert Bragg155e9412017-06-13 12:23:05 +0100291 * The highest sampling frequency we can theoretically program the OA unit
292 * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
293 *
294 * Initialized just before we register the sysctl parameter.
Robert Bragg00319ba2016-11-07 19:49:55 +0000295 */
Robert Bragg155e9412017-06-13 12:23:05 +0100296static int oa_sample_rate_hard_limit;
Robert Bragg00319ba2016-11-07 19:49:55 +0000297
298/* Theoretically we can program the OA unit to sample every 160ns but don't
299 * allow that by default unless root...
300 *
301 * The default threshold of 100000Hz is based on perf's similar
302 * kernel.perf_event_max_sample_rate sysctl parameter.
303 */
304static u32 i915_oa_max_sample_rate = 100000;
305
Robert Braggd7965152016-11-07 19:49:52 +0000306/* XXX: beware if future OA HW adds new report formats that the current
307 * code assumes all reports have a power-of-two size and ~(size - 1) can
308 * be used as a mask to align the OA tail pointer.
309 */
310static struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
311 [I915_OA_FORMAT_A13] = { 0, 64 },
312 [I915_OA_FORMAT_A29] = { 1, 128 },
313 [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
314 /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
315 [I915_OA_FORMAT_B4_C8] = { 4, 64 },
316 [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
317 [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
318 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
319};
320
Robert Bragg19f81df2017-06-13 12:23:03 +0100321static struct i915_oa_format gen8_plus_oa_formats[I915_OA_FORMAT_MAX] = {
322 [I915_OA_FORMAT_A12] = { 0, 64 },
323 [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
324 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
325 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
326};
327
Robert Braggd7965152016-11-07 19:49:52 +0000328#define SAMPLE_OA_REPORT (1<<0)
Robert Braggeec688e2016-11-07 19:49:47 +0000329
Robert Bragg16d98b32016-12-07 21:40:33 +0000330/**
331 * struct perf_open_properties - for validated properties given to open a stream
332 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
333 * @single_context: Whether a single or all gpu contexts should be monitored
334 * @ctx_handle: A gem ctx handle for use with @single_context
335 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
336 * @oa_format: An OA unit HW report format
337 * @oa_periodic: Whether to enable periodic OA unit sampling
338 * @oa_period_exponent: The OA unit sampling period is derived from this
339 *
340 * As read_properties_unlocked() enumerates and validates the properties given
341 * to open a stream of metrics the configuration is built up in the structure
342 * which starts out zero initialized.
343 */
Robert Braggeec688e2016-11-07 19:49:47 +0000344struct perf_open_properties {
345 u32 sample_flags;
346
347 u64 single_context:1;
348 u64 ctx_handle;
Robert Braggd7965152016-11-07 19:49:52 +0000349
350 /* OA sampling state */
351 int metrics_set;
352 int oa_format;
353 bool oa_periodic;
354 int oa_period_exponent;
Robert Braggeec688e2016-11-07 19:49:47 +0000355};
356
Robert Bragg19f81df2017-06-13 12:23:03 +0100357static u32 gen8_oa_hw_tail_read(struct drm_i915_private *dev_priv)
358{
359 return I915_READ(GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
360}
361
362static u32 gen7_oa_hw_tail_read(struct drm_i915_private *dev_priv)
363{
364 u32 oastatus1 = I915_READ(GEN7_OASTATUS1);
365
366 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
367}
368
Robert Bragg0dd860c2017-05-11 16:43:28 +0100369/**
Robert Bragg19f81df2017-06-13 12:23:03 +0100370 * oa_buffer_check_unlocked - check for data and update tail ptr state
Robert Bragg0dd860c2017-05-11 16:43:28 +0100371 * @dev_priv: i915 device instance
Robert Braggd7965152016-11-07 19:49:52 +0000372 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100373 * This is either called via fops (for blocking reads in user ctx) or the poll
374 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
375 * if there is data available for userspace to read.
Robert Braggd7965152016-11-07 19:49:52 +0000376 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100377 * This function is central to providing a workaround for the OA unit tail
378 * pointer having a race with respect to what data is visible to the CPU.
379 * It is responsible for reading tail pointers from the hardware and giving
380 * the pointers time to 'age' before they are made available for reading.
381 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
382 *
383 * Besides returning true when there is data available to read() this function
384 * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp
385 * and .aged_tail_idx state used for reading.
386 *
387 * Note: It's safe to read OA config state here unlocked, assuming that this is
388 * only called while the stream is enabled, while the global OA configuration
389 * can't be modified.
390 *
391 * Returns: %true if the OA buffer contains data, else %false
Robert Braggd7965152016-11-07 19:49:52 +0000392 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100393static bool oa_buffer_check_unlocked(struct drm_i915_private *dev_priv)
Robert Braggd7965152016-11-07 19:49:52 +0000394{
395 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100396 unsigned long flags;
397 unsigned int aged_idx;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100398 u32 head, hw_tail, aged_tail, aging_tail;
399 u64 now;
Robert Braggd7965152016-11-07 19:49:52 +0000400
Robert Bragg0dd860c2017-05-11 16:43:28 +0100401 /* We have to consider the (unlikely) possibility that read() errors
402 * could result in an OA buffer reset which might reset the head,
403 * tails[] and aged_tail state.
404 */
405 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
406
407 /* NB: The head we observe here might effectively be a little out of
408 * date (between head and tails[aged_idx].offset if there is currently
409 * a read() in progress.
410 */
411 head = dev_priv->perf.oa.oa_buffer.head;
412
413 aged_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
414 aged_tail = dev_priv->perf.oa.oa_buffer.tails[aged_idx].offset;
415 aging_tail = dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset;
416
Robert Bragg19f81df2017-06-13 12:23:03 +0100417 hw_tail = dev_priv->perf.oa.ops.oa_hw_tail_read(dev_priv);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100418
419 /* The tail pointer increases in 64 byte increments,
420 * not in report_size steps...
421 */
422 hw_tail &= ~(report_size - 1);
423
424 now = ktime_get_mono_fast_ns();
425
Robert Bragg4117ebc2017-05-11 16:43:30 +0100426 /* Update the aged tail
427 *
428 * Flip the tail pointer available for read()s once the aging tail is
429 * old enough to trust that the corresponding data will be visible to
430 * the CPU...
431 *
432 * Do this before updating the aging pointer in case we may be able to
433 * immediately start aging a new pointer too (if new data has become
434 * available) without needing to wait for a later hrtimer callback.
435 */
436 if (aging_tail != INVALID_TAIL_PTR &&
437 ((now - dev_priv->perf.oa.oa_buffer.aging_timestamp) >
438 OA_TAIL_MARGIN_NSEC)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100439
Robert Bragg4117ebc2017-05-11 16:43:30 +0100440 aged_idx ^= 1;
441 dev_priv->perf.oa.oa_buffer.aged_tail_idx = aged_idx;
442
443 aged_tail = aging_tail;
444
445 /* Mark that we need a new pointer to start aging... */
446 dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR;
447 aging_tail = INVALID_TAIL_PTR;
448 }
449
Robert Bragg0dd860c2017-05-11 16:43:28 +0100450 /* Update the aging tail
451 *
452 * We throttle aging tail updates until we have a new tail that
453 * represents >= one report more data than is already available for
454 * reading. This ensures there will be enough data for a successful
455 * read once this new pointer has aged and ensures we will give the new
456 * pointer time to age.
457 */
458 if (aging_tail == INVALID_TAIL_PTR &&
459 (aged_tail == INVALID_TAIL_PTR ||
460 OA_TAKEN(hw_tail, aged_tail) >= report_size)) {
461 struct i915_vma *vma = dev_priv->perf.oa.oa_buffer.vma;
462 u32 gtt_offset = i915_ggtt_offset(vma);
463
464 /* Be paranoid and do a bounds check on the pointer read back
465 * from hardware, just in case some spurious hardware condition
466 * could put the tail out of bounds...
467 */
468 if (hw_tail >= gtt_offset &&
469 hw_tail < (gtt_offset + OA_BUFFER_SIZE)) {
470 dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset =
471 aging_tail = hw_tail;
472 dev_priv->perf.oa.oa_buffer.aging_timestamp = now;
473 } else {
474 DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %u\n",
475 hw_tail);
476 }
477 }
478
Robert Bragg0dd860c2017-05-11 16:43:28 +0100479 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
480
481 return aged_tail == INVALID_TAIL_PTR ?
482 false : OA_TAKEN(aged_tail, head) >= report_size;
Robert Braggd7965152016-11-07 19:49:52 +0000483}
484
485/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000486 * append_oa_status - Appends a status record to a userspace read() buffer.
487 * @stream: An i915-perf stream opened for OA metrics
488 * @buf: destination buffer given by userspace
489 * @count: the number of bytes userspace wants to read
490 * @offset: (inout): the current position for writing into @buf
491 * @type: The kind of status to report to userspace
492 *
493 * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
494 * into the userspace read() buffer.
495 *
496 * The @buf @offset will only be updated on success.
497 *
498 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000499 */
500static int append_oa_status(struct i915_perf_stream *stream,
501 char __user *buf,
502 size_t count,
503 size_t *offset,
504 enum drm_i915_perf_record_type type)
505{
506 struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
507
508 if ((count - *offset) < header.size)
509 return -ENOSPC;
510
511 if (copy_to_user(buf + *offset, &header, sizeof(header)))
512 return -EFAULT;
513
514 (*offset) += header.size;
515
516 return 0;
517}
518
519/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000520 * append_oa_sample - Copies single OA report into userspace read() buffer.
521 * @stream: An i915-perf stream opened for OA metrics
522 * @buf: destination buffer given by userspace
523 * @count: the number of bytes userspace wants to read
524 * @offset: (inout): the current position for writing into @buf
525 * @report: A single OA report to (optionally) include as part of the sample
526 *
527 * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
528 * properties when opening a stream, tracked as `stream->sample_flags`. This
529 * function copies the requested components of a single sample to the given
530 * read() @buf.
531 *
532 * The @buf @offset will only be updated on success.
533 *
534 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000535 */
536static int append_oa_sample(struct i915_perf_stream *stream,
537 char __user *buf,
538 size_t count,
539 size_t *offset,
540 const u8 *report)
541{
542 struct drm_i915_private *dev_priv = stream->dev_priv;
543 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
544 struct drm_i915_perf_record_header header;
545 u32 sample_flags = stream->sample_flags;
546
547 header.type = DRM_I915_PERF_RECORD_SAMPLE;
548 header.pad = 0;
549 header.size = stream->sample_size;
550
551 if ((count - *offset) < header.size)
552 return -ENOSPC;
553
554 buf += *offset;
555 if (copy_to_user(buf, &header, sizeof(header)))
556 return -EFAULT;
557 buf += sizeof(header);
558
559 if (sample_flags & SAMPLE_OA_REPORT) {
560 if (copy_to_user(buf, report, report_size))
561 return -EFAULT;
562 }
563
564 (*offset) += header.size;
565
566 return 0;
567}
568
569/**
570 * Copies all buffered OA reports into userspace read() buffer.
571 * @stream: An i915-perf stream opened for OA metrics
572 * @buf: destination buffer given by userspace
573 * @count: the number of bytes userspace wants to read
574 * @offset: (inout): the current position for writing into @buf
Robert Braggd7965152016-11-07 19:49:52 +0000575 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000576 * Notably any error condition resulting in a short read (-%ENOSPC or
577 * -%EFAULT) will be returned even though one or more records may
Robert Braggd7965152016-11-07 19:49:52 +0000578 * have been successfully copied. In this case it's up to the caller
579 * to decide if the error should be squashed before returning to
580 * userspace.
581 *
582 * Note: reports are consumed from the head, and appended to the
Robert Bragge81b3a52017-05-11 16:43:24 +0100583 * tail, so the tail chases the head?... If you think that's mad
Robert Braggd7965152016-11-07 19:49:52 +0000584 * and back-to-front you're not alone, but this follows the
585 * Gen PRM naming convention.
Robert Bragg16d98b32016-12-07 21:40:33 +0000586 *
587 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000588 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100589static int gen8_append_oa_reports(struct i915_perf_stream *stream,
590 char __user *buf,
591 size_t count,
592 size_t *offset)
593{
594 struct drm_i915_private *dev_priv = stream->dev_priv;
595 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
596 u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr;
597 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
598 u32 mask = (OA_BUFFER_SIZE - 1);
599 size_t start_offset = *offset;
600 unsigned long flags;
601 unsigned int aged_tail_idx;
602 u32 head, tail;
603 u32 taken;
604 int ret = 0;
605
606 if (WARN_ON(!stream->enabled))
607 return -EIO;
608
609 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
610
611 head = dev_priv->perf.oa.oa_buffer.head;
612 aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
613 tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset;
614
615 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
616
617 /*
618 * An invalid tail pointer here means we're still waiting for the poll
619 * hrtimer callback to give us a pointer
620 */
621 if (tail == INVALID_TAIL_PTR)
622 return -EAGAIN;
623
624 /*
625 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
626 * while indexing relative to oa_buf_base.
627 */
628 head -= gtt_offset;
629 tail -= gtt_offset;
630
631 /*
632 * An out of bounds or misaligned head or tail pointer implies a driver
633 * bug since we validate + align the tail pointers we read from the
634 * hardware and we are in full control of the head pointer which should
635 * only be incremented by multiples of the report size (notably also
636 * all a power of two).
637 */
638 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
639 tail > OA_BUFFER_SIZE || tail % report_size,
640 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
641 head, tail))
642 return -EIO;
643
644
645 for (/* none */;
646 (taken = OA_TAKEN(tail, head));
647 head = (head + report_size) & mask) {
648 u8 *report = oa_buf_base + head;
649 u32 *report32 = (void *)report;
650 u32 ctx_id;
651 u32 reason;
652
653 /*
654 * All the report sizes factor neatly into the buffer
655 * size so we never expect to see a report split
656 * between the beginning and end of the buffer.
657 *
658 * Given the initial alignment check a misalignment
659 * here would imply a driver bug that would result
660 * in an overrun.
661 */
662 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
663 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
664 break;
665 }
666
667 /*
668 * The reason field includes flags identifying what
669 * triggered this specific report (mostly timer
670 * triggered or e.g. due to a context switch).
671 *
672 * This field is never expected to be zero so we can
673 * check that the report isn't invalid before copying
674 * it to userspace...
675 */
676 reason = ((report32[0] >> OAREPORT_REASON_SHIFT) &
677 OAREPORT_REASON_MASK);
678 if (reason == 0) {
679 if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs))
680 DRM_NOTE("Skipping spurious, invalid OA report\n");
681 continue;
682 }
683
684 /*
685 * XXX: Just keep the lower 21 bits for now since I'm not
686 * entirely sure if the HW touches any of the higher bits in
687 * this field
688 */
689 ctx_id = report32[2] & 0x1fffff;
690
691 /*
692 * Squash whatever is in the CTX_ID field if it's marked as
693 * invalid to be sure we avoid false-positive, single-context
694 * filtering below...
695 *
696 * Note: that we don't clear the valid_ctx_bit so userspace can
697 * understand that the ID has been squashed by the kernel.
698 */
699 if (!(report32[0] & dev_priv->perf.oa.gen8_valid_ctx_bit))
700 ctx_id = report32[2] = INVALID_CTX_ID;
701
702 /*
703 * NB: For Gen 8 the OA unit no longer supports clock gating
704 * off for a specific context and the kernel can't securely
705 * stop the counters from updating as system-wide / global
706 * values.
707 *
708 * Automatic reports now include a context ID so reports can be
709 * filtered on the cpu but it's not worth trying to
710 * automatically subtract/hide counter progress for other
711 * contexts while filtering since we can't stop userspace
712 * issuing MI_REPORT_PERF_COUNT commands which would still
713 * provide a side-band view of the real values.
714 *
715 * To allow userspace (such as Mesa/GL_INTEL_performance_query)
716 * to normalize counters for a single filtered context then it
717 * needs be forwarded bookend context-switch reports so that it
718 * can track switches in between MI_REPORT_PERF_COUNT commands
719 * and can itself subtract/ignore the progress of counters
720 * associated with other contexts. Note that the hardware
721 * automatically triggers reports when switching to a new
722 * context which are tagged with the ID of the newly active
723 * context. To avoid the complexity (and likely fragility) of
724 * reading ahead while parsing reports to try and minimize
725 * forwarding redundant context switch reports (i.e. between
726 * other, unrelated contexts) we simply elect to forward them
727 * all.
728 *
729 * We don't rely solely on the reason field to identify context
730 * switches since it's not-uncommon for periodic samples to
731 * identify a switch before any 'context switch' report.
732 */
733 if (!dev_priv->perf.oa.exclusive_stream->ctx ||
734 dev_priv->perf.oa.specific_ctx_id == ctx_id ||
735 (dev_priv->perf.oa.oa_buffer.last_ctx_id ==
736 dev_priv->perf.oa.specific_ctx_id) ||
737 reason & OAREPORT_REASON_CTX_SWITCH) {
738
739 /*
740 * While filtering for a single context we avoid
741 * leaking the IDs of other contexts.
742 */
743 if (dev_priv->perf.oa.exclusive_stream->ctx &&
744 dev_priv->perf.oa.specific_ctx_id != ctx_id) {
745 report32[2] = INVALID_CTX_ID;
746 }
747
748 ret = append_oa_sample(stream, buf, count, offset,
749 report);
750 if (ret)
751 break;
752
753 dev_priv->perf.oa.oa_buffer.last_ctx_id = ctx_id;
754 }
755
756 /*
757 * The above reason field sanity check is based on
758 * the assumption that the OA buffer is initially
759 * zeroed and we reset the field after copying so the
760 * check is still meaningful once old reports start
761 * being overwritten.
762 */
763 report32[0] = 0;
764 }
765
766 if (start_offset != *offset) {
767 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
768
769 /*
770 * We removed the gtt_offset for the copy loop above, indexing
771 * relative to oa_buf_base so put back here...
772 */
773 head += gtt_offset;
774
775 I915_WRITE(GEN8_OAHEADPTR, head & GEN8_OAHEADPTR_MASK);
776 dev_priv->perf.oa.oa_buffer.head = head;
777
778 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
779 }
780
781 return ret;
782}
783
784/**
785 * gen8_oa_read - copy status records then buffered OA reports
786 * @stream: An i915-perf stream opened for OA metrics
787 * @buf: destination buffer given by userspace
788 * @count: the number of bytes userspace wants to read
789 * @offset: (inout): the current position for writing into @buf
790 *
791 * Checks OA unit status registers and if necessary appends corresponding
792 * status records for userspace (such as for a buffer full condition) and then
793 * initiate appending any buffered OA reports.
794 *
795 * Updates @offset according to the number of bytes successfully copied into
796 * the userspace buffer.
797 *
798 * NB: some data may be successfully copied to the userspace buffer
799 * even if an error is returned, and this is reflected in the
800 * updated @offset.
801 *
802 * Returns: zero on success or a negative error code
803 */
804static int gen8_oa_read(struct i915_perf_stream *stream,
805 char __user *buf,
806 size_t count,
807 size_t *offset)
808{
809 struct drm_i915_private *dev_priv = stream->dev_priv;
810 u32 oastatus;
811 int ret;
812
813 if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr))
814 return -EIO;
815
816 oastatus = I915_READ(GEN8_OASTATUS);
817
818 /*
819 * We treat OABUFFER_OVERFLOW as a significant error:
820 *
821 * Although theoretically we could handle this more gracefully
822 * sometimes, some Gens don't correctly suppress certain
823 * automatically triggered reports in this condition and so we
824 * have to assume that old reports are now being trampled
825 * over.
826 *
827 * Considering how we don't currently give userspace control
828 * over the OA buffer size and always configure a large 16MB
829 * buffer, then a buffer overflow does anyway likely indicate
830 * that something has gone quite badly wrong.
831 */
832 if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
833 ret = append_oa_status(stream, buf, count, offset,
834 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
835 if (ret)
836 return ret;
837
838 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
839 dev_priv->perf.oa.period_exponent);
840
841 dev_priv->perf.oa.ops.oa_disable(dev_priv);
842 dev_priv->perf.oa.ops.oa_enable(dev_priv);
843
844 /*
845 * Note: .oa_enable() is expected to re-init the oabuffer and
846 * reset GEN8_OASTATUS for us
847 */
848 oastatus = I915_READ(GEN8_OASTATUS);
849 }
850
851 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
852 ret = append_oa_status(stream, buf, count, offset,
853 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
854 if (ret)
855 return ret;
856 I915_WRITE(GEN8_OASTATUS,
857 oastatus & ~GEN8_OASTATUS_REPORT_LOST);
858 }
859
860 return gen8_append_oa_reports(stream, buf, count, offset);
861}
862
863/**
864 * Copies all buffered OA reports into userspace read() buffer.
865 * @stream: An i915-perf stream opened for OA metrics
866 * @buf: destination buffer given by userspace
867 * @count: the number of bytes userspace wants to read
868 * @offset: (inout): the current position for writing into @buf
869 *
870 * Notably any error condition resulting in a short read (-%ENOSPC or
871 * -%EFAULT) will be returned even though one or more records may
872 * have been successfully copied. In this case it's up to the caller
873 * to decide if the error should be squashed before returning to
874 * userspace.
875 *
876 * Note: reports are consumed from the head, and appended to the
877 * tail, so the tail chases the head?... If you think that's mad
878 * and back-to-front you're not alone, but this follows the
879 * Gen PRM naming convention.
880 *
881 * Returns: 0 on success, negative error code on failure.
882 */
Robert Braggd7965152016-11-07 19:49:52 +0000883static int gen7_append_oa_reports(struct i915_perf_stream *stream,
884 char __user *buf,
885 size_t count,
Robert Bragg3bb335c2017-05-11 16:43:27 +0100886 size_t *offset)
Robert Braggd7965152016-11-07 19:49:52 +0000887{
888 struct drm_i915_private *dev_priv = stream->dev_priv;
889 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
890 u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr;
Robert Braggd7965152016-11-07 19:49:52 +0000891 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
892 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100893 size_t start_offset = *offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100894 unsigned long flags;
895 unsigned int aged_tail_idx;
896 u32 head, tail;
Robert Braggd7965152016-11-07 19:49:52 +0000897 u32 taken;
898 int ret = 0;
899
900 if (WARN_ON(!stream->enabled))
901 return -EIO;
902
Robert Bragg0dd860c2017-05-11 16:43:28 +0100903 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
Robert Braggf2790202017-05-11 16:43:26 +0100904
Robert Bragg0dd860c2017-05-11 16:43:28 +0100905 head = dev_priv->perf.oa.oa_buffer.head;
906 aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
907 tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset;
908
909 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
910
911 /* An invalid tail pointer here means we're still waiting for the poll
912 * hrtimer callback to give us a pointer
Robert Braggf2790202017-05-11 16:43:26 +0100913 */
Robert Bragg0dd860c2017-05-11 16:43:28 +0100914 if (tail == INVALID_TAIL_PTR)
Robert Braggd7965152016-11-07 19:49:52 +0000915 return -EAGAIN;
916
Robert Bragg0dd860c2017-05-11 16:43:28 +0100917 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
918 * while indexing relative to oa_buf_base.
919 */
920 head -= gtt_offset;
921 tail -= gtt_offset;
922
923 /* An out of bounds or misaligned head or tail pointer implies a driver
924 * bug since we validate + align the tail pointers we read from the
925 * hardware and we are in full control of the head pointer which should
926 * only be incremented by multiples of the report size (notably also
927 * all a power of two).
928 */
929 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
930 tail > OA_BUFFER_SIZE || tail % report_size,
931 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
932 head, tail))
933 return -EIO;
934
Robert Braggd7965152016-11-07 19:49:52 +0000935
936 for (/* none */;
937 (taken = OA_TAKEN(tail, head));
938 head = (head + report_size) & mask) {
939 u8 *report = oa_buf_base + head;
940 u32 *report32 = (void *)report;
941
942 /* All the report sizes factor neatly into the buffer
943 * size so we never expect to see a report split
944 * between the beginning and end of the buffer.
945 *
946 * Given the initial alignment check a misalignment
947 * here would imply a driver bug that would result
948 * in an overrun.
949 */
950 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
951 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
952 break;
953 }
954
955 /* The report-ID field for periodic samples includes
956 * some undocumented flags related to what triggered
957 * the report and is never expected to be zero so we
958 * can check that the report isn't invalid before
959 * copying it to userspace...
960 */
961 if (report32[0] == 0) {
Robert Bragg712122e2017-05-11 16:43:31 +0100962 if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs))
963 DRM_NOTE("Skipping spurious, invalid OA report\n");
Robert Braggd7965152016-11-07 19:49:52 +0000964 continue;
965 }
966
967 ret = append_oa_sample(stream, buf, count, offset, report);
968 if (ret)
969 break;
970
971 /* The above report-id field sanity check is based on
972 * the assumption that the OA buffer is initially
973 * zeroed and we reset the field after copying so the
974 * check is still meaningful once old reports start
975 * being overwritten.
976 */
977 report32[0] = 0;
978 }
979
Robert Bragg3bb335c2017-05-11 16:43:27 +0100980 if (start_offset != *offset) {
Robert Bragg0dd860c2017-05-11 16:43:28 +0100981 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
982
Robert Bragg3bb335c2017-05-11 16:43:27 +0100983 /* We removed the gtt_offset for the copy loop above, indexing
984 * relative to oa_buf_base so put back here...
985 */
986 head += gtt_offset;
987
988 I915_WRITE(GEN7_OASTATUS2,
989 ((head & GEN7_OASTATUS2_HEAD_MASK) |
990 OA_MEM_SELECT_GGTT));
991 dev_priv->perf.oa.oa_buffer.head = head;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100992
993 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100994 }
Robert Braggd7965152016-11-07 19:49:52 +0000995
996 return ret;
997}
998
Robert Bragg16d98b32016-12-07 21:40:33 +0000999/**
1000 * gen7_oa_read - copy status records then buffered OA reports
1001 * @stream: An i915-perf stream opened for OA metrics
1002 * @buf: destination buffer given by userspace
1003 * @count: the number of bytes userspace wants to read
1004 * @offset: (inout): the current position for writing into @buf
1005 *
1006 * Checks Gen 7 specific OA unit status registers and if necessary appends
1007 * corresponding status records for userspace (such as for a buffer full
1008 * condition) and then initiate appending any buffered OA reports.
1009 *
1010 * Updates @offset according to the number of bytes successfully copied into
1011 * the userspace buffer.
1012 *
1013 * Returns: zero on success or a negative error code
1014 */
Robert Braggd7965152016-11-07 19:49:52 +00001015static int gen7_oa_read(struct i915_perf_stream *stream,
1016 char __user *buf,
1017 size_t count,
1018 size_t *offset)
1019{
1020 struct drm_i915_private *dev_priv = stream->dev_priv;
Robert Braggd7965152016-11-07 19:49:52 +00001021 u32 oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001022 int ret;
1023
1024 if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr))
1025 return -EIO;
1026
Robert Braggd7965152016-11-07 19:49:52 +00001027 oastatus1 = I915_READ(GEN7_OASTATUS1);
1028
Robert Braggd7965152016-11-07 19:49:52 +00001029 /* XXX: On Haswell we don't have a safe way to clear oastatus1
1030 * bits while the OA unit is enabled (while the tail pointer
1031 * may be updated asynchronously) so we ignore status bits
1032 * that have already been reported to userspace.
1033 */
1034 oastatus1 &= ~dev_priv->perf.oa.gen7_latched_oastatus1;
1035
1036 /* We treat OABUFFER_OVERFLOW as a significant error:
1037 *
1038 * - The status can be interpreted to mean that the buffer is
1039 * currently full (with a higher precedence than OA_TAKEN()
1040 * which will start to report a near-empty buffer after an
1041 * overflow) but it's awkward that we can't clear the status
1042 * on Haswell, so without a reset we won't be able to catch
1043 * the state again.
1044 *
1045 * - Since it also implies the HW has started overwriting old
1046 * reports it may also affect our sanity checks for invalid
1047 * reports when copying to userspace that assume new reports
1048 * are being written to cleared memory.
1049 *
1050 * - In the future we may want to introduce a flight recorder
1051 * mode where the driver will automatically maintain a safe
1052 * guard band between head/tail, avoiding this overflow
1053 * condition, but we avoid the added driver complexity for
1054 * now.
1055 */
1056 if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
1057 ret = append_oa_status(stream, buf, count, offset,
1058 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
1059 if (ret)
1060 return ret;
1061
Robert Bragg19f81df2017-06-13 12:23:03 +01001062 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
1063 dev_priv->perf.oa.period_exponent);
Robert Braggd7965152016-11-07 19:49:52 +00001064
1065 dev_priv->perf.oa.ops.oa_disable(dev_priv);
1066 dev_priv->perf.oa.ops.oa_enable(dev_priv);
1067
Robert Braggd7965152016-11-07 19:49:52 +00001068 oastatus1 = I915_READ(GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001069 }
1070
1071 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
1072 ret = append_oa_status(stream, buf, count, offset,
1073 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1074 if (ret)
1075 return ret;
1076 dev_priv->perf.oa.gen7_latched_oastatus1 |=
1077 GEN7_OASTATUS1_REPORT_LOST;
1078 }
1079
Robert Bragg3bb335c2017-05-11 16:43:27 +01001080 return gen7_append_oa_reports(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001081}
1082
Robert Bragg16d98b32016-12-07 21:40:33 +00001083/**
1084 * i915_oa_wait_unlocked - handles blocking IO until OA data available
1085 * @stream: An i915-perf stream opened for OA metrics
1086 *
1087 * Called when userspace tries to read() from a blocking stream FD opened
1088 * for OA metrics. It waits until the hrtimer callback finds a non-empty
1089 * OA buffer and wakes us.
1090 *
1091 * Note: it's acceptable to have this return with some false positives
1092 * since any subsequent read handling will return -EAGAIN if there isn't
1093 * really data ready for userspace yet.
1094 *
1095 * Returns: zero on success or a negative error code
1096 */
Robert Braggd7965152016-11-07 19:49:52 +00001097static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
1098{
1099 struct drm_i915_private *dev_priv = stream->dev_priv;
1100
1101 /* We would wait indefinitely if periodic sampling is not enabled */
1102 if (!dev_priv->perf.oa.periodic)
1103 return -EIO;
1104
Robert Braggd7965152016-11-07 19:49:52 +00001105 return wait_event_interruptible(dev_priv->perf.oa.poll_wq,
Robert Bragg19f81df2017-06-13 12:23:03 +01001106 oa_buffer_check_unlocked(dev_priv));
Robert Braggd7965152016-11-07 19:49:52 +00001107}
1108
Robert Bragg16d98b32016-12-07 21:40:33 +00001109/**
1110 * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
1111 * @stream: An i915-perf stream opened for OA metrics
1112 * @file: An i915 perf stream file
1113 * @wait: poll() state table
1114 *
1115 * For handling userspace polling on an i915 perf stream opened for OA metrics,
1116 * this starts a poll_wait with the wait queue that our hrtimer callback wakes
1117 * when it sees data ready to read in the circular OA buffer.
1118 */
Robert Braggd7965152016-11-07 19:49:52 +00001119static void i915_oa_poll_wait(struct i915_perf_stream *stream,
1120 struct file *file,
1121 poll_table *wait)
1122{
1123 struct drm_i915_private *dev_priv = stream->dev_priv;
1124
1125 poll_wait(file, &dev_priv->perf.oa.poll_wq, wait);
1126}
1127
Robert Bragg16d98b32016-12-07 21:40:33 +00001128/**
1129 * i915_oa_read - just calls through to &i915_oa_ops->read
1130 * @stream: An i915-perf stream opened for OA metrics
1131 * @buf: destination buffer given by userspace
1132 * @count: the number of bytes userspace wants to read
1133 * @offset: (inout): the current position for writing into @buf
1134 *
1135 * Updates @offset according to the number of bytes successfully copied into
1136 * the userspace buffer.
1137 *
1138 * Returns: zero on success or a negative error code
1139 */
Robert Braggd7965152016-11-07 19:49:52 +00001140static int i915_oa_read(struct i915_perf_stream *stream,
1141 char __user *buf,
1142 size_t count,
1143 size_t *offset)
1144{
1145 struct drm_i915_private *dev_priv = stream->dev_priv;
1146
1147 return dev_priv->perf.oa.ops.read(stream, buf, count, offset);
1148}
1149
Robert Bragg16d98b32016-12-07 21:40:33 +00001150/**
1151 * oa_get_render_ctx_id - determine and hold ctx hw id
1152 * @stream: An i915-perf stream opened for OA metrics
1153 *
1154 * Determine the render context hw id, and ensure it remains fixed for the
Robert Braggd7965152016-11-07 19:49:52 +00001155 * lifetime of the stream. This ensures that we don't have to worry about
1156 * updating the context ID in OACONTROL on the fly.
Robert Bragg16d98b32016-12-07 21:40:33 +00001157 *
1158 * Returns: zero on success or a negative error code
Robert Braggd7965152016-11-07 19:49:52 +00001159 */
1160static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
1161{
1162 struct drm_i915_private *dev_priv = stream->dev_priv;
Robert Braggd7965152016-11-07 19:49:52 +00001163
Robert Bragg19f81df2017-06-13 12:23:03 +01001164 if (i915.enable_execlists)
1165 dev_priv->perf.oa.specific_ctx_id = stream->ctx->hw_id;
1166 else {
1167 struct intel_engine_cs *engine = dev_priv->engine[RCS];
1168 struct intel_ring *ring;
1169 int ret;
Robert Braggd7965152016-11-07 19:49:52 +00001170
Robert Bragg19f81df2017-06-13 12:23:03 +01001171 ret = i915_mutex_lock_interruptible(&dev_priv->drm);
1172 if (ret)
1173 return ret;
Robert Braggd7965152016-11-07 19:49:52 +00001174
Robert Bragg19f81df2017-06-13 12:23:03 +01001175 /*
1176 * As the ID is the gtt offset of the context's vma we
1177 * pin the vma to ensure the ID remains fixed.
1178 *
1179 * NB: implied RCS engine...
1180 */
1181 ring = engine->context_pin(engine, stream->ctx);
1182 mutex_unlock(&dev_priv->drm.struct_mutex);
1183 if (IS_ERR(ring))
1184 return PTR_ERR(ring);
1185
1186
1187 /*
1188 * Explicitly track the ID (instead of calling
1189 * i915_ggtt_offset() on the fly) considering the difference
1190 * with gen8+ and execlists
1191 */
1192 dev_priv->perf.oa.specific_ctx_id =
1193 i915_ggtt_offset(stream->ctx->engine[engine->id].state);
1194 }
Robert Braggd7965152016-11-07 19:49:52 +00001195
Chris Wilson266a2402017-05-04 10:33:08 +01001196 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001197}
1198
Robert Bragg16d98b32016-12-07 21:40:33 +00001199/**
1200 * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
1201 * @stream: An i915-perf stream opened for OA metrics
1202 *
1203 * In case anything needed doing to ensure the context HW ID would remain valid
1204 * for the lifetime of the stream, then that can be undone here.
1205 */
Robert Braggd7965152016-11-07 19:49:52 +00001206static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
1207{
1208 struct drm_i915_private *dev_priv = stream->dev_priv;
1209
Robert Bragg19f81df2017-06-13 12:23:03 +01001210 if (i915.enable_execlists) {
1211 dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID;
1212 } else {
1213 struct intel_engine_cs *engine = dev_priv->engine[RCS];
Robert Braggd7965152016-11-07 19:49:52 +00001214
Robert Bragg19f81df2017-06-13 12:23:03 +01001215 mutex_lock(&dev_priv->drm.struct_mutex);
Robert Braggd7965152016-11-07 19:49:52 +00001216
Robert Bragg19f81df2017-06-13 12:23:03 +01001217 dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID;
1218 engine->context_unpin(engine, stream->ctx);
1219
1220 mutex_unlock(&dev_priv->drm.struct_mutex);
1221 }
Robert Braggd7965152016-11-07 19:49:52 +00001222}
1223
1224static void
1225free_oa_buffer(struct drm_i915_private *i915)
1226{
1227 mutex_lock(&i915->drm.struct_mutex);
1228
1229 i915_gem_object_unpin_map(i915->perf.oa.oa_buffer.vma->obj);
1230 i915_vma_unpin(i915->perf.oa.oa_buffer.vma);
1231 i915_gem_object_put(i915->perf.oa.oa_buffer.vma->obj);
1232
1233 i915->perf.oa.oa_buffer.vma = NULL;
1234 i915->perf.oa.oa_buffer.vaddr = NULL;
1235
1236 mutex_unlock(&i915->drm.struct_mutex);
1237}
1238
1239static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
1240{
1241 struct drm_i915_private *dev_priv = stream->dev_priv;
1242
1243 BUG_ON(stream != dev_priv->perf.oa.exclusive_stream);
1244
Robert Bragg19f81df2017-06-13 12:23:03 +01001245 /*
1246 * Unset exclusive_stream first, it might be checked while
1247 * disabling the metric set on gen8+.
1248 */
1249 dev_priv->perf.oa.exclusive_stream = NULL;
1250
Robert Braggd7965152016-11-07 19:49:52 +00001251 dev_priv->perf.oa.ops.disable_metric_set(dev_priv);
1252
1253 free_oa_buffer(dev_priv);
1254
1255 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
1256 intel_runtime_pm_put(dev_priv);
1257
1258 if (stream->ctx)
1259 oa_put_render_ctx_id(stream);
1260
Robert Bragg712122e2017-05-11 16:43:31 +01001261 if (dev_priv->perf.oa.spurious_report_rs.missed) {
1262 DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n",
1263 dev_priv->perf.oa.spurious_report_rs.missed);
1264 }
Robert Braggd7965152016-11-07 19:49:52 +00001265}
1266
1267static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv)
1268{
1269 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001270 unsigned long flags;
1271
1272 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
Robert Braggd7965152016-11-07 19:49:52 +00001273
1274 /* Pre-DevBDW: OABUFFER must be set with counters off,
1275 * before OASTATUS1, but after OASTATUS2
1276 */
1277 I915_WRITE(GEN7_OASTATUS2, gtt_offset | OA_MEM_SELECT_GGTT); /* head */
Robert Braggf2790202017-05-11 16:43:26 +01001278 dev_priv->perf.oa.oa_buffer.head = gtt_offset;
1279
Robert Braggd7965152016-11-07 19:49:52 +00001280 I915_WRITE(GEN7_OABUFFER, gtt_offset);
Robert Braggf2790202017-05-11 16:43:26 +01001281
Robert Braggd7965152016-11-07 19:49:52 +00001282 I915_WRITE(GEN7_OASTATUS1, gtt_offset | OABUFFER_SIZE_16M); /* tail */
1283
Robert Bragg0dd860c2017-05-11 16:43:28 +01001284 /* Mark that we need updated tail pointers to read from... */
1285 dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1286 dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1287
1288 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1289
Robert Braggd7965152016-11-07 19:49:52 +00001290 /* On Haswell we have to track which OASTATUS1 flags we've
1291 * already seen since they can't be cleared while periodic
1292 * sampling is enabled.
1293 */
1294 dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
1295
1296 /* NB: although the OA buffer will initially be allocated
1297 * zeroed via shmfs (and so this memset is redundant when
1298 * first allocating), we may re-init the OA buffer, either
1299 * when re-enabling a stream or in error/reset paths.
1300 *
1301 * The reason we clear the buffer for each re-init is for the
1302 * sanity check in gen7_append_oa_reports() that looks at the
1303 * report-id field to make sure it's non-zero which relies on
1304 * the assumption that new reports are being written to zeroed
1305 * memory...
1306 */
1307 memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1308
1309 /* Maybe make ->pollin per-stream state if we support multiple
1310 * concurrent streams in the future.
1311 */
1312 dev_priv->perf.oa.pollin = false;
1313}
1314
Robert Bragg19f81df2017-06-13 12:23:03 +01001315static void gen8_init_oa_buffer(struct drm_i915_private *dev_priv)
1316{
1317 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
1318 unsigned long flags;
1319
1320 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1321
1322 I915_WRITE(GEN8_OASTATUS, 0);
1323 I915_WRITE(GEN8_OAHEADPTR, gtt_offset);
1324 dev_priv->perf.oa.oa_buffer.head = gtt_offset;
1325
1326 I915_WRITE(GEN8_OABUFFER_UDW, 0);
1327
1328 /*
1329 * PRM says:
1330 *
1331 * "This MMIO must be set before the OATAILPTR
1332 * register and after the OAHEADPTR register. This is
1333 * to enable proper functionality of the overflow
1334 * bit."
1335 */
1336 I915_WRITE(GEN8_OABUFFER, gtt_offset |
1337 OABUFFER_SIZE_16M | OA_MEM_SELECT_GGTT);
1338 I915_WRITE(GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
1339
1340 /* Mark that we need updated tail pointers to read from... */
1341 dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1342 dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1343
1344 /*
1345 * Reset state used to recognise context switches, affecting which
1346 * reports we will forward to userspace while filtering for a single
1347 * context.
1348 */
1349 dev_priv->perf.oa.oa_buffer.last_ctx_id = INVALID_CTX_ID;
1350
1351 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1352
1353 /*
1354 * NB: although the OA buffer will initially be allocated
1355 * zeroed via shmfs (and so this memset is redundant when
1356 * first allocating), we may re-init the OA buffer, either
1357 * when re-enabling a stream or in error/reset paths.
1358 *
1359 * The reason we clear the buffer for each re-init is for the
1360 * sanity check in gen8_append_oa_reports() that looks at the
1361 * reason field to make sure it's non-zero which relies on
1362 * the assumption that new reports are being written to zeroed
1363 * memory...
1364 */
1365 memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1366
1367 /*
1368 * Maybe make ->pollin per-stream state if we support multiple
1369 * concurrent streams in the future.
1370 */
1371 dev_priv->perf.oa.pollin = false;
1372}
1373
Robert Braggd7965152016-11-07 19:49:52 +00001374static int alloc_oa_buffer(struct drm_i915_private *dev_priv)
1375{
1376 struct drm_i915_gem_object *bo;
1377 struct i915_vma *vma;
1378 int ret;
1379
1380 if (WARN_ON(dev_priv->perf.oa.oa_buffer.vma))
1381 return -ENODEV;
1382
1383 ret = i915_mutex_lock_interruptible(&dev_priv->drm);
1384 if (ret)
1385 return ret;
1386
1387 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
1388 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
1389
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00001390 bo = i915_gem_object_create(dev_priv, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001391 if (IS_ERR(bo)) {
1392 DRM_ERROR("Failed to allocate OA buffer\n");
1393 ret = PTR_ERR(bo);
1394 goto unlock;
1395 }
1396
1397 ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC);
1398 if (ret)
1399 goto err_unref;
1400
1401 /* PreHSW required 512K alignment, HSW requires 16M */
1402 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0);
1403 if (IS_ERR(vma)) {
1404 ret = PTR_ERR(vma);
1405 goto err_unref;
1406 }
1407 dev_priv->perf.oa.oa_buffer.vma = vma;
1408
1409 dev_priv->perf.oa.oa_buffer.vaddr =
1410 i915_gem_object_pin_map(bo, I915_MAP_WB);
1411 if (IS_ERR(dev_priv->perf.oa.oa_buffer.vaddr)) {
1412 ret = PTR_ERR(dev_priv->perf.oa.oa_buffer.vaddr);
1413 goto err_unpin;
1414 }
1415
1416 dev_priv->perf.oa.ops.init_oa_buffer(dev_priv);
1417
1418 DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p\n",
1419 i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma),
1420 dev_priv->perf.oa.oa_buffer.vaddr);
1421
1422 goto unlock;
1423
1424err_unpin:
1425 __i915_vma_unpin(vma);
1426
1427err_unref:
1428 i915_gem_object_put(bo);
1429
1430 dev_priv->perf.oa.oa_buffer.vaddr = NULL;
1431 dev_priv->perf.oa.oa_buffer.vma = NULL;
1432
1433unlock:
1434 mutex_unlock(&dev_priv->drm.struct_mutex);
1435 return ret;
1436}
1437
1438static void config_oa_regs(struct drm_i915_private *dev_priv,
1439 const struct i915_oa_reg *regs,
1440 int n_regs)
1441{
1442 int i;
1443
1444 for (i = 0; i < n_regs; i++) {
1445 const struct i915_oa_reg *reg = regs + i;
1446
1447 I915_WRITE(reg->addr, reg->value);
1448 }
1449}
1450
1451static int hsw_enable_metric_set(struct drm_i915_private *dev_priv)
1452{
1453 int ret = i915_oa_select_metric_set_hsw(dev_priv);
Lionel Landwerlin3f488d92017-06-13 12:23:01 +01001454 int i;
Robert Braggd7965152016-11-07 19:49:52 +00001455
1456 if (ret)
1457 return ret;
1458
1459 I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) |
1460 GT_NOA_ENABLE));
1461
1462 /* PRM:
1463 *
1464 * OA unit is using “crclk” for its functionality. When trunk
1465 * level clock gating takes place, OA clock would be gated,
1466 * unable to count the events from non-render clock domain.
1467 * Render clock gating must be disabled when OA is enabled to
1468 * count the events from non-render domain. Unit level clock
1469 * gating for RCS should also be disabled.
1470 */
1471 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1472 ~GEN7_DOP_CLOCK_GATE_ENABLE));
1473 I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) |
1474 GEN6_CSUNIT_CLOCK_GATE_DISABLE));
1475
Lionel Landwerlin3f488d92017-06-13 12:23:01 +01001476 for (i = 0; i < dev_priv->perf.oa.n_mux_configs; i++) {
1477 config_oa_regs(dev_priv, dev_priv->perf.oa.mux_regs[i],
1478 dev_priv->perf.oa.mux_regs_lens[i]);
1479 }
Robert Braggd7965152016-11-07 19:49:52 +00001480
1481 /* It apparently takes a fairly long time for a new MUX
1482 * configuration to be be applied after these register writes.
1483 * This delay duration was derived empirically based on the
1484 * render_basic config but hopefully it covers the maximum
1485 * configuration latency.
1486 *
1487 * As a fallback, the checks in _append_oa_reports() to skip
1488 * invalid OA reports do also seem to work to discard reports
1489 * generated before this config has completed - albeit not
1490 * silently.
1491 *
1492 * Unfortunately this is essentially a magic number, since we
1493 * don't currently know of a reliable mechanism for predicting
1494 * how long the MUX config will take to apply and besides
1495 * seeing invalid reports we don't know of a reliable way to
1496 * explicitly check that the MUX config has landed.
1497 *
1498 * It's even possible we've miss characterized the underlying
1499 * problem - it just seems like the simplest explanation why
1500 * a delay at this location would mitigate any invalid reports.
1501 */
1502 usleep_range(15000, 20000);
1503
1504 config_oa_regs(dev_priv, dev_priv->perf.oa.b_counter_regs,
1505 dev_priv->perf.oa.b_counter_regs_len);
1506
1507 return 0;
1508}
1509
1510static void hsw_disable_metric_set(struct drm_i915_private *dev_priv)
1511{
1512 I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) &
1513 ~GEN6_CSUNIT_CLOCK_GATE_DISABLE));
1514 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) |
1515 GEN7_DOP_CLOCK_GATE_ENABLE));
1516
1517 I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
1518 ~GT_NOA_ENABLE));
1519}
1520
Robert Bragg19f81df2017-06-13 12:23:03 +01001521/*
1522 * NB: It must always remain pointer safe to run this even if the OA unit
1523 * has been disabled.
1524 *
1525 * It's fine to put out-of-date values into these per-context registers
1526 * in the case that the OA unit has been disabled.
1527 */
1528static void gen8_update_reg_state_unlocked(struct i915_gem_context *ctx,
1529 u32 *reg_state)
1530{
1531 struct drm_i915_private *dev_priv = ctx->i915;
1532 const struct i915_oa_reg *flex_regs = dev_priv->perf.oa.flex_regs;
1533 int n_flex_regs = dev_priv->perf.oa.flex_regs_len;
1534 u32 ctx_oactxctrl = dev_priv->perf.oa.ctx_oactxctrl_offset;
1535 u32 ctx_flexeu0 = dev_priv->perf.oa.ctx_flexeu0_offset;
1536 /* The MMIO offsets for Flex EU registers aren't contiguous */
1537 u32 flex_mmio[] = {
1538 i915_mmio_reg_offset(EU_PERF_CNTL0),
1539 i915_mmio_reg_offset(EU_PERF_CNTL1),
1540 i915_mmio_reg_offset(EU_PERF_CNTL2),
1541 i915_mmio_reg_offset(EU_PERF_CNTL3),
1542 i915_mmio_reg_offset(EU_PERF_CNTL4),
1543 i915_mmio_reg_offset(EU_PERF_CNTL5),
1544 i915_mmio_reg_offset(EU_PERF_CNTL6),
1545 };
1546 int i;
1547
1548 reg_state[ctx_oactxctrl] = i915_mmio_reg_offset(GEN8_OACTXCONTROL);
1549 reg_state[ctx_oactxctrl+1] = (dev_priv->perf.oa.period_exponent <<
1550 GEN8_OA_TIMER_PERIOD_SHIFT) |
1551 (dev_priv->perf.oa.periodic ?
1552 GEN8_OA_TIMER_ENABLE : 0) |
1553 GEN8_OA_COUNTER_RESUME;
1554
1555 for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) {
1556 u32 state_offset = ctx_flexeu0 + i * 2;
1557 u32 mmio = flex_mmio[i];
1558
1559 /*
1560 * This arbitrary default will select the 'EU FPU0 Pipeline
1561 * Active' event. In the future it's anticipated that there
1562 * will be an explicit 'No Event' we can select, but not yet...
1563 */
1564 u32 value = 0;
1565 int j;
1566
1567 for (j = 0; j < n_flex_regs; j++) {
1568 if (i915_mmio_reg_offset(flex_regs[j].addr) == mmio) {
1569 value = flex_regs[j].value;
1570 break;
1571 }
1572 }
1573
1574 reg_state[state_offset] = mmio;
1575 reg_state[state_offset+1] = value;
1576 }
1577}
1578
1579/*
1580 * Same as gen8_update_reg_state_unlocked only through the batchbuffer. This
1581 * is only used by the kernel context.
1582 */
1583static int gen8_emit_oa_config(struct drm_i915_gem_request *req)
1584{
1585 struct drm_i915_private *dev_priv = req->i915;
1586 const struct i915_oa_reg *flex_regs = dev_priv->perf.oa.flex_regs;
1587 int n_flex_regs = dev_priv->perf.oa.flex_regs_len;
1588 /* The MMIO offsets for Flex EU registers aren't contiguous */
1589 u32 flex_mmio[] = {
1590 i915_mmio_reg_offset(EU_PERF_CNTL0),
1591 i915_mmio_reg_offset(EU_PERF_CNTL1),
1592 i915_mmio_reg_offset(EU_PERF_CNTL2),
1593 i915_mmio_reg_offset(EU_PERF_CNTL3),
1594 i915_mmio_reg_offset(EU_PERF_CNTL4),
1595 i915_mmio_reg_offset(EU_PERF_CNTL5),
1596 i915_mmio_reg_offset(EU_PERF_CNTL6),
1597 };
1598 u32 *cs;
1599 int i;
1600
1601 cs = intel_ring_begin(req, n_flex_regs * 2 + 4);
1602 if (IS_ERR(cs))
1603 return PTR_ERR(cs);
1604
1605 *cs++ = MI_LOAD_REGISTER_IMM(n_flex_regs + 1);
1606
1607 *cs++ = i915_mmio_reg_offset(GEN8_OACTXCONTROL);
1608 *cs++ = (dev_priv->perf.oa.period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
1609 (dev_priv->perf.oa.periodic ? GEN8_OA_TIMER_ENABLE : 0) |
1610 GEN8_OA_COUNTER_RESUME;
1611
1612 for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) {
1613 u32 mmio = flex_mmio[i];
1614
1615 /*
1616 * This arbitrary default will select the 'EU FPU0 Pipeline
1617 * Active' event. In the future it's anticipated that there
1618 * will be an explicit 'No Event' we can select, but not
1619 * yet...
1620 */
1621 u32 value = 0;
1622 int j;
1623
1624 for (j = 0; j < n_flex_regs; j++) {
1625 if (i915_mmio_reg_offset(flex_regs[j].addr) == mmio) {
1626 value = flex_regs[j].value;
1627 break;
1628 }
1629 }
1630
1631 *cs++ = mmio;
1632 *cs++ = value;
1633 }
1634
1635 *cs++ = MI_NOOP;
1636 intel_ring_advance(req, cs);
1637
1638 return 0;
1639}
1640
1641static int gen8_switch_to_updated_kernel_context(struct drm_i915_private *dev_priv)
1642{
1643 struct intel_engine_cs *engine = dev_priv->engine[RCS];
1644 struct i915_gem_timeline *timeline;
1645 struct drm_i915_gem_request *req;
1646 int ret;
1647
1648 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1649
1650 i915_gem_retire_requests(dev_priv);
1651
1652 req = i915_gem_request_alloc(engine, dev_priv->kernel_context);
1653 if (IS_ERR(req))
1654 return PTR_ERR(req);
1655
1656 ret = gen8_emit_oa_config(req);
1657 if (ret) {
1658 i915_add_request(req);
1659 return ret;
1660 }
1661
1662 /* Queue this switch after all other activity */
1663 list_for_each_entry(timeline, &dev_priv->gt.timelines, link) {
1664 struct drm_i915_gem_request *prev;
1665 struct intel_timeline *tl;
1666
1667 tl = &timeline->engine[engine->id];
1668 prev = i915_gem_active_raw(&tl->last_request,
1669 &dev_priv->drm.struct_mutex);
1670 if (prev)
1671 i915_sw_fence_await_sw_fence_gfp(&req->submit,
1672 &prev->submit,
1673 GFP_KERNEL);
1674 }
1675
1676 ret = i915_switch_context(req);
1677 i915_add_request(req);
1678
1679 return ret;
1680}
1681
1682/*
1683 * Manages updating the per-context aspects of the OA stream
1684 * configuration across all contexts.
1685 *
1686 * The awkward consideration here is that OACTXCONTROL controls the
1687 * exponent for periodic sampling which is primarily used for system
1688 * wide profiling where we'd like a consistent sampling period even in
1689 * the face of context switches.
1690 *
1691 * Our approach of updating the register state context (as opposed to
1692 * say using a workaround batch buffer) ensures that the hardware
1693 * won't automatically reload an out-of-date timer exponent even
1694 * transiently before a WA BB could be parsed.
1695 *
1696 * This function needs to:
1697 * - Ensure the currently running context's per-context OA state is
1698 * updated
1699 * - Ensure that all existing contexts will have the correct per-context
1700 * OA state if they are scheduled for use.
1701 * - Ensure any new contexts will be initialized with the correct
1702 * per-context OA state.
1703 *
1704 * Note: it's only the RCS/Render context that has any OA state.
1705 */
1706static int gen8_configure_all_contexts(struct drm_i915_private *dev_priv,
1707 bool interruptible)
1708{
1709 struct i915_gem_context *ctx;
1710 int ret;
1711 unsigned int wait_flags = I915_WAIT_LOCKED;
1712
1713 if (interruptible) {
1714 ret = i915_mutex_lock_interruptible(&dev_priv->drm);
1715 if (ret)
1716 return ret;
1717
1718 wait_flags |= I915_WAIT_INTERRUPTIBLE;
1719 } else {
1720 mutex_lock(&dev_priv->drm.struct_mutex);
1721 }
1722
1723 /* Switch away from any user context. */
1724 ret = gen8_switch_to_updated_kernel_context(dev_priv);
1725 if (ret)
1726 goto out;
1727
1728 /*
1729 * The OA register config is setup through the context image. This image
1730 * might be written to by the GPU on context switch (in particular on
1731 * lite-restore). This means we can't safely update a context's image,
1732 * if this context is scheduled/submitted to run on the GPU.
1733 *
1734 * We could emit the OA register config through the batch buffer but
1735 * this might leave small interval of time where the OA unit is
1736 * configured at an invalid sampling period.
1737 *
1738 * So far the best way to work around this issue seems to be draining
1739 * the GPU from any submitted work.
1740 */
1741 ret = i915_gem_wait_for_idle(dev_priv, wait_flags);
1742 if (ret)
1743 goto out;
1744
1745 /* Update all contexts now that we've stalled the submission. */
1746 list_for_each_entry(ctx, &dev_priv->context_list, link) {
1747 struct intel_context *ce = &ctx->engine[RCS];
1748 u32 *regs;
1749
1750 /* OA settings will be set upon first use */
1751 if (!ce->state)
1752 continue;
1753
1754 regs = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
1755 if (IS_ERR(regs)) {
1756 ret = PTR_ERR(regs);
1757 goto out;
1758 }
1759
1760 ce->state->obj->mm.dirty = true;
1761 regs += LRC_STATE_PN * PAGE_SIZE / sizeof(*regs);
1762
1763 gen8_update_reg_state_unlocked(ctx, regs);
1764
1765 i915_gem_object_unpin_map(ce->state->obj);
1766 }
1767
1768 out:
1769 mutex_unlock(&dev_priv->drm.struct_mutex);
1770
1771 return ret;
1772}
1773
1774static int gen8_enable_metric_set(struct drm_i915_private *dev_priv)
1775{
1776 int ret = dev_priv->perf.oa.ops.select_metric_set(dev_priv);
1777 int i;
1778
1779 if (ret)
1780 return ret;
1781
1782 /*
1783 * We disable slice/unslice clock ratio change reports on SKL since
1784 * they are too noisy. The HW generates a lot of redundant reports
1785 * where the ratio hasn't really changed causing a lot of redundant
1786 * work to processes and increasing the chances we'll hit buffer
1787 * overruns.
1788 *
1789 * Although we don't currently use the 'disable overrun' OABUFFER
1790 * feature it's worth noting that clock ratio reports have to be
1791 * disabled before considering to use that feature since the HW doesn't
1792 * correctly block these reports.
1793 *
1794 * Currently none of the high-level metrics we have depend on knowing
1795 * this ratio to normalize.
1796 *
1797 * Note: This register is not power context saved and restored, but
1798 * that's OK considering that we disable RC6 while the OA unit is
1799 * enabled.
1800 *
1801 * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
1802 * be read back from automatically triggered reports, as part of the
1803 * RPT_ID field.
1804 */
1805 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1806 I915_WRITE(GEN8_OA_DEBUG,
1807 _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
1808 GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
1809 }
1810
1811 /*
1812 * Update all contexts prior writing the mux configurations as we need
1813 * to make sure all slices/subslices are ON before writing to NOA
1814 * registers.
1815 */
1816 ret = gen8_configure_all_contexts(dev_priv, true);
1817 if (ret)
1818 return ret;
1819
1820 I915_WRITE(GDT_CHICKEN_BITS, 0xA0);
1821 for (i = 0; i < dev_priv->perf.oa.n_mux_configs; i++) {
1822 config_oa_regs(dev_priv, dev_priv->perf.oa.mux_regs[i],
1823 dev_priv->perf.oa.mux_regs_lens[i]);
1824 }
1825 I915_WRITE(GDT_CHICKEN_BITS, 0x80);
1826
1827 config_oa_regs(dev_priv, dev_priv->perf.oa.b_counter_regs,
1828 dev_priv->perf.oa.b_counter_regs_len);
1829
1830 return 0;
1831}
1832
1833static void gen8_disable_metric_set(struct drm_i915_private *dev_priv)
1834{
1835 /* Reset all contexts' slices/subslices configurations. */
1836 gen8_configure_all_contexts(dev_priv, false);
1837}
1838
Robert Braggd7965152016-11-07 19:49:52 +00001839static void gen7_update_oacontrol_locked(struct drm_i915_private *dev_priv)
1840{
Chris Wilson67520412017-03-02 13:28:01 +00001841 lockdep_assert_held(&dev_priv->perf.hook_lock);
Robert Braggd7965152016-11-07 19:49:52 +00001842
1843 if (dev_priv->perf.oa.exclusive_stream->enabled) {
1844 struct i915_gem_context *ctx =
1845 dev_priv->perf.oa.exclusive_stream->ctx;
1846 u32 ctx_id = dev_priv->perf.oa.specific_ctx_id;
1847
1848 bool periodic = dev_priv->perf.oa.periodic;
1849 u32 period_exponent = dev_priv->perf.oa.period_exponent;
1850 u32 report_format = dev_priv->perf.oa.oa_buffer.format;
1851
1852 I915_WRITE(GEN7_OACONTROL,
1853 (ctx_id & GEN7_OACONTROL_CTX_MASK) |
1854 (period_exponent <<
1855 GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
1856 (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
1857 (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
1858 (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
1859 GEN7_OACONTROL_ENABLE);
1860 } else
1861 I915_WRITE(GEN7_OACONTROL, 0);
1862}
1863
1864static void gen7_oa_enable(struct drm_i915_private *dev_priv)
1865{
1866 unsigned long flags;
1867
1868 /* Reset buf pointers so we don't forward reports from before now.
1869 *
1870 * Think carefully if considering trying to avoid this, since it
1871 * also ensures status flags and the buffer itself are cleared
1872 * in error paths, and we have checks for invalid reports based
1873 * on the assumption that certain fields are written to zeroed
1874 * memory which this helps maintains.
1875 */
1876 gen7_init_oa_buffer(dev_priv);
1877
1878 spin_lock_irqsave(&dev_priv->perf.hook_lock, flags);
1879 gen7_update_oacontrol_locked(dev_priv);
1880 spin_unlock_irqrestore(&dev_priv->perf.hook_lock, flags);
1881}
1882
Robert Bragg19f81df2017-06-13 12:23:03 +01001883static void gen8_oa_enable(struct drm_i915_private *dev_priv)
1884{
1885 u32 report_format = dev_priv->perf.oa.oa_buffer.format;
1886
1887 /*
1888 * Reset buf pointers so we don't forward reports from before now.
1889 *
1890 * Think carefully if considering trying to avoid this, since it
1891 * also ensures status flags and the buffer itself are cleared
1892 * in error paths, and we have checks for invalid reports based
1893 * on the assumption that certain fields are written to zeroed
1894 * memory which this helps maintains.
1895 */
1896 gen8_init_oa_buffer(dev_priv);
1897
1898 /*
1899 * Note: we don't rely on the hardware to perform single context
1900 * filtering and instead filter on the cpu based on the context-id
1901 * field of reports
1902 */
1903 I915_WRITE(GEN8_OACONTROL, (report_format <<
1904 GEN8_OA_REPORT_FORMAT_SHIFT) |
1905 GEN8_OA_COUNTER_ENABLE);
1906}
1907
Robert Bragg16d98b32016-12-07 21:40:33 +00001908/**
1909 * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
1910 * @stream: An i915 perf stream opened for OA metrics
1911 *
1912 * [Re]enables hardware periodic sampling according to the period configured
1913 * when opening the stream. This also starts a hrtimer that will periodically
1914 * check for data in the circular OA buffer for notifying userspace (e.g.
1915 * during a read() or poll()).
1916 */
Robert Braggd7965152016-11-07 19:49:52 +00001917static void i915_oa_stream_enable(struct i915_perf_stream *stream)
1918{
1919 struct drm_i915_private *dev_priv = stream->dev_priv;
1920
1921 dev_priv->perf.oa.ops.oa_enable(dev_priv);
1922
1923 if (dev_priv->perf.oa.periodic)
1924 hrtimer_start(&dev_priv->perf.oa.poll_check_timer,
1925 ns_to_ktime(POLL_PERIOD),
1926 HRTIMER_MODE_REL_PINNED);
1927}
1928
1929static void gen7_oa_disable(struct drm_i915_private *dev_priv)
1930{
1931 I915_WRITE(GEN7_OACONTROL, 0);
1932}
1933
Robert Bragg19f81df2017-06-13 12:23:03 +01001934static void gen8_oa_disable(struct drm_i915_private *dev_priv)
1935{
1936 I915_WRITE(GEN8_OACONTROL, 0);
1937}
1938
Robert Bragg16d98b32016-12-07 21:40:33 +00001939/**
1940 * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
1941 * @stream: An i915 perf stream opened for OA metrics
1942 *
1943 * Stops the OA unit from periodically writing counter reports into the
1944 * circular OA buffer. This also stops the hrtimer that periodically checks for
1945 * data in the circular OA buffer, for notifying userspace.
1946 */
Robert Braggd7965152016-11-07 19:49:52 +00001947static void i915_oa_stream_disable(struct i915_perf_stream *stream)
1948{
1949 struct drm_i915_private *dev_priv = stream->dev_priv;
1950
1951 dev_priv->perf.oa.ops.oa_disable(dev_priv);
1952
1953 if (dev_priv->perf.oa.periodic)
1954 hrtimer_cancel(&dev_priv->perf.oa.poll_check_timer);
1955}
1956
Robert Braggd7965152016-11-07 19:49:52 +00001957static const struct i915_perf_stream_ops i915_oa_stream_ops = {
1958 .destroy = i915_oa_stream_destroy,
1959 .enable = i915_oa_stream_enable,
1960 .disable = i915_oa_stream_disable,
1961 .wait_unlocked = i915_oa_wait_unlocked,
1962 .poll_wait = i915_oa_poll_wait,
1963 .read = i915_oa_read,
1964};
1965
Robert Bragg16d98b32016-12-07 21:40:33 +00001966/**
1967 * i915_oa_stream_init - validate combined props for OA stream and init
1968 * @stream: An i915 perf stream
1969 * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
1970 * @props: The property state that configures stream (individually validated)
1971 *
1972 * While read_properties_unlocked() validates properties in isolation it
1973 * doesn't ensure that the combination necessarily makes sense.
1974 *
1975 * At this point it has been determined that userspace wants a stream of
1976 * OA metrics, but still we need to further validate the combined
1977 * properties are OK.
1978 *
1979 * If the configuration makes sense then we can allocate memory for
1980 * a circular OA buffer and apply the requested metric set configuration.
1981 *
1982 * Returns: zero on success or a negative error code.
1983 */
Robert Braggd7965152016-11-07 19:49:52 +00001984static int i915_oa_stream_init(struct i915_perf_stream *stream,
1985 struct drm_i915_perf_open_param *param,
1986 struct perf_open_properties *props)
1987{
1988 struct drm_i915_private *dev_priv = stream->dev_priv;
1989 int format_size;
1990 int ret;
1991
Robert Bragg442b8c02016-11-07 19:49:53 +00001992 /* If the sysfs metrics/ directory wasn't registered for some
1993 * reason then don't let userspace try their luck with config
1994 * IDs
1995 */
1996 if (!dev_priv->perf.metrics_kobj) {
Robert Bragg77085502016-12-01 17:21:52 +00001997 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
Robert Bragg442b8c02016-11-07 19:49:53 +00001998 return -EINVAL;
1999 }
2000
Robert Braggd7965152016-11-07 19:49:52 +00002001 if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
Robert Bragg77085502016-12-01 17:21:52 +00002002 DRM_DEBUG("Only OA report sampling supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002003 return -EINVAL;
2004 }
2005
2006 if (!dev_priv->perf.oa.ops.init_oa_buffer) {
Robert Bragg77085502016-12-01 17:21:52 +00002007 DRM_DEBUG("OA unit not supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002008 return -ENODEV;
2009 }
2010
2011 /* To avoid the complexity of having to accurately filter
2012 * counter reports and marshal to the appropriate client
2013 * we currently only allow exclusive access
2014 */
2015 if (dev_priv->perf.oa.exclusive_stream) {
Robert Bragg77085502016-12-01 17:21:52 +00002016 DRM_DEBUG("OA unit already in use\n");
Robert Braggd7965152016-11-07 19:49:52 +00002017 return -EBUSY;
2018 }
2019
2020 if (!props->metrics_set) {
Robert Bragg77085502016-12-01 17:21:52 +00002021 DRM_DEBUG("OA metric set not specified\n");
Robert Braggd7965152016-11-07 19:49:52 +00002022 return -EINVAL;
2023 }
2024
2025 if (!props->oa_format) {
Robert Bragg77085502016-12-01 17:21:52 +00002026 DRM_DEBUG("OA report format not specified\n");
Robert Braggd7965152016-11-07 19:49:52 +00002027 return -EINVAL;
2028 }
2029
Robert Bragg712122e2017-05-11 16:43:31 +01002030 /* We set up some ratelimit state to potentially throttle any _NOTES
2031 * about spurious, invalid OA reports which we don't forward to
2032 * userspace.
2033 *
2034 * The initialization is associated with opening the stream (not driver
2035 * init) considering we print a _NOTE about any throttling when closing
2036 * the stream instead of waiting until driver _fini which no one would
2037 * ever see.
2038 *
2039 * Using the same limiting factors as printk_ratelimit()
2040 */
2041 ratelimit_state_init(&dev_priv->perf.oa.spurious_report_rs,
2042 5 * HZ, 10);
2043 /* Since we use a DRM_NOTE for spurious reports it would be
2044 * inconsistent to let __ratelimit() automatically print a warning for
2045 * throttling.
2046 */
2047 ratelimit_set_flags(&dev_priv->perf.oa.spurious_report_rs,
2048 RATELIMIT_MSG_ON_RELEASE);
2049
Robert Braggd7965152016-11-07 19:49:52 +00002050 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
2051
2052 format_size = dev_priv->perf.oa.oa_formats[props->oa_format].size;
2053
2054 stream->sample_flags |= SAMPLE_OA_REPORT;
2055 stream->sample_size += format_size;
2056
2057 dev_priv->perf.oa.oa_buffer.format_size = format_size;
2058 if (WARN_ON(dev_priv->perf.oa.oa_buffer.format_size == 0))
2059 return -EINVAL;
2060
2061 dev_priv->perf.oa.oa_buffer.format =
2062 dev_priv->perf.oa.oa_formats[props->oa_format].format;
2063
2064 dev_priv->perf.oa.metrics_set = props->metrics_set;
2065
2066 dev_priv->perf.oa.periodic = props->oa_periodic;
Robert Bragg0dd860c2017-05-11 16:43:28 +01002067 if (dev_priv->perf.oa.periodic)
Robert Braggd7965152016-11-07 19:49:52 +00002068 dev_priv->perf.oa.period_exponent = props->oa_period_exponent;
2069
Robert Braggd7965152016-11-07 19:49:52 +00002070 if (stream->ctx) {
2071 ret = oa_get_render_ctx_id(stream);
2072 if (ret)
2073 return ret;
2074 }
2075
2076 ret = alloc_oa_buffer(dev_priv);
2077 if (ret)
2078 goto err_oa_buf_alloc;
2079
2080 /* PRM - observability performance counters:
2081 *
2082 * OACONTROL, performance counter enable, note:
2083 *
2084 * "When this bit is set, in order to have coherent counts,
2085 * RC6 power state and trunk clock gating must be disabled.
2086 * This can be achieved by programming MMIO registers as
2087 * 0xA094=0 and 0xA090[31]=1"
2088 *
2089 * In our case we are expecting that taking pm + FORCEWAKE
2090 * references will effectively disable RC6.
2091 */
2092 intel_runtime_pm_get(dev_priv);
2093 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2094
2095 ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv);
2096 if (ret)
2097 goto err_enable;
2098
2099 stream->ops = &i915_oa_stream_ops;
2100
2101 dev_priv->perf.oa.exclusive_stream = stream;
2102
2103 return 0;
2104
2105err_enable:
2106 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
2107 intel_runtime_pm_put(dev_priv);
2108 free_oa_buffer(dev_priv);
2109
2110err_oa_buf_alloc:
2111 if (stream->ctx)
2112 oa_put_render_ctx_id(stream);
2113
2114 return ret;
2115}
2116
Robert Bragg19f81df2017-06-13 12:23:03 +01002117void i915_oa_init_reg_state(struct intel_engine_cs *engine,
2118 struct i915_gem_context *ctx,
2119 u32 *reg_state)
2120{
2121 struct drm_i915_private *dev_priv = engine->i915;
2122
2123 if (engine->id != RCS)
2124 return;
2125
2126 if (!dev_priv->perf.initialized)
2127 return;
2128
2129 gen8_update_reg_state_unlocked(ctx, reg_state);
2130}
2131
Robert Bragg16d98b32016-12-07 21:40:33 +00002132/**
2133 * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation
2134 * @stream: An i915 perf stream
2135 * @file: An i915 perf stream file
2136 * @buf: destination buffer given by userspace
2137 * @count: the number of bytes userspace wants to read
2138 * @ppos: (inout) file seek position (unused)
2139 *
2140 * Besides wrapping &i915_perf_stream_ops->read this provides a common place to
2141 * ensure that if we've successfully copied any data then reporting that takes
2142 * precedence over any internal error status, so the data isn't lost.
2143 *
2144 * For example ret will be -ENOSPC whenever there is more buffered data than
2145 * can be copied to userspace, but that's only interesting if we weren't able
2146 * to copy some data because it implies the userspace buffer is too small to
2147 * receive a single record (and we never split records).
2148 *
2149 * Another case with ret == -EFAULT is more of a grey area since it would seem
2150 * like bad form for userspace to ask us to overrun its buffer, but the user
2151 * knows best:
2152 *
2153 * http://yarchive.net/comp/linux/partial_reads_writes.html
2154 *
2155 * Returns: The number of bytes copied or a negative error code on failure.
2156 */
Robert Braggeec688e2016-11-07 19:49:47 +00002157static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
2158 struct file *file,
2159 char __user *buf,
2160 size_t count,
2161 loff_t *ppos)
2162{
2163 /* Note we keep the offset (aka bytes read) separate from any
2164 * error status so that the final check for whether we return
2165 * the bytes read with a higher precedence than any error (see
2166 * comment below) doesn't need to be handled/duplicated in
2167 * stream->ops->read() implementations.
2168 */
2169 size_t offset = 0;
2170 int ret = stream->ops->read(stream, buf, count, &offset);
2171
Robert Braggeec688e2016-11-07 19:49:47 +00002172 return offset ?: (ret ?: -EAGAIN);
2173}
2174
Robert Bragg16d98b32016-12-07 21:40:33 +00002175/**
2176 * i915_perf_read - handles read() FOP for i915 perf stream FDs
2177 * @file: An i915 perf stream file
2178 * @buf: destination buffer given by userspace
2179 * @count: the number of bytes userspace wants to read
2180 * @ppos: (inout) file seek position (unused)
2181 *
2182 * The entry point for handling a read() on a stream file descriptor from
2183 * userspace. Most of the work is left to the i915_perf_read_locked() and
2184 * &i915_perf_stream_ops->read but to save having stream implementations (of
2185 * which we might have multiple later) we handle blocking read here.
2186 *
2187 * We can also consistently treat trying to read from a disabled stream
2188 * as an IO error so implementations can assume the stream is enabled
2189 * while reading.
2190 *
2191 * Returns: The number of bytes copied or a negative error code on failure.
2192 */
Robert Braggeec688e2016-11-07 19:49:47 +00002193static ssize_t i915_perf_read(struct file *file,
2194 char __user *buf,
2195 size_t count,
2196 loff_t *ppos)
2197{
2198 struct i915_perf_stream *stream = file->private_data;
2199 struct drm_i915_private *dev_priv = stream->dev_priv;
2200 ssize_t ret;
2201
Robert Braggd7965152016-11-07 19:49:52 +00002202 /* To ensure it's handled consistently we simply treat all reads of a
2203 * disabled stream as an error. In particular it might otherwise lead
2204 * to a deadlock for blocking file descriptors...
2205 */
2206 if (!stream->enabled)
2207 return -EIO;
2208
Robert Braggeec688e2016-11-07 19:49:47 +00002209 if (!(file->f_flags & O_NONBLOCK)) {
Robert Braggd7965152016-11-07 19:49:52 +00002210 /* There's the small chance of false positives from
2211 * stream->ops->wait_unlocked.
2212 *
2213 * E.g. with single context filtering since we only wait until
2214 * oabuffer has >= 1 report we don't immediately know whether
2215 * any reports really belong to the current context
Robert Braggeec688e2016-11-07 19:49:47 +00002216 */
2217 do {
2218 ret = stream->ops->wait_unlocked(stream);
2219 if (ret)
2220 return ret;
2221
2222 mutex_lock(&dev_priv->perf.lock);
2223 ret = i915_perf_read_locked(stream, file,
2224 buf, count, ppos);
2225 mutex_unlock(&dev_priv->perf.lock);
2226 } while (ret == -EAGAIN);
2227 } else {
2228 mutex_lock(&dev_priv->perf.lock);
2229 ret = i915_perf_read_locked(stream, file, buf, count, ppos);
2230 mutex_unlock(&dev_priv->perf.lock);
2231 }
2232
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002233 /* We allow the poll checking to sometimes report false positive POLLIN
2234 * events where we might actually report EAGAIN on read() if there's
2235 * not really any data available. In this situation though we don't
2236 * want to enter a busy loop between poll() reporting a POLLIN event
2237 * and read() returning -EAGAIN. Clearing the oa.pollin state here
2238 * effectively ensures we back off until the next hrtimer callback
2239 * before reporting another POLLIN event.
2240 */
2241 if (ret >= 0 || ret == -EAGAIN) {
Robert Braggd7965152016-11-07 19:49:52 +00002242 /* Maybe make ->pollin per-stream state if we support multiple
2243 * concurrent streams in the future.
2244 */
2245 dev_priv->perf.oa.pollin = false;
2246 }
2247
Robert Braggeec688e2016-11-07 19:49:47 +00002248 return ret;
2249}
2250
Robert Braggd7965152016-11-07 19:49:52 +00002251static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
2252{
2253 struct drm_i915_private *dev_priv =
2254 container_of(hrtimer, typeof(*dev_priv),
2255 perf.oa.poll_check_timer);
2256
Robert Bragg19f81df2017-06-13 12:23:03 +01002257 if (oa_buffer_check_unlocked(dev_priv)) {
Robert Braggd7965152016-11-07 19:49:52 +00002258 dev_priv->perf.oa.pollin = true;
2259 wake_up(&dev_priv->perf.oa.poll_wq);
2260 }
2261
2262 hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
2263
2264 return HRTIMER_RESTART;
2265}
2266
Robert Bragg16d98b32016-12-07 21:40:33 +00002267/**
2268 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
2269 * @dev_priv: i915 device instance
2270 * @stream: An i915 perf stream
2271 * @file: An i915 perf stream file
2272 * @wait: poll() state table
2273 *
2274 * For handling userspace polling on an i915 perf stream, this calls through to
2275 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
2276 * will be woken for new stream data.
2277 *
2278 * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
2279 * with any non-file-operation driver hooks.
2280 *
2281 * Returns: any poll events that are ready without sleeping
2282 */
Robert Braggd7965152016-11-07 19:49:52 +00002283static unsigned int i915_perf_poll_locked(struct drm_i915_private *dev_priv,
2284 struct i915_perf_stream *stream,
Robert Braggeec688e2016-11-07 19:49:47 +00002285 struct file *file,
2286 poll_table *wait)
2287{
Robert Braggd7965152016-11-07 19:49:52 +00002288 unsigned int events = 0;
Robert Braggeec688e2016-11-07 19:49:47 +00002289
2290 stream->ops->poll_wait(stream, file, wait);
2291
Robert Braggd7965152016-11-07 19:49:52 +00002292 /* Note: we don't explicitly check whether there's something to read
2293 * here since this path may be very hot depending on what else
2294 * userspace is polling, or on the timeout in use. We rely solely on
2295 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
2296 * samples to read.
2297 */
2298 if (dev_priv->perf.oa.pollin)
2299 events |= POLLIN;
Robert Braggeec688e2016-11-07 19:49:47 +00002300
Robert Braggd7965152016-11-07 19:49:52 +00002301 return events;
Robert Braggeec688e2016-11-07 19:49:47 +00002302}
2303
Robert Bragg16d98b32016-12-07 21:40:33 +00002304/**
2305 * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
2306 * @file: An i915 perf stream file
2307 * @wait: poll() state table
2308 *
2309 * For handling userspace polling on an i915 perf stream, this ensures
2310 * poll_wait() gets called with a wait queue that will be woken for new stream
2311 * data.
2312 *
2313 * Note: Implementation deferred to i915_perf_poll_locked()
2314 *
2315 * Returns: any poll events that are ready without sleeping
2316 */
Robert Braggeec688e2016-11-07 19:49:47 +00002317static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
2318{
2319 struct i915_perf_stream *stream = file->private_data;
2320 struct drm_i915_private *dev_priv = stream->dev_priv;
2321 int ret;
2322
2323 mutex_lock(&dev_priv->perf.lock);
Robert Braggd7965152016-11-07 19:49:52 +00002324 ret = i915_perf_poll_locked(dev_priv, stream, file, wait);
Robert Braggeec688e2016-11-07 19:49:47 +00002325 mutex_unlock(&dev_priv->perf.lock);
2326
2327 return ret;
2328}
2329
Robert Bragg16d98b32016-12-07 21:40:33 +00002330/**
2331 * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
2332 * @stream: A disabled i915 perf stream
2333 *
2334 * [Re]enables the associated capture of data for this stream.
2335 *
2336 * If a stream was previously enabled then there's currently no intention
2337 * to provide userspace any guarantee about the preservation of previously
2338 * buffered data.
2339 */
Robert Braggeec688e2016-11-07 19:49:47 +00002340static void i915_perf_enable_locked(struct i915_perf_stream *stream)
2341{
2342 if (stream->enabled)
2343 return;
2344
2345 /* Allow stream->ops->enable() to refer to this */
2346 stream->enabled = true;
2347
2348 if (stream->ops->enable)
2349 stream->ops->enable(stream);
2350}
2351
Robert Bragg16d98b32016-12-07 21:40:33 +00002352/**
2353 * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
2354 * @stream: An enabled i915 perf stream
2355 *
2356 * Disables the associated capture of data for this stream.
2357 *
2358 * The intention is that disabling an re-enabling a stream will ideally be
2359 * cheaper than destroying and re-opening a stream with the same configuration,
2360 * though there are no formal guarantees about what state or buffered data
2361 * must be retained between disabling and re-enabling a stream.
2362 *
2363 * Note: while a stream is disabled it's considered an error for userspace
2364 * to attempt to read from the stream (-EIO).
2365 */
Robert Braggeec688e2016-11-07 19:49:47 +00002366static void i915_perf_disable_locked(struct i915_perf_stream *stream)
2367{
2368 if (!stream->enabled)
2369 return;
2370
2371 /* Allow stream->ops->disable() to refer to this */
2372 stream->enabled = false;
2373
2374 if (stream->ops->disable)
2375 stream->ops->disable(stream);
2376}
2377
Robert Bragg16d98b32016-12-07 21:40:33 +00002378/**
2379 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2380 * @stream: An i915 perf stream
2381 * @cmd: the ioctl request
2382 * @arg: the ioctl data
2383 *
2384 * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
2385 * with any non-file-operation driver hooks.
2386 *
2387 * Returns: zero on success or a negative error code. Returns -EINVAL for
2388 * an unknown ioctl request.
2389 */
Robert Braggeec688e2016-11-07 19:49:47 +00002390static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
2391 unsigned int cmd,
2392 unsigned long arg)
2393{
2394 switch (cmd) {
2395 case I915_PERF_IOCTL_ENABLE:
2396 i915_perf_enable_locked(stream);
2397 return 0;
2398 case I915_PERF_IOCTL_DISABLE:
2399 i915_perf_disable_locked(stream);
2400 return 0;
2401 }
2402
2403 return -EINVAL;
2404}
2405
Robert Bragg16d98b32016-12-07 21:40:33 +00002406/**
2407 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2408 * @file: An i915 perf stream file
2409 * @cmd: the ioctl request
2410 * @arg: the ioctl data
2411 *
2412 * Implementation deferred to i915_perf_ioctl_locked().
2413 *
2414 * Returns: zero on success or a negative error code. Returns -EINVAL for
2415 * an unknown ioctl request.
2416 */
Robert Braggeec688e2016-11-07 19:49:47 +00002417static long i915_perf_ioctl(struct file *file,
2418 unsigned int cmd,
2419 unsigned long arg)
2420{
2421 struct i915_perf_stream *stream = file->private_data;
2422 struct drm_i915_private *dev_priv = stream->dev_priv;
2423 long ret;
2424
2425 mutex_lock(&dev_priv->perf.lock);
2426 ret = i915_perf_ioctl_locked(stream, cmd, arg);
2427 mutex_unlock(&dev_priv->perf.lock);
2428
2429 return ret;
2430}
2431
Robert Bragg16d98b32016-12-07 21:40:33 +00002432/**
2433 * i915_perf_destroy_locked - destroy an i915 perf stream
2434 * @stream: An i915 perf stream
2435 *
2436 * Frees all resources associated with the given i915 perf @stream, disabling
2437 * any associated data capture in the process.
2438 *
2439 * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
2440 * with any non-file-operation driver hooks.
2441 */
Robert Braggeec688e2016-11-07 19:49:47 +00002442static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
2443{
Robert Braggeec688e2016-11-07 19:49:47 +00002444 if (stream->enabled)
2445 i915_perf_disable_locked(stream);
2446
2447 if (stream->ops->destroy)
2448 stream->ops->destroy(stream);
2449
2450 list_del(&stream->link);
2451
Chris Wilson69df05e2016-12-18 15:37:21 +00002452 if (stream->ctx)
2453 i915_gem_context_put_unlocked(stream->ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002454
2455 kfree(stream);
2456}
2457
Robert Bragg16d98b32016-12-07 21:40:33 +00002458/**
2459 * i915_perf_release - handles userspace close() of a stream file
2460 * @inode: anonymous inode associated with file
2461 * @file: An i915 perf stream file
2462 *
2463 * Cleans up any resources associated with an open i915 perf stream file.
2464 *
2465 * NB: close() can't really fail from the userspace point of view.
2466 *
2467 * Returns: zero on success or a negative error code.
2468 */
Robert Braggeec688e2016-11-07 19:49:47 +00002469static int i915_perf_release(struct inode *inode, struct file *file)
2470{
2471 struct i915_perf_stream *stream = file->private_data;
2472 struct drm_i915_private *dev_priv = stream->dev_priv;
2473
2474 mutex_lock(&dev_priv->perf.lock);
2475 i915_perf_destroy_locked(stream);
2476 mutex_unlock(&dev_priv->perf.lock);
2477
2478 return 0;
2479}
2480
2481
2482static const struct file_operations fops = {
2483 .owner = THIS_MODULE,
2484 .llseek = no_llseek,
2485 .release = i915_perf_release,
2486 .poll = i915_perf_poll,
2487 .read = i915_perf_read,
2488 .unlocked_ioctl = i915_perf_ioctl,
2489};
2490
2491
2492static struct i915_gem_context *
2493lookup_context(struct drm_i915_private *dev_priv,
2494 struct drm_i915_file_private *file_priv,
2495 u32 ctx_user_handle)
2496{
2497 struct i915_gem_context *ctx;
2498 int ret;
2499
2500 ret = i915_mutex_lock_interruptible(&dev_priv->drm);
2501 if (ret)
2502 return ERR_PTR(ret);
2503
2504 ctx = i915_gem_context_lookup(file_priv, ctx_user_handle);
2505 if (!IS_ERR(ctx))
2506 i915_gem_context_get(ctx);
2507
2508 mutex_unlock(&dev_priv->drm.struct_mutex);
2509
2510 return ctx;
2511}
2512
Robert Bragg16d98b32016-12-07 21:40:33 +00002513/**
2514 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
2515 * @dev_priv: i915 device instance
2516 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
2517 * @props: individually validated u64 property value pairs
2518 * @file: drm file
2519 *
2520 * See i915_perf_ioctl_open() for interface details.
2521 *
2522 * Implements further stream config validation and stream initialization on
2523 * behalf of i915_perf_open_ioctl() with the &drm_i915_private->perf.lock mutex
2524 * taken to serialize with any non-file-operation driver hooks.
2525 *
2526 * Note: at this point the @props have only been validated in isolation and
2527 * it's still necessary to validate that the combination of properties makes
2528 * sense.
2529 *
2530 * In the case where userspace is interested in OA unit metrics then further
2531 * config validation and stream initialization details will be handled by
2532 * i915_oa_stream_init(). The code here should only validate config state that
2533 * will be relevant to all stream types / backends.
2534 *
2535 * Returns: zero on success or a negative error code.
2536 */
Robert Braggeec688e2016-11-07 19:49:47 +00002537static int
2538i915_perf_open_ioctl_locked(struct drm_i915_private *dev_priv,
2539 struct drm_i915_perf_open_param *param,
2540 struct perf_open_properties *props,
2541 struct drm_file *file)
2542{
2543 struct i915_gem_context *specific_ctx = NULL;
2544 struct i915_perf_stream *stream = NULL;
2545 unsigned long f_flags = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01002546 bool privileged_op = true;
Robert Braggeec688e2016-11-07 19:49:47 +00002547 int stream_fd;
2548 int ret;
2549
2550 if (props->single_context) {
2551 u32 ctx_handle = props->ctx_handle;
2552 struct drm_i915_file_private *file_priv = file->driver_priv;
2553
2554 specific_ctx = lookup_context(dev_priv, file_priv, ctx_handle);
2555 if (IS_ERR(specific_ctx)) {
2556 ret = PTR_ERR(specific_ctx);
2557 if (ret != -EINTR)
Robert Bragg77085502016-12-01 17:21:52 +00002558 DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n",
Robert Braggeec688e2016-11-07 19:49:47 +00002559 ctx_handle);
2560 goto err;
2561 }
2562 }
2563
Robert Bragg19f81df2017-06-13 12:23:03 +01002564 /*
2565 * On Haswell the OA unit supports clock gating off for a specific
2566 * context and in this mode there's no visibility of metrics for the
2567 * rest of the system, which we consider acceptable for a
2568 * non-privileged client.
2569 *
2570 * For Gen8+ the OA unit no longer supports clock gating off for a
2571 * specific context and the kernel can't securely stop the counters
2572 * from updating as system-wide / global values. Even though we can
2573 * filter reports based on the included context ID we can't block
2574 * clients from seeing the raw / global counter values via
2575 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
2576 * enable the OA unit by default.
2577 */
2578 if (IS_HASWELL(dev_priv) && specific_ctx)
2579 privileged_op = false;
2580
Robert Braggccdf6342016-11-07 19:49:54 +00002581 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
2582 * we check a dev.i915.perf_stream_paranoid sysctl option
2583 * to determine if it's ok to access system wide OA counters
2584 * without CAP_SYS_ADMIN privileges.
2585 */
Robert Bragg19f81df2017-06-13 12:23:03 +01002586 if (privileged_op &&
Robert Braggccdf6342016-11-07 19:49:54 +00002587 i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002588 DRM_DEBUG("Insufficient privileges to open system-wide i915 perf stream\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002589 ret = -EACCES;
2590 goto err_ctx;
2591 }
2592
2593 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2594 if (!stream) {
2595 ret = -ENOMEM;
2596 goto err_ctx;
2597 }
2598
Robert Braggeec688e2016-11-07 19:49:47 +00002599 stream->dev_priv = dev_priv;
2600 stream->ctx = specific_ctx;
2601
Robert Braggd7965152016-11-07 19:49:52 +00002602 ret = i915_oa_stream_init(stream, param, props);
2603 if (ret)
2604 goto err_alloc;
2605
2606 /* we avoid simply assigning stream->sample_flags = props->sample_flags
2607 * to have _stream_init check the combination of sample flags more
2608 * thoroughly, but still this is the expected result at this point.
Robert Braggeec688e2016-11-07 19:49:47 +00002609 */
Robert Braggd7965152016-11-07 19:49:52 +00002610 if (WARN_ON(stream->sample_flags != props->sample_flags)) {
2611 ret = -ENODEV;
Matthew Auld22f880c2017-03-27 21:34:59 +01002612 goto err_flags;
Robert Braggd7965152016-11-07 19:49:52 +00002613 }
Robert Braggeec688e2016-11-07 19:49:47 +00002614
2615 list_add(&stream->link, &dev_priv->perf.streams);
2616
2617 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
2618 f_flags |= O_CLOEXEC;
2619 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
2620 f_flags |= O_NONBLOCK;
2621
2622 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
2623 if (stream_fd < 0) {
2624 ret = stream_fd;
2625 goto err_open;
2626 }
2627
2628 if (!(param->flags & I915_PERF_FLAG_DISABLED))
2629 i915_perf_enable_locked(stream);
2630
2631 return stream_fd;
2632
2633err_open:
2634 list_del(&stream->link);
Matthew Auld22f880c2017-03-27 21:34:59 +01002635err_flags:
Robert Braggeec688e2016-11-07 19:49:47 +00002636 if (stream->ops->destroy)
2637 stream->ops->destroy(stream);
2638err_alloc:
2639 kfree(stream);
2640err_ctx:
Chris Wilson69df05e2016-12-18 15:37:21 +00002641 if (specific_ctx)
2642 i915_gem_context_put_unlocked(specific_ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002643err:
2644 return ret;
2645}
2646
Robert Bragg155e9412017-06-13 12:23:05 +01002647static u64 oa_exponent_to_ns(struct drm_i915_private *dev_priv, int exponent)
2648{
2649 return div_u64(1000000000ULL * (2ULL << exponent),
2650 dev_priv->perf.oa.timestamp_frequency);
2651}
2652
Robert Bragg16d98b32016-12-07 21:40:33 +00002653/**
2654 * read_properties_unlocked - validate + copy userspace stream open properties
2655 * @dev_priv: i915 device instance
2656 * @uprops: The array of u64 key value pairs given by userspace
2657 * @n_props: The number of key value pairs expected in @uprops
2658 * @props: The stream configuration built up while validating properties
Robert Braggeec688e2016-11-07 19:49:47 +00002659 *
2660 * Note this function only validates properties in isolation it doesn't
2661 * validate that the combination of properties makes sense or that all
2662 * properties necessary for a particular kind of stream have been set.
Robert Bragg16d98b32016-12-07 21:40:33 +00002663 *
2664 * Note that there currently aren't any ordering requirements for properties so
2665 * we shouldn't validate or assume anything about ordering here. This doesn't
2666 * rule out defining new properties with ordering requirements in the future.
Robert Braggeec688e2016-11-07 19:49:47 +00002667 */
2668static int read_properties_unlocked(struct drm_i915_private *dev_priv,
2669 u64 __user *uprops,
2670 u32 n_props,
2671 struct perf_open_properties *props)
2672{
2673 u64 __user *uprop = uprops;
2674 int i;
2675
2676 memset(props, 0, sizeof(struct perf_open_properties));
2677
2678 if (!n_props) {
Robert Bragg77085502016-12-01 17:21:52 +00002679 DRM_DEBUG("No i915 perf properties given\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002680 return -EINVAL;
2681 }
2682
2683 /* Considering that ID = 0 is reserved and assuming that we don't
2684 * (currently) expect any configurations to ever specify duplicate
2685 * values for a particular property ID then the last _PROP_MAX value is
2686 * one greater than the maximum number of properties we expect to get
2687 * from userspace.
2688 */
2689 if (n_props >= DRM_I915_PERF_PROP_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002690 DRM_DEBUG("More i915 perf properties specified than exist\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002691 return -EINVAL;
2692 }
2693
2694 for (i = 0; i < n_props; i++) {
Robert Bragg00319ba2016-11-07 19:49:55 +00002695 u64 oa_period, oa_freq_hz;
Robert Braggeec688e2016-11-07 19:49:47 +00002696 u64 id, value;
2697 int ret;
2698
2699 ret = get_user(id, uprop);
2700 if (ret)
2701 return ret;
2702
2703 ret = get_user(value, uprop + 1);
2704 if (ret)
2705 return ret;
2706
Matthew Auld0a309f92017-03-27 21:32:36 +01002707 if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
2708 DRM_DEBUG("Unknown i915 perf property ID\n");
2709 return -EINVAL;
2710 }
2711
Robert Braggeec688e2016-11-07 19:49:47 +00002712 switch ((enum drm_i915_perf_property_id)id) {
2713 case DRM_I915_PERF_PROP_CTX_HANDLE:
2714 props->single_context = 1;
2715 props->ctx_handle = value;
2716 break;
Robert Braggd7965152016-11-07 19:49:52 +00002717 case DRM_I915_PERF_PROP_SAMPLE_OA:
2718 props->sample_flags |= SAMPLE_OA_REPORT;
2719 break;
2720 case DRM_I915_PERF_PROP_OA_METRICS_SET:
2721 if (value == 0 ||
2722 value > dev_priv->perf.oa.n_builtin_sets) {
Robert Bragg77085502016-12-01 17:21:52 +00002723 DRM_DEBUG("Unknown OA metric set ID\n");
Robert Braggd7965152016-11-07 19:49:52 +00002724 return -EINVAL;
2725 }
2726 props->metrics_set = value;
2727 break;
2728 case DRM_I915_PERF_PROP_OA_FORMAT:
2729 if (value == 0 || value >= I915_OA_FORMAT_MAX) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002730 DRM_DEBUG("Out-of-range OA report format %llu\n",
2731 value);
Robert Braggd7965152016-11-07 19:49:52 +00002732 return -EINVAL;
2733 }
2734 if (!dev_priv->perf.oa.oa_formats[value].size) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002735 DRM_DEBUG("Unsupported OA report format %llu\n",
2736 value);
Robert Braggd7965152016-11-07 19:49:52 +00002737 return -EINVAL;
2738 }
2739 props->oa_format = value;
2740 break;
2741 case DRM_I915_PERF_PROP_OA_EXPONENT:
2742 if (value > OA_EXPONENT_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002743 DRM_DEBUG("OA timer exponent too high (> %u)\n",
2744 OA_EXPONENT_MAX);
Robert Braggd7965152016-11-07 19:49:52 +00002745 return -EINVAL;
2746 }
2747
Robert Bragg00319ba2016-11-07 19:49:55 +00002748 /* Theoretically we can program the OA unit to sample
Robert Bragg155e9412017-06-13 12:23:05 +01002749 * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
2750 * for BXT. We don't allow such high sampling
2751 * frequencies by default unless root.
Robert Braggd7965152016-11-07 19:49:52 +00002752 */
Robert Bragg155e9412017-06-13 12:23:05 +01002753
Robert Bragg00319ba2016-11-07 19:49:55 +00002754 BUILD_BUG_ON(sizeof(oa_period) != 8);
Robert Bragg155e9412017-06-13 12:23:05 +01002755 oa_period = oa_exponent_to_ns(dev_priv, value);
Robert Bragg00319ba2016-11-07 19:49:55 +00002756
2757 /* This check is primarily to ensure that oa_period <=
2758 * UINT32_MAX (before passing to do_div which only
2759 * accepts a u32 denominator), but we can also skip
2760 * checking anything < 1Hz which implicitly can't be
2761 * limited via an integer oa_max_sample_rate.
2762 */
2763 if (oa_period <= NSEC_PER_SEC) {
2764 u64 tmp = NSEC_PER_SEC;
2765 do_div(tmp, oa_period);
2766 oa_freq_hz = tmp;
2767 } else
2768 oa_freq_hz = 0;
2769
2770 if (oa_freq_hz > i915_oa_max_sample_rate &&
2771 !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002772 DRM_DEBUG("OA exponent would exceed the max sampling frequency (sysctl dev.i915.oa_max_sample_rate) %uHz without root privileges\n",
Robert Bragg00319ba2016-11-07 19:49:55 +00002773 i915_oa_max_sample_rate);
Robert Braggd7965152016-11-07 19:49:52 +00002774 return -EACCES;
2775 }
2776
2777 props->oa_periodic = true;
2778 props->oa_period_exponent = value;
2779 break;
Matthew Auld0a309f92017-03-27 21:32:36 +01002780 case DRM_I915_PERF_PROP_MAX:
Robert Braggeec688e2016-11-07 19:49:47 +00002781 MISSING_CASE(id);
Robert Braggeec688e2016-11-07 19:49:47 +00002782 return -EINVAL;
2783 }
2784
2785 uprop += 2;
2786 }
2787
2788 return 0;
2789}
2790
Robert Bragg16d98b32016-12-07 21:40:33 +00002791/**
2792 * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
2793 * @dev: drm device
2794 * @data: ioctl data copied from userspace (unvalidated)
2795 * @file: drm file
2796 *
2797 * Validates the stream open parameters given by userspace including flags
2798 * and an array of u64 key, value pair properties.
2799 *
2800 * Very little is assumed up front about the nature of the stream being
2801 * opened (for instance we don't assume it's for periodic OA unit metrics). An
2802 * i915-perf stream is expected to be a suitable interface for other forms of
2803 * buffered data written by the GPU besides periodic OA metrics.
2804 *
2805 * Note we copy the properties from userspace outside of the i915 perf
2806 * mutex to avoid an awkward lockdep with mmap_sem.
2807 *
2808 * Most of the implementation details are handled by
2809 * i915_perf_open_ioctl_locked() after taking the &drm_i915_private->perf.lock
2810 * mutex for serializing with any non-file-operation driver hooks.
2811 *
2812 * Return: A newly opened i915 Perf stream file descriptor or negative
2813 * error code on failure.
2814 */
Robert Braggeec688e2016-11-07 19:49:47 +00002815int i915_perf_open_ioctl(struct drm_device *dev, void *data,
2816 struct drm_file *file)
2817{
2818 struct drm_i915_private *dev_priv = dev->dev_private;
2819 struct drm_i915_perf_open_param *param = data;
2820 struct perf_open_properties props;
2821 u32 known_open_flags;
2822 int ret;
2823
2824 if (!dev_priv->perf.initialized) {
Robert Bragg77085502016-12-01 17:21:52 +00002825 DRM_DEBUG("i915 perf interface not available for this system\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002826 return -ENOTSUPP;
2827 }
2828
2829 known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
2830 I915_PERF_FLAG_FD_NONBLOCK |
2831 I915_PERF_FLAG_DISABLED;
2832 if (param->flags & ~known_open_flags) {
Robert Bragg77085502016-12-01 17:21:52 +00002833 DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002834 return -EINVAL;
2835 }
2836
2837 ret = read_properties_unlocked(dev_priv,
2838 u64_to_user_ptr(param->properties_ptr),
2839 param->num_properties,
2840 &props);
2841 if (ret)
2842 return ret;
2843
2844 mutex_lock(&dev_priv->perf.lock);
2845 ret = i915_perf_open_ioctl_locked(dev_priv, param, &props, file);
2846 mutex_unlock(&dev_priv->perf.lock);
2847
2848 return ret;
2849}
2850
Robert Bragg16d98b32016-12-07 21:40:33 +00002851/**
2852 * i915_perf_register - exposes i915-perf to userspace
2853 * @dev_priv: i915 device instance
2854 *
2855 * In particular OA metric sets are advertised under a sysfs metrics/
2856 * directory allowing userspace to enumerate valid IDs that can be
2857 * used to open an i915-perf stream.
2858 */
Robert Bragg442b8c02016-11-07 19:49:53 +00002859void i915_perf_register(struct drm_i915_private *dev_priv)
2860{
Robert Bragg442b8c02016-11-07 19:49:53 +00002861 if (!dev_priv->perf.initialized)
2862 return;
2863
2864 /* To be sure we're synchronized with an attempted
2865 * i915_perf_open_ioctl(); considering that we register after
2866 * being exposed to userspace.
2867 */
2868 mutex_lock(&dev_priv->perf.lock);
2869
2870 dev_priv->perf.metrics_kobj =
2871 kobject_create_and_add("metrics",
2872 &dev_priv->drm.primary->kdev->kobj);
2873 if (!dev_priv->perf.metrics_kobj)
2874 goto exit;
2875
Robert Bragg19f81df2017-06-13 12:23:03 +01002876 if (IS_HASWELL(dev_priv)) {
2877 if (i915_perf_register_sysfs_hsw(dev_priv))
2878 goto sysfs_error;
2879 } else if (IS_BROADWELL(dev_priv)) {
2880 if (i915_perf_register_sysfs_bdw(dev_priv))
2881 goto sysfs_error;
2882 } else if (IS_CHERRYVIEW(dev_priv)) {
2883 if (i915_perf_register_sysfs_chv(dev_priv))
2884 goto sysfs_error;
2885 } else if (IS_SKYLAKE(dev_priv)) {
2886 if (IS_SKL_GT2(dev_priv)) {
2887 if (i915_perf_register_sysfs_sklgt2(dev_priv))
2888 goto sysfs_error;
2889 } else if (IS_SKL_GT3(dev_priv)) {
2890 if (i915_perf_register_sysfs_sklgt3(dev_priv))
2891 goto sysfs_error;
2892 } else if (IS_SKL_GT4(dev_priv)) {
2893 if (i915_perf_register_sysfs_sklgt4(dev_priv))
2894 goto sysfs_error;
2895 } else
2896 goto sysfs_error;
2897 } else if (IS_BROXTON(dev_priv)) {
2898 if (i915_perf_register_sysfs_bxt(dev_priv))
2899 goto sysfs_error;
Robert Bragg442b8c02016-11-07 19:49:53 +00002900 }
2901
Robert Bragg19f81df2017-06-13 12:23:03 +01002902 goto exit;
2903
2904sysfs_error:
2905 kobject_put(dev_priv->perf.metrics_kobj);
2906 dev_priv->perf.metrics_kobj = NULL;
2907
Robert Bragg442b8c02016-11-07 19:49:53 +00002908exit:
2909 mutex_unlock(&dev_priv->perf.lock);
2910}
2911
Robert Bragg16d98b32016-12-07 21:40:33 +00002912/**
2913 * i915_perf_unregister - hide i915-perf from userspace
2914 * @dev_priv: i915 device instance
2915 *
2916 * i915-perf state cleanup is split up into an 'unregister' and
2917 * 'deinit' phase where the interface is first hidden from
2918 * userspace by i915_perf_unregister() before cleaning up
2919 * remaining state in i915_perf_fini().
2920 */
Robert Bragg442b8c02016-11-07 19:49:53 +00002921void i915_perf_unregister(struct drm_i915_private *dev_priv)
2922{
Robert Bragg442b8c02016-11-07 19:49:53 +00002923 if (!dev_priv->perf.metrics_kobj)
2924 return;
2925
Robert Bragg19f81df2017-06-13 12:23:03 +01002926 if (IS_HASWELL(dev_priv))
2927 i915_perf_unregister_sysfs_hsw(dev_priv);
2928 else if (IS_BROADWELL(dev_priv))
2929 i915_perf_unregister_sysfs_bdw(dev_priv);
2930 else if (IS_CHERRYVIEW(dev_priv))
2931 i915_perf_unregister_sysfs_chv(dev_priv);
2932 else if (IS_SKYLAKE(dev_priv)) {
2933 if (IS_SKL_GT2(dev_priv))
2934 i915_perf_unregister_sysfs_sklgt2(dev_priv);
2935 else if (IS_SKL_GT3(dev_priv))
2936 i915_perf_unregister_sysfs_sklgt3(dev_priv);
2937 else if (IS_SKL_GT4(dev_priv))
2938 i915_perf_unregister_sysfs_sklgt4(dev_priv);
2939 } else if (IS_BROXTON(dev_priv))
2940 i915_perf_unregister_sysfs_bxt(dev_priv);
Robert Bragg442b8c02016-11-07 19:49:53 +00002941
2942 kobject_put(dev_priv->perf.metrics_kobj);
2943 dev_priv->perf.metrics_kobj = NULL;
2944}
2945
Robert Braggccdf6342016-11-07 19:49:54 +00002946static struct ctl_table oa_table[] = {
2947 {
2948 .procname = "perf_stream_paranoid",
2949 .data = &i915_perf_stream_paranoid,
2950 .maxlen = sizeof(i915_perf_stream_paranoid),
2951 .mode = 0644,
2952 .proc_handler = proc_dointvec_minmax,
2953 .extra1 = &zero,
2954 .extra2 = &one,
2955 },
Robert Bragg00319ba2016-11-07 19:49:55 +00002956 {
2957 .procname = "oa_max_sample_rate",
2958 .data = &i915_oa_max_sample_rate,
2959 .maxlen = sizeof(i915_oa_max_sample_rate),
2960 .mode = 0644,
2961 .proc_handler = proc_dointvec_minmax,
2962 .extra1 = &zero,
2963 .extra2 = &oa_sample_rate_hard_limit,
2964 },
Robert Braggccdf6342016-11-07 19:49:54 +00002965 {}
2966};
2967
2968static struct ctl_table i915_root[] = {
2969 {
2970 .procname = "i915",
2971 .maxlen = 0,
2972 .mode = 0555,
2973 .child = oa_table,
2974 },
2975 {}
2976};
2977
2978static struct ctl_table dev_root[] = {
2979 {
2980 .procname = "dev",
2981 .maxlen = 0,
2982 .mode = 0555,
2983 .child = i915_root,
2984 },
2985 {}
2986};
2987
Robert Bragg16d98b32016-12-07 21:40:33 +00002988/**
2989 * i915_perf_init - initialize i915-perf state on module load
2990 * @dev_priv: i915 device instance
2991 *
2992 * Initializes i915-perf state without exposing anything to userspace.
2993 *
2994 * Note: i915-perf initialization is split into an 'init' and 'register'
2995 * phase with the i915_perf_register() exposing state to userspace.
2996 */
Robert Braggeec688e2016-11-07 19:49:47 +00002997void i915_perf_init(struct drm_i915_private *dev_priv)
2998{
Robert Bragg19f81df2017-06-13 12:23:03 +01002999 dev_priv->perf.oa.n_builtin_sets = 0;
Robert Braggd7965152016-11-07 19:49:52 +00003000
Robert Bragg19f81df2017-06-13 12:23:03 +01003001 if (IS_HASWELL(dev_priv)) {
3002 dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer;
3003 dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set;
3004 dev_priv->perf.oa.ops.disable_metric_set = hsw_disable_metric_set;
3005 dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable;
3006 dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable;
3007 dev_priv->perf.oa.ops.read = gen7_oa_read;
3008 dev_priv->perf.oa.ops.oa_hw_tail_read =
3009 gen7_oa_hw_tail_read;
Robert Braggd7965152016-11-07 19:49:52 +00003010
Robert Bragg155e9412017-06-13 12:23:05 +01003011 dev_priv->perf.oa.timestamp_frequency = 12500000;
3012
Robert Bragg19f81df2017-06-13 12:23:03 +01003013 dev_priv->perf.oa.oa_formats = hsw_oa_formats;
Robert Braggd7965152016-11-07 19:49:52 +00003014
Robert Bragg19f81df2017-06-13 12:23:03 +01003015 dev_priv->perf.oa.n_builtin_sets =
3016 i915_oa_n_builtin_metric_sets_hsw;
3017 } else if (i915.enable_execlists) {
3018 /* Note: that although we could theoretically also support the
3019 * legacy ringbuffer mode on BDW (and earlier iterations of
3020 * this driver, before upstreaming did this) it didn't seem
3021 * worth the complexity to maintain now that BDW+ enable
3022 * execlist mode by default.
3023 */
Robert Braggd7965152016-11-07 19:49:52 +00003024
Robert Bragg19f81df2017-06-13 12:23:03 +01003025 if (IS_GEN8(dev_priv)) {
3026 dev_priv->perf.oa.ctx_oactxctrl_offset = 0x120;
3027 dev_priv->perf.oa.ctx_flexeu0_offset = 0x2ce;
Robert Bragg155e9412017-06-13 12:23:05 +01003028
3029 dev_priv->perf.oa.timestamp_frequency = 12500000;
3030
Robert Bragg19f81df2017-06-13 12:23:03 +01003031 dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<25);
Robert Braggd7965152016-11-07 19:49:52 +00003032
Robert Bragg19f81df2017-06-13 12:23:03 +01003033 if (IS_BROADWELL(dev_priv)) {
3034 dev_priv->perf.oa.n_builtin_sets =
3035 i915_oa_n_builtin_metric_sets_bdw;
3036 dev_priv->perf.oa.ops.select_metric_set =
3037 i915_oa_select_metric_set_bdw;
3038 } else if (IS_CHERRYVIEW(dev_priv)) {
3039 dev_priv->perf.oa.n_builtin_sets =
3040 i915_oa_n_builtin_metric_sets_chv;
3041 dev_priv->perf.oa.ops.select_metric_set =
3042 i915_oa_select_metric_set_chv;
3043 }
3044 } else if (IS_GEN9(dev_priv)) {
3045 dev_priv->perf.oa.ctx_oactxctrl_offset = 0x128;
3046 dev_priv->perf.oa.ctx_flexeu0_offset = 0x3de;
Robert Bragg155e9412017-06-13 12:23:05 +01003047
3048 dev_priv->perf.oa.timestamp_frequency = 12000000;
3049
Robert Bragg19f81df2017-06-13 12:23:03 +01003050 dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<16);
Robert Braggeec688e2016-11-07 19:49:47 +00003051
Robert Bragg19f81df2017-06-13 12:23:03 +01003052 if (IS_SKL_GT2(dev_priv)) {
3053 dev_priv->perf.oa.n_builtin_sets =
3054 i915_oa_n_builtin_metric_sets_sklgt2;
3055 dev_priv->perf.oa.ops.select_metric_set =
3056 i915_oa_select_metric_set_sklgt2;
3057 } else if (IS_SKL_GT3(dev_priv)) {
3058 dev_priv->perf.oa.n_builtin_sets =
3059 i915_oa_n_builtin_metric_sets_sklgt3;
3060 dev_priv->perf.oa.ops.select_metric_set =
3061 i915_oa_select_metric_set_sklgt3;
3062 } else if (IS_SKL_GT4(dev_priv)) {
3063 dev_priv->perf.oa.n_builtin_sets =
3064 i915_oa_n_builtin_metric_sets_sklgt4;
3065 dev_priv->perf.oa.ops.select_metric_set =
3066 i915_oa_select_metric_set_sklgt4;
3067 } else if (IS_BROXTON(dev_priv)) {
Robert Bragg155e9412017-06-13 12:23:05 +01003068 dev_priv->perf.oa.timestamp_frequency = 19200000;
3069
Robert Bragg19f81df2017-06-13 12:23:03 +01003070 dev_priv->perf.oa.n_builtin_sets =
3071 i915_oa_n_builtin_metric_sets_bxt;
3072 dev_priv->perf.oa.ops.select_metric_set =
3073 i915_oa_select_metric_set_bxt;
3074 }
3075 }
Robert Braggccdf6342016-11-07 19:49:54 +00003076
Robert Bragg19f81df2017-06-13 12:23:03 +01003077 if (dev_priv->perf.oa.n_builtin_sets) {
3078 dev_priv->perf.oa.ops.init_oa_buffer = gen8_init_oa_buffer;
3079 dev_priv->perf.oa.ops.enable_metric_set =
3080 gen8_enable_metric_set;
3081 dev_priv->perf.oa.ops.disable_metric_set =
3082 gen8_disable_metric_set;
3083 dev_priv->perf.oa.ops.oa_enable = gen8_oa_enable;
3084 dev_priv->perf.oa.ops.oa_disable = gen8_oa_disable;
3085 dev_priv->perf.oa.ops.read = gen8_oa_read;
3086 dev_priv->perf.oa.ops.oa_hw_tail_read =
3087 gen8_oa_hw_tail_read;
3088
3089 dev_priv->perf.oa.oa_formats = gen8_plus_oa_formats;
3090 }
3091 }
3092
3093 if (dev_priv->perf.oa.n_builtin_sets) {
3094 hrtimer_init(&dev_priv->perf.oa.poll_check_timer,
3095 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3096 dev_priv->perf.oa.poll_check_timer.function = oa_poll_check_timer_cb;
3097 init_waitqueue_head(&dev_priv->perf.oa.poll_wq);
3098
3099 INIT_LIST_HEAD(&dev_priv->perf.streams);
3100 mutex_init(&dev_priv->perf.lock);
3101 spin_lock_init(&dev_priv->perf.hook_lock);
3102 spin_lock_init(&dev_priv->perf.oa.oa_buffer.ptr_lock);
3103
Robert Bragg155e9412017-06-13 12:23:05 +01003104 oa_sample_rate_hard_limit =
3105 dev_priv->perf.oa.timestamp_frequency / 2;
Robert Bragg19f81df2017-06-13 12:23:03 +01003106 dev_priv->perf.sysctl_header = register_sysctl_table(dev_root);
3107
3108 dev_priv->perf.initialized = true;
3109 }
Robert Braggeec688e2016-11-07 19:49:47 +00003110}
3111
Robert Bragg16d98b32016-12-07 21:40:33 +00003112/**
3113 * i915_perf_fini - Counter part to i915_perf_init()
3114 * @dev_priv: i915 device instance
3115 */
Robert Braggeec688e2016-11-07 19:49:47 +00003116void i915_perf_fini(struct drm_i915_private *dev_priv)
3117{
3118 if (!dev_priv->perf.initialized)
3119 return;
3120
Robert Braggccdf6342016-11-07 19:49:54 +00003121 unregister_sysctl_table(dev_priv->perf.sysctl_header);
3122
Robert Braggd7965152016-11-07 19:49:52 +00003123 memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops));
Robert Bragg19f81df2017-06-13 12:23:03 +01003124
Robert Braggeec688e2016-11-07 19:49:47 +00003125 dev_priv->perf.initialized = false;
3126}