From patchwork Fri Dec 13 11:42:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Lu X-Patchwork-Id: 13906933 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 13449E7717D for ; Fri, 13 Dec 2024 11:42:52 +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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=PWiFjcH+lkMmfM0xrtRosmpbQQbfT5FY7F+ghlYABgI=; b=hyctRyHI+ykx5n t701ZG8ijjW51VCEs/WwifBdd0F2TFOE/19ZsS1PzTaBVk6tn63AsIUqCaFMvKrYM3O03+PKeI0m6 1xqZvG70p1Uc2QDLPG2gUPlsQPEfnrAxBrEVFjWrcnA+Dr7oScd9qft2Lx+HHIM4SbdDwmzU444Dg LwQOpZmiXFwbWPNVJ3iiJpLK1FVE6eLYZyPZzSNKt/rrNKdw7BzzRzSBY084+A5KcXQQVD8LnVSoc N/nhtHKDvg/AC0127dFpPprIyLJktiPbhoAUAFMcPh8Q+6LHNd/R8NQP2IS/kSv4rOFdQGPsTRuZY 0ojyIgbk6LrVPCwHj3cQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tM44G-00000003bmF-0Xs9; Fri, 13 Dec 2024 11:42:48 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tM44D-00000003blN-1luk for linux-riscv@lists.infradead.org; Fri, 13 Dec 2024 11:42:46 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-728e729562fso1348365b3a.0 for ; Fri, 13 Dec 2024 03:42:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734090164; x=1734694964; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LeEQjuKWeFN2ZDkGY720uCzhuIIhCR6ezIvsDMVpnR4=; b=eBSau4fXiM2RhmFqPp+l8dQRPnZo4VRi/F59Ms1njEiTvNki73wUpMGrhCdlcQLje+ n4KsdFizRe850fQ+b7ffpBVlcXFlJM2HaBOtl18DrBKihDvPA/qyx+ayxGIKDx1PSOCI z9k/67hzqVMSsrJNHiocYQ2FIN0VCXRHPPs9UFu6u9/0QmB8F86ZJiIT43F6HZoIl4Wk iaji21+RO0GazAi/Brur5PS/cU3asKvkR4iWZrmiFPta1kb3Cw2EbrUJBUesVEb7oIRW 1iX6Wo2cbJJl5INuMlmoOCItfTPr2GzvOaFQn7v9H/5M4JLVTsHJ1zrhaAL4YD3tCR9j jfnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734090164; x=1734694964; h=content-transfer-encoding:mime-version: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=LeEQjuKWeFN2ZDkGY720uCzhuIIhCR6ezIvsDMVpnR4=; b=CF1e8nhFL/4J3LDfIUXKiCaohGEnWXEm/UWYhUMRQIvLlNHw12OcIJ9nIKpdibQhj0 Y5qV00GeyS4UmZ56z/aHCw3QjeG1JVS6no/4EAVZsB+TkVDJEId+RKHcTPI1GMUMrV94 oT7taosGtZdzAXJN/aP08AzZppOg99EsdLxFo8CF4S8FQzEuekTbiXvZVuUjXyhKjZWb dSmQF5ZNjFVBGUkLtkWxzEMv5TWN2Q16X7bvuqh8SgdJ+WDbqxFTGxDyubqUMX2UqUzI fMPiEsZ4A1ZojsZ9os3PRN4f2p66B55G1J8VimW57hGwwgjvX3Ui1+/oWUL8Ail0ppck AiwQ== X-Forwarded-Encrypted: i=1; AJvYcCU+mExWDsFjTMIyfDkk3zSvROkEFnl2gev3FIQzEo46MclHBZTpXGh5MFDwlQhCQwyTQE4PiBkh4H7eKg==@lists.infradead.org X-Gm-Message-State: AOJu0YyOEa0l563ftqxJfgmMGzj0a3zecby+UsAHltquslzl18iHTd6z lV3aCqPZ6MoBznSoQLp87XOoj9zNy+l0cwyFuDMBwHGn/Ulq3V/n4P378DExT1U= X-Gm-Gg: ASbGnct+e7yWFcFDnstmB6chdFs1070bhKX5E+NRO7mjznylGaoBZCiuu7QK49zULL6 4VPavMM20/EgwsIW0RGYwnLbCYCt6FSS9m1s91UwvvA4wsVodn+74L63h3cPQ/jLKVHpHTb9MI0 1trqDeT1vxNBznyitmH+GG1RZwCkx1ZoQyvgClnkruMhtfSw6slPJm7lLg6+kQbyjvM6byPuzck 7teB2BnhN8VRqBNPlkixiqQBaMDnJZ+gGYugCMMeBib45hJpLq9AcJgT7m9yPRvDgge+X/O8/C9 ZC9rnDCiWc/CN1nHVfYrf0+dUx7rLvPbOKrlmXg= X-Google-Smtp-Source: AGHT+IGeWdd36o5upIqzrwhc6coASaeu5E8HRRrsujN3Aueo0WxZt2dbtsoJCJze86+/n/dSVtetAg== X-Received: by 2002:a05:6a20:43a0:b0:1e0:cbd1:8046 with SMTP id adf61e73a8af0-1e1dfaa0ba4mr3644498637.0.1734090164609; Fri, 13 Dec 2024 03:42:44 -0800 (PST) Received: from J9GPGXL7NT.bytedance.net ([61.213.176.56]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4b961f1asm8666340a12.30.2024.12.13.03.42.41 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 13 Dec 2024 03:42:44 -0800 (PST) From: Xu Lu To: tjeznach@rivosinc.com, joro@8bytes.org, will@kernel.org Cc: lihangjing@bytedance.com, xieyongji@bytedance.com, guojinhui.liam@bytedance.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Xu Lu Subject: [PATCH 1/2] iommu/riscv: Empty iommu queue before enabling it Date: Fri, 13 Dec 2024 19:42:32 +0800 Message-Id: <20241213114233.12388-2-luxu.kernel@bytedance.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20241213114233.12388-1-luxu.kernel@bytedance.com> References: <20241213114233.12388-1-luxu.kernel@bytedance.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241213_034245_458066_2D625D7B X-CRM114-Status: UNSURE ( 8.59 ) X-CRM114-Notice: Please train this message. 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: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Changing cqen/fqen/pqen from 0 to 1 sets the cqh/fqt/pqt registers to 0. But the cqt/fqh/pqh registers are left unmodified. This commit resets cqt/fqh/pqh registers to ensure corresponding queues are empty before being enabled during initialization. Signed-off-by: Xu Lu --- drivers/iommu/riscv/iommu.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index 8a05def774bd..84806724f568 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -240,6 +240,12 @@ static int riscv_iommu_queue_enable(struct riscv_iommu_device *iommu, return rc; } + /* Empty queue before enabling it */ + if (queue->qid == RISCV_IOMMU_INTR_CQ) + riscv_iommu_writel(queue->iommu, Q_TAIL(queue), 0); + else + riscv_iommu_writel(queue->iommu, Q_HEAD(queue), 0); + /* * Enable queue with interrupts, clear any memory fault if any. * Wait for the hardware to acknowledge request and activate queue From patchwork Fri Dec 13 11:42:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Lu X-Patchwork-Id: 13906934 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 2CFC7E77180 for ; Fri, 13 Dec 2024 11:42:56 +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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=oXENyGnx4Babcl/B/1VljMba1RZHrffUhUzK6r2yhCU=; b=iqaxiGTrHy77Ym MXAF2rdnA7kyKgsqD5Mhv385DAc68nyzEN4JPXFsMNiOinmieUUG4SsSzYy5t4UkX8asOBzu/s5bR 7sDOlRHyWf5cj0bbiccdji/jhaYuuOCGHJap7noXMaVeJON6gmkS61MJZkqEP9o1Zav55imP1+MMW sfwupcoam7g/RWcgLfh/3N1c/RLdb+n+EDH5R9pnCZiKFTLVVz70/Wf/QrHa/Al2l2j3eT+hpE29N 5LLafPa6+i2Iop2VYrEbjIMTuhWR7kBXTHV7/9jcuC8uWTKOggiCXSthMg6kAja+tKR6fZXJuvWcW cv7mh3TTw8Mz87+E0+TQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tM44J-00000003boL-2ntM; Fri, 13 Dec 2024 11:42:51 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tM44G-00000003bmV-3ome for linux-riscv@lists.infradead.org; Fri, 13 Dec 2024 11:42:49 +0000 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-728eedfca37so1772205b3a.2 for ; Fri, 13 Dec 2024 03:42:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734090168; x=1734694968; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2nzXBeY/zx1suskgdf8fbpsAzoYK9okK//XYDozr9/c=; b=OK6LbN1oSttZle5Yq4TVnEQXdmnBZ1rAK3fVZZJWhSs9jbRRa7gEQT//yjkoYOLRMQ S6thRG3/JeVja6kIoyV3bXFij+o895KKZWprs8ydFxFVNUEUeJcoSCVjEyFrEw7L11yG 5+lvNpHIJchLvi4Z3DBEEzIl/mHF3IXakOdm/iorX+GlFF/YxUwNlxPmYwWGEn4dFFG5 FdxsiCcLxN5nQxaLg8K/i8c7rfnNeeNM9IWTGaLq/e5AfxWt4eaLdl0sh/4W2KdbrOag FfD1lYyu/1bKjSwcaB3dQHvtGaWXTq4dDjz9Hjxhjm8jWIZVL4OcYQ2aCC90Sj/nUAS2 QRYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734090168; x=1734694968; h=content-transfer-encoding:mime-version: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=2nzXBeY/zx1suskgdf8fbpsAzoYK9okK//XYDozr9/c=; b=qgVW1eQ3dZEVpedjdOc9FAOiLdbAUhJqk3pizCSMvnZxUvvB+nsQE54Xu3lRJD3GRp l7UONNUisVC3rWdIi2QcGlZKoJziWC4jl0N3XgW8Zn6PXSLkCEkTzbYVrIf3U0hxvi8m bZSLMDD778yW2C5vhG9c3y4ETinDmCnSeSjZEFJEUUnBv0ygtisCJ7CfJOpDgOglVV0R LrwVSyKAfjItV5A1m2+YRquyuDNL14cCLuxpmnTufxeqSCW0kiuxXrae0qhZhtREb91H hFztjj4WiZPRKAE4h5Gt576qfBtwZtFIZz5tVb76mfclKJmLzIPND0o3mvKwp/WtKrz5 aiNA== X-Forwarded-Encrypted: i=1; AJvYcCUr2SqojoXOOuhXart7lkCFNTWvRMNNEKL2YqQKp2mWQ3XaHHCTeft3hnEKWfbVuCVFrrWVMmpRghC00w==@lists.infradead.org X-Gm-Message-State: AOJu0YziEPdnMjbx1bk9/zs6JaelLqjC7SiuDC5vKomSDXtH9eSBi4f/ qcU5YAxt1qXOzKrxfANXnBWS/b1mytSnBmSHo3zE3+vJPam1U4v/FK54QNA6yak= X-Gm-Gg: ASbGncvPDo5HRuHcyRgjIqFO1MdVT0cCne/4lx8HEiB480xJFxHT9jIte2x3lET5HTw 4Uhmal2RwRKo0z2OwtcpSRNPXV1t6AGErL2oInrqg/NusFQeXbfv3jeeP/Xqo3aiItAsyzOKDwt jVrv99g2cT19inB3o2wvt0NxjJh8//LhIIosLGRlSHq0lIluzHohG4CWQBab/sA5OWCiffLkR33 2PLc4oOUTp9gjL2z/uZfDrti+JJr04ub1yjnifXtlvp3F7CfOJlBLA70cgytyV/WcnZ8+ANx02x tBnWoaxo0BrCbU4hTreZ9phm2E8HKDmvH4VM7Yg= X-Google-Smtp-Source: AGHT+IHchEZVi5a6A4EscQE7riigGZANnu99XJHe62AE6HVH8l0CdHEKKzwg+sAunEM97R10zLGscQ== X-Received: by 2002:a05:6a20:2446:b0:1db:ec3e:c959 with SMTP id adf61e73a8af0-1e1dfd3d5c8mr4085521637.10.1734090168101; Fri, 13 Dec 2024 03:42:48 -0800 (PST) Received: from J9GPGXL7NT.bytedance.net ([61.213.176.56]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd4b961f1asm8666340a12.30.2024.12.13.03.42.45 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 13 Dec 2024 03:42:47 -0800 (PST) From: Xu Lu To: tjeznach@rivosinc.com, joro@8bytes.org, will@kernel.org Cc: lihangjing@bytedance.com, xieyongji@bytedance.com, guojinhui.liam@bytedance.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Xu Lu Subject: [PATCH 2/2] iommu/riscv: Add shutdown function for iommu driver Date: Fri, 13 Dec 2024 19:42:33 +0800 Message-Id: <20241213114233.12388-3-luxu.kernel@bytedance.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20241213114233.12388-1-luxu.kernel@bytedance.com> References: <20241213114233.12388-1-luxu.kernel@bytedance.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241213_034248_945957_7538011D X-CRM114-Status: GOOD ( 12.91 ) 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: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This commit supplies shutdown callback for iommu pmu driver. The shutdown callback resets necessary registers so that newly booted kernel can pass riscv_iommu_init_check() after kexec. Also, the shutdown callback resets iommu mode to bare instead of off so that new kernel can still use PCIE devices even when CONFIG_RISCV_IOMMU is not enabled. Signed-off-by: Xu Lu --- drivers/iommu/riscv/iommu-pci.c | 8 ++++++++ drivers/iommu/riscv/iommu-platform.c | 6 ++++++ drivers/iommu/riscv/iommu.c | 6 ++++-- drivers/iommu/riscv/iommu.h | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/riscv/iommu-pci.c b/drivers/iommu/riscv/iommu-pci.c index c7a89143014c..d82d2b00904c 100644 --- a/drivers/iommu/riscv/iommu-pci.c +++ b/drivers/iommu/riscv/iommu-pci.c @@ -101,6 +101,13 @@ static void riscv_iommu_pci_remove(struct pci_dev *pdev) riscv_iommu_remove(iommu); } +static void riscv_iommu_pci_shutdown(struct pci_dev *pdev) +{ + struct riscv_iommu_device *iommu = dev_get_drvdata(&pdev->dev); + + riscv_iommu_disable(iommu); +} + static const struct pci_device_id riscv_iommu_pci_tbl[] = { {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_RISCV_IOMMU), 0}, {PCI_VDEVICE(RIVOS, PCI_DEVICE_ID_RIVOS_RISCV_IOMMU_GA), 0}, @@ -112,6 +119,7 @@ static struct pci_driver riscv_iommu_pci_driver = { .id_table = riscv_iommu_pci_tbl, .probe = riscv_iommu_pci_probe, .remove = riscv_iommu_pci_remove, + .shutdown = riscv_iommu_pci_shutdown, .driver = { .suppress_bind_attrs = true, }, diff --git a/drivers/iommu/riscv/iommu-platform.c b/drivers/iommu/riscv/iommu-platform.c index 382ba2841849..62c40b99cf62 100644 --- a/drivers/iommu/riscv/iommu-platform.c +++ b/drivers/iommu/riscv/iommu-platform.c @@ -74,6 +74,11 @@ static void riscv_iommu_platform_remove(struct platform_device *pdev) riscv_iommu_remove(dev_get_drvdata(&pdev->dev)); }; +static void riscv_iommu_platform_shutdown(struct platform_device *pdev) +{ + riscv_iommu_disable(dev_get_drvdata(&pdev->dev)); +}; + static const struct of_device_id riscv_iommu_of_match[] = { {.compatible = "riscv,iommu",}, {}, @@ -82,6 +87,7 @@ static const struct of_device_id riscv_iommu_of_match[] = { static struct platform_driver riscv_iommu_platform_driver = { .probe = riscv_iommu_platform_probe, .remove = riscv_iommu_platform_remove, + .shutdown = riscv_iommu_platform_shutdown, .driver = { .name = "riscv,iommu", .of_match_table = riscv_iommu_of_match, diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index 84806724f568..670b4302aca8 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -651,9 +651,11 @@ static struct riscv_iommu_dc *riscv_iommu_get_dc(struct riscv_iommu_device *iomm * This is best effort IOMMU translation shutdown flow. * Disable IOMMU without waiting for hardware response. */ -static void riscv_iommu_disable(struct riscv_iommu_device *iommu) +void riscv_iommu_disable(struct riscv_iommu_device *iommu) { - riscv_iommu_writeq(iommu, RISCV_IOMMU_REG_DDTP, 0); + riscv_iommu_writeq(iommu, RISCV_IOMMU_REG_DDTP, + FIELD_PREP(RISCV_IOMMU_DDTP_IOMMU_MODE, + RISCV_IOMMU_DDTP_IOMMU_MODE_BARE)); riscv_iommu_writel(iommu, RISCV_IOMMU_REG_CQCSR, 0); riscv_iommu_writel(iommu, RISCV_IOMMU_REG_FQCSR, 0); riscv_iommu_writel(iommu, RISCV_IOMMU_REG_PQCSR, 0); diff --git a/drivers/iommu/riscv/iommu.h b/drivers/iommu/riscv/iommu.h index b1c4664542b4..46df79dd5495 100644 --- a/drivers/iommu/riscv/iommu.h +++ b/drivers/iommu/riscv/iommu.h @@ -64,6 +64,7 @@ struct riscv_iommu_device { int riscv_iommu_init(struct riscv_iommu_device *iommu); void riscv_iommu_remove(struct riscv_iommu_device *iommu); +void riscv_iommu_disable(struct riscv_iommu_device *iommu); #define riscv_iommu_readl(iommu, addr) \ readl_relaxed((iommu)->reg + (addr))