From patchwork Mon Apr 4 08:19:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8738341 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E7BBDC0553 for ; Mon, 4 Apr 2016 08:20:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D3E1D2024F for ; Mon, 4 Apr 2016 08:20:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AE904201DD for ; Mon, 4 Apr 2016 08:20:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754794AbcDDIUA (ORCPT ); Mon, 4 Apr 2016 04:20:00 -0400 Received: from mail-lb0-f169.google.com ([209.85.217.169]:36720 "EHLO mail-lb0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754788AbcDDIT5 (ORCPT ); Mon, 4 Apr 2016 04:19:57 -0400 Received: by mail-lb0-f169.google.com with SMTP id qe11so148709140lbc.3 for ; Mon, 04 Apr 2016 01:19:56 -0700 (PDT) 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=8oiJZ+dAmUaPuFl4Z+WfDPE03F/d0+oI0sqMyBgMyTE=; b=jcTkyvRjgJk6w/4Jzly0VDQgoa21Fo3oqmXuo3CDtOoNulYhOEwmyK14+i6m/naou5 eYOWZ5S6VlHiUAU6G6jjxl7in6JMOloS44YFiSXt4zoB31Enr7Q0v/VEfGUJyHbtKjUX JUaGSxI2hOm4sFBYTwy9dT9u9Dd+A5rQ+Uo6A= 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=8oiJZ+dAmUaPuFl4Z+WfDPE03F/d0+oI0sqMyBgMyTE=; b=M7Ik6xwSFud3waF50NGk+t3hs3dlITYYFEOo5FAFpHdHPD3YglD30xTjxu4X0wZsl3 yUm+WQVjFtHYvHtEd53HQ2fjVMxmf6FxEUkztOFPQbesU7EuMgmFOc+e7W08YDagkQVU hf7uvJjQLP1dzff7Gxz1fVcRkdGs2ml2HQLu4yjnfCfO7c31odh2fpTvyRQ//f6gTKrW w8kV0wW4BnC9XjfNbVE4XMCRi+MZqX15ckKoe0YBs5V5v+12vzNH0bU7tAcjtXjdFTBs xVzIGMUU/mJHpRPlveLtJPPahIisZygPg9d4y8O22TRL08PNKiDxMGD1vl4Io4mdNjvR 1P2A== X-Gm-Message-State: AD7BkJIY1dxuEEIWf3VQ4I40dOeAFwlKzeXvXEJ6ySg/DkdCqGf5hINKIDlNeMtxM3ASzafD X-Received: by 10.28.88.81 with SMTP id m78mr10633580wmb.58.1459757994600; Mon, 04 Apr 2016 01:19:54 -0700 (PDT) Received: from new-host-2.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id w8sm27717203wjf.19.2016.04.04.01.19.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 04 Apr 2016 01:19:53 -0700 (PDT) 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 Cc: suravee.suthikulpanit@amd.com, patches@linaro.org, linux-kernel@vger.kernel.org, Manish.Jaggi@caviumnetworks.com, Bharat.Bhushan@freescale.com, pranav.sawargaonkar@gmail.com, p.fedin@samsung.com, iommu@lists.linux-foundation.org, Jean-Philippe.Brucker@arm.com, julien.grall@arm.com Subject: [PATCH v6 4/4] msi: IOMMU map the doorbell address when needed Date: Mon, 4 Apr 2016 08:19:32 +0000 Message-Id: <1459757972-2814-5-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1459757972-2814-1-git-send-email-eric.auger@linaro.org> References: <1459757972-2814-1-git-send-email-eric.auger@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,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 is emitted by a device attached to an iommu domain and this iommu domain requires MSI mapping, the msi address (aka doorbell) must be mapped in the IOMMU. Else MSI write transaction will cause a fault. We handle the iommu mapping/unmapping anytime the msi address is updated. In case the mapping fails we just WARN_ON. Signed-off-by: Eric Auger --- v6: - check the domain type to detect bypass situation (rebase on new default domain modality) - fix the msi_domain_set_affinity sequence: unmap the address once the PCI device has been given the new address - use new proto for iommu_put_reserved_iova. New function names v5: - use macros to increase the readability - add comments - fix a typo that caused a compilation error if CONFIG_IOMMU_API is not set --- include/linux/msi.h | 15 +++++++ kernel/irq/msi.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/include/linux/msi.h b/include/linux/msi.h index 08441b1..1dc41db 100644 --- a/include/linux/msi.h +++ b/include/linux/msi.h @@ -10,6 +10,21 @@ struct msi_msg { u32 data; /* 16 bits of msi message data */ }; +/* Helpers to convert the msi message address to a an iova/physical address */ +#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT +#define msg_to_dma_addr(msg) \ + (((dma_addr_t)((msg)->address_hi) << 32) | (msg)->address_lo) +#else +#define msg_to_dma_addr(msg) ((msg)->address_lo) +#endif + +#ifdef CONFIG_PHYS_ADDR_T_64BIT +#define msg_to_phys_addr(msg) \ + (((phys_addr_t)((msg)->address_hi) << 32) | (msg)->address_lo) +#else +#define msg_to_phys_addr(msg) ((msg)->address_lo) +#endif + extern int pci_msi_ignore_mask; /* Helper functions */ struct irq_data; diff --git a/kernel/irq/msi.c b/kernel/irq/msi.c index 9b0ba4a..7440653 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) { @@ -56,6 +58,94 @@ static inline void irq_chip_write_msi_msg(struct irq_data *data, } /** + * msi_map_doorbell: make sure an IOMMU mapping exists on domain @d + * for the message physical address (aka. doorbell) + * + * Either allocate an IOVA and create a mapping or simply increment + * a reference count on the existing IOMMU mapping + * @d: iommu domain handle the mapping belongs to + * @msg: msi message handle + */ +static int msi_map_doorbell(struct iommu_domain *d, struct msi_msg *msg) +{ +#ifdef CONFIG_IOMMU_DMA_RESERVED + phys_addr_t addr; + dma_addr_t iova; + int ret; + + addr = msg_to_phys_addr(msg); + ret = iommu_get_reserved_iova(d, addr, sizeof(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 +} + +/** + * msi_unmap_doorbell: decrements the reference count on an existing + * doorbell IOMMU mapping + * + * @d: iommu domain the mapping is attached to + * @msg: msi message containing the doorbell IOVA to unbind + */ +static void msi_unmap_doorbell(struct iommu_domain *d, struct msi_msg *msg) +{ +#ifdef CONFIG_IOMMU_DMA_RESERVED + dma_addr_t iova; + + iova = msg_to_dma_addr(msg); + iommu_put_reserved_iova(d, iova); +#endif +} + +#ifdef CONFIG_IOMMU_API +/** + * irq_data_to_msi_mapping_domain: checks if an irq corresponds to + * an MSI whose write address must be mapped in an IOMMU domain + * + * determine whether the irq corresponds to an MSI emitted by a device, + * upstream to an IOMMU, and if this IOMMU requires a binding of the + * MSI address + * + * @irq_data: irq data handle + */ +static struct iommu_domain * +irq_data_to_msi_mapping_domain(struct irq_data *irq_data) +{ + struct iommu_domain *d; + struct msi_desc *desc; + struct device *dev; + 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 || (d->type == IOMMU_DOMAIN_DMA)) + return NULL; + + ret = iommu_domain_get_attr(d, DOMAIN_ATTR_MSI_MAPPING, NULL); + if (!ret) + return d; + else + return NULL; +} +#else +static inline struct iommu_domain * +irq_data_to_msi_mapping_domain(struct irq_data *irq_data) +{ + return NULL; +} +#endif /* CONFIG_IOMMU_API */ + +/** * msi_domain_set_affinity - Generic affinity setter function for MSI domains * @irq_data: The irq data associated to the interrupt * @mask: The affinity mask to set @@ -73,8 +163,27 @@ 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) { + bool doorbell_change = false; + struct iommu_domain *d; + struct msi_msg old_msg; + + d = irq_data_to_msi_mapping_domain(irq_data); + if (unlikely(d)) { + get_cached_msi_msg(irq_data->irq, &old_msg); + doorbell_change = + (old_msg.address_lo != msg.address_lo) || + (old_msg.address_hi != msg.address_hi); + } + BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg)); + + if (unlikely(doorbell_change)) + WARN_ON(msi_map_doorbell(d, &msg)); + irq_chip_write_msi_msg(irq_data, &msg); + + if (unlikely(doorbell_change)) + msi_unmap_doorbell(d, &old_msg); } return ret; @@ -83,19 +192,33 @@ int msi_domain_set_affinity(struct irq_data *irq_data, static void msi_domain_activate(struct irq_domain *domain, struct irq_data *irq_data) { + struct iommu_domain *d; struct msi_msg msg; + d = irq_data_to_msi_mapping_domain(irq_data); + BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg)); + if (unlikely(d)) + WARN_ON(msi_map_doorbell(d, &msg)); irq_chip_write_msi_msg(irq_data, &msg); } static void msi_domain_deactivate(struct irq_domain *domain, struct irq_data *irq_data) { + struct iommu_domain *d; + struct msi_msg old_msg; struct msi_msg msg; + d = irq_data_to_msi_mapping_domain(irq_data); + if (unlikely(d)) + get_cached_msi_msg(irq_data->irq, &old_msg); + memset(&msg, 0, sizeof(msg)); irq_chip_write_msi_msg(irq_data, &msg); + + if (unlikely(d)) + msi_unmap_doorbell(d, &old_msg); } static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,