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