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> |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 196 | |
| 197 | #include "i915_drv.h" |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 198 | #include "i915_oa_hsw.h" |
| 199 | |
| 200 | /* HW requires this to be a power of two, between 128k and 16M, though driver |
| 201 | * is currently generally designed assuming the largest 16M size is used such |
| 202 | * that the overflow cases are unlikely in normal operation. |
| 203 | */ |
| 204 | #define OA_BUFFER_SIZE SZ_16M |
| 205 | |
| 206 | #define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1)) |
| 207 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 208 | /** |
| 209 | * DOC: OA Tail Pointer Race |
| 210 | * |
| 211 | * 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] | 212 | * 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] | 213 | * been written out to the OA buffer so far (in terms of what's visible to the |
| 214 | * CPU). |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 215 | * |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 216 | * Although this can be observed explicitly while copying reports to userspace |
| 217 | * by checking for a zeroed report-id field in tail reports, we want to account |
| 218 | * for this earlier, as part of the _oa_buffer_check to avoid lots of redundant |
| 219 | * read() attempts. |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 220 | * |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 221 | * In effect we define a tail pointer for reading that lags the real tail |
| 222 | * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough |
| 223 | * time for the corresponding reports to become visible to the CPU. |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 224 | * |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 225 | * To manage this we actually track two tail pointers: |
| 226 | * 1) An 'aging' tail with an associated timestamp that is tracked until we |
| 227 | * can trust the corresponding data is visible to the CPU; at which point |
| 228 | * it is considered 'aged'. |
| 229 | * 2) An 'aged' tail that can be used for read()ing. |
| 230 | * |
| 231 | * The two separate pointers let us decouple read()s from tail pointer aging. |
| 232 | * |
| 233 | * The tail pointers are checked and updated at a limited rate within a hrtimer |
| 234 | * callback (the same callback that is used for delivering POLLIN events) |
| 235 | * |
| 236 | * Initially the tails are marked invalid with %INVALID_TAIL_PTR which |
| 237 | * indicates that an updated tail pointer is needed. |
| 238 | * |
| 239 | * Most of the implementation details for this workaround are in |
| 240 | * gen7_oa_buffer_check_unlocked() and gen7_appand_oa_reports() |
| 241 | * |
| 242 | * Note for posterity: previously the driver used to define an effective tail |
| 243 | * pointer that lagged the real pointer by a 'tail margin' measured in bytes |
| 244 | * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency. |
| 245 | * This was flawed considering that the OA unit may also automatically generate |
| 246 | * non-periodic reports (such as on context switch) or the OA unit may be |
| 247 | * enabled without any periodic sampling. |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 248 | */ |
| 249 | #define OA_TAIL_MARGIN_NSEC 100000ULL |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 250 | #define INVALID_TAIL_PTR 0xffffffff |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 251 | |
| 252 | /* frequency for checking whether the OA unit has written new reports to the |
| 253 | * circular OA buffer... |
| 254 | */ |
| 255 | #define POLL_FREQUENCY 200 |
| 256 | #define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY) |
| 257 | |
Robert Bragg | ccdf634 | 2016-11-07 19:49:54 +0000 | [diff] [blame] | 258 | /* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */ |
| 259 | static int zero; |
| 260 | static int one = 1; |
| 261 | static u32 i915_perf_stream_paranoid = true; |
| 262 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 263 | /* The maximum exponent the hardware accepts is 63 (essentially it selects one |
| 264 | * of the 64bit timestamp bits to trigger reports from) but there's currently |
| 265 | * no known use case for sampling as infrequently as once per 47 thousand years. |
| 266 | * |
| 267 | * Since the timestamps included in OA reports are only 32bits it seems |
| 268 | * reasonable to limit the OA exponent where it's still possible to account for |
| 269 | * overflow in OA report timestamps. |
| 270 | */ |
| 271 | #define OA_EXPONENT_MAX 31 |
| 272 | |
| 273 | #define INVALID_CTX_ID 0xffffffff |
| 274 | |
| 275 | |
Robert Bragg | 00319ba | 2016-11-07 19:49:55 +0000 | [diff] [blame] | 276 | /* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate |
| 277 | * |
| 278 | * 160ns is the smallest sampling period we can theoretically program the OA |
| 279 | * unit with on Haswell, corresponding to 6.25MHz. |
| 280 | */ |
| 281 | static int oa_sample_rate_hard_limit = 6250000; |
| 282 | |
| 283 | /* Theoretically we can program the OA unit to sample every 160ns but don't |
| 284 | * allow that by default unless root... |
| 285 | * |
| 286 | * The default threshold of 100000Hz is based on perf's similar |
| 287 | * kernel.perf_event_max_sample_rate sysctl parameter. |
| 288 | */ |
| 289 | static u32 i915_oa_max_sample_rate = 100000; |
| 290 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 291 | /* XXX: beware if future OA HW adds new report formats that the current |
| 292 | * code assumes all reports have a power-of-two size and ~(size - 1) can |
| 293 | * be used as a mask to align the OA tail pointer. |
| 294 | */ |
| 295 | static struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = { |
| 296 | [I915_OA_FORMAT_A13] = { 0, 64 }, |
| 297 | [I915_OA_FORMAT_A29] = { 1, 128 }, |
| 298 | [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 }, |
| 299 | /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */ |
| 300 | [I915_OA_FORMAT_B4_C8] = { 4, 64 }, |
| 301 | [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 }, |
| 302 | [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 }, |
| 303 | [I915_OA_FORMAT_C4_B8] = { 7, 64 }, |
| 304 | }; |
| 305 | |
| 306 | #define SAMPLE_OA_REPORT (1<<0) |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 307 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 308 | /** |
| 309 | * struct perf_open_properties - for validated properties given to open a stream |
| 310 | * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags |
| 311 | * @single_context: Whether a single or all gpu contexts should be monitored |
| 312 | * @ctx_handle: A gem ctx handle for use with @single_context |
| 313 | * @metrics_set: An ID for an OA unit metric set advertised via sysfs |
| 314 | * @oa_format: An OA unit HW report format |
| 315 | * @oa_periodic: Whether to enable periodic OA unit sampling |
| 316 | * @oa_period_exponent: The OA unit sampling period is derived from this |
| 317 | * |
| 318 | * As read_properties_unlocked() enumerates and validates the properties given |
| 319 | * to open a stream of metrics the configuration is built up in the structure |
| 320 | * which starts out zero initialized. |
| 321 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 322 | struct perf_open_properties { |
| 323 | u32 sample_flags; |
| 324 | |
| 325 | u64 single_context:1; |
| 326 | u64 ctx_handle; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 327 | |
| 328 | /* OA sampling state */ |
| 329 | int metrics_set; |
| 330 | int oa_format; |
| 331 | bool oa_periodic; |
| 332 | int oa_period_exponent; |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 335 | /** |
| 336 | * gen7_oa_buffer_check_unlocked - check for data and update tail ptr state |
| 337 | * @dev_priv: i915 device instance |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 338 | * |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 339 | * This is either called via fops (for blocking reads in user ctx) or the poll |
| 340 | * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check |
| 341 | * if there is data available for userspace to read. |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 342 | * |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 343 | * This function is central to providing a workaround for the OA unit tail |
| 344 | * pointer having a race with respect to what data is visible to the CPU. |
| 345 | * It is responsible for reading tail pointers from the hardware and giving |
| 346 | * the pointers time to 'age' before they are made available for reading. |
| 347 | * (See description of OA_TAIL_MARGIN_NSEC above for further details.) |
| 348 | * |
| 349 | * Besides returning true when there is data available to read() this function |
| 350 | * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp |
| 351 | * and .aged_tail_idx state used for reading. |
| 352 | * |
| 353 | * Note: It's safe to read OA config state here unlocked, assuming that this is |
| 354 | * only called while the stream is enabled, while the global OA configuration |
| 355 | * can't be modified. |
| 356 | * |
| 357 | * Returns: %true if the OA buffer contains data, else %false |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 358 | */ |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 359 | static bool gen7_oa_buffer_check_unlocked(struct drm_i915_private *dev_priv) |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 360 | { |
| 361 | int report_size = dev_priv->perf.oa.oa_buffer.format_size; |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 362 | unsigned long flags; |
| 363 | unsigned int aged_idx; |
| 364 | u32 oastatus1; |
| 365 | u32 head, hw_tail, aged_tail, aging_tail; |
| 366 | u64 now; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 367 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 368 | /* We have to consider the (unlikely) possibility that read() errors |
| 369 | * could result in an OA buffer reset which might reset the head, |
| 370 | * tails[] and aged_tail state. |
| 371 | */ |
| 372 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
| 373 | |
| 374 | /* NB: The head we observe here might effectively be a little out of |
| 375 | * date (between head and tails[aged_idx].offset if there is currently |
| 376 | * a read() in progress. |
| 377 | */ |
| 378 | head = dev_priv->perf.oa.oa_buffer.head; |
| 379 | |
| 380 | aged_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx; |
| 381 | aged_tail = dev_priv->perf.oa.oa_buffer.tails[aged_idx].offset; |
| 382 | aging_tail = dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset; |
| 383 | |
| 384 | oastatus1 = I915_READ(GEN7_OASTATUS1); |
| 385 | hw_tail = oastatus1 & GEN7_OASTATUS1_TAIL_MASK; |
| 386 | |
| 387 | /* The tail pointer increases in 64 byte increments, |
| 388 | * not in report_size steps... |
| 389 | */ |
| 390 | hw_tail &= ~(report_size - 1); |
| 391 | |
| 392 | now = ktime_get_mono_fast_ns(); |
| 393 | |
Robert Bragg | 4117ebc | 2017-05-11 16:43:30 +0100 | [diff] [blame^] | 394 | /* Update the aged tail |
| 395 | * |
| 396 | * Flip the tail pointer available for read()s once the aging tail is |
| 397 | * old enough to trust that the corresponding data will be visible to |
| 398 | * the CPU... |
| 399 | * |
| 400 | * Do this before updating the aging pointer in case we may be able to |
| 401 | * immediately start aging a new pointer too (if new data has become |
| 402 | * available) without needing to wait for a later hrtimer callback. |
| 403 | */ |
| 404 | if (aging_tail != INVALID_TAIL_PTR && |
| 405 | ((now - dev_priv->perf.oa.oa_buffer.aging_timestamp) > |
| 406 | OA_TAIL_MARGIN_NSEC)) { |
| 407 | aged_idx ^= 1; |
| 408 | dev_priv->perf.oa.oa_buffer.aged_tail_idx = aged_idx; |
| 409 | |
| 410 | aged_tail = aging_tail; |
| 411 | |
| 412 | /* Mark that we need a new pointer to start aging... */ |
| 413 | dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR; |
| 414 | aging_tail = INVALID_TAIL_PTR; |
| 415 | } |
| 416 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 417 | /* Update the aging tail |
| 418 | * |
| 419 | * We throttle aging tail updates until we have a new tail that |
| 420 | * represents >= one report more data than is already available for |
| 421 | * reading. This ensures there will be enough data for a successful |
| 422 | * read once this new pointer has aged and ensures we will give the new |
| 423 | * pointer time to age. |
| 424 | */ |
| 425 | if (aging_tail == INVALID_TAIL_PTR && |
| 426 | (aged_tail == INVALID_TAIL_PTR || |
| 427 | OA_TAKEN(hw_tail, aged_tail) >= report_size)) { |
| 428 | struct i915_vma *vma = dev_priv->perf.oa.oa_buffer.vma; |
| 429 | u32 gtt_offset = i915_ggtt_offset(vma); |
| 430 | |
| 431 | /* Be paranoid and do a bounds check on the pointer read back |
| 432 | * from hardware, just in case some spurious hardware condition |
| 433 | * could put the tail out of bounds... |
| 434 | */ |
| 435 | if (hw_tail >= gtt_offset && |
| 436 | hw_tail < (gtt_offset + OA_BUFFER_SIZE)) { |
| 437 | dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset = |
| 438 | aging_tail = hw_tail; |
| 439 | dev_priv->perf.oa.oa_buffer.aging_timestamp = now; |
| 440 | } else { |
| 441 | DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %u\n", |
| 442 | hw_tail); |
| 443 | } |
| 444 | } |
| 445 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 446 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
| 447 | |
| 448 | return aged_tail == INVALID_TAIL_PTR ? |
| 449 | false : OA_TAKEN(aged_tail, head) >= report_size; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /** |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 453 | * append_oa_status - Appends a status record to a userspace read() buffer. |
| 454 | * @stream: An i915-perf stream opened for OA metrics |
| 455 | * @buf: destination buffer given by userspace |
| 456 | * @count: the number of bytes userspace wants to read |
| 457 | * @offset: (inout): the current position for writing into @buf |
| 458 | * @type: The kind of status to report to userspace |
| 459 | * |
| 460 | * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`) |
| 461 | * into the userspace read() buffer. |
| 462 | * |
| 463 | * The @buf @offset will only be updated on success. |
| 464 | * |
| 465 | * Returns: 0 on success, negative error code on failure. |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 466 | */ |
| 467 | static int append_oa_status(struct i915_perf_stream *stream, |
| 468 | char __user *buf, |
| 469 | size_t count, |
| 470 | size_t *offset, |
| 471 | enum drm_i915_perf_record_type type) |
| 472 | { |
| 473 | struct drm_i915_perf_record_header header = { type, 0, sizeof(header) }; |
| 474 | |
| 475 | if ((count - *offset) < header.size) |
| 476 | return -ENOSPC; |
| 477 | |
| 478 | if (copy_to_user(buf + *offset, &header, sizeof(header))) |
| 479 | return -EFAULT; |
| 480 | |
| 481 | (*offset) += header.size; |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /** |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 487 | * append_oa_sample - Copies single OA report into userspace read() buffer. |
| 488 | * @stream: An i915-perf stream opened for OA metrics |
| 489 | * @buf: destination buffer given by userspace |
| 490 | * @count: the number of bytes userspace wants to read |
| 491 | * @offset: (inout): the current position for writing into @buf |
| 492 | * @report: A single OA report to (optionally) include as part of the sample |
| 493 | * |
| 494 | * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*` |
| 495 | * properties when opening a stream, tracked as `stream->sample_flags`. This |
| 496 | * function copies the requested components of a single sample to the given |
| 497 | * read() @buf. |
| 498 | * |
| 499 | * The @buf @offset will only be updated on success. |
| 500 | * |
| 501 | * Returns: 0 on success, negative error code on failure. |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 502 | */ |
| 503 | static int append_oa_sample(struct i915_perf_stream *stream, |
| 504 | char __user *buf, |
| 505 | size_t count, |
| 506 | size_t *offset, |
| 507 | const u8 *report) |
| 508 | { |
| 509 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 510 | int report_size = dev_priv->perf.oa.oa_buffer.format_size; |
| 511 | struct drm_i915_perf_record_header header; |
| 512 | u32 sample_flags = stream->sample_flags; |
| 513 | |
| 514 | header.type = DRM_I915_PERF_RECORD_SAMPLE; |
| 515 | header.pad = 0; |
| 516 | header.size = stream->sample_size; |
| 517 | |
| 518 | if ((count - *offset) < header.size) |
| 519 | return -ENOSPC; |
| 520 | |
| 521 | buf += *offset; |
| 522 | if (copy_to_user(buf, &header, sizeof(header))) |
| 523 | return -EFAULT; |
| 524 | buf += sizeof(header); |
| 525 | |
| 526 | if (sample_flags & SAMPLE_OA_REPORT) { |
| 527 | if (copy_to_user(buf, report, report_size)) |
| 528 | return -EFAULT; |
| 529 | } |
| 530 | |
| 531 | (*offset) += header.size; |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Copies all buffered OA reports into userspace read() buffer. |
| 538 | * @stream: An i915-perf stream opened for OA metrics |
| 539 | * @buf: destination buffer given by userspace |
| 540 | * @count: the number of bytes userspace wants to read |
| 541 | * @offset: (inout): the current position for writing into @buf |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 542 | * |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 543 | * Notably any error condition resulting in a short read (-%ENOSPC or |
| 544 | * -%EFAULT) will be returned even though one or more records may |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 545 | * have been successfully copied. In this case it's up to the caller |
| 546 | * to decide if the error should be squashed before returning to |
| 547 | * userspace. |
| 548 | * |
| 549 | * Note: reports are consumed from the head, and appended to the |
Robert Bragg | e81b3a5 | 2017-05-11 16:43:24 +0100 | [diff] [blame] | 550 | * 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] | 551 | * and back-to-front you're not alone, but this follows the |
| 552 | * Gen PRM naming convention. |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 553 | * |
| 554 | * Returns: 0 on success, negative error code on failure. |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 555 | */ |
| 556 | static int gen7_append_oa_reports(struct i915_perf_stream *stream, |
| 557 | char __user *buf, |
| 558 | size_t count, |
Robert Bragg | 3bb335c | 2017-05-11 16:43:27 +0100 | [diff] [blame] | 559 | size_t *offset) |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 560 | { |
| 561 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 562 | int report_size = dev_priv->perf.oa.oa_buffer.format_size; |
| 563 | u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 564 | u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma); |
| 565 | u32 mask = (OA_BUFFER_SIZE - 1); |
Robert Bragg | 3bb335c | 2017-05-11 16:43:27 +0100 | [diff] [blame] | 566 | size_t start_offset = *offset; |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 567 | unsigned long flags; |
| 568 | unsigned int aged_tail_idx; |
| 569 | u32 head, tail; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 570 | u32 taken; |
| 571 | int ret = 0; |
| 572 | |
| 573 | if (WARN_ON(!stream->enabled)) |
| 574 | return -EIO; |
| 575 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 576 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
Robert Bragg | f279020 | 2017-05-11 16:43:26 +0100 | [diff] [blame] | 577 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 578 | head = dev_priv->perf.oa.oa_buffer.head; |
| 579 | aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx; |
| 580 | tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset; |
| 581 | |
| 582 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
| 583 | |
| 584 | /* An invalid tail pointer here means we're still waiting for the poll |
| 585 | * hrtimer callback to give us a pointer |
Robert Bragg | f279020 | 2017-05-11 16:43:26 +0100 | [diff] [blame] | 586 | */ |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 587 | if (tail == INVALID_TAIL_PTR) |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 588 | return -EAGAIN; |
| 589 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 590 | /* NB: oa_buffer.head/tail include the gtt_offset which we don't want |
| 591 | * while indexing relative to oa_buf_base. |
| 592 | */ |
| 593 | head -= gtt_offset; |
| 594 | tail -= gtt_offset; |
| 595 | |
| 596 | /* An out of bounds or misaligned head or tail pointer implies a driver |
| 597 | * bug since we validate + align the tail pointers we read from the |
| 598 | * hardware and we are in full control of the head pointer which should |
| 599 | * only be incremented by multiples of the report size (notably also |
| 600 | * all a power of two). |
| 601 | */ |
| 602 | if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size || |
| 603 | tail > OA_BUFFER_SIZE || tail % report_size, |
| 604 | "Inconsistent OA buffer pointers: head = %u, tail = %u\n", |
| 605 | head, tail)) |
| 606 | return -EIO; |
| 607 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 608 | |
| 609 | for (/* none */; |
| 610 | (taken = OA_TAKEN(tail, head)); |
| 611 | head = (head + report_size) & mask) { |
| 612 | u8 *report = oa_buf_base + head; |
| 613 | u32 *report32 = (void *)report; |
| 614 | |
| 615 | /* All the report sizes factor neatly into the buffer |
| 616 | * size so we never expect to see a report split |
| 617 | * between the beginning and end of the buffer. |
| 618 | * |
| 619 | * Given the initial alignment check a misalignment |
| 620 | * here would imply a driver bug that would result |
| 621 | * in an overrun. |
| 622 | */ |
| 623 | if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) { |
| 624 | DRM_ERROR("Spurious OA head ptr: non-integral report offset\n"); |
| 625 | break; |
| 626 | } |
| 627 | |
| 628 | /* The report-ID field for periodic samples includes |
| 629 | * some undocumented flags related to what triggered |
| 630 | * the report and is never expected to be zero so we |
| 631 | * can check that the report isn't invalid before |
| 632 | * copying it to userspace... |
| 633 | */ |
| 634 | if (report32[0] == 0) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 635 | DRM_NOTE("Skipping spurious, invalid OA report\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 636 | continue; |
| 637 | } |
| 638 | |
| 639 | ret = append_oa_sample(stream, buf, count, offset, report); |
| 640 | if (ret) |
| 641 | break; |
| 642 | |
| 643 | /* The above report-id field sanity check is based on |
| 644 | * the assumption that the OA buffer is initially |
| 645 | * zeroed and we reset the field after copying so the |
| 646 | * check is still meaningful once old reports start |
| 647 | * being overwritten. |
| 648 | */ |
| 649 | report32[0] = 0; |
| 650 | } |
| 651 | |
Robert Bragg | 3bb335c | 2017-05-11 16:43:27 +0100 | [diff] [blame] | 652 | if (start_offset != *offset) { |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 653 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
| 654 | |
Robert Bragg | 3bb335c | 2017-05-11 16:43:27 +0100 | [diff] [blame] | 655 | /* We removed the gtt_offset for the copy loop above, indexing |
| 656 | * relative to oa_buf_base so put back here... |
| 657 | */ |
| 658 | head += gtt_offset; |
| 659 | |
| 660 | I915_WRITE(GEN7_OASTATUS2, |
| 661 | ((head & GEN7_OASTATUS2_HEAD_MASK) | |
| 662 | OA_MEM_SELECT_GGTT)); |
| 663 | dev_priv->perf.oa.oa_buffer.head = head; |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 664 | |
| 665 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
Robert Bragg | 3bb335c | 2017-05-11 16:43:27 +0100 | [diff] [blame] | 666 | } |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 671 | /** |
| 672 | * gen7_oa_read - copy status records then buffered OA reports |
| 673 | * @stream: An i915-perf stream opened for OA metrics |
| 674 | * @buf: destination buffer given by userspace |
| 675 | * @count: the number of bytes userspace wants to read |
| 676 | * @offset: (inout): the current position for writing into @buf |
| 677 | * |
| 678 | * Checks Gen 7 specific OA unit status registers and if necessary appends |
| 679 | * corresponding status records for userspace (such as for a buffer full |
| 680 | * condition) and then initiate appending any buffered OA reports. |
| 681 | * |
| 682 | * Updates @offset according to the number of bytes successfully copied into |
| 683 | * the userspace buffer. |
| 684 | * |
| 685 | * Returns: zero on success or a negative error code |
| 686 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 687 | static int gen7_oa_read(struct i915_perf_stream *stream, |
| 688 | char __user *buf, |
| 689 | size_t count, |
| 690 | size_t *offset) |
| 691 | { |
| 692 | struct drm_i915_private *dev_priv = stream->dev_priv; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 693 | u32 oastatus1; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 694 | int ret; |
| 695 | |
| 696 | if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr)) |
| 697 | return -EIO; |
| 698 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 699 | oastatus1 = I915_READ(GEN7_OASTATUS1); |
| 700 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 701 | /* XXX: On Haswell we don't have a safe way to clear oastatus1 |
| 702 | * bits while the OA unit is enabled (while the tail pointer |
| 703 | * may be updated asynchronously) so we ignore status bits |
| 704 | * that have already been reported to userspace. |
| 705 | */ |
| 706 | oastatus1 &= ~dev_priv->perf.oa.gen7_latched_oastatus1; |
| 707 | |
| 708 | /* We treat OABUFFER_OVERFLOW as a significant error: |
| 709 | * |
| 710 | * - The status can be interpreted to mean that the buffer is |
| 711 | * currently full (with a higher precedence than OA_TAKEN() |
| 712 | * which will start to report a near-empty buffer after an |
| 713 | * overflow) but it's awkward that we can't clear the status |
| 714 | * on Haswell, so without a reset we won't be able to catch |
| 715 | * the state again. |
| 716 | * |
| 717 | * - Since it also implies the HW has started overwriting old |
| 718 | * reports it may also affect our sanity checks for invalid |
| 719 | * reports when copying to userspace that assume new reports |
| 720 | * are being written to cleared memory. |
| 721 | * |
| 722 | * - In the future we may want to introduce a flight recorder |
| 723 | * mode where the driver will automatically maintain a safe |
| 724 | * guard band between head/tail, avoiding this overflow |
| 725 | * condition, but we avoid the added driver complexity for |
| 726 | * now. |
| 727 | */ |
| 728 | if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) { |
| 729 | ret = append_oa_status(stream, buf, count, offset, |
| 730 | DRM_I915_PERF_RECORD_OA_BUFFER_LOST); |
| 731 | if (ret) |
| 732 | return ret; |
| 733 | |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 734 | DRM_DEBUG("OA buffer overflow: force restart\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 735 | |
| 736 | dev_priv->perf.oa.ops.oa_disable(dev_priv); |
| 737 | dev_priv->perf.oa.ops.oa_enable(dev_priv); |
| 738 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 739 | oastatus1 = I915_READ(GEN7_OASTATUS1); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) { |
| 743 | ret = append_oa_status(stream, buf, count, offset, |
| 744 | DRM_I915_PERF_RECORD_OA_REPORT_LOST); |
| 745 | if (ret) |
| 746 | return ret; |
| 747 | dev_priv->perf.oa.gen7_latched_oastatus1 |= |
| 748 | GEN7_OASTATUS1_REPORT_LOST; |
| 749 | } |
| 750 | |
Robert Bragg | 3bb335c | 2017-05-11 16:43:27 +0100 | [diff] [blame] | 751 | return gen7_append_oa_reports(stream, buf, count, offset); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 754 | /** |
| 755 | * i915_oa_wait_unlocked - handles blocking IO until OA data available |
| 756 | * @stream: An i915-perf stream opened for OA metrics |
| 757 | * |
| 758 | * Called when userspace tries to read() from a blocking stream FD opened |
| 759 | * for OA metrics. It waits until the hrtimer callback finds a non-empty |
| 760 | * OA buffer and wakes us. |
| 761 | * |
| 762 | * Note: it's acceptable to have this return with some false positives |
| 763 | * since any subsequent read handling will return -EAGAIN if there isn't |
| 764 | * really data ready for userspace yet. |
| 765 | * |
| 766 | * Returns: zero on success or a negative error code |
| 767 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 768 | static int i915_oa_wait_unlocked(struct i915_perf_stream *stream) |
| 769 | { |
| 770 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 771 | |
| 772 | /* We would wait indefinitely if periodic sampling is not enabled */ |
| 773 | if (!dev_priv->perf.oa.periodic) |
| 774 | return -EIO; |
| 775 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 776 | return wait_event_interruptible(dev_priv->perf.oa.poll_wq, |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 777 | dev_priv->perf.oa.ops.oa_buffer_check(dev_priv)); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 780 | /** |
| 781 | * i915_oa_poll_wait - call poll_wait() for an OA stream poll() |
| 782 | * @stream: An i915-perf stream opened for OA metrics |
| 783 | * @file: An i915 perf stream file |
| 784 | * @wait: poll() state table |
| 785 | * |
| 786 | * For handling userspace polling on an i915 perf stream opened for OA metrics, |
| 787 | * this starts a poll_wait with the wait queue that our hrtimer callback wakes |
| 788 | * when it sees data ready to read in the circular OA buffer. |
| 789 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 790 | static void i915_oa_poll_wait(struct i915_perf_stream *stream, |
| 791 | struct file *file, |
| 792 | poll_table *wait) |
| 793 | { |
| 794 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 795 | |
| 796 | poll_wait(file, &dev_priv->perf.oa.poll_wq, wait); |
| 797 | } |
| 798 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 799 | /** |
| 800 | * i915_oa_read - just calls through to &i915_oa_ops->read |
| 801 | * @stream: An i915-perf stream opened for OA metrics |
| 802 | * @buf: destination buffer given by userspace |
| 803 | * @count: the number of bytes userspace wants to read |
| 804 | * @offset: (inout): the current position for writing into @buf |
| 805 | * |
| 806 | * Updates @offset according to the number of bytes successfully copied into |
| 807 | * the userspace buffer. |
| 808 | * |
| 809 | * Returns: zero on success or a negative error code |
| 810 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 811 | static int i915_oa_read(struct i915_perf_stream *stream, |
| 812 | char __user *buf, |
| 813 | size_t count, |
| 814 | size_t *offset) |
| 815 | { |
| 816 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 817 | |
| 818 | return dev_priv->perf.oa.ops.read(stream, buf, count, offset); |
| 819 | } |
| 820 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 821 | /** |
| 822 | * oa_get_render_ctx_id - determine and hold ctx hw id |
| 823 | * @stream: An i915-perf stream opened for OA metrics |
| 824 | * |
| 825 | * 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] | 826 | * lifetime of the stream. This ensures that we don't have to worry about |
| 827 | * updating the context ID in OACONTROL on the fly. |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 828 | * |
| 829 | * Returns: zero on success or a negative error code |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 830 | */ |
| 831 | static int oa_get_render_ctx_id(struct i915_perf_stream *stream) |
| 832 | { |
| 833 | struct drm_i915_private *dev_priv = stream->dev_priv; |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 834 | struct intel_engine_cs *engine = dev_priv->engine[RCS]; |
Chris Wilson | 266a240 | 2017-05-04 10:33:08 +0100 | [diff] [blame] | 835 | struct intel_ring *ring; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 836 | int ret; |
| 837 | |
| 838 | ret = i915_mutex_lock_interruptible(&dev_priv->drm); |
| 839 | if (ret) |
| 840 | return ret; |
| 841 | |
| 842 | /* As the ID is the gtt offset of the context's vma we pin |
| 843 | * the vma to ensure the ID remains fixed. |
| 844 | * |
| 845 | * NB: implied RCS engine... |
| 846 | */ |
Chris Wilson | 266a240 | 2017-05-04 10:33:08 +0100 | [diff] [blame] | 847 | ring = engine->context_pin(engine, stream->ctx); |
| 848 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 849 | if (IS_ERR(ring)) |
| 850 | return PTR_ERR(ring); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 851 | |
| 852 | /* Explicitly track the ID (instead of calling i915_ggtt_offset() |
| 853 | * on the fly) considering the difference with gen8+ and |
| 854 | * execlists |
| 855 | */ |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 856 | dev_priv->perf.oa.specific_ctx_id = |
| 857 | i915_ggtt_offset(stream->ctx->engine[engine->id].state); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 858 | |
Chris Wilson | 266a240 | 2017-05-04 10:33:08 +0100 | [diff] [blame] | 859 | return 0; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 862 | /** |
| 863 | * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold |
| 864 | * @stream: An i915-perf stream opened for OA metrics |
| 865 | * |
| 866 | * In case anything needed doing to ensure the context HW ID would remain valid |
| 867 | * for the lifetime of the stream, then that can be undone here. |
| 868 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 869 | static void oa_put_render_ctx_id(struct i915_perf_stream *stream) |
| 870 | { |
| 871 | struct drm_i915_private *dev_priv = stream->dev_priv; |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 872 | struct intel_engine_cs *engine = dev_priv->engine[RCS]; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 873 | |
| 874 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 875 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 876 | dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID; |
Chris Wilson | e8a9c58 | 2016-12-18 15:37:20 +0000 | [diff] [blame] | 877 | engine->context_unpin(engine, stream->ctx); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 878 | |
| 879 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 880 | } |
| 881 | |
| 882 | static void |
| 883 | free_oa_buffer(struct drm_i915_private *i915) |
| 884 | { |
| 885 | mutex_lock(&i915->drm.struct_mutex); |
| 886 | |
| 887 | i915_gem_object_unpin_map(i915->perf.oa.oa_buffer.vma->obj); |
| 888 | i915_vma_unpin(i915->perf.oa.oa_buffer.vma); |
| 889 | i915_gem_object_put(i915->perf.oa.oa_buffer.vma->obj); |
| 890 | |
| 891 | i915->perf.oa.oa_buffer.vma = NULL; |
| 892 | i915->perf.oa.oa_buffer.vaddr = NULL; |
| 893 | |
| 894 | mutex_unlock(&i915->drm.struct_mutex); |
| 895 | } |
| 896 | |
| 897 | static void i915_oa_stream_destroy(struct i915_perf_stream *stream) |
| 898 | { |
| 899 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 900 | |
| 901 | BUG_ON(stream != dev_priv->perf.oa.exclusive_stream); |
| 902 | |
| 903 | dev_priv->perf.oa.ops.disable_metric_set(dev_priv); |
| 904 | |
| 905 | free_oa_buffer(dev_priv); |
| 906 | |
| 907 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 908 | intel_runtime_pm_put(dev_priv); |
| 909 | |
| 910 | if (stream->ctx) |
| 911 | oa_put_render_ctx_id(stream); |
| 912 | |
| 913 | dev_priv->perf.oa.exclusive_stream = NULL; |
| 914 | } |
| 915 | |
| 916 | static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv) |
| 917 | { |
| 918 | u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma); |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 919 | unsigned long flags; |
| 920 | |
| 921 | spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 922 | |
| 923 | /* Pre-DevBDW: OABUFFER must be set with counters off, |
| 924 | * before OASTATUS1, but after OASTATUS2 |
| 925 | */ |
| 926 | I915_WRITE(GEN7_OASTATUS2, gtt_offset | OA_MEM_SELECT_GGTT); /* head */ |
Robert Bragg | f279020 | 2017-05-11 16:43:26 +0100 | [diff] [blame] | 927 | dev_priv->perf.oa.oa_buffer.head = gtt_offset; |
| 928 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 929 | I915_WRITE(GEN7_OABUFFER, gtt_offset); |
Robert Bragg | f279020 | 2017-05-11 16:43:26 +0100 | [diff] [blame] | 930 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 931 | I915_WRITE(GEN7_OASTATUS1, gtt_offset | OABUFFER_SIZE_16M); /* tail */ |
| 932 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 933 | /* Mark that we need updated tail pointers to read from... */ |
| 934 | dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR; |
| 935 | dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR; |
| 936 | |
| 937 | spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags); |
| 938 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 939 | /* On Haswell we have to track which OASTATUS1 flags we've |
| 940 | * already seen since they can't be cleared while periodic |
| 941 | * sampling is enabled. |
| 942 | */ |
| 943 | dev_priv->perf.oa.gen7_latched_oastatus1 = 0; |
| 944 | |
| 945 | /* NB: although the OA buffer will initially be allocated |
| 946 | * zeroed via shmfs (and so this memset is redundant when |
| 947 | * first allocating), we may re-init the OA buffer, either |
| 948 | * when re-enabling a stream or in error/reset paths. |
| 949 | * |
| 950 | * The reason we clear the buffer for each re-init is for the |
| 951 | * sanity check in gen7_append_oa_reports() that looks at the |
| 952 | * report-id field to make sure it's non-zero which relies on |
| 953 | * the assumption that new reports are being written to zeroed |
| 954 | * memory... |
| 955 | */ |
| 956 | memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE); |
| 957 | |
| 958 | /* Maybe make ->pollin per-stream state if we support multiple |
| 959 | * concurrent streams in the future. |
| 960 | */ |
| 961 | dev_priv->perf.oa.pollin = false; |
| 962 | } |
| 963 | |
| 964 | static int alloc_oa_buffer(struct drm_i915_private *dev_priv) |
| 965 | { |
| 966 | struct drm_i915_gem_object *bo; |
| 967 | struct i915_vma *vma; |
| 968 | int ret; |
| 969 | |
| 970 | if (WARN_ON(dev_priv->perf.oa.oa_buffer.vma)) |
| 971 | return -ENODEV; |
| 972 | |
| 973 | ret = i915_mutex_lock_interruptible(&dev_priv->drm); |
| 974 | if (ret) |
| 975 | return ret; |
| 976 | |
| 977 | BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE); |
| 978 | BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M); |
| 979 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 980 | bo = i915_gem_object_create(dev_priv, OA_BUFFER_SIZE); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 981 | if (IS_ERR(bo)) { |
| 982 | DRM_ERROR("Failed to allocate OA buffer\n"); |
| 983 | ret = PTR_ERR(bo); |
| 984 | goto unlock; |
| 985 | } |
| 986 | |
| 987 | ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC); |
| 988 | if (ret) |
| 989 | goto err_unref; |
| 990 | |
| 991 | /* PreHSW required 512K alignment, HSW requires 16M */ |
| 992 | vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0); |
| 993 | if (IS_ERR(vma)) { |
| 994 | ret = PTR_ERR(vma); |
| 995 | goto err_unref; |
| 996 | } |
| 997 | dev_priv->perf.oa.oa_buffer.vma = vma; |
| 998 | |
| 999 | dev_priv->perf.oa.oa_buffer.vaddr = |
| 1000 | i915_gem_object_pin_map(bo, I915_MAP_WB); |
| 1001 | if (IS_ERR(dev_priv->perf.oa.oa_buffer.vaddr)) { |
| 1002 | ret = PTR_ERR(dev_priv->perf.oa.oa_buffer.vaddr); |
| 1003 | goto err_unpin; |
| 1004 | } |
| 1005 | |
| 1006 | dev_priv->perf.oa.ops.init_oa_buffer(dev_priv); |
| 1007 | |
| 1008 | DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p\n", |
| 1009 | i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma), |
| 1010 | dev_priv->perf.oa.oa_buffer.vaddr); |
| 1011 | |
| 1012 | goto unlock; |
| 1013 | |
| 1014 | err_unpin: |
| 1015 | __i915_vma_unpin(vma); |
| 1016 | |
| 1017 | err_unref: |
| 1018 | i915_gem_object_put(bo); |
| 1019 | |
| 1020 | dev_priv->perf.oa.oa_buffer.vaddr = NULL; |
| 1021 | dev_priv->perf.oa.oa_buffer.vma = NULL; |
| 1022 | |
| 1023 | unlock: |
| 1024 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | static void config_oa_regs(struct drm_i915_private *dev_priv, |
| 1029 | const struct i915_oa_reg *regs, |
| 1030 | int n_regs) |
| 1031 | { |
| 1032 | int i; |
| 1033 | |
| 1034 | for (i = 0; i < n_regs; i++) { |
| 1035 | const struct i915_oa_reg *reg = regs + i; |
| 1036 | |
| 1037 | I915_WRITE(reg->addr, reg->value); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | static int hsw_enable_metric_set(struct drm_i915_private *dev_priv) |
| 1042 | { |
| 1043 | int ret = i915_oa_select_metric_set_hsw(dev_priv); |
| 1044 | |
| 1045 | if (ret) |
| 1046 | return ret; |
| 1047 | |
| 1048 | I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) | |
| 1049 | GT_NOA_ENABLE)); |
| 1050 | |
| 1051 | /* PRM: |
| 1052 | * |
| 1053 | * OA unit is using “crclk” for its functionality. When trunk |
| 1054 | * level clock gating takes place, OA clock would be gated, |
| 1055 | * unable to count the events from non-render clock domain. |
| 1056 | * Render clock gating must be disabled when OA is enabled to |
| 1057 | * count the events from non-render domain. Unit level clock |
| 1058 | * gating for RCS should also be disabled. |
| 1059 | */ |
| 1060 | I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) & |
| 1061 | ~GEN7_DOP_CLOCK_GATE_ENABLE)); |
| 1062 | I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) | |
| 1063 | GEN6_CSUNIT_CLOCK_GATE_DISABLE)); |
| 1064 | |
| 1065 | config_oa_regs(dev_priv, dev_priv->perf.oa.mux_regs, |
| 1066 | dev_priv->perf.oa.mux_regs_len); |
| 1067 | |
| 1068 | /* It apparently takes a fairly long time for a new MUX |
| 1069 | * configuration to be be applied after these register writes. |
| 1070 | * This delay duration was derived empirically based on the |
| 1071 | * render_basic config but hopefully it covers the maximum |
| 1072 | * configuration latency. |
| 1073 | * |
| 1074 | * As a fallback, the checks in _append_oa_reports() to skip |
| 1075 | * invalid OA reports do also seem to work to discard reports |
| 1076 | * generated before this config has completed - albeit not |
| 1077 | * silently. |
| 1078 | * |
| 1079 | * Unfortunately this is essentially a magic number, since we |
| 1080 | * don't currently know of a reliable mechanism for predicting |
| 1081 | * how long the MUX config will take to apply and besides |
| 1082 | * seeing invalid reports we don't know of a reliable way to |
| 1083 | * explicitly check that the MUX config has landed. |
| 1084 | * |
| 1085 | * It's even possible we've miss characterized the underlying |
| 1086 | * problem - it just seems like the simplest explanation why |
| 1087 | * a delay at this location would mitigate any invalid reports. |
| 1088 | */ |
| 1089 | usleep_range(15000, 20000); |
| 1090 | |
| 1091 | config_oa_regs(dev_priv, dev_priv->perf.oa.b_counter_regs, |
| 1092 | dev_priv->perf.oa.b_counter_regs_len); |
| 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | static void hsw_disable_metric_set(struct drm_i915_private *dev_priv) |
| 1098 | { |
| 1099 | I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) & |
| 1100 | ~GEN6_CSUNIT_CLOCK_GATE_DISABLE)); |
| 1101 | I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) | |
| 1102 | GEN7_DOP_CLOCK_GATE_ENABLE)); |
| 1103 | |
| 1104 | I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) & |
| 1105 | ~GT_NOA_ENABLE)); |
| 1106 | } |
| 1107 | |
| 1108 | static void gen7_update_oacontrol_locked(struct drm_i915_private *dev_priv) |
| 1109 | { |
Chris Wilson | 6752041 | 2017-03-02 13:28:01 +0000 | [diff] [blame] | 1110 | lockdep_assert_held(&dev_priv->perf.hook_lock); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (dev_priv->perf.oa.exclusive_stream->enabled) { |
| 1113 | struct i915_gem_context *ctx = |
| 1114 | dev_priv->perf.oa.exclusive_stream->ctx; |
| 1115 | u32 ctx_id = dev_priv->perf.oa.specific_ctx_id; |
| 1116 | |
| 1117 | bool periodic = dev_priv->perf.oa.periodic; |
| 1118 | u32 period_exponent = dev_priv->perf.oa.period_exponent; |
| 1119 | u32 report_format = dev_priv->perf.oa.oa_buffer.format; |
| 1120 | |
| 1121 | I915_WRITE(GEN7_OACONTROL, |
| 1122 | (ctx_id & GEN7_OACONTROL_CTX_MASK) | |
| 1123 | (period_exponent << |
| 1124 | GEN7_OACONTROL_TIMER_PERIOD_SHIFT) | |
| 1125 | (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) | |
| 1126 | (report_format << GEN7_OACONTROL_FORMAT_SHIFT) | |
| 1127 | (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) | |
| 1128 | GEN7_OACONTROL_ENABLE); |
| 1129 | } else |
| 1130 | I915_WRITE(GEN7_OACONTROL, 0); |
| 1131 | } |
| 1132 | |
| 1133 | static void gen7_oa_enable(struct drm_i915_private *dev_priv) |
| 1134 | { |
| 1135 | unsigned long flags; |
| 1136 | |
| 1137 | /* Reset buf pointers so we don't forward reports from before now. |
| 1138 | * |
| 1139 | * Think carefully if considering trying to avoid this, since it |
| 1140 | * also ensures status flags and the buffer itself are cleared |
| 1141 | * in error paths, and we have checks for invalid reports based |
| 1142 | * on the assumption that certain fields are written to zeroed |
| 1143 | * memory which this helps maintains. |
| 1144 | */ |
| 1145 | gen7_init_oa_buffer(dev_priv); |
| 1146 | |
| 1147 | spin_lock_irqsave(&dev_priv->perf.hook_lock, flags); |
| 1148 | gen7_update_oacontrol_locked(dev_priv); |
| 1149 | spin_unlock_irqrestore(&dev_priv->perf.hook_lock, flags); |
| 1150 | } |
| 1151 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1152 | /** |
| 1153 | * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream |
| 1154 | * @stream: An i915 perf stream opened for OA metrics |
| 1155 | * |
| 1156 | * [Re]enables hardware periodic sampling according to the period configured |
| 1157 | * when opening the stream. This also starts a hrtimer that will periodically |
| 1158 | * check for data in the circular OA buffer for notifying userspace (e.g. |
| 1159 | * during a read() or poll()). |
| 1160 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1161 | static void i915_oa_stream_enable(struct i915_perf_stream *stream) |
| 1162 | { |
| 1163 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 1164 | |
| 1165 | dev_priv->perf.oa.ops.oa_enable(dev_priv); |
| 1166 | |
| 1167 | if (dev_priv->perf.oa.periodic) |
| 1168 | hrtimer_start(&dev_priv->perf.oa.poll_check_timer, |
| 1169 | ns_to_ktime(POLL_PERIOD), |
| 1170 | HRTIMER_MODE_REL_PINNED); |
| 1171 | } |
| 1172 | |
| 1173 | static void gen7_oa_disable(struct drm_i915_private *dev_priv) |
| 1174 | { |
| 1175 | I915_WRITE(GEN7_OACONTROL, 0); |
| 1176 | } |
| 1177 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1178 | /** |
| 1179 | * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream |
| 1180 | * @stream: An i915 perf stream opened for OA metrics |
| 1181 | * |
| 1182 | * Stops the OA unit from periodically writing counter reports into the |
| 1183 | * circular OA buffer. This also stops the hrtimer that periodically checks for |
| 1184 | * data in the circular OA buffer, for notifying userspace. |
| 1185 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1186 | static void i915_oa_stream_disable(struct i915_perf_stream *stream) |
| 1187 | { |
| 1188 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 1189 | |
| 1190 | dev_priv->perf.oa.ops.oa_disable(dev_priv); |
| 1191 | |
| 1192 | if (dev_priv->perf.oa.periodic) |
| 1193 | hrtimer_cancel(&dev_priv->perf.oa.poll_check_timer); |
| 1194 | } |
| 1195 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1196 | static const struct i915_perf_stream_ops i915_oa_stream_ops = { |
| 1197 | .destroy = i915_oa_stream_destroy, |
| 1198 | .enable = i915_oa_stream_enable, |
| 1199 | .disable = i915_oa_stream_disable, |
| 1200 | .wait_unlocked = i915_oa_wait_unlocked, |
| 1201 | .poll_wait = i915_oa_poll_wait, |
| 1202 | .read = i915_oa_read, |
| 1203 | }; |
| 1204 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1205 | /** |
| 1206 | * i915_oa_stream_init - validate combined props for OA stream and init |
| 1207 | * @stream: An i915 perf stream |
| 1208 | * @param: The open parameters passed to `DRM_I915_PERF_OPEN` |
| 1209 | * @props: The property state that configures stream (individually validated) |
| 1210 | * |
| 1211 | * While read_properties_unlocked() validates properties in isolation it |
| 1212 | * doesn't ensure that the combination necessarily makes sense. |
| 1213 | * |
| 1214 | * At this point it has been determined that userspace wants a stream of |
| 1215 | * OA metrics, but still we need to further validate the combined |
| 1216 | * properties are OK. |
| 1217 | * |
| 1218 | * If the configuration makes sense then we can allocate memory for |
| 1219 | * a circular OA buffer and apply the requested metric set configuration. |
| 1220 | * |
| 1221 | * Returns: zero on success or a negative error code. |
| 1222 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1223 | static int i915_oa_stream_init(struct i915_perf_stream *stream, |
| 1224 | struct drm_i915_perf_open_param *param, |
| 1225 | struct perf_open_properties *props) |
| 1226 | { |
| 1227 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 1228 | int format_size; |
| 1229 | int ret; |
| 1230 | |
Robert Bragg | 442b8c0 | 2016-11-07 19:49:53 +0000 | [diff] [blame] | 1231 | /* If the sysfs metrics/ directory wasn't registered for some |
| 1232 | * reason then don't let userspace try their luck with config |
| 1233 | * IDs |
| 1234 | */ |
| 1235 | if (!dev_priv->perf.metrics_kobj) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1236 | DRM_DEBUG("OA metrics weren't advertised via sysfs\n"); |
Robert Bragg | 442b8c0 | 2016-11-07 19:49:53 +0000 | [diff] [blame] | 1237 | return -EINVAL; |
| 1238 | } |
| 1239 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1240 | if (!(props->sample_flags & SAMPLE_OA_REPORT)) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1241 | DRM_DEBUG("Only OA report sampling supported\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1242 | return -EINVAL; |
| 1243 | } |
| 1244 | |
| 1245 | if (!dev_priv->perf.oa.ops.init_oa_buffer) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1246 | DRM_DEBUG("OA unit not supported\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1247 | return -ENODEV; |
| 1248 | } |
| 1249 | |
| 1250 | /* To avoid the complexity of having to accurately filter |
| 1251 | * counter reports and marshal to the appropriate client |
| 1252 | * we currently only allow exclusive access |
| 1253 | */ |
| 1254 | if (dev_priv->perf.oa.exclusive_stream) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1255 | DRM_DEBUG("OA unit already in use\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1256 | return -EBUSY; |
| 1257 | } |
| 1258 | |
| 1259 | if (!props->metrics_set) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1260 | DRM_DEBUG("OA metric set not specified\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1261 | return -EINVAL; |
| 1262 | } |
| 1263 | |
| 1264 | if (!props->oa_format) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1265 | DRM_DEBUG("OA report format not specified\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1266 | return -EINVAL; |
| 1267 | } |
| 1268 | |
| 1269 | stream->sample_size = sizeof(struct drm_i915_perf_record_header); |
| 1270 | |
| 1271 | format_size = dev_priv->perf.oa.oa_formats[props->oa_format].size; |
| 1272 | |
| 1273 | stream->sample_flags |= SAMPLE_OA_REPORT; |
| 1274 | stream->sample_size += format_size; |
| 1275 | |
| 1276 | dev_priv->perf.oa.oa_buffer.format_size = format_size; |
| 1277 | if (WARN_ON(dev_priv->perf.oa.oa_buffer.format_size == 0)) |
| 1278 | return -EINVAL; |
| 1279 | |
| 1280 | dev_priv->perf.oa.oa_buffer.format = |
| 1281 | dev_priv->perf.oa.oa_formats[props->oa_format].format; |
| 1282 | |
| 1283 | dev_priv->perf.oa.metrics_set = props->metrics_set; |
| 1284 | |
| 1285 | dev_priv->perf.oa.periodic = props->oa_periodic; |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 1286 | if (dev_priv->perf.oa.periodic) |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1287 | dev_priv->perf.oa.period_exponent = props->oa_period_exponent; |
| 1288 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1289 | if (stream->ctx) { |
| 1290 | ret = oa_get_render_ctx_id(stream); |
| 1291 | if (ret) |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
| 1295 | ret = alloc_oa_buffer(dev_priv); |
| 1296 | if (ret) |
| 1297 | goto err_oa_buf_alloc; |
| 1298 | |
| 1299 | /* PRM - observability performance counters: |
| 1300 | * |
| 1301 | * OACONTROL, performance counter enable, note: |
| 1302 | * |
| 1303 | * "When this bit is set, in order to have coherent counts, |
| 1304 | * RC6 power state and trunk clock gating must be disabled. |
| 1305 | * This can be achieved by programming MMIO registers as |
| 1306 | * 0xA094=0 and 0xA090[31]=1" |
| 1307 | * |
| 1308 | * In our case we are expecting that taking pm + FORCEWAKE |
| 1309 | * references will effectively disable RC6. |
| 1310 | */ |
| 1311 | intel_runtime_pm_get(dev_priv); |
| 1312 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 1313 | |
| 1314 | ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv); |
| 1315 | if (ret) |
| 1316 | goto err_enable; |
| 1317 | |
| 1318 | stream->ops = &i915_oa_stream_ops; |
| 1319 | |
| 1320 | dev_priv->perf.oa.exclusive_stream = stream; |
| 1321 | |
| 1322 | return 0; |
| 1323 | |
| 1324 | err_enable: |
| 1325 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 1326 | intel_runtime_pm_put(dev_priv); |
| 1327 | free_oa_buffer(dev_priv); |
| 1328 | |
| 1329 | err_oa_buf_alloc: |
| 1330 | if (stream->ctx) |
| 1331 | oa_put_render_ctx_id(stream); |
| 1332 | |
| 1333 | return ret; |
| 1334 | } |
| 1335 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1336 | /** |
| 1337 | * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation |
| 1338 | * @stream: An i915 perf stream |
| 1339 | * @file: An i915 perf stream file |
| 1340 | * @buf: destination buffer given by userspace |
| 1341 | * @count: the number of bytes userspace wants to read |
| 1342 | * @ppos: (inout) file seek position (unused) |
| 1343 | * |
| 1344 | * Besides wrapping &i915_perf_stream_ops->read this provides a common place to |
| 1345 | * ensure that if we've successfully copied any data then reporting that takes |
| 1346 | * precedence over any internal error status, so the data isn't lost. |
| 1347 | * |
| 1348 | * For example ret will be -ENOSPC whenever there is more buffered data than |
| 1349 | * can be copied to userspace, but that's only interesting if we weren't able |
| 1350 | * to copy some data because it implies the userspace buffer is too small to |
| 1351 | * receive a single record (and we never split records). |
| 1352 | * |
| 1353 | * Another case with ret == -EFAULT is more of a grey area since it would seem |
| 1354 | * like bad form for userspace to ask us to overrun its buffer, but the user |
| 1355 | * knows best: |
| 1356 | * |
| 1357 | * http://yarchive.net/comp/linux/partial_reads_writes.html |
| 1358 | * |
| 1359 | * Returns: The number of bytes copied or a negative error code on failure. |
| 1360 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1361 | static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream, |
| 1362 | struct file *file, |
| 1363 | char __user *buf, |
| 1364 | size_t count, |
| 1365 | loff_t *ppos) |
| 1366 | { |
| 1367 | /* Note we keep the offset (aka bytes read) separate from any |
| 1368 | * error status so that the final check for whether we return |
| 1369 | * the bytes read with a higher precedence than any error (see |
| 1370 | * comment below) doesn't need to be handled/duplicated in |
| 1371 | * stream->ops->read() implementations. |
| 1372 | */ |
| 1373 | size_t offset = 0; |
| 1374 | int ret = stream->ops->read(stream, buf, count, &offset); |
| 1375 | |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1376 | return offset ?: (ret ?: -EAGAIN); |
| 1377 | } |
| 1378 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1379 | /** |
| 1380 | * i915_perf_read - handles read() FOP for i915 perf stream FDs |
| 1381 | * @file: An i915 perf stream file |
| 1382 | * @buf: destination buffer given by userspace |
| 1383 | * @count: the number of bytes userspace wants to read |
| 1384 | * @ppos: (inout) file seek position (unused) |
| 1385 | * |
| 1386 | * The entry point for handling a read() on a stream file descriptor from |
| 1387 | * userspace. Most of the work is left to the i915_perf_read_locked() and |
| 1388 | * &i915_perf_stream_ops->read but to save having stream implementations (of |
| 1389 | * which we might have multiple later) we handle blocking read here. |
| 1390 | * |
| 1391 | * We can also consistently treat trying to read from a disabled stream |
| 1392 | * as an IO error so implementations can assume the stream is enabled |
| 1393 | * while reading. |
| 1394 | * |
| 1395 | * Returns: The number of bytes copied or a negative error code on failure. |
| 1396 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1397 | static ssize_t i915_perf_read(struct file *file, |
| 1398 | char __user *buf, |
| 1399 | size_t count, |
| 1400 | loff_t *ppos) |
| 1401 | { |
| 1402 | struct i915_perf_stream *stream = file->private_data; |
| 1403 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 1404 | ssize_t ret; |
| 1405 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1406 | /* To ensure it's handled consistently we simply treat all reads of a |
| 1407 | * disabled stream as an error. In particular it might otherwise lead |
| 1408 | * to a deadlock for blocking file descriptors... |
| 1409 | */ |
| 1410 | if (!stream->enabled) |
| 1411 | return -EIO; |
| 1412 | |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1413 | if (!(file->f_flags & O_NONBLOCK)) { |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1414 | /* There's the small chance of false positives from |
| 1415 | * stream->ops->wait_unlocked. |
| 1416 | * |
| 1417 | * E.g. with single context filtering since we only wait until |
| 1418 | * oabuffer has >= 1 report we don't immediately know whether |
| 1419 | * any reports really belong to the current context |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1420 | */ |
| 1421 | do { |
| 1422 | ret = stream->ops->wait_unlocked(stream); |
| 1423 | if (ret) |
| 1424 | return ret; |
| 1425 | |
| 1426 | mutex_lock(&dev_priv->perf.lock); |
| 1427 | ret = i915_perf_read_locked(stream, file, |
| 1428 | buf, count, ppos); |
| 1429 | mutex_unlock(&dev_priv->perf.lock); |
| 1430 | } while (ret == -EAGAIN); |
| 1431 | } else { |
| 1432 | mutex_lock(&dev_priv->perf.lock); |
| 1433 | ret = i915_perf_read_locked(stream, file, buf, count, ppos); |
| 1434 | mutex_unlock(&dev_priv->perf.lock); |
| 1435 | } |
| 1436 | |
Robert Bragg | 26ebd9c | 2017-05-11 16:43:25 +0100 | [diff] [blame] | 1437 | /* We allow the poll checking to sometimes report false positive POLLIN |
| 1438 | * events where we might actually report EAGAIN on read() if there's |
| 1439 | * not really any data available. In this situation though we don't |
| 1440 | * want to enter a busy loop between poll() reporting a POLLIN event |
| 1441 | * and read() returning -EAGAIN. Clearing the oa.pollin state here |
| 1442 | * effectively ensures we back off until the next hrtimer callback |
| 1443 | * before reporting another POLLIN event. |
| 1444 | */ |
| 1445 | if (ret >= 0 || ret == -EAGAIN) { |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1446 | /* Maybe make ->pollin per-stream state if we support multiple |
| 1447 | * concurrent streams in the future. |
| 1448 | */ |
| 1449 | dev_priv->perf.oa.pollin = false; |
| 1450 | } |
| 1451 | |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1452 | return ret; |
| 1453 | } |
| 1454 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1455 | static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer) |
| 1456 | { |
| 1457 | struct drm_i915_private *dev_priv = |
| 1458 | container_of(hrtimer, typeof(*dev_priv), |
| 1459 | perf.oa.poll_check_timer); |
| 1460 | |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 1461 | if (dev_priv->perf.oa.ops.oa_buffer_check(dev_priv)) { |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1462 | dev_priv->perf.oa.pollin = true; |
| 1463 | wake_up(&dev_priv->perf.oa.poll_wq); |
| 1464 | } |
| 1465 | |
| 1466 | hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD)); |
| 1467 | |
| 1468 | return HRTIMER_RESTART; |
| 1469 | } |
| 1470 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1471 | /** |
| 1472 | * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream |
| 1473 | * @dev_priv: i915 device instance |
| 1474 | * @stream: An i915 perf stream |
| 1475 | * @file: An i915 perf stream file |
| 1476 | * @wait: poll() state table |
| 1477 | * |
| 1478 | * For handling userspace polling on an i915 perf stream, this calls through to |
| 1479 | * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that |
| 1480 | * will be woken for new stream data. |
| 1481 | * |
| 1482 | * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize |
| 1483 | * with any non-file-operation driver hooks. |
| 1484 | * |
| 1485 | * Returns: any poll events that are ready without sleeping |
| 1486 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1487 | static unsigned int i915_perf_poll_locked(struct drm_i915_private *dev_priv, |
| 1488 | struct i915_perf_stream *stream, |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1489 | struct file *file, |
| 1490 | poll_table *wait) |
| 1491 | { |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1492 | unsigned int events = 0; |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1493 | |
| 1494 | stream->ops->poll_wait(stream, file, wait); |
| 1495 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1496 | /* Note: we don't explicitly check whether there's something to read |
| 1497 | * here since this path may be very hot depending on what else |
| 1498 | * userspace is polling, or on the timeout in use. We rely solely on |
| 1499 | * the hrtimer/oa_poll_check_timer_cb to notify us when there are |
| 1500 | * samples to read. |
| 1501 | */ |
| 1502 | if (dev_priv->perf.oa.pollin) |
| 1503 | events |= POLLIN; |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1504 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1505 | return events; |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1508 | /** |
| 1509 | * i915_perf_poll - call poll_wait() with a suitable wait queue for stream |
| 1510 | * @file: An i915 perf stream file |
| 1511 | * @wait: poll() state table |
| 1512 | * |
| 1513 | * For handling userspace polling on an i915 perf stream, this ensures |
| 1514 | * poll_wait() gets called with a wait queue that will be woken for new stream |
| 1515 | * data. |
| 1516 | * |
| 1517 | * Note: Implementation deferred to i915_perf_poll_locked() |
| 1518 | * |
| 1519 | * Returns: any poll events that are ready without sleeping |
| 1520 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1521 | static unsigned int i915_perf_poll(struct file *file, poll_table *wait) |
| 1522 | { |
| 1523 | struct i915_perf_stream *stream = file->private_data; |
| 1524 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 1525 | int ret; |
| 1526 | |
| 1527 | mutex_lock(&dev_priv->perf.lock); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1528 | ret = i915_perf_poll_locked(dev_priv, stream, file, wait); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1529 | mutex_unlock(&dev_priv->perf.lock); |
| 1530 | |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1534 | /** |
| 1535 | * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl |
| 1536 | * @stream: A disabled i915 perf stream |
| 1537 | * |
| 1538 | * [Re]enables the associated capture of data for this stream. |
| 1539 | * |
| 1540 | * If a stream was previously enabled then there's currently no intention |
| 1541 | * to provide userspace any guarantee about the preservation of previously |
| 1542 | * buffered data. |
| 1543 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1544 | static void i915_perf_enable_locked(struct i915_perf_stream *stream) |
| 1545 | { |
| 1546 | if (stream->enabled) |
| 1547 | return; |
| 1548 | |
| 1549 | /* Allow stream->ops->enable() to refer to this */ |
| 1550 | stream->enabled = true; |
| 1551 | |
| 1552 | if (stream->ops->enable) |
| 1553 | stream->ops->enable(stream); |
| 1554 | } |
| 1555 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1556 | /** |
| 1557 | * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl |
| 1558 | * @stream: An enabled i915 perf stream |
| 1559 | * |
| 1560 | * Disables the associated capture of data for this stream. |
| 1561 | * |
| 1562 | * The intention is that disabling an re-enabling a stream will ideally be |
| 1563 | * cheaper than destroying and re-opening a stream with the same configuration, |
| 1564 | * though there are no formal guarantees about what state or buffered data |
| 1565 | * must be retained between disabling and re-enabling a stream. |
| 1566 | * |
| 1567 | * Note: while a stream is disabled it's considered an error for userspace |
| 1568 | * to attempt to read from the stream (-EIO). |
| 1569 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1570 | static void i915_perf_disable_locked(struct i915_perf_stream *stream) |
| 1571 | { |
| 1572 | if (!stream->enabled) |
| 1573 | return; |
| 1574 | |
| 1575 | /* Allow stream->ops->disable() to refer to this */ |
| 1576 | stream->enabled = false; |
| 1577 | |
| 1578 | if (stream->ops->disable) |
| 1579 | stream->ops->disable(stream); |
| 1580 | } |
| 1581 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1582 | /** |
| 1583 | * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs |
| 1584 | * @stream: An i915 perf stream |
| 1585 | * @cmd: the ioctl request |
| 1586 | * @arg: the ioctl data |
| 1587 | * |
| 1588 | * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize |
| 1589 | * with any non-file-operation driver hooks. |
| 1590 | * |
| 1591 | * Returns: zero on success or a negative error code. Returns -EINVAL for |
| 1592 | * an unknown ioctl request. |
| 1593 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1594 | static long i915_perf_ioctl_locked(struct i915_perf_stream *stream, |
| 1595 | unsigned int cmd, |
| 1596 | unsigned long arg) |
| 1597 | { |
| 1598 | switch (cmd) { |
| 1599 | case I915_PERF_IOCTL_ENABLE: |
| 1600 | i915_perf_enable_locked(stream); |
| 1601 | return 0; |
| 1602 | case I915_PERF_IOCTL_DISABLE: |
| 1603 | i915_perf_disable_locked(stream); |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | return -EINVAL; |
| 1608 | } |
| 1609 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1610 | /** |
| 1611 | * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs |
| 1612 | * @file: An i915 perf stream file |
| 1613 | * @cmd: the ioctl request |
| 1614 | * @arg: the ioctl data |
| 1615 | * |
| 1616 | * Implementation deferred to i915_perf_ioctl_locked(). |
| 1617 | * |
| 1618 | * Returns: zero on success or a negative error code. Returns -EINVAL for |
| 1619 | * an unknown ioctl request. |
| 1620 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1621 | static long i915_perf_ioctl(struct file *file, |
| 1622 | unsigned int cmd, |
| 1623 | unsigned long arg) |
| 1624 | { |
| 1625 | struct i915_perf_stream *stream = file->private_data; |
| 1626 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 1627 | long ret; |
| 1628 | |
| 1629 | mutex_lock(&dev_priv->perf.lock); |
| 1630 | ret = i915_perf_ioctl_locked(stream, cmd, arg); |
| 1631 | mutex_unlock(&dev_priv->perf.lock); |
| 1632 | |
| 1633 | return ret; |
| 1634 | } |
| 1635 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1636 | /** |
| 1637 | * i915_perf_destroy_locked - destroy an i915 perf stream |
| 1638 | * @stream: An i915 perf stream |
| 1639 | * |
| 1640 | * Frees all resources associated with the given i915 perf @stream, disabling |
| 1641 | * any associated data capture in the process. |
| 1642 | * |
| 1643 | * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize |
| 1644 | * with any non-file-operation driver hooks. |
| 1645 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1646 | static void i915_perf_destroy_locked(struct i915_perf_stream *stream) |
| 1647 | { |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1648 | if (stream->enabled) |
| 1649 | i915_perf_disable_locked(stream); |
| 1650 | |
| 1651 | if (stream->ops->destroy) |
| 1652 | stream->ops->destroy(stream); |
| 1653 | |
| 1654 | list_del(&stream->link); |
| 1655 | |
Chris Wilson | 69df05e | 2016-12-18 15:37:21 +0000 | [diff] [blame] | 1656 | if (stream->ctx) |
| 1657 | i915_gem_context_put_unlocked(stream->ctx); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1658 | |
| 1659 | kfree(stream); |
| 1660 | } |
| 1661 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1662 | /** |
| 1663 | * i915_perf_release - handles userspace close() of a stream file |
| 1664 | * @inode: anonymous inode associated with file |
| 1665 | * @file: An i915 perf stream file |
| 1666 | * |
| 1667 | * Cleans up any resources associated with an open i915 perf stream file. |
| 1668 | * |
| 1669 | * NB: close() can't really fail from the userspace point of view. |
| 1670 | * |
| 1671 | * Returns: zero on success or a negative error code. |
| 1672 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1673 | static int i915_perf_release(struct inode *inode, struct file *file) |
| 1674 | { |
| 1675 | struct i915_perf_stream *stream = file->private_data; |
| 1676 | struct drm_i915_private *dev_priv = stream->dev_priv; |
| 1677 | |
| 1678 | mutex_lock(&dev_priv->perf.lock); |
| 1679 | i915_perf_destroy_locked(stream); |
| 1680 | mutex_unlock(&dev_priv->perf.lock); |
| 1681 | |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
| 1685 | |
| 1686 | static const struct file_operations fops = { |
| 1687 | .owner = THIS_MODULE, |
| 1688 | .llseek = no_llseek, |
| 1689 | .release = i915_perf_release, |
| 1690 | .poll = i915_perf_poll, |
| 1691 | .read = i915_perf_read, |
| 1692 | .unlocked_ioctl = i915_perf_ioctl, |
| 1693 | }; |
| 1694 | |
| 1695 | |
| 1696 | static struct i915_gem_context * |
| 1697 | lookup_context(struct drm_i915_private *dev_priv, |
| 1698 | struct drm_i915_file_private *file_priv, |
| 1699 | u32 ctx_user_handle) |
| 1700 | { |
| 1701 | struct i915_gem_context *ctx; |
| 1702 | int ret; |
| 1703 | |
| 1704 | ret = i915_mutex_lock_interruptible(&dev_priv->drm); |
| 1705 | if (ret) |
| 1706 | return ERR_PTR(ret); |
| 1707 | |
| 1708 | ctx = i915_gem_context_lookup(file_priv, ctx_user_handle); |
| 1709 | if (!IS_ERR(ctx)) |
| 1710 | i915_gem_context_get(ctx); |
| 1711 | |
| 1712 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 1713 | |
| 1714 | return ctx; |
| 1715 | } |
| 1716 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1717 | /** |
| 1718 | * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD |
| 1719 | * @dev_priv: i915 device instance |
| 1720 | * @param: The open parameters passed to 'DRM_I915_PERF_OPEN` |
| 1721 | * @props: individually validated u64 property value pairs |
| 1722 | * @file: drm file |
| 1723 | * |
| 1724 | * See i915_perf_ioctl_open() for interface details. |
| 1725 | * |
| 1726 | * Implements further stream config validation and stream initialization on |
| 1727 | * behalf of i915_perf_open_ioctl() with the &drm_i915_private->perf.lock mutex |
| 1728 | * taken to serialize with any non-file-operation driver hooks. |
| 1729 | * |
| 1730 | * Note: at this point the @props have only been validated in isolation and |
| 1731 | * it's still necessary to validate that the combination of properties makes |
| 1732 | * sense. |
| 1733 | * |
| 1734 | * In the case where userspace is interested in OA unit metrics then further |
| 1735 | * config validation and stream initialization details will be handled by |
| 1736 | * i915_oa_stream_init(). The code here should only validate config state that |
| 1737 | * will be relevant to all stream types / backends. |
| 1738 | * |
| 1739 | * Returns: zero on success or a negative error code. |
| 1740 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1741 | static int |
| 1742 | i915_perf_open_ioctl_locked(struct drm_i915_private *dev_priv, |
| 1743 | struct drm_i915_perf_open_param *param, |
| 1744 | struct perf_open_properties *props, |
| 1745 | struct drm_file *file) |
| 1746 | { |
| 1747 | struct i915_gem_context *specific_ctx = NULL; |
| 1748 | struct i915_perf_stream *stream = NULL; |
| 1749 | unsigned long f_flags = 0; |
| 1750 | int stream_fd; |
| 1751 | int ret; |
| 1752 | |
| 1753 | if (props->single_context) { |
| 1754 | u32 ctx_handle = props->ctx_handle; |
| 1755 | struct drm_i915_file_private *file_priv = file->driver_priv; |
| 1756 | |
| 1757 | specific_ctx = lookup_context(dev_priv, file_priv, ctx_handle); |
| 1758 | if (IS_ERR(specific_ctx)) { |
| 1759 | ret = PTR_ERR(specific_ctx); |
| 1760 | if (ret != -EINTR) |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1761 | DRM_DEBUG("Failed to look up context with ID %u for opening perf stream\n", |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1762 | ctx_handle); |
| 1763 | goto err; |
| 1764 | } |
| 1765 | } |
| 1766 | |
Robert Bragg | ccdf634 | 2016-11-07 19:49:54 +0000 | [diff] [blame] | 1767 | /* Similar to perf's kernel.perf_paranoid_cpu sysctl option |
| 1768 | * we check a dev.i915.perf_stream_paranoid sysctl option |
| 1769 | * to determine if it's ok to access system wide OA counters |
| 1770 | * without CAP_SYS_ADMIN privileges. |
| 1771 | */ |
| 1772 | if (!specific_ctx && |
| 1773 | i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1774 | DRM_DEBUG("Insufficient privileges to open system-wide i915 perf stream\n"); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1775 | ret = -EACCES; |
| 1776 | goto err_ctx; |
| 1777 | } |
| 1778 | |
| 1779 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 1780 | if (!stream) { |
| 1781 | ret = -ENOMEM; |
| 1782 | goto err_ctx; |
| 1783 | } |
| 1784 | |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1785 | stream->dev_priv = dev_priv; |
| 1786 | stream->ctx = specific_ctx; |
| 1787 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1788 | ret = i915_oa_stream_init(stream, param, props); |
| 1789 | if (ret) |
| 1790 | goto err_alloc; |
| 1791 | |
| 1792 | /* we avoid simply assigning stream->sample_flags = props->sample_flags |
| 1793 | * to have _stream_init check the combination of sample flags more |
| 1794 | * thoroughly, but still this is the expected result at this point. |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1795 | */ |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1796 | if (WARN_ON(stream->sample_flags != props->sample_flags)) { |
| 1797 | ret = -ENODEV; |
Matthew Auld | 22f880c | 2017-03-27 21:34:59 +0100 | [diff] [blame] | 1798 | goto err_flags; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1799 | } |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1800 | |
| 1801 | list_add(&stream->link, &dev_priv->perf.streams); |
| 1802 | |
| 1803 | if (param->flags & I915_PERF_FLAG_FD_CLOEXEC) |
| 1804 | f_flags |= O_CLOEXEC; |
| 1805 | if (param->flags & I915_PERF_FLAG_FD_NONBLOCK) |
| 1806 | f_flags |= O_NONBLOCK; |
| 1807 | |
| 1808 | stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags); |
| 1809 | if (stream_fd < 0) { |
| 1810 | ret = stream_fd; |
| 1811 | goto err_open; |
| 1812 | } |
| 1813 | |
| 1814 | if (!(param->flags & I915_PERF_FLAG_DISABLED)) |
| 1815 | i915_perf_enable_locked(stream); |
| 1816 | |
| 1817 | return stream_fd; |
| 1818 | |
| 1819 | err_open: |
| 1820 | list_del(&stream->link); |
Matthew Auld | 22f880c | 2017-03-27 21:34:59 +0100 | [diff] [blame] | 1821 | err_flags: |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1822 | if (stream->ops->destroy) |
| 1823 | stream->ops->destroy(stream); |
| 1824 | err_alloc: |
| 1825 | kfree(stream); |
| 1826 | err_ctx: |
Chris Wilson | 69df05e | 2016-12-18 15:37:21 +0000 | [diff] [blame] | 1827 | if (specific_ctx) |
| 1828 | i915_gem_context_put_unlocked(specific_ctx); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1829 | err: |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1833 | /** |
| 1834 | * read_properties_unlocked - validate + copy userspace stream open properties |
| 1835 | * @dev_priv: i915 device instance |
| 1836 | * @uprops: The array of u64 key value pairs given by userspace |
| 1837 | * @n_props: The number of key value pairs expected in @uprops |
| 1838 | * @props: The stream configuration built up while validating properties |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1839 | * |
| 1840 | * Note this function only validates properties in isolation it doesn't |
| 1841 | * validate that the combination of properties makes sense or that all |
| 1842 | * properties necessary for a particular kind of stream have been set. |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1843 | * |
| 1844 | * Note that there currently aren't any ordering requirements for properties so |
| 1845 | * we shouldn't validate or assume anything about ordering here. This doesn't |
| 1846 | * rule out defining new properties with ordering requirements in the future. |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1847 | */ |
| 1848 | static int read_properties_unlocked(struct drm_i915_private *dev_priv, |
| 1849 | u64 __user *uprops, |
| 1850 | u32 n_props, |
| 1851 | struct perf_open_properties *props) |
| 1852 | { |
| 1853 | u64 __user *uprop = uprops; |
| 1854 | int i; |
| 1855 | |
| 1856 | memset(props, 0, sizeof(struct perf_open_properties)); |
| 1857 | |
| 1858 | if (!n_props) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1859 | DRM_DEBUG("No i915 perf properties given\n"); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1860 | return -EINVAL; |
| 1861 | } |
| 1862 | |
| 1863 | /* Considering that ID = 0 is reserved and assuming that we don't |
| 1864 | * (currently) expect any configurations to ever specify duplicate |
| 1865 | * values for a particular property ID then the last _PROP_MAX value is |
| 1866 | * one greater than the maximum number of properties we expect to get |
| 1867 | * from userspace. |
| 1868 | */ |
| 1869 | if (n_props >= DRM_I915_PERF_PROP_MAX) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1870 | DRM_DEBUG("More i915 perf properties specified than exist\n"); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1871 | return -EINVAL; |
| 1872 | } |
| 1873 | |
| 1874 | for (i = 0; i < n_props; i++) { |
Robert Bragg | 00319ba | 2016-11-07 19:49:55 +0000 | [diff] [blame] | 1875 | u64 oa_period, oa_freq_hz; |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1876 | u64 id, value; |
| 1877 | int ret; |
| 1878 | |
| 1879 | ret = get_user(id, uprop); |
| 1880 | if (ret) |
| 1881 | return ret; |
| 1882 | |
| 1883 | ret = get_user(value, uprop + 1); |
| 1884 | if (ret) |
| 1885 | return ret; |
| 1886 | |
Matthew Auld | 0a309f9 | 2017-03-27 21:32:36 +0100 | [diff] [blame] | 1887 | if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) { |
| 1888 | DRM_DEBUG("Unknown i915 perf property ID\n"); |
| 1889 | return -EINVAL; |
| 1890 | } |
| 1891 | |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1892 | switch ((enum drm_i915_perf_property_id)id) { |
| 1893 | case DRM_I915_PERF_PROP_CTX_HANDLE: |
| 1894 | props->single_context = 1; |
| 1895 | props->ctx_handle = value; |
| 1896 | break; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1897 | case DRM_I915_PERF_PROP_SAMPLE_OA: |
| 1898 | props->sample_flags |= SAMPLE_OA_REPORT; |
| 1899 | break; |
| 1900 | case DRM_I915_PERF_PROP_OA_METRICS_SET: |
| 1901 | if (value == 0 || |
| 1902 | value > dev_priv->perf.oa.n_builtin_sets) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1903 | DRM_DEBUG("Unknown OA metric set ID\n"); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1904 | return -EINVAL; |
| 1905 | } |
| 1906 | props->metrics_set = value; |
| 1907 | break; |
| 1908 | case DRM_I915_PERF_PROP_OA_FORMAT: |
| 1909 | if (value == 0 || value >= I915_OA_FORMAT_MAX) { |
Robert Bragg | 52c57c2 | 2017-05-11 16:43:29 +0100 | [diff] [blame] | 1910 | DRM_DEBUG("Out-of-range OA report format %llu\n", |
| 1911 | value); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1912 | return -EINVAL; |
| 1913 | } |
| 1914 | if (!dev_priv->perf.oa.oa_formats[value].size) { |
Robert Bragg | 52c57c2 | 2017-05-11 16:43:29 +0100 | [diff] [blame] | 1915 | DRM_DEBUG("Unsupported OA report format %llu\n", |
| 1916 | value); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1917 | return -EINVAL; |
| 1918 | } |
| 1919 | props->oa_format = value; |
| 1920 | break; |
| 1921 | case DRM_I915_PERF_PROP_OA_EXPONENT: |
| 1922 | if (value > OA_EXPONENT_MAX) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1923 | DRM_DEBUG("OA timer exponent too high (> %u)\n", |
| 1924 | OA_EXPONENT_MAX); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1925 | return -EINVAL; |
| 1926 | } |
| 1927 | |
Robert Bragg | 00319ba | 2016-11-07 19:49:55 +0000 | [diff] [blame] | 1928 | /* Theoretically we can program the OA unit to sample |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1929 | * every 160ns but don't allow that by default unless |
| 1930 | * root. |
| 1931 | * |
Robert Bragg | 00319ba | 2016-11-07 19:49:55 +0000 | [diff] [blame] | 1932 | * On Haswell the period is derived from the exponent |
| 1933 | * as: |
| 1934 | * |
| 1935 | * period = 80ns * 2^(exponent + 1) |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1936 | */ |
Robert Bragg | 00319ba | 2016-11-07 19:49:55 +0000 | [diff] [blame] | 1937 | BUILD_BUG_ON(sizeof(oa_period) != 8); |
| 1938 | oa_period = 80ull * (2ull << value); |
| 1939 | |
| 1940 | /* This check is primarily to ensure that oa_period <= |
| 1941 | * UINT32_MAX (before passing to do_div which only |
| 1942 | * accepts a u32 denominator), but we can also skip |
| 1943 | * checking anything < 1Hz which implicitly can't be |
| 1944 | * limited via an integer oa_max_sample_rate. |
| 1945 | */ |
| 1946 | if (oa_period <= NSEC_PER_SEC) { |
| 1947 | u64 tmp = NSEC_PER_SEC; |
| 1948 | do_div(tmp, oa_period); |
| 1949 | oa_freq_hz = tmp; |
| 1950 | } else |
| 1951 | oa_freq_hz = 0; |
| 1952 | |
| 1953 | if (oa_freq_hz > i915_oa_max_sample_rate && |
| 1954 | !capable(CAP_SYS_ADMIN)) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 1955 | 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] | 1956 | i915_oa_max_sample_rate); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 1957 | return -EACCES; |
| 1958 | } |
| 1959 | |
| 1960 | props->oa_periodic = true; |
| 1961 | props->oa_period_exponent = value; |
| 1962 | break; |
Matthew Auld | 0a309f9 | 2017-03-27 21:32:36 +0100 | [diff] [blame] | 1963 | case DRM_I915_PERF_PROP_MAX: |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1964 | MISSING_CASE(id); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1965 | return -EINVAL; |
| 1966 | } |
| 1967 | |
| 1968 | uprop += 2; |
| 1969 | } |
| 1970 | |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 1974 | /** |
| 1975 | * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD |
| 1976 | * @dev: drm device |
| 1977 | * @data: ioctl data copied from userspace (unvalidated) |
| 1978 | * @file: drm file |
| 1979 | * |
| 1980 | * Validates the stream open parameters given by userspace including flags |
| 1981 | * and an array of u64 key, value pair properties. |
| 1982 | * |
| 1983 | * Very little is assumed up front about the nature of the stream being |
| 1984 | * opened (for instance we don't assume it's for periodic OA unit metrics). An |
| 1985 | * i915-perf stream is expected to be a suitable interface for other forms of |
| 1986 | * buffered data written by the GPU besides periodic OA metrics. |
| 1987 | * |
| 1988 | * Note we copy the properties from userspace outside of the i915 perf |
| 1989 | * mutex to avoid an awkward lockdep with mmap_sem. |
| 1990 | * |
| 1991 | * Most of the implementation details are handled by |
| 1992 | * i915_perf_open_ioctl_locked() after taking the &drm_i915_private->perf.lock |
| 1993 | * mutex for serializing with any non-file-operation driver hooks. |
| 1994 | * |
| 1995 | * Return: A newly opened i915 Perf stream file descriptor or negative |
| 1996 | * error code on failure. |
| 1997 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 1998 | int i915_perf_open_ioctl(struct drm_device *dev, void *data, |
| 1999 | struct drm_file *file) |
| 2000 | { |
| 2001 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2002 | struct drm_i915_perf_open_param *param = data; |
| 2003 | struct perf_open_properties props; |
| 2004 | u32 known_open_flags; |
| 2005 | int ret; |
| 2006 | |
| 2007 | if (!dev_priv->perf.initialized) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 2008 | DRM_DEBUG("i915 perf interface not available for this system\n"); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2009 | return -ENOTSUPP; |
| 2010 | } |
| 2011 | |
| 2012 | known_open_flags = I915_PERF_FLAG_FD_CLOEXEC | |
| 2013 | I915_PERF_FLAG_FD_NONBLOCK | |
| 2014 | I915_PERF_FLAG_DISABLED; |
| 2015 | if (param->flags & ~known_open_flags) { |
Robert Bragg | 7708550 | 2016-12-01 17:21:52 +0000 | [diff] [blame] | 2016 | DRM_DEBUG("Unknown drm_i915_perf_open_param flag\n"); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2017 | return -EINVAL; |
| 2018 | } |
| 2019 | |
| 2020 | ret = read_properties_unlocked(dev_priv, |
| 2021 | u64_to_user_ptr(param->properties_ptr), |
| 2022 | param->num_properties, |
| 2023 | &props); |
| 2024 | if (ret) |
| 2025 | return ret; |
| 2026 | |
| 2027 | mutex_lock(&dev_priv->perf.lock); |
| 2028 | ret = i915_perf_open_ioctl_locked(dev_priv, param, &props, file); |
| 2029 | mutex_unlock(&dev_priv->perf.lock); |
| 2030 | |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 2034 | /** |
| 2035 | * i915_perf_register - exposes i915-perf to userspace |
| 2036 | * @dev_priv: i915 device instance |
| 2037 | * |
| 2038 | * In particular OA metric sets are advertised under a sysfs metrics/ |
| 2039 | * directory allowing userspace to enumerate valid IDs that can be |
| 2040 | * used to open an i915-perf stream. |
| 2041 | */ |
Robert Bragg | 442b8c0 | 2016-11-07 19:49:53 +0000 | [diff] [blame] | 2042 | void i915_perf_register(struct drm_i915_private *dev_priv) |
| 2043 | { |
| 2044 | if (!IS_HASWELL(dev_priv)) |
| 2045 | return; |
| 2046 | |
| 2047 | if (!dev_priv->perf.initialized) |
| 2048 | return; |
| 2049 | |
| 2050 | /* To be sure we're synchronized with an attempted |
| 2051 | * i915_perf_open_ioctl(); considering that we register after |
| 2052 | * being exposed to userspace. |
| 2053 | */ |
| 2054 | mutex_lock(&dev_priv->perf.lock); |
| 2055 | |
| 2056 | dev_priv->perf.metrics_kobj = |
| 2057 | kobject_create_and_add("metrics", |
| 2058 | &dev_priv->drm.primary->kdev->kobj); |
| 2059 | if (!dev_priv->perf.metrics_kobj) |
| 2060 | goto exit; |
| 2061 | |
| 2062 | if (i915_perf_register_sysfs_hsw(dev_priv)) { |
| 2063 | kobject_put(dev_priv->perf.metrics_kobj); |
| 2064 | dev_priv->perf.metrics_kobj = NULL; |
| 2065 | } |
| 2066 | |
| 2067 | exit: |
| 2068 | mutex_unlock(&dev_priv->perf.lock); |
| 2069 | } |
| 2070 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 2071 | /** |
| 2072 | * i915_perf_unregister - hide i915-perf from userspace |
| 2073 | * @dev_priv: i915 device instance |
| 2074 | * |
| 2075 | * i915-perf state cleanup is split up into an 'unregister' and |
| 2076 | * 'deinit' phase where the interface is first hidden from |
| 2077 | * userspace by i915_perf_unregister() before cleaning up |
| 2078 | * remaining state in i915_perf_fini(). |
| 2079 | */ |
Robert Bragg | 442b8c0 | 2016-11-07 19:49:53 +0000 | [diff] [blame] | 2080 | void i915_perf_unregister(struct drm_i915_private *dev_priv) |
| 2081 | { |
| 2082 | if (!IS_HASWELL(dev_priv)) |
| 2083 | return; |
| 2084 | |
| 2085 | if (!dev_priv->perf.metrics_kobj) |
| 2086 | return; |
| 2087 | |
| 2088 | i915_perf_unregister_sysfs_hsw(dev_priv); |
| 2089 | |
| 2090 | kobject_put(dev_priv->perf.metrics_kobj); |
| 2091 | dev_priv->perf.metrics_kobj = NULL; |
| 2092 | } |
| 2093 | |
Robert Bragg | ccdf634 | 2016-11-07 19:49:54 +0000 | [diff] [blame] | 2094 | static struct ctl_table oa_table[] = { |
| 2095 | { |
| 2096 | .procname = "perf_stream_paranoid", |
| 2097 | .data = &i915_perf_stream_paranoid, |
| 2098 | .maxlen = sizeof(i915_perf_stream_paranoid), |
| 2099 | .mode = 0644, |
| 2100 | .proc_handler = proc_dointvec_minmax, |
| 2101 | .extra1 = &zero, |
| 2102 | .extra2 = &one, |
| 2103 | }, |
Robert Bragg | 00319ba | 2016-11-07 19:49:55 +0000 | [diff] [blame] | 2104 | { |
| 2105 | .procname = "oa_max_sample_rate", |
| 2106 | .data = &i915_oa_max_sample_rate, |
| 2107 | .maxlen = sizeof(i915_oa_max_sample_rate), |
| 2108 | .mode = 0644, |
| 2109 | .proc_handler = proc_dointvec_minmax, |
| 2110 | .extra1 = &zero, |
| 2111 | .extra2 = &oa_sample_rate_hard_limit, |
| 2112 | }, |
Robert Bragg | ccdf634 | 2016-11-07 19:49:54 +0000 | [diff] [blame] | 2113 | {} |
| 2114 | }; |
| 2115 | |
| 2116 | static struct ctl_table i915_root[] = { |
| 2117 | { |
| 2118 | .procname = "i915", |
| 2119 | .maxlen = 0, |
| 2120 | .mode = 0555, |
| 2121 | .child = oa_table, |
| 2122 | }, |
| 2123 | {} |
| 2124 | }; |
| 2125 | |
| 2126 | static struct ctl_table dev_root[] = { |
| 2127 | { |
| 2128 | .procname = "dev", |
| 2129 | .maxlen = 0, |
| 2130 | .mode = 0555, |
| 2131 | .child = i915_root, |
| 2132 | }, |
| 2133 | {} |
| 2134 | }; |
| 2135 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 2136 | /** |
| 2137 | * i915_perf_init - initialize i915-perf state on module load |
| 2138 | * @dev_priv: i915 device instance |
| 2139 | * |
| 2140 | * Initializes i915-perf state without exposing anything to userspace. |
| 2141 | * |
| 2142 | * Note: i915-perf initialization is split into an 'init' and 'register' |
| 2143 | * phase with the i915_perf_register() exposing state to userspace. |
| 2144 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2145 | void i915_perf_init(struct drm_i915_private *dev_priv) |
| 2146 | { |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 2147 | if (!IS_HASWELL(dev_priv)) |
| 2148 | return; |
| 2149 | |
| 2150 | hrtimer_init(&dev_priv->perf.oa.poll_check_timer, |
| 2151 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2152 | dev_priv->perf.oa.poll_check_timer.function = oa_poll_check_timer_cb; |
| 2153 | init_waitqueue_head(&dev_priv->perf.oa.poll_wq); |
| 2154 | |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2155 | INIT_LIST_HEAD(&dev_priv->perf.streams); |
| 2156 | mutex_init(&dev_priv->perf.lock); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 2157 | spin_lock_init(&dev_priv->perf.hook_lock); |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 2158 | spin_lock_init(&dev_priv->perf.oa.oa_buffer.ptr_lock); |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 2159 | |
| 2160 | dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer; |
| 2161 | dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set; |
| 2162 | dev_priv->perf.oa.ops.disable_metric_set = hsw_disable_metric_set; |
| 2163 | dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable; |
| 2164 | dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable; |
| 2165 | dev_priv->perf.oa.ops.read = gen7_oa_read; |
Robert Bragg | 0dd860c | 2017-05-11 16:43:28 +0100 | [diff] [blame] | 2166 | dev_priv->perf.oa.ops.oa_buffer_check = |
| 2167 | gen7_oa_buffer_check_unlocked; |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 2168 | |
| 2169 | dev_priv->perf.oa.oa_formats = hsw_oa_formats; |
| 2170 | |
| 2171 | dev_priv->perf.oa.n_builtin_sets = |
| 2172 | i915_oa_n_builtin_metric_sets_hsw; |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2173 | |
Robert Bragg | ccdf634 | 2016-11-07 19:49:54 +0000 | [diff] [blame] | 2174 | dev_priv->perf.sysctl_header = register_sysctl_table(dev_root); |
| 2175 | |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2176 | dev_priv->perf.initialized = true; |
| 2177 | } |
| 2178 | |
Robert Bragg | 16d98b3 | 2016-12-07 21:40:33 +0000 | [diff] [blame] | 2179 | /** |
| 2180 | * i915_perf_fini - Counter part to i915_perf_init() |
| 2181 | * @dev_priv: i915 device instance |
| 2182 | */ |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2183 | void i915_perf_fini(struct drm_i915_private *dev_priv) |
| 2184 | { |
| 2185 | if (!dev_priv->perf.initialized) |
| 2186 | return; |
| 2187 | |
Robert Bragg | ccdf634 | 2016-11-07 19:49:54 +0000 | [diff] [blame] | 2188 | unregister_sysctl_table(dev_priv->perf.sysctl_header); |
| 2189 | |
Robert Bragg | d796515 | 2016-11-07 19:49:52 +0000 | [diff] [blame] | 2190 | memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops)); |
Robert Bragg | eec688e | 2016-11-07 19:49:47 +0000 | [diff] [blame] | 2191 | dev_priv->perf.initialized = false; |
| 2192 | } |