From patchwork Fri Jun 14 14:21:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zong Li X-Patchwork-Id: 13698746 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4FE35C27C79 for ; Fri, 14 Jun 2024 14:22:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uD8puqJ0aB7gkNbxQQniBAb8KmBKchswtrLBEpxZ2h0=; b=mwhvO5By64nWbs 4GlvO9kfXeSNvF03j/5kvGnBCCcywUz2rDCdMuD1RUk2CbQwHPfaZBH8JhctcCd8fYE2q7T5oEDhb 0mN+zKgvHrX87D+UUKcotpLBmSn0QgaOn4faCE/Hy6wzM1lt1dQYZBQCymxkByjPrE43iU+UlgRUa F/SvGema+v7OFipmA3kUvLjmBqQRsqgfh1vBAhBa+Kj4t3eNINklt04kgYZZ5jL7uHx0eVBfcWenx DdJVmAd8FoHZp/SD6TqAzGkLo9OT/lo0ZcPMWvZ02j7A/FBmcYGkhgWNX2gzU0ftgSomRoaiu4ypj gQWCDVyKlTrWNAhcsIcg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sI7oz-0000000301Q-1j6B; Fri, 14 Jun 2024 14:22:29 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sI7ot-00000002zwn-3fqA for linux-riscv@lists.infradead.org; Fri, 14 Jun 2024 14:22:25 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1f44b45d6abso17889835ad.0 for ; Fri, 14 Jun 2024 07:22:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1718374942; x=1718979742; darn=lists.infradead.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=3Daef6kUbuf4+6WFIjDcTl7WQW1O4tQM7jDjBoLtjZ4=; b=a+LtSVRFql0tZowRHa9yVVCxSGbIRhnxA97CTBJVFoJvzimEmhra2iymA+kugnIpXL ZVBfu8I2aqMIYkQo7YnJJKXoA1OI0hsjT9/woyyq8jq0uG9KURpSAV7OXxLa6qO/IfQ3 hsXDY1T7isWfLfZRPsbzkEN/1Iq1PK0ht5LjVXlJRYkOgzxDeHJpqAaOTWM9octf8AEv Jsl3pM83H14ysE8OdAuSdGeEIl+EW/5qOzR3+Rz+jCw3W6tgzNRNnIl8fV/SS0XEUrje KD1C2wpKWo3NQeJW8bKjbLp3pKrv9WBQgEycbfkj9Nrs7GOwgmPQSju/94XpFo93qAaS tAoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718374942; x=1718979742; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3Daef6kUbuf4+6WFIjDcTl7WQW1O4tQM7jDjBoLtjZ4=; b=sxcactXg+qL4pvNz6qAm2VK8egrBb3p962TGy4Bk/mjCApiGVCCo03LZoORu9U9Xmm 9sZaRmB3Yrp8QYFfY3g0C/S0kYqQ8eS/C8asUb4zSpYgbEVYVK+04rs8apV/C9WvcK9s G0zGmRMeOGOacXQSzb7vV69VbugRFgH3OamaOhIiXNXFsw2iP79T3mKz9EIRsoF8re4c oE3zmyDTluunLUjkCRriH1PF4Z9Vpw85fEsOqBqI2k3NpOpOueNs5gcS0gXqyJ8b4gkN gbuYDpeE0B9TrpaHUwJupEZbBunC4764Fs1t88TF5nIEjnfDR2ltf/M4BortQy1gyROE hMEA== X-Forwarded-Encrypted: i=1; AJvYcCXiEBqNVnXg5AhssVCwptPcdBtNepBxl+/F83AidriXo0Qz/nPWxFhKuJFNUHgEb/YUqZy1IYxkEfkS9OpF9ufQHBCrGzvjGVcsE57EHPrD X-Gm-Message-State: AOJu0Yw4+pJEYkzxuFX9PIpi0NB07mM6VeXKH8rZzy1Z/l8qWgo7Vcif QhhEuRpJaK6O9hOcR+dPEqJat+vUWPii7H6xAAs27pQtjyovlgUQBeZ2tKGqL7M= X-Google-Smtp-Source: AGHT+IFqgOZYvBJ4kxh5nI8iWhYcYwWgqXcjlHpYLYmYilMYTECy6aBNWzRZ7wIFJH/yInxDq6z0lg== X-Received: by 2002:a17:903:32ca:b0:1f7:c33:aa92 with SMTP id d9443c01a7336-1f8625c06f1mr32662045ad.9.1718374942583; Fri, 14 Jun 2024 07:22:22 -0700 (PDT) Received: from hsinchu26.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855e559d9sm32522005ad.35.2024.06.14.07.22.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 07:22:22 -0700 (PDT) From: Zong Li To: joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, tjeznach@rivosinc.com, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, jgg@ziepe.ca, kevin.tian@intel.com, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-riscv@lists.infradead.org Cc: Zong Li Subject: [RFC PATCH v2 05/10] iommu/riscv: support GSCID and GVMA invalidation command Date: Fri, 14 Jun 2024 22:21:51 +0800 Message-Id: <20240614142156.29420-6-zong.li@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240614142156.29420-1-zong.li@sifive.com> References: <20240614142156.29420-1-zong.li@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240614_072223_965783_8DCAB6DD X-CRM114-Status: GOOD ( 17.59 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This patch adds a ID Allocator for GSCID and a wrap for setting up GSCID in IOTLB invalidation command. Set up iohgatp to enable second stage table and flush stage-2 table if the GSCID is set. The GSCID of domain should be freed when release domain. GSCID will be allocated for parent domain in nested IOMMU process. Signed-off-by: Zong Li --- drivers/iommu/riscv/iommu-bits.h | 7 ++++++ drivers/iommu/riscv/iommu.c | 39 ++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/riscv/iommu-bits.h b/drivers/iommu/riscv/iommu-bits.h index 60523449f016..214735a335fd 100644 --- a/drivers/iommu/riscv/iommu-bits.h +++ b/drivers/iommu/riscv/iommu-bits.h @@ -731,6 +731,13 @@ static inline void riscv_iommu_cmd_inval_vma(struct riscv_iommu_command *cmd) cmd->dword1 = 0; } +static inline void riscv_iommu_cmd_inval_gvma(struct riscv_iommu_command *cmd) +{ + cmd->dword0 = FIELD_PREP(RISCV_IOMMU_CMD_OPCODE, RISCV_IOMMU_CMD_IOTINVAL_OPCODE) | + FIELD_PREP(RISCV_IOMMU_CMD_FUNC, RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA); + cmd->dword1 = 0; +} + static inline void riscv_iommu_cmd_inval_set_addr(struct riscv_iommu_command *cmd, u64 addr) { diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index df7aeb2571ae..45309bd096e5 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -45,6 +45,10 @@ static DEFINE_IDA(riscv_iommu_pscids); #define RISCV_IOMMU_MAX_PSCID (BIT(20) - 1) +/* IOMMU GSCID allocation namespace. */ +static DEFINE_IDA(riscv_iommu_gscids); +#define RISCV_IOMMU_MAX_GSCID (BIT(16) - 1) + /* Device resource-managed allocations */ struct riscv_iommu_devres { void *addr; @@ -845,6 +849,7 @@ struct riscv_iommu_domain { struct list_head bonds; spinlock_t lock; /* protect bonds list updates. */ int pscid; + int gscid; int amo_enabled:1; int numa_node; unsigned int pgd_mode; @@ -993,20 +998,33 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, rcu_read_lock(); prev = NULL; + list_for_each_entry_rcu(bond, &domain->bonds, list) { iommu = dev_to_iommu(bond->dev); /* * IOTLB invalidation request can be safely omitted if already sent - * to the IOMMU for the same PSCID, and with domain->bonds list + * to the IOMMU for the same PSCID/GSCID, and with domain->bonds list * arranged based on the device's IOMMU, it's sufficient to check * last device the invalidation was sent to. */ if (iommu == prev) continue; - riscv_iommu_cmd_inval_vma(&cmd); - riscv_iommu_cmd_inval_set_pscid(&cmd, domain->pscid); + /* + * S2 domain needs to flush entries in stage-2 page table, its + * bond list has host devices and pass-through devices, the GVMA + * command is no effect on host devices, because there are no + * mapping of host devices in stage-2 page table. + */ + if (domain->gscid) { + riscv_iommu_cmd_inval_gvma(&cmd); + riscv_iommu_cmd_inval_set_gscid(&cmd, domain->gscid); + } else { + riscv_iommu_cmd_inval_vma(&cmd); + riscv_iommu_cmd_inval_set_pscid(&cmd, domain->pscid); + } + if (len && len < RISCV_IOMMU_IOTLB_INVAL_LIMIT) { for (iova = start; iova < end; iova += PAGE_SIZE) { riscv_iommu_cmd_inval_set_addr(&cmd, iova); @@ -1015,6 +1033,7 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, } else { riscv_iommu_cmd_send(iommu, &cmd); } + prev = iommu; } @@ -1083,6 +1102,7 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, WRITE_ONCE(dc->fsc, new_dc->fsc); WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID); + WRITE_ONCE(dc->iohgatp, new_dc->iohgatp); /* Update device context, write TC.V as the last step. */ dma_wmb(); WRITE_ONCE(dc->tc, tc); @@ -1354,6 +1374,9 @@ static void riscv_iommu_free_paging_domain(struct iommu_domain *iommu_domain) if ((int)domain->pscid > 0) ida_free(&riscv_iommu_pscids, domain->pscid); + if ((int)domain->gscid > 0) + ida_free(&riscv_iommu_gscids, domain->gscid); + riscv_iommu_pte_free(domain, _io_pte_entry(pfn, _PAGE_TABLE), NULL); kfree(domain); } @@ -1384,8 +1407,14 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain, if (!riscv_iommu_pt_supported(iommu, domain->pgd_mode)) return -ENODEV; - dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, domain->pgd_mode) | - FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, virt_to_pfn(domain->pgd_root)); + if (domain->gscid) + dc.iohgatp = FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, domain->pgd_mode) | + FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain->gscid) | + FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, virt_to_pfn(domain->pgd_root)); + else + dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, domain->pgd_mode) | + FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, virt_to_pfn(domain->pgd_root)); + dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) | RISCV_IOMMU_PC_TA_V;