aboutsummaryrefslogtreecommitdiffstats
path: root/manual/CHAPTER_Eval/grep-it.sh
diff options
context:
space:
mode:
authorClifford Wolf <clifford@clifford.at>2014-07-27 02:00:04 +0200
committerClifford Wolf <clifford@clifford.at>2014-07-27 02:00:04 +0200
commitc91570bde32d59679ea8b72ed041ef8f6bb0d51a (patch)
treeb9a8e932654aab29e5a0ced451b3e7566094b242 /manual/CHAPTER_Eval/grep-it.sh
parent4c4b6021562c598c4510831bd547edaa97d14dac (diff)
downloadyosys-c91570bde32d59679ea8b72ed041ef8f6bb0d51a.tar.gz
yosys-c91570bde32d59679ea8b72ed041ef8f6bb0d51a.tar.bz2
yosys-c91570bde32d59679ea8b72ed041ef8f6bb0d51a.zip
Mostly cosmetic changes to rtlil.h
Diffstat (limited to 'manual/CHAPTER_Eval/grep-it.sh')
0 files changed, 0 insertions, 0 deletions
>99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
/*
 *  Xen domain firmware emulation support
 *  Copyright (C) 2004 Hewlett-Packard Co.
 *       Dan Magenheimer (dan.magenheimer@hp.com)
 *
 * Copyright (c) 2006 Isaku Yamahata <yamahata at valinux co jp>
 *                    VA Linux Systems Japan K.K.
 *                    dom0 vp model support
 */

#include <xen/config.h>
#include <asm/system.h>
#include <asm/pgalloc.h>

#include <linux/efi.h>
#include <linux/sort.h>
#include <asm/io.h>
#include <asm/pal.h>
#include <asm/sal.h>
#include <asm/meminit.h>
#include <asm/fpswa.h>
#include <xen/compile.h>
#include <xen/acpi.h>

#include <asm/dom_fw.h>
#include <asm/bundle.h>

static void dom_fw_init (struct domain *d, struct ia64_boot_param *bp, char *fw_mem, int fw_mem_size, unsigned long maxmem);

extern struct domain *dom0;
extern unsigned long dom0_start;

extern unsigned long running_on_sim;

/* Note: two domains cannot be created simulteanously!  */
static unsigned long dom_fw_base_mpa = -1;
static unsigned long imva_fw_base = -1;

#define FW_VENDOR "X\0e\0n\0/\0i\0a\0\066\0\064\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

#define MAKE_MD(typ, attr, start, end, abs) 				\
	do {								\
		md = efi_memmap + i++;					\
		md->type = typ;						\
		md->pad = 0;						\
		md->phys_addr = abs ? start : start_mpaddr + start;	\
		md->virt_addr = 0;					\
		md->num_pages = (end - start) >> EFI_PAGE_SHIFT;	\
		md->attribute = attr;					\
	} while (0)

#define EFI_HYPERCALL_PATCH(tgt, call)					\
	do {								\
		dom_efi_hypercall_patch(d, FW_HYPERCALL_##call##_PADDR,	\
		                        FW_HYPERCALL_##call);		\
		tgt = dom_pa((unsigned long) pfn);			\
		*pfn++ = FW_HYPERCALL_##call##_PADDR + start_mpaddr;	\
		*pfn++ = 0;						\
	} while (0)

// return domain (meta)physical address for a given imva
// this function is a call-back from dom_fw_init
static unsigned long
dom_pa(unsigned long imva)
{
	if (dom_fw_base_mpa == -1 || imva_fw_base == -1) {
		printf("dom_pa: uninitialized! (spinning...)\n");
		while(1);
	}
	if (imva - imva_fw_base > PAGE_SIZE) {
		printf("dom_pa: bad offset! imva=0x%lx, imva_fw_base=0x%lx (spinning...)\n",
			imva, imva_fw_base);
		while(1);
	}
	return dom_fw_base_mpa + (imva - imva_fw_base);
}

// allocate a page for fw
// build_physmap_table() which is called by new_thread()
// does for domU.
#define ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, mpaddr)   \
    do {                                            \
        if ((d) == dom0) {                          \
            assign_new_domain0_page((d), (mpaddr)); \
        }                                           \
    } while (0)

/**************************************************************************
Hypercall bundle creation
**************************************************************************/

static void build_hypercall_bundle(UINT64 *imva, UINT64 brkimm, UINT64 hypnum, UINT64 ret)
{
	INST64_A5 slot0;
	INST64_I19 slot1;
	INST64_B4 slot2;
	IA64_BUNDLE bundle;

	// slot1: mov r2 = hypnum (low 20 bits)
	slot0.inst = 0;
	slot0.qp = 0; slot0.r1 = 2; slot0.r3 = 0; slot0.major = 0x9;
	slot0.imm7b = hypnum; slot0.imm9d = hypnum >> 7;
	slot0.imm5c = hypnum >> 16; slot0.s = 0;
	// slot1: break brkimm
	slot1.inst = 0;
	slot1.qp = 0; slot1.x6 = 0; slot1.x3 = 0; slot1.major = 0x0;
	slot1.imm20 = brkimm; slot1.i = brkimm >> 20;
	// if ret slot2: br.ret.sptk.many rp
	// else slot2: br.cond.sptk.many rp
	slot2.inst = 0; slot2.qp = 0; slot2.p = 1; slot2.b2 = 0;
	slot2.wh = 0; slot2.d = 0; slot2.major = 0x0;
	if (ret) {
		slot2.btype = 4; slot2.x6 = 0x21;
	}
	else {
		slot2.btype = 0; slot2.x6 = 0x20;
	}
	
	bundle.i64[0] = 0; bundle.i64[1] = 0;
	bundle.template = 0x11;
	bundle.slot0 = slot0.inst; bundle.slot2 = slot2.inst;
	bundle.slot1a = slot1.inst; bundle.slot1b = slot1.inst >> 18;
	
	imva[0] = bundle.i64[0]; imva[1] = bundle.i64[1];
	ia64_fc(imva);
	ia64_fc(imva + 1);
}

static void build_pal_hypercall_bundles(UINT64 *imva, UINT64 brkimm, UINT64 hypnum)
{
	extern unsigned long pal_call_stub[];
	IA64_BUNDLE bundle;
	INST64_A5 slot_a5;
	INST64_M37 slot_m37;

	/* The source of the hypercall stub is the pal_call_stub function
	   defined in xenasm.S.  */

	/* Copy the first bundle and patch the hypercall number.  */
	bundle.i64[0] = pal_call_stub[0];
	bundle.i64[1] = pal_call_stub[1];
	slot_a5.inst = bundle.slot0;
	slot_a5.imm7b = hypnum;
	slot_a5.imm9d = hypnum >> 7;
	slot_a5.imm5c = hypnum >> 16;
	bundle.slot0 = slot_a5.inst;
	imva[0] = bundle.i64[0];
	imva[1] = bundle.i64[1];
	ia64_fc(imva);
	ia64_fc(imva + 1);
	
	/* Copy the second bundle and patch the hypercall vector.  */
	bundle.i64[0] = pal_call_stub[2];
	bundle.i64[1] = pal_call_stub[3];
	slot_m37.inst = bundle.slot0;
	slot_m37.imm20a = brkimm;
	slot_m37.i = brkimm >> 20;
	bundle.slot0 = slot_m37.inst;
	imva[2] = bundle.i64[0];
	imva[3] = bundle.i64[1];
	ia64_fc(imva + 2);
	ia64_fc(imva + 3);
}

// builds a hypercall bundle at domain physical address
static void dom_fpswa_hypercall_patch(struct domain *d)
{
	unsigned long *entry_imva, *patch_imva;
	unsigned long entry_paddr = FW_HYPERCALL_FPSWA_ENTRY_PADDR;
	unsigned long patch_paddr = FW_HYPERCALL_FPSWA_PATCH_PADDR;

#ifndef CONFIG_XEN_IA64_DOM0_VP
	if (d == dom0) {
		entry_paddr += dom0_start;
		patch_paddr += dom0_start;
	}
#endif
	ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, entry_paddr);
	ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, patch_paddr);
	entry_imva = domain_mpa_to_imva(d, entry_paddr);
	patch_imva = domain_mpa_to_imva(d, patch_paddr);

	*entry_imva++ = patch_paddr;
	*entry_imva   = 0;
	build_hypercall_bundle(patch_imva, d->arch.breakimm, FW_HYPERCALL_FPSWA, 1);
}

// builds a hypercall bundle at domain physical address
static void dom_efi_hypercall_patch(struct domain *d, unsigned long paddr, unsigned long hypercall)
{
	unsigned long *imva;

#ifndef CONFIG_XEN_IA64_DOM0_VP
	if (d == dom0) paddr += dom0_start;
#endif
	ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, paddr);
	imva = domain_mpa_to_imva(d, paddr);
	build_hypercall_bundle(imva, d->arch.breakimm, hypercall, 1);
}

// builds a hypercall bundle at domain physical address
static void dom_fw_hypercall_patch(struct domain *d, unsigned long paddr, unsigned long hypercall,unsigned long ret)
{
	unsigned long *imva;

	ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, paddr);
	imva = domain_mpa_to_imva(d, paddr);
	build_hypercall_bundle(imva, d->arch.breakimm, hypercall, ret);
}

static void dom_fw_pal_hypercall_patch(struct domain *d, unsigned long paddr)
{
	unsigned long *imva;

	ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, paddr);
	imva = domain_mpa_to_imva(d, paddr);
	build_pal_hypercall_bundles(imva, d->arch.breakimm, FW_HYPERCALL_PAL_CALL);
}


void dom_fw_setup(struct domain *d, unsigned long bp_mpa, unsigned long maxmem)
{
	struct ia64_boot_param *bp;

	dom_fw_base_mpa = 0;
#ifndef CONFIG_XEN_IA64_DOM0_VP
	if (d == dom0) {
		dom_fw_base_mpa += dom0_start;
		bp_mpa += dom0_start;
	}
#endif
	ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, dom_fw_base_mpa);
	imva_fw_base = (unsigned long) domain_mpa_to_imva(d, dom_fw_base_mpa);
	ASSIGN_NEW_DOMAIN_PAGE_IF_DOM0(d, bp_mpa);
	bp = domain_mpa_to_imva(d, bp_mpa);
	dom_fw_init(d, bp, (char *) imva_fw_base, PAGE_SIZE, maxmem);
}


/* the following heavily leveraged from linux/arch/ia64/hp/sim/fw-emu.c */

#define NFUNCPTRS 20

static inline void
print_md(efi_memory_desc_t *md)
{
	printk("domain mem: type=%2u, attr=0x%016lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
		md->type, md->attribute, md->phys_addr,
		md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
		md->num_pages >> (20 - EFI_PAGE_SHIFT));
}

static u32 lsapic_nbr;

/* Modify lsapic table.  Provides LPs.  */
static int 
acpi_update_lsapic (acpi_table_entry_header *header, const unsigned long end)
{
	struct acpi_table_lsapic *lsapic;
	int enable;

	lsapic = (struct acpi_table_lsapic *) header;
	if (!lsapic)
		return -EINVAL;

	if (lsapic_nbr < MAX_VIRT_CPUS && dom0->vcpu[lsapic_nbr] != NULL)
		enable = 1;
	else
		enable = 0;
	if (lsapic->flags.enabled && enable) {
		printk("enable lsapic entry: 0x%lx\n", (u64)lsapic);
		lsapic_nbr++;
	} else if (lsapic->flags.enabled) {
		printk("DISABLE lsapic entry: 0x%lx\n", (u64)lsapic);
		lsapic->flags.enabled = 0;
	}
	return 0;
}

static u8
generate_acpi_checksum(void *tbl, unsigned long len)
{
	u8 *ptr, sum = 0;

	for (ptr = tbl; len > 0 ; len--, ptr++)
		sum += *ptr;

	return 0 - sum;
}

static int
acpi_update_madt_checksum (unsigned long phys_addr, unsigned long size)
{
	struct acpi_table_madt* acpi_madt;

	if (!phys_addr || !size)
		return -EINVAL;

	acpi_madt = (struct acpi_table_madt *) __va(phys_addr);
	acpi_madt->header.checksum = 0;
	acpi_madt->header.checksum = generate_acpi_checksum(acpi_madt, size);

	return 0;
}

/* base is physical address of acpi table */
static void touch_acpi_table(void)
{
	lsapic_nbr = 0;
	if (acpi_table_parse_madt(ACPI_MADT_LSAPIC, acpi_update_lsapic, 0) < 0)
		printk("Error parsing MADT - no LAPIC entires\n");
	acpi_table_parse(ACPI_APIC, acpi_update_madt_checksum);

	return;
}

struct fake_acpi_tables {
	struct acpi20_table_rsdp rsdp;
	struct xsdt_descriptor_rev2 xsdt;
	u64 madt_ptr;
	struct fadt_descriptor_rev2 fadt;
	struct facs_descriptor_rev2 facs;
	struct acpi_table_header dsdt;
	u8 aml[8 + 11 * MAX_VIRT_CPUS];
	struct acpi_table_madt madt;
	struct acpi_table_lsapic lsapic[MAX_VIRT_CPUS];
	u8 pm1a_evt_blk[4];
	u8 pm1a_cnt_blk[1];
	u8 pm_tmr_blk[4];
};

/* Create enough of an ACPI structure to make the guest OS ACPI happy. */
static void
dom_fw_fake_acpi(struct domain *d, struct fake_acpi_tables *tables)
{
	struct acpi20_table_rsdp *rsdp = &tables->rsdp;
	struct xsdt_descriptor_rev2 *xsdt = &tables->xsdt;
	struct fadt_descriptor_rev2 *fadt = &tables->fadt;
	struct facs_descriptor_rev2 *facs = &tables->facs;
	struct acpi_table_header *dsdt = &tables->dsdt;
	struct acpi_table_madt *madt = &tables->madt;
	struct acpi_table_lsapic *lsapic = tables->lsapic;
	int i;
	int aml_len;

	memset(tables, 0, sizeof(struct fake_acpi_tables));

	/* setup XSDT (64bit version of RSDT) */
	strncpy(xsdt->signature, XSDT_SIG, 4);
	/* XSDT points to both the FADT and the MADT, so add one entry */
	xsdt->length = sizeof(struct xsdt_descriptor_rev2) + sizeof(u64);
	xsdt->revision = 1;
	strcpy(xsdt->oem_id, "XEN");
	strcpy(xsdt->oem_table_id, "Xen/ia64");
	strcpy(xsdt->asl_compiler_id, "XEN");
	xsdt->asl_compiler_revision = (XEN_VERSION<<16)|(XEN_SUBVERSION);

	xsdt->table_offset_entry[0] = dom_pa((unsigned long) fadt);
	tables->madt_ptr = dom_pa((unsigned long) madt);

	xsdt->checksum = generate_acpi_checksum(xsdt, xsdt->length);

	/* setup FADT */
	strncpy(fadt->signature, FADT_SIG, 4);
	fadt->length = sizeof(struct fadt_descriptor_rev2);
	fadt->revision = FADT2_REVISION_ID;
	strcpy(fadt->oem_id, "XEN");
	strcpy(fadt->oem_table_id, "Xen/ia64");
	strcpy(fadt->asl_compiler_id, "XEN");
	fadt->asl_compiler_revision = (XEN_VERSION<<16)|(XEN_SUBVERSION);

	strncpy(facs->signature, FACS_SIG, 4);
	facs->version = 1;
	facs->length = sizeof(struct facs_descriptor_rev2);

	fadt->xfirmware_ctrl = dom_pa((unsigned long) facs);
	fadt->Xdsdt = dom_pa((unsigned long) dsdt);

	/*
	 * All of the below FADT entries are filled it to prevent warnings
	 * from sanity checks in the ACPI CA.  Emulate required ACPI hardware
	 * registers in system memory.
	 */
	fadt->pm1_evt_len = 4;
	fadt->xpm1a_evt_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
	fadt->xpm1a_evt_blk.register_bit_width = 8;
	fadt->xpm1a_evt_blk.address = dom_pa((unsigned long) &tables->pm1a_evt_blk);
	fadt->pm1_cnt_len = 1;
	fadt->xpm1a_cnt_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
	fadt->xpm1a_cnt_blk.register_bit_width = 8;
	fadt->xpm1a_cnt_blk.address = dom_pa((unsigned long) &tables->pm1a_cnt_blk);
	fadt->pm_tm_len = 4;
	fadt->xpm_tmr_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
	fadt->xpm_tmr_blk.register_bit_width = 8;
	fadt->xpm_tmr_blk.address = dom_pa((unsigned long) &tables->pm_tmr_blk);

	fadt->checksum = generate_acpi_checksum(fadt, fadt->length);

	/* setup RSDP */
	strncpy(rsdp->signature, RSDP_SIG, 8);
	strcpy(rsdp->oem_id, "XEN");
	rsdp->revision = 2; /* ACPI 2.0 includes XSDT */
	rsdp->length = sizeof(struct acpi20_table_rsdp);
	rsdp->xsdt_address = dom_pa((unsigned long) xsdt);

	rsdp->checksum = generate_acpi_checksum(rsdp,
	                                        ACPI_RSDP_CHECKSUM_LENGTH);
	rsdp->ext_checksum = generate_acpi_checksum(rsdp, rsdp->length);

	/* setup DSDT with trivial namespace. */ 
	strncpy(dsdt->signature, DSDT_SIG, 4);
	dsdt->revision = 1;
	strcpy(dsdt->oem_id, "XEN");
	strcpy(dsdt->oem_table_id, "Xen/ia64");
	strcpy(dsdt->asl_compiler_id, "XEN");
	dsdt->asl_compiler_revision = (XEN_VERSION<<16)|(XEN_SUBVERSION);

	/* Trivial namespace, avoids ACPI CA complaints */
	tables->aml[0] = 0x10; /* Scope */
	tables->aml[1] = 0x40; /* length/offset to next object (patched) */
	tables->aml[2] = 0x00;
	strncpy((char *)&tables->aml[3], "_SB_", 4);

	/* The processor object isn't absolutely necessary, revist for SMP */
	aml_len = 7;
	for (i = 0; i < 3; i++) {
		unsigned char *p = tables->aml + aml_len;
		p[0] = 0x5b; /* processor object */
		p[1] = 0x83;
		p[2] = 0x0b; /* next */
		p[3] = 'C';
		p[4] = 'P';
		snprintf ((char *)p + 5, 3, "%02x", i);
		if (i < 16)
			p[5] = 'U';
		p[7] = i;	/* acpi_id */
		p[8] = 0;	/* pblk_addr */
		p[9] = 0;
		p[10] = 0;
		p[11] = 0;
		p[12] = 0;	/* pblk_len */
		aml_len += 13;
	}
	tables->aml[1] = 0x40 + ((aml_len - 1) & 0x0f);
	tables->aml[2] = (aml_len - 1) >> 4;
	dsdt->length = sizeof(struct acpi_table_header) + aml_len;
	dsdt->checksum = generate_acpi_checksum(dsdt, dsdt->length);

	/* setup MADT */
	strncpy(madt->header.signature, APIC_SIG, 4);
	madt->header.revision = 2;
	madt->header.length = sizeof(struct acpi_table_madt) +
		MAX_VIRT_CPUS * sizeof(struct acpi_table_lsapic);
	strcpy(madt->header.oem_id, "XEN");
	strcpy(madt->header.oem_table_id, "Xen/ia64");
	strcpy(madt->header.asl_compiler_id, "XEN");
	madt->header.asl_compiler_revision = (XEN_VERSION<<16)|(XEN_SUBVERSION);

	/* An LSAPIC entry describes a CPU.  */
	for (i = 0; i < MAX_VIRT_CPUS; i++) {
		lsapic[i].header.type = ACPI_MADT_LSAPIC;
		lsapic[i].header.length = sizeof(struct acpi_table_lsapic);
		lsapic[i].acpi_id = i;
		lsapic[i].id = i;
		lsapic[i].eid = 0;
		lsapic[i].flags.enabled = (d->vcpu[i] != NULL);
	}

	madt->header.checksum = generate_acpi_checksum(madt,
	                                               madt->header.length);
	return;
}

#define NUM_EFI_SYS_TABLES 6
#define NUM_MEM_DESCS	64 //large enough

struct dom0_passthrough_arg {
#ifdef CONFIG_XEN_IA64_DOM0_VP
    struct domain*      d;
    int                 flags;
#endif
    efi_memory_desc_t *md;
    int*                i;
};

static int
dom_fw_dom0_passthrough(efi_memory_desc_t *md, void *arg__)
{
    struct dom0_passthrough_arg* arg = (struct dom0_passthrough_arg*)arg__;
    unsigned long paddr;

#ifdef CONFIG_XEN_IA64_DOM0_VP
    struct domain* d = arg->d;
    u64 start = md->phys_addr;
    u64 size = md->num_pages << EFI_PAGE_SHIFT;

    if (md->type == EFI_MEMORY_MAPPED_IO ||
        md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {

        //XXX some machine has large mmio area whose size is about several TB.
        //    It requires impractical memory to map such a huge region
        //    to a domain.
        //    For now we don't map it, but later we must fix this.
        if (md->type == EFI_MEMORY_MAPPED_IO && (size > 0x100000000UL))
            return 0;

        paddr = assign_domain_mmio_page(d, start, size);
    } else
        paddr = assign_domain_mach_page(d, start, size, arg->flags);
#else
    paddr = md->phys_addr;
#endif

    BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
           md->type != EFI_RUNTIME_SERVICES_DATA &&
           md->type != EFI_ACPI_RECLAIM_MEMORY &&
           md->type != EFI_ACPI_MEMORY_NVS &&
           md->type != EFI_RESERVED_TYPE &&
           md->type != EFI_MEMORY_MAPPED_IO &&
           md->type != EFI_MEMORY_MAPPED_IO_PORT_SPACE);

    arg->md->type = md->type;
    arg->md->pad = 0;
    arg->md->phys_addr = paddr;
    arg->md->virt_addr = 0;
    arg->md->num_pages = md->num_pages;
    arg->md->attribute = md->attribute;

    (*arg->i)++;
    arg->md++;
    return 0;
}

/*
 * Create dom0 MDT entries for conventional memory below 1MB.  Without
 * this Linux will assume VGA is present because 0xA0000 will always
 * be either a hole in the MDT or an I/O region via the passthrough.
 */
static int
dom_fw_dom0_lowmem(efi_memory_desc_t *md, void *arg__)
{
    struct dom0_passthrough_arg* arg = (struct dom0_passthrough_arg*)arg__;
    u64 end = min(HYPERCALL_START,
                  md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT));

    BUG_ON(md->type != EFI_CONVENTIONAL_MEMORY);

    /* avoid hypercall area */
    if (md->phys_addr >= HYPERCALL_START)
        return 0;

    /* avoid firmware base area */
    if (md->phys_addr < dom_pa(imva_fw_base))
        end = min(end, dom_pa(imva_fw_base));
    else if (md->phys_addr < dom_pa(imva_fw_base + PAGE_SIZE)) {
        if (end < dom_pa(imva_fw_base + PAGE_SIZE))
            return 0;
        md->phys_addr = dom_pa(imva_fw_base + PAGE_SIZE);
    }

    arg->md->type = md->type;
    arg->md->pad = 0;
    arg->md->phys_addr = md->phys_addr;
    arg->md->virt_addr = 0;
    arg->md->num_pages = (end - md->phys_addr) >> EFI_PAGE_SHIFT;
    arg->md->attribute = md->attribute;

    (*arg->i)++;
    arg->md++;

    /* if firmware area spliced the md, add the upper part here */
    if (end == dom_pa(imva_fw_base)) {
        end = min(HYPERCALL_START,
                  md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT));
	if (end > dom_pa(imva_fw_base + PAGE_SIZE)) {
            arg->md->type = md->type;
            arg->md->pad = 0;
            arg->md->phys_addr = dom_pa(imva_fw_base + PAGE_SIZE);
            arg->md->virt_addr = 0;
            arg->md->num_pages = (end - arg->md->phys_addr) >> EFI_PAGE_SHIFT;
            arg->md->attribute = md->attribute;

            (*arg->i)++;
            arg->md++;
        }
    }
    return 0;
}

static int
efi_mdt_cmp(const void *a, const void *b)
{
	const efi_memory_desc_t *x = a, *y = b;

	if (x->phys_addr > y->phys_addr)
		return 1;
	if (x->phys_addr < y->phys_addr)
		return -1;

	// num_pages == 0 is allowed.
	if (x->num_pages > y->num_pages)
		return 1;
	if (x->num_pages < y->num_pages)
		return -1;

	return 0;
}

static void
dom_fw_init (struct domain *d, struct ia64_boot_param *bp, char *fw_mem, int fw_mem_size, unsigned long maxmem)
{
	efi_system_table_t *efi_systab;
	efi_runtime_services_t *efi_runtime;
	efi_config_table_t *efi_tables;
	struct ia64_sal_systab *sal_systab;
	struct ia64_sal_desc_entry_point *sal_ed;
	struct ia64_sal_desc_ap_wakeup *sal_wakeup;
	fpswa_interface_t *fpswa_inf;
	efi_memory_desc_t *efi_memmap, *md;
 	struct xen_sal_data *sal_data;
	unsigned long *pfn;
	unsigned char checksum = 0;
	char *cp, *fw_vendor;
	int num_mds, j, i = 0;
#ifdef CONFIG_XEN_IA64_DOM0_VP
	const unsigned long start_mpaddr = 0;
#else
	const unsigned long start_mpaddr = ((d==dom0)?dom0_start:0);
#endif

/* FIXME: should check size but for now we have a whole MB to play with.
   And if stealing code from fw-emu.c, watch out for new fw_vendor on the end!
	if (fw_mem_size < sizeof(fw_mem_proto)) {
		printf("sys_fw_init: insufficient space for fw_mem\n");
		return 0;
	}
*/
	memset(fw_mem, 0, fw_mem_size);

	cp = fw_mem;
	efi_systab  = (void *) cp; cp += sizeof(*efi_systab);
	efi_runtime = (void *) cp; cp += sizeof(*efi_runtime);
	efi_tables  = (void *) cp; cp += NUM_EFI_SYS_TABLES * sizeof(*efi_tables);
	sal_systab  = (void *) cp; cp += sizeof(*sal_systab);
	sal_ed      = (void *) cp; cp += sizeof(*sal_ed);
	sal_wakeup  = (void *) cp; cp += sizeof(*sal_wakeup);
	fpswa_inf   = (void *) cp; cp += sizeof(*fpswa_inf);
	efi_memmap  = (void *) cp; cp += NUM_MEM_DESCS*sizeof(*efi_memmap);
	pfn         = (void *) cp; cp += NFUNCPTRS * 2 * sizeof(pfn);
	sal_data    = (void *) cp; cp += sizeof(*sal_data);

	/* Initialise for EFI_SET_VIRTUAL_ADDRESS_MAP emulation */
	d->arch.efi_runtime = efi_runtime;
	d->arch.fpswa_inf   = fpswa_inf;
	d->arch.sal_data    = sal_data;

	memset(efi_systab, 0, sizeof(efi_systab));
	efi_systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
	efi_systab->hdr.revision  = EFI_SYSTEM_TABLE_REVISION;
	efi_systab->hdr.headersize = sizeof(efi_systab->hdr);
	fw_vendor = cp;
	cp += sizeof(FW_VENDOR) + (8-((unsigned long)cp & 7)); // round to 64-bit boundary

	memcpy(fw_vendor,FW_VENDOR,sizeof(FW_VENDOR));
	efi_systab->fw_vendor = dom_pa((unsigned long) fw_vendor);
	efi_systab->fw_revision = 1;
	efi_systab->runtime = (void *) dom_pa((unsigned long) efi_runtime);
	efi_systab->nr_tables = NUM_EFI_SYS_TABLES;
	efi_systab->tables = dom_pa((unsigned long) efi_tables);

	efi_runtime->hdr.signature = EFI_RUNTIME_SERVICES_SIGNATURE;
	efi_runtime->hdr.revision = EFI_RUNTIME_SERVICES_REVISION;
	efi_runtime->hdr.headersize = sizeof(efi_runtime->hdr);

	EFI_HYPERCALL_PATCH(efi_runtime->get_time,EFI_GET_TIME);
	EFI_HYPERCALL_PATCH(efi_runtime->set_time,EFI_SET_TIME);
	EFI_HYPERCALL_PATCH(efi_runtime->get_wakeup_time,EFI_GET_WAKEUP_TIME);
	EFI_HYPERCALL_PATCH(efi_runtime->set_wakeup_time,EFI_SET_WAKEUP_TIME);
	EFI_HYPERCALL_PATCH(efi_runtime->set_virtual_address_map,EFI_SET_VIRTUAL_ADDRESS_MAP);
	EFI_HYPERCALL_PATCH(efi_runtime->get_variable,EFI_GET_VARIABLE);
	EFI_HYPERCALL_PATCH(efi_runtime->get_next_variable,EFI_GET_NEXT_VARIABLE);
	EFI_HYPERCALL_PATCH(efi_runtime->set_variable,EFI_SET_VARIABLE);
	EFI_HYPERCALL_PATCH(efi_runtime->get_next_high_mono_count,EFI_GET_NEXT_HIGH_MONO_COUNT);
	EFI_HYPERCALL_PATCH(efi_runtime->reset_system,EFI_RESET_SYSTEM);

	efi_tables[0].guid = SAL_SYSTEM_TABLE_GUID;
	efi_tables[0].table = dom_pa((unsigned long) sal_systab);
	for (i = 1; i < NUM_EFI_SYS_TABLES; i++) {
		efi_tables[i].guid = NULL_GUID;
		efi_tables[i].table = 0;
	}
	if (d == dom0) {
		printf("Domain0 EFI passthrough:");
		i = 1;
		if (efi.mps) {
			efi_tables[i].guid = MPS_TABLE_GUID;
			efi_tables[i].table = __pa(efi.mps);
			printf(" MPS=0x%lx",efi_tables[i].table);
			i++;
		}

		touch_acpi_table();

		if (efi.acpi20) {
			efi_tables[i].guid = ACPI_20_TABLE_GUID;
			efi_tables[i].table = __pa(efi.acpi20);
			printf(" ACPI 2.0=0x%lx",efi_tables[i].table);
			i++;
		}
		if (efi.acpi) {
			efi_tables[i].guid = ACPI_TABLE_GUID;
			efi_tables[i].table = __pa(efi.acpi);
			printf(" ACPI=0x%lx",efi_tables[i].table);
			i++;
		}
		if (efi.smbios) {
			efi_tables[i].guid = SMBIOS_TABLE_GUID;
			efi_tables[i].table = __pa(efi.smbios);
			printf(" SMBIOS=0x%lx",efi_tables[i].table);
			i++;
		}
		if (efi.hcdp) {
			efi_tables[i].guid = HCDP_TABLE_GUID;
			efi_tables[i].table = __pa(efi.hcdp);
			printf(" HCDP=0x%lx",efi_tables[i].table);
			i++;
		}
		printf("\n");
	} else {
		printf("DomainU EFI build up:");
		i = 1;

		if ((unsigned long)fw_mem + fw_mem_size - (unsigned long)cp >=
		    sizeof(struct fake_acpi_tables)) {
			struct fake_acpi_tables *acpi_tables;

			acpi_tables = (void *)cp;
			cp += sizeof(struct fake_acpi_tables);
			dom_fw_fake_acpi(d, acpi_tables);

			efi_tables[i].guid = ACPI_20_TABLE_GUID;
			efi_tables[i].table = dom_pa((unsigned long) acpi_tables);
			printf(" ACPI 2.0=0x%lx",efi_tables[i].table);
			i++;
		}
		printf("\n");
	}

	/* fill in the SAL system table: */
	memcpy(sal_systab->signature, "SST_", 4);
	sal_systab->size = sizeof(*sal_systab);
	sal_systab->sal_rev_minor = 1;
	sal_systab->sal_rev_major = 0;
	sal_systab->entry_count = 2;

	strcpy((char *)sal_systab->oem_id, "Xen/ia64");
	strcpy((char *)sal_systab->product_id, "Xen/ia64");

	/* fill in an entry point: */
	sal_ed->type = SAL_DESC_ENTRY_POINT;
	sal_ed->pal_proc = FW_HYPERCALL_PAL_CALL_PADDR + start_mpaddr;
	dom_fw_pal_hypercall_patch (d, sal_ed->pal_proc);
	sal_ed->sal_proc = FW_HYPERCALL_SAL_CALL_PADDR + start_mpaddr;
	dom_fw_hypercall_patch (d, sal_ed->sal_proc, FW_HYPERCALL_SAL_CALL, 1);
	sal_ed->gp = 0;  // will be ignored

	/* Fill an AP wakeup descriptor.  */
	sal_wakeup->type = SAL_DESC_AP_WAKEUP;
	sal_wakeup->mechanism = IA64_SAL_AP_EXTERNAL_INT;
	sal_wakeup->vector = XEN_SAL_BOOT_RENDEZ_VEC;

	/* Compute checksum.  */
	for (cp = (char *) sal_systab; cp < (char *) efi_memmap; ++cp)
		checksum += *cp;
	sal_systab->checksum = -checksum;

	/* SAL return point.  */
	d->arch.sal_return_addr = FW_HYPERCALL_SAL_RETURN_PADDR + start_mpaddr;
	dom_fw_hypercall_patch (d, d->arch.sal_return_addr,
				FW_HYPERCALL_SAL_RETURN, 0);

	/* Fill in the FPSWA interface: */
	fpswa_inf->revision = fpswa_interface->revision;
	dom_fpswa_hypercall_patch(d);
	fpswa_inf->fpswa = (void *) FW_HYPERCALL_FPSWA_ENTRY_PADDR + start_mpaddr;

	i = 0; /* Used by MAKE_MD */

	/* Create dom0/domu md entry for fw_mem area */
	MAKE_MD(EFI_ACPI_RECLAIM_MEMORY, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
	        dom_pa((unsigned long)fw_mem),
	        dom_pa((unsigned long)fw_mem + fw_mem_size), 1);

	if (d == dom0) {
#ifndef CONFIG_XEN_IA64_DOM0_VP
		/*
		 * This is a bad hack.  Dom0 may share other domains' memory
		 * through a dom0 physical address.  Unfortunately, this
		 * address may be used in maddr_to_page (e.g. in the loopback
		 * driver) but when Linux initializes memory it only creates
		 * page structs for the physical memory it knows about.  And
		 * on ia64, only for full writeback granules.  So, we reserve
		 * the last full granule of Xen's memory for dom0 (in
		 * start_kernel) to ensure dom0 creates a large enough memmap
		 */
		unsigned long last_start = max_page << PAGE_SHIFT;
		unsigned long last_end = last_start + IA64_GRANULE_SIZE;

		/* simulate 1MB free memory at physical address zero */
		MAKE_MD(EFI_LOADER_DATA,EFI_MEMORY_WB,0*MB,1*MB, 0);//XXX
#endif
		/* hypercall patches live here, masquerade as reserved PAL memory */
		MAKE_MD(EFI_PAL_CODE,EFI_MEMORY_WB|EFI_MEMORY_RUNTIME,HYPERCALL_START,HYPERCALL_END, 0);

#ifndef CONFIG_XEN_IA64_DOM0_VP
 		MAKE_MD(EFI_CONVENTIONAL_MEMORY,EFI_MEMORY_WB,HYPERCALL_END,maxmem-IA64_GRANULE_SIZE, 0);//XXX make sure this doesn't overlap on i/o, runtime area.
/* hack */	MAKE_MD(EFI_CONVENTIONAL_MEMORY,EFI_MEMORY_WB,last_start,last_end,1);
#endif

		/* pass through the I/O port space */
		if (!running_on_sim) {
			struct dom0_passthrough_arg arg;
			arg.md = &efi_memmap[i];
			arg.i = &i;
#ifdef CONFIG_XEN_IA64_DOM0_VP
			arg.d = d;
			arg.flags = ASSIGN_writable;
#endif
			//XXX Is this needed?
			efi_memmap_walk_type(EFI_RUNTIME_SERVICES_CODE,
			                     dom_fw_dom0_passthrough, &arg);
			// for ACPI table.
#ifdef CONFIG_XEN_IA64_DOM0_VP
			arg.flags = ASSIGN_readonly;
#endif
			efi_memmap_walk_type(EFI_RUNTIME_SERVICES_DATA,
			                     dom_fw_dom0_passthrough, &arg);
#ifdef CONFIG_XEN_IA64_DOM0_VP
			arg.flags = ASSIGN_writable;
#endif
			efi_memmap_walk_type(EFI_ACPI_RECLAIM_MEMORY,
			                     dom_fw_dom0_passthrough, &arg);
			efi_memmap_walk_type(EFI_ACPI_MEMORY_NVS,
			                     dom_fw_dom0_passthrough, &arg);
			efi_memmap_walk_type(EFI_RESERVED_TYPE,
			                     dom_fw_dom0_passthrough, &arg);
			efi_memmap_walk_type(EFI_MEMORY_MAPPED_IO,
			                     dom_fw_dom0_passthrough, &arg);
			efi_memmap_walk_type(EFI_MEMORY_MAPPED_IO_PORT_SPACE,
			                     dom_fw_dom0_passthrough, &arg);
			efi_memmap_walk_type(EFI_CONVENTIONAL_MEMORY,
			                     dom_fw_dom0_lowmem, &arg);
		}
		else MAKE_MD(EFI_RESERVED_TYPE,0,0,0,0);
	} else {
#ifndef CONFIG_XEN_IA64_DOM0_VP
	  	/* Dom0 maps legacy mmio in first MB.  */
		MAKE_MD(EFI_LOADER_DATA, EFI_MEMORY_WB, 0*MB, 1*MB, 1);
		MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
			HYPERCALL_END, maxmem, 1);
#endif
		/* hypercall patches live here, masquerade as reserved
		   PAL memory */
		MAKE_MD(EFI_PAL_CODE, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
			HYPERCALL_START, HYPERCALL_END, 1);
		/* Create an entry for IO ports.  */
		MAKE_MD(EFI_MEMORY_MAPPED_IO_PORT_SPACE, EFI_MEMORY_UC,
			IO_PORTS_PADDR, IO_PORTS_PADDR + IO_PORTS_SIZE, 1);