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>
11 * (c) Copyright 2006 Hewlett-Packard Development Company, L.P.
12 * Bjorn Helgaas <bjorn.helgaas@hp.com>
14 * All EFI Runtime Services are not implemented yet as EFI only
15 * supports physical mode addressing on SoftSDV. This is to be fixed
16 * in a future version. --drummond 1999-07-20
18 * Implemented EFI runtime services and virtual mode calls. --davidm
20 * Goutham Rao: <goutham.rao@intel.com>
21 * Skip non-WB memory and ignore empty memory ranges.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/time.h>
28 #include <linux/efi.h>
31 #include <asm/kregs.h>
32 #include <asm/meminit.h>
33 #include <asm/pgtable.h>
34 #include <asm/processor.h>
39 extern efi_status_t
efi_call_phys (void *, ...);
43 static efi_runtime_services_t
*runtime
;
44 static unsigned long mem_limit
= ~0UL, max_addr
= ~0UL;
46 #define efi_call_virt(f, args...) (*(f))(args)
48 #define STUB_GET_TIME(prefix, adjust_arg) \
50 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
52 struct ia64_fpreg fr[6]; \
53 efi_time_cap_t *atc = NULL; \
57 atc = adjust_arg(tc); \
58 ia64_save_scratch_fpregs(fr); \
59 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
60 ia64_load_scratch_fpregs(fr); \
64 #define STUB_SET_TIME(prefix, adjust_arg) \
66 prefix##_set_time (efi_time_t *tm) \
68 struct ia64_fpreg fr[6]; \
71 ia64_save_scratch_fpregs(fr); \
72 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
73 ia64_load_scratch_fpregs(fr); \
77 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
79 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
81 struct ia64_fpreg fr[6]; \
84 ia64_save_scratch_fpregs(fr); \
85 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
86 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
87 ia64_load_scratch_fpregs(fr); \
91 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
93 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
95 struct ia64_fpreg fr[6]; \
96 efi_time_t *atm = NULL; \
100 atm = adjust_arg(tm); \
101 ia64_save_scratch_fpregs(fr); \
102 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
104 ia64_load_scratch_fpregs(fr); \
108 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
109 static efi_status_t \
110 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
111 unsigned long *data_size, void *data) \
113 struct ia64_fpreg fr[6]; \
118 aattr = adjust_arg(attr); \
119 ia64_save_scratch_fpregs(fr); \
120 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
121 adjust_arg(name), adjust_arg(vendor), aattr, \
122 adjust_arg(data_size), adjust_arg(data)); \
123 ia64_load_scratch_fpregs(fr); \
127 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
128 static efi_status_t \
129 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
131 struct ia64_fpreg fr[6]; \
134 ia64_save_scratch_fpregs(fr); \
135 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
136 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
137 ia64_load_scratch_fpregs(fr); \
141 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
142 static efi_status_t \
143 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
144 unsigned long data_size, void *data) \
146 struct ia64_fpreg fr[6]; \
149 ia64_save_scratch_fpregs(fr); \
150 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
151 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
153 ia64_load_scratch_fpregs(fr); \
157 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
158 static efi_status_t \
159 prefix##_get_next_high_mono_count (u32 *count) \
161 struct ia64_fpreg fr[6]; \
164 ia64_save_scratch_fpregs(fr); \
165 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
166 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
167 ia64_load_scratch_fpregs(fr); \
171 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
173 prefix##_reset_system (int reset_type, efi_status_t status, \
174 unsigned long data_size, efi_char16_t *data) \
176 struct ia64_fpreg fr[6]; \
177 efi_char16_t *adata = NULL; \
180 adata = adjust_arg(data); \
182 ia64_save_scratch_fpregs(fr); \
183 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
184 reset_type, status, data_size, adata); \
185 /* should not return, but just in case... */ \
186 ia64_load_scratch_fpregs(fr); \
189 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
191 STUB_GET_TIME(phys
, phys_ptr
)
192 STUB_SET_TIME(phys
, phys_ptr
)
193 STUB_GET_WAKEUP_TIME(phys
, phys_ptr
)
194 STUB_SET_WAKEUP_TIME(phys
, phys_ptr
)
195 STUB_GET_VARIABLE(phys
, phys_ptr
)
196 STUB_GET_NEXT_VARIABLE(phys
, phys_ptr
)
197 STUB_SET_VARIABLE(phys
, phys_ptr
)
198 STUB_GET_NEXT_HIGH_MONO_COUNT(phys
, phys_ptr
)
199 STUB_RESET_SYSTEM(phys
, phys_ptr
)
203 STUB_GET_TIME(virt
, id
)
204 STUB_SET_TIME(virt
, id
)
205 STUB_GET_WAKEUP_TIME(virt
, id
)
206 STUB_SET_WAKEUP_TIME(virt
, id
)
207 STUB_GET_VARIABLE(virt
, id
)
208 STUB_GET_NEXT_VARIABLE(virt
, id
)
209 STUB_SET_VARIABLE(virt
, id
)
210 STUB_GET_NEXT_HIGH_MONO_COUNT(virt
, id
)
211 STUB_RESET_SYSTEM(virt
, id
)
214 efi_gettimeofday (struct timespec
*ts
)
218 memset(ts
, 0, sizeof(ts
));
219 if ((*efi
.get_time
)(&tm
, NULL
) != EFI_SUCCESS
)
222 ts
->tv_sec
= mktime(tm
.year
, tm
.month
, tm
.day
, tm
.hour
, tm
.minute
, tm
.second
);
223 ts
->tv_nsec
= tm
.nanosecond
;
227 is_available_memory (efi_memory_desc_t
*md
)
229 if (!(md
->attribute
& EFI_MEMORY_WB
))
233 case EFI_LOADER_CODE
:
234 case EFI_LOADER_DATA
:
235 case EFI_BOOT_SERVICES_CODE
:
236 case EFI_BOOT_SERVICES_DATA
:
237 case EFI_CONVENTIONAL_MEMORY
:
243 typedef struct kern_memdesc
{
249 static kern_memdesc_t
*kern_memmap
;
251 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
254 kmd_end(kern_memdesc_t
*kmd
)
256 return (kmd
->start
+ (kmd
->num_pages
<< EFI_PAGE_SHIFT
));
260 efi_md_end(efi_memory_desc_t
*md
)
262 return (md
->phys_addr
+ efi_md_size(md
));
266 efi_wb(efi_memory_desc_t
*md
)
268 return (md
->attribute
& EFI_MEMORY_WB
);
272 efi_uc(efi_memory_desc_t
*md
)
274 return (md
->attribute
& EFI_MEMORY_UC
);
278 walk (efi_freemem_callback_t callback
, void *arg
, u64 attr
)
281 u64 start
, end
, voff
;
283 voff
= (attr
== EFI_MEMORY_WB
) ? PAGE_OFFSET
: __IA64_UNCACHED_OFFSET
;
284 for (k
= kern_memmap
; k
->start
!= ~0UL; k
++) {
285 if (k
->attribute
!= attr
)
287 start
= PAGE_ALIGN(k
->start
);
288 end
= (k
->start
+ (k
->num_pages
<< EFI_PAGE_SHIFT
)) & PAGE_MASK
;
290 if ((*callback
)(start
+ voff
, end
+ voff
, arg
) < 0)
296 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
297 * has memory that is available for OS use.
300 efi_memmap_walk (efi_freemem_callback_t callback
, void *arg
)
302 walk(callback
, arg
, EFI_MEMORY_WB
);
306 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
307 * has memory that is available for uncached allocator.
310 efi_memmap_walk_uc (efi_freemem_callback_t callback
, void *arg
)
312 walk(callback
, arg
, EFI_MEMORY_UC
);
316 * Look for the PAL_CODE region reported by EFI and maps it using an
317 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
318 * Abstraction Layer chapter 11 in ADAG
322 efi_get_pal_addr (void)
324 void *efi_map_start
, *efi_map_end
, *p
;
325 efi_memory_desc_t
*md
;
327 int pal_code_count
= 0;
330 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
331 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
332 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
334 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
336 if (md
->type
!= EFI_PAL_CODE
)
339 if (++pal_code_count
> 1) {
340 printk(KERN_ERR
"Too many EFI Pal Code memory ranges, dropped @ %lx\n",
345 * The only ITLB entry in region 7 that is used is the one installed by
346 * __start(). That entry covers a 64MB range.
348 mask
= ~((1 << KERNEL_TR_PAGE_SHIFT
) - 1);
349 vaddr
= PAGE_OFFSET
+ md
->phys_addr
;
352 * We must check that the PAL mapping won't overlap with the kernel
355 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
356 * 256KB and that only one ITR is needed to map it. This implies that the
357 * PAL code is always aligned on its size, i.e., the closest matching page
358 * size supported by the TLB. Therefore PAL code is guaranteed never to
359 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
360 * now the following test is enough to determine whether or not we need a
361 * dedicated ITR for the PAL code.
363 if ((vaddr
& mask
) == (KERNEL_START
& mask
)) {
364 printk(KERN_INFO
"%s: no need to install ITR for PAL code\n",
369 if (md
->num_pages
<< EFI_PAGE_SHIFT
> IA64_GRANULE_SIZE
)
370 panic("Woah! PAL code size bigger than a granule!");
373 mask
= ~((1 << IA64_GRANULE_SHIFT
) - 1);
375 printk(KERN_INFO
"CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
376 smp_processor_id(), md
->phys_addr
,
377 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
378 vaddr
& mask
, (vaddr
& mask
) + IA64_GRANULE_SIZE
);
380 return __va(md
->phys_addr
);
382 printk(KERN_WARNING
"%s: no PAL-code memory-descriptor found",
388 efi_map_pal_code (void)
390 void *pal_vaddr
= efi_get_pal_addr ();
397 * Cannot write to CRx with PSR.ic=1
399 psr
= ia64_clear_ic();
400 ia64_itr(0x1, IA64_TR_PALCODE
, GRANULEROUNDDOWN((unsigned long) pal_vaddr
),
401 pte_val(pfn_pte(__pa(pal_vaddr
) >> PAGE_SHIFT
, PAGE_KERNEL
)),
403 ia64_set_psr(psr
); /* restore psr */
410 void *efi_map_start
, *efi_map_end
;
411 efi_config_table_t
*config_tables
;
414 char *cp
, vendor
[100] = "unknown";
415 extern char saved_command_line
[];
418 /* it's too early to be able to use the standard kernel command line support... */
419 for (cp
= saved_command_line
; *cp
; ) {
420 if (memcmp(cp
, "mem=", 4) == 0) {
421 mem_limit
= memparse(cp
+ 4, &cp
);
422 } else if (memcmp(cp
, "max_addr=", 9) == 0) {
423 max_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
425 while (*cp
!= ' ' && *cp
)
431 if (max_addr
!= ~0UL)
432 printk(KERN_INFO
"Ignoring memory above %luMB\n", max_addr
>> 20);
434 efi
.systab
= __va(ia64_boot_param
->efi_systab
);
437 * Verify the EFI Table
439 if (efi
.systab
== NULL
)
440 panic("Woah! Can't find EFI system table.\n");
441 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
442 panic("Woah! EFI system table signature incorrect\n");
443 if ((efi
.systab
->hdr
.revision
^ EFI_SYSTEM_TABLE_REVISION
) >> 16 != 0)
444 printk(KERN_WARNING
"Warning: EFI system table major version mismatch: "
445 "got %d.%02d, expected %d.%02d\n",
446 efi
.systab
->hdr
.revision
>> 16, efi
.systab
->hdr
.revision
& 0xffff,
447 EFI_SYSTEM_TABLE_REVISION
>> 16, EFI_SYSTEM_TABLE_REVISION
& 0xffff);
449 config_tables
= __va(efi
.systab
->tables
);
451 /* Show what we know for posterity */
452 c16
= __va(efi
.systab
->fw_vendor
);
454 for (i
= 0;i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
459 printk(KERN_INFO
"EFI v%u.%.02u by %s:",
460 efi
.systab
->hdr
.revision
>> 16, efi
.systab
->hdr
.revision
& 0xffff, vendor
);
462 efi
.mps
= EFI_INVALID_TABLE_ADDR
;
463 efi
.acpi
= EFI_INVALID_TABLE_ADDR
;
464 efi
.acpi20
= EFI_INVALID_TABLE_ADDR
;
465 efi
.smbios
= EFI_INVALID_TABLE_ADDR
;
466 efi
.sal_systab
= EFI_INVALID_TABLE_ADDR
;
467 efi
.boot_info
= EFI_INVALID_TABLE_ADDR
;
468 efi
.hcdp
= EFI_INVALID_TABLE_ADDR
;
469 efi
.uga
= EFI_INVALID_TABLE_ADDR
;
471 for (i
= 0; i
< (int) efi
.systab
->nr_tables
; i
++) {
472 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
473 efi
.mps
= config_tables
[i
].table
;
474 printk(" MPS=0x%lx", config_tables
[i
].table
);
475 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
476 efi
.acpi20
= config_tables
[i
].table
;
477 printk(" ACPI 2.0=0x%lx", config_tables
[i
].table
);
478 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
479 efi
.acpi
= config_tables
[i
].table
;
480 printk(" ACPI=0x%lx", config_tables
[i
].table
);
481 } else if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
482 efi
.smbios
= config_tables
[i
].table
;
483 printk(" SMBIOS=0x%lx", config_tables
[i
].table
);
484 } else if (efi_guidcmp(config_tables
[i
].guid
, SAL_SYSTEM_TABLE_GUID
) == 0) {
485 efi
.sal_systab
= config_tables
[i
].table
;
486 printk(" SALsystab=0x%lx", config_tables
[i
].table
);
487 } else if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
488 efi
.hcdp
= config_tables
[i
].table
;
489 printk(" HCDP=0x%lx", config_tables
[i
].table
);
494 runtime
= __va(efi
.systab
->runtime
);
495 efi
.get_time
= phys_get_time
;
496 efi
.set_time
= phys_set_time
;
497 efi
.get_wakeup_time
= phys_get_wakeup_time
;
498 efi
.set_wakeup_time
= phys_set_wakeup_time
;
499 efi
.get_variable
= phys_get_variable
;
500 efi
.get_next_variable
= phys_get_next_variable
;
501 efi
.set_variable
= phys_set_variable
;
502 efi
.get_next_high_mono_count
= phys_get_next_high_mono_count
;
503 efi
.reset_system
= phys_reset_system
;
505 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
506 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
507 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
510 /* print EFI memory map: */
512 efi_memory_desc_t
*md
;
515 for (i
= 0, p
= efi_map_start
; p
< efi_map_end
; ++i
, p
+= efi_desc_size
) {
517 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
518 i
, md
->type
, md
->attribute
, md
->phys_addr
,
519 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
520 md
->num_pages
>> (20 - EFI_PAGE_SHIFT
));
526 efi_enter_virtual_mode();
530 efi_enter_virtual_mode (void)
532 void *efi_map_start
, *efi_map_end
, *p
;
533 efi_memory_desc_t
*md
;
537 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
538 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
539 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
541 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
543 if (md
->attribute
& EFI_MEMORY_RUNTIME
) {
545 * Some descriptors have multiple bits set, so the order of
546 * the tests is relevant.
548 if (md
->attribute
& EFI_MEMORY_WB
) {
549 md
->virt_addr
= (u64
) __va(md
->phys_addr
);
550 } else if (md
->attribute
& EFI_MEMORY_UC
) {
551 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
552 } else if (md
->attribute
& EFI_MEMORY_WC
) {
554 md
->virt_addr
= ia64_remap(md
->phys_addr
, (_PAGE_A
| _PAGE_P
560 printk(KERN_INFO
"EFI_MEMORY_WC mapping\n");
561 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
563 } else if (md
->attribute
& EFI_MEMORY_WT
) {
565 md
->virt_addr
= ia64_remap(md
->phys_addr
, (_PAGE_A
| _PAGE_P
566 | _PAGE_D
| _PAGE_MA_WT
570 printk(KERN_INFO
"EFI_MEMORY_WT mapping\n");
571 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
577 status
= efi_call_phys(__va(runtime
->set_virtual_address_map
),
578 ia64_boot_param
->efi_memmap_size
,
579 efi_desc_size
, ia64_boot_param
->efi_memdesc_version
,
580 ia64_boot_param
->efi_memmap
);
581 if (status
!= EFI_SUCCESS
) {
582 printk(KERN_WARNING
"warning: unable to switch EFI into virtual mode "
583 "(status=%lu)\n", status
);
588 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
590 efi
.get_time
= virt_get_time
;
591 efi
.set_time
= virt_set_time
;
592 efi
.get_wakeup_time
= virt_get_wakeup_time
;
593 efi
.set_wakeup_time
= virt_set_wakeup_time
;
594 efi
.get_variable
= virt_get_variable
;
595 efi
.get_next_variable
= virt_get_next_variable
;
596 efi
.set_variable
= virt_set_variable
;
597 efi
.get_next_high_mono_count
= virt_get_next_high_mono_count
;
598 efi
.reset_system
= virt_reset_system
;
602 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
603 * this type, other I/O port ranges should be described via ACPI.
606 efi_get_iobase (void)
608 void *efi_map_start
, *efi_map_end
, *p
;
609 efi_memory_desc_t
*md
;
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
) {
618 if (md
->type
== EFI_MEMORY_MAPPED_IO_PORT_SPACE
) {
619 if (md
->attribute
& EFI_MEMORY_UC
)
620 return md
->phys_addr
;
626 static struct kern_memdesc
*
627 kern_memory_descriptor (unsigned long phys_addr
)
629 struct kern_memdesc
*md
;
631 for (md
= kern_memmap
; md
->start
!= ~0UL; md
++) {
632 if (phys_addr
- md
->start
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
638 static efi_memory_desc_t
*
639 efi_memory_descriptor (unsigned long phys_addr
)
641 void *efi_map_start
, *efi_map_end
, *p
;
642 efi_memory_desc_t
*md
;
645 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
646 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
647 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
649 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
652 if (phys_addr
- md
->phys_addr
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
659 efi_mem_type (unsigned long phys_addr
)
661 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
669 efi_mem_attributes (unsigned long phys_addr
)
671 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
674 return md
->attribute
;
677 EXPORT_SYMBOL(efi_mem_attributes
);
680 efi_mem_attribute (unsigned long phys_addr
, unsigned long size
)
682 unsigned long end
= phys_addr
+ size
;
683 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
690 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
691 * the kernel that firmware needs this region mapped.
693 attr
= md
->attribute
& ~EFI_MEMORY_RUNTIME
;
695 unsigned long md_end
= efi_md_end(md
);
700 md
= efi_memory_descriptor(md_end
);
701 if (!md
|| (md
->attribute
& ~EFI_MEMORY_RUNTIME
) != attr
)
708 kern_mem_attribute (unsigned long phys_addr
, unsigned long size
)
710 unsigned long end
= phys_addr
+ size
;
711 struct kern_memdesc
*md
;
715 * This is a hack for ioremap calls before we set up kern_memmap.
716 * Maybe we should do efi_memmap_init() earlier instead.
719 attr
= efi_mem_attribute(phys_addr
, size
);
720 if (attr
& EFI_MEMORY_WB
)
721 return EFI_MEMORY_WB
;
725 md
= kern_memory_descriptor(phys_addr
);
729 attr
= md
->attribute
;
731 unsigned long md_end
= kmd_end(md
);
736 md
= kern_memory_descriptor(md_end
);
737 if (!md
|| md
->attribute
!= attr
)
742 EXPORT_SYMBOL(kern_mem_attribute
);
745 valid_phys_addr_range (unsigned long phys_addr
, unsigned long size
)
750 * /dev/mem reads and writes use copy_to_user(), which implicitly
751 * uses a granule-sized kernel identity mapping. It's really
752 * only safe to do this for regions in kern_memmap. For more
753 * details, see Documentation/ia64/aliasing.txt.
755 attr
= kern_mem_attribute(phys_addr
, size
);
756 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
762 valid_mmap_phys_addr_range (unsigned long pfn
, unsigned long size
)
765 * MMIO regions are often missing from the EFI memory map.
766 * We must allow mmap of them for programs like X, so we
767 * currently can't do any useful validation.
773 phys_mem_access_prot(struct file
*file
, unsigned long pfn
, unsigned long size
,
776 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
780 * For /dev/mem mmap, we use user mappings, but if the region is
781 * in kern_memmap (and hence may be covered by a kernel mapping),
782 * we must use the same attribute as the kernel mapping.
784 attr
= kern_mem_attribute(phys_addr
, size
);
785 if (attr
& EFI_MEMORY_WB
)
786 return pgprot_cacheable(vma_prot
);
787 else if (attr
& EFI_MEMORY_UC
)
788 return pgprot_noncached(vma_prot
);
791 * Some chipsets don't support UC access to memory. If
792 * WB is supported, we prefer that.
794 if (efi_mem_attribute(phys_addr
, size
) & EFI_MEMORY_WB
)
795 return pgprot_cacheable(vma_prot
);
797 return pgprot_noncached(vma_prot
);
801 efi_uart_console_only(void)
804 char *s
, name
[] = "ConOut";
805 efi_guid_t guid
= EFI_GLOBAL_VARIABLE_GUID
;
806 efi_char16_t
*utf16
, name_utf16
[32];
807 unsigned char data
[1024];
808 unsigned long size
= sizeof(data
);
809 struct efi_generic_dev_path
*hdr
, *end_addr
;
812 /* Convert to UTF-16 */
816 *utf16
++ = *s
++ & 0x7f;
819 status
= efi
.get_variable(name_utf16
, &guid
, NULL
, &size
, data
);
820 if (status
!= EFI_SUCCESS
) {
821 printk(KERN_ERR
"No EFI %s variable?\n", name
);
825 hdr
= (struct efi_generic_dev_path
*) data
;
826 end_addr
= (struct efi_generic_dev_path
*) ((u8
*) data
+ size
);
827 while (hdr
< end_addr
) {
828 if (hdr
->type
== EFI_DEV_MSG
&&
829 hdr
->sub_type
== EFI_DEV_MSG_UART
)
831 else if (hdr
->type
== EFI_DEV_END_PATH
||
832 hdr
->type
== EFI_DEV_END_PATH2
) {
835 if (hdr
->sub_type
== EFI_DEV_END_ENTIRE
)
839 hdr
= (struct efi_generic_dev_path
*) ((u8
*) hdr
+ hdr
->length
);
841 printk(KERN_ERR
"Malformed %s value\n", name
);
846 * Look for the first granule aligned memory descriptor memory
847 * that is big enough to hold EFI memory map. Make sure this
848 * descriptor is atleast granule sized so it does not get trimmed
850 struct kern_memdesc
*
851 find_memmap_space (void)
853 u64 contig_low
=0, contig_high
=0;
855 void *efi_map_start
, *efi_map_end
, *p
, *q
;
856 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
857 u64 space_needed
, efi_desc_size
;
858 unsigned long total_mem
= 0;
860 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
861 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
862 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
865 * Worst case: we need 3 kernel descriptors for each efi descriptor
866 * (if every entry has a WB part in the middle, and UC head and tail),
867 * plus one for the end marker.
869 space_needed
= sizeof(kern_memdesc_t
) *
870 (3 * (ia64_boot_param
->efi_memmap_size
/efi_desc_size
) + 1);
872 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
877 if (pmd
== NULL
|| !efi_wb(pmd
) || efi_md_end(pmd
) != md
->phys_addr
) {
878 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
879 contig_high
= efi_md_end(md
);
880 for (q
= p
+ efi_desc_size
; q
< efi_map_end
; q
+= efi_desc_size
) {
882 if (!efi_wb(check_md
))
884 if (contig_high
!= check_md
->phys_addr
)
886 contig_high
= efi_md_end(check_md
);
888 contig_high
= GRANULEROUNDDOWN(contig_high
);
890 if (!is_available_memory(md
) || md
->type
== EFI_LOADER_DATA
)
893 /* Round ends inward to granule boundaries */
894 as
= max(contig_low
, md
->phys_addr
);
895 ae
= min(contig_high
, efi_md_end(md
));
897 /* keep within max_addr= command line arg */
898 ae
= min(ae
, max_addr
);
902 /* avoid going over mem= command line arg */
903 if (total_mem
+ (ae
- as
) > mem_limit
)
904 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
909 if (ae
- as
> space_needed
)
912 if (p
>= efi_map_end
)
913 panic("Can't allocate space for kernel memory descriptors");
919 * Walk the EFI memory map and gather all memory available for kernel
920 * to use. We can allocate partial granules only if the unavailable
921 * parts exist, and are WB.
924 efi_memmap_init(unsigned long *s
, unsigned long *e
)
926 struct kern_memdesc
*k
, *prev
= NULL
;
927 u64 contig_low
=0, contig_high
=0;
929 void *efi_map_start
, *efi_map_end
, *p
, *q
;
930 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
932 unsigned long total_mem
= 0;
934 k
= kern_memmap
= find_memmap_space();
936 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
937 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
938 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
940 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
943 if (efi_uc(md
) && (md
->type
== EFI_CONVENTIONAL_MEMORY
||
944 md
->type
== EFI_BOOT_SERVICES_DATA
)) {
945 k
->attribute
= EFI_MEMORY_UC
;
946 k
->start
= md
->phys_addr
;
947 k
->num_pages
= md
->num_pages
;
952 if (pmd
== NULL
|| !efi_wb(pmd
) || efi_md_end(pmd
) != md
->phys_addr
) {
953 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
954 contig_high
= efi_md_end(md
);
955 for (q
= p
+ efi_desc_size
; q
< efi_map_end
; q
+= efi_desc_size
) {
957 if (!efi_wb(check_md
))
959 if (contig_high
!= check_md
->phys_addr
)
961 contig_high
= efi_md_end(check_md
);
963 contig_high
= GRANULEROUNDDOWN(contig_high
);
965 if (!is_available_memory(md
))
969 * Round ends inward to granule boundaries
970 * Give trimmings to uncached allocator
972 if (md
->phys_addr
< contig_low
) {
973 lim
= min(efi_md_end(md
), contig_low
);
975 if (k
> kern_memmap
&& (k
-1)->attribute
== EFI_MEMORY_UC
&&
976 kmd_end(k
-1) == md
->phys_addr
) {
977 (k
-1)->num_pages
+= (lim
- md
->phys_addr
) >> EFI_PAGE_SHIFT
;
979 k
->attribute
= EFI_MEMORY_UC
;
980 k
->start
= md
->phys_addr
;
981 k
->num_pages
= (lim
- md
->phys_addr
) >> EFI_PAGE_SHIFT
;
989 if (efi_md_end(md
) > contig_high
) {
990 lim
= max(md
->phys_addr
, contig_high
);
992 if (lim
== md
->phys_addr
&& k
> kern_memmap
&&
993 (k
-1)->attribute
== EFI_MEMORY_UC
&&
994 kmd_end(k
-1) == md
->phys_addr
) {
995 (k
-1)->num_pages
+= md
->num_pages
;
997 k
->attribute
= EFI_MEMORY_UC
;
999 k
->num_pages
= (efi_md_end(md
) - lim
) >> EFI_PAGE_SHIFT
;
1005 ae
= efi_md_end(md
);
1007 /* keep within max_addr= command line arg */
1008 ae
= min(ae
, max_addr
);
1012 /* avoid going over mem= command line arg */
1013 if (total_mem
+ (ae
- as
) > mem_limit
)
1014 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1018 if (prev
&& kmd_end(prev
) == md
->phys_addr
) {
1019 prev
->num_pages
+= (ae
- as
) >> EFI_PAGE_SHIFT
;
1020 total_mem
+= ae
- as
;
1023 k
->attribute
= EFI_MEMORY_WB
;
1025 k
->num_pages
= (ae
- as
) >> EFI_PAGE_SHIFT
;
1026 total_mem
+= ae
- as
;
1029 k
->start
= ~0L; /* end-marker */
1031 /* reserve the memory we are using for kern_memmap */
1032 *s
= (u64
)kern_memmap
;
1037 efi_initialize_iomem_resources(struct resource
*code_resource
,
1038 struct resource
*data_resource
)
1040 struct resource
*res
;
1041 void *efi_map_start
, *efi_map_end
, *p
;
1042 efi_memory_desc_t
*md
;
1045 unsigned long flags
;
1047 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1048 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1049 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1053 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1056 if (md
->num_pages
== 0) /* should not happen */
1059 flags
= IORESOURCE_MEM
;
1062 case EFI_MEMORY_MAPPED_IO
:
1063 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1066 case EFI_LOADER_CODE
:
1067 case EFI_LOADER_DATA
:
1068 case EFI_BOOT_SERVICES_DATA
:
1069 case EFI_BOOT_SERVICES_CODE
:
1070 case EFI_CONVENTIONAL_MEMORY
:
1071 if (md
->attribute
& EFI_MEMORY_WP
) {
1072 name
= "System ROM";
1073 flags
|= IORESOURCE_READONLY
;
1075 name
= "System RAM";
1079 case EFI_ACPI_MEMORY_NVS
:
1080 name
= "ACPI Non-volatile Storage";
1081 flags
|= IORESOURCE_BUSY
;
1084 case EFI_UNUSABLE_MEMORY
:
1086 flags
|= IORESOURCE_BUSY
| IORESOURCE_DISABLED
;
1089 case EFI_RESERVED_TYPE
:
1090 case EFI_RUNTIME_SERVICES_CODE
:
1091 case EFI_RUNTIME_SERVICES_DATA
:
1092 case EFI_ACPI_RECLAIM_MEMORY
:
1095 flags
|= IORESOURCE_BUSY
;
1099 if ((res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
)) == NULL
) {
1100 printk(KERN_ERR
"failed to alocate resource for iomem\n");
1105 res
->start
= md
->phys_addr
;
1106 res
->end
= md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
1109 if (insert_resource(&iomem_resource
, res
) < 0)
1113 * We don't know which region contains
1114 * kernel data so we try it repeatedly and
1115 * let the resource manager test it.
1117 insert_resource(res
, code_resource
);
1118 insert_resource(res
, data_resource
);