Merge tag 'for-linus-2021-01-24' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / arch / arm / xen / enlighten.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
4c071ee5 2#include <xen/xen.h>
0ec53ecf 3#include <xen/events.h>
b3b52fd8
SS
4#include <xen/grant_table.h>
5#include <xen/hvm.h>
9a9ab3cc 6#include <xen/interface/vcpu.h>
4c071ee5
SS
7#include <xen/interface/xen.h>
8#include <xen/interface/memory.h>
b3b52fd8 9#include <xen/interface/hvm/params.h>
ef61ee0d 10#include <xen/features.h>
4c071ee5 11#include <xen/platform_pci.h>
b3b52fd8 12#include <xen/xenbus.h>
c61ba729 13#include <xen/page.h>
6abb749e 14#include <xen/interface/sched.h>
f832da06 15#include <xen/xen-ops.h>
4c071ee5
SS
16#include <asm/xen/hypervisor.h>
17#include <asm/xen/hypercall.h>
6abb749e 18#include <asm/system_misc.h>
0cac5c30 19#include <asm/efi.h>
0ec53ecf
SS
20#include <linux/interrupt.h>
21#include <linux/irqreturn.h>
4c071ee5 22#include <linux/module.h>
2e01f166 23#include <linux/of.h>
9b08aaa3 24#include <linux/of_fdt.h>
2e01f166
SS
25#include <linux/of_irq.h>
26#include <linux/of_address.h>
e1a9c16b
JG
27#include <linux/cpuidle.h>
28#include <linux/cpufreq.h>
8b271d57 29#include <linux/cpu.h>
f1dddd11 30#include <linux/console.h>
7d5f6f81
SS
31#include <linux/pvclock_gtod.h>
32#include <linux/time64.h>
e709fba1 33#include <linux/timekeeping.h>
7d5f6f81 34#include <linux/timekeeper_internal.h>
d22cbe65 35#include <linux/acpi.h>
4c071ee5 36
f832da06
IC
37#include <linux/mm.h>
38
74f4c438 39static struct start_info _xen_start_info;
4c071ee5 40struct start_info *xen_start_info = &_xen_start_info;
35c8ab4c 41EXPORT_SYMBOL(xen_start_info);
4c071ee5
SS
42
43enum xen_domain_type xen_domain_type = XEN_NATIVE;
35c8ab4c 44EXPORT_SYMBOL(xen_domain_type);
4c071ee5
SS
45
46struct shared_info xen_dummy_shared_info;
47struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
48
49DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
9a9ab3cc 50static struct vcpu_info __percpu *xen_vcpu_info;
4c071ee5 51
88e957d6 52/* Linux <-> Xen vCPU id mapping */
55467dea 53DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
88e957d6
VK
54EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
55
c61ba729
IC
56/* These are unused until we support booting "pre-ballooned" */
57unsigned long xen_released_pages;
58struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
59
81e863c3 60static __read_mostly unsigned int xen_events_irq;
0ec53ecf 61
1fe83888
RPM
62uint32_t xen_start_flags;
63EXPORT_SYMBOL(xen_start_flags);
64
a13d7201 65int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
f832da06
IC
66 int nr, struct page **pages)
67{
628c28ee 68 return xen_xlate_unmap_gfn_range(vma, nr, pages);
f832da06 69}
a13d7201 70EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range);
f832da06 71
e709fba1
SS
72static 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
7d5f6f81
SS
93static 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
132static struct notifier_block xen_pvclock_gtod_notifier = {
133 .notifier_call = xen_pvclock_gtod_notify,
134};
135
4761adb6 136static int xen_starting_cpu(unsigned int cpu)
9a9ab3cc
SS
137{
138 struct vcpu_register_vcpu_info info;
139 struct vcpu_info *vcpup;
140 int err;
141
cb9644bf
SS
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
9a9ab3cc
SS
150 pr_info("Xen: initializing cpu%d\n", cpu);
151 vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
152
5a067711 153 info.mfn = percpu_to_gfn(vcpup);
250c9af3 154 info.offset = xen_offset_in_page(vcpup);
9a9ab3cc 155
ad5475f9
VK
156 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu),
157 &info);
d7266d78
SS
158 BUG_ON(err);
159 per_cpu(xen_vcpu, cpu) = vcpup;
160
f88af722
SS
161 if (!xen_kernel_unmapped_at_usr())
162 xen_setup_runstate_info(cpu);
34e38523 163
cb9644bf 164after_register_vcpu_info:
3cc8e40e 165 enable_percpu_irq(xen_events_irq, 0);
4761adb6
RC
166 return 0;
167}
168
169static int xen_dying_cpu(unsigned int cpu)
170{
171 disable_percpu_irq(xen_events_irq);
172 return 0;
9a9ab3cc
SS
173}
174
fa12a870 175void xen_reboot(int reason)
6abb749e 176{
fa12a870 177 struct sched_shutdown r = { .reason = reason };
6abb749e 178 int rc;
fa12a870 179
6abb749e 180 rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
a91c7775 181 BUG_ON(rc);
6abb749e
SS
182}
183
fa12a870
JG
184static void xen_restart(enum reboot_mode reboot_mode, const char *cmd)
185{
186 xen_reboot(SHUTDOWN_reboot);
187}
188
189
6abb749e
SS
190static void xen_power_off(void)
191{
fa12a870 192 xen_reboot(SHUTDOWN_poweroff);
6abb749e
SS
193}
194
8b271d57
JG
195static irqreturn_t xen_arm_callback(int irq, void *arg)
196{
197 xen_hvm_evtchn_do_upcall();
198 return IRQ_HANDLED;
199}
200
9b08aaa3
SZ
201static __initdata struct {
202 const char *compat;
203 const char *prefix;
204 const char *version;
205 bool found;
206} hyper_node = {"xen,xen", "xen,xen-", NULL, false};
207
208static int __init fdt_find_hyper_node(unsigned long node, const char *uname,
209 int depth, void *data)
210{
211 const void *s = NULL;
212 int len;
213
214 if (depth != 1 || strcmp(uname, "hypervisor") != 0)
215 return 0;
216
217 if (of_flat_dt_is_compatible(node, hyper_node.compat))
218 hyper_node.found = true;
219
220 s = of_get_flat_dt_prop(node, "compatible", &len);
221 if (strlen(hyper_node.prefix) + 3 < len &&
222 !strncmp(hyper_node.prefix, s, strlen(hyper_node.prefix)))
223 hyper_node.version = s + strlen(hyper_node.prefix);
224
0cac5c30
SZ
225 /*
226 * Check if Xen supports EFI by checking whether there is the
227 * "/hypervisor/uefi" node in DT. If so, runtime services are available
228 * through proxy functions (e.g. in case of Xen dom0 EFI implementation
229 * they call special hypercall which executes relevant EFI functions)
230 * and that is why they are always enabled.
231 */
232 if (IS_ENABLED(CONFIG_XEN_EFI)) {
233 if ((of_get_flat_dt_subnode_by_name(node, "uefi") > 0) &&
234 !efi_runtime_disabled())
235 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
236 }
237
9b08aaa3
SZ
238 return 0;
239}
240
2e01f166
SS
241/*
242 * see Documentation/devicetree/bindings/arm/xen.txt for the
243 * documentation of the Xen Device Tree format.
244 */
5882bfef 245void __init xen_early_init(void)
2e01f166 246{
9b08aaa3
SZ
247 of_scan_flat_dt(fdt_find_hyper_node, NULL);
248 if (!hyper_node.found) {
2e01f166 249 pr_debug("No Xen support\n");
5882bfef 250 return;
2e01f166 251 }
9b08aaa3
SZ
252
253 if (hyper_node.version == NULL) {
2e01f166 254 pr_debug("Xen version not found\n");
5882bfef 255 return;
81e863c3
JG
256 }
257
9b08aaa3 258 pr_info("Xen %s support found\n", hyper_node.version);
8b271d57 259
2e01f166
SS
260 xen_domain_type = XEN_HVM_DOMAIN;
261
ef61ee0d 262 xen_setup_features();
5ebc77de 263
ef61ee0d 264 if (xen_feature(XENFEAT_dom0))
1fe83888 265 xen_start_flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
f1dddd11
AB
266
267 if (!console_set_on_cmdline && !xen_initial_domain())
268 add_preferred_console("hvc", 0, NULL);
5882bfef
SS
269}
270
d22cbe65
SZ
271static void __init xen_acpi_guest_init(void)
272{
273#ifdef CONFIG_ACPI
274 struct xen_hvm_param a;
275 int interrupt, trigger, polarity;
276
277 a.domid = DOMID_SELF;
278 a.index = HVM_PARAM_CALLBACK_IRQ;
279
280 if (HYPERVISOR_hvm_op(HVMOP_get_param, &a)
281 || (a.value >> 56) != HVM_PARAM_CALLBACK_TYPE_PPI) {
282 xen_events_irq = 0;
283 return;
284 }
285
286 interrupt = a.value & 0xff;
287 trigger = ((a.value >> 8) & 0x1) ? ACPI_EDGE_SENSITIVE
288 : ACPI_LEVEL_SENSITIVE;
289 polarity = ((a.value >> 8) & 0x2) ? ACPI_ACTIVE_LOW
290 : ACPI_ACTIVE_HIGH;
291 xen_events_irq = acpi_register_gsi(NULL, interrupt, trigger, polarity);
292#endif
293}
294
295static void __init xen_dt_guest_init(void)
296{
9b08aaa3
SZ
297 struct device_node *xen_node;
298
299 xen_node = of_find_compatible_node(NULL, NULL, "xen,xen");
300 if (!xen_node) {
301 pr_err("Xen support was detected before, but it has disappeared\n");
302 return;
303 }
304
d22cbe65
SZ
305 xen_events_irq = irq_of_parse_and_map(xen_node, 0);
306}
307
5882bfef
SS
308static int __init xen_guest_init(void)
309{
310 struct xen_add_to_physmap xatp;
311 struct shared_info *shared_info_page = NULL;
de75abbe 312 int cpu;
5882bfef
SS
313
314 if (!xen_domain())
315 return 0;
316
d22cbe65
SZ
317 if (!acpi_disabled)
318 xen_acpi_guest_init();
319 else
320 xen_dt_guest_init();
321
5882bfef
SS
322 if (!xen_events_irq) {
323 pr_err("Xen event channel interrupt not found\n");
324 return -ENODEV;
325 }
326
0cac5c30
SZ
327 /*
328 * The fdt parsing codes have set EFI_RUNTIME_SERVICES if Xen EFI
329 * parameters are found. Force enable runtime services.
330 */
331 if (efi_enabled(EFI_RUNTIME_SERVICES))
332 xen_efi_runtime_setup();
333
5882bfef 334 shared_info_page = (struct shared_info *)get_zeroed_page(GFP_KERNEL);
ef61ee0d 335
2e01f166
SS
336 if (!shared_info_page) {
337 pr_err("not enough memory\n");
338 return -ENOMEM;
339 }
340 xatp.domid = DOMID_SELF;
341 xatp.idx = 0;
342 xatp.space = XENMAPSPACE_shared_info;
250c9af3 343 xatp.gpfn = virt_to_gfn(shared_info_page);
2e01f166
SS
344 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
345 BUG();
346
347 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
348
349 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
350 * page, we use it in the event channel upcall and in some pvclock
9a9ab3cc 351 * related functions.
2e01f166
SS
352 * The shared info contains exactly 1 CPU (the boot CPU). The guest
353 * is required to use VCPUOP_register_vcpu_info to place vcpu info
9a9ab3cc
SS
354 * for secondary CPUs as they are brought up.
355 * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
356 */
24d5373d 357 xen_vcpu_info = alloc_percpu(struct vcpu_info);
9a9ab3cc
SS
358 if (xen_vcpu_info == NULL)
359 return -ENOMEM;
b3b52fd8 360
88e957d6 361 /* Direct vCPU id mapping for ARM guests. */
de75abbe
VK
362 for_each_possible_cpu(cpu)
363 per_cpu(xen_vcpu_id, cpu) = cpu;
88e957d6 364
3cf4095d
SZ
365 xen_auto_xlat_grant_frames.count = gnttab_max_grant_frames();
366 if (xen_xlate_map_ballooned_pages(&xen_auto_xlat_grant_frames.pfn,
367 &xen_auto_xlat_grant_frames.vaddr,
368 xen_auto_xlat_grant_frames.count)) {
efaf30a3
KRW
369 free_percpu(xen_vcpu_info);
370 return -ENOMEM;
371 }
b3b52fd8
SS
372 gnttab_init();
373 if (!xen_initial_domain())
3499ba81 374 xenbus_probe();
b3b52fd8 375
e1a9c16b
JG
376 /*
377 * Making sure board specific code will not set up ops for
378 * cpu idle and cpu freq.
379 */
380 disable_cpuidle();
381 disable_cpufreq();
382
8b271d57
JG
383 xen_init_IRQ();
384
385 if (request_percpu_irq(xen_events_irq, xen_arm_callback,
386 "events", &xen_vcpu)) {
387 pr_err("Error request IRQ %d\n", xen_events_irq);
388 return -EINVAL;
389 }
390
f88af722
SS
391 if (!xen_kernel_unmapped_at_usr())
392 xen_time_setup_guest();
ecb23dc6 393
7d5f6f81
SS
394 if (xen_initial_domain())
395 pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
34e38523 396
4761adb6 397 return cpuhp_setup_state(CPUHP_AP_ARM_XEN_STARTING,
73c1b41e 398 "arm/xen:starting", xen_starting_cpu,
4761adb6 399 xen_dying_cpu);
1aa3d8d9 400}
8b271d57 401early_initcall(xen_guest_init);
1aa3d8d9
SS
402
403static int __init xen_pm_init(void)
404{
9dd4b294
RH
405 if (!xen_domain())
406 return -ENODEV;
407
6abb749e
SS
408 pm_power_off = xen_power_off;
409 arm_pm_restart = xen_restart;
e709fba1
SS
410 if (!xen_initial_domain()) {
411 struct timespec64 ts;
412 xen_read_wallclock(&ts);
413 do_settimeofday64(&ts);
414 }
6abb749e 415
2e01f166
SS
416 return 0;
417}
9dd4b294 418late_initcall(xen_pm_init);
0ec53ecf 419
79390289
SS
420
421/* empty stubs */
422void xen_arch_pre_suspend(void) { }
423void xen_arch_post_suspend(int suspend_cancelled) { }
424void xen_timer_resume(void) { }
425void xen_arch_resume(void) { }
ffb7dbed 426void xen_arch_suspend(void) { }
79390289
SS
427
428
d5f985c8 429/* In the hypercall.S file. */
911dec0d
KRW
430EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
431EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
ab277bbf
SS
432EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
433EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
434EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
435EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
436EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
437EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
ea0af613 438EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
176455e9 439EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
ade77a2d 440EXPORT_SYMBOL_GPL(HYPERVISOR_platform_op_raw);
9f1d3414 441EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
4b5ae015 442EXPORT_SYMBOL_GPL(HYPERVISOR_vm_assist);
ab520be8 443EXPORT_SYMBOL_GPL(HYPERVISOR_dm_op);
911dec0d 444EXPORT_SYMBOL_GPL(privcmd_call);