Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1630722rwb; Mon, 7 Nov 2022 04:05:48 -0800 (PST) X-Google-Smtp-Source: AMsMyM7ba+xsRk9tkJuBNSathHMLqi1NCzwKb+PvKdELv6aBGLjrBLSofw+l9bEGI2/q06q62eVd X-Received: by 2002:a17:907:b07:b0:7a8:beb3:aa2a with SMTP id h7-20020a1709070b0700b007a8beb3aa2amr47384074ejl.459.1667822747749; Mon, 07 Nov 2022 04:05:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667822747; cv=none; d=google.com; s=arc-20160816; b=pffvkkJ7lnOKjjNadlnPlAvyM+P0syTiBXrBPEd0G3E6LsQXEaIhwbl05fLycwxn/b kVc+nfaOubutV/KGUnq9fxwRotSr0eBDS2WEuc2JSc+08Pa6K8dFNn9znhLL0WHIFaog 3hhHNbXAYIL4Z+bFo+9bCV8lAnMM0HgJ7ggndt70aFUepsUMzjE5adpUqqpaXjCwWkOe uN6yKajRp4W0Hp+LmY/LDf1JBE4Jv5YnGgeWvepDkxLK2paGQvE53QajbHBvJFocrHsN ewj8TZVMFMRnxUjAVC1N7X1iQ3PQuG+TVHOIsOvGL52cd3B0qhaP0FWxs8KNSRGxiQKP /54A== 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=l2YJe232AjbBrXVSLEcOhE/jd9OJMavq43Y5tZiYdYg=; b=YmG5uvDPDvdC+vA7x9GYdynSwp04u5is2gQEOSxrRlgaZLjg+zyowL0T0cuY8vH6mn 4xkpgbvNgU36d86yyjej7Vl3chg8TwhiJq6mqKNwUlaNIKv7aoqVRJNxR9AOzjsEWZUA 3wG65sr2SoiXwKZXiHbTn3hUsTbbnMTYkQgmOdH464cLfC2a5+kG9ZJHHMdiyZ/tlak5 J2YmbJOY03kzAsDkldE9WuU2LG4ToNGut91JDngnzExiX+OrwBkBI5C1NI2JKp3A0YMq XfN3jMFRytlpTyqU70DqAk6hxy6FhSLRsEvPNaXU58O2ftV8BtTAgzqPjZABXrexdel6 eUWg== 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 m14-20020a50ef0e000000b00457e6752422si7956018eds.189.2022.11.07.04.05.19; Mon, 07 Nov 2022 04:05:47 -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 S231949AbiKGLtN (ORCPT + 93 others); Mon, 7 Nov 2022 06:49:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231995AbiKGLtH (ORCPT ); Mon, 7 Nov 2022 06:49:07 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BA49A1AD96 for ; Mon, 7 Nov 2022 03:49:04 -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 934B01FB; Mon, 7 Nov 2022 03:49:10 -0800 (PST) Received: from a077893.arm.com (unknown [10.163.40.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 6087B3F73D; Mon, 7 Nov 2022 03:49:01 -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 V2] arm64/mm: Simplify and document pte_to_phys() for 52 bit addresses Date: Mon, 7 Nov 2022 17:18:50 +0530 Message-Id: <20221107114850.2902150-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 Suggested-by: Ard Biesheuvel Signed-off-by: Anshuman Khandual --- This applies on v6.1-rc4 Changes in V2: - 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..6a39a3601cf7 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