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