]>
Commit | Line | Data |
---|---|---|
eec688e1 RB |
1 | /* |
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 | ||
7abbd8d6 RB |
27 | |
28 | /** | |
16d98b31 | 29 | * DOC: i915 Perf Overview |
7abbd8d6 RB |
30 | * |
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 | * | |
16d98b31 RB |
48 | */ |
49 | ||
50 | /** | |
51 | * DOC: i915 Perf History and Comparison with Core Perf | |
7abbd8d6 RB |
52 | * |
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 | * | |
16d98b31 RB |
82 | * Issues hit with first prototype based on Core Perf |
83 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
7abbd8d6 RB |
84 | * |
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 | * | |
16d98b31 | 142 | * - As a side note on perf's grouping feature; there was also some concern |
7abbd8d6 RB |
143 | * 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 | ||
eec688e1 | 194 | #include <linux/anon_inodes.h> |
d7965152 | 195 | #include <linux/sizes.h> |
f89823c2 | 196 | #include <linux/uuid.h> |
eec688e1 RB |
197 | |
198 | #include "i915_drv.h" | |
d7965152 | 199 | #include "i915_oa_hsw.h" |
19f81df2 RB |
200 | #include "i915_oa_bdw.h" |
201 | #include "i915_oa_chv.h" | |
202 | #include "i915_oa_sklgt2.h" | |
203 | #include "i915_oa_sklgt3.h" | |
204 | #include "i915_oa_sklgt4.h" | |
205 | #include "i915_oa_bxt.h" | |
6c5c1d89 LL |
206 | #include "i915_oa_kblgt2.h" |
207 | #include "i915_oa_kblgt3.h" | |
28c7ef9e | 208 | #include "i915_oa_glk.h" |
d7965152 RB |
209 | |
210 | /* HW requires this to be a power of two, between 128k and 16M, though driver | |
211 | * is currently generally designed assuming the largest 16M size is used such | |
212 | * that the overflow cases are unlikely in normal operation. | |
213 | */ | |
214 | #define OA_BUFFER_SIZE SZ_16M | |
215 | ||
216 | #define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1)) | |
217 | ||
0dd860cf RB |
218 | /** |
219 | * DOC: OA Tail Pointer Race | |
220 | * | |
221 | * There's a HW race condition between OA unit tail pointer register updates and | |
d7965152 | 222 | * writes to memory whereby the tail pointer can sometimes get ahead of what's |
0dd860cf RB |
223 | * been written out to the OA buffer so far (in terms of what's visible to the |
224 | * CPU). | |
225 | * | |
226 | * Although this can be observed explicitly while copying reports to userspace | |
227 | * by checking for a zeroed report-id field in tail reports, we want to account | |
19f81df2 | 228 | * for this earlier, as part of the oa_buffer_check to avoid lots of redundant |
0dd860cf RB |
229 | * read() attempts. |
230 | * | |
231 | * In effect we define a tail pointer for reading that lags the real tail | |
232 | * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough | |
233 | * time for the corresponding reports to become visible to the CPU. | |
234 | * | |
235 | * To manage this we actually track two tail pointers: | |
236 | * 1) An 'aging' tail with an associated timestamp that is tracked until we | |
237 | * can trust the corresponding data is visible to the CPU; at which point | |
238 | * it is considered 'aged'. | |
239 | * 2) An 'aged' tail that can be used for read()ing. | |
d7965152 | 240 | * |
0dd860cf | 241 | * The two separate pointers let us decouple read()s from tail pointer aging. |
d7965152 | 242 | * |
0dd860cf RB |
243 | * The tail pointers are checked and updated at a limited rate within a hrtimer |
244 | * callback (the same callback that is used for delivering POLLIN events) | |
d7965152 | 245 | * |
0dd860cf RB |
246 | * Initially the tails are marked invalid with %INVALID_TAIL_PTR which |
247 | * indicates that an updated tail pointer is needed. | |
248 | * | |
249 | * Most of the implementation details for this workaround are in | |
19f81df2 | 250 | * oa_buffer_check_unlocked() and _append_oa_reports() |
0dd860cf RB |
251 | * |
252 | * Note for posterity: previously the driver used to define an effective tail | |
253 | * pointer that lagged the real pointer by a 'tail margin' measured in bytes | |
254 | * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency. | |
255 | * This was flawed considering that the OA unit may also automatically generate | |
256 | * non-periodic reports (such as on context switch) or the OA unit may be | |
257 | * enabled without any periodic sampling. | |
d7965152 RB |
258 | */ |
259 | #define OA_TAIL_MARGIN_NSEC 100000ULL | |
0dd860cf | 260 | #define INVALID_TAIL_PTR 0xffffffff |
d7965152 RB |
261 | |
262 | /* frequency for checking whether the OA unit has written new reports to the | |
263 | * circular OA buffer... | |
264 | */ | |
265 | #define POLL_FREQUENCY 200 | |
266 | #define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY) | |
267 | ||
ccdf6341 RB |
268 | /* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */ |
269 | static int zero; | |
270 | static int one = 1; | |
271 | static u32 i915_perf_stream_paranoid = true; | |
272 | ||
d7965152 RB |
273 | /* The maximum exponent the hardware accepts is 63 (essentially it selects one |
274 | * of the 64bit timestamp bits to trigger reports from) but there's currently | |
275 | * no known use case for sampling as infrequently as once per 47 thousand years. | |
276 | * | |
277 | * Since the timestamps included in OA reports are only 32bits it seems | |
278 | * reasonable to limit the OA exponent where it's still possible to account for | |
279 | * overflow in OA report timestamps. | |
280 | */ | |
281 | #define OA_EXPONENT_MAX 31 | |
282 | ||
283 | #define INVALID_CTX_ID 0xffffffff | |
284 | ||
19f81df2 RB |
285 | /* On Gen8+ automatically triggered OA reports include a 'reason' field... */ |
286 | #define OAREPORT_REASON_MASK 0x3f | |
287 | #define OAREPORT_REASON_SHIFT 19 | |
288 | #define OAREPORT_REASON_TIMER (1<<0) | |
289 | #define OAREPORT_REASON_CTX_SWITCH (1<<3) | |
290 | #define OAREPORT_REASON_CLK_RATIO (1<<5) | |
291 | ||
d7965152 | 292 | |
00319ba0 RB |
293 | /* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate |
294 | * | |
155e941f RB |
295 | * The highest sampling frequency we can theoretically program the OA unit |
296 | * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell. | |
297 | * | |
298 | * Initialized just before we register the sysctl parameter. | |
00319ba0 | 299 | */ |
155e941f | 300 | static int oa_sample_rate_hard_limit; |
00319ba0 RB |
301 | |
302 | /* Theoretically we can program the OA unit to sample every 160ns but don't | |
303 | * allow that by default unless root... | |
304 | * | |
305 | * The default threshold of 100000Hz is based on perf's similar | |
306 | * kernel.perf_event_max_sample_rate sysctl parameter. | |
307 | */ | |
308 | static u32 i915_oa_max_sample_rate = 100000; | |
309 | ||
d7965152 RB |
310 | /* XXX: beware if future OA HW adds new report formats that the current |
311 | * code assumes all reports have a power-of-two size and ~(size - 1) can | |
312 | * be used as a mask to align the OA tail pointer. | |
313 | */ | |
314 | static struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = { | |
315 | [I915_OA_FORMAT_A13] = { 0, 64 }, | |
316 | [I915_OA_FORMAT_A29] = { 1, 128 }, | |
317 | [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 }, | |
318 | /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */ | |
319 | [I915_OA_FORMAT_B4_C8] = { 4, 64 }, | |
320 | [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 }, | |
321 | [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 }, | |
322 | [I915_OA_FORMAT_C4_B8] = { 7, 64 }, | |
323 | }; | |
324 | ||
19f81df2 RB |
325 | static struct i915_oa_format gen8_plus_oa_formats[I915_OA_FORMAT_MAX] = { |
326 | [I915_OA_FORMAT_A12] = { 0, 64 }, | |
327 | [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 }, | |
328 | [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 }, | |
329 | [I915_OA_FORMAT_C4_B8] = { 7, 64 }, | |
330 | }; | |
331 | ||
d7965152 | 332 | #define SAMPLE_OA_REPORT (1<<0) |
eec688e1 | 333 | |
16d98b31 RB |
334 | /** |
335 | * struct perf_open_properties - for validated properties given to open a stream | |
336 | * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags | |
337 | * @single_context: Whether a single or all gpu contexts should be monitored | |
338 | * @ctx_handle: A gem ctx handle for use with @single_context | |
339 | * @metrics_set: An ID for an OA unit metric set advertised via sysfs | |
340 | * @oa_format: An OA unit HW report format | |
341 | * @oa_periodic: Whether to enable periodic OA unit sampling | |
342 | * @oa_period_exponent: The OA unit sampling period is derived from this | |
343 | * | |
344 | * As read_properties_unlocked() enumerates and validates the properties given | |
345 | * to open a stream of metrics the configuration is built up in the structure | |
346 | * which starts out zero initialized. | |
347 | */ | |
eec688e1 RB |
348 | struct perf_open_properties { |
349 | u32 sample_flags; | |
350 | ||
351 | u64 single_context:1; | |
352 | u64 ctx_handle; | |
d7965152 RB |
353 | |
354 | /* OA sampling state */ | |
355 | int metrics_set; | |
356 | int oa_format; | |
357 | bool oa_periodic; | |
358 | int oa_period_exponent; | |
359 | }; | |
360 | ||
f89823c2 LL |
361 | static void free_oa_config(struct drm_i915_private *dev_priv, |
362 | struct i915_oa_config *oa_config) | |
363 | { | |
364 | if (!PTR_ERR(oa_config->flex_regs)) | |
365 | kfree(oa_config->flex_regs); | |
366 | if (!PTR_ERR(oa_config->b_counter_regs)) | |
367 | kfree(oa_config->b_counter_regs); | |
368 | if (!PTR_ERR(oa_config->mux_regs)) | |
369 | kfree(oa_config->mux_regs); | |
370 | kfree(oa_config); | |
371 | } | |
372 | ||
373 | static void put_oa_config(struct drm_i915_private *dev_priv, | |
374 | struct i915_oa_config *oa_config) | |
375 | { | |
376 | if (!atomic_dec_and_test(&oa_config->ref_count)) | |
377 | return; | |
378 | ||
379 | free_oa_config(dev_priv, oa_config); | |
380 | } | |
381 | ||
382 | static int get_oa_config(struct drm_i915_private *dev_priv, | |
383 | int metrics_set, | |
384 | struct i915_oa_config **out_config) | |
385 | { | |
386 | int ret; | |
387 | ||
388 | if (metrics_set == 1) { | |
389 | *out_config = &dev_priv->perf.oa.test_config; | |
390 | atomic_inc(&dev_priv->perf.oa.test_config.ref_count); | |
391 | return 0; | |
392 | } | |
393 | ||
394 | ret = mutex_lock_interruptible(&dev_priv->perf.metrics_lock); | |
395 | if (ret) | |
396 | return ret; | |
397 | ||
398 | *out_config = idr_find(&dev_priv->perf.metrics_idr, metrics_set); | |
399 | if (!*out_config) | |
400 | ret = -EINVAL; | |
401 | else | |
402 | atomic_inc(&(*out_config)->ref_count); | |
403 | ||
404 | mutex_unlock(&dev_priv->perf.metrics_lock); | |
405 | ||
406 | return ret; | |
407 | } | |
408 | ||
19f81df2 RB |
409 | static u32 gen8_oa_hw_tail_read(struct drm_i915_private *dev_priv) |
410 | { | |
411 | return I915_READ(GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK; | |
412 | } | |
413 | ||
414 | static u32 gen7_oa_hw_tail_read(struct drm_i915_private *dev_priv) | |
415 | { | |
416 | u32 oastatus1 = I915_READ(GEN7_OASTATUS1); | |
417 | ||
418 | return oastatus1 & GEN7_OASTATUS1_TAIL_MASK; | |
419 | } | |
420 | ||
0dd860cf | 421 | /** |
19f81df2 | 422 | * oa_buffer_check_unlocked - check for data and update tail ptr state |
0dd860cf | 423 | * @dev_priv: i915 device instance |
d7965152 | 424 | * |
0dd860cf RB |
425 | * This is either called via fops (for blocking reads in user ctx) or the poll |
426 | * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check | |
427 | * if there is data available for userspace to read. | |
d7965152 | 428 | * |
0dd860cf RB |
429 | * This function is central to providing a workaround for the OA unit tail |
430 | * pointer having a race with respect to what data is visible to the CPU. | |
431 | * It is responsible for reading tail pointers from the hardware and giving | |
432 | * the pointers time to 'age' before they are made available for reading. | |
433 | * (See description of OA_TAIL_MARGIN_NSEC above for further details.) | |
434 | * | |
435 | * Besides returning true when there is data available to read() this function | |
436 | * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp | |
437 | * and .aged_tail_idx state used for reading. | |
438 | * | |
439 | * Note: It's safe to read OA config state here unlocked, assuming that this is | |
440 | * only called while the stream is enabled, while the global OA configuration | |
441 | * can't be modified. | |
442 | * | |
443 | * Returns: %true if the OA buffer contains data, else %false | |
d7965152 | 444 | */ |
19f81df2 | 445 | static bool oa_buffer_check_unlocked(struct drm_i915_private *dev_priv) |
d7965152 RB |
446 | { |
447 | int report_size = dev_priv->perf.oa.oa_buffer.format_size; | |
0dd860cf RB |
448 | unsigned long flags; |
449 | unsigned int aged_idx; | |
0dd860cf RB |
450 | u32 head, hw_tail, aged_tail, aging_tail; |
451 | u64 now; | |
452 | ||
453 | /* We have to consider the (unlikely) possibility that read() errors | |
454 | * could result in an OA buffer reset which might reset the head, | |
455 | * tails[] and aged_tail state. | |
456 | */ | |
457 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
458 | ||
459 | /* NB: The head we observe here might effectively be a little out of | |
460 | * date (between head and tails[aged_idx].offset if there is currently | |
461 | * a read() in progress. | |
462 | */ | |
463 | head = dev_priv->perf.oa.oa_buffer.head; | |
464 | ||
465 | aged_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx; | |
466 | aged_tail = dev_priv->perf.oa.oa_buffer.tails[aged_idx].offset; | |
467 | aging_tail = dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset; | |
468 | ||
19f81df2 | 469 | hw_tail = dev_priv->perf.oa.ops.oa_hw_tail_read(dev_priv); |
0dd860cf RB |
470 | |
471 | /* The tail pointer increases in 64 byte increments, | |
472 | * not in report_size steps... | |
473 | */ | |
474 | hw_tail &= ~(report_size - 1); | |
475 | ||
476 | now = ktime_get_mono_fast_ns(); | |
477 | ||
4117ebc7 RB |
478 | /* Update the aged tail |
479 | * | |
480 | * Flip the tail pointer available for read()s once the aging tail is | |
481 | * old enough to trust that the corresponding data will be visible to | |
482 | * the CPU... | |
483 | * | |
484 | * Do this before updating the aging pointer in case we may be able to | |
485 | * immediately start aging a new pointer too (if new data has become | |
486 | * available) without needing to wait for a later hrtimer callback. | |
487 | */ | |
488 | if (aging_tail != INVALID_TAIL_PTR && | |
489 | ((now - dev_priv->perf.oa.oa_buffer.aging_timestamp) > | |
490 | OA_TAIL_MARGIN_NSEC)) { | |
19f81df2 | 491 | |
4117ebc7 RB |
492 | aged_idx ^= 1; |
493 | dev_priv->perf.oa.oa_buffer.aged_tail_idx = aged_idx; | |
494 | ||
495 | aged_tail = aging_tail; | |
496 | ||
497 | /* Mark that we need a new pointer to start aging... */ | |
498 | dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR; | |
499 | aging_tail = INVALID_TAIL_PTR; | |
500 | } | |
501 | ||
0dd860cf RB |
502 | /* Update the aging tail |
503 | * | |
504 | * We throttle aging tail updates until we have a new tail that | |
505 | * represents >= one report more data than is already available for | |
506 | * reading. This ensures there will be enough data for a successful | |
507 | * read once this new pointer has aged and ensures we will give the new | |
508 | * pointer time to age. | |
509 | */ | |
510 | if (aging_tail == INVALID_TAIL_PTR && | |
511 | (aged_tail == INVALID_TAIL_PTR || | |
512 | OA_TAKEN(hw_tail, aged_tail) >= report_size)) { | |
513 | struct i915_vma *vma = dev_priv->perf.oa.oa_buffer.vma; | |
514 | u32 gtt_offset = i915_ggtt_offset(vma); | |
515 | ||
516 | /* Be paranoid and do a bounds check on the pointer read back | |
517 | * from hardware, just in case some spurious hardware condition | |
518 | * could put the tail out of bounds... | |
519 | */ | |
520 | if (hw_tail >= gtt_offset && | |
521 | hw_tail < (gtt_offset + OA_BUFFER_SIZE)) { | |
522 | dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset = | |
523 | aging_tail = hw_tail; | |
524 | dev_priv->perf.oa.oa_buffer.aging_timestamp = now; | |
525 | } else { | |
526 | DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %u\n", | |
527 | hw_tail); | |
528 | } | |
529 | } | |
530 | ||
0dd860cf RB |
531 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
532 | ||
533 | return aged_tail == INVALID_TAIL_PTR ? | |
534 | false : OA_TAKEN(aged_tail, head) >= report_size; | |
d7965152 RB |
535 | } |
536 | ||
537 | /** | |
16d98b31 RB |
538 | * append_oa_status - Appends a status record to a userspace read() buffer. |
539 | * @stream: An i915-perf stream opened for OA metrics | |
540 | * @buf: destination buffer given by userspace | |
541 | * @count: the number of bytes userspace wants to read | |
542 | * @offset: (inout): the current position for writing into @buf | |
543 | * @type: The kind of status to report to userspace | |
544 | * | |
545 | * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`) | |
546 | * into the userspace read() buffer. | |
547 | * | |
548 | * The @buf @offset will only be updated on success. | |
549 | * | |
550 | * Returns: 0 on success, negative error code on failure. | |
d7965152 RB |
551 | */ |
552 | static int append_oa_status(struct i915_perf_stream *stream, | |
553 | char __user *buf, | |
554 | size_t count, | |
555 | size_t *offset, | |
556 | enum drm_i915_perf_record_type type) | |
557 | { | |
558 | struct drm_i915_perf_record_header header = { type, 0, sizeof(header) }; | |
559 | ||
560 | if ((count - *offset) < header.size) | |
561 | return -ENOSPC; | |
562 | ||
563 | if (copy_to_user(buf + *offset, &header, sizeof(header))) | |
564 | return -EFAULT; | |
565 | ||
566 | (*offset) += header.size; | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
571 | /** | |
16d98b31 RB |
572 | * append_oa_sample - Copies single OA report into userspace read() buffer. |
573 | * @stream: An i915-perf stream opened for OA metrics | |
574 | * @buf: destination buffer given by userspace | |
575 | * @count: the number of bytes userspace wants to read | |
576 | * @offset: (inout): the current position for writing into @buf | |
577 | * @report: A single OA report to (optionally) include as part of the sample | |
578 | * | |
579 | * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*` | |
580 | * properties when opening a stream, tracked as `stream->sample_flags`. This | |
581 | * function copies the requested components of a single sample to the given | |
582 | * read() @buf. | |
583 | * | |
584 | * The @buf @offset will only be updated on success. | |
585 | * | |
586 | * Returns: 0 on success, negative error code on failure. | |
d7965152 RB |
587 | */ |
588 | static int append_oa_sample(struct i915_perf_stream *stream, | |
589 | char __user *buf, | |
590 | size_t count, | |
591 | size_t *offset, | |
592 | const u8 *report) | |
593 | { | |
594 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
595 | int report_size = dev_priv->perf.oa.oa_buffer.format_size; | |
596 | struct drm_i915_perf_record_header header; | |
597 | u32 sample_flags = stream->sample_flags; | |
598 | ||
599 | header.type = DRM_I915_PERF_RECORD_SAMPLE; | |
600 | header.pad = 0; | |
601 | header.size = stream->sample_size; | |
602 | ||
603 | if ((count - *offset) < header.size) | |
604 | return -ENOSPC; | |
605 | ||
606 | buf += *offset; | |
607 | if (copy_to_user(buf, &header, sizeof(header))) | |
608 | return -EFAULT; | |
609 | buf += sizeof(header); | |
610 | ||
611 | if (sample_flags & SAMPLE_OA_REPORT) { | |
612 | if (copy_to_user(buf, report, report_size)) | |
613 | return -EFAULT; | |
614 | } | |
615 | ||
616 | (*offset) += header.size; | |
617 | ||
618 | return 0; | |
619 | } | |
620 | ||
19f81df2 RB |
621 | /** |
622 | * Copies all buffered OA reports into userspace read() buffer. | |
623 | * @stream: An i915-perf stream opened for OA metrics | |
624 | * @buf: destination buffer given by userspace | |
625 | * @count: the number of bytes userspace wants to read | |
626 | * @offset: (inout): the current position for writing into @buf | |
627 | * | |
628 | * Notably any error condition resulting in a short read (-%ENOSPC or | |
629 | * -%EFAULT) will be returned even though one or more records may | |
630 | * have been successfully copied. In this case it's up to the caller | |
631 | * to decide if the error should be squashed before returning to | |
632 | * userspace. | |
633 | * | |
634 | * Note: reports are consumed from the head, and appended to the | |
635 | * tail, so the tail chases the head?... If you think that's mad | |
636 | * and back-to-front you're not alone, but this follows the | |
637 | * Gen PRM naming convention. | |
638 | * | |
639 | * Returns: 0 on success, negative error code on failure. | |
640 | */ | |
641 | static int gen8_append_oa_reports(struct i915_perf_stream *stream, | |
642 | char __user *buf, | |
643 | size_t count, | |
644 | size_t *offset) | |
645 | { | |
646 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
647 | int report_size = dev_priv->perf.oa.oa_buffer.format_size; | |
648 | u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr; | |
649 | u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma); | |
650 | u32 mask = (OA_BUFFER_SIZE - 1); | |
651 | size_t start_offset = *offset; | |
652 | unsigned long flags; | |
653 | unsigned int aged_tail_idx; | |
654 | u32 head, tail; | |
655 | u32 taken; | |
656 | int ret = 0; | |
657 | ||
658 | if (WARN_ON(!stream->enabled)) | |
659 | return -EIO; | |
660 | ||
661 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
662 | ||
663 | head = dev_priv->perf.oa.oa_buffer.head; | |
664 | aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx; | |
665 | tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset; | |
666 | ||
667 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
668 | ||
669 | /* | |
670 | * An invalid tail pointer here means we're still waiting for the poll | |
671 | * hrtimer callback to give us a pointer | |
672 | */ | |
673 | if (tail == INVALID_TAIL_PTR) | |
674 | return -EAGAIN; | |
675 | ||
676 | /* | |
677 | * NB: oa_buffer.head/tail include the gtt_offset which we don't want | |
678 | * while indexing relative to oa_buf_base. | |
679 | */ | |
680 | head -= gtt_offset; | |
681 | tail -= gtt_offset; | |
682 | ||
683 | /* | |
684 | * An out of bounds or misaligned head or tail pointer implies a driver | |
685 | * bug since we validate + align the tail pointers we read from the | |
686 | * hardware and we are in full control of the head pointer which should | |
687 | * only be incremented by multiples of the report size (notably also | |
688 | * all a power of two). | |
689 | */ | |
690 | if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size || | |
691 | tail > OA_BUFFER_SIZE || tail % report_size, | |
692 | "Inconsistent OA buffer pointers: head = %u, tail = %u\n", | |
693 | head, tail)) | |
694 | return -EIO; | |
695 | ||
696 | ||
697 | for (/* none */; | |
698 | (taken = OA_TAKEN(tail, head)); | |
699 | head = (head + report_size) & mask) { | |
700 | u8 *report = oa_buf_base + head; | |
701 | u32 *report32 = (void *)report; | |
702 | u32 ctx_id; | |
703 | u32 reason; | |
704 | ||
705 | /* | |
706 | * All the report sizes factor neatly into the buffer | |
707 | * size so we never expect to see a report split | |
708 | * between the beginning and end of the buffer. | |
709 | * | |
710 | * Given the initial alignment check a misalignment | |
711 | * here would imply a driver bug that would result | |
712 | * in an overrun. | |
713 | */ | |
714 | if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) { | |
715 | DRM_ERROR("Spurious OA head ptr: non-integral report offset\n"); | |
716 | break; | |
717 | } | |
718 | ||
719 | /* | |
720 | * The reason field includes flags identifying what | |
721 | * triggered this specific report (mostly timer | |
722 | * triggered or e.g. due to a context switch). | |
723 | * | |
724 | * This field is never expected to be zero so we can | |
725 | * check that the report isn't invalid before copying | |
726 | * it to userspace... | |
727 | */ | |
728 | reason = ((report32[0] >> OAREPORT_REASON_SHIFT) & | |
729 | OAREPORT_REASON_MASK); | |
730 | if (reason == 0) { | |
731 | if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs)) | |
732 | DRM_NOTE("Skipping spurious, invalid OA report\n"); | |
733 | continue; | |
734 | } | |
735 | ||
736 | /* | |
737 | * XXX: Just keep the lower 21 bits for now since I'm not | |
738 | * entirely sure if the HW touches any of the higher bits in | |
739 | * this field | |
740 | */ | |
741 | ctx_id = report32[2] & 0x1fffff; | |
742 | ||
743 | /* | |
744 | * Squash whatever is in the CTX_ID field if it's marked as | |
745 | * invalid to be sure we avoid false-positive, single-context | |
746 | * filtering below... | |
747 | * | |
748 | * Note: that we don't clear the valid_ctx_bit so userspace can | |
749 | * understand that the ID has been squashed by the kernel. | |
750 | */ | |
751 | if (!(report32[0] & dev_priv->perf.oa.gen8_valid_ctx_bit)) | |
752 | ctx_id = report32[2] = INVALID_CTX_ID; | |
753 | ||
754 | /* | |
755 | * NB: For Gen 8 the OA unit no longer supports clock gating | |
756 | * off for a specific context and the kernel can't securely | |
757 | * stop the counters from updating as system-wide / global | |
758 | * values. | |
759 | * | |
760 | * Automatic reports now include a context ID so reports can be | |
761 | * filtered on the cpu but it's not worth trying to | |
762 | * automatically subtract/hide counter progress for other | |
763 | * contexts while filtering since we can't stop userspace | |
764 | * issuing MI_REPORT_PERF_COUNT commands which would still | |
765 | * provide a side-band view of the real values. | |
766 | * | |
767 | * To allow userspace (such as Mesa/GL_INTEL_performance_query) | |
768 | * to normalize counters for a single filtered context then it | |
769 | * needs be forwarded bookend context-switch reports so that it | |
770 | * can track switches in between MI_REPORT_PERF_COUNT commands | |
771 | * and can itself subtract/ignore the progress of counters | |
772 | * associated with other contexts. Note that the hardware | |
773 | * automatically triggers reports when switching to a new | |
774 | * context which are tagged with the ID of the newly active | |
775 | * context. To avoid the complexity (and likely fragility) of | |
776 | * reading ahead while parsing reports to try and minimize | |
777 | * forwarding redundant context switch reports (i.e. between | |
778 | * other, unrelated contexts) we simply elect to forward them | |
779 | * all. | |
780 | * | |
781 | * We don't rely solely on the reason field to identify context | |
782 | * switches since it's not-uncommon for periodic samples to | |
783 | * identify a switch before any 'context switch' report. | |
784 | */ | |
785 | if (!dev_priv->perf.oa.exclusive_stream->ctx || | |
786 | dev_priv->perf.oa.specific_ctx_id == ctx_id || | |
787 | (dev_priv->perf.oa.oa_buffer.last_ctx_id == | |
788 | dev_priv->perf.oa.specific_ctx_id) || | |
789 | reason & OAREPORT_REASON_CTX_SWITCH) { | |
790 | ||
791 | /* | |
792 | * While filtering for a single context we avoid | |
793 | * leaking the IDs of other contexts. | |
794 | */ | |
795 | if (dev_priv->perf.oa.exclusive_stream->ctx && | |
796 | dev_priv->perf.oa.specific_ctx_id != ctx_id) { | |
797 | report32[2] = INVALID_CTX_ID; | |
798 | } | |
799 | ||
800 | ret = append_oa_sample(stream, buf, count, offset, | |
801 | report); | |
802 | if (ret) | |
803 | break; | |
804 | ||
805 | dev_priv->perf.oa.oa_buffer.last_ctx_id = ctx_id; | |
806 | } | |
807 | ||
808 | /* | |
809 | * The above reason field sanity check is based on | |
810 | * the assumption that the OA buffer is initially | |
811 | * zeroed and we reset the field after copying so the | |
812 | * check is still meaningful once old reports start | |
813 | * being overwritten. | |
814 | */ | |
815 | report32[0] = 0; | |
816 | } | |
817 | ||
818 | if (start_offset != *offset) { | |
819 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
820 | ||
821 | /* | |
822 | * We removed the gtt_offset for the copy loop above, indexing | |
823 | * relative to oa_buf_base so put back here... | |
824 | */ | |
825 | head += gtt_offset; | |
826 | ||
827 | I915_WRITE(GEN8_OAHEADPTR, head & GEN8_OAHEADPTR_MASK); | |
828 | dev_priv->perf.oa.oa_buffer.head = head; | |
829 | ||
830 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
831 | } | |
832 | ||
833 | return ret; | |
834 | } | |
835 | ||
836 | /** | |
837 | * gen8_oa_read - copy status records then buffered OA reports | |
838 | * @stream: An i915-perf stream opened for OA metrics | |
839 | * @buf: destination buffer given by userspace | |
840 | * @count: the number of bytes userspace wants to read | |
841 | * @offset: (inout): the current position for writing into @buf | |
842 | * | |
843 | * Checks OA unit status registers and if necessary appends corresponding | |
844 | * status records for userspace (such as for a buffer full condition) and then | |
845 | * initiate appending any buffered OA reports. | |
846 | * | |
847 | * Updates @offset according to the number of bytes successfully copied into | |
848 | * the userspace buffer. | |
849 | * | |
850 | * NB: some data may be successfully copied to the userspace buffer | |
851 | * even if an error is returned, and this is reflected in the | |
852 | * updated @offset. | |
853 | * | |
854 | * Returns: zero on success or a negative error code | |
855 | */ | |
856 | static int gen8_oa_read(struct i915_perf_stream *stream, | |
857 | char __user *buf, | |
858 | size_t count, | |
859 | size_t *offset) | |
860 | { | |
861 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
862 | u32 oastatus; | |
863 | int ret; | |
864 | ||
865 | if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr)) | |
866 | return -EIO; | |
867 | ||
868 | oastatus = I915_READ(GEN8_OASTATUS); | |
869 | ||
870 | /* | |
871 | * We treat OABUFFER_OVERFLOW as a significant error: | |
872 | * | |
873 | * Although theoretically we could handle this more gracefully | |
874 | * sometimes, some Gens don't correctly suppress certain | |
875 | * automatically triggered reports in this condition and so we | |
876 | * have to assume that old reports are now being trampled | |
877 | * over. | |
878 | * | |
879 | * Considering how we don't currently give userspace control | |
880 | * over the OA buffer size and always configure a large 16MB | |
881 | * buffer, then a buffer overflow does anyway likely indicate | |
882 | * that something has gone quite badly wrong. | |
883 | */ | |
884 | if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) { | |
885 | ret = append_oa_status(stream, buf, count, offset, | |
886 | DRM_I915_PERF_RECORD_OA_BUFFER_LOST); | |
887 | if (ret) | |
888 | return ret; | |
889 | ||
890 | DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n", | |
891 | dev_priv->perf.oa.period_exponent); | |
892 | ||
893 | dev_priv->perf.oa.ops.oa_disable(dev_priv); | |
894 | dev_priv->perf.oa.ops.oa_enable(dev_priv); | |
895 | ||
896 | /* | |
897 | * Note: .oa_enable() is expected to re-init the oabuffer and | |
898 | * reset GEN8_OASTATUS for us | |
899 | */ | |
900 | oastatus = I915_READ(GEN8_OASTATUS); | |
901 | } | |
902 | ||
903 | if (oastatus & GEN8_OASTATUS_REPORT_LOST) { | |
904 | ret = append_oa_status(stream, buf, count, offset, | |
905 | DRM_I915_PERF_RECORD_OA_REPORT_LOST); | |
906 | if (ret) | |
907 | return ret; | |
908 | I915_WRITE(GEN8_OASTATUS, | |
909 | oastatus & ~GEN8_OASTATUS_REPORT_LOST); | |
910 | } | |
911 | ||
912 | return gen8_append_oa_reports(stream, buf, count, offset); | |
913 | } | |
914 | ||
d7965152 RB |
915 | /** |
916 | * Copies all buffered OA reports into userspace read() buffer. | |
917 | * @stream: An i915-perf stream opened for OA metrics | |
918 | * @buf: destination buffer given by userspace | |
919 | * @count: the number of bytes userspace wants to read | |
920 | * @offset: (inout): the current position for writing into @buf | |
d7965152 | 921 | * |
16d98b31 RB |
922 | * Notably any error condition resulting in a short read (-%ENOSPC or |
923 | * -%EFAULT) will be returned even though one or more records may | |
d7965152 RB |
924 | * have been successfully copied. In this case it's up to the caller |
925 | * to decide if the error should be squashed before returning to | |
926 | * userspace. | |
927 | * | |
928 | * Note: reports are consumed from the head, and appended to the | |
e81b3a55 | 929 | * tail, so the tail chases the head?... If you think that's mad |
d7965152 RB |
930 | * and back-to-front you're not alone, but this follows the |
931 | * Gen PRM naming convention. | |
16d98b31 RB |
932 | * |
933 | * Returns: 0 on success, negative error code on failure. | |
d7965152 RB |
934 | */ |
935 | static int gen7_append_oa_reports(struct i915_perf_stream *stream, | |
936 | char __user *buf, | |
937 | size_t count, | |
3bb335c1 | 938 | size_t *offset) |
d7965152 RB |
939 | { |
940 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
941 | int report_size = dev_priv->perf.oa.oa_buffer.format_size; | |
942 | u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr; | |
d7965152 RB |
943 | u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma); |
944 | u32 mask = (OA_BUFFER_SIZE - 1); | |
3bb335c1 | 945 | size_t start_offset = *offset; |
0dd860cf RB |
946 | unsigned long flags; |
947 | unsigned int aged_tail_idx; | |
948 | u32 head, tail; | |
d7965152 RB |
949 | u32 taken; |
950 | int ret = 0; | |
951 | ||
952 | if (WARN_ON(!stream->enabled)) | |
953 | return -EIO; | |
954 | ||
0dd860cf | 955 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
f279020a | 956 | |
0dd860cf RB |
957 | head = dev_priv->perf.oa.oa_buffer.head; |
958 | aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx; | |
959 | tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset; | |
f279020a | 960 | |
0dd860cf | 961 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
d7965152 | 962 | |
0dd860cf RB |
963 | /* An invalid tail pointer here means we're still waiting for the poll |
964 | * hrtimer callback to give us a pointer | |
d7965152 | 965 | */ |
0dd860cf RB |
966 | if (tail == INVALID_TAIL_PTR) |
967 | return -EAGAIN; | |
d7965152 | 968 | |
0dd860cf RB |
969 | /* NB: oa_buffer.head/tail include the gtt_offset which we don't want |
970 | * while indexing relative to oa_buf_base. | |
d7965152 | 971 | */ |
0dd860cf RB |
972 | head -= gtt_offset; |
973 | tail -= gtt_offset; | |
d7965152 | 974 | |
0dd860cf RB |
975 | /* An out of bounds or misaligned head or tail pointer implies a driver |
976 | * bug since we validate + align the tail pointers we read from the | |
977 | * hardware and we are in full control of the head pointer which should | |
978 | * only be incremented by multiples of the report size (notably also | |
979 | * all a power of two). | |
d7965152 | 980 | */ |
0dd860cf RB |
981 | if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size || |
982 | tail > OA_BUFFER_SIZE || tail % report_size, | |
983 | "Inconsistent OA buffer pointers: head = %u, tail = %u\n", | |
984 | head, tail)) | |
985 | return -EIO; | |
d7965152 | 986 | |
d7965152 RB |
987 | |
988 | for (/* none */; | |
989 | (taken = OA_TAKEN(tail, head)); | |
990 | head = (head + report_size) & mask) { | |
991 | u8 *report = oa_buf_base + head; | |
992 | u32 *report32 = (void *)report; | |
993 | ||
994 | /* All the report sizes factor neatly into the buffer | |
995 | * size so we never expect to see a report split | |
996 | * between the beginning and end of the buffer. | |
997 | * | |
998 | * Given the initial alignment check a misalignment | |
999 | * here would imply a driver bug that would result | |
1000 | * in an overrun. | |
1001 | */ | |
1002 | if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) { | |
1003 | DRM_ERROR("Spurious OA head ptr: non-integral report offset\n"); | |
1004 | break; | |
1005 | } | |
1006 | ||
1007 | /* The report-ID field for periodic samples includes | |
1008 | * some undocumented flags related to what triggered | |
1009 | * the report and is never expected to be zero so we | |
1010 | * can check that the report isn't invalid before | |
1011 | * copying it to userspace... | |
1012 | */ | |
1013 | if (report32[0] == 0) { | |
712122ea RB |
1014 | if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs)) |
1015 | DRM_NOTE("Skipping spurious, invalid OA report\n"); | |
d7965152 RB |
1016 | continue; |
1017 | } | |
1018 | ||
1019 | ret = append_oa_sample(stream, buf, count, offset, report); | |
1020 | if (ret) | |
1021 | break; | |
1022 | ||
1023 | /* The above report-id field sanity check is based on | |
1024 | * the assumption that the OA buffer is initially | |
1025 | * zeroed and we reset the field after copying so the | |
1026 | * check is still meaningful once old reports start | |
1027 | * being overwritten. | |
1028 | */ | |
1029 | report32[0] = 0; | |
1030 | } | |
1031 | ||
3bb335c1 | 1032 | if (start_offset != *offset) { |
0dd860cf RB |
1033 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
1034 | ||
3bb335c1 RB |
1035 | /* We removed the gtt_offset for the copy loop above, indexing |
1036 | * relative to oa_buf_base so put back here... | |
1037 | */ | |
1038 | head += gtt_offset; | |
1039 | ||
1040 | I915_WRITE(GEN7_OASTATUS2, | |
1041 | ((head & GEN7_OASTATUS2_HEAD_MASK) | | |
1042 | OA_MEM_SELECT_GGTT)); | |
1043 | dev_priv->perf.oa.oa_buffer.head = head; | |
0dd860cf RB |
1044 | |
1045 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
3bb335c1 | 1046 | } |
d7965152 RB |
1047 | |
1048 | return ret; | |
1049 | } | |
1050 | ||
16d98b31 RB |
1051 | /** |
1052 | * gen7_oa_read - copy status records then buffered OA reports | |
1053 | * @stream: An i915-perf stream opened for OA metrics | |
1054 | * @buf: destination buffer given by userspace | |
1055 | * @count: the number of bytes userspace wants to read | |
1056 | * @offset: (inout): the current position for writing into @buf | |
1057 | * | |
1058 | * Checks Gen 7 specific OA unit status registers and if necessary appends | |
1059 | * corresponding status records for userspace (such as for a buffer full | |
1060 | * condition) and then initiate appending any buffered OA reports. | |
1061 | * | |
1062 | * Updates @offset according to the number of bytes successfully copied into | |
1063 | * the userspace buffer. | |
1064 | * | |
1065 | * Returns: zero on success or a negative error code | |
1066 | */ | |
d7965152 RB |
1067 | static int gen7_oa_read(struct i915_perf_stream *stream, |
1068 | char __user *buf, | |
1069 | size_t count, | |
1070 | size_t *offset) | |
1071 | { | |
1072 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
d7965152 | 1073 | u32 oastatus1; |
d7965152 RB |
1074 | int ret; |
1075 | ||
1076 | if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr)) | |
1077 | return -EIO; | |
1078 | ||
d7965152 RB |
1079 | oastatus1 = I915_READ(GEN7_OASTATUS1); |
1080 | ||
d7965152 RB |
1081 | /* XXX: On Haswell we don't have a safe way to clear oastatus1 |
1082 | * bits while the OA unit is enabled (while the tail pointer | |
1083 | * may be updated asynchronously) so we ignore status bits | |
1084 | * that have already been reported to userspace. | |
1085 | */ | |
1086 | oastatus1 &= ~dev_priv->perf.oa.gen7_latched_oastatus1; | |
1087 | ||
1088 | /* We treat OABUFFER_OVERFLOW as a significant error: | |
1089 | * | |
1090 | * - The status can be interpreted to mean that the buffer is | |
1091 | * currently full (with a higher precedence than OA_TAKEN() | |
1092 | * which will start to report a near-empty buffer after an | |
1093 | * overflow) but it's awkward that we can't clear the status | |
1094 | * on Haswell, so without a reset we won't be able to catch | |
1095 | * the state again. | |
1096 | * | |
1097 | * - Since it also implies the HW has started overwriting old | |
1098 | * reports it may also affect our sanity checks for invalid | |
1099 | * reports when copying to userspace that assume new reports | |
1100 | * are being written to cleared memory. | |
1101 | * | |
1102 | * - In the future we may want to introduce a flight recorder | |
1103 | * mode where the driver will automatically maintain a safe | |
1104 | * guard band between head/tail, avoiding this overflow | |
1105 | * condition, but we avoid the added driver complexity for | |
1106 | * now. | |
1107 | */ | |
1108 | if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) { | |
1109 | ret = append_oa_status(stream, buf, count, offset, | |
1110 | DRM_I915_PERF_RECORD_OA_BUFFER_LOST); | |
1111 | if (ret) | |
1112 | return ret; | |
1113 | ||
19f81df2 RB |
1114 | DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n", |
1115 | dev_priv->perf.oa.period_exponent); | |
d7965152 RB |
1116 | |
1117 | dev_priv->perf.oa.ops.oa_disable(dev_priv); | |
1118 | dev_priv->perf.oa.ops.oa_enable(dev_priv); | |
1119 | ||
d7965152 | 1120 | oastatus1 = I915_READ(GEN7_OASTATUS1); |
d7965152 RB |
1121 | } |
1122 | ||
1123 | if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) { | |
1124 | ret = append_oa_status(stream, buf, count, offset, | |
1125 | DRM_I915_PERF_RECORD_OA_REPORT_LOST); | |
1126 | if (ret) | |
1127 | return ret; | |
1128 | dev_priv->perf.oa.gen7_latched_oastatus1 |= | |
1129 | GEN7_OASTATUS1_REPORT_LOST; | |
1130 | } | |
1131 | ||
3bb335c1 | 1132 | return gen7_append_oa_reports(stream, buf, count, offset); |
d7965152 RB |
1133 | } |
1134 | ||
16d98b31 RB |
1135 | /** |
1136 | * i915_oa_wait_unlocked - handles blocking IO until OA data available | |
1137 | * @stream: An i915-perf stream opened for OA metrics | |
1138 | * | |
1139 | * Called when userspace tries to read() from a blocking stream FD opened | |
1140 | * for OA metrics. It waits until the hrtimer callback finds a non-empty | |
1141 | * OA buffer and wakes us. | |
1142 | * | |
1143 | * Note: it's acceptable to have this return with some false positives | |
1144 | * since any subsequent read handling will return -EAGAIN if there isn't | |
1145 | * really data ready for userspace yet. | |
1146 | * | |
1147 | * Returns: zero on success or a negative error code | |
1148 | */ | |
d7965152 RB |
1149 | static int i915_oa_wait_unlocked(struct i915_perf_stream *stream) |
1150 | { | |
1151 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
1152 | ||
1153 | /* We would wait indefinitely if periodic sampling is not enabled */ | |
1154 | if (!dev_priv->perf.oa.periodic) | |
1155 | return -EIO; | |
1156 | ||
d7965152 | 1157 | return wait_event_interruptible(dev_priv->perf.oa.poll_wq, |
19f81df2 | 1158 | oa_buffer_check_unlocked(dev_priv)); |
d7965152 RB |
1159 | } |
1160 | ||
16d98b31 RB |
1161 | /** |
1162 | * i915_oa_poll_wait - call poll_wait() for an OA stream poll() | |
1163 | * @stream: An i915-perf stream opened for OA metrics | |
1164 | * @file: An i915 perf stream file | |
1165 | * @wait: poll() state table | |
1166 | * | |
1167 | * For handling userspace polling on an i915 perf stream opened for OA metrics, | |
1168 | * this starts a poll_wait with the wait queue that our hrtimer callback wakes | |
1169 | * when it sees data ready to read in the circular OA buffer. | |
1170 | */ | |
d7965152 RB |
1171 | static void i915_oa_poll_wait(struct i915_perf_stream *stream, |
1172 | struct file *file, | |
1173 | poll_table *wait) | |
1174 | { | |
1175 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
1176 | ||
1177 | poll_wait(file, &dev_priv->perf.oa.poll_wq, wait); | |
1178 | } | |
1179 | ||
16d98b31 RB |
1180 | /** |
1181 | * i915_oa_read - just calls through to &i915_oa_ops->read | |
1182 | * @stream: An i915-perf stream opened for OA metrics | |
1183 | * @buf: destination buffer given by userspace | |
1184 | * @count: the number of bytes userspace wants to read | |
1185 | * @offset: (inout): the current position for writing into @buf | |
1186 | * | |
1187 | * Updates @offset according to the number of bytes successfully copied into | |
1188 | * the userspace buffer. | |
1189 | * | |
1190 | * Returns: zero on success or a negative error code | |
1191 | */ | |
d7965152 RB |
1192 | static int i915_oa_read(struct i915_perf_stream *stream, |
1193 | char __user *buf, | |
1194 | size_t count, | |
1195 | size_t *offset) | |
1196 | { | |
1197 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
1198 | ||
1199 | return dev_priv->perf.oa.ops.read(stream, buf, count, offset); | |
1200 | } | |
1201 | ||
16d98b31 RB |
1202 | /** |
1203 | * oa_get_render_ctx_id - determine and hold ctx hw id | |
1204 | * @stream: An i915-perf stream opened for OA metrics | |
1205 | * | |
1206 | * Determine the render context hw id, and ensure it remains fixed for the | |
d7965152 RB |
1207 | * lifetime of the stream. This ensures that we don't have to worry about |
1208 | * updating the context ID in OACONTROL on the fly. | |
16d98b31 RB |
1209 | * |
1210 | * Returns: zero on success or a negative error code | |
d7965152 RB |
1211 | */ |
1212 | static int oa_get_render_ctx_id(struct i915_perf_stream *stream) | |
1213 | { | |
1214 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
d7965152 | 1215 | |
19f81df2 RB |
1216 | if (i915.enable_execlists) |
1217 | dev_priv->perf.oa.specific_ctx_id = stream->ctx->hw_id; | |
1218 | else { | |
1219 | struct intel_engine_cs *engine = dev_priv->engine[RCS]; | |
1220 | struct intel_ring *ring; | |
1221 | int ret; | |
d7965152 | 1222 | |
19f81df2 RB |
1223 | ret = i915_mutex_lock_interruptible(&dev_priv->drm); |
1224 | if (ret) | |
1225 | return ret; | |
1226 | ||
1227 | /* | |
1228 | * As the ID is the gtt offset of the context's vma we | |
1229 | * pin the vma to ensure the ID remains fixed. | |
1230 | * | |
1231 | * NB: implied RCS engine... | |
1232 | */ | |
1233 | ring = engine->context_pin(engine, stream->ctx); | |
1234 | mutex_unlock(&dev_priv->drm.struct_mutex); | |
1235 | if (IS_ERR(ring)) | |
1236 | return PTR_ERR(ring); | |
d7965152 | 1237 | |
19f81df2 RB |
1238 | |
1239 | /* | |
1240 | * Explicitly track the ID (instead of calling | |
1241 | * i915_ggtt_offset() on the fly) considering the difference | |
1242 | * with gen8+ and execlists | |
1243 | */ | |
1244 | dev_priv->perf.oa.specific_ctx_id = | |
1245 | i915_ggtt_offset(stream->ctx->engine[engine->id].state); | |
1246 | } | |
d7965152 | 1247 | |
266a240b | 1248 | return 0; |
d7965152 RB |
1249 | } |
1250 | ||
16d98b31 RB |
1251 | /** |
1252 | * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold | |
1253 | * @stream: An i915-perf stream opened for OA metrics | |
1254 | * | |
1255 | * In case anything needed doing to ensure the context HW ID would remain valid | |
1256 | * for the lifetime of the stream, then that can be undone here. | |
1257 | */ | |
d7965152 RB |
1258 | static void oa_put_render_ctx_id(struct i915_perf_stream *stream) |
1259 | { | |
1260 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
1261 | ||
19f81df2 RB |
1262 | if (i915.enable_execlists) { |
1263 | dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID; | |
1264 | } else { | |
1265 | struct intel_engine_cs *engine = dev_priv->engine[RCS]; | |
d7965152 | 1266 | |
19f81df2 | 1267 | mutex_lock(&dev_priv->drm.struct_mutex); |
d7965152 | 1268 | |
19f81df2 RB |
1269 | dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID; |
1270 | engine->context_unpin(engine, stream->ctx); | |
1271 | ||
1272 | mutex_unlock(&dev_priv->drm.struct_mutex); | |
1273 | } | |
d7965152 RB |
1274 | } |
1275 | ||
1276 | static void | |
1277 | free_oa_buffer(struct drm_i915_private *i915) | |
1278 | { | |
1279 | mutex_lock(&i915->drm.struct_mutex); | |
1280 | ||
1281 | i915_gem_object_unpin_map(i915->perf.oa.oa_buffer.vma->obj); | |
1282 | i915_vma_unpin(i915->perf.oa.oa_buffer.vma); | |
1283 | i915_gem_object_put(i915->perf.oa.oa_buffer.vma->obj); | |
1284 | ||
1285 | i915->perf.oa.oa_buffer.vma = NULL; | |
1286 | i915->perf.oa.oa_buffer.vaddr = NULL; | |
1287 | ||
1288 | mutex_unlock(&i915->drm.struct_mutex); | |
1289 | } | |
1290 | ||
1291 | static void i915_oa_stream_destroy(struct i915_perf_stream *stream) | |
1292 | { | |
1293 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
1294 | ||
1295 | BUG_ON(stream != dev_priv->perf.oa.exclusive_stream); | |
1296 | ||
19f81df2 | 1297 | /* |
f89823c2 LL |
1298 | * Unset exclusive_stream first, it will be checked while disabling |
1299 | * the metric set on gen8+. | |
19f81df2 | 1300 | */ |
701f8231 | 1301 | mutex_lock(&dev_priv->drm.struct_mutex); |
19f81df2 | 1302 | dev_priv->perf.oa.exclusive_stream = NULL; |
701f8231 | 1303 | mutex_unlock(&dev_priv->drm.struct_mutex); |
19f81df2 | 1304 | |
d7965152 RB |
1305 | dev_priv->perf.oa.ops.disable_metric_set(dev_priv); |
1306 | ||
1307 | free_oa_buffer(dev_priv); | |
1308 | ||
1309 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); | |
1310 | intel_runtime_pm_put(dev_priv); | |
1311 | ||
1312 | if (stream->ctx) | |
1313 | oa_put_render_ctx_id(stream); | |
1314 | ||
f89823c2 LL |
1315 | put_oa_config(dev_priv, stream->oa_config); |
1316 | ||
712122ea RB |
1317 | if (dev_priv->perf.oa.spurious_report_rs.missed) { |
1318 | DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n", | |
1319 | dev_priv->perf.oa.spurious_report_rs.missed); | |
1320 | } | |
d7965152 RB |
1321 | } |
1322 | ||
1323 | static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv) | |
1324 | { | |
1325 | u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma); | |
0dd860cf RB |
1326 | unsigned long flags; |
1327 | ||
1328 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
d7965152 RB |
1329 | |
1330 | /* Pre-DevBDW: OABUFFER must be set with counters off, | |
1331 | * before OASTATUS1, but after OASTATUS2 | |
1332 | */ | |
1333 | I915_WRITE(GEN7_OASTATUS2, gtt_offset | OA_MEM_SELECT_GGTT); /* head */ | |
f279020a RB |
1334 | dev_priv->perf.oa.oa_buffer.head = gtt_offset; |
1335 | ||
d7965152 | 1336 | I915_WRITE(GEN7_OABUFFER, gtt_offset); |
f279020a | 1337 | |
d7965152 RB |
1338 | I915_WRITE(GEN7_OASTATUS1, gtt_offset | OABUFFER_SIZE_16M); /* tail */ |
1339 | ||
0dd860cf RB |
1340 | /* Mark that we need updated tail pointers to read from... */ |
1341 | dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR; | |
1342 | dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR; | |
1343 | ||
1344 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
1345 | ||
d7965152 RB |
1346 | /* On Haswell we have to track which OASTATUS1 flags we've |
1347 | * already seen since they can't be cleared while periodic | |
1348 | * sampling is enabled. | |
1349 | */ | |
1350 | dev_priv->perf.oa.gen7_latched_oastatus1 = 0; | |
1351 | ||
1352 | /* NB: although the OA buffer will initially be allocated | |
1353 | * zeroed via shmfs (and so this memset is redundant when | |
1354 | * first allocating), we may re-init the OA buffer, either | |
1355 | * when re-enabling a stream or in error/reset paths. | |
1356 | * | |
1357 | * The reason we clear the buffer for each re-init is for the | |
1358 | * sanity check in gen7_append_oa_reports() that looks at the | |
1359 | * report-id field to make sure it's non-zero which relies on | |
1360 | * the assumption that new reports are being written to zeroed | |
1361 | * memory... | |
1362 | */ | |
1363 | memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE); | |
1364 | ||
1365 | /* Maybe make ->pollin per-stream state if we support multiple | |
1366 | * concurrent streams in the future. | |
1367 | */ | |
1368 | dev_priv->perf.oa.pollin = false; | |
1369 | } | |
1370 | ||
19f81df2 RB |
1371 | static void gen8_init_oa_buffer(struct drm_i915_private *dev_priv) |
1372 | { | |
1373 | u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma); | |
1374 | unsigned long flags; | |
1375 | ||
1376 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
1377 | ||
1378 | I915_WRITE(GEN8_OASTATUS, 0); | |
1379 | I915_WRITE(GEN8_OAHEADPTR, gtt_offset); | |
1380 | dev_priv->perf.oa.oa_buffer.head = gtt_offset; | |
1381 | ||
1382 | I915_WRITE(GEN8_OABUFFER_UDW, 0); | |
1383 | ||
1384 | /* | |
1385 | * PRM says: | |
1386 | * | |
1387 | * "This MMIO must be set before the OATAILPTR | |
1388 | * register and after the OAHEADPTR register. This is | |
1389 | * to enable proper functionality of the overflow | |
1390 | * bit." | |
1391 | */ | |
1392 | I915_WRITE(GEN8_OABUFFER, gtt_offset | | |
1393 | OABUFFER_SIZE_16M | OA_MEM_SELECT_GGTT); | |
1394 | I915_WRITE(GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK); | |
1395 | ||
1396 | /* Mark that we need updated tail pointers to read from... */ | |
1397 | dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR; | |
1398 | dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR; | |
1399 | ||
1400 | /* | |
1401 | * Reset state used to recognise context switches, affecting which | |
1402 | * reports we will forward to userspace while filtering for a single | |
1403 | * context. | |
1404 | */ | |
1405 | dev_priv->perf.oa.oa_buffer.last_ctx_id = INVALID_CTX_ID; | |
1406 | ||
1407 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); | |
1408 | ||
1409 | /* | |
1410 | * NB: although the OA buffer will initially be allocated | |
1411 | * zeroed via shmfs (and so this memset is redundant when | |
1412 | * first allocating), we may re-init the OA buffer, either | |
1413 | * when re-enabling a stream or in error/reset paths. | |
1414 | * | |
1415 | * The reason we clear the buffer for each re-init is for the | |
1416 | * sanity check in gen8_append_oa_reports() that looks at the | |
1417 | * reason field to make sure it's non-zero which relies on | |
1418 | * the assumption that new reports are being written to zeroed | |
1419 | * memory... | |
1420 | */ | |
1421 | memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE); | |
1422 | ||
1423 | /* | |
1424 | * Maybe make ->pollin per-stream state if we support multiple | |
1425 | * concurrent streams in the future. | |
1426 | */ | |
1427 | dev_priv->perf.oa.pollin = false; | |
1428 | } | |
1429 | ||
d7965152 RB |
1430 | static int alloc_oa_buffer(struct drm_i915_private *dev_priv) |
1431 | { | |
1432 | struct drm_i915_gem_object *bo; | |
1433 | struct i915_vma *vma; | |
1434 | int ret; | |
1435 | ||
1436 | if (WARN_ON(dev_priv->perf.oa.oa_buffer.vma)) | |
1437 | return -ENODEV; | |
1438 | ||
1439 | ret = i915_mutex_lock_interruptible(&dev_priv->drm); | |
1440 | if (ret) | |
1441 | return ret; | |
1442 | ||
1443 | BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE); | |
1444 | BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M); | |
1445 | ||
12d79d78 | 1446 | bo = i915_gem_object_create(dev_priv, OA_BUFFER_SIZE); |
d7965152 RB |
1447 | if (IS_ERR(bo)) { |
1448 | DRM_ERROR("Failed to allocate OA buffer\n"); | |
1449 | ret = PTR_ERR(bo); | |
1450 | goto unlock; | |
1451 | } | |
1452 | ||
1453 | ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC); | |
1454 | if (ret) | |
1455 | goto err_unref; | |
1456 | ||
1457 | /* PreHSW required 512K alignment, HSW requires 16M */ | |
1458 | vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0); | |
1459 | if (IS_ERR(vma)) { | |
1460 | ret = PTR_ERR(vma); | |
1461 | goto err_unref; | |
1462 | } | |
1463 | dev_priv->perf.oa.oa_buffer.vma = vma; | |
1464 | ||
1465 | dev_priv->perf.oa.oa_buffer.vaddr = | |
1466 | i915_gem_object_pin_map(bo, I915_MAP_WB); | |
1467 | if (IS_ERR(dev_priv->perf.oa.oa_buffer.vaddr)) { | |
1468 | ret = PTR_ERR(dev_priv->perf.oa.oa_buffer.vaddr); | |
1469 | goto err_unpin; | |
1470 | } | |
1471 | ||
1472 | dev_priv->perf.oa.ops.init_oa_buffer(dev_priv); | |
1473 | ||
1474 | DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p\n", | |
1475 | i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma), | |
1476 | dev_priv->perf.oa.oa_buffer.vaddr); | |
1477 | ||
1478 | goto unlock; | |
1479 | ||
1480 | err_unpin: | |
1481 | __i915_vma_unpin(vma); | |
1482 | ||
1483 | err_unref: | |
1484 | i915_gem_object_put(bo); | |
1485 | ||
1486 | dev_priv->perf.oa.oa_buffer.vaddr = NULL; | |
1487 | dev_priv->perf.oa.oa_buffer.vma = NULL; | |
1488 | ||
1489 | unlock: | |
1490 | mutex_unlock(&dev_priv->drm.struct_mutex); | |
1491 | return ret; | |
1492 | } | |
1493 | ||
1494 | static void config_oa_regs(struct drm_i915_private *dev_priv, | |
1495 | const struct i915_oa_reg *regs, | |
701f8231 | 1496 | u32 n_regs) |
d7965152 | 1497 | { |
701f8231 | 1498 | u32 i; |
d7965152 RB |
1499 | |
1500 | for (i = 0; i < n_regs; i++) { | |
1501 | const struct i915_oa_reg *reg = regs + i; | |
1502 | ||
1503 | I915_WRITE(reg->addr, reg->value); | |
1504 | } | |
1505 | } | |
1506 | ||
701f8231 LL |
1507 | static int hsw_enable_metric_set(struct drm_i915_private *dev_priv, |
1508 | const struct i915_oa_config *oa_config) | |
d7965152 | 1509 | { |
d7965152 RB |
1510 | /* PRM: |
1511 | * | |
1512 | * OA unit is using “crclk” for its functionality. When trunk | |
1513 | * level clock gating takes place, OA clock would be gated, | |
1514 | * unable to count the events from non-render clock domain. | |
1515 | * Render clock gating must be disabled when OA is enabled to | |
1516 | * count the events from non-render domain. Unit level clock | |
1517 | * gating for RCS should also be disabled. | |
1518 | */ | |
1519 | I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) & | |
1520 | ~GEN7_DOP_CLOCK_GATE_ENABLE)); | |
1521 | I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) | | |
1522 | GEN6_CSUNIT_CLOCK_GATE_DISABLE)); | |
1523 | ||
701f8231 | 1524 | config_oa_regs(dev_priv, oa_config->mux_regs, oa_config->mux_regs_len); |
d7965152 RB |
1525 | |
1526 | /* It apparently takes a fairly long time for a new MUX | |
1527 | * configuration to be be applied after these register writes. | |
1528 | * This delay duration was derived empirically based on the | |
1529 | * render_basic config but hopefully it covers the maximum | |
1530 | * configuration latency. | |
1531 | * | |
1532 | * As a fallback, the checks in _append_oa_reports() to skip | |
1533 | * invalid OA reports do also seem to work to discard reports | |
1534 | * generated before this config has completed - albeit not | |
1535 | * silently. | |
1536 | * | |
1537 | * Unfortunately this is essentially a magic number, since we | |
1538 | * don't currently know of a reliable mechanism for predicting | |
1539 | * how long the MUX config will take to apply and besides | |
1540 | * seeing invalid reports we don't know of a reliable way to | |
1541 | * explicitly check that the MUX config has landed. | |
1542 | * | |
1543 | * It's even possible we've miss characterized the underlying | |
1544 | * problem - it just seems like the simplest explanation why | |
1545 | * a delay at this location would mitigate any invalid reports. | |
1546 | */ | |
1547 | usleep_range(15000, 20000); | |
1548 | ||
701f8231 LL |
1549 | config_oa_regs(dev_priv, oa_config->b_counter_regs, |
1550 | oa_config->b_counter_regs_len); | |
d7965152 RB |
1551 | |
1552 | return 0; | |
1553 | } | |
1554 | ||
1555 | static void hsw_disable_metric_set(struct drm_i915_private *dev_priv) | |
1556 | { | |
1557 | I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) & | |
1558 | ~GEN6_CSUNIT_CLOCK_GATE_DISABLE)); | |
1559 | I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) | | |
1560 | GEN7_DOP_CLOCK_GATE_ENABLE)); | |
1561 | ||
1562 | I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) & | |
1563 | ~GT_NOA_ENABLE)); | |
1564 | } | |
1565 | ||
19f81df2 RB |
1566 | /* |
1567 | * NB: It must always remain pointer safe to run this even if the OA unit | |
1568 | * has been disabled. | |
1569 | * | |
1570 | * It's fine to put out-of-date values into these per-context registers | |
1571 | * in the case that the OA unit has been disabled. | |
1572 | */ | |
1573 | static void gen8_update_reg_state_unlocked(struct i915_gem_context *ctx, | |
701f8231 LL |
1574 | u32 *reg_state, |
1575 | const struct i915_oa_config *oa_config) | |
19f81df2 RB |
1576 | { |
1577 | struct drm_i915_private *dev_priv = ctx->i915; | |
19f81df2 RB |
1578 | u32 ctx_oactxctrl = dev_priv->perf.oa.ctx_oactxctrl_offset; |
1579 | u32 ctx_flexeu0 = dev_priv->perf.oa.ctx_flexeu0_offset; | |
1580 | /* The MMIO offsets for Flex EU registers aren't contiguous */ | |
1581 | u32 flex_mmio[] = { | |
1582 | i915_mmio_reg_offset(EU_PERF_CNTL0), | |
1583 | i915_mmio_reg_offset(EU_PERF_CNTL1), | |
1584 | i915_mmio_reg_offset(EU_PERF_CNTL2), | |
1585 | i915_mmio_reg_offset(EU_PERF_CNTL3), | |
1586 | i915_mmio_reg_offset(EU_PERF_CNTL4), | |
1587 | i915_mmio_reg_offset(EU_PERF_CNTL5), | |
1588 | i915_mmio_reg_offset(EU_PERF_CNTL6), | |
1589 | }; | |
1590 | int i; | |
1591 | ||
1592 | reg_state[ctx_oactxctrl] = i915_mmio_reg_offset(GEN8_OACTXCONTROL); | |
1593 | reg_state[ctx_oactxctrl+1] = (dev_priv->perf.oa.period_exponent << | |
1594 | GEN8_OA_TIMER_PERIOD_SHIFT) | | |
1595 | (dev_priv->perf.oa.periodic ? | |
1596 | GEN8_OA_TIMER_ENABLE : 0) | | |
1597 | GEN8_OA_COUNTER_RESUME; | |
1598 | ||
1599 | for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) { | |
1600 | u32 state_offset = ctx_flexeu0 + i * 2; | |
1601 | u32 mmio = flex_mmio[i]; | |
1602 | ||
1603 | /* | |
1604 | * This arbitrary default will select the 'EU FPU0 Pipeline | |
1605 | * Active' event. In the future it's anticipated that there | |
1606 | * will be an explicit 'No Event' we can select, but not yet... | |
1607 | */ | |
1608 | u32 value = 0; | |
19f81df2 | 1609 | |
701f8231 LL |
1610 | if (oa_config) { |
1611 | u32 j; | |
1612 | ||
1613 | for (j = 0; j < oa_config->flex_regs_len; j++) { | |
1614 | if (i915_mmio_reg_offset(oa_config->flex_regs[j].addr) == mmio) { | |
1615 | value = oa_config->flex_regs[j].value; | |
1616 | break; | |
1617 | } | |
19f81df2 RB |
1618 | } |
1619 | } | |
1620 | ||
1621 | reg_state[state_offset] = mmio; | |
1622 | reg_state[state_offset+1] = value; | |
1623 | } | |
1624 | } | |
1625 | ||
1626 | /* | |
1627 | * Same as gen8_update_reg_state_unlocked only through the batchbuffer. This | |
1628 | * is only used by the kernel context. | |
1629 | */ | |
701f8231 LL |
1630 | static int gen8_emit_oa_config(struct drm_i915_gem_request *req, |
1631 | const struct i915_oa_config *oa_config) | |
19f81df2 RB |
1632 | { |
1633 | struct drm_i915_private *dev_priv = req->i915; | |
19f81df2 RB |
1634 | /* The MMIO offsets for Flex EU registers aren't contiguous */ |
1635 | u32 flex_mmio[] = { | |
1636 | i915_mmio_reg_offset(EU_PERF_CNTL0), | |
1637 | i915_mmio_reg_offset(EU_PERF_CNTL1), | |
1638 | i915_mmio_reg_offset(EU_PERF_CNTL2), | |
1639 | i915_mmio_reg_offset(EU_PERF_CNTL3), | |
1640 | i915_mmio_reg_offset(EU_PERF_CNTL4), | |
1641 | i915_mmio_reg_offset(EU_PERF_CNTL5), | |
1642 | i915_mmio_reg_offset(EU_PERF_CNTL6), | |
1643 | }; | |
1644 | u32 *cs; | |
1645 | int i; | |
1646 | ||
01d928e9 | 1647 | cs = intel_ring_begin(req, ARRAY_SIZE(flex_mmio) * 2 + 4); |
19f81df2 RB |
1648 | if (IS_ERR(cs)) |
1649 | return PTR_ERR(cs); | |
1650 | ||
01d928e9 | 1651 | *cs++ = MI_LOAD_REGISTER_IMM(ARRAY_SIZE(flex_mmio) + 1); |
19f81df2 RB |
1652 | |
1653 | *cs++ = i915_mmio_reg_offset(GEN8_OACTXCONTROL); | |
1654 | *cs++ = (dev_priv->perf.oa.period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) | | |
1655 | (dev_priv->perf.oa.periodic ? GEN8_OA_TIMER_ENABLE : 0) | | |
1656 | GEN8_OA_COUNTER_RESUME; | |
1657 | ||
1658 | for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) { | |
1659 | u32 mmio = flex_mmio[i]; | |
1660 | ||
1661 | /* | |
1662 | * This arbitrary default will select the 'EU FPU0 Pipeline | |
1663 | * Active' event. In the future it's anticipated that there | |
1664 | * will be an explicit 'No Event' we can select, but not | |
1665 | * yet... | |
1666 | */ | |
1667 | u32 value = 0; | |
19f81df2 | 1668 | |
701f8231 LL |
1669 | if (oa_config) { |
1670 | u32 j; | |
1671 | ||
1672 | for (j = 0; j < oa_config->flex_regs_len; j++) { | |
1673 | if (i915_mmio_reg_offset(oa_config->flex_regs[j].addr) == mmio) { | |
1674 | value = oa_config->flex_regs[j].value; | |
1675 | break; | |
1676 | } | |
19f81df2 RB |
1677 | } |
1678 | } | |
1679 | ||
1680 | *cs++ = mmio; | |
1681 | *cs++ = value; | |
1682 | } | |
1683 | ||
1684 | *cs++ = MI_NOOP; | |
1685 | intel_ring_advance(req, cs); | |
1686 | ||
1687 | return 0; | |
1688 | } | |
1689 | ||
701f8231 LL |
1690 | static int gen8_switch_to_updated_kernel_context(struct drm_i915_private *dev_priv, |
1691 | const struct i915_oa_config *oa_config) | |
19f81df2 RB |
1692 | { |
1693 | struct intel_engine_cs *engine = dev_priv->engine[RCS]; | |
1694 | struct i915_gem_timeline *timeline; | |
1695 | struct drm_i915_gem_request *req; | |
1696 | int ret; | |
1697 | ||
1698 | lockdep_assert_held(&dev_priv->drm.struct_mutex); | |
1699 | ||
1700 | i915_gem_retire_requests(dev_priv); | |
1701 | ||
1702 | req = i915_gem_request_alloc(engine, dev_priv->kernel_context); | |
1703 | if (IS_ERR(req)) | |
1704 | return PTR_ERR(req); | |
1705 | ||
701f8231 | 1706 | ret = gen8_emit_oa_config(req, oa_config); |
19f81df2 RB |
1707 | if (ret) { |
1708 | i915_add_request(req); | |
1709 | return ret; | |
1710 | } | |
1711 | ||
1712 | /* Queue this switch after all other activity */ | |
1713 | list_for_each_entry(timeline, &dev_priv->gt.timelines, link) { | |
1714 | struct drm_i915_gem_request *prev; | |
1715 | struct intel_timeline *tl; | |
1716 | ||
1717 | tl = &timeline->engine[engine->id]; | |
1718 | prev = i915_gem_active_raw(&tl->last_request, | |
1719 | &dev_priv->drm.struct_mutex); | |
1720 | if (prev) | |
1721 | i915_sw_fence_await_sw_fence_gfp(&req->submit, | |
1722 | &prev->submit, | |
1723 | GFP_KERNEL); | |
1724 | } | |
1725 | ||
1726 | ret = i915_switch_context(req); | |
1727 | i915_add_request(req); | |
1728 | ||
1729 | return ret; | |
1730 | } | |
1731 | ||
1732 | /* | |
1733 | * Manages updating the per-context aspects of the OA stream | |
1734 | * configuration across all contexts. | |
1735 | * | |
1736 | * The awkward consideration here is that OACTXCONTROL controls the | |
1737 | * exponent for periodic sampling which is primarily used for system | |
1738 | * wide profiling where we'd like a consistent sampling period even in | |
1739 | * the face of context switches. | |
1740 | * | |
1741 | * Our approach of updating the register state context (as opposed to | |
1742 | * say using a workaround batch buffer) ensures that the hardware | |
1743 | * won't automatically reload an out-of-date timer exponent even | |
1744 | * transiently before a WA BB could be parsed. | |
1745 | * | |
1746 | * This function needs to: | |
1747 | * - Ensure the currently running context's per-context OA state is | |
1748 | * updated | |
1749 | * - Ensure that all existing contexts will have the correct per-context | |
1750 | * OA state if they are scheduled for use. | |
1751 | * - Ensure any new contexts will be initialized with the correct | |
1752 | * per-context OA state. | |
1753 | * | |
1754 | * Note: it's only the RCS/Render context that has any OA state. | |
1755 | */ | |
1756 | static int gen8_configure_all_contexts(struct drm_i915_private *dev_priv, | |
701f8231 | 1757 | const struct i915_oa_config *oa_config, |
19f81df2 RB |
1758 | bool interruptible) |
1759 | { | |
1760 | struct i915_gem_context *ctx; | |
1761 | int ret; | |
1762 | unsigned int wait_flags = I915_WAIT_LOCKED; | |
1763 | ||
1764 | if (interruptible) { | |
1765 | ret = i915_mutex_lock_interruptible(&dev_priv->drm); | |
1766 | if (ret) | |
1767 | return ret; | |
1768 | ||
1769 | wait_flags |= I915_WAIT_INTERRUPTIBLE; | |
1770 | } else { | |
1771 | mutex_lock(&dev_priv->drm.struct_mutex); | |
1772 | } | |
1773 | ||
1774 | /* Switch away from any user context. */ | |
701f8231 | 1775 | ret = gen8_switch_to_updated_kernel_context(dev_priv, oa_config); |
19f81df2 RB |
1776 | if (ret) |
1777 | goto out; | |
1778 | ||
1779 | /* | |
1780 | * The OA register config is setup through the context image. This image | |
1781 | * might be written to by the GPU on context switch (in particular on | |
1782 | * lite-restore). This means we can't safely update a context's image, | |
1783 | * if this context is scheduled/submitted to run on the GPU. | |
1784 | * | |
1785 | * We could emit the OA register config through the batch buffer but | |
1786 | * this might leave small interval of time where the OA unit is | |
1787 | * configured at an invalid sampling period. | |
1788 | * | |
1789 | * So far the best way to work around this issue seems to be draining | |
1790 | * the GPU from any submitted work. | |
1791 | */ | |
1792 | ret = i915_gem_wait_for_idle(dev_priv, wait_flags); | |
1793 | if (ret) | |
1794 | goto out; | |
1795 | ||
1796 | /* Update all contexts now that we've stalled the submission. */ | |
829a0af2 | 1797 | list_for_each_entry(ctx, &dev_priv->contexts.list, link) { |
19f81df2 RB |
1798 | struct intel_context *ce = &ctx->engine[RCS]; |
1799 | u32 *regs; | |
1800 | ||
1801 | /* OA settings will be set upon first use */ | |
1802 | if (!ce->state) | |
1803 | continue; | |
1804 | ||
1805 | regs = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB); | |
1806 | if (IS_ERR(regs)) { | |
1807 | ret = PTR_ERR(regs); | |
1808 | goto out; | |
1809 | } | |
1810 | ||
1811 | ce->state->obj->mm.dirty = true; | |
1812 | regs += LRC_STATE_PN * PAGE_SIZE / sizeof(*regs); | |
1813 | ||
701f8231 | 1814 | gen8_update_reg_state_unlocked(ctx, regs, oa_config); |
19f81df2 RB |
1815 | |
1816 | i915_gem_object_unpin_map(ce->state->obj); | |
1817 | } | |
1818 | ||
1819 | out: | |
1820 | mutex_unlock(&dev_priv->drm.struct_mutex); | |
1821 | ||
1822 | return ret; | |
1823 | } | |
1824 | ||
701f8231 LL |
1825 | static int gen8_enable_metric_set(struct drm_i915_private *dev_priv, |
1826 | const struct i915_oa_config *oa_config) | |
19f81df2 | 1827 | { |
701f8231 | 1828 | int ret; |
19f81df2 RB |
1829 | |
1830 | /* | |
1831 | * We disable slice/unslice clock ratio change reports on SKL since | |
1832 | * they are too noisy. The HW generates a lot of redundant reports | |
1833 | * where the ratio hasn't really changed causing a lot of redundant | |
1834 | * work to processes and increasing the chances we'll hit buffer | |
1835 | * overruns. | |
1836 | * | |
1837 | * Although we don't currently use the 'disable overrun' OABUFFER | |
1838 | * feature it's worth noting that clock ratio reports have to be | |
1839 | * disabled before considering to use that feature since the HW doesn't | |
1840 | * correctly block these reports. | |
1841 | * | |
1842 | * Currently none of the high-level metrics we have depend on knowing | |
1843 | * this ratio to normalize. | |
1844 | * | |
1845 | * Note: This register is not power context saved and restored, but | |
1846 | * that's OK considering that we disable RC6 while the OA unit is | |
1847 | * enabled. | |
1848 | * | |
1849 | * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to | |
1850 | * be read back from automatically triggered reports, as part of the | |
1851 | * RPT_ID field. | |
1852 | */ | |
6c5c1d89 | 1853 | if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv) || |
28c7ef9e | 1854 | IS_KABYLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) { |
19f81df2 RB |
1855 | I915_WRITE(GEN8_OA_DEBUG, |
1856 | _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS | | |
1857 | GEN9_OA_DEBUG_INCLUDE_CLK_RATIO)); | |
1858 | } | |
1859 | ||
1860 | /* | |
1861 | * Update all contexts prior writing the mux configurations as we need | |
1862 | * to make sure all slices/subslices are ON before writing to NOA | |
1863 | * registers. | |
1864 | */ | |
701f8231 | 1865 | ret = gen8_configure_all_contexts(dev_priv, oa_config, true); |
19f81df2 RB |
1866 | if (ret) |
1867 | return ret; | |
1868 | ||
701f8231 LL |
1869 | config_oa_regs(dev_priv, oa_config->mux_regs, oa_config->mux_regs_len); |
1870 | ||
701f8231 LL |
1871 | config_oa_regs(dev_priv, oa_config->b_counter_regs, |
1872 | oa_config->b_counter_regs_len); | |
19f81df2 RB |
1873 | |
1874 | return 0; | |
1875 | } | |
1876 | ||
1877 | static void gen8_disable_metric_set(struct drm_i915_private *dev_priv) | |
1878 | { | |
1879 | /* Reset all contexts' slices/subslices configurations. */ | |
701f8231 | 1880 | gen8_configure_all_contexts(dev_priv, NULL, false); |
28964cf2 LL |
1881 | |
1882 | I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) & | |
1883 | ~GT_NOA_ENABLE)); | |
1884 | ||
19f81df2 RB |
1885 | } |
1886 | ||
1bef3409 | 1887 | static void gen7_oa_enable(struct drm_i915_private *dev_priv) |
d7965152 | 1888 | { |
1bef3409 RB |
1889 | /* |
1890 | * Reset buf pointers so we don't forward reports from before now. | |
1891 | * | |
1892 | * Think carefully if considering trying to avoid this, since it | |
1893 | * also ensures status flags and the buffer itself are cleared | |
1894 | * in error paths, and we have checks for invalid reports based | |
1895 | * on the assumption that certain fields are written to zeroed | |
1896 | * memory which this helps maintains. | |
1897 | */ | |
1898 | gen7_init_oa_buffer(dev_priv); | |
d7965152 RB |
1899 | |
1900 | if (dev_priv->perf.oa.exclusive_stream->enabled) { | |
1901 | struct i915_gem_context *ctx = | |
1902 | dev_priv->perf.oa.exclusive_stream->ctx; | |
1903 | u32 ctx_id = dev_priv->perf.oa.specific_ctx_id; | |
1904 | ||
1905 | bool periodic = dev_priv->perf.oa.periodic; | |
1906 | u32 period_exponent = dev_priv->perf.oa.period_exponent; | |
1907 | u32 report_format = dev_priv->perf.oa.oa_buffer.format; | |
1908 | ||
1909 | I915_WRITE(GEN7_OACONTROL, | |
1910 | (ctx_id & GEN7_OACONTROL_CTX_MASK) | | |
1911 | (period_exponent << | |
1912 | GEN7_OACONTROL_TIMER_PERIOD_SHIFT) | | |
1913 | (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) | | |
1914 | (report_format << GEN7_OACONTROL_FORMAT_SHIFT) | | |
1915 | (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) | | |
1916 | GEN7_OACONTROL_ENABLE); | |
1917 | } else | |
1918 | I915_WRITE(GEN7_OACONTROL, 0); | |
1919 | } | |
1920 | ||
19f81df2 RB |
1921 | static void gen8_oa_enable(struct drm_i915_private *dev_priv) |
1922 | { | |
1923 | u32 report_format = dev_priv->perf.oa.oa_buffer.format; | |
1924 | ||
1925 | /* | |
1926 | * Reset buf pointers so we don't forward reports from before now. | |
1927 | * | |
1928 | * Think carefully if considering trying to avoid this, since it | |
1929 | * also ensures status flags and the buffer itself are cleared | |
1930 | * in error paths, and we have checks for invalid reports based | |
1931 | * on the assumption that certain fields are written to zeroed | |
1932 | * memory which this helps maintains. | |
1933 | */ | |
1934 | gen8_init_oa_buffer(dev_priv); | |
1935 | ||
1936 | /* | |
1937 | * Note: we don't rely on the hardware to perform single context | |
1938 | * filtering and instead filter on the cpu based on the context-id | |
1939 | * field of reports | |
1940 | */ | |
1941 | I915_WRITE(GEN8_OACONTROL, (report_format << | |
1942 | GEN8_OA_REPORT_FORMAT_SHIFT) | | |
1943 | GEN8_OA_COUNTER_ENABLE); | |
1944 | } | |
1945 | ||
16d98b31 RB |
1946 | /** |
1947 | * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream | |
1948 | * @stream: An i915 perf stream opened for OA metrics | |
1949 | * | |
1950 | * [Re]enables hardware periodic sampling according to the period configured | |
1951 | * when opening the stream. This also starts a hrtimer that will periodically | |
1952 | * check for data in the circular OA buffer for notifying userspace (e.g. | |
1953 | * during a read() or poll()). | |
1954 | */ | |
d7965152 RB |
1955 | static void i915_oa_stream_enable(struct i915_perf_stream *stream) |
1956 | { | |
1957 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
1958 | ||
1959 | dev_priv->perf.oa.ops.oa_enable(dev_priv); | |
1960 | ||
1961 | if (dev_priv->perf.oa.periodic) | |
1962 | hrtimer_start(&dev_priv->perf.oa.poll_check_timer, | |
1963 | ns_to_ktime(POLL_PERIOD), | |
1964 | HRTIMER_MODE_REL_PINNED); | |
1965 | } | |
1966 | ||
1967 | static void gen7_oa_disable(struct drm_i915_private *dev_priv) | |
1968 | { | |
1969 | I915_WRITE(GEN7_OACONTROL, 0); | |
1970 | } | |
1971 | ||
19f81df2 RB |
1972 | static void gen8_oa_disable(struct drm_i915_private *dev_priv) |
1973 | { | |
1974 | I915_WRITE(GEN8_OACONTROL, 0); | |
1975 | } | |
1976 | ||
16d98b31 RB |
1977 | /** |
1978 | * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream | |
1979 | * @stream: An i915 perf stream opened for OA metrics | |
1980 | * | |
1981 | * Stops the OA unit from periodically writing counter reports into the | |
1982 | * circular OA buffer. This also stops the hrtimer that periodically checks for | |
1983 | * data in the circular OA buffer, for notifying userspace. | |
1984 | */ | |
d7965152 RB |
1985 | static void i915_oa_stream_disable(struct i915_perf_stream *stream) |
1986 | { | |
1987 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
1988 | ||
1989 | dev_priv->perf.oa.ops.oa_disable(dev_priv); | |
1990 | ||
1991 | if (dev_priv->perf.oa.periodic) | |
1992 | hrtimer_cancel(&dev_priv->perf.oa.poll_check_timer); | |
1993 | } | |
1994 | ||
d7965152 RB |
1995 | static const struct i915_perf_stream_ops i915_oa_stream_ops = { |
1996 | .destroy = i915_oa_stream_destroy, | |
1997 | .enable = i915_oa_stream_enable, | |
1998 | .disable = i915_oa_stream_disable, | |
1999 | .wait_unlocked = i915_oa_wait_unlocked, | |
2000 | .poll_wait = i915_oa_poll_wait, | |
2001 | .read = i915_oa_read, | |
eec688e1 RB |
2002 | }; |
2003 | ||
16d98b31 RB |
2004 | /** |
2005 | * i915_oa_stream_init - validate combined props for OA stream and init | |
2006 | * @stream: An i915 perf stream | |
2007 | * @param: The open parameters passed to `DRM_I915_PERF_OPEN` | |
2008 | * @props: The property state that configures stream (individually validated) | |
2009 | * | |
2010 | * While read_properties_unlocked() validates properties in isolation it | |
2011 | * doesn't ensure that the combination necessarily makes sense. | |
2012 | * | |
2013 | * At this point it has been determined that userspace wants a stream of | |
2014 | * OA metrics, but still we need to further validate the combined | |
2015 | * properties are OK. | |
2016 | * | |
2017 | * If the configuration makes sense then we can allocate memory for | |
2018 | * a circular OA buffer and apply the requested metric set configuration. | |
2019 | * | |
2020 | * Returns: zero on success or a negative error code. | |
2021 | */ | |
d7965152 RB |
2022 | static int i915_oa_stream_init(struct i915_perf_stream *stream, |
2023 | struct drm_i915_perf_open_param *param, | |
2024 | struct perf_open_properties *props) | |
2025 | { | |
2026 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
2027 | int format_size; | |
2028 | int ret; | |
2029 | ||
442b8c06 RB |
2030 | /* If the sysfs metrics/ directory wasn't registered for some |
2031 | * reason then don't let userspace try their luck with config | |
2032 | * IDs | |
2033 | */ | |
2034 | if (!dev_priv->perf.metrics_kobj) { | |
7708550c | 2035 | DRM_DEBUG("OA metrics weren't advertised via sysfs\n"); |
442b8c06 RB |
2036 | return -EINVAL; |
2037 | } | |
2038 | ||
d7965152 | 2039 | if (!(props->sample_flags & SAMPLE_OA_REPORT)) { |
7708550c | 2040 | DRM_DEBUG("Only OA report sampling supported\n"); |
d7965152 RB |
2041 | return -EINVAL; |
2042 | } | |
2043 | ||
2044 | if (!dev_priv->perf.oa.ops.init_oa_buffer) { | |
7708550c | 2045 | DRM_DEBUG("OA unit not supported\n"); |
d7965152 RB |
2046 | return -ENODEV; |
2047 | } | |
2048 | ||
2049 | /* To avoid the complexity of having to accurately filter | |
2050 | * counter reports and marshal to the appropriate client | |
2051 | * we currently only allow exclusive access | |
2052 | */ | |
2053 | if (dev_priv->perf.oa.exclusive_stream) { | |
7708550c | 2054 | DRM_DEBUG("OA unit already in use\n"); |
d7965152 RB |
2055 | return -EBUSY; |
2056 | } | |
2057 | ||
d7965152 | 2058 | if (!props->oa_format) { |
7708550c | 2059 | DRM_DEBUG("OA report format not specified\n"); |
d7965152 RB |
2060 | return -EINVAL; |
2061 | } | |
2062 | ||
712122ea RB |
2063 | /* We set up some ratelimit state to potentially throttle any _NOTES |
2064 | * about spurious, invalid OA reports which we don't forward to | |
2065 | * userspace. | |
2066 | * | |
2067 | * The initialization is associated with opening the stream (not driver | |
2068 | * init) considering we print a _NOTE about any throttling when closing | |
2069 | * the stream instead of waiting until driver _fini which no one would | |
2070 | * ever see. | |
2071 | * | |
2072 | * Using the same limiting factors as printk_ratelimit() | |
2073 | */ | |
2074 | ratelimit_state_init(&dev_priv->perf.oa.spurious_report_rs, | |
2075 | 5 * HZ, 10); | |
2076 | /* Since we use a DRM_NOTE for spurious reports it would be | |
2077 | * inconsistent to let __ratelimit() automatically print a warning for | |
2078 | * throttling. | |
2079 | */ | |
2080 | ratelimit_set_flags(&dev_priv->perf.oa.spurious_report_rs, | |
2081 | RATELIMIT_MSG_ON_RELEASE); | |
2082 | ||
d7965152 RB |
2083 | stream->sample_size = sizeof(struct drm_i915_perf_record_header); |
2084 | ||
2085 | format_size = dev_priv->perf.oa.oa_formats[props->oa_format].size; | |
2086 | ||
2087 | stream->sample_flags |= SAMPLE_OA_REPORT; | |
2088 | stream->sample_size += format_size; | |
2089 | ||
2090 | dev_priv->perf.oa.oa_buffer.format_size = format_size; | |
2091 | if (WARN_ON(dev_priv->perf.oa.oa_buffer.format_size == 0)) | |
2092 | return -EINVAL; | |
2093 | ||
2094 | dev_priv->perf.oa.oa_buffer.format = | |
2095 | dev_priv->perf.oa.oa_formats[props->oa_format].format; | |
2096 | ||
d7965152 | 2097 | dev_priv->perf.oa.periodic = props->oa_periodic; |
0dd860cf | 2098 | if (dev_priv->perf.oa.periodic) |
d7965152 RB |
2099 | dev_priv->perf.oa.period_exponent = props->oa_period_exponent; |
2100 | ||
d7965152 RB |
2101 | if (stream->ctx) { |
2102 | ret = oa_get_render_ctx_id(stream); | |
2103 | if (ret) | |
2104 | return ret; | |
2105 | } | |
2106 | ||
f89823c2 LL |
2107 | ret = get_oa_config(dev_priv, props->metrics_set, &stream->oa_config); |
2108 | if (ret) | |
2109 | goto err_config; | |
701f8231 | 2110 | |
d7965152 RB |
2111 | /* PRM - observability performance counters: |
2112 | * | |
2113 | * OACONTROL, performance counter enable, note: | |
2114 | * | |
2115 | * "When this bit is set, in order to have coherent counts, | |
2116 | * RC6 power state and trunk clock gating must be disabled. | |
2117 | * This can be achieved by programming MMIO registers as | |
2118 | * 0xA094=0 and 0xA090[31]=1" | |
2119 | * | |
2120 | * In our case we are expecting that taking pm + FORCEWAKE | |
2121 | * references will effectively disable RC6. | |
2122 | */ | |
2123 | intel_runtime_pm_get(dev_priv); | |
2124 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); | |
2125 | ||
987f8c44 | 2126 | ret = alloc_oa_buffer(dev_priv); |
2127 | if (ret) | |
2128 | goto err_oa_buf_alloc; | |
2129 | ||
701f8231 LL |
2130 | ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv, |
2131 | stream->oa_config); | |
d7965152 RB |
2132 | if (ret) |
2133 | goto err_enable; | |
2134 | ||
2135 | stream->ops = &i915_oa_stream_ops; | |
2136 | ||
701f8231 LL |
2137 | /* Lock device for exclusive_stream access late because |
2138 | * enable_metric_set() might lock as well on gen8+. | |
2139 | */ | |
2140 | ret = i915_mutex_lock_interruptible(&dev_priv->drm); | |
2141 | if (ret) | |
2142 | goto err_lock; | |
2143 | ||
d7965152 RB |
2144 | dev_priv->perf.oa.exclusive_stream = stream; |
2145 | ||
701f8231 LL |
2146 | mutex_unlock(&dev_priv->drm.struct_mutex); |
2147 | ||
d7965152 RB |
2148 | return 0; |
2149 | ||
701f8231 LL |
2150 | err_lock: |
2151 | dev_priv->perf.oa.ops.disable_metric_set(dev_priv); | |
2152 | ||
d7965152 | 2153 | err_enable: |
d7965152 RB |
2154 | free_oa_buffer(dev_priv); |
2155 | ||
2156 | err_oa_buf_alloc: | |
f89823c2 LL |
2157 | put_oa_config(dev_priv, stream->oa_config); |
2158 | ||
987f8c44 | 2159 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
2160 | intel_runtime_pm_put(dev_priv); | |
f89823c2 LL |
2161 | |
2162 | err_config: | |
d7965152 RB |
2163 | if (stream->ctx) |
2164 | oa_put_render_ctx_id(stream); | |
2165 | ||
2166 | return ret; | |
2167 | } | |
2168 | ||
19f81df2 RB |
2169 | void i915_oa_init_reg_state(struct intel_engine_cs *engine, |
2170 | struct i915_gem_context *ctx, | |
2171 | u32 *reg_state) | |
2172 | { | |
28b6cb08 | 2173 | struct i915_perf_stream *stream; |
19f81df2 RB |
2174 | |
2175 | if (engine->id != RCS) | |
2176 | return; | |
2177 | ||
28b6cb08 | 2178 | stream = engine->i915->perf.oa.exclusive_stream; |
701f8231 LL |
2179 | if (stream) |
2180 | gen8_update_reg_state_unlocked(ctx, reg_state, stream->oa_config); | |
19f81df2 RB |
2181 | } |
2182 | ||
16d98b31 RB |
2183 | /** |
2184 | * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation | |
2185 | * @stream: An i915 perf stream | |
2186 | * @file: An i915 perf stream file | |
2187 | * @buf: destination buffer given by userspace | |
2188 | * @count: the number of bytes userspace wants to read | |
2189 | * @ppos: (inout) file seek position (unused) | |
2190 | * | |
2191 | * Besides wrapping &i915_perf_stream_ops->read this provides a common place to | |
2192 | * ensure that if we've successfully copied any data then reporting that takes | |
2193 | * precedence over any internal error status, so the data isn't lost. | |
2194 | * | |
2195 | * For example ret will be -ENOSPC whenever there is more buffered data than | |
2196 | * can be copied to userspace, but that's only interesting if we weren't able | |
2197 | * to copy some data because it implies the userspace buffer is too small to | |
2198 | * receive a single record (and we never split records). | |
2199 | * | |
2200 | * Another case with ret == -EFAULT is more of a grey area since it would seem | |
2201 | * like bad form for userspace to ask us to overrun its buffer, but the user | |
2202 | * knows best: | |
2203 | * | |
2204 | * http://yarchive.net/comp/linux/partial_reads_writes.html | |
2205 | * | |
2206 | * Returns: The number of bytes copied or a negative error code on failure. | |
2207 | */ | |
eec688e1 RB |
2208 | static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream, |
2209 | struct file *file, | |
2210 | char __user *buf, | |
2211 | size_t count, | |
2212 | loff_t *ppos) | |
2213 | { | |
2214 | /* Note we keep the offset (aka bytes read) separate from any | |
2215 | * error status so that the final check for whether we return | |
2216 | * the bytes read with a higher precedence than any error (see | |
2217 | * comment below) doesn't need to be handled/duplicated in | |
2218 | * stream->ops->read() implementations. | |
2219 | */ | |
2220 | size_t offset = 0; | |
2221 | int ret = stream->ops->read(stream, buf, count, &offset); | |
2222 | ||
eec688e1 RB |
2223 | return offset ?: (ret ?: -EAGAIN); |
2224 | } | |
2225 | ||
16d98b31 RB |
2226 | /** |
2227 | * i915_perf_read - handles read() FOP for i915 perf stream FDs | |
2228 | * @file: An i915 perf stream file | |
2229 | * @buf: destination buffer given by userspace | |
2230 | * @count: the number of bytes userspace wants to read | |
2231 | * @ppos: (inout) file seek position (unused) | |
2232 | * | |
2233 | * The entry point for handling a read() on a stream file descriptor from | |
2234 | * userspace. Most of the work is left to the i915_perf_read_locked() and | |
2235 | * &i915_perf_stream_ops->read but to save having stream implementations (of | |
2236 | * which we might have multiple later) we handle blocking read here. | |
2237 | * | |
2238 | * We can also consistently treat trying to read from a disabled stream | |
2239 | * as an IO error so implementations can assume the stream is enabled | |
2240 | * while reading. | |
2241 | * | |
2242 | * Returns: The number of bytes copied or a negative error code on failure. | |
2243 | */ | |
eec688e1 RB |
2244 | static ssize_t i915_perf_read(struct file *file, |
2245 | char __user *buf, | |
2246 | size_t count, | |
2247 | loff_t *ppos) | |
2248 | { | |
2249 | struct i915_perf_stream *stream = file->private_data; | |
2250 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
2251 | ssize_t ret; | |
2252 | ||
d7965152 RB |
2253 | /* To ensure it's handled consistently we simply treat all reads of a |
2254 | * disabled stream as an error. In particular it might otherwise lead | |
2255 | * to a deadlock for blocking file descriptors... | |
2256 | */ | |
2257 | if (!stream->enabled) | |
2258 | return -EIO; | |
2259 | ||
eec688e1 | 2260 | if (!(file->f_flags & O_NONBLOCK)) { |
d7965152 RB |
2261 | /* There's the small chance of false positives from |
2262 | * stream->ops->wait_unlocked. | |
2263 | * | |
2264 | * E.g. with single context filtering since we only wait until | |
2265 | * oabuffer has >= 1 report we don't immediately know whether | |
2266 | * any reports really belong to the current context | |
eec688e1 RB |
2267 | */ |
2268 | do { | |
2269 | ret = stream->ops->wait_unlocked(stream); | |
2270 | if (ret) | |
2271 | return ret; | |
2272 | ||
2273 | mutex_lock(&dev_priv->perf.lock); | |
2274 | ret = i915_perf_read_locked(stream, file, | |
2275 | buf, count, ppos); | |
2276 | mutex_unlock(&dev_priv->perf.lock); | |
2277 | } while (ret == -EAGAIN); | |
2278 | } else { | |
2279 | mutex_lock(&dev_priv->perf.lock); | |
2280 | ret = i915_perf_read_locked(stream, file, buf, count, ppos); | |
2281 | mutex_unlock(&dev_priv->perf.lock); | |
2282 | } | |
2283 | ||
26ebd9c7 RB |
2284 | /* We allow the poll checking to sometimes report false positive POLLIN |
2285 | * events where we might actually report EAGAIN on read() if there's | |
2286 | * not really any data available. In this situation though we don't | |
2287 | * want to enter a busy loop between poll() reporting a POLLIN event | |
2288 | * and read() returning -EAGAIN. Clearing the oa.pollin state here | |
2289 | * effectively ensures we back off until the next hrtimer callback | |
2290 | * before reporting another POLLIN event. | |
2291 | */ | |
2292 | if (ret >= 0 || ret == -EAGAIN) { | |
d7965152 RB |
2293 | /* Maybe make ->pollin per-stream state if we support multiple |
2294 | * concurrent streams in the future. | |
2295 | */ | |
2296 | dev_priv->perf.oa.pollin = false; | |
2297 | } | |
2298 | ||
eec688e1 RB |
2299 | return ret; |
2300 | } | |
2301 | ||
d7965152 RB |
2302 | static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer) |
2303 | { | |
2304 | struct drm_i915_private *dev_priv = | |
2305 | container_of(hrtimer, typeof(*dev_priv), | |
2306 | perf.oa.poll_check_timer); | |
2307 | ||
19f81df2 | 2308 | if (oa_buffer_check_unlocked(dev_priv)) { |
d7965152 RB |
2309 | dev_priv->perf.oa.pollin = true; |
2310 | wake_up(&dev_priv->perf.oa.poll_wq); | |
2311 | } | |
2312 | ||
2313 | hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD)); | |
2314 | ||
2315 | return HRTIMER_RESTART; | |
2316 | } | |
2317 | ||
16d98b31 RB |
2318 | /** |
2319 | * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream | |
2320 | * @dev_priv: i915 device instance | |
2321 | * @stream: An i915 perf stream | |
2322 | * @file: An i915 perf stream file | |
2323 | * @wait: poll() state table | |
2324 | * | |
2325 | * For handling userspace polling on an i915 perf stream, this calls through to | |
2326 | * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that | |
2327 | * will be woken for new stream data. | |
2328 | * | |
2329 | * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize | |
2330 | * with any non-file-operation driver hooks. | |
2331 | * | |
2332 | * Returns: any poll events that are ready without sleeping | |
2333 | */ | |
d7965152 RB |
2334 | static unsigned int i915_perf_poll_locked(struct drm_i915_private *dev_priv, |
2335 | struct i915_perf_stream *stream, | |
eec688e1 RB |
2336 | struct file *file, |
2337 | poll_table *wait) | |
2338 | { | |
d7965152 | 2339 | unsigned int events = 0; |
eec688e1 RB |
2340 | |
2341 | stream->ops->poll_wait(stream, file, wait); | |
2342 | ||
d7965152 RB |
2343 | /* Note: we don't explicitly check whether there's something to read |
2344 | * here since this path may be very hot depending on what else | |
2345 | * userspace is polling, or on the timeout in use. We rely solely on | |
2346 | * the hrtimer/oa_poll_check_timer_cb to notify us when there are | |
2347 | * samples to read. | |
2348 | */ | |
2349 | if (dev_priv->perf.oa.pollin) | |
2350 | events |= POLLIN; | |
eec688e1 | 2351 | |
d7965152 | 2352 | return events; |
eec688e1 RB |
2353 | } |
2354 | ||
16d98b31 RB |
2355 | /** |
2356 | * i915_perf_poll - call poll_wait() with a suitable wait queue for stream | |
2357 | * @file: An i915 perf stream file | |
2358 | * @wait: poll() state table | |
2359 | * | |
2360 | * For handling userspace polling on an i915 perf stream, this ensures | |
2361 | * poll_wait() gets called with a wait queue that will be woken for new stream | |
2362 | * data. | |
2363 | * | |
2364 | * Note: Implementation deferred to i915_perf_poll_locked() | |
2365 | * | |
2366 | * Returns: any poll events that are ready without sleeping | |
2367 | */ | |
eec688e1 RB |
2368 | static unsigned int i915_perf_poll(struct file *file, poll_table *wait) |
2369 | { | |
2370 | struct i915_perf_stream *stream = file->private_data; | |
2371 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
2372 | int ret; | |
2373 | ||
2374 | mutex_lock(&dev_priv->perf.lock); | |
d7965152 | 2375 | ret = i915_perf_poll_locked(dev_priv, stream, file, wait); |
eec688e1 RB |
2376 | mutex_unlock(&dev_priv->perf.lock); |
2377 | ||
2378 | return ret; | |
2379 | } | |
2380 | ||
16d98b31 RB |
2381 | /** |
2382 | * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl | |
2383 | * @stream: A disabled i915 perf stream | |
2384 | * | |
2385 | * [Re]enables the associated capture of data for this stream. | |
2386 | * | |
2387 | * If a stream was previously enabled then there's currently no intention | |
2388 | * to provide userspace any guarantee about the preservation of previously | |
2389 | * buffered data. | |
2390 | */ | |
eec688e1 RB |
2391 | static void i915_perf_enable_locked(struct i915_perf_stream *stream) |
2392 | { | |
2393 | if (stream->enabled) | |
2394 | return; | |
2395 | ||
2396 | /* Allow stream->ops->enable() to refer to this */ | |
2397 | stream->enabled = true; | |
2398 | ||
2399 | if (stream->ops->enable) | |
2400 | stream->ops->enable(stream); | |
2401 | } | |
2402 | ||
16d98b31 RB |
2403 | /** |
2404 | * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl | |
2405 | * @stream: An enabled i915 perf stream | |
2406 | * | |
2407 | * Disables the associated capture of data for this stream. | |
2408 | * | |
2409 | * The intention is that disabling an re-enabling a stream will ideally be | |
2410 | * cheaper than destroying and re-opening a stream with the same configuration, | |
2411 | * though there are no formal guarantees about what state or buffered data | |
2412 | * must be retained between disabling and re-enabling a stream. | |
2413 | * | |
2414 | * Note: while a stream is disabled it's considered an error for userspace | |
2415 | * to attempt to read from the stream (-EIO). | |
2416 | */ | |
eec688e1 RB |
2417 | static void i915_perf_disable_locked(struct i915_perf_stream *stream) |
2418 | { | |
2419 | if (!stream->enabled) | |
2420 | return; | |
2421 | ||
2422 | /* Allow stream->ops->disable() to refer to this */ | |
2423 | stream->enabled = false; | |
2424 | ||
2425 | if (stream->ops->disable) | |
2426 | stream->ops->disable(stream); | |
2427 | } | |
2428 | ||
16d98b31 RB |
2429 | /** |
2430 | * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs | |
2431 | * @stream: An i915 perf stream | |
2432 | * @cmd: the ioctl request | |
2433 | * @arg: the ioctl data | |
2434 | * | |
2435 | * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize | |
2436 | * with any non-file-operation driver hooks. | |
2437 | * | |
2438 | * Returns: zero on success or a negative error code. Returns -EINVAL for | |
2439 | * an unknown ioctl request. | |
2440 | */ | |
eec688e1 RB |
2441 | static long i915_perf_ioctl_locked(struct i915_perf_stream *stream, |
2442 | unsigned int cmd, | |
2443 | unsigned long arg) | |
2444 | { | |
2445 | switch (cmd) { | |
2446 | case I915_PERF_IOCTL_ENABLE: | |
2447 | i915_perf_enable_locked(stream); | |
2448 | return 0; | |
2449 | case I915_PERF_IOCTL_DISABLE: | |
2450 | i915_perf_disable_locked(stream); | |
2451 | return 0; | |
2452 | } | |
2453 | ||
2454 | return -EINVAL; | |
2455 | } | |
2456 | ||
16d98b31 RB |
2457 | /** |
2458 | * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs | |
2459 | * @file: An i915 perf stream file | |
2460 | * @cmd: the ioctl request | |
2461 | * @arg: the ioctl data | |
2462 | * | |
2463 | * Implementation deferred to i915_perf_ioctl_locked(). | |
2464 | * | |
2465 | * Returns: zero on success or a negative error code. Returns -EINVAL for | |
2466 | * an unknown ioctl request. | |
2467 | */ | |
eec688e1 RB |
2468 | static long i915_perf_ioctl(struct file *file, |
2469 | unsigned int cmd, | |
2470 | unsigned long arg) | |
2471 | { | |
2472 | struct i915_perf_stream *stream = file->private_data; | |
2473 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
2474 | long ret; | |
2475 | ||
2476 | mutex_lock(&dev_priv->perf.lock); | |
2477 | ret = i915_perf_ioctl_locked(stream, cmd, arg); | |
2478 | mutex_unlock(&dev_priv->perf.lock); | |
2479 | ||
2480 | return ret; | |
2481 | } | |
2482 | ||
16d98b31 RB |
2483 | /** |
2484 | * i915_perf_destroy_locked - destroy an i915 perf stream | |
2485 | * @stream: An i915 perf stream | |
2486 | * | |
2487 | * Frees all resources associated with the given i915 perf @stream, disabling | |
2488 | * any associated data capture in the process. | |
2489 | * | |
2490 | * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize | |
2491 | * with any non-file-operation driver hooks. | |
2492 | */ | |
eec688e1 RB |
2493 | static void i915_perf_destroy_locked(struct i915_perf_stream *stream) |
2494 | { | |
eec688e1 RB |
2495 | if (stream->enabled) |
2496 | i915_perf_disable_locked(stream); | |
2497 | ||
2498 | if (stream->ops->destroy) | |
2499 | stream->ops->destroy(stream); | |
2500 | ||
2501 | list_del(&stream->link); | |
2502 | ||
69df05e1 | 2503 | if (stream->ctx) |
5f09a9c8 | 2504 | i915_gem_context_put(stream->ctx); |
eec688e1 RB |
2505 | |
2506 | kfree(stream); | |
2507 | } | |
2508 | ||
16d98b31 RB |
2509 | /** |
2510 | * i915_perf_release - handles userspace close() of a stream file | |
2511 | * @inode: anonymous inode associated with file | |
2512 | * @file: An i915 perf stream file | |
2513 | * | |
2514 | * Cleans up any resources associated with an open i915 perf stream file. | |
2515 | * | |
2516 | * NB: close() can't really fail from the userspace point of view. | |
2517 | * | |
2518 | * Returns: zero on success or a negative error code. | |
2519 | */ | |
eec688e1 RB |
2520 | static int i915_perf_release(struct inode *inode, struct file *file) |
2521 | { | |
2522 | struct i915_perf_stream *stream = file->private_data; | |
2523 | struct drm_i915_private *dev_priv = stream->dev_priv; | |
2524 | ||
2525 | mutex_lock(&dev_priv->perf.lock); | |
2526 | i915_perf_destroy_locked(stream); | |
2527 | mutex_unlock(&dev_priv->perf.lock); | |
2528 | ||
2529 | return 0; | |
2530 | } | |
2531 | ||
2532 | ||
2533 | static const struct file_operations fops = { | |
2534 | .owner = THIS_MODULE, | |
2535 | .llseek = no_llseek, | |
2536 | .release = i915_perf_release, | |
2537 | .poll = i915_perf_poll, | |
2538 | .read = i915_perf_read, | |
2539 | .unlocked_ioctl = i915_perf_ioctl, | |
7277f755 LL |
2540 | /* Our ioctl have no arguments, so it's safe to use the same function |
2541 | * to handle 32bits compatibility. | |
2542 | */ | |
2543 | .compat_ioctl = i915_perf_ioctl, | |
eec688e1 RB |
2544 | }; |
2545 | ||
2546 | ||
16d98b31 RB |
2547 | /** |
2548 | * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD | |
2549 | * @dev_priv: i915 device instance | |
2550 | * @param: The open parameters passed to 'DRM_I915_PERF_OPEN` | |
2551 | * @props: individually validated u64 property value pairs | |
2552 | * @file: drm file | |
2553 | * | |
2554 | * See i915_perf_ioctl_open() for interface details. | |
2555 | * | |
2556 | * Implements further stream config validation and stream initialization on | |
2557 | * behalf of i915_perf_open_ioctl() with the &drm_i915_private->perf.lock mutex | |
2558 | * taken to serialize with any non-file-operation driver hooks. | |
2559 | * | |
2560 | * Note: at this point the @props have only been validated in isolation and | |
2561 | * it's still necessary to validate that the combination of properties makes | |
2562 | * sense. | |
2563 | * | |
2564 | * In the case where userspace is interested in OA unit metrics then further | |
2565 | * config validation and stream initialization details will be handled by | |
2566 | * i915_oa_stream_init(). The code here should only validate config state that | |
2567 | * will be relevant to all stream types / backends. | |
2568 | * | |
2569 | * Returns: zero on success or a negative error code. | |
2570 | */ | |
eec688e1 RB |
2571 | static int |
2572 | i915_perf_open_ioctl_locked(struct drm_i915_private *dev_priv, | |
2573 | struct drm_i915_perf_open_param *param, | |
2574 | struct perf_open_properties *props, | |
2575 | struct drm_file *file) | |
2576 | { | |
2577 | struct i915_gem_context *specific_ctx = NULL; | |
2578 | struct i915_perf_stream *stream = NULL; | |
2579 | unsigned long f_flags = 0; | |
19f81df2 | 2580 | bool privileged_op = true; |
eec688e1 RB |
2581 | int stream_fd; |
2582 | int ret; | |
2583 | ||
2584 | if (props->single_context) { | |
2585 | u32 ctx_handle = props->ctx_handle; | |
2586 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
2587 | ||
635f56c3 ID |
2588 | specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle); |
2589 | if (!specific_ctx) { | |
2590 | DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n", | |
2591 | ctx_handle); | |
2592 | ret = -ENOENT; | |
eec688e1 RB |
2593 | goto err; |
2594 | } | |
2595 | } | |
2596 | ||
19f81df2 RB |
2597 | /* |
2598 | * On Haswell the OA unit supports clock gating off for a specific | |
2599 | * context and in this mode there's no visibility of metrics for the | |
2600 | * rest of the system, which we consider acceptable for a | |
2601 | * non-privileged client. | |
2602 | * | |
2603 | * For Gen8+ the OA unit no longer supports clock gating off for a | |
2604 | * specific context and the kernel can't securely stop the counters | |
2605 | * from updating as system-wide / global values. Even though we can | |
2606 | * filter reports based on the included context ID we can't block | |
2607 | * clients from seeing the raw / global counter values via | |
2608 | * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to | |
2609 | * enable the OA unit by default. | |
2610 | */ | |
2611 | if (IS_HASWELL(dev_priv) && specific_ctx) | |
2612 | privileged_op = false; | |
2613 | ||
ccdf6341 RB |
2614 | /* Similar to perf's kernel.perf_paranoid_cpu sysctl option |
2615 | * we check a dev.i915.perf_stream_paranoid sysctl option | |
2616 | * to determine if it's ok to access system wide OA counters | |
2617 | * without CAP_SYS_ADMIN privileges. | |
2618 | */ | |
19f81df2 | 2619 | if (privileged_op && |
ccdf6341 | 2620 | i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) { |
7708550c | 2621 | DRM_DEBUG("Insufficient privileges to open system-wide i915 perf stream\n"); |
eec688e1 RB |
2622 | ret = -EACCES; |
2623 | goto err_ctx; | |
2624 | } | |
2625 | ||
2626 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); | |
2627 | if (!stream) { | |
2628 | ret = -ENOMEM; | |
2629 | goto err_ctx; | |
2630 | } | |
2631 | ||
eec688e1 RB |
2632 | stream->dev_priv = dev_priv; |
2633 | stream->ctx = specific_ctx; | |
2634 | ||
d7965152 RB |
2635 | ret = i915_oa_stream_init(stream, param, props); |
2636 | if (ret) | |
2637 | goto err_alloc; | |
2638 | ||
2639 | /* we avoid simply assigning stream->sample_flags = props->sample_flags | |
2640 | * to have _stream_init check the combination of sample flags more | |
2641 | * thoroughly, but still this is the expected result at this point. | |
eec688e1 | 2642 | */ |
d7965152 RB |
2643 | if (WARN_ON(stream->sample_flags != props->sample_flags)) { |
2644 | ret = -ENODEV; | |
22f880ca | 2645 | goto err_flags; |
d7965152 | 2646 | } |
eec688e1 RB |
2647 | |
2648 | list_add(&stream->link, &dev_priv->perf.streams); | |
2649 | ||
2650 | if (param->flags & I915_PERF_FLAG_FD_CLOEXEC) | |
2651 | f_flags |= O_CLOEXEC; | |
2652 | if (param->flags & I915_PERF_FLAG_FD_NONBLOCK) | |
2653 | f_flags |= O_NONBLOCK; | |
2654 | ||
2655 | stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags); | |
2656 | if (stream_fd < 0) { | |
2657 | ret = stream_fd; | |
2658 | goto err_open; | |
2659 | } | |
2660 | ||
2661 | if (!(param->flags & I915_PERF_FLAG_DISABLED)) | |
2662 | i915_perf_enable_locked(stream); | |
2663 | ||
2664 | return stream_fd; | |
2665 | ||
2666 | err_open: | |
2667 | list_del(&stream->link); | |
22f880ca | 2668 | err_flags: |
eec688e1 RB |
2669 | if (stream->ops->destroy) |
2670 | stream->ops->destroy(stream); | |
2671 | err_alloc: | |
2672 | kfree(stream); | |
2673 | err_ctx: | |
69df05e1 | 2674 | if (specific_ctx) |
5f09a9c8 | 2675 | i915_gem_context_put(specific_ctx); |
eec688e1 RB |
2676 | err: |
2677 | return ret; | |
2678 | } | |
2679 | ||
155e941f RB |
2680 | static u64 oa_exponent_to_ns(struct drm_i915_private *dev_priv, int exponent) |
2681 | { | |
2682 | return div_u64(1000000000ULL * (2ULL << exponent), | |
2683 | dev_priv->perf.oa.timestamp_frequency); | |
2684 | } | |
2685 | ||
16d98b31 RB |
2686 | /** |
2687 | * read_properties_unlocked - validate + copy userspace stream open properties | |
2688 | * @dev_priv: i915 device instance | |
2689 | * @uprops: The array of u64 key value pairs given by userspace | |
2690 | * @n_props: The number of key value pairs expected in @uprops | |
2691 | * @props: The stream configuration built up while validating properties | |
eec688e1 RB |
2692 | * |
2693 | * Note this function only validates properties in isolation it doesn't | |
2694 | * validate that the combination of properties makes sense or that all | |
2695 | * properties necessary for a particular kind of stream have been set. | |
16d98b31 RB |
2696 | * |
2697 | * Note that there currently aren't any ordering requirements for properties so | |
2698 | * we shouldn't validate or assume anything about ordering here. This doesn't | |
2699 | * rule out defining new properties with ordering requirements in the future. | |
eec688e1 RB |
2700 | */ |
2701 | static int read_properties_unlocked(struct drm_i915_private *dev_priv, | |
2702 | u64 __user *uprops, | |
2703 | u32 n_props, | |
2704 | struct perf_open_properties *props) | |
2705 | { | |
2706 | u64 __user *uprop = uprops; | |
701f8231 | 2707 | u32 i; |
eec688e1 RB |
2708 | |
2709 | memset(props, 0, sizeof(struct perf_open_properties)); | |
2710 | ||
2711 | if (!n_props) { | |
7708550c | 2712 | DRM_DEBUG("No i915 perf properties given\n"); |
eec688e1 RB |
2713 | return -EINVAL; |
2714 | } | |
2715 | ||
2716 | /* Considering that ID = 0 is reserved and assuming that we don't | |
2717 | * (currently) expect any configurations to ever specify duplicate | |
2718 | * values for a particular property ID then the last _PROP_MAX value is | |
2719 | * one greater than the maximum number of properties we expect to get | |
2720 | * from userspace. | |
2721 | */ | |
2722 | if (n_props >= DRM_I915_PERF_PROP_MAX) { | |
7708550c | 2723 | DRM_DEBUG("More i915 perf properties specified than exist\n"); |
eec688e1 RB |
2724 | return -EINVAL; |
2725 | } | |
2726 | ||
2727 | for (i = 0; i < n_props; i++) { | |
00319ba0 | 2728 | u64 oa_period, oa_freq_hz; |
eec688e1 RB |
2729 | u64 id, value; |
2730 | int ret; | |
2731 | ||
2732 | ret = get_user(id, uprop); | |
2733 | if (ret) | |
2734 | return ret; | |
2735 | ||
2736 | ret = get_user(value, uprop + 1); | |
2737 | if (ret) | |
2738 | return ret; | |
2739 | ||
0a309f9e MA |
2740 | if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) { |
2741 | DRM_DEBUG("Unknown i915 perf property ID\n"); | |
2742 | return -EINVAL; | |
2743 | } | |
2744 | ||
eec688e1 RB |
2745 | switch ((enum drm_i915_perf_property_id)id) { |
2746 | case DRM_I915_PERF_PROP_CTX_HANDLE: | |
2747 | props->single_context = 1; | |
2748 | props->ctx_handle = value; | |
2749 | break; | |
d7965152 RB |
2750 | case DRM_I915_PERF_PROP_SAMPLE_OA: |
2751 | props->sample_flags |= SAMPLE_OA_REPORT; | |
2752 | break; | |
2753 | case DRM_I915_PERF_PROP_OA_METRICS_SET: | |
701f8231 | 2754 | if (value == 0) { |
7708550c | 2755 | DRM_DEBUG("Unknown OA metric set ID\n"); |
d7965152 RB |
2756 | return -EINVAL; |
2757 | } | |
2758 | props->metrics_set = value; | |
2759 | break; | |
2760 | case DRM_I915_PERF_PROP_OA_FORMAT: | |
2761 | if (value == 0 || value >= I915_OA_FORMAT_MAX) { | |
52c57c26 RB |
2762 | DRM_DEBUG("Out-of-range OA report format %llu\n", |
2763 | value); | |
d7965152 RB |
2764 | return -EINVAL; |
2765 | } | |
2766 | if (!dev_priv->perf.oa.oa_formats[value].size) { | |
52c57c26 RB |
2767 | DRM_DEBUG("Unsupported OA report format %llu\n", |
2768 | value); | |
d7965152 RB |
2769 | return -EINVAL; |
2770 | } | |
2771 | props->oa_format = value; | |
2772 | break; | |
2773 | case DRM_I915_PERF_PROP_OA_EXPONENT: | |
2774 | if (value > OA_EXPONENT_MAX) { | |
7708550c RB |
2775 | DRM_DEBUG("OA timer exponent too high (> %u)\n", |
2776 | OA_EXPONENT_MAX); | |
d7965152 RB |
2777 | return -EINVAL; |
2778 | } | |
2779 | ||
00319ba0 | 2780 | /* Theoretically we can program the OA unit to sample |
155e941f RB |
2781 | * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns |
2782 | * for BXT. We don't allow such high sampling | |
2783 | * frequencies by default unless root. | |
00319ba0 | 2784 | */ |
155e941f | 2785 | |
00319ba0 | 2786 | BUILD_BUG_ON(sizeof(oa_period) != 8); |
155e941f | 2787 | oa_period = oa_exponent_to_ns(dev_priv, value); |
00319ba0 RB |
2788 | |
2789 | /* This check is primarily to ensure that oa_period <= | |
2790 | * UINT32_MAX (before passing to do_div which only | |
2791 | * accepts a u32 denominator), but we can also skip | |
2792 | * checking anything < 1Hz which implicitly can't be | |
2793 | * limited via an integer oa_max_sample_rate. | |
d7965152 | 2794 | */ |
00319ba0 RB |
2795 | if (oa_period <= NSEC_PER_SEC) { |
2796 | u64 tmp = NSEC_PER_SEC; | |
2797 | do_div(tmp, oa_period); | |
2798 | oa_freq_hz = tmp; | |
2799 | } else | |
2800 | oa_freq_hz = 0; | |
2801 | ||
2802 | if (oa_freq_hz > i915_oa_max_sample_rate && | |
2803 | !capable(CAP_SYS_ADMIN)) { | |
7708550c | 2804 | DRM_DEBUG("OA exponent would exceed the max sampling frequency (sysctl dev.i915.oa_max_sample_rate) %uHz without root privileges\n", |
00319ba0 | 2805 | i915_oa_max_sample_rate); |
d7965152 RB |
2806 | return -EACCES; |
2807 | } | |
2808 | ||
2809 | props->oa_periodic = true; | |
2810 | props->oa_period_exponent = value; | |
2811 | break; | |
0a309f9e | 2812 | case DRM_I915_PERF_PROP_MAX: |
eec688e1 | 2813 | MISSING_CASE(id); |
eec688e1 RB |
2814 | return -EINVAL; |
2815 | } | |
2816 | ||
2817 | uprop += 2; | |
2818 | } | |
2819 | ||
2820 | return 0; | |
2821 | } | |
2822 | ||
16d98b31 RB |
2823 | /** |
2824 | * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD | |
2825 | * @dev: drm device | |
2826 | * @data: ioctl data copied from userspace (unvalidated) | |
2827 | * @file: drm file | |
2828 | * | |
2829 | * Validates the stream open parameters given by userspace including flags | |
2830 | * and an array of u64 key, value pair properties. | |
2831 | * | |
2832 | * Very little is assumed up front about the nature of the stream being | |
2833 | * opened (for instance we don't assume it's for periodic OA unit metrics). An | |
2834 | * i915-perf stream is expected to be a suitable interface for other forms of | |
2835 | * buffered data written by the GPU besides periodic OA metrics. | |
2836 | * | |
2837 | * Note we copy the properties from userspace outside of the i915 perf | |
2838 | * mutex to avoid an awkward lockdep with mmap_sem. | |
2839 | * | |
2840 | * Most of the implementation details are handled by | |
2841 | * i915_perf_open_ioctl_locked() after taking the &drm_i915_private->perf.lock | |
2842 | * mutex for serializing with any non-file-operation driver hooks. | |
2843 | * | |
2844 | * Return: A newly opened i915 Perf stream file descriptor or negative | |
2845 | * error code on failure. | |
2846 | */ | |
eec688e1 RB |
2847 | int i915_perf_open_ioctl(struct drm_device *dev, void *data, |
2848 | struct drm_file *file) | |
2849 | { | |
2850 | struct drm_i915_private *dev_priv = dev->dev_private; | |
2851 | struct drm_i915_perf_open_param *param = data; | |
2852 | struct perf_open_properties props; | |
2853 | u32 known_open_flags; | |
2854 | int ret; | |
2855 | ||
2856 | if (!dev_priv->perf.initialized) { | |
7708550c | 2857 | DRM_DEBUG("i915 perf interface not available for this system\n"); |
eec688e1 RB |
2858 | return -ENOTSUPP; |
2859 | } | |
2860 | ||
2861 | known_open_flags = I915_PERF_FLAG_FD_CLOEXEC | | |
2862 | I915_PERF_FLAG_FD_NONBLOCK | | |
2863 | I915_PERF_FLAG_DISABLED; | |
2864 | if (param->flags & ~known_open_flags) { | |
7708550c | 2865 | DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n"); |
eec688e1 RB |
2866 | return -EINVAL; |
2867 | } | |
2868 | ||
2869 | ret = read_properties_unlocked(dev_priv, | |
2870 | u64_to_user_ptr(param->properties_ptr), | |
2871 | param->num_properties, | |
2872 | &props); | |
2873 | if (ret) | |
2874 | return ret; | |
2875 | ||
2876 | mutex_lock(&dev_priv->perf.lock); | |
2877 | ret = i915_perf_open_ioctl_locked(dev_priv, param, &props, file); | |
2878 | mutex_unlock(&dev_priv->perf.lock); | |
2879 | ||
2880 | return ret; | |
2881 | } | |
2882 | ||
16d98b31 RB |
2883 | /** |
2884 | * i915_perf_register - exposes i915-perf to userspace | |
2885 | * @dev_priv: i915 device instance | |
2886 | * | |
2887 | * In particular OA metric sets are advertised under a sysfs metrics/ | |
2888 | * directory allowing userspace to enumerate valid IDs that can be | |
2889 | * used to open an i915-perf stream. | |
2890 | */ | |
442b8c06 RB |
2891 | void i915_perf_register(struct drm_i915_private *dev_priv) |
2892 | { | |
701f8231 LL |
2893 | int ret; |
2894 | ||
442b8c06 RB |
2895 | if (!dev_priv->perf.initialized) |
2896 | return; | |
2897 | ||
2898 | /* To be sure we're synchronized with an attempted | |
2899 | * i915_perf_open_ioctl(); considering that we register after | |
2900 | * being exposed to userspace. | |
2901 | */ | |
2902 | mutex_lock(&dev_priv->perf.lock); | |
2903 | ||
2904 | dev_priv->perf.metrics_kobj = | |
2905 | kobject_create_and_add("metrics", | |
2906 | &dev_priv->drm.primary->kdev->kobj); | |
2907 | if (!dev_priv->perf.metrics_kobj) | |
2908 | goto exit; | |
2909 | ||
40f75ea4 | 2910 | sysfs_attr_init(&dev_priv->perf.oa.test_config.sysfs_metric_id.attr); |
701f8231 | 2911 | |
19f81df2 | 2912 | if (IS_HASWELL(dev_priv)) { |
701f8231 | 2913 | i915_perf_load_test_config_hsw(dev_priv); |
19f81df2 | 2914 | } else if (IS_BROADWELL(dev_priv)) { |
701f8231 | 2915 | i915_perf_load_test_config_bdw(dev_priv); |
19f81df2 | 2916 | } else if (IS_CHERRYVIEW(dev_priv)) { |
701f8231 | 2917 | i915_perf_load_test_config_chv(dev_priv); |
19f81df2 | 2918 | } else if (IS_SKYLAKE(dev_priv)) { |
701f8231 LL |
2919 | if (IS_SKL_GT2(dev_priv)) |
2920 | i915_perf_load_test_config_sklgt2(dev_priv); | |
2921 | else if (IS_SKL_GT3(dev_priv)) | |
2922 | i915_perf_load_test_config_sklgt3(dev_priv); | |
2923 | else if (IS_SKL_GT4(dev_priv)) | |
2924 | i915_perf_load_test_config_sklgt4(dev_priv); | |
19f81df2 | 2925 | } else if (IS_BROXTON(dev_priv)) { |
701f8231 | 2926 | i915_perf_load_test_config_bxt(dev_priv); |
6c5c1d89 | 2927 | } else if (IS_KABYLAKE(dev_priv)) { |
701f8231 LL |
2928 | if (IS_KBL_GT2(dev_priv)) |
2929 | i915_perf_load_test_config_kblgt2(dev_priv); | |
2930 | else if (IS_KBL_GT3(dev_priv)) | |
2931 | i915_perf_load_test_config_kblgt3(dev_priv); | |
28c7ef9e | 2932 | } else if (IS_GEMINILAKE(dev_priv)) { |
701f8231 | 2933 | i915_perf_load_test_config_glk(dev_priv); |
442b8c06 RB |
2934 | } |
2935 | ||
701f8231 LL |
2936 | if (dev_priv->perf.oa.test_config.id == 0) |
2937 | goto sysfs_error; | |
2938 | ||
2939 | ret = sysfs_create_group(dev_priv->perf.metrics_kobj, | |
2940 | &dev_priv->perf.oa.test_config.sysfs_metric); | |
2941 | if (ret) | |
2942 | goto sysfs_error; | |
f89823c2 LL |
2943 | |
2944 | atomic_set(&dev_priv->perf.oa.test_config.ref_count, 1); | |
2945 | ||
19f81df2 RB |
2946 | goto exit; |
2947 | ||
2948 | sysfs_error: | |
2949 | kobject_put(dev_priv->perf.metrics_kobj); | |
2950 | dev_priv->perf.metrics_kobj = NULL; | |
2951 | ||
442b8c06 RB |
2952 | exit: |
2953 | mutex_unlock(&dev_priv->perf.lock); | |
2954 | } | |
2955 | ||
16d98b31 RB |
2956 | /** |
2957 | * i915_perf_unregister - hide i915-perf from userspace | |
2958 | * @dev_priv: i915 device instance | |
2959 | * | |
2960 | * i915-perf state cleanup is split up into an 'unregister' and | |
2961 | * 'deinit' phase where the interface is first hidden from | |
2962 | * userspace by i915_perf_unregister() before cleaning up | |
2963 | * remaining state in i915_perf_fini(). | |
2964 | */ | |
442b8c06 RB |
2965 | void i915_perf_unregister(struct drm_i915_private *dev_priv) |
2966 | { | |
442b8c06 RB |
2967 | if (!dev_priv->perf.metrics_kobj) |
2968 | return; | |
2969 | ||
701f8231 LL |
2970 | sysfs_remove_group(dev_priv->perf.metrics_kobj, |
2971 | &dev_priv->perf.oa.test_config.sysfs_metric); | |
442b8c06 RB |
2972 | |
2973 | kobject_put(dev_priv->perf.metrics_kobj); | |
2974 | dev_priv->perf.metrics_kobj = NULL; | |
2975 | } | |
2976 | ||
f89823c2 LL |
2977 | static bool gen8_is_valid_flex_addr(struct drm_i915_private *dev_priv, u32 addr) |
2978 | { | |
2979 | static const i915_reg_t flex_eu_regs[] = { | |
2980 | EU_PERF_CNTL0, | |
2981 | EU_PERF_CNTL1, | |
2982 | EU_PERF_CNTL2, | |
2983 | EU_PERF_CNTL3, | |
2984 | EU_PERF_CNTL4, | |
2985 | EU_PERF_CNTL5, | |
2986 | EU_PERF_CNTL6, | |
2987 | }; | |
2988 | int i; | |
2989 | ||
2990 | for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) { | |
2991 | if (flex_eu_regs[i].reg == addr) | |
2992 | return true; | |
2993 | } | |
2994 | return false; | |
2995 | } | |
2996 | ||
2997 | static bool gen7_is_valid_b_counter_addr(struct drm_i915_private *dev_priv, u32 addr) | |
2998 | { | |
2999 | return (addr >= OASTARTTRIG1.reg && addr <= OASTARTTRIG8.reg) || | |
3000 | (addr >= OAREPORTTRIG1.reg && addr <= OAREPORTTRIG8.reg) || | |
3001 | (addr >= OACEC0_0.reg && addr <= OACEC7_1.reg); | |
3002 | } | |
3003 | ||
3004 | static bool gen7_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr) | |
3005 | { | |
3006 | return addr == HALF_SLICE_CHICKEN2.reg || | |
3007 | (addr >= MICRO_BP0_0.reg && addr <= NOA_WRITE.reg) || | |
3008 | (addr >= OA_PERFCNT1_LO.reg && addr <= OA_PERFCNT2_HI.reg) || | |
3009 | (addr >= OA_PERFMATRIX_LO.reg && addr <= OA_PERFMATRIX_HI.reg); | |
3010 | } | |
3011 | ||
3012 | static bool gen8_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr) | |
3013 | { | |
3014 | return gen7_is_valid_mux_addr(dev_priv, addr) || | |
3015 | addr == WAIT_FOR_RC6_EXIT.reg || | |
3016 | (addr >= RPM_CONFIG0.reg && addr <= NOA_CONFIG(8).reg); | |
3017 | } | |
3018 | ||
3019 | static bool hsw_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr) | |
3020 | { | |
3021 | return gen7_is_valid_mux_addr(dev_priv, addr) || | |
3022 | (addr >= 0x25100 && addr <= 0x2FF90) || | |
3023 | addr == 0x9ec0; | |
3024 | } | |
3025 | ||
3026 | static bool chv_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr) | |
3027 | { | |
3028 | return gen7_is_valid_mux_addr(dev_priv, addr) || | |
3029 | (addr >= 0x182300 && addr <= 0x1823A4); | |
3030 | } | |
3031 | ||
3032 | static uint32_t mask_reg_value(u32 reg, u32 val) | |
3033 | { | |
3034 | /* HALF_SLICE_CHICKEN2 is programmed with a the | |
3035 | * WaDisableSTUnitPowerOptimization workaround. Make sure the value | |
3036 | * programmed by userspace doesn't change this. | |
3037 | */ | |
3038 | if (HALF_SLICE_CHICKEN2.reg == reg) | |
3039 | val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE); | |
3040 | ||
3041 | /* WAIT_FOR_RC6_EXIT has only one bit fullfilling the function | |
3042 | * indicated by its name and a bunch of selection fields used by OA | |
3043 | * configs. | |
3044 | */ | |
3045 | if (WAIT_FOR_RC6_EXIT.reg == reg) | |
3046 | val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE); | |
3047 | ||
3048 | return val; | |
3049 | } | |
3050 | ||
3051 | static struct i915_oa_reg *alloc_oa_regs(struct drm_i915_private *dev_priv, | |
3052 | bool (*is_valid)(struct drm_i915_private *dev_priv, u32 addr), | |
3053 | u32 __user *regs, | |
3054 | u32 n_regs) | |
3055 | { | |
3056 | struct i915_oa_reg *oa_regs; | |
3057 | int err; | |
3058 | u32 i; | |
3059 | ||
3060 | if (!n_regs) | |
3061 | return NULL; | |
3062 | ||
3063 | if (!access_ok(VERIFY_READ, regs, n_regs * sizeof(u32) * 2)) | |
3064 | return ERR_PTR(-EFAULT); | |
3065 | ||
3066 | /* No is_valid function means we're not allowing any register to be programmed. */ | |
3067 | GEM_BUG_ON(!is_valid); | |
3068 | if (!is_valid) | |
3069 | return ERR_PTR(-EINVAL); | |
3070 | ||
3071 | oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL); | |
3072 | if (!oa_regs) | |
3073 | return ERR_PTR(-ENOMEM); | |
3074 | ||
3075 | for (i = 0; i < n_regs; i++) { | |
3076 | u32 addr, value; | |
3077 | ||
3078 | err = get_user(addr, regs); | |
3079 | if (err) | |
3080 | goto addr_err; | |
3081 | ||
3082 | if (!is_valid(dev_priv, addr)) { | |
3083 | DRM_DEBUG("Invalid oa_reg address: %X\n", addr); | |
3084 | err = -EINVAL; | |
3085 | goto addr_err; | |
3086 | } | |
3087 | ||
3088 | err = get_user(value, regs + 1); | |
3089 | if (err) | |
3090 | goto addr_err; | |
3091 | ||
3092 | oa_regs[i].addr = _MMIO(addr); | |
3093 | oa_regs[i].value = mask_reg_value(addr, value); | |
3094 | ||
3095 | regs += 2; | |
3096 | } | |
3097 | ||
3098 | return oa_regs; | |
3099 | ||
3100 | addr_err: | |
3101 | kfree(oa_regs); | |
3102 | return ERR_PTR(err); | |
3103 | } | |
3104 | ||
3105 | static ssize_t show_dynamic_id(struct device *dev, | |
3106 | struct device_attribute *attr, | |
3107 | char *buf) | |
3108 | { | |
3109 | struct i915_oa_config *oa_config = | |
3110 | container_of(attr, typeof(*oa_config), sysfs_metric_id); | |
3111 | ||
3112 | return sprintf(buf, "%d\n", oa_config->id); | |
3113 | } | |
3114 | ||
3115 | static int create_dynamic_oa_sysfs_entry(struct drm_i915_private *dev_priv, | |
3116 | struct i915_oa_config *oa_config) | |
3117 | { | |
28152a23 | 3118 | sysfs_attr_init(&oa_config->sysfs_metric_id.attr); |
f89823c2 LL |
3119 | oa_config->sysfs_metric_id.attr.name = "id"; |
3120 | oa_config->sysfs_metric_id.attr.mode = S_IRUGO; | |
3121 | oa_config->sysfs_metric_id.show = show_dynamic_id; | |
3122 | oa_config->sysfs_metric_id.store = NULL; | |
3123 | ||
3124 | oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr; | |
3125 | oa_config->attrs[1] = NULL; | |
3126 | ||
3127 | oa_config->sysfs_metric.name = oa_config->uuid; | |
3128 | oa_config->sysfs_metric.attrs = oa_config->attrs; | |
3129 | ||
3130 | return sysfs_create_group(dev_priv->perf.metrics_kobj, | |
3131 | &oa_config->sysfs_metric); | |
3132 | } | |
3133 | ||
3134 | /** | |
3135 | * i915_perf_add_config_ioctl - DRM ioctl() for userspace to add a new OA config | |
3136 | * @dev: drm device | |
3137 | * @data: ioctl data (pointer to struct drm_i915_perf_oa_config) copied from | |
3138 | * userspace (unvalidated) | |
3139 | * @file: drm file | |
3140 | * | |
3141 | * Validates the submitted OA register to be saved into a new OA config that | |
3142 | * can then be used for programming the OA unit and its NOA network. | |
3143 | * | |
3144 | * Returns: A new allocated config number to be used with the perf open ioctl | |
3145 | * or a negative error code on failure. | |
3146 | */ | |
3147 | int i915_perf_add_config_ioctl(struct drm_device *dev, void *data, | |
3148 | struct drm_file *file) | |
3149 | { | |
3150 | struct drm_i915_private *dev_priv = dev->dev_private; | |
3151 | struct drm_i915_perf_oa_config *args = data; | |
3152 | struct i915_oa_config *oa_config, *tmp; | |
3153 | int err, id; | |
3154 | ||
3155 | if (!dev_priv->perf.initialized) { | |
3156 | DRM_DEBUG("i915 perf interface not available for this system\n"); | |
3157 | return -ENOTSUPP; | |
3158 | } | |
3159 | ||
3160 | if (!dev_priv->perf.metrics_kobj) { | |
3161 | DRM_DEBUG("OA metrics weren't advertised via sysfs\n"); | |
3162 | return -EINVAL; | |
3163 | } | |
3164 | ||
3165 | if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) { | |
3166 | DRM_DEBUG("Insufficient privileges to add i915 OA config\n"); | |
3167 | return -EACCES; | |
3168 | } | |
3169 | ||
3170 | if ((!args->mux_regs_ptr || !args->n_mux_regs) && | |
3171 | (!args->boolean_regs_ptr || !args->n_boolean_regs) && | |
3172 | (!args->flex_regs_ptr || !args->n_flex_regs)) { | |
3173 | DRM_DEBUG("No OA registers given\n"); | |
3174 | return -EINVAL; | |
3175 | } | |
3176 | ||
3177 | oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL); | |
3178 | if (!oa_config) { | |
3179 | DRM_DEBUG("Failed to allocate memory for the OA config\n"); | |
3180 | return -ENOMEM; | |
3181 | } | |
3182 | ||
3183 | atomic_set(&oa_config->ref_count, 1); | |
3184 | ||
3185 | if (!uuid_is_valid(args->uuid)) { | |
3186 | DRM_DEBUG("Invalid uuid format for OA config\n"); | |
3187 | err = -EINVAL; | |
3188 | goto reg_err; | |
3189 | } | |
3190 | ||
3191 | /* Last character in oa_config->uuid will be 0 because oa_config is | |
3192 | * kzalloc. | |
3193 | */ | |
3194 | memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid)); | |
3195 | ||
3196 | oa_config->mux_regs_len = args->n_mux_regs; | |
3197 | oa_config->mux_regs = | |
3198 | alloc_oa_regs(dev_priv, | |
3199 | dev_priv->perf.oa.ops.is_valid_mux_reg, | |
3200 | u64_to_user_ptr(args->mux_regs_ptr), | |
3201 | args->n_mux_regs); | |
3202 | ||
3203 | if (IS_ERR(oa_config->mux_regs)) { | |
3204 | DRM_DEBUG("Failed to create OA config for mux_regs\n"); | |
3205 | err = PTR_ERR(oa_config->mux_regs); | |
3206 | goto reg_err; | |
3207 | } | |
3208 | ||
3209 | oa_config->b_counter_regs_len = args->n_boolean_regs; | |
3210 | oa_config->b_counter_regs = | |
3211 | alloc_oa_regs(dev_priv, | |
3212 | dev_priv->perf.oa.ops.is_valid_b_counter_reg, | |
3213 | u64_to_user_ptr(args->boolean_regs_ptr), | |
3214 | args->n_boolean_regs); | |
3215 | ||
3216 | if (IS_ERR(oa_config->b_counter_regs)) { | |
3217 | DRM_DEBUG("Failed to create OA config for b_counter_regs\n"); | |
3218 | err = PTR_ERR(oa_config->b_counter_regs); | |
3219 | goto reg_err; | |
3220 | } | |
3221 | ||
3222 | if (INTEL_GEN(dev_priv) < 8) { | |
3223 | if (args->n_flex_regs != 0) { | |
3224 | err = -EINVAL; | |
3225 | goto reg_err; | |
3226 | } | |
3227 | } else { | |
3228 | oa_config->flex_regs_len = args->n_flex_regs; | |
3229 | oa_config->flex_regs = | |
3230 | alloc_oa_regs(dev_priv, | |
3231 | dev_priv->perf.oa.ops.is_valid_flex_reg, | |
3232 | u64_to_user_ptr(args->flex_regs_ptr), | |
3233 | args->n_flex_regs); | |
3234 | ||
3235 | if (IS_ERR(oa_config->flex_regs)) { | |
3236 | DRM_DEBUG("Failed to create OA config for flex_regs\n"); | |
3237 | err = PTR_ERR(oa_config->flex_regs); | |
3238 | goto reg_err; | |
3239 | } | |
3240 | } | |
3241 | ||
3242 | err = mutex_lock_interruptible(&dev_priv->perf.metrics_lock); | |
3243 | if (err) | |
3244 | goto reg_err; | |
3245 | ||
3246 | /* We shouldn't have too many configs, so this iteration shouldn't be | |
3247 | * too costly. | |
3248 | */ | |
3249 | idr_for_each_entry(&dev_priv->perf.metrics_idr, tmp, id) { | |
3250 | if (!strcmp(tmp->uuid, oa_config->uuid)) { | |
3251 | DRM_DEBUG("OA config already exists with this uuid\n"); | |
3252 | err = -EADDRINUSE; | |
3253 | goto sysfs_err; | |
3254 | } | |
3255 | } | |
3256 | ||
3257 | err = create_dynamic_oa_sysfs_entry(dev_priv, oa_config); | |
3258 | if (err) { | |
3259 | DRM_DEBUG("Failed to create sysfs entry for OA config\n"); | |
3260 | goto sysfs_err; | |
3261 | } | |
3262 | ||
3263 | /* Config id 0 is invalid, id 1 for kernel stored test config. */ | |
3264 | oa_config->id = idr_alloc(&dev_priv->perf.metrics_idr, | |
3265 | oa_config, 2, | |
3266 | 0, GFP_KERNEL); | |
3267 | if (oa_config->id < 0) { | |
3268 | DRM_DEBUG("Failed to create sysfs entry for OA config\n"); | |
3269 | err = oa_config->id; | |
3270 | goto sysfs_err; | |
3271 | } | |
3272 | ||
3273 | mutex_unlock(&dev_priv->perf.metrics_lock); | |
3274 | ||
3275 | return oa_config->id; | |
3276 | ||
3277 | sysfs_err: | |
3278 | mutex_unlock(&dev_priv->perf.metrics_lock); | |
3279 | reg_err: | |
3280 | put_oa_config(dev_priv, oa_config); | |
3281 | DRM_DEBUG("Failed to add new OA config\n"); | |
3282 | return err; | |
3283 | } | |
3284 | ||
3285 | /** | |
3286 | * i915_perf_remove_config_ioctl - DRM ioctl() for userspace to remove an OA config | |
3287 | * @dev: drm device | |
3288 | * @data: ioctl data (pointer to u64 integer) copied from userspace | |
3289 | * @file: drm file | |
3290 | * | |
3291 | * Configs can be removed while being used, the will stop appearing in sysfs | |
3292 | * and their content will be freed when the stream using the config is closed. | |
3293 | * | |
3294 | * Returns: 0 on success or a negative error code on failure. | |
3295 | */ | |
3296 | int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data, | |
3297 | struct drm_file *file) | |
3298 | { | |
3299 | struct drm_i915_private *dev_priv = dev->dev_private; | |
3300 | u64 *arg = data; | |
3301 | struct i915_oa_config *oa_config; | |
3302 | int ret; | |
3303 | ||
3304 | if (!dev_priv->perf.initialized) { | |
3305 | DRM_DEBUG("i915 perf interface not available for this system\n"); | |
3306 | return -ENOTSUPP; | |
3307 | } | |
3308 | ||
3309 | if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) { | |
3310 | DRM_DEBUG("Insufficient privileges to remove i915 OA config\n"); | |
3311 | return -EACCES; | |
3312 | } | |
3313 | ||
3314 | ret = mutex_lock_interruptible(&dev_priv->perf.metrics_lock); | |
3315 | if (ret) | |
3316 | goto lock_err; | |
3317 | ||
3318 | oa_config = idr_find(&dev_priv->perf.metrics_idr, *arg); | |
3319 | if (!oa_config) { | |
3320 | DRM_DEBUG("Failed to remove unknown OA config\n"); | |
3321 | ret = -ENOENT; | |
3322 | goto config_err; | |
3323 | } | |
3324 | ||
3325 | GEM_BUG_ON(*arg != oa_config->id); | |
3326 | ||
3327 | sysfs_remove_group(dev_priv->perf.metrics_kobj, | |
3328 | &oa_config->sysfs_metric); | |
3329 | ||
3330 | idr_remove(&dev_priv->perf.metrics_idr, *arg); | |
3331 | put_oa_config(dev_priv, oa_config); | |
3332 | ||
3333 | config_err: | |
3334 | mutex_unlock(&dev_priv->perf.metrics_lock); | |
3335 | lock_err: | |
3336 | return ret; | |
3337 | } | |
3338 | ||
ccdf6341 RB |
3339 | static struct ctl_table oa_table[] = { |
3340 | { | |
3341 | .procname = "perf_stream_paranoid", | |
3342 | .data = &i915_perf_stream_paranoid, | |
3343 | .maxlen = sizeof(i915_perf_stream_paranoid), | |
3344 | .mode = 0644, | |
3345 | .proc_handler = proc_dointvec_minmax, | |
3346 | .extra1 = &zero, | |
3347 | .extra2 = &one, | |
3348 | }, | |
00319ba0 RB |
3349 | { |
3350 | .procname = "oa_max_sample_rate", | |
3351 | .data = &i915_oa_max_sample_rate, | |
3352 | .maxlen = sizeof(i915_oa_max_sample_rate), | |
3353 | .mode = 0644, | |
3354 | .proc_handler = proc_dointvec_minmax, | |
3355 | .extra1 = &zero, | |
3356 | .extra2 = &oa_sample_rate_hard_limit, | |
3357 | }, | |
ccdf6341 RB |
3358 | {} |
3359 | }; | |
3360 | ||
3361 | static struct ctl_table i915_root[] = { | |
3362 | { | |
3363 | .procname = "i915", | |
3364 | .maxlen = 0, | |
3365 | .mode = 0555, | |
3366 | .child = oa_table, | |
3367 | }, | |
3368 | {} | |
3369 | }; | |
3370 | ||
3371 | static struct ctl_table dev_root[] = { | |
3372 | { | |
3373 | .procname = "dev", | |
3374 | .maxlen = 0, | |
3375 | .mode = 0555, | |
3376 | .child = i915_root, | |
3377 | }, | |
3378 | {} | |
3379 | }; | |
3380 | ||
16d98b31 RB |
3381 | /** |
3382 | * i915_perf_init - initialize i915-perf state on module load | |
3383 | * @dev_priv: i915 device instance | |
3384 | * | |
3385 | * Initializes i915-perf state without exposing anything to userspace. | |
3386 | * | |
3387 | * Note: i915-perf initialization is split into an 'init' and 'register' | |
3388 | * phase with the i915_perf_register() exposing state to userspace. | |
3389 | */ | |
eec688e1 RB |
3390 | void i915_perf_init(struct drm_i915_private *dev_priv) |
3391 | { | |
701f8231 | 3392 | dev_priv->perf.oa.timestamp_frequency = 0; |
19f81df2 RB |
3393 | |
3394 | if (IS_HASWELL(dev_priv)) { | |
f89823c2 LL |
3395 | dev_priv->perf.oa.ops.is_valid_b_counter_reg = |
3396 | gen7_is_valid_b_counter_addr; | |
3397 | dev_priv->perf.oa.ops.is_valid_mux_reg = | |
3398 | hsw_is_valid_mux_addr; | |
3399 | dev_priv->perf.oa.ops.is_valid_flex_reg = NULL; | |
19f81df2 RB |
3400 | dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer; |
3401 | dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set; | |
3402 | dev_priv->perf.oa.ops.disable_metric_set = hsw_disable_metric_set; | |
3403 | dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable; | |
3404 | dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable; | |
3405 | dev_priv->perf.oa.ops.read = gen7_oa_read; | |
3406 | dev_priv->perf.oa.ops.oa_hw_tail_read = | |
3407 | gen7_oa_hw_tail_read; | |
3408 | ||
155e941f RB |
3409 | dev_priv->perf.oa.timestamp_frequency = 12500000; |
3410 | ||
19f81df2 | 3411 | dev_priv->perf.oa.oa_formats = hsw_oa_formats; |
19f81df2 RB |
3412 | } else if (i915.enable_execlists) { |
3413 | /* Note: that although we could theoretically also support the | |
3414 | * legacy ringbuffer mode on BDW (and earlier iterations of | |
3415 | * this driver, before upstreaming did this) it didn't seem | |
3416 | * worth the complexity to maintain now that BDW+ enable | |
3417 | * execlist mode by default. | |
3418 | */ | |
f89823c2 LL |
3419 | dev_priv->perf.oa.ops.is_valid_b_counter_reg = |
3420 | gen7_is_valid_b_counter_addr; | |
3421 | dev_priv->perf.oa.ops.is_valid_mux_reg = | |
3422 | gen8_is_valid_mux_addr; | |
3423 | dev_priv->perf.oa.ops.is_valid_flex_reg = | |
3424 | gen8_is_valid_flex_addr; | |
d7965152 | 3425 | |
701f8231 LL |
3426 | dev_priv->perf.oa.ops.init_oa_buffer = gen8_init_oa_buffer; |
3427 | dev_priv->perf.oa.ops.enable_metric_set = gen8_enable_metric_set; | |
3428 | dev_priv->perf.oa.ops.disable_metric_set = gen8_disable_metric_set; | |
3429 | dev_priv->perf.oa.ops.oa_enable = gen8_oa_enable; | |
3430 | dev_priv->perf.oa.ops.oa_disable = gen8_oa_disable; | |
3431 | dev_priv->perf.oa.ops.read = gen8_oa_read; | |
3432 | dev_priv->perf.oa.ops.oa_hw_tail_read = gen8_oa_hw_tail_read; | |
3433 | ||
3434 | dev_priv->perf.oa.oa_formats = gen8_plus_oa_formats; | |
3435 | ||
19f81df2 RB |
3436 | if (IS_GEN8(dev_priv)) { |
3437 | dev_priv->perf.oa.ctx_oactxctrl_offset = 0x120; | |
3438 | dev_priv->perf.oa.ctx_flexeu0_offset = 0x2ce; | |
155e941f RB |
3439 | |
3440 | dev_priv->perf.oa.timestamp_frequency = 12500000; | |
3441 | ||
19f81df2 | 3442 | dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<25); |
f89823c2 LL |
3443 | if (IS_CHERRYVIEW(dev_priv)) { |
3444 | dev_priv->perf.oa.ops.is_valid_mux_reg = | |
3445 | chv_is_valid_mux_addr; | |
3446 | } | |
19f81df2 RB |
3447 | } else if (IS_GEN9(dev_priv)) { |
3448 | dev_priv->perf.oa.ctx_oactxctrl_offset = 0x128; | |
3449 | dev_priv->perf.oa.ctx_flexeu0_offset = 0x3de; | |
155e941f | 3450 | |
19f81df2 RB |
3451 | dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<16); |
3452 | ||
701f8231 LL |
3453 | switch (dev_priv->info.platform) { |
3454 | case INTEL_BROXTON: | |
3455 | case INTEL_GEMINILAKE: | |
155e941f | 3456 | dev_priv->perf.oa.timestamp_frequency = 19200000; |
701f8231 LL |
3457 | break; |
3458 | case INTEL_SKYLAKE: | |
3459 | case INTEL_KABYLAKE: | |
3460 | dev_priv->perf.oa.timestamp_frequency = 12000000; | |
3461 | break; | |
3462 | default: | |
3463 | /* Leave timestamp_frequency to 0 so we can | |
3464 | * detect unsupported platforms. | |
3465 | */ | |
3466 | break; | |
19f81df2 RB |
3467 | } |
3468 | } | |
19f81df2 | 3469 | } |
d7965152 | 3470 | |
701f8231 | 3471 | if (dev_priv->perf.oa.timestamp_frequency) { |
19f81df2 RB |
3472 | hrtimer_init(&dev_priv->perf.oa.poll_check_timer, |
3473 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
3474 | dev_priv->perf.oa.poll_check_timer.function = oa_poll_check_timer_cb; | |
3475 | init_waitqueue_head(&dev_priv->perf.oa.poll_wq); | |
d7965152 | 3476 | |
19f81df2 RB |
3477 | INIT_LIST_HEAD(&dev_priv->perf.streams); |
3478 | mutex_init(&dev_priv->perf.lock); | |
19f81df2 | 3479 | spin_lock_init(&dev_priv->perf.oa.oa_buffer.ptr_lock); |
eec688e1 | 3480 | |
155e941f RB |
3481 | oa_sample_rate_hard_limit = |
3482 | dev_priv->perf.oa.timestamp_frequency / 2; | |
19f81df2 | 3483 | dev_priv->perf.sysctl_header = register_sysctl_table(dev_root); |
ccdf6341 | 3484 | |
f89823c2 LL |
3485 | mutex_init(&dev_priv->perf.metrics_lock); |
3486 | idr_init(&dev_priv->perf.metrics_idr); | |
3487 | ||
19f81df2 RB |
3488 | dev_priv->perf.initialized = true; |
3489 | } | |
eec688e1 RB |
3490 | } |
3491 | ||
f89823c2 LL |
3492 | static int destroy_config(int id, void *p, void *data) |
3493 | { | |
3494 | struct drm_i915_private *dev_priv = data; | |
3495 | struct i915_oa_config *oa_config = p; | |
3496 | ||
3497 | put_oa_config(dev_priv, oa_config); | |
3498 | ||
3499 | return 0; | |
3500 | } | |
3501 | ||
16d98b31 RB |
3502 | /** |
3503 | * i915_perf_fini - Counter part to i915_perf_init() | |
3504 | * @dev_priv: i915 device instance | |
3505 | */ | |
eec688e1 RB |
3506 | void i915_perf_fini(struct drm_i915_private *dev_priv) |
3507 | { | |
3508 | if (!dev_priv->perf.initialized) | |
3509 | return; | |
3510 | ||
f89823c2 LL |
3511 | idr_for_each(&dev_priv->perf.metrics_idr, destroy_config, dev_priv); |
3512 | idr_destroy(&dev_priv->perf.metrics_idr); | |
3513 | ||
ccdf6341 RB |
3514 | unregister_sysctl_table(dev_priv->perf.sysctl_header); |
3515 | ||
d7965152 | 3516 | memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops)); |
19f81df2 | 3517 | |
eec688e1 RB |
3518 | dev_priv->perf.initialized = false; |
3519 | } |