Linux Audio

Check our new training course

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