From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f49.google.com (mail-dl1-f49.google.com [74.125.82.49]) (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 DE8E13C5DBA for ; Tue, 23 Jun 2026 08:19:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782202748; cv=none; b=m+6J0ybZsrQ4m+vdOAQ8rXbk8zZxQ3Bfxic8jjZcSE5PxX3J+BsVu2AZFlXadyWLPcrqdHRk5GCgYN/MHnsqzeelXRitJjfJW3uwz3F3JL14PRxALBJC8jBdgMshbWECtw3a8HO/FfpqEOK1Ke04ospzVdMVCjVR/qHorcDHGm4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782202748; c=relaxed/simple; bh=weI36dBrZRSvtSFbESYh7XC1OFFFZe4YOoHRUWMU6DM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=dQhGqKXTMoR1F/xTgAXfSK1O+cUDZjE1MsKk5Y+RZt3immdN+rgB/zMdZAT+ChFcJ1cfx+opXi04uyZEA6h8HxLCldCDDMj6naD0tO+3ckLhKN1ZJ+G+beekqaxWtqrK1Vu0Zbr3Ib4DUFd5+Wbv58WNbjMGCmnMoukQNEXl9YI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=NioiHLnz; arc=none smtp.client-ip=74.125.82.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="NioiHLnz" Received: by mail-dl1-f49.google.com with SMTP id a92af1059eb24-139b914bab6so2344208c88.1 for ; Tue, 23 Jun 2026 01:19:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1782202746; x=1782807546; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=xTbtFL6V7xdQq2v5wFOMr8PK+R/BJm2uQjBRjFSF9J8=; b=NioiHLnzIuEk9yb8xrGmLgYodGXiWbTi1uOTCiYL9oFqMVTvE//Z4ic/SZAhmppU2e 9g7EJE+X0o5SlXEo9SqGwgjK1VUDom30C5sFwHSt5jXfZmMu9DhdZhN8cK8JhXGQKCa5 0X6LBnRS3DzbX+MWJWeLbrsbRVQzZ5fqP2nuhz3hovW8WWoPuh1pBk24VYL/xYpLNPHp g0fxXJLQB2/rKXaDSVFTeP7ckG7RUBe4xAQOHWXM7d/7u5TVcJ0FdoHPi2zABm2Ikp2S c5np5+skmYhcinX9vZ2Tf5g27jMPiuDOf81fVtuxBwKT63vtloEbw4yuvv2b4TfbF5D8 /gqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782202746; x=1782807546; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=xTbtFL6V7xdQq2v5wFOMr8PK+R/BJm2uQjBRjFSF9J8=; b=BCDJ+7fa8wpvLVXgNFmDh3tZBLGTrw+E3xfzfsblvm6Pl5otCRZxNtNYHbyS/Y8HGp nfFDhyfXusnVQGFbeC4plfpmZKLldAp8r6Z5Dre/jmG3HcIytJ2b8wCRHjAj8YpIAJr/ AhzE10avO+66Xc3I11nClTCc1JoJffaRheeCv7+/mu2fNR/JLAMSJOtLmOQw4MXycvSi mpBYLz7AxEVFchVTq+EiR6IIpdsMl2YvZkqacsmSBs6enfCGxyURkNs6dovtr9Fifv66 FWj+PLxH/oYRb6EkruvlDN90aZZPEK1CY5wq4qOYEsCZ4eZM9PQXTRYSz9akQcd741GB u7aA== X-Forwarded-Encrypted: i=1; AFNElJ8u8AcNDuX4O71vz3V0Ri2LEnf+3HUJBP8AP8gkUF+pknw7tukqnL2fzJONIZTM1Im54FxgGHfUEzQOl4w=@vger.kernel.org X-Gm-Message-State: AOJu0YyChdslwzkx3M6vbrS2G5IunfNjlaY92sy7RA/1AZVpZXS0XS+6 +r+BhoXt6Tf2rz0k3PZfgHcItF7z745EjOSrJ9/fTSZhXCzJQwYbvYnYeh4wvvavE6I= X-Gm-Gg: AfdE7ckdR77SUSN9nhhfR7ubLdiapXpuOOe1M1EKaCXTj5vhIatjZeRjRNV8QfCN93K mOdo9ZCIbRNbpqjO+aeGf4hXGZ95kKc5tSyiTv4/ZkkENHHUNCrQbmkt/E0k+8BqgmXHAvAozg5 ZyiKkLOXGPGQpqjvQi4dPckV5wMCf7MtfrIJm2SkqMNjzAMci9wi2ezMii8K4O7YRRne9fSFP0J rlqeuLq8W2ybrLweglY8oOkXfvrvg8SHX0nWsIIpYlCH8/MKUCIhm+9LKpXyKzYvZ2dimKh6ARK gPKixBp1GFP8LKIgJLJkPtOWIQo4k5a7lR8MeT6oy6oMHi7Gu5poUlZ+B5kYz2pf9OSBL9/xTPd gzXKtfTmvBE0fl0MQYvAwZmwgte47/1EU6ihwzurrO50G3J/KeRi4u2e9RC7O4BQBgAFUpebFcB zV+lthdWQafJ6d016MGTpahXdryViIWcvkSU4= X-Received: by 2002:a05:7022:45a6:b0:138:3613:db8 with SMTP id a92af1059eb24-139c6ebfc80mr1045661c88.3.1782202745949; Tue, 23 Jun 2026 01:19:05 -0700 (PDT) Received: from sw04.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-139adca1b00sm11246236c88.2.2026.06.23.01.19.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jun 2026 01:19:05 -0700 (PDT) From: Zong Li To: tomasz.jeznach@linux.dev, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, iommu@lists.linux.dev, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH] iommu/riscv: Replace illegal command with dummy IOFENCE to prevent hardware lockup Date: Tue, 23 Jun 2026 01:19:02 -0700 Message-ID: <20260623081902.589810-1-zong.li@sifive.com> X-Mailer: git-send-email @GIT_VERSION@ Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When the RISC-V IOMMU encounters an illegal command, the hardware stops processing and the HEAD register remains pointing at the illegal command. If software does not handle this properly, the hardware will be stuck at this index indefinitely, preventing any further command queue operations. This patch implements a recovery mechanism by replacing the illegal command with a dummy IOFENCE instruction (all operands are zero): 1. Prevents hardware lockup: By overwriting the illegal command with a valid instruction, the hardware can continue processing from the current position instead of being stuck. 2. Enables user recovery: After replacing the illegal command, the user/driver has an opportunity to retry the original failed operation rather than losing all queued work. 3. Minimal hardware impact: A dummy IOFENCE behaves as a NOP, it it performs no cache invalidation operations and has no side effects on the system state. This is the safest replacement instruction. Signed-off-by: Zong Li --- drivers/iommu/riscv/iommu.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index cec3ddd7ab10..6305ec5f467b 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -464,13 +464,35 @@ static unsigned int riscv_iommu_queue_send(struct riscv_iommu_queue *queue, static irqreturn_t riscv_iommu_cmdq_process(int irq, void *data) { const struct riscv_iommu_queue *queue = (struct riscv_iommu_queue *)data; - unsigned int ctrl; + struct riscv_iommu_command cmd; + unsigned int ctrl, head; /* Clear MF/CQ errors, complete error recovery to be implemented. */ ctrl = riscv_iommu_readl(queue->iommu, queue->qcr); if (ctrl & (RISCV_IOMMU_CQCSR_CQMF | RISCV_IOMMU_CQCSR_CMD_TO | RISCV_IOMMU_CQCSR_CMD_ILL | RISCV_IOMMU_CQCSR_FENCE_W_IP)) { + /* + * The head pointer is not updated by the hardware, it + * still points to the index of illegal command + */ + riscv_iommu_readl_timeout(queue->iommu, Q_HEAD(queue), head, + !(head & ~queue->mask), 0, + RISCV_IOMMU_QUEUE_TIMEOUT); + + if (ctrl & RISCV_IOMMU_CQCSR_CMD_ILL) { + /* + * Use a dummy IOFENCE instead of the illegal command + * to prevent hardware lockup + */ + memset(&cmd, 0, sizeof(cmd)); + cmd.dword0 = FIELD_PREP(RISCV_IOMMU_CMD0_OPCODE, + RISCV_IOMMU_CMD_IOFENCE_OPCODE); + memcpy(queue->base + head * sizeof(cmd), &cmd, sizeof(cmd)); + dma_wmb(); + } + riscv_iommu_writel(queue->iommu, queue->qcr, ctrl); + dev_warn(queue->iommu->dev, "Queue #%u error; fault:%d timeout:%d illegal:%d fence_w_ip:%d\n", queue->qid, -- 2.43.7