From patchwork Tue Apr 10 06:02:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Yulei" X-Patchwork-Id: 896494 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=intel.com Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40KxLq0xRyz9s0n for ; Tue, 10 Apr 2018 15:58:59 +1000 (AEST) Received: from localhost ([::1]:42902 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f5mIz-0002xd-6D for incoming@patchwork.ozlabs.org; Tue, 10 Apr 2018 01:58:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54674) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f5mGI-0001Kd-6m for qemu-devel@nongnu.org; Tue, 10 Apr 2018 01:56:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f5mGE-0004yc-Am for qemu-devel@nongnu.org; Tue, 10 Apr 2018 01:56:10 -0400 Received: from mga01.intel.com ([192.55.52.88]:10729) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f5mGE-0004y2-2w for qemu-devel@nongnu.org; Tue, 10 Apr 2018 01:56:06 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Apr 2018 22:56:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,430,1517904000"; d="scan'208";a="44905770" Received: from yzhang13-kvm.sh.intel.com ([10.239.36.9]) by fmsmga004.fm.intel.com with ESMTP; 09 Apr 2018 22:56:03 -0700 From: Yulei Zhang To: qemu-devel@nongnu.org Date: Tue, 10 Apr 2018 14:02:57 +0800 Message-Id: <1523340177-8975-1-git-send-email-yulei.zhang@intel.com> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.88 Subject: [Qemu-devel] [RFC PATCH V4 2/4] vfio: Add vm status change callback to stop/restart the mdev device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, quintela@redhat.com, joonas.lahtinen@linux.intel.com, kwankhede@nvidia.com, zhenyuw@linux.intel.com, dgilbert@redhat.com, Yulei Zhang , alex.williamson@redhat.com, zhi.a.wang@intel.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" VM status change handler is added to change the vfio pci device status during the migration, write the demanded device status to the DEVICE STATUS subregion to stop the device on the source side before fetch its status and start the deivce on the target side after restore its status. Signed-off-by: Yulei Zhang --- hw/vfio/pci.c | 20 ++++++++++++++++++++ include/hw/vfio/vfio-common.h | 1 + linux-headers/linux/vfio.h | 6 ++++++ roms/seabios | 2 +- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index f98a9dd..13d8c73 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -38,6 +38,7 @@ static void vfio_disable_interrupts(VFIOPCIDevice *vdev); static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled); +static void vfio_vm_change_state_handler(void *pv, int running, RunState state); /* * Disabling BAR mmaping can be slow, but toggling it around INTx can @@ -2896,6 +2897,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_err_notifier(vdev); vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + qemu_add_vm_change_state_handler(vfio_vm_change_state_handler, vdev); return; @@ -2982,6 +2984,24 @@ post_reset: vfio_pci_post_reset(vdev); } +static void vfio_vm_change_state_handler(void *pv, int running, RunState state) +{ + VFIOPCIDevice *vdev = pv; + VFIODevice *vbasedev = &vdev->vbasedev; + uint8_t dev_state; + uint8_t sz = 1; + + dev_state = running ? VFIO_DEVICE_START : VFIO_DEVICE_STOP; + + if (pwrite(vdev->vbasedev.fd, &dev_state, + sz, vdev->device_state.offset) != sz) { + error_report("vfio: Failed to %s device", running ? "start" : "stop"); + return; + } + + vbasedev->device_state = dev_state; +} + static void vfio_instance_init(Object *obj) { PCIDevice *pci_dev = PCI_DEVICE(obj); diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index f3a2ac9..9c14a8f 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -125,6 +125,7 @@ typedef struct VFIODevice { unsigned int num_irqs; unsigned int num_regions; unsigned int flags; + bool device_state; } VFIODevice; struct VFIODeviceOps { diff --git a/linux-headers/linux/vfio.h b/linux-headers/linux/vfio.h index e3380ad..8f02f2f 100644 --- a/linux-headers/linux/vfio.h +++ b/linux-headers/linux/vfio.h @@ -304,6 +304,12 @@ struct vfio_region_info_cap_type { /* Mdev sub-type for device state save and restore */ #define VFIO_REGION_SUBTYPE_DEVICE_STATE (4) +/* Offset in region to save device state */ +#define VFIO_DEVICE_STATE_OFFSET 1 + +#define VFIO_DEVICE_START 0 +#define VFIO_DEVICE_STOP 1 + /** * VFIO_DEVICE_GET_IRQ_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 9, * struct vfio_irq_info) diff --git a/roms/seabios b/roms/seabios index 63451fc..5f4c7b1 160000 --- a/roms/seabios +++ b/roms/seabios @@ -1 +1 @@ -Subproject commit 63451fca13c75870e1703eb3e20584d91179aebc +Subproject commit 5f4c7b13cdf9c450eb55645f4362ea58fa61b79b