From patchwork Wed Jan 8 19:50:53 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Salter X-Patchwork-Id: 22986 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qa0-f70.google.com (mail-qa0-f70.google.com [209.85.216.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 99C7C204BC for ; Wed, 8 Jan 2014 19:51:24 +0000 (UTC) Received: by mail-qa0-f70.google.com with SMTP id j5sf3540408qaq.1 for ; Wed, 08 Jan 2014 11:51:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=X1XDSAarscAOsFO18Y0VTWwsFrBBD7RaCAHvVB8numQ=; b=VfgaGWv9UnQxozbP003D/EEYIUjGlNgR9d9tYiS/W0qxFRkFW3Tee9DwAD5kC7XVBZ bVxaiy8dJPzGvZ77So4uL90Aql4igFy4huGsmw0CMyWfD7UzeJs2MD+768u7/aF9UXtH Me9GTF2NBHv820iEsthGqfAbiXH8hdr8jWMYA7eDBenusZ5RJscva0SVk5W+IjRzRZX/ 04h2Ptm7dUQxwkE0SNToEPn3vu/8PQtHiXDQwVO9u7ykfQ8G5N+/0Yhjsdx772TmBcLF 19sf3r00KsZyo7D5mnUCq9sEZX5a5WO4y7gSCws1ZYO6tdCBfDDXNCbeprrcbgi+xmtd apjA== X-Gm-Message-State: ALoCoQkJYyHLg1eToIKP5rvnCWFl05LZEK7hVB9+84Rlq1+kYmU1UgnreXvBGAA/fu1SUgSXC5gd X-Received: by 10.236.191.136 with SMTP id g8mr1547811yhn.48.1389210683539; Wed, 08 Jan 2014 11:51:23 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.116.108 with SMTP id jv12ls832393qeb.18.gmail; Wed, 08 Jan 2014 11:51:23 -0800 (PST) X-Received: by 10.58.186.233 with SMTP id fn9mr5980208vec.31.1389210683403; Wed, 08 Jan 2014 11:51:23 -0800 (PST) Received: from mail-vb0-f53.google.com (mail-vb0-f53.google.com [209.85.212.53]) by mx.google.com with ESMTPS id vq3si1155691veb.141.2014.01.08.11.51.23 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 08 Jan 2014 11:51:23 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.53 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.53; Received: by mail-vb0-f53.google.com with SMTP id o19so1481142vbm.26 for ; Wed, 08 Jan 2014 11:51:23 -0800 (PST) X-Received: by 10.53.8.1 with SMTP id dg1mr4879475vdd.52.1389210683298; Wed, 08 Jan 2014 11:51:23 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.59.13.131 with SMTP id ey3csp248805ved; Wed, 8 Jan 2014 11:51:22 -0800 (PST) X-Received: by 10.66.192.38 with SMTP id hd6mr15049379pac.146.1389210681684; Wed, 08 Jan 2014 11:51:21 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id xa2si1458563pab.200.2014.01.08.11.51.21; Wed, 08 Jan 2014 11:51:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757096AbaAHTvP (ORCPT + 26 others); Wed, 8 Jan 2014 14:51:15 -0500 Received: from mx1.redhat.com ([209.132.183.28]:1348 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750861AbaAHTvN (ORCPT ); Wed, 8 Jan 2014 14:51:13 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s08JpAlp028116 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 8 Jan 2014 14:51:10 -0500 Received: from deneb.redhat.com (ovpn-113-105.phx2.redhat.com [10.3.113.105]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s08Jp9YD031864; Wed, 8 Jan 2014 14:51:09 -0500 From: Mark Salter To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, Matt Fleming , x86@kernel.org, leif.lindholm@linaro.org, Roy Franz , Mark Salter Subject: [PATCH] x86: efi: create memory map iteration helper Date: Wed, 8 Jan 2014 14:50:53 -0500 Message-Id: <1389210653-15271-1-git-send-email-msalter@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: msalter@redhat.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.53 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , There are about a dozen places in the x86 EFI code which loop through the EFI memory map descriptors. All of these places use essentially the same for-loop code. This patch adds a for_each_efi_memory_desc() helper to clean up all of the duplicate code and make it available for other architectures to use. Signed-off-by: Mark Salter --- arch/x86/platform/efi/efi.c | 48 +++++++++++++----------------------------- arch/x86/platform/efi/efi_64.c | 5 +---- drivers/firmware/efi/efi.c | 5 ++--- include/linux/efi.h | 6 ++++++ 4 files changed, 24 insertions(+), 40 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index cceb813..211aa57 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -328,10 +328,9 @@ void efi_get_time(struct timespec *now) static void __init do_add_efi_memmap(void) { - void *p; + efi_memory_desc_t *md; - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - efi_memory_desc_t *md = p; + for_each_efi_memory_desc(&memmap, md) { unsigned long long start = md->phys_addr; unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; int e820_type; @@ -402,16 +401,12 @@ int __init efi_memblock_x86_reserve_range(void) static void __init print_efi_memmap(void) { efi_memory_desc_t *md; - void *p; - int i; + int i = 0; - for (p = memmap.map, i = 0; - p < memmap.map_end; - p += memmap.desc_size, i++) { - md = p; + for_each_efi_memory_desc(&memmap, md) { pr_info("mem%02u: type=%u, attr=0x%llx, " "range=[0x%016llx-0x%016llx) (%lluMB)\n", - i, md->type, md->attribute, md->phys_addr, + i++, md->type, md->attribute, md->phys_addr, md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), (md->num_pages >> (20 - EFI_PAGE_SHIFT))); } @@ -420,10 +415,9 @@ static void __init print_efi_memmap(void) void __init efi_reserve_boot_services(void) { - void *p; + efi_memory_desc_t *md; - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - efi_memory_desc_t *md = p; + for_each_efi_memory_desc(&memmap, md) { u64 start = md->phys_addr; u64 size = md->num_pages << EFI_PAGE_SHIFT; @@ -461,13 +455,12 @@ void __init efi_unmap_memmap(void) void __init efi_free_boot_services(void) { - void *p; + efi_memory_desc_t *md; if (!efi_is_native()) return; - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - efi_memory_desc_t *md = p; + for_each_efi_memory_desc(&memmap, md) { unsigned long long start = md->phys_addr; unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; @@ -718,12 +711,9 @@ void __init efi_set_executable(efi_memory_desc_t *md, bool executable) static void __init runtime_code_page_mkexec(void) { efi_memory_desc_t *md; - void *p; /* Make EFI runtime service code area executable */ - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - md = p; - + for_each_efi_memory_desc(&memmap, md) { if (md->type != EFI_RUNTIME_SERVICES_CODE) continue; @@ -755,7 +745,7 @@ void __init efi_enter_virtual_mode(void) efi_status_t status; unsigned long size; u64 end, systab, start_pfn, end_pfn; - void *p, *va, *new_memmap = NULL; + void *va, *new_memmap = NULL; int count = 0; efi.systab = NULL; @@ -771,9 +761,8 @@ void __init efi_enter_virtual_mode(void) } /* Merge contiguous regions of the same type and attribute */ - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { + for_each_efi_memory_desc(&memmap, md) { u64 prev_size; - md = p; if (!prev_md) { prev_md = md; @@ -797,8 +786,7 @@ void __init efi_enter_virtual_mode(void) prev_md = md; } - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - md = p; + for_each_efi_memory_desc(&memmap, md) { if (!(md->attribute & EFI_MEMORY_RUNTIME)) { #ifdef CONFIG_X86_64 if (md->type != EFI_BOOT_SERVICES_CODE && @@ -895,33 +883,27 @@ void __init efi_enter_virtual_mode(void) u32 efi_mem_type(unsigned long phys_addr) { efi_memory_desc_t *md; - void *p; if (!efi_enabled(EFI_MEMMAP)) return 0; - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - md = p; + for_each_efi_memory_desc(&memmap, md) if ((md->phys_addr <= phys_addr) && (phys_addr < (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)))) return md->type; - } return 0; } u64 efi_mem_attributes(unsigned long phys_addr) { efi_memory_desc_t *md; - void *p; - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - md = p; + for_each_efi_memory_desc(&memmap, md) if ((md->phys_addr <= phys_addr) && (phys_addr < (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)))) return md->attribute; - } return 0; } diff --git a/arch/x86/platform/efi/efi_64.c b/arch/x86/platform/efi/efi_64.c index 39a0e7f..90aaefc 100644 --- a/arch/x86/platform/efi/efi_64.c +++ b/arch/x86/platform/efi/efi_64.c @@ -45,18 +45,15 @@ static unsigned long efi_flags __initdata; static void __init early_code_mapping_set_exec(int executable) { efi_memory_desc_t *md; - void *p; if (!(__supported_pte_mask & _PAGE_NX)) return; /* Make EFI service code area executable */ - for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { - md = p; + for_each_efi_memory_desc(&memmap, md) if (md->type == EFI_RUNTIME_SERVICES_CODE || md->type == EFI_BOOT_SERVICES_CODE) efi_set_executable(md, executable); - } } void __init efi_call_phys_prelog(void) diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 2e2fbde..70648da 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -158,14 +158,13 @@ subsys_initcall(efisubsys_init); void __iomem *efi_lookup_mapped_addr(u64 phys_addr) { struct efi_memory_map *map; - void *p; + efi_memory_desc_t *md; map = efi.memmap; if (!map) return NULL; if (WARN_ON(!map->map)) return NULL; - for (p = map->map; p < map->map_end; p += map->desc_size) { - efi_memory_desc_t *md = p; + for_each_efi_memory_desc(map, md) { u64 size = md->num_pages << EFI_PAGE_SHIFT; u64 end = md->phys_addr + size; if (!(md->attribute & EFI_MEMORY_RUNTIME) && diff --git a/include/linux/efi.h b/include/linux/efi.h index 11ce678..9dc5b05 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -618,6 +618,12 @@ extern int efi_set_rtc_mmss(const struct timespec *now); extern void efi_reserve_boot_services(void); extern struct efi_memory_map memmap; +/* Iterate through an efi_memory_map */ +#define for_each_efi_memory_desc(m, md) \ + for ((md) = (m)->map; \ + (md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \ + (md) = (void *)(md) + (m)->desc_size) + /** * efi_range_is_wc - check the WC bit on an address range * @start: starting kvirt address