linux/arch/x86/platform/efi/efi.c
<<
>>
Prefs
   1/*
   2 * Common EFI (Extensible Firmware Interface) support functions
   3 * Based on Extensible Firmware Interface Specification version 1.0
   4 *
   5 * Copyright (C) 1999 VA Linux Systems
   6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
   7 * Copyright (C) 1999-2002 Hewlett-Packard Co.
   8 *      David Mosberger-Tang <davidm@hpl.hp.com>
   9 *      Stephane Eranian <eranian@hpl.hp.com>
  10 * Copyright (C) 2005-2008 Intel Co.
  11 *      Fenghua Yu <fenghua.yu@intel.com>
  12 *      Bibo Mao <bibo.mao@intel.com>
  13 *      Chandramouli Narayanan <mouli@linux.intel.com>
  14 *      Huang Ying <ying.huang@intel.com>
  15 * Copyright (C) 2013 SuSE Labs
  16 *      Borislav Petkov <bp@suse.de> - runtime services VA mapping
  17 *
  18 * Copied from efi_32.c to eliminate the duplicated code between EFI
  19 * 32/64 support code. --ying 2007-10-26
  20 *
  21 * All EFI Runtime Services are not implemented yet as EFI only
  22 * supports physical mode addressing on SoftSDV. This is to be fixed
  23 * in a future version.  --drummond 1999-07-20
  24 *
  25 * Implemented EFI runtime services and virtual mode calls.  --davidm
  26 *
  27 * Goutham Rao: <goutham.rao@intel.com>
  28 *      Skip non-WB memory and ignore empty memory ranges.
  29 */
  30
  31#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32
  33#include <linux/kernel.h>
  34#include <linux/init.h>
  35#include <linux/efi.h>
  36#include <linux/efi-bgrt.h>
  37#include <linux/export.h>
  38#include <linux/bootmem.h>
  39#include <linux/slab.h>
  40#include <linux/memblock.h>
  41#include <linux/spinlock.h>
  42#include <linux/uaccess.h>
  43#include <linux/time.h>
  44#include <linux/io.h>
  45#include <linux/reboot.h>
  46#include <linux/bcd.h>
  47
  48#include <asm/setup.h>
  49#include <asm/efi.h>
  50#include <asm/time.h>
  51#include <asm/cacheflush.h>
  52#include <asm/tlbflush.h>
  53#include <asm/x86_init.h>
  54#include <asm/rtc.h>
  55#include <asm/uv/uv.h>
  56
  57#define EFI_DEBUG
  58
  59struct efi_memory_map memmap;
  60
  61static struct efi efi_phys __initdata;
  62static efi_system_table_t efi_systab __initdata;
  63
  64static efi_config_table_type_t arch_tables[] __initdata = {
  65#ifdef CONFIG_X86_UV
  66        {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
  67#endif
  68        {NULL_GUID, NULL, NULL},
  69};
  70
  71u64 efi_setup;          /* efi setup_data physical address */
  72
  73int add_efi_memmap;
  74EXPORT_SYMBOL(add_efi_memmap);
  75
  76static int __init setup_add_efi_memmap(char *arg)
  77{
  78        add_efi_memmap = 1;
  79        return 0;
  80}
  81early_param("add_efi_memmap", setup_add_efi_memmap);
  82
  83static efi_status_t __init phys_efi_set_virtual_address_map(
  84        unsigned long memory_map_size,
  85        unsigned long descriptor_size,
  86        u32 descriptor_version,
  87        efi_memory_desc_t *virtual_map)
  88{
  89        efi_status_t status;
  90        unsigned long flags;
  91        pgd_t *save_pgd;
  92
  93        save_pgd = efi_call_phys_prolog();
  94
  95        /* Disable interrupts around EFI calls: */
  96        local_irq_save(flags);
  97        status = efi_call_phys(efi_phys.set_virtual_address_map,
  98                               memory_map_size, descriptor_size,
  99                               descriptor_version, virtual_map);
 100        local_irq_restore(flags);
 101
 102        efi_call_phys_epilog(save_pgd);
 103
 104        return status;
 105}
 106
 107int efi_set_rtc_mmss(const struct timespec *now)
 108{
 109        unsigned long nowtime = now->tv_sec;
 110        efi_status_t    status;
 111        efi_time_t      eft;
 112        efi_time_cap_t  cap;
 113        struct rtc_time tm;
 114
 115        status = efi.get_time(&eft, &cap);
 116        if (status != EFI_SUCCESS) {
 117                pr_err("Oops: efitime: can't read time!\n");
 118                return -1;
 119        }
 120
 121        rtc_time_to_tm(nowtime, &tm);
 122        if (!rtc_valid_tm(&tm)) {
 123                eft.year = tm.tm_year + 1900;
 124                eft.month = tm.tm_mon + 1;
 125                eft.day = tm.tm_mday;
 126                eft.minute = tm.tm_min;
 127                eft.second = tm.tm_sec;
 128                eft.nanosecond = 0;
 129        } else {
 130                pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
 131                       __func__, nowtime);
 132                return -1;
 133        }
 134
 135        status = efi.set_time(&eft);
 136        if (status != EFI_SUCCESS) {
 137                pr_err("Oops: efitime: can't write time!\n");
 138                return -1;
 139        }
 140        return 0;
 141}
 142
 143void efi_get_time(struct timespec *now)
 144{
 145        efi_status_t status;
 146        efi_time_t eft;
 147        efi_time_cap_t cap;
 148
 149        status = efi.get_time(&eft, &cap);
 150        if (status != EFI_SUCCESS)
 151                pr_err("Oops: efitime: can't read time!\n");
 152
 153        now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
 154                             eft.minute, eft.second);
 155        now->tv_nsec = 0;
 156}
 157
 158void __init efi_find_mirror(void)
 159{
 160        void *p;
 161        u64 mirror_size = 0, total_size = 0;
 162
 163        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 164                efi_memory_desc_t *md = p;
 165                unsigned long long start = md->phys_addr;
 166                unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
 167
 168                total_size += size;
 169                if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
 170                        memblock_mark_mirror(start, size);
 171                        mirror_size += size;
 172                }
 173        }
 174        if (mirror_size)
 175                pr_info("Memory: %lldM/%lldM mirrored memory\n",
 176                        mirror_size>>20, total_size>>20);
 177}
 178
 179/*
 180 * Tell the kernel about the EFI memory map.  This might include
 181 * more than the max 128 entries that can fit in the e820 legacy
 182 * (zeropage) memory map.
 183 */
 184
 185static void __init do_add_efi_memmap(void)
 186{
 187        void *p;
 188
 189        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 190                efi_memory_desc_t *md = p;
 191                unsigned long long start = md->phys_addr;
 192                unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
 193                int e820_type;
 194
 195                switch (md->type) {
 196                case EFI_LOADER_CODE:
 197                case EFI_LOADER_DATA:
 198                case EFI_BOOT_SERVICES_CODE:
 199                case EFI_BOOT_SERVICES_DATA:
 200                case EFI_CONVENTIONAL_MEMORY:
 201                        if (md->attribute & EFI_MEMORY_WB)
 202                                e820_type = E820_RAM;
 203                        else
 204                                e820_type = E820_RESERVED;
 205                        break;
 206                case EFI_ACPI_RECLAIM_MEMORY:
 207                        e820_type = E820_ACPI;
 208                        break;
 209                case EFI_ACPI_MEMORY_NVS:
 210                        e820_type = E820_NVS;
 211                        break;
 212                case EFI_UNUSABLE_MEMORY:
 213                        e820_type = E820_UNUSABLE;
 214                        break;
 215                case EFI_PERSISTENT_MEMORY:
 216                        e820_type = E820_PMEM;
 217                        break;
 218                default:
 219                        /*
 220                         * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
 221                         * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
 222                         * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
 223                         */
 224                        e820_type = E820_RESERVED;
 225                        break;
 226                }
 227                e820_add_region(start, size, e820_type);
 228        }
 229        sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
 230}
 231
 232int __init efi_memblock_x86_reserve_range(void)
 233{
 234        struct efi_info *e = &boot_params.efi_info;
 235        phys_addr_t pmap;
 236
 237#ifdef CONFIG_X86_32
 238        /* Can't handle data above 4GB at this time */
 239        if (e->efi_memmap_hi) {
 240                pr_err("Memory map is above 4GB, disabling EFI.\n");
 241                return -EINVAL;
 242        }
 243        pmap =  e->efi_memmap;
 244#else
 245        pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
 246#endif
 247        memmap.phys_map         = pmap;
 248        memmap.nr_map           = e->efi_memmap_size /
 249                                  e->efi_memdesc_size;
 250        memmap.desc_size        = e->efi_memdesc_size;
 251        memmap.desc_version     = e->efi_memdesc_version;
 252
 253        memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
 254
 255        efi.memmap = &memmap;
 256
 257        return 0;
 258}
 259
 260static void __init print_efi_memmap(void)
 261{
 262#ifdef EFI_DEBUG
 263        efi_memory_desc_t *md;
 264        void *p;
 265        int i;
 266
 267        for (p = memmap.map, i = 0;
 268             p < memmap.map_end;
 269             p += memmap.desc_size, i++) {
 270                md = p;
 271                pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
 272                        i, md->type, md->attribute, md->phys_addr,
 273                        md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
 274                        (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
 275        }
 276#endif  /*  EFI_DEBUG  */
 277}
 278
 279void __init efi_unmap_memmap(void)
 280{
 281        clear_bit(EFI_MEMMAP, &efi.flags);
 282        if (memmap.map) {
 283                early_memunmap(memmap.map, memmap.nr_map * memmap.desc_size);
 284                memmap.map = NULL;
 285        }
 286}
 287
 288static int __init efi_systab_init(void *phys)
 289{
 290        if (efi_enabled(EFI_64BIT)) {
 291                efi_system_table_64_t *systab64;
 292                struct efi_setup_data *data = NULL;
 293                u64 tmp = 0;
 294
 295                if (efi_setup) {
 296                        data = early_memremap(efi_setup, sizeof(*data));
 297                        if (!data)
 298                                return -ENOMEM;
 299                }
 300                systab64 = early_memremap((unsigned long)phys,
 301                                         sizeof(*systab64));
 302                if (systab64 == NULL) {
 303                        pr_err("Couldn't map the system table!\n");
 304                        if (data)
 305                                early_memunmap(data, sizeof(*data));
 306                        return -ENOMEM;
 307                }
 308
 309                efi_systab.hdr = systab64->hdr;
 310                efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
 311                                              systab64->fw_vendor;
 312                tmp |= data ? data->fw_vendor : systab64->fw_vendor;
 313                efi_systab.fw_revision = systab64->fw_revision;
 314                efi_systab.con_in_handle = systab64->con_in_handle;
 315                tmp |= systab64->con_in_handle;
 316                efi_systab.con_in = systab64->con_in;
 317                tmp |= systab64->con_in;
 318                efi_systab.con_out_handle = systab64->con_out_handle;
 319                tmp |= systab64->con_out_handle;
 320                efi_systab.con_out = systab64->con_out;
 321                tmp |= systab64->con_out;
 322                efi_systab.stderr_handle = systab64->stderr_handle;
 323                tmp |= systab64->stderr_handle;
 324                efi_systab.stderr = systab64->stderr;
 325                tmp |= systab64->stderr;
 326                efi_systab.runtime = data ?
 327                                     (void *)(unsigned long)data->runtime :
 328                                     (void *)(unsigned long)systab64->runtime;
 329                tmp |= data ? data->runtime : systab64->runtime;
 330                efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
 331                tmp |= systab64->boottime;
 332                efi_systab.nr_tables = systab64->nr_tables;
 333                efi_systab.tables = data ? (unsigned long)data->tables :
 334                                           systab64->tables;
 335                tmp |= data ? data->tables : systab64->tables;
 336
 337                early_memunmap(systab64, sizeof(*systab64));
 338                if (data)
 339                        early_memunmap(data, sizeof(*data));
 340#ifdef CONFIG_X86_32
 341                if (tmp >> 32) {
 342                        pr_err("EFI data located above 4GB, disabling EFI.\n");
 343                        return -EINVAL;
 344                }
 345#endif
 346        } else {
 347                efi_system_table_32_t *systab32;
 348
 349                systab32 = early_memremap((unsigned long)phys,
 350                                         sizeof(*systab32));
 351                if (systab32 == NULL) {
 352                        pr_err("Couldn't map the system table!\n");
 353                        return -ENOMEM;
 354                }
 355
 356                efi_systab.hdr = systab32->hdr;
 357                efi_systab.fw_vendor = systab32->fw_vendor;
 358                efi_systab.fw_revision = systab32->fw_revision;
 359                efi_systab.con_in_handle = systab32->con_in_handle;
 360                efi_systab.con_in = systab32->con_in;
 361                efi_systab.con_out_handle = systab32->con_out_handle;
 362                efi_systab.con_out = systab32->con_out;
 363                efi_systab.stderr_handle = systab32->stderr_handle;
 364                efi_systab.stderr = systab32->stderr;
 365                efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
 366                efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
 367                efi_systab.nr_tables = systab32->nr_tables;
 368                efi_systab.tables = systab32->tables;
 369
 370                early_memunmap(systab32, sizeof(*systab32));
 371        }
 372
 373        efi.systab = &efi_systab;
 374
 375        /*
 376         * Verify the EFI Table
 377         */
 378        if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
 379                pr_err("System table signature incorrect!\n");
 380                return -EINVAL;
 381        }
 382        if ((efi.systab->hdr.revision >> 16) == 0)
 383                pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
 384                       efi.systab->hdr.revision >> 16,
 385                       efi.systab->hdr.revision & 0xffff);
 386
 387        return 0;
 388}
 389
 390static int __init efi_runtime_init32(void)
 391{
 392        efi_runtime_services_32_t *runtime;
 393
 394        runtime = early_memremap((unsigned long)efi.systab->runtime,
 395                        sizeof(efi_runtime_services_32_t));
 396        if (!runtime) {
 397                pr_err("Could not map the runtime service table!\n");
 398                return -ENOMEM;
 399        }
 400
 401        /*
 402         * We will only need *early* access to the SetVirtualAddressMap
 403         * EFI runtime service. All other runtime services will be called
 404         * via the virtual mapping.
 405         */
 406        efi_phys.set_virtual_address_map =
 407                        (efi_set_virtual_address_map_t *)
 408                        (unsigned long)runtime->set_virtual_address_map;
 409        early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
 410
 411        return 0;
 412}
 413
 414static int __init efi_runtime_init64(void)
 415{
 416        efi_runtime_services_64_t *runtime;
 417
 418        runtime = early_memremap((unsigned long)efi.systab->runtime,
 419                        sizeof(efi_runtime_services_64_t));
 420        if (!runtime) {
 421                pr_err("Could not map the runtime service table!\n");
 422                return -ENOMEM;
 423        }
 424
 425        /*
 426         * We will only need *early* access to the SetVirtualAddressMap
 427         * EFI runtime service. All other runtime services will be called
 428         * via the virtual mapping.
 429         */
 430        efi_phys.set_virtual_address_map =
 431                        (efi_set_virtual_address_map_t *)
 432                        (unsigned long)runtime->set_virtual_address_map;
 433        early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
 434
 435        return 0;
 436}
 437
 438static int __init efi_runtime_init(void)
 439{
 440        int rv;
 441
 442        /*
 443         * Check out the runtime services table. We need to map
 444         * the runtime services table so that we can grab the physical
 445         * address of several of the EFI runtime functions, needed to
 446         * set the firmware into virtual mode.
 447         */
 448        if (efi_enabled(EFI_64BIT))
 449                rv = efi_runtime_init64();
 450        else
 451                rv = efi_runtime_init32();
 452
 453        if (rv)
 454                return rv;
 455
 456        return 0;
 457}
 458
 459static int __init efi_memmap_init(void)
 460{
 461        /* Map the EFI memory map */
 462        memmap.map = early_memremap((unsigned long)memmap.phys_map,
 463                                   memmap.nr_map * memmap.desc_size);
 464        if (memmap.map == NULL) {
 465                pr_err("Could not map the memory map!\n");
 466                return -ENOMEM;
 467        }
 468        memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
 469
 470        if (add_efi_memmap)
 471                do_add_efi_memmap();
 472
 473        return 0;
 474}
 475
 476void __init efi_init(void)
 477{
 478        efi_char16_t *c16;
 479        char vendor[100] = "unknown";
 480        int i = 0;
 481        void *tmp;
 482
 483#ifdef CONFIG_X86_32
 484        if (boot_params.efi_info.efi_systab_hi ||
 485            boot_params.efi_info.efi_memmap_hi) {
 486                pr_info("Table located above 4GB, disabling EFI.\n");
 487                return;
 488        }
 489        efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
 490#else
 491        efi_phys.systab = (efi_system_table_t *)
 492                          (boot_params.efi_info.efi_systab |
 493                          ((__u64)boot_params.efi_info.efi_systab_hi<<32));
 494#endif
 495
 496        if (efi_systab_init(efi_phys.systab))
 497                return;
 498
 499        set_bit(EFI_SYSTEM_TABLES, &efi.flags);
 500
 501        efi.config_table = (unsigned long)efi.systab->tables;
 502        efi.fw_vendor    = (unsigned long)efi.systab->fw_vendor;
 503        efi.runtime      = (unsigned long)efi.systab->runtime;
 504
 505        /*
 506         * Show what we know for posterity
 507         */
 508        c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
 509        if (c16) {
 510                for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
 511                        vendor[i] = *c16++;
 512                vendor[i] = '\0';
 513        } else
 514                pr_err("Could not map the firmware vendor!\n");
 515        early_memunmap(tmp, 2);
 516
 517        pr_info("EFI v%u.%.02u by %s\n",
 518                efi.systab->hdr.revision >> 16,
 519                efi.systab->hdr.revision & 0xffff, vendor);
 520
 521        if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
 522                return;
 523
 524        if (efi_config_init(arch_tables))
 525                return;
 526
 527        set_bit(EFI_CONFIG_TABLES, &efi.flags);
 528
 529        /*
 530         * Note: We currently don't support runtime services on an EFI
 531         * that doesn't match the kernel 32/64-bit mode.
 532         */
 533
 534        if (!efi_runtime_supported())
 535                pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
 536        else {
 537                if (efi_runtime_disabled() || efi_runtime_init())
 538                        return;
 539                set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 540        }
 541
 542        if (efi_memmap_init())
 543                return;
 544
 545        set_bit(EFI_MEMMAP, &efi.flags);
 546
 547#ifdef CONFIG_X86_32
 548        if (efi_is_native()) {
 549                x86_platform.get_wallclock = efi_get_time;
 550                x86_platform.set_wallclock = efi_set_rtc_mmss;
 551        }
 552#endif
 553        print_efi_memmap();
 554
 555        efi_esrt_init();
 556}
 557
 558void __init efi_late_init(void)
 559{
 560        /* RHEL7: Do not run on kdump kernel */
 561        if (!efi_setup)
 562                efi_bgrt_init();
 563}
 564
 565void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
 566{
 567        u64 addr, npages;
 568
 569        addr = md->virt_addr;
 570        npages = md->num_pages;
 571
 572        memrange_efi_to_native(&addr, &npages);
 573
 574        if (executable)
 575                set_memory_x(addr, npages);
 576        else
 577                set_memory_nx(addr, npages);
 578}
 579
 580static void __init runtime_code_page_mkexec(void)
 581{
 582        efi_memory_desc_t *md;
 583        void *p;
 584
 585        /* Make EFI runtime service code area executable */
 586        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 587                md = p;
 588
 589                if (md->type != EFI_RUNTIME_SERVICES_CODE)
 590                        continue;
 591
 592                efi_set_executable(md, true);
 593        }
 594}
 595
 596void __init efi_memory_uc(u64 addr, unsigned long size)
 597{
 598        unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
 599        u64 npages;
 600
 601        npages = round_up(size, page_shift) / page_shift;
 602        memrange_efi_to_native(&addr, &npages);
 603        set_memory_uc(addr, npages);
 604}
 605
 606void __init old_map_region(efi_memory_desc_t *md)
 607{
 608        u64 start_pfn, end_pfn, end;
 609        unsigned long size;
 610        void *va;
 611
 612        start_pfn = PFN_DOWN(md->phys_addr);
 613        size      = md->num_pages << PAGE_SHIFT;
 614        end       = md->phys_addr + size;
 615        end_pfn   = PFN_UP(end);
 616
 617        if (pfn_range_is_mapped(start_pfn, end_pfn)) {
 618                va = __va(md->phys_addr);
 619
 620                if (!(md->attribute & EFI_MEMORY_WB))
 621                        efi_memory_uc((u64)(unsigned long)va, size);
 622        } else
 623                va = efi_ioremap(md->phys_addr, size,
 624                                 md->type, md->attribute);
 625
 626        md->virt_addr = (u64) (unsigned long) va;
 627        if (!va)
 628                pr_err("ioremap of 0x%llX failed!\n",
 629                       (unsigned long long)md->phys_addr);
 630}
 631
 632/* Merge contiguous regions of the same type and attribute */
 633static void __init efi_merge_regions(void)
 634{
 635        void *p;
 636        efi_memory_desc_t *md, *prev_md = NULL;
 637
 638        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 639                u64 prev_size;
 640                md = p;
 641
 642                if (!prev_md) {
 643                        prev_md = md;
 644                        continue;
 645                }
 646
 647                if (prev_md->type != md->type ||
 648                    prev_md->attribute != md->attribute) {
 649                        prev_md = md;
 650                        continue;
 651                }
 652
 653                prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
 654
 655                if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
 656                        prev_md->num_pages += md->num_pages;
 657                        md->type = EFI_RESERVED_TYPE;
 658                        md->attribute = 0;
 659                        continue;
 660                }
 661                prev_md = md;
 662        }
 663}
 664
 665static void __init get_systab_virt_addr(efi_memory_desc_t *md)
 666{
 667        unsigned long size;
 668        u64 end, systab;
 669
 670        size = md->num_pages << EFI_PAGE_SHIFT;
 671        end = md->phys_addr + size;
 672        systab = (u64)(unsigned long)efi_phys.systab;
 673        if (md->phys_addr <= systab && systab < end) {
 674                systab += md->virt_addr - md->phys_addr;
 675                efi.systab = (efi_system_table_t *)(unsigned long)systab;
 676        }
 677}
 678
 679static void __init save_runtime_map(void)
 680{
 681#ifdef CONFIG_KEXEC_CORE
 682        efi_memory_desc_t *md;
 683        void *tmp, *p, *q = NULL;
 684        int count = 0;
 685
 686        if (efi_enabled(EFI_OLD_MEMMAP))
 687                return;
 688
 689        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 690                md = p;
 691
 692                if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
 693                    (md->type == EFI_BOOT_SERVICES_CODE) ||
 694                    (md->type == EFI_BOOT_SERVICES_DATA))
 695                        continue;
 696                tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
 697                if (!tmp)
 698                        goto out;
 699                q = tmp;
 700
 701                memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
 702                count++;
 703        }
 704
 705        efi_runtime_map_setup(q, count, memmap.desc_size);
 706        return;
 707
 708out:
 709        kfree(q);
 710        pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
 711#endif
 712}
 713
 714static void *realloc_pages(void *old_memmap, int old_shift)
 715{
 716        void *ret;
 717
 718        ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
 719        if (!ret)
 720                goto out;
 721
 722        /*
 723         * A first-time allocation doesn't have anything to copy.
 724         */
 725        if (!old_memmap)
 726                return ret;
 727
 728        memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
 729
 730out:
 731        free_pages((unsigned long)old_memmap, old_shift);
 732        return ret;
 733}
 734
 735static bool should_map_region(efi_memory_desc_t *md)
 736{
 737        /*
 738         * Runtime regions always require runtime mappings (obviously).
 739         */
 740        if (md->attribute & EFI_MEMORY_RUNTIME)
 741                return true;
 742
 743        /*
 744         * 32-bit EFI doesn't suffer from the bug that requires us to
 745         * reserve boot services regions, and mixed mode support
 746         * doesn't exist for 32-bit kernels.
 747         */
 748        if (IS_ENABLED(CONFIG_X86_32))
 749                return false;
 750
 751        /*
 752         * Map all of RAM so that we can access arguments in the 1:1
 753         * mapping when making EFI runtime calls.
 754         */
 755        if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
 756                if (md->type == EFI_CONVENTIONAL_MEMORY ||
 757                    md->type == EFI_LOADER_DATA ||
 758                    md->type == EFI_LOADER_CODE)
 759                        return true;
 760        }
 761
 762        /*
 763         * Map boot services regions as a workaround for buggy
 764         * firmware that accesses them even when they shouldn't.
 765         *
 766         * See efi_{reserve,free}_boot_services().
 767         */
 768        if (md->type == EFI_BOOT_SERVICES_CODE ||
 769            md->type == EFI_BOOT_SERVICES_DATA)
 770                return true;
 771
 772        return false;
 773}
 774
 775/*
 776 * Iterate the EFI memory map in reverse order because the regions
 777 * will be mapped top-down. The end result is the same as if we had
 778 * mapped things forward, but doesn't require us to change the
 779 * existing implementation of efi_map_region().
 780 */
 781static inline void *efi_map_next_entry_reverse(void *entry)
 782{
 783        /* Initial call */
 784        if (!entry)
 785                return memmap.map_end - memmap.desc_size;
 786
 787        entry -= memmap.desc_size;
 788        if (entry < memmap.map)
 789                return NULL;
 790
 791        return entry;
 792}
 793
 794/*
 795 * efi_map_next_entry - Return the next EFI memory map descriptor
 796 * @entry: Previous EFI memory map descriptor
 797 *
 798 * This is a helper function to iterate over the EFI memory map, which
 799 * we do in different orders depending on the current configuration.
 800 *
 801 * To begin traversing the memory map @entry must be %NULL.
 802 *
 803 * Returns %NULL when we reach the end of the memory map.
 804 */
 805static void *efi_map_next_entry(void *entry)
 806{
 807        if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
 808                /*
 809                 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
 810                 * config table feature requires us to map all entries
 811                 * in the same order as they appear in the EFI memory
 812                 * map. That is to say, entry N must have a lower
 813                 * virtual address than entry N+1. This is because the
 814                 * firmware toolchain leaves relative references in
 815                 * the code/data sections, which are split and become
 816                 * separate EFI memory regions. Mapping things
 817                 * out-of-order leads to the firmware accessing
 818                 * unmapped addresses.
 819                 *
 820                 * Since we need to map things this way whether or not
 821                 * the kernel actually makes use of
 822                 * EFI_PROPERTIES_TABLE, let's just switch to this
 823                 * scheme by default for 64-bit.
 824                 */
 825                return efi_map_next_entry_reverse(entry);
 826        }
 827
 828        /* Initial call */
 829        if (!entry)
 830                return memmap.map;
 831
 832        entry += memmap.desc_size;
 833        if (entry >= memmap.map_end)
 834                return NULL;
 835
 836        return entry;
 837}
 838
 839/*
 840 * Map the efi memory ranges of the runtime services and update new_mmap with
 841 * virtual addresses.
 842 */
 843static void * __init efi_map_regions(int *count, int *pg_shift)
 844{
 845        void *p, *new_memmap = NULL;
 846        unsigned long left = 0;
 847        efi_memory_desc_t *md;
 848
 849        p = NULL;
 850        while ((p = efi_map_next_entry(p))) {
 851                md = p;
 852
 853                if (!should_map_region(md))
 854                        continue;
 855
 856                efi_map_region(md);
 857                get_systab_virt_addr(md);
 858
 859                if (left < memmap.desc_size) {
 860                        new_memmap = realloc_pages(new_memmap, *pg_shift);
 861                        if (!new_memmap)
 862                                return NULL;
 863
 864                        left += PAGE_SIZE << *pg_shift;
 865                        (*pg_shift)++;
 866                }
 867
 868                memcpy(new_memmap + (*count * memmap.desc_size), md,
 869                       memmap.desc_size);
 870
 871                left -= memmap.desc_size;
 872                (*count)++;
 873        }
 874
 875        return new_memmap;
 876}
 877
 878static void __init kexec_enter_virtual_mode(void)
 879{
 880#ifdef CONFIG_KEXEC_CORE
 881        efi_memory_desc_t *md;
 882        unsigned int num_pages;
 883        void *p;
 884
 885        efi.systab = NULL;
 886
 887        /*
 888         * We don't do virtual mode, since we don't do runtime services, on
 889         * non-native EFI
 890         */
 891        if (!efi_is_native()) {
 892                efi_unmap_memmap();
 893                clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 894                return;
 895        }
 896
 897        if (efi_alloc_page_tables()) {
 898                pr_err("Failed to allocate EFI page tables\n");
 899                clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 900                return;
 901        }
 902
 903        /*
 904        * Map efi regions which were passed via setup_data. The virt_addr is a
 905        * fixed addr which was used in first kernel of a kexec boot.
 906        */
 907        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 908                md = p;
 909                efi_map_region_fixed(md); /* FIXME: add error handling */
 910                get_systab_virt_addr(md);
 911        }
 912
 913        save_runtime_map();
 914
 915        BUG_ON(!efi.systab);
 916
 917        num_pages = ALIGN(memmap.nr_map * memmap.desc_size, PAGE_SIZE);
 918        num_pages >>= PAGE_SHIFT;
 919
 920        if (efi_setup_page_tables(memmap.phys_map, num_pages)) {
 921                clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 922                return;
 923        }
 924
 925        efi_sync_low_kernel_mappings();
 926
 927        /*
 928         * Now that EFI is in virtual mode, update the function
 929         * pointers in the runtime service table to the new virtual addresses.
 930         *
 931         * Call EFI services through wrapper functions.
 932         */
 933        efi.runtime_version = efi_systab.hdr.revision;
 934
 935        efi_native_runtime_setup();
 936
 937        efi.set_virtual_address_map = NULL;
 938
 939        if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
 940                runtime_code_page_mkexec();
 941
 942        /* clean DUMMY object */
 943        efi_delete_dummy_variable();
 944#endif
 945}
 946
 947/*
 948 * This function will switch the EFI runtime services to virtual mode.
 949 * Essentially, we look through the EFI memmap and map every region that
 950 * has the runtime attribute bit set in its memory descriptor into the
 951 * efi_pgd page table.
 952 *
 953 * The old method which used to update that memory descriptor with the
 954 * virtual address obtained from ioremap() is still supported when the
 955 * kernel is booted with efi=old_map on its command line. Same old
 956 * method enabled the runtime services to be called without having to
 957 * thunk back into physical mode for every invocation.
 958 *
 959 * The new method does a pagetable switch in a preemption-safe manner
 960 * so that we're in a different address space when calling a runtime
 961 * function. For function arguments passing we do copy the PUDs of the
 962 * kernel page table into efi_pgd prior to each call.
 963 *
 964 * Specially for kexec boot, efi runtime maps in previous kernel should
 965 * be passed in via setup_data. In that case runtime ranges will be mapped
 966 * to the same virtual addresses as the first kernel, see
 967 * kexec_enter_virtual_mode().
 968 */
 969static void __init __efi_enter_virtual_mode(void)
 970{
 971        int count = 0, pg_shift = 0;
 972        void *new_memmap = NULL;
 973        efi_status_t status;
 974
 975        efi.systab = NULL;
 976
 977        if (efi_alloc_page_tables()) {
 978                pr_err("Failed to allocate EFI page tables\n");
 979                clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 980                return;
 981        }
 982
 983        efi_merge_regions();
 984        new_memmap = efi_map_regions(&count, &pg_shift);
 985        if (!new_memmap) {
 986                pr_err("Error reallocating memory, EFI runtime non-functional!\n");
 987                clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 988                return;
 989        }
 990
 991        save_runtime_map();
 992
 993        BUG_ON(!efi.systab);
 994
 995        if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift)) {
 996                clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 997                return;
 998        }
 999
1000        efi_sync_low_kernel_mappings();
1001        efi_dump_pagetable();
1002
1003        if (efi_is_native()) {
1004                status = phys_efi_set_virtual_address_map(
1005                                memmap.desc_size * count,
1006                                memmap.desc_size,
1007                                memmap.desc_version,
1008                                (efi_memory_desc_t *)__pa(new_memmap));
1009        } else {
1010                status = efi_thunk_set_virtual_address_map(
1011                                efi_phys.set_virtual_address_map,
1012                                memmap.desc_size * count,
1013                                memmap.desc_size,
1014                                memmap.desc_version,
1015                                (efi_memory_desc_t *)__pa(new_memmap));
1016        }
1017
1018        if (status != EFI_SUCCESS) {
1019                pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1020                         status);
1021                panic("EFI call to SetVirtualAddressMap() failed!");
1022        }
1023
1024        /*
1025         * Now that EFI is in virtual mode, update the function
1026         * pointers in the runtime service table to the new virtual addresses.
1027         *
1028         * Call EFI services through wrapper functions.
1029         */
1030        efi.runtime_version = efi_systab.hdr.revision;
1031
1032        if (efi_is_native())
1033                efi_native_runtime_setup();
1034        else
1035                efi_thunk_runtime_setup();
1036
1037        efi.set_virtual_address_map = NULL;
1038
1039        if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1040                runtime_code_page_mkexec();
1041
1042        /*
1043         * We mapped the descriptor array into the EFI pagetable above
1044         * but we're not unmapping it here because if we're running in
1045         * EFI mixed mode we need all of memory to be accessible when
1046         * we pass parameters to the EFI runtime services in the
1047         * thunking code.
1048         *
1049         * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1050         */
1051        free_pages((unsigned long)new_memmap, pg_shift);
1052
1053        /* clean DUMMY object */
1054        efi_delete_dummy_variable();
1055}
1056
1057void __init efi_enter_virtual_mode(void)
1058{
1059        if (efi_setup)
1060                kexec_enter_virtual_mode();
1061        else
1062                __efi_enter_virtual_mode();
1063}
1064
1065/*
1066 * Convenience functions to obtain memory types and attributes
1067 */
1068int efi_mem_type(unsigned long phys_addr)
1069{
1070        efi_memory_desc_t *md;
1071        void *p;
1072
1073        if (!efi_enabled(EFI_MEMMAP))
1074                return -ENOTSUPP;
1075
1076        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1077                md = p;
1078                if ((md->phys_addr <= phys_addr) &&
1079                    (phys_addr < (md->phys_addr +
1080                                  (md->num_pages << EFI_PAGE_SHIFT))))
1081                        return md->type;
1082        }
1083        return -EINVAL;
1084}
1085
1086u64 efi_mem_attributes(unsigned long phys_addr)
1087{
1088        efi_memory_desc_t *md;
1089        void *p;
1090
1091        if (!efi_enabled(EFI_MEMMAP))
1092                return 0;
1093
1094        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1095                md = p;
1096                if ((md->phys_addr <= phys_addr) &&
1097                    (phys_addr < (md->phys_addr +
1098                                  (md->num_pages << EFI_PAGE_SHIFT))))
1099                        return md->attribute;
1100        }
1101        return 0;
1102}
1103
1104static int __init parse_efi_cmdline(char *str)
1105{
1106        if (*str == '=')
1107                str++;
1108
1109        if (!strncmp(str, "old_map", 7))
1110                set_bit(EFI_OLD_MEMMAP, &efi.flags);
1111
1112        return 0;
1113}
1114early_param("efi", parse_efi_cmdline);
1115