dmaengine: driver for the iop32x, iop33x, and iop13xx raid engines
[pv_ops_mirror.git] / arch / ia64 / kernel / efi.c
blob75ec3478d8a2c3aaf593810391e1d54a992fed1d
1 /*
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/bootmem.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/time.h>
29 #include <linux/efi.h>
30 #include <linux/kexec.h>
32 #include <asm/io.h>
33 #include <asm/kregs.h>
34 #include <asm/meminit.h>
35 #include <asm/pgtable.h>
36 #include <asm/processor.h>
37 #include <asm/mca.h>
39 #define EFI_DEBUG 0
41 extern efi_status_t efi_call_phys (void *, ...);
43 struct efi efi;
44 EXPORT_SYMBOL(efi);
45 static efi_runtime_services_t *runtime;
46 static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
48 #define efi_call_virt(f, args...) (*(f))(args)
50 #define STUB_GET_TIME(prefix, adjust_arg) \
51 static efi_status_t \
52 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
53 { \
54 struct ia64_fpreg fr[6]; \
55 efi_time_cap_t *atc = NULL; \
56 efi_status_t ret; \
58 if (tc) \
59 atc = adjust_arg(tc); \
60 ia64_save_scratch_fpregs(fr); \
61 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
62 ia64_load_scratch_fpregs(fr); \
63 return ret; \
66 #define STUB_SET_TIME(prefix, adjust_arg) \
67 static efi_status_t \
68 prefix##_set_time (efi_time_t *tm) \
69 { \
70 struct ia64_fpreg fr[6]; \
71 efi_status_t ret; \
73 ia64_save_scratch_fpregs(fr); \
74 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
75 ia64_load_scratch_fpregs(fr); \
76 return ret; \
79 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
80 static efi_status_t \
81 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
82 { \
83 struct ia64_fpreg fr[6]; \
84 efi_status_t ret; \
86 ia64_save_scratch_fpregs(fr); \
87 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
88 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
89 ia64_load_scratch_fpregs(fr); \
90 return ret; \
93 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
94 static efi_status_t \
95 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
96 { \
97 struct ia64_fpreg fr[6]; \
98 efi_time_t *atm = NULL; \
99 efi_status_t ret; \
101 if (tm) \
102 atm = adjust_arg(tm); \
103 ia64_save_scratch_fpregs(fr); \
104 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
105 enabled, atm); \
106 ia64_load_scratch_fpregs(fr); \
107 return ret; \
110 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
111 static efi_status_t \
112 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
113 unsigned long *data_size, void *data) \
115 struct ia64_fpreg fr[6]; \
116 u32 *aattr = NULL; \
117 efi_status_t ret; \
119 if (attr) \
120 aattr = adjust_arg(attr); \
121 ia64_save_scratch_fpregs(fr); \
122 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
123 adjust_arg(name), adjust_arg(vendor), aattr, \
124 adjust_arg(data_size), adjust_arg(data)); \
125 ia64_load_scratch_fpregs(fr); \
126 return ret; \
129 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
130 static efi_status_t \
131 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
133 struct ia64_fpreg fr[6]; \
134 efi_status_t ret; \
136 ia64_save_scratch_fpregs(fr); \
137 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
138 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
139 ia64_load_scratch_fpregs(fr); \
140 return ret; \
143 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
144 static efi_status_t \
145 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
146 unsigned long data_size, void *data) \
148 struct ia64_fpreg fr[6]; \
149 efi_status_t ret; \
151 ia64_save_scratch_fpregs(fr); \
152 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
153 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
154 adjust_arg(data)); \
155 ia64_load_scratch_fpregs(fr); \
156 return ret; \
159 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
160 static efi_status_t \
161 prefix##_get_next_high_mono_count (u32 *count) \
163 struct ia64_fpreg fr[6]; \
164 efi_status_t ret; \
166 ia64_save_scratch_fpregs(fr); \
167 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
168 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
169 ia64_load_scratch_fpregs(fr); \
170 return ret; \
173 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
174 static void \
175 prefix##_reset_system (int reset_type, efi_status_t status, \
176 unsigned long data_size, efi_char16_t *data) \
178 struct ia64_fpreg fr[6]; \
179 efi_char16_t *adata = NULL; \
181 if (data) \
182 adata = adjust_arg(data); \
184 ia64_save_scratch_fpregs(fr); \
185 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
186 reset_type, status, data_size, adata); \
187 /* should not return, but just in case... */ \
188 ia64_load_scratch_fpregs(fr); \
191 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
193 STUB_GET_TIME(phys, phys_ptr)
194 STUB_SET_TIME(phys, phys_ptr)
195 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
196 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
197 STUB_GET_VARIABLE(phys, phys_ptr)
198 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
199 STUB_SET_VARIABLE(phys, phys_ptr)
200 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
201 STUB_RESET_SYSTEM(phys, phys_ptr)
203 #define id(arg) arg
205 STUB_GET_TIME(virt, id)
206 STUB_SET_TIME(virt, id)
207 STUB_GET_WAKEUP_TIME(virt, id)
208 STUB_SET_WAKEUP_TIME(virt, id)
209 STUB_GET_VARIABLE(virt, id)
210 STUB_GET_NEXT_VARIABLE(virt, id)
211 STUB_SET_VARIABLE(virt, id)
212 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
213 STUB_RESET_SYSTEM(virt, id)
215 void
216 efi_gettimeofday (struct timespec *ts)
218 efi_time_t tm;
220 memset(ts, 0, sizeof(ts));
221 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
222 return;
224 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
225 ts->tv_nsec = tm.nanosecond;
228 static int
229 is_memory_available (efi_memory_desc_t *md)
231 if (!(md->attribute & EFI_MEMORY_WB))
232 return 0;
234 switch (md->type) {
235 case EFI_LOADER_CODE:
236 case EFI_LOADER_DATA:
237 case EFI_BOOT_SERVICES_CODE:
238 case EFI_BOOT_SERVICES_DATA:
239 case EFI_CONVENTIONAL_MEMORY:
240 return 1;
242 return 0;
245 typedef struct kern_memdesc {
246 u64 attribute;
247 u64 start;
248 u64 num_pages;
249 } kern_memdesc_t;
251 static kern_memdesc_t *kern_memmap;
253 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
255 static inline u64
256 kmd_end(kern_memdesc_t *kmd)
258 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
261 static inline u64
262 efi_md_end(efi_memory_desc_t *md)
264 return (md->phys_addr + efi_md_size(md));
267 static inline int
268 efi_wb(efi_memory_desc_t *md)
270 return (md->attribute & EFI_MEMORY_WB);
273 static inline int
274 efi_uc(efi_memory_desc_t *md)
276 return (md->attribute & EFI_MEMORY_UC);
279 static void
280 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
282 kern_memdesc_t *k;
283 u64 start, end, voff;
285 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
286 for (k = kern_memmap; k->start != ~0UL; k++) {
287 if (k->attribute != attr)
288 continue;
289 start = PAGE_ALIGN(k->start);
290 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
291 if (start < end)
292 if ((*callback)(start + voff, end + voff, arg) < 0)
293 return;
298 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
299 * has memory that is available for OS use.
301 void
302 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
304 walk(callback, arg, EFI_MEMORY_WB);
308 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
309 * has memory that is available for uncached allocator.
311 void
312 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
314 walk(callback, arg, EFI_MEMORY_UC);
318 * Look for the PAL_CODE region reported by EFI and maps it using an
319 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
320 * Abstraction Layer chapter 11 in ADAG
323 void *
324 efi_get_pal_addr (void)
326 void *efi_map_start, *efi_map_end, *p;
327 efi_memory_desc_t *md;
328 u64 efi_desc_size;
329 int pal_code_count = 0;
330 u64 vaddr, mask;
332 efi_map_start = __va(ia64_boot_param->efi_memmap);
333 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
334 efi_desc_size = ia64_boot_param->efi_memdesc_size;
336 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
337 md = p;
338 if (md->type != EFI_PAL_CODE)
339 continue;
341 if (++pal_code_count > 1) {
342 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
343 md->phys_addr);
344 continue;
347 * The only ITLB entry in region 7 that is used is the one installed by
348 * __start(). That entry covers a 64MB range.
350 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
351 vaddr = PAGE_OFFSET + md->phys_addr;
354 * We must check that the PAL mapping won't overlap with the kernel
355 * mapping.
357 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
358 * 256KB and that only one ITR is needed to map it. This implies that the
359 * PAL code is always aligned on its size, i.e., the closest matching page
360 * size supported by the TLB. Therefore PAL code is guaranteed never to
361 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
362 * now the following test is enough to determine whether or not we need a
363 * dedicated ITR for the PAL code.
365 if ((vaddr & mask) == (KERNEL_START & mask)) {
366 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
367 __FUNCTION__);
368 continue;
371 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
372 panic("Woah! PAL code size bigger than a granule!");
374 #if EFI_DEBUG
375 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
377 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
378 smp_processor_id(), md->phys_addr,
379 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
380 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
381 #endif
382 return __va(md->phys_addr);
384 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
385 __FUNCTION__);
386 return NULL;
389 void
390 efi_map_pal_code (void)
392 void *pal_vaddr = efi_get_pal_addr ();
393 u64 psr;
395 if (!pal_vaddr)
396 return;
399 * Cannot write to CRx with PSR.ic=1
401 psr = ia64_clear_ic();
402 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
403 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
404 IA64_GRANULE_SHIFT);
405 ia64_set_psr(psr); /* restore psr */
406 ia64_srlz_i();
409 void __init
410 efi_init (void)
412 void *efi_map_start, *efi_map_end;
413 efi_config_table_t *config_tables;
414 efi_char16_t *c16;
415 u64 efi_desc_size;
416 char *cp, vendor[100] = "unknown";
417 int i;
419 /* it's too early to be able to use the standard kernel command line support... */
420 for (cp = boot_command_line; *cp; ) {
421 if (memcmp(cp, "mem=", 4) == 0) {
422 mem_limit = memparse(cp + 4, &cp);
423 } else if (memcmp(cp, "max_addr=", 9) == 0) {
424 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
425 } else if (memcmp(cp, "min_addr=", 9) == 0) {
426 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
427 } else {
428 while (*cp != ' ' && *cp)
429 ++cp;
430 while (*cp == ' ')
431 ++cp;
434 if (min_addr != 0UL)
435 printk(KERN_INFO "Ignoring memory below %luMB\n", min_addr >> 20);
436 if (max_addr != ~0UL)
437 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
439 efi.systab = __va(ia64_boot_param->efi_systab);
442 * Verify the EFI Table
444 if (efi.systab == NULL)
445 panic("Woah! Can't find EFI system table.\n");
446 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
447 panic("Woah! EFI system table signature incorrect\n");
448 if ((efi.systab->hdr.revision >> 16) == 0)
449 printk(KERN_WARNING "Warning: EFI system table version "
450 "%d.%02d, expected 1.00 or greater\n",
451 efi.systab->hdr.revision >> 16,
452 efi.systab->hdr.revision & 0xffff);
454 config_tables = __va(efi.systab->tables);
456 /* Show what we know for posterity */
457 c16 = __va(efi.systab->fw_vendor);
458 if (c16) {
459 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
460 vendor[i] = *c16++;
461 vendor[i] = '\0';
464 printk(KERN_INFO "EFI v%u.%.02u by %s:",
465 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
467 efi.mps = EFI_INVALID_TABLE_ADDR;
468 efi.acpi = EFI_INVALID_TABLE_ADDR;
469 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
470 efi.smbios = EFI_INVALID_TABLE_ADDR;
471 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
472 efi.boot_info = EFI_INVALID_TABLE_ADDR;
473 efi.hcdp = EFI_INVALID_TABLE_ADDR;
474 efi.uga = EFI_INVALID_TABLE_ADDR;
476 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
477 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
478 efi.mps = config_tables[i].table;
479 printk(" MPS=0x%lx", config_tables[i].table);
480 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
481 efi.acpi20 = config_tables[i].table;
482 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
483 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
484 efi.acpi = config_tables[i].table;
485 printk(" ACPI=0x%lx", config_tables[i].table);
486 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
487 efi.smbios = config_tables[i].table;
488 printk(" SMBIOS=0x%lx", config_tables[i].table);
489 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
490 efi.sal_systab = config_tables[i].table;
491 printk(" SALsystab=0x%lx", config_tables[i].table);
492 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
493 efi.hcdp = config_tables[i].table;
494 printk(" HCDP=0x%lx", config_tables[i].table);
497 printk("\n");
499 runtime = __va(efi.systab->runtime);
500 efi.get_time = phys_get_time;
501 efi.set_time = phys_set_time;
502 efi.get_wakeup_time = phys_get_wakeup_time;
503 efi.set_wakeup_time = phys_set_wakeup_time;
504 efi.get_variable = phys_get_variable;
505 efi.get_next_variable = phys_get_next_variable;
506 efi.set_variable = phys_set_variable;
507 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
508 efi.reset_system = phys_reset_system;
510 efi_map_start = __va(ia64_boot_param->efi_memmap);
511 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
512 efi_desc_size = ia64_boot_param->efi_memdesc_size;
514 #if EFI_DEBUG
515 /* print EFI memory map: */
517 efi_memory_desc_t *md;
518 void *p;
520 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
521 md = p;
522 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
523 i, md->type, md->attribute, md->phys_addr,
524 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
525 md->num_pages >> (20 - EFI_PAGE_SHIFT));
528 #endif
530 efi_map_pal_code();
531 efi_enter_virtual_mode();
534 void
535 efi_enter_virtual_mode (void)
537 void *efi_map_start, *efi_map_end, *p;
538 efi_memory_desc_t *md;
539 efi_status_t status;
540 u64 efi_desc_size;
542 efi_map_start = __va(ia64_boot_param->efi_memmap);
543 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
544 efi_desc_size = ia64_boot_param->efi_memdesc_size;
546 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
547 md = p;
548 if (md->attribute & EFI_MEMORY_RUNTIME) {
550 * Some descriptors have multiple bits set, so the order of
551 * the tests is relevant.
553 if (md->attribute & EFI_MEMORY_WB) {
554 md->virt_addr = (u64) __va(md->phys_addr);
555 } else if (md->attribute & EFI_MEMORY_UC) {
556 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
557 } else if (md->attribute & EFI_MEMORY_WC) {
558 #if 0
559 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
560 | _PAGE_D
561 | _PAGE_MA_WC
562 | _PAGE_PL_0
563 | _PAGE_AR_RW));
564 #else
565 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
566 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
567 #endif
568 } else if (md->attribute & EFI_MEMORY_WT) {
569 #if 0
570 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
571 | _PAGE_D | _PAGE_MA_WT
572 | _PAGE_PL_0
573 | _PAGE_AR_RW));
574 #else
575 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
576 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
577 #endif
582 status = efi_call_phys(__va(runtime->set_virtual_address_map),
583 ia64_boot_param->efi_memmap_size,
584 efi_desc_size, ia64_boot_param->efi_memdesc_version,
585 ia64_boot_param->efi_memmap);
586 if (status != EFI_SUCCESS) {
587 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
588 "(status=%lu)\n", status);
589 return;
593 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
595 efi.get_time = virt_get_time;
596 efi.set_time = virt_set_time;
597 efi.get_wakeup_time = virt_get_wakeup_time;
598 efi.set_wakeup_time = virt_set_wakeup_time;
599 efi.get_variable = virt_get_variable;
600 efi.get_next_variable = virt_get_next_variable;
601 efi.set_variable = virt_set_variable;
602 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
603 efi.reset_system = virt_reset_system;
607 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
608 * this type, other I/O port ranges should be described via ACPI.
611 efi_get_iobase (void)
613 void *efi_map_start, *efi_map_end, *p;
614 efi_memory_desc_t *md;
615 u64 efi_desc_size;
617 efi_map_start = __va(ia64_boot_param->efi_memmap);
618 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
619 efi_desc_size = ia64_boot_param->efi_memdesc_size;
621 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
622 md = p;
623 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
624 if (md->attribute & EFI_MEMORY_UC)
625 return md->phys_addr;
628 return 0;
631 static struct kern_memdesc *
632 kern_memory_descriptor (unsigned long phys_addr)
634 struct kern_memdesc *md;
636 for (md = kern_memmap; md->start != ~0UL; md++) {
637 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
638 return md;
640 return NULL;
643 static efi_memory_desc_t *
644 efi_memory_descriptor (unsigned long phys_addr)
646 void *efi_map_start, *efi_map_end, *p;
647 efi_memory_desc_t *md;
648 u64 efi_desc_size;
650 efi_map_start = __va(ia64_boot_param->efi_memmap);
651 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
652 efi_desc_size = ia64_boot_param->efi_memdesc_size;
654 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
655 md = p;
657 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
658 return md;
660 return NULL;
663 static int
664 efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
666 void *efi_map_start, *efi_map_end, *p;
667 efi_memory_desc_t *md;
668 u64 efi_desc_size;
669 unsigned long end;
671 efi_map_start = __va(ia64_boot_param->efi_memmap);
672 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
673 efi_desc_size = ia64_boot_param->efi_memdesc_size;
675 end = phys_addr + size;
677 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
678 md = p;
680 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
681 return 1;
683 return 0;
687 efi_mem_type (unsigned long phys_addr)
689 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
691 if (md)
692 return md->type;
693 return 0;
697 efi_mem_attributes (unsigned long phys_addr)
699 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
701 if (md)
702 return md->attribute;
703 return 0;
705 EXPORT_SYMBOL(efi_mem_attributes);
708 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
710 unsigned long end = phys_addr + size;
711 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
712 u64 attr;
714 if (!md)
715 return 0;
718 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
719 * the kernel that firmware needs this region mapped.
721 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
722 do {
723 unsigned long md_end = efi_md_end(md);
725 if (end <= md_end)
726 return attr;
728 md = efi_memory_descriptor(md_end);
729 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
730 return 0;
731 } while (md);
732 return 0;
736 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
738 unsigned long end = phys_addr + size;
739 struct kern_memdesc *md;
740 u64 attr;
743 * This is a hack for ioremap calls before we set up kern_memmap.
744 * Maybe we should do efi_memmap_init() earlier instead.
746 if (!kern_memmap) {
747 attr = efi_mem_attribute(phys_addr, size);
748 if (attr & EFI_MEMORY_WB)
749 return EFI_MEMORY_WB;
750 return 0;
753 md = kern_memory_descriptor(phys_addr);
754 if (!md)
755 return 0;
757 attr = md->attribute;
758 do {
759 unsigned long md_end = kmd_end(md);
761 if (end <= md_end)
762 return attr;
764 md = kern_memory_descriptor(md_end);
765 if (!md || md->attribute != attr)
766 return 0;
767 } while (md);
768 return 0;
770 EXPORT_SYMBOL(kern_mem_attribute);
773 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
775 u64 attr;
778 * /dev/mem reads and writes use copy_to_user(), which implicitly
779 * uses a granule-sized kernel identity mapping. It's really
780 * only safe to do this for regions in kern_memmap. For more
781 * details, see Documentation/ia64/aliasing.txt.
783 attr = kern_mem_attribute(phys_addr, size);
784 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
785 return 1;
786 return 0;
790 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
792 unsigned long phys_addr = pfn << PAGE_SHIFT;
793 u64 attr;
795 attr = efi_mem_attribute(phys_addr, size);
798 * /dev/mem mmap uses normal user pages, so we don't need the entire
799 * granule, but the entire region we're mapping must support the same
800 * attribute.
802 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
803 return 1;
806 * Intel firmware doesn't tell us about all the MMIO regions, so
807 * in general we have to allow mmap requests. But if EFI *does*
808 * tell us about anything inside this region, we should deny it.
809 * The user can always map a smaller region to avoid the overlap.
811 if (efi_memmap_intersects(phys_addr, size))
812 return 0;
814 return 1;
817 pgprot_t
818 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
819 pgprot_t vma_prot)
821 unsigned long phys_addr = pfn << PAGE_SHIFT;
822 u64 attr;
825 * For /dev/mem mmap, we use user mappings, but if the region is
826 * in kern_memmap (and hence may be covered by a kernel mapping),
827 * we must use the same attribute as the kernel mapping.
829 attr = kern_mem_attribute(phys_addr, size);
830 if (attr & EFI_MEMORY_WB)
831 return pgprot_cacheable(vma_prot);
832 else if (attr & EFI_MEMORY_UC)
833 return pgprot_noncached(vma_prot);
836 * Some chipsets don't support UC access to memory. If
837 * WB is supported, we prefer that.
839 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
840 return pgprot_cacheable(vma_prot);
842 return pgprot_noncached(vma_prot);
845 int __init
846 efi_uart_console_only(void)
848 efi_status_t status;
849 char *s, name[] = "ConOut";
850 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
851 efi_char16_t *utf16, name_utf16[32];
852 unsigned char data[1024];
853 unsigned long size = sizeof(data);
854 struct efi_generic_dev_path *hdr, *end_addr;
855 int uart = 0;
857 /* Convert to UTF-16 */
858 utf16 = name_utf16;
859 s = name;
860 while (*s)
861 *utf16++ = *s++ & 0x7f;
862 *utf16 = 0;
864 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
865 if (status != EFI_SUCCESS) {
866 printk(KERN_ERR "No EFI %s variable?\n", name);
867 return 0;
870 hdr = (struct efi_generic_dev_path *) data;
871 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
872 while (hdr < end_addr) {
873 if (hdr->type == EFI_DEV_MSG &&
874 hdr->sub_type == EFI_DEV_MSG_UART)
875 uart = 1;
876 else if (hdr->type == EFI_DEV_END_PATH ||
877 hdr->type == EFI_DEV_END_PATH2) {
878 if (!uart)
879 return 0;
880 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
881 return 1;
882 uart = 0;
884 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
886 printk(KERN_ERR "Malformed %s value\n", name);
887 return 0;
891 * Look for the first granule aligned memory descriptor memory
892 * that is big enough to hold EFI memory map. Make sure this
893 * descriptor is atleast granule sized so it does not get trimmed
895 struct kern_memdesc *
896 find_memmap_space (void)
898 u64 contig_low=0, contig_high=0;
899 u64 as = 0, ae;
900 void *efi_map_start, *efi_map_end, *p, *q;
901 efi_memory_desc_t *md, *pmd = NULL, *check_md;
902 u64 space_needed, efi_desc_size;
903 unsigned long total_mem = 0;
905 efi_map_start = __va(ia64_boot_param->efi_memmap);
906 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
907 efi_desc_size = ia64_boot_param->efi_memdesc_size;
910 * Worst case: we need 3 kernel descriptors for each efi descriptor
911 * (if every entry has a WB part in the middle, and UC head and tail),
912 * plus one for the end marker.
914 space_needed = sizeof(kern_memdesc_t) *
915 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
917 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
918 md = p;
919 if (!efi_wb(md)) {
920 continue;
922 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
923 contig_low = GRANULEROUNDUP(md->phys_addr);
924 contig_high = efi_md_end(md);
925 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
926 check_md = q;
927 if (!efi_wb(check_md))
928 break;
929 if (contig_high != check_md->phys_addr)
930 break;
931 contig_high = efi_md_end(check_md);
933 contig_high = GRANULEROUNDDOWN(contig_high);
935 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
936 continue;
938 /* Round ends inward to granule boundaries */
939 as = max(contig_low, md->phys_addr);
940 ae = min(contig_high, efi_md_end(md));
942 /* keep within max_addr= and min_addr= command line arg */
943 as = max(as, min_addr);
944 ae = min(ae, max_addr);
945 if (ae <= as)
946 continue;
948 /* avoid going over mem= command line arg */
949 if (total_mem + (ae - as) > mem_limit)
950 ae -= total_mem + (ae - as) - mem_limit;
952 if (ae <= as)
953 continue;
955 if (ae - as > space_needed)
956 break;
958 if (p >= efi_map_end)
959 panic("Can't allocate space for kernel memory descriptors");
961 return __va(as);
965 * Walk the EFI memory map and gather all memory available for kernel
966 * to use. We can allocate partial granules only if the unavailable
967 * parts exist, and are WB.
969 void
970 efi_memmap_init(unsigned long *s, unsigned long *e)
972 struct kern_memdesc *k, *prev = NULL;
973 u64 contig_low=0, contig_high=0;
974 u64 as, ae, lim;
975 void *efi_map_start, *efi_map_end, *p, *q;
976 efi_memory_desc_t *md, *pmd = NULL, *check_md;
977 u64 efi_desc_size;
978 unsigned long total_mem = 0;
980 k = kern_memmap = find_memmap_space();
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;
986 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
987 md = p;
988 if (!efi_wb(md)) {
989 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
990 md->type == EFI_BOOT_SERVICES_DATA)) {
991 k->attribute = EFI_MEMORY_UC;
992 k->start = md->phys_addr;
993 k->num_pages = md->num_pages;
994 k++;
996 continue;
998 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
999 contig_low = GRANULEROUNDUP(md->phys_addr);
1000 contig_high = efi_md_end(md);
1001 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
1002 check_md = q;
1003 if (!efi_wb(check_md))
1004 break;
1005 if (contig_high != check_md->phys_addr)
1006 break;
1007 contig_high = efi_md_end(check_md);
1009 contig_high = GRANULEROUNDDOWN(contig_high);
1011 if (!is_memory_available(md))
1012 continue;
1014 #ifdef CONFIG_CRASH_DUMP
1015 /* saved_max_pfn should ignore max_addr= command line arg */
1016 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1017 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1018 #endif
1020 * Round ends inward to granule boundaries
1021 * Give trimmings to uncached allocator
1023 if (md->phys_addr < contig_low) {
1024 lim = min(efi_md_end(md), contig_low);
1025 if (efi_uc(md)) {
1026 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
1027 kmd_end(k-1) == md->phys_addr) {
1028 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1029 } else {
1030 k->attribute = EFI_MEMORY_UC;
1031 k->start = md->phys_addr;
1032 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1033 k++;
1036 as = contig_low;
1037 } else
1038 as = md->phys_addr;
1040 if (efi_md_end(md) > contig_high) {
1041 lim = max(md->phys_addr, contig_high);
1042 if (efi_uc(md)) {
1043 if (lim == md->phys_addr && k > kern_memmap &&
1044 (k-1)->attribute == EFI_MEMORY_UC &&
1045 kmd_end(k-1) == md->phys_addr) {
1046 (k-1)->num_pages += md->num_pages;
1047 } else {
1048 k->attribute = EFI_MEMORY_UC;
1049 k->start = lim;
1050 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1051 k++;
1054 ae = contig_high;
1055 } else
1056 ae = efi_md_end(md);
1058 /* keep within max_addr= and min_addr= command line arg */
1059 as = max(as, min_addr);
1060 ae = min(ae, max_addr);
1061 if (ae <= as)
1062 continue;
1064 /* avoid going over mem= command line arg */
1065 if (total_mem + (ae - as) > mem_limit)
1066 ae -= total_mem + (ae - as) - mem_limit;
1068 if (ae <= as)
1069 continue;
1070 if (prev && kmd_end(prev) == md->phys_addr) {
1071 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1072 total_mem += ae - as;
1073 continue;
1075 k->attribute = EFI_MEMORY_WB;
1076 k->start = as;
1077 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1078 total_mem += ae - as;
1079 prev = k++;
1081 k->start = ~0L; /* end-marker */
1083 /* reserve the memory we are using for kern_memmap */
1084 *s = (u64)kern_memmap;
1085 *e = (u64)++k;
1088 void
1089 efi_initialize_iomem_resources(struct resource *code_resource,
1090 struct resource *data_resource)
1092 struct resource *res;
1093 void *efi_map_start, *efi_map_end, *p;
1094 efi_memory_desc_t *md;
1095 u64 efi_desc_size;
1096 char *name;
1097 unsigned long flags;
1099 efi_map_start = __va(ia64_boot_param->efi_memmap);
1100 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1101 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1103 res = NULL;
1105 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1106 md = p;
1108 if (md->num_pages == 0) /* should not happen */
1109 continue;
1111 flags = IORESOURCE_MEM;
1112 switch (md->type) {
1114 case EFI_MEMORY_MAPPED_IO:
1115 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1116 continue;
1118 case EFI_LOADER_CODE:
1119 case EFI_LOADER_DATA:
1120 case EFI_BOOT_SERVICES_DATA:
1121 case EFI_BOOT_SERVICES_CODE:
1122 case EFI_CONVENTIONAL_MEMORY:
1123 if (md->attribute & EFI_MEMORY_WP) {
1124 name = "System ROM";
1125 flags |= IORESOURCE_READONLY;
1126 } else {
1127 name = "System RAM";
1129 break;
1131 case EFI_ACPI_MEMORY_NVS:
1132 name = "ACPI Non-volatile Storage";
1133 flags |= IORESOURCE_BUSY;
1134 break;
1136 case EFI_UNUSABLE_MEMORY:
1137 name = "reserved";
1138 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
1139 break;
1141 case EFI_RESERVED_TYPE:
1142 case EFI_RUNTIME_SERVICES_CODE:
1143 case EFI_RUNTIME_SERVICES_DATA:
1144 case EFI_ACPI_RECLAIM_MEMORY:
1145 default:
1146 name = "reserved";
1147 flags |= IORESOURCE_BUSY;
1148 break;
1151 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1152 printk(KERN_ERR "failed to alocate resource for iomem\n");
1153 return;
1156 res->name = name;
1157 res->start = md->phys_addr;
1158 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1159 res->flags = flags;
1161 if (insert_resource(&iomem_resource, res) < 0)
1162 kfree(res);
1163 else {
1165 * We don't know which region contains
1166 * kernel data so we try it repeatedly and
1167 * let the resource manager test it.
1169 insert_resource(res, code_resource);
1170 insert_resource(res, data_resource);
1171 #ifdef CONFIG_KEXEC
1172 insert_resource(res, &efi_memmap_res);
1173 insert_resource(res, &boot_param_res);
1174 if (crashk_res.end > crashk_res.start)
1175 insert_resource(res, &crashk_res);
1176 #endif
1181 #ifdef CONFIG_KEXEC
1182 /* find a block of memory aligned to 64M exclude reserved regions
1183 rsvd_regions are sorted
1185 unsigned long __init
1186 kdump_find_rsvd_region (unsigned long size,
1187 struct rsvd_region *r, int n)
1189 int i;
1190 u64 start, end;
1191 u64 alignment = 1UL << _PAGE_SIZE_64M;
1192 void *efi_map_start, *efi_map_end, *p;
1193 efi_memory_desc_t *md;
1194 u64 efi_desc_size;
1196 efi_map_start = __va(ia64_boot_param->efi_memmap);
1197 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1198 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1200 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1201 md = p;
1202 if (!efi_wb(md))
1203 continue;
1204 start = ALIGN(md->phys_addr, alignment);
1205 end = efi_md_end(md);
1206 for (i = 0; i < n; i++) {
1207 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1208 if (__pa(r[i].start) > start + size)
1209 return start;
1210 start = ALIGN(__pa(r[i].end), alignment);
1211 if (i < n-1 && __pa(r[i+1].start) < start + size)
1212 continue;
1213 else
1214 break;
1217 if (end > start + size)
1218 return start;
1221 printk(KERN_WARNING "Cannot reserve 0x%lx byte of memory for crashdump\n",
1222 size);
1223 return ~0UL;
1225 #endif
1227 #ifdef CONFIG_PROC_VMCORE
1228 /* locate the size find a the descriptor at a certain address */
1229 unsigned long
1230 vmcore_find_descriptor_size (unsigned long address)
1232 void *efi_map_start, *efi_map_end, *p;
1233 efi_memory_desc_t *md;
1234 u64 efi_desc_size;
1235 unsigned long ret = 0;
1237 efi_map_start = __va(ia64_boot_param->efi_memmap);
1238 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1239 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1241 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1242 md = p;
1243 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1244 && md->phys_addr == address) {
1245 ret = efi_md_size(md);
1246 break;
1250 if (ret == 0)
1251 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1253 return ret;
1255 #endif