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 | |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 31 | #include <linux/kernel.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/efi.h> |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 34 | #include <linux/efi-bgrt.h> |
Paul Gortmaker | 69c60c8 | 2011-05-26 12:22:53 -0400 | [diff] [blame] | 35 | #include <linux/export.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 36 | #include <linux/bootmem.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 37 | #include <linux/memblock.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 38 | #include <linux/spinlock.h> |
| 39 | #include <linux/uaccess.h> |
| 40 | #include <linux/time.h> |
| 41 | #include <linux/io.h> |
| 42 | #include <linux/reboot.h> |
| 43 | #include <linux/bcd.h> |
| 44 | |
| 45 | #include <asm/setup.h> |
| 46 | #include <asm/efi.h> |
| 47 | #include <asm/time.h> |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 48 | #include <asm/cacheflush.h> |
| 49 | #include <asm/tlbflush.h> |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 50 | #include <asm/x86_init.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 51 | |
| 52 | #define EFI_DEBUG 1 |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 53 | |
| 54 | int efi_enabled; |
| 55 | EXPORT_SYMBOL(efi_enabled); |
| 56 | |
Jan Beulich | d80603c | 2011-07-05 12:22:18 +0100 | [diff] [blame] | 57 | struct efi __read_mostly efi = { |
| 58 | .mps = EFI_INVALID_TABLE_ADDR, |
| 59 | .acpi = EFI_INVALID_TABLE_ADDR, |
| 60 | .acpi20 = EFI_INVALID_TABLE_ADDR, |
| 61 | .smbios = EFI_INVALID_TABLE_ADDR, |
| 62 | .sal_systab = EFI_INVALID_TABLE_ADDR, |
| 63 | .boot_info = EFI_INVALID_TABLE_ADDR, |
| 64 | .hcdp = EFI_INVALID_TABLE_ADDR, |
| 65 | .uga = EFI_INVALID_TABLE_ADDR, |
| 66 | .uv_systab = EFI_INVALID_TABLE_ADDR, |
| 67 | }; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 68 | EXPORT_SYMBOL(efi); |
| 69 | |
| 70 | struct efi_memory_map memmap; |
| 71 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 72 | bool efi_64bit; |
| 73 | static bool efi_native; |
| 74 | |
Harvey Harrison | ecaea42 | 2008-02-13 13:26:13 -0800 | [diff] [blame] | 75 | static struct efi efi_phys __initdata; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 76 | static efi_system_table_t efi_systab __initdata; |
| 77 | |
Huang, Ying | 8b2cb7a | 2008-01-30 13:32:11 +0100 | [diff] [blame] | 78 | static int __init setup_noefi(char *arg) |
| 79 | { |
| 80 | efi_enabled = 0; |
| 81 | return 0; |
| 82 | } |
| 83 | early_param("noefi", setup_noefi); |
| 84 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 85 | int add_efi_memmap; |
| 86 | EXPORT_SYMBOL(add_efi_memmap); |
| 87 | |
| 88 | static int __init setup_add_efi_memmap(char *arg) |
| 89 | { |
| 90 | add_efi_memmap = 1; |
| 91 | return 0; |
| 92 | } |
| 93 | early_param("add_efi_memmap", setup_add_efi_memmap); |
| 94 | |
| 95 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 96 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
| 97 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 98 | unsigned long flags; |
| 99 | efi_status_t status; |
| 100 | |
| 101 | spin_lock_irqsave(&rtc_lock, flags); |
| 102 | status = efi_call_virt2(get_time, tm, tc); |
| 103 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 104 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static efi_status_t virt_efi_set_time(efi_time_t *tm) |
| 108 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 109 | unsigned long flags; |
| 110 | efi_status_t status; |
| 111 | |
| 112 | spin_lock_irqsave(&rtc_lock, flags); |
| 113 | status = efi_call_virt1(set_time, tm); |
| 114 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 115 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, |
| 119 | efi_bool_t *pending, |
| 120 | efi_time_t *tm) |
| 121 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 122 | unsigned long flags; |
| 123 | efi_status_t status; |
| 124 | |
| 125 | spin_lock_irqsave(&rtc_lock, flags); |
| 126 | status = efi_call_virt3(get_wakeup_time, |
| 127 | enabled, pending, tm); |
| 128 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 129 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) |
| 133 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 134 | unsigned long flags; |
| 135 | efi_status_t status; |
| 136 | |
| 137 | spin_lock_irqsave(&rtc_lock, flags); |
| 138 | status = efi_call_virt2(set_wakeup_time, |
| 139 | enabled, tm); |
| 140 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 141 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, |
| 145 | efi_guid_t *vendor, |
| 146 | u32 *attr, |
| 147 | unsigned long *data_size, |
| 148 | void *data) |
| 149 | { |
| 150 | return efi_call_virt5(get_variable, |
| 151 | name, vendor, attr, |
| 152 | data_size, data); |
| 153 | } |
| 154 | |
| 155 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, |
| 156 | efi_char16_t *name, |
| 157 | efi_guid_t *vendor) |
| 158 | { |
| 159 | return efi_call_virt3(get_next_variable, |
| 160 | name_size, name, vendor); |
| 161 | } |
| 162 | |
| 163 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, |
| 164 | efi_guid_t *vendor, |
Matthew Garrett | f7a2d73 | 2011-06-06 15:36:24 -0400 | [diff] [blame] | 165 | u32 attr, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 166 | unsigned long data_size, |
| 167 | void *data) |
| 168 | { |
| 169 | return efi_call_virt5(set_variable, |
| 170 | name, vendor, attr, |
| 171 | data_size, data); |
| 172 | } |
| 173 | |
Matthew Garrett | 3b37023 | 2011-06-06 15:36:25 -0400 | [diff] [blame] | 174 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
| 175 | u64 *storage_space, |
| 176 | u64 *remaining_space, |
| 177 | u64 *max_variable_size) |
| 178 | { |
| 179 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 180 | return EFI_UNSUPPORTED; |
| 181 | |
| 182 | return efi_call_virt4(query_variable_info, attr, storage_space, |
| 183 | remaining_space, max_variable_size); |
| 184 | } |
| 185 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 186 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
| 187 | { |
| 188 | return efi_call_virt1(get_next_high_mono_count, count); |
| 189 | } |
| 190 | |
| 191 | static void virt_efi_reset_system(int reset_type, |
| 192 | efi_status_t status, |
| 193 | unsigned long data_size, |
| 194 | efi_char16_t *data) |
| 195 | { |
| 196 | efi_call_virt4(reset_system, reset_type, status, |
| 197 | data_size, data); |
| 198 | } |
| 199 | |
Matthew Garrett | 3b37023 | 2011-06-06 15:36:25 -0400 | [diff] [blame] | 200 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, |
| 201 | unsigned long count, |
| 202 | unsigned long sg_list) |
| 203 | { |
| 204 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 205 | return EFI_UNSUPPORTED; |
| 206 | |
| 207 | return efi_call_virt3(update_capsule, capsules, count, sg_list); |
| 208 | } |
| 209 | |
| 210 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, |
| 211 | unsigned long count, |
| 212 | u64 *max_size, |
| 213 | int *reset_type) |
| 214 | { |
| 215 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 216 | return EFI_UNSUPPORTED; |
| 217 | |
| 218 | return efi_call_virt4(query_capsule_caps, capsules, count, max_size, |
| 219 | reset_type); |
| 220 | } |
| 221 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 222 | static efi_status_t __init phys_efi_set_virtual_address_map( |
| 223 | unsigned long memory_map_size, |
| 224 | unsigned long descriptor_size, |
| 225 | u32 descriptor_version, |
| 226 | efi_memory_desc_t *virtual_map) |
| 227 | { |
| 228 | efi_status_t status; |
| 229 | |
| 230 | efi_call_phys_prelog(); |
| 231 | status = efi_call_phys4(efi_phys.set_virtual_address_map, |
| 232 | memory_map_size, descriptor_size, |
| 233 | descriptor_version, virtual_map); |
| 234 | efi_call_phys_epilog(); |
| 235 | return status; |
| 236 | } |
| 237 | |
H. Peter Anvin | f026cfa | 2012-08-14 09:53:38 -0700 | [diff] [blame] | 238 | static efi_status_t __init phys_efi_get_time(efi_time_t *tm, |
| 239 | efi_time_cap_t *tc) |
| 240 | { |
| 241 | unsigned long flags; |
| 242 | efi_status_t status; |
| 243 | |
| 244 | spin_lock_irqsave(&rtc_lock, flags); |
| 245 | efi_call_phys_prelog(); |
| 246 | status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm), |
| 247 | virt_to_phys(tc)); |
| 248 | efi_call_phys_epilog(); |
| 249 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 250 | return status; |
| 251 | } |
| 252 | |
| 253 | int efi_set_rtc_mmss(unsigned long nowtime) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 254 | { |
| 255 | int real_seconds, real_minutes; |
| 256 | efi_status_t status; |
| 257 | efi_time_t eft; |
| 258 | efi_time_cap_t cap; |
| 259 | |
| 260 | status = efi.get_time(&eft, &cap); |
| 261 | if (status != EFI_SUCCESS) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 262 | pr_err("Oops: efitime: can't read time!\n"); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | real_seconds = nowtime % 60; |
| 267 | real_minutes = nowtime / 60; |
| 268 | if (((abs(real_minutes - eft.minute) + 15)/30) & 1) |
| 269 | real_minutes += 30; |
| 270 | real_minutes %= 60; |
| 271 | eft.minute = real_minutes; |
| 272 | eft.second = real_seconds; |
| 273 | |
| 274 | status = efi.set_time(&eft); |
| 275 | if (status != EFI_SUCCESS) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 276 | pr_err("Oops: efitime: can't write time!\n"); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 277 | return -1; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
H. Peter Anvin | f026cfa | 2012-08-14 09:53:38 -0700 | [diff] [blame] | 282 | unsigned long efi_get_time(void) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 283 | { |
| 284 | efi_status_t status; |
| 285 | efi_time_t eft; |
| 286 | efi_time_cap_t cap; |
| 287 | |
| 288 | status = efi.get_time(&eft, &cap); |
| 289 | if (status != EFI_SUCCESS) |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 290 | pr_err("Oops: efitime: can't read time!\n"); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 291 | |
| 292 | return mktime(eft.year, eft.month, eft.day, eft.hour, |
| 293 | eft.minute, eft.second); |
| 294 | } |
| 295 | |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 296 | /* |
| 297 | * Tell the kernel about the EFI memory map. This might include |
| 298 | * more than the max 128 entries that can fit in the e820 legacy |
| 299 | * (zeropage) memory map. |
| 300 | */ |
| 301 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 302 | static void __init do_add_efi_memmap(void) |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 303 | { |
| 304 | void *p; |
| 305 | |
| 306 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 307 | efi_memory_desc_t *md = p; |
| 308 | unsigned long long start = md->phys_addr; |
| 309 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 310 | int e820_type; |
| 311 | |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 312 | switch (md->type) { |
| 313 | case EFI_LOADER_CODE: |
| 314 | case EFI_LOADER_DATA: |
| 315 | case EFI_BOOT_SERVICES_CODE: |
| 316 | case EFI_BOOT_SERVICES_DATA: |
| 317 | case EFI_CONVENTIONAL_MEMORY: |
| 318 | if (md->attribute & EFI_MEMORY_WB) |
| 319 | e820_type = E820_RAM; |
| 320 | else |
| 321 | e820_type = E820_RESERVED; |
| 322 | break; |
| 323 | case EFI_ACPI_RECLAIM_MEMORY: |
| 324 | e820_type = E820_ACPI; |
| 325 | break; |
| 326 | case EFI_ACPI_MEMORY_NVS: |
| 327 | e820_type = E820_NVS; |
| 328 | break; |
| 329 | case EFI_UNUSABLE_MEMORY: |
| 330 | e820_type = E820_UNUSABLE; |
| 331 | break; |
| 332 | default: |
| 333 | /* |
| 334 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE |
| 335 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO |
| 336 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE |
| 337 | */ |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 338 | e820_type = E820_RESERVED; |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 339 | break; |
| 340 | } |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 341 | e820_add_region(start, size, e820_type); |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 342 | } |
| 343 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 344 | } |
| 345 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 346 | int __init efi_memblock_x86_reserve_range(void) |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 347 | { |
| 348 | unsigned long pmap; |
| 349 | |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 350 | #ifdef CONFIG_X86_32 |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 351 | /* Can't handle data above 4GB at this time */ |
| 352 | if (boot_params.efi_info.efi_memmap_hi) { |
| 353 | pr_err("Memory map is above 4GB, disabling EFI.\n"); |
| 354 | return -EINVAL; |
| 355 | } |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 356 | pmap = boot_params.efi_info.efi_memmap; |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 357 | #else |
| 358 | pmap = (boot_params.efi_info.efi_memmap | |
| 359 | ((__u64)boot_params.efi_info.efi_memmap_hi<<32)); |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 360 | #endif |
| 361 | memmap.phys_map = (void *)pmap; |
| 362 | memmap.nr_map = boot_params.efi_info.efi_memmap_size / |
| 363 | boot_params.efi_info.efi_memdesc_size; |
| 364 | memmap.desc_version = boot_params.efi_info.efi_memdesc_version; |
| 365 | memmap.desc_size = boot_params.efi_info.efi_memdesc_size; |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 366 | memblock_reserve(pmap, memmap.nr_map * memmap.desc_size); |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 367 | |
| 368 | return 0; |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 369 | } |
| 370 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 371 | #if EFI_DEBUG |
| 372 | static void __init print_efi_memmap(void) |
| 373 | { |
| 374 | efi_memory_desc_t *md; |
| 375 | void *p; |
| 376 | int i; |
| 377 | |
| 378 | for (p = memmap.map, i = 0; |
| 379 | p < memmap.map_end; |
| 380 | p += memmap.desc_size, i++) { |
| 381 | md = p; |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 382 | pr_info("mem%02u: type=%u, attr=0x%llx, " |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 383 | "range=[0x%016llx-0x%016llx) (%lluMB)\n", |
| 384 | i, md->type, md->attribute, md->phys_addr, |
| 385 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), |
| 386 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); |
| 387 | } |
| 388 | } |
| 389 | #endif /* EFI_DEBUG */ |
| 390 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 391 | void __init efi_reserve_boot_services(void) |
| 392 | { |
| 393 | void *p; |
| 394 | |
| 395 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 396 | efi_memory_desc_t *md = p; |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 397 | u64 start = md->phys_addr; |
| 398 | u64 size = md->num_pages << EFI_PAGE_SHIFT; |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 399 | |
| 400 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 401 | md->type != EFI_BOOT_SERVICES_DATA) |
| 402 | continue; |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 403 | /* Only reserve where possible: |
| 404 | * - Not within any already allocated areas |
| 405 | * - Not over any memory area (really needed, if above?) |
| 406 | * - Not within any part of the kernel |
| 407 | * - Not the bios reserved area |
| 408 | */ |
| 409 | if ((start+size >= virt_to_phys(_text) |
| 410 | && start <= virt_to_phys(_end)) || |
| 411 | !e820_all_mapped(start, start+size, E820_RAM) || |
Tejun Heo | bf61549 | 2011-07-12 09:58:05 +0200 | [diff] [blame] | 412 | memblock_is_region_reserved(start, size)) { |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 413 | /* Could not reserve, skip it */ |
| 414 | md->num_pages = 0; |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 415 | memblock_dbg("Could not reserve boot range " |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 416 | "[0x%010llx-0x%010llx]\n", |
| 417 | start, start+size-1); |
| 418 | } else |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 419 | memblock_reserve(start, size); |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 423 | static void __init efi_unmap_memmap(void) |
| 424 | { |
| 425 | if (memmap.map) { |
| 426 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); |
| 427 | memmap.map = NULL; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void __init efi_free_boot_services(void) |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 432 | { |
| 433 | void *p; |
| 434 | |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 435 | if (!efi_native) |
| 436 | return; |
| 437 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 438 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 439 | efi_memory_desc_t *md = p; |
| 440 | unsigned long long start = md->phys_addr; |
| 441 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 442 | |
| 443 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 444 | md->type != EFI_BOOT_SERVICES_DATA) |
| 445 | continue; |
| 446 | |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 447 | /* Could not reserve boot area */ |
| 448 | if (!size) |
| 449 | continue; |
| 450 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 451 | free_bootmem_late(start, size); |
| 452 | } |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 453 | |
| 454 | efi_unmap_memmap(); |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 457 | static int __init efi_systab_init(void *phys) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 458 | { |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 459 | if (efi_64bit) { |
| 460 | efi_system_table_64_t *systab64; |
| 461 | u64 tmp = 0; |
| 462 | |
| 463 | systab64 = early_ioremap((unsigned long)phys, |
| 464 | sizeof(*systab64)); |
| 465 | if (systab64 == NULL) { |
| 466 | pr_err("Couldn't map the system table!\n"); |
| 467 | return -ENOMEM; |
| 468 | } |
| 469 | |
| 470 | efi_systab.hdr = systab64->hdr; |
| 471 | efi_systab.fw_vendor = systab64->fw_vendor; |
| 472 | tmp |= systab64->fw_vendor; |
| 473 | efi_systab.fw_revision = systab64->fw_revision; |
| 474 | efi_systab.con_in_handle = systab64->con_in_handle; |
| 475 | tmp |= systab64->con_in_handle; |
| 476 | efi_systab.con_in = systab64->con_in; |
| 477 | tmp |= systab64->con_in; |
| 478 | efi_systab.con_out_handle = systab64->con_out_handle; |
| 479 | tmp |= systab64->con_out_handle; |
| 480 | efi_systab.con_out = systab64->con_out; |
| 481 | tmp |= systab64->con_out; |
| 482 | efi_systab.stderr_handle = systab64->stderr_handle; |
| 483 | tmp |= systab64->stderr_handle; |
| 484 | efi_systab.stderr = systab64->stderr; |
| 485 | tmp |= systab64->stderr; |
| 486 | efi_systab.runtime = (void *)(unsigned long)systab64->runtime; |
| 487 | tmp |= systab64->runtime; |
| 488 | efi_systab.boottime = (void *)(unsigned long)systab64->boottime; |
| 489 | tmp |= systab64->boottime; |
| 490 | efi_systab.nr_tables = systab64->nr_tables; |
| 491 | efi_systab.tables = systab64->tables; |
| 492 | tmp |= systab64->tables; |
| 493 | |
| 494 | early_iounmap(systab64, sizeof(*systab64)); |
| 495 | #ifdef CONFIG_X86_32 |
| 496 | if (tmp >> 32) { |
| 497 | pr_err("EFI data located above 4GB, disabling EFI.\n"); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | #endif |
| 501 | } else { |
| 502 | efi_system_table_32_t *systab32; |
| 503 | |
| 504 | systab32 = early_ioremap((unsigned long)phys, |
| 505 | sizeof(*systab32)); |
| 506 | if (systab32 == NULL) { |
| 507 | pr_err("Couldn't map the system table!\n"); |
| 508 | return -ENOMEM; |
| 509 | } |
| 510 | |
| 511 | efi_systab.hdr = systab32->hdr; |
| 512 | efi_systab.fw_vendor = systab32->fw_vendor; |
| 513 | efi_systab.fw_revision = systab32->fw_revision; |
| 514 | efi_systab.con_in_handle = systab32->con_in_handle; |
| 515 | efi_systab.con_in = systab32->con_in; |
| 516 | efi_systab.con_out_handle = systab32->con_out_handle; |
| 517 | efi_systab.con_out = systab32->con_out; |
| 518 | efi_systab.stderr_handle = systab32->stderr_handle; |
| 519 | efi_systab.stderr = systab32->stderr; |
| 520 | efi_systab.runtime = (void *)(unsigned long)systab32->runtime; |
| 521 | efi_systab.boottime = (void *)(unsigned long)systab32->boottime; |
| 522 | efi_systab.nr_tables = systab32->nr_tables; |
| 523 | efi_systab.tables = systab32->tables; |
| 524 | |
| 525 | early_iounmap(systab32, sizeof(*systab32)); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 526 | } |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 527 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 528 | efi.systab = &efi_systab; |
| 529 | |
| 530 | /* |
| 531 | * Verify the EFI Table |
| 532 | */ |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 533 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 534 | pr_err("System table signature incorrect!\n"); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 535 | return -EINVAL; |
| 536 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 537 | if ((efi.systab->hdr.revision >> 16) == 0) |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 538 | pr_err("Warning: System table version " |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 539 | "%d.%02d, expected 1.00 or greater!\n", |
| 540 | efi.systab->hdr.revision >> 16, |
| 541 | efi.systab->hdr.revision & 0xffff); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 542 | |
| 543 | return 0; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 544 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 545 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 546 | static int __init efi_config_init(u64 tables, int nr_tables) |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 547 | { |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 548 | void *config_tables, *tablep; |
| 549 | int i, sz; |
| 550 | |
| 551 | if (efi_64bit) |
| 552 | sz = sizeof(efi_config_table_64_t); |
| 553 | else |
| 554 | sz = sizeof(efi_config_table_32_t); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 555 | |
| 556 | /* |
| 557 | * Let's see what config tables the firmware passed to us. |
| 558 | */ |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 559 | config_tables = early_ioremap(tables, nr_tables * sz); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 560 | if (config_tables == NULL) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 561 | pr_err("Could not map Configuration table!\n"); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 562 | return -ENOMEM; |
| 563 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 564 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 565 | tablep = config_tables; |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 566 | pr_info(""); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 567 | for (i = 0; i < efi.systab->nr_tables; i++) { |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 568 | efi_guid_t guid; |
| 569 | unsigned long table; |
Olof Johansson | a6a46f4 | 2012-02-12 13:24:27 -0800 | [diff] [blame] | 570 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 571 | if (efi_64bit) { |
| 572 | u64 table64; |
| 573 | guid = ((efi_config_table_64_t *)tablep)->guid; |
| 574 | table64 = ((efi_config_table_64_t *)tablep)->table; |
| 575 | table = table64; |
| 576 | #ifdef CONFIG_X86_32 |
| 577 | if (table64 >> 32) { |
| 578 | pr_cont("\n"); |
| 579 | pr_err("Table located above 4GB, disabling EFI.\n"); |
| 580 | early_iounmap(config_tables, |
| 581 | efi.systab->nr_tables * sz); |
| 582 | return -EINVAL; |
| 583 | } |
| 584 | #endif |
| 585 | } else { |
| 586 | guid = ((efi_config_table_32_t *)tablep)->guid; |
| 587 | table = ((efi_config_table_32_t *)tablep)->table; |
| 588 | } |
Olof Johansson | a6a46f4 | 2012-02-12 13:24:27 -0800 | [diff] [blame] | 589 | if (!efi_guidcmp(guid, MPS_TABLE_GUID)) { |
| 590 | efi.mps = table; |
| 591 | pr_cont(" MPS=0x%lx ", table); |
| 592 | } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) { |
| 593 | efi.acpi20 = table; |
| 594 | pr_cont(" ACPI 2.0=0x%lx ", table); |
| 595 | } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) { |
| 596 | efi.acpi = table; |
| 597 | pr_cont(" ACPI=0x%lx ", table); |
| 598 | } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) { |
| 599 | efi.smbios = table; |
| 600 | pr_cont(" SMBIOS=0x%lx ", table); |
Nick Piggin | 03b4863 | 2009-01-20 04:36:04 +0100 | [diff] [blame] | 601 | #ifdef CONFIG_X86_UV |
Olof Johansson | a6a46f4 | 2012-02-12 13:24:27 -0800 | [diff] [blame] | 602 | } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) { |
| 603 | efi.uv_systab = table; |
| 604 | pr_cont(" UVsystab=0x%lx ", table); |
Nick Piggin | 03b4863 | 2009-01-20 04:36:04 +0100 | [diff] [blame] | 605 | #endif |
Olof Johansson | a6a46f4 | 2012-02-12 13:24:27 -0800 | [diff] [blame] | 606 | } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) { |
| 607 | efi.hcdp = table; |
| 608 | pr_cont(" HCDP=0x%lx ", table); |
| 609 | } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) { |
| 610 | efi.uga = table; |
| 611 | pr_cont(" UGA=0x%lx ", table); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 612 | } |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 613 | tablep += sz; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 614 | } |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 615 | pr_cont("\n"); |
Olof Johansson | a6a46f4 | 2012-02-12 13:24:27 -0800 | [diff] [blame] | 616 | early_iounmap(config_tables, efi.systab->nr_tables * sz); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 617 | return 0; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 620 | static int __init efi_runtime_init(void) |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 621 | { |
| 622 | efi_runtime_services_t *runtime; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * Check out the runtime services table. We need to map |
| 626 | * the runtime services table so that we can grab the physical |
| 627 | * address of several of the EFI runtime functions, needed to |
| 628 | * set the firmware into virtual mode. |
| 629 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 630 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
| 631 | sizeof(efi_runtime_services_t)); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 632 | if (!runtime) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 633 | pr_err("Could not map the runtime service table!\n"); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 634 | return -ENOMEM; |
| 635 | } |
| 636 | /* |
| 637 | * We will only need *early* access to the following |
H. Peter Anvin | f026cfa | 2012-08-14 09:53:38 -0700 | [diff] [blame] | 638 | * two EFI runtime services before set_virtual_address_map |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 639 | * is invoked. |
| 640 | */ |
H. Peter Anvin | f026cfa | 2012-08-14 09:53:38 -0700 | [diff] [blame] | 641 | efi_phys.get_time = (efi_get_time_t *)runtime->get_time; |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 642 | efi_phys.set_virtual_address_map = |
| 643 | (efi_set_virtual_address_map_t *) |
| 644 | runtime->set_virtual_address_map; |
H. Peter Anvin | f026cfa | 2012-08-14 09:53:38 -0700 | [diff] [blame] | 645 | /* |
| 646 | * Make efi_get_time can be called before entering |
| 647 | * virtual mode. |
| 648 | */ |
| 649 | efi.get_time = phys_efi_get_time; |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 650 | early_iounmap(runtime, sizeof(efi_runtime_services_t)); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 651 | |
| 652 | return 0; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 653 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 654 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 655 | static int __init efi_memmap_init(void) |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 656 | { |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 657 | /* Map the EFI memory map */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 658 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
| 659 | memmap.nr_map * memmap.desc_size); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 660 | if (memmap.map == NULL) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 661 | pr_err("Could not map the memory map!\n"); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 662 | return -ENOMEM; |
| 663 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 664 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); |
Russ Anderson | 175e438 | 2008-10-02 17:32:06 -0500 | [diff] [blame] | 665 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 666 | if (add_efi_memmap) |
| 667 | do_add_efi_memmap(); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 668 | |
| 669 | return 0; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void __init efi_init(void) |
| 673 | { |
| 674 | efi_char16_t *c16; |
| 675 | char vendor[100] = "unknown"; |
| 676 | int i = 0; |
| 677 | void *tmp; |
| 678 | |
| 679 | #ifdef CONFIG_X86_32 |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 680 | if (boot_params.efi_info.efi_systab_hi || |
| 681 | boot_params.efi_info.efi_memmap_hi) { |
| 682 | pr_info("Table located above 4GB, disabling EFI.\n"); |
| 683 | efi_enabled = 0; |
| 684 | return; |
| 685 | } |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 686 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 687 | efi_native = !efi_64bit; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 688 | #else |
| 689 | efi_phys.systab = (efi_system_table_t *) |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 690 | (boot_params.efi_info.efi_systab | |
| 691 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); |
| 692 | efi_native = efi_64bit; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 693 | #endif |
| 694 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 695 | if (efi_systab_init(efi_phys.systab)) { |
| 696 | efi_enabled = 0; |
| 697 | return; |
| 698 | } |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 699 | |
| 700 | /* |
| 701 | * Show what we know for posterity |
| 702 | */ |
| 703 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); |
| 704 | if (c16) { |
| 705 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) |
| 706 | vendor[i] = *c16++; |
| 707 | vendor[i] = '\0'; |
| 708 | } else |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 709 | pr_err("Could not map the firmware vendor!\n"); |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 710 | early_iounmap(tmp, 2); |
| 711 | |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 712 | pr_info("EFI v%u.%.02u by %s\n", |
| 713 | efi.systab->hdr.revision >> 16, |
| 714 | efi.systab->hdr.revision & 0xffff, vendor); |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 715 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 716 | if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) { |
| 717 | efi_enabled = 0; |
| 718 | return; |
| 719 | } |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 720 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 721 | /* |
| 722 | * Note: We currently don't support runtime services on an EFI |
| 723 | * that doesn't match the kernel 32/64-bit mode. |
| 724 | */ |
| 725 | |
| 726 | if (!efi_native) |
| 727 | pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n"); |
| 728 | else if (efi_runtime_init()) { |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 729 | efi_enabled = 0; |
| 730 | return; |
| 731 | } |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 732 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 733 | if (efi_memmap_init()) { |
| 734 | efi_enabled = 0; |
| 735 | return; |
| 736 | } |
H. Peter Anvin | f026cfa | 2012-08-14 09:53:38 -0700 | [diff] [blame] | 737 | #ifdef CONFIG_X86_32 |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 738 | if (efi_native) { |
| 739 | x86_platform.get_wallclock = efi_get_time; |
| 740 | x86_platform.set_wallclock = efi_set_rtc_mmss; |
| 741 | } |
H. Peter Anvin | f026cfa | 2012-08-14 09:53:38 -0700 | [diff] [blame] | 742 | #endif |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 743 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 744 | #if EFI_DEBUG |
| 745 | print_efi_memmap(); |
| 746 | #endif |
| 747 | } |
| 748 | |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 749 | void __init efi_late_init(void) |
| 750 | { |
| 751 | efi_bgrt_init(); |
| 752 | } |
| 753 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 754 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
| 755 | { |
| 756 | u64 addr, npages; |
| 757 | |
| 758 | addr = md->virt_addr; |
| 759 | npages = md->num_pages; |
| 760 | |
| 761 | memrange_efi_to_native(&addr, &npages); |
| 762 | |
| 763 | if (executable) |
| 764 | set_memory_x(addr, npages); |
| 765 | else |
| 766 | set_memory_nx(addr, npages); |
| 767 | } |
| 768 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 769 | static void __init runtime_code_page_mkexec(void) |
| 770 | { |
| 771 | efi_memory_desc_t *md; |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 772 | void *p; |
| 773 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 774 | /* Make EFI runtime service code area executable */ |
| 775 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 776 | md = p; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 777 | |
| 778 | if (md->type != EFI_RUNTIME_SERVICES_CODE) |
| 779 | continue; |
| 780 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 781 | efi_set_executable(md, true); |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 782 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 783 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 784 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 785 | /* |
Josh Triplett | 7bc90e0 | 2012-09-28 17:56:08 -0700 | [diff] [blame] | 786 | * We can't ioremap data in EFI boot services RAM, because we've already mapped |
| 787 | * it as RAM. So, look it up in the existing EFI memory map instead. Only |
| 788 | * callable after efi_enter_virtual_mode and before efi_free_boot_services. |
| 789 | */ |
| 790 | void __iomem *efi_lookup_mapped_addr(u64 phys_addr) |
| 791 | { |
| 792 | void *p; |
| 793 | if (WARN_ON(!memmap.map)) |
| 794 | return NULL; |
| 795 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 796 | efi_memory_desc_t *md = p; |
| 797 | u64 size = md->num_pages << EFI_PAGE_SHIFT; |
| 798 | u64 end = md->phys_addr + size; |
| 799 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
| 800 | md->type != EFI_BOOT_SERVICES_CODE && |
| 801 | md->type != EFI_BOOT_SERVICES_DATA) |
| 802 | continue; |
| 803 | if (!md->virt_addr) |
| 804 | continue; |
| 805 | if (phys_addr >= md->phys_addr && phys_addr < end) { |
| 806 | phys_addr += md->virt_addr - md->phys_addr; |
| 807 | return (__force void __iomem *)(unsigned long)phys_addr; |
| 808 | } |
| 809 | } |
| 810 | return NULL; |
| 811 | } |
| 812 | |
Matt Fleming | 3e8fa26 | 2012-10-19 13:25:46 +0100 | [diff] [blame^] | 813 | void efi_memory_uc(u64 addr, unsigned long size) |
| 814 | { |
| 815 | unsigned long page_shift = 1UL << EFI_PAGE_SHIFT; |
| 816 | u64 npages; |
| 817 | |
| 818 | npages = round_up(size, page_shift) / page_shift; |
| 819 | memrange_efi_to_native(&addr, &npages); |
| 820 | set_memory_uc(addr, npages); |
| 821 | } |
| 822 | |
Josh Triplett | 7bc90e0 | 2012-09-28 17:56:08 -0700 | [diff] [blame] | 823 | /* |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 824 | * This function will switch the EFI runtime services to virtual mode. |
| 825 | * Essentially, look through the EFI memmap and map every region that |
| 826 | * has the runtime attribute bit set in its memory descriptor and update |
| 827 | * that memory descriptor with the virtual address obtained from ioremap(). |
| 828 | * This enables the runtime services to be called without having to |
| 829 | * thunk back into physical mode for every invocation. |
| 830 | */ |
| 831 | void __init efi_enter_virtual_mode(void) |
| 832 | { |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 833 | efi_memory_desc_t *md, *prev_md = NULL; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 834 | efi_status_t status; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 835 | unsigned long size; |
Matt Fleming | 3e8fa26 | 2012-10-19 13:25:46 +0100 | [diff] [blame^] | 836 | u64 end, systab, end_pfn; |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 837 | void *p, *va, *new_memmap = NULL; |
| 838 | int count = 0; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 839 | |
| 840 | efi.systab = NULL; |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 841 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 842 | /* |
| 843 | * We don't do virtual mode, since we don't do runtime services, on |
| 844 | * non-native EFI |
| 845 | */ |
| 846 | |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 847 | if (!efi_native) { |
| 848 | efi_unmap_memmap(); |
| 849 | return; |
| 850 | } |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 851 | |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 852 | /* Merge contiguous regions of the same type and attribute */ |
| 853 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 854 | u64 prev_size; |
| 855 | md = p; |
| 856 | |
| 857 | if (!prev_md) { |
| 858 | prev_md = md; |
| 859 | continue; |
| 860 | } |
| 861 | |
| 862 | if (prev_md->type != md->type || |
| 863 | prev_md->attribute != md->attribute) { |
| 864 | prev_md = md; |
| 865 | continue; |
| 866 | } |
| 867 | |
| 868 | prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; |
| 869 | |
| 870 | if (md->phys_addr == (prev_md->phys_addr + prev_size)) { |
| 871 | prev_md->num_pages += md->num_pages; |
| 872 | md->type = EFI_RESERVED_TYPE; |
| 873 | md->attribute = 0; |
| 874 | continue; |
| 875 | } |
| 876 | prev_md = md; |
| 877 | } |
| 878 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 879 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 880 | md = p; |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 881 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
| 882 | md->type != EFI_BOOT_SERVICES_CODE && |
| 883 | md->type != EFI_BOOT_SERVICES_DATA) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 884 | continue; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 885 | |
| 886 | size = md->num_pages << EFI_PAGE_SHIFT; |
| 887 | end = md->phys_addr + size; |
| 888 | |
Huang Ying | dd39ecf | 2009-03-04 10:58:33 +0800 | [diff] [blame] | 889 | end_pfn = PFN_UP(end); |
| 890 | if (end_pfn <= max_low_pfn_mapped |
| 891 | || (end_pfn > (1UL << (32 - PAGE_SHIFT)) |
Matt Fleming | 3e8fa26 | 2012-10-19 13:25:46 +0100 | [diff] [blame^] | 892 | && end_pfn <= max_pfn_mapped)) { |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 893 | va = __va(md->phys_addr); |
Matt Fleming | 3e8fa26 | 2012-10-19 13:25:46 +0100 | [diff] [blame^] | 894 | |
| 895 | if (!(md->attribute & EFI_MEMORY_WB)) |
| 896 | efi_memory_uc((u64)(unsigned long)va, size); |
| 897 | } else |
| 898 | va = efi_ioremap(md->phys_addr, size, |
| 899 | md->type, md->attribute); |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 900 | |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 901 | md->virt_addr = (u64) (unsigned long) va; |
| 902 | |
| 903 | if (!va) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 904 | pr_err("ioremap of 0x%llX failed!\n", |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 905 | (unsigned long long)md->phys_addr); |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 906 | continue; |
| 907 | } |
| 908 | |
| 909 | systab = (u64) (unsigned long) efi_phys.systab; |
| 910 | if (md->phys_addr <= systab && systab < end) { |
| 911 | systab += md->virt_addr - md->phys_addr; |
| 912 | efi.systab = (efi_system_table_t *) (unsigned long) systab; |
| 913 | } |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 914 | new_memmap = krealloc(new_memmap, |
| 915 | (count + 1) * memmap.desc_size, |
| 916 | GFP_KERNEL); |
| 917 | memcpy(new_memmap + (count * memmap.desc_size), md, |
| 918 | memmap.desc_size); |
| 919 | count++; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | BUG_ON(!efi.systab); |
| 923 | |
| 924 | status = phys_efi_set_virtual_address_map( |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 925 | memmap.desc_size * count, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 926 | memmap.desc_size, |
| 927 | memmap.desc_version, |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 928 | (efi_memory_desc_t *)__pa(new_memmap)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 929 | |
| 930 | if (status != EFI_SUCCESS) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 931 | pr_alert("Unable to switch EFI into virtual mode " |
| 932 | "(status=%lx)!\n", status); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 933 | panic("EFI call to SetVirtualAddressMap() failed!"); |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * Now that EFI is in virtual mode, update the function |
| 938 | * pointers in the runtime service table to the new virtual addresses. |
| 939 | * |
| 940 | * Call EFI services through wrapper functions. |
| 941 | */ |
Seiji Aguchi | d6cf86d | 2012-07-24 13:27:23 +0000 | [diff] [blame] | 942 | efi.runtime_version = efi_systab.fw_revision; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 943 | efi.get_time = virt_efi_get_time; |
| 944 | efi.set_time = virt_efi_set_time; |
| 945 | efi.get_wakeup_time = virt_efi_get_wakeup_time; |
| 946 | efi.set_wakeup_time = virt_efi_set_wakeup_time; |
| 947 | efi.get_variable = virt_efi_get_variable; |
| 948 | efi.get_next_variable = virt_efi_get_next_variable; |
| 949 | efi.set_variable = virt_efi_set_variable; |
| 950 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; |
| 951 | efi.reset_system = virt_efi_reset_system; |
Matthew Garrett | 2b5e8ef | 2011-05-05 15:19:42 -0400 | [diff] [blame] | 952 | efi.set_virtual_address_map = NULL; |
Matthew Garrett | 3b37023 | 2011-06-06 15:36:25 -0400 | [diff] [blame] | 953 | efi.query_variable_info = virt_efi_query_variable_info; |
| 954 | efi.update_capsule = virt_efi_update_capsule; |
| 955 | efi.query_capsule_caps = virt_efi_query_capsule_caps; |
Huang, Ying | 4de0d4a | 2008-02-13 17:22:41 +0800 | [diff] [blame] | 956 | if (__supported_pte_mask & _PAGE_NX) |
| 957 | runtime_code_page_mkexec(); |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 958 | |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 959 | kfree(new_memmap); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* |
| 963 | * Convenience functions to obtain memory types and attributes |
| 964 | */ |
| 965 | u32 efi_mem_type(unsigned long phys_addr) |
| 966 | { |
| 967 | efi_memory_desc_t *md; |
| 968 | void *p; |
| 969 | |
| 970 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 971 | md = p; |
| 972 | if ((md->phys_addr <= phys_addr) && |
| 973 | (phys_addr < (md->phys_addr + |
| 974 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 975 | return md->type; |
| 976 | } |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | u64 efi_mem_attributes(unsigned long phys_addr) |
| 981 | { |
| 982 | efi_memory_desc_t *md; |
| 983 | void *p; |
| 984 | |
| 985 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 986 | md = p; |
| 987 | if ((md->phys_addr <= phys_addr) && |
| 988 | (phys_addr < (md->phys_addr + |
| 989 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 990 | return md->attribute; |
| 991 | } |
| 992 | return 0; |
| 993 | } |