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