vfio_iommu_spapr_tce.c 33 KB

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