enlighten.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_GPL(xen_start_info);
  31. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  32. EXPORT_SYMBOL_GPL(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. /* map fgmfn of domid to lpfn in the current domain */
  47. static int map_foreign_page(unsigned long lpfn, unsigned long fgmfn,
  48. unsigned int domid)
  49. {
  50. int rc;
  51. struct xen_add_to_physmap_range xatp = {
  52. .domid = DOMID_SELF,
  53. .foreign_domid = domid,
  54. .size = 1,
  55. .space = XENMAPSPACE_gmfn_foreign,
  56. };
  57. xen_ulong_t idx = fgmfn;
  58. xen_pfn_t gpfn = lpfn;
  59. int err = 0;
  60. set_xen_guest_handle(xatp.idxs, &idx);
  61. set_xen_guest_handle(xatp.gpfns, &gpfn);
  62. set_xen_guest_handle(xatp.errs, &err);
  63. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
  64. if (rc || err) {
  65. pr_warn("Failed to map pfn to mfn rc:%d:%d pfn:%lx mfn:%lx\n",
  66. rc, err, lpfn, fgmfn);
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. struct remap_data {
  72. xen_pfn_t fgmfn; /* foreign domain's gmfn */
  73. pgprot_t prot;
  74. domid_t domid;
  75. struct vm_area_struct *vma;
  76. int index;
  77. struct page **pages;
  78. struct xen_remap_mfn_info *info;
  79. };
  80. static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
  81. void *data)
  82. {
  83. struct remap_data *info = data;
  84. struct page *page = info->pages[info->index++];
  85. unsigned long pfn = page_to_pfn(page);
  86. pte_t pte = pte_mkspecial(pfn_pte(pfn, info->prot));
  87. if (map_foreign_page(pfn, info->fgmfn, info->domid))
  88. return -EFAULT;
  89. set_pte_at(info->vma->vm_mm, addr, ptep, pte);
  90. return 0;
  91. }
  92. int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
  93. unsigned long addr,
  94. xen_pfn_t mfn, int nr,
  95. pgprot_t prot, unsigned domid,
  96. struct page **pages)
  97. {
  98. int err;
  99. struct remap_data data;
  100. /* TBD: Batching, current sole caller only does page at a time */
  101. if (nr > 1)
  102. return -EINVAL;
  103. data.fgmfn = mfn;
  104. data.prot = prot;
  105. data.domid = domid;
  106. data.vma = vma;
  107. data.index = 0;
  108. data.pages = pages;
  109. err = apply_to_page_range(vma->vm_mm, addr, nr << PAGE_SHIFT,
  110. remap_pte_fn, &data);
  111. return err;
  112. }
  113. EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
  114. int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
  115. int nr, struct page **pages)
  116. {
  117. int i;
  118. for (i = 0; i < nr; i++) {
  119. struct xen_remove_from_physmap xrp;
  120. unsigned long rc, pfn;
  121. pfn = page_to_pfn(pages[i]);
  122. xrp.domid = DOMID_SELF;
  123. xrp.gpfn = pfn;
  124. rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
  125. if (rc) {
  126. pr_warn("Failed to unmap pfn:%lx rc:%ld\n",
  127. pfn, rc);
  128. return rc;
  129. }
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(xen_unmap_domain_mfn_range);
  134. static void xen_percpu_init(void)
  135. {
  136. struct vcpu_register_vcpu_info info;
  137. struct vcpu_info *vcpup;
  138. int err;
  139. int cpu = get_cpu();
  140. pr_info("Xen: initializing cpu%d\n", cpu);
  141. vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
  142. info.mfn = __pa(vcpup) >> PAGE_SHIFT;
  143. info.offset = offset_in_page(vcpup);
  144. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  145. BUG_ON(err);
  146. per_cpu(xen_vcpu, cpu) = vcpup;
  147. enable_percpu_irq(xen_events_irq, 0);
  148. put_cpu();
  149. }
  150. static void xen_restart(enum reboot_mode reboot_mode, const char *cmd)
  151. {
  152. struct sched_shutdown r = { .reason = SHUTDOWN_reboot };
  153. int rc;
  154. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  155. BUG_ON(rc);
  156. }
  157. static void xen_power_off(void)
  158. {
  159. struct sched_shutdown r = { .reason = SHUTDOWN_poweroff };
  160. int rc;
  161. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  162. BUG_ON(rc);
  163. }
  164. static int xen_cpu_notification(struct notifier_block *self,
  165. unsigned long action,
  166. void *hcpu)
  167. {
  168. switch (action) {
  169. case CPU_STARTING:
  170. xen_percpu_init();
  171. break;
  172. default:
  173. break;
  174. }
  175. return NOTIFY_OK;
  176. }
  177. static struct notifier_block xen_cpu_notifier = {
  178. .notifier_call = xen_cpu_notification,
  179. };
  180. static irqreturn_t xen_arm_callback(int irq, void *arg)
  181. {
  182. xen_hvm_evtchn_do_upcall();
  183. return IRQ_HANDLED;
  184. }
  185. /*
  186. * see Documentation/devicetree/bindings/arm/xen.txt for the
  187. * documentation of the Xen Device Tree format.
  188. */
  189. #define GRANT_TABLE_PHYSADDR 0
  190. static int __init xen_guest_init(void)
  191. {
  192. struct xen_add_to_physmap xatp;
  193. static struct shared_info *shared_info_page = 0;
  194. struct device_node *node;
  195. int len;
  196. const char *s = NULL;
  197. const char *version = NULL;
  198. const char *xen_prefix = "xen,xen-";
  199. struct resource res;
  200. phys_addr_t grant_frames;
  201. node = of_find_compatible_node(NULL, NULL, "xen,xen");
  202. if (!node) {
  203. pr_debug("No Xen support\n");
  204. return 0;
  205. }
  206. s = of_get_property(node, "compatible", &len);
  207. if (strlen(xen_prefix) + 3 < len &&
  208. !strncmp(xen_prefix, s, strlen(xen_prefix)))
  209. version = s + strlen(xen_prefix);
  210. if (version == NULL) {
  211. pr_debug("Xen version not found\n");
  212. return 0;
  213. }
  214. if (of_address_to_resource(node, GRANT_TABLE_PHYSADDR, &res))
  215. return 0;
  216. grant_frames = res.start;
  217. xen_events_irq = irq_of_parse_and_map(node, 0);
  218. pr_info("Xen %s support found, events_irq=%d gnttab_frame=%pa\n",
  219. version, xen_events_irq, &grant_frames);
  220. if (xen_events_irq < 0)
  221. return -ENODEV;
  222. xen_domain_type = XEN_HVM_DOMAIN;
  223. xen_setup_features();
  224. if (!xen_feature(XENFEAT_grant_map_identity)) {
  225. pr_warn("Please upgrade your Xen.\n"
  226. "If your platform has any non-coherent DMA devices, they won't work properly.\n");
  227. }
  228. if (xen_feature(XENFEAT_dom0))
  229. xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
  230. else
  231. xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
  232. if (!shared_info_page)
  233. shared_info_page = (struct shared_info *)
  234. get_zeroed_page(GFP_KERNEL);
  235. if (!shared_info_page) {
  236. pr_err("not enough memory\n");
  237. return -ENOMEM;
  238. }
  239. xatp.domid = DOMID_SELF;
  240. xatp.idx = 0;
  241. xatp.space = XENMAPSPACE_shared_info;
  242. xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
  243. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  244. BUG();
  245. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  246. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  247. * page, we use it in the event channel upcall and in some pvclock
  248. * related functions.
  249. * The shared info contains exactly 1 CPU (the boot CPU). The guest
  250. * is required to use VCPUOP_register_vcpu_info to place vcpu info
  251. * for secondary CPUs as they are brought up.
  252. * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
  253. */
  254. xen_vcpu_info = __alloc_percpu(sizeof(struct vcpu_info),
  255. sizeof(struct vcpu_info));
  256. if (xen_vcpu_info == NULL)
  257. return -ENOMEM;
  258. if (gnttab_setup_auto_xlat_frames(grant_frames)) {
  259. free_percpu(xen_vcpu_info);
  260. return -ENOMEM;
  261. }
  262. gnttab_init();
  263. if (!xen_initial_domain())
  264. xenbus_probe(NULL);
  265. /*
  266. * Making sure board specific code will not set up ops for
  267. * cpu idle and cpu freq.
  268. */
  269. disable_cpuidle();
  270. disable_cpufreq();
  271. xen_init_IRQ();
  272. if (request_percpu_irq(xen_events_irq, xen_arm_callback,
  273. "events", &xen_vcpu)) {
  274. pr_err("Error request IRQ %d\n", xen_events_irq);
  275. return -EINVAL;
  276. }
  277. xen_percpu_init();
  278. register_cpu_notifier(&xen_cpu_notifier);
  279. return 0;
  280. }
  281. early_initcall(xen_guest_init);
  282. static int __init xen_pm_init(void)
  283. {
  284. if (!xen_domain())
  285. return -ENODEV;
  286. pm_power_off = xen_power_off;
  287. arm_pm_restart = xen_restart;
  288. return 0;
  289. }
  290. late_initcall(xen_pm_init);
  291. /* empty stubs */
  292. void xen_arch_pre_suspend(void) { }
  293. void xen_arch_post_suspend(int suspend_cancelled) { }
  294. void xen_timer_resume(void) { }
  295. void xen_arch_resume(void) { }
  296. /* In the hypervisor.S file. */
  297. EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
  298. EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
  299. EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
  300. EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
  301. EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
  302. EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
  303. EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
  304. EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
  305. EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
  306. EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
  307. EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
  308. EXPORT_SYMBOL_GPL(privcmd_call);