Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iommu/vt-d: Use the generic iommu page table

Replace the VT-d iommu_domain implementation of the VT-d second stage and
first stage page tables with the iommupt VTDSS and x86_64
pagetables. x86_64 is shared with the AMD driver.

There are a couple notable things in VT-d:
- Like AMD the second stage format is not sign extended, unlike AMD it
cannot decode a full 64 bits. The first stage format is a normal sign
extended x86 page table
- The HW caps can indicate how many levels, how many address bits and what
leaf page sizes are supported in HW. As before the highest number of
levels that can translate the entire supported address width is used.
The supported page sizes are adjusted directly from the dedicated
first/second stage cap bits.
- VTD requires flushing 'write buffers'. This logic is left unchanged,
the write buffer flushes on any gather flush or through iotlb_sync_map.
- Like ARM, VTD has an optional non-coherent page table walker that
requires cache flushing. This is supported through PT_FEAT_DMA_INCOHERENT
the same as ARM, however x86 can't use the DMA API for flush, it must
call the arch function clflush_cache_range()
- The PT_FEAT_DYNAMIC_TOP can probably be supported on VT-d someday for the
second stage when it uses 128 bit atomic stores for the HW context
structures.
- PT_FEAT_VTDSS_FORCE_WRITEABLE is used to work around ERRATA_772415_SPR17
- A kernel command line parameter "sp_off" disables all page sizes except
4k

Remove all the unused iommu_domain page table code. The debugfs paths have
their own independent page table walker that is left alone for now.

This corrects a race with the non-coherent walker that the ARM
implementations have fixed:

CPU 0 CPU 1
pfn_to_dma_pte() pfn_to_dma_pte()
pte = &parent[offset];
if (!dma_pte_present(pte)) {
try_cmpxchg64(&pte->val)
pte = &parent[offset];
.. dma_pte_present(pte) ..
[...]
// iommu_map() completes
// Device does DMA
domain_flush_cache(pte)

The CPU 1 mapping operation shares a page table level with the CPU 0
mapping operation. CPU 0 installed a new page table level but has not
flushed it yet. CPU1 returns from iommu_map() and the device does DMA. The
non coherent walker fails to see the new table level installed by CPU 0
and fails the DMA with non-present.

The iommupt PT_FEAT_DMA_INCOHERENT implementation uses the ARM design of
storing a flag when CPU 0 completes the flush. If the flag is not set CPU
1 will also flush to ensure the HW can fully walk to the PTE being
installed.

Cc: Tina Zhang <tina.zhang@intel.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>

authored by

Jason Gunthorpe and committed by
Joerg Roedel
d373449d ef7bfe5b

+175 -861
+4
drivers/iommu/intel/Kconfig
··· 13 13 bool "Support for Intel IOMMU using DMA Remapping Devices" 14 14 depends on PCI_MSI && ACPI && X86 15 15 select IOMMU_API 16 + select GENERIC_PT 17 + select IOMMU_PT 18 + select IOMMU_PT_X86_64 19 + select IOMMU_PT_VTDSS 16 20 select IOMMU_IOVA 17 21 select IOMMU_IOPF 18 22 select IOMMUFD_DRIVER if IOMMUFD
+143 -758
drivers/iommu/intel/iommu.c
··· 45 45 46 46 #define DEFAULT_DOMAIN_ADDRESS_WIDTH 57 47 47 48 - #define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << ((gaw) - VTD_PAGE_SHIFT)) - 1) 49 - #define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << (gaw)) - 1) 50 - 51 - /* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR 52 - to match. That way, we can use 'unsigned long' for PFNs with impunity. */ 53 - #define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \ 54 - __DOMAIN_MAX_PFN(gaw), (unsigned long)-1)) 55 - #define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT) 56 - 57 48 static void __init check_tylersburg_isoch(void); 49 + static int intel_iommu_set_dirty_tracking(struct iommu_domain *domain, 50 + bool enable); 58 51 static int rwbf_quirk; 59 52 60 53 #define rwbf_required(iommu) (rwbf_quirk || cap_rwbf((iommu)->cap)) ··· 210 217 #define IDENTMAP_AZALIA 4 211 218 212 219 const struct iommu_ops intel_iommu_ops; 213 - static const struct iommu_dirty_ops intel_dirty_ops; 214 220 215 221 static bool translation_pre_enabled(struct intel_iommu *iommu) 216 222 { ··· 277 285 } 278 286 __setup("intel_iommu=", intel_iommu_setup); 279 287 280 - static int domain_pfn_supported(struct dmar_domain *domain, unsigned long pfn) 281 - { 282 - int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; 283 - 284 - return !(addr_width < BITS_PER_LONG && pfn >> addr_width); 285 - } 286 - 287 288 /* 288 289 * Calculate the Supported Adjusted Guest Address Widths of an IOMMU. 289 290 * Refer to 11.4.2 of the VT-d spec for the encoding of each bit of ··· 336 351 { 337 352 return sm_supported(iommu) ? 338 353 ecap_smpwc(iommu->ecap) : ecap_coherent(iommu->ecap); 339 - } 340 - 341 - /* Return the super pagesize bitmap if supported. */ 342 - static unsigned long domain_super_pgsize_bitmap(struct dmar_domain *domain) 343 - { 344 - unsigned long bitmap = 0; 345 - 346 - /* 347 - * 1-level super page supports page size of 2MiB, 2-level super page 348 - * supports page size of both 2MiB and 1GiB. 349 - */ 350 - if (domain->iommu_superpage == 1) 351 - bitmap |= SZ_2M; 352 - else if (domain->iommu_superpage == 2) 353 - bitmap |= SZ_2M | SZ_1G; 354 - 355 - return bitmap; 356 354 } 357 355 358 356 struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus, ··· 524 556 return iommu; 525 557 } 526 558 527 - static void domain_flush_cache(struct dmar_domain *domain, 528 - void *addr, int size) 529 - { 530 - if (!domain->iommu_coherency) 531 - clflush_cache_range(addr, size); 532 - } 533 - 534 559 static void free_context_table(struct intel_iommu *iommu) 535 560 { 536 561 struct context_entry *context; ··· 667 706 pgtable_walk(iommu, addr >> VTD_PAGE_SHIFT, bus, devfn, pgtable, level); 668 707 } 669 708 #endif 670 - 671 - static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, 672 - unsigned long pfn, int *target_level, 673 - gfp_t gfp) 674 - { 675 - struct dma_pte *parent, *pte; 676 - int level = agaw_to_level(domain->agaw); 677 - int offset; 678 - 679 - if (!domain_pfn_supported(domain, pfn)) 680 - /* Address beyond IOMMU's addressing capabilities. */ 681 - return NULL; 682 - 683 - parent = domain->pgd; 684 - 685 - while (1) { 686 - void *tmp_page; 687 - 688 - offset = pfn_level_offset(pfn, level); 689 - pte = &parent[offset]; 690 - if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte))) 691 - break; 692 - if (level == *target_level) 693 - break; 694 - 695 - if (!dma_pte_present(pte)) { 696 - uint64_t pteval, tmp; 697 - 698 - tmp_page = iommu_alloc_pages_node_sz(domain->nid, gfp, 699 - SZ_4K); 700 - 701 - if (!tmp_page) 702 - return NULL; 703 - 704 - domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE); 705 - pteval = virt_to_phys(tmp_page) | DMA_PTE_READ | 706 - DMA_PTE_WRITE; 707 - if (domain->use_first_level) 708 - pteval |= DMA_FL_PTE_US | DMA_FL_PTE_ACCESS; 709 - 710 - tmp = 0ULL; 711 - if (!try_cmpxchg64(&pte->val, &tmp, pteval)) 712 - /* Someone else set it while we were thinking; use theirs. */ 713 - iommu_free_pages(tmp_page); 714 - else 715 - domain_flush_cache(domain, pte, sizeof(*pte)); 716 - } 717 - if (level == 1) 718 - break; 719 - 720 - parent = phys_to_virt(dma_pte_addr(pte)); 721 - level--; 722 - } 723 - 724 - if (!*target_level) 725 - *target_level = level; 726 - 727 - return pte; 728 - } 729 - 730 - /* return address's pte at specific level */ 731 - static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain, 732 - unsigned long pfn, 733 - int level, int *large_page) 734 - { 735 - struct dma_pte *parent, *pte; 736 - int total = agaw_to_level(domain->agaw); 737 - int offset; 738 - 739 - parent = domain->pgd; 740 - while (level <= total) { 741 - offset = pfn_level_offset(pfn, total); 742 - pte = &parent[offset]; 743 - if (level == total) 744 - return pte; 745 - 746 - if (!dma_pte_present(pte)) { 747 - *large_page = total; 748 - break; 749 - } 750 - 751 - if (dma_pte_superpage(pte)) { 752 - *large_page = total; 753 - return pte; 754 - } 755 - 756 - parent = phys_to_virt(dma_pte_addr(pte)); 757 - total--; 758 - } 759 - return NULL; 760 - } 761 - 762 - /* clear last level pte, a tlb flush should be followed */ 763 - static void dma_pte_clear_range(struct dmar_domain *domain, 764 - unsigned long start_pfn, 765 - unsigned long last_pfn) 766 - { 767 - unsigned int large_page; 768 - struct dma_pte *first_pte, *pte; 769 - 770 - if (WARN_ON(!domain_pfn_supported(domain, last_pfn)) || 771 - WARN_ON(start_pfn > last_pfn)) 772 - return; 773 - 774 - /* we don't need lock here; nobody else touches the iova range */ 775 - do { 776 - large_page = 1; 777 - first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page); 778 - if (!pte) { 779 - start_pfn = align_to_level(start_pfn + 1, large_page + 1); 780 - continue; 781 - } 782 - do { 783 - dma_clear_pte(pte); 784 - start_pfn += lvl_to_nr_pages(large_page); 785 - pte++; 786 - } while (start_pfn <= last_pfn && !first_pte_in_page(pte)); 787 - 788 - domain_flush_cache(domain, first_pte, 789 - (void *)pte - (void *)first_pte); 790 - 791 - } while (start_pfn && start_pfn <= last_pfn); 792 - } 793 - 794 - static void dma_pte_free_level(struct dmar_domain *domain, int level, 795 - int retain_level, struct dma_pte *pte, 796 - unsigned long pfn, unsigned long start_pfn, 797 - unsigned long last_pfn) 798 - { 799 - pfn = max(start_pfn, pfn); 800 - pte = &pte[pfn_level_offset(pfn, level)]; 801 - 802 - do { 803 - unsigned long level_pfn; 804 - struct dma_pte *level_pte; 805 - 806 - if (!dma_pte_present(pte) || dma_pte_superpage(pte)) 807 - goto next; 808 - 809 - level_pfn = pfn & level_mask(level); 810 - level_pte = phys_to_virt(dma_pte_addr(pte)); 811 - 812 - if (level > 2) { 813 - dma_pte_free_level(domain, level - 1, retain_level, 814 - level_pte, level_pfn, start_pfn, 815 - last_pfn); 816 - } 817 - 818 - /* 819 - * Free the page table if we're below the level we want to 820 - * retain and the range covers the entire table. 821 - */ 822 - if (level < retain_level && !(start_pfn > level_pfn || 823 - last_pfn < level_pfn + level_size(level) - 1)) { 824 - dma_clear_pte(pte); 825 - domain_flush_cache(domain, pte, sizeof(*pte)); 826 - iommu_free_pages(level_pte); 827 - } 828 - next: 829 - pfn += level_size(level); 830 - } while (!first_pte_in_page(++pte) && pfn <= last_pfn); 831 - } 832 - 833 - /* 834 - * clear last level (leaf) ptes and free page table pages below the 835 - * level we wish to keep intact. 836 - */ 837 - static void dma_pte_free_pagetable(struct dmar_domain *domain, 838 - unsigned long start_pfn, 839 - unsigned long last_pfn, 840 - int retain_level) 841 - { 842 - dma_pte_clear_range(domain, start_pfn, last_pfn); 843 - 844 - /* We don't need lock here; nobody else touches the iova range */ 845 - dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level, 846 - domain->pgd, 0, start_pfn, last_pfn); 847 - 848 - /* free pgd */ 849 - if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { 850 - iommu_free_pages(domain->pgd); 851 - domain->pgd = NULL; 852 - } 853 - } 854 - 855 - /* When a page at a given level is being unlinked from its parent, we don't 856 - need to *modify* it at all. All we need to do is make a list of all the 857 - pages which can be freed just as soon as we've flushed the IOTLB and we 858 - know the hardware page-walk will no longer touch them. 859 - The 'pte' argument is the *parent* PTE, pointing to the page that is to 860 - be freed. */ 861 - static void dma_pte_list_pagetables(struct dmar_domain *domain, 862 - int level, struct dma_pte *parent_pte, 863 - struct iommu_pages_list *freelist) 864 - { 865 - struct dma_pte *pte = phys_to_virt(dma_pte_addr(parent_pte)); 866 - 867 - iommu_pages_list_add(freelist, pte); 868 - 869 - if (level == 1) 870 - return; 871 - 872 - do { 873 - if (dma_pte_present(pte) && !dma_pte_superpage(pte)) 874 - dma_pte_list_pagetables(domain, level - 1, pte, freelist); 875 - pte++; 876 - } while (!first_pte_in_page(pte)); 877 - } 878 - 879 - static void dma_pte_clear_level(struct dmar_domain *domain, int level, 880 - struct dma_pte *pte, unsigned long pfn, 881 - unsigned long start_pfn, unsigned long last_pfn, 882 - struct iommu_pages_list *freelist) 883 - { 884 - struct dma_pte *first_pte = NULL, *last_pte = NULL; 885 - 886 - pfn = max(start_pfn, pfn); 887 - pte = &pte[pfn_level_offset(pfn, level)]; 888 - 889 - do { 890 - unsigned long level_pfn = pfn & level_mask(level); 891 - 892 - if (!dma_pte_present(pte)) 893 - goto next; 894 - 895 - /* If range covers entire pagetable, free it */ 896 - if (start_pfn <= level_pfn && 897 - last_pfn >= level_pfn + level_size(level) - 1) { 898 - /* These suborbinate page tables are going away entirely. Don't 899 - bother to clear them; we're just going to *free* them. */ 900 - if (level > 1 && !dma_pte_superpage(pte)) 901 - dma_pte_list_pagetables(domain, level - 1, pte, freelist); 902 - 903 - dma_clear_pte(pte); 904 - if (!first_pte) 905 - first_pte = pte; 906 - last_pte = pte; 907 - } else if (level > 1) { 908 - /* Recurse down into a level that isn't *entirely* obsolete */ 909 - dma_pte_clear_level(domain, level - 1, 910 - phys_to_virt(dma_pte_addr(pte)), 911 - level_pfn, start_pfn, last_pfn, 912 - freelist); 913 - } 914 - next: 915 - pfn = level_pfn + level_size(level); 916 - } while (!first_pte_in_page(++pte) && pfn <= last_pfn); 917 - 918 - if (first_pte) 919 - domain_flush_cache(domain, first_pte, 920 - (void *)++last_pte - (void *)first_pte); 921 - } 922 - 923 - /* We can't just free the pages because the IOMMU may still be walking 924 - the page tables, and may have cached the intermediate levels. The 925 - pages can only be freed after the IOTLB flush has been done. */ 926 - static void domain_unmap(struct dmar_domain *domain, unsigned long start_pfn, 927 - unsigned long last_pfn, 928 - struct iommu_pages_list *freelist) 929 - { 930 - if (WARN_ON(!domain_pfn_supported(domain, last_pfn)) || 931 - WARN_ON(start_pfn > last_pfn)) 932 - return; 933 - 934 - /* we don't need lock here; nobody else touches the iova range */ 935 - dma_pte_clear_level(domain, agaw_to_level(domain->agaw), 936 - domain->pgd, 0, start_pfn, last_pfn, freelist); 937 - 938 - /* free pgd */ 939 - if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { 940 - iommu_pages_list_add(freelist, domain->pgd); 941 - domain->pgd = NULL; 942 - } 943 - } 944 709 945 710 /* iommu handling */ 946 711 static int iommu_alloc_root_entry(struct intel_iommu *iommu) ··· 1147 1460 domain_lookup_dev_info(domain, iommu, bus, devfn); 1148 1461 u16 did = domain_id_iommu(domain, iommu); 1149 1462 int translation = CONTEXT_TT_MULTI_LEVEL; 1150 - struct dma_pte *pgd = domain->pgd; 1463 + struct pt_iommu_vtdss_hw_info pt_info; 1151 1464 struct context_entry *context; 1152 1465 int ret; 1153 1466 1154 1467 if (WARN_ON(!intel_domain_is_ss_paging(domain))) 1155 1468 return -EINVAL; 1469 + 1470 + pt_iommu_vtdss_hw_info(&domain->sspt, &pt_info); 1156 1471 1157 1472 pr_debug("Set context mapping for %02x:%02x.%d\n", 1158 1473 bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); ··· 1178 1489 else 1179 1490 translation = CONTEXT_TT_MULTI_LEVEL; 1180 1491 1181 - context_set_address_root(context, virt_to_phys(pgd)); 1182 - context_set_address_width(context, domain->agaw); 1492 + context_set_address_root(context, pt_info.ssptptr); 1493 + context_set_address_width(context, pt_info.aw); 1183 1494 context_set_translation_type(context, translation); 1184 1495 context_set_fault_enable(context); 1185 1496 context_set_present(context); ··· 1222 1533 return ret; 1223 1534 1224 1535 iommu_enable_pci_ats(info); 1225 - 1226 - return 0; 1227 - } 1228 - 1229 - /* Return largest possible superpage level for a given mapping */ 1230 - static int hardware_largepage_caps(struct dmar_domain *domain, unsigned long iov_pfn, 1231 - unsigned long phy_pfn, unsigned long pages) 1232 - { 1233 - int support, level = 1; 1234 - unsigned long pfnmerge; 1235 - 1236 - support = domain->iommu_superpage; 1237 - 1238 - /* To use a large page, the virtual *and* physical addresses 1239 - must be aligned to 2MiB/1GiB/etc. Lower bits set in either 1240 - of them will mean we have to use smaller pages. So just 1241 - merge them and check both at once. */ 1242 - pfnmerge = iov_pfn | phy_pfn; 1243 - 1244 - while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) { 1245 - pages >>= VTD_STRIDE_SHIFT; 1246 - if (!pages) 1247 - break; 1248 - pfnmerge >>= VTD_STRIDE_SHIFT; 1249 - level++; 1250 - support--; 1251 - } 1252 - return level; 1253 - } 1254 - 1255 - /* 1256 - * Ensure that old small page tables are removed to make room for superpage(s). 1257 - * We're going to add new large pages, so make sure we don't remove their parent 1258 - * tables. The IOTLB/devTLBs should be flushed if any PDE/PTEs are cleared. 1259 - */ 1260 - static void switch_to_super_page(struct dmar_domain *domain, 1261 - unsigned long start_pfn, 1262 - unsigned long end_pfn, int level) 1263 - { 1264 - unsigned long lvl_pages = lvl_to_nr_pages(level); 1265 - struct dma_pte *pte = NULL; 1266 - 1267 - if (WARN_ON(!IS_ALIGNED(start_pfn, lvl_pages) || 1268 - !IS_ALIGNED(end_pfn + 1, lvl_pages))) 1269 - return; 1270 - 1271 - while (start_pfn <= end_pfn) { 1272 - if (!pte) 1273 - pte = pfn_to_dma_pte(domain, start_pfn, &level, 1274 - GFP_ATOMIC); 1275 - 1276 - if (dma_pte_present(pte)) { 1277 - dma_pte_free_pagetable(domain, start_pfn, 1278 - start_pfn + lvl_pages - 1, 1279 - level + 1); 1280 - 1281 - cache_tag_flush_range(domain, start_pfn << VTD_PAGE_SHIFT, 1282 - end_pfn << VTD_PAGE_SHIFT, 0); 1283 - } 1284 - 1285 - pte++; 1286 - start_pfn += lvl_pages; 1287 - if (first_pte_in_page(pte)) 1288 - pte = NULL; 1289 - } 1290 - } 1291 - 1292 - static int 1293 - __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, 1294 - unsigned long phys_pfn, unsigned long nr_pages, int prot, 1295 - gfp_t gfp) 1296 - { 1297 - struct dma_pte *first_pte = NULL, *pte = NULL; 1298 - unsigned int largepage_lvl = 0; 1299 - unsigned long lvl_pages = 0; 1300 - phys_addr_t pteval; 1301 - u64 attr; 1302 - 1303 - if (unlikely(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1))) 1304 - return -EINVAL; 1305 - 1306 - if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) 1307 - return -EINVAL; 1308 - 1309 - if (!(prot & DMA_PTE_WRITE) && domain->nested_parent) { 1310 - pr_err_ratelimited("Read-only mapping is disallowed on the domain which serves as the parent in a nested configuration, due to HW errata (ERRATA_772415_SPR17)\n"); 1311 - return -EINVAL; 1312 - } 1313 - 1314 - attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP); 1315 - if (domain->use_first_level) { 1316 - attr |= DMA_FL_PTE_PRESENT | DMA_FL_PTE_US | DMA_FL_PTE_ACCESS; 1317 - if (prot & DMA_PTE_WRITE) 1318 - attr |= DMA_FL_PTE_DIRTY; 1319 - } 1320 - 1321 - domain->has_mappings = true; 1322 - 1323 - pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | attr; 1324 - 1325 - while (nr_pages > 0) { 1326 - uint64_t tmp; 1327 - 1328 - if (!pte) { 1329 - largepage_lvl = hardware_largepage_caps(domain, iov_pfn, 1330 - phys_pfn, nr_pages); 1331 - 1332 - pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl, 1333 - gfp); 1334 - if (!pte) 1335 - return -ENOMEM; 1336 - first_pte = pte; 1337 - 1338 - lvl_pages = lvl_to_nr_pages(largepage_lvl); 1339 - 1340 - /* It is large page*/ 1341 - if (largepage_lvl > 1) { 1342 - unsigned long end_pfn; 1343 - unsigned long pages_to_remove; 1344 - 1345 - pteval |= DMA_PTE_LARGE_PAGE; 1346 - pages_to_remove = min_t(unsigned long, 1347 - round_down(nr_pages, lvl_pages), 1348 - nr_pte_to_next_page(pte) * lvl_pages); 1349 - end_pfn = iov_pfn + pages_to_remove - 1; 1350 - switch_to_super_page(domain, iov_pfn, end_pfn, largepage_lvl); 1351 - } else { 1352 - pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; 1353 - } 1354 - 1355 - } 1356 - /* We don't need lock here, nobody else 1357 - * touches the iova range 1358 - */ 1359 - tmp = 0ULL; 1360 - if (!try_cmpxchg64_local(&pte->val, &tmp, pteval)) { 1361 - static int dumps = 5; 1362 - pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n", 1363 - iov_pfn, tmp, (unsigned long long)pteval); 1364 - if (dumps) { 1365 - dumps--; 1366 - debug_dma_dump_mappings(NULL); 1367 - } 1368 - WARN_ON(1); 1369 - } 1370 - 1371 - nr_pages -= lvl_pages; 1372 - iov_pfn += lvl_pages; 1373 - phys_pfn += lvl_pages; 1374 - pteval += lvl_pages * VTD_PAGE_SIZE; 1375 - 1376 - /* If the next PTE would be the first in a new page, then we 1377 - * need to flush the cache on the entries we've just written. 1378 - * And then we'll need to recalculate 'pte', so clear it and 1379 - * let it get set again in the if (!pte) block above. 1380 - * 1381 - * If we're done (!nr_pages) we need to flush the cache too. 1382 - * 1383 - * Also if we've been setting superpages, we may need to 1384 - * recalculate 'pte' and switch back to smaller pages for the 1385 - * end of the mapping, if the trailing size is not enough to 1386 - * use another superpage (i.e. nr_pages < lvl_pages). 1387 - */ 1388 - pte++; 1389 - if (!nr_pages || first_pte_in_page(pte) || 1390 - (largepage_lvl > 1 && nr_pages < lvl_pages)) { 1391 - domain_flush_cache(domain, first_pte, 1392 - (void *)pte - (void *)first_pte); 1393 - pte = NULL; 1394 - } 1395 - } 1396 1536 1397 1537 return 0; 1398 1538 } ··· 1287 1769 struct device *dev, 1288 1770 u32 pasid, struct iommu_domain *old) 1289 1771 { 1290 - struct dma_pte *pgd = domain->pgd; 1291 - int level, flags = 0; 1772 + struct pt_iommu_x86_64_hw_info pt_info; 1773 + unsigned int flags = 0; 1292 1774 1293 - level = agaw_to_level(domain->agaw); 1294 - if (level != 4 && level != 5) 1775 + pt_iommu_x86_64_hw_info(&domain->fspt, &pt_info); 1776 + if (WARN_ON(pt_info.levels != 4 && pt_info.levels != 5)) 1295 1777 return -EINVAL; 1296 1778 1297 - if (level == 5) 1779 + if (pt_info.levels == 5) 1298 1780 flags |= PASID_FLAG_FL5LP; 1299 1781 1300 1782 if (domain->force_snooping) ··· 1302 1784 1303 1785 return __domain_setup_first_level(iommu, dev, pasid, 1304 1786 domain_id_iommu(domain, iommu), 1305 - __pa(pgd), flags, old); 1787 + pt_info.gcr3_pt, flags, old); 1306 1788 } 1307 1789 1308 1790 static int dmar_domain_attach_device(struct dmar_domain *domain, ··· 2770 3252 } 2771 3253 }; 2772 3254 2773 - static int iommu_superpage_capability(struct intel_iommu *iommu, bool first_stage) 3255 + static struct dmar_domain *paging_domain_alloc(void) 2774 3256 { 2775 - if (!intel_iommu_superpage) 2776 - return 0; 2777 - 2778 - if (first_stage) 2779 - return cap_fl1gp_support(iommu->cap) ? 2 : 1; 2780 - 2781 - return fls(cap_super_page_val(iommu->cap)); 2782 - } 2783 - 2784 - static struct dmar_domain *paging_domain_alloc(struct device *dev, bool first_stage) 2785 - { 2786 - struct device_domain_info *info = dev_iommu_priv_get(dev); 2787 - struct intel_iommu *iommu = info->iommu; 2788 3257 struct dmar_domain *domain; 2789 - int addr_width; 2790 3258 2791 3259 domain = kzalloc(sizeof(*domain), GFP_KERNEL); 2792 3260 if (!domain) ··· 2787 3283 INIT_LIST_HEAD(&domain->s1_domains); 2788 3284 spin_lock_init(&domain->s1_lock); 2789 3285 2790 - domain->nid = dev_to_node(dev); 2791 - domain->use_first_level = first_stage; 2792 - 2793 - domain->domain.type = IOMMU_DOMAIN_UNMANAGED; 2794 - 2795 - /* calculate the address width */ 2796 - addr_width = agaw_to_width(iommu->agaw); 2797 - if (addr_width > cap_mgaw(iommu->cap)) 2798 - addr_width = cap_mgaw(iommu->cap); 2799 - domain->gaw = addr_width; 2800 - domain->agaw = iommu->agaw; 2801 - domain->max_addr = __DOMAIN_MAX_ADDR(addr_width); 2802 - 2803 - /* iommu memory access coherency */ 2804 - domain->iommu_coherency = iommu_paging_structure_coherency(iommu); 2805 - 2806 - /* pagesize bitmap */ 2807 - domain->domain.pgsize_bitmap = SZ_4K; 2808 - domain->iommu_superpage = iommu_superpage_capability(iommu, first_stage); 2809 - domain->domain.pgsize_bitmap |= domain_super_pgsize_bitmap(domain); 2810 - 2811 - /* 2812 - * IOVA aperture: First-level translation restricts the input-address 2813 - * to a canonical address (i.e., address bits 63:N have the same value 2814 - * as address bit [N-1], where N is 48-bits with 4-level paging and 2815 - * 57-bits with 5-level paging). Hence, skip bit [N-1]. 2816 - */ 2817 - domain->domain.geometry.force_aperture = true; 2818 - domain->domain.geometry.aperture_start = 0; 2819 - if (first_stage) 2820 - domain->domain.geometry.aperture_end = __DOMAIN_MAX_ADDR(domain->gaw - 1); 2821 - else 2822 - domain->domain.geometry.aperture_end = __DOMAIN_MAX_ADDR(domain->gaw); 2823 - 2824 - /* always allocate the top pgd */ 2825 - domain->pgd = iommu_alloc_pages_node_sz(domain->nid, GFP_KERNEL, SZ_4K); 2826 - if (!domain->pgd) { 2827 - kfree(domain); 2828 - return ERR_PTR(-ENOMEM); 2829 - } 2830 - domain_flush_cache(domain, domain->pgd, PAGE_SIZE); 2831 - 2832 3286 return domain; 2833 3287 } 2834 3288 ··· 2794 3332 intel_iommu_domain_alloc_first_stage(struct device *dev, 2795 3333 struct intel_iommu *iommu, u32 flags) 2796 3334 { 3335 + struct pt_iommu_x86_64_cfg cfg = {}; 2797 3336 struct dmar_domain *dmar_domain; 3337 + int ret; 2798 3338 2799 3339 if (flags & ~IOMMU_HWPT_ALLOC_PASID) 2800 3340 return ERR_PTR(-EOPNOTSUPP); ··· 2805 3341 if (!sm_supported(iommu) || !ecap_flts(iommu->ecap)) 2806 3342 return ERR_PTR(-EOPNOTSUPP); 2807 3343 2808 - dmar_domain = paging_domain_alloc(dev, true); 3344 + dmar_domain = paging_domain_alloc(); 2809 3345 if (IS_ERR(dmar_domain)) 2810 3346 return ERR_CAST(dmar_domain); 2811 3347 3348 + if (cap_fl5lp_support(iommu->cap)) 3349 + cfg.common.hw_max_vasz_lg2 = 57; 3350 + else 3351 + cfg.common.hw_max_vasz_lg2 = 48; 3352 + cfg.common.hw_max_oasz_lg2 = 52; 3353 + cfg.common.features = BIT(PT_FEAT_SIGN_EXTEND) | 3354 + BIT(PT_FEAT_FLUSH_RANGE); 3355 + /* First stage always uses scalable mode */ 3356 + if (!ecap_smpwc(iommu->ecap)) 3357 + cfg.common.features |= BIT(PT_FEAT_DMA_INCOHERENT); 3358 + dmar_domain->iommu.iommu_device = dev; 3359 + dmar_domain->iommu.nid = dev_to_node(dev); 2812 3360 dmar_domain->domain.ops = &intel_fs_paging_domain_ops; 2813 3361 /* 2814 3362 * iotlb sync for map is only needed for legacy implementations that ··· 2830 3354 if (rwbf_required(iommu)) 2831 3355 dmar_domain->iotlb_sync_map = true; 2832 3356 3357 + ret = pt_iommu_x86_64_init(&dmar_domain->fspt, &cfg, GFP_KERNEL); 3358 + if (ret) { 3359 + kfree(dmar_domain); 3360 + return ERR_PTR(ret); 3361 + } 3362 + 3363 + if (!cap_fl1gp_support(iommu->cap)) 3364 + dmar_domain->domain.pgsize_bitmap &= ~(u64)SZ_1G; 3365 + if (!intel_iommu_superpage) 3366 + dmar_domain->domain.pgsize_bitmap = SZ_4K; 3367 + 2833 3368 return &dmar_domain->domain; 2834 3369 } 3370 + 3371 + static int compute_vasz_lg2_ss(struct intel_iommu *iommu) 3372 + { 3373 + unsigned int sagaw = cap_sagaw(iommu->cap); 3374 + unsigned int mgaw = cap_mgaw(iommu->cap); 3375 + 3376 + /* 3377 + * Find the largest table size that both the mgaw and sagaw support. 3378 + * This sets both the number of table levels and the valid range of 3379 + * IOVA. 3380 + */ 3381 + if (mgaw >= 48 && (sagaw & BIT(3))) 3382 + return min(57, mgaw); 3383 + else if (mgaw >= 39 && (sagaw & BIT(2))) 3384 + return min(48, mgaw); 3385 + else if (mgaw >= 30 && (sagaw & BIT(1))) 3386 + return min(39, mgaw); 3387 + return 0; 3388 + } 3389 + 3390 + static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { 3391 + IOMMU_PT_DIRTY_OPS(vtdss), 3392 + .set_dirty_tracking = intel_iommu_set_dirty_tracking, 3393 + }; 2835 3394 2836 3395 static struct iommu_domain * 2837 3396 intel_iommu_domain_alloc_second_stage(struct device *dev, 2838 3397 struct intel_iommu *iommu, u32 flags) 2839 3398 { 3399 + struct pt_iommu_vtdss_cfg cfg = {}; 2840 3400 struct dmar_domain *dmar_domain; 3401 + unsigned int sslps; 3402 + int ret; 2841 3403 2842 3404 if (flags & 2843 3405 (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING | ··· 2892 3378 if (sm_supported(iommu) && !ecap_slts(iommu->ecap)) 2893 3379 return ERR_PTR(-EOPNOTSUPP); 2894 3380 2895 - dmar_domain = paging_domain_alloc(dev, false); 3381 + dmar_domain = paging_domain_alloc(); 2896 3382 if (IS_ERR(dmar_domain)) 2897 3383 return ERR_CAST(dmar_domain); 2898 3384 3385 + cfg.common.hw_max_vasz_lg2 = compute_vasz_lg2_ss(iommu); 3386 + cfg.common.hw_max_oasz_lg2 = 52; 3387 + cfg.common.features = BIT(PT_FEAT_FLUSH_RANGE); 3388 + 3389 + /* 3390 + * Read-only mapping is disallowed on the domain which serves as the 3391 + * parent in a nested configuration, due to HW errata 3392 + * (ERRATA_772415_SPR17) 3393 + */ 3394 + if (flags & IOMMU_HWPT_ALLOC_NEST_PARENT) 3395 + cfg.common.features |= BIT(PT_FEAT_VTDSS_FORCE_WRITEABLE); 3396 + 3397 + if (!iommu_paging_structure_coherency(iommu)) 3398 + cfg.common.features |= BIT(PT_FEAT_DMA_INCOHERENT); 3399 + dmar_domain->iommu.iommu_device = dev; 3400 + dmar_domain->iommu.nid = dev_to_node(dev); 2899 3401 dmar_domain->domain.ops = &intel_ss_paging_domain_ops; 2900 3402 dmar_domain->nested_parent = flags & IOMMU_HWPT_ALLOC_NEST_PARENT; 2901 3403 2902 3404 if (flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING) 2903 - dmar_domain->domain.dirty_ops = &intel_dirty_ops; 3405 + dmar_domain->domain.dirty_ops = &intel_second_stage_dirty_ops; 3406 + 3407 + ret = pt_iommu_vtdss_init(&dmar_domain->sspt, &cfg, GFP_KERNEL); 3408 + if (ret) { 3409 + kfree(dmar_domain); 3410 + return ERR_PTR(ret); 3411 + } 3412 + 3413 + /* Adjust the supported page sizes to HW capability */ 3414 + sslps = cap_super_page_val(iommu->cap); 3415 + if (!(sslps & BIT(0))) 3416 + dmar_domain->domain.pgsize_bitmap &= ~(u64)SZ_2M; 3417 + if (!(sslps & BIT(1))) 3418 + dmar_domain->domain.pgsize_bitmap &= ~(u64)SZ_1G; 3419 + if (!intel_iommu_superpage) 3420 + dmar_domain->domain.pgsize_bitmap = SZ_4K; 2904 3421 2905 3422 /* 2906 3423 * Besides the internal write buffer flush, the caching mode used for ··· 2973 3428 if (WARN_ON(!list_empty(&dmar_domain->devices))) 2974 3429 return; 2975 3430 2976 - if (dmar_domain->pgd) { 2977 - struct iommu_pages_list freelist = 2978 - IOMMU_PAGES_LIST_INIT(freelist); 2979 - 2980 - domain_unmap(dmar_domain, 0, DOMAIN_MAX_PFN(dmar_domain->gaw), 2981 - &freelist); 2982 - iommu_put_pages_list(&freelist); 2983 - } 3431 + pt_iommu_deinit(&dmar_domain->iommu); 2984 3432 2985 3433 kfree(dmar_domain->qi_batch); 2986 3434 kfree(dmar_domain); ··· 2988 3450 2989 3451 /* Only SL is available in legacy mode */ 2990 3452 if (!sm_supported(iommu) || !ecap_flts(iommu->ecap)) 3453 + return -EINVAL; 3454 + 3455 + if (!!ecap_smpwc(iommu->ecap) != 3456 + !(dmar_domain->fspt.x86_64_pt.common.features & 3457 + BIT(PT_FEAT_DMA_INCOHERENT))) 3458 + return -EINVAL; 3459 + 3460 + /* Supports the number of table levels */ 3461 + if (!cap_fl5lp_support(iommu->cap) && 3462 + dmar_domain->fspt.x86_64_pt.common.max_vasz_lg2 > 48) 2991 3463 return -EINVAL; 2992 3464 2993 3465 /* Same page size support */ ··· 3016 3468 paging_domain_compatible_second_stage(struct dmar_domain *dmar_domain, 3017 3469 struct intel_iommu *iommu) 3018 3470 { 3471 + unsigned int vasz_lg2 = dmar_domain->sspt.vtdss_pt.common.max_vasz_lg2; 3019 3472 unsigned int sslps = cap_super_page_val(iommu->cap); 3473 + struct pt_iommu_vtdss_hw_info pt_info; 3474 + 3475 + pt_iommu_vtdss_hw_info(&dmar_domain->sspt, &pt_info); 3020 3476 3021 3477 if (dmar_domain->domain.dirty_ops && !ssads_supported(iommu)) 3022 3478 return -EINVAL; ··· 3029 3477 3030 3478 /* Legacy mode always supports second stage */ 3031 3479 if (sm_supported(iommu) && !ecap_slts(iommu->ecap)) 3480 + return -EINVAL; 3481 + 3482 + if (iommu_paging_structure_coherency(iommu) != 3483 + !(dmar_domain->sspt.vtdss_pt.common.features & 3484 + BIT(PT_FEAT_DMA_INCOHERENT))) 3485 + return -EINVAL; 3486 + 3487 + /* Address width falls within the capability */ 3488 + if (cap_mgaw(iommu->cap) < vasz_lg2) 3489 + return -EINVAL; 3490 + 3491 + /* Page table level is supported. */ 3492 + if (!(cap_sagaw(iommu->cap) & BIT(pt_info.aw))) 3032 3493 return -EINVAL; 3033 3494 3034 3495 /* Same page size support */ ··· 3055 3490 !dmar_domain->iotlb_sync_map) 3056 3491 return -EINVAL; 3057 3492 3493 + /* 3494 + * FIXME this is locked wrong, it needs to be under the 3495 + * dmar_domain->lock 3496 + */ 3497 + if ((dmar_domain->sspt.vtdss_pt.common.features & 3498 + BIT(PT_FEAT_VTDSS_FORCE_COHERENCE)) && 3499 + !ecap_sc_support(iommu->ecap)) 3500 + return -EINVAL; 3058 3501 return 0; 3059 3502 } 3060 3503 ··· 3072 3499 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 3073 3500 struct intel_iommu *iommu = info->iommu; 3074 3501 int ret = -EINVAL; 3075 - int addr_width; 3076 3502 3077 3503 if (intel_domain_is_fs_paging(dmar_domain)) 3078 3504 ret = paging_domain_compatible_first_stage(dmar_domain, iommu); ··· 3081 3509 ret = -EINVAL; 3082 3510 if (ret) 3083 3511 return ret; 3084 - 3085 - /* 3086 - * FIXME this is locked wrong, it needs to be under the 3087 - * dmar_domain->lock 3088 - */ 3089 - if (dmar_domain->force_snooping && !ecap_sc_support(iommu->ecap)) 3090 - return -EINVAL; 3091 - 3092 - if (dmar_domain->iommu_coherency != 3093 - iommu_paging_structure_coherency(iommu)) 3094 - return -EINVAL; 3095 - 3096 - 3097 - /* check if this iommu agaw is sufficient for max mapped address */ 3098 - addr_width = agaw_to_width(iommu->agaw); 3099 - if (addr_width > cap_mgaw(iommu->cap)) 3100 - addr_width = cap_mgaw(iommu->cap); 3101 - 3102 - if (dmar_domain->gaw > addr_width || dmar_domain->agaw > iommu->agaw) 3103 - return -EINVAL; 3104 3512 3105 3513 if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev) && 3106 3514 context_copied(iommu, info->bus, info->devfn)) ··· 3112 3560 return ret; 3113 3561 } 3114 3562 3115 - static int intel_iommu_map(struct iommu_domain *domain, 3116 - unsigned long iova, phys_addr_t hpa, 3117 - size_t size, int iommu_prot, gfp_t gfp) 3118 - { 3119 - struct dmar_domain *dmar_domain = to_dmar_domain(domain); 3120 - u64 max_addr; 3121 - int prot = 0; 3122 - 3123 - if (iommu_prot & IOMMU_READ) 3124 - prot |= DMA_PTE_READ; 3125 - if (iommu_prot & IOMMU_WRITE) 3126 - prot |= DMA_PTE_WRITE; 3127 - if (dmar_domain->set_pte_snp) 3128 - prot |= DMA_PTE_SNP; 3129 - 3130 - max_addr = iova + size; 3131 - if (dmar_domain->max_addr < max_addr) { 3132 - u64 end; 3133 - 3134 - /* check if minimum agaw is sufficient for mapped address */ 3135 - end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1; 3136 - if (end < max_addr) { 3137 - pr_err("%s: iommu width (%d) is not " 3138 - "sufficient for the mapped address (%llx)\n", 3139 - __func__, dmar_domain->gaw, max_addr); 3140 - return -EFAULT; 3141 - } 3142 - dmar_domain->max_addr = max_addr; 3143 - } 3144 - /* Round up size to next multiple of PAGE_SIZE, if it and 3145 - the low bits of hpa would take us onto the next page */ 3146 - size = aligned_nrpages(hpa, size); 3147 - return __domain_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT, 3148 - hpa >> VTD_PAGE_SHIFT, size, prot, gfp); 3149 - } 3150 - 3151 - static int intel_iommu_map_pages(struct iommu_domain *domain, 3152 - unsigned long iova, phys_addr_t paddr, 3153 - size_t pgsize, size_t pgcount, 3154 - int prot, gfp_t gfp, size_t *mapped) 3155 - { 3156 - unsigned long pgshift = __ffs(pgsize); 3157 - size_t size = pgcount << pgshift; 3158 - int ret; 3159 - 3160 - if (pgsize != SZ_4K && pgsize != SZ_2M && pgsize != SZ_1G) 3161 - return -EINVAL; 3162 - 3163 - if (!IS_ALIGNED(iova | paddr, pgsize)) 3164 - return -EINVAL; 3165 - 3166 - ret = intel_iommu_map(domain, iova, paddr, size, prot, gfp); 3167 - if (!ret && mapped) 3168 - *mapped = size; 3169 - 3170 - return ret; 3171 - } 3172 - 3173 - static size_t intel_iommu_unmap(struct iommu_domain *domain, 3174 - unsigned long iova, size_t size, 3175 - struct iommu_iotlb_gather *gather) 3176 - { 3177 - struct dmar_domain *dmar_domain = to_dmar_domain(domain); 3178 - unsigned long start_pfn, last_pfn; 3179 - int level = 0; 3180 - 3181 - /* Cope with horrid API which requires us to unmap more than the 3182 - size argument if it happens to be a large-page mapping. */ 3183 - if (unlikely(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, 3184 - &level, GFP_ATOMIC))) 3185 - return 0; 3186 - 3187 - if (size < VTD_PAGE_SIZE << level_to_offset_bits(level)) 3188 - size = VTD_PAGE_SIZE << level_to_offset_bits(level); 3189 - 3190 - start_pfn = iova >> VTD_PAGE_SHIFT; 3191 - last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT; 3192 - 3193 - domain_unmap(dmar_domain, start_pfn, last_pfn, &gather->freelist); 3194 - 3195 - if (dmar_domain->max_addr == iova + size) 3196 - dmar_domain->max_addr = iova; 3197 - 3198 - /* 3199 - * We do not use page-selective IOTLB invalidation in flush queue, 3200 - * so there is no need to track page and sync iotlb. 3201 - */ 3202 - if (!iommu_iotlb_gather_queued(gather)) 3203 - iommu_iotlb_gather_add_page(domain, gather, iova, size); 3204 - 3205 - return size; 3206 - } 3207 - 3208 - static size_t intel_iommu_unmap_pages(struct iommu_domain *domain, 3209 - unsigned long iova, 3210 - size_t pgsize, size_t pgcount, 3211 - struct iommu_iotlb_gather *gather) 3212 - { 3213 - unsigned long pgshift = __ffs(pgsize); 3214 - size_t size = pgcount << pgshift; 3215 - 3216 - return intel_iommu_unmap(domain, iova, size, gather); 3217 - } 3218 - 3219 3563 static void intel_iommu_tlb_sync(struct iommu_domain *domain, 3220 3564 struct iommu_iotlb_gather *gather) 3221 3565 { ··· 3119 3671 gather->end, 3120 3672 iommu_pages_list_empty(&gather->freelist)); 3121 3673 iommu_put_pages_list(&gather->freelist); 3122 - } 3123 - 3124 - static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, 3125 - dma_addr_t iova) 3126 - { 3127 - struct dmar_domain *dmar_domain = to_dmar_domain(domain); 3128 - struct dma_pte *pte; 3129 - int level = 0; 3130 - u64 phys = 0; 3131 - 3132 - pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level, 3133 - GFP_ATOMIC); 3134 - if (pte && dma_pte_present(pte)) 3135 - phys = dma_pte_addr(pte) + 3136 - (iova & (BIT_MASK(level_to_offset_bits(level) + 3137 - VTD_PAGE_SHIFT) - 1)); 3138 - 3139 - return phys; 3140 3674 } 3141 3675 3142 3676 static bool domain_support_force_snooping(struct dmar_domain *domain) ··· 3162 3732 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 3163 3733 3164 3734 guard(spinlock_irqsave)(&dmar_domain->lock); 3165 - if (!domain_support_force_snooping(dmar_domain) || 3166 - dmar_domain->has_mappings) 3735 + if (!domain_support_force_snooping(dmar_domain)) 3167 3736 return false; 3168 3737 3169 3738 /* 3170 3739 * Second level page table supports per-PTE snoop control. The 3171 3740 * iommu_map() interface will handle this by setting SNP bit. 3172 3741 */ 3173 - dmar_domain->set_pte_snp = true; 3742 + dmar_domain->sspt.vtdss_pt.common.features |= 3743 + BIT(PT_FEAT_VTDSS_FORCE_COHERENCE); 3174 3744 dmar_domain->force_snooping = true; 3175 3745 return true; 3176 3746 } ··· 3734 4304 return ret; 3735 4305 } 3736 4306 3737 - static int intel_iommu_read_and_clear_dirty(struct iommu_domain *domain, 3738 - unsigned long iova, size_t size, 3739 - unsigned long flags, 3740 - struct iommu_dirty_bitmap *dirty) 3741 - { 3742 - struct dmar_domain *dmar_domain = to_dmar_domain(domain); 3743 - unsigned long end = iova + size - 1; 3744 - unsigned long pgsize; 3745 - 3746 - /* 3747 - * IOMMUFD core calls into a dirty tracking disabled domain without an 3748 - * IOVA bitmap set in order to clean dirty bits in all PTEs that might 3749 - * have occurred when we stopped dirty tracking. This ensures that we 3750 - * never inherit dirtied bits from a previous cycle. 3751 - */ 3752 - if (!dmar_domain->dirty_tracking && dirty->bitmap) 3753 - return -EINVAL; 3754 - 3755 - do { 3756 - struct dma_pte *pte; 3757 - int lvl = 0; 3758 - 3759 - pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &lvl, 3760 - GFP_ATOMIC); 3761 - pgsize = level_size(lvl) << VTD_PAGE_SHIFT; 3762 - if (!pte || !dma_pte_present(pte)) { 3763 - iova += pgsize; 3764 - continue; 3765 - } 3766 - 3767 - if (dma_sl_pte_test_and_clear_dirty(pte, flags)) 3768 - iommu_dirty_bitmap_record(dirty, iova, pgsize); 3769 - iova += pgsize; 3770 - } while (iova < end); 3771 - 3772 - return 0; 3773 - } 3774 - 3775 - static const struct iommu_dirty_ops intel_dirty_ops = { 3776 - .set_dirty_tracking = intel_iommu_set_dirty_tracking, 3777 - .read_and_clear_dirty = intel_iommu_read_and_clear_dirty, 3778 - }; 3779 - 3780 4307 static int context_setup_pass_through(struct device *dev, u8 bus, u8 devfn) 3781 4308 { 3782 4309 struct device_domain_info *info = dev_iommu_priv_get(dev); ··· 3853 4466 }; 3854 4467 3855 4468 const struct iommu_domain_ops intel_fs_paging_domain_ops = { 4469 + IOMMU_PT_DOMAIN_OPS(x86_64), 3856 4470 .attach_dev = intel_iommu_attach_device, 3857 4471 .set_dev_pasid = intel_iommu_set_dev_pasid, 3858 - .map_pages = intel_iommu_map_pages, 3859 - .unmap_pages = intel_iommu_unmap_pages, 3860 4472 .iotlb_sync_map = intel_iommu_iotlb_sync_map, 3861 4473 .flush_iotlb_all = intel_flush_iotlb_all, 3862 4474 .iotlb_sync = intel_iommu_tlb_sync, 3863 - .iova_to_phys = intel_iommu_iova_to_phys, 3864 4475 .free = intel_iommu_domain_free, 3865 4476 .enforce_cache_coherency = intel_iommu_enforce_cache_coherency_fs, 3866 4477 }; 3867 4478 3868 4479 const struct iommu_domain_ops intel_ss_paging_domain_ops = { 4480 + IOMMU_PT_DOMAIN_OPS(vtdss), 3869 4481 .attach_dev = intel_iommu_attach_device, 3870 4482 .set_dev_pasid = intel_iommu_set_dev_pasid, 3871 - .map_pages = intel_iommu_map_pages, 3872 - .unmap_pages = intel_iommu_unmap_pages, 3873 4483 .iotlb_sync_map = intel_iommu_iotlb_sync_map, 3874 4484 .flush_iotlb_all = intel_flush_iotlb_all, 3875 4485 .iotlb_sync = intel_iommu_tlb_sync, 3876 - .iova_to_phys = intel_iommu_iova_to_phys, 3877 4486 .free = intel_iommu_domain_free, 3878 4487 .enforce_cache_coherency = intel_iommu_enforce_cache_coherency_ss, 3879 4488 }; ··· 4184 4801 4185 4802 return ret; 4186 4803 } 4804 + 4805 + MODULE_IMPORT_NS("GENERIC_PT_IOMMU");
+14 -83
drivers/iommu/intel/iommu.h
··· 23 23 #include <linux/xarray.h> 24 24 #include <linux/perf_event.h> 25 25 #include <linux/pci.h> 26 + #include <linux/generic_pt/iommu.h> 26 27 27 - #include <asm/cacheflush.h> 28 28 #include <asm/iommu.h> 29 29 #include <uapi/linux/iommufd.h> 30 30 ··· 595 595 }; 596 596 597 597 struct dmar_domain { 598 - int nid; /* node id */ 598 + union { 599 + struct iommu_domain domain; 600 + struct pt_iommu iommu; 601 + /* First stage page table */ 602 + struct pt_iommu_x86_64 fspt; 603 + /* Second stage page table */ 604 + struct pt_iommu_vtdss sspt; 605 + }; 606 + 599 607 struct xarray iommu_array; /* Attached IOMMU array */ 600 608 601 - u8 iommu_coherency: 1; /* indicate coherency of iommu access */ 602 - u8 force_snooping : 1; /* Create IOPTEs with snoop control */ 603 - u8 set_pte_snp:1; 604 - u8 use_first_level:1; /* DMA translation for the domain goes 605 - * through the first level page table, 606 - * otherwise, goes through the second 607 - * level. 608 - */ 609 + u8 force_snooping:1; /* Create PASID entry with snoop control */ 609 610 u8 dirty_tracking:1; /* Dirty tracking is enabled */ 610 611 u8 nested_parent:1; /* Has other domains nested on it */ 611 - u8 has_mappings:1; /* Has mappings configured through 612 - * iommu_map() interface. 613 - */ 614 612 u8 iotlb_sync_map:1; /* Need to flush IOTLB cache or write 615 613 * buffer when creating mappings. 616 614 */ ··· 621 623 struct list_head cache_tags; /* Cache tag list */ 622 624 struct qi_batch *qi_batch; /* Batched QI descriptors */ 623 625 624 - int iommu_superpage;/* Level of superpages supported: 625 - 0 == 4KiB (no superpages), 1 == 2MiB, 626 - 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */ 627 626 union { 628 627 /* DMA remapping domain */ 629 628 struct { 630 - /* virtual address */ 631 - struct dma_pte *pgd; 632 - /* max guest address width */ 633 - int gaw; 634 - /* 635 - * adjusted guest address width: 636 - * 0: level 2 30-bit 637 - * 1: level 3 39-bit 638 - * 2: level 4 48-bit 639 - * 3: level 5 57-bit 640 - */ 641 - int agaw; 642 - /* maximum mapped address */ 643 - u64 max_addr; 644 629 /* Protect the s1_domains list */ 645 630 spinlock_t s1_lock; 646 631 /* Track s1_domains nested on this domain */ ··· 645 664 struct mmu_notifier notifier; 646 665 }; 647 666 }; 648 - 649 - struct iommu_domain domain; /* generic domain data structure for 650 - iommu core */ 651 667 }; 668 + PT_IOMMU_CHECK_DOMAIN(struct dmar_domain, iommu, domain); 669 + PT_IOMMU_CHECK_DOMAIN(struct dmar_domain, sspt.iommu, domain); 670 + PT_IOMMU_CHECK_DOMAIN(struct dmar_domain, fspt.iommu, domain); 652 671 653 672 /* 654 673 * In theory, the VT-d 4.0 spec can support up to 2 ^ 16 counters. ··· 847 866 u64 val; 848 867 }; 849 868 850 - static inline void dma_clear_pte(struct dma_pte *pte) 851 - { 852 - pte->val = 0; 853 - } 854 - 855 869 static inline u64 dma_pte_addr(struct dma_pte *pte) 856 870 { 857 871 #ifdef CONFIG_64BIT ··· 862 886 return (pte->val & 3) != 0; 863 887 } 864 888 865 - static inline bool dma_sl_pte_test_and_clear_dirty(struct dma_pte *pte, 866 - unsigned long flags) 867 - { 868 - if (flags & IOMMU_DIRTY_NO_CLEAR) 869 - return (pte->val & DMA_SL_PTE_DIRTY) != 0; 870 - 871 - return test_and_clear_bit(DMA_SL_PTE_DIRTY_BIT, 872 - (unsigned long *)&pte->val); 873 - } 874 - 875 889 static inline bool dma_pte_superpage(struct dma_pte *pte) 876 890 { 877 891 return (pte->val & DMA_PTE_LARGE_PAGE); 878 - } 879 - 880 - static inline bool first_pte_in_page(struct dma_pte *pte) 881 - { 882 - return IS_ALIGNED((unsigned long)pte, VTD_PAGE_SIZE); 883 - } 884 - 885 - static inline int nr_pte_to_next_page(struct dma_pte *pte) 886 - { 887 - return first_pte_in_page(pte) ? BIT_ULL(VTD_STRIDE_SHIFT) : 888 - (struct dma_pte *)ALIGN((unsigned long)pte, VTD_PAGE_SIZE) - pte; 889 892 } 890 893 891 894 static inline bool context_present(struct context_entry *context) ··· 882 927 return agaw + 2; 883 928 } 884 929 885 - static inline int agaw_to_width(int agaw) 886 - { 887 - return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH); 888 - } 889 - 890 930 static inline int width_to_agaw(int width) 891 931 { 892 932 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE); ··· 897 947 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK; 898 948 } 899 949 900 - static inline u64 level_mask(int level) 901 - { 902 - return -1ULL << level_to_offset_bits(level); 903 - } 904 - 905 - static inline u64 level_size(int level) 906 - { 907 - return 1ULL << level_to_offset_bits(level); 908 - } 909 - 910 - static inline u64 align_to_level(u64 pfn, int level) 911 - { 912 - return (pfn + level_size(level) - 1) & level_mask(level); 913 - } 914 - 915 - static inline unsigned long lvl_to_nr_pages(unsigned int lvl) 916 - { 917 - return 1UL << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH); 918 - } 919 950 920 951 static inline void context_set_present(struct context_entry *context) 921 952 {
-5
drivers/iommu/intel/nested.c
··· 29 29 30 30 device_block_translation(dev); 31 31 32 - if (iommu->agaw < dmar_domain->s2_domain->agaw) { 33 - dev_err_ratelimited(dev, "Adjusted guest address width not compatible\n"); 34 - return -ENODEV; 35 - } 36 - 37 32 /* 38 33 * Stage-1 domain cannot work alone, it is nested on a s2_domain. 39 34 * The s2_domain will be used in nested translation, hence needs
+14 -15
drivers/iommu/intel/pasid.c
··· 483 483 struct dmar_domain *domain, 484 484 struct device *dev, u32 pasid) 485 485 { 486 + struct pt_iommu_vtdss_hw_info pt_info; 486 487 struct pasid_entry *pte; 487 - struct dma_pte *pgd; 488 - u64 pgd_val; 489 488 u16 did; 489 + 490 + pt_iommu_vtdss_hw_info(&domain->sspt, &pt_info); 490 491 491 492 /* 492 493 * If hardware advertises no support for second level ··· 499 498 return -EINVAL; 500 499 } 501 500 502 - pgd = domain->pgd; 503 - pgd_val = virt_to_phys(pgd); 504 501 did = domain_id_iommu(domain, iommu); 505 502 506 503 spin_lock(&iommu->lock); ··· 513 514 return -EBUSY; 514 515 } 515 516 516 - pasid_pte_config_second_level(iommu, pte, pgd_val, domain->agaw, 517 + pasid_pte_config_second_level(iommu, pte, pt_info.ssptptr, pt_info.aw, 517 518 did, domain->dirty_tracking); 518 519 spin_unlock(&iommu->lock); 519 520 ··· 527 528 struct device *dev, u16 old_did, 528 529 u32 pasid) 529 530 { 531 + struct pt_iommu_vtdss_hw_info pt_info; 530 532 struct pasid_entry *pte, new_pte; 531 - struct dma_pte *pgd; 532 - u64 pgd_val; 533 533 u16 did; 534 + 535 + pt_iommu_vtdss_hw_info(&domain->sspt, &pt_info); 534 536 535 537 /* 536 538 * If hardware advertises no support for second level ··· 543 543 return -EINVAL; 544 544 } 545 545 546 - pgd = domain->pgd; 547 - pgd_val = virt_to_phys(pgd); 548 546 did = domain_id_iommu(domain, iommu); 549 547 550 - pasid_pte_config_second_level(iommu, &new_pte, pgd_val, 551 - domain->agaw, did, 552 - domain->dirty_tracking); 548 + pasid_pte_config_second_level(iommu, &new_pte, pt_info.ssptptr, 549 + pt_info.aw, did, domain->dirty_tracking); 553 550 554 551 spin_lock(&iommu->lock); 555 552 pte = intel_pasid_get_entry(dev, pasid); ··· 744 747 struct dmar_domain *s2_domain, 745 748 u16 did) 746 749 { 747 - struct dma_pte *pgd = s2_domain->pgd; 750 + struct pt_iommu_vtdss_hw_info pt_info; 748 751 749 752 lockdep_assert_held(&iommu->lock); 753 + 754 + pt_iommu_vtdss_hw_info(&s2_domain->sspt, &pt_info); 750 755 751 756 pasid_clear_entry(pte); 752 757 ··· 769 770 if (s2_domain->force_snooping) 770 771 pasid_set_pgsnp(pte); 771 772 772 - pasid_set_slptr(pte, virt_to_phys(pgd)); 773 + pasid_set_slptr(pte, pt_info.ssptptr); 773 774 pasid_set_fault_enable(pte); 774 775 pasid_set_domain_id(pte, did); 775 - pasid_set_address_width(pte, s2_domain->agaw); 776 + pasid_set_address_width(pte, pt_info.aw); 776 777 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); 777 778 if (s2_domain->dirty_tracking) 778 779 pasid_set_ssade(pte);