Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * VFIO-KVM bridge pseudo device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2013 Red Hat, Inc.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *     Author: Alex Williamson <alex.williamson@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/vfio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "vfio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #ifdef CONFIG_SPAPR_TCE_IOMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/kvm_ppc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct kvm_vfio_group {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct vfio_group *vfio_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) struct kvm_vfio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct list_head group_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	bool noncoherent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct vfio_group *vfio_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct vfio_group *(*fn)(struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	fn = symbol_get(vfio_group_get_external_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	vfio_group = fn(filep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	symbol_put(vfio_group_get_external_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return vfio_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static bool kvm_vfio_external_group_match_file(struct vfio_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 					       struct file *filep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	bool ret, (*fn)(struct vfio_group *, struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	fn = symbol_get(vfio_external_group_match_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	ret = fn(group, filep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	symbol_put(vfio_external_group_match_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	void (*fn)(struct vfio_group *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	fn = symbol_get(vfio_group_put_external_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	fn(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	symbol_put(vfio_group_put_external_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static void kvm_vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	void (*fn)(struct vfio_group *, struct kvm *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	fn = symbol_get(vfio_group_set_kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	fn(group, kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	symbol_put(vfio_group_set_kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	long (*fn)(struct vfio_group *, unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	fn = symbol_get(vfio_external_check_extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ret = fn(vfio_group, VFIO_DMA_CC_IOMMU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	symbol_put(vfio_external_check_extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return ret > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #ifdef CONFIG_SPAPR_TCE_IOMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int kvm_vfio_external_user_iommu_id(struct vfio_group *vfio_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int (*fn)(struct vfio_group *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	fn = symbol_get(vfio_external_user_iommu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ret = fn(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	symbol_put(vfio_external_user_iommu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static struct iommu_group *kvm_vfio_group_get_iommu_group(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		struct vfio_group *group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int group_id = kvm_vfio_external_user_iommu_id(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (group_id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return iommu_group_get_by_id(group_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		struct vfio_group *vfio_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct iommu_group *grp = kvm_vfio_group_get_iommu_group(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (WARN_ON_ONCE(!grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	kvm_spapr_tce_release_iommu_group(kvm, grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	iommu_group_put(grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * Groups can use the same or different IOMMU domains.  If the same then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * adding a new group may change the coherency of groups we've previously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * been told about.  We don't want to care about any of that so we retest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * each group and bail as soon as we find one that's noncoherent.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * means we only ever [un]register_noncoherent_dma once for the whole device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void kvm_vfio_update_coherency(struct kvm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct kvm_vfio *kv = dev->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	bool noncoherent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct kvm_vfio_group *kvg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	mutex_lock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	list_for_each_entry(kvg, &kv->group_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		if (!kvm_vfio_group_is_coherent(kvg->vfio_group)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			noncoherent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (noncoherent != kv->noncoherent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		kv->noncoherent = noncoherent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		if (kv->noncoherent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			kvm_arch_register_noncoherent_dma(dev->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			kvm_arch_unregister_noncoherent_dma(dev->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	mutex_unlock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct kvm_vfio *kv = dev->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct vfio_group *vfio_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct kvm_vfio_group *kvg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	int32_t fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	case KVM_DEV_VFIO_GROUP_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		if (get_user(fd, argp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (!f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		vfio_group = kvm_vfio_group_get_external_user(f.file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		if (IS_ERR(vfio_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			return PTR_ERR(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		mutex_lock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		list_for_each_entry(kvg, &kv->group_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			if (kvg->vfio_group == vfio_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				mutex_unlock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				kvm_vfio_group_put_external_user(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		if (!kvg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			mutex_unlock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			kvm_vfio_group_put_external_user(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		list_add_tail(&kvg->node, &kv->group_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		kvg->vfio_group = vfio_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		kvm_arch_start_assignment(dev->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		mutex_unlock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		kvm_vfio_group_set_kvm(vfio_group, dev->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		kvm_vfio_update_coherency(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	case KVM_DEV_VFIO_GROUP_DEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (get_user(fd, argp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (!f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		mutex_lock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		list_for_each_entry(kvg, &kv->group_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			if (!kvm_vfio_external_group_match_file(kvg->vfio_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 								f.file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			list_del(&kvg->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			kvm_arch_end_assignment(dev->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) #ifdef CONFIG_SPAPR_TCE_IOMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			kvm_spapr_tce_release_vfio_group(dev->kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 							 kvg->vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			kvm_vfio_group_put_external_user(kvg->vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			kfree(kvg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		mutex_unlock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		kvm_vfio_update_coherency(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) #ifdef CONFIG_SPAPR_TCE_IOMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		struct kvm_vfio_spapr_tce param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		struct kvm_vfio *kv = dev->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		struct vfio_group *vfio_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		struct kvm_vfio_group *kvg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		struct iommu_group *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		if (copy_from_user(&param, (void __user *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 				sizeof(struct kvm_vfio_spapr_tce)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		f = fdget(param.groupfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		if (!f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		vfio_group = kvm_vfio_group_get_external_user(f.file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (IS_ERR(vfio_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			return PTR_ERR(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		grp = kvm_vfio_group_get_iommu_group(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		if (WARN_ON_ONCE(!grp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			kvm_vfio_group_put_external_user(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		mutex_lock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		list_for_each_entry(kvg, &kv->group_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			if (kvg->vfio_group != vfio_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			ret = kvm_spapr_tce_attach_iommu_group(dev->kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 					param.tablefd, grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		mutex_unlock(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		iommu_group_put(grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		kvm_vfio_group_put_external_user(vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) #endif /* CONFIG_SPAPR_TCE_IOMMU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int kvm_vfio_set_attr(struct kvm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			     struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	switch (attr->group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	case KVM_DEV_VFIO_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		return kvm_vfio_set_group(dev, attr->attr, attr->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static int kvm_vfio_has_attr(struct kvm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			     struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	switch (attr->group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	case KVM_DEV_VFIO_GROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		switch (attr->attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		case KVM_DEV_VFIO_GROUP_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		case KVM_DEV_VFIO_GROUP_DEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) #ifdef CONFIG_SPAPR_TCE_IOMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static void kvm_vfio_destroy(struct kvm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	struct kvm_vfio *kv = dev->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct kvm_vfio_group *kvg, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #ifdef CONFIG_SPAPR_TCE_IOMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		kvm_spapr_tce_release_vfio_group(dev->kvm, kvg->vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		kvm_vfio_group_put_external_user(kvg->vfio_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		list_del(&kvg->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		kfree(kvg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		kvm_arch_end_assignment(dev->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	kvm_vfio_update_coherency(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	kfree(kv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static int kvm_vfio_create(struct kvm_device *dev, u32 type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static struct kvm_device_ops kvm_vfio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	.name = "kvm-vfio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	.create = kvm_vfio_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	.destroy = kvm_vfio_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	.set_attr = kvm_vfio_set_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	.has_attr = kvm_vfio_has_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static int kvm_vfio_create(struct kvm_device *dev, u32 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	struct kvm_device *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	struct kvm_vfio *kv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	/* Only one VFIO "device" per VM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (tmp->ops == &kvm_vfio_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	if (!kv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	INIT_LIST_HEAD(&kv->group_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	mutex_init(&kv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	dev->private = kv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) int kvm_vfio_ops_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) void kvm_vfio_ops_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }