vfio_iommu_spapr_tce.c 33 KB

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