2 * VFIO-KVM bridge pseudo device
4 * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/errno.h>
13 #include <linux/file.h>
14 #include <linux/kvm_host.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 #include <linux/vfio.h>
23 struct kvm_vfio_group
{
24 struct list_head node
;
25 struct vfio_group
*vfio_group
;
29 struct list_head group_list
;
34 static struct vfio_group
*kvm_vfio_group_get_external_user(struct file
*filep
)
36 struct vfio_group
*vfio_group
;
37 struct vfio_group
*(*fn
)(struct file
*);
39 fn
= symbol_get(vfio_group_get_external_user
);
41 return ERR_PTR(-EINVAL
);
43 vfio_group
= fn(filep
);
45 symbol_put(vfio_group_get_external_user
);
50 static void kvm_vfio_group_put_external_user(struct vfio_group
*vfio_group
)
52 void (*fn
)(struct vfio_group
*);
54 fn
= symbol_get(vfio_group_put_external_user
);
60 symbol_put(vfio_group_put_external_user
);
63 static void kvm_vfio_group_set_kvm(struct vfio_group
*group
, struct kvm
*kvm
)
65 void (*fn
)(struct vfio_group
*, struct kvm
*);
67 fn
= symbol_get(vfio_group_set_kvm
);
73 symbol_put(vfio_group_set_kvm
);
76 static bool kvm_vfio_group_is_coherent(struct vfio_group
*vfio_group
)
78 long (*fn
)(struct vfio_group
*, unsigned long);
81 fn
= symbol_get(vfio_external_check_extension
);
85 ret
= fn(vfio_group
, VFIO_DMA_CC_IOMMU
);
87 symbol_put(vfio_external_check_extension
);
93 * Groups can use the same or different IOMMU domains. If the same then
94 * adding a new group may change the coherency of groups we've previously
95 * been told about. We don't want to care about any of that so we retest
96 * each group and bail as soon as we find one that's noncoherent. This
97 * means we only ever [un]register_noncoherent_dma once for the whole device.
99 static void kvm_vfio_update_coherency(struct kvm_device
*dev
)
101 struct kvm_vfio
*kv
= dev
->private;
102 bool noncoherent
= false;
103 struct kvm_vfio_group
*kvg
;
105 mutex_lock(&kv
->lock
);
107 list_for_each_entry(kvg
, &kv
->group_list
, node
) {
108 if (!kvm_vfio_group_is_coherent(kvg
->vfio_group
)) {
114 if (noncoherent
!= kv
->noncoherent
) {
115 kv
->noncoherent
= noncoherent
;
118 kvm_arch_register_noncoherent_dma(dev
->kvm
);
120 kvm_arch_unregister_noncoherent_dma(dev
->kvm
);
123 mutex_unlock(&kv
->lock
);
126 static int kvm_vfio_set_group(struct kvm_device
*dev
, long attr
, u64 arg
)
128 struct kvm_vfio
*kv
= dev
->private;
129 struct vfio_group
*vfio_group
;
130 struct kvm_vfio_group
*kvg
;
131 int32_t __user
*argp
= (int32_t __user
*)(unsigned long)arg
;
137 case KVM_DEV_VFIO_GROUP_ADD
:
138 if (get_user(fd
, argp
))
145 vfio_group
= kvm_vfio_group_get_external_user(f
.file
);
148 if (IS_ERR(vfio_group
))
149 return PTR_ERR(vfio_group
);
151 mutex_lock(&kv
->lock
);
153 list_for_each_entry(kvg
, &kv
->group_list
, node
) {
154 if (kvg
->vfio_group
== vfio_group
) {
155 mutex_unlock(&kv
->lock
);
156 kvm_vfio_group_put_external_user(vfio_group
);
161 kvg
= kzalloc(sizeof(*kvg
), GFP_KERNEL
);
163 mutex_unlock(&kv
->lock
);
164 kvm_vfio_group_put_external_user(vfio_group
);
168 list_add_tail(&kvg
->node
, &kv
->group_list
);
169 kvg
->vfio_group
= vfio_group
;
171 kvm_arch_start_assignment(dev
->kvm
);
173 mutex_unlock(&kv
->lock
);
175 kvm_vfio_group_set_kvm(vfio_group
, dev
->kvm
);
177 kvm_vfio_update_coherency(dev
);
181 case KVM_DEV_VFIO_GROUP_DEL
:
182 if (get_user(fd
, argp
))
189 vfio_group
= kvm_vfio_group_get_external_user(f
.file
);
192 if (IS_ERR(vfio_group
))
193 return PTR_ERR(vfio_group
);
197 mutex_lock(&kv
->lock
);
199 list_for_each_entry(kvg
, &kv
->group_list
, node
) {
200 if (kvg
->vfio_group
!= vfio_group
)
203 list_del(&kvg
->node
);
204 kvm_vfio_group_put_external_user(kvg
->vfio_group
);
210 kvm_arch_end_assignment(dev
->kvm
);
212 mutex_unlock(&kv
->lock
);
214 kvm_vfio_group_set_kvm(vfio_group
, NULL
);
216 kvm_vfio_group_put_external_user(vfio_group
);
218 kvm_vfio_update_coherency(dev
);
226 static int kvm_vfio_set_attr(struct kvm_device
*dev
,
227 struct kvm_device_attr
*attr
)
229 switch (attr
->group
) {
230 case KVM_DEV_VFIO_GROUP
:
231 return kvm_vfio_set_group(dev
, attr
->attr
, attr
->addr
);
237 static int kvm_vfio_has_attr(struct kvm_device
*dev
,
238 struct kvm_device_attr
*attr
)
240 switch (attr
->group
) {
241 case KVM_DEV_VFIO_GROUP
:
242 switch (attr
->attr
) {
243 case KVM_DEV_VFIO_GROUP_ADD
:
244 case KVM_DEV_VFIO_GROUP_DEL
:
254 static void kvm_vfio_destroy(struct kvm_device
*dev
)
256 struct kvm_vfio
*kv
= dev
->private;
257 struct kvm_vfio_group
*kvg
, *tmp
;
259 list_for_each_entry_safe(kvg
, tmp
, &kv
->group_list
, node
) {
260 kvm_vfio_group_set_kvm(kvg
->vfio_group
, NULL
);
261 kvm_vfio_group_put_external_user(kvg
->vfio_group
);
262 list_del(&kvg
->node
);
264 kvm_arch_end_assignment(dev
->kvm
);
267 kvm_vfio_update_coherency(dev
);
270 kfree(dev
); /* alloc by kvm_ioctl_create_device, free by .destroy */
273 static int kvm_vfio_create(struct kvm_device
*dev
, u32 type
);
275 static struct kvm_device_ops kvm_vfio_ops
= {
277 .create
= kvm_vfio_create
,
278 .destroy
= kvm_vfio_destroy
,
279 .set_attr
= kvm_vfio_set_attr
,
280 .has_attr
= kvm_vfio_has_attr
,
283 static int kvm_vfio_create(struct kvm_device
*dev
, u32 type
)
285 struct kvm_device
*tmp
;
288 /* Only one VFIO "device" per VM */
289 list_for_each_entry(tmp
, &dev
->kvm
->devices
, vm_node
)
290 if (tmp
->ops
== &kvm_vfio_ops
)
293 kv
= kzalloc(sizeof(*kv
), GFP_KERNEL
);
297 INIT_LIST_HEAD(&kv
->group_list
);
298 mutex_init(&kv
->lock
);
305 int kvm_vfio_ops_init(void)
307 return kvm_register_device_ops(&kvm_vfio_ops
, KVM_DEV_TYPE_VFIO
);
310 void kvm_vfio_ops_exit(void)
312 kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO
);