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 #ifdef CONFIG_SPAPR_TCE_IOMMU
24 #include <asm/kvm_ppc.h>
27 struct kvm_vfio_group
{
28 struct list_head node
;
29 struct vfio_group
*vfio_group
;
33 struct list_head group_list
;
38 static struct vfio_group
*kvm_vfio_group_get_external_user(struct file
*filep
)
40 struct vfio_group
*vfio_group
;
41 struct vfio_group
*(*fn
)(struct file
*);
43 fn
= symbol_get(vfio_group_get_external_user
);
45 return ERR_PTR(-EINVAL
);
47 vfio_group
= fn(filep
);
49 symbol_put(vfio_group_get_external_user
);
54 static bool kvm_vfio_external_group_match_file(struct vfio_group
*group
,
57 bool ret
, (*fn
)(struct vfio_group
*, struct file
*);
59 fn
= symbol_get(vfio_external_group_match_file
);
63 ret
= fn(group
, filep
);
65 symbol_put(vfio_external_group_match_file
);
70 static void kvm_vfio_group_put_external_user(struct vfio_group
*vfio_group
)
72 void (*fn
)(struct vfio_group
*);
74 fn
= symbol_get(vfio_group_put_external_user
);
80 symbol_put(vfio_group_put_external_user
);
83 static void kvm_vfio_group_set_kvm(struct vfio_group
*group
, struct kvm
*kvm
)
85 void (*fn
)(struct vfio_group
*, struct kvm
*);
87 fn
= symbol_get(vfio_group_set_kvm
);
93 symbol_put(vfio_group_set_kvm
);
96 static bool kvm_vfio_group_is_coherent(struct vfio_group
*vfio_group
)
98 long (*fn
)(struct vfio_group
*, unsigned long);
101 fn
= symbol_get(vfio_external_check_extension
);
105 ret
= fn(vfio_group
, VFIO_DMA_CC_IOMMU
);
107 symbol_put(vfio_external_check_extension
);
112 #ifdef CONFIG_SPAPR_TCE_IOMMU
113 static int kvm_vfio_external_user_iommu_id(struct vfio_group
*vfio_group
)
115 int (*fn
)(struct vfio_group
*);
118 fn
= symbol_get(vfio_external_user_iommu_id
);
122 ret
= fn(vfio_group
);
124 symbol_put(vfio_external_user_iommu_id
);
129 static struct iommu_group
*kvm_vfio_group_get_iommu_group(
130 struct vfio_group
*group
)
132 int group_id
= kvm_vfio_external_user_iommu_id(group
);
137 return iommu_group_get_by_id(group_id
);
140 static void kvm_spapr_tce_release_vfio_group(struct kvm
*kvm
,
141 struct vfio_group
*vfio_group
)
143 struct iommu_group
*grp
= kvm_vfio_group_get_iommu_group(vfio_group
);
145 if (WARN_ON_ONCE(!grp
))
148 kvm_spapr_tce_release_iommu_group(kvm
, grp
);
149 iommu_group_put(grp
);
154 * Groups can use the same or different IOMMU domains. If the same then
155 * adding a new group may change the coherency of groups we've previously
156 * been told about. We don't want to care about any of that so we retest
157 * each group and bail as soon as we find one that's noncoherent. This
158 * means we only ever [un]register_noncoherent_dma once for the whole device.
160 static void kvm_vfio_update_coherency(struct kvm_device
*dev
)
162 struct kvm_vfio
*kv
= dev
->private;
163 bool noncoherent
= false;
164 struct kvm_vfio_group
*kvg
;
166 mutex_lock(&kv
->lock
);
168 list_for_each_entry(kvg
, &kv
->group_list
, node
) {
169 if (!kvm_vfio_group_is_coherent(kvg
->vfio_group
)) {
175 if (noncoherent
!= kv
->noncoherent
) {
176 kv
->noncoherent
= noncoherent
;
179 kvm_arch_register_noncoherent_dma(dev
->kvm
);
181 kvm_arch_unregister_noncoherent_dma(dev
->kvm
);
184 mutex_unlock(&kv
->lock
);
187 static int kvm_vfio_set_group(struct kvm_device
*dev
, long attr
, u64 arg
)
189 struct kvm_vfio
*kv
= dev
->private;
190 struct vfio_group
*vfio_group
;
191 struct kvm_vfio_group
*kvg
;
192 int32_t __user
*argp
= (int32_t __user
*)(unsigned long)arg
;
198 case KVM_DEV_VFIO_GROUP_ADD
:
199 if (get_user(fd
, argp
))
206 vfio_group
= kvm_vfio_group_get_external_user(f
.file
);
209 if (IS_ERR(vfio_group
))
210 return PTR_ERR(vfio_group
);
212 mutex_lock(&kv
->lock
);
214 list_for_each_entry(kvg
, &kv
->group_list
, node
) {
215 if (kvg
->vfio_group
== vfio_group
) {
216 mutex_unlock(&kv
->lock
);
217 kvm_vfio_group_put_external_user(vfio_group
);
222 kvg
= kzalloc(sizeof(*kvg
), GFP_KERNEL
);
224 mutex_unlock(&kv
->lock
);
225 kvm_vfio_group_put_external_user(vfio_group
);
229 list_add_tail(&kvg
->node
, &kv
->group_list
);
230 kvg
->vfio_group
= vfio_group
;
232 kvm_arch_start_assignment(dev
->kvm
);
234 mutex_unlock(&kv
->lock
);
236 kvm_vfio_group_set_kvm(vfio_group
, dev
->kvm
);
238 kvm_vfio_update_coherency(dev
);
242 case KVM_DEV_VFIO_GROUP_DEL
:
243 if (get_user(fd
, argp
))
252 mutex_lock(&kv
->lock
);
254 list_for_each_entry(kvg
, &kv
->group_list
, node
) {
255 if (!kvm_vfio_external_group_match_file(kvg
->vfio_group
,
259 list_del(&kvg
->node
);
260 kvm_arch_end_assignment(dev
->kvm
);
261 #ifdef CONFIG_SPAPR_TCE_IOMMU
262 kvm_spapr_tce_release_vfio_group(dev
->kvm
,
265 kvm_vfio_group_set_kvm(kvg
->vfio_group
, NULL
);
266 kvm_vfio_group_put_external_user(kvg
->vfio_group
);
272 mutex_unlock(&kv
->lock
);
276 kvm_vfio_update_coherency(dev
);
280 #ifdef CONFIG_SPAPR_TCE_IOMMU
281 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE
: {
282 struct kvm_vfio_spapr_tce param
;
283 struct kvm_vfio
*kv
= dev
->private;
284 struct vfio_group
*vfio_group
;
285 struct kvm_vfio_group
*kvg
;
287 struct iommu_group
*grp
;
289 if (copy_from_user(¶m
, (void __user
*)arg
,
290 sizeof(struct kvm_vfio_spapr_tce
)))
293 f
= fdget(param
.groupfd
);
297 vfio_group
= kvm_vfio_group_get_external_user(f
.file
);
300 if (IS_ERR(vfio_group
))
301 return PTR_ERR(vfio_group
);
303 grp
= kvm_vfio_group_get_iommu_group(vfio_group
);
304 if (WARN_ON_ONCE(!grp
)) {
305 kvm_vfio_group_put_external_user(vfio_group
);
311 mutex_lock(&kv
->lock
);
313 list_for_each_entry(kvg
, &kv
->group_list
, node
) {
314 if (kvg
->vfio_group
!= vfio_group
)
317 ret
= kvm_spapr_tce_attach_iommu_group(dev
->kvm
,
322 mutex_unlock(&kv
->lock
);
324 iommu_group_put(grp
);
325 kvm_vfio_group_put_external_user(vfio_group
);
329 #endif /* CONFIG_SPAPR_TCE_IOMMU */
335 static int kvm_vfio_set_attr(struct kvm_device
*dev
,
336 struct kvm_device_attr
*attr
)
338 switch (attr
->group
) {
339 case KVM_DEV_VFIO_GROUP
:
340 return kvm_vfio_set_group(dev
, attr
->attr
, attr
->addr
);
346 static int kvm_vfio_has_attr(struct kvm_device
*dev
,
347 struct kvm_device_attr
*attr
)
349 switch (attr
->group
) {
350 case KVM_DEV_VFIO_GROUP
:
351 switch (attr
->attr
) {
352 case KVM_DEV_VFIO_GROUP_ADD
:
353 case KVM_DEV_VFIO_GROUP_DEL
:
354 #ifdef CONFIG_SPAPR_TCE_IOMMU
355 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE
:
366 static void kvm_vfio_destroy(struct kvm_device
*dev
)
368 struct kvm_vfio
*kv
= dev
->private;
369 struct kvm_vfio_group
*kvg
, *tmp
;
371 list_for_each_entry_safe(kvg
, tmp
, &kv
->group_list
, node
) {
372 #ifdef CONFIG_SPAPR_TCE_IOMMU
373 kvm_spapr_tce_release_vfio_group(dev
->kvm
, kvg
->vfio_group
);
375 kvm_vfio_group_set_kvm(kvg
->vfio_group
, NULL
);
376 kvm_vfio_group_put_external_user(kvg
->vfio_group
);
377 list_del(&kvg
->node
);
379 kvm_arch_end_assignment(dev
->kvm
);
382 kvm_vfio_update_coherency(dev
);
385 kfree(dev
); /* alloc by kvm_ioctl_create_device, free by .destroy */
388 static int kvm_vfio_create(struct kvm_device
*dev
, u32 type
);
390 static struct kvm_device_ops kvm_vfio_ops
= {
392 .create
= kvm_vfio_create
,
393 .destroy
= kvm_vfio_destroy
,
394 .set_attr
= kvm_vfio_set_attr
,
395 .has_attr
= kvm_vfio_has_attr
,
398 static int kvm_vfio_create(struct kvm_device
*dev
, u32 type
)
400 struct kvm_device
*tmp
;
403 /* Only one VFIO "device" per VM */
404 list_for_each_entry(tmp
, &dev
->kvm
->devices
, vm_node
)
405 if (tmp
->ops
== &kvm_vfio_ops
)
408 kv
= kzalloc(sizeof(*kv
), GFP_KERNEL
);
412 INIT_LIST_HEAD(&kv
->group_list
);
413 mutex_init(&kv
->lock
);
420 int kvm_vfio_ops_init(void)
422 return kvm_register_device_ops(&kvm_vfio_ops
, KVM_DEV_TYPE_VFIO
);
425 void kvm_vfio_ops_exit(void)
427 kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO
);