Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp235pxb; Fri, 15 Jan 2021 04:20:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJxuErXngqp0hdT4t7EJxz5j1rT6pHNdDd3vzUm+XhXyk5D9yw9lLorJdOW89IRwGC/nr6Mi X-Received: by 2002:a05:6402:4c1:: with SMTP id n1mr9452643edw.66.1610713211573; Fri, 15 Jan 2021 04:20:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610713211; cv=none; d=google.com; s=arc-20160816; b=JCdpBIsFZl55irZ8/0WB52BXcRX2GG4y9DDCeHd/qWhGc6poTC/aCMecannk3v4pSO F/QAQNPJcz4u5cpg/m4s19N6zCAdIy9nXAJ15SsxabUGwDSSWjfPwvMshzXzPBNacJ6s Tb5wwzrzNTmovGHxkAnRBBXm6xdi77OMx8I4C/hR12Dg3lhhpGXkIwyVsZan6AbqHktr CC+yqAhL5Nc9sypAw5NdLySU/e4eQuw+tnEwSAk2sOT9czIQ1vuFkrwludA34iuad0AN vFBZYGxwjYqrNvrpcumqQuh31jd/jkmf9c0wESDvTtOQn6zKXftMVelJHykUig3GZXQ+ IVYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=hA5EhjJM+hXi0ADUfsbthjgHQnBae4rfy+Odk6b3lhc=; b=AD1m7d4kbqLAdOkcXm/qDw7d408hoEk8EmIp5/ah/HK1afbItpSMDI8ZpiRiyNyBzb EmaeFccZmxMzLiO5iE6NY6OS7iXMO12Hsm1TG39PrDbTcGLrClucknuJA5hN+HH+rQ2P SHBDPk+KYqyCiGqWNoRGSfoSEMm7sZOQJfrk11kDNEEfORcbXguNE+LC0/2789DlSm2l pf9GBqHDi6XQe2AKPgXHTAbmIGJFYaG2IS/mTL9pzLOOVTYS1w8WkyYvsFzgspiOQLx0 l5OMORb+oj1kBpTAXoN6ugMEkx6NDokwRqDwWnn1Tik5uNpaFHAQ/RRtHShzYJ9Vd/Go cJXg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id di28si3943103edb.383.2021.01.15.04.19.47; Fri, 15 Jan 2021 04:20:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731333AbhAOMPm (ORCPT + 99 others); Fri, 15 Jan 2021 07:15:42 -0500 Received: from foss.arm.com ([217.140.110.172]:37770 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731207AbhAOMPk (ORCPT ); Fri, 15 Jan 2021 07:15:40 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 57C4214F6; Fri, 15 Jan 2021 04:14:52 -0800 (PST) Received: from usa.arm.com (a074945.blr.arm.com [10.162.16.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id D41F23F70D; Fri, 15 Jan 2021 04:14:47 -0800 (PST) From: Vivek Gautam To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, iommu@lists.linux-foundation.org, virtualization@lists.linux-foundation.org Cc: joro@8bytes.org, will.deacon@arm.com, mst@redhat.com, robin.murphy@arm.com, jean-philippe@linaro.org, eric.auger@redhat.com, alex.williamson@redhat.com, kevin.tian@intel.com, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, lorenzo.pieralisi@arm.com, shameerali.kolothum.thodi@huawei.com, vivek.gautam@arm.com Subject: [PATCH RFC v1 12/15] iommu/virtio: Add support for INVALIDATE request Date: Fri, 15 Jan 2021 17:43:39 +0530 Message-Id: <20210115121342.15093-13-vivek.gautam@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210115121342.15093-1-vivek.gautam@arm.com> References: <20210115121342.15093-1-vivek.gautam@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jean-Philippe Brucker Add support for tlb invalidation ops that can send invalidation requests to back-end virtio-iommu when stage-1 page tables are supported. Signed-off-by: Jean-Philippe Brucker [Vivek: Refactoring the iommu_flush_ops, and adding only one pasid sync op that's needed with current iommu-pasid-table infrastructure. Also updating uapi defines as required by latest changes] Signed-off-by: Vivek Gautam Cc: Joerg Roedel Cc: Will Deacon Cc: Michael S. Tsirkin Cc: Robin Murphy Cc: Jean-Philippe Brucker Cc: Eric Auger Cc: Alex Williamson Cc: Kevin Tian Cc: Jacob Pan Cc: Liu Yi L Cc: Lorenzo Pieralisi Cc: Shameerali Kolothum Thodi --- drivers/iommu/virtio-iommu.c | 95 ++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index ae5dfd3f8269..004ea94e3731 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +64,8 @@ struct viommu_mapping { }; struct viommu_mm { + int pasid; + u64 archid; struct io_pgtable_ops *ops; struct viommu_domain *domain; }; @@ -692,6 +695,98 @@ static void viommu_event_handler(struct virtqueue *vq) virtqueue_kick(vq); } +/* PASID and pgtable APIs */ + +static void __viommu_flush_pasid_tlb_all(struct viommu_domain *vdomain, + int pasid, u64 arch_id, int type) +{ + struct virtio_iommu_req_invalidate req = { + .head.type = VIRTIO_IOMMU_T_INVALIDATE, + .inv_gran = cpu_to_le32(VIRTIO_IOMMU_INVAL_G_PASID), + .flags = cpu_to_le32(VIRTIO_IOMMU_INVAL_F_PASID), + .inv_type = cpu_to_le32(type), + + .domain = cpu_to_le32(vdomain->id), + .pasid = cpu_to_le32(pasid), + .archid = cpu_to_le64(arch_id), + }; + + if (viommu_send_req_sync(vdomain->viommu, &req, sizeof(req))) + pr_debug("could not send invalidate request\n"); +} + +static void viommu_flush_tlb_add(struct iommu_iotlb_gather *gather, + unsigned long iova, size_t granule, + void *cookie) +{ + struct viommu_mm *viommu_mm = cookie; + struct viommu_domain *vdomain = viommu_mm->domain; + struct iommu_domain *domain = &vdomain->domain; + + iommu_iotlb_gather_add_page(domain, gather, iova, granule); +} + +static void viommu_flush_tlb_walk(unsigned long iova, size_t size, + size_t granule, void *cookie) +{ + struct viommu_mm *viommu_mm = cookie; + struct viommu_domain *vdomain = viommu_mm->domain; + struct virtio_iommu_req_invalidate req = { + .head.type = VIRTIO_IOMMU_T_INVALIDATE, + .inv_gran = cpu_to_le32(VIRTIO_IOMMU_INVAL_G_VA), + .inv_type = cpu_to_le32(VIRTIO_IOMMU_INV_T_IOTLB), + .flags = cpu_to_le32(VIRTIO_IOMMU_INVAL_F_ARCHID), + + .domain = cpu_to_le32(vdomain->id), + .pasid = cpu_to_le32(viommu_mm->pasid), + .archid = cpu_to_le64(viommu_mm->archid), + .virt_start = cpu_to_le64(iova), + .nr_pages = cpu_to_le64(size / granule), + .granule = ilog2(granule), + }; + + if (viommu_add_req(vdomain->viommu, &req, sizeof(req))) + pr_debug("could not add invalidate request\n"); +} + +static void viommu_flush_tlb_all(void *cookie) +{ + struct viommu_mm *viommu_mm = cookie; + + if (!viommu_mm->archid) + return; + + __viommu_flush_pasid_tlb_all(viommu_mm->domain, viommu_mm->pasid, + viommu_mm->archid, + VIRTIO_IOMMU_INV_T_IOTLB); +} + +static struct iommu_flush_ops viommu_flush_ops = { + .tlb_flush_all = viommu_flush_tlb_all, + .tlb_flush_walk = viommu_flush_tlb_walk, + .tlb_add_page = viommu_flush_tlb_add, +}; + +static void viommu_flush_pasid(void *cookie, int pasid, bool leaf) +{ + struct viommu_domain *vdomain = cookie; + struct virtio_iommu_req_invalidate req = { + .head.type = VIRTIO_IOMMU_T_INVALIDATE, + .inv_gran = cpu_to_le32(VIRTIO_IOMMU_INVAL_G_PASID), + .inv_type = cpu_to_le32(VIRTIO_IOMMU_INV_T_PASID), + .flags = cpu_to_le32(VIRTIO_IOMMU_INVAL_F_PASID), + + .domain = cpu_to_le32(vdomain->id), + .pasid = cpu_to_le32(pasid), + }; + + if (leaf) + req.flags |= cpu_to_le32(VIRTIO_IOMMU_INVAL_F_LEAF); + + if (viommu_send_req_sync(vdomain->viommu, &req, sizeof(req))) + pr_debug("could not send invalidate request\n"); +} + /* IOMMU API */ static struct iommu_domain *viommu_domain_alloc(unsigned type) -- 2.17.1