drm/ast: Only warn about unsupported TX chips on Gen4 and later
[drm/drm-misc.git] / virt / kvm / vfio.c
blob196a102e34fb87ce350c954643f800caa7a095bc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * VFIO-KVM bridge pseudo device
5 * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
7 */
9 #include <linux/errno.h>
10 #include <linux/file.h>
11 #include <linux/kvm_host.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
17 #include <linux/vfio.h>
18 #include "vfio.h"
20 #ifdef CONFIG_SPAPR_TCE_IOMMU
21 #include <asm/kvm_ppc.h>
22 #endif
24 struct kvm_vfio_file {
25 struct list_head node;
26 struct file *file;
27 #ifdef CONFIG_SPAPR_TCE_IOMMU
28 struct iommu_group *iommu_group;
29 #endif
32 struct kvm_vfio {
33 struct list_head file_list;
34 struct mutex lock;
35 bool noncoherent;
38 static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
40 void (*fn)(struct file *file, struct kvm *kvm);
42 fn = symbol_get(vfio_file_set_kvm);
43 if (!fn)
44 return;
46 fn(file, kvm);
48 symbol_put(vfio_file_set_kvm);
51 static bool kvm_vfio_file_enforced_coherent(struct file *file)
53 bool (*fn)(struct file *file);
54 bool ret;
56 fn = symbol_get(vfio_file_enforced_coherent);
57 if (!fn)
58 return false;
60 ret = fn(file);
62 symbol_put(vfio_file_enforced_coherent);
64 return ret;
67 static bool kvm_vfio_file_is_valid(struct file *file)
69 bool (*fn)(struct file *file);
70 bool ret;
72 fn = symbol_get(vfio_file_is_valid);
73 if (!fn)
74 return false;
76 ret = fn(file);
78 symbol_put(vfio_file_is_valid);
80 return ret;
83 #ifdef CONFIG_SPAPR_TCE_IOMMU
84 static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
86 struct iommu_group *(*fn)(struct file *file);
87 struct iommu_group *ret;
89 fn = symbol_get(vfio_file_iommu_group);
90 if (!fn)
91 return NULL;
93 ret = fn(file);
95 symbol_put(vfio_file_iommu_group);
97 return ret;
100 static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
101 struct kvm_vfio_file *kvf)
103 if (WARN_ON_ONCE(!kvf->iommu_group))
104 return;
106 kvm_spapr_tce_release_iommu_group(kvm, kvf->iommu_group);
107 iommu_group_put(kvf->iommu_group);
108 kvf->iommu_group = NULL;
110 #endif
113 * Groups/devices can use the same or different IOMMU domains. If the same
114 * then adding a new group/device may change the coherency of groups/devices
115 * we've previously been told about. We don't want to care about any of
116 * that so we retest each group/device and bail as soon as we find one that's
117 * noncoherent. This means we only ever [un]register_noncoherent_dma once
118 * for the whole device.
120 static void kvm_vfio_update_coherency(struct kvm_device *dev)
122 struct kvm_vfio *kv = dev->private;
123 bool noncoherent = false;
124 struct kvm_vfio_file *kvf;
126 list_for_each_entry(kvf, &kv->file_list, node) {
127 if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
128 noncoherent = true;
129 break;
133 if (noncoherent != kv->noncoherent) {
134 kv->noncoherent = noncoherent;
136 if (kv->noncoherent)
137 kvm_arch_register_noncoherent_dma(dev->kvm);
138 else
139 kvm_arch_unregister_noncoherent_dma(dev->kvm);
143 static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
145 struct kvm_vfio *kv = dev->private;
146 struct kvm_vfio_file *kvf;
147 struct file *filp;
148 int ret = 0;
150 filp = fget(fd);
151 if (!filp)
152 return -EBADF;
154 /* Ensure the FD is a vfio FD. */
155 if (!kvm_vfio_file_is_valid(filp)) {
156 ret = -EINVAL;
157 goto out_fput;
160 mutex_lock(&kv->lock);
162 list_for_each_entry(kvf, &kv->file_list, node) {
163 if (kvf->file == filp) {
164 ret = -EEXIST;
165 goto out_unlock;
169 kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
170 if (!kvf) {
171 ret = -ENOMEM;
172 goto out_unlock;
175 kvf->file = get_file(filp);
176 list_add_tail(&kvf->node, &kv->file_list);
178 kvm_arch_start_assignment(dev->kvm);
179 kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
180 kvm_vfio_update_coherency(dev);
182 out_unlock:
183 mutex_unlock(&kv->lock);
184 out_fput:
185 fput(filp);
186 return ret;
189 static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
191 struct kvm_vfio *kv = dev->private;
192 struct kvm_vfio_file *kvf;
193 CLASS(fd, f)(fd);
194 int ret;
196 if (fd_empty(f))
197 return -EBADF;
199 ret = -ENOENT;
201 mutex_lock(&kv->lock);
203 list_for_each_entry(kvf, &kv->file_list, node) {
204 if (kvf->file != fd_file(f))
205 continue;
207 list_del(&kvf->node);
208 kvm_arch_end_assignment(dev->kvm);
209 #ifdef CONFIG_SPAPR_TCE_IOMMU
210 kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
211 #endif
212 kvm_vfio_file_set_kvm(kvf->file, NULL);
213 fput(kvf->file);
214 kfree(kvf);
215 ret = 0;
216 break;
219 kvm_vfio_update_coherency(dev);
221 mutex_unlock(&kv->lock);
222 return ret;
225 #ifdef CONFIG_SPAPR_TCE_IOMMU
226 static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
227 void __user *arg)
229 struct kvm_vfio_spapr_tce param;
230 struct kvm_vfio *kv = dev->private;
231 struct kvm_vfio_file *kvf;
232 int ret;
234 if (copy_from_user(&param, arg, sizeof(struct kvm_vfio_spapr_tce)))
235 return -EFAULT;
237 CLASS(fd, f)(param.groupfd);
238 if (fd_empty(f))
239 return -EBADF;
241 ret = -ENOENT;
243 mutex_lock(&kv->lock);
245 list_for_each_entry(kvf, &kv->file_list, node) {
246 if (kvf->file != fd_file(f))
247 continue;
249 if (!kvf->iommu_group) {
250 kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
251 if (WARN_ON_ONCE(!kvf->iommu_group)) {
252 ret = -EIO;
253 goto err_fdput;
257 ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
258 kvf->iommu_group);
259 break;
262 err_fdput:
263 mutex_unlock(&kv->lock);
264 return ret;
266 #endif
268 static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
269 void __user *arg)
271 int32_t __user *argp = arg;
272 int32_t fd;
274 switch (attr) {
275 case KVM_DEV_VFIO_FILE_ADD:
276 if (get_user(fd, argp))
277 return -EFAULT;
278 return kvm_vfio_file_add(dev, fd);
280 case KVM_DEV_VFIO_FILE_DEL:
281 if (get_user(fd, argp))
282 return -EFAULT;
283 return kvm_vfio_file_del(dev, fd);
285 #ifdef CONFIG_SPAPR_TCE_IOMMU
286 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
287 return kvm_vfio_file_set_spapr_tce(dev, arg);
288 #endif
291 return -ENXIO;
294 static int kvm_vfio_set_attr(struct kvm_device *dev,
295 struct kvm_device_attr *attr)
297 switch (attr->group) {
298 case KVM_DEV_VFIO_FILE:
299 return kvm_vfio_set_file(dev, attr->attr,
300 u64_to_user_ptr(attr->addr));
303 return -ENXIO;
306 static int kvm_vfio_has_attr(struct kvm_device *dev,
307 struct kvm_device_attr *attr)
309 switch (attr->group) {
310 case KVM_DEV_VFIO_FILE:
311 switch (attr->attr) {
312 case KVM_DEV_VFIO_FILE_ADD:
313 case KVM_DEV_VFIO_FILE_DEL:
314 #ifdef CONFIG_SPAPR_TCE_IOMMU
315 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
316 #endif
317 return 0;
320 break;
323 return -ENXIO;
326 static void kvm_vfio_release(struct kvm_device *dev)
328 struct kvm_vfio *kv = dev->private;
329 struct kvm_vfio_file *kvf, *tmp;
331 list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
332 #ifdef CONFIG_SPAPR_TCE_IOMMU
333 kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
334 #endif
335 kvm_vfio_file_set_kvm(kvf->file, NULL);
336 fput(kvf->file);
337 list_del(&kvf->node);
338 kfree(kvf);
339 kvm_arch_end_assignment(dev->kvm);
342 kvm_vfio_update_coherency(dev);
344 kfree(kv);
345 kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */
348 static int kvm_vfio_create(struct kvm_device *dev, u32 type);
350 static const struct kvm_device_ops kvm_vfio_ops = {
351 .name = "kvm-vfio",
352 .create = kvm_vfio_create,
353 .release = kvm_vfio_release,
354 .set_attr = kvm_vfio_set_attr,
355 .has_attr = kvm_vfio_has_attr,
358 static int kvm_vfio_create(struct kvm_device *dev, u32 type)
360 struct kvm_device *tmp;
361 struct kvm_vfio *kv;
363 lockdep_assert_held(&dev->kvm->lock);
365 /* Only one VFIO "device" per VM */
366 list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
367 if (tmp->ops == &kvm_vfio_ops)
368 return -EBUSY;
370 kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
371 if (!kv)
372 return -ENOMEM;
374 INIT_LIST_HEAD(&kv->file_list);
375 mutex_init(&kv->lock);
377 dev->private = kv;
379 return 0;
382 int kvm_vfio_ops_init(void)
384 return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
387 void kvm_vfio_ops_exit(void)
389 kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);