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

Merge tag 'iommu-updates-v5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu

Pull iommu updates from Joerg Roedel:

- Big cleanup of almost unsused parts of the IOMMU API by Christoph
Hellwig. This mostly affects the Freescale PAMU driver.

- New IOMMU driver for Unisoc SOCs

- ARM SMMU Updates from Will:
- Drop vestigial PREFETCH_ADDR support (SMMUv3)
- Elide TLB sync logic for empty gather (SMMUv3)
- Fix "Service Failure Mode" handling (SMMUv3)
- New Qualcomm compatible string (SMMUv2)

- Removal of the AMD IOMMU performance counter writeable check on AMD.
It caused long boot delays on some machines and is only needed to
work around an errata on some older (possibly pre-production) chips.
If someone is still hit by this hardware issue anyway the performance
counters will just return 0.

- Support for targeted invalidations in the AMD IOMMU driver. Before
that the driver only invalidated a single 4k page or the whole IO/TLB
for an address space. This has been extended now and is mostly useful
for emulated AMD IOMMUs.

- Several fixes for the Shared Virtual Memory support in the Intel VT-d
driver

- Mediatek drivers can now be built as modules

- Re-introduction of the forcedac boot option which got lost when
converting the Intel VT-d driver to the common dma-iommu
implementation.

- Extension of the IOMMU device registration interface and support
iommu_ops to be const again when drivers are built as modules.

* tag 'iommu-updates-v5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (84 commits)
iommu: Streamline registration interface
iommu: Statically set module owner
iommu/mediatek-v1: Add error handle for mtk_iommu_probe
iommu/mediatek-v1: Avoid build fail when build as module
iommu/mediatek: Always enable the clk on resume
iommu/fsl-pamu: Fix uninitialized variable warning
iommu/vt-d: Force to flush iotlb before creating superpage
iommu/amd: Put newline after closing bracket in warning
iommu/vt-d: Fix an error handling path in 'intel_prepare_irq_remapping()'
iommu/vt-d: Fix build error of pasid_enable_wpe() with !X86
iommu/amd: Remove performance counter pre-initialization test
Revert "iommu/amd: Fix performance counter initialization"
iommu/amd: Remove duplicate check of devid
iommu/exynos: Remove unneeded local variable initialization
iommu/amd: Page-specific invalidations for more than one page
iommu/arm-smmu-v3: Remove the unused fields for PREFETCH_CONFIG command
iommu/vt-d: Avoid unnecessary cache flush in pasid entry teardown
iommu/vt-d: Invalidate PASID cache when root/context entry changed
iommu/vt-d: Remove WO permissions on second-level paging entries
iommu/vt-d: Report the right page fault address
...

+2131 -2256
+8 -7
Documentation/admin-guide/kernel-parameters.txt
··· 1877 1877 bypassed by not enabling DMAR with this option. In 1878 1878 this case, gfx device will use physical address for 1879 1879 DMA. 1880 - forcedac [X86-64] 1881 - With this option iommu will not optimize to look 1882 - for io virtual address below 32-bit forcing dual 1883 - address cycle on pci bus for cards supporting greater 1884 - than 32-bit addressing. The default is to look 1885 - for translation below 32-bit and if not available 1886 - then look in the higher range. 1887 1880 strict [Default Off] 1888 1881 With this option on every unmap_single operation will 1889 1882 result in a hardware IOTLB flush operation as opposed ··· 1964 1971 nopt [X86] 1965 1972 nobypass [PPC/POWERNV] 1966 1973 Disable IOMMU bypass, using IOMMU for PCI devices. 1974 + 1975 + iommu.forcedac= [ARM64, X86] Control IOVA allocation for PCI devices. 1976 + Format: { "0" | "1" } 1977 + 0 - Try to allocate a 32-bit DMA address first, before 1978 + falling back to the full range if needed. 1979 + 1 - Allocate directly from the full usable range, 1980 + forcing Dual Address Cycle for PCI cards supporting 1981 + greater than 32-bit addressing. 1967 1982 1968 1983 iommu.strict= [ARM64] Configure TLB invalidation behaviour 1969 1984 Format: { "0" | "1" }
+1
Documentation/devicetree/bindings/iommu/arm,smmu.yaml
··· 34 34 items: 35 35 - enum: 36 36 - qcom,sc7180-smmu-500 37 + - qcom,sc7280-smmu-500 37 38 - qcom,sc8180x-smmu-500 38 39 - qcom,sdm845-smmu-500 39 40 - qcom,sm8150-smmu-500
+57
Documentation/devicetree/bindings/iommu/sprd,iommu.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + # Copyright 2020 Unisoc Inc. 3 + %YAML 1.2 4 + --- 5 + $id: http://devicetree.org/schemas/iommu/sprd,iommu.yaml# 6 + $schema: http://devicetree.org/meta-schemas/core.yaml# 7 + 8 + title: Unisoc IOMMU and Multi-media MMU 9 + 10 + maintainers: 11 + - Chunyan Zhang <zhang.lyra@gmail.com> 12 + 13 + properties: 14 + compatible: 15 + enum: 16 + - sprd,iommu-v1 17 + 18 + "#iommu-cells": 19 + const: 0 20 + description: 21 + Unisoc IOMMUs are all single-master IOMMU devices, therefore no 22 + additional information needs to associate with its master device. 23 + Please refer to the generic bindings document for more details, 24 + Documentation/devicetree/bindings/iommu/iommu.txt 25 + 26 + reg: 27 + maxItems: 1 28 + 29 + clocks: 30 + description: 31 + Reference to a gate clock phandle, since access to some of IOMMUs are 32 + controlled by gate clock, but this is not required. 33 + 34 + required: 35 + - compatible 36 + - reg 37 + - "#iommu-cells" 38 + 39 + additionalProperties: false 40 + 41 + examples: 42 + - | 43 + iommu_disp: iommu@63000800 { 44 + compatible = "sprd,iommu-v1"; 45 + reg = <0x63000800 0x80>; 46 + #iommu-cells = <0>; 47 + }; 48 + 49 + - | 50 + iommu_jpg: iommu@62300300 { 51 + compatible = "sprd,iommu-v1"; 52 + reg = <0x62300300 0x80>; 53 + #iommu-cells = <0>; 54 + clocks = <&mm_gate 1>; 55 + }; 56 + 57 + ...
+3 -9
arch/powerpc/include/asm/fsl_pamu_stash.h
··· 7 7 #ifndef __FSL_PAMU_STASH_H 8 8 #define __FSL_PAMU_STASH_H 9 9 10 + struct iommu_domain; 11 + 10 12 /* cache stash targets */ 11 13 enum pamu_stash_target { 12 14 PAMU_ATTR_CACHE_L1 = 1, ··· 16 14 PAMU_ATTR_CACHE_L3, 17 15 }; 18 16 19 - /* 20 - * This attribute allows configuring stashig specific parameters 21 - * in the PAMU hardware. 22 - */ 23 - 24 - struct pamu_stash_attribute { 25 - u32 cpu; /* cpu number */ 26 - u32 cache; /* cache to stash to: L1,L2,L3 */ 27 - }; 17 + int fsl_pamu_configure_l1_stash(struct iommu_domain *domain, u32 cpu); 28 18 29 19 #endif /* __FSL_PAMU_STASH_H */
+1
arch/x86/events/amd/iommu.c
··· 14 14 #include <linux/init.h> 15 15 #include <linux/cpumask.h> 16 16 #include <linux/slab.h> 17 + #include <linux/amd-iommu.h> 17 18 18 19 #include "../perf_event.h" 19 20 #include "iommu.h"
-19
arch/x86/events/amd/iommu.h
··· 21 21 #define PC_MAX_SPEC_BNKS 64 22 22 #define PC_MAX_SPEC_CNTRS 16 23 23 24 - struct amd_iommu; 25 - 26 - /* amd_iommu_init.c external support functions */ 27 - extern int amd_iommu_get_num_iommus(void); 28 - 29 - extern bool amd_iommu_pc_supported(void); 30 - 31 - extern u8 amd_iommu_pc_get_max_banks(unsigned int idx); 32 - 33 - extern u8 amd_iommu_pc_get_max_counters(unsigned int idx); 34 - 35 - extern int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, 36 - u8 fxn, u64 *value); 37 - 38 - extern int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, 39 - u8 fxn, u64 *value); 40 - 41 - extern struct amd_iommu *get_amd_iommu(int idx); 42 - 43 24 #endif /*_PERF_EVENT_AMD_IOMMU_H_*/
+7 -6
drivers/acpi/arm64/iort.c
··· 968 968 static void iort_named_component_init(struct device *dev, 969 969 struct acpi_iort_node *node) 970 970 { 971 + struct property_entry props[2] = {}; 971 972 struct acpi_iort_named_component *nc; 972 - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 973 - 974 - if (!fwspec) 975 - return; 976 973 977 974 nc = (struct acpi_iort_named_component *)node->node_data; 978 - fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS, 979 - nc->node_flags); 975 + props[0] = PROPERTY_ENTRY_U32("pasid-num-bits", 976 + FIELD_GET(ACPI_IORT_NC_PASID_BITS, 977 + nc->node_flags)); 978 + 979 + if (device_add_properties(dev, props)) 980 + dev_warn(dev, "Could not add device properties\n"); 980 981 } 981 982 982 983 static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node)
-4
drivers/gpu/drm/amd/amdkfd/kfd_iommu.c
··· 333 333 return 0; 334 334 } 335 335 336 - extern bool amd_iommu_pc_supported(void); 337 - extern u8 amd_iommu_pc_get_max_banks(u16 devid); 338 - extern u8 amd_iommu_pc_get_max_counters(u16 devid); 339 - 340 336 /** kfd_iommu_add_perf_counters - Add IOMMU performance counters to topology 341 337 */ 342 338 int kfd_iommu_add_perf_counters(struct kfd_topology_device *kdev)
+1 -4
drivers/gpu/drm/msm/adreno/adreno_gpu.c
··· 188 188 189 189 void adreno_set_llc_attributes(struct iommu_domain *iommu) 190 190 { 191 - struct io_pgtable_domain_attr pgtbl_cfg; 192 - 193 - pgtbl_cfg.quirks = IO_PGTABLE_QUIRK_ARM_OUTER_WBWA; 194 - iommu_domain_set_attr(iommu, DOMAIN_ATTR_IO_PGTABLE_CFG, &pgtbl_cfg); 191 + iommu_set_pgtable_quirks(iommu, IO_PGTABLE_QUIRK_ARM_OUTER_WBWA); 195 192 } 196 193 197 194 struct msm_gem_address_space *
+14 -2
drivers/iommu/Kconfig
··· 349 349 is not implemented as it is not necessary for VFIO. 350 350 351 351 config MTK_IOMMU 352 - bool "MTK IOMMU Support" 352 + tristate "MediaTek IOMMU Support" 353 353 depends on ARCH_MEDIATEK || COMPILE_TEST 354 354 select ARM_DMA_USE_IOMMU 355 355 select IOMMU_API ··· 364 364 If unsure, say N here. 365 365 366 366 config MTK_IOMMU_V1 367 - bool "MTK IOMMU Version 1 (M4U gen1) Support" 367 + tristate "MediaTek IOMMU Version 1 (M4U gen1) Support" 368 368 depends on ARM 369 369 depends on ARCH_MEDIATEK || COMPILE_TEST 370 370 select ARM_DMA_USE_IOMMU ··· 407 407 Para-virtualised IOMMU driver with virtio. 408 408 409 409 Say Y here if you intend to run this kernel as a guest. 410 + 411 + config SPRD_IOMMU 412 + tristate "Unisoc IOMMU Support" 413 + depends on ARCH_SPRD || COMPILE_TEST 414 + select IOMMU_API 415 + help 416 + Support for IOMMU on Unisoc's SoCs, this IOMMU can be used by 417 + Unisoc's multimedia devices, such as display, Image codec(jpeg) 418 + and a few signal processors, including VSP(video), GSP(graphic), 419 + ISP(image), and CPP(camera pixel processor), etc. 420 + 421 + Say Y here if you want to use the multimedia devices listed above. 410 422 411 423 endif # IOMMU_SUPPORT
+2 -1
drivers/iommu/Makefile
··· 27 27 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o 28 28 obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o 29 29 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o 30 - obj-$(CONFIG_IOMMU_SVA_LIB) += iommu-sva-lib.o 30 + obj-$(CONFIG_IOMMU_SVA_LIB) += iommu-sva-lib.o io-pgfault.o 31 + obj-$(CONFIG_SPRD_IOMMU) += sprd-iommu.o
-2
drivers/iommu/amd/amd_iommu.h
··· 11 11 12 12 #include "amd_iommu_types.h" 13 13 14 - extern int amd_iommu_get_num_iommus(void); 15 14 extern int amd_iommu_init_dma_ops(void); 16 15 extern int amd_iommu_init_passthrough(void); 17 16 extern irqreturn_t amd_iommu_int_thread(int irq, void *data); ··· 64 65 extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid, 65 66 unsigned long cr3); 66 67 extern int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid); 67 - extern struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev); 68 68 69 69 #ifdef CONFIG_IRQ_REMAP 70 70 extern int amd_iommu_create_irq_domain(struct amd_iommu *iommu);
-1
drivers/iommu/amd/amd_iommu_types.h
··· 693 693 } ats; /* ATS state */ 694 694 bool pri_tlp; /* PASID TLB required for 695 695 PPR completions */ 696 - u32 errata; /* Bitmap for errata to apply */ 697 696 bool use_vapic; /* Enable device to use vapic mode */ 698 697 bool defer_attach; 699 698
+4 -55
drivers/iommu/amd/init.c
··· 12 12 #include <linux/acpi.h> 13 13 #include <linux/list.h> 14 14 #include <linux/bitmap.h> 15 - #include <linux/delay.h> 16 15 #include <linux/slab.h> 17 16 #include <linux/syscore_ops.h> 18 17 #include <linux/interrupt.h> ··· 207 208 * for a specific device. It is also indexed by the PCI device id. 208 209 */ 209 210 struct amd_iommu **amd_iommu_rlookup_table; 210 - EXPORT_SYMBOL(amd_iommu_rlookup_table); 211 211 212 212 /* 213 213 * This table is used to find the irq remapping table for a given device id ··· 255 257 static int amd_iommu_enable_interrupts(void); 256 258 static int __init iommu_go_to_state(enum iommu_init_state state); 257 259 static void init_device_table_dma(void); 258 - static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, 259 - u8 fxn, u64 *value, bool is_write); 260 260 261 261 static bool amd_iommu_pre_enabled = true; 262 262 ··· 264 268 { 265 269 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED); 266 270 } 267 - EXPORT_SYMBOL(translation_pre_enabled); 268 271 269 272 static void clear_translation_pre_enabled(struct amd_iommu *iommu) 270 273 { ··· 1712 1717 return 0; 1713 1718 } 1714 1719 1715 - static void __init init_iommu_perf_ctr(struct amd_iommu *iommu) 1720 + static void init_iommu_perf_ctr(struct amd_iommu *iommu) 1716 1721 { 1717 - int retry; 1722 + u64 val; 1718 1723 struct pci_dev *pdev = iommu->dev; 1719 - u64 val = 0xabcd, val2 = 0, save_reg, save_src; 1720 1724 1721 1725 if (!iommu_feature(iommu, FEATURE_PC)) 1722 1726 return; 1723 1727 1724 1728 amd_iommu_pc_present = true; 1725 - 1726 - /* save the value to restore, if writable */ 1727 - if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, false) || 1728 - iommu_pc_get_set_reg(iommu, 0, 0, 8, &save_src, false)) 1729 - goto pc_false; 1730 - 1731 - /* 1732 - * Disable power gating by programing the performance counter 1733 - * source to 20 (i.e. counts the reads and writes from/to IOMMU 1734 - * Reserved Register [MMIO Offset 1FF8h] that are ignored.), 1735 - * which never get incremented during this init phase. 1736 - * (Note: The event is also deprecated.) 1737 - */ 1738 - val = 20; 1739 - if (iommu_pc_get_set_reg(iommu, 0, 0, 8, &val, true)) 1740 - goto pc_false; 1741 - 1742 - /* Check if the performance counters can be written to */ 1743 - val = 0xabcd; 1744 - for (retry = 5; retry; retry--) { 1745 - if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true) || 1746 - iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false) || 1747 - val2) 1748 - break; 1749 - 1750 - /* Wait about 20 msec for power gating to disable and retry. */ 1751 - msleep(20); 1752 - } 1753 - 1754 - /* restore */ 1755 - if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, true) || 1756 - iommu_pc_get_set_reg(iommu, 0, 0, 8, &save_src, true)) 1757 - goto pc_false; 1758 - 1759 - if (val != val2) 1760 - goto pc_false; 1761 1729 1762 1730 pci_info(pdev, "IOMMU performance counters supported\n"); 1763 1731 ··· 1728 1770 iommu->max_banks = (u8) ((val >> 12) & 0x3f); 1729 1771 iommu->max_counters = (u8) ((val >> 7) & 0xf); 1730 1772 1731 - return; 1732 - 1733 - pc_false: 1734 - pci_err(pdev, "Unable to read/write to IOMMU perf counter.\n"); 1735 - amd_iommu_pc_present = false; 1736 1773 return; 1737 1774 } 1738 1775 ··· 1790 1837 * IVHD and MMIO conflict. 1791 1838 */ 1792 1839 if (features != iommu->features) 1793 - pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx\n).", 1840 + pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx).\n", 1794 1841 features, iommu->features); 1795 1842 } 1796 1843 ··· 1888 1935 1889 1936 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev, 1890 1937 amd_iommu_groups, "ivhd%d", iommu->index); 1891 - iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops); 1892 - iommu_device_register(&iommu->iommu); 1938 + iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL); 1893 1939 1894 1940 return pci_enable_device(iommu->dev); 1895 1941 } ··· 3229 3277 return iommu; 3230 3278 return NULL; 3231 3279 } 3232 - EXPORT_SYMBOL(get_amd_iommu); 3233 3280 3234 3281 /**************************************************************************** 3235 3282 * ··· 3310 3359 3311 3360 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false); 3312 3361 } 3313 - EXPORT_SYMBOL(amd_iommu_pc_get_reg); 3314 3362 3315 3363 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value) 3316 3364 { ··· 3318 3368 3319 3369 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true); 3320 3370 } 3321 - EXPORT_SYMBOL(amd_iommu_pc_set_reg);
+53 -160
drivers/iommu/amd/iommu.c
··· 290 290 return true; 291 291 } 292 292 293 - static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum) 294 - { 295 - struct iommu_dev_data *dev_data; 296 - 297 - dev_data = dev_iommu_priv_get(&pdev->dev); 298 - 299 - return dev_data->errata & (1 << erratum) ? true : false; 300 - } 301 - 302 293 /* 303 294 * This function checks if the driver got a valid device from the caller to 304 295 * avoid dereferencing invalid pointers. ··· 852 861 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY); 853 862 } 854 863 864 + /* 865 + * Builds an invalidation address which is suitable for one page or multiple 866 + * pages. Sets the size bit (S) as needed is more than one page is flushed. 867 + */ 868 + static inline u64 build_inv_address(u64 address, size_t size) 869 + { 870 + u64 pages, end, msb_diff; 871 + 872 + pages = iommu_num_pages(address, size, PAGE_SIZE); 873 + 874 + if (pages == 1) 875 + return address & PAGE_MASK; 876 + 877 + end = address + size - 1; 878 + 879 + /* 880 + * msb_diff would hold the index of the most significant bit that 881 + * flipped between the start and end. 882 + */ 883 + msb_diff = fls64(end ^ address) - 1; 884 + 885 + /* 886 + * Bits 63:52 are sign extended. If for some reason bit 51 is different 887 + * between the start and the end, invalidate everything. 888 + */ 889 + if (unlikely(msb_diff > 51)) { 890 + address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; 891 + } else { 892 + /* 893 + * The msb-bit must be clear on the address. Just set all the 894 + * lower bits. 895 + */ 896 + address |= 1ull << (msb_diff - 1); 897 + } 898 + 899 + /* Clear bits 11:0 */ 900 + address &= PAGE_MASK; 901 + 902 + /* Set the size bit - we flush more than one 4kb page */ 903 + return address | CMD_INV_IOMMU_PAGES_SIZE_MASK; 904 + } 905 + 855 906 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, 856 907 size_t size, u16 domid, int pde) 857 908 { 858 - u64 pages; 859 - bool s; 860 - 861 - pages = iommu_num_pages(address, size, PAGE_SIZE); 862 - s = false; 863 - 864 - if (pages > 1) { 865 - /* 866 - * If we have to flush more than one page, flush all 867 - * TLB entries for this domain 868 - */ 869 - address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; 870 - s = true; 871 - } 872 - 873 - address &= PAGE_MASK; 909 + u64 inv_address = build_inv_address(address, size); 874 910 875 911 memset(cmd, 0, sizeof(*cmd)); 876 912 cmd->data[1] |= domid; 877 - cmd->data[2] = lower_32_bits(address); 878 - cmd->data[3] = upper_32_bits(address); 913 + cmd->data[2] = lower_32_bits(inv_address); 914 + cmd->data[3] = upper_32_bits(inv_address); 879 915 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); 880 - if (s) /* size bit - we flush more than one 4kb page */ 881 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; 882 916 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */ 883 917 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; 884 918 } ··· 911 895 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, 912 896 u64 address, size_t size) 913 897 { 914 - u64 pages; 915 - bool s; 916 - 917 - pages = iommu_num_pages(address, size, PAGE_SIZE); 918 - s = false; 919 - 920 - if (pages > 1) { 921 - /* 922 - * If we have to flush more than one page, flush all 923 - * TLB entries for this domain 924 - */ 925 - address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; 926 - s = true; 927 - } 928 - 929 - address &= PAGE_MASK; 898 + u64 inv_address = build_inv_address(address, size); 930 899 931 900 memset(cmd, 0, sizeof(*cmd)); 932 901 cmd->data[0] = devid; 933 902 cmd->data[0] |= (qdep & 0xff) << 24; 934 903 cmd->data[1] = devid; 935 - cmd->data[2] = lower_32_bits(address); 936 - cmd->data[3] = upper_32_bits(address); 904 + cmd->data[2] = lower_32_bits(inv_address); 905 + cmd->data[3] = upper_32_bits(inv_address); 937 906 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); 938 - if (s) 939 - cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; 940 907 } 941 908 942 909 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid, ··· 1530 1531 pci_disable_pasid(pdev); 1531 1532 } 1532 1533 1533 - /* FIXME: Change generic reset-function to do the same */ 1534 - static int pri_reset_while_enabled(struct pci_dev *pdev) 1535 - { 1536 - u16 control; 1537 - int pos; 1538 - 1539 - pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); 1540 - if (!pos) 1541 - return -EINVAL; 1542 - 1543 - pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control); 1544 - control |= PCI_PRI_CTRL_RESET; 1545 - pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control); 1546 - 1547 - return 0; 1548 - } 1549 - 1550 1534 static int pdev_iommuv2_enable(struct pci_dev *pdev) 1551 1535 { 1552 - bool reset_enable; 1553 - int reqs, ret; 1554 - 1555 - /* FIXME: Hardcode number of outstanding requests for now */ 1556 - reqs = 32; 1557 - if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE)) 1558 - reqs = 1; 1559 - reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET); 1536 + int ret; 1560 1537 1561 1538 /* Only allow access to user-accessible pages */ 1562 1539 ret = pci_enable_pasid(pdev, 0); ··· 1545 1570 goto out_err; 1546 1571 1547 1572 /* Enable PRI */ 1548 - ret = pci_enable_pri(pdev, reqs); 1573 + /* FIXME: Hardcode number of outstanding requests for now */ 1574 + ret = pci_enable_pri(pdev, 32); 1549 1575 if (ret) 1550 1576 goto out_err; 1551 - 1552 - if (reset_enable) { 1553 - ret = pri_reset_while_enabled(pdev); 1554 - if (ret) 1555 - goto out_err; 1556 - } 1557 1577 1558 1578 ret = pci_enable_ats(pdev, PAGE_SHIFT); 1559 1579 if (ret) ··· 1685 1715 return ERR_PTR(-ENODEV); 1686 1716 1687 1717 devid = get_device_id(dev); 1688 - if (devid < 0) 1689 - return ERR_PTR(devid); 1690 - 1691 1718 iommu = amd_iommu_rlookup_table[devid]; 1692 1719 1693 1720 if (dev_iommu_priv_get(dev)) ··· 1736 1769 return pci_device_group(dev); 1737 1770 1738 1771 return acpihid_device_group(dev); 1739 - } 1740 - 1741 - static int amd_iommu_domain_get_attr(struct iommu_domain *domain, 1742 - enum iommu_attr attr, void *data) 1743 - { 1744 - switch (domain->type) { 1745 - case IOMMU_DOMAIN_UNMANAGED: 1746 - return -ENODEV; 1747 - case IOMMU_DOMAIN_DMA: 1748 - switch (attr) { 1749 - case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE: 1750 - *(int *)data = !amd_iommu_unmap_flush; 1751 - return 0; 1752 - default: 1753 - return -ENODEV; 1754 - } 1755 - break; 1756 - default: 1757 - return -EINVAL; 1758 - } 1759 1772 } 1760 1773 1761 1774 /***************************************************************************** ··· 1802 1855 pr_info("IO/TLB flush on unmap enabled\n"); 1803 1856 else 1804 1857 pr_info("Lazy IO/TLB flushing enabled\n"); 1805 - 1858 + iommu_set_dma_strict(amd_iommu_unmap_flush); 1806 1859 return 0; 1807 1860 1808 1861 } ··· 1966 2019 struct device *dev) 1967 2020 { 1968 2021 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 2022 + int devid = get_device_id(dev); 1969 2023 struct amd_iommu *iommu; 1970 - int devid; 1971 2024 1972 2025 if (!check_device(dev)) 1973 - return; 1974 - 1975 - devid = get_device_id(dev); 1976 - if (devid < 0) 1977 2026 return; 1978 2027 1979 2028 if (dev_data->domain != NULL) ··· 2200 2257 .release_device = amd_iommu_release_device, 2201 2258 .probe_finalize = amd_iommu_probe_finalize, 2202 2259 .device_group = amd_iommu_device_group, 2203 - .domain_get_attr = amd_iommu_domain_get_attr, 2204 2260 .get_resv_regions = amd_iommu_get_resv_regions, 2205 2261 .put_resv_regions = generic_iommu_put_resv_regions, 2206 2262 .is_attach_deferred = amd_iommu_is_attach_deferred, ··· 2251 2309 struct protection_domain *domain = to_pdomain(dom); 2252 2310 unsigned long flags; 2253 2311 int levels, ret; 2254 - 2255 - if (pasids <= 0 || pasids > (PASID_MASK + 1)) 2256 - return -EINVAL; 2257 2312 2258 2313 /* Number of GCR3 table levels required */ 2259 2314 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9) ··· 2501 2562 return iommu_queue_command(iommu, &cmd); 2502 2563 } 2503 2564 EXPORT_SYMBOL(amd_iommu_complete_ppr); 2504 - 2505 - struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev) 2506 - { 2507 - struct protection_domain *pdomain; 2508 - struct iommu_dev_data *dev_data; 2509 - struct device *dev = &pdev->dev; 2510 - struct iommu_domain *io_domain; 2511 - 2512 - if (!check_device(dev)) 2513 - return NULL; 2514 - 2515 - dev_data = dev_iommu_priv_get(&pdev->dev); 2516 - pdomain = dev_data->domain; 2517 - io_domain = iommu_get_domain_for_dev(dev); 2518 - 2519 - if (pdomain == NULL && dev_data->defer_attach) { 2520 - dev_data->defer_attach = false; 2521 - pdomain = to_pdomain(io_domain); 2522 - attach_device(dev, pdomain); 2523 - } 2524 - 2525 - if (pdomain == NULL) 2526 - return NULL; 2527 - 2528 - if (io_domain->type != IOMMU_DOMAIN_DMA) 2529 - return NULL; 2530 - 2531 - /* Only return IOMMUv2 domains */ 2532 - if (!(pdomain->flags & PD_IOMMUV2_MASK)) 2533 - return NULL; 2534 - 2535 - return &pdomain->domain; 2536 - } 2537 - EXPORT_SYMBOL(amd_iommu_get_v2_domain); 2538 - 2539 - void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum) 2540 - { 2541 - struct iommu_dev_data *dev_data; 2542 - 2543 - if (!amd_iommu_v2_supported()) 2544 - return; 2545 - 2546 - dev_data = dev_iommu_priv_get(&pdev->dev); 2547 - dev_data->errata |= (1 << erratum); 2548 - } 2549 - EXPORT_SYMBOL(amd_iommu_enable_device_erratum); 2550 2565 2551 2566 int amd_iommu_device_info(struct pci_dev *pdev, 2552 2567 struct amd_iommu_device_info *info)
+145 -102
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
··· 245 245 break; 246 246 case CMDQ_OP_PREFETCH_CFG: 247 247 cmd[0] |= FIELD_PREP(CMDQ_PREFETCH_0_SID, ent->prefetch.sid); 248 - cmd[1] |= FIELD_PREP(CMDQ_PREFETCH_1_SIZE, ent->prefetch.size); 249 - cmd[1] |= ent->prefetch.addr & CMDQ_PREFETCH_1_ADDR_MASK; 250 248 break; 251 249 case CMDQ_OP_CFGI_CD: 252 250 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SSID, ent->cfgi.ssid); ··· 907 909 908 910 spin_lock_irqsave(&smmu_domain->devices_lock, flags); 909 911 list_for_each_entry(master, &smmu_domain->devices, domain_head) { 910 - for (i = 0; i < master->num_sids; i++) { 911 - cmd.cfgi.sid = master->sids[i]; 912 + for (i = 0; i < master->num_streams; i++) { 913 + cmd.cfgi.sid = master->streams[i].id; 912 914 arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); 913 915 } 914 916 } ··· 1353 1355 return 0; 1354 1356 } 1355 1357 1358 + __maybe_unused 1359 + static struct arm_smmu_master * 1360 + arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid) 1361 + { 1362 + struct rb_node *node; 1363 + struct arm_smmu_stream *stream; 1364 + 1365 + lockdep_assert_held(&smmu->streams_mutex); 1366 + 1367 + node = smmu->streams.rb_node; 1368 + while (node) { 1369 + stream = rb_entry(node, struct arm_smmu_stream, node); 1370 + if (stream->id < sid) 1371 + node = node->rb_right; 1372 + else if (stream->id > sid) 1373 + node = node->rb_left; 1374 + else 1375 + return stream->master; 1376 + } 1377 + 1378 + return NULL; 1379 + } 1380 + 1356 1381 /* IRQ and event handlers */ 1357 1382 static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev) 1358 1383 { ··· 1609 1588 1610 1589 arm_smmu_atc_inv_to_cmd(0, 0, 0, &cmd); 1611 1590 1612 - for (i = 0; i < master->num_sids; i++) { 1613 - cmd.atc.sid = master->sids[i]; 1591 + for (i = 0; i < master->num_streams; i++) { 1592 + cmd.atc.sid = master->streams[i].id; 1614 1593 arm_smmu_cmdq_issue_cmd(master->smmu, &cmd); 1615 1594 } 1616 1595 ··· 1653 1632 if (!master->ats_enabled) 1654 1633 continue; 1655 1634 1656 - for (i = 0; i < master->num_sids; i++) { 1657 - cmd.atc.sid = master->sids[i]; 1635 + for (i = 0; i < master->num_streams; i++) { 1636 + cmd.atc.sid = master->streams[i].id; 1658 1637 arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd); 1659 1638 } 1660 1639 } ··· 2038 2017 .iommu_dev = smmu->dev, 2039 2018 }; 2040 2019 2041 - if (smmu_domain->non_strict) 2020 + if (!iommu_get_dma_strict(domain)) 2042 2021 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT; 2043 2022 2044 2023 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain); ··· 2086 2065 int i, j; 2087 2066 struct arm_smmu_device *smmu = master->smmu; 2088 2067 2089 - for (i = 0; i < master->num_sids; ++i) { 2090 - u32 sid = master->sids[i]; 2068 + for (i = 0; i < master->num_streams; ++i) { 2069 + u32 sid = master->streams[i].id; 2091 2070 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid); 2092 2071 2093 2072 /* Bridged PCI devices may end up with duplicated IDs */ 2094 2073 for (j = 0; j < i; j++) 2095 - if (master->sids[j] == sid) 2074 + if (master->streams[j].id == sid) 2096 2075 break; 2097 2076 if (j < i) 2098 2077 continue; ··· 2326 2305 { 2327 2306 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 2328 2307 2308 + if (!gather->pgsize) 2309 + return; 2310 + 2329 2311 arm_smmu_tlb_inv_range_domain(gather->start, 2330 2312 gather->end - gather->start + 1, 2331 2313 gather->pgsize, true, smmu_domain); ··· 2369 2345 return sid < limit; 2370 2346 } 2371 2347 2348 + static int arm_smmu_insert_master(struct arm_smmu_device *smmu, 2349 + struct arm_smmu_master *master) 2350 + { 2351 + int i; 2352 + int ret = 0; 2353 + struct arm_smmu_stream *new_stream, *cur_stream; 2354 + struct rb_node **new_node, *parent_node = NULL; 2355 + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); 2356 + 2357 + master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams), 2358 + GFP_KERNEL); 2359 + if (!master->streams) 2360 + return -ENOMEM; 2361 + master->num_streams = fwspec->num_ids; 2362 + 2363 + mutex_lock(&smmu->streams_mutex); 2364 + for (i = 0; i < fwspec->num_ids; i++) { 2365 + u32 sid = fwspec->ids[i]; 2366 + 2367 + new_stream = &master->streams[i]; 2368 + new_stream->id = sid; 2369 + new_stream->master = master; 2370 + 2371 + /* 2372 + * Check the SIDs are in range of the SMMU and our stream table 2373 + */ 2374 + if (!arm_smmu_sid_in_range(smmu, sid)) { 2375 + ret = -ERANGE; 2376 + break; 2377 + } 2378 + 2379 + /* Ensure l2 strtab is initialised */ 2380 + if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { 2381 + ret = arm_smmu_init_l2_strtab(smmu, sid); 2382 + if (ret) 2383 + break; 2384 + } 2385 + 2386 + /* Insert into SID tree */ 2387 + new_node = &(smmu->streams.rb_node); 2388 + while (*new_node) { 2389 + cur_stream = rb_entry(*new_node, struct arm_smmu_stream, 2390 + node); 2391 + parent_node = *new_node; 2392 + if (cur_stream->id > new_stream->id) { 2393 + new_node = &((*new_node)->rb_left); 2394 + } else if (cur_stream->id < new_stream->id) { 2395 + new_node = &((*new_node)->rb_right); 2396 + } else { 2397 + dev_warn(master->dev, 2398 + "stream %u already in tree\n", 2399 + cur_stream->id); 2400 + ret = -EINVAL; 2401 + break; 2402 + } 2403 + } 2404 + if (ret) 2405 + break; 2406 + 2407 + rb_link_node(&new_stream->node, parent_node, new_node); 2408 + rb_insert_color(&new_stream->node, &smmu->streams); 2409 + } 2410 + 2411 + if (ret) { 2412 + for (i--; i >= 0; i--) 2413 + rb_erase(&master->streams[i].node, &smmu->streams); 2414 + kfree(master->streams); 2415 + } 2416 + mutex_unlock(&smmu->streams_mutex); 2417 + 2418 + return ret; 2419 + } 2420 + 2421 + static void arm_smmu_remove_master(struct arm_smmu_master *master) 2422 + { 2423 + int i; 2424 + struct arm_smmu_device *smmu = master->smmu; 2425 + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); 2426 + 2427 + if (!smmu || !master->streams) 2428 + return; 2429 + 2430 + mutex_lock(&smmu->streams_mutex); 2431 + for (i = 0; i < fwspec->num_ids; i++) 2432 + rb_erase(&master->streams[i].node, &smmu->streams); 2433 + mutex_unlock(&smmu->streams_mutex); 2434 + 2435 + kfree(master->streams); 2436 + } 2437 + 2372 2438 static struct iommu_ops arm_smmu_ops; 2373 2439 2374 2440 static struct iommu_device *arm_smmu_probe_device(struct device *dev) 2375 2441 { 2376 - int i, ret; 2442 + int ret; 2377 2443 struct arm_smmu_device *smmu; 2378 2444 struct arm_smmu_master *master; 2379 2445 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); ··· 2484 2370 2485 2371 master->dev = dev; 2486 2372 master->smmu = smmu; 2487 - master->sids = fwspec->ids; 2488 - master->num_sids = fwspec->num_ids; 2489 2373 INIT_LIST_HEAD(&master->bonds); 2490 2374 dev_iommu_priv_set(dev, master); 2491 2375 2492 - /* Check the SIDs are in range of the SMMU and our stream table */ 2493 - for (i = 0; i < master->num_sids; i++) { 2494 - u32 sid = master->sids[i]; 2376 + ret = arm_smmu_insert_master(smmu, master); 2377 + if (ret) 2378 + goto err_free_master; 2495 2379 2496 - if (!arm_smmu_sid_in_range(smmu, sid)) { 2497 - ret = -ERANGE; 2498 - goto err_free_master; 2499 - } 2500 - 2501 - /* Ensure l2 strtab is initialised */ 2502 - if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { 2503 - ret = arm_smmu_init_l2_strtab(smmu, sid); 2504 - if (ret) 2505 - goto err_free_master; 2506 - } 2507 - } 2508 - 2509 - master->ssid_bits = min(smmu->ssid_bits, fwspec->num_pasid_bits); 2380 + device_property_read_u32(dev, "pasid-num-bits", &master->ssid_bits); 2381 + master->ssid_bits = min(smmu->ssid_bits, master->ssid_bits); 2510 2382 2511 2383 /* 2512 2384 * Note that PASID must be enabled before, and disabled after ATS: ··· 2528 2428 WARN_ON(arm_smmu_master_sva_enabled(master)); 2529 2429 arm_smmu_detach_dev(master); 2530 2430 arm_smmu_disable_pasid(master); 2431 + arm_smmu_remove_master(master); 2531 2432 kfree(master); 2532 2433 iommu_fwspec_free(dev); 2533 2434 } ··· 2550 2449 return group; 2551 2450 } 2552 2451 2553 - static int arm_smmu_domain_get_attr(struct iommu_domain *domain, 2554 - enum iommu_attr attr, void *data) 2452 + static int arm_smmu_enable_nesting(struct iommu_domain *domain) 2555 2453 { 2556 2454 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 2557 - 2558 - switch (domain->type) { 2559 - case IOMMU_DOMAIN_UNMANAGED: 2560 - switch (attr) { 2561 - case DOMAIN_ATTR_NESTING: 2562 - *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED); 2563 - return 0; 2564 - default: 2565 - return -ENODEV; 2566 - } 2567 - break; 2568 - case IOMMU_DOMAIN_DMA: 2569 - switch (attr) { 2570 - case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE: 2571 - *(int *)data = smmu_domain->non_strict; 2572 - return 0; 2573 - default: 2574 - return -ENODEV; 2575 - } 2576 - break; 2577 - default: 2578 - return -EINVAL; 2579 - } 2580 - } 2581 - 2582 - static int arm_smmu_domain_set_attr(struct iommu_domain *domain, 2583 - enum iommu_attr attr, void *data) 2584 - { 2585 2455 int ret = 0; 2586 - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 2587 2456 2588 2457 mutex_lock(&smmu_domain->init_mutex); 2589 - 2590 - switch (domain->type) { 2591 - case IOMMU_DOMAIN_UNMANAGED: 2592 - switch (attr) { 2593 - case DOMAIN_ATTR_NESTING: 2594 - if (smmu_domain->smmu) { 2595 - ret = -EPERM; 2596 - goto out_unlock; 2597 - } 2598 - 2599 - if (*(int *)data) 2600 - smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; 2601 - else 2602 - smmu_domain->stage = ARM_SMMU_DOMAIN_S1; 2603 - break; 2604 - default: 2605 - ret = -ENODEV; 2606 - } 2607 - break; 2608 - case IOMMU_DOMAIN_DMA: 2609 - switch(attr) { 2610 - case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE: 2611 - smmu_domain->non_strict = *(int *)data; 2612 - break; 2613 - default: 2614 - ret = -ENODEV; 2615 - } 2616 - break; 2617 - default: 2618 - ret = -EINVAL; 2619 - } 2620 - 2621 - out_unlock: 2458 + if (smmu_domain->smmu) 2459 + ret = -EPERM; 2460 + else 2461 + smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; 2622 2462 mutex_unlock(&smmu_domain->init_mutex); 2463 + 2623 2464 return ret; 2624 2465 } 2625 2466 ··· 2662 2619 .probe_device = arm_smmu_probe_device, 2663 2620 .release_device = arm_smmu_release_device, 2664 2621 .device_group = arm_smmu_device_group, 2665 - .domain_get_attr = arm_smmu_domain_get_attr, 2666 - .domain_set_attr = arm_smmu_domain_set_attr, 2622 + .enable_nesting = arm_smmu_enable_nesting, 2667 2623 .of_xlate = arm_smmu_of_xlate, 2668 2624 .get_resv_regions = arm_smmu_get_resv_regions, 2669 2625 .put_resv_regions = generic_iommu_put_resv_regions, ··· 2674 2632 .sva_unbind = arm_smmu_sva_unbind, 2675 2633 .sva_get_pasid = arm_smmu_sva_get_pasid, 2676 2634 .pgsize_bitmap = -1UL, /* Restricted during device attach */ 2635 + .owner = THIS_MODULE, 2677 2636 }; 2678 2637 2679 2638 /* Probing and initialisation functions */ ··· 2893 2850 static int arm_smmu_init_structures(struct arm_smmu_device *smmu) 2894 2851 { 2895 2852 int ret; 2853 + 2854 + mutex_init(&smmu->streams_mutex); 2855 + smmu->streams = RB_ROOT; 2896 2856 2897 2857 ret = arm_smmu_init_queues(smmu); 2898 2858 if (ret) ··· 3666 3620 if (ret) 3667 3621 return ret; 3668 3622 3669 - iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops); 3670 - iommu_device_set_fwnode(&smmu->iommu, dev->fwnode); 3671 - 3672 - ret = iommu_device_register(&smmu->iommu); 3623 + ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); 3673 3624 if (ret) { 3674 3625 dev_err(dev, "Failed to register iommu\n"); 3675 3626 return ret;
+12 -6
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
··· 115 115 #define GERROR_PRIQ_ABT_ERR (1 << 3) 116 116 #define GERROR_EVTQ_ABT_ERR (1 << 2) 117 117 #define GERROR_CMDQ_ERR (1 << 0) 118 - #define GERROR_ERR_MASK 0xfd 118 + #define GERROR_ERR_MASK 0x1fd 119 119 120 120 #define ARM_SMMU_GERRORN 0x64 121 121 ··· 410 410 #define CMDQ_OP_PREFETCH_CFG 0x1 411 411 struct { 412 412 u32 sid; 413 - u8 size; 414 - u64 addr; 415 413 } prefetch; 416 414 417 415 #define CMDQ_OP_CFGI_STE 0x3 ··· 637 639 638 640 /* IOMMU core code handle */ 639 641 struct iommu_device iommu; 642 + 643 + struct rb_root streams; 644 + struct mutex streams_mutex; 645 + }; 646 + 647 + struct arm_smmu_stream { 648 + u32 id; 649 + struct arm_smmu_master *master; 650 + struct rb_node node; 640 651 }; 641 652 642 653 /* SMMU private data for each master */ ··· 654 647 struct device *dev; 655 648 struct arm_smmu_domain *domain; 656 649 struct list_head domain_head; 657 - u32 *sids; 658 - unsigned int num_sids; 650 + struct arm_smmu_stream *streams; 651 + unsigned int num_streams; 659 652 bool ats_enabled; 660 653 bool sva_enabled; 661 654 struct list_head bonds; ··· 675 668 struct mutex init_mutex; /* Protects smmu pointer */ 676 669 677 670 struct io_pgtable_ops *pgtbl_ops; 678 - bool non_strict; 679 671 atomic_t nr_ats_masters; 680 672 681 673 enum arm_smmu_domain_stage stage;
+31 -94
drivers/iommu/arm/arm-smmu/arm-smmu.c
··· 761 761 .iommu_dev = smmu->dev, 762 762 }; 763 763 764 + if (!iommu_get_dma_strict(domain)) 765 + pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT; 766 + 764 767 if (smmu->impl && smmu->impl->init_context) { 765 768 ret = smmu->impl->init_context(smmu_domain, &pgtbl_cfg, dev); 766 769 if (ret) 767 770 goto out_clear_smmu; 768 771 } 769 772 770 - if (smmu_domain->pgtbl_cfg.quirks) 771 - pgtbl_cfg.quirks |= smmu_domain->pgtbl_cfg.quirks; 773 + if (smmu_domain->pgtbl_quirks) 774 + pgtbl_cfg.quirks |= smmu_domain->pgtbl_quirks; 772 775 773 776 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain); 774 777 if (!pgtbl_ops) { ··· 1484 1481 return group; 1485 1482 } 1486 1483 1487 - static int arm_smmu_domain_get_attr(struct iommu_domain *domain, 1488 - enum iommu_attr attr, void *data) 1484 + static int arm_smmu_enable_nesting(struct iommu_domain *domain) 1489 1485 { 1490 1486 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 1491 - 1492 - switch(domain->type) { 1493 - case IOMMU_DOMAIN_UNMANAGED: 1494 - switch (attr) { 1495 - case DOMAIN_ATTR_NESTING: 1496 - *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED); 1497 - return 0; 1498 - case DOMAIN_ATTR_IO_PGTABLE_CFG: { 1499 - struct io_pgtable_domain_attr *pgtbl_cfg = data; 1500 - *pgtbl_cfg = smmu_domain->pgtbl_cfg; 1501 - 1502 - return 0; 1503 - } 1504 - default: 1505 - return -ENODEV; 1506 - } 1507 - break; 1508 - case IOMMU_DOMAIN_DMA: 1509 - switch (attr) { 1510 - case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE: { 1511 - bool non_strict = smmu_domain->pgtbl_cfg.quirks & 1512 - IO_PGTABLE_QUIRK_NON_STRICT; 1513 - *(int *)data = non_strict; 1514 - return 0; 1515 - } 1516 - default: 1517 - return -ENODEV; 1518 - } 1519 - break; 1520 - default: 1521 - return -EINVAL; 1522 - } 1523 - } 1524 - 1525 - static int arm_smmu_domain_set_attr(struct iommu_domain *domain, 1526 - enum iommu_attr attr, void *data) 1527 - { 1528 1487 int ret = 0; 1529 - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 1530 1488 1531 1489 mutex_lock(&smmu_domain->init_mutex); 1532 - 1533 - switch(domain->type) { 1534 - case IOMMU_DOMAIN_UNMANAGED: 1535 - switch (attr) { 1536 - case DOMAIN_ATTR_NESTING: 1537 - if (smmu_domain->smmu) { 1538 - ret = -EPERM; 1539 - goto out_unlock; 1540 - } 1541 - 1542 - if (*(int *)data) 1543 - smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; 1544 - else 1545 - smmu_domain->stage = ARM_SMMU_DOMAIN_S1; 1546 - break; 1547 - case DOMAIN_ATTR_IO_PGTABLE_CFG: { 1548 - struct io_pgtable_domain_attr *pgtbl_cfg = data; 1549 - 1550 - if (smmu_domain->smmu) { 1551 - ret = -EPERM; 1552 - goto out_unlock; 1553 - } 1554 - 1555 - smmu_domain->pgtbl_cfg = *pgtbl_cfg; 1556 - break; 1557 - } 1558 - default: 1559 - ret = -ENODEV; 1560 - } 1561 - break; 1562 - case IOMMU_DOMAIN_DMA: 1563 - switch (attr) { 1564 - case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE: 1565 - if (*(int *)data) 1566 - smmu_domain->pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT; 1567 - else 1568 - smmu_domain->pgtbl_cfg.quirks &= ~IO_PGTABLE_QUIRK_NON_STRICT; 1569 - break; 1570 - default: 1571 - ret = -ENODEV; 1572 - } 1573 - break; 1574 - default: 1575 - ret = -EINVAL; 1576 - } 1577 - out_unlock: 1490 + if (smmu_domain->smmu) 1491 + ret = -EPERM; 1492 + else 1493 + smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; 1578 1494 mutex_unlock(&smmu_domain->init_mutex); 1495 + 1496 + return ret; 1497 + } 1498 + 1499 + static int arm_smmu_set_pgtable_quirks(struct iommu_domain *domain, 1500 + unsigned long quirks) 1501 + { 1502 + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 1503 + int ret = 0; 1504 + 1505 + mutex_lock(&smmu_domain->init_mutex); 1506 + if (smmu_domain->smmu) 1507 + ret = -EPERM; 1508 + else 1509 + smmu_domain->pgtbl_quirks = quirks; 1510 + mutex_unlock(&smmu_domain->init_mutex); 1511 + 1579 1512 return ret; 1580 1513 } 1581 1514 ··· 1570 1631 .probe_device = arm_smmu_probe_device, 1571 1632 .release_device = arm_smmu_release_device, 1572 1633 .device_group = arm_smmu_device_group, 1573 - .domain_get_attr = arm_smmu_domain_get_attr, 1574 - .domain_set_attr = arm_smmu_domain_set_attr, 1634 + .enable_nesting = arm_smmu_enable_nesting, 1635 + .set_pgtable_quirks = arm_smmu_set_pgtable_quirks, 1575 1636 .of_xlate = arm_smmu_of_xlate, 1576 1637 .get_resv_regions = arm_smmu_get_resv_regions, 1577 1638 .put_resv_regions = generic_iommu_put_resv_regions, 1578 1639 .def_domain_type = arm_smmu_def_domain_type, 1579 1640 .pgsize_bitmap = -1UL, /* Restricted during device attach */ 1641 + .owner = THIS_MODULE, 1580 1642 }; 1581 1643 1582 1644 static void arm_smmu_device_reset(struct arm_smmu_device *smmu) ··· 2161 2221 return err; 2162 2222 } 2163 2223 2164 - iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops); 2165 - iommu_device_set_fwnode(&smmu->iommu, dev->fwnode); 2166 - 2167 - err = iommu_device_register(&smmu->iommu); 2224 + err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); 2168 2225 if (err) { 2169 2226 dev_err(dev, "Failed to register iommu\n"); 2170 2227 return err;
+1 -1
drivers/iommu/arm/arm-smmu/arm-smmu.h
··· 364 364 struct arm_smmu_domain { 365 365 struct arm_smmu_device *smmu; 366 366 struct io_pgtable_ops *pgtbl_ops; 367 - struct io_pgtable_domain_attr pgtbl_cfg; 367 + unsigned long pgtbl_quirks; 368 368 const struct iommu_flush_ops *flush_ops; 369 369 struct arm_smmu_cfg cfg; 370 370 enum arm_smmu_domain_stage stage;
+1 -4
drivers/iommu/arm/arm-smmu/qcom_iommu.c
··· 847 847 return ret; 848 848 } 849 849 850 - iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops); 851 - iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode); 852 - 853 - ret = iommu_device_register(&qcom_iommu->iommu); 850 + ret = iommu_device_register(&qcom_iommu->iommu, &qcom_iommu_ops, dev); 854 851 if (ret) { 855 852 dev_err(dev, "Failed to register iommu\n"); 856 853 return ret;
+10 -13
drivers/iommu/dma-iommu.c
··· 52 52 }; 53 53 54 54 static DEFINE_STATIC_KEY_FALSE(iommu_deferred_attach_enabled); 55 + bool iommu_dma_forcedac __read_mostly; 55 56 56 - void iommu_dma_free_cpu_cached_iovas(unsigned int cpu, 57 - struct iommu_domain *domain) 57 + static int __init iommu_dma_forcedac_setup(char *str) 58 58 { 59 - struct iommu_dma_cookie *cookie = domain->iova_cookie; 60 - struct iova_domain *iovad = &cookie->iovad; 59 + int ret = kstrtobool(str, &iommu_dma_forcedac); 61 60 62 - free_cpu_cached_iovas(cpu, iovad); 61 + if (!ret && iommu_dma_forcedac) 62 + pr_info("Forcing DAC for PCI devices\n"); 63 + return ret; 63 64 } 65 + early_param("iommu.forcedac", iommu_dma_forcedac_setup); 64 66 65 67 static void iommu_dma_entry_dtor(unsigned long data) 66 68 { ··· 306 304 307 305 cookie = container_of(iovad, struct iommu_dma_cookie, iovad); 308 306 domain = cookie->fq_domain; 309 - /* 310 - * The IOMMU driver supporting DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE 311 - * implies that ops->flush_iotlb_all must be non-NULL. 312 - */ 307 + 313 308 domain->ops->flush_iotlb_all(domain); 314 309 } 315 310 ··· 333 334 struct iommu_dma_cookie *cookie = domain->iova_cookie; 334 335 unsigned long order, base_pfn; 335 336 struct iova_domain *iovad; 336 - int attr; 337 337 338 338 if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE) 339 339 return -EINVAL; ··· 369 371 init_iova_domain(iovad, 1UL << order, base_pfn); 370 372 371 373 if (!cookie->fq_domain && (!dev || !dev_is_untrusted(dev)) && 372 - !iommu_domain_get_attr(domain, DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, &attr) && 373 - attr) { 374 + domain->ops->flush_iotlb_all && !iommu_get_dma_strict(domain)) { 374 375 if (init_iova_flush_queue(iovad, iommu_dma_flush_iotlb_all, 375 376 iommu_dma_entry_dtor)) 376 377 pr_warn("iova flush queue initialization failed\n"); ··· 441 444 dma_limit = min(dma_limit, (u64)domain->geometry.aperture_end); 442 445 443 446 /* Try to get PCI devices a SAC address */ 444 - if (dma_limit > DMA_BIT_MASK(32) && dev_is_pci(dev)) 447 + if (dma_limit > DMA_BIT_MASK(32) && !iommu_dma_forcedac && dev_is_pci(dev)) 445 448 iova = alloc_iova_fast(iovad, iova_len, 446 449 DMA_BIT_MASK(32) >> shift, false); 447 450
+2 -5
drivers/iommu/exynos-iommu.c
··· 407 407 struct sysmmu_drvdata *data = dev_id; 408 408 const struct sysmmu_fault_info *finfo; 409 409 unsigned int i, n, itype; 410 - sysmmu_iova_t fault_addr = -1; 410 + sysmmu_iova_t fault_addr; 411 411 unsigned short reg_status, reg_clear; 412 412 int ret = -ENOSYS; 413 413 ··· 630 630 if (ret) 631 631 return ret; 632 632 633 - iommu_device_set_ops(&data->iommu, &exynos_iommu_ops); 634 - iommu_device_set_fwnode(&data->iommu, &dev->of_node->fwnode); 635 - 636 - ret = iommu_device_register(&data->iommu); 633 + ret = iommu_device_register(&data->iommu, &exynos_iommu_ops, dev); 637 634 if (ret) 638 635 return ret; 639 636
+11 -282
drivers/iommu/fsl_pamu.c
··· 63 63 /* maximum subwindows permitted per liodn */ 64 64 static u32 max_subwindow_count; 65 65 66 - /* Pool for fspi allocation */ 67 - static struct gen_pool *spaace_pool; 68 - 69 - /** 70 - * pamu_get_max_subwin_cnt() - Return the maximum supported 71 - * subwindow count per liodn. 72 - * 73 - */ 74 - u32 pamu_get_max_subwin_cnt(void) 75 - { 76 - return max_subwindow_count; 77 - } 78 - 79 66 /** 80 67 * pamu_get_ppaace() - Return the primary PACCE 81 68 * @liodn: liodn PAACT index for desired PAACE ··· 142 155 return fls64(addrspace_size) - 2; 143 156 } 144 157 145 - /* Derive the PAACE window count encoding for the subwindow count */ 146 - static unsigned int map_subwindow_cnt_to_wce(u32 subwindow_cnt) 147 - { 148 - /* window count is 2^(WCE+1) bytes */ 149 - return __ffs(subwindow_cnt) - 1; 150 - } 151 - 152 158 /* 153 159 * Set the PAACE type as primary and set the coherency required domain 154 160 * attribute ··· 155 175 } 156 176 157 177 /* 158 - * Set the PAACE type as secondary and set the coherency required domain 159 - * attribute. 160 - */ 161 - static void pamu_init_spaace(struct paace *spaace) 162 - { 163 - set_bf(spaace->addr_bitfields, PAACE_AF_PT, PAACE_PT_SECONDARY); 164 - set_bf(spaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR, 165 - PAACE_M_COHERENCE_REQ); 166 - } 167 - 168 - /* 169 - * Return the spaace (corresponding to the secondary window index) 170 - * for a particular ppaace. 171 - */ 172 - static struct paace *pamu_get_spaace(struct paace *paace, u32 wnum) 173 - { 174 - u32 subwin_cnt; 175 - struct paace *spaace = NULL; 176 - 177 - subwin_cnt = 1UL << (get_bf(paace->impl_attr, PAACE_IA_WCE) + 1); 178 - 179 - if (wnum < subwin_cnt) 180 - spaace = &spaact[paace->fspi + wnum]; 181 - else 182 - pr_debug("secondary paace out of bounds\n"); 183 - 184 - return spaace; 185 - } 186 - 187 - /** 188 - * pamu_get_fspi_and_allocate() - Allocates fspi index and reserves subwindows 189 - * required for primary PAACE in the secondary 190 - * PAACE table. 191 - * @subwin_cnt: Number of subwindows to be reserved. 192 - * 193 - * A PPAACE entry may have a number of associated subwindows. A subwindow 194 - * corresponds to a SPAACE entry in the SPAACT table. Each PAACE entry stores 195 - * the index (fspi) of the first SPAACE entry in the SPAACT table. This 196 - * function returns the index of the first SPAACE entry. The remaining 197 - * SPAACE entries are reserved contiguously from that index. 198 - * 199 - * Returns a valid fspi index in the range of 0 - SPAACE_NUMBER_ENTRIES on success. 200 - * If no SPAACE entry is available or the allocator can not reserve the required 201 - * number of contiguous entries function returns ULONG_MAX indicating a failure. 202 - * 203 - */ 204 - static unsigned long pamu_get_fspi_and_allocate(u32 subwin_cnt) 205 - { 206 - unsigned long spaace_addr; 207 - 208 - spaace_addr = gen_pool_alloc(spaace_pool, subwin_cnt * sizeof(struct paace)); 209 - if (!spaace_addr) 210 - return ULONG_MAX; 211 - 212 - return (spaace_addr - (unsigned long)spaact) / (sizeof(struct paace)); 213 - } 214 - 215 - /* Release the subwindows reserved for a particular LIODN */ 216 - void pamu_free_subwins(int liodn) 217 - { 218 - struct paace *ppaace; 219 - u32 subwin_cnt, size; 220 - 221 - ppaace = pamu_get_ppaace(liodn); 222 - if (!ppaace) { 223 - pr_debug("Invalid liodn entry\n"); 224 - return; 225 - } 226 - 227 - if (get_bf(ppaace->addr_bitfields, PPAACE_AF_MW)) { 228 - subwin_cnt = 1UL << (get_bf(ppaace->impl_attr, PAACE_IA_WCE) + 1); 229 - size = (subwin_cnt - 1) * sizeof(struct paace); 230 - gen_pool_free(spaace_pool, (unsigned long)&spaact[ppaace->fspi], size); 231 - set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0); 232 - } 233 - } 234 - 235 - /* 236 178 * Function used for updating stash destination for the coressponding 237 179 * LIODN. 238 180 */ 239 - int pamu_update_paace_stash(int liodn, u32 subwin, u32 value) 181 + int pamu_update_paace_stash(int liodn, u32 value) 240 182 { 241 183 struct paace *paace; 242 184 ··· 166 264 if (!paace) { 167 265 pr_debug("Invalid liodn entry\n"); 168 266 return -ENOENT; 169 - } 170 - if (subwin) { 171 - paace = pamu_get_spaace(paace, subwin - 1); 172 - if (!paace) 173 - return -ENOENT; 174 267 } 175 268 set_bf(paace->impl_attr, PAACE_IA_CID, value); 176 - 177 - mb(); 178 - 179 - return 0; 180 - } 181 - 182 - /* Disable a subwindow corresponding to the LIODN */ 183 - int pamu_disable_spaace(int liodn, u32 subwin) 184 - { 185 - struct paace *paace; 186 - 187 - paace = pamu_get_ppaace(liodn); 188 - if (!paace) { 189 - pr_debug("Invalid liodn entry\n"); 190 - return -ENOENT; 191 - } 192 - if (subwin) { 193 - paace = pamu_get_spaace(paace, subwin - 1); 194 - if (!paace) 195 - return -ENOENT; 196 - set_bf(paace->addr_bitfields, PAACE_AF_V, PAACE_V_INVALID); 197 - } else { 198 - set_bf(paace->addr_bitfields, PAACE_AF_AP, 199 - PAACE_AP_PERMS_DENIED); 200 - } 201 269 202 270 mb(); 203 271 ··· 178 306 * pamu_config_paace() - Sets up PPAACE entry for specified liodn 179 307 * 180 308 * @liodn: Logical IO device number 181 - * @win_addr: starting address of DSA window 182 - * @win-size: size of DSA window 183 309 * @omi: Operation mapping index -- if ~omi == 0 then omi not defined 184 - * @rpn: real (true physical) page number 185 310 * @stashid: cache stash id for associated cpu -- if ~stashid == 0 then 186 311 * stashid not defined 187 - * @snoopid: snoop id for hardware coherency -- if ~snoopid == 0 then 188 - * snoopid not defined 189 - * @subwin_cnt: number of sub-windows 190 312 * @prot: window permissions 191 313 * 192 314 * Returns 0 upon success else error code < 0 returned 193 315 */ 194 - int pamu_config_ppaace(int liodn, phys_addr_t win_addr, phys_addr_t win_size, 195 - u32 omi, unsigned long rpn, u32 snoopid, u32 stashid, 196 - u32 subwin_cnt, int prot) 316 + int pamu_config_ppaace(int liodn, u32 omi, u32 stashid, int prot) 197 317 { 198 318 struct paace *ppaace; 199 - unsigned long fspi; 200 - 201 - if ((win_size & (win_size - 1)) || win_size < PAMU_PAGE_SIZE) { 202 - pr_debug("window size too small or not a power of two %pa\n", 203 - &win_size); 204 - return -EINVAL; 205 - } 206 - 207 - if (win_addr & (win_size - 1)) { 208 - pr_debug("window address is not aligned with window size\n"); 209 - return -EINVAL; 210 - } 211 319 212 320 ppaace = pamu_get_ppaace(liodn); 213 321 if (!ppaace) ··· 195 343 196 344 /* window size is 2^(WSE+1) bytes */ 197 345 set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE, 198 - map_addrspace_size_to_wse(win_size)); 346 + map_addrspace_size_to_wse(1ULL << 36)); 199 347 200 348 pamu_init_ppaace(ppaace); 201 349 202 - ppaace->wbah = win_addr >> (PAMU_PAGE_SHIFT + 20); 203 - set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL, 204 - (win_addr >> PAMU_PAGE_SHIFT)); 350 + ppaace->wbah = 0; 351 + set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL, 0); 205 352 206 353 /* set up operation mapping if it's configured */ 207 354 if (omi < OME_NUMBER_ENTRIES) { ··· 215 364 if (~stashid != 0) 216 365 set_bf(ppaace->impl_attr, PAACE_IA_CID, stashid); 217 366 218 - /* configure snoop id */ 219 - if (~snoopid != 0) 220 - ppaace->domain_attr.to_host.snpid = snoopid; 221 - 222 - if (subwin_cnt) { 223 - /* The first entry is in the primary PAACE instead */ 224 - fspi = pamu_get_fspi_and_allocate(subwin_cnt - 1); 225 - if (fspi == ULONG_MAX) { 226 - pr_debug("spaace indexes exhausted\n"); 227 - return -EINVAL; 228 - } 229 - 230 - /* window count is 2^(WCE+1) bytes */ 231 - set_bf(ppaace->impl_attr, PAACE_IA_WCE, 232 - map_subwindow_cnt_to_wce(subwin_cnt)); 233 - set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0x1); 234 - ppaace->fspi = fspi; 235 - } else { 236 - set_bf(ppaace->impl_attr, PAACE_IA_ATM, PAACE_ATM_WINDOW_XLATE); 237 - ppaace->twbah = rpn >> 20; 238 - set_bf(ppaace->win_bitfields, PAACE_WIN_TWBAL, rpn); 239 - set_bf(ppaace->addr_bitfields, PAACE_AF_AP, prot); 240 - set_bf(ppaace->impl_attr, PAACE_IA_WCE, 0); 241 - set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0); 242 - } 243 - mb(); 244 - 245 - return 0; 246 - } 247 - 248 - /** 249 - * pamu_config_spaace() - Sets up SPAACE entry for specified subwindow 250 - * 251 - * @liodn: Logical IO device number 252 - * @subwin_cnt: number of sub-windows associated with dma-window 253 - * @subwin: subwindow index 254 - * @subwin_size: size of subwindow 255 - * @omi: Operation mapping index 256 - * @rpn: real (true physical) page number 257 - * @snoopid: snoop id for hardware coherency -- if ~snoopid == 0 then 258 - * snoopid not defined 259 - * @stashid: cache stash id for associated cpu 260 - * @enable: enable/disable subwindow after reconfiguration 261 - * @prot: sub window permissions 262 - * 263 - * Returns 0 upon success else error code < 0 returned 264 - */ 265 - int pamu_config_spaace(int liodn, u32 subwin_cnt, u32 subwin, 266 - phys_addr_t subwin_size, u32 omi, unsigned long rpn, 267 - u32 snoopid, u32 stashid, int enable, int prot) 268 - { 269 - struct paace *paace; 270 - 271 - /* setup sub-windows */ 272 - if (!subwin_cnt) { 273 - pr_debug("Invalid subwindow count\n"); 274 - return -EINVAL; 275 - } 276 - 277 - paace = pamu_get_ppaace(liodn); 278 - if (subwin > 0 && subwin < subwin_cnt && paace) { 279 - paace = pamu_get_spaace(paace, subwin - 1); 280 - 281 - if (paace && !(paace->addr_bitfields & PAACE_V_VALID)) { 282 - pamu_init_spaace(paace); 283 - set_bf(paace->addr_bitfields, SPAACE_AF_LIODN, liodn); 284 - } 285 - } 286 - 287 - if (!paace) { 288 - pr_debug("Invalid liodn entry\n"); 289 - return -ENOENT; 290 - } 291 - 292 - if ((subwin_size & (subwin_size - 1)) || subwin_size < PAMU_PAGE_SIZE) { 293 - pr_debug("subwindow size out of range, or not a power of 2\n"); 294 - return -EINVAL; 295 - } 296 - 297 - if (rpn == ULONG_MAX) { 298 - pr_debug("real page number out of range\n"); 299 - return -EINVAL; 300 - } 301 - 302 - /* window size is 2^(WSE+1) bytes */ 303 - set_bf(paace->win_bitfields, PAACE_WIN_SWSE, 304 - map_addrspace_size_to_wse(subwin_size)); 305 - 306 - set_bf(paace->impl_attr, PAACE_IA_ATM, PAACE_ATM_WINDOW_XLATE); 307 - paace->twbah = rpn >> 20; 308 - set_bf(paace->win_bitfields, PAACE_WIN_TWBAL, rpn); 309 - set_bf(paace->addr_bitfields, PAACE_AF_AP, prot); 310 - 311 - /* configure snoop id */ 312 - if (~snoopid != 0) 313 - paace->domain_attr.to_host.snpid = snoopid; 314 - 315 - /* set up operation mapping if it's configured */ 316 - if (omi < OME_NUMBER_ENTRIES) { 317 - set_bf(paace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED); 318 - paace->op_encode.index_ot.omi = omi; 319 - } else if (~omi != 0) { 320 - pr_debug("bad operation mapping index: %d\n", omi); 321 - return -EINVAL; 322 - } 323 - 324 - if (~stashid != 0) 325 - set_bf(paace->impl_attr, PAACE_IA_CID, stashid); 326 - 327 - smp_wmb(); 328 - 329 - if (enable) 330 - set_bf(paace->addr_bitfields, PAACE_AF_V, PAACE_V_VALID); 331 - 367 + set_bf(ppaace->impl_attr, PAACE_IA_ATM, PAACE_ATM_WINDOW_XLATE); 368 + ppaace->twbah = 0; 369 + set_bf(ppaace->win_bitfields, PAACE_WIN_TWBAL, 0); 370 + set_bf(ppaace->addr_bitfields, PAACE_AF_AP, prot); 371 + set_bf(ppaace->impl_attr, PAACE_IA_WCE, 0); 372 + set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0); 332 373 mb(); 333 374 334 375 return 0; ··· 872 1129 spaact_phys = virt_to_phys(spaact); 873 1130 omt_phys = virt_to_phys(omt); 874 1131 875 - spaace_pool = gen_pool_create(ilog2(sizeof(struct paace)), -1); 876 - if (!spaace_pool) { 877 - ret = -ENOMEM; 878 - dev_err(dev, "Failed to allocate spaace gen pool\n"); 879 - goto error; 880 - } 881 - 882 - ret = gen_pool_add(spaace_pool, (unsigned long)spaact, SPAACT_SIZE, -1); 883 - if (ret) 884 - goto error_genpool; 885 - 886 1132 pamubypenr = in_be32(&guts_regs->pamubypenr); 887 1133 888 1134 for (pamu_reg_off = 0, pamu_counter = 0x80000000; pamu_reg_off < size; ··· 898 1166 probed = true; 899 1167 900 1168 return 0; 901 - 902 - error_genpool: 903 - gen_pool_destroy(spaace_pool); 904 1169 905 1170 error: 906 1171 if (irq != NO_IRQ)
+2 -10
drivers/iommu/fsl_pamu.h
··· 383 383 int pamu_domain_init(void); 384 384 int pamu_enable_liodn(int liodn); 385 385 int pamu_disable_liodn(int liodn); 386 - void pamu_free_subwins(int liodn); 387 - int pamu_config_ppaace(int liodn, phys_addr_t win_addr, phys_addr_t win_size, 388 - u32 omi, unsigned long rpn, u32 snoopid, uint32_t stashid, 389 - u32 subwin_cnt, int prot); 390 - int pamu_config_spaace(int liodn, u32 subwin_cnt, u32 subwin_addr, 391 - phys_addr_t subwin_size, u32 omi, unsigned long rpn, 392 - uint32_t snoopid, u32 stashid, int enable, int prot); 386 + int pamu_config_ppaace(int liodn, u32 omi, uint32_t stashid, int prot); 393 387 394 388 u32 get_stash_id(u32 stash_dest_hint, u32 vcpu); 395 389 void get_ome_index(u32 *omi_index, struct device *dev); 396 - int pamu_update_paace_stash(int liodn, u32 subwin, u32 value); 397 - int pamu_disable_spaace(int liodn, u32 subwin); 398 - u32 pamu_get_max_subwin_cnt(void); 390 + int pamu_update_paace_stash(int liodn, u32 value); 399 391 400 392 #endif /* __FSL_PAMU_H */
+56 -637
drivers/iommu/fsl_pamu_domain.c
··· 54 54 return 0; 55 55 } 56 56 57 - static phys_addr_t get_phys_addr(struct fsl_dma_domain *dma_domain, dma_addr_t iova) 58 - { 59 - u32 win_cnt = dma_domain->win_cnt; 60 - struct dma_window *win_ptr = &dma_domain->win_arr[0]; 61 - struct iommu_domain_geometry *geom; 62 - 63 - geom = &dma_domain->iommu_domain.geometry; 64 - 65 - if (!win_cnt || !dma_domain->geom_size) { 66 - pr_debug("Number of windows/geometry not configured for the domain\n"); 67 - return 0; 68 - } 69 - 70 - if (win_cnt > 1) { 71 - u64 subwin_size; 72 - dma_addr_t subwin_iova; 73 - u32 wnd; 74 - 75 - subwin_size = dma_domain->geom_size >> ilog2(win_cnt); 76 - subwin_iova = iova & ~(subwin_size - 1); 77 - wnd = (subwin_iova - geom->aperture_start) >> ilog2(subwin_size); 78 - win_ptr = &dma_domain->win_arr[wnd]; 79 - } 80 - 81 - if (win_ptr->valid) 82 - return win_ptr->paddr + (iova & (win_ptr->size - 1)); 83 - 84 - return 0; 85 - } 86 - 87 - static int map_subwins(int liodn, struct fsl_dma_domain *dma_domain) 88 - { 89 - struct dma_window *sub_win_ptr = &dma_domain->win_arr[0]; 90 - int i, ret; 91 - unsigned long rpn, flags; 92 - 93 - for (i = 0; i < dma_domain->win_cnt; i++) { 94 - if (sub_win_ptr[i].valid) { 95 - rpn = sub_win_ptr[i].paddr >> PAMU_PAGE_SHIFT; 96 - spin_lock_irqsave(&iommu_lock, flags); 97 - ret = pamu_config_spaace(liodn, dma_domain->win_cnt, i, 98 - sub_win_ptr[i].size, 99 - ~(u32)0, 100 - rpn, 101 - dma_domain->snoop_id, 102 - dma_domain->stash_id, 103 - (i > 0) ? 1 : 0, 104 - sub_win_ptr[i].prot); 105 - spin_unlock_irqrestore(&iommu_lock, flags); 106 - if (ret) { 107 - pr_debug("SPAACE configuration failed for liodn %d\n", 108 - liodn); 109 - return ret; 110 - } 111 - } 112 - } 113 - 114 - return ret; 115 - } 116 - 117 - static int map_win(int liodn, struct fsl_dma_domain *dma_domain) 118 - { 119 - int ret; 120 - struct dma_window *wnd = &dma_domain->win_arr[0]; 121 - phys_addr_t wnd_addr = dma_domain->iommu_domain.geometry.aperture_start; 122 - unsigned long flags; 123 - 124 - spin_lock_irqsave(&iommu_lock, flags); 125 - ret = pamu_config_ppaace(liodn, wnd_addr, 126 - wnd->size, 127 - ~(u32)0, 128 - wnd->paddr >> PAMU_PAGE_SHIFT, 129 - dma_domain->snoop_id, dma_domain->stash_id, 130 - 0, wnd->prot); 131 - spin_unlock_irqrestore(&iommu_lock, flags); 132 - if (ret) 133 - pr_debug("PAACE configuration failed for liodn %d\n", liodn); 134 - 135 - return ret; 136 - } 137 - 138 - /* Map the DMA window corresponding to the LIODN */ 139 - static int map_liodn(int liodn, struct fsl_dma_domain *dma_domain) 140 - { 141 - if (dma_domain->win_cnt > 1) 142 - return map_subwins(liodn, dma_domain); 143 - else 144 - return map_win(liodn, dma_domain); 145 - } 146 - 147 - /* Update window/subwindow mapping for the LIODN */ 148 - static int update_liodn(int liodn, struct fsl_dma_domain *dma_domain, u32 wnd_nr) 149 - { 150 - int ret; 151 - struct dma_window *wnd = &dma_domain->win_arr[wnd_nr]; 152 - unsigned long flags; 153 - 154 - spin_lock_irqsave(&iommu_lock, flags); 155 - if (dma_domain->win_cnt > 1) { 156 - ret = pamu_config_spaace(liodn, dma_domain->win_cnt, wnd_nr, 157 - wnd->size, 158 - ~(u32)0, 159 - wnd->paddr >> PAMU_PAGE_SHIFT, 160 - dma_domain->snoop_id, 161 - dma_domain->stash_id, 162 - (wnd_nr > 0) ? 1 : 0, 163 - wnd->prot); 164 - if (ret) 165 - pr_debug("Subwindow reconfiguration failed for liodn %d\n", 166 - liodn); 167 - } else { 168 - phys_addr_t wnd_addr; 169 - 170 - wnd_addr = dma_domain->iommu_domain.geometry.aperture_start; 171 - 172 - ret = pamu_config_ppaace(liodn, wnd_addr, 173 - wnd->size, 174 - ~(u32)0, 175 - wnd->paddr >> PAMU_PAGE_SHIFT, 176 - dma_domain->snoop_id, dma_domain->stash_id, 177 - 0, wnd->prot); 178 - if (ret) 179 - pr_debug("Window reconfiguration failed for liodn %d\n", 180 - liodn); 181 - } 182 - 183 - spin_unlock_irqrestore(&iommu_lock, flags); 184 - 185 - return ret; 186 - } 187 - 188 57 static int update_liodn_stash(int liodn, struct fsl_dma_domain *dma_domain, 189 58 u32 val) 190 59 { 191 - int ret = 0, i; 60 + int ret = 0; 192 61 unsigned long flags; 193 62 194 63 spin_lock_irqsave(&iommu_lock, flags); 195 - if (!dma_domain->win_arr) { 196 - pr_debug("Windows not configured, stash destination update failed for liodn %d\n", 197 - liodn); 64 + ret = pamu_update_paace_stash(liodn, val); 65 + if (ret) { 66 + pr_debug("Failed to update SPAACE for liodn %d\n ", liodn); 198 67 spin_unlock_irqrestore(&iommu_lock, flags); 199 - return -EINVAL; 200 - } 201 - 202 - for (i = 0; i < dma_domain->win_cnt; i++) { 203 - ret = pamu_update_paace_stash(liodn, i, val); 204 - if (ret) { 205 - pr_debug("Failed to update SPAACE %d field for liodn %d\n ", 206 - i, liodn); 207 - spin_unlock_irqrestore(&iommu_lock, flags); 208 - return ret; 209 - } 68 + return ret; 210 69 } 211 70 212 71 spin_unlock_irqrestore(&iommu_lock, flags); ··· 74 215 } 75 216 76 217 /* Set the geometry parameters for a LIODN */ 77 - static int pamu_set_liodn(int liodn, struct device *dev, 78 - struct fsl_dma_domain *dma_domain, 79 - struct iommu_domain_geometry *geom_attr, 80 - u32 win_cnt) 218 + static int pamu_set_liodn(struct fsl_dma_domain *dma_domain, struct device *dev, 219 + int liodn) 81 220 { 82 - phys_addr_t window_addr, window_size; 83 - phys_addr_t subwin_size; 84 - int ret = 0, i; 85 221 u32 omi_index = ~(u32)0; 86 222 unsigned long flags; 223 + int ret; 87 224 88 225 /* 89 226 * Configure the omi_index at the geometry setup time. ··· 88 233 */ 89 234 get_ome_index(&omi_index, dev); 90 235 91 - window_addr = geom_attr->aperture_start; 92 - window_size = dma_domain->geom_size; 93 - 94 236 spin_lock_irqsave(&iommu_lock, flags); 95 237 ret = pamu_disable_liodn(liodn); 96 - if (!ret) 97 - ret = pamu_config_ppaace(liodn, window_addr, window_size, omi_index, 98 - 0, dma_domain->snoop_id, 99 - dma_domain->stash_id, win_cnt, 0); 238 + if (ret) 239 + goto out_unlock; 240 + ret = pamu_config_ppaace(liodn, omi_index, dma_domain->stash_id, 0); 241 + if (ret) 242 + goto out_unlock; 243 + ret = pamu_config_ppaace(liodn, ~(u32)0, dma_domain->stash_id, 244 + PAACE_AP_PERMS_QUERY | PAACE_AP_PERMS_UPDATE); 245 + out_unlock: 100 246 spin_unlock_irqrestore(&iommu_lock, flags); 101 247 if (ret) { 102 - pr_debug("PAACE configuration failed for liodn %d, win_cnt =%d\n", 103 - liodn, win_cnt); 104 - return ret; 248 + pr_debug("PAACE configuration failed for liodn %d\n", 249 + liodn); 105 250 } 106 - 107 - if (win_cnt > 1) { 108 - subwin_size = window_size >> ilog2(win_cnt); 109 - for (i = 0; i < win_cnt; i++) { 110 - spin_lock_irqsave(&iommu_lock, flags); 111 - ret = pamu_disable_spaace(liodn, i); 112 - if (!ret) 113 - ret = pamu_config_spaace(liodn, win_cnt, i, 114 - subwin_size, omi_index, 115 - 0, dma_domain->snoop_id, 116 - dma_domain->stash_id, 117 - 0, 0); 118 - spin_unlock_irqrestore(&iommu_lock, flags); 119 - if (ret) { 120 - pr_debug("SPAACE configuration failed for liodn %d\n", 121 - liodn); 122 - return ret; 123 - } 124 - } 125 - } 126 - 127 251 return ret; 128 252 } 129 253 130 - static int check_size(u64 size, dma_addr_t iova) 131 - { 132 - /* 133 - * Size must be a power of two and at least be equal 134 - * to PAMU page size. 135 - */ 136 - if ((size & (size - 1)) || size < PAMU_PAGE_SIZE) { 137 - pr_debug("Size too small or not a power of two\n"); 138 - return -EINVAL; 139 - } 140 - 141 - /* iova must be page size aligned */ 142 - if (iova & (size - 1)) { 143 - pr_debug("Address is not aligned with window size\n"); 144 - return -EINVAL; 145 - } 146 - 147 - return 0; 148 - } 149 - 150 - static struct fsl_dma_domain *iommu_alloc_dma_domain(void) 151 - { 152 - struct fsl_dma_domain *domain; 153 - 154 - domain = kmem_cache_zalloc(fsl_pamu_domain_cache, GFP_KERNEL); 155 - if (!domain) 156 - return NULL; 157 - 158 - domain->stash_id = ~(u32)0; 159 - domain->snoop_id = ~(u32)0; 160 - domain->win_cnt = pamu_get_max_subwin_cnt(); 161 - domain->geom_size = 0; 162 - 163 - INIT_LIST_HEAD(&domain->devices); 164 - 165 - spin_lock_init(&domain->domain_lock); 166 - 167 - return domain; 168 - } 169 - 170 - static void remove_device_ref(struct device_domain_info *info, u32 win_cnt) 254 + static void remove_device_ref(struct device_domain_info *info) 171 255 { 172 256 unsigned long flags; 173 257 174 258 list_del(&info->link); 175 259 spin_lock_irqsave(&iommu_lock, flags); 176 - if (win_cnt > 1) 177 - pamu_free_subwins(info->liodn); 178 260 pamu_disable_liodn(info->liodn); 179 261 spin_unlock_irqrestore(&iommu_lock, flags); 180 262 spin_lock_irqsave(&device_domain_lock, flags); ··· 129 337 /* Remove the device from the domain device list */ 130 338 list_for_each_entry_safe(info, tmp, &dma_domain->devices, link) { 131 339 if (!dev || (info->dev == dev)) 132 - remove_device_ref(info, dma_domain->win_cnt); 340 + remove_device_ref(info); 133 341 } 134 342 spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 135 343 } ··· 171 379 static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain, 172 380 dma_addr_t iova) 173 381 { 174 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 175 - 176 382 if (iova < domain->geometry.aperture_start || 177 383 iova > domain->geometry.aperture_end) 178 384 return 0; 179 - 180 - return get_phys_addr(dma_domain, iova); 385 + return iova; 181 386 } 182 387 183 388 static bool fsl_pamu_capable(enum iommu_cap cap) ··· 188 399 189 400 /* remove all the devices from the device list */ 190 401 detach_device(NULL, dma_domain); 191 - 192 - dma_domain->enabled = 0; 193 - dma_domain->mapped = 0; 194 - 195 402 kmem_cache_free(fsl_pamu_domain_cache, dma_domain); 196 403 } 197 404 ··· 198 413 if (type != IOMMU_DOMAIN_UNMANAGED) 199 414 return NULL; 200 415 201 - dma_domain = iommu_alloc_dma_domain(); 202 - if (!dma_domain) { 203 - pr_debug("dma_domain allocation failed\n"); 416 + dma_domain = kmem_cache_zalloc(fsl_pamu_domain_cache, GFP_KERNEL); 417 + if (!dma_domain) 204 418 return NULL; 205 - } 206 - /* defaul geometry 64 GB i.e. maximum system address */ 419 + 420 + dma_domain->stash_id = ~(u32)0; 421 + INIT_LIST_HEAD(&dma_domain->devices); 422 + spin_lock_init(&dma_domain->domain_lock); 423 + 424 + /* default geometry 64 GB i.e. maximum system address */ 207 425 dma_domain->iommu_domain. geometry.aperture_start = 0; 208 426 dma_domain->iommu_domain.geometry.aperture_end = (1ULL << 36) - 1; 209 427 dma_domain->iommu_domain.geometry.force_aperture = true; 210 428 211 429 return &dma_domain->iommu_domain; 212 - } 213 - 214 - /* Configure geometry settings for all LIODNs associated with domain */ 215 - static int pamu_set_domain_geometry(struct fsl_dma_domain *dma_domain, 216 - struct iommu_domain_geometry *geom_attr, 217 - u32 win_cnt) 218 - { 219 - struct device_domain_info *info; 220 - int ret = 0; 221 - 222 - list_for_each_entry(info, &dma_domain->devices, link) { 223 - ret = pamu_set_liodn(info->liodn, info->dev, dma_domain, 224 - geom_attr, win_cnt); 225 - if (ret) 226 - break; 227 - } 228 - 229 - return ret; 230 430 } 231 431 232 432 /* Update stash destination for all LIODNs associated with the domain */ ··· 229 459 return ret; 230 460 } 231 461 232 - /* Update domain mappings for all LIODNs associated with the domain */ 233 - static int update_domain_mapping(struct fsl_dma_domain *dma_domain, u32 wnd_nr) 234 - { 235 - struct device_domain_info *info; 236 - int ret = 0; 237 - 238 - list_for_each_entry(info, &dma_domain->devices, link) { 239 - ret = update_liodn(info->liodn, dma_domain, wnd_nr); 240 - if (ret) 241 - break; 242 - } 243 - return ret; 244 - } 245 - 246 - static int disable_domain_win(struct fsl_dma_domain *dma_domain, u32 wnd_nr) 247 - { 248 - struct device_domain_info *info; 249 - int ret = 0; 250 - 251 - list_for_each_entry(info, &dma_domain->devices, link) { 252 - if (dma_domain->win_cnt == 1 && dma_domain->enabled) { 253 - ret = pamu_disable_liodn(info->liodn); 254 - if (!ret) 255 - dma_domain->enabled = 0; 256 - } else { 257 - ret = pamu_disable_spaace(info->liodn, wnd_nr); 258 - } 259 - } 260 - 261 - return ret; 262 - } 263 - 264 - static void fsl_pamu_window_disable(struct iommu_domain *domain, u32 wnd_nr) 265 - { 266 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 267 - unsigned long flags; 268 - int ret; 269 - 270 - spin_lock_irqsave(&dma_domain->domain_lock, flags); 271 - if (!dma_domain->win_arr) { 272 - pr_debug("Number of windows not configured\n"); 273 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 274 - return; 275 - } 276 - 277 - if (wnd_nr >= dma_domain->win_cnt) { 278 - pr_debug("Invalid window index\n"); 279 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 280 - return; 281 - } 282 - 283 - if (dma_domain->win_arr[wnd_nr].valid) { 284 - ret = disable_domain_win(dma_domain, wnd_nr); 285 - if (!ret) { 286 - dma_domain->win_arr[wnd_nr].valid = 0; 287 - dma_domain->mapped--; 288 - } 289 - } 290 - 291 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 292 - } 293 - 294 - static int fsl_pamu_window_enable(struct iommu_domain *domain, u32 wnd_nr, 295 - phys_addr_t paddr, u64 size, int prot) 296 - { 297 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 298 - struct dma_window *wnd; 299 - int pamu_prot = 0; 300 - int ret; 301 - unsigned long flags; 302 - u64 win_size; 303 - 304 - if (prot & IOMMU_READ) 305 - pamu_prot |= PAACE_AP_PERMS_QUERY; 306 - if (prot & IOMMU_WRITE) 307 - pamu_prot |= PAACE_AP_PERMS_UPDATE; 308 - 309 - spin_lock_irqsave(&dma_domain->domain_lock, flags); 310 - if (!dma_domain->win_arr) { 311 - pr_debug("Number of windows not configured\n"); 312 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 313 - return -ENODEV; 314 - } 315 - 316 - if (wnd_nr >= dma_domain->win_cnt) { 317 - pr_debug("Invalid window index\n"); 318 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 319 - return -EINVAL; 320 - } 321 - 322 - win_size = dma_domain->geom_size >> ilog2(dma_domain->win_cnt); 323 - if (size > win_size) { 324 - pr_debug("Invalid window size\n"); 325 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 326 - return -EINVAL; 327 - } 328 - 329 - if (dma_domain->win_cnt == 1) { 330 - if (dma_domain->enabled) { 331 - pr_debug("Disable the window before updating the mapping\n"); 332 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 333 - return -EBUSY; 334 - } 335 - 336 - ret = check_size(size, domain->geometry.aperture_start); 337 - if (ret) { 338 - pr_debug("Aperture start not aligned to the size\n"); 339 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 340 - return -EINVAL; 341 - } 342 - } 343 - 344 - wnd = &dma_domain->win_arr[wnd_nr]; 345 - if (!wnd->valid) { 346 - wnd->paddr = paddr; 347 - wnd->size = size; 348 - wnd->prot = pamu_prot; 349 - 350 - ret = update_domain_mapping(dma_domain, wnd_nr); 351 - if (!ret) { 352 - wnd->valid = 1; 353 - dma_domain->mapped++; 354 - } 355 - } else { 356 - pr_debug("Disable the window before updating the mapping\n"); 357 - ret = -EBUSY; 358 - } 359 - 360 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 361 - 362 - return ret; 363 - } 364 - 365 - /* 366 - * Attach the LIODN to the DMA domain and configure the geometry 367 - * and window mappings. 368 - */ 369 - static int handle_attach_device(struct fsl_dma_domain *dma_domain, 370 - struct device *dev, const u32 *liodn, 371 - int num) 372 - { 373 - unsigned long flags; 374 - struct iommu_domain *domain = &dma_domain->iommu_domain; 375 - int ret = 0; 376 - int i; 377 - 378 - spin_lock_irqsave(&dma_domain->domain_lock, flags); 379 - for (i = 0; i < num; i++) { 380 - /* Ensure that LIODN value is valid */ 381 - if (liodn[i] >= PAACE_NUMBER_ENTRIES) { 382 - pr_debug("Invalid liodn %d, attach device failed for %pOF\n", 383 - liodn[i], dev->of_node); 384 - ret = -EINVAL; 385 - break; 386 - } 387 - 388 - attach_device(dma_domain, liodn[i], dev); 389 - /* 390 - * Check if geometry has already been configured 391 - * for the domain. If yes, set the geometry for 392 - * the LIODN. 393 - */ 394 - if (dma_domain->win_arr) { 395 - u32 win_cnt = dma_domain->win_cnt > 1 ? dma_domain->win_cnt : 0; 396 - 397 - ret = pamu_set_liodn(liodn[i], dev, dma_domain, 398 - &domain->geometry, win_cnt); 399 - if (ret) 400 - break; 401 - if (dma_domain->mapped) { 402 - /* 403 - * Create window/subwindow mapping for 404 - * the LIODN. 405 - */ 406 - ret = map_liodn(liodn[i], dma_domain); 407 - if (ret) 408 - break; 409 - } 410 - } 411 - } 412 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 413 - 414 - return ret; 415 - } 416 - 417 462 static int fsl_pamu_attach_device(struct iommu_domain *domain, 418 463 struct device *dev) 419 464 { 420 465 struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 466 + unsigned long flags; 467 + int len, ret = 0, i; 421 468 const u32 *liodn; 422 - u32 liodn_cnt; 423 - int len, ret = 0; 424 469 struct pci_dev *pdev = NULL; 425 470 struct pci_controller *pci_ctl; 426 471 ··· 255 670 } 256 671 257 672 liodn = of_get_property(dev->of_node, "fsl,liodn", &len); 258 - if (liodn) { 259 - liodn_cnt = len / sizeof(u32); 260 - ret = handle_attach_device(dma_domain, dev, liodn, liodn_cnt); 261 - } else { 673 + if (!liodn) { 262 674 pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node); 263 - ret = -EINVAL; 675 + return -EINVAL; 264 676 } 265 677 678 + spin_lock_irqsave(&dma_domain->domain_lock, flags); 679 + for (i = 0; i < len / sizeof(u32); i++) { 680 + /* Ensure that LIODN value is valid */ 681 + if (liodn[i] >= PAACE_NUMBER_ENTRIES) { 682 + pr_debug("Invalid liodn %d, attach device failed for %pOF\n", 683 + liodn[i], dev->of_node); 684 + ret = -EINVAL; 685 + break; 686 + } 687 + 688 + attach_device(dma_domain, liodn[i], dev); 689 + ret = pamu_set_liodn(dma_domain, dev, liodn[i]); 690 + if (ret) 691 + break; 692 + ret = pamu_enable_liodn(liodn[i]); 693 + if (ret) 694 + break; 695 + } 696 + spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 266 697 return ret; 267 698 } 268 699 ··· 313 712 pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node); 314 713 } 315 714 316 - static int configure_domain_geometry(struct iommu_domain *domain, void *data) 317 - { 318 - struct iommu_domain_geometry *geom_attr = data; 319 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 320 - dma_addr_t geom_size; 321 - unsigned long flags; 322 - 323 - geom_size = geom_attr->aperture_end - geom_attr->aperture_start + 1; 324 - /* 325 - * Sanity check the geometry size. Also, we do not support 326 - * DMA outside of the geometry. 327 - */ 328 - if (check_size(geom_size, geom_attr->aperture_start) || 329 - !geom_attr->force_aperture) { 330 - pr_debug("Invalid PAMU geometry attributes\n"); 331 - return -EINVAL; 332 - } 333 - 334 - spin_lock_irqsave(&dma_domain->domain_lock, flags); 335 - if (dma_domain->enabled) { 336 - pr_debug("Can't set geometry attributes as domain is active\n"); 337 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 338 - return -EBUSY; 339 - } 340 - 341 - /* Copy the domain geometry information */ 342 - memcpy(&domain->geometry, geom_attr, 343 - sizeof(struct iommu_domain_geometry)); 344 - dma_domain->geom_size = geom_size; 345 - 346 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 347 - 348 - return 0; 349 - } 350 - 351 715 /* Set the domain stash attribute */ 352 - static int configure_domain_stash(struct fsl_dma_domain *dma_domain, void *data) 716 + int fsl_pamu_configure_l1_stash(struct iommu_domain *domain, u32 cpu) 353 717 { 354 - struct pamu_stash_attribute *stash_attr = data; 718 + struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 355 719 unsigned long flags; 356 720 int ret; 357 721 358 722 spin_lock_irqsave(&dma_domain->domain_lock, flags); 359 - 360 - memcpy(&dma_domain->dma_stash, stash_attr, 361 - sizeof(struct pamu_stash_attribute)); 362 - 363 - dma_domain->stash_id = get_stash_id(stash_attr->cache, 364 - stash_attr->cpu); 723 + dma_domain->stash_id = get_stash_id(PAMU_ATTR_CACHE_L1, cpu); 365 724 if (dma_domain->stash_id == ~(u32)0) { 366 725 pr_debug("Invalid stash attributes\n"); 367 726 spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 368 727 return -EINVAL; 369 728 } 370 - 371 729 ret = update_domain_stash(dma_domain, dma_domain->stash_id); 372 - 373 730 spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 374 - 375 - return ret; 376 - } 377 - 378 - /* Configure domain dma state i.e. enable/disable DMA */ 379 - static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable) 380 - { 381 - struct device_domain_info *info; 382 - unsigned long flags; 383 - int ret; 384 - 385 - spin_lock_irqsave(&dma_domain->domain_lock, flags); 386 - 387 - if (enable && !dma_domain->mapped) { 388 - pr_debug("Can't enable DMA domain without valid mapping\n"); 389 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 390 - return -ENODEV; 391 - } 392 - 393 - dma_domain->enabled = enable; 394 - list_for_each_entry(info, &dma_domain->devices, link) { 395 - ret = (enable) ? pamu_enable_liodn(info->liodn) : 396 - pamu_disable_liodn(info->liodn); 397 - if (ret) 398 - pr_debug("Unable to set dma state for liodn %d", 399 - info->liodn); 400 - } 401 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 402 - 403 - return 0; 404 - } 405 - 406 - static int fsl_pamu_set_windows(struct iommu_domain *domain, u32 w_count) 407 - { 408 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 409 - unsigned long flags; 410 - int ret; 411 - 412 - spin_lock_irqsave(&dma_domain->domain_lock, flags); 413 - /* Ensure domain is inactive i.e. DMA should be disabled for the domain */ 414 - if (dma_domain->enabled) { 415 - pr_debug("Can't set geometry attributes as domain is active\n"); 416 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 417 - return -EBUSY; 418 - } 419 - 420 - /* Ensure that the geometry has been set for the domain */ 421 - if (!dma_domain->geom_size) { 422 - pr_debug("Please configure geometry before setting the number of windows\n"); 423 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 424 - return -EINVAL; 425 - } 426 - 427 - /* 428 - * Ensure we have valid window count i.e. it should be less than 429 - * maximum permissible limit and should be a power of two. 430 - */ 431 - if (w_count > pamu_get_max_subwin_cnt() || !is_power_of_2(w_count)) { 432 - pr_debug("Invalid window count\n"); 433 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 434 - return -EINVAL; 435 - } 436 - 437 - ret = pamu_set_domain_geometry(dma_domain, &domain->geometry, 438 - w_count > 1 ? w_count : 0); 439 - if (!ret) { 440 - kfree(dma_domain->win_arr); 441 - dma_domain->win_arr = kcalloc(w_count, 442 - sizeof(*dma_domain->win_arr), 443 - GFP_ATOMIC); 444 - if (!dma_domain->win_arr) { 445 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 446 - return -ENOMEM; 447 - } 448 - dma_domain->win_cnt = w_count; 449 - } 450 - spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 451 - 452 - return ret; 453 - } 454 - 455 - static int fsl_pamu_set_domain_attr(struct iommu_domain *domain, 456 - enum iommu_attr attr_type, void *data) 457 - { 458 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 459 - int ret = 0; 460 - 461 - switch (attr_type) { 462 - case DOMAIN_ATTR_GEOMETRY: 463 - ret = configure_domain_geometry(domain, data); 464 - break; 465 - case DOMAIN_ATTR_FSL_PAMU_STASH: 466 - ret = configure_domain_stash(dma_domain, data); 467 - break; 468 - case DOMAIN_ATTR_FSL_PAMU_ENABLE: 469 - ret = configure_domain_dma_state(dma_domain, *(int *)data); 470 - break; 471 - case DOMAIN_ATTR_WINDOWS: 472 - ret = fsl_pamu_set_windows(domain, *(u32 *)data); 473 - break; 474 - default: 475 - pr_debug("Unsupported attribute type\n"); 476 - ret = -EINVAL; 477 - break; 478 - } 479 - 480 - return ret; 481 - } 482 - 483 - static int fsl_pamu_get_domain_attr(struct iommu_domain *domain, 484 - enum iommu_attr attr_type, void *data) 485 - { 486 - struct fsl_dma_domain *dma_domain = to_fsl_dma_domain(domain); 487 - int ret = 0; 488 - 489 - switch (attr_type) { 490 - case DOMAIN_ATTR_FSL_PAMU_STASH: 491 - memcpy(data, &dma_domain->dma_stash, 492 - sizeof(struct pamu_stash_attribute)); 493 - break; 494 - case DOMAIN_ATTR_FSL_PAMU_ENABLE: 495 - *(int *)data = dma_domain->enabled; 496 - break; 497 - case DOMAIN_ATTR_FSL_PAMUV1: 498 - *(int *)data = DOMAIN_ATTR_FSL_PAMUV1; 499 - break; 500 - case DOMAIN_ATTR_WINDOWS: 501 - *(u32 *)data = dma_domain->win_cnt; 502 - break; 503 - default: 504 - pr_debug("Unsupported attribute type\n"); 505 - ret = -EINVAL; 506 - break; 507 - } 508 731 509 732 return ret; 510 733 } ··· 456 1031 .domain_free = fsl_pamu_domain_free, 457 1032 .attach_dev = fsl_pamu_attach_device, 458 1033 .detach_dev = fsl_pamu_detach_device, 459 - .domain_window_enable = fsl_pamu_window_enable, 460 - .domain_window_disable = fsl_pamu_window_disable, 461 1034 .iova_to_phys = fsl_pamu_iova_to_phys, 462 - .domain_set_attr = fsl_pamu_set_domain_attr, 463 - .domain_get_attr = fsl_pamu_get_domain_attr, 464 1035 .probe_device = fsl_pamu_probe_device, 465 1036 .release_device = fsl_pamu_release_device, 466 1037 .device_group = fsl_pamu_device_group, ··· 474 1053 if (ret) 475 1054 return ret; 476 1055 477 - iommu_device_set_ops(&pamu_iommu, &fsl_pamu_ops); 478 - 479 - ret = iommu_device_register(&pamu_iommu); 1056 + ret = iommu_device_register(&pamu_iommu, &fsl_pamu_ops, NULL); 480 1057 if (ret) { 481 1058 iommu_device_sysfs_remove(&pamu_iommu); 482 1059 pr_err("Can't register iommu device\n");
-46
drivers/iommu/fsl_pamu_domain.h
··· 9 9 10 10 #include "fsl_pamu.h" 11 11 12 - struct dma_window { 13 - phys_addr_t paddr; 14 - u64 size; 15 - int valid; 16 - int prot; 17 - }; 18 - 19 12 struct fsl_dma_domain { 20 - /* 21 - * Indicates the geometry size for the domain. 22 - * This would be set when the geometry is 23 - * configured for the domain. 24 - */ 25 - dma_addr_t geom_size; 26 - /* 27 - * Number of windows assocaited with this domain. 28 - * During domain initialization, it is set to the 29 - * the maximum number of subwindows allowed for a LIODN. 30 - * Minimum value for this is 1 indicating a single PAMU 31 - * window, without any sub windows. Value can be set/ 32 - * queried by set_attr/get_attr API for DOMAIN_ATTR_WINDOWS. 33 - * Value can only be set once the geometry has been configured. 34 - */ 35 - u32 win_cnt; 36 - /* 37 - * win_arr contains information of the configured 38 - * windows for a domain. This is allocated only 39 - * when the number of windows for the domain are 40 - * set. 41 - */ 42 - struct dma_window *win_arr; 43 13 /* list of devices associated with the domain */ 44 14 struct list_head devices; 45 - /* dma_domain states: 46 - * mapped - A particular mapping has been created 47 - * within the configured geometry. 48 - * enabled - DMA has been enabled for the given 49 - * domain. This translates to setting of the 50 - * valid bit for the primary PAACE in the PAMU 51 - * PAACT table. Domain geometry should be set and 52 - * it must have a valid mapping before DMA can be 53 - * enabled for it. 54 - * 55 - */ 56 - int mapped; 57 - int enabled; 58 - /* stash_id obtained from the stash attribute details */ 59 15 u32 stash_id; 60 - struct pamu_stash_attribute dma_stash; 61 - u32 snoop_id; 62 16 struct iommu_domain iommu_domain; 63 17 spinlock_t domain_lock; 64 18 };
+65 -7
drivers/iommu/intel/dmar.c
··· 1140 1140 if (err) 1141 1141 goto err_unmap; 1142 1142 1143 - iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops); 1144 - 1145 - err = iommu_device_register(&iommu->iommu); 1143 + err = iommu_device_register(&iommu->iommu, &intel_iommu_ops, NULL); 1146 1144 if (err) 1147 1145 goto err_unmap; 1148 1146 } ··· 1203 1205 } 1204 1206 } 1205 1207 1208 + static const char *qi_type_string(u8 type) 1209 + { 1210 + switch (type) { 1211 + case QI_CC_TYPE: 1212 + return "Context-cache Invalidation"; 1213 + case QI_IOTLB_TYPE: 1214 + return "IOTLB Invalidation"; 1215 + case QI_DIOTLB_TYPE: 1216 + return "Device-TLB Invalidation"; 1217 + case QI_IEC_TYPE: 1218 + return "Interrupt Entry Cache Invalidation"; 1219 + case QI_IWD_TYPE: 1220 + return "Invalidation Wait"; 1221 + case QI_EIOTLB_TYPE: 1222 + return "PASID-based IOTLB Invalidation"; 1223 + case QI_PC_TYPE: 1224 + return "PASID-cache Invalidation"; 1225 + case QI_DEIOTLB_TYPE: 1226 + return "PASID-based Device-TLB Invalidation"; 1227 + case QI_PGRP_RESP_TYPE: 1228 + return "Page Group Response"; 1229 + default: 1230 + return "UNKNOWN"; 1231 + } 1232 + } 1233 + 1234 + static void qi_dump_fault(struct intel_iommu *iommu, u32 fault) 1235 + { 1236 + unsigned int head = dmar_readl(iommu->reg + DMAR_IQH_REG); 1237 + u64 iqe_err = dmar_readq(iommu->reg + DMAR_IQER_REG); 1238 + struct qi_desc *desc = iommu->qi->desc + head; 1239 + 1240 + if (fault & DMA_FSTS_IQE) 1241 + pr_err("VT-d detected Invalidation Queue Error: Reason %llx", 1242 + DMAR_IQER_REG_IQEI(iqe_err)); 1243 + if (fault & DMA_FSTS_ITE) 1244 + pr_err("VT-d detected Invalidation Time-out Error: SID %llx", 1245 + DMAR_IQER_REG_ITESID(iqe_err)); 1246 + if (fault & DMA_FSTS_ICE) 1247 + pr_err("VT-d detected Invalidation Completion Error: SID %llx", 1248 + DMAR_IQER_REG_ICESID(iqe_err)); 1249 + 1250 + pr_err("QI HEAD: %s qw0 = 0x%llx, qw1 = 0x%llx\n", 1251 + qi_type_string(desc->qw0 & 0xf), 1252 + (unsigned long long)desc->qw0, 1253 + (unsigned long long)desc->qw1); 1254 + 1255 + head = ((head >> qi_shift(iommu)) + QI_LENGTH - 1) % QI_LENGTH; 1256 + head <<= qi_shift(iommu); 1257 + desc = iommu->qi->desc + head; 1258 + 1259 + pr_err("QI PRIOR: %s qw0 = 0x%llx, qw1 = 0x%llx\n", 1260 + qi_type_string(desc->qw0 & 0xf), 1261 + (unsigned long long)desc->qw0, 1262 + (unsigned long long)desc->qw1); 1263 + } 1264 + 1206 1265 static int qi_check_fault(struct intel_iommu *iommu, int index, int wait_index) 1207 1266 { 1208 1267 u32 fault; ··· 1271 1216 return -EAGAIN; 1272 1217 1273 1218 fault = readl(iommu->reg + DMAR_FSTS_REG); 1219 + if (fault & (DMA_FSTS_IQE | DMA_FSTS_ITE | DMA_FSTS_ICE)) 1220 + qi_dump_fault(iommu, fault); 1274 1221 1275 1222 /* 1276 1223 * If IQE happens, the head points to the descriptor associated ··· 1289 1232 * used by software as private data. We won't print 1290 1233 * out these two qw's for security consideration. 1291 1234 */ 1292 - pr_err("VT-d detected invalid descriptor: qw0 = %llx, qw1 = %llx\n", 1293 - (unsigned long long)desc->qw0, 1294 - (unsigned long long)desc->qw1); 1295 1235 memcpy(desc, qi->desc + (wait_index << shift), 1296 1236 1 << shift); 1297 1237 writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG); 1238 + pr_info("Invalidation Queue Error (IQE) cleared\n"); 1298 1239 return -EINVAL; 1299 1240 } 1300 1241 } ··· 1309 1254 tail = ((tail >> shift) - 1 + QI_LENGTH) % QI_LENGTH; 1310 1255 1311 1256 writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG); 1257 + pr_info("Invalidation Time-out Error (ITE) cleared\n"); 1312 1258 1313 1259 do { 1314 1260 if (qi->desc_status[head] == QI_IN_USE) ··· 1321 1265 return -EAGAIN; 1322 1266 } 1323 1267 1324 - if (fault & DMA_FSTS_ICE) 1268 + if (fault & DMA_FSTS_ICE) { 1325 1269 writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG); 1270 + pr_info("Invalidation Completion Error (ICE) cleared\n"); 1271 + } 1326 1272 1327 1273 return 0; 1328 1274 }
+95 -138
drivers/iommu/intel/iommu.c
··· 360 360 EXPORT_SYMBOL_GPL(intel_iommu_enabled); 361 361 362 362 static int dmar_map_gfx = 1; 363 - static int dmar_forcedac; 364 363 static int intel_iommu_strict; 365 364 static int intel_iommu_superpage = 1; 366 365 static int iommu_identity_mapping; ··· 450 451 dmar_map_gfx = 0; 451 452 pr_info("Disable GFX device mapping\n"); 452 453 } else if (!strncmp(str, "forcedac", 8)) { 453 - pr_info("Forcing DAC for PCI devices\n"); 454 - dmar_forcedac = 1; 454 + pr_warn("intel_iommu=forcedac deprecated; use iommu.forcedac instead\n"); 455 + iommu_dma_forcedac = true; 455 456 } else if (!strncmp(str, "strict", 6)) { 456 457 pr_info("Disable batched IOTLB flush\n"); 457 458 intel_iommu_strict = 1; ··· 657 658 rcu_read_lock(); 658 659 for_each_active_iommu(iommu, drhd) { 659 660 if (iommu != skip) { 660 - if (!ecap_sc_support(iommu->ecap)) { 661 + /* 662 + * If the hardware is operating in the scalable mode, 663 + * the snooping control is always supported since we 664 + * always set PASID-table-entry.PGSNP bit if the domain 665 + * is managed outside (UNMANAGED). 666 + */ 667 + if (!sm_supported(iommu) && 668 + !ecap_sc_support(iommu->ecap)) { 661 669 ret = 0; 662 670 break; 663 671 } ··· 1346 1340 readl, (sts & DMA_GSTS_RTPS), sts); 1347 1341 1348 1342 raw_spin_unlock_irqrestore(&iommu->register_lock, flag); 1343 + 1344 + iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); 1345 + if (sm_supported(iommu)) 1346 + qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0); 1347 + iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 1349 1348 } 1350 1349 1351 1350 void iommu_flush_write_buffer(struct intel_iommu *iommu) ··· 2300 2289 return level; 2301 2290 } 2302 2291 2292 + /* 2293 + * Ensure that old small page tables are removed to make room for superpage(s). 2294 + * We're going to add new large pages, so make sure we don't remove their parent 2295 + * tables. The IOTLB/devTLBs should be flushed if any PDE/PTEs are cleared. 2296 + */ 2297 + static void switch_to_super_page(struct dmar_domain *domain, 2298 + unsigned long start_pfn, 2299 + unsigned long end_pfn, int level) 2300 + { 2301 + unsigned long lvl_pages = lvl_to_nr_pages(level); 2302 + struct dma_pte *pte = NULL; 2303 + int i; 2304 + 2305 + while (start_pfn <= end_pfn) { 2306 + if (!pte) 2307 + pte = pfn_to_dma_pte(domain, start_pfn, &level); 2308 + 2309 + if (dma_pte_present(pte)) { 2310 + dma_pte_free_pagetable(domain, start_pfn, 2311 + start_pfn + lvl_pages - 1, 2312 + level + 1); 2313 + 2314 + for_each_domain_iommu(i, domain) 2315 + iommu_flush_iotlb_psi(g_iommus[i], domain, 2316 + start_pfn, lvl_pages, 2317 + 0, 0); 2318 + } 2319 + 2320 + pte++; 2321 + start_pfn += lvl_pages; 2322 + if (first_pte_in_page(pte)) 2323 + pte = NULL; 2324 + } 2325 + } 2326 + 2303 2327 static int 2304 2328 __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, 2305 2329 unsigned long phys_pfn, unsigned long nr_pages, int prot) ··· 2351 2305 return -EINVAL; 2352 2306 2353 2307 attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP); 2308 + attr |= DMA_FL_PTE_PRESENT; 2354 2309 if (domain_use_first_level(domain)) { 2355 - attr |= DMA_FL_PTE_PRESENT | DMA_FL_PTE_XD | DMA_FL_PTE_US; 2310 + attr |= DMA_FL_PTE_XD | DMA_FL_PTE_US; 2356 2311 2357 2312 if (domain->domain.type == IOMMU_DOMAIN_DMA) { 2358 2313 attr |= DMA_FL_PTE_ACCESS; ··· 2376 2329 return -ENOMEM; 2377 2330 /* It is large page*/ 2378 2331 if (largepage_lvl > 1) { 2379 - unsigned long nr_superpages, end_pfn; 2332 + unsigned long end_pfn; 2380 2333 2381 2334 pteval |= DMA_PTE_LARGE_PAGE; 2382 - lvl_pages = lvl_to_nr_pages(largepage_lvl); 2383 - 2384 - nr_superpages = nr_pages / lvl_pages; 2385 - end_pfn = iov_pfn + nr_superpages * lvl_pages - 1; 2386 - 2387 - /* 2388 - * Ensure that old small page tables are 2389 - * removed to make room for superpage(s). 2390 - * We're adding new large pages, so make sure 2391 - * we don't remove their parent tables. 2392 - */ 2393 - dma_pte_free_pagetable(domain, iov_pfn, end_pfn, 2394 - largepage_lvl + 1); 2335 + end_pfn = ((iov_pfn + nr_pages) & level_mask(largepage_lvl)) - 1; 2336 + switch_to_super_page(domain, iov_pfn, end_pfn, largepage_lvl); 2395 2337 } else { 2396 2338 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; 2397 2339 } ··· 2458 2422 (((u16)bus) << 8) | devfn, 2459 2423 DMA_CCMD_MASK_NOBIT, 2460 2424 DMA_CCMD_DEVICE_INVL); 2425 + 2426 + if (sm_supported(iommu)) 2427 + qi_flush_pasid_cache(iommu, did_old, QI_PC_ALL_PASIDS, 0); 2428 + 2461 2429 iommu->flush.flush_iotlb(iommu, 2462 2430 did_old, 2463 2431 0, ··· 2544 2504 return -EINVAL; 2545 2505 2546 2506 flags |= (level == 5) ? PASID_FLAG_FL5LP : 0; 2507 + 2508 + if (domain->domain.type == IOMMU_DOMAIN_UNMANAGED) 2509 + flags |= PASID_FLAG_PAGE_SNOOP; 2547 2510 2548 2511 return intel_pasid_setup_first_level(iommu, dev, (pgd_t *)pgd, pasid, 2549 2512 domain->iommu_did[iommu->seq_id], ··· 3310 3267 register_pasid_allocator(iommu); 3311 3268 #endif 3312 3269 iommu_set_root_entry(iommu); 3313 - iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); 3314 - iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 3315 3270 } 3316 3271 3317 3272 #ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA ··· 3499 3458 } 3500 3459 3501 3460 iommu_flush_write_buffer(iommu); 3502 - 3503 3461 iommu_set_root_entry(iommu); 3504 - 3505 - iommu->flush.flush_context(iommu, 0, 0, 0, 3506 - DMA_CCMD_GLOBAL_INVL); 3507 - iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 3508 3462 iommu_enable_translation(iommu); 3509 3463 iommu_disable_protect_mem_regions(iommu); 3510 3464 } ··· 3882 3846 goto disable_iommu; 3883 3847 3884 3848 iommu_set_root_entry(iommu); 3885 - iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); 3886 - iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 3887 3849 iommu_enable_translation(iommu); 3888 3850 3889 3851 iommu_disable_protect_mem_regions(iommu); ··· 4098 4064 .notifier_call = intel_iommu_memory_notifier, 4099 4065 .priority = 0 4100 4066 }; 4101 - 4102 - static void free_all_cpu_cached_iovas(unsigned int cpu) 4103 - { 4104 - int i; 4105 - 4106 - for (i = 0; i < g_num_of_iommus; i++) { 4107 - struct intel_iommu *iommu = g_iommus[i]; 4108 - struct dmar_domain *domain; 4109 - int did; 4110 - 4111 - if (!iommu) 4112 - continue; 4113 - 4114 - for (did = 0; did < cap_ndoms(iommu->cap); did++) { 4115 - domain = get_iommu_domain(iommu, (u16)did); 4116 - 4117 - if (!domain || domain->domain.type != IOMMU_DOMAIN_DMA) 4118 - continue; 4119 - 4120 - iommu_dma_free_cpu_cached_iovas(cpu, &domain->domain); 4121 - } 4122 - } 4123 - } 4124 - 4125 - static int intel_iommu_cpu_dead(unsigned int cpu) 4126 - { 4127 - free_all_cpu_cached_iovas(cpu); 4128 - return 0; 4129 - } 4130 4067 4131 4068 static void intel_disable_iommus(void) 4132 4069 { ··· 4382 4377 4383 4378 down_read(&dmar_global_lock); 4384 4379 for_each_active_iommu(iommu, drhd) { 4380 + /* 4381 + * The flush queue implementation does not perform 4382 + * page-selective invalidations that are required for efficient 4383 + * TLB flushes in virtual environments. The benefit of batching 4384 + * is likely to be much lower than the overhead of synchronizing 4385 + * the virtual and physical IOMMU page-tables. 4386 + */ 4387 + if (!intel_iommu_strict && cap_caching_mode(iommu->cap)) { 4388 + pr_warn("IOMMU batching is disabled due to virtualization"); 4389 + intel_iommu_strict = 1; 4390 + } 4385 4391 iommu_device_sysfs_add(&iommu->iommu, NULL, 4386 4392 intel_iommu_groups, 4387 4393 "%s", iommu->name); 4388 - iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops); 4389 - iommu_device_register(&iommu->iommu); 4394 + iommu_device_register(&iommu->iommu, &intel_iommu_ops, NULL); 4390 4395 } 4391 4396 up_read(&dmar_global_lock); 4392 4397 4398 + iommu_set_dma_strict(intel_iommu_strict); 4393 4399 bus_set_iommu(&pci_bus_type, &intel_iommu_ops); 4394 4400 if (si_domain && !hw_pass_through) 4395 4401 register_memory_notifier(&intel_iommu_memory_nb); 4396 - cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL, 4397 - intel_iommu_cpu_dead); 4398 4402 4399 4403 down_read(&dmar_global_lock); 4400 4404 if (probe_acpi_namespace_devices()) ··· 5357 5343 static bool 5358 5344 intel_iommu_dev_has_feat(struct device *dev, enum iommu_dev_features feat) 5359 5345 { 5346 + struct device_domain_info *info = get_domain_info(dev); 5347 + 5360 5348 if (feat == IOMMU_DEV_FEAT_AUX) { 5361 5349 int ret; 5362 5350 ··· 5373 5357 return !!siov_find_pci_dvsec(to_pci_dev(dev)); 5374 5358 } 5375 5359 5376 - if (feat == IOMMU_DEV_FEAT_SVA) { 5377 - struct device_domain_info *info = get_domain_info(dev); 5360 + if (feat == IOMMU_DEV_FEAT_IOPF) 5361 + return info && info->pri_supported; 5378 5362 5363 + if (feat == IOMMU_DEV_FEAT_SVA) 5379 5364 return info && (info->iommu->flags & VTD_FLAG_SVM_CAPABLE) && 5380 5365 info->pasid_supported && info->pri_supported && 5381 5366 info->ats_supported; 5382 - } 5383 5367 5384 5368 return false; 5385 5369 } ··· 5390 5374 if (feat == IOMMU_DEV_FEAT_AUX) 5391 5375 return intel_iommu_enable_auxd(dev); 5392 5376 5377 + if (feat == IOMMU_DEV_FEAT_IOPF) 5378 + return intel_iommu_dev_has_feat(dev, feat) ? 0 : -ENODEV; 5379 + 5393 5380 if (feat == IOMMU_DEV_FEAT_SVA) { 5394 5381 struct device_domain_info *info = get_domain_info(dev); 5395 5382 5396 5383 if (!info) 5384 + return -EINVAL; 5385 + 5386 + if (!info->pasid_enabled || !info->pri_enabled || !info->ats_enabled) 5397 5387 return -EINVAL; 5398 5388 5399 5389 if (info->iommu->flags & VTD_FLAG_SVM_CAPABLE) ··· 5445 5423 } 5446 5424 5447 5425 static int 5448 - intel_iommu_domain_set_attr(struct iommu_domain *domain, 5449 - enum iommu_attr attr, void *data) 5426 + intel_iommu_enable_nesting(struct iommu_domain *domain) 5450 5427 { 5451 5428 struct dmar_domain *dmar_domain = to_dmar_domain(domain); 5452 5429 unsigned long flags; 5453 - int ret = 0; 5430 + int ret = -ENODEV; 5454 5431 5455 - if (domain->type != IOMMU_DOMAIN_UNMANAGED) 5456 - return -EINVAL; 5457 - 5458 - switch (attr) { 5459 - case DOMAIN_ATTR_NESTING: 5460 - spin_lock_irqsave(&device_domain_lock, flags); 5461 - if (nested_mode_support() && 5462 - list_empty(&dmar_domain->devices)) { 5463 - dmar_domain->flags |= DOMAIN_FLAG_NESTING_MODE; 5464 - dmar_domain->flags &= ~DOMAIN_FLAG_USE_FIRST_LEVEL; 5465 - } else { 5466 - ret = -ENODEV; 5467 - } 5468 - spin_unlock_irqrestore(&device_domain_lock, flags); 5469 - break; 5470 - default: 5471 - ret = -EINVAL; 5472 - break; 5432 + spin_lock_irqsave(&device_domain_lock, flags); 5433 + if (nested_mode_support() && list_empty(&dmar_domain->devices)) { 5434 + dmar_domain->flags |= DOMAIN_FLAG_NESTING_MODE; 5435 + dmar_domain->flags &= ~DOMAIN_FLAG_USE_FIRST_LEVEL; 5436 + ret = 0; 5473 5437 } 5438 + spin_unlock_irqrestore(&device_domain_lock, flags); 5474 5439 5475 5440 return ret; 5476 - } 5477 - 5478 - static bool domain_use_flush_queue(void) 5479 - { 5480 - struct dmar_drhd_unit *drhd; 5481 - struct intel_iommu *iommu; 5482 - bool r = true; 5483 - 5484 - if (intel_iommu_strict) 5485 - return false; 5486 - 5487 - /* 5488 - * The flush queue implementation does not perform page-selective 5489 - * invalidations that are required for efficient TLB flushes in virtual 5490 - * environments. The benefit of batching is likely to be much lower than 5491 - * the overhead of synchronizing the virtual and physical IOMMU 5492 - * page-tables. 5493 - */ 5494 - rcu_read_lock(); 5495 - for_each_active_iommu(iommu, drhd) { 5496 - if (!cap_caching_mode(iommu->cap)) 5497 - continue; 5498 - 5499 - pr_warn_once("IOMMU batching is disabled due to virtualization"); 5500 - r = false; 5501 - break; 5502 - } 5503 - rcu_read_unlock(); 5504 - 5505 - return r; 5506 - } 5507 - 5508 - static int 5509 - intel_iommu_domain_get_attr(struct iommu_domain *domain, 5510 - enum iommu_attr attr, void *data) 5511 - { 5512 - switch (domain->type) { 5513 - case IOMMU_DOMAIN_UNMANAGED: 5514 - return -ENODEV; 5515 - case IOMMU_DOMAIN_DMA: 5516 - switch (attr) { 5517 - case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE: 5518 - *(int *)data = domain_use_flush_queue(); 5519 - return 0; 5520 - default: 5521 - return -ENODEV; 5522 - } 5523 - break; 5524 - default: 5525 - return -EINVAL; 5526 - } 5527 5441 } 5528 5442 5529 5443 /* ··· 5534 5576 .capable = intel_iommu_capable, 5535 5577 .domain_alloc = intel_iommu_domain_alloc, 5536 5578 .domain_free = intel_iommu_domain_free, 5537 - .domain_get_attr = intel_iommu_domain_get_attr, 5538 - .domain_set_attr = intel_iommu_domain_set_attr, 5579 + .enable_nesting = intel_iommu_enable_nesting, 5539 5580 .attach_dev = intel_iommu_attach_device, 5540 5581 .detach_dev = intel_iommu_detach_device, 5541 5582 .aux_attach_dev = intel_iommu_aux_attach_device,
+1 -1
drivers/iommu/intel/irq_remapping.c
··· 736 736 return -ENODEV; 737 737 738 738 if (intel_cap_audit(CAP_AUDIT_STATIC_IRQR, NULL)) 739 - goto error; 739 + return -ENODEV; 740 740 741 741 if (!dmar_ir_support()) 742 742 return -ENODEV;
+65 -10
drivers/iommu/intel/pasid.c
··· 24 24 /* 25 25 * Intel IOMMU system wide PASID name space: 26 26 */ 27 - static DEFINE_SPINLOCK(pasid_lock); 28 27 u32 intel_pasid_max_id = PASID_MAX; 29 28 30 29 int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid) ··· 230 231 return info->pasid_table; 231 232 } 232 233 233 - int intel_pasid_get_dev_max_id(struct device *dev) 234 + static int intel_pasid_get_dev_max_id(struct device *dev) 234 235 { 235 236 struct device_domain_info *info; 236 237 ··· 241 242 return info->pasid_table->max_pasid; 242 243 } 243 244 244 - struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) 245 + static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) 245 246 { 246 247 struct device_domain_info *info; 247 248 struct pasid_table *pasid_table; ··· 258 259 dir_index = pasid >> PASID_PDE_SHIFT; 259 260 index = pasid & PASID_PTE_MASK; 260 261 261 - spin_lock(&pasid_lock); 262 + retry: 262 263 entries = get_pasid_table_from_pde(&dir[dir_index]); 263 264 if (!entries) { 264 265 entries = alloc_pgtable_page(info->iommu->node); 265 - if (!entries) { 266 - spin_unlock(&pasid_lock); 266 + if (!entries) 267 267 return NULL; 268 - } 269 268 270 - WRITE_ONCE(dir[dir_index].val, 271 - (u64)virt_to_phys(entries) | PASID_PTE_PRESENT); 269 + /* 270 + * The pasid directory table entry won't be freed after 271 + * allocation. No worry about the race with free and 272 + * clear. However, this entry might be populated by others 273 + * while we are preparing it. Use theirs with a retry. 274 + */ 275 + if (cmpxchg64(&dir[dir_index].val, 0ULL, 276 + (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) { 277 + free_pgtable_page(entries); 278 + goto retry; 279 + } 272 280 } 273 - spin_unlock(&pasid_lock); 274 281 275 282 return &entries[index]; 276 283 } ··· 399 394 } 400 395 401 396 /* 397 + * Setup the WPE(Write Protect Enable) field (Bit 132) of a 398 + * scalable mode PASID entry. 399 + */ 400 + static inline void pasid_set_wpe(struct pasid_entry *pe) 401 + { 402 + pasid_set_bits(&pe->val[2], 1 << 4, 1 << 4); 403 + } 404 + 405 + /* 402 406 * Setup the P(Present) field (Bit 0) of a scalable mode PASID 403 407 * entry. 404 408 */ ··· 423 409 static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value) 424 410 { 425 411 pasid_set_bits(&pe->val[1], 1 << 23, value << 23); 412 + } 413 + 414 + /* 415 + * Setup the Page Snoop (PGSNP) field (Bit 88) of a scalable mode 416 + * PASID entry. 417 + */ 418 + static inline void 419 + pasid_set_pgsnp(struct pasid_entry *pe) 420 + { 421 + pasid_set_bits(&pe->val[1], 1ULL << 24, 1ULL << 24); 426 422 } 427 423 428 424 /* ··· 517 493 if (WARN_ON(!pte)) 518 494 return; 519 495 496 + if (!(pte->val[0] & PASID_PTE_PRESENT)) 497 + return; 498 + 520 499 did = pasid_get_domain_id(pte); 521 500 intel_pasid_clear_entry(dev, pasid, fault_ignore); 522 501 ··· 548 521 iommu_flush_write_buffer(iommu); 549 522 } 550 523 } 524 + 525 + static inline int pasid_enable_wpe(struct pasid_entry *pte) 526 + { 527 + #ifdef CONFIG_X86 528 + unsigned long cr0 = read_cr0(); 529 + 530 + /* CR0.WP is normally set but just to be sure */ 531 + if (unlikely(!(cr0 & X86_CR0_WP))) { 532 + pr_err_ratelimited("No CPU write protect!\n"); 533 + return -EINVAL; 534 + } 535 + #endif 536 + pasid_set_wpe(pte); 537 + 538 + return 0; 539 + }; 551 540 552 541 /* 553 542 * Set up the scalable mode pasid table entry for first only ··· 596 553 return -EINVAL; 597 554 } 598 555 pasid_set_sre(pte); 556 + if (pasid_enable_wpe(pte)) 557 + return -EINVAL; 558 + 599 559 } 600 560 601 561 if (flags & PASID_FLAG_FL5LP) { ··· 610 564 return -EINVAL; 611 565 } 612 566 } 567 + 568 + if (flags & PASID_FLAG_PAGE_SNOOP) 569 + pasid_set_pgsnp(pte); 613 570 614 571 pasid_set_domain_id(pte, did); 615 572 pasid_set_address_width(pte, iommu->agaw); ··· 692 643 pasid_set_fault_enable(pte); 693 644 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); 694 645 646 + if (domain->domain.type == IOMMU_DOMAIN_UNMANAGED) 647 + pasid_set_pgsnp(pte); 648 + 695 649 /* 696 650 * Since it is a second level only translation setup, we should 697 651 * set SRE bit as well (addresses are expected to be GPAs). ··· 758 706 return -EINVAL; 759 707 } 760 708 pasid_set_sre(pte); 709 + /* Enable write protect WP if guest requested */ 710 + if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_WPE) 711 + pasid_set_wpe(pte); 761 712 } 762 713 763 714 if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_EAFE) {
+1 -5
drivers/iommu/intel/pasid.h
··· 48 48 */ 49 49 #define PASID_FLAG_SUPERVISOR_MODE BIT(0) 50 50 #define PASID_FLAG_NESTED BIT(1) 51 + #define PASID_FLAG_PAGE_SNOOP BIT(2) 51 52 52 53 /* 53 54 * The PASID_FLAG_FL5LP flag Indicates using 5-level paging for first- ··· 100 99 } 101 100 102 101 extern unsigned int intel_pasid_max_id; 103 - int intel_pasid_alloc_id(void *ptr, int start, int end, gfp_t gfp); 104 - void intel_pasid_free_id(u32 pasid); 105 - void *intel_pasid_lookup_id(u32 pasid); 106 102 int intel_pasid_alloc_table(struct device *dev); 107 103 void intel_pasid_free_table(struct device *dev); 108 104 struct pasid_table *intel_pasid_get_table(struct device *dev); 109 - int intel_pasid_get_dev_max_id(struct device *dev); 110 - struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid); 111 105 int intel_pasid_setup_first_level(struct intel_iommu *iommu, 112 106 struct device *dev, pgd_t *pgd, 113 107 u32 pasid, u16 did, int flags);
+40 -44
drivers/iommu/intel/svm.c
··· 462 462 /* Caller must hold pasid_mutex, mm reference */ 463 463 static int 464 464 intel_svm_bind_mm(struct device *dev, unsigned int flags, 465 - struct svm_dev_ops *ops, 466 465 struct mm_struct *mm, struct intel_svm_dev **sd) 467 466 { 468 467 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 468 + struct intel_svm *svm = NULL, *t; 469 469 struct device_domain_info *info; 470 470 struct intel_svm_dev *sdev; 471 - struct intel_svm *svm = NULL; 472 471 unsigned long iflags; 473 472 int pasid_max; 474 473 int ret; ··· 493 494 } 494 495 } 495 496 496 - if (!(flags & SVM_FLAG_PRIVATE_PASID)) { 497 - struct intel_svm *t; 497 + list_for_each_entry(t, &global_svm_list, list) { 498 + if (t->mm != mm) 499 + continue; 498 500 499 - list_for_each_entry(t, &global_svm_list, list) { 500 - if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID)) 501 - continue; 502 - 503 - svm = t; 504 - if (svm->pasid >= pasid_max) { 505 - dev_warn(dev, 506 - "Limited PASID width. Cannot use existing PASID %d\n", 507 - svm->pasid); 508 - ret = -ENOSPC; 509 - goto out; 510 - } 511 - 512 - /* Find the matching device in svm list */ 513 - for_each_svm_dev(sdev, svm, dev) { 514 - if (sdev->ops != ops) { 515 - ret = -EBUSY; 516 - goto out; 517 - } 518 - sdev->users++; 519 - goto success; 520 - } 521 - 522 - break; 501 + svm = t; 502 + if (svm->pasid >= pasid_max) { 503 + dev_warn(dev, 504 + "Limited PASID width. Cannot use existing PASID %d\n", 505 + svm->pasid); 506 + ret = -ENOSPC; 507 + goto out; 523 508 } 509 + 510 + /* Find the matching device in svm list */ 511 + for_each_svm_dev(sdev, svm, dev) { 512 + sdev->users++; 513 + goto success; 514 + } 515 + 516 + break; 524 517 } 525 518 526 519 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); ··· 541 550 542 551 /* Finish the setup now we know we're keeping it */ 543 552 sdev->users = 1; 544 - sdev->ops = ops; 545 553 init_rcu_head(&sdev->rcu); 546 554 547 555 if (!svm) { ··· 852 862 /* Fill in event data for device specific processing */ 853 863 memset(&event, 0, sizeof(struct iommu_fault_event)); 854 864 event.fault.type = IOMMU_FAULT_PAGE_REQ; 855 - event.fault.prm.addr = desc->addr; 865 + event.fault.prm.addr = (u64)desc->addr << VTD_PAGE_SHIFT; 856 866 event.fault.prm.pasid = desc->pasid; 857 867 event.fault.prm.grpid = desc->prg_index; 858 868 event.fault.prm.perm = prq_to_iommu_prot(desc); ··· 885 895 struct intel_iommu *iommu = d; 886 896 struct intel_svm *svm = NULL; 887 897 int head, tail, handled = 0; 898 + unsigned int flags = 0; 888 899 889 900 /* Clear PPR bit before reading head/tail registers, to 890 901 * ensure that we get a new interrupt if needed. */ ··· 911 920 ((unsigned long long *)req)[1]); 912 921 goto no_pasid; 913 922 } 914 - 923 + /* We shall not receive page request for supervisor SVM */ 924 + if (req->pm_req && (req->rd_req | req->wr_req)) { 925 + pr_err("Unexpected page request in Privilege Mode"); 926 + /* No need to find the matching sdev as for bad_req */ 927 + goto no_pasid; 928 + } 929 + /* DMA read with exec requeset is not supported. */ 930 + if (req->exe_req && req->rd_req) { 931 + pr_err("Execution request not supported\n"); 932 + goto no_pasid; 933 + } 915 934 if (!svm || svm->pasid != req->pasid) { 916 935 rcu_read_lock(); 917 936 svm = ioasid_find(NULL, req->pasid, NULL); ··· 983 982 if (access_error(vma, req)) 984 983 goto invalid; 985 984 986 - ret = handle_mm_fault(vma, address, 987 - req->wr_req ? FAULT_FLAG_WRITE : 0, 988 - NULL); 985 + flags = FAULT_FLAG_USER | FAULT_FLAG_REMOTE; 986 + if (req->wr_req) 987 + flags |= FAULT_FLAG_WRITE; 988 + 989 + ret = handle_mm_fault(vma, address, flags, NULL); 989 990 if (ret & VM_FAULT_ERROR) 990 991 goto invalid; 991 992 ··· 996 993 mmap_read_unlock(svm->mm); 997 994 mmput(svm->mm); 998 995 bad_req: 999 - WARN_ON(!sdev); 1000 - if (sdev && sdev->ops && sdev->ops->fault_cb) { 1001 - int rwxp = (req->rd_req << 3) | (req->wr_req << 2) | 1002 - (req->exe_req << 1) | (req->pm_req); 1003 - sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, 1004 - req->priv_data, rwxp, result); 1005 - } 1006 996 /* We get here in the error case where the PASID lookup failed, 1007 997 and these can be NULL. Do not use them below this point! */ 1008 998 sdev = NULL; ··· 1017 1021 QI_PGRP_RESP_TYPE; 1018 1022 resp.qw1 = QI_PGRP_IDX(req->prg_index) | 1019 1023 QI_PGRP_LPIG(req->lpig); 1024 + resp.qw2 = 0; 1025 + resp.qw3 = 0; 1020 1026 1021 1027 if (req->priv_data_present) 1022 1028 memcpy(&resp.qw2, req->priv_data, 1023 1029 sizeof(req->priv_data)); 1024 - resp.qw2 = 0; 1025 - resp.qw3 = 0; 1026 1030 qi_submit_sync(iommu, &resp, 1, 0); 1027 1031 } 1028 1032 prq_advance: ··· 1070 1074 if (drvdata) 1071 1075 flags = *(unsigned int *)drvdata; 1072 1076 mutex_lock(&pasid_mutex); 1073 - ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev); 1077 + ret = intel_svm_bind_mm(dev, flags, mm, &sdev); 1074 1078 if (ret) 1075 1079 sva = ERR_PTR(ret); 1076 1080 else if (sdev)
+461
drivers/iommu/io-pgfault.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Handle device page faults 4 + * 5 + * Copyright (C) 2020 ARM Ltd. 6 + */ 7 + 8 + #include <linux/iommu.h> 9 + #include <linux/list.h> 10 + #include <linux/sched/mm.h> 11 + #include <linux/slab.h> 12 + #include <linux/workqueue.h> 13 + 14 + #include "iommu-sva-lib.h" 15 + 16 + /** 17 + * struct iopf_queue - IO Page Fault queue 18 + * @wq: the fault workqueue 19 + * @devices: devices attached to this queue 20 + * @lock: protects the device list 21 + */ 22 + struct iopf_queue { 23 + struct workqueue_struct *wq; 24 + struct list_head devices; 25 + struct mutex lock; 26 + }; 27 + 28 + /** 29 + * struct iopf_device_param - IO Page Fault data attached to a device 30 + * @dev: the device that owns this param 31 + * @queue: IOPF queue 32 + * @queue_list: index into queue->devices 33 + * @partial: faults that are part of a Page Request Group for which the last 34 + * request hasn't been submitted yet. 35 + */ 36 + struct iopf_device_param { 37 + struct device *dev; 38 + struct iopf_queue *queue; 39 + struct list_head queue_list; 40 + struct list_head partial; 41 + }; 42 + 43 + struct iopf_fault { 44 + struct iommu_fault fault; 45 + struct list_head list; 46 + }; 47 + 48 + struct iopf_group { 49 + struct iopf_fault last_fault; 50 + struct list_head faults; 51 + struct work_struct work; 52 + struct device *dev; 53 + }; 54 + 55 + static int iopf_complete_group(struct device *dev, struct iopf_fault *iopf, 56 + enum iommu_page_response_code status) 57 + { 58 + struct iommu_page_response resp = { 59 + .version = IOMMU_PAGE_RESP_VERSION_1, 60 + .pasid = iopf->fault.prm.pasid, 61 + .grpid = iopf->fault.prm.grpid, 62 + .code = status, 63 + }; 64 + 65 + if ((iopf->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) && 66 + (iopf->fault.prm.flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID)) 67 + resp.flags = IOMMU_PAGE_RESP_PASID_VALID; 68 + 69 + return iommu_page_response(dev, &resp); 70 + } 71 + 72 + static enum iommu_page_response_code 73 + iopf_handle_single(struct iopf_fault *iopf) 74 + { 75 + vm_fault_t ret; 76 + struct mm_struct *mm; 77 + struct vm_area_struct *vma; 78 + unsigned int access_flags = 0; 79 + unsigned int fault_flags = FAULT_FLAG_REMOTE; 80 + struct iommu_fault_page_request *prm = &iopf->fault.prm; 81 + enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID; 82 + 83 + if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID)) 84 + return status; 85 + 86 + mm = iommu_sva_find(prm->pasid); 87 + if (IS_ERR_OR_NULL(mm)) 88 + return status; 89 + 90 + mmap_read_lock(mm); 91 + 92 + vma = find_extend_vma(mm, prm->addr); 93 + if (!vma) 94 + /* Unmapped area */ 95 + goto out_put_mm; 96 + 97 + if (prm->perm & IOMMU_FAULT_PERM_READ) 98 + access_flags |= VM_READ; 99 + 100 + if (prm->perm & IOMMU_FAULT_PERM_WRITE) { 101 + access_flags |= VM_WRITE; 102 + fault_flags |= FAULT_FLAG_WRITE; 103 + } 104 + 105 + if (prm->perm & IOMMU_FAULT_PERM_EXEC) { 106 + access_flags |= VM_EXEC; 107 + fault_flags |= FAULT_FLAG_INSTRUCTION; 108 + } 109 + 110 + if (!(prm->perm & IOMMU_FAULT_PERM_PRIV)) 111 + fault_flags |= FAULT_FLAG_USER; 112 + 113 + if (access_flags & ~vma->vm_flags) 114 + /* Access fault */ 115 + goto out_put_mm; 116 + 117 + ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL); 118 + status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID : 119 + IOMMU_PAGE_RESP_SUCCESS; 120 + 121 + out_put_mm: 122 + mmap_read_unlock(mm); 123 + mmput(mm); 124 + 125 + return status; 126 + } 127 + 128 + static void iopf_handle_group(struct work_struct *work) 129 + { 130 + struct iopf_group *group; 131 + struct iopf_fault *iopf, *next; 132 + enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS; 133 + 134 + group = container_of(work, struct iopf_group, work); 135 + 136 + list_for_each_entry_safe(iopf, next, &group->faults, list) { 137 + /* 138 + * For the moment, errors are sticky: don't handle subsequent 139 + * faults in the group if there is an error. 140 + */ 141 + if (status == IOMMU_PAGE_RESP_SUCCESS) 142 + status = iopf_handle_single(iopf); 143 + 144 + if (!(iopf->fault.prm.flags & 145 + IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) 146 + kfree(iopf); 147 + } 148 + 149 + iopf_complete_group(group->dev, &group->last_fault, status); 150 + kfree(group); 151 + } 152 + 153 + /** 154 + * iommu_queue_iopf - IO Page Fault handler 155 + * @fault: fault event 156 + * @cookie: struct device, passed to iommu_register_device_fault_handler. 157 + * 158 + * Add a fault to the device workqueue, to be handled by mm. 159 + * 160 + * This module doesn't handle PCI PASID Stop Marker; IOMMU drivers must discard 161 + * them before reporting faults. A PASID Stop Marker (LRW = 0b100) doesn't 162 + * expect a response. It may be generated when disabling a PASID (issuing a 163 + * PASID stop request) by some PCI devices. 164 + * 165 + * The PASID stop request is issued by the device driver before unbind(). Once 166 + * it completes, no page request is generated for this PASID anymore and 167 + * outstanding ones have been pushed to the IOMMU (as per PCIe 4.0r1.0 - 6.20.1 168 + * and 10.4.1.2 - Managing PASID TLP Prefix Usage). Some PCI devices will wait 169 + * for all outstanding page requests to come back with a response before 170 + * completing the PASID stop request. Others do not wait for page responses, and 171 + * instead issue this Stop Marker that tells us when the PASID can be 172 + * reallocated. 173 + * 174 + * It is safe to discard the Stop Marker because it is an optimization. 175 + * a. Page requests, which are posted requests, have been flushed to the IOMMU 176 + * when the stop request completes. 177 + * b. The IOMMU driver flushes all fault queues on unbind() before freeing the 178 + * PASID. 179 + * 180 + * So even though the Stop Marker might be issued by the device *after* the stop 181 + * request completes, outstanding faults will have been dealt with by the time 182 + * the PASID is freed. 183 + * 184 + * Return: 0 on success and <0 on error. 185 + */ 186 + int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) 187 + { 188 + int ret; 189 + struct iopf_group *group; 190 + struct iopf_fault *iopf, *next; 191 + struct iopf_device_param *iopf_param; 192 + 193 + struct device *dev = cookie; 194 + struct dev_iommu *param = dev->iommu; 195 + 196 + lockdep_assert_held(&param->lock); 197 + 198 + if (fault->type != IOMMU_FAULT_PAGE_REQ) 199 + /* Not a recoverable page fault */ 200 + return -EOPNOTSUPP; 201 + 202 + /* 203 + * As long as we're holding param->lock, the queue can't be unlinked 204 + * from the device and therefore cannot disappear. 205 + */ 206 + iopf_param = param->iopf_param; 207 + if (!iopf_param) 208 + return -ENODEV; 209 + 210 + if (!(fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) { 211 + iopf = kzalloc(sizeof(*iopf), GFP_KERNEL); 212 + if (!iopf) 213 + return -ENOMEM; 214 + 215 + iopf->fault = *fault; 216 + 217 + /* Non-last request of a group. Postpone until the last one */ 218 + list_add(&iopf->list, &iopf_param->partial); 219 + 220 + return 0; 221 + } 222 + 223 + group = kzalloc(sizeof(*group), GFP_KERNEL); 224 + if (!group) { 225 + /* 226 + * The caller will send a response to the hardware. But we do 227 + * need to clean up before leaving, otherwise partial faults 228 + * will be stuck. 229 + */ 230 + ret = -ENOMEM; 231 + goto cleanup_partial; 232 + } 233 + 234 + group->dev = dev; 235 + group->last_fault.fault = *fault; 236 + INIT_LIST_HEAD(&group->faults); 237 + list_add(&group->last_fault.list, &group->faults); 238 + INIT_WORK(&group->work, iopf_handle_group); 239 + 240 + /* See if we have partial faults for this group */ 241 + list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) { 242 + if (iopf->fault.prm.grpid == fault->prm.grpid) 243 + /* Insert *before* the last fault */ 244 + list_move(&iopf->list, &group->faults); 245 + } 246 + 247 + queue_work(iopf_param->queue->wq, &group->work); 248 + return 0; 249 + 250 + cleanup_partial: 251 + list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) { 252 + if (iopf->fault.prm.grpid == fault->prm.grpid) { 253 + list_del(&iopf->list); 254 + kfree(iopf); 255 + } 256 + } 257 + return ret; 258 + } 259 + EXPORT_SYMBOL_GPL(iommu_queue_iopf); 260 + 261 + /** 262 + * iopf_queue_flush_dev - Ensure that all queued faults have been processed 263 + * @dev: the endpoint whose faults need to be flushed. 264 + * 265 + * The IOMMU driver calls this before releasing a PASID, to ensure that all 266 + * pending faults for this PASID have been handled, and won't hit the address 267 + * space of the next process that uses this PASID. The driver must make sure 268 + * that no new fault is added to the queue. In particular it must flush its 269 + * low-level queue before calling this function. 270 + * 271 + * Return: 0 on success and <0 on error. 272 + */ 273 + int iopf_queue_flush_dev(struct device *dev) 274 + { 275 + int ret = 0; 276 + struct iopf_device_param *iopf_param; 277 + struct dev_iommu *param = dev->iommu; 278 + 279 + if (!param) 280 + return -ENODEV; 281 + 282 + mutex_lock(&param->lock); 283 + iopf_param = param->iopf_param; 284 + if (iopf_param) 285 + flush_workqueue(iopf_param->queue->wq); 286 + else 287 + ret = -ENODEV; 288 + mutex_unlock(&param->lock); 289 + 290 + return ret; 291 + } 292 + EXPORT_SYMBOL_GPL(iopf_queue_flush_dev); 293 + 294 + /** 295 + * iopf_queue_discard_partial - Remove all pending partial fault 296 + * @queue: the queue whose partial faults need to be discarded 297 + * 298 + * When the hardware queue overflows, last page faults in a group may have been 299 + * lost and the IOMMU driver calls this to discard all partial faults. The 300 + * driver shouldn't be adding new faults to this queue concurrently. 301 + * 302 + * Return: 0 on success and <0 on error. 303 + */ 304 + int iopf_queue_discard_partial(struct iopf_queue *queue) 305 + { 306 + struct iopf_fault *iopf, *next; 307 + struct iopf_device_param *iopf_param; 308 + 309 + if (!queue) 310 + return -EINVAL; 311 + 312 + mutex_lock(&queue->lock); 313 + list_for_each_entry(iopf_param, &queue->devices, queue_list) { 314 + list_for_each_entry_safe(iopf, next, &iopf_param->partial, 315 + list) { 316 + list_del(&iopf->list); 317 + kfree(iopf); 318 + } 319 + } 320 + mutex_unlock(&queue->lock); 321 + return 0; 322 + } 323 + EXPORT_SYMBOL_GPL(iopf_queue_discard_partial); 324 + 325 + /** 326 + * iopf_queue_add_device - Add producer to the fault queue 327 + * @queue: IOPF queue 328 + * @dev: device to add 329 + * 330 + * Return: 0 on success and <0 on error. 331 + */ 332 + int iopf_queue_add_device(struct iopf_queue *queue, struct device *dev) 333 + { 334 + int ret = -EBUSY; 335 + struct iopf_device_param *iopf_param; 336 + struct dev_iommu *param = dev->iommu; 337 + 338 + if (!param) 339 + return -ENODEV; 340 + 341 + iopf_param = kzalloc(sizeof(*iopf_param), GFP_KERNEL); 342 + if (!iopf_param) 343 + return -ENOMEM; 344 + 345 + INIT_LIST_HEAD(&iopf_param->partial); 346 + iopf_param->queue = queue; 347 + iopf_param->dev = dev; 348 + 349 + mutex_lock(&queue->lock); 350 + mutex_lock(&param->lock); 351 + if (!param->iopf_param) { 352 + list_add(&iopf_param->queue_list, &queue->devices); 353 + param->iopf_param = iopf_param; 354 + ret = 0; 355 + } 356 + mutex_unlock(&param->lock); 357 + mutex_unlock(&queue->lock); 358 + 359 + if (ret) 360 + kfree(iopf_param); 361 + 362 + return ret; 363 + } 364 + EXPORT_SYMBOL_GPL(iopf_queue_add_device); 365 + 366 + /** 367 + * iopf_queue_remove_device - Remove producer from fault queue 368 + * @queue: IOPF queue 369 + * @dev: device to remove 370 + * 371 + * Caller makes sure that no more faults are reported for this device. 372 + * 373 + * Return: 0 on success and <0 on error. 374 + */ 375 + int iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) 376 + { 377 + int ret = -EINVAL; 378 + struct iopf_fault *iopf, *next; 379 + struct iopf_device_param *iopf_param; 380 + struct dev_iommu *param = dev->iommu; 381 + 382 + if (!param || !queue) 383 + return -EINVAL; 384 + 385 + mutex_lock(&queue->lock); 386 + mutex_lock(&param->lock); 387 + iopf_param = param->iopf_param; 388 + if (iopf_param && iopf_param->queue == queue) { 389 + list_del(&iopf_param->queue_list); 390 + param->iopf_param = NULL; 391 + ret = 0; 392 + } 393 + mutex_unlock(&param->lock); 394 + mutex_unlock(&queue->lock); 395 + if (ret) 396 + return ret; 397 + 398 + /* Just in case some faults are still stuck */ 399 + list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) 400 + kfree(iopf); 401 + 402 + kfree(iopf_param); 403 + 404 + return 0; 405 + } 406 + EXPORT_SYMBOL_GPL(iopf_queue_remove_device); 407 + 408 + /** 409 + * iopf_queue_alloc - Allocate and initialize a fault queue 410 + * @name: a unique string identifying the queue (for workqueue) 411 + * 412 + * Return: the queue on success and NULL on error. 413 + */ 414 + struct iopf_queue *iopf_queue_alloc(const char *name) 415 + { 416 + struct iopf_queue *queue; 417 + 418 + queue = kzalloc(sizeof(*queue), GFP_KERNEL); 419 + if (!queue) 420 + return NULL; 421 + 422 + /* 423 + * The WQ is unordered because the low-level handler enqueues faults by 424 + * group. PRI requests within a group have to be ordered, but once 425 + * that's dealt with, the high-level function can handle groups out of 426 + * order. 427 + */ 428 + queue->wq = alloc_workqueue("iopf_queue/%s", WQ_UNBOUND, 0, name); 429 + if (!queue->wq) { 430 + kfree(queue); 431 + return NULL; 432 + } 433 + 434 + INIT_LIST_HEAD(&queue->devices); 435 + mutex_init(&queue->lock); 436 + 437 + return queue; 438 + } 439 + EXPORT_SYMBOL_GPL(iopf_queue_alloc); 440 + 441 + /** 442 + * iopf_queue_free - Free IOPF queue 443 + * @queue: queue to free 444 + * 445 + * Counterpart to iopf_queue_alloc(). The driver must not be queuing faults or 446 + * adding/removing devices on this queue anymore. 447 + */ 448 + void iopf_queue_free(struct iopf_queue *queue) 449 + { 450 + struct iopf_device_param *iopf_param, *next; 451 + 452 + if (!queue) 453 + return; 454 + 455 + list_for_each_entry_safe(iopf_param, next, &queue->devices, queue_list) 456 + iopf_queue_remove_device(queue, iopf_param->dev); 457 + 458 + destroy_workqueue(queue->wq); 459 + kfree(queue); 460 + } 461 + EXPORT_SYMBOL_GPL(iopf_queue_free);
+53
drivers/iommu/iommu-sva-lib.h
··· 12 12 void iommu_sva_free_pasid(struct mm_struct *mm); 13 13 struct mm_struct *iommu_sva_find(ioasid_t pasid); 14 14 15 + /* I/O Page fault */ 16 + struct device; 17 + struct iommu_fault; 18 + struct iopf_queue; 19 + 20 + #ifdef CONFIG_IOMMU_SVA_LIB 21 + int iommu_queue_iopf(struct iommu_fault *fault, void *cookie); 22 + 23 + int iopf_queue_add_device(struct iopf_queue *queue, struct device *dev); 24 + int iopf_queue_remove_device(struct iopf_queue *queue, 25 + struct device *dev); 26 + int iopf_queue_flush_dev(struct device *dev); 27 + struct iopf_queue *iopf_queue_alloc(const char *name); 28 + void iopf_queue_free(struct iopf_queue *queue); 29 + int iopf_queue_discard_partial(struct iopf_queue *queue); 30 + 31 + #else /* CONFIG_IOMMU_SVA_LIB */ 32 + static inline int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) 33 + { 34 + return -ENODEV; 35 + } 36 + 37 + static inline int iopf_queue_add_device(struct iopf_queue *queue, 38 + struct device *dev) 39 + { 40 + return -ENODEV; 41 + } 42 + 43 + static inline int iopf_queue_remove_device(struct iopf_queue *queue, 44 + struct device *dev) 45 + { 46 + return -ENODEV; 47 + } 48 + 49 + static inline int iopf_queue_flush_dev(struct device *dev) 50 + { 51 + return -ENODEV; 52 + } 53 + 54 + static inline struct iopf_queue *iopf_queue_alloc(const char *name) 55 + { 56 + return NULL; 57 + } 58 + 59 + static inline void iopf_queue_free(struct iopf_queue *queue) 60 + { 61 + } 62 + 63 + static inline int iopf_queue_discard_partial(struct iopf_queue *queue) 64 + { 65 + return -ENODEV; 66 + } 67 + #endif /* CONFIG_IOMMU_SVA_LIB */ 15 68 #endif /* _IOMMU_SVA_LIB_H */
+74 -87
drivers/iommu/iommu.c
··· 69 69 }; 70 70 71 71 #define IOMMU_CMD_LINE_DMA_API BIT(0) 72 - 73 - static void iommu_set_cmd_line_dma_api(void) 74 - { 75 - iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; 76 - } 77 - 78 - static bool iommu_cmd_line_dma_api(void) 79 - { 80 - return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API); 81 - } 72 + #define IOMMU_CMD_LINE_STRICT BIT(1) 82 73 83 74 static int iommu_alloc_default_domain(struct iommu_group *group, 84 75 struct device *dev); ··· 121 130 122 131 static int __init iommu_subsys_init(void) 123 132 { 124 - bool cmd_line = iommu_cmd_line_dma_api(); 125 - 126 - if (!cmd_line) { 133 + if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) { 127 134 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH)) 128 135 iommu_set_default_passthrough(false); 129 136 else ··· 135 146 136 147 pr_info("Default domain type: %s %s\n", 137 148 iommu_domain_type_str(iommu_def_domain_type), 138 - cmd_line ? "(set via kernel command line)" : ""); 149 + (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ? 150 + "(set via kernel command line)" : ""); 139 151 140 152 return 0; 141 153 } 142 154 subsys_initcall(iommu_subsys_init); 143 155 144 - int iommu_device_register(struct iommu_device *iommu) 156 + /** 157 + * iommu_device_register() - Register an IOMMU hardware instance 158 + * @iommu: IOMMU handle for the instance 159 + * @ops: IOMMU ops to associate with the instance 160 + * @hwdev: (optional) actual instance device, used for fwnode lookup 161 + * 162 + * Return: 0 on success, or an error. 163 + */ 164 + int iommu_device_register(struct iommu_device *iommu, 165 + const struct iommu_ops *ops, struct device *hwdev) 145 166 { 167 + /* We need to be able to take module references appropriately */ 168 + if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner)) 169 + return -EINVAL; 170 + 171 + iommu->ops = ops; 172 + if (hwdev) 173 + iommu->fwnode = hwdev->fwnode; 174 + 146 175 spin_lock(&iommu_device_lock); 147 176 list_add_tail(&iommu->list, &iommu_device_list); 148 177 spin_unlock(&iommu_device_lock); ··· 336 329 337 330 static int __init iommu_dma_setup(char *str) 338 331 { 339 - return kstrtobool(str, &iommu_dma_strict); 332 + int ret = kstrtobool(str, &iommu_dma_strict); 333 + 334 + if (!ret) 335 + iommu_cmd_line |= IOMMU_CMD_LINE_STRICT; 336 + return ret; 340 337 } 341 338 early_param("iommu.strict", iommu_dma_setup); 339 + 340 + void iommu_set_dma_strict(bool strict) 341 + { 342 + if (strict || !(iommu_cmd_line & IOMMU_CMD_LINE_STRICT)) 343 + iommu_dma_strict = strict; 344 + } 345 + 346 + bool iommu_get_dma_strict(struct iommu_domain *domain) 347 + { 348 + /* only allow lazy flushing for DMA domains */ 349 + if (domain->type == IOMMU_DOMAIN_DMA) 350 + return iommu_dma_strict; 351 + return true; 352 + } 353 + EXPORT_SYMBOL_GPL(iommu_get_dma_strict); 342 354 343 355 static ssize_t iommu_group_attr_show(struct kobject *kobj, 344 356 struct attribute *__attr, char *buf) ··· 1537 1511 group->default_domain = dom; 1538 1512 if (!group->domain) 1539 1513 group->domain = dom; 1540 - 1541 - if (!iommu_dma_strict) { 1542 - int attr = 1; 1543 - iommu_domain_set_attr(dom, 1544 - DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, 1545 - &attr); 1546 - } 1547 - 1548 1514 return 0; 1549 1515 } 1550 1516 ··· 2628 2610 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC); 2629 2611 } 2630 2612 2631 - int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, 2632 - phys_addr_t paddr, u64 size, int prot) 2633 - { 2634 - if (unlikely(domain->ops->domain_window_enable == NULL)) 2635 - return -ENODEV; 2636 - 2637 - return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, 2638 - prot); 2639 - } 2640 - EXPORT_SYMBOL_GPL(iommu_domain_window_enable); 2641 - 2642 2613 /** 2643 2614 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework 2644 2615 * @domain: the iommu domain where the fault has happened ··· 2682 2675 } 2683 2676 core_initcall(iommu_init); 2684 2677 2685 - int iommu_domain_get_attr(struct iommu_domain *domain, 2686 - enum iommu_attr attr, void *data) 2678 + int iommu_enable_nesting(struct iommu_domain *domain) 2687 2679 { 2688 - struct iommu_domain_geometry *geometry; 2689 - bool *paging; 2690 - int ret = 0; 2691 - 2692 - switch (attr) { 2693 - case DOMAIN_ATTR_GEOMETRY: 2694 - geometry = data; 2695 - *geometry = domain->geometry; 2696 - 2697 - break; 2698 - case DOMAIN_ATTR_PAGING: 2699 - paging = data; 2700 - *paging = (domain->pgsize_bitmap != 0UL); 2701 - break; 2702 - default: 2703 - if (!domain->ops->domain_get_attr) 2704 - return -EINVAL; 2705 - 2706 - ret = domain->ops->domain_get_attr(domain, attr, data); 2707 - } 2708 - 2709 - return ret; 2680 + if (domain->type != IOMMU_DOMAIN_UNMANAGED) 2681 + return -EINVAL; 2682 + if (!domain->ops->enable_nesting) 2683 + return -EINVAL; 2684 + return domain->ops->enable_nesting(domain); 2710 2685 } 2711 - EXPORT_SYMBOL_GPL(iommu_domain_get_attr); 2686 + EXPORT_SYMBOL_GPL(iommu_enable_nesting); 2712 2687 2713 - int iommu_domain_set_attr(struct iommu_domain *domain, 2714 - enum iommu_attr attr, void *data) 2688 + int iommu_set_pgtable_quirks(struct iommu_domain *domain, 2689 + unsigned long quirk) 2715 2690 { 2716 - int ret = 0; 2717 - 2718 - switch (attr) { 2719 - default: 2720 - if (domain->ops->domain_set_attr == NULL) 2721 - return -EINVAL; 2722 - 2723 - ret = domain->ops->domain_set_attr(domain, attr, data); 2724 - } 2725 - 2726 - return ret; 2691 + if (domain->type != IOMMU_DOMAIN_UNMANAGED) 2692 + return -EINVAL; 2693 + if (!domain->ops->set_pgtable_quirks) 2694 + return -EINVAL; 2695 + return domain->ops->set_pgtable_quirks(domain, quirk); 2727 2696 } 2728 - EXPORT_SYMBOL_GPL(iommu_domain_set_attr); 2697 + EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); 2729 2698 2730 2699 void iommu_get_resv_regions(struct device *dev, struct list_head *list) 2731 2700 { ··· 2760 2777 void iommu_set_default_passthrough(bool cmd_line) 2761 2778 { 2762 2779 if (cmd_line) 2763 - iommu_set_cmd_line_dma_api(); 2764 - 2780 + iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; 2765 2781 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY; 2766 2782 } 2767 2783 2768 2784 void iommu_set_default_translated(bool cmd_line) 2769 2785 { 2770 2786 if (cmd_line) 2771 - iommu_set_cmd_line_dma_api(); 2772 - 2787 + iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; 2773 2788 iommu_def_domain_type = IOMMU_DOMAIN_DMA; 2774 2789 } 2775 2790 ··· 2859 2878 */ 2860 2879 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) 2861 2880 { 2862 - const struct iommu_ops *ops = dev->bus->iommu_ops; 2881 + if (dev->iommu && dev->iommu->iommu_dev) { 2882 + const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; 2863 2883 2864 - if (ops && ops->dev_enable_feat) 2865 - return ops->dev_enable_feat(dev, feat); 2884 + if (ops->dev_enable_feat) 2885 + return ops->dev_enable_feat(dev, feat); 2886 + } 2866 2887 2867 2888 return -ENODEV; 2868 2889 } ··· 2877 2894 */ 2878 2895 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat) 2879 2896 { 2880 - const struct iommu_ops *ops = dev->bus->iommu_ops; 2897 + if (dev->iommu && dev->iommu->iommu_dev) { 2898 + const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; 2881 2899 2882 - if (ops && ops->dev_disable_feat) 2883 - return ops->dev_disable_feat(dev, feat); 2900 + if (ops->dev_disable_feat) 2901 + return ops->dev_disable_feat(dev, feat); 2902 + } 2884 2903 2885 2904 return -EBUSY; 2886 2905 } ··· 2890 2905 2891 2906 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat) 2892 2907 { 2893 - const struct iommu_ops *ops = dev->bus->iommu_ops; 2908 + if (dev->iommu && dev->iommu->iommu_dev) { 2909 + const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; 2894 2910 2895 - if (ops && ops->dev_feat_enabled) 2896 - return ops->dev_feat_enabled(dev, feat); 2911 + if (ops->dev_feat_enabled) 2912 + return ops->dev_feat_enabled(dev, feat); 2913 + } 2897 2914 2898 2915 return false; 2899 2916 }
+83 -13
drivers/iommu/iova.c
··· 22 22 unsigned long size, 23 23 unsigned long limit_pfn); 24 24 static void init_iova_rcaches(struct iova_domain *iovad); 25 + static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad); 25 26 static void free_iova_rcaches(struct iova_domain *iovad); 26 27 static void fq_destroy_all_entries(struct iova_domain *iovad); 27 28 static void fq_flush_timeout(struct timer_list *t); 29 + 30 + static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node) 31 + { 32 + struct iova_domain *iovad; 33 + 34 + iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead); 35 + 36 + free_cpu_cached_iovas(cpu, iovad); 37 + return 0; 38 + } 39 + 28 40 static void free_global_cached_iovas(struct iova_domain *iovad); 41 + 42 + static struct iova *to_iova(struct rb_node *node) 43 + { 44 + return rb_entry(node, struct iova, node); 45 + } 29 46 30 47 void 31 48 init_iova_domain(struct iova_domain *iovad, unsigned long granule, ··· 68 51 iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR; 69 52 rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node); 70 53 rb_insert_color(&iovad->anchor.node, &iovad->rbroot); 54 + cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD, &iovad->cpuhp_dead); 71 55 init_iova_rcaches(iovad); 72 56 } 73 57 EXPORT_SYMBOL_GPL(init_iova_domain); ··· 154 136 { 155 137 struct iova *cached_iova; 156 138 157 - cached_iova = rb_entry(iovad->cached32_node, struct iova, node); 139 + cached_iova = to_iova(iovad->cached32_node); 158 140 if (free == cached_iova || 159 141 (free->pfn_hi < iovad->dma_32bit_pfn && 160 142 free->pfn_lo >= cached_iova->pfn_lo)) { ··· 162 144 iovad->max32_alloc_size = iovad->dma_32bit_pfn; 163 145 } 164 146 165 - cached_iova = rb_entry(iovad->cached_node, struct iova, node); 147 + cached_iova = to_iova(iovad->cached_node); 166 148 if (free->pfn_lo >= cached_iova->pfn_lo) 167 149 iovad->cached_node = rb_next(&free->node); 150 + } 151 + 152 + static struct rb_node *iova_find_limit(struct iova_domain *iovad, unsigned long limit_pfn) 153 + { 154 + struct rb_node *node, *next; 155 + /* 156 + * Ideally what we'd like to judge here is whether limit_pfn is close 157 + * enough to the highest-allocated IOVA that starting the allocation 158 + * walk from the anchor node will be quicker than this initial work to 159 + * find an exact starting point (especially if that ends up being the 160 + * anchor node anyway). This is an incredibly crude approximation which 161 + * only really helps the most likely case, but is at least trivially easy. 162 + */ 163 + if (limit_pfn > iovad->dma_32bit_pfn) 164 + return &iovad->anchor.node; 165 + 166 + node = iovad->rbroot.rb_node; 167 + while (to_iova(node)->pfn_hi < limit_pfn) 168 + node = node->rb_right; 169 + 170 + search_left: 171 + while (node->rb_left && to_iova(node->rb_left)->pfn_lo >= limit_pfn) 172 + node = node->rb_left; 173 + 174 + if (!node->rb_left) 175 + return node; 176 + 177 + next = node->rb_left; 178 + while (next->rb_right) { 179 + next = next->rb_right; 180 + if (to_iova(next)->pfn_lo >= limit_pfn) { 181 + node = next; 182 + goto search_left; 183 + } 184 + } 185 + 186 + return node; 168 187 } 169 188 170 189 /* Insert the iova into domain rbtree by holding writer lock */ ··· 214 159 new = (start) ? &start : &(root->rb_node); 215 160 /* Figure out where to put new node */ 216 161 while (*new) { 217 - struct iova *this = rb_entry(*new, struct iova, node); 162 + struct iova *this = to_iova(*new); 218 163 219 164 parent = *new; 220 165 ··· 253 198 goto iova32_full; 254 199 255 200 curr = __get_cached_rbnode(iovad, limit_pfn); 256 - curr_iova = rb_entry(curr, struct iova, node); 201 + curr_iova = to_iova(curr); 257 202 retry_pfn = curr_iova->pfn_hi + 1; 258 203 259 204 retry: ··· 262 207 new_pfn = (high_pfn - size) & align_mask; 263 208 prev = curr; 264 209 curr = rb_prev(curr); 265 - curr_iova = rb_entry(curr, struct iova, node); 210 + curr_iova = to_iova(curr); 266 211 } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn); 267 212 268 213 if (high_pfn < size || new_pfn < low_pfn) { 269 214 if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) { 270 215 high_pfn = limit_pfn; 271 216 low_pfn = retry_pfn; 272 - curr = &iovad->anchor.node; 273 - curr_iova = rb_entry(curr, struct iova, node); 217 + curr = iova_find_limit(iovad, limit_pfn); 218 + curr_iova = to_iova(curr); 274 219 goto retry; 275 220 } 276 221 iovad->max32_alloc_size = size; ··· 312 257 { 313 258 mutex_lock(&iova_cache_mutex); 314 259 if (!iova_cache_users) { 260 + int ret; 261 + 262 + ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL, 263 + iova_cpuhp_dead); 264 + if (ret) { 265 + mutex_unlock(&iova_cache_mutex); 266 + pr_err("Couldn't register cpuhp handler\n"); 267 + return ret; 268 + } 269 + 315 270 iova_cache = kmem_cache_create( 316 271 "iommu_iova", sizeof(struct iova), 0, 317 272 SLAB_HWCACHE_ALIGN, NULL); 318 273 if (!iova_cache) { 274 + cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD); 319 275 mutex_unlock(&iova_cache_mutex); 320 276 pr_err("Couldn't create iova cache\n"); 321 277 return -ENOMEM; ··· 348 282 return; 349 283 } 350 284 iova_cache_users--; 351 - if (!iova_cache_users) 285 + if (!iova_cache_users) { 286 + cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD); 352 287 kmem_cache_destroy(iova_cache); 288 + } 353 289 mutex_unlock(&iova_cache_mutex); 354 290 } 355 291 EXPORT_SYMBOL_GPL(iova_cache_put); ··· 399 331 assert_spin_locked(&iovad->iova_rbtree_lock); 400 332 401 333 while (node) { 402 - struct iova *iova = rb_entry(node, struct iova, node); 334 + struct iova *iova = to_iova(node); 403 335 404 336 if (pfn < iova->pfn_lo) 405 337 node = node->rb_left; ··· 535 467 536 468 free_iova(iovad, pfn); 537 469 } 538 - EXPORT_SYMBOL_GPL(free_iova_fast); 539 470 540 471 #define fq_ring_for_each(i, fq) \ 541 472 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE) ··· 673 606 { 674 607 struct iova *iova, *tmp; 675 608 609 + cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD, 610 + &iovad->cpuhp_dead); 611 + 676 612 free_iova_flush_queue(iovad); 677 613 free_iova_rcaches(iovad); 678 614 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node) ··· 687 617 __is_range_overlap(struct rb_node *node, 688 618 unsigned long pfn_lo, unsigned long pfn_hi) 689 619 { 690 - struct iova *iova = rb_entry(node, struct iova, node); 620 + struct iova *iova = to_iova(node); 691 621 692 622 if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo)) 693 623 return 1; ··· 755 685 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); 756 686 for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) { 757 687 if (__is_range_overlap(node, pfn_lo, pfn_hi)) { 758 - iova = rb_entry(node, struct iova, node); 688 + iova = to_iova(node); 759 689 __adjust_overlap_range(iova, &pfn_lo, &pfn_hi); 760 690 if ((pfn_lo >= iova->pfn_lo) && 761 691 (pfn_hi <= iova->pfn_hi)) ··· 1040 970 /* 1041 971 * free all the IOVA ranges cached by a cpu (used when cpu is unplugged) 1042 972 */ 1043 - void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad) 973 + static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad) 1044 974 { 1045 975 struct iova_cpu_rcache *cpu_rcache; 1046 976 struct iova_rcache *rcache;
+1 -5
drivers/iommu/ipmmu-vmsa.c
··· 1076 1076 if (ret) 1077 1077 return ret; 1078 1078 1079 - iommu_device_set_ops(&mmu->iommu, &ipmmu_ops); 1080 - iommu_device_set_fwnode(&mmu->iommu, 1081 - &pdev->dev.of_node->fwnode); 1082 - 1083 - ret = iommu_device_register(&mmu->iommu); 1079 + ret = iommu_device_register(&mmu->iommu, &ipmmu_ops, &pdev->dev); 1084 1080 if (ret) 1085 1081 return ret; 1086 1082
+1 -4
drivers/iommu/msm_iommu.c
··· 792 792 goto fail; 793 793 } 794 794 795 - iommu_device_set_ops(&iommu->iommu, &msm_iommu_ops); 796 - iommu_device_set_fwnode(&iommu->iommu, &pdev->dev.of_node->fwnode); 797 - 798 - ret = iommu_device_register(&iommu->iommu); 795 + ret = iommu_device_register(&iommu->iommu, &msm_iommu_ops, &pdev->dev); 799 796 if (ret) { 800 797 pr_err("Could not register msm-smmu at %pa\n", &ioaddr); 801 798 goto fail;
+14 -27
drivers/iommu/mtk_iommu.c
··· 17 17 #include <linux/iopoll.h> 18 18 #include <linux/list.h> 19 19 #include <linux/mfd/syscon.h> 20 + #include <linux/module.h> 20 21 #include <linux/of_address.h> 21 22 #include <linux/of_iommu.h> 22 23 #include <linux/of_irq.h> ··· 684 683 .get_resv_regions = mtk_iommu_get_resv_regions, 685 684 .put_resv_regions = generic_iommu_put_resv_regions, 686 685 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M, 686 + .owner = THIS_MODULE, 687 687 }; 688 688 689 689 static int mtk_iommu_hw_init(const struct mtk_iommu_data *data) 690 690 { 691 691 u32 regval; 692 - int ret; 693 - 694 - ret = clk_prepare_enable(data->bclk); 695 - if (ret) { 696 - dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret); 697 - return ret; 698 - } 699 692 700 693 if (data->plat_data->m4u_plat == M4U_MT8173) { 701 694 regval = F_MMU_PREFETCH_RT_REPLACE_MOD | ··· 755 760 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0, 756 761 dev_name(data->dev), (void *)data)) { 757 762 writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR); 758 - clk_disable_unprepare(data->bclk); 759 763 dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq); 760 764 return -ENODEV; 761 765 } ··· 892 898 if (ret) 893 899 goto out_link_remove; 894 900 895 - iommu_device_set_ops(&data->iommu, &mtk_iommu_ops); 896 - iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode); 897 - 898 - ret = iommu_device_register(&data->iommu); 901 + ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev); 899 902 if (ret) 900 903 goto out_sysfs_remove; 901 904 ··· 968 977 void __iomem *base = data->base; 969 978 int ret; 970 979 971 - /* Avoid first resume to affect the default value of registers below. */ 972 - if (!m4u_dom) 973 - return 0; 974 980 ret = clk_prepare_enable(data->bclk); 975 981 if (ret) { 976 982 dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret); 977 983 return ret; 978 984 } 985 + 986 + /* 987 + * Uppon first resume, only enable the clk and return, since the values of the 988 + * registers are not yet set. 989 + */ 990 + if (!m4u_dom) 991 + return 0; 992 + 979 993 writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL); 980 994 writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL); 981 995 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS); ··· 1075 1079 .pm = &mtk_iommu_pm_ops, 1076 1080 } 1077 1081 }; 1082 + module_platform_driver(mtk_iommu_driver); 1078 1083 1079 - static int __init mtk_iommu_init(void) 1080 - { 1081 - int ret; 1082 - 1083 - ret = platform_driver_register(&mtk_iommu_driver); 1084 - if (ret != 0) 1085 - pr_err("Failed to register MTK IOMMU driver\n"); 1086 - 1087 - return ret; 1088 - } 1089 - 1090 - subsys_initcall(mtk_iommu_init) 1084 + MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations"); 1085 + MODULE_LICENSE("GPL v2");
+55 -49
drivers/iommu/mtk_iommu_v1.c
··· 20 20 #include <linux/iommu.h> 21 21 #include <linux/iopoll.h> 22 22 #include <linux/list.h> 23 + #include <linux/module.h> 23 24 #include <linux/of_address.h> 24 25 #include <linux/of_iommu.h> 25 26 #include <linux/of_irq.h> ··· 424 423 { 425 424 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 426 425 struct of_phandle_args iommu_spec; 427 - struct of_phandle_iterator it; 428 426 struct mtk_iommu_data *data; 429 - int err; 427 + int err, idx = 0; 430 428 431 - of_for_each_phandle(&it, err, dev->of_node, "iommus", 432 - "#iommu-cells", -1) { 433 - int count = of_phandle_iterator_args(&it, iommu_spec.args, 434 - MAX_PHANDLE_ARGS); 435 - iommu_spec.np = of_node_get(it.node); 436 - iommu_spec.args_count = count; 429 + while (!of_parse_phandle_with_args(dev->of_node, "iommus", 430 + "#iommu-cells", 431 + idx, &iommu_spec)) { 437 432 438 - mtk_iommu_create_mapping(dev, &iommu_spec); 433 + err = mtk_iommu_create_mapping(dev, &iommu_spec); 434 + of_node_put(iommu_spec.np); 435 + if (err) 436 + return ERR_PTR(err); 439 437 440 438 /* dev->iommu_fwspec might have changed */ 441 439 fwspec = dev_iommu_fwspec_get(dev); 442 - 443 - of_node_put(iommu_spec.np); 440 + idx++; 444 441 } 445 442 446 443 if (!fwspec || fwspec->ops != &mtk_iommu_ops) ··· 528 529 .def_domain_type = mtk_iommu_def_domain_type, 529 530 .device_group = generic_device_group, 530 531 .pgsize_bitmap = ~0UL << MT2701_IOMMU_PAGE_SHIFT, 532 + .owner = THIS_MODULE, 531 533 }; 532 534 533 535 static const struct of_device_id mtk_iommu_of_ids[] = { ··· 547 547 struct device *dev = &pdev->dev; 548 548 struct resource *res; 549 549 struct component_match *match = NULL; 550 - struct of_phandle_args larb_spec; 551 - struct of_phandle_iterator it; 552 550 void *protect; 553 - int larb_nr, ret, err; 551 + int larb_nr, ret, i; 554 552 555 553 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 556 554 if (!data) ··· 576 578 if (IS_ERR(data->bclk)) 577 579 return PTR_ERR(data->bclk); 578 580 579 - larb_nr = 0; 580 - of_for_each_phandle(&it, err, dev->of_node, 581 - "mediatek,larbs", NULL, 0) { 581 + larb_nr = of_count_phandle_with_args(dev->of_node, 582 + "mediatek,larbs", NULL); 583 + if (larb_nr < 0) 584 + return larb_nr; 585 + 586 + for (i = 0; i < larb_nr; i++) { 587 + struct device_node *larbnode; 582 588 struct platform_device *plarbdev; 583 - int count = of_phandle_iterator_args(&it, larb_spec.args, 584 - MAX_PHANDLE_ARGS); 585 589 586 - if (count) 590 + larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i); 591 + if (!larbnode) 592 + return -EINVAL; 593 + 594 + if (!of_device_is_available(larbnode)) { 595 + of_node_put(larbnode); 587 596 continue; 588 - 589 - larb_spec.np = of_node_get(it.node); 590 - if (!of_device_is_available(larb_spec.np)) 591 - continue; 592 - 593 - plarbdev = of_find_device_by_node(larb_spec.np); 594 - if (!plarbdev) { 595 - plarbdev = of_platform_device_create( 596 - larb_spec.np, NULL, 597 - platform_bus_type.dev_root); 598 - if (!plarbdev) { 599 - of_node_put(larb_spec.np); 600 - return -EPROBE_DEFER; 601 - } 602 597 } 603 598 604 - data->larb_imu[larb_nr].dev = &plarbdev->dev; 599 + plarbdev = of_find_device_by_node(larbnode); 600 + if (!plarbdev) { 601 + of_node_put(larbnode); 602 + return -EPROBE_DEFER; 603 + } 604 + data->larb_imu[i].dev = &plarbdev->dev; 605 + 605 606 component_match_add_release(dev, &match, release_of, 606 - compare_of, larb_spec.np); 607 - larb_nr++; 607 + compare_of, larbnode); 608 608 } 609 609 610 610 platform_set_drvdata(pdev, data); ··· 616 620 if (ret) 617 621 return ret; 618 622 619 - iommu_device_set_ops(&data->iommu, &mtk_iommu_ops); 620 - 621 - ret = iommu_device_register(&data->iommu); 623 + ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev); 622 624 if (ret) 623 - return ret; 625 + goto out_sysfs_remove; 624 626 625 - if (!iommu_present(&platform_bus_type)) 626 - bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); 627 + if (!iommu_present(&platform_bus_type)) { 628 + ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); 629 + if (ret) 630 + goto out_dev_unreg; 631 + } 627 632 628 - return component_master_add_with_match(dev, &mtk_iommu_com_ops, match); 633 + ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match); 634 + if (ret) 635 + goto out_bus_set_null; 636 + return ret; 637 + 638 + out_bus_set_null: 639 + bus_set_iommu(&platform_bus_type, NULL); 640 + out_dev_unreg: 641 + iommu_device_unregister(&data->iommu); 642 + out_sysfs_remove: 643 + iommu_device_sysfs_remove(&data->iommu); 644 + return ret; 629 645 } 630 646 631 647 static int mtk_iommu_remove(struct platform_device *pdev) ··· 699 691 .pm = &mtk_iommu_pm_ops, 700 692 } 701 693 }; 694 + module_platform_driver(mtk_iommu_driver); 702 695 703 - static int __init m4u_init(void) 704 - { 705 - return platform_driver_register(&mtk_iommu_driver); 706 - } 707 - subsys_initcall(m4u_init); 696 + MODULE_DESCRIPTION("IOMMU API for MediaTek M4U v1 implementations"); 697 + MODULE_LICENSE("GPL v2");
-5
drivers/iommu/of_iommu.c
··· 210 210 of_pci_iommu_init, &info); 211 211 } else { 212 212 err = of_iommu_configure_device(master_np, dev, id); 213 - 214 - fwspec = dev_iommu_fwspec_get(dev); 215 - if (!err && fwspec) 216 - of_property_read_u32(master_np, "pasid-num-bits", 217 - &fwspec->num_pasid_bits); 218 213 } 219 214 220 215 /*
+1 -4
drivers/iommu/omap-iommu.c
··· 1235 1235 if (err) 1236 1236 goto out_group; 1237 1237 1238 - iommu_device_set_ops(&obj->iommu, &omap_iommu_ops); 1239 - iommu_device_set_fwnode(&obj->iommu, &of->fwnode); 1240 - 1241 - err = iommu_device_register(&obj->iommu); 1238 + err = iommu_device_register(&obj->iommu, &omap_iommu_ops, &pdev->dev); 1242 1239 if (err) 1243 1240 goto out_sysfs; 1244 1241 }
+1 -4
drivers/iommu/rockchip-iommu.c
··· 1196 1196 if (err) 1197 1197 goto err_put_group; 1198 1198 1199 - iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops); 1200 - iommu_device_set_fwnode(&iommu->iommu, &dev->of_node->fwnode); 1201 - 1202 - err = iommu_device_register(&iommu->iommu); 1199 + err = iommu_device_register(&iommu->iommu, &rk_iommu_ops, dev); 1203 1200 if (err) 1204 1201 goto err_remove_sysfs; 1205 1202
+1 -3
drivers/iommu/s390-iommu.c
··· 333 333 if (rc) 334 334 goto out_err; 335 335 336 - iommu_device_set_ops(&zdev->iommu_dev, &s390_iommu_ops); 337 - 338 - rc = iommu_device_register(&zdev->iommu_dev); 336 + rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL); 339 337 if (rc) 340 338 goto out_sysfs; 341 339
+575
drivers/iommu/sprd-iommu.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Unisoc IOMMU driver 4 + * 5 + * Copyright (C) 2020 Unisoc, Inc. 6 + * Author: Chunyan Zhang <chunyan.zhang@unisoc.com> 7 + */ 8 + 9 + #include <linux/clk.h> 10 + #include <linux/device.h> 11 + #include <linux/dma-iommu.h> 12 + #include <linux/dma-mapping.h> 13 + #include <linux/errno.h> 14 + #include <linux/iommu.h> 15 + #include <linux/mfd/syscon.h> 16 + #include <linux/module.h> 17 + #include <linux/of_platform.h> 18 + #include <linux/regmap.h> 19 + #include <linux/slab.h> 20 + 21 + #define SPRD_IOMMU_PAGE_SHIFT 12 22 + #define SPRD_IOMMU_PAGE_SIZE SZ_4K 23 + 24 + #define SPRD_EX_CFG 0x0 25 + #define SPRD_IOMMU_VAOR_BYPASS BIT(4) 26 + #define SPRD_IOMMU_GATE_EN BIT(1) 27 + #define SPRD_IOMMU_EN BIT(0) 28 + #define SPRD_EX_UPDATE 0x4 29 + #define SPRD_EX_FIRST_VPN 0x8 30 + #define SPRD_EX_VPN_RANGE 0xc 31 + #define SPRD_EX_FIRST_PPN 0x10 32 + #define SPRD_EX_DEFAULT_PPN 0x14 33 + 34 + #define SPRD_IOMMU_VERSION 0x0 35 + #define SPRD_VERSION_MASK GENMASK(15, 8) 36 + #define SPRD_VERSION_SHIFT 0x8 37 + #define SPRD_VAU_CFG 0x4 38 + #define SPRD_VAU_UPDATE 0x8 39 + #define SPRD_VAU_AUTH_CFG 0xc 40 + #define SPRD_VAU_FIRST_PPN 0x10 41 + #define SPRD_VAU_DEFAULT_PPN_RD 0x14 42 + #define SPRD_VAU_DEFAULT_PPN_WR 0x18 43 + #define SPRD_VAU_FIRST_VPN 0x1c 44 + #define SPRD_VAU_VPN_RANGE 0x20 45 + 46 + enum sprd_iommu_version { 47 + SPRD_IOMMU_EX, 48 + SPRD_IOMMU_VAU, 49 + }; 50 + 51 + /* 52 + * struct sprd_iommu_device - high-level sprd IOMMU device representation, 53 + * including hardware information and configuration, also driver data, etc 54 + * 55 + * @ver: sprd IOMMU IP version 56 + * @prot_page_va: protect page base virtual address 57 + * @prot_page_pa: protect page base physical address, data would be 58 + * written to here while translation fault 59 + * @base: mapped base address for accessing registers 60 + * @dev: pointer to basic device structure 61 + * @iommu: IOMMU core representation 62 + * @group: IOMMU group 63 + * @eb: gate clock which controls IOMMU access 64 + */ 65 + struct sprd_iommu_device { 66 + enum sprd_iommu_version ver; 67 + u32 *prot_page_va; 68 + dma_addr_t prot_page_pa; 69 + void __iomem *base; 70 + struct device *dev; 71 + struct iommu_device iommu; 72 + struct iommu_group *group; 73 + struct clk *eb; 74 + }; 75 + 76 + struct sprd_iommu_domain { 77 + spinlock_t pgtlock; /* lock for page table */ 78 + struct iommu_domain domain; 79 + u32 *pgt_va; /* page table virtual address base */ 80 + dma_addr_t pgt_pa; /* page table physical address base */ 81 + struct sprd_iommu_device *sdev; 82 + }; 83 + 84 + static const struct iommu_ops sprd_iommu_ops; 85 + 86 + static struct sprd_iommu_domain *to_sprd_domain(struct iommu_domain *dom) 87 + { 88 + return container_of(dom, struct sprd_iommu_domain, domain); 89 + } 90 + 91 + static inline void 92 + sprd_iommu_write(struct sprd_iommu_device *sdev, unsigned int reg, u32 val) 93 + { 94 + writel_relaxed(val, sdev->base + reg); 95 + } 96 + 97 + static inline u32 98 + sprd_iommu_read(struct sprd_iommu_device *sdev, unsigned int reg) 99 + { 100 + return readl_relaxed(sdev->base + reg); 101 + } 102 + 103 + static inline void 104 + sprd_iommu_update_bits(struct sprd_iommu_device *sdev, unsigned int reg, 105 + u32 mask, u32 shift, u32 val) 106 + { 107 + u32 t = sprd_iommu_read(sdev, reg); 108 + 109 + t = (t & (~(mask << shift))) | ((val & mask) << shift); 110 + sprd_iommu_write(sdev, reg, t); 111 + } 112 + 113 + static inline int 114 + sprd_iommu_get_version(struct sprd_iommu_device *sdev) 115 + { 116 + int ver = (sprd_iommu_read(sdev, SPRD_IOMMU_VERSION) & 117 + SPRD_VERSION_MASK) >> SPRD_VERSION_SHIFT; 118 + 119 + switch (ver) { 120 + case SPRD_IOMMU_EX: 121 + case SPRD_IOMMU_VAU: 122 + return ver; 123 + default: 124 + return -EINVAL; 125 + } 126 + } 127 + 128 + static size_t 129 + sprd_iommu_pgt_size(struct iommu_domain *domain) 130 + { 131 + return ((domain->geometry.aperture_end - 132 + domain->geometry.aperture_start + 1) >> 133 + SPRD_IOMMU_PAGE_SHIFT) * sizeof(u32); 134 + } 135 + 136 + static struct iommu_domain *sprd_iommu_domain_alloc(unsigned int domain_type) 137 + { 138 + struct sprd_iommu_domain *dom; 139 + 140 + if (domain_type != IOMMU_DOMAIN_DMA && domain_type != IOMMU_DOMAIN_UNMANAGED) 141 + return NULL; 142 + 143 + dom = kzalloc(sizeof(*dom), GFP_KERNEL); 144 + if (!dom) 145 + return NULL; 146 + 147 + if (iommu_get_dma_cookie(&dom->domain)) { 148 + kfree(dom); 149 + return NULL; 150 + } 151 + 152 + spin_lock_init(&dom->pgtlock); 153 + 154 + dom->domain.geometry.aperture_start = 0; 155 + dom->domain.geometry.aperture_end = SZ_256M - 1; 156 + 157 + return &dom->domain; 158 + } 159 + 160 + static void sprd_iommu_domain_free(struct iommu_domain *domain) 161 + { 162 + struct sprd_iommu_domain *dom = to_sprd_domain(domain); 163 + 164 + iommu_put_dma_cookie(domain); 165 + kfree(dom); 166 + } 167 + 168 + static void sprd_iommu_first_vpn(struct sprd_iommu_domain *dom) 169 + { 170 + struct sprd_iommu_device *sdev = dom->sdev; 171 + u32 val; 172 + unsigned int reg; 173 + 174 + if (sdev->ver == SPRD_IOMMU_EX) 175 + reg = SPRD_EX_FIRST_VPN; 176 + else 177 + reg = SPRD_VAU_FIRST_VPN; 178 + 179 + val = dom->domain.geometry.aperture_start >> SPRD_IOMMU_PAGE_SHIFT; 180 + sprd_iommu_write(sdev, reg, val); 181 + } 182 + 183 + static void sprd_iommu_vpn_range(struct sprd_iommu_domain *dom) 184 + { 185 + struct sprd_iommu_device *sdev = dom->sdev; 186 + u32 val; 187 + unsigned int reg; 188 + 189 + if (sdev->ver == SPRD_IOMMU_EX) 190 + reg = SPRD_EX_VPN_RANGE; 191 + else 192 + reg = SPRD_VAU_VPN_RANGE; 193 + 194 + val = (dom->domain.geometry.aperture_end - 195 + dom->domain.geometry.aperture_start) >> SPRD_IOMMU_PAGE_SHIFT; 196 + sprd_iommu_write(sdev, reg, val); 197 + } 198 + 199 + static void sprd_iommu_first_ppn(struct sprd_iommu_domain *dom) 200 + { 201 + u32 val = dom->pgt_pa >> SPRD_IOMMU_PAGE_SHIFT; 202 + struct sprd_iommu_device *sdev = dom->sdev; 203 + unsigned int reg; 204 + 205 + if (sdev->ver == SPRD_IOMMU_EX) 206 + reg = SPRD_EX_FIRST_PPN; 207 + else 208 + reg = SPRD_VAU_FIRST_PPN; 209 + 210 + sprd_iommu_write(sdev, reg, val); 211 + } 212 + 213 + static void sprd_iommu_default_ppn(struct sprd_iommu_device *sdev) 214 + { 215 + u32 val = sdev->prot_page_pa >> SPRD_IOMMU_PAGE_SHIFT; 216 + 217 + if (sdev->ver == SPRD_IOMMU_EX) { 218 + sprd_iommu_write(sdev, SPRD_EX_DEFAULT_PPN, val); 219 + } else if (sdev->ver == SPRD_IOMMU_VAU) { 220 + sprd_iommu_write(sdev, SPRD_VAU_DEFAULT_PPN_RD, val); 221 + sprd_iommu_write(sdev, SPRD_VAU_DEFAULT_PPN_WR, val); 222 + } 223 + } 224 + 225 + static void sprd_iommu_hw_en(struct sprd_iommu_device *sdev, bool en) 226 + { 227 + unsigned int reg_cfg; 228 + u32 mask, val; 229 + 230 + if (sdev->ver == SPRD_IOMMU_EX) 231 + reg_cfg = SPRD_EX_CFG; 232 + else 233 + reg_cfg = SPRD_VAU_CFG; 234 + 235 + mask = SPRD_IOMMU_EN | SPRD_IOMMU_GATE_EN; 236 + val = en ? mask : 0; 237 + sprd_iommu_update_bits(sdev, reg_cfg, mask, 0, val); 238 + } 239 + 240 + static int sprd_iommu_attach_device(struct iommu_domain *domain, 241 + struct device *dev) 242 + { 243 + struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev); 244 + struct sprd_iommu_domain *dom = to_sprd_domain(domain); 245 + size_t pgt_size = sprd_iommu_pgt_size(domain); 246 + 247 + if (dom->sdev) { 248 + pr_err("There's already a device attached to this domain.\n"); 249 + return -EINVAL; 250 + } 251 + 252 + dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL); 253 + if (!dom->pgt_va) 254 + return -ENOMEM; 255 + 256 + dom->sdev = sdev; 257 + 258 + sprd_iommu_first_ppn(dom); 259 + sprd_iommu_first_vpn(dom); 260 + sprd_iommu_vpn_range(dom); 261 + sprd_iommu_default_ppn(sdev); 262 + sprd_iommu_hw_en(sdev, true); 263 + 264 + return 0; 265 + } 266 + 267 + static void sprd_iommu_detach_device(struct iommu_domain *domain, 268 + struct device *dev) 269 + { 270 + struct sprd_iommu_domain *dom = to_sprd_domain(domain); 271 + struct sprd_iommu_device *sdev = dom->sdev; 272 + size_t pgt_size = sprd_iommu_pgt_size(domain); 273 + 274 + if (!sdev) 275 + return; 276 + 277 + dma_free_coherent(sdev->dev, pgt_size, dom->pgt_va, dom->pgt_pa); 278 + sprd_iommu_hw_en(sdev, false); 279 + dom->sdev = NULL; 280 + } 281 + 282 + static int sprd_iommu_map(struct iommu_domain *domain, unsigned long iova, 283 + phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 284 + { 285 + struct sprd_iommu_domain *dom = to_sprd_domain(domain); 286 + unsigned int page_num = size >> SPRD_IOMMU_PAGE_SHIFT; 287 + unsigned long flags; 288 + unsigned int i; 289 + u32 *pgt_base_iova; 290 + u32 pabase = (u32)paddr; 291 + unsigned long start = domain->geometry.aperture_start; 292 + unsigned long end = domain->geometry.aperture_end; 293 + 294 + if (!dom->sdev) { 295 + pr_err("No sprd_iommu_device attached to the domain\n"); 296 + return -EINVAL; 297 + } 298 + 299 + if (iova < start || (iova + size) > (end + 1)) { 300 + dev_err(dom->sdev->dev, "(iova(0x%lx) + size(%zx)) are not in the range!\n", 301 + iova, size); 302 + return -EINVAL; 303 + } 304 + 305 + pgt_base_iova = dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT); 306 + 307 + spin_lock_irqsave(&dom->pgtlock, flags); 308 + for (i = 0; i < page_num; i++) { 309 + pgt_base_iova[i] = pabase >> SPRD_IOMMU_PAGE_SHIFT; 310 + pabase += SPRD_IOMMU_PAGE_SIZE; 311 + } 312 + spin_unlock_irqrestore(&dom->pgtlock, flags); 313 + 314 + return 0; 315 + } 316 + 317 + static size_t sprd_iommu_unmap(struct iommu_domain *domain, unsigned long iova, 318 + size_t size, struct iommu_iotlb_gather *iotlb_gather) 319 + { 320 + struct sprd_iommu_domain *dom = to_sprd_domain(domain); 321 + unsigned long flags; 322 + u32 *pgt_base_iova; 323 + unsigned int page_num = size >> SPRD_IOMMU_PAGE_SHIFT; 324 + unsigned long start = domain->geometry.aperture_start; 325 + unsigned long end = domain->geometry.aperture_end; 326 + 327 + if (iova < start || (iova + size) > (end + 1)) 328 + return -EINVAL; 329 + 330 + pgt_base_iova = dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT); 331 + 332 + spin_lock_irqsave(&dom->pgtlock, flags); 333 + memset(pgt_base_iova, 0, page_num * sizeof(u32)); 334 + spin_unlock_irqrestore(&dom->pgtlock, flags); 335 + 336 + return 0; 337 + } 338 + 339 + static void sprd_iommu_sync_map(struct iommu_domain *domain, 340 + unsigned long iova, size_t size) 341 + { 342 + struct sprd_iommu_domain *dom = to_sprd_domain(domain); 343 + unsigned int reg; 344 + 345 + if (dom->sdev->ver == SPRD_IOMMU_EX) 346 + reg = SPRD_EX_UPDATE; 347 + else 348 + reg = SPRD_VAU_UPDATE; 349 + 350 + /* clear IOMMU TLB buffer after page table updated */ 351 + sprd_iommu_write(dom->sdev, reg, 0xffffffff); 352 + } 353 + 354 + static void sprd_iommu_sync(struct iommu_domain *domain, 355 + struct iommu_iotlb_gather *iotlb_gather) 356 + { 357 + sprd_iommu_sync_map(domain, 0, 0); 358 + } 359 + 360 + static phys_addr_t sprd_iommu_iova_to_phys(struct iommu_domain *domain, 361 + dma_addr_t iova) 362 + { 363 + struct sprd_iommu_domain *dom = to_sprd_domain(domain); 364 + unsigned long flags; 365 + phys_addr_t pa; 366 + unsigned long start = domain->geometry.aperture_start; 367 + unsigned long end = domain->geometry.aperture_end; 368 + 369 + if (WARN_ON(iova < start || iova > end)) 370 + return 0; 371 + 372 + spin_lock_irqsave(&dom->pgtlock, flags); 373 + pa = *(dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT)); 374 + pa = (pa << SPRD_IOMMU_PAGE_SHIFT) + ((iova - start) & (SPRD_IOMMU_PAGE_SIZE - 1)); 375 + spin_unlock_irqrestore(&dom->pgtlock, flags); 376 + 377 + return pa; 378 + } 379 + 380 + static struct iommu_device *sprd_iommu_probe_device(struct device *dev) 381 + { 382 + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 383 + struct sprd_iommu_device *sdev; 384 + 385 + if (!fwspec || fwspec->ops != &sprd_iommu_ops) 386 + return ERR_PTR(-ENODEV); 387 + 388 + sdev = dev_iommu_priv_get(dev); 389 + 390 + return &sdev->iommu; 391 + } 392 + 393 + static void sprd_iommu_release_device(struct device *dev) 394 + { 395 + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 396 + 397 + if (!fwspec || fwspec->ops != &sprd_iommu_ops) 398 + return; 399 + 400 + iommu_fwspec_free(dev); 401 + } 402 + 403 + static struct iommu_group *sprd_iommu_device_group(struct device *dev) 404 + { 405 + struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev); 406 + 407 + return iommu_group_ref_get(sdev->group); 408 + } 409 + 410 + static int sprd_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) 411 + { 412 + struct platform_device *pdev; 413 + 414 + if (!dev_iommu_priv_get(dev)) { 415 + pdev = of_find_device_by_node(args->np); 416 + dev_iommu_priv_set(dev, platform_get_drvdata(pdev)); 417 + platform_device_put(pdev); 418 + } 419 + 420 + return 0; 421 + } 422 + 423 + 424 + static const struct iommu_ops sprd_iommu_ops = { 425 + .domain_alloc = sprd_iommu_domain_alloc, 426 + .domain_free = sprd_iommu_domain_free, 427 + .attach_dev = sprd_iommu_attach_device, 428 + .detach_dev = sprd_iommu_detach_device, 429 + .map = sprd_iommu_map, 430 + .unmap = sprd_iommu_unmap, 431 + .iotlb_sync_map = sprd_iommu_sync_map, 432 + .iotlb_sync = sprd_iommu_sync, 433 + .iova_to_phys = sprd_iommu_iova_to_phys, 434 + .probe_device = sprd_iommu_probe_device, 435 + .release_device = sprd_iommu_release_device, 436 + .device_group = sprd_iommu_device_group, 437 + .of_xlate = sprd_iommu_of_xlate, 438 + .pgsize_bitmap = ~0UL << SPRD_IOMMU_PAGE_SHIFT, 439 + .owner = THIS_MODULE, 440 + }; 441 + 442 + static const struct of_device_id sprd_iommu_of_match[] = { 443 + { .compatible = "sprd,iommu-v1" }, 444 + { }, 445 + }; 446 + MODULE_DEVICE_TABLE(of, sprd_iommu_of_match); 447 + 448 + /* 449 + * Clock is not required, access to some of IOMMUs is controlled by gate 450 + * clk, enabled clocks for that kind of IOMMUs before accessing. 451 + * Return 0 for success or no clocks found. 452 + */ 453 + static int sprd_iommu_clk_enable(struct sprd_iommu_device *sdev) 454 + { 455 + struct clk *eb; 456 + 457 + eb = devm_clk_get_optional(sdev->dev, NULL); 458 + if (!eb) 459 + return 0; 460 + 461 + if (IS_ERR(eb)) 462 + return PTR_ERR(eb); 463 + 464 + sdev->eb = eb; 465 + return clk_prepare_enable(eb); 466 + } 467 + 468 + static void sprd_iommu_clk_disable(struct sprd_iommu_device *sdev) 469 + { 470 + if (sdev->eb) 471 + clk_disable_unprepare(sdev->eb); 472 + } 473 + 474 + static int sprd_iommu_probe(struct platform_device *pdev) 475 + { 476 + struct sprd_iommu_device *sdev; 477 + struct device *dev = &pdev->dev; 478 + void __iomem *base; 479 + int ret; 480 + 481 + sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL); 482 + if (!sdev) 483 + return -ENOMEM; 484 + 485 + base = devm_platform_ioremap_resource(pdev, 0); 486 + if (IS_ERR(base)) { 487 + dev_err(dev, "Failed to get ioremap resource.\n"); 488 + return PTR_ERR(base); 489 + } 490 + sdev->base = base; 491 + 492 + sdev->prot_page_va = dma_alloc_coherent(dev, SPRD_IOMMU_PAGE_SIZE, 493 + &sdev->prot_page_pa, GFP_KERNEL); 494 + if (!sdev->prot_page_va) 495 + return -ENOMEM; 496 + 497 + platform_set_drvdata(pdev, sdev); 498 + sdev->dev = dev; 499 + 500 + /* All the client devices are in the same iommu-group */ 501 + sdev->group = iommu_group_alloc(); 502 + if (IS_ERR(sdev->group)) { 503 + ret = PTR_ERR(sdev->group); 504 + goto free_page; 505 + } 506 + 507 + ret = iommu_device_sysfs_add(&sdev->iommu, dev, NULL, dev_name(dev)); 508 + if (ret) 509 + goto put_group; 510 + 511 + ret = iommu_device_register(&sdev->iommu, &sprd_iommu_ops, dev); 512 + if (ret) 513 + goto remove_sysfs; 514 + 515 + if (!iommu_present(&platform_bus_type)) 516 + bus_set_iommu(&platform_bus_type, &sprd_iommu_ops); 517 + 518 + ret = sprd_iommu_clk_enable(sdev); 519 + if (ret) 520 + goto unregister_iommu; 521 + 522 + ret = sprd_iommu_get_version(sdev); 523 + if (ret < 0) { 524 + dev_err(dev, "IOMMU version(%d) is invalid.\n", ret); 525 + goto disable_clk; 526 + } 527 + sdev->ver = ret; 528 + 529 + return 0; 530 + 531 + disable_clk: 532 + sprd_iommu_clk_disable(sdev); 533 + unregister_iommu: 534 + iommu_device_unregister(&sdev->iommu); 535 + remove_sysfs: 536 + iommu_device_sysfs_remove(&sdev->iommu); 537 + put_group: 538 + iommu_group_put(sdev->group); 539 + free_page: 540 + dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa); 541 + return ret; 542 + } 543 + 544 + static int sprd_iommu_remove(struct platform_device *pdev) 545 + { 546 + struct sprd_iommu_device *sdev = platform_get_drvdata(pdev); 547 + 548 + dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa); 549 + 550 + iommu_group_put(sdev->group); 551 + sdev->group = NULL; 552 + 553 + bus_set_iommu(&platform_bus_type, NULL); 554 + 555 + platform_set_drvdata(pdev, NULL); 556 + iommu_device_sysfs_remove(&sdev->iommu); 557 + iommu_device_unregister(&sdev->iommu); 558 + 559 + return 0; 560 + } 561 + 562 + static struct platform_driver sprd_iommu_driver = { 563 + .driver = { 564 + .name = "sprd-iommu", 565 + .of_match_table = sprd_iommu_of_match, 566 + .suppress_bind_attrs = true, 567 + }, 568 + .probe = sprd_iommu_probe, 569 + .remove = sprd_iommu_remove, 570 + }; 571 + module_platform_driver(sprd_iommu_driver); 572 + 573 + MODULE_DESCRIPTION("IOMMU driver for Unisoc SoCs"); 574 + MODULE_ALIAS("platform:sprd-iommu"); 575 + MODULE_LICENSE("GPL");
+1 -4
drivers/iommu/sun50i-iommu.c
··· 968 968 if (ret) 969 969 goto err_free_group; 970 970 971 - iommu_device_set_ops(&iommu->iommu, &sun50i_iommu_ops); 972 - iommu_device_set_fwnode(&iommu->iommu, &pdev->dev.of_node->fwnode); 973 - 974 - ret = iommu_device_register(&iommu->iommu); 971 + ret = iommu_device_register(&iommu->iommu, &sun50i_iommu_ops, &pdev->dev); 975 972 if (ret) 976 973 goto err_remove_sysfs; 977 974
+1 -4
drivers/iommu/tegra-gart.c
··· 353 353 if (err) 354 354 goto free_gart; 355 355 356 - iommu_device_set_ops(&gart->iommu, &gart_iommu_ops); 357 - iommu_device_set_fwnode(&gart->iommu, dev->fwnode); 358 - 359 - err = iommu_device_register(&gart->iommu); 356 + err = iommu_device_register(&gart->iommu, &gart_iommu_ops, dev); 360 357 if (err) 361 358 goto remove_sysfs; 362 359
+1 -4
drivers/iommu/tegra-smmu.c
··· 1145 1145 if (err) 1146 1146 return ERR_PTR(err); 1147 1147 1148 - iommu_device_set_ops(&smmu->iommu, &tegra_smmu_ops); 1149 - iommu_device_set_fwnode(&smmu->iommu, dev->fwnode); 1150 - 1151 - err = iommu_device_register(&smmu->iommu); 1148 + err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev); 1152 1149 if (err) 1153 1150 goto remove_sysfs; 1154 1151
+2 -4
drivers/iommu/virtio-iommu.c
··· 945 945 .get_resv_regions = viommu_get_resv_regions, 946 946 .put_resv_regions = generic_iommu_put_resv_regions, 947 947 .of_xlate = viommu_of_xlate, 948 + .owner = THIS_MODULE, 948 949 }; 949 950 950 951 static int viommu_init_vqs(struct viommu_dev *viommu) ··· 1066 1065 if (ret) 1067 1066 goto err_free_vqs; 1068 1067 1069 - iommu_device_set_ops(&viommu->iommu, &viommu_ops); 1070 - iommu_device_set_fwnode(&viommu->iommu, parent_dev->fwnode); 1071 - 1072 - iommu_device_register(&viommu->iommu); 1068 + iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev); 1073 1069 1074 1070 #ifdef CONFIG_PCI 1075 1071 if (pci_bus_type.iommu_ops != &viommu_ops) {
+30 -9
drivers/misc/uacce/uacce.c
··· 385 385 kfree(uacce); 386 386 } 387 387 388 + static unsigned int uacce_enable_sva(struct device *parent, unsigned int flags) 389 + { 390 + if (!(flags & UACCE_DEV_SVA)) 391 + return flags; 392 + 393 + flags &= ~UACCE_DEV_SVA; 394 + 395 + if (iommu_dev_enable_feature(parent, IOMMU_DEV_FEAT_IOPF)) 396 + return flags; 397 + 398 + if (iommu_dev_enable_feature(parent, IOMMU_DEV_FEAT_SVA)) { 399 + iommu_dev_disable_feature(parent, IOMMU_DEV_FEAT_IOPF); 400 + return flags; 401 + } 402 + 403 + return flags | UACCE_DEV_SVA; 404 + } 405 + 406 + static void uacce_disable_sva(struct uacce_device *uacce) 407 + { 408 + if (!(uacce->flags & UACCE_DEV_SVA)) 409 + return; 410 + 411 + iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_SVA); 412 + iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_IOPF); 413 + } 414 + 388 415 /** 389 416 * uacce_alloc() - alloc an accelerator 390 417 * @parent: pointer of uacce parent device ··· 431 404 if (!uacce) 432 405 return ERR_PTR(-ENOMEM); 433 406 434 - if (flags & UACCE_DEV_SVA) { 435 - ret = iommu_dev_enable_feature(parent, IOMMU_DEV_FEAT_SVA); 436 - if (ret) 437 - flags &= ~UACCE_DEV_SVA; 438 - } 407 + flags = uacce_enable_sva(parent, flags); 439 408 440 409 uacce->parent = parent; 441 410 uacce->flags = flags; ··· 455 432 return uacce; 456 433 457 434 err_with_uacce: 458 - if (flags & UACCE_DEV_SVA) 459 - iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_SVA); 435 + uacce_disable_sva(uacce); 460 436 kfree(uacce); 461 437 return ERR_PTR(ret); 462 438 } ··· 509 487 mutex_unlock(&uacce->queues_lock); 510 488 511 489 /* disable sva now since no opened queues */ 512 - if (uacce->flags & UACCE_DEV_SVA) 513 - iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_SVA); 490 + uacce_disable_sva(uacce); 514 491 515 492 if (uacce->cdev) 516 493 cdev_device_del(uacce->cdev, &uacce->dev);
+3 -52
drivers/soc/fsl/qbman/qman_portal.c
··· 46 46 { 47 47 #ifdef CONFIG_FSL_PAMU 48 48 struct device *dev = pcfg->dev; 49 - int window_count = 1; 50 - struct iommu_domain_geometry geom_attr; 51 - struct pamu_stash_attribute stash_attr; 52 49 int ret; 53 50 54 51 pcfg->iommu_domain = iommu_domain_alloc(&platform_bus_type); ··· 53 56 dev_err(dev, "%s(): iommu_domain_alloc() failed", __func__); 54 57 goto no_iommu; 55 58 } 56 - geom_attr.aperture_start = 0; 57 - geom_attr.aperture_end = 58 - ((dma_addr_t)1 << min(8 * sizeof(dma_addr_t), (size_t)36)) - 1; 59 - geom_attr.force_aperture = true; 60 - ret = iommu_domain_set_attr(pcfg->iommu_domain, DOMAIN_ATTR_GEOMETRY, 61 - &geom_attr); 59 + ret = fsl_pamu_configure_l1_stash(pcfg->iommu_domain, cpu); 62 60 if (ret < 0) { 63 - dev_err(dev, "%s(): iommu_domain_set_attr() = %d", __func__, 64 - ret); 65 - goto out_domain_free; 66 - } 67 - ret = iommu_domain_set_attr(pcfg->iommu_domain, DOMAIN_ATTR_WINDOWS, 68 - &window_count); 69 - if (ret < 0) { 70 - dev_err(dev, "%s(): iommu_domain_set_attr() = %d", __func__, 71 - ret); 72 - goto out_domain_free; 73 - } 74 - stash_attr.cpu = cpu; 75 - stash_attr.cache = PAMU_ATTR_CACHE_L1; 76 - ret = iommu_domain_set_attr(pcfg->iommu_domain, 77 - DOMAIN_ATTR_FSL_PAMU_STASH, 78 - &stash_attr); 79 - if (ret < 0) { 80 - dev_err(dev, "%s(): iommu_domain_set_attr() = %d", 81 - __func__, ret); 82 - goto out_domain_free; 83 - } 84 - ret = iommu_domain_window_enable(pcfg->iommu_domain, 0, 0, 1ULL << 36, 85 - IOMMU_READ | IOMMU_WRITE); 86 - if (ret < 0) { 87 - dev_err(dev, "%s(): iommu_domain_window_enable() = %d", 61 + dev_err(dev, "%s(): fsl_pamu_configure_l1_stash() = %d", 88 62 __func__, ret); 89 63 goto out_domain_free; 90 64 } ··· 65 97 ret); 66 98 goto out_domain_free; 67 99 } 68 - ret = iommu_domain_set_attr(pcfg->iommu_domain, 69 - DOMAIN_ATTR_FSL_PAMU_ENABLE, 70 - &window_count); 71 - if (ret < 0) { 72 - dev_err(dev, "%s(): iommu_domain_set_attr() = %d", __func__, 73 - ret); 74 - goto out_detach_device; 75 - } 76 100 77 101 no_iommu: 78 102 #endif ··· 73 113 return; 74 114 75 115 #ifdef CONFIG_FSL_PAMU 76 - out_detach_device: 77 - iommu_detach_device(pcfg->iommu_domain, NULL); 78 116 out_domain_free: 79 117 iommu_domain_free(pcfg->iommu_domain); 80 118 pcfg->iommu_domain = NULL; ··· 127 169 unsigned int cpu) 128 170 { 129 171 #ifdef CONFIG_FSL_PAMU /* TODO */ 130 - struct pamu_stash_attribute stash_attr; 131 - int ret; 132 - 133 172 if (pcfg->iommu_domain) { 134 - stash_attr.cpu = cpu; 135 - stash_attr.cache = PAMU_ATTR_CACHE_L1; 136 - ret = iommu_domain_set_attr(pcfg->iommu_domain, 137 - DOMAIN_ATTR_FSL_PAMU_STASH, &stash_attr); 138 - if (ret < 0) { 173 + if (fsl_pamu_configure_l1_stash(pcfg->iommu_domain, cpu) < 0) { 139 174 dev_err(pcfg->dev, 140 175 "Failed to update pamu stash setting\n"); 141 176 return;
+13 -18
drivers/vfio/vfio_iommu_type1.c
··· 2248 2248 int ret; 2249 2249 bool resv_msi, msi_remap; 2250 2250 phys_addr_t resv_msi_base = 0; 2251 - struct iommu_domain_geometry geo; 2251 + struct iommu_domain_geometry *geo; 2252 2252 LIST_HEAD(iova_copy); 2253 2253 LIST_HEAD(group_resv_regions); 2254 2254 ··· 2316 2316 } 2317 2317 2318 2318 if (iommu->nesting) { 2319 - int attr = 1; 2320 - 2321 - ret = iommu_domain_set_attr(domain->domain, DOMAIN_ATTR_NESTING, 2322 - &attr); 2319 + ret = iommu_enable_nesting(domain->domain); 2323 2320 if (ret) 2324 2321 goto out_domain; 2325 2322 } ··· 2326 2329 goto out_domain; 2327 2330 2328 2331 /* Get aperture info */ 2329 - iommu_domain_get_attr(domain->domain, DOMAIN_ATTR_GEOMETRY, &geo); 2330 - 2331 - if (vfio_iommu_aper_conflict(iommu, geo.aperture_start, 2332 - geo.aperture_end)) { 2332 + geo = &domain->domain->geometry; 2333 + if (vfio_iommu_aper_conflict(iommu, geo->aperture_start, 2334 + geo->aperture_end)) { 2333 2335 ret = -EINVAL; 2334 2336 goto out_detach; 2335 2337 } ··· 2351 2355 if (ret) 2352 2356 goto out_detach; 2353 2357 2354 - ret = vfio_iommu_aper_resize(&iova_copy, geo.aperture_start, 2355 - geo.aperture_end); 2358 + ret = vfio_iommu_aper_resize(&iova_copy, geo->aperture_start, 2359 + geo->aperture_end); 2356 2360 if (ret) 2357 2361 goto out_detach; 2358 2362 ··· 2485 2489 struct list_head *iova_copy) 2486 2490 { 2487 2491 struct vfio_domain *domain; 2488 - struct iommu_domain_geometry geo; 2489 2492 struct vfio_iova *node; 2490 2493 dma_addr_t start = 0; 2491 2494 dma_addr_t end = (dma_addr_t)~0; ··· 2493 2498 return; 2494 2499 2495 2500 list_for_each_entry(domain, &iommu->domain_list, next) { 2496 - iommu_domain_get_attr(domain->domain, DOMAIN_ATTR_GEOMETRY, 2497 - &geo); 2498 - if (geo.aperture_start > start) 2499 - start = geo.aperture_start; 2500 - if (geo.aperture_end < end) 2501 - end = geo.aperture_end; 2501 + struct iommu_domain_geometry *geo = &domain->domain->geometry; 2502 + 2503 + if (geo->aperture_start > start) 2504 + start = geo->aperture_start; 2505 + if (geo->aperture_end < end) 2506 + end = geo->aperture_end; 2502 2507 } 2503 2508 2504 2509 /* Modify aperture limits. The new aper is either same or bigger */
+3 -7
drivers/vhost/vdpa.c
··· 836 836 static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v) 837 837 { 838 838 struct vdpa_iova_range *range = &v->range; 839 - struct iommu_domain_geometry geo; 840 839 struct vdpa_device *vdpa = v->vdpa; 841 840 const struct vdpa_config_ops *ops = vdpa->config; 842 841 843 842 if (ops->get_iova_range) { 844 843 *range = ops->get_iova_range(vdpa); 845 - } else if (v->domain && 846 - !iommu_domain_get_attr(v->domain, 847 - DOMAIN_ATTR_GEOMETRY, &geo) && 848 - geo.force_aperture) { 849 - range->first = geo.aperture_start; 850 - range->last = geo.aperture_end; 844 + } else if (v->domain && v->domain->geometry.force_aperture) { 845 + range->first = v->domain->geometry.aperture_start; 846 + range->last = v->domain->geometry.aperture_end; 851 847 } else { 852 848 range->first = 0; 853 849 range->last = ULLONG_MAX;
+12 -18
include/linux/amd-iommu.h
··· 10 10 11 11 #include <linux/types.h> 12 12 13 + struct amd_iommu; 14 + 13 15 /* 14 16 * This is mainly used to communicate information back-and-forth 15 17 * between SVM and IOMMU for setting up and tearing down posted ··· 33 31 34 32 extern int amd_iommu_detect(void); 35 33 extern int amd_iommu_init_hardware(void); 36 - 37 - /** 38 - * amd_iommu_enable_device_erratum() - Enable erratum workaround for device 39 - * in the IOMMUv2 driver 40 - * @pdev: The PCI device the workaround is necessary for 41 - * @erratum: The erratum workaround to enable 42 - * 43 - * The function needs to be called before amd_iommu_init_device(). 44 - * Possible values for the erratum number are for now: 45 - * - AMD_PRI_DEV_ERRATUM_ENABLE_RESET - Reset PRI capability when PRI 46 - * is enabled 47 - * - AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE - Limit number of outstanding PRI 48 - * requests to one 49 - */ 50 - #define AMD_PRI_DEV_ERRATUM_ENABLE_RESET 0 51 - #define AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE 1 52 - 53 - extern void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum); 54 34 55 35 /** 56 36 * amd_iommu_init_device() - Init device for use with IOMMUv2 driver ··· 195 211 return 0; 196 212 } 197 213 #endif /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */ 214 + 215 + int amd_iommu_get_num_iommus(void); 216 + bool amd_iommu_pc_supported(void); 217 + u8 amd_iommu_pc_get_max_banks(unsigned int idx); 218 + u8 amd_iommu_pc_get_max_counters(unsigned int idx); 219 + int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, 220 + u64 *value); 221 + int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, 222 + u64 *value); 223 + struct amd_iommu *get_amd_iommu(unsigned int idx); 198 224 199 225 #endif /* _ASM_X86_AMD_IOMMU_H */
+1 -1
include/linux/cpuhotplug.h
··· 57 57 CPUHP_PAGE_ALLOC_DEAD, 58 58 CPUHP_NET_DEV_DEAD, 59 59 CPUHP_PCI_XGENE_DEAD, 60 - CPUHP_IOMMU_INTEL_DEAD, 60 + CPUHP_IOMMU_IOVA_DEAD, 61 61 CPUHP_LUSTRE_CFS_DEAD, 62 62 CPUHP_AP_ARM_CACHE_B15_RAC_DEAD, 63 63 CPUHP_PADATA_DEAD,
+2 -5
include/linux/dma-iommu.h
··· 40 40 void iommu_dma_free_cpu_cached_iovas(unsigned int cpu, 41 41 struct iommu_domain *domain); 42 42 43 + extern bool iommu_dma_forcedac; 44 + 43 45 #else /* CONFIG_IOMMU_DMA */ 44 46 45 47 struct iommu_domain; ··· 80 78 } 81 79 82 80 static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list) 83 - { 84 - } 85 - 86 - static inline void iommu_dma_free_cpu_cached_iovas(unsigned int cpu, 87 - struct iommu_domain *domain) 88 81 { 89 82 } 90 83
+7 -3
include/linux/intel-iommu.h
··· 20 20 #include <linux/io-64-nonatomic-lo-hi.h> 21 21 #include <linux/dmar.h> 22 22 #include <linux/ioasid.h> 23 + #include <linux/bitfield.h> 23 24 24 25 #include <asm/cacheflush.h> 25 26 #include <asm/iommu.h> ··· 81 80 #define DMAR_IQ_SHIFT 4 /* Invalidation queue head/tail shift */ 82 81 #define DMAR_IQA_REG 0x90 /* Invalidation queue addr register */ 83 82 #define DMAR_ICS_REG 0x9c /* Invalidation complete status register */ 83 + #define DMAR_IQER_REG 0xb0 /* Invalidation queue error record register */ 84 84 #define DMAR_IRTA_REG 0xb8 /* Interrupt remapping table addr register */ 85 85 #define DMAR_PQH_REG 0xc0 /* Page request queue head register */ 86 86 #define DMAR_PQT_REG 0xc8 /* Page request queue tail register */ ··· 127 125 #define DMAR_VCCAP_REG 0xe00 /* Virtual command capability register */ 128 126 #define DMAR_VCMD_REG 0xe10 /* Virtual command register */ 129 127 #define DMAR_VCRSP_REG 0xe20 /* Virtual command response register */ 128 + 129 + #define DMAR_IQER_REG_IQEI(reg) FIELD_GET(GENMASK_ULL(3, 0), reg) 130 + #define DMAR_IQER_REG_ITESID(reg) FIELD_GET(GENMASK_ULL(47, 32), reg) 131 + #define DMAR_IQER_REG_ICESID(reg) FIELD_GET(GENMASK_ULL(63, 48), reg) 130 132 131 133 #define OFFSET_STRIDE (9) 132 134 ··· 378 372 /* PASID cache invalidation granu */ 379 373 #define QI_PC_ALL_PASIDS 0 380 374 #define QI_PC_PASID_SEL 1 375 + #define QI_PC_GLOBAL 3 381 376 382 377 #define QI_EIOTLB_ADDR(addr) ((u64)(addr) & VTD_PAGE_MASK) 383 378 #define QI_EIOTLB_IH(ih) (((u64)ih) << 6) ··· 770 763 int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt, 771 764 struct iommu_page_response *msg); 772 765 773 - struct svm_dev_ops; 774 - 775 766 struct intel_svm_dev { 776 767 struct list_head list; 777 768 struct rcu_head rcu; 778 769 struct device *dev; 779 770 struct intel_iommu *iommu; 780 - struct svm_dev_ops *ops; 781 771 struct iommu_sva sva; 782 772 u32 pasid; 783 773 int users;
+3 -20
include/linux/intel-svm.h
··· 8 8 #ifndef __INTEL_SVM_H__ 9 9 #define __INTEL_SVM_H__ 10 10 11 - struct device; 12 - 13 - struct svm_dev_ops { 14 - void (*fault_cb)(struct device *dev, u32 pasid, u64 address, 15 - void *private, int rwxp, int response); 16 - }; 17 - 18 11 /* Values for rxwp in fault_cb callback */ 19 12 #define SVM_REQ_READ (1<<3) 20 13 #define SVM_REQ_WRITE (1<<2) 21 14 #define SVM_REQ_EXEC (1<<1) 22 15 #define SVM_REQ_PRIV (1<<0) 23 - 24 - /* 25 - * The SVM_FLAG_PRIVATE_PASID flag requests a PASID which is *not* the "main" 26 - * PASID for the current process. Even if a PASID already exists, a new one 27 - * will be allocated. And the PASID allocated with SVM_FLAG_PRIVATE_PASID 28 - * will not be given to subsequent callers. This facility allows a driver to 29 - * disambiguate between multiple device contexts which access the same MM, 30 - * if there is no other way to do so. It should be used sparingly, if at all. 31 - */ 32 - #define SVM_FLAG_PRIVATE_PASID (1<<0) 33 16 34 17 /* 35 18 * The SVM_FLAG_SUPERVISOR_MODE flag requests a PASID which can be used only ··· 25 42 * It is unlikely that we will ever hook into flush_tlb_kernel_range() to 26 43 * do such IOTLB flushes automatically. 27 44 */ 28 - #define SVM_FLAG_SUPERVISOR_MODE (1<<1) 45 + #define SVM_FLAG_SUPERVISOR_MODE BIT(0) 29 46 /* 30 47 * The SVM_FLAG_GUEST_MODE flag is used when a PASID bind is for guest 31 48 * processes. Compared to the host bind, the primary differences are: 32 49 * 1. mm life cycle management 33 50 * 2. fault reporting 34 51 */ 35 - #define SVM_FLAG_GUEST_MODE (1<<2) 52 + #define SVM_FLAG_GUEST_MODE BIT(1) 36 53 /* 37 54 * The SVM_FLAG_GUEST_PASID flag is used when a guest has its own PASID space, 38 55 * which requires guest and host PASID translation at both directions. 39 56 */ 40 - #define SVM_FLAG_GUEST_PASID (1<<3) 57 + #define SVM_FLAG_GUEST_PASID BIT(2) 41 58 42 59 #endif /* __INTEL_SVM_H__ */
-4
include/linux/io-pgtable.h
··· 204 204 205 205 #define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops) 206 206 207 - struct io_pgtable_domain_attr { 208 - unsigned long quirks; 209 - }; 210 - 211 207 static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop) 212 208 { 213 209 if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_all)
+40 -101
include/linux/iommu.h
··· 96 96 IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */ 97 97 }; 98 98 99 - /* 100 - * Following constraints are specifc to FSL_PAMUV1: 101 - * -aperture must be power of 2, and naturally aligned 102 - * -number of windows must be power of 2, and address space size 103 - * of each window is determined by aperture size / # of windows 104 - * -the actual size of the mapped region of a window must be power 105 - * of 2 starting with 4KB and physical address must be naturally 106 - * aligned. 107 - * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints. 108 - * The caller can invoke iommu_domain_get_attr to check if the underlying 109 - * iommu implementation supports these constraints. 110 - */ 111 - 112 - enum iommu_attr { 113 - DOMAIN_ATTR_GEOMETRY, 114 - DOMAIN_ATTR_PAGING, 115 - DOMAIN_ATTR_WINDOWS, 116 - DOMAIN_ATTR_FSL_PAMU_STASH, 117 - DOMAIN_ATTR_FSL_PAMU_ENABLE, 118 - DOMAIN_ATTR_FSL_PAMUV1, 119 - DOMAIN_ATTR_NESTING, /* two stages of translation */ 120 - DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, 121 - DOMAIN_ATTR_IO_PGTABLE_CFG, 122 - DOMAIN_ATTR_MAX, 123 - }; 124 - 125 99 /* These are the possible reserved region types */ 126 100 enum iommu_resv_type { 127 101 /* Memory regions which must be mapped 1:1 at all times */ ··· 130 156 enum iommu_resv_type type; 131 157 }; 132 158 133 - /* Per device IOMMU features */ 159 + /** 160 + * enum iommu_dev_features - Per device IOMMU features 161 + * @IOMMU_DEV_FEAT_AUX: Auxiliary domain feature 162 + * @IOMMU_DEV_FEAT_SVA: Shared Virtual Addresses 163 + * @IOMMU_DEV_FEAT_IOPF: I/O Page Faults such as PRI or Stall. Generally 164 + * enabling %IOMMU_DEV_FEAT_SVA requires 165 + * %IOMMU_DEV_FEAT_IOPF, but some devices manage I/O Page 166 + * Faults themselves instead of relying on the IOMMU. When 167 + * supported, this feature must be enabled before and 168 + * disabled after %IOMMU_DEV_FEAT_SVA. 169 + * 170 + * Device drivers query whether a feature is supported using 171 + * iommu_dev_has_feature(), and enable it using iommu_dev_enable_feature(). 172 + */ 134 173 enum iommu_dev_features { 135 - IOMMU_DEV_FEAT_AUX, /* Aux-domain feature */ 136 - IOMMU_DEV_FEAT_SVA, /* Shared Virtual Addresses */ 174 + IOMMU_DEV_FEAT_AUX, 175 + IOMMU_DEV_FEAT_SVA, 176 + IOMMU_DEV_FEAT_IOPF, 137 177 }; 138 178 139 179 #define IOMMU_PASID_INVALID (-1U) ··· 191 203 * @probe_finalize: Do final setup work after the device is added to an IOMMU 192 204 * group and attached to the groups domain 193 205 * @device_group: find iommu group for a particular device 194 - * @domain_get_attr: Query domain attributes 195 - * @domain_set_attr: Change domain attributes 206 + * @enable_nesting: Enable nesting 207 + * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) 196 208 * @get_resv_regions: Request list of reserved regions for a device 197 209 * @put_resv_regions: Free list of reserved regions for a device 198 210 * @apply_resv_region: Temporary helper call-back for iova reserved ranges 199 - * @domain_window_enable: Configure and enable a particular window for a domain 200 - * @domain_window_disable: Disable a particular window for a domain 201 211 * @of_xlate: add OF master IDs to iommu grouping 202 212 * @is_attach_deferred: Check if domain attach should be deferred from iommu 203 213 * driver init to device driver init (default no) ··· 241 255 void (*release_device)(struct device *dev); 242 256 void (*probe_finalize)(struct device *dev); 243 257 struct iommu_group *(*device_group)(struct device *dev); 244 - int (*domain_get_attr)(struct iommu_domain *domain, 245 - enum iommu_attr attr, void *data); 246 - int (*domain_set_attr)(struct iommu_domain *domain, 247 - enum iommu_attr attr, void *data); 258 + int (*enable_nesting)(struct iommu_domain *domain); 259 + int (*set_pgtable_quirks)(struct iommu_domain *domain, 260 + unsigned long quirks); 248 261 249 262 /* Request/Free a list of reserved regions for a device */ 250 263 void (*get_resv_regions)(struct device *dev, struct list_head *list); ··· 251 266 void (*apply_resv_region)(struct device *dev, 252 267 struct iommu_domain *domain, 253 268 struct iommu_resv_region *region); 254 - 255 - /* Window handling functions */ 256 - int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr, 257 - phys_addr_t paddr, u64 size, int prot); 258 - void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr); 259 269 260 270 int (*of_xlate)(struct device *dev, struct of_phandle_args *args); 261 271 bool (*is_attach_deferred)(struct iommu_domain *domain, struct device *dev); ··· 333 353 * struct dev_iommu - Collection of per-device IOMMU data 334 354 * 335 355 * @fault_param: IOMMU detected device fault reporting data 356 + * @iopf_param: I/O Page Fault queue and data 336 357 * @fwspec: IOMMU fwspec data 337 358 * @iommu_dev: IOMMU device this device is linked to 338 359 * @priv: IOMMU Driver private data ··· 344 363 struct dev_iommu { 345 364 struct mutex lock; 346 365 struct iommu_fault_param *fault_param; 366 + struct iopf_device_param *iopf_param; 347 367 struct iommu_fwspec *fwspec; 348 368 struct iommu_device *iommu_dev; 349 369 void *priv; 350 370 }; 351 371 352 - int iommu_device_register(struct iommu_device *iommu); 372 + int iommu_device_register(struct iommu_device *iommu, 373 + const struct iommu_ops *ops, 374 + struct device *hwdev); 353 375 void iommu_device_unregister(struct iommu_device *iommu); 354 376 int iommu_device_sysfs_add(struct iommu_device *iommu, 355 377 struct device *parent, ··· 362 378 int iommu_device_link(struct iommu_device *iommu, struct device *link); 363 379 void iommu_device_unlink(struct iommu_device *iommu, struct device *link); 364 380 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain); 365 - 366 - static inline void __iommu_device_set_ops(struct iommu_device *iommu, 367 - const struct iommu_ops *ops) 368 - { 369 - iommu->ops = ops; 370 - } 371 - 372 - #define iommu_device_set_ops(iommu, ops) \ 373 - do { \ 374 - struct iommu_ops *__ops = (struct iommu_ops *)(ops); \ 375 - __ops->owner = THIS_MODULE; \ 376 - __iommu_device_set_ops(iommu, __ops); \ 377 - } while (0) 378 - 379 - static inline void iommu_device_set_fwnode(struct iommu_device *iommu, 380 - struct fwnode_handle *fwnode) 381 - { 382 - iommu->fwnode = fwnode; 383 - } 384 381 385 382 static inline struct iommu_device *dev_to_iommu_device(struct device *dev) 386 383 { ··· 472 507 extern int iommu_group_id(struct iommu_group *group); 473 508 extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *); 474 509 475 - extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr, 476 - void *data); 477 - extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr, 478 - void *data); 510 + int iommu_enable_nesting(struct iommu_domain *domain); 511 + int iommu_set_pgtable_quirks(struct iommu_domain *domain, 512 + unsigned long quirks); 479 513 480 - /* Window handling function prototypes */ 481 - extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, 482 - phys_addr_t offset, u64 size, 483 - int prot); 514 + void iommu_set_dma_strict(bool val); 515 + bool iommu_get_dma_strict(struct iommu_domain *domain); 484 516 485 517 extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev, 486 518 unsigned long iova, int flags); ··· 509 547 * structure can be rewritten. 510 548 */ 511 549 if (gather->pgsize != size || 512 - end < gather->start || start > gather->end) { 550 + end + 1 < gather->start || start > gather->end + 1) { 513 551 if (gather->pgsize) 514 552 iommu_iotlb_sync(domain, gather); 515 553 gather->pgsize = size; ··· 533 571 * struct iommu_fwspec - per-device IOMMU instance data 534 572 * @ops: ops for this device's IOMMU 535 573 * @iommu_fwnode: firmware handle for this device's IOMMU 536 - * @iommu_priv: IOMMU driver private data for this device 537 - * @num_pasid_bits: number of PASID bits supported by this device 574 + * @flags: IOMMU_FWSPEC_* flags 538 575 * @num_ids: number of associated device IDs 539 576 * @ids: IDs which this device may present to the IOMMU 540 577 */ ··· 541 580 const struct iommu_ops *ops; 542 581 struct fwnode_handle *iommu_fwnode; 543 582 u32 flags; 544 - u32 num_pasid_bits; 545 583 unsigned int num_ids; 546 584 u32 ids[]; 547 585 }; ··· 702 742 { 703 743 } 704 744 705 - static inline int iommu_domain_window_enable(struct iommu_domain *domain, 706 - u32 wnd_nr, phys_addr_t paddr, 707 - u64 size, int prot) 708 - { 709 - return -ENODEV; 710 - } 711 - 712 745 static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) 713 746 { 714 747 return 0; ··· 842 889 return -ENODEV; 843 890 } 844 891 845 - static inline int iommu_domain_get_attr(struct iommu_domain *domain, 846 - enum iommu_attr attr, void *data) 892 + static inline int iommu_set_pgtable_quirks(struct iommu_domain *domain, 893 + unsigned long quirks) 847 894 { 848 - return -EINVAL; 895 + return 0; 849 896 } 850 897 851 - static inline int iommu_domain_set_attr(struct iommu_domain *domain, 852 - enum iommu_attr attr, void *data) 853 - { 854 - return -EINVAL; 855 - } 856 - 857 - static inline int iommu_device_register(struct iommu_device *iommu) 898 + static inline int iommu_device_register(struct iommu_device *iommu, 899 + const struct iommu_ops *ops, 900 + struct device *hwdev) 858 901 { 859 902 return -ENODEV; 860 - } 861 - 862 - static inline void iommu_device_set_ops(struct iommu_device *iommu, 863 - const struct iommu_ops *ops) 864 - { 865 - } 866 - 867 - static inline void iommu_device_set_fwnode(struct iommu_device *iommu, 868 - struct fwnode_handle *fwnode) 869 - { 870 903 } 871 904 872 905 static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
+1 -5
include/linux/iova.h
··· 95 95 flush-queues */ 96 96 atomic_t fq_timer_on; /* 1 when timer is active, 0 97 97 when not */ 98 + struct hlist_node cpuhp_dead; 98 99 }; 99 100 100 101 static inline unsigned long iova_size(struct iova *iova) ··· 157 156 iova_flush_cb flush_cb, iova_entry_dtor entry_dtor); 158 157 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn); 159 158 void put_iova_domain(struct iova_domain *iovad); 160 - void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad); 161 159 #else 162 160 static inline int iova_cache_get(void) 163 161 { ··· 233 233 { 234 234 } 235 235 236 - static inline void free_cpu_cached_iovas(unsigned int cpu, 237 - struct iova_domain *iovad) 238 - { 239 - } 240 236 #endif 241 237 242 238 #endif
-120
include/trace/events/intel_iommu.h
··· 15 15 #include <linux/tracepoint.h> 16 16 #include <linux/intel-iommu.h> 17 17 18 - DECLARE_EVENT_CLASS(dma_map, 19 - TP_PROTO(struct device *dev, dma_addr_t dev_addr, phys_addr_t phys_addr, 20 - size_t size), 21 - 22 - TP_ARGS(dev, dev_addr, phys_addr, size), 23 - 24 - TP_STRUCT__entry( 25 - __string(dev_name, dev_name(dev)) 26 - __field(dma_addr_t, dev_addr) 27 - __field(phys_addr_t, phys_addr) 28 - __field(size_t, size) 29 - ), 30 - 31 - TP_fast_assign( 32 - __assign_str(dev_name, dev_name(dev)); 33 - __entry->dev_addr = dev_addr; 34 - __entry->phys_addr = phys_addr; 35 - __entry->size = size; 36 - ), 37 - 38 - TP_printk("dev=%s dev_addr=0x%llx phys_addr=0x%llx size=%zu", 39 - __get_str(dev_name), 40 - (unsigned long long)__entry->dev_addr, 41 - (unsigned long long)__entry->phys_addr, 42 - __entry->size) 43 - ); 44 - 45 - DEFINE_EVENT(dma_map, map_single, 46 - TP_PROTO(struct device *dev, dma_addr_t dev_addr, phys_addr_t phys_addr, 47 - size_t size), 48 - TP_ARGS(dev, dev_addr, phys_addr, size) 49 - ); 50 - 51 - DEFINE_EVENT(dma_map, bounce_map_single, 52 - TP_PROTO(struct device *dev, dma_addr_t dev_addr, phys_addr_t phys_addr, 53 - size_t size), 54 - TP_ARGS(dev, dev_addr, phys_addr, size) 55 - ); 56 - 57 - DECLARE_EVENT_CLASS(dma_unmap, 58 - TP_PROTO(struct device *dev, dma_addr_t dev_addr, size_t size), 59 - 60 - TP_ARGS(dev, dev_addr, size), 61 - 62 - TP_STRUCT__entry( 63 - __string(dev_name, dev_name(dev)) 64 - __field(dma_addr_t, dev_addr) 65 - __field(size_t, size) 66 - ), 67 - 68 - TP_fast_assign( 69 - __assign_str(dev_name, dev_name(dev)); 70 - __entry->dev_addr = dev_addr; 71 - __entry->size = size; 72 - ), 73 - 74 - TP_printk("dev=%s dev_addr=0x%llx size=%zu", 75 - __get_str(dev_name), 76 - (unsigned long long)__entry->dev_addr, 77 - __entry->size) 78 - ); 79 - 80 - DEFINE_EVENT(dma_unmap, unmap_single, 81 - TP_PROTO(struct device *dev, dma_addr_t dev_addr, size_t size), 82 - TP_ARGS(dev, dev_addr, size) 83 - ); 84 - 85 - DEFINE_EVENT(dma_unmap, unmap_sg, 86 - TP_PROTO(struct device *dev, dma_addr_t dev_addr, size_t size), 87 - TP_ARGS(dev, dev_addr, size) 88 - ); 89 - 90 - DEFINE_EVENT(dma_unmap, bounce_unmap_single, 91 - TP_PROTO(struct device *dev, dma_addr_t dev_addr, size_t size), 92 - TP_ARGS(dev, dev_addr, size) 93 - ); 94 - 95 - DECLARE_EVENT_CLASS(dma_map_sg, 96 - TP_PROTO(struct device *dev, int index, int total, 97 - struct scatterlist *sg), 98 - 99 - TP_ARGS(dev, index, total, sg), 100 - 101 - TP_STRUCT__entry( 102 - __string(dev_name, dev_name(dev)) 103 - __field(dma_addr_t, dev_addr) 104 - __field(phys_addr_t, phys_addr) 105 - __field(size_t, size) 106 - __field(int, index) 107 - __field(int, total) 108 - ), 109 - 110 - TP_fast_assign( 111 - __assign_str(dev_name, dev_name(dev)); 112 - __entry->dev_addr = sg->dma_address; 113 - __entry->phys_addr = sg_phys(sg); 114 - __entry->size = sg->dma_length; 115 - __entry->index = index; 116 - __entry->total = total; 117 - ), 118 - 119 - TP_printk("dev=%s [%d/%d] dev_addr=0x%llx phys_addr=0x%llx size=%zu", 120 - __get_str(dev_name), __entry->index, __entry->total, 121 - (unsigned long long)__entry->dev_addr, 122 - (unsigned long long)__entry->phys_addr, 123 - __entry->size) 124 - ); 125 - 126 - DEFINE_EVENT(dma_map_sg, map_sg, 127 - TP_PROTO(struct device *dev, int index, int total, 128 - struct scatterlist *sg), 129 - TP_ARGS(dev, index, total, sg) 130 - ); 131 - 132 - DEFINE_EVENT(dma_map_sg, bounce_map_sg, 133 - TP_PROTO(struct device *dev, int index, int total, 134 - struct scatterlist *sg), 135 - TP_ARGS(dev, index, total, sg) 136 - ); 137 - 138 18 TRACE_EVENT(qi_submit, 139 19 TP_PROTO(struct intel_iommu *iommu, u64 qw0, u64 qw1, u64 qw2, u64 qw3), 140 20
+2 -1
include/uapi/linux/iommu.h
··· 288 288 #define IOMMU_SVA_VTD_GPASID_PWT (1 << 3) /* page-level write through */ 289 289 #define IOMMU_SVA_VTD_GPASID_EMTE (1 << 4) /* extended mem type enable */ 290 290 #define IOMMU_SVA_VTD_GPASID_CD (1 << 5) /* PASID-level cache disable */ 291 - #define IOMMU_SVA_VTD_GPASID_LAST (1 << 6) 291 + #define IOMMU_SVA_VTD_GPASID_WPE (1 << 6) /* Write protect enable */ 292 + #define IOMMU_SVA_VTD_GPASID_LAST (1 << 7) 292 293 __u64 flags; 293 294 __u32 pat; 294 295 __u32 emt;