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