Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 2 | /* |
| 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 Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 10 | #include <asm/paravirt.h> |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 11 | #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 */ |
| 21 | static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate); |
| 22 | |
| 23 | /* return an consistent snapshot of 64-bit time/counter value */ |
| 24 | static u64 get64(const u64 *p) |
| 25 | { |
| 26 | u64 ret; |
| 27 | |
| 28 | if (BITS_PER_LONG < 64) { |
| 29 | u32 *p32 = (u32 *)p; |
Stefano Stabellini | 2dd887e | 2015-11-20 15:02:44 +0000 | [diff] [blame] | 30 | u32 h, l, h2; |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 31 | |
| 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 Stabellini | 2dd887e | 2015-11-20 15:02:44 +0000 | [diff] [blame] | 39 | h = READ_ONCE(p32[1]); |
| 40 | l = READ_ONCE(p32[0]); |
| 41 | h2 = READ_ONCE(p32[1]); |
| 42 | } while(h2 != h); |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 43 | |
| 44 | ret = (((u64)h) << 32) | l; |
| 45 | } else |
Stefano Stabellini | 2dd887e | 2015-11-20 15:02:44 +0000 | [diff] [blame] | 46 | ret = READ_ONCE(*p); |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | |
Juergen Gross | 6ba286a | 2016-07-06 07:00:30 +0200 | [diff] [blame] | 51 | static void xen_get_runstate_snapshot_cpu(struct vcpu_runstate_info *res, |
| 52 | unsigned int cpu) |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 53 | { |
| 54 | u64 state_time; |
| 55 | struct vcpu_runstate_info *state; |
| 56 | |
| 57 | BUG_ON(preemptible()); |
| 58 | |
Juergen Gross | 6ba286a | 2016-07-06 07:00:30 +0200 | [diff] [blame] | 59 | state = per_cpu_ptr(&xen_runstate, cpu); |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 60 | |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 61 | do { |
| 62 | state_time = get64(&state->state_entry_time); |
Juergen Gross | 6ba286a | 2016-07-06 07:00:30 +0200 | [diff] [blame] | 63 | rmb(); /* Hypervisor might update data. */ |
Stefano Stabellini | 2dd887e | 2015-11-20 15:02:44 +0000 | [diff] [blame] | 64 | *res = READ_ONCE(*state); |
Juergen Gross | 6ba286a | 2016-07-06 07:00:30 +0200 | [diff] [blame] | 65 | 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 | */ |
| 73 | void xen_get_runstate_snapshot(struct vcpu_runstate_info *res) |
| 74 | { |
| 75 | xen_get_runstate_snapshot_cpu(res, smp_processor_id()); |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* return true when a vcpu could run but has no real cpu to run on */ |
| 79 | bool xen_vcpu_stolen(int vcpu) |
| 80 | { |
| 81 | return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable; |
| 82 | } |
| 83 | |
Juergen Gross | d34c30c | 2016-07-26 14:15:11 +0200 | [diff] [blame] | 84 | u64 xen_steal_clock(int cpu) |
Juergen Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 85 | { |
| 86 | struct vcpu_runstate_info state; |
| 87 | |
Juergen Gross | 6ba286a | 2016-07-06 07:00:30 +0200 | [diff] [blame] | 88 | xen_get_runstate_snapshot_cpu(&state, cpu); |
Juergen Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 89 | return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline]; |
| 90 | } |
| 91 | |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 92 | void 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 Kuznetsov | ad5475f | 2016-06-30 17:56:38 +0200 | [diff] [blame] | 99 | xen_vcpu_nr(cpu), &area)) |
Stefano Stabellini | 4ccefbe | 2015-11-05 15:15:07 +0000 | [diff] [blame] | 100 | BUG(); |
| 101 | } |
| 102 | |
Juergen Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 103 | void __init xen_time_setup_guest(void) |
| 104 | { |
Juergen Gross | 6ba286a | 2016-07-06 07:00:30 +0200 | [diff] [blame] | 105 | bool xen_runstate_remote; |
| 106 | |
| 107 | xen_runstate_remote = !HYPERVISOR_vm_assist(VMASST_CMD_enable, |
| 108 | VMASST_TYPE_runstate_update_flag); |
| 109 | |
Juergen Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 110 | pv_time_ops.steal_clock = xen_steal_clock; |
| 111 | |
| 112 | static_key_slow_inc(¶virt_steal_enabled); |
Juergen Gross | 6ba286a | 2016-07-06 07:00:30 +0200 | [diff] [blame] | 113 | if (xen_runstate_remote) |
| 114 | static_key_slow_inc(¶virt_steal_rq_enabled); |
Juergen Gross | ecb23dc | 2016-05-20 09:26:48 +0200 | [diff] [blame] | 115 | } |