blob: 2257b666376647452186fbf5c50529edc03b1e3f [file] [log] [blame]
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +00001/*
2 * Xen stolen ticks accounting.
3 */
4#include <linux/kernel.h>
5#include <linux/kernel_stat.h>
6#include <linux/math64.h>
7#include <linux/gfp.h>
8
Juergen Grossecb23dc2016-05-20 09:26:48 +02009#include <asm/paravirt.h>
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000010#include <asm/xen/hypervisor.h>
11#include <asm/xen/hypercall.h>
12
13#include <xen/events.h>
14#include <xen/features.h>
15#include <xen/interface/xen.h>
16#include <xen/interface/vcpu.h>
17#include <xen/xen-ops.h>
18
19/* runstate info updated by Xen */
20static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate);
21
22/* return an consistent snapshot of 64-bit time/counter value */
23static u64 get64(const u64 *p)
24{
25 u64 ret;
26
27 if (BITS_PER_LONG < 64) {
28 u32 *p32 = (u32 *)p;
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000029 u32 h, l, h2;
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000030
31 /*
32 * Read high then low, and then make sure high is
33 * still the same; this will only loop if low wraps
34 * and carries into high.
35 * XXX some clean way to make this endian-proof?
36 */
37 do {
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000038 h = READ_ONCE(p32[1]);
39 l = READ_ONCE(p32[0]);
40 h2 = READ_ONCE(p32[1]);
41 } while(h2 != h);
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000042
43 ret = (((u64)h) << 32) | l;
44 } else
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000045 ret = READ_ONCE(*p);
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000046
47 return ret;
48}
49
50/*
51 * Runstate accounting
52 */
53void xen_get_runstate_snapshot(struct vcpu_runstate_info *res)
54{
55 u64 state_time;
56 struct vcpu_runstate_info *state;
57
58 BUG_ON(preemptible());
59
60 state = this_cpu_ptr(&xen_runstate);
61
62 /*
63 * The runstate info is always updated by the hypervisor on
64 * the current CPU, so there's no need to use anything
65 * stronger than a compiler barrier when fetching it.
66 */
67 do {
68 state_time = get64(&state->state_entry_time);
Stefano Stabellini2dd887e2015-11-20 15:02:44 +000069 *res = READ_ONCE(*state);
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000070 } while (get64(&state->state_entry_time) != state_time);
71}
72
73/* return true when a vcpu could run but has no real cpu to run on */
74bool xen_vcpu_stolen(int vcpu)
75{
76 return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable;
77}
78
Juergen Grossecb23dc2016-05-20 09:26:48 +020079static u64 xen_steal_clock(int cpu)
80{
81 struct vcpu_runstate_info state;
82
83 BUG_ON(cpu != smp_processor_id());
84 xen_get_runstate_snapshot(&state);
85 return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline];
86}
87
Stefano Stabellini4ccefbe2015-11-05 15:15:07 +000088void xen_setup_runstate_info(int cpu)
89{
90 struct vcpu_register_runstate_memory_area area;
91
92 area.addr.v = &per_cpu(xen_runstate, cpu);
93
94 if (HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area,
95 cpu, &area))
96 BUG();
97}
98
Juergen Grossecb23dc2016-05-20 09:26:48 +020099void __init xen_time_setup_guest(void)
100{
101 pv_time_ops.steal_clock = xen_steal_clock;
102
103 static_key_slow_inc(&paravirt_steal_enabled);
104 /*
105 * We can't set paravirt_steal_rq_enabled as this would require the
106 * capability to read another cpu's runstate info.
107 */
108}