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 | |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/efi.h> |
| 32 | #include <linux/bootmem.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 33 | #include <linux/memblock.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/uaccess.h> |
| 36 | #include <linux/time.h> |
| 37 | #include <linux/io.h> |
| 38 | #include <linux/reboot.h> |
| 39 | #include <linux/bcd.h> |
| 40 | |
| 41 | #include <asm/setup.h> |
| 42 | #include <asm/efi.h> |
| 43 | #include <asm/time.h> |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 44 | #include <asm/cacheflush.h> |
| 45 | #include <asm/tlbflush.h> |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 46 | #include <asm/x86_init.h> |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 47 | |
| 48 | #define EFI_DEBUG 1 |
| 49 | #define PFX "EFI: " |
| 50 | |
| 51 | int efi_enabled; |
| 52 | EXPORT_SYMBOL(efi_enabled); |
| 53 | |
| 54 | struct efi efi; |
| 55 | EXPORT_SYMBOL(efi); |
| 56 | |
| 57 | struct efi_memory_map memmap; |
| 58 | |
Harvey Harrison | ecaea42 | 2008-02-13 13:26:13 -0800 | [diff] [blame] | 59 | static struct efi efi_phys __initdata; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 60 | static efi_system_table_t efi_systab __initdata; |
| 61 | |
Huang, Ying | 8b2cb7a | 2008-01-30 13:32:11 +0100 | [diff] [blame] | 62 | static int __init setup_noefi(char *arg) |
| 63 | { |
| 64 | efi_enabled = 0; |
| 65 | return 0; |
| 66 | } |
| 67 | early_param("noefi", setup_noefi); |
| 68 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 69 | int add_efi_memmap; |
| 70 | EXPORT_SYMBOL(add_efi_memmap); |
| 71 | |
| 72 | static int __init setup_add_efi_memmap(char *arg) |
| 73 | { |
| 74 | add_efi_memmap = 1; |
| 75 | return 0; |
| 76 | } |
| 77 | early_param("add_efi_memmap", setup_add_efi_memmap); |
| 78 | |
| 79 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 80 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
| 81 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 82 | unsigned long flags; |
| 83 | efi_status_t status; |
| 84 | |
| 85 | spin_lock_irqsave(&rtc_lock, flags); |
| 86 | status = efi_call_virt2(get_time, tm, tc); |
| 87 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 88 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static efi_status_t virt_efi_set_time(efi_time_t *tm) |
| 92 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 93 | unsigned long flags; |
| 94 | efi_status_t status; |
| 95 | |
| 96 | spin_lock_irqsave(&rtc_lock, flags); |
| 97 | status = efi_call_virt1(set_time, tm); |
| 98 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 99 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, |
| 103 | efi_bool_t *pending, |
| 104 | efi_time_t *tm) |
| 105 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 106 | unsigned long flags; |
| 107 | efi_status_t status; |
| 108 | |
| 109 | spin_lock_irqsave(&rtc_lock, flags); |
| 110 | status = efi_call_virt3(get_wakeup_time, |
| 111 | enabled, pending, tm); |
| 112 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 113 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) |
| 117 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 118 | unsigned long flags; |
| 119 | efi_status_t status; |
| 120 | |
| 121 | spin_lock_irqsave(&rtc_lock, flags); |
| 122 | status = efi_call_virt2(set_wakeup_time, |
| 123 | enabled, tm); |
| 124 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 125 | return status; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, |
| 129 | efi_guid_t *vendor, |
| 130 | u32 *attr, |
| 131 | unsigned long *data_size, |
| 132 | void *data) |
| 133 | { |
| 134 | return efi_call_virt5(get_variable, |
| 135 | name, vendor, attr, |
| 136 | data_size, data); |
| 137 | } |
| 138 | |
| 139 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, |
| 140 | efi_char16_t *name, |
| 141 | efi_guid_t *vendor) |
| 142 | { |
| 143 | return efi_call_virt3(get_next_variable, |
| 144 | name_size, name, vendor); |
| 145 | } |
| 146 | |
| 147 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, |
| 148 | efi_guid_t *vendor, |
| 149 | unsigned long attr, |
| 150 | unsigned long data_size, |
| 151 | void *data) |
| 152 | { |
| 153 | return efi_call_virt5(set_variable, |
| 154 | name, vendor, attr, |
| 155 | data_size, data); |
| 156 | } |
| 157 | |
| 158 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
| 159 | { |
| 160 | return efi_call_virt1(get_next_high_mono_count, count); |
| 161 | } |
| 162 | |
| 163 | static void virt_efi_reset_system(int reset_type, |
| 164 | efi_status_t status, |
| 165 | unsigned long data_size, |
| 166 | efi_char16_t *data) |
| 167 | { |
| 168 | efi_call_virt4(reset_system, reset_type, status, |
| 169 | data_size, data); |
| 170 | } |
| 171 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 172 | static efi_status_t __init phys_efi_set_virtual_address_map( |
| 173 | unsigned long memory_map_size, |
| 174 | unsigned long descriptor_size, |
| 175 | u32 descriptor_version, |
| 176 | efi_memory_desc_t *virtual_map) |
| 177 | { |
| 178 | efi_status_t status; |
| 179 | |
| 180 | efi_call_phys_prelog(); |
| 181 | status = efi_call_phys4(efi_phys.set_virtual_address_map, |
| 182 | memory_map_size, descriptor_size, |
| 183 | descriptor_version, virtual_map); |
| 184 | efi_call_phys_epilog(); |
| 185 | return status; |
| 186 | } |
| 187 | |
| 188 | static efi_status_t __init phys_efi_get_time(efi_time_t *tm, |
| 189 | efi_time_cap_t *tc) |
| 190 | { |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 191 | unsigned long flags; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 192 | efi_status_t status; |
| 193 | |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 194 | spin_lock_irqsave(&rtc_lock, flags); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 195 | efi_call_phys_prelog(); |
| 196 | status = efi_call_phys2(efi_phys.get_time, tm, tc); |
| 197 | efi_call_phys_epilog(); |
Jan Beulich | ef68c8f | 2011-07-19 11:53:07 +0100 | [diff] [blame] | 198 | spin_unlock_irqrestore(&rtc_lock, flags); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 199 | return status; |
| 200 | } |
| 201 | |
| 202 | int efi_set_rtc_mmss(unsigned long nowtime) |
| 203 | { |
| 204 | int real_seconds, real_minutes; |
| 205 | efi_status_t status; |
| 206 | efi_time_t eft; |
| 207 | efi_time_cap_t cap; |
| 208 | |
| 209 | status = efi.get_time(&eft, &cap); |
| 210 | if (status != EFI_SUCCESS) { |
| 211 | printk(KERN_ERR "Oops: efitime: can't read time!\n"); |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | real_seconds = nowtime % 60; |
| 216 | real_minutes = nowtime / 60; |
| 217 | if (((abs(real_minutes - eft.minute) + 15)/30) & 1) |
| 218 | real_minutes += 30; |
| 219 | real_minutes %= 60; |
| 220 | eft.minute = real_minutes; |
| 221 | eft.second = real_seconds; |
| 222 | |
| 223 | status = efi.set_time(&eft); |
| 224 | if (status != EFI_SUCCESS) { |
| 225 | printk(KERN_ERR "Oops: efitime: can't write time!\n"); |
| 226 | return -1; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | unsigned long efi_get_time(void) |
| 232 | { |
| 233 | efi_status_t status; |
| 234 | efi_time_t eft; |
| 235 | efi_time_cap_t cap; |
| 236 | |
| 237 | status = efi.get_time(&eft, &cap); |
| 238 | if (status != EFI_SUCCESS) |
| 239 | printk(KERN_ERR "Oops: efitime: can't read time!\n"); |
| 240 | |
| 241 | return mktime(eft.year, eft.month, eft.day, eft.hour, |
| 242 | eft.minute, eft.second); |
| 243 | } |
| 244 | |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 245 | /* |
| 246 | * Tell the kernel about the EFI memory map. This might include |
| 247 | * more than the max 128 entries that can fit in the e820 legacy |
| 248 | * (zeropage) memory map. |
| 249 | */ |
| 250 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 251 | static void __init do_add_efi_memmap(void) |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 252 | { |
| 253 | void *p; |
| 254 | |
| 255 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 256 | efi_memory_desc_t *md = p; |
| 257 | unsigned long long start = md->phys_addr; |
| 258 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 259 | int e820_type; |
| 260 | |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 261 | switch (md->type) { |
| 262 | case EFI_LOADER_CODE: |
| 263 | case EFI_LOADER_DATA: |
| 264 | case EFI_BOOT_SERVICES_CODE: |
| 265 | case EFI_BOOT_SERVICES_DATA: |
| 266 | case EFI_CONVENTIONAL_MEMORY: |
| 267 | if (md->attribute & EFI_MEMORY_WB) |
| 268 | e820_type = E820_RAM; |
| 269 | else |
| 270 | e820_type = E820_RESERVED; |
| 271 | break; |
| 272 | case EFI_ACPI_RECLAIM_MEMORY: |
| 273 | e820_type = E820_ACPI; |
| 274 | break; |
| 275 | case EFI_ACPI_MEMORY_NVS: |
| 276 | e820_type = E820_NVS; |
| 277 | break; |
| 278 | case EFI_UNUSABLE_MEMORY: |
| 279 | e820_type = E820_UNUSABLE; |
| 280 | break; |
| 281 | default: |
| 282 | /* |
| 283 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE |
| 284 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO |
| 285 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE |
| 286 | */ |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 287 | e820_type = E820_RESERVED; |
Cliff Wickman | e2a7147 | 2009-06-16 16:43:40 -0500 | [diff] [blame] | 288 | break; |
| 289 | } |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 290 | e820_add_region(start, size, e820_type); |
Paul Jackson | 69c9189 | 2008-05-14 08:15:58 -0700 | [diff] [blame] | 291 | } |
| 292 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 293 | } |
| 294 | |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 295 | void __init efi_memblock_x86_reserve_range(void) |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 296 | { |
| 297 | unsigned long pmap; |
| 298 | |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 299 | #ifdef CONFIG_X86_32 |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 300 | pmap = boot_params.efi_info.efi_memmap; |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 301 | #else |
| 302 | pmap = (boot_params.efi_info.efi_memmap | |
| 303 | ((__u64)boot_params.efi_info.efi_memmap_hi<<32)); |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 304 | #endif |
| 305 | memmap.phys_map = (void *)pmap; |
| 306 | memmap.nr_map = boot_params.efi_info.efi_memmap_size / |
| 307 | boot_params.efi_info.efi_memdesc_size; |
| 308 | memmap.desc_version = boot_params.efi_info.efi_memdesc_version; |
| 309 | memmap.desc_size = boot_params.efi_info.efi_memdesc_size; |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 310 | memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size, |
Huang, Ying | ecacf09 | 2008-06-02 14:26:21 +0800 | [diff] [blame] | 311 | "EFI memmap"); |
| 312 | } |
| 313 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 314 | #if EFI_DEBUG |
| 315 | static void __init print_efi_memmap(void) |
| 316 | { |
| 317 | efi_memory_desc_t *md; |
| 318 | void *p; |
| 319 | int i; |
| 320 | |
| 321 | for (p = memmap.map, i = 0; |
| 322 | p < memmap.map_end; |
| 323 | p += memmap.desc_size, i++) { |
| 324 | md = p; |
| 325 | printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, " |
| 326 | "range=[0x%016llx-0x%016llx) (%lluMB)\n", |
| 327 | i, md->type, md->attribute, md->phys_addr, |
| 328 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), |
| 329 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); |
| 330 | } |
| 331 | } |
| 332 | #endif /* EFI_DEBUG */ |
| 333 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 334 | void __init efi_reserve_boot_services(void) |
| 335 | { |
| 336 | void *p; |
| 337 | |
| 338 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 339 | efi_memory_desc_t *md = p; |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 340 | u64 start = md->phys_addr; |
| 341 | u64 size = md->num_pages << EFI_PAGE_SHIFT; |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 342 | |
| 343 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 344 | md->type != EFI_BOOT_SERVICES_DATA) |
| 345 | continue; |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 346 | /* Only reserve where possible: |
| 347 | * - Not within any already allocated areas |
| 348 | * - Not over any memory area (really needed, if above?) |
| 349 | * - Not within any part of the kernel |
| 350 | * - Not the bios reserved area |
| 351 | */ |
| 352 | if ((start+size >= virt_to_phys(_text) |
| 353 | && start <= virt_to_phys(_end)) || |
| 354 | !e820_all_mapped(start, start+size, E820_RAM) || |
| 355 | memblock_x86_check_reserved_size(&start, &size, |
| 356 | 1<<EFI_PAGE_SHIFT)) { |
| 357 | /* Could not reserve, skip it */ |
| 358 | md->num_pages = 0; |
| 359 | memblock_dbg(PFX "Could not reserve boot range " |
| 360 | "[0x%010llx-0x%010llx]\n", |
| 361 | start, start+size-1); |
| 362 | } else |
| 363 | memblock_x86_reserve_range(start, start+size, |
| 364 | "EFI Boot"); |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | static void __init efi_free_boot_services(void) |
| 369 | { |
| 370 | void *p; |
| 371 | |
| 372 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 373 | efi_memory_desc_t *md = p; |
| 374 | unsigned long long start = md->phys_addr; |
| 375 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; |
| 376 | |
| 377 | if (md->type != EFI_BOOT_SERVICES_CODE && |
| 378 | md->type != EFI_BOOT_SERVICES_DATA) |
| 379 | continue; |
| 380 | |
Maarten Lankhorst | 7d68dc3 | 2011-06-14 19:53:09 +0200 | [diff] [blame] | 381 | /* Could not reserve boot area */ |
| 382 | if (!size) |
| 383 | continue; |
| 384 | |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 385 | free_bootmem_late(start, size); |
| 386 | } |
| 387 | } |
| 388 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 389 | void __init efi_init(void) |
| 390 | { |
| 391 | efi_config_table_t *config_tables; |
| 392 | efi_runtime_services_t *runtime; |
| 393 | efi_char16_t *c16; |
| 394 | char vendor[100] = "unknown"; |
| 395 | int i = 0; |
| 396 | void *tmp; |
| 397 | |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 398 | #ifdef CONFIG_X86_32 |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 399 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
Paul Jackson | 05486fa | 2008-06-22 07:22:02 -0700 | [diff] [blame] | 400 | #else |
| 401 | efi_phys.systab = (efi_system_table_t *) |
| 402 | (boot_params.efi_info.efi_systab | |
| 403 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 404 | #endif |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 405 | |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 406 | efi.systab = early_ioremap((unsigned long)efi_phys.systab, |
| 407 | sizeof(efi_system_table_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 408 | if (efi.systab == NULL) |
| 409 | printk(KERN_ERR "Couldn't map the EFI system table!\n"); |
| 410 | memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t)); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 411 | early_iounmap(efi.systab, sizeof(efi_system_table_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 412 | efi.systab = &efi_systab; |
| 413 | |
| 414 | /* |
| 415 | * Verify the EFI Table |
| 416 | */ |
| 417 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 418 | printk(KERN_ERR "EFI system table signature incorrect!\n"); |
| 419 | if ((efi.systab->hdr.revision >> 16) == 0) |
| 420 | printk(KERN_ERR "Warning: EFI system table version " |
| 421 | "%d.%02d, expected 1.00 or greater!\n", |
| 422 | efi.systab->hdr.revision >> 16, |
| 423 | efi.systab->hdr.revision & 0xffff); |
| 424 | |
| 425 | /* |
| 426 | * Show what we know for posterity |
| 427 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 428 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 429 | if (c16) { |
Roel Kluin | fdb8a42 | 2009-08-06 15:58:13 -0700 | [diff] [blame] | 430 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 431 | vendor[i] = *c16++; |
| 432 | vendor[i] = '\0'; |
| 433 | } else |
| 434 | printk(KERN_ERR PFX "Could not map the firmware vendor!\n"); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 435 | early_iounmap(tmp, 2); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 436 | |
Frans Pop | 3235dc3 | 2010-02-06 18:47:17 +0100 | [diff] [blame] | 437 | printk(KERN_INFO "EFI v%u.%.02u by %s\n", |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 438 | efi.systab->hdr.revision >> 16, |
| 439 | efi.systab->hdr.revision & 0xffff, vendor); |
| 440 | |
| 441 | /* |
| 442 | * Let's see what config tables the firmware passed to us. |
| 443 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 444 | config_tables = early_ioremap( |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 445 | efi.systab->tables, |
| 446 | efi.systab->nr_tables * sizeof(efi_config_table_t)); |
| 447 | if (config_tables == NULL) |
| 448 | printk(KERN_ERR "Could not map EFI Configuration Table!\n"); |
| 449 | |
| 450 | printk(KERN_INFO); |
| 451 | for (i = 0; i < efi.systab->nr_tables; i++) { |
| 452 | if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) { |
| 453 | efi.mps = config_tables[i].table; |
| 454 | printk(" MPS=0x%lx ", config_tables[i].table); |
| 455 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 456 | ACPI_20_TABLE_GUID)) { |
| 457 | efi.acpi20 = config_tables[i].table; |
| 458 | printk(" ACPI 2.0=0x%lx ", config_tables[i].table); |
| 459 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 460 | ACPI_TABLE_GUID)) { |
| 461 | efi.acpi = config_tables[i].table; |
| 462 | printk(" ACPI=0x%lx ", config_tables[i].table); |
| 463 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 464 | SMBIOS_TABLE_GUID)) { |
| 465 | efi.smbios = config_tables[i].table; |
| 466 | printk(" SMBIOS=0x%lx ", config_tables[i].table); |
Nick Piggin | 03b4863 | 2009-01-20 04:36:04 +0100 | [diff] [blame] | 467 | #ifdef CONFIG_X86_UV |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 468 | } else if (!efi_guidcmp(config_tables[i].guid, |
Russ Anderson | a50f70b | 2008-10-03 11:58:54 -0500 | [diff] [blame] | 469 | UV_SYSTEM_TABLE_GUID)) { |
| 470 | efi.uv_systab = config_tables[i].table; |
| 471 | printk(" UVsystab=0x%lx ", config_tables[i].table); |
Nick Piggin | 03b4863 | 2009-01-20 04:36:04 +0100 | [diff] [blame] | 472 | #endif |
Russ Anderson | a50f70b | 2008-10-03 11:58:54 -0500 | [diff] [blame] | 473 | } else if (!efi_guidcmp(config_tables[i].guid, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 474 | HCDP_TABLE_GUID)) { |
| 475 | efi.hcdp = config_tables[i].table; |
| 476 | printk(" HCDP=0x%lx ", config_tables[i].table); |
| 477 | } else if (!efi_guidcmp(config_tables[i].guid, |
| 478 | UGA_IO_PROTOCOL_GUID)) { |
| 479 | efi.uga = config_tables[i].table; |
| 480 | printk(" UGA=0x%lx ", config_tables[i].table); |
| 481 | } |
| 482 | } |
| 483 | printk("\n"); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 484 | early_iounmap(config_tables, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 485 | efi.systab->nr_tables * sizeof(efi_config_table_t)); |
| 486 | |
| 487 | /* |
| 488 | * Check out the runtime services table. We need to map |
| 489 | * the runtime services table so that we can grab the physical |
| 490 | * address of several of the EFI runtime functions, needed to |
| 491 | * set the firmware into virtual mode. |
| 492 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 493 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
| 494 | sizeof(efi_runtime_services_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 495 | if (runtime != NULL) { |
| 496 | /* |
| 497 | * We will only need *early* access to the following |
| 498 | * two EFI runtime services before set_virtual_address_map |
| 499 | * is invoked. |
| 500 | */ |
| 501 | efi_phys.get_time = (efi_get_time_t *)runtime->get_time; |
| 502 | efi_phys.set_virtual_address_map = |
| 503 | (efi_set_virtual_address_map_t *) |
| 504 | runtime->set_virtual_address_map; |
| 505 | /* |
| 506 | * Make efi_get_time can be called before entering |
| 507 | * virtual mode. |
| 508 | */ |
| 509 | efi.get_time = phys_efi_get_time; |
| 510 | } else |
| 511 | printk(KERN_ERR "Could not map the EFI runtime service " |
| 512 | "table!\n"); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 513 | early_iounmap(runtime, sizeof(efi_runtime_services_t)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 514 | |
| 515 | /* Map the EFI memory map */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 516 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
| 517 | memmap.nr_map * memmap.desc_size); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 518 | if (memmap.map == NULL) |
| 519 | printk(KERN_ERR "Could not map the EFI memory map!\n"); |
| 520 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); |
Russ Anderson | 175e438 | 2008-10-02 17:32:06 -0500 | [diff] [blame] | 521 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 522 | if (memmap.desc_size != sizeof(efi_memory_desc_t)) |
Russ Anderson | 175e438 | 2008-10-02 17:32:06 -0500 | [diff] [blame] | 523 | printk(KERN_WARNING |
| 524 | "Kernel-defined memdesc doesn't match the one from EFI!\n"); |
| 525 | |
Paul Jackson | 200001e | 2008-06-25 05:44:46 -0700 | [diff] [blame] | 526 | if (add_efi_memmap) |
| 527 | do_add_efi_memmap(); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 528 | |
Feng Tang | 772be89 | 2009-10-20 12:54:02 +0800 | [diff] [blame] | 529 | #ifdef CONFIG_X86_32 |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 530 | x86_platform.get_wallclock = efi_get_time; |
| 531 | x86_platform.set_wallclock = efi_set_rtc_mmss; |
Feng Tang | 772be89 | 2009-10-20 12:54:02 +0800 | [diff] [blame] | 532 | #endif |
Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 533 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 534 | #if EFI_DEBUG |
| 535 | print_efi_memmap(); |
| 536 | #endif |
| 537 | } |
| 538 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 539 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
| 540 | { |
| 541 | u64 addr, npages; |
| 542 | |
| 543 | addr = md->virt_addr; |
| 544 | npages = md->num_pages; |
| 545 | |
| 546 | memrange_efi_to_native(&addr, &npages); |
| 547 | |
| 548 | if (executable) |
| 549 | set_memory_x(addr, npages); |
| 550 | else |
| 551 | set_memory_nx(addr, npages); |
| 552 | } |
| 553 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 554 | static void __init runtime_code_page_mkexec(void) |
| 555 | { |
| 556 | efi_memory_desc_t *md; |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 557 | void *p; |
| 558 | |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 559 | /* Make EFI runtime service code area executable */ |
| 560 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 561 | md = p; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 562 | |
| 563 | if (md->type != EFI_RUNTIME_SERVICES_CODE) |
| 564 | continue; |
| 565 | |
Matthew Garrett | 9cd2b07 | 2011-05-05 15:19:43 -0400 | [diff] [blame] | 566 | efi_set_executable(md, true); |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 567 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 568 | } |
Huang, Ying | a2172e2 | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 569 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 570 | /* |
| 571 | * This function will switch the EFI runtime services to virtual mode. |
| 572 | * Essentially, look through the EFI memmap and map every region that |
| 573 | * has the runtime attribute bit set in its memory descriptor and update |
| 574 | * that memory descriptor with the virtual address obtained from ioremap(). |
| 575 | * This enables the runtime services to be called without having to |
| 576 | * thunk back into physical mode for every invocation. |
| 577 | */ |
| 578 | void __init efi_enter_virtual_mode(void) |
| 579 | { |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 580 | efi_memory_desc_t *md, *prev_md = NULL; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 581 | efi_status_t status; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 582 | unsigned long size; |
Huang Ying | dd39ecf | 2009-03-04 10:58:33 +0800 | [diff] [blame] | 583 | u64 end, systab, addr, npages, end_pfn; |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 584 | void *p, *va, *new_memmap = NULL; |
| 585 | int count = 0; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 586 | |
| 587 | efi.systab = NULL; |
Matthew Garrett | 202f9d0 | 2011-05-05 15:19:44 -0400 | [diff] [blame] | 588 | |
| 589 | /* Merge contiguous regions of the same type and attribute */ |
| 590 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 591 | u64 prev_size; |
| 592 | md = p; |
| 593 | |
| 594 | if (!prev_md) { |
| 595 | prev_md = md; |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | if (prev_md->type != md->type || |
| 600 | prev_md->attribute != md->attribute) { |
| 601 | prev_md = md; |
| 602 | continue; |
| 603 | } |
| 604 | |
| 605 | prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; |
| 606 | |
| 607 | if (md->phys_addr == (prev_md->phys_addr + prev_size)) { |
| 608 | prev_md->num_pages += md->num_pages; |
| 609 | md->type = EFI_RESERVED_TYPE; |
| 610 | md->attribute = 0; |
| 611 | continue; |
| 612 | } |
| 613 | prev_md = md; |
| 614 | } |
| 615 | |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 616 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 617 | md = p; |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 618 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
| 619 | md->type != EFI_BOOT_SERVICES_CODE && |
| 620 | md->type != EFI_BOOT_SERVICES_DATA) |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 621 | continue; |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 622 | |
| 623 | size = md->num_pages << EFI_PAGE_SHIFT; |
| 624 | end = md->phys_addr + size; |
| 625 | |
Huang Ying | dd39ecf | 2009-03-04 10:58:33 +0800 | [diff] [blame] | 626 | end_pfn = PFN_UP(end); |
| 627 | if (end_pfn <= max_low_pfn_mapped |
| 628 | || (end_pfn > (1UL << (32 - PAGE_SHIFT)) |
| 629 | && end_pfn <= max_pfn_mapped)) |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 630 | va = __va(md->phys_addr); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 631 | else |
Paul Mackerras | 6a7bbd5 | 2009-08-03 22:38:10 +1000 | [diff] [blame] | 632 | va = efi_ioremap(md->phys_addr, size, md->type); |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 633 | |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 634 | md->virt_addr = (u64) (unsigned long) va; |
| 635 | |
| 636 | if (!va) { |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 637 | printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n", |
| 638 | (unsigned long long)md->phys_addr); |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 639 | continue; |
| 640 | } |
| 641 | |
Huang, Ying | 4a3575f | 2008-02-25 15:18:37 +0800 | [diff] [blame] | 642 | if (!(md->attribute & EFI_MEMORY_WB)) { |
| 643 | addr = md->virt_addr; |
| 644 | npages = md->num_pages; |
| 645 | memrange_efi_to_native(&addr, &npages); |
| 646 | set_memory_uc(addr, npages); |
| 647 | } |
Thomas Gleixner | e85f205 | 2008-02-12 19:46:48 +0100 | [diff] [blame] | 648 | |
Huang, Ying | 1c083eb | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 649 | systab = (u64) (unsigned long) efi_phys.systab; |
| 650 | if (md->phys_addr <= systab && systab < end) { |
| 651 | systab += md->virt_addr - md->phys_addr; |
| 652 | efi.systab = (efi_system_table_t *) (unsigned long) systab; |
| 653 | } |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 654 | new_memmap = krealloc(new_memmap, |
| 655 | (count + 1) * memmap.desc_size, |
| 656 | GFP_KERNEL); |
| 657 | memcpy(new_memmap + (count * memmap.desc_size), md, |
| 658 | memmap.desc_size); |
| 659 | count++; |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | BUG_ON(!efi.systab); |
| 663 | |
| 664 | status = phys_efi_set_virtual_address_map( |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 665 | memmap.desc_size * count, |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 666 | memmap.desc_size, |
| 667 | memmap.desc_version, |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 668 | (efi_memory_desc_t *)__pa(new_memmap)); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 669 | |
| 670 | if (status != EFI_SUCCESS) { |
| 671 | printk(KERN_ALERT "Unable to switch EFI into virtual mode " |
| 672 | "(status=%lx)!\n", status); |
| 673 | panic("EFI call to SetVirtualAddressMap() failed!"); |
| 674 | } |
| 675 | |
| 676 | /* |
Matthew Garrett | 916f676 | 2011-05-25 09:53:13 -0400 | [diff] [blame] | 677 | * Thankfully, it does seem that no runtime services other than |
| 678 | * SetVirtualAddressMap() will touch boot services code, so we can |
| 679 | * get rid of it all at this point |
| 680 | */ |
| 681 | efi_free_boot_services(); |
| 682 | |
| 683 | /* |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 684 | * Now that EFI is in virtual mode, update the function |
| 685 | * pointers in the runtime service table to the new virtual addresses. |
| 686 | * |
| 687 | * Call EFI services through wrapper functions. |
| 688 | */ |
| 689 | efi.get_time = virt_efi_get_time; |
| 690 | efi.set_time = virt_efi_set_time; |
| 691 | efi.get_wakeup_time = virt_efi_get_wakeup_time; |
| 692 | efi.set_wakeup_time = virt_efi_set_wakeup_time; |
| 693 | efi.get_variable = virt_efi_get_variable; |
| 694 | efi.get_next_variable = virt_efi_get_next_variable; |
| 695 | efi.set_variable = virt_efi_set_variable; |
| 696 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; |
| 697 | efi.reset_system = virt_efi_reset_system; |
Matthew Garrett | 2b5e8ef | 2011-05-05 15:19:42 -0400 | [diff] [blame] | 698 | efi.set_virtual_address_map = NULL; |
Huang, Ying | 4de0d4a | 2008-02-13 17:22:41 +0800 | [diff] [blame] | 699 | if (__supported_pte_mask & _PAGE_NX) |
| 700 | runtime_code_page_mkexec(); |
Huang, Ying | a382806 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 701 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); |
| 702 | memmap.map = NULL; |
Matthew Garrett | 7cb00b7 | 2011-05-05 15:19:45 -0400 | [diff] [blame] | 703 | kfree(new_memmap); |
Huang, Ying | 5b83683 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Convenience functions to obtain memory types and attributes |
| 708 | */ |
| 709 | u32 efi_mem_type(unsigned long phys_addr) |
| 710 | { |
| 711 | efi_memory_desc_t *md; |
| 712 | void *p; |
| 713 | |
| 714 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 715 | md = p; |
| 716 | if ((md->phys_addr <= phys_addr) && |
| 717 | (phys_addr < (md->phys_addr + |
| 718 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 719 | return md->type; |
| 720 | } |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | u64 efi_mem_attributes(unsigned long phys_addr) |
| 725 | { |
| 726 | efi_memory_desc_t *md; |
| 727 | void *p; |
| 728 | |
| 729 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 730 | md = p; |
| 731 | if ((md->phys_addr <= phys_addr) && |
| 732 | (phys_addr < (md->phys_addr + |
| 733 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 734 | return md->attribute; |
| 735 | } |
| 736 | return 0; |
| 737 | } |