From patchwork Fri Feb 26 17:35:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8440461 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0666C9F52D for ; Fri, 26 Feb 2016 17:45:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0B5BB203B4 for ; Fri, 26 Feb 2016 17:45:26 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 10A4C203AD for ; Fri, 26 Feb 2016 17:45:24 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aZMQd-0000V1-G3; Fri, 26 Feb 2016 17:43:47 +0000 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aZMJv-0001yS-Be for linux-arm-kernel@lists.infradead.org; Fri, 26 Feb 2016 17:37:01 +0000 Received: by mail-wm0-x234.google.com with SMTP id c200so82113164wme.0 for ; Fri, 26 Feb 2016 09:36:31 -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=p/ufF+Kvp8QR57/m896yeSFea5HKTWUJjISmaqdqzIs=; b=L6UOGGh/XLSmT95rmoKUcJfvDq71U3xf7WNca07b+p+nKzGr6qCfZ11qmquI3cE8tG ZhDYd9nhi+qdgmp8KpxVJuWu/Q/uSegJFokS627v4IMwV8iwsL+NZ90EFB+QdEQzU3MG dZRyxxr2ZF2jCgsPOk4wQsw66k2qOtsEQ2z9Y= 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=p/ufF+Kvp8QR57/m896yeSFea5HKTWUJjISmaqdqzIs=; b=OdVWPX1n+tggTnvnP+V68X68PesmyDrtDY0YtdQ/q9fzZAfCjwfLKT9dosQ5KMZrOm T5yEesAFKx0Br7RUyBSiJCu/+QYLCXZ+frgj/NBCoUwvEVRnT0YtqiCYa3X1CuFFQ/PO wCACf0gVGwEO343mCUymMgFg/2Bcq2aP4ysopt1qyufGUjA1UHIAGGz49cWDh3tTm/Nn 4Ts1J6tbzvkYS4EWt4TSoEgdAWPGj2HIfDFF69LHJvWICAeIe5FBY3oAKyqEPHjr8Dqr wbKj7K1NT4DuHxDYvhQCCT2vRIArVdnch4I9kvQ9OPk266KmVP3OLce8ISagIX1Kl/av n7Wg== X-Gm-Message-State: AD7BkJKJJV/LicXB3XqDG6/aYjETRkzABYHicFFcVzKNergNKkF2ZPm3Jy17Wc6ZZ6Zf0VHo X-Received: by 10.28.97.136 with SMTP id v130mr4193150wmb.20.1456508189957; Fri, 26 Feb 2016 09:36:29 -0800 (PST) Received: from new-host-8.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id 77sm3750373wmp.18.2016.02.26.09.36.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Feb 2016 09:36:27 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, robin.murphy@arm.com, alex.williamson@redhat.com, will.deacon@arm.com, joro@8bytes.org, tglx@linutronix.de, jason@lakedaemon.net, marc.zyngier@arm.com, christoffer.dall@linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Subject: [RFC v4 09/14] msi: IOMMU map the doorbell address when needed Date: Fri, 26 Feb 2016 17:35:49 +0000 Message-Id: <1456508154-2253-10-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1456508154-2253-1-git-send-email-eric.auger@linaro.org> References: <1456508154-2253-1-git-send-email-eric.auger@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160226_093652_127509_1B048504 X-CRM114-Status: GOOD ( 15.87 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: patches@linaro.org, Manish.Jaggi@caviumnetworks.com, p.fedin@samsung.com, linux-kernel@vger.kernel.org, Bharat.Bhushan@freescale.com, iommu@lists.linux-foundation.org, pranav.sawargaonkar@gmail.com, suravee.suthikulpanit@amd.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In case the msi_desc references a device attached to an iommu domain and this iommu domain requires MSI mapping, the msi address (aka doorbell) needs to be mapped in the IOMMU. Else any MSI write transaction will cause a fault. We perform this action at msi message composition time. We need to track when the doorbell address changes. On domain deactivate, we now also call the composition function with a new erase parameter. In case the mapping fails we just WARN_ON. Signed-off-by: Eric Auger --- v3 -> v4: - that code was formely in irq-gic-common.c "irqchip/gicv2m/v3-its-pci-msi: IOMMU map the MSI frame when needed" also the [un]mapping was done in irq_write_msi_msg; now done on compose v2 -> v3: - protect iova/addr manipulation with CONFIG_ARCH_DMA_ADDR_T_64BIT and CONFIG_PHYS_ADDR_T_64BIT - only expose gic_pci_msi_domain_write_msg in case CONFIG_IOMMU_API & CONFIG_PCI_MSI_IRQ_DOMAIN are set. - gic_set/unset_msi_addr duly become static --- kernel/irq/msi.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 3 deletions(-) diff --git a/kernel/irq/msi.c b/kernel/irq/msi.c index 9b0ba4a..93bd861 100644 --- a/kernel/irq/msi.c +++ b/kernel/irq/msi.c @@ -17,6 +17,8 @@ /* Temparory solution for building, will be removed later */ #include +#include +#include struct msi_desc *alloc_msi_entry(struct device *dev) { @@ -55,6 +57,110 @@ static inline void irq_chip_write_msi_msg(struct irq_data *data, data->chip->irq_write_msi_msg(data, msg); } +static int msi_map_doorbell(struct iommu_domain *d, struct msi_msg *msg) +{ +#ifdef CONFIG_IOMMU_DMA_RESERVED + dma_addr_t iova; + phys_addr_t addr; + int ret; + +#ifdef CONFIG_PHYS_ADDR_T_64BIT + addr = ((phys_addr_t)(msg->address_hi) << 32) | msg->address_lo; +#else + addr = msg->address_lo; +#endif + + ret = iommu_get_single_reserved(d, addr, IOMMU_WRITE, &iova); + if (!ret) { + msg->address_lo = lower_32_bits(iova); + msg->address_hi = upper_32_bits(iova); + } + return ret; +#else + return -ENODEV; +#endif +} + +static void msi_unmap_doorbell(struct iommu_domain *d, struct msi_msg *msg) +{ +#ifdef CONFIG_IOMMU_DMA_RESERVED + dma_addr_t iova; + +#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT + iova = ((dma_addr_t)(msg->address_hi) << 32) | msg->address_lo; +#else + iova = msg->address_lo; +#endif + + iommu_put_single_reserved(d, iova); +#endif +} + +#ifdef CONFIG_IOMMU_API +static struct iommu_domain * + irq_data_to_msi_mapping_domain(struct irq_data *irq_data) +{ + + struct msi_desc *desc; + struct device *dev; + struct iommu_domain *d; + int ret; + + desc = irq_data_get_msi_desc(irq_data); + if (!desc) + return NULL; + + dev = msi_desc_to_dev(desc); + + d = iommu_get_domain_for_dev(dev); + if (!d) + return NULL; + + ret = iommu_domain_get_attr(d, DOMAIN_ATTR_MSI_MAPPING, NULL); + if (!ret) + return d; + else + return NULL; +} +#else +static inline iommu_domain * + irq_data_to_msi_mapping_domain(struct irq_data *irq_data) +{ + return NULL; +} +#endif /* CONFIG_IOMMU_API */ + +static int msi_compose(struct irq_data *irq_data, + struct msi_msg *msg, bool erase) +{ + struct msi_msg old_msg; + struct iommu_domain *d; + int ret = 0; + + d = irq_data_to_msi_mapping_domain(irq_data); + if (unlikely(d)) + get_cached_msi_msg(irq_data->irq, &old_msg); + + if (erase) + memset(msg, 0, sizeof(*msg)); + else + ret = irq_chip_compose_msi_msg(irq_data, msg); + + if (!d) + goto out; + + /* handle (re-)mapping of MSI doorbell */ + if ((old_msg.address_lo != msg->address_lo) || + (old_msg.address_hi != msg->address_hi)) + msi_unmap_doorbell(d, &old_msg); + + if (!erase) + WARN_ON(msi_map_doorbell(d, msg)); + +out: + return ret; +} + /** * msi_domain_set_affinity - Generic affinity setter function for MSI domains * @irq_data: The irq data associated to the interrupt @@ -73,7 +179,7 @@ int msi_domain_set_affinity(struct irq_data *irq_data, ret = parent->chip->irq_set_affinity(parent, mask, force); if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) { - BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg)); + BUG_ON(msi_compose(irq_data, &msg, false)); irq_chip_write_msi_msg(irq_data, &msg); } @@ -85,7 +191,7 @@ static void msi_domain_activate(struct irq_domain *domain, { struct msi_msg msg; - BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg)); + BUG_ON(msi_compose(irq_data, &msg, false)); irq_chip_write_msi_msg(irq_data, &msg); } @@ -94,7 +200,7 @@ static void msi_domain_deactivate(struct irq_domain *domain, { struct msi_msg msg; - memset(&msg, 0, sizeof(msg)); + msi_compose(irq_data, &msg, true); irq_chip_write_msi_msg(irq_data, &msg); }