All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit
@ 2016-09-14 14:19 Robert Bragg
  2016-09-14 14:19 ` [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure Robert Bragg
                   ` (14 more replies)
  0 siblings, 15 replies; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: David Airlie, dri-devel, Sourab Gupta, Daniel Vetter

This just rebases my i915 perf series on a recent drm-intel-nightly.

Considering now that this series has been reviewed a number of times by Chris,
and I think I've responded to his feedback: I wonder if this series is ready
to be added to drm-intel-nightly soon?

I think most of the effort for this series at the moment is just keeping up
with rebasing on nightlies.

Regards,
- Robert

Robert Bragg (11):
  drm/i915: Add i915 perf infrastructure
  drm/i915: rename OACONTROL GEN7_OACONTROL
  drm/i915: return EACCES for check_cmd() failures
  drm/i915: don't whitelist oacontrol in cmd parser
  drm/i915: Add 'render basic' Haswell OA unit config
  drm/i915: Enable i915 perf stream for Haswell OA unit
  drm/i915: advertise available metrics via sysfs
  drm/i915: Add dev.i915.perf_event_paranoid sysctl option
  drm/i915: add oa_event_min_timer_exponent sysctl
  drm/i915: Add more Haswell OA metric sets
  drm/i915: Add a kerneldoc summary for i915_perf.c

 drivers/gpu/drm/i915/Makefile           |    4 +
 drivers/gpu/drm/i915/i915_cmd_parser.c  |   40 +-
 drivers/gpu/drm/i915/i915_drv.c         |    9 +
 drivers/gpu/drm/i915/i915_drv.h         |  162 +++
 drivers/gpu/drm/i915/i915_gem_context.c |   22 +-
 drivers/gpu/drm/i915/i915_oa_hsw.c      |  751 ++++++++++++++
 drivers/gpu/drm/i915/i915_oa_hsw.h      |   38 +
 drivers/gpu/drm/i915/i915_perf.c        | 1686 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/i915_reg.h         |  340 ++++++-
 drivers/gpu/drm/i915/intel_ringbuffer.c |   10 +-
 include/uapi/drm/i915_drm.h             |  133 +++
 11 files changed, 3154 insertions(+), 41 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/i915_oa_hsw.c
 create mode 100644 drivers/gpu/drm/i915/i915_oa_hsw.h
 create mode 100644 drivers/gpu/drm/i915/i915_perf.c

-- 
2.9.2

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-09-14 14:42   ` [Intel-gfx] " Emil Velikov
  2016-09-14 14:19 ` [PATCH v5 02/11] drm/i915: rename OACONTROL GEN7_OACONTROL Robert Bragg
                   ` (13 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: David Airlie, dri-devel, Sourab Gupta, Daniel Vetter

Adds base i915 perf infrastructure for Gen performance metrics.

This adds a DRM_IOCTL_I915_PERF_OPEN ioctl that takes an array of uint64
properties to configure a stream of metrics and returns a new fd usable
with standard VFS system calls including read() to read typed and sized
records; ioctl() to enable or disable capture and poll() to wait for
data.

A stream is opened something like:

  uint64_t properties[] = {
      /* Single context sampling */
      DRM_I915_PERF_PROP_CTX_HANDLE,        ctx_handle,

      /* Include OA reports in samples */
      DRM_I915_PERF_PROP_SAMPLE_OA,         true,

      /* OA unit configuration */
      DRM_I915_PERF_PROP_OA_METRICS_SET,    metrics_set_id,
      DRM_I915_PERF_PROP_OA_FORMAT,         report_format,
      DRM_I915_PERF_PROP_OA_EXPONENT,       period_exponent,
   };
   struct drm_i915_perf_open_param parm = {
      .flags = I915_PERF_FLAG_FD_CLOEXEC |
               I915_PERF_FLAG_FD_NONBLOCK |
               I915_PERF_FLAG_DISABLED,
      .properties_ptr = (uint64_t)properties,
      .num_properties = sizeof(properties) / 16,
   };
   int fd = drmIoctl(drm_fd, DRM_IOCTL_I915_PERF_OPEN, &param);

Records read all start with a common { type, size } header with
DRM_I915_PERF_RECORD_SAMPLE being of most interest. Sample records
contain an extensible number of fields and it's the
DRM_I915_PERF_PROP_SAMPLE_xyz properties given when opening that
determine what's included in every sample.

No specific streams are supported yet so any attempt to open a stream
will return an error.

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/Makefile    |   3 +
 drivers/gpu/drm/i915/i915_drv.c  |   4 +
 drivers/gpu/drm/i915/i915_drv.h  |  91 ++++++++
 drivers/gpu/drm/i915/i915_perf.c | 448 +++++++++++++++++++++++++++++++++++++++
 include/uapi/drm/i915_drm.h      |  67 ++++++
 5 files changed, 613 insertions(+)
 create mode 100644 drivers/gpu/drm/i915/i915_perf.c

diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
index a998c2b..d991781 100644
--- a/drivers/gpu/drm/i915/Makefile
+++ b/drivers/gpu/drm/i915/Makefile
@@ -110,6 +110,9 @@ i915-y += dvo_ch7017.o \
 # virtual gpu code
 i915-y += i915_vgpu.o
 
+# perf code
+i915-y += i915_perf.o
+
 ifeq ($(CONFIG_DRM_I915_GVT),y)
 i915-y += intel_gvt.o
 include $(src)/gvt/Makefile
diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
index 7f4e8ad..14f22fc 100644
--- a/drivers/gpu/drm/i915/i915_drv.c
+++ b/drivers/gpu/drm/i915/i915_drv.c
@@ -838,6 +838,8 @@ static int i915_driver_init_early(struct drm_i915_private *dev_priv,
 
 	intel_device_info_dump(dev_priv);
 
+	i915_perf_init(dev_priv);
+
 	/* Not all pre-production machines fall into this category, only the
 	 * very first ones. Almost everything should work, except for maybe
 	 * suspend/resume. And we don't implement workarounds that affect only
@@ -859,6 +861,7 @@ err_workqueues:
  */
 static void i915_driver_cleanup_early(struct drm_i915_private *dev_priv)
 {
+	i915_perf_fini(dev_priv);
 	i915_gem_load_cleanup(&dev_priv->drm);
 	i915_workqueues_cleanup(dev_priv);
 }
@@ -2560,6 +2563,7 @@ static const struct drm_ioctl_desc i915_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
 };
 
 static struct drm_driver driver = {
diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index 1e2dda8..0f5cd8f 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -1740,6 +1740,84 @@ struct intel_wm_config {
 	bool sprites_scaled;
 };
 
+struct i915_perf_stream;
+
+struct i915_perf_stream_ops {
+	/* Enables the collection of HW samples, either in response to
+	 * I915_PERF_IOCTL_ENABLE or implicitly called when stream is
+	 * opened without I915_PERF_FLAG_DISABLED.
+	 */
+	void (*enable)(struct i915_perf_stream *stream);
+
+	/* Disables the collection of HW samples, either in response to
+	 * I915_PERF_IOCTL_DISABLE or implicitly called before
+	 * destroying the stream.
+	 */
+	void (*disable)(struct i915_perf_stream *stream);
+
+	/* Return: true if any i915 perf records are ready to read()
+	 * for this stream.
+	 */
+	bool (*can_read)(struct i915_perf_stream *stream);
+
+	/* Call poll_wait, passing a wait queue that will be woken
+	 * once there is something ready to read() for the stream
+	 */
+	void (*poll_wait)(struct i915_perf_stream *stream,
+			  struct file *file,
+			  poll_table *wait);
+
+	/* For handling a blocking read, wait until there is something
+	 * to ready to read() for the stream. E.g. wait on the same
+	 * wait queue that would be passed to poll_wait() until
+	 * ->can_read() returns true (if its safe to call ->can_read()
+	 * without the i915 perf lock held).
+	 */
+	int (*wait_unlocked)(struct i915_perf_stream *stream);
+
+	/* read - Copy buffered metrics as records to userspace
+	 * @buf: the userspace, destination buffer
+	 * @count: the number of bytes to copy, requested by userspace
+	 * @offset: zero at the start of the read, updated as the read
+	 *          proceeds, it represents how many bytes have been
+	 *          copied so far and the buffer offset for copying the
+	 *          next record.
+	 *
+	 * Copy as many buffered i915 perf samples and records for
+	 * this stream to userspace as will fit in the given buffer.
+	 *
+	 * Only write complete records; returning -ENOSPC if there
+	 * isn't room for a complete record.
+	 *
+	 * Return any error condition that results in a short read
+	 * such as -ENOSPC or -EFAULT, even though these may be
+	 * squashed before returning to userspace.
+	 */
+	int (*read)(struct i915_perf_stream *stream,
+		    char __user *buf,
+		    size_t count,
+		    size_t *offset);
+
+	/* Cleanup any stream specific resources.
+	 *
+	 * The stream will always be disabled before this is called.
+	 */
+	void (*destroy)(struct i915_perf_stream *stream);
+};
+
+struct i915_perf_stream {
+	struct drm_i915_private *dev_priv;
+
+	struct list_head link;
+
+	u32 sample_flags;
+
+	struct i915_gem_context *ctx;
+	bool enabled;
+
+	struct i915_perf_stream_ops *ops;
+};
+
 struct drm_i915_private {
 	struct drm_device drm;
 
@@ -2040,6 +2118,12 @@ struct drm_i915_private {
 
 	struct i915_runtime_pm pm;
 
+	struct {
+		bool initialized;
+		struct mutex lock;
+		struct list_head streams;
+	} perf;
+
 	/* Abstract the submission mechanism (legacy ringbuffer or execlists) away */
 	struct {
 		void (*resume)(struct drm_i915_private *);
@@ -3445,6 +3529,9 @@ int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
 				       struct drm_file *file);
 
+int i915_perf_open_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *file);
+
 /* i915_gem_evict.c */
 int __must_check i915_gem_evict_something(struct i915_address_space *vm,
 					  u64 min_size, u64 alignment,
@@ -3555,6 +3642,10 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
 			    u32 batch_len,
 			    bool is_master);
 
+/* i915_perf.c */
+extern void i915_perf_init(struct drm_i915_private *dev_priv);
+extern void i915_perf_fini(struct drm_i915_private *dev_priv);
+
 /* i915_suspend.c */
 extern int i915_save_state(struct drm_device *dev);
 extern int i915_restore_state(struct drm_device *dev);
diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
new file mode 100644
index 0000000..87530f5
--- /dev/null
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -0,0 +1,448 @@
+/*
+ * Copyright © 2015-2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Robert Bragg <robert@sixbynine.org>
+ */
+
+#include <linux/anon_inodes.h>
+#include <linux/sizes.h>
+
+#include "i915_drv.h"
+
+struct perf_open_properties {
+	u32 sample_flags;
+
+	u64 single_context:1;
+	u64 ctx_handle;
+};
+
+static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
+				     struct file *file,
+				     char __user *buf,
+				     size_t count,
+				     loff_t *ppos)
+{
+	/* Note we keep the offset (aka bytes read) separate from any
+	 * error status so that the final check for whether we return
+	 * the bytes read with a higher precedence than any error (see
+	 * comment below) doesn't need to be handled/duplicated in
+	 * stream->ops->read() implementations.
+	 */
+	size_t offset = 0;
+	int ret = stream->ops->read(stream, buf, count, &offset);
+
+	/* If we've successfully copied any data then reporting that
+	 * takes precedence over any internal error status, so the
+	 * data isn't lost.
+	 *
+	 * For example ret will be -ENOSPC whenever there is more
+	 * buffered data than can be copied to userspace, but that's
+	 * only interesting if we weren't able to copy some data
+	 * because it implies the userspace buffer is too small to
+	 * receive a single record (and we never split records).
+	 *
+	 * Another case with ret == -EFAULT is more of a grey area
+	 * since it would seem like bad form for userspace to ask us
+	 * to overrun its buffer, but the user knows best:
+	 *
+	 *   http://yarchive.net/comp/linux/partial_reads_writes.html
+	 */
+	return offset ?: (ret ?: -EAGAIN);
+}
+
+static ssize_t i915_perf_read(struct file *file,
+			      char __user *buf,
+			      size_t count,
+			      loff_t *ppos)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	ssize_t ret;
+
+	if (!(file->f_flags & O_NONBLOCK)) {
+		/* Allow false positives from stream->ops->wait_unlocked.
+		 */
+		do {
+			ret = stream->ops->wait_unlocked(stream);
+			if (ret)
+				return ret;
+
+			mutex_lock(&dev_priv->perf.lock);
+			ret = i915_perf_read_locked(stream, file,
+						    buf, count, ppos);
+			mutex_unlock(&dev_priv->perf.lock);
+		} while (ret == -EAGAIN);
+	} else {
+		mutex_lock(&dev_priv->perf.lock);
+		ret = i915_perf_read_locked(stream, file, buf, count, ppos);
+		mutex_unlock(&dev_priv->perf.lock);
+	}
+
+	return ret;
+}
+
+static unsigned int i915_perf_poll_locked(struct i915_perf_stream *stream,
+					  struct file *file,
+					  poll_table *wait)
+{
+	unsigned int streams = 0;
+
+	stream->ops->poll_wait(stream, file, wait);
+
+	if (stream->ops->can_read(stream))
+		streams |= POLLIN;
+
+	return streams;
+}
+
+static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	int ret;
+
+	mutex_lock(&dev_priv->perf.lock);
+	ret = i915_perf_poll_locked(stream, file, wait);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return ret;
+}
+
+static void i915_perf_enable_locked(struct i915_perf_stream *stream)
+{
+	if (stream->enabled)
+		return;
+
+	/* Allow stream->ops->enable() to refer to this */
+	stream->enabled = true;
+
+	if (stream->ops->enable)
+		stream->ops->enable(stream);
+}
+
+static void i915_perf_disable_locked(struct i915_perf_stream *stream)
+{
+	if (!stream->enabled)
+		return;
+
+	/* Allow stream->ops->disable() to refer to this */
+	stream->enabled = false;
+
+	if (stream->ops->disable)
+		stream->ops->disable(stream);
+}
+
+static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
+				   unsigned int cmd,
+				   unsigned long arg)
+{
+	switch (cmd) {
+	case I915_PERF_IOCTL_ENABLE:
+		i915_perf_enable_locked(stream);
+		return 0;
+	case I915_PERF_IOCTL_DISABLE:
+		i915_perf_disable_locked(stream);
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static long i915_perf_ioctl(struct file *file,
+			    unsigned int cmd,
+			    unsigned long arg)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	long ret;
+
+	mutex_lock(&dev_priv->perf.lock);
+	ret = i915_perf_ioctl_locked(stream, cmd, arg);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return ret;
+}
+
+static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	if (stream->enabled)
+		i915_perf_disable_locked(stream);
+
+	if (stream->ops->destroy)
+		stream->ops->destroy(stream);
+
+	list_del(&stream->link);
+
+	if (stream->ctx) {
+		mutex_lock(&dev_priv->drm.struct_mutex);
+		i915_gem_context_put(stream->ctx);
+		mutex_unlock(&dev_priv->drm.struct_mutex);
+	}
+
+	kfree(stream);
+}
+
+static int i915_perf_release(struct inode *inode, struct file *file)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	mutex_lock(&dev_priv->perf.lock);
+	i915_perf_destroy_locked(stream);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return 0;
+}
+
+
+static const struct file_operations fops = {
+	.owner		= THIS_MODULE,
+	.llseek		= no_llseek,
+	.release	= i915_perf_release,
+	.poll		= i915_perf_poll,
+	.read		= i915_perf_read,
+	.unlocked_ioctl	= i915_perf_ioctl,
+};
+
+
+static struct i915_gem_context *
+lookup_context(struct drm_i915_private *dev_priv,
+	       struct drm_i915_file_private *file_priv,
+	       u32 ctx_user_handle)
+{
+	struct i915_gem_context *ctx;
+	int ret;
+
+	ret = i915_mutex_lock_interruptible(&dev_priv->drm);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ctx = i915_gem_context_lookup(file_priv, ctx_user_handle);
+	if (!IS_ERR(ctx))
+		i915_gem_context_get(ctx);
+
+	mutex_unlock(&dev_priv->drm.struct_mutex);
+
+	return ctx;
+}
+
+int i915_perf_open_ioctl_locked(struct drm_device *dev,
+				struct drm_i915_perf_open_param *param,
+				struct perf_open_properties *props,
+				struct drm_file *file)
+{
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct i915_gem_context *specific_ctx = NULL;
+	struct i915_perf_stream *stream = NULL;
+	unsigned long f_flags = 0;
+	int stream_fd;
+	int ret = 0;
+
+	if (props->single_context) {
+		u32 ctx_handle = props->ctx_handle;
+		struct drm_i915_file_private *file_priv = file->driver_priv;
+
+		specific_ctx = lookup_context(dev_priv, file_priv, ctx_handle);
+		if (IS_ERR(specific_ctx)) {
+			ret = PTR_ERR(specific_ctx);
+			if (ret != -EINTR)
+				DRM_ERROR("Failed to look up context with ID %u for opening perf stream\n",
+					  ctx_handle);
+			goto err;
+		}
+	}
+
+	if (!specific_ctx && !capable(CAP_SYS_ADMIN)) {
+		DRM_ERROR("Insufficient privileges to open system-wide i915 perf stream\n");
+		ret = -EACCES;
+		goto err_ctx;
+	}
+
+	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
+	if (!stream) {
+		ret = -ENOMEM;
+		goto err_ctx;
+	}
+
+	stream->sample_flags = props->sample_flags;
+	stream->dev_priv = dev_priv;
+	stream->ctx = specific_ctx;
+
+	/*
+	 * TODO: support sampling something
+	 *
+	 * For now this is as far as we can go.
+	 */
+	DRM_ERROR("Unsupported i915 perf stream configuration\n");
+	ret = -EINVAL;
+	goto err_alloc;
+
+	list_add(&stream->link, &dev_priv->perf.streams);
+
+	if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
+		f_flags |= O_CLOEXEC;
+	if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
+		f_flags |= O_NONBLOCK;
+
+	stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
+	if (stream_fd < 0) {
+		ret = stream_fd;
+		goto err_open;
+	}
+
+	if (!(param->flags & I915_PERF_FLAG_DISABLED))
+		i915_perf_enable_locked(stream);
+
+	return stream_fd;
+
+err_open:
+	list_del(&stream->link);
+	if (stream->ops->destroy)
+		stream->ops->destroy(stream);
+err_alloc:
+	kfree(stream);
+err_ctx:
+	if (specific_ctx) {
+		mutex_lock(&dev_priv->drm.struct_mutex);
+		i915_gem_context_put(specific_ctx);
+		mutex_unlock(&dev_priv->drm.struct_mutex);
+	}
+err:
+	return ret;
+}
+
+/* Note we copy the properties from userspace outside of the i915 perf
+ * mutex to avoid an awkward lockdep with mmap_sem.
+ *
+ * Note this function only validates properties in isolation it doesn't
+ * validate that the combination of properties makes sense or that all
+ * properties necessary for a particular kind of stream have been set.
+ */
+static int read_properties_unlocked(struct drm_i915_private *dev_priv,
+				    u64 __user *uprops,
+				    u32 n_props,
+				    struct perf_open_properties *props)
+{
+	u64 __user *uprop = uprops;
+	int i;
+
+	memset(props, 0, sizeof(struct perf_open_properties));
+
+	if (!n_props) {
+		DRM_ERROR("No i915 perf properties given");
+		return -EINVAL;
+	}
+
+	if (n_props > DRM_I915_PERF_PROP_MAX) {
+		DRM_ERROR("More i915 perf properties specified than exist");
+		return -EINVAL;
+	}
+
+	for (i = 0; i < n_props; i++) {
+		u64 id, value;
+		int ret;
+
+		ret = get_user(id, (u64 __user *)uprop);
+		if (ret)
+			return ret;
+
+		if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
+			DRM_ERROR("Unknown i915 perf property ID");
+			return -EINVAL;
+		}
+
+		ret = get_user(value, (u64 __user *)uprop + 1);
+		if (ret)
+			return ret;
+
+		switch ((enum drm_i915_perf_property_id)id) {
+		case DRM_I915_PERF_PROP_CTX_HANDLE:
+			props->single_context = 1;
+			props->ctx_handle = value;
+			break;
+
+		case DRM_I915_PERF_PROP_MAX:
+			BUG();
+		}
+
+		uprop += 2;
+	}
+
+	return 0;
+}
+
+int i915_perf_open_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *file)
+{
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_i915_perf_open_param *param = data;
+	struct perf_open_properties props;
+	u32 known_open_flags = 0;
+	int ret;
+
+	if (!dev_priv->perf.initialized) {
+		DRM_ERROR("i915 perf interface not available for this system");
+		return -ENOTSUPP;
+	}
+
+	known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
+			   I915_PERF_FLAG_FD_NONBLOCK |
+			   I915_PERF_FLAG_DISABLED;
+	if (param->flags & ~known_open_flags) {
+		DRM_ERROR("Unknown drm_i915_perf_open_param flag\n");
+		return -EINVAL;
+	}
+
+	ret = read_properties_unlocked(dev_priv,
+				       u64_to_user_ptr(param->properties_ptr),
+				       param->num_properties,
+				       &props);
+	if (ret)
+		return ret;
+
+	mutex_lock(&dev_priv->perf.lock);
+	ret = i915_perf_open_ioctl_locked(dev, param, &props, file);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return ret;
+}
+
+void i915_perf_init(struct drm_i915_private *dev_priv)
+{
+	INIT_LIST_HEAD(&dev_priv->perf.streams);
+	mutex_init(&dev_priv->perf.lock);
+
+	dev_priv->perf.initialized = true;
+}
+
+void i915_perf_fini(struct drm_i915_private *dev_priv)
+{
+	if (!dev_priv->perf.initialized)
+		return;
+
+	/* Currently nothing to clean up */
+
+	dev_priv->perf.initialized = false;
+}
diff --git a/include/uapi/drm/i915_drm.h b/include/uapi/drm/i915_drm.h
index 03725fe..7777678 100644
--- a/include/uapi/drm/i915_drm.h
+++ b/include/uapi/drm/i915_drm.h
@@ -258,6 +258,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_USERPTR		0x33
 #define DRM_I915_GEM_CONTEXT_GETPARAM	0x34
 #define DRM_I915_GEM_CONTEXT_SETPARAM	0x35
+#define DRM_I915_PERF_OPEN		0x36
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -311,6 +312,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_USERPTR			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR, struct drm_i915_gem_userptr)
 #define DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM	DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_GETPARAM, struct drm_i915_gem_context_param)
 #define DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM	DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_SETPARAM, struct drm_i915_gem_context_param)
+#define DRM_IOCTL_I915_PERF_OPEN	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_PERF_OPEN, struct drm_i915_perf_open_param)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -1222,6 +1224,71 @@ struct drm_i915_gem_context_param {
 	__u64 value;
 };
 
+enum drm_i915_perf_property_id {
+	/**
+	 * Open the stream for a specific context handle (as used with
+	 * execbuffer2). A stream opened for a specific context this way
+	 * won't typically require root privileges.
+	 */
+	DRM_I915_PERF_PROP_CTX_HANDLE = 1,
+
+	DRM_I915_PERF_PROP_MAX /* non-ABI */
+};
+
+struct drm_i915_perf_open_param {
+	__u32 flags;
+#define I915_PERF_FLAG_FD_CLOEXEC	(1<<0)
+#define I915_PERF_FLAG_FD_NONBLOCK	(1<<1)
+#define I915_PERF_FLAG_DISABLED		(1<<2)
+
+	/** The number of u64 (id, value) pairs */
+	__u32 num_properties;
+
+	/**
+	 * Pointer to array of u64 (id, value) pairs configuring the stream
+	 * to open.
+	 */
+	__u64 __user properties_ptr;
+};
+
+#define I915_PERF_IOCTL_ENABLE	_IO('i', 0x0)
+#define I915_PERF_IOCTL_DISABLE	_IO('i', 0x1)
+
+/**
+ * Common to all i915 perf records
+ */
+struct drm_i915_perf_record_header {
+	__u32 type;
+	__u16 pad;
+	__u16 size;
+};
+
+enum drm_i915_perf_record_type {
+
+	/**
+	 * Samples are the work horse record type whose contents are extensible
+	 * and defined when opening an i915 perf stream based on the given
+	 * properties.
+	 *
+	 * Boolean properties following the naming convention
+	 * DRM_I915_PERF_SAMPLE_xyz_PROP request the inclusion of 'xyz' data in
+	 * every sample.
+	 *
+	 * The order of these sample properties given by userspace has no
+	 * affect on the ordering of data within a sample. The order will be
+	 * documented here.
+	 *
+	 * struct {
+	 *     struct drm_i915_perf_record_header header;
+	 *
+	 *     TODO: itemize extensible sample data here
+	 * };
+	 */
+	DRM_I915_PERF_RECORD_SAMPLE = 1,
+
+	DRM_I915_PERF_RECORD_MAX /* non-ABI */
+};
+
 #if defined(__cplusplus)
 }
 #endif
-- 
2.9.2

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 02/11] drm/i915: rename OACONTROL GEN7_OACONTROL
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
  2016-09-14 14:19 ` [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:11   ` Matthew Auld
  2016-09-14 14:19 ` [PATCH v5 03/11] drm/i915: return EACCES for check_cmd() failures Robert Bragg
                   ` (12 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: David Airlie, dri-devel, Sourab Gupta, Daniel Vetter

OACONTROL changes quite a bit for gen8, with some bits split out into a
per-context OACTXCONTROL register. Rename now before adding more gen7 OA
registers

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_cmd_parser.c | 4 ++--
 drivers/gpu/drm/i915/i915_reg.h        | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_cmd_parser.c b/drivers/gpu/drm/i915/i915_cmd_parser.c
index 3c72b3b..7269fe8 100644
--- a/drivers/gpu/drm/i915/i915_cmd_parser.c
+++ b/drivers/gpu/drm/i915/i915_cmd_parser.c
@@ -450,7 +450,7 @@ static const struct drm_i915_reg_descriptor gen7_render_regs[] = {
 	REG64(PS_INVOCATION_COUNT),
 	REG64(PS_DEPTH_COUNT),
 	REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
-	REG32(OACONTROL), /* Only allowed for LRI and SRM. See below. */
+	REG32(GEN7_OACONTROL), /* Only allowed for LRI and SRM. See below. */
 	REG64(MI_PREDICATE_SRC0),
 	REG64(MI_PREDICATE_SRC1),
 	REG32(GEN7_3DPRIM_END_OFFSET),
@@ -1108,7 +1108,7 @@ static bool check_cmd(const struct intel_engine_cs *engine,
 			 * to the register. Hence, limit OACONTROL writes to
 			 * only MI_LOAD_REGISTER_IMM commands.
 			 */
-			if (reg_addr == i915_mmio_reg_offset(OACONTROL)) {
+			if (reg_addr == i915_mmio_reg_offset(GEN7_OACONTROL)) {
 				if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
 					DRM_DEBUG_DRIVER("CMD: Rejected LRM to OACONTROL\n");
 					return false;
diff --git a/drivers/gpu/drm/i915/i915_reg.h b/drivers/gpu/drm/i915/i915_reg.h
index a29d707..90756b2 100644
--- a/drivers/gpu/drm/i915/i915_reg.h
+++ b/drivers/gpu/drm/i915/i915_reg.h
@@ -616,7 +616,7 @@ static inline bool i915_mmio_reg_valid(i915_reg_t reg)
 #define HSW_CS_GPR(n)                   _MMIO(0x2600 + (n) * 8)
 #define HSW_CS_GPR_UDW(n)               _MMIO(0x2600 + (n) * 8 + 4)
 
-#define OACONTROL _MMIO(0x2360)
+#define GEN7_OACONTROL _MMIO(0x2360)
 
 #define _GEN7_PIPEA_DE_LOAD_SL	0x70068
 #define _GEN7_PIPEB_DE_LOAD_SL	0x71068
-- 
2.9.2

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 03/11] drm/i915: return EACCES for check_cmd() failures
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
  2016-09-14 14:19 ` [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure Robert Bragg
  2016-09-14 14:19 ` [PATCH v5 02/11] drm/i915: rename OACONTROL GEN7_OACONTROL Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-09-14 14:19 ` [PATCH v5 04/11] drm/i915: don't whitelist oacontrol in cmd parser Robert Bragg
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

check_cmd() is checking whether a command adheres to certain
restrictions that ensure it's safe to execute within a privileged batch
buffer. Returning false implies a privilege problem, not that the
command is invalid.

The distinction makes the difference between allowing the buffer to be
executed as an unprivileged batch buffer or returning an EINVAL error to
userspace without executing anything.

In a case where userspace may want to test whether it can successfully
write to a register that needs privileges the distinction may be
important and an EINVAL error may be considered fatal.

In particular this is currently true for Mesa, which includes a test for
whether OACONTROL can be written too, but Mesa treats any error when
flushing a batch buffer as fatal, calling exit(1).

As it is currently Mesa can gracefully handle a failure to write to
OACONTROL if the command parser is disabled, but if we were to remove
OACONTROL from the parser's whitelist then the returned EINVAL would
break Mesa applications as they attempt an OACONTROL write.

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_cmd_parser.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/i915/i915_cmd_parser.c b/drivers/gpu/drm/i915/i915_cmd_parser.c
index 7269fe8..5ad02dc 100644
--- a/drivers/gpu/drm/i915/i915_cmd_parser.c
+++ b/drivers/gpu/drm/i915/i915_cmd_parser.c
@@ -1272,7 +1272,7 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
 
 		if (!check_cmd(engine, desc, cmd, length, is_master,
 			       &oacontrol_set)) {
-			ret = -EINVAL;
+			ret = -EACCES;
 			break;
 		}
 
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 04/11] drm/i915: don't whitelist oacontrol in cmd parser
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (2 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 03/11] drm/i915: return EACCES for check_cmd() failures Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-09-14 14:19 ` [PATCH v5 05/11] drm/i915: Add 'render basic' Haswell OA unit config Robert Bragg
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

Being able to program OACONTROL from a non-privileged batch buffer is
not sufficient to be able to configure the OA unit. This was originally
allowed to help enable Mesa to expose OA counters via the
INTEL_performance_query extension, but the current implementation based
on programming OACONTROL via a batch buffer isn't able to report useable
data without a more complete OA unit configuration. Mesa handles the
possibility that writes to OACONTROL may not be allowed and so only
advertises the extension after explicitly testing that a write to
OACONTROL succeeds. Based on this; removing OACONTROL from the whitelist
should be ok for userspace.

Removing this simplifies adding a new kernel api for configuring the OA
unit without needing to consider the possibility that userspace might
trample on OACONTROL state which we'd like to start managing within
the kernel instead. In particular running any Mesa based GL application
currently results in clearing OACONTROL when initializing which would
disable the capturing of metrics.

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_cmd_parser.c | 38 ++--------------------------------
 1 file changed, 2 insertions(+), 36 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_cmd_parser.c b/drivers/gpu/drm/i915/i915_cmd_parser.c
index 5ad02dc..bdee590 100644
--- a/drivers/gpu/drm/i915/i915_cmd_parser.c
+++ b/drivers/gpu/drm/i915/i915_cmd_parser.c
@@ -450,7 +450,6 @@ static const struct drm_i915_reg_descriptor gen7_render_regs[] = {
 	REG64(PS_INVOCATION_COUNT),
 	REG64(PS_DEPTH_COUNT),
 	REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
-	REG32(GEN7_OACONTROL), /* Only allowed for LRI and SRM. See below. */
 	REG64(MI_PREDICATE_SRC0),
 	REG64(MI_PREDICATE_SRC1),
 	REG32(GEN7_3DPRIM_END_OFFSET),
@@ -1060,8 +1059,7 @@ bool intel_engine_needs_cmd_parser(struct intel_engine_cs *engine)
 static bool check_cmd(const struct intel_engine_cs *engine,
 		      const struct drm_i915_cmd_descriptor *desc,
 		      const u32 *cmd, u32 length,
-		      const bool is_master,
-		      bool *oacontrol_set)
+		      const bool is_master)
 {
 	if (desc->flags & CMD_DESC_SKIP)
 		return true;
@@ -1099,31 +1097,6 @@ static bool check_cmd(const struct intel_engine_cs *engine,
 			}
 
 			/*
-			 * OACONTROL requires some special handling for
-			 * writes. We want to make sure that any batch which
-			 * enables OA also disables it before the end of the
-			 * batch. The goal is to prevent one process from
-			 * snooping on the perf data from another process. To do
-			 * that, we need to check the value that will be written
-			 * to the register. Hence, limit OACONTROL writes to
-			 * only MI_LOAD_REGISTER_IMM commands.
-			 */
-			if (reg_addr == i915_mmio_reg_offset(GEN7_OACONTROL)) {
-				if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
-					DRM_DEBUG_DRIVER("CMD: Rejected LRM to OACONTROL\n");
-					return false;
-				}
-
-				if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
-					DRM_DEBUG_DRIVER("CMD: Rejected LRR to OACONTROL\n");
-					return false;
-				}
-
-				if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1))
-					*oacontrol_set = (cmd[offset + 1] != 0);
-			}
-
-			/*
 			 * Check the value written to the register against the
 			 * allowed mask/value pair given in the whitelist entry.
 			 */
@@ -1214,7 +1187,6 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
 	u32 *cmd, *batch_end;
 	struct drm_i915_cmd_descriptor default_desc = noop_desc;
 	const struct drm_i915_cmd_descriptor *desc = &default_desc;
-	bool oacontrol_set = false; /* OACONTROL tracking. See check_cmd() */
 	bool needs_clflush_after = false;
 	int ret = 0;
 
@@ -1270,8 +1242,7 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
 			break;
 		}
 
-		if (!check_cmd(engine, desc, cmd, length, is_master,
-			       &oacontrol_set)) {
+		if (!check_cmd(engine, desc, cmd, length, is_master)) {
 			ret = -EACCES;
 			break;
 		}
@@ -1279,11 +1250,6 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
 		cmd += length;
 	}
 
-	if (oacontrol_set) {
-		DRM_DEBUG_DRIVER("CMD: batch set OACONTROL but did not clear it\n");
-		ret = -EINVAL;
-	}
-
 	if (cmd >= batch_end) {
 		DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
 		ret = -EINVAL;
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 05/11] drm/i915: Add 'render basic' Haswell OA unit config
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (3 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 04/11] drm/i915: don't whitelist oacontrol in cmd parser Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:15   ` Matthew Auld
  2016-09-14 14:19 ` [PATCH v5 06/11] drm/i915: Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (9 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

Adds a static OA unit, MUX + B Counter configuration for basic render
metrics on Haswell. This is auto generated from an XML
description of metric sets, currently maintained in gputop, ref:

  https://github.com/rib/gputop
  > gputop-data/oa-*.xml
  > scripts/i915-perf-kernelgen.py

  $ make -C gputop-data -f Makefile.xml SYSFS=0 WHITELIST=RenderBasic

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/Makefile      |   3 +-
 drivers/gpu/drm/i915/i915_drv.h    |  14 ++++
 drivers/gpu/drm/i915/i915_oa_hsw.c | 143 +++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/i915_oa_hsw.h |  34 +++++++++
 4 files changed, 193 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/i915/i915_oa_hsw.c
 create mode 100644 drivers/gpu/drm/i915/i915_oa_hsw.h

diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
index d991781..6cb25dd 100644
--- a/drivers/gpu/drm/i915/Makefile
+++ b/drivers/gpu/drm/i915/Makefile
@@ -111,7 +111,8 @@ i915-y += dvo_ch7017.o \
 i915-y += i915_vgpu.o
 
 # perf code
-i915-y += i915_perf.o
+i915-y += i915_perf.o \
+	  i915_oa_hsw.o
 
 ifeq ($(CONFIG_DRM_I915_GVT),y)
 i915-y += intel_gvt.o
diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index 0f5cd8f..5fad018 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -1740,6 +1740,11 @@ struct intel_wm_config {
 	bool sprites_scaled;
 };
 
+struct i915_oa_reg {
+	i915_reg_t addr;
+	u32 value;
+};
+
 struct i915_perf_stream;
 
 struct i915_perf_stream_ops {
@@ -2122,6 +2127,15 @@ struct drm_i915_private {
 		bool initialized;
 		struct mutex lock;
 		struct list_head streams;
+
+		struct {
+			u32 metrics_set;
+
+			const struct i915_oa_reg *mux_regs;
+			int mux_regs_len;
+			const struct i915_oa_reg *b_counter_regs;
+			int b_counter_regs_len;
+		} oa;
 	} perf;
 
 	/* Abstract the submission mechanism (legacy ringbuffer or execlists) away */
diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.c b/drivers/gpu/drm/i915/i915_oa_hsw.c
new file mode 100644
index 0000000..eb5ceca
--- /dev/null
+++ b/drivers/gpu/drm/i915/i915_oa_hsw.c
@@ -0,0 +1,143 @@
+/*
+ * Autogenerated file, DO NOT EDIT manually!
+ *
+ * Copyright (c) 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include "i915_drv.h"
+
+enum metric_set_id {
+	METRIC_SET_ID_RENDER_BASIC = 1,
+};
+
+int i915_oa_n_builtin_metric_sets_hsw = 1;
+
+static const struct i915_oa_reg b_counter_config_render_basic[] = {
+	{ _MMIO(0x2724), 0x00800000 },
+	{ _MMIO(0x2720), 0x00000000 },
+	{ _MMIO(0x2714), 0x00800000 },
+	{ _MMIO(0x2710), 0x00000000 },
+};
+
+static const struct i915_oa_reg mux_config_render_basic[] = {
+	{ _MMIO(0x253a4), 0x01600000 },
+	{ _MMIO(0x25440), 0x00100000 },
+	{ _MMIO(0x25128), 0x00000000 },
+	{ _MMIO(0x2691c), 0x00000800 },
+	{ _MMIO(0x26aa0), 0x01500000 },
+	{ _MMIO(0x26b9c), 0x00006000 },
+	{ _MMIO(0x2791c), 0x00000800 },
+	{ _MMIO(0x27aa0), 0x01500000 },
+	{ _MMIO(0x27b9c), 0x00006000 },
+	{ _MMIO(0x2641c), 0x00000400 },
+	{ _MMIO(0x25380), 0x00000010 },
+	{ _MMIO(0x2538c), 0x00000000 },
+	{ _MMIO(0x25384), 0x0800aaaa },
+	{ _MMIO(0x25400), 0x00000004 },
+	{ _MMIO(0x2540c), 0x06029000 },
+	{ _MMIO(0x25410), 0x00000002 },
+	{ _MMIO(0x25404), 0x5c30ffff },
+	{ _MMIO(0x25100), 0x00000016 },
+	{ _MMIO(0x25110), 0x00000400 },
+	{ _MMIO(0x25104), 0x00000000 },
+	{ _MMIO(0x26804), 0x00001211 },
+	{ _MMIO(0x26884), 0x00000100 },
+	{ _MMIO(0x26900), 0x00000002 },
+	{ _MMIO(0x26908), 0x00700000 },
+	{ _MMIO(0x26904), 0x00000000 },
+	{ _MMIO(0x26984), 0x00001022 },
+	{ _MMIO(0x26a04), 0x00000011 },
+	{ _MMIO(0x26a80), 0x00000006 },
+	{ _MMIO(0x26a88), 0x00000c02 },
+	{ _MMIO(0x26a84), 0x00000000 },
+	{ _MMIO(0x26b04), 0x00001000 },
+	{ _MMIO(0x26b80), 0x00000002 },
+	{ _MMIO(0x26b8c), 0x00000007 },
+	{ _MMIO(0x26b84), 0x00000000 },
+	{ _MMIO(0x27804), 0x00004844 },
+	{ _MMIO(0x27884), 0x00000400 },
+	{ _MMIO(0x27900), 0x00000002 },
+	{ _MMIO(0x27908), 0x0e000000 },
+	{ _MMIO(0x27904), 0x00000000 },
+	{ _MMIO(0x27984), 0x00004088 },
+	{ _MMIO(0x27a04), 0x00000044 },
+	{ _MMIO(0x27a80), 0x00000006 },
+	{ _MMIO(0x27a88), 0x00018040 },
+	{ _MMIO(0x27a84), 0x00000000 },
+	{ _MMIO(0x27b04), 0x00004000 },
+	{ _MMIO(0x27b80), 0x00000002 },
+	{ _MMIO(0x27b8c), 0x000000e0 },
+	{ _MMIO(0x27b84), 0x00000000 },
+	{ _MMIO(0x26104), 0x00002222 },
+	{ _MMIO(0x26184), 0x0c006666 },
+	{ _MMIO(0x26284), 0x04000000 },
+	{ _MMIO(0x26304), 0x04000000 },
+	{ _MMIO(0x26400), 0x00000002 },
+	{ _MMIO(0x26410), 0x000000a0 },
+	{ _MMIO(0x26404), 0x00000000 },
+	{ _MMIO(0x25420), 0x04108020 },
+	{ _MMIO(0x25424), 0x1284a420 },
+	{ _MMIO(0x2541c), 0x00000000 },
+	{ _MMIO(0x25428), 0x00042049 },
+};
+
+static const struct i915_oa_reg *
+get_render_basic_mux_config(struct drm_i915_private *dev_priv,
+			    int *len)
+{
+	*len = ARRAY_SIZE(mux_config_render_basic);
+	return mux_config_render_basic;
+}
+
+int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv)
+{
+	dev_priv->perf.oa.mux_regs = NULL;
+	dev_priv->perf.oa.mux_regs_len = 0;
+	dev_priv->perf.oa.b_counter_regs = NULL;
+	dev_priv->perf.oa.b_counter_regs_len = 0;
+
+	switch (dev_priv->perf.oa.metrics_set) {
+	case METRIC_SET_ID_RENDER_BASIC:
+		dev_priv->perf.oa.mux_regs =
+			get_render_basic_mux_config(dev_priv,
+						    &dev_priv->perf.oa.mux_regs_len);
+		if (!dev_priv->perf.oa.mux_regs) {
+			DRM_DEBUG_DRIVER("No suitable MUX config for \"RENDER_BASIC\" metric set");
+
+			/* EINVAL because *_register_sysfs already checked this
+			 * and so it wouldn't have been advertised so userspace and
+			 * so shouldn't have been requested
+			 */
+			return -EINVAL;
+		}
+
+		dev_priv->perf.oa.b_counter_regs =
+			b_counter_config_render_basic;
+		dev_priv->perf.oa.b_counter_regs_len =
+			ARRAY_SIZE(b_counter_config_render_basic);
+
+		return 0;
+	default:
+		return -ENODEV;
+	}
+}
diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.h b/drivers/gpu/drm/i915/i915_oa_hsw.h
new file mode 100644
index 0000000..b618a1f
--- /dev/null
+++ b/drivers/gpu/drm/i915/i915_oa_hsw.h
@@ -0,0 +1,34 @@
+/*
+ * Autogenerated file, DO NOT EDIT manually!
+ *
+ * Copyright (c) 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __I915_OA_HSW_H__
+#define __I915_OA_HSW_H__
+
+extern int i915_oa_n_builtin_metric_sets_hsw;
+
+extern int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv);
+
+#endif
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 06/11] drm/i915: Enable i915 perf stream for Haswell OA unit
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (4 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 05/11] drm/i915: Add 'render basic' Haswell OA unit config Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:19   ` Matthew Auld
  2016-09-14 14:19 ` [PATCH v5 07/11] drm/i915: advertise available metrics via sysfs Robert Bragg
                   ` (8 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

Gen graphics hardware can be set up to periodically write snapshots of
performance counters into a circular buffer via its Observation
Architecture and this patch exposes that capability to userspace via the
i915 perf interface.

Cc: Chris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: Robert Bragg <robert@sixbynine.org>
Signed-off-by: Zhenyu Wang <zhenyuw@linux.intel.com>
---
 drivers/gpu/drm/i915/i915_drv.h         |  72 ++-
 drivers/gpu/drm/i915/i915_gem_context.c |  22 +-
 drivers/gpu/drm/i915/i915_perf.c        | 998 +++++++++++++++++++++++++++++++-
 drivers/gpu/drm/i915/i915_reg.h         | 338 +++++++++++
 drivers/gpu/drm/i915/intel_ringbuffer.c |  10 +-
 include/uapi/drm/i915_drm.h             |  70 ++-
 6 files changed, 1477 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index 5fad018..551f078 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -1740,6 +1740,11 @@ struct intel_wm_config {
 	bool sprites_scaled;
 };
 
+struct i915_oa_format {
+	u32 format;
+	int size;
+};
+
 struct i915_oa_reg {
 	i915_reg_t addr;
 	u32 value;
@@ -1760,11 +1765,6 @@ struct i915_perf_stream_ops {
 	 */
 	void (*disable)(struct i915_perf_stream *stream);
 
-	/* Return: true if any i915 perf records are ready to read()
-	 * for this stream.
-	 */
-	bool (*can_read)(struct i915_perf_stream *stream);
-
 	/* Call poll_wait, passing a wait queue that will be woken
 	 * once there is something ready to read() for the stream
 	 */
@@ -1774,9 +1774,7 @@ struct i915_perf_stream_ops {
 
 	/* For handling a blocking read, wait until there is something
 	 * to ready to read() for the stream. E.g. wait on the same
-	 * wait queue that would be passed to poll_wait() until
-	 * ->can_read() returns true (if its safe to call ->can_read()
-	 * without the i915 perf lock held).
+	 * wait queue that would be passed to poll_wait().
 	 */
 	int (*wait_unlocked)(struct i915_perf_stream *stream);
 
@@ -1816,11 +1814,28 @@ struct i915_perf_stream {
 	struct list_head link;
 
 	u32 sample_flags;
+	int sample_size;
 
 	struct i915_gem_context *ctx;
 	bool enabled;
 
-	struct i915_perf_stream_ops *ops;
+	const struct i915_perf_stream_ops *ops;
+};
+
+struct i915_oa_ops {
+	void (*init_oa_buffer)(struct drm_i915_private *dev_priv);
+	int (*enable_metric_set)(struct drm_i915_private *dev_priv);
+	void (*disable_metric_set)(struct drm_i915_private *dev_priv);
+	void (*oa_enable)(struct drm_i915_private *dev_priv);
+	void (*oa_disable)(struct drm_i915_private *dev_priv);
+	void (*update_oacontrol)(struct drm_i915_private *dev_priv);
+	void (*update_hw_ctx_id_locked)(struct drm_i915_private *dev_priv,
+					u32 ctx_id);
+	int (*read)(struct i915_perf_stream *stream,
+		    char __user *buf,
+		    size_t count,
+		    size_t *offset);
+	bool (*oa_buffer_is_empty)(struct drm_i915_private *dev_priv);
 };
 
 struct drm_i915_private {
@@ -2125,16 +2140,48 @@ struct drm_i915_private {
 
 	struct {
 		bool initialized;
+
 		struct mutex lock;
 		struct list_head streams;
 
+		spinlock_t hook_lock;
+
 		struct {
-			u32 metrics_set;
+			struct i915_perf_stream *exclusive_stream;
+
+			u32 specific_ctx_id;
+
+			struct hrtimer poll_check_timer;
+			wait_queue_head_t poll_wq;
+			atomic_t pollin;
+
+			bool periodic;
+			int period_exponent;
+			int timestamp_frequency;
+
+			int tail_margin;
+
+			int metrics_set;
 
 			const struct i915_oa_reg *mux_regs;
 			int mux_regs_len;
 			const struct i915_oa_reg *b_counter_regs;
 			int b_counter_regs_len;
+
+			struct {
+				struct drm_i915_gem_object *obj;
+				struct i915_vma *vma;
+				u32 gtt_offset;
+				u8 *addr;
+				int format;
+				int format_size;
+			} oa_buffer;
+
+			u32 gen7_latched_oastatus1;
+
+			struct i915_oa_ops ops;
+			const struct i915_oa_format *oa_formats;
+			int n_builtin_sets;
 		} oa;
 	} perf;
 
@@ -3499,6 +3546,9 @@ struct drm_i915_gem_object *
 i915_gem_alloc_context_obj(struct drm_device *dev, size_t size);
 struct i915_gem_context *
 i915_gem_context_create_gvt(struct drm_device *dev);
+int i915_gem_context_pin_legacy_rcs_state(struct drm_i915_private *dev_priv,
+					  struct i915_gem_context *ctx,
+					  u64 flags);
 
 static inline struct i915_gem_context *
 i915_gem_context_lookup(struct drm_i915_file_private *file_priv, u32 id)
@@ -3545,6 +3595,8 @@ int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
 
 int i915_perf_open_ioctl(struct drm_device *dev, void *data,
 			 struct drm_file *file);
+void i915_oa_legacy_context_pin_notify(struct drm_i915_private *dev_priv,
+				       struct i915_gem_context *ctx);
 
 /* i915_gem_evict.c */
 int __must_check i915_gem_evict_something(struct i915_address_space *vm,
diff --git a/drivers/gpu/drm/i915/i915_gem_context.c b/drivers/gpu/drm/i915/i915_gem_context.c
index df10f4e9..62966e9 100644
--- a/drivers/gpu/drm/i915/i915_gem_context.c
+++ b/drivers/gpu/drm/i915/i915_gem_context.c
@@ -134,6 +134,25 @@ static int get_context_size(struct drm_i915_private *dev_priv)
 	return ret;
 }
 
+int i915_gem_context_pin_legacy_rcs_state(struct drm_i915_private *dev_priv,
+					  struct i915_gem_context *ctx,
+					  u64 flags)
+{
+	int ret;
+
+	lockdep_assert_held(&ctx->i915->drm.struct_mutex);
+
+	ret = i915_vma_pin(ctx->engine[RCS].state, 0,
+			   ctx->ggtt_alignment,
+			   flags);
+	if (ret)
+		return ret;
+
+	i915_oa_legacy_context_pin_notify(dev_priv, ctx);
+
+	return 0;
+}
+
 void i915_gem_context_free(struct kref *ctx_ref)
 {
 	struct i915_gem_context *ctx = container_of(ctx_ref, typeof(*ctx), ref);
@@ -770,7 +789,8 @@ static int do_rcs_switch(struct drm_i915_gem_request *req)
 	}
 
 	/* Trying to pin first makes error handling easier. */
-	ret = i915_vma_pin(vma, 0, to->ggtt_alignment, PIN_GLOBAL);
+	ret = i915_gem_context_pin_legacy_rcs_state(engine->i915, to,
+						    PIN_GLOBAL);
 	if (ret)
 		return ret;
 
diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
index 87530f5..5305982 100644
--- a/drivers/gpu/drm/i915/i915_perf.c
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -28,14 +28,860 @@
 #include <linux/sizes.h>
 
 #include "i915_drv.h"
+#include "intel_ringbuffer.h"
+#include "intel_lrc.h"
+#include "i915_oa_hsw.h"
+
+/* Must be a power of two */
+#define OA_BUFFER_SIZE		SZ_16M
+#define OA_TAKEN(tail, head)	((tail - head) & (OA_BUFFER_SIZE - 1))
+
+/* There's a HW race condition between OA unit tail pointer register updates and
+ * writes to memory whereby the tail pointer can sometimes get ahead of what's
+ * been written out to the OA buffer so far.
+ *
+ * Although this can be observed explicitly by checking for a zeroed report-id
+ * field in tail reports, it seems preferable to account for this earlier e.g.
+ * as part of the _oa_buffer_is_empty checks to minimize -EAGAIN polling cycles
+ * in this situation.
+ *
+ * To give time for the most recent reports to land before they may be copied to
+ * userspace, the driver operates as if the tail pointer effectively lags behind
+ * the HW tail pointer by 'tail_margin' bytes. The margin in bytes is calculated
+ * based on this constant in nanoseconds, the current OA sampling exponent
+ * and current report size.
+ *
+ * There is also a fallback check while reading to simply skip over reports with
+ * a zeroed report-id.
+ */
+#define OA_TAIL_MARGIN_NSEC	100000ULL
+
+/* frequency for checking whether the OA unit has written new reports to the
+ * circular OA buffer...
+ */
+#define POLL_FREQUENCY 200
+#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
+
+/* The maximum exponent the hardware accepts is 63 (essentially it selects one
+ * of the 64bit timestamp bits to trigger reports from) but there's currently
+ * no known use case for sampling as infrequently as once per 47 thousand years.
+ *
+ * Since the timestamps included in OA reports are only 32bits it seems
+ * reasonable to limit the OA exponent where it's still possible to account for
+ * overflow in OA report timestamps.
+ */
+#define OA_EXPONENT_MAX 31
+
+/* XXX: beware if future OA HW adds new report formats that the current
+ * code assumes all reports have a power-of-two size and ~(size - 1) can
+ * be used as a mask to align the OA tail pointer.
+ */
+static struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
+	[I915_OA_FORMAT_A13]	    = { 0, 64 },
+	[I915_OA_FORMAT_A29]	    = { 1, 128 },
+	[I915_OA_FORMAT_A13_B8_C8]  = { 2, 128 },
+	/* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
+	[I915_OA_FORMAT_B4_C8]	    = { 4, 64 },
+	[I915_OA_FORMAT_A45_B8_C8]  = { 5, 256 },
+	[I915_OA_FORMAT_B4_C8_A16]  = { 6, 128 },
+	[I915_OA_FORMAT_C4_B8]	    = { 7, 64 },
+};
+
+#define SAMPLE_OA_REPORT      (1<<0)
 
 struct perf_open_properties {
 	u32 sample_flags;
 
 	u64 single_context:1;
 	u64 ctx_handle;
+
+	/* OA sampling state */
+	int metrics_set;
+	int oa_format;
+	bool oa_periodic;
+	int oa_period_exponent;
 };
 
+/* NB: This is either called via fops or the poll check hrtimer (atomic ctx)
+ *
+ * It's safe to read OA config state here unlocked, assuming that this is only
+ * called while the stream is enabled, while the global OA configuration can't
+ * be modified.
+ *
+ * Note: we don't lock around the head/tail reads even though there's the slim
+ * possibility of read() fop errors forcing a re-init of the OA buffer
+ * pointers.  A race here could result in a false positive !empty status which
+ * is acceptable.
+ */
+static bool gen7_oa_buffer_is_empty_fop_unlocked(struct drm_i915_private *dev_priv)
+{
+	int report_size = dev_priv->perf.oa.oa_buffer.format_size;
+	u32 oastatus2 = I915_READ(GEN7_OASTATUS2);
+	u32 oastatus1 = I915_READ(GEN7_OASTATUS1);
+	u32 head = oastatus2 & GEN7_OASTATUS2_HEAD_MASK;
+	u32 tail = oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
+
+	return OA_TAKEN(tail, head) <
+		dev_priv->perf.oa.tail_margin + report_size;
+}
+
+/**
+ * Appends a status record to a userspace read() buffer.
+ */
+static int append_oa_status(struct i915_perf_stream *stream,
+			    char __user *buf,
+			    size_t count,
+			    size_t *offset,
+			    enum drm_i915_perf_record_type type)
+{
+	struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
+
+	if ((count - *offset) < header.size)
+		return -ENOSPC;
+
+	if (copy_to_user(buf + *offset, &header, sizeof(header)))
+		return -EFAULT;
+
+	(*offset) += header.size;
+
+	return 0;
+}
+
+/**
+ * Copies single OA report into userspace read() buffer.
+ */
+static int append_oa_sample(struct i915_perf_stream *stream,
+			    char __user *buf,
+			    size_t count,
+			    size_t *offset,
+			    const u8 *report)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	int report_size = dev_priv->perf.oa.oa_buffer.format_size;
+	struct drm_i915_perf_record_header header;
+	u32 sample_flags = stream->sample_flags;
+
+	header.type = DRM_I915_PERF_RECORD_SAMPLE;
+	header.pad = 0;
+	header.size = stream->sample_size;
+
+	if ((count - *offset) < header.size)
+		return -ENOSPC;
+
+	buf += *offset;
+	if (copy_to_user(buf, &header, sizeof(header)))
+		return -EFAULT;
+	buf += sizeof(header);
+
+	if (sample_flags & SAMPLE_OA_REPORT) {
+		if (copy_to_user(buf, report, report_size))
+			return -EFAULT;
+	}
+
+	(*offset) += header.size;
+
+	return 0;
+}
+
+/**
+ * Copies all buffered OA reports into userspace read() buffer.
+ * @head_ptr: (inout): the head pointer before and after appending
+ *
+ * Returns 0 on success, negative error code on failure.
+ *
+ * Notably any error condition resulting in a short read (-ENOSPC or
+ * -EFAULT) will be returned even though one or more records may
+ * have been successfully copied. In this case it's up to the caller
+ * to decide if the error should be squashed before returning to
+ * userspace.
+ */
+static int gen7_append_oa_reports(struct i915_perf_stream *stream,
+				  char __user *buf,
+				  size_t count,
+				  size_t *offset,
+				  u32 *head_ptr,
+				  u32 tail)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	int report_size = dev_priv->perf.oa.oa_buffer.format_size;
+	u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.addr;
+	int tail_margin = dev_priv->perf.oa.tail_margin;
+	u32 mask = (OA_BUFFER_SIZE - 1);
+	u32 head;
+	u32 taken;
+	int ret = 0;
+
+	BUG_ON(!stream->enabled);
+
+	head = *head_ptr - dev_priv->perf.oa.oa_buffer.gtt_offset;
+	tail -= dev_priv->perf.oa.oa_buffer.gtt_offset;
+
+	/* The OA unit is expected to wrap the tail pointer according to the OA
+	 * buffer size and since we should never write a misaligned head
+	 * pointer we don't expect to read one back either...
+	 */
+	if (tail > OA_BUFFER_SIZE || head > OA_BUFFER_SIZE ||
+	    head % report_size) {
+		DRM_ERROR("Inconsistent OA buffer pointer (head = %u, tail = %u): force restart",
+			  head, tail);
+		dev_priv->perf.oa.ops.oa_disable(dev_priv);
+		dev_priv->perf.oa.ops.oa_enable(dev_priv);
+		*head_ptr = I915_READ(GEN7_OASTATUS2) &
+			GEN7_OASTATUS2_HEAD_MASK;
+		return -EIO;
+	}
+
+
+	/* The tail pointer increases in 64 byte increments, not in report_size
+	 * steps...
+	 */
+	tail &= ~(report_size - 1);
+
+	/* Move the tail pointer back by the current tail_margin to account for
+	 * the possibility that the latest reports may not have really landed
+	 * in memory yet...
+	 */
+
+	if (OA_TAKEN(tail, head) < report_size + tail_margin)
+		return -EAGAIN;
+
+	tail -= tail_margin;
+	tail &= mask;
+
+	for (/* none */;
+	     (taken = OA_TAKEN(tail, head));
+	     head = (head + report_size) & mask) {
+		u8 *report = oa_buf_base + head;
+		u32 *report32 = (void *)report;
+
+		/* All the report sizes factor neatly into the buffer
+		 * size so we never expect to see a report split
+		 * between the beginning and end of the buffer.
+		 *
+		 * Given the initial alignment check a misalignment
+		 * here would imply a driver bug that would result
+		 * in an overrun.
+		 */
+		BUG_ON((OA_BUFFER_SIZE - head) < report_size);
+
+		/* The report-ID field for periodic samples includes
+		 * some undocumented flags related to what triggered
+		 * the report and is never expected to be zero so we
+		 * can check that the report isn't invalid before
+		 * copying it to userspace...
+		 */
+		if (report32[0] == 0) {
+			DRM_ERROR("Skipping spurious, invalid OA report\n");
+			continue;
+		}
+
+		ret = append_oa_sample(stream, buf, count, offset, report);
+		if (ret)
+			break;
+
+		/* The above report-id field sanity check is based on
+		 * the assumption that the OA buffer is initially
+		 * zeroed and we reset the field after copying so the
+		 * check is still meaningful once old reports start
+		 * being overwritten.
+		 */
+		report32[0] = 0;
+	}
+
+	*head_ptr = dev_priv->perf.oa.oa_buffer.gtt_offset + head;
+
+	return ret;
+}
+
+static int gen7_oa_read(struct i915_perf_stream *stream,
+			char __user *buf,
+			size_t count,
+			size_t *offset)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	int report_size = dev_priv->perf.oa.oa_buffer.format_size;
+	u32 oastatus2;
+	u32 oastatus1;
+	u32 head;
+	u32 tail;
+	int ret;
+
+	BUG_ON(!dev_priv->perf.oa.oa_buffer.addr);
+
+	oastatus2 = I915_READ(GEN7_OASTATUS2);
+	oastatus1 = I915_READ(GEN7_OASTATUS1);
+
+	head = oastatus2 & GEN7_OASTATUS2_HEAD_MASK;
+	tail = oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
+
+	/* XXX: On Haswell we don't have a safe way to clear oastatus1
+	 * bits while the OA unit is enabled (while the tail pointer
+	 * may be updated asynchronously) so we ignore status bits
+	 * that have already been reported to userspace.
+	 */
+	oastatus1 &= ~dev_priv->perf.oa.gen7_latched_oastatus1;
+
+	/* We treat OABUFFER_OVERFLOW as a significant error:
+	 *
+	 * - The status can be interpreted to mean that the buffer is
+	 *   currently full (with a higher precedence than OA_TAKEN()
+	 *   which will start to report a near-empty buffer after an
+	 *   overflow) but it's awkward that we can't clear the status
+	 *   on Haswell, so without a reset we won't be able to catch
+	 *   the state again.
+	 *
+	 * - Since it also implies the HW has started overwriting old
+	 *   reports it may also affect our sanity checks for invalid
+	 *   reports when copying to userspace that assume new reports
+	 *   are being written to cleared memory.
+	 *
+	 * - In the future we may want to introduce a flight recorder
+	 *   mode where the driver will automatically maintain a safe
+	 *   guard band between head/tail, avoiding this overflow
+	 *   condition, but we avoid the added driver complexity for
+	 *   now.
+	 */
+	if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
+		ret = append_oa_status(stream, buf, count, offset,
+				       DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
+		if (ret)
+			return ret;
+
+		DRM_ERROR("OA buffer overflow: force restart");
+
+		dev_priv->perf.oa.ops.oa_disable(dev_priv);
+		dev_priv->perf.oa.ops.oa_enable(dev_priv);
+
+		oastatus2 = I915_READ(GEN7_OASTATUS2);
+		oastatus1 = I915_READ(GEN7_OASTATUS1);
+
+		head = oastatus2 & GEN7_OASTATUS2_HEAD_MASK;
+		tail = oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
+	}
+
+	if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
+		ret = append_oa_status(stream, buf, count, offset,
+				       DRM_I915_PERF_RECORD_OA_REPORT_LOST);
+		if (ret)
+			return ret;
+		dev_priv->perf.oa.gen7_latched_oastatus1 |=
+			GEN7_OASTATUS1_REPORT_LOST;
+	}
+
+	ret = gen7_append_oa_reports(stream, buf, count, offset,
+				     &head, tail);
+
+	/* All the report sizes are a power of two and the
+	 * head should always be incremented by some multiple
+	 * of the report size.
+	 *
+	 * A warning here, but notably if we later read back a
+	 * misaligned pointer we will treat that as a bug since
+	 * it could lead to a buffer overrun.
+	 */
+	WARN_ONCE(head & (report_size - 1),
+		  "i915: Writing misaligned OA head pointer");
+
+	/* Note: we update the head pointer here even if an error
+	 * was returned since the error may represent a short read
+	 * where some some reports were successfully copied.
+	 */
+	I915_WRITE(GEN7_OASTATUS2,
+		   ((head & GEN7_OASTATUS2_HEAD_MASK) |
+		    OA_MEM_SELECT_GGTT));
+
+	return ret;
+}
+
+static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	/* We would wait indefinitly if periodic sampling is not enabled */
+	if (!dev_priv->perf.oa.periodic)
+		return -EIO;
+
+	/* Note: the oa_buffer_is_empty() condition is ok to run unlocked as it
+	 * just performs mmio reads of the OA buffer head + tail pointers and
+	 * it's assumed we're handling some operation that implies the stream
+	 * can't be destroyed until completion (such as a read()) that ensures
+	 * the device + OA buffer can't disappear
+	 */
+	return wait_event_interruptible(dev_priv->perf.oa.poll_wq,
+					!dev_priv->perf.oa.ops.oa_buffer_is_empty(dev_priv));
+}
+
+static void i915_oa_poll_wait(struct i915_perf_stream *stream,
+			      struct file *file,
+			      poll_table *wait)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	poll_wait(file, &dev_priv->perf.oa.poll_wq, wait);
+}
+
+static int i915_oa_read(struct i915_perf_stream *stream,
+			char __user *buf,
+			size_t count,
+			size_t *offset)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	return dev_priv->perf.oa.ops.read(stream, buf, count, offset);
+}
+
+static void
+free_oa_buffer(struct drm_i915_private *i915)
+{
+	mutex_lock(&i915->drm.struct_mutex);
+
+	i915_gem_object_unpin_map(i915->perf.oa.oa_buffer.obj);
+	__i915_vma_unpin(i915->perf.oa.oa_buffer.vma);
+	i915_gem_object_put(i915->perf.oa.oa_buffer.obj);
+
+	i915->perf.oa.oa_buffer.obj = NULL;
+	i915->perf.oa.oa_buffer.gtt_offset = 0;
+	i915->perf.oa.oa_buffer.addr = NULL;
+
+	mutex_unlock(&i915->drm.struct_mutex);
+}
+
+static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	BUG_ON(stream != dev_priv->perf.oa.exclusive_stream);
+
+	dev_priv->perf.oa.ops.disable_metric_set(dev_priv);
+
+	free_oa_buffer(dev_priv);
+
+	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
+	intel_runtime_pm_put(dev_priv);
+
+	dev_priv->perf.oa.exclusive_stream = NULL;
+}
+
+static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv)
+{
+	/* Pre-DevBDW: OABUFFER must be set with counters off,
+	 * before OASTATUS1, but after OASTATUS2
+	 */
+	I915_WRITE(GEN7_OASTATUS2, dev_priv->perf.oa.oa_buffer.gtt_offset |
+		   OA_MEM_SELECT_GGTT); /* head */
+	I915_WRITE(GEN7_OABUFFER, dev_priv->perf.oa.oa_buffer.gtt_offset);
+	I915_WRITE(GEN7_OASTATUS1, dev_priv->perf.oa.oa_buffer.gtt_offset |
+		   OABUFFER_SIZE_16M); /* tail */
+
+	/* On Haswell we have to track which OASTATUS1 flags we've
+	 * already seen since they can't be cleared while periodic
+	 * sampling is enabled.
+	 */
+	dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
+
+	/* NB: although the OA buffer will initially be allocated
+	 * zeroed via shmfs (and so this memset is redundant when
+	 * first allocating), we may re-init the OA buffer, either
+	 * when re-enabling a stream or in error/reset paths.
+	 *
+	 * The reason we clear the buffer for each re-init is for the
+	 * sanity check in gen7_append_oa_reports() that looks at the
+	 * report-id field to make sure it's non-zero which relies on
+	 * the assumption that new reports are being written to zeroed
+	 * memory...
+	 */
+	memset(dev_priv->perf.oa.oa_buffer.addr, 0, SZ_16M);
+
+	/* Maybe make ->pollin per-stream state if we support multiple
+	 * concurrent streams in the future. */
+	atomic_set(&dev_priv->perf.oa.pollin, false);
+}
+
+static int alloc_oa_buffer(struct drm_i915_private *dev_priv)
+{
+	struct drm_i915_gem_object *bo;
+	enum i915_map_type map;
+	struct i915_vma *vma;
+	int ret;
+
+	BUG_ON(dev_priv->perf.oa.oa_buffer.obj);
+
+	ret = i915_mutex_lock_interruptible(&dev_priv->drm);
+	if (ret)
+		return ret;
+
+	bo = i915_gem_object_create(&dev_priv->drm, OA_BUFFER_SIZE);
+	if (IS_ERR(bo)) {
+		DRM_ERROR("Failed to allocate OA buffer\n");
+		ret = PTR_ERR(bo);
+		goto unlock;
+	}
+	dev_priv->perf.oa.oa_buffer.obj = bo;
+
+	ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC);
+	if (ret)
+		goto err_unref;
+
+	/* PreHSW required 512K alignment, HSW requires 16M */
+	vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, PIN_MAPPABLE);
+	if (IS_ERR(vma)) {
+		ret = PTR_ERR(vma);
+		goto err_unref;
+	}
+	dev_priv->perf.oa.oa_buffer.vma = vma;
+
+	map = HAS_LLC(dev_priv) ? I915_MAP_WB : I915_MAP_WC;
+
+	dev_priv->perf.oa.oa_buffer.gtt_offset = i915_ggtt_offset(vma);
+	dev_priv->perf.oa.oa_buffer.addr = i915_gem_object_pin_map(bo, map);
+	if (dev_priv->perf.oa.oa_buffer.addr == NULL)
+		goto err_unpin;
+
+	dev_priv->perf.oa.ops.init_oa_buffer(dev_priv);
+
+	DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p",
+			 dev_priv->perf.oa.oa_buffer.gtt_offset,
+			 dev_priv->perf.oa.oa_buffer.addr);
+
+	goto unlock;
+
+err_unpin:
+	__i915_vma_unpin(vma);
+
+err_unref:
+	i915_gem_object_put(bo);
+
+	dev_priv->perf.oa.oa_buffer.gtt_offset = 0;
+	dev_priv->perf.oa.oa_buffer.addr = NULL;
+	dev_priv->perf.oa.oa_buffer.vma = NULL;
+	dev_priv->perf.oa.oa_buffer.obj = NULL;
+
+unlock:
+	mutex_unlock(&dev_priv->drm.struct_mutex);
+	return ret;
+}
+
+static void config_oa_regs(struct drm_i915_private *dev_priv,
+			   const struct i915_oa_reg *regs,
+			   int n_regs)
+{
+	int i;
+
+	for (i = 0; i < n_regs; i++) {
+		const struct i915_oa_reg *reg = regs + i;
+
+		I915_WRITE(reg->addr, reg->value);
+	}
+}
+
+static int hsw_enable_metric_set(struct drm_i915_private *dev_priv)
+{
+	int ret = i915_oa_select_metric_set_hsw(dev_priv);
+
+	if (ret)
+		return ret;
+
+	I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) |
+				      GT_NOA_ENABLE));
+
+	/* PRM:
+	 *
+	 * OA unit is using “crclk” for its functionality. When trunk
+	 * level clock gating takes place, OA clock would be gated,
+	 * unable to count the events from non-render clock domain.
+	 * Render clock gating must be disabled when OA is enabled to
+	 * count the events from non-render domain. Unit level clock
+	 * gating for RCS should also be disabled.
+	 */
+	I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
+				    ~GEN7_DOP_CLOCK_GATE_ENABLE));
+	I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) |
+				  GEN6_CSUNIT_CLOCK_GATE_DISABLE));
+
+	config_oa_regs(dev_priv, dev_priv->perf.oa.mux_regs,
+		       dev_priv->perf.oa.mux_regs_len);
+
+	/* It apparently takes a fairly long time for a new MUX
+	 * configuration to be be applied after these register writes.
+	 * This delay duration was derived empirically based on the
+	 * render_basic config but hopefully it covers the maximum
+	 * configuration latency.
+	 *
+	 * As a fallback, the checks in _append_oa_reports() to skip
+	 * invalid OA reports do also seem to work to discard reports
+	 * generated before this config has completed - albeit not
+	 * silently.
+	 *
+	 * Unfortunately this is essentially a magic number, since we
+	 * don't currently know of a reliable mechanism for predicting
+	 * how long the MUX config will take to apply and besides
+	 * seeing invalid reports we don't know of a reliable way to
+	 * explicitly check that the MUX config has landed.
+	 *
+	 * It's even possible we've miss characterized the underlying
+	 * problem - it just seems like the simplest explanation why
+	 * a delay at this location would mitigate any invalid reports.
+	 */
+	usleep_range(15000, 20000);
+
+	config_oa_regs(dev_priv, dev_priv->perf.oa.b_counter_regs,
+		       dev_priv->perf.oa.b_counter_regs_len);
+
+	return 0;
+}
+
+static void hsw_disable_metric_set(struct drm_i915_private *dev_priv)
+{
+	I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) &
+				  ~GEN6_CSUNIT_CLOCK_GATE_DISABLE));
+	I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) |
+				    GEN7_DOP_CLOCK_GATE_ENABLE));
+
+	I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
+				      ~GT_NOA_ENABLE));
+}
+
+static void gen7_update_oacontrol_locked(struct drm_i915_private *dev_priv)
+{
+	assert_spin_locked(&dev_priv->perf.hook_lock);
+
+	if (dev_priv->perf.oa.exclusive_stream->enabled) {
+		unsigned long ctx_id = 0;
+
+		if (dev_priv->perf.oa.exclusive_stream->ctx)
+			ctx_id = dev_priv->perf.oa.specific_ctx_id;
+
+		if (dev_priv->perf.oa.exclusive_stream->ctx == NULL || ctx_id) {
+			bool periodic = dev_priv->perf.oa.periodic;
+			u32 period_exponent = dev_priv->perf.oa.period_exponent;
+			u32 report_format = dev_priv->perf.oa.oa_buffer.format;
+
+			I915_WRITE(GEN7_OACONTROL,
+				   (ctx_id & GEN7_OACONTROL_CTX_MASK) |
+				   (period_exponent <<
+				    GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
+				   (periodic ?
+				    GEN7_OACONTROL_TIMER_ENABLE : 0) |
+				   (report_format <<
+				    GEN7_OACONTROL_FORMAT_SHIFT) |
+				   (ctx_id ?
+				    GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
+				   GEN7_OACONTROL_ENABLE);
+			return;
+		}
+	}
+
+	I915_WRITE(GEN7_OACONTROL, 0);
+}
+
+static void gen7_oa_enable(struct drm_i915_private *dev_priv)
+{
+	unsigned long flags;
+
+	/* Reset buf pointers so we don't forward reports from before now.
+	 *
+	 * Think carefully if considering trying to avoid this, since it
+	 * also ensures status flags and the buffer itself are cleared
+	 * in error paths, and we have checks for invalid reports based
+	 * on the assumption that certain fields are written to zeroed
+	 * memory which this helps maintains.
+	 */
+	gen7_init_oa_buffer(dev_priv);
+
+	spin_lock_irqsave(&dev_priv->perf.hook_lock, flags);
+	gen7_update_oacontrol_locked(dev_priv);
+	spin_unlock_irqrestore(&dev_priv->perf.hook_lock, flags);
+}
+
+static void i915_oa_stream_enable(struct i915_perf_stream *stream)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	dev_priv->perf.oa.ops.oa_enable(dev_priv);
+
+	if (dev_priv->perf.oa.periodic)
+		hrtimer_start(&dev_priv->perf.oa.poll_check_timer,
+			      ns_to_ktime(POLL_PERIOD),
+			      HRTIMER_MODE_REL_PINNED);
+}
+
+static void gen7_oa_disable(struct drm_i915_private *dev_priv)
+{
+	I915_WRITE(GEN7_OACONTROL, 0);
+}
+
+static void i915_oa_stream_disable(struct i915_perf_stream *stream)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	dev_priv->perf.oa.ops.oa_disable(dev_priv);
+
+	if (dev_priv->perf.oa.periodic)
+		hrtimer_cancel(&dev_priv->perf.oa.poll_check_timer);
+}
+
+static u64 oa_exponent_to_ns(struct drm_i915_private *dev_priv, int exponent)
+{
+	return 1000000000ULL * (2ULL << exponent) /
+		dev_priv->perf.oa.timestamp_frequency;
+}
+
+static const struct i915_perf_stream_ops i915_oa_stream_ops = {
+	.destroy = i915_oa_stream_destroy,
+	.enable = i915_oa_stream_enable,
+	.disable = i915_oa_stream_disable,
+	.wait_unlocked = i915_oa_wait_unlocked,
+	.poll_wait = i915_oa_poll_wait,
+	.read = i915_oa_read,
+};
+
+static int i915_oa_stream_init(struct i915_perf_stream *stream,
+			       struct drm_i915_perf_open_param *param,
+			       struct perf_open_properties *props)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	int format_size;
+	int ret;
+
+	if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
+		DRM_ERROR("Only OA report sampling supported\n");
+		return -EINVAL;
+	}
+
+	if (!dev_priv->perf.oa.ops.init_oa_buffer) {
+		DRM_ERROR("OA unit not supported\n");
+		return -ENODEV;
+	}
+
+	/* To avoid the complexity of having to accurately filter
+	 * counter reports and marshal to the appropriate client
+	 * we currently only allow exclusive access
+	 */
+	if (dev_priv->perf.oa.exclusive_stream) {
+		DRM_ERROR("OA unit already in use\n");
+		return -EBUSY;
+	}
+
+	if (!props->metrics_set) {
+		DRM_ERROR("OA metric set not specified\n");
+		return -EINVAL;
+	}
+
+	if (!props->oa_format) {
+		DRM_ERROR("OA report format not specified\n");
+		return -EINVAL;
+	}
+
+	stream->sample_size = sizeof(struct drm_i915_perf_record_header);
+
+	format_size = dev_priv->perf.oa.oa_formats[props->oa_format].size;
+
+	stream->sample_flags |= SAMPLE_OA_REPORT;
+	stream->sample_size += format_size;
+
+	dev_priv->perf.oa.oa_buffer.format_size = format_size;
+	BUG_ON(dev_priv->perf.oa.oa_buffer.format_size == 0);
+
+	dev_priv->perf.oa.oa_buffer.format =
+		dev_priv->perf.oa.oa_formats[props->oa_format].format;
+
+	dev_priv->perf.oa.metrics_set = props->metrics_set;
+
+	dev_priv->perf.oa.periodic = props->oa_periodic;
+	if (dev_priv->perf.oa.periodic) {
+		u64 period_ns = oa_exponent_to_ns(dev_priv,
+						  props->oa_period_exponent);
+
+		dev_priv->perf.oa.period_exponent = props->oa_period_exponent;
+
+		/* See comment for OA_TAIL_MARGIN_NSEC for details
+		 * about this tail_margin...
+		 */
+		dev_priv->perf.oa.tail_margin =
+			((OA_TAIL_MARGIN_NSEC / period_ns) + 1) * format_size;
+	}
+
+	ret = alloc_oa_buffer(dev_priv);
+	if (ret)
+		return ret;
+
+	/* PRM - observability performance counters:
+	 *
+	 *   OACONTROL, performance counter enable, note:
+	 *
+	 *   "When this bit is set, in order to have coherent counts,
+	 *   RC6 power state and trunk clock gating must be disabled.
+	 *   This can be achieved by programming MMIO registers as
+	 *   0xA094=0 and 0xA090[31]=1"
+	 *
+	 *   In our case we are expecting that taking pm + FORCEWAKE
+	 *   references will effectively disable RC6.
+	 */
+	intel_runtime_pm_get(dev_priv);
+	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
+
+	ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv);
+	if (ret) {
+		intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
+		intel_runtime_pm_put(dev_priv);
+		free_oa_buffer(dev_priv);
+		return ret;
+	}
+
+	stream->ops = &i915_oa_stream_ops;
+
+	/* On Haswell we have to track which OASTATUS1 flags we've already
+	 * seen since they can't be cleared while periodic sampling is enabled.
+	 */
+	dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
+
+	dev_priv->perf.oa.exclusive_stream = stream;
+
+	return 0;
+}
+
+static void gen7_update_hw_ctx_id_locked(struct drm_i915_private *dev_priv,
+					 u32 ctx_id)
+{
+	assert_spin_locked(&dev_priv->perf.hook_lock);
+
+	dev_priv->perf.oa.specific_ctx_id = ctx_id;
+	gen7_update_oacontrol_locked(dev_priv);
+}
+
+static void
+i915_oa_legacy_context_pin_notify_locked(struct drm_i915_private *dev_priv,
+					 struct i915_gem_context *ctx)
+{
+	assert_spin_locked(&dev_priv->perf.hook_lock);
+
+	BUG_ON(i915.enable_execlists);
+
+	if (dev_priv->perf.oa.ops.update_hw_ctx_id_locked == NULL)
+		return;
+
+	if (dev_priv->perf.oa.exclusive_stream &&
+	    dev_priv->perf.oa.exclusive_stream->ctx == ctx) {
+		struct i915_vma *vma = ctx->engine[RCS].state;
+		u32 ctx_id = i915_ggtt_offset(vma);
+
+		dev_priv->perf.oa.ops.update_hw_ctx_id_locked(dev_priv, ctx_id);
+	}
+}
+
+void i915_oa_legacy_context_pin_notify(struct drm_i915_private *dev_priv,
+				       struct i915_gem_context *ctx)
+{
+	unsigned long flags;
+
+	if (!dev_priv->perf.initialized)
+		return;
+
+	spin_lock_irqsave(&dev_priv->perf.hook_lock, flags);
+	i915_oa_legacy_context_pin_notify_locked(dev_priv, ctx);
+	spin_unlock_irqrestore(&dev_priv->perf.hook_lock, flags);
+}
+
 static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
 				     struct file *file,
 				     char __user *buf,
@@ -79,8 +925,20 @@ static ssize_t i915_perf_read(struct file *file,
 	struct drm_i915_private *dev_priv = stream->dev_priv;
 	ssize_t ret;
 
+	/* To ensure it's handled consistently we simply treat all reads of a
+	 * disabled stream as an error. In particular it might otherwise lead
+	 * to a deadlock for blocking file descriptors...
+	 */
+	if (!stream->enabled)
+		return -EIO;
+
 	if (!(file->f_flags & O_NONBLOCK)) {
-		/* Allow false positives from stream->ops->wait_unlocked.
+		/* There's the small chance of false positives from
+		 * stream->ops->wait_unlocked.
+		 *
+		 * E.g. with single context filtering since we only wait until
+		 * oabuffer has >= 1 report we don't immediately know whether
+		 * any reports really belong to the current context
 		 */
 		do {
 			ret = stream->ops->wait_unlocked(stream);
@@ -98,21 +956,50 @@ static ssize_t i915_perf_read(struct file *file,
 		mutex_unlock(&dev_priv->perf.lock);
 	}
 
+	if (ret >= 0) {
+		/* Maybe make ->pollin per-stream state if we support multiple
+		 * concurrent streams in the future. */
+		atomic_set(&dev_priv->perf.oa.pollin, false);
+	}
+
 	return ret;
 }
 
-static unsigned int i915_perf_poll_locked(struct i915_perf_stream *stream,
+static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
+{
+	struct drm_i915_private *dev_priv =
+		container_of(hrtimer, typeof(*dev_priv),
+			     perf.oa.poll_check_timer);
+
+	if (!dev_priv->perf.oa.ops.oa_buffer_is_empty(dev_priv)) {
+		atomic_set(&dev_priv->perf.oa.pollin, true);
+		wake_up(&dev_priv->perf.oa.poll_wq);
+	}
+
+	hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
+
+	return HRTIMER_RESTART;
+}
+
+static unsigned int i915_perf_poll_locked(struct drm_i915_private *dev_priv,
+					  struct i915_perf_stream *stream,
 					  struct file *file,
 					  poll_table *wait)
 {
-	unsigned int streams = 0;
+	unsigned int events = 0;
 
 	stream->ops->poll_wait(stream, file, wait);
 
-	if (stream->ops->can_read(stream))
-		streams |= POLLIN;
+	/* Note: we don't explicitly check whether there's something to read
+	 * here since this path may be very hot depending on what else
+	 * userspace is polling, or on the timeout in use. We rely solely on
+	 * the hrtimer/oa_poll_check_timer_cb to notify us when there are
+	 * samples to read.
+	 */
+	if (atomic_read(&dev_priv->perf.oa.pollin))
+		events |= POLLIN;
 
-	return streams;
+	return events;
 }
 
 static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
@@ -122,7 +1009,7 @@ static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
 	int ret;
 
 	mutex_lock(&dev_priv->perf.lock);
-	ret = i915_perf_poll_locked(stream, file, wait);
+	ret = i915_perf_poll_locked(dev_priv, stream, file, wait);
 	mutex_unlock(&dev_priv->perf.lock);
 
 	return ret;
@@ -286,18 +1173,18 @@ int i915_perf_open_ioctl_locked(struct drm_device *dev,
 		goto err_ctx;
 	}
 
-	stream->sample_flags = props->sample_flags;
 	stream->dev_priv = dev_priv;
 	stream->ctx = specific_ctx;
 
-	/*
-	 * TODO: support sampling something
-	 *
-	 * For now this is as far as we can go.
+	ret = i915_oa_stream_init(stream, param, props);
+	if (ret)
+		goto err_alloc;
+
+	/* we avoid simply assigning stream->sample_flags = props->sample_flags
+	 * to have _stream_init check the combination of sample flags more
+	 * thoroughly, but still this is the expected result at this point.
 	 */
-	DRM_ERROR("Unsupported i915 perf stream configuration\n");
-	ret = -EINVAL;
-	goto err_alloc;
+	BUG_ON(stream->sample_flags != props->sample_flags);
 
 	list_add(&stream->link, &dev_priv->perf.streams);
 
@@ -382,7 +1269,56 @@ static int read_properties_unlocked(struct drm_i915_private *dev_priv,
 			props->single_context = 1;
 			props->ctx_handle = value;
 			break;
-
+		case DRM_I915_PERF_PROP_SAMPLE_OA:
+			props->sample_flags |= SAMPLE_OA_REPORT;
+			break;
+		case DRM_I915_PERF_PROP_OA_METRICS_SET:
+			if (value == 0 ||
+			    value > dev_priv->perf.oa.n_builtin_sets) {
+				DRM_ERROR("Unknown OA metric set ID");
+				return -EINVAL;
+			}
+			props->metrics_set = value;
+			break;
+		case DRM_I915_PERF_PROP_OA_FORMAT:
+			if (value == 0 || value >= I915_OA_FORMAT_MAX) {
+				DRM_ERROR("Invalid OA report format\n");
+				return -EINVAL;
+			}
+			if (!dev_priv->perf.oa.oa_formats[value].size) {
+				DRM_ERROR("Invalid OA report format\n");
+				return -EINVAL;
+			}
+			props->oa_format = value;
+			break;
+		case DRM_I915_PERF_PROP_OA_EXPONENT:
+			if (value > OA_EXPONENT_MAX) {
+				DRM_ERROR("OA timer exponent too high (> %u)\n",
+					  OA_EXPONENT_MAX);
+				return -EINVAL;
+			}
+
+			/* NB: The exponent represents a period as follows:
+			 *
+			 *   80ns * 2^(period_exponent + 1)
+			 *
+			 * Theoretically we can program the OA unit to sample
+			 * every 160ns but don't allow that by default unless
+			 * root.
+			 *
+			 * Referring to perf's
+			 * kernel.perf_event_max_sample_rate for a precedent
+			 * (100000 by default); with an OA exponent of 6 we get
+			 * a period of 10.240 microseconds -just under 100000Hz
+			 */
+			if (value < 6 && !capable(CAP_SYS_ADMIN)) {
+				DRM_ERROR("Sampling period too high without root privileges\n");
+				return -EACCES;
+			}
+
+			props->oa_periodic = true;
+			props->oa_period_exponent = value;
+			break;
 		case DRM_I915_PERF_PROP_MAX:
 			BUG();
 		}
@@ -431,8 +1367,35 @@ int i915_perf_open_ioctl(struct drm_device *dev, void *data,
 
 void i915_perf_init(struct drm_i915_private *dev_priv)
 {
+	if (!IS_HASWELL(dev_priv))
+		return;
+
+	hrtimer_init(&dev_priv->perf.oa.poll_check_timer,
+		     CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	dev_priv->perf.oa.poll_check_timer.function = oa_poll_check_timer_cb;
+	init_waitqueue_head(&dev_priv->perf.oa.poll_wq);
+
 	INIT_LIST_HEAD(&dev_priv->perf.streams);
 	mutex_init(&dev_priv->perf.lock);
+	spin_lock_init(&dev_priv->perf.hook_lock);
+
+	dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer;
+	dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set;
+	dev_priv->perf.oa.ops.disable_metric_set = hsw_disable_metric_set;
+	dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable;
+	dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable;
+	dev_priv->perf.oa.ops.update_hw_ctx_id_locked =
+		gen7_update_hw_ctx_id_locked;
+	dev_priv->perf.oa.ops.read = gen7_oa_read;
+	dev_priv->perf.oa.ops.oa_buffer_is_empty =
+		gen7_oa_buffer_is_empty_fop_unlocked;
+
+	dev_priv->perf.oa.timestamp_frequency = 12500000;
+
+	dev_priv->perf.oa.oa_formats = hsw_oa_formats;
+
+	dev_priv->perf.oa.n_builtin_sets =
+		i915_oa_n_builtin_metric_sets_hsw;
 
 	dev_priv->perf.initialized = true;
 }
@@ -442,7 +1405,6 @@ void i915_perf_fini(struct drm_i915_private *dev_priv)
 	if (!dev_priv->perf.initialized)
 		return;
 
-	/* Currently nothing to clean up */
-
+	memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops));
 	dev_priv->perf.initialized = false;
 }
diff --git a/drivers/gpu/drm/i915/i915_reg.h b/drivers/gpu/drm/i915/i915_reg.h
index 90756b2..a0ccc09 100644
--- a/drivers/gpu/drm/i915/i915_reg.h
+++ b/drivers/gpu/drm/i915/i915_reg.h
@@ -617,6 +617,343 @@ static inline bool i915_mmio_reg_valid(i915_reg_t reg)
 #define HSW_CS_GPR_UDW(n)               _MMIO(0x2600 + (n) * 8 + 4)
 
 #define GEN7_OACONTROL _MMIO(0x2360)
+#define  GEN7_OACONTROL_CTX_MASK	    0xFFFFF000
+#define  GEN7_OACONTROL_TIMER_PERIOD_MASK   0x3F
+#define  GEN7_OACONTROL_TIMER_PERIOD_SHIFT  6
+#define  GEN7_OACONTROL_TIMER_ENABLE	    (1<<5)
+#define  GEN7_OACONTROL_FORMAT_A13	    (0<<2)
+#define  GEN7_OACONTROL_FORMAT_A29	    (1<<2)
+#define  GEN7_OACONTROL_FORMAT_A13_B8_C8    (2<<2)
+#define  GEN7_OACONTROL_FORMAT_A29_B8_C8    (3<<2)
+#define  GEN7_OACONTROL_FORMAT_B4_C8	    (4<<2)
+#define  GEN7_OACONTROL_FORMAT_A45_B8_C8    (5<<2)
+#define  GEN7_OACONTROL_FORMAT_B4_C8_A16    (6<<2)
+#define  GEN7_OACONTROL_FORMAT_C4_B8	    (7<<2)
+#define  GEN7_OACONTROL_FORMAT_SHIFT	    2
+#define  GEN7_OACONTROL_PER_CTX_ENABLE	    (1<<1)
+#define  GEN7_OACONTROL_ENABLE		    (1<<0)
+
+#define GEN8_OACTXID _MMIO(0x2364)
+
+#define GEN8_OACONTROL _MMIO(0x2B00)
+#define  GEN8_OA_REPORT_FORMAT_A12	    (0<<2)
+#define  GEN8_OA_REPORT_FORMAT_A12_B8_C8    (2<<2)
+#define  GEN8_OA_REPORT_FORMAT_A36_B8_C8    (5<<2)
+#define  GEN8_OA_REPORT_FORMAT_C4_B8	    (7<<2)
+#define  GEN8_OA_REPORT_FORMAT_SHIFT	    2
+#define  GEN8_OA_SPECIFIC_CONTEXT_ENABLE    (1<<1)
+#define  GEN8_OA_COUNTER_ENABLE             (1<<0)
+
+#define GEN8_OACTXCONTROL _MMIO(0x2360)
+#define  GEN8_OA_TIMER_PERIOD_MASK	    0x3F
+#define  GEN8_OA_TIMER_PERIOD_SHIFT	    2
+#define  GEN8_OA_TIMER_ENABLE		    (1<<1)
+#define  GEN8_OA_COUNTER_RESUME		    (1<<0)
+
+#define GEN7_OABUFFER _MMIO(0x23B0) /* R/W */
+#define  GEN7_OABUFFER_OVERRUN_DISABLE	    (1<<3)
+#define  GEN7_OABUFFER_EDGE_TRIGGER	    (1<<2)
+#define  GEN7_OABUFFER_STOP_RESUME_ENABLE   (1<<1)
+#define  GEN7_OABUFFER_RESUME		    (1<<0)
+
+#define GEN8_OABUFFER _MMIO(0x2b14)
+
+#define GEN7_OASTATUS1 _MMIO(0x2364)
+#define  GEN7_OASTATUS1_TAIL_MASK	    0xffffffc0
+#define  GEN7_OASTATUS1_COUNTER_OVERFLOW    (1<<2)
+#define  GEN7_OASTATUS1_OABUFFER_OVERFLOW   (1<<1)
+#define  GEN7_OASTATUS1_REPORT_LOST	    (1<<0)
+
+#define GEN7_OASTATUS2 _MMIO(0x2368)
+#define GEN7_OASTATUS2_HEAD_MASK    0xffffffc0
+
+#define GEN8_OASTATUS _MMIO(0x2b08)
+#define  GEN8_OASTATUS_OVERRUN_STATUS	    (1<<3)
+#define  GEN8_OASTATUS_COUNTER_OVERFLOW     (1<<2)
+#define  GEN8_OASTATUS_OABUFFER_OVERFLOW    (1<<1)
+#define  GEN8_OASTATUS_REPORT_LOST	    (1<<0)
+
+#define GEN8_OAHEADPTR _MMIO(0x2B0C)
+#define GEN8_OATAILPTR _MMIO(0x2B10)
+
+#define OABUFFER_SIZE_128K  (0<<3)
+#define OABUFFER_SIZE_256K  (1<<3)
+#define OABUFFER_SIZE_512K  (2<<3)
+#define OABUFFER_SIZE_1M    (3<<3)
+#define OABUFFER_SIZE_2M    (4<<3)
+#define OABUFFER_SIZE_4M    (5<<3)
+#define OABUFFER_SIZE_8M    (6<<3)
+#define OABUFFER_SIZE_16M   (7<<3)
+
+#define OA_MEM_SELECT_GGTT  (1<<0)
+
+#define EU_PERF_CNTL0	    _MMIO(0xe458)
+
+#define GDT_CHICKEN_BITS    _MMIO(0x9840)
+#define GT_NOA_ENABLE	    0x00000080
+
+/*
+ * OA Boolean state
+ */
+
+#define OAREPORTTRIG1 _MMIO(0x2740)
+#define OAREPORTTRIG1_THRESHOLD_MASK 0xffff
+#define OAREPORTTRIG1_EDGE_LEVEL_TRIGER_SELECT_MASK 0xffff0000 /* 0=level */
+
+#define OAREPORTTRIG2 _MMIO(0x2744)
+#define OAREPORTTRIG2_INVERT_A_0  (1<<0)
+#define OAREPORTTRIG2_INVERT_A_1  (1<<1)
+#define OAREPORTTRIG2_INVERT_A_2  (1<<2)
+#define OAREPORTTRIG2_INVERT_A_3  (1<<3)
+#define OAREPORTTRIG2_INVERT_A_4  (1<<4)
+#define OAREPORTTRIG2_INVERT_A_5  (1<<5)
+#define OAREPORTTRIG2_INVERT_A_6  (1<<6)
+#define OAREPORTTRIG2_INVERT_A_7  (1<<7)
+#define OAREPORTTRIG2_INVERT_A_8  (1<<8)
+#define OAREPORTTRIG2_INVERT_A_9  (1<<9)
+#define OAREPORTTRIG2_INVERT_A_10 (1<<10)
+#define OAREPORTTRIG2_INVERT_A_11 (1<<11)
+#define OAREPORTTRIG2_INVERT_A_12 (1<<12)
+#define OAREPORTTRIG2_INVERT_A_13 (1<<13)
+#define OAREPORTTRIG2_INVERT_A_14 (1<<14)
+#define OAREPORTTRIG2_INVERT_A_15 (1<<15)
+#define OAREPORTTRIG2_INVERT_B_0  (1<<16)
+#define OAREPORTTRIG2_INVERT_B_1  (1<<17)
+#define OAREPORTTRIG2_INVERT_B_2  (1<<18)
+#define OAREPORTTRIG2_INVERT_B_3  (1<<19)
+#define OAREPORTTRIG2_INVERT_C_0  (1<<20)
+#define OAREPORTTRIG2_INVERT_C_1  (1<<21)
+#define OAREPORTTRIG2_INVERT_D_0  (1<<22)
+#define OAREPORTTRIG2_THRESHOLD_ENABLE	    (1<<23)
+#define OAREPORTTRIG2_REPORT_TRIGGER_ENABLE (1<<31)
+
+#define OAREPORTTRIG3 _MMIO(0x2748)
+#define OAREPORTTRIG3_NOA_SELECT_MASK	    0xf
+#define OAREPORTTRIG3_NOA_SELECT_8_SHIFT    0
+#define OAREPORTTRIG3_NOA_SELECT_9_SHIFT    4
+#define OAREPORTTRIG3_NOA_SELECT_10_SHIFT   8
+#define OAREPORTTRIG3_NOA_SELECT_11_SHIFT   12
+#define OAREPORTTRIG3_NOA_SELECT_12_SHIFT   16
+#define OAREPORTTRIG3_NOA_SELECT_13_SHIFT   20
+#define OAREPORTTRIG3_NOA_SELECT_14_SHIFT   24
+#define OAREPORTTRIG3_NOA_SELECT_15_SHIFT   28
+
+#define OAREPORTTRIG4 _MMIO(0x274c)
+#define OAREPORTTRIG4_NOA_SELECT_MASK	    0xf
+#define OAREPORTTRIG4_NOA_SELECT_0_SHIFT    0
+#define OAREPORTTRIG4_NOA_SELECT_1_SHIFT    4
+#define OAREPORTTRIG4_NOA_SELECT_2_SHIFT    8
+#define OAREPORTTRIG4_NOA_SELECT_3_SHIFT    12
+#define OAREPORTTRIG4_NOA_SELECT_4_SHIFT    16
+#define OAREPORTTRIG4_NOA_SELECT_5_SHIFT    20
+#define OAREPORTTRIG4_NOA_SELECT_6_SHIFT    24
+#define OAREPORTTRIG4_NOA_SELECT_7_SHIFT    28
+
+#define OAREPORTTRIG5 _MMIO(0x2750)
+#define OAREPORTTRIG5_THRESHOLD_MASK 0xffff
+#define OAREPORTTRIG5_EDGE_LEVEL_TRIGER_SELECT_MASK 0xffff0000 /* 0=level */
+
+#define OAREPORTTRIG6 _MMIO(0x2754)
+#define OAREPORTTRIG6_INVERT_A_0  (1<<0)
+#define OAREPORTTRIG6_INVERT_A_1  (1<<1)
+#define OAREPORTTRIG6_INVERT_A_2  (1<<2)
+#define OAREPORTTRIG6_INVERT_A_3  (1<<3)
+#define OAREPORTTRIG6_INVERT_A_4  (1<<4)
+#define OAREPORTTRIG6_INVERT_A_5  (1<<5)
+#define OAREPORTTRIG6_INVERT_A_6  (1<<6)
+#define OAREPORTTRIG6_INVERT_A_7  (1<<7)
+#define OAREPORTTRIG6_INVERT_A_8  (1<<8)
+#define OAREPORTTRIG6_INVERT_A_9  (1<<9)
+#define OAREPORTTRIG6_INVERT_A_10 (1<<10)
+#define OAREPORTTRIG6_INVERT_A_11 (1<<11)
+#define OAREPORTTRIG6_INVERT_A_12 (1<<12)
+#define OAREPORTTRIG6_INVERT_A_13 (1<<13)
+#define OAREPORTTRIG6_INVERT_A_14 (1<<14)
+#define OAREPORTTRIG6_INVERT_A_15 (1<<15)
+#define OAREPORTTRIG6_INVERT_B_0  (1<<16)
+#define OAREPORTTRIG6_INVERT_B_1  (1<<17)
+#define OAREPORTTRIG6_INVERT_B_2  (1<<18)
+#define OAREPORTTRIG6_INVERT_B_3  (1<<19)
+#define OAREPORTTRIG6_INVERT_C_0  (1<<20)
+#define OAREPORTTRIG6_INVERT_C_1  (1<<21)
+#define OAREPORTTRIG6_INVERT_D_0  (1<<22)
+#define OAREPORTTRIG6_THRESHOLD_ENABLE	    (1<<23)
+#define OAREPORTTRIG6_REPORT_TRIGGER_ENABLE (1<<31)
+
+#define OAREPORTTRIG7 _MMIO(0x2758)
+#define OAREPORTTRIG7_NOA_SELECT_MASK	    0xf
+#define OAREPORTTRIG7_NOA_SELECT_8_SHIFT    0
+#define OAREPORTTRIG7_NOA_SELECT_9_SHIFT    4
+#define OAREPORTTRIG7_NOA_SELECT_10_SHIFT   8
+#define OAREPORTTRIG7_NOA_SELECT_11_SHIFT   12
+#define OAREPORTTRIG7_NOA_SELECT_12_SHIFT   16
+#define OAREPORTTRIG7_NOA_SELECT_13_SHIFT   20
+#define OAREPORTTRIG7_NOA_SELECT_14_SHIFT   24
+#define OAREPORTTRIG7_NOA_SELECT_15_SHIFT   28
+
+#define OAREPORTTRIG8 _MMIO(0x275c)
+#define OAREPORTTRIG8_NOA_SELECT_MASK	    0xf
+#define OAREPORTTRIG8_NOA_SELECT_0_SHIFT    0
+#define OAREPORTTRIG8_NOA_SELECT_1_SHIFT    4
+#define OAREPORTTRIG8_NOA_SELECT_2_SHIFT    8
+#define OAREPORTTRIG8_NOA_SELECT_3_SHIFT    12
+#define OAREPORTTRIG8_NOA_SELECT_4_SHIFT    16
+#define OAREPORTTRIG8_NOA_SELECT_5_SHIFT    20
+#define OAREPORTTRIG8_NOA_SELECT_6_SHIFT    24
+#define OAREPORTTRIG8_NOA_SELECT_7_SHIFT    28
+
+#define OASTARTTRIG1 _MMIO(0x2710)
+#define OASTARTTRIG1_THRESHOLD_COUNT_MASK_MBZ 0xffff0000
+#define OASTARTTRIG1_THRESHOLD_MASK	      0xffff
+
+#define OASTARTTRIG2 _MMIO(0x2714)
+#define OASTARTTRIG2_INVERT_A_0 (1<<0)
+#define OASTARTTRIG2_INVERT_A_1 (1<<1)
+#define OASTARTTRIG2_INVERT_A_2 (1<<2)
+#define OASTARTTRIG2_INVERT_A_3 (1<<3)
+#define OASTARTTRIG2_INVERT_A_4 (1<<4)
+#define OASTARTTRIG2_INVERT_A_5 (1<<5)
+#define OASTARTTRIG2_INVERT_A_6 (1<<6)
+#define OASTARTTRIG2_INVERT_A_7 (1<<7)
+#define OASTARTTRIG2_INVERT_A_8 (1<<8)
+#define OASTARTTRIG2_INVERT_A_9 (1<<9)
+#define OASTARTTRIG2_INVERT_A_10 (1<<10)
+#define OASTARTTRIG2_INVERT_A_11 (1<<11)
+#define OASTARTTRIG2_INVERT_A_12 (1<<12)
+#define OASTARTTRIG2_INVERT_A_13 (1<<13)
+#define OASTARTTRIG2_INVERT_A_14 (1<<14)
+#define OASTARTTRIG2_INVERT_A_15 (1<<15)
+#define OASTARTTRIG2_INVERT_B_0 (1<<16)
+#define OASTARTTRIG2_INVERT_B_1 (1<<17)
+#define OASTARTTRIG2_INVERT_B_2 (1<<18)
+#define OASTARTTRIG2_INVERT_B_3 (1<<19)
+#define OASTARTTRIG2_INVERT_C_0 (1<<20)
+#define OASTARTTRIG2_INVERT_C_1 (1<<21)
+#define OASTARTTRIG2_INVERT_D_0 (1<<22)
+#define OASTARTTRIG2_THRESHOLD_ENABLE	    (1<<23)
+#define OASTARTTRIG2_START_TRIG_FLAG_MBZ    (1<<24)
+#define OASTARTTRIG2_EVENT_SELECT_0  (1<<28)
+#define OASTARTTRIG2_EVENT_SELECT_1  (1<<29)
+#define OASTARTTRIG2_EVENT_SELECT_2  (1<<30)
+#define OASTARTTRIG2_EVENT_SELECT_3  (1<<31)
+
+#define OASTARTTRIG3 _MMIO(0x2718)
+#define OASTARTTRIG3_NOA_SELECT_MASK	   0xf
+#define OASTARTTRIG3_NOA_SELECT_8_SHIFT    0
+#define OASTARTTRIG3_NOA_SELECT_9_SHIFT    4
+#define OASTARTTRIG3_NOA_SELECT_10_SHIFT   8
+#define OASTARTTRIG3_NOA_SELECT_11_SHIFT   12
+#define OASTARTTRIG3_NOA_SELECT_12_SHIFT   16
+#define OASTARTTRIG3_NOA_SELECT_13_SHIFT   20
+#define OASTARTTRIG3_NOA_SELECT_14_SHIFT   24
+#define OASTARTTRIG3_NOA_SELECT_15_SHIFT   28
+
+#define OASTARTTRIG4 _MMIO(0x271c)
+#define OASTARTTRIG4_NOA_SELECT_MASK	    0xf
+#define OASTARTTRIG4_NOA_SELECT_0_SHIFT    0
+#define OASTARTTRIG4_NOA_SELECT_1_SHIFT    4
+#define OASTARTTRIG4_NOA_SELECT_2_SHIFT    8
+#define OASTARTTRIG4_NOA_SELECT_3_SHIFT    12
+#define OASTARTTRIG4_NOA_SELECT_4_SHIFT    16
+#define OASTARTTRIG4_NOA_SELECT_5_SHIFT    20
+#define OASTARTTRIG4_NOA_SELECT_6_SHIFT    24
+#define OASTARTTRIG4_NOA_SELECT_7_SHIFT    28
+
+#define OASTARTTRIG5 _MMIO(0x2720)
+#define OASTARTTRIG5_THRESHOLD_COUNT_MASK_MBZ 0xffff0000
+#define OASTARTTRIG5_THRESHOLD_MASK	      0xffff
+
+#define OASTARTTRIG6 _MMIO(0x2724)
+#define OASTARTTRIG6_INVERT_A_0 (1<<0)
+#define OASTARTTRIG6_INVERT_A_1 (1<<1)
+#define OASTARTTRIG6_INVERT_A_2 (1<<2)
+#define OASTARTTRIG6_INVERT_A_3 (1<<3)
+#define OASTARTTRIG6_INVERT_A_4 (1<<4)
+#define OASTARTTRIG6_INVERT_A_5 (1<<5)
+#define OASTARTTRIG6_INVERT_A_6 (1<<6)
+#define OASTARTTRIG6_INVERT_A_7 (1<<7)
+#define OASTARTTRIG6_INVERT_A_8 (1<<8)
+#define OASTARTTRIG6_INVERT_A_9 (1<<9)
+#define OASTARTTRIG6_INVERT_A_10 (1<<10)
+#define OASTARTTRIG6_INVERT_A_11 (1<<11)
+#define OASTARTTRIG6_INVERT_A_12 (1<<12)
+#define OASTARTTRIG6_INVERT_A_13 (1<<13)
+#define OASTARTTRIG6_INVERT_A_14 (1<<14)
+#define OASTARTTRIG6_INVERT_A_15 (1<<15)
+#define OASTARTTRIG6_INVERT_B_0 (1<<16)
+#define OASTARTTRIG6_INVERT_B_1 (1<<17)
+#define OASTARTTRIG6_INVERT_B_2 (1<<18)
+#define OASTARTTRIG6_INVERT_B_3 (1<<19)
+#define OASTARTTRIG6_INVERT_C_0 (1<<20)
+#define OASTARTTRIG6_INVERT_C_1 (1<<21)
+#define OASTARTTRIG6_INVERT_D_0 (1<<22)
+#define OASTARTTRIG6_THRESHOLD_ENABLE	    (1<<23)
+#define OASTARTTRIG6_START_TRIG_FLAG_MBZ    (1<<24)
+#define OASTARTTRIG6_EVENT_SELECT_4  (1<<28)
+#define OASTARTTRIG6_EVENT_SELECT_5  (1<<29)
+#define OASTARTTRIG6_EVENT_SELECT_6  (1<<30)
+#define OASTARTTRIG6_EVENT_SELECT_7  (1<<31)
+
+#define OASTARTTRIG7 _MMIO(0x2728)
+#define OASTARTTRIG7_NOA_SELECT_MASK	   0xf
+#define OASTARTTRIG7_NOA_SELECT_8_SHIFT    0
+#define OASTARTTRIG7_NOA_SELECT_9_SHIFT    4
+#define OASTARTTRIG7_NOA_SELECT_10_SHIFT   8
+#define OASTARTTRIG7_NOA_SELECT_11_SHIFT   12
+#define OASTARTTRIG7_NOA_SELECT_12_SHIFT   16
+#define OASTARTTRIG7_NOA_SELECT_13_SHIFT   20
+#define OASTARTTRIG7_NOA_SELECT_14_SHIFT   24
+#define OASTARTTRIG7_NOA_SELECT_15_SHIFT   28
+
+#define OASTARTTRIG8 _MMIO(0x272c)
+#define OASTARTTRIG8_NOA_SELECT_MASK	   0xf
+#define OASTARTTRIG8_NOA_SELECT_0_SHIFT    0
+#define OASTARTTRIG8_NOA_SELECT_1_SHIFT    4
+#define OASTARTTRIG8_NOA_SELECT_2_SHIFT    8
+#define OASTARTTRIG8_NOA_SELECT_3_SHIFT    12
+#define OASTARTTRIG8_NOA_SELECT_4_SHIFT    16
+#define OASTARTTRIG8_NOA_SELECT_5_SHIFT    20
+#define OASTARTTRIG8_NOA_SELECT_6_SHIFT    24
+#define OASTARTTRIG8_NOA_SELECT_7_SHIFT    28
+
+/* CECX_0 */
+#define OACEC_COMPARE_LESS_OR_EQUAL	6
+#define OACEC_COMPARE_NOT_EQUAL		5
+#define OACEC_COMPARE_LESS_THAN		4
+#define OACEC_COMPARE_GREATER_OR_EQUAL	3
+#define OACEC_COMPARE_EQUAL		2
+#define OACEC_COMPARE_GREATER_THAN	1
+#define OACEC_COMPARE_ANY_EQUAL		0
+
+#define OACEC_COMPARE_VALUE_MASK    0xffff
+#define OACEC_COMPARE_VALUE_SHIFT   3
+
+#define OACEC_SELECT_NOA	(0<<19)
+#define OACEC_SELECT_PREV	(1<<19)
+#define OACEC_SELECT_BOOLEAN	(2<<19)
+
+/* CECX_1 */
+#define OACEC_MASK_MASK		    0xffff
+#define OACEC_CONSIDERATIONS_MASK   0xffff
+#define OACEC_CONSIDERATIONS_SHIFT  16
+
+#define OACEC0_0 _MMIO(0x2770)
+#define OACEC0_1 _MMIO(0x2774)
+#define OACEC1_0 _MMIO(0x2778)
+#define OACEC1_1 _MMIO(0x277c)
+#define OACEC2_0 _MMIO(0x2780)
+#define OACEC2_1 _MMIO(0x2784)
+#define OACEC3_0 _MMIO(0x2788)
+#define OACEC3_1 _MMIO(0x278c)
+#define OACEC4_0 _MMIO(0x2790)
+#define OACEC4_1 _MMIO(0x2794)
+#define OACEC5_0 _MMIO(0x2798)
+#define OACEC5_1 _MMIO(0x279c)
+#define OACEC6_0 _MMIO(0x27a0)
+#define OACEC6_1 _MMIO(0x27a4)
+#define OACEC7_0 _MMIO(0x27a8)
+#define OACEC7_1 _MMIO(0x27ac)
+
 
 #define _GEN7_PIPEA_DE_LOAD_SL	0x70068
 #define _GEN7_PIPEB_DE_LOAD_SL	0x71068
@@ -6977,6 +7314,7 @@ enum {
 # define GEN6_RCCUNIT_CLOCK_GATE_DISABLE		(1 << 11)
 
 #define GEN6_UCGCTL3				_MMIO(0x9408)
+# define GEN6_OACSUNIT_CLOCK_GATE_DISABLE		(1 << 20)
 
 #define GEN7_UCGCTL4				_MMIO(0x940c)
 #define  GEN7_L3BANK2X_CLOCK_GATE_DISABLE	(1<<25)
diff --git a/drivers/gpu/drm/i915/intel_ringbuffer.c b/drivers/gpu/drm/i915/intel_ringbuffer.c
index 7a74750..22d5ff1 100644
--- a/drivers/gpu/drm/i915/intel_ringbuffer.c
+++ b/drivers/gpu/drm/i915/intel_ringbuffer.c
@@ -2043,8 +2043,14 @@ static int intel_ring_context_pin(struct i915_gem_context *ctx,
 		if (ret)
 			goto error;
 
-		ret = i915_vma_pin(ce->state, 0, ctx->ggtt_alignment,
-				   PIN_GLOBAL | PIN_HIGH);
+		if (engine->id == RCS) {
+			u64 vma_flags = PIN_GLOBAL | PIN_HIGH;
+			ret = i915_gem_context_pin_legacy_rcs_state(engine->i915,
+								    ctx,
+								    vma_flags);
+		} else
+			ret = i915_vma_pin(ce->state, 0, ctx->ggtt_alignment,
+					   PIN_GLOBAL | PIN_HIGH);
 		if (ret)
 			goto error;
 	}
diff --git a/include/uapi/drm/i915_drm.h b/include/uapi/drm/i915_drm.h
index 7777678..5ad3747 100644
--- a/include/uapi/drm/i915_drm.h
+++ b/include/uapi/drm/i915_drm.h
@@ -1224,6 +1224,18 @@ struct drm_i915_gem_context_param {
 	__u64 value;
 };
 
+enum drm_i915_oa_format {
+	I915_OA_FORMAT_A13 = 1,
+	I915_OA_FORMAT_A29,
+	I915_OA_FORMAT_A13_B8_C8,
+	I915_OA_FORMAT_B4_C8,
+	I915_OA_FORMAT_A45_B8_C8,
+	I915_OA_FORMAT_B4_C8_A16,
+	I915_OA_FORMAT_C4_B8,
+
+	I915_OA_FORMAT_MAX	    /* non-ABI */
+};
+
 enum drm_i915_perf_property_id {
 	/**
 	 * Open the stream for a specific context handle (as used with
@@ -1232,6 +1244,32 @@ enum drm_i915_perf_property_id {
 	 */
 	DRM_I915_PERF_PROP_CTX_HANDLE = 1,
 
+	/**
+	 * A value of 1 requests the inclusion of raw OA unit reports as
+	 * part of stream samples.
+	 */
+	DRM_I915_PERF_PROP_SAMPLE_OA,
+
+	/**
+	 * The value specifies which set of OA unit metrics should be
+	 * be configured, defining the contents of any OA unit reports.
+	 */
+	DRM_I915_PERF_PROP_OA_METRICS_SET,
+
+	/**
+	 * The value specifies the size and layout of OA unit reports.
+	 */
+	DRM_I915_PERF_PROP_OA_FORMAT,
+
+	/**
+	 * Specifying this property implicitly requests periodic OA unit
+	 * sampling and (at least on Haswell) the sampling frequency is derived
+	 * from this exponent as follows:
+	 *
+	 *   80ns * 2^(period_exponent + 1)
+	 */
+	DRM_I915_PERF_PROP_OA_EXPONENT,
+
 	DRM_I915_PERF_PROP_MAX /* non-ABI */
 };
 
@@ -1251,7 +1289,22 @@ struct drm_i915_perf_open_param {
 	__u64 __user properties_ptr;
 };
 
+/**
+ * Enable data capture for a stream that was either opened in a disabled state
+ * via I915_PERF_FLAG_DIABLED or was later disabled via I915_PERF_IOCTL_DISABLE.
+ *
+ * It is intended to be cheaper to disable and enable a stream than it may be
+ * to close and re-open a stream with the same configuration.
+ *
+ * It's undefined whether any pending data for the stream will be lost.
+ */
 #define I915_PERF_IOCTL_ENABLE	_IO('i', 0x0)
+
+/**
+ * Disable data capture for a stream.
+ *
+ * It is an error to try and read a stream that is disabled.
+ */
 #define I915_PERF_IOCTL_DISABLE	_IO('i', 0x1)
 
 /**
@@ -1275,17 +1328,30 @@ enum drm_i915_perf_record_type {
 	 * every sample.
 	 *
 	 * The order of these sample properties given by userspace has no
-	 * affect on the ordering of data within a sample. The order will be
+	 * affect on the ordering of data within a sample. The order is
 	 * documented here.
 	 *
 	 * struct {
 	 *     struct drm_i915_perf_record_header header;
 	 *
-	 *     TODO: itemize extensible sample data here
+	 *     { u32 oa_report[]; } && DRM_I915_PERF_PROP_SAMPLE_OA
 	 * };
 	 */
 	DRM_I915_PERF_RECORD_SAMPLE = 1,
 
+	/*
+	 * Indicates that one or more OA reports were not written by the
+	 * hardware. This can happen for example if an MI_REPORT_PERF_COUNT
+	 * command collides with periodic sampling - which would be more likely
+	 * at higher sampling frequencies.
+	 */
+	DRM_I915_PERF_RECORD_OA_REPORT_LOST = 2,
+
+	/**
+	 * An error occurred that resulted in all pending OA reports being lost.
+	 */
+	DRM_I915_PERF_RECORD_OA_BUFFER_LOST = 3,
+
 	DRM_I915_PERF_RECORD_MAX /* non-ABI */
 };
 
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 07/11] drm/i915: advertise available metrics via sysfs
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (5 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 06/11] drm/i915: Enable i915 perf stream for Haswell OA unit Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:20   ` Matthew Auld
  2016-09-14 14:19 ` [PATCH v5 08/11] drm/i915: Add dev.i915.perf_event_paranoid sysctl option Robert Bragg
                   ` (7 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

Each metric set is given a sysfs entry like:

/sys/class/drm/card0/metrics/<guid>/id

This allows userspace to enumerate the specific sets that are available
for the current system. The 'id' file contains an unsigned integer that
can be used to open the associated metric set via
DRM_IOCTL_I915_PERF_OPEN. The <guid> is a globally unique ID for a
specific OA unit register configuration that can be reliably used by
userspace as a key to lookup corresponding counter meta data and
normalization equations.

The guid registry is currently maintained as part of gputop along with
the XML metric set descriptions and code generation scripts, ref:

 https://github.com/rib/gputop
 > gputop-data/guids.xml
 > scripts/update-guids.py
 > gputop-data/oa-*.xml
 > scripts/i915-perf-kernelgen.py

 $ make -C gputop-data -f Makefile.xml SYSFS=1 WHITELIST=RenderBasic

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_drv.c    |  5 ++++
 drivers/gpu/drm/i915/i915_drv.h    |  4 +++
 drivers/gpu/drm/i915/i915_oa_hsw.c | 51 +++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/i915_oa_hsw.h |  4 +++
 drivers/gpu/drm/i915/i915_perf.c   | 52 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 116 insertions(+)

diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
index 14f22fc..8965df2 100644
--- a/drivers/gpu/drm/i915/i915_drv.c
+++ b/drivers/gpu/drm/i915/i915_drv.c
@@ -1125,6 +1125,9 @@ static void i915_driver_register(struct drm_i915_private *dev_priv)
 	if (drm_dev_register(dev, 0) == 0) {
 		i915_debugfs_register(dev_priv);
 		i915_setup_sysfs(dev_priv);
+
+		/* Depends on sysfs having been initialized */
+		i915_perf_register(dev_priv);
 	} else
 		DRM_ERROR("Failed to register driver for userspace access!\n");
 
@@ -1161,6 +1164,8 @@ static void i915_driver_unregister(struct drm_i915_private *dev_priv)
 	acpi_video_unregister();
 	intel_opregion_unregister(dev_priv);
 
+	i915_perf_unregister(dev_priv);
+
 	i915_teardown_sysfs(dev_priv);
 	i915_debugfs_unregister(dev_priv);
 	drm_dev_unregister(&dev_priv->drm);
diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index 551f078..f5ddf70 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -2141,6 +2141,8 @@ struct drm_i915_private {
 	struct {
 		bool initialized;
 
+		struct kobject *metrics_kobj;
+
 		struct mutex lock;
 		struct list_head streams;
 
@@ -3711,6 +3713,8 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
 /* i915_perf.c */
 extern void i915_perf_init(struct drm_i915_private *dev_priv);
 extern void i915_perf_fini(struct drm_i915_private *dev_priv);
+extern void i915_perf_register(struct drm_i915_private *dev_priv);
+extern void i915_perf_unregister(struct drm_i915_private *dev_priv);
 
 /* i915_suspend.c */
 extern int i915_save_state(struct drm_device *dev);
diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.c b/drivers/gpu/drm/i915/i915_oa_hsw.c
index eb5ceca..656334d 100644
--- a/drivers/gpu/drm/i915/i915_oa_hsw.c
+++ b/drivers/gpu/drm/i915/i915_oa_hsw.c
@@ -24,6 +24,8 @@
  *
  */
 
+#include <linux/sysfs.h>
+
 #include "i915_drv.h"
 
 enum metric_set_id {
@@ -141,3 +143,52 @@ int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv)
 		return -ENODEV;
 	}
 }
+
+static ssize_t
+show_render_basic_id(struct device *kdev, struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", METRIC_SET_ID_RENDER_BASIC);
+}
+
+static struct device_attribute dev_attr_render_basic_id = {
+	.attr = { .name = "id", .mode = S_IRUGO },
+	.show = show_render_basic_id,
+	.store = NULL,
+};
+
+static struct attribute *attrs_render_basic[] = {
+	&dev_attr_render_basic_id.attr,
+	NULL,
+};
+
+static struct attribute_group group_render_basic = {
+	.name = "403d8832-1a27-4aa6-a64e-f5389ce7b212",
+	.attrs =  attrs_render_basic,
+};
+
+int
+i915_perf_register_sysfs_hsw(struct drm_i915_private *dev_priv)
+{
+	int mux_len;
+	int ret = 0;
+
+	if (get_render_basic_mux_config(dev_priv, &mux_len)) {
+		ret = sysfs_create_group(dev_priv->perf.metrics_kobj, &group_render_basic);
+		if (ret)
+			goto error_render_basic;
+	}
+
+	return 0;
+
+error_render_basic:
+	return ret;
+}
+
+void
+i915_perf_unregister_sysfs_hsw(struct drm_i915_private *dev_priv)
+{
+	int mux_len;
+
+	if (get_render_basic_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_render_basic);
+}
diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.h b/drivers/gpu/drm/i915/i915_oa_hsw.h
index b618a1f..429a229 100644
--- a/drivers/gpu/drm/i915/i915_oa_hsw.h
+++ b/drivers/gpu/drm/i915/i915_oa_hsw.h
@@ -31,4 +31,8 @@ extern int i915_oa_n_builtin_metric_sets_hsw;
 
 extern int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv);
 
+extern int i915_perf_register_sysfs_hsw(struct drm_i915_private *dev_priv);
+
+extern void i915_perf_unregister_sysfs_hsw(struct drm_i915_private *dev_priv);
+
 #endif
diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
index 5305982..e890c38 100644
--- a/drivers/gpu/drm/i915/i915_perf.c
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -743,6 +743,15 @@ static int i915_oa_stream_init(struct i915_perf_stream *stream,
 	int format_size;
 	int ret;
 
+	/* If the sysfs metrics/ directory wasn't registered for some
+	 * reason then don't let userspace try their luck with config
+	 * IDs
+	 */
+	if (!dev_priv->perf.metrics_kobj) {
+		DRM_ERROR("OA metrics weren't advertised via sysfs\n");
+		return -EINVAL;
+	}
+
 	if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
 		DRM_ERROR("Only OA report sampling supported\n");
 		return -EINVAL;
@@ -1365,6 +1374,49 @@ int i915_perf_open_ioctl(struct drm_device *dev, void *data,
 	return ret;
 }
 
+void i915_perf_register(struct drm_i915_private *dev_priv)
+{
+	if (!IS_HASWELL(dev_priv))
+		return;
+
+	if (!dev_priv->perf.initialized)
+		return;
+
+	/* To be sure we're synchronized with an attempted
+	 * i915_perf_open_ioctl(); considering that we register after
+	 * being exposed to userspace.
+	 */
+	mutex_lock(&dev_priv->perf.lock);
+
+	dev_priv->perf.metrics_kobj =
+		kobject_create_and_add("metrics",
+				       &dev_priv->drm.primary->kdev->kobj);
+	if (!dev_priv->perf.metrics_kobj)
+		goto exit;
+
+	if (i915_perf_register_sysfs_hsw(dev_priv)) {
+		kobject_put(dev_priv->perf.metrics_kobj);
+		dev_priv->perf.metrics_kobj = NULL;
+	}
+
+exit:
+	mutex_unlock(&dev_priv->perf.lock);
+}
+
+void i915_perf_unregister(struct drm_i915_private *dev_priv)
+{
+	if (!IS_HASWELL(dev_priv))
+		return;
+
+	if (!dev_priv->perf.metrics_kobj)
+		return;
+
+	i915_perf_unregister_sysfs_hsw(dev_priv);
+
+	kobject_put(dev_priv->perf.metrics_kobj);
+	dev_priv->perf.metrics_kobj = NULL;
+}
+
 void i915_perf_init(struct drm_i915_private *dev_priv)
 {
 	if (!IS_HASWELL(dev_priv))
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 08/11] drm/i915: Add dev.i915.perf_event_paranoid sysctl option
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (6 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 07/11] drm/i915: advertise available metrics via sysfs Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:21   ` Matthew Auld
  2016-09-14 14:19 ` [PATCH v5 09/11] drm/i915: add oa_event_min_timer_exponent sysctl Robert Bragg
                   ` (6 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: David Airlie, dri-devel, Sourab Gupta, Daniel Vetter

Consistent with the kernel.perf_event_paranoid sysctl option that can
allow non-root users to access system wide cpu metrics, this can
optionally allow non-root users to access system wide OA counter metrics
from Gen graphics hardware.

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_drv.h  |  1 +
 drivers/gpu/drm/i915/i915_perf.c | 45 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index f5ddf70..eaba7a9 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -2142,6 +2142,7 @@ struct drm_i915_private {
 		bool initialized;
 
 		struct kobject *metrics_kobj;
+		struct ctl_table_header *sysctl_header;
 
 		struct mutex lock;
 		struct list_head streams;
diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
index e890c38..38b13fa 100644
--- a/drivers/gpu/drm/i915/i915_perf.c
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -62,6 +62,8 @@
 #define POLL_FREQUENCY 200
 #define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
 
+static u32 i915_perf_stream_paranoid = true;
+
 /* The maximum exponent the hardware accepts is 63 (essentially it selects one
  * of the 64bit timestamp bits to trigger reports from) but there's currently
  * no known use case for sampling as infrequently as once per 47 thousand years.
@@ -1170,7 +1172,13 @@ int i915_perf_open_ioctl_locked(struct drm_device *dev,
 		}
 	}
 
-	if (!specific_ctx && !capable(CAP_SYS_ADMIN)) {
+	/* Similar to perf's kernel.perf_paranoid_cpu sysctl option
+	 * we check a dev.i915.perf_stream_paranoid sysctl option
+	 * to determine if it's ok to access system wide OA counters
+	 * without CAP_SYS_ADMIN privileges.
+	 */
+	if (!specific_ctx &&
+	    i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
 		DRM_ERROR("Insufficient privileges to open system-wide i915 perf stream\n");
 		ret = -EACCES;
 		goto err_ctx;
@@ -1417,6 +1425,37 @@ void i915_perf_unregister(struct drm_i915_private *dev_priv)
 	dev_priv->perf.metrics_kobj = NULL;
 }
 
+static struct ctl_table oa_table[] = {
+	{
+	 .procname = "perf_stream_paranoid",
+	 .data = &i915_perf_stream_paranoid,
+	 .maxlen = sizeof(i915_perf_stream_paranoid),
+	 .mode = 0644,
+	 .proc_handler = proc_dointvec,
+	 },
+	{}
+};
+
+static struct ctl_table i915_root[] = {
+	{
+	 .procname = "i915",
+	 .maxlen = 0,
+	 .mode = 0555,
+	 .child = oa_table,
+	 },
+	{}
+};
+
+static struct ctl_table dev_root[] = {
+	{
+	 .procname = "dev",
+	 .maxlen = 0,
+	 .mode = 0555,
+	 .child = i915_root,
+	 },
+	{}
+};
+
 void i915_perf_init(struct drm_i915_private *dev_priv)
 {
 	if (!IS_HASWELL(dev_priv))
@@ -1449,6 +1488,8 @@ void i915_perf_init(struct drm_i915_private *dev_priv)
 	dev_priv->perf.oa.n_builtin_sets =
 		i915_oa_n_builtin_metric_sets_hsw;
 
+	dev_priv->perf.sysctl_header = register_sysctl_table(dev_root);
+
 	dev_priv->perf.initialized = true;
 }
 
@@ -1457,6 +1498,8 @@ void i915_perf_fini(struct drm_i915_private *dev_priv)
 	if (!dev_priv->perf.initialized)
 		return;
 
+	unregister_sysctl_table(dev_priv->perf.sysctl_header);
+
 	memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops));
 	dev_priv->perf.initialized = false;
 }
-- 
2.9.2

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 09/11] drm/i915: add oa_event_min_timer_exponent sysctl
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (7 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 08/11] drm/i915: Add dev.i915.perf_event_paranoid sysctl option Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:21   ` Matthew Auld
  2016-09-14 14:19 ` [PATCH v5 10/11] drm/i915: Add more Haswell OA metric sets Robert Bragg
                   ` (5 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

The minimal sampling period is now configurable via a
dev.i915.oa_min_timer_exponent sysctl parameter.

Following the precedent set by perf, the default is the minimum that
won't (on its own) exceed the default kernel.perf_event_max_sample_rate
default of 100000 samples/s.

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_perf.c | 42 ++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
index 38b13fa..a7a248b 100644
--- a/drivers/gpu/drm/i915/i915_perf.c
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -74,6 +74,23 @@ static u32 i915_perf_stream_paranoid = true;
  */
 #define OA_EXPONENT_MAX 31
 
+/* for sysctl proc_dointvec_minmax of i915_oa_min_timer_exponent */
+static int zero;
+static int oa_exponent_max = OA_EXPONENT_MAX;
+
+/* Theoretically we can program the OA unit to sample every 160ns but don't
+ * allow that by default unless root...
+ *
+ * The period is derived from the exponent as:
+ *
+ *   period = 80ns * 2^(exponent + 1)
+ *
+ * Referring to perf's kernel.perf_event_max_sample_rate for a precedent
+ * (100000 by default); with an OA exponent of 6 we get a period of 10.240
+ * microseconds - just under 100000Hz
+ */
+static u32 i915_oa_min_timer_exponent = 6;
+
 /* XXX: beware if future OA HW adds new report formats that the current
  * code assumes all reports have a power-of-two size and ~(size - 1) can
  * be used as a mask to align the OA tail pointer.
@@ -1315,21 +1332,13 @@ static int read_properties_unlocked(struct drm_i915_private *dev_priv,
 				return -EINVAL;
 			}
 
-			/* NB: The exponent represents a period as follows:
-			 *
-			 *   80ns * 2^(period_exponent + 1)
-			 *
-			 * Theoretically we can program the OA unit to sample
+			/* Theoretically we can program the OA unit to sample
 			 * every 160ns but don't allow that by default unless
 			 * root.
-			 *
-			 * Referring to perf's
-			 * kernel.perf_event_max_sample_rate for a precedent
-			 * (100000 by default); with an OA exponent of 6 we get
-			 * a period of 10.240 microseconds -just under 100000Hz
 			 */
-			if (value < 6 && !capable(CAP_SYS_ADMIN)) {
-				DRM_ERROR("Sampling period too high without root privileges\n");
+			if (value < i915_oa_min_timer_exponent &&
+			    !capable(CAP_SYS_ADMIN)) {
+				DRM_ERROR("OA timer exponent too low without root privileges\n");
 				return -EACCES;
 			}
 
@@ -1433,6 +1442,15 @@ static struct ctl_table oa_table[] = {
 	 .mode = 0644,
 	 .proc_handler = proc_dointvec,
 	 },
+	{
+	 .procname = "oa_min_timer_exponent",
+	 .data = &i915_oa_min_timer_exponent,
+	 .maxlen = sizeof(i915_oa_min_timer_exponent),
+	 .mode = 0644,
+	 .proc_handler = proc_dointvec_minmax,
+	 .extra1 = &zero,
+	 .extra2 = &oa_exponent_max,
+	 },
 	{}
 };
 
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 10/11] drm/i915: Add more Haswell OA metric sets
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (8 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 09/11] drm/i915: add oa_event_min_timer_exponent sysctl Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:22   ` Matthew Auld
  2016-09-14 14:19 ` [PATCH v5 11/11] drm/i915: Add a kerneldoc summary for i915_perf.c Robert Bragg
                   ` (4 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

This adds 'compute', 'compute extended', 'memory reads', 'memory writes'
and 'sampler balance' metric sets for Haswell.

The code is auto generated from an XML description of metric sets,
currently maintained in gputop, ref:

 https://github.com/rib/gputop
 > gputop-data/oa-*.xml
 > scripts/i915-perf-kernelgen.py

 $ make -C gputop-data -f Makefile.xml

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_oa_hsw.c | 559 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 558 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.c b/drivers/gpu/drm/i915/i915_oa_hsw.c
index 656334d..7906a26 100644
--- a/drivers/gpu/drm/i915/i915_oa_hsw.c
+++ b/drivers/gpu/drm/i915/i915_oa_hsw.c
@@ -30,9 +30,14 @@
 
 enum metric_set_id {
 	METRIC_SET_ID_RENDER_BASIC = 1,
+	METRIC_SET_ID_COMPUTE_BASIC,
+	METRIC_SET_ID_COMPUTE_EXTENDED,
+	METRIC_SET_ID_MEMORY_READS,
+	METRIC_SET_ID_MEMORY_WRITES,
+	METRIC_SET_ID_SAMPLER_BALANCE,
 };
 
-int i915_oa_n_builtin_metric_sets_hsw = 1;
+int i915_oa_n_builtin_metric_sets_hsw = 6;
 
 static const struct i915_oa_reg b_counter_config_render_basic[] = {
 	{ _MMIO(0x2724), 0x00800000 },
@@ -111,6 +116,298 @@ get_render_basic_mux_config(struct drm_i915_private *dev_priv,
 	return mux_config_render_basic;
 }
 
+static const struct i915_oa_reg b_counter_config_compute_basic[] = {
+	{ _MMIO(0x2710), 0x00000000 },
+	{ _MMIO(0x2714), 0x00800000 },
+	{ _MMIO(0x2718), 0xaaaaaaaa },
+	{ _MMIO(0x271c), 0xaaaaaaaa },
+	{ _MMIO(0x2720), 0x00000000 },
+	{ _MMIO(0x2724), 0x00800000 },
+	{ _MMIO(0x2728), 0xaaaaaaaa },
+	{ _MMIO(0x272c), 0xaaaaaaaa },
+	{ _MMIO(0x2740), 0x00000000 },
+	{ _MMIO(0x2744), 0x00000000 },
+	{ _MMIO(0x2748), 0x00000000 },
+	{ _MMIO(0x274c), 0x00000000 },
+	{ _MMIO(0x2750), 0x00000000 },
+	{ _MMIO(0x2754), 0x00000000 },
+	{ _MMIO(0x2758), 0x00000000 },
+	{ _MMIO(0x275c), 0x00000000 },
+	{ _MMIO(0x236c), 0x00000000 },
+};
+
+static const struct i915_oa_reg mux_config_compute_basic[] = {
+	{ _MMIO(0x253a4), 0x00000000 },
+	{ _MMIO(0x2681c), 0x01f00800 },
+	{ _MMIO(0x26820), 0x00001000 },
+	{ _MMIO(0x2781c), 0x01f00800 },
+	{ _MMIO(0x26520), 0x00000007 },
+	{ _MMIO(0x265a0), 0x00000007 },
+	{ _MMIO(0x25380), 0x00000010 },
+	{ _MMIO(0x2538c), 0x00300000 },
+	{ _MMIO(0x25384), 0xaa8aaaaa },
+	{ _MMIO(0x25404), 0xffffffff },
+	{ _MMIO(0x26800), 0x00004202 },
+	{ _MMIO(0x26808), 0x00605817 },
+	{ _MMIO(0x2680c), 0x10001005 },
+	{ _MMIO(0x26804), 0x00000000 },
+	{ _MMIO(0x27800), 0x00000102 },
+	{ _MMIO(0x27808), 0x0c0701e0 },
+	{ _MMIO(0x2780c), 0x000200a0 },
+	{ _MMIO(0x27804), 0x00000000 },
+	{ _MMIO(0x26484), 0x44000000 },
+	{ _MMIO(0x26704), 0x44000000 },
+	{ _MMIO(0x26500), 0x00000006 },
+	{ _MMIO(0x26510), 0x00000001 },
+	{ _MMIO(0x26504), 0x88000000 },
+	{ _MMIO(0x26580), 0x00000006 },
+	{ _MMIO(0x26590), 0x00000020 },
+	{ _MMIO(0x26584), 0x00000000 },
+	{ _MMIO(0x26104), 0x55822222 },
+	{ _MMIO(0x26184), 0xaa866666 },
+	{ _MMIO(0x25420), 0x08320c83 },
+	{ _MMIO(0x25424), 0x06820c83 },
+	{ _MMIO(0x2541c), 0x00000000 },
+	{ _MMIO(0x25428), 0x00000c03 },
+};
+
+static const struct i915_oa_reg *
+get_compute_basic_mux_config(struct drm_i915_private *dev_priv,
+			     int *len)
+{
+	*len = ARRAY_SIZE(mux_config_compute_basic);
+	return mux_config_compute_basic;
+}
+
+static const struct i915_oa_reg b_counter_config_compute_extended[] = {
+	{ _MMIO(0x2724), 0xf0800000 },
+	{ _MMIO(0x2720), 0x00000000 },
+	{ _MMIO(0x2714), 0xf0800000 },
+	{ _MMIO(0x2710), 0x00000000 },
+	{ _MMIO(0x2770), 0x0007fe2a },
+	{ _MMIO(0x2774), 0x0000ff00 },
+	{ _MMIO(0x2778), 0x0007fe6a },
+	{ _MMIO(0x277c), 0x0000ff00 },
+	{ _MMIO(0x2780), 0x0007fe92 },
+	{ _MMIO(0x2784), 0x0000ff00 },
+	{ _MMIO(0x2788), 0x0007fea2 },
+	{ _MMIO(0x278c), 0x0000ff00 },
+	{ _MMIO(0x2790), 0x0007fe32 },
+	{ _MMIO(0x2794), 0x0000ff00 },
+	{ _MMIO(0x2798), 0x0007fe9a },
+	{ _MMIO(0x279c), 0x0000ff00 },
+	{ _MMIO(0x27a0), 0x0007ff23 },
+	{ _MMIO(0x27a4), 0x0000ff00 },
+	{ _MMIO(0x27a8), 0x0007fff3 },
+	{ _MMIO(0x27ac), 0x0000fffe },
+};
+
+static const struct i915_oa_reg mux_config_compute_extended[] = {
+	{ _MMIO(0x2681c), 0x3eb00800 },
+	{ _MMIO(0x26820), 0x00900000 },
+	{ _MMIO(0x25384), 0x02aaaaaa },
+	{ _MMIO(0x25404), 0x03ffffff },
+	{ _MMIO(0x26800), 0x00142284 },
+	{ _MMIO(0x26808), 0x0e629062 },
+	{ _MMIO(0x2680c), 0x3f6f55cb },
+	{ _MMIO(0x26810), 0x00000014 },
+	{ _MMIO(0x26804), 0x00000000 },
+	{ _MMIO(0x26104), 0x02aaaaaa },
+	{ _MMIO(0x26184), 0x02aaaaaa },
+	{ _MMIO(0x25420), 0x00000000 },
+	{ _MMIO(0x25424), 0x00000000 },
+	{ _MMIO(0x2541c), 0x00000000 },
+	{ _MMIO(0x25428), 0x00000000 },
+};
+
+static const struct i915_oa_reg *
+get_compute_extended_mux_config(struct drm_i915_private *dev_priv,
+				int *len)
+{
+	*len = ARRAY_SIZE(mux_config_compute_extended);
+	return mux_config_compute_extended;
+}
+
+static const struct i915_oa_reg b_counter_config_memory_reads[] = {
+	{ _MMIO(0x2724), 0xf0800000 },
+	{ _MMIO(0x2720), 0x00000000 },
+	{ _MMIO(0x2714), 0xf0800000 },
+	{ _MMIO(0x2710), 0x00000000 },
+	{ _MMIO(0x274c), 0x76543298 },
+	{ _MMIO(0x2748), 0x98989898 },
+	{ _MMIO(0x2744), 0x000000e4 },
+	{ _MMIO(0x2740), 0x00000000 },
+	{ _MMIO(0x275c), 0x98a98a98 },
+	{ _MMIO(0x2758), 0x88888888 },
+	{ _MMIO(0x2754), 0x000c5500 },
+	{ _MMIO(0x2750), 0x00000000 },
+	{ _MMIO(0x2770), 0x0007f81a },
+	{ _MMIO(0x2774), 0x0000fc00 },
+	{ _MMIO(0x2778), 0x0007f82a },
+	{ _MMIO(0x277c), 0x0000fc00 },
+	{ _MMIO(0x2780), 0x0007f872 },
+	{ _MMIO(0x2784), 0x0000fc00 },
+	{ _MMIO(0x2788), 0x0007f8ba },
+	{ _MMIO(0x278c), 0x0000fc00 },
+	{ _MMIO(0x2790), 0x0007f87a },
+	{ _MMIO(0x2794), 0x0000fc00 },
+	{ _MMIO(0x2798), 0x0007f8ea },
+	{ _MMIO(0x279c), 0x0000fc00 },
+	{ _MMIO(0x27a0), 0x0007f8e2 },
+	{ _MMIO(0x27a4), 0x0000fc00 },
+	{ _MMIO(0x27a8), 0x0007f8f2 },
+	{ _MMIO(0x27ac), 0x0000fc00 },
+};
+
+static const struct i915_oa_reg mux_config_memory_reads[] = {
+	{ _MMIO(0x253a4), 0x34300000 },
+	{ _MMIO(0x25440), 0x2d800000 },
+	{ _MMIO(0x25444), 0x00000008 },
+	{ _MMIO(0x25128), 0x0e600000 },
+	{ _MMIO(0x25380), 0x00000450 },
+	{ _MMIO(0x25390), 0x00052c43 },
+	{ _MMIO(0x25384), 0x00000000 },
+	{ _MMIO(0x25400), 0x00006144 },
+	{ _MMIO(0x25408), 0x0a418820 },
+	{ _MMIO(0x2540c), 0x000820e6 },
+	{ _MMIO(0x25404), 0xff500000 },
+	{ _MMIO(0x25100), 0x000005d6 },
+	{ _MMIO(0x2510c), 0x0ef00000 },
+	{ _MMIO(0x25104), 0x00000000 },
+	{ _MMIO(0x25420), 0x02108421 },
+	{ _MMIO(0x25424), 0x00008421 },
+	{ _MMIO(0x2541c), 0x00000000 },
+	{ _MMIO(0x25428), 0x00000000 },
+};
+
+static const struct i915_oa_reg *
+get_memory_reads_mux_config(struct drm_i915_private *dev_priv,
+			    int *len)
+{
+	*len = ARRAY_SIZE(mux_config_memory_reads);
+	return mux_config_memory_reads;
+}
+
+static const struct i915_oa_reg b_counter_config_memory_writes[] = {
+	{ _MMIO(0x2724), 0xf0800000 },
+	{ _MMIO(0x2720), 0x00000000 },
+	{ _MMIO(0x2714), 0xf0800000 },
+	{ _MMIO(0x2710), 0x00000000 },
+	{ _MMIO(0x274c), 0x76543298 },
+	{ _MMIO(0x2748), 0x98989898 },
+	{ _MMIO(0x2744), 0x000000e4 },
+	{ _MMIO(0x2740), 0x00000000 },
+	{ _MMIO(0x275c), 0xbabababa },
+	{ _MMIO(0x2758), 0x88888888 },
+	{ _MMIO(0x2754), 0x000c5500 },
+	{ _MMIO(0x2750), 0x00000000 },
+	{ _MMIO(0x2770), 0x0007f81a },
+	{ _MMIO(0x2774), 0x0000fc00 },
+	{ _MMIO(0x2778), 0x0007f82a },
+	{ _MMIO(0x277c), 0x0000fc00 },
+	{ _MMIO(0x2780), 0x0007f822 },
+	{ _MMIO(0x2784), 0x0000fc00 },
+	{ _MMIO(0x2788), 0x0007f8ba },
+	{ _MMIO(0x278c), 0x0000fc00 },
+	{ _MMIO(0x2790), 0x0007f87a },
+	{ _MMIO(0x2794), 0x0000fc00 },
+	{ _MMIO(0x2798), 0x0007f8ea },
+	{ _MMIO(0x279c), 0x0000fc00 },
+	{ _MMIO(0x27a0), 0x0007f8e2 },
+	{ _MMIO(0x27a4), 0x0000fc00 },
+	{ _MMIO(0x27a8), 0x0007f8f2 },
+	{ _MMIO(0x27ac), 0x0000fc00 },
+};
+
+static const struct i915_oa_reg mux_config_memory_writes[] = {
+	{ _MMIO(0x253a4), 0x34300000 },
+	{ _MMIO(0x25440), 0x01500000 },
+	{ _MMIO(0x25444), 0x00000120 },
+	{ _MMIO(0x25128), 0x0c200000 },
+	{ _MMIO(0x25380), 0x00000450 },
+	{ _MMIO(0x25390), 0x00052c43 },
+	{ _MMIO(0x25384), 0x00000000 },
+	{ _MMIO(0x25400), 0x00007184 },
+	{ _MMIO(0x25408), 0x0a418820 },
+	{ _MMIO(0x2540c), 0x000820e6 },
+	{ _MMIO(0x25404), 0xff500000 },
+	{ _MMIO(0x25100), 0x000005d6 },
+	{ _MMIO(0x2510c), 0x1e700000 },
+	{ _MMIO(0x25104), 0x00000000 },
+	{ _MMIO(0x25420), 0x02108421 },
+	{ _MMIO(0x25424), 0x00008421 },
+	{ _MMIO(0x2541c), 0x00000000 },
+	{ _MMIO(0x25428), 0x00000000 },
+};
+
+static const struct i915_oa_reg *
+get_memory_writes_mux_config(struct drm_i915_private *dev_priv,
+			     int *len)
+{
+	*len = ARRAY_SIZE(mux_config_memory_writes);
+	return mux_config_memory_writes;
+}
+
+static const struct i915_oa_reg b_counter_config_sampler_balance[] = {
+	{ _MMIO(0x2740), 0x00000000 },
+	{ _MMIO(0x2744), 0x00800000 },
+	{ _MMIO(0x2710), 0x00000000 },
+	{ _MMIO(0x2714), 0x00800000 },
+	{ _MMIO(0x2720), 0x00000000 },
+	{ _MMIO(0x2724), 0x00800000 },
+};
+
+static const struct i915_oa_reg mux_config_sampler_balance[] = {
+	{ _MMIO(0x2eb9c), 0x01906400 },
+	{ _MMIO(0x2fb9c), 0x01906400 },
+	{ _MMIO(0x253a4), 0x00000000 },
+	{ _MMIO(0x26b9c), 0x01906400 },
+	{ _MMIO(0x27b9c), 0x01906400 },
+	{ _MMIO(0x27104), 0x00a00000 },
+	{ _MMIO(0x27184), 0x00a50000 },
+	{ _MMIO(0x2e804), 0x00500000 },
+	{ _MMIO(0x2e984), 0x00500000 },
+	{ _MMIO(0x2eb04), 0x00500000 },
+	{ _MMIO(0x2eb80), 0x00000084 },
+	{ _MMIO(0x2eb8c), 0x14200000 },
+	{ _MMIO(0x2eb84), 0x00000000 },
+	{ _MMIO(0x2f804), 0x00050000 },
+	{ _MMIO(0x2f984), 0x00050000 },
+	{ _MMIO(0x2fb04), 0x00050000 },
+	{ _MMIO(0x2fb80), 0x00000084 },
+	{ _MMIO(0x2fb8c), 0x00050800 },
+	{ _MMIO(0x2fb84), 0x00000000 },
+	{ _MMIO(0x25380), 0x00000010 },
+	{ _MMIO(0x2538c), 0x000000c0 },
+	{ _MMIO(0x25384), 0xaa550000 },
+	{ _MMIO(0x25404), 0xffffc000 },
+	{ _MMIO(0x26804), 0x50000000 },
+	{ _MMIO(0x26984), 0x50000000 },
+	{ _MMIO(0x26b04), 0x50000000 },
+	{ _MMIO(0x26b80), 0x00000084 },
+	{ _MMIO(0x26b90), 0x00050800 },
+	{ _MMIO(0x26b84), 0x00000000 },
+	{ _MMIO(0x27804), 0x05000000 },
+	{ _MMIO(0x27984), 0x05000000 },
+	{ _MMIO(0x27b04), 0x05000000 },
+	{ _MMIO(0x27b80), 0x00000084 },
+	{ _MMIO(0x27b90), 0x00000142 },
+	{ _MMIO(0x27b84), 0x00000000 },
+	{ _MMIO(0x26104), 0xa0000000 },
+	{ _MMIO(0x26184), 0xa5000000 },
+	{ _MMIO(0x25424), 0x00008620 },
+	{ _MMIO(0x2541c), 0x00000000 },
+	{ _MMIO(0x25428), 0x0004a54a },
+};
+
+static const struct i915_oa_reg *
+get_sampler_balance_mux_config(struct drm_i915_private *dev_priv,
+			       int *len)
+{
+	*len = ARRAY_SIZE(mux_config_sampler_balance);
+	return mux_config_sampler_balance;
+}
+
 int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv)
 {
 	dev_priv->perf.oa.mux_regs = NULL;
@@ -139,6 +436,106 @@ int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv)
 			ARRAY_SIZE(b_counter_config_render_basic);
 
 		return 0;
+	case METRIC_SET_ID_COMPUTE_BASIC:
+		dev_priv->perf.oa.mux_regs =
+			get_compute_basic_mux_config(dev_priv,
+						     &dev_priv->perf.oa.mux_regs_len);
+		if (!dev_priv->perf.oa.mux_regs) {
+			DRM_DEBUG_DRIVER("No suitable MUX config for \"COMPUTE_BASIC\" metric set");
+
+			/* EINVAL because *_register_sysfs already checked this
+			 * and so it wouldn't have been advertised so userspace and
+			 * so shouldn't have been requested
+			 */
+			return -EINVAL;
+		}
+
+		dev_priv->perf.oa.b_counter_regs =
+			b_counter_config_compute_basic;
+		dev_priv->perf.oa.b_counter_regs_len =
+			ARRAY_SIZE(b_counter_config_compute_basic);
+
+		return 0;
+	case METRIC_SET_ID_COMPUTE_EXTENDED:
+		dev_priv->perf.oa.mux_regs =
+			get_compute_extended_mux_config(dev_priv,
+							&dev_priv->perf.oa.mux_regs_len);
+		if (!dev_priv->perf.oa.mux_regs) {
+			DRM_DEBUG_DRIVER("No suitable MUX config for \"COMPUTE_EXTENDED\" metric set");
+
+			/* EINVAL because *_register_sysfs already checked this
+			 * and so it wouldn't have been advertised so userspace and
+			 * so shouldn't have been requested
+			 */
+			return -EINVAL;
+		}
+
+		dev_priv->perf.oa.b_counter_regs =
+			b_counter_config_compute_extended;
+		dev_priv->perf.oa.b_counter_regs_len =
+			ARRAY_SIZE(b_counter_config_compute_extended);
+
+		return 0;
+	case METRIC_SET_ID_MEMORY_READS:
+		dev_priv->perf.oa.mux_regs =
+			get_memory_reads_mux_config(dev_priv,
+						    &dev_priv->perf.oa.mux_regs_len);
+		if (!dev_priv->perf.oa.mux_regs) {
+			DRM_DEBUG_DRIVER("No suitable MUX config for \"MEMORY_READS\" metric set");
+
+			/* EINVAL because *_register_sysfs already checked this
+			 * and so it wouldn't have been advertised so userspace and
+			 * so shouldn't have been requested
+			 */
+			return -EINVAL;
+		}
+
+		dev_priv->perf.oa.b_counter_regs =
+			b_counter_config_memory_reads;
+		dev_priv->perf.oa.b_counter_regs_len =
+			ARRAY_SIZE(b_counter_config_memory_reads);
+
+		return 0;
+	case METRIC_SET_ID_MEMORY_WRITES:
+		dev_priv->perf.oa.mux_regs =
+			get_memory_writes_mux_config(dev_priv,
+						     &dev_priv->perf.oa.mux_regs_len);
+		if (!dev_priv->perf.oa.mux_regs) {
+			DRM_DEBUG_DRIVER("No suitable MUX config for \"MEMORY_WRITES\" metric set");
+
+			/* EINVAL because *_register_sysfs already checked this
+			 * and so it wouldn't have been advertised so userspace and
+			 * so shouldn't have been requested
+			 */
+			return -EINVAL;
+		}
+
+		dev_priv->perf.oa.b_counter_regs =
+			b_counter_config_memory_writes;
+		dev_priv->perf.oa.b_counter_regs_len =
+			ARRAY_SIZE(b_counter_config_memory_writes);
+
+		return 0;
+	case METRIC_SET_ID_SAMPLER_BALANCE:
+		dev_priv->perf.oa.mux_regs =
+			get_sampler_balance_mux_config(dev_priv,
+						       &dev_priv->perf.oa.mux_regs_len);
+		if (!dev_priv->perf.oa.mux_regs) {
+			DRM_DEBUG_DRIVER("No suitable MUX config for \"SAMPLER_BALANCE\" metric set");
+
+			/* EINVAL because *_register_sysfs already checked this
+			 * and so it wouldn't have been advertised so userspace and
+			 * so shouldn't have been requested
+			 */
+			return -EINVAL;
+		}
+
+		dev_priv->perf.oa.b_counter_regs =
+			b_counter_config_sampler_balance;
+		dev_priv->perf.oa.b_counter_regs_len =
+			ARRAY_SIZE(b_counter_config_sampler_balance);
+
+		return 0;
 	default:
 		return -ENODEV;
 	}
@@ -166,6 +563,116 @@ static struct attribute_group group_render_basic = {
 	.attrs =  attrs_render_basic,
 };
 
+static ssize_t
+show_compute_basic_id(struct device *kdev, struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", METRIC_SET_ID_COMPUTE_BASIC);
+}
+
+static struct device_attribute dev_attr_compute_basic_id = {
+	.attr = { .name = "id", .mode = S_IRUGO },
+	.show = show_compute_basic_id,
+	.store = NULL,
+};
+
+static struct attribute *attrs_compute_basic[] = {
+	&dev_attr_compute_basic_id.attr,
+	NULL,
+};
+
+static struct attribute_group group_compute_basic = {
+	.name = "39ad14bc-2380-45c4-91eb-fbcb3aa7ae7b",
+	.attrs =  attrs_compute_basic,
+};
+
+static ssize_t
+show_compute_extended_id(struct device *kdev, struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", METRIC_SET_ID_COMPUTE_EXTENDED);
+}
+
+static struct device_attribute dev_attr_compute_extended_id = {
+	.attr = { .name = "id", .mode = S_IRUGO },
+	.show = show_compute_extended_id,
+	.store = NULL,
+};
+
+static struct attribute *attrs_compute_extended[] = {
+	&dev_attr_compute_extended_id.attr,
+	NULL,
+};
+
+static struct attribute_group group_compute_extended = {
+	.name = "3865be28-6982-49fe-9494-e4d1b4795413",
+	.attrs =  attrs_compute_extended,
+};
+
+static ssize_t
+show_memory_reads_id(struct device *kdev, struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", METRIC_SET_ID_MEMORY_READS);
+}
+
+static struct device_attribute dev_attr_memory_reads_id = {
+	.attr = { .name = "id", .mode = S_IRUGO },
+	.show = show_memory_reads_id,
+	.store = NULL,
+};
+
+static struct attribute *attrs_memory_reads[] = {
+	&dev_attr_memory_reads_id.attr,
+	NULL,
+};
+
+static struct attribute_group group_memory_reads = {
+	.name = "bb5ed49b-2497-4095-94f6-26ba294db88a",
+	.attrs =  attrs_memory_reads,
+};
+
+static ssize_t
+show_memory_writes_id(struct device *kdev, struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", METRIC_SET_ID_MEMORY_WRITES);
+}
+
+static struct device_attribute dev_attr_memory_writes_id = {
+	.attr = { .name = "id", .mode = S_IRUGO },
+	.show = show_memory_writes_id,
+	.store = NULL,
+};
+
+static struct attribute *attrs_memory_writes[] = {
+	&dev_attr_memory_writes_id.attr,
+	NULL,
+};
+
+static struct attribute_group group_memory_writes = {
+	.name = "3358d639-9b5f-45ab-976d-9b08cbfc6240",
+	.attrs =  attrs_memory_writes,
+};
+
+static ssize_t
+show_sampler_balance_id(struct device *kdev, struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", METRIC_SET_ID_SAMPLER_BALANCE);
+}
+
+static struct device_attribute dev_attr_sampler_balance_id = {
+	.attr = { .name = "id", .mode = S_IRUGO },
+	.show = show_sampler_balance_id,
+	.store = NULL,
+};
+
+static struct attribute *attrs_sampler_balance[] = {
+	&dev_attr_sampler_balance_id.attr,
+	NULL,
+};
+
+static struct attribute_group group_sampler_balance = {
+	.name = "bc274488-b4b6-40c7-90da-b77d7ad16189",
+	.attrs =  attrs_sampler_balance,
+};
+
 int
 i915_perf_register_sysfs_hsw(struct drm_i915_private *dev_priv)
 {
@@ -177,9 +684,49 @@ i915_perf_register_sysfs_hsw(struct drm_i915_private *dev_priv)
 		if (ret)
 			goto error_render_basic;
 	}
+	if (get_compute_basic_mux_config(dev_priv, &mux_len)) {
+		ret = sysfs_create_group(dev_priv->perf.metrics_kobj, &group_compute_basic);
+		if (ret)
+			goto error_compute_basic;
+	}
+	if (get_compute_extended_mux_config(dev_priv, &mux_len)) {
+		ret = sysfs_create_group(dev_priv->perf.metrics_kobj, &group_compute_extended);
+		if (ret)
+			goto error_compute_extended;
+	}
+	if (get_memory_reads_mux_config(dev_priv, &mux_len)) {
+		ret = sysfs_create_group(dev_priv->perf.metrics_kobj, &group_memory_reads);
+		if (ret)
+			goto error_memory_reads;
+	}
+	if (get_memory_writes_mux_config(dev_priv, &mux_len)) {
+		ret = sysfs_create_group(dev_priv->perf.metrics_kobj, &group_memory_writes);
+		if (ret)
+			goto error_memory_writes;
+	}
+	if (get_sampler_balance_mux_config(dev_priv, &mux_len)) {
+		ret = sysfs_create_group(dev_priv->perf.metrics_kobj, &group_sampler_balance);
+		if (ret)
+			goto error_sampler_balance;
+	}
 
 	return 0;
 
+error_sampler_balance:
+	if (get_sampler_balance_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_memory_writes);
+error_memory_writes:
+	if (get_sampler_balance_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_memory_reads);
+error_memory_reads:
+	if (get_sampler_balance_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_compute_extended);
+error_compute_extended:
+	if (get_sampler_balance_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_compute_basic);
+error_compute_basic:
+	if (get_sampler_balance_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_render_basic);
 error_render_basic:
 	return ret;
 }
@@ -191,4 +738,14 @@ i915_perf_unregister_sysfs_hsw(struct drm_i915_private *dev_priv)
 
 	if (get_render_basic_mux_config(dev_priv, &mux_len))
 		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_render_basic);
+	if (get_compute_basic_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_compute_basic);
+	if (get_compute_extended_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_compute_extended);
+	if (get_memory_reads_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_memory_reads);
+	if (get_memory_writes_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_memory_writes);
+	if (get_sampler_balance_mux_config(dev_priv, &mux_len))
+		sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_sampler_balance);
 }
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* [PATCH v5 11/11] drm/i915: Add a kerneldoc summary for i915_perf.c
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (9 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 10/11] drm/i915: Add more Haswell OA metric sets Robert Bragg
@ 2016-09-14 14:19 ` Robert Bragg
  2016-10-07 17:24   ` Matthew Auld
  2016-09-14 14:50 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3) Patchwork
                   ` (3 subsequent siblings)
  14 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 14:19 UTC (permalink / raw)
  To: intel-gfx; +Cc: dri-devel, Sourab Gupta, Daniel Vetter, Robert Bragg

In particular this tries to capture for posterity some of the early
challenges we had with using the core perf infrastructure in case we
ever want to revisit adapting perf for device metrics.

Cc: Chris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/i915_perf.c | 163 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)

diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
index a7a248b..891efe6 100644
--- a/drivers/gpu/drm/i915/i915_perf.c
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -24,6 +24,169 @@
  *   Robert Bragg <robert@sixbynine.org>
  */
 
+
+/**
+ * DOC: i915 Perf, streaming API for GPU metrics
+ *
+ * Gen graphics supports a large number of performance counters that can help
+ * driver and application developers understand and optimize their use of the
+ * GPU.
+ *
+ * This i915 perf interface enables userspace to configure and open a file
+ * descriptor representing a stream of GPU metrics which can then be read() as
+ * a stream of sample records.
+ *
+ * The interface is particularly suited to exposing buffered metrics that are
+ * captured by DMA from the GPU, unsynchronized with and unrelated to the CPU.
+ *
+ * Streams representing a single context are accessible to applications with a
+ * corresponding drm file descriptor, such that OpenGL can use the interface
+ * without special privileges. Access to system-wide metrics requires root
+ * privileges by default, unless changed via the dev.i915.perf_event_paranoid
+ * sysctl option.
+ *
+ *
+ * The interface was initially inspired by the core Perf infrastructure but
+ * some notable differences are:
+ *
+ * i915 perf file descriptors represent a "stream" instead of an "event"; where
+ * a perf event primarily corresponds to a single 64bit value, while a stream
+ * might sample sets of tightly-coupled counters, depending on the
+ * configuration.  For example the Gen OA unit isn't designed to support
+ * orthogonal configurations of individual counters; it's configured for a set
+ * of related counters. Samples for an i915 perf stream capturing OA metrics
+ * will include a set of counter values packed in a compact HW specific format.
+ * The OA unit supports a number of different packing formats which can be
+ * selected by the user opening the stream. Perf has support for grouping
+ * events, but each event in the group is configured, validated and
+ * authenticated individually with separate system calls.
+ *
+ * i915 perf stream configurations are provided as an array of u64 (key,value)
+ * pairs, instead of a fixed struct with multiple miscellaneous config members,
+ * interleaved with event-type specific members.
+ *
+ * i915 perf doesn't support exposing metrics via an mmap'd circular buffer.
+ * The supported metrics are being written to memory by the GPU unsynchronized
+ * with the CPU, using HW specific packing formats for counter sets. Sometimes
+ * the constraints on HW configuration require reports to be filtered before it
+ * would be acceptable to expose them to unprivileged applications - to hide
+ * the metrics of other processes/contexts. For these use cases a read() based
+ * interface is a good fit, and provides an opportunity to filter data as it
+ * gets copied from the GPU mapped buffers to userspace buffers.
+ *
+ *
+ * Some notes regarding Linux Perf:
+ * --------------------------------
+ *
+ * The first prototype of this driver was based on the core perf
+ * infrastructure, and while we did make that mostly work, with some changes to
+ * perf, we found we were breaking or working around too many assumptions baked
+ * into perf's currently cpu centric design.
+ *
+ * In the end we didn't see a clear benefit to making perf's implementation and
+ * interface more complex by changing design assumptions while we knew we still
+ * wouldn't be able to use any existing perf based userspace tools.
+ *
+ * Also considering the Gen specific nature of the Observability hardware and
+ * how userspace will sometimes need to combine i915 perf OA metrics with
+ * side-band OA data captured via MI_REPORT_PERF_COUNT commands; we're
+ * expecting the interface to be used by a platform specific userspace such as
+ * OpenGL or tools. This is to say; we aren't inherently missing out on having
+ * a standard vendor/architecture agnostic interface by not using perf.
+ *
+ *
+ * For posterity, in case we might re-visit trying to adapt core perf to be
+ * better suited to exposing i915 metrics these were the main pain points we
+ * hit:
+ *
+ * - The perf based OA PMU driver broke some significant design assumptions:
+ *
+ *   Existing perf pmus are used for profiling work on a cpu and we were
+ *   introducing the idea of _IS_DEVICE pmus with different security
+ *   implications, the need to fake cpu-related data (such as user/kernel
+ *   registers) to fit with perf's current design, and adding _DEVICE records
+ *   as a way to forward device-specific status records.
+ *
+ *   The OA unit writes reports of counters into a circular buffer, without
+ *   involvement from the CPU, making our PMU driver the first of a kind.
+ *
+ *   Given the way we were periodically forward data from the GPU-mapped, OA
+ *   buffer to perf's buffer, those bursts of sample writes looked to perf like
+ *   we were sampling too fast and so we had to subvert its throttling checks.
+ *
+ *   Perf supports groups of counters and allows those to be read via
+ *   transactions internally but transactions currently seem designed to be
+ *   explicitly initiated from the cpu (say in response to a userspace read())
+ *   and while we could pull a report out of the OA buffer we can't
+ *   trigger a report from the cpu on demand.
+ *
+ *   Related to being report based; the OA counters are configured in HW as a
+ *   set while perf generally expects counter configurations to be orthogonal.
+ *   Although counters can be associated with a group leader as they are
+ *   opened, there's no clear precedent for being able to provide group-wide
+ *   configuration attributes (for example we want to let userspace choose the
+ *   OA unit report format used to capture all counters in a set, or specify a
+ *   GPU context to filter metrics on). We avoided using perf's grouping
+ *   feature and forwarded OA reports to userspace via perf's 'raw' sample
+ *   field. This suited our userspace well considering how coupled the counters
+ *   are when dealing with normalizing. It would be inconvenient to split
+ *   counters up into separate events, only to require userspace to recombine
+ *   them. For Mesa it's also convenient to be forwarded raw, periodic reports
+ *   for combining with the side-band raw reports it captures using
+ *   MI_REPORT_PERF_COUNT commands.
+ *
+ *   _ As a side note on perf's grouping feature; there was also some concern
+ *     that using PERF_FORMAT_GROUP as a way to pack together counter values
+ *     would quite drastically inflate our sample sizes, which would likely
+ *     lower the effective sampling resolutions we could use when the available
+ *     memory bandwidth is limited.
+ *
+ *     With the OA unit's report formats, counters are packed together as 32
+ *     or 40bit values, with the largest report size being 256 bytes.
+ *
+ *     PERF_FORMAT_GROUP values are 64bit, but there doesn't appear to be a
+ *     documented ordering to the values, implying PERF_FORMAT_ID must also be
+ *     used to add a 64bit ID before each value; giving 16 bytes per counter.
+ *
+ *   Related to counter orthogonality; we can't time share the OA unit, while
+ *   event scheduling is a central design idea within perf for allowing
+ *   userspace to open + enable more events than can be configured in HW at any
+ *   one time.  The OA unit is not designed to allow re-configuration while in
+ *   use. We can't reconfigure the OA unit without losing internal OA unit
+ *   state which we can't access explicitly to save and restore. Reconfiguring
+ *   the OA unit is also relatively slow, involving ~100 register writes. From
+ *   userspace Mesa also depends on a stable OA configuration when emitting
+ *   MI_REPORT_PERF_COUNT commands and importantly the OA unit can't be
+ *   disabled while there are outstanding MI_RPC commands lest we hang the
+ *   command streamer.
+ *
+ *   The contents of sample records aren't extensible by device drivers (i.e.
+ *   the sample_type bits). As an example; Sourab Gupta had been looking to
+ *   attach GPU timestamps to our OA samples. We were shoehorning OA reports
+ *   into sample records by using the 'raw' field, but it's tricky to pack more
+ *   than one thing into this field because events/core.c currently only lets a
+ *   pmu give a single raw data pointer plus len which will be copied into the
+ *   ring buffer. To include more than the OA report we'd have to copy the
+ *   report into an intermediate larger buffer. I'd been considering allowing a
+ *   vector of data+len values to be specified for copying the raw data, but
+ *   it felt like a kludge to being using the raw field for this purpose.
+ *
+ * - It felt like our perf based PMU was making some technical compromises
+ *   just for the sake of using perf:
+ *
+ *   perf_event_open() requires events to either relate to a pid or a specific
+ *   cpu core, while our device pmu related to neither.  Events opened with a
+ *   pid will be automatically enabled/disabled according to the scheduling of
+ *   that process - so not appropriate for us. When an event is related to a
+ *   cpu id, perf ensures pmu methods will be invoked via an inter process
+ *   interrupt on that core. To avoid invasive changes our userspace opened OA
+ *   perf events for a specific cpu. This was workable but it meant the
+ *   majority of the OA driver ran in atomic context, including all OA report
+ *   forwarding, which wasn't really necessary in our case and seems to make
+ *   our locking requirements somewhat complex as we handled the interaction
+ *   with the rest of the i915 driver.
+ */
+
 #include <linux/anon_inodes.h>
 #include <linux/sizes.h>
 
-- 
2.9.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* Re: [Intel-gfx] [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure
  2016-09-14 14:19 ` [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure Robert Bragg
@ 2016-09-14 14:42   ` Emil Velikov
  2016-09-14 15:02     ` Robert Bragg
  2016-09-14 15:32     ` [PATCH] " Robert Bragg
  0 siblings, 2 replies; 33+ messages in thread
From: Emil Velikov @ 2016-09-14 14:42 UTC (permalink / raw)
  To: Robert Bragg; +Cc: Daniel Vetter, intel-gfx, Sourab Gupta, ML dri-devel

Hi Robert,

I think I've spotted one interesting, yet trivial bit.

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> Adds base i915 perf infrastructure for Gen performance metrics.
>
> This adds a DRM_IOCTL_I915_PERF_OPEN ioctl that takes an array of uint64
> properties to configure a stream of metrics and returns a new fd usable
> with standard VFS system calls including read() to read typed and sized
> records; ioctl() to enable or disable capture and poll() to wait for
> data.
>
> A stream is opened something like:
>
>   uint64_t properties[] = {
>       /* Single context sampling */
>       DRM_I915_PERF_PROP_CTX_HANDLE,        ctx_handle,
>
>       /* Include OA reports in samples */
>       DRM_I915_PERF_PROP_SAMPLE_OA,         true,
>
>       /* OA unit configuration */
>       DRM_I915_PERF_PROP_OA_METRICS_SET,    metrics_set_id,
>       DRM_I915_PERF_PROP_OA_FORMAT,         report_format,
>       DRM_I915_PERF_PROP_OA_EXPONENT,       period_exponent,
>    };
>    struct drm_i915_perf_open_param parm = {
>       .flags = I915_PERF_FLAG_FD_CLOEXEC |
>                I915_PERF_FLAG_FD_NONBLOCK |
>                I915_PERF_FLAG_DISABLED,
>       .properties_ptr = (uint64_t)properties,
>       .num_properties = sizeof(properties) / 16,
>    };
>    int fd = drmIoctl(drm_fd, DRM_IOCTL_I915_PERF_OPEN, &param);
>
> Records read all start with a common { type, size } header with
> DRM_I915_PERF_RECORD_SAMPLE being of most interest. Sample records
> contain an extensible number of fields and it's the
> DRM_I915_PERF_PROP_SAMPLE_xyz properties given when opening that
> determine what's included in every sample.
>
If I'm understanding the above correctly the ioctl can only read user
data and does not write to params, correct ?

> --- a/include/uapi/drm/i915_drm.h
> +++ b/include/uapi/drm/i915_drm.h

> +#define DRM_IOCTL_I915_PERF_OPEN       DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_PERF_OPEN, struct drm_i915_perf_open_param)

If so, we seems to have a one letter too much in DRM_IOWR - should one
use DRM_IOW/DRM_IOR ? Then again I'm not sure how many ioctls bother,
so please don't read too much into my suggestion :-)

Regards,
Emil
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 33+ messages in thread

* ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3)
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (10 preceding siblings ...)
  2016-09-14 14:19 ` [PATCH v5 11/11] drm/i915: Add a kerneldoc summary for i915_perf.c Robert Bragg
@ 2016-09-14 14:50 ` Patchwork
  2016-09-14 16:19 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4) Patchwork
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 33+ messages in thread
From: Patchwork @ 2016-09-14 14:50 UTC (permalink / raw)
  To: Robert Bragg; +Cc: intel-gfx

== Series Details ==

Series: Enable i915 perf stream for Haswell OA unit (rev3)
URL   : https://patchwork.freedesktop.org/series/11295/
State : failure

== Summary ==

Series 11295v3 Enable i915 perf stream for Haswell OA unit
https://patchwork.freedesktop.org/api/1.0/series/11295/revisions/3/mbox/

Test drv_module_reload_basic:
                dmesg-warn -> PASS       (fi-skl-6770hq)
Test gem_exec_parse:
        Subgroup basic-rejected:
                pass       -> FAIL       (fi-ivb-3520m)
                pass       -> FAIL       (fi-ivb-3770)
                pass       -> FAIL       (fi-byt-n2820)
                pass       -> FAIL       (fi-byt-j1900)
                pass       -> FAIL       (fi-hsw-4770r)
                pass       -> FAIL       (fi-hsw-4770k)
Test gem_exec_suspend:
        Subgroup basic-s3:
                incomplete -> PASS       (fi-hsw-4770k)
Test kms_busy:
        Subgroup basic-flip-default-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-default-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-default-c:
                skip       -> PASS       (fi-skl-6770hq)
Test kms_cursor_legacy:
        Subgroup basic-flip-after-cursor-legacy:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-after-cursor-varying-size:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-before-cursor-legacy:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-before-cursor-varying-size:
                skip       -> PASS       (fi-skl-6770hq)
Test kms_flip:
        Subgroup basic-flip-vs-dpms:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-vs-modeset:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-vs-wf_vblank:
                skip       -> DMESG-WARN (fi-skl-6770hq)
        Subgroup basic-plain-flip:
                skip       -> PASS       (fi-skl-6770hq)
Test kms_frontbuffer_tracking:
        Subgroup basic:
                skip       -> FAIL       (fi-skl-6770hq)
Test kms_pipe_crc_basic:
        Subgroup hang-read-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup hang-read-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup hang-read-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-a-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-b-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-c-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-a-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-b-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-c-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup suspend-read-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup suspend-read-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup suspend-read-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
Test pm_rpm:
        Subgroup basic-pci-d3-state:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-rte:
                skip       -> DMESG-WARN (fi-skl-6770hq)
Test prime_vgem:
        Subgroup basic-fence-flip:
                skip       -> PASS       (fi-skl-6770hq)

fi-bdw-5557u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15 
fi-bsw-n3050     total:244  pass:202  dwarn:0   dfail:0   fail:0   skip:42 
fi-byt-j1900     total:244  pass:209  dwarn:2   dfail:0   fail:2   skip:31 
fi-byt-n2820     total:244  pass:207  dwarn:0   dfail:0   fail:2   skip:35 
fi-hsw-4770k     total:244  pass:225  dwarn:0   dfail:0   fail:1   skip:18 
fi-hsw-4770r     total:244  pass:221  dwarn:0   dfail:0   fail:1   skip:22 
fi-ilk-650       total:244  pass:183  dwarn:0   dfail:0   fail:1   skip:60 
fi-ivb-3520m     total:244  pass:218  dwarn:0   dfail:0   fail:1   skip:25 
fi-ivb-3770      total:244  pass:206  dwarn:0   dfail:0   fail:1   skip:37 
fi-skl-6260u     total:244  pass:230  dwarn:0   dfail:0   fail:0   skip:14 
WARNING: Long output truncated

Results at /archive/results/CI_IGT_test/Patchwork_2533/

9aa8c0cdbc076bcc0486d7a31922a0f77c032fe7 drm-intel-nightly: 2016y-09m-14d-09h-19m-25s UTC integration manifest
36985e3 drm/i915: Add a kerneldoc summary for i915_perf.c
9aab6b3 drm/i915: Add more Haswell OA metric sets
bdbb5e3 drm/i915: add oa_event_min_timer_exponent sysctl
bff8073 drm/i915: Add dev.i915.perf_event_paranoid sysctl option
c7d3b3b drm/i915: advertise available metrics via sysfs
c56f5bb drm/i915: Enable i915 perf stream for Haswell OA unit
182685f drm/i915: Add 'render basic' Haswell OA unit config
a48ac08 drm/i915: don't whitelist oacontrol in cmd parser
45f7ac0 drm/i915: return EACCES for check_cmd() failures
956313e drm/i915: rename OACONTROL GEN7_OACONTROL
db94420 drm/i915: Add i915 perf infrastructure

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure
  2016-09-14 14:42   ` [Intel-gfx] " Emil Velikov
@ 2016-09-14 15:02     ` Robert Bragg
  2016-09-14 15:32     ` [PATCH] " Robert Bragg
  1 sibling, 0 replies; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 15:02 UTC (permalink / raw)
  To: Emil Velikov
  Cc: David Airlie, Daniel Vetter, intel-gfx, Sourab Gupta, ML dri-devel


[-- Attachment #1.1: Type: text/plain, Size: 2915 bytes --]

On Wed, Sep 14, 2016 at 3:42 PM, Emil Velikov <emil.l.velikov@gmail.com>
wrote:

> Hi Robert,
>
> I think I've spotted one interesting, yet trivial bit.
>
> On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> > Adds base i915 perf infrastructure for Gen performance metrics.
> >
> > This adds a DRM_IOCTL_I915_PERF_OPEN ioctl that takes an array of uint64
> > properties to configure a stream of metrics and returns a new fd usable
> > with standard VFS system calls including read() to read typed and sized
> > records; ioctl() to enable or disable capture and poll() to wait for
> > data.
> >
> > A stream is opened something like:
> >
> >   uint64_t properties[] = {
> >       /* Single context sampling */
> >       DRM_I915_PERF_PROP_CTX_HANDLE,        ctx_handle,
> >
> >       /* Include OA reports in samples */
> >       DRM_I915_PERF_PROP_SAMPLE_OA,         true,
> >
> >       /* OA unit configuration */
> >       DRM_I915_PERF_PROP_OA_METRICS_SET,    metrics_set_id,
> >       DRM_I915_PERF_PROP_OA_FORMAT,         report_format,
> >       DRM_I915_PERF_PROP_OA_EXPONENT,       period_exponent,
> >    };
> >    struct drm_i915_perf_open_param parm = {
> >       .flags = I915_PERF_FLAG_FD_CLOEXEC |
> >                I915_PERF_FLAG_FD_NONBLOCK |
> >                I915_PERF_FLAG_DISABLED,
> >       .properties_ptr = (uint64_t)properties,
> >       .num_properties = sizeof(properties) / 16,
> >    };
> >    int fd = drmIoctl(drm_fd, DRM_IOCTL_I915_PERF_OPEN, &param);
> >
> > Records read all start with a common { type, size } header with
> > DRM_I915_PERF_RECORD_SAMPLE being of most interest. Sample records
> > contain an extensible number of fields and it's the
> > DRM_I915_PERF_PROP_SAMPLE_xyz properties given when opening that
> > determine what's included in every sample.
> >
> If I'm understanding the above correctly the ioctl can only read user
> data and does not write to params, correct ?
>
> > --- a/include/uapi/drm/i915_drm.h
> > +++ b/include/uapi/drm/i915_drm.h
>
> > +#define DRM_IOCTL_I915_PERF_OPEN       DRM_IOWR(DRM_COMMAND_BASE +
> DRM_I915_PERF_OPEN, struct drm_i915_perf_open_param)
>
> If so, we seems to have a one letter too much in DRM_IOWR - should one
> use DRM_IOW/DRM_IOR ? Then again I'm not sure how many ioctls bother,
> so please don't read too much into my suggestion :-)
>

Ah, yep, good catch, I don't write back to the param struct any more.

The first iteration of this interface was even more closely modeled on the
core linux perf interface where the param struct starts with a size member
and in a case where userspace passes a structure that's smaller than
expected the kernel returns an error but also writes back the expected size
to inform userspace.

i915 perf moved to taking an array of u64 properties and no longer writes
back a size member in the param struct like perf.

Thanks,
- Robert



>
> Regards,
> Emil
>

[-- Attachment #1.2: Type: text/html, Size: 4017 bytes --]

[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* [PATCH] drm/i915: Add i915 perf infrastructure
  2016-09-14 14:42   ` [Intel-gfx] " Emil Velikov
  2016-09-14 15:02     ` Robert Bragg
@ 2016-09-14 15:32     ` Robert Bragg
  2016-10-07 17:10       ` Matthew Auld
  1 sibling, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-09-14 15:32 UTC (permalink / raw)
  To: intel-gfx; +Cc: David Airlie, dri-devel, Sourab Gupta, Daniel Vetter

Adds base i915 perf infrastructure for Gen performance metrics.

This adds a DRM_IOCTL_I915_PERF_OPEN ioctl that takes an array of uint64
properties to configure a stream of metrics and returns a new fd usable
with standard VFS system calls including read() to read typed and sized
records; ioctl() to enable or disable capture and poll() to wait for
data.

A stream is opened something like:

  uint64_t properties[] = {
      /* Single context sampling */
      DRM_I915_PERF_PROP_CTX_HANDLE,        ctx_handle,

      /* Include OA reports in samples */
      DRM_I915_PERF_PROP_SAMPLE_OA,         true,

      /* OA unit configuration */
      DRM_I915_PERF_PROP_OA_METRICS_SET,    metrics_set_id,
      DRM_I915_PERF_PROP_OA_FORMAT,         report_format,
      DRM_I915_PERF_PROP_OA_EXPONENT,       period_exponent,
   };
   struct drm_i915_perf_open_param parm = {
      .flags = I915_PERF_FLAG_FD_CLOEXEC |
               I915_PERF_FLAG_FD_NONBLOCK |
               I915_PERF_FLAG_DISABLED,
      .properties_ptr = (uint64_t)properties,
      .num_properties = sizeof(properties) / 16,
   };
   int fd = drmIoctl(drm_fd, DRM_IOCTL_I915_PERF_OPEN, &param);

Records read all start with a common { type, size } header with
DRM_I915_PERF_RECORD_SAMPLE being of most interest. Sample records
contain an extensible number of fields and it's the
DRM_I915_PERF_PROP_SAMPLE_xyz properties given when opening that
determine what's included in every sample.

No specific streams are supported yet so any attempt to open a stream
will return an error.

v4:
    s/DRM_IORW/DRM_IOR/ - Emil Velikov
v3:
    update read() interface to avoid passing state struct - Chris Wilson
    fix some rebase fallout, with i915-perf init/deinit
v2:
    use i915_gem_context_get() - Chris Wilson

Signed-off-by: Robert Bragg <robert@sixbynine.org>
---
 drivers/gpu/drm/i915/Makefile    |   3 +
 drivers/gpu/drm/i915/i915_drv.c  |   4 +
 drivers/gpu/drm/i915/i915_drv.h  |  91 ++++++++
 drivers/gpu/drm/i915/i915_perf.c | 448 +++++++++++++++++++++++++++++++++++++++
 include/uapi/drm/i915_drm.h      |  67 ++++++
 5 files changed, 613 insertions(+)
 create mode 100644 drivers/gpu/drm/i915/i915_perf.c

diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
index a998c2b..d991781 100644
--- a/drivers/gpu/drm/i915/Makefile
+++ b/drivers/gpu/drm/i915/Makefile
@@ -110,6 +110,9 @@ i915-y += dvo_ch7017.o \
 # virtual gpu code
 i915-y += i915_vgpu.o
 
+# perf code
+i915-y += i915_perf.o
+
 ifeq ($(CONFIG_DRM_I915_GVT),y)
 i915-y += intel_gvt.o
 include $(src)/gvt/Makefile
diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
index 7f4e8ad..14f22fc 100644
--- a/drivers/gpu/drm/i915/i915_drv.c
+++ b/drivers/gpu/drm/i915/i915_drv.c
@@ -838,6 +838,8 @@ static int i915_driver_init_early(struct drm_i915_private *dev_priv,
 
 	intel_device_info_dump(dev_priv);
 
+	i915_perf_init(dev_priv);
+
 	/* Not all pre-production machines fall into this category, only the
 	 * very first ones. Almost everything should work, except for maybe
 	 * suspend/resume. And we don't implement workarounds that affect only
@@ -859,6 +861,7 @@ err_workqueues:
  */
 static void i915_driver_cleanup_early(struct drm_i915_private *dev_priv)
 {
+	i915_perf_fini(dev_priv);
 	i915_gem_load_cleanup(&dev_priv->drm);
 	i915_workqueues_cleanup(dev_priv);
 }
@@ -2560,6 +2563,7 @@ static const struct drm_ioctl_desc i915_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
 };
 
 static struct drm_driver driver = {
diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index 1e2dda8..0f5cd8f 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -1740,6 +1740,84 @@ struct intel_wm_config {
 	bool sprites_scaled;
 };
 
+struct i915_perf_stream;
+
+struct i915_perf_stream_ops {
+	/* Enables the collection of HW samples, either in response to
+	 * I915_PERF_IOCTL_ENABLE or implicitly called when stream is
+	 * opened without I915_PERF_FLAG_DISABLED.
+	 */
+	void (*enable)(struct i915_perf_stream *stream);
+
+	/* Disables the collection of HW samples, either in response to
+	 * I915_PERF_IOCTL_DISABLE or implicitly called before
+	 * destroying the stream.
+	 */
+	void (*disable)(struct i915_perf_stream *stream);
+
+	/* Return: true if any i915 perf records are ready to read()
+	 * for this stream.
+	 */
+	bool (*can_read)(struct i915_perf_stream *stream);
+
+	/* Call poll_wait, passing a wait queue that will be woken
+	 * once there is something ready to read() for the stream
+	 */
+	void (*poll_wait)(struct i915_perf_stream *stream,
+			  struct file *file,
+			  poll_table *wait);
+
+	/* For handling a blocking read, wait until there is something
+	 * to ready to read() for the stream. E.g. wait on the same
+	 * wait queue that would be passed to poll_wait() until
+	 * ->can_read() returns true (if its safe to call ->can_read()
+	 * without the i915 perf lock held).
+	 */
+	int (*wait_unlocked)(struct i915_perf_stream *stream);
+
+	/* read - Copy buffered metrics as records to userspace
+	 * @buf: the userspace, destination buffer
+	 * @count: the number of bytes to copy, requested by userspace
+	 * @offset: zero at the start of the read, updated as the read
+	 *          proceeds, it represents how many bytes have been
+	 *          copied so far and the buffer offset for copying the
+	 *          next record.
+	 *
+	 * Copy as many buffered i915 perf samples and records for
+	 * this stream to userspace as will fit in the given buffer.
+	 *
+	 * Only write complete records; returning -ENOSPC if there
+	 * isn't room for a complete record.
+	 *
+	 * Return any error condition that results in a short read
+	 * such as -ENOSPC or -EFAULT, even though these may be
+	 * squashed before returning to userspace.
+	 */
+	int (*read)(struct i915_perf_stream *stream,
+		    char __user *buf,
+		    size_t count,
+		    size_t *offset);
+
+	/* Cleanup any stream specific resources.
+	 *
+	 * The stream will always be disabled before this is called.
+	 */
+	void (*destroy)(struct i915_perf_stream *stream);
+};
+
+struct i915_perf_stream {
+	struct drm_i915_private *dev_priv;
+
+	struct list_head link;
+
+	u32 sample_flags;
+
+	struct i915_gem_context *ctx;
+	bool enabled;
+
+	struct i915_perf_stream_ops *ops;
+};
+
 struct drm_i915_private {
 	struct drm_device drm;
 
@@ -2040,6 +2118,12 @@ struct drm_i915_private {
 
 	struct i915_runtime_pm pm;
 
+	struct {
+		bool initialized;
+		struct mutex lock;
+		struct list_head streams;
+	} perf;
+
 	/* Abstract the submission mechanism (legacy ringbuffer or execlists) away */
 	struct {
 		void (*resume)(struct drm_i915_private *);
@@ -3445,6 +3529,9 @@ int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
 				       struct drm_file *file);
 
+int i915_perf_open_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *file);
+
 /* i915_gem_evict.c */
 int __must_check i915_gem_evict_something(struct i915_address_space *vm,
 					  u64 min_size, u64 alignment,
@@ -3555,6 +3642,10 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
 			    u32 batch_len,
 			    bool is_master);
 
+/* i915_perf.c */
+extern void i915_perf_init(struct drm_i915_private *dev_priv);
+extern void i915_perf_fini(struct drm_i915_private *dev_priv);
+
 /* i915_suspend.c */
 extern int i915_save_state(struct drm_device *dev);
 extern int i915_restore_state(struct drm_device *dev);
diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
new file mode 100644
index 0000000..87530f5
--- /dev/null
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -0,0 +1,448 @@
+/*
+ * Copyright © 2015-2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Robert Bragg <robert@sixbynine.org>
+ */
+
+#include <linux/anon_inodes.h>
+#include <linux/sizes.h>
+
+#include "i915_drv.h"
+
+struct perf_open_properties {
+	u32 sample_flags;
+
+	u64 single_context:1;
+	u64 ctx_handle;
+};
+
+static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
+				     struct file *file,
+				     char __user *buf,
+				     size_t count,
+				     loff_t *ppos)
+{
+	/* Note we keep the offset (aka bytes read) separate from any
+	 * error status so that the final check for whether we return
+	 * the bytes read with a higher precedence than any error (see
+	 * comment below) doesn't need to be handled/duplicated in
+	 * stream->ops->read() implementations.
+	 */
+	size_t offset = 0;
+	int ret = stream->ops->read(stream, buf, count, &offset);
+
+	/* If we've successfully copied any data then reporting that
+	 * takes precedence over any internal error status, so the
+	 * data isn't lost.
+	 *
+	 * For example ret will be -ENOSPC whenever there is more
+	 * buffered data than can be copied to userspace, but that's
+	 * only interesting if we weren't able to copy some data
+	 * because it implies the userspace buffer is too small to
+	 * receive a single record (and we never split records).
+	 *
+	 * Another case with ret == -EFAULT is more of a grey area
+	 * since it would seem like bad form for userspace to ask us
+	 * to overrun its buffer, but the user knows best:
+	 *
+	 *   http://yarchive.net/comp/linux/partial_reads_writes.html
+	 */
+	return offset ?: (ret ?: -EAGAIN);
+}
+
+static ssize_t i915_perf_read(struct file *file,
+			      char __user *buf,
+			      size_t count,
+			      loff_t *ppos)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	ssize_t ret;
+
+	if (!(file->f_flags & O_NONBLOCK)) {
+		/* Allow false positives from stream->ops->wait_unlocked.
+		 */
+		do {
+			ret = stream->ops->wait_unlocked(stream);
+			if (ret)
+				return ret;
+
+			mutex_lock(&dev_priv->perf.lock);
+			ret = i915_perf_read_locked(stream, file,
+						    buf, count, ppos);
+			mutex_unlock(&dev_priv->perf.lock);
+		} while (ret == -EAGAIN);
+	} else {
+		mutex_lock(&dev_priv->perf.lock);
+		ret = i915_perf_read_locked(stream, file, buf, count, ppos);
+		mutex_unlock(&dev_priv->perf.lock);
+	}
+
+	return ret;
+}
+
+static unsigned int i915_perf_poll_locked(struct i915_perf_stream *stream,
+					  struct file *file,
+					  poll_table *wait)
+{
+	unsigned int streams = 0;
+
+	stream->ops->poll_wait(stream, file, wait);
+
+	if (stream->ops->can_read(stream))
+		streams |= POLLIN;
+
+	return streams;
+}
+
+static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	int ret;
+
+	mutex_lock(&dev_priv->perf.lock);
+	ret = i915_perf_poll_locked(stream, file, wait);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return ret;
+}
+
+static void i915_perf_enable_locked(struct i915_perf_stream *stream)
+{
+	if (stream->enabled)
+		return;
+
+	/* Allow stream->ops->enable() to refer to this */
+	stream->enabled = true;
+
+	if (stream->ops->enable)
+		stream->ops->enable(stream);
+}
+
+static void i915_perf_disable_locked(struct i915_perf_stream *stream)
+{
+	if (!stream->enabled)
+		return;
+
+	/* Allow stream->ops->disable() to refer to this */
+	stream->enabled = false;
+
+	if (stream->ops->disable)
+		stream->ops->disable(stream);
+}
+
+static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
+				   unsigned int cmd,
+				   unsigned long arg)
+{
+	switch (cmd) {
+	case I915_PERF_IOCTL_ENABLE:
+		i915_perf_enable_locked(stream);
+		return 0;
+	case I915_PERF_IOCTL_DISABLE:
+		i915_perf_disable_locked(stream);
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static long i915_perf_ioctl(struct file *file,
+			    unsigned int cmd,
+			    unsigned long arg)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+	long ret;
+
+	mutex_lock(&dev_priv->perf.lock);
+	ret = i915_perf_ioctl_locked(stream, cmd, arg);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return ret;
+}
+
+static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
+{
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	if (stream->enabled)
+		i915_perf_disable_locked(stream);
+
+	if (stream->ops->destroy)
+		stream->ops->destroy(stream);
+
+	list_del(&stream->link);
+
+	if (stream->ctx) {
+		mutex_lock(&dev_priv->drm.struct_mutex);
+		i915_gem_context_put(stream->ctx);
+		mutex_unlock(&dev_priv->drm.struct_mutex);
+	}
+
+	kfree(stream);
+}
+
+static int i915_perf_release(struct inode *inode, struct file *file)
+{
+	struct i915_perf_stream *stream = file->private_data;
+	struct drm_i915_private *dev_priv = stream->dev_priv;
+
+	mutex_lock(&dev_priv->perf.lock);
+	i915_perf_destroy_locked(stream);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return 0;
+}
+
+
+static const struct file_operations fops = {
+	.owner		= THIS_MODULE,
+	.llseek		= no_llseek,
+	.release	= i915_perf_release,
+	.poll		= i915_perf_poll,
+	.read		= i915_perf_read,
+	.unlocked_ioctl	= i915_perf_ioctl,
+};
+
+
+static struct i915_gem_context *
+lookup_context(struct drm_i915_private *dev_priv,
+	       struct drm_i915_file_private *file_priv,
+	       u32 ctx_user_handle)
+{
+	struct i915_gem_context *ctx;
+	int ret;
+
+	ret = i915_mutex_lock_interruptible(&dev_priv->drm);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ctx = i915_gem_context_lookup(file_priv, ctx_user_handle);
+	if (!IS_ERR(ctx))
+		i915_gem_context_get(ctx);
+
+	mutex_unlock(&dev_priv->drm.struct_mutex);
+
+	return ctx;
+}
+
+int i915_perf_open_ioctl_locked(struct drm_device *dev,
+				struct drm_i915_perf_open_param *param,
+				struct perf_open_properties *props,
+				struct drm_file *file)
+{
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct i915_gem_context *specific_ctx = NULL;
+	struct i915_perf_stream *stream = NULL;
+	unsigned long f_flags = 0;
+	int stream_fd;
+	int ret = 0;
+
+	if (props->single_context) {
+		u32 ctx_handle = props->ctx_handle;
+		struct drm_i915_file_private *file_priv = file->driver_priv;
+
+		specific_ctx = lookup_context(dev_priv, file_priv, ctx_handle);
+		if (IS_ERR(specific_ctx)) {
+			ret = PTR_ERR(specific_ctx);
+			if (ret != -EINTR)
+				DRM_ERROR("Failed to look up context with ID %u for opening perf stream\n",
+					  ctx_handle);
+			goto err;
+		}
+	}
+
+	if (!specific_ctx && !capable(CAP_SYS_ADMIN)) {
+		DRM_ERROR("Insufficient privileges to open system-wide i915 perf stream\n");
+		ret = -EACCES;
+		goto err_ctx;
+	}
+
+	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
+	if (!stream) {
+		ret = -ENOMEM;
+		goto err_ctx;
+	}
+
+	stream->sample_flags = props->sample_flags;
+	stream->dev_priv = dev_priv;
+	stream->ctx = specific_ctx;
+
+	/*
+	 * TODO: support sampling something
+	 *
+	 * For now this is as far as we can go.
+	 */
+	DRM_ERROR("Unsupported i915 perf stream configuration\n");
+	ret = -EINVAL;
+	goto err_alloc;
+
+	list_add(&stream->link, &dev_priv->perf.streams);
+
+	if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
+		f_flags |= O_CLOEXEC;
+	if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
+		f_flags |= O_NONBLOCK;
+
+	stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
+	if (stream_fd < 0) {
+		ret = stream_fd;
+		goto err_open;
+	}
+
+	if (!(param->flags & I915_PERF_FLAG_DISABLED))
+		i915_perf_enable_locked(stream);
+
+	return stream_fd;
+
+err_open:
+	list_del(&stream->link);
+	if (stream->ops->destroy)
+		stream->ops->destroy(stream);
+err_alloc:
+	kfree(stream);
+err_ctx:
+	if (specific_ctx) {
+		mutex_lock(&dev_priv->drm.struct_mutex);
+		i915_gem_context_put(specific_ctx);
+		mutex_unlock(&dev_priv->drm.struct_mutex);
+	}
+err:
+	return ret;
+}
+
+/* Note we copy the properties from userspace outside of the i915 perf
+ * mutex to avoid an awkward lockdep with mmap_sem.
+ *
+ * Note this function only validates properties in isolation it doesn't
+ * validate that the combination of properties makes sense or that all
+ * properties necessary for a particular kind of stream have been set.
+ */
+static int read_properties_unlocked(struct drm_i915_private *dev_priv,
+				    u64 __user *uprops,
+				    u32 n_props,
+				    struct perf_open_properties *props)
+{
+	u64 __user *uprop = uprops;
+	int i;
+
+	memset(props, 0, sizeof(struct perf_open_properties));
+
+	if (!n_props) {
+		DRM_ERROR("No i915 perf properties given");
+		return -EINVAL;
+	}
+
+	if (n_props > DRM_I915_PERF_PROP_MAX) {
+		DRM_ERROR("More i915 perf properties specified than exist");
+		return -EINVAL;
+	}
+
+	for (i = 0; i < n_props; i++) {
+		u64 id, value;
+		int ret;
+
+		ret = get_user(id, (u64 __user *)uprop);
+		if (ret)
+			return ret;
+
+		if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
+			DRM_ERROR("Unknown i915 perf property ID");
+			return -EINVAL;
+		}
+
+		ret = get_user(value, (u64 __user *)uprop + 1);
+		if (ret)
+			return ret;
+
+		switch ((enum drm_i915_perf_property_id)id) {
+		case DRM_I915_PERF_PROP_CTX_HANDLE:
+			props->single_context = 1;
+			props->ctx_handle = value;
+			break;
+
+		case DRM_I915_PERF_PROP_MAX:
+			BUG();
+		}
+
+		uprop += 2;
+	}
+
+	return 0;
+}
+
+int i915_perf_open_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *file)
+{
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_i915_perf_open_param *param = data;
+	struct perf_open_properties props;
+	u32 known_open_flags = 0;
+	int ret;
+
+	if (!dev_priv->perf.initialized) {
+		DRM_ERROR("i915 perf interface not available for this system");
+		return -ENOTSUPP;
+	}
+
+	known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
+			   I915_PERF_FLAG_FD_NONBLOCK |
+			   I915_PERF_FLAG_DISABLED;
+	if (param->flags & ~known_open_flags) {
+		DRM_ERROR("Unknown drm_i915_perf_open_param flag\n");
+		return -EINVAL;
+	}
+
+	ret = read_properties_unlocked(dev_priv,
+				       u64_to_user_ptr(param->properties_ptr),
+				       param->num_properties,
+				       &props);
+	if (ret)
+		return ret;
+
+	mutex_lock(&dev_priv->perf.lock);
+	ret = i915_perf_open_ioctl_locked(dev, param, &props, file);
+	mutex_unlock(&dev_priv->perf.lock);
+
+	return ret;
+}
+
+void i915_perf_init(struct drm_i915_private *dev_priv)
+{
+	INIT_LIST_HEAD(&dev_priv->perf.streams);
+	mutex_init(&dev_priv->perf.lock);
+
+	dev_priv->perf.initialized = true;
+}
+
+void i915_perf_fini(struct drm_i915_private *dev_priv)
+{
+	if (!dev_priv->perf.initialized)
+		return;
+
+	/* Currently nothing to clean up */
+
+	dev_priv->perf.initialized = false;
+}
diff --git a/include/uapi/drm/i915_drm.h b/include/uapi/drm/i915_drm.h
index 03725fe..77fe79b 100644
--- a/include/uapi/drm/i915_drm.h
+++ b/include/uapi/drm/i915_drm.h
@@ -258,6 +258,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_USERPTR		0x33
 #define DRM_I915_GEM_CONTEXT_GETPARAM	0x34
 #define DRM_I915_GEM_CONTEXT_SETPARAM	0x35
+#define DRM_I915_PERF_OPEN		0x36
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -311,6 +312,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_USERPTR			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR, struct drm_i915_gem_userptr)
 #define DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM	DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_GETPARAM, struct drm_i915_gem_context_param)
 #define DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM	DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_SETPARAM, struct drm_i915_gem_context_param)
+#define DRM_IOCTL_I915_PERF_OPEN	DRM_IOR(DRM_COMMAND_BASE + DRM_I915_PERF_OPEN, struct drm_i915_perf_open_param)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -1222,6 +1224,71 @@ struct drm_i915_gem_context_param {
 	__u64 value;
 };
 
+enum drm_i915_perf_property_id {
+	/**
+	 * Open the stream for a specific context handle (as used with
+	 * execbuffer2). A stream opened for a specific context this way
+	 * won't typically require root privileges.
+	 */
+	DRM_I915_PERF_PROP_CTX_HANDLE = 1,
+
+	DRM_I915_PERF_PROP_MAX /* non-ABI */
+};
+
+struct drm_i915_perf_open_param {
+	__u32 flags;
+#define I915_PERF_FLAG_FD_CLOEXEC	(1<<0)
+#define I915_PERF_FLAG_FD_NONBLOCK	(1<<1)
+#define I915_PERF_FLAG_DISABLED		(1<<2)
+
+	/** The number of u64 (id, value) pairs */
+	__u32 num_properties;
+
+	/**
+	 * Pointer to array of u64 (id, value) pairs configuring the stream
+	 * to open.
+	 */
+	__u64 __user properties_ptr;
+};
+
+#define I915_PERF_IOCTL_ENABLE	_IO('i', 0x0)
+#define I915_PERF_IOCTL_DISABLE	_IO('i', 0x1)
+
+/**
+ * Common to all i915 perf records
+ */
+struct drm_i915_perf_record_header {
+	__u32 type;
+	__u16 pad;
+	__u16 size;
+};
+
+enum drm_i915_perf_record_type {
+
+	/**
+	 * Samples are the work horse record type whose contents are extensible
+	 * and defined when opening an i915 perf stream based on the given
+	 * properties.
+	 *
+	 * Boolean properties following the naming convention
+	 * DRM_I915_PERF_SAMPLE_xyz_PROP request the inclusion of 'xyz' data in
+	 * every sample.
+	 *
+	 * The order of these sample properties given by userspace has no
+	 * affect on the ordering of data within a sample. The order will be
+	 * documented here.
+	 *
+	 * struct {
+	 *     struct drm_i915_perf_record_header header;
+	 *
+	 *     TODO: itemize extensible sample data here
+	 * };
+	 */
+	DRM_I915_PERF_RECORD_SAMPLE = 1,
+
+	DRM_I915_PERF_RECORD_MAX /* non-ABI */
+};
+
 #if defined(__cplusplus)
 }
 #endif
-- 
2.9.2

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 33+ messages in thread

* ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4)
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (11 preceding siblings ...)
  2016-09-14 14:50 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3) Patchwork
@ 2016-09-14 16:19 ` Patchwork
  2016-09-15  7:20 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3) Patchwork
  2016-09-15  7:20 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4) Patchwork
  14 siblings, 0 replies; 33+ messages in thread
From: Patchwork @ 2016-09-14 16:19 UTC (permalink / raw)
  To: Robert Bragg; +Cc: intel-gfx

== Series Details ==

Series: Enable i915 perf stream for Haswell OA unit (rev4)
URL   : https://patchwork.freedesktop.org/series/11295/
State : failure

== Summary ==

Series 11295v4 Enable i915 perf stream for Haswell OA unit
https://patchwork.freedesktop.org/api/1.0/series/11295/revisions/4/mbox/

Test drv_module_reload_basic:
                dmesg-warn -> PASS       (fi-skl-6770hq)
                pass       -> SKIP       (fi-skl-6260u)
Test gem_exec_parse:
        Subgroup basic-rejected:
                pass       -> FAIL       (fi-ivb-3520m)
                pass       -> FAIL       (fi-ivb-3770)
                pass       -> FAIL       (fi-hsw-4770k)
                pass       -> FAIL       (fi-byt-j1900)
                pass       -> FAIL       (fi-hsw-4770r)
Test gem_exec_suspend:
        Subgroup basic-s3:
                incomplete -> PASS       (fi-hsw-4770k)
Test kms_busy:
        Subgroup basic-flip-default-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-default-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-default-c:
                skip       -> PASS       (fi-skl-6770hq)
Test kms_cursor_legacy:
        Subgroup basic-flip-after-cursor-legacy:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-after-cursor-varying-size:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-before-cursor-legacy:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-before-cursor-varying-size:
                skip       -> PASS       (fi-skl-6770hq)
Test kms_flip:
        Subgroup basic-flip-vs-dpms:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-vs-modeset:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-flip-vs-wf_vblank:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-plain-flip:
                skip       -> PASS       (fi-skl-6770hq)
Test kms_frontbuffer_tracking:
        Subgroup basic:
                skip       -> FAIL       (fi-skl-6770hq)
Test kms_pipe_crc_basic:
        Subgroup hang-read-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup hang-read-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup hang-read-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-a-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-b-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup nonblocking-crc-pipe-c-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-a-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-b-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup read-crc-pipe-c-frame-sequence:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup suspend-read-crc-pipe-a:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup suspend-read-crc-pipe-b:
                skip       -> PASS       (fi-skl-6770hq)
                dmesg-warn -> PASS       (fi-byt-j1900)
        Subgroup suspend-read-crc-pipe-c:
                skip       -> PASS       (fi-skl-6770hq)
Test pm_rpm:
        Subgroup basic-pci-d3-state:
                skip       -> PASS       (fi-skl-6770hq)
        Subgroup basic-rte:
                skip       -> PASS       (fi-skl-6770hq)
Test prime_vgem:
        Subgroup basic-fence-flip:
                skip       -> PASS       (fi-skl-6770hq)

fi-bdw-5557u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15 
fi-bsw-n3050     total:244  pass:202  dwarn:0   dfail:0   fail:0   skip:42 
fi-byt-j1900     total:244  pass:210  dwarn:1   dfail:0   fail:2   skip:31 
fi-hsw-4770k     total:244  pass:225  dwarn:0   dfail:0   fail:1   skip:18 
fi-hsw-4770r     total:244  pass:221  dwarn:0   dfail:0   fail:1   skip:22 
fi-ilk-650       total:244  pass:183  dwarn:0   dfail:0   fail:1   skip:60 
fi-ivb-3520m     total:244  pass:218  dwarn:0   dfail:0   fail:1   skip:25 
fi-ivb-3770      total:244  pass:206  dwarn:0   dfail:0   fail:1   skip:37 
fi-skl-6260u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15 
WARNING: Long output truncated

Results at /archive/results/CI_IGT_test/Patchwork_2535/

9aa8c0cdbc076bcc0486d7a31922a0f77c032fe7 drm-intel-nightly: 2016y-09m-14d-09h-19m-25s UTC integration manifest
ac3d501 drm/i915: Add a kerneldoc summary for i915_perf.c
9748144 drm/i915: Add more Haswell OA metric sets
695808f drm/i915: add oa_event_min_timer_exponent sysctl
b0c7ab1 drm/i915: Add dev.i915.perf_event_paranoid sysctl option
89749a7 drm/i915: advertise available metrics via sysfs
de45a10 drm/i915: Enable i915 perf stream for Haswell OA unit
af51dda drm/i915: Add 'render basic' Haswell OA unit config
4217060 drm/i915: don't whitelist oacontrol in cmd parser
1d86d19 drm/i915: return EACCES for check_cmd() failures
d9423dc drm/i915: rename OACONTROL GEN7_OACONTROL
de1d4ac drm/i915: Add i915 perf infrastructure

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3)
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (12 preceding siblings ...)
  2016-09-14 16:19 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4) Patchwork
@ 2016-09-15  7:20 ` Patchwork
  2016-09-15  7:20 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4) Patchwork
  14 siblings, 0 replies; 33+ messages in thread
From: Patchwork @ 2016-09-15  7:20 UTC (permalink / raw)
  To: Robert Bragg; +Cc: intel-gfx

== Series Details ==

Series: Enable i915 perf stream for Haswell OA unit (rev3)
URL   : https://patchwork.freedesktop.org/series/11295/
State : failure

== Summary ==

Series 11295v3 Enable i915 perf stream for Haswell OA unit
https://patchwork.freedesktop.org/api/1.0/series/11295/revisions/3/mbox/

Test gem_exec_parse:
        Subgroup basic-rejected:
                pass       -> FAIL       (fi-ivb-3520m)
                pass       -> FAIL       (fi-byt-n2820)
                pass       -> FAIL       (fi-ivb-3770)
                pass       -> FAIL       (fi-byt-j1900)
                pass       -> FAIL       (fi-hsw-4770r)
                pass       -> FAIL       (fi-hsw-4770k)
Test gem_exec_suspend:
        Subgroup basic-s3:
                incomplete -> PASS       (fi-hsw-4770k)
Test kms_flip:
        Subgroup basic-flip-vs-wf_vblank:
                pass       -> DMESG-WARN (fi-skl-6770hq)
Test pm_rpm:
        Subgroup basic-rte:
                pass       -> DMESG-WARN (fi-skl-6770hq)

fi-bdw-5557u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15 
fi-bsw-n3050     total:244  pass:202  dwarn:0   dfail:0   fail:0   skip:42 
fi-byt-j1900     total:244  pass:209  dwarn:2   dfail:0   fail:2   skip:31 
fi-byt-n2820     total:244  pass:207  dwarn:0   dfail:0   fail:2   skip:35 
fi-hsw-4770k     total:244  pass:225  dwarn:0   dfail:0   fail:1   skip:18 
fi-hsw-4770r     total:244  pass:221  dwarn:0   dfail:0   fail:1   skip:22 
fi-ilk-650       total:244  pass:183  dwarn:0   dfail:0   fail:1   skip:60 
fi-ivb-3520m     total:244  pass:218  dwarn:0   dfail:0   fail:1   skip:25 
fi-ivb-3770      total:244  pass:206  dwarn:0   dfail:0   fail:1   skip:37 
fi-skl-6260u     total:244  pass:230  dwarn:0   dfail:0   fail:0   skip:14 
fi-skl-6700hq    total:244  pass:221  dwarn:0   dfail:0   fail:1   skip:22 
fi-skl-6700k     total:244  pass:219  dwarn:1   dfail:0   fail:0   skip:24 
fi-skl-6770hq    total:244  pass:226  dwarn:3   dfail:0   fail:1   skip:14 
fi-snb-2520m     total:244  pass:208  dwarn:0   dfail:0   fail:0   skip:36 
fi-snb-2600      total:244  pass:207  dwarn:0   dfail:0   fail:0   skip:37 

Results at /archive/results/CI_IGT_test/Patchwork_2533/

9aa8c0cdbc076bcc0486d7a31922a0f77c032fe7 drm-intel-nightly: 2016y-09m-14d-09h-19m-25s UTC integration manifest
36985e3 drm/i915: Add a kerneldoc summary for i915_perf.c
9aab6b3 drm/i915: Add more Haswell OA metric sets
bdbb5e3 drm/i915: add oa_event_min_timer_exponent sysctl
bff8073 drm/i915: Add dev.i915.perf_event_paranoid sysctl option
c7d3b3b drm/i915: advertise available metrics via sysfs
c56f5bb drm/i915: Enable i915 perf stream for Haswell OA unit
182685f drm/i915: Add 'render basic' Haswell OA unit config
a48ac08 drm/i915: don't whitelist oacontrol in cmd parser
45f7ac0 drm/i915: return EACCES for check_cmd() failures
956313e drm/i915: rename OACONTROL GEN7_OACONTROL
db94420 drm/i915: Add i915 perf infrastructure

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4)
  2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
                   ` (13 preceding siblings ...)
  2016-09-15  7:20 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3) Patchwork
@ 2016-09-15  7:20 ` Patchwork
  14 siblings, 0 replies; 33+ messages in thread
From: Patchwork @ 2016-09-15  7:20 UTC (permalink / raw)
  To: Robert Bragg; +Cc: intel-gfx

== Series Details ==

Series: Enable i915 perf stream for Haswell OA unit (rev4)
URL   : https://patchwork.freedesktop.org/series/11295/
State : failure

== Summary ==

Series 11295v4 Enable i915 perf stream for Haswell OA unit
https://patchwork.freedesktop.org/api/1.0/series/11295/revisions/4/mbox/

Test drv_module_reload_basic:
                pass       -> SKIP       (fi-skl-6260u)
Test gem_exec_parse:
        Subgroup basic-rejected:
                pass       -> FAIL       (fi-hsw-4770r)
                pass       -> FAIL       (fi-ivb-3770)
                pass       -> FAIL       (fi-hsw-4770k)
                pass       -> FAIL       (fi-ivb-3520m)
                pass       -> FAIL       (fi-byt-j1900)
Test gem_exec_suspend:
        Subgroup basic-s3:
                incomplete -> PASS       (fi-hsw-4770k)
Test kms_pipe_crc_basic:
        Subgroup suspend-read-crc-pipe-b:
                dmesg-warn -> PASS       (fi-byt-j1900)

fi-bdw-5557u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15 
fi-bsw-n3050     total:244  pass:202  dwarn:0   dfail:0   fail:0   skip:42 
fi-byt-j1900     total:244  pass:210  dwarn:1   dfail:0   fail:2   skip:31 
fi-hsw-4770k     total:244  pass:225  dwarn:0   dfail:0   fail:1   skip:18 
fi-hsw-4770r     total:244  pass:221  dwarn:0   dfail:0   fail:1   skip:22 
fi-ilk-650       total:244  pass:183  dwarn:0   dfail:0   fail:1   skip:60 
fi-ivb-3520m     total:244  pass:218  dwarn:0   dfail:0   fail:1   skip:25 
fi-ivb-3770      total:244  pass:206  dwarn:0   dfail:0   fail:1   skip:37 
fi-skl-6260u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15 
fi-skl-6700hq    total:244  pass:221  dwarn:0   dfail:0   fail:1   skip:22 
fi-skl-6700k     total:244  pass:219  dwarn:1   dfail:0   fail:0   skip:24 
fi-skl-6770hq    total:244  pass:228  dwarn:1   dfail:0   fail:1   skip:14 
fi-snb-2520m     total:244  pass:208  dwarn:0   dfail:0   fail:0   skip:36 
fi-snb-2600      total:244  pass:207  dwarn:0   dfail:0   fail:0   skip:37 

Results at /archive/results/CI_IGT_test/Patchwork_2535/

9aa8c0cdbc076bcc0486d7a31922a0f77c032fe7 drm-intel-nightly: 2016y-09m-14d-09h-19m-25s UTC integration manifest
ac3d501 drm/i915: Add a kerneldoc summary for i915_perf.c
9748144 drm/i915: Add more Haswell OA metric sets
695808f drm/i915: add oa_event_min_timer_exponent sysctl
b0c7ab1 drm/i915: Add dev.i915.perf_event_paranoid sysctl option
89749a7 drm/i915: advertise available metrics via sysfs
de45a10 drm/i915: Enable i915 perf stream for Haswell OA unit
af51dda drm/i915: Add 'render basic' Haswell OA unit config
4217060 drm/i915: don't whitelist oacontrol in cmd parser
1d86d19 drm/i915: return EACCES for check_cmd() failures
d9423dc drm/i915: rename OACONTROL GEN7_OACONTROL
de1d4ac drm/i915: Add i915 perf infrastructure

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] drm/i915: Add i915 perf infrastructure
  2016-09-14 15:32     ` [PATCH] " Robert Bragg
@ 2016-10-07 17:10       ` Matthew Auld
  2016-10-11 19:03         ` [Intel-gfx] " Robert Bragg
  0 siblings, 1 reply; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:10 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 16:32, Robert Bragg <robert@sixbynine.org> wrote:
> Adds base i915 perf infrastructure for Gen performance metrics.
>
> This adds a DRM_IOCTL_I915_PERF_OPEN ioctl that takes an array of uint64
> properties to configure a stream of metrics and returns a new fd usable
> with standard VFS system calls including read() to read typed and sized
> records; ioctl() to enable or disable capture and poll() to wait for
> data.
>
> A stream is opened something like:
>
>   uint64_t properties[] = {
>       /* Single context sampling */
>       DRM_I915_PERF_PROP_CTX_HANDLE,        ctx_handle,
>
>       /* Include OA reports in samples */
>       DRM_I915_PERF_PROP_SAMPLE_OA,         true,
>
>       /* OA unit configuration */
>       DRM_I915_PERF_PROP_OA_METRICS_SET,    metrics_set_id,
>       DRM_I915_PERF_PROP_OA_FORMAT,         report_format,
>       DRM_I915_PERF_PROP_OA_EXPONENT,       period_exponent,
>    };
>    struct drm_i915_perf_open_param parm = {
>       .flags = I915_PERF_FLAG_FD_CLOEXEC |
>                I915_PERF_FLAG_FD_NONBLOCK |
>                I915_PERF_FLAG_DISABLED,
>       .properties_ptr = (uint64_t)properties,
>       .num_properties = sizeof(properties) / 16,
>    };
>    int fd = drmIoctl(drm_fd, DRM_IOCTL_I915_PERF_OPEN, &param);
>
> Records read all start with a common { type, size } header with
> DRM_I915_PERF_RECORD_SAMPLE being of most interest. Sample records
> contain an extensible number of fields and it's the
> DRM_I915_PERF_PROP_SAMPLE_xyz properties given when opening that
> determine what's included in every sample.
>
> No specific streams are supported yet so any attempt to open a stream
> will return an error.
>
> v4:
>     s/DRM_IORW/DRM_IOR/ - Emil Velikov
> v3:
>     update read() interface to avoid passing state struct - Chris Wilson
>     fix some rebase fallout, with i915-perf init/deinit
> v2:
>     use i915_gem_context_get() - Chris Wilson
>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
> ---
>  drivers/gpu/drm/i915/Makefile    |   3 +
>  drivers/gpu/drm/i915/i915_drv.c  |   4 +
>  drivers/gpu/drm/i915/i915_drv.h  |  91 ++++++++
>  drivers/gpu/drm/i915/i915_perf.c | 448 +++++++++++++++++++++++++++++++++++++++
>  include/uapi/drm/i915_drm.h      |  67 ++++++
>  5 files changed, 613 insertions(+)
>  create mode 100644 drivers/gpu/drm/i915/i915_perf.c
>
> diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
> index a998c2b..d991781 100644
> --- a/drivers/gpu/drm/i915/Makefile
> +++ b/drivers/gpu/drm/i915/Makefile
> @@ -110,6 +110,9 @@ i915-y += dvo_ch7017.o \
>  # virtual gpu code
>  i915-y += i915_vgpu.o
>
> +# perf code
> +i915-y += i915_perf.o
> +
>  ifeq ($(CONFIG_DRM_I915_GVT),y)
>  i915-y += intel_gvt.o
>  include $(src)/gvt/Makefile
> diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
> index 7f4e8ad..14f22fc 100644
> --- a/drivers/gpu/drm/i915/i915_drv.c
> +++ b/drivers/gpu/drm/i915/i915_drv.c
> @@ -838,6 +838,8 @@ static int i915_driver_init_early(struct drm_i915_private *dev_priv,
>
>         intel_device_info_dump(dev_priv);
>
> +       i915_perf_init(dev_priv);
> +
>         /* Not all pre-production machines fall into this category, only the
>          * very first ones. Almost everything should work, except for maybe
>          * suspend/resume. And we don't implement workarounds that affect only
> @@ -859,6 +861,7 @@ err_workqueues:
>   */
>  static void i915_driver_cleanup_early(struct drm_i915_private *dev_priv)
>  {
> +       i915_perf_fini(dev_priv);
>         i915_gem_load_cleanup(&dev_priv->drm);
>         i915_workqueues_cleanup(dev_priv);
>  }
> @@ -2560,6 +2563,7 @@ static const struct drm_ioctl_desc i915_ioctls[] = {
>         DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
>         DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
>         DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
> +       DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
>  };
>
>  static struct drm_driver driver = {
> diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> index 1e2dda8..0f5cd8f 100644
> --- a/drivers/gpu/drm/i915/i915_drv.h
> +++ b/drivers/gpu/drm/i915/i915_drv.h
> @@ -1740,6 +1740,84 @@ struct intel_wm_config {
>         bool sprites_scaled;
>  };
>
> +struct i915_perf_stream;
> +
> +struct i915_perf_stream_ops {
> +       /* Enables the collection of HW samples, either in response to
> +        * I915_PERF_IOCTL_ENABLE or implicitly called when stream is
> +        * opened without I915_PERF_FLAG_DISABLED.
> +        */
> +       void (*enable)(struct i915_perf_stream *stream);
> +
> +       /* Disables the collection of HW samples, either in response to
> +        * I915_PERF_IOCTL_DISABLE or implicitly called before
> +        * destroying the stream.
> +        */
> +       void (*disable)(struct i915_perf_stream *stream);
> +
> +       /* Return: true if any i915 perf records are ready to read()
> +        * for this stream.
> +        */
> +       bool (*can_read)(struct i915_perf_stream *stream);
> +
> +       /* Call poll_wait, passing a wait queue that will be woken
> +        * once there is something ready to read() for the stream
> +        */
> +       void (*poll_wait)(struct i915_perf_stream *stream,
> +                         struct file *file,
> +                         poll_table *wait);
> +
> +       /* For handling a blocking read, wait until there is something
> +        * to ready to read() for the stream. E.g. wait on the same
> +        * wait queue that would be passed to poll_wait() until
> +        * ->can_read() returns true (if its safe to call ->can_read()
> +        * without the i915 perf lock held).
> +        */
> +       int (*wait_unlocked)(struct i915_perf_stream *stream);
> +
> +       /* read - Copy buffered metrics as records to userspace
> +        * @buf: the userspace, destination buffer
> +        * @count: the number of bytes to copy, requested by userspace
> +        * @offset: zero at the start of the read, updated as the read
> +        *          proceeds, it represents how many bytes have been
> +        *          copied so far and the buffer offset for copying the
> +        *          next record.
> +        *
> +        * Copy as many buffered i915 perf samples and records for
> +        * this stream to userspace as will fit in the given buffer.
> +        *
> +        * Only write complete records; returning -ENOSPC if there
> +        * isn't room for a complete record.
> +        *
> +        * Return any error condition that results in a short read
> +        * such as -ENOSPC or -EFAULT, even though these may be
> +        * squashed before returning to userspace.
> +        */
> +       int (*read)(struct i915_perf_stream *stream,
> +                   char __user *buf,
> +                   size_t count,
> +                   size_t *offset);
> +
> +       /* Cleanup any stream specific resources.
> +        *
> +        * The stream will always be disabled before this is called.
> +        */
> +       void (*destroy)(struct i915_perf_stream *stream);
> +};
> +
> +struct i915_perf_stream {
> +       struct drm_i915_private *dev_priv;
> +
> +       struct list_head link;
> +
> +       u32 sample_flags;
> +
> +       struct i915_gem_context *ctx;
> +       bool enabled;
> +
> +       struct i915_perf_stream_ops *ops;
> +};
> +
>  struct drm_i915_private {
>         struct drm_device drm;
>
> @@ -2040,6 +2118,12 @@ struct drm_i915_private {
>
>         struct i915_runtime_pm pm;
>
> +       struct {
> +               bool initialized;
> +               struct mutex lock;
> +               struct list_head streams;
> +       } perf;
> +
>         /* Abstract the submission mechanism (legacy ringbuffer or execlists) away */
>         struct {
>                 void (*resume)(struct drm_i915_private *);
> @@ -3445,6 +3529,9 @@ int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
>  int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
>                                        struct drm_file *file);
>
> +int i915_perf_open_ioctl(struct drm_device *dev, void *data,
> +                        struct drm_file *file);
> +
>  /* i915_gem_evict.c */
>  int __must_check i915_gem_evict_something(struct i915_address_space *vm,
>                                           u64 min_size, u64 alignment,
> @@ -3555,6 +3642,10 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
>                             u32 batch_len,
>                             bool is_master);
>
> +/* i915_perf.c */
> +extern void i915_perf_init(struct drm_i915_private *dev_priv);
> +extern void i915_perf_fini(struct drm_i915_private *dev_priv);
> +
>  /* i915_suspend.c */
>  extern int i915_save_state(struct drm_device *dev);
>  extern int i915_restore_state(struct drm_device *dev);
> diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
> new file mode 100644
> index 0000000..87530f5
> --- /dev/null
> +++ b/drivers/gpu/drm/i915/i915_perf.c
> @@ -0,0 +1,448 @@
> +/*
> + * Copyright © 2015-2016 Intel Corporation
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + *
> + * Authors:
> + *   Robert Bragg <robert@sixbynine.org>
> + */
> +
> +#include <linux/anon_inodes.h>
> +#include <linux/sizes.h>
This should really be introduced in a later patch, but meh.

> +
> +#include "i915_drv.h"
> +
> +struct perf_open_properties {
> +       u32 sample_flags;
> +
> +       u64 single_context:1;
> +       u64 ctx_handle;
> +};
> +
> +static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
> +                                    struct file *file,
> +                                    char __user *buf,
> +                                    size_t count,
> +                                    loff_t *ppos)
> +{
> +       /* Note we keep the offset (aka bytes read) separate from any
> +        * error status so that the final check for whether we return
> +        * the bytes read with a higher precedence than any error (see
> +        * comment below) doesn't need to be handled/duplicated in
> +        * stream->ops->read() implementations.
> +        */
> +       size_t offset = 0;
> +       int ret = stream->ops->read(stream, buf, count, &offset);
> +
> +       /* If we've successfully copied any data then reporting that
> +        * takes precedence over any internal error status, so the
> +        * data isn't lost.
> +        *
> +        * For example ret will be -ENOSPC whenever there is more
> +        * buffered data than can be copied to userspace, but that's
> +        * only interesting if we weren't able to copy some data
> +        * because it implies the userspace buffer is too small to
> +        * receive a single record (and we never split records).
> +        *
> +        * Another case with ret == -EFAULT is more of a grey area
> +        * since it would seem like bad form for userspace to ask us
> +        * to overrun its buffer, but the user knows best:
> +        *
> +        *   http://yarchive.net/comp/linux/partial_reads_writes.html
> +        */
> +       return offset ?: (ret ?: -EAGAIN);
> +}
> +
> +static ssize_t i915_perf_read(struct file *file,
> +                             char __user *buf,
> +                             size_t count,
> +                             loff_t *ppos)
> +{
> +       struct i915_perf_stream *stream = file->private_data;
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +       ssize_t ret;
> +
> +       if (!(file->f_flags & O_NONBLOCK)) {
> +               /* Allow false positives from stream->ops->wait_unlocked.
> +                */
> +               do {
> +                       ret = stream->ops->wait_unlocked(stream);
> +                       if (ret)
> +                               return ret;
> +
> +                       mutex_lock(&dev_priv->perf.lock);
> +                       ret = i915_perf_read_locked(stream, file,
> +                                                   buf, count, ppos);
> +                       mutex_unlock(&dev_priv->perf.lock);
> +               } while (ret == -EAGAIN);
> +       } else {
> +               mutex_lock(&dev_priv->perf.lock);
> +               ret = i915_perf_read_locked(stream, file, buf, count, ppos);
> +               mutex_unlock(&dev_priv->perf.lock);
> +       }
> +
> +       return ret;
> +}
> +
> +static unsigned int i915_perf_poll_locked(struct i915_perf_stream *stream,
> +                                         struct file *file,
> +                                         poll_table *wait)
> +{
> +       unsigned int streams = 0;
> +
> +       stream->ops->poll_wait(stream, file, wait);
> +
> +       if (stream->ops->can_read(stream))
> +               streams |= POLLIN;
> +
> +       return streams;
> +}
> +
> +static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
> +{
> +       struct i915_perf_stream *stream = file->private_data;
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +       int ret;
> +
> +       mutex_lock(&dev_priv->perf.lock);
> +       ret = i915_perf_poll_locked(stream, file, wait);
> +       mutex_unlock(&dev_priv->perf.lock);
> +
> +       return ret;
> +}
> +
> +static void i915_perf_enable_locked(struct i915_perf_stream *stream)
> +{
> +       if (stream->enabled)
> +               return;
> +
> +       /* Allow stream->ops->enable() to refer to this */
> +       stream->enabled = true;
> +
> +       if (stream->ops->enable)
> +               stream->ops->enable(stream);
> +}
> +
> +static void i915_perf_disable_locked(struct i915_perf_stream *stream)
> +{
> +       if (!stream->enabled)
> +               return;
> +
> +       /* Allow stream->ops->disable() to refer to this */
> +       stream->enabled = false;
> +
> +       if (stream->ops->disable)
> +               stream->ops->disable(stream);
> +}
> +
> +static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
> +                                  unsigned int cmd,
> +                                  unsigned long arg)
> +{
> +       switch (cmd) {
> +       case I915_PERF_IOCTL_ENABLE:
> +               i915_perf_enable_locked(stream);
> +               return 0;
> +       case I915_PERF_IOCTL_DISABLE:
> +               i915_perf_disable_locked(stream);
> +               return 0;
> +       }
> +
> +       return -EINVAL;
> +}
> +
> +static long i915_perf_ioctl(struct file *file,
> +                           unsigned int cmd,
> +                           unsigned long arg)
> +{
> +       struct i915_perf_stream *stream = file->private_data;
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +       long ret;
> +
> +       mutex_lock(&dev_priv->perf.lock);
> +       ret = i915_perf_ioctl_locked(stream, cmd, arg);
> +       mutex_unlock(&dev_priv->perf.lock);
> +
> +       return ret;
> +}
> +
> +static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       if (stream->enabled)
> +               i915_perf_disable_locked(stream);
> +
> +       if (stream->ops->destroy)
> +               stream->ops->destroy(stream);
> +
> +       list_del(&stream->link);
> +
> +       if (stream->ctx) {
> +               mutex_lock(&dev_priv->drm.struct_mutex);
> +               i915_gem_context_put(stream->ctx);
> +               mutex_unlock(&dev_priv->drm.struct_mutex);
> +       }
> +
> +       kfree(stream);
> +}
> +
> +static int i915_perf_release(struct inode *inode, struct file *file)
> +{
> +       struct i915_perf_stream *stream = file->private_data;
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       mutex_lock(&dev_priv->perf.lock);
> +       i915_perf_destroy_locked(stream);
> +       mutex_unlock(&dev_priv->perf.lock);
> +
> +       return 0;
> +}
> +
> +
> +static const struct file_operations fops = {
> +       .owner          = THIS_MODULE,
> +       .llseek         = no_llseek,
> +       .release        = i915_perf_release,
> +       .poll           = i915_perf_poll,
> +       .read           = i915_perf_read,
> +       .unlocked_ioctl = i915_perf_ioctl,
> +};
> +
> +
> +static struct i915_gem_context *
> +lookup_context(struct drm_i915_private *dev_priv,
> +              struct drm_i915_file_private *file_priv,
> +              u32 ctx_user_handle)
> +{
> +       struct i915_gem_context *ctx;
> +       int ret;
> +
> +       ret = i915_mutex_lock_interruptible(&dev_priv->drm);
> +       if (ret)
> +               return ERR_PTR(ret);
> +
> +       ctx = i915_gem_context_lookup(file_priv, ctx_user_handle);
> +       if (!IS_ERR(ctx))
> +               i915_gem_context_get(ctx);
> +
> +       mutex_unlock(&dev_priv->drm.struct_mutex);
> +
> +       return ctx;
> +}
> +
> +int i915_perf_open_ioctl_locked(struct drm_device *dev,
> +                               struct drm_i915_perf_open_param *param,
> +                               struct perf_open_properties *props,
> +                               struct drm_file *file)
> +{
This should be static and also let's just make it take dev_priv directly.

> +       struct drm_i915_private *dev_priv = dev->dev_private;
> +       struct i915_gem_context *specific_ctx = NULL;
> +       struct i915_perf_stream *stream = NULL;
> +       unsigned long f_flags = 0;
> +       int stream_fd;
> +       int ret = 0;
No need to initialize.

> +
> +       if (props->single_context) {
> +               u32 ctx_handle = props->ctx_handle;
> +               struct drm_i915_file_private *file_priv = file->driver_priv;
> +
> +               specific_ctx = lookup_context(dev_priv, file_priv, ctx_handle);
> +               if (IS_ERR(specific_ctx)) {
> +                       ret = PTR_ERR(specific_ctx);
> +                       if (ret != -EINTR)
> +                               DRM_ERROR("Failed to look up context with ID %u for opening perf stream\n",
> +                                         ctx_handle);
> +                       goto err;
> +               }
> +       }
> +
> +       if (!specific_ctx && !capable(CAP_SYS_ADMIN)) {
> +               DRM_ERROR("Insufficient privileges to open system-wide i915 perf stream\n");
> +               ret = -EACCES;
> +               goto err_ctx;
> +       }
> +
> +       stream = kzalloc(sizeof(*stream), GFP_KERNEL);
> +       if (!stream) {
> +               ret = -ENOMEM;
> +               goto err_ctx;
> +       }
> +
> +       stream->sample_flags = props->sample_flags;
> +       stream->dev_priv = dev_priv;
> +       stream->ctx = specific_ctx;
> +
> +       /*
> +        * TODO: support sampling something
> +        *
> +        * For now this is as far as we can go.
> +        */
> +       DRM_ERROR("Unsupported i915 perf stream configuration\n");
> +       ret = -EINVAL;
> +       goto err_alloc;
> +
> +       list_add(&stream->link, &dev_priv->perf.streams);
> +
> +       if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
> +               f_flags |= O_CLOEXEC;
> +       if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
> +               f_flags |= O_NONBLOCK;
> +
> +       stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
> +       if (stream_fd < 0) {
> +               ret = stream_fd;
> +               goto err_open;
> +       }
> +
> +       if (!(param->flags & I915_PERF_FLAG_DISABLED))
> +               i915_perf_enable_locked(stream);
> +
> +       return stream_fd;
> +
> +err_open:
> +       list_del(&stream->link);
> +       if (stream->ops->destroy)
> +               stream->ops->destroy(stream);
> +err_alloc:
> +       kfree(stream);
> +err_ctx:
> +       if (specific_ctx) {
> +               mutex_lock(&dev_priv->drm.struct_mutex);
> +               i915_gem_context_put(specific_ctx);
> +               mutex_unlock(&dev_priv->drm.struct_mutex);
> +       }
> +err:
> +       return ret;
> +}
> +
> +/* Note we copy the properties from userspace outside of the i915 perf
> + * mutex to avoid an awkward lockdep with mmap_sem.
> + *
> + * Note this function only validates properties in isolation it doesn't
> + * validate that the combination of properties makes sense or that all
> + * properties necessary for a particular kind of stream have been set.
> + */
> +static int read_properties_unlocked(struct drm_i915_private *dev_priv,
> +                                   u64 __user *uprops,
> +                                   u32 n_props,
> +                                   struct perf_open_properties *props)
> +{
> +       u64 __user *uprop = uprops;
> +       int i;
> +
> +       memset(props, 0, sizeof(struct perf_open_properties));
> +
> +       if (!n_props) {
> +               DRM_ERROR("No i915 perf properties given");
> +               return -EINVAL;
> +       }
> +
> +       if (n_props > DRM_I915_PERF_PROP_MAX) {
> +               DRM_ERROR("More i915 perf properties specified than exist");
> +               return -EINVAL;
> +       }
> +
> +       for (i = 0; i < n_props; i++) {
> +               u64 id, value;
> +               int ret;
> +
> +               ret = get_user(id, (u64 __user *)uprop);
> +               if (ret)
> +                       return ret;
> +
> +               if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
> +                       DRM_ERROR("Unknown i915 perf property ID");
> +                       return -EINVAL;
> +               }
> +
> +               ret = get_user(value, (u64 __user *)uprop + 1);
> +               if (ret)
> +                       return ret;
> +
> +               switch ((enum drm_i915_perf_property_id)id) {
> +               case DRM_I915_PERF_PROP_CTX_HANDLE:
> +                       props->single_context = 1;
> +                       props->ctx_handle = value;
> +                       break;
> +
Don't bother with the new line here, it just gets removed later.

> +               case DRM_I915_PERF_PROP_MAX:
> +                       BUG();
We already handle this case above, but I guess we still need this in
order to silence gcc...

> +               }
> +
> +               uprop += 2;
> +       }
> +
> +       return 0;
> +}
> +
> +int i915_perf_open_ioctl(struct drm_device *dev, void *data,
> +                        struct drm_file *file)
> +{
> +       struct drm_i915_private *dev_priv = dev->dev_private;
> +       struct drm_i915_perf_open_param *param = data;
> +       struct perf_open_properties props;
> +       u32 known_open_flags = 0;
No need to initialize.

> +       int ret;
> +
> +       if (!dev_priv->perf.initialized) {
> +               DRM_ERROR("i915 perf interface not available for this system");
> +               return -ENOTSUPP;
> +       }
> +
> +       known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
> +                          I915_PERF_FLAG_FD_NONBLOCK |
> +                          I915_PERF_FLAG_DISABLED;
> +       if (param->flags & ~known_open_flags) {
> +               DRM_ERROR("Unknown drm_i915_perf_open_param flag\n");
> +               return -EINVAL;
> +       }
> +
> +       ret = read_properties_unlocked(dev_priv,
> +                                      u64_to_user_ptr(param->properties_ptr),
> +                                      param->num_properties,
> +                                      &props);
> +       if (ret)
> +               return ret;
> +
> +       mutex_lock(&dev_priv->perf.lock);
> +       ret = i915_perf_open_ioctl_locked(dev, param, &props, file);
> +       mutex_unlock(&dev_priv->perf.lock);
> +
> +       return ret;
> +}
> +
> +void i915_perf_init(struct drm_i915_private *dev_priv)
> +{
> +       INIT_LIST_HEAD(&dev_priv->perf.streams);
> +       mutex_init(&dev_priv->perf.lock);
> +
> +       dev_priv->perf.initialized = true;
> +}
> +
> +void i915_perf_fini(struct drm_i915_private *dev_priv)
> +{
> +       if (!dev_priv->perf.initialized)
> +               return;
> +
> +       /* Currently nothing to clean up */
> +
> +       dev_priv->perf.initialized = false;
> +}
> diff --git a/include/uapi/drm/i915_drm.h b/include/uapi/drm/i915_drm.h
> index 03725fe..77fe79b 100644
> --- a/include/uapi/drm/i915_drm.h
> +++ b/include/uapi/drm/i915_drm.h
> @@ -258,6 +258,7 @@ typedef struct _drm_i915_sarea {
>  #define DRM_I915_GEM_USERPTR           0x33
>  #define DRM_I915_GEM_CONTEXT_GETPARAM  0x34
>  #define DRM_I915_GEM_CONTEXT_SETPARAM  0x35
> +#define DRM_I915_PERF_OPEN             0x36
>
>  #define DRM_IOCTL_I915_INIT            DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
>  #define DRM_IOCTL_I915_FLUSH           DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
> @@ -311,6 +312,7 @@ typedef struct _drm_i915_sarea {
>  #define DRM_IOCTL_I915_GEM_USERPTR                     DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR, struct drm_i915_gem_userptr)
>  #define DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM    DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_GETPARAM, struct drm_i915_gem_context_param)
>  #define DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM    DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_SETPARAM, struct drm_i915_gem_context_param)
> +#define DRM_IOCTL_I915_PERF_OPEN       DRM_IOR(DRM_COMMAND_BASE + DRM_I915_PERF_OPEN, struct drm_i915_perf_open_param)
As you already pointed out this will need to be IOW.

>
>  /* Allow drivers to submit batchbuffers directly to hardware, relying
>   * on the security mechanisms provided by hardware.
> @@ -1222,6 +1224,71 @@ struct drm_i915_gem_context_param {
>         __u64 value;
>  };
>
> +enum drm_i915_perf_property_id {
> +       /**
> +        * Open the stream for a specific context handle (as used with
> +        * execbuffer2). A stream opened for a specific context this way
> +        * won't typically require root privileges.
> +        */
> +       DRM_I915_PERF_PROP_CTX_HANDLE = 1,
> +
> +       DRM_I915_PERF_PROP_MAX /* non-ABI */
> +};
> +
> +struct drm_i915_perf_open_param {
> +       __u32 flags;
> +#define I915_PERF_FLAG_FD_CLOEXEC      (1<<0)
> +#define I915_PERF_FLAG_FD_NONBLOCK     (1<<1)
> +#define I915_PERF_FLAG_DISABLED                (1<<2)
> +
> +       /** The number of u64 (id, value) pairs */
> +       __u32 num_properties;
> +
> +       /**
> +        * Pointer to array of u64 (id, value) pairs configuring the stream
> +        * to open.
> +        */
> +       __u64 __user properties_ptr;
> +};
> +
> +#define I915_PERF_IOCTL_ENABLE _IO('i', 0x0)
> +#define I915_PERF_IOCTL_DISABLE        _IO('i', 0x1)
> +
> +/**
> + * Common to all i915 perf records
> + */
> +struct drm_i915_perf_record_header {
> +       __u32 type;
> +       __u16 pad;
> +       __u16 size;
> +};
> +
> +enum drm_i915_perf_record_type {
> +
> +       /**
> +        * Samples are the work horse record type whose contents are extensible
> +        * and defined when opening an i915 perf stream based on the given
> +        * properties.
> +        *
> +        * Boolean properties following the naming convention
> +        * DRM_I915_PERF_SAMPLE_xyz_PROP request the inclusion of 'xyz' data in
> +        * every sample.
> +        *
> +        * The order of these sample properties given by userspace has no
> +        * affect on the ordering of data within a sample. The order will be
> +        * documented here.
> +        *
> +        * struct {
> +        *     struct drm_i915_perf_record_header header;
> +        *
> +        *     TODO: itemize extensible sample data here
> +        * };
> +        */
> +       DRM_I915_PERF_RECORD_SAMPLE = 1,
> +
> +       DRM_I915_PERF_RECORD_MAX /* non-ABI */
> +};
> +
>  #if defined(__cplusplus)
>  }
>  #endif
> --
> 2.9.2
>
> _______________________________________________
> Intel-gfx mailing list
> Intel-gfx@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/intel-gfx
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 02/11] drm/i915: rename OACONTROL GEN7_OACONTROL
  2016-09-14 14:19 ` [PATCH v5 02/11] drm/i915: rename OACONTROL GEN7_OACONTROL Robert Bragg
@ 2016-10-07 17:11   ` Matthew Auld
  0 siblings, 0 replies; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:11 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> OACONTROL changes quite a bit for gen8, with some bits split out into a
> per-context OACTXCONTROL register. Rename now before adding more gen7 OA
> registers
>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 05/11] drm/i915: Add 'render basic' Haswell OA unit config
  2016-09-14 14:19 ` [PATCH v5 05/11] drm/i915: Add 'render basic' Haswell OA unit config Robert Bragg
@ 2016-10-07 17:15   ` Matthew Auld
  0 siblings, 0 replies; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:15 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> Adds a static OA unit, MUX + B Counter configuration for basic render
> metrics on Haswell. This is auto generated from an XML
> description of metric sets, currently maintained in gputop, ref:
>
>   https://github.com/rib/gputop
>   > gputop-data/oa-*.xml
>   > scripts/i915-perf-kernelgen.py
>
>   $ make -C gputop-data -f Makefile.xml SYSFS=0 WHITELIST=RenderBasic
>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
> ---
>  drivers/gpu/drm/i915/Makefile      |   3 +-
>  drivers/gpu/drm/i915/i915_drv.h    |  14 ++++
>  drivers/gpu/drm/i915/i915_oa_hsw.c | 143 +++++++++++++++++++++++++++++++++++++
>  drivers/gpu/drm/i915/i915_oa_hsw.h |  34 +++++++++
>  4 files changed, 193 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/gpu/drm/i915/i915_oa_hsw.c
>  create mode 100644 drivers/gpu/drm/i915/i915_oa_hsw.h
>
> diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
> index d991781..6cb25dd 100644
> --- a/drivers/gpu/drm/i915/Makefile
> +++ b/drivers/gpu/drm/i915/Makefile
> @@ -111,7 +111,8 @@ i915-y += dvo_ch7017.o \
>  i915-y += i915_vgpu.o
>
>  # perf code
> -i915-y += i915_perf.o
> +i915-y += i915_perf.o \
> +         i915_oa_hsw.o
>
>  ifeq ($(CONFIG_DRM_I915_GVT),y)
>  i915-y += intel_gvt.o
> diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> index 0f5cd8f..5fad018 100644
> --- a/drivers/gpu/drm/i915/i915_drv.h
> +++ b/drivers/gpu/drm/i915/i915_drv.h
> @@ -1740,6 +1740,11 @@ struct intel_wm_config {
>         bool sprites_scaled;
>  };
>
> +struct i915_oa_reg {
> +       i915_reg_t addr;
> +       u32 value;
> +};
> +
>  struct i915_perf_stream;
>
>  struct i915_perf_stream_ops {
> @@ -2122,6 +2127,15 @@ struct drm_i915_private {
>                 bool initialized;
>                 struct mutex lock;
>                 struct list_head streams;
> +
> +               struct {
> +                       u32 metrics_set;
> +
> +                       const struct i915_oa_reg *mux_regs;
> +                       int mux_regs_len;
> +                       const struct i915_oa_reg *b_counter_regs;
> +                       int b_counter_regs_len;
> +               } oa;
>         } perf;
>
>         /* Abstract the submission mechanism (legacy ringbuffer or execlists) away */
> diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.c b/drivers/gpu/drm/i915/i915_oa_hsw.c
> new file mode 100644
> index 0000000..eb5ceca
> --- /dev/null
> +++ b/drivers/gpu/drm/i915/i915_oa_hsw.c
> @@ -0,0 +1,143 @@
> +/*
> + * Autogenerated file, DO NOT EDIT manually!
> + *
> + * Copyright (c) 2015 Intel Corporation
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + *
> + */
> +
> +#include "i915_drv.h"
We need to be including i915_oa_hsw.h here, right?

With that fixed:
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 06/11] drm/i915: Enable i915 perf stream for Haswell OA unit
  2016-09-14 14:19 ` [PATCH v5 06/11] drm/i915: Enable i915 perf stream for Haswell OA unit Robert Bragg
@ 2016-10-07 17:19   ` Matthew Auld
  2016-10-12  0:55     ` Robert Bragg
  0 siblings, 1 reply; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:19 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> Gen graphics hardware can be set up to periodically write snapshots of
> performance counters into a circular buffer via its Observation
> Architecture and this patch exposes that capability to userspace via the
> i915 perf interface.
>
> Cc: Chris Wilson <chris@chris-wilson.co.uk>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
> Signed-off-by: Zhenyu Wang <zhenyuw@linux.intel.com>
> ---
>  drivers/gpu/drm/i915/i915_drv.h         |  72 ++-
>  drivers/gpu/drm/i915/i915_gem_context.c |  22 +-
>  drivers/gpu/drm/i915/i915_perf.c        | 998 +++++++++++++++++++++++++++++++-
>  drivers/gpu/drm/i915/i915_reg.h         | 338 +++++++++++
>  drivers/gpu/drm/i915/intel_ringbuffer.c |  10 +-
>  include/uapi/drm/i915_drm.h             |  70 ++-
>  6 files changed, 1477 insertions(+), 33 deletions(-)
>
> diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> index 5fad018..551f078 100644
> --- a/drivers/gpu/drm/i915/i915_drv.h
> +++ b/drivers/gpu/drm/i915/i915_drv.h
> @@ -1740,6 +1740,11 @@ struct intel_wm_config {
>         bool sprites_scaled;
>  };
>
> +struct i915_oa_format {
> +       u32 format;
> +       int size;
> +};
> +
>  struct i915_oa_reg {
>         i915_reg_t addr;
>         u32 value;
> @@ -1760,11 +1765,6 @@ struct i915_perf_stream_ops {
>          */
>         void (*disable)(struct i915_perf_stream *stream);
>
> -       /* Return: true if any i915 perf records are ready to read()
> -        * for this stream.
> -        */
> -       bool (*can_read)(struct i915_perf_stream *stream);
> -
>         /* Call poll_wait, passing a wait queue that will be woken
>          * once there is something ready to read() for the stream
>          */
> @@ -1774,9 +1774,7 @@ struct i915_perf_stream_ops {
>
>         /* For handling a blocking read, wait until there is something
>          * to ready to read() for the stream. E.g. wait on the same
> -        * wait queue that would be passed to poll_wait() until
> -        * ->can_read() returns true (if its safe to call ->can_read()
> -        * without the i915 perf lock held).
> +        * wait queue that would be passed to poll_wait().
>          */
>         int (*wait_unlocked)(struct i915_perf_stream *stream);
>
> @@ -1816,11 +1814,28 @@ struct i915_perf_stream {
>         struct list_head link;
>
>         u32 sample_flags;
> +       int sample_size;
>
>         struct i915_gem_context *ctx;
>         bool enabled;
>
> -       struct i915_perf_stream_ops *ops;
> +       const struct i915_perf_stream_ops *ops;
> +};
> +
> +struct i915_oa_ops {
> +       void (*init_oa_buffer)(struct drm_i915_private *dev_priv);
> +       int (*enable_metric_set)(struct drm_i915_private *dev_priv);
> +       void (*disable_metric_set)(struct drm_i915_private *dev_priv);
> +       void (*oa_enable)(struct drm_i915_private *dev_priv);
> +       void (*oa_disable)(struct drm_i915_private *dev_priv);
> +       void (*update_oacontrol)(struct drm_i915_private *dev_priv);
> +       void (*update_hw_ctx_id_locked)(struct drm_i915_private *dev_priv,
> +                                       u32 ctx_id);
> +       int (*read)(struct i915_perf_stream *stream,
> +                   char __user *buf,
> +                   size_t count,
> +                   size_t *offset);
> +       bool (*oa_buffer_is_empty)(struct drm_i915_private *dev_priv);
>  };
>
>  struct drm_i915_private {
> @@ -2125,16 +2140,48 @@ struct drm_i915_private {
>
>         struct {
>                 bool initialized;
> +
>                 struct mutex lock;
>                 struct list_head streams;
>
> +               spinlock_t hook_lock;
> +
>                 struct {
> -                       u32 metrics_set;
> +                       struct i915_perf_stream *exclusive_stream;
> +
> +                       u32 specific_ctx_id;
> +
> +                       struct hrtimer poll_check_timer;
> +                       wait_queue_head_t poll_wq;
> +                       atomic_t pollin;
> +
> +                       bool periodic;
> +                       int period_exponent;
> +                       int timestamp_frequency;
> +
> +                       int tail_margin;
> +
> +                       int metrics_set;
>
>                         const struct i915_oa_reg *mux_regs;
>                         int mux_regs_len;
>                         const struct i915_oa_reg *b_counter_regs;
>                         int b_counter_regs_len;
> +
> +                       struct {
> +                               struct drm_i915_gem_object *obj;
> +                               struct i915_vma *vma;
> +                               u32 gtt_offset;
> +                               u8 *addr;
> +                               int format;
> +                               int format_size;
> +                       } oa_buffer;
> +
> +                       u32 gen7_latched_oastatus1;
> +
> +                       struct i915_oa_ops ops;
> +                       const struct i915_oa_format *oa_formats;
> +                       int n_builtin_sets;
>                 } oa;
>         } perf;
>
> @@ -3499,6 +3546,9 @@ struct drm_i915_gem_object *
>  i915_gem_alloc_context_obj(struct drm_device *dev, size_t size);
>  struct i915_gem_context *
>  i915_gem_context_create_gvt(struct drm_device *dev);
> +int i915_gem_context_pin_legacy_rcs_state(struct drm_i915_private *dev_priv,
> +                                         struct i915_gem_context *ctx,
> +                                         u64 flags);
>
>  static inline struct i915_gem_context *
>  i915_gem_context_lookup(struct drm_i915_file_private *file_priv, u32 id)
> @@ -3545,6 +3595,8 @@ int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
>
>  int i915_perf_open_ioctl(struct drm_device *dev, void *data,
>                          struct drm_file *file);
> +void i915_oa_legacy_context_pin_notify(struct drm_i915_private *dev_priv,
> +                                      struct i915_gem_context *ctx);
>
>  /* i915_gem_evict.c */
>  int __must_check i915_gem_evict_something(struct i915_address_space *vm,
> diff --git a/drivers/gpu/drm/i915/i915_gem_context.c b/drivers/gpu/drm/i915/i915_gem_context.c
> index df10f4e9..62966e9 100644
> --- a/drivers/gpu/drm/i915/i915_gem_context.c
> +++ b/drivers/gpu/drm/i915/i915_gem_context.c
> @@ -134,6 +134,25 @@ static int get_context_size(struct drm_i915_private *dev_priv)
>         return ret;
>  }
>
> +int i915_gem_context_pin_legacy_rcs_state(struct drm_i915_private *dev_priv,
> +                                         struct i915_gem_context *ctx,
> +                                         u64 flags)
> +{
> +       int ret;
> +
> +       lockdep_assert_held(&ctx->i915->drm.struct_mutex);
> +
> +       ret = i915_vma_pin(ctx->engine[RCS].state, 0,
> +                          ctx->ggtt_alignment,
> +                          flags);
> +       if (ret)
> +               return ret;
> +
> +       i915_oa_legacy_context_pin_notify(dev_priv, ctx);
> +
> +       return 0;
> +}
> +
>  void i915_gem_context_free(struct kref *ctx_ref)
>  {
>         struct i915_gem_context *ctx = container_of(ctx_ref, typeof(*ctx), ref);
> @@ -770,7 +789,8 @@ static int do_rcs_switch(struct drm_i915_gem_request *req)
>         }
>
>         /* Trying to pin first makes error handling easier. */
> -       ret = i915_vma_pin(vma, 0, to->ggtt_alignment, PIN_GLOBAL);
> +       ret = i915_gem_context_pin_legacy_rcs_state(engine->i915, to,
> +                                                   PIN_GLOBAL);
>         if (ret)
>                 return ret;
>
> diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
> index 87530f5..5305982 100644
> --- a/drivers/gpu/drm/i915/i915_perf.c
> +++ b/drivers/gpu/drm/i915/i915_perf.c
> @@ -28,14 +28,860 @@
>  #include <linux/sizes.h>
>
>  #include "i915_drv.h"
> +#include "intel_ringbuffer.h"
> +#include "intel_lrc.h"
Superfluous includes.

> +#include "i915_oa_hsw.h"
> +
> +/* Must be a power of two */
> +#define OA_BUFFER_SIZE         SZ_16M
It's a power of two between 128K and 16M, maybe add a build_bug_on and
build_bug_on_not_power_of_2 to check this?

> +#define OA_TAKEN(tail, head)   ((tail - head) & (OA_BUFFER_SIZE - 1))
> +
> +/* There's a HW race condition between OA unit tail pointer register updates and
> + * writes to memory whereby the tail pointer can sometimes get ahead of what's
> + * been written out to the OA buffer so far.
> + *
> + * Although this can be observed explicitly by checking for a zeroed report-id
> + * field in tail reports, it seems preferable to account for this earlier e.g.
> + * as part of the _oa_buffer_is_empty checks to minimize -EAGAIN polling cycles
> + * in this situation.
> + *
> + * To give time for the most recent reports to land before they may be copied to
> + * userspace, the driver operates as if the tail pointer effectively lags behind
> + * the HW tail pointer by 'tail_margin' bytes. The margin in bytes is calculated
> + * based on this constant in nanoseconds, the current OA sampling exponent
> + * and current report size.
> + *
> + * There is also a fallback check while reading to simply skip over reports with
> + * a zeroed report-id.
> + */
> +#define OA_TAIL_MARGIN_NSEC    100000ULL
Yikes!

> +
> +/* frequency for checking whether the OA unit has written new reports to the
> + * circular OA buffer...
> + */
> +#define POLL_FREQUENCY 200
> +#define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
> +
> +/* The maximum exponent the hardware accepts is 63 (essentially it selects one
> + * of the 64bit timestamp bits to trigger reports from) but there's currently
> + * no known use case for sampling as infrequently as once per 47 thousand years.
> + *
> + * Since the timestamps included in OA reports are only 32bits it seems
> + * reasonable to limit the OA exponent where it's still possible to account for
> + * overflow in OA report timestamps.
> + */
> +#define OA_EXPONENT_MAX 31
> +
> +/* XXX: beware if future OA HW adds new report formats that the current
> + * code assumes all reports have a power-of-two size and ~(size - 1) can
> + * be used as a mask to align the OA tail pointer.
> + */
> +static struct i915_oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
> +       [I915_OA_FORMAT_A13]        = { 0, 64 },
> +       [I915_OA_FORMAT_A29]        = { 1, 128 },
> +       [I915_OA_FORMAT_A13_B8_C8]  = { 2, 128 },
> +       /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
> +       [I915_OA_FORMAT_B4_C8]      = { 4, 64 },
> +       [I915_OA_FORMAT_A45_B8_C8]  = { 5, 256 },
> +       [I915_OA_FORMAT_B4_C8_A16]  = { 6, 128 },
> +       [I915_OA_FORMAT_C4_B8]      = { 7, 64 },
> +};
> +
> +#define SAMPLE_OA_REPORT      (1<<0)
>
>  struct perf_open_properties {
>         u32 sample_flags;
>
>         u64 single_context:1;
>         u64 ctx_handle;
> +
> +       /* OA sampling state */
> +       int metrics_set;
> +       int oa_format;
> +       bool oa_periodic;
> +       int oa_period_exponent;
>  };
>
> +/* NB: This is either called via fops or the poll check hrtimer (atomic ctx)
> + *
> + * It's safe to read OA config state here unlocked, assuming that this is only
> + * called while the stream is enabled, while the global OA configuration can't
> + * be modified.
> + *
> + * Note: we don't lock around the head/tail reads even though there's the slim
> + * possibility of read() fop errors forcing a re-init of the OA buffer
> + * pointers.  A race here could result in a false positive !empty status which
> + * is acceptable.
> + */
> +static bool gen7_oa_buffer_is_empty_fop_unlocked(struct drm_i915_private *dev_priv)
> +{
> +       int report_size = dev_priv->perf.oa.oa_buffer.format_size;
> +       u32 oastatus2 = I915_READ(GEN7_OASTATUS2);
> +       u32 oastatus1 = I915_READ(GEN7_OASTATUS1);
> +       u32 head = oastatus2 & GEN7_OASTATUS2_HEAD_MASK;
> +       u32 tail = oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
> +
> +       return OA_TAKEN(tail, head) <
> +               dev_priv->perf.oa.tail_margin + report_size;
> +}
> +
> +/**
> + * Appends a status record to a userspace read() buffer.
> + */
> +static int append_oa_status(struct i915_perf_stream *stream,
> +                           char __user *buf,
> +                           size_t count,
> +                           size_t *offset,
> +                           enum drm_i915_perf_record_type type)
> +{
> +       struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
> +
> +       if ((count - *offset) < header.size)
> +               return -ENOSPC;
> +
> +       if (copy_to_user(buf + *offset, &header, sizeof(header)))
> +               return -EFAULT;
> +
> +       (*offset) += header.size;
> +
> +       return 0;
> +}
> +
> +/**
> + * Copies single OA report into userspace read() buffer.
> + */
> +static int append_oa_sample(struct i915_perf_stream *stream,
> +                           char __user *buf,
> +                           size_t count,
> +                           size_t *offset,
> +                           const u8 *report)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +       int report_size = dev_priv->perf.oa.oa_buffer.format_size;
> +       struct drm_i915_perf_record_header header;
> +       u32 sample_flags = stream->sample_flags;
> +
> +       header.type = DRM_I915_PERF_RECORD_SAMPLE;
> +       header.pad = 0;
> +       header.size = stream->sample_size;
> +
> +       if ((count - *offset) < header.size)
> +               return -ENOSPC;
> +
> +       buf += *offset;
> +       if (copy_to_user(buf, &header, sizeof(header)))
> +               return -EFAULT;
> +       buf += sizeof(header);
> +
> +       if (sample_flags & SAMPLE_OA_REPORT) {
> +               if (copy_to_user(buf, report, report_size))
> +                       return -EFAULT;
> +       }
> +
> +       (*offset) += header.size;
> +
> +       return 0;
> +}
> +
> +/**
> + * Copies all buffered OA reports into userspace read() buffer.
> + * @head_ptr: (inout): the head pointer before and after appending
> + *
> + * Returns 0 on success, negative error code on failure.
> + *
> + * Notably any error condition resulting in a short read (-ENOSPC or
> + * -EFAULT) will be returned even though one or more records may
> + * have been successfully copied. In this case it's up to the caller
> + * to decide if the error should be squashed before returning to
> + * userspace.
> + */
> +static int gen7_append_oa_reports(struct i915_perf_stream *stream,
> +                                 char __user *buf,
> +                                 size_t count,
> +                                 size_t *offset,
> +                                 u32 *head_ptr,
> +                                 u32 tail)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +       int report_size = dev_priv->perf.oa.oa_buffer.format_size;
> +       u8 *oa_buf_base = dev_priv->perf.oa.oa_buffer.addr;
> +       int tail_margin = dev_priv->perf.oa.tail_margin;
> +       u32 mask = (OA_BUFFER_SIZE - 1);
> +       u32 head;
> +       u32 taken;
> +       int ret = 0;
> +
> +       BUG_ON(!stream->enabled);
> +
> +       head = *head_ptr - dev_priv->perf.oa.oa_buffer.gtt_offset;
> +       tail -= dev_priv->perf.oa.oa_buffer.gtt_offset;
> +
> +       /* The OA unit is expected to wrap the tail pointer according to the OA
> +        * buffer size and since we should never write a misaligned head
> +        * pointer we don't expect to read one back either...
> +        */
> +       if (tail > OA_BUFFER_SIZE || head > OA_BUFFER_SIZE ||
> +           head % report_size) {
> +               DRM_ERROR("Inconsistent OA buffer pointer (head = %u, tail = %u): force restart",
> +                         head, tail);
> +               dev_priv->perf.oa.ops.oa_disable(dev_priv);
> +               dev_priv->perf.oa.ops.oa_enable(dev_priv);
> +               *head_ptr = I915_READ(GEN7_OASTATUS2) &
> +                       GEN7_OASTATUS2_HEAD_MASK;
> +               return -EIO;
> +       }
> +
> +
> +       /* The tail pointer increases in 64 byte increments, not in report_size
> +        * steps...
> +        */
> +       tail &= ~(report_size - 1);
> +
> +       /* Move the tail pointer back by the current tail_margin to account for
> +        * the possibility that the latest reports may not have really landed
> +        * in memory yet...
> +        */
> +
> +       if (OA_TAKEN(tail, head) < report_size + tail_margin)
> +               return -EAGAIN;
> +
> +       tail -= tail_margin;
> +       tail &= mask;
> +
> +       for (/* none */;
> +            (taken = OA_TAKEN(tail, head));
> +            head = (head + report_size) & mask) {
> +               u8 *report = oa_buf_base + head;
> +               u32 *report32 = (void *)report;
> +
> +               /* All the report sizes factor neatly into the buffer
> +                * size so we never expect to see a report split
> +                * between the beginning and end of the buffer.
> +                *
> +                * Given the initial alignment check a misalignment
> +                * here would imply a driver bug that would result
> +                * in an overrun.
> +                */
> +               BUG_ON((OA_BUFFER_SIZE - head) < report_size);
> +
> +               /* The report-ID field for periodic samples includes
> +                * some undocumented flags related to what triggered
> +                * the report and is never expected to be zero so we
> +                * can check that the report isn't invalid before
> +                * copying it to userspace...
> +                */
> +               if (report32[0] == 0) {
> +                       DRM_ERROR("Skipping spurious, invalid OA report\n");
> +                       continue;
> +               }
> +
> +               ret = append_oa_sample(stream, buf, count, offset, report);
> +               if (ret)
> +                       break;
> +
> +               /* The above report-id field sanity check is based on
> +                * the assumption that the OA buffer is initially
> +                * zeroed and we reset the field after copying so the
> +                * check is still meaningful once old reports start
> +                * being overwritten.
> +                */
> +               report32[0] = 0;
> +       }
> +
> +       *head_ptr = dev_priv->perf.oa.oa_buffer.gtt_offset + head;
> +
> +       return ret;
> +}
> +
> +static int gen7_oa_read(struct i915_perf_stream *stream,
> +                       char __user *buf,
> +                       size_t count,
> +                       size_t *offset)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +       int report_size = dev_priv->perf.oa.oa_buffer.format_size;
> +       u32 oastatus2;
> +       u32 oastatus1;
> +       u32 head;
> +       u32 tail;
> +       int ret;
> +
> +       BUG_ON(!dev_priv->perf.oa.oa_buffer.addr);
> +
> +       oastatus2 = I915_READ(GEN7_OASTATUS2);
> +       oastatus1 = I915_READ(GEN7_OASTATUS1);
> +
> +       head = oastatus2 & GEN7_OASTATUS2_HEAD_MASK;
> +       tail = oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
> +
> +       /* XXX: On Haswell we don't have a safe way to clear oastatus1
> +        * bits while the OA unit is enabled (while the tail pointer
> +        * may be updated asynchronously) so we ignore status bits
> +        * that have already been reported to userspace.
> +        */
> +       oastatus1 &= ~dev_priv->perf.oa.gen7_latched_oastatus1;
> +
> +       /* We treat OABUFFER_OVERFLOW as a significant error:
> +        *
> +        * - The status can be interpreted to mean that the buffer is
> +        *   currently full (with a higher precedence than OA_TAKEN()
> +        *   which will start to report a near-empty buffer after an
> +        *   overflow) but it's awkward that we can't clear the status
> +        *   on Haswell, so without a reset we won't be able to catch
> +        *   the state again.
> +        *
> +        * - Since it also implies the HW has started overwriting old
> +        *   reports it may also affect our sanity checks for invalid
> +        *   reports when copying to userspace that assume new reports
> +        *   are being written to cleared memory.
> +        *
> +        * - In the future we may want to introduce a flight recorder
> +        *   mode where the driver will automatically maintain a safe
> +        *   guard band between head/tail, avoiding this overflow
> +        *   condition, but we avoid the added driver complexity for
> +        *   now.
> +        */
> +       if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
> +               ret = append_oa_status(stream, buf, count, offset,
> +                                      DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
> +               if (ret)
> +                       return ret;
> +
> +               DRM_ERROR("OA buffer overflow: force restart");
> +
> +               dev_priv->perf.oa.ops.oa_disable(dev_priv);
> +               dev_priv->perf.oa.ops.oa_enable(dev_priv);
> +
> +               oastatus2 = I915_READ(GEN7_OASTATUS2);
> +               oastatus1 = I915_READ(GEN7_OASTATUS1);
> +
> +               head = oastatus2 & GEN7_OASTATUS2_HEAD_MASK;
> +               tail = oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
> +       }
> +
> +       if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
> +               ret = append_oa_status(stream, buf, count, offset,
> +                                      DRM_I915_PERF_RECORD_OA_REPORT_LOST);
> +               if (ret)
> +                       return ret;
> +               dev_priv->perf.oa.gen7_latched_oastatus1 |=
> +                       GEN7_OASTATUS1_REPORT_LOST;
> +       }
> +
> +       ret = gen7_append_oa_reports(stream, buf, count, offset,
> +                                    &head, tail);
> +
> +       /* All the report sizes are a power of two and the
> +        * head should always be incremented by some multiple
> +        * of the report size.
> +        *
> +        * A warning here, but notably if we later read back a
> +        * misaligned pointer we will treat that as a bug since
> +        * it could lead to a buffer overrun.
> +        */
> +       WARN_ONCE(head & (report_size - 1),
> +                 "i915: Writing misaligned OA head pointer");
> +
> +       /* Note: we update the head pointer here even if an error
> +        * was returned since the error may represent a short read
> +        * where some some reports were successfully copied.
> +        */
> +       I915_WRITE(GEN7_OASTATUS2,
> +                  ((head & GEN7_OASTATUS2_HEAD_MASK) |
> +                   OA_MEM_SELECT_GGTT));
> +
> +       return ret;
> +}
> +
> +static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       /* We would wait indefinitly if periodic sampling is not enabled */
> +       if (!dev_priv->perf.oa.periodic)
> +               return -EIO;
> +
> +       /* Note: the oa_buffer_is_empty() condition is ok to run unlocked as it
> +        * just performs mmio reads of the OA buffer head + tail pointers and
> +        * it's assumed we're handling some operation that implies the stream
> +        * can't be destroyed until completion (such as a read()) that ensures
> +        * the device + OA buffer can't disappear
> +        */
> +       return wait_event_interruptible(dev_priv->perf.oa.poll_wq,
> +                                       !dev_priv->perf.oa.ops.oa_buffer_is_empty(dev_priv));
> +}
> +
> +static void i915_oa_poll_wait(struct i915_perf_stream *stream,
> +                             struct file *file,
> +                             poll_table *wait)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       poll_wait(file, &dev_priv->perf.oa.poll_wq, wait);
> +}
> +
> +static int i915_oa_read(struct i915_perf_stream *stream,
> +                       char __user *buf,
> +                       size_t count,
> +                       size_t *offset)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       return dev_priv->perf.oa.ops.read(stream, buf, count, offset);
> +}
> +
> +static void
> +free_oa_buffer(struct drm_i915_private *i915)
> +{
> +       mutex_lock(&i915->drm.struct_mutex);
> +
> +       i915_gem_object_unpin_map(i915->perf.oa.oa_buffer.obj);
> +       __i915_vma_unpin(i915->perf.oa.oa_buffer.vma);
> +       i915_gem_object_put(i915->perf.oa.oa_buffer.obj);
> +
> +       i915->perf.oa.oa_buffer.obj = NULL;
> +       i915->perf.oa.oa_buffer.gtt_offset = 0;
> +       i915->perf.oa.oa_buffer.addr = NULL;
> +
> +       mutex_unlock(&i915->drm.struct_mutex);
> +}
> +
> +static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       BUG_ON(stream != dev_priv->perf.oa.exclusive_stream);
> +
> +       dev_priv->perf.oa.ops.disable_metric_set(dev_priv);
> +
> +       free_oa_buffer(dev_priv);
> +
> +       intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
> +       intel_runtime_pm_put(dev_priv);
> +
> +       dev_priv->perf.oa.exclusive_stream = NULL;
> +}
> +
> +static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv)
> +{
> +       /* Pre-DevBDW: OABUFFER must be set with counters off,
> +        * before OASTATUS1, but after OASTATUS2
> +        */
> +       I915_WRITE(GEN7_OASTATUS2, dev_priv->perf.oa.oa_buffer.gtt_offset |
> +                  OA_MEM_SELECT_GGTT); /* head */
> +       I915_WRITE(GEN7_OABUFFER, dev_priv->perf.oa.oa_buffer.gtt_offset);
> +       I915_WRITE(GEN7_OASTATUS1, dev_priv->perf.oa.oa_buffer.gtt_offset |
> +                  OABUFFER_SIZE_16M); /* tail */
> +
> +       /* On Haswell we have to track which OASTATUS1 flags we've
> +        * already seen since they can't be cleared while periodic
> +        * sampling is enabled.
> +        */
> +       dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
> +
> +       /* NB: although the OA buffer will initially be allocated
> +        * zeroed via shmfs (and so this memset is redundant when
> +        * first allocating), we may re-init the OA buffer, either
> +        * when re-enabling a stream or in error/reset paths.
> +        *
> +        * The reason we clear the buffer for each re-init is for the
> +        * sanity check in gen7_append_oa_reports() that looks at the
> +        * report-id field to make sure it's non-zero which relies on
> +        * the assumption that new reports are being written to zeroed
> +        * memory...
> +        */
> +       memset(dev_priv->perf.oa.oa_buffer.addr, 0, SZ_16M);
OA_BUFFER_SIZE

> +
> +       /* Maybe make ->pollin per-stream state if we support multiple
> +        * concurrent streams in the future. */
> +       atomic_set(&dev_priv->perf.oa.pollin, false);
> +}
> +
> +static int alloc_oa_buffer(struct drm_i915_private *dev_priv)
> +{
> +       struct drm_i915_gem_object *bo;
> +       enum i915_map_type map;
> +       struct i915_vma *vma;
> +       int ret;
> +
> +       BUG_ON(dev_priv->perf.oa.oa_buffer.obj);
> +
> +       ret = i915_mutex_lock_interruptible(&dev_priv->drm);
> +       if (ret)
> +               return ret;
> +
> +       bo = i915_gem_object_create(&dev_priv->drm, OA_BUFFER_SIZE);
> +       if (IS_ERR(bo)) {
> +               DRM_ERROR("Failed to allocate OA buffer\n");
> +               ret = PTR_ERR(bo);
> +               goto unlock;
> +       }
> +       dev_priv->perf.oa.oa_buffer.obj = bo;
> +
> +       ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC);
> +       if (ret)
> +               goto err_unref;
> +
> +       /* PreHSW required 512K alignment, HSW requires 16M */
> +       vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M, PIN_MAPPABLE);
> +       if (IS_ERR(vma)) {
> +               ret = PTR_ERR(vma);
> +               goto err_unref;
> +       }
> +       dev_priv->perf.oa.oa_buffer.vma = vma;
> +
> +       map = HAS_LLC(dev_priv) ? I915_MAP_WB : I915_MAP_WC;
> +
> +       dev_priv->perf.oa.oa_buffer.gtt_offset = i915_ggtt_offset(vma);
> +       dev_priv->perf.oa.oa_buffer.addr = i915_gem_object_pin_map(bo, map);
> +       if (dev_priv->perf.oa.oa_buffer.addr == NULL)
i915_gem_object_pin_map can't return NULL.

> +               goto err_unpin;
> +
> +       dev_priv->perf.oa.ops.init_oa_buffer(dev_priv);
> +
> +       DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x, vaddr = %p",
> +                        dev_priv->perf.oa.oa_buffer.gtt_offset,
> +                        dev_priv->perf.oa.oa_buffer.addr);
> +
> +       goto unlock;
> +
> +err_unpin:
> +       __i915_vma_unpin(vma);
> +
> +err_unref:
> +       i915_gem_object_put(bo);
> +
> +       dev_priv->perf.oa.oa_buffer.gtt_offset = 0;
> +       dev_priv->perf.oa.oa_buffer.addr = NULL;
> +       dev_priv->perf.oa.oa_buffer.vma = NULL;
> +       dev_priv->perf.oa.oa_buffer.obj = NULL;
> +
> +unlock:
> +       mutex_unlock(&dev_priv->drm.struct_mutex);
> +       return ret;
> +}
> +
> +static void config_oa_regs(struct drm_i915_private *dev_priv,
> +                          const struct i915_oa_reg *regs,
> +                          int n_regs)
> +{
> +       int i;
> +
> +       for (i = 0; i < n_regs; i++) {
> +               const struct i915_oa_reg *reg = regs + i;
> +
> +               I915_WRITE(reg->addr, reg->value);
> +       }
> +}
> +
> +static int hsw_enable_metric_set(struct drm_i915_private *dev_priv)
> +{
> +       int ret = i915_oa_select_metric_set_hsw(dev_priv);
> +
> +       if (ret)
> +               return ret;
> +
> +       I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) |
> +                                     GT_NOA_ENABLE));
> +
> +       /* PRM:
> +        *
> +        * OA unit is using “crclk” for its functionality. When trunk
> +        * level clock gating takes place, OA clock would be gated,
> +        * unable to count the events from non-render clock domain.
> +        * Render clock gating must be disabled when OA is enabled to
> +        * count the events from non-render domain. Unit level clock
> +        * gating for RCS should also be disabled.
> +        */
> +       I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
> +                                   ~GEN7_DOP_CLOCK_GATE_ENABLE));
> +       I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) |
> +                                 GEN6_CSUNIT_CLOCK_GATE_DISABLE));
> +
> +       config_oa_regs(dev_priv, dev_priv->perf.oa.mux_regs,
> +                      dev_priv->perf.oa.mux_regs_len);
> +
> +       /* It apparently takes a fairly long time for a new MUX
> +        * configuration to be be applied after these register writes.
> +        * This delay duration was derived empirically based on the
> +        * render_basic config but hopefully it covers the maximum
> +        * configuration latency.
> +        *
> +        * As a fallback, the checks in _append_oa_reports() to skip
> +        * invalid OA reports do also seem to work to discard reports
> +        * generated before this config has completed - albeit not
> +        * silently.
> +        *
> +        * Unfortunately this is essentially a magic number, since we
> +        * don't currently know of a reliable mechanism for predicting
> +        * how long the MUX config will take to apply and besides
> +        * seeing invalid reports we don't know of a reliable way to
> +        * explicitly check that the MUX config has landed.
> +        *
> +        * It's even possible we've miss characterized the underlying
> +        * problem - it just seems like the simplest explanation why
> +        * a delay at this location would mitigate any invalid reports.
> +        */
> +       usleep_range(15000, 20000);
> +
> +       config_oa_regs(dev_priv, dev_priv->perf.oa.b_counter_regs,
> +                      dev_priv->perf.oa.b_counter_regs_len);
> +
> +       return 0;
> +}
> +
> +static void hsw_disable_metric_set(struct drm_i915_private *dev_priv)
> +{
> +       I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) &
> +                                 ~GEN6_CSUNIT_CLOCK_GATE_DISABLE));
> +       I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) |
> +                                   GEN7_DOP_CLOCK_GATE_ENABLE));
> +
> +       I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
> +                                     ~GT_NOA_ENABLE));
> +}
> +
> +static void gen7_update_oacontrol_locked(struct drm_i915_private *dev_priv)
> +{
> +       assert_spin_locked(&dev_priv->perf.hook_lock);
> +
> +       if (dev_priv->perf.oa.exclusive_stream->enabled) {
> +               unsigned long ctx_id = 0;
> +
> +               if (dev_priv->perf.oa.exclusive_stream->ctx)
> +                       ctx_id = dev_priv->perf.oa.specific_ctx_id;
> +
> +               if (dev_priv->perf.oa.exclusive_stream->ctx == NULL || ctx_id) {
gem context state pinned at ggtt offset zero is unlikely but still
possible, as pointed
out by Chris.

> +                       bool periodic = dev_priv->perf.oa.periodic;
> +                       u32 period_exponent = dev_priv->perf.oa.period_exponent;
> +                       u32 report_format = dev_priv->perf.oa.oa_buffer.format;
> +
> +                       I915_WRITE(GEN7_OACONTROL,
> +                                  (ctx_id & GEN7_OACONTROL_CTX_MASK) |
> +                                  (period_exponent <<
> +                                   GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
> +                                  (periodic ?
> +                                   GEN7_OACONTROL_TIMER_ENABLE : 0) |
> +                                  (report_format <<
> +                                   GEN7_OACONTROL_FORMAT_SHIFT) |
> +                                  (ctx_id ?
> +                                   GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
> +                                  GEN7_OACONTROL_ENABLE);
> +                       return;
> +               }
> +       }
> +
> +       I915_WRITE(GEN7_OACONTROL, 0);
> +}
> +
> +static void gen7_oa_enable(struct drm_i915_private *dev_priv)
> +{
> +       unsigned long flags;
> +
> +       /* Reset buf pointers so we don't forward reports from before now.
> +        *
> +        * Think carefully if considering trying to avoid this, since it
> +        * also ensures status flags and the buffer itself are cleared
> +        * in error paths, and we have checks for invalid reports based
> +        * on the assumption that certain fields are written to zeroed
> +        * memory which this helps maintains.
> +        */
> +       gen7_init_oa_buffer(dev_priv);
> +
> +       spin_lock_irqsave(&dev_priv->perf.hook_lock, flags);
> +       gen7_update_oacontrol_locked(dev_priv);
> +       spin_unlock_irqrestore(&dev_priv->perf.hook_lock, flags);
> +}
> +
> +static void i915_oa_stream_enable(struct i915_perf_stream *stream)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       dev_priv->perf.oa.ops.oa_enable(dev_priv);
> +
> +       if (dev_priv->perf.oa.periodic)
> +               hrtimer_start(&dev_priv->perf.oa.poll_check_timer,
> +                             ns_to_ktime(POLL_PERIOD),
> +                             HRTIMER_MODE_REL_PINNED);
> +}
> +
> +static void gen7_oa_disable(struct drm_i915_private *dev_priv)
> +{
> +       I915_WRITE(GEN7_OACONTROL, 0);
> +}
> +
> +static void i915_oa_stream_disable(struct i915_perf_stream *stream)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +
> +       dev_priv->perf.oa.ops.oa_disable(dev_priv);
> +
> +       if (dev_priv->perf.oa.periodic)
> +               hrtimer_cancel(&dev_priv->perf.oa.poll_check_timer);
> +}
> +
> +static u64 oa_exponent_to_ns(struct drm_i915_private *dev_priv, int exponent)
> +{
> +       return 1000000000ULL * (2ULL << exponent) /
> +               dev_priv->perf.oa.timestamp_frequency;
> +}
> +
> +static const struct i915_perf_stream_ops i915_oa_stream_ops = {
> +       .destroy = i915_oa_stream_destroy,
> +       .enable = i915_oa_stream_enable,
> +       .disable = i915_oa_stream_disable,
> +       .wait_unlocked = i915_oa_wait_unlocked,
> +       .poll_wait = i915_oa_poll_wait,
> +       .read = i915_oa_read,
> +};
> +
> +static int i915_oa_stream_init(struct i915_perf_stream *stream,
> +                              struct drm_i915_perf_open_param *param,
> +                              struct perf_open_properties *props)
> +{
> +       struct drm_i915_private *dev_priv = stream->dev_priv;
> +       int format_size;
> +       int ret;
> +
> +       if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
> +               DRM_ERROR("Only OA report sampling supported\n");
> +               return -EINVAL;
> +       }
> +
> +       if (!dev_priv->perf.oa.ops.init_oa_buffer) {
> +               DRM_ERROR("OA unit not supported\n");
> +               return -ENODEV;
> +       }
> +
> +       /* To avoid the complexity of having to accurately filter
> +        * counter reports and marshal to the appropriate client
> +        * we currently only allow exclusive access
> +        */
> +       if (dev_priv->perf.oa.exclusive_stream) {
> +               DRM_ERROR("OA unit already in use\n");
> +               return -EBUSY;
> +       }
> +
> +       if (!props->metrics_set) {
> +               DRM_ERROR("OA metric set not specified\n");
> +               return -EINVAL;
> +       }
> +
> +       if (!props->oa_format) {
> +               DRM_ERROR("OA report format not specified\n");
> +               return -EINVAL;
> +       }
> +
> +       stream->sample_size = sizeof(struct drm_i915_perf_record_header);
> +
> +       format_size = dev_priv->perf.oa.oa_formats[props->oa_format].size;
> +
> +       stream->sample_flags |= SAMPLE_OA_REPORT;
> +       stream->sample_size += format_size;
> +
> +       dev_priv->perf.oa.oa_buffer.format_size = format_size;
> +       BUG_ON(dev_priv->perf.oa.oa_buffer.format_size == 0);
> +
> +       dev_priv->perf.oa.oa_buffer.format =
> +               dev_priv->perf.oa.oa_formats[props->oa_format].format;
> +
> +       dev_priv->perf.oa.metrics_set = props->metrics_set;
> +
> +       dev_priv->perf.oa.periodic = props->oa_periodic;
> +       if (dev_priv->perf.oa.periodic) {
> +               u64 period_ns = oa_exponent_to_ns(dev_priv,
> +                                                 props->oa_period_exponent);
> +
> +               dev_priv->perf.oa.period_exponent = props->oa_period_exponent;
> +
> +               /* See comment for OA_TAIL_MARGIN_NSEC for details
> +                * about this tail_margin...
> +                */
> +               dev_priv->perf.oa.tail_margin =
> +                       ((OA_TAIL_MARGIN_NSEC / period_ns) + 1) * format_size;
> +       }
> +
> +       ret = alloc_oa_buffer(dev_priv);
> +       if (ret)
> +               return ret;
> +
> +       /* PRM - observability performance counters:
> +        *
> +        *   OACONTROL, performance counter enable, note:
> +        *
> +        *   "When this bit is set, in order to have coherent counts,
> +        *   RC6 power state and trunk clock gating must be disabled.
> +        *   This can be achieved by programming MMIO registers as
> +        *   0xA094=0 and 0xA090[31]=1"
> +        *
> +        *   In our case we are expecting that taking pm + FORCEWAKE
> +        *   references will effectively disable RC6.
> +        */
> +       intel_runtime_pm_get(dev_priv);
> +       intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
> +
> +       ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv);
> +       if (ret) {
> +               intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
> +               intel_runtime_pm_put(dev_priv);
> +               free_oa_buffer(dev_priv);
> +               return ret;
> +       }
> +
> +       stream->ops = &i915_oa_stream_ops;
> +
> +       /* On Haswell we have to track which OASTATUS1 flags we've already
> +        * seen since they can't be cleared while periodic sampling is enabled.
> +        */
> +       dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
We already did this step.

> +
> +       dev_priv->perf.oa.exclusive_stream = stream;
> +
> +       return 0;
> +}
> +
> +static void gen7_update_hw_ctx_id_locked(struct drm_i915_private *dev_priv,
> +                                        u32 ctx_id)
> +{
> +       assert_spin_locked(&dev_priv->perf.hook_lock);
> +
> +       dev_priv->perf.oa.specific_ctx_id = ctx_id;
> +       gen7_update_oacontrol_locked(dev_priv);
> +}
> +
> +static void
> +i915_oa_legacy_context_pin_notify_locked(struct drm_i915_private *dev_priv,
> +                                        struct i915_gem_context *ctx)
> +{
> +       assert_spin_locked(&dev_priv->perf.hook_lock);
> +
> +       BUG_ON(i915.enable_execlists);
> +
> +       if (dev_priv->perf.oa.ops.update_hw_ctx_id_locked == NULL)
For consistency, if (!dev_priv->perf.oa.ops.update_hw_ctx_id_locked)

> +               return;
> +
> +       if (dev_priv->perf.oa.exclusive_stream &&
> +           dev_priv->perf.oa.exclusive_stream->ctx == ctx) {
> +               struct i915_vma *vma = ctx->engine[RCS].state;
> +               u32 ctx_id = i915_ggtt_offset(vma);
> +
> +               dev_priv->perf.oa.ops.update_hw_ctx_id_locked(dev_priv, ctx_id);
> +       }
> +}
> +
> +void i915_oa_legacy_context_pin_notify(struct drm_i915_private *dev_priv,
> +                                      struct i915_gem_context *ctx)
> +{
> +       unsigned long flags;
> +
> +       if (!dev_priv->perf.initialized)
> +               return;
> +
> +       spin_lock_irqsave(&dev_priv->perf.hook_lock, flags);
> +       i915_oa_legacy_context_pin_notify_locked(dev_priv, ctx);
> +       spin_unlock_irqrestore(&dev_priv->perf.hook_lock, flags);
> +}
> +
>  static ssize_t i915_perf_read_locked(struct i915_perf_stream *stream,
>                                      struct file *file,
>                                      char __user *buf,
> @@ -79,8 +925,20 @@ static ssize_t i915_perf_read(struct file *file,
>         struct drm_i915_private *dev_priv = stream->dev_priv;
>         ssize_t ret;
>
> +       /* To ensure it's handled consistently we simply treat all reads of a
> +        * disabled stream as an error. In particular it might otherwise lead
> +        * to a deadlock for blocking file descriptors...
> +        */
> +       if (!stream->enabled)
> +               return -EIO;
> +
>         if (!(file->f_flags & O_NONBLOCK)) {
> -               /* Allow false positives from stream->ops->wait_unlocked.
> +               /* There's the small chance of false positives from
> +                * stream->ops->wait_unlocked.
> +                *
> +                * E.g. with single context filtering since we only wait until
> +                * oabuffer has >= 1 report we don't immediately know whether
> +                * any reports really belong to the current context
>                  */
>                 do {
>                         ret = stream->ops->wait_unlocked(stream);
> @@ -98,21 +956,50 @@ static ssize_t i915_perf_read(struct file *file,
>                 mutex_unlock(&dev_priv->perf.lock);
>         }
>
> +       if (ret >= 0) {
> +               /* Maybe make ->pollin per-stream state if we support multiple
> +                * concurrent streams in the future. */
> +               atomic_set(&dev_priv->perf.oa.pollin, false);
> +       }
> +
>         return ret;
>  }
>
> -static unsigned int i915_perf_poll_locked(struct i915_perf_stream *stream,
> +static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
> +{
> +       struct drm_i915_private *dev_priv =
> +               container_of(hrtimer, typeof(*dev_priv),
> +                            perf.oa.poll_check_timer);
> +
> +       if (!dev_priv->perf.oa.ops.oa_buffer_is_empty(dev_priv)) {
> +               atomic_set(&dev_priv->perf.oa.pollin, true);
> +               wake_up(&dev_priv->perf.oa.poll_wq);
> +       }
> +
> +       hrtimer_forward_now(hrtimer, ns_to_ktime(POLL_PERIOD));
> +
> +       return HRTIMER_RESTART;
> +}
> +
> +static unsigned int i915_perf_poll_locked(struct drm_i915_private *dev_priv,
> +                                         struct i915_perf_stream *stream,
>                                           struct file *file,
>                                           poll_table *wait)
>  {
> -       unsigned int streams = 0;
> +       unsigned int events = 0;
>
>         stream->ops->poll_wait(stream, file, wait);
>
> -       if (stream->ops->can_read(stream))
> -               streams |= POLLIN;
> +       /* Note: we don't explicitly check whether there's something to read
> +        * here since this path may be very hot depending on what else
> +        * userspace is polling, or on the timeout in use. We rely solely on
> +        * the hrtimer/oa_poll_check_timer_cb to notify us when there are
> +        * samples to read.
> +        */
> +       if (atomic_read(&dev_priv->perf.oa.pollin))
> +               events |= POLLIN;
>
> -       return streams;
> +       return events;
>  }
>
>  static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
> @@ -122,7 +1009,7 @@ static unsigned int i915_perf_poll(struct file *file, poll_table *wait)
>         int ret;
>
>         mutex_lock(&dev_priv->perf.lock);
> -       ret = i915_perf_poll_locked(stream, file, wait);
> +       ret = i915_perf_poll_locked(dev_priv, stream, file, wait);
>         mutex_unlock(&dev_priv->perf.lock);
>
>         return ret;
> @@ -286,18 +1173,18 @@ int i915_perf_open_ioctl_locked(struct drm_device *dev,
>                 goto err_ctx;
>         }
>
> -       stream->sample_flags = props->sample_flags;
>         stream->dev_priv = dev_priv;
>         stream->ctx = specific_ctx;
>
> -       /*
> -        * TODO: support sampling something
> -        *
> -        * For now this is as far as we can go.
> +       ret = i915_oa_stream_init(stream, param, props);
> +       if (ret)
> +               goto err_alloc;
> +
> +       /* we avoid simply assigning stream->sample_flags = props->sample_flags
> +        * to have _stream_init check the combination of sample flags more
> +        * thoroughly, but still this is the expected result at this point.
>          */
> -       DRM_ERROR("Unsupported i915 perf stream configuration\n");
> -       ret = -EINVAL;
> -       goto err_alloc;
> +       BUG_ON(stream->sample_flags != props->sample_flags);
>
>         list_add(&stream->link, &dev_priv->perf.streams);
>
> @@ -382,7 +1269,56 @@ static int read_properties_unlocked(struct drm_i915_private *dev_priv,
>                         props->single_context = 1;
>                         props->ctx_handle = value;
>                         break;
> -
> +               case DRM_I915_PERF_PROP_SAMPLE_OA:
> +                       props->sample_flags |= SAMPLE_OA_REPORT;
> +                       break;
> +               case DRM_I915_PERF_PROP_OA_METRICS_SET:
> +                       if (value == 0 ||
> +                           value > dev_priv->perf.oa.n_builtin_sets) {
> +                               DRM_ERROR("Unknown OA metric set ID");
> +                               return -EINVAL;
> +                       }
> +                       props->metrics_set = value;
> +                       break;
> +               case DRM_I915_PERF_PROP_OA_FORMAT:
> +                       if (value == 0 || value >= I915_OA_FORMAT_MAX) {
> +                               DRM_ERROR("Invalid OA report format\n");
> +                               return -EINVAL;
> +                       }
> +                       if (!dev_priv->perf.oa.oa_formats[value].size) {
> +                               DRM_ERROR("Invalid OA report format\n");
> +                               return -EINVAL;
> +                       }
> +                       props->oa_format = value;
> +                       break;
> +               case DRM_I915_PERF_PROP_OA_EXPONENT:
> +                       if (value > OA_EXPONENT_MAX) {
> +                               DRM_ERROR("OA timer exponent too high (> %u)\n",
> +                                         OA_EXPONENT_MAX);
> +                               return -EINVAL;
> +                       }
> +
> +                       /* NB: The exponent represents a period as follows:
> +                        *
> +                        *   80ns * 2^(period_exponent + 1)
> +                        *
> +                        * Theoretically we can program the OA unit to sample
> +                        * every 160ns but don't allow that by default unless
> +                        * root.
> +                        *
> +                        * Referring to perf's
> +                        * kernel.perf_event_max_sample_rate for a precedent
> +                        * (100000 by default); with an OA exponent of 6 we get
> +                        * a period of 10.240 microseconds -just under 100000Hz
> +                        */
> +                       if (value < 6 && !capable(CAP_SYS_ADMIN)) {
> +                               DRM_ERROR("Sampling period too high without root privileges\n");
> +                               return -EACCES;
> +                       }
> +
> +                       props->oa_periodic = true;
> +                       props->oa_period_exponent = value;
> +                       break;
>                 case DRM_I915_PERF_PROP_MAX:
>                         BUG();
>                 }
> @@ -431,8 +1367,35 @@ int i915_perf_open_ioctl(struct drm_device *dev, void *data,
>
>  void i915_perf_init(struct drm_i915_private *dev_priv)
>  {
> +       if (!IS_HASWELL(dev_priv))
> +               return;
> +
> +       hrtimer_init(&dev_priv->perf.oa.poll_check_timer,
> +                    CLOCK_MONOTONIC, HRTIMER_MODE_REL);
> +       dev_priv->perf.oa.poll_check_timer.function = oa_poll_check_timer_cb;
> +       init_waitqueue_head(&dev_priv->perf.oa.poll_wq);
> +
>         INIT_LIST_HEAD(&dev_priv->perf.streams);
>         mutex_init(&dev_priv->perf.lock);
> +       spin_lock_init(&dev_priv->perf.hook_lock);
> +
> +       dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer;
> +       dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set;
> +       dev_priv->perf.oa.ops.disable_metric_set = hsw_disable_metric_set;
> +       dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable;
> +       dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable;
> +       dev_priv->perf.oa.ops.update_hw_ctx_id_locked =
> +               gen7_update_hw_ctx_id_locked;
> +       dev_priv->perf.oa.ops.read = gen7_oa_read;
> +       dev_priv->perf.oa.ops.oa_buffer_is_empty =
> +               gen7_oa_buffer_is_empty_fop_unlocked;
> +
> +       dev_priv->perf.oa.timestamp_frequency = 12500000;
Slightly magical.

> +
> +       dev_priv->perf.oa.oa_formats = hsw_oa_formats;
> +
> +       dev_priv->perf.oa.n_builtin_sets =
> +               i915_oa_n_builtin_metric_sets_hsw;
>
>         dev_priv->perf.initialized = true;
>  }
> @@ -442,7 +1405,6 @@ void i915_perf_fini(struct drm_i915_private *dev_priv)
>         if (!dev_priv->perf.initialized)
>                 return;
>
> -       /* Currently nothing to clean up */
> -
> +       memset(&dev_priv->perf.oa.ops, 0, sizeof(dev_priv->perf.oa.ops));
>         dev_priv->perf.initialized = false;
>  }
> diff --git a/drivers/gpu/drm/i915/i915_reg.h b/drivers/gpu/drm/i915/i915_reg.h
> index 90756b2..a0ccc09 100644
> --- a/drivers/gpu/drm/i915/i915_reg.h
> +++ b/drivers/gpu/drm/i915/i915_reg.h
> @@ -617,6 +617,343 @@ static inline bool i915_mmio_reg_valid(i915_reg_t reg)
>  #define HSW_CS_GPR_UDW(n)               _MMIO(0x2600 + (n) * 8 + 4)
>
>  #define GEN7_OACONTROL _MMIO(0x2360)
> +#define  GEN7_OACONTROL_CTX_MASK           0xFFFFF000
> +#define  GEN7_OACONTROL_TIMER_PERIOD_MASK   0x3F
> +#define  GEN7_OACONTROL_TIMER_PERIOD_SHIFT  6
> +#define  GEN7_OACONTROL_TIMER_ENABLE       (1<<5)
> +#define  GEN7_OACONTROL_FORMAT_A13         (0<<2)
> +#define  GEN7_OACONTROL_FORMAT_A29         (1<<2)
> +#define  GEN7_OACONTROL_FORMAT_A13_B8_C8    (2<<2)
> +#define  GEN7_OACONTROL_FORMAT_A29_B8_C8    (3<<2)
> +#define  GEN7_OACONTROL_FORMAT_B4_C8       (4<<2)
> +#define  GEN7_OACONTROL_FORMAT_A45_B8_C8    (5<<2)
> +#define  GEN7_OACONTROL_FORMAT_B4_C8_A16    (6<<2)
> +#define  GEN7_OACONTROL_FORMAT_C4_B8       (7<<2)
> +#define  GEN7_OACONTROL_FORMAT_SHIFT       2
> +#define  GEN7_OACONTROL_PER_CTX_ENABLE     (1<<1)
> +#define  GEN7_OACONTROL_ENABLE             (1<<0)
> +
> +#define GEN8_OACTXID _MMIO(0x2364)
> +
> +#define GEN8_OACONTROL _MMIO(0x2B00)
> +#define  GEN8_OA_REPORT_FORMAT_A12         (0<<2)
> +#define  GEN8_OA_REPORT_FORMAT_A12_B8_C8    (2<<2)
> +#define  GEN8_OA_REPORT_FORMAT_A36_B8_C8    (5<<2)
> +#define  GEN8_OA_REPORT_FORMAT_C4_B8       (7<<2)
> +#define  GEN8_OA_REPORT_FORMAT_SHIFT       2
> +#define  GEN8_OA_SPECIFIC_CONTEXT_ENABLE    (1<<1)
> +#define  GEN8_OA_COUNTER_ENABLE             (1<<0)
> +
> +#define GEN8_OACTXCONTROL _MMIO(0x2360)
> +#define  GEN8_OA_TIMER_PERIOD_MASK         0x3F
> +#define  GEN8_OA_TIMER_PERIOD_SHIFT        2
> +#define  GEN8_OA_TIMER_ENABLE              (1<<1)
> +#define  GEN8_OA_COUNTER_RESUME                    (1<<0)
> +
> +#define GEN7_OABUFFER _MMIO(0x23B0) /* R/W */
> +#define  GEN7_OABUFFER_OVERRUN_DISABLE     (1<<3)
> +#define  GEN7_OABUFFER_EDGE_TRIGGER        (1<<2)
> +#define  GEN7_OABUFFER_STOP_RESUME_ENABLE   (1<<1)
> +#define  GEN7_OABUFFER_RESUME              (1<<0)
> +
> +#define GEN8_OABUFFER _MMIO(0x2b14)
> +
> +#define GEN7_OASTATUS1 _MMIO(0x2364)
> +#define  GEN7_OASTATUS1_TAIL_MASK          0xffffffc0
> +#define  GEN7_OASTATUS1_COUNTER_OVERFLOW    (1<<2)
> +#define  GEN7_OASTATUS1_OABUFFER_OVERFLOW   (1<<1)
> +#define  GEN7_OASTATUS1_REPORT_LOST        (1<<0)
> +
> +#define GEN7_OASTATUS2 _MMIO(0x2368)
> +#define GEN7_OASTATUS2_HEAD_MASK    0xffffffc0
> +
> +#define GEN8_OASTATUS _MMIO(0x2b08)
> +#define  GEN8_OASTATUS_OVERRUN_STATUS      (1<<3)
> +#define  GEN8_OASTATUS_COUNTER_OVERFLOW     (1<<2)
> +#define  GEN8_OASTATUS_OABUFFER_OVERFLOW    (1<<1)
> +#define  GEN8_OASTATUS_REPORT_LOST         (1<<0)
> +
> +#define GEN8_OAHEADPTR _MMIO(0x2B0C)
> +#define GEN8_OATAILPTR _MMIO(0x2B10)
> +
> +#define OABUFFER_SIZE_128K  (0<<3)
> +#define OABUFFER_SIZE_256K  (1<<3)
> +#define OABUFFER_SIZE_512K  (2<<3)
> +#define OABUFFER_SIZE_1M    (3<<3)
> +#define OABUFFER_SIZE_2M    (4<<3)
> +#define OABUFFER_SIZE_4M    (5<<3)
> +#define OABUFFER_SIZE_8M    (6<<3)
> +#define OABUFFER_SIZE_16M   (7<<3)
> +
> +#define OA_MEM_SELECT_GGTT  (1<<0)
> +
> +#define EU_PERF_CNTL0      _MMIO(0xe458)
> +
> +#define GDT_CHICKEN_BITS    _MMIO(0x9840)
> +#define GT_NOA_ENABLE      0x00000080
> +
> +/*
> + * OA Boolean state
> + */
> +
> +#define OAREPORTTRIG1 _MMIO(0x2740)
> +#define OAREPORTTRIG1_THRESHOLD_MASK 0xffff
> +#define OAREPORTTRIG1_EDGE_LEVEL_TRIGER_SELECT_MASK 0xffff0000 /* 0=level */
> +
> +#define OAREPORTTRIG2 _MMIO(0x2744)
> +#define OAREPORTTRIG2_INVERT_A_0  (1<<0)
> +#define OAREPORTTRIG2_INVERT_A_1  (1<<1)
> +#define OAREPORTTRIG2_INVERT_A_2  (1<<2)
> +#define OAREPORTTRIG2_INVERT_A_3  (1<<3)
> +#define OAREPORTTRIG2_INVERT_A_4  (1<<4)
> +#define OAREPORTTRIG2_INVERT_A_5  (1<<5)
> +#define OAREPORTTRIG2_INVERT_A_6  (1<<6)
> +#define OAREPORTTRIG2_INVERT_A_7  (1<<7)
> +#define OAREPORTTRIG2_INVERT_A_8  (1<<8)
> +#define OAREPORTTRIG2_INVERT_A_9  (1<<9)
> +#define OAREPORTTRIG2_INVERT_A_10 (1<<10)
> +#define OAREPORTTRIG2_INVERT_A_11 (1<<11)
> +#define OAREPORTTRIG2_INVERT_A_12 (1<<12)
> +#define OAREPORTTRIG2_INVERT_A_13 (1<<13)
> +#define OAREPORTTRIG2_INVERT_A_14 (1<<14)
> +#define OAREPORTTRIG2_INVERT_A_15 (1<<15)
> +#define OAREPORTTRIG2_INVERT_B_0  (1<<16)
> +#define OAREPORTTRIG2_INVERT_B_1  (1<<17)
> +#define OAREPORTTRIG2_INVERT_B_2  (1<<18)
> +#define OAREPORTTRIG2_INVERT_B_3  (1<<19)
> +#define OAREPORTTRIG2_INVERT_C_0  (1<<20)
> +#define OAREPORTTRIG2_INVERT_C_1  (1<<21)
> +#define OAREPORTTRIG2_INVERT_D_0  (1<<22)
> +#define OAREPORTTRIG2_THRESHOLD_ENABLE     (1<<23)
> +#define OAREPORTTRIG2_REPORT_TRIGGER_ENABLE (1<<31)
> +
> +#define OAREPORTTRIG3 _MMIO(0x2748)
> +#define OAREPORTTRIG3_NOA_SELECT_MASK      0xf
> +#define OAREPORTTRIG3_NOA_SELECT_8_SHIFT    0
> +#define OAREPORTTRIG3_NOA_SELECT_9_SHIFT    4
> +#define OAREPORTTRIG3_NOA_SELECT_10_SHIFT   8
> +#define OAREPORTTRIG3_NOA_SELECT_11_SHIFT   12
> +#define OAREPORTTRIG3_NOA_SELECT_12_SHIFT   16
> +#define OAREPORTTRIG3_NOA_SELECT_13_SHIFT   20
> +#define OAREPORTTRIG3_NOA_SELECT_14_SHIFT   24
> +#define OAREPORTTRIG3_NOA_SELECT_15_SHIFT   28
> +
> +#define OAREPORTTRIG4 _MMIO(0x274c)
> +#define OAREPORTTRIG4_NOA_SELECT_MASK      0xf
> +#define OAREPORTTRIG4_NOA_SELECT_0_SHIFT    0
> +#define OAREPORTTRIG4_NOA_SELECT_1_SHIFT    4
> +#define OAREPORTTRIG4_NOA_SELECT_2_SHIFT    8
> +#define OAREPORTTRIG4_NOA_SELECT_3_SHIFT    12
> +#define OAREPORTTRIG4_NOA_SELECT_4_SHIFT    16
> +#define OAREPORTTRIG4_NOA_SELECT_5_SHIFT    20
> +#define OAREPORTTRIG4_NOA_SELECT_6_SHIFT    24
> +#define OAREPORTTRIG4_NOA_SELECT_7_SHIFT    28
> +
> +#define OAREPORTTRIG5 _MMIO(0x2750)
> +#define OAREPORTTRIG5_THRESHOLD_MASK 0xffff
> +#define OAREPORTTRIG5_EDGE_LEVEL_TRIGER_SELECT_MASK 0xffff0000 /* 0=level */
> +
> +#define OAREPORTTRIG6 _MMIO(0x2754)
> +#define OAREPORTTRIG6_INVERT_A_0  (1<<0)
> +#define OAREPORTTRIG6_INVERT_A_1  (1<<1)
> +#define OAREPORTTRIG6_INVERT_A_2  (1<<2)
> +#define OAREPORTTRIG6_INVERT_A_3  (1<<3)
> +#define OAREPORTTRIG6_INVERT_A_4  (1<<4)
> +#define OAREPORTTRIG6_INVERT_A_5  (1<<5)
> +#define OAREPORTTRIG6_INVERT_A_6  (1<<6)
> +#define OAREPORTTRIG6_INVERT_A_7  (1<<7)
> +#define OAREPORTTRIG6_INVERT_A_8  (1<<8)
> +#define OAREPORTTRIG6_INVERT_A_9  (1<<9)
> +#define OAREPORTTRIG6_INVERT_A_10 (1<<10)
> +#define OAREPORTTRIG6_INVERT_A_11 (1<<11)
> +#define OAREPORTTRIG6_INVERT_A_12 (1<<12)
> +#define OAREPORTTRIG6_INVERT_A_13 (1<<13)
> +#define OAREPORTTRIG6_INVERT_A_14 (1<<14)
> +#define OAREPORTTRIG6_INVERT_A_15 (1<<15)
> +#define OAREPORTTRIG6_INVERT_B_0  (1<<16)
> +#define OAREPORTTRIG6_INVERT_B_1  (1<<17)
> +#define OAREPORTTRIG6_INVERT_B_2  (1<<18)
> +#define OAREPORTTRIG6_INVERT_B_3  (1<<19)
> +#define OAREPORTTRIG6_INVERT_C_0  (1<<20)
> +#define OAREPORTTRIG6_INVERT_C_1  (1<<21)
> +#define OAREPORTTRIG6_INVERT_D_0  (1<<22)
> +#define OAREPORTTRIG6_THRESHOLD_ENABLE     (1<<23)
> +#define OAREPORTTRIG6_REPORT_TRIGGER_ENABLE (1<<31)
> +
> +#define OAREPORTTRIG7 _MMIO(0x2758)
> +#define OAREPORTTRIG7_NOA_SELECT_MASK      0xf
> +#define OAREPORTTRIG7_NOA_SELECT_8_SHIFT    0
> +#define OAREPORTTRIG7_NOA_SELECT_9_SHIFT    4
> +#define OAREPORTTRIG7_NOA_SELECT_10_SHIFT   8
> +#define OAREPORTTRIG7_NOA_SELECT_11_SHIFT   12
> +#define OAREPORTTRIG7_NOA_SELECT_12_SHIFT   16
> +#define OAREPORTTRIG7_NOA_SELECT_13_SHIFT   20
> +#define OAREPORTTRIG7_NOA_SELECT_14_SHIFT   24
> +#define OAREPORTTRIG7_NOA_SELECT_15_SHIFT   28
> +
> +#define OAREPORTTRIG8 _MMIO(0x275c)
> +#define OAREPORTTRIG8_NOA_SELECT_MASK      0xf
> +#define OAREPORTTRIG8_NOA_SELECT_0_SHIFT    0
> +#define OAREPORTTRIG8_NOA_SELECT_1_SHIFT    4
> +#define OAREPORTTRIG8_NOA_SELECT_2_SHIFT    8
> +#define OAREPORTTRIG8_NOA_SELECT_3_SHIFT    12
> +#define OAREPORTTRIG8_NOA_SELECT_4_SHIFT    16
> +#define OAREPORTTRIG8_NOA_SELECT_5_SHIFT    20
> +#define OAREPORTTRIG8_NOA_SELECT_6_SHIFT    24
> +#define OAREPORTTRIG8_NOA_SELECT_7_SHIFT    28
> +
> +#define OASTARTTRIG1 _MMIO(0x2710)
> +#define OASTARTTRIG1_THRESHOLD_COUNT_MASK_MBZ 0xffff0000
> +#define OASTARTTRIG1_THRESHOLD_MASK          0xffff
> +
> +#define OASTARTTRIG2 _MMIO(0x2714)
> +#define OASTARTTRIG2_INVERT_A_0 (1<<0)
> +#define OASTARTTRIG2_INVERT_A_1 (1<<1)
> +#define OASTARTTRIG2_INVERT_A_2 (1<<2)
> +#define OASTARTTRIG2_INVERT_A_3 (1<<3)
> +#define OASTARTTRIG2_INVERT_A_4 (1<<4)
> +#define OASTARTTRIG2_INVERT_A_5 (1<<5)
> +#define OASTARTTRIG2_INVERT_A_6 (1<<6)
> +#define OASTARTTRIG2_INVERT_A_7 (1<<7)
> +#define OASTARTTRIG2_INVERT_A_8 (1<<8)
> +#define OASTARTTRIG2_INVERT_A_9 (1<<9)
> +#define OASTARTTRIG2_INVERT_A_10 (1<<10)
> +#define OASTARTTRIG2_INVERT_A_11 (1<<11)
> +#define OASTARTTRIG2_INVERT_A_12 (1<<12)
> +#define OASTARTTRIG2_INVERT_A_13 (1<<13)
> +#define OASTARTTRIG2_INVERT_A_14 (1<<14)
> +#define OASTARTTRIG2_INVERT_A_15 (1<<15)
> +#define OASTARTTRIG2_INVERT_B_0 (1<<16)
> +#define OASTARTTRIG2_INVERT_B_1 (1<<17)
> +#define OASTARTTRIG2_INVERT_B_2 (1<<18)
> +#define OASTARTTRIG2_INVERT_B_3 (1<<19)
> +#define OASTARTTRIG2_INVERT_C_0 (1<<20)
> +#define OASTARTTRIG2_INVERT_C_1 (1<<21)
> +#define OASTARTTRIG2_INVERT_D_0 (1<<22)
> +#define OASTARTTRIG2_THRESHOLD_ENABLE      (1<<23)
> +#define OASTARTTRIG2_START_TRIG_FLAG_MBZ    (1<<24)
> +#define OASTARTTRIG2_EVENT_SELECT_0  (1<<28)
> +#define OASTARTTRIG2_EVENT_SELECT_1  (1<<29)
> +#define OASTARTTRIG2_EVENT_SELECT_2  (1<<30)
> +#define OASTARTTRIG2_EVENT_SELECT_3  (1<<31)
> +
> +#define OASTARTTRIG3 _MMIO(0x2718)
> +#define OASTARTTRIG3_NOA_SELECT_MASK      0xf
> +#define OASTARTTRIG3_NOA_SELECT_8_SHIFT    0
> +#define OASTARTTRIG3_NOA_SELECT_9_SHIFT    4
> +#define OASTARTTRIG3_NOA_SELECT_10_SHIFT   8
> +#define OASTARTTRIG3_NOA_SELECT_11_SHIFT   12
> +#define OASTARTTRIG3_NOA_SELECT_12_SHIFT   16
> +#define OASTARTTRIG3_NOA_SELECT_13_SHIFT   20
> +#define OASTARTTRIG3_NOA_SELECT_14_SHIFT   24
> +#define OASTARTTRIG3_NOA_SELECT_15_SHIFT   28
> +
> +#define OASTARTTRIG4 _MMIO(0x271c)
> +#define OASTARTTRIG4_NOA_SELECT_MASK       0xf
> +#define OASTARTTRIG4_NOA_SELECT_0_SHIFT    0
> +#define OASTARTTRIG4_NOA_SELECT_1_SHIFT    4
> +#define OASTARTTRIG4_NOA_SELECT_2_SHIFT    8
> +#define OASTARTTRIG4_NOA_SELECT_3_SHIFT    12
> +#define OASTARTTRIG4_NOA_SELECT_4_SHIFT    16
> +#define OASTARTTRIG4_NOA_SELECT_5_SHIFT    20
> +#define OASTARTTRIG4_NOA_SELECT_6_SHIFT    24
> +#define OASTARTTRIG4_NOA_SELECT_7_SHIFT    28
> +
> +#define OASTARTTRIG5 _MMIO(0x2720)
> +#define OASTARTTRIG5_THRESHOLD_COUNT_MASK_MBZ 0xffff0000
> +#define OASTARTTRIG5_THRESHOLD_MASK          0xffff
> +
> +#define OASTARTTRIG6 _MMIO(0x2724)
> +#define OASTARTTRIG6_INVERT_A_0 (1<<0)
> +#define OASTARTTRIG6_INVERT_A_1 (1<<1)
> +#define OASTARTTRIG6_INVERT_A_2 (1<<2)
> +#define OASTARTTRIG6_INVERT_A_3 (1<<3)
> +#define OASTARTTRIG6_INVERT_A_4 (1<<4)
> +#define OASTARTTRIG6_INVERT_A_5 (1<<5)
> +#define OASTARTTRIG6_INVERT_A_6 (1<<6)
> +#define OASTARTTRIG6_INVERT_A_7 (1<<7)
> +#define OASTARTTRIG6_INVERT_A_8 (1<<8)
> +#define OASTARTTRIG6_INVERT_A_9 (1<<9)
> +#define OASTARTTRIG6_INVERT_A_10 (1<<10)
> +#define OASTARTTRIG6_INVERT_A_11 (1<<11)
> +#define OASTARTTRIG6_INVERT_A_12 (1<<12)
> +#define OASTARTTRIG6_INVERT_A_13 (1<<13)
> +#define OASTARTTRIG6_INVERT_A_14 (1<<14)
> +#define OASTARTTRIG6_INVERT_A_15 (1<<15)
> +#define OASTARTTRIG6_INVERT_B_0 (1<<16)
> +#define OASTARTTRIG6_INVERT_B_1 (1<<17)
> +#define OASTARTTRIG6_INVERT_B_2 (1<<18)
> +#define OASTARTTRIG6_INVERT_B_3 (1<<19)
> +#define OASTARTTRIG6_INVERT_C_0 (1<<20)
> +#define OASTARTTRIG6_INVERT_C_1 (1<<21)
> +#define OASTARTTRIG6_INVERT_D_0 (1<<22)
> +#define OASTARTTRIG6_THRESHOLD_ENABLE      (1<<23)
> +#define OASTARTTRIG6_START_TRIG_FLAG_MBZ    (1<<24)
> +#define OASTARTTRIG6_EVENT_SELECT_4  (1<<28)
> +#define OASTARTTRIG6_EVENT_SELECT_5  (1<<29)
> +#define OASTARTTRIG6_EVENT_SELECT_6  (1<<30)
> +#define OASTARTTRIG6_EVENT_SELECT_7  (1<<31)
> +
> +#define OASTARTTRIG7 _MMIO(0x2728)
> +#define OASTARTTRIG7_NOA_SELECT_MASK      0xf
> +#define OASTARTTRIG7_NOA_SELECT_8_SHIFT    0
> +#define OASTARTTRIG7_NOA_SELECT_9_SHIFT    4
> +#define OASTARTTRIG7_NOA_SELECT_10_SHIFT   8
> +#define OASTARTTRIG7_NOA_SELECT_11_SHIFT   12
> +#define OASTARTTRIG7_NOA_SELECT_12_SHIFT   16
> +#define OASTARTTRIG7_NOA_SELECT_13_SHIFT   20
> +#define OASTARTTRIG7_NOA_SELECT_14_SHIFT   24
> +#define OASTARTTRIG7_NOA_SELECT_15_SHIFT   28
> +
> +#define OASTARTTRIG8 _MMIO(0x272c)
> +#define OASTARTTRIG8_NOA_SELECT_MASK      0xf
> +#define OASTARTTRIG8_NOA_SELECT_0_SHIFT    0
> +#define OASTARTTRIG8_NOA_SELECT_1_SHIFT    4
> +#define OASTARTTRIG8_NOA_SELECT_2_SHIFT    8
> +#define OASTARTTRIG8_NOA_SELECT_3_SHIFT    12
> +#define OASTARTTRIG8_NOA_SELECT_4_SHIFT    16
> +#define OASTARTTRIG8_NOA_SELECT_5_SHIFT    20
> +#define OASTARTTRIG8_NOA_SELECT_6_SHIFT    24
> +#define OASTARTTRIG8_NOA_SELECT_7_SHIFT    28
> +
> +/* CECX_0 */
> +#define OACEC_COMPARE_LESS_OR_EQUAL    6
> +#define OACEC_COMPARE_NOT_EQUAL                5
> +#define OACEC_COMPARE_LESS_THAN                4
> +#define OACEC_COMPARE_GREATER_OR_EQUAL 3
> +#define OACEC_COMPARE_EQUAL            2
> +#define OACEC_COMPARE_GREATER_THAN     1
> +#define OACEC_COMPARE_ANY_EQUAL                0
> +
> +#define OACEC_COMPARE_VALUE_MASK    0xffff
> +#define OACEC_COMPARE_VALUE_SHIFT   3
> +
> +#define OACEC_SELECT_NOA       (0<<19)
> +#define OACEC_SELECT_PREV      (1<<19)
> +#define OACEC_SELECT_BOOLEAN   (2<<19)
> +
> +/* CECX_1 */
> +#define OACEC_MASK_MASK                    0xffff
> +#define OACEC_CONSIDERATIONS_MASK   0xffff
> +#define OACEC_CONSIDERATIONS_SHIFT  16
> +
> +#define OACEC0_0 _MMIO(0x2770)
> +#define OACEC0_1 _MMIO(0x2774)
> +#define OACEC1_0 _MMIO(0x2778)
> +#define OACEC1_1 _MMIO(0x277c)
> +#define OACEC2_0 _MMIO(0x2780)
> +#define OACEC2_1 _MMIO(0x2784)
> +#define OACEC3_0 _MMIO(0x2788)
> +#define OACEC3_1 _MMIO(0x278c)
> +#define OACEC4_0 _MMIO(0x2790)
> +#define OACEC4_1 _MMIO(0x2794)
> +#define OACEC5_0 _MMIO(0x2798)
> +#define OACEC5_1 _MMIO(0x279c)
> +#define OACEC6_0 _MMIO(0x27a0)
> +#define OACEC6_1 _MMIO(0x27a4)
> +#define OACEC7_0 _MMIO(0x27a8)
> +#define OACEC7_1 _MMIO(0x27ac)
> +
>
>  #define _GEN7_PIPEA_DE_LOAD_SL 0x70068
>  #define _GEN7_PIPEB_DE_LOAD_SL 0x71068
> @@ -6977,6 +7314,7 @@ enum {
>  # define GEN6_RCCUNIT_CLOCK_GATE_DISABLE               (1 << 11)
>
>  #define GEN6_UCGCTL3                           _MMIO(0x9408)
> +# define GEN6_OACSUNIT_CLOCK_GATE_DISABLE              (1 << 20)
>
>  #define GEN7_UCGCTL4                           _MMIO(0x940c)
>  #define  GEN7_L3BANK2X_CLOCK_GATE_DISABLE      (1<<25)
> diff --git a/drivers/gpu/drm/i915/intel_ringbuffer.c b/drivers/gpu/drm/i915/intel_ringbuffer.c
> index 7a74750..22d5ff1 100644
> --- a/drivers/gpu/drm/i915/intel_ringbuffer.c
> +++ b/drivers/gpu/drm/i915/intel_ringbuffer.c
> @@ -2043,8 +2043,14 @@ static int intel_ring_context_pin(struct i915_gem_context *ctx,
>                 if (ret)
>                         goto error;
>
> -               ret = i915_vma_pin(ce->state, 0, ctx->ggtt_alignment,
> -                                  PIN_GLOBAL | PIN_HIGH);
> +               if (engine->id == RCS) {
> +                       u64 vma_flags = PIN_GLOBAL | PIN_HIGH;
Maybe move this up a level and reuse for both paths.

> +                       ret = i915_gem_context_pin_legacy_rcs_state(engine->i915,
> +                                                                   ctx,
> +                                                                   vma_flags);
> +               } else
> +                       ret = i915_vma_pin(ce->state, 0, ctx->ggtt_alignment,
> +                                          PIN_GLOBAL | PIN_HIGH);
>                 if (ret)
>                         goto error;
>         }
> diff --git a/include/uapi/drm/i915_drm.h b/include/uapi/drm/i915_drm.h
> index 7777678..5ad3747 100644
> --- a/include/uapi/drm/i915_drm.h
> +++ b/include/uapi/drm/i915_drm.h
> @@ -1224,6 +1224,18 @@ struct drm_i915_gem_context_param {
>         __u64 value;
>  };
>
> +enum drm_i915_oa_format {
> +       I915_OA_FORMAT_A13 = 1,
> +       I915_OA_FORMAT_A29,
> +       I915_OA_FORMAT_A13_B8_C8,
> +       I915_OA_FORMAT_B4_C8,
> +       I915_OA_FORMAT_A45_B8_C8,
> +       I915_OA_FORMAT_B4_C8_A16,
> +       I915_OA_FORMAT_C4_B8,
> +
> +       I915_OA_FORMAT_MAX          /* non-ABI */
> +};
> +
>  enum drm_i915_perf_property_id {
>         /**
>          * Open the stream for a specific context handle (as used with
> @@ -1232,6 +1244,32 @@ enum drm_i915_perf_property_id {
>          */
>         DRM_I915_PERF_PROP_CTX_HANDLE = 1,
>
> +       /**
> +        * A value of 1 requests the inclusion of raw OA unit reports as
> +        * part of stream samples.
> +        */
> +       DRM_I915_PERF_PROP_SAMPLE_OA,
> +
> +       /**
> +        * The value specifies which set of OA unit metrics should be
> +        * be configured, defining the contents of any OA unit reports.
> +        */
> +       DRM_I915_PERF_PROP_OA_METRICS_SET,
> +
> +       /**
> +        * The value specifies the size and layout of OA unit reports.
> +        */
> +       DRM_I915_PERF_PROP_OA_FORMAT,
> +
> +       /**
> +        * Specifying this property implicitly requests periodic OA unit
> +        * sampling and (at least on Haswell) the sampling frequency is derived
> +        * from this exponent as follows:
> +        *
> +        *   80ns * 2^(period_exponent + 1)
> +        */
> +       DRM_I915_PERF_PROP_OA_EXPONENT,
> +
>         DRM_I915_PERF_PROP_MAX /* non-ABI */
>  };
>
> @@ -1251,7 +1289,22 @@ struct drm_i915_perf_open_param {
>         __u64 __user properties_ptr;
>  };
>
> +/**
> + * Enable data capture for a stream that was either opened in a disabled state
> + * via I915_PERF_FLAG_DIABLED or was later disabled via I915_PERF_IOCTL_DISABLE.
> + *
> + * It is intended to be cheaper to disable and enable a stream than it may be
> + * to close and re-open a stream with the same configuration.
> + *
> + * It's undefined whether any pending data for the stream will be lost.
> + */
>  #define I915_PERF_IOCTL_ENABLE _IO('i', 0x0)
> +
> +/**
> + * Disable data capture for a stream.
> + *
> + * It is an error to try and read a stream that is disabled.
> + */
>  #define I915_PERF_IOCTL_DISABLE        _IO('i', 0x1)
>
>  /**
> @@ -1275,17 +1328,30 @@ enum drm_i915_perf_record_type {
>          * every sample.
>          *
>          * The order of these sample properties given by userspace has no
> -        * affect on the ordering of data within a sample. The order will be
> +        * affect on the ordering of data within a sample. The order is
>          * documented here.
>          *
>          * struct {
>          *     struct drm_i915_perf_record_header header;
>          *
> -        *     TODO: itemize extensible sample data here
> +        *     { u32 oa_report[]; } && DRM_I915_PERF_PROP_SAMPLE_OA
>          * };
>          */
>         DRM_I915_PERF_RECORD_SAMPLE = 1,
>
> +       /*
> +        * Indicates that one or more OA reports were not written by the
> +        * hardware. This can happen for example if an MI_REPORT_PERF_COUNT
> +        * command collides with periodic sampling - which would be more likely
> +        * at higher sampling frequencies.
> +        */
> +       DRM_I915_PERF_RECORD_OA_REPORT_LOST = 2,
> +
> +       /**
> +        * An error occurred that resulted in all pending OA reports being lost.
> +        */
> +       DRM_I915_PERF_RECORD_OA_BUFFER_LOST = 3,
> +
>         DRM_I915_PERF_RECORD_MAX /* non-ABI */
>  };
>
> --
> 2.9.2
>
> _______________________________________________
> Intel-gfx mailing list
> Intel-gfx@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/intel-gfx
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 07/11] drm/i915: advertise available metrics via sysfs
  2016-09-14 14:19 ` [PATCH v5 07/11] drm/i915: advertise available metrics via sysfs Robert Bragg
@ 2016-10-07 17:20   ` Matthew Auld
  0 siblings, 0 replies; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:20 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> Each metric set is given a sysfs entry like:
>
> /sys/class/drm/card0/metrics/<guid>/id
>
> This allows userspace to enumerate the specific sets that are available
> for the current system. The 'id' file contains an unsigned integer that
> can be used to open the associated metric set via
> DRM_IOCTL_I915_PERF_OPEN. The <guid> is a globally unique ID for a
> specific OA unit register configuration that can be reliably used by
> userspace as a key to lookup corresponding counter meta data and
> normalization equations.
>
> The guid registry is currently maintained as part of gputop along with
> the XML metric set descriptions and code generation scripts, ref:
>
>  https://github.com/rib/gputop
>  > gputop-data/guids.xml
>  > scripts/update-guids.py
>  > gputop-data/oa-*.xml
>  > scripts/i915-perf-kernelgen.py
>
>  $ make -C gputop-data -f Makefile.xml SYSFS=1 WHITELIST=RenderBasic
>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
> ---
>  drivers/gpu/drm/i915/i915_drv.c    |  5 ++++
>  drivers/gpu/drm/i915/i915_drv.h    |  4 +++
>  drivers/gpu/drm/i915/i915_oa_hsw.c | 51 +++++++++++++++++++++++++++++++++++++
>  drivers/gpu/drm/i915/i915_oa_hsw.h |  4 +++
>  drivers/gpu/drm/i915/i915_perf.c   | 52 ++++++++++++++++++++++++++++++++++++++
>  5 files changed, 116 insertions(+)
>
> diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
> index 14f22fc..8965df2 100644
> --- a/drivers/gpu/drm/i915/i915_drv.c
> +++ b/drivers/gpu/drm/i915/i915_drv.c
> @@ -1125,6 +1125,9 @@ static void i915_driver_register(struct drm_i915_private *dev_priv)
>         if (drm_dev_register(dev, 0) == 0) {
>                 i915_debugfs_register(dev_priv);
>                 i915_setup_sysfs(dev_priv);
> +
> +               /* Depends on sysfs having been initialized */
> +               i915_perf_register(dev_priv);
>         } else
>                 DRM_ERROR("Failed to register driver for userspace access!\n");
>
> @@ -1161,6 +1164,8 @@ static void i915_driver_unregister(struct drm_i915_private *dev_priv)
>         acpi_video_unregister();
>         intel_opregion_unregister(dev_priv);
>
> +       i915_perf_unregister(dev_priv);
> +
>         i915_teardown_sysfs(dev_priv);
>         i915_debugfs_unregister(dev_priv);
>         drm_dev_unregister(&dev_priv->drm);
> diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> index 551f078..f5ddf70 100644
> --- a/drivers/gpu/drm/i915/i915_drv.h
> +++ b/drivers/gpu/drm/i915/i915_drv.h
> @@ -2141,6 +2141,8 @@ struct drm_i915_private {
>         struct {
>                 bool initialized;
>
> +               struct kobject *metrics_kobj;
> +
>                 struct mutex lock;
>                 struct list_head streams;
>
> @@ -3711,6 +3713,8 @@ int intel_engine_cmd_parser(struct intel_engine_cs *engine,
>  /* i915_perf.c */
>  extern void i915_perf_init(struct drm_i915_private *dev_priv);
>  extern void i915_perf_fini(struct drm_i915_private *dev_priv);
> +extern void i915_perf_register(struct drm_i915_private *dev_priv);
> +extern void i915_perf_unregister(struct drm_i915_private *dev_priv);
>
>  /* i915_suspend.c */
>  extern int i915_save_state(struct drm_device *dev);
> diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.c b/drivers/gpu/drm/i915/i915_oa_hsw.c
> index eb5ceca..656334d 100644
> --- a/drivers/gpu/drm/i915/i915_oa_hsw.c
> +++ b/drivers/gpu/drm/i915/i915_oa_hsw.c
> @@ -24,6 +24,8 @@
>   *
>   */
>
> +#include <linux/sysfs.h>
> +
>  #include "i915_drv.h"
>
>  enum metric_set_id {
> @@ -141,3 +143,52 @@ int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv)
>                 return -ENODEV;
>         }
>  }
> +
> +static ssize_t
> +show_render_basic_id(struct device *kdev, struct device_attribute *attr, char *buf)
> +{
> +       return sprintf(buf, "%d\n", METRIC_SET_ID_RENDER_BASIC);
> +}
> +
> +static struct device_attribute dev_attr_render_basic_id = {
> +       .attr = { .name = "id", .mode = S_IRUGO },
> +       .show = show_render_basic_id,
> +       .store = NULL,
> +};
> +
> +static struct attribute *attrs_render_basic[] = {
> +       &dev_attr_render_basic_id.attr,
> +       NULL,
> +};
> +
> +static struct attribute_group group_render_basic = {
> +       .name = "403d8832-1a27-4aa6-a64e-f5389ce7b212",
> +       .attrs =  attrs_render_basic,
> +};
> +
> +int
> +i915_perf_register_sysfs_hsw(struct drm_i915_private *dev_priv)
> +{
> +       int mux_len;
> +       int ret = 0;
> +
> +       if (get_render_basic_mux_config(dev_priv, &mux_len)) {
> +               ret = sysfs_create_group(dev_priv->perf.metrics_kobj, &group_render_basic);
> +               if (ret)
> +                       goto error_render_basic;
> +       }
> +
> +       return 0;
> +
> +error_render_basic:
> +       return ret;
> +}
> +
> +void
> +i915_perf_unregister_sysfs_hsw(struct drm_i915_private *dev_priv)
> +{
> +       int mux_len;
> +
> +       if (get_render_basic_mux_config(dev_priv, &mux_len))
> +               sysfs_remove_group(dev_priv->perf.metrics_kobj, &group_render_basic);
> +}
> diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.h b/drivers/gpu/drm/i915/i915_oa_hsw.h
> index b618a1f..429a229 100644
> --- a/drivers/gpu/drm/i915/i915_oa_hsw.h
> +++ b/drivers/gpu/drm/i915/i915_oa_hsw.h
> @@ -31,4 +31,8 @@ extern int i915_oa_n_builtin_metric_sets_hsw;
>
>  extern int i915_oa_select_metric_set_hsw(struct drm_i915_private *dev_priv);
>
> +extern int i915_perf_register_sysfs_hsw(struct drm_i915_private *dev_priv);
> +
> +extern void i915_perf_unregister_sysfs_hsw(struct drm_i915_private *dev_priv);
> +
>  #endif
> diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
> index 5305982..e890c38 100644
> --- a/drivers/gpu/drm/i915/i915_perf.c
> +++ b/drivers/gpu/drm/i915/i915_perf.c
> @@ -743,6 +743,15 @@ static int i915_oa_stream_init(struct i915_perf_stream *stream,
>         int format_size;
>         int ret;
>
> +       /* If the sysfs metrics/ directory wasn't registered for some
> +        * reason then don't let userspace try their luck with config
> +        * IDs
> +        */
> +       if (!dev_priv->perf.metrics_kobj) {
> +               DRM_ERROR("OA metrics weren't advertised via sysfs\n");
> +               return -EINVAL;
> +       }
> +
>         if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
>                 DRM_ERROR("Only OA report sampling supported\n");
>                 return -EINVAL;
> @@ -1365,6 +1374,49 @@ int i915_perf_open_ioctl(struct drm_device *dev, void *data,
>         return ret;
>  }
>
> +void i915_perf_register(struct drm_i915_private *dev_priv)
> +{
> +       if (!IS_HASWELL(dev_priv))
> +               return;
> +
> +       if (!dev_priv->perf.initialized)
> +               return;
> +
> +       /* To be sure we're synchronized with an attempted
> +        * i915_perf_open_ioctl(); considering that we register after
> +        * being exposed to userspace.
> +        */
> +       mutex_lock(&dev_priv->perf.lock);
> +
> +       dev_priv->perf.metrics_kobj =
> +               kobject_create_and_add("metrics",
> +                                      &dev_priv->drm.primary->kdev->kobj);
> +       if (!dev_priv->perf.metrics_kobj)
> +               goto exit;
> +
> +       if (i915_perf_register_sysfs_hsw(dev_priv)) {
> +               kobject_put(dev_priv->perf.metrics_kobj);
> +               dev_priv->perf.metrics_kobj = NULL;
> +       }
> +
> +exit:
> +       mutex_unlock(&dev_priv->perf.lock);
> +}
> +
> +void i915_perf_unregister(struct drm_i915_private *dev_priv)
> +{
> +       if (!IS_HASWELL(dev_priv))
> +               return;
Probably no need for this check.

Reviewed-by: Matthew Auld <matthew.auld@intel.com>
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 08/11] drm/i915: Add dev.i915.perf_event_paranoid sysctl option
  2016-09-14 14:19 ` [PATCH v5 08/11] drm/i915: Add dev.i915.perf_event_paranoid sysctl option Robert Bragg
@ 2016-10-07 17:21   ` Matthew Auld
  0 siblings, 0 replies; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:21 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> Consistent with the kernel.perf_event_paranoid sysctl option that can
> allow non-root users to access system wide cpu metrics, this can
> optionally allow non-root users to access system wide OA counter metrics
> from Gen graphics hardware.
>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
> ---
>  drivers/gpu/drm/i915/i915_drv.h  |  1 +
>  drivers/gpu/drm/i915/i915_perf.c | 45 +++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 45 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> index f5ddf70..eaba7a9 100644
> --- a/drivers/gpu/drm/i915/i915_drv.h
> +++ b/drivers/gpu/drm/i915/i915_drv.h
> @@ -2142,6 +2142,7 @@ struct drm_i915_private {
>                 bool initialized;
>
>                 struct kobject *metrics_kobj;
> +               struct ctl_table_header *sysctl_header;
>
>                 struct mutex lock;
>                 struct list_head streams;
> diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
> index e890c38..38b13fa 100644
> --- a/drivers/gpu/drm/i915/i915_perf.c
> +++ b/drivers/gpu/drm/i915/i915_perf.c
> @@ -62,6 +62,8 @@
>  #define POLL_FREQUENCY 200
>  #define POLL_PERIOD (NSEC_PER_SEC / POLL_FREQUENCY)
>
> +static u32 i915_perf_stream_paranoid = true;
> +
>  /* The maximum exponent the hardware accepts is 63 (essentially it selects one
>   * of the 64bit timestamp bits to trigger reports from) but there's currently
>   * no known use case for sampling as infrequently as once per 47 thousand years.
> @@ -1170,7 +1172,13 @@ int i915_perf_open_ioctl_locked(struct drm_device *dev,
>                 }
>         }
>
> -       if (!specific_ctx && !capable(CAP_SYS_ADMIN)) {
> +       /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
> +        * we check a dev.i915.perf_stream_paranoid sysctl option
> +        * to determine if it's ok to access system wide OA counters
> +        * without CAP_SYS_ADMIN privileges.
> +        */
> +       if (!specific_ctx &&
> +           i915_perf_stream_paranoid && !capable(CAP_SYS_ADMIN)) {
>                 DRM_ERROR("Insufficient privileges to open system-wide i915 perf stream\n");
>                 ret = -EACCES;
>                 goto err_ctx;
> @@ -1417,6 +1425,37 @@ void i915_perf_unregister(struct drm_i915_private *dev_priv)
>         dev_priv->perf.metrics_kobj = NULL;
>  }
>
> +static struct ctl_table oa_table[] = {
> +       {
> +        .procname = "perf_stream_paranoid",
> +        .data = &i915_perf_stream_paranoid,
> +        .maxlen = sizeof(i915_perf_stream_paranoid),
> +        .mode = 0644,
> +        .proc_handler = proc_dointvec,
If this really is just a bool, why not:

  .proc_handler = proc_dointvec_minmax,
  .extra1 = &zero,
  .extra2 = &one

Reviewed-by: Matthew Auld <matthew.auld@intel.com>
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 09/11] drm/i915: add oa_event_min_timer_exponent sysctl
  2016-09-14 14:19 ` [PATCH v5 09/11] drm/i915: add oa_event_min_timer_exponent sysctl Robert Bragg
@ 2016-10-07 17:21   ` Matthew Auld
  0 siblings, 0 replies; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:21 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> The minimal sampling period is now configurable via a
> dev.i915.oa_min_timer_exponent sysctl parameter.
>
> Following the precedent set by perf, the default is the minimum that
> won't (on its own) exceed the default kernel.perf_event_max_sample_rate
> default of 100000 samples/s.
>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 10/11] drm/i915: Add more Haswell OA metric sets
  2016-09-14 14:19 ` [PATCH v5 10/11] drm/i915: Add more Haswell OA metric sets Robert Bragg
@ 2016-10-07 17:22   ` Matthew Auld
  0 siblings, 0 replies; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:22 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> This adds 'compute', 'compute extended', 'memory reads', 'memory writes'
> and 'sampler balance' metric sets for Haswell.
>
> The code is auto generated from an XML description of metric sets,
> currently maintained in gputop, ref:
>
>  https://github.com/rib/gputop
>  > gputop-data/oa-*.xml
>  > scripts/i915-perf-kernelgen.py
>
>  $ make -C gputop-data -f Makefile.xml
>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
> ---
>  drivers/gpu/drm/i915/i915_oa_hsw.c | 559 ++++++++++++++++++++++++++++++++++++-
>  1 file changed, 558 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/i915/i915_oa_hsw.c b/drivers/gpu/drm/i915/i915_oa_hsw.c
> index 656334d..7906a26 100644
> --- a/drivers/gpu/drm/i915/i915_oa_hsw.c
> +++ b/drivers/gpu/drm/i915/i915_oa_hsw.c
> @@ -30,9 +30,14 @@
>
>  enum metric_set_id {
>         METRIC_SET_ID_RENDER_BASIC = 1,
> +       METRIC_SET_ID_COMPUTE_BASIC,
> +       METRIC_SET_ID_COMPUTE_EXTENDED,
> +       METRIC_SET_ID_MEMORY_READS,
> +       METRIC_SET_ID_MEMORY_WRITES,
> +       METRIC_SET_ID_SAMPLER_BALANCE,
>  };
>
> -int i915_oa_n_builtin_metric_sets_hsw = 1;
> +int i915_oa_n_builtin_metric_sets_hsw = 6;
You didn't fancy having a METRIC_SET_ID_MAX?

Reviewed-by: Matthew Auld <matthew.auld@intel.com>
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 11/11] drm/i915: Add a kerneldoc summary for i915_perf.c
  2016-09-14 14:19 ` [PATCH v5 11/11] drm/i915: Add a kerneldoc summary for i915_perf.c Robert Bragg
@ 2016-10-07 17:24   ` Matthew Auld
  0 siblings, 0 replies; 33+ messages in thread
From: Matthew Auld @ 2016-10-07 17:24 UTC (permalink / raw)
  To: Robert Bragg
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, dri-devel

On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
> In particular this tries to capture for posterity some of the early
> challenges we had with using the core perf infrastructure in case we
> ever want to revisit adapting perf for device metrics.
>
> Cc: Chris Wilson <chris@chris-wilson.co.uk>
> Signed-off-by: Robert Bragg <robert@sixbynine.org>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [Intel-gfx] [PATCH] drm/i915: Add i915 perf infrastructure
  2016-10-07 17:10       ` Matthew Auld
@ 2016-10-11 19:03         ` Robert Bragg
  2016-10-12 11:41           ` Joonas Lahtinen
  0 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-10-11 19:03 UTC (permalink / raw)
  To: Matthew Auld
  Cc: Daniel Vetter, Intel Graphics Development, Sourab Gupta, ML dri-devel


[-- Attachment #1.1: Type: text/plain, Size: 2387 bytes --]

On Fri, Oct 7, 2016 at 10:10 AM, Matthew Auld <
matthew.william.auld@gmail.com> wrote:

> On 14 September 2016 at 16:32, Robert Bragg <robert@sixbynine.org> wrote:
>
> > +
> > +int i915_perf_open_ioctl_locked(struct drm_device *dev,
> > +                               struct drm_i915_perf_open_param *param,
> > +                               struct perf_open_properties *props,
> > +                               struct drm_file *file)
> > +{
> This should be static and also let's just make it take dev_priv directly.
>

Ah, yep, done.


> > +               case DRM_I915_PERF_PROP_MAX:
> > +                       BUG();
> We already handle this case above, but I guess we still need this in
> order to silence gcc...
>

right, and preferable to having a default: case, for the future compiler
warning to handle any new properties here.



> > diff --git a/include/uapi/drm/i915_drm.h b/include/uapi/drm/i915_drm.h
> > index 03725fe..77fe79b 100644
> > --- a/include/uapi/drm/i915_drm.h
> > +++ b/include/uapi/drm/i915_drm.h
> > @@ -258,6 +258,7 @@ typedef struct _drm_i915_sarea {
> >  #define DRM_I915_GEM_USERPTR           0x33
> >  #define DRM_I915_GEM_CONTEXT_GETPARAM  0x34
> >  #define DRM_I915_GEM_CONTEXT_SETPARAM  0x35
> > +#define DRM_I915_PERF_OPEN             0x36
> >
> >  #define DRM_IOCTL_I915_INIT            DRM_IOW( DRM_COMMAND_BASE +
> DRM_I915_INIT, drm_i915_init_t)
> >  #define DRM_IOCTL_I915_FLUSH           DRM_IO ( DRM_COMMAND_BASE +
> DRM_I915_FLUSH)
> > @@ -311,6 +312,7 @@ typedef struct _drm_i915_sarea {
> >  #define DRM_IOCTL_I915_GEM_USERPTR                     DRM_IOWR
> (DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR, struct drm_i915_gem_userptr)
> >  #define DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM    DRM_IOWR
> (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_GETPARAM, struct
> drm_i915_gem_context_param)
> >  #define DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM    DRM_IOWR
> (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_SETPARAM, struct
> drm_i915_gem_context_param)
> > +#define DRM_IOCTL_I915_PERF_OPEN       DRM_IOR(DRM_COMMAND_BASE +
> DRM_I915_PERF_OPEN, struct drm_i915_perf_open_param)
> As you already pointed out this will need to be IOW.
>

Yeah, changed locally after I realised the mistake here, just didn't get
around to posting the patch.


Also applied the notes to not redundantly init some vars, spurious new
line, redundant include.

Thanks,
- Robert

[-- Attachment #1.2: Type: text/html, Size: 3575 bytes --]

[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH v5 06/11] drm/i915: Enable i915 perf stream for Haswell OA unit
  2016-10-07 17:19   ` Matthew Auld
@ 2016-10-12  0:55     ` Robert Bragg
  0 siblings, 0 replies; 33+ messages in thread
From: Robert Bragg @ 2016-10-12  0:55 UTC (permalink / raw)
  To: Matthew Auld
  Cc: David Airlie, Daniel Vetter, Intel Graphics Development,
	Sourab Gupta, ML dri-devel


[-- Attachment #1.1: Type: text/plain, Size: 21008 bytes --]

On Fri, Oct 7, 2016 at 10:19 AM, Matthew Auld <
matthew.william.auld@gmail.com> wrote:

> On 14 September 2016 at 15:19, Robert Bragg <robert@sixbynine.org> wrote:
>
> > diff --git a/drivers/gpu/drm/i915/i915_perf.c
> b/drivers/gpu/drm/i915/i915_perf.c
> > index 87530f5..5305982 100644
> > --- a/drivers/gpu/drm/i915/i915_perf.c
> > +++ b/drivers/gpu/drm/i915/i915_perf.c
> > @@ -28,14 +28,860 @@
> >  #include <linux/sizes.h>
> >
> >  #include "i915_drv.h"
> > +#include "intel_ringbuffer.h"
> > +#include "intel_lrc.h"
> Superfluous includes.
>

ah, yup, removed.


> > +#include "i915_oa_hsw.h"
> > +
> > +/* Must be a power of two */
> > +#define OA_BUFFER_SIZE         SZ_16M
> It's a power of two between 128K and 16M, maybe add a build_bug_on and
> build_bug_on_not_power_of_2 to check this?
>

okey, added assertions to init_oa_buffer()



>
> > +#define OA_TAKEN(tail, head)   ((tail - head) & (OA_BUFFER_SIZE - 1))
> > +
> > +/* There's a HW race condition between OA unit tail pointer register
> updates and
> > + * writes to memory whereby the tail pointer can sometimes get ahead of
> what's
> > + * been written out to the OA buffer so far.
> > + *
> > + * Although this can be observed explicitly by checking for a zeroed
> report-id
> > + * field in tail reports, it seems preferable to account for this
> earlier e.g.
> > + * as part of the _oa_buffer_is_empty checks to minimize -EAGAIN
> polling cycles
> > + * in this situation.
> > + *
> > + * To give time for the most recent reports to land before they may be
> copied to
> > + * userspace, the driver operates as if the tail pointer effectively
> lags behind
> > + * the HW tail pointer by 'tail_margin' bytes. The margin in bytes is
> calculated
> > + * based on this constant in nanoseconds, the current OA sampling
> exponent
> > + * and current report size.
> > + *
> > + * There is also a fallback check while reading to simply skip over
> reports with
> > + * a zeroed report-id.
> > + */
> > +#define OA_TAIL_MARGIN_NSEC    100000ULL
> Yikes!
>

Yeah :-)

Although I've had some feedback from the hw side that there probably is a
race as described here; it's currently an assumption that a 100
microseconds is always enough time for any internally buffered OA reports
to get as far as being coherent with the cpu's view. If a more detailed
analysis is ever done to quantify the maximum latency (maybe not best to
measure as a unit of time) maybe we can update this, but for now I've found
this to work. I'm not really pushing for, or expecting this to be
investigated in detail for Haswell.


>
>
> > +
> > +static void gen7_init_oa_buffer(struct drm_i915_private *dev_priv)
> > +{
> > +       /* Pre-DevBDW: OABUFFER must be set with counters off,
> > +        * before OASTATUS1, but after OASTATUS2
> > +        */
> > +       I915_WRITE(GEN7_OASTATUS2, dev_priv->perf.oa.oa_buffer.gtt_offset
> |
> > +                  OA_MEM_SELECT_GGTT); /* head */
> > +       I915_WRITE(GEN7_OABUFFER, dev_priv->perf.oa.oa_buffer.
> gtt_offset);
> > +       I915_WRITE(GEN7_OASTATUS1, dev_priv->perf.oa.oa_buffer.gtt_offset
> |
> > +                  OABUFFER_SIZE_16M); /* tail */
> > +
> > +       /* On Haswell we have to track which OASTATUS1 flags we've
> > +        * already seen since they can't be cleared while periodic
> > +        * sampling is enabled.
> > +        */
> > +       dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
> > +
> > +       /* NB: although the OA buffer will initially be allocated
> > +        * zeroed via shmfs (and so this memset is redundant when
> > +        * first allocating), we may re-init the OA buffer, either
> > +        * when re-enabling a stream or in error/reset paths.
> > +        *
> > +        * The reason we clear the buffer for each re-init is for the
> > +        * sanity check in gen7_append_oa_reports() that looks at the
> > +        * report-id field to make sure it's non-zero which relies on
> > +        * the assumption that new reports are being written to zeroed
> > +        * memory...
> > +        */
> > +       memset(dev_priv->perf.oa.oa_buffer.addr, 0, SZ_16M);
> OA_BUFFER_SIZE
>

ah, yep.


>
> > +
> > +       /* Maybe make ->pollin per-stream state if we support multiple
> > +        * concurrent streams in the future. */
> > +       atomic_set(&dev_priv->perf.oa.pollin, false);
> > +}
> > +
> > +static int alloc_oa_buffer(struct drm_i915_private *dev_priv)
> > +{
> > +       struct drm_i915_gem_object *bo;
> > +       enum i915_map_type map;
> > +       struct i915_vma *vma;
> > +       int ret;
> > +
> > +       BUG_ON(dev_priv->perf.oa.oa_buffer.obj);
> > +
> > +       ret = i915_mutex_lock_interruptible(&dev_priv->drm);
> > +       if (ret)
> > +               return ret;
> > +
> > +       bo = i915_gem_object_create(&dev_priv->drm, OA_BUFFER_SIZE);
> > +       if (IS_ERR(bo)) {
> > +               DRM_ERROR("Failed to allocate OA buffer\n");
> > +               ret = PTR_ERR(bo);
> > +               goto unlock;
> > +       }
> > +       dev_priv->perf.oa.oa_buffer.obj = bo;
> > +
> > +       ret = i915_gem_object_set_cache_level(bo, I915_CACHE_LLC);
> > +       if (ret)
> > +               goto err_unref;
> > +
> > +       /* PreHSW required 512K alignment, HSW requires 16M */
> > +       vma = i915_gem_object_ggtt_pin(bo, NULL, 0, SZ_16M,
> PIN_MAPPABLE);
> > +       if (IS_ERR(vma)) {
> > +               ret = PTR_ERR(vma);
> > +               goto err_unref;
> > +       }
> > +       dev_priv->perf.oa.oa_buffer.vma = vma;
> > +
> > +       map = HAS_LLC(dev_priv) ? I915_MAP_WB : I915_MAP_WC;
> > +
> > +       dev_priv->perf.oa.oa_buffer.gtt_offset = i915_ggtt_offset(vma);
> > +       dev_priv->perf.oa.oa_buffer.addr = i915_gem_object_pin_map(bo,
> map);
> > +       if (dev_priv->perf.oa.oa_buffer.addr == NULL)
> i915_gem_object_pin_map can't return NULL.
>
>
ah, and this error path would have also returned an uninitialized ret error
code.


> > +               goto err_unpin;
> > +
> > +       dev_priv->perf.oa.ops.init_oa_buffer(dev_priv);
> > +
> > +       DRM_DEBUG_DRIVER("OA Buffer initialized, gtt offset = 0x%x,
> vaddr = %p",
> > +                        dev_priv->perf.oa.oa_buffer.gtt_offset,
> > +                        dev_priv->perf.oa.oa_buffer.addr);
> > +
> > +       goto unlock;
> > +
> > +err_unpin:
> > +       __i915_vma_unpin(vma);
> > +
> > +err_unref:
> > +       i915_gem_object_put(bo);
> > +
> > +       dev_priv->perf.oa.oa_buffer.gtt_offset = 0;
> > +       dev_priv->perf.oa.oa_buffer.addr = NULL;
> > +       dev_priv->perf.oa.oa_buffer.vma = NULL;
> > +       dev_priv->perf.oa.oa_buffer.obj = NULL;
> > +
> > +unlock:
> > +       mutex_unlock(&dev_priv->drm.struct_mutex);
> > +       return ret;
> > +}
> > +
> > +static void config_oa_regs(struct drm_i915_private *dev_priv,
> > +                          const struct i915_oa_reg *regs,
> > +                          int n_regs)
> > +{
> > +       int i;
> > +
> > +       for (i = 0; i < n_regs; i++) {
> > +               const struct i915_oa_reg *reg = regs + i;
> > +
> > +               I915_WRITE(reg->addr, reg->value);
> > +       }
> > +}
> > +
> > +static int hsw_enable_metric_set(struct drm_i915_private *dev_priv)
> > +{
> > +       int ret = i915_oa_select_metric_set_hsw(dev_priv);
> > +
> > +       if (ret)
> > +               return ret;
> > +
> > +       I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) |
> > +                                     GT_NOA_ENABLE));
> > +
> > +       /* PRM:
> > +        *
> > +        * OA unit is using “crclk” for its functionality. When trunk
> > +        * level clock gating takes place, OA clock would be gated,
> > +        * unable to count the events from non-render clock domain.
> > +        * Render clock gating must be disabled when OA is enabled to
> > +        * count the events from non-render domain. Unit level clock
> > +        * gating for RCS should also be disabled.
> > +        */
> > +       I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
> > +                                   ~GEN7_DOP_CLOCK_GATE_ENABLE));
> > +       I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) |
> > +                                 GEN6_CSUNIT_CLOCK_GATE_DISABLE));
> > +
> > +       config_oa_regs(dev_priv, dev_priv->perf.oa.mux_regs,
> > +                      dev_priv->perf.oa.mux_regs_len);
> > +
> > +       /* It apparently takes a fairly long time for a new MUX
> > +        * configuration to be be applied after these register writes.
> > +        * This delay duration was derived empirically based on the
> > +        * render_basic config but hopefully it covers the maximum
> > +        * configuration latency.
> > +        *
> > +        * As a fallback, the checks in _append_oa_reports() to skip
> > +        * invalid OA reports do also seem to work to discard reports
> > +        * generated before this config has completed - albeit not
> > +        * silently.
> > +        *
> > +        * Unfortunately this is essentially a magic number, since we
> > +        * don't currently know of a reliable mechanism for predicting
> > +        * how long the MUX config will take to apply and besides
> > +        * seeing invalid reports we don't know of a reliable way to
> > +        * explicitly check that the MUX config has landed.
> > +        *
> > +        * It's even possible we've miss characterized the underlying
> > +        * problem - it just seems like the simplest explanation why
> > +        * a delay at this location would mitigate any invalid reports.
> > +        */
> > +       usleep_range(15000, 20000);
> > +
> > +       config_oa_regs(dev_priv, dev_priv->perf.oa.b_counter_regs,
> > +                      dev_priv->perf.oa.b_counter_regs_len);
> > +
> > +       return 0;
> > +}
> > +
> > +static void hsw_disable_metric_set(struct drm_i915_private *dev_priv)
> > +{
> > +       I915_WRITE(GEN6_UCGCTL1, (I915_READ(GEN6_UCGCTL1) &
> > +                                 ~GEN6_CSUNIT_CLOCK_GATE_DISABLE));
> > +       I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) |
> > +                                   GEN7_DOP_CLOCK_GATE_ENABLE));
> > +
> > +       I915_WRITE(GDT_CHICKEN_BITS, (I915_READ(GDT_CHICKEN_BITS) &
> > +                                     ~GT_NOA_ENABLE));
> > +}
> > +
> > +static void gen7_update_oacontrol_locked(struct drm_i915_private
> *dev_priv)
> > +{
> > +       assert_spin_locked(&dev_priv->perf.hook_lock);
> > +
> > +       if (dev_priv->perf.oa.exclusive_stream->enabled) {
> > +               unsigned long ctx_id = 0;
> > +
> > +               if (dev_priv->perf.oa.exclusive_stream->ctx)
> > +                       ctx_id = dev_priv->perf.oa.specific_ctx_id;
> > +
> > +               if (dev_priv->perf.oa.exclusive_stream->ctx == NULL ||
> ctx_id) {
> gem context state pinned at ggtt offset zero is unlikely but still
> possible, as pointed
> out by Chris.
>

I've updated i915-perf to use an INVALID_CTX_ID define of 0xffffffff where
appropriate.

When looking at this I realised that specific_ctx_id isn't explicltly
initialized on Haswell when the stream is first opened (only for gen8+ in
later patches) so I've also updated i915_oa_stream_init() to try
initializing specific_ctx_id if the ctx is already pinned. I'll also update
the specific ctx igt test which probably didn't catch this because the
single context being tested is unused when opening the stream and so it
works to rely on the pinning hook in that case.


>
>
> > +
> > +static int i915_oa_stream_init(struct i915_perf_stream *stream,
> > +                              struct drm_i915_perf_open_param *param,
> > +                              struct perf_open_properties *props)
> > +{
> > +       struct drm_i915_private *dev_priv = stream->dev_priv;
> > +       int format_size;
> > +       int ret;
> > +
> > +       if (!(props->sample_flags & SAMPLE_OA_REPORT)) {
> > +               DRM_ERROR("Only OA report sampling supported\n");
> > +               return -EINVAL;
> > +       }
> > +
> > +       if (!dev_priv->perf.oa.ops.init_oa_buffer) {
> > +               DRM_ERROR("OA unit not supported\n");
> > +               return -ENODEV;
> > +       }
> > +
> > +       /* To avoid the complexity of having to accurately filter
> > +        * counter reports and marshal to the appropriate client
> > +        * we currently only allow exclusive access
> > +        */
> > +       if (dev_priv->perf.oa.exclusive_stream) {
> > +               DRM_ERROR("OA unit already in use\n");
> > +               return -EBUSY;
> > +       }
> > +
> > +       if (!props->metrics_set) {
> > +               DRM_ERROR("OA metric set not specified\n");
> > +               return -EINVAL;
> > +       }
> > +
> > +       if (!props->oa_format) {
> > +               DRM_ERROR("OA report format not specified\n");
> > +               return -EINVAL;
> > +       }
> > +
> > +       stream->sample_size = sizeof(struct drm_i915_perf_record_header);
> > +
> > +       format_size = dev_priv->perf.oa.oa_formats[
> props->oa_format].size;
> > +
> > +       stream->sample_flags |= SAMPLE_OA_REPORT;
> > +       stream->sample_size += format_size;
> > +
> > +       dev_priv->perf.oa.oa_buffer.format_size = format_size;
> > +       BUG_ON(dev_priv->perf.oa.oa_buffer.format_size == 0);
> > +
> > +       dev_priv->perf.oa.oa_buffer.format =
> > +               dev_priv->perf.oa.oa_formats[props->oa_format].format;
> > +
> > +       dev_priv->perf.oa.metrics_set = props->metrics_set;
> > +
> > +       dev_priv->perf.oa.periodic = props->oa_periodic;
> > +       if (dev_priv->perf.oa.periodic) {
> > +               u64 period_ns = oa_exponent_to_ns(dev_priv,
> > +
>  props->oa_period_exponent);
> > +
> > +               dev_priv->perf.oa.period_exponent =
> props->oa_period_exponent;
> > +
> > +               /* See comment for OA_TAIL_MARGIN_NSEC for details
> > +                * about this tail_margin...
> > +                */
> > +               dev_priv->perf.oa.tail_margin =
> > +                       ((OA_TAIL_MARGIN_NSEC / period_ns) + 1) *
> format_size;
> > +       }
> > +
> > +       ret = alloc_oa_buffer(dev_priv);
> > +       if (ret)
> > +               return ret;
> > +
> > +       /* PRM - observability performance counters:
> > +        *
> > +        *   OACONTROL, performance counter enable, note:
> > +        *
> > +        *   "When this bit is set, in order to have coherent counts,
> > +        *   RC6 power state and trunk clock gating must be disabled.
> > +        *   This can be achieved by programming MMIO registers as
> > +        *   0xA094=0 and 0xA090[31]=1"
> > +        *
> > +        *   In our case we are expecting that taking pm + FORCEWAKE
> > +        *   references will effectively disable RC6.
> > +        */
> > +       intel_runtime_pm_get(dev_priv);
> > +       intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
> > +
> > +       ret = dev_priv->perf.oa.ops.enable_metric_set(dev_priv);
> > +       if (ret) {
> > +               intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
> > +               intel_runtime_pm_put(dev_priv);
> > +               free_oa_buffer(dev_priv);
> > +               return ret;
> > +       }
> > +
> > +       stream->ops = &i915_oa_stream_ops;
> > +
> > +       /* On Haswell we have to track which OASTATUS1 flags we've
> already
> > +        * seen since they can't be cleared while periodic sampling is
> enabled.
> > +        */
> > +       dev_priv->perf.oa.gen7_latched_oastatus1 = 0;
> We already did this step.
>
>
Ah, yep.



> > +
> > +       dev_priv->perf.oa.exclusive_stream = stream;
> > +
> > +       return 0;
> > +}
> > +
> > +static void gen7_update_hw_ctx_id_locked(struct drm_i915_private
> *dev_priv,
> > +                                        u32 ctx_id)
> > +{
> > +       assert_spin_locked(&dev_priv->perf.hook_lock);
> > +
> > +       dev_priv->perf.oa.specific_ctx_id = ctx_id;
> > +       gen7_update_oacontrol_locked(dev_priv);
> > +}
> > +
> > +static void
> > +i915_oa_legacy_context_pin_notify_locked(struct drm_i915_private
> *dev_priv,
> > +                                        struct i915_gem_context *ctx)
> > +{
> > +       assert_spin_locked(&dev_priv->perf.hook_lock);
> > +
> > +       BUG_ON(i915.enable_execlists);
> > +
> > +       if (dev_priv->perf.oa.ops.update_hw_ctx_id_locked == NULL)
> For consistency, if (!dev_priv->perf.oa.ops.update_hw_ctx_id_locked)
>
> > +               return;
> > +
> > +       if (dev_priv->perf.oa.exclusive_stream &&
> > +           dev_priv->perf.oa.exclusive_stream->ctx == ctx) {
> > +               struct i915_vma *vma = ctx->engine[RCS].state;
> > +               u32 ctx_id = i915_ggtt_offset(vma);
> > +
> > +               dev_priv->perf.oa.ops.update_hw_ctx_id_locked(dev_priv,
> ctx_id);
> > +       }
> > +}
> > +
>
> > @@ -431,8 +1367,35 @@ int i915_perf_open_ioctl(struct drm_device *dev,
> void *data,
> >
> >  void i915_perf_init(struct drm_i915_private *dev_priv)
> >  {
> > +       if (!IS_HASWELL(dev_priv))
> > +               return;
> > +
> > +       hrtimer_init(&dev_priv->perf.oa.poll_check_timer,
> > +                    CLOCK_MONOTONIC, HRTIMER_MODE_REL);
> > +       dev_priv->perf.oa.poll_check_timer.function =
> oa_poll_check_timer_cb;
> > +       init_waitqueue_head(&dev_priv->perf.oa.poll_wq);
> > +
> >         INIT_LIST_HEAD(&dev_priv->perf.streams);
> >         mutex_init(&dev_priv->perf.lock);
> > +       spin_lock_init(&dev_priv->perf.hook_lock);
> > +
> > +       dev_priv->perf.oa.ops.init_oa_buffer = gen7_init_oa_buffer;
> > +       dev_priv->perf.oa.ops.enable_metric_set = hsw_enable_metric_set;
> > +       dev_priv->perf.oa.ops.disable_metric_set =
> hsw_disable_metric_set;
> > +       dev_priv->perf.oa.ops.oa_enable = gen7_oa_enable;
> > +       dev_priv->perf.oa.ops.oa_disable = gen7_oa_disable;
> > +       dev_priv->perf.oa.ops.update_hw_ctx_id_locked =
> > +               gen7_update_hw_ctx_id_locked;
> > +       dev_priv->perf.oa.ops.read = gen7_oa_read;
> > +       dev_priv->perf.oa.ops.oa_buffer_is_empty =
> > +               gen7_oa_buffer_is_empty_fop_unlocked;
> > +
> > +       dev_priv->perf.oa.timestamp_frequency = 12500000;
> Slightly magical.
>

Not sure how to dymistify this with a comment; it pretty much is just a
magic hardware constant detailing the fixed frequency of the HW timestamp
counter. I think it's likely not actually specified in the PRMs for < gen 9
considering that I think for all gens prior to SKL the frequency has
remained the same.

Other software that currently deals with these raw HW timestamps (such as
Mesa) actually work in terms of an integer multiplier (80) when scaling HW
timestamps to have nanosecond units, and you can derive the frequency from
that. Maybe the scalar of 80 is noted in the PRMs somewhere instead of a
frequency.

Awkwardly once we get to Skylake an integer multiple no longer works, so
it's preferable to track the platform differences in terms of a frequency
like this.

I'll consider noting some of this in a comment.


> > diff --git a/drivers/gpu/drm/i915/intel_ringbuffer.c
> b/drivers/gpu/drm/i915/intel_ringbuffer.c
> > index 7a74750..22d5ff1 100644
> > --- a/drivers/gpu/drm/i915/intel_ringbuffer.c
> > +++ b/drivers/gpu/drm/i915/intel_ringbuffer.c
> > @@ -2043,8 +2043,14 @@ static int intel_ring_context_pin(struct
> i915_gem_context *ctx,
> >                 if (ret)
> >                         goto error;
> >
> > -               ret = i915_vma_pin(ce->state, 0, ctx->ggtt_alignment,
> > -                                  PIN_GLOBAL | PIN_HIGH);
> > +               if (engine->id == RCS) {
> > +                       u64 vma_flags = PIN_GLOBAL | PIN_HIGH;
> Maybe move this up a level and reuse for both paths.
>

Ah yep,

>
> > +                       ret = i915_gem_context_pin_legacy_
> rcs_state(engine->i915,
> > +                                                                   ctx,
> > +
>  vma_flags);
> > +               } else
> > +                       ret = i915_vma_pin(ce->state, 0,
> ctx->ggtt_alignment,
> > +                                          PIN_GLOBAL | PIN_HIGH);
> >                 if (ret)
> >                         goto error;
> >         }
>



I'm travelling this week, but will send out an updated patch when I've had
a chance to test the change for initializing the specific_ctx_id on haswell.

thanks for the feedback.

Regards,
- Robert

[-- Attachment #1.2: Type: text/html, Size: 28255 bytes --]

[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [Intel-gfx] [PATCH] drm/i915: Add i915 perf infrastructure
  2016-10-11 19:03         ` [Intel-gfx] " Robert Bragg
@ 2016-10-12 11:41           ` Joonas Lahtinen
  2016-10-19 16:35             ` Robert Bragg
  0 siblings, 1 reply; 33+ messages in thread
From: Joonas Lahtinen @ 2016-10-12 11:41 UTC (permalink / raw)
  To: Robert Bragg, Matthew Auld
  Cc: Daniel Vetter, Intel Graphics Development, Sourab Gupta, ML dri-devel

On ti, 2016-10-11 at 12:03 -0700, Robert Bragg wrote:
> > > +               case DRM_I915_PERF_PROP_MAX:
> > > +                       BUG();
> > 
> > We already handle this case above, but I guess we still need this in
> > order to silence gcc...
> 
> right, and preferable to having a default: case, for the future compiler warning to handle any new properties here.

Please, do use MISSING_CASE instead. Daniel is known to get upset for
far less ;)

Generally consensus is that BUG() is used only when there're no other options to back out.

Regards, Joonas
-- 
Joonas Lahtinen
Open Source Technology Center
Intel Corporation
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [PATCH] drm/i915: Add i915 perf infrastructure
  2016-10-12 11:41           ` Joonas Lahtinen
@ 2016-10-19 16:35             ` Robert Bragg
  2016-10-20 15:19               ` [Intel-gfx] " Joonas Lahtinen
  0 siblings, 1 reply; 33+ messages in thread
From: Robert Bragg @ 2016-10-19 16:35 UTC (permalink / raw)
  To: Joonas Lahtinen
  Cc: David Airlie, Intel Graphics Development, ML dri-devel,
	Sourab Gupta, Daniel Vetter


[-- Attachment #1.1: Type: text/plain, Size: 1079 bytes --]

On Wed, Oct 12, 2016 at 12:41 PM, Joonas Lahtinen <
joonas.lahtinen@linux.intel.com> wrote:

> On ti, 2016-10-11 at 12:03 -0700, Robert Bragg wrote:
> > > > +               case DRM_I915_PERF_PROP_MAX:
> > > > +                       BUG();
> > >
> > > We already handle this case above, but I guess we still need this in
> > > order to silence gcc...
> >
> > right, and preferable to having a default: case, for the future compiler
> warning to handle any new properties here.
>
> Please, do use MISSING_CASE instead. Daniel is known to get upset for
> far less ;)
>
> Generally consensus is that BUG() is used only when there're no other
> options to back out.
>

thanks for this pointer.

I'll add a default: with MISSING_CASE as that looks like an i915-specific
convention; though it seems like a real shame to defer missing case issues
to runtime errors instead of taking advantage of the compiler complaining
at build time that a case has been forgotten.

Thanks,
- Robert



>
> Regards, Joonas
> --
> Joonas Lahtinen
> Open Source Technology Center
> Intel Corporation
>

[-- Attachment #1.2: Type: text/html, Size: 1811 bytes --]

[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: [Intel-gfx] [PATCH] drm/i915: Add i915 perf infrastructure
  2016-10-19 16:35             ` Robert Bragg
@ 2016-10-20 15:19               ` Joonas Lahtinen
  0 siblings, 0 replies; 33+ messages in thread
From: Joonas Lahtinen @ 2016-10-20 15:19 UTC (permalink / raw)
  To: Robert Bragg
  Cc: Intel Graphics Development, Matthew Auld, ML dri-devel,
	Sourab Gupta, Daniel Vetter

On ke, 2016-10-19 at 17:35 +0100, Robert Bragg wrote:
> I'll add a default: with MISSING_CASE as that looks like an i915-
> specific convention; though it seems like a real shame to defer
> missing case issues to runtime errors instead of taking advantage of
> the compiler complaining at build time that a case has been
> forgotten.

I think the key point here is not "having MISSING_CASE", but "not
having BUG".

There has been talk about using compile time checking more effectively,
so adding default is not needed. You can keep similar code construct
but reduce into WARN_ONCE or so.

Regards, Joonas
-- 
Joonas Lahtinen
Open Source Technology Center
Intel Corporation
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 33+ messages in thread

end of thread, other threads:[~2016-10-20 15:19 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-14 14:19 [PATCH v5 00/11] Enable i915 perf stream for Haswell OA unit Robert Bragg
2016-09-14 14:19 ` [PATCH v5 01/11] drm/i915: Add i915 perf infrastructure Robert Bragg
2016-09-14 14:42   ` [Intel-gfx] " Emil Velikov
2016-09-14 15:02     ` Robert Bragg
2016-09-14 15:32     ` [PATCH] " Robert Bragg
2016-10-07 17:10       ` Matthew Auld
2016-10-11 19:03         ` [Intel-gfx] " Robert Bragg
2016-10-12 11:41           ` Joonas Lahtinen
2016-10-19 16:35             ` Robert Bragg
2016-10-20 15:19               ` [Intel-gfx] " Joonas Lahtinen
2016-09-14 14:19 ` [PATCH v5 02/11] drm/i915: rename OACONTROL GEN7_OACONTROL Robert Bragg
2016-10-07 17:11   ` Matthew Auld
2016-09-14 14:19 ` [PATCH v5 03/11] drm/i915: return EACCES for check_cmd() failures Robert Bragg
2016-09-14 14:19 ` [PATCH v5 04/11] drm/i915: don't whitelist oacontrol in cmd parser Robert Bragg
2016-09-14 14:19 ` [PATCH v5 05/11] drm/i915: Add 'render basic' Haswell OA unit config Robert Bragg
2016-10-07 17:15   ` Matthew Auld
2016-09-14 14:19 ` [PATCH v5 06/11] drm/i915: Enable i915 perf stream for Haswell OA unit Robert Bragg
2016-10-07 17:19   ` Matthew Auld
2016-10-12  0:55     ` Robert Bragg
2016-09-14 14:19 ` [PATCH v5 07/11] drm/i915: advertise available metrics via sysfs Robert Bragg
2016-10-07 17:20   ` Matthew Auld
2016-09-14 14:19 ` [PATCH v5 08/11] drm/i915: Add dev.i915.perf_event_paranoid sysctl option Robert Bragg
2016-10-07 17:21   ` Matthew Auld
2016-09-14 14:19 ` [PATCH v5 09/11] drm/i915: add oa_event_min_timer_exponent sysctl Robert Bragg
2016-10-07 17:21   ` Matthew Auld
2016-09-14 14:19 ` [PATCH v5 10/11] drm/i915: Add more Haswell OA metric sets Robert Bragg
2016-10-07 17:22   ` Matthew Auld
2016-09-14 14:19 ` [PATCH v5 11/11] drm/i915: Add a kerneldoc summary for i915_perf.c Robert Bragg
2016-10-07 17:24   ` Matthew Auld
2016-09-14 14:50 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3) Patchwork
2016-09-14 16:19 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4) Patchwork
2016-09-15  7:20 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev3) Patchwork
2016-09-15  7:20 ` ✗ Fi.CI.BAT: failure for Enable i915 perf stream for Haswell OA unit (rev4) Patchwork

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.