blob: f28fc1ac87600d4cb25d323b2a6e7aabf19e8849 [file] [log] [blame]
Stefano Stabellini4c071ee2012-09-14 13:53:39 +00001#include <xen/xen.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00002#include <xen/events.h>
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +00003#include <xen/grant_table.h>
4#include <xen/hvm.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +00005#include <xen/interface/xen.h>
6#include <xen/interface/memory.h>
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +00007#include <xen/interface/hvm/params.h>
Stefano Stabellinief61ee0d2012-08-08 17:20:18 +00008#include <xen/features.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +00009#include <xen/platform_pci.h>
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +000010#include <xen/xenbus.h>
Ian Campbellc61ba722012-10-03 12:28:26 +010011#include <xen/page.h>
Ian Campbellf832da02012-10-03 16:37:09 +010012#include <xen/xen-ops.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000013#include <asm/xen/hypervisor.h>
14#include <asm/xen/hypercall.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000015#include <linux/interrupt.h>
16#include <linux/irqreturn.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000017#include <linux/module.h>
Stefano Stabellini2e01f162012-09-14 10:47:52 +000018#include <linux/of.h>
19#include <linux/of_irq.h>
20#include <linux/of_address.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000021
Ian Campbellf832da02012-10-03 16:37:09 +010022#include <linux/mm.h>
23
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000024struct start_info _xen_start_info;
25struct start_info *xen_start_info = &_xen_start_info;
26EXPORT_SYMBOL_GPL(xen_start_info);
27
28enum xen_domain_type xen_domain_type = XEN_NATIVE;
29EXPORT_SYMBOL_GPL(xen_domain_type);
30
31struct shared_info xen_dummy_shared_info;
32struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
33
34DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
35
Ian Campbellc61ba722012-10-03 12:28:26 +010036/* These are unused until we support booting "pre-ballooned" */
37unsigned long xen_released_pages;
38struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
39
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000040/* TODO: to be removed */
41__read_mostly int xen_have_vector_callback;
42EXPORT_SYMBOL_GPL(xen_have_vector_callback);
43
44int xen_platform_pci_unplug = XEN_UNPLUG_ALL;
45EXPORT_SYMBOL_GPL(xen_platform_pci_unplug);
46
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000047static __read_mostly int xen_events_irq = -1;
48
Ian Campbellf832da02012-10-03 16:37:09 +010049/* map fgmfn of domid to lpfn in the current domain */
50static int map_foreign_page(unsigned long lpfn, unsigned long fgmfn,
51 unsigned int domid)
52{
53 int rc;
54 struct xen_add_to_physmap_range xatp = {
55 .domid = DOMID_SELF,
56 .foreign_domid = domid,
57 .size = 1,
58 .space = XENMAPSPACE_gmfn_foreign,
59 };
60 xen_ulong_t idx = fgmfn;
61 xen_pfn_t gpfn = lpfn;
62
63 set_xen_guest_handle(xatp.idxs, &idx);
64 set_xen_guest_handle(xatp.gpfns, &gpfn);
65
66 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
67 if (rc) {
68 pr_warn("Failed to map pfn to mfn rc:%d pfn:%lx mfn:%lx\n",
69 rc, lpfn, fgmfn);
70 return 1;
71 }
72 return 0;
73}
74
75struct remap_data {
76 xen_pfn_t fgmfn; /* foreign domain's gmfn */
77 pgprot_t prot;
78 domid_t domid;
79 struct vm_area_struct *vma;
80 int index;
81 struct page **pages;
82 struct xen_remap_mfn_info *info;
83};
84
85static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
86 void *data)
87{
88 struct remap_data *info = data;
89 struct page *page = info->pages[info->index++];
90 unsigned long pfn = page_to_pfn(page);
91 pte_t pte = pfn_pte(pfn, info->prot);
92
93 if (map_foreign_page(pfn, info->fgmfn, info->domid))
94 return -EFAULT;
95 set_pte_at(info->vma->vm_mm, addr, ptep, pte);
96
97 return 0;
98}
99
Stefano Stabellini4c071ee2012-09-14 13:53:39 +0000100int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
101 unsigned long addr,
Ian Campbellf832da02012-10-03 16:37:09 +0100102 xen_pfn_t mfn, int nr,
103 pgprot_t prot, unsigned domid,
104 struct page **pages)
Stefano Stabellini4c071ee2012-09-14 13:53:39 +0000105{
Ian Campbellf832da02012-10-03 16:37:09 +0100106 int err;
107 struct remap_data data;
108
109 /* TBD: Batching, current sole caller only does page at a time */
110 if (nr > 1)
111 return -EINVAL;
112
113 data.fgmfn = mfn;
114 data.prot = prot;
115 data.domid = domid;
116 data.vma = vma;
117 data.index = 0;
118 data.pages = pages;
119 err = apply_to_page_range(vma->vm_mm, addr, nr << PAGE_SHIFT,
120 remap_pte_fn, &data);
121 return err;
Stefano Stabellini4c071ee2012-09-14 13:53:39 +0000122}
123EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000124
Ian Campbellf832da02012-10-03 16:37:09 +0100125int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
126 int nr, struct page **pages)
127{
128 int i;
129
130 for (i = 0; i < nr; i++) {
131 struct xen_remove_from_physmap xrp;
132 unsigned long rc, pfn;
133
134 pfn = page_to_pfn(pages[i]);
135
136 xrp.domid = DOMID_SELF;
137 xrp.gpfn = pfn;
138 rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
139 if (rc) {
140 pr_warn("Failed to unmap pfn:%lx rc:%ld\n",
141 pfn, rc);
142 return rc;
143 }
144 }
145 return 0;
146}
147EXPORT_SYMBOL_GPL(xen_unmap_domain_mfn_range);
148
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000149/*
150 * see Documentation/devicetree/bindings/arm/xen.txt for the
151 * documentation of the Xen Device Tree format.
152 */
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000153#define GRANT_TABLE_PHYSADDR 0
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000154static int __init xen_guest_init(void)
155{
156 struct xen_add_to_physmap xatp;
157 static struct shared_info *shared_info_page = 0;
158 struct device_node *node;
159 int len;
160 const char *s = NULL;
161 const char *version = NULL;
162 const char *xen_prefix = "xen,xen-";
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000163 struct resource res;
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000164
165 node = of_find_compatible_node(NULL, NULL, "xen,xen");
166 if (!node) {
167 pr_debug("No Xen support\n");
168 return 0;
169 }
170 s = of_get_property(node, "compatible", &len);
171 if (strlen(xen_prefix) + 3 < len &&
172 !strncmp(xen_prefix, s, strlen(xen_prefix)))
173 version = s + strlen(xen_prefix);
174 if (version == NULL) {
175 pr_debug("Xen version not found\n");
176 return 0;
177 }
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000178 if (of_address_to_resource(node, GRANT_TABLE_PHYSADDR, &res))
179 return 0;
180 xen_hvm_resume_frames = res.start >> PAGE_SHIFT;
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000181 xen_events_irq = irq_of_parse_and_map(node, 0);
182 pr_info("Xen %s support found, events_irq=%d gnttab_frame_pfn=%lx\n",
183 version, xen_events_irq, xen_hvm_resume_frames);
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000184 xen_domain_type = XEN_HVM_DOMAIN;
185
Stefano Stabellinief61ee0d2012-08-08 17:20:18 +0000186 xen_setup_features();
187 if (xen_feature(XENFEAT_dom0))
188 xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
189 else
190 xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
191
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000192 if (!shared_info_page)
193 shared_info_page = (struct shared_info *)
194 get_zeroed_page(GFP_KERNEL);
195 if (!shared_info_page) {
196 pr_err("not enough memory\n");
197 return -ENOMEM;
198 }
199 xatp.domid = DOMID_SELF;
200 xatp.idx = 0;
201 xatp.space = XENMAPSPACE_shared_info;
202 xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
203 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
204 BUG();
205
206 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
207
208 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
209 * page, we use it in the event channel upcall and in some pvclock
210 * related functions. We don't need the vcpu_info placement
211 * optimizations because we don't use any pv_mmu or pv_irq op on
212 * HVM.
213 * The shared info contains exactly 1 CPU (the boot CPU). The guest
214 * is required to use VCPUOP_register_vcpu_info to place vcpu info
215 * for secondary CPUs as they are brought up. */
216 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000217
218 gnttab_init();
219 if (!xen_initial_domain())
220 xenbus_probe(NULL);
221
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000222 return 0;
223}
224core_initcall(xen_guest_init);
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000225
226static irqreturn_t xen_arm_callback(int irq, void *arg)
227{
228 xen_hvm_evtchn_do_upcall();
229 return IRQ_HANDLED;
230}
231
232static int __init xen_init_events(void)
233{
234 if (!xen_domain() || xen_events_irq < 0)
235 return -ENODEV;
236
237 xen_init_IRQ();
238
239 if (request_percpu_irq(xen_events_irq, xen_arm_callback,
240 "events", xen_vcpu)) {
241 pr_err("Error requesting IRQ %d\n", xen_events_irq);
242 return -EINVAL;
243 }
244
245 enable_percpu_irq(xen_events_irq, 0);
246
247 return 0;
248}
249postcore_initcall(xen_init_events);