From patchwork Mon Feb 20 01:28:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lan Tianyu X-Patchwork-Id: 729685 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3vRR6t3Y13z9s7p for ; Mon, 20 Feb 2017 12:36:21 +1100 (AEDT) Received: from localhost ([::1]:35644 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cfctm-0008KL-95 for incoming@patchwork.ozlabs.org; Sun, 19 Feb 2017 20:36:18 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50104) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cfcsq-0007oE-AU for qemu-devel@nongnu.org; Sun, 19 Feb 2017 20:35:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cfcsl-0007ov-DR for qemu-devel@nongnu.org; Sun, 19 Feb 2017 20:35:20 -0500 Received: from mga06.intel.com ([134.134.136.31]:30207) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cfcsl-0007oT-38 for qemu-devel@nongnu.org; Sun, 19 Feb 2017 20:35:15 -0500 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP; 19 Feb 2017 17:35:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.35,184,1484035200"; d="scan'208"; a="1100005094" Received: from lantianyu-ws.sh.intel.com (HELO localhost) ([10.239.159.159]) by orsmga001.jf.intel.com with ESMTP; 19 Feb 2017 17:35:11 -0800 From: Lan Tianyu To: qemu-devel@nongnu.org Date: Mon, 20 Feb 2017 09:28:04 +0800 Message-Id: <1487554087-15347-2-git-send-email-tianyu.lan@intel.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1487554087-15347-1-git-send-email-tianyu.lan@intel.com> References: <1487554087-15347-1-git-send-email-tianyu.lan@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.31 Subject: [Qemu-devel] [Resend RFC PATCH 1/4] VFIO: Set eventfd for IOMMU fault event via new vfio cmd 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: Lan Tianyu , kevin.tian@intel.com, yi.l.liu@intel.com, mst@redhat.com, jan.kiszka@siemens.com, jasowang@redhat.com, peterx@redhat.com, alex.williamson@redhat.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This patch is to assign an event fd to VFIO IOMMU type1 driver in order to get notification when IOMMU driver reports fault event. Signed-off-by: Lan Tianyu --- hw/vfio/common.c | 37 +++++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-common.h | 3 +++ linux-headers/linux/vfio.h | 13 +++++++++++++ 3 files changed, 53 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 6b33b9f..628b424 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -33,6 +33,7 @@ #include "qemu/error-report.h" #include "qemu/range.h" #include "sysemu/kvm.h" +#include "sysemu/sysemu.h" #include "trace.h" #include "qapi/error.h" @@ -294,6 +295,34 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *section) section->offset_within_address_space & (1ULL << 63); } +static void vfio_iommu_fault(void *opaque) +{ +} + +static int vfio_set_iommu_fault_notifier(struct VFIOContainer *container) +{ + struct vfio_iommu_type1_set_fault_eventfd eventfd; + int ret; + + ret = event_notifier_init(&container->fault_notifier, 0); + if (ret < 0) { + error_report("vfio: Failed to init notifier for IOMMU fault event"); + return ret; + } + + eventfd.fd = event_notifier_get_fd(&container->fault_notifier); + eventfd.argsz = sizeof(eventfd); + + ret = ioctl(container->fd, VFIO_IOMMU_SET_FAULT_EVENTFD, &eventfd); + if (ret < 0) { + error_report("vfio: Failed to set notifier for IOMMU fault event"); + return ret; + } + + qemu_set_fd_handler(eventfd.fd, vfio_iommu_fault, NULL, container); + return 0; +} + /* Called with rcu_read_lock held. */ static bool vfio_get_vaddr(IOMMUTLBEntry *iotlb, void **vaddr, bool *read_only) @@ -1103,6 +1132,14 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, goto listener_release_exit; } + if (memory_region_is_iommu(container->space->as->root)) { + if (vfio_set_iommu_fault_notifier(container)) { + error_setg_errno(errp, -ret, + "Fail to set IOMMU fault notifier"); + goto listener_release_exit; + } + } + container->initialized = true; QLIST_INIT(&container->group_list); diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index c582de1..1b594c6 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -26,6 +26,7 @@ #include "exec/memory.h" #include "qemu/queue.h" #include "qemu/notify.h" +#include "qemu/event_notifier.h" #ifdef CONFIG_LINUX #include #endif @@ -81,6 +82,8 @@ typedef struct VFIOContainer { unsigned iommu_type; int error; bool initialized; + EventNotifier fault_notifier; + /* * This assumes the host IOMMU can support only a single * contiguous IOVA window. We may need to generalize that in diff --git a/linux-headers/linux/vfio.h b/linux-headers/linux/vfio.h index 759b850..ca890ee 100644 --- a/linux-headers/linux/vfio.h +++ b/linux-headers/linux/vfio.h @@ -537,6 +537,19 @@ struct vfio_iommu_type1_dma_unmap { #define VFIO_IOMMU_ENABLE _IO(VFIO_TYPE, VFIO_BASE + 15) #define VFIO_IOMMU_DISABLE _IO(VFIO_TYPE, VFIO_BASE + 16) +/* + * VFIO_IOMMU_SET_FAULT_EVENT_FD _IO(VFIO_TYPE, VFIO_BASE + 17) + * + * Receive eventfd from userspace to notify fault event from IOMMU. + */ +struct vfio_iommu_type1_set_fault_eventfd { + __u32 argsz; + __u32 flags; + __u32 fd; +}; + +#define VFIO_IOMMU_SET_FAULT_EVENTFD _IO(VFIO_TYPE, VFIO_BASE + 17) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*