Skip to contentSkip to conversation

VFIO Device states interface in GVT

PATCH In review Yan Zhao 2 participants 10 messages Feb 19, 2019 – Feb 20, 2019

Subsystems (5)
lore
Share & tools
lore

Yan Zhao

author
#0 lore raw

cover VFIO Device states interface in GVT

This patchset provides GVT vGPU with device states control and interfaces to get/set device data.

Desgin of device state control and interfaces to get/set device data
====================================================================

CODE STRUCTURES
---------------
/* Device State region type and sub-type */
#define VFIO_REGION_TYPE_DEVICE_STATE (1 << 1)
#define VFIO_REGION_SUBTYPE_DEVICE_STATE_CTL (1)
#define VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_CONFIG (2)
#define VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_MEMORY (3)
#define VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_DIRTYBITMAP (4)

#define VFIO_DEVICE_STATE_INTERFACE_VERSION 1
#define VFIO_DEVICE_DATA_CAP_DEVICE_MEMORY 1
#define VFIO_DEVICE_DATA_CAP_SYSTEM_MEMORY 2

#define VFIO_DEVICE_STATE_RUNNING 0
#define VFIO_DEVICE_STATE_STOP 1
#define VFIO_DEVICE_STATE_LOGGING 2

#define VFIO_DEVICE_DATA_ACTION_GET_BUFFER 1
#define VFIO_DEVICE_DATA_ACTION_SET_BUFFER 2

struct vfio_device_state_ctl {
	__u32 version;	  /* ro, version of device control interface*/
	__u32 device_state;       /* VFIO device state, wo */
	__u32 caps;		 /* ro */
        struct {
		__u32 action;  /* wo, GET_BUFFER or SET_BUFFER */ 
		__u64 size;    /*rw, total size of device config*/
	} device_config;
	struct {
		__u32 action;    /* wo, GET_BUFFER or SET_BUFFER */ 
		__u64 size;     /* rw, total size of device memory*/  
                __u64 pos;/*chunk offset in total buffer of device memory*/
	} device_memory;
	struct {
		__u64 start_addr; /* wo */
		__u64 page_nr;   /* wo */
	} system_memory;
};

DEVICE DATA
-----------
A VFIO device's data can be divided into 3 categories: device config, device memory and system memory dirty pages.

Device Config: such kind of data like MMIOs, page tables...
Every device is supposed to possess device config data. Usually the size of device config data is small (no big than 10M), and it needs to be loaded in certain strict order.
Therefore no dirty data logging is enabled for device config and it must be got/set as a whole.

Device Memory: device's internal memory, standalone and outside system
memory. It is usually very big.
Not all device has device memory. Like IGD only uses system memory and has no device memory.

System Memory Dirty Pages: A device can produce dirty pages in system
memory.

DEVICE STATE REGIONS
---------------------
A VFIO device driver needs to register two mandatory regions and optionally another two regions if it plans to support device state management.

So, there are up to four regions in total.
one is control region (region CTL) which is accessed via read/write system call from user space;
the left three are data regions which are mmaped into user space and can be accessed in the same way as accessing memory from user space. (If data regions failed to be mmaped into user space, the way of read/write system calls from user space is also valid).

1. region CTL:
Mandatory.
This is a control region.
Its layout is defined in struct vfio_device_state_ctl. Reading from this region can get version, capabilities and data size of device state interfaces.
Writing to this region can set device state, data size and choose which interface to use, i.e, among
"get device config buffer", "set device config buffer", "get device memory buffer", "set device memory buffer", "get system memory dirty bitmap".
2. region DEVICE_CONFIG
Mandatory.
This is a data region that holds device config data. It is able to be mmaped into user space.
3. region DEVICE_MEMORY
Optional.
This is a data region that holds device memory data. It is able to be mmaped into user space.
4. region DIRTY_BITMAP
Optional.
This is a data region that holds bitmap of dirty pages in system memory that a VFIO devices produces.
It is able to be mmaped into user space.

DEVICE STATES
-------------
Four states are defined for a VFIO device:
RUNNING, RUNNING & LOGGING, STOP & LOGGING, STOP.
They can be set by writing to device_state field of vfio_device_state_ctl region.

LOGGING state is a special state that it CANNOT exist independently. It must be set alongside with state RUNNING or STOP, i.e,
RUNNING & LOGGING, STOP & LOGGING

It is used for dirty data logging both for device memory and system memory.

LOGGING only impacts device/system memory.
Device config should be always accessible and return whole config snapshot regardless of LOGGING state.

Typical state transition flows for VFIO devices are:

    (a) RUNNING --> RUNNING & LOGGING --> STOP & LOGGING --> STOP
    (b) RUNNING --> STOP --> RUNNING

RUNNING: In this state, a VFIO device is in active state ready to receive
commands from device driver.
interfaces includes "get device config buffer", "get device config size", "get device memory buffer", "get device memory size" return whole data snapshot.
"get system memory dirty bitmap" returns empty bitmap. It is the default state that a VFIO device enters initially.

STOP: In this state, a VFIO device is deactivated to interact with
device driver.
"get device config buffer", "get device config size" "get device memory buffer", "get device memory size",
return whole data snapshot.
"get system memory dirty bitmap" returns empty bitmap.

RUNNING & LOGGING: In this state, a VFIO device is in active state.
"get device config buffer", "get device config size" returns whole snapshot of device config.
"get device memory buffer", "get device memory size", "get system memory dirty bitmap" returns dirty data since last call to those interfaces.

STOP & LOGGING: In this state, the VFIO device is deactivated.
"get device config buffer", "get device config size" returns whole snapshot of device config.
"get device memory buffer", "get device memory size", "get system memory dirty bitmap" returns dirty data since last call to those interfaces.

Note:
The reason why RUNNING is the default state is that device's active state must not depend on device state interface.
It is possible that region vfio_device_state_ctl fails to got registered. In that condition, a device needs be in active state by default.

DEVICE DATA CAPS
------------------
Device Config capability is by default on, no need to set this cap.

For devices that have devcie memory, it is required to expose DEVICE_MEMORY capability.
#define VFIO_DEVICE_DATA_CAP_DEVICE_MEMORY 1

For devices producing dirty pages in system memory, it is required to expose cap SYSTEM_MEMORY in order to get dirty bitmap in certain range of system memory.
#define VFIO_DEVICE_DATA_CAP_SYSTEM_MEMORY 2

see section "DEVICE STATE INTERFACE" for "get caps" interface to get device data caps from userspace VFIO.

DEVICE STATE INTERFACES
------------------------
1. get version
(1) user space calls read system call on "version" field of region CTL.
(2) VFIO driver writes version number of device state interfaces to the "version" field of region CTL.

2. get caps
(1) user space calls read system call on "caps" field of region CTL.
(2) if a VFIO device has huge device memory, VFIO driver reports VFIO_DEVICE_DATA_CAP_DEVICE_MEMORY in "caps" field of region CTL. if a VFIO device produces dirty pages in system memory, VFIO driver reports VFIO_DEVICE_DATA_CAP_SYSTEM_MEMORY in "caps" field of region CTL.

3. set device state
(1) user space calls write system call on "device_state" field of region CTL.
(2) device state transitions as:

       RUNNING -- start dirty data logging --> RUNNING & LOGGING 
       RUNNING -- deactivate --> STOP
       RUNNING -- deactivate & start dirty data longging --> STOP & LOGGING
       RUNNING & LOGGING -- stop dirty data logging --> RUNNING
       RUNNING & LOGGING -- deactivate --> STOP & LOGGING
       RUNNING & LOGGING -- deactivate & stop dirty data logging --> STOP
       STOP -- activate --> RUNNING
       STOP -- start dirty data logging --> STOP & LOGGING
       STOP -- activate & start dirty data logging --> RUNNING & LOGGING
       STOP & LOGGING -- stop dirty data logging --> STOP
       STOP & LOGGING -- activate --> RUNNING & LOGGING
       STOP & LOGGING -- activate & stop dirty data logging --> RUNNING

4. get device config size
(1) user space calls read system call on "device_config.size" field of region CTL for the total size of device config snapshot.
(2) VFIO driver writes device config data's total size in "device_config.size" field of region CTL.

5. set device config size
(1) user space calls write system call.
total size of device config snapshot --> "device_config.size" field of region CTL.
(2) VFIO driver reads device config data's total size from "device_config.size" field of region CTL.

6 get device config buffer
(1) user space calls write system call.

       "GET_BUFFER" --> "device_config.action" field of region CTL.

(2) VFIO driver
a. gets whole snapshot for device config
b. writes whole device config snapshot to region DEVICE_CONFIG.
(3) user space reads the whole of device config snapshot from region DEVICE_CONFIG.

7. set device config buffer
(1) user space writes whole of device config data to region DEVICE_CONFIG.
(2) user space calls write system call.

       "SET_BUFFER" --> "device_config.action" field of region CTL.

(3) VFIO driver loads whole of device config from region DEVICE_CONFIG.

8. get device memory size
(1) user space calls read system call on "device_memory.size" field of region CTL for device memory size.
(2) VFIO driver
a. gets device memory snapshot (in state RUNNING or STOP), or
gets device memory dirty data (in state RUNNING & LOGGING or state STOP & LOGGING)
b. writes size in "device_memory.size" field of region CTL

9. set device memory size
(1) user space calls write system call on "device_memory.size" field of region CTL to set total size of device memory snapshot.
(2) VFIO driver reads device memory's size from "device_memory.size" field of region CTL.

10. get device memory buffer
(1) user space calls write system.

       pos --> "device_memory.pos" field of region CTL,
       "GET_BUFFER" --> "device_memory.action" field of region CTL.
       (pos must be 0 or multiples of length of region DEVICE_MEMORY).

(2) VFIO driver writes N'th chunk of device memory snapshot/dirty data to region DEVICE_MEMORY.
(N equals to pos/(region length of DEVICE_MEMORY))
(3) user space reads the N'th chunk of device memory snapshot/dirty data from region DEVICE_MEMORY.

11. set device memory buffer
(1) user space writes N'th chunk of device memory snapshot/dirty data to region DEVICE_MEMORY.
(N equals to pos/(region length of DEVICE_MEMORY))
(2) user space writes pos to "device_memory.pos" field and writes "SET_BUFFER" to "device_memory.action" field of region CTL.
(3) VFIO driver loads N'th chunk of device memory snapshot/dirty data from region DEVICE_MEMORY.

12. get system memory dirty bitmap
(1) user space calls write system call to specify a range of system memory that querying dirty pages.
system memory's start address --> "system_memory.start_addr" field of region CTL,
system memory's page count --> "system_memory.page_nr" field of region CTL.
(2) if device state is not in RUNNING or STOP & LOGGING, VFIO driver returns empty bitmap; otherwise,
VFIO driver checks the page_nr,
if it's larger than the size that region DIRTY_BITMAP can support, error returns; if not,
VFIO driver returns as bitmap to specify dirty pages that device produces since last query in this range of system memory .
(3) usespace reads back the dirty bitmap from region DIRTY_BITMAP.

EXAMPLE USAGE
-------------
Take live migration of a VFIO device as an example to use those device state interfaces.

Live migration save path:

(QEMU LIVE MIGRATION STATE --> DEVICE STATE INTERFACE --> DEVICE STATE)

MIGRATION_STATUS_NONE --> VFIO_DEVICE_STATE_RUNNING
|
MIGRATION_STATUS_SAVE_SETUP
|
.save_setup callback -->
get device memory size (whole snapshot size)
get device memory buffer (whole snapshot data)
set device state --> VFIO_DEVICE_STATE_RUNNING & VFIO_DEVICE_STATE_LOGGING |
MIGRATION_STATUS_ACTIVE
|
.save_live_pending callback --> get device memory size (dirty data) .save_live_iteration callback --> get device memory buffer (dirty data) .log_sync callback --> get system memory dirty bitmap
|
(vcpu stops) --> set device state -->
VFIO_DEVICE_STATE_STOP & VFIO_DEVICE_STATE_LOGGING
|
.save_live_complete_precopy callback -->
get device memory size (dirty data)
get device memory buffer (dirty data)
get device config size (whole snapshot size)
get device config buffer (whole snapshot data)
|
.save_cleanup callback --> set device state --> VFIO_DEVICE_STATE_STOP MIGRATION_STATUS_COMPLETED

MIGRATION_STATUS_CANCELLED or
MIGRATION_STATUS_FAILED
|
(vcpu starts) --> set device state --> VFIO_DEVICE_STATE_RUNNING

Live migration load path:

(QEMU LIVE MIGRATION STATE --> DEVICE STATE INTERFACE --> DEVICE STATE)

MIGRATION_STATUS_NONE --> VFIO_DEVICE_STATE_RUNNING
|
(vcpu stops) --> set device state --> VFIO_DEVICE_STATE_STOP
|
MIGRATION_STATUS_ACTIVE
|
.load state callback -->
set device memory size, set device memory buffer, set device config size, set device config buffer
|
(vcpu starts) --> set device state --> VFIO_DEVICE_STATE_RUNNING
|
MIGRATION_STATUS_COMPLETED

Patch Orgnization
=================

The first 6 patches let vGPU view its base ggtt address as starting from
0. Before vGPU submitting workloads to HW, trap vGPU's workloads, scan commands to patch them to start from base address of the ggtt partition assiggned to the vGPU.

The latter two patches implements the VFIO device states interfaces. Patch 7 implements loading device config data from vGPU and restoring device config data into vGPU through GVT's internal interface intel_gvt_save_restore.

Patch 8 exposes device states interfaces to userspace VFIO through VFIO regions of type VFIO_REGION_TYPE_DEVICE_STATE. Through those regions, user space VFIO can get/set device's state and data.

Yan Zhao (2):
drm/i915/gvt: vGPU device config data save/restore interface drm/i915/gvt: VFIO device states interfaces

Yulei Zhang (6):
drm/i915/gvt: Apply g2h adjust for GTT mmio access drm/i915/gvt: Apply g2h adjustment during fence mmio access drm/i915/gvt: Patch the gma in gpu commands during command parser drm/i915/gvt: Retrieve the guest gm base address from PVINFO drm/i915/gvt: Align the guest gm aperture start offset for live
migration
drm/i915/gvt: Apply g2h adjustment to buffer start gma for dmabuf

 drivers/gpu/drm/i915/gvt/Makefile      |   2 +-
 drivers/gpu/drm/i915/gvt/aperture_gm.c |   6 +-
 drivers/gpu/drm/i915/gvt/cfg_space.c   |   3 +-
 drivers/gpu/drm/i915/gvt/cmd_parser.c  |  31 +-
 drivers/gpu/drm/i915/gvt/dmabuf.c      |   3 +
 drivers/gpu/drm/i915/gvt/execlist.c    |   2 +-
 drivers/gpu/drm/i915/gvt/gtt.c         |  25 +-
 drivers/gpu/drm/i915/gvt/gtt.h         |   3 +
 drivers/gpu/drm/i915/gvt/gvt.c         |   1 +
 drivers/gpu/drm/i915/gvt/gvt.h         |  48 +-
 drivers/gpu/drm/i915/gvt/kvmgt.c       | 414 +++++++++++-
 drivers/gpu/drm/i915/gvt/migrate.c     | 863 +++++++++++++++++++++++++
 drivers/gpu/drm/i915/gvt/migrate.h     |  97 +++
 drivers/gpu/drm/i915/gvt/mmio.c        |  13 +
 drivers/gpu/drm/i915/gvt/mmio.h        |   1 +
 drivers/gpu/drm/i915/gvt/vgpu.c        |  11 +-
 include/uapi/linux/vfio.h              |  38 ++
 17 files changed, 1511 insertions(+), 50 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/gvt/migrate.c
 create mode 100644 drivers/gpu/drm/i915/gvt/migrate.h
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 1/8 drm/i915/gvt: Apply g2h adjust for GTT mmio access

From: Yulei Zhang <[email protected]>

Apply guest to host gma conversion while guest try to access the GTT mmio registers, as after enable live migration the host gma will be changed due to the resourece re-allocation, but guest gma should be remaining unchanged, thus g2h conversion is request for it.

---
 drivers/gpu/drm/i915/gvt/gtt.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/gvt/gtt.c +7 -8
index c7103dd2d8d5..8a5d26d1d402 100644
@@ -65,8 +65,7 @@ bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size)
 /* translate a guest gmadr to host gmadr */
 int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr)
 {
-	if (WARN(!vgpu_gmadr_is_valid(vgpu, g_addr),
-		 "invalid guest gmadr %llx\n", g_addr))
+	if (!vgpu_gmadr_is_valid(vgpu, g_addr))
 		return -EACCES;
 
 	if (vgpu_gmadr_is_aperture(vgpu, g_addr))
@@ -2162,7 +2161,8 @@ static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off,
 	struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm;
 	struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops;
 	unsigned long g_gtt_index = off >> info->gtt_entry_size_shift;
-	unsigned long gma, gfn;
+	unsigned long gfn;
+	unsigned long h_gtt_index;
 	struct intel_gvt_gtt_entry e, m;
 	dma_addr_t dma_addr;
 	int ret;
@@ -2172,10 +2172,8 @@ static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off,
 	if (bytes != 4 && bytes != 8)
 		return -EINVAL;
 
-	gma = g_gtt_index << I915_GTT_PAGE_SHIFT;
-
 	/* the VM may configure the whole GM space when ballooning is used */
-	if (!vgpu_gmadr_is_valid(vgpu, gma))
+	if (intel_gvt_ggtt_index_g2h(vgpu, g_gtt_index, &h_gtt_index))
 		return 0;
 
 	e.type = GTT_TYPE_GGTT_PTE;
@@ -2259,11 +2257,12 @@ static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off,
 out:
 	ggtt_set_guest_entry(ggtt_mm, &e, g_gtt_index);
 
-	ggtt_get_host_entry(ggtt_mm, &e, g_gtt_index);
+	ggtt_get_host_entry(ggtt_mm, &e, h_gtt_index);
 	ggtt_invalidate_pte(vgpu, &e);
 
-	ggtt_set_host_entry(ggtt_mm, &m, g_gtt_index);
+	ggtt_set_host_entry(ggtt_mm, &m, h_gtt_index);
 	ggtt_invalidate(gvt->dev_priv);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH 2/8 drm/i915/gvt: Apply g2h adjustment during fence mmio access

In reply to Yan Zhao

From: Yulei Zhang <[email protected]>

Apply the guest to host gma conversion while guest config the fence mmio registers due to the host gma change after the migration.

---
 drivers/gpu/drm/i915/gvt/aperture_gm.c |  6 ++++--
 drivers/gpu/drm/i915/gvt/gvt.h         | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gvt/aperture_gm.c +4 -2
index 359d37d5c958..123c475f2f6e 100644
@@ -146,8 +146,10 @@ void intel_vgpu_write_fence(struct intel_vgpu *vgpu,
 	I915_WRITE(fence_reg_lo, 0);
 	POSTING_READ(fence_reg_lo);
 
-	I915_WRITE(fence_reg_hi, upper_32_bits(value));
-	I915_WRITE(fence_reg_lo, lower_32_bits(value));
+	I915_WRITE(fence_reg_hi,
+		intel_gvt_reg_g2h(vgpu, upper_32_bits(value), 0xFFFFF000));
+	I915_WRITE(fence_reg_lo,
+		intel_gvt_reg_g2h(vgpu, lower_32_bits(value), 0xFFFFF000));
 	POSTING_READ(fence_reg_lo);
 }
 
drivers/gpu/drm/i915/gvt/gvt.h +14 -0
index b4ab1dad0143..8621d0f5fd26 100644
@@ -530,6 +530,20 @@ int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index,
 int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index,
 			     unsigned long *g_index);
 
+/* apply guest to host gma conversion in GM registers setting */
+static inline u64 intel_gvt_reg_g2h(struct intel_vgpu *vgpu,
+		u32 addr, u32 mask)
+{
+	u64 gma;
+
+	if (addr) {
+		intel_gvt_ggtt_gmadr_g2h(vgpu,
+				addr & mask, &gma);
+		addr = gma | (addr & (~mask));
+	}
+	return addr;
+}
+
 void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
 		bool primary);
 void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu);
Signature · 1 line
2.17.1
author
#3 lore raw

PATCH 3/8 drm/i915/gvt: Patch the gma in gpu commands during command parser

In reply to Yan Zhao

From: Yulei Zhang <[email protected]>

Adjust the graphics memory address in gpu commands according to the shift offset in guests' aperture and hidden gm address, and patch the commands before submit to execute.

---
 drivers/gpu/drm/i915/gvt/cmd_parser.c | 31 ++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/gvt/cmd_parser.c +23 -8
index 77ae634eb11c..90836756b235 100644
@@ -984,7 +984,8 @@ static int cmd_handler_lrr(struct parser_exec_state *s)
 }
 
 static inline int cmd_address_audit(struct parser_exec_state *s,
-		unsigned long guest_gma, int op_size, bool index_mode);
+				    unsigned long guest_gma, int op_size,
+				    bool index_mode, int offset);
 
 static int cmd_handler_lrm(struct parser_exec_state *s)
 {
@@ -1006,7 +1007,8 @@ static int cmd_handler_lrm(struct parser_exec_state *s)
 			gma = cmd_gma(s, i + 1);
 			if (gmadr_bytes == 8)
 				gma |= (cmd_gma_hi(s, i + 2)) << 32;
-			ret |= cmd_address_audit(s, gma, sizeof(u32), false);
+			ret |= cmd_address_audit(s, gma, sizeof(u32),
+						 false, i + 1);
 			if (ret)
 				break;
 		}
@@ -1030,7 +1032,8 @@ static int cmd_handler_srm(struct parser_exec_state *s)
 			gma = cmd_gma(s, i + 1);
 			if (gmadr_bytes == 8)
 				gma |= (cmd_gma_hi(s, i + 2)) << 32;
-			ret |= cmd_address_audit(s, gma, sizeof(u32), false);
+			ret |= cmd_address_audit(s, gma, sizeof(u32),
+						 false, i + 1);
 			if (ret)
 				break;
 		}
@@ -1102,7 +1105,7 @@ static int cmd_handler_pipe_control(struct parser_exec_state *s)
 				if (cmd_val(s, 1) & (1 << 21))
 					index_mode = true;
 				ret |= cmd_address_audit(s, gma, sizeof(u64),
-						index_mode);
+						index_mode, 2);
 			}
 		}
 	}
@@ -1432,10 +1435,13 @@ static unsigned long get_gma_bb_from_cmd(struct parser_exec_state *s, int index)
 }
 
 static inline int cmd_address_audit(struct parser_exec_state *s,
-		unsigned long guest_gma, int op_size, bool index_mode)
+				    unsigned long guest_gma, int op_size,
+				    bool index_mode, int offset)
 {
 	struct intel_vgpu *vgpu = s->vgpu;
 	u32 max_surface_size = vgpu->gvt->device_info.max_surface_size;
+	int gmadr_bytes = vgpu->gvt->device_info.gmadr_bytes_in_cmd;
+	u64 host_gma;
 	int i;
 	int ret;
 
@@ -1453,6 +1459,14 @@ static inline int cmd_address_audit(struct parser_exec_state *s,
 	} else if (!intel_gvt_ggtt_validate_range(vgpu, guest_gma, op_size)) {
 		ret = -EFAULT;
 		goto err;
+	} else
+		intel_gvt_ggtt_gmadr_g2h(vgpu, guest_gma, &host_gma);
+
+	if (offset > 0) {
+		patch_value(s, cmd_ptr(s, offset), host_gma & GENMASK(31, 2));
+		if (gmadr_bytes == 8)
+			patch_value(s, cmd_ptr(s, offset + 1),
+				(host_gma >> 32) & GENMASK(15, 0));
 	}
 
 	return 0;
@@ -1497,7 +1511,7 @@ static int cmd_handler_mi_store_data_imm(struct parser_exec_state *s)
 		gma = (gma_high << 32) | gma_low;
 		core_id = (cmd_val(s, 1) & (1 << 0)) ? 1 : 0;
 	}
-	ret = cmd_address_audit(s, gma + op_size * core_id, op_size, false);
+	ret = cmd_address_audit(s, gma + op_size * core_id, op_size, false, 1);
 	return ret;
 }
 
@@ -1541,7 +1555,7 @@ static int cmd_handler_mi_op_2f(struct parser_exec_state *s)
 		gma_high = cmd_val(s, 2) & GENMASK(15, 0);
 		gma = (gma_high << 32) | gma;
 	}
-	ret = cmd_address_audit(s, gma, op_size, false);
+	ret = cmd_address_audit(s, gma, op_size, false, 1);
 	return ret;
 }
 
@@ -1581,7 +1595,8 @@ static int cmd_handler_mi_flush_dw(struct parser_exec_state *s)
 		/* Store Data Index */
 		if (cmd_val(s, 0) & (1 << 21))
 			index_mode = true;
-		ret = cmd_address_audit(s, gma, sizeof(u64), index_mode);
+		ret = cmd_address_audit(s, (gma | (1 << 2)),
+					sizeof(u64), index_mode, 1);
 	}
 	/* Check notify bit */
 	if ((cmd_val(s, 0) & (1 << 8)))
Signature · 1 line
2.17.1
author
#4 lore raw

PATCH 4/8 drm/i915/gvt: Retrieve the guest gm base address from PVINFO

In reply to Yan Zhao

From: Yulei Zhang <[email protected]>

As after migration the host gm base address will be changed due to resource re-allocation, in order to make sure the guest gm address doesn't change with that to retrieve the guest gm base address from PVINFO.

---
 drivers/gpu/drm/i915/gvt/cfg_space.c |  3 ++-
 drivers/gpu/drm/i915/gvt/gtt.c       |  8 ++++----
 drivers/gpu/drm/i915/gvt/gvt.h       | 22 ++++++++++++++++++----
 3 files changed, 24 insertions(+), 9 deletions(-)
drivers/gpu/drm/i915/gvt/cfg_space.c +2 -1
index 19cf1bbe059d..272f8dba7b21 100644
@@ -33,6 +33,7 @@
 
 #include "i915_drv.h"
 #include "gvt.h"
+#include "i915_pvinfo.h"
 
 enum {
 	INTEL_GVT_PCI_BAR_GTTMMIO = 0,
@@ -133,7 +134,7 @@ static int map_aperture(struct intel_vgpu *vgpu, bool map)
 	else
 		val = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
 
-	first_gfn = (val + vgpu_aperture_offset(vgpu)) >> PAGE_SHIFT;
+	first_gfn = (val + vgpu_guest_aperture_offset(vgpu)) >> PAGE_SHIFT;
 
 	ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu, first_gfn,
 						  aperture_pa >> PAGE_SHIFT,
drivers/gpu/drm/i915/gvt/gtt.c +4 -4
index 8a5d26d1d402..753ad975c958 100644
@@ -70,10 +70,10 @@ int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr)
 
 	if (vgpu_gmadr_is_aperture(vgpu, g_addr))
 		*h_addr = vgpu_aperture_gmadr_base(vgpu)
-			  + (g_addr - vgpu_aperture_offset(vgpu));
+			  + (g_addr - vgpu_guest_aperture_gmadr_base(vgpu));
 	else
 		*h_addr = vgpu_hidden_gmadr_base(vgpu)
-			  + (g_addr - vgpu_hidden_offset(vgpu));
+			  + (g_addr - vgpu_guest_hidden_gmadr_base(vgpu));
 	return 0;
 }
 
@@ -85,10 +85,10 @@ int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr)
 		return -EACCES;
 
 	if (gvt_gmadr_is_aperture(vgpu->gvt, h_addr))
-		*g_addr = vgpu_aperture_gmadr_base(vgpu)
+		*g_addr = vgpu_guest_aperture_gmadr_base(vgpu)
 			+ (h_addr - gvt_aperture_gmadr_base(vgpu->gvt));
 	else
-		*g_addr = vgpu_hidden_gmadr_base(vgpu)
+		*g_addr = vgpu_guest_hidden_gmadr_base(vgpu)
 			+ (h_addr - gvt_hidden_gmadr_base(vgpu->gvt));
 	return 0;
 }
drivers/gpu/drm/i915/gvt/gvt.h +18 -4
index 8621d0f5fd26..1f5ef59a36ac 100644
@@ -424,6 +424,20 @@ int intel_gvt_load_firmware(struct intel_gvt *gvt);
 #define vgpu_fence_base(vgpu) (vgpu->fence.base)
 #define vgpu_fence_sz(vgpu) (vgpu->fence.size)
 
+/* Aperture/GM space definitions for vGPU Guest view point */
+#define vgpu_guest_aperture_offset(vgpu) \
+	vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base))
+#define vgpu_guest_hidden_offset(vgpu)	\
+	vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.base))
+
+#define vgpu_guest_aperture_gmadr_base(vgpu) (vgpu_guest_aperture_offset(vgpu))
+#define vgpu_guest_aperture_gmadr_end(vgpu) \
+	(vgpu_guest_aperture_gmadr_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
+
+#define vgpu_guest_hidden_gmadr_base(vgpu) (vgpu_guest_hidden_offset(vgpu))
+#define vgpu_guest_hidden_gmadr_end(vgpu) \
+	(vgpu_guest_hidden_gmadr_base(vgpu) + vgpu_hidden_sz(vgpu) - 1)
+
 struct intel_vgpu_creation_params {
 	__u64 handle;
 	__u64 low_gm_sz;  /* in MB */
@@ -499,12 +513,12 @@ void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu);
 
 /* validating GM functions */
 #define vgpu_gmadr_is_aperture(vgpu, gmadr) \
-	((gmadr >= vgpu_aperture_gmadr_base(vgpu)) && \
-	 (gmadr <= vgpu_aperture_gmadr_end(vgpu)))
+	((gmadr >= vgpu_guest_aperture_gmadr_base(vgpu)) && \
+	 (gmadr <= vgpu_guest_aperture_gmadr_end(vgpu)))
 
 #define vgpu_gmadr_is_hidden(vgpu, gmadr) \
-	((gmadr >= vgpu_hidden_gmadr_base(vgpu)) && \
-	 (gmadr <= vgpu_hidden_gmadr_end(vgpu)))
+	((gmadr >= vgpu_guest_hidden_gmadr_base(vgpu)) && \
+	 (gmadr <= vgpu_guest_hidden_gmadr_end(vgpu)))
 
 #define vgpu_gmadr_is_valid(vgpu, gmadr) \
 	 ((vgpu_gmadr_is_aperture(vgpu, gmadr) || \
Signature · 1 line
2.17.1
author
#5 lore raw

PATCH 5/8 drm/i915/gvt: Align the guest gm aperture start offset for live migration

In reply to Yan Zhao

From: Yulei Zhang <[email protected]>

As guest gm aperture region start offset is initialized when vGPU created, in order to make sure that start offset is remain the same after migration, align the aperture start offset to 0 for guest.

---
 drivers/gpu/drm/i915/gvt/kvmgt.c |  3 +--
 drivers/gpu/drm/i915/gvt/vgpu.c  | 10 ++++++++--
 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gvt/kvmgt.c +1 -2
index c1072143da1d..223c67e87680 100644
@@ -1198,8 +1198,7 @@ static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
 			sparse->header.version = 1;
 			sparse->nr_areas = nr_areas;
 			cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
-			sparse->areas[0].offset =
-					PAGE_ALIGN(vgpu_aperture_offset(vgpu));
+			sparse->areas[0].offset = 0;
 			sparse->areas[0].size = vgpu_aperture_sz(vgpu);
 			break;
 
drivers/gpu/drm/i915/gvt/vgpu.c +8 -2
index c628be05fbfe..fcccda35a456 100644
@@ -48,8 +48,7 @@ void populate_pvinfo_page(struct intel_vgpu *vgpu)
 	vgpu_vreg_t(vgpu, vgtif_reg(vgt_caps)) |= VGT_CAPS_HWSP_EMULATION;
 	vgpu_vreg_t(vgpu, vgtif_reg(vgt_caps)) |= VGT_CAPS_HUGE_GTT;
 
-	vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base)) =
-		vgpu_aperture_gmadr_base(vgpu);
+	vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base)) = 0;
 	vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.mappable_gmadr.size)) =
 		vgpu_aperture_sz(vgpu);
 	vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.base)) =
@@ -524,6 +523,9 @@ void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
 {
 	struct intel_gvt *gvt = vgpu->gvt;
 	struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
+	u64 maddr = vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base));
+	u64 unmaddr = vgpu_vreg_t(vgpu,
+				vgtif_reg(avail_rs.nonmappable_gmadr.base));
 	unsigned int resetting_eng = dmlr ? ALL_ENGINES : engine_mask;
 
 	gvt_dbg_core("------------------------------------------\n");
@@ -556,6 +558,10 @@ void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
 
 		intel_vgpu_reset_mmio(vgpu, dmlr);
 		populate_pvinfo_page(vgpu);
+		vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base)) =
+			maddr;
+		vgpu_vreg_t(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.base)) =
+			unmaddr;
 		intel_vgpu_reset_display(vgpu);
 
 		if (dmlr) {
Signature · 1 line
2.17.1
author
#6 lore raw

PATCH 6/8 drm/i915/gvt: Apply g2h adjustment to buffer start gma for dmabuf

In reply to Yan Zhao

From: Yulei Zhang <[email protected]>

Adjust the buffer start gma in dmabuf for display in host domain.

---
 drivers/gpu/drm/i915/gvt/dmabuf.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/i915/gvt/dmabuf.c +3 -0
index 51ed99a37803..e96c655c40bc 100644
@@ -293,6 +293,9 @@ static int vgpu_get_plane_info(struct drm_device *dev,
 		return -EFAULT;
 	}
 
+	/* Apply g2h adjust to buffer start gma for display */
+	intel_gvt_ggtt_gmadr_g2h(vgpu, info->start, &info->start);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
author
#7 lore raw

PATCH 7/8 drm/i915/gvt: vGPU device config data save/restore interface

In reply to Yan Zhao

The patch implments the gvt interface intel_gvt_save_restore to save/restore vGPU's device config data for live migration.

vGPU device config data includes vreg, vggtt, vcfg space, workloads, ppgtt, execlist.
It does not include dirty pages in system memory produced by vGPU.

---
 drivers/gpu/drm/i915/gvt/Makefile   |   2 +-
 drivers/gpu/drm/i915/gvt/execlist.c |   2 +-
 drivers/gpu/drm/i915/gvt/gtt.c      |   2 +-
 drivers/gpu/drm/i915/gvt/gtt.h      |   3 +
 drivers/gpu/drm/i915/gvt/gvt.c      |   1 +
 drivers/gpu/drm/i915/gvt/gvt.h      |   9 +
 drivers/gpu/drm/i915/gvt/migrate.c  | 863 ++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/gvt/migrate.h  |  97 ++++
 drivers/gpu/drm/i915/gvt/mmio.c     |  13 +
 drivers/gpu/drm/i915/gvt/mmio.h     |   1 +
 drivers/gpu/drm/i915/gvt/vgpu.c     |   1 +
 11 files changed, 991 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/gvt/migrate.c
 create mode 100644 drivers/gpu/drm/i915/gvt/migrate.h
drivers/gpu/drm/i915/gvt/Makefile +1 -1
index b016dc753db9..f863fbed1792 100644
@@ -3,7 +3,7 @@ GVT_DIR := gvt
 GVT_SOURCE := gvt.o aperture_gm.o handlers.o vgpu.o trace_points.o firmware.o \
 	interrupt.o gtt.o cfg_space.o opregion.o mmio.o display.o edid.o \
 	execlist.o scheduler.o sched_policy.o mmio_context.o cmd_parser.o debugfs.o \
-	fb_decoder.o dmabuf.o page_track.o
+	fb_decoder.o dmabuf.o page_track.o migrate.o
 
 ccflags-y				+= -I$(src) -I$(src)/$(GVT_DIR)
 i915-y					+= $(addprefix $(GVT_DIR)/, $(GVT_SOURCE))
drivers/gpu/drm/i915/gvt/execlist.c +1 -1
index 70494e394d2c..992e2260eec9 100644
@@ -437,7 +437,7 @@ static int complete_execlist_workload(struct intel_vgpu_workload *workload)
 	return ret;
 }
 
-static int submit_context(struct intel_vgpu *vgpu, int ring_id,
+int submit_context(struct intel_vgpu *vgpu, int ring_id,
 		struct execlist_ctx_descriptor_format *desc,
 		bool emulate_schedule_in)
 {
drivers/gpu/drm/i915/gvt/gtt.c +1 -1
index 753ad975c958..18e3f08b0553 100644
@@ -589,7 +589,7 @@ static inline void ppgtt_set_shadow_root_entry(struct intel_vgpu_mm *mm,
 	_ppgtt_set_root_entry(mm, entry, index, false);
 }
 
-static void ggtt_get_guest_entry(struct intel_vgpu_mm *mm,
+void ggtt_get_guest_entry(struct intel_vgpu_mm *mm,
 		struct intel_gvt_gtt_entry *entry, unsigned long index)
 {
 	struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops;
drivers/gpu/drm/i915/gvt/gtt.h +3 -0
index d8cb04cc946d..73709768b666 100644
@@ -270,6 +270,9 @@ struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
 
 int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[]);
 
+void ggtt_get_guest_entry(struct intel_vgpu_mm *mm,
+		struct intel_gvt_gtt_entry *entry, unsigned long index);
+
 int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
 	unsigned int off, void *p_data, unsigned int bytes);
 
drivers/gpu/drm/i915/gvt/gvt.c +1 -0
index 733a2a0d0c30..3dd9e4ebd39b 100644
@@ -185,6 +185,7 @@ static const struct intel_gvt_ops intel_gvt_ops = {
 	.vgpu_query_plane = intel_vgpu_query_plane,
 	.vgpu_get_dmabuf = intel_vgpu_get_dmabuf,
 	.write_protect_handler = intel_vgpu_page_track_handler,
+	.vgpu_save_restore = intel_gvt_save_restore,
 };
 
 /**
drivers/gpu/drm/i915/gvt/gvt.h +9 -0
index 1f5ef59a36ac..cfde510e9d77 100644
@@ -46,6 +46,7 @@
 #include "sched_policy.h"
 #include "mmio_context.h"
 #include "cmd_parser.h"
+#include "migrate.h"
 #include "fb_decoder.h"
 #include "dmabuf.h"
 #include "page_track.h"
@@ -510,6 +511,8 @@ void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
 void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu);
 void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu);
 void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu);
+int intel_gvt_save_restore(struct intel_vgpu *vgpu, char *buf,
+		size_t count, void *base, uint64_t off, bool restore);
 
 /* validating GM functions */
 #define vgpu_gmadr_is_aperture(vgpu, gmadr) \
@@ -609,6 +612,9 @@ struct intel_gvt_ops {
 	int (*vgpu_get_dmabuf)(struct intel_vgpu *vgpu, unsigned int);
 	int (*write_protect_handler)(struct intel_vgpu *, u64, void *,
 				     unsigned int);
+	int (*vgpu_save_restore)(struct intel_vgpu *vgpu, char *buf,
+					size_t count, void *base,
+					uint64_t off, bool restore);
 };
 
 
@@ -722,6 +728,9 @@ int intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu);
 void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu);
 int intel_gvt_debugfs_init(struct intel_gvt *gvt);
 void intel_gvt_debugfs_clean(struct intel_gvt *gvt);
+int submit_context(struct intel_vgpu *vgpu, int ring_id,
+		struct execlist_ctx_descriptor_format *desc,
+		bool emulate_schedule_in);
 
 
 #include "trace.h"
drivers/gpu/drm/i915/gvt/migrate.c new file +863 -0

863 lines not on this page · raw message

drivers/gpu/drm/i915/gvt/migrate.h new file +97 -0

97 lines not on this page · raw message

drivers/gpu/drm/i915/gvt/mmio.c +13 -0
index 43f65848ecd6..6221d2f274fc 100644
@@ -50,6 +50,19 @@ int intel_vgpu_gpa_to_mmio_offset(struct intel_vgpu *vgpu, u64 gpa)
 	return gpa - gttmmio_gpa;
 }
 
+/**
+ * intel_vgpu_mmio_offset_to_GPA - translate a MMIO offset to GPA
+ * @vgpu: a vGPU
+ *
+ * Returns:
+ * Zero on success, negative error code if failed
+ */
+int intel_vgpu_mmio_offset_to_gpa(struct intel_vgpu *vgpu, u64 offset)
+{
+	return offset + ((*(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0)) &
+		~GENMASK(3, 0));
+}
+
 #define reg_is_mmio(gvt, reg)  \
 	(reg >= 0 && reg < gvt->device_info.mmio_size)
 
drivers/gpu/drm/i915/gvt/mmio.h +1 -0
index 1ffc69eba30e..a2bddb0257cf 100644
@@ -82,6 +82,7 @@ void intel_vgpu_reset_mmio(struct intel_vgpu *vgpu, bool dmlr);
 void intel_vgpu_clean_mmio(struct intel_vgpu *vgpu);
 
 int intel_vgpu_gpa_to_mmio_offset(struct intel_vgpu *vgpu, u64 gpa);
+int intel_vgpu_mmio_offset_to_gpa(struct intel_vgpu *vgpu, u64 offset);
 
 int intel_vgpu_emulate_mmio_read(struct intel_vgpu *vgpu, u64 pa,
 				void *p_data, unsigned int bytes);
drivers/gpu/drm/i915/gvt/vgpu.c +1 -0
index fcccda35a456..7676dcfdca09 100644
@@ -213,6 +213,7 @@ void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu)
 {
 	mutex_lock(&vgpu->gvt->lock);
 	vgpu->active = true;
+	intel_vgpu_start_schedule(vgpu);
 	mutex_unlock(&vgpu->gvt->lock);
 }
 
Signature · 1 line
2.17.1
#8 lore raw

Re: [PATCH 7/8] drm/i915/gvt: vGPU device config data save/restore interface

Quoted 11 lines · Yan Zhao

The patch implments the gvt interface intel_gvt_save_restore to save/restore vGPU's device config data for live migration.

vGPU device config data includes vreg, vggtt, vcfg space, workloads, ppgtt, execlist.
It does not include dirty pages in system memory produced by vGPU.

...

Quoted 59 lines
drivers/gpu/drm/i915/gvt/migrate.h new file +59 -0
+
+#ifndef __GVT_MIGRATE_H__
+#define __GVT_MIGRATE_H__
+
+#define MIGRATION_DIRTY_BITMAP_SIZE (16*1024UL)
+
+/* Assume 9MB is enough to descript VM kernel state */
+#define MIGRATION_IMG_MAX_SIZE (9*1024UL*1024UL)
+#define GVT_MMIO_SIZE (2*1024UL*1024UL)
+#define GVT_MIGRATION_VERSION	0
+
+enum gvt_migration_type_t {
+	GVT_MIGRATION_NONE,
+	GVT_MIGRATION_HEAD,
+	GVT_MIGRATION_CFG_SPACE,
+	GVT_MIGRATION_VREG,
+	GVT_MIGRATION_SREG,
+	GVT_MIGRATION_GTT,
+	GVT_MIGRATION_PPGTT,
+	GVT_MIGRATION_WORKLOAD,
+	GVT_MIGRATION_EXECLIST,
+};
+
+struct gvt_ppgtt_entry_t {
+	int page_table_level;
+	u64 pdp[4];
+};
+
+struct gvt_pending_workload_t {
+	int ring_id;
+	bool emulate_schedule_in;
+	struct execlist_ctx_descriptor_format ctx_desc;
+	struct intel_vgpu_elsp_dwords elsp_dwords;
+};
+
+struct gvt_region_t {
+	enum gvt_migration_type_t type;
+	u32 size;		/* obj size of bytes to read/write */
+};
+
+struct gvt_migration_obj_t {
+	void *img;
+	void *vgpu;
+	u32 offset;
+	struct gvt_region_t region;
+	/* operation func defines how data save-restore */
+	struct gvt_migration_operation_t *ops;
+	char *name;
+};
+
+struct gvt_migration_operation_t {
+	/* called during pre-copy stage, VM is still alive */
+	int (*pre_copy)(const struct gvt_migration_obj_t *obj);
+	/* called before when VM was paused,
+	 * return bytes transferred
+	 */
+	int (*pre_save)(const struct gvt_migration_obj_t *obj);
+	/* called before load the state of device */
+	int (*pre_load)(const struct gvt_migration_obj_t *obj, u32 size);

drivers/gpu/drm/i915/gvt/migrate.h line 95

Quoted 10 lines
drivers/gpu/drm/i915/gvt/migrate.h new file +10 -0
+	/* called after load the state of device, VM already alive */
+	int (*post_load)(const struct gvt_migration_obj_t *obj, u32 size);
+};
+
+struct gvt_image_header_t {
+	int version;
+	int data_size;
+	u64 crc_check;
+	u64 global_data[64];
+};

I think this misses device info that should ship with the image, currently what I can think is that each platform should have seperate type, e.g BDW, SKL, KBL, etc. We won't allow to restore onto different platform than the source.

Quoted 57 lines
drivers/gpu/drm/i915/gvt/migrate.h new file +2 -0
+
+#endif
drivers/gpu/drm/i915/gvt/mmio.c +13 -0
index 43f65848ecd6..6221d2f274fc 100644
@@ -50,6 +50,19 @@ int intel_vgpu_gpa_to_mmio_offset(struct intel_vgpu *vgpu, u64 gpa)
 	return gpa - gttmmio_gpa;
 }
 
+/**
+ * intel_vgpu_mmio_offset_to_GPA - translate a MMIO offset to GPA
+ * @vgpu: a vGPU
+ *
+ * Returns:
+ * Zero on success, negative error code if failed
+ */
+int intel_vgpu_mmio_offset_to_gpa(struct intel_vgpu *vgpu, u64 offset)
+{
+	return offset + ((*(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0)) &
+		~GENMASK(3, 0));
+}
+
 #define reg_is_mmio(gvt, reg)  \
 	(reg >= 0 && reg < gvt->device_info.mmio_size)
 
drivers/gpu/drm/i915/gvt/mmio.h +1 -0
index 1ffc69eba30e..a2bddb0257cf 100644
@@ -82,6 +82,7 @@ void intel_vgpu_reset_mmio(struct intel_vgpu *vgpu, bool dmlr);
 void intel_vgpu_clean_mmio(struct intel_vgpu *vgpu);
 
 int intel_vgpu_gpa_to_mmio_offset(struct intel_vgpu *vgpu, u64 gpa);
+int intel_vgpu_mmio_offset_to_gpa(struct intel_vgpu *vgpu, u64 offset);
 
 int intel_vgpu_emulate_mmio_read(struct intel_vgpu *vgpu, u64 pa,
 				void *p_data, unsigned int bytes);
drivers/gpu/drm/i915/gvt/vgpu.c +1 -0
index fcccda35a456..7676dcfdca09 100644
@@ -213,6 +213,7 @@ void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu)
 {
 	mutex_lock(&vgpu->gvt->lock);
 	vgpu->active = true;
+	intel_vgpu_start_schedule(vgpu);
 	mutex_unlock(&vgpu->gvt->lock);
 }
 
Signature · 6 lines
2.17.1

_______________________________________________
intel-gvt-dev mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gvt-dev
Signature · 3 lines
Open Source Technology Center, Intel ltd.

$gpg --keyserver wwwkeys.pgp.net --recv-keys 4D781827
author
#9 lore raw

PATCH 8/8 drm/i915/gvt: VFIO device states interfaces

In reply to Yan Zhao

This patch registers 3 VFIO device state regiones of type VFIO_REGION_TYPE_DEVICE_STATE, and subtype VFIO_REGION_SUBTYPE_DEVICE_STATE_CTL, VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_CONFIG, VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_DIRTYBITMAP.

userspace VFIO will check the existence of those regions to get/set vGPU's device states.

region of subtype VFIO_REGION_SUBTYPE_DEVICE_STATE_CTL is the control region, its layout is defined in struct vfio_device_state_ctl. Reading from userspace into this region will get device state interace's version and device data caps.

As Intel vGPU does not have device memory, so it does not support cap VFIO_DEVICE_DATA_CAP_DEVICE_MEMORY.

But Intel vGPU will produce dirty page in system memory, cap VFIO_DEVICE_DATA_CAP_SYSTEM_MEMORY is reported.

through writing to the control region, vGPU's state can also be set to one of VFIO_DEVICE_STATE_RUNNING, VFIO_DEVICE_STATE_STOP, VFIO_DEVICE_STATE_RUNNING & VFIO_DEVICE_STATE_LOGGING, VFIO_DEVICE_STATE_STOP & VFIO_DEVICE_STATE_LOGGING.
state VFIO_DEVICE_STATE_LOGGING is set to notify logging dirty page in system memory, but since vGPU's dirty page logging now is implemented by cache of dma pages for guest gfns in vggtt and ppgtt, nothing special needs to be done in the two LOGGING states, like start/stop logging threads...

vGPU's device config data (including vreg, vggtt, vcfg space, workloads, ppgtt, execlist, which are saved/restored through gvt interface intel_gvt_save_restore) is hold in region of subtype VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_CONFIG.
This region is mmaped into userspace VFIO.

Therefore userspace VFIO's reading from this config data region requires it first write GET_BUFFER to device_config.action in the above control region, so that GVT can load config data of vGPU into config data region first;
And after userspace VFIO's writing to config data region, SET_BUFFER is also needed to write to device_config.action in control region, so GVT can restore config data into vGPU.
(Also, if device config data region failed to be mmaped into userspace VFIO, read/write handlers are also provided).

vGPU's region for dirty bitmap logging in system memory is of subtype VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_DIRTYBITMAP. It's also mmaped into userspace VFIO. By writing start_addr and page count of a range of system memory, dirty pages' bitmap produced by vGPU is saved in this region dirty bitmap. Userspace VFIO can directly read dirty bitmap from mmaped region or through this region's read/write handlers.

---
 drivers/gpu/drm/i915/gvt/gvt.h   |   3 +
 drivers/gpu/drm/i915/gvt/kvmgt.c | 412 +++++++++++++++++++++++++++++--
 include/uapi/linux/vfio.h        |  38 +++
 3 files changed, 437 insertions(+), 16 deletions(-)
drivers/gpu/drm/i915/gvt/gvt.h +3 -0
index cfde510e9d77..b0580169f595 100644
@@ -227,6 +227,9 @@ struct intel_vgpu {
 		struct work_struct release_work;
 		atomic_t released;
 		struct vfio_device *vfio_device;
+		struct vfio_device_state_ctl *state_ctl;
+		void *state_config;
+		void *state_bitmap;
 	} vdev;
 #endif
 
drivers/gpu/drm/i915/gvt/kvmgt.c +396 -16

486 lines not on this page · raw message

include/uapi/linux/vfio.h +38 -0
index 813102810f53..a577b242e3bd 100644
@@ -303,6 +303,14 @@ struct vfio_region_info_cap_type {
 #define VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG	(2)
 #define VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG	(3)
 
+
+/* Device State region type and sub-type */
+#define VFIO_REGION_TYPE_DEVICE_STATE           (1 << 1)
+#define VFIO_REGION_SUBTYPE_DEVICE_STATE_CTL       (1)
+#define VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_CONFIG      (2)
+#define VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_MEMORY      (3)
+#define VFIO_REGION_SUBTYPE_DEVICE_STATE_DATA_DIRTYBITMAP (4)
+
 #define VFIO_REGION_TYPE_GFX                    (1)
 #define VFIO_REGION_SUBTYPE_GFX_EDID            (1)
 
@@ -866,6 +874,36 @@ struct vfio_iommu_spapr_tce_remove {
 };
 #define VFIO_IOMMU_SPAPR_TCE_REMOVE	_IO(VFIO_TYPE, VFIO_BASE + 20)
 
+#define VFIO_DEVICE_STATE_INTERFACE_VERSION 1
+#define VFIO_DEVICE_DATA_CAP_DEVICE_MEMORY 1
+#define VFIO_DEVICE_DATA_CAP_SYSTEM_MEMORY 2
+
+#define VFIO_DEVICE_STATE_RUNNING 0
+#define VFIO_DEVICE_STATE_STOP 1
+#define VFIO_DEVICE_STATE_LOGGING 2
+
+#define VFIO_DEVICE_DATA_ACTION_GET_BUFFER 1
+#define VFIO_DEVICE_DATA_ACTION_SET_BUFFER 2
+
+struct vfio_device_state_ctl {
+	__u32 version;		  /* ro */
+	__u32 device_state;       /* VFIO device state, wo */
+	__u32 caps;		 /* ro */
+	struct {
+		__u32 action;  /* wo, GET_BUFFER or SET_BUFFER */
+		__u64 size;    /*rw, total size of device config*/
+	} device_config;
+	struct {
+		__u32 action;    /* wo, GET_BUFFER or SET_BUFFER */
+		__u64 size;     /* rw, total size of device memory*/
+		__u64 pos;/*chunk offset in total buffer of device memory*/
+	} device_memory;
+	struct {
+		__u64 start_addr; /* wo */
+		__u64 page_nr;   /* wo */
+	} system_memory;
+} __attribute__((packed));
+
 /* ***************************************************************** */
 
 #endif /* _UAPIVFIO_H */
Signature · 1 line
2.17.1