vfio_iommu_spapr_tce.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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 <linux/vmalloc.h>
  22. #include <asm/iommu.h>
  23. #include <asm/tce.h>
  24. #include <asm/mmu_context.h>
  25. #define DRIVER_VERSION "0.1"
  26. #define DRIVER_AUTHOR "aik@ozlabs.ru"
  27. #define DRIVER_DESC "VFIO IOMMU SPAPR TCE"
  28. static void tce_iommu_detach_group(void *iommu_data,
  29. struct iommu_group *iommu_group);
  30. static long try_increment_locked_vm(struct mm_struct *mm, long npages)
  31. {
  32. long ret = 0, locked, lock_limit;
  33. if (WARN_ON_ONCE(!mm))
  34. return -EPERM;
  35. if (!npages)
  36. return 0;
  37. down_write(&mm->mmap_sem);
  38. locked = mm->locked_vm + npages;
  39. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  40. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  41. ret = -ENOMEM;
  42. else
  43. mm->locked_vm += npages;
  44. pr_debug("[%d] RLIMIT_MEMLOCK +%ld %ld/%ld%s\n", current->pid,
  45. npages << PAGE_SHIFT,
  46. mm->locked_vm << PAGE_SHIFT,
  47. rlimit(RLIMIT_MEMLOCK),
  48. ret ? " - exceeded" : "");
  49. up_write(&mm->mmap_sem);
  50. return ret;
  51. }
  52. static void decrement_locked_vm(struct mm_struct *mm, long npages)
  53. {
  54. if (!mm || !npages)
  55. return;
  56. down_write(&mm->mmap_sem);
  57. if (WARN_ON_ONCE(npages > mm->locked_vm))
  58. npages = mm->locked_vm;
  59. mm->locked_vm -= npages;
  60. pr_debug("[%d] RLIMIT_MEMLOCK -%ld %ld/%ld\n", current->pid,
  61. npages << PAGE_SHIFT,
  62. mm->locked_vm << PAGE_SHIFT,
  63. rlimit(RLIMIT_MEMLOCK));
  64. up_write(&mm->mmap_sem);
  65. }
  66. /*
  67. * VFIO IOMMU fd for SPAPR_TCE IOMMU implementation
  68. *
  69. * This code handles mapping and unmapping of user data buffers
  70. * into DMA'ble space using the IOMMU
  71. */
  72. struct tce_iommu_group {
  73. struct list_head next;
  74. struct iommu_group *grp;
  75. };
  76. /*
  77. * A container needs to remember which preregistered region it has
  78. * referenced to do proper cleanup at the userspace process exit.
  79. */
  80. struct tce_iommu_prereg {
  81. struct list_head next;
  82. struct mm_iommu_table_group_mem_t *mem;
  83. };
  84. /*
  85. * The container descriptor supports only a single group per container.
  86. * Required by the API as the container is not supplied with the IOMMU group
  87. * at the moment of initialization.
  88. */
  89. struct tce_container {
  90. struct mutex lock;
  91. bool enabled;
  92. bool v2;
  93. bool def_window_pending;
  94. unsigned long locked_pages;
  95. struct mm_struct *mm;
  96. struct iommu_table *tables[IOMMU_TABLE_GROUP_MAX_TABLES];
  97. struct list_head group_list;
  98. struct list_head prereg_list;
  99. };
  100. static long tce_iommu_mm_set(struct tce_container *container)
  101. {
  102. if (container->mm) {
  103. if (container->mm == current->mm)
  104. return 0;
  105. return -EPERM;
  106. }
  107. BUG_ON(!current->mm);
  108. container->mm = current->mm;
  109. atomic_inc(&container->mm->mm_count);
  110. return 0;
  111. }
  112. static long tce_iommu_prereg_free(struct tce_container *container,
  113. struct tce_iommu_prereg *tcemem)
  114. {
  115. long ret;
  116. ret = mm_iommu_put(container->mm, tcemem->mem);
  117. if (ret)
  118. return ret;
  119. list_del(&tcemem->next);
  120. kfree(tcemem);
  121. return 0;
  122. }
  123. static long tce_iommu_unregister_pages(struct tce_container *container,
  124. __u64 vaddr, __u64 size)
  125. {
  126. struct mm_iommu_table_group_mem_t *mem;
  127. struct tce_iommu_prereg *tcemem;
  128. bool found = false;
  129. if ((vaddr & ~PAGE_MASK) || (size & ~PAGE_MASK))
  130. return -EINVAL;
  131. mem = mm_iommu_find(container->mm, vaddr, size >> PAGE_SHIFT);
  132. if (!mem)
  133. return -ENOENT;
  134. list_for_each_entry(tcemem, &container->prereg_list, next) {
  135. if (tcemem->mem == mem) {
  136. found = true;
  137. break;
  138. }
  139. }
  140. if (!found)
  141. return -ENOENT;
  142. return tce_iommu_prereg_free(container, tcemem);
  143. }
  144. static long tce_iommu_register_pages(struct tce_container *container,
  145. __u64 vaddr, __u64 size)
  146. {
  147. long ret = 0;
  148. struct mm_iommu_table_group_mem_t *mem = NULL;
  149. struct tce_iommu_prereg *tcemem;
  150. unsigned long entries = size >> PAGE_SHIFT;
  151. if ((vaddr & ~PAGE_MASK) || (size & ~PAGE_MASK) ||
  152. ((vaddr + size) < vaddr))
  153. return -EINVAL;
  154. mem = mm_iommu_find(container->mm, vaddr, entries);
  155. if (mem) {
  156. list_for_each_entry(tcemem, &container->prereg_list, next) {
  157. if (tcemem->mem == mem)
  158. return -EBUSY;
  159. }
  160. }
  161. ret = mm_iommu_get(container->mm, vaddr, entries, &mem);
  162. if (ret)
  163. return ret;
  164. tcemem = kzalloc(sizeof(*tcemem), GFP_KERNEL);
  165. tcemem->mem = mem;
  166. list_add(&tcemem->next, &container->prereg_list);
  167. container->enabled = true;
  168. return 0;
  169. }
  170. static long tce_iommu_userspace_view_alloc(struct iommu_table *tbl,
  171. struct mm_struct *mm)
  172. {
  173. unsigned long cb = _ALIGN_UP(sizeof(tbl->it_userspace[0]) *
  174. tbl->it_size, PAGE_SIZE);
  175. unsigned long *uas;
  176. long ret;
  177. BUG_ON(tbl->it_userspace);
  178. ret = try_increment_locked_vm(mm, cb >> PAGE_SHIFT);
  179. if (ret)
  180. return ret;
  181. uas = vzalloc(cb);
  182. if (!uas) {
  183. decrement_locked_vm(mm, cb >> PAGE_SHIFT);
  184. return -ENOMEM;
  185. }
  186. tbl->it_userspace = uas;
  187. return 0;
  188. }
  189. static void tce_iommu_userspace_view_free(struct iommu_table *tbl,
  190. struct mm_struct *mm)
  191. {
  192. unsigned long cb = _ALIGN_UP(sizeof(tbl->it_userspace[0]) *
  193. tbl->it_size, PAGE_SIZE);
  194. if (!tbl->it_userspace)
  195. return;
  196. vfree(tbl->it_userspace);
  197. tbl->it_userspace = NULL;
  198. decrement_locked_vm(mm, cb >> PAGE_SHIFT);
  199. }
  200. static bool tce_page_is_contained(struct page *page, unsigned page_shift)
  201. {
  202. /*
  203. * Check that the TCE table granularity is not bigger than the size of
  204. * a page we just found. Otherwise the hardware can get access to
  205. * a bigger memory chunk that it should.
  206. */
  207. return (PAGE_SHIFT + compound_order(compound_head(page))) >= page_shift;
  208. }
  209. static inline bool tce_groups_attached(struct tce_container *container)
  210. {
  211. return !list_empty(&container->group_list);
  212. }
  213. static long tce_iommu_find_table(struct tce_container *container,
  214. phys_addr_t ioba, struct iommu_table **ptbl)
  215. {
  216. long i;
  217. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  218. struct iommu_table *tbl = container->tables[i];
  219. if (tbl) {
  220. unsigned long entry = ioba >> tbl->it_page_shift;
  221. unsigned long start = tbl->it_offset;
  222. unsigned long end = start + tbl->it_size;
  223. if ((start <= entry) && (entry < end)) {
  224. *ptbl = tbl;
  225. return i;
  226. }
  227. }
  228. }
  229. return -1;
  230. }
  231. static int tce_iommu_find_free_table(struct tce_container *container)
  232. {
  233. int i;
  234. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  235. if (!container->tables[i])
  236. return i;
  237. }
  238. return -ENOSPC;
  239. }
  240. static int tce_iommu_enable(struct tce_container *container)
  241. {
  242. int ret = 0;
  243. unsigned long locked;
  244. struct iommu_table_group *table_group;
  245. struct tce_iommu_group *tcegrp;
  246. if (container->enabled)
  247. return -EBUSY;
  248. /*
  249. * When userspace pages are mapped into the IOMMU, they are effectively
  250. * locked memory, so, theoretically, we need to update the accounting
  251. * of locked pages on each map and unmap. For powerpc, the map unmap
  252. * paths can be very hot, though, and the accounting would kill
  253. * performance, especially since it would be difficult to impossible
  254. * to handle the accounting in real mode only.
  255. *
  256. * To address that, rather than precisely accounting every page, we
  257. * instead account for a worst case on locked memory when the iommu is
  258. * enabled and disabled. The worst case upper bound on locked memory
  259. * is the size of the whole iommu window, which is usually relatively
  260. * small (compared to total memory sizes) on POWER hardware.
  261. *
  262. * Also we don't have a nice way to fail on H_PUT_TCE due to ulimits,
  263. * that would effectively kill the guest at random points, much better
  264. * enforcing the limit based on the max that the guest can map.
  265. *
  266. * Unfortunately at the moment it counts whole tables, no matter how
  267. * much memory the guest has. I.e. for 4GB guest and 4 IOMMU groups
  268. * each with 2GB DMA window, 8GB will be counted here. The reason for
  269. * this is that we cannot tell here the amount of RAM used by the guest
  270. * as this information is only available from KVM and VFIO is
  271. * KVM agnostic.
  272. *
  273. * So we do not allow enabling a container without a group attached
  274. * as there is no way to know how much we should increment
  275. * the locked_vm counter.
  276. */
  277. if (!tce_groups_attached(container))
  278. return -ENODEV;
  279. tcegrp = list_first_entry(&container->group_list,
  280. struct tce_iommu_group, next);
  281. table_group = iommu_group_get_iommudata(tcegrp->grp);
  282. if (!table_group)
  283. return -ENODEV;
  284. if (!table_group->tce32_size)
  285. return -EPERM;
  286. ret = tce_iommu_mm_set(container);
  287. if (ret)
  288. return ret;
  289. locked = table_group->tce32_size >> PAGE_SHIFT;
  290. ret = try_increment_locked_vm(container->mm, locked);
  291. if (ret)
  292. return ret;
  293. container->locked_pages = locked;
  294. container->enabled = true;
  295. return ret;
  296. }
  297. static void tce_iommu_disable(struct tce_container *container)
  298. {
  299. if (!container->enabled)
  300. return;
  301. container->enabled = false;
  302. BUG_ON(!container->mm);
  303. decrement_locked_vm(container->mm, container->locked_pages);
  304. }
  305. static void *tce_iommu_open(unsigned long arg)
  306. {
  307. struct tce_container *container;
  308. if ((arg != VFIO_SPAPR_TCE_IOMMU) && (arg != VFIO_SPAPR_TCE_v2_IOMMU)) {
  309. pr_err("tce_vfio: Wrong IOMMU type\n");
  310. return ERR_PTR(-EINVAL);
  311. }
  312. container = kzalloc(sizeof(*container), GFP_KERNEL);
  313. if (!container)
  314. return ERR_PTR(-ENOMEM);
  315. mutex_init(&container->lock);
  316. INIT_LIST_HEAD_RCU(&container->group_list);
  317. INIT_LIST_HEAD_RCU(&container->prereg_list);
  318. container->v2 = arg == VFIO_SPAPR_TCE_v2_IOMMU;
  319. return container;
  320. }
  321. static int tce_iommu_clear(struct tce_container *container,
  322. struct iommu_table *tbl,
  323. unsigned long entry, unsigned long pages);
  324. static void tce_iommu_free_table(struct tce_container *container,
  325. struct iommu_table *tbl);
  326. static void tce_iommu_release(void *iommu_data)
  327. {
  328. struct tce_container *container = iommu_data;
  329. struct tce_iommu_group *tcegrp;
  330. long i;
  331. while (tce_groups_attached(container)) {
  332. tcegrp = list_first_entry(&container->group_list,
  333. struct tce_iommu_group, next);
  334. tce_iommu_detach_group(iommu_data, tcegrp->grp);
  335. }
  336. /*
  337. * If VFIO created a table, it was not disposed
  338. * by tce_iommu_detach_group() so do it now.
  339. */
  340. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  341. struct iommu_table *tbl = container->tables[i];
  342. if (!tbl)
  343. continue;
  344. tce_iommu_clear(container, tbl, tbl->it_offset, tbl->it_size);
  345. tce_iommu_free_table(container, tbl);
  346. }
  347. while (!list_empty(&container->prereg_list)) {
  348. struct tce_iommu_prereg *tcemem;
  349. tcemem = list_first_entry(&container->prereg_list,
  350. struct tce_iommu_prereg, next);
  351. WARN_ON_ONCE(tce_iommu_prereg_free(container, tcemem));
  352. }
  353. tce_iommu_disable(container);
  354. if (container->mm)
  355. mmdrop(container->mm);
  356. mutex_destroy(&container->lock);
  357. kfree(container);
  358. }
  359. static void tce_iommu_unuse_page(struct tce_container *container,
  360. unsigned long hpa)
  361. {
  362. struct page *page;
  363. page = pfn_to_page(hpa >> PAGE_SHIFT);
  364. put_page(page);
  365. }
  366. static int tce_iommu_prereg_ua_to_hpa(struct tce_container *container,
  367. unsigned long tce, unsigned long size,
  368. unsigned long *phpa, struct mm_iommu_table_group_mem_t **pmem)
  369. {
  370. long ret = 0;
  371. struct mm_iommu_table_group_mem_t *mem;
  372. mem = mm_iommu_lookup(container->mm, tce, size);
  373. if (!mem)
  374. return -EINVAL;
  375. ret = mm_iommu_ua_to_hpa(mem, tce, phpa);
  376. if (ret)
  377. return -EINVAL;
  378. *pmem = mem;
  379. return 0;
  380. }
  381. static void tce_iommu_unuse_page_v2(struct tce_container *container,
  382. struct iommu_table *tbl, unsigned long entry)
  383. {
  384. struct mm_iommu_table_group_mem_t *mem = NULL;
  385. int ret;
  386. unsigned long hpa = 0;
  387. unsigned long *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
  388. if (!pua)
  389. return;
  390. ret = tce_iommu_prereg_ua_to_hpa(container, *pua, IOMMU_PAGE_SIZE(tbl),
  391. &hpa, &mem);
  392. if (ret)
  393. pr_debug("%s: tce %lx at #%lx was not cached, ret=%d\n",
  394. __func__, *pua, entry, ret);
  395. if (mem)
  396. mm_iommu_mapped_dec(mem);
  397. *pua = 0;
  398. }
  399. static int tce_iommu_clear(struct tce_container *container,
  400. struct iommu_table *tbl,
  401. unsigned long entry, unsigned long pages)
  402. {
  403. unsigned long oldhpa;
  404. long ret;
  405. enum dma_data_direction direction;
  406. for ( ; pages; --pages, ++entry) {
  407. direction = DMA_NONE;
  408. oldhpa = 0;
  409. ret = iommu_tce_xchg(tbl, entry, &oldhpa, &direction);
  410. if (ret)
  411. continue;
  412. if (direction == DMA_NONE)
  413. continue;
  414. if (container->v2) {
  415. tce_iommu_unuse_page_v2(container, tbl, entry);
  416. continue;
  417. }
  418. tce_iommu_unuse_page(container, oldhpa);
  419. }
  420. return 0;
  421. }
  422. static int tce_iommu_use_page(unsigned long tce, unsigned long *hpa)
  423. {
  424. struct page *page = NULL;
  425. enum dma_data_direction direction = iommu_tce_direction(tce);
  426. if (get_user_pages_fast(tce & PAGE_MASK, 1,
  427. direction != DMA_TO_DEVICE, &page) != 1)
  428. return -EFAULT;
  429. *hpa = __pa((unsigned long) page_address(page));
  430. return 0;
  431. }
  432. static long tce_iommu_build(struct tce_container *container,
  433. struct iommu_table *tbl,
  434. unsigned long entry, unsigned long tce, unsigned long pages,
  435. enum dma_data_direction direction)
  436. {
  437. long i, ret = 0;
  438. struct page *page;
  439. unsigned long hpa;
  440. enum dma_data_direction dirtmp;
  441. for (i = 0; i < pages; ++i) {
  442. unsigned long offset = tce & IOMMU_PAGE_MASK(tbl) & ~PAGE_MASK;
  443. ret = tce_iommu_use_page(tce, &hpa);
  444. if (ret)
  445. break;
  446. page = pfn_to_page(hpa >> PAGE_SHIFT);
  447. if (!tce_page_is_contained(page, tbl->it_page_shift)) {
  448. ret = -EPERM;
  449. break;
  450. }
  451. hpa |= offset;
  452. dirtmp = direction;
  453. ret = iommu_tce_xchg(tbl, entry + i, &hpa, &dirtmp);
  454. if (ret) {
  455. tce_iommu_unuse_page(container, hpa);
  456. pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
  457. __func__, entry << tbl->it_page_shift,
  458. tce, ret);
  459. break;
  460. }
  461. if (dirtmp != DMA_NONE)
  462. tce_iommu_unuse_page(container, hpa);
  463. tce += IOMMU_PAGE_SIZE(tbl);
  464. }
  465. if (ret)
  466. tce_iommu_clear(container, tbl, entry, i);
  467. return ret;
  468. }
  469. static long tce_iommu_build_v2(struct tce_container *container,
  470. struct iommu_table *tbl,
  471. unsigned long entry, unsigned long tce, unsigned long pages,
  472. enum dma_data_direction direction)
  473. {
  474. long i, ret = 0;
  475. struct page *page;
  476. unsigned long hpa;
  477. enum dma_data_direction dirtmp;
  478. if (!tbl->it_userspace) {
  479. ret = tce_iommu_userspace_view_alloc(tbl, container->mm);
  480. if (ret)
  481. return ret;
  482. }
  483. for (i = 0; i < pages; ++i) {
  484. struct mm_iommu_table_group_mem_t *mem = NULL;
  485. unsigned long *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl,
  486. entry + i);
  487. ret = tce_iommu_prereg_ua_to_hpa(container,
  488. tce, IOMMU_PAGE_SIZE(tbl), &hpa, &mem);
  489. if (ret)
  490. break;
  491. page = pfn_to_page(hpa >> PAGE_SHIFT);
  492. if (!tce_page_is_contained(page, tbl->it_page_shift)) {
  493. ret = -EPERM;
  494. break;
  495. }
  496. /* Preserve offset within IOMMU page */
  497. hpa |= tce & IOMMU_PAGE_MASK(tbl) & ~PAGE_MASK;
  498. dirtmp = direction;
  499. /* The registered region is being unregistered */
  500. if (mm_iommu_mapped_inc(mem))
  501. break;
  502. ret = iommu_tce_xchg(tbl, entry + i, &hpa, &dirtmp);
  503. if (ret) {
  504. /* dirtmp cannot be DMA_NONE here */
  505. tce_iommu_unuse_page_v2(container, tbl, entry + i);
  506. pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
  507. __func__, entry << tbl->it_page_shift,
  508. tce, ret);
  509. break;
  510. }
  511. if (dirtmp != DMA_NONE)
  512. tce_iommu_unuse_page_v2(container, tbl, entry + i);
  513. *pua = tce;
  514. tce += IOMMU_PAGE_SIZE(tbl);
  515. }
  516. if (ret)
  517. tce_iommu_clear(container, tbl, entry, i);
  518. return ret;
  519. }
  520. static long tce_iommu_create_table(struct tce_container *container,
  521. struct iommu_table_group *table_group,
  522. int num,
  523. __u32 page_shift,
  524. __u64 window_size,
  525. __u32 levels,
  526. struct iommu_table **ptbl)
  527. {
  528. long ret, table_size;
  529. table_size = table_group->ops->get_table_size(page_shift, window_size,
  530. levels);
  531. if (!table_size)
  532. return -EINVAL;
  533. ret = try_increment_locked_vm(container->mm, table_size >> PAGE_SHIFT);
  534. if (ret)
  535. return ret;
  536. ret = table_group->ops->create_table(table_group, num,
  537. page_shift, window_size, levels, ptbl);
  538. WARN_ON(!ret && !(*ptbl)->it_ops->free);
  539. WARN_ON(!ret && ((*ptbl)->it_allocated_size != table_size));
  540. return ret;
  541. }
  542. static void tce_iommu_free_table(struct tce_container *container,
  543. struct iommu_table *tbl)
  544. {
  545. unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT;
  546. tce_iommu_userspace_view_free(tbl, container->mm);
  547. tbl->it_ops->free(tbl);
  548. decrement_locked_vm(container->mm, pages);
  549. }
  550. static long tce_iommu_create_window(struct tce_container *container,
  551. __u32 page_shift, __u64 window_size, __u32 levels,
  552. __u64 *start_addr)
  553. {
  554. struct tce_iommu_group *tcegrp;
  555. struct iommu_table_group *table_group;
  556. struct iommu_table *tbl = NULL;
  557. long ret, num;
  558. num = tce_iommu_find_free_table(container);
  559. if (num < 0)
  560. return num;
  561. /* Get the first group for ops::create_table */
  562. tcegrp = list_first_entry(&container->group_list,
  563. struct tce_iommu_group, next);
  564. table_group = iommu_group_get_iommudata(tcegrp->grp);
  565. if (!table_group)
  566. return -EFAULT;
  567. if (!(table_group->pgsizes & (1ULL << page_shift)))
  568. return -EINVAL;
  569. if (!table_group->ops->set_window || !table_group->ops->unset_window ||
  570. !table_group->ops->get_table_size ||
  571. !table_group->ops->create_table)
  572. return -EPERM;
  573. /* Create TCE table */
  574. ret = tce_iommu_create_table(container, table_group, num,
  575. page_shift, window_size, levels, &tbl);
  576. if (ret)
  577. return ret;
  578. BUG_ON(!tbl->it_ops->free);
  579. /*
  580. * Program the table to every group.
  581. * Groups have been tested for compatibility at the attach time.
  582. */
  583. list_for_each_entry(tcegrp, &container->group_list, next) {
  584. table_group = iommu_group_get_iommudata(tcegrp->grp);
  585. ret = table_group->ops->set_window(table_group, num, tbl);
  586. if (ret)
  587. goto unset_exit;
  588. }
  589. container->tables[num] = tbl;
  590. /* Return start address assigned by platform in create_table() */
  591. *start_addr = tbl->it_offset << tbl->it_page_shift;
  592. return 0;
  593. unset_exit:
  594. list_for_each_entry(tcegrp, &container->group_list, next) {
  595. table_group = iommu_group_get_iommudata(tcegrp->grp);
  596. table_group->ops->unset_window(table_group, num);
  597. }
  598. tce_iommu_free_table(container, tbl);
  599. return ret;
  600. }
  601. static long tce_iommu_remove_window(struct tce_container *container,
  602. __u64 start_addr)
  603. {
  604. struct iommu_table_group *table_group = NULL;
  605. struct iommu_table *tbl;
  606. struct tce_iommu_group *tcegrp;
  607. int num;
  608. num = tce_iommu_find_table(container, start_addr, &tbl);
  609. if (num < 0)
  610. return -EINVAL;
  611. BUG_ON(!tbl->it_size);
  612. /* Detach groups from IOMMUs */
  613. list_for_each_entry(tcegrp, &container->group_list, next) {
  614. table_group = iommu_group_get_iommudata(tcegrp->grp);
  615. /*
  616. * SPAPR TCE IOMMU exposes the default DMA window to
  617. * the guest via dma32_window_start/size of
  618. * VFIO_IOMMU_SPAPR_TCE_GET_INFO. Some platforms allow
  619. * the userspace to remove this window, some do not so
  620. * here we check for the platform capability.
  621. */
  622. if (!table_group->ops || !table_group->ops->unset_window)
  623. return -EPERM;
  624. table_group->ops->unset_window(table_group, num);
  625. }
  626. /* Free table */
  627. tce_iommu_clear(container, tbl, tbl->it_offset, tbl->it_size);
  628. tce_iommu_free_table(container, tbl);
  629. container->tables[num] = NULL;
  630. return 0;
  631. }
  632. static long tce_iommu_create_default_window(struct tce_container *container)
  633. {
  634. long ret;
  635. __u64 start_addr = 0;
  636. struct tce_iommu_group *tcegrp;
  637. struct iommu_table_group *table_group;
  638. if (!container->def_window_pending)
  639. return 0;
  640. if (!tce_groups_attached(container))
  641. return -ENODEV;
  642. tcegrp = list_first_entry(&container->group_list,
  643. struct tce_iommu_group, next);
  644. table_group = iommu_group_get_iommudata(tcegrp->grp);
  645. if (!table_group)
  646. return -ENODEV;
  647. ret = tce_iommu_create_window(container, IOMMU_PAGE_SHIFT_4K,
  648. table_group->tce32_size, 1, &start_addr);
  649. WARN_ON_ONCE(!ret && start_addr);
  650. if (!ret)
  651. container->def_window_pending = false;
  652. return ret;
  653. }
  654. static long tce_iommu_ioctl(void *iommu_data,
  655. unsigned int cmd, unsigned long arg)
  656. {
  657. struct tce_container *container = iommu_data;
  658. unsigned long minsz, ddwsz;
  659. long ret;
  660. switch (cmd) {
  661. case VFIO_CHECK_EXTENSION:
  662. switch (arg) {
  663. case VFIO_SPAPR_TCE_IOMMU:
  664. case VFIO_SPAPR_TCE_v2_IOMMU:
  665. ret = 1;
  666. break;
  667. default:
  668. ret = vfio_spapr_iommu_eeh_ioctl(NULL, cmd, arg);
  669. break;
  670. }
  671. return (ret < 0) ? 0 : ret;
  672. }
  673. /*
  674. * Sanity check to prevent one userspace from manipulating
  675. * another userspace mm.
  676. */
  677. BUG_ON(!container);
  678. if (container->mm && container->mm != current->mm)
  679. return -EPERM;
  680. switch (cmd) {
  681. case VFIO_IOMMU_SPAPR_TCE_GET_INFO: {
  682. struct vfio_iommu_spapr_tce_info info;
  683. struct tce_iommu_group *tcegrp;
  684. struct iommu_table_group *table_group;
  685. if (!tce_groups_attached(container))
  686. return -ENXIO;
  687. tcegrp = list_first_entry(&container->group_list,
  688. struct tce_iommu_group, next);
  689. table_group = iommu_group_get_iommudata(tcegrp->grp);
  690. if (!table_group)
  691. return -ENXIO;
  692. minsz = offsetofend(struct vfio_iommu_spapr_tce_info,
  693. dma32_window_size);
  694. if (copy_from_user(&info, (void __user *)arg, minsz))
  695. return -EFAULT;
  696. if (info.argsz < minsz)
  697. return -EINVAL;
  698. info.dma32_window_start = table_group->tce32_start;
  699. info.dma32_window_size = table_group->tce32_size;
  700. info.flags = 0;
  701. memset(&info.ddw, 0, sizeof(info.ddw));
  702. if (table_group->max_dynamic_windows_supported &&
  703. container->v2) {
  704. info.flags |= VFIO_IOMMU_SPAPR_INFO_DDW;
  705. info.ddw.pgsizes = table_group->pgsizes;
  706. info.ddw.max_dynamic_windows_supported =
  707. table_group->max_dynamic_windows_supported;
  708. info.ddw.levels = table_group->max_levels;
  709. }
  710. ddwsz = offsetofend(struct vfio_iommu_spapr_tce_info, ddw);
  711. if (info.argsz >= ddwsz)
  712. minsz = ddwsz;
  713. if (copy_to_user((void __user *)arg, &info, minsz))
  714. return -EFAULT;
  715. return 0;
  716. }
  717. case VFIO_IOMMU_MAP_DMA: {
  718. struct vfio_iommu_type1_dma_map param;
  719. struct iommu_table *tbl = NULL;
  720. long num;
  721. enum dma_data_direction direction;
  722. if (!container->enabled)
  723. return -EPERM;
  724. minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
  725. if (copy_from_user(&param, (void __user *)arg, minsz))
  726. return -EFAULT;
  727. if (param.argsz < minsz)
  728. return -EINVAL;
  729. if (param.flags & ~(VFIO_DMA_MAP_FLAG_READ |
  730. VFIO_DMA_MAP_FLAG_WRITE))
  731. return -EINVAL;
  732. ret = tce_iommu_create_default_window(container);
  733. if (ret)
  734. return ret;
  735. num = tce_iommu_find_table(container, param.iova, &tbl);
  736. if (num < 0)
  737. return -ENXIO;
  738. if ((param.size & ~IOMMU_PAGE_MASK(tbl)) ||
  739. (param.vaddr & ~IOMMU_PAGE_MASK(tbl)))
  740. return -EINVAL;
  741. /* iova is checked by the IOMMU API */
  742. if (param.flags & VFIO_DMA_MAP_FLAG_READ) {
  743. if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
  744. direction = DMA_BIDIRECTIONAL;
  745. else
  746. direction = DMA_TO_DEVICE;
  747. } else {
  748. if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
  749. direction = DMA_FROM_DEVICE;
  750. else
  751. return -EINVAL;
  752. }
  753. ret = iommu_tce_put_param_check(tbl, param.iova, param.vaddr);
  754. if (ret)
  755. return ret;
  756. if (container->v2)
  757. ret = tce_iommu_build_v2(container, tbl,
  758. param.iova >> tbl->it_page_shift,
  759. param.vaddr,
  760. param.size >> tbl->it_page_shift,
  761. direction);
  762. else
  763. ret = tce_iommu_build(container, tbl,
  764. param.iova >> tbl->it_page_shift,
  765. param.vaddr,
  766. param.size >> tbl->it_page_shift,
  767. direction);
  768. iommu_flush_tce(tbl);
  769. return ret;
  770. }
  771. case VFIO_IOMMU_UNMAP_DMA: {
  772. struct vfio_iommu_type1_dma_unmap param;
  773. struct iommu_table *tbl = NULL;
  774. long num;
  775. if (!container->enabled)
  776. return -EPERM;
  777. minsz = offsetofend(struct vfio_iommu_type1_dma_unmap,
  778. size);
  779. if (copy_from_user(&param, (void __user *)arg, minsz))
  780. return -EFAULT;
  781. if (param.argsz < minsz)
  782. return -EINVAL;
  783. /* No flag is supported now */
  784. if (param.flags)
  785. return -EINVAL;
  786. ret = tce_iommu_create_default_window(container);
  787. if (ret)
  788. return ret;
  789. num = tce_iommu_find_table(container, param.iova, &tbl);
  790. if (num < 0)
  791. return -ENXIO;
  792. if (param.size & ~IOMMU_PAGE_MASK(tbl))
  793. return -EINVAL;
  794. ret = iommu_tce_clear_param_check(tbl, param.iova, 0,
  795. param.size >> tbl->it_page_shift);
  796. if (ret)
  797. return ret;
  798. ret = tce_iommu_clear(container, tbl,
  799. param.iova >> tbl->it_page_shift,
  800. param.size >> tbl->it_page_shift);
  801. iommu_flush_tce(tbl);
  802. return ret;
  803. }
  804. case VFIO_IOMMU_SPAPR_REGISTER_MEMORY: {
  805. struct vfio_iommu_spapr_register_memory param;
  806. if (!container->v2)
  807. break;
  808. minsz = offsetofend(struct vfio_iommu_spapr_register_memory,
  809. size);
  810. ret = tce_iommu_mm_set(container);
  811. if (ret)
  812. return ret;
  813. if (copy_from_user(&param, (void __user *)arg, minsz))
  814. return -EFAULT;
  815. if (param.argsz < minsz)
  816. return -EINVAL;
  817. /* No flag is supported now */
  818. if (param.flags)
  819. return -EINVAL;
  820. mutex_lock(&container->lock);
  821. ret = tce_iommu_register_pages(container, param.vaddr,
  822. param.size);
  823. mutex_unlock(&container->lock);
  824. return ret;
  825. }
  826. case VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY: {
  827. struct vfio_iommu_spapr_register_memory param;
  828. if (!container->v2)
  829. break;
  830. if (!container->mm)
  831. return -EPERM;
  832. minsz = offsetofend(struct vfio_iommu_spapr_register_memory,
  833. size);
  834. if (copy_from_user(&param, (void __user *)arg, minsz))
  835. return -EFAULT;
  836. if (param.argsz < minsz)
  837. return -EINVAL;
  838. /* No flag is supported now */
  839. if (param.flags)
  840. return -EINVAL;
  841. mutex_lock(&container->lock);
  842. ret = tce_iommu_unregister_pages(container, param.vaddr,
  843. param.size);
  844. mutex_unlock(&container->lock);
  845. return ret;
  846. }
  847. case VFIO_IOMMU_ENABLE:
  848. if (container->v2)
  849. break;
  850. mutex_lock(&container->lock);
  851. ret = tce_iommu_enable(container);
  852. mutex_unlock(&container->lock);
  853. return ret;
  854. case VFIO_IOMMU_DISABLE:
  855. if (container->v2)
  856. break;
  857. mutex_lock(&container->lock);
  858. tce_iommu_disable(container);
  859. mutex_unlock(&container->lock);
  860. return 0;
  861. case VFIO_EEH_PE_OP: {
  862. struct tce_iommu_group *tcegrp;
  863. ret = 0;
  864. list_for_each_entry(tcegrp, &container->group_list, next) {
  865. ret = vfio_spapr_iommu_eeh_ioctl(tcegrp->grp,
  866. cmd, arg);
  867. if (ret)
  868. return ret;
  869. }
  870. return ret;
  871. }
  872. case VFIO_IOMMU_SPAPR_TCE_CREATE: {
  873. struct vfio_iommu_spapr_tce_create create;
  874. if (!container->v2)
  875. break;
  876. ret = tce_iommu_mm_set(container);
  877. if (ret)
  878. return ret;
  879. if (!tce_groups_attached(container))
  880. return -ENXIO;
  881. minsz = offsetofend(struct vfio_iommu_spapr_tce_create,
  882. start_addr);
  883. if (copy_from_user(&create, (void __user *)arg, minsz))
  884. return -EFAULT;
  885. if (create.argsz < minsz)
  886. return -EINVAL;
  887. if (create.flags)
  888. return -EINVAL;
  889. mutex_lock(&container->lock);
  890. ret = tce_iommu_create_default_window(container);
  891. if (ret)
  892. return ret;
  893. ret = tce_iommu_create_window(container, create.page_shift,
  894. create.window_size, create.levels,
  895. &create.start_addr);
  896. mutex_unlock(&container->lock);
  897. if (!ret && copy_to_user((void __user *)arg, &create, minsz))
  898. ret = -EFAULT;
  899. return ret;
  900. }
  901. case VFIO_IOMMU_SPAPR_TCE_REMOVE: {
  902. struct vfio_iommu_spapr_tce_remove remove;
  903. if (!container->v2)
  904. break;
  905. ret = tce_iommu_mm_set(container);
  906. if (ret)
  907. return ret;
  908. if (!tce_groups_attached(container))
  909. return -ENXIO;
  910. minsz = offsetofend(struct vfio_iommu_spapr_tce_remove,
  911. start_addr);
  912. if (copy_from_user(&remove, (void __user *)arg, minsz))
  913. return -EFAULT;
  914. if (remove.argsz < minsz)
  915. return -EINVAL;
  916. if (remove.flags)
  917. return -EINVAL;
  918. if (container->def_window_pending && !remove.start_addr) {
  919. container->def_window_pending = false;
  920. return 0;
  921. }
  922. mutex_lock(&container->lock);
  923. ret = tce_iommu_remove_window(container, remove.start_addr);
  924. mutex_unlock(&container->lock);
  925. return ret;
  926. }
  927. }
  928. return -ENOTTY;
  929. }
  930. static void tce_iommu_release_ownership(struct tce_container *container,
  931. struct iommu_table_group *table_group)
  932. {
  933. int i;
  934. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  935. struct iommu_table *tbl = container->tables[i];
  936. if (!tbl)
  937. continue;
  938. tce_iommu_clear(container, tbl, tbl->it_offset, tbl->it_size);
  939. tce_iommu_userspace_view_free(tbl, container->mm);
  940. if (tbl->it_map)
  941. iommu_release_ownership(tbl);
  942. container->tables[i] = NULL;
  943. }
  944. }
  945. static int tce_iommu_take_ownership(struct tce_container *container,
  946. struct iommu_table_group *table_group)
  947. {
  948. int i, j, rc = 0;
  949. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  950. struct iommu_table *tbl = table_group->tables[i];
  951. if (!tbl || !tbl->it_map)
  952. continue;
  953. rc = iommu_take_ownership(tbl);
  954. if (rc) {
  955. for (j = 0; j < i; ++j)
  956. iommu_release_ownership(
  957. table_group->tables[j]);
  958. return rc;
  959. }
  960. }
  961. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i)
  962. container->tables[i] = table_group->tables[i];
  963. return 0;
  964. }
  965. static void tce_iommu_release_ownership_ddw(struct tce_container *container,
  966. struct iommu_table_group *table_group)
  967. {
  968. long i;
  969. if (!table_group->ops->unset_window) {
  970. WARN_ON_ONCE(1);
  971. return;
  972. }
  973. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i)
  974. table_group->ops->unset_window(table_group, i);
  975. table_group->ops->release_ownership(table_group);
  976. }
  977. static long tce_iommu_take_ownership_ddw(struct tce_container *container,
  978. struct iommu_table_group *table_group)
  979. {
  980. if (!table_group->ops->create_table || !table_group->ops->set_window ||
  981. !table_group->ops->release_ownership) {
  982. WARN_ON_ONCE(1);
  983. return -EFAULT;
  984. }
  985. table_group->ops->take_ownership(table_group);
  986. return 0;
  987. }
  988. static int tce_iommu_attach_group(void *iommu_data,
  989. struct iommu_group *iommu_group)
  990. {
  991. int ret;
  992. struct tce_container *container = iommu_data;
  993. struct iommu_table_group *table_group;
  994. struct tce_iommu_group *tcegrp = NULL;
  995. mutex_lock(&container->lock);
  996. /* pr_debug("tce_vfio: Attaching group #%u to iommu %p\n",
  997. iommu_group_id(iommu_group), iommu_group); */
  998. table_group = iommu_group_get_iommudata(iommu_group);
  999. if (tce_groups_attached(container) && (!table_group->ops ||
  1000. !table_group->ops->take_ownership ||
  1001. !table_group->ops->release_ownership)) {
  1002. ret = -EBUSY;
  1003. goto unlock_exit;
  1004. }
  1005. /* Check if new group has the same iommu_ops (i.e. compatible) */
  1006. list_for_each_entry(tcegrp, &container->group_list, next) {
  1007. struct iommu_table_group *table_group_tmp;
  1008. if (tcegrp->grp == iommu_group) {
  1009. pr_warn("tce_vfio: Group %d is already attached\n",
  1010. iommu_group_id(iommu_group));
  1011. ret = -EBUSY;
  1012. goto unlock_exit;
  1013. }
  1014. table_group_tmp = iommu_group_get_iommudata(tcegrp->grp);
  1015. if (table_group_tmp->ops->create_table !=
  1016. table_group->ops->create_table) {
  1017. pr_warn("tce_vfio: Group %d is incompatible with group %d\n",
  1018. iommu_group_id(iommu_group),
  1019. iommu_group_id(tcegrp->grp));
  1020. ret = -EPERM;
  1021. goto unlock_exit;
  1022. }
  1023. }
  1024. tcegrp = kzalloc(sizeof(*tcegrp), GFP_KERNEL);
  1025. if (!tcegrp) {
  1026. ret = -ENOMEM;
  1027. goto unlock_exit;
  1028. }
  1029. if (!table_group->ops || !table_group->ops->take_ownership ||
  1030. !table_group->ops->release_ownership) {
  1031. ret = tce_iommu_take_ownership(container, table_group);
  1032. } else {
  1033. ret = tce_iommu_take_ownership_ddw(container, table_group);
  1034. if (!tce_groups_attached(container) && !container->tables[0])
  1035. container->def_window_pending = true;
  1036. }
  1037. if (!ret) {
  1038. tcegrp->grp = iommu_group;
  1039. list_add(&tcegrp->next, &container->group_list);
  1040. }
  1041. unlock_exit:
  1042. if (ret && tcegrp)
  1043. kfree(tcegrp);
  1044. mutex_unlock(&container->lock);
  1045. return ret;
  1046. }
  1047. static void tce_iommu_detach_group(void *iommu_data,
  1048. struct iommu_group *iommu_group)
  1049. {
  1050. struct tce_container *container = iommu_data;
  1051. struct iommu_table_group *table_group;
  1052. bool found = false;
  1053. struct tce_iommu_group *tcegrp;
  1054. mutex_lock(&container->lock);
  1055. list_for_each_entry(tcegrp, &container->group_list, next) {
  1056. if (tcegrp->grp == iommu_group) {
  1057. found = true;
  1058. break;
  1059. }
  1060. }
  1061. if (!found) {
  1062. pr_warn("tce_vfio: detaching unattached group #%u\n",
  1063. iommu_group_id(iommu_group));
  1064. goto unlock_exit;
  1065. }
  1066. list_del(&tcegrp->next);
  1067. kfree(tcegrp);
  1068. table_group = iommu_group_get_iommudata(iommu_group);
  1069. BUG_ON(!table_group);
  1070. if (!table_group->ops || !table_group->ops->release_ownership)
  1071. tce_iommu_release_ownership(container, table_group);
  1072. else
  1073. tce_iommu_release_ownership_ddw(container, table_group);
  1074. unlock_exit:
  1075. mutex_unlock(&container->lock);
  1076. }
  1077. const struct vfio_iommu_driver_ops tce_iommu_driver_ops = {
  1078. .name = "iommu-vfio-powerpc",
  1079. .owner = THIS_MODULE,
  1080. .open = tce_iommu_open,
  1081. .release = tce_iommu_release,
  1082. .ioctl = tce_iommu_ioctl,
  1083. .attach_group = tce_iommu_attach_group,
  1084. .detach_group = tce_iommu_detach_group,
  1085. };
  1086. static int __init tce_iommu_init(void)
  1087. {
  1088. return vfio_register_iommu_driver(&tce_iommu_driver_ops);
  1089. }
  1090. static void __exit tce_iommu_cleanup(void)
  1091. {
  1092. vfio_unregister_iommu_driver(&tce_iommu_driver_ops);
  1093. }
  1094. module_init(tce_iommu_init);
  1095. module_exit(tce_iommu_cleanup);
  1096. MODULE_VERSION(DRIVER_VERSION);
  1097. MODULE_LICENSE("GPL v2");
  1098. MODULE_AUTHOR(DRIVER_AUTHOR);
  1099. MODULE_DESCRIPTION(DRIVER_DESC);