vfio_iommu_spapr_tce.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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. struct tce_iommu_prereg *tcemem, *tmtmp;
  337. long i;
  338. while (tce_groups_attached(container)) {
  339. tcegrp = list_first_entry(&container->group_list,
  340. struct tce_iommu_group, next);
  341. tce_iommu_detach_group(iommu_data, tcegrp->grp);
  342. }
  343. /*
  344. * If VFIO created a table, it was not disposed
  345. * by tce_iommu_detach_group() so do it now.
  346. */
  347. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  348. struct iommu_table *tbl = container->tables[i];
  349. if (!tbl)
  350. continue;
  351. tce_iommu_clear(container, tbl, tbl->it_offset, tbl->it_size);
  352. tce_iommu_free_table(container, tbl);
  353. }
  354. list_for_each_entry_safe(tcemem, tmtmp, &container->prereg_list, next)
  355. WARN_ON(tce_iommu_prereg_free(container, tcemem));
  356. tce_iommu_disable(container);
  357. if (container->mm)
  358. mmdrop(container->mm);
  359. mutex_destroy(&container->lock);
  360. kfree(container);
  361. }
  362. static void tce_iommu_unuse_page(struct tce_container *container,
  363. unsigned long hpa)
  364. {
  365. struct page *page;
  366. page = pfn_to_page(hpa >> PAGE_SHIFT);
  367. put_page(page);
  368. }
  369. static int tce_iommu_prereg_ua_to_hpa(struct tce_container *container,
  370. unsigned long tce, unsigned long shift,
  371. unsigned long *phpa, struct mm_iommu_table_group_mem_t **pmem)
  372. {
  373. long ret = 0;
  374. struct mm_iommu_table_group_mem_t *mem;
  375. mem = mm_iommu_lookup(container->mm, tce, 1ULL << shift);
  376. if (!mem)
  377. return -EINVAL;
  378. ret = mm_iommu_ua_to_hpa(mem, tce, shift, phpa);
  379. if (ret)
  380. return -EINVAL;
  381. *pmem = mem;
  382. return 0;
  383. }
  384. static void tce_iommu_unuse_page_v2(struct tce_container *container,
  385. struct iommu_table *tbl, unsigned long entry)
  386. {
  387. struct mm_iommu_table_group_mem_t *mem = NULL;
  388. int ret;
  389. unsigned long hpa = 0;
  390. unsigned long *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
  391. if (!pua)
  392. return;
  393. ret = tce_iommu_prereg_ua_to_hpa(container, *pua, tbl->it_page_shift,
  394. &hpa, &mem);
  395. if (ret)
  396. pr_debug("%s: tce %lx at #%lx was not cached, ret=%d\n",
  397. __func__, *pua, entry, ret);
  398. if (mem)
  399. mm_iommu_mapped_dec(mem);
  400. *pua = 0;
  401. }
  402. static int tce_iommu_clear(struct tce_container *container,
  403. struct iommu_table *tbl,
  404. unsigned long entry, unsigned long pages)
  405. {
  406. unsigned long oldhpa;
  407. long ret;
  408. enum dma_data_direction direction;
  409. for ( ; pages; --pages, ++entry) {
  410. direction = DMA_NONE;
  411. oldhpa = 0;
  412. ret = iommu_tce_xchg(tbl, entry, &oldhpa, &direction);
  413. if (ret)
  414. continue;
  415. if (direction == DMA_NONE)
  416. continue;
  417. if (container->v2) {
  418. tce_iommu_unuse_page_v2(container, tbl, entry);
  419. continue;
  420. }
  421. tce_iommu_unuse_page(container, oldhpa);
  422. }
  423. return 0;
  424. }
  425. static int tce_iommu_use_page(unsigned long tce, unsigned long *hpa)
  426. {
  427. struct page *page = NULL;
  428. enum dma_data_direction direction = iommu_tce_direction(tce);
  429. if (get_user_pages_fast(tce & PAGE_MASK, 1,
  430. direction != DMA_TO_DEVICE, &page) != 1)
  431. return -EFAULT;
  432. *hpa = __pa((unsigned long) page_address(page));
  433. return 0;
  434. }
  435. static long tce_iommu_build(struct tce_container *container,
  436. struct iommu_table *tbl,
  437. unsigned long entry, unsigned long tce, unsigned long pages,
  438. enum dma_data_direction direction)
  439. {
  440. long i, ret = 0;
  441. struct page *page;
  442. unsigned long hpa;
  443. enum dma_data_direction dirtmp;
  444. for (i = 0; i < pages; ++i) {
  445. unsigned long offset = tce & IOMMU_PAGE_MASK(tbl) & ~PAGE_MASK;
  446. ret = tce_iommu_use_page(tce, &hpa);
  447. if (ret)
  448. break;
  449. page = pfn_to_page(hpa >> PAGE_SHIFT);
  450. if (!tce_page_is_contained(page, tbl->it_page_shift)) {
  451. ret = -EPERM;
  452. break;
  453. }
  454. hpa |= offset;
  455. dirtmp = direction;
  456. ret = iommu_tce_xchg(tbl, entry + i, &hpa, &dirtmp);
  457. if (ret) {
  458. tce_iommu_unuse_page(container, hpa);
  459. pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
  460. __func__, entry << tbl->it_page_shift,
  461. tce, ret);
  462. break;
  463. }
  464. if (dirtmp != DMA_NONE)
  465. tce_iommu_unuse_page(container, hpa);
  466. tce += IOMMU_PAGE_SIZE(tbl);
  467. }
  468. if (ret)
  469. tce_iommu_clear(container, tbl, entry, i);
  470. return ret;
  471. }
  472. static long tce_iommu_build_v2(struct tce_container *container,
  473. struct iommu_table *tbl,
  474. unsigned long entry, unsigned long tce, unsigned long pages,
  475. enum dma_data_direction direction)
  476. {
  477. long i, ret = 0;
  478. struct page *page;
  479. unsigned long hpa;
  480. enum dma_data_direction dirtmp;
  481. if (!tbl->it_userspace) {
  482. ret = tce_iommu_userspace_view_alloc(tbl, container->mm);
  483. if (ret)
  484. return ret;
  485. }
  486. for (i = 0; i < pages; ++i) {
  487. struct mm_iommu_table_group_mem_t *mem = NULL;
  488. unsigned long *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl,
  489. entry + i);
  490. ret = tce_iommu_prereg_ua_to_hpa(container,
  491. tce, tbl->it_page_shift, &hpa, &mem);
  492. if (ret)
  493. break;
  494. page = pfn_to_page(hpa >> PAGE_SHIFT);
  495. if (!tce_page_is_contained(page, tbl->it_page_shift)) {
  496. ret = -EPERM;
  497. break;
  498. }
  499. /* Preserve offset within IOMMU page */
  500. hpa |= tce & IOMMU_PAGE_MASK(tbl) & ~PAGE_MASK;
  501. dirtmp = direction;
  502. /* The registered region is being unregistered */
  503. if (mm_iommu_mapped_inc(mem))
  504. break;
  505. ret = iommu_tce_xchg(tbl, entry + i, &hpa, &dirtmp);
  506. if (ret) {
  507. /* dirtmp cannot be DMA_NONE here */
  508. tce_iommu_unuse_page_v2(container, tbl, entry + i);
  509. pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
  510. __func__, entry << tbl->it_page_shift,
  511. tce, ret);
  512. break;
  513. }
  514. if (dirtmp != DMA_NONE)
  515. tce_iommu_unuse_page_v2(container, tbl, entry + i);
  516. *pua = tce;
  517. tce += IOMMU_PAGE_SIZE(tbl);
  518. }
  519. if (ret)
  520. tce_iommu_clear(container, tbl, entry, i);
  521. return ret;
  522. }
  523. static long tce_iommu_create_table(struct tce_container *container,
  524. struct iommu_table_group *table_group,
  525. int num,
  526. __u32 page_shift,
  527. __u64 window_size,
  528. __u32 levels,
  529. struct iommu_table **ptbl)
  530. {
  531. long ret, table_size;
  532. table_size = table_group->ops->get_table_size(page_shift, window_size,
  533. levels);
  534. if (!table_size)
  535. return -EINVAL;
  536. ret = try_increment_locked_vm(container->mm, table_size >> PAGE_SHIFT);
  537. if (ret)
  538. return ret;
  539. ret = table_group->ops->create_table(table_group, num,
  540. page_shift, window_size, levels, ptbl);
  541. WARN_ON(!ret && !(*ptbl)->it_ops->free);
  542. WARN_ON(!ret && ((*ptbl)->it_allocated_size != table_size));
  543. return ret;
  544. }
  545. static void tce_iommu_free_table(struct tce_container *container,
  546. struct iommu_table *tbl)
  547. {
  548. unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT;
  549. tce_iommu_userspace_view_free(tbl, container->mm);
  550. iommu_tce_table_put(tbl);
  551. decrement_locked_vm(container->mm, pages);
  552. }
  553. static long tce_iommu_create_window(struct tce_container *container,
  554. __u32 page_shift, __u64 window_size, __u32 levels,
  555. __u64 *start_addr)
  556. {
  557. struct tce_iommu_group *tcegrp;
  558. struct iommu_table_group *table_group;
  559. struct iommu_table *tbl = NULL;
  560. long ret, num;
  561. num = tce_iommu_find_free_table(container);
  562. if (num < 0)
  563. return num;
  564. /* Get the first group for ops::create_table */
  565. tcegrp = list_first_entry(&container->group_list,
  566. struct tce_iommu_group, next);
  567. table_group = iommu_group_get_iommudata(tcegrp->grp);
  568. if (!table_group)
  569. return -EFAULT;
  570. if (!(table_group->pgsizes & (1ULL << page_shift)))
  571. return -EINVAL;
  572. if (!table_group->ops->set_window || !table_group->ops->unset_window ||
  573. !table_group->ops->get_table_size ||
  574. !table_group->ops->create_table)
  575. return -EPERM;
  576. /* Create TCE table */
  577. ret = tce_iommu_create_table(container, table_group, num,
  578. page_shift, window_size, levels, &tbl);
  579. if (ret)
  580. return ret;
  581. BUG_ON(!tbl->it_ops->free);
  582. /*
  583. * Program the table to every group.
  584. * Groups have been tested for compatibility at the attach time.
  585. */
  586. list_for_each_entry(tcegrp, &container->group_list, next) {
  587. table_group = iommu_group_get_iommudata(tcegrp->grp);
  588. ret = table_group->ops->set_window(table_group, num, tbl);
  589. if (ret)
  590. goto unset_exit;
  591. }
  592. container->tables[num] = tbl;
  593. /* Return start address assigned by platform in create_table() */
  594. *start_addr = tbl->it_offset << tbl->it_page_shift;
  595. return 0;
  596. unset_exit:
  597. list_for_each_entry(tcegrp, &container->group_list, next) {
  598. table_group = iommu_group_get_iommudata(tcegrp->grp);
  599. table_group->ops->unset_window(table_group, num);
  600. }
  601. tce_iommu_free_table(container, tbl);
  602. return ret;
  603. }
  604. static long tce_iommu_remove_window(struct tce_container *container,
  605. __u64 start_addr)
  606. {
  607. struct iommu_table_group *table_group = NULL;
  608. struct iommu_table *tbl;
  609. struct tce_iommu_group *tcegrp;
  610. int num;
  611. num = tce_iommu_find_table(container, start_addr, &tbl);
  612. if (num < 0)
  613. return -EINVAL;
  614. BUG_ON(!tbl->it_size);
  615. /* Detach groups from IOMMUs */
  616. list_for_each_entry(tcegrp, &container->group_list, next) {
  617. table_group = iommu_group_get_iommudata(tcegrp->grp);
  618. /*
  619. * SPAPR TCE IOMMU exposes the default DMA window to
  620. * the guest via dma32_window_start/size of
  621. * VFIO_IOMMU_SPAPR_TCE_GET_INFO. Some platforms allow
  622. * the userspace to remove this window, some do not so
  623. * here we check for the platform capability.
  624. */
  625. if (!table_group->ops || !table_group->ops->unset_window)
  626. return -EPERM;
  627. table_group->ops->unset_window(table_group, num);
  628. }
  629. /* Free table */
  630. tce_iommu_clear(container, tbl, tbl->it_offset, tbl->it_size);
  631. tce_iommu_free_table(container, tbl);
  632. container->tables[num] = NULL;
  633. return 0;
  634. }
  635. static long tce_iommu_create_default_window(struct tce_container *container)
  636. {
  637. long ret;
  638. __u64 start_addr = 0;
  639. struct tce_iommu_group *tcegrp;
  640. struct iommu_table_group *table_group;
  641. if (!container->def_window_pending)
  642. return 0;
  643. if (!tce_groups_attached(container))
  644. return -ENODEV;
  645. tcegrp = list_first_entry(&container->group_list,
  646. struct tce_iommu_group, next);
  647. table_group = iommu_group_get_iommudata(tcegrp->grp);
  648. if (!table_group)
  649. return -ENODEV;
  650. ret = tce_iommu_create_window(container, IOMMU_PAGE_SHIFT_4K,
  651. table_group->tce32_size, 1, &start_addr);
  652. WARN_ON_ONCE(!ret && start_addr);
  653. if (!ret)
  654. container->def_window_pending = false;
  655. return ret;
  656. }
  657. static long tce_iommu_ioctl(void *iommu_data,
  658. unsigned int cmd, unsigned long arg)
  659. {
  660. struct tce_container *container = iommu_data;
  661. unsigned long minsz, ddwsz;
  662. long ret;
  663. switch (cmd) {
  664. case VFIO_CHECK_EXTENSION:
  665. switch (arg) {
  666. case VFIO_SPAPR_TCE_IOMMU:
  667. case VFIO_SPAPR_TCE_v2_IOMMU:
  668. ret = 1;
  669. break;
  670. default:
  671. ret = vfio_spapr_iommu_eeh_ioctl(NULL, cmd, arg);
  672. break;
  673. }
  674. return (ret < 0) ? 0 : ret;
  675. }
  676. /*
  677. * Sanity check to prevent one userspace from manipulating
  678. * another userspace mm.
  679. */
  680. BUG_ON(!container);
  681. if (container->mm && container->mm != current->mm)
  682. return -EPERM;
  683. switch (cmd) {
  684. case VFIO_IOMMU_SPAPR_TCE_GET_INFO: {
  685. struct vfio_iommu_spapr_tce_info info;
  686. struct tce_iommu_group *tcegrp;
  687. struct iommu_table_group *table_group;
  688. if (!tce_groups_attached(container))
  689. return -ENXIO;
  690. tcegrp = list_first_entry(&container->group_list,
  691. struct tce_iommu_group, next);
  692. table_group = iommu_group_get_iommudata(tcegrp->grp);
  693. if (!table_group)
  694. return -ENXIO;
  695. minsz = offsetofend(struct vfio_iommu_spapr_tce_info,
  696. dma32_window_size);
  697. if (copy_from_user(&info, (void __user *)arg, minsz))
  698. return -EFAULT;
  699. if (info.argsz < minsz)
  700. return -EINVAL;
  701. info.dma32_window_start = table_group->tce32_start;
  702. info.dma32_window_size = table_group->tce32_size;
  703. info.flags = 0;
  704. memset(&info.ddw, 0, sizeof(info.ddw));
  705. if (table_group->max_dynamic_windows_supported &&
  706. container->v2) {
  707. info.flags |= VFIO_IOMMU_SPAPR_INFO_DDW;
  708. info.ddw.pgsizes = table_group->pgsizes;
  709. info.ddw.max_dynamic_windows_supported =
  710. table_group->max_dynamic_windows_supported;
  711. info.ddw.levels = table_group->max_levels;
  712. }
  713. ddwsz = offsetofend(struct vfio_iommu_spapr_tce_info, ddw);
  714. if (info.argsz >= ddwsz)
  715. minsz = ddwsz;
  716. if (copy_to_user((void __user *)arg, &info, minsz))
  717. return -EFAULT;
  718. return 0;
  719. }
  720. case VFIO_IOMMU_MAP_DMA: {
  721. struct vfio_iommu_type1_dma_map param;
  722. struct iommu_table *tbl = NULL;
  723. long num;
  724. enum dma_data_direction direction;
  725. if (!container->enabled)
  726. return -EPERM;
  727. minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
  728. if (copy_from_user(&param, (void __user *)arg, minsz))
  729. return -EFAULT;
  730. if (param.argsz < minsz)
  731. return -EINVAL;
  732. if (param.flags & ~(VFIO_DMA_MAP_FLAG_READ |
  733. VFIO_DMA_MAP_FLAG_WRITE))
  734. return -EINVAL;
  735. ret = tce_iommu_create_default_window(container);
  736. if (ret)
  737. return ret;
  738. num = tce_iommu_find_table(container, param.iova, &tbl);
  739. if (num < 0)
  740. return -ENXIO;
  741. if ((param.size & ~IOMMU_PAGE_MASK(tbl)) ||
  742. (param.vaddr & ~IOMMU_PAGE_MASK(tbl)))
  743. return -EINVAL;
  744. /* iova is checked by the IOMMU API */
  745. if (param.flags & VFIO_DMA_MAP_FLAG_READ) {
  746. if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
  747. direction = DMA_BIDIRECTIONAL;
  748. else
  749. direction = DMA_TO_DEVICE;
  750. } else {
  751. if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
  752. direction = DMA_FROM_DEVICE;
  753. else
  754. return -EINVAL;
  755. }
  756. ret = iommu_tce_put_param_check(tbl, param.iova, param.vaddr);
  757. if (ret)
  758. return ret;
  759. if (container->v2)
  760. ret = tce_iommu_build_v2(container, tbl,
  761. param.iova >> tbl->it_page_shift,
  762. param.vaddr,
  763. param.size >> tbl->it_page_shift,
  764. direction);
  765. else
  766. ret = tce_iommu_build(container, tbl,
  767. param.iova >> tbl->it_page_shift,
  768. param.vaddr,
  769. param.size >> tbl->it_page_shift,
  770. direction);
  771. iommu_flush_tce(tbl);
  772. return ret;
  773. }
  774. case VFIO_IOMMU_UNMAP_DMA: {
  775. struct vfio_iommu_type1_dma_unmap param;
  776. struct iommu_table *tbl = NULL;
  777. long num;
  778. if (!container->enabled)
  779. return -EPERM;
  780. minsz = offsetofend(struct vfio_iommu_type1_dma_unmap,
  781. size);
  782. if (copy_from_user(&param, (void __user *)arg, minsz))
  783. return -EFAULT;
  784. if (param.argsz < minsz)
  785. return -EINVAL;
  786. /* No flag is supported now */
  787. if (param.flags)
  788. return -EINVAL;
  789. ret = tce_iommu_create_default_window(container);
  790. if (ret)
  791. return ret;
  792. num = tce_iommu_find_table(container, param.iova, &tbl);
  793. if (num < 0)
  794. return -ENXIO;
  795. if (param.size & ~IOMMU_PAGE_MASK(tbl))
  796. return -EINVAL;
  797. ret = iommu_tce_clear_param_check(tbl, param.iova, 0,
  798. param.size >> tbl->it_page_shift);
  799. if (ret)
  800. return ret;
  801. ret = tce_iommu_clear(container, tbl,
  802. param.iova >> tbl->it_page_shift,
  803. param.size >> tbl->it_page_shift);
  804. iommu_flush_tce(tbl);
  805. return ret;
  806. }
  807. case VFIO_IOMMU_SPAPR_REGISTER_MEMORY: {
  808. struct vfio_iommu_spapr_register_memory param;
  809. if (!container->v2)
  810. break;
  811. minsz = offsetofend(struct vfio_iommu_spapr_register_memory,
  812. size);
  813. ret = tce_iommu_mm_set(container);
  814. if (ret)
  815. return ret;
  816. if (copy_from_user(&param, (void __user *)arg, minsz))
  817. return -EFAULT;
  818. if (param.argsz < minsz)
  819. return -EINVAL;
  820. /* No flag is supported now */
  821. if (param.flags)
  822. return -EINVAL;
  823. mutex_lock(&container->lock);
  824. ret = tce_iommu_register_pages(container, param.vaddr,
  825. param.size);
  826. mutex_unlock(&container->lock);
  827. return ret;
  828. }
  829. case VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY: {
  830. struct vfio_iommu_spapr_register_memory param;
  831. if (!container->v2)
  832. break;
  833. if (!container->mm)
  834. return -EPERM;
  835. minsz = offsetofend(struct vfio_iommu_spapr_register_memory,
  836. size);
  837. if (copy_from_user(&param, (void __user *)arg, minsz))
  838. return -EFAULT;
  839. if (param.argsz < minsz)
  840. return -EINVAL;
  841. /* No flag is supported now */
  842. if (param.flags)
  843. return -EINVAL;
  844. mutex_lock(&container->lock);
  845. ret = tce_iommu_unregister_pages(container, param.vaddr,
  846. param.size);
  847. mutex_unlock(&container->lock);
  848. return ret;
  849. }
  850. case VFIO_IOMMU_ENABLE:
  851. if (container->v2)
  852. break;
  853. mutex_lock(&container->lock);
  854. ret = tce_iommu_enable(container);
  855. mutex_unlock(&container->lock);
  856. return ret;
  857. case VFIO_IOMMU_DISABLE:
  858. if (container->v2)
  859. break;
  860. mutex_lock(&container->lock);
  861. tce_iommu_disable(container);
  862. mutex_unlock(&container->lock);
  863. return 0;
  864. case VFIO_EEH_PE_OP: {
  865. struct tce_iommu_group *tcegrp;
  866. ret = 0;
  867. list_for_each_entry(tcegrp, &container->group_list, next) {
  868. ret = vfio_spapr_iommu_eeh_ioctl(tcegrp->grp,
  869. cmd, arg);
  870. if (ret)
  871. return ret;
  872. }
  873. return ret;
  874. }
  875. case VFIO_IOMMU_SPAPR_TCE_CREATE: {
  876. struct vfio_iommu_spapr_tce_create create;
  877. if (!container->v2)
  878. break;
  879. ret = tce_iommu_mm_set(container);
  880. if (ret)
  881. return ret;
  882. if (!tce_groups_attached(container))
  883. return -ENXIO;
  884. minsz = offsetofend(struct vfio_iommu_spapr_tce_create,
  885. start_addr);
  886. if (copy_from_user(&create, (void __user *)arg, minsz))
  887. return -EFAULT;
  888. if (create.argsz < minsz)
  889. return -EINVAL;
  890. if (create.flags)
  891. return -EINVAL;
  892. mutex_lock(&container->lock);
  893. ret = tce_iommu_create_default_window(container);
  894. if (!ret)
  895. ret = tce_iommu_create_window(container,
  896. create.page_shift,
  897. create.window_size, create.levels,
  898. &create.start_addr);
  899. mutex_unlock(&container->lock);
  900. if (!ret && copy_to_user((void __user *)arg, &create, minsz))
  901. ret = -EFAULT;
  902. return ret;
  903. }
  904. case VFIO_IOMMU_SPAPR_TCE_REMOVE: {
  905. struct vfio_iommu_spapr_tce_remove remove;
  906. if (!container->v2)
  907. break;
  908. ret = tce_iommu_mm_set(container);
  909. if (ret)
  910. return ret;
  911. if (!tce_groups_attached(container))
  912. return -ENXIO;
  913. minsz = offsetofend(struct vfio_iommu_spapr_tce_remove,
  914. start_addr);
  915. if (copy_from_user(&remove, (void __user *)arg, minsz))
  916. return -EFAULT;
  917. if (remove.argsz < minsz)
  918. return -EINVAL;
  919. if (remove.flags)
  920. return -EINVAL;
  921. if (container->def_window_pending && !remove.start_addr) {
  922. container->def_window_pending = false;
  923. return 0;
  924. }
  925. mutex_lock(&container->lock);
  926. ret = tce_iommu_remove_window(container, remove.start_addr);
  927. mutex_unlock(&container->lock);
  928. return ret;
  929. }
  930. }
  931. return -ENOTTY;
  932. }
  933. static void tce_iommu_release_ownership(struct tce_container *container,
  934. struct iommu_table_group *table_group)
  935. {
  936. int i;
  937. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  938. struct iommu_table *tbl = container->tables[i];
  939. if (!tbl)
  940. continue;
  941. tce_iommu_clear(container, tbl, tbl->it_offset, tbl->it_size);
  942. tce_iommu_userspace_view_free(tbl, container->mm);
  943. if (tbl->it_map)
  944. iommu_release_ownership(tbl);
  945. container->tables[i] = NULL;
  946. }
  947. }
  948. static int tce_iommu_take_ownership(struct tce_container *container,
  949. struct iommu_table_group *table_group)
  950. {
  951. int i, j, rc = 0;
  952. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  953. struct iommu_table *tbl = table_group->tables[i];
  954. if (!tbl || !tbl->it_map)
  955. continue;
  956. rc = iommu_take_ownership(tbl);
  957. if (rc) {
  958. for (j = 0; j < i; ++j)
  959. iommu_release_ownership(
  960. table_group->tables[j]);
  961. return rc;
  962. }
  963. }
  964. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i)
  965. container->tables[i] = table_group->tables[i];
  966. return 0;
  967. }
  968. static void tce_iommu_release_ownership_ddw(struct tce_container *container,
  969. struct iommu_table_group *table_group)
  970. {
  971. long i;
  972. if (!table_group->ops->unset_window) {
  973. WARN_ON_ONCE(1);
  974. return;
  975. }
  976. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i)
  977. table_group->ops->unset_window(table_group, i);
  978. table_group->ops->release_ownership(table_group);
  979. }
  980. static long tce_iommu_take_ownership_ddw(struct tce_container *container,
  981. struct iommu_table_group *table_group)
  982. {
  983. long i, ret = 0;
  984. if (!table_group->ops->create_table || !table_group->ops->set_window ||
  985. !table_group->ops->release_ownership) {
  986. WARN_ON_ONCE(1);
  987. return -EFAULT;
  988. }
  989. table_group->ops->take_ownership(table_group);
  990. /* Set all windows to the new group */
  991. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  992. struct iommu_table *tbl = container->tables[i];
  993. if (!tbl)
  994. continue;
  995. ret = table_group->ops->set_window(table_group, i, tbl);
  996. if (ret)
  997. goto release_exit;
  998. }
  999. return 0;
  1000. release_exit:
  1001. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i)
  1002. table_group->ops->unset_window(table_group, i);
  1003. table_group->ops->release_ownership(table_group);
  1004. return ret;
  1005. }
  1006. static int tce_iommu_attach_group(void *iommu_data,
  1007. struct iommu_group *iommu_group)
  1008. {
  1009. int ret;
  1010. struct tce_container *container = iommu_data;
  1011. struct iommu_table_group *table_group;
  1012. struct tce_iommu_group *tcegrp = NULL;
  1013. mutex_lock(&container->lock);
  1014. /* pr_debug("tce_vfio: Attaching group #%u to iommu %p\n",
  1015. iommu_group_id(iommu_group), iommu_group); */
  1016. table_group = iommu_group_get_iommudata(iommu_group);
  1017. if (!table_group) {
  1018. ret = -ENODEV;
  1019. goto unlock_exit;
  1020. }
  1021. if (tce_groups_attached(container) && (!table_group->ops ||
  1022. !table_group->ops->take_ownership ||
  1023. !table_group->ops->release_ownership)) {
  1024. ret = -EBUSY;
  1025. goto unlock_exit;
  1026. }
  1027. /* Check if new group has the same iommu_ops (i.e. compatible) */
  1028. list_for_each_entry(tcegrp, &container->group_list, next) {
  1029. struct iommu_table_group *table_group_tmp;
  1030. if (tcegrp->grp == iommu_group) {
  1031. pr_warn("tce_vfio: Group %d is already attached\n",
  1032. iommu_group_id(iommu_group));
  1033. ret = -EBUSY;
  1034. goto unlock_exit;
  1035. }
  1036. table_group_tmp = iommu_group_get_iommudata(tcegrp->grp);
  1037. if (table_group_tmp->ops->create_table !=
  1038. table_group->ops->create_table) {
  1039. pr_warn("tce_vfio: Group %d is incompatible with group %d\n",
  1040. iommu_group_id(iommu_group),
  1041. iommu_group_id(tcegrp->grp));
  1042. ret = -EPERM;
  1043. goto unlock_exit;
  1044. }
  1045. }
  1046. tcegrp = kzalloc(sizeof(*tcegrp), GFP_KERNEL);
  1047. if (!tcegrp) {
  1048. ret = -ENOMEM;
  1049. goto unlock_exit;
  1050. }
  1051. if (!table_group->ops || !table_group->ops->take_ownership ||
  1052. !table_group->ops->release_ownership) {
  1053. if (container->v2) {
  1054. ret = -EPERM;
  1055. goto unlock_exit;
  1056. }
  1057. ret = tce_iommu_take_ownership(container, table_group);
  1058. } else {
  1059. if (!container->v2) {
  1060. ret = -EPERM;
  1061. goto unlock_exit;
  1062. }
  1063. ret = tce_iommu_take_ownership_ddw(container, table_group);
  1064. if (!tce_groups_attached(container) && !container->tables[0])
  1065. container->def_window_pending = true;
  1066. }
  1067. if (!ret) {
  1068. tcegrp->grp = iommu_group;
  1069. list_add(&tcegrp->next, &container->group_list);
  1070. }
  1071. unlock_exit:
  1072. if (ret && tcegrp)
  1073. kfree(tcegrp);
  1074. mutex_unlock(&container->lock);
  1075. return ret;
  1076. }
  1077. static void tce_iommu_detach_group(void *iommu_data,
  1078. struct iommu_group *iommu_group)
  1079. {
  1080. struct tce_container *container = iommu_data;
  1081. struct iommu_table_group *table_group;
  1082. bool found = false;
  1083. struct tce_iommu_group *tcegrp;
  1084. mutex_lock(&container->lock);
  1085. list_for_each_entry(tcegrp, &container->group_list, next) {
  1086. if (tcegrp->grp == iommu_group) {
  1087. found = true;
  1088. break;
  1089. }
  1090. }
  1091. if (!found) {
  1092. pr_warn("tce_vfio: detaching unattached group #%u\n",
  1093. iommu_group_id(iommu_group));
  1094. goto unlock_exit;
  1095. }
  1096. list_del(&tcegrp->next);
  1097. kfree(tcegrp);
  1098. table_group = iommu_group_get_iommudata(iommu_group);
  1099. BUG_ON(!table_group);
  1100. if (!table_group->ops || !table_group->ops->release_ownership)
  1101. tce_iommu_release_ownership(container, table_group);
  1102. else
  1103. tce_iommu_release_ownership_ddw(container, table_group);
  1104. unlock_exit:
  1105. mutex_unlock(&container->lock);
  1106. }
  1107. const struct vfio_iommu_driver_ops tce_iommu_driver_ops = {
  1108. .name = "iommu-vfio-powerpc",
  1109. .owner = THIS_MODULE,
  1110. .open = tce_iommu_open,
  1111. .release = tce_iommu_release,
  1112. .ioctl = tce_iommu_ioctl,
  1113. .attach_group = tce_iommu_attach_group,
  1114. .detach_group = tce_iommu_detach_group,
  1115. };
  1116. static int __init tce_iommu_init(void)
  1117. {
  1118. return vfio_register_iommu_driver(&tce_iommu_driver_ops);
  1119. }
  1120. static void __exit tce_iommu_cleanup(void)
  1121. {
  1122. vfio_unregister_iommu_driver(&tce_iommu_driver_ops);
  1123. }
  1124. module_init(tce_iommu_init);
  1125. module_exit(tce_iommu_cleanup);
  1126. MODULE_VERSION(DRIVER_VERSION);
  1127. MODULE_LICENSE("GPL v2");
  1128. MODULE_AUTHOR(DRIVER_AUTHOR);
  1129. MODULE_DESCRIPTION(DRIVER_DESC);