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