vfio_iommu_spapr_tce.c 32 KB

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