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>
22 struct kvm_vfio_group {
23 struct list_head node;
24 struct vfio_group *vfio_group;
28 struct list_head group_list;
33 static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
35 struct vfio_group *vfio_group;
36 struct vfio_group *(*fn)(struct file *);
38 fn = symbol_get(vfio_group_get_external_user);
40 return ERR_PTR(-EINVAL);
42 vfio_group = fn(filep);
44 symbol_put(vfio_group_get_external_user);
49 static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
51 void (*fn)(struct vfio_group *);
53 fn = symbol_get(vfio_group_put_external_user);
59 symbol_put(vfio_group_put_external_user);
63 * Groups can use the same or different IOMMU domains. If the same then
64 * adding a new group may change the coherency of groups we've previously
65 * been told about. We don't want to care about any of that so we retest
66 * each group and bail as soon as we find one that's noncoherent. This
67 * means we only ever [un]register_noncoherent_dma once for the whole device.
69 static void kvm_vfio_update_coherency(struct kvm_device *dev)
71 struct kvm_vfio *kv = dev->private;
72 bool noncoherent = false;
73 struct kvm_vfio_group *kvg;
75 mutex_lock(&kv->lock);
77 list_for_each_entry(kvg, &kv->group_list, node) {
79 * TODO: We need an interface to check the coherency of
80 * the IOMMU domain this group is using. For now, assume
81 * it's always noncoherent.
87 if (noncoherent != kv->noncoherent) {
88 kv->noncoherent = noncoherent;
91 kvm_arch_register_noncoherent_dma(dev->kvm);
93 kvm_arch_unregister_noncoherent_dma(dev->kvm);
96 mutex_unlock(&kv->lock);
99 static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
101 struct kvm_vfio *kv = dev->private;
102 struct vfio_group *vfio_group;
103 struct kvm_vfio_group *kvg;
104 int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
110 case KVM_DEV_VFIO_GROUP_ADD:
111 if (get_user(fd, argp))
118 vfio_group = kvm_vfio_group_get_external_user(f.file);
121 if (IS_ERR(vfio_group))
122 return PTR_ERR(vfio_group);
124 mutex_lock(&kv->lock);
126 list_for_each_entry(kvg, &kv->group_list, node) {
127 if (kvg->vfio_group == vfio_group) {
128 mutex_unlock(&kv->lock);
129 kvm_vfio_group_put_external_user(vfio_group);
134 kvg = kzalloc(sizeof(*kvg), GFP_KERNEL);
136 mutex_unlock(&kv->lock);
137 kvm_vfio_group_put_external_user(vfio_group);
141 list_add_tail(&kvg->node, &kv->group_list);
142 kvg->vfio_group = vfio_group;
144 mutex_unlock(&kv->lock);
146 kvm_vfio_update_coherency(dev);
150 case KVM_DEV_VFIO_GROUP_DEL:
151 if (get_user(fd, argp))
158 vfio_group = kvm_vfio_group_get_external_user(f.file);
161 if (IS_ERR(vfio_group))
162 return PTR_ERR(vfio_group);
166 mutex_lock(&kv->lock);
168 list_for_each_entry(kvg, &kv->group_list, node) {
169 if (kvg->vfio_group != vfio_group)
172 list_del(&kvg->node);
173 kvm_vfio_group_put_external_user(kvg->vfio_group);
179 mutex_unlock(&kv->lock);
181 kvm_vfio_group_put_external_user(vfio_group);
183 kvm_vfio_update_coherency(dev);
191 static int kvm_vfio_set_attr(struct kvm_device *dev,
192 struct kvm_device_attr *attr)
194 switch (attr->group) {
195 case KVM_DEV_VFIO_GROUP:
196 return kvm_vfio_set_group(dev, attr->attr, attr->addr);
202 static int kvm_vfio_has_attr(struct kvm_device *dev,
203 struct kvm_device_attr *attr)
205 switch (attr->group) {
206 case KVM_DEV_VFIO_GROUP:
207 switch (attr->attr) {
208 case KVM_DEV_VFIO_GROUP_ADD:
209 case KVM_DEV_VFIO_GROUP_DEL:
219 static void kvm_vfio_destroy(struct kvm_device *dev)
221 struct kvm_vfio *kv = dev->private;
222 struct kvm_vfio_group *kvg, *tmp;
224 list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
225 kvm_vfio_group_put_external_user(kvg->vfio_group);
226 list_del(&kvg->node);
230 kvm_vfio_update_coherency(dev);
233 kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
236 static int kvm_vfio_create(struct kvm_device *dev, u32 type)
238 struct kvm_device *tmp;
241 /* Only one VFIO "device" per VM */
242 list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
243 if (tmp->ops == &kvm_vfio_ops)
246 kv = kzalloc(sizeof(*kv), GFP_KERNEL);
250 INIT_LIST_HEAD(&kv->group_list);
251 mutex_init(&kv->lock);
258 struct kvm_device_ops kvm_vfio_ops = {
260 .create = kvm_vfio_create,
261 .destroy = kvm_vfio_destroy,
262 .set_attr = kvm_vfio_set_attr,
263 .has_attr = kvm_vfio_has_attr,