enlighten.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include <xen/xen.h>
  2. #include <xen/events.h>
  3. #include <xen/grant_table.h>
  4. #include <xen/hvm.h>
  5. #include <xen/interface/vcpu.h>
  6. #include <xen/interface/xen.h>
  7. #include <xen/interface/memory.h>
  8. #include <xen/interface/hvm/params.h>
  9. #include <xen/features.h>
  10. #include <xen/platform_pci.h>
  11. #include <xen/xenbus.h>
  12. #include <xen/page.h>
  13. #include <xen/interface/sched.h>
  14. #include <xen/xen-ops.h>
  15. #include <asm/paravirt.h>
  16. #include <asm/xen/hypervisor.h>
  17. #include <asm/xen/hypercall.h>
  18. #include <asm/system_misc.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irqreturn.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_address.h>
  25. #include <linux/cpuidle.h>
  26. #include <linux/cpufreq.h>
  27. #include <linux/cpu.h>
  28. #include <linux/console.h>
  29. #include <linux/pvclock_gtod.h>
  30. #include <linux/time64.h>
  31. #include <linux/timekeeping.h>
  32. #include <linux/timekeeper_internal.h>
  33. #include <linux/mm.h>
  34. struct start_info _xen_start_info;
  35. struct start_info *xen_start_info = &_xen_start_info;
  36. EXPORT_SYMBOL(xen_start_info);
  37. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  38. EXPORT_SYMBOL(xen_domain_type);
  39. struct shared_info xen_dummy_shared_info;
  40. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  41. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  42. static struct vcpu_info __percpu *xen_vcpu_info;
  43. /* These are unused until we support booting "pre-ballooned" */
  44. unsigned long xen_released_pages;
  45. struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
  46. static __read_mostly unsigned int xen_events_irq;
  47. static __initdata struct device_node *xen_node;
  48. int xen_remap_domain_gfn_array(struct vm_area_struct *vma,
  49. unsigned long addr,
  50. xen_pfn_t *gfn, int nr,
  51. int *err_ptr, pgprot_t prot,
  52. unsigned domid,
  53. struct page **pages)
  54. {
  55. return xen_xlate_remap_gfn_array(vma, addr, gfn, nr, err_ptr,
  56. prot, domid, pages);
  57. }
  58. EXPORT_SYMBOL_GPL(xen_remap_domain_gfn_array);
  59. /* Not used by XENFEAT_auto_translated guests. */
  60. int xen_remap_domain_gfn_range(struct vm_area_struct *vma,
  61. unsigned long addr,
  62. xen_pfn_t gfn, int nr,
  63. pgprot_t prot, unsigned domid,
  64. struct page **pages)
  65. {
  66. return -ENOSYS;
  67. }
  68. EXPORT_SYMBOL_GPL(xen_remap_domain_gfn_range);
  69. int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
  70. int nr, struct page **pages)
  71. {
  72. return xen_xlate_unmap_gfn_range(vma, nr, pages);
  73. }
  74. EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range);
  75. static unsigned long long xen_stolen_accounting(int cpu)
  76. {
  77. struct vcpu_runstate_info state;
  78. BUG_ON(cpu != smp_processor_id());
  79. xen_get_runstate_snapshot(&state);
  80. WARN_ON(state.state != RUNSTATE_running);
  81. return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline];
  82. }
  83. static void xen_read_wallclock(struct timespec64 *ts)
  84. {
  85. u32 version;
  86. struct timespec64 now, ts_monotonic;
  87. struct shared_info *s = HYPERVISOR_shared_info;
  88. struct pvclock_wall_clock *wall_clock = &(s->wc);
  89. /* get wallclock at system boot */
  90. do {
  91. version = wall_clock->version;
  92. rmb(); /* fetch version before time */
  93. now.tv_sec = ((uint64_t)wall_clock->sec_hi << 32) | wall_clock->sec;
  94. now.tv_nsec = wall_clock->nsec;
  95. rmb(); /* fetch time before checking version */
  96. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  97. /* time since system boot */
  98. ktime_get_ts64(&ts_monotonic);
  99. *ts = timespec64_add(now, ts_monotonic);
  100. }
  101. static int xen_pvclock_gtod_notify(struct notifier_block *nb,
  102. unsigned long was_set, void *priv)
  103. {
  104. /* Protected by the calling core code serialization */
  105. static struct timespec64 next_sync;
  106. struct xen_platform_op op;
  107. struct timespec64 now, system_time;
  108. struct timekeeper *tk = priv;
  109. now.tv_sec = tk->xtime_sec;
  110. now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
  111. system_time = timespec64_add(now, tk->wall_to_monotonic);
  112. /*
  113. * We only take the expensive HV call when the clock was set
  114. * or when the 11 minutes RTC synchronization time elapsed.
  115. */
  116. if (!was_set && timespec64_compare(&now, &next_sync) < 0)
  117. return NOTIFY_OK;
  118. op.cmd = XENPF_settime64;
  119. op.u.settime64.mbz = 0;
  120. op.u.settime64.secs = now.tv_sec;
  121. op.u.settime64.nsecs = now.tv_nsec;
  122. op.u.settime64.system_time = timespec64_to_ns(&system_time);
  123. (void)HYPERVISOR_platform_op(&op);
  124. /*
  125. * Move the next drift compensation time 11 minutes
  126. * ahead. That's emulating the sync_cmos_clock() update for
  127. * the hardware RTC.
  128. */
  129. next_sync = now;
  130. next_sync.tv_sec += 11 * 60;
  131. return NOTIFY_OK;
  132. }
  133. static struct notifier_block xen_pvclock_gtod_notifier = {
  134. .notifier_call = xen_pvclock_gtod_notify,
  135. };
  136. static void xen_percpu_init(void)
  137. {
  138. struct vcpu_register_vcpu_info info;
  139. struct vcpu_info *vcpup;
  140. int err;
  141. int cpu = get_cpu();
  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. pr_info("Xen: initializing cpu%d\n", cpu);
  150. vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
  151. info.mfn = virt_to_gfn(vcpup);
  152. info.offset = xen_offset_in_page(vcpup);
  153. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  154. BUG_ON(err);
  155. per_cpu(xen_vcpu, cpu) = vcpup;
  156. xen_setup_runstate_info(cpu);
  157. after_register_vcpu_info:
  158. enable_percpu_irq(xen_events_irq, 0);
  159. put_cpu();
  160. }
  161. static void xen_restart(enum reboot_mode reboot_mode, const char *cmd)
  162. {
  163. struct sched_shutdown r = { .reason = SHUTDOWN_reboot };
  164. int rc;
  165. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  166. BUG_ON(rc);
  167. }
  168. static void xen_power_off(void)
  169. {
  170. struct sched_shutdown r = { .reason = SHUTDOWN_poweroff };
  171. int rc;
  172. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  173. BUG_ON(rc);
  174. }
  175. static int xen_cpu_notification(struct notifier_block *self,
  176. unsigned long action,
  177. void *hcpu)
  178. {
  179. switch (action) {
  180. case CPU_STARTING:
  181. xen_percpu_init();
  182. break;
  183. case CPU_DYING:
  184. disable_percpu_irq(xen_events_irq);
  185. break;
  186. default:
  187. break;
  188. }
  189. return NOTIFY_OK;
  190. }
  191. static struct notifier_block xen_cpu_notifier = {
  192. .notifier_call = xen_cpu_notification,
  193. };
  194. static irqreturn_t xen_arm_callback(int irq, void *arg)
  195. {
  196. xen_hvm_evtchn_do_upcall();
  197. return IRQ_HANDLED;
  198. }
  199. /*
  200. * see Documentation/devicetree/bindings/arm/xen.txt for the
  201. * documentation of the Xen Device Tree format.
  202. */
  203. #define GRANT_TABLE_PHYSADDR 0
  204. void __init xen_early_init(void)
  205. {
  206. int len;
  207. const char *s = NULL;
  208. const char *version = NULL;
  209. const char *xen_prefix = "xen,xen-";
  210. xen_node = of_find_compatible_node(NULL, NULL, "xen,xen");
  211. if (!xen_node) {
  212. pr_debug("No Xen support\n");
  213. return;
  214. }
  215. s = of_get_property(xen_node, "compatible", &len);
  216. if (strlen(xen_prefix) + 3 < len &&
  217. !strncmp(xen_prefix, s, strlen(xen_prefix)))
  218. version = s + strlen(xen_prefix);
  219. if (version == NULL) {
  220. pr_debug("Xen version not found\n");
  221. return;
  222. }
  223. pr_info("Xen %s support found\n", version);
  224. xen_domain_type = XEN_HVM_DOMAIN;
  225. xen_setup_features();
  226. if (xen_feature(XENFEAT_dom0))
  227. xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
  228. else
  229. xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
  230. if (!console_set_on_cmdline && !xen_initial_domain())
  231. add_preferred_console("hvc", 0, NULL);
  232. }
  233. static int __init xen_guest_init(void)
  234. {
  235. struct xen_add_to_physmap xatp;
  236. struct shared_info *shared_info_page = NULL;
  237. struct resource res;
  238. phys_addr_t grant_frames;
  239. if (!xen_domain())
  240. return 0;
  241. if (of_address_to_resource(xen_node, GRANT_TABLE_PHYSADDR, &res)) {
  242. pr_err("Xen grant table base address not found\n");
  243. return -ENODEV;
  244. }
  245. grant_frames = res.start;
  246. xen_events_irq = irq_of_parse_and_map(xen_node, 0);
  247. if (!xen_events_irq) {
  248. pr_err("Xen event channel interrupt not found\n");
  249. return -ENODEV;
  250. }
  251. shared_info_page = (struct shared_info *)get_zeroed_page(GFP_KERNEL);
  252. if (!shared_info_page) {
  253. pr_err("not enough memory\n");
  254. return -ENOMEM;
  255. }
  256. xatp.domid = DOMID_SELF;
  257. xatp.idx = 0;
  258. xatp.space = XENMAPSPACE_shared_info;
  259. xatp.gpfn = virt_to_gfn(shared_info_page);
  260. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  261. BUG();
  262. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  263. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  264. * page, we use it in the event channel upcall and in some pvclock
  265. * related functions.
  266. * The shared info contains exactly 1 CPU (the boot CPU). The guest
  267. * is required to use VCPUOP_register_vcpu_info to place vcpu info
  268. * for secondary CPUs as they are brought up.
  269. * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
  270. */
  271. xen_vcpu_info = __alloc_percpu(sizeof(struct vcpu_info),
  272. sizeof(struct vcpu_info));
  273. if (xen_vcpu_info == NULL)
  274. return -ENOMEM;
  275. if (gnttab_setup_auto_xlat_frames(grant_frames)) {
  276. free_percpu(xen_vcpu_info);
  277. return -ENOMEM;
  278. }
  279. gnttab_init();
  280. if (!xen_initial_domain())
  281. xenbus_probe(NULL);
  282. /*
  283. * Making sure board specific code will not set up ops for
  284. * cpu idle and cpu freq.
  285. */
  286. disable_cpuidle();
  287. disable_cpufreq();
  288. xen_init_IRQ();
  289. if (request_percpu_irq(xen_events_irq, xen_arm_callback,
  290. "events", &xen_vcpu)) {
  291. pr_err("Error request IRQ %d\n", xen_events_irq);
  292. return -EINVAL;
  293. }
  294. xen_percpu_init();
  295. register_cpu_notifier(&xen_cpu_notifier);
  296. pv_time_ops.steal_clock = xen_stolen_accounting;
  297. static_key_slow_inc(&paravirt_steal_enabled);
  298. if (xen_initial_domain())
  299. pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
  300. return 0;
  301. }
  302. early_initcall(xen_guest_init);
  303. static int __init xen_pm_init(void)
  304. {
  305. if (!xen_domain())
  306. return -ENODEV;
  307. pm_power_off = xen_power_off;
  308. arm_pm_restart = xen_restart;
  309. if (!xen_initial_domain()) {
  310. struct timespec64 ts;
  311. xen_read_wallclock(&ts);
  312. do_settimeofday64(&ts);
  313. }
  314. return 0;
  315. }
  316. late_initcall(xen_pm_init);
  317. /* empty stubs */
  318. void xen_arch_pre_suspend(void) { }
  319. void xen_arch_post_suspend(int suspend_cancelled) { }
  320. void xen_timer_resume(void) { }
  321. void xen_arch_resume(void) { }
  322. void xen_arch_suspend(void) { }
  323. /* In the hypercall.S file. */
  324. EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
  325. EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
  326. EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
  327. EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
  328. EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
  329. EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
  330. EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
  331. EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
  332. EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
  333. EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
  334. EXPORT_SYMBOL_GPL(HYPERVISOR_platform_op);
  335. EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
  336. EXPORT_SYMBOL_GPL(privcmd_call);