blob: 111e09c3f4bf1a588b9b3ed0d889bc166519f704 [file] [log] [blame]
Alex Williamsonec535002013-10-30 11:02:17 -06001/*
2 * VFIO-KVM bridge pseudo device
3 *
4 * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
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.
10 */
11
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>
Paolo Bonzini3c3c29f2014-09-24 13:02:46 +020021#include "vfio.h"
Alex Williamsonec535002013-10-30 11:02:17 -060022
23struct kvm_vfio_group {
24 struct list_head node;
25 struct vfio_group *vfio_group;
26};
27
28struct kvm_vfio {
29 struct list_head group_list;
30 struct mutex lock;
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060031 bool noncoherent;
Alex Williamsonec535002013-10-30 11:02:17 -060032};
33
34static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
35{
36 struct vfio_group *vfio_group;
37 struct vfio_group *(*fn)(struct file *);
38
39 fn = symbol_get(vfio_group_get_external_user);
40 if (!fn)
41 return ERR_PTR(-EINVAL);
42
43 vfio_group = fn(filep);
44
45 symbol_put(vfio_group_get_external_user);
46
47 return vfio_group;
48}
49
Alex Williamson8f9dec02017-06-28 13:50:05 -060050static bool kvm_vfio_external_group_match_file(struct vfio_group *group,
51 struct file *filep)
52{
53 bool ret, (*fn)(struct vfio_group *, struct file *);
54
55 fn = symbol_get(vfio_external_group_match_file);
56 if (!fn)
57 return false;
58
59 ret = fn(group, filep);
60
61 symbol_put(vfio_external_group_match_file);
62
63 return ret;
64}
65
Alex Williamsonec535002013-10-30 11:02:17 -060066static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
67{
68 void (*fn)(struct vfio_group *);
69
70 fn = symbol_get(vfio_group_put_external_user);
71 if (!fn)
72 return;
73
74 fn(vfio_group);
75
76 symbol_put(vfio_group_put_external_user);
77}
78
Alex Williamson9d830d42014-02-26 11:38:40 -070079static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group)
80{
81 long (*fn)(struct vfio_group *, unsigned long);
82 long ret;
83
84 fn = symbol_get(vfio_external_check_extension);
85 if (!fn)
86 return false;
87
88 ret = fn(vfio_group, VFIO_DMA_CC_IOMMU);
89
90 symbol_put(vfio_external_check_extension);
91
92 return ret > 0;
93}
94
Alex Williamsone0f0bbc2013-10-30 11:02:30 -060095/*
96 * Groups can use the same or different IOMMU domains. If the same then
97 * adding a new group may change the coherency of groups we've previously
98 * been told about. We don't want to care about any of that so we retest
99 * each group and bail as soon as we find one that's noncoherent. This
100 * means we only ever [un]register_noncoherent_dma once for the whole device.
101 */
102static void kvm_vfio_update_coherency(struct kvm_device *dev)
103{
104 struct kvm_vfio *kv = dev->private;
105 bool noncoherent = false;
106 struct kvm_vfio_group *kvg;
107
108 mutex_lock(&kv->lock);
109
110 list_for_each_entry(kvg, &kv->group_list, node) {
Alex Williamson9d830d42014-02-26 11:38:40 -0700111 if (!kvm_vfio_group_is_coherent(kvg->vfio_group)) {
112 noncoherent = true;
113 break;
114 }
Alex Williamsone0f0bbc2013-10-30 11:02:30 -0600115 }
116
117 if (noncoherent != kv->noncoherent) {
118 kv->noncoherent = noncoherent;
119
120 if (kv->noncoherent)
121 kvm_arch_register_noncoherent_dma(dev->kvm);
122 else
123 kvm_arch_unregister_noncoherent_dma(dev->kvm);
124 }
125
126 mutex_unlock(&kv->lock);
127}
128
Alex Williamsonec535002013-10-30 11:02:17 -0600129static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
130{
131 struct kvm_vfio *kv = dev->private;
132 struct vfio_group *vfio_group;
133 struct kvm_vfio_group *kvg;
Paul Bollee81d1ad2014-01-10 01:28:46 +0100134 int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
Alex Williamsonec535002013-10-30 11:02:17 -0600135 struct fd f;
136 int32_t fd;
137 int ret;
138
139 switch (attr) {
140 case KVM_DEV_VFIO_GROUP_ADD:
Paul Bollee81d1ad2014-01-10 01:28:46 +0100141 if (get_user(fd, argp))
Alex Williamsonec535002013-10-30 11:02:17 -0600142 return -EFAULT;
143
144 f = fdget(fd);
145 if (!f.file)
146 return -EBADF;
147
148 vfio_group = kvm_vfio_group_get_external_user(f.file);
149 fdput(f);
150
151 if (IS_ERR(vfio_group))
152 return PTR_ERR(vfio_group);
153
154 mutex_lock(&kv->lock);
155
156 list_for_each_entry(kvg, &kv->group_list, node) {
157 if (kvg->vfio_group == vfio_group) {
158 mutex_unlock(&kv->lock);
159 kvm_vfio_group_put_external_user(vfio_group);
160 return -EEXIST;
161 }
162 }
163
164 kvg = kzalloc(sizeof(*kvg), GFP_KERNEL);
165 if (!kvg) {
166 mutex_unlock(&kv->lock);
167 kvm_vfio_group_put_external_user(vfio_group);
168 return -ENOMEM;
169 }
170
171 list_add_tail(&kvg->node, &kv->group_list);
172 kvg->vfio_group = vfio_group;
173
Paolo Bonzini5544eb92015-07-07 15:41:58 +0200174 kvm_arch_start_assignment(dev->kvm);
175
Alex Williamsonec535002013-10-30 11:02:17 -0600176 mutex_unlock(&kv->lock);
177
Alex Williamsone0f0bbc2013-10-30 11:02:30 -0600178 kvm_vfio_update_coherency(dev);
179
Alex Williamsonec535002013-10-30 11:02:17 -0600180 return 0;
181
182 case KVM_DEV_VFIO_GROUP_DEL:
Paul Bollee81d1ad2014-01-10 01:28:46 +0100183 if (get_user(fd, argp))
Alex Williamsonec535002013-10-30 11:02:17 -0600184 return -EFAULT;
185
186 f = fdget(fd);
187 if (!f.file)
188 return -EBADF;
189
Alex Williamsonec535002013-10-30 11:02:17 -0600190 ret = -ENOENT;
191
192 mutex_lock(&kv->lock);
193
194 list_for_each_entry(kvg, &kv->group_list, node) {
Alex Williamson8f9dec02017-06-28 13:50:05 -0600195 if (!kvm_vfio_external_group_match_file(kvg->vfio_group,
196 f.file))
Alex Williamsonec535002013-10-30 11:02:17 -0600197 continue;
198
199 list_del(&kvg->node);
200 kvm_vfio_group_put_external_user(kvg->vfio_group);
201 kfree(kvg);
202 ret = 0;
203 break;
204 }
205
Paolo Bonzini5544eb92015-07-07 15:41:58 +0200206 kvm_arch_end_assignment(dev->kvm);
207
Alex Williamsonec535002013-10-30 11:02:17 -0600208 mutex_unlock(&kv->lock);
209
Alex Williamson8f9dec02017-06-28 13:50:05 -0600210 fdput(f);
Alex Williamsonec535002013-10-30 11:02:17 -0600211
Alex Williamsone0f0bbc2013-10-30 11:02:30 -0600212 kvm_vfio_update_coherency(dev);
213
Alex Williamsonec535002013-10-30 11:02:17 -0600214 return ret;
215 }
216
217 return -ENXIO;
218}
219
220static int kvm_vfio_set_attr(struct kvm_device *dev,
221 struct kvm_device_attr *attr)
222{
223 switch (attr->group) {
224 case KVM_DEV_VFIO_GROUP:
225 return kvm_vfio_set_group(dev, attr->attr, attr->addr);
226 }
227
228 return -ENXIO;
229}
230
231static int kvm_vfio_has_attr(struct kvm_device *dev,
232 struct kvm_device_attr *attr)
233{
234 switch (attr->group) {
235 case KVM_DEV_VFIO_GROUP:
236 switch (attr->attr) {
237 case KVM_DEV_VFIO_GROUP_ADD:
238 case KVM_DEV_VFIO_GROUP_DEL:
239 return 0;
240 }
241
242 break;
243 }
244
245 return -ENXIO;
246}
247
248static void kvm_vfio_destroy(struct kvm_device *dev)
249{
250 struct kvm_vfio *kv = dev->private;
251 struct kvm_vfio_group *kvg, *tmp;
252
253 list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
254 kvm_vfio_group_put_external_user(kvg->vfio_group);
255 list_del(&kvg->node);
256 kfree(kvg);
Paolo Bonzini5544eb92015-07-07 15:41:58 +0200257 kvm_arch_end_assignment(dev->kvm);
Alex Williamsonec535002013-10-30 11:02:17 -0600258 }
259
Alex Williamsone0f0bbc2013-10-30 11:02:30 -0600260 kvm_vfio_update_coherency(dev);
261
Alex Williamsonec535002013-10-30 11:02:17 -0600262 kfree(kv);
263 kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
264}
265
Will Deacon80ce1632014-09-02 10:27:36 +0100266static int kvm_vfio_create(struct kvm_device *dev, u32 type);
267
268static struct kvm_device_ops kvm_vfio_ops = {
269 .name = "kvm-vfio",
270 .create = kvm_vfio_create,
271 .destroy = kvm_vfio_destroy,
272 .set_attr = kvm_vfio_set_attr,
273 .has_attr = kvm_vfio_has_attr,
274};
275
Alex Williamsonec535002013-10-30 11:02:17 -0600276static int kvm_vfio_create(struct kvm_device *dev, u32 type)
277{
278 struct kvm_device *tmp;
279 struct kvm_vfio *kv;
280
281 /* Only one VFIO "device" per VM */
282 list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
283 if (tmp->ops == &kvm_vfio_ops)
284 return -EBUSY;
285
286 kv = kzalloc(sizeof(*kv), GFP_KERNEL);
287 if (!kv)
288 return -ENOMEM;
289
290 INIT_LIST_HEAD(&kv->group_list);
291 mutex_init(&kv->lock);
292
293 dev->private = kv;
294
295 return 0;
296}
297
Paolo Bonzini3c3c29f2014-09-24 13:02:46 +0200298int kvm_vfio_ops_init(void)
Will Deacon80ce1632014-09-02 10:27:36 +0100299{
300 return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
301}
Wanpeng Li571ee1b2014-10-09 18:30:08 +0800302
303void kvm_vfio_ops_exit(void)
304{
305 kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
306}