forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/drivers/gpu/drm/i915/i915_perf.c
....@@ -195,21 +195,15 @@
195195 #include <linux/sizes.h>
196196 #include <linux/uuid.h>
197197
198
+#include "gem/i915_gem_context.h"
199
+#include "gt/intel_engine_pm.h"
200
+#include "gt/intel_engine_user.h"
201
+#include "gt/intel_gt.h"
202
+#include "gt/intel_lrc_reg.h"
203
+#include "gt/intel_ring.h"
204
+
198205 #include "i915_drv.h"
199
-#include "i915_oa_hsw.h"
200
-#include "i915_oa_bdw.h"
201
-#include "i915_oa_chv.h"
202
-#include "i915_oa_sklgt2.h"
203
-#include "i915_oa_sklgt3.h"
204
-#include "i915_oa_sklgt4.h"
205
-#include "i915_oa_bxt.h"
206
-#include "i915_oa_kblgt2.h"
207
-#include "i915_oa_kblgt3.h"
208
-#include "i915_oa_glk.h"
209
-#include "i915_oa_cflgt2.h"
210
-#include "i915_oa_cflgt3.h"
211
-#include "i915_oa_cnl.h"
212
-#include "i915_oa_icl.h"
206
+#include "i915_perf.h"
213207
214208 /* HW requires this to be a power of two, between 128k and 16M, though driver
215209 * is currently generally designed assuming the largest 16M size is used such
....@@ -229,26 +223,17 @@
229223 *
230224 * Although this can be observed explicitly while copying reports to userspace
231225 * by checking for a zeroed report-id field in tail reports, we want to account
232
- * for this earlier, as part of the oa_buffer_check to avoid lots of redundant
233
- * read() attempts.
226
+ * for this earlier, as part of the oa_buffer_check_unlocked to avoid lots of
227
+ * redundant read() attempts.
234228 *
235
- * In effect we define a tail pointer for reading that lags the real tail
236
- * pointer by at least %OA_TAIL_MARGIN_NSEC nanoseconds, which gives enough
237
- * time for the corresponding reports to become visible to the CPU.
238
- *
239
- * To manage this we actually track two tail pointers:
240
- * 1) An 'aging' tail with an associated timestamp that is tracked until we
241
- * can trust the corresponding data is visible to the CPU; at which point
242
- * it is considered 'aged'.
243
- * 2) An 'aged' tail that can be used for read()ing.
244
- *
245
- * The two separate pointers let us decouple read()s from tail pointer aging.
246
- *
247
- * The tail pointers are checked and updated at a limited rate within a hrtimer
248
- * callback (the same callback that is used for delivering EPOLLIN events)
249
- *
250
- * Initially the tails are marked invalid with %INVALID_TAIL_PTR which
251
- * indicates that an updated tail pointer is needed.
229
+ * We workaround this issue in oa_buffer_check_unlocked() by reading the reports
230
+ * in the OA buffer, starting from the tail reported by the HW until we find a
231
+ * report with its first 2 dwords not 0 meaning its previous report is
232
+ * completely in memory and ready to be read. Those dwords are also set to 0
233
+ * once read and the whole buffer is cleared upon OA buffer initialization. The
234
+ * first dword is the reason for this report while the second is the timestamp,
235
+ * making the chances of having those 2 fields at 0 fairly unlikely. A more
236
+ * detailed explanation is available in oa_buffer_check_unlocked().
252237 *
253238 * Most of the implementation details for this workaround are in
254239 * oa_buffer_check_unlocked() and _append_oa_reports()
....@@ -263,15 +248,13 @@
263248 #define OA_TAIL_MARGIN_NSEC 100000ULL
264249 #define INVALID_TAIL_PTR 0xffffffff
265250
266
-/* frequency for checking whether the OA unit has written new reports to the
267
- * circular OA buffer...
251
+/* The default frequency for checking whether the OA unit has written new
252
+ * reports to the circular OA buffer...
268253 */
269
-#define POLL_FREQUENCY 200
270
-#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
254
+#define DEFAULT_POLL_FREQUENCY_HZ 200
255
+#define DEFAULT_POLL_PERIOD_NS (NSEC_PER_SEC / DEFAULT_POLL_FREQUENCY_HZ)
271256
272257 /* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
273
-static int zero;
274
-static int one = 1;
275258 static u32 i915_perf_stream_paranoid = true;
276259
277260 /* The maximum exponent the hardware accepts is 63 (essentially it selects one
....@@ -288,6 +271,7 @@
288271
289272 /* On Gen8+ automatically triggered OA reports include a 'reason' field... */
290273 #define OAREPORT_REASON_MASK 0x3f
274
+#define OAREPORT_REASON_MASK_EXTENDED 0x7f
291275 #define OAREPORT_REASON_SHIFT 19
292276 #define OAREPORT_REASON_TIMER (1<<0)
293277 #define OAREPORT_REASON_CTX_SWITCH (1<<3)
....@@ -333,17 +317,30 @@
333317 [I915_OA_FORMAT_C4_B8] = { 7, 64 },
334318 };
335319
320
+static const struct i915_oa_format gen12_oa_formats[I915_OA_FORMAT_MAX] = {
321
+ [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
322
+};
323
+
336324 #define SAMPLE_OA_REPORT (1<<0)
337325
338326 /**
339327 * struct perf_open_properties - for validated properties given to open a stream
340328 * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
341329 * @single_context: Whether a single or all gpu contexts should be monitored
330
+ * @hold_preemption: Whether the preemption is disabled for the filtered
331
+ * context
342332 * @ctx_handle: A gem ctx handle for use with @single_context
343333 * @metrics_set: An ID for an OA unit metric set advertised via sysfs
344334 * @oa_format: An OA unit HW report format
345335 * @oa_periodic: Whether to enable periodic OA unit sampling
346336 * @oa_period_exponent: The OA unit sampling period is derived from this
337
+ * @engine: The engine (typically rcs0) being monitored by the OA unit
338
+ * @has_sseu: Whether @sseu was specified by userspace
339
+ * @sseu: internal SSEU configuration computed either from the userspace
340
+ * specified configuration in the opening parameters or a default value
341
+ * (see get_default_sseu_config())
342
+ * @poll_oa_period: The period in nanoseconds at which the CPU will check for OA
343
+ * data availability
347344 *
348345 * As read_properties_unlocked() enumerates and validates the properties given
349346 * to open a stream of metrics the configuration is built up in the structure
....@@ -353,6 +350,7 @@
353350 u32 sample_flags;
354351
355352 u64 single_context:1;
353
+ u64 hold_preemption:1;
356354 u64 ctx_handle;
357355
358356 /* OA sampling state */
....@@ -360,71 +358,85 @@
360358 int oa_format;
361359 bool oa_periodic;
362360 int oa_period_exponent;
361
+
362
+ struct intel_engine_cs *engine;
363
+
364
+ bool has_sseu;
365
+ struct intel_sseu sseu;
366
+
367
+ u64 poll_oa_period;
363368 };
364369
365
-static void free_oa_config(struct drm_i915_private *dev_priv,
366
- struct i915_oa_config *oa_config)
370
+struct i915_oa_config_bo {
371
+ struct llist_node node;
372
+
373
+ struct i915_oa_config *oa_config;
374
+ struct i915_vma *vma;
375
+};
376
+
377
+static struct ctl_table_header *sysctl_header;
378
+
379
+static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer);
380
+
381
+void i915_oa_config_release(struct kref *ref)
367382 {
368
- if (!PTR_ERR(oa_config->flex_regs))
369
- kfree(oa_config->flex_regs);
370
- if (!PTR_ERR(oa_config->b_counter_regs))
371
- kfree(oa_config->b_counter_regs);
372
- if (!PTR_ERR(oa_config->mux_regs))
373
- kfree(oa_config->mux_regs);
374
- kfree(oa_config);
383
+ struct i915_oa_config *oa_config =
384
+ container_of(ref, typeof(*oa_config), ref);
385
+
386
+ kfree(oa_config->flex_regs);
387
+ kfree(oa_config->b_counter_regs);
388
+ kfree(oa_config->mux_regs);
389
+
390
+ kfree_rcu(oa_config, rcu);
375391 }
376392
377
-static void put_oa_config(struct drm_i915_private *dev_priv,
378
- struct i915_oa_config *oa_config)
393
+struct i915_oa_config *
394
+i915_perf_get_oa_config(struct i915_perf *perf, int metrics_set)
379395 {
380
- if (!atomic_dec_and_test(&oa_config->ref_count))
381
- return;
396
+ struct i915_oa_config *oa_config;
382397
383
- free_oa_config(dev_priv, oa_config);
398
+ rcu_read_lock();
399
+ oa_config = idr_find(&perf->metrics_idr, metrics_set);
400
+ if (oa_config)
401
+ oa_config = i915_oa_config_get(oa_config);
402
+ rcu_read_unlock();
403
+
404
+ return oa_config;
384405 }
385406
386
-static int get_oa_config(struct drm_i915_private *dev_priv,
387
- int metrics_set,
388
- struct i915_oa_config **out_config)
407
+static void free_oa_config_bo(struct i915_oa_config_bo *oa_bo)
389408 {
390
- int ret;
391
-
392
- if (metrics_set == 1) {
393
- *out_config = &dev_priv->perf.oa.test_config;
394
- atomic_inc(&dev_priv->perf.oa.test_config.ref_count);
395
- return 0;
396
- }
397
-
398
- ret = mutex_lock_interruptible(&dev_priv->perf.metrics_lock);
399
- if (ret)
400
- return ret;
401
-
402
- *out_config = idr_find(&dev_priv->perf.metrics_idr, metrics_set);
403
- if (!*out_config)
404
- ret = -EINVAL;
405
- else
406
- atomic_inc(&(*out_config)->ref_count);
407
-
408
- mutex_unlock(&dev_priv->perf.metrics_lock);
409
-
410
- return ret;
409
+ i915_oa_config_put(oa_bo->oa_config);
410
+ i915_vma_put(oa_bo->vma);
411
+ kfree(oa_bo);
411412 }
412413
413
-static u32 gen8_oa_hw_tail_read(struct drm_i915_private *dev_priv)
414
+static u32 gen12_oa_hw_tail_read(struct i915_perf_stream *stream)
414415 {
415
- return I915_READ(GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
416
+ struct intel_uncore *uncore = stream->uncore;
417
+
418
+ return intel_uncore_read(uncore, GEN12_OAG_OATAILPTR) &
419
+ GEN12_OAG_OATAILPTR_MASK;
416420 }
417421
418
-static u32 gen7_oa_hw_tail_read(struct drm_i915_private *dev_priv)
422
+static u32 gen8_oa_hw_tail_read(struct i915_perf_stream *stream)
419423 {
420
- u32 oastatus1 = I915_READ(GEN7_OASTATUS1);
424
+ struct intel_uncore *uncore = stream->uncore;
425
+
426
+ return intel_uncore_read(uncore, GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
427
+}
428
+
429
+static u32 gen7_oa_hw_tail_read(struct i915_perf_stream *stream)
430
+{
431
+ struct intel_uncore *uncore = stream->uncore;
432
+ u32 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
421433
422434 return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
423435 }
424436
425437 /**
426438 * oa_buffer_check_unlocked - check for data and update tail ptr state
427
- * @dev_priv: i915 device instance
439
+ * @stream: i915 stream instance
428440 *
429441 * This is either called via fops (for blocking reads in user ctx) or the poll
430442 * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
....@@ -437,8 +449,8 @@
437449 * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
438450 *
439451 * Besides returning true when there is data available to read() this function
440
- * also has the side effect of updating the oa_buffer.tails[], .aging_timestamp
441
- * and .aged_tail_idx state used for reading.
452
+ * also updates the tail, aging_tail and aging_timestamp in the oa_buffer
453
+ * object.
442454 *
443455 * Note: It's safe to read OA config state here unlocked, assuming that this is
444456 * only called while the stream is enabled, while the global OA configuration
....@@ -446,31 +458,22 @@
446458 *
447459 * Returns: %true if the OA buffer contains data, else %false
448460 */
449
-static bool oa_buffer_check_unlocked(struct drm_i915_private *dev_priv)
461
+static bool oa_buffer_check_unlocked(struct i915_perf_stream *stream)
450462 {
451
- int report_size = dev_priv->perf.oa.oa_buffer.format_size;
463
+ u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
464
+ int report_size = stream->oa_buffer.format_size;
452465 unsigned long flags;
453
- unsigned int aged_idx;
454
- u32 head, hw_tail, aged_tail, aging_tail;
466
+ bool pollin;
467
+ u32 hw_tail;
455468 u64 now;
456469
457470 /* We have to consider the (unlikely) possibility that read() errors
458
- * could result in an OA buffer reset which might reset the head,
459
- * tails[] and aged_tail state.
471
+ * could result in an OA buffer reset which might reset the head and
472
+ * tail state.
460473 */
461
- spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
474
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
462475
463
- /* NB: The head we observe here might effectively be a little out of
464
- * date (between head and tails[aged_idx].offset if there is currently
465
- * a read() in progress.
466
- */
467
- head = dev_priv->perf.oa.oa_buffer.head;
468
-
469
- aged_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
470
- aged_tail = dev_priv->perf.oa.oa_buffer.tails[aged_idx].offset;
471
- aging_tail = dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset;
472
-
473
- hw_tail = dev_priv->perf.oa.ops.oa_hw_tail_read(dev_priv);
476
+ hw_tail = stream->perf->ops.oa_hw_tail_read(stream);
474477
475478 /* The tail pointer increases in 64 byte increments,
476479 * not in report_size steps...
....@@ -479,63 +482,63 @@
479482
480483 now = ktime_get_mono_fast_ns();
481484
482
- /* Update the aged tail
483
- *
484
- * Flip the tail pointer available for read()s once the aging tail is
485
- * old enough to trust that the corresponding data will be visible to
486
- * the CPU...
487
- *
488
- * Do this before updating the aging pointer in case we may be able to
489
- * immediately start aging a new pointer too (if new data has become
490
- * available) without needing to wait for a later hrtimer callback.
491
- */
492
- if (aging_tail != INVALID_TAIL_PTR &&
493
- ((now - dev_priv->perf.oa.oa_buffer.aging_timestamp) >
494
- OA_TAIL_MARGIN_NSEC)) {
495
-
496
- aged_idx ^= 1;
497
- dev_priv->perf.oa.oa_buffer.aged_tail_idx = aged_idx;
498
-
499
- aged_tail = aging_tail;
500
-
501
- /* Mark that we need a new pointer to start aging... */
502
- dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset = INVALID_TAIL_PTR;
503
- aging_tail = INVALID_TAIL_PTR;
504
- }
505
-
506
- /* Update the aging tail
507
- *
508
- * We throttle aging tail updates until we have a new tail that
509
- * represents >= one report more data than is already available for
510
- * reading. This ensures there will be enough data for a successful
511
- * read once this new pointer has aged and ensures we will give the new
512
- * pointer time to age.
513
- */
514
- if (aging_tail == INVALID_TAIL_PTR &&
515
- (aged_tail == INVALID_TAIL_PTR ||
516
- OA_TAKEN(hw_tail, aged_tail) >= report_size)) {
517
- struct i915_vma *vma = dev_priv->perf.oa.oa_buffer.vma;
518
- u32 gtt_offset = i915_ggtt_offset(vma);
519
-
520
- /* Be paranoid and do a bounds check on the pointer read back
521
- * from hardware, just in case some spurious hardware condition
522
- * could put the tail out of bounds...
485
+ if (hw_tail == stream->oa_buffer.aging_tail &&
486
+ (now - stream->oa_buffer.aging_timestamp) > OA_TAIL_MARGIN_NSEC) {
487
+ /* If the HW tail hasn't move since the last check and the HW
488
+ * tail has been aging for long enough, declare it the new
489
+ * tail.
523490 */
524
- if (hw_tail >= gtt_offset &&
525
- hw_tail < (gtt_offset + OA_BUFFER_SIZE)) {
526
- dev_priv->perf.oa.oa_buffer.tails[!aged_idx].offset =
527
- aging_tail = hw_tail;
528
- dev_priv->perf.oa.oa_buffer.aging_timestamp = now;
529
- } else {
530
- DRM_ERROR("Ignoring spurious out of range OA buffer tail pointer = %u\n",
531
- hw_tail);
491
+ stream->oa_buffer.tail = stream->oa_buffer.aging_tail;
492
+ } else {
493
+ u32 head, tail, aged_tail;
494
+
495
+ /* NB: The head we observe here might effectively be a little
496
+ * out of date. If a read() is in progress, the head could be
497
+ * anywhere between this head and stream->oa_buffer.tail.
498
+ */
499
+ head = stream->oa_buffer.head - gtt_offset;
500
+ aged_tail = stream->oa_buffer.tail - gtt_offset;
501
+
502
+ hw_tail -= gtt_offset;
503
+ tail = hw_tail;
504
+
505
+ /* Walk the stream backward until we find a report with dword 0
506
+ * & 1 not at 0. Since the circular buffer pointers progress by
507
+ * increments of 64 bytes and that reports can be up to 256
508
+ * bytes long, we can't tell whether a report has fully landed
509
+ * in memory before the first 2 dwords of the following report
510
+ * have effectively landed.
511
+ *
512
+ * This is assuming that the writes of the OA unit land in
513
+ * memory in the order they were written to.
514
+ * If not : (╯°□°)╯︵ ┻━┻
515
+ */
516
+ while (OA_TAKEN(tail, aged_tail) >= report_size) {
517
+ u32 *report32 = (void *)(stream->oa_buffer.vaddr + tail);
518
+
519
+ if (report32[0] != 0 || report32[1] != 0)
520
+ break;
521
+
522
+ tail = (tail - report_size) & (OA_BUFFER_SIZE - 1);
532523 }
524
+
525
+ if (OA_TAKEN(hw_tail, tail) > report_size &&
526
+ __ratelimit(&stream->perf->tail_pointer_race))
527
+ DRM_NOTE("unlanded report(s) head=0x%x "
528
+ "tail=0x%x hw_tail=0x%x\n",
529
+ head, tail, hw_tail);
530
+
531
+ stream->oa_buffer.tail = gtt_offset + tail;
532
+ stream->oa_buffer.aging_tail = gtt_offset + hw_tail;
533
+ stream->oa_buffer.aging_timestamp = now;
533534 }
534535
535
- spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
536
+ pollin = OA_TAKEN(stream->oa_buffer.tail - gtt_offset,
537
+ stream->oa_buffer.head - gtt_offset) >= report_size;
536538
537
- return aged_tail == INVALID_TAIL_PTR ?
538
- false : OA_TAKEN(aged_tail, head) >= report_size;
539
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
540
+
541
+ return pollin;
539542 }
540543
541544 /**
....@@ -595,10 +598,8 @@
595598 size_t *offset,
596599 const u8 *report)
597600 {
598
- struct drm_i915_private *dev_priv = stream->dev_priv;
599
- int report_size = dev_priv->perf.oa.oa_buffer.format_size;
601
+ int report_size = stream->oa_buffer.format_size;
600602 struct drm_i915_perf_record_header header;
601
- u32 sample_flags = stream->sample_flags;
602603
603604 header.type = DRM_I915_PERF_RECORD_SAMPLE;
604605 header.pad = 0;
....@@ -612,10 +613,8 @@
612613 return -EFAULT;
613614 buf += sizeof(header);
614615
615
- if (sample_flags & SAMPLE_OA_REPORT) {
616
- if (copy_to_user(buf, report, report_size))
617
- return -EFAULT;
618
- }
616
+ if (copy_to_user(buf, report, report_size))
617
+ return -EFAULT;
619618
620619 (*offset) += header.size;
621620
....@@ -647,35 +646,26 @@
647646 size_t count,
648647 size_t *offset)
649648 {
650
- struct drm_i915_private *dev_priv = stream->dev_priv;
651
- int report_size = dev_priv->perf.oa.oa_buffer.format_size;
652
- u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr;
653
- u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
649
+ struct intel_uncore *uncore = stream->uncore;
650
+ int report_size = stream->oa_buffer.format_size;
651
+ u8 *oa_buf_base = stream->oa_buffer.vaddr;
652
+ u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
654653 u32 mask = (OA_BUFFER_SIZE - 1);
655654 size_t start_offset = *offset;
656655 unsigned long flags;
657
- unsigned int aged_tail_idx;
658656 u32 head, tail;
659657 u32 taken;
660658 int ret = 0;
661659
662
- if (WARN_ON(!stream->enabled))
660
+ if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
663661 return -EIO;
664662
665
- spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
663
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
666664
667
- head = dev_priv->perf.oa.oa_buffer.head;
668
- aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
669
- tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset;
665
+ head = stream->oa_buffer.head;
666
+ tail = stream->oa_buffer.tail;
670667
671
- spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
672
-
673
- /*
674
- * An invalid tail pointer here means we're still waiting for the poll
675
- * hrtimer callback to give us a pointer
676
- */
677
- if (tail == INVALID_TAIL_PTR)
678
- return -EAGAIN;
668
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
679669
680670 /*
681671 * NB: oa_buffer.head/tail include the gtt_offset which we don't want
....@@ -691,10 +681,11 @@
691681 * only be incremented by multiples of the report size (notably also
692682 * all a power of two).
693683 */
694
- if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
695
- tail > OA_BUFFER_SIZE || tail % report_size,
696
- "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
697
- head, tail))
684
+ if (drm_WARN_ONCE(&uncore->i915->drm,
685
+ head > OA_BUFFER_SIZE || head % report_size ||
686
+ tail > OA_BUFFER_SIZE || tail % report_size,
687
+ "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
688
+ head, tail))
698689 return -EIO;
699690
700691
....@@ -715,8 +706,10 @@
715706 * here would imply a driver bug that would result
716707 * in an overrun.
717708 */
718
- if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
719
- DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
709
+ if (drm_WARN_ON(&uncore->i915->drm,
710
+ (OA_BUFFER_SIZE - head) < report_size)) {
711
+ drm_err(&uncore->i915->drm,
712
+ "Spurious OA head ptr: non-integral report offset\n");
720713 break;
721714 }
722715
....@@ -730,14 +723,16 @@
730723 * it to userspace...
731724 */
732725 reason = ((report32[0] >> OAREPORT_REASON_SHIFT) &
733
- OAREPORT_REASON_MASK);
726
+ (IS_GEN(stream->perf->i915, 12) ?
727
+ OAREPORT_REASON_MASK_EXTENDED :
728
+ OAREPORT_REASON_MASK));
734729 if (reason == 0) {
735
- if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs))
730
+ if (__ratelimit(&stream->perf->spurious_report_rs))
736731 DRM_NOTE("Skipping spurious, invalid OA report\n");
737732 continue;
738733 }
739734
740
- ctx_id = report32[2] & dev_priv->perf.oa.specific_ctx_id_mask;
735
+ ctx_id = report32[2] & stream->specific_ctx_id_mask;
741736
742737 /*
743738 * Squash whatever is in the CTX_ID field if it's marked as
....@@ -747,7 +742,8 @@
747742 * Note: that we don't clear the valid_ctx_bit so userspace can
748743 * understand that the ID has been squashed by the kernel.
749744 */
750
- if (!(report32[0] & dev_priv->perf.oa.gen8_valid_ctx_bit))
745
+ if (!(report32[0] & stream->perf->gen8_valid_ctx_bit) &&
746
+ INTEL_GEN(stream->perf->i915) <= 11)
751747 ctx_id = report32[2] = INVALID_CTX_ID;
752748
753749 /*
....@@ -781,18 +777,17 @@
781777 * switches since it's not-uncommon for periodic samples to
782778 * identify a switch before any 'context switch' report.
783779 */
784
- if (!dev_priv->perf.oa.exclusive_stream->ctx ||
785
- dev_priv->perf.oa.specific_ctx_id == ctx_id ||
786
- (dev_priv->perf.oa.oa_buffer.last_ctx_id ==
787
- dev_priv->perf.oa.specific_ctx_id) ||
780
+ if (!stream->perf->exclusive_stream->ctx ||
781
+ stream->specific_ctx_id == ctx_id ||
782
+ stream->oa_buffer.last_ctx_id == stream->specific_ctx_id ||
788783 reason & OAREPORT_REASON_CTX_SWITCH) {
789784
790785 /*
791786 * While filtering for a single context we avoid
792787 * leaking the IDs of other contexts.
793788 */
794
- if (dev_priv->perf.oa.exclusive_stream->ctx &&
795
- dev_priv->perf.oa.specific_ctx_id != ctx_id) {
789
+ if (stream->perf->exclusive_stream->ctx &&
790
+ stream->specific_ctx_id != ctx_id) {
796791 report32[2] = INVALID_CTX_ID;
797792 }
798793
....@@ -801,32 +796,35 @@
801796 if (ret)
802797 break;
803798
804
- dev_priv->perf.oa.oa_buffer.last_ctx_id = ctx_id;
799
+ stream->oa_buffer.last_ctx_id = ctx_id;
805800 }
806801
807802 /*
808
- * The above reason field sanity check is based on
809
- * the assumption that the OA buffer is initially
810
- * zeroed and we reset the field after copying so the
811
- * check is still meaningful once old reports start
812
- * being overwritten.
803
+ * Clear out the first 2 dword as a mean to detect unlanded
804
+ * reports.
813805 */
814806 report32[0] = 0;
807
+ report32[1] = 0;
815808 }
816809
817810 if (start_offset != *offset) {
818
- spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
811
+ i915_reg_t oaheadptr;
812
+
813
+ oaheadptr = IS_GEN(stream->perf->i915, 12) ?
814
+ GEN12_OAG_OAHEADPTR : GEN8_OAHEADPTR;
815
+
816
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
819817
820818 /*
821819 * We removed the gtt_offset for the copy loop above, indexing
822820 * relative to oa_buf_base so put back here...
823821 */
824822 head += gtt_offset;
823
+ intel_uncore_write(uncore, oaheadptr,
824
+ head & GEN12_OAG_OAHEADPTR_MASK);
825
+ stream->oa_buffer.head = head;
825826
826
- I915_WRITE(GEN8_OAHEADPTR, head & GEN8_OAHEADPTR_MASK);
827
- dev_priv->perf.oa.oa_buffer.head = head;
828
-
829
- spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
827
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
830828 }
831829
832830 return ret;
....@@ -857,14 +855,18 @@
857855 size_t count,
858856 size_t *offset)
859857 {
860
- struct drm_i915_private *dev_priv = stream->dev_priv;
858
+ struct intel_uncore *uncore = stream->uncore;
861859 u32 oastatus;
860
+ i915_reg_t oastatus_reg;
862861 int ret;
863862
864
- if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr))
863
+ if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
865864 return -EIO;
866865
867
- oastatus = I915_READ(GEN8_OASTATUS);
866
+ oastatus_reg = IS_GEN(stream->perf->i915, 12) ?
867
+ GEN12_OAG_OASTATUS : GEN8_OASTATUS;
868
+
869
+ oastatus = intel_uncore_read(uncore, oastatus_reg);
868870
869871 /*
870872 * We treat OABUFFER_OVERFLOW as a significant error:
....@@ -887,16 +889,16 @@
887889 return ret;
888890
889891 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
890
- dev_priv->perf.oa.period_exponent);
892
+ stream->period_exponent);
891893
892
- dev_priv->perf.oa.ops.oa_disable(dev_priv);
893
- dev_priv->perf.oa.ops.oa_enable(dev_priv);
894
+ stream->perf->ops.oa_disable(stream);
895
+ stream->perf->ops.oa_enable(stream);
894896
895897 /*
896898 * Note: .oa_enable() is expected to re-init the oabuffer and
897899 * reset GEN8_OASTATUS for us
898900 */
899
- oastatus = I915_READ(GEN8_OASTATUS);
901
+ oastatus = intel_uncore_read(uncore, oastatus_reg);
900902 }
901903
902904 if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
....@@ -904,8 +906,13 @@
904906 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
905907 if (ret)
906908 return ret;
907
- I915_WRITE(GEN8_OASTATUS,
908
- oastatus & ~GEN8_OASTATUS_REPORT_LOST);
909
+
910
+ intel_uncore_rmw(uncore, oastatus_reg,
911
+ GEN8_OASTATUS_COUNTER_OVERFLOW |
912
+ GEN8_OASTATUS_REPORT_LOST,
913
+ IS_GEN_RANGE(uncore->i915, 8, 10) ?
914
+ (GEN8_OASTATUS_HEAD_POINTER_WRAP |
915
+ GEN8_OASTATUS_TAIL_POINTER_WRAP) : 0);
909916 }
910917
911918 return gen8_append_oa_reports(stream, buf, count, offset);
....@@ -936,34 +943,26 @@
936943 size_t count,
937944 size_t *offset)
938945 {
939
- struct drm_i915_private *dev_priv = stream->dev_priv;
940
- int report_size = dev_priv->perf.oa.oa_buffer.format_size;
941
- u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.vaddr;
942
- u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
946
+ struct intel_uncore *uncore = stream->uncore;
947
+ int report_size = stream->oa_buffer.format_size;
948
+ u8 *oa_buf_base = stream->oa_buffer.vaddr;
949
+ u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
943950 u32 mask = (OA_BUFFER_SIZE - 1);
944951 size_t start_offset = *offset;
945952 unsigned long flags;
946
- unsigned int aged_tail_idx;
947953 u32 head, tail;
948954 u32 taken;
949955 int ret = 0;
950956
951
- if (WARN_ON(!stream->enabled))
957
+ if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
952958 return -EIO;
953959
954
- spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
960
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
955961
956
- head = dev_priv->perf.oa.oa_buffer.head;
957
- aged_tail_idx = dev_priv->perf.oa.oa_buffer.aged_tail_idx;
958
- tail = dev_priv->perf.oa.oa_buffer.tails[aged_tail_idx].offset;
962
+ head = stream->oa_buffer.head;
963
+ tail = stream->oa_buffer.tail;
959964
960
- spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
961
-
962
- /* An invalid tail pointer here means we're still waiting for the poll
963
- * hrtimer callback to give us a pointer
964
- */
965
- if (tail == INVALID_TAIL_PTR)
966
- return -EAGAIN;
965
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
967966
968967 /* NB: oa_buffer.head/tail include the gtt_offset which we don't want
969968 * while indexing relative to oa_buf_base.
....@@ -977,10 +976,11 @@
977976 * only be incremented by multiples of the report size (notably also
978977 * all a power of two).
979978 */
980
- if (WARN_ONCE(head > OA_BUFFER_SIZE || head % report_size ||
981
- tail > OA_BUFFER_SIZE || tail % report_size,
982
- "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
983
- head, tail))
979
+ if (drm_WARN_ONCE(&uncore->i915->drm,
980
+ head > OA_BUFFER_SIZE || head % report_size ||
981
+ tail > OA_BUFFER_SIZE || tail % report_size,
982
+ "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
983
+ head, tail))
984984 return -EIO;
985985
986986
....@@ -998,8 +998,10 @@
998998 * here would imply a driver bug that would result
999999 * in an overrun.
10001000 */
1001
- if (WARN_ON((OA_BUFFER_SIZE - head) < report_size)) {
1002
- DRM_ERROR("Spurious OA head ptr: non-integral report offset\n");
1001
+ if (drm_WARN_ON(&uncore->i915->drm,
1002
+ (OA_BUFFER_SIZE - head) < report_size)) {
1003
+ drm_err(&uncore->i915->drm,
1004
+ "Spurious OA head ptr: non-integral report offset\n");
10031005 break;
10041006 }
10051007
....@@ -1010,7 +1012,7 @@
10101012 * copying it to userspace...
10111013 */
10121014 if (report32[0] == 0) {
1013
- if (__ratelimit(&dev_priv->perf.oa.spurious_report_rs))
1015
+ if (__ratelimit(&stream->perf->spurious_report_rs))
10141016 DRM_NOTE("Skipping spurious, invalid OA report\n");
10151017 continue;
10161018 }
....@@ -1019,29 +1021,27 @@
10191021 if (ret)
10201022 break;
10211023
1022
- /* The above report-id field sanity check is based on
1023
- * the assumption that the OA buffer is initially
1024
- * zeroed and we reset the field after copying so the
1025
- * check is still meaningful once old reports start
1026
- * being overwritten.
1024
+ /* Clear out the first 2 dwords as a mean to detect unlanded
1025
+ * reports.
10271026 */
10281027 report32[0] = 0;
1028
+ report32[1] = 0;
10291029 }
10301030
10311031 if (start_offset != *offset) {
1032
- spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1032
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
10331033
10341034 /* We removed the gtt_offset for the copy loop above, indexing
10351035 * relative to oa_buf_base so put back here...
10361036 */
10371037 head += gtt_offset;
10381038
1039
- I915_WRITE(GEN7_OASTATUS2,
1040
- ((head & GEN7_OASTATUS2_HEAD_MASK) |
1041
- GEN7_OASTATUS2_MEM_SELECT_GGTT));
1042
- dev_priv->perf.oa.oa_buffer.head = head;
1039
+ intel_uncore_write(uncore, GEN7_OASTATUS2,
1040
+ (head & GEN7_OASTATUS2_HEAD_MASK) |
1041
+ GEN7_OASTATUS2_MEM_SELECT_GGTT);
1042
+ stream->oa_buffer.head = head;
10431043
1044
- spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1044
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
10451045 }
10461046
10471047 return ret;
....@@ -1068,21 +1068,21 @@
10681068 size_t count,
10691069 size_t *offset)
10701070 {
1071
- struct drm_i915_private *dev_priv = stream->dev_priv;
1071
+ struct intel_uncore *uncore = stream->uncore;
10721072 u32 oastatus1;
10731073 int ret;
10741074
1075
- if (WARN_ON(!dev_priv->perf.oa.oa_buffer.vaddr))
1075
+ if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
10761076 return -EIO;
10771077
1078
- oastatus1 = I915_READ(GEN7_OASTATUS1);
1078
+ oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
10791079
10801080 /* XXX: On Haswell we don't have a safe way to clear oastatus1
10811081 * bits while the OA unit is enabled (while the tail pointer
10821082 * may be updated asynchronously) so we ignore status bits
10831083 * that have already been reported to userspace.
10841084 */
1085
- oastatus1 &= ~dev_priv->perf.oa.gen7_latched_oastatus1;
1085
+ oastatus1 &= ~stream->perf->gen7_latched_oastatus1;
10861086
10871087 /* We treat OABUFFER_OVERFLOW as a significant error:
10881088 *
....@@ -1111,12 +1111,12 @@
11111111 return ret;
11121112
11131113 DRM_DEBUG("OA buffer overflow (exponent = %d): force restart\n",
1114
- dev_priv->perf.oa.period_exponent);
1114
+ stream->period_exponent);
11151115
1116
- dev_priv->perf.oa.ops.oa_disable(dev_priv);
1117
- dev_priv->perf.oa.ops.oa_enable(dev_priv);
1116
+ stream->perf->ops.oa_disable(stream);
1117
+ stream->perf->ops.oa_enable(stream);
11181118
1119
- oastatus1 = I915_READ(GEN7_OASTATUS1);
1119
+ oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
11201120 }
11211121
11221122 if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
....@@ -1124,7 +1124,7 @@
11241124 DRM_I915_PERF_RECORD_OA_REPORT_LOST);
11251125 if (ret)
11261126 return ret;
1127
- dev_priv->perf.oa.gen7_latched_oastatus1 |=
1127
+ stream->perf->gen7_latched_oastatus1 |=
11281128 GEN7_OASTATUS1_REPORT_LOST;
11291129 }
11301130
....@@ -1147,14 +1147,12 @@
11471147 */
11481148 static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
11491149 {
1150
- struct drm_i915_private *dev_priv = stream->dev_priv;
1151
-
11521150 /* We would wait indefinitely if periodic sampling is not enabled */
1153
- if (!dev_priv->perf.oa.periodic)
1151
+ if (!stream->periodic)
11541152 return -EIO;
11551153
1156
- return wait_event_interruptible(dev_priv->perf.oa.poll_wq,
1157
- oa_buffer_check_unlocked(dev_priv));
1154
+ return wait_event_interruptible(stream->poll_wq,
1155
+ oa_buffer_check_unlocked(stream));
11581156 }
11591157
11601158 /**
....@@ -1171,9 +1169,7 @@
11711169 struct file *file,
11721170 poll_table *wait)
11731171 {
1174
- struct drm_i915_private *dev_priv = stream->dev_priv;
1175
-
1176
- poll_wait(file, &dev_priv->perf.oa.poll_wq, wait);
1172
+ poll_wait(file, &stream->poll_wq, wait);
11771173 }
11781174
11791175 /**
....@@ -1193,36 +1189,48 @@
11931189 size_t count,
11941190 size_t *offset)
11951191 {
1196
- struct drm_i915_private *dev_priv = stream->dev_priv;
1197
-
1198
- return dev_priv->perf.oa.ops.read(stream, buf, count, offset);
1192
+ return stream->perf->ops.read(stream, buf, count, offset);
11991193 }
12001194
1201
-static struct intel_context *oa_pin_context(struct drm_i915_private *i915,
1202
- struct i915_gem_context *ctx)
1195
+static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)
12031196 {
1204
- struct intel_engine_cs *engine = i915->engine[RCS];
1197
+ struct i915_gem_engines_iter it;
1198
+ struct i915_gem_context *ctx = stream->ctx;
12051199 struct intel_context *ce;
1206
- int ret;
1200
+ struct i915_gem_ww_ctx ww;
1201
+ int err = -ENODEV;
12071202
1208
- ret = i915_mutex_lock_interruptible(&i915->drm);
1209
- if (ret)
1210
- return ERR_PTR(ret);
1203
+ for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
1204
+ if (ce->engine != stream->engine) /* first match! */
1205
+ continue;
12111206
1207
+ err = 0;
1208
+ break;
1209
+ }
1210
+ i915_gem_context_unlock_engines(ctx);
1211
+
1212
+ if (err)
1213
+ return ERR_PTR(err);
1214
+
1215
+ i915_gem_ww_ctx_init(&ww, true);
1216
+retry:
12121217 /*
12131218 * As the ID is the gtt offset of the context's vma we
12141219 * pin the vma to ensure the ID remains fixed.
1215
- *
1216
- * NB: implied RCS engine...
12171220 */
1218
- ce = intel_context_pin(ctx, engine);
1219
- mutex_unlock(&i915->drm.struct_mutex);
1220
- if (IS_ERR(ce))
1221
- return ce;
1221
+ err = intel_context_pin_ww(ce, &ww);
1222
+ if (err == -EDEADLK) {
1223
+ err = i915_gem_ww_ctx_backoff(&ww);
1224
+ if (!err)
1225
+ goto retry;
1226
+ }
1227
+ i915_gem_ww_ctx_fini(&ww);
12221228
1223
- i915->perf.oa.pinned_ctx = ce;
1229
+ if (err)
1230
+ return ERR_PTR(err);
12241231
1225
- return ce;
1232
+ stream->pinned_ctx = ce;
1233
+ return stream->pinned_ctx;
12261234 }
12271235
12281236 /**
....@@ -1237,28 +1245,31 @@
12371245 */
12381246 static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
12391247 {
1240
- struct drm_i915_private *i915 = stream->dev_priv;
12411248 struct intel_context *ce;
12421249
1243
- ce = oa_pin_context(i915, stream->ctx);
1250
+ ce = oa_pin_context(stream);
12441251 if (IS_ERR(ce))
12451252 return PTR_ERR(ce);
12461253
1247
- switch (INTEL_GEN(i915)) {
1254
+ switch (INTEL_GEN(ce->engine->i915)) {
12481255 case 7: {
12491256 /*
12501257 * On Haswell we don't do any post processing of the reports
12511258 * and don't need to use the mask.
12521259 */
1253
- i915->perf.oa.specific_ctx_id = i915_ggtt_offset(ce->state);
1254
- i915->perf.oa.specific_ctx_id_mask = 0;
1260
+ stream->specific_ctx_id = i915_ggtt_offset(ce->state);
1261
+ stream->specific_ctx_id_mask = 0;
12551262 break;
12561263 }
12571264
12581265 case 8:
12591266 case 9:
12601267 case 10:
1261
- if (USES_GUC_SUBMISSION(i915)) {
1268
+ if (intel_engine_in_execlists_submission_mode(ce->engine)) {
1269
+ stream->specific_ctx_id_mask =
1270
+ (1U << GEN8_CTX_ID_WIDTH) - 1;
1271
+ stream->specific_ctx_id = stream->specific_ctx_id_mask;
1272
+ } else {
12621273 /*
12631274 * When using GuC, the context descriptor we write in
12641275 * i915 is read by GuC and rewritten before it's
....@@ -1269,43 +1280,40 @@
12691280 * dropped by GuC. They won't be part of the context
12701281 * ID in the OA reports, so squash those lower bits.
12711282 */
1272
- i915->perf.oa.specific_ctx_id =
1273
- lower_32_bits(ce->lrc_desc) >> 12;
1283
+ stream->specific_ctx_id = ce->lrc.lrca >> 12;
12741284
12751285 /*
12761286 * GuC uses the top bit to signal proxy submission, so
12771287 * ignore that bit.
12781288 */
1279
- i915->perf.oa.specific_ctx_id_mask =
1289
+ stream->specific_ctx_id_mask =
12801290 (1U << (GEN8_CTX_ID_WIDTH - 1)) - 1;
1281
- } else {
1282
- i915->perf.oa.specific_ctx_id_mask =
1283
- (1U << GEN8_CTX_ID_WIDTH) - 1;
1284
- i915->perf.oa.specific_ctx_id =
1285
- upper_32_bits(ce->lrc_desc);
1286
- i915->perf.oa.specific_ctx_id &=
1287
- i915->perf.oa.specific_ctx_id_mask;
12881291 }
12891292 break;
12901293
1291
- case 11: {
1292
- i915->perf.oa.specific_ctx_id_mask =
1293
- ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) << (GEN11_SW_CTX_ID_SHIFT - 32) |
1294
- ((1U << GEN11_ENGINE_INSTANCE_WIDTH) - 1) << (GEN11_ENGINE_INSTANCE_SHIFT - 32) |
1295
- ((1 << GEN11_ENGINE_CLASS_WIDTH) - 1) << (GEN11_ENGINE_CLASS_SHIFT - 32);
1296
- i915->perf.oa.specific_ctx_id = upper_32_bits(ce->lrc_desc);
1297
- i915->perf.oa.specific_ctx_id &=
1298
- i915->perf.oa.specific_ctx_id_mask;
1294
+ case 11:
1295
+ case 12: {
1296
+ stream->specific_ctx_id_mask =
1297
+ ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) << (GEN11_SW_CTX_ID_SHIFT - 32);
1298
+ /*
1299
+ * Pick an unused context id
1300
+ * 0 - BITS_PER_LONG are used by other contexts
1301
+ * GEN12_MAX_CONTEXT_HW_ID (0x7ff) is used by idle context
1302
+ */
1303
+ stream->specific_ctx_id = (GEN12_MAX_CONTEXT_HW_ID - 1) << (GEN11_SW_CTX_ID_SHIFT - 32);
12991304 break;
13001305 }
13011306
13021307 default:
1303
- MISSING_CASE(INTEL_GEN(i915));
1308
+ MISSING_CASE(INTEL_GEN(ce->engine->i915));
13041309 }
13051310
1306
- DRM_DEBUG_DRIVER("filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
1307
- i915->perf.oa.specific_ctx_id,
1308
- i915->perf.oa.specific_ctx_id_mask);
1311
+ ce->tag = stream->specific_ctx_id;
1312
+
1313
+ drm_dbg(&stream->perf->i915->drm,
1314
+ "filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
1315
+ stream->specific_ctx_id,
1316
+ stream->specific_ctx_id_mask);
13091317
13101318 return 0;
13111319 }
....@@ -1319,95 +1327,106 @@
13191327 */
13201328 static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
13211329 {
1322
- struct drm_i915_private *dev_priv = stream->dev_priv;
13231330 struct intel_context *ce;
13241331
1325
- dev_priv->perf.oa.specific_ctx_id = INVALID_CTX_ID;
1326
- dev_priv->perf.oa.specific_ctx_id_mask = 0;
1327
-
1328
- ce = fetch_and_zero(&dev_priv->perf.oa.pinned_ctx);
1332
+ ce = fetch_and_zero(&stream->pinned_ctx);
13291333 if (ce) {
1330
- mutex_lock(&dev_priv->drm.struct_mutex);
1334
+ ce->tag = 0; /* recomputed on next submission after parking */
13311335 intel_context_unpin(ce);
1332
- mutex_unlock(&dev_priv->drm.struct_mutex);
13331336 }
1337
+
1338
+ stream->specific_ctx_id = INVALID_CTX_ID;
1339
+ stream->specific_ctx_id_mask = 0;
13341340 }
13351341
13361342 static void
1337
-free_oa_buffer(struct drm_i915_private *i915)
1343
+free_oa_buffer(struct i915_perf_stream *stream)
13381344 {
1339
- mutex_lock(&i915->drm.struct_mutex);
1345
+ i915_vma_unpin_and_release(&stream->oa_buffer.vma,
1346
+ I915_VMA_RELEASE_MAP);
13401347
1341
- i915_gem_object_unpin_map(i915->perf.oa.oa_buffer.vma->obj);
1342
- i915_vma_unpin(i915->perf.oa.oa_buffer.vma);
1343
- i915_gem_object_put(i915->perf.oa.oa_buffer.vma->obj);
1348
+ stream->oa_buffer.vaddr = NULL;
1349
+}
13441350
1345
- i915->perf.oa.oa_buffer.vma = NULL;
1346
- i915->perf.oa.oa_buffer.vaddr = NULL;
1351
+static void
1352
+free_oa_configs(struct i915_perf_stream *stream)
1353
+{
1354
+ struct i915_oa_config_bo *oa_bo, *tmp;
13471355
1348
- mutex_unlock(&i915->drm.struct_mutex);
1356
+ i915_oa_config_put(stream->oa_config);
1357
+ llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node)
1358
+ free_oa_config_bo(oa_bo);
1359
+}
1360
+
1361
+static void
1362
+free_noa_wait(struct i915_perf_stream *stream)
1363
+{
1364
+ i915_vma_unpin_and_release(&stream->noa_wait, 0);
13491365 }
13501366
13511367 static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
13521368 {
1353
- struct drm_i915_private *dev_priv = stream->dev_priv;
1369
+ struct i915_perf *perf = stream->perf;
13541370
1355
- BUG_ON(stream != dev_priv->perf.oa.exclusive_stream);
1371
+ BUG_ON(stream != perf->exclusive_stream);
13561372
13571373 /*
13581374 * Unset exclusive_stream first, it will be checked while disabling
13591375 * the metric set on gen8+.
1376
+ *
1377
+ * See i915_oa_init_reg_state() and lrc_configure_all_contexts()
13601378 */
1361
- mutex_lock(&dev_priv->drm.struct_mutex);
1362
- dev_priv->perf.oa.exclusive_stream = NULL;
1363
- dev_priv->perf.oa.ops.disable_metric_set(dev_priv);
1364
- mutex_unlock(&dev_priv->drm.struct_mutex);
1379
+ WRITE_ONCE(perf->exclusive_stream, NULL);
1380
+ perf->ops.disable_metric_set(stream);
13651381
1366
- free_oa_buffer(dev_priv);
1382
+ free_oa_buffer(stream);
13671383
1368
- intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
1369
- intel_runtime_pm_put(dev_priv);
1384
+ intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
1385
+ intel_engine_pm_put(stream->engine);
13701386
13711387 if (stream->ctx)
13721388 oa_put_render_ctx_id(stream);
13731389
1374
- put_oa_config(dev_priv, stream->oa_config);
1390
+ free_oa_configs(stream);
1391
+ free_noa_wait(stream);
13751392
1376
- if (dev_priv->perf.oa.spurious_report_rs.missed) {
1393
+ if (perf->spurious_report_rs.missed) {
13771394 DRM_NOTE("%d spurious OA report notices suppressed due to ratelimiting\n",
1378
- dev_priv->perf.oa.spurious_report_rs.missed);
1395
+ perf->spurious_report_rs.missed);
13791396 }
13801397 }
13811398
1382
-static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv)
1399
+static void gen7_init_oa_buffer(struct i915_perf_stream *stream)
13831400 {
1384
- u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
1401
+ struct intel_uncore *uncore = stream->uncore;
1402
+ u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
13851403 unsigned long flags;
13861404
1387
- spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1405
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
13881406
13891407 /* Pre-DevBDW: OABUFFER must be set with counters off,
13901408 * before OASTATUS1, but after OASTATUS2
13911409 */
1392
- I915_WRITE(GEN7_OASTATUS2,
1393
- gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT); /* head */
1394
- dev_priv->perf.oa.oa_buffer.head = gtt_offset;
1410
+ intel_uncore_write(uncore, GEN7_OASTATUS2, /* head */
1411
+ gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT);
1412
+ stream->oa_buffer.head = gtt_offset;
13951413
1396
- I915_WRITE(GEN7_OABUFFER, gtt_offset);
1414
+ intel_uncore_write(uncore, GEN7_OABUFFER, gtt_offset);
13971415
1398
- I915_WRITE(GEN7_OASTATUS1, gtt_offset | OABUFFER_SIZE_16M); /* tail */
1416
+ intel_uncore_write(uncore, GEN7_OASTATUS1, /* tail */
1417
+ gtt_offset | OABUFFER_SIZE_16M);
13991418
14001419 /* Mark that we need updated tail pointers to read from... */
1401
- dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1402
- dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1420
+ stream->oa_buffer.aging_tail = INVALID_TAIL_PTR;
1421
+ stream->oa_buffer.tail = gtt_offset;
14031422
1404
- spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1423
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
14051424
14061425 /* On Haswell we have to track which OASTATUS1 flags we've
14071426 * already seen since they can't be cleared while periodic
14081427 * sampling is enabled.
14091428 */
1410
- dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
1429
+ stream->perf->gen7_latched_oastatus1 = 0;
14111430
14121431 /* NB: although the OA buffer will initially be allocated
14131432 * zeroed via shmfs (and so this memset is redundant when
....@@ -1420,26 +1439,22 @@
14201439 * the assumption that new reports are being written to zeroed
14211440 * memory...
14221441 */
1423
- memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1424
-
1425
- /* Maybe make ->pollin per-stream state if we support multiple
1426
- * concurrent streams in the future.
1427
- */
1428
- dev_priv->perf.oa.pollin = false;
1442
+ memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
14291443 }
14301444
1431
-static void gen8_init_oa_buffer(struct drm_i915_private *dev_priv)
1445
+static void gen8_init_oa_buffer(struct i915_perf_stream *stream)
14321446 {
1433
- u32 gtt_offset = i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma);
1447
+ struct intel_uncore *uncore = stream->uncore;
1448
+ u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
14341449 unsigned long flags;
14351450
1436
- spin_lock_irqsave(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1451
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
14371452
1438
- I915_WRITE(GEN8_OASTATUS, 0);
1439
- I915_WRITE(GEN8_OAHEADPTR, gtt_offset);
1440
- dev_priv->perf.oa.oa_buffer.head = gtt_offset;
1453
+ intel_uncore_write(uncore, GEN8_OASTATUS, 0);
1454
+ intel_uncore_write(uncore, GEN8_OAHEADPTR, gtt_offset);
1455
+ stream->oa_buffer.head = gtt_offset;
14411456
1442
- I915_WRITE(GEN8_OABUFFER_UDW, 0);
1457
+ intel_uncore_write(uncore, GEN8_OABUFFER_UDW, 0);
14431458
14441459 /*
14451460 * PRM says:
....@@ -1449,22 +1464,22 @@
14491464 * to enable proper functionality of the overflow
14501465 * bit."
14511466 */
1452
- I915_WRITE(GEN8_OABUFFER, gtt_offset |
1467
+ intel_uncore_write(uncore, GEN8_OABUFFER, gtt_offset |
14531468 OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
1454
- I915_WRITE(GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
1469
+ intel_uncore_write(uncore, GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
14551470
14561471 /* Mark that we need updated tail pointers to read from... */
1457
- dev_priv->perf.oa.oa_buffer.tails[0].offset = INVALID_TAIL_PTR;
1458
- dev_priv->perf.oa.oa_buffer.tails[1].offset = INVALID_TAIL_PTR;
1472
+ stream->oa_buffer.aging_tail = INVALID_TAIL_PTR;
1473
+ stream->oa_buffer.tail = gtt_offset;
14591474
14601475 /*
14611476 * Reset state used to recognise context switches, affecting which
14621477 * reports we will forward to userspace while filtering for a single
14631478 * context.
14641479 */
1465
- dev_priv->perf.oa.oa_buffer.last_ctx_id = INVALID_CTX_ID;
1480
+ stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
14661481
1467
- spin_unlock_irqrestore(&dev_priv->perf.oa.oa_buffer.ptr_lock, flags);
1482
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
14681483
14691484 /*
14701485 * NB: although the OA buffer will initially be allocated
....@@ -1478,41 +1493,84 @@
14781493 * the assumption that new reports are being written to zeroed
14791494 * memory...
14801495 */
1481
- memset(dev_priv->perf.oa.oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
1482
-
1483
- /*
1484
- * Maybe make ->pollin per-stream state if we support multiple
1485
- * concurrent streams in the future.
1486
- */
1487
- dev_priv->perf.oa.pollin = false;
1496
+ memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
14881497 }
14891498
1490
-static int alloc_oa_buffer(struct drm_i915_private *dev_priv)
1499
+static void gen12_init_oa_buffer(struct i915_perf_stream *stream)
14911500 {
1501
+ struct intel_uncore *uncore = stream->uncore;
1502
+ u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
1503
+ unsigned long flags;
1504
+
1505
+ spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
1506
+
1507
+ intel_uncore_write(uncore, GEN12_OAG_OASTATUS, 0);
1508
+ intel_uncore_write(uncore, GEN12_OAG_OAHEADPTR,
1509
+ gtt_offset & GEN12_OAG_OAHEADPTR_MASK);
1510
+ stream->oa_buffer.head = gtt_offset;
1511
+
1512
+ /*
1513
+ * PRM says:
1514
+ *
1515
+ * "This MMIO must be set before the OATAILPTR
1516
+ * register and after the OAHEADPTR register. This is
1517
+ * to enable proper functionality of the overflow
1518
+ * bit."
1519
+ */
1520
+ intel_uncore_write(uncore, GEN12_OAG_OABUFFER, gtt_offset |
1521
+ OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
1522
+ intel_uncore_write(uncore, GEN12_OAG_OATAILPTR,
1523
+ gtt_offset & GEN12_OAG_OATAILPTR_MASK);
1524
+
1525
+ /* Mark that we need updated tail pointers to read from... */
1526
+ stream->oa_buffer.aging_tail = INVALID_TAIL_PTR;
1527
+ stream->oa_buffer.tail = gtt_offset;
1528
+
1529
+ /*
1530
+ * Reset state used to recognise context switches, affecting which
1531
+ * reports we will forward to userspace while filtering for a single
1532
+ * context.
1533
+ */
1534
+ stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
1535
+
1536
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
1537
+
1538
+ /*
1539
+ * NB: although the OA buffer will initially be allocated
1540
+ * zeroed via shmfs (and so this memset is redundant when
1541
+ * first allocating), we may re-init the OA buffer, either
1542
+ * when re-enabling a stream or in error/reset paths.
1543
+ *
1544
+ * The reason we clear the buffer for each re-init is for the
1545
+ * sanity check in gen8_append_oa_reports() that looks at the
1546
+ * reason field to make sure it's non-zero which relies on
1547
+ * the assumption that new reports are being written to zeroed
1548
+ * memory...
1549
+ */
1550
+ memset(stream->oa_buffer.vaddr, 0,
1551
+ stream->oa_buffer.vma->size);
1552
+}
1553
+
1554
+static int alloc_oa_buffer(struct i915_perf_stream *stream)
1555
+{
1556
+ struct drm_i915_private *i915 = stream->perf->i915;
14921557 struct drm_i915_gem_object *bo;
14931558 struct i915_vma *vma;
14941559 int ret;
14951560
1496
- if (WARN_ON(dev_priv->perf.oa.oa_buffer.vma))
1561
+ if (drm_WARN_ON(&i915->drm, stream->oa_buffer.vma))
14971562 return -ENODEV;
1498
-
1499
- ret = i915_mutex_lock_interruptible(&dev_priv->drm);
1500
- if (ret)
1501
- return ret;
15021563
15031564 BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
15041565 BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
15051566
1506
- bo = i915_gem_object_create(dev_priv, OA_BUFFER_SIZE);
1567
+ bo = i915_gem_object_create_shmem(stream->perf->i915, OA_BUFFER_SIZE);
15071568 if (IS_ERR(bo)) {
1508
- DRM_ERROR("Failed to allocate OA buffer\n");
1509
- ret = PTR_ERR(bo);
1510
- goto unlock;
1569
+ drm_err(&i915->drm, "Failed to allocate OA buffer\n");
1570
+ return PTR_ERR(bo);
15111571 }
15121572
1513
- ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC);
1514
- if (ret)
1515
- goto err_unref;
1573
+ i915_gem_object_set_cache_coherency(bo, I915_CACHE_LLC);
15161574
15171575 /* PreHSW required 512K alignment, HSW requires 16M */
15181576 vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, 0);
....@@ -1520,22 +1578,16 @@
15201578 ret = PTR_ERR(vma);
15211579 goto err_unref;
15221580 }
1523
- dev_priv->perf.oa.oa_buffer.vma = vma;
1581
+ stream->oa_buffer.vma = vma;
15241582
1525
- dev_priv->perf.oa.oa_buffer.vaddr =
1583
+ stream->oa_buffer.vaddr =
15261584 i915_gem_object_pin_map(bo, I915_MAP_WB);
1527
- if (IS_ERR(dev_priv->perf.oa.oa_buffer.vaddr)) {
1528
- ret = PTR_ERR(dev_priv->perf.oa.oa_buffer.vaddr);
1585
+ if (IS_ERR(stream->oa_buffer.vaddr)) {
1586
+ ret = PTR_ERR(stream->oa_buffer.vaddr);
15291587 goto err_unpin;
15301588 }
15311589
1532
- dev_priv->perf.oa.ops.init_oa_buffer(dev_priv);
1533
-
1534
- DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p\n",
1535
- i915_ggtt_offset(dev_priv->perf.oa.oa_buffer.vma),
1536
- dev_priv->perf.oa.oa_buffer.vaddr);
1537
-
1538
- goto unlock;
1590
+ return 0;
15391591
15401592 err_unpin:
15411593 __i915_vma_unpin(vma);
....@@ -1543,31 +1595,429 @@
15431595 err_unref:
15441596 i915_gem_object_put(bo);
15451597
1546
- dev_priv->perf.oa.oa_buffer.vaddr = NULL;
1547
- dev_priv->perf.oa.oa_buffer.vma = NULL;
1598
+ stream->oa_buffer.vaddr = NULL;
1599
+ stream->oa_buffer.vma = NULL;
15481600
1549
-unlock:
1550
- mutex_unlock(&dev_priv->drm.struct_mutex);
15511601 return ret;
15521602 }
15531603
1554
-static void config_oa_regs(struct drm_i915_private *dev_priv,
1555
- const struct i915_oa_reg *regs,
1556
- u32 n_regs)
1604
+static u32 *save_restore_register(struct i915_perf_stream *stream, u32 *cs,
1605
+ bool save, i915_reg_t reg, u32 offset,
1606
+ u32 dword_count)
1607
+{
1608
+ u32 cmd;
1609
+ u32 d;
1610
+
1611
+ cmd = save ? MI_STORE_REGISTER_MEM : MI_LOAD_REGISTER_MEM;
1612
+ cmd |= MI_SRM_LRM_GLOBAL_GTT;
1613
+ if (INTEL_GEN(stream->perf->i915) >= 8)
1614
+ cmd++;
1615
+
1616
+ for (d = 0; d < dword_count; d++) {
1617
+ *cs++ = cmd;
1618
+ *cs++ = i915_mmio_reg_offset(reg) + 4 * d;
1619
+ *cs++ = intel_gt_scratch_offset(stream->engine->gt,
1620
+ offset) + 4 * d;
1621
+ *cs++ = 0;
1622
+ }
1623
+
1624
+ return cs;
1625
+}
1626
+
1627
+static int alloc_noa_wait(struct i915_perf_stream *stream)
1628
+{
1629
+ struct drm_i915_private *i915 = stream->perf->i915;
1630
+ struct drm_i915_gem_object *bo;
1631
+ struct i915_vma *vma;
1632
+ const u64 delay_ticks = 0xffffffffffffffff -
1633
+ i915_cs_timestamp_ns_to_ticks(i915, atomic64_read(&stream->perf->noa_programming_delay));
1634
+ const u32 base = stream->engine->mmio_base;
1635
+#define CS_GPR(x) GEN8_RING_CS_GPR(base, x)
1636
+ u32 *batch, *ts0, *cs, *jump;
1637
+ int ret, i;
1638
+ enum {
1639
+ START_TS,
1640
+ NOW_TS,
1641
+ DELTA_TS,
1642
+ JUMP_PREDICATE,
1643
+ DELTA_TARGET,
1644
+ N_CS_GPR
1645
+ };
1646
+
1647
+ bo = i915_gem_object_create_internal(i915, 4096);
1648
+ if (IS_ERR(bo)) {
1649
+ drm_err(&i915->drm,
1650
+ "Failed to allocate NOA wait batchbuffer\n");
1651
+ return PTR_ERR(bo);
1652
+ }
1653
+
1654
+ /*
1655
+ * We pin in GGTT because we jump into this buffer now because
1656
+ * multiple OA config BOs will have a jump to this address and it
1657
+ * needs to be fixed during the lifetime of the i915/perf stream.
1658
+ */
1659
+ vma = i915_gem_object_ggtt_pin(bo, NULL, 0, 0, PIN_HIGH);
1660
+ if (IS_ERR(vma)) {
1661
+ ret = PTR_ERR(vma);
1662
+ goto err_unref;
1663
+ }
1664
+
1665
+ batch = cs = i915_gem_object_pin_map(bo, I915_MAP_WB);
1666
+ if (IS_ERR(batch)) {
1667
+ ret = PTR_ERR(batch);
1668
+ goto err_unpin;
1669
+ }
1670
+
1671
+ /* Save registers. */
1672
+ for (i = 0; i < N_CS_GPR; i++)
1673
+ cs = save_restore_register(
1674
+ stream, cs, true /* save */, CS_GPR(i),
1675
+ INTEL_GT_SCRATCH_FIELD_PERF_CS_GPR + 8 * i, 2);
1676
+ cs = save_restore_register(
1677
+ stream, cs, true /* save */, MI_PREDICATE_RESULT_1,
1678
+ INTEL_GT_SCRATCH_FIELD_PERF_PREDICATE_RESULT_1, 1);
1679
+
1680
+ /* First timestamp snapshot location. */
1681
+ ts0 = cs;
1682
+
1683
+ /*
1684
+ * Initial snapshot of the timestamp register to implement the wait.
1685
+ * We work with 32b values, so clear out the top 32b bits of the
1686
+ * register because the ALU works 64bits.
1687
+ */
1688
+ *cs++ = MI_LOAD_REGISTER_IMM(1);
1689
+ *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS)) + 4;
1690
+ *cs++ = 0;
1691
+ *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1692
+ *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
1693
+ *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS));
1694
+
1695
+ /*
1696
+ * This is the location we're going to jump back into until the
1697
+ * required amount of time has passed.
1698
+ */
1699
+ jump = cs;
1700
+
1701
+ /*
1702
+ * Take another snapshot of the timestamp register. Take care to clear
1703
+ * up the top 32bits of CS_GPR(1) as we're using it for other
1704
+ * operations below.
1705
+ */
1706
+ *cs++ = MI_LOAD_REGISTER_IMM(1);
1707
+ *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS)) + 4;
1708
+ *cs++ = 0;
1709
+ *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1710
+ *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
1711
+ *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS));
1712
+
1713
+ /*
1714
+ * Do a diff between the 2 timestamps and store the result back into
1715
+ * CS_GPR(1).
1716
+ */
1717
+ *cs++ = MI_MATH(5);
1718
+ *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(NOW_TS));
1719
+ *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(START_TS));
1720
+ *cs++ = MI_MATH_SUB;
1721
+ *cs++ = MI_MATH_STORE(MI_MATH_REG(DELTA_TS), MI_MATH_REG_ACCU);
1722
+ *cs++ = MI_MATH_STORE(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
1723
+
1724
+ /*
1725
+ * Transfer the carry flag (set to 1 if ts1 < ts0, meaning the
1726
+ * timestamp have rolled over the 32bits) into the predicate register
1727
+ * to be used for the predicated jump.
1728
+ */
1729
+ *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1730
+ *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
1731
+ *cs++ = i915_mmio_reg_offset(MI_PREDICATE_RESULT_1);
1732
+
1733
+ /* Restart from the beginning if we had timestamps roll over. */
1734
+ *cs++ = (INTEL_GEN(i915) < 8 ?
1735
+ MI_BATCH_BUFFER_START :
1736
+ MI_BATCH_BUFFER_START_GEN8) |
1737
+ MI_BATCH_PREDICATE;
1738
+ *cs++ = i915_ggtt_offset(vma) + (ts0 - batch) * 4;
1739
+ *cs++ = 0;
1740
+
1741
+ /*
1742
+ * Now add the diff between to previous timestamps and add it to :
1743
+ * (((1 * << 64) - 1) - delay_ns)
1744
+ *
1745
+ * When the Carry Flag contains 1 this means the elapsed time is
1746
+ * longer than the expected delay, and we can exit the wait loop.
1747
+ */
1748
+ *cs++ = MI_LOAD_REGISTER_IMM(2);
1749
+ *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET));
1750
+ *cs++ = lower_32_bits(delay_ticks);
1751
+ *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET)) + 4;
1752
+ *cs++ = upper_32_bits(delay_ticks);
1753
+
1754
+ *cs++ = MI_MATH(4);
1755
+ *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(DELTA_TS));
1756
+ *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(DELTA_TARGET));
1757
+ *cs++ = MI_MATH_ADD;
1758
+ *cs++ = MI_MATH_STOREINV(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
1759
+
1760
+ *cs++ = MI_ARB_CHECK;
1761
+
1762
+ /*
1763
+ * Transfer the result into the predicate register to be used for the
1764
+ * predicated jump.
1765
+ */
1766
+ *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
1767
+ *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
1768
+ *cs++ = i915_mmio_reg_offset(MI_PREDICATE_RESULT_1);
1769
+
1770
+ /* Predicate the jump. */
1771
+ *cs++ = (INTEL_GEN(i915) < 8 ?
1772
+ MI_BATCH_BUFFER_START :
1773
+ MI_BATCH_BUFFER_START_GEN8) |
1774
+ MI_BATCH_PREDICATE;
1775
+ *cs++ = i915_ggtt_offset(vma) + (jump - batch) * 4;
1776
+ *cs++ = 0;
1777
+
1778
+ /* Restore registers. */
1779
+ for (i = 0; i < N_CS_GPR; i++)
1780
+ cs = save_restore_register(
1781
+ stream, cs, false /* restore */, CS_GPR(i),
1782
+ INTEL_GT_SCRATCH_FIELD_PERF_CS_GPR + 8 * i, 2);
1783
+ cs = save_restore_register(
1784
+ stream, cs, false /* restore */, MI_PREDICATE_RESULT_1,
1785
+ INTEL_GT_SCRATCH_FIELD_PERF_PREDICATE_RESULT_1, 1);
1786
+
1787
+ /* And return to the ring. */
1788
+ *cs++ = MI_BATCH_BUFFER_END;
1789
+
1790
+ GEM_BUG_ON(cs - batch > PAGE_SIZE / sizeof(*batch));
1791
+
1792
+ i915_gem_object_flush_map(bo);
1793
+ __i915_gem_object_release_map(bo);
1794
+
1795
+ stream->noa_wait = vma;
1796
+ return 0;
1797
+
1798
+err_unpin:
1799
+ i915_vma_unpin_and_release(&vma, 0);
1800
+err_unref:
1801
+ i915_gem_object_put(bo);
1802
+ return ret;
1803
+}
1804
+
1805
+static u32 *write_cs_mi_lri(u32 *cs,
1806
+ const struct i915_oa_reg *reg_data,
1807
+ u32 n_regs)
15571808 {
15581809 u32 i;
15591810
15601811 for (i = 0; i < n_regs; i++) {
1561
- const struct i915_oa_reg *reg = regs + i;
1812
+ if ((i % MI_LOAD_REGISTER_IMM_MAX_REGS) == 0) {
1813
+ u32 n_lri = min_t(u32,
1814
+ n_regs - i,
1815
+ MI_LOAD_REGISTER_IMM_MAX_REGS);
15621816
1563
- I915_WRITE(reg->addr, reg->value);
1817
+ *cs++ = MI_LOAD_REGISTER_IMM(n_lri);
1818
+ }
1819
+ *cs++ = i915_mmio_reg_offset(reg_data[i].addr);
1820
+ *cs++ = reg_data[i].value;
15641821 }
1822
+
1823
+ return cs;
15651824 }
15661825
1567
-static int hsw_enable_metric_set(struct drm_i915_private *dev_priv,
1568
- const struct i915_oa_config *oa_config)
1826
+static int num_lri_dwords(int num_regs)
15691827 {
1570
- /* PRM:
1828
+ int count = 0;
1829
+
1830
+ if (num_regs > 0) {
1831
+ count += DIV_ROUND_UP(num_regs, MI_LOAD_REGISTER_IMM_MAX_REGS);
1832
+ count += num_regs * 2;
1833
+ }
1834
+
1835
+ return count;
1836
+}
1837
+
1838
+static struct i915_oa_config_bo *
1839
+alloc_oa_config_buffer(struct i915_perf_stream *stream,
1840
+ struct i915_oa_config *oa_config)
1841
+{
1842
+ struct drm_i915_gem_object *obj;
1843
+ struct i915_oa_config_bo *oa_bo;
1844
+ size_t config_length = 0;
1845
+ u32 *cs;
1846
+ int err;
1847
+
1848
+ oa_bo = kzalloc(sizeof(*oa_bo), GFP_KERNEL);
1849
+ if (!oa_bo)
1850
+ return ERR_PTR(-ENOMEM);
1851
+
1852
+ config_length += num_lri_dwords(oa_config->mux_regs_len);
1853
+ config_length += num_lri_dwords(oa_config->b_counter_regs_len);
1854
+ config_length += num_lri_dwords(oa_config->flex_regs_len);
1855
+ config_length += 3; /* MI_BATCH_BUFFER_START */
1856
+ config_length = ALIGN(sizeof(u32) * config_length, I915_GTT_PAGE_SIZE);
1857
+
1858
+ obj = i915_gem_object_create_shmem(stream->perf->i915, config_length);
1859
+ if (IS_ERR(obj)) {
1860
+ err = PTR_ERR(obj);
1861
+ goto err_free;
1862
+ }
1863
+
1864
+ cs = i915_gem_object_pin_map(obj, I915_MAP_WB);
1865
+ if (IS_ERR(cs)) {
1866
+ err = PTR_ERR(cs);
1867
+ goto err_oa_bo;
1868
+ }
1869
+
1870
+ cs = write_cs_mi_lri(cs,
1871
+ oa_config->mux_regs,
1872
+ oa_config->mux_regs_len);
1873
+ cs = write_cs_mi_lri(cs,
1874
+ oa_config->b_counter_regs,
1875
+ oa_config->b_counter_regs_len);
1876
+ cs = write_cs_mi_lri(cs,
1877
+ oa_config->flex_regs,
1878
+ oa_config->flex_regs_len);
1879
+
1880
+ /* Jump into the active wait. */
1881
+ *cs++ = (INTEL_GEN(stream->perf->i915) < 8 ?
1882
+ MI_BATCH_BUFFER_START :
1883
+ MI_BATCH_BUFFER_START_GEN8);
1884
+ *cs++ = i915_ggtt_offset(stream->noa_wait);
1885
+ *cs++ = 0;
1886
+
1887
+ i915_gem_object_flush_map(obj);
1888
+ __i915_gem_object_release_map(obj);
1889
+
1890
+ oa_bo->vma = i915_vma_instance(obj,
1891
+ &stream->engine->gt->ggtt->vm,
1892
+ NULL);
1893
+ if (IS_ERR(oa_bo->vma)) {
1894
+ err = PTR_ERR(oa_bo->vma);
1895
+ goto err_oa_bo;
1896
+ }
1897
+
1898
+ oa_bo->oa_config = i915_oa_config_get(oa_config);
1899
+ llist_add(&oa_bo->node, &stream->oa_config_bos);
1900
+
1901
+ return oa_bo;
1902
+
1903
+err_oa_bo:
1904
+ i915_gem_object_put(obj);
1905
+err_free:
1906
+ kfree(oa_bo);
1907
+ return ERR_PTR(err);
1908
+}
1909
+
1910
+static struct i915_vma *
1911
+get_oa_vma(struct i915_perf_stream *stream, struct i915_oa_config *oa_config)
1912
+{
1913
+ struct i915_oa_config_bo *oa_bo;
1914
+
1915
+ /*
1916
+ * Look for the buffer in the already allocated BOs attached
1917
+ * to the stream.
1918
+ */
1919
+ llist_for_each_entry(oa_bo, stream->oa_config_bos.first, node) {
1920
+ if (oa_bo->oa_config == oa_config &&
1921
+ memcmp(oa_bo->oa_config->uuid,
1922
+ oa_config->uuid,
1923
+ sizeof(oa_config->uuid)) == 0)
1924
+ goto out;
1925
+ }
1926
+
1927
+ oa_bo = alloc_oa_config_buffer(stream, oa_config);
1928
+ if (IS_ERR(oa_bo))
1929
+ return ERR_CAST(oa_bo);
1930
+
1931
+out:
1932
+ return i915_vma_get(oa_bo->vma);
1933
+}
1934
+
1935
+static int
1936
+emit_oa_config(struct i915_perf_stream *stream,
1937
+ struct i915_oa_config *oa_config,
1938
+ struct intel_context *ce,
1939
+ struct i915_active *active)
1940
+{
1941
+ struct i915_request *rq;
1942
+ struct i915_vma *vma;
1943
+ struct i915_gem_ww_ctx ww;
1944
+ int err;
1945
+
1946
+ vma = get_oa_vma(stream, oa_config);
1947
+ if (IS_ERR(vma))
1948
+ return PTR_ERR(vma);
1949
+
1950
+ i915_gem_ww_ctx_init(&ww, true);
1951
+retry:
1952
+ err = i915_gem_object_lock(vma->obj, &ww);
1953
+ if (err)
1954
+ goto err;
1955
+
1956
+ err = i915_vma_pin_ww(vma, &ww, 0, 0, PIN_GLOBAL | PIN_HIGH);
1957
+ if (err)
1958
+ goto err;
1959
+
1960
+ intel_engine_pm_get(ce->engine);
1961
+ rq = i915_request_create(ce);
1962
+ intel_engine_pm_put(ce->engine);
1963
+ if (IS_ERR(rq)) {
1964
+ err = PTR_ERR(rq);
1965
+ goto err_vma_unpin;
1966
+ }
1967
+
1968
+ if (!IS_ERR_OR_NULL(active)) {
1969
+ /* After all individual context modifications */
1970
+ err = i915_request_await_active(rq, active,
1971
+ I915_ACTIVE_AWAIT_ACTIVE);
1972
+ if (err)
1973
+ goto err_add_request;
1974
+
1975
+ err = i915_active_add_request(active, rq);
1976
+ if (err)
1977
+ goto err_add_request;
1978
+ }
1979
+
1980
+ err = i915_request_await_object(rq, vma->obj, 0);
1981
+ if (!err)
1982
+ err = i915_vma_move_to_active(vma, rq, 0);
1983
+ if (err)
1984
+ goto err_add_request;
1985
+
1986
+ err = rq->engine->emit_bb_start(rq,
1987
+ vma->node.start, 0,
1988
+ I915_DISPATCH_SECURE);
1989
+ if (err)
1990
+ goto err_add_request;
1991
+
1992
+err_add_request:
1993
+ i915_request_add(rq);
1994
+err_vma_unpin:
1995
+ i915_vma_unpin(vma);
1996
+err:
1997
+ if (err == -EDEADLK) {
1998
+ err = i915_gem_ww_ctx_backoff(&ww);
1999
+ if (!err)
2000
+ goto retry;
2001
+ }
2002
+
2003
+ i915_gem_ww_ctx_fini(&ww);
2004
+ i915_vma_put(vma);
2005
+ return err;
2006
+}
2007
+
2008
+static struct intel_context *oa_context(struct i915_perf_stream *stream)
2009
+{
2010
+ return stream->pinned_ctx ?: stream->engine->kernel_context;
2011
+}
2012
+
2013
+static int
2014
+hsw_enable_metric_set(struct i915_perf_stream *stream,
2015
+ struct i915_active *active)
2016
+{
2017
+ struct intel_uncore *uncore = stream->uncore;
2018
+
2019
+ /*
2020
+ * PRM:
15712021 *
15722022 * OA unit is using “crclk” for its functionality. When trunk
15732023 * level clock gating takes place, OA clock would be gated,
....@@ -1576,53 +2026,49 @@
15762026 * count the events from non-render domain. Unit level clock
15772027 * gating for RCS should also be disabled.
15782028 */
1579
- I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1580
- ~GEN7_DOP_CLOCK_GATE_ENABLE));
1581
- I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) |
1582
- GEN6_CSUNIT_CLOCK_GATE_DISABLE));
2029
+ intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2030
+ GEN7_DOP_CLOCK_GATE_ENABLE, 0);
2031
+ intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2032
+ 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
15832033
1584
- config_oa_regs(dev_priv, oa_config->mux_regs, oa_config->mux_regs_len);
2034
+ return emit_oa_config(stream,
2035
+ stream->oa_config, oa_context(stream),
2036
+ active);
2037
+}
15852038
1586
- /* It apparently takes a fairly long time for a new MUX
1587
- * configuration to be be applied after these register writes.
1588
- * This delay duration was derived empirically based on the
1589
- * render_basic config but hopefully it covers the maximum
1590
- * configuration latency.
1591
- *
1592
- * As a fallback, the checks in _append_oa_reports() to skip
1593
- * invalid OA reports do also seem to work to discard reports
1594
- * generated before this config has completed - albeit not
1595
- * silently.
1596
- *
1597
- * Unfortunately this is essentially a magic number, since we
1598
- * don't currently know of a reliable mechanism for predicting
1599
- * how long the MUX config will take to apply and besides
1600
- * seeing invalid reports we don't know of a reliable way to
1601
- * explicitly check that the MUX config has landed.
1602
- *
1603
- * It's even possible we've miss characterized the underlying
1604
- * problem - it just seems like the simplest explanation why
1605
- * a delay at this location would mitigate any invalid reports.
2039
+static void hsw_disable_metric_set(struct i915_perf_stream *stream)
2040
+{
2041
+ struct intel_uncore *uncore = stream->uncore;
2042
+
2043
+ intel_uncore_rmw(uncore, GEN6_UCGCTL1,
2044
+ GEN6_CSUNIT_CLOCK_GATE_DISABLE, 0);
2045
+ intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
2046
+ 0, GEN7_DOP_CLOCK_GATE_ENABLE);
2047
+
2048
+ intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
2049
+}
2050
+
2051
+static u32 oa_config_flex_reg(const struct i915_oa_config *oa_config,
2052
+ i915_reg_t reg)
2053
+{
2054
+ u32 mmio = i915_mmio_reg_offset(reg);
2055
+ int i;
2056
+
2057
+ /*
2058
+ * This arbitrary default will select the 'EU FPU0 Pipeline
2059
+ * Active' event. In the future it's anticipated that there
2060
+ * will be an explicit 'No Event' we can select, but not yet...
16062061 */
1607
- usleep_range(15000, 20000);
2062
+ if (!oa_config)
2063
+ return 0;
16082064
1609
- config_oa_regs(dev_priv, oa_config->b_counter_regs,
1610
- oa_config->b_counter_regs_len);
2065
+ for (i = 0; i < oa_config->flex_regs_len; i++) {
2066
+ if (i915_mmio_reg_offset(oa_config->flex_regs[i].addr) == mmio)
2067
+ return oa_config->flex_regs[i].value;
2068
+ }
16112069
16122070 return 0;
16132071 }
1614
-
1615
-static void hsw_disable_metric_set(struct drm_i915_private *dev_priv)
1616
-{
1617
- I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) &
1618
- ~GEN6_CSUNIT_CLOCK_GATE_DISABLE));
1619
- I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) |
1620
- GEN7_DOP_CLOCK_GATE_ENABLE));
1621
-
1622
- I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
1623
- ~GT_NOA_ENABLE));
1624
-}
1625
-
16262072 /*
16272073 * NB: It must always remain pointer safe to run this even if the OA unit
16282074 * has been disabled.
....@@ -1630,158 +2076,214 @@
16302076 * It's fine to put out-of-date values into these per-context registers
16312077 * in the case that the OA unit has been disabled.
16322078 */
1633
-static void gen8_update_reg_state_unlocked(struct i915_gem_context *ctx,
1634
- u32 *reg_state,
1635
- const struct i915_oa_config *oa_config)
2079
+static void
2080
+gen8_update_reg_state_unlocked(const struct intel_context *ce,
2081
+ const struct i915_perf_stream *stream)
16362082 {
1637
- struct drm_i915_private *dev_priv = ctx->i915;
1638
- u32 ctx_oactxctrl = dev_priv->perf.oa.ctx_oactxctrl_offset;
1639
- u32 ctx_flexeu0 = dev_priv->perf.oa.ctx_flexeu0_offset;
2083
+ u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
2084
+ u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
16402085 /* The MMIO offsets for Flex EU registers aren't contiguous */
1641
- u32 flex_mmio[] = {
1642
- i915_mmio_reg_offset(EU_PERF_CNTL0),
1643
- i915_mmio_reg_offset(EU_PERF_CNTL1),
1644
- i915_mmio_reg_offset(EU_PERF_CNTL2),
1645
- i915_mmio_reg_offset(EU_PERF_CNTL3),
1646
- i915_mmio_reg_offset(EU_PERF_CNTL4),
1647
- i915_mmio_reg_offset(EU_PERF_CNTL5),
1648
- i915_mmio_reg_offset(EU_PERF_CNTL6),
2086
+ i915_reg_t flex_regs[] = {
2087
+ EU_PERF_CNTL0,
2088
+ EU_PERF_CNTL1,
2089
+ EU_PERF_CNTL2,
2090
+ EU_PERF_CNTL3,
2091
+ EU_PERF_CNTL4,
2092
+ EU_PERF_CNTL5,
2093
+ EU_PERF_CNTL6,
16492094 };
2095
+ u32 *reg_state = ce->lrc_reg_state;
16502096 int i;
16512097
1652
- reg_state[ctx_oactxctrl] = i915_mmio_reg_offset(GEN8_OACTXCONTROL);
1653
- reg_state[ctx_oactxctrl+1] = (dev_priv->perf.oa.period_exponent <<
1654
- GEN8_OA_TIMER_PERIOD_SHIFT) |
1655
- (dev_priv->perf.oa.periodic ?
1656
- GEN8_OA_TIMER_ENABLE : 0) |
1657
- GEN8_OA_COUNTER_RESUME;
2098
+ reg_state[ctx_oactxctrl + 1] =
2099
+ (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2100
+ (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2101
+ GEN8_OA_COUNTER_RESUME;
16582102
1659
- for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) {
1660
- u32 state_offset = ctx_flexeu0 + i * 2;
1661
- u32 mmio = flex_mmio[i];
1662
-
1663
- /*
1664
- * This arbitrary default will select the 'EU FPU0 Pipeline
1665
- * Active' event. In the future it's anticipated that there
1666
- * will be an explicit 'No Event' we can select, but not yet...
1667
- */
1668
- u32 value = 0;
1669
-
1670
- if (oa_config) {
1671
- u32 j;
1672
-
1673
- for (j = 0; j < oa_config->flex_regs_len; j++) {
1674
- if (i915_mmio_reg_offset(oa_config->flex_regs[j].addr) == mmio) {
1675
- value = oa_config->flex_regs[j].value;
1676
- break;
1677
- }
1678
- }
1679
- }
1680
-
1681
- reg_state[state_offset] = mmio;
1682
- reg_state[state_offset+1] = value;
1683
- }
2103
+ for (i = 0; i < ARRAY_SIZE(flex_regs); i++)
2104
+ reg_state[ctx_flexeu0 + i * 2 + 1] =
2105
+ oa_config_flex_reg(stream->oa_config, flex_regs[i]);
16842106 }
16852107
1686
-/*
1687
- * Same as gen8_update_reg_state_unlocked only through the batchbuffer. This
1688
- * is only used by the kernel context.
1689
- */
1690
-static int gen8_emit_oa_config(struct i915_request *rq,
1691
- const struct i915_oa_config *oa_config)
1692
-{
1693
- struct drm_i915_private *dev_priv = rq->i915;
1694
- /* The MMIO offsets for Flex EU registers aren't contiguous */
1695
- u32 flex_mmio[] = {
1696
- i915_mmio_reg_offset(EU_PERF_CNTL0),
1697
- i915_mmio_reg_offset(EU_PERF_CNTL1),
1698
- i915_mmio_reg_offset(EU_PERF_CNTL2),
1699
- i915_mmio_reg_offset(EU_PERF_CNTL3),
1700
- i915_mmio_reg_offset(EU_PERF_CNTL4),
1701
- i915_mmio_reg_offset(EU_PERF_CNTL5),
1702
- i915_mmio_reg_offset(EU_PERF_CNTL6),
1703
- };
1704
- u32 *cs;
1705
- int i;
2108
+struct flex {
2109
+ i915_reg_t reg;
2110
+ u32 offset;
2111
+ u32 value;
2112
+};
17062113
1707
- cs = intel_ring_begin(rq, ARRAY_SIZE(flex_mmio) * 2 + 4);
2114
+static int
2115
+gen8_store_flex(struct i915_request *rq,
2116
+ struct intel_context *ce,
2117
+ const struct flex *flex, unsigned int count)
2118
+{
2119
+ u32 offset;
2120
+ u32 *cs;
2121
+
2122
+ cs = intel_ring_begin(rq, 4 * count);
17082123 if (IS_ERR(cs))
17092124 return PTR_ERR(cs);
17102125
1711
- *cs++ = MI_LOAD_REGISTER_IMM(ARRAY_SIZE(flex_mmio) + 1);
2126
+ offset = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET;
2127
+ do {
2128
+ *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
2129
+ *cs++ = offset + flex->offset * sizeof(u32);
2130
+ *cs++ = 0;
2131
+ *cs++ = flex->value;
2132
+ } while (flex++, --count);
17122133
1713
- *cs++ = i915_mmio_reg_offset(GEN8_OACTXCONTROL);
1714
- *cs++ = (dev_priv->perf.oa.period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
1715
- (dev_priv->perf.oa.periodic ? GEN8_OA_TIMER_ENABLE : 0) |
1716
- GEN8_OA_COUNTER_RESUME;
1717
-
1718
- for (i = 0; i < ARRAY_SIZE(flex_mmio); i++) {
1719
- u32 mmio = flex_mmio[i];
1720
-
1721
- /*
1722
- * This arbitrary default will select the 'EU FPU0 Pipeline
1723
- * Active' event. In the future it's anticipated that there
1724
- * will be an explicit 'No Event' we can select, but not
1725
- * yet...
1726
- */
1727
- u32 value = 0;
1728
-
1729
- if (oa_config) {
1730
- u32 j;
1731
-
1732
- for (j = 0; j < oa_config->flex_regs_len; j++) {
1733
- if (i915_mmio_reg_offset(oa_config->flex_regs[j].addr) == mmio) {
1734
- value = oa_config->flex_regs[j].value;
1735
- break;
1736
- }
1737
- }
1738
- }
1739
-
1740
- *cs++ = mmio;
1741
- *cs++ = value;
1742
- }
1743
-
1744
- *cs++ = MI_NOOP;
17452134 intel_ring_advance(rq, cs);
17462135
17472136 return 0;
17482137 }
17492138
1750
-static int gen8_switch_to_updated_kernel_context(struct drm_i915_private *dev_priv,
1751
- const struct i915_oa_config *oa_config)
2139
+static int
2140
+gen8_load_flex(struct i915_request *rq,
2141
+ struct intel_context *ce,
2142
+ const struct flex *flex, unsigned int count)
17522143 {
1753
- struct intel_engine_cs *engine = dev_priv->engine[RCS];
1754
- struct i915_timeline *timeline;
2144
+ u32 *cs;
2145
+
2146
+ GEM_BUG_ON(!count || count > 63);
2147
+
2148
+ cs = intel_ring_begin(rq, 2 * count + 2);
2149
+ if (IS_ERR(cs))
2150
+ return PTR_ERR(cs);
2151
+
2152
+ *cs++ = MI_LOAD_REGISTER_IMM(count);
2153
+ do {
2154
+ *cs++ = i915_mmio_reg_offset(flex->reg);
2155
+ *cs++ = flex->value;
2156
+ } while (flex++, --count);
2157
+ *cs++ = MI_NOOP;
2158
+
2159
+ intel_ring_advance(rq, cs);
2160
+
2161
+ return 0;
2162
+}
2163
+
2164
+static int gen8_modify_context(struct intel_context *ce,
2165
+ const struct flex *flex, unsigned int count)
2166
+{
17552167 struct i915_request *rq;
1756
- int ret;
2168
+ int err;
17572169
1758
- lockdep_assert_held(&dev_priv->drm.struct_mutex);
1759
-
1760
- i915_retire_requests(dev_priv);
1761
-
1762
- rq = i915_request_alloc(engine, dev_priv->kernel_context);
2170
+ rq = intel_engine_create_kernel_request(ce->engine);
17632171 if (IS_ERR(rq))
17642172 return PTR_ERR(rq);
17652173
1766
- ret = gen8_emit_oa_config(rq, oa_config);
1767
- if (ret) {
1768
- i915_request_add(rq);
1769
- return ret;
1770
- }
1771
-
1772
- /* Queue this switch after all other activity */
1773
- list_for_each_entry(timeline, &dev_priv->gt.timelines, link) {
1774
- struct i915_request *prev;
1775
-
1776
- prev = i915_gem_active_raw(&timeline->last_request,
1777
- &dev_priv->drm.struct_mutex);
1778
- if (prev)
1779
- i915_request_await_dma_fence(rq, &prev->fence);
1780
- }
2174
+ /* Serialise with the remote context */
2175
+ err = intel_context_prepare_remote_request(ce, rq);
2176
+ if (err == 0)
2177
+ err = gen8_store_flex(rq, ce, flex, count);
17812178
17822179 i915_request_add(rq);
2180
+ return err;
2181
+}
17832182
1784
- return 0;
2183
+static int
2184
+gen8_modify_self(struct intel_context *ce,
2185
+ const struct flex *flex, unsigned int count,
2186
+ struct i915_active *active)
2187
+{
2188
+ struct i915_request *rq;
2189
+ int err;
2190
+
2191
+ intel_engine_pm_get(ce->engine);
2192
+ rq = i915_request_create(ce);
2193
+ intel_engine_pm_put(ce->engine);
2194
+ if (IS_ERR(rq))
2195
+ return PTR_ERR(rq);
2196
+
2197
+ if (!IS_ERR_OR_NULL(active)) {
2198
+ err = i915_active_add_request(active, rq);
2199
+ if (err)
2200
+ goto err_add_request;
2201
+ }
2202
+
2203
+ err = gen8_load_flex(rq, ce, flex, count);
2204
+ if (err)
2205
+ goto err_add_request;
2206
+
2207
+err_add_request:
2208
+ i915_request_add(rq);
2209
+ return err;
2210
+}
2211
+
2212
+static int gen8_configure_context(struct i915_gem_context *ctx,
2213
+ struct flex *flex, unsigned int count)
2214
+{
2215
+ struct i915_gem_engines_iter it;
2216
+ struct intel_context *ce;
2217
+ int err = 0;
2218
+
2219
+ for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
2220
+ GEM_BUG_ON(ce == ce->engine->kernel_context);
2221
+
2222
+ if (ce->engine->class != RENDER_CLASS)
2223
+ continue;
2224
+
2225
+ /* Otherwise OA settings will be set upon first use */
2226
+ if (!intel_context_pin_if_active(ce))
2227
+ continue;
2228
+
2229
+ flex->value = intel_sseu_make_rpcs(ce->engine->gt, &ce->sseu);
2230
+ err = gen8_modify_context(ce, flex, count);
2231
+
2232
+ intel_context_unpin(ce);
2233
+ if (err)
2234
+ break;
2235
+ }
2236
+ i915_gem_context_unlock_engines(ctx);
2237
+
2238
+ return err;
2239
+}
2240
+
2241
+static int gen12_configure_oar_context(struct i915_perf_stream *stream,
2242
+ struct i915_active *active)
2243
+{
2244
+ int err;
2245
+ struct intel_context *ce = stream->pinned_ctx;
2246
+ u32 format = stream->oa_buffer.format;
2247
+ struct flex regs_context[] = {
2248
+ {
2249
+ GEN8_OACTXCONTROL,
2250
+ stream->perf->ctx_oactxctrl_offset + 1,
2251
+ active ? GEN8_OA_COUNTER_RESUME : 0,
2252
+ },
2253
+ };
2254
+ /* Offsets in regs_lri are not used since this configuration is only
2255
+ * applied using LRI. Initialize the correct offsets for posterity.
2256
+ */
2257
+#define GEN12_OAR_OACONTROL_OFFSET 0x5B0
2258
+ struct flex regs_lri[] = {
2259
+ {
2260
+ GEN12_OAR_OACONTROL,
2261
+ GEN12_OAR_OACONTROL_OFFSET + 1,
2262
+ (format << GEN12_OAR_OACONTROL_COUNTER_FORMAT_SHIFT) |
2263
+ (active ? GEN12_OAR_OACONTROL_COUNTER_ENABLE : 0)
2264
+ },
2265
+ {
2266
+ RING_CONTEXT_CONTROL(ce->engine->mmio_base),
2267
+ CTX_CONTEXT_CONTROL,
2268
+ _MASKED_FIELD(GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE,
2269
+ active ?
2270
+ GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE :
2271
+ 0)
2272
+ },
2273
+ };
2274
+
2275
+ /* Modify the context image of pinned context with regs_context*/
2276
+ err = intel_context_lock_pinned(ce);
2277
+ if (err)
2278
+ return err;
2279
+
2280
+ err = gen8_modify_context(ce, regs_context, ARRAY_SIZE(regs_context));
2281
+ intel_context_unlock_pinned(ce);
2282
+ if (err)
2283
+ return err;
2284
+
2285
+ /* Apply regs_lri using LRI with pinned context */
2286
+ return gen8_modify_self(ce, regs_lri, ARRAY_SIZE(regs_lri), active);
17852287 }
17862288
17872289 /*
....@@ -1807,21 +2309,20 @@
18072309 * per-context OA state.
18082310 *
18092311 * Note: it's only the RCS/Render context that has any OA state.
2312
+ * Note: the first flex register passed must always be R_PWR_CLK_STATE
18102313 */
1811
-static int gen8_configure_all_contexts(struct drm_i915_private *dev_priv,
1812
- const struct i915_oa_config *oa_config)
2314
+static int
2315
+oa_configure_all_contexts(struct i915_perf_stream *stream,
2316
+ struct flex *regs,
2317
+ size_t num_regs,
2318
+ struct i915_active *active)
18132319 {
1814
- struct intel_engine_cs *engine = dev_priv->engine[RCS];
1815
- struct i915_gem_context *ctx;
1816
- int ret;
1817
- unsigned int wait_flags = I915_WAIT_LOCKED;
2320
+ struct drm_i915_private *i915 = stream->perf->i915;
2321
+ struct intel_engine_cs *engine;
2322
+ struct i915_gem_context *ctx, *cn;
2323
+ int err;
18182324
1819
- lockdep_assert_held(&dev_priv->drm.struct_mutex);
1820
-
1821
- /* Switch away from any user context. */
1822
- ret = gen8_switch_to_updated_kernel_context(dev_priv, oa_config);
1823
- if (ret)
1824
- goto out;
2325
+ lockdep_assert_held(&stream->perf->lock);
18252326
18262327 /*
18272328 * The OA register config is setup through the context image. This image
....@@ -1833,45 +2334,116 @@
18332334 * this might leave small interval of time where the OA unit is
18342335 * configured at an invalid sampling period.
18352336 *
1836
- * So far the best way to work around this issue seems to be draining
1837
- * the GPU from any submitted work.
2337
+ * Note that since we emit all requests from a single ring, there
2338
+ * is still an implicit global barrier here that may cause a high
2339
+ * priority context to wait for an otherwise independent low priority
2340
+ * context. Contexts idle at the time of reconfiguration are not
2341
+ * trapped behind the barrier.
18382342 */
1839
- ret = i915_gem_wait_for_idle(dev_priv,
1840
- wait_flags,
1841
- MAX_SCHEDULE_TIMEOUT);
1842
- if (ret)
1843
- goto out;
1844
-
1845
- /* Update all contexts now that we've stalled the submission. */
1846
- list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
1847
- struct intel_context *ce = to_intel_context(ctx, engine);
1848
- u32 *regs;
1849
-
1850
- /* OA settings will be set upon first use */
1851
- if (!ce->state)
2343
+ spin_lock(&i915->gem.contexts.lock);
2344
+ list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
2345
+ if (!kref_get_unless_zero(&ctx->ref))
18522346 continue;
18532347
1854
- regs = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
1855
- if (IS_ERR(regs)) {
1856
- ret = PTR_ERR(regs);
1857
- goto out;
2348
+ spin_unlock(&i915->gem.contexts.lock);
2349
+
2350
+ err = gen8_configure_context(ctx, regs, num_regs);
2351
+ if (err) {
2352
+ i915_gem_context_put(ctx);
2353
+ return err;
18582354 }
18592355
1860
- ce->state->obj->mm.dirty = true;
1861
- regs += LRC_STATE_PN * PAGE_SIZE / sizeof(*regs);
2356
+ spin_lock(&i915->gem.contexts.lock);
2357
+ list_safe_reset_next(ctx, cn, link);
2358
+ i915_gem_context_put(ctx);
2359
+ }
2360
+ spin_unlock(&i915->gem.contexts.lock);
18622361
1863
- gen8_update_reg_state_unlocked(ctx, regs, oa_config);
2362
+ /*
2363
+ * After updating all other contexts, we need to modify ourselves.
2364
+ * If we don't modify the kernel_context, we do not get events while
2365
+ * idle.
2366
+ */
2367
+ for_each_uabi_engine(engine, i915) {
2368
+ struct intel_context *ce = engine->kernel_context;
18642369
1865
- i915_gem_object_unpin_map(ce->state->obj);
2370
+ if (engine->class != RENDER_CLASS)
2371
+ continue;
2372
+
2373
+ regs[0].value = intel_sseu_make_rpcs(engine->gt, &ce->sseu);
2374
+
2375
+ err = gen8_modify_self(ce, regs, num_regs, active);
2376
+ if (err)
2377
+ return err;
18662378 }
18672379
1868
- out:
1869
- return ret;
2380
+ return 0;
18702381 }
18712382
1872
-static int gen8_enable_metric_set(struct drm_i915_private *dev_priv,
1873
- const struct i915_oa_config *oa_config)
2383
+static int
2384
+gen12_configure_all_contexts(struct i915_perf_stream *stream,
2385
+ const struct i915_oa_config *oa_config,
2386
+ struct i915_active *active)
18742387 {
2388
+ struct flex regs[] = {
2389
+ {
2390
+ GEN8_R_PWR_CLK_STATE,
2391
+ CTX_R_PWR_CLK_STATE,
2392
+ },
2393
+ };
2394
+
2395
+ return oa_configure_all_contexts(stream,
2396
+ regs, ARRAY_SIZE(regs),
2397
+ active);
2398
+}
2399
+
2400
+static int
2401
+lrc_configure_all_contexts(struct i915_perf_stream *stream,
2402
+ const struct i915_oa_config *oa_config,
2403
+ struct i915_active *active)
2404
+{
2405
+ /* The MMIO offsets for Flex EU registers aren't contiguous */
2406
+ const u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
2407
+#define ctx_flexeuN(N) (ctx_flexeu0 + 2 * (N) + 1)
2408
+ struct flex regs[] = {
2409
+ {
2410
+ GEN8_R_PWR_CLK_STATE,
2411
+ CTX_R_PWR_CLK_STATE,
2412
+ },
2413
+ {
2414
+ GEN8_OACTXCONTROL,
2415
+ stream->perf->ctx_oactxctrl_offset + 1,
2416
+ },
2417
+ { EU_PERF_CNTL0, ctx_flexeuN(0) },
2418
+ { EU_PERF_CNTL1, ctx_flexeuN(1) },
2419
+ { EU_PERF_CNTL2, ctx_flexeuN(2) },
2420
+ { EU_PERF_CNTL3, ctx_flexeuN(3) },
2421
+ { EU_PERF_CNTL4, ctx_flexeuN(4) },
2422
+ { EU_PERF_CNTL5, ctx_flexeuN(5) },
2423
+ { EU_PERF_CNTL6, ctx_flexeuN(6) },
2424
+ };
2425
+#undef ctx_flexeuN
2426
+ int i;
2427
+
2428
+ regs[1].value =
2429
+ (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
2430
+ (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
2431
+ GEN8_OA_COUNTER_RESUME;
2432
+
2433
+ for (i = 2; i < ARRAY_SIZE(regs); i++)
2434
+ regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
2435
+
2436
+ return oa_configure_all_contexts(stream,
2437
+ regs, ARRAY_SIZE(regs),
2438
+ active);
2439
+}
2440
+
2441
+static int
2442
+gen8_enable_metric_set(struct i915_perf_stream *stream,
2443
+ struct i915_active *active)
2444
+{
2445
+ struct intel_uncore *uncore = stream->uncore;
2446
+ struct i915_oa_config *oa_config = stream->oa_config;
18752447 int ret;
18762448
18772449 /*
....@@ -1897,10 +2469,10 @@
18972469 * be read back from automatically triggered reports, as part of the
18982470 * RPT_ID field.
18992471 */
1900
- if (IS_GEN(dev_priv, 9, 11)) {
1901
- I915_WRITE(GEN8_OA_DEBUG,
1902
- _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
1903
- GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
2472
+ if (IS_GEN_RANGE(stream->perf->i915, 9, 11)) {
2473
+ intel_uncore_write(uncore, GEN8_OA_DEBUG,
2474
+ _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2475
+ GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
19042476 }
19052477
19062478 /*
....@@ -1908,45 +2480,117 @@
19082480 * to make sure all slices/subslices are ON before writing to NOA
19092481 * registers.
19102482 */
1911
- ret = gen8_configure_all_contexts(dev_priv, oa_config);
2483
+ ret = lrc_configure_all_contexts(stream, oa_config, active);
19122484 if (ret)
19132485 return ret;
19142486
1915
- config_oa_regs(dev_priv, oa_config->mux_regs, oa_config->mux_regs_len);
1916
-
1917
- config_oa_regs(dev_priv, oa_config->b_counter_regs,
1918
- oa_config->b_counter_regs_len);
1919
-
1920
- return 0;
2487
+ return emit_oa_config(stream,
2488
+ stream->oa_config, oa_context(stream),
2489
+ active);
19212490 }
19222491
1923
-static void gen8_disable_metric_set(struct drm_i915_private *dev_priv)
2492
+static u32 oag_report_ctx_switches(const struct i915_perf_stream *stream)
19242493 {
1925
- /* Reset all contexts' slices/subslices configurations. */
1926
- gen8_configure_all_contexts(dev_priv, NULL);
1927
-
1928
- I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
1929
- ~GT_NOA_ENABLE));
2494
+ return _MASKED_FIELD(GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS,
2495
+ (stream->sample_flags & SAMPLE_OA_REPORT) ?
2496
+ 0 : GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS);
19302497 }
19312498
1932
-static void gen10_disable_metric_set(struct drm_i915_private *dev_priv)
2499
+static int
2500
+gen12_enable_metric_set(struct i915_perf_stream *stream,
2501
+ struct i915_active *active)
19332502 {
2503
+ struct intel_uncore *uncore = stream->uncore;
2504
+ struct i915_oa_config *oa_config = stream->oa_config;
2505
+ bool periodic = stream->periodic;
2506
+ u32 period_exponent = stream->period_exponent;
2507
+ int ret;
2508
+
2509
+ intel_uncore_write(uncore, GEN12_OAG_OA_DEBUG,
2510
+ /* Disable clk ratio reports, like previous Gens. */
2511
+ _MASKED_BIT_ENABLE(GEN12_OAG_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
2512
+ GEN12_OAG_OA_DEBUG_INCLUDE_CLK_RATIO) |
2513
+ /*
2514
+ * If the user didn't require OA reports, instruct
2515
+ * the hardware not to emit ctx switch reports.
2516
+ */
2517
+ oag_report_ctx_switches(stream));
2518
+
2519
+ intel_uncore_write(uncore, GEN12_OAG_OAGLBCTXCTRL, periodic ?
2520
+ (GEN12_OAG_OAGLBCTXCTRL_COUNTER_RESUME |
2521
+ GEN12_OAG_OAGLBCTXCTRL_TIMER_ENABLE |
2522
+ (period_exponent << GEN12_OAG_OAGLBCTXCTRL_TIMER_PERIOD_SHIFT))
2523
+ : 0);
2524
+
2525
+ /*
2526
+ * Update all contexts prior writing the mux configurations as we need
2527
+ * to make sure all slices/subslices are ON before writing to NOA
2528
+ * registers.
2529
+ */
2530
+ ret = gen12_configure_all_contexts(stream, oa_config, active);
2531
+ if (ret)
2532
+ return ret;
2533
+
2534
+ /*
2535
+ * For Gen12, performance counters are context
2536
+ * saved/restored. Only enable it for the context that
2537
+ * requested this.
2538
+ */
2539
+ if (stream->ctx) {
2540
+ ret = gen12_configure_oar_context(stream, active);
2541
+ if (ret)
2542
+ return ret;
2543
+ }
2544
+
2545
+ return emit_oa_config(stream,
2546
+ stream->oa_config, oa_context(stream),
2547
+ active);
2548
+}
2549
+
2550
+static void gen8_disable_metric_set(struct i915_perf_stream *stream)
2551
+{
2552
+ struct intel_uncore *uncore = stream->uncore;
2553
+
19342554 /* Reset all contexts' slices/subslices configurations. */
1935
- gen8_configure_all_contexts(dev_priv, NULL);
2555
+ lrc_configure_all_contexts(stream, NULL, NULL);
2556
+
2557
+ intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
2558
+}
2559
+
2560
+static void gen10_disable_metric_set(struct i915_perf_stream *stream)
2561
+{
2562
+ struct intel_uncore *uncore = stream->uncore;
2563
+
2564
+ /* Reset all contexts' slices/subslices configurations. */
2565
+ lrc_configure_all_contexts(stream, NULL, NULL);
19362566
19372567 /* Make sure we disable noa to save power. */
1938
- I915_WRITE(RPM_CONFIG1,
1939
- I915_READ(RPM_CONFIG1) & ~GEN10_GT_NOA_ENABLE);
2568
+ intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
19402569 }
19412570
1942
-static void gen7_oa_enable(struct drm_i915_private *dev_priv)
2571
+static void gen12_disable_metric_set(struct i915_perf_stream *stream)
19432572 {
1944
- struct i915_gem_context *ctx =
1945
- dev_priv->perf.oa.exclusive_stream->ctx;
1946
- u32 ctx_id = dev_priv->perf.oa.specific_ctx_id;
1947
- bool periodic = dev_priv->perf.oa.periodic;
1948
- u32 period_exponent = dev_priv->perf.oa.period_exponent;
1949
- u32 report_format = dev_priv->perf.oa.oa_buffer.format;
2573
+ struct intel_uncore *uncore = stream->uncore;
2574
+
2575
+ /* Reset all contexts' slices/subslices configurations. */
2576
+ gen12_configure_all_contexts(stream, NULL, NULL);
2577
+
2578
+ /* disable the context save/restore or OAR counters */
2579
+ if (stream->ctx)
2580
+ gen12_configure_oar_context(stream, NULL);
2581
+
2582
+ /* Make sure we disable noa to save power. */
2583
+ intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
2584
+}
2585
+
2586
+static void gen7_oa_enable(struct i915_perf_stream *stream)
2587
+{
2588
+ struct intel_uncore *uncore = stream->uncore;
2589
+ struct i915_gem_context *ctx = stream->ctx;
2590
+ u32 ctx_id = stream->specific_ctx_id;
2591
+ bool periodic = stream->periodic;
2592
+ u32 period_exponent = stream->period_exponent;
2593
+ u32 report_format = stream->oa_buffer.format;
19502594
19512595 /*
19522596 * Reset buf pointers so we don't forward reports from before now.
....@@ -1957,21 +2601,22 @@
19572601 * on the assumption that certain fields are written to zeroed
19582602 * memory which this helps maintains.
19592603 */
1960
- gen7_init_oa_buffer(dev_priv);
2604
+ gen7_init_oa_buffer(stream);
19612605
1962
- I915_WRITE(GEN7_OACONTROL,
1963
- (ctx_id & GEN7_OACONTROL_CTX_MASK) |
1964
- (period_exponent <<
1965
- GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
1966
- (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
1967
- (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
1968
- (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
1969
- GEN7_OACONTROL_ENABLE);
2606
+ intel_uncore_write(uncore, GEN7_OACONTROL,
2607
+ (ctx_id & GEN7_OACONTROL_CTX_MASK) |
2608
+ (period_exponent <<
2609
+ GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
2610
+ (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
2611
+ (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
2612
+ (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
2613
+ GEN7_OACONTROL_ENABLE);
19702614 }
19712615
1972
-static void gen8_oa_enable(struct drm_i915_private *dev_priv)
2616
+static void gen8_oa_enable(struct i915_perf_stream *stream)
19732617 {
1974
- u32 report_format = dev_priv->perf.oa.oa_buffer.format;
2618
+ struct intel_uncore *uncore = stream->uncore;
2619
+ u32 report_format = stream->oa_buffer.format;
19752620
19762621 /*
19772622 * Reset buf pointers so we don't forward reports from before now.
....@@ -1982,16 +2627,35 @@
19822627 * on the assumption that certain fields are written to zeroed
19832628 * memory which this helps maintains.
19842629 */
1985
- gen8_init_oa_buffer(dev_priv);
2630
+ gen8_init_oa_buffer(stream);
19862631
19872632 /*
19882633 * Note: we don't rely on the hardware to perform single context
19892634 * filtering and instead filter on the cpu based on the context-id
19902635 * field of reports
19912636 */
1992
- I915_WRITE(GEN8_OACONTROL, (report_format <<
1993
- GEN8_OA_REPORT_FORMAT_SHIFT) |
1994
- GEN8_OA_COUNTER_ENABLE);
2637
+ intel_uncore_write(uncore, GEN8_OACONTROL,
2638
+ (report_format << GEN8_OA_REPORT_FORMAT_SHIFT) |
2639
+ GEN8_OA_COUNTER_ENABLE);
2640
+}
2641
+
2642
+static void gen12_oa_enable(struct i915_perf_stream *stream)
2643
+{
2644
+ struct intel_uncore *uncore = stream->uncore;
2645
+ u32 report_format = stream->oa_buffer.format;
2646
+
2647
+ /*
2648
+ * If we don't want OA reports from the OA buffer, then we don't even
2649
+ * need to program the OAG unit.
2650
+ */
2651
+ if (!(stream->sample_flags & SAMPLE_OA_REPORT))
2652
+ return;
2653
+
2654
+ gen12_init_oa_buffer(stream);
2655
+
2656
+ intel_uncore_write(uncore, GEN12_OAG_OACONTROL,
2657
+ (report_format << GEN12_OAG_OACONTROL_OA_COUNTER_FORMAT_SHIFT) |
2658
+ GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE);
19952659 }
19962660
19972661 /**
....@@ -2005,32 +2669,59 @@
20052669 */
20062670 static void i915_oa_stream_enable(struct i915_perf_stream *stream)
20072671 {
2008
- struct drm_i915_private *dev_priv = stream->dev_priv;
2672
+ stream->pollin = false;
20092673
2010
- dev_priv->perf.oa.ops.oa_enable(dev_priv);
2674
+ stream->perf->ops.oa_enable(stream);
20112675
2012
- if (dev_priv->perf.oa.periodic)
2013
- hrtimer_start(&dev_priv->perf.oa.poll_check_timer,
2014
- ns_to_ktime(POLL_PERIOD),
2676
+ if (stream->sample_flags & SAMPLE_OA_REPORT)
2677
+ hrtimer_start(&stream->poll_check_timer,
2678
+ ns_to_ktime(stream->poll_oa_period),
20152679 HRTIMER_MODE_REL_PINNED);
20162680 }
20172681
2018
-static void gen7_oa_disable(struct drm_i915_private *dev_priv)
2682
+static void gen7_oa_disable(struct i915_perf_stream *stream)
20192683 {
2020
- I915_WRITE(GEN7_OACONTROL, 0);
2021
- if (intel_wait_for_register(dev_priv,
2684
+ struct intel_uncore *uncore = stream->uncore;
2685
+
2686
+ intel_uncore_write(uncore, GEN7_OACONTROL, 0);
2687
+ if (intel_wait_for_register(uncore,
20222688 GEN7_OACONTROL, GEN7_OACONTROL_ENABLE, 0,
20232689 50))
2024
- DRM_ERROR("wait for OA to be disabled timed out\n");
2690
+ drm_err(&stream->perf->i915->drm,
2691
+ "wait for OA to be disabled timed out\n");
20252692 }
20262693
2027
-static void gen8_oa_disable(struct drm_i915_private *dev_priv)
2694
+static void gen8_oa_disable(struct i915_perf_stream *stream)
20282695 {
2029
- I915_WRITE(GEN8_OACONTROL, 0);
2030
- if (intel_wait_for_register(dev_priv,
2696
+ struct intel_uncore *uncore = stream->uncore;
2697
+
2698
+ intel_uncore_write(uncore, GEN8_OACONTROL, 0);
2699
+ if (intel_wait_for_register(uncore,
20312700 GEN8_OACONTROL, GEN8_OA_COUNTER_ENABLE, 0,
20322701 50))
2033
- DRM_ERROR("wait for OA to be disabled timed out\n");
2702
+ drm_err(&stream->perf->i915->drm,
2703
+ "wait for OA to be disabled timed out\n");
2704
+}
2705
+
2706
+static void gen12_oa_disable(struct i915_perf_stream *stream)
2707
+{
2708
+ struct intel_uncore *uncore = stream->uncore;
2709
+
2710
+ intel_uncore_write(uncore, GEN12_OAG_OACONTROL, 0);
2711
+ if (intel_wait_for_register(uncore,
2712
+ GEN12_OAG_OACONTROL,
2713
+ GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE, 0,
2714
+ 50))
2715
+ drm_err(&stream->perf->i915->drm,
2716
+ "wait for OA to be disabled timed out\n");
2717
+
2718
+ intel_uncore_write(uncore, GEN12_OA_TLB_INV_CR, 1);
2719
+ if (intel_wait_for_register(uncore,
2720
+ GEN12_OA_TLB_INV_CR,
2721
+ 1, 0,
2722
+ 50))
2723
+ drm_err(&stream->perf->i915->drm,
2724
+ "wait for OA tlb invalidate timed out\n");
20342725 }
20352726
20362727 /**
....@@ -2043,12 +2734,10 @@
20432734 */
20442735 static void i915_oa_stream_disable(struct i915_perf_stream *stream)
20452736 {
2046
- struct drm_i915_private *dev_priv = stream->dev_priv;
2737
+ stream->perf->ops.oa_disable(stream);
20472738
2048
- dev_priv->perf.oa.ops.oa_disable(dev_priv);
2049
-
2050
- if (dev_priv->perf.oa.periodic)
2051
- hrtimer_cancel(&dev_priv->perf.oa.poll_check_timer);
2739
+ if (stream->sample_flags & SAMPLE_OA_REPORT)
2740
+ hrtimer_cancel(&stream->poll_check_timer);
20522741 }
20532742
20542743 static const struct i915_perf_stream_ops i915_oa_stream_ops = {
....@@ -2059,6 +2748,55 @@
20592748 .poll_wait = i915_oa_poll_wait,
20602749 .read = i915_oa_read,
20612750 };
2751
+
2752
+static int i915_perf_stream_enable_sync(struct i915_perf_stream *stream)
2753
+{
2754
+ struct i915_active *active;
2755
+ int err;
2756
+
2757
+ active = i915_active_create();
2758
+ if (!active)
2759
+ return -ENOMEM;
2760
+
2761
+ err = stream->perf->ops.enable_metric_set(stream, active);
2762
+ if (err == 0)
2763
+ __i915_active_wait(active, TASK_UNINTERRUPTIBLE);
2764
+
2765
+ i915_active_put(active);
2766
+ return err;
2767
+}
2768
+
2769
+static void
2770
+get_default_sseu_config(struct intel_sseu *out_sseu,
2771
+ struct intel_engine_cs *engine)
2772
+{
2773
+ const struct sseu_dev_info *devinfo_sseu = &engine->gt->info.sseu;
2774
+
2775
+ *out_sseu = intel_sseu_from_device_info(devinfo_sseu);
2776
+
2777
+ if (IS_GEN(engine->i915, 11)) {
2778
+ /*
2779
+ * We only need subslice count so it doesn't matter which ones
2780
+ * we select - just turn off low bits in the amount of half of
2781
+ * all available subslices per slice.
2782
+ */
2783
+ out_sseu->subslice_mask =
2784
+ ~(~0 << (hweight8(out_sseu->subslice_mask) / 2));
2785
+ out_sseu->slice_mask = 0x1;
2786
+ }
2787
+}
2788
+
2789
+static int
2790
+get_sseu_config(struct intel_sseu *out_sseu,
2791
+ struct intel_engine_cs *engine,
2792
+ const struct drm_i915_gem_context_param_sseu *drm_sseu)
2793
+{
2794
+ if (drm_sseu->engine.engine_class != engine->uabi_class ||
2795
+ drm_sseu->engine.engine_instance != engine->uabi_instance)
2796
+ return -EINVAL;
2797
+
2798
+ return i915_gem_user_to_context_sseu(engine->gt, drm_sseu, out_sseu);
2799
+}
20622800
20632801 /**
20642802 * i915_oa_stream_init - validate combined props for OA stream and init
....@@ -2082,34 +2820,43 @@
20822820 struct drm_i915_perf_open_param *param,
20832821 struct perf_open_properties *props)
20842822 {
2085
- struct drm_i915_private *dev_priv = stream->dev_priv;
2823
+ struct drm_i915_private *i915 = stream->perf->i915;
2824
+ struct i915_perf *perf = stream->perf;
20862825 int format_size;
20872826 int ret;
20882827
2089
- /* If the sysfs metrics/ directory wasn't registered for some
2828
+ if (!props->engine) {
2829
+ DRM_DEBUG("OA engine not specified\n");
2830
+ return -EINVAL;
2831
+ }
2832
+
2833
+ /*
2834
+ * If the sysfs metrics/ directory wasn't registered for some
20902835 * reason then don't let userspace try their luck with config
20912836 * IDs
20922837 */
2093
- if (!dev_priv->perf.metrics_kobj) {
2838
+ if (!perf->metrics_kobj) {
20942839 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
20952840 return -EINVAL;
20962841 }
20972842
2098
- if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
2843
+ if (!(props->sample_flags & SAMPLE_OA_REPORT) &&
2844
+ (INTEL_GEN(perf->i915) < 12 || !stream->ctx)) {
20992845 DRM_DEBUG("Only OA report sampling supported\n");
21002846 return -EINVAL;
21012847 }
21022848
2103
- if (!dev_priv->perf.oa.ops.init_oa_buffer) {
2849
+ if (!perf->ops.enable_metric_set) {
21042850 DRM_DEBUG("OA unit not supported\n");
21052851 return -ENODEV;
21062852 }
21072853
2108
- /* To avoid the complexity of having to accurately filter
2854
+ /*
2855
+ * To avoid the complexity of having to accurately filter
21092856 * counter reports and marshal to the appropriate client
21102857 * we currently only allow exclusive access
21112858 */
2112
- if (dev_priv->perf.oa.exclusive_stream) {
2859
+ if (perf->exclusive_stream) {
21132860 DRM_DEBUG("OA unit already in use\n");
21142861 return -EBUSY;
21152862 }
....@@ -2119,43 +2866,28 @@
21192866 return -EINVAL;
21202867 }
21212868
2122
- /* We set up some ratelimit state to potentially throttle any _NOTES
2123
- * about spurious, invalid OA reports which we don't forward to
2124
- * userspace.
2125
- *
2126
- * The initialization is associated with opening the stream (not driver
2127
- * init) considering we print a _NOTE about any throttling when closing
2128
- * the stream instead of waiting until driver _fini which no one would
2129
- * ever see.
2130
- *
2131
- * Using the same limiting factors as printk_ratelimit()
2132
- */
2133
- ratelimit_state_init(&dev_priv->perf.oa.spurious_report_rs,
2134
- 5 * HZ, 10);
2135
- /* Since we use a DRM_NOTE for spurious reports it would be
2136
- * inconsistent to let __ratelimit() automatically print a warning for
2137
- * throttling.
2138
- */
2139
- ratelimit_set_flags(&dev_priv->perf.oa.spurious_report_rs,
2140
- RATELIMIT_MSG_ON_RELEASE);
2869
+ stream->engine = props->engine;
2870
+ stream->uncore = stream->engine->gt->uncore;
21412871
21422872 stream->sample_size = sizeof(struct drm_i915_perf_record_header);
21432873
2144
- format_size = dev_priv->perf.oa.oa_formats[props->oa_format].size;
2874
+ format_size = perf->oa_formats[props->oa_format].size;
21452875
2146
- stream->sample_flags |= SAMPLE_OA_REPORT;
2876
+ stream->sample_flags = props->sample_flags;
21472877 stream->sample_size += format_size;
21482878
2149
- dev_priv->perf.oa.oa_buffer.format_size = format_size;
2150
- if (WARN_ON(dev_priv->perf.oa.oa_buffer.format_size == 0))
2879
+ stream->oa_buffer.format_size = format_size;
2880
+ if (drm_WARN_ON(&i915->drm, stream->oa_buffer.format_size == 0))
21512881 return -EINVAL;
21522882
2153
- dev_priv->perf.oa.oa_buffer.format =
2154
- dev_priv->perf.oa.oa_formats[props->oa_format].format;
2883
+ stream->hold_preemption = props->hold_preemption;
21552884
2156
- dev_priv->perf.oa.periodic = props->oa_periodic;
2157
- if (dev_priv->perf.oa.periodic)
2158
- dev_priv->perf.oa.period_exponent = props->oa_period_exponent;
2885
+ stream->oa_buffer.format =
2886
+ perf->oa_formats[props->oa_format].format;
2887
+
2888
+ stream->periodic = props->oa_periodic;
2889
+ if (stream->periodic)
2890
+ stream->period_exponent = props->oa_period_exponent;
21592891
21602892 if (stream->ctx) {
21612893 ret = oa_get_render_ctx_id(stream);
....@@ -2165,9 +2897,16 @@
21652897 }
21662898 }
21672899
2168
- ret = get_oa_config(dev_priv, props->metrics_set, &stream->oa_config);
2900
+ ret = alloc_noa_wait(stream);
21692901 if (ret) {
2902
+ DRM_DEBUG("Unable to allocate NOA wait batch buffer\n");
2903
+ goto err_noa_wait_alloc;
2904
+ }
2905
+
2906
+ stream->oa_config = i915_perf_get_oa_config(perf, props->metrics_set);
2907
+ if (!stream->oa_config) {
21702908 DRM_DEBUG("Invalid OA config id=%i\n", props->metrics_set);
2909
+ ret = -EINVAL;
21712910 goto err_config;
21722911 }
21732912
....@@ -2183,107 +2922,69 @@
21832922 * In our case we are expecting that taking pm + FORCEWAKE
21842923 * references will effectively disable RC6.
21852924 */
2186
- intel_runtime_pm_get(dev_priv);
2187
- intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2925
+ intel_engine_pm_get(stream->engine);
2926
+ intel_uncore_forcewake_get(stream->uncore, FORCEWAKE_ALL);
21882927
2189
- ret = alloc_oa_buffer(dev_priv);
2928
+ ret = alloc_oa_buffer(stream);
21902929 if (ret)
21912930 goto err_oa_buf_alloc;
21922931
2193
- ret = i915_mutex_lock_interruptible(&dev_priv->drm);
2194
- if (ret)
2195
- goto err_lock;
2932
+ stream->ops = &i915_oa_stream_ops;
21962933
2197
- ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv,
2198
- stream->oa_config);
2934
+ perf->sseu = props->sseu;
2935
+ WRITE_ONCE(perf->exclusive_stream, stream);
2936
+
2937
+ ret = i915_perf_stream_enable_sync(stream);
21992938 if (ret) {
22002939 DRM_DEBUG("Unable to enable metric set\n");
22012940 goto err_enable;
22022941 }
22032942
2204
- stream->ops = &i915_oa_stream_ops;
2943
+ DRM_DEBUG("opening stream oa config uuid=%s\n",
2944
+ stream->oa_config->uuid);
22052945
2206
- dev_priv->perf.oa.exclusive_stream = stream;
2207
-
2208
- mutex_unlock(&dev_priv->drm.struct_mutex);
2946
+ hrtimer_init(&stream->poll_check_timer,
2947
+ CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2948
+ stream->poll_check_timer.function = oa_poll_check_timer_cb;
2949
+ init_waitqueue_head(&stream->poll_wq);
2950
+ spin_lock_init(&stream->oa_buffer.ptr_lock);
22092951
22102952 return 0;
22112953
22122954 err_enable:
2213
- dev_priv->perf.oa.ops.disable_metric_set(dev_priv);
2214
- mutex_unlock(&dev_priv->drm.struct_mutex);
2955
+ WRITE_ONCE(perf->exclusive_stream, NULL);
2956
+ perf->ops.disable_metric_set(stream);
22152957
2216
-err_lock:
2217
- free_oa_buffer(dev_priv);
2958
+ free_oa_buffer(stream);
22182959
22192960 err_oa_buf_alloc:
2220
- put_oa_config(dev_priv, stream->oa_config);
2961
+ free_oa_configs(stream);
22212962
2222
- intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
2223
- intel_runtime_pm_put(dev_priv);
2963
+ intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
2964
+ intel_engine_pm_put(stream->engine);
22242965
22252966 err_config:
2967
+ free_noa_wait(stream);
2968
+
2969
+err_noa_wait_alloc:
22262970 if (stream->ctx)
22272971 oa_put_render_ctx_id(stream);
22282972
22292973 return ret;
22302974 }
22312975
2232
-void i915_oa_init_reg_state(struct intel_engine_cs *engine,
2233
- struct i915_gem_context *ctx,
2234
- u32 *reg_state)
2976
+void i915_oa_init_reg_state(const struct intel_context *ce,
2977
+ const struct intel_engine_cs *engine)
22352978 {
22362979 struct i915_perf_stream *stream;
22372980
2238
- if (engine->id != RCS)
2981
+ if (engine->class != RENDER_CLASS)
22392982 return;
22402983
2241
- stream = engine->i915->perf.oa.exclusive_stream;
2242
- if (stream)
2243
- gen8_update_reg_state_unlocked(ctx, reg_state, stream->oa_config);
2244
-}
2245
-
2246
-/**
2247
- * i915_perf_read_locked - &i915_perf_stream_ops->read with error normalisation
2248
- * @stream: An i915 perf stream
2249
- * @file: An i915 perf stream file
2250
- * @buf: destination buffer given by userspace
2251
- * @count: the number of bytes userspace wants to read
2252
- * @ppos: (inout) file seek position (unused)
2253
- *
2254
- * Besides wrapping &i915_perf_stream_ops->read this provides a common place to
2255
- * ensure that if we've successfully copied any data then reporting that takes
2256
- * precedence over any internal error status, so the data isn't lost.
2257
- *
2258
- * For example ret will be -ENOSPC whenever there is more buffered data than
2259
- * can be copied to userspace, but that's only interesting if we weren't able
2260
- * to copy some data because it implies the userspace buffer is too small to
2261
- * receive a single record (and we never split records).
2262
- *
2263
- * Another case with ret == -EFAULT is more of a grey area since it would seem
2264
- * like bad form for userspace to ask us to overrun its buffer, but the user
2265
- * knows best:
2266
- *
2267
- * http://yarchive.net/comp/linux/partial_reads_writes.html
2268
- *
2269
- * Returns: The number of bytes copied or a negative error code on failure.
2270
- */
2271
-static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
2272
- struct file *file,
2273
- char __user *buf,
2274
- size_t count,
2275
- loff_t *ppos)
2276
-{
2277
- /* Note we keep the offset (aka bytes read) separate from any
2278
- * error status so that the final check for whether we return
2279
- * the bytes read with a higher precedence than any error (see
2280
- * comment below) doesn't need to be handled/duplicated in
2281
- * stream->ops->read() implementations.
2282
- */
2283
- size_t offset = 0;
2284
- int ret = stream->ops->read(stream, buf, count, &offset);
2285
-
2286
- return offset ?: (ret ?: -EAGAIN);
2984
+ /* perf.exclusive_stream serialised by lrc_configure_all_contexts() */
2985
+ stream = READ_ONCE(engine->i915->perf.exclusive_stream);
2986
+ if (stream && INTEL_GEN(stream->perf->i915) < 12)
2987
+ gen8_update_reg_state_unlocked(ce, stream);
22872988 }
22882989
22892990 /**
....@@ -2310,14 +3011,15 @@
23103011 loff_t *ppos)
23113012 {
23123013 struct i915_perf_stream *stream = file->private_data;
2313
- struct drm_i915_private *dev_priv = stream->dev_priv;
2314
- ssize_t ret;
3014
+ struct i915_perf *perf = stream->perf;
3015
+ size_t offset = 0;
3016
+ int ret;
23153017
23163018 /* To ensure it's handled consistently we simply treat all reads of a
23173019 * disabled stream as an error. In particular it might otherwise lead
23183020 * to a deadlock for blocking file descriptors...
23193021 */
2320
- if (!stream->enabled)
3022
+ if (!stream->enabled || !(stream->sample_flags & SAMPLE_OA_REPORT))
23213023 return -EIO;
23223024
23233025 if (!(file->f_flags & O_NONBLOCK)) {
....@@ -2333,15 +3035,14 @@
23333035 if (ret)
23343036 return ret;
23353037
2336
- mutex_lock(&dev_priv->perf.lock);
2337
- ret = i915_perf_read_locked(stream, file,
2338
- buf, count, ppos);
2339
- mutex_unlock(&dev_priv->perf.lock);
2340
- } while (ret == -EAGAIN);
3038
+ mutex_lock(&perf->lock);
3039
+ ret = stream->ops->read(stream, buf, count, &offset);
3040
+ mutex_unlock(&perf->lock);
3041
+ } while (!offset && !ret);
23413042 } else {
2342
- mutex_lock(&dev_priv->perf.lock);
2343
- ret = i915_perf_read_locked(stream, file, buf, count, ppos);
2344
- mutex_unlock(&dev_priv->perf.lock);
3043
+ mutex_lock(&perf->lock);
3044
+ ret = stream->ops->read(stream, buf, count, &offset);
3045
+ mutex_unlock(&perf->lock);
23453046 }
23463047
23473048 /* We allow the poll checking to sometimes report false positive EPOLLIN
....@@ -2351,36 +3052,35 @@
23513052 * and read() returning -EAGAIN. Clearing the oa.pollin state here
23523053 * effectively ensures we back off until the next hrtimer callback
23533054 * before reporting another EPOLLIN event.
3055
+ * The exception to this is if ops->read() returned -ENOSPC which means
3056
+ * that more OA data is available than could fit in the user provided
3057
+ * buffer. In this case we want the next poll() call to not block.
23543058 */
2355
- if (ret >= 0 || ret == -EAGAIN) {
2356
- /* Maybe make ->pollin per-stream state if we support multiple
2357
- * concurrent streams in the future.
2358
- */
2359
- dev_priv->perf.oa.pollin = false;
2360
- }
3059
+ if (ret != -ENOSPC)
3060
+ stream->pollin = false;
23613061
2362
- return ret;
3062
+ /* Possible values for ret are 0, -EFAULT, -ENOSPC, -EIO, ... */
3063
+ return offset ?: (ret ?: -EAGAIN);
23633064 }
23643065
23653066 static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
23663067 {
2367
- struct drm_i915_private *dev_priv =
2368
- container_of(hrtimer, typeof(*dev_priv),
2369
- perf.oa.poll_check_timer);
3068
+ struct i915_perf_stream *stream =
3069
+ container_of(hrtimer, typeof(*stream), poll_check_timer);
23703070
2371
- if (oa_buffer_check_unlocked(dev_priv)) {
2372
- dev_priv->perf.oa.pollin = true;
2373
- wake_up(&dev_priv->perf.oa.poll_wq);
3071
+ if (oa_buffer_check_unlocked(stream)) {
3072
+ stream->pollin = true;
3073
+ wake_up(&stream->poll_wq);
23743074 }
23753075
2376
- hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
3076
+ hrtimer_forward_now(hrtimer,
3077
+ ns_to_ktime(stream->poll_oa_period));
23773078
23783079 return HRTIMER_RESTART;
23793080 }
23803081
23813082 /**
23823083 * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
2383
- * @dev_priv: i915 device instance
23843084 * @stream: An i915 perf stream
23853085 * @file: An i915 perf stream file
23863086 * @wait: poll() state table
....@@ -2389,15 +3089,14 @@
23893089 * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
23903090 * will be woken for new stream data.
23913091 *
2392
- * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
3092
+ * Note: The &perf->lock mutex has been taken to serialize
23933093 * with any non-file-operation driver hooks.
23943094 *
23953095 * Returns: any poll events that are ready without sleeping
23963096 */
2397
-static __poll_t i915_perf_poll_locked(struct drm_i915_private *dev_priv,
2398
- struct i915_perf_stream *stream,
2399
- struct file *file,
2400
- poll_table *wait)
3097
+static __poll_t i915_perf_poll_locked(struct i915_perf_stream *stream,
3098
+ struct file *file,
3099
+ poll_table *wait)
24013100 {
24023101 __poll_t events = 0;
24033102
....@@ -2409,7 +3108,7 @@
24093108 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
24103109 * samples to read.
24113110 */
2412
- if (dev_priv->perf.oa.pollin)
3111
+ if (stream->pollin)
24133112 events |= EPOLLIN;
24143113
24153114 return events;
....@@ -2431,12 +3130,12 @@
24313130 static __poll_t i915_perf_poll(struct file *file, poll_table *wait)
24323131 {
24333132 struct i915_perf_stream *stream = file->private_data;
2434
- struct drm_i915_private *dev_priv = stream->dev_priv;
3133
+ struct i915_perf *perf = stream->perf;
24353134 __poll_t ret;
24363135
2437
- mutex_lock(&dev_priv->perf.lock);
2438
- ret = i915_perf_poll_locked(dev_priv, stream, file, wait);
2439
- mutex_unlock(&dev_priv->perf.lock);
3136
+ mutex_lock(&perf->lock);
3137
+ ret = i915_perf_poll_locked(stream, file, wait);
3138
+ mutex_unlock(&perf->lock);
24403139
24413140 return ret;
24423141 }
....@@ -2461,6 +3160,9 @@
24613160
24623161 if (stream->ops->enable)
24633162 stream->ops->enable(stream);
3163
+
3164
+ if (stream->hold_preemption)
3165
+ intel_context_set_nopreempt(stream->pinned_ctx);
24643166 }
24653167
24663168 /**
....@@ -2485,8 +3187,45 @@
24853187 /* Allow stream->ops->disable() to refer to this */
24863188 stream->enabled = false;
24873189
3190
+ if (stream->hold_preemption)
3191
+ intel_context_clear_nopreempt(stream->pinned_ctx);
3192
+
24883193 if (stream->ops->disable)
24893194 stream->ops->disable(stream);
3195
+}
3196
+
3197
+static long i915_perf_config_locked(struct i915_perf_stream *stream,
3198
+ unsigned long metrics_set)
3199
+{
3200
+ struct i915_oa_config *config;
3201
+ long ret = stream->oa_config->id;
3202
+
3203
+ config = i915_perf_get_oa_config(stream->perf, metrics_set);
3204
+ if (!config)
3205
+ return -EINVAL;
3206
+
3207
+ if (config != stream->oa_config) {
3208
+ int err;
3209
+
3210
+ /*
3211
+ * If OA is bound to a specific context, emit the
3212
+ * reconfiguration inline from that context. The update
3213
+ * will then be ordered with respect to submission on that
3214
+ * context.
3215
+ *
3216
+ * When set globally, we use a low priority kernel context,
3217
+ * so it will effectively take effect when idle.
3218
+ */
3219
+ err = emit_oa_config(stream, config, oa_context(stream), NULL);
3220
+ if (!err)
3221
+ config = xchg(&stream->oa_config, config);
3222
+ else
3223
+ ret = err;
3224
+ }
3225
+
3226
+ i915_oa_config_put(config);
3227
+
3228
+ return ret;
24903229 }
24913230
24923231 /**
....@@ -2495,7 +3234,7 @@
24953234 * @cmd: the ioctl request
24963235 * @arg: the ioctl data
24973236 *
2498
- * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
3237
+ * Note: The &perf->lock mutex has been taken to serialize
24993238 * with any non-file-operation driver hooks.
25003239 *
25013240 * Returns: zero on success or a negative error code. Returns -EINVAL for
....@@ -2512,6 +3251,8 @@
25123251 case I915_PERF_IOCTL_DISABLE:
25133252 i915_perf_disable_locked(stream);
25143253 return 0;
3254
+ case I915_PERF_IOCTL_CONFIG:
3255
+ return i915_perf_config_locked(stream, arg);
25153256 }
25163257
25173258 return -EINVAL;
....@@ -2533,12 +3274,12 @@
25333274 unsigned long arg)
25343275 {
25353276 struct i915_perf_stream *stream = file->private_data;
2536
- struct drm_i915_private *dev_priv = stream->dev_priv;
3277
+ struct i915_perf *perf = stream->perf;
25373278 long ret;
25383279
2539
- mutex_lock(&dev_priv->perf.lock);
3280
+ mutex_lock(&perf->lock);
25403281 ret = i915_perf_ioctl_locked(stream, cmd, arg);
2541
- mutex_unlock(&dev_priv->perf.lock);
3282
+ mutex_unlock(&perf->lock);
25423283
25433284 return ret;
25443285 }
....@@ -2550,7 +3291,7 @@
25503291 * Frees all resources associated with the given i915 perf @stream, disabling
25513292 * any associated data capture in the process.
25523293 *
2553
- * Note: The &drm_i915_private->perf.lock mutex has been taken to serialize
3294
+ * Note: The &perf->lock mutex has been taken to serialize
25543295 * with any non-file-operation driver hooks.
25553296 */
25563297 static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
....@@ -2560,8 +3301,6 @@
25603301
25613302 if (stream->ops->destroy)
25623303 stream->ops->destroy(stream);
2563
-
2564
- list_del(&stream->link);
25653304
25663305 if (stream->ctx)
25673306 i915_gem_context_put(stream->ctx);
....@@ -2583,11 +3322,14 @@
25833322 static int i915_perf_release(struct inode *inode, struct file *file)
25843323 {
25853324 struct i915_perf_stream *stream = file->private_data;
2586
- struct drm_i915_private *dev_priv = stream->dev_priv;
3325
+ struct i915_perf *perf = stream->perf;
25873326
2588
- mutex_lock(&dev_priv->perf.lock);
3327
+ mutex_lock(&perf->lock);
25893328 i915_perf_destroy_locked(stream);
2590
- mutex_unlock(&dev_priv->perf.lock);
3329
+ mutex_unlock(&perf->lock);
3330
+
3331
+ /* Release the reference the perf stream kept on the driver. */
3332
+ drm_dev_put(&perf->i915->drm);
25913333
25923334 return 0;
25933335 }
....@@ -2609,7 +3351,7 @@
26093351
26103352 /**
26113353 * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
2612
- * @dev_priv: i915 device instance
3354
+ * @perf: i915 perf instance
26133355 * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
26143356 * @props: individually validated u64 property value pairs
26153357 * @file: drm file
....@@ -2617,7 +3359,7 @@
26173359 * See i915_perf_ioctl_open() for interface details.
26183360 *
26193361 * Implements further stream config validation and stream initialization on
2620
- * behalf of i915_perf_open_ioctl() with the &drm_i915_private->perf.lock mutex
3362
+ * behalf of i915_perf_open_ioctl() with the &perf->lock mutex
26213363 * taken to serialize with any non-file-operation driver hooks.
26223364 *
26233365 * Note: at this point the @props have only been validated in isolation and
....@@ -2632,7 +3374,7 @@
26323374 * Returns: zero on success or a negative error code.
26333375 */
26343376 static int
2635
-i915_perf_open_ioctl_locked(struct drm_i915_private *dev_priv,
3377
+i915_perf_open_ioctl_locked(struct i915_perf *perf,
26363378 struct drm_i915_perf_open_param *param,
26373379 struct perf_open_properties *props,
26383380 struct drm_file *file)
....@@ -2663,25 +3405,50 @@
26633405 * rest of the system, which we consider acceptable for a
26643406 * non-privileged client.
26653407 *
2666
- * For Gen8+ the OA unit no longer supports clock gating off for a
3408
+ * For Gen8->11 the OA unit no longer supports clock gating off for a
26673409 * specific context and the kernel can't securely stop the counters
26683410 * from updating as system-wide / global values. Even though we can
26693411 * filter reports based on the included context ID we can't block
26703412 * clients from seeing the raw / global counter values via
26713413 * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
26723414 * enable the OA unit by default.
3415
+ *
3416
+ * For Gen12+ we gain a new OAR unit that only monitors the RCS on a
3417
+ * per context basis. So we can relax requirements there if the user
3418
+ * doesn't request global stream access (i.e. query based sampling
3419
+ * using MI_RECORD_PERF_COUNT.
26733420 */
2674
- if (IS_HASWELL(dev_priv) && specific_ctx)
3421
+ if (IS_HASWELL(perf->i915) && specific_ctx)
26753422 privileged_op = false;
3423
+ else if (IS_GEN(perf->i915, 12) && specific_ctx &&
3424
+ (props->sample_flags & SAMPLE_OA_REPORT) == 0)
3425
+ privileged_op = false;
3426
+
3427
+ if (props->hold_preemption) {
3428
+ if (!props->single_context) {
3429
+ DRM_DEBUG("preemption disable with no context\n");
3430
+ ret = -EINVAL;
3431
+ goto err;
3432
+ }
3433
+ privileged_op = true;
3434
+ }
3435
+
3436
+ /*
3437
+ * Asking for SSEU configuration is a priviliged operation.
3438
+ */
3439
+ if (props->has_sseu)
3440
+ privileged_op = true;
3441
+ else
3442
+ get_default_sseu_config(&props->sseu, props->engine);
26763443
26773444 /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
26783445 * we check a dev.i915.perf_stream_paranoid sysctl option
26793446 * to determine if it's ok to access system wide OA counters
2680
- * without CAP_SYS_ADMIN privileges.
3447
+ * without CAP_PERFMON or CAP_SYS_ADMIN privileges.
26813448 */
26823449 if (privileged_op &&
2683
- i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
2684
- DRM_DEBUG("Insufficient privileges to open system-wide i915 perf stream\n");
3450
+ i915_perf_stream_paranoid && !perfmon_capable()) {
3451
+ DRM_DEBUG("Insufficient privileges to open i915 perf stream\n");
26853452 ret = -EACCES;
26863453 goto err_ctx;
26873454 }
....@@ -2692,8 +3459,9 @@
26923459 goto err_ctx;
26933460 }
26943461
2695
- stream->dev_priv = dev_priv;
3462
+ stream->perf = perf;
26963463 stream->ctx = specific_ctx;
3464
+ stream->poll_oa_period = props->poll_oa_period;
26973465
26983466 ret = i915_oa_stream_init(stream, param, props);
26993467 if (ret)
....@@ -2708,8 +3476,6 @@
27083476 goto err_flags;
27093477 }
27103478
2711
- list_add(&stream->link, &dev_priv->perf.streams);
2712
-
27133479 if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
27143480 f_flags |= O_CLOEXEC;
27153481 if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
....@@ -2718,16 +3484,19 @@
27183484 stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
27193485 if (stream_fd < 0) {
27203486 ret = stream_fd;
2721
- goto err_open;
3487
+ goto err_flags;
27223488 }
27233489
27243490 if (!(param->flags & I915_PERF_FLAG_DISABLED))
27253491 i915_perf_enable_locked(stream);
27263492
3493
+ /* Take a reference on the driver that will be kept with stream_fd
3494
+ * until its release.
3495
+ */
3496
+ drm_dev_get(&perf->i915->drm);
3497
+
27273498 return stream_fd;
27283499
2729
-err_open:
2730
- list_del(&stream->link);
27313500 err_flags:
27323501 if (stream->ops->destroy)
27333502 stream->ops->destroy(stream);
....@@ -2740,15 +3509,14 @@
27403509 return ret;
27413510 }
27423511
2743
-static u64 oa_exponent_to_ns(struct drm_i915_private *dev_priv, int exponent)
3512
+static u64 oa_exponent_to_ns(struct i915_perf *perf, int exponent)
27443513 {
2745
- return div64_u64(1000000000ULL * (2ULL << exponent),
2746
- 1000ULL * INTEL_INFO(dev_priv)->cs_timestamp_frequency_khz);
3514
+ return i915_cs_timestamp_ticks_to_ns(perf->i915, 2ULL << exponent);
27473515 }
27483516
27493517 /**
27503518 * read_properties_unlocked - validate + copy userspace stream open properties
2751
- * @dev_priv: i915 device instance
3519
+ * @perf: i915 perf instance
27523520 * @uprops: The array of u64 key value pairs given by userspace
27533521 * @n_props: The number of key value pairs expected in @uprops
27543522 * @props: The stream configuration built up while validating properties
....@@ -2761,18 +3529,29 @@
27613529 * we shouldn't validate or assume anything about ordering here. This doesn't
27623530 * rule out defining new properties with ordering requirements in the future.
27633531 */
2764
-static int read_properties_unlocked(struct drm_i915_private *dev_priv,
3532
+static int read_properties_unlocked(struct i915_perf *perf,
27653533 u64 __user *uprops,
27663534 u32 n_props,
27673535 struct perf_open_properties *props)
27683536 {
27693537 u64 __user *uprop = uprops;
27703538 u32 i;
3539
+ int ret;
27713540
27723541 memset(props, 0, sizeof(struct perf_open_properties));
3542
+ props->poll_oa_period = DEFAULT_POLL_PERIOD_NS;
27733543
27743544 if (!n_props) {
27753545 DRM_DEBUG("No i915 perf properties given\n");
3546
+ return -EINVAL;
3547
+ }
3548
+
3549
+ /* At the moment we only support using i915-perf on the RCS. */
3550
+ props->engine = intel_engine_lookup_user(perf->i915,
3551
+ I915_ENGINE_CLASS_RENDER,
3552
+ 0);
3553
+ if (!props->engine) {
3554
+ DRM_DEBUG("No RENDER-capable engines\n");
27763555 return -EINVAL;
27773556 }
27783557
....@@ -2790,7 +3569,6 @@
27903569 for (i = 0; i < n_props; i++) {
27913570 u64 oa_period, oa_freq_hz;
27923571 u64 id, value;
2793
- int ret;
27943572
27953573 ret = get_user(id, uprop);
27963574 if (ret)
....@@ -2827,7 +3605,7 @@
28273605 value);
28283606 return -EINVAL;
28293607 }
2830
- if (!dev_priv->perf.oa.oa_formats[value].size) {
3608
+ if (!perf->oa_formats[value].size) {
28313609 DRM_DEBUG("Unsupported OA report format %llu\n",
28323610 value);
28333611 return -EINVAL;
....@@ -2848,7 +3626,7 @@
28483626 */
28493627
28503628 BUILD_BUG_ON(sizeof(oa_period) != 8);
2851
- oa_period = oa_exponent_to_ns(dev_priv, value);
3629
+ oa_period = oa_exponent_to_ns(perf, value);
28523630
28533631 /* This check is primarily to ensure that oa_period <=
28543632 * UINT32_MAX (before passing to do_div which only
....@@ -2863,15 +3641,43 @@
28633641 } else
28643642 oa_freq_hz = 0;
28653643
2866
- if (oa_freq_hz > i915_oa_max_sample_rate &&
2867
- !capable(CAP_SYS_ADMIN)) {
2868
- DRM_DEBUG("OA exponent would exceed the max sampling frequency (sysctl dev.i915.oa_max_sample_rate) %uHz without root privileges\n",
3644
+ if (oa_freq_hz > i915_oa_max_sample_rate && !perfmon_capable()) {
3645
+ DRM_DEBUG("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",
28693646 i915_oa_max_sample_rate);
28703647 return -EACCES;
28713648 }
28723649
28733650 props->oa_periodic = true;
28743651 props->oa_period_exponent = value;
3652
+ break;
3653
+ case DRM_I915_PERF_PROP_HOLD_PREEMPTION:
3654
+ props->hold_preemption = !!value;
3655
+ break;
3656
+ case DRM_I915_PERF_PROP_GLOBAL_SSEU: {
3657
+ struct drm_i915_gem_context_param_sseu user_sseu;
3658
+
3659
+ if (copy_from_user(&user_sseu,
3660
+ u64_to_user_ptr(value),
3661
+ sizeof(user_sseu))) {
3662
+ DRM_DEBUG("Unable to copy global sseu parameter\n");
3663
+ return -EFAULT;
3664
+ }
3665
+
3666
+ ret = get_sseu_config(&props->sseu, props->engine, &user_sseu);
3667
+ if (ret) {
3668
+ DRM_DEBUG("Invalid SSEU configuration\n");
3669
+ return ret;
3670
+ }
3671
+ props->has_sseu = true;
3672
+ break;
3673
+ }
3674
+ case DRM_I915_PERF_PROP_POLL_OA_PERIOD:
3675
+ if (value < 100000 /* 100us */) {
3676
+ DRM_DEBUG("OA availability timer too small (%lluns < 100us)\n",
3677
+ value);
3678
+ return -EINVAL;
3679
+ }
3680
+ props->poll_oa_period = value;
28753681 break;
28763682 case DRM_I915_PERF_PROP_MAX:
28773683 MISSING_CASE(id);
....@@ -2899,10 +3705,10 @@
28993705 * buffered data written by the GPU besides periodic OA metrics.
29003706 *
29013707 * Note we copy the properties from userspace outside of the i915 perf
2902
- * mutex to avoid an awkward lockdep with mmap_sem.
3708
+ * mutex to avoid an awkward lockdep with mmap_lock.
29033709 *
29043710 * Most of the implementation details are handled by
2905
- * i915_perf_open_ioctl_locked() after taking the &drm_i915_private->perf.lock
3711
+ * i915_perf_open_ioctl_locked() after taking the &perf->lock
29063712 * mutex for serializing with any non-file-operation driver hooks.
29073713 *
29083714 * Return: A newly opened i915 Perf stream file descriptor or negative
....@@ -2911,13 +3717,13 @@
29113717 int i915_perf_open_ioctl(struct drm_device *dev, void *data,
29123718 struct drm_file *file)
29133719 {
2914
- struct drm_i915_private *dev_priv = dev->dev_private;
3720
+ struct i915_perf *perf = &to_i915(dev)->perf;
29153721 struct drm_i915_perf_open_param *param = data;
29163722 struct perf_open_properties props;
29173723 u32 known_open_flags;
29183724 int ret;
29193725
2920
- if (!dev_priv->perf.initialized) {
3726
+ if (!perf->i915) {
29213727 DRM_DEBUG("i915 perf interface not available for this system\n");
29223728 return -ENOTSUPP;
29233729 }
....@@ -2930,124 +3736,69 @@
29303736 return -EINVAL;
29313737 }
29323738
2933
- ret = read_properties_unlocked(dev_priv,
3739
+ ret = read_properties_unlocked(perf,
29343740 u64_to_user_ptr(param->properties_ptr),
29353741 param->num_properties,
29363742 &props);
29373743 if (ret)
29383744 return ret;
29393745
2940
- mutex_lock(&dev_priv->perf.lock);
2941
- ret = i915_perf_open_ioctl_locked(dev_priv, param, &props, file);
2942
- mutex_unlock(&dev_priv->perf.lock);
3746
+ mutex_lock(&perf->lock);
3747
+ ret = i915_perf_open_ioctl_locked(perf, param, &props, file);
3748
+ mutex_unlock(&perf->lock);
29433749
29443750 return ret;
29453751 }
29463752
29473753 /**
29483754 * i915_perf_register - exposes i915-perf to userspace
2949
- * @dev_priv: i915 device instance
3755
+ * @i915: i915 device instance
29503756 *
29513757 * In particular OA metric sets are advertised under a sysfs metrics/
29523758 * directory allowing userspace to enumerate valid IDs that can be
29533759 * used to open an i915-perf stream.
29543760 */
2955
-void i915_perf_register(struct drm_i915_private *dev_priv)
3761
+void i915_perf_register(struct drm_i915_private *i915)
29563762 {
2957
- int ret;
3763
+ struct i915_perf *perf = &i915->perf;
29583764
2959
- if (!dev_priv->perf.initialized)
3765
+ if (!perf->i915)
29603766 return;
29613767
29623768 /* To be sure we're synchronized with an attempted
29633769 * i915_perf_open_ioctl(); considering that we register after
29643770 * being exposed to userspace.
29653771 */
2966
- mutex_lock(&dev_priv->perf.lock);
3772
+ mutex_lock(&perf->lock);
29673773
2968
- dev_priv->perf.metrics_kobj =
3774
+ perf->metrics_kobj =
29693775 kobject_create_and_add("metrics",
2970
- &dev_priv->drm.primary->kdev->kobj);
2971
- if (!dev_priv->perf.metrics_kobj)
2972
- goto exit;
3776
+ &i915->drm.primary->kdev->kobj);
29733777
2974
- sysfs_attr_init(&dev_priv->perf.oa.test_config.sysfs_metric_id.attr);
2975
-
2976
- if (IS_HASWELL(dev_priv)) {
2977
- i915_perf_load_test_config_hsw(dev_priv);
2978
- } else if (IS_BROADWELL(dev_priv)) {
2979
- i915_perf_load_test_config_bdw(dev_priv);
2980
- } else if (IS_CHERRYVIEW(dev_priv)) {
2981
- i915_perf_load_test_config_chv(dev_priv);
2982
- } else if (IS_SKYLAKE(dev_priv)) {
2983
- if (IS_SKL_GT2(dev_priv))
2984
- i915_perf_load_test_config_sklgt2(dev_priv);
2985
- else if (IS_SKL_GT3(dev_priv))
2986
- i915_perf_load_test_config_sklgt3(dev_priv);
2987
- else if (IS_SKL_GT4(dev_priv))
2988
- i915_perf_load_test_config_sklgt4(dev_priv);
2989
- } else if (IS_BROXTON(dev_priv)) {
2990
- i915_perf_load_test_config_bxt(dev_priv);
2991
- } else if (IS_KABYLAKE(dev_priv)) {
2992
- if (IS_KBL_GT2(dev_priv))
2993
- i915_perf_load_test_config_kblgt2(dev_priv);
2994
- else if (IS_KBL_GT3(dev_priv))
2995
- i915_perf_load_test_config_kblgt3(dev_priv);
2996
- } else if (IS_GEMINILAKE(dev_priv)) {
2997
- i915_perf_load_test_config_glk(dev_priv);
2998
- } else if (IS_COFFEELAKE(dev_priv)) {
2999
- if (IS_CFL_GT2(dev_priv))
3000
- i915_perf_load_test_config_cflgt2(dev_priv);
3001
- if (IS_CFL_GT3(dev_priv))
3002
- i915_perf_load_test_config_cflgt3(dev_priv);
3003
- } else if (IS_CANNONLAKE(dev_priv)) {
3004
- i915_perf_load_test_config_cnl(dev_priv);
3005
- } else if (IS_ICELAKE(dev_priv)) {
3006
- i915_perf_load_test_config_icl(dev_priv);
3007
- }
3008
-
3009
- if (dev_priv->perf.oa.test_config.id == 0)
3010
- goto sysfs_error;
3011
-
3012
- ret = sysfs_create_group(dev_priv->perf.metrics_kobj,
3013
- &dev_priv->perf.oa.test_config.sysfs_metric);
3014
- if (ret)
3015
- goto sysfs_error;
3016
-
3017
- atomic_set(&dev_priv->perf.oa.test_config.ref_count, 1);
3018
-
3019
- goto exit;
3020
-
3021
-sysfs_error:
3022
- kobject_put(dev_priv->perf.metrics_kobj);
3023
- dev_priv->perf.metrics_kobj = NULL;
3024
-
3025
-exit:
3026
- mutex_unlock(&dev_priv->perf.lock);
3778
+ mutex_unlock(&perf->lock);
30273779 }
30283780
30293781 /**
30303782 * i915_perf_unregister - hide i915-perf from userspace
3031
- * @dev_priv: i915 device instance
3783
+ * @i915: i915 device instance
30323784 *
30333785 * i915-perf state cleanup is split up into an 'unregister' and
30343786 * 'deinit' phase where the interface is first hidden from
30353787 * userspace by i915_perf_unregister() before cleaning up
30363788 * remaining state in i915_perf_fini().
30373789 */
3038
-void i915_perf_unregister(struct drm_i915_private *dev_priv)
3790
+void i915_perf_unregister(struct drm_i915_private *i915)
30393791 {
3040
- if (!dev_priv->perf.metrics_kobj)
3792
+ struct i915_perf *perf = &i915->perf;
3793
+
3794
+ if (!perf->metrics_kobj)
30413795 return;
30423796
3043
- sysfs_remove_group(dev_priv->perf.metrics_kobj,
3044
- &dev_priv->perf.oa.test_config.sysfs_metric);
3045
-
3046
- kobject_put(dev_priv->perf.metrics_kobj);
3047
- dev_priv->perf.metrics_kobj = NULL;
3797
+ kobject_put(perf->metrics_kobj);
3798
+ perf->metrics_kobj = NULL;
30483799 }
30493800
3050
-static bool gen8_is_valid_flex_addr(struct drm_i915_private *dev_priv, u32 addr)
3801
+static bool gen8_is_valid_flex_addr(struct i915_perf *perf, u32 addr)
30513802 {
30523803 static const i915_reg_t flex_eu_regs[] = {
30533804 EU_PERF_CNTL0,
....@@ -3067,78 +3818,103 @@
30673818 return false;
30683819 }
30693820
3070
-static bool gen7_is_valid_b_counter_addr(struct drm_i915_private *dev_priv, u32 addr)
3821
+#define ADDR_IN_RANGE(addr, start, end) \
3822
+ ((addr) >= (start) && \
3823
+ (addr) <= (end))
3824
+
3825
+#define REG_IN_RANGE(addr, start, end) \
3826
+ ((addr) >= i915_mmio_reg_offset(start) && \
3827
+ (addr) <= i915_mmio_reg_offset(end))
3828
+
3829
+#define REG_EQUAL(addr, mmio) \
3830
+ ((addr) == i915_mmio_reg_offset(mmio))
3831
+
3832
+static bool gen7_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
30713833 {
3072
- return (addr >= i915_mmio_reg_offset(OASTARTTRIG1) &&
3073
- addr <= i915_mmio_reg_offset(OASTARTTRIG8)) ||
3074
- (addr >= i915_mmio_reg_offset(OAREPORTTRIG1) &&
3075
- addr <= i915_mmio_reg_offset(OAREPORTTRIG8)) ||
3076
- (addr >= i915_mmio_reg_offset(OACEC0_0) &&
3077
- addr <= i915_mmio_reg_offset(OACEC7_1));
3834
+ return REG_IN_RANGE(addr, OASTARTTRIG1, OASTARTTRIG8) ||
3835
+ REG_IN_RANGE(addr, OAREPORTTRIG1, OAREPORTTRIG8) ||
3836
+ REG_IN_RANGE(addr, OACEC0_0, OACEC7_1);
30783837 }
30793838
3080
-static bool gen7_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr)
3839
+static bool gen7_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
30813840 {
3082
- return addr == i915_mmio_reg_offset(HALF_SLICE_CHICKEN2) ||
3083
- (addr >= i915_mmio_reg_offset(MICRO_BP0_0) &&
3084
- addr <= i915_mmio_reg_offset(NOA_WRITE)) ||
3085
- (addr >= i915_mmio_reg_offset(OA_PERFCNT1_LO) &&
3086
- addr <= i915_mmio_reg_offset(OA_PERFCNT2_HI)) ||
3087
- (addr >= i915_mmio_reg_offset(OA_PERFMATRIX_LO) &&
3088
- addr <= i915_mmio_reg_offset(OA_PERFMATRIX_HI));
3841
+ return REG_EQUAL(addr, HALF_SLICE_CHICKEN2) ||
3842
+ REG_IN_RANGE(addr, MICRO_BP0_0, NOA_WRITE) ||
3843
+ REG_IN_RANGE(addr, OA_PERFCNT1_LO, OA_PERFCNT2_HI) ||
3844
+ REG_IN_RANGE(addr, OA_PERFMATRIX_LO, OA_PERFMATRIX_HI);
30893845 }
30903846
3091
-static bool gen8_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr)
3847
+static bool gen8_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
30923848 {
3093
- return gen7_is_valid_mux_addr(dev_priv, addr) ||
3094
- addr == i915_mmio_reg_offset(WAIT_FOR_RC6_EXIT) ||
3095
- (addr >= i915_mmio_reg_offset(RPM_CONFIG0) &&
3096
- addr <= i915_mmio_reg_offset(NOA_CONFIG(8)));
3849
+ return gen7_is_valid_mux_addr(perf, addr) ||
3850
+ REG_EQUAL(addr, WAIT_FOR_RC6_EXIT) ||
3851
+ REG_IN_RANGE(addr, RPM_CONFIG0, NOA_CONFIG(8));
30973852 }
30983853
3099
-static bool gen10_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr)
3854
+static bool gen10_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
31003855 {
3101
- return gen8_is_valid_mux_addr(dev_priv, addr) ||
3102
- (addr >= i915_mmio_reg_offset(OA_PERFCNT3_LO) &&
3103
- addr <= i915_mmio_reg_offset(OA_PERFCNT4_HI));
3856
+ return gen8_is_valid_mux_addr(perf, addr) ||
3857
+ REG_EQUAL(addr, GEN10_NOA_WRITE_HIGH) ||
3858
+ REG_IN_RANGE(addr, OA_PERFCNT3_LO, OA_PERFCNT4_HI);
31043859 }
31053860
3106
-static bool hsw_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr)
3861
+static bool hsw_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
31073862 {
3108
- return gen7_is_valid_mux_addr(dev_priv, addr) ||
3109
- (addr >= 0x25100 && addr <= 0x2FF90) ||
3110
- (addr >= i915_mmio_reg_offset(HSW_MBVID2_NOA0) &&
3111
- addr <= i915_mmio_reg_offset(HSW_MBVID2_NOA9)) ||
3112
- addr == i915_mmio_reg_offset(HSW_MBVID2_MISR0);
3863
+ return gen7_is_valid_mux_addr(perf, addr) ||
3864
+ ADDR_IN_RANGE(addr, 0x25100, 0x2FF90) ||
3865
+ REG_IN_RANGE(addr, HSW_MBVID2_NOA0, HSW_MBVID2_NOA9) ||
3866
+ REG_EQUAL(addr, HSW_MBVID2_MISR0);
31133867 }
31143868
3115
-static bool chv_is_valid_mux_addr(struct drm_i915_private *dev_priv, u32 addr)
3869
+static bool chv_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
31163870 {
3117
- return gen7_is_valid_mux_addr(dev_priv, addr) ||
3118
- (addr >= 0x182300 && addr <= 0x1823A4);
3871
+ return gen7_is_valid_mux_addr(perf, addr) ||
3872
+ ADDR_IN_RANGE(addr, 0x182300, 0x1823A4);
31193873 }
31203874
3121
-static uint32_t mask_reg_value(u32 reg, u32 val)
3875
+static bool gen12_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
3876
+{
3877
+ return REG_IN_RANGE(addr, GEN12_OAG_OASTARTTRIG1, GEN12_OAG_OASTARTTRIG8) ||
3878
+ REG_IN_RANGE(addr, GEN12_OAG_OAREPORTTRIG1, GEN12_OAG_OAREPORTTRIG8) ||
3879
+ REG_IN_RANGE(addr, GEN12_OAG_CEC0_0, GEN12_OAG_CEC7_1) ||
3880
+ REG_IN_RANGE(addr, GEN12_OAG_SCEC0_0, GEN12_OAG_SCEC7_1) ||
3881
+ REG_EQUAL(addr, GEN12_OAA_DBG_REG) ||
3882
+ REG_EQUAL(addr, GEN12_OAG_OA_PESS) ||
3883
+ REG_EQUAL(addr, GEN12_OAG_SPCTR_CNF);
3884
+}
3885
+
3886
+static bool gen12_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
3887
+{
3888
+ return REG_EQUAL(addr, NOA_WRITE) ||
3889
+ REG_EQUAL(addr, GEN10_NOA_WRITE_HIGH) ||
3890
+ REG_EQUAL(addr, GDT_CHICKEN_BITS) ||
3891
+ REG_EQUAL(addr, WAIT_FOR_RC6_EXIT) ||
3892
+ REG_EQUAL(addr, RPM_CONFIG0) ||
3893
+ REG_EQUAL(addr, RPM_CONFIG1) ||
3894
+ REG_IN_RANGE(addr, NOA_CONFIG(0), NOA_CONFIG(8));
3895
+}
3896
+
3897
+static u32 mask_reg_value(u32 reg, u32 val)
31223898 {
31233899 /* HALF_SLICE_CHICKEN2 is programmed with a the
31243900 * WaDisableSTUnitPowerOptimization workaround. Make sure the value
31253901 * programmed by userspace doesn't change this.
31263902 */
3127
- if (i915_mmio_reg_offset(HALF_SLICE_CHICKEN2) == reg)
3903
+ if (REG_EQUAL(reg, HALF_SLICE_CHICKEN2))
31283904 val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE);
31293905
31303906 /* WAIT_FOR_RC6_EXIT has only one bit fullfilling the function
31313907 * indicated by its name and a bunch of selection fields used by OA
31323908 * configs.
31333909 */
3134
- if (i915_mmio_reg_offset(WAIT_FOR_RC6_EXIT) == reg)
3910
+ if (REG_EQUAL(reg, WAIT_FOR_RC6_EXIT))
31353911 val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE);
31363912
31373913 return val;
31383914 }
31393915
3140
-static struct i915_oa_reg *alloc_oa_regs(struct drm_i915_private *dev_priv,
3141
- bool (*is_valid)(struct drm_i915_private *dev_priv, u32 addr),
3916
+static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,
3917
+ bool (*is_valid)(struct i915_perf *perf, u32 addr),
31423918 u32 __user *regs,
31433919 u32 n_regs)
31443920 {
....@@ -3148,9 +3924,6 @@
31483924
31493925 if (!n_regs)
31503926 return NULL;
3151
-
3152
- if (!access_ok(VERIFY_READ, regs, n_regs * sizeof(u32) * 2))
3153
- return ERR_PTR(-EFAULT);
31543927
31553928 /* No is_valid function means we're not allowing any register to be programmed. */
31563929 GEM_BUG_ON(!is_valid);
....@@ -3168,7 +3941,7 @@
31683941 if (err)
31693942 goto addr_err;
31703943
3171
- if (!is_valid(dev_priv, addr)) {
3944
+ if (!is_valid(perf, addr)) {
31723945 DRM_DEBUG("Invalid oa_reg address: %X\n", addr);
31733946 err = -EINVAL;
31743947 goto addr_err;
....@@ -3191,8 +3964,8 @@
31913964 return ERR_PTR(err);
31923965 }
31933966
3194
-static ssize_t show_dynamic_id(struct device *dev,
3195
- struct device_attribute *attr,
3967
+static ssize_t show_dynamic_id(struct kobject *kobj,
3968
+ struct kobj_attribute *attr,
31963969 char *buf)
31973970 {
31983971 struct i915_oa_config *oa_config =
....@@ -3201,7 +3974,7 @@
32013974 return sprintf(buf, "%d\n", oa_config->id);
32023975 }
32033976
3204
-static int create_dynamic_oa_sysfs_entry(struct drm_i915_private *dev_priv,
3977
+static int create_dynamic_oa_sysfs_entry(struct i915_perf *perf,
32053978 struct i915_oa_config *oa_config)
32063979 {
32073980 sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
....@@ -3216,7 +3989,7 @@
32163989 oa_config->sysfs_metric.name = oa_config->uuid;
32173990 oa_config->sysfs_metric.attrs = oa_config->attrs;
32183991
3219
- return sysfs_create_group(dev_priv->perf.metrics_kobj,
3992
+ return sysfs_create_group(perf->metrics_kobj,
32203993 &oa_config->sysfs_metric);
32213994 }
32223995
....@@ -3236,22 +4009,23 @@
32364009 int i915_perf_add_config_ioctl(struct drm_device *dev, void *data,
32374010 struct drm_file *file)
32384011 {
3239
- struct drm_i915_private *dev_priv = dev->dev_private;
4012
+ struct i915_perf *perf = &to_i915(dev)->perf;
32404013 struct drm_i915_perf_oa_config *args = data;
32414014 struct i915_oa_config *oa_config, *tmp;
4015
+ struct i915_oa_reg *regs;
32424016 int err, id;
32434017
3244
- if (!dev_priv->perf.initialized) {
4018
+ if (!perf->i915) {
32454019 DRM_DEBUG("i915 perf interface not available for this system\n");
32464020 return -ENOTSUPP;
32474021 }
32484022
3249
- if (!dev_priv->perf.metrics_kobj) {
4023
+ if (!perf->metrics_kobj) {
32504024 DRM_DEBUG("OA metrics weren't advertised via sysfs\n");
32514025 return -EINVAL;
32524026 }
32534027
3254
- if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
4028
+ if (i915_perf_stream_paranoid && !perfmon_capable()) {
32554029 DRM_DEBUG("Insufficient privileges to add i915 OA config\n");
32564030 return -EACCES;
32574031 }
....@@ -3269,7 +4043,8 @@
32694043 return -ENOMEM;
32704044 }
32714045
3272
- atomic_set(&oa_config->ref_count, 1);
4046
+ oa_config->perf = perf;
4047
+ kref_init(&oa_config->ref);
32734048
32744049 if (!uuid_is_valid(args->uuid)) {
32754050 DRM_DEBUG("Invalid uuid format for OA config\n");
....@@ -3283,59 +4058,59 @@
32834058 memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid));
32844059
32854060 oa_config->mux_regs_len = args->n_mux_regs;
3286
- oa_config->mux_regs =
3287
- alloc_oa_regs(dev_priv,
3288
- dev_priv->perf.oa.ops.is_valid_mux_reg,
3289
- u64_to_user_ptr(args->mux_regs_ptr),
3290
- args->n_mux_regs);
4061
+ regs = alloc_oa_regs(perf,
4062
+ perf->ops.is_valid_mux_reg,
4063
+ u64_to_user_ptr(args->mux_regs_ptr),
4064
+ args->n_mux_regs);
32914065
3292
- if (IS_ERR(oa_config->mux_regs)) {
4066
+ if (IS_ERR(regs)) {
32934067 DRM_DEBUG("Failed to create OA config for mux_regs\n");
3294
- err = PTR_ERR(oa_config->mux_regs);
4068
+ err = PTR_ERR(regs);
32954069 goto reg_err;
32964070 }
4071
+ oa_config->mux_regs = regs;
32974072
32984073 oa_config->b_counter_regs_len = args->n_boolean_regs;
3299
- oa_config->b_counter_regs =
3300
- alloc_oa_regs(dev_priv,
3301
- dev_priv->perf.oa.ops.is_valid_b_counter_reg,
3302
- u64_to_user_ptr(args->boolean_regs_ptr),
3303
- args->n_boolean_regs);
4074
+ regs = alloc_oa_regs(perf,
4075
+ perf->ops.is_valid_b_counter_reg,
4076
+ u64_to_user_ptr(args->boolean_regs_ptr),
4077
+ args->n_boolean_regs);
33044078
3305
- if (IS_ERR(oa_config->b_counter_regs)) {
4079
+ if (IS_ERR(regs)) {
33064080 DRM_DEBUG("Failed to create OA config for b_counter_regs\n");
3307
- err = PTR_ERR(oa_config->b_counter_regs);
4081
+ err = PTR_ERR(regs);
33084082 goto reg_err;
33094083 }
4084
+ oa_config->b_counter_regs = regs;
33104085
3311
- if (INTEL_GEN(dev_priv) < 8) {
4086
+ if (INTEL_GEN(perf->i915) < 8) {
33124087 if (args->n_flex_regs != 0) {
33134088 err = -EINVAL;
33144089 goto reg_err;
33154090 }
33164091 } else {
33174092 oa_config->flex_regs_len = args->n_flex_regs;
3318
- oa_config->flex_regs =
3319
- alloc_oa_regs(dev_priv,
3320
- dev_priv->perf.oa.ops.is_valid_flex_reg,
3321
- u64_to_user_ptr(args->flex_regs_ptr),
3322
- args->n_flex_regs);
4093
+ regs = alloc_oa_regs(perf,
4094
+ perf->ops.is_valid_flex_reg,
4095
+ u64_to_user_ptr(args->flex_regs_ptr),
4096
+ args->n_flex_regs);
33234097
3324
- if (IS_ERR(oa_config->flex_regs)) {
4098
+ if (IS_ERR(regs)) {
33254099 DRM_DEBUG("Failed to create OA config for flex_regs\n");
3326
- err = PTR_ERR(oa_config->flex_regs);
4100
+ err = PTR_ERR(regs);
33274101 goto reg_err;
33284102 }
4103
+ oa_config->flex_regs = regs;
33294104 }
33304105
3331
- err = mutex_lock_interruptible(&dev_priv->perf.metrics_lock);
4106
+ err = mutex_lock_interruptible(&perf->metrics_lock);
33324107 if (err)
33334108 goto reg_err;
33344109
33354110 /* We shouldn't have too many configs, so this iteration shouldn't be
33364111 * too costly.
33374112 */
3338
- idr_for_each_entry(&dev_priv->perf.metrics_idr, tmp, id) {
4113
+ idr_for_each_entry(&perf->metrics_idr, tmp, id) {
33394114 if (!strcmp(tmp->uuid, oa_config->uuid)) {
33404115 DRM_DEBUG("OA config already exists with this uuid\n");
33414116 err = -EADDRINUSE;
....@@ -3343,14 +4118,14 @@
33434118 }
33444119 }
33454120
3346
- err = create_dynamic_oa_sysfs_entry(dev_priv, oa_config);
4121
+ err = create_dynamic_oa_sysfs_entry(perf, oa_config);
33474122 if (err) {
33484123 DRM_DEBUG("Failed to create sysfs entry for OA config\n");
33494124 goto sysfs_err;
33504125 }
33514126
33524127 /* Config id 0 is invalid, id 1 for kernel stored test config. */
3353
- oa_config->id = idr_alloc(&dev_priv->perf.metrics_idr,
4128
+ oa_config->id = idr_alloc(&perf->metrics_idr,
33544129 oa_config, 2,
33554130 0, GFP_KERNEL);
33564131 if (oa_config->id < 0) {
....@@ -3359,16 +4134,16 @@
33594134 goto sysfs_err;
33604135 }
33614136
3362
- mutex_unlock(&dev_priv->perf.metrics_lock);
4137
+ mutex_unlock(&perf->metrics_lock);
33634138
33644139 DRM_DEBUG("Added config %s id=%i\n", oa_config->uuid, oa_config->id);
33654140
33664141 return oa_config->id;
33674142
33684143 sysfs_err:
3369
- mutex_unlock(&dev_priv->perf.metrics_lock);
4144
+ mutex_unlock(&perf->metrics_lock);
33704145 reg_err:
3371
- put_oa_config(dev_priv, oa_config);
4146
+ i915_oa_config_put(oa_config);
33724147 DRM_DEBUG("Failed to add new OA config\n");
33734148 return err;
33744149 }
....@@ -3387,46 +4162,48 @@
33874162 int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data,
33884163 struct drm_file *file)
33894164 {
3390
- struct drm_i915_private *dev_priv = dev->dev_private;
4165
+ struct i915_perf *perf = &to_i915(dev)->perf;
33914166 u64 *arg = data;
33924167 struct i915_oa_config *oa_config;
33934168 int ret;
33944169
3395
- if (!dev_priv->perf.initialized) {
4170
+ if (!perf->i915) {
33964171 DRM_DEBUG("i915 perf interface not available for this system\n");
33974172 return -ENOTSUPP;
33984173 }
33994174
3400
- if (i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
4175
+ if (i915_perf_stream_paranoid && !perfmon_capable()) {
34014176 DRM_DEBUG("Insufficient privileges to remove i915 OA config\n");
34024177 return -EACCES;
34034178 }
34044179
3405
- ret = mutex_lock_interruptible(&dev_priv->perf.metrics_lock);
4180
+ ret = mutex_lock_interruptible(&perf->metrics_lock);
34064181 if (ret)
3407
- goto lock_err;
4182
+ return ret;
34084183
3409
- oa_config = idr_find(&dev_priv->perf.metrics_idr, *arg);
4184
+ oa_config = idr_find(&perf->metrics_idr, *arg);
34104185 if (!oa_config) {
34114186 DRM_DEBUG("Failed to remove unknown OA config\n");
34124187 ret = -ENOENT;
3413
- goto config_err;
4188
+ goto err_unlock;
34144189 }
34154190
34164191 GEM_BUG_ON(*arg != oa_config->id);
34174192
3418
- sysfs_remove_group(dev_priv->perf.metrics_kobj,
3419
- &oa_config->sysfs_metric);
4193
+ sysfs_remove_group(perf->metrics_kobj, &oa_config->sysfs_metric);
34204194
3421
- idr_remove(&dev_priv->perf.metrics_idr, *arg);
4195
+ idr_remove(&perf->metrics_idr, *arg);
4196
+
4197
+ mutex_unlock(&perf->metrics_lock);
34224198
34234199 DRM_DEBUG("Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
34244200
3425
- put_oa_config(dev_priv, oa_config);
4201
+ i915_oa_config_put(oa_config);
34264202
3427
-config_err:
3428
- mutex_unlock(&dev_priv->perf.metrics_lock);
3429
-lock_err:
4203
+ return 0;
4204
+
4205
+err_unlock:
4206
+ mutex_unlock(&perf->metrics_lock);
34304207 return ret;
34314208 }
34324209
....@@ -3437,8 +4214,8 @@
34374214 .maxlen = sizeof(i915_perf_stream_paranoid),
34384215 .mode = 0644,
34394216 .proc_handler = proc_dointvec_minmax,
3440
- .extra1 = &zero,
3441
- .extra2 = &one,
4217
+ .extra1 = SYSCTL_ZERO,
4218
+ .extra2 = SYSCTL_ONE,
34424219 },
34434220 {
34444221 .procname = "oa_max_sample_rate",
....@@ -3446,7 +4223,7 @@
34464223 .maxlen = sizeof(i915_oa_max_sample_rate),
34474224 .mode = 0644,
34484225 .proc_handler = proc_dointvec_minmax,
3449
- .extra1 = &zero,
4226
+ .extra1 = SYSCTL_ZERO,
34504227 .extra2 = &oa_sample_rate_hard_limit,
34514228 },
34524229 {}
....@@ -3473,138 +4250,221 @@
34734250 };
34744251
34754252 /**
3476
- * i915_perf_init - initialize i915-perf state on module load
3477
- * @dev_priv: i915 device instance
4253
+ * i915_perf_init - initialize i915-perf state on module bind
4254
+ * @i915: i915 device instance
34784255 *
34794256 * Initializes i915-perf state without exposing anything to userspace.
34804257 *
34814258 * Note: i915-perf initialization is split into an 'init' and 'register'
34824259 * phase with the i915_perf_register() exposing state to userspace.
34834260 */
3484
-void i915_perf_init(struct drm_i915_private *dev_priv)
4261
+void i915_perf_init(struct drm_i915_private *i915)
34854262 {
3486
- if (IS_HASWELL(dev_priv)) {
3487
- dev_priv->perf.oa.ops.is_valid_b_counter_reg =
3488
- gen7_is_valid_b_counter_addr;
3489
- dev_priv->perf.oa.ops.is_valid_mux_reg =
3490
- hsw_is_valid_mux_addr;
3491
- dev_priv->perf.oa.ops.is_valid_flex_reg = NULL;
3492
- dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer;
3493
- dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set;
3494
- dev_priv->perf.oa.ops.disable_metric_set = hsw_disable_metric_set;
3495
- dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable;
3496
- dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable;
3497
- dev_priv->perf.oa.ops.read = gen7_oa_read;
3498
- dev_priv->perf.oa.ops.oa_hw_tail_read =
3499
- gen7_oa_hw_tail_read;
4263
+ struct i915_perf *perf = &i915->perf;
35004264
3501
- dev_priv->perf.oa.oa_formats = hsw_oa_formats;
3502
- } else if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
4265
+ /* XXX const struct i915_perf_ops! */
4266
+
4267
+ if (IS_HASWELL(i915)) {
4268
+ perf->ops.is_valid_b_counter_reg = gen7_is_valid_b_counter_addr;
4269
+ perf->ops.is_valid_mux_reg = hsw_is_valid_mux_addr;
4270
+ perf->ops.is_valid_flex_reg = NULL;
4271
+ perf->ops.enable_metric_set = hsw_enable_metric_set;
4272
+ perf->ops.disable_metric_set = hsw_disable_metric_set;
4273
+ perf->ops.oa_enable = gen7_oa_enable;
4274
+ perf->ops.oa_disable = gen7_oa_disable;
4275
+ perf->ops.read = gen7_oa_read;
4276
+ perf->ops.oa_hw_tail_read = gen7_oa_hw_tail_read;
4277
+
4278
+ perf->oa_formats = hsw_oa_formats;
4279
+ } else if (HAS_LOGICAL_RING_CONTEXTS(i915)) {
35034280 /* Note: that although we could theoretically also support the
35044281 * legacy ringbuffer mode on BDW (and earlier iterations of
35054282 * this driver, before upstreaming did this) it didn't seem
35064283 * worth the complexity to maintain now that BDW+ enable
35074284 * execlist mode by default.
35084285 */
3509
- dev_priv->perf.oa.oa_formats = gen8_plus_oa_formats;
4286
+ perf->ops.read = gen8_oa_read;
35104287
3511
- dev_priv->perf.oa.ops.init_oa_buffer = gen8_init_oa_buffer;
3512
- dev_priv->perf.oa.ops.oa_enable = gen8_oa_enable;
3513
- dev_priv->perf.oa.ops.oa_disable = gen8_oa_disable;
3514
- dev_priv->perf.oa.ops.read = gen8_oa_read;
3515
- dev_priv->perf.oa.ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
4288
+ if (IS_GEN_RANGE(i915, 8, 9)) {
4289
+ perf->oa_formats = gen8_plus_oa_formats;
35164290
3517
- if (IS_GEN8(dev_priv) || IS_GEN9(dev_priv)) {
3518
- dev_priv->perf.oa.ops.is_valid_b_counter_reg =
4291
+ perf->ops.is_valid_b_counter_reg =
35194292 gen7_is_valid_b_counter_addr;
3520
- dev_priv->perf.oa.ops.is_valid_mux_reg =
4293
+ perf->ops.is_valid_mux_reg =
35214294 gen8_is_valid_mux_addr;
3522
- dev_priv->perf.oa.ops.is_valid_flex_reg =
4295
+ perf->ops.is_valid_flex_reg =
35234296 gen8_is_valid_flex_addr;
35244297
3525
- if (IS_CHERRYVIEW(dev_priv)) {
3526
- dev_priv->perf.oa.ops.is_valid_mux_reg =
4298
+ if (IS_CHERRYVIEW(i915)) {
4299
+ perf->ops.is_valid_mux_reg =
35274300 chv_is_valid_mux_addr;
35284301 }
35294302
3530
- dev_priv->perf.oa.ops.enable_metric_set = gen8_enable_metric_set;
3531
- dev_priv->perf.oa.ops.disable_metric_set = gen8_disable_metric_set;
4303
+ perf->ops.oa_enable = gen8_oa_enable;
4304
+ perf->ops.oa_disable = gen8_oa_disable;
4305
+ perf->ops.enable_metric_set = gen8_enable_metric_set;
4306
+ perf->ops.disable_metric_set = gen8_disable_metric_set;
4307
+ perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
35324308
3533
- if (IS_GEN8(dev_priv)) {
3534
- dev_priv->perf.oa.ctx_oactxctrl_offset = 0x120;
3535
- dev_priv->perf.oa.ctx_flexeu0_offset = 0x2ce;
4309
+ if (IS_GEN(i915, 8)) {
4310
+ perf->ctx_oactxctrl_offset = 0x120;
4311
+ perf->ctx_flexeu0_offset = 0x2ce;
35364312
3537
- dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<25);
4313
+ perf->gen8_valid_ctx_bit = BIT(25);
35384314 } else {
3539
- dev_priv->perf.oa.ctx_oactxctrl_offset = 0x128;
3540
- dev_priv->perf.oa.ctx_flexeu0_offset = 0x3de;
4315
+ perf->ctx_oactxctrl_offset = 0x128;
4316
+ perf->ctx_flexeu0_offset = 0x3de;
35414317
3542
- dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<16);
4318
+ perf->gen8_valid_ctx_bit = BIT(16);
35434319 }
3544
- } else if (IS_GEN(dev_priv, 10, 11)) {
3545
- dev_priv->perf.oa.ops.is_valid_b_counter_reg =
4320
+ } else if (IS_GEN_RANGE(i915, 10, 11)) {
4321
+ perf->oa_formats = gen8_plus_oa_formats;
4322
+
4323
+ perf->ops.is_valid_b_counter_reg =
35464324 gen7_is_valid_b_counter_addr;
3547
- dev_priv->perf.oa.ops.is_valid_mux_reg =
4325
+ perf->ops.is_valid_mux_reg =
35484326 gen10_is_valid_mux_addr;
3549
- dev_priv->perf.oa.ops.is_valid_flex_reg =
4327
+ perf->ops.is_valid_flex_reg =
35504328 gen8_is_valid_flex_addr;
35514329
3552
- dev_priv->perf.oa.ops.enable_metric_set = gen8_enable_metric_set;
3553
- dev_priv->perf.oa.ops.disable_metric_set = gen10_disable_metric_set;
4330
+ perf->ops.oa_enable = gen8_oa_enable;
4331
+ perf->ops.oa_disable = gen8_oa_disable;
4332
+ perf->ops.enable_metric_set = gen8_enable_metric_set;
4333
+ perf->ops.disable_metric_set = gen10_disable_metric_set;
4334
+ perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
35544335
3555
- dev_priv->perf.oa.ctx_oactxctrl_offset = 0x128;
3556
- dev_priv->perf.oa.ctx_flexeu0_offset = 0x3de;
4336
+ if (IS_GEN(i915, 10)) {
4337
+ perf->ctx_oactxctrl_offset = 0x128;
4338
+ perf->ctx_flexeu0_offset = 0x3de;
4339
+ } else {
4340
+ perf->ctx_oactxctrl_offset = 0x124;
4341
+ perf->ctx_flexeu0_offset = 0x78e;
4342
+ }
4343
+ perf->gen8_valid_ctx_bit = BIT(16);
4344
+ } else if (IS_GEN(i915, 12)) {
4345
+ perf->oa_formats = gen12_oa_formats;
35574346
3558
- dev_priv->perf.oa.gen8_valid_ctx_bit = (1<<16);
4347
+ perf->ops.is_valid_b_counter_reg =
4348
+ gen12_is_valid_b_counter_addr;
4349
+ perf->ops.is_valid_mux_reg =
4350
+ gen12_is_valid_mux_addr;
4351
+ perf->ops.is_valid_flex_reg =
4352
+ gen8_is_valid_flex_addr;
4353
+
4354
+ perf->ops.oa_enable = gen12_oa_enable;
4355
+ perf->ops.oa_disable = gen12_oa_disable;
4356
+ perf->ops.enable_metric_set = gen12_enable_metric_set;
4357
+ perf->ops.disable_metric_set = gen12_disable_metric_set;
4358
+ perf->ops.oa_hw_tail_read = gen12_oa_hw_tail_read;
4359
+
4360
+ perf->ctx_flexeu0_offset = 0;
4361
+ perf->ctx_oactxctrl_offset = 0x144;
35594362 }
35604363 }
35614364
3562
- if (dev_priv->perf.oa.ops.enable_metric_set) {
3563
- hrtimer_init(&dev_priv->perf.oa.poll_check_timer,
3564
- CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3565
- dev_priv->perf.oa.poll_check_timer.function = oa_poll_check_timer_cb;
3566
- init_waitqueue_head(&dev_priv->perf.oa.poll_wq);
4365
+ if (perf->ops.enable_metric_set) {
4366
+ mutex_init(&perf->lock);
35674367
3568
- INIT_LIST_HEAD(&dev_priv->perf.streams);
3569
- mutex_init(&dev_priv->perf.lock);
3570
- spin_lock_init(&dev_priv->perf.oa.oa_buffer.ptr_lock);
4368
+ oa_sample_rate_hard_limit =
4369
+ RUNTIME_INFO(i915)->cs_timestamp_frequency_hz / 2;
35714370
3572
- oa_sample_rate_hard_limit = 1000 *
3573
- (INTEL_INFO(dev_priv)->cs_timestamp_frequency_khz / 2);
3574
- dev_priv->perf.sysctl_header = register_sysctl_table(dev_root);
4371
+ mutex_init(&perf->metrics_lock);
4372
+ idr_init(&perf->metrics_idr);
35754373
3576
- mutex_init(&dev_priv->perf.metrics_lock);
3577
- idr_init(&dev_priv->perf.metrics_idr);
4374
+ /* We set up some ratelimit state to potentially throttle any
4375
+ * _NOTES about spurious, invalid OA reports which we don't
4376
+ * forward to userspace.
4377
+ *
4378
+ * We print a _NOTE about any throttling when closing the
4379
+ * stream instead of waiting until driver _fini which no one
4380
+ * would ever see.
4381
+ *
4382
+ * Using the same limiting factors as printk_ratelimit()
4383
+ */
4384
+ ratelimit_state_init(&perf->spurious_report_rs, 5 * HZ, 10);
4385
+ /* Since we use a DRM_NOTE for spurious reports it would be
4386
+ * inconsistent to let __ratelimit() automatically print a
4387
+ * warning for throttling.
4388
+ */
4389
+ ratelimit_set_flags(&perf->spurious_report_rs,
4390
+ RATELIMIT_MSG_ON_RELEASE);
35784391
3579
- dev_priv->perf.initialized = true;
4392
+ ratelimit_state_init(&perf->tail_pointer_race,
4393
+ 5 * HZ, 10);
4394
+ ratelimit_set_flags(&perf->tail_pointer_race,
4395
+ RATELIMIT_MSG_ON_RELEASE);
4396
+
4397
+ atomic64_set(&perf->noa_programming_delay,
4398
+ 500 * 1000 /* 500us */);
4399
+
4400
+ perf->i915 = i915;
35804401 }
35814402 }
35824403
35834404 static int destroy_config(int id, void *p, void *data)
35844405 {
3585
- struct drm_i915_private *dev_priv = data;
3586
- struct i915_oa_config *oa_config = p;
3587
-
3588
- put_oa_config(dev_priv, oa_config);
3589
-
4406
+ i915_oa_config_put(p);
35904407 return 0;
4408
+}
4409
+
4410
+void i915_perf_sysctl_register(void)
4411
+{
4412
+ sysctl_header = register_sysctl_table(dev_root);
4413
+}
4414
+
4415
+void i915_perf_sysctl_unregister(void)
4416
+{
4417
+ unregister_sysctl_table(sysctl_header);
35914418 }
35924419
35934420 /**
35944421 * i915_perf_fini - Counter part to i915_perf_init()
3595
- * @dev_priv: i915 device instance
4422
+ * @i915: i915 device instance
35964423 */
3597
-void i915_perf_fini(struct drm_i915_private *dev_priv)
4424
+void i915_perf_fini(struct drm_i915_private *i915)
35984425 {
3599
- if (!dev_priv->perf.initialized)
4426
+ struct i915_perf *perf = &i915->perf;
4427
+
4428
+ if (!perf->i915)
36004429 return;
36014430
3602
- idr_for_each(&dev_priv->perf.metrics_idr, destroy_config, dev_priv);
3603
- idr_destroy(&dev_priv->perf.metrics_idr);
4431
+ idr_for_each(&perf->metrics_idr, destroy_config, perf);
4432
+ idr_destroy(&perf->metrics_idr);
36044433
3605
- unregister_sysctl_table(dev_priv->perf.sysctl_header);
3606
-
3607
- memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops));
3608
-
3609
- dev_priv->perf.initialized = false;
4434
+ memset(&perf->ops, 0, sizeof(perf->ops));
4435
+ perf->i915 = NULL;
36104436 }
4437
+
4438
+/**
4439
+ * i915_perf_ioctl_version - Version of the i915-perf subsystem
4440
+ *
4441
+ * This version number is used by userspace to detect available features.
4442
+ */
4443
+int i915_perf_ioctl_version(void)
4444
+{
4445
+ /*
4446
+ * 1: Initial version
4447
+ * I915_PERF_IOCTL_ENABLE
4448
+ * I915_PERF_IOCTL_DISABLE
4449
+ *
4450
+ * 2: Added runtime modification of OA config.
4451
+ * I915_PERF_IOCTL_CONFIG
4452
+ *
4453
+ * 3: Add DRM_I915_PERF_PROP_HOLD_PREEMPTION parameter to hold
4454
+ * preemption on a particular context so that performance data is
4455
+ * accessible from a delta of MI_RPC reports without looking at the
4456
+ * OA buffer.
4457
+ *
4458
+ * 4: Add DRM_I915_PERF_PROP_ALLOWED_SSEU to limit what contexts can
4459
+ * be run for the duration of the performance recording based on
4460
+ * their SSEU configuration.
4461
+ *
4462
+ * 5: Add DRM_I915_PERF_PROP_POLL_OA_PERIOD parameter that controls the
4463
+ * interval for the hrtimer used to check for OA data.
4464
+ */
4465
+ return 5;
4466
+}
4467
+
4468
+#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
4469
+#include "selftests/i915_perf.c"
4470
+#endif