aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
authorGraeme Gregory <graeme.gregory@linaro.org>2013-12-04 12:05:18 +0000
committerGraeme Gregory <graeme.gregory@linaro.org>2013-12-04 12:05:18 +0000
commitaca2a2cda8ae17c08fc1c56581c47ed970de41b7 (patch)
tree1bafcfbffe9a89b261698e42cc9e5017a31dd1b8 /arch
parent977623b23419d6349c4f84ad6f2b969f31fc5e57 (diff)
parentbd8676e7bd1c213d944b7c9404615cd7ce02cba4 (diff)
Merge branch 'msalter/armv8-uefi-latest' of git://git.linaro.org/arm/acpi/acpi into leg-kernel
Conflicts: Documentation/devicetree/bindings/staging/dwc2.txt arch/arm/Kconfig arch/arm/boot/dts/bcm11351-brt.dts arch/arm/boot/dts/bcm11351.dtsi arch/arm/boot/dts/bcm28155-ap.dts arch/arm/common/Kconfig drivers/Kconfig drivers/cpufreq/arm_big_little.c drivers/cpufreq/arm_big_little.h drivers/cpufreq/cpufreq_interactive.c drivers/cpufreq/cpufreq_stats.c drivers/mmc/core/sd.c drivers/usb/gadget/s3c-hsotg.c include/linux/efi.h include/uapi/linux/input.h kernel/time/alarmtimer.c kernel/trace/trace_functions_graph.c lib/Kconfig.debug mm/page_alloc.c
Diffstat (limited to 'arch')
-rw-r--r--arch/arm/configs/bcm_defconfig1
-rw-r--r--arch/arm64/.gitignore8
-rw-r--r--arch/arm64/Kconfig23
-rw-r--r--arch/arm64/Makefile1
-rw-r--r--arch/arm64/efi/Makefile32
-rw-r--r--arch/arm64/efi/efi-entry.S87
-rw-r--r--arch/arm64/efi/efi-stub.c318
-rw-r--r--arch/arm64/efi/efi.c529
-rw-r--r--arch/arm64/efi/libfdt_env.h15
-rw-r--r--arch/arm64/include/asm/efi.h18
-rw-r--r--arch/arm64/include/asm/fixmap.h117
-rw-r--r--arch/arm64/include/asm/io.h8
-rw-r--r--arch/arm64/include/asm/memory.h2
-rw-r--r--arch/arm64/kernel/early_printk.c8
-rw-r--r--arch/arm64/kernel/head.S114
-rw-r--r--arch/arm64/kernel/setup.c8
-rw-r--r--arch/arm64/kernel/vmlinux.lds.S31
-rw-r--r--arch/arm64/mm/ioremap.c295
-rw-r--r--arch/arm64/mm/mmu.c41
19 files changed, 1581 insertions, 75 deletions
diff --git a/arch/arm/configs/bcm_defconfig b/arch/arm/configs/bcm_defconfig
index e3d11d731a46..a8e91db88d89 100644
--- a/arch/arm/configs/bcm_defconfig
+++ b/arch/arm/configs/bcm_defconfig
@@ -25,6 +25,7 @@ CONFIG_MODULE_UNLOAD=y
# CONFIG_BLK_DEV_BSG is not set
CONFIG_PARTITION_ADVANCED=y
CONFIG_ARCH_BCM=y
+CONFIG_ARCH_BCM_MOBILE=y
CONFIG_ARM_THUMBEE=y
CONFIG_PREEMPT=y
CONFIG_AEABI=y
diff --git a/arch/arm64/.gitignore b/arch/arm64/.gitignore
new file mode 100644
index 000000000000..f597eb929238
--- /dev/null
+++ b/arch/arm64/.gitignore
@@ -0,0 +1,8 @@
+
+efi/fdt.h
+efi/libfdt.h
+efi/libfdt_internal.h
+efi/fdt_rw.c
+efi/fdt_ro.c
+efi/fdt_wip.c
+efi/fdt.c
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index c04454876bcb..bce0e922ad36 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -239,6 +239,27 @@ config CMDLINE_FORCE
This is useful if you cannot or don't want to change the
command-line options your boot loader passes to the kernel.
+config EFI
+ bool "UEFI runtime service support"
+ depends on !CPU_BIG_ENDIAN && !ARM64_64K_PAGES && OF
+ select UCS2_STRING
+ ---help---
+ This enables the kernel to use UEFI runtime services that are
+ available (such as the UEFI variable services).
+
+ This option is only useful on systems that have UEFI firmware.
+ However, even with this option, the resultant kernel should
+ continue to boot on existing non-UEFI platforms.
+
+config EFI_STUB
+ bool "EFI stub support"
+ depends on !CPU_BIG_ENDIAN && !ARM64_64K_PAGES && OF
+ default y
+ help
+ This kernel feature allows an Image to be loaded directly
+ by EFI firmware without the use of a bootloader.
+ See Documentation/efi-stub.txt for more information.
+
endmenu
menu "Userspace binary formats"
@@ -270,6 +291,8 @@ source "net/Kconfig"
source "drivers/Kconfig"
+source "drivers/firmware/Kconfig"
+
source "fs/Kconfig"
source "arch/arm64/kvm/Kconfig"
diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index d90cf79f233a..9a9c858d421c 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -39,6 +39,7 @@ export TEXT_OFFSET GZFLAGS
core-y += arch/arm64/kernel/ arch/arm64/mm/
core-$(CONFIG_KVM) += arch/arm64/kvm/
core-$(CONFIG_XEN) += arch/arm64/xen/
+core-$(findstring y,$(CONFIG_EFI)$(CONFIG_EFI_STUB)) += arch/arm64/efi/
libs-y := arch/arm64/lib/ $(libs-y)
libs-y += $(LIBGCC)
diff --git a/arch/arm64/efi/Makefile b/arch/arm64/efi/Makefile
new file mode 100644
index 000000000000..c571f71ba5c1
--- /dev/null
+++ b/arch/arm64/efi/Makefile
@@ -0,0 +1,32 @@
+#
+# This file is subject to the terms and conditions of the GNU General Public
+# License. See the file "COPYING" in the main directory of this archive
+# for more details.
+#
+# Copyright (C) 2013, Red Hat, Inc.
+# Author: Mark Salter <msalter@redhat.com>
+#
+
+ifeq ($(CONFIG_EFI),y)
+efi_objs += efi.o
+endif
+ifeq ($(CONFIG_EFI_STUB),y)
+efi_objs += efi-stub.o efi-entry.o
+endif
+
+libfdt_src := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c
+libfdt_hdrs := fdt.h libfdt.h libfdt_internal.h
+
+libfdt_objs := $(addsuffix .o, $(basename $(libfdt_src)))
+
+$(addprefix $(obj)/,$(libfdt_src) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/libfdt/%
+ $(call cmd,shipped)
+
+$(addprefix $(obj)/,$(libfdt_objs)): $(addprefix $(obj)/,$(libfdt_hdrs))
+
+CFLAGS_efi-stub.o += -DTEXT_OFFSET=$(TEXT_OFFSET)
+ccflags-y := -I$(obj) -I$(srctree)/$(obj)
+
+obj-y += $(libfdt_objs) $(efi_objs)
+
+clean-files := $(libfdt_src) $(libfdt_hdrs)
diff --git a/arch/arm64/efi/efi-entry.S b/arch/arm64/efi/efi-entry.S
new file mode 100644
index 000000000000..789968210331
--- /dev/null
+++ b/arch/arm64/efi/efi-entry.S
@@ -0,0 +1,87 @@
+/*
+ * EFI entry point.
+ *
+ * Copyright (C) 2013 Red Hat, Inc.
+ * Author: Mark Salter <msalter@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/linkage.h>
+#include <linux/init.h>
+
+#include <asm/assembler.h>
+
+#define EFI_LOAD_ERROR 0x8000000000000001
+
+ /*
+ * We arrive here from the EFI boot manager with:
+ *
+ * * MMU on with identity-mapped RAM.
+ * * Icache and Dcache on
+ *
+ * We will most likely be running from some place other than where
+ * we want to be. The kernel image wants to be placed at TEXT_OFFSET
+ * from start of RAM.
+ *
+ */
+ENTRY(efi_stub_entry)
+ stp x29, x30, [sp, #-32]!
+
+ /*
+ * Call efi_entry to do the real work.
+ * x0 and x1 are already set up by firmware. Current runtime
+ * address of image is calculated and passed via *image_addr.
+ *
+ * unsigned long efi_entry(void *handle,
+ * efi_system_table_t *sys_table,
+ * unsigned long *image_addr) ;
+ */
+ adrp x8, _text
+ add x8, x8, #:lo12:_text
+ add x2, sp, 16
+ str x8, [x2]
+ bl efi_entry
+ cmn x0, #1
+ b.eq efi_load_fail
+
+ /*
+ * efi_entry() will have relocated the kernel image if necessary
+ * and we return here with device tree address in x0 and the kernel
+ * entry point stored at *image_addr. Save those values in registers
+ * which are preserved by __flush_dcache_all.
+ */
+ ldr x1, [sp, #16]
+ mov x20, x0
+ mov x21, x1
+
+ bl __flush_dcache_all
+ /* Turn off Dcache and MMU */
+ mrs x0, sctlr_el1
+ bic x0, x0, #1 << 0 // clear SCTLR.M
+ bic x0, x0, #1 << 2 // clear SCTLR.C
+ msr sctlr_el1, x0
+ isb
+
+ /* Jump to real entry point */
+ mov x0, x20
+ mov x1, xzr
+ mov x2, xzr
+ mov x3, xzr
+ br x21
+
+efi_load_fail:
+ mov x0, EFI_LOAD_ERROR
+ ldp x29, x30, [sp], #32
+ ret
+
+ENDPROC(efi_stub_entry)
diff --git a/arch/arm64/efi/efi-stub.c b/arch/arm64/efi/efi-stub.c
new file mode 100644
index 000000000000..c5f18e7cc5e4
--- /dev/null
+++ b/arch/arm64/efi/efi-stub.c
@@ -0,0 +1,318 @@
+/*
+ * linux/arch/arm/boot/compressed/efi-stub.c
+ *
+ * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
+ *
+ * This file implements the EFI boot stub for the ARM kernel
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#include <linux/efi.h>
+#include <linux/libfdt.h>
+#include <asm/sections.h>
+
+/*
+ * temporary hack to provide cmdline when booting directly to kernel (no grub)
+ */
+#define CMDLINE_HACK
+
+/* Error code returned to ASM code instead of valid FDT address. */
+#define EFI_STUB_ERROR (~0UL)
+
+/* EFI function call wrappers. These are not required for
+ * ARM, but wrappers are required for X86 to convert between
+ * ABIs. These wrappers are provided to allow code sharing
+ * between X86 and ARM. Since these wrappers directly invoke the
+ * EFI function pointer, the function pointer type must be properly
+ * defined, which is not the case for X86 One advantage of this is
+ * it allows for type checking of arguments, which is not
+ * possible with the X86 wrappers.
+ */
+#define efi_call_phys0(f) f()
+#define efi_call_phys1(f, a1) f(a1)
+#define efi_call_phys2(f, a1, a2) f(a1, a2)
+#define efi_call_phys3(f, a1, a2, a3) f(a1, a2, a3)
+#define efi_call_phys4(f, a1, a2, a3, a4) f(a1, a2, a3, a4)
+#define efi_call_phys5(f, a1, a2, a3, a4, a5) f(a1, a2, a3, a4, a5)
+
+/*
+ * AArch64 requires the DTB to be 8-byte aligned in the first 512MiB from
+ * start of kernel and may not cross a 2MiB boundary. We set alignment to
+ * equal max size so we know it won't cross a 2MiB boudary.
+ */
+#define MAX_DTB_SIZE 0x40000
+#define DTB_ALIGN MAX_DTB_SIZE
+#define MAX_DTB_OFFSET 0x20000000
+
+#define pr_efi(msg) efi_printk(sys_table, "EFI stub: "msg)
+#define pr_efi_err(msg) efi_printk(sys_table, "EFI stub: ERROR: "msg)
+
+struct fdt_region {
+ u64 base;
+ u64 size;
+};
+
+/* Include shared EFI stub code */
+#include "../../../drivers/firmware/efi/efi-stub-helper.c"
+
+static unsigned long get_dram_base(efi_system_table_t *sys_table)
+{
+ efi_status_t status;
+ unsigned long map_size, desc_size;
+ unsigned long membase = ~0UL;
+ efi_memory_desc_t *memory_map;
+ int i;
+
+ status = efi_get_memory_map(sys_table, &memory_map, &map_size,
+ &desc_size, NULL, NULL);
+ if (status == EFI_SUCCESS) {
+ for (i = 0; i < (map_size / sizeof(efi_memory_desc_t)); i++) {
+ efi_memory_desc_t *desc;
+ unsigned long m = (unsigned long)memory_map;
+
+ desc = (efi_memory_desc_t *)(m + (i * desc_size));
+
+ if (desc->num_pages == 0)
+ break;
+
+ if (desc->type == EFI_CONVENTIONAL_MEMORY) {
+ unsigned long base = desc->phys_addr;
+
+ base &= ~((unsigned long)(TEXT_OFFSET - 1));
+
+ if (membase > base)
+ membase = base;
+ }
+ }
+ }
+ return membase;
+}
+
+unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
+ unsigned long *image_addr)
+{
+ efi_loaded_image_t *image;
+ efi_status_t status;
+ int err, image_allocated = 0;
+ unsigned long image_size, mem_size;
+ unsigned long dram_base;
+ /* addr/point and size pairs for memory management*/
+ u64 initrd_addr;
+ u64 initrd_size = 0;
+ u64 fdt_addr; /* Original DTB */
+ u64 fdt_size = 0;
+ unsigned long new_fdt_size;
+ char *cmdline_ptr;
+ int cmdline_size = 0;
+ unsigned long new_fdt_addr;
+ unsigned long map_size, desc_size;
+ unsigned long mmap_key;
+ efi_memory_desc_t *memory_map;
+ u32 desc_ver;
+ efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
+
+ /* Check if we were booted by the EFI firmware */
+ if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
+ goto fail;
+
+ pr_efi("Booting Linux Kernel...\n");
+
+ /* get the command line from EFI, using the LOADED_IMAGE protocol */
+ status = efi_call_phys3(sys_table->boottime->handle_protocol,
+ handle, &proto, (void *)&image);
+ if (status != EFI_SUCCESS) {
+ pr_efi("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
+ goto fail;
+ }
+
+ /*
+ * We are going to copy this into device tree, so we don't care where
+ * in memory it is.
+ */
+ cmdline_ptr = efi_convert_cmdline_to_ascii(sys_table, image,
+ &cmdline_size);
+ if (!cmdline_ptr) {
+ pr_efi_err("converting command line to ascii.\n");
+ goto fail;
+ }
+ if (*cmdline_ptr == 0) {
+#ifdef CMDLINE_HACK
+ int i;
+ char *p;
+ unsigned long new_addr;
+ unsigned long cmdline_len;
+
+ /* free the empty cmdline_ptr. */
+ efi_free(sys_table, cmdline_size, (u64)cmdline_ptr);
+ cmdline_len = 0;
+
+ /* get cmdline from file */
+ status = handle_cmdline_files(sys_table, image,
+ "cmd=cmdline\n", "cmd=", ~0UL,
+ (unsigned long *)&cmdline_ptr,
+ &cmdline_len);
+
+ cmdline_size = cmdline_len;
+ if (status != EFI_SUCCESS) {
+ pr_efi("CMDLINE_HACK Failed to read cmdline file\n");
+ goto fail;
+ }
+
+ for (i = 0, p = cmdline_ptr; i < cmdline_size; i++, p++)
+ if (*p == '\n' || *p == '\0')
+ break;
+ /* i is length of string not counting terminating NULL or \n */
+ status = efi_high_alloc(sys_table, i + 1, 0, &new_addr, ~0UL);
+ if (status != EFI_SUCCESS) {
+ pr_efi("CMDLINE_HACK alloc for new cmdline failed\n");
+ goto fail;
+ }
+ p = (char *)new_addr;
+ memcpy(p, cmdline_ptr, i);
+ p[i] = 0;
+
+ efi_free(sys_table, cmdline_size, (u64)cmdline_ptr);
+ cmdline_size = i + 1;
+ cmdline_ptr = p;
+#endif
+ }
+
+ status = handle_cmdline_files(sys_table, image, cmdline_ptr, "dtb=",
+ ~0UL, (unsigned long *)&fdt_addr,
+ (unsigned long *)&fdt_size);
+ if (status != EFI_SUCCESS)
+ fdt_addr = 0;
+
+ if (fdt_addr) {
+ err = fdt_check_header((void *)fdt_addr);
+ if (err != 0) {
+ pr_efi_err("Device Tree header not valid\n");
+ goto fail_free_dtb;
+ }
+ if (fdt_totalsize((void *)fdt_addr) > fdt_size) {
+ pr_efi_err("Incomplete device tree.\n");
+ goto fail_free_dtb;
+
+ }
+ }
+
+ dram_base = get_dram_base(sys_table);
+ if (dram_base == ~0UL) {
+ pr_efi("Failed to get DRAM base\n");
+ goto fail_free_dtb;
+ }
+
+ /* Relocate the image, if required. */
+ image_size = image->image_size;
+ if (*image_addr != (dram_base + TEXT_OFFSET)) {
+ mem_size = image_size + (_end - __bss_start);
+ status = efi_relocate_kernel(sys_table, image_addr, image_size,
+ mem_size, dram_base + TEXT_OFFSET,
+ PAGE_SIZE);
+ if (status != EFI_SUCCESS) {
+ pr_efi("Failed to relocate kernel\n");
+ goto fail_free_dtb;
+ }
+ image_allocated = 1;
+ if (*image_addr != (dram_base + TEXT_OFFSET)) {
+ pr_efi("Failed to alloc kernel memory\n");
+ goto fail_free_image;
+ }
+ }
+
+ status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+ dram_base + 0x20000000,
+ (unsigned long *)&initrd_addr,
+ (unsigned long *)&initrd_size);
+ if (status != EFI_SUCCESS) {
+ pr_efi("Error loading initrd\n");
+ goto fail_free_image;
+ }
+
+ /* Estimate size of new FDT, and allocate memory for it. We
+ * will allocate a bigger buffer if this ends up being too
+ * small, so a rough guess is OK here.*/
+ new_fdt_size = fdt_size + EFI_PAGE_SIZE;
+ while (1) {
+ status = efi_high_alloc(sys_table, new_fdt_size, DTB_ALIGN,
+ &new_fdt_addr,
+ dram_base + MAX_DTB_OFFSET);
+ if (status != EFI_SUCCESS) {
+ pr_efi_err("No memory for new devive tree.\n");
+ goto fail_free_initrd;
+ }
+
+ /* Now that we have done our final memory allocation (and free)
+ * we can get the memory map key needed for
+ * exit_boot_services().
+ */
+ status = efi_get_memory_map(sys_table, &memory_map, &map_size,
+ &desc_size, &desc_ver, &mmap_key);
+ if (status != EFI_SUCCESS)
+ goto fail_free_new_fdt;
+
+ status = update_fdt(sys_table,
+ (void *)fdt_addr, (void *)new_fdt_addr,
+ new_fdt_size, cmdline_ptr,
+ initrd_addr, initrd_size,
+ memory_map, map_size, desc_size, desc_ver);
+
+ /* Succeeding the first time is the expected case. */
+ if (status == EFI_SUCCESS)
+ break;
+
+ if (status == EFI_BUFFER_TOO_SMALL) {
+ /* We need to allocate more space for the new
+ * device tree, so free existing buffer that is
+ * too small. Also free memory map, as we will need
+ * to get new one that reflects the free/alloc we do
+ * on the device tree buffer. */
+ efi_free(sys_table, new_fdt_size, new_fdt_addr);
+ efi_call_phys1(sys_table->boottime->free_pool,
+ memory_map);
+ new_fdt_size += EFI_PAGE_SIZE;
+ } else {
+ pr_efi_err("Unable to constuct new device tree.\n");
+ goto fail_free_mmap;
+ }
+ }
+
+ /* Now we are ready to exit_boot_services.*/
+ status = efi_call_phys2(sys_table->boottime->exit_boot_services,
+ handle, mmap_key);
+
+ if (status != EFI_SUCCESS) {
+ pr_efi_err("Exit boot services failed.\n");
+ goto fail_free_mmap;
+ }
+
+ /* Now we need to return the FDT address to the calling
+ * assembly to this can be used as part of normal boot.
+ */
+ return new_fdt_addr;
+
+fail_free_mmap:
+ efi_call_phys1(sys_table->boottime->free_pool, memory_map);
+
+fail_free_new_fdt:
+ efi_free(sys_table, new_fdt_size, new_fdt_addr);
+
+fail_free_initrd:
+ efi_free(sys_table, initrd_size, initrd_addr);
+
+fail_free_image:
+ if (image_allocated)
+ efi_free(sys_table, mem_size, *image_addr);
+
+fail_free_dtb:
+ if (fdt_addr)
+ efi_free(sys_table, fdt_size, fdt_addr);
+
+ efi_free(sys_table, cmdline_size, (u64)cmdline_ptr);
+
+fail:
+ return EFI_STUB_ERROR;
+}
diff --git a/arch/arm64/efi/efi.c b/arch/arm64/efi/efi.c
new file mode 100644
index 000000000000..5793b15c1663
--- /dev/null
+++ b/arch/arm64/efi/efi.c
@@ -0,0 +1,529 @@
+/*
+ * Extensible Firmware Interface
+ *
+ * Based on Extensible Firmware Interface Specification version 2.3.1
+ *
+ * Copyright (C) 2013 Linaro Ltd.
+ *
+ */
+
+#include <linux/efi.h>
+#include <linux/export.h>
+#include <linux/memblock.h>
+#include <linux/of.h>
+#include <linux/of_fdt.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+
+#include <asm/cacheflush.h>
+#include <asm/efi.h>
+#include <asm/tlbflush.h>
+#include <asm/mmu_context.h>
+
+/* shouldn't need this, but some firmware images are broken... */
+#define KEEP_BOOT_SERVICES_REGIONS
+
+#define efi_early_remap(a, b) \
+ ((__force void *)early_ioremap((a), (b)))
+#define efi_early_unmap(a, b) \
+ early_iounmap((void __iomem *)(a), (b))
+
+struct efi_memory_map memmap;
+
+static efi_runtime_services_t *runtime;
+
+static u64 efi_system_table;
+
+static unsigned long arm_efi_facility;
+
+/* Default memory map descriptor information */
+#define DESC_SIZE 48
+#define DESC_VER 1
+
+/*
+ * Returns 1 if 'facility' is enabled, 0 otherwise.
+ */
+int efi_enabled(int facility)
+{
+ return test_bit(facility, &arm_efi_facility) != 0;
+}
+EXPORT_SYMBOL(efi_enabled);
+
+static int uefi_debug __initdata;
+static int __init uefi_debug_setup(char *str)
+{
+ uefi_debug = 1;
+
+ return 0;
+}
+early_param("uefi_debug", uefi_debug_setup);
+
+static int __init fdt_find_efi_params(unsigned long node, const char *uname,
+ int depth, void *data)
+{
+ unsigned long len;
+ __be32 *prop;
+
+ if (depth != 1 ||
+ (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
+ return 0;
+
+ pr_info("Getting EFI parameters from FDT.\n");
+
+ prop = of_get_flat_dt_prop(node, "linux,efi-system-table", &len);
+ if (!prop) {
+ pr_info("No EFI system table in FDT\n");
+ return 0;
+ }
+ efi_system_table = of_read_ulong(prop, len/4);
+
+ prop = of_get_flat_dt_prop(node, "linux,efi-mmap", &len);
+ if (!prop || !len) {
+ pr_info("No EFI memmap in FDT\n");
+ return 0;
+ }
+ memmap.map = (void *)prop;
+ memmap.map_end = memmap.map + len;
+ memmap.phys_map = (void *)virt_to_phys(memmap.map);
+
+ prop = of_get_flat_dt_prop(node, "linux,efi-mmap-desc-size", &len);
+ if (prop)
+ memmap.desc_size = of_read_ulong(prop, len/4);
+ else
+ memmap.desc_size = DESC_SIZE;
+
+ prop = of_get_flat_dt_prop(node, "linux,efi-mmap-desc-ver", &len);
+ if (prop)
+ memmap.desc_version = of_read_ulong(prop, len/4);
+ else
+ memmap.desc_version = DESC_VER;
+
+ if (uefi_debug) {
+ pr_info(" EFI system table @ %p\n", (void *)efi_system_table);
+ pr_info(" EFI mmap @ %p-%p\n", memmap.phys_map,
+ memmap.map_end);
+ pr_info(" EFI mmap descriptor size = 0x%lx\n",
+ memmap.desc_size);
+ pr_info(" EFI mmap descriptor version = 0x%lx\n",
+ memmap.desc_version);
+ }
+
+ return 1;
+}
+
+static void * __init __efi_alloc(u64 size)
+{
+ phys_addr_t p;
+ void *v;
+
+ size = PAGE_ALIGN(size);
+ p = memblock_alloc(size, PAGE_SIZE);
+ if (!p)
+ return NULL;
+ v = phys_to_virt(p);
+ memset(v, 0, size);
+ return v;
+}
+
+#define PGD_END (&swapper_pg_dir[sizeof(idmap_pg_dir)/sizeof(pgd_t)])
+#ifndef CONFIG_SMP
+#define PTE_FLAGS (PTE_TYPE_PAGE | PTE_AF)
+#define PMD_FLAGS (PMD_TYPE_SECT | PMD_SECT_AF)
+#else
+#define PTE_FLAGS (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)
+#define PMD_FLAGS (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
+#endif
+
+#ifdef CONFIG_ARM64_64K_PAGES
+#define MM_MMUFLAGS (PTE_ATTRINDX(MT_NORMAL) | PTE_FLAGS)
+#else
+#define MM_MMUFLAGS (PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS)
+#endif
+
+#ifdef CONFIG_ARM64_64K_PAGES
+static void __init __memory_idmap(unsigned long addr, unsigned long len)
+{
+ unsigned long end, next, p;
+ pgd_t *pgd;
+ pud_t *pud;
+ pmd_t *pmd;
+ pte_t *pte;
+
+ /* page align it */
+ len = PAGE_ALIGN(len + (addr & ~PAGE_MASK));
+ addr &= PAGE_MASK;
+
+ end = addr + len;
+ pgd = &idmap_pg_dir[pgd_index(addr)];
+
+ do {
+ next = pgd_addr_end(addr, end);
+ if (pgd >= PGD_END)
+ continue;
+
+ pud = pud_offset(pgd, addr);
+ pmd = pmd_offset(pud, addr);
+
+ if (pmd_none(*pmd)) {
+ pte = __efi_alloc(PAGE_SIZE);
+ if (!pte)
+ continue;
+ set_pmd(pmd, __pmd(__pa(pte) | PMD_TYPE_TABLE));
+ }
+
+ for (p = addr; p < next; p += PAGE_SIZE) {
+ pte = pte_offset_kernel(pmd, p);
+ if (pte_none(*pte))
+ set_pte(pte, __pte(p | MM_MMUFLAGS));
+ }
+ } while (pgd++, addr = next, addr != end);
+}
+#else
+static void __init __memory_idmap(unsigned long addr, unsigned long len)
+{
+ unsigned long end, next, p;
+ pgd_t *pgd;
+ pud_t *pud;
+ pmd_t *pmd;
+
+ /* section align it */
+ len = ALIGN(len + (addr & ~SECTION_MASK), SECTION_SIZE);
+ addr &= SECTION_MASK;
+
+ end = addr + len;
+ pgd = &idmap_pg_dir[pgd_index(addr)];
+
+ do {
+ next = pgd_addr_end(addr, end);
+ if (pgd >= PGD_END)
+ continue;
+
+ pud = pud_offset(pgd, addr);
+ if (pud_none(*pud)) {
+ pmd = __efi_alloc(PAGE_SIZE);
+ if (!pmd)
+ continue;
+ set_pud(pud, __pud(__pa(pmd) | PMD_TYPE_TABLE));
+ }
+
+ for (p = addr; p < next; p += SECTION_SIZE) {
+ pmd = pmd_offset(pud, p);
+
+ if (pmd_none(*pmd))
+ set_pmd(pmd, __pmd(p | MM_MMUFLAGS));
+ }
+ } while (pgd++, addr = next, addr != end);
+}
+#endif
+
+static void __init efi_setup_idmap(void)
+{
+ struct memblock_region *r;
+ efi_memory_desc_t *md;
+ unsigned long next, end;
+
+ for_each_memblock(memory, r)
+ __memory_idmap(r->base, r->size);
+
+ next = (unsigned long)memmap.map;
+ end = (unsigned long)memmap.map_end;
+
+ for (; next < end; next += memmap.desc_size) {
+ md = (efi_memory_desc_t *)next;
+
+ if (md->num_pages == 0)
+ break;
+
+ if (md->type == EFI_MEMORY_MAPPED_IO)
+ continue;
+
+ __memory_idmap(md->phys_addr, md->num_pages << EFI_PAGE_SHIFT);
+ }
+}
+
+static int __init uefi_init(void)
+{
+ efi_char16_t *c16;
+ char vendor[100] = "unknown";
+ int i, retval;
+
+ efi.systab = efi_early_remap(efi_system_table,
+ sizeof(efi_system_table_t));
+
+ /*
+ * Verify the EFI Table
+ */
+ if (efi.systab == NULL)
+ panic("Whoa! Can't find EFI system table.\n");
+ if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
+ panic("Whoa! EFI system table signature incorrect\n");
+ if ((efi.systab->hdr.revision >> 16) == 0)
+ pr_warn("Warning: EFI system table version %d.%02d, expected 1.00 or greater\n",
+ efi.systab->hdr.revision >> 16,
+ efi.systab->hdr.revision & 0xffff);
+
+ /* Show what we know for posterity */
+ c16 = efi_early_remap(efi.systab->fw_vendor,
+ sizeof(vendor));
+ if (c16) {
+ for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
+ vendor[i] = c16[i];
+ vendor[i] = '\0';
+ }
+
+ pr_info("EFI v%u.%.02u by %s\n",
+ efi.systab->hdr.revision >> 16,
+ efi.systab->hdr.revision & 0xffff, vendor);
+
+ retval = efi_config_init(NULL);
+ if (retval == 0)
+ set_bit(EFI_CONFIG_TABLES, &arm_efi_facility);
+
+ efi_early_unmap(c16, sizeof(vendor));
+ efi_early_unmap(efi.systab, sizeof(efi_system_table_t));
+
+ return retval;
+}
+
+static __init int is_discardable_region(efi_memory_desc_t *md)
+{
+ if (md->attribute & EFI_MEMORY_RUNTIME)
+ return 0;
+
+ switch (md->type) {
+#ifdef KEEP_BOOT_SERVICES_REGIONS
+ case EFI_BOOT_SERVICES_CODE:
+ case EFI_BOOT_SERVICES_DATA:
+#endif
+ /* Keep tables around for any future kexec operations */
+ case EFI_ACPI_RECLAIM_MEMORY:
+ return 0;
+ }
+
+ return 1;
+}
+
+static __initdata struct {
+ u32 type;
+ const char *name;
+} memory_type_name_map[] = {
+ {EFI_RESERVED_TYPE, "Reserved"},
+ {EFI_LOADER_CODE, "Loader Code"},
+ {EFI_LOADER_DATA, "Loader Data"},
+ {EFI_BOOT_SERVICES_CODE, "Boot Services Code"},
+ {EFI_BOOT_SERVICES_DATA, "Boot Services Data"},
+ {EFI_RUNTIME_SERVICES_CODE, "Runtime Services Code"},
+ {EFI_RUNTIME_SERVICES_DATA, "Runtime Services Data"},
+ {EFI_CONVENTIONAL_MEMORY, "Conventional Memory"},
+ {EFI_UNUSABLE_MEMORY, "Unusable Memory"},
+ {EFI_ACPI_RECLAIM_MEMORY, "ACPI Reclaim Memory"},
+ {EFI_ACPI_MEMORY_NVS, "ACPI Memory NVS"},
+ {EFI_MEMORY_MAPPED_IO, "Memory Mapped I/O"},
+ {EFI_MEMORY_MAPPED_IO_PORT_SPACE, "Memory Mapped I/O Port Space"},
+ {EFI_PAL_CODE, "EFI PAL Code"},
+ {EFI_MAX_MEMORY_TYPE, NULL},
+};
+
+static __init int reserve_regions(void)
+{
+ void *map;
+ efi_memory_desc_t md;
+ unsigned long mapsize;
+ int num;
+ u64 paddr;
+ u64 npages;
+
+ memmap.nr_map = 0;
+
+ if (uefi_debug)
+ pr_info("Processing EFI memory map:\n");
+
+ mapsize = memmap.map_end - memmap.map;
+
+ for (map = memmap.map, num = mapsize/memmap.desc_size;
+ num-- > 0;
+ map += memmap.desc_size) {
+
+ memcpy(&md, map, sizeof(md));
+
+ /* Some UEFI firmware images terminate with a NULL entry */
+ if (md.num_pages == 0)
+ break;
+
+ if (is_discardable_region(&md))
+ continue;
+
+ if (md.type != EFI_MEMORY_MAPPED_IO) {
+ paddr = md.phys_addr;
+ npages = md.num_pages;
+ memrange_efi_to_native(&paddr, &npages);
+ memblock_reserve(paddr, npages << PAGE_SHIFT);
+ if (uefi_debug)
+ pr_info(" 0x%012llx-0x%012llx (%s)\n",
+ paddr, npages << PAGE_SHIFT,
+ memory_type_name_map[md.type].name);
+ }
+ memmap.nr_map++;
+ }
+
+ if (uefi_debug)
+ pr_info("%d EFI regions reserved.\n", memmap.nr_map);
+
+ return 0;
+}
+
+void __init efi_init(void)
+{
+ /* Grab system table location out of FDT */
+ if (!of_scan_flat_dt(fdt_find_efi_params, NULL))
+ return;
+
+ set_bit(EFI_BOOT, &arm_efi_facility);
+ set_bit(EFI_64BIT, &arm_efi_facility);
+
+ uefi_init();
+
+ reserve_regions();
+}
+
+static int __init remap_region(efi_memory_desc_t *md, efi_memory_desc_t *entry)
+{
+ u64 va;
+ u64 paddr;
+ u64 npages;
+ u64 size;
+
+ *entry = *md;
+ paddr = entry->phys_addr;
+ npages = entry->num_pages;
+
+ memrange_efi_to_native(&paddr, &npages);
+
+ size = npages << PAGE_SHIFT;
+
+ /*
+ * Map everything writeback-capable as coherent memory,
+ * anything else as device.
+ */
+ if (md->attribute & EFI_MEMORY_WB) {
+ if (memblock_is_memory(paddr))
+ va = (u64)phys_to_virt(paddr);
+ else
+ va = (__force u64)efi_remap(paddr, size);
+ } else
+ va = (__force u64)efi_ioremap(paddr, size);
+ if (!va)
+ return 0;
+ entry->virt_addr = va;
+
+ if (uefi_debug)
+ pr_info(" %p-%p => %p : (%s)\n",
+ (void *)paddr, (void *)paddr + size - 1, (void *)va,
+ md->attribute & EFI_MEMORY_WB ? "WB" : "I/O");
+ return 1;
+}
+
+static int __init remap_regions(void)
+{
+ void *map, *next;
+ efi_memory_desc_t md;
+ unsigned long mapsize;
+ int num;
+ u64 addr;
+
+ mapsize = memmap.map_end - memmap.map;
+ map = memmap.map;
+
+ /* Allocate space for the physical region map */
+ memmap.map = __efi_alloc(memmap.nr_map * memmap.desc_size);
+ if (!memmap.map)
+ return 0;
+
+ memmap.phys_map = (void *)virt_to_phys(memmap.map);
+
+ next = memmap.map;
+ for (num = mapsize/memmap.desc_size;
+ num-- > 0;
+ map += memmap.desc_size) {
+
+ memcpy(&md, map, sizeof(md));
+
+ if (is_discardable_region(&md))
+ continue;
+
+ if (!remap_region(&md, next))
+ return 0;
+
+ next += memmap.desc_size;
+ }
+
+ memmap.map_end = next;
+ efi.memmap = &memmap;
+
+ efi.systab = (__force void *)efi_lookup_mapped_addr(efi_system_table);
+ if (efi.systab)
+ set_bit(EFI_SYSTEM_TABLES, &arm_efi_facility);
+
+ /*
+ * efi.systab->runtime is a pointer to something guaranteed by
+ * the UEFI specification to be 1:1 mapped in a 4GB address space.
+ */
+ addr = (u64)efi.systab->runtime;
+ runtime = (__force void *)efi_lookup_mapped_addr(addr);
+
+ return 1;
+}
+
+
+/*
+ * Called from setup_arch with interrupts disabled.
+ */
+void __init efi_enter_virtual_mode(void)
+{
+ efi_status_t status;
+
+ if (!efi_enabled(EFI_BOOT)) {
+ pr_info("EFI services will not be available.\n");
+ return;
+ }
+ pr_info("Remapping and enabling EFI services.\n");
+
+ /* Map the regions we reserved earlier */
+ if (!remap_regions()) {
+ pr_info("Failed to remap EFI regions - runtime services will not be available.\n");
+ return;
+ }
+
+ /* Call SetVirtualAddressMap with the physical address of the map */
+ efi.set_virtual_address_map = runtime->set_virtual_address_map;
+
+ /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
+ efi_setup_idmap();
+
+ cpu_switch_mm(idmap_pg_dir, &init_mm);
+ flush_tlb_all();
+ flush_cache_all();
+
+ status = efi.set_virtual_address_map(memmap.nr_map * memmap.desc_size,
+ memmap.desc_size,
+ memmap.desc_version,
+ memmap.phys_map);
+ cpu_set_reserved_ttbr0();
+ flush_tlb_all();
+ flush_cache_all();
+
+ if (status != EFI_SUCCESS) {
+ pr_info("Failed to set EFI virtual address map! [%lx]\n",
+ status);
+ return;
+ }
+
+ pr_info("EFI Virtual address map set\n");
+
+ /* Set up function pointers for efivars */
+ efi.get_variable = (efi_get_variable_t *)runtime->get_variable;
+ efi.get_next_variable =
+ (efi_get_next_variable_t *)runtime->get_next_variable;
+ efi.set_variable = (efi_set_variable_t *)runtime->set_variable;
+ set_bit(EFI_RUNTIME_SERVICES, &arm_efi_facility);
+}
diff --git a/arch/arm64/efi/libfdt_env.h b/arch/arm64/efi/libfdt_env.h
new file mode 100644
index 000000000000..f10bd09b731f
--- /dev/null
+++ b/arch/arm64/efi/libfdt_env.h
@@ -0,0 +1,15 @@
+#ifndef _ARM64_LIBFDT_ENV_H
+#define _ARM64_LIBFDT_ENV_H
+
+#include <linux/types.h>
+#include <linux/string.h>
+#include <asm/byteorder.h>
+
+#define fdt16_to_cpu(x) be16_to_cpu(x)
+#define cpu_to_fdt16(x) cpu_to_be16(x)
+#define fdt32_to_cpu(x) be32_to_cpu(x)
+#define cpu_to_fdt32(x) cpu_to_be32(x)
+#define fdt64_to_cpu(x) be64_to_cpu(x)
+#define cpu_to_fdt64(x) cpu_to_be64(x)
+
+#endif
diff --git a/arch/arm64/include/asm/efi.h b/arch/arm64/include/asm/efi.h
new file mode 100644
index 000000000000..7384048fc3b3
--- /dev/null
+++ b/arch/arm64/include/asm/efi.h
@@ -0,0 +1,18 @@
+#ifndef _ASM_ARM64_EFI_H
+#define _ASM_ARM64_EFI_H
+
+#include <asm/io.h>
+
+#ifdef CONFIG_EFI
+extern void efi_init(void);
+#else
+#define efi_init()
+#endif
+
+#define efi_remap(cookie, size) __ioremap((cookie), (size), PAGE_KERNEL_EXEC)
+#define efi_ioremap(cookie, size) __ioremap((cookie), (size), \
+ __pgprot(PROT_DEVICE_nGnRE))
+#define efi_unmap(cookie) __iounmap((cookie))
+#define efi_iounmap(cookie) __iounmap((cookie))
+
+#endif /* _ASM_ARM64_EFI_H */
diff --git a/arch/arm64/include/asm/fixmap.h b/arch/arm64/include/asm/fixmap.h
new file mode 100644
index 000000000000..e73b219c21f4
--- /dev/null
+++ b/arch/arm64/include/asm/fixmap.h
@@ -0,0 +1,117 @@
+/*
+ * fixmap.h: compile-time virtual memory allocation
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ * Copyright (C) 1998 Ingo Molnar
+ *
+ */
+
+#ifndef _ASM_ARM64_FIXMAP_H
+#define _ASM_ARM64_FIXMAP_H
+
+#ifndef __ASSEMBLY__
+#include <linux/kernel.h>
+#include <asm/page.h>
+
+/*
+ * Here we define all the compile-time 'special' virtual
+ * addresses. The point is to have a constant address at
+ * compile time, but to set the physical address only
+ * in the boot process.
+ *
+ * These 'compile-time allocated' memory buffers are
+ * page-sized. Use set_fixmap(idx,phys) to associate
+ * physical memory with fixmap indices.
+ *
+ */
+enum fixed_addresses {
+ FIX_EARLYCON,
+ __end_of_permanent_fixed_addresses,
+
+ /*
+ * Temporary boot-time mappings, used by early_ioremap(),
+ * before ioremap() is functional.
+ */
+#ifdef CONFIG_ARM64_64K_PAGES
+#define NR_FIX_BTMAPS 4
+#else
+#define NR_FIX_BTMAPS 64
+#endif
+#define FIX_BTMAPS_SLOTS 7
+#define TOTAL_FIX_BTMAPS (NR_FIX_BTMAPS * FIX_BTMAPS_SLOTS)
+ FIX_BTMAP_END = __end_of_permanent_fixed_addresses,
+ FIX_BTMAP_BEGIN = FIX_BTMAP_END + TOTAL_FIX_BTMAPS - 1,
+ __end_of_fixed_addresses
+};
+
+#define FIXADDR_SIZE (__end_of_permanent_fixed_addresses << PAGE_SHIFT)
+#define FIXADDR_BOOT_SIZE (__end_of_fixed_addresses << PAGE_SHIFT)
+#define FIXADDR_START (FIXADDR_TOP - FIXADDR_SIZE)
+#define FIXADDR_BOOT_START (FIXADDR_TOP - FIXADDR_BOOT_SIZE)
+
+extern void __set_fixmap(enum fixed_addresses idx,
+ phys_addr_t phys, pgprot_t flags);
+
+#define set_fixmap(idx, phys) \
+ __set_fixmap(idx, phys, PAGE_KERNEL)
+
+#define set_fixmap_io(idx, phys) \
+ __set_fixmap(idx, phys, __pgprot(PROT_DEVICE_nGnRE))
+
+#define clear_fixmap(idx) \
+ __set_fixmap(idx, 0, __pgprot(0))
+
+#define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT))
+#define __virt_to_fix(x) ((FIXADDR_TOP - ((x)&PAGE_MASK)) >> PAGE_SHIFT)
+
+extern void __this_fixmap_does_not_exist(void);
+
+/*
+ * 'index to address' translation. If anyone tries to use the idx
+ * directly without translation, we catch the bug with a NULL-deference
+ * kernel oops. Illegal ranges of incoming indices are caught too.
+ */
+static __always_inline unsigned long fix_to_virt(const unsigned int idx)
+{
+ /*
+ * this branch gets completely eliminated after inlining,
+ * except when someone tries to use fixaddr indices in an
+ * illegal way. (such as mixing up address types or using
+ * out-of-range indices).
+ *
+ * If it doesn't get removed, the linker will complain
+ * loudly with a reasonably clear error message..
+ */
+ if (idx >= __end_of_fixed_addresses)
+ __this_fixmap_does_not_exist();
+
+ return __fix_to_virt(idx);
+}
+
+static inline unsigned long virt_to_fix(const unsigned long vaddr)
+{
+ BUG_ON(vaddr >= FIXADDR_TOP || vaddr < FIXADDR_START);
+ return __virt_to_fix(vaddr);
+}
+
+/* Return an pointer with offset calculated */
+static __always_inline unsigned long
+__set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
+{
+ __set_fixmap(idx, phys, flags);
+ return fix_to_virt(idx) + (phys & (PAGE_SIZE - 1));
+}
+
+#define set_fixmap_offset(idx, phys) \
+ __set_fixmap_offset(idx, phys, PAGE_KERNEL)
+
+#define set_fixmap_offset_nocache(idx, phys) \
+ __set_fixmap_offset(idx, phys, PAGE_KERNEL_NOCACHE)
+
+extern void early_ioremap_init(void);
+
+#endif /* !__ASSEMBLY__ */
+#endif /* _ASM_ARM64_FIXMAP_H */
diff --git a/arch/arm64/include/asm/io.h b/arch/arm64/include/asm/io.h
index 1d12f89140ba..a0bd6a4d2b74 100644
--- a/arch/arm64/include/asm/io.h
+++ b/arch/arm64/include/asm/io.h
@@ -224,6 +224,13 @@ extern void __memset_io(volatile void __iomem *, int, size_t);
*/
extern void __iomem *__ioremap(phys_addr_t phys_addr, size_t size, pgprot_t prot);
extern void __iounmap(volatile void __iomem *addr);
+extern void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size);
+extern void __iomem *early_ioremap(resource_size_t phys_addr,
+ unsigned long size);
+extern void __iomem *early_memremap(resource_size_t phys_addr,
+ unsigned long size);
+extern void early_iounmap(void __iomem *addr, unsigned long size);
+extern void __init early_ioremap_reset(void);
#define PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_DIRTY)
#define PROT_DEVICE_nGnRE (PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_ATTRINDX(MT_DEVICE_nGnRE))
@@ -233,7 +240,6 @@ extern void __iounmap(volatile void __iomem *addr);
#define ioremap(addr, size) __ioremap((addr), (size), __pgprot(PROT_DEVICE_nGnRE))
#define ioremap_nocache(addr, size) __ioremap((addr), (size), __pgprot(PROT_DEVICE_nGnRE))
#define ioremap_wc(addr, size) __ioremap((addr), (size), __pgprot(PROT_NORMAL_NC))
-#define ioremap_cached(addr, size) __ioremap((addr), (size), __pgprot(PROT_NORMAL))
#define iounmap __iounmap
#define PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF)
diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index 20925bcf4e2a..58e68c78cbf3 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -43,7 +43,7 @@
#define PAGE_OFFSET UL(0xffffffc000000000)
#define MODULES_END (PAGE_OFFSET)
#define MODULES_VADDR (MODULES_END - SZ_64M)
-#define EARLYCON_IOBASE (MODULES_VADDR - SZ_4M)
+#define FIXADDR_TOP (MODULES_VADDR - SZ_2M - SZ_64K)
#define VA_BITS (39)
#define TASK_SIZE_64 (UL(1) << VA_BITS)
diff --git a/arch/arm64/kernel/early_printk.c b/arch/arm64/kernel/early_printk.c
index fbb6e1843659..850d9a4470c6 100644
--- a/arch/arm64/kernel/early_printk.c
+++ b/arch/arm64/kernel/early_printk.c
@@ -26,6 +26,8 @@
#include <linux/amba/serial.h>
#include <linux/serial_reg.h>
+#include <asm/fixmap.h>
+
static void __iomem *early_base;
static void (*printch)(char ch);
@@ -141,8 +143,10 @@ static int __init setup_early_printk(char *buf)
}
/* no options parsing yet */
- if (paddr)
- early_base = early_io_map(paddr, EARLYCON_IOBASE);
+ if (paddr) {
+ set_fixmap_io(FIX_EARLYCON, paddr);
+ early_base = (void __iomem *)fix_to_virt(FIX_EARLYCON);
+ }
printch = match->printch;
early_console = &early_console_dev;
diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index 7090c126797c..cd961927461c 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -107,8 +107,18 @@
/*
* DO NOT MODIFY. Image header expected by Linux boot-loaders.
*/
+#ifdef CONFIG_EFI_STUB
+ /*
+ * Magic "MZ" signature for PE/COFF
+ * Little Endian: add x13, x18, #0x16
+ */
+efi_head:
+ .long 0x91005a4d
+ b stext
+#else
b stext // branch to kernel start, magic
.long 0 // reserved
+#endif
.quad TEXT_OFFSET // Image load offset from start of RAM
.quad 0 // reserved
.quad 0 // reserved
@@ -119,7 +129,102 @@
.byte 0x52
.byte 0x4d
.byte 0x64
+#ifdef CONFIG_EFI_STUB
+ .long pe_header - efi_head // Offset to the PE header.
+#else
.word 0 // reserved
+#endif
+
+#ifdef CONFIG_EFI_STUB
+ .align 3
+pe_header:
+ .ascii "PE"
+ .short 0
+coff_header:
+ .short 0xaa64 // AArch64
+ .short 2 // nr_sections
+ .long 0 // TimeDateStamp
+ .long 0 // PointerToSymbolTable
+ .long 1 // NumberOfSymbols
+ .short section_table - optional_header // SizeOfOptionalHeader
+ .short 0x206 // Characteristics.
+ // IMAGE_FILE_DEBUG_STRIPPED |
+ // IMAGE_FILE_EXECUTABLE_IMAGE |
+ // IMAGE_FILE_LINE_NUMS_STRIPPED
+optional_header:
+ .short 0x20b // PE32+ format
+ .byte 0x02 // MajorLinkerVersion
+ .byte 0x14 // MinorLinkerVersion
+ .long __bss_start - stext // SizeOfCode
+ .long 0 // SizeOfInitializedData
+ .long 0 // SizeOfUninitializedData
+ .long efi_stub_entry - efi_head // AddressOfEntryPoint
+ .long stext - efi_head // BaseOfCode
+
+extra_header_fields:
+ .quad 0 // ImageBase
+ .long 0x20 // SectionAlignment
+ .long 0x20 // FileAlignment
+ .short 0 // MajorOperatingSystemVersion
+ .short 0 // MinorOperatingSystemVersion
+ .short 0 // MajorImageVersion
+ .short 0 // MinorImageVersion
+ .short 0 // MajorSubsystemVersion
+ .short 0 // MinorSubsystemVersion
+ .long 0 // Win32VersionValue
+
+ .long __bss_start - efi_head // SizeOfImage
+
+ // Everything before the kernel image is considered part of the header
+ .long stext - efi_head // SizeOfHeaders
+ .long 0 // CheckSum
+ .short 0xa // Subsystem (EFI application)
+ .short 0 // DllCharacteristics
+ .quad 0 // SizeOfStackReserve
+ .quad 0 // SizeOfStackCommit
+ .quad 0 // SizeOfHeapReserve
+ .quad 0 // SizeOfHeapCommit
+ .long 0 // LoaderFlags
+ .long 0x0 // NumberOfRvaAndSizes
+
+ // Section table
+section_table:
+
+ /*
+ * The EFI application loader requires a relocation section
+ * because EFI applications must be relocatable. This is a
+ * dummy section as far as we are concerned.
+ */
+ .ascii ".reloc"
+ .byte 0
+ .byte 0 // end of 0 padding of section name
+ .long 0
+ .long 0
+ .long 0 // SizeOfRawData
+ .long 0 // PointerToRawData
+ .long 0 // PointerToRelocations
+ .long 0 // PointerToLineNumbers
+ .short 0 // NumberOfRelocations
+ .short 0 // NumberOfLineNumbers
+ .long 0x42100040 // Characteristics (section flags)
+
+
+ .ascii ".text"
+ .byte 0
+ .byte 0
+ .byte 0 // end of 0 padding of section name
+ .long __bss_start - stext // VirtualSize
+ .long stext - efi_head // VirtualAddress
+ .long __bss_start - stext // SizeOfRawData
+ .long stext - efi_head // PointerToRawData
+
+ .long 0 // PointerToRelocations (0 for executables)
+ .long 0 // PointerToLineNumbers (0 for executables)
+ .short 0 // NumberOfRelocations (0 for executables)
+ .short 0 // NumberOfLineNumbers (0 for executables)
+ .long 0xe0500020 // Characteristics (section flags)
+ .align 5
+#endif
ENTRY(stext)
mov x21, x0 // x21=FDT
@@ -376,7 +481,7 @@ ENDPROC(__calc_phys_offset)
* - identity mapping to enable the MMU (low address, TTBR0)
* - first few MB of the kernel linear mapping to jump to once the MMU has
* been enabled, including the FDT blob (TTBR1)
- * - UART mapping if CONFIG_EARLY_PRINTK is enabled (TTBR1)
+ * - pgd entry for fixed mappings
*/
__create_page_tables:
pgtbl x25, x26, x24 // idmap_pg_dir and swapper_pg_dir addresses
@@ -429,15 +534,12 @@ __create_page_tables:
sub x6, x6, #1 // inclusive range
create_block_map x0, x7, x3, x5, x6
1:
-#ifdef CONFIG_EARLY_PRINTK
/*
- * Create the pgd entry for the UART mapping. The full mapping is done
- * later based earlyprintk kernel parameter.
+ * Create the pgd entry for the fixed mappings.
*/
- ldr x5, =EARLYCON_IOBASE // UART virtual address
+ ldr x5, =FIXADDR_TOP // Fixed mapping virtual address
add x0, x26, #2 * PAGE_SIZE // section table address
create_pgd_entry x26, x0, x5, x6, x7
-#endif
ret
ENDPROC(__create_page_tables)
.ltorg
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index 055cfb80e05c..fb8d90f2f5ba 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -41,7 +41,9 @@
#include <linux/memblock.h>
#include <linux/of_fdt.h>
#include <linux/of_platform.h>
+#include <linux/efi.h>
+#include <asm/fixmap.h>
#include <asm/cputype.h>
#include <asm/elf.h>
#include <asm/cputable.h>
@@ -53,6 +55,7 @@
#include <asm/traps.h>
#include <asm/memblock.h>
#include <asm/psci.h>
+#include <asm/efi.h>
unsigned int processor_id;
EXPORT_SYMBOL(processor_id);
@@ -252,13 +255,18 @@ void __init setup_arch(char **cmdline_p)
*cmdline_p = boot_command_line;
+ early_ioremap_init();
parse_early_param();
arm64_memblock_init();
+ efi_init();
paging_init();
request_standard_resources();
+ if (efi_enabled(EFI_BOOT))
+ efi_enter_virtual_mode();
+
unflatten_device_tree();
psci_init();
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index f8ab9d8e2ea3..4ff9dcfe0d90 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -97,30 +97,13 @@ SECTIONS
PERCPU_SECTION(64)
__init_end = .;
- . = ALIGN(THREAD_SIZE);
- __data_loc = .;
-
- .data : AT(__data_loc) {
- _data = .; /* address in memory */
- _sdata = .;
-
- /*
- * first, the init task union, aligned
- * to an 8192 byte boundary.
- */
- INIT_TASK_DATA(THREAD_SIZE)
- NOSAVE_DATA
- CACHELINE_ALIGNED_DATA(64)
- READ_MOSTLY_DATA(64)
-
- /*
- * and the usual data section
- */
- DATA_DATA
- CONSTRUCTORS
-
- _edata = .;
- }
+
+ . = ALIGN(PAGE_SIZE);
+ _data = .;
+ __data_loc = _data - LOAD_OFFSET;
+ _sdata = .;
+ RW_DATA_SECTION(64, PAGE_SIZE, THREAD_SIZE)
+ _edata = .;
_edata_loc = __data_loc + SIZEOF(.data);
BSS_SECTION(0, 0, 0)
diff --git a/arch/arm64/mm/ioremap.c b/arch/arm64/mm/ioremap.c
index 1725cd6db37a..0218e914f78f 100644
--- a/arch/arm64/mm/ioremap.c
+++ b/arch/arm64/mm/ioremap.c
@@ -25,6 +25,10 @@
#include <linux/vmalloc.h>
#include <linux/io.h>
+#include <asm/fixmap.h>
+#include <asm/tlbflush.h>
+#include <asm/pgalloc.h>
+
static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
pgprot_t prot, void *caller)
{
@@ -79,6 +83,297 @@ void __iounmap(volatile void __iomem *io_addr)
{
void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
+ /* Nothing to do for normal memory. See ioremap_cache() */
+ if (pfn_valid(__phys_to_pfn(__virt_to_phys(addr))))
+ return;
+
vunmap(addr);
}
EXPORT_SYMBOL(__iounmap);
+
+void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
+{
+ /* For normal memory, we already have a cacheable mapping. */
+ if (pfn_valid(__phys_to_pfn(phys_addr)))
+ return (void __iomem *)__phys_to_virt(phys_addr);
+
+ return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
+ __builtin_return_address(0));
+}
+EXPORT_SYMBOL(ioremap_cache);
+
+static int early_ioremap_debug __initdata;
+
+static int __init early_ioremap_debug_setup(char *str)
+{
+ early_ioremap_debug = 1;
+
+ return 0;
+}
+early_param("early_ioremap_debug", early_ioremap_debug_setup);
+
+static int after_paging_init __initdata;
+#ifndef CONFIG_ARM64_64K_PAGES
+static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
+#endif
+
+static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
+{
+ pgd_t *pgd = pgd_offset_k(addr);
+ pud_t *pud = pud_offset(pgd, addr);
+ pmd_t *pmd = pmd_offset(pud, addr);
+
+ return pmd;
+}
+
+static inline pte_t * __init early_ioremap_pte(unsigned long addr)
+{
+ pmd_t *pmd = early_ioremap_pmd(addr);
+ return pte_offset_kernel(pmd, addr);
+}
+
+static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
+
+void __init early_ioremap_init(void)
+{
+ pmd_t *pmd;
+ int i;
+
+ if (early_ioremap_debug)
+ pr_info("early_ioremap_init()\n");
+
+ for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
+ slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
+
+ pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
+#ifndef CONFIG_ARM64_64K_PAGES
+ /* need to populate pmd for 4k pagesize only */
+ pmd_populate_kernel(&init_mm, pmd, bm_pte);
+#endif
+
+ /*
+ * The boot-ioremap range spans multiple pmds, for which
+ * we are not prepared:
+ */
+ BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
+ != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
+
+ if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
+ WARN_ON(1);
+ pr_warn("pmd %p != %p\n",
+ pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
+ pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
+ fix_to_virt(FIX_BTMAP_BEGIN));
+ pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
+ fix_to_virt(FIX_BTMAP_END));
+
+ pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
+ pr_warn("FIX_BTMAP_BEGIN: %d\n",
+ FIX_BTMAP_BEGIN);
+ }
+}
+
+void __init early_ioremap_reset(void)
+{
+ after_paging_init = 1;
+}
+
+void __init __set_fixmap(enum fixed_addresses idx,
+ phys_addr_t phys, pgprot_t flags)
+{
+ unsigned long addr = __fix_to_virt(idx);
+ pte_t *pte;
+
+ if (idx >= __end_of_fixed_addresses) {
+ BUG();
+ return;
+ }
+ if (after_paging_init) {
+ WARN_ON(1);
+ return;
+ }
+
+ pte = early_ioremap_pte(addr);
+
+ if (pgprot_val(flags))
+ set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
+ else {
+ pte_clear(&init_mm, addr, pte);
+ flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
+ }
+}
+
+static inline void __init early_set_fixmap(enum fixed_addresses idx,
+ phys_addr_t phys, pgprot_t prot)
+{
+ __set_fixmap(idx, phys, prot);
+}
+
+static inline void __init early_clear_fixmap(enum fixed_addresses idx)
+{
+ __set_fixmap(idx, 0, __pgprot(0));
+}
+
+static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
+static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
+
+static int __init check_early_ioremap_leak(void)
+{
+ int count = 0;
+ int i;
+
+ for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
+ if (prev_map[i])
+ count++;
+
+ if (!count)
+ return 0;
+ WARN(1, KERN_WARNING
+ "Debug warning: early ioremap leak of %d areas detected.\n",
+ count);
+ pr_warn("Please boot with early_ioremap_debug and report the dmesg.\n");
+
+ return 1;
+}
+late_initcall(check_early_ioremap_leak);
+
+static void __init __iomem *
+__early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
+{
+ unsigned long offset;
+ resource_size_t last_addr;
+ unsigned int nrpages;
+ enum fixed_addresses idx0, idx;
+ int i, slot;
+
+ WARN_ON(system_state != SYSTEM_BOOTING);
+
+ slot = -1;
+ for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
+ if (!prev_map[i]) {
+ slot = i;
+ break;
+ }
+ }
+
+ if (slot < 0) {
+ pr_info("%s(%08llx, %08lx) not found slot\n",
+ __func__, (u64)phys_addr, size);
+ WARN_ON(1);
+ return NULL;
+ }
+
+ if (early_ioremap_debug) {
+ pr_info("%s(%08llx, %08lx) [%d] => ",
+ __func__, (u64)phys_addr, size, slot);
+ dump_stack();
+ }
+
+ /* Don't allow wraparound or zero size */
+ last_addr = phys_addr + size - 1;
+ if (!size || last_addr < phys_addr) {
+ WARN_ON(1);
+ return NULL;
+ }
+
+ prev_size[slot] = size;
+ /*
+ * Mappings have to be page-aligned
+ */
+ offset = phys_addr & ~PAGE_MASK;
+ phys_addr &= PAGE_MASK;
+ size = PAGE_ALIGN(last_addr + 1) - phys_addr;
+
+ /*
+ * Mappings have to fit in the FIX_BTMAP area.
+ */
+ nrpages = size >> PAGE_SHIFT;
+ if (nrpages > NR_FIX_BTMAPS) {
+ WARN_ON(1);
+ return NULL;
+ }
+
+ /*
+ * Ok, go for it..
+ */
+ idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
+ idx = idx0;
+ while (nrpages > 0) {
+ early_set_fixmap(idx, phys_addr, prot);
+ phys_addr += PAGE_SIZE;
+ --idx;
+ --nrpages;
+ }
+ if (early_ioremap_debug)
+ pr_cont("%08lx + %08lx\n", offset, slot_virt[slot]);
+
+ prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
+ return prev_map[slot];
+}
+
+/* Remap an IO device */
+void __init __iomem *
+early_ioremap(resource_size_t phys_addr, unsigned long size)
+{
+ return __early_ioremap(phys_addr, size, __pgprot(PROT_DEVICE_nGnRE));
+}
+
+/* Remap memory */
+void __init __iomem *
+early_memremap(resource_size_t phys_addr, unsigned long size)
+{
+ return __early_ioremap(phys_addr, size,
+ __pgprot(PROT_NORMAL | PTE_PXN | PTE_UXN));
+}
+
+void __init early_iounmap(void __iomem *addr, unsigned long size)
+{
+ unsigned long virt_addr;
+ unsigned long offset;
+ unsigned int nrpages;
+ enum fixed_addresses idx;
+ int i, slot;
+
+ slot = -1;
+ for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
+ if (prev_map[i] == addr) {
+ slot = i;
+ break;
+ }
+ }
+
+ if (slot < 0) {
+ pr_info("early_iounmap(%p, %08lx) not found slot\n",
+ addr, size);
+ WARN_ON(1);
+ return;
+ }
+
+ if (prev_size[slot] != size) {
+ pr_info("early_iounmap(%p, %08lx) [%d] size mismatch: %08lx\n",
+ addr, size, slot, prev_size[slot]);
+ WARN_ON(1);
+ return;
+ }
+
+ if (early_ioremap_debug) {
+ pr_info("early_iounmap(%p, %08lx) [%d]\n", addr, size, slot);
+ dump_stack();
+ }
+
+ virt_addr = (unsigned long)addr;
+ if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
+ WARN_ON(1);
+ return;
+ }
+ offset = virt_addr & ~PAGE_MASK;
+ nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
+
+ idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
+ while (nrpages > 0) {
+ early_clear_fixmap(idx);
+ --idx;
+ --nrpages;
+ }
+ prev_map[slot] = NULL;
+}
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index f557ebbe7013..9849f7f7231f 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -252,47 +252,6 @@ static void __init create_mapping(phys_addr_t phys, unsigned long virt,
} while (pgd++, addr = next, addr != end);
}
-#ifdef CONFIG_EARLY_PRINTK
-/*
- * Create an early I/O mapping using the pgd/pmd entries already populated
- * in head.S as this function is called too early to allocated any memory. The
- * mapping size is 2MB with 4KB pages or 64KB or 64KB pages.
- */
-void __iomem * __init early_io_map(phys_addr_t phys, unsigned long virt)
-{
- unsigned long size, mask;
- bool page64k = IS_ENABLED(CONFIG_ARM64_64K_PAGES);
- pgd_t *pgd;
- pud_t *pud;
- pmd_t *pmd;
- pte_t *pte;
-
- /*
- * No early pte entries with !ARM64_64K_PAGES configuration, so using
- * sections (pmd).
- */
- size = page64k ? PAGE_SIZE : SECTION_SIZE;
- mask = ~(size - 1);
-
- pgd = pgd_offset_k(virt);
- pud = pud_offset(pgd, virt);
- if (pud_none(*pud))
- return NULL;
- pmd = pmd_offset(pud, virt);
-
- if (page64k) {
- if (pmd_none(*pmd))
- return NULL;
- pte = pte_offset_kernel(pmd, virt);
- set_pte(pte, __pte((phys & mask) | PROT_DEVICE_nGnRE));
- } else {
- set_pmd(pmd, __pmd((phys & mask) | PROT_SECT_DEVICE_nGnRE));
- }
-
- return (void __iomem *)((virt & mask) + (phys & ~mask));
-}
-#endif
-
static void __init map_mem(void)
{
struct memblock_region *reg;