enlighten.c 15 KB

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