blob: fa9a4f9b9542a67faf76383caf6240226950e6cf [file] [log] [blame]
Anup Patel99cdc6c2021-09-27 17:10:01 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
4 *
5 * Authors:
6 * Anup Patel <anup.patel@wdc.com>
7 */
8
9#include <linux/bitops.h>
10#include <linux/errno.h>
11#include <linux/err.h>
12#include <linux/hugetlb.h>
13#include <linux/module.h>
14#include <linux/uaccess.h>
15#include <linux/vmalloc.h>
16#include <linux/kvm_host.h>
17#include <linux/sched/signal.h>
Anup Patel9d05c1f2021-09-27 17:10:09 +053018#include <asm/csr.h>
Anup Patel99cdc6c2021-09-27 17:10:01 +053019#include <asm/page.h>
20#include <asm/pgtable.h>
Anup Patel9d05c1f2021-09-27 17:10:09 +053021#include <asm/sbi.h>
22
23#ifdef CONFIG_64BIT
24static unsigned long stage2_mode = (HGATP_MODE_SV39X4 << HGATP_MODE_SHIFT);
25static unsigned long stage2_pgd_levels = 3;
26#define stage2_index_bits 9
27#else
28static unsigned long stage2_mode = (HGATP_MODE_SV32X4 << HGATP_MODE_SHIFT);
29static unsigned long stage2_pgd_levels = 2;
30#define stage2_index_bits 10
31#endif
32
33#define stage2_pgd_xbits 2
34#define stage2_pgd_size (1UL << (HGATP_PAGE_SHIFT + stage2_pgd_xbits))
35#define stage2_gpa_bits (HGATP_PAGE_SHIFT + \
36 (stage2_pgd_levels * stage2_index_bits) + \
37 stage2_pgd_xbits)
38#define stage2_gpa_size ((gpa_t)(1ULL << stage2_gpa_bits))
39
40#define stage2_pte_leaf(__ptep) \
41 (pte_val(*(__ptep)) & (_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC))
42
43static inline unsigned long stage2_pte_index(gpa_t addr, u32 level)
44{
45 unsigned long mask;
46 unsigned long shift = HGATP_PAGE_SHIFT + (stage2_index_bits * level);
47
48 if (level == (stage2_pgd_levels - 1))
49 mask = (PTRS_PER_PTE * (1UL << stage2_pgd_xbits)) - 1;
50 else
51 mask = PTRS_PER_PTE - 1;
52
53 return (addr >> shift) & mask;
54}
55
56static inline unsigned long stage2_pte_page_vaddr(pte_t pte)
57{
58 return (unsigned long)pfn_to_virt(pte_val(pte) >> _PAGE_PFN_SHIFT);
59}
60
61static int stage2_page_size_to_level(unsigned long page_size, u32 *out_level)
62{
63 u32 i;
64 unsigned long psz = 1UL << 12;
65
66 for (i = 0; i < stage2_pgd_levels; i++) {
67 if (page_size == (psz << (i * stage2_index_bits))) {
68 *out_level = i;
69 return 0;
70 }
71 }
72
73 return -EINVAL;
74}
75
76static int stage2_level_to_page_size(u32 level, unsigned long *out_pgsize)
77{
78 if (stage2_pgd_levels < level)
79 return -EINVAL;
80
81 *out_pgsize = 1UL << (12 + (level * stage2_index_bits));
82
83 return 0;
84}
85
86static int stage2_cache_topup(struct kvm_mmu_page_cache *pcache,
87 int min, int max)
88{
89 void *page;
90
91 BUG_ON(max > KVM_MMU_PAGE_CACHE_NR_OBJS);
92 if (pcache->nobjs >= min)
93 return 0;
94 while (pcache->nobjs < max) {
95 page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
96 if (!page)
97 return -ENOMEM;
98 pcache->objects[pcache->nobjs++] = page;
99 }
100
101 return 0;
102}
103
104static void stage2_cache_flush(struct kvm_mmu_page_cache *pcache)
105{
106 while (pcache && pcache->nobjs)
107 free_page((unsigned long)pcache->objects[--pcache->nobjs]);
108}
109
110static void *stage2_cache_alloc(struct kvm_mmu_page_cache *pcache)
111{
112 void *p;
113
114 if (!pcache)
115 return NULL;
116
117 BUG_ON(!pcache->nobjs);
118 p = pcache->objects[--pcache->nobjs];
119
120 return p;
121}
122
123static bool stage2_get_leaf_entry(struct kvm *kvm, gpa_t addr,
124 pte_t **ptepp, u32 *ptep_level)
125{
126 pte_t *ptep;
127 u32 current_level = stage2_pgd_levels - 1;
128
129 *ptep_level = current_level;
130 ptep = (pte_t *)kvm->arch.pgd;
131 ptep = &ptep[stage2_pte_index(addr, current_level)];
132 while (ptep && pte_val(*ptep)) {
133 if (stage2_pte_leaf(ptep)) {
134 *ptep_level = current_level;
135 *ptepp = ptep;
136 return true;
137 }
138
139 if (current_level) {
140 current_level--;
141 *ptep_level = current_level;
142 ptep = (pte_t *)stage2_pte_page_vaddr(*ptep);
143 ptep = &ptep[stage2_pte_index(addr, current_level)];
144 } else {
145 ptep = NULL;
146 }
147 }
148
149 return false;
150}
151
152static void stage2_remote_tlb_flush(struct kvm *kvm, u32 level, gpa_t addr)
153{
154 struct cpumask hmask;
155 unsigned long size = PAGE_SIZE;
156 struct kvm_vmid *vmid = &kvm->arch.vmid;
157
158 if (stage2_level_to_page_size(level, &size))
159 return;
160 addr &= ~(size - 1);
161
162 /*
163 * TODO: Instead of cpu_online_mask, we should only target CPUs
164 * where the Guest/VM is running.
165 */
166 preempt_disable();
167 riscv_cpuid_to_hartid_mask(cpu_online_mask, &hmask);
168 sbi_remote_hfence_gvma_vmid(cpumask_bits(&hmask), addr, size,
169 READ_ONCE(vmid->vmid));
170 preempt_enable();
171}
172
173static int stage2_set_pte(struct kvm *kvm, u32 level,
174 struct kvm_mmu_page_cache *pcache,
175 gpa_t addr, const pte_t *new_pte)
176{
177 u32 current_level = stage2_pgd_levels - 1;
178 pte_t *next_ptep = (pte_t *)kvm->arch.pgd;
179 pte_t *ptep = &next_ptep[stage2_pte_index(addr, current_level)];
180
181 if (current_level < level)
182 return -EINVAL;
183
184 while (current_level != level) {
185 if (stage2_pte_leaf(ptep))
186 return -EEXIST;
187
188 if (!pte_val(*ptep)) {
189 next_ptep = stage2_cache_alloc(pcache);
190 if (!next_ptep)
191 return -ENOMEM;
192 *ptep = pfn_pte(PFN_DOWN(__pa(next_ptep)),
193 __pgprot(_PAGE_TABLE));
194 } else {
195 if (stage2_pte_leaf(ptep))
196 return -EEXIST;
197 next_ptep = (pte_t *)stage2_pte_page_vaddr(*ptep);
198 }
199
200 current_level--;
201 ptep = &next_ptep[stage2_pte_index(addr, current_level)];
202 }
203
204 *ptep = *new_pte;
205 if (stage2_pte_leaf(ptep))
206 stage2_remote_tlb_flush(kvm, current_level, addr);
207
208 return 0;
209}
210
211static int stage2_map_page(struct kvm *kvm,
212 struct kvm_mmu_page_cache *pcache,
213 gpa_t gpa, phys_addr_t hpa,
214 unsigned long page_size,
215 bool page_rdonly, bool page_exec)
216{
217 int ret;
218 u32 level = 0;
219 pte_t new_pte;
220 pgprot_t prot;
221
222 ret = stage2_page_size_to_level(page_size, &level);
223 if (ret)
224 return ret;
225
226 /*
227 * A RISC-V implementation can choose to either:
228 * 1) Update 'A' and 'D' PTE bits in hardware
229 * 2) Generate page fault when 'A' and/or 'D' bits are not set
230 * PTE so that software can update these bits.
231 *
232 * We support both options mentioned above. To achieve this, we
233 * always set 'A' and 'D' PTE bits at time of creating stage2
234 * mapping. To support KVM dirty page logging with both options
235 * mentioned above, we will write-protect stage2 PTEs to track
236 * dirty pages.
237 */
238
239 if (page_exec) {
240 if (page_rdonly)
241 prot = PAGE_READ_EXEC;
242 else
243 prot = PAGE_WRITE_EXEC;
244 } else {
245 if (page_rdonly)
246 prot = PAGE_READ;
247 else
248 prot = PAGE_WRITE;
249 }
250 new_pte = pfn_pte(PFN_DOWN(hpa), prot);
251 new_pte = pte_mkdirty(new_pte);
252
253 return stage2_set_pte(kvm, level, pcache, gpa, &new_pte);
254}
255
256enum stage2_op {
257 STAGE2_OP_NOP = 0, /* Nothing */
258 STAGE2_OP_CLEAR, /* Clear/Unmap */
259 STAGE2_OP_WP, /* Write-protect */
260};
261
262static void stage2_op_pte(struct kvm *kvm, gpa_t addr,
263 pte_t *ptep, u32 ptep_level, enum stage2_op op)
264{
265 int i, ret;
266 pte_t *next_ptep;
267 u32 next_ptep_level;
268 unsigned long next_page_size, page_size;
269
270 ret = stage2_level_to_page_size(ptep_level, &page_size);
271 if (ret)
272 return;
273
274 BUG_ON(addr & (page_size - 1));
275
276 if (!pte_val(*ptep))
277 return;
278
279 if (ptep_level && !stage2_pte_leaf(ptep)) {
280 next_ptep = (pte_t *)stage2_pte_page_vaddr(*ptep);
281 next_ptep_level = ptep_level - 1;
282 ret = stage2_level_to_page_size(next_ptep_level,
283 &next_page_size);
284 if (ret)
285 return;
286
287 if (op == STAGE2_OP_CLEAR)
288 set_pte(ptep, __pte(0));
289 for (i = 0; i < PTRS_PER_PTE; i++)
290 stage2_op_pte(kvm, addr + i * next_page_size,
291 &next_ptep[i], next_ptep_level, op);
292 if (op == STAGE2_OP_CLEAR)
293 put_page(virt_to_page(next_ptep));
294 } else {
295 if (op == STAGE2_OP_CLEAR)
296 set_pte(ptep, __pte(0));
297 else if (op == STAGE2_OP_WP)
298 set_pte(ptep, __pte(pte_val(*ptep) & ~_PAGE_WRITE));
299 stage2_remote_tlb_flush(kvm, ptep_level, addr);
300 }
301}
302
303static void stage2_unmap_range(struct kvm *kvm, gpa_t start, gpa_t size)
304{
305 int ret;
306 pte_t *ptep;
307 u32 ptep_level;
308 bool found_leaf;
309 unsigned long page_size;
310 gpa_t addr = start, end = start + size;
311
312 while (addr < end) {
313 found_leaf = stage2_get_leaf_entry(kvm, addr,
314 &ptep, &ptep_level);
315 ret = stage2_level_to_page_size(ptep_level, &page_size);
316 if (ret)
317 break;
318
319 if (!found_leaf)
320 goto next;
321
322 if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
323 stage2_op_pte(kvm, addr, ptep,
324 ptep_level, STAGE2_OP_CLEAR);
325
326next:
327 addr += page_size;
328 }
329}
330
331static void stage2_wp_range(struct kvm *kvm, gpa_t start, gpa_t end)
332{
333 int ret;
334 pte_t *ptep;
335 u32 ptep_level;
336 bool found_leaf;
337 gpa_t addr = start;
338 unsigned long page_size;
339
340 while (addr < end) {
341 found_leaf = stage2_get_leaf_entry(kvm, addr,
342 &ptep, &ptep_level);
343 ret = stage2_level_to_page_size(ptep_level, &page_size);
344 if (ret)
345 break;
346
347 if (!found_leaf)
348 goto next;
349
350 if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
351 stage2_op_pte(kvm, addr, ptep,
352 ptep_level, STAGE2_OP_WP);
353
354next:
355 addr += page_size;
356 }
357}
358
359static void stage2_wp_memory_region(struct kvm *kvm, int slot)
360{
361 struct kvm_memslots *slots = kvm_memslots(kvm);
362 struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
363 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
364 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
365
366 spin_lock(&kvm->mmu_lock);
367 stage2_wp_range(kvm, start, end);
368 spin_unlock(&kvm->mmu_lock);
369 kvm_flush_remote_tlbs(kvm);
370}
371
372static int stage2_ioremap(struct kvm *kvm, gpa_t gpa, phys_addr_t hpa,
373 unsigned long size, bool writable)
374{
375 pte_t pte;
376 int ret = 0;
377 unsigned long pfn;
378 phys_addr_t addr, end;
379 struct kvm_mmu_page_cache pcache = { 0, };
380
381 end = (gpa + size + PAGE_SIZE - 1) & PAGE_MASK;
382 pfn = __phys_to_pfn(hpa);
383
384 for (addr = gpa; addr < end; addr += PAGE_SIZE) {
385 pte = pfn_pte(pfn, PAGE_KERNEL);
386
387 if (!writable)
388 pte = pte_wrprotect(pte);
389
390 ret = stage2_cache_topup(&pcache,
391 stage2_pgd_levels,
392 KVM_MMU_PAGE_CACHE_NR_OBJS);
393 if (ret)
394 goto out;
395
396 spin_lock(&kvm->mmu_lock);
397 ret = stage2_set_pte(kvm, 0, &pcache, addr, &pte);
398 spin_unlock(&kvm->mmu_lock);
399 if (ret)
400 goto out;
401
402 pfn++;
403 }
404
405out:
406 stage2_cache_flush(&pcache);
407 return ret;
408
409}
410
411void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
412 struct kvm_memory_slot *slot,
413 gfn_t gfn_offset,
414 unsigned long mask)
415{
416 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
417 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
418 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
419
420 stage2_wp_range(kvm, start, end);
421}
Anup Patel99cdc6c2021-09-27 17:10:01 +0530422
423void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
424{
425}
426
Anup Patel9d05c1f2021-09-27 17:10:09 +0530427void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
428 const struct kvm_memory_slot *memslot)
429{
430 kvm_flush_remote_tlbs(kvm);
431}
432
Anup Patel99cdc6c2021-09-27 17:10:01 +0530433void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free)
434{
435}
436
437void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
438{
439}
440
441void kvm_arch_flush_shadow_all(struct kvm *kvm)
442{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530443 kvm_riscv_stage2_free_pgd(kvm);
Anup Patel99cdc6c2021-09-27 17:10:01 +0530444}
445
446void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
447 struct kvm_memory_slot *slot)
448{
449}
450
451void kvm_arch_commit_memory_region(struct kvm *kvm,
452 const struct kvm_userspace_memory_region *mem,
453 struct kvm_memory_slot *old,
454 const struct kvm_memory_slot *new,
455 enum kvm_mr_change change)
456{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530457 /*
458 * At this point memslot has been committed and there is an
459 * allocated dirty_bitmap[], dirty pages will be tracked while
460 * the memory slot is write protected.
461 */
462 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
463 stage2_wp_memory_region(kvm, mem->slot);
Anup Patel99cdc6c2021-09-27 17:10:01 +0530464}
465
466int kvm_arch_prepare_memory_region(struct kvm *kvm,
467 struct kvm_memory_slot *memslot,
468 const struct kvm_userspace_memory_region *mem,
469 enum kvm_mr_change change)
470{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530471 hva_t hva = mem->userspace_addr;
472 hva_t reg_end = hva + mem->memory_size;
473 bool writable = !(mem->flags & KVM_MEM_READONLY);
474 int ret = 0;
475
476 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
477 change != KVM_MR_FLAGS_ONLY)
478 return 0;
479
480 /*
481 * Prevent userspace from creating a memory region outside of the GPA
482 * space addressable by the KVM guest GPA space.
483 */
484 if ((memslot->base_gfn + memslot->npages) >=
485 (stage2_gpa_size >> PAGE_SHIFT))
486 return -EFAULT;
487
488 mmap_read_lock(current->mm);
489
490 /*
491 * A memory region could potentially cover multiple VMAs, and
492 * any holes between them, so iterate over all of them to find
493 * out if we can map any of them right now.
494 *
495 * +--------------------------------------------+
496 * +---------------+----------------+ +----------------+
497 * | : VMA 1 | VMA 2 | | VMA 3 : |
498 * +---------------+----------------+ +----------------+
499 * | memory region |
500 * +--------------------------------------------+
501 */
502 do {
503 struct vm_area_struct *vma = find_vma(current->mm, hva);
504 hva_t vm_start, vm_end;
505
506 if (!vma || vma->vm_start >= reg_end)
507 break;
508
509 /*
510 * Mapping a read-only VMA is only allowed if the
511 * memory region is configured as read-only.
512 */
513 if (writable && !(vma->vm_flags & VM_WRITE)) {
514 ret = -EPERM;
515 break;
516 }
517
518 /* Take the intersection of this VMA with the memory region */
519 vm_start = max(hva, vma->vm_start);
520 vm_end = min(reg_end, vma->vm_end);
521
522 if (vma->vm_flags & VM_PFNMAP) {
523 gpa_t gpa = mem->guest_phys_addr +
524 (vm_start - mem->userspace_addr);
525 phys_addr_t pa;
526
527 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
528 pa += vm_start - vma->vm_start;
529
530 /* IO region dirty page logging not allowed */
531 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
532 ret = -EINVAL;
533 goto out;
534 }
535
536 ret = stage2_ioremap(kvm, gpa, pa,
537 vm_end - vm_start, writable);
538 if (ret)
539 break;
540 }
541 hva = vm_end;
542 } while (hva < reg_end);
543
544 if (change == KVM_MR_FLAGS_ONLY)
545 goto out;
546
547 spin_lock(&kvm->mmu_lock);
548 if (ret)
549 stage2_unmap_range(kvm, mem->guest_phys_addr,
550 mem->memory_size);
551 spin_unlock(&kvm->mmu_lock);
552
553out:
554 mmap_read_unlock(current->mm);
555 return ret;
Anup Patel99cdc6c2021-09-27 17:10:01 +0530556}
557
Anup Patel9f701322021-09-27 17:10:06 +0530558int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu,
559 struct kvm_memory_slot *memslot,
560 gpa_t gpa, unsigned long hva, bool is_write)
561{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530562 int ret;
563 kvm_pfn_t hfn;
564 bool writeable;
565 short vma_pageshift;
566 gfn_t gfn = gpa >> PAGE_SHIFT;
567 struct vm_area_struct *vma;
568 struct kvm *kvm = vcpu->kvm;
569 struct kvm_mmu_page_cache *pcache = &vcpu->arch.mmu_page_cache;
570 bool logging = (memslot->dirty_bitmap &&
571 !(memslot->flags & KVM_MEM_READONLY)) ? true : false;
572 unsigned long vma_pagesize;
573
574 mmap_read_lock(current->mm);
575
576 vma = find_vma_intersection(current->mm, hva, hva + 1);
577 if (unlikely(!vma)) {
578 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
579 mmap_read_unlock(current->mm);
580 return -EFAULT;
581 }
582
583 if (is_vm_hugetlb_page(vma))
584 vma_pageshift = huge_page_shift(hstate_vma(vma));
585 else
586 vma_pageshift = PAGE_SHIFT;
587 vma_pagesize = 1ULL << vma_pageshift;
588 if (logging || (vma->vm_flags & VM_PFNMAP))
589 vma_pagesize = PAGE_SIZE;
590
591 if (vma_pagesize == PMD_SIZE || vma_pagesize == PGDIR_SIZE)
592 gfn = (gpa & huge_page_mask(hstate_vma(vma))) >> PAGE_SHIFT;
593
594 mmap_read_unlock(current->mm);
595
596 if (vma_pagesize != PGDIR_SIZE &&
597 vma_pagesize != PMD_SIZE &&
598 vma_pagesize != PAGE_SIZE) {
599 kvm_err("Invalid VMA page size 0x%lx\n", vma_pagesize);
600 return -EFAULT;
601 }
602
603 /* We need minimum second+third level pages */
604 ret = stage2_cache_topup(pcache, stage2_pgd_levels,
605 KVM_MMU_PAGE_CACHE_NR_OBJS);
606 if (ret) {
607 kvm_err("Failed to topup stage2 cache\n");
608 return ret;
609 }
610
611 hfn = gfn_to_pfn_prot(kvm, gfn, is_write, &writeable);
612 if (hfn == KVM_PFN_ERR_HWPOISON) {
613 send_sig_mceerr(BUS_MCEERR_AR, (void __user *)hva,
614 vma_pageshift, current);
615 return 0;
616 }
617 if (is_error_noslot_pfn(hfn))
618 return -EFAULT;
619
620 /*
621 * If logging is active then we allow writable pages only
622 * for write faults.
623 */
624 if (logging && !is_write)
625 writeable = false;
626
627 spin_lock(&kvm->mmu_lock);
628
629 if (writeable) {
630 kvm_set_pfn_dirty(hfn);
631 mark_page_dirty(kvm, gfn);
632 ret = stage2_map_page(kvm, pcache, gpa, hfn << PAGE_SHIFT,
633 vma_pagesize, false, true);
634 } else {
635 ret = stage2_map_page(kvm, pcache, gpa, hfn << PAGE_SHIFT,
636 vma_pagesize, true, true);
637 }
638
639 if (ret)
640 kvm_err("Failed to map in stage2\n");
641
642 spin_unlock(&kvm->mmu_lock);
643 kvm_set_pfn_accessed(hfn);
644 kvm_release_pfn_clean(hfn);
645 return ret;
Anup Patel9f701322021-09-27 17:10:06 +0530646}
647
Anup Patel99cdc6c2021-09-27 17:10:01 +0530648void kvm_riscv_stage2_flush_cache(struct kvm_vcpu *vcpu)
649{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530650 stage2_cache_flush(&vcpu->arch.mmu_page_cache);
Anup Patel99cdc6c2021-09-27 17:10:01 +0530651}
652
653int kvm_riscv_stage2_alloc_pgd(struct kvm *kvm)
654{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530655 struct page *pgd_page;
656
657 if (kvm->arch.pgd != NULL) {
658 kvm_err("kvm_arch already initialized?\n");
659 return -EINVAL;
660 }
661
662 pgd_page = alloc_pages(GFP_KERNEL | __GFP_ZERO,
663 get_order(stage2_pgd_size));
664 if (!pgd_page)
665 return -ENOMEM;
666 kvm->arch.pgd = page_to_virt(pgd_page);
667 kvm->arch.pgd_phys = page_to_phys(pgd_page);
668
Anup Patel99cdc6c2021-09-27 17:10:01 +0530669 return 0;
670}
671
672void kvm_riscv_stage2_free_pgd(struct kvm *kvm)
673{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530674 void *pgd = NULL;
675
676 spin_lock(&kvm->mmu_lock);
677 if (kvm->arch.pgd) {
678 stage2_unmap_range(kvm, 0UL, stage2_gpa_size);
679 pgd = READ_ONCE(kvm->arch.pgd);
680 kvm->arch.pgd = NULL;
681 kvm->arch.pgd_phys = 0;
682 }
683 spin_unlock(&kvm->mmu_lock);
684
685 if (pgd)
686 free_pages((unsigned long)pgd, get_order(stage2_pgd_size));
Anup Patel99cdc6c2021-09-27 17:10:01 +0530687}
688
689void kvm_riscv_stage2_update_hgatp(struct kvm_vcpu *vcpu)
690{
Anup Patel9d05c1f2021-09-27 17:10:09 +0530691 unsigned long hgatp = stage2_mode;
692 struct kvm_arch *k = &vcpu->kvm->arch;
693
694 hgatp |= (READ_ONCE(k->vmid.vmid) << HGATP_VMID_SHIFT) &
695 HGATP_VMID_MASK;
696 hgatp |= (k->pgd_phys >> PAGE_SHIFT) & HGATP_PPN;
697
698 csr_write(CSR_HGATP, hgatp);
699
700 if (!kvm_riscv_stage2_vmid_bits())
701 __kvm_riscv_hfence_gvma_all();
702}
703
704void kvm_riscv_stage2_mode_detect(void)
705{
706#ifdef CONFIG_64BIT
707 /* Try Sv48x4 stage2 mode */
708 csr_write(CSR_HGATP, HGATP_MODE_SV48X4 << HGATP_MODE_SHIFT);
709 if ((csr_read(CSR_HGATP) >> HGATP_MODE_SHIFT) == HGATP_MODE_SV48X4) {
710 stage2_mode = (HGATP_MODE_SV48X4 << HGATP_MODE_SHIFT);
711 stage2_pgd_levels = 4;
712 }
713 csr_write(CSR_HGATP, 0);
714
715 __kvm_riscv_hfence_gvma_all();
716#endif
717}
718
719unsigned long kvm_riscv_stage2_mode(void)
720{
721 return stage2_mode >> HGATP_MODE_SHIFT;
Anup Patel99cdc6c2021-09-27 17:10:01 +0530722}