vfio_iommu_spapr_tce.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * VFIO: IOMMU DMA mapping support for TCE on POWER
  3. *
  4. * Copyright (C) 2013 IBM Corp. All rights reserved.
  5. * Author: Alexey Kardashevskiy <aik@ozlabs.ru>
  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. * Derived from original vfio_iommu_type1.c:
  12. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  13. * Author: Alex Williamson <alex.williamson@redhat.com>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/err.h>
  20. #include <linux/vfio.h>
  21. #include <asm/iommu.h>
  22. #include <asm/tce.h>
  23. #define DRIVER_VERSION "0.1"
  24. #define DRIVER_AUTHOR "aik@ozlabs.ru"
  25. #define DRIVER_DESC "VFIO IOMMU SPAPR TCE"
  26. static void tce_iommu_detach_group(void *iommu_data,
  27. struct iommu_group *iommu_group);
  28. /*
  29. * VFIO IOMMU fd for SPAPR_TCE IOMMU implementation
  30. *
  31. * This code handles mapping and unmapping of user data buffers
  32. * into DMA'ble space using the IOMMU
  33. */
  34. /*
  35. * The container descriptor supports only a single group per container.
  36. * Required by the API as the container is not supplied with the IOMMU group
  37. * at the moment of initialization.
  38. */
  39. struct tce_container {
  40. struct mutex lock;
  41. struct iommu_table *tbl;
  42. bool enabled;
  43. };
  44. static int tce_iommu_enable(struct tce_container *container)
  45. {
  46. int ret = 0;
  47. unsigned long locked, lock_limit, npages;
  48. struct iommu_table *tbl = container->tbl;
  49. if (!container->tbl)
  50. return -ENXIO;
  51. if (!current->mm)
  52. return -ESRCH; /* process exited */
  53. if (container->enabled)
  54. return -EBUSY;
  55. /*
  56. * When userspace pages are mapped into the IOMMU, they are effectively
  57. * locked memory, so, theoretically, we need to update the accounting
  58. * of locked pages on each map and unmap. For powerpc, the map unmap
  59. * paths can be very hot, though, and the accounting would kill
  60. * performance, especially since it would be difficult to impossible
  61. * to handle the accounting in real mode only.
  62. *
  63. * To address that, rather than precisely accounting every page, we
  64. * instead account for a worst case on locked memory when the iommu is
  65. * enabled and disabled. The worst case upper bound on locked memory
  66. * is the size of the whole iommu window, which is usually relatively
  67. * small (compared to total memory sizes) on POWER hardware.
  68. *
  69. * Also we don't have a nice way to fail on H_PUT_TCE due to ulimits,
  70. * that would effectively kill the guest at random points, much better
  71. * enforcing the limit based on the max that the guest can map.
  72. */
  73. down_write(&current->mm->mmap_sem);
  74. npages = (tbl->it_size << IOMMU_PAGE_SHIFT) >> PAGE_SHIFT;
  75. locked = current->mm->locked_vm + npages;
  76. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  77. if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
  78. pr_warn("RLIMIT_MEMLOCK (%ld) exceeded\n",
  79. rlimit(RLIMIT_MEMLOCK));
  80. ret = -ENOMEM;
  81. } else {
  82. current->mm->locked_vm += npages;
  83. container->enabled = true;
  84. }
  85. up_write(&current->mm->mmap_sem);
  86. return ret;
  87. }
  88. static void tce_iommu_disable(struct tce_container *container)
  89. {
  90. if (!container->enabled)
  91. return;
  92. container->enabled = false;
  93. if (!container->tbl || !current->mm)
  94. return;
  95. down_write(&current->mm->mmap_sem);
  96. current->mm->locked_vm -= (container->tbl->it_size <<
  97. IOMMU_PAGE_SHIFT) >> PAGE_SHIFT;
  98. up_write(&current->mm->mmap_sem);
  99. }
  100. static void *tce_iommu_open(unsigned long arg)
  101. {
  102. struct tce_container *container;
  103. if (arg != VFIO_SPAPR_TCE_IOMMU) {
  104. pr_err("tce_vfio: Wrong IOMMU type\n");
  105. return ERR_PTR(-EINVAL);
  106. }
  107. container = kzalloc(sizeof(*container), GFP_KERNEL);
  108. if (!container)
  109. return ERR_PTR(-ENOMEM);
  110. mutex_init(&container->lock);
  111. return container;
  112. }
  113. static void tce_iommu_release(void *iommu_data)
  114. {
  115. struct tce_container *container = iommu_data;
  116. WARN_ON(container->tbl && !container->tbl->it_group);
  117. tce_iommu_disable(container);
  118. if (container->tbl && container->tbl->it_group)
  119. tce_iommu_detach_group(iommu_data, container->tbl->it_group);
  120. mutex_destroy(&container->lock);
  121. kfree(container);
  122. }
  123. static long tce_iommu_ioctl(void *iommu_data,
  124. unsigned int cmd, unsigned long arg)
  125. {
  126. struct tce_container *container = iommu_data;
  127. unsigned long minsz;
  128. long ret;
  129. switch (cmd) {
  130. case VFIO_CHECK_EXTENSION:
  131. return (arg == VFIO_SPAPR_TCE_IOMMU) ? 1 : 0;
  132. case VFIO_IOMMU_SPAPR_TCE_GET_INFO: {
  133. struct vfio_iommu_spapr_tce_info info;
  134. struct iommu_table *tbl = container->tbl;
  135. if (WARN_ON(!tbl))
  136. return -ENXIO;
  137. minsz = offsetofend(struct vfio_iommu_spapr_tce_info,
  138. dma32_window_size);
  139. if (copy_from_user(&info, (void __user *)arg, minsz))
  140. return -EFAULT;
  141. if (info.argsz < minsz)
  142. return -EINVAL;
  143. info.dma32_window_start = tbl->it_offset << IOMMU_PAGE_SHIFT;
  144. info.dma32_window_size = tbl->it_size << IOMMU_PAGE_SHIFT;
  145. info.flags = 0;
  146. if (copy_to_user((void __user *)arg, &info, minsz))
  147. return -EFAULT;
  148. return 0;
  149. }
  150. case VFIO_IOMMU_MAP_DMA: {
  151. struct vfio_iommu_type1_dma_map param;
  152. struct iommu_table *tbl = container->tbl;
  153. unsigned long tce, i;
  154. if (!tbl)
  155. return -ENXIO;
  156. BUG_ON(!tbl->it_group);
  157. minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
  158. if (copy_from_user(&param, (void __user *)arg, minsz))
  159. return -EFAULT;
  160. if (param.argsz < minsz)
  161. return -EINVAL;
  162. if (param.flags & ~(VFIO_DMA_MAP_FLAG_READ |
  163. VFIO_DMA_MAP_FLAG_WRITE))
  164. return -EINVAL;
  165. if ((param.size & ~IOMMU_PAGE_MASK) ||
  166. (param.vaddr & ~IOMMU_PAGE_MASK))
  167. return -EINVAL;
  168. /* iova is checked by the IOMMU API */
  169. tce = param.vaddr;
  170. if (param.flags & VFIO_DMA_MAP_FLAG_READ)
  171. tce |= TCE_PCI_READ;
  172. if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
  173. tce |= TCE_PCI_WRITE;
  174. ret = iommu_tce_put_param_check(tbl, param.iova, tce);
  175. if (ret)
  176. return ret;
  177. for (i = 0; i < (param.size >> IOMMU_PAGE_SHIFT); ++i) {
  178. ret = iommu_put_tce_user_mode(tbl,
  179. (param.iova >> IOMMU_PAGE_SHIFT) + i,
  180. tce);
  181. if (ret)
  182. break;
  183. tce += IOMMU_PAGE_SIZE;
  184. }
  185. if (ret)
  186. iommu_clear_tces_and_put_pages(tbl,
  187. param.iova >> IOMMU_PAGE_SHIFT, i);
  188. iommu_flush_tce(tbl);
  189. return ret;
  190. }
  191. case VFIO_IOMMU_UNMAP_DMA: {
  192. struct vfio_iommu_type1_dma_unmap param;
  193. struct iommu_table *tbl = container->tbl;
  194. if (WARN_ON(!tbl))
  195. return -ENXIO;
  196. minsz = offsetofend(struct vfio_iommu_type1_dma_unmap,
  197. size);
  198. if (copy_from_user(&param, (void __user *)arg, minsz))
  199. return -EFAULT;
  200. if (param.argsz < minsz)
  201. return -EINVAL;
  202. /* No flag is supported now */
  203. if (param.flags)
  204. return -EINVAL;
  205. if (param.size & ~IOMMU_PAGE_MASK)
  206. return -EINVAL;
  207. ret = iommu_tce_clear_param_check(tbl, param.iova, 0,
  208. param.size >> IOMMU_PAGE_SHIFT);
  209. if (ret)
  210. return ret;
  211. ret = iommu_clear_tces_and_put_pages(tbl,
  212. param.iova >> IOMMU_PAGE_SHIFT,
  213. param.size >> IOMMU_PAGE_SHIFT);
  214. iommu_flush_tce(tbl);
  215. return ret;
  216. }
  217. case VFIO_IOMMU_ENABLE:
  218. mutex_lock(&container->lock);
  219. ret = tce_iommu_enable(container);
  220. mutex_unlock(&container->lock);
  221. return ret;
  222. case VFIO_IOMMU_DISABLE:
  223. mutex_lock(&container->lock);
  224. tce_iommu_disable(container);
  225. mutex_unlock(&container->lock);
  226. return 0;
  227. }
  228. return -ENOTTY;
  229. }
  230. static int tce_iommu_attach_group(void *iommu_data,
  231. struct iommu_group *iommu_group)
  232. {
  233. int ret;
  234. struct tce_container *container = iommu_data;
  235. struct iommu_table *tbl = iommu_group_get_iommudata(iommu_group);
  236. BUG_ON(!tbl);
  237. mutex_lock(&container->lock);
  238. /* pr_debug("tce_vfio: Attaching group #%u to iommu %p\n",
  239. iommu_group_id(iommu_group), iommu_group); */
  240. if (container->tbl) {
  241. pr_warn("tce_vfio: Only one group per IOMMU container is allowed, existing id=%d, attaching id=%d\n",
  242. iommu_group_id(container->tbl->it_group),
  243. iommu_group_id(iommu_group));
  244. ret = -EBUSY;
  245. } else if (container->enabled) {
  246. pr_err("tce_vfio: attaching group #%u to enabled container\n",
  247. iommu_group_id(iommu_group));
  248. ret = -EBUSY;
  249. } else {
  250. ret = iommu_take_ownership(tbl);
  251. if (!ret)
  252. container->tbl = tbl;
  253. }
  254. mutex_unlock(&container->lock);
  255. return ret;
  256. }
  257. static void tce_iommu_detach_group(void *iommu_data,
  258. struct iommu_group *iommu_group)
  259. {
  260. struct tce_container *container = iommu_data;
  261. struct iommu_table *tbl = iommu_group_get_iommudata(iommu_group);
  262. BUG_ON(!tbl);
  263. mutex_lock(&container->lock);
  264. if (tbl != container->tbl) {
  265. pr_warn("tce_vfio: detaching group #%u, expected group is #%u\n",
  266. iommu_group_id(iommu_group),
  267. iommu_group_id(tbl->it_group));
  268. } else {
  269. if (container->enabled) {
  270. pr_warn("tce_vfio: detaching group #%u from enabled container, forcing disable\n",
  271. iommu_group_id(tbl->it_group));
  272. tce_iommu_disable(container);
  273. }
  274. /* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
  275. iommu_group_id(iommu_group), iommu_group); */
  276. container->tbl = NULL;
  277. iommu_release_ownership(tbl);
  278. }
  279. mutex_unlock(&container->lock);
  280. }
  281. const struct vfio_iommu_driver_ops tce_iommu_driver_ops = {
  282. .name = "iommu-vfio-powerpc",
  283. .owner = THIS_MODULE,
  284. .open = tce_iommu_open,
  285. .release = tce_iommu_release,
  286. .ioctl = tce_iommu_ioctl,
  287. .attach_group = tce_iommu_attach_group,
  288. .detach_group = tce_iommu_detach_group,
  289. };
  290. static int __init tce_iommu_init(void)
  291. {
  292. return vfio_register_iommu_driver(&tce_iommu_driver_ops);
  293. }
  294. static void __exit tce_iommu_cleanup(void)
  295. {
  296. vfio_unregister_iommu_driver(&tce_iommu_driver_ops);
  297. }
  298. module_init(tce_iommu_init);
  299. module_exit(tce_iommu_cleanup);
  300. MODULE_VERSION(DRIVER_VERSION);
  301. MODULE_LICENSE("GPL v2");
  302. MODULE_AUTHOR(DRIVER_AUTHOR);
  303. MODULE_DESCRIPTION(DRIVER_DESC);