1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 #include <linux/export.h>
6 #include <xen/hvc-console.h>
9 #include <asm/bootparam.h>
10 #include <asm/io_apic.h>
11 #include <asm/hypervisor.h>
12 #include <asm/e820/api.h>
13 #include <asm/setup.h>
16 #include <asm/xen/interface.h>
17 #include <asm/xen/hypercall.h>
19 #include <xen/interface/memory.h>
26 * The variable xen_pvh needs to live in a data segment since it is used
27 * after startup_{32|64} is invoked, which will clear the .bss segment.
29 bool __ro_after_init xen_pvh
;
30 EXPORT_SYMBOL_GPL(xen_pvh
);
32 #ifdef CONFIG_XEN_DOM0
33 int xen_pvh_setup_gsi(int gsi
, int trigger
, int polarity
)
36 struct physdev_setup_gsi setup_gsi
;
39 setup_gsi
.triggering
= (trigger
== ACPI_EDGE_SENSITIVE
? 0 : 1);
40 setup_gsi
.polarity
= (polarity
== ACPI_ACTIVE_HIGH
? 0 : 1);
42 ret
= HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi
, &setup_gsi
);
44 xen_raw_printk("Already setup the GSI :%d\n", gsi
);
47 xen_raw_printk("Fail to setup GSI (%d)!\n", gsi
);
51 EXPORT_SYMBOL_GPL(xen_pvh_setup_gsi
);
55 * Reserve e820 UNUSABLE regions to inflate the memory balloon.
57 * On PVH dom0 the host memory map is used, RAM regions available to dom0 are
58 * located as the same place as in the native memory map, but since dom0 gets
59 * less memory than the total amount of host RAM the ranges that can't be
60 * populated are converted from RAM -> UNUSABLE. Use such regions (up to the
61 * ratio signaled in EXTRA_MEM_RATIO) in order to inflate the balloon driver at
62 * boot. Doing so prevents the guest (even if just temporary) from using holes
63 * in the memory map in order to map grants or foreign addresses, and
64 * hopefully limits the risk of a clash with a device MMIO region. Ideally the
65 * hypervisor should notify us which memory ranges are suitable for creating
66 * foreign mappings, but that's not yet implemented.
68 static void __init
pvh_reserve_extra_memory(void)
70 struct boot_params
*bootp
= &boot_params
;
71 unsigned int i
, ram_pages
= 0, extra_pages
;
73 for (i
= 0; i
< bootp
->e820_entries
; i
++) {
74 struct boot_e820_entry
*e
= &bootp
->e820_table
[i
];
76 if (e
->type
!= E820_TYPE_RAM
)
78 ram_pages
+= PFN_DOWN(e
->addr
+ e
->size
) - PFN_UP(e
->addr
);
81 /* Max amount of extra memory. */
82 extra_pages
= EXTRA_MEM_RATIO
* ram_pages
;
85 * Convert UNUSABLE ranges to RAM and reserve them for foreign mapping
88 for (i
= 0; i
< bootp
->e820_entries
&& extra_pages
; i
++) {
89 struct boot_e820_entry
*e
= &bootp
->e820_table
[i
];
92 if (e
->type
!= E820_TYPE_UNUSABLE
)
95 pages
= min(extra_pages
,
96 PFN_DOWN(e
->addr
+ e
->size
) - PFN_UP(e
->addr
));
98 if (pages
!= (PFN_DOWN(e
->addr
+ e
->size
) - PFN_UP(e
->addr
))) {
99 struct boot_e820_entry
*next
;
101 if (bootp
->e820_entries
==
102 ARRAY_SIZE(bootp
->e820_table
))
103 /* No space left to split - skip region. */
109 (bootp
->e820_entries
- i
) * sizeof(*e
));
110 bootp
->e820_entries
++;
111 next
->addr
= PAGE_ALIGN(e
->addr
) + PFN_PHYS(pages
);
112 e
->size
= next
->addr
- e
->addr
;
113 next
->size
-= e
->size
;
115 e
->type
= E820_TYPE_RAM
;
116 extra_pages
-= pages
;
118 xen_add_extra_mem(PFN_UP(e
->addr
), pages
);
122 static void __init
pvh_arch_setup(void)
124 pvh_reserve_extra_memory();
126 if (xen_initial_domain())
127 xen_add_preferred_consoles();
130 void __init
xen_pvh_init(struct boot_params
*boot_params
)
136 xen_domain_type
= XEN_HVM_DOMAIN
;
137 xen_start_flags
= pvh_start_info
.flags
;
139 msr
= cpuid_ebx(xen_cpuid_base() + 2);
140 pfn
= __pa(hypercall_page
);
141 wrmsr_safe(msr
, (u32
)pfn
, (u32
)(pfn
>> 32));
143 x86_init
.oem
.arch_setup
= pvh_arch_setup
;
144 x86_init
.oem
.banner
= xen_banner
;
146 xen_efi_init(boot_params
);
148 if (xen_initial_domain()) {
149 struct xen_platform_op op
= {
150 .cmd
= XENPF_get_dom0_console
,
152 int ret
= HYPERVISOR_platform_op(&op
);
155 xen_init_vga(&op
.u
.dom0_console
,
156 min(ret
* sizeof(char),
157 sizeof(op
.u
.dom0_console
)),
158 &boot_params
->screen_info
);
162 void __init
mem_map_via_hcall(struct boot_params
*boot_params_p
)
164 struct xen_memory_map memmap
;
167 memmap
.nr_entries
= ARRAY_SIZE(boot_params_p
->e820_table
);
168 set_xen_guest_handle(memmap
.buffer
, boot_params_p
->e820_table
);
169 rc
= HYPERVISOR_memory_op(XENMEM_memory_map
, &memmap
);
171 xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc
);
174 boot_params_p
->e820_entries
= memmap
.nr_entries
;