From patchwork Fri Feb 12 08:13:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8288381 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 BB2D39F3CD for ; Fri, 12 Feb 2016 08:22:19 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B2DF12035D for ; Fri, 12 Feb 2016 08:22:18 +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 A534C202EC for ; Fri, 12 Feb 2016 08:22:17 +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 1aU8xp-0003wY-AK; Fri, 12 Feb 2016 08:20:29 +0000 Received: from mail-wm0-x22c.google.com ([2a00:1450:400c:c09::22c]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aU8sI-0004np-B8 for linux-arm-kernel@lists.infradead.org; Fri, 12 Feb 2016 08:14:51 +0000 Received: by mail-wm0-x22c.google.com with SMTP id 128so51769951wmz.1 for ; Fri, 12 Feb 2016 00:14:25 -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=oLjRgxkpKqPd9vr2OD8QlGx8HqDfe5Dh5YGjJ+SpKuc=; b=fkuPCx7W1HQBNJ9jWD3Gr9UZPk6iyIVR1FnDjA7Oc8DrTv41rwLcDMFo9kArh+xIl0 6czaNUU1AAM28gckg58x3visSM/lnQ0CLh2PXbGPoMQo0YNHBtS/R+VMwkoNE1N2wcz9 GChdcJkZnsNNB9oo5E8bV5u3d2IzXfK/SjU+o= 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=oLjRgxkpKqPd9vr2OD8QlGx8HqDfe5Dh5YGjJ+SpKuc=; b=Bvo4Z+YR7+tM6AJL31Sbes1Tkqc7wsArEknzKf57+Rb/lq9d73KSwSnVA8+3p3SQyK sbOO3NL6FKZgpUYmlgpjdlznrl7tvO/NrNjt5GnzSzp09x2rEm48CJX/8A4sH14A9kva iEo9P5gr6wxtEw8J4pbqpsNalw/WNYcSGSd5gU+u4WVhyaKUEdZCTI23rXAemNXxFgyo 3lVfphAnRLx+Mao9WrlPJ8OMlF6ZrJtP/dHbb+1xfwxA+8JYKpin/SzVSjM2WbNIroy9 Bcy7sbG1YqfunK+Ay4VxbDXf4czx+atMUthVYcqTqTnTerUerk0Ou8/90C3wU3JVxf1f XEjQ== X-Gm-Message-State: AG10YOTK2Qm5j1LCfbherCxIjmnHadVkQqaoYL0Qw06DjtqNbgSmvk0alRY/JKlHQRDzdWdl X-Received: by 10.194.63.75 with SMTP id e11mr225081wjs.79.1455264864687; Fri, 12 Feb 2016 00:14:24 -0800 (PST) Received: from new-host-17.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id x66sm1243977wmb.20.2016.02.12.00.14.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Feb 2016 00:14:22 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, 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 v3 10/15] vfio: allow the user to register reserved iova range for MSI mapping Date: Fri, 12 Feb 2016 08:13:12 +0000 Message-Id: <1455264797-2334-11-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1455264797-2334-1-git-send-email-eric.auger@linaro.org> References: <1455264797-2334-1-git-send-email-eric.auger@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160212_001446_793138_A3BA42EA X-CRM114-Status: GOOD ( 20.33 ) 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: Thomas.Lendacky@amd.com, brijesh.singh@amd.com, 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, leo.duran@amd.com, suravee.suthikulpanit@amd.com, sherry.hurwitz@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.2 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 The user is allowed to register a reserved IOVA range by using the DMA MAP API and setting the new flag: VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA. It provides the base address and the size. This region is stored in the vfio_dma rb tree. At that point the iova range is not mapped to any target address yet. The host kernel will use those iova when needed, typically when the VFIO-PCI device allocates its MSI's. This patch also handles the destruction of the reserved binding RB-tree and domain's iova_domains. Signed-off-by: Eric Auger Signed-off-by: Bharat Bhushan --- v1 -> v2: - set returned value according to alloc_reserved_iova_domain result - free the iova domains in case any error occurs RFC v1 -> v1: - takes into account Alex comments, based on [RFC PATCH 1/6] vfio: Add interface for add/del reserved iova region: - use the existing dma map/unmap ioctl interface with a flag to register a reserved IOVA range. A single reserved iova region is allowed. --- drivers/vfio/vfio_iommu_type1.c | 75 ++++++++++++++++++++++++++++++++++++++++- include/uapi/linux/vfio.h | 9 +++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index b9326c9..c5d3b48 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -673,6 +673,75 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, return ret; } +static int vfio_register_reserved_iova_range(struct vfio_iommu *iommu, + struct vfio_iommu_type1_dma_map *map) +{ + dma_addr_t iova = map->iova; + size_t size = map->size; + uint64_t mask; + struct vfio_dma *dma; + int ret = 0; + struct vfio_domain *d; + unsigned long order; + + /* Verify that none of our __u64 fields overflow */ + if (map->size != size || map->iova != iova) + return -EINVAL; + + order = __ffs(vfio_pgsize_bitmap(iommu)); + mask = ((uint64_t)1 << order) - 1; + + WARN_ON(mask & PAGE_MASK); + + /* we currently only support MSI_RESERVED_IOVA */ + if (!(map->flags & VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA)) + return -EINVAL; + + if (!size || (size | iova) & mask) + return -EINVAL; + + /* Don't allow IOVA address wrap */ + if (iova + size - 1 < iova) + return -EINVAL; + + mutex_lock(&iommu->lock); + + /* check if the iova domain has not been instantiated already*/ + d = list_first_entry(&iommu->domain_list, + struct vfio_domain, next); + + if (vfio_find_dma(iommu, iova, size)) { + ret = -EEXIST; + goto out; + } + + dma = kzalloc(sizeof(*dma), GFP_KERNEL); + if (!dma) { + ret = -ENOMEM; + goto out; + } + + dma->iova = iova; + dma->size = size; + dma->type = VFIO_IOVA_RESERVED; + + list_for_each_entry(d, &iommu->domain_list, next) + ret |= iommu_alloc_reserved_iova_domain(d->domain, iova, + size, order); + + if (ret) { + list_for_each_entry(d, &iommu->domain_list, next) + iommu_free_reserved_iova_domain(d->domain); + goto out; + } + + vfio_link_dma(iommu, dma); + +out: + mutex_unlock(&iommu->lock); + return ret; +} + static int vfio_bus_type(struct device *dev, void *data) { struct bus_type **bus = data; @@ -1045,7 +1114,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, } else if (cmd == VFIO_IOMMU_MAP_DMA) { struct vfio_iommu_type1_dma_map map; uint32_t mask = VFIO_DMA_MAP_FLAG_READ | - VFIO_DMA_MAP_FLAG_WRITE; + VFIO_DMA_MAP_FLAG_WRITE | + VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA; minsz = offsetofend(struct vfio_iommu_type1_dma_map, size); @@ -1055,6 +1125,9 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, if (map.argsz < minsz || map.flags & ~mask) return -EINVAL; + if (map.flags & VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA) + return vfio_register_reserved_iova_range(iommu, &map); + return vfio_dma_do_map(iommu, &map); } else if (cmd == VFIO_IOMMU_UNMAP_DMA) { diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 43e183b..982e326 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -411,12 +411,21 @@ struct vfio_iommu_type1_info { * * Map process virtual addresses to IO virtual addresses using the * provided struct vfio_dma_map. Caller sets argsz. READ &/ WRITE required. + * + * In case MSI_RESERVED_IOVA is set, the API only aims at registering an IOVA + * region which will be used on some platforms to map the host MSI frame. + * in that specific case, vaddr and prot are ignored. The requirement for + * provisioning such IOVA range can be checked by calling VFIO_IOMMU_GET_INFO + * with the VFIO_IOMMU_INFO_REQUIRE_MSI_MAP attribute. A single + * MSI_RESERVED_IOVA region can be registered */ struct vfio_iommu_type1_dma_map { __u32 argsz; __u32 flags; #define VFIO_DMA_MAP_FLAG_READ (1 << 0) /* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1) /* writable from device */ +/* reserved iova for MSI vectors*/ +#define VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA (1 << 2) __u64 vaddr; /* Process virtual address */ __u64 iova; /* IO virtual address */ __u64 size; /* Size of mapping (bytes) */