1 // SPDX-License-Identifier: GPL-2.0-only
3 * VFIO-KVM bridge pseudo device
5 * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
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>
20 #ifdef CONFIG_SPAPR_TCE_IOMMU
21 #include <asm/kvm_ppc.h>
24 struct kvm_vfio_group {
25 struct list_head node;
26 struct vfio_group *vfio_group;
30 struct list_head group_list;
35 static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
37 struct vfio_group *vfio_group;
38 struct vfio_group *(*fn)(struct file *);
40 fn = symbol_get(vfio_group_get_external_user);
42 return ERR_PTR(-EINVAL);
44 vfio_group = fn(filep);
46 symbol_put(vfio_group_get_external_user);
51 static bool kvm_vfio_external_group_match_file(struct vfio_group *group,
54 bool ret, (*fn)(struct vfio_group *, struct file *);
56 fn = symbol_get(vfio_external_group_match_file);
60 ret = fn(group, filep);
62 symbol_put(vfio_external_group_match_file);
67 static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
69 void (*fn)(struct vfio_group *);
71 fn = symbol_get(vfio_group_put_external_user);
77 symbol_put(vfio_group_put_external_user);
80 static void kvm_vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
82 void (*fn)(struct vfio_group *, struct kvm *);
84 fn = symbol_get(vfio_group_set_kvm);
90 symbol_put(vfio_group_set_kvm);
93 static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group)
95 long (*fn)(struct vfio_group *, unsigned long);
98 fn = symbol_get(vfio_external_check_extension);
102 ret = fn(vfio_group, VFIO_DMA_CC_IOMMU);
104 symbol_put(vfio_external_check_extension);
109 #ifdef CONFIG_SPAPR_TCE_IOMMU
110 static int kvm_vfio_external_user_iommu_id(struct vfio_group *vfio_group)
112 int (*fn)(struct vfio_group *);
115 fn = symbol_get(vfio_external_user_iommu_id);
119 ret = fn(vfio_group);
121 symbol_put(vfio_external_user_iommu_id);
126 static struct iommu_group *kvm_vfio_group_get_iommu_group(
127 struct vfio_group *group)
129 int group_id = kvm_vfio_external_user_iommu_id(group);
134 return iommu_group_get_by_id(group_id);
137 static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
138 struct vfio_group *vfio_group)
140 struct iommu_group *grp = kvm_vfio_group_get_iommu_group(vfio_group);
142 if (WARN_ON_ONCE(!grp))
145 kvm_spapr_tce_release_iommu_group(kvm, grp);
146 iommu_group_put(grp);
151 * Groups can use the same or different IOMMU domains. If the same then
152 * adding a new group may change the coherency of groups we've previously
153 * been told about. We don't want to care about any of that so we retest
154 * each group and bail as soon as we find one that's noncoherent. This
155 * means we only ever [un]register_noncoherent_dma once for the whole device.
157 static void kvm_vfio_update_coherency(struct kvm_device *dev)
159 struct kvm_vfio *kv = dev->private;
160 bool noncoherent = false;
161 struct kvm_vfio_group *kvg;
163 mutex_lock(&kv->lock);
165 list_for_each_entry(kvg, &kv->group_list, node) {
166 if (!kvm_vfio_group_is_coherent(kvg->vfio_group)) {
172 if (noncoherent != kv->noncoherent) {
173 kv->noncoherent = noncoherent;
176 kvm_arch_register_noncoherent_dma(dev->kvm);
178 kvm_arch_unregister_noncoherent_dma(dev->kvm);
181 mutex_unlock(&kv->lock);
184 static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
186 struct kvm_vfio *kv = dev->private;
187 struct vfio_group *vfio_group;
188 struct kvm_vfio_group *kvg;
189 int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
195 case KVM_DEV_VFIO_GROUP_ADD:
196 if (get_user(fd, argp))
203 vfio_group = kvm_vfio_group_get_external_user(f.file);
206 if (IS_ERR(vfio_group))
207 return PTR_ERR(vfio_group);
209 mutex_lock(&kv->lock);
211 list_for_each_entry(kvg, &kv->group_list, node) {
212 if (kvg->vfio_group == vfio_group) {
213 mutex_unlock(&kv->lock);
214 kvm_vfio_group_put_external_user(vfio_group);
219 kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
221 mutex_unlock(&kv->lock);
222 kvm_vfio_group_put_external_user(vfio_group);
226 list_add_tail(&kvg->node, &kv->group_list);
227 kvg->vfio_group = vfio_group;
229 kvm_arch_start_assignment(dev->kvm);
231 mutex_unlock(&kv->lock);
233 kvm_vfio_group_set_kvm(vfio_group, dev->kvm);
235 kvm_vfio_update_coherency(dev);
239 case KVM_DEV_VFIO_GROUP_DEL:
240 if (get_user(fd, argp))
249 mutex_lock(&kv->lock);
251 list_for_each_entry(kvg, &kv->group_list, node) {
252 if (!kvm_vfio_external_group_match_file(kvg->vfio_group,
256 list_del(&kvg->node);
257 kvm_arch_end_assignment(dev->kvm);
258 #ifdef CONFIG_SPAPR_TCE_IOMMU
259 kvm_spapr_tce_release_vfio_group(dev->kvm,
262 kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
263 kvm_vfio_group_put_external_user(kvg->vfio_group);
269 mutex_unlock(&kv->lock);
273 kvm_vfio_update_coherency(dev);
277 #ifdef CONFIG_SPAPR_TCE_IOMMU
278 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: {
279 struct kvm_vfio_spapr_tce param;
280 struct kvm_vfio *kv = dev->private;
281 struct vfio_group *vfio_group;
282 struct kvm_vfio_group *kvg;
284 struct iommu_group *grp;
286 if (copy_from_user(¶m, (void __user *)arg,
287 sizeof(struct kvm_vfio_spapr_tce)))
290 f = fdget(param.groupfd);
294 vfio_group = kvm_vfio_group_get_external_user(f.file);
297 if (IS_ERR(vfio_group))
298 return PTR_ERR(vfio_group);
300 grp = kvm_vfio_group_get_iommu_group(vfio_group);
301 if (WARN_ON_ONCE(!grp)) {
302 kvm_vfio_group_put_external_user(vfio_group);
308 mutex_lock(&kv->lock);
310 list_for_each_entry(kvg, &kv->group_list, node) {
311 if (kvg->vfio_group != vfio_group)
314 ret = kvm_spapr_tce_attach_iommu_group(dev->kvm,
319 mutex_unlock(&kv->lock);
321 iommu_group_put(grp);
322 kvm_vfio_group_put_external_user(vfio_group);
326 #endif /* CONFIG_SPAPR_TCE_IOMMU */
332 static int kvm_vfio_set_attr(struct kvm_device *dev,
333 struct kvm_device_attr *attr)
335 switch (attr->group) {
336 case KVM_DEV_VFIO_GROUP:
337 return kvm_vfio_set_group(dev, attr->attr, attr->addr);
343 static int kvm_vfio_has_attr(struct kvm_device *dev,
344 struct kvm_device_attr *attr)
346 switch (attr->group) {
347 case KVM_DEV_VFIO_GROUP:
348 switch (attr->attr) {
349 case KVM_DEV_VFIO_GROUP_ADD:
350 case KVM_DEV_VFIO_GROUP_DEL:
351 #ifdef CONFIG_SPAPR_TCE_IOMMU
352 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
363 static void kvm_vfio_destroy(struct kvm_device *dev)
365 struct kvm_vfio *kv = dev->private;
366 struct kvm_vfio_group *kvg, *tmp;
368 list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
369 #ifdef CONFIG_SPAPR_TCE_IOMMU
370 kvm_spapr_tce_release_vfio_group(dev->kvm, kvg->vfio_group);
372 kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
373 kvm_vfio_group_put_external_user(kvg->vfio_group);
374 list_del(&kvg->node);
376 kvm_arch_end_assignment(dev->kvm);
379 kvm_vfio_update_coherency(dev);
382 kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
385 static int kvm_vfio_create(struct kvm_device *dev, u32 type);
387 static struct kvm_device_ops kvm_vfio_ops = {
389 .create = kvm_vfio_create,
390 .destroy = kvm_vfio_destroy,
391 .set_attr = kvm_vfio_set_attr,
392 .has_attr = kvm_vfio_has_attr,
395 static int kvm_vfio_create(struct kvm_device *dev, u32 type)
397 struct kvm_device *tmp;
400 /* Only one VFIO "device" per VM */
401 list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
402 if (tmp->ops == &kvm_vfio_ops)
405 kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
409 INIT_LIST_HEAD(&kv->group_list);
410 mutex_init(&kv->lock);
417 int kvm_vfio_ops_init(void)
419 return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
422 void kvm_vfio_ops_exit(void)
424 kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);