debuggers.hg

view xen/arch/ia64/xen/dom_fw.c @ 13702:d2784d93e760

ia64 and ppc: Remove uses of strcpy and strncpy.
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author kfraser@localhost.localdomain
date Mon Jan 29 15:01:33 2007 +0000 (2007-01-29)
parents 90db0f68b121
children 0803bdfdd9c5
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 strlcpy(xsdt->signature, XSDT_SIG, sizeof(xsdt->signature));
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 strlcpy(xsdt->oem_id, "XEN", sizeof(xsdt->oem_id));
341 strlcpy(xsdt->oem_table_id, "Xen/ia64", sizeof(xsdt->oem_table_id));
342 strlcpy(xsdt->asl_compiler_id, "XEN", sizeof(xsdt->asl_compiler_id));
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 strlcpy(fadt->signature, FADT_SIG, sizeof(fadt->signature));
353 fadt->length = sizeof(struct fadt_descriptor_rev2);
354 fadt->revision = FADT2_REVISION_ID;
355 strlcpy(fadt->oem_id, "XEN", sizeof(fadt->oem_id));
356 strlcpy(fadt->oem_table_id, "Xen/ia64", sizeof(fadt->oem_table_id));
357 strlcpy(fadt->asl_compiler_id, "XEN", sizeof(fadt->asl_compiler_id));
358 fadt->asl_compiler_revision = (xen_major_version() << 16) |
359 xen_minor_version();
361 strlcpy(facs->signature, FACS_SIG, sizeof(facs->signature));
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 strlcpy(rsdp->signature, RSDP_SIG, sizeof(rsdp->signature));
390 strlcpy(rsdp->oem_id, "XEN", sizeof(rsdp->oem_id));
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 strlcpy(dsdt->signature, DSDT_SIG, sizeof(dsdt->signature));
401 dsdt->revision = 1;
402 strlcpy(dsdt->oem_id, "XEN", sizeof(dsdt->oem_id));
403 strlcpy(dsdt->oem_table_id, "Xen/ia64", sizeof(dsdt->oem_table_id));
404 strlcpy(dsdt->asl_compiler_id, "XEN", sizeof(dsdt->asl_compiler_id));
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 strlcpy((char *)&tables->aml[3], "_SB_", 5);
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 strlcpy(madt->header.signature, APIC_SIG, sizeof(madt->header.signature));
441 madt->header.revision = 2;
442 strlcpy(madt->header.oem_id, "XEN",
443 sizeof(madt->header.oem_id));
444 strlcpy(madt->header.oem_table_id, "Xen/ia64",
445 sizeof(madt->header.oem_table_id));
446 strlcpy(madt->header.asl_compiler_id, "XEN",
447 sizeof(madt->header.asl_compiler_id));
448 madt->header.asl_compiler_revision = (xen_major_version() << 16) |
449 xen_minor_version();
451 /* An LSAPIC entry describes a CPU. */
452 nbr_cpus = 0;
453 for (i = 0; i < MAX_VIRT_CPUS; i++) {
454 lsapic[i].header.type = ACPI_MADT_LSAPIC;
455 lsapic[i].header.length = sizeof(struct acpi_table_lsapic);
456 lsapic[i].acpi_id = i;
457 lsapic[i].id = i;
458 lsapic[i].eid = 0;
459 if (d->vcpu[i] != NULL) {
460 lsapic[i].flags.enabled = 1;
461 nbr_cpus++;
462 }
463 }
464 madt->header.length = sizeof(struct acpi_table_madt) +
465 nbr_cpus * sizeof(struct acpi_table_lsapic);
466 madt->header.checksum = generate_acpi_checksum(madt,
467 madt->header.length);
468 return;
469 }
471 static int
472 efi_mdt_cmp(const void *a, const void *b)
473 {
474 const efi_memory_desc_t *x = a, *y = b;
476 if (x->phys_addr > y->phys_addr)
477 return 1;
478 if (x->phys_addr < y->phys_addr)
479 return -1;
481 // num_pages == 0 is allowed.
482 if (x->num_pages > y->num_pages)
483 return 1;
484 if (x->num_pages < y->num_pages)
485 return -1;
487 return 0;
488 }
490 #define NFUNCPTRS 16
491 #define NUM_EFI_SYS_TABLES 6
492 #define NUM_MEM_DESCS 64 //large enough
494 struct fw_tables {
495 efi_system_table_t efi_systab;
496 efi_runtime_services_t efi_runtime;
497 efi_config_table_t efi_tables[NUM_EFI_SYS_TABLES];
499 struct ia64_sal_systab sal_systab;
500 struct ia64_sal_desc_entry_point sal_ed;
501 struct ia64_sal_desc_ap_wakeup sal_wakeup;
502 /* End of SAL descriptors. Do not forget to update checkum bound. */
504 fpswa_interface_t fpswa_inf;
505 efi_memory_desc_t efi_memmap[NUM_MEM_DESCS];
506 unsigned long func_ptrs[2*NFUNCPTRS];
507 struct xen_sal_data sal_data;
508 unsigned char fw_vendor[sizeof(FW_VENDOR)];
509 };
510 #define FW_FIELD_MPA(field) \
511 FW_TABLES_BASE_PADDR + offsetof(struct fw_tables, field)
513 /* Complete the dom0 memmap. */
514 static int
515 complete_dom0_memmap(struct domain *d,
516 struct fw_tables *tables,
517 unsigned long maxmem,
518 int num_mds)
519 {
520 efi_memory_desc_t *md;
521 u64 addr;
522 void *efi_map_start, *efi_map_end, *p;
523 u64 efi_desc_size;
524 int i;
525 unsigned long dom_mem = maxmem - (d->tot_pages << PAGE_SHIFT);
527 /* Walk through all MDT entries.
528 Copy all interesting entries. */
529 efi_map_start = __va(ia64_boot_param->efi_memmap);
530 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
531 efi_desc_size = ia64_boot_param->efi_memdesc_size;
533 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
534 const efi_memory_desc_t *md = p;
535 efi_memory_desc_t *dom_md = &tables->efi_memmap[num_mds];
536 u64 start = md->phys_addr;
537 u64 size = md->num_pages << EFI_PAGE_SHIFT;
538 u64 end = start + size;
539 unsigned long flags;
541 switch (md->type) {
542 case EFI_RUNTIME_SERVICES_CODE:
543 case EFI_RUNTIME_SERVICES_DATA:
544 case EFI_ACPI_RECLAIM_MEMORY:
545 case EFI_ACPI_MEMORY_NVS:
546 case EFI_RESERVED_TYPE:
547 /*
548 * Map into dom0 - We must respect protection
549 * and cache attributes. Not all of these pages
550 * are writable!!!
551 */
552 flags = ASSIGN_writable; /* dummy - zero */
553 if (md->attribute & EFI_MEMORY_WP)
554 flags |= ASSIGN_readonly;
555 if ((md->attribute & EFI_MEMORY_UC) &&
556 !(md->attribute & EFI_MEMORY_WB))
557 flags |= ASSIGN_nocache;
559 assign_domain_mach_page(d, start, size, flags);
561 /* Fall-through. */
562 case EFI_MEMORY_MAPPED_IO:
563 /* Will be mapped with ioremap. */
564 /* Copy descriptor. */
565 *dom_md = *md;
566 dom_md->virt_addr = 0;
567 num_mds++;
568 break;
570 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
571 /* Map into dom0. */
572 assign_domain_mmio_page(d, start, size);
573 /* Copy descriptor. */
574 *dom_md = *md;
575 dom_md->virt_addr = 0;
576 num_mds++;
577 break;
579 case EFI_CONVENTIONAL_MEMORY:
580 case EFI_LOADER_CODE:
581 case EFI_LOADER_DATA:
582 case EFI_BOOT_SERVICES_CODE:
583 case EFI_BOOT_SERVICES_DATA:
584 if (!(md->attribute & EFI_MEMORY_WB))
585 break;
587 start = max(FW_END_PADDR, start);
588 end = min(start + dom_mem, end);
589 if (end <= start)
590 break;
592 dom_md->type = EFI_CONVENTIONAL_MEMORY;
593 dom_md->phys_addr = start;
594 dom_md->virt_addr = 0;
595 dom_md->num_pages = (end - start) >> EFI_PAGE_SHIFT;
596 dom_md->attribute = EFI_MEMORY_WB;
597 num_mds++;
599 dom_mem -= dom_md->num_pages << EFI_PAGE_SHIFT;
600 d->arch.convmem_end = end;
601 break;
603 case EFI_UNUSABLE_MEMORY:
604 case EFI_PAL_CODE:
605 /* Discard. */
606 break;
608 default:
609 /* Print a warning but continue. */
610 printk("complete_dom0_memmap: warning: "
611 "unhandled MDT entry type %u\n", md->type);
612 }
613 }
614 BUG_ON(num_mds > NUM_MEM_DESCS);
616 sort(tables->efi_memmap, num_mds, sizeof(efi_memory_desc_t),
617 efi_mdt_cmp, NULL);
619 /* setup_guest() @ libxc/xc_linux_build() arranges memory for domU.
620 * however no one arranges memory for dom0,
621 * instead we allocate pages manually.
622 */
623 for (i = 0; i < num_mds; i++) {
624 md = &tables->efi_memmap[i];
626 if (md->type == EFI_LOADER_DATA ||
627 md->type == EFI_PAL_CODE ||
628 md->type == EFI_CONVENTIONAL_MEMORY) {
629 unsigned long start = md->phys_addr & PAGE_MASK;
630 unsigned long end = md->phys_addr +
631 (md->num_pages << EFI_PAGE_SHIFT);
633 if (end == start) {
634 /* md->num_pages = 0 is allowed. */
635 continue;
636 }
638 for (addr = start; addr < end; addr += PAGE_SIZE)
639 assign_new_domain0_page(d, addr);
640 }
641 }
642 // Map low-memory holes & unmapped MMIO for legacy drivers
643 for (addr = 0; addr < ONE_MB; addr += PAGE_SIZE) {
644 if (domain_page_mapped(d, addr))
645 continue;
647 if (efi_mmio(addr, PAGE_SIZE))
648 assign_domain_mmio_page(d, addr, PAGE_SIZE);
649 }
650 return num_mds;
651 }
653 static void
654 dom_fw_init(struct domain *d,
655 struct ia64_boot_param *bp,
656 struct fw_tables *tables,
657 unsigned long hypercalls_imva,
658 unsigned long maxmem)
659 {
660 efi_memory_desc_t *md;
661 unsigned long pfn;
662 unsigned char checksum;
663 char *cp;
664 int num_mds, i;
666 memset(tables, 0, sizeof(struct fw_tables));
668 /* Initialise for EFI_SET_VIRTUAL_ADDRESS_MAP emulation */
669 d->arch.efi_runtime = &tables->efi_runtime;
670 d->arch.fpswa_inf = &tables->fpswa_inf;
671 d->arch.sal_data = &tables->sal_data;
673 /* EFI systab. */
674 tables->efi_systab.hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
675 tables->efi_systab.hdr.revision = EFI_SYSTEM_TABLE_REVISION;
676 tables->efi_systab.hdr.headersize = sizeof(tables->efi_systab.hdr);
678 memcpy(tables->fw_vendor,FW_VENDOR,sizeof(FW_VENDOR));
679 tables->efi_systab.fw_vendor = FW_FIELD_MPA(fw_vendor);
680 tables->efi_systab.fw_revision = 1;
681 tables->efi_systab.runtime = (void *)FW_FIELD_MPA(efi_runtime);
682 tables->efi_systab.nr_tables = NUM_EFI_SYS_TABLES;
683 tables->efi_systab.tables = FW_FIELD_MPA(efi_tables);
685 /* EFI runtime. */
686 tables->efi_runtime.hdr.signature = EFI_RUNTIME_SERVICES_SIGNATURE;
687 tables->efi_runtime.hdr.revision = EFI_RUNTIME_SERVICES_REVISION;
688 tables->efi_runtime.hdr.headersize = sizeof(tables->efi_runtime.hdr);
690 pfn = 0;
691 EFI_HYPERCALL_PATCH(get_time,EFI_GET_TIME);
692 EFI_HYPERCALL_PATCH(set_time,EFI_SET_TIME);
693 EFI_HYPERCALL_PATCH(get_wakeup_time,EFI_GET_WAKEUP_TIME);
694 EFI_HYPERCALL_PATCH(set_wakeup_time,EFI_SET_WAKEUP_TIME);
695 EFI_HYPERCALL_PATCH(set_virtual_address_map,
696 EFI_SET_VIRTUAL_ADDRESS_MAP);
697 EFI_HYPERCALL_PATCH(get_variable,EFI_GET_VARIABLE);
698 EFI_HYPERCALL_PATCH(get_next_variable,EFI_GET_NEXT_VARIABLE);
699 EFI_HYPERCALL_PATCH(set_variable,EFI_SET_VARIABLE);
700 EFI_HYPERCALL_PATCH(get_next_high_mono_count,
701 EFI_GET_NEXT_HIGH_MONO_COUNT);
702 EFI_HYPERCALL_PATCH(reset_system,EFI_RESET_SYSTEM);
704 /* System tables. */
705 tables->efi_tables[0].guid = SAL_SYSTEM_TABLE_GUID;
706 tables->efi_tables[0].table = FW_FIELD_MPA(sal_systab);
707 for (i = 1; i < NUM_EFI_SYS_TABLES; i++) {
708 tables->efi_tables[i].guid = NULL_GUID;
709 tables->efi_tables[i].table = 0;
710 }
711 i = 1;
712 if (d == dom0) {
713 /* Write messages to the console. */
714 touch_acpi_table();
716 printk("Domain0 EFI passthrough:");
717 if (efi.mps) {
718 tables->efi_tables[i].guid = MPS_TABLE_GUID;
719 tables->efi_tables[i].table = __pa(efi.mps);
720 printk(" MPS=0x%lx",tables->efi_tables[i].table);
721 i++;
722 }
724 if (efi.acpi20) {
725 tables->efi_tables[i].guid = ACPI_20_TABLE_GUID;
726 tables->efi_tables[i].table = __pa(efi.acpi20);
727 printk(" ACPI 2.0=0x%lx",tables->efi_tables[i].table);
728 i++;
729 }
730 if (efi.acpi) {
731 tables->efi_tables[i].guid = ACPI_TABLE_GUID;
732 tables->efi_tables[i].table = __pa(efi.acpi);
733 printk(" ACPI=0x%lx",tables->efi_tables[i].table);
734 i++;
735 }
736 if (efi.smbios) {
737 tables->efi_tables[i].guid = SMBIOS_TABLE_GUID;
738 tables->efi_tables[i].table = __pa(efi.smbios);
739 printk(" SMBIOS=0x%lx",tables->efi_tables[i].table);
740 i++;
741 }
742 if (efi.hcdp) {
743 tables->efi_tables[i].guid = HCDP_TABLE_GUID;
744 tables->efi_tables[i].table = __pa(efi.hcdp);
745 printk(" HCDP=0x%lx",tables->efi_tables[i].table);
746 i++;
747 }
748 printk("\n");
749 } else {
750 printk("DomainU EFI build up:");
752 tables->efi_tables[i].guid = ACPI_20_TABLE_GUID;
753 tables->efi_tables[i].table = FW_ACPI_BASE_PADDR;
754 printk(" ACPI 2.0=0x%lx",tables->efi_tables[i].table);
755 i++;
756 printk("\n");
757 }
759 /* fill in the SAL system table: */
760 memcpy(tables->sal_systab.signature, "SST_", 4);
761 tables->sal_systab.size = sizeof(tables->sal_systab);
762 tables->sal_systab.sal_rev_minor = 1;
763 tables->sal_systab.sal_rev_major = 0;
764 tables->sal_systab.entry_count = 2;
766 strlcpy((char *)tables->sal_systab.oem_id, "Xen/ia64",
767 sizeof(tables->sal_systab.oem_id));
768 strlcpy((char *)tables->sal_systab.product_id, "Xen/ia64",
769 sizeof(tables->sal_systab.product_id));
771 /* PAL entry point: */
772 tables->sal_ed.type = SAL_DESC_ENTRY_POINT;
773 tables->sal_ed.pal_proc = FW_HYPERCALL_PAL_CALL_PADDR;
774 dom_fw_pal_hypercall_patch(d, tables->sal_ed.pal_proc,
775 hypercalls_imva);
776 /* SAL entry point. */
777 tables->sal_ed.sal_proc = FW_HYPERCALL_SAL_CALL_PADDR;
778 dom_fw_hypercall_patch(d, tables->sal_ed.sal_proc,
779 FW_HYPERCALL_SAL_CALL, 1, hypercalls_imva);
780 tables->sal_ed.gp = 0; /* will be ignored */
782 /* Fill an AP wakeup descriptor. */
783 tables->sal_wakeup.type = SAL_DESC_AP_WAKEUP;
784 tables->sal_wakeup.mechanism = IA64_SAL_AP_EXTERNAL_INT;
785 tables->sal_wakeup.vector = XEN_SAL_BOOT_RENDEZ_VEC;
787 /* Compute checksum. */
788 checksum = 0;
789 for (cp = (char *)&tables->sal_systab;
790 cp < (char *)&tables->fpswa_inf;
791 ++cp)
792 checksum += *cp;
793 tables->sal_systab.checksum = -checksum;
795 /* SAL return point. */
796 dom_fw_hypercall_patch(d, FW_HYPERCALL_SAL_RETURN_PADDR,
797 FW_HYPERCALL_SAL_RETURN, 0, hypercalls_imva);
799 /* Fill in the FPSWA interface: */
800 if (fpswa_interface) {
801 tables->fpswa_inf.revision = fpswa_interface->revision;
802 dom_fpswa_hypercall_patch(d, hypercalls_imva);
803 tables->fpswa_inf.fpswa =
804 (void *)FW_HYPERCALL_FPSWA_ENTRY_PADDR;
805 }
807 i = 0; /* Used by MAKE_MD */
809 /* hypercall patches live here, masquerade as reserved PAL memory */
810 MAKE_MD(EFI_PAL_CODE,EFI_MEMORY_WB|EFI_MEMORY_RUNTIME,
811 FW_HYPERCALL_BASE_PADDR, FW_HYPERCALL_END_PADDR);
813 /* Create dom0/domu md entry for fw and cpi tables area. */
814 MAKE_MD(EFI_ACPI_MEMORY_NVS, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
815 FW_ACPI_BASE_PADDR, FW_ACPI_END_PADDR);
816 MAKE_MD(EFI_RUNTIME_SERVICES_DATA, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
817 FW_TABLES_BASE_PADDR, FW_TABLES_END_PADDR);
819 if (d != dom0 || running_on_sim) {
820 /* DomU (or hp-ski).
821 Create a continuous memory area. */
822 /* Memory. */
823 MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
824 FW_END_PADDR, maxmem);
825 d->arch.convmem_end = maxmem;
827 /* Create an entry for IO ports. */
828 MAKE_MD(EFI_MEMORY_MAPPED_IO_PORT_SPACE, EFI_MEMORY_UC,
829 IO_PORTS_PADDR, IO_PORTS_PADDR + IO_PORTS_SIZE);
831 num_mds = i;
832 }
833 else {
834 /* Dom0.
835 We must preserve ACPI data from real machine,
836 as well as IO areas. */
837 num_mds = complete_dom0_memmap(d, tables, maxmem, i);
838 }
840 /* Display memmap. */
841 for (i = 0 ; i < num_mds; i++)
842 print_md(&tables->efi_memmap[i]);
844 /* Fill boot_param */
845 bp->efi_systab = FW_FIELD_MPA(efi_systab);
846 bp->efi_memmap = FW_FIELD_MPA(efi_memmap);
847 bp->efi_memmap_size = num_mds * sizeof(efi_memory_desc_t);
848 bp->efi_memdesc_size = sizeof(efi_memory_desc_t);
849 bp->efi_memdesc_version = EFI_MEMDESC_VERSION;
850 bp->command_line = 0;
851 bp->console_info.num_cols = 80;
852 bp->console_info.num_rows = 25;
853 bp->console_info.orig_x = 0;
854 bp->console_info.orig_y = 24;
855 if (fpswa_interface)
856 bp->fpswa = FW_FIELD_MPA(fpswa_inf);
857 }
859 void dom_fw_setup(struct domain *d, unsigned long bp_mpa, unsigned long maxmem)
860 {
861 struct ia64_boot_param *bp;
862 unsigned long imva_tables_base;
863 unsigned long imva_hypercall_base;
865 BUILD_BUG_ON(sizeof(struct fw_tables) >
866 (FW_TABLES_END_PADDR - FW_TABLES_BASE_PADDR));
868 BUILD_BUG_ON(sizeof(struct fake_acpi_tables) >
869 (FW_ACPI_END_PADDR - FW_ACPI_BASE_PADDR));
871 /* Create page for hypercalls. */
872 assign_new_domain_page_if_dom0(d, FW_HYPERCALL_BASE_PADDR);
873 imva_hypercall_base = (unsigned long)domain_mpa_to_imva
874 (d, FW_HYPERCALL_BASE_PADDR);
876 /* Create page for acpi tables. */
877 if (d != dom0) {
878 void *imva;
880 assign_new_domain_page_if_dom0(d, FW_ACPI_BASE_PADDR);
881 imva = domain_mpa_to_imva (d, FW_ACPI_BASE_PADDR);
882 dom_fw_fake_acpi(d, (struct fake_acpi_tables *)imva);
883 }
885 /* Create page for FW tables. */
886 assign_new_domain_page_if_dom0(d, FW_TABLES_BASE_PADDR);
887 imva_tables_base = (unsigned long)domain_mpa_to_imva
888 (d, FW_TABLES_BASE_PADDR);
890 /* Create page for boot_param. */
891 assign_new_domain_page_if_dom0(d, bp_mpa);
892 bp = domain_mpa_to_imva(d, bp_mpa);
894 dom_fw_init(d, bp, (struct fw_tables *)imva_tables_base,
895 imva_hypercall_base, maxmem);
896 }