blob: 6c87d116df63c92748ee2ef1761cc1d752c94bc1 [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 Stabellini9a9ab3c2013-04-25 16:17:04 +00005#include <xen/interface/vcpu.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +00006#include <xen/interface/xen.h>
7#include <xen/interface/memory.h>
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +00008#include <xen/interface/hvm/params.h>
Stefano Stabellinief61ee0d2012-08-08 17:20:18 +00009#include <xen/features.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000010#include <xen/platform_pci.h>
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +000011#include <xen/xenbus.h>
Ian Campbellc61ba722012-10-03 12:28:26 +010012#include <xen/page.h>
Ian Campbellf832da02012-10-03 16:37:09 +010013#include <xen/xen-ops.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000014#include <asm/xen/hypervisor.h>
15#include <asm/xen/hypercall.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000016#include <linux/interrupt.h>
17#include <linux/irqreturn.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000018#include <linux/module.h>
Stefano Stabellini2e01f162012-09-14 10:47:52 +000019#include <linux/of.h>
20#include <linux/of_irq.h>
21#include <linux/of_address.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000022
Ian Campbellf832da02012-10-03 16:37:09 +010023#include <linux/mm.h>
24
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000025struct start_info _xen_start_info;
26struct start_info *xen_start_info = &_xen_start_info;
27EXPORT_SYMBOL_GPL(xen_start_info);
28
29enum xen_domain_type xen_domain_type = XEN_NATIVE;
30EXPORT_SYMBOL_GPL(xen_domain_type);
31
32struct shared_info xen_dummy_shared_info;
33struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
34
35DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +000036static struct vcpu_info __percpu *xen_vcpu_info;
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000037
Ian Campbellc61ba722012-10-03 12:28:26 +010038/* These are unused until we support booting "pre-ballooned" */
39unsigned long xen_released_pages;
40struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
41
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000042/* TODO: to be removed */
43__read_mostly int xen_have_vector_callback;
44EXPORT_SYMBOL_GPL(xen_have_vector_callback);
45
46int xen_platform_pci_unplug = XEN_UNPLUG_ALL;
47EXPORT_SYMBOL_GPL(xen_platform_pci_unplug);
48
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000049static __read_mostly int xen_events_irq = -1;
50
Ian Campbellf832da02012-10-03 16:37:09 +010051/* map fgmfn of domid to lpfn in the current domain */
52static int map_foreign_page(unsigned long lpfn, unsigned long fgmfn,
53 unsigned int domid)
54{
55 int rc;
56 struct xen_add_to_physmap_range xatp = {
57 .domid = DOMID_SELF,
58 .foreign_domid = domid,
59 .size = 1,
60 .space = XENMAPSPACE_gmfn_foreign,
61 };
62 xen_ulong_t idx = fgmfn;
63 xen_pfn_t gpfn = lpfn;
Ian Campbell07d0c942013-02-19 22:00:58 -050064 int err = 0;
Ian Campbellf832da02012-10-03 16:37:09 +010065
66 set_xen_guest_handle(xatp.idxs, &idx);
67 set_xen_guest_handle(xatp.gpfns, &gpfn);
Ian Campbell07d0c942013-02-19 22:00:58 -050068 set_xen_guest_handle(xatp.errs, &err);
Ian Campbellf832da02012-10-03 16:37:09 +010069
70 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
Ian Campbell07d0c942013-02-19 22:00:58 -050071 if (rc || err) {
72 pr_warn("Failed to map pfn to mfn rc:%d:%d pfn:%lx mfn:%lx\n",
73 rc, err, lpfn, fgmfn);
Ian Campbellf832da02012-10-03 16:37:09 +010074 return 1;
75 }
76 return 0;
77}
78
79struct remap_data {
80 xen_pfn_t fgmfn; /* foreign domain's gmfn */
81 pgprot_t prot;
82 domid_t domid;
83 struct vm_area_struct *vma;
84 int index;
85 struct page **pages;
86 struct xen_remap_mfn_info *info;
87};
88
89static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
90 void *data)
91{
92 struct remap_data *info = data;
93 struct page *page = info->pages[info->index++];
94 unsigned long pfn = page_to_pfn(page);
95 pte_t pte = pfn_pte(pfn, info->prot);
96
97 if (map_foreign_page(pfn, info->fgmfn, info->domid))
98 return -EFAULT;
99 set_pte_at(info->vma->vm_mm, addr, ptep, pte);
100
101 return 0;
102}
103
Stefano Stabellini4c071ee2012-09-14 13:53:39 +0000104int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
105 unsigned long addr,
Ian Campbellf832da02012-10-03 16:37:09 +0100106 xen_pfn_t mfn, int nr,
107 pgprot_t prot, unsigned domid,
108 struct page **pages)
Stefano Stabellini4c071ee2012-09-14 13:53:39 +0000109{
Ian Campbellf832da02012-10-03 16:37:09 +0100110 int err;
111 struct remap_data data;
112
113 /* TBD: Batching, current sole caller only does page at a time */
114 if (nr > 1)
115 return -EINVAL;
116
117 data.fgmfn = mfn;
118 data.prot = prot;
119 data.domid = domid;
120 data.vma = vma;
121 data.index = 0;
122 data.pages = pages;
123 err = apply_to_page_range(vma->vm_mm, addr, nr << PAGE_SHIFT,
124 remap_pte_fn, &data);
125 return err;
Stefano Stabellini4c071ee2012-09-14 13:53:39 +0000126}
127EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000128
Ian Campbellf832da02012-10-03 16:37:09 +0100129int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
130 int nr, struct page **pages)
131{
132 int i;
133
134 for (i = 0; i < nr; i++) {
135 struct xen_remove_from_physmap xrp;
136 unsigned long rc, pfn;
137
138 pfn = page_to_pfn(pages[i]);
139
140 xrp.domid = DOMID_SELF;
141 xrp.gpfn = pfn;
142 rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
143 if (rc) {
144 pr_warn("Failed to unmap pfn:%lx rc:%ld\n",
145 pfn, rc);
146 return rc;
147 }
148 }
149 return 0;
150}
151EXPORT_SYMBOL_GPL(xen_unmap_domain_mfn_range);
152
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000153static int __init xen_secondary_init(unsigned int cpu)
154{
155 struct vcpu_register_vcpu_info info;
156 struct vcpu_info *vcpup;
157 int err;
158
159 pr_info("Xen: initializing cpu%d\n", cpu);
160 vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
161
162 info.mfn = __pa(vcpup) >> PAGE_SHIFT;
163 info.offset = offset_in_page(vcpup);
164
165 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
166 if (err) {
167 pr_debug("register_vcpu_info failed: err=%d\n", err);
168 } else {
169 /* This cpu is using the registered vcpu info, even if
170 later ones fail to. */
171 per_cpu(xen_vcpu, cpu) = vcpup;
172 }
173 return 0;
174}
175
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000176/*
177 * see Documentation/devicetree/bindings/arm/xen.txt for the
178 * documentation of the Xen Device Tree format.
179 */
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000180#define GRANT_TABLE_PHYSADDR 0
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000181static int __init xen_guest_init(void)
182{
183 struct xen_add_to_physmap xatp;
184 static struct shared_info *shared_info_page = 0;
185 struct device_node *node;
186 int len;
187 const char *s = NULL;
188 const char *version = NULL;
189 const char *xen_prefix = "xen,xen-";
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000190 struct resource res;
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000191 int i;
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000192
193 node = of_find_compatible_node(NULL, NULL, "xen,xen");
194 if (!node) {
195 pr_debug("No Xen support\n");
196 return 0;
197 }
198 s = of_get_property(node, "compatible", &len);
199 if (strlen(xen_prefix) + 3 < len &&
200 !strncmp(xen_prefix, s, strlen(xen_prefix)))
201 version = s + strlen(xen_prefix);
202 if (version == NULL) {
203 pr_debug("Xen version not found\n");
204 return 0;
205 }
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000206 if (of_address_to_resource(node, GRANT_TABLE_PHYSADDR, &res))
207 return 0;
208 xen_hvm_resume_frames = res.start >> PAGE_SHIFT;
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000209 xen_events_irq = irq_of_parse_and_map(node, 0);
210 pr_info("Xen %s support found, events_irq=%d gnttab_frame_pfn=%lx\n",
211 version, xen_events_irq, xen_hvm_resume_frames);
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000212 xen_domain_type = XEN_HVM_DOMAIN;
213
Stefano Stabellinief61ee0d2012-08-08 17:20:18 +0000214 xen_setup_features();
215 if (xen_feature(XENFEAT_dom0))
216 xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
217 else
218 xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
219
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000220 if (!shared_info_page)
221 shared_info_page = (struct shared_info *)
222 get_zeroed_page(GFP_KERNEL);
223 if (!shared_info_page) {
224 pr_err("not enough memory\n");
225 return -ENOMEM;
226 }
227 xatp.domid = DOMID_SELF;
228 xatp.idx = 0;
229 xatp.space = XENMAPSPACE_shared_info;
230 xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
231 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
232 BUG();
233
234 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
235
236 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
237 * page, we use it in the event channel upcall and in some pvclock
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000238 * related functions.
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000239 * The shared info contains exactly 1 CPU (the boot CPU). The guest
240 * is required to use VCPUOP_register_vcpu_info to place vcpu info
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000241 * for secondary CPUs as they are brought up.
242 * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
243 */
244 xen_vcpu_info = __alloc_percpu(sizeof(struct vcpu_info),
245 sizeof(struct vcpu_info));
246 if (xen_vcpu_info == NULL)
247 return -ENOMEM;
248 for_each_online_cpu(i)
249 xen_secondary_init(i);
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000250
251 gnttab_init();
252 if (!xen_initial_domain())
253 xenbus_probe(NULL);
254
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000255 return 0;
256}
257core_initcall(xen_guest_init);
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000258
259static irqreturn_t xen_arm_callback(int irq, void *arg)
260{
261 xen_hvm_evtchn_do_upcall();
262 return IRQ_HANDLED;
263}
264
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000265static __init void xen_percpu_enable_events(void *unused)
266{
267 enable_percpu_irq(xen_events_irq, 0);
268}
269
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000270static int __init xen_init_events(void)
271{
272 if (!xen_domain() || xen_events_irq < 0)
273 return -ENODEV;
274
275 xen_init_IRQ();
276
277 if (request_percpu_irq(xen_events_irq, xen_arm_callback,
Stefano Stabellini2798ba72013-04-25 13:53:09 +0000278 "events", &xen_vcpu)) {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000279 pr_err("Error requesting IRQ %d\n", xen_events_irq);
280 return -EINVAL;
281 }
282
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000283 on_each_cpu(xen_percpu_enable_events, NULL, 0);
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000284
285 return 0;
286}
287postcore_initcall(xen_init_events);
Stefano Stabelliniea542092012-08-08 17:20:58 +0000288
Konrad Rzeszutek Wilk911dec02012-11-06 17:06:52 -0500289/* In the hypervisor.S file. */
290EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
291EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
Stefano Stabelliniab277bb2012-11-08 15:58:55 +0000292EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
293EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
294EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
295EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
296EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
297EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
Stefano Stabelliniea0af612013-04-25 13:53:05 +0000298EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
Konrad Rzeszutek Wilk911dec02012-11-06 17:06:52 -0500299EXPORT_SYMBOL_GPL(privcmd_call);