0001
0002 #include <xen/xen.h>
0003 #include <xen/events.h>
0004 #include <xen/grant_table.h>
0005 #include <xen/hvm.h>
0006 #include <xen/interface/vcpu.h>
0007 #include <xen/interface/xen.h>
0008 #include <xen/interface/memory.h>
0009 #include <xen/interface/hvm/params.h>
0010 #include <xen/features.h>
0011 #include <xen/platform_pci.h>
0012 #include <xen/xenbus.h>
0013 #include <xen/page.h>
0014 #include <xen/interface/sched.h>
0015 #include <xen/xen-ops.h>
0016 #include <asm/xen/hypervisor.h>
0017 #include <asm/xen/hypercall.h>
0018 #include <asm/system_misc.h>
0019 #include <asm/efi.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/irqreturn.h>
0022 #include <linux/module.h>
0023 #include <linux/of.h>
0024 #include <linux/of_fdt.h>
0025 #include <linux/of_irq.h>
0026 #include <linux/of_address.h>
0027 #include <linux/cpuidle.h>
0028 #include <linux/cpufreq.h>
0029 #include <linux/cpu.h>
0030 #include <linux/console.h>
0031 #include <linux/pvclock_gtod.h>
0032 #include <linux/reboot.h>
0033 #include <linux/time64.h>
0034 #include <linux/timekeeping.h>
0035 #include <linux/timekeeper_internal.h>
0036 #include <linux/acpi.h>
0037 #include <linux/virtio_anchor.h>
0038
0039 #include <linux/mm.h>
0040
0041 static struct start_info _xen_start_info;
0042 struct start_info *xen_start_info = &_xen_start_info;
0043 EXPORT_SYMBOL(xen_start_info);
0044
0045 enum xen_domain_type xen_domain_type = XEN_NATIVE;
0046 EXPORT_SYMBOL(xen_domain_type);
0047
0048 struct shared_info xen_dummy_shared_info;
0049 struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
0050
0051 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
0052 static struct vcpu_info __percpu *xen_vcpu_info;
0053
0054
0055 DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
0056 EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
0057
0058
0059 unsigned long xen_released_pages;
0060 struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
0061
0062 static __read_mostly unsigned int xen_events_irq;
0063 static __read_mostly phys_addr_t xen_grant_frames;
0064
0065 #define GRANT_TABLE_INDEX 0
0066 #define EXT_REGION_INDEX 1
0067
0068 uint32_t xen_start_flags;
0069 EXPORT_SYMBOL(xen_start_flags);
0070
0071 int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
0072 int nr, struct page **pages)
0073 {
0074 return xen_xlate_unmap_gfn_range(vma, nr, pages);
0075 }
0076 EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range);
0077
0078 static void xen_read_wallclock(struct timespec64 *ts)
0079 {
0080 u32 version;
0081 struct timespec64 now, ts_monotonic;
0082 struct shared_info *s = HYPERVISOR_shared_info;
0083 struct pvclock_wall_clock *wall_clock = &(s->wc);
0084
0085
0086 do {
0087 version = wall_clock->version;
0088 rmb();
0089 now.tv_sec = ((uint64_t)wall_clock->sec_hi << 32) | wall_clock->sec;
0090 now.tv_nsec = wall_clock->nsec;
0091 rmb();
0092 } while ((wall_clock->version & 1) || (version != wall_clock->version));
0093
0094
0095 ktime_get_ts64(&ts_monotonic);
0096 *ts = timespec64_add(now, ts_monotonic);
0097 }
0098
0099 static int xen_pvclock_gtod_notify(struct notifier_block *nb,
0100 unsigned long was_set, void *priv)
0101 {
0102
0103 static struct timespec64 next_sync;
0104
0105 struct xen_platform_op op;
0106 struct timespec64 now, system_time;
0107 struct timekeeper *tk = priv;
0108
0109 now.tv_sec = tk->xtime_sec;
0110 now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
0111 system_time = timespec64_add(now, tk->wall_to_monotonic);
0112
0113
0114
0115
0116
0117 if (!was_set && timespec64_compare(&now, &next_sync) < 0)
0118 return NOTIFY_OK;
0119
0120 op.cmd = XENPF_settime64;
0121 op.u.settime64.mbz = 0;
0122 op.u.settime64.secs = now.tv_sec;
0123 op.u.settime64.nsecs = now.tv_nsec;
0124 op.u.settime64.system_time = timespec64_to_ns(&system_time);
0125 (void)HYPERVISOR_platform_op(&op);
0126
0127
0128
0129
0130
0131
0132 next_sync = now;
0133 next_sync.tv_sec += 11 * 60;
0134
0135 return NOTIFY_OK;
0136 }
0137
0138 static struct notifier_block xen_pvclock_gtod_notifier = {
0139 .notifier_call = xen_pvclock_gtod_notify,
0140 };
0141
0142 static int xen_starting_cpu(unsigned int cpu)
0143 {
0144 struct vcpu_register_vcpu_info info;
0145 struct vcpu_info *vcpup;
0146 int err;
0147
0148
0149
0150
0151
0152
0153 if (per_cpu(xen_vcpu, cpu) != NULL)
0154 goto after_register_vcpu_info;
0155
0156 pr_info("Xen: initializing cpu%d\n", cpu);
0157 vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
0158
0159 info.mfn = percpu_to_gfn(vcpup);
0160 info.offset = xen_offset_in_page(vcpup);
0161
0162 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu),
0163 &info);
0164 BUG_ON(err);
0165 per_cpu(xen_vcpu, cpu) = vcpup;
0166
0167 if (!xen_kernel_unmapped_at_usr())
0168 xen_setup_runstate_info(cpu);
0169
0170 after_register_vcpu_info:
0171 enable_percpu_irq(xen_events_irq, 0);
0172 return 0;
0173 }
0174
0175 static int xen_dying_cpu(unsigned int cpu)
0176 {
0177 disable_percpu_irq(xen_events_irq);
0178 return 0;
0179 }
0180
0181 void xen_reboot(int reason)
0182 {
0183 struct sched_shutdown r = { .reason = reason };
0184 int rc;
0185
0186 rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
0187 BUG_ON(rc);
0188 }
0189
0190 static int xen_restart(struct notifier_block *nb, unsigned long action,
0191 void *data)
0192 {
0193 xen_reboot(SHUTDOWN_reboot);
0194
0195 return NOTIFY_DONE;
0196 }
0197
0198 static struct notifier_block xen_restart_nb = {
0199 .notifier_call = xen_restart,
0200 .priority = 192,
0201 };
0202
0203 static void xen_power_off(void)
0204 {
0205 xen_reboot(SHUTDOWN_poweroff);
0206 }
0207
0208 static irqreturn_t xen_arm_callback(int irq, void *arg)
0209 {
0210 xen_hvm_evtchn_do_upcall();
0211 return IRQ_HANDLED;
0212 }
0213
0214 static __initdata struct {
0215 const char *compat;
0216 const char *prefix;
0217 const char *version;
0218 bool found;
0219 } hyper_node = {"xen,xen", "xen,xen-", NULL, false};
0220
0221 static int __init fdt_find_hyper_node(unsigned long node, const char *uname,
0222 int depth, void *data)
0223 {
0224 const void *s = NULL;
0225 int len;
0226
0227 if (depth != 1 || strcmp(uname, "hypervisor") != 0)
0228 return 0;
0229
0230 if (of_flat_dt_is_compatible(node, hyper_node.compat))
0231 hyper_node.found = true;
0232
0233 s = of_get_flat_dt_prop(node, "compatible", &len);
0234 if (strlen(hyper_node.prefix) + 3 < len &&
0235 !strncmp(hyper_node.prefix, s, strlen(hyper_node.prefix)))
0236 hyper_node.version = s + strlen(hyper_node.prefix);
0237
0238
0239
0240
0241
0242
0243
0244
0245 if (IS_ENABLED(CONFIG_XEN_EFI)) {
0246 if ((of_get_flat_dt_subnode_by_name(node, "uefi") > 0) &&
0247 !efi_runtime_disabled())
0248 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0249 }
0250
0251 return 0;
0252 }
0253
0254
0255
0256
0257
0258 void __init xen_early_init(void)
0259 {
0260 of_scan_flat_dt(fdt_find_hyper_node, NULL);
0261 if (!hyper_node.found) {
0262 pr_debug("No Xen support\n");
0263 return;
0264 }
0265
0266 if (hyper_node.version == NULL) {
0267 pr_debug("Xen version not found\n");
0268 return;
0269 }
0270
0271 pr_info("Xen %s support found\n", hyper_node.version);
0272
0273 xen_domain_type = XEN_HVM_DOMAIN;
0274
0275 xen_setup_features();
0276
0277 if (xen_feature(XENFEAT_dom0))
0278 xen_start_flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
0279
0280 if (!console_set_on_cmdline && !xen_initial_domain())
0281 add_preferred_console("hvc", 0, NULL);
0282 }
0283
0284 static void __init xen_acpi_guest_init(void)
0285 {
0286 #ifdef CONFIG_ACPI
0287 struct xen_hvm_param a;
0288 int interrupt, trigger, polarity;
0289
0290 a.domid = DOMID_SELF;
0291 a.index = HVM_PARAM_CALLBACK_IRQ;
0292
0293 if (HYPERVISOR_hvm_op(HVMOP_get_param, &a)
0294 || (a.value >> 56) != HVM_PARAM_CALLBACK_TYPE_PPI) {
0295 xen_events_irq = 0;
0296 return;
0297 }
0298
0299 interrupt = a.value & 0xff;
0300 trigger = ((a.value >> 8) & 0x1) ? ACPI_EDGE_SENSITIVE
0301 : ACPI_LEVEL_SENSITIVE;
0302 polarity = ((a.value >> 8) & 0x2) ? ACPI_ACTIVE_LOW
0303 : ACPI_ACTIVE_HIGH;
0304 xen_events_irq = acpi_register_gsi(NULL, interrupt, trigger, polarity);
0305 #endif
0306 }
0307
0308 #ifdef CONFIG_XEN_UNPOPULATED_ALLOC
0309
0310
0311
0312
0313 static struct resource xen_resource = {
0314 .name = "Xen unused space",
0315 };
0316
0317 int __init arch_xen_unpopulated_init(struct resource **res)
0318 {
0319 struct device_node *np;
0320 struct resource *regs, *tmp_res;
0321 uint64_t min_gpaddr = -1, max_gpaddr = 0;
0322 unsigned int i, nr_reg = 0;
0323 int rc;
0324
0325 if (!xen_domain())
0326 return -ENODEV;
0327
0328 if (!acpi_disabled)
0329 return -ENODEV;
0330
0331 np = of_find_compatible_node(NULL, NULL, "xen,xen");
0332 if (WARN_ON(!np))
0333 return -ENODEV;
0334
0335
0336 while (of_get_address(np, nr_reg + EXT_REGION_INDEX, NULL, NULL))
0337 nr_reg++;
0338
0339 if (!nr_reg) {
0340 pr_err("No extended regions are found\n");
0341 of_node_put(np);
0342 return -EINVAL;
0343 }
0344
0345 regs = kcalloc(nr_reg, sizeof(*regs), GFP_KERNEL);
0346 if (!regs) {
0347 of_node_put(np);
0348 return -ENOMEM;
0349 }
0350
0351
0352
0353
0354
0355 for (i = 0; i < nr_reg; i++) {
0356 rc = of_address_to_resource(np, i + EXT_REGION_INDEX, ®s[i]);
0357 if (rc)
0358 goto err;
0359
0360 if (max_gpaddr < regs[i].end)
0361 max_gpaddr = regs[i].end;
0362 if (min_gpaddr > regs[i].start)
0363 min_gpaddr = regs[i].start;
0364 }
0365
0366 xen_resource.start = min_gpaddr;
0367 xen_resource.end = max_gpaddr;
0368
0369
0370
0371
0372
0373 for (i = 1; i < nr_reg; i++) {
0374 resource_size_t start, end;
0375
0376
0377 if (regs[i - 1].end + 1 > regs[i].start) {
0378 rc = -EINVAL;
0379 goto err;
0380 }
0381
0382
0383 if (regs[i - 1].end + 1 == regs[i].start)
0384 continue;
0385
0386 start = regs[i - 1].end + 1;
0387 end = regs[i].start - 1;
0388
0389 tmp_res = kzalloc(sizeof(*tmp_res), GFP_KERNEL);
0390 if (!tmp_res) {
0391 rc = -ENOMEM;
0392 goto err;
0393 }
0394
0395 tmp_res->name = "Unavailable space";
0396 tmp_res->start = start;
0397 tmp_res->end = end;
0398
0399 rc = insert_resource(&xen_resource, tmp_res);
0400 if (rc) {
0401 pr_err("Cannot insert resource %pR (%d)\n", tmp_res, rc);
0402 kfree(tmp_res);
0403 goto err;
0404 }
0405 }
0406
0407 *res = &xen_resource;
0408
0409 err:
0410 of_node_put(np);
0411 kfree(regs);
0412 return rc;
0413 }
0414 #endif
0415
0416 static void __init xen_dt_guest_init(void)
0417 {
0418 struct device_node *xen_node;
0419 struct resource res;
0420
0421 xen_node = of_find_compatible_node(NULL, NULL, "xen,xen");
0422 if (!xen_node) {
0423 pr_err("Xen support was detected before, but it has disappeared\n");
0424 return;
0425 }
0426
0427 xen_events_irq = irq_of_parse_and_map(xen_node, 0);
0428
0429 if (of_address_to_resource(xen_node, GRANT_TABLE_INDEX, &res)) {
0430 pr_err("Xen grant table region is not found\n");
0431 of_node_put(xen_node);
0432 return;
0433 }
0434 of_node_put(xen_node);
0435 xen_grant_frames = res.start;
0436 }
0437
0438 static int __init xen_guest_init(void)
0439 {
0440 struct xen_add_to_physmap xatp;
0441 struct shared_info *shared_info_page = NULL;
0442 int rc, cpu;
0443
0444 if (!xen_domain())
0445 return 0;
0446
0447 if (IS_ENABLED(CONFIG_XEN_VIRTIO))
0448 virtio_set_mem_acc_cb(xen_virtio_mem_acc);
0449
0450 if (!acpi_disabled)
0451 xen_acpi_guest_init();
0452 else
0453 xen_dt_guest_init();
0454
0455 if (!xen_events_irq) {
0456 pr_err("Xen event channel interrupt not found\n");
0457 return -ENODEV;
0458 }
0459
0460
0461
0462
0463
0464 if (efi_enabled(EFI_RUNTIME_SERVICES))
0465 xen_efi_runtime_setup();
0466
0467 shared_info_page = (struct shared_info *)get_zeroed_page(GFP_KERNEL);
0468
0469 if (!shared_info_page) {
0470 pr_err("not enough memory\n");
0471 return -ENOMEM;
0472 }
0473 xatp.domid = DOMID_SELF;
0474 xatp.idx = 0;
0475 xatp.space = XENMAPSPACE_shared_info;
0476 xatp.gpfn = virt_to_gfn(shared_info_page);
0477 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
0478 BUG();
0479
0480 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490 xen_vcpu_info = alloc_percpu(struct vcpu_info);
0491 if (xen_vcpu_info == NULL)
0492 return -ENOMEM;
0493
0494
0495 for_each_possible_cpu(cpu)
0496 per_cpu(xen_vcpu_id, cpu) = cpu;
0497
0498 if (!xen_grant_frames) {
0499 xen_auto_xlat_grant_frames.count = gnttab_max_grant_frames();
0500 rc = xen_xlate_map_ballooned_pages(&xen_auto_xlat_grant_frames.pfn,
0501 &xen_auto_xlat_grant_frames.vaddr,
0502 xen_auto_xlat_grant_frames.count);
0503 } else
0504 rc = gnttab_setup_auto_xlat_frames(xen_grant_frames);
0505 if (rc) {
0506 free_percpu(xen_vcpu_info);
0507 return rc;
0508 }
0509 gnttab_init();
0510
0511
0512
0513
0514
0515 disable_cpuidle();
0516 disable_cpufreq();
0517
0518 xen_init_IRQ();
0519
0520 if (request_percpu_irq(xen_events_irq, xen_arm_callback,
0521 "events", &xen_vcpu)) {
0522 pr_err("Error request IRQ %d\n", xen_events_irq);
0523 return -EINVAL;
0524 }
0525
0526 if (!xen_kernel_unmapped_at_usr())
0527 xen_time_setup_guest();
0528
0529 if (xen_initial_domain())
0530 pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
0531
0532 return cpuhp_setup_state(CPUHP_AP_ARM_XEN_STARTING,
0533 "arm/xen:starting", xen_starting_cpu,
0534 xen_dying_cpu);
0535 }
0536 early_initcall(xen_guest_init);
0537
0538 static int __init xen_pm_init(void)
0539 {
0540 if (!xen_domain())
0541 return -ENODEV;
0542
0543 pm_power_off = xen_power_off;
0544 register_restart_handler(&xen_restart_nb);
0545 if (!xen_initial_domain()) {
0546 struct timespec64 ts;
0547 xen_read_wallclock(&ts);
0548 do_settimeofday64(&ts);
0549 }
0550
0551 return 0;
0552 }
0553 late_initcall(xen_pm_init);
0554
0555
0556
0557 void xen_arch_pre_suspend(void) { }
0558 void xen_arch_post_suspend(int suspend_cancelled) { }
0559 void xen_timer_resume(void) { }
0560 void xen_arch_resume(void) { }
0561 void xen_arch_suspend(void) { }
0562
0563
0564
0565 EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
0566 EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
0567 EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
0568 EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
0569 EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
0570 EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
0571 EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
0572 EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
0573 EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
0574 EXPORT_SYMBOL_GPL(HYPERVISOR_platform_op_raw);
0575 EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
0576 EXPORT_SYMBOL_GPL(HYPERVISOR_vm_assist);
0577 EXPORT_SYMBOL_GPL(HYPERVISOR_dm_op);
0578 EXPORT_SYMBOL_GPL(privcmd_call);