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