From patchwork Fri Feb 26 17:35:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8440481 Return-Path: X-Original-To: patchwork-linux-arm@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 433B1C0553 for ; Fri, 26 Feb 2016 17:46:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2291220395 for ; Fri, 26 Feb 2016 17:46:49 +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 04A4D2035E for ; Fri, 26 Feb 2016 17:46:48 +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 1aZMRo-0001h1-U0; Fri, 26 Feb 2016 17:45:00 +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 1aZMK5-0002CZ-BK for linux-arm-kernel@lists.infradead.org; Fri, 26 Feb 2016 17:37:12 +0000 Received: by mail-wm0-x234.google.com with SMTP id g62so79676386wme.0 for ; Fri, 26 Feb 2016 09:36: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=q8J3iprjh0QdSIhB818mRhzMYr6hJwEkYxq5JFaK1wk=; b=XRI2/L20EWnzU12mbaqPqMcYDRvO8UuHPFOd9OG7cNyphyTM4x3NuoHhxGq8V4r4re T+IshWiD6Pvq59FQEb6ZTleiUXNaY43eKhxp3EbCh6HeZEX+QdxvjK3QHMe+GAFBC2JD dlUe9DP2V3O6mzS1LA38fPbvX3CqiG+Q4yE6I= 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=q8J3iprjh0QdSIhB818mRhzMYr6hJwEkYxq5JFaK1wk=; b=dlpwdhE8ylVZoJdOhtnWw1BfS6tZG3tV6Nru7ni7VOpECjZWLrxEhSE+6gM0k6Cdjr irw9y32eLmSDF/trLOeOwGf0urYW6PkgxaOvTV7KwL71skenuDgRPNOPDu2198Jo0FoT u2RA/mcrvmqmHBpn6G+fVsCM/imb1kbtn7kPKZIoMIBfpa1w442U4MUnMv0uKmJJoJ4w H1DY/cc8wLWAIw66bHb+/yosWun5Puo7ZCT0WAaXYU7kws+mywlR9URVogvrr0ZPHFKl Hba9TDS/5L+E9ZovvTeheA3C9y3KGWu/ehsXxwcqD7QVENYtVl654uGp6fxGJ26oRNak N0Eg== X-Gm-Message-State: AD7BkJL89DsDWlbjIcEMXAeCzkk5at0aXKJVwfgelH13TO2yVEs2reexEPbwfusMNZt+GXlw X-Received: by 10.28.23.196 with SMTP id 187mr3652909wmx.17.1456508204163; Fri, 26 Feb 2016 09:36:44 -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.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Feb 2016 09:36:42 -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 14/14] vfio/type1: return MSI mapping requirements with VFIO_IOMMU_GET_INFO Date: Fri, 26 Feb 2016 17:35:54 +0000 Message-Id: <1456508154-2253-15-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_093701_812161_6B53DE18 X-CRM114-Status: GOOD ( 21.45 ) 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 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. 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 | 146 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 2 + 2 files changed, 148 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 88a40f1..47aad41 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,18 @@ 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; + size_t page_size; + 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 +280,128 @@ 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->page_size = 1 << __ffs(bitmap); + 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 @@ -1168,6 +1303,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, } } else if (cmd == VFIO_IOMMU_GET_INFO) { struct vfio_iommu_type1_info info; + struct vfio_msi_map_info msi_info; + int ret; minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes); @@ -1179,6 +1316,15 @@ 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) + 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 d5a48e7..863c68a 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -400,7 +400,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)