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