ia64/xen-unstable

view xen/arch/ia64/linux-xen/efi.c @ 10888:5379548bfc79

[NET] Enable TCPv4 segmentation offload in front/back drivers.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Tue Aug 01 11:54:45 2006 +0100 (2006-08-01)
parents 0a226de3fc37
children 6bf652c677c8
line source
1 /*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 0.9 April 30, 1999
5 *
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 *
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
15 *
16 * Implemented EFI runtime services and virtual mode calls. --davidm
17 *
18 * Goutham Rao: <goutham.rao@intel.com>
19 * Skip non-WB memory and ignore empty memory ranges.
20 */
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>
28 #include <asm/io.h>
29 #include <asm/kregs.h>
30 #include <asm/meminit.h>
31 #include <asm/pgtable.h>
32 #include <asm/processor.h>
33 #include <asm/mca.h>
35 #include <linux/efi.h>
37 #define EFI_DEBUG 0
39 extern efi_status_t efi_call_phys (void *, ...);
40 extern unsigned long long memparse (char *ptr, char **retptr);
42 struct efi efi;
43 EXPORT_SYMBOL(efi);
44 static efi_runtime_services_t *runtime;
45 #if defined(XEN) && !defined(CONFIG_VIRTUAL_FRAME_TABLE)
46 // this is a temporary hack to avoid CONFIG_VIRTUAL_MEM_MAP
47 static unsigned long mem_limit = ~0UL, max_addr = 0x100000000;
48 #else
49 static unsigned long mem_limit = ~0UL, max_addr = ~0UL;
50 #endif
52 #define efi_call_virt(f, args...) (*(f))(args)
54 #define STUB_GET_TIME(prefix, adjust_arg) \
55 static efi_status_t \
56 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
57 { \
58 struct ia64_fpreg fr[6]; \
59 efi_time_cap_t *atc = NULL; \
60 efi_status_t ret; \
61 \
62 if (tc) \
63 atc = adjust_arg(tc); \
64 ia64_save_scratch_fpregs(fr); \
65 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
66 ia64_load_scratch_fpregs(fr); \
67 return ret; \
68 }
70 #define STUB_SET_TIME(prefix, adjust_arg) \
71 static efi_status_t \
72 prefix##_set_time (efi_time_t *tm) \
73 { \
74 struct ia64_fpreg fr[6]; \
75 efi_status_t ret; \
76 \
77 ia64_save_scratch_fpregs(fr); \
78 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
79 ia64_load_scratch_fpregs(fr); \
80 return ret; \
81 }
83 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
84 static efi_status_t \
85 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
86 { \
87 struct ia64_fpreg fr[6]; \
88 efi_status_t ret; \
89 \
90 ia64_save_scratch_fpregs(fr); \
91 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
92 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
93 ia64_load_scratch_fpregs(fr); \
94 return ret; \
95 }
97 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
98 static efi_status_t \
99 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
100 { \
101 struct ia64_fpreg fr[6]; \
102 efi_time_t *atm = NULL; \
103 efi_status_t ret; \
104 \
105 if (tm) \
106 atm = adjust_arg(tm); \
107 ia64_save_scratch_fpregs(fr); \
108 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
109 enabled, atm); \
110 ia64_load_scratch_fpregs(fr); \
111 return ret; \
112 }
114 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
115 static efi_status_t \
116 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
117 unsigned long *data_size, void *data) \
118 { \
119 struct ia64_fpreg fr[6]; \
120 u32 *aattr = NULL; \
121 efi_status_t ret; \
122 \
123 if (attr) \
124 aattr = adjust_arg(attr); \
125 ia64_save_scratch_fpregs(fr); \
126 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
127 adjust_arg(name), adjust_arg(vendor), aattr, \
128 adjust_arg(data_size), adjust_arg(data)); \
129 ia64_load_scratch_fpregs(fr); \
130 return ret; \
131 }
133 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
134 static efi_status_t \
135 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
136 { \
137 struct ia64_fpreg fr[6]; \
138 efi_status_t ret; \
139 \
140 ia64_save_scratch_fpregs(fr); \
141 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
142 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
143 ia64_load_scratch_fpregs(fr); \
144 return ret; \
145 }
147 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
148 static efi_status_t \
149 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
150 unsigned long data_size, void *data) \
151 { \
152 struct ia64_fpreg fr[6]; \
153 efi_status_t ret; \
154 \
155 ia64_save_scratch_fpregs(fr); \
156 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
157 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
158 adjust_arg(data)); \
159 ia64_load_scratch_fpregs(fr); \
160 return ret; \
161 }
163 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
164 static efi_status_t \
165 prefix##_get_next_high_mono_count (u32 *count) \
166 { \
167 struct ia64_fpreg fr[6]; \
168 efi_status_t ret; \
169 \
170 ia64_save_scratch_fpregs(fr); \
171 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
172 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
173 ia64_load_scratch_fpregs(fr); \
174 return ret; \
175 }
177 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
178 static void \
179 prefix##_reset_system (int reset_type, efi_status_t status, \
180 unsigned long data_size, efi_char16_t *data) \
181 { \
182 struct ia64_fpreg fr[6]; \
183 efi_char16_t *adata = NULL; \
184 \
185 if (data) \
186 adata = adjust_arg(data); \
187 \
188 ia64_save_scratch_fpregs(fr); \
189 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
190 reset_type, status, data_size, adata); \
191 /* should not return, but just in case... */ \
192 ia64_load_scratch_fpregs(fr); \
193 }
195 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
197 STUB_GET_TIME(phys, phys_ptr)
198 STUB_SET_TIME(phys, phys_ptr)
199 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
200 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
201 STUB_GET_VARIABLE(phys, phys_ptr)
202 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
203 STUB_SET_VARIABLE(phys, phys_ptr)
204 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
205 STUB_RESET_SYSTEM(phys, phys_ptr)
207 #define id(arg) arg
209 STUB_GET_TIME(virt, id)
210 STUB_SET_TIME(virt, id)
211 STUB_GET_WAKEUP_TIME(virt, id)
212 STUB_SET_WAKEUP_TIME(virt, id)
213 STUB_GET_VARIABLE(virt, id)
214 STUB_GET_NEXT_VARIABLE(virt, id)
215 STUB_SET_VARIABLE(virt, id)
216 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
217 STUB_RESET_SYSTEM(virt, id)
219 void
220 efi_gettimeofday (struct timespec *ts)
221 {
222 efi_time_t tm;
224 memset(ts, 0, sizeof(ts));
225 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
226 return;
228 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
229 ts->tv_nsec = tm.nanosecond;
230 }
232 static int
233 is_available_memory (efi_memory_desc_t *md)
234 {
235 if (!(md->attribute & EFI_MEMORY_WB))
236 return 0;
238 switch (md->type) {
239 case EFI_LOADER_CODE:
240 case EFI_LOADER_DATA:
241 case EFI_BOOT_SERVICES_CODE:
242 case EFI_BOOT_SERVICES_DATA:
243 case EFI_CONVENTIONAL_MEMORY:
244 return 1;
245 }
246 return 0;
247 }
249 typedef struct kern_memdesc {
250 u64 attribute;
251 u64 start;
252 u64 num_pages;
253 } kern_memdesc_t;
255 static kern_memdesc_t *kern_memmap;
257 static void
258 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
259 {
260 kern_memdesc_t *k;
261 u64 start, end, voff;
263 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
264 for (k = kern_memmap; k->start != ~0UL; k++) {
265 if (k->attribute != attr)
266 continue;
267 start = PAGE_ALIGN(k->start);
268 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
269 if (start < end)
270 if ((*callback)(start + voff, end + voff, arg) < 0)
271 return;
272 }
273 }
275 /*
276 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
277 * has memory that is available for OS use.
278 */
279 void
280 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
281 {
282 walk(callback, arg, EFI_MEMORY_WB);
283 }
285 /*
286 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
287 * has memory that is available for uncached allocator.
288 */
289 void
290 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
291 {
292 walk(callback, arg, EFI_MEMORY_UC);
293 }
295 #ifdef XEN
296 void
297 efi_memmap_walk_type(u32 type, efi_walk_type_callback_t callback, void *arg)
298 {
299 void *efi_map_start, *efi_map_end, *p;
300 efi_memory_desc_t *md;
301 u64 efi_desc_size;
303 efi_map_start = __va(ia64_boot_param->efi_memmap);
304 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
305 efi_desc_size = ia64_boot_param->efi_memdesc_size;
307 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
308 md = p;
309 if (md->type == type) {
310 if ((*callback)(md, arg) < 0)
311 return;
312 }
313 }
314 }
315 #endif
317 /*
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
321 */
323 void *
324 efi_get_pal_addr (void)
325 {
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;
345 }
346 /*
347 * The only ITLB entry in region 7 that is used is the one installed by
348 * __start(). That entry covers a 64MB range.
349 */
350 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
351 vaddr = PAGE_OFFSET + md->phys_addr;
353 /*
354 * We must check that the PAL mapping won't overlap with the kernel
355 * mapping.
356 *
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.
364 */
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;
369 }
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);
383 }
384 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found",
385 __FUNCTION__);
386 return NULL;
387 }
390 #ifdef XEN
391 void *pal_vaddr = 0;
392 #endif
394 void
395 efi_map_pal_code (void)
396 {
397 #ifdef XEN
398 u64 psr;
399 if (!pal_vaddr) {
400 pal_vaddr = efi_get_pal_addr ();
401 }
402 #else
403 void *pal_vaddr = efi_get_pal_addr ();
404 u64 psr;
406 if (!pal_vaddr)
407 return;
408 #endif
410 /*
411 * Cannot write to CRx with PSR.ic=1
412 */
413 psr = ia64_clear_ic();
414 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
415 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
416 IA64_GRANULE_SHIFT);
417 ia64_set_psr(psr); /* restore psr */
418 ia64_srlz_i();
419 }
421 void __init
422 efi_init (void)
423 {
424 void *efi_map_start, *efi_map_end;
425 efi_config_table_t *config_tables;
426 efi_char16_t *c16;
427 u64 efi_desc_size;
428 char *cp, *end, vendor[100] = "unknown";
429 extern char saved_command_line[];
430 int i;
432 /* it's too early to be able to use the standard kernel command line support... */
433 for (cp = saved_command_line; *cp; ) {
434 if (memcmp(cp, "mem=", 4) == 0) {
435 cp += 4;
436 mem_limit = memparse(cp, &end);
437 if (end != cp)
438 break;
439 cp = end;
440 } else if (memcmp(cp, "max_addr=", 9) == 0) {
441 cp += 9;
442 max_addr = GRANULEROUNDDOWN(memparse(cp, &end));
443 if (end != cp)
444 break;
445 cp = end;
446 } else {
447 while (*cp != ' ' && *cp)
448 ++cp;
449 while (*cp == ' ')
450 ++cp;
451 }
452 }
453 if (max_addr != ~0UL)
454 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
456 efi.systab = __va(ia64_boot_param->efi_systab);
458 /*
459 * Verify the EFI Table
460 */
461 if (efi.systab == NULL)
462 panic("Woah! Can't find EFI system table.\n");
463 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
464 panic("Woah! EFI system table signature incorrect\n");
465 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
466 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
467 "got %d.%02d, expected %d.%02d\n",
468 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
469 EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
471 config_tables = __va(efi.systab->tables);
473 /* Show what we know for posterity */
474 c16 = __va(efi.systab->fw_vendor);
475 if (c16) {
476 for (i = 0;i < (int) sizeof(vendor) && *c16; ++i)
477 vendor[i] = *c16++;
478 vendor[i] = '\0';
479 }
481 printk(KERN_INFO "EFI v%u.%.02u by %s:",
482 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
484 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
485 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
486 efi.mps = __va(config_tables[i].table);
487 printk(" MPS=0x%lx", config_tables[i].table);
488 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
489 efi.acpi20 = __va(config_tables[i].table);
490 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
491 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
492 efi.acpi = __va(config_tables[i].table);
493 printk(" ACPI=0x%lx", config_tables[i].table);
494 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
495 efi.smbios = __va(config_tables[i].table);
496 printk(" SMBIOS=0x%lx", config_tables[i].table);
497 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
498 efi.sal_systab = __va(config_tables[i].table);
499 printk(" SALsystab=0x%lx", config_tables[i].table);
500 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
501 efi.hcdp = __va(config_tables[i].table);
502 printk(" HCDP=0x%lx", config_tables[i].table);
503 }
504 }
505 printk("\n");
507 runtime = __va(efi.systab->runtime);
508 efi.get_time = phys_get_time;
509 efi.set_time = phys_set_time;
510 efi.get_wakeup_time = phys_get_wakeup_time;
511 efi.set_wakeup_time = phys_set_wakeup_time;
512 efi.get_variable = phys_get_variable;
513 efi.get_next_variable = phys_get_next_variable;
514 efi.set_variable = phys_set_variable;
515 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
516 efi.reset_system = phys_reset_system;
518 efi_map_start = __va(ia64_boot_param->efi_memmap);
519 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
520 efi_desc_size = ia64_boot_param->efi_memdesc_size;
522 #if EFI_DEBUG
523 /* print EFI memory map: */
524 {
525 efi_memory_desc_t *md;
526 void *p;
528 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
529 md = p;
530 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
531 i, md->type, md->attribute, md->phys_addr,
532 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
533 md->num_pages >> (20 - EFI_PAGE_SHIFT));
534 }
535 }
536 #endif
538 efi_map_pal_code();
539 efi_enter_virtual_mode();
540 }
542 void
543 efi_enter_virtual_mode (void)
544 {
545 void *efi_map_start, *efi_map_end, *p;
546 efi_memory_desc_t *md;
547 efi_status_t status;
548 u64 efi_desc_size;
550 efi_map_start = __va(ia64_boot_param->efi_memmap);
551 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
552 efi_desc_size = ia64_boot_param->efi_memdesc_size;
554 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
555 md = p;
556 if (md->attribute & EFI_MEMORY_RUNTIME) {
557 /*
558 * Some descriptors have multiple bits set, so the order of
559 * the tests is relevant.
560 */
561 if (md->attribute & EFI_MEMORY_WB) {
562 md->virt_addr = (u64) __va(md->phys_addr);
563 } else if (md->attribute & EFI_MEMORY_UC) {
564 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
565 } else if (md->attribute & EFI_MEMORY_WC) {
566 #if 0
567 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
568 | _PAGE_D
569 | _PAGE_MA_WC
570 | _PAGE_PL_0
571 | _PAGE_AR_RW));
572 #else
573 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
574 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
575 #endif
576 } else if (md->attribute & EFI_MEMORY_WT) {
577 #if 0
578 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
579 | _PAGE_D | _PAGE_MA_WT
580 | _PAGE_PL_0
581 | _PAGE_AR_RW));
582 #else
583 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
584 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
585 #endif
586 }
587 }
588 }
590 status = efi_call_phys(__va(runtime->set_virtual_address_map),
591 ia64_boot_param->efi_memmap_size,
592 efi_desc_size, ia64_boot_param->efi_memdesc_version,
593 ia64_boot_param->efi_memmap);
594 if (status != EFI_SUCCESS) {
595 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
596 "(status=%lu)\n", status);
597 return;
598 }
600 /*
601 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
602 */
603 efi.get_time = virt_get_time;
604 efi.set_time = virt_set_time;
605 efi.get_wakeup_time = virt_get_wakeup_time;
606 efi.set_wakeup_time = virt_set_wakeup_time;
607 efi.get_variable = virt_get_variable;
608 efi.get_next_variable = virt_get_next_variable;
609 efi.set_variable = virt_set_variable;
610 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
611 efi.reset_system = virt_reset_system;
612 }
614 /*
615 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
616 * this type, other I/O port ranges should be described via ACPI.
617 */
618 u64
619 efi_get_iobase (void)
620 {
621 void *efi_map_start, *efi_map_end, *p;
622 efi_memory_desc_t *md;
623 u64 efi_desc_size;
625 efi_map_start = __va(ia64_boot_param->efi_memmap);
626 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
627 efi_desc_size = ia64_boot_param->efi_memdesc_size;
629 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
630 md = p;
631 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
632 if (md->attribute & EFI_MEMORY_UC)
633 return md->phys_addr;
634 }
635 }
636 return 0;
637 }
639 u32
640 efi_mem_type (unsigned long phys_addr)
641 {
642 void *efi_map_start, *efi_map_end, *p;
643 efi_memory_desc_t *md;
644 u64 efi_desc_size;
646 efi_map_start = __va(ia64_boot_param->efi_memmap);
647 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
648 efi_desc_size = ia64_boot_param->efi_memdesc_size;
650 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
651 md = p;
653 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
654 return md->type;
655 }
656 return 0;
657 }
659 u64
660 efi_mem_attributes (unsigned long phys_addr)
661 {
662 void *efi_map_start, *efi_map_end, *p;
663 efi_memory_desc_t *md;
664 u64 efi_desc_size;
666 efi_map_start = __va(ia64_boot_param->efi_memmap);
667 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
668 efi_desc_size = ia64_boot_param->efi_memdesc_size;
670 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
671 md = p;
673 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
674 return md->attribute;
675 }
676 return 0;
677 }
678 EXPORT_SYMBOL(efi_mem_attributes);
680 int
681 valid_phys_addr_range (unsigned long phys_addr, unsigned long *size)
682 {
683 void *efi_map_start, *efi_map_end, *p;
684 efi_memory_desc_t *md;
685 u64 efi_desc_size;
687 efi_map_start = __va(ia64_boot_param->efi_memmap);
688 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
689 efi_desc_size = ia64_boot_param->efi_memdesc_size;
691 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
692 md = p;
694 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT)) {
695 if (!(md->attribute & EFI_MEMORY_WB))
696 return 0;
698 if (*size > md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - phys_addr)
699 *size = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - phys_addr;
700 return 1;
701 }
702 }
703 return 0;
704 }
706 int __init
707 efi_uart_console_only(void)
708 {
709 efi_status_t status;
710 char *s, name[] = "ConOut";
711 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
712 efi_char16_t *utf16, name_utf16[32];
713 unsigned char data[1024];
714 unsigned long size = sizeof(data);
715 struct efi_generic_dev_path *hdr, *end_addr;
716 int uart = 0;
718 /* Convert to UTF-16 */
719 utf16 = name_utf16;
720 s = name;
721 while (*s)
722 *utf16++ = *s++ & 0x7f;
723 *utf16 = 0;
725 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
726 if (status != EFI_SUCCESS) {
727 printk(KERN_ERR "No EFI %s variable?\n", name);
728 return 0;
729 }
731 hdr = (struct efi_generic_dev_path *) data;
732 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
733 while (hdr < end_addr) {
734 if (hdr->type == EFI_DEV_MSG &&
735 hdr->sub_type == EFI_DEV_MSG_UART)
736 uart = 1;
737 else if (hdr->type == EFI_DEV_END_PATH ||
738 hdr->type == EFI_DEV_END_PATH2) {
739 if (!uart)
740 return 0;
741 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
742 return 1;
743 uart = 0;
744 }
745 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
746 }
747 printk(KERN_ERR "Malformed %s value\n", name);
748 return 0;
749 }
751 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
753 static inline u64
754 kmd_end(kern_memdesc_t *kmd)
755 {
756 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
757 }
759 static inline u64
760 efi_md_end(efi_memory_desc_t *md)
761 {
762 return (md->phys_addr + efi_md_size(md));
763 }
765 static inline int
766 efi_wb(efi_memory_desc_t *md)
767 {
768 return (md->attribute & EFI_MEMORY_WB);
769 }
771 static inline int
772 efi_uc(efi_memory_desc_t *md)
773 {
774 return (md->attribute & EFI_MEMORY_UC);
775 }
777 /*
778 * Look for the first granule aligned memory descriptor memory
779 * that is big enough to hold EFI memory map. Make sure this
780 * descriptor is atleast granule sized so it does not get trimmed
781 */
782 struct kern_memdesc *
783 find_memmap_space (void)
784 {
785 u64 contig_low=0, contig_high=0;
786 u64 as = 0, ae;
787 void *efi_map_start, *efi_map_end, *p, *q;
788 efi_memory_desc_t *md, *pmd = NULL, *check_md;
789 u64 space_needed, efi_desc_size;
790 unsigned long total_mem = 0;
792 efi_map_start = __va(ia64_boot_param->efi_memmap);
793 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
794 efi_desc_size = ia64_boot_param->efi_memdesc_size;
796 /*
797 * Worst case: we need 3 kernel descriptors for each efi descriptor
798 * (if every entry has a WB part in the middle, and UC head and tail),
799 * plus one for the end marker.
800 */
801 space_needed = sizeof(kern_memdesc_t) *
802 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
804 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
805 md = p;
806 if (!efi_wb(md)) {
807 continue;
808 }
809 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
810 contig_low = GRANULEROUNDUP(md->phys_addr);
811 contig_high = efi_md_end(md);
812 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
813 check_md = q;
814 if (!efi_wb(check_md))
815 break;
816 if (contig_high != check_md->phys_addr)
817 break;
818 contig_high = efi_md_end(check_md);
819 }
820 contig_high = GRANULEROUNDDOWN(contig_high);
821 }
822 if (!is_available_memory(md) || md->type == EFI_LOADER_DATA)
823 continue;
825 /* Round ends inward to granule boundaries */
826 as = max(contig_low, md->phys_addr);
827 ae = min(contig_high, efi_md_end(md));
829 /* keep within max_addr= command line arg */
830 ae = min(ae, max_addr);
831 if (ae <= as)
832 continue;
834 /* avoid going over mem= command line arg */
835 if (total_mem + (ae - as) > mem_limit)
836 ae -= total_mem + (ae - as) - mem_limit;
838 if (ae <= as)
839 continue;
841 if (ae - as > space_needed)
842 break;
843 }
844 if (p >= efi_map_end)
845 panic("Can't allocate space for kernel memory descriptors");
847 return __va(as);
848 }
850 /*
851 * Walk the EFI memory map and gather all memory available for kernel
852 * to use. We can allocate partial granules only if the unavailable
853 * parts exist, and are WB.
854 */
855 void
856 efi_memmap_init(unsigned long *s, unsigned long *e)
857 {
858 struct kern_memdesc *k, *prev = 0;
859 u64 contig_low=0, contig_high=0;
860 u64 as, ae, lim;
861 void *efi_map_start, *efi_map_end, *p, *q;
862 efi_memory_desc_t *md, *pmd = NULL, *check_md;
863 u64 efi_desc_size;
864 unsigned long total_mem = 0;
866 k = kern_memmap = find_memmap_space();
868 efi_map_start = __va(ia64_boot_param->efi_memmap);
869 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
870 efi_desc_size = ia64_boot_param->efi_memdesc_size;
872 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
873 md = p;
874 if (!efi_wb(md)) {
875 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
876 md->type == EFI_BOOT_SERVICES_DATA)) {
877 k->attribute = EFI_MEMORY_UC;
878 k->start = md->phys_addr;
879 k->num_pages = md->num_pages;
880 k++;
881 }
882 continue;
883 }
884 #ifdef XEN
885 // this works around a problem in the ski bootloader
886 {
887 extern long running_on_sim;
888 if (running_on_sim && md->type != EFI_CONVENTIONAL_MEMORY)
889 continue;
890 }
891 #endif
892 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
893 contig_low = GRANULEROUNDUP(md->phys_addr);
894 contig_high = efi_md_end(md);
895 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
896 check_md = q;
897 if (!efi_wb(check_md))
898 break;
899 if (contig_high != check_md->phys_addr)
900 break;
901 contig_high = efi_md_end(check_md);
902 }
903 contig_high = GRANULEROUNDDOWN(contig_high);
904 }
905 if (!is_available_memory(md))
906 continue;
908 /*
909 * Round ends inward to granule boundaries
910 * Give trimmings to uncached allocator
911 */
912 if (md->phys_addr < contig_low) {
913 lim = min(efi_md_end(md), contig_low);
914 if (efi_uc(md)) {
915 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
916 kmd_end(k-1) == md->phys_addr) {
917 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
918 } else {
919 k->attribute = EFI_MEMORY_UC;
920 k->start = md->phys_addr;
921 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
922 k++;
923 }
924 }
925 as = contig_low;
926 } else
927 as = md->phys_addr;
929 if (efi_md_end(md) > contig_high) {
930 lim = max(md->phys_addr, contig_high);
931 if (efi_uc(md)) {
932 if (lim == md->phys_addr && k > kern_memmap &&
933 (k-1)->attribute == EFI_MEMORY_UC &&
934 kmd_end(k-1) == md->phys_addr) {
935 (k-1)->num_pages += md->num_pages;
936 } else {
937 k->attribute = EFI_MEMORY_UC;
938 k->start = lim;
939 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
940 k++;
941 }
942 }
943 ae = contig_high;
944 } else
945 ae = efi_md_end(md);
947 /* keep within max_addr= command line arg */
948 ae = min(ae, max_addr);
949 if (ae <= as)
950 continue;
952 /* avoid going over mem= command line arg */
953 if (total_mem + (ae - as) > mem_limit)
954 ae -= total_mem + (ae - as) - mem_limit;
956 if (ae <= as)
957 continue;
958 if (prev && kmd_end(prev) == md->phys_addr) {
959 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
960 total_mem += ae - as;
961 continue;
962 }
963 k->attribute = EFI_MEMORY_WB;
964 k->start = as;
965 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
966 total_mem += ae - as;
967 prev = k++;
968 }
969 k->start = ~0L; /* end-marker */
971 /* reserve the memory we are using for kern_memmap */
972 *s = (u64)kern_memmap;
973 *e = (u64)++k;
974 }