Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1858666rwb; Mon, 7 Nov 2022 06:37:53 -0800 (PST) X-Google-Smtp-Source: AMsMyM4V2vXIIXN4idpNrJ5iI8+RN7m2CFX/VKjh4zKVgUAyfKbZf3E++kO/qJxHEHRbX0O1blvt X-Received: by 2002:a50:fb15:0:b0:464:4947:133 with SMTP id d21-20020a50fb15000000b0046449470133mr22036004edq.231.1667831873017; Mon, 07 Nov 2022 06:37:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667831873; cv=none; d=google.com; s=arc-20160816; b=o02vODr93MyRp59bOzFHOKIs4cG3UUwKCok7EwqUl8Syty1Bbedu8e1BDug+EkN8mx OTSdzyXZuRAyhmEdtGKN6XfFCkpu8R2bpi9YRyHuWRFvscWIqcEJqO1jR3469DfBXqz1 ngFCmgUFGqOIy7ek6p/HCRfrvhwkIFNro8z2lnuynis7/tyaQnqIBwgLD6ZEVlmkc463 saQ2l542UmSgEBqGkjaL54hYSNEJV7TzpKUQx8APSDxU4wtk+xKs6wPlYCFikVElNVwJ Ba105SLOd1GB+OGOHL2sndwqpyBODPuTkoLN9W315dgVz9cpnxMMB6nH3LmEyvbmhJ0H FCwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=rlrhP3RcxnP/lWEjvQXqE58km0/BmwyIBIsnmqaKcFI=; b=sMern6FORmbkq2g0q0Qj+/khDES+POsPJk8XMTa7saN2jXtf6F3kKm6lFVD50MV3ho ZL/wRcpJKFmTCAE4UikSJ9d08td6+u45ID7f2OiK11cQxjwsZZAGGMB4QFnL07HvdtBR Alx7Ke+hXOUfKnyxFGXrqRSwvIbFC7a3B57Ls+kAa9a2naxoZTjUuGGMLBajU39GCH8/ BnHsGCpi3x48gethW0Lz04zUXjp7BcDj5xW10lmWqlVlB4iX+Uf/JE+uGEzsSI95YmIB g43GAJfIqxRgdfp5Ixqni9MD4WheEQRxJkCUYW2w9xVWszEL1QAmWYGk5ueYLybv4/Wy 4PsA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r26-20020aa7d15a000000b00447eb58b921si8501158edo.38.2022.11.07.06.37.30; Mon, 07 Nov 2022 06:37:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232420AbiKGOWp (ORCPT + 93 others); Mon, 7 Nov 2022 09:22:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232361AbiKGOW2 (ORCPT ); Mon, 7 Nov 2022 09:22:28 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7E96D2B0 for ; Mon, 7 Nov 2022 06:18:31 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 780B41FB; Mon, 7 Nov 2022 06:18:16 -0800 (PST) Received: from a077893.arm.com (unknown [10.163.40.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 7E2443F73D; Mon, 7 Nov 2022 06:18:07 -0800 (PST) From: Anshuman Khandual To: linux-arm-kernel@lists.infradead.org Cc: Anshuman Khandual , Catalin Marinas , Will Deacon , Mark Brown , Mark Rutland , Ard Biesheuvel , linux-kernel@vger.kernel.org Subject: [PATCH V3] arm64/mm: Simplify and document pte_to_phys() for 52 bit addresses Date: Mon, 7 Nov 2022 19:47:53 +0530 Message-Id: <20221107141753.2938621-1-anshuman.khandual@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pte_to_phys() assembly definition does multiple bits field transformations to derive physical address, embedded inside a page table entry. Unlike its C counter part i.e __pte_to_phys(), pte_to_phys() is not very apparent. It simplifies these operations via a new macro PTE_ADDR_HIGH_SHIFT indicating how far the pte encoded higher address bits need to be left shifted. While here, this also updates __pte_to_phys() and __phys_to_pte_val(). Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Brown Cc: Mark Rutland Cc: Ard Biesheuvel Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Ard Biesheuvel Suggested-by: Ard Biesheuvel Signed-off-by: Anshuman Khandual --- This applies on v6.1-rc4 Changes in V3: - Added tab after the mnemonics per Ard Changes in V2: https://lore.kernel.org/all/20221107114850.2902150-1-anshuman.khandual@arm.com/ - Added PTE_ADDR_HIGH_SHIFT based method per Ard Changes in V1: https://lore.kernel.org/all/20221031082421.1957288-1-anshuman.khandual@arm.com/ arch/arm64/include/asm/assembler.h | 8 +++----- arch/arm64/include/asm/pgtable-hwdef.h | 1 + arch/arm64/include/asm/pgtable.h | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h index e5957a53be39..89038067ef34 100644 --- a/arch/arm64/include/asm/assembler.h +++ b/arch/arm64/include/asm/assembler.h @@ -660,12 +660,10 @@ alternative_endif .endm .macro pte_to_phys, phys, pte -#ifdef CONFIG_ARM64_PA_BITS_52 - ubfiz \phys, \pte, #(48 - 16 - 12), #16 - bfxil \phys, \pte, #16, #32 - lsl \phys, \phys, #16 -#else and \phys, \pte, #PTE_ADDR_MASK +#ifdef CONFIG_ARM64_PA_BITS_52 + orr \phys, \phys, \phys, lsl #PTE_ADDR_HIGH_SHIFT + and \phys, \phys, GENMASK_ULL(PHYS_MASK_SHIFT - 1, PAGE_SHIFT) #endif .endm diff --git a/arch/arm64/include/asm/pgtable-hwdef.h b/arch/arm64/include/asm/pgtable-hwdef.h index 5ab8d163198f..f658aafc47df 100644 --- a/arch/arm64/include/asm/pgtable-hwdef.h +++ b/arch/arm64/include/asm/pgtable-hwdef.h @@ -159,6 +159,7 @@ #ifdef CONFIG_ARM64_PA_BITS_52 #define PTE_ADDR_HIGH (_AT(pteval_t, 0xf) << 12) #define PTE_ADDR_MASK (PTE_ADDR_LOW | PTE_ADDR_HIGH) +#define PTE_ADDR_HIGH_SHIFT 36 #else #define PTE_ADDR_MASK PTE_ADDR_LOW #endif diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 71a1af42f0e8..daedd6172227 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -77,11 +77,11 @@ extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]; static inline phys_addr_t __pte_to_phys(pte_t pte) { return (pte_val(pte) & PTE_ADDR_LOW) | - ((pte_val(pte) & PTE_ADDR_HIGH) << 36); + ((pte_val(pte) & PTE_ADDR_HIGH) << PTE_ADDR_HIGH_SHIFT); } static inline pteval_t __phys_to_pte_val(phys_addr_t phys) { - return (phys | (phys >> 36)) & PTE_ADDR_MASK; + return (phys | (phys >> PTE_ADDR_HIGH_SHIFT)) & PTE_ADDR_MASK; } #else #define __pte_to_phys(pte) (pte_val(pte) & PTE_ADDR_MASK) -- 2.25.1