vfio_iommu_spapr_tce.c 30 KB

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