blob: a63fedbdcbe9876a413236f41a62c2ffe8c61cba [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +00002/*
3 * Xen stolen ticks accounting.
4 */
5#include <linux/kernel.h>
6#include <linux/kernel_stat.h>
7#include <linux/math64.h>
8#include <linux/gfp.h>
9
Juergen Grossecb23dc2016-05-20 09:26:48 +020010#include <asm/paravirt.h>
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000011#include <asm/xen/hypervisor.h>
12#include <asm/xen/hypercall.h>
13
14#include <xen/events.h>
15#include <xen/features.h>
16#include <xen/interface/xen.h>
17#include <xen/interface/vcpu.h>
18#include <xen/xen-ops.h>
19
20/* runstate info updated by Xen */
21static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate);
22
23/* return an consistent snapshot of 64-bit time/counter value */
24static u64 get64(const u64 *p)
25{
26 u64 ret;
27
28 if (BITS_PER_LONG < 64) {
29 u32 *p32 = (u32 *)p;
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000030 u32 h, l, h2;
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000031
32 /*
33 * Read high then low, and then make sure high is
34 * still the same; this will only loop if low wraps
35 * and carries into high.
36 * XXX some clean way to make this endian-proof?
37 */
38 do {
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000039 h = READ_ONCE(p32[1]);
40 l = READ_ONCE(p32[0]);
41 h2 = READ_ONCE(p32[1]);
42 } while(h2 != h);
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000043
44 ret = (((u64)h) << 32) | l;
45 } else
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000046 ret = READ_ONCE(*p);
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000047
48 return ret;
49}
50
Juergen Gross6ba286a2016-07-06 07:00:30 +020051static void xen_get_runstate_snapshot_cpu(struct vcpu_runstate_info *res,
52 unsigned int cpu)
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000053{
54 u64 state_time;
55 struct vcpu_runstate_info *state;
56
57 BUG_ON(preemptible());
58
Juergen Gross6ba286a2016-07-06 07:00:30 +020059 state = per_cpu_ptr(&xen_runstate, cpu);
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000060
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000061 do {
62 state_time = get64(&state->state_entry_time);
Juergen Gross6ba286a2016-07-06 07:00:30 +020063 rmb(); /* Hypervisor might update data. */
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000064 *res = READ_ONCE(*state);
Juergen Gross6ba286a2016-07-06 07:00:30 +020065 rmb(); /* Hypervisor might update data. */
66 } while (get64(&state->state_entry_time) != state_time ||
67 (state_time & XEN_RUNSTATE_UPDATE));
68}
69
70/*
71 * Runstate accounting
72 */
73void xen_get_runstate_snapshot(struct vcpu_runstate_info *res)
74{
75 xen_get_runstate_snapshot_cpu(res, smp_processor_id());
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000076}
77
78/* return true when a vcpu could run but has no real cpu to run on */
79bool xen_vcpu_stolen(int vcpu)
80{
81 return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable;
82}
83
Juergen Grossd34c30c2016-07-26 14:15:11 +020084u64 xen_steal_clock(int cpu)
Juergen Grossecb23dc2016-05-20 09:26:48 +020085{
86 struct vcpu_runstate_info state;
87
Juergen Gross6ba286a2016-07-06 07:00:30 +020088 xen_get_runstate_snapshot_cpu(&state, cpu);
Juergen Grossecb23dc2016-05-20 09:26:48 +020089 return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline];
90}
91
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000092void xen_setup_runstate_info(int cpu)
93{
94 struct vcpu_register_runstate_memory_area area;
95
96 area.addr.v = &per_cpu(xen_runstate, cpu);
97
98 if (HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area,
Vitaly Kuznetsovad5475f2016-06-30 17:56:38 +020099 xen_vcpu_nr(cpu), &area))
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +0000100 BUG();
101}
102
Juergen Grossecb23dc2016-05-20 09:26:48 +0200103void __init xen_time_setup_guest(void)
104{
Juergen Gross6ba286a2016-07-06 07:00:30 +0200105 bool xen_runstate_remote;
106
107 xen_runstate_remote = !HYPERVISOR_vm_assist(VMASST_CMD_enable,
108 VMASST_TYPE_runstate_update_flag);
109
Juergen Grossecb23dc2016-05-20 09:26:48 +0200110 pv_time_ops.steal_clock = xen_steal_clock;
111
112 static_key_slow_inc(&paravirt_steal_enabled);
Juergen Gross6ba286a2016-07-06 07:00:30 +0200113 if (xen_runstate_remote)
114 static_key_slow_inc(&paravirt_steal_rq_enabled);
Juergen Grossecb23dc2016-05-20 09:26:48 +0200115}