Skip to contentSkip to conversation

drm/i915/context

RFC PATCH New Ben Widawsky 1 participant 9 messages Feb 26, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover drm/i915/context

drivers/gpu/drm/i915/Makefile           |    1 +
drivers/gpu/drm/i915/i915_context.c     |  489 +++++++++++++++++++++++++++++++
drivers/gpu/drm/i915/i915_dma.c         |    8 +
drivers/gpu/drm/i915/i915_drv.h         |   41 +++
drivers/gpu/drm/i915/i915_reg.h         |    1 +
drivers/gpu/drm/i915/intel_ringbuffer.c |   97 ++++++
drivers/gpu/drm/i915/intel_ringbuffer.h |    6 +
include/drm/i915_drm.h                  |   20 ++
8 files changed, 663 insertions(+), 0 deletions(-)

I'm sorry, I know you're probably all getting sick of this by now... I am too.

This patch series enables context creation and destruction via IOCTLs. It is lacking functionality as compared to the previous patch series (as well as what was in my fd.o repo).

While I continue to work on the client side of this feature, it would be helpful if people could review this to find any major flaws. The comments I have received so far (mostly from Chris and Daniel) have been extremely helpful.

More details:
Context validation (checking if context associated bos have moved) is gone. Also, there are no more references to ppgtt.

I will build up the context validation as I implement the mesa side. The plan for validation is the same as before, in that once a context bo gets an address, the code will validate at execbuffer time if the bo is still there, and force the client to resubmit if a bo moved. The big open right now is whether or not a buffer is explicitly associated via some part of the ioctl interface, or if the client code implicitly associates buffers by setting an address for the buffer when it does execbuffer. The previous patches did the former, I feel the latter is a better solution because it integrates with ppgtt a lot more nicely. Both solutions provide a poor way to disassociate a buffer, with the former being a bit better than the latter.

As for the ppgtt stuff, I had previously, and still believe the only relevant part is the desired aperture size, the rest should fall in pretty nicely with the logical context stuff.

I had a number of unit tests and stress tests which won't work because of the previously mentioned changes. However, here is my current unit test for the create/destroy:
http://cgit.freedesktop.org/~bwidawsk/intel-gpu-tools/diff/tests/gem_ctx_create.c?h=context_support

Here is the repo (be warned I have a tendency to push -f a lot):
http://cgit.freedesktop.org/~bwidawsk/drm-intel/log/

#1 lore raw

RFC PATCH 1/8 drm/i915/context: CXT_SIZE register offset added

---
 drivers/gpu/drm/i915/i915_reg.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
drivers/gpu/drm/i915/i915_reg.h +1 -0
index e1be98f..b37c175 100644
@@ -1249,6 +1249,7 @@
  */
 #define CCID			0x2180
 #define   CCID_EN		(1<<0)
+#define CXT_SIZE		0x21a0
 /*
  * Overlay regs
  */
Signature · 1 line
1.7.3.4
#2 lore raw

RFC PATCH 2/8 drm/i915/context: Preliminary context support

In reply to Ben Widawsky

Created a new file i915_context.c which will hold a majority of the context related code. This file has nulled functions that get called by the proper part of the i915 driver. This of course requires a Makefile addition, as well as calls to the new nulled functions from the other partsd of the driver (i915_dma.c).

Created 2 new ioctls, one for creating a context, and one for destroying a context. These are defined in the necessary places, and again have null functionality

---
 drivers/gpu/drm/i915/Makefile       |    1 +
 drivers/gpu/drm/i915/i915_context.c |   69 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/i915_dma.c     |    8 ++++
 drivers/gpu/drm/i915/i915_drv.h     |   31 ++++++++++++++++
 include/drm/i915_drm.h              |   16 ++++++++
 5 files changed, 125 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/i915_context.c
drivers/gpu/drm/i915/Makefile +1 -0
index 0ae6a7c..fa52bc0 100644
@@ -6,6 +6,7 @@ ccflags-y := -Iinclude/drm
 i915-y := i915_drv.o i915_dma.o i915_irq.o i915_mem.o \
 	  i915_debugfs.o \
           i915_suspend.o \
+	  i915_context.o \
 	  i915_gem.o \
 	  i915_gem_debug.o \
 	  i915_gem_evict.o \
drivers/gpu/drm/i915/i915_context.c new file +69 -0

69 lines not on this page · raw message

drivers/gpu/drm/i915/i915_dma.c +8 -0
index ffa2196..5dd2fca 100644
@@ -2041,6 +2041,8 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags)
 
 	ips_ping_for_i915_load();
 
+	i915_context_load(dev);
+
 	return 0;
 
 out_gem_unload:
@@ -2066,6 +2068,8 @@ int i915_driver_unload(struct drm_device *dev)
 	struct drm_i915_private *dev_priv = dev->dev_private;
 	int ret;
 
+	i915_context_unload(dev);
+
 	spin_lock(&mchdev_lock);
 	i915_mch_dev = NULL;
 	spin_unlock(&mchdev_lock);
@@ -2166,6 +2170,7 @@ int i915_driver_open(struct drm_device *dev, struct drm_file *file)
 	spin_lock_init(&file_priv->mm.lock);
 	INIT_LIST_HEAD(&file_priv->mm.request_list);
 
+	i915_context_open(dev, file);
 	return 0;
 }
 
@@ -2202,6 +2207,7 @@ void i915_driver_lastclose(struct drm_device * dev)
 void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
 {
 	drm_i915_private_t *dev_priv = dev->dev_private;
+	i915_context_close(dev, file_priv);
 	i915_gem_release(dev, file_priv);
 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
 		i915_mem_release(dev, file_priv, dev_priv->agp_heap);
@@ -2255,6 +2261,8 @@ struct drm_ioctl_desc i915_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
 	DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
 	DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
+	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_context_create_ioctl, DRM_UNLOCKED),
+	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_context_destroy_ioctl, DRM_UNLOCKED),
 };
 
 int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
drivers/gpu/drm/i915/i915_drv.h +31 -0
index 3d4fd01..3bd9875 100644
@@ -892,6 +892,22 @@ struct drm_i915_file_private {
 		struct spinlock lock;
 		struct list_head request_list;
 	} mm;
+
+	struct spinlock context_idr_lock;
+	struct idr context_idr;
+};
+
+struct drm_i915_gem_context {
+	struct drm_device *dev;
+	struct drm_file *file;
+
+	uint32_t id;
+	struct drm_gem_object *obj;
+	uint8_t ring_enable;
+	bool is_default;
+
+	struct drm_gem_object **bufs;
+	int slot_count;
 };
 
 enum intel_chip_family {
@@ -961,6 +977,9 @@ enum intel_chip_family {
 #define HAS_PCH_CPT(dev) (INTEL_PCH_TYPE(dev) == PCH_CPT)
 #define HAS_PCH_IBX(dev) (INTEL_PCH_TYPE(dev) == PCH_IBX)
 
+#define HAS_HW_CONTEXTS(dev)	(INTEL_INFO(dev)->gen >= 4)
+#define HAS_PPGTT(dev)		(INTEL_INFO(dev)->gen >= 7)
+
 #include "i915_trace.h"
 
 extern struct drm_ioctl_desc i915_ioctls[];
@@ -1123,6 +1142,18 @@ void i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
 				    struct intel_ring_buffer *ring,
 				    u32 seqno);
 
+/* i915_context.c */
+int i915_context_create_ioctl(struct drm_device *dev, void *data,
+			      struct drm_file *file);
+int i915_context_destroy_ioctl(struct drm_device *dev, void *data,
+			       struct drm_file *file);
+extern void i915_context_load(struct drm_device *dev);
+extern void i915_context_unload(struct drm_device *dev);
+extern void i915_context_open(struct drm_device *dev, struct drm_file *file);
+extern void i915_context_close(struct drm_device *dev, struct drm_file *file);
+extern struct drm_i915_gem_context *i915_get_context(struct drm_file *file,
+						     uint32_t id);
+
 /**
  * Returns true if seq1 is later than seq2.
  */
include/drm/i915_drm.h +16 -0
index 0039f1f..28e3c5f 100644
@@ -198,6 +198,8 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_OVERLAY_PUT_IMAGE	0x27
 #define DRM_I915_OVERLAY_ATTRS	0x28
 #define DRM_I915_GEM_EXECBUFFER2	0x29
+#define DRM_I915_GEM_CONTEXT_CREATE	0x2a
+#define DRM_I915_GEM_CONTEXT_DESTROY	0x2b
 
 #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)
@@ -239,6 +241,9 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_MADVISE	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MADVISE, struct drm_i915_gem_madvise)
 #define DRM_IOCTL_I915_OVERLAY_PUT_IMAGE	DRM_IOW(DRM_COMMAND_BASE + DRM_IOCTL_I915_OVERLAY_ATTRS, struct drm_intel_overlay_put_image)
 #define DRM_IOCTL_I915_OVERLAY_ATTRS	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_OVERLAY_ATTRS, struct drm_intel_overlay_attrs)
+#define DRM_IOCTL_I915_GEM_CONTEXT_CREATE	DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_CREATE, struct drm_i915_gem_context_create)
+#define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
+
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -843,4 +848,15 @@ struct drm_intel_overlay_attrs {
 	__u32 gamma5;
 };
 
+struct drm_i915_gem_context_create {
+	/* input: number of slots to allocate */
+	__s32 slot_count;
+	/* output: id of new context*/
+	__u32 ctx_id;
+};
+
+struct drm_i915_gem_context_destroy {
+	__u32 ctx_id;
+};
+
 #endif				/* _I915_DRM_H_ */
Signature · 1 line
1.7.3.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#3 lore raw

RFC PATCH 3/8 drm/i915/context: implement load and unload

In reply to Ben Widawsky

Loading of the context submodule context, which is called from the main i915 driver, must create and initialize the default context for the device. The default context is used for clients which do not set up contexts (legacy), and as a known good state for the hardware, because once contexts are enabled, you can't disable them. Doing this at initialization allows later clients to determine whether or not context support exist before actually trying to create or run them (if there was an error at load, contexts will be disabled).

Unloading the context is much easier. The ref counts, and pin counts are taken care of by the rest of the driver, so all this code needs to do is free the kernel memory associated with the default context.

This commit doesn't exercise the hardware's context switch yet, but does everything else needing to create and destroy the default context, including:
context id creation
context id destruction
context allocation
context initialization (currently a nop)

---
 drivers/gpu/drm/i915/i915_context.c     |  189 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/i915_drv.h         |    6 +-
 drivers/gpu/drm/i915/intel_ringbuffer.h |    6 +
 include/drm/i915_drm.h                  |    6 +-
 4 files changed, 205 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_context.c +189 -0
index bae7274..490aa5a 100644
@@ -26,7 +26,163 @@
 
 #include "drmP.h"
 #include "i915_drm.h"
+#include "i915_drv.h"
 
+#define DEFAULT_CONTEXT_ID 0
+
+#define CONTEXT_SIZE dev_priv->context_size
+#define CONTEXT_ALIGN 4096
+
+static int context_generate_id(struct drm_i915_gem_context *ctx)
+{
+	struct drm_i915_file_private *file_priv = ctx->file->driver_priv;
+	int ret, id;
+
+	if (WARN_ON(!mutex_is_locked(&ctx->dev->struct_mutex)))
+		return -ENOENT;
+
+again:
+	if (idr_pre_get(&file_priv->context_idr, GFP_KERNEL) == 0)
+		return -ENOMEM;
+
+	spin_lock(&file_priv->context_idr_lock);
+	ret = idr_get_new_above(&file_priv->context_idr, ctx,
+				DEFAULT_CONTEXT_ID + 1, &id);
+	if (ret == -EAGAIN) {
+		spin_unlock(&file_priv->context_idr_lock);
+		goto again;
+	}
+	spin_unlock(&file_priv->context_idr_lock);
+
+	return id;
+}
+
+static void context_destroy_id(struct drm_i915_gem_context *ctx)
+{
+	struct drm_i915_file_private *file_priv = ctx->file->driver_priv;
+
+	spin_lock(&file_priv->context_idr_lock);
+	idr_remove(&file_priv->context_idr, ctx->id);
+	spin_unlock(&file_priv->context_idr_lock);
+}
+
+/*
+ * Initialize a context for the given ring.
+ * @wait_for_switch: whether or not to wait for the context switch instruction
+ * to actually execute.
+ */
+static int context_init(struct drm_i915_gem_context *ctx,
+			struct intel_ring_buffer *ring,
+			bool wait_for_switch)
+{
+	struct drm_i915_private *dev_priv = ctx->dev->dev_private;
+	struct drm_i915_gem_context *last;
+	int ret;
+
+	if (ring->context_switch == NULL)
+		return 0;
+
+	return -ENOMEM;
+}
+
+/*
+ * Logical context is created, and initialized. The context has a backing buffer
+ * object which is referenced at the end of this function.
+ *
+ * @ring_mask: rings for which this context should be initialized (1 means don't
+ *	init)
+ * @ctx_out: output context which was created
+ *
+ * XXX this function or a wrapper to it needs to be global for power related
+ * stuff
+ */
+static int logical_context_alloc(struct drm_device *dev, struct drm_file *file,
+				 uint8_t ring_mask,
+				 struct drm_i915_gem_context **ctx_out)
+{
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_i915_gem_context *ctx;
+	int ret, i;
+#define RING_MASK (uint8_t)((1 << I915_NUM_RINGS) - 1)
+	uint8_t pass = 0;
+
+	if (WARN_ON(!mutex_is_locked(&dev->struct_mutex)))
+		return -EINVAL;
+
+	ctx = kzalloc(sizeof(struct drm_i915_gem_context), GFP_KERNEL);
+	if (ctx == NULL)
+		return -ENOMEM;
+
+	ctx->dev = dev;
+	ctx->file = file;
+
+	if (dev_priv->default_context == *ctx_out) {
+		ctx->is_default = true;
+		ctx->id = DEFAULT_CONTEXT_ID;
+	} else
+		ctx->id = context_generate_id(ctx);
+
+
+	ctx->obj = i915_gem_alloc_object(dev, CONTEXT_SIZE);
+	if (!ctx->obj) {
+		ret = -ENOMEM;
+		goto id_out;
+	}
+
+	ret = i915_gem_object_pin(ctx->obj, CONTEXT_ALIGN, false);
+	if (ret) {
+		DRM_ERROR("Failed to pin context: %d\n", ret);
+		goto err_unref;
+	}
+
+	/* XXX do we need this, we can get rid of pinning above if not? */
+	ret = i915_gem_object_set_to_gtt_domain(ctx->obj, false);
+	if (ret) {
+		DRM_ERROR("failed to set domain on context: %d", ret);
+		goto err_unpin;
+	}
+
+	i915_gem_object_unpin(ctx->obj);
+
+	for (i = 0; i < I915_NUM_RINGS; i++) {
+		if (ring_mask & (1 << i))
+			continue;
+
+		ret = context_init(ctx, &dev_priv->ring[i], true);
+		if (!ret)
+			pass |= (1 << i);
+	}
+
+	if ((ring_mask ^ pass) != RING_MASK) {
+		DRM_DEBUG_DRIVER("ring(s) couldn't initialize context "
+				 "mask = %x, passed = %x\n", ring_mask, pass);
+		if (!pass) {
+			DRM_ERROR("Couldn't initialize context for any ring");
+			ret = -EIO;
+			goto err_unref;
+		}
+	}
+
+	ctx->ring_enable = pass;
+
+	DRM_DEBUG_DRIVER("Context %d allocated, rings %x\n", ctx->id, pass);
+	*ctx_out = ctx;
+	return 0;
+
+err_unpin:
+	i915_gem_object_unpin(ctx->obj);
+err_unref:
+	drm_gem_object_unreference(&ctx->obj->base);
+id_out:
+	if (!ctx->is_default)
+		context_destroy_id(ctx);
+	kfree(ctx);
+	return ret;
+}
+
+/**
+ * i915_context_create_ioctl() - not yet supported
+ */
 int i915_context_create_ioctl(struct drm_device *dev, void *data,
 			      struct drm_file *file)
 {
@@ -35,6 +191,9 @@ int i915_context_create_ioctl(struct drm_device *dev, void *data,
 	return -EIO;
 }
 
+/**
+ * i915_context_destroy_ioctl() - not yet supported
+ */
 int i915_context_destroy_ioctl(struct drm_device *dev, void *data,
 			       struct drm_file *file)
 {
@@ -42,14 +201,44 @@ int i915_context_destroy_ioctl(struct drm_device *dev, void *data,
 	return -EINVAL;
 }
 
+/**
+ * i915_context_load() - Creates a default context.
+ * @dev: Device for which the context.
+ */
 void i915_context_load(struct drm_device *dev)
 {
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	uint32_t size;
+
+	int ret;
+	if (!HAS_HW_CONTEXTS(dev)) {
+		dev_priv->contexts_disabled = true;
+		return;
+	}
 
+	mutex_lock(&dev->struct_mutex);
+	size = I915_READ(CXT_SIZE);
+	if (size > 0x4000) /*  1MB */ {
+		DRM_ERROR("Context bo size seems invalid.");
+		size = 20;
+	}
+	size *= 64;
+	size = roundup(size, 4096);
+	dev_priv->context_size = size;
+	DRM_DEBUG_DRIVER("Logical context size = %d\n", size);
+	ret = logical_context_alloc(dev, NULL, 0, &dev_priv->default_context);
+	if (ret)
+		dev_priv->contexts_disabled = true;
+	mutex_unlock(&dev->struct_mutex);
 }
 
 void i915_context_unload(struct drm_device *dev)
 {
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	if (dev_priv->contexts_disabled)
+		return;
 
+	kfree(dev_priv->default_context);
 }
 
 void i915_context_open(struct drm_device *dev, struct drm_file *file)
drivers/gpu/drm/i915/i915_drv.h +5 -1
index 3bd9875..15f86fb 100644
@@ -709,6 +709,10 @@ typedef struct drm_i915_private {
 	struct intel_fbdev *fbdev;
 
 	struct drm_property *broadcast_rgb_property;
+
+	uint32_t context_size;
+	struct drm_i915_gem_context *default_context;
+	bool contexts_disabled;
 } drm_i915_private_t;
 
 struct drm_i915_gem_object {
@@ -902,8 +906,8 @@ struct drm_i915_gem_context {
 	struct drm_file *file;
 
 	uint32_t id;
-	struct drm_gem_object *obj;
 	uint8_t ring_enable;
+	struct drm_i915_gem_object *obj;
 	bool is_default;
 
 	struct drm_gem_object **bufs;
drivers/gpu/drm/i915/intel_ringbuffer.h +6 -0
index bd6a5fb..a4e707c 100644
@@ -77,6 +77,12 @@ struct  intel_ring_buffer {
 	int		(*dispatch_execbuffer)(struct intel_ring_buffer *ring,
 					       u32 offset, u32 length);
 	void		(*cleanup)(struct intel_ring_buffer *ring);
+	struct		drm_i915_gem_context *last_context;
+	struct drm_i915_gem_context *(*context_switch)
+				      (struct intel_ring_buffer *ring,
+				       struct drm_i915_gem_context *ctx,
+				       u32 segno,
+				       u32 flags);
 
 	/**
 	 * List of objects currently involved in rendering from the
include/drm/i915_drm.h +5 -1
index 28e3c5f..49df333 100644
@@ -851,8 +851,12 @@ struct drm_intel_overlay_attrs {
 struct drm_i915_gem_context_create {
 	/* input: number of slots to allocate */
 	__s32 slot_count;
-	/* output: id of new context*/
+	/* input: desired ring mask */
+	__u8 ring_mask;
+	/* output: id of new context */
 	__u32 ctx_id;
+	/* output: rings for which context is valid */
+	__u8 ring_enable;
 };
 
 struct drm_i915_gem_context_destroy {
Signature · 1 line
1.7.3.4
#4 lore raw

RFC PATCH 4/8 drm/i915/context: logical context free

In reply to Ben Widawsky

Another review only commit. The opposite of context alloc is context free. Context free needs to employ context_fini (the opposite of context init) which will switch back to the default context if needed. This function is not currently implemented.

---
 drivers/gpu/drm/i915/i915_context.c |   37 +++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)
drivers/gpu/drm/i915/i915_context.c +37 -0
index 490aa5a..babceca 100644
@@ -180,6 +180,43 @@ id_out:
 	return ret;
 }
 
+static void logical_context_fini(struct drm_i915_gem_context *ctx)
+{
+
+}
+
+static int logical_context_free(struct drm_file *file, uint32_t id)
+{
+	struct drm_i915_gem_context *ctx;
+
+	if (WARN_ON(id == DEFAULT_CONTEXT_ID))
+		return 0;
+
+	/*  ref and pin the object */
+	ctx = i915_get_context(file, id);
+	if (!ctx) {
+		DRM_ERROR("Couldn't find context %d", id);
+		return -EINVAL;
+	}
+
+	i915_release_context(ctx);
+
+	mutex_lock(&ctx->dev->struct_mutex);
+
+	logical_context_fini(ctx);
+	drm_gem_object_unreference(&ctx->obj->base);
+
+	mutex_unlock(&ctx->dev->struct_mutex);
+
+	context_destroy_id(ctx);
+
+	ctx->file = NULL;
+	ctx->dev = NULL;
+	kfree(ctx);
+
+	return 0;
+}
+
 /**
  * i915_context_create_ioctl() - not yet supported
  */
Signature · 1 line
1.7.3.4
#5 lore raw

RFC PATCH 5/8 drm/i915/context: implement context switch wait

In reply to Ben Widawsky

This commit is for review readability only.

Function implements the wait for context switch.

---
 drivers/gpu/drm/i915/i915_context.c |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
drivers/gpu/drm/i915/i915_context.c +23 -0
index babceca..45b5181 100644
@@ -66,6 +66,29 @@ static void context_destroy_id(struct drm_i915_gem_context *ctx)
 	spin_unlock(&file_priv->context_idr_lock);
 }
 
+static int wait_for_context_switch(struct intel_ring_buffer *ring)
+{
+	struct drm_i915_gem_request *request;
+	int ret;
+
+	request = kzalloc(sizeof(*request), GFP_KERNEL);
+	if (request == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = i915_add_request(ring, NULL, request);
+	if (ret) {
+		kfree(request);
+		goto out;
+	}
+
+	ret = i915_wait_request(ring, request->seqno);
+
+out:
+	return ret;
+}
+
 /*
  * Initialize a context for the given ring.
  * @wait_for_switch: whether or not to wait for the context switch instruction
Signature · 1 line
1.7.3.4
#6 lore raw

RFC PATCH 6/8 drm/i915/context: context init implementation

In reply to Ben Widawsky

Again for the sake of review, this commit is broken out. Context init for default context and subsequent contexts is implemented here. Since none of the context_switch code is implemented, it doesn't really do much.

---
 drivers/gpu/drm/i915/i915_context.c |   67 +++++++++++++++++++++++++++++++++-
 drivers/gpu/drm/i915/i915_drv.h     |    5 +++
 2 files changed, 70 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_context.c +65 -2
index 45b5181..2e38fa8 100644
@@ -99,13 +99,70 @@ static int context_init(struct drm_i915_gem_context *ctx,
 			bool wait_for_switch)
 {
 	struct drm_i915_private *dev_priv = ctx->dev->dev_private;
-	struct drm_i915_gem_context *last;
+	struct drm_i915_gem_context *last = NULL;
 	int ret;
 
 	if (ring->context_switch == NULL)
 		return 0;
 
-	return -ENOMEM;
+	drm_gem_object_reference(&ctx->obj->base);
+	ret = i915_gem_object_pin(ctx->obj, CONTEXT_ALIGN, false);
+	if (ret) {
+		drm_gem_object_unreference(&ctx->obj->base);
+		return ret;
+	}
+
+	if (ctx->is_default) {
+		/*
+		 * XXX default context is always first. The first context needs
+		 * to do an extra save because the first save (according to the
+		 * spec) doesn't actually do anything. So the outcome is
+		 * 1. Save without restore (no context saved)
+		 * 2. Save without restore (context is saved)
+		 * 3. Save with restore (loads the ctx from step 2)
+		 */
+		last = ring->context_switch(ring, ctx, 0,
+					    I915_CONTEXT_SAVE_ONLY);
+		if (!last) {
+			ret = EIO;
+			goto err_out;
+		}
+		last = ring->context_switch(ring, ctx, 0,
+					    I915_CONTEXT_SAVE_ONLY |
+					    I915_CONTEXT_FORCED_SWITCH);
+		if (!last) {
+			ret = EIO;
+			goto err_out;
+		}
+		last = ring->context_switch(ring, ctx, 0,
+					    I915_CONTEXT_NORMAL_SWITCH |
+					    I915_CONTEXT_FORCED_SWITCH);
+	} else {
+		last = ring->context_switch(ring, ctx, 0,
+					    I915_CONTEXT_SAVE_ONLY);
+	}
+
+	if (!last) {
+		ret = EIO;
+		goto err_out;
+	}
+
+	if (!last->is_default)
+		i915_release_context(last);
+
+	if (wait_for_switch) {
+		ret = wait_for_context_switch(ring);
+		if (ret)
+			goto err_out;
+	}
+
+	return 0;
+
+err_out:
+	WARN_ON(ctx->is_default);
+	i915_gem_object_unpin(ctx->obj);
+	drm_gem_object_unreference(&ctx->obj->base);
+	return ret;
 }
 
 /*
@@ -316,3 +373,9 @@ struct drm_i915_gem_context *i915_get_context(struct drm_file *file,
 {
 	return NULL;
 }
+
+void i915_release_context(struct drm_i915_gem_context *ctx)
+{
+	i915_gem_object_unpin(ctx->obj);
+	drm_gem_object_unreference(&ctx->obj->base);
+}
drivers/gpu/drm/i915/i915_drv.h +5 -0
index 15f86fb..98ccbd9 100644
@@ -914,6 +914,10 @@ struct drm_i915_gem_context {
 	int slot_count;
 };
 
+#define I915_CONTEXT_NORMAL_SWITCH	(1 << 0)
+#define I915_CONTEXT_SAVE_ONLY		(1 << 1)
+#define I915_CONTEXT_FORCED_SWITCH	(1 << 2)
+
 enum intel_chip_family {
 	CHIP_I8XX = 0x01,
 	CHIP_I9XX = 0x02,
@@ -1157,6 +1161,7 @@ extern void i915_context_open(struct drm_device *dev, struct drm_file *file);
 extern void i915_context_close(struct drm_device *dev, struct drm_file *file);
 extern struct drm_i915_gem_context *i915_get_context(struct drm_file *file,
 						     uint32_t id);
+extern void i915_release_context(struct drm_i915_gem_context *ctx);
 
 /**
  * Returns true if seq1 is later than seq2.
Signature · 1 line
1.7.3.4
#7 lore raw

RFC PATCH 7/8 drm/i915/context: context switch implementation

In reply to Ben Widawsky

Once again, split out mostly for review...

Implementation of the context switch code (MI_SET_CONTEXT), which is supposed to be per ringbuffer. There is a bit of ugliness for the initial default context load, for which I had no better solution.

Slightly modified the default context creation code to check for the previously mentioned ugliness.

---
 drivers/gpu/drm/i915/i915_context.c     |   28 ++++++---
 drivers/gpu/drm/i915/i915_drv.h         |    1 +
 drivers/gpu/drm/i915/intel_ringbuffer.c |   97 +++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+), 9 deletions(-)
drivers/gpu/drm/i915/i915_context.c +19 -9
index 2e38fa8..3719a87 100644
@@ -123,28 +123,34 @@ static int context_init(struct drm_i915_gem_context *ctx,
 		 */
 		last = ring->context_switch(ring, ctx, 0,
 					    I915_CONTEXT_SAVE_ONLY);
-		if (!last) {
-			ret = EIO;
+		if (last != ctx) {
+			DRM_ERROR("Context switch state invalid");
+			ret = -EIO;
 			goto err_out;
 		}
 		last = ring->context_switch(ring, ctx, 0,
 					    I915_CONTEXT_SAVE_ONLY |
 					    I915_CONTEXT_FORCED_SWITCH);
-		if (!last) {
-			ret = EIO;
+		if (last != ctx) {
+			DRM_ERROR("First context switch fail");
+			ret = -EIO;
 			goto err_out;
 		}
 		last = ring->context_switch(ring, ctx, 0,
 					    I915_CONTEXT_NORMAL_SWITCH |
 					    I915_CONTEXT_FORCED_SWITCH);
+		if (last != ctx) {
+			DRM_ERROR("Final context switch fail");
+			ret = -EIO;
+			goto err_out;
+		}
 	} else {
 		last = ring->context_switch(ring, ctx, 0,
 					    I915_CONTEXT_SAVE_ONLY);
-	}
-
-	if (!last) {
-		ret = EIO;
-		goto err_out;
+		if (!last) {
+			ret = -EIO;
+			goto err_out;
+		}
 	}
 
 	if (!last->is_default)
@@ -346,7 +352,11 @@ void i915_context_load(struct drm_device *dev)
 	ret = logical_context_alloc(dev, NULL, 0, &dev_priv->default_context);
 	if (ret)
 		dev_priv->contexts_disabled = true;
+	else {
+		DRM_DEBUG_DRIVER("HW context support initialized\n");
+	}
 	mutex_unlock(&dev->struct_mutex);
+
 }
 
 void i915_context_unload(struct drm_device *dev)
drivers/gpu/drm/i915/i915_drv.h +1 -0
index 98ccbd9..39669ff 100644
@@ -909,6 +909,7 @@ struct drm_i915_gem_context {
 	uint8_t ring_enable;
 	struct drm_i915_gem_object *obj;
 	bool is_default;
+	bool is_initialized;
 
 	struct drm_gem_object **bufs;
 	int slot_count;
drivers/gpu/drm/i915/intel_ringbuffer.c +97 -0

109 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#8 lore raw

RFC PATCH 8/8 drm/i915/context: create and destroy ioctls

In reply to Ben Widawsky

The create and destroy ioctls are quite simple but depend on a lot of other infrastructure, including:
file open handling
file close handling - cleanup stray contexts context_fini - possible switch back to default context get_context - object lookup is needed by destroy ioctl

There are two things lacking from the previous set of patches:
* no mention of gtt size in create ioctl
* no handling of the "slots"

gtt size and everything else related to ppgtt will be deferred as much as possible. I think my dream of getting the API right, and not having a long term topic branch is quickly becoming unattainable.

slots, I'm still undecided on whether or not to use slots to manage the buffers associated with the context. It remains in the ioctl semantics for now as I feel the API doesn't really suffer by having it there...

---
 drivers/gpu/drm/i915/i915_context.c |  110 +++++++++++++++++++++++++++++++++--
 1 files changed, 104 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/i915_context.c +104 -6
index 3719a87..da4a0ff 100644
@@ -33,6 +33,8 @@
 #define CONTEXT_SIZE dev_priv->context_size
 #define CONTEXT_ALIGN 4096
 
+static int context_idr_cleanup(int id, void *p, void *data);
+
 static int context_generate_id(struct drm_i915_gem_context *ctx)
 {
 	struct drm_i915_file_private *file_priv = ctx->file->driver_priv;
@@ -268,7 +270,50 @@ id_out:
 
 static void logical_context_fini(struct drm_i915_gem_context *ctx)
 {
+	struct drm_device *dev = ctx->dev;
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_i915_gem_context *last;
+	int i;
+	int ret = 0;
+
+	if (WARN_ON(!mutex_is_locked(&dev->struct_mutex)))
+		return;
+
+	if (ctx == dev_priv->default_context)
+		return;
+
+	for (i = 0; i < I915_NUM_RINGS; i++) {
+		struct intel_ring_buffer *ring = &dev_priv->ring[i];
+		if (!ring->context_switch)
+			continue;
+
+		if (!(ctx->ring_enable & (1 << i)))
+			continue;
+
+		if (ring->last_context != ctx)
+			continue;
 
+		/*
+		 * XXX We can prevent restoring contexts, but not saving them
+		 * so if we're going to take away our backing context object
+		 * of the last context, we have to switch now.
+		 */
+
+		DRM_DEBUG_DRIVER("Switching to default context\n");
+		last = ring->context_switch(ring, dev_priv->default_context,
+					    0, I915_CONTEXT_NORMAL_SWITCH);
+		if (!last) {
+			DRM_ERROR("Couldn't switch back to default context\n");
+			continue;
+		}
+
+		BUG_ON(last != ctx);
+		ret = wait_for_context_switch(ring);
+		if (ret)
+			DRM_ERROR("Wait for default context switch failed "
+				  "ring = %d (%d)", i, ret);
+		i915_release_context(last);
+	}
 }
 
 static int logical_context_free(struct drm_file *file, uint32_t id)
@@ -304,24 +349,40 @@ static int logical_context_free(struct drm_file *file, uint32_t id)
 }
 
 /**
- * i915_context_create_ioctl() - not yet supported
+ * i915_context_create_ioctl()
  */
 int i915_context_create_ioctl(struct drm_device *dev, void *data,
 			      struct drm_file *file)
 {
 	struct drm_i915_gem_context_create *args = data;
-	args->ctx_id = 0;
-	return -EIO;
+	struct drm_i915_gem_context *out_ctx = NULL;
+	int ret = 0;
+
+	mutex_lock(&dev->struct_mutex);
+	ret = logical_context_alloc(dev, file, args->ring_mask, &out_ctx);
+	mutex_unlock(&dev->struct_mutex);
+	if (ret == 0) {
+		args->ctx_id = out_ctx->id;
+		if (WARN_ON(args->ctx_id == DEFAULT_CONTEXT_ID))
+			return -ENXIO;
+		args->ring_enable = out_ctx->ring_enable;
+	}
+
+	return ret;
 }
 
 /**
- * i915_context_destroy_ioctl() - not yet supported
+ * i915_context_destroy_ioctl()
  */
 int i915_context_destroy_ioctl(struct drm_device *dev, void *data,
 			       struct drm_file *file)
 {
 	struct drm_i915_gem_context_destroy *args = data;
-	return -EINVAL;
+
+	if (args->ctx_id == DEFAULT_CONTEXT_ID)
+		return -EINVAL;
+
+	return logical_context_free(file, args->ctx_id);
 }
 
 /**
@@ -370,18 +431,55 @@ void i915_context_unload(struct drm_device *dev)
 
 void i915_context_open(struct drm_device *dev, struct drm_file *file)
 {
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_i915_file_private *file_priv = file->driver_priv;
+
+	if (dev_priv->contexts_disabled)
+		return;
 
+	idr_init(&file_priv->context_idr);
+	spin_lock_init(&file_priv->context_idr_lock);
 }
 
 void i915_context_close(struct drm_device *dev, struct drm_file *file)
 {
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_i915_file_private *file_priv = file->driver_priv;
+
+	if (dev_priv->contexts_disabled)
+		return;
 
+	idr_for_each(&file_priv->context_idr, context_idr_cleanup, file);
+	idr_destroy(&file_priv->context_idr);
+}
+
+static int context_idr_cleanup(int id, void *p, void *data)
+{
+	struct drm_file *file = (struct drm_file *) data;
+	logical_context_free(file, id);
+	return 0;
 }
 
 struct drm_i915_gem_context *i915_get_context(struct drm_file *file,
 					      uint32_t id)
 {
-	return NULL;
+	struct drm_i915_file_private *file_priv = file->driver_priv;
+	struct drm_i915_gem_context *ctx = NULL;
+	int ret;
+
+	spin_lock(&file_priv->context_idr_lock);
+	ctx = idr_find(&file_priv->context_idr, id);
+	if (ctx) {
+		drm_gem_object_reference(&ctx->obj->base);
+		ret = i915_gem_object_pin(ctx->obj, CONTEXT_ALIGN, false);
+		if (ret) {
+			drm_gem_object_unreference(&ctx->obj->base);
+			ctx = NULL;
+		}
+	}
+	spin_unlock(&file_priv->context_idr_lock);
+
+	return ctx;
 }
 
 void i915_release_context(struct drm_i915_gem_context *ctx)
Signature · 1 line
1.7.3.4