From nobody Sat Jul 25 21:59:20 2026 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E841D35F61A for ; Mon, 13 Jul 2026 06:09:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783922960; cv=none; b=RdARzlX4YRPIkiqZDLdrvGFprh5zC5HwuGIOzi5nb9jljIyUHp9CGsuf9wMCsBpIZHxFCmFx+Qrv7L5p02MGc9bcmUURgo5Q8fQkfDds1UuGREyqFtNdWLxHmCqXjpDEhEDBTdCyvRFk/YAngJOogTmu5Q9+wPq8eek/WegGDVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783922960; c=relaxed/simple; bh=dknP9TWgNosCi5sh1QkgEVJLOLrIREEdfpe+SqFEniQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TWIFehlOeANrgJiAx1vwKFba7gWi46sbo7cxEl6AKu447YQSFK0/+3EmOfjnfzIXfWeimXZOQ7O2x5UrOEYoZAqr0p01sL7l2EJeQD/txKAHNg4g+8C0S+Ito3GXaYvBkhqovKY2VcCjlm7eRMf4n2MCH7t3JwPL+rE05XK+yEg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=SNXAbMiF; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="SNXAbMiF" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-38a0c7e841fso3222024a91.2 for ; Sun, 12 Jul 2026 23:09:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1783922958; x=1784527758; darn=vger.kernel.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:content-type; bh=zRVFbRCCSU313/FdRD3Vin4a5GxhaG0qQy0/ly3lbz0=; b=SNXAbMiFW3usYqDgr4fTO9Do4pK+cP2r5gyDPchwOnh4iZXYikT8q9iQr1QqyuT8n7 DSLStLM40W/3CkKYYscKjgmdinDRtkAldRlJ/e2juXLAy1MJS9uPoNMu/wPt6aSQK3XQ Yizj1mJznrBorWEm62P8US8+xKFPKe16khW3FGkeAo6aAAHkSxi230aXXfQk49xB9H3C OozOYhFzwXTACINd1alje8ehaDqOfUPI0Bpw7/Zr7cOXNZC/oktcA0+dxLnFGBLcG24W hZBsY5hptGxrZ755voZV/tMhaPCeahLF9VHehzlDV0PjjUoA1tdcgCjPUa9vi24Bx+xS zOtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783922958; x=1784527758; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zRVFbRCCSU313/FdRD3Vin4a5GxhaG0qQy0/ly3lbz0=; b=Q+qSzRMWHPktzpfvZ49KPJj2qn7Z9O5gOsE7QL1TAcUA9ell8Q+59OA02JduG+aCL1 B4HSiHUztgI1NTHtSOmGOkz2eNEgiw9vAX0NTrcVDLtsJV0288fNgez3M99neRXWU1jm 4AfA9ebIOXPgtRsqm6R0JeCVUOhYiw0uYsUcqpHMx2Z6J64LeBaOoCiwTWpuCRyQAO5B iV7qNG4hUiVCdG56ytGGDPbIGyhJiw/ZXTGU7i68W4cEutMEumOPpjCyWsNsBWmKaYUA vYMC38jus05AkAiMLQX8ne8OZg/CVt5TmZbOwm2EJDOGF5tPE16+W3qiZqvfHHkstALz XvmA== X-Forwarded-Encrypted: i=1; AHgh+RqboreJiUjm8KDmu97behvyIyWu9tcJowJoR4WGl1T4T9aMGsMY2QBBL0i1pgHusiJt+5sJABlBam9RP2k=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6aYSnIa0rboD2Fjn55oG+bmEbhY4idsoTlQmDaaOC3qJsADmL hcpaqwbRZwn4sazGRuEoGGhYLqi0E2/W3PXy8IEi15PXWoqyGoB4UGkY/cTIJsGoUSQ= X-Gm-Gg: AfdE7cmPORVfK296tbtAEHuSBIt10eAZSk4q7ocQmc9d6UsqBoR00Vycw7M9YldS4Jm aNA0J34Bbhs81F7aZnUIvZ4337psdWETocIEZ7V1dl8TQ9qcB9xAdWMWnO2OtuoLg2f4bZ+It4d 0yws+YVy1whklQA+QiOiywvLZZMTVXVLvd9obqjBGQYTJx6Ph5rWVt2LT9Ak7feOZA8d1pfl6h9 RQFVYYbU7qLzusASSSZTBAmCnZmKzCUplNENgXrQFmVXvuwqfjLKAK71B0p0F5kow2p57OKEycY NADKflgkHE3AZ4vHsLCve0zlg2epxBW83IE/qcD0iMrlS2UKzEqWJE1hqIDWivhqW9R4nrg6giJ WOI4HAOSs/twpd1BTuk9C9JPi1gLPSdEyhfMASPhQ0Iw0nb55iq7DTNB2Pqr35801nFyDfmIHU2 tTBXA9DNVxM5kIq2YKzWU3UzWqbhqN5+nrIB1s/RFKvjJ0/sSfIn/dV/YrGuadgA== X-Received: by 2002:a17:90b:5863:b0:36d:b424:4f17 with SMTP id 98e67ed59e1d1-38dc73bcfdfmr7418132a91.1.1783922957998; Sun, 12 Jul 2026 23:09:17 -0700 (PDT) Received: from FJ7FR2JRQ3.bytedance.net ([178.93.176.7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-38a5564f58csm6035548a91.8.2026.07.12.23.09.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 12 Jul 2026 23:09:17 -0700 (PDT) From: Zhanpeng Zhang To: Tomasz Jeznach , Joerg Roedel , Will Deacon Cc: Robin Murphy , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Guo Ren , David Laight , Vivian Wang , Zong Li , cuiyunhui@bytedance.com, yuanzhu@bytedance.com, iommu@lists.linux.dev, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Zhanpeng Zhang Subject: [PATCH v2] iommu/riscv: Use 32-bit MMIO accesses for 64-bit registers Date: Mon, 13 Jul 2026 14:09:02 +0800 Message-ID: <20260713060902.15161-1-zhangzhanpeng.jasper@bytedance.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260615064855.90316-1-zhangzhanpeng.jasper@bytedance.com> References: <20260615064855.90316-1-zhangzhanpeng.jasper@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The RISC-V IOMMU specification [1] permits 64-bit registers to be accessed using two 32-bit transactions, high half first, and leaves the single-copy atomicity of 8-byte IOMMU register accesses unspecified. Use the generic hi_lo_readq_relaxed() and hi_lo_writeq_relaxed() helpers for ordinary 64-bit IOMMU registers. Poll DDTP.BUSY in the low half before reading the full register from high to low. HPM counter reads require a rollover-aware sequence and remain outside these accessors. This follows the 32-bit access direction proposed by Guo Ren [2] and uses the generic non-atomic MMIO helpers suggested by David Laight. [1] https://docs.riscv.org/reference/iommu/ [2] https://lore.kernel.org/r/20250903144217.837448-1-guoren@kernel.org Suggested-by: Guo Ren Suggested-by: David Laight Signed-off-by: Zhanpeng Zhang --- Changes in v2: - Rework the patch based on Guo Ren's earlier proposal [1]. - Drop the build-time option and use 32-bit accesses unconditionally. - Drop the global lock and use the generic high-low MMIO helpers, as suggested by David Laight. - Poll DDTP.BUSY through its low half before reading the full register. Link to v1: [2] Specification discussion: [3] [1]: https://lore.kernel.org/r/20250903144217.837448-1-guoren@kernel.org [2]: https://lore.kernel.org/r/20260615064855.90316-1-zhangzhanpeng.jasper@= bytedance.com [3]: https://github.com/riscv-non-isa/riscv-iommu/issues/765 drivers/iommu/riscv/iommu.c | 18 +++++++++++------- drivers/iommu/riscv/iommu.h | 9 +++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index cec3ddd7ab1..14b572058ed 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -668,12 +668,16 @@ void riscv_iommu_disable(struct riscv_iommu_device *i= ommu) riscv_iommu_writel(iommu, RISCV_IOMMU_REG_PQCSR, 0); } =20 -#define riscv_iommu_read_ddtp(iommu) ({ \ - u64 ddtp; \ - riscv_iommu_readq_timeout((iommu), RISCV_IOMMU_REG_DDTP, ddtp, \ - !(ddtp & RISCV_IOMMU_DDTP_BUSY), 10, \ - RISCV_IOMMU_DDTP_TIMEOUT); \ - ddtp; }) +static u64 riscv_iommu_read_ddtp(struct riscv_iommu_device *iommu) +{ + u32 ddtp_lo; + + riscv_iommu_readl_timeout(iommu, RISCV_IOMMU_REG_DDTP, ddtp_lo, + !(ddtp_lo & RISCV_IOMMU_DDTP_BUSY), 10, + RISCV_IOMMU_DDTP_TIMEOUT); + + return riscv_iommu_readq(iommu, RISCV_IOMMU_REG_DDTP); +} =20 static int riscv_iommu_iodir_alloc(struct riscv_iommu_device *iommu) { @@ -1501,7 +1505,7 @@ static int riscv_iommu_init_check(struct riscv_iommu_= device *iommu) * regular boot flow and disable translation when we boot into a kexec * kernel and the previous kernel left them enabled. */ - ddtp =3D riscv_iommu_readq(iommu, RISCV_IOMMU_REG_DDTP); + ddtp =3D riscv_iommu_read_ddtp(iommu); if (ddtp & RISCV_IOMMU_DDTP_BUSY) return -EBUSY; =20 diff --git a/drivers/iommu/riscv/iommu.h b/drivers/iommu/riscv/iommu.h index 46df79dd549..1b03790fbe1 100644 --- a/drivers/iommu/riscv/iommu.h +++ b/drivers/iommu/riscv/iommu.h @@ -11,6 +11,7 @@ #ifndef _RISCV_IOMMU_H_ #define _RISCV_IOMMU_H_ =20 +#include #include #include #include @@ -70,17 +71,13 @@ void riscv_iommu_disable(struct riscv_iommu_device *iom= mu); readl_relaxed((iommu)->reg + (addr)) =20 #define riscv_iommu_readq(iommu, addr) \ - readq_relaxed((iommu)->reg + (addr)) + hi_lo_readq_relaxed((iommu)->reg + (addr)) =20 #define riscv_iommu_writel(iommu, addr, val) \ writel_relaxed((val), (iommu)->reg + (addr)) =20 #define riscv_iommu_writeq(iommu, addr, val) \ - writeq_relaxed((val), (iommu)->reg + (addr)) - -#define riscv_iommu_readq_timeout(iommu, addr, val, cond, delay_us, timeou= t_us) \ - readx_poll_timeout(readq_relaxed, (iommu)->reg + (addr), val, cond, \ - delay_us, timeout_us) + hi_lo_writeq_relaxed((val), (iommu)->reg + (addr)) =20 #define riscv_iommu_readl_timeout(iommu, addr, val, cond, delay_us, timeou= t_us) \ readx_poll_timeout(readl_relaxed, (iommu)->reg + (addr), val, cond, \ --=20 2.50.1 (Apple Git-155)