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> |
Borislav Petkov | d2f7cbe | 2013-10-31 17:25:08 +0100 | [diff] [blame] | 15 | * Copyright (C) 2013 SuSE Labs |
| 16 | * Borislav Petkov <bp@suse.de> - runtime services VA mapping |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 17 | * |
| 18 | * Copied from efi_32.c to eliminate the duplicated code between EFI |
| 19 | * 32/64 support code. --ying 2007-10-26 |
| 20 | * |
| 21 | * All EFI Runtime Services are not implemented yet as EFI only |
| 22 | * supports physical mode addressing on SoftSDV. This is to be fixed |
| 23 | * in a future version. --drummond 1999-07-20 |
| 24 | * |
| 25 | * Implemented EFI runtime services and virtual mode calls. --davidm |
| 26 | * |
| 27 | * Goutham Rao: <goutham.rao@intel.com> |
| 28 | * Skip non-WB memory and ignore empty memory ranges. |
| 29 | */ |
| 30 | |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 31 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 32 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 33 | #include <linux/kernel.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/efi.h> |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 36 | #include <linux/efi-bgrt.h> |
Paul Gortmaker | 69c60c8 | 2011-05-26 12:22:53 -0400 | [diff] [blame] | 37 | #include <linux/export.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 38 | #include <linux/bootmem.h> |
David Howells | 0d01ff2 | 2013-04-11 23:51:01 +0100 | [diff] [blame] | 39 | #include <linux/slab.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 40 | #include <linux/memblock.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 41 | #include <linux/spinlock.h> |
| 42 | #include <linux/uaccess.h> |
| 43 | #include <linux/time.h> |
| 44 | #include <linux/io.h> |
| 45 | #include <linux/reboot.h> |
| 46 | #include <linux/bcd.h> |
| 47 | |
| 48 | #include <asm/setup.h> |
| 49 | #include <asm/efi.h> |
| 50 | #include <asm/time.h> |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 51 | #include <asm/cacheflush.h> |
| 52 | #include <asm/tlbflush.h> |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 53 | #include <asm/x86_init.h> |
Prarit Bhargava | 3195ef5 | 2013-02-14 12:02:54 -0500 | [diff] [blame] | 54 | #include <asm/rtc.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 55 | |
Borislav Petkov | f4fccac | 2013-10-31 17:24:59 +0100 | [diff] [blame] | 56 | #define EFI_DEBUG |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 57 | |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 58 | #define EFI_MIN_RESERVE 5120 |
| 59 | |
| 60 | #define EFI_DUMMY_GUID \ |
| 61 | EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9) |
| 62 | |
| 63 | static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 }; |
Matthew Garrett | 31ff2f2 | 2013-04-15 13:09:47 -0700 | [diff] [blame] | 64 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 65 | struct efi_memory_map memmap; |
| 66 | |
Harvey Harrison | ecaea42 | 2008-02-13 13:26:13 -0800 | [diff] [blame] | 67 | static struct efi efi_phys __initdata; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 68 | static efi_system_table_t efi_systab __initdata; |
| 69 | |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 70 | unsigned long x86_efi_facility; |
| 71 | |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 72 | static __initdata efi_config_table_type_t arch_tables[] = { |
| 73 | #ifdef CONFIG_X86_UV |
| 74 | {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab}, |
| 75 | #endif |
Leif Lindholm | 722da9d | 2013-10-03 15:42:37 +0100 | [diff] [blame] | 76 | {NULL_GUID, NULL, NULL}, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 79 | u64 efi_setup; /* efi setup_data physical address */ |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 80 | |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Returns 1 if 'facility' is enabled, 0 otherwise. |
| 83 | */ |
| 84 | int efi_enabled(int facility) |
| 85 | { |
| 86 | return test_bit(facility, &x86_efi_facility) != 0; |
| 87 | } |
| 88 | EXPORT_SYMBOL(efi_enabled); |
| 89 | |
Matt Fleming | 058e7b5 | 2013-02-22 15:03:47 +0000 | [diff] [blame] | 90 | static bool __initdata disable_runtime = false; |
Huang, Ying | 8b2cb7a | 2008-01-30 13:32:11 +0100 | [diff] [blame] | 91 | static int __init setup_noefi(char *arg) |
| 92 | { |
Matt Fleming | fb834c7 | 2013-02-20 20:36:12 +0000 | [diff] [blame] | 93 | disable_runtime = true; |
Huang, Ying | 8b2cb7a | 2008-01-30 13:32:11 +0100 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | early_param("noefi", setup_noefi); |
| 97 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 98 | int add_efi_memmap; |
| 99 | EXPORT_SYMBOL(add_efi_memmap); |
| 100 | |
| 101 | static int __init setup_add_efi_memmap(char *arg) |
| 102 | { |
| 103 | add_efi_memmap = 1; |
| 104 | return 0; |
| 105 | } |
| 106 | early_param("add_efi_memmap", setup_add_efi_memmap); |
| 107 | |
Richard Weinberger | 8c58bf3 | 2013-04-17 01:00:53 +0200 | [diff] [blame] | 108 | static bool efi_no_storage_paranoia; |
| 109 | |
| 110 | static int __init setup_storage_paranoia(char *arg) |
| 111 | { |
| 112 | efi_no_storage_paranoia = true; |
| 113 | return 0; |
| 114 | } |
| 115 | early_param("efi_no_storage_paranoia", setup_storage_paranoia); |
| 116 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 117 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
| 118 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 119 | unsigned long flags; |
| 120 | efi_status_t status; |
| 121 | |
| 122 | spin_lock_irqsave(&rtc_lock, flags); |
| 123 | status = efi_call_virt2(get_time, tm, tc); |
| 124 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 125 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static efi_status_t virt_efi_set_time(efi_time_t *tm) |
| 129 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 130 | unsigned long flags; |
| 131 | efi_status_t status; |
| 132 | |
| 133 | spin_lock_irqsave(&rtc_lock, flags); |
| 134 | status = efi_call_virt1(set_time, 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_get_wakeup_time(efi_bool_t *enabled, |
| 140 | efi_bool_t *pending, |
| 141 | efi_time_t *tm) |
| 142 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 143 | unsigned long flags; |
| 144 | efi_status_t status; |
| 145 | |
| 146 | spin_lock_irqsave(&rtc_lock, flags); |
| 147 | status = efi_call_virt3(get_wakeup_time, |
| 148 | enabled, pending, tm); |
| 149 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 150 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) |
| 154 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 155 | unsigned long flags; |
| 156 | efi_status_t status; |
| 157 | |
| 158 | spin_lock_irqsave(&rtc_lock, flags); |
| 159 | status = efi_call_virt2(set_wakeup_time, |
| 160 | enabled, tm); |
| 161 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 162 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, |
| 166 | efi_guid_t *vendor, |
| 167 | u32 *attr, |
| 168 | unsigned long *data_size, |
| 169 | void *data) |
| 170 | { |
| 171 | return efi_call_virt5(get_variable, |
| 172 | name, vendor, attr, |
| 173 | data_size, data); |
| 174 | } |
| 175 | |
| 176 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, |
| 177 | efi_char16_t *name, |
| 178 | efi_guid_t *vendor) |
| 179 | { |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 180 | return efi_call_virt3(get_next_variable, |
| 181 | name_size, name, vendor); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, |
| 185 | efi_guid_t *vendor, |
Matthew Garrett | f7a2d73 | 2011-06-06 15:36:24 -0400 | [diff] [blame] | 186 | u32 attr, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 187 | unsigned long data_size, |
| 188 | void *data) |
| 189 | { |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 190 | return efi_call_virt5(set_variable, |
| 191 | name, vendor, attr, |
| 192 | data_size, data); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Matthew Garrett | 3b37023 | 2011-06-06 15:36:25 -0400 | [diff] [blame] | 195 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
| 196 | u64 *storage_space, |
| 197 | u64 *remaining_space, |
| 198 | u64 *max_variable_size) |
| 199 | { |
| 200 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 201 | return EFI_UNSUPPORTED; |
| 202 | |
| 203 | return efi_call_virt4(query_variable_info, attr, storage_space, |
| 204 | remaining_space, max_variable_size); |
| 205 | } |
| 206 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 207 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
| 208 | { |
| 209 | return efi_call_virt1(get_next_high_mono_count, count); |
| 210 | } |
| 211 | |
| 212 | static void virt_efi_reset_system(int reset_type, |
| 213 | efi_status_t status, |
| 214 | unsigned long data_size, |
| 215 | efi_char16_t *data) |
| 216 | { |
| 217 | efi_call_virt4(reset_system, reset_type, status, |
| 218 | data_size, data); |
| 219 | } |
| 220 | |
Matthew Garrett | 3b37023 | 2011-06-06 15:36:25 -0400 | [diff] [blame] | 221 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, |
| 222 | unsigned long count, |
| 223 | unsigned long sg_list) |
| 224 | { |
| 225 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 226 | return EFI_UNSUPPORTED; |
| 227 | |
| 228 | return efi_call_virt3(update_capsule, capsules, count, sg_list); |
| 229 | } |
| 230 | |
| 231 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, |
| 232 | unsigned long count, |
| 233 | u64 *max_size, |
| 234 | int *reset_type) |
| 235 | { |
| 236 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 237 | return EFI_UNSUPPORTED; |
| 238 | |
| 239 | return efi_call_virt4(query_capsule_caps, capsules, count, max_size, |
| 240 | reset_type); |
| 241 | } |
| 242 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 243 | static efi_status_t __init phys_efi_set_virtual_address_map( |
| 244 | unsigned long memory_map_size, |
| 245 | unsigned long descriptor_size, |
| 246 | u32 descriptor_version, |
| 247 | efi_memory_desc_t *virtual_map) |
| 248 | { |
| 249 | efi_status_t status; |
| 250 | |
| 251 | efi_call_phys_prelog(); |
| 252 | status = efi_call_phys4(efi_phys.set_virtual_address_map, |
| 253 | memory_map_size, descriptor_size, |
| 254 | descriptor_version, virtual_map); |
| 255 | efi_call_phys_epilog(); |
| 256 | return status; |
| 257 | } |
| 258 | |
Linus Torvalds | 11520e5 | 2012-12-15 15:15:24 -0800 | [diff] [blame] | 259 | static efi_status_t __init phys_efi_get_time(efi_time_t *tm, |
| 260 | efi_time_cap_t *tc) |
| 261 | { |
| 262 | unsigned long flags; |
| 263 | efi_status_t status; |
| 264 | |
| 265 | spin_lock_irqsave(&rtc_lock, flags); |
| 266 | efi_call_phys_prelog(); |
| 267 | status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm), |
| 268 | virt_to_phys(tc)); |
| 269 | efi_call_phys_epilog(); |
| 270 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 271 | return status; |
| 272 | } |
| 273 | |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 274 | int efi_set_rtc_mmss(const struct timespec *now) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 275 | { |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 276 | unsigned long nowtime = now->tv_sec; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 277 | efi_status_t status; |
| 278 | efi_time_t eft; |
| 279 | efi_time_cap_t cap; |
Prarit Bhargava | 3195ef5 | 2013-02-14 12:02:54 -0500 | [diff] [blame] | 280 | struct rtc_time tm; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 281 | |
| 282 | status = efi.get_time(&eft, &cap); |
| 283 | if (status != EFI_SUCCESS) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 284 | pr_err("Oops: efitime: can't read time!\n"); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
Prarit Bhargava | 3195ef5 | 2013-02-14 12:02:54 -0500 | [diff] [blame] | 288 | rtc_time_to_tm(nowtime, &tm); |
| 289 | if (!rtc_valid_tm(&tm)) { |
| 290 | eft.year = tm.tm_year + 1900; |
| 291 | eft.month = tm.tm_mon + 1; |
| 292 | eft.day = tm.tm_mday; |
| 293 | eft.minute = tm.tm_min; |
| 294 | eft.second = tm.tm_sec; |
| 295 | eft.nanosecond = 0; |
| 296 | } else { |
| 297 | printk(KERN_ERR |
| 298 | "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n", |
| 299 | __FUNCTION__, nowtime); |
| 300 | return -1; |
| 301 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 302 | |
| 303 | status = efi.set_time(&eft); |
| 304 | if (status != EFI_SUCCESS) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 305 | pr_err("Oops: efitime: can't write time!\n"); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 311 | void efi_get_time(struct timespec *now) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 312 | { |
| 313 | efi_status_t status; |
| 314 | efi_time_t eft; |
| 315 | efi_time_cap_t cap; |
| 316 | |
| 317 | status = efi.get_time(&eft, &cap); |
| 318 | if (status != EFI_SUCCESS) |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 319 | pr_err("Oops: efitime: can't read time!\n"); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 320 | |
David Vrabel | 3565184 | 2013-05-13 18:56:06 +0100 | [diff] [blame] | 321 | now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour, |
| 322 | eft.minute, eft.second); |
| 323 | now->tv_nsec = 0; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 326 | /* |
| 327 | * Tell the kernel about the EFI memory map. This might include |
| 328 | * more than the max 128 entries that can fit in the e820 legacy |
| 329 | * (zeropage) memory map. |
| 330 | */ |
| 331 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 332 | static void __init do_add_efi_memmap(void) |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 333 | { |
| 334 | void *p; |
| 335 | |
| 336 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 337 | efi_memory_desc_t *md = p; |
| 338 | unsigned long long start = md->phys_addr; |
| 339 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 340 | int e820_type; |
| 341 | |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 342 | switch (md->type) { |
| 343 | case EFI_LOADER_CODE: |
| 344 | case EFI_LOADER_DATA: |
| 345 | case EFI_BOOT_SERVICES_CODE: |
| 346 | case EFI_BOOT_SERVICES_DATA: |
| 347 | case EFI_CONVENTIONAL_MEMORY: |
| 348 | if (md->attribute & EFI_MEMORY_WB) |
| 349 | e820_type = E820_RAM; |
| 350 | else |
| 351 | e820_type = E820_RESERVED; |
| 352 | break; |
| 353 | case EFI_ACPI_RECLAIM_MEMORY: |
| 354 | e820_type = E820_ACPI; |
| 355 | break; |
| 356 | case EFI_ACPI_MEMORY_NVS: |
| 357 | e820_type = E820_NVS; |
| 358 | break; |
| 359 | case EFI_UNUSABLE_MEMORY: |
| 360 | e820_type = E820_UNUSABLE; |
| 361 | break; |
| 362 | default: |
| 363 | /* |
| 364 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE |
| 365 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO |
| 366 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE |
| 367 | */ |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 368 | e820_type = E820_RESERVED; |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 369 | break; |
| 370 | } |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 371 | e820_add_region(start, size, e820_type); |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 372 | } |
| 373 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 374 | } |
| 375 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 376 | int __init efi_memblock_x86_reserve_range(void) |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 377 | { |
Borislav Petkov | 15b9c35 | 2013-03-01 17:07:44 +0100 | [diff] [blame] | 378 | struct efi_info *e = &boot_params.efi_info; |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 379 | unsigned long pmap; |
| 380 | |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 381 | #ifdef CONFIG_X86_32 |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 382 | /* Can't handle data above 4GB at this time */ |
Borislav Petkov | 15b9c35 | 2013-03-01 17:07:44 +0100 | [diff] [blame] | 383 | if (e->efi_memmap_hi) { |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 384 | pr_err("Memory map is above 4GB, disabling EFI.\n"); |
| 385 | return -EINVAL; |
| 386 | } |
Borislav Petkov | 15b9c35 | 2013-03-01 17:07:44 +0100 | [diff] [blame] | 387 | pmap = e->efi_memmap; |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 388 | #else |
Borislav Petkov | 15b9c35 | 2013-03-01 17:07:44 +0100 | [diff] [blame] | 389 | pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32)); |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 390 | #endif |
Borislav Petkov | 15b9c35 | 2013-03-01 17:07:44 +0100 | [diff] [blame] | 391 | memmap.phys_map = (void *)pmap; |
| 392 | memmap.nr_map = e->efi_memmap_size / |
| 393 | e->efi_memdesc_size; |
| 394 | memmap.desc_size = e->efi_memdesc_size; |
| 395 | memmap.desc_version = e->efi_memdesc_version; |
| 396 | |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 397 | memblock_reserve(pmap, memmap.nr_map * memmap.desc_size); |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 398 | |
Leif Lindholm | 258f6fd | 2013-09-05 11:34:55 +0100 | [diff] [blame] | 399 | efi.memmap = &memmap; |
| 400 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 401 | return 0; |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 402 | } |
| 403 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 404 | static void __init print_efi_memmap(void) |
| 405 | { |
Borislav Petkov | f4fccac | 2013-10-31 17:24:59 +0100 | [diff] [blame] | 406 | #ifdef EFI_DEBUG |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 407 | efi_memory_desc_t *md; |
| 408 | void *p; |
| 409 | int i; |
| 410 | |
| 411 | for (p = memmap.map, i = 0; |
| 412 | p < memmap.map_end; |
| 413 | p += memmap.desc_size, i++) { |
| 414 | md = p; |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 415 | pr_info("mem%02u: type=%u, attr=0x%llx, " |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 416 | "range=[0x%016llx-0x%016llx) (%lluMB)\n", |
| 417 | i, md->type, md->attribute, md->phys_addr, |
| 418 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), |
| 419 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); |
| 420 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 421 | #endif /* EFI_DEBUG */ |
Borislav Petkov | f4fccac | 2013-10-31 17:24:59 +0100 | [diff] [blame] | 422 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 423 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 424 | void __init efi_reserve_boot_services(void) |
| 425 | { |
| 426 | void *p; |
| 427 | |
| 428 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 429 | efi_memory_desc_t *md = p; |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 430 | u64 start = md->phys_addr; |
| 431 | u64 size = md->num_pages << EFI_PAGE_SHIFT; |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 432 | |
| 433 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 434 | md->type != EFI_BOOT_SERVICES_DATA) |
| 435 | continue; |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 436 | /* Only reserve where possible: |
| 437 | * - Not within any already allocated areas |
| 438 | * - Not over any memory area (really needed, if above?) |
| 439 | * - Not within any part of the kernel |
| 440 | * - Not the bios reserved area |
| 441 | */ |
Dave Young | a7f84f0 | 2013-12-20 18:02:15 +0800 | [diff] [blame] | 442 | if ((start + size > __pa_symbol(_text) |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 443 | && start <= __pa_symbol(_end)) || |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 444 | !e820_all_mapped(start, start+size, E820_RAM) || |
Tejun Heo | bf61549 | 2011-07-12 09:58:05 +0200 | [diff] [blame] | 445 | memblock_is_region_reserved(start, size)) { |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 446 | /* Could not reserve, skip it */ |
| 447 | md->num_pages = 0; |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 448 | memblock_dbg("Could not reserve boot range " |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 449 | "[0x%010llx-0x%010llx]\n", |
| 450 | start, start+size-1); |
| 451 | } else |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 452 | memblock_reserve(start, size); |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Olof Johansson | 5189c2a | 2012-10-24 10:00:44 -0700 | [diff] [blame] | 456 | void __init efi_unmap_memmap(void) |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 457 | { |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 458 | clear_bit(EFI_MEMMAP, &x86_efi_facility); |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 459 | if (memmap.map) { |
| 460 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); |
| 461 | memmap.map = NULL; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void __init efi_free_boot_services(void) |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 466 | { |
| 467 | void *p; |
| 468 | |
Olof Johansson | 5189c2a | 2012-10-24 10:00:44 -0700 | [diff] [blame] | 469 | if (!efi_is_native()) |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 470 | return; |
| 471 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 472 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 473 | efi_memory_desc_t *md = p; |
| 474 | unsigned long long start = md->phys_addr; |
| 475 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 476 | |
| 477 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 478 | md->type != EFI_BOOT_SERVICES_DATA) |
| 479 | continue; |
| 480 | |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 481 | /* Could not reserve boot area */ |
| 482 | if (!size) |
| 483 | continue; |
| 484 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 485 | free_bootmem_late(start, size); |
| 486 | } |
Josh Triplett | 7851079 | 2012-09-28 17:55:44 -0700 | [diff] [blame] | 487 | |
| 488 | efi_unmap_memmap(); |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 491 | static int __init efi_systab_init(void *phys) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 492 | { |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 493 | if (efi_enabled(EFI_64BIT)) { |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 494 | efi_system_table_64_t *systab64; |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 495 | struct efi_setup_data *data = NULL; |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 496 | u64 tmp = 0; |
| 497 | |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 498 | if (efi_setup) { |
| 499 | data = early_memremap(efi_setup, sizeof(*data)); |
| 500 | if (!data) |
| 501 | return -ENOMEM; |
| 502 | } |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 503 | systab64 = early_ioremap((unsigned long)phys, |
| 504 | sizeof(*systab64)); |
| 505 | if (systab64 == NULL) { |
| 506 | pr_err("Couldn't map the system table!\n"); |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 507 | if (data) |
| 508 | early_iounmap(data, sizeof(*data)); |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 509 | return -ENOMEM; |
| 510 | } |
| 511 | |
| 512 | efi_systab.hdr = systab64->hdr; |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 513 | efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor : |
| 514 | systab64->fw_vendor; |
| 515 | tmp |= data ? data->fw_vendor : systab64->fw_vendor; |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 516 | efi_systab.fw_revision = systab64->fw_revision; |
| 517 | efi_systab.con_in_handle = systab64->con_in_handle; |
| 518 | tmp |= systab64->con_in_handle; |
| 519 | efi_systab.con_in = systab64->con_in; |
| 520 | tmp |= systab64->con_in; |
| 521 | efi_systab.con_out_handle = systab64->con_out_handle; |
| 522 | tmp |= systab64->con_out_handle; |
| 523 | efi_systab.con_out = systab64->con_out; |
| 524 | tmp |= systab64->con_out; |
| 525 | efi_systab.stderr_handle = systab64->stderr_handle; |
| 526 | tmp |= systab64->stderr_handle; |
| 527 | efi_systab.stderr = systab64->stderr; |
| 528 | tmp |= systab64->stderr; |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 529 | efi_systab.runtime = data ? |
| 530 | (void *)(unsigned long)data->runtime : |
| 531 | (void *)(unsigned long)systab64->runtime; |
| 532 | tmp |= data ? data->runtime : systab64->runtime; |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 533 | efi_systab.boottime = (void *)(unsigned long)systab64->boottime; |
| 534 | tmp |= systab64->boottime; |
| 535 | efi_systab.nr_tables = systab64->nr_tables; |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 536 | efi_systab.tables = data ? (unsigned long)data->tables : |
| 537 | systab64->tables; |
| 538 | tmp |= data ? data->tables : systab64->tables; |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 539 | |
| 540 | early_iounmap(systab64, sizeof(*systab64)); |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 541 | if (data) |
| 542 | early_iounmap(data, sizeof(*data)); |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 543 | #ifdef CONFIG_X86_32 |
| 544 | if (tmp >> 32) { |
| 545 | pr_err("EFI data located above 4GB, disabling EFI.\n"); |
| 546 | return -EINVAL; |
| 547 | } |
| 548 | #endif |
| 549 | } else { |
| 550 | efi_system_table_32_t *systab32; |
| 551 | |
| 552 | systab32 = early_ioremap((unsigned long)phys, |
| 553 | sizeof(*systab32)); |
| 554 | if (systab32 == NULL) { |
| 555 | pr_err("Couldn't map the system table!\n"); |
| 556 | return -ENOMEM; |
| 557 | } |
| 558 | |
| 559 | efi_systab.hdr = systab32->hdr; |
| 560 | efi_systab.fw_vendor = systab32->fw_vendor; |
| 561 | efi_systab.fw_revision = systab32->fw_revision; |
| 562 | efi_systab.con_in_handle = systab32->con_in_handle; |
| 563 | efi_systab.con_in = systab32->con_in; |
| 564 | efi_systab.con_out_handle = systab32->con_out_handle; |
| 565 | efi_systab.con_out = systab32->con_out; |
| 566 | efi_systab.stderr_handle = systab32->stderr_handle; |
| 567 | efi_systab.stderr = systab32->stderr; |
| 568 | efi_systab.runtime = (void *)(unsigned long)systab32->runtime; |
| 569 | efi_systab.boottime = (void *)(unsigned long)systab32->boottime; |
| 570 | efi_systab.nr_tables = systab32->nr_tables; |
| 571 | efi_systab.tables = systab32->tables; |
| 572 | |
| 573 | early_iounmap(systab32, sizeof(*systab32)); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 574 | } |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 575 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 576 | efi.systab = &efi_systab; |
| 577 | |
| 578 | /* |
| 579 | * Verify the EFI Table |
| 580 | */ |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 581 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 582 | pr_err("System table signature incorrect!\n"); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 583 | return -EINVAL; |
| 584 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 585 | if ((efi.systab->hdr.revision >> 16) == 0) |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 586 | pr_err("Warning: System table version " |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 587 | "%d.%02d, expected 1.00 or greater!\n", |
| 588 | efi.systab->hdr.revision >> 16, |
| 589 | efi.systab->hdr.revision & 0xffff); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 590 | |
| 591 | return 0; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 592 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 593 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 594 | static int __init efi_runtime_init(void) |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 595 | { |
| 596 | efi_runtime_services_t *runtime; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 597 | |
| 598 | /* |
| 599 | * Check out the runtime services table. We need to map |
| 600 | * the runtime services table so that we can grab the physical |
| 601 | * address of several of the EFI runtime functions, needed to |
| 602 | * set the firmware into virtual mode. |
| 603 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 604 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
| 605 | sizeof(efi_runtime_services_t)); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 606 | if (!runtime) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 607 | pr_err("Could not map the runtime service table!\n"); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 608 | return -ENOMEM; |
| 609 | } |
| 610 | /* |
| 611 | * We will only need *early* access to the following |
Linus Torvalds | 11520e5 | 2012-12-15 15:15:24 -0800 | [diff] [blame] | 612 | * two EFI runtime services before set_virtual_address_map |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 613 | * is invoked. |
| 614 | */ |
Linus Torvalds | 11520e5 | 2012-12-15 15:15:24 -0800 | [diff] [blame] | 615 | efi_phys.get_time = (efi_get_time_t *)runtime->get_time; |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 616 | efi_phys.set_virtual_address_map = |
| 617 | (efi_set_virtual_address_map_t *) |
| 618 | runtime->set_virtual_address_map; |
Linus Torvalds | 11520e5 | 2012-12-15 15:15:24 -0800 | [diff] [blame] | 619 | /* |
| 620 | * Make efi_get_time can be called before entering |
| 621 | * virtual mode. |
| 622 | */ |
| 623 | efi.get_time = phys_efi_get_time; |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 624 | early_iounmap(runtime, sizeof(efi_runtime_services_t)); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 625 | |
| 626 | return 0; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 627 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 628 | |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 629 | static int __init efi_memmap_init(void) |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 630 | { |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 631 | /* Map the EFI memory map */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 632 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
| 633 | memmap.nr_map * memmap.desc_size); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 634 | if (memmap.map == NULL) { |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 635 | pr_err("Could not map the memory map!\n"); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 636 | return -ENOMEM; |
| 637 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 638 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); |
Russ Anderson | 175e438 | 2008-10-02 17:32:06 -0500 | [diff] [blame] | 639 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 640 | if (add_efi_memmap) |
| 641 | do_add_efi_memmap(); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 642 | |
| 643 | return 0; |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 646 | /* |
| 647 | * A number of config table entries get remapped to virtual addresses |
| 648 | * after entering EFI virtual mode. However, the kexec kernel requires |
| 649 | * their physical addresses therefore we pass them via setup_data and |
| 650 | * correct those entries to their respective physical addresses here. |
| 651 | * |
| 652 | * Currently only handles smbios which is necessary for some firmware |
| 653 | * implementation. |
| 654 | */ |
| 655 | static int __init efi_reuse_config(u64 tables, int nr_tables) |
| 656 | { |
| 657 | int i, sz, ret = 0; |
| 658 | void *p, *tablep; |
| 659 | struct efi_setup_data *data; |
| 660 | |
| 661 | if (!efi_setup) |
| 662 | return 0; |
| 663 | |
| 664 | if (!efi_enabled(EFI_64BIT)) |
| 665 | return 0; |
| 666 | |
| 667 | data = early_memremap(efi_setup, sizeof(*data)); |
| 668 | if (!data) { |
| 669 | ret = -ENOMEM; |
| 670 | goto out; |
| 671 | } |
| 672 | |
| 673 | if (!data->smbios) |
| 674 | goto out_memremap; |
| 675 | |
| 676 | sz = sizeof(efi_config_table_64_t); |
| 677 | |
| 678 | p = tablep = early_memremap(tables, nr_tables * sz); |
| 679 | if (!p) { |
| 680 | pr_err("Could not map Configuration table!\n"); |
| 681 | ret = -ENOMEM; |
| 682 | goto out_memremap; |
| 683 | } |
| 684 | |
| 685 | for (i = 0; i < efi.systab->nr_tables; i++) { |
| 686 | efi_guid_t guid; |
| 687 | |
| 688 | guid = ((efi_config_table_64_t *)p)->guid; |
| 689 | |
| 690 | if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) |
| 691 | ((efi_config_table_64_t *)p)->table = data->smbios; |
| 692 | p += sz; |
| 693 | } |
| 694 | early_iounmap(tablep, nr_tables * sz); |
| 695 | |
| 696 | out_memremap: |
| 697 | early_iounmap(data, sizeof(*data)); |
| 698 | out: |
| 699 | return ret; |
| 700 | } |
| 701 | |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 702 | void __init efi_init(void) |
| 703 | { |
| 704 | efi_char16_t *c16; |
| 705 | char vendor[100] = "unknown"; |
| 706 | int i = 0; |
| 707 | void *tmp; |
| 708 | |
| 709 | #ifdef CONFIG_X86_32 |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 710 | if (boot_params.efi_info.efi_systab_hi || |
| 711 | boot_params.efi_info.efi_memmap_hi) { |
| 712 | pr_info("Table located above 4GB, disabling EFI.\n"); |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 713 | return; |
| 714 | } |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 715 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
| 716 | #else |
| 717 | efi_phys.systab = (efi_system_table_t *) |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 718 | (boot_params.efi_info.efi_systab | |
| 719 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 720 | #endif |
| 721 | |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 722 | if (efi_systab_init(efi_phys.systab)) |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 723 | return; |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 724 | |
| 725 | set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility); |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 726 | |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 727 | efi.config_table = (unsigned long)efi.systab->tables; |
| 728 | efi.fw_vendor = (unsigned long)efi.systab->fw_vendor; |
| 729 | efi.runtime = (unsigned long)efi.systab->runtime; |
| 730 | |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 731 | /* |
| 732 | * Show what we know for posterity |
| 733 | */ |
| 734 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); |
| 735 | if (c16) { |
| 736 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) |
| 737 | vendor[i] = *c16++; |
| 738 | vendor[i] = '\0'; |
| 739 | } else |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 740 | pr_err("Could not map the firmware vendor!\n"); |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 741 | early_iounmap(tmp, 2); |
| 742 | |
Olof Johansson | e3cb3f5 | 2012-02-12 13:24:26 -0800 | [diff] [blame] | 743 | pr_info("EFI v%u.%.02u by %s\n", |
| 744 | efi.systab->hdr.revision >> 16, |
| 745 | efi.systab->hdr.revision & 0xffff, vendor); |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 746 | |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 747 | if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables)) |
| 748 | return; |
| 749 | |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 750 | if (efi_config_init(arch_tables)) |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 751 | return; |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 752 | |
| 753 | set_bit(EFI_CONFIG_TABLES, &x86_efi_facility); |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 754 | |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 755 | /* |
| 756 | * Note: We currently don't support runtime services on an EFI |
| 757 | * that doesn't match the kernel 32/64-bit mode. |
| 758 | */ |
| 759 | |
Olof Johansson | 5189c2a | 2012-10-24 10:00:44 -0700 | [diff] [blame] | 760 | if (!efi_is_native()) |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 761 | 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] | 762 | else { |
Matt Fleming | fb834c7 | 2013-02-20 20:36:12 +0000 | [diff] [blame] | 763 | if (disable_runtime || efi_runtime_init()) |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 764 | return; |
| 765 | set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility); |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 766 | } |
Olof Johansson | 83e7ee6 | 2012-02-12 13:24:25 -0800 | [diff] [blame] | 767 | |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 768 | if (efi_memmap_init()) |
Olof Johansson | 140bf27 | 2012-02-12 13:24:28 -0800 | [diff] [blame] | 769 | return; |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 770 | |
| 771 | set_bit(EFI_MEMMAP, &x86_efi_facility); |
| 772 | |
Linus Torvalds | 11520e5 | 2012-12-15 15:15:24 -0800 | [diff] [blame] | 773 | #ifdef CONFIG_X86_32 |
Olof Johansson | 5189c2a | 2012-10-24 10:00:44 -0700 | [diff] [blame] | 774 | if (efi_is_native()) { |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 775 | x86_platform.get_wallclock = efi_get_time; |
| 776 | x86_platform.set_wallclock = efi_set_rtc_mmss; |
| 777 | } |
Linus Torvalds | 11520e5 | 2012-12-15 15:15:24 -0800 | [diff] [blame] | 778 | #endif |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 779 | print_efi_memmap(); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 780 | } |
| 781 | |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 782 | void __init efi_late_init(void) |
| 783 | { |
| 784 | efi_bgrt_init(); |
| 785 | } |
| 786 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 787 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
| 788 | { |
| 789 | u64 addr, npages; |
| 790 | |
| 791 | addr = md->virt_addr; |
| 792 | npages = md->num_pages; |
| 793 | |
| 794 | memrange_efi_to_native(&addr, &npages); |
| 795 | |
| 796 | if (executable) |
| 797 | set_memory_x(addr, npages); |
| 798 | else |
| 799 | set_memory_nx(addr, npages); |
| 800 | } |
| 801 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 802 | static void __init runtime_code_page_mkexec(void) |
| 803 | { |
| 804 | efi_memory_desc_t *md; |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 805 | void *p; |
| 806 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 807 | /* Make EFI runtime service code area executable */ |
| 808 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 809 | md = p; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 810 | |
| 811 | if (md->type != EFI_RUNTIME_SERVICES_CODE) |
| 812 | continue; |
| 813 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 814 | efi_set_executable(md, true); |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 815 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 816 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 817 | |
Matt Fleming | 3e8fa26 | 2012-10-19 13:25:46 +0100 | [diff] [blame] | 818 | void efi_memory_uc(u64 addr, unsigned long size) |
| 819 | { |
| 820 | unsigned long page_shift = 1UL << EFI_PAGE_SHIFT; |
| 821 | u64 npages; |
| 822 | |
| 823 | npages = round_up(size, page_shift) / page_shift; |
| 824 | memrange_efi_to_native(&addr, &npages); |
| 825 | set_memory_uc(addr, npages); |
| 826 | } |
| 827 | |
Borislav Petkov | d2f7cbe | 2013-10-31 17:25:08 +0100 | [diff] [blame] | 828 | void __init old_map_region(efi_memory_desc_t *md) |
| 829 | { |
| 830 | u64 start_pfn, end_pfn, end; |
| 831 | unsigned long size; |
| 832 | void *va; |
| 833 | |
| 834 | start_pfn = PFN_DOWN(md->phys_addr); |
| 835 | size = md->num_pages << PAGE_SHIFT; |
| 836 | end = md->phys_addr + size; |
| 837 | end_pfn = PFN_UP(end); |
| 838 | |
| 839 | if (pfn_range_is_mapped(start_pfn, end_pfn)) { |
| 840 | va = __va(md->phys_addr); |
| 841 | |
| 842 | if (!(md->attribute & EFI_MEMORY_WB)) |
| 843 | efi_memory_uc((u64)(unsigned long)va, size); |
| 844 | } else |
| 845 | va = efi_ioremap(md->phys_addr, size, |
| 846 | md->type, md->attribute); |
| 847 | |
| 848 | md->virt_addr = (u64) (unsigned long) va; |
| 849 | if (!va) |
| 850 | pr_err("ioremap of 0x%llX failed!\n", |
| 851 | (unsigned long long)md->phys_addr); |
| 852 | } |
| 853 | |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 854 | /* Merge contiguous regions of the same type and attribute */ |
| 855 | static void __init efi_merge_regions(void) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 856 | { |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 857 | void *p; |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 858 | efi_memory_desc_t *md, *prev_md = NULL; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 859 | |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 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 | } |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static void __init get_systab_virt_addr(efi_memory_desc_t *md) |
| 888 | { |
| 889 | unsigned long size; |
| 890 | u64 end, systab; |
| 891 | |
| 892 | size = md->num_pages << EFI_PAGE_SHIFT; |
| 893 | end = md->phys_addr + size; |
| 894 | systab = (u64)(unsigned long)efi_phys.systab; |
| 895 | if (md->phys_addr <= systab && systab < end) { |
| 896 | systab += md->virt_addr - md->phys_addr; |
| 897 | efi.systab = (efi_system_table_t *)(unsigned long)systab; |
| 898 | } |
| 899 | } |
| 900 | |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 901 | static int __init save_runtime_map(void) |
| 902 | { |
| 903 | efi_memory_desc_t *md; |
| 904 | void *tmp, *p, *q = NULL; |
| 905 | int count = 0; |
| 906 | |
| 907 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 908 | md = p; |
| 909 | |
| 910 | if (!(md->attribute & EFI_MEMORY_RUNTIME) || |
| 911 | (md->type == EFI_BOOT_SERVICES_CODE) || |
| 912 | (md->type == EFI_BOOT_SERVICES_DATA)) |
| 913 | continue; |
| 914 | tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL); |
| 915 | if (!tmp) |
| 916 | goto out; |
| 917 | q = tmp; |
| 918 | |
| 919 | memcpy(q + count * memmap.desc_size, md, memmap.desc_size); |
| 920 | count++; |
| 921 | } |
| 922 | |
Matt Fleming | 518548a | 2013-12-21 16:09:46 +0000 | [diff] [blame^] | 923 | efi_runtime_map_setup(q, count, memmap.desc_size); |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 924 | |
| 925 | return 0; |
| 926 | out: |
| 927 | kfree(q); |
| 928 | return -ENOMEM; |
| 929 | } |
| 930 | |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 931 | /* |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 932 | * Map efi regions which were passed via setup_data. The virt_addr is a fixed |
| 933 | * addr which was used in first kernel of a kexec boot. |
| 934 | */ |
| 935 | static void __init efi_map_regions_fixed(void) |
| 936 | { |
| 937 | void *p; |
| 938 | efi_memory_desc_t *md; |
| 939 | |
| 940 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 941 | md = p; |
| 942 | efi_map_region_fixed(md); /* FIXME: add error handling */ |
| 943 | get_systab_virt_addr(md); |
| 944 | } |
| 945 | |
| 946 | } |
| 947 | |
| 948 | /* |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 949 | * Map efi memory ranges for runtime serivce and update new_memmap with virtual |
| 950 | * addresses. |
| 951 | */ |
| 952 | static void * __init efi_map_regions(int *count) |
| 953 | { |
| 954 | efi_memory_desc_t *md; |
| 955 | void *p, *tmp, *new_memmap = NULL; |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 956 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 957 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 958 | md = p; |
Josh Boyer | 7008701 | 2013-04-18 07:51:34 -0700 | [diff] [blame] | 959 | if (!(md->attribute & EFI_MEMORY_RUNTIME)) { |
| 960 | #ifdef CONFIG_X86_64 |
| 961 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 962 | md->type != EFI_BOOT_SERVICES_DATA) |
| 963 | #endif |
| 964 | continue; |
| 965 | } |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 966 | |
Borislav Petkov | d2f7cbe | 2013-10-31 17:25:08 +0100 | [diff] [blame] | 967 | efi_map_region(md); |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 968 | get_systab_virt_addr(md); |
Borislav Petkov | d2f7cbe | 2013-10-31 17:25:08 +0100 | [diff] [blame] | 969 | |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 970 | tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size, |
| 971 | GFP_KERNEL); |
| 972 | if (!tmp) |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 973 | goto out; |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 974 | new_memmap = tmp; |
| 975 | memcpy(new_memmap + (*count * memmap.desc_size), md, |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 976 | memmap.desc_size); |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 977 | (*count)++; |
| 978 | } |
| 979 | |
| 980 | return new_memmap; |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 981 | out: |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 982 | kfree(new_memmap); |
| 983 | return NULL; |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * This function will switch the EFI runtime services to virtual mode. |
| 988 | * Essentially, we look through the EFI memmap and map every region that |
| 989 | * has the runtime attribute bit set in its memory descriptor into the |
| 990 | * ->trampoline_pgd page table using a top-down VA allocation scheme. |
| 991 | * |
| 992 | * The old method which used to update that memory descriptor with the |
| 993 | * virtual address obtained from ioremap() is still supported when the |
| 994 | * kernel is booted with efi=old_map on its command line. Same old |
| 995 | * method enabled the runtime services to be called without having to |
| 996 | * thunk back into physical mode for every invocation. |
| 997 | * |
| 998 | * The new method does a pagetable switch in a preemption-safe manner |
| 999 | * so that we're in a different address space when calling a runtime |
| 1000 | * function. For function arguments passing we do copy the PGDs of the |
| 1001 | * kernel page table into ->trampoline_pgd prior to each call. |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 1002 | * |
| 1003 | * Specially for kexec boot, efi runtime maps in previous kernel should |
| 1004 | * be passed in via setup_data. In that case runtime ranges will be mapped |
| 1005 | * to the same virtual addresses as the first kernel. |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 1006 | */ |
| 1007 | void __init efi_enter_virtual_mode(void) |
| 1008 | { |
| 1009 | efi_status_t status; |
| 1010 | void *new_memmap = NULL; |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 1011 | int err, count = 0; |
Dave Young | 481f75c | 2013-12-20 18:02:16 +0800 | [diff] [blame] | 1012 | |
| 1013 | efi.systab = NULL; |
| 1014 | |
| 1015 | /* |
| 1016 | * We don't do virtual mode, since we don't do runtime services, on |
| 1017 | * non-native EFI |
| 1018 | */ |
| 1019 | if (!efi_is_native()) { |
| 1020 | efi_unmap_memmap(); |
| 1021 | return; |
| 1022 | } |
| 1023 | |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 1024 | if (efi_setup) { |
| 1025 | efi_map_regions_fixed(); |
| 1026 | } else { |
| 1027 | efi_merge_regions(); |
| 1028 | new_memmap = efi_map_regions(&count); |
| 1029 | if (!new_memmap) { |
| 1030 | pr_err("Error reallocating memory, EFI runtime non-functional!\n"); |
| 1031 | return; |
| 1032 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 1035 | err = save_runtime_map(); |
| 1036 | if (err) |
| 1037 | pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n"); |
| 1038 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 1039 | BUG_ON(!efi.systab); |
| 1040 | |
Borislav Petkov | d2f7cbe | 2013-10-31 17:25:08 +0100 | [diff] [blame] | 1041 | efi_setup_page_tables(); |
| 1042 | efi_sync_low_kernel_mappings(); |
| 1043 | |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 1044 | if (!efi_setup) { |
| 1045 | status = phys_efi_set_virtual_address_map( |
| 1046 | memmap.desc_size * count, |
| 1047 | memmap.desc_size, |
| 1048 | memmap.desc_version, |
| 1049 | (efi_memory_desc_t *)__pa(new_memmap)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 1050 | |
Dave Young | 1fec053 | 2013-12-20 18:02:19 +0800 | [diff] [blame] | 1051 | if (status != EFI_SUCCESS) { |
| 1052 | pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n", |
| 1053 | status); |
| 1054 | panic("EFI call to SetVirtualAddressMap() failed!"); |
| 1055 | } |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | * Now that EFI is in virtual mode, update the function |
| 1060 | * pointers in the runtime service table to the new virtual addresses. |
| 1061 | * |
| 1062 | * Call EFI services through wrapper functions. |
| 1063 | */ |
Matt Fleming | 712ba9e | 2013-01-25 10:07:25 +0000 | [diff] [blame] | 1064 | efi.runtime_version = efi_systab.hdr.revision; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 1065 | efi.get_time = virt_efi_get_time; |
| 1066 | efi.set_time = virt_efi_set_time; |
| 1067 | efi.get_wakeup_time = virt_efi_get_wakeup_time; |
| 1068 | efi.set_wakeup_time = virt_efi_set_wakeup_time; |
| 1069 | efi.get_variable = virt_efi_get_variable; |
| 1070 | efi.get_next_variable = virt_efi_get_next_variable; |
| 1071 | efi.set_variable = virt_efi_set_variable; |
| 1072 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; |
| 1073 | efi.reset_system = virt_efi_reset_system; |
Matthew Garrett | 2b5e8ef | 2011-05-05 15:19:42 -0400 | [diff] [blame] | 1074 | efi.set_virtual_address_map = NULL; |
Matthew Garrett | 3b37023 | 2011-06-06 15:36:25 -0400 | [diff] [blame] | 1075 | efi.query_variable_info = virt_efi_query_variable_info; |
| 1076 | efi.update_capsule = virt_efi_update_capsule; |
| 1077 | efi.query_capsule_caps = virt_efi_query_capsule_caps; |
Borislav Petkov | d2f7cbe | 2013-10-31 17:25:08 +0100 | [diff] [blame] | 1078 | |
| 1079 | if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX)) |
Huang, Ying | 4de0d4a | 2008-02-13 17:22:41 +0800 | [diff] [blame] | 1080 | runtime_code_page_mkexec(); |
Olof Johansson | 1adbfa3 | 2012-02-12 13:24:29 -0800 | [diff] [blame] | 1081 | |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 1082 | kfree(new_memmap); |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 1083 | |
| 1084 | /* clean DUMMY object */ |
| 1085 | efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID, |
| 1086 | EFI_VARIABLE_NON_VOLATILE | |
| 1087 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1088 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1089 | 0, NULL); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Convenience functions to obtain memory types and attributes |
| 1094 | */ |
| 1095 | u32 efi_mem_type(unsigned long phys_addr) |
| 1096 | { |
| 1097 | efi_memory_desc_t *md; |
| 1098 | void *p; |
| 1099 | |
Matt Fleming | 83e6818 | 2012-11-14 09:42:35 +0000 | [diff] [blame] | 1100 | if (!efi_enabled(EFI_MEMMAP)) |
| 1101 | return 0; |
| 1102 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 1103 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 1104 | md = p; |
| 1105 | if ((md->phys_addr <= phys_addr) && |
| 1106 | (phys_addr < (md->phys_addr + |
| 1107 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 1108 | return md->type; |
| 1109 | } |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | u64 efi_mem_attributes(unsigned long phys_addr) |
| 1114 | { |
| 1115 | efi_memory_desc_t *md; |
| 1116 | void *p; |
| 1117 | |
| 1118 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 1119 | md = p; |
| 1120 | if ((md->phys_addr <= phys_addr) && |
| 1121 | (phys_addr < (md->phys_addr + |
| 1122 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 1123 | return md->attribute; |
| 1124 | } |
| 1125 | return 0; |
| 1126 | } |
Matt Fleming | a6e4d5a | 2013-03-25 09:14:30 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* |
| 1129 | * Some firmware has serious problems when using more than 50% of the EFI |
| 1130 | * variable store, i.e. it triggers bugs that can brick machines. Ensure that |
| 1131 | * we never use more than this safe limit. |
| 1132 | * |
| 1133 | * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable |
| 1134 | * store. |
| 1135 | */ |
| 1136 | efi_status_t efi_query_variable_store(u32 attributes, unsigned long size) |
| 1137 | { |
| 1138 | efi_status_t status; |
| 1139 | u64 storage_size, remaining_size, max_size; |
| 1140 | |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 1141 | if (!(attributes & EFI_VARIABLE_NON_VOLATILE)) |
| 1142 | return 0; |
| 1143 | |
Matt Fleming | a6e4d5a | 2013-03-25 09:14:30 +0000 | [diff] [blame] | 1144 | status = efi.query_variable_info(attributes, &storage_size, |
| 1145 | &remaining_size, &max_size); |
| 1146 | if (status != EFI_SUCCESS) |
| 1147 | return status; |
| 1148 | |
Matthew Garrett | 31ff2f2 | 2013-04-15 13:09:47 -0700 | [diff] [blame] | 1149 | /* |
| 1150 | * Some firmware implementations refuse to boot if there's insufficient |
| 1151 | * space in the variable store. We account for that by refusing the |
| 1152 | * write if permitting it would reduce the available space to under |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 1153 | * 5KB. This figure was provided by Samsung, so should be safe. |
Matthew Garrett | 31ff2f2 | 2013-04-15 13:09:47 -0700 | [diff] [blame] | 1154 | */ |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 1155 | if ((remaining_size - size < EFI_MIN_RESERVE) && |
| 1156 | !efi_no_storage_paranoia) { |
Richard Weinberger | 7791c84 | 2013-04-10 10:59:34 +0200 | [diff] [blame] | 1157 | |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 1158 | /* |
| 1159 | * Triggering garbage collection may require that the firmware |
| 1160 | * generate a real EFI_OUT_OF_RESOURCES error. We can force |
| 1161 | * that by attempting to use more space than is available. |
| 1162 | */ |
| 1163 | unsigned long dummy_size = remaining_size + 1024; |
Ben Hutchings | b8cb62f | 2013-06-16 21:27:12 +0100 | [diff] [blame] | 1164 | void *dummy = kzalloc(dummy_size, GFP_ATOMIC); |
| 1165 | |
| 1166 | if (!dummy) |
| 1167 | return EFI_OUT_OF_RESOURCES; |
Richard Weinberger | 8c58bf3 | 2013-04-17 01:00:53 +0200 | [diff] [blame] | 1168 | |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 1169 | status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID, |
| 1170 | EFI_VARIABLE_NON_VOLATILE | |
| 1171 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1172 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1173 | dummy_size, dummy); |
| 1174 | |
| 1175 | if (status == EFI_SUCCESS) { |
| 1176 | /* |
| 1177 | * This should have failed, so if it didn't make sure |
| 1178 | * that we delete it... |
| 1179 | */ |
| 1180 | efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID, |
| 1181 | EFI_VARIABLE_NON_VOLATILE | |
| 1182 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1183 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1184 | 0, dummy); |
| 1185 | } |
| 1186 | |
Ben Hutchings | b8cb62f | 2013-06-16 21:27:12 +0100 | [diff] [blame] | 1187 | kfree(dummy); |
| 1188 | |
Matthew Garrett | f8b8404 | 2013-06-01 16:06:20 -0400 | [diff] [blame] | 1189 | /* |
| 1190 | * The runtime code may now have triggered a garbage collection |
| 1191 | * run, so check the variable info again |
| 1192 | */ |
| 1193 | status = efi.query_variable_info(attributes, &storage_size, |
| 1194 | &remaining_size, &max_size); |
| 1195 | |
| 1196 | if (status != EFI_SUCCESS) |
| 1197 | return status; |
| 1198 | |
| 1199 | /* |
| 1200 | * There still isn't enough room, so return an error |
| 1201 | */ |
| 1202 | if (remaining_size - size < EFI_MIN_RESERVE) |
| 1203 | return EFI_OUT_OF_RESOURCES; |
| 1204 | } |
Matt Fleming | a6e4d5a | 2013-03-25 09:14:30 +0000 | [diff] [blame] | 1205 | |
| 1206 | return EFI_SUCCESS; |
| 1207 | } |
Sergey Vlasov | 3668011 | 2013-04-16 18:31:09 +0400 | [diff] [blame] | 1208 | EXPORT_SYMBOL_GPL(efi_query_variable_store); |
Borislav Petkov | d2f7cbe | 2013-10-31 17:25:08 +0100 | [diff] [blame] | 1209 | |
| 1210 | static int __init parse_efi_cmdline(char *str) |
| 1211 | { |
| 1212 | if (*str == '=') |
| 1213 | str++; |
| 1214 | |
| 1215 | if (!strncmp(str, "old_map", 7)) |
| 1216 | set_bit(EFI_OLD_MEMMAP, &x86_efi_facility); |
| 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | early_param("efi", parse_efi_cmdline); |