Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4134401rwb; Mon, 31 Jul 2023 01:46:49 -0700 (PDT) X-Google-Smtp-Source: APBJJlEfoDaqH9D3m6ekC2HdIoRhX2l+XsxqTNLGYo2No3hGRC7uh2ooOVRa49IEejQchBXfhSK6 X-Received: by 2002:a05:6402:1a27:b0:521:7ab6:b95d with SMTP id be7-20020a0564021a2700b005217ab6b95dmr7664252edb.29.1690793209478; Mon, 31 Jul 2023 01:46:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690793209; cv=none; d=google.com; s=arc-20160816; b=BiZhfyypflf0K0GNoGrLcKpP1sevBDaL9f8LOn6oOJu9X4m88RiVGxogjwjFAbo5Ps iLRruR6eI5XSxFbwOApXAiDGk4OyOfPov29MDF6kBWrvRImWbhKBL0TOnW+Nn23yl4ZN WCdNS5JAtka/bvi58y6Xlr0adNUHwfn7iiwQiIJbS232mglMhU+MN4YbYXPzL38UVLgz cyB4dYU77kGj7igmWEhoLyM4KhDPHQqCyF+Xq7zBMI3VkIXaZCjJHVbBJMkwInhcrHgB ce7CYvBAy0Iop2xWWd9kvhAta1HN6yxs7jPVMxxYETApcGRqVf5p956cODsR0XcECDhg g/rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=uHauh0efPqBTqf56QAv8YjVP272Ig9QLMnZTsT29Eps=; fh=mKfyWPDLNt63b4x10yZlJ3I4atEj/UBaPo0F9StBEvQ=; b=hihr6qR2g+D8htLSiD+1fZTjxbeRQ9ZlG45XlzdN1nvbFIbIb/kofDIaplRfhEdWA5 sf0oAffK5TLw2yghpX5WntIerzwIDP6NNIQsaJaluZa/0lnLVGf0nImRZedCN+7Lc03l 9DyJ8adH7qGsyxOlkfHxZOpQMBgOAliPXXH1dQ8yW9Cle7E2NHu6OxjQdvTuzFc4Nhju YFFEg+wp5r6sgGCYKmeiYU6AXrE3j3nTyHX0Vg7POWoTLE57sPHrYtZLlOsM3WBlPSMQ FTWZovCp8hh5oEA/Gi9DonH5WrFChx64J5VnS7Wai0h7SSj1m2VXR6beKhitzAs6TBBK h0kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=TulGKMcX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o23-20020aa7c517000000b0051e1c495a0csi6536733edq.376.2023.07.31.01.46.25; Mon, 31 Jul 2023 01:46:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=TulGKMcX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231966AbjGaIOq (ORCPT + 99 others); Mon, 31 Jul 2023 04:14:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229504AbjGaIOM (ORCPT ); Mon, 31 Jul 2023 04:14:12 -0400 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11752183 for ; Mon, 31 Jul 2023 01:12:25 -0700 (PDT) Received: by mail-io1-xd2c.google.com with SMTP id ca18e2360f4ac-78bb3ff7cbcso160210939f.3 for ; Mon, 31 Jul 2023 01:12:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1690791145; x=1691395945; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=uHauh0efPqBTqf56QAv8YjVP272Ig9QLMnZTsT29Eps=; b=TulGKMcX321aWapxcZccg9DOsP4HNkn2WTblLDm26pSR0fGnsJcdGRD/TzCyDxqhHk mU+S8phtTNqeqm4bZD/j4KV4vQSUH//JL9+fD5b4MphnWCAlkYaR9wD4xTeeBtI3WlXE heYDFjVKSD7pk1bpwKDno2Kcqc6I1ycWAwNIkqP4zXl+ZV583LOlYYbCfHsd6g0T10BY fIfcJLYPvQNBnFWYQ3NoqCsorXChKjAmIBR/M6NDJIp78N3k6H+HJtCawVHNJpZrReEA 8czg7aUa2sUuR2yiInxZ0cG1A7XahnbXvkiqGSgY5S342d4vdSy/jzvDHDFodXku/YQx 23Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690791145; x=1691395945; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uHauh0efPqBTqf56QAv8YjVP272Ig9QLMnZTsT29Eps=; b=CoG1CDTCjCFeErD0ObPCTX114G9WsH1PMiNt897L4xZG+8s3JNNXufWKcPwdrboXQF Qjq96dMd1Dp58ZOywCUsAoRi082rR6PVRK/3xZoCm149rPM2Pxmlk27fZOZw35kreXMK Tx+SQoHhjuY7ix7bFCRJTGfruo16Q1ROvfm5OdUfTLI2b57M0QC/aNsZhZncwARY8sWq aLGnnhCkny5iDvSg1NUq9rfQEdXZixdxSlDeHfIrqEDCu8yN+/3HbEASTeqoABENdFz7 qAUjNcLzwwf6uyfgRAapdeO2ayPWRZ9XqITHWKDk941NpPBfk/TFfWRL1Rhr+lBm+BWL 8oOA== X-Gm-Message-State: ABy/qLbUkQN6jbe57KsTchrePfrzRx2sVw63jOGjF4a15UN5eZCuKeK7 9B0j6AzjQf6aATO8SijVI7A/Z+k3/SvYHeD/QkY71vPqEKXXSsXx9uI= X-Received: by 2002:a92:cda5:0:b0:348:cd6b:d593 with SMTP id g5-20020a92cda5000000b00348cd6bd593mr7885407ild.27.1690791144902; Mon, 31 Jul 2023 01:12:24 -0700 (PDT) MIME-Version: 1.0 References: <0c391072fe0be52b3bdf3d826e4313d960aecba0.1689792825.git.tjeznach@rivosinc.com> In-Reply-To: <0c391072fe0be52b3bdf3d826e4313d960aecba0.1689792825.git.tjeznach@rivosinc.com> From: Zong Li Date: Mon, 31 Jul 2023 16:12:14 +0800 Message-ID: Subject: Re: [PATCH 11/11] RISC-V: drivers/iommu/riscv: Add G-Stage translation support To: Tomasz Jeznach Cc: Joerg Roedel , Will Deacon , Robin Murphy , Paul Walmsley , Anup Patel , Albert Ou , linux@rivosinc.com, linux-kernel@vger.kernel.org, Sebastien Boeuf , iommu@lists.linux.dev, Palmer Dabbelt , Nick Kossifidis , linux-riscv@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 20, 2023 at 3:34=E2=80=AFAM Tomasz Jeznach wrote: > > This change introduces 2nd stage translation configuration > support, enabling nested translation for IOMMU hardware. > Pending integration with VMM IOMMUFD interfaces to manage > 1st stage translation and IOMMU virtialization interfaces. > > Signed-off-by: Tomasz Jeznach > --- > drivers/iommu/riscv/iommu.c | 58 ++++++++++++++++++++++++++++--------- > drivers/iommu/riscv/iommu.h | 3 +- > 2 files changed, 46 insertions(+), 15 deletions(-) > > diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c > index 7b3e3e135cf6..3ca2f0194d3c 100644 > --- a/drivers/iommu/riscv/iommu.c > +++ b/drivers/iommu/riscv/iommu.c > @@ -1418,6 +1418,19 @@ static struct iommu_domain *riscv_iommu_domain_all= oc(unsigned type) > return &domain->domain; > } > > +/* mark domain as second-stage translation */ > +static int riscv_iommu_enable_nesting(struct iommu_domain *iommu_domain) > +{ > + struct riscv_iommu_domain *domain =3D iommu_domain_to_riscv(iommu= _domain); > + > + mutex_lock(&domain->lock); > + if (list_empty(&domain->endpoints)) > + domain->g_stage =3D true; > + mutex_unlock(&domain->lock); > + > + return domain->g_stage ? 0 : -EBUSY; > +} > + > static void riscv_iommu_domain_free(struct iommu_domain *iommu_domain) > { > struct riscv_iommu_domain *domain =3D iommu_domain_to_riscv(iommu= _domain); > @@ -1433,7 +1446,7 @@ static void riscv_iommu_domain_free(struct iommu_do= main *iommu_domain) > free_io_pgtable_ops(&domain->pgtbl.ops); > > if (domain->pgd_root) > - free_pages((unsigned long)domain->pgd_root, 0); > + free_pages((unsigned long)domain->pgd_root, domain->g_sta= ge ? 2 : 0); > > if ((int)domain->pscid > 0) > ida_free(&riscv_iommu_pscids, domain->pscid); > @@ -1483,7 +1496,8 @@ static int riscv_iommu_domain_finalize(struct riscv= _iommu_domain *domain, > > /* TODO: Fix this for RV32 */ > domain->mode =3D satp_mode >> 60; > - domain->pgd_root =3D (pgd_t *) __get_free_pages(GFP_KERNEL | __GF= P_ZERO, 0); > + domain->pgd_root =3D (pgd_t *) __get_free_pages(GFP_KERNEL | __GF= P_ZERO, > + domain->g_stage ? 2= : 0); > > if (!domain->pgd_root) > return -ENOMEM; > @@ -1499,6 +1513,8 @@ static u64 riscv_iommu_domain_atp(struct riscv_iomm= u_domain *domain) > u64 atp =3D FIELD_PREP(RISCV_IOMMU_DC_FSC_MODE, domain->mode); > if (domain->mode !=3D RISCV_IOMMU_DC_FSC_MODE_BARE) > atp |=3D FIELD_PREP(RISCV_IOMMU_DC_FSC_PPN, virt_to_pfn(d= omain->pgd_root)); > + if (domain->g_stage) > + atp |=3D FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain-= >pscid); > return atp; > } > > @@ -1541,20 +1557,30 @@ static int riscv_iommu_attach_dev(struct iommu_do= main *iommu_domain, struct devi > if (!dc) > return -ENODEV; > > - /* > - * S-Stage translation table. G-Stage remains unmodified (BARE). > - */ > - val =3D FIELD_PREP(RISCV_IOMMU_DC_TA_PSCID, domain->pscid); > - > - if (ep->pasid_enabled) { > - ep->pc[0].ta =3D cpu_to_le64(val | RISCV_IOMMU_PC_TA_V); > - ep->pc[0].fsc =3D cpu_to_le64(riscv_iommu_domain_atp(doma= in)); > + if (domain->g_stage) { > + /* > + * Enable G-Stage translation with initial pass-through m= ode > + * for S-Stage. VMM is responsible for more restrictive > + * guest VA translation scheme configuration. > + */ > dc->ta =3D 0; > - dc->fsc =3D cpu_to_le64(virt_to_pfn(ep->pc) | > - FIELD_PREP(RISCV_IOMMU_DC_FSC_MODE, RISCV_IOMMU_DC_FS= C_PDTP_MODE_PD8)); > + dc->fsc =3D 0ULL; /* RISCV_IOMMU_DC_FSC_MODE_BARE */ ; > + dc->iohgatp =3D cpu_to_le64(riscv_iommu_domain_atp(domain= )); > } else { > - dc->ta =3D cpu_to_le64(val); > - dc->fsc =3D cpu_to_le64(riscv_iommu_domain_atp(domain)); > + /* S-Stage translation table. G-Stage remains unmodified.= */ > + if (ep->pasid_enabled) { > + val =3D FIELD_PREP(RISCV_IOMMU_DC_TA_PSCID, domai= n->pscid); > + ep->pc[0].ta =3D cpu_to_le64(val | RISCV_IOMMU_PC= _TA_V); > + ep->pc[0].fsc =3D cpu_to_le64(riscv_iommu_domain_= atp(domain)); > + dc->ta =3D 0; > + val =3D FIELD_PREP(RISCV_IOMMU_DC_FSC_MODE, > + RISCV_IOMMU_DC_FSC_PDTP_MODE_PD= 8); > + dc->fsc =3D cpu_to_le64(val | virt_to_pfn(ep->pc)= ); > + } else { > + val =3D FIELD_PREP(RISCV_IOMMU_DC_TA_PSCID, domai= n->pscid); > + dc->ta =3D cpu_to_le64(val); > + dc->fsc =3D cpu_to_le64(riscv_iommu_domain_atp(do= main)); > + } > } > > wmb(); > @@ -1599,6 +1625,9 @@ static int riscv_iommu_set_dev_pasid(struct iommu_d= omain *iommu_domain, > if (!iommu_domain || !iommu_domain->mm) > return -EINVAL; > > + if (domain->g_stage) > + return -EINVAL; > + > /* Driver uses TC.DPE mode, PASID #0 is incorrect. */ > if (pasid =3D=3D 0) > return -EINVAL; > @@ -1969,6 +1998,7 @@ static const struct iommu_domain_ops riscv_iommu_do= main_ops =3D { > .iotlb_sync =3D riscv_iommu_iotlb_sync, > .iotlb_sync_map =3D riscv_iommu_iotlb_sync_map, > .flush_iotlb_all =3D riscv_iommu_flush_iotlb_all, > + .enable_nesting =3D riscv_iommu_enable_nesting, > }; > I don't see the GVMA invalidate command, I guess we need do something likes that in 'riscv_iommu_mm_invalidate' > static const struct iommu_ops riscv_iommu_ops =3D { > diff --git a/drivers/iommu/riscv/iommu.h b/drivers/iommu/riscv/iommu.h > index 55418a1144fb..55e5aafea5bc 100644 > --- a/drivers/iommu/riscv/iommu.h > +++ b/drivers/iommu/riscv/iommu.h > @@ -102,8 +102,9 @@ struct riscv_iommu_domain { > struct riscv_iommu_device *iommu; > > unsigned mode; /* RIO_ATP_MODE_* enum */ > - unsigned pscid; /* RISC-V IOMMU PSCID */ > + unsigned pscid; /* RISC-V IOMMU PSCID / GSCID */ > ioasid_t pasid; /* IOMMU_DOMAIN_SVA: Cached PASID */ > + bool g_stage; /* 2nd stage translation domain */ > > pgd_t *pgd_root; /* page table root pointer */ > }; > -- > 2.34.1 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv