1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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>
31#include <linux/mm.h>
32
33#include <asm/io.h>
34#include <asm/kregs.h>
35#include <asm/meminit.h>
36#include <asm/pgtable.h>
37#include <asm/processor.h>
38#include <asm/mca.h>
39
40#define EFI_DEBUG 0
41
42extern efi_status_t efi_call_phys (void *, ...);
43
44struct efi efi;
45EXPORT_SYMBOL(efi);
46static efi_runtime_services_t *runtime;
47static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
48
49#define efi_call_virt(f, args...) (*(f))(args)
50
51#define STUB_GET_TIME(prefix, adjust_arg) \
52static efi_status_t \
53prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
54{ \
55 struct ia64_fpreg fr[6]; \
56 efi_time_cap_t *atc = NULL; \
57 efi_status_t ret; \
58 \
59 if (tc) \
60 atc = adjust_arg(tc); \
61 ia64_save_scratch_fpregs(fr); \
62 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
63 ia64_load_scratch_fpregs(fr); \
64 return ret; \
65}
66
67#define STUB_SET_TIME(prefix, adjust_arg) \
68static efi_status_t \
69prefix##_set_time (efi_time_t *tm) \
70{ \
71 struct ia64_fpreg fr[6]; \
72 efi_status_t ret; \
73 \
74 ia64_save_scratch_fpregs(fr); \
75 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
76 ia64_load_scratch_fpregs(fr); \
77 return ret; \
78}
79
80#define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
81static efi_status_t \
82prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
83{ \
84 struct ia64_fpreg fr[6]; \
85 efi_status_t ret; \
86 \
87 ia64_save_scratch_fpregs(fr); \
88 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
89 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
90 ia64_load_scratch_fpregs(fr); \
91 return ret; \
92}
93
94#define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
95static efi_status_t \
96prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
97{ \
98 struct ia64_fpreg fr[6]; \
99 efi_time_t *atm = NULL; \
100 efi_status_t ret; \
101 \
102 if (tm) \
103 atm = adjust_arg(tm); \
104 ia64_save_scratch_fpregs(fr); \
105 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
106 enabled, atm); \
107 ia64_load_scratch_fpregs(fr); \
108 return ret; \
109}
110
111#define STUB_GET_VARIABLE(prefix, adjust_arg) \
112static efi_status_t \
113prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
114 unsigned long *data_size, void *data) \
115{ \
116 struct ia64_fpreg fr[6]; \
117 u32 *aattr = NULL; \
118 efi_status_t ret; \
119 \
120 if (attr) \
121 aattr = adjust_arg(attr); \
122 ia64_save_scratch_fpregs(fr); \
123 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
124 adjust_arg(name), adjust_arg(vendor), aattr, \
125 adjust_arg(data_size), adjust_arg(data)); \
126 ia64_load_scratch_fpregs(fr); \
127 return ret; \
128}
129
130#define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
131static efi_status_t \
132prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
133{ \
134 struct ia64_fpreg fr[6]; \
135 efi_status_t ret; \
136 \
137 ia64_save_scratch_fpregs(fr); \
138 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
139 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
140 ia64_load_scratch_fpregs(fr); \
141 return ret; \
142}
143
144#define STUB_SET_VARIABLE(prefix, adjust_arg) \
145static efi_status_t \
146prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
147 unsigned long data_size, void *data) \
148{ \
149 struct ia64_fpreg fr[6]; \
150 efi_status_t ret; \
151 \
152 ia64_save_scratch_fpregs(fr); \
153 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
154 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
155 adjust_arg(data)); \
156 ia64_load_scratch_fpregs(fr); \
157 return ret; \
158}
159
160#define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
161static efi_status_t \
162prefix##_get_next_high_mono_count (u32 *count) \
163{ \
164 struct ia64_fpreg fr[6]; \
165 efi_status_t ret; \
166 \
167 ia64_save_scratch_fpregs(fr); \
168 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
169 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
170 ia64_load_scratch_fpregs(fr); \
171 return ret; \
172}
173
174#define STUB_RESET_SYSTEM(prefix, adjust_arg) \
175static void \
176prefix##_reset_system (int reset_type, efi_status_t status, \
177 unsigned long data_size, efi_char16_t *data) \
178{ \
179 struct ia64_fpreg fr[6]; \
180 efi_char16_t *adata = NULL; \
181 \
182 if (data) \
183 adata = adjust_arg(data); \
184 \
185 ia64_save_scratch_fpregs(fr); \
186 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
187 reset_type, status, data_size, adata); \
188 \
189 ia64_load_scratch_fpregs(fr); \
190}
191
192#define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
193
194STUB_GET_TIME(phys, phys_ptr)
195STUB_SET_TIME(phys, phys_ptr)
196STUB_GET_WAKEUP_TIME(phys, phys_ptr)
197STUB_SET_WAKEUP_TIME(phys, phys_ptr)
198STUB_GET_VARIABLE(phys, phys_ptr)
199STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
200STUB_SET_VARIABLE(phys, phys_ptr)
201STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
202STUB_RESET_SYSTEM(phys, phys_ptr)
203
204#define id(arg) arg
205
206STUB_GET_TIME(virt, id)
207STUB_SET_TIME(virt, id)
208STUB_GET_WAKEUP_TIME(virt, id)
209STUB_SET_WAKEUP_TIME(virt, id)
210STUB_GET_VARIABLE(virt, id)
211STUB_GET_NEXT_VARIABLE(virt, id)
212STUB_SET_VARIABLE(virt, id)
213STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
214STUB_RESET_SYSTEM(virt, id)
215
216void
217efi_gettimeofday (struct timespec *ts)
218{
219 efi_time_t tm;
220
221 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
222 memset(ts, 0, sizeof(*ts));
223 return;
224 }
225
226 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
227 ts->tv_nsec = tm.nanosecond;
228}
229
230static int
231is_memory_available (efi_memory_desc_t *md)
232{
233 if (!(md->attribute & EFI_MEMORY_WB))
234 return 0;
235
236 switch (md->type) {
237 case EFI_LOADER_CODE:
238 case EFI_LOADER_DATA:
239 case EFI_BOOT_SERVICES_CODE:
240 case EFI_BOOT_SERVICES_DATA:
241 case EFI_CONVENTIONAL_MEMORY:
242 return 1;
243 }
244 return 0;
245}
246
247typedef struct kern_memdesc {
248 u64 attribute;
249 u64 start;
250 u64 num_pages;
251} kern_memdesc_t;
252
253static kern_memdesc_t *kern_memmap;
254
255#define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
256
257static inline u64
258kmd_end(kern_memdesc_t *kmd)
259{
260 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
261}
262
263static inline u64
264efi_md_end(efi_memory_desc_t *md)
265{
266 return (md->phys_addr + efi_md_size(md));
267}
268
269static inline int
270efi_wb(efi_memory_desc_t *md)
271{
272 return (md->attribute & EFI_MEMORY_WB);
273}
274
275static inline int
276efi_uc(efi_memory_desc_t *md)
277{
278 return (md->attribute & EFI_MEMORY_UC);
279}
280
281static void
282walk (efi_freemem_callback_t callback, void *arg, u64 attr)
283{
284 kern_memdesc_t *k;
285 u64 start, end, voff;
286
287 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
288 for (k = kern_memmap; k->start != ~0UL; k++) {
289 if (k->attribute != attr)
290 continue;
291 start = PAGE_ALIGN(k->start);
292 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
293 if (start < end)
294 if ((*callback)(start + voff, end + voff, arg) < 0)
295 return;
296 }
297}
298
299
300
301
302
303void
304efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
305{
306 walk(callback, arg, EFI_MEMORY_WB);
307}
308
309
310
311
312
313void
314efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
315{
316 walk(callback, arg, EFI_MEMORY_UC);
317}
318
319
320
321
322
323
324
325void *
326efi_get_pal_addr (void)
327{
328 void *efi_map_start, *efi_map_end, *p;
329 efi_memory_desc_t *md;
330 u64 efi_desc_size;
331 int pal_code_count = 0;
332 u64 vaddr, mask;
333
334 efi_map_start = __va(ia64_boot_param->efi_memmap);
335 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
336 efi_desc_size = ia64_boot_param->efi_memdesc_size;
337
338 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
339 md = p;
340 if (md->type != EFI_PAL_CODE)
341 continue;
342
343 if (++pal_code_count > 1) {
344 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
345 md->phys_addr);
346 continue;
347 }
348
349
350
351
352 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
353 vaddr = PAGE_OFFSET + md->phys_addr;
354
355
356
357
358
359
360
361
362
363
364
365
366
367 if ((vaddr & mask) == (KERNEL_START & mask)) {
368 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
369 __FUNCTION__);
370 continue;
371 }
372
373 if (efi_md_size(md) > IA64_GRANULE_SIZE)
374 panic("Woah! PAL code size bigger than a granule!");
375
376#if EFI_DEBUG
377 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
378
379 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
380 smp_processor_id(), md->phys_addr,
381 md->phys_addr + efi_md_size(md),
382 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
383#endif
384 return __va(md->phys_addr);
385 }
386 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
387 __FUNCTION__);
388 return NULL;
389}
390
391void
392efi_map_pal_code (void)
393{
394 void *pal_vaddr = efi_get_pal_addr ();
395 u64 psr;
396
397 if (!pal_vaddr)
398 return;
399
400
401
402
403 psr = ia64_clear_ic();
404 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
405 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
406 IA64_GRANULE_SHIFT);
407 ia64_set_psr(psr);
408 ia64_srlz_i();
409}
410
411void __init
412efi_init (void)
413{
414 void *efi_map_start, *efi_map_end;
415 efi_config_table_t *config_tables;
416 efi_char16_t *c16;
417 u64 efi_desc_size;
418 char *cp, vendor[100] = "unknown";
419 int i;
420
421
422 for (cp = boot_command_line; *cp; ) {
423 if (memcmp(cp, "mem=", 4) == 0) {
424 mem_limit = memparse(cp + 4, &cp);
425 } else if (memcmp(cp, "max_addr=", 9) == 0) {
426 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
427 } else if (memcmp(cp, "min_addr=", 9) == 0) {
428 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
429 } else {
430 while (*cp != ' ' && *cp)
431 ++cp;
432 while (*cp == ' ')
433 ++cp;
434 }
435 }
436 if (min_addr != 0UL)
437 printk(KERN_INFO "Ignoring memory below %luMB\n", min_addr >> 20);
438 if (max_addr != ~0UL)
439 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
440
441 efi.systab = __va(ia64_boot_param->efi_systab);
442
443
444
445
446 if (efi.systab == NULL)
447 panic("Woah! Can't find EFI system table.\n");
448 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
449 panic("Woah! EFI system table signature incorrect\n");
450 if ((efi.systab->hdr.revision >> 16) == 0)
451 printk(KERN_WARNING "Warning: EFI system table version "
452 "%d.%02d, expected 1.00 or greater\n",
453 efi.systab->hdr.revision >> 16,
454 efi.systab->hdr.revision & 0xffff);
455
456 config_tables = __va(efi.systab->tables);
457
458
459 c16 = __va(efi.systab->fw_vendor);
460 if (c16) {
461 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
462 vendor[i] = *c16++;
463 vendor[i] = '\0';
464 }
465
466 printk(KERN_INFO "EFI v%u.%.02u by %s:",
467 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
468
469 efi.mps = EFI_INVALID_TABLE_ADDR;
470 efi.acpi = EFI_INVALID_TABLE_ADDR;
471 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
472 efi.smbios = EFI_INVALID_TABLE_ADDR;
473 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
474 efi.boot_info = EFI_INVALID_TABLE_ADDR;
475 efi.hcdp = EFI_INVALID_TABLE_ADDR;
476 efi.uga = EFI_INVALID_TABLE_ADDR;
477
478 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
479 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
480 efi.mps = config_tables[i].table;
481 printk(" MPS=0x%lx", config_tables[i].table);
482 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
483 efi.acpi20 = config_tables[i].table;
484 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
485 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
486 efi.acpi = config_tables[i].table;
487 printk(" ACPI=0x%lx", config_tables[i].table);
488 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
489 efi.smbios = config_tables[i].table;
490 printk(" SMBIOS=0x%lx", config_tables[i].table);
491 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
492 efi.sal_systab = config_tables[i].table;
493 printk(" SALsystab=0x%lx", config_tables[i].table);
494 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
495 efi.hcdp = config_tables[i].table;
496 printk(" HCDP=0x%lx", config_tables[i].table);
497 }
498 }
499 printk("\n");
500
501 runtime = __va(efi.systab->runtime);
502 efi.get_time = phys_get_time;
503 efi.set_time = phys_set_time;
504 efi.get_wakeup_time = phys_get_wakeup_time;
505 efi.set_wakeup_time = phys_set_wakeup_time;
506 efi.get_variable = phys_get_variable;
507 efi.get_next_variable = phys_get_next_variable;
508 efi.set_variable = phys_set_variable;
509 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
510 efi.reset_system = phys_reset_system;
511
512 efi_map_start = __va(ia64_boot_param->efi_memmap);
513 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
514 efi_desc_size = ia64_boot_param->efi_memdesc_size;
515
516#if EFI_DEBUG
517
518 {
519 efi_memory_desc_t *md;
520 void *p;
521
522 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
523 md = p;
524 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
525 i, md->type, md->attribute, md->phys_addr,
526 md->phys_addr + efi_md_size(md),
527 md->num_pages >> (20 - EFI_PAGE_SHIFT));
528 }
529 }
530#endif
531
532 efi_map_pal_code();
533 efi_enter_virtual_mode();
534}
535
536void
537efi_enter_virtual_mode (void)
538{
539 void *efi_map_start, *efi_map_end, *p;
540 efi_memory_desc_t *md;
541 efi_status_t status;
542 u64 efi_desc_size;
543
544 efi_map_start = __va(ia64_boot_param->efi_memmap);
545 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
546 efi_desc_size = ia64_boot_param->efi_memdesc_size;
547
548 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
549 md = p;
550 if (md->attribute & EFI_MEMORY_RUNTIME) {
551
552
553
554
555 if (md->attribute & EFI_MEMORY_WB) {
556 md->virt_addr = (u64) __va(md->phys_addr);
557 } else if (md->attribute & EFI_MEMORY_UC) {
558 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
559 } else if (md->attribute & EFI_MEMORY_WC) {
560#if 0
561 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
562 | _PAGE_D
563 | _PAGE_MA_WC
564 | _PAGE_PL_0
565 | _PAGE_AR_RW));
566#else
567 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
568 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
569#endif
570 } else if (md->attribute & EFI_MEMORY_WT) {
571#if 0
572 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
573 | _PAGE_D | _PAGE_MA_WT
574 | _PAGE_PL_0
575 | _PAGE_AR_RW));
576#else
577 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
578 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
579#endif
580 }
581 }
582 }
583
584 status = efi_call_phys(__va(runtime->set_virtual_address_map),
585 ia64_boot_param->efi_memmap_size,
586 efi_desc_size, ia64_boot_param->efi_memdesc_version,
587 ia64_boot_param->efi_memmap);
588 if (status != EFI_SUCCESS) {
589 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
590 "(status=%lu)\n", status);
591 return;
592 }
593
594
595
596
597 efi.get_time = virt_get_time;
598 efi.set_time = virt_set_time;
599 efi.get_wakeup_time = virt_get_wakeup_time;
600 efi.set_wakeup_time = virt_set_wakeup_time;
601 efi.get_variable = virt_get_variable;
602 efi.get_next_variable = virt_get_next_variable;
603 efi.set_variable = virt_set_variable;
604 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
605 efi.reset_system = virt_reset_system;
606}
607
608
609
610
611
612u64
613efi_get_iobase (void)
614{
615 void *efi_map_start, *efi_map_end, *p;
616 efi_memory_desc_t *md;
617 u64 efi_desc_size;
618
619 efi_map_start = __va(ia64_boot_param->efi_memmap);
620 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
621 efi_desc_size = ia64_boot_param->efi_memdesc_size;
622
623 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
624 md = p;
625 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
626 if (md->attribute & EFI_MEMORY_UC)
627 return md->phys_addr;
628 }
629 }
630 return 0;
631}
632
633static struct kern_memdesc *
634kern_memory_descriptor (unsigned long phys_addr)
635{
636 struct kern_memdesc *md;
637
638 for (md = kern_memmap; md->start != ~0UL; md++) {
639 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
640 return md;
641 }
642 return NULL;
643}
644
645static efi_memory_desc_t *
646efi_memory_descriptor (unsigned long phys_addr)
647{
648 void *efi_map_start, *efi_map_end, *p;
649 efi_memory_desc_t *md;
650 u64 efi_desc_size;
651
652 efi_map_start = __va(ia64_boot_param->efi_memmap);
653 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
654 efi_desc_size = ia64_boot_param->efi_memdesc_size;
655
656 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
657 md = p;
658
659 if (phys_addr - md->phys_addr < efi_md_size(md))
660 return md;
661 }
662 return NULL;
663}
664
665static int
666efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
667{
668 void *efi_map_start, *efi_map_end, *p;
669 efi_memory_desc_t *md;
670 u64 efi_desc_size;
671 unsigned long end;
672
673 efi_map_start = __va(ia64_boot_param->efi_memmap);
674 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
675 efi_desc_size = ia64_boot_param->efi_memdesc_size;
676
677 end = phys_addr + size;
678
679 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
680 md = p;
681
682 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
683 return 1;
684 }
685 return 0;
686}
687
688u32
689efi_mem_type (unsigned long phys_addr)
690{
691 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
692
693 if (md)
694 return md->type;
695 return 0;
696}
697
698u64
699efi_mem_attributes (unsigned long phys_addr)
700{
701 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
702
703 if (md)
704 return md->attribute;
705 return 0;
706}
707EXPORT_SYMBOL(efi_mem_attributes);
708
709u64
710efi_mem_attribute (unsigned long phys_addr, unsigned long size)
711{
712 unsigned long end = phys_addr + size;
713 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
714 u64 attr;
715
716 if (!md)
717 return 0;
718
719
720
721
722
723 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
724 do {
725 unsigned long md_end = efi_md_end(md);
726
727 if (end <= md_end)
728 return attr;
729
730 md = efi_memory_descriptor(md_end);
731 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
732 return 0;
733 } while (md);
734 return 0;
735}
736
737u64
738kern_mem_attribute (unsigned long phys_addr, unsigned long size)
739{
740 unsigned long end = phys_addr + size;
741 struct kern_memdesc *md;
742 u64 attr;
743
744
745
746
747
748 if (!kern_memmap) {
749 attr = efi_mem_attribute(phys_addr, size);
750 if (attr & EFI_MEMORY_WB)
751 return EFI_MEMORY_WB;
752 return 0;
753 }
754
755 md = kern_memory_descriptor(phys_addr);
756 if (!md)
757 return 0;
758
759 attr = md->attribute;
760 do {
761 unsigned long md_end = kmd_end(md);
762
763 if (end <= md_end)
764 return attr;
765
766 md = kern_memory_descriptor(md_end);
767 if (!md || md->attribute != attr)
768 return 0;
769 } while (md);
770 return 0;
771}
772EXPORT_SYMBOL(kern_mem_attribute);
773
774int
775valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
776{
777 u64 attr;
778
779
780
781
782
783
784
785 attr = kern_mem_attribute(phys_addr, size);
786 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
787 return 1;
788 return 0;
789}
790
791int
792valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
793{
794 unsigned long phys_addr = pfn << PAGE_SHIFT;
795 u64 attr;
796
797 attr = efi_mem_attribute(phys_addr, size);
798
799
800
801
802
803
804 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
805 return 1;
806
807
808
809
810
811
812
813 if (efi_memmap_intersects(phys_addr, size))
814 return 0;
815
816 return 1;
817}
818
819pgprot_t
820phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
821 pgprot_t vma_prot)
822{
823 unsigned long phys_addr = pfn << PAGE_SHIFT;
824 u64 attr;
825
826
827
828
829
830
831 attr = kern_mem_attribute(phys_addr, size);
832 if (attr & EFI_MEMORY_WB)
833 return pgprot_cacheable(vma_prot);
834 else if (attr & EFI_MEMORY_UC)
835 return pgprot_noncached(vma_prot);
836
837
838
839
840
841 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
842 return pgprot_cacheable(vma_prot);
843
844 return pgprot_noncached(vma_prot);
845}
846
847int __init
848efi_uart_console_only(void)
849{
850 efi_status_t status;
851 char *s, name[] = "ConOut";
852 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
853 efi_char16_t *utf16, name_utf16[32];
854 unsigned char data[1024];
855 unsigned long size = sizeof(data);
856 struct efi_generic_dev_path *hdr, *end_addr;
857 int uart = 0;
858
859
860 utf16 = name_utf16;
861 s = name;
862 while (*s)
863 *utf16++ = *s++ & 0x7f;
864 *utf16 = 0;
865
866 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
867 if (status != EFI_SUCCESS) {
868 printk(KERN_ERR "No EFI %s variable?\n", name);
869 return 0;
870 }
871
872 hdr = (struct efi_generic_dev_path *) data;
873 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
874 while (hdr < end_addr) {
875 if (hdr->type == EFI_DEV_MSG &&
876 hdr->sub_type == EFI_DEV_MSG_UART)
877 uart = 1;
878 else if (hdr->type == EFI_DEV_END_PATH ||
879 hdr->type == EFI_DEV_END_PATH2) {
880 if (!uart)
881 return 0;
882 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
883 return 1;
884 uart = 0;
885 }
886 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
887 }
888 printk(KERN_ERR "Malformed %s value\n", name);
889 return 0;
890}
891
892
893
894
895
896
897struct kern_memdesc *
898find_memmap_space (void)
899{
900 u64 contig_low=0, contig_high=0;
901 u64 as = 0, ae;
902 void *efi_map_start, *efi_map_end, *p, *q;
903 efi_memory_desc_t *md, *pmd = NULL, *check_md;
904 u64 space_needed, efi_desc_size;
905 unsigned long total_mem = 0;
906
907 efi_map_start = __va(ia64_boot_param->efi_memmap);
908 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
909 efi_desc_size = ia64_boot_param->efi_memdesc_size;
910
911
912
913
914
915
916 space_needed = sizeof(kern_memdesc_t) *
917 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
918
919 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
920 md = p;
921 if (!efi_wb(md)) {
922 continue;
923 }
924 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
925 contig_low = GRANULEROUNDUP(md->phys_addr);
926 contig_high = efi_md_end(md);
927 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
928 check_md = q;
929 if (!efi_wb(check_md))
930 break;
931 if (contig_high != check_md->phys_addr)
932 break;
933 contig_high = efi_md_end(check_md);
934 }
935 contig_high = GRANULEROUNDDOWN(contig_high);
936 }
937 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
938 continue;
939
940
941 as = max(contig_low, md->phys_addr);
942 ae = min(contig_high, efi_md_end(md));
943
944
945 as = max(as, min_addr);
946 ae = min(ae, max_addr);
947 if (ae <= as)
948 continue;
949
950
951 if (total_mem + (ae - as) > mem_limit)
952 ae -= total_mem + (ae - as) - mem_limit;
953
954 if (ae <= as)
955 continue;
956
957 if (ae - as > space_needed)
958 break;
959 }
960 if (p >= efi_map_end)
961 panic("Can't allocate space for kernel memory descriptors");
962
963 return __va(as);
964}
965
966
967
968
969
970
971unsigned long
972efi_memmap_init(unsigned long *s, unsigned long *e)
973{
974 struct kern_memdesc *k, *prev = NULL;
975 u64 contig_low=0, contig_high=0;
976 u64 as, ae, lim;
977 void *efi_map_start, *efi_map_end, *p, *q;
978 efi_memory_desc_t *md, *pmd = NULL, *check_md;
979 u64 efi_desc_size;
980 unsigned long total_mem = 0;
981
982 k = kern_memmap = find_memmap_space();
983
984 efi_map_start = __va(ia64_boot_param->efi_memmap);
985 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
986 efi_desc_size = ia64_boot_param->efi_memdesc_size;
987
988 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
989 md = p;
990 if (!efi_wb(md)) {
991 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
992 md->type == EFI_BOOT_SERVICES_DATA)) {
993 k->attribute = EFI_MEMORY_UC;
994 k->start = md->phys_addr;
995 k->num_pages = md->num_pages;
996 k++;
997 }
998 continue;
999 }
1000 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
1001 contig_low = GRANULEROUNDUP(md->phys_addr);
1002 contig_high = efi_md_end(md);
1003 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
1004 check_md = q;
1005 if (!efi_wb(check_md))
1006 break;
1007 if (contig_high != check_md->phys_addr)
1008 break;
1009 contig_high = efi_md_end(check_md);
1010 }
1011 contig_high = GRANULEROUNDDOWN(contig_high);
1012 }
1013 if (!is_memory_available(md))
1014 continue;
1015
1016#ifdef CONFIG_CRASH_DUMP
1017
1018 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1019 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1020#endif
1021
1022
1023
1024
1025 if (md->phys_addr < contig_low) {
1026 lim = min(efi_md_end(md), contig_low);
1027 if (efi_uc(md)) {
1028 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
1029 kmd_end(k-1) == md->phys_addr) {
1030 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1031 } else {
1032 k->attribute = EFI_MEMORY_UC;
1033 k->start = md->phys_addr;
1034 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1035 k++;
1036 }
1037 }
1038 as = contig_low;
1039 } else
1040 as = md->phys_addr;
1041
1042 if (efi_md_end(md) > contig_high) {
1043 lim = max(md->phys_addr, contig_high);
1044 if (efi_uc(md)) {
1045 if (lim == md->phys_addr && k > kern_memmap &&
1046 (k-1)->attribute == EFI_MEMORY_UC &&
1047 kmd_end(k-1) == md->phys_addr) {
1048 (k-1)->num_pages += md->num_pages;
1049 } else {
1050 k->attribute = EFI_MEMORY_UC;
1051 k->start = lim;
1052 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1053 k++;
1054 }
1055 }
1056 ae = contig_high;
1057 } else
1058 ae = efi_md_end(md);
1059
1060
1061 as = max(as, min_addr);
1062 ae = min(ae, max_addr);
1063 if (ae <= as)
1064 continue;
1065
1066
1067 if (total_mem + (ae - as) > mem_limit)
1068 ae -= total_mem + (ae - as) - mem_limit;
1069
1070 if (ae <= as)
1071 continue;
1072 if (prev && kmd_end(prev) == md->phys_addr) {
1073 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1074 total_mem += ae - as;
1075 continue;
1076 }
1077 k->attribute = EFI_MEMORY_WB;
1078 k->start = as;
1079 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1080 total_mem += ae - as;
1081 prev = k++;
1082 }
1083 k->start = ~0L;
1084
1085
1086 *s = (u64)kern_memmap;
1087 *e = (u64)++k;
1088
1089 return total_mem;
1090}
1091
1092void
1093efi_initialize_iomem_resources(struct resource *code_resource,
1094 struct resource *data_resource,
1095 struct resource *bss_resource)
1096{
1097 struct resource *res;
1098 void *efi_map_start, *efi_map_end, *p;
1099 efi_memory_desc_t *md;
1100 u64 efi_desc_size;
1101 char *name;
1102 unsigned long flags;
1103
1104 efi_map_start = __va(ia64_boot_param->efi_memmap);
1105 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1106 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1107
1108 res = NULL;
1109
1110 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1111 md = p;
1112
1113 if (md->num_pages == 0)
1114 continue;
1115
1116 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1117 switch (md->type) {
1118
1119 case EFI_MEMORY_MAPPED_IO:
1120 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1121 continue;
1122
1123 case EFI_LOADER_CODE:
1124 case EFI_LOADER_DATA:
1125 case EFI_BOOT_SERVICES_DATA:
1126 case EFI_BOOT_SERVICES_CODE:
1127 case EFI_CONVENTIONAL_MEMORY:
1128 if (md->attribute & EFI_MEMORY_WP) {
1129 name = "System ROM";
1130 flags |= IORESOURCE_READONLY;
1131 } else {
1132 name = "System RAM";
1133 }
1134 break;
1135
1136 case EFI_ACPI_MEMORY_NVS:
1137 name = "ACPI Non-volatile Storage";
1138 break;
1139
1140 case EFI_UNUSABLE_MEMORY:
1141 name = "reserved";
1142 flags |= IORESOURCE_DISABLED;
1143 break;
1144
1145 case EFI_RESERVED_TYPE:
1146 case EFI_RUNTIME_SERVICES_CODE:
1147 case EFI_RUNTIME_SERVICES_DATA:
1148 case EFI_ACPI_RECLAIM_MEMORY:
1149 default:
1150 name = "reserved";
1151 break;
1152 }
1153
1154 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1155 printk(KERN_ERR "failed to alocate resource for iomem\n");
1156 return;
1157 }
1158
1159 res->name = name;
1160 res->start = md->phys_addr;
1161 res->end = md->phys_addr + efi_md_size(md) - 1;
1162 res->flags = flags;
1163
1164 if (insert_resource(&iomem_resource, res) < 0)
1165 kfree(res);
1166 else {
1167
1168
1169
1170
1171
1172 insert_resource(res, code_resource);
1173 insert_resource(res, data_resource);
1174 insert_resource(res, bss_resource);
1175#ifdef CONFIG_KEXEC
1176 insert_resource(res, &efi_memmap_res);
1177 insert_resource(res, &boot_param_res);
1178 if (crashk_res.end > crashk_res.start)
1179 insert_resource(res, &crashk_res);
1180#endif
1181 }
1182 }
1183}
1184
1185#ifdef CONFIG_KEXEC
1186
1187
1188
1189unsigned long __init
1190kdump_find_rsvd_region (unsigned long size,
1191 struct rsvd_region *r, int n)
1192{
1193 int i;
1194 u64 start, end;
1195 u64 alignment = 1UL << _PAGE_SIZE_64M;
1196 void *efi_map_start, *efi_map_end, *p;
1197 efi_memory_desc_t *md;
1198 u64 efi_desc_size;
1199
1200 efi_map_start = __va(ia64_boot_param->efi_memmap);
1201 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1202 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1203
1204 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1205 md = p;
1206 if (!efi_wb(md))
1207 continue;
1208 start = ALIGN(md->phys_addr, alignment);
1209 end = efi_md_end(md);
1210 for (i = 0; i < n; i++) {
1211 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1212 if (__pa(r[i].start) > start + size)
1213 return start;
1214 start = ALIGN(__pa(r[i].end), alignment);
1215 if (i < n-1 && __pa(r[i+1].start) < start + size)
1216 continue;
1217 else
1218 break;
1219 }
1220 }
1221 if (end > start + size)
1222 return start;
1223 }
1224
1225 printk(KERN_WARNING "Cannot reserve 0x%lx byte of memory for crashdump\n",
1226 size);
1227 return ~0UL;
1228}
1229#endif
1230
1231#ifdef CONFIG_PROC_VMCORE
1232
1233unsigned long __init
1234vmcore_find_descriptor_size (unsigned long address)
1235{
1236 void *efi_map_start, *efi_map_end, *p;
1237 efi_memory_desc_t *md;
1238 u64 efi_desc_size;
1239 unsigned long ret = 0;
1240
1241 efi_map_start = __va(ia64_boot_param->efi_memmap);
1242 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1243 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1244
1245 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1246 md = p;
1247 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1248 && md->phys_addr == address) {
1249 ret = efi_md_size(md);
1250 break;
1251 }
1252 }
1253
1254 if (ret == 0)
1255 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1256
1257 return ret;
1258}
1259#endif
1260