io_pgtable.c 13 KB

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