From patchwork Mon Apr 4 08:30:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8738551 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 1CF25C0553 for ; Mon, 4 Apr 2016 08:31:14 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 10B392024F for ; Mon, 4 Apr 2016 08:31:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C88B22022A for ; Mon, 4 Apr 2016 08:31:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932245AbcDDIak (ORCPT ); Mon, 4 Apr 2016 04:30:40 -0400 Received: from mail-lb0-f178.google.com ([209.85.217.178]:34521 "EHLO mail-lb0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932234AbcDDIai (ORCPT ); Mon, 4 Apr 2016 04:30:38 -0400 Received: by mail-lb0-f178.google.com with SMTP id vo2so149388143lbb.1 for ; Mon, 04 Apr 2016 01:30:37 -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=FLCjZCFLpwjpltzLVThzKFIZ8cKH4zVe4NXs83m1XyE=; b=MfHMAso5we4y9Cw6KCEoqjR/bM5SZe5mzYCZYcftoKPdLPA6iNYCjJ9ntijdMrC71U 7jqbRKjb2GCGTcZXaDDK/yk1kQF4rF+LUQScjDuoW1ODqYzPZ7WXeSlLALq04R+x2e4E 8Q50R9bfhooHl1Ljai/YRsORFACs/SirnOCcA= 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=FLCjZCFLpwjpltzLVThzKFIZ8cKH4zVe4NXs83m1XyE=; b=RjgjN1SUPjAtVgR/tJn6xfZs/udoGAwBnSXyUz2H5A6V1WDYg4ecMJcJUJQOmtnUyE x/JG26N7kV4BarYEDsWCSyutg1aQQAOA8pBmylF6XmfQtxbGCzkpAT3ZK+HmQbsCGzlJ WwqJA9Ncmb1h8uym6bv4MRsgO4VhQfwUAWLrTylLALmdJW813qoWCtpsl+KrJ9nPKsM3 wvBzcIeurkU72bwfOJ5AiznSQSS9LP3OC3XCMrmhDeVLPDHJC8puQZEhZW43oVRdVqpu 8dDTTqm6eiljUKuw801nGVOC2iCi6jPSrSj+Wu5IX1CjxAFTQqK93MlXSTv9ESJT9y03 twjg== X-Gm-Message-State: AD7BkJI9la2N8IF+DG6Jd+nK4cLJaR5ROf9g8wNlK778LZuBwzX1RImBDHd14JD8CHstdgkT X-Received: by 10.28.142.213 with SMTP id q204mr10632753wmd.69.1459758636884; Mon, 04 Apr 2016 01:30:36 -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 j71sm1178257wmj.21.2016.04.04.01.30.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 04 Apr 2016 01:30:34 -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 5/5] vfio/type1: return MSI mapping requirements with VFIO_IOMMU_GET_INFO Date: Mon, 4 Apr 2016 08:30:11 +0000 Message-Id: <1459758611-2972-6-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1459758611-2972-1-git-send-email-eric.auger@linaro.org> References: <1459758611-2972-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 This patch allows the user-space to know whether MSI addresses need to be mapped in the IOMMU. The user-space uses VFIO_IOMMU_GET_INFO ioctl and IOMMU_INFO_REQUIRE_MSI_MAP gets set if they need to. Also the number of IOMMU pages requested to map those is returned in msi_iova_pages field. User-space must use this information to allocate an IOVA contiguous region of size msi_iova_pages * ffs(iova_pgsizes) and pass it with VFIO_IOMMU_MAP_DMA iotcl (VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA set). Signed-off-by: Eric Auger --- Currently it is assumed a single doorbell page is used per MSI controller. This is the case for known ARM MSI controllers (GICv2M, GICv3 ITS, ...). If an MSI controller were to expose more doorbells it could implement a new callback at irq_chip interface. v4 -> v5: - move msi_info and ret declaration within the conditional code v3 -> v4: - replace former vfio_domains_require_msi_mapping by more complex computation of MSI mapping requirements, especially the number of pages to be provided by the user-space. - reword patch title RFC v1 -> v1: - derived from [RFC PATCH 3/6] vfio: Extend iommu-info to return MSIs automap state - renamed allow_msi_reconfig into require_msi_mapping - fixed VFIO_IOMMU_GET_INFO --- drivers/vfio/vfio_iommu_type1.c | 147 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 2 + 2 files changed, 149 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index b330b81..f1def50 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -39,6 +39,7 @@ #include #include #include +#include #define DRIVER_VERSION "0.2" #define DRIVER_AUTHOR "Alex Williamson " @@ -95,6 +96,17 @@ struct vfio_group { struct list_head next; }; +struct vfio_irq_chip { + struct list_head next; + struct irq_chip *chip; +}; + +struct vfio_msi_map_info { + bool mapping_required; + unsigned int iova_pages; + struct list_head irq_chip_list; +}; + /* * This code handles mapping and unmapping of user data buffers * into DMA'ble space using the IOMMU @@ -267,6 +279,127 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) return ret; } +#if defined(CONFIG_GENERIC_MSI_IRQ_DOMAIN) && defined(CONFIG_IOMMU_DMA_RESERVED) +/** + * vfio_dev_compute_msi_map_info: augment MSI mapping info (@data) with + * the @dev device requirements. + * + * @dev: device handle + * @data: opaque pointing to a struct vfio_msi_map_info + * + * returns 0 upon success or -ENOMEM + */ +static int vfio_dev_compute_msi_map_info(struct device *dev, void *data) +{ + struct irq_domain *domain; + struct msi_domain_info *info; + struct vfio_msi_map_info *msi_info = (struct vfio_msi_map_info *)data; + struct irq_chip *chip; + struct vfio_irq_chip *iter, *new; + + domain = dev_get_msi_domain(dev); + if (!domain) + return 0; + + /* Let's compute the needs for the MSI domain */ + info = msi_get_domain_info(domain); + chip = info->chip; + list_for_each_entry(iter, &msi_info->irq_chip_list, next) { + if (iter->chip == chip) + return 0; + } + + new = kzalloc(sizeof(*new), GFP_KERNEL); + if (!new) + return -ENOMEM; + + new->chip = chip; + + list_add(&new->next, &msi_info->irq_chip_list); + + /* + * new irq_chip to be taken into account; we currently assume + * a single iova doorbell by irq chip requesting MSI mapping + */ + msi_info->iova_pages += 1; + return 0; +} + +/** + * vfio_domain_compute_msi_map_info: compute MSI mapping requirements (@data) + * for vfio_domain @d + * + * @d: vfio domain handle + * @data: opaque pointing to a struct vfio_msi_map_info + * + * returns 0 upon success or -ENOMEM + */ +static int vfio_domain_compute_msi_map_info(struct vfio_domain *d, void *data) +{ + int ret = 0; + struct vfio_msi_map_info *msi_info = (struct vfio_msi_map_info *)data; + struct vfio_irq_chip *iter, *tmp; + struct vfio_group *g; + + msi_info->iova_pages = 0; + INIT_LIST_HEAD(&msi_info->irq_chip_list); + + if (iommu_domain_get_attr(d->domain, + DOMAIN_ATTR_MSI_MAPPING, NULL)) + return 0; + msi_info->mapping_required = true; + list_for_each_entry(g, &d->group_list, next) { + ret = iommu_group_for_each_dev(g->iommu_group, msi_info, + vfio_dev_compute_msi_map_info); + if (ret) + goto out; + } +out: + list_for_each_entry_safe(iter, tmp, &msi_info->irq_chip_list, next) { + list_del(&iter->next); + kfree(iter); + } + return ret; +} + +/** + * vfio_compute_msi_map_info: compute MSI mapping requirements + * + * Do some MSI addresses need to be mapped? IOMMU page size? + * Max number of IOVA pages needed by any domain to map MSI + * + * @iommu: iommu handle + * @info: msi map info handle + * + * returns 0 upon success or -ENOMEM + */ +static int vfio_compute_msi_map_info(struct vfio_iommu *iommu, + struct vfio_msi_map_info *msi_info) +{ + int ret = 0; + struct vfio_domain *d; + unsigned long bitmap = ULONG_MAX; + unsigned int iova_pages = 0; + + msi_info->mapping_required = false; + + mutex_lock(&iommu->lock); + list_for_each_entry(d, &iommu->domain_list, next) { + bitmap &= d->domain->ops->pgsize_bitmap; + ret = vfio_domain_compute_msi_map_info(d, msi_info); + if (ret) + goto out; + if (msi_info->iova_pages > iova_pages) + iova_pages = msi_info->iova_pages; + } +out: + msi_info->iova_pages = iova_pages; + mutex_unlock(&iommu->lock); + return ret; +} + +#endif + /* * Attempt to pin pages. We really don't want to track all the pfns and * the iommu can only map chunks of consecutive pfns anyway, so get the @@ -1179,6 +1312,20 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, info.flags = VFIO_IOMMU_INFO_PGSIZES; +#if defined(CONFIG_GENERIC_MSI_IRQ_DOMAIN) && defined(CONFIG_IOMMU_DMA_RESERVED) + { + struct vfio_msi_map_info msi_info; + int ret; + + ret = vfio_compute_msi_map_info(iommu, &msi_info); + if (ret) + return ret; + + if (msi_info.mapping_required) + info.flags |= VFIO_IOMMU_INFO_REQUIRE_MSI_MAP; + info.msi_iova_pages = msi_info.iova_pages; + } +#endif info.iova_pgsizes = vfio_pgsize_bitmap(iommu); return copy_to_user((void __user *)arg, &info, minsz) ? diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index a49be8a..e3e501c 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -488,7 +488,9 @@ struct vfio_iommu_type1_info { __u32 argsz; __u32 flags; #define VFIO_IOMMU_INFO_PGSIZES (1 << 0) /* supported page sizes info */ +#define VFIO_IOMMU_INFO_REQUIRE_MSI_MAP (1 << 1)/* MSI must be mapped */ __u64 iova_pgsizes; /* Bitmap of supported page sizes */ + __u32 msi_iova_pages; /* number of IOVA pages needed to map MSIs */ }; #define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)