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