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