From patchwork Tue Apr 18 21:19:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 9686379 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3E4E3602C2 for ; Tue, 18 Apr 2017 21:20:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E10E27816 for ; Tue, 18 Apr 2017 21:20:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 227242837F; Tue, 18 Apr 2017 21:20:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 677BC27816 for ; Tue, 18 Apr 2017 21:20:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757938AbdDRVUD (ORCPT ); Tue, 18 Apr 2017 17:20:03 -0400 Received: from mail-co1nam03on0046.outbound.protection.outlook.com ([104.47.40.46]:59447 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932908AbdDRVTc (ORCPT ); Tue, 18 Apr 2017 17:19:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=lhr04bLAd2vDwTSPQPr/EAtlxcTucoAz36Rgs04gLYI=; b=30LlroZdtMRIbhZXdGl3/Wd9isOYqwPjY9U1zS5a75HW7RfeI+0ASL4U1YRTFHtq7GQenrmG0Yu0MwYltyiLVq9ECn2fkoa0Iu9xmLao5rQtcpSmQIaDNvVWs4mIoiHmg8BUKcwkHfhn6+uVhWIuUDwel5lNO7fAOE+36Py+zS4= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=amd.com; Received: from tlendack-t1.amdoffice.net (165.204.77.1) by CY4PR12MB1141.namprd12.prod.outlook.com (10.168.163.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1034.10; Tue, 18 Apr 2017 21:19:23 +0000 From: Tom Lendacky Subject: [PATCH v5 17/32] x86/mm: Add support to access boot related data in the clear To: , , , , , , , , , CC: Rik van Riel , Radim =?utf-8?b?S3LEjW3DocWZ?= , Toshimitsu Kani , Arnd Bergmann , Jonathan Corbet , Matt Fleming , "Michael S. Tsirkin" , Joerg Roedel , Konrad Rzeszutek Wilk , Paolo Bonzini , Larry Woodman , Brijesh Singh , Ingo Molnar , Borislav Petkov , Andy Lutomirski , "H. Peter Anvin" , Andrey Ryabinin , Alexander Potapenko , Dave Young , Thomas Gleixner , Dmitry Vyukov Date: Tue, 18 Apr 2017 16:19:21 -0500 Message-ID: <20170418211921.10190.1537.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20170418211612.10190.82788.stgit@tlendack-t1.amdoffice.net> References: <20170418211612.10190.82788.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: CY4PR12CA0038.namprd12.prod.outlook.com (10.175.82.152) To CY4PR12MB1141.namprd12.prod.outlook.com (10.168.163.149) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a94d8f3d-5269-4bbd-2f66-08d486a096fe X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081)(201703131423075)(201703031133081); SRVR:CY4PR12MB1141; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1141; 3:Fb43aXkjRTo9FFwdotQeF99aLIpbhLP8TaFXCBDMhblNmuB1P1IwD/qdyu7YBk3sfESewM53U4NRV9tcVhX6InOpqnGzGOf27e94/NH0d6BvciAz8wE517YaJcwL0me0+U5DTfgdkXAuQa3v0qIW91rvouSEW4LbwRs7EkiafnQG4M/TNmJvW6SOXzAiTav/KYvwYHDpzIYrwKPTajpP1S0Xwz1nyUyvrNrs2J/OjYq+UJLGc38bdeb4qL+2drdj1d6WSNok6f7cA+QRbMbHFotvQ7IOL8ZgUI4ecFWvB+pyun37742ofAhl48+rBTBV2tlMhdJM10ZLAAGJuz6lFCEx+36nnqO3cH/AaBmyuiY=; 25:Jo0SqBOwjZ0/jXPS7wsHL/6iPXD7CREBoH5klegWpfWcrQcp9Ki4xlWFBbAoMwWnVdyCegcD56xR+0JJ0Pz/OlSr23GlOiZ+98tdwSfuBli8DRCMF8ua6x8/6oKwEXSTHf6yhGFWXcXVqHXHPyK5aV+SNolJRo4pPwYJ9iwe9ocFX8JtdiV55ZbkxHdAsD0Q8xIifP3dDxO+YDcwrqcHGCcGjPkbYYtXfXL/Ys06aeVhCUMYD3hDIj2K0qFhfuEgA7aWcohwPC6xNhPk8lM7WkoheeMN7DZVAbIU+v3zvSR8BnE0OrZVdqhzrAtkhfZcTDR8ZpC6tdbhopJye92fUFva8iZlulN98SWMKRia2pRW/5vU5AkV2pNqldnTTqPwji4GYsTHv05u0yvdFtzcl09eM15563KrSq0GSZnsgRhDAOHZWWeD4RxWHGbZTr8piW+6bG8GLl6CP1rv4ZeL6w== X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1141; 31:bsnispizAO/xst2qHzM77PE2/PBLYDYuegA1shcE5T5vDA+91RkLD5T43wg5Ab+p+wNy6Bt2ObnNH9AK6n3yTL7jLEkyv3dldwmu+saNLW3cRMTySZ2fpMmkOG0xeo7fmfg4mRozfUgLTIST8uiqSFicLDJ5PeNbWEssYTDjwPfwRbOmy0sgHAzvPaZ+1YpF1o2j8sGLC9qy/3VZtC8fGTPst/GrCxWyH15PYqcL11A=; 20:ynV9ls/St2cUhoHPIoedhKM5CDftm3f1NJURo7+Y/HztS8dw8yoYzvXn405Zogd16P+PigCBCr/lylvK8U6vaIR/ULXWFQ1C5Ox714MoBO5mmQ8H/MHJondkuG/DQOfFybrRGLC7hPnY3MhKRuR9ibT00ZTQmdGRPoyz/nfRfbpggIj8aHLAmXrAIk5rJ+1TqFZhrxFRIotM8gSNhjup+vRBIaFlDQO+BUGHQ8uno88fZKwKssoaQgZcFsQWXAVeMdAaxQl1HTRgEGvzuWDzAWZY4Bm9bWzCcRnvmx0x/X+5OiVkL75rU7c0KkbUbLP9B4Fd21NnU3FdjaqEYjH+ExFDX8ugeOv6gGtNfFQ4lP7BARlH3UVOZWIsQV6kfki30jeOxX2c9ANwuvRbSd6srIGrg295oWcdo9uCC0P3v2cT7TTU+vSZNAenFhTnuYJQ3JE8U0xIzMu22LKYkr9NXrX9EYpt/T5l0yY2LwokJgL7iUKkA3sucdMM3dULMfDd X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(93006095)(93001095)(3002001)(6055026)(6041248)(20161123562025)(20161123564025)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(20161123555025)(6072148); SRVR:CY4PR12MB1141; BCL:0; PCL:0; RULEID:; SRVR:CY4PR12MB1141; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1141; 4:F/ia6YmlVOcf9fC4CLJOwI1y4fTjMsdvvdnciLMWL128W7uUJAZr0q36lEO5pJnNsGP+QjyLZZuyKNRzXmDBZC9VUmOunw4NoqgzbtagXN8h874buLYYF4LdWOW/0gCC960BVmXGYK8kEuAkwolOlSSQnFIj1HHXdTUR4UMm68o3yFNy9YvLsexAU24MmQ+feQ8iWCdfD3WcCdLd6R+KwQ6YXATCKU+/0DPaPsOrUoecP4MSFbIvOc9E2dBS7POzYPM+IR5Vb1ovGLovApFDmohB2Wq2Ueg8qLJrWCLDaXlUj6liLLz5pfhPZTdDhy/Q3MocB/yK51hCM0hQJ9dxMe0XHis0W7zukw0xWsGjCp3gJ8efphiLKXI8UK5xaQ+WmQvWQqneQPD0q7z+Hz0a3p84pY7aOrHgIZIUzwpJb8E7YoLMxE7KjltX5V2rrAcqNVIXUEkP5OxhLqigCCS6JvlbrKOSXwjLV5fP0QeQzaEuBfd4FWbmgTJyeAaOQd4RKzZT/oSO1fv1dFGqIG2c/qnoIaT4dLlNO5RjuA68Wf++68tlBJKSI7ELd4MqVHC56TP1z+eQNwBArWSHJ98NpsM2btlZ2tYE+zHvDgXS7bnfg1Wlpkj43qyWTEpvNnsRKQy8zUwtZW8Fxo01CiRZJYNYP0STm9+7wydKuWnvUR9N/DUf460dBC1x+EJQUFjUSKL4ilXoat/AGR2sep2ECqdybaZRGbE+jL61VQHJHcwRGFY4tkXd+3yyvHFnX/dnUD0yWuBg5xXlGFNs+U5Kmw== X-Forefront-PRVS: 028166BF91 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(39450400003)(39860400002)(39850400002)(39840400002)(39400400002)(39410400002)(42186005)(3846002)(305945005)(25786009)(7406005)(50466002)(7736002)(38730400002)(2906002)(4001350100001)(575784001)(230700001)(53416004)(86362001)(2950100002)(5660300001)(103116003)(23676002)(6116002)(7416002)(2201001)(33646002)(1076002)(50986999)(54356999)(81166006)(55016002)(76176999)(97746001)(83506001)(66066001)(189998001)(9686003)(4326008)(6506006)(47776003)(53936002)(54906002)(8676002)(921003)(71626007)(1121003)(357404004); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR12MB1141; H:tlendack-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5EdQbWTHJZmU5xgN3t?= X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1141; 6:va1vf+TOPeqN9u0OozUzqOG7pCtz4RtA4e3JKUOXiH8H4JBB5yeur8A2bDjaCq75+NHvuR8y0qaDKvzhw0G7rGDQW/Kd8Y2LPSWT2BFyO5cpZPC05zX9fqKjn5zNSEXvGSnvTjKj9pvEMEgJT4n9HhOsdIOztUFwvqvDvBRMn8pt/XKobPk/1OiybklJLBoLuC6y6/P/DT7Mo7n/BoW9dCtfhMb56E3QVlKk+8tctuuCdHZ9VUIYcB8G47XVKfNNztvs9hIqzKBgx77lGRa2AP30A4zBVF3CPjTer6rLwbrCgPX6nLRLjkViQxk95Z/XLFUNSctcW+9ICyHkpvBoszFOdVFj6Ai/8p0jkM14wly9034mUob0zdnQxzh4Fh0fnZk8qqcHDHZeH3C9SNfO3IhkRT+vaS+tlfPqrrbL4/pmFNfd9mlEIKl2/5PeLSDpeac9nAHC2TV9j3zE1x9YSJq0xOddjJPJjXcz9X6emLw=; 5:EMwyFjW81h+zozKX/QmOf71953UCjkJVGfbSejyqyoY2+6HdBziYMOZE9gcxJ0EiI36CQvRFLvMtp1AqDeqG46QQrLnDLQj/cIhgobc7PX4GzX+lFJ9pRpC252u+r3h28Sr7XJeY4dHRpz+z/AUDVQ==; 24:0HubAuOlAm4V3ij9FDGyApp4s1YyZgLzc8sJy/4w2txYYFH8BkdqJYjvPPUvwh/U1Rh6G44teFt/BHKGPexHSbSCm2N8ez9HxeSjAOz2pfU= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1141; 7:dziz7ctHIed6yAEmQf4cnkgMWk5ucJZBjyHlz/VuoWqu2s54vTVOL7VnqcF/s9uzGUKvXJrgHmsIeUE20vvKfZEMPGbIZ/1cOjeocPopTFv5N1OvlC5ixPsTmr00KFo2hxyxaahRQwyWp7l+g63bczC3NxFG906Q8SI8WSs5TgAcdA7K/EOC1M7fCexizYEpQg6NAWm182SmfF98iRGtpIkAvfPRebSXxDw76u1ydYRGM445gQnsRzFEnIjkFQrxQJ5xjgAtz2vu9d3dzoaRggEAkVmUAvXcoX3rIudhZYqoJt0mAiENoKzlu8R+nfDs5VeOH8grWBZA0PLkNifEXQ==; 20:sW58rlFngY2IR7jrQYQo24S8dw6voi9yxmESEcx3L4QbCzRLJLS6jvY8iHzz9KjBENDMGDG+RFu5GdzAUSXUxOULsg/GaxxkYkRF8ddhKuVE3ZtA3FBo6ugWEbYVipdEhR900+1tPueEBsAmAvj2xEOkovaHGmgmv3Smi7i2vRcok0bJqqDEvn5Fi5yuUQkQ4ZO8rDi5nvvDOia/P1s8Y3qYtRcJxVa0IfSCQocvfMkrnjoYQWJbBgIdSbMbzfAH X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2017 21:19:23.4707 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1141 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Boot data (such as EFI related data) is not encrypted when the system is booted because UEFI/BIOS does not run with SME active. In order to access this data properly it needs to be mapped decrypted. The early_memremap() support is updated to provide an arch specific routine to modify the pagetable protection attributes before they are applied to the new mapping. This is used to remove the encryption mask for boot related data. The memremap() support is updated to provide an arch specific routine to determine if RAM remapping is allowed. RAM remapping will cause an encrypted mapping to be generated. By preventing RAM remapping, ioremap_cache() will be used instead, which will provide a decrypted mapping of the boot related data. Signed-off-by: Tom Lendacky --- arch/x86/include/asm/io.h | 4 + arch/x86/mm/ioremap.c | 182 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/io.h | 2 kernel/memremap.c | 20 ++++- mm/early_ioremap.c | 18 ++++ 5 files changed, 219 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/io.h b/arch/x86/include/asm/io.h index 7afb0e2..75f2858 100644 --- a/arch/x86/include/asm/io.h +++ b/arch/x86/include/asm/io.h @@ -381,4 +381,8 @@ extern int __must_check arch_phys_wc_add(unsigned long base, #define arch_io_reserve_memtype_wc arch_io_reserve_memtype_wc #endif +extern bool arch_memremap_do_ram_remap(resource_size_t offset, size_t size, + unsigned long flags); +#define arch_memremap_do_ram_remap arch_memremap_do_ram_remap + #endif /* _ASM_X86_IO_H */ diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c index 9bfcb1f..bce0604 100644 --- a/arch/x86/mm/ioremap.c +++ b/arch/x86/mm/ioremap.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include #include "physaddr.h" @@ -419,6 +421,186 @@ void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr) iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK)); } +/* + * Examine the physical address to determine if it is an area of memory + * that should be mapped decrypted. If the memory is not part of the + * kernel usable area it was accessed and created decrypted, so these + * areas should be mapped decrypted. + */ +static bool memremap_should_map_decrypted(resource_size_t phys_addr, + unsigned long size) +{ + /* Check if the address is outside kernel usable area */ + switch (e820__get_entry_type(phys_addr, phys_addr + size - 1)) { + case E820_TYPE_RESERVED: + case E820_TYPE_ACPI: + case E820_TYPE_NVS: + case E820_TYPE_UNUSABLE: + return true; + default: + break; + } + + return false; +} + +/* + * Examine the physical address to determine if it is EFI data. Check + * it against the boot params structure and EFI tables and memory types. + */ +static bool memremap_is_efi_data(resource_size_t phys_addr, + unsigned long size) +{ + u64 paddr; + + /* Check if the address is part of EFI boot/runtime data */ + if (efi_enabled(EFI_BOOT)) { + paddr = boot_params.efi_info.efi_memmap_hi; + paddr <<= 32; + paddr |= boot_params.efi_info.efi_memmap; + if (phys_addr == paddr) + return true; + + paddr = boot_params.efi_info.efi_systab_hi; + paddr <<= 32; + paddr |= boot_params.efi_info.efi_systab; + if (phys_addr == paddr) + return true; + + if (efi_table_address_match(phys_addr)) + return true; + + switch (efi_mem_type(phys_addr)) { + case EFI_BOOT_SERVICES_DATA: + case EFI_RUNTIME_SERVICES_DATA: + return true; + default: + break; + } + } + + return false; +} + +/* + * Examine the physical address to determine if it is boot data by checking + * it against the boot params setup_data chain. + */ +static bool memremap_is_setup_data(resource_size_t phys_addr, + unsigned long size) +{ + struct setup_data *data; + u64 paddr, paddr_next; + + paddr = boot_params.hdr.setup_data; + while (paddr) { + bool is_setup_data = false; + + if (phys_addr == paddr) + return true; + + data = memremap(paddr, sizeof(*data), + MEMREMAP_WB | MEMREMAP_DEC); + + paddr_next = data->next; + + if ((phys_addr > paddr) && (phys_addr < (paddr + data->len))) + is_setup_data = true; + + memunmap(data); + + if (is_setup_data) + return true; + + paddr = paddr_next; + } + + return false; +} + +/* + * Examine the physical address to determine if it is boot data by checking + * it against the boot params setup_data chain (early boot version). + */ +static bool __init early_memremap_is_setup_data(resource_size_t phys_addr, + unsigned long size) +{ + struct setup_data *data; + u64 paddr, paddr_next; + + paddr = boot_params.hdr.setup_data; + while (paddr) { + bool is_setup_data = false; + + if (phys_addr == paddr) + return true; + + data = early_memremap_decrypted(paddr, sizeof(*data)); + + paddr_next = data->next; + + if ((phys_addr > paddr) && (phys_addr < (paddr + data->len))) + is_setup_data = true; + + early_memunmap(data, sizeof(*data)); + + if (is_setup_data) + return true; + + paddr = paddr_next; + } + + return false; +} + +/* + * Architecture function to determine if RAM remap is allowed. By default, a + * RAM remap will map the data as encrypted. Determine if a RAM remap should + * not be done so that the data will be mapped decrypted. + */ +bool arch_memremap_do_ram_remap(resource_size_t phys_addr, unsigned long size, + unsigned long flags) +{ + if (!sme_active()) + return true; + + if (flags & MEMREMAP_ENC) + return true; + + if (flags & MEMREMAP_DEC) + return false; + + if (memremap_is_setup_data(phys_addr, size) || + memremap_is_efi_data(phys_addr, size) || + memremap_should_map_decrypted(phys_addr, size)) + return false; + + return true; +} + +/* + * Architecture override of __weak function to adjust the protection attributes + * used when remapping memory. By default, early_memremp() will map the data + * as encrypted. Determine if an encrypted mapping should not be done and set + * the appropriate protection attributes. + */ +pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr, + unsigned long size, + pgprot_t prot) +{ + if (!sme_active()) + return prot; + + if (early_memremap_is_setup_data(phys_addr, size) || + memremap_is_efi_data(phys_addr, size) || + memremap_should_map_decrypted(phys_addr, size)) + prot = pgprot_decrypted(prot); + else + prot = pgprot_encrypted(prot); + + return prot; +} + #ifdef CONFIG_ARCH_USE_MEMREMAP_PROT /* Remap memory with encryption */ void __init *early_memremap_encrypted(resource_size_t phys_addr, diff --git a/include/linux/io.h b/include/linux/io.h index 82ef36e..deaeb1d 100644 --- a/include/linux/io.h +++ b/include/linux/io.h @@ -136,6 +136,8 @@ enum { MEMREMAP_WB = 1 << 0, MEMREMAP_WT = 1 << 1, MEMREMAP_WC = 1 << 2, + MEMREMAP_ENC = 1 << 3, + MEMREMAP_DEC = 1 << 4, }; void *memremap(resource_size_t offset, size_t size, unsigned long flags); diff --git a/kernel/memremap.c b/kernel/memremap.c index 07e85e5..2361bf7 100644 --- a/kernel/memremap.c +++ b/kernel/memremap.c @@ -34,13 +34,24 @@ static void *arch_memremap_wb(resource_size_t offset, unsigned long size) } #endif -static void *try_ram_remap(resource_size_t offset, size_t size) +#ifndef arch_memremap_do_ram_remap +static bool arch_memremap_do_ram_remap(resource_size_t offset, size_t size, + unsigned long flags) +{ + return true; +} +#endif + +static void *try_ram_remap(resource_size_t offset, size_t size, + unsigned long flags) { unsigned long pfn = PHYS_PFN(offset); /* In the simple case just return the existing linear address */ - if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn))) + if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn)) && + arch_memremap_do_ram_remap(offset, size, flags)) return __va(offset); + return NULL; /* fallback to arch_memremap_wb */ } @@ -48,7 +59,8 @@ static void *try_ram_remap(resource_size_t offset, size_t size) * memremap() - remap an iomem_resource as cacheable memory * @offset: iomem resource start address * @size: size of remap - * @flags: any of MEMREMAP_WB, MEMREMAP_WT and MEMREMAP_WC + * @flags: any of MEMREMAP_WB, MEMREMAP_WT, MEMREMAP_WC, + * MEMREMAP_ENC, MEMREMAP_DEC * * memremap() is "ioremap" for cases where it is known that the resource * being mapped does not have i/o side effects and the __iomem @@ -95,7 +107,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags) * the requested range is potentially in System RAM. */ if (is_ram == REGION_INTERSECTS) - addr = try_ram_remap(offset, size); + addr = try_ram_remap(offset, size, flags); if (!addr) addr = arch_memremap_wb(offset, size); } diff --git a/mm/early_ioremap.c b/mm/early_ioremap.c index d7d30da..b1dd4a9 100644 --- a/mm/early_ioremap.c +++ b/mm/early_ioremap.c @@ -30,6 +30,13 @@ static int __init early_ioremap_debug_setup(char *str) static int after_paging_init __initdata; +pgprot_t __init __weak early_memremap_pgprot_adjust(resource_size_t phys_addr, + unsigned long size, + pgprot_t prot) +{ + return prot; +} + void __init __weak early_ioremap_shutdown(void) { } @@ -215,14 +222,19 @@ void __init early_iounmap(void __iomem *addr, unsigned long size) void __init * early_memremap(resource_size_t phys_addr, unsigned long size) { - return (__force void *)__early_ioremap(phys_addr, size, - FIXMAP_PAGE_NORMAL); + pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size, + FIXMAP_PAGE_NORMAL); + + return (__force void *)__early_ioremap(phys_addr, size, prot); } #ifdef FIXMAP_PAGE_RO void __init * early_memremap_ro(resource_size_t phys_addr, unsigned long size) { - return (__force void *)__early_ioremap(phys_addr, size, FIXMAP_PAGE_RO); + pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size, + FIXMAP_PAGE_RO); + + return (__force void *)__early_ioremap(phys_addr, size, prot); } #endif