blob: e65d7fe6489f3e80512a7b38724c929ee647d12f [file] [log] [blame]
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +02001#define pr_fmt(fmt) "Hyper-V: " fmt
2
3#include <linux/hyperv.h>
4#include <linux/log2.h>
5#include <linux/slab.h>
6#include <linux/types.h>
7
8#include <asm/fpu/api.h>
9#include <asm/mshyperv.h>
10#include <asm/msr.h>
11#include <asm/tlbflush.h>
Peter Zijlstra48a8b972018-08-22 17:30:16 +020012#include <asm/tlb.h>
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020013
Vitaly Kuznetsov773b79f2017-08-02 18:09:21 +020014#define CREATE_TRACE_POINTS
15#include <asm/trace/hyperv.h>
16
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020017/* Each gva in gva_list encodes up to 4096 pages to flush */
18#define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
19
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +020020static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
21 const struct flush_tlb_info *info);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +020022
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020023/*
24 * Fills in gva_list starting from offset. Returns the number of items added.
25 */
26static inline int fill_gva_list(u64 gva_list[], int offset,
27 unsigned long start, unsigned long end)
28{
29 int gva_n = offset;
30 unsigned long cur = start, diff;
31
32 do {
33 diff = end > cur ? end - cur : 0;
34
35 gva_list[gva_n] = cur & PAGE_MASK;
36 /*
37 * Lower 12 bits encode the number of additional
38 * pages to flush (in addition to the 'cur' page).
39 */
40 if (diff >= HV_TLB_FLUSH_UNIT)
41 gva_list[gva_n] |= ~PAGE_MASK;
42 else if (diff)
43 gva_list[gva_n] |= (diff - 1) >> PAGE_SHIFT;
44
45 cur += HV_TLB_FLUSH_UNIT;
46 gva_n++;
47
48 } while (cur < end);
49
50 return gva_n - offset;
51}
52
53static void hyperv_flush_tlb_others(const struct cpumask *cpus,
54 const struct flush_tlb_info *info)
55{
56 int cpu, vcpu, gva_n, max_gvas;
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +020057 struct hv_tlb_flush **flush_pcpu;
58 struct hv_tlb_flush *flush;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020059 u64 status = U64_MAX;
60 unsigned long flags;
61
Vitaly Kuznetsov773b79f2017-08-02 18:09:21 +020062 trace_hyperv_mmu_flush_tlb_others(cpus, info);
63
K. Y. Srinivasan9a2d78e2018-05-16 14:53:34 -070064 if (!hv_hypercall_pg)
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020065 goto do_native;
66
67 if (cpumask_empty(cpus))
68 return;
69
70 local_irq_save(flags);
71
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +020072 flush_pcpu = (struct hv_tlb_flush **)
K. Y. Srinivasan9a2d78e2018-05-16 14:53:34 -070073 this_cpu_ptr(hyperv_pcpu_input_arg);
Vitaly Kuznetsov60d73a72017-10-05 13:39:24 +020074
75 flush = *flush_pcpu;
76
77 if (unlikely(!flush)) {
78 local_irq_restore(flags);
79 goto do_native;
80 }
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020081
82 if (info->mm) {
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +010083 /*
84 * AddressSpace argument must match the CR3 with PCID bits
85 * stripped out.
86 */
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020087 flush->address_space = virt_to_phys(info->mm->pgd);
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +010088 flush->address_space &= CR3_ADDR_MASK;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +020089 flush->flags = 0;
90 } else {
91 flush->address_space = 0;
92 flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
93 }
94
95 flush->processor_mask = 0;
96 if (cpumask_equal(cpus, cpu_present_mask)) {
97 flush->flags |= HV_FLUSH_ALL_PROCESSORS;
98 } else {
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +020099 /*
100 * From the supplied CPU set we need to figure out if we can get
101 * away with cheaper HVCALL_FLUSH_VIRTUAL_ADDRESS_{LIST,SPACE}
102 * hypercalls. This is possible when the highest VP number in
103 * the set is < 64. As VP numbers are usually in ascending order
104 * and match Linux CPU ids, here is an optimization: we check
105 * the VP number for the highest bit in the supplied set first
106 * so we can quickly find out if using *_EX hypercalls is a
107 * must. We will also check all VP numbers when walking the
108 * supplied CPU set to remain correct in all cases.
109 */
110 if (hv_cpu_number_to_vp_number(cpumask_last(cpus)) >= 64)
111 goto do_ex_hypercall;
112
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200113 for_each_cpu(cpu, cpus) {
114 vcpu = hv_cpu_number_to_vp_number(cpu);
Vitaly Kuznetsov110d2a72018-07-09 19:40:12 +0200115 if (vcpu == VP_INVAL) {
116 local_irq_restore(flags);
117 goto do_native;
118 }
119
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200120 if (vcpu >= 64)
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200121 goto do_ex_hypercall;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200122
123 __set_bit(vcpu, (unsigned long *)
124 &flush->processor_mask);
125 }
126 }
127
128 /*
129 * We can flush not more than max_gvas with one hypercall. Flush the
130 * whole address space if we were asked to do more.
131 */
132 max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);
133
134 if (info->end == TLB_FLUSH_ALL) {
135 flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
136 status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
137 flush, NULL);
138 } else if (info->end &&
139 ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
140 status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
141 flush, NULL);
142 } else {
143 gva_n = fill_gva_list(flush->gva_list, 0,
144 info->start, info->end);
145 status = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
146 gva_n, 0, flush, NULL);
147 }
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200148 goto check_status;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200149
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200150do_ex_hypercall:
151 status = hyperv_flush_tlb_others_ex(cpus, info);
152
153check_status:
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200154 local_irq_restore(flags);
155
156 if (!(status & HV_HYPERCALL_RESULT_MASK))
157 return;
158do_native:
159 native_flush_tlb_others(cpus, info);
160}
161
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200162static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
163 const struct flush_tlb_info *info)
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200164{
165 int nr_bank = 0, max_gvas, gva_n;
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +0200166 struct hv_tlb_flush_ex **flush_pcpu;
167 struct hv_tlb_flush_ex *flush;
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200168 u64 status;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200169
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200170 if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
171 return U64_MAX;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200172
Vitaly Kuznetsovc9c92be2018-05-16 17:21:24 +0200173 flush_pcpu = (struct hv_tlb_flush_ex **)
K. Y. Srinivasan9a2d78e2018-05-16 14:53:34 -0700174 this_cpu_ptr(hyperv_pcpu_input_arg);
Vitaly Kuznetsov60d73a72017-10-05 13:39:24 +0200175
176 flush = *flush_pcpu;
177
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200178 if (info->mm) {
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +0100179 /*
180 * AddressSpace argument must match the CR3 with PCID bits
181 * stripped out.
182 */
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200183 flush->address_space = virt_to_phys(info->mm->pgd);
Vitaly Kuznetsov617ab452018-01-24 11:36:29 +0100184 flush->address_space &= CR3_ADDR_MASK;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200185 flush->flags = 0;
186 } else {
187 flush->address_space = 0;
188 flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
189 }
190
191 flush->hv_vp_set.valid_bank_mask = 0;
192
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200193 flush->hv_vp_set.format = HV_GENERIC_SET_SPARSE_4K;
194 nr_bank = cpumask_to_vpset(&(flush->hv_vp_set), cpus);
Vitaly Kuznetsov0f0caa52018-07-09 19:40:11 +0200195 if (nr_bank < 0)
196 return U64_MAX;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200197
198 /*
199 * We can flush not more than max_gvas with one hypercall. Flush the
200 * whole address space if we were asked to do more.
201 */
202 max_gvas =
203 (PAGE_SIZE - sizeof(*flush) - nr_bank *
204 sizeof(flush->hv_vp_set.bank_contents[0])) /
205 sizeof(flush->gva_list[0]);
206
207 if (info->end == TLB_FLUSH_ALL) {
208 flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
209 status = hv_do_rep_hypercall(
210 HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
Marcelo Henrique Cerriab7ff472017-10-05 10:34:29 -0300211 0, nr_bank, flush, NULL);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200212 } else if (info->end &&
213 ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
214 status = hv_do_rep_hypercall(
215 HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
Marcelo Henrique Cerriab7ff472017-10-05 10:34:29 -0300216 0, nr_bank, flush, NULL);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200217 } else {
218 gva_n = fill_gva_list(flush->gva_list, nr_bank,
219 info->start, info->end);
220 status = hv_do_rep_hypercall(
221 HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
Marcelo Henrique Cerriab7ff472017-10-05 10:34:29 -0300222 gva_n, nr_bank, flush, NULL);
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200223 }
224
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200225 return status;
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200226}
227
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200228void hyperv_setup_mmu_ops(void)
229{
Vitaly Kuznetsov628f54c2017-08-02 18:09:20 +0200230 if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
231 return;
232
Vitaly Kuznetsov0e4c88f2018-06-21 15:32:38 +0200233 pr_info("Using hypercall for remote TLB flush\n");
Juergen Gross5c835112018-08-28 09:40:19 +0200234 pv_ops.mmu.flush_tlb_others = hyperv_flush_tlb_others;
235 pv_ops.mmu.tlb_remove_table = tlb_remove_table;
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200236}