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