enlighten.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/xen/hypervisor.h>
  16. #include <asm/xen/hypercall.h>
  17. #include <asm/system_misc.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irqreturn.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_address.h>
  24. #include <linux/cpuidle.h>
  25. #include <linux/cpufreq.h>
  26. #include <linux/cpu.h>
  27. #include <linux/mm.h>
  28. struct start_info _xen_start_info;
  29. struct start_info *xen_start_info = &_xen_start_info;
  30. EXPORT_SYMBOL(xen_start_info);
  31. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  32. EXPORT_SYMBOL(xen_domain_type);
  33. struct shared_info xen_dummy_shared_info;
  34. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  35. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  36. static struct vcpu_info __percpu *xen_vcpu_info;
  37. /* These are unused until we support booting "pre-ballooned" */
  38. unsigned long xen_released_pages;
  39. struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
  40. /* TODO: to be removed */
  41. __read_mostly int xen_have_vector_callback;
  42. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  43. int xen_platform_pci_unplug = XEN_UNPLUG_ALL;
  44. EXPORT_SYMBOL_GPL(xen_platform_pci_unplug);
  45. static __read_mostly int xen_events_irq = -1;
  46. int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
  47. unsigned long addr,
  48. xen_pfn_t *mfn, int nr,
  49. int *err_ptr, pgprot_t prot,
  50. unsigned domid,
  51. struct page **pages)
  52. {
  53. return xen_xlate_remap_gfn_array(vma, addr, mfn, nr, err_ptr,
  54. prot, domid, pages);
  55. }
  56. EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_array);
  57. /* Not used by XENFEAT_auto_translated guests. */
  58. int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
  59. unsigned long addr,
  60. xen_pfn_t mfn, int nr,
  61. pgprot_t prot, unsigned domid,
  62. struct page **pages)
  63. {
  64. return -ENOSYS;
  65. }
  66. EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
  67. int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
  68. int nr, struct page **pages)
  69. {
  70. return xen_xlate_unmap_gfn_range(vma, nr, pages);
  71. }
  72. EXPORT_SYMBOL_GPL(xen_unmap_domain_mfn_range);
  73. static void xen_percpu_init(void)
  74. {
  75. struct vcpu_register_vcpu_info info;
  76. struct vcpu_info *vcpup;
  77. int err;
  78. int cpu = get_cpu();
  79. pr_info("Xen: initializing cpu%d\n", cpu);
  80. vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
  81. info.mfn = __pa(vcpup) >> PAGE_SHIFT;
  82. info.offset = offset_in_page(vcpup);
  83. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  84. BUG_ON(err);
  85. per_cpu(xen_vcpu, cpu) = vcpup;
  86. enable_percpu_irq(xen_events_irq, 0);
  87. put_cpu();
  88. }
  89. static void xen_restart(enum reboot_mode reboot_mode, const char *cmd)
  90. {
  91. struct sched_shutdown r = { .reason = SHUTDOWN_reboot };
  92. int rc;
  93. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  94. BUG_ON(rc);
  95. }
  96. static void xen_power_off(void)
  97. {
  98. struct sched_shutdown r = { .reason = SHUTDOWN_poweroff };
  99. int rc;
  100. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  101. BUG_ON(rc);
  102. }
  103. static int xen_cpu_notification(struct notifier_block *self,
  104. unsigned long action,
  105. void *hcpu)
  106. {
  107. switch (action) {
  108. case CPU_STARTING:
  109. xen_percpu_init();
  110. break;
  111. default:
  112. break;
  113. }
  114. return NOTIFY_OK;
  115. }
  116. static struct notifier_block xen_cpu_notifier = {
  117. .notifier_call = xen_cpu_notification,
  118. };
  119. static irqreturn_t xen_arm_callback(int irq, void *arg)
  120. {
  121. xen_hvm_evtchn_do_upcall();
  122. return IRQ_HANDLED;
  123. }
  124. /*
  125. * see Documentation/devicetree/bindings/arm/xen.txt for the
  126. * documentation of the Xen Device Tree format.
  127. */
  128. #define GRANT_TABLE_PHYSADDR 0
  129. static int __init xen_guest_init(void)
  130. {
  131. struct xen_add_to_physmap xatp;
  132. static struct shared_info *shared_info_page = 0;
  133. struct device_node *node;
  134. int len;
  135. const char *s = NULL;
  136. const char *version = NULL;
  137. const char *xen_prefix = "xen,xen-";
  138. struct resource res;
  139. phys_addr_t grant_frames;
  140. node = of_find_compatible_node(NULL, NULL, "xen,xen");
  141. if (!node) {
  142. pr_debug("No Xen support\n");
  143. return 0;
  144. }
  145. s = of_get_property(node, "compatible", &len);
  146. if (strlen(xen_prefix) + 3 < len &&
  147. !strncmp(xen_prefix, s, strlen(xen_prefix)))
  148. version = s + strlen(xen_prefix);
  149. if (version == NULL) {
  150. pr_debug("Xen version not found\n");
  151. return 0;
  152. }
  153. if (of_address_to_resource(node, GRANT_TABLE_PHYSADDR, &res))
  154. return 0;
  155. grant_frames = res.start;
  156. xen_events_irq = irq_of_parse_and_map(node, 0);
  157. pr_info("Xen %s support found, events_irq=%d gnttab_frame=%pa\n",
  158. version, xen_events_irq, &grant_frames);
  159. if (xen_events_irq < 0)
  160. return -ENODEV;
  161. xen_domain_type = XEN_HVM_DOMAIN;
  162. xen_setup_features();
  163. if (xen_feature(XENFEAT_dom0))
  164. xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
  165. else
  166. xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
  167. if (!shared_info_page)
  168. shared_info_page = (struct shared_info *)
  169. get_zeroed_page(GFP_KERNEL);
  170. if (!shared_info_page) {
  171. pr_err("not enough memory\n");
  172. return -ENOMEM;
  173. }
  174. xatp.domid = DOMID_SELF;
  175. xatp.idx = 0;
  176. xatp.space = XENMAPSPACE_shared_info;
  177. xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
  178. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  179. BUG();
  180. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  181. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  182. * page, we use it in the event channel upcall and in some pvclock
  183. * related functions.
  184. * The shared info contains exactly 1 CPU (the boot CPU). The guest
  185. * is required to use VCPUOP_register_vcpu_info to place vcpu info
  186. * for secondary CPUs as they are brought up.
  187. * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
  188. */
  189. xen_vcpu_info = __alloc_percpu(sizeof(struct vcpu_info),
  190. sizeof(struct vcpu_info));
  191. if (xen_vcpu_info == NULL)
  192. return -ENOMEM;
  193. if (gnttab_setup_auto_xlat_frames(grant_frames)) {
  194. free_percpu(xen_vcpu_info);
  195. return -ENOMEM;
  196. }
  197. gnttab_init();
  198. if (!xen_initial_domain())
  199. xenbus_probe(NULL);
  200. /*
  201. * Making sure board specific code will not set up ops for
  202. * cpu idle and cpu freq.
  203. */
  204. disable_cpuidle();
  205. disable_cpufreq();
  206. xen_init_IRQ();
  207. if (request_percpu_irq(xen_events_irq, xen_arm_callback,
  208. "events", &xen_vcpu)) {
  209. pr_err("Error request IRQ %d\n", xen_events_irq);
  210. return -EINVAL;
  211. }
  212. xen_percpu_init();
  213. register_cpu_notifier(&xen_cpu_notifier);
  214. return 0;
  215. }
  216. early_initcall(xen_guest_init);
  217. static int __init xen_pm_init(void)
  218. {
  219. if (!xen_domain())
  220. return -ENODEV;
  221. pm_power_off = xen_power_off;
  222. arm_pm_restart = xen_restart;
  223. return 0;
  224. }
  225. late_initcall(xen_pm_init);
  226. /* empty stubs */
  227. void xen_arch_pre_suspend(void) { }
  228. void xen_arch_post_suspend(int suspend_cancelled) { }
  229. void xen_timer_resume(void) { }
  230. void xen_arch_resume(void) { }
  231. void xen_arch_suspend(void) { }
  232. /* In the hypervisor.S file. */
  233. EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
  234. EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
  235. EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
  236. EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
  237. EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
  238. EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
  239. EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
  240. EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
  241. EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
  242. EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
  243. EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
  244. EXPORT_SYMBOL_GPL(privcmd_call);