Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 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 | * |
| 16 | * Copied from efi_32.c to eliminate the duplicated code between EFI |
| 17 | * 32/64 support code. --ying 2007-10-26 |
| 18 | * |
| 19 | * All EFI Runtime Services are not implemented yet as EFI only |
| 20 | * supports physical mode addressing on SoftSDV. This is to be fixed |
| 21 | * in a future version. --drummond 1999-07-20 |
| 22 | * |
| 23 | * Implemented EFI runtime services and virtual mode calls. --davidm |
| 24 | * |
| 25 | * Goutham Rao: <goutham.rao@intel.com> |
| 26 | * Skip non-WB memory and ignore empty memory ranges. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/efi.h> |
| 32 | #include <linux/bootmem.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 33 | #include <linux/memblock.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/uaccess.h> |
| 36 | #include <linux/time.h> |
| 37 | #include <linux/io.h> |
| 38 | #include <linux/reboot.h> |
| 39 | #include <linux/bcd.h> |
| 40 | |
| 41 | #include <asm/setup.h> |
| 42 | #include <asm/efi.h> |
| 43 | #include <asm/time.h> |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 44 | #include <asm/cacheflush.h> |
| 45 | #include <asm/tlbflush.h> |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 46 | #include <asm/x86_init.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 47 | |
| 48 | #define EFI_DEBUG 1 |
| 49 | #define PFX "EFI: " |
| 50 | |
| 51 | int efi_enabled; |
| 52 | EXPORT_SYMBOL(efi_enabled); |
| 53 | |
| 54 | struct efi efi; |
| 55 | EXPORT_SYMBOL(efi); |
| 56 | |
| 57 | struct efi_memory_map memmap; |
| 58 | |
Harvey Harrison | ecaea42 | 2008-02-13 13:26:13 -0800 | [diff] [blame] | 59 | static struct efi efi_phys __initdata; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 60 | static efi_system_table_t efi_systab __initdata; |
| 61 | |
Huang, Ying | 8b2cb7a | 2008-01-30 13:32:11 +0100 | [diff] [blame] | 62 | static int __init setup_noefi(char *arg) |
| 63 | { |
| 64 | efi_enabled = 0; |
| 65 | return 0; |
| 66 | } |
| 67 | early_param("noefi", setup_noefi); |
| 68 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 69 | int add_efi_memmap; |
| 70 | EXPORT_SYMBOL(add_efi_memmap); |
| 71 | |
| 72 | static int __init setup_add_efi_memmap(char *arg) |
| 73 | { |
| 74 | add_efi_memmap = 1; |
| 75 | return 0; |
| 76 | } |
| 77 | early_param("add_efi_memmap", setup_add_efi_memmap); |
| 78 | |
| 79 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 80 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
| 81 | { |
| 82 | return efi_call_virt2(get_time, tm, tc); |
| 83 | } |
| 84 | |
| 85 | static efi_status_t virt_efi_set_time(efi_time_t *tm) |
| 86 | { |
| 87 | return efi_call_virt1(set_time, tm); |
| 88 | } |
| 89 | |
| 90 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, |
| 91 | efi_bool_t *pending, |
| 92 | efi_time_t *tm) |
| 93 | { |
| 94 | return efi_call_virt3(get_wakeup_time, |
| 95 | enabled, pending, tm); |
| 96 | } |
| 97 | |
| 98 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) |
| 99 | { |
| 100 | return efi_call_virt2(set_wakeup_time, |
| 101 | enabled, tm); |
| 102 | } |
| 103 | |
| 104 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, |
| 105 | efi_guid_t *vendor, |
| 106 | u32 *attr, |
| 107 | unsigned long *data_size, |
| 108 | void *data) |
| 109 | { |
| 110 | return efi_call_virt5(get_variable, |
| 111 | name, vendor, attr, |
| 112 | data_size, data); |
| 113 | } |
| 114 | |
| 115 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, |
| 116 | efi_char16_t *name, |
| 117 | efi_guid_t *vendor) |
| 118 | { |
| 119 | return efi_call_virt3(get_next_variable, |
| 120 | name_size, name, vendor); |
| 121 | } |
| 122 | |
| 123 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, |
| 124 | efi_guid_t *vendor, |
Matthew Garrett | f7a2d73 | 2011-06-06 15:36:24 -0400 | [diff] [blame^] | 125 | u32 attr, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 126 | unsigned long data_size, |
| 127 | void *data) |
| 128 | { |
| 129 | return efi_call_virt5(set_variable, |
| 130 | name, vendor, attr, |
| 131 | data_size, data); |
| 132 | } |
| 133 | |
| 134 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
| 135 | { |
| 136 | return efi_call_virt1(get_next_high_mono_count, count); |
| 137 | } |
| 138 | |
| 139 | static void virt_efi_reset_system(int reset_type, |
| 140 | efi_status_t status, |
| 141 | unsigned long data_size, |
| 142 | efi_char16_t *data) |
| 143 | { |
| 144 | efi_call_virt4(reset_system, reset_type, status, |
| 145 | data_size, data); |
| 146 | } |
| 147 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 148 | static efi_status_t __init phys_efi_set_virtual_address_map( |
| 149 | unsigned long memory_map_size, |
| 150 | unsigned long descriptor_size, |
| 151 | u32 descriptor_version, |
| 152 | efi_memory_desc_t *virtual_map) |
| 153 | { |
| 154 | efi_status_t status; |
| 155 | |
| 156 | efi_call_phys_prelog(); |
| 157 | status = efi_call_phys4(efi_phys.set_virtual_address_map, |
| 158 | memory_map_size, descriptor_size, |
| 159 | descriptor_version, virtual_map); |
| 160 | efi_call_phys_epilog(); |
| 161 | return status; |
| 162 | } |
| 163 | |
| 164 | static efi_status_t __init phys_efi_get_time(efi_time_t *tm, |
| 165 | efi_time_cap_t *tc) |
| 166 | { |
| 167 | efi_status_t status; |
| 168 | |
| 169 | efi_call_phys_prelog(); |
| 170 | status = efi_call_phys2(efi_phys.get_time, tm, tc); |
| 171 | efi_call_phys_epilog(); |
| 172 | return status; |
| 173 | } |
| 174 | |
| 175 | int efi_set_rtc_mmss(unsigned long nowtime) |
| 176 | { |
| 177 | int real_seconds, real_minutes; |
| 178 | efi_status_t status; |
| 179 | efi_time_t eft; |
| 180 | efi_time_cap_t cap; |
| 181 | |
| 182 | status = efi.get_time(&eft, &cap); |
| 183 | if (status != EFI_SUCCESS) { |
| 184 | printk(KERN_ERR "Oops: efitime: can't read time!\n"); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | real_seconds = nowtime % 60; |
| 189 | real_minutes = nowtime / 60; |
| 190 | if (((abs(real_minutes - eft.minute) + 15)/30) & 1) |
| 191 | real_minutes += 30; |
| 192 | real_minutes %= 60; |
| 193 | eft.minute = real_minutes; |
| 194 | eft.second = real_seconds; |
| 195 | |
| 196 | status = efi.set_time(&eft); |
| 197 | if (status != EFI_SUCCESS) { |
| 198 | printk(KERN_ERR "Oops: efitime: can't write time!\n"); |
| 199 | return -1; |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | unsigned long efi_get_time(void) |
| 205 | { |
| 206 | efi_status_t status; |
| 207 | efi_time_t eft; |
| 208 | efi_time_cap_t cap; |
| 209 | |
| 210 | status = efi.get_time(&eft, &cap); |
| 211 | if (status != EFI_SUCCESS) |
| 212 | printk(KERN_ERR "Oops: efitime: can't read time!\n"); |
| 213 | |
| 214 | return mktime(eft.year, eft.month, eft.day, eft.hour, |
| 215 | eft.minute, eft.second); |
| 216 | } |
| 217 | |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 218 | /* |
| 219 | * Tell the kernel about the EFI memory map. This might include |
| 220 | * more than the max 128 entries that can fit in the e820 legacy |
| 221 | * (zeropage) memory map. |
| 222 | */ |
| 223 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 224 | static void __init do_add_efi_memmap(void) |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 225 | { |
| 226 | void *p; |
| 227 | |
| 228 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 229 | efi_memory_desc_t *md = p; |
| 230 | unsigned long long start = md->phys_addr; |
| 231 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 232 | int e820_type; |
| 233 | |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 234 | switch (md->type) { |
| 235 | case EFI_LOADER_CODE: |
| 236 | case EFI_LOADER_DATA: |
| 237 | case EFI_BOOT_SERVICES_CODE: |
| 238 | case EFI_BOOT_SERVICES_DATA: |
| 239 | case EFI_CONVENTIONAL_MEMORY: |
| 240 | if (md->attribute & EFI_MEMORY_WB) |
| 241 | e820_type = E820_RAM; |
| 242 | else |
| 243 | e820_type = E820_RESERVED; |
| 244 | break; |
| 245 | case EFI_ACPI_RECLAIM_MEMORY: |
| 246 | e820_type = E820_ACPI; |
| 247 | break; |
| 248 | case EFI_ACPI_MEMORY_NVS: |
| 249 | e820_type = E820_NVS; |
| 250 | break; |
| 251 | case EFI_UNUSABLE_MEMORY: |
| 252 | e820_type = E820_UNUSABLE; |
| 253 | break; |
| 254 | default: |
| 255 | /* |
| 256 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE |
| 257 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO |
| 258 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE |
| 259 | */ |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 260 | e820_type = E820_RESERVED; |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 261 | break; |
| 262 | } |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 263 | e820_add_region(start, size, e820_type); |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 264 | } |
| 265 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 266 | } |
| 267 | |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 268 | void __init efi_memblock_x86_reserve_range(void) |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 269 | { |
| 270 | unsigned long pmap; |
| 271 | |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 272 | #ifdef CONFIG_X86_32 |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 273 | pmap = boot_params.efi_info.efi_memmap; |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 274 | #else |
| 275 | pmap = (boot_params.efi_info.efi_memmap | |
| 276 | ((__u64)boot_params.efi_info.efi_memmap_hi<<32)); |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 277 | #endif |
| 278 | memmap.phys_map = (void *)pmap; |
| 279 | memmap.nr_map = boot_params.efi_info.efi_memmap_size / |
| 280 | boot_params.efi_info.efi_memdesc_size; |
| 281 | memmap.desc_version = boot_params.efi_info.efi_memdesc_version; |
| 282 | memmap.desc_size = boot_params.efi_info.efi_memdesc_size; |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 283 | memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size, |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 284 | "EFI memmap"); |
| 285 | } |
| 286 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 287 | #if EFI_DEBUG |
| 288 | static void __init print_efi_memmap(void) |
| 289 | { |
| 290 | efi_memory_desc_t *md; |
| 291 | void *p; |
| 292 | int i; |
| 293 | |
| 294 | for (p = memmap.map, i = 0; |
| 295 | p < memmap.map_end; |
| 296 | p += memmap.desc_size, i++) { |
| 297 | md = p; |
| 298 | printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, " |
| 299 | "range=[0x%016llx-0x%016llx) (%lluMB)\n", |
| 300 | i, md->type, md->attribute, md->phys_addr, |
| 301 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), |
| 302 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); |
| 303 | } |
| 304 | } |
| 305 | #endif /* EFI_DEBUG */ |
| 306 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 307 | void __init efi_reserve_boot_services(void) |
| 308 | { |
| 309 | void *p; |
| 310 | |
| 311 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 312 | efi_memory_desc_t *md = p; |
| 313 | unsigned long long start = md->phys_addr; |
| 314 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 315 | |
| 316 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 317 | md->type != EFI_BOOT_SERVICES_DATA) |
| 318 | continue; |
| 319 | |
| 320 | memblock_x86_reserve_range(start, start + size, "EFI Boot"); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static void __init efi_free_boot_services(void) |
| 325 | { |
| 326 | void *p; |
| 327 | |
| 328 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 329 | efi_memory_desc_t *md = p; |
| 330 | unsigned long long start = md->phys_addr; |
| 331 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 332 | |
| 333 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 334 | md->type != EFI_BOOT_SERVICES_DATA) |
| 335 | continue; |
| 336 | |
| 337 | free_bootmem_late(start, size); |
| 338 | } |
| 339 | } |
| 340 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 341 | void __init efi_init(void) |
| 342 | { |
| 343 | efi_config_table_t *config_tables; |
| 344 | efi_runtime_services_t *runtime; |
| 345 | efi_char16_t *c16; |
| 346 | char vendor[100] = "unknown"; |
| 347 | int i = 0; |
| 348 | void *tmp; |
| 349 | |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 350 | #ifdef CONFIG_X86_32 |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 351 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 352 | #else |
| 353 | efi_phys.systab = (efi_system_table_t *) |
| 354 | (boot_params.efi_info.efi_systab | |
| 355 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 356 | #endif |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 357 | |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 358 | efi.systab = early_ioremap((unsigned long)efi_phys.systab, |
| 359 | sizeof(efi_system_table_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 360 | if (efi.systab == NULL) |
| 361 | printk(KERN_ERR "Couldn't map the EFI system table!\n"); |
| 362 | memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t)); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 363 | early_iounmap(efi.systab, sizeof(efi_system_table_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 364 | efi.systab = &efi_systab; |
| 365 | |
| 366 | /* |
| 367 | * Verify the EFI Table |
| 368 | */ |
| 369 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 370 | printk(KERN_ERR "EFI system table signature incorrect!\n"); |
| 371 | if ((efi.systab->hdr.revision >> 16) == 0) |
| 372 | printk(KERN_ERR "Warning: EFI system table version " |
| 373 | "%d.%02d, expected 1.00 or greater!\n", |
| 374 | efi.systab->hdr.revision >> 16, |
| 375 | efi.systab->hdr.revision & 0xffff); |
| 376 | |
| 377 | /* |
| 378 | * Show what we know for posterity |
| 379 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 380 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 381 | if (c16) { |
Roel Kluin | fdb8a42 | 2009-08-06 15:58:13 -0700 | [diff] [blame] | 382 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 383 | vendor[i] = *c16++; |
| 384 | vendor[i] = '\0'; |
| 385 | } else |
| 386 | printk(KERN_ERR PFX "Could not map the firmware vendor!\n"); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 387 | early_iounmap(tmp, 2); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 388 | |
Frans Pop | 3235dc3 | 2010-02-06 18:47:17 +0100 | [diff] [blame] | 389 | printk(KERN_INFO "EFI v%u.%.02u by %s\n", |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 390 | efi.systab->hdr.revision >> 16, |
| 391 | efi.systab->hdr.revision & 0xffff, vendor); |
| 392 | |
| 393 | /* |
| 394 | * Let's see what config tables the firmware passed to us. |
| 395 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 396 | config_tables = early_ioremap( |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 397 | efi.systab->tables, |
| 398 | efi.systab->nr_tables * sizeof(efi_config_table_t)); |
| 399 | if (config_tables == NULL) |
| 400 | printk(KERN_ERR "Could not map EFI Configuration Table!\n"); |
| 401 | |
| 402 | printk(KERN_INFO); |
| 403 | for (i = 0; i < efi.systab->nr_tables; i++) { |
| 404 | if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) { |
| 405 | efi.mps = config_tables[i].table; |
| 406 | printk(" MPS=0x%lx ", config_tables[i].table); |
| 407 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 408 | ACPI_20_TABLE_GUID)) { |
| 409 | efi.acpi20 = config_tables[i].table; |
| 410 | printk(" ACPI 2.0=0x%lx ", config_tables[i].table); |
| 411 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 412 | ACPI_TABLE_GUID)) { |
| 413 | efi.acpi = config_tables[i].table; |
| 414 | printk(" ACPI=0x%lx ", config_tables[i].table); |
| 415 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 416 | SMBIOS_TABLE_GUID)) { |
| 417 | efi.smbios = config_tables[i].table; |
| 418 | printk(" SMBIOS=0x%lx ", config_tables[i].table); |
Nick Piggin | 03b4863 | 2009-01-20 04:36:04 +0100 | [diff] [blame] | 419 | #ifdef CONFIG_X86_UV |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 420 | } else if (!efi_guidcmp(config_tables[i].guid, |
Russ Anderson | a50f70b | 2008-10-03 11:58:54 -0500 | [diff] [blame] | 421 | UV_SYSTEM_TABLE_GUID)) { |
| 422 | efi.uv_systab = config_tables[i].table; |
| 423 | printk(" UVsystab=0x%lx ", config_tables[i].table); |
Nick Piggin | 03b4863 | 2009-01-20 04:36:04 +0100 | [diff] [blame] | 424 | #endif |
Russ Anderson | a50f70b | 2008-10-03 11:58:54 -0500 | [diff] [blame] | 425 | } else if (!efi_guidcmp(config_tables[i].guid, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 426 | HCDP_TABLE_GUID)) { |
| 427 | efi.hcdp = config_tables[i].table; |
| 428 | printk(" HCDP=0x%lx ", config_tables[i].table); |
| 429 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 430 | UGA_IO_PROTOCOL_GUID)) { |
| 431 | efi.uga = config_tables[i].table; |
| 432 | printk(" UGA=0x%lx ", config_tables[i].table); |
| 433 | } |
| 434 | } |
| 435 | printk("\n"); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 436 | early_iounmap(config_tables, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 437 | efi.systab->nr_tables * sizeof(efi_config_table_t)); |
| 438 | |
| 439 | /* |
| 440 | * Check out the runtime services table. We need to map |
| 441 | * the runtime services table so that we can grab the physical |
| 442 | * address of several of the EFI runtime functions, needed to |
| 443 | * set the firmware into virtual mode. |
| 444 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 445 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
| 446 | sizeof(efi_runtime_services_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 447 | if (runtime != NULL) { |
| 448 | /* |
| 449 | * We will only need *early* access to the following |
| 450 | * two EFI runtime services before set_virtual_address_map |
| 451 | * is invoked. |
| 452 | */ |
| 453 | efi_phys.get_time = (efi_get_time_t *)runtime->get_time; |
| 454 | efi_phys.set_virtual_address_map = |
| 455 | (efi_set_virtual_address_map_t *) |
| 456 | runtime->set_virtual_address_map; |
| 457 | /* |
| 458 | * Make efi_get_time can be called before entering |
| 459 | * virtual mode. |
| 460 | */ |
| 461 | efi.get_time = phys_efi_get_time; |
| 462 | } else |
| 463 | printk(KERN_ERR "Could not map the EFI runtime service " |
| 464 | "table!\n"); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 465 | early_iounmap(runtime, sizeof(efi_runtime_services_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 466 | |
| 467 | /* Map the EFI memory map */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 468 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
| 469 | memmap.nr_map * memmap.desc_size); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 470 | if (memmap.map == NULL) |
| 471 | printk(KERN_ERR "Could not map the EFI memory map!\n"); |
| 472 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); |
Russ Anderson | 175e438 | 2008-10-02 17:32:06 -0500 | [diff] [blame] | 473 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 474 | if (memmap.desc_size != sizeof(efi_memory_desc_t)) |
Russ Anderson | 175e438 | 2008-10-02 17:32:06 -0500 | [diff] [blame] | 475 | printk(KERN_WARNING |
| 476 | "Kernel-defined memdesc doesn't match the one from EFI!\n"); |
| 477 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 478 | if (add_efi_memmap) |
| 479 | do_add_efi_memmap(); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 480 | |
Feng Tang | 772be89 | 2009-10-20 12:54:02 +0800 | [diff] [blame] | 481 | #ifdef CONFIG_X86_32 |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 482 | x86_platform.get_wallclock = efi_get_time; |
| 483 | x86_platform.set_wallclock = efi_set_rtc_mmss; |
Feng Tang | 772be89 | 2009-10-20 12:54:02 +0800 | [diff] [blame] | 484 | #endif |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 485 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 486 | /* Setup for EFI runtime service */ |
| 487 | reboot_type = BOOT_EFI; |
| 488 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 489 | #if EFI_DEBUG |
| 490 | print_efi_memmap(); |
| 491 | #endif |
| 492 | } |
| 493 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 494 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
| 495 | { |
| 496 | u64 addr, npages; |
| 497 | |
| 498 | addr = md->virt_addr; |
| 499 | npages = md->num_pages; |
| 500 | |
| 501 | memrange_efi_to_native(&addr, &npages); |
| 502 | |
| 503 | if (executable) |
| 504 | set_memory_x(addr, npages); |
| 505 | else |
| 506 | set_memory_nx(addr, npages); |
| 507 | } |
| 508 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 509 | static void __init runtime_code_page_mkexec(void) |
| 510 | { |
| 511 | efi_memory_desc_t *md; |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 512 | void *p; |
| 513 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 514 | /* Make EFI runtime service code area executable */ |
| 515 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 516 | md = p; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 517 | |
| 518 | if (md->type != EFI_RUNTIME_SERVICES_CODE) |
| 519 | continue; |
| 520 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 521 | efi_set_executable(md, true); |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 522 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 523 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 524 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 525 | /* |
| 526 | * This function will switch the EFI runtime services to virtual mode. |
| 527 | * Essentially, look through the EFI memmap and map every region that |
| 528 | * has the runtime attribute bit set in its memory descriptor and update |
| 529 | * that memory descriptor with the virtual address obtained from ioremap(). |
| 530 | * This enables the runtime services to be called without having to |
| 531 | * thunk back into physical mode for every invocation. |
| 532 | */ |
| 533 | void __init efi_enter_virtual_mode(void) |
| 534 | { |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 535 | efi_memory_desc_t *md, *prev_md = NULL; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 536 | efi_status_t status; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 537 | unsigned long size; |
Huang Ying | dd39ecf | 2009-03-04 10:58:33 +0800 | [diff] [blame] | 538 | u64 end, systab, addr, npages, end_pfn; |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 539 | void *p, *va, *new_memmap = NULL; |
| 540 | int count = 0; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 541 | |
| 542 | efi.systab = NULL; |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 543 | |
| 544 | /* Merge contiguous regions of the same type and attribute */ |
| 545 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 546 | u64 prev_size; |
| 547 | md = p; |
| 548 | |
| 549 | if (!prev_md) { |
| 550 | prev_md = md; |
| 551 | continue; |
| 552 | } |
| 553 | |
| 554 | if (prev_md->type != md->type || |
| 555 | prev_md->attribute != md->attribute) { |
| 556 | prev_md = md; |
| 557 | continue; |
| 558 | } |
| 559 | |
| 560 | prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; |
| 561 | |
| 562 | if (md->phys_addr == (prev_md->phys_addr + prev_size)) { |
| 563 | prev_md->num_pages += md->num_pages; |
| 564 | md->type = EFI_RESERVED_TYPE; |
| 565 | md->attribute = 0; |
| 566 | continue; |
| 567 | } |
| 568 | prev_md = md; |
| 569 | } |
| 570 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 571 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 572 | md = p; |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 573 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
| 574 | md->type != EFI_BOOT_SERVICES_CODE && |
| 575 | md->type != EFI_BOOT_SERVICES_DATA) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 576 | continue; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 577 | |
| 578 | size = md->num_pages << EFI_PAGE_SHIFT; |
| 579 | end = md->phys_addr + size; |
| 580 | |
Huang Ying | dd39ecf | 2009-03-04 10:58:33 +0800 | [diff] [blame] | 581 | end_pfn = PFN_UP(end); |
| 582 | if (end_pfn <= max_low_pfn_mapped |
| 583 | || (end_pfn > (1UL << (32 - PAGE_SHIFT)) |
| 584 | && end_pfn <= max_pfn_mapped)) |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 585 | va = __va(md->phys_addr); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 586 | else |
Paul Mackerras | 6a7bbd5 | 2009-08-03 22:38:10 +1000 | [diff] [blame] | 587 | va = efi_ioremap(md->phys_addr, size, md->type); |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 588 | |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 589 | md->virt_addr = (u64) (unsigned long) va; |
| 590 | |
| 591 | if (!va) { |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 592 | printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n", |
| 593 | (unsigned long long)md->phys_addr); |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 594 | continue; |
| 595 | } |
| 596 | |
Huang, Ying | 4a3575f | 2008-02-25 15:18:37 +0800 | [diff] [blame] | 597 | if (!(md->attribute & EFI_MEMORY_WB)) { |
| 598 | addr = md->virt_addr; |
| 599 | npages = md->num_pages; |
| 600 | memrange_efi_to_native(&addr, &npages); |
| 601 | set_memory_uc(addr, npages); |
| 602 | } |
Thomas Gleixner | e85f205 | 2008-02-12 19:46:48 +0100 | [diff] [blame] | 603 | |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 604 | systab = (u64) (unsigned long) efi_phys.systab; |
| 605 | if (md->phys_addr <= systab && systab < end) { |
| 606 | systab += md->virt_addr - md->phys_addr; |
| 607 | efi.systab = (efi_system_table_t *) (unsigned long) systab; |
| 608 | } |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 609 | new_memmap = krealloc(new_memmap, |
| 610 | (count + 1) * memmap.desc_size, |
| 611 | GFP_KERNEL); |
| 612 | memcpy(new_memmap + (count * memmap.desc_size), md, |
| 613 | memmap.desc_size); |
| 614 | count++; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | BUG_ON(!efi.systab); |
| 618 | |
| 619 | status = phys_efi_set_virtual_address_map( |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 620 | memmap.desc_size * count, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 621 | memmap.desc_size, |
| 622 | memmap.desc_version, |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 623 | (efi_memory_desc_t *)__pa(new_memmap)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 624 | |
| 625 | if (status != EFI_SUCCESS) { |
| 626 | printk(KERN_ALERT "Unable to switch EFI into virtual mode " |
| 627 | "(status=%lx)!\n", status); |
| 628 | panic("EFI call to SetVirtualAddressMap() failed!"); |
| 629 | } |
| 630 | |
| 631 | /* |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 632 | * Thankfully, it does seem that no runtime services other than |
| 633 | * SetVirtualAddressMap() will touch boot services code, so we can |
| 634 | * get rid of it all at this point |
| 635 | */ |
| 636 | efi_free_boot_services(); |
| 637 | |
| 638 | /* |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 639 | * Now that EFI is in virtual mode, update the function |
| 640 | * pointers in the runtime service table to the new virtual addresses. |
| 641 | * |
| 642 | * Call EFI services through wrapper functions. |
| 643 | */ |
| 644 | efi.get_time = virt_efi_get_time; |
| 645 | efi.set_time = virt_efi_set_time; |
| 646 | efi.get_wakeup_time = virt_efi_get_wakeup_time; |
| 647 | efi.set_wakeup_time = virt_efi_set_wakeup_time; |
| 648 | efi.get_variable = virt_efi_get_variable; |
| 649 | efi.get_next_variable = virt_efi_get_next_variable; |
| 650 | efi.set_variable = virt_efi_set_variable; |
| 651 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; |
| 652 | efi.reset_system = virt_efi_reset_system; |
Matthew Garrett | 2b5e8ef | 2011-05-05 15:19:42 -0400 | [diff] [blame] | 653 | efi.set_virtual_address_map = NULL; |
Huang, Ying | 4de0d4a | 2008-02-13 17:22:41 +0800 | [diff] [blame] | 654 | if (__supported_pte_mask & _PAGE_NX) |
| 655 | runtime_code_page_mkexec(); |
Huang, Ying | a382806 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 656 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); |
| 657 | memmap.map = NULL; |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 658 | kfree(new_memmap); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Convenience functions to obtain memory types and attributes |
| 663 | */ |
| 664 | u32 efi_mem_type(unsigned long phys_addr) |
| 665 | { |
| 666 | efi_memory_desc_t *md; |
| 667 | void *p; |
| 668 | |
| 669 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 670 | md = p; |
| 671 | if ((md->phys_addr <= phys_addr) && |
| 672 | (phys_addr < (md->phys_addr + |
| 673 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 674 | return md->type; |
| 675 | } |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | u64 efi_mem_attributes(unsigned long phys_addr) |
| 680 | { |
| 681 | efi_memory_desc_t *md; |
| 682 | void *p; |
| 683 | |
| 684 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 685 | md = p; |
| 686 | if ((md->phys_addr <= phys_addr) && |
| 687 | (phys_addr < (md->phys_addr + |
| 688 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 689 | return md->attribute; |
| 690 | } |
| 691 | return 0; |
| 692 | } |