[Intel-gfx] [RFC 24/29] drm/i915: gvt: Full execlist status emulation

Zhi Wang zhi.a.wang at intel.com
Thu Jan 28 02:21:46 PST 2016


This patch introduces a full execlist status emulation framework.

Under virtulization environment, the execlist status are fully emulated
including virtual CSB emulation, virtual execlist emulation. The framework
will emulate the virtual CSB according to the guest workload running status.

Signed-off-by: Zhi Wang <zhi.a.wang at intel.com>
---
 drivers/gpu/drm/i915/gvt/Makefile   |   2 +-
 drivers/gpu/drm/i915/gvt/debug.h    |   6 +-
 drivers/gpu/drm/i915/gvt/execlist.c | 348 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/gvt/execlist.h | 126 +++++++++++++
 drivers/gpu/drm/i915/gvt/gvt.h      |   2 +
 drivers/gpu/drm/i915/gvt/instance.c |   2 +
 6 files changed, 484 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/gvt/execlist.c
 create mode 100644 drivers/gpu/drm/i915/gvt/execlist.h

diff --git a/drivers/gpu/drm/i915/gvt/Makefile b/drivers/gpu/drm/i915/gvt/Makefile
index 8874fe0..bb3170b 100644
--- a/drivers/gpu/drm/i915/gvt/Makefile
+++ b/drivers/gpu/drm/i915/gvt/Makefile
@@ -1,6 +1,6 @@
 GVT_SOURCE := gvt.o params.o aperture_gm.o mmio.o handlers.o instance.o \
 		trace_points.o interrupt.o gtt.o cfg_space.o opregion.o utility.o \
-		fb_decoder.o display.o edid.o control.o
+		fb_decoder.o display.o edid.o control.o execlist.o
 
 ccflags-y			+= -I$(src) -I$(src)/.. -Wall -Werror -Wno-unused-function
 i915_gvt-y			:= $(GVT_SOURCE)
diff --git a/drivers/gpu/drm/i915/gvt/debug.h b/drivers/gpu/drm/i915/gvt/debug.h
index 807d9e8..88b7d48 100644
--- a/drivers/gpu/drm/i915/gvt/debug.h
+++ b/drivers/gpu/drm/i915/gvt/debug.h
@@ -73,7 +73,8 @@ enum {
 	GVT_DBG_IRQ = (1 << 2),
 	GVT_DBG_DPY = (1 << 3),
 	GVT_DBG_RENDER = (1 << 4),
-	GVT_DBG_EDID = (1 << 5)
+	GVT_DBG_EDID = (1 << 5),
+	GVT_DBG_EL = (1 << 6),
 };
 
 #define gvt_dbg_core(fmt, args...) \
@@ -85,4 +86,7 @@ enum {
 #define gvt_dbg_irq(fmt, args...) \
 	gvt_dbg(GVT_DBG_IRQ, fmt, ##args)
 
+#define gvt_dbg_el(fmt, args...) \
+	gvt_dbg(GVT_DBG_EL, fmt, ##args)
+
 #endif
diff --git a/drivers/gpu/drm/i915/gvt/execlist.c b/drivers/gpu/drm/i915/gvt/execlist.c
new file mode 100644
index 0000000..4d49d00
--- /dev/null
+++ b/drivers/gpu/drm/i915/gvt/execlist.c
@@ -0,0 +1,348 @@
+/*
+ * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
+ *
+ * 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 "gvt.h"
+
+#define execlist_ring_mmio(ring_id, offset) \
+	(gvt_ring_id_to_render_mmio_base(ring_id) + (offset))
+
+#define valid_context(ctx) ((ctx)->valid)
+#define same_context(a, b) (((a)->context_id == (b)->context_id) && \
+		((a)->lrca == (b)->lrca))
+
+static int context_switch_events[] = {
+	[RCS] = RCS_AS_CONTEXT_SWITCH,
+	[BCS] = BCS_AS_CONTEXT_SWITCH,
+	[VCS] = VCS_AS_CONTEXT_SWITCH,
+	[VCS2] = VCS2_AS_CONTEXT_SWITCH,
+	[VECS] = VECS_AS_CONTEXT_SWITCH,
+};
+
+static int ring_id_to_context_switch_event(int ring_id)
+{
+	ASSERT(ring_id >= 0 && ring_id < ARRAY_SIZE(context_switch_events));
+	return context_switch_events[ring_id];
+}
+
+static void switch_virtual_execlist_slot(struct gvt_virtual_execlist_info *info)
+{
+	gvt_dbg_el("[before] running slot %d/context %x pending slot %d",
+		info->running_execlist ? info->running_execlist->index : -1,
+		info->running_context ? info->running_context->context_id : 0,
+		info->pending_execlist ? info->pending_execlist->index : -1);
+
+	info->running_execlist = info->pending_execlist;
+	info->pending_execlist = NULL;
+	info->running_context = info->running_context ?
+		&info->running_execlist->ctx[0] : NULL;
+
+	gvt_dbg_el("[after] running slot %d/context %x pending slot %d",
+		info->running_execlist ? info->running_execlist->index : -1,
+		info->running_context ? info->running_context->context_id : 0,
+		info->pending_execlist ? info->pending_execlist->index : -1);
+}
+
+static void emulate_execlist_status(struct gvt_virtual_execlist_info *info)
+{
+	struct gvt_execlist_state *running = info->running_execlist;
+	struct gvt_execlist_state *pending = info->pending_execlist;
+	struct execlist_ctx_descriptor_format *desc = info->running_context;
+	struct vgt_device *vgt = info->vgt;
+
+	struct execlist_status_format status;
+
+	u32 status_reg = execlist_ring_mmio(info->ring_id, _EL_OFFSET_STATUS);
+
+	status.ldw = __vreg(vgt, status_reg);
+	status.udw = __vreg(vgt, status_reg + 4);
+
+	if (running) {
+		status.current_execlist_pointer = !!running->index;
+		status.execlist_write_pointer = !!!running->index;
+		status.execlist_0_active = status.execlist_0_valid =
+			!!!(running->index);
+		status.execlist_1_active = status.execlist_1_valid =
+			!!(running->index);
+	} else {
+		status.context_id = 0;
+		status.execlist_0_active = status.execlist_0_valid = 0;
+		status.execlist_1_active = status.execlist_1_valid = 0;
+	}
+
+	status.context_id = desc ? desc->context_id : 0;
+	status.execlist_queue_full = !!(pending);
+
+	__vreg(vgt, status_reg) = status.ldw;
+	__vreg(vgt, status_reg + 4) = status.udw;
+
+	gvt_dbg_el("[vgt %d] status reg offset %x ldw %x udw %x",
+		vgt->id, status_reg, status.ldw, status.udw);
+}
+
+static void emulate_csb_update(struct gvt_virtual_execlist_info *info,
+		struct execlist_context_status_format *status)
+{
+	struct vgt_device *vgt = info->vgt;
+	struct execlist_context_status_pointer_format ctx_status_ptr;
+	u32 write_pointer;
+	u32 ctx_status_ptr_reg, ctx_status_buf_reg, offset;
+
+	ctx_status_ptr_reg = execlist_ring_mmio(info->ring_id,
+			_EL_OFFSET_STATUS_PTR);
+	ctx_status_buf_reg = execlist_ring_mmio(info->ring_id,
+			_EL_OFFSET_STATUS_BUF);
+
+	ctx_status_ptr.dw = __vreg(vgt, ctx_status_ptr_reg);
+
+	write_pointer = ctx_status_ptr.write_ptr;
+
+	if (write_pointer == 0x7)
+		write_pointer = 0;
+	else {
+		++write_pointer;
+		write_pointer %= 0x6;
+	}
+
+	offset = ctx_status_buf_reg + write_pointer * 8;
+
+	__vreg(vgt, offset) = status->ldw;
+	__vreg(vgt, offset + 4) = status->udw;
+
+	ctx_status_ptr.write_ptr = write_pointer;
+	__vreg(vgt, ctx_status_ptr_reg) = ctx_status_ptr.dw;
+
+	gvt_dbg_el("[vgt %d] w pointer %u reg %x csb l %x csb h %x",
+		vgt->id, write_pointer, offset, status->ldw, status->udw);
+
+	gvt_trigger_virtual_event(vgt, ring_id_to_context_switch_event(info->ring_id));
+}
+
+static bool emulate_execlist_ctx_schedule_out(struct gvt_virtual_execlist_info *info,
+		struct execlist_ctx_descriptor_format *ctx)
+{
+	struct gvt_execlist_state *running = info->running_execlist;
+	struct gvt_execlist_state *pending = info->pending_execlist;
+	struct execlist_ctx_descriptor_format *ctx0 = &running->ctx[0];
+	struct execlist_ctx_descriptor_format *ctx1 = &running->ctx[1];
+	struct execlist_context_status_format status;
+
+	memset(&status, 0, sizeof(status));
+
+	gvt_dbg_el("schedule out context id %x", ctx->context_id);
+
+	if (!same_context(ctx, info->running_context)) {
+		gvt_err("schedule out context is not running context,"
+				"ctx id %x running ctx id %x",
+				ctx->context_id,
+				info->running_context->context_id);
+		return false;
+	}
+
+	/* ctx1 is valid, ctx0/ctx is scheduled-out -> element switch */
+	if (valid_context(ctx1) && same_context(ctx0, ctx)) {
+		gvt_dbg_el("ctx 1 valid, ctx/ctx 0 is scheduled-out");
+
+		info->running_context = ctx1;
+
+		emulate_execlist_status(info);
+
+		status.context_complete = status.element_switch = 1;
+		status.context_id = ctx->context_id;
+
+		emulate_csb_update(info, &status);
+		/*
+		 * ctx1 is not valid, ctx == ctx0
+		 * ctx1 is valid, ctx1 == ctx
+		 * 	--> last element is finished
+		 * emulate:
+		 * 	active-to-idle if there is *no* pending execlist
+		 * 	context-complete if there *is* pending execlist
+		 */
+	} else if ((!valid_context(ctx1) && same_context(ctx0, ctx))
+			|| (valid_context(ctx1) && same_context(ctx1, ctx))) {
+		gvt_dbg_el("need to switch virtual execlist slot");
+
+		switch_virtual_execlist_slot(info);
+
+		emulate_execlist_status(info);
+
+		if (!pending)
+			status.context_complete = status.active_to_idle = 1;
+		else
+			status.context_complete = 1;
+
+		status.context_id = ctx->context_id;
+
+		emulate_csb_update(info, &status);
+	} else {
+		ASSERT(0);
+		return false;
+	}
+
+	return true;
+}
+
+static struct gvt_execlist_state *get_next_execlist_state(
+		struct gvt_virtual_execlist_info *info)
+{
+	u32 status_reg = execlist_ring_mmio(info->ring_id, _EL_OFFSET_STATUS);
+	struct vgt_device *vgt = info->vgt;
+	struct execlist_status_format status;
+
+	status.ldw = __vreg(vgt, status_reg);
+	status.udw = __vreg(vgt, status_reg + 4);
+
+	if (status.execlist_queue_full) {
+		gvt_err("virtual execlist slots are full");
+		return NULL;
+	}
+
+	return &info->execlist[status.execlist_write_pointer];
+}
+
+static bool emulate_execlist_schedule_in(struct gvt_virtual_execlist_info *info,
+		struct execlist_ctx_descriptor_format ctx[2])
+{
+	struct gvt_execlist_state *running = info->running_execlist;
+	struct gvt_execlist_state *execlist = get_next_execlist_state(info);
+
+	struct execlist_ctx_descriptor_format *ctx0, *ctx1;
+	struct execlist_context_status_format status;
+
+	gvt_dbg_el("emulate schedule-in");
+
+	if (!execlist) {
+		gvt_err("no avaiable execlist slot");
+		return false;
+	}
+
+	memset(&status, 0, sizeof(status));
+	memset(execlist->ctx, 0, sizeof(execlist->ctx));
+
+	execlist->ctx[0] = ctx[0];
+	execlist->ctx[1] = ctx[1];
+
+	gvt_dbg_el("alloc slot index %d ctx 0 %x ctx 1 %x",
+			execlist->index, ctx[0].context_id,
+			ctx[1].context_id);
+
+	/*
+	 * no running execlist, make this write bundle as running execlist
+	 * -> idle-to-active
+	 */
+	if (!running) {
+		gvt_dbg_el("no current running execlist");
+
+		info->running_execlist = execlist;
+		info->pending_execlist = NULL;
+		info->running_context = &execlist->ctx[0];
+
+		gvt_dbg_el("running slot index %d running context %x",
+				info->running_execlist->index,
+				info->running_context->context_id);
+
+		emulate_execlist_status(info);
+
+		status.idle_to_active = 1;
+		status.context_id = 0;
+
+		emulate_csb_update(info, &status);
+		return true;
+	}
+
+	ctx0 = &running->ctx[0];
+	ctx1 = &running->ctx[1];
+
+	gvt_dbg_el("current running execlist index %d ctx 0 %x ctx 1 %x",
+		running->index, ctx0->context_id, ctx1->context_id);
+
+	/*
+	 * already has an running execlist
+	 *	a. running ctx1 is valid,
+	 *	   ctx0 is finished, and running ctx1 == new execlist ctx[0]
+	 *	b. running ctx1 is not valid,
+	 *	   ctx0 == new execlist ctx[0]
+	 * ----> lite-restore + preempted
+	 */
+	if ((valid_context(ctx1) && same_context(ctx1, &execlist->ctx[0]) &&
+		(!same_context(ctx0, info->running_context))) || /* condition a */
+			(!valid_context(ctx1) &&
+			same_context(ctx0, &execlist->ctx[0]))) { /* condition b */
+		gvt_dbg_el("need to switch virtual execlist slot");
+
+		info->pending_execlist = execlist;
+		switch_virtual_execlist_slot(info);
+
+		emulate_execlist_status(info);
+
+		status.lite_restore = status.preempted = 1;
+		status.context_id = ctx0->context_id;
+
+		emulate_csb_update(info, &status);
+	} else {
+		gvt_dbg_el("emulate as pending slot");
+		/*
+		 * otherwise
+		 * --> emulate pending execlist exist + but no preemption case
+		 */
+		info->pending_execlist = execlist;
+		emulate_execlist_status(info);
+	}
+
+	return true;
+}
+
+static bool init_virtual_execlist_info(struct vgt_device *vgt,
+		int ring_id, struct gvt_virtual_execlist_info *info)
+{
+	struct execlist_context_status_pointer_format ctx_status_ptr;
+	u32 ctx_status_ptr_reg;
+
+	memset(info, 0, sizeof(*info));
+
+	info->vgt = vgt;
+	info->ring_id = ring_id;
+	info->execlist[0].index = 0;
+	info->execlist[1].index = 1;
+
+	ctx_status_ptr_reg = execlist_ring_mmio(info->ring_id,
+			_EL_OFFSET_STATUS_PTR);
+
+	ctx_status_ptr.dw = __vreg(vgt, ctx_status_ptr_reg);
+	ctx_status_ptr.read_ptr = ctx_status_ptr.write_ptr = 0x7;
+	__vreg(vgt, ctx_status_ptr_reg) = ctx_status_ptr.dw;
+
+	return true;
+}
+
+bool gvt_init_virtual_execlist_info(struct vgt_device *vgt)
+{
+	int i;
+
+	/* each ring has a virtual execlist engine */
+	for (i = 0; i < I915_NUM_RINGS; i++)
+		init_virtual_execlist_info(vgt,
+				i, &vgt->virtual_execlist_info[i]);
+
+	return true;
+}
diff --git a/drivers/gpu/drm/i915/gvt/execlist.h b/drivers/gpu/drm/i915/gvt/execlist.h
new file mode 100644
index 0000000..bcd5a9e
--- /dev/null
+++ b/drivers/gpu/drm/i915/gvt/execlist.h
@@ -0,0 +1,126 @@
+/*
+ * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
+ *
+ * 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 _GVT_EXECLIST_H_
+#define _GVT_EXECLIST_H_
+
+struct execlist_ctx_descriptor_format {
+	union {
+		u32 udw;
+		u32 context_id;
+	};
+	union {
+		u32 ldw;
+		struct {
+			u32 valid                  : 1;
+			u32 force_pd_restore       : 1;
+			u32 force_restore          : 1;
+			u32 addressing_mode        : 2;
+			u32 llc_coherency          : 1;
+			u32 fault_handling         : 2;
+			u32 privilege_access       : 1;
+			u32 reserved               : 3;
+			u32 lrca                   : 20;
+		};
+	};
+};
+
+struct execlist_status_format {
+        union {
+                u32 ldw;
+                struct {
+                        u32 current_execlist_pointer       :1;
+                        u32 execlist_write_pointer         :1;
+                        u32 execlist_queue_full            :1;
+                        u32 execlist_1_valid               :1;
+                        u32 execlist_0_valid               :1;
+                        u32 last_ctx_switch_reason         :9;
+                        u32 current_active_elm_status      :2;
+                        u32 arbitration_enable             :1;
+                        u32 execlist_1_active              :1;
+                        u32 execlist_0_active              :1;
+                        u32 reserved                       :13;
+                };
+        };
+        union {
+                u32 udw;
+                u32 context_id;
+        };
+};
+
+struct execlist_context_status_pointer_format {
+	union {
+		u32 dw;
+		struct {
+			u32 write_ptr              :3;
+			u32 reserved               :5;
+			u32 read_ptr               :3;
+			u32 reserved2              :5;
+			u32 mask                   :16;
+		};
+	};
+};
+
+struct execlist_context_status_format {
+	union {
+		u32 ldw;
+		struct {
+			u32 idle_to_active         :1;
+			u32 preempted              :1;
+			u32 element_switch         :1;
+			u32 active_to_idle         :1;
+			u32 context_complete       :1;
+			u32 wait_on_sync_flip      :1;
+			u32 wait_on_vblank         :1;
+			u32 wait_on_semaphore      :1;
+			u32 wait_on_scanline       :1;
+			u32 reserved               :2;
+			u32 semaphore_wait_mode    :1;
+			u32 display_plane          :3;
+			u32 lite_restore           :1;
+			u32 reserved_2             :16;
+		};
+	};
+	union {
+		u32 udw;
+		u32 context_id;
+	};
+};
+
+struct gvt_execlist_state {
+	struct execlist_ctx_descriptor_format ctx[2];
+	u32 index;
+};
+
+struct gvt_virtual_execlist_info {
+	struct gvt_execlist_state execlist[2];
+	struct gvt_execlist_state *running_execlist;
+	struct gvt_execlist_state *pending_execlist;
+	struct execlist_ctx_descriptor_format *running_context;
+	int ring_id;
+	struct vgt_device *vgt;
+};
+
+bool gvt_init_virtual_execlist_info(struct vgt_device *vgt);
+
+#endif /*_GVT_EXECLIST_H_*/
diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 1d5c15c..f40788b 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -42,6 +42,7 @@
 #include "fb_decoder.h"
 #include "edid.h"
 #include "display.h"
+#include "execlist.h"
 
 #define GVT_MAX_VGPU 8
 
@@ -149,6 +150,7 @@ struct vgt_device {
 	struct pgt_device *pdev;
 	atomic_t active;
 	struct gvt_virtual_device_state state;
+	struct gvt_virtual_execlist_info virtual_execlist_info[I915_NUM_RINGS];
 	struct gvt_statistics stat;
 	struct gvt_vgtt_info gtt;
 	void *hypervisor_data;
diff --git a/drivers/gpu/drm/i915/gvt/instance.c b/drivers/gpu/drm/i915/gvt/instance.c
index 3015fdf..959c8ee 100644
--- a/drivers/gpu/drm/i915/gvt/instance.c
+++ b/drivers/gpu/drm/i915/gvt/instance.c
@@ -183,6 +183,8 @@ static bool create_virtual_device_state(struct vgt_device *vgt,
 	if (!gvt_init_virtual_display_state(vgt))
 		return false;
 
+	gvt_init_virtual_execlist_info(vgt);
+
 	return true;
 }
 
-- 
1.9.1



More information about the Intel-gfx mailing list