enlighten.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. if (!xen_kernel_unmapped_at_usr())
  140. xen_setup_runstate_info(cpu);
  141. after_register_vcpu_info:
  142. enable_percpu_irq(xen_events_irq, 0);
  143. return 0;
  144. }
  145. static int xen_dying_cpu(unsigned int cpu)
  146. {
  147. disable_percpu_irq(xen_events_irq);
  148. return 0;
  149. }
  150. void xen_reboot(int reason)
  151. {
  152. struct sched_shutdown r = { .reason = reason };
  153. int rc;
  154. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  155. BUG_ON(rc);
  156. }
  157. static int xen_restart(struct notifier_block *nb, unsigned long action,
  158. void *data)
  159. {
  160. xen_reboot(SHUTDOWN_reboot);
  161. return NOTIFY_DONE;
  162. }
  163. static struct notifier_block xen_restart_nb = {
  164. .notifier_call = xen_restart,
  165. .priority = 192,
  166. };
  167. static void xen_power_off(void)
  168. {
  169. xen_reboot(SHUTDOWN_poweroff);
  170. }
  171. static irqreturn_t xen_arm_callback(int irq, void *arg)
  172. {
  173. xen_evtchn_do_upcall();
  174. return IRQ_HANDLED;
  175. }
  176. static __initdata struct {
  177. const char *compat;
  178. const char *prefix;
  179. const char *version;
  180. bool found;
  181. } hyper_node = {"xen,xen", "xen,xen-", NULL, false};
  182. static int __init fdt_find_hyper_node(unsigned long node, const char *uname,
  183. int depth, void *data)
  184. {
  185. const void *s = NULL;
  186. int len;
  187. if (depth != 1 || strcmp(uname, "hypervisor") != 0)
  188. return 0;
  189. if (of_flat_dt_is_compatible(node, hyper_node.compat))
  190. hyper_node.found = true;
  191. s = of_get_flat_dt_prop(node, "compatible", &len);
  192. if (strlen(hyper_node.prefix) + 3 < len &&
  193. !strncmp(hyper_node.prefix, s, strlen(hyper_node.prefix)))
  194. hyper_node.version = s + strlen(hyper_node.prefix);
  195. /*
  196. * Check if Xen supports EFI by checking whether there is the
  197. * "/hypervisor/uefi" node in DT. If so, runtime services are available
  198. * through proxy functions (e.g. in case of Xen dom0 EFI implementation
  199. * they call special hypercall which executes relevant EFI functions)
  200. * and that is why they are always enabled.
  201. */
  202. if (IS_ENABLED(CONFIG_XEN_EFI)) {
  203. if ((of_get_flat_dt_subnode_by_name(node, "uefi") > 0) &&
  204. !efi_runtime_disabled())
  205. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  206. }
  207. return 0;
  208. }
  209. /*
  210. * see Documentation/devicetree/bindings/arm/xen.txt for the
  211. * documentation of the Xen Device Tree format.
  212. */
  213. void __init xen_early_init(void)
  214. {
  215. of_scan_flat_dt(fdt_find_hyper_node, NULL);
  216. if (!hyper_node.found) {
  217. pr_debug("No Xen support\n");
  218. return;
  219. }
  220. if (hyper_node.version == NULL) {
  221. pr_debug("Xen version not found\n");
  222. return;
  223. }
  224. pr_info("Xen %s support found\n", hyper_node.version);
  225. xen_domain_type = XEN_HVM_DOMAIN;
  226. xen_setup_features();
  227. if (xen_feature(XENFEAT_dom0))
  228. xen_start_flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
  229. if (!console_set_on_cmdline && !xen_initial_domain())
  230. add_preferred_console("hvc", 0, NULL);
  231. }
  232. static void __init xen_acpi_guest_init(void)
  233. {
  234. #ifdef CONFIG_ACPI
  235. struct xen_hvm_param a;
  236. int interrupt, trigger, polarity;
  237. a.domid = DOMID_SELF;
  238. a.index = HVM_PARAM_CALLBACK_IRQ;
  239. if (HYPERVISOR_hvm_op(HVMOP_get_param, &a)
  240. || (a.value >> 56) != HVM_PARAM_CALLBACK_TYPE_PPI) {
  241. xen_events_irq = 0;
  242. return;
  243. }
  244. interrupt = a.value & 0xff;
  245. trigger = ((a.value >> 8) & 0x1) ? ACPI_EDGE_SENSITIVE
  246. : ACPI_LEVEL_SENSITIVE;
  247. polarity = ((a.value >> 8) & 0x2) ? ACPI_ACTIVE_LOW
  248. : ACPI_ACTIVE_HIGH;
  249. xen_events_irq = acpi_register_gsi(NULL, interrupt, trigger, polarity);
  250. #endif
  251. }
  252. #ifdef CONFIG_XEN_UNPOPULATED_ALLOC
  253. /*
  254. * A type-less specific Xen resource which contains extended regions
  255. * (unused regions of guest physical address space provided by the hypervisor).
  256. */
  257. static struct resource xen_resource = {
  258. .name = "Xen unused space",
  259. };
  260. int __init arch_xen_unpopulated_init(struct resource **res)
  261. {
  262. struct device_node *np;
  263. struct resource *regs, *tmp_res;
  264. uint64_t min_gpaddr = -1, max_gpaddr = 0;
  265. unsigned int i, nr_reg = 0;
  266. int rc;
  267. if (!xen_domain())
  268. return -ENODEV;
  269. if (!acpi_disabled)
  270. return -ENODEV;
  271. np = of_find_compatible_node(NULL, NULL, "xen,xen");
  272. if (WARN_ON(!np))
  273. return -ENODEV;
  274. /* Skip region 0 which is reserved for grant table space */
  275. while (of_get_address(np, nr_reg + EXT_REGION_INDEX, NULL, NULL))
  276. nr_reg++;
  277. if (!nr_reg) {
  278. pr_err("No extended regions are found\n");
  279. of_node_put(np);
  280. return -EINVAL;
  281. }
  282. regs = kcalloc(nr_reg, sizeof(*regs), GFP_KERNEL);
  283. if (!regs) {
  284. of_node_put(np);
  285. return -ENOMEM;
  286. }
  287. /*
  288. * Create resource from extended regions provided by the hypervisor to be
  289. * used as unused address space for Xen scratch pages.
  290. */
  291. for (i = 0; i < nr_reg; i++) {
  292. rc = of_address_to_resource(np, i + EXT_REGION_INDEX, &regs[i]);
  293. if (rc)
  294. goto err;
  295. if (max_gpaddr < regs[i].end)
  296. max_gpaddr = regs[i].end;
  297. if (min_gpaddr > regs[i].start)
  298. min_gpaddr = regs[i].start;
  299. }
  300. xen_resource.start = min_gpaddr;
  301. xen_resource.end = max_gpaddr;
  302. /*
  303. * Mark holes between extended regions as unavailable. The rest of that
  304. * address space will be available for the allocation.
  305. */
  306. for (i = 1; i < nr_reg; i++) {
  307. resource_size_t start, end;
  308. /* There is an overlap between regions */
  309. if (regs[i - 1].end + 1 > regs[i].start) {
  310. rc = -EINVAL;
  311. goto err;
  312. }
  313. /* There is no hole between regions */
  314. if (regs[i - 1].end + 1 == regs[i].start)
  315. continue;
  316. start = regs[i - 1].end + 1;
  317. end = regs[i].start - 1;
  318. tmp_res = kzalloc(sizeof(*tmp_res), GFP_KERNEL);
  319. if (!tmp_res) {
  320. rc = -ENOMEM;
  321. goto err;
  322. }
  323. tmp_res->name = "Unavailable space";
  324. tmp_res->start = start;
  325. tmp_res->end = end;
  326. rc = insert_resource(&xen_resource, tmp_res);
  327. if (rc) {
  328. pr_err("Cannot insert resource %pR (%d)\n", tmp_res, rc);
  329. kfree(tmp_res);
  330. goto err;
  331. }
  332. }
  333. *res = &xen_resource;
  334. err:
  335. of_node_put(np);
  336. kfree(regs);
  337. return rc;
  338. }
  339. #endif
  340. static void __init xen_dt_guest_init(void)
  341. {
  342. struct device_node *xen_node;
  343. struct resource res;
  344. xen_node = of_find_compatible_node(NULL, NULL, "xen,xen");
  345. if (!xen_node) {
  346. pr_err("Xen support was detected before, but it has disappeared\n");
  347. return;
  348. }
  349. xen_events_irq = irq_of_parse_and_map(xen_node, 0);
  350. if (of_address_to_resource(xen_node, GRANT_TABLE_INDEX, &res)) {
  351. pr_err("Xen grant table region is not found\n");
  352. of_node_put(xen_node);
  353. return;
  354. }
  355. of_node_put(xen_node);
  356. xen_grant_frames = res.start;
  357. }
  358. static int __init xen_guest_init(void)
  359. {
  360. struct xen_add_to_physmap xatp;
  361. struct shared_info *shared_info_page = NULL;
  362. int rc, cpu;
  363. if (!xen_domain())
  364. return 0;
  365. if (IS_ENABLED(CONFIG_XEN_VIRTIO))
  366. virtio_set_mem_acc_cb(xen_virtio_mem_acc);
  367. if (!acpi_disabled)
  368. xen_acpi_guest_init();
  369. else
  370. xen_dt_guest_init();
  371. if (!xen_events_irq) {
  372. pr_err("Xen event channel interrupt not found\n");
  373. return -ENODEV;
  374. }
  375. /*
  376. * The fdt parsing codes have set EFI_RUNTIME_SERVICES if Xen EFI
  377. * parameters are found. Force enable runtime services.
  378. */
  379. if (efi_enabled(EFI_RUNTIME_SERVICES))
  380. xen_efi_runtime_setup();
  381. shared_info_page = (struct shared_info *)get_zeroed_page(GFP_KERNEL);
  382. if (!shared_info_page) {
  383. pr_err("not enough memory\n");
  384. return -ENOMEM;
  385. }
  386. xatp.domid = DOMID_SELF;
  387. xatp.idx = 0;
  388. xatp.space = XENMAPSPACE_shared_info;
  389. xatp.gpfn = virt_to_gfn(shared_info_page);
  390. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  391. BUG();
  392. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  393. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  394. * page, we use it in the event channel upcall and in some pvclock
  395. * related functions.
  396. * The shared info contains exactly 1 CPU (the boot CPU). The guest
  397. * is required to use VCPUOP_register_vcpu_info to place vcpu info
  398. * for secondary CPUs as they are brought up.
  399. * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
  400. */
  401. xen_vcpu_info = __alloc_percpu(sizeof(struct vcpu_info),
  402. 1 << fls(sizeof(struct vcpu_info) - 1));
  403. if (xen_vcpu_info == NULL)
  404. return -ENOMEM;
  405. /* Direct vCPU id mapping for ARM guests. */
  406. for_each_possible_cpu(cpu)
  407. per_cpu(xen_vcpu_id, cpu) = cpu;
  408. if (!xen_grant_frames) {
  409. xen_auto_xlat_grant_frames.count = gnttab_max_grant_frames();
  410. rc = xen_xlate_map_ballooned_pages(&xen_auto_xlat_grant_frames.pfn,
  411. &xen_auto_xlat_grant_frames.vaddr,
  412. xen_auto_xlat_grant_frames.count);
  413. } else
  414. rc = gnttab_setup_auto_xlat_frames(xen_grant_frames);
  415. if (rc) {
  416. free_percpu(xen_vcpu_info);
  417. return rc;
  418. }
  419. gnttab_init();
  420. /*
  421. * Making sure board specific code will not set up ops for
  422. * cpu idle and cpu freq.
  423. */
  424. disable_cpuidle();
  425. disable_cpufreq();
  426. xen_init_IRQ();
  427. if (request_percpu_irq(xen_events_irq, xen_arm_callback,
  428. "events", &xen_vcpu)) {
  429. pr_err("Error request IRQ %d\n", xen_events_irq);
  430. return -EINVAL;
  431. }
  432. if (!xen_kernel_unmapped_at_usr())
  433. xen_time_setup_guest();
  434. if (xen_initial_domain())
  435. pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
  436. return cpuhp_setup_state(CPUHP_AP_ARM_XEN_STARTING,
  437. "arm/xen:starting", xen_starting_cpu,
  438. xen_dying_cpu);
  439. }
  440. early_initcall(xen_guest_init);
  441. static int __init xen_pm_init(void)
  442. {
  443. if (!xen_domain())
  444. return -ENODEV;
  445. pm_power_off = xen_power_off;
  446. register_restart_handler(&xen_restart_nb);
  447. if (!xen_initial_domain()) {
  448. struct timespec64 ts;
  449. xen_read_wallclock(&ts);
  450. do_settimeofday64(&ts);
  451. }
  452. return 0;
  453. }
  454. late_initcall(xen_pm_init);
  455. /* empty stubs */
  456. void xen_arch_pre_suspend(void) { }
  457. void xen_arch_post_suspend(int suspend_cancelled) { }
  458. void xen_timer_resume(void) { }
  459. void xen_arch_resume(void) { }
  460. void xen_arch_suspend(void) { }
  461. /* In the hypercall.S file. */
  462. EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
  463. EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
  464. EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
  465. EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
  466. EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
  467. EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
  468. EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
  469. EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
  470. EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
  471. EXPORT_SYMBOL_GPL(HYPERVISOR_platform_op_raw);
  472. EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
  473. EXPORT_SYMBOL_GPL(HYPERVISOR_vm_assist);
  474. EXPORT_SYMBOL_GPL(HYPERVISOR_dm_op);
  475. EXPORT_SYMBOL_GPL(privcmd_call);