treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / ia64 / kernel / efi.c
blob0a34dcc435c6167add04c1a8fc0356fad8c0afa1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Extensible Firmware Interface
5 * Based on Extensible Firmware Interface Specification version 0.9
6 * April 30, 1999
8 * Copyright (C) 1999 VA Linux Systems
9 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
10 * Copyright (C) 1999-2003 Hewlett-Packard Co.
11 * David Mosberger-Tang <davidm@hpl.hp.com>
12 * Stephane Eranian <eranian@hpl.hp.com>
13 * (c) Copyright 2006 Hewlett-Packard Development Company, L.P.
14 * Bjorn Helgaas <bjorn.helgaas@hp.com>
16 * All EFI Runtime Services are not implemented yet as EFI only
17 * supports physical mode addressing on SoftSDV. This is to be fixed
18 * in a future version. --drummond 1999-07-20
20 * Implemented EFI runtime services and virtual mode calls. --davidm
22 * Goutham Rao: <goutham.rao@intel.com>
23 * Skip non-WB memory and ignore empty memory ranges.
25 #include <linux/module.h>
26 #include <linux/memblock.h>
27 #include <linux/crash_dump.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/efi.h>
34 #include <linux/kexec.h>
35 #include <linux/mm.h>
37 #include <asm/io.h>
38 #include <asm/kregs.h>
39 #include <asm/meminit.h>
40 #include <asm/pgtable.h>
41 #include <asm/processor.h>
42 #include <asm/mca.h>
43 #include <asm/setup.h>
44 #include <asm/tlbflush.h>
46 #define EFI_DEBUG 0
48 static __initdata unsigned long palo_phys;
50 unsigned long sal_systab_phys = EFI_INVALID_TABLE_ADDR;
52 static __initdata efi_config_table_type_t arch_tables[] = {
53 {PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID, "PALO", &palo_phys},
54 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &sal_systab_phys},
55 {NULL_GUID, NULL, 0},
58 extern efi_status_t efi_call_phys (void *, ...);
60 static efi_runtime_services_t *runtime;
61 static u64 mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
63 #define efi_call_virt(f, args...) (*(f))(args)
65 #define STUB_GET_TIME(prefix, adjust_arg) \
66 static efi_status_t \
67 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
68 { \
69 struct ia64_fpreg fr[6]; \
70 efi_time_cap_t *atc = NULL; \
71 efi_status_t ret; \
73 if (tc) \
74 atc = adjust_arg(tc); \
75 ia64_save_scratch_fpregs(fr); \
76 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
77 adjust_arg(tm), atc); \
78 ia64_load_scratch_fpregs(fr); \
79 return ret; \
82 #define STUB_SET_TIME(prefix, adjust_arg) \
83 static efi_status_t \
84 prefix##_set_time (efi_time_t *tm) \
85 { \
86 struct ia64_fpreg fr[6]; \
87 efi_status_t ret; \
89 ia64_save_scratch_fpregs(fr); \
90 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
91 adjust_arg(tm)); \
92 ia64_load_scratch_fpregs(fr); \
93 return ret; \
96 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
97 static efi_status_t \
98 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
99 efi_time_t *tm) \
101 struct ia64_fpreg fr[6]; \
102 efi_status_t ret; \
104 ia64_save_scratch_fpregs(fr); \
105 ret = efi_call_##prefix( \
106 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
107 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
108 ia64_load_scratch_fpregs(fr); \
109 return ret; \
112 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
113 static efi_status_t \
114 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
116 struct ia64_fpreg fr[6]; \
117 efi_time_t *atm = NULL; \
118 efi_status_t ret; \
120 if (tm) \
121 atm = adjust_arg(tm); \
122 ia64_save_scratch_fpregs(fr); \
123 ret = efi_call_##prefix( \
124 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
125 enabled, atm); \
126 ia64_load_scratch_fpregs(fr); \
127 return ret; \
130 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
131 static efi_status_t \
132 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
133 unsigned long *data_size, void *data) \
135 struct ia64_fpreg fr[6]; \
136 u32 *aattr = NULL; \
137 efi_status_t ret; \
139 if (attr) \
140 aattr = adjust_arg(attr); \
141 ia64_save_scratch_fpregs(fr); \
142 ret = efi_call_##prefix( \
143 (efi_get_variable_t *) __va(runtime->get_variable), \
144 adjust_arg(name), adjust_arg(vendor), aattr, \
145 adjust_arg(data_size), adjust_arg(data)); \
146 ia64_load_scratch_fpregs(fr); \
147 return ret; \
150 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
151 static efi_status_t \
152 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
153 efi_guid_t *vendor) \
155 struct ia64_fpreg fr[6]; \
156 efi_status_t ret; \
158 ia64_save_scratch_fpregs(fr); \
159 ret = efi_call_##prefix( \
160 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
161 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
162 ia64_load_scratch_fpregs(fr); \
163 return ret; \
166 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
167 static efi_status_t \
168 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
169 u32 attr, unsigned long data_size, \
170 void *data) \
172 struct ia64_fpreg fr[6]; \
173 efi_status_t ret; \
175 ia64_save_scratch_fpregs(fr); \
176 ret = efi_call_##prefix( \
177 (efi_set_variable_t *) __va(runtime->set_variable), \
178 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
179 adjust_arg(data)); \
180 ia64_load_scratch_fpregs(fr); \
181 return ret; \
184 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
185 static efi_status_t \
186 prefix##_get_next_high_mono_count (u32 *count) \
188 struct ia64_fpreg fr[6]; \
189 efi_status_t ret; \
191 ia64_save_scratch_fpregs(fr); \
192 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
193 __va(runtime->get_next_high_mono_count), \
194 adjust_arg(count)); \
195 ia64_load_scratch_fpregs(fr); \
196 return ret; \
199 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
200 static void \
201 prefix##_reset_system (int reset_type, efi_status_t status, \
202 unsigned long data_size, efi_char16_t *data) \
204 struct ia64_fpreg fr[6]; \
205 efi_char16_t *adata = NULL; \
207 if (data) \
208 adata = adjust_arg(data); \
210 ia64_save_scratch_fpregs(fr); \
211 efi_call_##prefix( \
212 (efi_reset_system_t *) __va(runtime->reset_system), \
213 reset_type, status, data_size, adata); \
214 /* should not return, but just in case... */ \
215 ia64_load_scratch_fpregs(fr); \
218 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
220 STUB_GET_TIME(phys, phys_ptr)
221 STUB_SET_TIME(phys, phys_ptr)
222 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
223 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
224 STUB_GET_VARIABLE(phys, phys_ptr)
225 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
226 STUB_SET_VARIABLE(phys, phys_ptr)
227 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
228 STUB_RESET_SYSTEM(phys, phys_ptr)
230 #define id(arg) arg
232 STUB_GET_TIME(virt, id)
233 STUB_SET_TIME(virt, id)
234 STUB_GET_WAKEUP_TIME(virt, id)
235 STUB_SET_WAKEUP_TIME(virt, id)
236 STUB_GET_VARIABLE(virt, id)
237 STUB_GET_NEXT_VARIABLE(virt, id)
238 STUB_SET_VARIABLE(virt, id)
239 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
240 STUB_RESET_SYSTEM(virt, id)
242 void
243 efi_gettimeofday (struct timespec64 *ts)
245 efi_time_t tm;
247 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
248 memset(ts, 0, sizeof(*ts));
249 return;
252 ts->tv_sec = mktime64(tm.year, tm.month, tm.day,
253 tm.hour, tm.minute, tm.second);
254 ts->tv_nsec = tm.nanosecond;
257 static int
258 is_memory_available (efi_memory_desc_t *md)
260 if (!(md->attribute & EFI_MEMORY_WB))
261 return 0;
263 switch (md->type) {
264 case EFI_LOADER_CODE:
265 case EFI_LOADER_DATA:
266 case EFI_BOOT_SERVICES_CODE:
267 case EFI_BOOT_SERVICES_DATA:
268 case EFI_CONVENTIONAL_MEMORY:
269 return 1;
271 return 0;
274 typedef struct kern_memdesc {
275 u64 attribute;
276 u64 start;
277 u64 num_pages;
278 } kern_memdesc_t;
280 static kern_memdesc_t *kern_memmap;
282 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
284 static inline u64
285 kmd_end(kern_memdesc_t *kmd)
287 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
290 static inline u64
291 efi_md_end(efi_memory_desc_t *md)
293 return (md->phys_addr + efi_md_size(md));
296 static inline int
297 efi_wb(efi_memory_desc_t *md)
299 return (md->attribute & EFI_MEMORY_WB);
302 static inline int
303 efi_uc(efi_memory_desc_t *md)
305 return (md->attribute & EFI_MEMORY_UC);
308 static void
309 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
311 kern_memdesc_t *k;
312 u64 start, end, voff;
314 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
315 for (k = kern_memmap; k->start != ~0UL; k++) {
316 if (k->attribute != attr)
317 continue;
318 start = PAGE_ALIGN(k->start);
319 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
320 if (start < end)
321 if ((*callback)(start + voff, end + voff, arg) < 0)
322 return;
327 * Walk the EFI memory map and call CALLBACK once for each EFI memory
328 * descriptor that has memory that is available for OS use.
330 void
331 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
333 walk(callback, arg, EFI_MEMORY_WB);
337 * Walk the EFI memory map and call CALLBACK once for each EFI memory
338 * descriptor that has memory that is available for uncached allocator.
340 void
341 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
343 walk(callback, arg, EFI_MEMORY_UC);
347 * Look for the PAL_CODE region reported by EFI and map it using an
348 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
349 * Abstraction Layer chapter 11 in ADAG
351 void *
352 efi_get_pal_addr (void)
354 void *efi_map_start, *efi_map_end, *p;
355 efi_memory_desc_t *md;
356 u64 efi_desc_size;
357 int pal_code_count = 0;
358 u64 vaddr, mask;
360 efi_map_start = __va(ia64_boot_param->efi_memmap);
361 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
362 efi_desc_size = ia64_boot_param->efi_memdesc_size;
364 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
365 md = p;
366 if (md->type != EFI_PAL_CODE)
367 continue;
369 if (++pal_code_count > 1) {
370 printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
371 "dropped @ %llx\n", md->phys_addr);
372 continue;
375 * The only ITLB entry in region 7 that is used is the one
376 * installed by __start(). That entry covers a 64MB range.
378 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
379 vaddr = PAGE_OFFSET + md->phys_addr;
382 * We must check that the PAL mapping won't overlap with the
383 * kernel mapping.
385 * PAL code is guaranteed to be aligned on a power of 2 between
386 * 4k and 256KB and that only one ITR is needed to map it. This
387 * implies that the PAL code is always aligned on its size,
388 * i.e., the closest matching page size supported by the TLB.
389 * Therefore PAL code is guaranteed never to cross a 64MB unless
390 * it is bigger than 64MB (very unlikely!). So for now the
391 * following test is enough to determine whether or not we need
392 * a dedicated ITR for the PAL code.
394 if ((vaddr & mask) == (KERNEL_START & mask)) {
395 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
396 __func__);
397 continue;
400 if (efi_md_size(md) > IA64_GRANULE_SIZE)
401 panic("Whoa! PAL code size bigger than a granule!");
403 #if EFI_DEBUG
404 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
406 printk(KERN_INFO "CPU %d: mapping PAL code "
407 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
408 smp_processor_id(), md->phys_addr,
409 md->phys_addr + efi_md_size(md),
410 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
411 #endif
412 return __va(md->phys_addr);
414 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
415 __func__);
416 return NULL;
420 static u8 __init palo_checksum(u8 *buffer, u32 length)
422 u8 sum = 0;
423 u8 *end = buffer + length;
425 while (buffer < end)
426 sum = (u8) (sum + *(buffer++));
428 return sum;
432 * Parse and handle PALO table which is published at:
433 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
435 static void __init handle_palo(unsigned long phys_addr)
437 struct palo_table *palo = __va(phys_addr);
438 u8 checksum;
440 if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) {
441 printk(KERN_INFO "PALO signature incorrect.\n");
442 return;
445 checksum = palo_checksum((u8 *)palo, palo->length);
446 if (checksum) {
447 printk(KERN_INFO "PALO checksum incorrect.\n");
448 return;
451 setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO);
454 void
455 efi_map_pal_code (void)
457 void *pal_vaddr = efi_get_pal_addr ();
458 u64 psr;
460 if (!pal_vaddr)
461 return;
464 * Cannot write to CRx with PSR.ic=1
466 psr = ia64_clear_ic();
467 ia64_itr(0x1, IA64_TR_PALCODE,
468 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
469 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
470 IA64_GRANULE_SHIFT);
471 ia64_set_psr(psr); /* restore psr */
474 void __init
475 efi_init (void)
477 void *efi_map_start, *efi_map_end;
478 efi_char16_t *c16;
479 u64 efi_desc_size;
480 char *cp, vendor[100] = "unknown";
481 int i;
483 set_bit(EFI_BOOT, &efi.flags);
484 set_bit(EFI_64BIT, &efi.flags);
487 * It's too early to be able to use the standard kernel command line
488 * support...
490 for (cp = boot_command_line; *cp; ) {
491 if (memcmp(cp, "mem=", 4) == 0) {
492 mem_limit = memparse(cp + 4, &cp);
493 } else if (memcmp(cp, "max_addr=", 9) == 0) {
494 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
495 } else if (memcmp(cp, "min_addr=", 9) == 0) {
496 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
497 } else {
498 while (*cp != ' ' && *cp)
499 ++cp;
500 while (*cp == ' ')
501 ++cp;
504 if (min_addr != 0UL)
505 printk(KERN_INFO "Ignoring memory below %lluMB\n",
506 min_addr >> 20);
507 if (max_addr != ~0UL)
508 printk(KERN_INFO "Ignoring memory above %lluMB\n",
509 max_addr >> 20);
511 efi.systab = __va(ia64_boot_param->efi_systab);
514 * Verify the EFI Table
516 if (efi.systab == NULL)
517 panic("Whoa! Can't find EFI system table.\n");
518 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
519 panic("Whoa! EFI system table signature incorrect\n");
520 if ((efi.systab->hdr.revision >> 16) == 0)
521 printk(KERN_WARNING "Warning: EFI system table version "
522 "%d.%02d, expected 1.00 or greater\n",
523 efi.systab->hdr.revision >> 16,
524 efi.systab->hdr.revision & 0xffff);
526 /* Show what we know for posterity */
527 c16 = __va(efi.systab->fw_vendor);
528 if (c16) {
529 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
530 vendor[i] = *c16++;
531 vendor[i] = '\0';
534 printk(KERN_INFO "EFI v%u.%.02u by %s:",
535 efi.systab->hdr.revision >> 16,
536 efi.systab->hdr.revision & 0xffff, vendor);
538 palo_phys = EFI_INVALID_TABLE_ADDR;
540 if (efi_config_init(arch_tables) != 0)
541 return;
543 if (palo_phys != EFI_INVALID_TABLE_ADDR)
544 handle_palo(palo_phys);
546 runtime = __va(efi.systab->runtime);
547 efi.get_time = phys_get_time;
548 efi.set_time = phys_set_time;
549 efi.get_wakeup_time = phys_get_wakeup_time;
550 efi.set_wakeup_time = phys_set_wakeup_time;
551 efi.get_variable = phys_get_variable;
552 efi.get_next_variable = phys_get_next_variable;
553 efi.set_variable = phys_set_variable;
554 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
555 efi.reset_system = phys_reset_system;
557 efi_map_start = __va(ia64_boot_param->efi_memmap);
558 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
559 efi_desc_size = ia64_boot_param->efi_memdesc_size;
561 #if EFI_DEBUG
562 /* print EFI memory map: */
564 efi_memory_desc_t *md;
565 void *p;
567 for (i = 0, p = efi_map_start; p < efi_map_end;
568 ++i, p += efi_desc_size)
570 const char *unit;
571 unsigned long size;
572 char buf[64];
574 md = p;
575 size = md->num_pages << EFI_PAGE_SHIFT;
577 if ((size >> 40) > 0) {
578 size >>= 40;
579 unit = "TB";
580 } else if ((size >> 30) > 0) {
581 size >>= 30;
582 unit = "GB";
583 } else if ((size >> 20) > 0) {
584 size >>= 20;
585 unit = "MB";
586 } else {
587 size >>= 10;
588 unit = "KB";
591 printk("mem%02d: %s "
592 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
593 i, efi_md_typeattr_format(buf, sizeof(buf), md),
594 md->phys_addr,
595 md->phys_addr + efi_md_size(md), size, unit);
598 #endif
600 efi_map_pal_code();
601 efi_enter_virtual_mode();
604 void
605 efi_enter_virtual_mode (void)
607 void *efi_map_start, *efi_map_end, *p;
608 efi_memory_desc_t *md;
609 efi_status_t status;
610 u64 efi_desc_size;
612 efi_map_start = __va(ia64_boot_param->efi_memmap);
613 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
614 efi_desc_size = ia64_boot_param->efi_memdesc_size;
616 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
617 md = p;
618 if (md->attribute & EFI_MEMORY_RUNTIME) {
620 * Some descriptors have multiple bits set, so the
621 * order of the tests is relevant.
623 if (md->attribute & EFI_MEMORY_WB) {
624 md->virt_addr = (u64) __va(md->phys_addr);
625 } else if (md->attribute & EFI_MEMORY_UC) {
626 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
627 } else if (md->attribute & EFI_MEMORY_WC) {
628 #if 0
629 md->virt_addr = ia64_remap(md->phys_addr,
630 (_PAGE_A |
631 _PAGE_P |
632 _PAGE_D |
633 _PAGE_MA_WC |
634 _PAGE_PL_0 |
635 _PAGE_AR_RW));
636 #else
637 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
638 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
639 #endif
640 } else if (md->attribute & EFI_MEMORY_WT) {
641 #if 0
642 md->virt_addr = ia64_remap(md->phys_addr,
643 (_PAGE_A |
644 _PAGE_P |
645 _PAGE_D |
646 _PAGE_MA_WT |
647 _PAGE_PL_0 |
648 _PAGE_AR_RW));
649 #else
650 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
651 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
652 #endif
657 status = efi_call_phys(__va(runtime->set_virtual_address_map),
658 ia64_boot_param->efi_memmap_size,
659 efi_desc_size,
660 ia64_boot_param->efi_memdesc_version,
661 ia64_boot_param->efi_memmap);
662 if (status != EFI_SUCCESS) {
663 printk(KERN_WARNING "warning: unable to switch EFI into "
664 "virtual mode (status=%lu)\n", status);
665 return;
668 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
671 * Now that EFI is in virtual mode, we call the EFI functions more
672 * efficiently:
674 efi.get_time = virt_get_time;
675 efi.set_time = virt_set_time;
676 efi.get_wakeup_time = virt_get_wakeup_time;
677 efi.set_wakeup_time = virt_set_wakeup_time;
678 efi.get_variable = virt_get_variable;
679 efi.get_next_variable = virt_get_next_variable;
680 efi.set_variable = virt_set_variable;
681 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
682 efi.reset_system = virt_reset_system;
686 * Walk the EFI memory map looking for the I/O port range. There can only be
687 * one entry of this type, other I/O port ranges should be described via ACPI.
690 efi_get_iobase (void)
692 void *efi_map_start, *efi_map_end, *p;
693 efi_memory_desc_t *md;
694 u64 efi_desc_size;
696 efi_map_start = __va(ia64_boot_param->efi_memmap);
697 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
698 efi_desc_size = ia64_boot_param->efi_memdesc_size;
700 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
701 md = p;
702 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
703 if (md->attribute & EFI_MEMORY_UC)
704 return md->phys_addr;
707 return 0;
710 static struct kern_memdesc *
711 kern_memory_descriptor (unsigned long phys_addr)
713 struct kern_memdesc *md;
715 for (md = kern_memmap; md->start != ~0UL; md++) {
716 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
717 return md;
719 return NULL;
722 static efi_memory_desc_t *
723 efi_memory_descriptor (unsigned long phys_addr)
725 void *efi_map_start, *efi_map_end, *p;
726 efi_memory_desc_t *md;
727 u64 efi_desc_size;
729 efi_map_start = __va(ia64_boot_param->efi_memmap);
730 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
731 efi_desc_size = ia64_boot_param->efi_memdesc_size;
733 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
734 md = p;
736 if (phys_addr - md->phys_addr < efi_md_size(md))
737 return md;
739 return NULL;
742 static int
743 efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
745 void *efi_map_start, *efi_map_end, *p;
746 efi_memory_desc_t *md;
747 u64 efi_desc_size;
748 unsigned long end;
750 efi_map_start = __va(ia64_boot_param->efi_memmap);
751 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
752 efi_desc_size = ia64_boot_param->efi_memdesc_size;
754 end = phys_addr + size;
756 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
757 md = p;
758 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
759 return 1;
761 return 0;
765 efi_mem_type (unsigned long phys_addr)
767 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
769 if (md)
770 return md->type;
771 return -EINVAL;
775 efi_mem_attributes (unsigned long phys_addr)
777 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
779 if (md)
780 return md->attribute;
781 return 0;
783 EXPORT_SYMBOL(efi_mem_attributes);
786 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
788 unsigned long end = phys_addr + size;
789 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
790 u64 attr;
792 if (!md)
793 return 0;
796 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
797 * the kernel that firmware needs this region mapped.
799 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
800 do {
801 unsigned long md_end = efi_md_end(md);
803 if (end <= md_end)
804 return attr;
806 md = efi_memory_descriptor(md_end);
807 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
808 return 0;
809 } while (md);
810 return 0; /* never reached */
814 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
816 unsigned long end = phys_addr + size;
817 struct kern_memdesc *md;
818 u64 attr;
821 * This is a hack for ioremap calls before we set up kern_memmap.
822 * Maybe we should do efi_memmap_init() earlier instead.
824 if (!kern_memmap) {
825 attr = efi_mem_attribute(phys_addr, size);
826 if (attr & EFI_MEMORY_WB)
827 return EFI_MEMORY_WB;
828 return 0;
831 md = kern_memory_descriptor(phys_addr);
832 if (!md)
833 return 0;
835 attr = md->attribute;
836 do {
837 unsigned long md_end = kmd_end(md);
839 if (end <= md_end)
840 return attr;
842 md = kern_memory_descriptor(md_end);
843 if (!md || md->attribute != attr)
844 return 0;
845 } while (md);
846 return 0; /* never reached */
850 valid_phys_addr_range (phys_addr_t phys_addr, unsigned long size)
852 u64 attr;
855 * /dev/mem reads and writes use copy_to_user(), which implicitly
856 * uses a granule-sized kernel identity mapping. It's really
857 * only safe to do this for regions in kern_memmap. For more
858 * details, see Documentation/ia64/aliasing.rst.
860 attr = kern_mem_attribute(phys_addr, size);
861 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
862 return 1;
863 return 0;
867 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
869 unsigned long phys_addr = pfn << PAGE_SHIFT;
870 u64 attr;
872 attr = efi_mem_attribute(phys_addr, size);
875 * /dev/mem mmap uses normal user pages, so we don't need the entire
876 * granule, but the entire region we're mapping must support the same
877 * attribute.
879 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
880 return 1;
883 * Intel firmware doesn't tell us about all the MMIO regions, so
884 * in general we have to allow mmap requests. But if EFI *does*
885 * tell us about anything inside this region, we should deny it.
886 * The user can always map a smaller region to avoid the overlap.
888 if (efi_memmap_intersects(phys_addr, size))
889 return 0;
891 return 1;
894 pgprot_t
895 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
896 pgprot_t vma_prot)
898 unsigned long phys_addr = pfn << PAGE_SHIFT;
899 u64 attr;
902 * For /dev/mem mmap, we use user mappings, but if the region is
903 * in kern_memmap (and hence may be covered by a kernel mapping),
904 * we must use the same attribute as the kernel mapping.
906 attr = kern_mem_attribute(phys_addr, size);
907 if (attr & EFI_MEMORY_WB)
908 return pgprot_cacheable(vma_prot);
909 else if (attr & EFI_MEMORY_UC)
910 return pgprot_noncached(vma_prot);
913 * Some chipsets don't support UC access to memory. If
914 * WB is supported, we prefer that.
916 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
917 return pgprot_cacheable(vma_prot);
919 return pgprot_noncached(vma_prot);
922 int __init
923 efi_uart_console_only(void)
925 efi_status_t status;
926 char *s, name[] = "ConOut";
927 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
928 efi_char16_t *utf16, name_utf16[32];
929 unsigned char data[1024];
930 unsigned long size = sizeof(data);
931 struct efi_generic_dev_path *hdr, *end_addr;
932 int uart = 0;
934 /* Convert to UTF-16 */
935 utf16 = name_utf16;
936 s = name;
937 while (*s)
938 *utf16++ = *s++ & 0x7f;
939 *utf16 = 0;
941 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
942 if (status != EFI_SUCCESS) {
943 printk(KERN_ERR "No EFI %s variable?\n", name);
944 return 0;
947 hdr = (struct efi_generic_dev_path *) data;
948 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
949 while (hdr < end_addr) {
950 if (hdr->type == EFI_DEV_MSG &&
951 hdr->sub_type == EFI_DEV_MSG_UART)
952 uart = 1;
953 else if (hdr->type == EFI_DEV_END_PATH ||
954 hdr->type == EFI_DEV_END_PATH2) {
955 if (!uart)
956 return 0;
957 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
958 return 1;
959 uart = 0;
961 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
963 printk(KERN_ERR "Malformed %s value\n", name);
964 return 0;
968 * Look for the first granule aligned memory descriptor memory
969 * that is big enough to hold EFI memory map. Make sure this
970 * descriptor is at least granule sized so it does not get trimmed
972 struct kern_memdesc *
973 find_memmap_space (void)
975 u64 contig_low=0, contig_high=0;
976 u64 as = 0, ae;
977 void *efi_map_start, *efi_map_end, *p, *q;
978 efi_memory_desc_t *md, *pmd = NULL, *check_md;
979 u64 space_needed, efi_desc_size;
980 unsigned long total_mem = 0;
982 efi_map_start = __va(ia64_boot_param->efi_memmap);
983 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
984 efi_desc_size = ia64_boot_param->efi_memdesc_size;
987 * Worst case: we need 3 kernel descriptors for each efi descriptor
988 * (if every entry has a WB part in the middle, and UC head and tail),
989 * plus one for the end marker.
991 space_needed = sizeof(kern_memdesc_t) *
992 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
994 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
995 md = p;
996 if (!efi_wb(md)) {
997 continue;
999 if (pmd == NULL || !efi_wb(pmd) ||
1000 efi_md_end(pmd) != md->phys_addr) {
1001 contig_low = GRANULEROUNDUP(md->phys_addr);
1002 contig_high = efi_md_end(md);
1003 for (q = p + efi_desc_size; q < efi_map_end;
1004 q += efi_desc_size) {
1005 check_md = q;
1006 if (!efi_wb(check_md))
1007 break;
1008 if (contig_high != check_md->phys_addr)
1009 break;
1010 contig_high = efi_md_end(check_md);
1012 contig_high = GRANULEROUNDDOWN(contig_high);
1014 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
1015 continue;
1017 /* Round ends inward to granule boundaries */
1018 as = max(contig_low, md->phys_addr);
1019 ae = min(contig_high, efi_md_end(md));
1021 /* keep within max_addr= and min_addr= command line arg */
1022 as = max(as, min_addr);
1023 ae = min(ae, max_addr);
1024 if (ae <= as)
1025 continue;
1027 /* avoid going over mem= command line arg */
1028 if (total_mem + (ae - as) > mem_limit)
1029 ae -= total_mem + (ae - as) - mem_limit;
1031 if (ae <= as)
1032 continue;
1034 if (ae - as > space_needed)
1035 break;
1037 if (p >= efi_map_end)
1038 panic("Can't allocate space for kernel memory descriptors");
1040 return __va(as);
1044 * Walk the EFI memory map and gather all memory available for kernel
1045 * to use. We can allocate partial granules only if the unavailable
1046 * parts exist, and are WB.
1048 unsigned long
1049 efi_memmap_init(u64 *s, u64 *e)
1051 struct kern_memdesc *k, *prev = NULL;
1052 u64 contig_low=0, contig_high=0;
1053 u64 as, ae, lim;
1054 void *efi_map_start, *efi_map_end, *p, *q;
1055 efi_memory_desc_t *md, *pmd = NULL, *check_md;
1056 u64 efi_desc_size;
1057 unsigned long total_mem = 0;
1059 k = kern_memmap = find_memmap_space();
1061 efi_map_start = __va(ia64_boot_param->efi_memmap);
1062 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1063 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1065 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1066 md = p;
1067 if (!efi_wb(md)) {
1068 if (efi_uc(md) &&
1069 (md->type == EFI_CONVENTIONAL_MEMORY ||
1070 md->type == EFI_BOOT_SERVICES_DATA)) {
1071 k->attribute = EFI_MEMORY_UC;
1072 k->start = md->phys_addr;
1073 k->num_pages = md->num_pages;
1074 k++;
1076 continue;
1078 if (pmd == NULL || !efi_wb(pmd) ||
1079 efi_md_end(pmd) != md->phys_addr) {
1080 contig_low = GRANULEROUNDUP(md->phys_addr);
1081 contig_high = efi_md_end(md);
1082 for (q = p + efi_desc_size; q < efi_map_end;
1083 q += efi_desc_size) {
1084 check_md = q;
1085 if (!efi_wb(check_md))
1086 break;
1087 if (contig_high != check_md->phys_addr)
1088 break;
1089 contig_high = efi_md_end(check_md);
1091 contig_high = GRANULEROUNDDOWN(contig_high);
1093 if (!is_memory_available(md))
1094 continue;
1097 * Round ends inward to granule boundaries
1098 * Give trimmings to uncached allocator
1100 if (md->phys_addr < contig_low) {
1101 lim = min(efi_md_end(md), contig_low);
1102 if (efi_uc(md)) {
1103 if (k > kern_memmap &&
1104 (k-1)->attribute == EFI_MEMORY_UC &&
1105 kmd_end(k-1) == md->phys_addr) {
1106 (k-1)->num_pages +=
1107 (lim - md->phys_addr)
1108 >> EFI_PAGE_SHIFT;
1109 } else {
1110 k->attribute = EFI_MEMORY_UC;
1111 k->start = md->phys_addr;
1112 k->num_pages = (lim - md->phys_addr)
1113 >> EFI_PAGE_SHIFT;
1114 k++;
1117 as = contig_low;
1118 } else
1119 as = md->phys_addr;
1121 if (efi_md_end(md) > contig_high) {
1122 lim = max(md->phys_addr, contig_high);
1123 if (efi_uc(md)) {
1124 if (lim == md->phys_addr && k > kern_memmap &&
1125 (k-1)->attribute == EFI_MEMORY_UC &&
1126 kmd_end(k-1) == md->phys_addr) {
1127 (k-1)->num_pages += md->num_pages;
1128 } else {
1129 k->attribute = EFI_MEMORY_UC;
1130 k->start = lim;
1131 k->num_pages = (efi_md_end(md) - lim)
1132 >> EFI_PAGE_SHIFT;
1133 k++;
1136 ae = contig_high;
1137 } else
1138 ae = efi_md_end(md);
1140 /* keep within max_addr= and min_addr= command line arg */
1141 as = max(as, min_addr);
1142 ae = min(ae, max_addr);
1143 if (ae <= as)
1144 continue;
1146 /* avoid going over mem= command line arg */
1147 if (total_mem + (ae - as) > mem_limit)
1148 ae -= total_mem + (ae - as) - mem_limit;
1150 if (ae <= as)
1151 continue;
1152 if (prev && kmd_end(prev) == md->phys_addr) {
1153 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1154 total_mem += ae - as;
1155 continue;
1157 k->attribute = EFI_MEMORY_WB;
1158 k->start = as;
1159 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1160 total_mem += ae - as;
1161 prev = k++;
1163 k->start = ~0L; /* end-marker */
1165 /* reserve the memory we are using for kern_memmap */
1166 *s = (u64)kern_memmap;
1167 *e = (u64)++k;
1169 return total_mem;
1172 void
1173 efi_initialize_iomem_resources(struct resource *code_resource,
1174 struct resource *data_resource,
1175 struct resource *bss_resource)
1177 struct resource *res;
1178 void *efi_map_start, *efi_map_end, *p;
1179 efi_memory_desc_t *md;
1180 u64 efi_desc_size;
1181 char *name;
1182 unsigned long flags, desc;
1184 efi_map_start = __va(ia64_boot_param->efi_memmap);
1185 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1186 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1188 res = NULL;
1190 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1191 md = p;
1193 if (md->num_pages == 0) /* should not happen */
1194 continue;
1196 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1197 desc = IORES_DESC_NONE;
1199 switch (md->type) {
1201 case EFI_MEMORY_MAPPED_IO:
1202 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1203 continue;
1205 case EFI_LOADER_CODE:
1206 case EFI_LOADER_DATA:
1207 case EFI_BOOT_SERVICES_DATA:
1208 case EFI_BOOT_SERVICES_CODE:
1209 case EFI_CONVENTIONAL_MEMORY:
1210 if (md->attribute & EFI_MEMORY_WP) {
1211 name = "System ROM";
1212 flags |= IORESOURCE_READONLY;
1213 } else if (md->attribute == EFI_MEMORY_UC) {
1214 name = "Uncached RAM";
1215 } else {
1216 name = "System RAM";
1217 flags |= IORESOURCE_SYSRAM;
1219 break;
1221 case EFI_ACPI_MEMORY_NVS:
1222 name = "ACPI Non-volatile Storage";
1223 desc = IORES_DESC_ACPI_NV_STORAGE;
1224 break;
1226 case EFI_UNUSABLE_MEMORY:
1227 name = "reserved";
1228 flags |= IORESOURCE_DISABLED;
1229 break;
1231 case EFI_PERSISTENT_MEMORY:
1232 name = "Persistent Memory";
1233 desc = IORES_DESC_PERSISTENT_MEMORY;
1234 break;
1236 case EFI_RESERVED_TYPE:
1237 case EFI_RUNTIME_SERVICES_CODE:
1238 case EFI_RUNTIME_SERVICES_DATA:
1239 case EFI_ACPI_RECLAIM_MEMORY:
1240 default:
1241 name = "reserved";
1242 break;
1245 if ((res = kzalloc(sizeof(struct resource),
1246 GFP_KERNEL)) == NULL) {
1247 printk(KERN_ERR
1248 "failed to allocate resource for iomem\n");
1249 return;
1252 res->name = name;
1253 res->start = md->phys_addr;
1254 res->end = md->phys_addr + efi_md_size(md) - 1;
1255 res->flags = flags;
1256 res->desc = desc;
1258 if (insert_resource(&iomem_resource, res) < 0)
1259 kfree(res);
1260 else {
1262 * We don't know which region contains
1263 * kernel data so we try it repeatedly and
1264 * let the resource manager test it.
1266 insert_resource(res, code_resource);
1267 insert_resource(res, data_resource);
1268 insert_resource(res, bss_resource);
1269 #ifdef CONFIG_KEXEC
1270 insert_resource(res, &efi_memmap_res);
1271 insert_resource(res, &boot_param_res);
1272 if (crashk_res.end > crashk_res.start)
1273 insert_resource(res, &crashk_res);
1274 #endif
1279 #ifdef CONFIG_KEXEC
1280 /* find a block of memory aligned to 64M exclude reserved regions
1281 rsvd_regions are sorted
1283 unsigned long __init
1284 kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
1286 int i;
1287 u64 start, end;
1288 u64 alignment = 1UL << _PAGE_SIZE_64M;
1289 void *efi_map_start, *efi_map_end, *p;
1290 efi_memory_desc_t *md;
1291 u64 efi_desc_size;
1293 efi_map_start = __va(ia64_boot_param->efi_memmap);
1294 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1295 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1297 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1298 md = p;
1299 if (!efi_wb(md))
1300 continue;
1301 start = ALIGN(md->phys_addr, alignment);
1302 end = efi_md_end(md);
1303 for (i = 0; i < n; i++) {
1304 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1305 if (__pa(r[i].start) > start + size)
1306 return start;
1307 start = ALIGN(__pa(r[i].end), alignment);
1308 if (i < n-1 &&
1309 __pa(r[i+1].start) < start + size)
1310 continue;
1311 else
1312 break;
1315 if (end > start + size)
1316 return start;
1319 printk(KERN_WARNING
1320 "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1321 return ~0UL;
1323 #endif
1325 #ifdef CONFIG_CRASH_DUMP
1326 /* locate the size find a the descriptor at a certain address */
1327 unsigned long __init
1328 vmcore_find_descriptor_size (unsigned long address)
1330 void *efi_map_start, *efi_map_end, *p;
1331 efi_memory_desc_t *md;
1332 u64 efi_desc_size;
1333 unsigned long ret = 0;
1335 efi_map_start = __va(ia64_boot_param->efi_memmap);
1336 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1337 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1339 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1340 md = p;
1341 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1342 && md->phys_addr == address) {
1343 ret = efi_md_size(md);
1344 break;
1348 if (ret == 0)
1349 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1351 return ret;
1353 #endif