direct-io.hg

view xen/arch/ia64/xen/dom_fw.c @ 13456:2d4807ed1056

[IA64] Respect memory attributes for EFI_RUNTIME memory

Respect memory attributes when mapping EFI_RUNTIME and reserved pages.
Not all of these pages are writable and/or cachable!

Signed-off-by: Jes Sorensen <jes@sgi.com>
author awilliam@xenbuild2.aw
date Thu Jan 04 16:00:56 2007 -0700 (2007-01-04)
parents d9b2dd57fdc4
children 90db0f68b121
line source
1 /*
2 * Xen domain firmware emulation support
3 * Copyright (C) 2004 Hewlett-Packard Co.
4 * Dan Magenheimer (dan.magenheimer@hp.com)
5 *
6 * Copyright (c) 2006 Isaku Yamahata <yamahata at valinux co jp>
7 * VA Linux Systems Japan K.K.
8 * dom0 vp model support
9 */
11 #include <xen/config.h>
12 #include <asm/system.h>
13 #include <asm/pgalloc.h>
15 #include <linux/efi.h>
16 #include <linux/sort.h>
17 #include <asm/io.h>
18 #include <asm/pal.h>
19 #include <asm/sal.h>
20 #include <asm/meminit.h>
21 #include <asm/fpswa.h>
22 #include <xen/version.h>
23 #include <xen/acpi.h>
24 #include <xen/errno.h>
26 #include <asm/dom_fw.h>
27 #include <asm/bundle.h>
29 #define ONE_MB (1UL << 20)
31 extern unsigned long running_on_sim;
33 #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"
35 #define MAKE_MD(typ, attr, start, end) \
36 do { \
37 md = tables->efi_memmap + i++; \
38 md->type = typ; \
39 md->pad = 0; \
40 md->phys_addr = start; \
41 md->virt_addr = 0; \
42 md->num_pages = (end - start) >> EFI_PAGE_SHIFT; \
43 md->attribute = attr; \
44 } while (0)
46 #define EFI_HYPERCALL_PATCH(tgt, call) \
47 do { \
48 dom_efi_hypercall_patch(d, FW_HYPERCALL_##call##_PADDR, \
49 FW_HYPERCALL_##call, hypercalls_imva); \
50 /* Descriptor address. */ \
51 tables->efi_runtime.tgt = \
52 FW_FIELD_MPA(func_ptrs) + 8 * pfn; \
53 /* Descriptor. */ \
54 tables->func_ptrs[pfn++] = FW_HYPERCALL_##call##_PADDR; \
55 tables->func_ptrs[pfn++] = 0; \
56 } while (0)
58 /* allocate a page for fw
59 * guest_setup() @ libxc/xc_linux_build.c does for domU
60 */
61 static inline void
62 assign_new_domain_page_if_dom0(struct domain *d, unsigned long mpaddr)
63 {
64 if (d == dom0)
65 assign_new_domain0_page(d, mpaddr);
66 }
68 /**************************************************************************
69 Hypercall bundle creation
70 **************************************************************************/
72 static void
73 build_hypercall_bundle(u64 *imva, u64 brkimm, u64 hypnum, u64 ret)
74 {
75 INST64_A5 slot0;
76 INST64_I19 slot1;
77 INST64_B4 slot2;
78 IA64_BUNDLE bundle;
80 // slot1: mov r2 = hypnum (low 20 bits)
81 slot0.inst = 0;
82 slot0.qp = 0; slot0.r1 = 2; slot0.r3 = 0; slot0.major = 0x9;
83 slot0.imm7b = hypnum; slot0.imm9d = hypnum >> 7;
84 slot0.imm5c = hypnum >> 16; slot0.s = 0;
85 // slot1: break brkimm
86 slot1.inst = 0;
87 slot1.qp = 0; slot1.x6 = 0; slot1.x3 = 0; slot1.major = 0x0;
88 slot1.imm20 = brkimm; slot1.i = brkimm >> 20;
89 // if ret slot2: br.ret.sptk.many rp
90 // else slot2: br.cond.sptk.many rp
91 slot2.inst = 0; slot2.qp = 0; slot2.p = 1; slot2.b2 = 0;
92 slot2.wh = 0; slot2.d = 0; slot2.major = 0x0;
93 if (ret) {
94 slot2.btype = 4; slot2.x6 = 0x21;
95 }
96 else {
97 slot2.btype = 0; slot2.x6 = 0x20;
98 }
100 bundle.i64[0] = 0; bundle.i64[1] = 0;
101 bundle.template = 0x11;
102 bundle.slot0 = slot0.inst; bundle.slot2 = slot2.inst;
103 bundle.slot1a = slot1.inst; bundle.slot1b = slot1.inst >> 18;
105 imva[0] = bundle.i64[0]; imva[1] = bundle.i64[1];
106 ia64_fc(imva);
107 ia64_fc(imva + 1);
108 }
110 static void
111 build_pal_hypercall_bundles(u64 *imva, u64 brkimm, u64 hypnum)
112 {
113 extern unsigned long pal_call_stub[];
114 IA64_BUNDLE bundle;
115 INST64_A5 slot_a5;
116 INST64_M37 slot_m37;
118 /* The source of the hypercall stub is the pal_call_stub function
119 defined in xenasm.S. */
121 /* Copy the first bundle and patch the hypercall number. */
122 bundle.i64[0] = pal_call_stub[0];
123 bundle.i64[1] = pal_call_stub[1];
124 slot_a5.inst = bundle.slot0;
125 slot_a5.imm7b = hypnum;
126 slot_a5.imm9d = hypnum >> 7;
127 slot_a5.imm5c = hypnum >> 16;
128 bundle.slot0 = slot_a5.inst;
129 imva[0] = bundle.i64[0];
130 imva[1] = bundle.i64[1];
131 ia64_fc(imva);
132 ia64_fc(imva + 1);
134 /* Copy the second bundle and patch the hypercall vector. */
135 bundle.i64[0] = pal_call_stub[2];
136 bundle.i64[1] = pal_call_stub[3];
137 slot_m37.inst = bundle.slot0;
138 slot_m37.imm20a = brkimm;
139 slot_m37.i = brkimm >> 20;
140 bundle.slot0 = slot_m37.inst;
141 imva[2] = bundle.i64[0];
142 imva[3] = bundle.i64[1];
143 ia64_fc(imva + 2);
144 ia64_fc(imva + 3);
145 }
147 // builds a hypercall bundle at domain physical address
148 static void
149 dom_fpswa_hypercall_patch(struct domain *d, unsigned long imva)
150 {
151 unsigned long *entry_imva, *patch_imva;
152 const unsigned long entry_paddr = FW_HYPERCALL_FPSWA_ENTRY_PADDR;
153 const unsigned long patch_paddr = FW_HYPERCALL_FPSWA_PATCH_PADDR;
155 entry_imva = (unsigned long *)(imva + entry_paddr -
156 FW_HYPERCALL_BASE_PADDR);
157 patch_imva = (unsigned long *)(imva + patch_paddr -
158 FW_HYPERCALL_BASE_PADDR);
160 /* Descriptor. */
161 *entry_imva++ = patch_paddr;
162 *entry_imva = 0;
164 build_hypercall_bundle(patch_imva, d->arch.breakimm,
165 FW_HYPERCALL_FPSWA, 1);
166 }
168 // builds a hypercall bundle at domain physical address
169 static void
170 dom_efi_hypercall_patch(struct domain *d, unsigned long paddr,
171 unsigned long hypercall, unsigned long imva)
172 {
173 build_hypercall_bundle((u64 *)(imva + paddr - FW_HYPERCALL_BASE_PADDR),
174 d->arch.breakimm, hypercall, 1);
175 }
177 // builds a hypercall bundle at domain physical address
178 static void
179 dom_fw_hypercall_patch(struct domain *d, unsigned long paddr,
180 unsigned long hypercall,unsigned long ret,
181 unsigned long imva)
182 {
183 build_hypercall_bundle((u64 *)(imva + paddr - FW_HYPERCALL_BASE_PADDR),
184 d->arch.breakimm, hypercall, ret);
185 }
187 static void
188 dom_fw_pal_hypercall_patch(struct domain *d, unsigned long paddr,
189 unsigned long imva)
190 {
191 build_pal_hypercall_bundles((u64*)(imva + paddr -
192 FW_HYPERCALL_BASE_PADDR),
193 d->arch.breakimm, FW_HYPERCALL_PAL_CALL);
194 }
196 static inline void
197 print_md(efi_memory_desc_t *md)
198 {
199 u64 size;
201 printk("dom mem: type=%2u, attr=0x%016lx, range=[0x%016lx-0x%016lx) ",
202 md->type, md->attribute, md->phys_addr,
203 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT));
205 size = md->num_pages << EFI_PAGE_SHIFT;
206 if (size > ONE_MB)
207 printk ("(%luMB)\n", size >> 20);
208 else
209 printk ("(%luKB)\n", size >> 10);
210 }
212 static u32 lsapic_nbr;
214 /* Modify lsapic table. Provides LPs. */
215 static int
216 acpi_update_lsapic (acpi_table_entry_header *header, const unsigned long end)
217 {
218 struct acpi_table_lsapic *lsapic;
219 int enable;
221 lsapic = (struct acpi_table_lsapic *) header;
222 if (!lsapic)
223 return -EINVAL;
225 if (lsapic_nbr < MAX_VIRT_CPUS && dom0->vcpu[lsapic_nbr] != NULL)
226 enable = 1;
227 else
228 enable = 0;
229 if (lsapic->flags.enabled && enable) {
230 printk("enable lsapic entry: 0x%lx\n", (u64)lsapic);
231 lsapic->id = lsapic_nbr;
232 lsapic->eid = 0;
233 lsapic_nbr++;
234 } else if (lsapic->flags.enabled) {
235 printk("DISABLE lsapic entry: 0x%lx\n", (u64)lsapic);
236 lsapic->flags.enabled = 0;
237 lsapic->id = 0;
238 lsapic->eid = 0;
239 }
240 return 0;
241 }
243 static int __init
244 acpi_patch_plat_int_src (
245 acpi_table_entry_header *header, const unsigned long end)
246 {
247 struct acpi_table_plat_int_src *plintsrc;
249 plintsrc = (struct acpi_table_plat_int_src *)header;
250 if (!plintsrc)
251 return -EINVAL;
253 if (plintsrc->type == ACPI_INTERRUPT_CPEI) {
254 printk("ACPI_INTERRUPT_CPEI disabled for Domain0\n");
255 plintsrc->type = -1;
256 }
257 return 0;
258 }
260 static u8
261 generate_acpi_checksum(void *tbl, unsigned long len)
262 {
263 u8 *ptr, sum = 0;
265 for (ptr = tbl; len > 0 ; len--, ptr++)
266 sum += *ptr;
268 return 0 - sum;
269 }
271 static int
272 acpi_update_madt_checksum (unsigned long phys_addr, unsigned long size)
273 {
274 struct acpi_table_madt* acpi_madt;
276 if (!phys_addr || !size)
277 return -EINVAL;
279 acpi_madt = (struct acpi_table_madt *) __va(phys_addr);
280 acpi_madt->header.checksum = 0;
281 acpi_madt->header.checksum = generate_acpi_checksum(acpi_madt, size);
283 return 0;
284 }
286 /* base is physical address of acpi table */
287 static void touch_acpi_table(void)
288 {
289 lsapic_nbr = 0;
290 if (acpi_table_parse_madt(ACPI_MADT_LSAPIC, acpi_update_lsapic, 0) < 0)
291 printk("Error parsing MADT - no LAPIC entries\n");
292 if (acpi_table_parse_madt(ACPI_MADT_PLAT_INT_SRC,
293 acpi_patch_plat_int_src, 0) < 0)
294 printk("Error parsing MADT - no PLAT_INT_SRC entries\n");
296 acpi_table_parse(ACPI_APIC, acpi_update_madt_checksum);
298 return;
299 }
301 struct fake_acpi_tables {
302 struct acpi20_table_rsdp rsdp;
303 struct xsdt_descriptor_rev2 xsdt;
304 u64 madt_ptr;
305 struct fadt_descriptor_rev2 fadt;
306 struct facs_descriptor_rev2 facs;
307 struct acpi_table_header dsdt;
308 u8 aml[8 + 11 * MAX_VIRT_CPUS];
309 struct acpi_table_madt madt;
310 struct acpi_table_lsapic lsapic[MAX_VIRT_CPUS];
311 u8 pm1a_evt_blk[4];
312 u8 pm1a_cnt_blk[1];
313 u8 pm_tmr_blk[4];
314 };
315 #define ACPI_TABLE_MPA(field) \
316 FW_ACPI_BASE_PADDR + offsetof(struct fake_acpi_tables, field);
318 /* Create enough of an ACPI structure to make the guest OS ACPI happy. */
319 static void
320 dom_fw_fake_acpi(struct domain *d, struct fake_acpi_tables *tables)
321 {
322 struct acpi20_table_rsdp *rsdp = &tables->rsdp;
323 struct xsdt_descriptor_rev2 *xsdt = &tables->xsdt;
324 struct fadt_descriptor_rev2 *fadt = &tables->fadt;
325 struct facs_descriptor_rev2 *facs = &tables->facs;
326 struct acpi_table_header *dsdt = &tables->dsdt;
327 struct acpi_table_madt *madt = &tables->madt;
328 struct acpi_table_lsapic *lsapic = tables->lsapic;
329 int i;
330 int aml_len;
331 int nbr_cpus;
333 memset(tables, 0, sizeof(struct fake_acpi_tables));
335 /* setup XSDT (64bit version of RSDT) */
336 strncpy(xsdt->signature, XSDT_SIG, 4);
337 /* XSDT points to both the FADT and the MADT, so add one entry */
338 xsdt->length = sizeof(struct xsdt_descriptor_rev2) + sizeof(u64);
339 xsdt->revision = 1;
340 strcpy(xsdt->oem_id, "XEN");
341 strcpy(xsdt->oem_table_id, "Xen/ia64");
342 strcpy(xsdt->asl_compiler_id, "XEN");
343 xsdt->asl_compiler_revision = (xen_major_version() << 16) |
344 xen_minor_version();
346 xsdt->table_offset_entry[0] = ACPI_TABLE_MPA(fadt);
347 tables->madt_ptr = ACPI_TABLE_MPA(madt);
349 xsdt->checksum = generate_acpi_checksum(xsdt, xsdt->length);
351 /* setup FADT */
352 strncpy(fadt->signature, FADT_SIG, 4);
353 fadt->length = sizeof(struct fadt_descriptor_rev2);
354 fadt->revision = FADT2_REVISION_ID;
355 strcpy(fadt->oem_id, "XEN");
356 strcpy(fadt->oem_table_id, "Xen/ia64");
357 strcpy(fadt->asl_compiler_id, "XEN");
358 fadt->asl_compiler_revision = (xen_major_version() << 16) |
359 xen_minor_version();
361 strncpy(facs->signature, FACS_SIG, 4);
362 facs->version = 1;
363 facs->length = sizeof(struct facs_descriptor_rev2);
365 fadt->xfirmware_ctrl = ACPI_TABLE_MPA(facs);
366 fadt->Xdsdt = ACPI_TABLE_MPA(dsdt);
368 /*
369 * All of the below FADT entries are filled it to prevent warnings
370 * from sanity checks in the ACPI CA. Emulate required ACPI hardware
371 * registers in system memory.
372 */
373 fadt->pm1_evt_len = 4;
374 fadt->xpm1a_evt_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
375 fadt->xpm1a_evt_blk.register_bit_width = 8;
376 fadt->xpm1a_evt_blk.address = ACPI_TABLE_MPA(pm1a_evt_blk);
377 fadt->pm1_cnt_len = 1;
378 fadt->xpm1a_cnt_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
379 fadt->xpm1a_cnt_blk.register_bit_width = 8;
380 fadt->xpm1a_cnt_blk.address = ACPI_TABLE_MPA(pm1a_cnt_blk);
381 fadt->pm_tm_len = 4;
382 fadt->xpm_tmr_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
383 fadt->xpm_tmr_blk.register_bit_width = 8;
384 fadt->xpm_tmr_blk.address = ACPI_TABLE_MPA(pm_tmr_blk);
386 fadt->checksum = generate_acpi_checksum(fadt, fadt->length);
388 /* setup RSDP */
389 strncpy(rsdp->signature, RSDP_SIG, 8);
390 strcpy(rsdp->oem_id, "XEN");
391 rsdp->revision = 2; /* ACPI 2.0 includes XSDT */
392 rsdp->length = sizeof(struct acpi20_table_rsdp);
393 rsdp->xsdt_address = ACPI_TABLE_MPA(xsdt);
395 rsdp->checksum = generate_acpi_checksum(rsdp,
396 ACPI_RSDP_CHECKSUM_LENGTH);
397 rsdp->ext_checksum = generate_acpi_checksum(rsdp, rsdp->length);
399 /* setup DSDT with trivial namespace. */
400 strncpy(dsdt->signature, DSDT_SIG, 4);
401 dsdt->revision = 1;
402 strcpy(dsdt->oem_id, "XEN");
403 strcpy(dsdt->oem_table_id, "Xen/ia64");
404 strcpy(dsdt->asl_compiler_id, "XEN");
405 dsdt->asl_compiler_revision = (xen_major_version() << 16) |
406 xen_minor_version();
408 /* Trivial namespace, avoids ACPI CA complaints */
409 tables->aml[0] = 0x10; /* Scope */
410 tables->aml[1] = 0x40; /* length/offset to next object (patched) */
411 tables->aml[2] = 0x00;
412 strncpy((char *)&tables->aml[3], "_SB_", 4);
414 /* The processor object isn't absolutely necessary, revist for SMP */
415 aml_len = 7;
416 for (i = 0; i < 3; i++) {
417 unsigned char *p = tables->aml + aml_len;
418 p[0] = 0x5b; /* processor object */
419 p[1] = 0x83;
420 p[2] = 0x0b; /* next */
421 p[3] = 'C';
422 p[4] = 'P';
423 snprintf ((char *)p + 5, 3, "%02x", i);
424 if (i < 16)
425 p[5] = 'U';
426 p[7] = i; /* acpi_id */
427 p[8] = 0; /* pblk_addr */
428 p[9] = 0;
429 p[10] = 0;
430 p[11] = 0;
431 p[12] = 0; /* pblk_len */
432 aml_len += 13;
433 }
434 tables->aml[1] = 0x40 + ((aml_len - 1) & 0x0f);
435 tables->aml[2] = (aml_len - 1) >> 4;
436 dsdt->length = sizeof(struct acpi_table_header) + aml_len;
437 dsdt->checksum = generate_acpi_checksum(dsdt, dsdt->length);
439 /* setup MADT */
440 strncpy(madt->header.signature, APIC_SIG, 4);
441 madt->header.revision = 2;
442 strcpy(madt->header.oem_id, "XEN");
443 strcpy(madt->header.oem_table_id, "Xen/ia64");
444 strcpy(madt->header.asl_compiler_id, "XEN");
445 madt->header.asl_compiler_revision = (xen_major_version() << 16) |
446 xen_minor_version();
448 /* An LSAPIC entry describes a CPU. */
449 nbr_cpus = 0;
450 for (i = 0; i < MAX_VIRT_CPUS; i++) {
451 lsapic[i].header.type = ACPI_MADT_LSAPIC;
452 lsapic[i].header.length = sizeof(struct acpi_table_lsapic);
453 lsapic[i].acpi_id = i;
454 lsapic[i].id = i;
455 lsapic[i].eid = 0;
456 if (d->vcpu[i] != NULL) {
457 lsapic[i].flags.enabled = 1;
458 nbr_cpus++;
459 }
460 }
461 madt->header.length = sizeof(struct acpi_table_madt) +
462 nbr_cpus * sizeof(struct acpi_table_lsapic);
463 madt->header.checksum = generate_acpi_checksum(madt,
464 madt->header.length);
465 return;
466 }
468 static int
469 efi_mdt_cmp(const void *a, const void *b)
470 {
471 const efi_memory_desc_t *x = a, *y = b;
473 if (x->phys_addr > y->phys_addr)
474 return 1;
475 if (x->phys_addr < y->phys_addr)
476 return -1;
478 // num_pages == 0 is allowed.
479 if (x->num_pages > y->num_pages)
480 return 1;
481 if (x->num_pages < y->num_pages)
482 return -1;
484 return 0;
485 }
487 #define NFUNCPTRS 16
488 #define NUM_EFI_SYS_TABLES 6
489 #define NUM_MEM_DESCS 64 //large enough
491 struct fw_tables {
492 efi_system_table_t efi_systab;
493 efi_runtime_services_t efi_runtime;
494 efi_config_table_t efi_tables[NUM_EFI_SYS_TABLES];
496 struct ia64_sal_systab sal_systab;
497 struct ia64_sal_desc_entry_point sal_ed;
498 struct ia64_sal_desc_ap_wakeup sal_wakeup;
499 /* End of SAL descriptors. Do not forget to update checkum bound. */
501 fpswa_interface_t fpswa_inf;
502 efi_memory_desc_t efi_memmap[NUM_MEM_DESCS];
503 unsigned long func_ptrs[2*NFUNCPTRS];
504 struct xen_sal_data sal_data;
505 unsigned char fw_vendor[sizeof(FW_VENDOR)];
506 };
507 #define FW_FIELD_MPA(field) \
508 FW_TABLES_BASE_PADDR + offsetof(struct fw_tables, field)
510 /* Complete the dom0 memmap. */
511 static int
512 complete_dom0_memmap(struct domain *d,
513 struct fw_tables *tables,
514 unsigned long maxmem,
515 int num_mds)
516 {
517 efi_memory_desc_t *md;
518 u64 addr;
519 void *efi_map_start, *efi_map_end, *p;
520 u64 efi_desc_size;
521 int i;
522 unsigned long dom_mem = maxmem - (d->tot_pages << PAGE_SHIFT);
524 /* Walk through all MDT entries.
525 Copy all interesting entries. */
526 efi_map_start = __va(ia64_boot_param->efi_memmap);
527 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
528 efi_desc_size = ia64_boot_param->efi_memdesc_size;
530 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
531 const efi_memory_desc_t *md = p;
532 efi_memory_desc_t *dom_md = &tables->efi_memmap[num_mds];
533 u64 start = md->phys_addr;
534 u64 size = md->num_pages << EFI_PAGE_SHIFT;
535 u64 end = start + size;
536 unsigned long flags;
538 switch (md->type) {
539 case EFI_RUNTIME_SERVICES_CODE:
540 case EFI_RUNTIME_SERVICES_DATA:
541 case EFI_ACPI_RECLAIM_MEMORY:
542 case EFI_ACPI_MEMORY_NVS:
543 case EFI_RESERVED_TYPE:
544 /*
545 * Map into dom0 - We must respect protection
546 * and cache attributes. Not all of these pages
547 * are writable!!!
548 */
549 flags = ASSIGN_writable; /* dummy - zero */
550 if (md->attribute & EFI_MEMORY_WP)
551 flags |= ASSIGN_readonly;
552 if (md->attribute & EFI_MEMORY_UC)
553 flags |= ASSIGN_nocache;
555 assign_domain_mach_page(d, start, size, flags);
557 /* Fall-through. */
558 case EFI_MEMORY_MAPPED_IO:
559 /* Will be mapped with ioremap. */
560 /* Copy descriptor. */
561 *dom_md = *md;
562 dom_md->virt_addr = 0;
563 num_mds++;
564 break;
566 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
567 /* Map into dom0. */
568 assign_domain_mmio_page(d, start, size);
569 /* Copy descriptor. */
570 *dom_md = *md;
571 dom_md->virt_addr = 0;
572 num_mds++;
573 break;
575 case EFI_CONVENTIONAL_MEMORY:
576 case EFI_LOADER_CODE:
577 case EFI_LOADER_DATA:
578 case EFI_BOOT_SERVICES_CODE:
579 case EFI_BOOT_SERVICES_DATA:
580 if (!(md->attribute & EFI_MEMORY_WB))
581 break;
583 start = max(FW_END_PADDR, start);
584 end = min(start + dom_mem, end);
585 if (end <= start)
586 break;
588 dom_md->type = EFI_CONVENTIONAL_MEMORY;
589 dom_md->phys_addr = start;
590 dom_md->virt_addr = 0;
591 dom_md->num_pages = (end - start) >> EFI_PAGE_SHIFT;
592 dom_md->attribute = EFI_MEMORY_WB;
593 num_mds++;
595 dom_mem -= dom_md->num_pages << EFI_PAGE_SHIFT;
596 d->arch.convmem_end = end;
597 break;
599 case EFI_UNUSABLE_MEMORY:
600 case EFI_PAL_CODE:
601 /* Discard. */
602 break;
604 default:
605 /* Print a warning but continue. */
606 printk("complete_dom0_memmap: warning: "
607 "unhandled MDT entry type %u\n", md->type);
608 }
609 }
610 BUG_ON(num_mds > NUM_MEM_DESCS);
612 sort(tables->efi_memmap, num_mds, sizeof(efi_memory_desc_t),
613 efi_mdt_cmp, NULL);
615 /* setup_guest() @ libxc/xc_linux_build() arranges memory for domU.
616 * however no one arranges memory for dom0,
617 * instead we allocate pages manually.
618 */
619 for (i = 0; i < num_mds; i++) {
620 md = &tables->efi_memmap[i];
622 if (md->type == EFI_LOADER_DATA ||
623 md->type == EFI_PAL_CODE ||
624 md->type == EFI_CONVENTIONAL_MEMORY) {
625 unsigned long start = md->phys_addr & PAGE_MASK;
626 unsigned long end = md->phys_addr +
627 (md->num_pages << EFI_PAGE_SHIFT);
629 if (end == start) {
630 /* md->num_pages = 0 is allowed. */
631 continue;
632 }
634 for (addr = start; addr < end; addr += PAGE_SIZE)
635 assign_new_domain0_page(d, addr);
636 }
637 }
638 // Map low-memory holes & unmapped MMIO for legacy drivers
639 for (addr = 0; addr < ONE_MB; addr += PAGE_SIZE) {
640 if (domain_page_mapped(d, addr))
641 continue;
643 if (efi_mmio(addr, PAGE_SIZE))
644 assign_domain_mmio_page(d, addr, PAGE_SIZE);
645 }
646 return num_mds;
647 }
649 static void
650 dom_fw_init(struct domain *d,
651 struct ia64_boot_param *bp,
652 struct fw_tables *tables,
653 unsigned long hypercalls_imva,
654 unsigned long maxmem)
655 {
656 efi_memory_desc_t *md;
657 unsigned long pfn;
658 unsigned char checksum;
659 char *cp;
660 int num_mds, i;
662 memset(tables, 0, sizeof(struct fw_tables));
664 /* Initialise for EFI_SET_VIRTUAL_ADDRESS_MAP emulation */
665 d->arch.efi_runtime = &tables->efi_runtime;
666 d->arch.fpswa_inf = &tables->fpswa_inf;
667 d->arch.sal_data = &tables->sal_data;
669 /* EFI systab. */
670 tables->efi_systab.hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
671 tables->efi_systab.hdr.revision = EFI_SYSTEM_TABLE_REVISION;
672 tables->efi_systab.hdr.headersize = sizeof(tables->efi_systab.hdr);
674 memcpy(tables->fw_vendor,FW_VENDOR,sizeof(FW_VENDOR));
675 tables->efi_systab.fw_vendor = FW_FIELD_MPA(fw_vendor);
676 tables->efi_systab.fw_revision = 1;
677 tables->efi_systab.runtime = (void *)FW_FIELD_MPA(efi_runtime);
678 tables->efi_systab.nr_tables = NUM_EFI_SYS_TABLES;
679 tables->efi_systab.tables = FW_FIELD_MPA(efi_tables);
681 /* EFI runtime. */
682 tables->efi_runtime.hdr.signature = EFI_RUNTIME_SERVICES_SIGNATURE;
683 tables->efi_runtime.hdr.revision = EFI_RUNTIME_SERVICES_REVISION;
684 tables->efi_runtime.hdr.headersize = sizeof(tables->efi_runtime.hdr);
686 pfn = 0;
687 EFI_HYPERCALL_PATCH(get_time,EFI_GET_TIME);
688 EFI_HYPERCALL_PATCH(set_time,EFI_SET_TIME);
689 EFI_HYPERCALL_PATCH(get_wakeup_time,EFI_GET_WAKEUP_TIME);
690 EFI_HYPERCALL_PATCH(set_wakeup_time,EFI_SET_WAKEUP_TIME);
691 EFI_HYPERCALL_PATCH(set_virtual_address_map,
692 EFI_SET_VIRTUAL_ADDRESS_MAP);
693 EFI_HYPERCALL_PATCH(get_variable,EFI_GET_VARIABLE);
694 EFI_HYPERCALL_PATCH(get_next_variable,EFI_GET_NEXT_VARIABLE);
695 EFI_HYPERCALL_PATCH(set_variable,EFI_SET_VARIABLE);
696 EFI_HYPERCALL_PATCH(get_next_high_mono_count,
697 EFI_GET_NEXT_HIGH_MONO_COUNT);
698 EFI_HYPERCALL_PATCH(reset_system,EFI_RESET_SYSTEM);
700 /* System tables. */
701 tables->efi_tables[0].guid = SAL_SYSTEM_TABLE_GUID;
702 tables->efi_tables[0].table = FW_FIELD_MPA(sal_systab);
703 for (i = 1; i < NUM_EFI_SYS_TABLES; i++) {
704 tables->efi_tables[i].guid = NULL_GUID;
705 tables->efi_tables[i].table = 0;
706 }
707 i = 1;
708 if (d == dom0) {
709 /* Write messages to the console. */
710 touch_acpi_table();
712 printk("Domain0 EFI passthrough:");
713 if (efi.mps) {
714 tables->efi_tables[i].guid = MPS_TABLE_GUID;
715 tables->efi_tables[i].table = __pa(efi.mps);
716 printk(" MPS=0x%lx",tables->efi_tables[i].table);
717 i++;
718 }
720 if (efi.acpi20) {
721 tables->efi_tables[i].guid = ACPI_20_TABLE_GUID;
722 tables->efi_tables[i].table = __pa(efi.acpi20);
723 printk(" ACPI 2.0=0x%lx",tables->efi_tables[i].table);
724 i++;
725 }
726 if (efi.acpi) {
727 tables->efi_tables[i].guid = ACPI_TABLE_GUID;
728 tables->efi_tables[i].table = __pa(efi.acpi);
729 printk(" ACPI=0x%lx",tables->efi_tables[i].table);
730 i++;
731 }
732 if (efi.smbios) {
733 tables->efi_tables[i].guid = SMBIOS_TABLE_GUID;
734 tables->efi_tables[i].table = __pa(efi.smbios);
735 printk(" SMBIOS=0x%lx",tables->efi_tables[i].table);
736 i++;
737 }
738 if (efi.hcdp) {
739 tables->efi_tables[i].guid = HCDP_TABLE_GUID;
740 tables->efi_tables[i].table = __pa(efi.hcdp);
741 printk(" HCDP=0x%lx",tables->efi_tables[i].table);
742 i++;
743 }
744 printk("\n");
745 } else {
746 printk("DomainU EFI build up:");
748 tables->efi_tables[i].guid = ACPI_20_TABLE_GUID;
749 tables->efi_tables[i].table = FW_ACPI_BASE_PADDR;
750 printk(" ACPI 2.0=0x%lx",tables->efi_tables[i].table);
751 i++;
752 printk("\n");
753 }
755 /* fill in the SAL system table: */
756 memcpy(tables->sal_systab.signature, "SST_", 4);
757 tables->sal_systab.size = sizeof(tables->sal_systab);
758 tables->sal_systab.sal_rev_minor = 1;
759 tables->sal_systab.sal_rev_major = 0;
760 tables->sal_systab.entry_count = 2;
762 strcpy((char *)tables->sal_systab.oem_id, "Xen/ia64");
763 strcpy((char *)tables->sal_systab.product_id, "Xen/ia64");
765 /* PAL entry point: */
766 tables->sal_ed.type = SAL_DESC_ENTRY_POINT;
767 tables->sal_ed.pal_proc = FW_HYPERCALL_PAL_CALL_PADDR;
768 dom_fw_pal_hypercall_patch(d, tables->sal_ed.pal_proc,
769 hypercalls_imva);
770 /* SAL entry point. */
771 tables->sal_ed.sal_proc = FW_HYPERCALL_SAL_CALL_PADDR;
772 dom_fw_hypercall_patch(d, tables->sal_ed.sal_proc,
773 FW_HYPERCALL_SAL_CALL, 1, hypercalls_imva);
774 tables->sal_ed.gp = 0; /* will be ignored */
776 /* Fill an AP wakeup descriptor. */
777 tables->sal_wakeup.type = SAL_DESC_AP_WAKEUP;
778 tables->sal_wakeup.mechanism = IA64_SAL_AP_EXTERNAL_INT;
779 tables->sal_wakeup.vector = XEN_SAL_BOOT_RENDEZ_VEC;
781 /* Compute checksum. */
782 checksum = 0;
783 for (cp = (char *)&tables->sal_systab;
784 cp < (char *)&tables->fpswa_inf;
785 ++cp)
786 checksum += *cp;
787 tables->sal_systab.checksum = -checksum;
789 /* SAL return point. */
790 dom_fw_hypercall_patch(d, FW_HYPERCALL_SAL_RETURN_PADDR,
791 FW_HYPERCALL_SAL_RETURN, 0, hypercalls_imva);
793 /* Fill in the FPSWA interface: */
794 if (fpswa_interface) {
795 tables->fpswa_inf.revision = fpswa_interface->revision;
796 dom_fpswa_hypercall_patch(d, hypercalls_imva);
797 tables->fpswa_inf.fpswa =
798 (void *)FW_HYPERCALL_FPSWA_ENTRY_PADDR;
799 }
801 i = 0; /* Used by MAKE_MD */
803 /* hypercall patches live here, masquerade as reserved PAL memory */
804 MAKE_MD(EFI_PAL_CODE,EFI_MEMORY_WB|EFI_MEMORY_RUNTIME,
805 FW_HYPERCALL_BASE_PADDR, FW_HYPERCALL_END_PADDR);
807 /* Create dom0/domu md entry for fw and cpi tables area. */
808 MAKE_MD(EFI_ACPI_MEMORY_NVS, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
809 FW_ACPI_BASE_PADDR, FW_ACPI_END_PADDR);
810 MAKE_MD(EFI_RUNTIME_SERVICES_DATA, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
811 FW_TABLES_BASE_PADDR, FW_TABLES_END_PADDR);
813 if (d != dom0 || running_on_sim) {
814 /* DomU (or hp-ski).
815 Create a continuous memory area. */
816 /* Memory. */
817 MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
818 FW_END_PADDR, maxmem);
819 d->arch.convmem_end = maxmem;
821 /* Create an entry for IO ports. */
822 MAKE_MD(EFI_MEMORY_MAPPED_IO_PORT_SPACE, EFI_MEMORY_UC,
823 IO_PORTS_PADDR, IO_PORTS_PADDR + IO_PORTS_SIZE);
825 num_mds = i;
826 }
827 else {
828 /* Dom0.
829 We must preserve ACPI data from real machine,
830 as well as IO areas. */
831 num_mds = complete_dom0_memmap(d, tables, maxmem, i);
832 }
834 /* Display memmap. */
835 for (i = 0 ; i < num_mds; i++)
836 print_md(&tables->efi_memmap[i]);
838 /* Fill boot_param */
839 bp->efi_systab = FW_FIELD_MPA(efi_systab);
840 bp->efi_memmap = FW_FIELD_MPA(efi_memmap);
841 bp->efi_memmap_size = num_mds * sizeof(efi_memory_desc_t);
842 bp->efi_memdesc_size = sizeof(efi_memory_desc_t);
843 bp->efi_memdesc_version = EFI_MEMDESC_VERSION;
844 bp->command_line = 0;
845 bp->console_info.num_cols = 80;
846 bp->console_info.num_rows = 25;
847 bp->console_info.orig_x = 0;
848 bp->console_info.orig_y = 24;
849 if (fpswa_interface)
850 bp->fpswa = FW_FIELD_MPA(fpswa_inf);
851 }
853 void dom_fw_setup(struct domain *d, unsigned long bp_mpa, unsigned long maxmem)
854 {
855 struct ia64_boot_param *bp;
856 unsigned long imva_tables_base;
857 unsigned long imva_hypercall_base;
859 BUILD_BUG_ON(sizeof(struct fw_tables) >
860 (FW_TABLES_END_PADDR - FW_TABLES_BASE_PADDR));
862 BUILD_BUG_ON(sizeof(struct fake_acpi_tables) >
863 (FW_ACPI_END_PADDR - FW_ACPI_BASE_PADDR));
865 /* Create page for hypercalls. */
866 assign_new_domain_page_if_dom0(d, FW_HYPERCALL_BASE_PADDR);
867 imva_hypercall_base = (unsigned long)domain_mpa_to_imva
868 (d, FW_HYPERCALL_BASE_PADDR);
870 /* Create page for acpi tables. */
871 if (d != dom0) {
872 void *imva;
874 assign_new_domain_page_if_dom0(d, FW_ACPI_BASE_PADDR);
875 imva = domain_mpa_to_imva (d, FW_ACPI_BASE_PADDR);
876 dom_fw_fake_acpi(d, (struct fake_acpi_tables *)imva);
877 }
879 /* Create page for FW tables. */
880 assign_new_domain_page_if_dom0(d, FW_TABLES_BASE_PADDR);
881 imva_tables_base = (unsigned long)domain_mpa_to_imva
882 (d, FW_TABLES_BASE_PADDR);
884 /* Create page for boot_param. */
885 assign_new_domain_page_if_dom0(d, bp_mpa);
886 bp = domain_mpa_to_imva(d, bp_mpa);
888 dom_fw_init(d, bp, (struct fw_tables *)imva_tables_base,
889 imva_hypercall_base, maxmem);
890 }