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