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