From patchwork Mon Feb 20 01:28:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "lan,Tianyu" X-Patchwork-Id: 9581917 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 18DC360578 for ; Mon, 20 Feb 2017 01:35:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E6082883F for ; Mon, 20 Feb 2017 01:35:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0192028842; Mon, 20 Feb 2017 01:35:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 925D12883F for ; Mon, 20 Feb 2017 01:35:52 +0000 (UTC) Received: from localhost ([::1]:35643 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cfctL-0007rb-KX for patchwork-qemu-devel@patchwork.kernel.org; Sun, 19 Feb 2017 20:35:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50135) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cfcsu-0007pt-V3 for qemu-devel@nongnu.org; Sun, 19 Feb 2017 20:35:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cfcst-0007sD-RP for qemu-devel@nongnu.org; Sun, 19 Feb 2017 20:35:24 -0500 Received: from mga09.intel.com ([134.134.136.24]:43564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cfcst-0007s1-Il for qemu-devel@nongnu.org; Sun, 19 Feb 2017 20:35:23 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Feb 2017 17:35:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,184,1484035200"; d="scan'208";a="50041807" Received: from lantianyu-ws.sh.intel.com (HELO localhost) ([10.239.159.159]) by orsmga002.jf.intel.com with ESMTP; 19 Feb 2017 17:35:20 -0800 From: Lan Tianyu To: qemu-devel@nongnu.org Date: Mon, 20 Feb 2017 09:28:07 +0800 Message-Id: <1487554087-15347-5-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.24 Subject: [Qemu-devel] [Resend RFC PATCH 4/4] VFIO: Read IOMMU fault info from kernel space when get fault event 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch is to implement fault event handler with new vfio cmd to get fault info and notify vIOMMU device model. Signed-off-by: Lan Tianyu --- hw/vfio/common.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ linux-headers/linux/vfio.h | 22 ++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 628b424..4f76e26 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -297,6 +297,57 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *section) static void vfio_iommu_fault(void *opaque) { + VFIOContainer *container = opaque; + struct vfio_iommu_type1_get_fault_info *info; + struct vfio_iommu_fault_info *fault_info; + MemoryRegion *mr = container->space->as->root; + int count = 0, i, ret; + IOMMUFaultInfo tmp; + + if (!event_notifier_test_and_clear(&container->fault_notifier)) { + return; + } + + info = g_malloc0(sizeof(*info)); + if (!info) { + error_report("vfio: Fail to allocate memory"); + return; + } + + info->argsz = sizeof(*info); + + ret = ioctl(container->fd, VFIO_IOMMU_GET_FAULT_INFO, info); + if (ret && ret != -ENOSPC) { + error_report("vfio: Can't get fault info"); + goto err_exit; + } + + count = info->count; + if (count <= 0) { + goto err_exit; + } + + info = g_realloc(info, sizeof(*info) + count * sizeof(*fault_info)); + info->argsz = sizeof(*info) + count * sizeof(*fault_info); + fault_info = info->fault_info; + + ret = ioctl(container->fd, VFIO_IOMMU_GET_FAULT_INFO, info); + if (ret) { + error_report("vfio: Can't get fault info"); + goto err_exit; + } + + for (i = 0; i < info->count; i++) { + tmp.addr = fault_info[i].addr; + tmp.sid = fault_info[i].sid; + tmp.fault_reason = fault_info[i].fault_reason; + tmp.is_write = fault_info[i].is_write; + + memory_region_iommu_fault_notify(mr, &tmp); + } + +err_exit: + g_free(info); } static int vfio_set_iommu_fault_notifier(struct VFIOContainer *container) diff --git a/linux-headers/linux/vfio.h b/linux-headers/linux/vfio.h index ca890ee..8b172f5 100644 --- a/linux-headers/linux/vfio.h +++ b/linux-headers/linux/vfio.h @@ -550,6 +550,28 @@ struct vfio_iommu_type1_set_fault_eventfd { #define VFIO_IOMMU_SET_FAULT_EVENTFD _IO(VFIO_TYPE, VFIO_BASE + 17) +/* + * VFIO_IOMMU_GET_FAULT_INFO _IO(VFIO_TYPE, VFIO_BASE + 18) + * + * Return IOMMU fault info to userspace. + */ + +struct vfio_iommu_fault_info { + __u64 addr; + __u16 sid; + __u8 fault_reason; + __u8 is_write:1; +}; + +struct vfio_iommu_type1_get_fault_info { + __u32 argsz; + __u32 flags; + __u32 count; + struct vfio_iommu_fault_info fault_info[]; +}; + +#define VFIO_IOMMU_GET_FAULT_INFO _IO(VFIO_TYPE, VFIO_BASE + 18) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*