io_pgtable.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CPU-agnostic AMD IO page table allocator.
  4. *
  5. * Copyright (C) 2020 Advanced Micro Devices, Inc.
  6. * Author: Suravee Suthikulpanit <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "AMD-Vi: " fmt
  9. #define dev_fmt(fmt) pr_fmt(fmt)
  10. #include <linux/atomic.h>
  11. #include <linux/bitops.h>
  12. #include <linux/io-pgtable.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sizes.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/dma-mapping.h>
  18. #include <asm/barrier.h>
  19. #include "amd_iommu_types.h"
  20. #include "amd_iommu.h"
  21. static void v1_tlb_flush_all(void *cookie)
  22. {
  23. }
  24. static void v1_tlb_flush_walk(unsigned long iova, size_t size,
  25. size_t granule, void *cookie)
  26. {
  27. }
  28. static void v1_tlb_add_page(struct iommu_iotlb_gather *gather,
  29. unsigned long iova, size_t granule,
  30. void *cookie)
  31. {
  32. }
  33. static const struct iommu_flush_ops v1_flush_ops = {
  34. .tlb_flush_all = v1_tlb_flush_all,
  35. .tlb_flush_walk = v1_tlb_flush_walk,
  36. .tlb_add_page = v1_tlb_add_page,
  37. };
  38. /*
  39. * Helper function to get the first pte of a large mapping
  40. */
  41. static u64 *first_pte_l7(u64 *pte, unsigned long *page_size,
  42. unsigned long *count)
  43. {
  44. unsigned long pte_mask, pg_size, cnt;
  45. u64 *fpte;
  46. pg_size = PTE_PAGE_SIZE(*pte);
  47. cnt = PAGE_SIZE_PTE_COUNT(pg_size);
  48. pte_mask = ~((cnt << 3) - 1);
  49. fpte = (u64 *)(((unsigned long)pte) & pte_mask);
  50. if (page_size)
  51. *page_size = pg_size;
  52. if (count)
  53. *count = cnt;
  54. return fpte;
  55. }
  56. /****************************************************************************
  57. *
  58. * The functions below are used the create the page table mappings for
  59. * unity mapped regions.
  60. *
  61. ****************************************************************************/
  62. static void free_pt_page(u64 *pt, struct list_head *freelist)
  63. {
  64. struct page *p = virt_to_page(pt);
  65. list_add_tail(&p->lru, freelist);
  66. }
  67. static void free_pt_lvl(u64 *pt, struct list_head *freelist, int lvl)
  68. {
  69. u64 *p;
  70. int i;
  71. for (i = 0; i < 512; ++i) {
  72. /* PTE present? */
  73. if (!IOMMU_PTE_PRESENT(pt[i]))
  74. continue;
  75. /* Large PTE? */
  76. if (PM_PTE_LEVEL(pt[i]) == 0 ||
  77. PM_PTE_LEVEL(pt[i]) == 7)
  78. continue;
  79. /*
  80. * Free the next level. No need to look at l1 tables here since
  81. * they can only contain leaf PTEs; just free them directly.
  82. */
  83. p = IOMMU_PTE_PAGE(pt[i]);
  84. if (lvl > 2)
  85. free_pt_lvl(p, freelist, lvl - 1);
  86. else
  87. free_pt_page(p, freelist);
  88. }
  89. free_pt_page(pt, freelist);
  90. }
  91. static void free_sub_pt(u64 *root, int mode, struct list_head *freelist)
  92. {
  93. switch (mode) {
  94. case PAGE_MODE_NONE:
  95. case PAGE_MODE_7_LEVEL:
  96. break;
  97. case PAGE_MODE_1_LEVEL:
  98. free_pt_page(root, freelist);
  99. break;
  100. case PAGE_MODE_2_LEVEL:
  101. case PAGE_MODE_3_LEVEL:
  102. case PAGE_MODE_4_LEVEL:
  103. case PAGE_MODE_5_LEVEL:
  104. case PAGE_MODE_6_LEVEL:
  105. free_pt_lvl(root, freelist, mode);
  106. break;
  107. default:
  108. BUG();
  109. }
  110. }
  111. void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
  112. u64 *root, int mode)
  113. {
  114. u64 pt_root;
  115. /* lowest 3 bits encode pgtable mode */
  116. pt_root = mode & 7;
  117. pt_root |= (u64)root;
  118. amd_iommu_domain_set_pt_root(domain, pt_root);
  119. }
  120. /*
  121. * This function is used to add another level to an IO page table. Adding
  122. * another level increases the size of the address space by 9 bits to a size up
  123. * to 64 bits.
  124. */
  125. static bool increase_address_space(struct protection_domain *domain,
  126. unsigned long address,
  127. gfp_t gfp)
  128. {
  129. unsigned long flags;
  130. bool ret = true;
  131. u64 *pte;
  132. pte = (void *)get_zeroed_page(gfp);
  133. if (!pte)
  134. return false;
  135. spin_lock_irqsave(&domain->lock, flags);
  136. if (address <= PM_LEVEL_SIZE(domain->iop.mode))
  137. goto out;
  138. ret = false;
  139. if (WARN_ON_ONCE(domain->iop.mode == PAGE_MODE_6_LEVEL))
  140. goto out;
  141. *pte = PM_LEVEL_PDE(domain->iop.mode, iommu_virt_to_phys(domain->iop.root));
  142. domain->iop.root = pte;
  143. domain->iop.mode += 1;
  144. amd_iommu_update_and_flush_device_table(domain);
  145. amd_iommu_domain_flush_complete(domain);
  146. /*
  147. * Device Table needs to be updated and flushed before the new root can
  148. * be published.
  149. */
  150. amd_iommu_domain_set_pgtable(domain, pte, domain->iop.mode);
  151. pte = NULL;
  152. ret = true;
  153. out:
  154. spin_unlock_irqrestore(&domain->lock, flags);
  155. free_page((unsigned long)pte);
  156. return ret;
  157. }
  158. static u64 *alloc_pte(struct protection_domain *domain,
  159. unsigned long address,
  160. unsigned long page_size,
  161. u64 **pte_page,
  162. gfp_t gfp,
  163. bool *updated)
  164. {
  165. int level, end_lvl;
  166. u64 *pte, *page;
  167. BUG_ON(!is_power_of_2(page_size));
  168. while (address > PM_LEVEL_SIZE(domain->iop.mode)) {
  169. /*
  170. * Return an error if there is no memory to update the
  171. * page-table.
  172. */
  173. if (!increase_address_space(domain, address, gfp))
  174. return NULL;
  175. }
  176. level = domain->iop.mode - 1;
  177. pte = &domain->iop.root[PM_LEVEL_INDEX(level, address)];
  178. address = PAGE_SIZE_ALIGN(address, page_size);
  179. end_lvl = PAGE_SIZE_LEVEL(page_size);
  180. while (level > end_lvl) {
  181. u64 __pte, __npte;
  182. int pte_level;
  183. __pte = *pte;
  184. pte_level = PM_PTE_LEVEL(__pte);
  185. /*
  186. * If we replace a series of large PTEs, we need
  187. * to tear down all of them.
  188. */
  189. if (IOMMU_PTE_PRESENT(__pte) &&
  190. pte_level == PAGE_MODE_7_LEVEL) {
  191. unsigned long count, i;
  192. u64 *lpte;
  193. lpte = first_pte_l7(pte, NULL, &count);
  194. /*
  195. * Unmap the replicated PTEs that still match the
  196. * original large mapping
  197. */
  198. for (i = 0; i < count; ++i)
  199. cmpxchg64(&lpte[i], __pte, 0ULL);
  200. *updated = true;
  201. continue;
  202. }
  203. if (!IOMMU_PTE_PRESENT(__pte) ||
  204. pte_level == PAGE_MODE_NONE) {
  205. page = (u64 *)get_zeroed_page(gfp);
  206. if (!page)
  207. return NULL;
  208. __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));
  209. /* pte could have been changed somewhere. */
  210. if (!try_cmpxchg64(pte, &__pte, __npte))
  211. free_page((unsigned long)page);
  212. else if (IOMMU_PTE_PRESENT(__pte))
  213. *updated = true;
  214. continue;
  215. }
  216. /* No level skipping support yet */
  217. if (pte_level != level)
  218. return NULL;
  219. level -= 1;
  220. pte = IOMMU_PTE_PAGE(__pte);
  221. if (pte_page && level == end_lvl)
  222. *pte_page = pte;
  223. pte = &pte[PM_LEVEL_INDEX(level, address)];
  224. }
  225. return pte;
  226. }
  227. /*
  228. * This function checks if there is a PTE for a given dma address. If
  229. * there is one, it returns the pointer to it.
  230. */
  231. static u64 *fetch_pte(struct amd_io_pgtable *pgtable,
  232. unsigned long address,
  233. unsigned long *page_size)
  234. {
  235. int level;
  236. u64 *pte;
  237. *page_size = 0;
  238. if (address > PM_LEVEL_SIZE(pgtable->mode))
  239. return NULL;
  240. level = pgtable->mode - 1;
  241. pte = &pgtable->root[PM_LEVEL_INDEX(level, address)];
  242. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  243. while (level > 0) {
  244. /* Not Present */
  245. if (!IOMMU_PTE_PRESENT(*pte))
  246. return NULL;
  247. /* Large PTE */
  248. if (PM_PTE_LEVEL(*pte) == 7 ||
  249. PM_PTE_LEVEL(*pte) == 0)
  250. break;
  251. /* No level skipping support yet */
  252. if (PM_PTE_LEVEL(*pte) != level)
  253. return NULL;
  254. level -= 1;
  255. /* Walk to the next level */
  256. pte = IOMMU_PTE_PAGE(*pte);
  257. pte = &pte[PM_LEVEL_INDEX(level, address)];
  258. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  259. }
  260. /*
  261. * If we have a series of large PTEs, make
  262. * sure to return a pointer to the first one.
  263. */
  264. if (PM_PTE_LEVEL(*pte) == PAGE_MODE_7_LEVEL)
  265. pte = first_pte_l7(pte, page_size, NULL);
  266. return pte;
  267. }
  268. static void free_clear_pte(u64 *pte, u64 pteval, struct list_head *freelist)
  269. {
  270. u64 *pt;
  271. int mode;
  272. while (!try_cmpxchg64(pte, &pteval, 0))
  273. pr_warn("AMD-Vi: IOMMU pte changed since we read it\n");
  274. if (!IOMMU_PTE_PRESENT(pteval))
  275. return;
  276. pt = IOMMU_PTE_PAGE(pteval);
  277. mode = IOMMU_PTE_MODE(pteval);
  278. free_sub_pt(pt, mode, freelist);
  279. }
  280. /*
  281. * Generic mapping functions. It maps a physical address into a DMA
  282. * address space. It allocates the page table pages if necessary.
  283. * In the future it can be extended to a generic mapping function
  284. * supporting all features of AMD IOMMU page tables like level skipping
  285. * and full 64 bit address spaces.
  286. */
  287. static int iommu_v1_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
  288. phys_addr_t paddr, size_t pgsize, size_t pgcount,
  289. int prot, gfp_t gfp, size_t *mapped)
  290. {
  291. struct protection_domain *dom = io_pgtable_ops_to_domain(ops);
  292. LIST_HEAD(freelist);
  293. bool updated = false;
  294. u64 __pte, *pte;
  295. int ret, i, count;
  296. BUG_ON(!IS_ALIGNED(iova, pgsize));
  297. BUG_ON(!IS_ALIGNED(paddr, pgsize));
  298. ret = -EINVAL;
  299. if (!(prot & IOMMU_PROT_MASK))
  300. goto out;
  301. while (pgcount > 0) {
  302. count = PAGE_SIZE_PTE_COUNT(pgsize);
  303. pte = alloc_pte(dom, iova, pgsize, NULL, gfp, &updated);
  304. ret = -ENOMEM;
  305. if (!pte)
  306. goto out;
  307. for (i = 0; i < count; ++i)
  308. free_clear_pte(&pte[i], pte[i], &freelist);
  309. if (!list_empty(&freelist))
  310. updated = true;
  311. if (count > 1) {
  312. __pte = PAGE_SIZE_PTE(__sme_set(paddr), pgsize);
  313. __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
  314. } else
  315. __pte = __sme_set(paddr) | IOMMU_PTE_PR | IOMMU_PTE_FC;
  316. if (prot & IOMMU_PROT_IR)
  317. __pte |= IOMMU_PTE_IR;
  318. if (prot & IOMMU_PROT_IW)
  319. __pte |= IOMMU_PTE_IW;
  320. for (i = 0; i < count; ++i)
  321. pte[i] = __pte;
  322. iova += pgsize;
  323. paddr += pgsize;
  324. pgcount--;
  325. if (mapped)
  326. *mapped += pgsize;
  327. }
  328. ret = 0;
  329. out:
  330. if (updated) {
  331. unsigned long flags;
  332. spin_lock_irqsave(&dom->lock, flags);
  333. /*
  334. * Flush domain TLB(s) and wait for completion. Any Device-Table
  335. * Updates and flushing already happened in
  336. * increase_address_space().
  337. */
  338. amd_iommu_domain_flush_tlb_pde(dom);
  339. amd_iommu_domain_flush_complete(dom);
  340. spin_unlock_irqrestore(&dom->lock, flags);
  341. }
  342. /* Everything flushed out, free pages now */
  343. put_pages_list(&freelist);
  344. return ret;
  345. }
  346. static unsigned long iommu_v1_unmap_pages(struct io_pgtable_ops *ops,
  347. unsigned long iova,
  348. size_t pgsize, size_t pgcount,
  349. struct iommu_iotlb_gather *gather)
  350. {
  351. struct amd_io_pgtable *pgtable = io_pgtable_ops_to_data(ops);
  352. unsigned long long unmapped;
  353. unsigned long unmap_size;
  354. u64 *pte;
  355. size_t size = pgcount << __ffs(pgsize);
  356. BUG_ON(!is_power_of_2(pgsize));
  357. unmapped = 0;
  358. while (unmapped < size) {
  359. pte = fetch_pte(pgtable, iova, &unmap_size);
  360. if (pte) {
  361. int i, count;
  362. count = PAGE_SIZE_PTE_COUNT(unmap_size);
  363. for (i = 0; i < count; i++)
  364. pte[i] = 0ULL;
  365. } else {
  366. return unmapped;
  367. }
  368. iova = (iova & ~(unmap_size - 1)) + unmap_size;
  369. unmapped += unmap_size;
  370. }
  371. return unmapped;
  372. }
  373. static phys_addr_t iommu_v1_iova_to_phys(struct io_pgtable_ops *ops, unsigned long iova)
  374. {
  375. struct amd_io_pgtable *pgtable = io_pgtable_ops_to_data(ops);
  376. unsigned long offset_mask, pte_pgsize;
  377. u64 *pte, __pte;
  378. pte = fetch_pte(pgtable, iova, &pte_pgsize);
  379. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  380. return 0;
  381. offset_mask = pte_pgsize - 1;
  382. __pte = __sme_clr(*pte & PM_ADDR_MASK);
  383. return (__pte & ~offset_mask) | (iova & offset_mask);
  384. }
  385. /*
  386. * ----------------------------------------------------
  387. */
  388. static void v1_free_pgtable(struct io_pgtable *iop)
  389. {
  390. struct amd_io_pgtable *pgtable = container_of(iop, struct amd_io_pgtable, iop);
  391. struct protection_domain *dom;
  392. LIST_HEAD(freelist);
  393. if (pgtable->mode == PAGE_MODE_NONE)
  394. return;
  395. dom = container_of(pgtable, struct protection_domain, iop);
  396. /* Page-table is not visible to IOMMU anymore, so free it */
  397. BUG_ON(pgtable->mode < PAGE_MODE_NONE ||
  398. pgtable->mode > PAGE_MODE_6_LEVEL);
  399. free_sub_pt(pgtable->root, pgtable->mode, &freelist);
  400. /* Update data structure */
  401. amd_iommu_domain_clr_pt_root(dom);
  402. /* Make changes visible to IOMMUs */
  403. amd_iommu_domain_update(dom);
  404. put_pages_list(&freelist);
  405. }
  406. static struct io_pgtable *v1_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
  407. {
  408. struct amd_io_pgtable *pgtable = io_pgtable_cfg_to_data(cfg);
  409. cfg->pgsize_bitmap = AMD_IOMMU_PGSIZES,
  410. cfg->ias = IOMMU_IN_ADDR_BIT_SIZE,
  411. cfg->oas = IOMMU_OUT_ADDR_BIT_SIZE,
  412. cfg->tlb = &v1_flush_ops;
  413. pgtable->iop.ops.map_pages = iommu_v1_map_pages;
  414. pgtable->iop.ops.unmap_pages = iommu_v1_unmap_pages;
  415. pgtable->iop.ops.iova_to_phys = iommu_v1_iova_to_phys;
  416. return &pgtable->iop;
  417. }
  418. struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns = {
  419. .alloc = v1_alloc_pgtable,
  420. .free = v1_free_pgtable,
  421. };