1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun #include <xen/xen.h>
3*4882a593Smuzhiyun #include <xen/events.h>
4*4882a593Smuzhiyun #include <xen/grant_table.h>
5*4882a593Smuzhiyun #include <xen/hvm.h>
6*4882a593Smuzhiyun #include <xen/interface/vcpu.h>
7*4882a593Smuzhiyun #include <xen/interface/xen.h>
8*4882a593Smuzhiyun #include <xen/interface/memory.h>
9*4882a593Smuzhiyun #include <xen/interface/hvm/params.h>
10*4882a593Smuzhiyun #include <xen/features.h>
11*4882a593Smuzhiyun #include <xen/platform_pci.h>
12*4882a593Smuzhiyun #include <xen/xenbus.h>
13*4882a593Smuzhiyun #include <xen/page.h>
14*4882a593Smuzhiyun #include <xen/interface/sched.h>
15*4882a593Smuzhiyun #include <xen/xen-ops.h>
16*4882a593Smuzhiyun #include <asm/xen/hypervisor.h>
17*4882a593Smuzhiyun #include <asm/xen/hypercall.h>
18*4882a593Smuzhiyun #include <asm/system_misc.h>
19*4882a593Smuzhiyun #include <asm/efi.h>
20*4882a593Smuzhiyun #include <linux/interrupt.h>
21*4882a593Smuzhiyun #include <linux/irqreturn.h>
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/of.h>
24*4882a593Smuzhiyun #include <linux/of_fdt.h>
25*4882a593Smuzhiyun #include <linux/of_irq.h>
26*4882a593Smuzhiyun #include <linux/of_address.h>
27*4882a593Smuzhiyun #include <linux/cpuidle.h>
28*4882a593Smuzhiyun #include <linux/cpufreq.h>
29*4882a593Smuzhiyun #include <linux/cpu.h>
30*4882a593Smuzhiyun #include <linux/console.h>
31*4882a593Smuzhiyun #include <linux/pvclock_gtod.h>
32*4882a593Smuzhiyun #include <linux/reboot.h>
33*4882a593Smuzhiyun #include <linux/time64.h>
34*4882a593Smuzhiyun #include <linux/timekeeping.h>
35*4882a593Smuzhiyun #include <linux/timekeeper_internal.h>
36*4882a593Smuzhiyun #include <linux/acpi.h>
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #include <linux/mm.h>
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static struct start_info _xen_start_info;
41*4882a593Smuzhiyun struct start_info *xen_start_info = &_xen_start_info;
42*4882a593Smuzhiyun EXPORT_SYMBOL(xen_start_info);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun enum xen_domain_type xen_domain_type = XEN_NATIVE;
45*4882a593Smuzhiyun EXPORT_SYMBOL(xen_domain_type);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun struct shared_info xen_dummy_shared_info;
48*4882a593Smuzhiyun struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
51*4882a593Smuzhiyun static struct vcpu_info __percpu *xen_vcpu_info;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* Linux <-> Xen vCPU id mapping */
54*4882a593Smuzhiyun DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
55*4882a593Smuzhiyun EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* These are unused until we support booting "pre-ballooned" */
58*4882a593Smuzhiyun unsigned long xen_released_pages;
59*4882a593Smuzhiyun struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun static __read_mostly unsigned int xen_events_irq;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun uint32_t xen_start_flags;
64*4882a593Smuzhiyun EXPORT_SYMBOL(xen_start_flags);
65*4882a593Smuzhiyun
xen_unmap_domain_gfn_range(struct vm_area_struct * vma,int nr,struct page ** pages)66*4882a593Smuzhiyun int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
67*4882a593Smuzhiyun int nr, struct page **pages)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun return xen_xlate_unmap_gfn_range(vma, nr, pages);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range);
72*4882a593Smuzhiyun
xen_read_wallclock(struct timespec64 * ts)73*4882a593Smuzhiyun static void xen_read_wallclock(struct timespec64 *ts)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun u32 version;
76*4882a593Smuzhiyun struct timespec64 now, ts_monotonic;
77*4882a593Smuzhiyun struct shared_info *s = HYPERVISOR_shared_info;
78*4882a593Smuzhiyun struct pvclock_wall_clock *wall_clock = &(s->wc);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* get wallclock at system boot */
81*4882a593Smuzhiyun do {
82*4882a593Smuzhiyun version = wall_clock->version;
83*4882a593Smuzhiyun rmb(); /* fetch version before time */
84*4882a593Smuzhiyun now.tv_sec = ((uint64_t)wall_clock->sec_hi << 32) | wall_clock->sec;
85*4882a593Smuzhiyun now.tv_nsec = wall_clock->nsec;
86*4882a593Smuzhiyun rmb(); /* fetch time before checking version */
87*4882a593Smuzhiyun } while ((wall_clock->version & 1) || (version != wall_clock->version));
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* time since system boot */
90*4882a593Smuzhiyun ktime_get_ts64(&ts_monotonic);
91*4882a593Smuzhiyun *ts = timespec64_add(now, ts_monotonic);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
xen_pvclock_gtod_notify(struct notifier_block * nb,unsigned long was_set,void * priv)94*4882a593Smuzhiyun static int xen_pvclock_gtod_notify(struct notifier_block *nb,
95*4882a593Smuzhiyun unsigned long was_set, void *priv)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun /* Protected by the calling core code serialization */
98*4882a593Smuzhiyun static struct timespec64 next_sync;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun struct xen_platform_op op;
101*4882a593Smuzhiyun struct timespec64 now, system_time;
102*4882a593Smuzhiyun struct timekeeper *tk = priv;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun now.tv_sec = tk->xtime_sec;
105*4882a593Smuzhiyun now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
106*4882a593Smuzhiyun system_time = timespec64_add(now, tk->wall_to_monotonic);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * We only take the expensive HV call when the clock was set
110*4882a593Smuzhiyun * or when the 11 minutes RTC synchronization time elapsed.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun if (!was_set && timespec64_compare(&now, &next_sync) < 0)
113*4882a593Smuzhiyun return NOTIFY_OK;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun op.cmd = XENPF_settime64;
116*4882a593Smuzhiyun op.u.settime64.mbz = 0;
117*4882a593Smuzhiyun op.u.settime64.secs = now.tv_sec;
118*4882a593Smuzhiyun op.u.settime64.nsecs = now.tv_nsec;
119*4882a593Smuzhiyun op.u.settime64.system_time = timespec64_to_ns(&system_time);
120*4882a593Smuzhiyun (void)HYPERVISOR_platform_op(&op);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun * Move the next drift compensation time 11 minutes
124*4882a593Smuzhiyun * ahead. That's emulating the sync_cmos_clock() update for
125*4882a593Smuzhiyun * the hardware RTC.
126*4882a593Smuzhiyun */
127*4882a593Smuzhiyun next_sync = now;
128*4882a593Smuzhiyun next_sync.tv_sec += 11 * 60;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun return NOTIFY_OK;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun static struct notifier_block xen_pvclock_gtod_notifier = {
134*4882a593Smuzhiyun .notifier_call = xen_pvclock_gtod_notify,
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun
xen_starting_cpu(unsigned int cpu)137*4882a593Smuzhiyun static int xen_starting_cpu(unsigned int cpu)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun struct vcpu_register_vcpu_info info;
140*4882a593Smuzhiyun struct vcpu_info *vcpup;
141*4882a593Smuzhiyun int err;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * VCPUOP_register_vcpu_info cannot be called twice for the same
145*4882a593Smuzhiyun * vcpu, so if vcpu_info is already registered, just get out. This
146*4882a593Smuzhiyun * can happen with cpu-hotplug.
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun if (per_cpu(xen_vcpu, cpu) != NULL)
149*4882a593Smuzhiyun goto after_register_vcpu_info;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun pr_info("Xen: initializing cpu%d\n", cpu);
152*4882a593Smuzhiyun vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun info.mfn = percpu_to_gfn(vcpup);
155*4882a593Smuzhiyun info.offset = xen_offset_in_page(vcpup);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu),
158*4882a593Smuzhiyun &info);
159*4882a593Smuzhiyun BUG_ON(err);
160*4882a593Smuzhiyun per_cpu(xen_vcpu, cpu) = vcpup;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun if (!xen_kernel_unmapped_at_usr())
163*4882a593Smuzhiyun xen_setup_runstate_info(cpu);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun after_register_vcpu_info:
166*4882a593Smuzhiyun enable_percpu_irq(xen_events_irq, 0);
167*4882a593Smuzhiyun return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
xen_dying_cpu(unsigned int cpu)170*4882a593Smuzhiyun static int xen_dying_cpu(unsigned int cpu)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun disable_percpu_irq(xen_events_irq);
173*4882a593Smuzhiyun return 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
xen_reboot(int reason)176*4882a593Smuzhiyun void xen_reboot(int reason)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun struct sched_shutdown r = { .reason = reason };
179*4882a593Smuzhiyun int rc;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
182*4882a593Smuzhiyun BUG_ON(rc);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
xen_restart(struct notifier_block * nb,unsigned long action,void * data)185*4882a593Smuzhiyun static int xen_restart(struct notifier_block *nb, unsigned long action,
186*4882a593Smuzhiyun void *data)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun xen_reboot(SHUTDOWN_reboot);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun return NOTIFY_DONE;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun static struct notifier_block xen_restart_nb = {
194*4882a593Smuzhiyun .notifier_call = xen_restart,
195*4882a593Smuzhiyun .priority = 192,
196*4882a593Smuzhiyun };
197*4882a593Smuzhiyun
xen_power_off(void)198*4882a593Smuzhiyun static void xen_power_off(void)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun xen_reboot(SHUTDOWN_poweroff);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
xen_arm_callback(int irq,void * arg)203*4882a593Smuzhiyun static irqreturn_t xen_arm_callback(int irq, void *arg)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun xen_hvm_evtchn_do_upcall();
206*4882a593Smuzhiyun return IRQ_HANDLED;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun static __initdata struct {
210*4882a593Smuzhiyun const char *compat;
211*4882a593Smuzhiyun const char *prefix;
212*4882a593Smuzhiyun const char *version;
213*4882a593Smuzhiyun bool found;
214*4882a593Smuzhiyun } hyper_node = {"xen,xen", "xen,xen-", NULL, false};
215*4882a593Smuzhiyun
fdt_find_hyper_node(unsigned long node,const char * uname,int depth,void * data)216*4882a593Smuzhiyun static int __init fdt_find_hyper_node(unsigned long node, const char *uname,
217*4882a593Smuzhiyun int depth, void *data)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun const void *s = NULL;
220*4882a593Smuzhiyun int len;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun if (depth != 1 || strcmp(uname, "hypervisor") != 0)
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun if (of_flat_dt_is_compatible(node, hyper_node.compat))
226*4882a593Smuzhiyun hyper_node.found = true;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun s = of_get_flat_dt_prop(node, "compatible", &len);
229*4882a593Smuzhiyun if (strlen(hyper_node.prefix) + 3 < len &&
230*4882a593Smuzhiyun !strncmp(hyper_node.prefix, s, strlen(hyper_node.prefix)))
231*4882a593Smuzhiyun hyper_node.version = s + strlen(hyper_node.prefix);
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /*
234*4882a593Smuzhiyun * Check if Xen supports EFI by checking whether there is the
235*4882a593Smuzhiyun * "/hypervisor/uefi" node in DT. If so, runtime services are available
236*4882a593Smuzhiyun * through proxy functions (e.g. in case of Xen dom0 EFI implementation
237*4882a593Smuzhiyun * they call special hypercall which executes relevant EFI functions)
238*4882a593Smuzhiyun * and that is why they are always enabled.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_XEN_EFI)) {
241*4882a593Smuzhiyun if ((of_get_flat_dt_subnode_by_name(node, "uefi") > 0) &&
242*4882a593Smuzhiyun !efi_runtime_disabled())
243*4882a593Smuzhiyun set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun return 0;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /*
250*4882a593Smuzhiyun * see Documentation/devicetree/bindings/arm/xen.txt for the
251*4882a593Smuzhiyun * documentation of the Xen Device Tree format.
252*4882a593Smuzhiyun */
xen_early_init(void)253*4882a593Smuzhiyun void __init xen_early_init(void)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun of_scan_flat_dt(fdt_find_hyper_node, NULL);
256*4882a593Smuzhiyun if (!hyper_node.found) {
257*4882a593Smuzhiyun pr_debug("No Xen support\n");
258*4882a593Smuzhiyun return;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (hyper_node.version == NULL) {
262*4882a593Smuzhiyun pr_debug("Xen version not found\n");
263*4882a593Smuzhiyun return;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun pr_info("Xen %s support found\n", hyper_node.version);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun xen_domain_type = XEN_HVM_DOMAIN;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun xen_setup_features();
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun if (xen_feature(XENFEAT_dom0))
273*4882a593Smuzhiyun xen_start_flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun if (!console_set_on_cmdline && !xen_initial_domain())
276*4882a593Smuzhiyun add_preferred_console("hvc", 0, NULL);
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
xen_acpi_guest_init(void)279*4882a593Smuzhiyun static void __init xen_acpi_guest_init(void)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun #ifdef CONFIG_ACPI
282*4882a593Smuzhiyun struct xen_hvm_param a;
283*4882a593Smuzhiyun int interrupt, trigger, polarity;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun a.domid = DOMID_SELF;
286*4882a593Smuzhiyun a.index = HVM_PARAM_CALLBACK_IRQ;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun if (HYPERVISOR_hvm_op(HVMOP_get_param, &a)
289*4882a593Smuzhiyun || (a.value >> 56) != HVM_PARAM_CALLBACK_TYPE_PPI) {
290*4882a593Smuzhiyun xen_events_irq = 0;
291*4882a593Smuzhiyun return;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun interrupt = a.value & 0xff;
295*4882a593Smuzhiyun trigger = ((a.value >> 8) & 0x1) ? ACPI_EDGE_SENSITIVE
296*4882a593Smuzhiyun : ACPI_LEVEL_SENSITIVE;
297*4882a593Smuzhiyun polarity = ((a.value >> 8) & 0x2) ? ACPI_ACTIVE_LOW
298*4882a593Smuzhiyun : ACPI_ACTIVE_HIGH;
299*4882a593Smuzhiyun xen_events_irq = acpi_register_gsi(NULL, interrupt, trigger, polarity);
300*4882a593Smuzhiyun #endif
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
xen_dt_guest_init(void)303*4882a593Smuzhiyun static void __init xen_dt_guest_init(void)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun struct device_node *xen_node;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun xen_node = of_find_compatible_node(NULL, NULL, "xen,xen");
308*4882a593Smuzhiyun if (!xen_node) {
309*4882a593Smuzhiyun pr_err("Xen support was detected before, but it has disappeared\n");
310*4882a593Smuzhiyun return;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun xen_events_irq = irq_of_parse_and_map(xen_node, 0);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
xen_guest_init(void)316*4882a593Smuzhiyun static int __init xen_guest_init(void)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun struct xen_add_to_physmap xatp;
319*4882a593Smuzhiyun struct shared_info *shared_info_page = NULL;
320*4882a593Smuzhiyun int cpu;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun if (!xen_domain())
323*4882a593Smuzhiyun return 0;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (!acpi_disabled)
326*4882a593Smuzhiyun xen_acpi_guest_init();
327*4882a593Smuzhiyun else
328*4882a593Smuzhiyun xen_dt_guest_init();
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun if (!xen_events_irq) {
331*4882a593Smuzhiyun pr_err("Xen event channel interrupt not found\n");
332*4882a593Smuzhiyun return -ENODEV;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun * The fdt parsing codes have set EFI_RUNTIME_SERVICES if Xen EFI
337*4882a593Smuzhiyun * parameters are found. Force enable runtime services.
338*4882a593Smuzhiyun */
339*4882a593Smuzhiyun if (efi_enabled(EFI_RUNTIME_SERVICES))
340*4882a593Smuzhiyun xen_efi_runtime_setup();
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun shared_info_page = (struct shared_info *)get_zeroed_page(GFP_KERNEL);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun if (!shared_info_page) {
345*4882a593Smuzhiyun pr_err("not enough memory\n");
346*4882a593Smuzhiyun return -ENOMEM;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun xatp.domid = DOMID_SELF;
349*4882a593Smuzhiyun xatp.idx = 0;
350*4882a593Smuzhiyun xatp.space = XENMAPSPACE_shared_info;
351*4882a593Smuzhiyun xatp.gpfn = virt_to_gfn(shared_info_page);
352*4882a593Smuzhiyun if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
353*4882a593Smuzhiyun BUG();
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
358*4882a593Smuzhiyun * page, we use it in the event channel upcall and in some pvclock
359*4882a593Smuzhiyun * related functions.
360*4882a593Smuzhiyun * The shared info contains exactly 1 CPU (the boot CPU). The guest
361*4882a593Smuzhiyun * is required to use VCPUOP_register_vcpu_info to place vcpu info
362*4882a593Smuzhiyun * for secondary CPUs as they are brought up.
363*4882a593Smuzhiyun * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
364*4882a593Smuzhiyun */
365*4882a593Smuzhiyun xen_vcpu_info = alloc_percpu(struct vcpu_info);
366*4882a593Smuzhiyun if (xen_vcpu_info == NULL)
367*4882a593Smuzhiyun return -ENOMEM;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /* Direct vCPU id mapping for ARM guests. */
370*4882a593Smuzhiyun for_each_possible_cpu(cpu)
371*4882a593Smuzhiyun per_cpu(xen_vcpu_id, cpu) = cpu;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun xen_auto_xlat_grant_frames.count = gnttab_max_grant_frames();
374*4882a593Smuzhiyun if (xen_xlate_map_ballooned_pages(&xen_auto_xlat_grant_frames.pfn,
375*4882a593Smuzhiyun &xen_auto_xlat_grant_frames.vaddr,
376*4882a593Smuzhiyun xen_auto_xlat_grant_frames.count)) {
377*4882a593Smuzhiyun free_percpu(xen_vcpu_info);
378*4882a593Smuzhiyun return -ENOMEM;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun gnttab_init();
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun /*
383*4882a593Smuzhiyun * Making sure board specific code will not set up ops for
384*4882a593Smuzhiyun * cpu idle and cpu freq.
385*4882a593Smuzhiyun */
386*4882a593Smuzhiyun disable_cpuidle();
387*4882a593Smuzhiyun disable_cpufreq();
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun xen_init_IRQ();
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun if (request_percpu_irq(xen_events_irq, xen_arm_callback,
392*4882a593Smuzhiyun "events", &xen_vcpu)) {
393*4882a593Smuzhiyun pr_err("Error request IRQ %d\n", xen_events_irq);
394*4882a593Smuzhiyun return -EINVAL;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun if (!xen_kernel_unmapped_at_usr())
398*4882a593Smuzhiyun xen_time_setup_guest();
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun if (xen_initial_domain())
401*4882a593Smuzhiyun pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun return cpuhp_setup_state(CPUHP_AP_ARM_XEN_STARTING,
404*4882a593Smuzhiyun "arm/xen:starting", xen_starting_cpu,
405*4882a593Smuzhiyun xen_dying_cpu);
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun early_initcall(xen_guest_init);
408*4882a593Smuzhiyun
xen_pm_init(void)409*4882a593Smuzhiyun static int __init xen_pm_init(void)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun if (!xen_domain())
412*4882a593Smuzhiyun return -ENODEV;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun pm_power_off = xen_power_off;
415*4882a593Smuzhiyun register_restart_handler(&xen_restart_nb);
416*4882a593Smuzhiyun if (!xen_initial_domain()) {
417*4882a593Smuzhiyun struct timespec64 ts;
418*4882a593Smuzhiyun xen_read_wallclock(&ts);
419*4882a593Smuzhiyun do_settimeofday64(&ts);
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun return 0;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun late_initcall(xen_pm_init);
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /* empty stubs */
xen_arch_pre_suspend(void)428*4882a593Smuzhiyun void xen_arch_pre_suspend(void) { }
xen_arch_post_suspend(int suspend_cancelled)429*4882a593Smuzhiyun void xen_arch_post_suspend(int suspend_cancelled) { }
xen_timer_resume(void)430*4882a593Smuzhiyun void xen_timer_resume(void) { }
xen_arch_resume(void)431*4882a593Smuzhiyun void xen_arch_resume(void) { }
xen_arch_suspend(void)432*4882a593Smuzhiyun void xen_arch_suspend(void) { }
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun /* In the hypercall.S file. */
436*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
437*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
438*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
439*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
440*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
441*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
442*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
443*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
444*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
445*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
446*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_platform_op_raw);
447*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
448*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_vm_assist);
449*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(HYPERVISOR_dm_op);
450*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(privcmd_call);
451