From patchwork Tue Jan 26 13:12:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 60459 Delivered-To: patches@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp1949198lbb; Tue, 26 Jan 2016 05:13:49 -0800 (PST) X-Received: by 10.194.89.72 with SMTP id bm8mr22765358wjb.60.1453814025495; Tue, 26 Jan 2016 05:13:45 -0800 (PST) Return-Path: Received: from mail-wm0-x233.google.com (mail-wm0-x233.google.com. [2a00:1450:400c:c09::233]) by mx.google.com with ESMTPS id je3si1794084wjb.14.2016.01.26.05.13.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jan 2016 05:13:45 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::233 as permitted sender) client-ip=2a00:1450:400c:c09::233; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::233 as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro.org Received: by mail-wm0-x233.google.com with SMTP id b14so129669059wmb.1 for ; Tue, 26 Jan 2016 05:13:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=H/mkrxvDLPINrGeMbGp8Oi25LbMIdKyB66qFvhevmT4=; b=VrN3GDKs0kyovUpQHgNXue0VOZqnjvJgUIMWUZPZ2fH7epa9M0PPpD5aRxi17Malu6 H4FwHJfrRhfZEmAU31+mjy3hq87FrR0gHETCCfebhrcja3H4C/uzBVRKmVCKDtz9zXoC wgt3Qc+ywRsMP4jBnb5qITFl4Q8TYZcGuQRvI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=H/mkrxvDLPINrGeMbGp8Oi25LbMIdKyB66qFvhevmT4=; b=SGZwbouo0r0J9yLZN69l4tqEH09yfV5Mw1MLmBlRNZAai2u+qfXgt5QWc5iP1Vhrvb MpqPd1P0e05PqiBvyRbZ7ZBQdntMsRVscNR8RQR4tVgt3jZ2Jc8VJRLeYlTUYrkDgmmV aruLlvZvviBXr9lS4d26zM+yVbwEkqs84LHicobKBgkrBdRKwVDiyi3K1k6SP+zqiz2Q DklB1GPWL/WH87tVa87CnGfJpDn0BHCO6plTZQTLZ59W9qbdXVtqMtB81CPSgESadjnw rmcTw79sqkKXeCATU3aonqjXbByXNDzxRO/fOSaBfXo1Y3ElERCrVD09k2zSUZwa6Lp5 x/CQ== X-Gm-Message-State: AG10YOQNFQ+QdnT2ua50AcFHU4UCYpci95DKJY+OwOS1e7uDouv77ICT/NNUE+l9VKocQBSRiEs= X-Received: by 10.28.184.76 with SMTP id i73mr22981640wmf.43.1453814025302; Tue, 26 Jan 2016 05:13:45 -0800 (PST) Return-Path: Received: from localhost.localdomain (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id ct2sm1388885wjb.46.2016.01.26.05.13.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jan 2016 05:13:43 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, alex.williamson@redhat.com, will.deacon@arm.com, christoffer.dall@linaro.org, marc.zyngier@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Cc: Bharat.Bhushan@freescale.com, pranav.sawargaonkar@gmail.com, p.fedin@samsung.com, suravee.suthikulpanit@amd.com, linux-kernel@vger.kernel.org, patches@linaro.org, iommu@lists.linux-foundation.org Subject: [PATCH 09/10] vfio-pci: create an iommu mapping for msi address Date: Tue, 26 Jan 2016 13:12:47 +0000 Message-Id: <1453813968-2024-10-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453813968-2024-1-git-send-email-eric.auger@linaro.org> References: <1453813968-2024-1-git-send-email-eric.auger@linaro.org> Some platforms require the MSI address programmed in the PCI device to be an IOVA and not a host physical address. This is typically the case for ARM and PowerPC, as opposed to x86. This patch allocates an IOVA page and maps it onto the physical page which contains the target MSI write transaction address. In case an IOMMU binding already exists between those 2, simply reprogram the device. The binding is destroyed by the VFIO IOMMU backend. Signed-off-by: Bharat Bhushan Signed-off-by: Eric Auger --- --- drivers/vfio/pci/vfio_pci_intrs.c | 73 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 5 deletions(-) -- 1.9.1 diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c index 3b3ba15..bac24c9 100644 --- a/drivers/vfio/pci/vfio_pci_intrs.c +++ b/drivers/vfio/pci/vfio_pci_intrs.c @@ -305,6 +305,57 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) return 0; } +/** + * vfio_set_mapped_msi_addr: overwrites the msi physical address with an iova + * + * @pdev: vfio pci device handle + * @irq: irq linux number + * returns 0 upon success, < 0 on failure + */ +static int vfio_set_mapped_msi_addr(struct vfio_pci_device *vdev, int irq) +{ + phys_addr_t msi_addr; + dma_addr_t msi_iova; + struct vfio_group *group = vdev->vfio_group; + struct msi_msg msg; + int ret; + + get_cached_msi_msg(irq, &msg); + msi_addr = (phys_addr_t)(msg.address_hi) << 32 | + (phys_addr_t)(msg.address_lo); + + ret = vfio_group_alloc_map_reserved_iova(group, msi_addr, + IOMMU_WRITE, &msi_iova); + if (ret) + goto out; + + /* Re-program the msi-address with the iova */ + msg.address_hi = (u32)(msi_iova >> 32); + msg.address_lo = (u32)(msi_iova & 0xffffffff); + pci_write_msi_msg(irq, &msg); + +out: + return ret; +} + +/** + * vfio_unset_mapped_msi_addr: decrement the ref counter of the msi iova page + * associated to the linux irq (in case it is null unmaps and frees resources) + * + * @pdev: vfio pci device handle + * @irq: irq linux number + */ +static void vfio_unset_mapped_msi_addr(struct vfio_pci_device *vdev, int irq) +{ + dma_addr_t msi_iova; + struct vfio_group *group = vdev->vfio_group; + struct msi_msg msg; + + get_cached_msi_msg(irq, &msg); + msi_iova = (u64)(msg.address_hi) << 32 | (u64)(msg.address_lo); + vfio_group_unmap_free_reserved_iova(group, msi_iova); +} + static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev, int vector, int fd, bool msix) { @@ -318,6 +369,7 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev, return -EINVAL; if (vdev->ctx[vector].trigger) { + vfio_unset_mapped_msi_addr(vdev, irq); free_irq(irq, vdev->ctx[vector].trigger); irq_bypass_unregister_producer(&vdev->ctx[vector].producer); kfree(vdev->ctx[vector].name); @@ -355,11 +407,8 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev, ret = request_irq(irq, vfio_msihandler, 0, vdev->ctx[vector].name, trigger); - if (ret) { - kfree(vdev->ctx[vector].name); - eventfd_ctx_put(trigger); - return ret; - } + if (ret) + goto error_free; vdev->ctx[vector].producer.token = trigger; vdev->ctx[vector].producer.irq = irq; @@ -369,9 +418,23 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev, "irq bypass producer (token %p) registration fails: %d\n", vdev->ctx[vector].producer.token, ret); + if (vfio_group_require_msi_mapping(vdev->vfio_group)) { + ret = vfio_set_mapped_msi_addr(vdev, irq); + if (ret) + goto error_free_irq; + } + vdev->ctx[vector].trigger = trigger; return 0; + +error_free_irq: + free_irq(irq, vdev->ctx[vector].trigger); +error_free: + kfree(vdev->ctx[vector].name); + eventfd_ctx_put(trigger); + return ret; + } static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,