vfio_iommu_spapr_tce.c 33 KB

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