xref: /openbmc/linux/arch/ia64/kernel/efi.c (revision 62eab49f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Extensible Firmware Interface
4  *
5  * Based on Extensible Firmware Interface Specification version 0.9
6  * April 30, 1999
7  *
8  * Copyright (C) 1999 VA Linux Systems
9  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
10  * Copyright (C) 1999-2003 Hewlett-Packard Co.
11  *	David Mosberger-Tang <davidm@hpl.hp.com>
12  *	Stephane Eranian <eranian@hpl.hp.com>
13  * (c) Copyright 2006 Hewlett-Packard Development Company, L.P.
14  *	Bjorn Helgaas <bjorn.helgaas@hp.com>
15  *
16  * All EFI Runtime Services are not implemented yet as EFI only
17  * supports physical mode addressing on SoftSDV. This is to be fixed
18  * in a future version.  --drummond 1999-07-20
19  *
20  * Implemented EFI runtime services and virtual mode calls.  --davidm
21  *
22  * Goutham Rao: <goutham.rao@intel.com>
23  *	Skip non-WB memory and ignore empty memory ranges.
24  */
25 #include <linux/module.h>
26 #include <linux/memblock.h>
27 #include <linux/crash_dump.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/efi.h>
34 #include <linux/kexec.h>
35 #include <linux/mm.h>
36 
37 #include <asm/efi.h>
38 #include <asm/io.h>
39 #include <asm/kregs.h>
40 #include <asm/meminit.h>
41 #include <asm/processor.h>
42 #include <asm/mca.h>
43 #include <asm/sal.h>
44 #include <asm/setup.h>
45 #include <asm/tlbflush.h>
46 
47 #define EFI_DEBUG	0
48 
49 #define ESI_TABLE_GUID					\
50     EFI_GUID(0x43EA58DC, 0xCF28, 0x4b06, 0xB3,		\
51 	     0x91, 0xB7, 0x50, 0x59, 0x34, 0x2B, 0xD4)
52 
53 static unsigned long mps_phys = EFI_INVALID_TABLE_ADDR;
54 static __initdata unsigned long palo_phys;
55 
56 unsigned long __initdata esi_phys = EFI_INVALID_TABLE_ADDR;
57 unsigned long hcdp_phys = EFI_INVALID_TABLE_ADDR;
58 unsigned long sal_systab_phys = EFI_INVALID_TABLE_ADDR;
59 
60 static const efi_config_table_type_t arch_tables[] __initconst = {
61 	{ESI_TABLE_GUID,				&esi_phys,		"ESI"		},
62 	{HCDP_TABLE_GUID,				&hcdp_phys,		"HCDP"		},
63 	{MPS_TABLE_GUID,				&mps_phys,		"MPS"		},
64 	{PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID,	&palo_phys,		"PALO"		},
65 	{SAL_SYSTEM_TABLE_GUID,				&sal_systab_phys,	"SALsystab"	},
66 	{},
67 };
68 
69 extern efi_status_t efi_call_phys (void *, ...);
70 
71 static efi_runtime_services_t *runtime;
72 static u64 mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
73 
74 #define efi_call_virt(f, args...)	(*(f))(args)
75 
76 #define STUB_GET_TIME(prefix, adjust_arg)				       \
77 static efi_status_t							       \
78 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc)			       \
79 {									       \
80 	struct ia64_fpreg fr[6];					       \
81 	efi_time_cap_t *atc = NULL;					       \
82 	efi_status_t ret;						       \
83 									       \
84 	if (tc)								       \
85 		atc = adjust_arg(tc);					       \
86 	ia64_save_scratch_fpregs(fr);					       \
87 	ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time),    \
88 				adjust_arg(tm), atc);			       \
89 	ia64_load_scratch_fpregs(fr);					       \
90 	return ret;							       \
91 }
92 
93 #define STUB_SET_TIME(prefix, adjust_arg)				       \
94 static efi_status_t							       \
95 prefix##_set_time (efi_time_t *tm)					       \
96 {									       \
97 	struct ia64_fpreg fr[6];					       \
98 	efi_status_t ret;						       \
99 									       \
100 	ia64_save_scratch_fpregs(fr);					       \
101 	ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time),    \
102 				adjust_arg(tm));			       \
103 	ia64_load_scratch_fpregs(fr);					       \
104 	return ret;							       \
105 }
106 
107 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg)			       \
108 static efi_status_t							       \
109 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending,	       \
110 			  efi_time_t *tm)				       \
111 {									       \
112 	struct ia64_fpreg fr[6];					       \
113 	efi_status_t ret;						       \
114 									       \
115 	ia64_save_scratch_fpregs(fr);					       \
116 	ret = efi_call_##prefix(					       \
117 		(efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time),      \
118 		adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm));     \
119 	ia64_load_scratch_fpregs(fr);					       \
120 	return ret;							       \
121 }
122 
123 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg)			       \
124 static efi_status_t							       \
125 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm)		       \
126 {									       \
127 	struct ia64_fpreg fr[6];					       \
128 	efi_time_t *atm = NULL;						       \
129 	efi_status_t ret;						       \
130 									       \
131 	if (tm)								       \
132 		atm = adjust_arg(tm);					       \
133 	ia64_save_scratch_fpregs(fr);					       \
134 	ret = efi_call_##prefix(					       \
135 		(efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time),      \
136 		enabled, atm);						       \
137 	ia64_load_scratch_fpregs(fr);					       \
138 	return ret;							       \
139 }
140 
141 #define STUB_GET_VARIABLE(prefix, adjust_arg)				       \
142 static efi_status_t							       \
143 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr,      \
144 		       unsigned long *data_size, void *data)		       \
145 {									       \
146 	struct ia64_fpreg fr[6];					       \
147 	u32 *aattr = NULL;						       \
148 	efi_status_t ret;						       \
149 									       \
150 	if (attr)							       \
151 		aattr = adjust_arg(attr);				       \
152 	ia64_save_scratch_fpregs(fr);					       \
153 	ret = efi_call_##prefix(					       \
154 		(efi_get_variable_t *) __va(runtime->get_variable),	       \
155 		adjust_arg(name), adjust_arg(vendor), aattr,		       \
156 		adjust_arg(data_size), adjust_arg(data));		       \
157 	ia64_load_scratch_fpregs(fr);					       \
158 	return ret;							       \
159 }
160 
161 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg)			       \
162 static efi_status_t							       \
163 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name,      \
164 			    efi_guid_t *vendor)				       \
165 {									       \
166 	struct ia64_fpreg fr[6];					       \
167 	efi_status_t ret;						       \
168 									       \
169 	ia64_save_scratch_fpregs(fr);					       \
170 	ret = efi_call_##prefix(					       \
171 		(efi_get_next_variable_t *) __va(runtime->get_next_variable),  \
172 		adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor));  \
173 	ia64_load_scratch_fpregs(fr);					       \
174 	return ret;							       \
175 }
176 
177 #define STUB_SET_VARIABLE(prefix, adjust_arg)				       \
178 static efi_status_t							       \
179 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor,		       \
180 		       u32 attr, unsigned long data_size,		       \
181 		       void *data)					       \
182 {									       \
183 	struct ia64_fpreg fr[6];					       \
184 	efi_status_t ret;						       \
185 									       \
186 	ia64_save_scratch_fpregs(fr);					       \
187 	ret = efi_call_##prefix(					       \
188 		(efi_set_variable_t *) __va(runtime->set_variable),	       \
189 		adjust_arg(name), adjust_arg(vendor), attr, data_size,	       \
190 		adjust_arg(data));					       \
191 	ia64_load_scratch_fpregs(fr);					       \
192 	return ret;							       \
193 }
194 
195 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg)		       \
196 static efi_status_t							       \
197 prefix##_get_next_high_mono_count (u32 *count)				       \
198 {									       \
199 	struct ia64_fpreg fr[6];					       \
200 	efi_status_t ret;						       \
201 									       \
202 	ia64_save_scratch_fpregs(fr);					       \
203 	ret = efi_call_##prefix((efi_get_next_high_mono_count_t *)	       \
204 				__va(runtime->get_next_high_mono_count),       \
205 				adjust_arg(count));			       \
206 	ia64_load_scratch_fpregs(fr);					       \
207 	return ret;							       \
208 }
209 
210 #define STUB_RESET_SYSTEM(prefix, adjust_arg)				       \
211 static void								       \
212 prefix##_reset_system (int reset_type, efi_status_t status,		       \
213 		       unsigned long data_size, efi_char16_t *data)	       \
214 {									       \
215 	struct ia64_fpreg fr[6];					       \
216 	efi_char16_t *adata = NULL;					       \
217 									       \
218 	if (data)							       \
219 		adata = adjust_arg(data);				       \
220 									       \
221 	ia64_save_scratch_fpregs(fr);					       \
222 	efi_call_##prefix(						       \
223 		(efi_reset_system_t *) __va(runtime->reset_system),	       \
224 		reset_type, status, data_size, adata);			       \
225 	/* should not return, but just in case... */			       \
226 	ia64_load_scratch_fpregs(fr);					       \
227 }
228 
229 #define phys_ptr(arg)	((__typeof__(arg)) ia64_tpa(arg))
230 
231 STUB_GET_TIME(phys, phys_ptr)
232 STUB_SET_TIME(phys, phys_ptr)
233 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
234 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
235 STUB_GET_VARIABLE(phys, phys_ptr)
236 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
237 STUB_SET_VARIABLE(phys, phys_ptr)
238 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
239 STUB_RESET_SYSTEM(phys, phys_ptr)
240 
241 #define id(arg)	arg
242 
243 STUB_GET_TIME(virt, id)
244 STUB_SET_TIME(virt, id)
245 STUB_GET_WAKEUP_TIME(virt, id)
246 STUB_SET_WAKEUP_TIME(virt, id)
247 STUB_GET_VARIABLE(virt, id)
248 STUB_GET_NEXT_VARIABLE(virt, id)
249 STUB_SET_VARIABLE(virt, id)
250 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
251 STUB_RESET_SYSTEM(virt, id)
252 
253 void
254 efi_gettimeofday (struct timespec64 *ts)
255 {
256 	efi_time_t tm;
257 
258 	if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
259 		memset(ts, 0, sizeof(*ts));
260 		return;
261 	}
262 
263 	ts->tv_sec = mktime64(tm.year, tm.month, tm.day,
264 			    tm.hour, tm.minute, tm.second);
265 	ts->tv_nsec = tm.nanosecond;
266 }
267 
268 static int
269 is_memory_available (efi_memory_desc_t *md)
270 {
271 	if (!(md->attribute & EFI_MEMORY_WB))
272 		return 0;
273 
274 	switch (md->type) {
275 	      case EFI_LOADER_CODE:
276 	      case EFI_LOADER_DATA:
277 	      case EFI_BOOT_SERVICES_CODE:
278 	      case EFI_BOOT_SERVICES_DATA:
279 	      case EFI_CONVENTIONAL_MEMORY:
280 		return 1;
281 	}
282 	return 0;
283 }
284 
285 typedef struct kern_memdesc {
286 	u64 attribute;
287 	u64 start;
288 	u64 num_pages;
289 } kern_memdesc_t;
290 
291 static kern_memdesc_t *kern_memmap;
292 
293 #define efi_md_size(md)	(md->num_pages << EFI_PAGE_SHIFT)
294 
295 static inline u64
296 kmd_end(kern_memdesc_t *kmd)
297 {
298 	return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
299 }
300 
301 static inline u64
302 efi_md_end(efi_memory_desc_t *md)
303 {
304 	return (md->phys_addr + efi_md_size(md));
305 }
306 
307 static inline int
308 efi_wb(efi_memory_desc_t *md)
309 {
310 	return (md->attribute & EFI_MEMORY_WB);
311 }
312 
313 static inline int
314 efi_uc(efi_memory_desc_t *md)
315 {
316 	return (md->attribute & EFI_MEMORY_UC);
317 }
318 
319 static void
320 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
321 {
322 	kern_memdesc_t *k;
323 	u64 start, end, voff;
324 
325 	voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
326 	for (k = kern_memmap; k->start != ~0UL; k++) {
327 		if (k->attribute != attr)
328 			continue;
329 		start = PAGE_ALIGN(k->start);
330 		end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
331 		if (start < end)
332 			if ((*callback)(start + voff, end + voff, arg) < 0)
333 				return;
334 	}
335 }
336 
337 /*
338  * Walk the EFI memory map and call CALLBACK once for each EFI memory
339  * descriptor that has memory that is available for OS use.
340  */
341 void
342 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
343 {
344 	walk(callback, arg, EFI_MEMORY_WB);
345 }
346 
347 /*
348  * Walk the EFI memory map and call CALLBACK once for each EFI memory
349  * descriptor that has memory that is available for uncached allocator.
350  */
351 void
352 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
353 {
354 	walk(callback, arg, EFI_MEMORY_UC);
355 }
356 
357 /*
358  * Look for the PAL_CODE region reported by EFI and map it using an
359  * ITR to enable safe PAL calls in virtual mode.  See IA-64 Processor
360  * Abstraction Layer chapter 11 in ADAG
361  */
362 void *
363 efi_get_pal_addr (void)
364 {
365 	void *efi_map_start, *efi_map_end, *p;
366 	efi_memory_desc_t *md;
367 	u64 efi_desc_size;
368 	int pal_code_count = 0;
369 	u64 vaddr, mask;
370 
371 	efi_map_start = __va(ia64_boot_param->efi_memmap);
372 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
373 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
374 
375 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
376 		md = p;
377 		if (md->type != EFI_PAL_CODE)
378 			continue;
379 
380 		if (++pal_code_count > 1) {
381 			printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
382 			       "dropped @ %llx\n", md->phys_addr);
383 			continue;
384 		}
385 		/*
386 		 * The only ITLB entry in region 7 that is used is the one
387 		 * installed by __start().  That entry covers a 64MB range.
388 		 */
389 		mask  = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
390 		vaddr = PAGE_OFFSET + md->phys_addr;
391 
392 		/*
393 		 * We must check that the PAL mapping won't overlap with the
394 		 * kernel mapping.
395 		 *
396 		 * PAL code is guaranteed to be aligned on a power of 2 between
397 		 * 4k and 256KB and that only one ITR is needed to map it. This
398 		 * implies that the PAL code is always aligned on its size,
399 		 * i.e., the closest matching page size supported by the TLB.
400 		 * Therefore PAL code is guaranteed never to cross a 64MB unless
401 		 * it is bigger than 64MB (very unlikely!).  So for now the
402 		 * following test is enough to determine whether or not we need
403 		 * a dedicated ITR for the PAL code.
404 		 */
405 		if ((vaddr & mask) == (KERNEL_START & mask)) {
406 			printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
407 			       __func__);
408 			continue;
409 		}
410 
411 		if (efi_md_size(md) > IA64_GRANULE_SIZE)
412 			panic("Whoa!  PAL code size bigger than a granule!");
413 
414 #if EFI_DEBUG
415 		mask  = ~((1 << IA64_GRANULE_SHIFT) - 1);
416 
417 		printk(KERN_INFO "CPU %d: mapping PAL code "
418                        "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
419                        smp_processor_id(), md->phys_addr,
420                        md->phys_addr + efi_md_size(md),
421                        vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
422 #endif
423 		return __va(md->phys_addr);
424 	}
425 	printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
426 	       __func__);
427 	return NULL;
428 }
429 
430 
431 static u8 __init palo_checksum(u8 *buffer, u32 length)
432 {
433 	u8 sum = 0;
434 	u8 *end = buffer + length;
435 
436 	while (buffer < end)
437 		sum = (u8) (sum + *(buffer++));
438 
439 	return sum;
440 }
441 
442 /*
443  * Parse and handle PALO table which is published at:
444  * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
445  */
446 static void __init handle_palo(unsigned long phys_addr)
447 {
448 	struct palo_table *palo = __va(phys_addr);
449 	u8  checksum;
450 
451 	if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) {
452 		printk(KERN_INFO "PALO signature incorrect.\n");
453 		return;
454 	}
455 
456 	checksum = palo_checksum((u8 *)palo, palo->length);
457 	if (checksum) {
458 		printk(KERN_INFO "PALO checksum incorrect.\n");
459 		return;
460 	}
461 
462 	setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO);
463 }
464 
465 void
466 efi_map_pal_code (void)
467 {
468 	void *pal_vaddr = efi_get_pal_addr ();
469 	u64 psr;
470 
471 	if (!pal_vaddr)
472 		return;
473 
474 	/*
475 	 * Cannot write to CRx with PSR.ic=1
476 	 */
477 	psr = ia64_clear_ic();
478 	ia64_itr(0x1, IA64_TR_PALCODE,
479 		 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
480 		 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
481 		 IA64_GRANULE_SHIFT);
482 	ia64_set_psr(psr);		/* restore psr */
483 }
484 
485 void __init
486 efi_init (void)
487 {
488 	const efi_system_table_t *efi_systab;
489 	void *efi_map_start, *efi_map_end;
490 	u64 efi_desc_size;
491 	char *cp;
492 
493 	set_bit(EFI_BOOT, &efi.flags);
494 	set_bit(EFI_64BIT, &efi.flags);
495 
496 	/*
497 	 * It's too early to be able to use the standard kernel command line
498 	 * support...
499 	 */
500 	for (cp = boot_command_line; *cp; ) {
501 		if (memcmp(cp, "mem=", 4) == 0) {
502 			mem_limit = memparse(cp + 4, &cp);
503 		} else if (memcmp(cp, "max_addr=", 9) == 0) {
504 			max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
505 		} else if (memcmp(cp, "min_addr=", 9) == 0) {
506 			min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
507 		} else {
508 			while (*cp != ' ' && *cp)
509 				++cp;
510 			while (*cp == ' ')
511 				++cp;
512 		}
513 	}
514 	if (min_addr != 0UL)
515 		printk(KERN_INFO "Ignoring memory below %lluMB\n",
516 		       min_addr >> 20);
517 	if (max_addr != ~0UL)
518 		printk(KERN_INFO "Ignoring memory above %lluMB\n",
519 		       max_addr >> 20);
520 
521 	efi_systab = __va(ia64_boot_param->efi_systab);
522 
523 	/*
524 	 * Verify the EFI Table
525 	 */
526 	if (efi_systab == NULL)
527 		panic("Whoa! Can't find EFI system table.\n");
528 	if (efi_systab_check_header(&efi_systab->hdr, 1))
529 		panic("Whoa! EFI system table signature incorrect\n");
530 
531 	efi_systab_report_header(&efi_systab->hdr, efi_systab->fw_vendor);
532 
533 	palo_phys      = EFI_INVALID_TABLE_ADDR;
534 
535 	if (efi_config_parse_tables(__va(efi_systab->tables),
536 				    efi_systab->nr_tables,
537 				    arch_tables) != 0)
538 		return;
539 
540 	if (palo_phys != EFI_INVALID_TABLE_ADDR)
541 		handle_palo(palo_phys);
542 
543 	runtime = __va(efi_systab->runtime);
544 	efi.get_time = phys_get_time;
545 	efi.set_time = phys_set_time;
546 	efi.get_wakeup_time = phys_get_wakeup_time;
547 	efi.set_wakeup_time = phys_set_wakeup_time;
548 	efi.get_variable = phys_get_variable;
549 	efi.get_next_variable = phys_get_next_variable;
550 	efi.set_variable = phys_set_variable;
551 	efi.get_next_high_mono_count = phys_get_next_high_mono_count;
552 	efi.reset_system = phys_reset_system;
553 
554 	efi_map_start = __va(ia64_boot_param->efi_memmap);
555 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
556 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
557 
558 #if EFI_DEBUG
559 	/* print EFI memory map: */
560 	{
561 		efi_memory_desc_t *md;
562 		void *p;
563 
564 		for (i = 0, p = efi_map_start; p < efi_map_end;
565 		     ++i, p += efi_desc_size)
566 		{
567 			const char *unit;
568 			unsigned long size;
569 			char buf[64];
570 
571 			md = p;
572 			size = md->num_pages << EFI_PAGE_SHIFT;
573 
574 			if ((size >> 40) > 0) {
575 				size >>= 40;
576 				unit = "TB";
577 			} else if ((size >> 30) > 0) {
578 				size >>= 30;
579 				unit = "GB";
580 			} else if ((size >> 20) > 0) {
581 				size >>= 20;
582 				unit = "MB";
583 			} else {
584 				size >>= 10;
585 				unit = "KB";
586 			}
587 
588 			printk("mem%02d: %s "
589 			       "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
590 			       i, efi_md_typeattr_format(buf, sizeof(buf), md),
591 			       md->phys_addr,
592 			       md->phys_addr + efi_md_size(md), size, unit);
593 		}
594 	}
595 #endif
596 
597 	efi_map_pal_code();
598 	efi_enter_virtual_mode();
599 }
600 
601 void
602 efi_enter_virtual_mode (void)
603 {
604 	void *efi_map_start, *efi_map_end, *p;
605 	efi_memory_desc_t *md;
606 	efi_status_t status;
607 	u64 efi_desc_size;
608 
609 	efi_map_start = __va(ia64_boot_param->efi_memmap);
610 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
611 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
612 
613 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
614 		md = p;
615 		if (md->attribute & EFI_MEMORY_RUNTIME) {
616 			/*
617 			 * Some descriptors have multiple bits set, so the
618 			 * order of the tests is relevant.
619 			 */
620 			if (md->attribute & EFI_MEMORY_WB) {
621 				md->virt_addr = (u64) __va(md->phys_addr);
622 			} else if (md->attribute & EFI_MEMORY_UC) {
623 				md->virt_addr = (u64) ioremap(md->phys_addr, 0);
624 			} else if (md->attribute & EFI_MEMORY_WC) {
625 #if 0
626 				md->virt_addr = ia64_remap(md->phys_addr,
627 							   (_PAGE_A |
628 							    _PAGE_P |
629 							    _PAGE_D |
630 							    _PAGE_MA_WC |
631 							    _PAGE_PL_0 |
632 							    _PAGE_AR_RW));
633 #else
634 				printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
635 				md->virt_addr = (u64) ioremap(md->phys_addr, 0);
636 #endif
637 			} else if (md->attribute & EFI_MEMORY_WT) {
638 #if 0
639 				md->virt_addr = ia64_remap(md->phys_addr,
640 							   (_PAGE_A |
641 							    _PAGE_P |
642 							    _PAGE_D |
643 							    _PAGE_MA_WT |
644 							    _PAGE_PL_0 |
645 							    _PAGE_AR_RW));
646 #else
647 				printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
648 				md->virt_addr = (u64) ioremap(md->phys_addr, 0);
649 #endif
650 			}
651 		}
652 	}
653 
654 	status = efi_call_phys(__va(runtime->set_virtual_address_map),
655 			       ia64_boot_param->efi_memmap_size,
656 			       efi_desc_size,
657 			       ia64_boot_param->efi_memdesc_version,
658 			       ia64_boot_param->efi_memmap);
659 	if (status != EFI_SUCCESS) {
660 		printk(KERN_WARNING "warning: unable to switch EFI into "
661 		       "virtual mode (status=%lu)\n", status);
662 		return;
663 	}
664 
665 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
666 
667 	/*
668 	 * Now that EFI is in virtual mode, we call the EFI functions more
669 	 * efficiently:
670 	 */
671 	efi.get_time = virt_get_time;
672 	efi.set_time = virt_set_time;
673 	efi.get_wakeup_time = virt_get_wakeup_time;
674 	efi.set_wakeup_time = virt_set_wakeup_time;
675 	efi.get_variable = virt_get_variable;
676 	efi.get_next_variable = virt_get_next_variable;
677 	efi.set_variable = virt_set_variable;
678 	efi.get_next_high_mono_count = virt_get_next_high_mono_count;
679 	efi.reset_system = virt_reset_system;
680 }
681 
682 /*
683  * Walk the EFI memory map looking for the I/O port range.  There can only be
684  * one entry of this type, other I/O port ranges should be described via ACPI.
685  */
686 u64
687 efi_get_iobase (void)
688 {
689 	void *efi_map_start, *efi_map_end, *p;
690 	efi_memory_desc_t *md;
691 	u64 efi_desc_size;
692 
693 	efi_map_start = __va(ia64_boot_param->efi_memmap);
694 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
695 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
696 
697 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
698 		md = p;
699 		if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
700 			if (md->attribute & EFI_MEMORY_UC)
701 				return md->phys_addr;
702 		}
703 	}
704 	return 0;
705 }
706 
707 static struct kern_memdesc *
708 kern_memory_descriptor (unsigned long phys_addr)
709 {
710 	struct kern_memdesc *md;
711 
712 	for (md = kern_memmap; md->start != ~0UL; md++) {
713 		if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
714 			 return md;
715 	}
716 	return NULL;
717 }
718 
719 static efi_memory_desc_t *
720 efi_memory_descriptor (unsigned long phys_addr)
721 {
722 	void *efi_map_start, *efi_map_end, *p;
723 	efi_memory_desc_t *md;
724 	u64 efi_desc_size;
725 
726 	efi_map_start = __va(ia64_boot_param->efi_memmap);
727 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
728 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
729 
730 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
731 		md = p;
732 
733 		if (phys_addr - md->phys_addr < efi_md_size(md))
734 			 return md;
735 	}
736 	return NULL;
737 }
738 
739 static int
740 efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
741 {
742 	void *efi_map_start, *efi_map_end, *p;
743 	efi_memory_desc_t *md;
744 	u64 efi_desc_size;
745 	unsigned long end;
746 
747 	efi_map_start = __va(ia64_boot_param->efi_memmap);
748 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
749 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
750 
751 	end = phys_addr + size;
752 
753 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
754 		md = p;
755 		if (md->phys_addr < end && efi_md_end(md) > phys_addr)
756 			return 1;
757 	}
758 	return 0;
759 }
760 
761 int
762 efi_mem_type (unsigned long phys_addr)
763 {
764 	efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
765 
766 	if (md)
767 		return md->type;
768 	return -EINVAL;
769 }
770 
771 u64
772 efi_mem_attributes (unsigned long phys_addr)
773 {
774 	efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
775 
776 	if (md)
777 		return md->attribute;
778 	return 0;
779 }
780 EXPORT_SYMBOL(efi_mem_attributes);
781 
782 u64
783 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
784 {
785 	unsigned long end = phys_addr + size;
786 	efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
787 	u64 attr;
788 
789 	if (!md)
790 		return 0;
791 
792 	/*
793 	 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
794 	 * the kernel that firmware needs this region mapped.
795 	 */
796 	attr = md->attribute & ~EFI_MEMORY_RUNTIME;
797 	do {
798 		unsigned long md_end = efi_md_end(md);
799 
800 		if (end <= md_end)
801 			return attr;
802 
803 		md = efi_memory_descriptor(md_end);
804 		if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
805 			return 0;
806 	} while (md);
807 	return 0;	/* never reached */
808 }
809 
810 u64
811 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
812 {
813 	unsigned long end = phys_addr + size;
814 	struct kern_memdesc *md;
815 	u64 attr;
816 
817 	/*
818 	 * This is a hack for ioremap calls before we set up kern_memmap.
819 	 * Maybe we should do efi_memmap_init() earlier instead.
820 	 */
821 	if (!kern_memmap) {
822 		attr = efi_mem_attribute(phys_addr, size);
823 		if (attr & EFI_MEMORY_WB)
824 			return EFI_MEMORY_WB;
825 		return 0;
826 	}
827 
828 	md = kern_memory_descriptor(phys_addr);
829 	if (!md)
830 		return 0;
831 
832 	attr = md->attribute;
833 	do {
834 		unsigned long md_end = kmd_end(md);
835 
836 		if (end <= md_end)
837 			return attr;
838 
839 		md = kern_memory_descriptor(md_end);
840 		if (!md || md->attribute != attr)
841 			return 0;
842 	} while (md);
843 	return 0;	/* never reached */
844 }
845 
846 int
847 valid_phys_addr_range (phys_addr_t phys_addr, unsigned long size)
848 {
849 	u64 attr;
850 
851 	/*
852 	 * /dev/mem reads and writes use copy_to_user(), which implicitly
853 	 * uses a granule-sized kernel identity mapping.  It's really
854 	 * only safe to do this for regions in kern_memmap.  For more
855 	 * details, see Documentation/ia64/aliasing.rst.
856 	 */
857 	attr = kern_mem_attribute(phys_addr, size);
858 	if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
859 		return 1;
860 	return 0;
861 }
862 
863 int
864 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
865 {
866 	unsigned long phys_addr = pfn << PAGE_SHIFT;
867 	u64 attr;
868 
869 	attr = efi_mem_attribute(phys_addr, size);
870 
871 	/*
872 	 * /dev/mem mmap uses normal user pages, so we don't need the entire
873 	 * granule, but the entire region we're mapping must support the same
874 	 * attribute.
875 	 */
876 	if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
877 		return 1;
878 
879 	/*
880 	 * Intel firmware doesn't tell us about all the MMIO regions, so
881 	 * in general we have to allow mmap requests.  But if EFI *does*
882 	 * tell us about anything inside this region, we should deny it.
883 	 * The user can always map a smaller region to avoid the overlap.
884 	 */
885 	if (efi_memmap_intersects(phys_addr, size))
886 		return 0;
887 
888 	return 1;
889 }
890 
891 pgprot_t
892 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
893 		     pgprot_t vma_prot)
894 {
895 	unsigned long phys_addr = pfn << PAGE_SHIFT;
896 	u64 attr;
897 
898 	/*
899 	 * For /dev/mem mmap, we use user mappings, but if the region is
900 	 * in kern_memmap (and hence may be covered by a kernel mapping),
901 	 * we must use the same attribute as the kernel mapping.
902 	 */
903 	attr = kern_mem_attribute(phys_addr, size);
904 	if (attr & EFI_MEMORY_WB)
905 		return pgprot_cacheable(vma_prot);
906 	else if (attr & EFI_MEMORY_UC)
907 		return pgprot_noncached(vma_prot);
908 
909 	/*
910 	 * Some chipsets don't support UC access to memory.  If
911 	 * WB is supported, we prefer that.
912 	 */
913 	if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
914 		return pgprot_cacheable(vma_prot);
915 
916 	return pgprot_noncached(vma_prot);
917 }
918 
919 int __init
920 efi_uart_console_only(void)
921 {
922 	efi_status_t status;
923 	char *s, name[] = "ConOut";
924 	efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
925 	efi_char16_t *utf16, name_utf16[32];
926 	unsigned char data[1024];
927 	unsigned long size = sizeof(data);
928 	struct efi_generic_dev_path *hdr, *end_addr;
929 	int uart = 0;
930 
931 	/* Convert to UTF-16 */
932 	utf16 = name_utf16;
933 	s = name;
934 	while (*s)
935 		*utf16++ = *s++ & 0x7f;
936 	*utf16 = 0;
937 
938 	status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
939 	if (status != EFI_SUCCESS) {
940 		printk(KERN_ERR "No EFI %s variable?\n", name);
941 		return 0;
942 	}
943 
944 	hdr = (struct efi_generic_dev_path *) data;
945 	end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
946 	while (hdr < end_addr) {
947 		if (hdr->type == EFI_DEV_MSG &&
948 		    hdr->sub_type == EFI_DEV_MSG_UART)
949 			uart = 1;
950 		else if (hdr->type == EFI_DEV_END_PATH ||
951 			  hdr->type == EFI_DEV_END_PATH2) {
952 			if (!uart)
953 				return 0;
954 			if (hdr->sub_type == EFI_DEV_END_ENTIRE)
955 				return 1;
956 			uart = 0;
957 		}
958 		hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
959 	}
960 	printk(KERN_ERR "Malformed %s value\n", name);
961 	return 0;
962 }
963 
964 /*
965  * Look for the first granule aligned memory descriptor memory
966  * that is big enough to hold EFI memory map. Make sure this
967  * descriptor is at least granule sized so it does not get trimmed
968  */
969 struct kern_memdesc *
970 find_memmap_space (void)
971 {
972 	u64	contig_low=0, contig_high=0;
973 	u64	as = 0, ae;
974 	void *efi_map_start, *efi_map_end, *p, *q;
975 	efi_memory_desc_t *md, *pmd = NULL, *check_md;
976 	u64	space_needed, efi_desc_size;
977 	unsigned long total_mem = 0;
978 
979 	efi_map_start = __va(ia64_boot_param->efi_memmap);
980 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
981 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
982 
983 	/*
984 	 * Worst case: we need 3 kernel descriptors for each efi descriptor
985 	 * (if every entry has a WB part in the middle, and UC head and tail),
986 	 * plus one for the end marker.
987 	 */
988 	space_needed = sizeof(kern_memdesc_t) *
989 		(3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
990 
991 	for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
992 		md = p;
993 		if (!efi_wb(md)) {
994 			continue;
995 		}
996 		if (pmd == NULL || !efi_wb(pmd) ||
997 		    efi_md_end(pmd) != md->phys_addr) {
998 			contig_low = GRANULEROUNDUP(md->phys_addr);
999 			contig_high = efi_md_end(md);
1000 			for (q = p + efi_desc_size; q < efi_map_end;
1001 			     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);
1008 			}
1009 			contig_high = GRANULEROUNDDOWN(contig_high);
1010 		}
1011 		if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
1012 			continue;
1013 
1014 		/* Round ends inward to granule boundaries */
1015 		as = max(contig_low, md->phys_addr);
1016 		ae = min(contig_high, efi_md_end(md));
1017 
1018 		/* keep within max_addr= and min_addr= command line arg */
1019 		as = max(as, min_addr);
1020 		ae = min(ae, max_addr);
1021 		if (ae <= as)
1022 			continue;
1023 
1024 		/* avoid going over mem= command line arg */
1025 		if (total_mem + (ae - as) > mem_limit)
1026 			ae -= total_mem + (ae - as) - mem_limit;
1027 
1028 		if (ae <= as)
1029 			continue;
1030 
1031 		if (ae - as > space_needed)
1032 			break;
1033 	}
1034 	if (p >= efi_map_end)
1035 		panic("Can't allocate space for kernel memory descriptors");
1036 
1037 	return __va(as);
1038 }
1039 
1040 /*
1041  * Walk the EFI memory map and gather all memory available for kernel
1042  * to use.  We can allocate partial granules only if the unavailable
1043  * parts exist, and are WB.
1044  */
1045 unsigned long
1046 efi_memmap_init(u64 *s, u64 *e)
1047 {
1048 	struct kern_memdesc *k, *prev = NULL;
1049 	u64	contig_low=0, contig_high=0;
1050 	u64	as, ae, lim;
1051 	void *efi_map_start, *efi_map_end, *p, *q;
1052 	efi_memory_desc_t *md, *pmd = NULL, *check_md;
1053 	u64	efi_desc_size;
1054 	unsigned long total_mem = 0;
1055 
1056 	k = kern_memmap = find_memmap_space();
1057 
1058 	efi_map_start = __va(ia64_boot_param->efi_memmap);
1059 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1060 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
1061 
1062 	for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1063 		md = p;
1064 		if (!efi_wb(md)) {
1065 			if (efi_uc(md) &&
1066 			    (md->type == EFI_CONVENTIONAL_MEMORY ||
1067 			     md->type == EFI_BOOT_SERVICES_DATA)) {
1068 				k->attribute = EFI_MEMORY_UC;
1069 				k->start = md->phys_addr;
1070 				k->num_pages = md->num_pages;
1071 				k++;
1072 			}
1073 			continue;
1074 		}
1075 		if (pmd == NULL || !efi_wb(pmd) ||
1076 		    efi_md_end(pmd) != md->phys_addr) {
1077 			contig_low = GRANULEROUNDUP(md->phys_addr);
1078 			contig_high = efi_md_end(md);
1079 			for (q = p + efi_desc_size; q < efi_map_end;
1080 			     q += efi_desc_size) {
1081 				check_md = q;
1082 				if (!efi_wb(check_md))
1083 					break;
1084 				if (contig_high != check_md->phys_addr)
1085 					break;
1086 				contig_high = efi_md_end(check_md);
1087 			}
1088 			contig_high = GRANULEROUNDDOWN(contig_high);
1089 		}
1090 		if (!is_memory_available(md))
1091 			continue;
1092 
1093 		/*
1094 		 * Round ends inward to granule boundaries
1095 		 * Give trimmings to uncached allocator
1096 		 */
1097 		if (md->phys_addr < contig_low) {
1098 			lim = min(efi_md_end(md), contig_low);
1099 			if (efi_uc(md)) {
1100 				if (k > kern_memmap &&
1101 				    (k-1)->attribute == EFI_MEMORY_UC &&
1102 				    kmd_end(k-1) == md->phys_addr) {
1103 					(k-1)->num_pages +=
1104 						(lim - md->phys_addr)
1105 						>> EFI_PAGE_SHIFT;
1106 				} else {
1107 					k->attribute = EFI_MEMORY_UC;
1108 					k->start = md->phys_addr;
1109 					k->num_pages = (lim - md->phys_addr)
1110 						>> EFI_PAGE_SHIFT;
1111 					k++;
1112 				}
1113 			}
1114 			as = contig_low;
1115 		} else
1116 			as = md->phys_addr;
1117 
1118 		if (efi_md_end(md) > contig_high) {
1119 			lim = max(md->phys_addr, contig_high);
1120 			if (efi_uc(md)) {
1121 				if (lim == md->phys_addr && k > kern_memmap &&
1122 				    (k-1)->attribute == EFI_MEMORY_UC &&
1123 				    kmd_end(k-1) == md->phys_addr) {
1124 					(k-1)->num_pages += md->num_pages;
1125 				} else {
1126 					k->attribute = EFI_MEMORY_UC;
1127 					k->start = lim;
1128 					k->num_pages = (efi_md_end(md) - lim)
1129 						>> EFI_PAGE_SHIFT;
1130 					k++;
1131 				}
1132 			}
1133 			ae = contig_high;
1134 		} else
1135 			ae = efi_md_end(md);
1136 
1137 		/* keep within max_addr= and min_addr= command line arg */
1138 		as = max(as, min_addr);
1139 		ae = min(ae, max_addr);
1140 		if (ae <= as)
1141 			continue;
1142 
1143 		/* avoid going over mem= command line arg */
1144 		if (total_mem + (ae - as) > mem_limit)
1145 			ae -= total_mem + (ae - as) - mem_limit;
1146 
1147 		if (ae <= as)
1148 			continue;
1149 		if (prev && kmd_end(prev) == md->phys_addr) {
1150 			prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1151 			total_mem += ae - as;
1152 			continue;
1153 		}
1154 		k->attribute = EFI_MEMORY_WB;
1155 		k->start = as;
1156 		k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1157 		total_mem += ae - as;
1158 		prev = k++;
1159 	}
1160 	k->start = ~0L; /* end-marker */
1161 
1162 	/* reserve the memory we are using for kern_memmap */
1163 	*s = (u64)kern_memmap;
1164 	*e = (u64)++k;
1165 
1166 	return total_mem;
1167 }
1168 
1169 void
1170 efi_initialize_iomem_resources(struct resource *code_resource,
1171 			       struct resource *data_resource,
1172 			       struct resource *bss_resource)
1173 {
1174 	struct resource *res;
1175 	void *efi_map_start, *efi_map_end, *p;
1176 	efi_memory_desc_t *md;
1177 	u64 efi_desc_size;
1178 	char *name;
1179 	unsigned long flags, desc;
1180 
1181 	efi_map_start = __va(ia64_boot_param->efi_memmap);
1182 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1183 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
1184 
1185 	res = NULL;
1186 
1187 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1188 		md = p;
1189 
1190 		if (md->num_pages == 0) /* should not happen */
1191 			continue;
1192 
1193 		flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1194 		desc = IORES_DESC_NONE;
1195 
1196 		switch (md->type) {
1197 
1198 			case EFI_MEMORY_MAPPED_IO:
1199 			case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1200 				continue;
1201 
1202 			case EFI_LOADER_CODE:
1203 			case EFI_LOADER_DATA:
1204 			case EFI_BOOT_SERVICES_DATA:
1205 			case EFI_BOOT_SERVICES_CODE:
1206 			case EFI_CONVENTIONAL_MEMORY:
1207 				if (md->attribute & EFI_MEMORY_WP) {
1208 					name = "System ROM";
1209 					flags |= IORESOURCE_READONLY;
1210 				} else if (md->attribute == EFI_MEMORY_UC) {
1211 					name = "Uncached RAM";
1212 				} else {
1213 					name = "System RAM";
1214 					flags |= IORESOURCE_SYSRAM;
1215 				}
1216 				break;
1217 
1218 			case EFI_ACPI_MEMORY_NVS:
1219 				name = "ACPI Non-volatile Storage";
1220 				desc = IORES_DESC_ACPI_NV_STORAGE;
1221 				break;
1222 
1223 			case EFI_UNUSABLE_MEMORY:
1224 				name = "reserved";
1225 				flags |= IORESOURCE_DISABLED;
1226 				break;
1227 
1228 			case EFI_PERSISTENT_MEMORY:
1229 				name = "Persistent Memory";
1230 				desc = IORES_DESC_PERSISTENT_MEMORY;
1231 				break;
1232 
1233 			case EFI_RESERVED_TYPE:
1234 			case EFI_RUNTIME_SERVICES_CODE:
1235 			case EFI_RUNTIME_SERVICES_DATA:
1236 			case EFI_ACPI_RECLAIM_MEMORY:
1237 			default:
1238 				name = "reserved";
1239 				break;
1240 		}
1241 
1242 		if ((res = kzalloc(sizeof(struct resource),
1243 				   GFP_KERNEL)) == NULL) {
1244 			printk(KERN_ERR
1245 			       "failed to allocate resource for iomem\n");
1246 			return;
1247 		}
1248 
1249 		res->name = name;
1250 		res->start = md->phys_addr;
1251 		res->end = md->phys_addr + efi_md_size(md) - 1;
1252 		res->flags = flags;
1253 		res->desc = desc;
1254 
1255 		if (insert_resource(&iomem_resource, res) < 0)
1256 			kfree(res);
1257 		else {
1258 			/*
1259 			 * We don't know which region contains
1260 			 * kernel data so we try it repeatedly and
1261 			 * let the resource manager test it.
1262 			 */
1263 			insert_resource(res, code_resource);
1264 			insert_resource(res, data_resource);
1265 			insert_resource(res, bss_resource);
1266 #ifdef CONFIG_KEXEC
1267                         insert_resource(res, &efi_memmap_res);
1268                         insert_resource(res, &boot_param_res);
1269 			if (crashk_res.end > crashk_res.start)
1270 				insert_resource(res, &crashk_res);
1271 #endif
1272 		}
1273 	}
1274 }
1275 
1276 #ifdef CONFIG_KEXEC
1277 /* find a block of memory aligned to 64M exclude reserved regions
1278    rsvd_regions are sorted
1279  */
1280 unsigned long __init
1281 kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
1282 {
1283 	int i;
1284 	u64 start, end;
1285 	u64 alignment = 1UL << _PAGE_SIZE_64M;
1286 	void *efi_map_start, *efi_map_end, *p;
1287 	efi_memory_desc_t *md;
1288 	u64 efi_desc_size;
1289 
1290 	efi_map_start = __va(ia64_boot_param->efi_memmap);
1291 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1292 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
1293 
1294 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1295 		md = p;
1296 		if (!efi_wb(md))
1297 			continue;
1298 		start = ALIGN(md->phys_addr, alignment);
1299 		end = efi_md_end(md);
1300 		for (i = 0; i < n; i++) {
1301 			if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1302 				if (__pa(r[i].start) > start + size)
1303 					return start;
1304 				start = ALIGN(__pa(r[i].end), alignment);
1305 				if (i < n-1 &&
1306 				    __pa(r[i+1].start) < start + size)
1307 					continue;
1308 				else
1309 					break;
1310 			}
1311 		}
1312 		if (end > start + size)
1313 			return start;
1314 	}
1315 
1316 	printk(KERN_WARNING
1317 	       "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1318 	return ~0UL;
1319 }
1320 #endif
1321 
1322 #ifdef CONFIG_CRASH_DUMP
1323 /* locate the size find a the descriptor at a certain address */
1324 unsigned long __init
1325 vmcore_find_descriptor_size (unsigned long address)
1326 {
1327 	void *efi_map_start, *efi_map_end, *p;
1328 	efi_memory_desc_t *md;
1329 	u64 efi_desc_size;
1330 	unsigned long ret = 0;
1331 
1332 	efi_map_start = __va(ia64_boot_param->efi_memmap);
1333 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1334 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
1335 
1336 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1337 		md = p;
1338 		if (efi_wb(md) && md->type == EFI_LOADER_DATA
1339 		    && md->phys_addr == address) {
1340 			ret = efi_md_size(md);
1341 			break;
1342 		}
1343 	}
1344 
1345 	if (ret == 0)
1346 		printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1347 
1348 	return ret;
1349 }
1350 #endif
1351 
1352 char *efi_systab_show_arch(char *str)
1353 {
1354 	if (mps_phys != EFI_INVALID_TABLE_ADDR)
1355 		str += sprintf(str, "MPS=0x%lx\n", mps_phys);
1356 	if (hcdp_phys != EFI_INVALID_TABLE_ADDR)
1357 		str += sprintf(str, "HCDP=0x%lx\n", hcdp_phys);
1358 	return str;
1359 }
1360