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