vfio_iommu_spapr_tce.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_4K) >> 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_4K) >> 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. switch (arg) {
  132. case VFIO_SPAPR_TCE_IOMMU:
  133. ret = 1;
  134. break;
  135. default:
  136. ret = vfio_spapr_iommu_eeh_ioctl(NULL, cmd, arg);
  137. break;
  138. }
  139. return (ret < 0) ? 0 : ret;
  140. case VFIO_IOMMU_SPAPR_TCE_GET_INFO: {
  141. struct vfio_iommu_spapr_tce_info info;
  142. struct iommu_table *tbl = container->tbl;
  143. if (WARN_ON(!tbl))
  144. return -ENXIO;
  145. minsz = offsetofend(struct vfio_iommu_spapr_tce_info,
  146. dma32_window_size);
  147. if (copy_from_user(&info, (void __user *)arg, minsz))
  148. return -EFAULT;
  149. if (info.argsz < minsz)
  150. return -EINVAL;
  151. info.dma32_window_start = tbl->it_offset << IOMMU_PAGE_SHIFT_4K;
  152. info.dma32_window_size = tbl->it_size << IOMMU_PAGE_SHIFT_4K;
  153. info.flags = 0;
  154. if (copy_to_user((void __user *)arg, &info, minsz))
  155. return -EFAULT;
  156. return 0;
  157. }
  158. case VFIO_IOMMU_MAP_DMA: {
  159. struct vfio_iommu_type1_dma_map param;
  160. struct iommu_table *tbl = container->tbl;
  161. unsigned long tce, i;
  162. if (!tbl)
  163. return -ENXIO;
  164. BUG_ON(!tbl->it_group);
  165. minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
  166. if (copy_from_user(&param, (void __user *)arg, minsz))
  167. return -EFAULT;
  168. if (param.argsz < minsz)
  169. return -EINVAL;
  170. if (param.flags & ~(VFIO_DMA_MAP_FLAG_READ |
  171. VFIO_DMA_MAP_FLAG_WRITE))
  172. return -EINVAL;
  173. if ((param.size & ~IOMMU_PAGE_MASK_4K) ||
  174. (param.vaddr & ~IOMMU_PAGE_MASK_4K))
  175. return -EINVAL;
  176. /* iova is checked by the IOMMU API */
  177. tce = param.vaddr;
  178. if (param.flags & VFIO_DMA_MAP_FLAG_READ)
  179. tce |= TCE_PCI_READ;
  180. if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
  181. tce |= TCE_PCI_WRITE;
  182. ret = iommu_tce_put_param_check(tbl, param.iova, tce);
  183. if (ret)
  184. return ret;
  185. for (i = 0; i < (param.size >> IOMMU_PAGE_SHIFT_4K); ++i) {
  186. ret = iommu_put_tce_user_mode(tbl,
  187. (param.iova >> IOMMU_PAGE_SHIFT_4K) + i,
  188. tce);
  189. if (ret)
  190. break;
  191. tce += IOMMU_PAGE_SIZE_4K;
  192. }
  193. if (ret)
  194. iommu_clear_tces_and_put_pages(tbl,
  195. param.iova >> IOMMU_PAGE_SHIFT_4K, i);
  196. iommu_flush_tce(tbl);
  197. return ret;
  198. }
  199. case VFIO_IOMMU_UNMAP_DMA: {
  200. struct vfio_iommu_type1_dma_unmap param;
  201. struct iommu_table *tbl = container->tbl;
  202. if (WARN_ON(!tbl))
  203. return -ENXIO;
  204. minsz = offsetofend(struct vfio_iommu_type1_dma_unmap,
  205. size);
  206. if (copy_from_user(&param, (void __user *)arg, minsz))
  207. return -EFAULT;
  208. if (param.argsz < minsz)
  209. return -EINVAL;
  210. /* No flag is supported now */
  211. if (param.flags)
  212. return -EINVAL;
  213. if (param.size & ~IOMMU_PAGE_MASK_4K)
  214. return -EINVAL;
  215. ret = iommu_tce_clear_param_check(tbl, param.iova, 0,
  216. param.size >> IOMMU_PAGE_SHIFT_4K);
  217. if (ret)
  218. return ret;
  219. ret = iommu_clear_tces_and_put_pages(tbl,
  220. param.iova >> IOMMU_PAGE_SHIFT_4K,
  221. param.size >> IOMMU_PAGE_SHIFT_4K);
  222. iommu_flush_tce(tbl);
  223. return ret;
  224. }
  225. case VFIO_IOMMU_ENABLE:
  226. mutex_lock(&container->lock);
  227. ret = tce_iommu_enable(container);
  228. mutex_unlock(&container->lock);
  229. return ret;
  230. case VFIO_IOMMU_DISABLE:
  231. mutex_lock(&container->lock);
  232. tce_iommu_disable(container);
  233. mutex_unlock(&container->lock);
  234. return 0;
  235. case VFIO_EEH_PE_OP:
  236. if (!container->tbl || !container->tbl->it_group)
  237. return -ENODEV;
  238. return vfio_spapr_iommu_eeh_ioctl(container->tbl->it_group,
  239. cmd, arg);
  240. }
  241. return -ENOTTY;
  242. }
  243. static int tce_iommu_attach_group(void *iommu_data,
  244. struct iommu_group *iommu_group)
  245. {
  246. int ret;
  247. struct tce_container *container = iommu_data;
  248. struct iommu_table *tbl = iommu_group_get_iommudata(iommu_group);
  249. BUG_ON(!tbl);
  250. mutex_lock(&container->lock);
  251. /* pr_debug("tce_vfio: Attaching group #%u to iommu %p\n",
  252. iommu_group_id(iommu_group), iommu_group); */
  253. if (container->tbl) {
  254. pr_warn("tce_vfio: Only one group per IOMMU container is allowed, existing id=%d, attaching id=%d\n",
  255. iommu_group_id(container->tbl->it_group),
  256. iommu_group_id(iommu_group));
  257. ret = -EBUSY;
  258. } else if (container->enabled) {
  259. pr_err("tce_vfio: attaching group #%u to enabled container\n",
  260. iommu_group_id(iommu_group));
  261. ret = -EBUSY;
  262. } else {
  263. ret = iommu_take_ownership(tbl);
  264. if (!ret)
  265. container->tbl = tbl;
  266. }
  267. mutex_unlock(&container->lock);
  268. return ret;
  269. }
  270. static void tce_iommu_detach_group(void *iommu_data,
  271. struct iommu_group *iommu_group)
  272. {
  273. struct tce_container *container = iommu_data;
  274. struct iommu_table *tbl = iommu_group_get_iommudata(iommu_group);
  275. BUG_ON(!tbl);
  276. mutex_lock(&container->lock);
  277. if (tbl != container->tbl) {
  278. pr_warn("tce_vfio: detaching group #%u, expected group is #%u\n",
  279. iommu_group_id(iommu_group),
  280. iommu_group_id(tbl->it_group));
  281. } else {
  282. if (container->enabled) {
  283. pr_warn("tce_vfio: detaching group #%u from enabled container, forcing disable\n",
  284. iommu_group_id(tbl->it_group));
  285. tce_iommu_disable(container);
  286. }
  287. /* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
  288. iommu_group_id(iommu_group), iommu_group); */
  289. container->tbl = NULL;
  290. iommu_release_ownership(tbl);
  291. }
  292. mutex_unlock(&container->lock);
  293. }
  294. const struct vfio_iommu_driver_ops tce_iommu_driver_ops = {
  295. .name = "iommu-vfio-powerpc",
  296. .owner = THIS_MODULE,
  297. .open = tce_iommu_open,
  298. .release = tce_iommu_release,
  299. .ioctl = tce_iommu_ioctl,
  300. .attach_group = tce_iommu_attach_group,
  301. .detach_group = tce_iommu_detach_group,
  302. };
  303. static int __init tce_iommu_init(void)
  304. {
  305. return vfio_register_iommu_driver(&tce_iommu_driver_ops);
  306. }
  307. static void __exit tce_iommu_cleanup(void)
  308. {
  309. vfio_unregister_iommu_driver(&tce_iommu_driver_ops);
  310. }
  311. module_init(tce_iommu_init);
  312. module_exit(tce_iommu_cleanup);
  313. MODULE_VERSION(DRIVER_VERSION);
  314. MODULE_LICENSE("GPL v2");
  315. MODULE_AUTHOR(DRIVER_AUTHOR);
  316. MODULE_DESCRIPTION(DRIVER_DESC);