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: 729686 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 3vRR705m2wz9s7p for ; Mon, 20 Feb 2017 12:36:28 +1100 (AEDT) Received: from localhost ([::1]:35645 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cfctu-0008Q1-AV for incoming@patchwork.ozlabs.org; Sun, 19 Feb 2017 20:36:26 -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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 -------- */ /*