vfio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  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. #include <linux/errno.h>
  12. #include <linux/file.h>
  13. #include <linux/kvm_host.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/vfio.h>
  20. #include "vfio.h"
  21. #ifdef CONFIG_SPAPR_TCE_IOMMU
  22. #include <asm/kvm_ppc.h>
  23. #endif
  24. struct kvm_vfio_group {
  25. struct list_head node;
  26. struct vfio_group *vfio_group;
  27. };
  28. struct kvm_vfio {
  29. struct list_head group_list;
  30. struct mutex lock;
  31. bool noncoherent;
  32. };
  33. static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
  34. {
  35. struct vfio_group *vfio_group;
  36. struct vfio_group *(*fn)(struct file *);
  37. fn = symbol_get(vfio_group_get_external_user);
  38. if (!fn)
  39. return ERR_PTR(-EINVAL);
  40. vfio_group = fn(filep);
  41. symbol_put(vfio_group_get_external_user);
  42. return vfio_group;
  43. }
  44. static bool kvm_vfio_external_group_match_file(struct vfio_group *group,
  45. struct file *filep)
  46. {
  47. bool ret, (*fn)(struct vfio_group *, struct file *);
  48. fn = symbol_get(vfio_external_group_match_file);
  49. if (!fn)
  50. return false;
  51. ret = fn(group, filep);
  52. symbol_put(vfio_external_group_match_file);
  53. return ret;
  54. }
  55. static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
  56. {
  57. void (*fn)(struct vfio_group *);
  58. fn = symbol_get(vfio_group_put_external_user);
  59. if (!fn)
  60. return;
  61. fn(vfio_group);
  62. symbol_put(vfio_group_put_external_user);
  63. }
  64. static void kvm_vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
  65. {
  66. void (*fn)(struct vfio_group *, struct kvm *);
  67. fn = symbol_get(vfio_group_set_kvm);
  68. if (!fn)
  69. return;
  70. fn(group, kvm);
  71. symbol_put(vfio_group_set_kvm);
  72. }
  73. static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group)
  74. {
  75. long (*fn)(struct vfio_group *, unsigned long);
  76. long ret;
  77. fn = symbol_get(vfio_external_check_extension);
  78. if (!fn)
  79. return false;
  80. ret = fn(vfio_group, VFIO_DMA_CC_IOMMU);
  81. symbol_put(vfio_external_check_extension);
  82. return ret > 0;
  83. }
  84. #ifdef CONFIG_SPAPR_TCE_IOMMU
  85. static int kvm_vfio_external_user_iommu_id(struct vfio_group *vfio_group)
  86. {
  87. int (*fn)(struct vfio_group *);
  88. int ret = -EINVAL;
  89. fn = symbol_get(vfio_external_user_iommu_id);
  90. if (!fn)
  91. return ret;
  92. ret = fn(vfio_group);
  93. symbol_put(vfio_external_user_iommu_id);
  94. return ret;
  95. }
  96. static struct iommu_group *kvm_vfio_group_get_iommu_group(
  97. struct vfio_group *group)
  98. {
  99. int group_id = kvm_vfio_external_user_iommu_id(group);
  100. if (group_id < 0)
  101. return NULL;
  102. return iommu_group_get_by_id(group_id);
  103. }
  104. static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
  105. struct vfio_group *vfio_group)
  106. {
  107. struct iommu_group *grp = kvm_vfio_group_get_iommu_group(vfio_group);
  108. if (WARN_ON_ONCE(!grp))
  109. return;
  110. kvm_spapr_tce_release_iommu_group(kvm, grp);
  111. iommu_group_put(grp);
  112. }
  113. #endif
  114. /*
  115. * Groups can use the same or different IOMMU domains. If the same then
  116. * adding a new group may change the coherency of groups we've previously
  117. * been told about. We don't want to care about any of that so we retest
  118. * each group and bail as soon as we find one that's noncoherent. This
  119. * means we only ever [un]register_noncoherent_dma once for the whole device.
  120. */
  121. static void kvm_vfio_update_coherency(struct kvm_device *dev)
  122. {
  123. struct kvm_vfio *kv = dev->private;
  124. bool noncoherent = false;
  125. struct kvm_vfio_group *kvg;
  126. mutex_lock(&kv->lock);
  127. list_for_each_entry(kvg, &kv->group_list, node) {
  128. if (!kvm_vfio_group_is_coherent(kvg->vfio_group)) {
  129. noncoherent = true;
  130. break;
  131. }
  132. }
  133. if (noncoherent != kv->noncoherent) {
  134. kv->noncoherent = noncoherent;
  135. if (kv->noncoherent)
  136. kvm_arch_register_noncoherent_dma(dev->kvm);
  137. else
  138. kvm_arch_unregister_noncoherent_dma(dev->kvm);
  139. }
  140. mutex_unlock(&kv->lock);
  141. }
  142. static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
  143. {
  144. struct kvm_vfio *kv = dev->private;
  145. struct vfio_group *vfio_group;
  146. struct kvm_vfio_group *kvg;
  147. int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
  148. struct fd f;
  149. int32_t fd;
  150. int ret;
  151. switch (attr) {
  152. case KVM_DEV_VFIO_GROUP_ADD:
  153. if (get_user(fd, argp))
  154. return -EFAULT;
  155. f = fdget(fd);
  156. if (!f.file)
  157. return -EBADF;
  158. vfio_group = kvm_vfio_group_get_external_user(f.file);
  159. fdput(f);
  160. if (IS_ERR(vfio_group))
  161. return PTR_ERR(vfio_group);
  162. mutex_lock(&kv->lock);
  163. list_for_each_entry(kvg, &kv->group_list, node) {
  164. if (kvg->vfio_group == vfio_group) {
  165. mutex_unlock(&kv->lock);
  166. kvm_vfio_group_put_external_user(vfio_group);
  167. return -EEXIST;
  168. }
  169. }
  170. kvg = kzalloc(sizeof(*kvg), GFP_KERNEL);
  171. if (!kvg) {
  172. mutex_unlock(&kv->lock);
  173. kvm_vfio_group_put_external_user(vfio_group);
  174. return -ENOMEM;
  175. }
  176. list_add_tail(&kvg->node, &kv->group_list);
  177. kvg->vfio_group = vfio_group;
  178. kvm_arch_start_assignment(dev->kvm);
  179. mutex_unlock(&kv->lock);
  180. kvm_vfio_group_set_kvm(vfio_group, dev->kvm);
  181. kvm_vfio_update_coherency(dev);
  182. return 0;
  183. case KVM_DEV_VFIO_GROUP_DEL:
  184. if (get_user(fd, argp))
  185. return -EFAULT;
  186. f = fdget(fd);
  187. if (!f.file)
  188. return -EBADF;
  189. ret = -ENOENT;
  190. mutex_lock(&kv->lock);
  191. list_for_each_entry(kvg, &kv->group_list, node) {
  192. if (!kvm_vfio_external_group_match_file(kvg->vfio_group,
  193. f.file))
  194. continue;
  195. list_del(&kvg->node);
  196. kvm_arch_end_assignment(dev->kvm);
  197. #ifdef CONFIG_SPAPR_TCE_IOMMU
  198. kvm_spapr_tce_release_vfio_group(dev->kvm,
  199. kvg->vfio_group);
  200. #endif
  201. kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
  202. kvm_vfio_group_put_external_user(kvg->vfio_group);
  203. kfree(kvg);
  204. ret = 0;
  205. break;
  206. }
  207. mutex_unlock(&kv->lock);
  208. fdput(f);
  209. kvm_vfio_update_coherency(dev);
  210. return ret;
  211. #ifdef CONFIG_SPAPR_TCE_IOMMU
  212. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: {
  213. struct kvm_vfio_spapr_tce param;
  214. struct kvm_vfio *kv = dev->private;
  215. struct vfio_group *vfio_group;
  216. struct kvm_vfio_group *kvg;
  217. struct fd f;
  218. struct iommu_group *grp;
  219. if (copy_from_user(&param, (void __user *)arg,
  220. sizeof(struct kvm_vfio_spapr_tce)))
  221. return -EFAULT;
  222. f = fdget(param.groupfd);
  223. if (!f.file)
  224. return -EBADF;
  225. vfio_group = kvm_vfio_group_get_external_user(f.file);
  226. fdput(f);
  227. if (IS_ERR(vfio_group))
  228. return PTR_ERR(vfio_group);
  229. grp = kvm_vfio_group_get_iommu_group(vfio_group);
  230. if (WARN_ON_ONCE(!grp)) {
  231. kvm_vfio_group_put_external_user(vfio_group);
  232. return -EIO;
  233. }
  234. ret = -ENOENT;
  235. mutex_lock(&kv->lock);
  236. list_for_each_entry(kvg, &kv->group_list, node) {
  237. if (kvg->vfio_group != vfio_group)
  238. continue;
  239. ret = kvm_spapr_tce_attach_iommu_group(dev->kvm,
  240. param.tablefd, grp);
  241. break;
  242. }
  243. mutex_unlock(&kv->lock);
  244. iommu_group_put(grp);
  245. kvm_vfio_group_put_external_user(vfio_group);
  246. return ret;
  247. }
  248. #endif /* CONFIG_SPAPR_TCE_IOMMU */
  249. }
  250. return -ENXIO;
  251. }
  252. static int kvm_vfio_set_attr(struct kvm_device *dev,
  253. struct kvm_device_attr *attr)
  254. {
  255. switch (attr->group) {
  256. case KVM_DEV_VFIO_GROUP:
  257. return kvm_vfio_set_group(dev, attr->attr, attr->addr);
  258. }
  259. return -ENXIO;
  260. }
  261. static int kvm_vfio_has_attr(struct kvm_device *dev,
  262. struct kvm_device_attr *attr)
  263. {
  264. switch (attr->group) {
  265. case KVM_DEV_VFIO_GROUP:
  266. switch (attr->attr) {
  267. case KVM_DEV_VFIO_GROUP_ADD:
  268. case KVM_DEV_VFIO_GROUP_DEL:
  269. #ifdef CONFIG_SPAPR_TCE_IOMMU
  270. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
  271. #endif
  272. return 0;
  273. }
  274. break;
  275. }
  276. return -ENXIO;
  277. }
  278. static void kvm_vfio_destroy(struct kvm_device *dev)
  279. {
  280. struct kvm_vfio *kv = dev->private;
  281. struct kvm_vfio_group *kvg, *tmp;
  282. list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
  283. #ifdef CONFIG_SPAPR_TCE_IOMMU
  284. kvm_spapr_tce_release_vfio_group(dev->kvm, kvg->vfio_group);
  285. #endif
  286. kvm_vfio_group_set_kvm(kvg->vfio_group, NULL);
  287. kvm_vfio_group_put_external_user(kvg->vfio_group);
  288. list_del(&kvg->node);
  289. kfree(kvg);
  290. kvm_arch_end_assignment(dev->kvm);
  291. }
  292. kvm_vfio_update_coherency(dev);
  293. kfree(kv);
  294. kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
  295. }
  296. static int kvm_vfio_create(struct kvm_device *dev, u32 type);
  297. static struct kvm_device_ops kvm_vfio_ops = {
  298. .name = "kvm-vfio",
  299. .create = kvm_vfio_create,
  300. .destroy = kvm_vfio_destroy,
  301. .set_attr = kvm_vfio_set_attr,
  302. .has_attr = kvm_vfio_has_attr,
  303. };
  304. static int kvm_vfio_create(struct kvm_device *dev, u32 type)
  305. {
  306. struct kvm_device *tmp;
  307. struct kvm_vfio *kv;
  308. /* Only one VFIO "device" per VM */
  309. list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
  310. if (tmp->ops == &kvm_vfio_ops)
  311. return -EBUSY;
  312. kv = kzalloc(sizeof(*kv), GFP_KERNEL);
  313. if (!kv)
  314. return -ENOMEM;
  315. INIT_LIST_HEAD(&kv->group_list);
  316. mutex_init(&kv->lock);
  317. dev->private = kv;
  318. return 0;
  319. }
  320. int kvm_vfio_ops_init(void)
  321. {
  322. return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
  323. }
  324. void kvm_vfio_ops_exit(void)
  325. {
  326. kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
  327. }