blob: 64f17264f2f5efa635b64db7060539c652c82c31 [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>
Stefano Stabellini6abb7492013-04-25 10:23:07 +000013#include <xen/interface/sched.h>
Ian Campbellf832da02012-10-03 16:37:09 +010014#include <xen/xen-ops.h>
Stefano Stabellini34e38522015-11-23 10:35:12 +000015#include <asm/paravirt.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000016#include <asm/xen/hypervisor.h>
17#include <asm/xen/hypercall.h>
Stefano Stabellini6abb7492013-04-25 10:23:07 +000018#include <asm/system_misc.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000019#include <linux/interrupt.h>
20#include <linux/irqreturn.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000021#include <linux/module.h>
Stefano Stabellini2e01f162012-09-14 10:47:52 +000022#include <linux/of.h>
23#include <linux/of_irq.h>
24#include <linux/of_address.h>
Julien Gralle1a9c162013-09-09 11:35:26 +000025#include <linux/cpuidle.h>
26#include <linux/cpufreq.h>
Julien Grall8b271d52014-01-30 12:52:59 +000027#include <linux/cpu.h>
Ard Biesheuvelf1dddd12015-05-06 14:14:22 +000028#include <linux/console.h>
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000029
Ian Campbellf832da02012-10-03 16:37:09 +010030#include <linux/mm.h>
31
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000032struct start_info _xen_start_info;
33struct start_info *xen_start_info = &_xen_start_info;
Chuck Tuffli35c8ab42014-12-21 12:30:58 -080034EXPORT_SYMBOL(xen_start_info);
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000035
36enum xen_domain_type xen_domain_type = XEN_NATIVE;
Chuck Tuffli35c8ab42014-12-21 12:30:58 -080037EXPORT_SYMBOL(xen_domain_type);
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000038
39struct shared_info xen_dummy_shared_info;
40struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
41
42DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +000043static struct vcpu_info __percpu *xen_vcpu_info;
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000044
Ian Campbellc61ba722012-10-03 12:28:26 +010045/* These are unused until we support booting "pre-ballooned" */
46unsigned long xen_released_pages;
47struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
48
Julien Grall81e863c2015-05-06 14:09:06 +000049static __read_mostly unsigned int xen_events_irq;
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000050
Stefano Stabellini5882bfe2015-05-06 14:13:31 +000051static __initdata struct device_node *xen_node;
52
Julien Gralla13d7202015-08-07 17:34:41 +010053int xen_remap_domain_gfn_array(struct vm_area_struct *vma,
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000054 unsigned long addr,
Julien Gralla13d7202015-08-07 17:34:41 +010055 xen_pfn_t *gfn, int nr,
David Vrabel4e8c0c82015-03-11 14:49:57 +000056 int *err_ptr, pgprot_t prot,
57 unsigned domid,
Ian Campbellf832da02012-10-03 16:37:09 +010058 struct page **pages)
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000059{
Julien Gralla13d7202015-08-07 17:34:41 +010060 return xen_xlate_remap_gfn_array(vma, addr, gfn, nr, err_ptr,
David Vrabel628c28e2015-03-11 14:49:56 +000061 prot, domid, pages);
Stefano Stabellini4c071ee2012-09-14 13:53:39 +000062}
Julien Gralla13d7202015-08-07 17:34:41 +010063EXPORT_SYMBOL_GPL(xen_remap_domain_gfn_array);
David Vrabel4e8c0c82015-03-11 14:49:57 +000064
65/* Not used by XENFEAT_auto_translated guests. */
Julien Gralla13d7202015-08-07 17:34:41 +010066int xen_remap_domain_gfn_range(struct vm_area_struct *vma,
David Vrabel4e8c0c82015-03-11 14:49:57 +000067 unsigned long addr,
Julien Gralla13d7202015-08-07 17:34:41 +010068 xen_pfn_t gfn, int nr,
David Vrabel4e8c0c82015-03-11 14:49:57 +000069 pgprot_t prot, unsigned domid,
70 struct page **pages)
71{
72 return -ENOSYS;
73}
Julien Gralla13d7202015-08-07 17:34:41 +010074EXPORT_SYMBOL_GPL(xen_remap_domain_gfn_range);
Stefano Stabellini2e01f162012-09-14 10:47:52 +000075
Julien Gralla13d7202015-08-07 17:34:41 +010076int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
Ian Campbellf832da02012-10-03 16:37:09 +010077 int nr, struct page **pages)
78{
David Vrabel628c28e2015-03-11 14:49:56 +000079 return xen_xlate_unmap_gfn_range(vma, nr, pages);
Ian Campbellf832da02012-10-03 16:37:09 +010080}
Julien Gralla13d7202015-08-07 17:34:41 +010081EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range);
Ian Campbellf832da02012-10-03 16:37:09 +010082
Stefano Stabellini34e38522015-11-23 10:35:12 +000083static unsigned long long xen_stolen_accounting(int cpu)
84{
85 struct vcpu_runstate_info state;
86
87 BUG_ON(cpu != smp_processor_id());
88
89 xen_get_runstate_snapshot(&state);
90
91 WARN_ON(state.state != RUNSTATE_running);
92
93 return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline];
94}
95
Julien Grall8b271d52014-01-30 12:52:59 +000096static void xen_percpu_init(void)
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +000097{
98 struct vcpu_register_vcpu_info info;
99 struct vcpu_info *vcpup;
100 int err;
Stefano Stabellini3cc8e402013-05-08 11:59:01 +0000101 int cpu = get_cpu();
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000102
Stefano Stabellinicb9644b2015-10-22 16:22:46 +0000103 /*
104 * VCPUOP_register_vcpu_info cannot be called twice for the same
105 * vcpu, so if vcpu_info is already registered, just get out. This
106 * can happen with cpu-hotplug.
107 */
108 if (per_cpu(xen_vcpu, cpu) != NULL)
109 goto after_register_vcpu_info;
110
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000111 pr_info("Xen: initializing cpu%d\n", cpu);
112 vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
113
Julien Grall250c9af2015-05-05 16:36:56 +0100114 info.mfn = virt_to_gfn(vcpup);
115 info.offset = xen_offset_in_page(vcpup);
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000116
117 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
Stefano Stabellinid7266d72013-05-08 13:02:38 +0000118 BUG_ON(err);
119 per_cpu(xen_vcpu, cpu) = vcpup;
120
Stefano Stabellini34e38522015-11-23 10:35:12 +0000121 xen_setup_runstate_info(cpu);
122
Stefano Stabellinicb9644b2015-10-22 16:22:46 +0000123after_register_vcpu_info:
Stefano Stabellini3cc8e402013-05-08 11:59:01 +0000124 enable_percpu_irq(xen_events_irq, 0);
Julien Grall0d7febe2013-07-29 17:06:05 +0100125 put_cpu();
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000126}
127
Stefano Stabellini2451ade2013-07-21 15:17:54 +0000128static void xen_restart(enum reboot_mode reboot_mode, const char *cmd)
Stefano Stabellini6abb7492013-04-25 10:23:07 +0000129{
130 struct sched_shutdown r = { .reason = SHUTDOWN_reboot };
131 int rc;
132 rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
Himangi Saraogia91c7772014-07-25 12:05:39 -0400133 BUG_ON(rc);
Stefano Stabellini6abb7492013-04-25 10:23:07 +0000134}
135
136static void xen_power_off(void)
137{
138 struct sched_shutdown r = { .reason = SHUTDOWN_poweroff };
139 int rc;
140 rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
Himangi Saraogia91c7772014-07-25 12:05:39 -0400141 BUG_ON(rc);
Stefano Stabellini6abb7492013-04-25 10:23:07 +0000142}
143
Julien Grall8b271d52014-01-30 12:52:59 +0000144static int xen_cpu_notification(struct notifier_block *self,
145 unsigned long action,
146 void *hcpu)
147{
148 switch (action) {
149 case CPU_STARTING:
150 xen_percpu_init();
151 break;
Stefano Stabellinicb9644b2015-10-22 16:22:46 +0000152 case CPU_DYING:
153 disable_percpu_irq(xen_events_irq);
154 break;
Julien Grall8b271d52014-01-30 12:52:59 +0000155 default:
156 break;
157 }
158
159 return NOTIFY_OK;
160}
161
162static struct notifier_block xen_cpu_notifier = {
163 .notifier_call = xen_cpu_notification,
164};
165
166static irqreturn_t xen_arm_callback(int irq, void *arg)
167{
168 xen_hvm_evtchn_do_upcall();
169 return IRQ_HANDLED;
170}
171
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000172/*
173 * see Documentation/devicetree/bindings/arm/xen.txt for the
174 * documentation of the Xen Device Tree format.
175 */
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000176#define GRANT_TABLE_PHYSADDR 0
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000177void __init xen_early_init(void)
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000178{
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000179 int len;
180 const char *s = NULL;
181 const char *version = NULL;
182 const char *xen_prefix = "xen,xen-";
183
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000184 xen_node = of_find_compatible_node(NULL, NULL, "xen,xen");
185 if (!xen_node) {
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000186 pr_debug("No Xen support\n");
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000187 return;
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000188 }
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000189 s = of_get_property(xen_node, "compatible", &len);
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000190 if (strlen(xen_prefix) + 3 < len &&
191 !strncmp(xen_prefix, s, strlen(xen_prefix)))
192 version = s + strlen(xen_prefix);
193 if (version == NULL) {
194 pr_debug("Xen version not found\n");
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000195 return;
Julien Grall81e863c2015-05-06 14:09:06 +0000196 }
197
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000198 pr_info("Xen %s support found\n", version);
Julien Grall8b271d52014-01-30 12:52:59 +0000199
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000200 xen_domain_type = XEN_HVM_DOMAIN;
201
Stefano Stabellinief61ee0d2012-08-08 17:20:18 +0000202 xen_setup_features();
Stefano Stabellini5ebc77d2014-09-10 22:49:30 +0000203
Stefano Stabellinief61ee0d2012-08-08 17:20:18 +0000204 if (xen_feature(XENFEAT_dom0))
205 xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
206 else
207 xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
Ard Biesheuvelf1dddd12015-05-06 14:14:22 +0000208
209 if (!console_set_on_cmdline && !xen_initial_domain())
210 add_preferred_console("hvc", 0, NULL);
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000211}
Stefano Stabellinief61ee0d2012-08-08 17:20:18 +0000212
Stefano Stabellini5882bfe2015-05-06 14:13:31 +0000213static int __init xen_guest_init(void)
214{
215 struct xen_add_to_physmap xatp;
216 struct shared_info *shared_info_page = NULL;
217 struct resource res;
218 phys_addr_t grant_frames;
219
220 if (!xen_domain())
221 return 0;
222
223 if (of_address_to_resource(xen_node, GRANT_TABLE_PHYSADDR, &res)) {
224 pr_err("Xen grant table base address not found\n");
225 return -ENODEV;
226 }
227 grant_frames = res.start;
228
229 xen_events_irq = irq_of_parse_and_map(xen_node, 0);
230 if (!xen_events_irq) {
231 pr_err("Xen event channel interrupt not found\n");
232 return -ENODEV;
233 }
234
235 shared_info_page = (struct shared_info *)get_zeroed_page(GFP_KERNEL);
236
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000237 if (!shared_info_page) {
238 pr_err("not enough memory\n");
239 return -ENOMEM;
240 }
241 xatp.domid = DOMID_SELF;
242 xatp.idx = 0;
243 xatp.space = XENMAPSPACE_shared_info;
Julien Grall250c9af2015-05-05 16:36:56 +0100244 xatp.gpfn = virt_to_gfn(shared_info_page);
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000245 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
246 BUG();
247
248 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
249
250 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
251 * page, we use it in the event channel upcall and in some pvclock
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000252 * related functions.
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000253 * The shared info contains exactly 1 CPU (the boot CPU). The guest
254 * is required to use VCPUOP_register_vcpu_info to place vcpu info
Stefano Stabellini9a9ab3c2013-04-25 16:17:04 +0000255 * for secondary CPUs as they are brought up.
256 * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
257 */
258 xen_vcpu_info = __alloc_percpu(sizeof(struct vcpu_info),
259 sizeof(struct vcpu_info));
260 if (xen_vcpu_info == NULL)
261 return -ENOMEM;
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000262
Konrad Rzeszutek Wilkefaf30a2014-01-06 10:40:36 -0500263 if (gnttab_setup_auto_xlat_frames(grant_frames)) {
264 free_percpu(xen_vcpu_info);
265 return -ENOMEM;
266 }
Stefano Stabellinib3b52fd2012-09-13 12:06:52 +0000267 gnttab_init();
268 if (!xen_initial_domain())
269 xenbus_probe(NULL);
270
Julien Gralle1a9c162013-09-09 11:35:26 +0000271 /*
272 * Making sure board specific code will not set up ops for
273 * cpu idle and cpu freq.
274 */
275 disable_cpuidle();
276 disable_cpufreq();
277
Julien Grall8b271d52014-01-30 12:52:59 +0000278 xen_init_IRQ();
279
280 if (request_percpu_irq(xen_events_irq, xen_arm_callback,
281 "events", &xen_vcpu)) {
282 pr_err("Error request IRQ %d\n", xen_events_irq);
283 return -EINVAL;
284 }
285
286 xen_percpu_init();
287
288 register_cpu_notifier(&xen_cpu_notifier);
289
Stefano Stabellini34e38522015-11-23 10:35:12 +0000290 pv_time_ops.steal_clock = xen_stolen_accounting;
291 static_key_slow_inc(&paravirt_steal_enabled);
292
Stefano Stabellini1aa3d8d2013-05-08 11:59:01 +0000293 return 0;
294}
Julien Grall8b271d52014-01-30 12:52:59 +0000295early_initcall(xen_guest_init);
Stefano Stabellini1aa3d8d2013-05-08 11:59:01 +0000296
297static int __init xen_pm_init(void)
298{
Rob Herring9dd4b292013-08-29 07:43:52 -0500299 if (!xen_domain())
300 return -ENODEV;
301
Stefano Stabellini6abb7492013-04-25 10:23:07 +0000302 pm_power_off = xen_power_off;
303 arm_pm_restart = xen_restart;
304
Stefano Stabellini2e01f162012-09-14 10:47:52 +0000305 return 0;
306}
Rob Herring9dd4b292013-08-29 07:43:52 -0500307late_initcall(xen_pm_init);
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +0000308
Stefano Stabellini79390282014-05-08 16:54:02 +0100309
310/* empty stubs */
311void xen_arch_pre_suspend(void) { }
312void xen_arch_post_suspend(int suspend_cancelled) { }
313void xen_timer_resume(void) { }
314void xen_arch_resume(void) { }
Boris Ostrovskyffb7dbe2015-05-07 12:55:23 -0400315void xen_arch_suspend(void) { }
Stefano Stabellini79390282014-05-08 16:54:02 +0100316
317
Juergen Grossd5f985c2015-09-14 15:20:52 +0200318/* In the hypercall.S file. */
Konrad Rzeszutek Wilk911dec02012-11-06 17:06:52 -0500319EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
320EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
Stefano Stabelliniab277bb2012-11-08 15:58:55 +0000321EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
322EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
323EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
324EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
325EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
326EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
Stefano Stabelliniea0af612013-04-25 13:53:05 +0000327EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
Stefano Stabellini176455e2013-07-02 10:42:40 +0000328EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
Stefano Stabellini72d39c62015-11-23 10:37:12 +0000329EXPORT_SYMBOL_GPL(HYPERVISOR_platform_op);
Ian Campbell9f1d34142014-05-09 16:10:49 +0000330EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
Konrad Rzeszutek Wilk911dec02012-11-06 17:06:52 -0500331EXPORT_SYMBOL_GPL(privcmd_call);