From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) (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 E84C83A1A21 for ; Fri, 1 May 2026 11:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777634426; cv=none; b=GSN2UKm/lZwOkJRxckkKTKW+B/CqqsYc6Uf8bQI85flTw3YVXWIVMs25i623T7uIimmbS5J2Cn3xYJs5t7uZoEEtBaxzRDzu+BsWg+2BdLi1lkn3ydrsj1cXFqSZmzh0wUSiU2mpCfuN+QqHJcmBda7VdRJM0hHcyf6AEhaHm/s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777634426; c=relaxed/simple; bh=ht3+eoYVAH5LRRYEvRvfYwLs0rEwiSQ92hy2jk6fn44=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FF/u7NgFqp7815Xh5Stc4Ekim20v9p30x+CCQqNr36cpYGjApHyXl+dB6I3JjvLxjn1CyWKvnh4cLCYZ4lpw5k/wzk8suNI8zrIWF4NJUJkGRnz9aHyULisXWbPhFRiCBcYEsayx6wH5bWFlA130jv73G7QocyNt6hGNWeesGys= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LJ8SxihL; arc=none smtp.client-ip=209.85.208.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LJ8SxihL" Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-6763c34cf60so2283234a12.0 for ; Fri, 01 May 2026 04:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777634422; x=1778239222; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DQ1p7g2Tvq/hKfuZPoTWdpsu5XMV18xbkA26lNofyBM=; b=LJ8SxihLXHJ8QzE4lxPI+zD6KcX4pykl92Hv8RiPpcYzfoz08kb1SiD6jSKM/XdgY+ 54/Q9pg4WNOY+7Tj+WSrLbPWEM+54O/VM5HRQvwM+iZWb7kzAvGtUL8N5Xs0kzpVI4F8 frTk9DZtZnGnc3zj4DRdMP1DSnHh3domUfjFOVmYYsWs9s1M5jU7E8DPnUfj4LFd0Ygl MIQgAGKKJrNrezvA44GzV8t/TK9h4lHEYISx5fGS8JQZxl5LmPRFXee+buVIs65c8A9k AQC4sQA6AigOsUb4QC9lStk9BpONIPZkO2zWqVE0VRr8bW48GrV0qYsIFTuv63lEhsLz yogA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777634422; x=1778239222; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DQ1p7g2Tvq/hKfuZPoTWdpsu5XMV18xbkA26lNofyBM=; b=AzNtoFmJsuw4Tg18LXD/Ej3uqa9tNBewgJVOSoE7CWSZ+86uvXzai1mFDSh4midK63 RyHZpYPhmNXJtKFFYvvw6nP6iEs+zj52kq0os/0ffJso8yL862UJ+GzgeV40XP0vxe5j J1wFCCFt0l60RbDR16EfrtiVdcgy9vHXuuqnpZTrknUXq89yx7ehafuyjm2tCcAx1auM 8ddzz+cSCcBEQCQvF2u5J6E427GzEEVwmLC5kfGPugXno0Hwk2I07vA4CwcT7gJzyIj2 t1U9buZSm4l+lmeyk6HmDcRLbYuHMACGvx69KvHhLvtl8D0Yocpwm6ej5kS/jHJ0/oQr lFQA== X-Forwarded-Encrypted: i=1; AFNElJ+h8DKIJFZ3hBZ4POalwgtS5IlsYRvAJ4uDWIIpJk0dcaBOj7NpIfOZkX7rymeqcyTPT5xq/05kQ/VFwrI=@vger.kernel.org X-Gm-Message-State: AOJu0Yzf5seVL9sOdvyfrqMlNET/QagI1eFpeTVkuhiXMBKWL+62TVSO 5XJykrMkCIEnk03IViQFzOgzC9qVYeOxxCDAvDR49Viohp/YraJ0wAgPsZe5l3eyQQDK3KkDDcq hve5MxHEOpdxcfg== X-Received: from edj4.prod.google.com ([2002:a05:6402:3244:b0:672:bfdb:8b72]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:d94f:0:b0:67b:acf5:f39e with SMTP id 4fb4d7f45d1cf-67bacf5f78amr800846a12.10.1777634422049; Fri, 01 May 2026 04:20:22 -0700 (PDT) Date: Fri, 1 May 2026 11:19:18 +0000 In-Reply-To: <20260501111928.259252-1-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260501111928.259252-1-smostafa@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260501111928.259252-17-smostafa@google.com> Subject: [PATCH v6 16/25] iommu/arm-smmu-v3-kvm: Add CMDQ functions From: Mostafa Saleh To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev Cc: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, joro@8bytes.org, jean-philippe@linaro.org, jgg@ziepe.ca, mark.rutland@arm.com, qperret@google.com, tabba@google.com, vdonnefort@google.com, sebastianene@google.com, keirf@google.com, Mostafa Saleh Content-Type: text/plain; charset="UTF-8" Add functions to access the command queue, there are 2 main usage: - Hypervisor's own commands, as TLB invalidation, would use functions as smmu_send_cmd(), which creates and sends a command. - Add host commands to the shadow command queue, after being filtered, these will be added with smmu_add_cmd_raw. Signed-off-by: Mostafa Saleh --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 14 ++- .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 107 ++++++++++++++++++ 2 files changed, 115 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index f904f4d19609..3fc499608d76 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -1156,19 +1156,21 @@ u32 smmu_idr5_to_oas(u32 reg); unsigned long smmu_idr5_to_pgsize(u32 reg); /* Queue functions shared between kernel and hyp. */ -static inline bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n) +static inline u32 queue_space(struct arm_smmu_ll_queue *q) { - u32 space, prod, cons; + u32 prod, cons; prod = Q_IDX(q, q->prod); cons = Q_IDX(q, q->cons); if (Q_WRP(q, q->prod) == Q_WRP(q, q->cons)) - space = (1 << q->max_n_shift) - (prod - cons); - else - space = cons - prod; + return (1 << q->max_n_shift) - (prod - cons); + return cons - prod; +} - return space >= n; +static inline bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n) +{ + return queue_space(q) >= n; } static inline bool queue_full(struct arm_smmu_ll_queue *q) diff --git a/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c index 3b77796dafc7..aac455599728 100644 --- a/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -22,6 +23,31 @@ struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; #define cmdq_size(cmdq) ((1 << ((cmdq)->llq.max_n_shift)) * CMDQ_ENT_DWORDS * 8) +/* + * Wait until @cond is true. + * Return 0 on success, or -ETIMEDOUT + */ +#define smmu_wait(use_wfe, _cond) \ +({ \ + int __ret = 0; \ + u64 delay = hyp_clock_ns() + ARM_SMMU_POLL_TIMEOUT_US * 1000; \ + \ + while (!(_cond)) { \ + if (use_wfe) { \ + wfe(); \ + if ((_cond)) \ + break; \ + } else { \ + cpu_relax(); \ + } \ + if (hyp_clock_ns() >= delay) { \ + __ret = -ETIMEDOUT; \ + break; \ + } \ + } \ + __ret; \ +}) + static bool is_cmdq_enabled(struct hyp_arm_smmu_v3_device *smmu) { return FIELD_GET(CR0_CMDQEN, smmu->cr0); @@ -74,6 +100,87 @@ static int smmu_unshare_pages(phys_addr_t addr, size_t size) return 0; } +__maybe_unused +static bool smmu_cmdq_has_space(struct arm_smmu_queue *cmdq, u32 n) +{ + struct arm_smmu_ll_queue *llq = &cmdq->llq; + + WRITE_ONCE(llq->cons, readl_relaxed(cmdq->cons_reg)); + return queue_has_space(llq, n); +} + +static bool smmu_cmdq_full(struct arm_smmu_queue *cmdq) +{ + struct arm_smmu_ll_queue *llq = &cmdq->llq; + + WRITE_ONCE(llq->cons, readl_relaxed(cmdq->cons_reg)); + return queue_full(llq); +} + +static bool smmu_cmdq_empty(struct arm_smmu_queue *cmdq) +{ + struct arm_smmu_ll_queue *llq = &cmdq->llq; + + WRITE_ONCE(llq->cons, readl_relaxed(cmdq->cons_reg)); + return queue_empty(llq); +} + +static void smmu_add_cmd_raw(struct hyp_arm_smmu_v3_device *smmu, + u64 *cmd) +{ + struct arm_smmu_queue *q = &smmu->cmdq; + struct arm_smmu_ll_queue *llq = &q->llq; + + queue_write(Q_ENT(q, llq->prod), cmd, CMDQ_ENT_DWORDS); + llq->prod = queue_inc_prod_n(llq, 1); +} + +static int smmu_add_cmd(struct hyp_arm_smmu_v3_device *smmu, + struct arm_smmu_cmdq_ent *ent) +{ + int ret; + u64 cmd[CMDQ_ENT_DWORDS]; + + ret = smmu_wait(false, !smmu_cmdq_full(&smmu->cmdq)); + if (ret) + return ret; + + ret = arm_smmu_cmdq_build_cmd(cmd, ent); + if (ret) + return ret; + + smmu_add_cmd_raw(smmu, cmd); + writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg); + return 0; +} + +static int smmu_sync_cmd(struct hyp_arm_smmu_v3_device *smmu) +{ + int ret; + struct arm_smmu_cmdq_ent cmd = { + .opcode = CMDQ_OP_CMD_SYNC, + }; + + ret = smmu_add_cmd(smmu, &cmd); + if (ret) + return ret; + + return smmu_wait(smmu->features & ARM_SMMU_FEAT_SEV, + smmu_cmdq_empty(&smmu->cmdq)); +} + +__maybe_unused +static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu, + struct arm_smmu_cmdq_ent *cmd) +{ + int ret = smmu_add_cmd(smmu, cmd); + + if (ret) + return ret; + + return smmu_sync_cmd(smmu); +} + /* Put the device in a state that can be probed by the host driver. */ static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) { -- 2.54.0.545.g6539524ca2-goog