1 #define pr_fmt(fmt) "Hyper-V: " fmt
3 #include <linux/hyperv.h>
4 #include <linux/log2.h>
5 #include <linux/slab.h>
6 #include <linux/types.h>
8 #include <asm/fpu/api.h>
9 #include <asm/mshyperv.h>
11 #include <asm/tlbflush.h>
13 #define CREATE_TRACE_POINTS
14 #include <asm/trace/hyperv.h>
16 /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
17 struct hv_flush_pcpu {
24 /* HvFlushVirtualAddressSpaceEx, HvFlushVirtualAddressListEx hypercalls */
25 struct hv_flush_pcpu_ex {
36 /* Each gva in gva_list encodes up to 4096 pages to flush */
37 #define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
39 static struct hv_flush_pcpu __percpu *pcpu_flush;
41 static struct hv_flush_pcpu_ex __percpu *pcpu_flush_ex;
44 * Fills in gva_list starting from offset. Returns the number of items added.
46 static inline int fill_gva_list(u64 gva_list[], int offset,
47 unsigned long start, unsigned long end)
50 unsigned long cur = start, diff;
53 diff = end > cur ? end - cur : 0;
55 gva_list[gva_n] = cur & PAGE_MASK;
57 * Lower 12 bits encode the number of additional
58 * pages to flush (in addition to the 'cur' page).
60 if (diff >= HV_TLB_FLUSH_UNIT)
61 gva_list[gva_n] |= ~PAGE_MASK;
63 gva_list[gva_n] |= (diff - 1) >> PAGE_SHIFT;
65 cur += HV_TLB_FLUSH_UNIT;
70 return gva_n - offset;
73 /* Return the number of banks in the resulting vp_set */
74 static inline int cpumask_to_vp_set(struct hv_flush_pcpu_ex *flush,
75 const struct cpumask *cpus)
77 int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
80 * Some banks may end up being empty but this is acceptable.
82 for_each_cpu(cpu, cpus) {
83 vcpu = hv_cpu_number_to_vp_number(cpu);
84 vcpu_bank = vcpu / 64;
85 vcpu_offset = vcpu % 64;
87 /* valid_bank_mask can represent up to 64 banks */
91 __set_bit(vcpu_offset, (unsigned long *)
92 &flush->hv_vp_set.bank_contents[vcpu_bank]);
93 if (vcpu_bank >= nr_bank)
94 nr_bank = vcpu_bank + 1;
96 flush->hv_vp_set.valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
101 static void hyperv_flush_tlb_others(const struct cpumask *cpus,
102 const struct flush_tlb_info *info)
104 int cpu, vcpu, gva_n, max_gvas;
105 struct hv_flush_pcpu *flush;
106 u64 status = U64_MAX;
109 trace_hyperv_mmu_flush_tlb_others(cpus, info);
111 if (!pcpu_flush || !hv_hypercall_pg)
114 if (cpumask_empty(cpus))
117 local_irq_save(flags);
119 flush = this_cpu_ptr(pcpu_flush);
122 flush->address_space = virt_to_phys(info->mm->pgd);
125 flush->address_space = 0;
126 flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
129 flush->processor_mask = 0;
130 if (cpumask_equal(cpus, cpu_present_mask)) {
131 flush->flags |= HV_FLUSH_ALL_PROCESSORS;
133 for_each_cpu(cpu, cpus) {
134 vcpu = hv_cpu_number_to_vp_number(cpu);
138 __set_bit(vcpu, (unsigned long *)
139 &flush->processor_mask);
144 * We can flush not more than max_gvas with one hypercall. Flush the
145 * whole address space if we were asked to do more.
147 max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);
149 if (info->end == TLB_FLUSH_ALL) {
150 flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
151 status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
153 } else if (info->end &&
154 ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
155 status = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
158 gva_n = fill_gva_list(flush->gva_list, 0,
159 info->start, info->end);
160 status = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
161 gva_n, 0, flush, NULL);
164 local_irq_restore(flags);
166 if (!(status & HV_HYPERCALL_RESULT_MASK))
169 native_flush_tlb_others(cpus, info);
172 static void hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
173 const struct flush_tlb_info *info)
175 int nr_bank = 0, max_gvas, gva_n;
176 struct hv_flush_pcpu_ex *flush;
177 u64 status = U64_MAX;
180 trace_hyperv_mmu_flush_tlb_others(cpus, info);
182 if (!pcpu_flush_ex || !hv_hypercall_pg)
185 if (cpumask_empty(cpus))
188 local_irq_save(flags);
190 flush = this_cpu_ptr(pcpu_flush_ex);
193 flush->address_space = virt_to_phys(info->mm->pgd);
196 flush->address_space = 0;
197 flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
200 flush->hv_vp_set.valid_bank_mask = 0;
202 if (!cpumask_equal(cpus, cpu_present_mask)) {
203 flush->hv_vp_set.format = HV_GENERIC_SET_SPARCE_4K;
204 nr_bank = cpumask_to_vp_set(flush, cpus);
208 flush->hv_vp_set.format = HV_GENERIC_SET_ALL;
209 flush->flags |= HV_FLUSH_ALL_PROCESSORS;
213 * We can flush not more than max_gvas with one hypercall. Flush the
214 * whole address space if we were asked to do more.
217 (PAGE_SIZE - sizeof(*flush) - nr_bank *
218 sizeof(flush->hv_vp_set.bank_contents[0])) /
219 sizeof(flush->gva_list[0]);
221 if (info->end == TLB_FLUSH_ALL) {
222 flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
223 status = hv_do_rep_hypercall(
224 HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
225 0, nr_bank + 2, flush, NULL);
226 } else if (info->end &&
227 ((info->end - info->start)/HV_TLB_FLUSH_UNIT) > max_gvas) {
228 status = hv_do_rep_hypercall(
229 HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX,
230 0, nr_bank + 2, flush, NULL);
232 gva_n = fill_gva_list(flush->gva_list, nr_bank,
233 info->start, info->end);
234 status = hv_do_rep_hypercall(
235 HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
236 gva_n, nr_bank + 2, flush, NULL);
239 local_irq_restore(flags);
241 if (!(status & HV_HYPERCALL_RESULT_MASK))
244 native_flush_tlb_others(cpus, info);
247 void hyperv_setup_mmu_ops(void)
249 if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
252 setup_clear_cpu_cap(X86_FEATURE_PCID);
254 if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED)) {
255 pr_info("Using hypercall for remote TLB flush\n");
256 pv_mmu_ops.flush_tlb_others = hyperv_flush_tlb_others;
258 pr_info("Using ext hypercall for remote TLB flush\n");
259 pv_mmu_ops.flush_tlb_others = hyperv_flush_tlb_others_ex;
263 void hyper_alloc_mmu(void)
265 if (!(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED))
268 if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
269 pcpu_flush = __alloc_percpu(PAGE_SIZE, PAGE_SIZE);
271 pcpu_flush_ex = __alloc_percpu(PAGE_SIZE, PAGE_SIZE);