1 // SPDX-License-Identifier: GPL-2.0-only
3 * CPU-agnostic AMD IO page table allocator.
5 * Copyright (C) 2020 Advanced Micro Devices, Inc.
9 #define pr_fmt(fmt) "AMD-Vi: " fmt
10 #define dev_fmt(fmt) pr_fmt(fmt)
12 #include <linux/atomic.h>
13 #include <linux/bitops.h>
14 #include <linux/io-pgtable.h>
15 #include <linux/kernel.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/dma-mapping.h>
21 #include <asm/barrier.h>
23 #include "amd_iommu_types.h"
24 #include "amd_iommu.h"
26 static void v1_tlb_flush_all(void *cookie)
30 static void v1_tlb_flush_walk(unsigned long iova, size_t size,
31 size_t granule, void *cookie)
35 static void v1_tlb_add_page(struct iommu_iotlb_gather *gather,
36 unsigned long iova, size_t granule,
41 static const struct iommu_flush_ops v1_flush_ops = {
42 .tlb_flush_all = v1_tlb_flush_all,
43 .tlb_flush_walk = v1_tlb_flush_walk,
44 .tlb_add_page = v1_tlb_add_page,
48 * Helper function to get the first pte of a large mapping
50 static u64 *first_pte_l7(u64 *pte, unsigned long *page_size,
53 unsigned long pte_mask, pg_size, cnt;
56 pg_size = PTE_PAGE_SIZE(*pte);
57 cnt = PAGE_SIZE_PTE_COUNT(pg_size);
58 pte_mask = ~((cnt << 3) - 1);
59 fpte = (u64 *)(((unsigned long)pte) & pte_mask);
70 /****************************************************************************
72 * The functions below are used the create the page table mappings for
73 * unity mapped regions.
75 ****************************************************************************/
77 static void free_page_list(struct page *freelist)
79 while (freelist != NULL) {
80 unsigned long p = (unsigned long)page_address(freelist);
82 freelist = freelist->freelist;
87 static struct page *free_pt_page(unsigned long pt, struct page *freelist)
89 struct page *p = virt_to_page((void *)pt);
91 p->freelist = freelist;
96 #define DEFINE_FREE_PT_FN(LVL, FN) \
97 static struct page *free_pt_##LVL (unsigned long __pt, struct page *freelist) \
105 for (i = 0; i < 512; ++i) { \
107 if (!IOMMU_PTE_PRESENT(pt[i])) \
111 if (PM_PTE_LEVEL(pt[i]) == 0 || \
112 PM_PTE_LEVEL(pt[i]) == 7) \
115 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
116 freelist = FN(p, freelist); \
119 return free_pt_page((unsigned long)pt, freelist); \
122 DEFINE_FREE_PT_FN(l2, free_pt_page)
123 DEFINE_FREE_PT_FN(l3, free_pt_l2)
124 DEFINE_FREE_PT_FN(l4, free_pt_l3)
125 DEFINE_FREE_PT_FN(l5, free_pt_l4)
126 DEFINE_FREE_PT_FN(l6, free_pt_l5)
128 static struct page *free_sub_pt(unsigned long root, int mode,
129 struct page *freelist)
133 case PAGE_MODE_7_LEVEL:
135 case PAGE_MODE_1_LEVEL:
136 freelist = free_pt_page(root, freelist);
138 case PAGE_MODE_2_LEVEL:
139 freelist = free_pt_l2(root, freelist);
141 case PAGE_MODE_3_LEVEL:
142 freelist = free_pt_l3(root, freelist);
144 case PAGE_MODE_4_LEVEL:
145 freelist = free_pt_l4(root, freelist);
147 case PAGE_MODE_5_LEVEL:
148 freelist = free_pt_l5(root, freelist);
150 case PAGE_MODE_6_LEVEL:
151 freelist = free_pt_l6(root, freelist);
160 void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
165 /* lowest 3 bits encode pgtable mode */
167 pt_root |= (u64)root;
169 amd_iommu_domain_set_pt_root(domain, pt_root);
173 * This function is used to add another level to an IO page table. Adding
174 * another level increases the size of the address space by 9 bits to a size up
177 static bool increase_address_space(struct protection_domain *domain,
178 unsigned long address,
185 pte = (void *)get_zeroed_page(gfp);
189 spin_lock_irqsave(&domain->lock, flags);
191 if (address <= PM_LEVEL_SIZE(domain->iop.mode))
195 if (WARN_ON_ONCE(domain->iop.mode == PAGE_MODE_6_LEVEL))
198 *pte = PM_LEVEL_PDE(domain->iop.mode, iommu_virt_to_phys(domain->iop.root));
200 domain->iop.root = pte;
201 domain->iop.mode += 1;
202 amd_iommu_update_and_flush_device_table(domain);
203 amd_iommu_domain_flush_complete(domain);
206 * Device Table needs to be updated and flushed before the new root can
209 amd_iommu_domain_set_pgtable(domain, pte, domain->iop.mode);
215 spin_unlock_irqrestore(&domain->lock, flags);
216 free_page((unsigned long)pte);
221 static u64 *alloc_pte(struct protection_domain *domain,
222 unsigned long address,
223 unsigned long page_size,
231 BUG_ON(!is_power_of_2(page_size));
233 while (address > PM_LEVEL_SIZE(domain->iop.mode)) {
235 * Return an error if there is no memory to update the
238 if (!increase_address_space(domain, address, gfp))
243 level = domain->iop.mode - 1;
244 pte = &domain->iop.root[PM_LEVEL_INDEX(level, address)];
245 address = PAGE_SIZE_ALIGN(address, page_size);
246 end_lvl = PAGE_SIZE_LEVEL(page_size);
248 while (level > end_lvl) {
253 pte_level = PM_PTE_LEVEL(__pte);
256 * If we replace a series of large PTEs, we need
257 * to tear down all of them.
259 if (IOMMU_PTE_PRESENT(__pte) &&
260 pte_level == PAGE_MODE_7_LEVEL) {
261 unsigned long count, i;
264 lpte = first_pte_l7(pte, NULL, &count);
267 * Unmap the replicated PTEs that still match the
268 * original large mapping
270 for (i = 0; i < count; ++i)
271 cmpxchg64(&lpte[i], __pte, 0ULL);
277 if (!IOMMU_PTE_PRESENT(__pte) ||
278 pte_level == PAGE_MODE_NONE) {
279 page = (u64 *)get_zeroed_page(gfp);
284 __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));
286 /* pte could have been changed somewhere. */
287 if (cmpxchg64(pte, __pte, __npte) != __pte)
288 free_page((unsigned long)page);
289 else if (IOMMU_PTE_PRESENT(__pte))
295 /* No level skipping support yet */
296 if (pte_level != level)
301 pte = IOMMU_PTE_PAGE(__pte);
303 if (pte_page && level == end_lvl)
306 pte = &pte[PM_LEVEL_INDEX(level, address)];
313 * This function checks if there is a PTE for a given dma address. If
314 * there is one, it returns the pointer to it.
316 static u64 *fetch_pte(struct amd_io_pgtable *pgtable,
317 unsigned long address,
318 unsigned long *page_size)
325 if (address > PM_LEVEL_SIZE(pgtable->mode))
328 level = pgtable->mode - 1;
329 pte = &pgtable->root[PM_LEVEL_INDEX(level, address)];
330 *page_size = PTE_LEVEL_PAGE_SIZE(level);
335 if (!IOMMU_PTE_PRESENT(*pte))
339 if (PM_PTE_LEVEL(*pte) == 7 ||
340 PM_PTE_LEVEL(*pte) == 0)
343 /* No level skipping support yet */
344 if (PM_PTE_LEVEL(*pte) != level)
349 /* Walk to the next level */
350 pte = IOMMU_PTE_PAGE(*pte);
351 pte = &pte[PM_LEVEL_INDEX(level, address)];
352 *page_size = PTE_LEVEL_PAGE_SIZE(level);
356 * If we have a series of large PTEs, make
357 * sure to return a pointer to the first one.
359 if (PM_PTE_LEVEL(*pte) == PAGE_MODE_7_LEVEL)
360 pte = first_pte_l7(pte, page_size, NULL);
365 static struct page *free_clear_pte(u64 *pte, u64 pteval, struct page *freelist)
370 while (cmpxchg64(pte, pteval, 0) != pteval) {
371 pr_warn("AMD-Vi: IOMMU pte changed since we read it\n");
375 if (!IOMMU_PTE_PRESENT(pteval))
378 pt = (unsigned long)IOMMU_PTE_PAGE(pteval);
379 mode = IOMMU_PTE_MODE(pteval);
381 return free_sub_pt(pt, mode, freelist);
385 * Generic mapping functions. It maps a physical address into a DMA
386 * address space. It allocates the page table pages if necessary.
387 * In the future it can be extended to a generic mapping function
388 * supporting all features of AMD IOMMU page tables like level skipping
389 * and full 64 bit address spaces.
391 static int iommu_v1_map_page(struct io_pgtable_ops *ops, unsigned long iova,
392 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
394 struct protection_domain *dom = io_pgtable_ops_to_domain(ops);
395 struct page *freelist = NULL;
396 bool updated = false;
400 BUG_ON(!IS_ALIGNED(iova, size));
401 BUG_ON(!IS_ALIGNED(paddr, size));
404 if (!(prot & IOMMU_PROT_MASK))
407 count = PAGE_SIZE_PTE_COUNT(size);
408 pte = alloc_pte(dom, iova, size, NULL, gfp, &updated);
414 for (i = 0; i < count; ++i)
415 freelist = free_clear_pte(&pte[i], pte[i], freelist);
417 if (freelist != NULL)
421 __pte = PAGE_SIZE_PTE(__sme_set(paddr), size);
422 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
424 __pte = __sme_set(paddr) | IOMMU_PTE_PR | IOMMU_PTE_FC;
426 if (prot & IOMMU_PROT_IR)
427 __pte |= IOMMU_PTE_IR;
428 if (prot & IOMMU_PROT_IW)
429 __pte |= IOMMU_PTE_IW;
431 for (i = 0; i < count; ++i)
440 spin_lock_irqsave(&dom->lock, flags);
442 * Flush domain TLB(s) and wait for completion. Any Device-Table
443 * Updates and flushing already happened in
444 * increase_address_space().
446 amd_iommu_domain_flush_tlb_pde(dom);
447 amd_iommu_domain_flush_complete(dom);
448 spin_unlock_irqrestore(&dom->lock, flags);
451 /* Everything flushed out, free pages now */
452 free_page_list(freelist);
457 static unsigned long iommu_v1_unmap_page(struct io_pgtable_ops *ops,
460 struct iommu_iotlb_gather *gather)
462 struct amd_io_pgtable *pgtable = io_pgtable_ops_to_data(ops);
463 unsigned long long unmapped;
464 unsigned long unmap_size;
467 BUG_ON(!is_power_of_2(size));
471 while (unmapped < size) {
472 pte = fetch_pte(pgtable, iova, &unmap_size);
476 count = PAGE_SIZE_PTE_COUNT(unmap_size);
477 for (i = 0; i < count; i++)
481 iova = (iova & ~(unmap_size - 1)) + unmap_size;
482 unmapped += unmap_size;
485 BUG_ON(unmapped && !is_power_of_2(unmapped));
490 static phys_addr_t iommu_v1_iova_to_phys(struct io_pgtable_ops *ops, unsigned long iova)
492 struct amd_io_pgtable *pgtable = io_pgtable_ops_to_data(ops);
493 unsigned long offset_mask, pte_pgsize;
496 pte = fetch_pte(pgtable, iova, &pte_pgsize);
498 if (!pte || !IOMMU_PTE_PRESENT(*pte))
501 offset_mask = pte_pgsize - 1;
502 __pte = __sme_clr(*pte & PM_ADDR_MASK);
504 return (__pte & ~offset_mask) | (iova & offset_mask);
508 * ----------------------------------------------------
510 static void v1_free_pgtable(struct io_pgtable *iop)
512 struct amd_io_pgtable *pgtable = container_of(iop, struct amd_io_pgtable, iop);
513 struct protection_domain *dom;
514 struct page *freelist = NULL;
517 if (pgtable->mode == PAGE_MODE_NONE)
520 dom = container_of(pgtable, struct protection_domain, iop);
522 /* Update data structure */
523 amd_iommu_domain_clr_pt_root(dom);
525 /* Make changes visible to IOMMUs */
526 amd_iommu_domain_update(dom);
528 /* Page-table is not visible to IOMMU anymore, so free it */
529 BUG_ON(pgtable->mode < PAGE_MODE_NONE ||
530 pgtable->mode > PAGE_MODE_6_LEVEL);
532 root = (unsigned long)pgtable->root;
533 freelist = free_sub_pt(root, pgtable->mode, freelist);
535 free_page_list(freelist);
538 static struct io_pgtable *v1_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
540 struct amd_io_pgtable *pgtable = io_pgtable_cfg_to_data(cfg);
542 cfg->pgsize_bitmap = AMD_IOMMU_PGSIZES,
543 cfg->ias = IOMMU_IN_ADDR_BIT_SIZE,
544 cfg->oas = IOMMU_OUT_ADDR_BIT_SIZE,
545 cfg->tlb = &v1_flush_ops;
547 pgtable->iop.ops.map = iommu_v1_map_page;
548 pgtable->iop.ops.unmap = iommu_v1_unmap_page;
549 pgtable->iop.ops.iova_to_phys = iommu_v1_iova_to_phys;
551 return &pgtable->iop;
554 struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns = {
555 .alloc = v1_alloc_pgtable,
556 .free = v1_free_pgtable,