From patchwork Mon May 27 11:42:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 1105798 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=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45CFrl56HWz9sB8 for ; Mon, 27 May 2019 21:59:31 +1000 (AEST) Received: from localhost ([127.0.0.1]:44253 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hVEHp-0007NO-Dj for incoming@patchwork.ozlabs.org; Mon, 27 May 2019 07:59:29 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38950) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hVE4b-0005Xs-N6 for qemu-devel@nongnu.org; Mon, 27 May 2019 07:45:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hVE4a-0005dt-Ek for qemu-devel@nongnu.org; Mon, 27 May 2019 07:45:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43016) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hVE4U-0005AU-Ql; Mon, 27 May 2019 07:45:43 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 42C3046202; Mon, 27 May 2019 11:45:12 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-67.ams2.redhat.com [10.36.116.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id 25F3D5D962; Mon, 27 May 2019 11:45:06 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org Date: Mon, 27 May 2019 13:42:01 +0200 Message-Id: <20190527114203.2762-26-eric.auger@redhat.com> In-Reply-To: <20190527114203.2762-1-eric.auger@redhat.com> References: <20190527114203.2762-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 27 May 2019 11:45:12 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v4 25/27] vfio-pci: register handler for iommu fault 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: drjones@redhat.com, yi.l.liu@intel.com, mst@redhat.com, jean-philippe.brucker@arm.com, zhangfei.gao@foxmail.com, peterx@redhat.com, alex.williamson@redhat.com, vincent.stehle@arm.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" We use the VFIO_PCI_DMA_FAULT_IRQ_INDEX "irq" index to set/unset a notifier for physical DMA faults. The associated eventfd is triggered, in nested mode, whenever a fault is detected at IOMMU physical level. As this is the first use of this new IRQ index, also handle it in irq_to_str() in case the signaling setup fails. The actual handler will be implemented in subsequent patches. Signed-off-by: Eric Auger --- v3 -> v4: - check VFIO_PCI_DMA_FAULT_IRQ_INDEX is supported at kernel level before attempting to set signaling for it. --- hw/vfio/common.c | 3 +++ hw/vfio/pci.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 1 + 3 files changed, 56 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 532ede0e70..cf0087321e 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -130,6 +130,9 @@ static char *irq_to_str(int index, int subindex) case VFIO_PCI_REQ_IRQ_INDEX: str = g_strdup_printf("REQ-%d", subindex); break; + case VFIO_PCI_DMA_FAULT_IRQ_INDEX: + str = g_strdup_printf("DMA-FAULT-%d", subindex); + break; default: str = g_strdup_printf("index %d (unknown)", index); break; diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index b613b20501..29d4f633b0 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2736,6 +2736,56 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static void vfio_dma_fault_notifier_handler(void *opaque) +{ + VFIOPCIDevice *vdev = opaque; + + if (!event_notifier_test_and_clear(&vdev->dma_fault_notifier)) { + return; + } +} + +static void vfio_register_dma_fault_notifier(VFIOPCIDevice *vdev) +{ + struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info), + .index = VFIO_PCI_DMA_FAULT_IRQ_INDEX }; + Error *err = NULL; + int32_t fd; + + if (ioctl(vdev->vbasedev.fd, + VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0 || irq_info.count < 1) { + return; + } + + if (event_notifier_init(&vdev->dma_fault_notifier, 0)) { + error_report("vfio: Unable to init event notifier for dma fault"); + return; + } + + fd = event_notifier_get_fd(&vdev->dma_fault_notifier); + qemu_set_fd_handler(fd, vfio_dma_fault_notifier_handler, NULL, vdev); + + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_DMA_FAULT_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + qemu_set_fd_handler(fd, NULL, NULL, vdev); + event_notifier_cleanup(&vdev->dma_fault_notifier); + } +} + +static void vfio_unregister_dma_fault_notifier(VFIOPCIDevice *vdev) +{ + Error *err = NULL; + + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_DMA_FAULT_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + } + qemu_set_fd_handler(event_notifier_get_fd(&vdev->dma_fault_notifier), + NULL, NULL, vdev); + event_notifier_cleanup(&vdev->dma_fault_notifier); +} + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev = PCI_VFIO(pdev); @@ -3035,6 +3085,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_err_notifier(vdev); vfio_register_req_notifier(vdev); + vfio_register_dma_fault_notifier(vdev); vfio_setup_resetfn_quirk(vdev); return; @@ -3073,6 +3124,7 @@ static void vfio_exitfn(PCIDevice *pdev) vfio_unregister_req_notifier(vdev); vfio_unregister_err_notifier(vdev); + vfio_unregister_dma_fault_notifier(vdev); pci_device_set_intx_routing_notifier(&vdev->pdev, NULL); vfio_disable_interrupts(vdev); if (vdev->intx.mmap_timer) { diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index cfcd1a81b8..96d29d667b 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -135,6 +135,7 @@ typedef struct VFIOPCIDevice { PCIHostDeviceAddress host; EventNotifier err_notifier; EventNotifier req_notifier; + EventNotifier dma_fault_notifier; int (*resetfn)(struct VFIOPCIDevice *); uint32_t vendor_id; uint32_t device_id;