From patchwork Mon Dec 2 14:06:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mostafa Saleh X-Patchwork-Id: 13890753 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 2017DD78321 for ; Mon, 2 Dec 2024 14:08:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=h1RF9JX+BAZDIHBPnUvFPOFh27GXZinqCyp9d856qhI=; b=vnuSNN/kYSQ1tnaNsYphrYoJGJ H+s0T351A0T/quNwfTL/N/YE7zdX+sK2WGpTiWq0Y8boaIFI7S3fy817XrExETFwRdyva8MVOqIYD x5If1c2C/RMdKndXnMu4CjvYaaxm5DReG3jP6Ypqafv+a9s0+eBI7LgW/ryiHJHF5etWtuCEktwtk Ir4KwI1uoSoQxrtGj+CkbUwLe9DFq+lNl/l189y91k7UaS8geLqMWRxJQ8WDofnIr5RPEOVK8leDN RLWtQTuaVKhtaO72ng1/H/QrxNg5TInHprf2nPCS3K80+0NaLaVO4SK1pIjLBZHHnRpCUsZwPLMS2 eDz8DYow==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tI766-00000006Mnc-1I19; Mon, 02 Dec 2024 14:08:22 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tI74F-00000006MSv-00hX for linux-arm-kernel@lists.infradead.org; Mon, 02 Dec 2024 14:06:28 +0000 Received: by mail-wm1-x349.google.com with SMTP id 5b1f17b1804b1-4349df2d87dso40882745e9.2 for ; Mon, 02 Dec 2024 06:06:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733148385; x=1733753185; darn=lists.infradead.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=h1RF9JX+BAZDIHBPnUvFPOFh27GXZinqCyp9d856qhI=; b=oP5rWWxAULFfL3w9XatEpm3o+bLVfB45nh+WbBQNrpSDXQjPfSfadr4U5bzBetf/KK LOker4bkQJZOGdkIswG8n9gnwfuCNwCmSiRBbwIrWf54QxQyF/z1mUs7iJ9O+D36zPxA ozfNcZHD8bKvdFdY6zLOcSJT0Yg4PARBQmWvHQUfLJg5p7/CL9feFoQMM8B2kz+kUwXQ 2jvNuPlWWhpvK3YmKoOSYn1ej2uzbv4M900L+0pqhqnD8lAXIm+Lb6x+uVBMC7cX0exU XHEakcBW3xP6PqM4sfBN4GV827puVvUKo+36oalWAqNOJI/rWUboAXMBe4Dhhym266VD emrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733148385; x=1733753185; 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=h1RF9JX+BAZDIHBPnUvFPOFh27GXZinqCyp9d856qhI=; b=GI3cLjFFHeNL+81TFFIuL2qU1haLQxV4VprAz7tCKI/E/w9VGLf2+jI5/HJqhmhWC5 JCvx1N5NQOHUpxf/NtDiyj6LK9ScfFh3CNG7ABqzDgnfMFUnD8xLwL2K17JWv44A4pOp xRd0t8VAxZQXxvhSkhK1ojkevYQrbbH55JjBgytufKKfUvRXc0SBNn7KuljpEdD3Hn8d rQX/oU5OIB86MTzfntKTdBpnHI7FVmbvFsO/dsFDjxHXvlW0QhQZLVFgTDm86xWVBZ9V glgVtFvMx7XK1t/jUUfX1u3liXrsz9rb2QKG95Z9OrOgvLpc67g8k98SirRtezBS8jba yKKA== X-Forwarded-Encrypted: i=1; AJvYcCWyTl11l/gf5sDblhpAtDJAOX0EveMfDvebOGcB6ea/tzMs64jYoXtgTx2dcCFPFsk6muoUwNttq5jCgBHwm7qR@lists.infradead.org X-Gm-Message-State: AOJu0YwcIhEBvVvQ/s07EAosLkGwyOP3OqERqOl6g1ItDKvs5G7qeeG8 3465hTECyqQzkU5XjBb/tWeqRdYFiQA4Uh/bLa7QGvPa3vaNYvJoG7NgzWsPh74CNNAQvmUYuyW H9ibBY0B7Bg== X-Google-Smtp-Source: AGHT+IGbcuRYpTh4YD2s/LlLvQ+RLHNmyee/bZzsqF0w9Suqv7VzmqnzD+QY/tWrJ1jck1m9Bz5q92qEuZjfxg== X-Received: from wman26.prod.google.com ([2002:a05:600c:6c5a:b0:434:9fab:eb5]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5493:b0:432:d735:cc71 with SMTP id 5b1f17b1804b1-434a9de8e1emr227230595e9.25.1733148385023; Mon, 02 Dec 2024 06:06:25 -0800 (PST) Date: Mon, 2 Dec 2024 14:06:03 +0000 In-Reply-To: <20241202140604.422235-1-smostafa@google.com> Mime-Version: 1.0 References: <20241202140604.422235-1-smostafa@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241202140604.422235-2-smostafa@google.com> Subject: [PATCH v2 1/2] iommu/io-pgtable-arm: Fix stage-2 concatenation with 16K From: Mostafa Saleh To: linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: will@kernel.org, robin.murphy@arm.com, joro@8bytes.org, Mostafa Saleh X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241202_060627_037458_0FE03687 X-CRM114-Status: GOOD ( 18.40 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org At the moment, io-pgtable-arm uses concatenation only if it is possible at level 0, which misses a case where concatenation is mandatory at level 1 according to R_SRKBC in Arm spec DDI0487 K.a. Also, that means concatenation can be used when not mandated, contradicting the comment on the code. However, these cases can only happen if the SMMUv3 driver is changed to use ias != oas for stage-2. This patch re-writes the code to use concatenation only if mandatory, fixing the missing case for level-1 and granule 16K with PA = 40 bits. Signed-off-by: Mostafa Saleh --- drivers/iommu/io-pgtable-arm.c | 45 +++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 6b9bb58a414f..0055876b3527 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -223,6 +223,33 @@ static inline int arm_lpae_max_entries(int i, struct arm_lpae_io_pgtable *data) return ptes_per_table - (i & (ptes_per_table - 1)); } +/* + * Check if concatenated PGDs are mandatory according to Arm DDI0487 (K.a) + * 1) R_DXBSH: For 16KB, and 48-bit input size, use level 1 instead of 0. + * 2) R_SRKBC: After de-ciphering the table for PA size and valid initial lookup + * a) 40 bits PA size with 4K: use level 1 instead of level 0 (2 tables for ias = oas) + * b) 40 bits PA size with 16K: use level 2 instead of level 1 (16 tables for ias = oas) + * c) 42 bits PA size with 4K: use level 1 instead of level 0 (8 tables for ias = oas) + * d) 48 bits PA size with 16K: use level 1 instead of level 0 (2 tables for ias = oas) + */ +static inline bool arm_lpae_concat_mandatory(struct arm_lpae_io_pgtable *data) +{ + unsigned int ias = data->iop.cfg.ias; + unsigned int oas = data->iop.cfg.oas; + + /* Covers 1 and 2.d */ + if ((ARM_LPAE_GRANULE(data) == SZ_16K) && (data->start_level == 0)) + return (oas == 48) || (ias = 48); + + /* Covers 2.a and 2.c */ + if ((ARM_LPAE_GRANULE(data) == SZ_4K) && (data->start_level == 0)) + return (oas == 40) || (oas == 42); + + /* Case 2.b */ + return (ARM_LPAE_GRANULE(data) == SZ_16K) && + (data->start_level == 1) && (oas == 40); +} + static bool selftest_running = false; static dma_addr_t __arm_lpae_dma_addr(void *pages) @@ -1006,18 +1033,12 @@ arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) if (!data) return NULL; - /* - * Concatenate PGDs at level 1 if possible in order to reduce - * the depth of the stage-2 walk. - */ - if (data->start_level == 0) { - unsigned long pgd_pages; - - pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); - if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { - data->pgd_bits += data->bits_per_level; - data->start_level++; - } + if (arm_lpae_concat_mandatory(data)) { + if (WARN_ON((ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte)) > + ARM_LPAE_S2_MAX_CONCAT_PAGES)) + return NULL; + data->pgd_bits += data->bits_per_level; + data->start_level++; } /* VTCR */ From patchwork Mon Dec 2 14:06:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mostafa Saleh X-Patchwork-Id: 13890755 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 8349ED78327 for ; Mon, 2 Dec 2024 14:09:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=jKxSUej1+l/0VRK8dY/i8qKE5HDYtkSoqmdKS10eGJE=; b=nbwmGSaCnAN+iH++pnAgqHs4FG wjA3fjl3wnhGhqLi4SaqhTaSFaJ+IyJARzfyvmldp7yiXiOxLivUdk4mn6Wt9qInJDOlmtuUW/f5s M6jlyEYthfmIT4CPazqQFluUnCM7DmUx++tldCIHZmWe0fFvCi6hSY4f1gMstZJHA97b/cxDxrAMl avm1V4pLgMyeOiVjbgUk8/qCE4vGowz9ma/+RZzXksIWJm3/qeX3963mKD8j4LqZcNIvS1g+CPIod iPKOUPc7WEF6502WHrxatge3iPT7TSznX5eLyejvu8J0FdTmtoyOVWaTWEKHscITQ90d7mYwgftDy 3DzyI81A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tI773-00000006Myv-3q4R; Mon, 02 Dec 2024 14:09:21 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tI74H-00000006MU9-24ZJ for linux-arm-kernel@lists.infradead.org; Mon, 02 Dec 2024 14:06:30 +0000 Received: by mail-wm1-x349.google.com with SMTP id 5b1f17b1804b1-434a104896cso35115125e9.1 for ; Mon, 02 Dec 2024 06:06:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733148388; x=1733753188; darn=lists.infradead.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=jKxSUej1+l/0VRK8dY/i8qKE5HDYtkSoqmdKS10eGJE=; b=Q8W0KhkEtbUpzSYShNDl8v/iFf51bvpCtl9CPQIv6KRW0o2NBZUadaBucV58BzzHig XBJSjKG992+0a1Y0P2TR/lDWF3n5ynvZQmTVbXZqYMLesEJ+7kpUaM2pBpvH+nkR1KtN SWTnWTaUfA6CRtW1L+mnKzwoNaXtqf42+WEr/Spew22T82tUnIrDY0alD/4iLnTuryT2 manRiZ1dbljWk1bft37BusJW2OblI6sJDEu0MQfURkGEjz++fBYJDK2tG2eIA+AwU+Zo zHDqflWNE7WygZOCMHmYRI8NFM0yzXlgRtixzZ4ErSfn3H7I1/3LYx0ID8D0KU0r4R4j 6luQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733148388; x=1733753188; 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=jKxSUej1+l/0VRK8dY/i8qKE5HDYtkSoqmdKS10eGJE=; b=H+dtBztZGEAt+ETiiLFOT2WgbCB340niq1rjhkTwu/Ipv8yOvjBTTXtHgW8+KUMFll AT+CnWgF+bBhdBsxQ2lPBY9zyZ01wIl4ERw8IdygOUkHEzB05u0GmpP+xLO7E0/tr8sb 0gkpAWtXkeTyyn0wiGKczlMlAGUzNS0e96EDcaArcVdYqzf6G3avJzccQyZes9pz+42q dHSZjqIxSqk7l4GPZ9HjU2nE2jZZDCw5Ang/k+gRByPsR2gUeLz6E5cSl9MPKQ8FdA62 Xf6Ofkf46f9ozR1c2aKI69nat/lVUhbS7l+TWft0v3AdSYT4Lq+JFE1MKXveoeMTLyG0 xk8A== X-Forwarded-Encrypted: i=1; AJvYcCWe4lMg14L0D+6zRQalgi295Lx5BqHtILSMi/4VokfYd9+vg0T0E1Qxq7FhcGcWt8mow3mBfIrIr5d5UJDTMFMy@lists.infradead.org X-Gm-Message-State: AOJu0YyWamU3StAFBWGluPhKjd753MrzdphbavFs9gUumHgzgclUQq9D 5aYJxU1ZrBtH/6yV44pnIgdapAlrcclrapST1DpNmBxWnWd3NApBYMTbxbXv15E6svEI3AVsdz4 7Dj/IzN3XlA== X-Google-Smtp-Source: AGHT+IGb9GFF+XeRLxQAr2q/gzezFVIRuu1iuJqUMG9tGhoRGftP6XM2BbzyeGE4do1mNb0+j1Ui/wgaLGW65g== X-Received: from wmqg12.prod.google.com ([2002:a05:600c:4ecc:b0:434:a172:6127]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f56:b0:434:a90b:94fe with SMTP id 5b1f17b1804b1-434a9dc1401mr242319915e9.10.1733148388003; Mon, 02 Dec 2024 06:06:28 -0800 (PST) Date: Mon, 2 Dec 2024 14:06:04 +0000 In-Reply-To: <20241202140604.422235-1-smostafa@google.com> Mime-Version: 1.0 References: <20241202140604.422235-1-smostafa@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241202140604.422235-3-smostafa@google.com> Subject: [PATCH v2 2/2] iommu/io-pgtable-arm: Add coverage for different OAS in selftest From: Mostafa Saleh To: linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: will@kernel.org, robin.murphy@arm.com, joro@8bytes.org, Mostafa Saleh X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241202_060629_537300_2A398368 X-CRM114-Status: GOOD ( 17.30 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Run selftests with different OAS values intead of hardcoding it to 48 bits. We always keep OAS >= IAS to make the config valid for stage-2. This can be further improved, if we split IAS/OAS configuration for stage-1 and stage-2 (to use input sizes compatible with VA_BITS as SMMUv3 does, or IAS > OAS which is valid for stage-1). However, that adds more complexity, and the current change improves coverage and makes it possible to test all concatenation cases. Signed-off-by: Mostafa Saleh --- drivers/iommu/io-pgtable-arm.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 0055876b3527..62015fdd6c6a 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -1385,15 +1385,14 @@ static int __init arm_lpae_do_selftests(void) SZ_64K | SZ_512M, }; - static const unsigned int ias[] __initconst = { + static const unsigned int address_size[] __initconst = { 32, 36, 40, 42, 44, 48, }; - int i, j, pass = 0, fail = 0; + int i, j, k, pass = 0, fail = 0; struct device dev; struct io_pgtable_cfg cfg = { .tlb = &dummy_tlb_ops, - .oas = 48, .coherent_walk = true, .iommu_dev = &dev, }; @@ -1402,15 +1401,19 @@ static int __init arm_lpae_do_selftests(void) set_dev_node(&dev, NUMA_NO_NODE); for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { - for (j = 0; j < ARRAY_SIZE(ias); ++j) { - cfg.pgsize_bitmap = pgsize[i]; - cfg.ias = ias[j]; - pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", - pgsize[i], ias[j]); - if (arm_lpae_run_tests(&cfg)) - fail++; - else - pass++; + for (j = 0; j < ARRAY_SIZE(address_size); ++j) { + /* Don't use ias > oas as it is not valid for stage-2. */ + for (k = 0; k <= j; ++k) { + cfg.pgsize_bitmap = pgsize[i]; + cfg.ias = address_size[k]; + cfg.oas = address_size[j]; + pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u OAS %u\n", + pgsize[i], cfg.ias, cfg.oas); + if (arm_lpae_run_tests(&cfg)) + fail++; + else + pass++; + } } }