xref: /openbmc/linux/arch/riscv/kvm/vm.c (revision dce54888)
1 // 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/errno.h>
10 #include <linux/err.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/kvm_host.h>
14 
15 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
16 	KVM_GENERIC_VM_STATS()
17 };
18 static_assert(ARRAY_SIZE(kvm_vm_stats_desc) ==
19 		sizeof(struct kvm_vm_stat) / sizeof(u64));
20 
21 const struct kvm_stats_header kvm_vm_stats_header = {
22 	.name_size = KVM_STATS_NAME_SIZE,
23 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
24 	.id_offset =  sizeof(struct kvm_stats_header),
25 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
26 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
27 		       sizeof(kvm_vm_stats_desc),
28 };
29 
30 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
31 {
32 	int r;
33 
34 	r = kvm_riscv_gstage_alloc_pgd(kvm);
35 	if (r)
36 		return r;
37 
38 	r = kvm_riscv_gstage_vmid_init(kvm);
39 	if (r) {
40 		kvm_riscv_gstage_free_pgd(kvm);
41 		return r;
42 	}
43 
44 	kvm_riscv_aia_init_vm(kvm);
45 
46 	kvm_riscv_guest_timer_init(kvm);
47 
48 	return 0;
49 }
50 
51 void kvm_arch_destroy_vm(struct kvm *kvm)
52 {
53 	kvm_destroy_vcpus(kvm);
54 
55 	kvm_riscv_aia_destroy_vm(kvm);
56 }
57 
58 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
59 {
60 	int r;
61 
62 	switch (ext) {
63 	case KVM_CAP_IOEVENTFD:
64 	case KVM_CAP_DEVICE_CTRL:
65 	case KVM_CAP_USER_MEMORY:
66 	case KVM_CAP_SYNC_MMU:
67 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
68 	case KVM_CAP_ONE_REG:
69 	case KVM_CAP_READONLY_MEM:
70 	case KVM_CAP_MP_STATE:
71 	case KVM_CAP_IMMEDIATE_EXIT:
72 		r = 1;
73 		break;
74 	case KVM_CAP_NR_VCPUS:
75 		r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS);
76 		break;
77 	case KVM_CAP_MAX_VCPUS:
78 		r = KVM_MAX_VCPUS;
79 		break;
80 	case KVM_CAP_NR_MEMSLOTS:
81 		r = KVM_USER_MEM_SLOTS;
82 		break;
83 	case KVM_CAP_VM_GPA_BITS:
84 		r = kvm_riscv_gstage_gpa_bits();
85 		break;
86 	default:
87 		r = 0;
88 		break;
89 	}
90 
91 	return r;
92 }
93 
94 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
95 {
96 	return -EINVAL;
97 }
98