From patchwork Thu Jul 18 13:20:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 813176 Delivered-To: patch@linaro.org Received: by 2002:adf:f288:0:b0:367:895a:4699 with SMTP id k8csp290468wro; Thu, 18 Jul 2024 06:22:34 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU2AEDaq5s6+rqiXZu6eOqjqEdN/7aom2Unr75ClPO8QlsT/gppPZpMkTYHbdqrB7xqBPxwcZ/Hw5rh6OKoI/yH X-Google-Smtp-Source: AGHT+IEDJ4C78o0YsI76s6wGM8ogIT6BvUZ0HjOtfC8tPhfuMLIGB2L7uuSgvrUjyf6G8bkpKC07 X-Received: by 2002:a05:6830:621c:b0:707:a4f3:e134 with SMTP id 46e09a7af769-708ecde2bedmr3054430a34.19.1721308954164; Thu, 18 Jul 2024 06:22:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721308954; cv=none; d=google.com; s=arc-20160816; b=ZynJnSYSpEbCsXijS7bg88WpUG0fWRxMuyfNy8Kt6lw9O+T5/z4/+wUSlrEOZB6Yw5 PwEKIpD7msVTvqx8FmRVoc00nc0nHoLJ/R5pNuqwLv4G4Ydn9+s1Fik+ZGDsood75cQV T/qDIRb8b4xeP3/ZQL4FkUWRyRXJ/7dve2Rft9y3JxC5ZjM+sEUyQE/Ppqqgaf+HukEU tsfBXK6wy0f3tQjy1NnE8SkaweAJOy0man1/BClkrd4YrOnX7fB3uk4CfhI3/nciobFl MFOrBcu1kQ/FjrGuR3JC4SWtF9f85PjG6TAgfRSBlqUUPEPVl8YsF6H4oZ3r4OF0afyx NSRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=X2AcFwMwmG8CuilTxkX1QR6WPC1ZxLwj44sonDWX5Es=; fh=PnYt+qEB9tAfMKoqBm2xjKOFpYyFFGPudh5cVIoieJM=; b=E4ZK7HDgjVlO0YKUST3zYil77lt9xF6Erk0HufywcuqsgXSewZfmdRRGT6qRXFuqvW Xh0ENRZFyf0gnti9skGFHtlJqYIJTzJNUWGv96pZDH3YxjrsDwMsCXhyf42JLSfIwz/p h2tiCvpRhEtsrefDk0Ji3yBoTykM/CDFCoYoHKsf0v5yb0xsuAdxSet001gd9yMNRIjH kIn8YM4CWi7Rg6sIdTxxsKwc+9dSY9QBuXW0btRTpkqD1AbUHkjGf6FFqNsY/XWQxI1V 2/uhbowZ01xveanGfbzl8Jc2TCLimnDbRdVm3ypTisvv8Cor9we1F4aUutvOY52eHJZe AawA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BNNkDjgF; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a1e0cc1a2514c-822c9c39fd1si157354241.171.2024.07.18.06.22.33 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 18 Jul 2024 06:22:34 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BNNkDjgF; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sUR47-0001bt-Fu; Thu, 18 Jul 2024 09:20:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sUR3o-0008EW-TD for qemu-devel@nongnu.org; Thu, 18 Jul 2024 09:20:42 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sUR3m-0004US-NP for qemu-devel@nongnu.org; Thu, 18 Jul 2024 09:20:40 -0400 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-427b4c621b9so2497655e9.1 for ; Thu, 18 Jul 2024 06:20:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1721308836; x=1721913636; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=X2AcFwMwmG8CuilTxkX1QR6WPC1ZxLwj44sonDWX5Es=; b=BNNkDjgFl80e1fh5MKk7eUlFv69FlCM+1ExZTTPvgtHx06Sc/I4ErYvtHvghLgfIeI IQT7o3QgSKhys6BYswG1gEMj+eavmrI6wmnaOOG2Zh+m+2FUm/6amPgUeV12Jnu+uLWU YMtP+uaNXhRPq2u3d9EbYxMSzHQrRYYVKiHz/G0NJyHPV1xMYMvnAvlDW9/kCRRqJa5L oNDXsJHmkn4cyAz7OTXWLWgjpd41sAGJe0/kPySzUKULyyp1H8fQJer4tTF6oOOM8/TF zObp9SC3K7wPJ67LnHUJShxQOIsxuDZ/8xyLO9N07LJI/W3bVByiZzOzgT6gmDfc2V1y 1OLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721308836; x=1721913636; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X2AcFwMwmG8CuilTxkX1QR6WPC1ZxLwj44sonDWX5Es=; b=Tkcz2NeC3QVt4cWDVYxZ0stkoioLWu3/XeGF1mVyH21Ah9QyGzO9FDaYT1DpzkQDmq 2BR/1mTyq88MKUU968I93hBkXMgoyxzA9d1E4/hWSPwVwqHqHjwN+HMIzUjNHpl1VEc6 gP1iHMS6xk0GIovQiQ+u1H42b4DGDwpjxtH/NIONZpI7894L3CsNnuWJhhdE9q4OJ4Bk +tOiDBdYWmUesZeuXfR+BqlK/mrZz1Dq7RKDnl9qs7MSJn0vUQ4BF2yXpqCgsht9NuyU 7iSU1haYkURlS9DCeR5AyKLCDkmzvR6/cmlClptnXVc5RQx0vRz1ZZ7OT+cVvId1alTc odNA== X-Gm-Message-State: AOJu0YzFbDLtMo7o8xNhkSDHXsKtvehDNrP2RFmIUSiHd0hgHRTFTXEh PXemsunbN81k3dqvC/m9D5EmN+5bvryJM2ZFRrHbblolMSJTDa0UKJVu5vcvjyhoo/JqC/ogFq2 r X-Received: by 2002:a05:600c:4fd5:b0:426:5f8f:51a4 with SMTP id 5b1f17b1804b1-427c2cb08a0mr34368815e9.12.1721308836428; Thu, 18 Jul 2024 06:20:36 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427d2a8e420sm14059315e9.35.2024.07.18.06.20.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 06:20:36 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 12/26] hw/arm/smmu-common: Rework TLB lookup for nesting Date: Thu, 18 Jul 2024 14:20:14 +0100 Message-Id: <20240718132028.697927-13-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240718132028.697927-1-peter.maydell@linaro.org> References: <20240718132028.697927-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Mostafa Saleh In the next patch, combine_tlb() will be added which combines 2 TLB entries into one for nested translations, which chooses the granule and level from the smallest entry. This means that with nested translation, an entry can be cached with the granule of stage-2 and not stage-1. However, currently, the lookup for an IOVA is done with input stage granule, which is stage-1 for nested configuration, which will not work with the above logic. This patch reworks lookup in that case, so it falls back to stage-2 granule if no entry is found using stage-1 granule. Also, drop aligning the iova to avoid over-aligning in case the iova is cached with a smaller granule, the TLB lookup will align the iova anyway for each granule and level, and the page table walker doesn't consider the page offset bits. Signed-off-by: Mostafa Saleh Reviewed-by: Jean-Philippe Brucker Reviewed-by: Eric Auger Reviewed-by: Alex Bennée Message-id: 20240715084519.1189624-10-smostafa@google.com Signed-off-by: Peter Maydell --- hw/arm/smmu-common.c | 64 +++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c index 7fbf8e22fe0..e374cae0db6 100644 --- a/hw/arm/smmu-common.c +++ b/hw/arm/smmu-common.c @@ -66,8 +66,10 @@ SMMUIOTLBKey smmu_get_iotlb_key(int asid, int vmid, uint64_t iova, return key; } -SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, - SMMUTransTableInfo *tt, hwaddr iova) +static SMMUTLBEntry *smmu_iotlb_lookup_all_levels(SMMUState *bs, + SMMUTransCfg *cfg, + SMMUTransTableInfo *tt, + hwaddr iova) { uint8_t tg = (tt->granule_sz - 10) / 2; uint8_t inputsize = 64 - tt->tsz; @@ -88,6 +90,36 @@ SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, } level++; } + return entry; +} + +/** + * smmu_iotlb_lookup - Look up for a TLB entry. + * @bs: SMMU state which includes the TLB instance + * @cfg: Configuration of the translation + * @tt: Translation table info (granule and tsz) + * @iova: IOVA address to lookup + * + * returns a valid entry on success, otherwise NULL. + * In case of nested translation, tt can be updated to include + * the granule of the found entry as it might different from + * the IOVA granule. + */ +SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, + SMMUTransTableInfo *tt, hwaddr iova) +{ + SMMUTLBEntry *entry = NULL; + + entry = smmu_iotlb_lookup_all_levels(bs, cfg, tt, iova); + /* + * For nested translation also try the s2 granule, as the TLB will insert + * it if the size of s2 tlb entry was smaller. + */ + if (!entry && (cfg->stage == SMMU_NESTED) && + (cfg->s2cfg.granule_sz != tt->granule_sz)) { + tt->granule_sz = cfg->s2cfg.granule_sz; + entry = smmu_iotlb_lookup_all_levels(bs, cfg, tt, iova); + } if (entry) { cfg->iotlb_hits++; @@ -569,18 +601,21 @@ int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm, SMMUTLBEntry *smmu_translate(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t addr, IOMMUAccessFlags flag, SMMUPTWEventInfo *info) { - uint64_t page_mask, aligned_addr; SMMUTLBEntry *cached_entry = NULL; SMMUTransTableInfo *tt; int status; /* - * Combined attributes used for TLB lookup, as only one stage is supported, - * it will hold attributes based on the enabled stage. + * Combined attributes used for TLB lookup, holds the attributes for + * the input stage. */ SMMUTransTableInfo tt_combined; - if (cfg->stage == SMMU_STAGE_1) { + if (cfg->stage == SMMU_STAGE_2) { + /* Stage2. */ + tt_combined.granule_sz = cfg->s2cfg.granule_sz; + tt_combined.tsz = cfg->s2cfg.tsz; + } else { /* Select stage1 translation table. */ tt = select_tt(cfg, addr); if (!tt) { @@ -590,22 +625,9 @@ SMMUTLBEntry *smmu_translate(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t addr, } tt_combined.granule_sz = tt->granule_sz; tt_combined.tsz = tt->tsz; - - } else { - /* Stage2. */ - tt_combined.granule_sz = cfg->s2cfg.granule_sz; - tt_combined.tsz = cfg->s2cfg.tsz; } - /* - * TLB lookup looks for granule and input size for a translation stage, - * as only one stage is supported right now, choose the right values - * from the configuration. - */ - page_mask = (1ULL << tt_combined.granule_sz) - 1; - aligned_addr = addr & ~page_mask; - - cached_entry = smmu_iotlb_lookup(bs, cfg, &tt_combined, aligned_addr); + cached_entry = smmu_iotlb_lookup(bs, cfg, &tt_combined, addr); if (cached_entry) { if ((flag & IOMMU_WO) && !(cached_entry->entry.perm & IOMMU_WO)) { info->type = SMMU_PTW_ERR_PERMISSION; @@ -616,7 +638,7 @@ SMMUTLBEntry *smmu_translate(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t addr, } cached_entry = g_new0(SMMUTLBEntry, 1); - status = smmu_ptw(cfg, aligned_addr, flag, cached_entry, info); + status = smmu_ptw(cfg, addr, flag, cached_entry, info); if (status) { g_free(cached_entry); return NULL;