xref: /openbmc/linux/arch/riscv/kvm/vm.c (revision 465191d6)
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 	return kvm_riscv_guest_timer_init(kvm);
45 }
46 
47 void kvm_arch_destroy_vm(struct kvm *kvm)
48 {
49 	kvm_destroy_vcpus(kvm);
50 }
51 
52 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
53 {
54 	int r;
55 
56 	switch (ext) {
57 	case KVM_CAP_IOEVENTFD:
58 	case KVM_CAP_DEVICE_CTRL:
59 	case KVM_CAP_USER_MEMORY:
60 	case KVM_CAP_SYNC_MMU:
61 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
62 	case KVM_CAP_ONE_REG:
63 	case KVM_CAP_READONLY_MEM:
64 	case KVM_CAP_MP_STATE:
65 	case KVM_CAP_IMMEDIATE_EXIT:
66 		r = 1;
67 		break;
68 	case KVM_CAP_NR_VCPUS:
69 		r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS);
70 		break;
71 	case KVM_CAP_MAX_VCPUS:
72 		r = KVM_MAX_VCPUS;
73 		break;
74 	case KVM_CAP_NR_MEMSLOTS:
75 		r = KVM_USER_MEM_SLOTS;
76 		break;
77 	case KVM_CAP_VM_GPA_BITS:
78 		r = kvm_riscv_gstage_gpa_bits();
79 		break;
80 	default:
81 		r = 0;
82 		break;
83 	}
84 
85 	return r;
86 }
87 
88 long kvm_arch_vm_ioctl(struct file *filp,
89 		       unsigned int ioctl, unsigned long arg)
90 {
91 	return -EINVAL;
92 }
93