blob: 06a5e1e83e6c6d319b51ebe89a9e53bba115415d [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"
Lionel Landwerlin6c5c1d82017-06-13 12:23:08 +0100205#include "i915_oa_kblgt2.h"
206#include "i915_oa_kblgt3.h"
Lionel Landwerlin28c7ef92017-06-13 12:23:09 +0100207#include "i915_oa_glk.h"
Robert Braggd7965152016-11-07 19:49:52 +0000208
209/* HW requires this to be a power of two, between 128k and 16M, though driver
210 * is currently generally designed assuming the largest 16M size is used such
211 * that the overflow cases are unlikely in normal operation.
212 */
213#define OA_BUFFER_SIZE SZ_16M
214
215#define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
216
Robert Bragg0dd860c2017-05-11 16:43:28 +0100217/**
218 * DOC: OA Tail Pointer Race
219 *
220 * There's a HW race condition between OA unit tail pointer register updates and
Robert Braggd7965152016-11-07 19:49:52 +0000221 * writes to memory whereby the tail pointer can sometimes get ahead of what's
Robert Bragg0dd860c2017-05-11 16:43:28 +0100222 * been written out to the OA buffer so far (in terms of what's visible to the
223 * CPU).
Robert Braggd7965152016-11-07 19:49:52 +0000224 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100225 * Although this can be observed explicitly while copying reports to userspace
226 * by checking for a zeroed report-id field in tail reports, we want to account
Robert Bragg19f81df2017-06-13 12:23:03 +0100227 * for this earlier, as part of the oa_buffer_check to avoid lots of redundant
Robert Bragg0dd860c2017-05-11 16:43:28 +0100228 * read() attempts.
Robert Braggd7965152016-11-07 19:49:52 +0000229 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100230 * In effect we define a tail pointer for reading that lags the real tail
231 * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough
232 * time for the corresponding reports to become visible to the CPU.
Robert Braggd7965152016-11-07 19:49:52 +0000233 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100234 * To manage this we actually track two tail pointers:
235 * 1) An 'aging' tail with an associated timestamp that is tracked until we
236 * can trust the corresponding data is visible to the CPU; at which point
237 * it is considered 'aged'.
238 * 2) An 'aged' tail that can be used for read()ing.
239 *
240 * The two separate pointers let us decouple read()s from tail pointer aging.
241 *
242 * The tail pointers are checked and updated at a limited rate within a hrtimer
243 * callback (the same callback that is used for delivering POLLIN events)
244 *
245 * Initially the tails are marked invalid with %INVALID_TAIL_PTR which
246 * indicates that an updated tail pointer is needed.
247 *
248 * Most of the implementation details for this workaround are in
Robert Bragg19f81df2017-06-13 12:23:03 +0100249 * oa_buffer_check_unlocked() and _append_oa_reports()
Robert Bragg0dd860c2017-05-11 16:43:28 +0100250 *
251 * Note for posterity: previously the driver used to define an effective tail
252 * pointer that lagged the real pointer by a 'tail margin' measured in bytes
253 * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
254 * This was flawed considering that the OA unit may also automatically generate
255 * non-periodic reports (such as on context switch) or the OA unit may be
256 * enabled without any periodic sampling.
Robert Braggd7965152016-11-07 19:49:52 +0000257 */
258#define OA_TAIL_MARGIN_NSEC 100000ULL
Robert Bragg0dd860c2017-05-11 16:43:28 +0100259#define INVALID_TAIL_PTR 0xffffffff
Robert Braggd7965152016-11-07 19:49:52 +0000260
261/* frequency for checking whether the OA unit has written new reports to the
262 * circular OA buffer...
263 */
264#define POLL_FREQUENCY 200
265#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
266
Robert Braggccdf6342016-11-07 19:49:54 +0000267/* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
268static int zero;
269static int one = 1;
270static u32 i915_perf_stream_paranoid = true;
271
Robert Braggd7965152016-11-07 19:49:52 +0000272/* The maximum exponent the hardware accepts is 63 (essentially it selects one
273 * of the 64bit timestamp bits to trigger reports from) but there's currently
274 * no known use case for sampling as infrequently as once per 47 thousand years.
275 *
276 * Since the timestamps included in OA reports are only 32bits it seems
277 * reasonable to limit the OA exponent where it's still possible to account for
278 * overflow in OA report timestamps.
279 */
280#define OA_EXPONENT_MAX 31
281
282#define INVALID_CTX_ID 0xffffffff
283
Robert Bragg19f81df2017-06-13 12:23:03 +0100284/* On Gen8+ automatically triggered OA reports include a 'reason' field... */
285#define OAREPORT_REASON_MASK 0x3f
286#define OAREPORT_REASON_SHIFT 19
287#define OAREPORT_REASON_TIMER (1<<0)
288#define OAREPORT_REASON_CTX_SWITCH (1<<3)
289#define OAREPORT_REASON_CLK_RATIO (1<<5)
290
Robert Braggd7965152016-11-07 19:49:52 +0000291
Robert Bragg00319ba2016-11-07 19:49:55 +0000292/* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
293 *
Robert Bragg155e9412017-06-13 12:23:05 +0100294 * The highest sampling frequency we can theoretically program the OA unit
295 * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
296 *
297 * Initialized just before we register the sysctl parameter.
Robert Bragg00319ba2016-11-07 19:49:55 +0000298 */
Robert Bragg155e9412017-06-13 12:23:05 +0100299static int oa_sample_rate_hard_limit;
Robert Bragg00319ba2016-11-07 19:49:55 +0000300
301/* Theoretically we can program the OA unit to sample every 160ns but don't
302 * allow that by default unless root...
303 *
304 * The default threshold of 100000Hz is based on perf's similar
305 * kernel.perf_event_max_sample_rate sysctl parameter.
306 */
307static u32 i915_oa_max_sample_rate = 100000;
308
Robert Braggd7965152016-11-07 19:49:52 +0000309/* XXX: beware if future OA HW adds new report formats that the current
310 * code assumes all reports have a power-of-two size and ~(size - 1) can
311 * be used as a mask to align the OA tail pointer.
312 */
313static struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
314 [I915_OA_FORMAT_A13] = { 0, 64 },
315 [I915_OA_FORMAT_A29] = { 1, 128 },
316 [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
317 /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
318 [I915_OA_FORMAT_B4_C8] = { 4, 64 },
319 [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
320 [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
321 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
322};
323
Robert Bragg19f81df2017-06-13 12:23:03 +0100324static struct i915_oa_format gen8_plus_oa_formats[I915_OA_FORMAT_MAX] = {
325 [I915_OA_FORMAT_A12] = { 0, 64 },
326 [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
327 [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
328 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
329};
330
Robert Braggd7965152016-11-07 19:49:52 +0000331#define SAMPLE_OA_REPORT (1<<0)
Robert Braggeec688e2016-11-07 19:49:47 +0000332
Robert Bragg16d98b32016-12-07 21:40:33 +0000333/**
334 * struct perf_open_properties - for validated properties given to open a stream
335 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
336 * @single_context: Whether a single or all gpu contexts should be monitored
337 * @ctx_handle: A gem ctx handle for use with @single_context
338 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
339 * @oa_format: An OA unit HW report format
340 * @oa_periodic: Whether to enable periodic OA unit sampling
341 * @oa_period_exponent: The OA unit sampling period is derived from this
342 *
343 * As read_properties_unlocked() enumerates and validates the properties given
344 * to open a stream of metrics the configuration is built up in the structure
345 * which starts out zero initialized.
346 */
Robert Braggeec688e2016-11-07 19:49:47 +0000347struct perf_open_properties {
348 u32 sample_flags;
349
350 u64 single_context:1;
351 u64 ctx_handle;
Robert Braggd7965152016-11-07 19:49:52 +0000352
353 /* OA sampling state */
354 int metrics_set;
355 int oa_format;
356 bool oa_periodic;
357 int oa_period_exponent;
Robert Braggeec688e2016-11-07 19:49:47 +0000358};
359
Robert Bragg19f81df2017-06-13 12:23:03 +0100360static u32 gen8_oa_hw_tail_read(struct drm_i915_private *dev_priv)
361{
362 return I915_READ(GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
363}
364
365static u32 gen7_oa_hw_tail_read(struct drm_i915_private *dev_priv)
366{
367 u32 oastatus1 = I915_READ(GEN7_OASTATUS1);
368
369 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
370}
371
Robert Bragg0dd860c2017-05-11 16:43:28 +0100372/**
Robert Bragg19f81df2017-06-13 12:23:03 +0100373 * oa_buffer_check_unlocked - check for data and update tail ptr state
Robert Bragg0dd860c2017-05-11 16:43:28 +0100374 * @dev_priv: i915 device instance
Robert Braggd7965152016-11-07 19:49:52 +0000375 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100376 * This is either called via fops (for blocking reads in user ctx) or the poll
377 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
378 * if there is data available for userspace to read.
Robert Braggd7965152016-11-07 19:49:52 +0000379 *
Robert Bragg0dd860c2017-05-11 16:43:28 +0100380 * This function is central to providing a workaround for the OA unit tail
381 * pointer having a race with respect to what data is visible to the CPU.
382 * It is responsible for reading tail pointers from the hardware and giving
383 * the pointers time to 'age' before they are made available for reading.
384 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
385 *
386 * Besides returning true when there is data available to read() this function
387 * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp
388 * and .aged_tail_idx state used for reading.
389 *
390 * Note: It's safe to read OA config state here unlocked, assuming that this is
391 * only called while the stream is enabled, while the global OA configuration
392 * can't be modified.
393 *
394 * Returns: %true if the OA buffer contains data, else %false
Robert Braggd7965152016-11-07 19:49:52 +0000395 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100396static bool oa_buffer_check_unlocked(struct drm_i915_private *dev_priv)
Robert Braggd7965152016-11-07 19:49:52 +0000397{
398 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100399 unsigned long flags;
400 unsigned int aged_idx;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100401 u32 head, hw_tail, aged_tail, aging_tail;
402 u64 now;
Robert Braggd7965152016-11-07 19:49:52 +0000403
Robert Bragg0dd860c2017-05-11 16:43:28 +0100404 /* We have to consider the (unlikely) possibility that read() errors
405 * could result in an OA buffer reset which might reset the head,
406 * tails[] and aged_tail state.
407 */
408 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
409
410 /* NB: The head we observe here might effectively be a little out of
411 * date (between head and tails[aged_idx].offset if there is currently
412 * a read() in progress.
413 */
414 head = dev_priv->perf.oa.oa_buffer.head;
415
416 aged_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
417 aged_tail = dev_priv->perf.oa.oa_buffer.tails[aged_idx].offset;
418 aging_tail = dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset;
419
Robert Bragg19f81df2017-06-13 12:23:03 +0100420 hw_tail = dev_priv->perf.oa.ops.oa_hw_tail_read(dev_priv);
Robert Bragg0dd860c2017-05-11 16:43:28 +0100421
422 /* The tail pointer increases in 64 byte increments,
423 * not in report_size steps...
424 */
425 hw_tail &= ~(report_size - 1);
426
427 now = ktime_get_mono_fast_ns();
428
Robert Bragg4117ebc2017-05-11 16:43:30 +0100429 /* Update the aged tail
430 *
431 * Flip the tail pointer available for read()s once the aging tail is
432 * old enough to trust that the corresponding data will be visible to
433 * the CPU...
434 *
435 * Do this before updating the aging pointer in case we may be able to
436 * immediately start aging a new pointer too (if new data has become
437 * available) without needing to wait for a later hrtimer callback.
438 */
439 if (aging_tail != INVALID_TAIL_PTR &&
440 ((now - dev_priv->perf.oa.oa_buffer.aging_timestamp) >
441 OA_TAIL_MARGIN_NSEC)) {
Robert Bragg19f81df2017-06-13 12:23:03 +0100442
Robert Bragg4117ebc2017-05-11 16:43:30 +0100443 aged_idx ^= 1;
444 dev_priv->perf.oa.oa_buffer.aged_tail_idx = aged_idx;
445
446 aged_tail = aging_tail;
447
448 /* Mark that we need a new pointer to start aging... */
449 dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR;
450 aging_tail = INVALID_TAIL_PTR;
451 }
452
Robert Bragg0dd860c2017-05-11 16:43:28 +0100453 /* Update the aging tail
454 *
455 * We throttle aging tail updates until we have a new tail that
456 * represents >= one report more data than is already available for
457 * reading. This ensures there will be enough data for a successful
458 * read once this new pointer has aged and ensures we will give the new
459 * pointer time to age.
460 */
461 if (aging_tail == INVALID_TAIL_PTR &&
462 (aged_tail == INVALID_TAIL_PTR ||
463 OA_TAKEN(hw_tail, aged_tail) >= report_size)) {
464 struct i915_vma *vma = dev_priv->perf.oa.oa_buffer.vma;
465 u32 gtt_offset = i915_ggtt_offset(vma);
466
467 /* Be paranoid and do a bounds check on the pointer read back
468 * from hardware, just in case some spurious hardware condition
469 * could put the tail out of bounds...
470 */
471 if (hw_tail >= gtt_offset &&
472 hw_tail < (gtt_offset + OA_BUFFER_SIZE)) {
473 dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset =
474 aging_tail = hw_tail;
475 dev_priv->perf.oa.oa_buffer.aging_timestamp = now;
476 } else {
477 DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %u\n",
478 hw_tail);
479 }
480 }
481
Robert Bragg0dd860c2017-05-11 16:43:28 +0100482 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
483
484 return aged_tail == INVALID_TAIL_PTR ?
485 false : OA_TAKEN(aged_tail, head) >= report_size;
Robert Braggd7965152016-11-07 19:49:52 +0000486}
487
488/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000489 * append_oa_status - Appends a status record to a userspace read() buffer.
490 * @stream: An i915-perf stream opened for OA metrics
491 * @buf: destination buffer given by userspace
492 * @count: the number of bytes userspace wants to read
493 * @offset: (inout): the current position for writing into @buf
494 * @type: The kind of status to report to userspace
495 *
496 * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
497 * into the userspace read() buffer.
498 *
499 * The @buf @offset will only be updated on success.
500 *
501 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000502 */
503static int append_oa_status(struct i915_perf_stream *stream,
504 char __user *buf,
505 size_t count,
506 size_t *offset,
507 enum drm_i915_perf_record_type type)
508{
509 struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
510
511 if ((count - *offset) < header.size)
512 return -ENOSPC;
513
514 if (copy_to_user(buf + *offset, &header, sizeof(header)))
515 return -EFAULT;
516
517 (*offset) += header.size;
518
519 return 0;
520}
521
522/**
Robert Bragg16d98b32016-12-07 21:40:33 +0000523 * append_oa_sample - Copies single OA report into userspace read() buffer.
524 * @stream: An i915-perf stream opened for OA metrics
525 * @buf: destination buffer given by userspace
526 * @count: the number of bytes userspace wants to read
527 * @offset: (inout): the current position for writing into @buf
528 * @report: A single OA report to (optionally) include as part of the sample
529 *
530 * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
531 * properties when opening a stream, tracked as `stream->sample_flags`. This
532 * function copies the requested components of a single sample to the given
533 * read() @buf.
534 *
535 * The @buf @offset will only be updated on success.
536 *
537 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000538 */
539static int append_oa_sample(struct i915_perf_stream *stream,
540 char __user *buf,
541 size_t count,
542 size_t *offset,
543 const u8 *report)
544{
545 struct drm_i915_private *dev_priv = stream->dev_priv;
546 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
547 struct drm_i915_perf_record_header header;
548 u32 sample_flags = stream->sample_flags;
549
550 header.type = DRM_I915_PERF_RECORD_SAMPLE;
551 header.pad = 0;
552 header.size = stream->sample_size;
553
554 if ((count - *offset) < header.size)
555 return -ENOSPC;
556
557 buf += *offset;
558 if (copy_to_user(buf, &header, sizeof(header)))
559 return -EFAULT;
560 buf += sizeof(header);
561
562 if (sample_flags & SAMPLE_OA_REPORT) {
563 if (copy_to_user(buf, report, report_size))
564 return -EFAULT;
565 }
566
567 (*offset) += header.size;
568
569 return 0;
570}
571
572/**
573 * Copies all buffered OA reports into userspace read() buffer.
574 * @stream: An i915-perf stream opened for OA metrics
575 * @buf: destination buffer given by userspace
576 * @count: the number of bytes userspace wants to read
577 * @offset: (inout): the current position for writing into @buf
Robert Braggd7965152016-11-07 19:49:52 +0000578 *
Robert Bragg16d98b32016-12-07 21:40:33 +0000579 * Notably any error condition resulting in a short read (-%ENOSPC or
580 * -%EFAULT) will be returned even though one or more records may
Robert Braggd7965152016-11-07 19:49:52 +0000581 * have been successfully copied. In this case it's up to the caller
582 * to decide if the error should be squashed before returning to
583 * userspace.
584 *
585 * Note: reports are consumed from the head, and appended to the
Robert Bragge81b3a52017-05-11 16:43:24 +0100586 * tail, so the tail chases the head?... If you think that's mad
Robert Braggd7965152016-11-07 19:49:52 +0000587 * and back-to-front you're not alone, but this follows the
588 * Gen PRM naming convention.
Robert Bragg16d98b32016-12-07 21:40:33 +0000589 *
590 * Returns: 0 on success, negative error code on failure.
Robert Braggd7965152016-11-07 19:49:52 +0000591 */
Robert Bragg19f81df2017-06-13 12:23:03 +0100592static int gen8_append_oa_reports(struct i915_perf_stream *stream,
593 char __user *buf,
594 size_t count,
595 size_t *offset)
596{
597 struct drm_i915_private *dev_priv = stream->dev_priv;
598 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
599 u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr;
600 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
601 u32 mask = (OA_BUFFER_SIZE - 1);
602 size_t start_offset = *offset;
603 unsigned long flags;
604 unsigned int aged_tail_idx;
605 u32 head, tail;
606 u32 taken;
607 int ret = 0;
608
609 if (WARN_ON(!stream->enabled))
610 return -EIO;
611
612 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
613
614 head = dev_priv->perf.oa.oa_buffer.head;
615 aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
616 tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset;
617
618 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
619
620 /*
621 * An invalid tail pointer here means we're still waiting for the poll
622 * hrtimer callback to give us a pointer
623 */
624 if (tail == INVALID_TAIL_PTR)
625 return -EAGAIN;
626
627 /*
628 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
629 * while indexing relative to oa_buf_base.
630 */
631 head -= gtt_offset;
632 tail -= gtt_offset;
633
634 /*
635 * An out of bounds or misaligned head or tail pointer implies a driver
636 * bug since we validate + align the tail pointers we read from the
637 * hardware and we are in full control of the head pointer which should
638 * only be incremented by multiples of the report size (notably also
639 * all a power of two).
640 */
641 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
642 tail > OA_BUFFER_SIZE || tail % report_size,
643 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
644 head, tail))
645 return -EIO;
646
647
648 for (/* none */;
649 (taken = OA_TAKEN(tail, head));
650 head = (head + report_size) & mask) {
651 u8 *report = oa_buf_base + head;
652 u32 *report32 = (void *)report;
653 u32 ctx_id;
654 u32 reason;
655
656 /*
657 * All the report sizes factor neatly into the buffer
658 * size so we never expect to see a report split
659 * between the beginning and end of the buffer.
660 *
661 * Given the initial alignment check a misalignment
662 * here would imply a driver bug that would result
663 * in an overrun.
664 */
665 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
666 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
667 break;
668 }
669
670 /*
671 * The reason field includes flags identifying what
672 * triggered this specific report (mostly timer
673 * triggered or e.g. due to a context switch).
674 *
675 * This field is never expected to be zero so we can
676 * check that the report isn't invalid before copying
677 * it to userspace...
678 */
679 reason = ((report32[0] >> OAREPORT_REASON_SHIFT) &
680 OAREPORT_REASON_MASK);
681 if (reason == 0) {
682 if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs))
683 DRM_NOTE("Skipping spurious, invalid OA report\n");
684 continue;
685 }
686
687 /*
688 * XXX: Just keep the lower 21 bits for now since I'm not
689 * entirely sure if the HW touches any of the higher bits in
690 * this field
691 */
692 ctx_id = report32[2] & 0x1fffff;
693
694 /*
695 * Squash whatever is in the CTX_ID field if it's marked as
696 * invalid to be sure we avoid false-positive, single-context
697 * filtering below...
698 *
699 * Note: that we don't clear the valid_ctx_bit so userspace can
700 * understand that the ID has been squashed by the kernel.
701 */
702 if (!(report32[0] & dev_priv->perf.oa.gen8_valid_ctx_bit))
703 ctx_id = report32[2] = INVALID_CTX_ID;
704
705 /*
706 * NB: For Gen 8 the OA unit no longer supports clock gating
707 * off for a specific context and the kernel can't securely
708 * stop the counters from updating as system-wide / global
709 * values.
710 *
711 * Automatic reports now include a context ID so reports can be
712 * filtered on the cpu but it's not worth trying to
713 * automatically subtract/hide counter progress for other
714 * contexts while filtering since we can't stop userspace
715 * issuing MI_REPORT_PERF_COUNT commands which would still
716 * provide a side-band view of the real values.
717 *
718 * To allow userspace (such as Mesa/GL_INTEL_performance_query)
719 * to normalize counters for a single filtered context then it
720 * needs be forwarded bookend context-switch reports so that it
721 * can track switches in between MI_REPORT_PERF_COUNT commands
722 * and can itself subtract/ignore the progress of counters
723 * associated with other contexts. Note that the hardware
724 * automatically triggers reports when switching to a new
725 * context which are tagged with the ID of the newly active
726 * context. To avoid the complexity (and likely fragility) of
727 * reading ahead while parsing reports to try and minimize
728 * forwarding redundant context switch reports (i.e. between
729 * other, unrelated contexts) we simply elect to forward them
730 * all.
731 *
732 * We don't rely solely on the reason field to identify context
733 * switches since it's not-uncommon for periodic samples to
734 * identify a switch before any 'context switch' report.
735 */
736 if (!dev_priv->perf.oa.exclusive_stream->ctx ||
737 dev_priv->perf.oa.specific_ctx_id == ctx_id ||
738 (dev_priv->perf.oa.oa_buffer.last_ctx_id ==
739 dev_priv->perf.oa.specific_ctx_id) ||
740 reason & OAREPORT_REASON_CTX_SWITCH) {
741
742 /*
743 * While filtering for a single context we avoid
744 * leaking the IDs of other contexts.
745 */
746 if (dev_priv->perf.oa.exclusive_stream->ctx &&
747 dev_priv->perf.oa.specific_ctx_id != ctx_id) {
748 report32[2] = INVALID_CTX_ID;
749 }
750
751 ret = append_oa_sample(stream, buf, count, offset,
752 report);
753 if (ret)
754 break;
755
756 dev_priv->perf.oa.oa_buffer.last_ctx_id = ctx_id;
757 }
758
759 /*
760 * The above reason field sanity check is based on
761 * the assumption that the OA buffer is initially
762 * zeroed and we reset the field after copying so the
763 * check is still meaningful once old reports start
764 * being overwritten.
765 */
766 report32[0] = 0;
767 }
768
769 if (start_offset != *offset) {
770 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
771
772 /*
773 * We removed the gtt_offset for the copy loop above, indexing
774 * relative to oa_buf_base so put back here...
775 */
776 head += gtt_offset;
777
778 I915_WRITE(GEN8_OAHEADPTR, head & GEN8_OAHEADPTR_MASK);
779 dev_priv->perf.oa.oa_buffer.head = head;
780
781 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
782 }
783
784 return ret;
785}
786
787/**
788 * gen8_oa_read - copy status records then buffered OA reports
789 * @stream: An i915-perf stream opened for OA metrics
790 * @buf: destination buffer given by userspace
791 * @count: the number of bytes userspace wants to read
792 * @offset: (inout): the current position for writing into @buf
793 *
794 * Checks OA unit status registers and if necessary appends corresponding
795 * status records for userspace (such as for a buffer full condition) and then
796 * initiate appending any buffered OA reports.
797 *
798 * Updates @offset according to the number of bytes successfully copied into
799 * the userspace buffer.
800 *
801 * NB: some data may be successfully copied to the userspace buffer
802 * even if an error is returned, and this is reflected in the
803 * updated @offset.
804 *
805 * Returns: zero on success or a negative error code
806 */
807static int gen8_oa_read(struct i915_perf_stream *stream,
808 char __user *buf,
809 size_t count,
810 size_t *offset)
811{
812 struct drm_i915_private *dev_priv = stream->dev_priv;
813 u32 oastatus;
814 int ret;
815
816 if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr))
817 return -EIO;
818
819 oastatus = I915_READ(GEN8_OASTATUS);
820
821 /*
822 * We treat OABUFFER_OVERFLOW as a significant error:
823 *
824 * Although theoretically we could handle this more gracefully
825 * sometimes, some Gens don't correctly suppress certain
826 * automatically triggered reports in this condition and so we
827 * have to assume that old reports are now being trampled
828 * over.
829 *
830 * Considering how we don't currently give userspace control
831 * over the OA buffer size and always configure a large 16MB
832 * buffer, then a buffer overflow does anyway likely indicate
833 * that something has gone quite badly wrong.
834 */
835 if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
836 ret = append_oa_status(stream, buf, count, offset,
837 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
838 if (ret)
839 return ret;
840
841 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
842 dev_priv->perf.oa.period_exponent);
843
844 dev_priv->perf.oa.ops.oa_disable(dev_priv);
845 dev_priv->perf.oa.ops.oa_enable(dev_priv);
846
847 /*
848 * Note: .oa_enable() is expected to re-init the oabuffer and
849 * reset GEN8_OASTATUS for us
850 */
851 oastatus = I915_READ(GEN8_OASTATUS);
852 }
853
854 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
855 ret = append_oa_status(stream, buf, count, offset,
856 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
857 if (ret)
858 return ret;
859 I915_WRITE(GEN8_OASTATUS,
860 oastatus & ~GEN8_OASTATUS_REPORT_LOST);
861 }
862
863 return gen8_append_oa_reports(stream, buf, count, offset);
864}
865
866/**
867 * Copies all buffered OA reports into userspace read() buffer.
868 * @stream: An i915-perf stream opened for OA metrics
869 * @buf: destination buffer given by userspace
870 * @count: the number of bytes userspace wants to read
871 * @offset: (inout): the current position for writing into @buf
872 *
873 * Notably any error condition resulting in a short read (-%ENOSPC or
874 * -%EFAULT) will be returned even though one or more records may
875 * have been successfully copied. In this case it's up to the caller
876 * to decide if the error should be squashed before returning to
877 * userspace.
878 *
879 * Note: reports are consumed from the head, and appended to the
880 * tail, so the tail chases the head?... If you think that's mad
881 * and back-to-front you're not alone, but this follows the
882 * Gen PRM naming convention.
883 *
884 * Returns: 0 on success, negative error code on failure.
885 */
Robert Braggd7965152016-11-07 19:49:52 +0000886static int gen7_append_oa_reports(struct i915_perf_stream *stream,
887 char __user *buf,
888 size_t count,
Robert Bragg3bb335c2017-05-11 16:43:27 +0100889 size_t *offset)
Robert Braggd7965152016-11-07 19:49:52 +0000890{
891 struct drm_i915_private *dev_priv = stream->dev_priv;
892 int report_size = dev_priv->perf.oa.oa_buffer.format_size;
893 u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr;
Robert Braggd7965152016-11-07 19:49:52 +0000894 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
895 u32 mask = (OA_BUFFER_SIZE - 1);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100896 size_t start_offset = *offset;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100897 unsigned long flags;
898 unsigned int aged_tail_idx;
899 u32 head, tail;
Robert Braggd7965152016-11-07 19:49:52 +0000900 u32 taken;
901 int ret = 0;
902
903 if (WARN_ON(!stream->enabled))
904 return -EIO;
905
Robert Bragg0dd860c2017-05-11 16:43:28 +0100906 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
Robert Braggf2790202017-05-11 16:43:26 +0100907
Robert Bragg0dd860c2017-05-11 16:43:28 +0100908 head = dev_priv->perf.oa.oa_buffer.head;
909 aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
910 tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset;
911
912 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
913
914 /* An invalid tail pointer here means we're still waiting for the poll
915 * hrtimer callback to give us a pointer
Robert Braggf2790202017-05-11 16:43:26 +0100916 */
Robert Bragg0dd860c2017-05-11 16:43:28 +0100917 if (tail == INVALID_TAIL_PTR)
Robert Braggd7965152016-11-07 19:49:52 +0000918 return -EAGAIN;
919
Robert Bragg0dd860c2017-05-11 16:43:28 +0100920 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
921 * while indexing relative to oa_buf_base.
922 */
923 head -= gtt_offset;
924 tail -= gtt_offset;
925
926 /* An out of bounds or misaligned head or tail pointer implies a driver
927 * bug since we validate + align the tail pointers we read from the
928 * hardware and we are in full control of the head pointer which should
929 * only be incremented by multiples of the report size (notably also
930 * all a power of two).
931 */
932 if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
933 tail > OA_BUFFER_SIZE || tail % report_size,
934 "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
935 head, tail))
936 return -EIO;
937
Robert Braggd7965152016-11-07 19:49:52 +0000938
939 for (/* none */;
940 (taken = OA_TAKEN(tail, head));
941 head = (head + report_size) & mask) {
942 u8 *report = oa_buf_base + head;
943 u32 *report32 = (void *)report;
944
945 /* All the report sizes factor neatly into the buffer
946 * size so we never expect to see a report split
947 * between the beginning and end of the buffer.
948 *
949 * Given the initial alignment check a misalignment
950 * here would imply a driver bug that would result
951 * in an overrun.
952 */
953 if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
954 DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
955 break;
956 }
957
958 /* The report-ID field for periodic samples includes
959 * some undocumented flags related to what triggered
960 * the report and is never expected to be zero so we
961 * can check that the report isn't invalid before
962 * copying it to userspace...
963 */
964 if (report32[0] == 0) {
Robert Bragg712122e2017-05-11 16:43:31 +0100965 if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs))
966 DRM_NOTE("Skipping spurious, invalid OA report\n");
Robert Braggd7965152016-11-07 19:49:52 +0000967 continue;
968 }
969
970 ret = append_oa_sample(stream, buf, count, offset, report);
971 if (ret)
972 break;
973
974 /* The above report-id field sanity check is based on
975 * the assumption that the OA buffer is initially
976 * zeroed and we reset the field after copying so the
977 * check is still meaningful once old reports start
978 * being overwritten.
979 */
980 report32[0] = 0;
981 }
982
Robert Bragg3bb335c2017-05-11 16:43:27 +0100983 if (start_offset != *offset) {
Robert Bragg0dd860c2017-05-11 16:43:28 +0100984 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
985
Robert Bragg3bb335c2017-05-11 16:43:27 +0100986 /* We removed the gtt_offset for the copy loop above, indexing
987 * relative to oa_buf_base so put back here...
988 */
989 head += gtt_offset;
990
991 I915_WRITE(GEN7_OASTATUS2,
992 ((head & GEN7_OASTATUS2_HEAD_MASK) |
993 OA_MEM_SELECT_GGTT));
994 dev_priv->perf.oa.oa_buffer.head = head;
Robert Bragg0dd860c2017-05-11 16:43:28 +0100995
996 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
Robert Bragg3bb335c2017-05-11 16:43:27 +0100997 }
Robert Braggd7965152016-11-07 19:49:52 +0000998
999 return ret;
1000}
1001
Robert Bragg16d98b32016-12-07 21:40:33 +00001002/**
1003 * gen7_oa_read - copy status records then buffered OA reports
1004 * @stream: An i915-perf stream opened for OA metrics
1005 * @buf: destination buffer given by userspace
1006 * @count: the number of bytes userspace wants to read
1007 * @offset: (inout): the current position for writing into @buf
1008 *
1009 * Checks Gen 7 specific OA unit status registers and if necessary appends
1010 * corresponding status records for userspace (such as for a buffer full
1011 * condition) and then initiate appending any buffered OA reports.
1012 *
1013 * Updates @offset according to the number of bytes successfully copied into
1014 * the userspace buffer.
1015 *
1016 * Returns: zero on success or a negative error code
1017 */
Robert Braggd7965152016-11-07 19:49:52 +00001018static int gen7_oa_read(struct i915_perf_stream *stream,
1019 char __user *buf,
1020 size_t count,
1021 size_t *offset)
1022{
1023 struct drm_i915_private *dev_priv = stream->dev_priv;
Robert Braggd7965152016-11-07 19:49:52 +00001024 u32 oastatus1;
Robert Braggd7965152016-11-07 19:49:52 +00001025 int ret;
1026
1027 if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr))
1028 return -EIO;
1029
Robert Braggd7965152016-11-07 19:49:52 +00001030 oastatus1 = I915_READ(GEN7_OASTATUS1);
1031
Robert Braggd7965152016-11-07 19:49:52 +00001032 /* XXX: On Haswell we don't have a safe way to clear oastatus1
1033 * bits while the OA unit is enabled (while the tail pointer
1034 * may be updated asynchronously) so we ignore status bits
1035 * that have already been reported to userspace.
1036 */
1037 oastatus1 &= ~dev_priv->perf.oa.gen7_latched_oastatus1;
1038
1039 /* We treat OABUFFER_OVERFLOW as a significant error:
1040 *
1041 * - The status can be interpreted to mean that the buffer is
1042 * currently full (with a higher precedence than OA_TAKEN()
1043 * which will start to report a near-empty buffer after an
1044 * overflow) but it's awkward that we can't clear the status
1045 * on Haswell, so without a reset we won't be able to catch
1046 * the state again.
1047 *
1048 * - Since it also implies the HW has started overwriting old
1049 * reports it may also affect our sanity checks for invalid
1050 * reports when copying to userspace that assume new reports
1051 * are being written to cleared memory.
1052 *
1053 * - In the future we may want to introduce a flight recorder
1054 * mode where the driver will automatically maintain a safe
1055 * guard band between head/tail, avoiding this overflow
1056 * condition, but we avoid the added driver complexity for
1057 * now.
1058 */
1059 if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
1060 ret = append_oa_status(stream, buf, count, offset,
1061 DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
1062 if (ret)
1063 return ret;
1064
Robert Bragg19f81df2017-06-13 12:23:03 +01001065 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
1066 dev_priv->perf.oa.period_exponent);
Robert Braggd7965152016-11-07 19:49:52 +00001067
1068 dev_priv->perf.oa.ops.oa_disable(dev_priv);
1069 dev_priv->perf.oa.ops.oa_enable(dev_priv);
1070
Robert Braggd7965152016-11-07 19:49:52 +00001071 oastatus1 = I915_READ(GEN7_OASTATUS1);
Robert Braggd7965152016-11-07 19:49:52 +00001072 }
1073
1074 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
1075 ret = append_oa_status(stream, buf, count, offset,
1076 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
1077 if (ret)
1078 return ret;
1079 dev_priv->perf.oa.gen7_latched_oastatus1 |=
1080 GEN7_OASTATUS1_REPORT_LOST;
1081 }
1082
Robert Bragg3bb335c2017-05-11 16:43:27 +01001083 return gen7_append_oa_reports(stream, buf, count, offset);
Robert Braggd7965152016-11-07 19:49:52 +00001084}
1085
Robert Bragg16d98b32016-12-07 21:40:33 +00001086/**
1087 * i915_oa_wait_unlocked - handles blocking IO until OA data available
1088 * @stream: An i915-perf stream opened for OA metrics
1089 *
1090 * Called when userspace tries to read() from a blocking stream FD opened
1091 * for OA metrics. It waits until the hrtimer callback finds a non-empty
1092 * OA buffer and wakes us.
1093 *
1094 * Note: it's acceptable to have this return with some false positives
1095 * since any subsequent read handling will return -EAGAIN if there isn't
1096 * really data ready for userspace yet.
1097 *
1098 * Returns: zero on success or a negative error code
1099 */
Robert Braggd7965152016-11-07 19:49:52 +00001100static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
1101{
1102 struct drm_i915_private *dev_priv = stream->dev_priv;
1103
1104 /* We would wait indefinitely if periodic sampling is not enabled */
1105 if (!dev_priv->perf.oa.periodic)
1106 return -EIO;
1107
Robert Braggd7965152016-11-07 19:49:52 +00001108 return wait_event_interruptible(dev_priv->perf.oa.poll_wq,
Robert Bragg19f81df2017-06-13 12:23:03 +01001109 oa_buffer_check_unlocked(dev_priv));
Robert Braggd7965152016-11-07 19:49:52 +00001110}
1111
Robert Bragg16d98b32016-12-07 21:40:33 +00001112/**
1113 * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
1114 * @stream: An i915-perf stream opened for OA metrics
1115 * @file: An i915 perf stream file
1116 * @wait: poll() state table
1117 *
1118 * For handling userspace polling on an i915 perf stream opened for OA metrics,
1119 * this starts a poll_wait with the wait queue that our hrtimer callback wakes
1120 * when it sees data ready to read in the circular OA buffer.
1121 */
Robert Braggd7965152016-11-07 19:49:52 +00001122static void i915_oa_poll_wait(struct i915_perf_stream *stream,
1123 struct file *file,
1124 poll_table *wait)
1125{
1126 struct drm_i915_private *dev_priv = stream->dev_priv;
1127
1128 poll_wait(file, &dev_priv->perf.oa.poll_wq, wait);
1129}
1130
Robert Bragg16d98b32016-12-07 21:40:33 +00001131/**
1132 * i915_oa_read - just calls through to &i915_oa_ops->read
1133 * @stream: An i915-perf stream opened for OA metrics
1134 * @buf: destination buffer given by userspace
1135 * @count: the number of bytes userspace wants to read
1136 * @offset: (inout): the current position for writing into @buf
1137 *
1138 * Updates @offset according to the number of bytes successfully copied into
1139 * the userspace buffer.
1140 *
1141 * Returns: zero on success or a negative error code
1142 */
Robert Braggd7965152016-11-07 19:49:52 +00001143static int i915_oa_read(struct i915_perf_stream *stream,
1144 char __user *buf,
1145 size_t count,
1146 size_t *offset)
1147{
1148 struct drm_i915_private *dev_priv = stream->dev_priv;
1149
1150 return dev_priv->perf.oa.ops.read(stream, buf, count, offset);
1151}
1152
Robert Bragg16d98b32016-12-07 21:40:33 +00001153/**
1154 * oa_get_render_ctx_id - determine and hold ctx hw id
1155 * @stream: An i915-perf stream opened for OA metrics
1156 *
1157 * Determine the render context hw id, and ensure it remains fixed for the
Robert Braggd7965152016-11-07 19:49:52 +00001158 * lifetime of the stream. This ensures that we don't have to worry about
1159 * updating the context ID in OACONTROL on the fly.
Robert Bragg16d98b32016-12-07 21:40:33 +00001160 *
1161 * Returns: zero on success or a negative error code
Robert Braggd7965152016-11-07 19:49:52 +00001162 */
1163static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
1164{
1165 struct drm_i915_private *dev_priv = stream->dev_priv;
Robert Braggd7965152016-11-07 19:49:52 +00001166
Robert Bragg19f81df2017-06-13 12:23:03 +01001167 if (i915.enable_execlists)
1168 dev_priv->perf.oa.specific_ctx_id = stream->ctx->hw_id;
1169 else {
1170 struct intel_engine_cs *engine = dev_priv->engine[RCS];
1171 struct intel_ring *ring;
1172 int ret;
Robert Braggd7965152016-11-07 19:49:52 +00001173
Robert Bragg19f81df2017-06-13 12:23:03 +01001174 ret = i915_mutex_lock_interruptible(&dev_priv->drm);
1175 if (ret)
1176 return ret;
Robert Braggd7965152016-11-07 19:49:52 +00001177
Robert Bragg19f81df2017-06-13 12:23:03 +01001178 /*
1179 * As the ID is the gtt offset of the context's vma we
1180 * pin the vma to ensure the ID remains fixed.
1181 *
1182 * NB: implied RCS engine...
1183 */
1184 ring = engine->context_pin(engine, stream->ctx);
1185 mutex_unlock(&dev_priv->drm.struct_mutex);
1186 if (IS_ERR(ring))
1187 return PTR_ERR(ring);
1188
1189
1190 /*
1191 * Explicitly track the ID (instead of calling
1192 * i915_ggtt_offset() on the fly) considering the difference
1193 * with gen8+ and execlists
1194 */
1195 dev_priv->perf.oa.specific_ctx_id =
1196 i915_ggtt_offset(stream->ctx->engine[engine->id].state);
1197 }
Robert Braggd7965152016-11-07 19:49:52 +00001198
Chris Wilson266a2402017-05-04 10:33:08 +01001199 return 0;
Robert Braggd7965152016-11-07 19:49:52 +00001200}
1201
Robert Bragg16d98b32016-12-07 21:40:33 +00001202/**
1203 * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
1204 * @stream: An i915-perf stream opened for OA metrics
1205 *
1206 * In case anything needed doing to ensure the context HW ID would remain valid
1207 * for the lifetime of the stream, then that can be undone here.
1208 */
Robert Braggd7965152016-11-07 19:49:52 +00001209static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
1210{
1211 struct drm_i915_private *dev_priv = stream->dev_priv;
1212
Robert Bragg19f81df2017-06-13 12:23:03 +01001213 if (i915.enable_execlists) {
1214 dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID;
1215 } else {
1216 struct intel_engine_cs *engine = dev_priv->engine[RCS];
Robert Braggd7965152016-11-07 19:49:52 +00001217
Robert Bragg19f81df2017-06-13 12:23:03 +01001218 mutex_lock(&dev_priv->drm.struct_mutex);
Robert Braggd7965152016-11-07 19:49:52 +00001219
Robert Bragg19f81df2017-06-13 12:23:03 +01001220 dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID;
1221 engine->context_unpin(engine, stream->ctx);
1222
1223 mutex_unlock(&dev_priv->drm.struct_mutex);
1224 }
Robert Braggd7965152016-11-07 19:49:52 +00001225}
1226
1227static void
1228free_oa_buffer(struct drm_i915_private *i915)
1229{
1230 mutex_lock(&i915->drm.struct_mutex);
1231
1232 i915_gem_object_unpin_map(i915->perf.oa.oa_buffer.vma->obj);
1233 i915_vma_unpin(i915->perf.oa.oa_buffer.vma);
1234 i915_gem_object_put(i915->perf.oa.oa_buffer.vma->obj);
1235
1236 i915->perf.oa.oa_buffer.vma = NULL;
1237 i915->perf.oa.oa_buffer.vaddr = NULL;
1238
1239 mutex_unlock(&i915->drm.struct_mutex);
1240}
1241
1242static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
1243{
1244 struct drm_i915_private *dev_priv = stream->dev_priv;
1245
1246 BUG_ON(stream != dev_priv->perf.oa.exclusive_stream);
1247
Robert Bragg19f81df2017-06-13 12:23:03 +01001248 /*
1249 * Unset exclusive_stream first, it might be checked while
1250 * disabling the metric set on gen8+.
1251 */
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001252 mutex_lock(&dev_priv->drm.struct_mutex);
Robert Bragg19f81df2017-06-13 12:23:03 +01001253 dev_priv->perf.oa.exclusive_stream = NULL;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001254 mutex_unlock(&dev_priv->drm.struct_mutex);
Robert Bragg19f81df2017-06-13 12:23:03 +01001255
Robert Braggd7965152016-11-07 19:49:52 +00001256 dev_priv->perf.oa.ops.disable_metric_set(dev_priv);
1257
1258 free_oa_buffer(dev_priv);
1259
1260 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
1261 intel_runtime_pm_put(dev_priv);
1262
1263 if (stream->ctx)
1264 oa_put_render_ctx_id(stream);
1265
Robert Bragg712122e2017-05-11 16:43:31 +01001266 if (dev_priv->perf.oa.spurious_report_rs.missed) {
1267 DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n",
1268 dev_priv->perf.oa.spurious_report_rs.missed);
1269 }
Robert Braggd7965152016-11-07 19:49:52 +00001270}
1271
1272static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv)
1273{
1274 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
Robert Bragg0dd860c2017-05-11 16:43:28 +01001275 unsigned long flags;
1276
1277 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
Robert Braggd7965152016-11-07 19:49:52 +00001278
1279 /* Pre-DevBDW: OABUFFER must be set with counters off,
1280 * before OASTATUS1, but after OASTATUS2
1281 */
1282 I915_WRITE(GEN7_OASTATUS2, gtt_offset | OA_MEM_SELECT_GGTT); /* head */
Robert Braggf2790202017-05-11 16:43:26 +01001283 dev_priv->perf.oa.oa_buffer.head = gtt_offset;
1284
Robert Braggd7965152016-11-07 19:49:52 +00001285 I915_WRITE(GEN7_OABUFFER, gtt_offset);
Robert Braggf2790202017-05-11 16:43:26 +01001286
Robert Braggd7965152016-11-07 19:49:52 +00001287 I915_WRITE(GEN7_OASTATUS1, gtt_offset | OABUFFER_SIZE_16M); /* tail */
1288
Robert Bragg0dd860c2017-05-11 16:43:28 +01001289 /* Mark that we need updated tail pointers to read from... */
1290 dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1291 dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1292
1293 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1294
Robert Braggd7965152016-11-07 19:49:52 +00001295 /* On Haswell we have to track which OASTATUS1 flags we've
1296 * already seen since they can't be cleared while periodic
1297 * sampling is enabled.
1298 */
1299 dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
1300
1301 /* NB: although the OA buffer will initially be allocated
1302 * zeroed via shmfs (and so this memset is redundant when
1303 * first allocating), we may re-init the OA buffer, either
1304 * when re-enabling a stream or in error/reset paths.
1305 *
1306 * The reason we clear the buffer for each re-init is for the
1307 * sanity check in gen7_append_oa_reports() that looks at the
1308 * report-id field to make sure it's non-zero which relies on
1309 * the assumption that new reports are being written to zeroed
1310 * memory...
1311 */
1312 memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1313
1314 /* Maybe make ->pollin per-stream state if we support multiple
1315 * concurrent streams in the future.
1316 */
1317 dev_priv->perf.oa.pollin = false;
1318}
1319
Robert Bragg19f81df2017-06-13 12:23:03 +01001320static void gen8_init_oa_buffer(struct drm_i915_private *dev_priv)
1321{
1322 u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
1323 unsigned long flags;
1324
1325 spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1326
1327 I915_WRITE(GEN8_OASTATUS, 0);
1328 I915_WRITE(GEN8_OAHEADPTR, gtt_offset);
1329 dev_priv->perf.oa.oa_buffer.head = gtt_offset;
1330
1331 I915_WRITE(GEN8_OABUFFER_UDW, 0);
1332
1333 /*
1334 * PRM says:
1335 *
1336 * "This MMIO must be set before the OATAILPTR
1337 * register and after the OAHEADPTR register. This is
1338 * to enable proper functionality of the overflow
1339 * bit."
1340 */
1341 I915_WRITE(GEN8_OABUFFER, gtt_offset |
1342 OABUFFER_SIZE_16M | OA_MEM_SELECT_GGTT);
1343 I915_WRITE(GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
1344
1345 /* Mark that we need updated tail pointers to read from... */
1346 dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1347 dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1348
1349 /*
1350 * Reset state used to recognise context switches, affecting which
1351 * reports we will forward to userspace while filtering for a single
1352 * context.
1353 */
1354 dev_priv->perf.oa.oa_buffer.last_ctx_id = INVALID_CTX_ID;
1355
1356 spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1357
1358 /*
1359 * NB: although the OA buffer will initially be allocated
1360 * zeroed via shmfs (and so this memset is redundant when
1361 * first allocating), we may re-init the OA buffer, either
1362 * when re-enabling a stream or in error/reset paths.
1363 *
1364 * The reason we clear the buffer for each re-init is for the
1365 * sanity check in gen8_append_oa_reports() that looks at the
1366 * reason field to make sure it's non-zero which relies on
1367 * the assumption that new reports are being written to zeroed
1368 * memory...
1369 */
1370 memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1371
1372 /*
1373 * Maybe make ->pollin per-stream state if we support multiple
1374 * concurrent streams in the future.
1375 */
1376 dev_priv->perf.oa.pollin = false;
1377}
1378
Robert Braggd7965152016-11-07 19:49:52 +00001379static int alloc_oa_buffer(struct drm_i915_private *dev_priv)
1380{
1381 struct drm_i915_gem_object *bo;
1382 struct i915_vma *vma;
1383 int ret;
1384
1385 if (WARN_ON(dev_priv->perf.oa.oa_buffer.vma))
1386 return -ENODEV;
1387
1388 ret = i915_mutex_lock_interruptible(&dev_priv->drm);
1389 if (ret)
1390 return ret;
1391
1392 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
1393 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
1394
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00001395 bo = i915_gem_object_create(dev_priv, OA_BUFFER_SIZE);
Robert Braggd7965152016-11-07 19:49:52 +00001396 if (IS_ERR(bo)) {
1397 DRM_ERROR("Failed to allocate OA buffer\n");
1398 ret = PTR_ERR(bo);
1399 goto unlock;
1400 }
1401
1402 ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC);
1403 if (ret)
1404 goto err_unref;
1405
1406 /* PreHSW required 512K alignment, HSW requires 16M */
1407 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0);
1408 if (IS_ERR(vma)) {
1409 ret = PTR_ERR(vma);
1410 goto err_unref;
1411 }
1412 dev_priv->perf.oa.oa_buffer.vma = vma;
1413
1414 dev_priv->perf.oa.oa_buffer.vaddr =
1415 i915_gem_object_pin_map(bo, I915_MAP_WB);
1416 if (IS_ERR(dev_priv->perf.oa.oa_buffer.vaddr)) {
1417 ret = PTR_ERR(dev_priv->perf.oa.oa_buffer.vaddr);
1418 goto err_unpin;
1419 }
1420
1421 dev_priv->perf.oa.ops.init_oa_buffer(dev_priv);
1422
1423 DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p\n",
1424 i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma),
1425 dev_priv->perf.oa.oa_buffer.vaddr);
1426
1427 goto unlock;
1428
1429err_unpin:
1430 __i915_vma_unpin(vma);
1431
1432err_unref:
1433 i915_gem_object_put(bo);
1434
1435 dev_priv->perf.oa.oa_buffer.vaddr = NULL;
1436 dev_priv->perf.oa.oa_buffer.vma = NULL;
1437
1438unlock:
1439 mutex_unlock(&dev_priv->drm.struct_mutex);
1440 return ret;
1441}
1442
1443static void config_oa_regs(struct drm_i915_private *dev_priv,
1444 const struct i915_oa_reg *regs,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001445 u32 n_regs)
Robert Braggd7965152016-11-07 19:49:52 +00001446{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001447 u32 i;
Robert Braggd7965152016-11-07 19:49:52 +00001448
1449 for (i = 0; i < n_regs; i++) {
1450 const struct i915_oa_reg *reg = regs + i;
1451
1452 I915_WRITE(reg->addr, reg->value);
1453 }
1454}
1455
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001456static int hsw_enable_metric_set(struct drm_i915_private *dev_priv,
1457 const struct i915_oa_config *oa_config)
Robert Braggd7965152016-11-07 19:49:52 +00001458{
Robert Braggd7965152016-11-07 19:49:52 +00001459 /* PRM:
1460 *
1461 * OA unit is using “crclk” for its functionality. When trunk
1462 * level clock gating takes place, OA clock would be gated,
1463 * unable to count the events from non-render clock domain.
1464 * Render clock gating must be disabled when OA is enabled to
1465 * count the events from non-render domain. Unit level clock
1466 * gating for RCS should also be disabled.
1467 */
1468 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1469 ~GEN7_DOP_CLOCK_GATE_ENABLE));
1470 I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) |
1471 GEN6_CSUNIT_CLOCK_GATE_DISABLE));
1472
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001473 config_oa_regs(dev_priv, oa_config->mux_regs, oa_config->mux_regs_len);
Robert Braggd7965152016-11-07 19:49:52 +00001474
1475 /* It apparently takes a fairly long time for a new MUX
1476 * configuration to be be applied after these register writes.
1477 * This delay duration was derived empirically based on the
1478 * render_basic config but hopefully it covers the maximum
1479 * configuration latency.
1480 *
1481 * As a fallback, the checks in _append_oa_reports() to skip
1482 * invalid OA reports do also seem to work to discard reports
1483 * generated before this config has completed - albeit not
1484 * silently.
1485 *
1486 * Unfortunately this is essentially a magic number, since we
1487 * don't currently know of a reliable mechanism for predicting
1488 * how long the MUX config will take to apply and besides
1489 * seeing invalid reports we don't know of a reliable way to
1490 * explicitly check that the MUX config has landed.
1491 *
1492 * It's even possible we've miss characterized the underlying
1493 * problem - it just seems like the simplest explanation why
1494 * a delay at this location would mitigate any invalid reports.
1495 */
1496 usleep_range(15000, 20000);
1497
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001498 config_oa_regs(dev_priv, oa_config->b_counter_regs,
1499 oa_config->b_counter_regs_len);
Robert Braggd7965152016-11-07 19:49:52 +00001500
1501 return 0;
1502}
1503
1504static void hsw_disable_metric_set(struct drm_i915_private *dev_priv)
1505{
1506 I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) &
1507 ~GEN6_CSUNIT_CLOCK_GATE_DISABLE));
1508 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) |
1509 GEN7_DOP_CLOCK_GATE_ENABLE));
1510
1511 I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
1512 ~GT_NOA_ENABLE));
1513}
1514
Robert Bragg19f81df2017-06-13 12:23:03 +01001515/*
1516 * NB: It must always remain pointer safe to run this even if the OA unit
1517 * has been disabled.
1518 *
1519 * It's fine to put out-of-date values into these per-context registers
1520 * in the case that the OA unit has been disabled.
1521 */
1522static void gen8_update_reg_state_unlocked(struct i915_gem_context *ctx,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001523 u32 *reg_state,
1524 const struct i915_oa_config *oa_config)
Robert Bragg19f81df2017-06-13 12:23:03 +01001525{
1526 struct drm_i915_private *dev_priv = ctx->i915;
Robert Bragg19f81df2017-06-13 12:23:03 +01001527 u32 ctx_oactxctrl = dev_priv->perf.oa.ctx_oactxctrl_offset;
1528 u32 ctx_flexeu0 = dev_priv->perf.oa.ctx_flexeu0_offset;
1529 /* The MMIO offsets for Flex EU registers aren't contiguous */
1530 u32 flex_mmio[] = {
1531 i915_mmio_reg_offset(EU_PERF_CNTL0),
1532 i915_mmio_reg_offset(EU_PERF_CNTL1),
1533 i915_mmio_reg_offset(EU_PERF_CNTL2),
1534 i915_mmio_reg_offset(EU_PERF_CNTL3),
1535 i915_mmio_reg_offset(EU_PERF_CNTL4),
1536 i915_mmio_reg_offset(EU_PERF_CNTL5),
1537 i915_mmio_reg_offset(EU_PERF_CNTL6),
1538 };
1539 int i;
1540
1541 reg_state[ctx_oactxctrl] = i915_mmio_reg_offset(GEN8_OACTXCONTROL);
1542 reg_state[ctx_oactxctrl+1] = (dev_priv->perf.oa.period_exponent <<
1543 GEN8_OA_TIMER_PERIOD_SHIFT) |
1544 (dev_priv->perf.oa.periodic ?
1545 GEN8_OA_TIMER_ENABLE : 0) |
1546 GEN8_OA_COUNTER_RESUME;
1547
1548 for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) {
1549 u32 state_offset = ctx_flexeu0 + i * 2;
1550 u32 mmio = flex_mmio[i];
1551
1552 /*
1553 * This arbitrary default will select the 'EU FPU0 Pipeline
1554 * Active' event. In the future it's anticipated that there
1555 * will be an explicit 'No Event' we can select, but not yet...
1556 */
1557 u32 value = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01001558
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001559 if (oa_config) {
1560 u32 j;
1561
1562 for (j = 0; j < oa_config->flex_regs_len; j++) {
1563 if (i915_mmio_reg_offset(oa_config->flex_regs[j].addr) == mmio) {
1564 value = oa_config->flex_regs[j].value;
1565 break;
1566 }
Robert Bragg19f81df2017-06-13 12:23:03 +01001567 }
1568 }
1569
1570 reg_state[state_offset] = mmio;
1571 reg_state[state_offset+1] = value;
1572 }
1573}
1574
1575/*
1576 * Same as gen8_update_reg_state_unlocked only through the batchbuffer. This
1577 * is only used by the kernel context.
1578 */
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001579static int gen8_emit_oa_config(struct drm_i915_gem_request *req,
1580 const struct i915_oa_config *oa_config)
Robert Bragg19f81df2017-06-13 12:23:03 +01001581{
1582 struct drm_i915_private *dev_priv = req->i915;
Robert Bragg19f81df2017-06-13 12:23:03 +01001583 /* The MMIO offsets for Flex EU registers aren't contiguous */
1584 u32 flex_mmio[] = {
1585 i915_mmio_reg_offset(EU_PERF_CNTL0),
1586 i915_mmio_reg_offset(EU_PERF_CNTL1),
1587 i915_mmio_reg_offset(EU_PERF_CNTL2),
1588 i915_mmio_reg_offset(EU_PERF_CNTL3),
1589 i915_mmio_reg_offset(EU_PERF_CNTL4),
1590 i915_mmio_reg_offset(EU_PERF_CNTL5),
1591 i915_mmio_reg_offset(EU_PERF_CNTL6),
1592 };
1593 u32 *cs;
1594 int i;
1595
Lionel Landwerlin01d928e2017-08-03 17:58:07 +01001596 cs = intel_ring_begin(req, ARRAY_SIZE(flex_mmio) * 2 + 4);
Robert Bragg19f81df2017-06-13 12:23:03 +01001597 if (IS_ERR(cs))
1598 return PTR_ERR(cs);
1599
Lionel Landwerlin01d928e2017-08-03 17:58:07 +01001600 *cs++ = MI_LOAD_REGISTER_IMM(ARRAY_SIZE(flex_mmio) + 1);
Robert Bragg19f81df2017-06-13 12:23:03 +01001601
1602 *cs++ = i915_mmio_reg_offset(GEN8_OACTXCONTROL);
1603 *cs++ = (dev_priv->perf.oa.period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
1604 (dev_priv->perf.oa.periodic ? GEN8_OA_TIMER_ENABLE : 0) |
1605 GEN8_OA_COUNTER_RESUME;
1606
1607 for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) {
1608 u32 mmio = flex_mmio[i];
1609
1610 /*
1611 * This arbitrary default will select the 'EU FPU0 Pipeline
1612 * Active' event. In the future it's anticipated that there
1613 * will be an explicit 'No Event' we can select, but not
1614 * yet...
1615 */
1616 u32 value = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01001617
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001618 if (oa_config) {
1619 u32 j;
1620
1621 for (j = 0; j < oa_config->flex_regs_len; j++) {
1622 if (i915_mmio_reg_offset(oa_config->flex_regs[j].addr) == mmio) {
1623 value = oa_config->flex_regs[j].value;
1624 break;
1625 }
Robert Bragg19f81df2017-06-13 12:23:03 +01001626 }
1627 }
1628
1629 *cs++ = mmio;
1630 *cs++ = value;
1631 }
1632
1633 *cs++ = MI_NOOP;
1634 intel_ring_advance(req, cs);
1635
1636 return 0;
1637}
1638
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001639static int gen8_switch_to_updated_kernel_context(struct drm_i915_private *dev_priv,
1640 const struct i915_oa_config *oa_config)
Robert Bragg19f81df2017-06-13 12:23:03 +01001641{
1642 struct intel_engine_cs *engine = dev_priv->engine[RCS];
1643 struct i915_gem_timeline *timeline;
1644 struct drm_i915_gem_request *req;
1645 int ret;
1646
1647 lockdep_assert_held(&dev_priv->drm.struct_mutex);
1648
1649 i915_gem_retire_requests(dev_priv);
1650
1651 req = i915_gem_request_alloc(engine, dev_priv->kernel_context);
1652 if (IS_ERR(req))
1653 return PTR_ERR(req);
1654
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001655 ret = gen8_emit_oa_config(req, oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01001656 if (ret) {
1657 i915_add_request(req);
1658 return ret;
1659 }
1660
1661 /* Queue this switch after all other activity */
1662 list_for_each_entry(timeline, &dev_priv->gt.timelines, link) {
1663 struct drm_i915_gem_request *prev;
1664 struct intel_timeline *tl;
1665
1666 tl = &timeline->engine[engine->id];
1667 prev = i915_gem_active_raw(&tl->last_request,
1668 &dev_priv->drm.struct_mutex);
1669 if (prev)
1670 i915_sw_fence_await_sw_fence_gfp(&req->submit,
1671 &prev->submit,
1672 GFP_KERNEL);
1673 }
1674
1675 ret = i915_switch_context(req);
1676 i915_add_request(req);
1677
1678 return ret;
1679}
1680
1681/*
1682 * Manages updating the per-context aspects of the OA stream
1683 * configuration across all contexts.
1684 *
1685 * The awkward consideration here is that OACTXCONTROL controls the
1686 * exponent for periodic sampling which is primarily used for system
1687 * wide profiling where we'd like a consistent sampling period even in
1688 * the face of context switches.
1689 *
1690 * Our approach of updating the register state context (as opposed to
1691 * say using a workaround batch buffer) ensures that the hardware
1692 * won't automatically reload an out-of-date timer exponent even
1693 * transiently before a WA BB could be parsed.
1694 *
1695 * This function needs to:
1696 * - Ensure the currently running context's per-context OA state is
1697 * updated
1698 * - Ensure that all existing contexts will have the correct per-context
1699 * OA state if they are scheduled for use.
1700 * - Ensure any new contexts will be initialized with the correct
1701 * per-context OA state.
1702 *
1703 * Note: it's only the RCS/Render context that has any OA state.
1704 */
1705static int gen8_configure_all_contexts(struct drm_i915_private *dev_priv,
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001706 const struct i915_oa_config *oa_config,
Robert Bragg19f81df2017-06-13 12:23:03 +01001707 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. */
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001724 ret = gen8_switch_to_updated_kernel_context(dev_priv, oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01001725 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. */
Chris Wilson829a0af2017-06-20 12:05:45 +01001746 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Robert Bragg19f81df2017-06-13 12:23:03 +01001747 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
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001763 gen8_update_reg_state_unlocked(ctx, regs, oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01001764
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
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001774static int gen8_enable_metric_set(struct drm_i915_private *dev_priv,
1775 const struct i915_oa_config *oa_config)
Robert Bragg19f81df2017-06-13 12:23:03 +01001776{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001777 int ret;
Robert Bragg19f81df2017-06-13 12:23:03 +01001778
1779 /*
1780 * We disable slice/unslice clock ratio change reports on SKL since
1781 * they are too noisy. The HW generates a lot of redundant reports
1782 * where the ratio hasn't really changed causing a lot of redundant
1783 * work to processes and increasing the chances we'll hit buffer
1784 * overruns.
1785 *
1786 * Although we don't currently use the 'disable overrun' OABUFFER
1787 * feature it's worth noting that clock ratio reports have to be
1788 * disabled before considering to use that feature since the HW doesn't
1789 * correctly block these reports.
1790 *
1791 * Currently none of the high-level metrics we have depend on knowing
1792 * this ratio to normalize.
1793 *
1794 * Note: This register is not power context saved and restored, but
1795 * that's OK considering that we disable RC6 while the OA unit is
1796 * enabled.
1797 *
1798 * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
1799 * be read back from automatically triggered reports, as part of the
1800 * RPT_ID field.
1801 */
Lionel Landwerlin6c5c1d82017-06-13 12:23:08 +01001802 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv) ||
Lionel Landwerlin28c7ef92017-06-13 12:23:09 +01001803 IS_KABYLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) {
Robert Bragg19f81df2017-06-13 12:23:03 +01001804 I915_WRITE(GEN8_OA_DEBUG,
1805 _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
1806 GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
1807 }
1808
1809 /*
1810 * Update all contexts prior writing the mux configurations as we need
1811 * to make sure all slices/subslices are ON before writing to NOA
1812 * registers.
1813 */
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001814 ret = gen8_configure_all_contexts(dev_priv, oa_config, true);
Robert Bragg19f81df2017-06-13 12:23:03 +01001815 if (ret)
1816 return ret;
1817
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001818 config_oa_regs(dev_priv, oa_config->mux_regs, oa_config->mux_regs_len);
1819
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001820 config_oa_regs(dev_priv, oa_config->b_counter_regs,
1821 oa_config->b_counter_regs_len);
Robert Bragg19f81df2017-06-13 12:23:03 +01001822
1823 return 0;
1824}
1825
1826static void gen8_disable_metric_set(struct drm_i915_private *dev_priv)
1827{
1828 /* Reset all contexts' slices/subslices configurations. */
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001829 gen8_configure_all_contexts(dev_priv, NULL, false);
Lionel Landwerlin28964cf2017-08-03 17:58:10 +01001830
1831 I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
1832 ~GT_NOA_ENABLE));
1833
Robert Bragg19f81df2017-06-13 12:23:03 +01001834}
1835
Robert Bragg1bef3402017-06-13 12:23:06 +01001836static void gen7_oa_enable(struct drm_i915_private *dev_priv)
Robert Braggd7965152016-11-07 19:49:52 +00001837{
Robert Bragg1bef3402017-06-13 12:23:06 +01001838 /*
1839 * Reset buf pointers so we don't forward reports from before now.
1840 *
1841 * Think carefully if considering trying to avoid this, since it
1842 * also ensures status flags and the buffer itself are cleared
1843 * in error paths, and we have checks for invalid reports based
1844 * on the assumption that certain fields are written to zeroed
1845 * memory which this helps maintains.
1846 */
1847 gen7_init_oa_buffer(dev_priv);
Robert Braggd7965152016-11-07 19:49:52 +00001848
1849 if (dev_priv->perf.oa.exclusive_stream->enabled) {
1850 struct i915_gem_context *ctx =
1851 dev_priv->perf.oa.exclusive_stream->ctx;
1852 u32 ctx_id = dev_priv->perf.oa.specific_ctx_id;
1853
1854 bool periodic = dev_priv->perf.oa.periodic;
1855 u32 period_exponent = dev_priv->perf.oa.period_exponent;
1856 u32 report_format = dev_priv->perf.oa.oa_buffer.format;
1857
1858 I915_WRITE(GEN7_OACONTROL,
1859 (ctx_id & GEN7_OACONTROL_CTX_MASK) |
1860 (period_exponent <<
1861 GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
1862 (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
1863 (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
1864 (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
1865 GEN7_OACONTROL_ENABLE);
1866 } else
1867 I915_WRITE(GEN7_OACONTROL, 0);
1868}
1869
Robert Bragg19f81df2017-06-13 12:23:03 +01001870static void gen8_oa_enable(struct drm_i915_private *dev_priv)
1871{
1872 u32 report_format = dev_priv->perf.oa.oa_buffer.format;
1873
1874 /*
1875 * Reset buf pointers so we don't forward reports from before now.
1876 *
1877 * Think carefully if considering trying to avoid this, since it
1878 * also ensures status flags and the buffer itself are cleared
1879 * in error paths, and we have checks for invalid reports based
1880 * on the assumption that certain fields are written to zeroed
1881 * memory which this helps maintains.
1882 */
1883 gen8_init_oa_buffer(dev_priv);
1884
1885 /*
1886 * Note: we don't rely on the hardware to perform single context
1887 * filtering and instead filter on the cpu based on the context-id
1888 * field of reports
1889 */
1890 I915_WRITE(GEN8_OACONTROL, (report_format <<
1891 GEN8_OA_REPORT_FORMAT_SHIFT) |
1892 GEN8_OA_COUNTER_ENABLE);
1893}
1894
Robert Bragg16d98b32016-12-07 21:40:33 +00001895/**
1896 * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
1897 * @stream: An i915 perf stream opened for OA metrics
1898 *
1899 * [Re]enables hardware periodic sampling according to the period configured
1900 * when opening the stream. This also starts a hrtimer that will periodically
1901 * check for data in the circular OA buffer for notifying userspace (e.g.
1902 * during a read() or poll()).
1903 */
Robert Braggd7965152016-11-07 19:49:52 +00001904static void i915_oa_stream_enable(struct i915_perf_stream *stream)
1905{
1906 struct drm_i915_private *dev_priv = stream->dev_priv;
1907
1908 dev_priv->perf.oa.ops.oa_enable(dev_priv);
1909
1910 if (dev_priv->perf.oa.periodic)
1911 hrtimer_start(&dev_priv->perf.oa.poll_check_timer,
1912 ns_to_ktime(POLL_PERIOD),
1913 HRTIMER_MODE_REL_PINNED);
1914}
1915
1916static void gen7_oa_disable(struct drm_i915_private *dev_priv)
1917{
1918 I915_WRITE(GEN7_OACONTROL, 0);
1919}
1920
Robert Bragg19f81df2017-06-13 12:23:03 +01001921static void gen8_oa_disable(struct drm_i915_private *dev_priv)
1922{
1923 I915_WRITE(GEN8_OACONTROL, 0);
1924}
1925
Robert Bragg16d98b32016-12-07 21:40:33 +00001926/**
1927 * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
1928 * @stream: An i915 perf stream opened for OA metrics
1929 *
1930 * Stops the OA unit from periodically writing counter reports into the
1931 * circular OA buffer. This also stops the hrtimer that periodically checks for
1932 * data in the circular OA buffer, for notifying userspace.
1933 */
Robert Braggd7965152016-11-07 19:49:52 +00001934static void i915_oa_stream_disable(struct i915_perf_stream *stream)
1935{
1936 struct drm_i915_private *dev_priv = stream->dev_priv;
1937
1938 dev_priv->perf.oa.ops.oa_disable(dev_priv);
1939
1940 if (dev_priv->perf.oa.periodic)
1941 hrtimer_cancel(&dev_priv->perf.oa.poll_check_timer);
1942}
1943
Robert Braggd7965152016-11-07 19:49:52 +00001944static const struct i915_perf_stream_ops i915_oa_stream_ops = {
1945 .destroy = i915_oa_stream_destroy,
1946 .enable = i915_oa_stream_enable,
1947 .disable = i915_oa_stream_disable,
1948 .wait_unlocked = i915_oa_wait_unlocked,
1949 .poll_wait = i915_oa_poll_wait,
1950 .read = i915_oa_read,
1951};
1952
Lionel Landwerlin701f8232017-08-03 17:58:08 +01001953static struct i915_oa_config *get_oa_config(struct drm_i915_private *dev_priv,
1954 int metrics_set)
1955{
1956 if (metrics_set == 1)
1957 return &dev_priv->perf.oa.test_config;
1958
1959 return NULL;
1960}
1961
Robert Bragg16d98b32016-12-07 21:40:33 +00001962/**
1963 * i915_oa_stream_init - validate combined props for OA stream and init
1964 * @stream: An i915 perf stream
1965 * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
1966 * @props: The property state that configures stream (individually validated)
1967 *
1968 * While read_properties_unlocked() validates properties in isolation it
1969 * doesn't ensure that the combination necessarily makes sense.
1970 *
1971 * At this point it has been determined that userspace wants a stream of
1972 * OA metrics, but still we need to further validate the combined
1973 * properties are OK.
1974 *
1975 * If the configuration makes sense then we can allocate memory for
1976 * a circular OA buffer and apply the requested metric set configuration.
1977 *
1978 * Returns: zero on success or a negative error code.
1979 */
Robert Braggd7965152016-11-07 19:49:52 +00001980static int i915_oa_stream_init(struct i915_perf_stream *stream,
1981 struct drm_i915_perf_open_param *param,
1982 struct perf_open_properties *props)
1983{
1984 struct drm_i915_private *dev_priv = stream->dev_priv;
1985 int format_size;
1986 int ret;
1987
Robert Bragg442b8c02016-11-07 19:49:53 +00001988 /* If the sysfs metrics/ directory wasn't registered for some
1989 * reason then don't let userspace try their luck with config
1990 * IDs
1991 */
1992 if (!dev_priv->perf.metrics_kobj) {
Robert Bragg77085502016-12-01 17:21:52 +00001993 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
Robert Bragg442b8c02016-11-07 19:49:53 +00001994 return -EINVAL;
1995 }
1996
Robert Braggd7965152016-11-07 19:49:52 +00001997 if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
Robert Bragg77085502016-12-01 17:21:52 +00001998 DRM_DEBUG("Only OA report sampling supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00001999 return -EINVAL;
2000 }
2001
2002 if (!dev_priv->perf.oa.ops.init_oa_buffer) {
Robert Bragg77085502016-12-01 17:21:52 +00002003 DRM_DEBUG("OA unit not supported\n");
Robert Braggd7965152016-11-07 19:49:52 +00002004 return -ENODEV;
2005 }
2006
2007 /* To avoid the complexity of having to accurately filter
2008 * counter reports and marshal to the appropriate client
2009 * we currently only allow exclusive access
2010 */
2011 if (dev_priv->perf.oa.exclusive_stream) {
Robert Bragg77085502016-12-01 17:21:52 +00002012 DRM_DEBUG("OA unit already in use\n");
Robert Braggd7965152016-11-07 19:49:52 +00002013 return -EBUSY;
2014 }
2015
Robert Braggd7965152016-11-07 19:49:52 +00002016 if (!props->oa_format) {
Robert Bragg77085502016-12-01 17:21:52 +00002017 DRM_DEBUG("OA report format not specified\n");
Robert Braggd7965152016-11-07 19:49:52 +00002018 return -EINVAL;
2019 }
2020
Robert Bragg712122e2017-05-11 16:43:31 +01002021 /* We set up some ratelimit state to potentially throttle any _NOTES
2022 * about spurious, invalid OA reports which we don't forward to
2023 * userspace.
2024 *
2025 * The initialization is associated with opening the stream (not driver
2026 * init) considering we print a _NOTE about any throttling when closing
2027 * the stream instead of waiting until driver _fini which no one would
2028 * ever see.
2029 *
2030 * Using the same limiting factors as printk_ratelimit()
2031 */
2032 ratelimit_state_init(&dev_priv->perf.oa.spurious_report_rs,
2033 5 * HZ, 10);
2034 /* Since we use a DRM_NOTE for spurious reports it would be
2035 * inconsistent to let __ratelimit() automatically print a warning for
2036 * throttling.
2037 */
2038 ratelimit_set_flags(&dev_priv->perf.oa.spurious_report_rs,
2039 RATELIMIT_MSG_ON_RELEASE);
2040
Robert Braggd7965152016-11-07 19:49:52 +00002041 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
2042
2043 format_size = dev_priv->perf.oa.oa_formats[props->oa_format].size;
2044
2045 stream->sample_flags |= SAMPLE_OA_REPORT;
2046 stream->sample_size += format_size;
2047
2048 dev_priv->perf.oa.oa_buffer.format_size = format_size;
2049 if (WARN_ON(dev_priv->perf.oa.oa_buffer.format_size == 0))
2050 return -EINVAL;
2051
2052 dev_priv->perf.oa.oa_buffer.format =
2053 dev_priv->perf.oa.oa_formats[props->oa_format].format;
2054
Robert Braggd7965152016-11-07 19:49:52 +00002055 dev_priv->perf.oa.periodic = props->oa_periodic;
Robert Bragg0dd860c2017-05-11 16:43:28 +01002056 if (dev_priv->perf.oa.periodic)
Robert Braggd7965152016-11-07 19:49:52 +00002057 dev_priv->perf.oa.period_exponent = props->oa_period_exponent;
2058
Robert Braggd7965152016-11-07 19:49:52 +00002059 if (stream->ctx) {
2060 ret = oa_get_render_ctx_id(stream);
2061 if (ret)
2062 return ret;
2063 }
2064
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002065 stream->oa_config = get_oa_config(dev_priv, props->metrics_set);
2066 if (!stream->oa_config)
2067 return -EINVAL;
2068
Robert Braggd7965152016-11-07 19:49:52 +00002069 /* PRM - observability performance counters:
2070 *
2071 * OACONTROL, performance counter enable, note:
2072 *
2073 * "When this bit is set, in order to have coherent counts,
2074 * RC6 power state and trunk clock gating must be disabled.
2075 * This can be achieved by programming MMIO registers as
2076 * 0xA094=0 and 0xA090[31]=1"
2077 *
2078 * In our case we are expecting that taking pm + FORCEWAKE
2079 * references will effectively disable RC6.
2080 */
2081 intel_runtime_pm_get(dev_priv);
2082 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2083
sagar.a.kamble@intel.com987f8c42017-06-27 23:09:41 +05302084 ret = alloc_oa_buffer(dev_priv);
2085 if (ret)
2086 goto err_oa_buf_alloc;
2087
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002088 ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv,
2089 stream->oa_config);
Robert Braggd7965152016-11-07 19:49:52 +00002090 if (ret)
2091 goto err_enable;
2092
2093 stream->ops = &i915_oa_stream_ops;
2094
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002095 /* Lock device for exclusive_stream access late because
2096 * enable_metric_set() might lock as well on gen8+.
2097 */
2098 ret = i915_mutex_lock_interruptible(&dev_priv->drm);
2099 if (ret)
2100 goto err_lock;
2101
Robert Braggd7965152016-11-07 19:49:52 +00002102 dev_priv->perf.oa.exclusive_stream = stream;
2103
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002104 mutex_unlock(&dev_priv->drm.struct_mutex);
2105
Robert Braggd7965152016-11-07 19:49:52 +00002106 return 0;
2107
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002108err_lock:
2109 dev_priv->perf.oa.ops.disable_metric_set(dev_priv);
2110
Robert Braggd7965152016-11-07 19:49:52 +00002111err_enable:
Robert Braggd7965152016-11-07 19:49:52 +00002112 free_oa_buffer(dev_priv);
2113
2114err_oa_buf_alloc:
sagar.a.kamble@intel.com987f8c42017-06-27 23:09:41 +05302115 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
2116 intel_runtime_pm_put(dev_priv);
Robert Braggd7965152016-11-07 19:49:52 +00002117 if (stream->ctx)
2118 oa_put_render_ctx_id(stream);
2119
2120 return ret;
2121}
2122
Robert Bragg19f81df2017-06-13 12:23:03 +01002123void i915_oa_init_reg_state(struct intel_engine_cs *engine,
2124 struct i915_gem_context *ctx,
2125 u32 *reg_state)
2126{
2127 struct drm_i915_private *dev_priv = engine->i915;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002128 struct i915_perf_stream *stream = dev_priv->perf.oa.exclusive_stream;
Robert Bragg19f81df2017-06-13 12:23:03 +01002129
2130 if (engine->id != RCS)
2131 return;
2132
2133 if (!dev_priv->perf.initialized)
2134 return;
2135
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002136 if (stream)
2137 gen8_update_reg_state_unlocked(ctx, reg_state, stream->oa_config);
Robert Bragg19f81df2017-06-13 12:23:03 +01002138}
2139
Robert Bragg16d98b32016-12-07 21:40:33 +00002140/**
2141 * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation
2142 * @stream: An i915 perf stream
2143 * @file: An i915 perf stream file
2144 * @buf: destination buffer given by userspace
2145 * @count: the number of bytes userspace wants to read
2146 * @ppos: (inout) file seek position (unused)
2147 *
2148 * Besides wrapping &i915_perf_stream_ops->read this provides a common place to
2149 * ensure that if we've successfully copied any data then reporting that takes
2150 * precedence over any internal error status, so the data isn't lost.
2151 *
2152 * For example ret will be -ENOSPC whenever there is more buffered data than
2153 * can be copied to userspace, but that's only interesting if we weren't able
2154 * to copy some data because it implies the userspace buffer is too small to
2155 * receive a single record (and we never split records).
2156 *
2157 * Another case with ret == -EFAULT is more of a grey area since it would seem
2158 * like bad form for userspace to ask us to overrun its buffer, but the user
2159 * knows best:
2160 *
2161 * http://yarchive.net/comp/linux/partial_reads_writes.html
2162 *
2163 * Returns: The number of bytes copied or a negative error code on failure.
2164 */
Robert Braggeec688e2016-11-07 19:49:47 +00002165static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
2166 struct file *file,
2167 char __user *buf,
2168 size_t count,
2169 loff_t *ppos)
2170{
2171 /* Note we keep the offset (aka bytes read) separate from any
2172 * error status so that the final check for whether we return
2173 * the bytes read with a higher precedence than any error (see
2174 * comment below) doesn't need to be handled/duplicated in
2175 * stream->ops->read() implementations.
2176 */
2177 size_t offset = 0;
2178 int ret = stream->ops->read(stream, buf, count, &offset);
2179
Robert Braggeec688e2016-11-07 19:49:47 +00002180 return offset ?: (ret ?: -EAGAIN);
2181}
2182
Robert Bragg16d98b32016-12-07 21:40:33 +00002183/**
2184 * i915_perf_read - handles read() FOP for i915 perf stream FDs
2185 * @file: An i915 perf stream file
2186 * @buf: destination buffer given by userspace
2187 * @count: the number of bytes userspace wants to read
2188 * @ppos: (inout) file seek position (unused)
2189 *
2190 * The entry point for handling a read() on a stream file descriptor from
2191 * userspace. Most of the work is left to the i915_perf_read_locked() and
2192 * &i915_perf_stream_ops->read but to save having stream implementations (of
2193 * which we might have multiple later) we handle blocking read here.
2194 *
2195 * We can also consistently treat trying to read from a disabled stream
2196 * as an IO error so implementations can assume the stream is enabled
2197 * while reading.
2198 *
2199 * Returns: The number of bytes copied or a negative error code on failure.
2200 */
Robert Braggeec688e2016-11-07 19:49:47 +00002201static ssize_t i915_perf_read(struct file *file,
2202 char __user *buf,
2203 size_t count,
2204 loff_t *ppos)
2205{
2206 struct i915_perf_stream *stream = file->private_data;
2207 struct drm_i915_private *dev_priv = stream->dev_priv;
2208 ssize_t ret;
2209
Robert Braggd7965152016-11-07 19:49:52 +00002210 /* To ensure it's handled consistently we simply treat all reads of a
2211 * disabled stream as an error. In particular it might otherwise lead
2212 * to a deadlock for blocking file descriptors...
2213 */
2214 if (!stream->enabled)
2215 return -EIO;
2216
Robert Braggeec688e2016-11-07 19:49:47 +00002217 if (!(file->f_flags & O_NONBLOCK)) {
Robert Braggd7965152016-11-07 19:49:52 +00002218 /* There's the small chance of false positives from
2219 * stream->ops->wait_unlocked.
2220 *
2221 * E.g. with single context filtering since we only wait until
2222 * oabuffer has >= 1 report we don't immediately know whether
2223 * any reports really belong to the current context
Robert Braggeec688e2016-11-07 19:49:47 +00002224 */
2225 do {
2226 ret = stream->ops->wait_unlocked(stream);
2227 if (ret)
2228 return ret;
2229
2230 mutex_lock(&dev_priv->perf.lock);
2231 ret = i915_perf_read_locked(stream, file,
2232 buf, count, ppos);
2233 mutex_unlock(&dev_priv->perf.lock);
2234 } while (ret == -EAGAIN);
2235 } else {
2236 mutex_lock(&dev_priv->perf.lock);
2237 ret = i915_perf_read_locked(stream, file, buf, count, ppos);
2238 mutex_unlock(&dev_priv->perf.lock);
2239 }
2240
Robert Bragg26ebd9c2017-05-11 16:43:25 +01002241 /* We allow the poll checking to sometimes report false positive POLLIN
2242 * events where we might actually report EAGAIN on read() if there's
2243 * not really any data available. In this situation though we don't
2244 * want to enter a busy loop between poll() reporting a POLLIN event
2245 * and read() returning -EAGAIN. Clearing the oa.pollin state here
2246 * effectively ensures we back off until the next hrtimer callback
2247 * before reporting another POLLIN event.
2248 */
2249 if (ret >= 0 || ret == -EAGAIN) {
Robert Braggd7965152016-11-07 19:49:52 +00002250 /* Maybe make ->pollin per-stream state if we support multiple
2251 * concurrent streams in the future.
2252 */
2253 dev_priv->perf.oa.pollin = false;
2254 }
2255
Robert Braggeec688e2016-11-07 19:49:47 +00002256 return ret;
2257}
2258
Robert Braggd7965152016-11-07 19:49:52 +00002259static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
2260{
2261 struct drm_i915_private *dev_priv =
2262 container_of(hrtimer, typeof(*dev_priv),
2263 perf.oa.poll_check_timer);
2264
Robert Bragg19f81df2017-06-13 12:23:03 +01002265 if (oa_buffer_check_unlocked(dev_priv)) {
Robert Braggd7965152016-11-07 19:49:52 +00002266 dev_priv->perf.oa.pollin = true;
2267 wake_up(&dev_priv->perf.oa.poll_wq);
2268 }
2269
2270 hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
2271
2272 return HRTIMER_RESTART;
2273}
2274
Robert Bragg16d98b32016-12-07 21:40:33 +00002275/**
2276 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
2277 * @dev_priv: i915 device instance
2278 * @stream: An i915 perf stream
2279 * @file: An i915 perf stream file
2280 * @wait: poll() state table
2281 *
2282 * For handling userspace polling on an i915 perf stream, this calls through to
2283 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
2284 * will be woken for new stream data.
2285 *
2286 * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
2287 * with any non-file-operation driver hooks.
2288 *
2289 * Returns: any poll events that are ready without sleeping
2290 */
Robert Braggd7965152016-11-07 19:49:52 +00002291static unsigned int i915_perf_poll_locked(struct drm_i915_private *dev_priv,
2292 struct i915_perf_stream *stream,
Robert Braggeec688e2016-11-07 19:49:47 +00002293 struct file *file,
2294 poll_table *wait)
2295{
Robert Braggd7965152016-11-07 19:49:52 +00002296 unsigned int events = 0;
Robert Braggeec688e2016-11-07 19:49:47 +00002297
2298 stream->ops->poll_wait(stream, file, wait);
2299
Robert Braggd7965152016-11-07 19:49:52 +00002300 /* Note: we don't explicitly check whether there's something to read
2301 * here since this path may be very hot depending on what else
2302 * userspace is polling, or on the timeout in use. We rely solely on
2303 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
2304 * samples to read.
2305 */
2306 if (dev_priv->perf.oa.pollin)
2307 events |= POLLIN;
Robert Braggeec688e2016-11-07 19:49:47 +00002308
Robert Braggd7965152016-11-07 19:49:52 +00002309 return events;
Robert Braggeec688e2016-11-07 19:49:47 +00002310}
2311
Robert Bragg16d98b32016-12-07 21:40:33 +00002312/**
2313 * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
2314 * @file: An i915 perf stream file
2315 * @wait: poll() state table
2316 *
2317 * For handling userspace polling on an i915 perf stream, this ensures
2318 * poll_wait() gets called with a wait queue that will be woken for new stream
2319 * data.
2320 *
2321 * Note: Implementation deferred to i915_perf_poll_locked()
2322 *
2323 * Returns: any poll events that are ready without sleeping
2324 */
Robert Braggeec688e2016-11-07 19:49:47 +00002325static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
2326{
2327 struct i915_perf_stream *stream = file->private_data;
2328 struct drm_i915_private *dev_priv = stream->dev_priv;
2329 int ret;
2330
2331 mutex_lock(&dev_priv->perf.lock);
Robert Braggd7965152016-11-07 19:49:52 +00002332 ret = i915_perf_poll_locked(dev_priv, stream, file, wait);
Robert Braggeec688e2016-11-07 19:49:47 +00002333 mutex_unlock(&dev_priv->perf.lock);
2334
2335 return ret;
2336}
2337
Robert Bragg16d98b32016-12-07 21:40:33 +00002338/**
2339 * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
2340 * @stream: A disabled i915 perf stream
2341 *
2342 * [Re]enables the associated capture of data for this stream.
2343 *
2344 * If a stream was previously enabled then there's currently no intention
2345 * to provide userspace any guarantee about the preservation of previously
2346 * buffered data.
2347 */
Robert Braggeec688e2016-11-07 19:49:47 +00002348static void i915_perf_enable_locked(struct i915_perf_stream *stream)
2349{
2350 if (stream->enabled)
2351 return;
2352
2353 /* Allow stream->ops->enable() to refer to this */
2354 stream->enabled = true;
2355
2356 if (stream->ops->enable)
2357 stream->ops->enable(stream);
2358}
2359
Robert Bragg16d98b32016-12-07 21:40:33 +00002360/**
2361 * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
2362 * @stream: An enabled i915 perf stream
2363 *
2364 * Disables the associated capture of data for this stream.
2365 *
2366 * The intention is that disabling an re-enabling a stream will ideally be
2367 * cheaper than destroying and re-opening a stream with the same configuration,
2368 * though there are no formal guarantees about what state or buffered data
2369 * must be retained between disabling and re-enabling a stream.
2370 *
2371 * Note: while a stream is disabled it's considered an error for userspace
2372 * to attempt to read from the stream (-EIO).
2373 */
Robert Braggeec688e2016-11-07 19:49:47 +00002374static void i915_perf_disable_locked(struct i915_perf_stream *stream)
2375{
2376 if (!stream->enabled)
2377 return;
2378
2379 /* Allow stream->ops->disable() to refer to this */
2380 stream->enabled = false;
2381
2382 if (stream->ops->disable)
2383 stream->ops->disable(stream);
2384}
2385
Robert Bragg16d98b32016-12-07 21:40:33 +00002386/**
2387 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2388 * @stream: An i915 perf stream
2389 * @cmd: the ioctl request
2390 * @arg: the ioctl data
2391 *
2392 * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
2393 * with any non-file-operation driver hooks.
2394 *
2395 * Returns: zero on success or a negative error code. Returns -EINVAL for
2396 * an unknown ioctl request.
2397 */
Robert Braggeec688e2016-11-07 19:49:47 +00002398static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
2399 unsigned int cmd,
2400 unsigned long arg)
2401{
2402 switch (cmd) {
2403 case I915_PERF_IOCTL_ENABLE:
2404 i915_perf_enable_locked(stream);
2405 return 0;
2406 case I915_PERF_IOCTL_DISABLE:
2407 i915_perf_disable_locked(stream);
2408 return 0;
2409 }
2410
2411 return -EINVAL;
2412}
2413
Robert Bragg16d98b32016-12-07 21:40:33 +00002414/**
2415 * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
2416 * @file: An i915 perf stream file
2417 * @cmd: the ioctl request
2418 * @arg: the ioctl data
2419 *
2420 * Implementation deferred to i915_perf_ioctl_locked().
2421 *
2422 * Returns: zero on success or a negative error code. Returns -EINVAL for
2423 * an unknown ioctl request.
2424 */
Robert Braggeec688e2016-11-07 19:49:47 +00002425static long i915_perf_ioctl(struct file *file,
2426 unsigned int cmd,
2427 unsigned long arg)
2428{
2429 struct i915_perf_stream *stream = file->private_data;
2430 struct drm_i915_private *dev_priv = stream->dev_priv;
2431 long ret;
2432
2433 mutex_lock(&dev_priv->perf.lock);
2434 ret = i915_perf_ioctl_locked(stream, cmd, arg);
2435 mutex_unlock(&dev_priv->perf.lock);
2436
2437 return ret;
2438}
2439
Robert Bragg16d98b32016-12-07 21:40:33 +00002440/**
2441 * i915_perf_destroy_locked - destroy an i915 perf stream
2442 * @stream: An i915 perf stream
2443 *
2444 * Frees all resources associated with the given i915 perf @stream, disabling
2445 * any associated data capture in the process.
2446 *
2447 * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
2448 * with any non-file-operation driver hooks.
2449 */
Robert Braggeec688e2016-11-07 19:49:47 +00002450static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
2451{
Robert Braggeec688e2016-11-07 19:49:47 +00002452 if (stream->enabled)
2453 i915_perf_disable_locked(stream);
2454
2455 if (stream->ops->destroy)
2456 stream->ops->destroy(stream);
2457
2458 list_del(&stream->link);
2459
Chris Wilson69df05e2016-12-18 15:37:21 +00002460 if (stream->ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01002461 i915_gem_context_put(stream->ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002462
2463 kfree(stream);
2464}
2465
Robert Bragg16d98b32016-12-07 21:40:33 +00002466/**
2467 * i915_perf_release - handles userspace close() of a stream file
2468 * @inode: anonymous inode associated with file
2469 * @file: An i915 perf stream file
2470 *
2471 * Cleans up any resources associated with an open i915 perf stream file.
2472 *
2473 * NB: close() can't really fail from the userspace point of view.
2474 *
2475 * Returns: zero on success or a negative error code.
2476 */
Robert Braggeec688e2016-11-07 19:49:47 +00002477static int i915_perf_release(struct inode *inode, struct file *file)
2478{
2479 struct i915_perf_stream *stream = file->private_data;
2480 struct drm_i915_private *dev_priv = stream->dev_priv;
2481
2482 mutex_lock(&dev_priv->perf.lock);
2483 i915_perf_destroy_locked(stream);
2484 mutex_unlock(&dev_priv->perf.lock);
2485
2486 return 0;
2487}
2488
2489
2490static const struct file_operations fops = {
2491 .owner = THIS_MODULE,
2492 .llseek = no_llseek,
2493 .release = i915_perf_release,
2494 .poll = i915_perf_poll,
2495 .read = i915_perf_read,
2496 .unlocked_ioctl = i915_perf_ioctl,
2497};
2498
2499
Robert Bragg16d98b32016-12-07 21:40:33 +00002500/**
2501 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
2502 * @dev_priv: i915 device instance
2503 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
2504 * @props: individually validated u64 property value pairs
2505 * @file: drm file
2506 *
2507 * See i915_perf_ioctl_open() for interface details.
2508 *
2509 * Implements further stream config validation and stream initialization on
2510 * behalf of i915_perf_open_ioctl() with the &drm_i915_private->perf.lock mutex
2511 * taken to serialize with any non-file-operation driver hooks.
2512 *
2513 * Note: at this point the @props have only been validated in isolation and
2514 * it's still necessary to validate that the combination of properties makes
2515 * sense.
2516 *
2517 * In the case where userspace is interested in OA unit metrics then further
2518 * config validation and stream initialization details will be handled by
2519 * i915_oa_stream_init(). The code here should only validate config state that
2520 * will be relevant to all stream types / backends.
2521 *
2522 * Returns: zero on success or a negative error code.
2523 */
Robert Braggeec688e2016-11-07 19:49:47 +00002524static int
2525i915_perf_open_ioctl_locked(struct drm_i915_private *dev_priv,
2526 struct drm_i915_perf_open_param *param,
2527 struct perf_open_properties *props,
2528 struct drm_file *file)
2529{
2530 struct i915_gem_context *specific_ctx = NULL;
2531 struct i915_perf_stream *stream = NULL;
2532 unsigned long f_flags = 0;
Robert Bragg19f81df2017-06-13 12:23:03 +01002533 bool privileged_op = true;
Robert Braggeec688e2016-11-07 19:49:47 +00002534 int stream_fd;
2535 int ret;
2536
2537 if (props->single_context) {
2538 u32 ctx_handle = props->ctx_handle;
2539 struct drm_i915_file_private *file_priv = file->driver_priv;
2540
Imre Deak635f56c2017-07-14 18:12:41 +03002541 specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle);
2542 if (!specific_ctx) {
2543 DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n",
2544 ctx_handle);
2545 ret = -ENOENT;
Robert Braggeec688e2016-11-07 19:49:47 +00002546 goto err;
2547 }
2548 }
2549
Robert Bragg19f81df2017-06-13 12:23:03 +01002550 /*
2551 * On Haswell the OA unit supports clock gating off for a specific
2552 * context and in this mode there's no visibility of metrics for the
2553 * rest of the system, which we consider acceptable for a
2554 * non-privileged client.
2555 *
2556 * For Gen8+ the OA unit no longer supports clock gating off for a
2557 * specific context and the kernel can't securely stop the counters
2558 * from updating as system-wide / global values. Even though we can
2559 * filter reports based on the included context ID we can't block
2560 * clients from seeing the raw / global counter values via
2561 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
2562 * enable the OA unit by default.
2563 */
2564 if (IS_HASWELL(dev_priv) && specific_ctx)
2565 privileged_op = false;
2566
Robert Braggccdf6342016-11-07 19:49:54 +00002567 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
2568 * we check a dev.i915.perf_stream_paranoid sysctl option
2569 * to determine if it's ok to access system wide OA counters
2570 * without CAP_SYS_ADMIN privileges.
2571 */
Robert Bragg19f81df2017-06-13 12:23:03 +01002572 if (privileged_op &&
Robert Braggccdf6342016-11-07 19:49:54 +00002573 i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002574 DRM_DEBUG("Insufficient privileges to open system-wide i915 perf stream\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002575 ret = -EACCES;
2576 goto err_ctx;
2577 }
2578
2579 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2580 if (!stream) {
2581 ret = -ENOMEM;
2582 goto err_ctx;
2583 }
2584
Robert Braggeec688e2016-11-07 19:49:47 +00002585 stream->dev_priv = dev_priv;
2586 stream->ctx = specific_ctx;
2587
Robert Braggd7965152016-11-07 19:49:52 +00002588 ret = i915_oa_stream_init(stream, param, props);
2589 if (ret)
2590 goto err_alloc;
2591
2592 /* we avoid simply assigning stream->sample_flags = props->sample_flags
2593 * to have _stream_init check the combination of sample flags more
2594 * thoroughly, but still this is the expected result at this point.
Robert Braggeec688e2016-11-07 19:49:47 +00002595 */
Robert Braggd7965152016-11-07 19:49:52 +00002596 if (WARN_ON(stream->sample_flags != props->sample_flags)) {
2597 ret = -ENODEV;
Matthew Auld22f880c2017-03-27 21:34:59 +01002598 goto err_flags;
Robert Braggd7965152016-11-07 19:49:52 +00002599 }
Robert Braggeec688e2016-11-07 19:49:47 +00002600
2601 list_add(&stream->link, &dev_priv->perf.streams);
2602
2603 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
2604 f_flags |= O_CLOEXEC;
2605 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
2606 f_flags |= O_NONBLOCK;
2607
2608 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
2609 if (stream_fd < 0) {
2610 ret = stream_fd;
2611 goto err_open;
2612 }
2613
2614 if (!(param->flags & I915_PERF_FLAG_DISABLED))
2615 i915_perf_enable_locked(stream);
2616
2617 return stream_fd;
2618
2619err_open:
2620 list_del(&stream->link);
Matthew Auld22f880c2017-03-27 21:34:59 +01002621err_flags:
Robert Braggeec688e2016-11-07 19:49:47 +00002622 if (stream->ops->destroy)
2623 stream->ops->destroy(stream);
2624err_alloc:
2625 kfree(stream);
2626err_ctx:
Chris Wilson69df05e2016-12-18 15:37:21 +00002627 if (specific_ctx)
Chris Wilson5f09a9c2017-06-20 12:05:46 +01002628 i915_gem_context_put(specific_ctx);
Robert Braggeec688e2016-11-07 19:49:47 +00002629err:
2630 return ret;
2631}
2632
Robert Bragg155e9412017-06-13 12:23:05 +01002633static u64 oa_exponent_to_ns(struct drm_i915_private *dev_priv, int exponent)
2634{
2635 return div_u64(1000000000ULL * (2ULL << exponent),
2636 dev_priv->perf.oa.timestamp_frequency);
2637}
2638
Robert Bragg16d98b32016-12-07 21:40:33 +00002639/**
2640 * read_properties_unlocked - validate + copy userspace stream open properties
2641 * @dev_priv: i915 device instance
2642 * @uprops: The array of u64 key value pairs given by userspace
2643 * @n_props: The number of key value pairs expected in @uprops
2644 * @props: The stream configuration built up while validating properties
Robert Braggeec688e2016-11-07 19:49:47 +00002645 *
2646 * Note this function only validates properties in isolation it doesn't
2647 * validate that the combination of properties makes sense or that all
2648 * properties necessary for a particular kind of stream have been set.
Robert Bragg16d98b32016-12-07 21:40:33 +00002649 *
2650 * Note that there currently aren't any ordering requirements for properties so
2651 * we shouldn't validate or assume anything about ordering here. This doesn't
2652 * rule out defining new properties with ordering requirements in the future.
Robert Braggeec688e2016-11-07 19:49:47 +00002653 */
2654static int read_properties_unlocked(struct drm_i915_private *dev_priv,
2655 u64 __user *uprops,
2656 u32 n_props,
2657 struct perf_open_properties *props)
2658{
2659 u64 __user *uprop = uprops;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002660 u32 i;
Robert Braggeec688e2016-11-07 19:49:47 +00002661
2662 memset(props, 0, sizeof(struct perf_open_properties));
2663
2664 if (!n_props) {
Robert Bragg77085502016-12-01 17:21:52 +00002665 DRM_DEBUG("No i915 perf properties given\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002666 return -EINVAL;
2667 }
2668
2669 /* Considering that ID = 0 is reserved and assuming that we don't
2670 * (currently) expect any configurations to ever specify duplicate
2671 * values for a particular property ID then the last _PROP_MAX value is
2672 * one greater than the maximum number of properties we expect to get
2673 * from userspace.
2674 */
2675 if (n_props >= DRM_I915_PERF_PROP_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002676 DRM_DEBUG("More i915 perf properties specified than exist\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002677 return -EINVAL;
2678 }
2679
2680 for (i = 0; i < n_props; i++) {
Robert Bragg00319ba2016-11-07 19:49:55 +00002681 u64 oa_period, oa_freq_hz;
Robert Braggeec688e2016-11-07 19:49:47 +00002682 u64 id, value;
2683 int ret;
2684
2685 ret = get_user(id, uprop);
2686 if (ret)
2687 return ret;
2688
2689 ret = get_user(value, uprop + 1);
2690 if (ret)
2691 return ret;
2692
Matthew Auld0a309f92017-03-27 21:32:36 +01002693 if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
2694 DRM_DEBUG("Unknown i915 perf property ID\n");
2695 return -EINVAL;
2696 }
2697
Robert Braggeec688e2016-11-07 19:49:47 +00002698 switch ((enum drm_i915_perf_property_id)id) {
2699 case DRM_I915_PERF_PROP_CTX_HANDLE:
2700 props->single_context = 1;
2701 props->ctx_handle = value;
2702 break;
Robert Braggd7965152016-11-07 19:49:52 +00002703 case DRM_I915_PERF_PROP_SAMPLE_OA:
2704 props->sample_flags |= SAMPLE_OA_REPORT;
2705 break;
2706 case DRM_I915_PERF_PROP_OA_METRICS_SET:
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002707 if (value == 0) {
Robert Bragg77085502016-12-01 17:21:52 +00002708 DRM_DEBUG("Unknown OA metric set ID\n");
Robert Braggd7965152016-11-07 19:49:52 +00002709 return -EINVAL;
2710 }
2711 props->metrics_set = value;
2712 break;
2713 case DRM_I915_PERF_PROP_OA_FORMAT:
2714 if (value == 0 || value >= I915_OA_FORMAT_MAX) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002715 DRM_DEBUG("Out-of-range OA report format %llu\n",
2716 value);
Robert Braggd7965152016-11-07 19:49:52 +00002717 return -EINVAL;
2718 }
2719 if (!dev_priv->perf.oa.oa_formats[value].size) {
Robert Bragg52c57c22017-05-11 16:43:29 +01002720 DRM_DEBUG("Unsupported OA report format %llu\n",
2721 value);
Robert Braggd7965152016-11-07 19:49:52 +00002722 return -EINVAL;
2723 }
2724 props->oa_format = value;
2725 break;
2726 case DRM_I915_PERF_PROP_OA_EXPONENT:
2727 if (value > OA_EXPONENT_MAX) {
Robert Bragg77085502016-12-01 17:21:52 +00002728 DRM_DEBUG("OA timer exponent too high (> %u)\n",
2729 OA_EXPONENT_MAX);
Robert Braggd7965152016-11-07 19:49:52 +00002730 return -EINVAL;
2731 }
2732
Robert Bragg00319ba2016-11-07 19:49:55 +00002733 /* Theoretically we can program the OA unit to sample
Robert Bragg155e9412017-06-13 12:23:05 +01002734 * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
2735 * for BXT. We don't allow such high sampling
2736 * frequencies by default unless root.
Robert Braggd7965152016-11-07 19:49:52 +00002737 */
Robert Bragg155e9412017-06-13 12:23:05 +01002738
Robert Bragg00319ba2016-11-07 19:49:55 +00002739 BUILD_BUG_ON(sizeof(oa_period) != 8);
Robert Bragg155e9412017-06-13 12:23:05 +01002740 oa_period = oa_exponent_to_ns(dev_priv, value);
Robert Bragg00319ba2016-11-07 19:49:55 +00002741
2742 /* This check is primarily to ensure that oa_period <=
2743 * UINT32_MAX (before passing to do_div which only
2744 * accepts a u32 denominator), but we can also skip
2745 * checking anything < 1Hz which implicitly can't be
2746 * limited via an integer oa_max_sample_rate.
2747 */
2748 if (oa_period <= NSEC_PER_SEC) {
2749 u64 tmp = NSEC_PER_SEC;
2750 do_div(tmp, oa_period);
2751 oa_freq_hz = tmp;
2752 } else
2753 oa_freq_hz = 0;
2754
2755 if (oa_freq_hz > i915_oa_max_sample_rate &&
2756 !capable(CAP_SYS_ADMIN)) {
Robert Bragg77085502016-12-01 17:21:52 +00002757 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 +00002758 i915_oa_max_sample_rate);
Robert Braggd7965152016-11-07 19:49:52 +00002759 return -EACCES;
2760 }
2761
2762 props->oa_periodic = true;
2763 props->oa_period_exponent = value;
2764 break;
Matthew Auld0a309f92017-03-27 21:32:36 +01002765 case DRM_I915_PERF_PROP_MAX:
Robert Braggeec688e2016-11-07 19:49:47 +00002766 MISSING_CASE(id);
Robert Braggeec688e2016-11-07 19:49:47 +00002767 return -EINVAL;
2768 }
2769
2770 uprop += 2;
2771 }
2772
2773 return 0;
2774}
2775
Robert Bragg16d98b32016-12-07 21:40:33 +00002776/**
2777 * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
2778 * @dev: drm device
2779 * @data: ioctl data copied from userspace (unvalidated)
2780 * @file: drm file
2781 *
2782 * Validates the stream open parameters given by userspace including flags
2783 * and an array of u64 key, value pair properties.
2784 *
2785 * Very little is assumed up front about the nature of the stream being
2786 * opened (for instance we don't assume it's for periodic OA unit metrics). An
2787 * i915-perf stream is expected to be a suitable interface for other forms of
2788 * buffered data written by the GPU besides periodic OA metrics.
2789 *
2790 * Note we copy the properties from userspace outside of the i915 perf
2791 * mutex to avoid an awkward lockdep with mmap_sem.
2792 *
2793 * Most of the implementation details are handled by
2794 * i915_perf_open_ioctl_locked() after taking the &drm_i915_private->perf.lock
2795 * mutex for serializing with any non-file-operation driver hooks.
2796 *
2797 * Return: A newly opened i915 Perf stream file descriptor or negative
2798 * error code on failure.
2799 */
Robert Braggeec688e2016-11-07 19:49:47 +00002800int i915_perf_open_ioctl(struct drm_device *dev, void *data,
2801 struct drm_file *file)
2802{
2803 struct drm_i915_private *dev_priv = dev->dev_private;
2804 struct drm_i915_perf_open_param *param = data;
2805 struct perf_open_properties props;
2806 u32 known_open_flags;
2807 int ret;
2808
2809 if (!dev_priv->perf.initialized) {
Robert Bragg77085502016-12-01 17:21:52 +00002810 DRM_DEBUG("i915 perf interface not available for this system\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002811 return -ENOTSUPP;
2812 }
2813
2814 known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
2815 I915_PERF_FLAG_FD_NONBLOCK |
2816 I915_PERF_FLAG_DISABLED;
2817 if (param->flags & ~known_open_flags) {
Robert Bragg77085502016-12-01 17:21:52 +00002818 DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n");
Robert Braggeec688e2016-11-07 19:49:47 +00002819 return -EINVAL;
2820 }
2821
2822 ret = read_properties_unlocked(dev_priv,
2823 u64_to_user_ptr(param->properties_ptr),
2824 param->num_properties,
2825 &props);
2826 if (ret)
2827 return ret;
2828
2829 mutex_lock(&dev_priv->perf.lock);
2830 ret = i915_perf_open_ioctl_locked(dev_priv, param, &props, file);
2831 mutex_unlock(&dev_priv->perf.lock);
2832
2833 return ret;
2834}
2835
Robert Bragg16d98b32016-12-07 21:40:33 +00002836/**
2837 * i915_perf_register - exposes i915-perf to userspace
2838 * @dev_priv: i915 device instance
2839 *
2840 * In particular OA metric sets are advertised under a sysfs metrics/
2841 * directory allowing userspace to enumerate valid IDs that can be
2842 * used to open an i915-perf stream.
2843 */
Robert Bragg442b8c02016-11-07 19:49:53 +00002844void i915_perf_register(struct drm_i915_private *dev_priv)
2845{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002846 int ret;
2847
Robert Bragg442b8c02016-11-07 19:49:53 +00002848 if (!dev_priv->perf.initialized)
2849 return;
2850
2851 /* To be sure we're synchronized with an attempted
2852 * i915_perf_open_ioctl(); considering that we register after
2853 * being exposed to userspace.
2854 */
2855 mutex_lock(&dev_priv->perf.lock);
2856
2857 dev_priv->perf.metrics_kobj =
2858 kobject_create_and_add("metrics",
2859 &dev_priv->drm.primary->kdev->kobj);
2860 if (!dev_priv->perf.metrics_kobj)
2861 goto exit;
2862
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002863 memset(&dev_priv->perf.oa.test_config, 0,
2864 sizeof(dev_priv->perf.oa.test_config));
2865
Robert Bragg19f81df2017-06-13 12:23:03 +01002866 if (IS_HASWELL(dev_priv)) {
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002867 i915_perf_load_test_config_hsw(dev_priv);
Robert Bragg19f81df2017-06-13 12:23:03 +01002868 } else if (IS_BROADWELL(dev_priv)) {
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002869 i915_perf_load_test_config_bdw(dev_priv);
Robert Bragg19f81df2017-06-13 12:23:03 +01002870 } else if (IS_CHERRYVIEW(dev_priv)) {
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002871 i915_perf_load_test_config_chv(dev_priv);
Robert Bragg19f81df2017-06-13 12:23:03 +01002872 } else if (IS_SKYLAKE(dev_priv)) {
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002873 if (IS_SKL_GT2(dev_priv))
2874 i915_perf_load_test_config_sklgt2(dev_priv);
2875 else if (IS_SKL_GT3(dev_priv))
2876 i915_perf_load_test_config_sklgt3(dev_priv);
2877 else if (IS_SKL_GT4(dev_priv))
2878 i915_perf_load_test_config_sklgt4(dev_priv);
Robert Bragg19f81df2017-06-13 12:23:03 +01002879 } else if (IS_BROXTON(dev_priv)) {
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002880 i915_perf_load_test_config_bxt(dev_priv);
Lionel Landwerlin6c5c1d82017-06-13 12:23:08 +01002881 } else if (IS_KABYLAKE(dev_priv)) {
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002882 if (IS_KBL_GT2(dev_priv))
2883 i915_perf_load_test_config_kblgt2(dev_priv);
2884 else if (IS_KBL_GT3(dev_priv))
2885 i915_perf_load_test_config_kblgt3(dev_priv);
Lionel Landwerlin28c7ef92017-06-13 12:23:09 +01002886 } else if (IS_GEMINILAKE(dev_priv)) {
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002887 i915_perf_load_test_config_glk(dev_priv);
Robert Bragg442b8c02016-11-07 19:49:53 +00002888 }
2889
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002890 if (dev_priv->perf.oa.test_config.id == 0)
2891 goto sysfs_error;
2892
2893 ret = sysfs_create_group(dev_priv->perf.metrics_kobj,
2894 &dev_priv->perf.oa.test_config.sysfs_metric);
2895 if (ret)
2896 goto sysfs_error;
Robert Bragg19f81df2017-06-13 12:23:03 +01002897 goto exit;
2898
2899sysfs_error:
2900 kobject_put(dev_priv->perf.metrics_kobj);
2901 dev_priv->perf.metrics_kobj = NULL;
2902
Robert Bragg442b8c02016-11-07 19:49:53 +00002903exit:
2904 mutex_unlock(&dev_priv->perf.lock);
2905}
2906
Robert Bragg16d98b32016-12-07 21:40:33 +00002907/**
2908 * i915_perf_unregister - hide i915-perf from userspace
2909 * @dev_priv: i915 device instance
2910 *
2911 * i915-perf state cleanup is split up into an 'unregister' and
2912 * 'deinit' phase where the interface is first hidden from
2913 * userspace by i915_perf_unregister() before cleaning up
2914 * remaining state in i915_perf_fini().
2915 */
Robert Bragg442b8c02016-11-07 19:49:53 +00002916void i915_perf_unregister(struct drm_i915_private *dev_priv)
2917{
Robert Bragg442b8c02016-11-07 19:49:53 +00002918 if (!dev_priv->perf.metrics_kobj)
2919 return;
2920
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002921 sysfs_remove_group(dev_priv->perf.metrics_kobj,
2922 &dev_priv->perf.oa.test_config.sysfs_metric);
Robert Bragg442b8c02016-11-07 19:49:53 +00002923
2924 kobject_put(dev_priv->perf.metrics_kobj);
2925 dev_priv->perf.metrics_kobj = NULL;
2926}
2927
Robert Braggccdf6342016-11-07 19:49:54 +00002928static struct ctl_table oa_table[] = {
2929 {
2930 .procname = "perf_stream_paranoid",
2931 .data = &i915_perf_stream_paranoid,
2932 .maxlen = sizeof(i915_perf_stream_paranoid),
2933 .mode = 0644,
2934 .proc_handler = proc_dointvec_minmax,
2935 .extra1 = &zero,
2936 .extra2 = &one,
2937 },
Robert Bragg00319ba2016-11-07 19:49:55 +00002938 {
2939 .procname = "oa_max_sample_rate",
2940 .data = &i915_oa_max_sample_rate,
2941 .maxlen = sizeof(i915_oa_max_sample_rate),
2942 .mode = 0644,
2943 .proc_handler = proc_dointvec_minmax,
2944 .extra1 = &zero,
2945 .extra2 = &oa_sample_rate_hard_limit,
2946 },
Robert Braggccdf6342016-11-07 19:49:54 +00002947 {}
2948};
2949
2950static struct ctl_table i915_root[] = {
2951 {
2952 .procname = "i915",
2953 .maxlen = 0,
2954 .mode = 0555,
2955 .child = oa_table,
2956 },
2957 {}
2958};
2959
2960static struct ctl_table dev_root[] = {
2961 {
2962 .procname = "dev",
2963 .maxlen = 0,
2964 .mode = 0555,
2965 .child = i915_root,
2966 },
2967 {}
2968};
2969
Robert Bragg16d98b32016-12-07 21:40:33 +00002970/**
2971 * i915_perf_init - initialize i915-perf state on module load
2972 * @dev_priv: i915 device instance
2973 *
2974 * Initializes i915-perf state without exposing anything to userspace.
2975 *
2976 * Note: i915-perf initialization is split into an 'init' and 'register'
2977 * phase with the i915_perf_register() exposing state to userspace.
2978 */
Robert Braggeec688e2016-11-07 19:49:47 +00002979void i915_perf_init(struct drm_i915_private *dev_priv)
2980{
Lionel Landwerlin701f8232017-08-03 17:58:08 +01002981 dev_priv->perf.oa.timestamp_frequency = 0;
Robert Braggd7965152016-11-07 19:49:52 +00002982
Robert Bragg19f81df2017-06-13 12:23:03 +01002983 if (IS_HASWELL(dev_priv)) {
2984 dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer;
2985 dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set;
2986 dev_priv->perf.oa.ops.disable_metric_set = hsw_disable_metric_set;
2987 dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable;
2988 dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable;
2989 dev_priv->perf.oa.ops.read = gen7_oa_read;
2990 dev_priv->perf.oa.ops.oa_hw_tail_read =
2991 gen7_oa_hw_tail_read;
Robert Braggd7965152016-11-07 19:49:52 +00002992
Robert Bragg155e9412017-06-13 12:23:05 +01002993 dev_priv->perf.oa.timestamp_frequency = 12500000;
2994
Robert Bragg19f81df2017-06-13 12:23:03 +01002995 dev_priv->perf.oa.oa_formats = hsw_oa_formats;
Robert Bragg19f81df2017-06-13 12:23:03 +01002996 } else if (i915.enable_execlists) {
2997 /* Note: that although we could theoretically also support the
2998 * legacy ringbuffer mode on BDW (and earlier iterations of
2999 * this driver, before upstreaming did this) it didn't seem
3000 * worth the complexity to maintain now that BDW+ enable
3001 * execlist mode by default.
3002 */
Robert Braggd7965152016-11-07 19:49:52 +00003003
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003004 dev_priv->perf.oa.ops.init_oa_buffer = gen8_init_oa_buffer;
3005 dev_priv->perf.oa.ops.enable_metric_set = gen8_enable_metric_set;
3006 dev_priv->perf.oa.ops.disable_metric_set = gen8_disable_metric_set;
3007 dev_priv->perf.oa.ops.oa_enable = gen8_oa_enable;
3008 dev_priv->perf.oa.ops.oa_disable = gen8_oa_disable;
3009 dev_priv->perf.oa.ops.read = gen8_oa_read;
3010 dev_priv->perf.oa.ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
3011
3012 dev_priv->perf.oa.oa_formats = gen8_plus_oa_formats;
3013
Robert Bragg19f81df2017-06-13 12:23:03 +01003014 if (IS_GEN8(dev_priv)) {
3015 dev_priv->perf.oa.ctx_oactxctrl_offset = 0x120;
3016 dev_priv->perf.oa.ctx_flexeu0_offset = 0x2ce;
Robert Bragg155e9412017-06-13 12:23:05 +01003017
3018 dev_priv->perf.oa.timestamp_frequency = 12500000;
3019
Robert Bragg19f81df2017-06-13 12:23:03 +01003020 dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<25);
Robert Bragg19f81df2017-06-13 12:23:03 +01003021 } else if (IS_GEN9(dev_priv)) {
3022 dev_priv->perf.oa.ctx_oactxctrl_offset = 0x128;
3023 dev_priv->perf.oa.ctx_flexeu0_offset = 0x3de;
Robert Bragg155e9412017-06-13 12:23:05 +01003024
Robert Bragg19f81df2017-06-13 12:23:03 +01003025 dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<16);
Robert Braggeec688e2016-11-07 19:49:47 +00003026
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003027 switch (dev_priv->info.platform) {
3028 case INTEL_BROXTON:
3029 case INTEL_GEMINILAKE:
Robert Bragg155e9412017-06-13 12:23:05 +01003030 dev_priv->perf.oa.timestamp_frequency = 19200000;
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003031 break;
3032 case INTEL_SKYLAKE:
3033 case INTEL_KABYLAKE:
3034 dev_priv->perf.oa.timestamp_frequency = 12000000;
3035 break;
3036 default:
3037 /* Leave timestamp_frequency to 0 so we can
3038 * detect unsupported platforms.
3039 */
3040 break;
Robert Bragg19f81df2017-06-13 12:23:03 +01003041 }
3042 }
Robert Bragg19f81df2017-06-13 12:23:03 +01003043 }
3044
Lionel Landwerlin701f8232017-08-03 17:58:08 +01003045 if (dev_priv->perf.oa.timestamp_frequency) {
Robert Bragg19f81df2017-06-13 12:23:03 +01003046 hrtimer_init(&dev_priv->perf.oa.poll_check_timer,
3047 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3048 dev_priv->perf.oa.poll_check_timer.function = oa_poll_check_timer_cb;
3049 init_waitqueue_head(&dev_priv->perf.oa.poll_wq);
3050
3051 INIT_LIST_HEAD(&dev_priv->perf.streams);
3052 mutex_init(&dev_priv->perf.lock);
Robert Bragg19f81df2017-06-13 12:23:03 +01003053 spin_lock_init(&dev_priv->perf.oa.oa_buffer.ptr_lock);
3054
Robert Bragg155e9412017-06-13 12:23:05 +01003055 oa_sample_rate_hard_limit =
3056 dev_priv->perf.oa.timestamp_frequency / 2;
Robert Bragg19f81df2017-06-13 12:23:03 +01003057 dev_priv->perf.sysctl_header = register_sysctl_table(dev_root);
3058
3059 dev_priv->perf.initialized = true;
3060 }
Robert Braggeec688e2016-11-07 19:49:47 +00003061}
3062
Robert Bragg16d98b32016-12-07 21:40:33 +00003063/**
3064 * i915_perf_fini - Counter part to i915_perf_init()
3065 * @dev_priv: i915 device instance
3066 */
Robert Braggeec688e2016-11-07 19:49:47 +00003067void i915_perf_fini(struct drm_i915_private *dev_priv)
3068{
3069 if (!dev_priv->perf.initialized)
3070 return;
3071
Robert Braggccdf6342016-11-07 19:49:54 +00003072 unregister_sysctl_table(dev_priv->perf.sysctl_header);
3073
Robert Braggd7965152016-11-07 19:49:52 +00003074 memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops));
Robert Bragg19f81df2017-06-13 12:23:03 +01003075
Robert Braggeec688e2016-11-07 19:49:47 +00003076 dev_priv->perf.initialized = false;
3077}