2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 0.9 April 30, 1999
6 * Copyright (C) 1999 VA Linux Systems
7 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8 * Copyright (C) 1999-2003 Hewlett-Packard Co.
9 * David Mosberger-Tang <davidm@hpl.hp.com>
10 * Stephane Eranian <eranian@hpl.hp.com>
12 * All EFI Runtime Services are not implemented yet as EFI only
13 * supports physical mode addressing on SoftSDV. This is to be fixed
14 * in a future version. --drummond 1999-07-20
16 * Implemented EFI runtime services and virtual mode calls. --davidm
18 * Goutham Rao: <goutham.rao@intel.com>
19 * Skip non-WB memory and ignore empty memory ranges.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/time.h>
27 #include <linux/efi.h>
30 #include <asm/kregs.h>
31 #include <asm/meminit.h>
32 #include <asm/pgtable.h>
33 #include <asm/processor.h>
38 extern efi_status_t
efi_call_phys (void *, ...);
42 static efi_runtime_services_t
*runtime
;
43 static unsigned long mem_limit
= ~0UL, max_addr
= ~0UL;
45 #define efi_call_virt(f, args...) (*(f))(args)
47 #define STUB_GET_TIME(prefix, adjust_arg) \
49 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
51 struct ia64_fpreg fr[6]; \
52 efi_time_cap_t *atc = NULL; \
56 atc = adjust_arg(tc); \
57 ia64_save_scratch_fpregs(fr); \
58 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
59 ia64_load_scratch_fpregs(fr); \
63 #define STUB_SET_TIME(prefix, adjust_arg) \
65 prefix##_set_time (efi_time_t *tm) \
67 struct ia64_fpreg fr[6]; \
70 ia64_save_scratch_fpregs(fr); \
71 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
72 ia64_load_scratch_fpregs(fr); \
76 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
78 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
80 struct ia64_fpreg fr[6]; \
83 ia64_save_scratch_fpregs(fr); \
84 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
85 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
86 ia64_load_scratch_fpregs(fr); \
90 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
92 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
94 struct ia64_fpreg fr[6]; \
95 efi_time_t *atm = NULL; \
99 atm = adjust_arg(tm); \
100 ia64_save_scratch_fpregs(fr); \
101 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
103 ia64_load_scratch_fpregs(fr); \
107 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
108 static efi_status_t \
109 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
110 unsigned long *data_size, void *data) \
112 struct ia64_fpreg fr[6]; \
117 aattr = adjust_arg(attr); \
118 ia64_save_scratch_fpregs(fr); \
119 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
120 adjust_arg(name), adjust_arg(vendor), aattr, \
121 adjust_arg(data_size), adjust_arg(data)); \
122 ia64_load_scratch_fpregs(fr); \
126 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
127 static efi_status_t \
128 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
130 struct ia64_fpreg fr[6]; \
133 ia64_save_scratch_fpregs(fr); \
134 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
135 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
136 ia64_load_scratch_fpregs(fr); \
140 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
141 static efi_status_t \
142 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
143 unsigned long data_size, void *data) \
145 struct ia64_fpreg fr[6]; \
148 ia64_save_scratch_fpregs(fr); \
149 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
150 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
152 ia64_load_scratch_fpregs(fr); \
156 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
157 static efi_status_t \
158 prefix##_get_next_high_mono_count (u32 *count) \
160 struct ia64_fpreg fr[6]; \
163 ia64_save_scratch_fpregs(fr); \
164 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
165 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
166 ia64_load_scratch_fpregs(fr); \
170 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
172 prefix##_reset_system (int reset_type, efi_status_t status, \
173 unsigned long data_size, efi_char16_t *data) \
175 struct ia64_fpreg fr[6]; \
176 efi_char16_t *adata = NULL; \
179 adata = adjust_arg(data); \
181 ia64_save_scratch_fpregs(fr); \
182 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
183 reset_type, status, data_size, adata); \
184 /* should not return, but just in case... */ \
185 ia64_load_scratch_fpregs(fr); \
188 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
190 STUB_GET_TIME(phys
, phys_ptr
)
191 STUB_SET_TIME(phys
, phys_ptr
)
192 STUB_GET_WAKEUP_TIME(phys
, phys_ptr
)
193 STUB_SET_WAKEUP_TIME(phys
, phys_ptr
)
194 STUB_GET_VARIABLE(phys
, phys_ptr
)
195 STUB_GET_NEXT_VARIABLE(phys
, phys_ptr
)
196 STUB_SET_VARIABLE(phys
, phys_ptr
)
197 STUB_GET_NEXT_HIGH_MONO_COUNT(phys
, phys_ptr
)
198 STUB_RESET_SYSTEM(phys
, phys_ptr
)
202 STUB_GET_TIME(virt
, id
)
203 STUB_SET_TIME(virt
, id
)
204 STUB_GET_WAKEUP_TIME(virt
, id
)
205 STUB_SET_WAKEUP_TIME(virt
, id
)
206 STUB_GET_VARIABLE(virt
, id
)
207 STUB_GET_NEXT_VARIABLE(virt
, id
)
208 STUB_SET_VARIABLE(virt
, id
)
209 STUB_GET_NEXT_HIGH_MONO_COUNT(virt
, id
)
210 STUB_RESET_SYSTEM(virt
, id
)
213 efi_gettimeofday (struct timespec
*ts
)
217 memset(ts
, 0, sizeof(ts
));
218 if ((*efi
.get_time
)(&tm
, NULL
) != EFI_SUCCESS
)
221 ts
->tv_sec
= mktime(tm
.year
, tm
.month
, tm
.day
, tm
.hour
, tm
.minute
, tm
.second
);
222 ts
->tv_nsec
= tm
.nanosecond
;
226 is_available_memory (efi_memory_desc_t
*md
)
228 if (!(md
->attribute
& EFI_MEMORY_WB
))
232 case EFI_LOADER_CODE
:
233 case EFI_LOADER_DATA
:
234 case EFI_BOOT_SERVICES_CODE
:
235 case EFI_BOOT_SERVICES_DATA
:
236 case EFI_CONVENTIONAL_MEMORY
:
242 typedef struct kern_memdesc
{
248 static kern_memdesc_t
*kern_memmap
;
250 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
253 kmd_end(kern_memdesc_t
*kmd
)
255 return (kmd
->start
+ (kmd
->num_pages
<< EFI_PAGE_SHIFT
));
259 efi_md_end(efi_memory_desc_t
*md
)
261 return (md
->phys_addr
+ efi_md_size(md
));
265 efi_wb(efi_memory_desc_t
*md
)
267 return (md
->attribute
& EFI_MEMORY_WB
);
271 efi_uc(efi_memory_desc_t
*md
)
273 return (md
->attribute
& EFI_MEMORY_UC
);
277 walk (efi_freemem_callback_t callback
, void *arg
, u64 attr
)
280 u64 start
, end
, voff
;
282 voff
= (attr
== EFI_MEMORY_WB
) ? PAGE_OFFSET
: __IA64_UNCACHED_OFFSET
;
283 for (k
= kern_memmap
; k
->start
!= ~0UL; k
++) {
284 if (k
->attribute
!= attr
)
286 start
= PAGE_ALIGN(k
->start
);
287 end
= (k
->start
+ (k
->num_pages
<< EFI_PAGE_SHIFT
)) & PAGE_MASK
;
289 if ((*callback
)(start
+ voff
, end
+ voff
, arg
) < 0)
295 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
296 * has memory that is available for OS use.
299 efi_memmap_walk (efi_freemem_callback_t callback
, void *arg
)
301 walk(callback
, arg
, EFI_MEMORY_WB
);
305 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
306 * has memory that is available for uncached allocator.
309 efi_memmap_walk_uc (efi_freemem_callback_t callback
, void *arg
)
311 walk(callback
, arg
, EFI_MEMORY_UC
);
315 * Look for the PAL_CODE region reported by EFI and maps it using an
316 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
317 * Abstraction Layer chapter 11 in ADAG
321 efi_get_pal_addr (void)
323 void *efi_map_start
, *efi_map_end
, *p
;
324 efi_memory_desc_t
*md
;
326 int pal_code_count
= 0;
329 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
330 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
331 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
333 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
335 if (md
->type
!= EFI_PAL_CODE
)
338 if (++pal_code_count
> 1) {
339 printk(KERN_ERR
"Too many EFI Pal Code memory ranges, dropped @ %lx\n",
344 * The only ITLB entry in region 7 that is used is the one installed by
345 * __start(). That entry covers a 64MB range.
347 mask
= ~((1 << KERNEL_TR_PAGE_SHIFT
) - 1);
348 vaddr
= PAGE_OFFSET
+ md
->phys_addr
;
351 * We must check that the PAL mapping won't overlap with the kernel
354 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
355 * 256KB and that only one ITR is needed to map it. This implies that the
356 * PAL code is always aligned on its size, i.e., the closest matching page
357 * size supported by the TLB. Therefore PAL code is guaranteed never to
358 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
359 * now the following test is enough to determine whether or not we need a
360 * dedicated ITR for the PAL code.
362 if ((vaddr
& mask
) == (KERNEL_START
& mask
)) {
363 printk(KERN_INFO
"%s: no need to install ITR for PAL code\n",
368 if (md
->num_pages
<< EFI_PAGE_SHIFT
> IA64_GRANULE_SIZE
)
369 panic("Woah! PAL code size bigger than a granule!");
372 mask
= ~((1 << IA64_GRANULE_SHIFT
) - 1);
374 printk(KERN_INFO
"CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
375 smp_processor_id(), md
->phys_addr
,
376 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
377 vaddr
& mask
, (vaddr
& mask
) + IA64_GRANULE_SIZE
);
379 return __va(md
->phys_addr
);
381 printk(KERN_WARNING
"%s: no PAL-code memory-descriptor found",
387 efi_map_pal_code (void)
389 void *pal_vaddr
= efi_get_pal_addr ();
396 * Cannot write to CRx with PSR.ic=1
398 psr
= ia64_clear_ic();
399 ia64_itr(0x1, IA64_TR_PALCODE
, GRANULEROUNDDOWN((unsigned long) pal_vaddr
),
400 pte_val(pfn_pte(__pa(pal_vaddr
) >> PAGE_SHIFT
, PAGE_KERNEL
)),
402 ia64_set_psr(psr
); /* restore psr */
409 void *efi_map_start
, *efi_map_end
;
410 efi_config_table_t
*config_tables
;
413 char *cp
, vendor
[100] = "unknown";
414 extern char saved_command_line
[];
417 /* it's too early to be able to use the standard kernel command line support... */
418 for (cp
= saved_command_line
; *cp
; ) {
419 if (memcmp(cp
, "mem=", 4) == 0) {
420 mem_limit
= memparse(cp
+ 4, &cp
);
421 } else if (memcmp(cp
, "max_addr=", 9) == 0) {
422 max_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
424 while (*cp
!= ' ' && *cp
)
430 if (max_addr
!= ~0UL)
431 printk(KERN_INFO
"Ignoring memory above %luMB\n", max_addr
>> 20);
433 efi
.systab
= __va(ia64_boot_param
->efi_systab
);
436 * Verify the EFI Table
438 if (efi
.systab
== NULL
)
439 panic("Woah! Can't find EFI system table.\n");
440 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
441 panic("Woah! EFI system table signature incorrect\n");
442 if ((efi
.systab
->hdr
.revision
^ EFI_SYSTEM_TABLE_REVISION
) >> 16 != 0)
443 printk(KERN_WARNING
"Warning: EFI system table major version mismatch: "
444 "got %d.%02d, expected %d.%02d\n",
445 efi
.systab
->hdr
.revision
>> 16, efi
.systab
->hdr
.revision
& 0xffff,
446 EFI_SYSTEM_TABLE_REVISION
>> 16, EFI_SYSTEM_TABLE_REVISION
& 0xffff);
448 config_tables
= __va(efi
.systab
->tables
);
450 /* Show what we know for posterity */
451 c16
= __va(efi
.systab
->fw_vendor
);
453 for (i
= 0;i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
458 printk(KERN_INFO
"EFI v%u.%.02u by %s:",
459 efi
.systab
->hdr
.revision
>> 16, efi
.systab
->hdr
.revision
& 0xffff, vendor
);
461 for (i
= 0; i
< (int) efi
.systab
->nr_tables
; i
++) {
462 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
463 efi
.mps
= __va(config_tables
[i
].table
);
464 printk(" MPS=0x%lx", config_tables
[i
].table
);
465 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
466 efi
.acpi20
= __va(config_tables
[i
].table
);
467 printk(" ACPI 2.0=0x%lx", config_tables
[i
].table
);
468 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
469 efi
.acpi
= __va(config_tables
[i
].table
);
470 printk(" ACPI=0x%lx", config_tables
[i
].table
);
471 } else if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
472 efi
.smbios
= __va(config_tables
[i
].table
);
473 printk(" SMBIOS=0x%lx", config_tables
[i
].table
);
474 } else if (efi_guidcmp(config_tables
[i
].guid
, SAL_SYSTEM_TABLE_GUID
) == 0) {
475 efi
.sal_systab
= __va(config_tables
[i
].table
);
476 printk(" SALsystab=0x%lx", config_tables
[i
].table
);
477 } else if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
478 efi
.hcdp
= __va(config_tables
[i
].table
);
479 printk(" HCDP=0x%lx", config_tables
[i
].table
);
484 runtime
= __va(efi
.systab
->runtime
);
485 efi
.get_time
= phys_get_time
;
486 efi
.set_time
= phys_set_time
;
487 efi
.get_wakeup_time
= phys_get_wakeup_time
;
488 efi
.set_wakeup_time
= phys_set_wakeup_time
;
489 efi
.get_variable
= phys_get_variable
;
490 efi
.get_next_variable
= phys_get_next_variable
;
491 efi
.set_variable
= phys_set_variable
;
492 efi
.get_next_high_mono_count
= phys_get_next_high_mono_count
;
493 efi
.reset_system
= phys_reset_system
;
495 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
496 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
497 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
500 /* print EFI memory map: */
502 efi_memory_desc_t
*md
;
505 for (i
= 0, p
= efi_map_start
; p
< efi_map_end
; ++i
, p
+= efi_desc_size
) {
507 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
508 i
, md
->type
, md
->attribute
, md
->phys_addr
,
509 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
510 md
->num_pages
>> (20 - EFI_PAGE_SHIFT
));
516 efi_enter_virtual_mode();
520 efi_enter_virtual_mode (void)
522 void *efi_map_start
, *efi_map_end
, *p
;
523 efi_memory_desc_t
*md
;
527 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
528 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
529 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
531 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
533 if (md
->attribute
& EFI_MEMORY_RUNTIME
) {
535 * Some descriptors have multiple bits set, so the order of
536 * the tests is relevant.
538 if (md
->attribute
& EFI_MEMORY_WB
) {
539 md
->virt_addr
= (u64
) __va(md
->phys_addr
);
540 } else if (md
->attribute
& EFI_MEMORY_UC
) {
541 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
542 } else if (md
->attribute
& EFI_MEMORY_WC
) {
544 md
->virt_addr
= ia64_remap(md
->phys_addr
, (_PAGE_A
| _PAGE_P
550 printk(KERN_INFO
"EFI_MEMORY_WC mapping\n");
551 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
553 } else if (md
->attribute
& EFI_MEMORY_WT
) {
555 md
->virt_addr
= ia64_remap(md
->phys_addr
, (_PAGE_A
| _PAGE_P
556 | _PAGE_D
| _PAGE_MA_WT
560 printk(KERN_INFO
"EFI_MEMORY_WT mapping\n");
561 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
567 status
= efi_call_phys(__va(runtime
->set_virtual_address_map
),
568 ia64_boot_param
->efi_memmap_size
,
569 efi_desc_size
, ia64_boot_param
->efi_memdesc_version
,
570 ia64_boot_param
->efi_memmap
);
571 if (status
!= EFI_SUCCESS
) {
572 printk(KERN_WARNING
"warning: unable to switch EFI into virtual mode "
573 "(status=%lu)\n", status
);
578 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
580 efi
.get_time
= virt_get_time
;
581 efi
.set_time
= virt_set_time
;
582 efi
.get_wakeup_time
= virt_get_wakeup_time
;
583 efi
.set_wakeup_time
= virt_set_wakeup_time
;
584 efi
.get_variable
= virt_get_variable
;
585 efi
.get_next_variable
= virt_get_next_variable
;
586 efi
.set_variable
= virt_set_variable
;
587 efi
.get_next_high_mono_count
= virt_get_next_high_mono_count
;
588 efi
.reset_system
= virt_reset_system
;
592 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
593 * this type, other I/O port ranges should be described via ACPI.
596 efi_get_iobase (void)
598 void *efi_map_start
, *efi_map_end
, *p
;
599 efi_memory_desc_t
*md
;
602 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
603 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
604 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
606 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
608 if (md
->type
== EFI_MEMORY_MAPPED_IO_PORT_SPACE
) {
609 if (md
->attribute
& EFI_MEMORY_UC
)
610 return md
->phys_addr
;
616 static efi_memory_desc_t
*
617 efi_memory_descriptor (unsigned long phys_addr
)
619 void *efi_map_start
, *efi_map_end
, *p
;
620 efi_memory_desc_t
*md
;
623 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
624 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
625 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
627 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
630 if (phys_addr
- md
->phys_addr
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
637 efi_memmap_has_mmio (void)
639 void *efi_map_start
, *efi_map_end
, *p
;
640 efi_memory_desc_t
*md
;
643 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
644 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
645 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
647 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
650 if (md
->type
== EFI_MEMORY_MAPPED_IO
)
657 efi_mem_type (unsigned long phys_addr
)
659 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
667 efi_mem_attributes (unsigned long phys_addr
)
669 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
672 return md
->attribute
;
675 EXPORT_SYMBOL(efi_mem_attributes
);
678 * Determines whether the memory at phys_addr supports the desired
679 * attribute (WB, UC, etc). If this returns 1, the caller can safely
680 * access *size bytes at phys_addr with the specified attribute.
683 efi_mem_attribute_range (unsigned long phys_addr
, unsigned long *size
, u64 attr
)
685 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
686 unsigned long md_end
;
688 if (!md
|| (md
->attribute
& attr
) != attr
)
692 md_end
= efi_md_end(md
);
693 if (phys_addr
+ *size
<= md_end
)
696 md
= efi_memory_descriptor(md_end
);
697 if (!md
|| (md
->attribute
& attr
) != attr
) {
698 *size
= md_end
- phys_addr
;
706 * For /dev/mem, we only allow read & write system calls to access
707 * write-back memory, because read & write don't allow the user to
708 * control access size.
711 valid_phys_addr_range (unsigned long phys_addr
, unsigned long *size
)
713 return efi_mem_attribute_range(phys_addr
, size
, EFI_MEMORY_WB
);
717 * We allow mmap of anything in the EFI memory map that supports
718 * either write-back or uncacheable access. For uncacheable regions,
719 * the supported access sizes are system-dependent, and the user is
720 * responsible for using the correct size.
722 * Note that this doesn't currently allow access to hot-added memory,
723 * because that doesn't appear in the boot-time EFI memory map.
726 valid_mmap_phys_addr_range (unsigned long phys_addr
, unsigned long *size
)
728 if (efi_mem_attribute_range(phys_addr
, size
, EFI_MEMORY_WB
))
731 if (efi_mem_attribute_range(phys_addr
, size
, EFI_MEMORY_UC
))
735 * Some firmware doesn't report MMIO regions in the EFI memory map.
736 * The Intel BigSur (a.k.a. HP i2000) has this problem. In this
737 * case, we can't use the EFI memory map to validate mmap requests.
739 if (!efi_memmap_has_mmio())
746 efi_uart_console_only(void)
749 char *s
, name
[] = "ConOut";
750 efi_guid_t guid
= EFI_GLOBAL_VARIABLE_GUID
;
751 efi_char16_t
*utf16
, name_utf16
[32];
752 unsigned char data
[1024];
753 unsigned long size
= sizeof(data
);
754 struct efi_generic_dev_path
*hdr
, *end_addr
;
757 /* Convert to UTF-16 */
761 *utf16
++ = *s
++ & 0x7f;
764 status
= efi
.get_variable(name_utf16
, &guid
, NULL
, &size
, data
);
765 if (status
!= EFI_SUCCESS
) {
766 printk(KERN_ERR
"No EFI %s variable?\n", name
);
770 hdr
= (struct efi_generic_dev_path
*) data
;
771 end_addr
= (struct efi_generic_dev_path
*) ((u8
*) data
+ size
);
772 while (hdr
< end_addr
) {
773 if (hdr
->type
== EFI_DEV_MSG
&&
774 hdr
->sub_type
== EFI_DEV_MSG_UART
)
776 else if (hdr
->type
== EFI_DEV_END_PATH
||
777 hdr
->type
== EFI_DEV_END_PATH2
) {
780 if (hdr
->sub_type
== EFI_DEV_END_ENTIRE
)
784 hdr
= (struct efi_generic_dev_path
*) ((u8
*) hdr
+ hdr
->length
);
786 printk(KERN_ERR
"Malformed %s value\n", name
);
791 * Look for the first granule aligned memory descriptor memory
792 * that is big enough to hold EFI memory map. Make sure this
793 * descriptor is atleast granule sized so it does not get trimmed
795 struct kern_memdesc
*
796 find_memmap_space (void)
798 u64 contig_low
=0, contig_high
=0;
800 void *efi_map_start
, *efi_map_end
, *p
, *q
;
801 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
802 u64 space_needed
, efi_desc_size
;
803 unsigned long total_mem
= 0;
805 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
806 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
807 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
810 * Worst case: we need 3 kernel descriptors for each efi descriptor
811 * (if every entry has a WB part in the middle, and UC head and tail),
812 * plus one for the end marker.
814 space_needed
= sizeof(kern_memdesc_t
) *
815 (3 * (ia64_boot_param
->efi_memmap_size
/efi_desc_size
) + 1);
817 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
822 if (pmd
== NULL
|| !efi_wb(pmd
) || efi_md_end(pmd
) != md
->phys_addr
) {
823 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
824 contig_high
= efi_md_end(md
);
825 for (q
= p
+ efi_desc_size
; q
< efi_map_end
; q
+= efi_desc_size
) {
827 if (!efi_wb(check_md
))
829 if (contig_high
!= check_md
->phys_addr
)
831 contig_high
= efi_md_end(check_md
);
833 contig_high
= GRANULEROUNDDOWN(contig_high
);
835 if (!is_available_memory(md
) || md
->type
== EFI_LOADER_DATA
)
838 /* Round ends inward to granule boundaries */
839 as
= max(contig_low
, md
->phys_addr
);
840 ae
= min(contig_high
, efi_md_end(md
));
842 /* keep within max_addr= command line arg */
843 ae
= min(ae
, max_addr
);
847 /* avoid going over mem= command line arg */
848 if (total_mem
+ (ae
- as
) > mem_limit
)
849 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
854 if (ae
- as
> space_needed
)
857 if (p
>= efi_map_end
)
858 panic("Can't allocate space for kernel memory descriptors");
864 * Walk the EFI memory map and gather all memory available for kernel
865 * to use. We can allocate partial granules only if the unavailable
866 * parts exist, and are WB.
869 efi_memmap_init(unsigned long *s
, unsigned long *e
)
871 struct kern_memdesc
*k
, *prev
= 0;
872 u64 contig_low
=0, contig_high
=0;
874 void *efi_map_start
, *efi_map_end
, *p
, *q
;
875 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
877 unsigned long total_mem
= 0;
879 k
= kern_memmap
= find_memmap_space();
881 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
882 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
883 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
885 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
888 if (efi_uc(md
) && (md
->type
== EFI_CONVENTIONAL_MEMORY
||
889 md
->type
== EFI_BOOT_SERVICES_DATA
)) {
890 k
->attribute
= EFI_MEMORY_UC
;
891 k
->start
= md
->phys_addr
;
892 k
->num_pages
= md
->num_pages
;
897 if (pmd
== NULL
|| !efi_wb(pmd
) || efi_md_end(pmd
) != md
->phys_addr
) {
898 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
899 contig_high
= efi_md_end(md
);
900 for (q
= p
+ efi_desc_size
; q
< efi_map_end
; q
+= efi_desc_size
) {
902 if (!efi_wb(check_md
))
904 if (contig_high
!= check_md
->phys_addr
)
906 contig_high
= efi_md_end(check_md
);
908 contig_high
= GRANULEROUNDDOWN(contig_high
);
910 if (!is_available_memory(md
))
914 * Round ends inward to granule boundaries
915 * Give trimmings to uncached allocator
917 if (md
->phys_addr
< contig_low
) {
918 lim
= min(efi_md_end(md
), contig_low
);
920 if (k
> kern_memmap
&& (k
-1)->attribute
== EFI_MEMORY_UC
&&
921 kmd_end(k
-1) == md
->phys_addr
) {
922 (k
-1)->num_pages
+= (lim
- md
->phys_addr
) >> EFI_PAGE_SHIFT
;
924 k
->attribute
= EFI_MEMORY_UC
;
925 k
->start
= md
->phys_addr
;
926 k
->num_pages
= (lim
- md
->phys_addr
) >> EFI_PAGE_SHIFT
;
934 if (efi_md_end(md
) > contig_high
) {
935 lim
= max(md
->phys_addr
, contig_high
);
937 if (lim
== md
->phys_addr
&& k
> kern_memmap
&&
938 (k
-1)->attribute
== EFI_MEMORY_UC
&&
939 kmd_end(k
-1) == md
->phys_addr
) {
940 (k
-1)->num_pages
+= md
->num_pages
;
942 k
->attribute
= EFI_MEMORY_UC
;
944 k
->num_pages
= (efi_md_end(md
) - lim
) >> EFI_PAGE_SHIFT
;
952 /* keep within max_addr= command line arg */
953 ae
= min(ae
, max_addr
);
957 /* avoid going over mem= command line arg */
958 if (total_mem
+ (ae
- as
) > mem_limit
)
959 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
963 if (prev
&& kmd_end(prev
) == md
->phys_addr
) {
964 prev
->num_pages
+= (ae
- as
) >> EFI_PAGE_SHIFT
;
965 total_mem
+= ae
- as
;
968 k
->attribute
= EFI_MEMORY_WB
;
970 k
->num_pages
= (ae
- as
) >> EFI_PAGE_SHIFT
;
971 total_mem
+= ae
- as
;
974 k
->start
= ~0L; /* end-marker */
976 /* reserve the memory we are using for kern_memmap */
977 *s
= (u64
)kern_memmap
;
982 efi_initialize_iomem_resources(struct resource
*code_resource
,
983 struct resource
*data_resource
)
985 struct resource
*res
;
986 void *efi_map_start
, *efi_map_end
, *p
;
987 efi_memory_desc_t
*md
;
992 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
993 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
994 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
998 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1001 if (md
->num_pages
== 0) /* should not happen */
1004 flags
= IORESOURCE_MEM
;
1007 case EFI_MEMORY_MAPPED_IO
:
1008 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1011 case EFI_LOADER_CODE
:
1012 case EFI_LOADER_DATA
:
1013 case EFI_BOOT_SERVICES_DATA
:
1014 case EFI_BOOT_SERVICES_CODE
:
1015 case EFI_CONVENTIONAL_MEMORY
:
1016 if (md
->attribute
& EFI_MEMORY_WP
) {
1017 name
= "System ROM";
1018 flags
|= IORESOURCE_READONLY
;
1020 name
= "System RAM";
1024 case EFI_ACPI_MEMORY_NVS
:
1025 name
= "ACPI Non-volatile Storage";
1026 flags
|= IORESOURCE_BUSY
;
1029 case EFI_UNUSABLE_MEMORY
:
1031 flags
|= IORESOURCE_BUSY
| IORESOURCE_DISABLED
;
1034 case EFI_RESERVED_TYPE
:
1035 case EFI_RUNTIME_SERVICES_CODE
:
1036 case EFI_RUNTIME_SERVICES_DATA
:
1037 case EFI_ACPI_RECLAIM_MEMORY
:
1040 flags
|= IORESOURCE_BUSY
;
1044 if ((res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
)) == NULL
) {
1045 printk(KERN_ERR
"failed to alocate resource for iomem\n");
1050 res
->start
= md
->phys_addr
;
1051 res
->end
= md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
1054 if (insert_resource(&iomem_resource
, res
) < 0)
1058 * We don't know which region contains
1059 * kernel data so we try it repeatedly and
1060 * let the resource manager test it.
1062 insert_resource(res
, code_resource
);
1063 insert_resource(res
, data_resource
);