vfio_iommu_spapr_tce.c 32 KB

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